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
);
290 #define MAX_DEPTH 32000
291 static int htree_depth(struct dx_dir_info
*dx_dir
,
292 struct dx_dirblock_info
*dx_db
)
296 while (dx_db
->type
!= DX_DIRBLOCK_ROOT
&& depth
< MAX_DEPTH
) {
297 dx_db
= &dx_dir
->dx_block
[dx_db
->parent
];
303 static int dict_de_cmp(const void *a
, const void *b
)
305 const struct ext2_dir_entry
*de_a
, *de_b
;
308 de_a
= (const struct ext2_dir_entry
*) a
;
309 a_len
= ext2fs_dirent_name_len(de_a
);
310 de_b
= (const struct ext2_dir_entry
*) b
;
311 b_len
= ext2fs_dirent_name_len(de_b
);
314 return (a_len
- b_len
);
316 return memcmp(de_a
->name
, de_b
->name
, a_len
);
320 * This is special sort function that makes sure that directory blocks
321 * with a dirblock of zero are sorted to the beginning of the list.
322 * This guarantees that the root node of the htree directories are
323 * processed first, so we know what hash version to use.
325 static EXT2_QSORT_TYPE
special_dir_block_cmp(const void *a
, const void *b
)
327 const struct ext2_db_entry2
*db_a
=
328 (const struct ext2_db_entry2
*) a
;
329 const struct ext2_db_entry2
*db_b
=
330 (const struct ext2_db_entry2
*) b
;
332 if (db_a
->blockcnt
&& !db_b
->blockcnt
)
335 if (!db_a
->blockcnt
&& db_b
->blockcnt
)
338 if (db_a
->blk
!= db_b
->blk
)
339 return (int) (db_a
->blk
- db_b
->blk
);
341 if (db_a
->ino
!= db_b
->ino
)
342 return (int) (db_a
->ino
- db_b
->ino
);
344 return (int) (db_a
->blockcnt
- db_b
->blockcnt
);
349 * Make sure the first entry in the directory is '.', and that the
350 * directory entry is sane.
352 static int check_dot(e2fsck_t ctx
,
353 struct ext2_dir_entry
*dirent
,
354 ext2_ino_t ino
, struct problem_context
*pctx
)
356 struct ext2_dir_entry
*nextdir
;
357 unsigned int rec_len
, new_len
;
360 problem_t problem
= 0;
363 problem
= PR_2_MISSING_DOT
;
364 else if ((ext2fs_dirent_name_len(dirent
) != 1) ||
365 (dirent
->name
[0] != '.'))
366 problem
= PR_2_1ST_NOT_DOT
;
367 else if (dirent
->name
[1] != '\0')
368 problem
= PR_2_DOT_NULL_TERM
;
370 (void) ext2fs_get_rec_len(ctx
->fs
, dirent
, &rec_len
);
372 if (fix_problem(ctx
, problem
, pctx
)) {
374 rec_len
= dirent
->rec_len
= 12;
376 ext2fs_dirent_set_name_len(dirent
, 1);
377 ext2fs_dirent_set_file_type(dirent
, EXT2_FT_UNKNOWN
);
378 dirent
->name
[0] = '.';
379 dirent
->name
[1] = '\0';
384 if (dirent
->inode
!= ino
) {
385 if (fix_problem(ctx
, PR_2_BAD_INODE_DOT
, pctx
)) {
391 new_len
= rec_len
- 12;
394 fix_problem(ctx
, PR_2_SPLIT_DOT
, pctx
)) {
395 nextdir
= (struct ext2_dir_entry
*)
396 ((char *) dirent
+ 12);
397 dirent
->rec_len
= 12;
398 (void) ext2fs_set_rec_len(ctx
->fs
, new_len
,
401 ext2fs_dirent_set_name_len(nextdir
, 0);
402 ext2fs_dirent_set_file_type(nextdir
,
412 * Make sure the second entry in the directory is '..', and that the
413 * directory entry is sane. We do not check the inode number of '..'
414 * here; this gets done in pass 3.
416 static int check_dotdot(e2fsck_t ctx
,
417 struct ext2_dir_entry
*dirent
,
418 ext2_ino_t ino
, struct problem_context
*pctx
)
420 problem_t problem
= 0;
421 unsigned int rec_len
;
424 problem
= PR_2_MISSING_DOT_DOT
;
425 else if ((ext2fs_dirent_name_len(dirent
) != 2) ||
426 (dirent
->name
[0] != '.') ||
427 (dirent
->name
[1] != '.'))
428 problem
= PR_2_2ND_NOT_DOT_DOT
;
429 else if (dirent
->name
[2] != '\0')
430 problem
= PR_2_DOT_DOT_NULL_TERM
;
432 (void) ext2fs_get_rec_len(ctx
->fs
, dirent
, &rec_len
);
434 if (fix_problem(ctx
, problem
, pctx
)) {
436 dirent
->rec_len
= 12;
438 * Note: we don't have the parent inode just
439 * yet, so we will fill it in with the root
440 * inode. This will get fixed in pass 3.
442 dirent
->inode
= EXT2_ROOT_INO
;
443 ext2fs_dirent_set_name_len(dirent
, 2);
444 ext2fs_dirent_set_file_type(dirent
, EXT2_FT_UNKNOWN
);
445 dirent
->name
[0] = '.';
446 dirent
->name
[1] = '.';
447 dirent
->name
[2] = '\0';
452 if (e2fsck_dir_info_set_dotdot(ctx
, ino
, dirent
->inode
)) {
453 fix_problem(ctx
, PR_2_NO_DIRINFO
, pctx
);
460 * Check to make sure a directory entry doesn't contain any illegal
463 static int check_name(e2fsck_t ctx
,
464 struct ext2_dir_entry
*dirent
,
465 struct problem_context
*pctx
)
471 for ( i
= 0; i
< ext2fs_dirent_name_len(dirent
); i
++) {
472 if (dirent
->name
[i
] != '/' && dirent
->name
[i
] != '\0')
475 fixup
= fix_problem(ctx
, PR_2_BAD_NAME
, pctx
);
478 dirent
->name
[i
] = '.';
484 static int encrypted_check_name(e2fsck_t ctx
,
485 struct ext2_dir_entry
*dirent
,
486 struct problem_context
*pctx
)
488 if (ext2fs_dirent_name_len(dirent
) < EXT4_CRYPTO_BLOCK_SIZE
) {
489 if (fix_problem(ctx
, PR_2_BAD_ENCRYPTED_NAME
, pctx
)) {
493 ext2fs_unmark_valid(ctx
->fs
);
499 * Check the directory filetype (if present)
501 static _INLINE_
int check_filetype(e2fsck_t ctx
,
502 struct ext2_dir_entry
*dirent
,
503 ext2_ino_t dir_ino
EXT2FS_ATTR((unused
)),
504 struct problem_context
*pctx
)
506 int filetype
= ext2fs_dirent_file_type(dirent
);
507 int should_be
= EXT2_FT_UNKNOWN
;
508 struct ext2_inode inode
;
510 if (!ext2fs_has_feature_filetype(ctx
->fs
->super
)) {
512 !fix_problem(ctx
, PR_2_CLEAR_FILETYPE
, pctx
))
514 ext2fs_dirent_set_file_type(dirent
, EXT2_FT_UNKNOWN
);
518 if (ext2fs_test_inode_bitmap2(ctx
->inode_dir_map
, dirent
->inode
)) {
519 should_be
= EXT2_FT_DIR
;
520 } else if (ext2fs_test_inode_bitmap2(ctx
->inode_reg_map
,
522 should_be
= EXT2_FT_REG_FILE
;
523 } else if (ctx
->inode_bad_map
&&
524 ext2fs_test_inode_bitmap2(ctx
->inode_bad_map
,
528 e2fsck_read_inode(ctx
, dirent
->inode
, &inode
,
530 should_be
= ext2_file_type(inode
.i_mode
);
532 if (filetype
== should_be
)
534 pctx
->num
= should_be
;
536 if (fix_problem(ctx
, filetype
? PR_2_BAD_FILETYPE
: PR_2_SET_FILETYPE
,
540 ext2fs_dirent_set_file_type(dirent
, should_be
);
544 static void parse_int_node(ext2_filsys fs
,
545 struct ext2_db_entry2
*db
,
546 struct check_dir_struct
*cd
,
547 struct dx_dir_info
*dx_dir
,
548 char *block_buf
, int failed_csum
)
550 struct ext2_dx_root_info
*root
;
551 struct ext2_dx_entry
*ent
;
552 struct ext2_dx_countlimit
*limit
;
553 struct dx_dirblock_info
*dx_db
;
554 int i
, expect_limit
, count
;
556 ext2_dirhash_t min_hash
= 0xffffffff;
557 ext2_dirhash_t max_hash
= 0;
558 ext2_dirhash_t hash
= 0, prev_hash
;
561 if (db
->blockcnt
== 0) {
562 root
= (struct ext2_dx_root_info
*) (block_buf
+ 24);
565 printf("Root node dump:\n");
566 printf("\t Reserved zero: %u\n", root
->reserved_zero
);
567 printf("\t Hash Version: %d\n", root
->hash_version
);
568 printf("\t Info length: %d\n", root
->info_length
);
569 printf("\t Indirect levels: %d\n", root
->indirect_levels
);
570 printf("\t Flags: %d\n", root
->unused_flags
);
573 ent
= (struct ext2_dx_entry
*) (block_buf
+ 24 + root
->info_length
);
576 (e2fsck_dir_will_be_rehashed(cd
->ctx
, cd
->pctx
.ino
) ||
577 fix_problem(cd
->ctx
, PR_2_HTREE_ROOT_CSUM_INVALID
,
581 ent
= (struct ext2_dx_entry
*) (block_buf
+8);
584 (e2fsck_dir_will_be_rehashed(cd
->ctx
, cd
->pctx
.ino
) ||
585 fix_problem(cd
->ctx
, PR_2_HTREE_NODE_CSUM_INVALID
,
590 limit
= (struct ext2_dx_countlimit
*) ent
;
593 printf("Number of entries (count): %d\n",
594 ext2fs_le16_to_cpu(limit
->count
));
595 printf("Number of entries (limit): %d\n",
596 ext2fs_le16_to_cpu(limit
->limit
));
599 count
= ext2fs_le16_to_cpu(limit
->count
);
600 if (ext2fs_has_feature_metadata_csum(fs
->super
))
601 csum_size
= sizeof(struct ext2_dx_tail
);
602 expect_limit
= (fs
->blocksize
-
603 (csum_size
+ ((char *) ent
- block_buf
))) /
604 sizeof(struct ext2_dx_entry
);
605 if (ext2fs_le16_to_cpu(limit
->limit
) != expect_limit
) {
606 cd
->pctx
.num
= ext2fs_le16_to_cpu(limit
->limit
);
607 if (fix_problem(cd
->ctx
, PR_2_HTREE_BAD_LIMIT
, &cd
->pctx
))
610 if (count
> expect_limit
) {
611 cd
->pctx
.num
= count
;
612 if (fix_problem(cd
->ctx
, PR_2_HTREE_BAD_COUNT
, &cd
->pctx
))
614 count
= expect_limit
;
617 for (i
=0; i
< count
; i
++) {
619 hash
= i
? (ext2fs_le32_to_cpu(ent
[i
].hash
) & ~1) : 0;
621 printf("Entry #%d: Hash 0x%08x, block %u\n", i
,
622 hash
, ext2fs_le32_to_cpu(ent
[i
].block
));
624 blk
= ext2fs_le32_to_cpu(ent
[i
].block
) & 0x0ffffff;
625 /* Check to make sure the block is valid */
626 if (blk
>= (blk_t
) dx_dir
->numblocks
) {
628 if (fix_problem(cd
->ctx
, PR_2_HTREE_BADBLK
,
633 if (hash
< prev_hash
&&
634 fix_problem(cd
->ctx
, PR_2_HTREE_HASH_ORDER
, &cd
->pctx
))
636 dx_db
= &dx_dir
->dx_block
[blk
];
637 if (dx_db
->flags
& DX_FLAG_REFERENCED
) {
638 dx_db
->flags
|= DX_FLAG_DUP_REF
;
640 dx_db
->flags
|= DX_FLAG_REFERENCED
;
641 dx_db
->parent
= db
->blockcnt
;
647 dx_db
->node_min_hash
= hash
;
649 dx_db
->node_max_hash
=
650 ext2fs_le32_to_cpu(ent
[i
+1].hash
) & ~1;
652 dx_db
->node_max_hash
= 0xfffffffe;
653 dx_db
->flags
|= DX_FLAG_LAST
;
656 dx_db
->flags
|= DX_FLAG_FIRST
;
659 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
660 db
->blockcnt
, min_hash
, max_hash
);
662 dx_db
= &dx_dir
->dx_block
[db
->blockcnt
];
663 dx_db
->min_hash
= min_hash
;
664 dx_db
->max_hash
= max_hash
;
668 clear_htree(cd
->ctx
, cd
->pctx
.ino
);
669 dx_dir
->numblocks
= 0;
670 e2fsck_rehash_dir_later(cd
->ctx
, cd
->pctx
.ino
);
674 * Given a busted directory, try to salvage it somehow.
677 static void salvage_directory(ext2_filsys fs
,
678 struct ext2_dir_entry
*dirent
,
679 struct ext2_dir_entry
*prev
,
680 unsigned int *offset
,
681 unsigned int block_len
)
683 char *cp
= (char *) dirent
;
685 unsigned int rec_len
, prev_rec_len
;
686 unsigned int name_len
;
689 * If the space left for the entry is too small to be an entry,
690 * we can't access dirent's fields, so plumb in the values needed
691 * so that the previous entry absorbs this one.
693 if (block_len
- *offset
< EXT2_DIR_ENTRY_HEADER_LEN
) {
695 rec_len
= block_len
- *offset
;
697 name_len
= ext2fs_dirent_name_len(dirent
);
698 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
700 left
= block_len
- *offset
- rec_len
;
703 * Special case of directory entry of size 8: copy what's left
704 * of the directory block up to cover up the invalid hole.
706 if ((left
>= 12) && (rec_len
== EXT2_DIR_ENTRY_HEADER_LEN
)) {
707 memmove(cp
, cp
+EXT2_DIR_ENTRY_HEADER_LEN
, left
);
708 memset(cp
+ left
, 0, EXT2_DIR_ENTRY_HEADER_LEN
);
712 * If the directory entry overruns the end of the directory
713 * block, and the name is small enough to fit, then adjust the
717 ((int) rec_len
+ left
> EXT2_DIR_ENTRY_HEADER_LEN
) &&
718 ((int) name_len
+ EXT2_DIR_ENTRY_HEADER_LEN
<= (int) rec_len
+ left
) &&
719 dirent
->inode
<= fs
->super
->s_inodes_count
&&
720 strnlen(dirent
->name
, name_len
) == name_len
) {
721 (void) ext2fs_set_rec_len(fs
, (int) rec_len
+ left
, dirent
);
725 * If the record length of the directory entry is a multiple
726 * of four, and not too big, such that it is valid, let the
727 * previous directory entry absorb the invalid one.
729 if (prev
&& rec_len
&& (rec_len
% 4) == 0 &&
730 (*offset
+ rec_len
<= block_len
)) {
731 (void) ext2fs_get_rec_len(fs
, prev
, &prev_rec_len
);
732 prev_rec_len
+= rec_len
;
733 (void) ext2fs_set_rec_len(fs
, prev_rec_len
, prev
);
738 * Default salvage method --- kill all of the directory
739 * entries for the rest of the block. We will either try to
740 * absorb it into the previous directory entry, or create a
741 * new empty directory entry the rest of the directory block.
744 (void) ext2fs_get_rec_len(fs
, prev
, &prev_rec_len
);
745 prev_rec_len
+= block_len
- *offset
;
746 (void) ext2fs_set_rec_len(fs
, prev_rec_len
, prev
);
747 *offset
= fs
->blocksize
;
749 rec_len
= block_len
- *offset
;
750 (void) ext2fs_set_rec_len(fs
, rec_len
, dirent
);
751 ext2fs_dirent_set_name_len(dirent
, 0);
752 ext2fs_dirent_set_file_type(dirent
, EXT2_FT_UNKNOWN
);
757 #define NEXT_DIRENT(d) ((void *)((char *)(d) + (d)->rec_len))
758 static errcode_t
insert_dirent_tail(ext2_filsys fs
, void *dirbuf
)
760 struct ext2_dir_entry
*d
;
762 struct ext2_dir_entry_tail
*t
;
765 top
= EXT2_DIRENT_TAIL(dirbuf
, fs
->blocksize
);
767 while (d
->rec_len
&& !(d
->rec_len
& 0x3) && NEXT_DIRENT(d
) <= top
)
771 unsigned int min_size
= EXT2_DIR_REC_LEN(
772 ext2fs_dirent_name_len(dirbuf
));
773 if (min_size
> (char *)top
- (char *)d
)
774 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM
;
775 d
->rec_len
= (char *)top
- (char *)d
;
778 t
= (struct ext2_dir_entry_tail
*)top
;
779 if (t
->det_reserved_zero1
||
780 t
->det_rec_len
!= sizeof(struct ext2_dir_entry_tail
) ||
781 t
->det_reserved_name_len
!= EXT2_DIR_NAME_LEN_CSUM
)
782 ext2fs_initialize_dirent_tail(fs
, t
);
788 static errcode_t
fix_inline_dir_size(e2fsck_t ctx
, ext2_ino_t ino
,
789 size_t *inline_data_size
,
790 struct problem_context
*pctx
,
793 ext2_filsys fs
= ctx
->fs
;
794 struct ext2_inode inode
;
795 size_t new_size
, old_size
;
798 old_size
= *inline_data_size
;
800 * If there's not enough bytes to start the "second" dir block
801 * (in the EA space) then truncate everything to the first block.
803 if (old_size
> EXT4_MIN_INLINE_DATA_SIZE
&&
804 old_size
< EXT4_MIN_INLINE_DATA_SIZE
+
805 EXT2_DIR_REC_LEN(1)) {
806 old_size
= EXT4_MIN_INLINE_DATA_SIZE
;
809 /* Increase to the next four-byte boundary for salvaging */
810 new_size
= old_size
+ (4 - (old_size
& 3));
811 memset(buf
+ old_size
, 0, new_size
- old_size
);
812 retval
= ext2fs_inline_data_set(fs
, ino
, 0, buf
, new_size
);
813 if (retval
== EXT2_ET_INLINE_DATA_NO_SPACE
) {
814 /* Or we can't, so truncate. */
816 retval
= ext2fs_inline_data_set(fs
, ino
, 0, buf
, new_size
);
818 if (fix_problem(ctx
, PR_2_FIX_INLINE_DIR_FAILED
,
826 if (fix_problem(ctx
, PR_2_FIX_INLINE_DIR_FAILED
,
835 retval
= ext2fs_read_inode(fs
, ino
, &inode
);
839 retval
= ext2fs_inode_size_set(fs
, &inode
, new_size
);
843 inode
.i_flags
&= ~EXT4_INLINE_DATA_FL
;
844 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
847 *inline_data_size
= new_size
;
853 static int check_dir_block2(ext2_filsys fs
,
854 struct ext2_db_entry2
*db
,
858 struct check_dir_struct
*cd
= priv_data
;
860 if (cd
->ra_entries
&& cd
->list_offset
>= cd
->next_ra_off
) {
861 err
= e2fsck_readahead_dblist(fs
,
862 E2FSCK_RA_DBLIST_IGNORE_BLOCKCNT
,
864 cd
->list_offset
+ cd
->ra_entries
/ 8,
868 cd
->next_ra_off
= cd
->list_offset
+ (cd
->ra_entries
* 7 / 8);
871 err
= check_dir_block(fs
, db
, priv_data
);
876 static int check_dir_block(ext2_filsys fs
,
877 struct ext2_db_entry2
*db
,
880 struct dx_dir_info
*dx_dir
;
881 struct dx_dirblock_info
*dx_db
= 0;
882 struct ext2_dir_entry
*dirent
, *prev
, dot
, dotdot
;
884 unsigned int offset
= 0;
885 int dir_modified
= 0;
887 unsigned int rec_len
;
888 blk64_t block_nr
= db
->blk
;
889 ext2_ino_t ino
= db
->ino
;
890 ext2_ino_t subdir_parent
;
892 struct check_dir_struct
*cd
;
896 struct ext2_dx_root_info
*root
;
897 struct ext2_dx_countlimit
*limit
;
898 static dict_t de_dict
;
899 struct problem_context pctx
;
902 int dx_csum_size
= 0, de_csum_size
= 0;
905 size_t inline_data_size
= 0;
908 size_t max_block_size
;
910 cd
= (struct check_dir_struct
*) priv_data
;
911 ibuf
= buf
= cd
->buf
;
914 if (ctx
->flags
& E2F_FLAG_RUN_RETURN
)
917 if (ctx
->progress
&& (ctx
->progress
)(ctx
, 2, cd
->count
++, cd
->max
))
920 if (ext2fs_has_feature_metadata_csum(fs
->super
)) {
921 dx_csum_size
= sizeof(struct ext2_dx_tail
);
922 de_csum_size
= sizeof(struct ext2_dir_entry_tail
);
925 if (ext2fs_has_feature_filetype(fs
->super
))
926 filetype
= EXT2_FT_DIR
<< 8;
929 * Make sure the inode is still in use (could have been
930 * deleted in the duplicate/bad blocks pass.
932 if (!(ext2fs_test_inode_bitmap2(ctx
->inode_used_map
, ino
)))
936 cd
->pctx
.blk
= block_nr
;
937 cd
->pctx
.blkcount
= db
->blockcnt
;
942 if (ext2fs_has_feature_inline_data(fs
->super
)) {
945 ec
= ext2fs_inline_data_size(fs
, ino
, &inline_data_size
);
946 if (ec
&& ec
!= EXT2_ET_NO_INLINE_DATA
)
950 if (db
->blk
== 0 && !inline_data_size
) {
951 if (allocate_dir_block(ctx
, db
, buf
, &cd
->pctx
))
961 if (ctx
->dirs_to_hash
&&
962 ext2fs_u32_list_test(ctx
->dirs_to_hash
, ino
))
966 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr
,
970 ehandler_operation(_("reading directory block"));
971 if (inline_data_size
) {
972 memset(buf
, 0, fs
->blocksize
- inline_data_size
);
973 cd
->pctx
.errcode
= ext2fs_inline_data_get(fs
, ino
, 0, buf
, 0);
974 if (cd
->pctx
.errcode
)
975 goto inline_read_fail
;
976 #ifdef WORDS_BIGENDIAN
978 goto skip_first_read_swab
;
979 *((__u32
*)buf
) = ext2fs_le32_to_cpu(*((__u32
*)buf
));
980 cd
->pctx
.errcode
= ext2fs_dirent_swab_in2(fs
,
981 buf
+ EXT4_INLINE_DATA_DOTDOT_SIZE
,
982 EXT4_MIN_INLINE_DATA_SIZE
- EXT4_INLINE_DATA_DOTDOT_SIZE
,
984 if (cd
->pctx
.errcode
)
985 goto inline_read_fail
;
986 skip_first_read_swab
:
987 if (inline_data_size
<= EXT4_MIN_INLINE_DATA_SIZE
||
989 goto inline_read_fail
;
990 cd
->pctx
.errcode
= ext2fs_dirent_swab_in2(fs
,
991 buf
+ EXT4_MIN_INLINE_DATA_SIZE
,
992 inline_data_size
- EXT4_MIN_INLINE_DATA_SIZE
,
996 cd
->pctx
.errcode
= ext2fs_read_dir_block4(fs
, block_nr
,
1000 pctx
.num
= inline_data_size
;
1001 if (((inline_data_size
& 3) ||
1002 (inline_data_size
> EXT4_MIN_INLINE_DATA_SIZE
&&
1003 inline_data_size
< EXT4_MIN_INLINE_DATA_SIZE
+
1004 EXT2_DIR_REC_LEN(1))) &&
1005 fix_problem(ctx
, PR_2_BAD_INLINE_DIR_SIZE
, &pctx
)) {
1006 errcode_t err
= fix_inline_dir_size(ctx
, ino
,
1007 &inline_data_size
, &pctx
,
1010 return DIRENT_ABORT
;
1013 ehandler_operation(0);
1014 if (cd
->pctx
.errcode
== EXT2_ET_DIR_CORRUPTED
)
1015 cd
->pctx
.errcode
= 0; /* We'll handle this ourselves */
1016 else if (cd
->pctx
.errcode
== EXT2_ET_DIR_CSUM_INVALID
) {
1017 cd
->pctx
.errcode
= 0; /* We'll handle this ourselves */
1020 if (cd
->pctx
.errcode
) {
1022 if (!fix_problem(ctx
, PR_2_READ_DIRBLOCK
, &cd
->pctx
)) {
1023 ctx
->flags
|= E2F_FLAG_ABORT
;
1024 return DIRENT_ABORT
;
1026 ext2fs_new_dir_block(fs
, db
->blockcnt
== 0 ? ino
: 0,
1027 EXT2_ROOT_INO
, &buf2
);
1028 memcpy(buf
, buf2
, fs
->blocksize
);
1029 ext2fs_free_mem(&buf2
);
1031 dx_dir
= e2fsck_get_dx_dir_info(ctx
, ino
);
1032 if (dx_dir
&& dx_dir
->numblocks
) {
1033 if (db
->blockcnt
>= dx_dir
->numblocks
) {
1035 if (fix_problem(ctx
, PR_2_UNEXPECTED_HTREE_BLOCK
,
1037 clear_htree(ctx
, ino
);
1038 dx_dir
->numblocks
= 0;
1042 fatal_error(ctx
, _("Can not continue."));
1044 dx_db
= &dx_dir
->dx_block
[db
->blockcnt
];
1045 dx_db
->type
= DX_DIRBLOCK_LEAF
;
1046 dx_db
->phys
= block_nr
;
1047 dx_db
->min_hash
= ~0;
1048 dx_db
->max_hash
= 0;
1050 dirent
= (struct ext2_dir_entry
*) buf
;
1051 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
1052 limit
= (struct ext2_dx_countlimit
*) (buf
+8);
1053 if (db
->blockcnt
== 0) {
1054 root
= (struct ext2_dx_root_info
*) (buf
+ 24);
1055 dx_db
->type
= DX_DIRBLOCK_ROOT
;
1056 dx_db
->flags
|= DX_FLAG_FIRST
| DX_FLAG_LAST
;
1057 if ((root
->reserved_zero
||
1058 root
->info_length
< 8 ||
1059 root
->indirect_levels
> 1) &&
1060 fix_problem(ctx
, PR_2_HTREE_BAD_ROOT
, &cd
->pctx
)) {
1061 clear_htree(ctx
, ino
);
1062 dx_dir
->numblocks
= 0;
1065 dx_dir
->hashversion
= root
->hash_version
;
1066 if ((dx_dir
->hashversion
<= EXT2_HASH_TEA
) &&
1067 (fs
->super
->s_flags
& EXT2_FLAGS_UNSIGNED_HASH
))
1068 dx_dir
->hashversion
+= 3;
1069 dx_dir
->depth
= root
->indirect_levels
+ 1;
1070 } else if ((dirent
->inode
== 0) &&
1071 (rec_len
== fs
->blocksize
) &&
1072 (ext2fs_dirent_name_len(dirent
) == 0) &&
1073 (ext2fs_le16_to_cpu(limit
->limit
) ==
1074 ((fs
->blocksize
- (8 + dx_csum_size
)) /
1075 sizeof(struct ext2_dx_entry
))))
1076 dx_db
->type
= DX_DIRBLOCK_NODE
;
1077 is_leaf
= (dx_db
->type
== DX_DIRBLOCK_LEAF
);
1081 /* Leaf node with no space for csum? Rebuild dirs in pass 3A. */
1082 if (is_leaf
&& !inline_data_size
&& failed_csum
&&
1083 !ext2fs_dirent_has_tail(fs
, (struct ext2_dir_entry
*)buf
)) {
1085 if (e2fsck_dir_will_be_rehashed(ctx
, ino
)) {
1089 if (!fix_problem(cd
->ctx
, PR_2_LEAF_NODE_MISSING_CSUM
,
1092 e2fsck_rehash_dir_later(ctx
, ino
);
1096 /* htree nodes don't use fake dirents to store checksums */
1101 if (inline_data_size
) {
1103 buf
+= EXT4_MIN_INLINE_DATA_SIZE
;
1104 max_block_size
= inline_data_size
- EXT4_MIN_INLINE_DATA_SIZE
;
1105 /* Zero-length second block, just exit */
1106 if (max_block_size
== 0)
1109 max_block_size
= EXT4_MIN_INLINE_DATA_SIZE
;
1112 max_block_size
= fs
->blocksize
- de_csum_size
;
1114 if (ctx
->encrypted_dirs
)
1115 encrypted
= ext2fs_u32_list_test(ctx
->encrypted_dirs
, ino
);
1117 dict_init(&de_dict
, DICTCOUNT_T_MAX
, dict_de_cmp
);
1121 ext2_ino_t first_unused_inode
;
1122 unsigned int name_len
;
1125 if (!inline_data_size
|| dot_state
> 1) {
1126 dirent
= (struct ext2_dir_entry
*) (buf
+ offset
);
1128 * If there's not even space for the entry header,
1129 * force salvaging this dir.
1131 if (max_block_size
- offset
< EXT2_DIR_ENTRY_HEADER_LEN
)
1132 rec_len
= EXT2_DIR_REC_LEN(1);
1134 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
1135 cd
->pctx
.dirent
= dirent
;
1136 cd
->pctx
.num
= offset
;
1137 if ((offset
+ rec_len
> max_block_size
) ||
1139 ((rec_len
% 4) != 0) ||
1140 (((unsigned) ext2fs_dirent_name_len(dirent
) + EXT2_DIR_ENTRY_HEADER_LEN
) > rec_len
)) {
1141 if (fix_problem(ctx
, PR_2_DIR_CORRUPTED
,
1143 #ifdef WORDS_BIGENDIAN
1145 * On big-endian systems, if the dirent
1146 * swap routine finds a rec_len that it
1147 * doesn't like, it continues
1148 * processing the block as if rec_len
1149 * == EXT2_DIR_ENTRY_HEADER_LEN. This means that the name
1150 * field gets byte swapped, which means
1151 * that salvage will not detect the
1152 * correct name length (unless the name
1153 * has a length that's an exact
1154 * multiple of four bytes), and it'll
1155 * discard the entry (unnecessarily)
1156 * and the rest of the dirent block.
1157 * Therefore, swap the rest of the
1158 * block back to disk order, run
1159 * salvage, and re-swap anything after
1160 * the salvaged dirent.
1162 int need_reswab
= 0;
1163 if (rec_len
< EXT2_DIR_ENTRY_HEADER_LEN
|| rec_len
% 4) {
1165 ext2fs_dirent_swab_in2(fs
,
1166 ((char *)dirent
) + EXT2_DIR_ENTRY_HEADER_LEN
,
1167 max_block_size
- offset
- EXT2_DIR_ENTRY_HEADER_LEN
,
1171 salvage_directory(fs
, dirent
, prev
,
1174 #ifdef WORDS_BIGENDIAN
1176 (void) ext2fs_get_rec_len(fs
,
1178 ext2fs_dirent_swab_in2(fs
,
1179 ((char *)dirent
) + offset
+ rec_len
,
1180 max_block_size
- offset
- rec_len
,
1187 goto abort_free_dict
;
1190 if (dot_state
== 0) {
1191 memset(&dot
, 0, sizeof(dot
));
1193 dirent
->inode
= ino
;
1194 dirent
->rec_len
= EXT2_DIR_REC_LEN(1);
1195 dirent
->name_len
= 1 | filetype
;
1196 dirent
->name
[0] = '.';
1197 } else if (dot_state
== 1) {
1198 memset(&dotdot
, 0, sizeof(dotdot
));
1201 ((struct ext2_dir_entry
*)buf
)->inode
;
1202 dirent
->rec_len
= EXT2_DIR_REC_LEN(2);
1203 dirent
->name_len
= 2 | filetype
;
1204 dirent
->name
[0] = '.';
1205 dirent
->name
[1] = '.';
1207 fatal_error(ctx
, _("Can not continue."));
1209 cd
->pctx
.dirent
= dirent
;
1210 cd
->pctx
.num
= offset
;
1213 if (dot_state
== 0) {
1214 if (check_dot(ctx
, dirent
, ino
, &cd
->pctx
))
1216 } else if (dot_state
== 1) {
1217 ret
= check_dotdot(ctx
, dirent
, ino
, &cd
->pctx
);
1219 goto abort_free_dict
;
1222 } else if (dirent
->inode
== ino
) {
1223 problem
= PR_2_LINK_DOT
;
1224 if (fix_problem(ctx
, PR_2_LINK_DOT
, &cd
->pctx
)) {
1234 * Make sure the inode listed is a legal one.
1236 name_len
= ext2fs_dirent_name_len(dirent
);
1237 if (((dirent
->inode
!= EXT2_ROOT_INO
) &&
1238 (dirent
->inode
< EXT2_FIRST_INODE(fs
->super
))) ||
1239 (dirent
->inode
> fs
->super
->s_inodes_count
)) {
1240 problem
= PR_2_BAD_INO
;
1241 } else if (ctx
->inode_bb_map
&&
1242 (ext2fs_test_inode_bitmap2(ctx
->inode_bb_map
,
1245 * If the inode is in a bad block, offer to
1248 problem
= PR_2_BB_INODE
;
1249 } else if ((dot_state
> 1) && (name_len
== 1) &&
1250 (dirent
->name
[0] == '.')) {
1252 * If there's a '.' entry in anything other
1253 * than the first directory entry, it's a
1254 * duplicate entry that should be removed.
1256 problem
= PR_2_DUP_DOT
;
1257 } else if ((dot_state
> 1) && (name_len
== 2) &&
1258 (dirent
->name
[0] == '.') &&
1259 (dirent
->name
[1] == '.')) {
1261 * If there's a '..' entry in anything other
1262 * than the second directory entry, it's a
1263 * duplicate entry that should be removed.
1265 problem
= PR_2_DUP_DOT_DOT
;
1266 } else if ((dot_state
> 1) &&
1267 (dirent
->inode
== EXT2_ROOT_INO
)) {
1269 * Don't allow links to the root directory.
1270 * We check this specially to make sure we
1271 * catch this error case even if the root
1272 * directory hasn't been created yet.
1274 problem
= PR_2_LINK_ROOT
;
1275 } else if ((dot_state
> 1) && (name_len
== 0)) {
1277 * Don't allow zero-length directory names.
1279 problem
= PR_2_NULL_NAME
;
1283 if (fix_problem(ctx
, problem
, &cd
->pctx
)) {
1288 ext2fs_unmark_valid(fs
);
1289 if (problem
== PR_2_BAD_INO
)
1295 * If the inode was marked as having bad fields in
1296 * pass1, process it and offer to fix/clear it.
1297 * (We wait until now so that we can display the
1298 * pathname to the user.)
1300 if (ctx
->inode_bad_map
&&
1301 ext2fs_test_inode_bitmap2(ctx
->inode_bad_map
,
1303 if (e2fsck_process_bad_inode(ctx
, ino
,
1305 buf
+ fs
->blocksize
)) {
1310 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
1311 return DIRENT_ABORT
;
1314 group
= ext2fs_group_of_ino(fs
, dirent
->inode
);
1315 first_unused_inode
= group
* fs
->super
->s_inodes_per_group
+
1316 1 + fs
->super
->s_inodes_per_group
-
1317 ext2fs_bg_itable_unused(fs
, group
);
1318 cd
->pctx
.group
= group
;
1321 * Check if the inode was missed out because
1322 * _INODE_UNINIT flag was set or bg_itable_unused was
1323 * incorrect. If so, clear the _INODE_UNINIT flag and
1324 * restart e2fsck. In the future it would be nice if
1325 * we could call a function in pass1.c that checks the
1326 * newly visible inodes.
1328 if (ext2fs_bg_flags_test(fs
, group
, EXT2_BG_INODE_UNINIT
)) {
1329 pctx
.num
= dirent
->inode
;
1330 if (fix_problem(ctx
, PR_2_INOREF_BG_INO_UNINIT
,
1332 ext2fs_bg_flags_clear(fs
, group
,
1333 EXT2_BG_INODE_UNINIT
);
1334 ext2fs_mark_super_dirty(fs
);
1335 ctx
->flags
|= E2F_FLAG_RESTART_LATER
;
1337 ext2fs_unmark_valid(fs
);
1338 if (problem
== PR_2_BAD_INO
)
1341 } else if (dirent
->inode
>= first_unused_inode
) {
1342 pctx
.num
= dirent
->inode
;
1343 if (fix_problem(ctx
, PR_2_INOREF_IN_UNUSED
, &cd
->pctx
)){
1344 ext2fs_bg_itable_unused_set(fs
, group
, 0);
1345 ext2fs_mark_super_dirty(fs
);
1346 ctx
->flags
|= E2F_FLAG_RESTART_LATER
;
1348 ext2fs_unmark_valid(fs
);
1349 if (problem
== PR_2_BAD_INO
)
1355 * Offer to clear unused inodes; if we are going to be
1356 * restarting the scan due to bg_itable_unused being
1357 * wrong, then don't clear any inodes to avoid zapping
1358 * inodes that were skipped during pass1 due to an
1359 * incorrect bg_itable_unused; we'll get any real
1360 * problems after we restart.
1362 if (!(ctx
->flags
& E2F_FLAG_RESTART_LATER
) &&
1363 !(ext2fs_test_inode_bitmap2(ctx
->inode_used_map
,
1365 problem
= PR_2_UNUSED_INODE
;
1368 if (fix_problem(ctx
, problem
, &cd
->pctx
)) {
1373 ext2fs_unmark_valid(fs
);
1374 if (problem
== PR_2_BAD_INO
)
1379 if (!encrypted
&& check_name(ctx
, dirent
, &cd
->pctx
))
1382 if (encrypted
&& (dot_state
) > 1 &&
1383 encrypted_check_name(ctx
, dirent
, &cd
->pctx
)) {
1388 if (check_filetype(ctx
, dirent
, ino
, &cd
->pctx
))
1392 ext2fs_dirhash(dx_dir
->hashversion
, dirent
->name
,
1393 ext2fs_dirent_name_len(dirent
),
1394 fs
->super
->s_hash_seed
, &hash
, 0);
1395 if (hash
< dx_db
->min_hash
)
1396 dx_db
->min_hash
= hash
;
1397 if (hash
> dx_db
->max_hash
)
1398 dx_db
->max_hash
= hash
;
1402 * If this is a directory, then mark its parent in its
1403 * dir_info structure. If the parent field is already
1404 * filled in, then this directory has more than one
1405 * hard link. We assume the first link is correct,
1406 * and ask the user if he/she wants to clear this one.
1408 if ((dot_state
> 1) &&
1409 (ext2fs_test_inode_bitmap2(ctx
->inode_dir_map
,
1411 if (e2fsck_dir_info_get_parent(ctx
, dirent
->inode
,
1413 cd
->pctx
.ino
= dirent
->inode
;
1414 fix_problem(ctx
, PR_2_NO_DIRINFO
, &cd
->pctx
);
1415 goto abort_free_dict
;
1417 if (subdir_parent
) {
1418 cd
->pctx
.ino2
= subdir_parent
;
1419 if (fix_problem(ctx
, PR_2_LINK_DIR
,
1427 (void) e2fsck_dir_info_set_parent(ctx
,
1428 dirent
->inode
, ino
);
1434 } else if (dict_lookup(&de_dict
, dirent
)) {
1435 clear_problem_context(&pctx
);
1437 pctx
.dirent
= dirent
;
1438 fix_problem(ctx
, PR_2_REPORT_DUP_DIRENT
, &pctx
);
1439 e2fsck_rehash_dir_later(ctx
, ino
);
1442 dict_alloc_insert(&de_dict
, dirent
, dirent
);
1444 ext2fs_icount_increment(ctx
->inode_count
, dirent
->inode
,
1447 ctx
->fs_links_count
++;
1448 ctx
->fs_total_count
++;
1452 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
1453 if (!inline_data_size
|| dot_state
> 1) {
1456 if (dot_state
== 1) {
1459 * If we get here, we're checking an inline
1460 * directory and we've just checked a (fake)
1461 * dotdot entry that we created on the stack.
1462 * Therefore set 'prev' to NULL so that if we
1463 * call salvage_directory on the next entry,
1464 * it won't try to absorb the next entry into
1465 * the on-stack dotdot entry.
1471 } while (offset
< max_block_size
);
1477 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1478 db
->blockcnt
, dx_db
->type
,
1479 dx_db
->min_hash
, dx_db
->max_hash
);
1481 cd
->pctx
.dir
= cd
->pctx
.ino
;
1482 if ((dx_db
->type
== DX_DIRBLOCK_ROOT
) ||
1483 (dx_db
->type
== DX_DIRBLOCK_NODE
))
1484 parse_int_node(fs
, db
, cd
, dx_dir
, buf
, failed_csum
);
1487 if (offset
!= max_block_size
) {
1488 cd
->pctx
.num
= rec_len
+ offset
- max_block_size
;
1489 if (fix_problem(ctx
, PR_2_FINAL_RECLEN
, &cd
->pctx
)) {
1490 dirent
->rec_len
= cd
->pctx
.num
;
1495 int flags
, will_rehash
;
1496 /* leaf block with no tail? Rehash dirs later. */
1497 if (ext2fs_has_feature_metadata_csum(fs
->super
) &&
1499 !inline_data_size
&&
1500 !ext2fs_dirent_has_tail(fs
, (struct ext2_dir_entry
*)buf
)) {
1501 if (insert_dirent_tail(fs
, buf
) == 0)
1503 e2fsck_rehash_dir_later(ctx
, ino
);
1507 will_rehash
= e2fsck_dir_will_be_rehashed(ctx
, ino
);
1509 flags
= ctx
->fs
->flags
;
1510 ctx
->fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
1512 if (inline_data_size
) {
1514 #ifdef WORDS_BIGENDIAN
1516 goto skip_first_write_swab
;
1517 *((__u32
*)buf
) = ext2fs_le32_to_cpu(*((__u32
*)buf
));
1518 cd
->pctx
.errcode
= ext2fs_dirent_swab_out2(fs
,
1519 buf
+ EXT4_INLINE_DATA_DOTDOT_SIZE
,
1520 EXT4_MIN_INLINE_DATA_SIZE
-
1521 EXT4_INLINE_DATA_DOTDOT_SIZE
,
1523 if (cd
->pctx
.errcode
)
1524 goto skip_second_write_swab
;
1525 skip_first_write_swab
:
1526 if (inline_data_size
<= EXT4_MIN_INLINE_DATA_SIZE
||
1528 goto skip_second_write_swab
;
1529 cd
->pctx
.errcode
= ext2fs_dirent_swab_out2(fs
,
1530 buf
+ EXT4_MIN_INLINE_DATA_SIZE
,
1532 EXT4_MIN_INLINE_DATA_SIZE
,
1534 skip_second_write_swab
:
1535 if (cd
->pctx
.errcode
&&
1536 !fix_problem(ctx
, PR_2_WRITE_DIRBLOCK
, &cd
->pctx
))
1537 goto abort_free_dict
;
1540 ext2fs_inline_data_set(fs
, ino
, 0, buf
,
1543 cd
->pctx
.errcode
= ext2fs_write_dir_block4(fs
, block_nr
,
1546 ctx
->fs
->flags
= (flags
&
1547 EXT2_FLAG_IGNORE_CSUM_ERRORS
) |
1549 ~EXT2_FLAG_IGNORE_CSUM_ERRORS
);
1550 if (cd
->pctx
.errcode
) {
1551 if (!fix_problem(ctx
, PR_2_WRITE_DIRBLOCK
,
1553 goto abort_free_dict
;
1555 ext2fs_mark_changed(fs
);
1556 } else if (is_leaf
&& failed_csum
&& !dir_modified
) {
1558 * If a leaf node that fails csum makes it this far without
1559 * alteration, ask the user if the checksum should be fixed.
1561 if (fix_problem(ctx
, PR_2_LEAF_NODE_ONLY_CSUM_INVALID
,
1565 dict_free_nodes(&de_dict
);
1568 ctx
->flags
|= E2F_FLAG_ABORT
;
1569 dict_free_nodes(&de_dict
);
1570 return DIRENT_ABORT
;
1579 * This function is called to deallocate a block, and is an interator
1580 * functioned called by deallocate inode via ext2fs_iterate_block().
1582 static int deallocate_inode_block(ext2_filsys fs
,
1584 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1585 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1586 int ref_offset
EXT2FS_ATTR((unused
)),
1589 struct del_block
*p
= priv_data
;
1593 if ((*block_nr
< fs
->super
->s_first_data_block
) ||
1594 (*block_nr
>= ext2fs_blocks_count(fs
->super
)))
1596 if ((*block_nr
% EXT2FS_CLUSTER_RATIO(fs
)) == 0)
1597 ext2fs_block_alloc_stats2(fs
, *block_nr
, -1);
1603 * This fuction deallocates an inode
1605 static void deallocate_inode(e2fsck_t ctx
, ext2_ino_t ino
, char* block_buf
)
1607 ext2_filsys fs
= ctx
->fs
;
1608 struct ext2_inode inode
;
1609 struct problem_context pctx
;
1611 struct del_block del_block
;
1613 e2fsck_read_inode(ctx
, ino
, &inode
, "deallocate_inode");
1614 clear_problem_context(&pctx
);
1618 * Fix up the bitmaps...
1620 e2fsck_read_bitmaps(ctx
);
1621 ext2fs_inode_alloc_stats2(fs
, ino
, -1, LINUX_S_ISDIR(inode
.i_mode
));
1623 if (ext2fs_file_acl_block(fs
, &inode
) &&
1624 ext2fs_has_feature_xattr(fs
->super
)) {
1625 pctx
.errcode
= ext2fs_adjust_ea_refcount3(fs
,
1626 ext2fs_file_acl_block(fs
, &inode
),
1627 block_buf
, -1, &count
, ino
);
1628 if (pctx
.errcode
== EXT2_ET_BAD_EA_BLOCK_NUM
) {
1633 pctx
.blk
= ext2fs_file_acl_block(fs
, &inode
);
1634 fix_problem(ctx
, PR_2_ADJ_EA_REFCOUNT
, &pctx
);
1635 ctx
->flags
|= E2F_FLAG_ABORT
;
1639 ext2fs_block_alloc_stats2(fs
,
1640 ext2fs_file_acl_block(fs
, &inode
), -1);
1642 ext2fs_file_acl_block_set(fs
, &inode
, 0);
1645 if (!ext2fs_inode_has_valid_blocks2(fs
, &inode
))
1648 /* Inline data inodes don't have blocks to iterate */
1649 if (inode
.i_flags
& EXT4_INLINE_DATA_FL
)
1652 if (LINUX_S_ISREG(inode
.i_mode
) &&
1653 ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode
)))
1656 del_block
.ctx
= ctx
;
1658 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
, 0, block_buf
,
1659 deallocate_inode_block
,
1662 fix_problem(ctx
, PR_2_DEALLOC_INODE
, &pctx
);
1663 ctx
->flags
|= E2F_FLAG_ABORT
;
1667 /* Inode may have changed by block_iterate, so reread it */
1668 e2fsck_read_inode(ctx
, ino
, &inode
, "deallocate_inode");
1669 e2fsck_clear_inode(ctx
, ino
, &inode
, 0, "deallocate_inode");
1673 * This fuction clears the htree flag on an inode
1675 static void clear_htree(e2fsck_t ctx
, ext2_ino_t ino
)
1677 struct ext2_inode inode
;
1679 e2fsck_read_inode(ctx
, ino
, &inode
, "clear_htree");
1680 inode
.i_flags
= inode
.i_flags
& ~EXT2_INDEX_FL
;
1681 e2fsck_write_inode(ctx
, ino
, &inode
, "clear_htree");
1682 if (ctx
->dirs_to_hash
)
1683 ext2fs_u32_list_add(ctx
->dirs_to_hash
, ino
);
1687 int e2fsck_process_bad_inode(e2fsck_t ctx
, ext2_ino_t dir
,
1688 ext2_ino_t ino
, char *buf
)
1690 ext2_filsys fs
= ctx
->fs
;
1691 struct ext2_inode inode
;
1692 int inode_modified
= 0;
1694 unsigned char *frag
, *fsize
;
1695 struct problem_context pctx
;
1696 problem_t problem
= 0;
1698 e2fsck_read_inode(ctx
, ino
, &inode
, "process_bad_inode");
1700 clear_problem_context(&pctx
);
1703 pctx
.inode
= &inode
;
1705 if (ext2fs_file_acl_block(fs
, &inode
) &&
1706 !ext2fs_has_feature_xattr(fs
->super
)) {
1707 if (fix_problem(ctx
, PR_2_FILE_ACL_ZERO
, &pctx
)) {
1708 ext2fs_file_acl_block_set(fs
, &inode
, 0);
1714 if (!LINUX_S_ISDIR(inode
.i_mode
) && !LINUX_S_ISREG(inode
.i_mode
) &&
1715 !LINUX_S_ISCHR(inode
.i_mode
) && !LINUX_S_ISBLK(inode
.i_mode
) &&
1716 !LINUX_S_ISLNK(inode
.i_mode
) && !LINUX_S_ISFIFO(inode
.i_mode
) &&
1717 !(LINUX_S_ISSOCK(inode
.i_mode
)))
1718 problem
= PR_2_BAD_MODE
;
1719 else if (LINUX_S_ISCHR(inode
.i_mode
)
1720 && !e2fsck_pass1_check_device_inode(fs
, &inode
))
1721 problem
= PR_2_BAD_CHAR_DEV
;
1722 else if (LINUX_S_ISBLK(inode
.i_mode
)
1723 && !e2fsck_pass1_check_device_inode(fs
, &inode
))
1724 problem
= PR_2_BAD_BLOCK_DEV
;
1725 else if (LINUX_S_ISFIFO(inode
.i_mode
)
1726 && !e2fsck_pass1_check_device_inode(fs
, &inode
))
1727 problem
= PR_2_BAD_FIFO
;
1728 else if (LINUX_S_ISSOCK(inode
.i_mode
)
1729 && !e2fsck_pass1_check_device_inode(fs
, &inode
))
1730 problem
= PR_2_BAD_SOCKET
;
1731 else if (LINUX_S_ISLNK(inode
.i_mode
)
1732 && !e2fsck_pass1_check_symlink(fs
, ino
, &inode
, buf
)) {
1733 problem
= PR_2_INVALID_SYMLINK
;
1737 if (fix_problem(ctx
, problem
, &pctx
)) {
1738 deallocate_inode(ctx
, ino
, 0);
1739 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
1747 if (inode
.i_faddr
) {
1748 if (fix_problem(ctx
, PR_2_FADDR_ZERO
, &pctx
)) {
1755 switch (fs
->super
->s_creator_os
) {
1757 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
1758 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
1763 if (frag
&& *frag
) {
1765 if (fix_problem(ctx
, PR_2_FRAG_ZERO
, &pctx
)) {
1772 if (fsize
&& *fsize
) {
1774 if (fix_problem(ctx
, PR_2_FSIZE_ZERO
, &pctx
)) {
1782 if ((fs
->super
->s_creator_os
== EXT2_OS_LINUX
) &&
1783 !ext2fs_has_feature_huge_file(fs
->super
) &&
1784 (inode
.osd2
.linux2
.l_i_blocks_hi
!= 0)) {
1785 pctx
.num
= inode
.osd2
.linux2
.l_i_blocks_hi
;
1786 if (fix_problem(ctx
, PR_2_BLOCKS_HI_ZERO
, &pctx
)) {
1787 inode
.osd2
.linux2
.l_i_blocks_hi
= 0;
1792 if ((fs
->super
->s_creator_os
== EXT2_OS_LINUX
) &&
1793 !ext2fs_has_feature_64bit(fs
->super
) &&
1794 inode
.osd2
.linux2
.l_i_file_acl_high
!= 0) {
1795 pctx
.num
= inode
.osd2
.linux2
.l_i_file_acl_high
;
1796 if (fix_problem(ctx
, PR_2_I_FILE_ACL_HI_ZERO
, &pctx
)) {
1797 inode
.osd2
.linux2
.l_i_file_acl_high
= 0;
1803 if (ext2fs_file_acl_block(fs
, &inode
) &&
1804 ((ext2fs_file_acl_block(fs
, &inode
) < fs
->super
->s_first_data_block
) ||
1805 (ext2fs_file_acl_block(fs
, &inode
) >= ext2fs_blocks_count(fs
->super
)))) {
1806 if (fix_problem(ctx
, PR_2_FILE_ACL_BAD
, &pctx
)) {
1807 ext2fs_file_acl_block_set(fs
, &inode
, 0);
1812 if (inode
.i_dir_acl
&&
1813 LINUX_S_ISDIR(inode
.i_mode
)) {
1814 if (fix_problem(ctx
, PR_2_DIR_ACL_ZERO
, &pctx
)) {
1815 inode
.i_dir_acl
= 0;
1822 e2fsck_write_inode(ctx
, ino
, &inode
, "process_bad_inode");
1823 if (!not_fixed
&& ctx
->inode_bad_map
)
1824 ext2fs_unmark_inode_bitmap2(ctx
->inode_bad_map
, ino
);
1829 * allocate_dir_block --- this function allocates a new directory
1830 * block for a particular inode; this is done if a directory has
1831 * a "hole" in it, or if a directory has a illegal block number
1832 * that was zeroed out and now needs to be replaced.
1834 static int allocate_dir_block(e2fsck_t ctx
,
1835 struct ext2_db_entry2
*db
,
1836 char *buf
EXT2FS_ATTR((unused
)),
1837 struct problem_context
*pctx
)
1839 ext2_filsys fs
= ctx
->fs
;
1842 struct ext2_inode inode
;
1844 if (fix_problem(ctx
, PR_2_DIRECTORY_HOLE
, pctx
) == 0)
1848 * Read the inode and block bitmaps in; we'll be messing with
1851 e2fsck_read_bitmaps(ctx
);
1854 * First, find a free block
1856 e2fsck_read_inode(ctx
, db
->ino
, &inode
, "allocate_dir_block");
1857 pctx
->errcode
= ext2fs_map_cluster_block(fs
, db
->ino
, &inode
,
1858 db
->blockcnt
, &blk
);
1859 if (pctx
->errcode
|| blk
== 0) {
1860 blk
= ext2fs_find_inode_goal(fs
, db
->ino
, &inode
, db
->blockcnt
);
1861 pctx
->errcode
= ext2fs_new_block2(fs
, blk
,
1862 ctx
->block_found_map
, &blk
);
1863 if (pctx
->errcode
) {
1864 pctx
->str
= "ext2fs_new_block";
1865 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);
1869 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, blk
);
1870 ext2fs_mark_block_bitmap2(fs
->block_map
, blk
);
1871 ext2fs_mark_bb_dirty(fs
);
1874 * Now let's create the actual data block for the inode
1877 pctx
->errcode
= ext2fs_new_dir_block(fs
, 0, 0, &block
);
1879 pctx
->errcode
= ext2fs_new_dir_block(fs
, db
->ino
,
1880 EXT2_ROOT_INO
, &block
);
1882 if (pctx
->errcode
) {
1883 pctx
->str
= "ext2fs_new_dir_block";
1884 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);
1888 pctx
->errcode
= ext2fs_write_dir_block4(fs
, blk
, block
, 0, db
->ino
);
1889 ext2fs_free_mem(&block
);
1890 if (pctx
->errcode
) {
1891 pctx
->str
= "ext2fs_write_dir_block";
1892 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);
1897 * Update the inode block count
1899 ext2fs_iblk_add_blocks(fs
, &inode
, 1);
1900 if (EXT2_I_SIZE(&inode
) < ((__u64
) db
->blockcnt
+1) * fs
->blocksize
) {
1901 pctx
->errcode
= ext2fs_inode_size_set(fs
, &inode
,
1902 (db
->blockcnt
+1) * fs
->blocksize
);
1903 if (pctx
->errcode
) {
1904 pctx
->str
= "ext2fs_inode_size_set";
1905 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);
1909 e2fsck_write_inode(ctx
, db
->ino
, &inode
, "allocate_dir_block");
1912 * Finally, update the block pointers for the inode
1915 pctx
->errcode
= ext2fs_bmap2(fs
, db
->ino
, &inode
, 0, BMAP_SET
,
1916 db
->blockcnt
, 0, &blk
);
1917 if (pctx
->errcode
) {
1918 pctx
->str
= "ext2fs_block_iterate";
1919 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);