2 * pass2.c --- check directory structure
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o
7 * This file may be redistributed under the terms of the GNU Public
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:
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
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 '..'.
27 * To minimize disk seek time, the directory blocks are processed in
28 * sorted order of block numbers.
30 * Pass 2 also collects the following information:
31 * - The inode numbers of the subdirectories for each directory.
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
39 * Pass 2 frees the following data structures
40 * - The inode_bad_map bitmap
41 * - The inode_reg_map bitmap
44 #define _GNU_SOURCE 1 /* get strnlen() */
50 #include "support/dict.h"
52 #ifdef NO_INLINE_FUNCS
55 #define _INLINE_ inline
58 /* #define DX_DEBUG */
61 * Keeps track of how many times an inode is referenced.
63 static void deallocate_inode(e2fsck_t ctx
, ext2_ino_t ino
, char* block_buf
);
64 static int check_dir_block2(ext2_filsys fs
,
65 struct ext2_db_entry2
*dir_blocks_info
,
67 static int check_dir_block(ext2_filsys fs
,
68 struct ext2_db_entry2
*dir_blocks_info
,
70 static int allocate_dir_block(e2fsck_t ctx
,
71 struct ext2_db_entry2
*dir_blocks_info
,
72 char *buf
, struct problem_context
*pctx
);
73 static void clear_htree(e2fsck_t ctx
, ext2_ino_t ino
);
74 static int htree_depth(struct dx_dir_info
*dx_dir
,
75 struct dx_dirblock_info
*dx_db
);
76 static EXT2_QSORT_TYPE
special_dir_block_cmp(const void *a
, const void *b
);
78 struct check_dir_struct
{
80 struct problem_context pctx
;
83 unsigned long long list_offset
;
84 unsigned long long ra_entries
;
85 unsigned long long next_ra_off
;
88 void e2fsck_pass2(e2fsck_t ctx
)
90 struct ext2_super_block
*sb
= ctx
->fs
->super
;
91 struct problem_context pctx
;
92 ext2_filsys fs
= ctx
->fs
;
95 struct resource_track rtrack
;
97 struct check_dir_struct cd
;
98 struct dx_dir_info
*dx_dir
;
99 struct dx_dirblock_info
*dx_db
, *dx_parent
;
104 int (*check_dir_func
)(ext2_filsys fs
,
105 struct ext2_db_entry2
*dir_blocks_info
,
108 init_resource_track(&rtrack
, ctx
->fs
->io
);
109 clear_problem_context(&cd
.pctx
);
112 mtrace_print("Pass 2");
115 if (!(ctx
->options
& E2F_OPT_PREEN
))
116 fix_problem(ctx
, PR_2_PASS_HEADER
, &cd
.pctx
);
118 cd
.pctx
.errcode
= e2fsck_setup_icount(ctx
, "inode_count",
119 EXT2_ICOUNT_OPT_INCREMENT
,
120 ctx
->inode_link_info
, &ctx
->inode_count
);
121 if (cd
.pctx
.errcode
) {
122 fix_problem(ctx
, PR_2_ALLOCATE_ICOUNT
, &cd
.pctx
);
123 ctx
->flags
|= E2F_FLAG_ABORT
;
126 buf
= (char *) e2fsck_allocate_memory(ctx
, 2*fs
->blocksize
,
127 "directory scan buffer");
130 * Set up the parent pointer for the root directory, if
131 * present. (If the root directory is not present, we will
132 * create it in pass 3.)
134 (void) e2fsck_dir_info_set_parent(ctx
, EXT2_ROOT_INO
, EXT2_ROOT_INO
);
139 cd
.max
= ext2fs_dblist_count2(fs
->dblist
);
141 cd
.ra_entries
= ctx
->readahead_kb
* 1024 / ctx
->fs
->blocksize
;
145 (void) (ctx
->progress
)(ctx
, 2, 0, cd
.max
);
147 if (ext2fs_has_feature_dir_index(fs
->super
))
148 ext2fs_dblist_sort2(fs
->dblist
, special_dir_block_cmp
);
150 check_dir_func
= cd
.ra_entries
? check_dir_block2
: check_dir_block
;
151 cd
.pctx
.errcode
= ext2fs_dblist_iterate2(fs
->dblist
, check_dir_func
,
153 if (ctx
->flags
& E2F_FLAG_RESTART_LATER
) {
154 ctx
->flags
|= E2F_FLAG_RESTART
;
155 ctx
->flags
&= ~E2F_FLAG_RESTART_LATER
;
158 if (ctx
->flags
& E2F_FLAG_RUN_RETURN
)
161 if (cd
.pctx
.errcode
) {
162 fix_problem(ctx
, PR_2_DBLIST_ITERATE
, &cd
.pctx
);
163 ctx
->flags
|= E2F_FLAG_ABORT
;
167 for (i
=0; (dx_dir
= e2fsck_dx_dir_info_iter(ctx
, &i
)) != 0;) {
168 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
170 if (e2fsck_dir_will_be_rehashed(ctx
, dx_dir
->ino
) ||
171 dx_dir
->numblocks
== 0)
173 clear_problem_context(&pctx
);
175 pctx
.dir
= dx_dir
->ino
;
176 dx_db
= dx_dir
->dx_block
;
177 if (dx_db
->flags
& DX_FLAG_REFERENCED
)
178 dx_db
->flags
|= DX_FLAG_DUP_REF
;
180 dx_db
->flags
|= DX_FLAG_REFERENCED
;
182 * Find all of the first and last leaf blocks, and
183 * update their parent's min and max hash values
185 for (b
=0, dx_db
= dx_dir
->dx_block
;
186 b
< dx_dir
->numblocks
;
188 if ((dx_db
->type
!= DX_DIRBLOCK_LEAF
) ||
189 !(dx_db
->flags
& (DX_FLAG_FIRST
| DX_FLAG_LAST
)))
191 dx_parent
= &dx_dir
->dx_block
[dx_db
->parent
];
193 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
195 if (dx_db
->flags
& DX_FLAG_FIRST
)
196 dx_parent
->min_hash
= dx_db
->min_hash
;
198 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
200 if (dx_db
->flags
& DX_FLAG_LAST
)
201 dx_parent
->max_hash
= dx_db
->max_hash
;
204 for (b
=0, dx_db
= dx_dir
->dx_block
;
205 b
< dx_dir
->numblocks
;
208 pctx
.group
= dx_db
->parent
;
210 if (!(dx_db
->flags
& DX_FLAG_FIRST
) &&
211 (dx_db
->min_hash
< dx_db
->node_min_hash
)) {
212 pctx
.blk
= dx_db
->min_hash
;
213 pctx
.blk2
= dx_db
->node_min_hash
;
214 code
= PR_2_HTREE_MIN_HASH
;
215 fix_problem(ctx
, code
, &pctx
);
218 if (dx_db
->type
== DX_DIRBLOCK_LEAF
) {
219 depth
= htree_depth(dx_dir
, dx_db
);
220 if (depth
!= dx_dir
->depth
) {
221 pctx
.num
= dx_dir
->depth
;
222 code
= PR_2_HTREE_BAD_DEPTH
;
223 fix_problem(ctx
, code
, &pctx
);
228 * This test doesn't apply for the root block
232 (dx_db
->max_hash
> dx_db
->node_max_hash
)) {
233 pctx
.blk
= dx_db
->max_hash
;
234 pctx
.blk2
= dx_db
->node_max_hash
;
235 code
= PR_2_HTREE_MAX_HASH
;
236 fix_problem(ctx
, code
, &pctx
);
239 if (!(dx_db
->flags
& DX_FLAG_REFERENCED
)) {
240 code
= PR_2_HTREE_NOTREF
;
241 fix_problem(ctx
, code
, &pctx
);
243 } else if (dx_db
->flags
& DX_FLAG_DUP_REF
) {
244 code
= PR_2_HTREE_DUPREF
;
245 fix_problem(ctx
, code
, &pctx
);
249 if (bad_dir
&& fix_problem(ctx
, PR_2_HTREE_CLEAR
, &pctx
)) {
250 clear_htree(ctx
, dx_dir
->ino
);
251 dx_dir
->numblocks
= 0;
254 e2fsck_free_dx_dir_info(ctx
);
256 ext2fs_free_mem(&buf
);
257 ext2fs_free_dblist(fs
->dblist
);
259 if (ctx
->inode_bad_map
) {
260 ext2fs_free_inode_bitmap(ctx
->inode_bad_map
);
261 ctx
->inode_bad_map
= 0;
263 if (ctx
->inode_reg_map
) {
264 ext2fs_free_inode_bitmap(ctx
->inode_reg_map
);
265 ctx
->inode_reg_map
= 0;
267 if (ctx
->encrypted_dirs
) {
268 ext2fs_u32_list_free(ctx
->encrypted_dirs
);
269 ctx
->encrypted_dirs
= 0;
272 clear_problem_context(&pctx
);
273 if (ctx
->large_files
) {
274 if (!ext2fs_has_feature_large_file(sb
) &&
275 fix_problem(ctx
, PR_2_FEATURE_LARGE_FILES
, &pctx
)) {
276 ext2fs_set_feature_large_file(sb
);
277 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
278 ext2fs_mark_super_dirty(fs
);
280 if (sb
->s_rev_level
== EXT2_GOOD_OLD_REV
&&
281 fix_problem(ctx
, PR_1_FS_REV_LEVEL
, &pctx
)) {
282 ext2fs_update_dynamic_rev(fs
);
283 ext2fs_mark_super_dirty(fs
);
287 print_resource_track(ctx
, _("Pass 2"), &rtrack
, fs
->io
);
289 ext2fs_free_mem(&buf
);
292 #define MAX_DEPTH 32000
293 static int htree_depth(struct dx_dir_info
*dx_dir
,
294 struct dx_dirblock_info
*dx_db
)
298 while (dx_db
->type
!= DX_DIRBLOCK_ROOT
&& depth
< MAX_DEPTH
) {
299 dx_db
= &dx_dir
->dx_block
[dx_db
->parent
];
305 static int dict_de_cmp(const void *a
, const void *b
)
307 const struct ext2_dir_entry
*de_a
, *de_b
;
310 de_a
= (const struct ext2_dir_entry
*) a
;
311 a_len
= ext2fs_dirent_name_len(de_a
);
312 de_b
= (const struct ext2_dir_entry
*) b
;
313 b_len
= ext2fs_dirent_name_len(de_b
);
316 return (a_len
- b_len
);
318 return memcmp(de_a
->name
, de_b
->name
, a_len
);
322 * This is special sort function that makes sure that directory blocks
323 * with a dirblock of zero are sorted to the beginning of the list.
324 * This guarantees that the root node of the htree directories are
325 * processed first, so we know what hash version to use.
327 static EXT2_QSORT_TYPE
special_dir_block_cmp(const void *a
, const void *b
)
329 const struct ext2_db_entry2
*db_a
=
330 (const struct ext2_db_entry2
*) a
;
331 const struct ext2_db_entry2
*db_b
=
332 (const struct ext2_db_entry2
*) b
;
334 if (db_a
->blockcnt
&& !db_b
->blockcnt
)
337 if (!db_a
->blockcnt
&& db_b
->blockcnt
)
340 if (db_a
->blk
!= db_b
->blk
)
341 return (int) (db_a
->blk
- db_b
->blk
);
343 if (db_a
->ino
!= db_b
->ino
)
344 return (int) (db_a
->ino
- db_b
->ino
);
346 return (int) (db_a
->blockcnt
- db_b
->blockcnt
);
351 * Make sure the first entry in the directory is '.', and that the
352 * directory entry is sane.
354 static int check_dot(e2fsck_t ctx
,
355 struct ext2_dir_entry
*dirent
,
356 ext2_ino_t ino
, struct problem_context
*pctx
)
358 struct ext2_dir_entry
*nextdir
;
359 unsigned int rec_len
, new_len
;
362 problem_t problem
= 0;
365 problem
= PR_2_MISSING_DOT
;
366 else if ((ext2fs_dirent_name_len(dirent
) != 1) ||
367 (dirent
->name
[0] != '.'))
368 problem
= PR_2_1ST_NOT_DOT
;
369 else if (dirent
->name
[1] != '\0')
370 problem
= PR_2_DOT_NULL_TERM
;
372 (void) ext2fs_get_rec_len(ctx
->fs
, dirent
, &rec_len
);
374 if (fix_problem(ctx
, problem
, pctx
)) {
376 rec_len
= dirent
->rec_len
= 12;
378 ext2fs_dirent_set_name_len(dirent
, 1);
379 ext2fs_dirent_set_file_type(dirent
, EXT2_FT_UNKNOWN
);
380 dirent
->name
[0] = '.';
381 dirent
->name
[1] = '\0';
386 if (dirent
->inode
!= ino
) {
387 if (fix_problem(ctx
, PR_2_BAD_INODE_DOT
, pctx
)) {
393 new_len
= rec_len
- 12;
396 fix_problem(ctx
, PR_2_SPLIT_DOT
, pctx
)) {
397 nextdir
= (struct ext2_dir_entry
*)
398 ((char *) dirent
+ 12);
399 dirent
->rec_len
= 12;
400 (void) ext2fs_set_rec_len(ctx
->fs
, new_len
,
403 ext2fs_dirent_set_name_len(nextdir
, 0);
404 ext2fs_dirent_set_file_type(nextdir
,
414 * Make sure the second entry in the directory is '..', and that the
415 * directory entry is sane. We do not check the inode number of '..'
416 * here; this gets done in pass 3.
418 static int check_dotdot(e2fsck_t ctx
,
419 struct ext2_dir_entry
*dirent
,
420 ext2_ino_t ino
, struct problem_context
*pctx
)
422 problem_t problem
= 0;
423 unsigned int rec_len
;
426 problem
= PR_2_MISSING_DOT_DOT
;
427 else if ((ext2fs_dirent_name_len(dirent
) != 2) ||
428 (dirent
->name
[0] != '.') ||
429 (dirent
->name
[1] != '.'))
430 problem
= PR_2_2ND_NOT_DOT_DOT
;
431 else if (dirent
->name
[2] != '\0')
432 problem
= PR_2_DOT_DOT_NULL_TERM
;
434 (void) ext2fs_get_rec_len(ctx
->fs
, dirent
, &rec_len
);
436 if (fix_problem(ctx
, problem
, pctx
)) {
438 dirent
->rec_len
= 12;
440 * Note: we don't have the parent inode just
441 * yet, so we will fill it in with the root
442 * inode. This will get fixed in pass 3.
444 dirent
->inode
= EXT2_ROOT_INO
;
445 ext2fs_dirent_set_name_len(dirent
, 2);
446 ext2fs_dirent_set_file_type(dirent
, EXT2_FT_UNKNOWN
);
447 dirent
->name
[0] = '.';
448 dirent
->name
[1] = '.';
449 dirent
->name
[2] = '\0';
454 if (e2fsck_dir_info_set_dotdot(ctx
, ino
, dirent
->inode
)) {
455 fix_problem(ctx
, PR_2_NO_DIRINFO
, pctx
);
462 * Check to make sure a directory entry doesn't contain any illegal
465 static int check_name(e2fsck_t ctx
,
466 struct ext2_dir_entry
*dirent
,
467 struct problem_context
*pctx
)
473 for ( i
= 0; i
< ext2fs_dirent_name_len(dirent
); i
++) {
474 if (dirent
->name
[i
] != '/' && dirent
->name
[i
] != '\0')
477 fixup
= fix_problem(ctx
, PR_2_BAD_NAME
, pctx
);
480 dirent
->name
[i
] = '.';
486 static int encrypted_check_name(e2fsck_t ctx
,
487 struct ext2_dir_entry
*dirent
,
488 struct problem_context
*pctx
)
490 if (ext2fs_dirent_name_len(dirent
) < EXT4_CRYPTO_BLOCK_SIZE
) {
491 if (fix_problem(ctx
, PR_2_BAD_ENCRYPTED_NAME
, pctx
)) {
495 ext2fs_unmark_valid(ctx
->fs
);
501 * Check the directory filetype (if present)
503 static _INLINE_
int check_filetype(e2fsck_t ctx
,
504 struct ext2_dir_entry
*dirent
,
505 ext2_ino_t dir_ino
EXT2FS_ATTR((unused
)),
506 struct problem_context
*pctx
)
508 int filetype
= ext2fs_dirent_file_type(dirent
);
509 int should_be
= EXT2_FT_UNKNOWN
;
510 struct ext2_inode inode
;
512 if (!ext2fs_has_feature_filetype(ctx
->fs
->super
)) {
514 !fix_problem(ctx
, PR_2_CLEAR_FILETYPE
, pctx
))
516 ext2fs_dirent_set_file_type(dirent
, EXT2_FT_UNKNOWN
);
520 if (ext2fs_test_inode_bitmap2(ctx
->inode_dir_map
, dirent
->inode
)) {
521 should_be
= EXT2_FT_DIR
;
522 } else if (ext2fs_test_inode_bitmap2(ctx
->inode_reg_map
,
524 should_be
= EXT2_FT_REG_FILE
;
525 } else if (ctx
->inode_bad_map
&&
526 ext2fs_test_inode_bitmap2(ctx
->inode_bad_map
,
530 e2fsck_read_inode(ctx
, dirent
->inode
, &inode
,
532 should_be
= ext2_file_type(inode
.i_mode
);
534 if (filetype
== should_be
)
536 pctx
->num
= should_be
;
538 if (fix_problem(ctx
, filetype
? PR_2_BAD_FILETYPE
: PR_2_SET_FILETYPE
,
542 ext2fs_dirent_set_file_type(dirent
, should_be
);
546 static void parse_int_node(ext2_filsys fs
,
547 struct ext2_db_entry2
*db
,
548 struct check_dir_struct
*cd
,
549 struct dx_dir_info
*dx_dir
,
550 char *block_buf
, int failed_csum
)
552 struct ext2_dx_root_info
*root
;
553 struct ext2_dx_entry
*ent
;
554 struct ext2_dx_countlimit
*limit
;
555 struct dx_dirblock_info
*dx_db
;
556 int i
, expect_limit
, count
;
558 ext2_dirhash_t min_hash
= 0xffffffff;
559 ext2_dirhash_t max_hash
= 0;
560 ext2_dirhash_t hash
= 0, prev_hash
;
563 if (db
->blockcnt
== 0) {
564 root
= (struct ext2_dx_root_info
*) (block_buf
+ 24);
567 printf("Root node dump:\n");
568 printf("\t Reserved zero: %u\n", root
->reserved_zero
);
569 printf("\t Hash Version: %d\n", root
->hash_version
);
570 printf("\t Info length: %d\n", root
->info_length
);
571 printf("\t Indirect levels: %d\n", root
->indirect_levels
);
572 printf("\t Flags: %d\n", root
->unused_flags
);
575 ent
= (struct ext2_dx_entry
*) (block_buf
+ 24 + root
->info_length
);
578 (e2fsck_dir_will_be_rehashed(cd
->ctx
, cd
->pctx
.ino
) ||
579 fix_problem(cd
->ctx
, PR_2_HTREE_ROOT_CSUM_INVALID
,
583 ent
= (struct ext2_dx_entry
*) (block_buf
+8);
586 (e2fsck_dir_will_be_rehashed(cd
->ctx
, cd
->pctx
.ino
) ||
587 fix_problem(cd
->ctx
, PR_2_HTREE_NODE_CSUM_INVALID
,
592 limit
= (struct ext2_dx_countlimit
*) ent
;
595 printf("Number of entries (count): %d\n",
596 ext2fs_le16_to_cpu(limit
->count
));
597 printf("Number of entries (limit): %d\n",
598 ext2fs_le16_to_cpu(limit
->limit
));
601 count
= ext2fs_le16_to_cpu(limit
->count
);
602 if (ext2fs_has_feature_metadata_csum(fs
->super
))
603 csum_size
= sizeof(struct ext2_dx_tail
);
604 expect_limit
= (fs
->blocksize
-
605 (csum_size
+ ((char *) ent
- block_buf
))) /
606 sizeof(struct ext2_dx_entry
);
607 if (ext2fs_le16_to_cpu(limit
->limit
) != expect_limit
) {
608 cd
->pctx
.num
= ext2fs_le16_to_cpu(limit
->limit
);
609 if (fix_problem(cd
->ctx
, PR_2_HTREE_BAD_LIMIT
, &cd
->pctx
))
612 if (count
> expect_limit
) {
613 cd
->pctx
.num
= count
;
614 if (fix_problem(cd
->ctx
, PR_2_HTREE_BAD_COUNT
, &cd
->pctx
))
616 count
= expect_limit
;
619 for (i
=0; i
< count
; i
++) {
621 hash
= i
? (ext2fs_le32_to_cpu(ent
[i
].hash
) & ~1) : 0;
623 printf("Entry #%d: Hash 0x%08x, block %u\n", i
,
624 hash
, ext2fs_le32_to_cpu(ent
[i
].block
));
626 blk
= ext2fs_le32_to_cpu(ent
[i
].block
) & 0x0ffffff;
627 /* Check to make sure the block is valid */
628 if (blk
>= (blk_t
) dx_dir
->numblocks
) {
630 if (fix_problem(cd
->ctx
, PR_2_HTREE_BADBLK
,
635 if (hash
< prev_hash
&&
636 fix_problem(cd
->ctx
, PR_2_HTREE_HASH_ORDER
, &cd
->pctx
))
638 dx_db
= &dx_dir
->dx_block
[blk
];
639 if (dx_db
->flags
& DX_FLAG_REFERENCED
) {
640 dx_db
->flags
|= DX_FLAG_DUP_REF
;
642 dx_db
->flags
|= DX_FLAG_REFERENCED
;
643 dx_db
->parent
= db
->blockcnt
;
649 dx_db
->node_min_hash
= hash
;
651 dx_db
->node_max_hash
=
652 ext2fs_le32_to_cpu(ent
[i
+1].hash
) & ~1;
654 dx_db
->node_max_hash
= 0xfffffffe;
655 dx_db
->flags
|= DX_FLAG_LAST
;
658 dx_db
->flags
|= DX_FLAG_FIRST
;
661 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
662 db
->blockcnt
, min_hash
, max_hash
);
664 dx_db
= &dx_dir
->dx_block
[db
->blockcnt
];
665 dx_db
->min_hash
= min_hash
;
666 dx_db
->max_hash
= max_hash
;
670 clear_htree(cd
->ctx
, cd
->pctx
.ino
);
671 dx_dir
->numblocks
= 0;
672 e2fsck_rehash_dir_later(cd
->ctx
, cd
->pctx
.ino
);
676 * Given a busted directory, try to salvage it somehow.
679 static void salvage_directory(ext2_filsys fs
,
680 struct ext2_dir_entry
*dirent
,
681 struct ext2_dir_entry
*prev
,
682 unsigned int *offset
,
683 unsigned int block_len
)
685 char *cp
= (char *) dirent
;
687 unsigned int rec_len
, prev_rec_len
;
688 unsigned int name_len
;
691 * If the space left for the entry is too small to be an entry,
692 * we can't access dirent's fields, so plumb in the values needed
693 * so that the previous entry absorbs this one.
695 if (block_len
- *offset
< EXT2_DIR_ENTRY_HEADER_LEN
) {
697 rec_len
= block_len
- *offset
;
699 name_len
= ext2fs_dirent_name_len(dirent
);
700 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
702 left
= block_len
- *offset
- rec_len
;
705 * Special case of directory entry of size 8: copy what's left
706 * of the directory block up to cover up the invalid hole.
708 if ((left
>= 12) && (rec_len
== EXT2_DIR_ENTRY_HEADER_LEN
)) {
709 memmove(cp
, cp
+EXT2_DIR_ENTRY_HEADER_LEN
, left
);
710 memset(cp
+ left
, 0, EXT2_DIR_ENTRY_HEADER_LEN
);
714 * If the directory entry overruns the end of the directory
715 * block, and the name is small enough to fit, then adjust the
719 ((int) rec_len
+ left
> EXT2_DIR_ENTRY_HEADER_LEN
) &&
720 ((int) name_len
+ EXT2_DIR_ENTRY_HEADER_LEN
<= (int) rec_len
+ left
) &&
721 dirent
->inode
<= fs
->super
->s_inodes_count
&&
722 strnlen(dirent
->name
, name_len
) == name_len
) {
723 (void) ext2fs_set_rec_len(fs
, (int) rec_len
+ left
, dirent
);
727 * If the record length of the directory entry is a multiple
728 * of four, and not too big, such that it is valid, let the
729 * previous directory entry absorb the invalid one.
731 if (prev
&& rec_len
&& (rec_len
% 4) == 0 &&
732 (*offset
+ rec_len
<= block_len
)) {
733 (void) ext2fs_get_rec_len(fs
, prev
, &prev_rec_len
);
734 prev_rec_len
+= rec_len
;
735 (void) ext2fs_set_rec_len(fs
, prev_rec_len
, prev
);
740 * Default salvage method --- kill all of the directory
741 * entries for the rest of the block. We will either try to
742 * absorb it into the previous directory entry, or create a
743 * new empty directory entry the rest of the directory block.
746 (void) ext2fs_get_rec_len(fs
, prev
, &prev_rec_len
);
747 prev_rec_len
+= block_len
- *offset
;
748 (void) ext2fs_set_rec_len(fs
, prev_rec_len
, prev
);
749 *offset
= fs
->blocksize
;
751 rec_len
= block_len
- *offset
;
752 (void) ext2fs_set_rec_len(fs
, rec_len
, dirent
);
753 ext2fs_dirent_set_name_len(dirent
, 0);
754 ext2fs_dirent_set_file_type(dirent
, EXT2_FT_UNKNOWN
);
759 #define NEXT_DIRENT(d) ((void *)((char *)(d) + (d)->rec_len))
760 static errcode_t
insert_dirent_tail(ext2_filsys fs
, void *dirbuf
)
762 struct ext2_dir_entry
*d
;
764 struct ext2_dir_entry_tail
*t
;
767 top
= EXT2_DIRENT_TAIL(dirbuf
, fs
->blocksize
);
769 while (d
->rec_len
&& !(d
->rec_len
& 0x3) && NEXT_DIRENT(d
) <= top
)
773 unsigned int min_size
= EXT2_DIR_REC_LEN(
774 ext2fs_dirent_name_len(dirbuf
));
775 if (min_size
> (char *)top
- (char *)d
)
776 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM
;
777 d
->rec_len
= (char *)top
- (char *)d
;
780 t
= (struct ext2_dir_entry_tail
*)top
;
781 if (t
->det_reserved_zero1
||
782 t
->det_rec_len
!= sizeof(struct ext2_dir_entry_tail
) ||
783 t
->det_reserved_name_len
!= EXT2_DIR_NAME_LEN_CSUM
)
784 ext2fs_initialize_dirent_tail(fs
, t
);
790 static errcode_t
fix_inline_dir_size(e2fsck_t ctx
, ext2_ino_t ino
,
791 size_t *inline_data_size
,
792 struct problem_context
*pctx
,
795 ext2_filsys fs
= ctx
->fs
;
796 struct ext2_inode inode
;
797 size_t new_size
, old_size
;
800 old_size
= *inline_data_size
;
802 * If there's not enough bytes to start the "second" dir block
803 * (in the EA space) then truncate everything to the first block.
805 if (old_size
> EXT4_MIN_INLINE_DATA_SIZE
&&
806 old_size
< EXT4_MIN_INLINE_DATA_SIZE
+
807 EXT2_DIR_REC_LEN(1)) {
808 old_size
= EXT4_MIN_INLINE_DATA_SIZE
;
811 /* Increase to the next four-byte boundary for salvaging */
812 new_size
= old_size
+ (4 - (old_size
& 3));
813 memset(buf
+ old_size
, 0, new_size
- old_size
);
814 retval
= ext2fs_inline_data_set(fs
, ino
, 0, buf
, new_size
);
815 if (retval
== EXT2_ET_INLINE_DATA_NO_SPACE
) {
816 /* Or we can't, so truncate. */
818 retval
= ext2fs_inline_data_set(fs
, ino
, 0, buf
, new_size
);
820 if (fix_problem(ctx
, PR_2_FIX_INLINE_DIR_FAILED
,
828 if (fix_problem(ctx
, PR_2_FIX_INLINE_DIR_FAILED
,
837 retval
= ext2fs_read_inode(fs
, ino
, &inode
);
841 retval
= ext2fs_inode_size_set(fs
, &inode
, new_size
);
845 inode
.i_flags
&= ~EXT4_INLINE_DATA_FL
;
846 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
849 *inline_data_size
= new_size
;
855 static int check_dir_block2(ext2_filsys fs
,
856 struct ext2_db_entry2
*db
,
860 struct check_dir_struct
*cd
= priv_data
;
862 if (cd
->ra_entries
&& cd
->list_offset
>= cd
->next_ra_off
) {
863 err
= e2fsck_readahead_dblist(fs
,
864 E2FSCK_RA_DBLIST_IGNORE_BLOCKCNT
,
866 cd
->list_offset
+ cd
->ra_entries
/ 8,
870 cd
->next_ra_off
= cd
->list_offset
+ (cd
->ra_entries
* 7 / 8);
873 err
= check_dir_block(fs
, db
, priv_data
);
878 static int check_dir_block(ext2_filsys fs
,
879 struct ext2_db_entry2
*db
,
882 struct dx_dir_info
*dx_dir
;
883 struct dx_dirblock_info
*dx_db
= 0;
884 struct ext2_dir_entry
*dirent
, *prev
, dot
, dotdot
;
886 unsigned int offset
= 0;
887 int dir_modified
= 0;
889 unsigned int rec_len
;
890 blk64_t block_nr
= db
->blk
;
891 ext2_ino_t ino
= db
->ino
;
892 ext2_ino_t subdir_parent
;
894 struct check_dir_struct
*cd
;
898 struct ext2_dx_root_info
*root
;
899 struct ext2_dx_countlimit
*limit
;
900 static dict_t de_dict
;
901 struct problem_context pctx
;
904 int dx_csum_size
= 0, de_csum_size
= 0;
907 size_t inline_data_size
= 0;
910 size_t max_block_size
;
912 cd
= (struct check_dir_struct
*) priv_data
;
913 ibuf
= buf
= cd
->buf
;
916 if (ctx
->flags
& E2F_FLAG_RUN_RETURN
)
919 if (ctx
->progress
&& (ctx
->progress
)(ctx
, 2, cd
->count
++, cd
->max
))
922 if (ext2fs_has_feature_metadata_csum(fs
->super
)) {
923 dx_csum_size
= sizeof(struct ext2_dx_tail
);
924 de_csum_size
= sizeof(struct ext2_dir_entry_tail
);
927 if (ext2fs_has_feature_filetype(fs
->super
))
928 filetype
= EXT2_FT_DIR
<< 8;
931 * Make sure the inode is still in use (could have been
932 * deleted in the duplicate/bad blocks pass.
934 if (!(ext2fs_test_inode_bitmap2(ctx
->inode_used_map
, ino
)))
938 cd
->pctx
.blk
= block_nr
;
939 cd
->pctx
.blkcount
= db
->blockcnt
;
944 if (ext2fs_has_feature_inline_data(fs
->super
)) {
947 ec
= ext2fs_inline_data_size(fs
, ino
, &inline_data_size
);
948 if (ec
&& ec
!= EXT2_ET_NO_INLINE_DATA
)
952 if (db
->blk
== 0 && !inline_data_size
) {
953 if (allocate_dir_block(ctx
, db
, buf
, &cd
->pctx
))
963 if (ctx
->dirs_to_hash
&&
964 ext2fs_u32_list_test(ctx
->dirs_to_hash
, ino
))
968 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr
,
972 ehandler_operation(_("reading directory block"));
973 if (inline_data_size
) {
974 memset(buf
, 0, fs
->blocksize
- inline_data_size
);
975 cd
->pctx
.errcode
= ext2fs_inline_data_get(fs
, ino
, 0, buf
, 0);
976 if (cd
->pctx
.errcode
)
977 goto inline_read_fail
;
978 #ifdef WORDS_BIGENDIAN
980 goto skip_first_read_swab
;
981 *((__u32
*)buf
) = ext2fs_le32_to_cpu(*((__u32
*)buf
));
982 cd
->pctx
.errcode
= ext2fs_dirent_swab_in2(fs
,
983 buf
+ EXT4_INLINE_DATA_DOTDOT_SIZE
,
984 EXT4_MIN_INLINE_DATA_SIZE
- EXT4_INLINE_DATA_DOTDOT_SIZE
,
986 if (cd
->pctx
.errcode
)
987 goto inline_read_fail
;
988 skip_first_read_swab
:
989 if (inline_data_size
<= EXT4_MIN_INLINE_DATA_SIZE
||
991 goto inline_read_fail
;
992 cd
->pctx
.errcode
= ext2fs_dirent_swab_in2(fs
,
993 buf
+ EXT4_MIN_INLINE_DATA_SIZE
,
994 inline_data_size
- EXT4_MIN_INLINE_DATA_SIZE
,
998 cd
->pctx
.errcode
= ext2fs_read_dir_block4(fs
, block_nr
,
1002 pctx
.num
= inline_data_size
;
1003 if (((inline_data_size
& 3) ||
1004 (inline_data_size
> EXT4_MIN_INLINE_DATA_SIZE
&&
1005 inline_data_size
< EXT4_MIN_INLINE_DATA_SIZE
+
1006 EXT2_DIR_REC_LEN(1))) &&
1007 fix_problem(ctx
, PR_2_BAD_INLINE_DIR_SIZE
, &pctx
)) {
1008 errcode_t err
= fix_inline_dir_size(ctx
, ino
,
1009 &inline_data_size
, &pctx
,
1012 return DIRENT_ABORT
;
1015 ehandler_operation(0);
1016 if (cd
->pctx
.errcode
== EXT2_ET_DIR_CORRUPTED
)
1017 cd
->pctx
.errcode
= 0; /* We'll handle this ourselves */
1018 else if (cd
->pctx
.errcode
== EXT2_ET_DIR_CSUM_INVALID
) {
1019 cd
->pctx
.errcode
= 0; /* We'll handle this ourselves */
1022 if (cd
->pctx
.errcode
) {
1024 if (!fix_problem(ctx
, PR_2_READ_DIRBLOCK
, &cd
->pctx
)) {
1025 ctx
->flags
|= E2F_FLAG_ABORT
;
1026 return DIRENT_ABORT
;
1028 ext2fs_new_dir_block(fs
, db
->blockcnt
== 0 ? ino
: 0,
1029 EXT2_ROOT_INO
, &buf2
);
1030 memcpy(buf
, buf2
, fs
->blocksize
);
1031 ext2fs_free_mem(&buf2
);
1033 dx_dir
= e2fsck_get_dx_dir_info(ctx
, ino
);
1034 if (dx_dir
&& dx_dir
->numblocks
) {
1035 if (db
->blockcnt
>= dx_dir
->numblocks
) {
1037 if (fix_problem(ctx
, PR_2_UNEXPECTED_HTREE_BLOCK
,
1039 clear_htree(ctx
, ino
);
1040 dx_dir
->numblocks
= 0;
1044 fatal_error(ctx
, _("Can not continue."));
1046 dx_db
= &dx_dir
->dx_block
[db
->blockcnt
];
1047 dx_db
->type
= DX_DIRBLOCK_LEAF
;
1048 dx_db
->phys
= block_nr
;
1049 dx_db
->min_hash
= ~0;
1050 dx_db
->max_hash
= 0;
1052 dirent
= (struct ext2_dir_entry
*) buf
;
1053 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
1054 limit
= (struct ext2_dx_countlimit
*) (buf
+8);
1055 if (db
->blockcnt
== 0) {
1056 root
= (struct ext2_dx_root_info
*) (buf
+ 24);
1057 dx_db
->type
= DX_DIRBLOCK_ROOT
;
1058 dx_db
->flags
|= DX_FLAG_FIRST
| DX_FLAG_LAST
;
1059 if ((root
->reserved_zero
||
1060 root
->info_length
< 8 ||
1061 root
->indirect_levels
> 1) &&
1062 fix_problem(ctx
, PR_2_HTREE_BAD_ROOT
, &cd
->pctx
)) {
1063 clear_htree(ctx
, ino
);
1064 dx_dir
->numblocks
= 0;
1067 dx_dir
->hashversion
= root
->hash_version
;
1068 if ((dx_dir
->hashversion
<= EXT2_HASH_TEA
) &&
1069 (fs
->super
->s_flags
& EXT2_FLAGS_UNSIGNED_HASH
))
1070 dx_dir
->hashversion
+= 3;
1071 dx_dir
->depth
= root
->indirect_levels
+ 1;
1072 } else if ((dirent
->inode
== 0) &&
1073 (rec_len
== fs
->blocksize
) &&
1074 (ext2fs_dirent_name_len(dirent
) == 0) &&
1075 (ext2fs_le16_to_cpu(limit
->limit
) ==
1076 ((fs
->blocksize
- (8 + dx_csum_size
)) /
1077 sizeof(struct ext2_dx_entry
))))
1078 dx_db
->type
= DX_DIRBLOCK_NODE
;
1079 is_leaf
= (dx_db
->type
== DX_DIRBLOCK_LEAF
);
1083 /* Leaf node with no space for csum? Rebuild dirs in pass 3A. */
1084 if (is_leaf
&& !inline_data_size
&& failed_csum
&&
1085 !ext2fs_dirent_has_tail(fs
, (struct ext2_dir_entry
*)buf
)) {
1087 if (e2fsck_dir_will_be_rehashed(ctx
, ino
)) {
1091 if (!fix_problem(cd
->ctx
, PR_2_LEAF_NODE_MISSING_CSUM
,
1094 e2fsck_rehash_dir_later(ctx
, ino
);
1098 /* htree nodes don't use fake dirents to store checksums */
1103 if (inline_data_size
) {
1105 buf
+= EXT4_MIN_INLINE_DATA_SIZE
;
1106 max_block_size
= inline_data_size
- EXT4_MIN_INLINE_DATA_SIZE
;
1107 /* Zero-length second block, just exit */
1108 if (max_block_size
== 0)
1111 max_block_size
= EXT4_MIN_INLINE_DATA_SIZE
;
1114 max_block_size
= fs
->blocksize
- de_csum_size
;
1116 if (ctx
->encrypted_dirs
)
1117 encrypted
= ext2fs_u32_list_test(ctx
->encrypted_dirs
, ino
);
1119 dict_init(&de_dict
, DICTCOUNT_T_MAX
, dict_de_cmp
);
1123 ext2_ino_t first_unused_inode
;
1124 unsigned int name_len
;
1127 if (!inline_data_size
|| dot_state
> 1) {
1128 dirent
= (struct ext2_dir_entry
*) (buf
+ offset
);
1130 * If there's not even space for the entry header,
1131 * force salvaging this dir.
1133 if (max_block_size
- offset
< EXT2_DIR_ENTRY_HEADER_LEN
)
1134 rec_len
= EXT2_DIR_REC_LEN(1);
1136 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
1137 cd
->pctx
.dirent
= dirent
;
1138 cd
->pctx
.num
= offset
;
1139 if ((offset
+ rec_len
> max_block_size
) ||
1141 ((rec_len
% 4) != 0) ||
1142 (((unsigned) ext2fs_dirent_name_len(dirent
) + EXT2_DIR_ENTRY_HEADER_LEN
) > rec_len
)) {
1143 if (fix_problem(ctx
, PR_2_DIR_CORRUPTED
,
1145 #ifdef WORDS_BIGENDIAN
1147 * On big-endian systems, if the dirent
1148 * swap routine finds a rec_len that it
1149 * doesn't like, it continues
1150 * processing the block as if rec_len
1151 * == EXT2_DIR_ENTRY_HEADER_LEN. This means that the name
1152 * field gets byte swapped, which means
1153 * that salvage will not detect the
1154 * correct name length (unless the name
1155 * has a length that's an exact
1156 * multiple of four bytes), and it'll
1157 * discard the entry (unnecessarily)
1158 * and the rest of the dirent block.
1159 * Therefore, swap the rest of the
1160 * block back to disk order, run
1161 * salvage, and re-swap anything after
1162 * the salvaged dirent.
1164 int need_reswab
= 0;
1165 if (rec_len
< EXT2_DIR_ENTRY_HEADER_LEN
|| rec_len
% 4) {
1167 ext2fs_dirent_swab_in2(fs
,
1168 ((char *)dirent
) + EXT2_DIR_ENTRY_HEADER_LEN
,
1169 max_block_size
- offset
- EXT2_DIR_ENTRY_HEADER_LEN
,
1173 salvage_directory(fs
, dirent
, prev
,
1176 #ifdef WORDS_BIGENDIAN
1178 (void) ext2fs_get_rec_len(fs
,
1180 ext2fs_dirent_swab_in2(fs
,
1181 ((char *)dirent
) + offset
+ rec_len
,
1182 max_block_size
- offset
- rec_len
,
1189 goto abort_free_dict
;
1192 if (dot_state
== 0) {
1193 memset(&dot
, 0, sizeof(dot
));
1195 dirent
->inode
= ino
;
1196 dirent
->rec_len
= EXT2_DIR_REC_LEN(1);
1197 dirent
->name_len
= 1 | filetype
;
1198 dirent
->name
[0] = '.';
1199 } else if (dot_state
== 1) {
1200 memset(&dotdot
, 0, sizeof(dotdot
));
1203 ((struct ext2_dir_entry
*)buf
)->inode
;
1204 dirent
->rec_len
= EXT2_DIR_REC_LEN(2);
1205 dirent
->name_len
= 2 | filetype
;
1206 dirent
->name
[0] = '.';
1207 dirent
->name
[1] = '.';
1209 fatal_error(ctx
, _("Can not continue."));
1211 cd
->pctx
.dirent
= dirent
;
1212 cd
->pctx
.num
= offset
;
1215 if (dot_state
== 0) {
1216 if (check_dot(ctx
, dirent
, ino
, &cd
->pctx
))
1218 } else if (dot_state
== 1) {
1219 ret
= check_dotdot(ctx
, dirent
, ino
, &cd
->pctx
);
1221 goto abort_free_dict
;
1224 } else if (dirent
->inode
== ino
) {
1225 problem
= PR_2_LINK_DOT
;
1226 if (fix_problem(ctx
, PR_2_LINK_DOT
, &cd
->pctx
)) {
1236 * Make sure the inode listed is a legal one.
1238 name_len
= ext2fs_dirent_name_len(dirent
);
1239 if (((dirent
->inode
!= EXT2_ROOT_INO
) &&
1240 (dirent
->inode
< EXT2_FIRST_INODE(fs
->super
))) ||
1241 (dirent
->inode
> fs
->super
->s_inodes_count
)) {
1242 problem
= PR_2_BAD_INO
;
1243 } else if (ctx
->inode_bb_map
&&
1244 (ext2fs_test_inode_bitmap2(ctx
->inode_bb_map
,
1247 * If the inode is in a bad block, offer to
1250 problem
= PR_2_BB_INODE
;
1251 } else if ((dot_state
> 1) && (name_len
== 1) &&
1252 (dirent
->name
[0] == '.')) {
1254 * If there's a '.' entry in anything other
1255 * than the first directory entry, it's a
1256 * duplicate entry that should be removed.
1258 problem
= PR_2_DUP_DOT
;
1259 } else if ((dot_state
> 1) && (name_len
== 2) &&
1260 (dirent
->name
[0] == '.') &&
1261 (dirent
->name
[1] == '.')) {
1263 * If there's a '..' entry in anything other
1264 * than the second directory entry, it's a
1265 * duplicate entry that should be removed.
1267 problem
= PR_2_DUP_DOT_DOT
;
1268 } else if ((dot_state
> 1) &&
1269 (dirent
->inode
== EXT2_ROOT_INO
)) {
1271 * Don't allow links to the root directory.
1272 * We check this specially to make sure we
1273 * catch this error case even if the root
1274 * directory hasn't been created yet.
1276 problem
= PR_2_LINK_ROOT
;
1277 } else if ((dot_state
> 1) && (name_len
== 0)) {
1279 * Don't allow zero-length directory names.
1281 problem
= PR_2_NULL_NAME
;
1285 if (fix_problem(ctx
, problem
, &cd
->pctx
)) {
1290 ext2fs_unmark_valid(fs
);
1291 if (problem
== PR_2_BAD_INO
)
1297 * If the inode was marked as having bad fields in
1298 * pass1, process it and offer to fix/clear it.
1299 * (We wait until now so that we can display the
1300 * pathname to the user.)
1302 if (ctx
->inode_bad_map
&&
1303 ext2fs_test_inode_bitmap2(ctx
->inode_bad_map
,
1305 if (e2fsck_process_bad_inode(ctx
, ino
,
1307 buf
+ fs
->blocksize
)) {
1312 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
1313 return DIRENT_ABORT
;
1316 group
= ext2fs_group_of_ino(fs
, dirent
->inode
);
1317 first_unused_inode
= group
* fs
->super
->s_inodes_per_group
+
1318 1 + fs
->super
->s_inodes_per_group
-
1319 ext2fs_bg_itable_unused(fs
, group
);
1320 cd
->pctx
.group
= group
;
1323 * Check if the inode was missed out because
1324 * _INODE_UNINIT flag was set or bg_itable_unused was
1325 * incorrect. If so, clear the _INODE_UNINIT flag and
1326 * restart e2fsck. In the future it would be nice if
1327 * we could call a function in pass1.c that checks the
1328 * newly visible inodes.
1330 if (ext2fs_bg_flags_test(fs
, group
, EXT2_BG_INODE_UNINIT
)) {
1331 pctx
.num
= dirent
->inode
;
1332 if (fix_problem(ctx
, PR_2_INOREF_BG_INO_UNINIT
,
1334 ext2fs_bg_flags_clear(fs
, group
,
1335 EXT2_BG_INODE_UNINIT
);
1336 ext2fs_mark_super_dirty(fs
);
1337 ctx
->flags
|= E2F_FLAG_RESTART_LATER
;
1339 ext2fs_unmark_valid(fs
);
1340 if (problem
== PR_2_BAD_INO
)
1343 } else if (dirent
->inode
>= first_unused_inode
) {
1344 pctx
.num
= dirent
->inode
;
1345 if (fix_problem(ctx
, PR_2_INOREF_IN_UNUSED
, &cd
->pctx
)){
1346 ext2fs_bg_itable_unused_set(fs
, group
, 0);
1347 ext2fs_mark_super_dirty(fs
);
1348 ctx
->flags
|= E2F_FLAG_RESTART_LATER
;
1350 ext2fs_unmark_valid(fs
);
1351 if (problem
== PR_2_BAD_INO
)
1357 * Offer to clear unused inodes; if we are going to be
1358 * restarting the scan due to bg_itable_unused being
1359 * wrong, then don't clear any inodes to avoid zapping
1360 * inodes that were skipped during pass1 due to an
1361 * incorrect bg_itable_unused; we'll get any real
1362 * problems after we restart.
1364 if (!(ctx
->flags
& E2F_FLAG_RESTART_LATER
) &&
1365 !(ext2fs_test_inode_bitmap2(ctx
->inode_used_map
,
1367 problem
= PR_2_UNUSED_INODE
;
1370 if (fix_problem(ctx
, problem
, &cd
->pctx
)) {
1375 ext2fs_unmark_valid(fs
);
1376 if (problem
== PR_2_BAD_INO
)
1381 if (!encrypted
&& check_name(ctx
, dirent
, &cd
->pctx
))
1384 if (encrypted
&& (dot_state
) > 1 &&
1385 encrypted_check_name(ctx
, dirent
, &cd
->pctx
)) {
1390 if (check_filetype(ctx
, dirent
, ino
, &cd
->pctx
))
1394 ext2fs_dirhash(dx_dir
->hashversion
, dirent
->name
,
1395 ext2fs_dirent_name_len(dirent
),
1396 fs
->super
->s_hash_seed
, &hash
, 0);
1397 if (hash
< dx_db
->min_hash
)
1398 dx_db
->min_hash
= hash
;
1399 if (hash
> dx_db
->max_hash
)
1400 dx_db
->max_hash
= hash
;
1404 * If this is a directory, then mark its parent in its
1405 * dir_info structure. If the parent field is already
1406 * filled in, then this directory has more than one
1407 * hard link. We assume the first link is correct,
1408 * and ask the user if he/she wants to clear this one.
1410 if ((dot_state
> 1) &&
1411 (ext2fs_test_inode_bitmap2(ctx
->inode_dir_map
,
1413 if (e2fsck_dir_info_get_parent(ctx
, dirent
->inode
,
1415 cd
->pctx
.ino
= dirent
->inode
;
1416 fix_problem(ctx
, PR_2_NO_DIRINFO
, &cd
->pctx
);
1417 goto abort_free_dict
;
1419 if (subdir_parent
) {
1420 cd
->pctx
.ino2
= subdir_parent
;
1421 if (fix_problem(ctx
, PR_2_LINK_DIR
,
1429 (void) e2fsck_dir_info_set_parent(ctx
,
1430 dirent
->inode
, ino
);
1436 } else if (dict_lookup(&de_dict
, dirent
)) {
1437 clear_problem_context(&pctx
);
1439 pctx
.dirent
= dirent
;
1440 fix_problem(ctx
, PR_2_REPORT_DUP_DIRENT
, &pctx
);
1441 e2fsck_rehash_dir_later(ctx
, ino
);
1444 dict_alloc_insert(&de_dict
, dirent
, dirent
);
1446 ext2fs_icount_increment(ctx
->inode_count
, dirent
->inode
,
1449 ctx
->fs_links_count
++;
1450 ctx
->fs_total_count
++;
1454 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
1455 if (!inline_data_size
|| dot_state
> 1) {
1458 if (dot_state
== 1) {
1461 * If we get here, we're checking an inline
1462 * directory and we've just checked a (fake)
1463 * dotdot entry that we created on the stack.
1464 * Therefore set 'prev' to NULL so that if we
1465 * call salvage_directory on the next entry,
1466 * it won't try to absorb the next entry into
1467 * the on-stack dotdot entry.
1473 } while (offset
< max_block_size
);
1479 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1480 db
->blockcnt
, dx_db
->type
,
1481 dx_db
->min_hash
, dx_db
->max_hash
);
1483 cd
->pctx
.dir
= cd
->pctx
.ino
;
1484 if ((dx_db
->type
== DX_DIRBLOCK_ROOT
) ||
1485 (dx_db
->type
== DX_DIRBLOCK_NODE
))
1486 parse_int_node(fs
, db
, cd
, dx_dir
, buf
, failed_csum
);
1489 if (offset
!= max_block_size
) {
1490 cd
->pctx
.num
= rec_len
+ offset
- max_block_size
;
1491 if (fix_problem(ctx
, PR_2_FINAL_RECLEN
, &cd
->pctx
)) {
1492 dirent
->rec_len
= cd
->pctx
.num
;
1497 int flags
, will_rehash
;
1498 /* leaf block with no tail? Rehash dirs later. */
1499 if (ext2fs_has_feature_metadata_csum(fs
->super
) &&
1501 !inline_data_size
&&
1502 !ext2fs_dirent_has_tail(fs
, (struct ext2_dir_entry
*)buf
)) {
1503 if (insert_dirent_tail(fs
, buf
) == 0)
1505 e2fsck_rehash_dir_later(ctx
, ino
);
1509 will_rehash
= e2fsck_dir_will_be_rehashed(ctx
, ino
);
1511 flags
= ctx
->fs
->flags
;
1512 ctx
->fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
1514 if (inline_data_size
) {
1516 #ifdef WORDS_BIGENDIAN
1518 goto skip_first_write_swab
;
1519 *((__u32
*)buf
) = ext2fs_le32_to_cpu(*((__u32
*)buf
));
1520 cd
->pctx
.errcode
= ext2fs_dirent_swab_out2(fs
,
1521 buf
+ EXT4_INLINE_DATA_DOTDOT_SIZE
,
1522 EXT4_MIN_INLINE_DATA_SIZE
-
1523 EXT4_INLINE_DATA_DOTDOT_SIZE
,
1525 if (cd
->pctx
.errcode
)
1526 goto skip_second_write_swab
;
1527 skip_first_write_swab
:
1528 if (inline_data_size
<= EXT4_MIN_INLINE_DATA_SIZE
||
1530 goto skip_second_write_swab
;
1531 cd
->pctx
.errcode
= ext2fs_dirent_swab_out2(fs
,
1532 buf
+ EXT4_MIN_INLINE_DATA_SIZE
,
1534 EXT4_MIN_INLINE_DATA_SIZE
,
1536 skip_second_write_swab
:
1537 if (cd
->pctx
.errcode
&&
1538 !fix_problem(ctx
, PR_2_WRITE_DIRBLOCK
, &cd
->pctx
))
1539 goto abort_free_dict
;
1542 ext2fs_inline_data_set(fs
, ino
, 0, buf
,
1545 cd
->pctx
.errcode
= ext2fs_write_dir_block4(fs
, block_nr
,
1548 ctx
->fs
->flags
= (flags
&
1549 EXT2_FLAG_IGNORE_CSUM_ERRORS
) |
1551 ~EXT2_FLAG_IGNORE_CSUM_ERRORS
);
1552 if (cd
->pctx
.errcode
) {
1553 if (!fix_problem(ctx
, PR_2_WRITE_DIRBLOCK
,
1555 goto abort_free_dict
;
1557 ext2fs_mark_changed(fs
);
1558 } else if (is_leaf
&& failed_csum
&& !dir_modified
) {
1560 * If a leaf node that fails csum makes it this far without
1561 * alteration, ask the user if the checksum should be fixed.
1563 if (fix_problem(ctx
, PR_2_LEAF_NODE_ONLY_CSUM_INVALID
,
1567 dict_free_nodes(&de_dict
);
1570 ctx
->flags
|= E2F_FLAG_ABORT
;
1571 dict_free_nodes(&de_dict
);
1572 return DIRENT_ABORT
;
1581 * This function is called to deallocate a block, and is an interator
1582 * functioned called by deallocate inode via ext2fs_iterate_block().
1584 static int deallocate_inode_block(ext2_filsys fs
,
1586 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1587 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1588 int ref_offset
EXT2FS_ATTR((unused
)),
1591 struct del_block
*p
= priv_data
;
1595 if ((*block_nr
< fs
->super
->s_first_data_block
) ||
1596 (*block_nr
>= ext2fs_blocks_count(fs
->super
)))
1598 if ((*block_nr
% EXT2FS_CLUSTER_RATIO(fs
)) == 0)
1599 ext2fs_block_alloc_stats2(fs
, *block_nr
, -1);
1605 * This fuction deallocates an inode
1607 static void deallocate_inode(e2fsck_t ctx
, ext2_ino_t ino
, char* block_buf
)
1609 ext2_filsys fs
= ctx
->fs
;
1610 struct ext2_inode inode
;
1611 struct problem_context pctx
;
1613 struct del_block del_block
;
1615 e2fsck_read_inode(ctx
, ino
, &inode
, "deallocate_inode");
1616 clear_problem_context(&pctx
);
1620 * Fix up the bitmaps...
1622 e2fsck_read_bitmaps(ctx
);
1623 ext2fs_inode_alloc_stats2(fs
, ino
, -1, LINUX_S_ISDIR(inode
.i_mode
));
1625 if (ext2fs_file_acl_block(fs
, &inode
) &&
1626 ext2fs_has_feature_xattr(fs
->super
)) {
1627 pctx
.errcode
= ext2fs_adjust_ea_refcount3(fs
,
1628 ext2fs_file_acl_block(fs
, &inode
),
1629 block_buf
, -1, &count
, ino
);
1630 if (pctx
.errcode
== EXT2_ET_BAD_EA_BLOCK_NUM
) {
1635 pctx
.blk
= ext2fs_file_acl_block(fs
, &inode
);
1636 fix_problem(ctx
, PR_2_ADJ_EA_REFCOUNT
, &pctx
);
1637 ctx
->flags
|= E2F_FLAG_ABORT
;
1641 ext2fs_block_alloc_stats2(fs
,
1642 ext2fs_file_acl_block(fs
, &inode
), -1);
1644 ext2fs_file_acl_block_set(fs
, &inode
, 0);
1647 if (!ext2fs_inode_has_valid_blocks2(fs
, &inode
))
1650 /* Inline data inodes don't have blocks to iterate */
1651 if (inode
.i_flags
& EXT4_INLINE_DATA_FL
)
1654 if (LINUX_S_ISREG(inode
.i_mode
) &&
1655 ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode
)))
1658 del_block
.ctx
= ctx
;
1660 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
, 0, block_buf
,
1661 deallocate_inode_block
,
1664 fix_problem(ctx
, PR_2_DEALLOC_INODE
, &pctx
);
1665 ctx
->flags
|= E2F_FLAG_ABORT
;
1669 /* Inode may have changed by block_iterate, so reread it */
1670 e2fsck_read_inode(ctx
, ino
, &inode
, "deallocate_inode");
1671 e2fsck_clear_inode(ctx
, ino
, &inode
, 0, "deallocate_inode");
1675 * This fuction clears the htree flag on an inode
1677 static void clear_htree(e2fsck_t ctx
, ext2_ino_t ino
)
1679 struct ext2_inode inode
;
1681 e2fsck_read_inode(ctx
, ino
, &inode
, "clear_htree");
1682 inode
.i_flags
= inode
.i_flags
& ~EXT2_INDEX_FL
;
1683 e2fsck_write_inode(ctx
, ino
, &inode
, "clear_htree");
1684 if (ctx
->dirs_to_hash
)
1685 ext2fs_u32_list_add(ctx
->dirs_to_hash
, ino
);
1689 int e2fsck_process_bad_inode(e2fsck_t ctx
, ext2_ino_t dir
,
1690 ext2_ino_t ino
, char *buf
)
1692 ext2_filsys fs
= ctx
->fs
;
1693 struct ext2_inode inode
;
1694 int inode_modified
= 0;
1696 unsigned char *frag
, *fsize
;
1697 struct problem_context pctx
;
1698 problem_t problem
= 0;
1700 e2fsck_read_inode(ctx
, ino
, &inode
, "process_bad_inode");
1702 clear_problem_context(&pctx
);
1705 pctx
.inode
= &inode
;
1707 if (ext2fs_file_acl_block(fs
, &inode
) &&
1708 !ext2fs_has_feature_xattr(fs
->super
)) {
1709 if (fix_problem(ctx
, PR_2_FILE_ACL_ZERO
, &pctx
)) {
1710 ext2fs_file_acl_block_set(fs
, &inode
, 0);
1716 if (!LINUX_S_ISDIR(inode
.i_mode
) && !LINUX_S_ISREG(inode
.i_mode
) &&
1717 !LINUX_S_ISCHR(inode
.i_mode
) && !LINUX_S_ISBLK(inode
.i_mode
) &&
1718 !LINUX_S_ISLNK(inode
.i_mode
) && !LINUX_S_ISFIFO(inode
.i_mode
) &&
1719 !(LINUX_S_ISSOCK(inode
.i_mode
)))
1720 problem
= PR_2_BAD_MODE
;
1721 else if (LINUX_S_ISCHR(inode
.i_mode
)
1722 && !e2fsck_pass1_check_device_inode(fs
, &inode
))
1723 problem
= PR_2_BAD_CHAR_DEV
;
1724 else if (LINUX_S_ISBLK(inode
.i_mode
)
1725 && !e2fsck_pass1_check_device_inode(fs
, &inode
))
1726 problem
= PR_2_BAD_BLOCK_DEV
;
1727 else if (LINUX_S_ISFIFO(inode
.i_mode
)
1728 && !e2fsck_pass1_check_device_inode(fs
, &inode
))
1729 problem
= PR_2_BAD_FIFO
;
1730 else if (LINUX_S_ISSOCK(inode
.i_mode
)
1731 && !e2fsck_pass1_check_device_inode(fs
, &inode
))
1732 problem
= PR_2_BAD_SOCKET
;
1733 else if (LINUX_S_ISLNK(inode
.i_mode
)
1734 && !e2fsck_pass1_check_symlink(fs
, ino
, &inode
, buf
)) {
1735 problem
= PR_2_INVALID_SYMLINK
;
1739 if (fix_problem(ctx
, problem
, &pctx
)) {
1740 deallocate_inode(ctx
, ino
, 0);
1741 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
1749 if (inode
.i_faddr
) {
1750 if (fix_problem(ctx
, PR_2_FADDR_ZERO
, &pctx
)) {
1757 switch (fs
->super
->s_creator_os
) {
1759 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
1760 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
1765 if (frag
&& *frag
) {
1767 if (fix_problem(ctx
, PR_2_FRAG_ZERO
, &pctx
)) {
1774 if (fsize
&& *fsize
) {
1776 if (fix_problem(ctx
, PR_2_FSIZE_ZERO
, &pctx
)) {
1784 if ((fs
->super
->s_creator_os
== EXT2_OS_LINUX
) &&
1785 !ext2fs_has_feature_huge_file(fs
->super
) &&
1786 (inode
.osd2
.linux2
.l_i_blocks_hi
!= 0)) {
1787 pctx
.num
= inode
.osd2
.linux2
.l_i_blocks_hi
;
1788 if (fix_problem(ctx
, PR_2_BLOCKS_HI_ZERO
, &pctx
)) {
1789 inode
.osd2
.linux2
.l_i_blocks_hi
= 0;
1794 if ((fs
->super
->s_creator_os
== EXT2_OS_LINUX
) &&
1795 !ext2fs_has_feature_64bit(fs
->super
) &&
1796 inode
.osd2
.linux2
.l_i_file_acl_high
!= 0) {
1797 pctx
.num
= inode
.osd2
.linux2
.l_i_file_acl_high
;
1798 if (fix_problem(ctx
, PR_2_I_FILE_ACL_HI_ZERO
, &pctx
)) {
1799 inode
.osd2
.linux2
.l_i_file_acl_high
= 0;
1805 if (ext2fs_file_acl_block(fs
, &inode
) &&
1806 ((ext2fs_file_acl_block(fs
, &inode
) < fs
->super
->s_first_data_block
) ||
1807 (ext2fs_file_acl_block(fs
, &inode
) >= ext2fs_blocks_count(fs
->super
)))) {
1808 if (fix_problem(ctx
, PR_2_FILE_ACL_BAD
, &pctx
)) {
1809 ext2fs_file_acl_block_set(fs
, &inode
, 0);
1814 if (inode
.i_size_high
&&
1815 LINUX_S_ISDIR(inode
.i_mode
)) {
1816 if (fix_problem(ctx
, PR_2_DIR_SIZE_HIGH_ZERO
, &pctx
)) {
1817 inode
.i_size_high
= 0;
1824 e2fsck_write_inode(ctx
, ino
, &inode
, "process_bad_inode");
1825 if (!not_fixed
&& ctx
->inode_bad_map
)
1826 ext2fs_unmark_inode_bitmap2(ctx
->inode_bad_map
, ino
);
1831 * allocate_dir_block --- this function allocates a new directory
1832 * block for a particular inode; this is done if a directory has
1833 * a "hole" in it, or if a directory has a illegal block number
1834 * that was zeroed out and now needs to be replaced.
1836 static int allocate_dir_block(e2fsck_t ctx
,
1837 struct ext2_db_entry2
*db
,
1838 char *buf
EXT2FS_ATTR((unused
)),
1839 struct problem_context
*pctx
)
1841 ext2_filsys fs
= ctx
->fs
;
1844 struct ext2_inode inode
;
1846 if (fix_problem(ctx
, PR_2_DIRECTORY_HOLE
, pctx
) == 0)
1850 * Read the inode and block bitmaps in; we'll be messing with
1853 e2fsck_read_bitmaps(ctx
);
1856 * First, find a free block
1858 e2fsck_read_inode(ctx
, db
->ino
, &inode
, "allocate_dir_block");
1859 pctx
->errcode
= ext2fs_map_cluster_block(fs
, db
->ino
, &inode
,
1860 db
->blockcnt
, &blk
);
1861 if (pctx
->errcode
|| blk
== 0) {
1862 blk
= ext2fs_find_inode_goal(fs
, db
->ino
, &inode
, db
->blockcnt
);
1863 pctx
->errcode
= ext2fs_new_block2(fs
, blk
,
1864 ctx
->block_found_map
, &blk
);
1865 if (pctx
->errcode
) {
1866 pctx
->str
= "ext2fs_new_block";
1867 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);
1871 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, blk
);
1872 ext2fs_mark_block_bitmap2(fs
->block_map
, blk
);
1873 ext2fs_mark_bb_dirty(fs
);
1876 * Now let's create the actual data block for the inode
1879 pctx
->errcode
= ext2fs_new_dir_block(fs
, 0, 0, &block
);
1881 pctx
->errcode
= ext2fs_new_dir_block(fs
, db
->ino
,
1882 EXT2_ROOT_INO
, &block
);
1884 if (pctx
->errcode
) {
1885 pctx
->str
= "ext2fs_new_dir_block";
1886 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);
1890 pctx
->errcode
= ext2fs_write_dir_block4(fs
, blk
, block
, 0, db
->ino
);
1891 ext2fs_free_mem(&block
);
1892 if (pctx
->errcode
) {
1893 pctx
->str
= "ext2fs_write_dir_block";
1894 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);
1899 * Update the inode block count
1901 ext2fs_iblk_add_blocks(fs
, &inode
, 1);
1902 if (EXT2_I_SIZE(&inode
) < ((__u64
) db
->blockcnt
+1) * fs
->blocksize
) {
1903 pctx
->errcode
= ext2fs_inode_size_set(fs
, &inode
,
1904 (db
->blockcnt
+1) * fs
->blocksize
);
1905 if (pctx
->errcode
) {
1906 pctx
->str
= "ext2fs_inode_size_set";
1907 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);
1911 e2fsck_write_inode(ctx
, db
->ino
, &inode
, "allocate_dir_block");
1914 * Finally, update the block pointers for the inode
1917 pctx
->errcode
= ext2fs_bmap2(fs
, db
->ino
, &inode
, 0, BMAP_SET
,
1918 db
->blockcnt
, 0, &blk
);
1919 if (pctx
->errcode
) {
1920 pctx
->str
= "ext2fs_block_iterate";
1921 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);