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() */
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;
100 unsigned int save_type;
105 int (*check_dir_func)(ext2_filsys fs,
106 struct ext2_db_entry2 *dir_blocks_info,
109 init_resource_track(&rtrack, ctx->fs->io);
110 clear_problem_context(&cd.pctx);
113 mtrace_print("Pass 2");
116 if (!(ctx->options & E2F_OPT_PREEN))
117 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
119 e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT,
121 if (ctx->inode_count)
124 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
125 "inode_count", &save_type);
126 cd.pctx.errcode = ext2fs_create_icount2(fs,
127 EXT2_ICOUNT_OPT_INCREMENT,
128 0, ctx->inode_link_info,
130 fs->default_bitmap_type = save_type;
132 if (cd.pctx.errcode) {
133 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
134 ctx->flags |= E2F_FLAG_ABORT;
137 buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
138 "directory scan buffer");
141 * Set up the parent pointer for the root directory, if
142 * present. (If the root directory is not present, we will
143 * create it in pass 3.)
145 (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
150 cd.max = ext2fs_dblist_count2(fs->dblist);
152 cd.ra_entries = ctx->readahead_kb * 1024 / ctx->fs->blocksize;
156 (void) (ctx->progress)(ctx, 2, 0, cd.max);
158 if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX)
159 ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp);
161 check_dir_func = cd.ra_entries ? check_dir_block2 : check_dir_block;
162 cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_func,
164 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
167 if (ctx->flags & E2F_FLAG_RESTART_LATER) {
168 ctx->flags |= E2F_FLAG_RESTART;
172 if (cd.pctx.errcode) {
173 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
174 ctx->flags |= E2F_FLAG_ABORT;
179 for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
180 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
182 if (e2fsck_dir_will_be_rehashed(ctx, dx_dir->ino) ||
183 dx_dir->numblocks == 0)
185 clear_problem_context(&pctx);
187 pctx.dir = dx_dir->ino;
188 dx_db = dx_dir->dx_block;
189 if (dx_db->flags & DX_FLAG_REFERENCED)
190 dx_db->flags |= DX_FLAG_DUP_REF;
192 dx_db->flags |= DX_FLAG_REFERENCED;
194 * Find all of the first and last leaf blocks, and
195 * update their parent's min and max hash values
197 for (b=0, dx_db = dx_dir->dx_block;
198 b < dx_dir->numblocks;
200 if ((dx_db->type != DX_DIRBLOCK_LEAF) ||
201 !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST)))
203 dx_parent = &dx_dir->dx_block[dx_db->parent];
205 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
207 if (dx_db->flags & DX_FLAG_FIRST)
208 dx_parent->min_hash = dx_db->min_hash;
210 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
212 if (dx_db->flags & DX_FLAG_LAST)
213 dx_parent->max_hash = dx_db->max_hash;
216 for (b=0, dx_db = dx_dir->dx_block;
217 b < dx_dir->numblocks;
220 pctx.group = dx_db->parent;
222 if (!(dx_db->flags & DX_FLAG_FIRST) &&
223 (dx_db->min_hash < dx_db->node_min_hash)) {
224 pctx.blk = dx_db->min_hash;
225 pctx.blk2 = dx_db->node_min_hash;
226 code = PR_2_HTREE_MIN_HASH;
227 fix_problem(ctx, code, &pctx);
230 if (dx_db->type == DX_DIRBLOCK_LEAF) {
231 depth = htree_depth(dx_dir, dx_db);
232 if (depth != dx_dir->depth) {
233 pctx.num = dx_dir->depth;
234 code = PR_2_HTREE_BAD_DEPTH;
235 fix_problem(ctx, code, &pctx);
240 * This test doesn't apply for the root block
244 (dx_db->max_hash > dx_db->node_max_hash)) {
245 pctx.blk = dx_db->max_hash;
246 pctx.blk2 = dx_db->node_max_hash;
247 code = PR_2_HTREE_MAX_HASH;
248 fix_problem(ctx, code, &pctx);
251 if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
252 code = PR_2_HTREE_NOTREF;
253 fix_problem(ctx, code, &pctx);
255 } else if (dx_db->flags & DX_FLAG_DUP_REF) {
256 code = PR_2_HTREE_DUPREF;
257 fix_problem(ctx, code, &pctx);
261 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
262 clear_htree(ctx, dx_dir->ino);
263 dx_dir->numblocks = 0;
266 e2fsck_free_dx_dir_info(ctx);
268 ext2fs_free_mem(&buf);
269 ext2fs_free_dblist(fs->dblist);
271 if (ctx->inode_bad_map) {
272 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
273 ctx->inode_bad_map = 0;
275 if (ctx->inode_reg_map) {
276 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
277 ctx->inode_reg_map = 0;
279 if (ctx->encrypted_dirs) {
280 ext2fs_u32_list_free(ctx->encrypted_dirs);
281 ctx->encrypted_dirs = 0;
284 clear_problem_context(&pctx);
285 if (ctx->large_files) {
286 if (!(sb->s_feature_ro_compat &
287 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
288 fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
289 sb->s_feature_ro_compat |=
290 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
291 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
292 ext2fs_mark_super_dirty(fs);
294 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
295 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
296 ext2fs_update_dynamic_rev(fs);
297 ext2fs_mark_super_dirty(fs);
301 print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io);
304 #define MAX_DEPTH 32000
305 static int htree_depth(struct dx_dir_info *dx_dir,
306 struct dx_dirblock_info *dx_db)
310 while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
311 dx_db = &dx_dir->dx_block[dx_db->parent];
317 static int dict_de_cmp(const void *a, const void *b)
319 const struct ext2_dir_entry *de_a, *de_b;
322 de_a = (const struct ext2_dir_entry *) a;
323 a_len = ext2fs_dirent_name_len(de_a);
324 de_b = (const struct ext2_dir_entry *) b;
325 b_len = ext2fs_dirent_name_len(de_b);
328 return (a_len - b_len);
330 return memcmp(de_a->name, de_b->name, a_len);
334 * This is special sort function that makes sure that directory blocks
335 * with a dirblock of zero are sorted to the beginning of the list.
336 * This guarantees that the root node of the htree directories are
337 * processed first, so we know what hash version to use.
339 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
341 const struct ext2_db_entry2 *db_a =
342 (const struct ext2_db_entry2 *) a;
343 const struct ext2_db_entry2 *db_b =
344 (const struct ext2_db_entry2 *) b;
346 if (db_a->blockcnt && !db_b->blockcnt)
349 if (!db_a->blockcnt && db_b->blockcnt)
352 if (db_a->blk != db_b->blk)
353 return (int) (db_a->blk - db_b->blk);
355 if (db_a->ino != db_b->ino)
356 return (int) (db_a->ino - db_b->ino);
358 return (int) (db_a->blockcnt - db_b->blockcnt);
363 * Make sure the first entry in the directory is '.', and that the
364 * directory entry is sane.
366 static int check_dot(e2fsck_t ctx,
367 struct ext2_dir_entry *dirent,
368 ext2_ino_t ino, struct problem_context *pctx)
370 struct ext2_dir_entry *nextdir;
371 unsigned int rec_len, new_len;
374 problem_t problem = 0;
377 problem = PR_2_MISSING_DOT;
378 else if ((ext2fs_dirent_name_len(dirent) != 1) ||
379 (dirent->name[0] != '.'))
380 problem = PR_2_1ST_NOT_DOT;
381 else if (dirent->name[1] != '\0')
382 problem = PR_2_DOT_NULL_TERM;
384 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
386 if (fix_problem(ctx, problem, pctx)) {
388 rec_len = dirent->rec_len = 12;
390 ext2fs_dirent_set_name_len(dirent, 1);
391 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
392 dirent->name[0] = '.';
393 dirent->name[1] = '\0';
398 if (dirent->inode != ino) {
399 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
405 new_len = rec_len - 12;
408 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
409 nextdir = (struct ext2_dir_entry *)
410 ((char *) dirent + 12);
411 dirent->rec_len = 12;
412 (void) ext2fs_set_rec_len(ctx->fs, new_len,
415 ext2fs_dirent_set_name_len(nextdir, 0);
416 ext2fs_dirent_set_file_type(nextdir,
426 * Make sure the second entry in the directory is '..', and that the
427 * directory entry is sane. We do not check the inode number of '..'
428 * here; this gets done in pass 3.
430 static int check_dotdot(e2fsck_t ctx,
431 struct ext2_dir_entry *dirent,
432 ext2_ino_t ino, struct problem_context *pctx)
434 problem_t problem = 0;
435 unsigned int rec_len;
438 problem = PR_2_MISSING_DOT_DOT;
439 else if ((ext2fs_dirent_name_len(dirent) != 2) ||
440 (dirent->name[0] != '.') ||
441 (dirent->name[1] != '.'))
442 problem = PR_2_2ND_NOT_DOT_DOT;
443 else if (dirent->name[2] != '\0')
444 problem = PR_2_DOT_DOT_NULL_TERM;
446 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
448 if (fix_problem(ctx, problem, pctx)) {
450 dirent->rec_len = 12;
452 * Note: we don't have the parent inode just
453 * yet, so we will fill it in with the root
454 * inode. This will get fixed in pass 3.
456 dirent->inode = EXT2_ROOT_INO;
457 ext2fs_dirent_set_name_len(dirent, 2);
458 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
459 dirent->name[0] = '.';
460 dirent->name[1] = '.';
461 dirent->name[2] = '\0';
466 if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) {
467 fix_problem(ctx, PR_2_NO_DIRINFO, pctx);
474 * Check to make sure a directory entry doesn't contain any illegal
477 static int check_name(e2fsck_t ctx,
478 struct ext2_dir_entry *dirent,
480 struct problem_context *pctx)
486 for ( i = 0; i < ext2fs_dirent_name_len(dirent); i++) {
487 if (dirent->name[i] != '/' && dirent->name[i] != '\0')
490 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
493 dirent->name[i] = '.';
500 * Check the directory filetype (if present)
502 static _INLINE_ int check_filetype(e2fsck_t ctx,
503 struct ext2_dir_entry *dirent,
504 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
505 struct problem_context *pctx)
507 int filetype = ext2fs_dirent_file_type(dirent);
508 int should_be = EXT2_FT_UNKNOWN;
509 struct ext2_inode inode;
511 if (!(ctx->fs->super->s_feature_incompat &
512 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
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);
547 static void parse_int_node(ext2_filsys fs,
548 struct ext2_db_entry2 *db,
549 struct check_dir_struct *cd,
550 struct dx_dir_info *dx_dir,
551 char *block_buf, int failed_csum)
553 struct ext2_dx_root_info *root;
554 struct ext2_dx_entry *ent;
555 struct ext2_dx_countlimit *limit;
556 struct dx_dirblock_info *dx_db;
557 int i, expect_limit, count;
559 ext2_dirhash_t min_hash = 0xffffffff;
560 ext2_dirhash_t max_hash = 0;
561 ext2_dirhash_t hash = 0, prev_hash;
564 if (db->blockcnt == 0) {
565 root = (struct ext2_dx_root_info *) (block_buf + 24);
568 printf("Root node dump:\n");
569 printf("\t Reserved zero: %u\n", root->reserved_zero);
570 printf("\t Hash Version: %d\n", root->hash_version);
571 printf("\t Info length: %d\n", root->info_length);
572 printf("\t Indirect levels: %d\n", root->indirect_levels);
573 printf("\t Flags: %d\n", root->unused_flags);
576 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
579 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
580 fix_problem(cd->ctx, PR_2_HTREE_ROOT_CSUM_INVALID,
584 ent = (struct ext2_dx_entry *) (block_buf+8);
587 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
588 fix_problem(cd->ctx, PR_2_HTREE_NODE_CSUM_INVALID,
593 limit = (struct ext2_dx_countlimit *) ent;
596 printf("Number of entries (count): %d\n",
597 ext2fs_le16_to_cpu(limit->count));
598 printf("Number of entries (limit): %d\n",
599 ext2fs_le16_to_cpu(limit->limit));
602 count = ext2fs_le16_to_cpu(limit->count);
603 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
604 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
605 csum_size = sizeof(struct ext2_dx_tail);
606 expect_limit = (fs->blocksize -
607 (csum_size + ((char *) ent - block_buf))) /
608 sizeof(struct ext2_dx_entry);
609 if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
610 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
611 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
614 if (count > expect_limit) {
615 cd->pctx.num = count;
616 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
618 count = expect_limit;
621 for (i=0; i < count; i++) {
623 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
625 printf("Entry #%d: Hash 0x%08x, block %u\n", i,
626 hash, ext2fs_le32_to_cpu(ent[i].block));
628 blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff;
629 /* Check to make sure the block is valid */
630 if (blk >= (blk_t) dx_dir->numblocks) {
632 if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
637 if (hash < prev_hash &&
638 fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
640 dx_db = &dx_dir->dx_block[blk];
641 if (dx_db->flags & DX_FLAG_REFERENCED) {
642 dx_db->flags |= DX_FLAG_DUP_REF;
644 dx_db->flags |= DX_FLAG_REFERENCED;
645 dx_db->parent = db->blockcnt;
651 dx_db->node_min_hash = hash;
653 dx_db->node_max_hash =
654 ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
656 dx_db->node_max_hash = 0xfffffffe;
657 dx_db->flags |= DX_FLAG_LAST;
660 dx_db->flags |= DX_FLAG_FIRST;
663 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
664 db->blockcnt, min_hash, max_hash);
666 dx_db = &dx_dir->dx_block[db->blockcnt];
667 dx_db->min_hash = min_hash;
668 dx_db->max_hash = max_hash;
672 clear_htree(cd->ctx, cd->pctx.ino);
673 dx_dir->numblocks = 0;
674 e2fsck_rehash_dir_later(cd->ctx, cd->pctx.ino);
676 #endif /* ENABLE_HTREE */
679 * Given a busted directory, try to salvage it somehow.
682 static void salvage_directory(ext2_filsys fs,
683 struct ext2_dir_entry *dirent,
684 struct ext2_dir_entry *prev,
685 unsigned int *offset,
686 unsigned int block_len)
688 char *cp = (char *) dirent;
690 unsigned int rec_len, prev_rec_len;
691 unsigned int name_len;
694 * If the space left for the entry is too small to be an entry,
695 * we can't access dirent's fields, so plumb in the values needed
696 * so that the previous entry absorbs this one.
698 if (block_len - *offset < EXT2_DIR_ENTRY_HEADER_LEN) {
700 rec_len = block_len - *offset;
702 name_len = ext2fs_dirent_name_len(dirent);
703 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
705 left = block_len - *offset - rec_len;
708 * Special case of directory entry of size 8: copy what's left
709 * of the directory block up to cover up the invalid hole.
711 if ((left >= 12) && (rec_len == EXT2_DIR_ENTRY_HEADER_LEN)) {
712 memmove(cp, cp+EXT2_DIR_ENTRY_HEADER_LEN, left);
713 memset(cp + left, 0, EXT2_DIR_ENTRY_HEADER_LEN);
717 * If the directory entry overruns the end of the directory
718 * block, and the name is small enough to fit, then adjust the
722 ((int) rec_len + left > EXT2_DIR_ENTRY_HEADER_LEN) &&
723 ((int) name_len + EXT2_DIR_ENTRY_HEADER_LEN <= (int) rec_len + left) &&
724 dirent->inode <= fs->super->s_inodes_count &&
725 strnlen(dirent->name, name_len) == name_len) {
726 (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent);
730 * If the record length of the directory entry is a multiple
731 * of four, and not too big, such that it is valid, let the
732 * previous directory entry absorb the invalid one.
734 if (prev && rec_len && (rec_len % 4) == 0 &&
735 (*offset + rec_len <= block_len)) {
736 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
737 prev_rec_len += rec_len;
738 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
743 * Default salvage method --- kill all of the directory
744 * entries for the rest of the block. We will either try to
745 * absorb it into the previous directory entry, or create a
746 * new empty directory entry the rest of the directory block.
749 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
750 prev_rec_len += block_len - *offset;
751 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
752 *offset = fs->blocksize;
754 rec_len = block_len - *offset;
755 (void) ext2fs_set_rec_len(fs, rec_len, dirent);
756 ext2fs_dirent_set_name_len(dirent, 0);
757 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
762 #define NEXT_DIRENT(d) ((void *)((char *)(d) + (d)->rec_len))
763 static errcode_t insert_dirent_tail(ext2_filsys fs, void *dirbuf)
765 struct ext2_dir_entry *d;
767 struct ext2_dir_entry_tail *t;
770 top = EXT2_DIRENT_TAIL(dirbuf, fs->blocksize);
772 while (d->rec_len && !(d->rec_len & 0x3) && NEXT_DIRENT(d) <= top)
776 size_t min_size = EXT2_DIR_REC_LEN(
777 ext2fs_dirent_name_len(dirbuf));
778 if (min_size > top - (void *)d)
779 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
780 d->rec_len = top - (void *)d;
783 t = (struct ext2_dir_entry_tail *)top;
784 if (t->det_reserved_zero1 ||
785 t->det_rec_len != sizeof(struct ext2_dir_entry_tail) ||
786 t->det_reserved_name_len != EXT2_DIR_NAME_LEN_CSUM)
787 ext2fs_initialize_dirent_tail(fs, t);
793 static errcode_t fix_inline_dir_size(e2fsck_t ctx, ext2_ino_t ino,
794 size_t *inline_data_size,
795 struct problem_context *pctx,
798 ext2_filsys fs = ctx->fs;
799 struct ext2_inode inode;
800 size_t new_size, old_size;
803 old_size = *inline_data_size;
805 * If there's not enough bytes to start the "second" dir block
806 * (in the EA space) then truncate everything to the first block.
808 if (old_size > EXT4_MIN_INLINE_DATA_SIZE &&
809 old_size < EXT4_MIN_INLINE_DATA_SIZE +
810 EXT2_DIR_REC_LEN(1)) {
811 old_size = EXT4_MIN_INLINE_DATA_SIZE;
814 /* Increase to the next four-byte boundary for salvaging */
815 new_size = old_size + (4 - (old_size & 3));
816 memset(buf + old_size, 0, new_size - old_size);
817 retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size);
818 if (retval == EXT2_ET_INLINE_DATA_NO_SPACE) {
819 /* Or we can't, so truncate. */
821 retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size);
823 if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED,
831 if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED,
840 retval = ext2fs_read_inode(fs, ino, &inode);
844 retval = ext2fs_inode_size_set(fs, &inode, new_size);
848 inode.i_flags &= ~EXT4_INLINE_DATA_FL;
849 retval = ext2fs_write_inode(fs, ino, &inode);
852 *inline_data_size = new_size;
858 int get_filename_hash(ext2_filsys fs, int encrypted, int version,
859 const char *name, int len, ext2_dirhash_t *ret_hash,
860 ext2_dirhash_t *ret_minor_hash)
862 char buf[2*EXT2FS_DIGEST_SIZE];
866 return ext2fs_dirhash(version, name, len,
867 fs->super->s_hash_seed,
868 ret_hash, ret_minor_hash);
870 if (len <= EXT2FS_DIGEST_SIZE)
871 buf_len = ext2fs_digest_encode(name, len, buf);
873 ext2fs_sha256(name, len, buf + EXT2FS_DIGEST_SIZE);
875 buf_len = ext2fs_digest_encode(buf + EXT2FS_DIGEST_SIZE,
876 EXT2FS_DIGEST_SIZE, buf + 1);
879 return ext2fs_dirhash(version, buf, buf_len,
880 fs->super->s_hash_seed,
881 ret_hash, ret_minor_hash);
884 static int check_dir_block2(ext2_filsys fs,
885 struct ext2_db_entry2 *db,
889 struct check_dir_struct *cd = priv_data;
891 if (cd->ra_entries && cd->list_offset >= cd->next_ra_off) {
892 err = e2fsck_readahead_dblist(fs,
893 E2FSCK_RA_DBLIST_IGNORE_BLOCKCNT,
895 cd->list_offset + cd->ra_entries / 8,
899 cd->next_ra_off = cd->list_offset + (cd->ra_entries * 7 / 8);
902 err = check_dir_block(fs, db, priv_data);
907 static int check_dir_block(ext2_filsys fs,
908 struct ext2_db_entry2 *db,
911 struct dx_dir_info *dx_dir;
913 struct dx_dirblock_info *dx_db = 0;
914 #endif /* ENABLE_HTREE */
915 struct ext2_dir_entry *dirent, *prev, dot, dotdot;
917 unsigned int offset = 0;
918 int dir_modified = 0;
920 unsigned int rec_len;
921 blk64_t block_nr = db->blk;
922 ext2_ino_t ino = db->ino;
923 ext2_ino_t subdir_parent;
925 struct check_dir_struct *cd;
929 struct ext2_dx_root_info *root;
930 struct ext2_dx_countlimit *limit;
931 static dict_t de_dict;
932 struct problem_context pctx;
935 int dx_csum_size = 0, de_csum_size = 0;
938 size_t inline_data_size = 0;
941 size_t max_block_size;
943 cd = (struct check_dir_struct *) priv_data;
944 ibuf = buf = cd->buf;
947 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
950 if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
953 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
954 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
955 dx_csum_size = sizeof(struct ext2_dx_tail);
956 de_csum_size = sizeof(struct ext2_dir_entry_tail);
959 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
960 EXT2_FEATURE_INCOMPAT_FILETYPE))
961 filetype = EXT2_FT_DIR << 8;
964 * Make sure the inode is still in use (could have been
965 * deleted in the duplicate/bad blocks pass.
967 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino)))
971 cd->pctx.blk = block_nr;
972 cd->pctx.blkcount = db->blockcnt;
977 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
978 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
981 ec = ext2fs_inline_data_size(fs, ino, &inline_data_size);
982 if (ec && ec != EXT2_ET_NO_INLINE_DATA)
986 if (db->blk == 0 && !inline_data_size) {
987 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
997 if (ctx->dirs_to_hash &&
998 ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
1002 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
1006 ehandler_operation(_("reading directory block"));
1007 if (inline_data_size) {
1008 memset(buf, 0, fs->blocksize - inline_data_size);
1009 cd->pctx.errcode = ext2fs_inline_data_get(fs, ino, 0, buf, 0);
1010 if (cd->pctx.errcode)
1011 goto inline_read_fail;
1012 #ifdef WORDS_BIGENDIAN
1014 goto skip_first_read_swab;
1015 *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf));
1016 cd->pctx.errcode = ext2fs_dirent_swab_in2(fs,
1017 buf + EXT4_INLINE_DATA_DOTDOT_SIZE,
1018 EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DATA_DOTDOT_SIZE,
1020 if (cd->pctx.errcode)
1021 goto inline_read_fail;
1022 skip_first_read_swab:
1023 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE ||
1025 goto inline_read_fail;
1026 cd->pctx.errcode = ext2fs_dirent_swab_in2(fs,
1027 buf + EXT4_MIN_INLINE_DATA_SIZE,
1028 inline_data_size - EXT4_MIN_INLINE_DATA_SIZE,
1032 cd->pctx.errcode = ext2fs_read_dir_block4(fs, block_nr,
1036 pctx.num = inline_data_size;
1037 if (((inline_data_size & 3) ||
1038 (inline_data_size > EXT4_MIN_INLINE_DATA_SIZE &&
1039 inline_data_size < EXT4_MIN_INLINE_DATA_SIZE +
1040 EXT2_DIR_REC_LEN(1))) &&
1041 fix_problem(ctx, PR_2_BAD_INLINE_DIR_SIZE, &pctx)) {
1042 errcode_t err = fix_inline_dir_size(ctx, ino,
1043 &inline_data_size, &pctx,
1046 return DIRENT_ABORT;
1049 ehandler_operation(0);
1050 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
1051 cd->pctx.errcode = 0; /* We'll handle this ourselves */
1052 else if (cd->pctx.errcode == EXT2_ET_DIR_CSUM_INVALID) {
1053 cd->pctx.errcode = 0; /* We'll handle this ourselves */
1056 if (cd->pctx.errcode) {
1058 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
1059 ctx->flags |= E2F_FLAG_ABORT;
1060 return DIRENT_ABORT;
1062 ext2fs_new_dir_block(fs, db->blockcnt == 0 ? ino : 0,
1063 EXT2_ROOT_INO, &buf2);
1064 memcpy(buf, buf2, fs->blocksize);
1065 ext2fs_free_mem(&buf2);
1068 dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
1069 if (dx_dir && dx_dir->numblocks) {
1070 if (db->blockcnt >= dx_dir->numblocks) {
1072 if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK,
1074 clear_htree(ctx, ino);
1075 dx_dir->numblocks = 0;
1079 fatal_error(ctx, _("Can not continue."));
1081 dx_db = &dx_dir->dx_block[db->blockcnt];
1082 dx_db->type = DX_DIRBLOCK_LEAF;
1083 dx_db->phys = block_nr;
1084 dx_db->min_hash = ~0;
1085 dx_db->max_hash = 0;
1087 dirent = (struct ext2_dir_entry *) buf;
1088 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1089 limit = (struct ext2_dx_countlimit *) (buf+8);
1090 if (db->blockcnt == 0) {
1091 root = (struct ext2_dx_root_info *) (buf + 24);
1092 dx_db->type = DX_DIRBLOCK_ROOT;
1093 dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
1094 if ((root->reserved_zero ||
1095 root->info_length < 8 ||
1096 root->indirect_levels > 1) &&
1097 fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
1098 clear_htree(ctx, ino);
1099 dx_dir->numblocks = 0;
1102 dx_dir->hashversion = root->hash_version;
1103 if ((dx_dir->hashversion <= EXT2_HASH_TEA) &&
1104 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
1105 dx_dir->hashversion += 3;
1106 dx_dir->depth = root->indirect_levels + 1;
1107 } else if ((dirent->inode == 0) &&
1108 (rec_len == fs->blocksize) &&
1109 (ext2fs_dirent_name_len(dirent) == 0) &&
1110 (ext2fs_le16_to_cpu(limit->limit) ==
1111 ((fs->blocksize - (8 + dx_csum_size)) /
1112 sizeof(struct ext2_dx_entry))))
1113 dx_db->type = DX_DIRBLOCK_NODE;
1117 #endif /* ENABLE_HTREE */
1119 /* Leaf node with no space for csum? Rebuild dirs in pass 3A. */
1120 if (is_leaf && !inline_data_size && failed_csum &&
1121 !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
1123 if (e2fsck_dir_will_be_rehashed(ctx, ino)) {
1127 if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_MISSING_CSUM,
1130 e2fsck_rehash_dir_later(ctx, ino);
1134 /* htree nodes don't use fake dirents to store checksums */
1139 if (inline_data_size) {
1141 buf += EXT4_MIN_INLINE_DATA_SIZE;
1142 max_block_size = inline_data_size - EXT4_MIN_INLINE_DATA_SIZE;
1143 /* Zero-length second block, just exit */
1144 if (max_block_size == 0)
1147 max_block_size = EXT4_MIN_INLINE_DATA_SIZE;
1150 max_block_size = fs->blocksize - de_csum_size;
1152 if (ctx->encrypted_dirs)
1153 encrypted = ext2fs_u32_list_test(ctx->encrypted_dirs, ino);
1155 dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
1159 ext2_ino_t first_unused_inode;
1160 unsigned int name_len;
1163 if (!inline_data_size || dot_state > 1) {
1164 dirent = (struct ext2_dir_entry *) (buf + offset);
1166 * If there's not even space for the entry header,
1167 * force salvaging this dir.
1169 if (max_block_size - offset < EXT2_DIR_ENTRY_HEADER_LEN)
1170 rec_len = EXT2_DIR_REC_LEN(1);
1172 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1173 cd->pctx.dirent = dirent;
1174 cd->pctx.num = offset;
1175 if ((offset + rec_len > max_block_size) ||
1177 ((rec_len % 4) != 0) ||
1178 ((ext2fs_dirent_name_len(dirent) + EXT2_DIR_ENTRY_HEADER_LEN) > rec_len)) {
1179 if (fix_problem(ctx, PR_2_DIR_CORRUPTED,
1181 #ifdef WORDS_BIGENDIAN
1183 * On big-endian systems, if the dirent
1184 * swap routine finds a rec_len that it
1185 * doesn't like, it continues
1186 * processing the block as if rec_len
1187 * == EXT2_DIR_ENTRY_HEADER_LEN. This means that the name
1188 * field gets byte swapped, which means
1189 * that salvage will not detect the
1190 * correct name length (unless the name
1191 * has a length that's an exact
1192 * multiple of four bytes), and it'll
1193 * discard the entry (unnecessarily)
1194 * and the rest of the dirent block.
1195 * Therefore, swap the rest of the
1196 * block back to disk order, run
1197 * salvage, and re-swap anything after
1198 * the salvaged dirent.
1200 int need_reswab = 0;
1201 if (rec_len < EXT2_DIR_ENTRY_HEADER_LEN || rec_len % 4) {
1203 ext2fs_dirent_swab_in2(fs,
1204 ((char *)dirent) + EXT2_DIR_ENTRY_HEADER_LEN,
1205 max_block_size - offset - EXT2_DIR_ENTRY_HEADER_LEN,
1209 salvage_directory(fs, dirent, prev,
1212 #ifdef WORDS_BIGENDIAN
1214 (void) ext2fs_get_rec_len(fs,
1216 ext2fs_dirent_swab_in2(fs,
1217 ((char *)dirent) + offset + rec_len,
1218 max_block_size - offset - rec_len,
1225 goto abort_free_dict;
1228 if (dot_state == 0) {
1229 memset(&dot, 0, sizeof(dot));
1231 dirent->inode = ino;
1232 dirent->rec_len = EXT2_DIR_REC_LEN(1);
1233 dirent->name_len = 1 | filetype;
1234 dirent->name[0] = '.';
1235 } else if (dot_state == 1) {
1236 memset(&dotdot, 0, sizeof(dotdot));
1239 ((struct ext2_dir_entry *)buf)->inode;
1240 dirent->rec_len = EXT2_DIR_REC_LEN(2);
1241 dirent->name_len = 2 | filetype;
1242 dirent->name[0] = '.';
1243 dirent->name[1] = '.';
1245 fatal_error(ctx, _("Can not continue."));
1247 cd->pctx.dirent = dirent;
1248 cd->pctx.num = offset;
1251 if (dot_state == 0) {
1252 if (check_dot(ctx, dirent, ino, &cd->pctx))
1254 } else if (dot_state == 1) {
1255 ret = check_dotdot(ctx, dirent, ino, &cd->pctx);
1257 goto abort_free_dict;
1260 } else if (dirent->inode == ino) {
1261 problem = PR_2_LINK_DOT;
1262 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
1272 * Make sure the inode listed is a legal one.
1274 name_len = ext2fs_dirent_name_len(dirent);
1275 if (((dirent->inode != EXT2_ROOT_INO) &&
1276 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
1277 (dirent->inode > fs->super->s_inodes_count)) {
1278 problem = PR_2_BAD_INO;
1279 } else if (ctx->inode_bb_map &&
1280 (ext2fs_test_inode_bitmap2(ctx->inode_bb_map,
1283 * If the inode is in a bad block, offer to
1286 problem = PR_2_BB_INODE;
1287 } else if ((dot_state > 1) && (name_len == 1) &&
1288 (dirent->name[0] == '.')) {
1290 * If there's a '.' entry in anything other
1291 * than the first directory entry, it's a
1292 * duplicate entry that should be removed.
1294 problem = PR_2_DUP_DOT;
1295 } else if ((dot_state > 1) && (name_len == 2) &&
1296 (dirent->name[0] == '.') &&
1297 (dirent->name[1] == '.')) {
1299 * If there's a '..' entry in anything other
1300 * than the second directory entry, it's a
1301 * duplicate entry that should be removed.
1303 problem = PR_2_DUP_DOT_DOT;
1304 } else if ((dot_state > 1) &&
1305 (dirent->inode == EXT2_ROOT_INO)) {
1307 * Don't allow links to the root directory.
1308 * We check this specially to make sure we
1309 * catch this error case even if the root
1310 * directory hasn't been created yet.
1312 problem = PR_2_LINK_ROOT;
1313 } else if ((dot_state > 1) && (name_len == 0)) {
1315 * Don't allow zero-length directory names.
1317 problem = PR_2_NULL_NAME;
1321 if (fix_problem(ctx, problem, &cd->pctx)) {
1326 ext2fs_unmark_valid(fs);
1327 if (problem == PR_2_BAD_INO)
1333 * If the inode was marked as having bad fields in
1334 * pass1, process it and offer to fix/clear it.
1335 * (We wait until now so that we can display the
1336 * pathname to the user.)
1338 if (ctx->inode_bad_map &&
1339 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
1341 if (e2fsck_process_bad_inode(ctx, ino,
1343 buf + fs->blocksize)) {
1348 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1349 return DIRENT_ABORT;
1352 group = ext2fs_group_of_ino(fs, dirent->inode);
1353 first_unused_inode = group * fs->super->s_inodes_per_group +
1354 1 + fs->super->s_inodes_per_group -
1355 ext2fs_bg_itable_unused(fs, group);
1356 cd->pctx.group = group;
1359 * Check if the inode was missed out because
1360 * _INODE_UNINIT flag was set or bg_itable_unused was
1361 * incorrect. If so, clear the _INODE_UNINIT flag and
1362 * restart e2fsck. In the future it would be nice if
1363 * we could call a function in pass1.c that checks the
1364 * newly visible inodes.
1366 if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) {
1367 pctx.num = dirent->inode;
1368 if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT,
1370 ext2fs_bg_flags_clear(fs, group,
1371 EXT2_BG_INODE_UNINIT);
1372 ext2fs_mark_super_dirty(fs);
1373 ctx->flags |= E2F_FLAG_RESTART_LATER;
1375 ext2fs_unmark_valid(fs);
1376 if (problem == PR_2_BAD_INO)
1379 } else if (dirent->inode >= first_unused_inode) {
1380 pctx.num = dirent->inode;
1381 if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){
1382 ext2fs_bg_itable_unused_set(fs, group, 0);
1383 ext2fs_mark_super_dirty(fs);
1384 ctx->flags |= E2F_FLAG_RESTART_LATER;
1386 ext2fs_unmark_valid(fs);
1387 if (problem == PR_2_BAD_INO)
1393 * Offer to clear unused inodes; if we are going to be
1394 * restarting the scan due to bg_itable_unused being
1395 * wrong, then don't clear any inodes to avoid zapping
1396 * inodes that were skipped during pass1 due to an
1397 * incorrect bg_itable_unused; we'll get any real
1398 * problems after we restart.
1400 if (!(ctx->flags & E2F_FLAG_RESTART_LATER) &&
1401 !(ext2fs_test_inode_bitmap2(ctx->inode_used_map,
1403 problem = PR_2_UNUSED_INODE;
1406 if (fix_problem(ctx, problem, &cd->pctx)) {
1411 ext2fs_unmark_valid(fs);
1412 if (problem == PR_2_BAD_INO)
1417 if (!encrypted && check_name(ctx, dirent, ino, &cd->pctx))
1420 if (check_filetype(ctx, dirent, ino, &cd->pctx))
1425 get_filename_hash(fs, encrypted, dx_dir->hashversion,
1427 ext2fs_dirent_name_len(dirent),
1429 if (hash < dx_db->min_hash)
1430 dx_db->min_hash = hash;
1431 if (hash > dx_db->max_hash)
1432 dx_db->max_hash = hash;
1437 * If this is a directory, then mark its parent in its
1438 * dir_info structure. If the parent field is already
1439 * filled in, then this directory has more than one
1440 * hard link. We assume the first link is correct,
1441 * and ask the user if he/she wants to clear this one.
1443 if ((dot_state > 1) &&
1444 (ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
1446 if (e2fsck_dir_info_get_parent(ctx, dirent->inode,
1448 cd->pctx.ino = dirent->inode;
1449 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
1450 goto abort_free_dict;
1452 if (subdir_parent) {
1453 cd->pctx.ino2 = subdir_parent;
1454 if (fix_problem(ctx, PR_2_LINK_DIR,
1462 (void) e2fsck_dir_info_set_parent(ctx,
1463 dirent->inode, ino);
1469 } else if (dict_lookup(&de_dict, dirent)) {
1470 clear_problem_context(&pctx);
1472 pctx.dirent = dirent;
1473 fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
1474 e2fsck_rehash_dir_later(ctx, ino);
1477 dict_alloc_insert(&de_dict, dirent, dirent);
1479 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1482 ctx->fs_links_count++;
1483 ctx->fs_total_count++;
1487 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1488 if (!inline_data_size || dot_state > 1) {
1491 if (dot_state == 1) {
1494 * If we get here, we're checking an inline
1495 * directory and we've just checked a (fake)
1496 * dotdot entry that we created on the stack.
1497 * Therefore set 'prev' to NULL so that if we
1498 * call salvage_directory on the next entry,
1499 * it won't try to absorb the next entry into
1500 * the on-stack dotdot entry.
1506 } while (offset < max_block_size);
1513 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1514 db->blockcnt, dx_db->type,
1515 dx_db->min_hash, dx_db->max_hash);
1517 cd->pctx.dir = cd->pctx.ino;
1518 if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1519 (dx_db->type == DX_DIRBLOCK_NODE))
1520 parse_int_node(fs, db, cd, dx_dir, buf, failed_csum);
1522 #endif /* ENABLE_HTREE */
1524 if (offset != max_block_size) {
1525 cd->pctx.num = rec_len + offset - max_block_size;
1526 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1527 dirent->rec_len = cd->pctx.num;
1532 int flags, will_rehash;
1533 /* leaf block with no tail? Rehash dirs later. */
1534 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
1535 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
1537 !inline_data_size &&
1538 !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
1539 if (insert_dirent_tail(fs, buf) == 0)
1541 e2fsck_rehash_dir_later(ctx, ino);
1545 will_rehash = e2fsck_dir_will_be_rehashed(ctx, ino);
1547 flags = ctx->fs->flags;
1548 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1550 if (inline_data_size) {
1552 #ifdef WORDS_BIGENDIAN
1554 goto skip_first_write_swab;
1555 *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf));
1556 cd->pctx.errcode = ext2fs_dirent_swab_out2(fs,
1557 buf + EXT4_INLINE_DATA_DOTDOT_SIZE,
1558 EXT4_MIN_INLINE_DATA_SIZE -
1559 EXT4_INLINE_DATA_DOTDOT_SIZE,
1561 if (cd->pctx.errcode)
1562 goto skip_second_write_swab;
1563 skip_first_write_swab:
1564 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE ||
1566 goto skip_second_write_swab;
1567 cd->pctx.errcode = ext2fs_dirent_swab_out2(fs,
1568 buf + EXT4_MIN_INLINE_DATA_SIZE,
1570 EXT4_MIN_INLINE_DATA_SIZE,
1572 skip_second_write_swab:
1573 if (cd->pctx.errcode &&
1574 !fix_problem(ctx, PR_2_WRITE_DIRBLOCK, &cd->pctx))
1575 goto abort_free_dict;
1578 ext2fs_inline_data_set(fs, ino, 0, buf,
1581 cd->pctx.errcode = ext2fs_write_dir_block4(fs, block_nr,
1584 ctx->fs->flags = (flags &
1585 EXT2_FLAG_IGNORE_CSUM_ERRORS) |
1587 ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
1588 if (cd->pctx.errcode) {
1589 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
1591 goto abort_free_dict;
1593 ext2fs_mark_changed(fs);
1594 } else if (is_leaf && failed_csum && !dir_modified) {
1596 * If a leaf node that fails csum makes it this far without
1597 * alteration, ask the user if the checksum should be fixed.
1599 if (fix_problem(ctx, PR_2_LEAF_NODE_ONLY_CSUM_INVALID,
1603 dict_free_nodes(&de_dict);
1606 ctx->flags |= E2F_FLAG_ABORT;
1607 dict_free_nodes(&de_dict);
1608 return DIRENT_ABORT;
1617 * This function is called to deallocate a block, and is an interator
1618 * functioned called by deallocate inode via ext2fs_iterate_block().
1620 static int deallocate_inode_block(ext2_filsys fs,
1622 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1623 blk64_t ref_block EXT2FS_ATTR((unused)),
1624 int ref_offset EXT2FS_ATTR((unused)),
1627 struct del_block *p = priv_data;
1631 if ((*block_nr < fs->super->s_first_data_block) ||
1632 (*block_nr >= ext2fs_blocks_count(fs->super)))
1634 if ((*block_nr % EXT2FS_CLUSTER_RATIO(fs)) == 0)
1635 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
1641 * This fuction deallocates an inode
1643 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
1645 ext2_filsys fs = ctx->fs;
1646 struct ext2_inode inode;
1647 struct problem_context pctx;
1649 struct del_block del_block;
1651 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1652 clear_problem_context(&pctx);
1656 * Fix up the bitmaps...
1658 e2fsck_read_bitmaps(ctx);
1659 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1661 if (ext2fs_file_acl_block(fs, &inode) &&
1662 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1663 pctx.errcode = ext2fs_adjust_ea_refcount3(fs,
1664 ext2fs_file_acl_block(fs, &inode),
1665 block_buf, -1, &count, ino);
1666 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1671 pctx.blk = ext2fs_file_acl_block(fs, &inode);
1672 fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1673 ctx->flags |= E2F_FLAG_ABORT;
1677 ext2fs_block_alloc_stats2(fs,
1678 ext2fs_file_acl_block(fs, &inode), -1);
1680 ext2fs_file_acl_block_set(fs, &inode, 0);
1683 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1686 /* Inline data inodes don't have blocks to iterate */
1687 if (inode.i_flags & EXT4_INLINE_DATA_FL)
1690 if (LINUX_S_ISREG(inode.i_mode) &&
1691 ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode)))
1694 del_block.ctx = ctx;
1696 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1697 deallocate_inode_block,
1700 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
1701 ctx->flags |= E2F_FLAG_ABORT;
1705 /* Inode may have changed by block_iterate, so reread it */
1706 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1707 e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
1711 * This fuction clears the htree flag on an inode
1713 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1715 struct ext2_inode inode;
1717 e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1718 inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1719 e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
1720 if (ctx->dirs_to_hash)
1721 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1725 int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1726 ext2_ino_t ino, char *buf)
1728 ext2_filsys fs = ctx->fs;
1729 struct ext2_inode inode;
1730 int inode_modified = 0;
1732 unsigned char *frag, *fsize;
1733 struct problem_context pctx;
1734 problem_t problem = 0;
1736 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
1738 clear_problem_context(&pctx);
1741 pctx.inode = &inode;
1743 if (ext2fs_file_acl_block(fs, &inode) &&
1744 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1745 if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
1746 ext2fs_file_acl_block_set(fs, &inode, 0);
1752 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1753 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1754 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
1755 !(LINUX_S_ISSOCK(inode.i_mode)))
1756 problem = PR_2_BAD_MODE;
1757 else if (LINUX_S_ISCHR(inode.i_mode)
1758 && !e2fsck_pass1_check_device_inode(fs, &inode))
1759 problem = PR_2_BAD_CHAR_DEV;
1760 else if (LINUX_S_ISBLK(inode.i_mode)
1761 && !e2fsck_pass1_check_device_inode(fs, &inode))
1762 problem = PR_2_BAD_BLOCK_DEV;
1763 else if (LINUX_S_ISFIFO(inode.i_mode)
1764 && !e2fsck_pass1_check_device_inode(fs, &inode))
1765 problem = PR_2_BAD_FIFO;
1766 else if (LINUX_S_ISSOCK(inode.i_mode)
1767 && !e2fsck_pass1_check_device_inode(fs, &inode))
1768 problem = PR_2_BAD_SOCKET;
1769 else if (LINUX_S_ISLNK(inode.i_mode)
1770 && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) {
1771 problem = PR_2_INVALID_SYMLINK;
1775 if (fix_problem(ctx, problem, &pctx)) {
1776 deallocate_inode(ctx, ino, 0);
1777 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1785 if (inode.i_faddr) {
1786 if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1793 switch (fs->super->s_creator_os) {
1795 frag = &inode.osd2.hurd2.h_i_frag;
1796 fsize = &inode.osd2.hurd2.h_i_fsize;
1801 if (frag && *frag) {
1803 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
1810 if (fsize && *fsize) {
1812 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
1820 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1821 !(fs->super->s_feature_ro_compat &
1822 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1823 (inode.osd2.linux2.l_i_blocks_hi != 0)) {
1824 pctx.num = inode.osd2.linux2.l_i_blocks_hi;
1825 if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) {
1826 inode.osd2.linux2.l_i_blocks_hi = 0;
1831 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1832 !(fs->super->s_feature_incompat &
1833 EXT4_FEATURE_INCOMPAT_64BIT) &&
1834 inode.osd2.linux2.l_i_file_acl_high != 0) {
1835 pctx.num = inode.osd2.linux2.l_i_file_acl_high;
1836 if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) {
1837 inode.osd2.linux2.l_i_file_acl_high = 0;
1843 if (ext2fs_file_acl_block(fs, &inode) &&
1844 ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) ||
1845 (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) {
1846 if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
1847 ext2fs_file_acl_block_set(fs, &inode, 0);
1852 if (inode.i_dir_acl &&
1853 LINUX_S_ISDIR(inode.i_mode)) {
1854 if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1855 inode.i_dir_acl = 0;
1862 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
1863 if (!not_fixed && ctx->inode_bad_map)
1864 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
1869 * allocate_dir_block --- this function allocates a new directory
1870 * block for a particular inode; this is done if a directory has
1871 * a "hole" in it, or if a directory has a illegal block number
1872 * that was zeroed out and now needs to be replaced.
1874 static int allocate_dir_block(e2fsck_t ctx,
1875 struct ext2_db_entry2 *db,
1876 char *buf EXT2FS_ATTR((unused)),
1877 struct problem_context *pctx)
1879 ext2_filsys fs = ctx->fs;
1882 struct ext2_inode inode;
1884 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
1888 * Read the inode and block bitmaps in; we'll be messing with
1891 e2fsck_read_bitmaps(ctx);
1894 * First, find a free block
1896 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1897 pctx->errcode = ext2fs_map_cluster_block(fs, db->ino, &inode,
1898 db->blockcnt, &blk);
1899 if (pctx->errcode || blk == 0) {
1900 blk = ext2fs_find_inode_goal(fs, db->ino, &inode, db->blockcnt);
1901 pctx->errcode = ext2fs_new_block2(fs, blk,
1902 ctx->block_found_map, &blk);
1903 if (pctx->errcode) {
1904 pctx->str = "ext2fs_new_block";
1905 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1909 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1910 ext2fs_mark_block_bitmap2(fs->block_map, blk);
1911 ext2fs_mark_bb_dirty(fs);
1914 * Now let's create the actual data block for the inode
1917 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
1919 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1920 EXT2_ROOT_INO, &block);
1922 if (pctx->errcode) {
1923 pctx->str = "ext2fs_new_dir_block";
1924 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1928 pctx->errcode = ext2fs_write_dir_block4(fs, blk, block, 0, db->ino);
1929 ext2fs_free_mem(&block);
1930 if (pctx->errcode) {
1931 pctx->str = "ext2fs_write_dir_block";
1932 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1937 * Update the inode block count
1939 ext2fs_iblk_add_blocks(fs, &inode, 1);
1940 if (EXT2_I_SIZE(&inode) < (db->blockcnt+1) * fs->blocksize) {
1941 pctx->errcode = ext2fs_inode_size_set(fs, &inode,
1942 (db->blockcnt+1) * fs->blocksize);
1943 if (pctx->errcode) {
1944 pctx->str = "ext2fs_inode_size_set";
1945 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1949 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
1952 * Finally, update the block pointers for the inode
1955 pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET,
1956 db->blockcnt, 0, &blk);
1957 if (pctx->errcode) {
1958 pctx->str = "ext2fs_block_iterate";
1959 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);