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 short 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 static void update_parents(struct dx_dir_info *dx_dir, int type)
90 struct dx_dirblock_info *dx_db, *dx_parent, *dx_previous;
93 for (b = 0, dx_db = dx_dir->dx_block;
94 b < dx_dir->numblocks;
96 dx_parent = &dx_dir->dx_block[dx_db->parent];
97 if (dx_db->type != type)
101 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
103 if (dx_db->flags & DX_FLAG_FIRST) {
104 dx_parent->min_hash = dx_db->min_hash;
105 if (dx_parent->previous) {
107 &dx_dir->dx_block[dx_parent->previous];
108 dx_previous->node_max_hash =
113 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
115 if (dx_db->flags & DX_FLAG_LAST) {
116 dx_parent->max_hash = dx_db->max_hash;
121 void e2fsck_pass2(e2fsck_t ctx)
123 struct ext2_super_block *sb = ctx->fs->super;
124 struct problem_context pctx;
125 ext2_filsys fs = ctx->fs;
127 #ifdef RESOURCE_TRACK
128 struct resource_track rtrack;
130 struct check_dir_struct cd;
131 struct dx_dir_info *dx_dir;
132 struct dx_dirblock_info *dx_db;
138 int (*check_dir_func)(ext2_filsys fs,
139 struct ext2_db_entry2 *dir_blocks_info,
142 init_resource_track(&rtrack, ctx->fs->io);
143 clear_problem_context(&cd.pctx);
146 mtrace_print("Pass 2");
149 if (!(ctx->options & E2F_OPT_PREEN))
150 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
152 cd.pctx.errcode = e2fsck_setup_icount(ctx, "inode_count",
153 EXT2_ICOUNT_OPT_INCREMENT,
154 ctx->inode_link_info, &ctx->inode_count);
155 if (cd.pctx.errcode) {
156 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
157 ctx->flags |= E2F_FLAG_ABORT;
160 buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
161 "directory scan buffer");
164 * Set up the parent pointer for the root directory, if
165 * present. (If the root directory is not present, we will
166 * create it in pass 3.)
168 (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
173 cd.max = ext2fs_dblist_count2(fs->dblist);
175 cd.ra_entries = ctx->readahead_kb * 1024 / ctx->fs->blocksize;
179 (void) (ctx->progress)(ctx, 2, 0, cd.max);
181 if (ext2fs_has_feature_dir_index(fs->super))
182 ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp);
184 check_dir_func = cd.ra_entries ? check_dir_block2 : check_dir_block;
185 cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_func,
187 if (ctx->flags & E2F_FLAG_RESTART_LATER) {
188 ctx->flags |= E2F_FLAG_RESTART;
189 ctx->flags &= ~E2F_FLAG_RESTART_LATER;
192 if (ctx->flags & E2F_FLAG_RUN_RETURN)
195 if (cd.pctx.errcode) {
196 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
197 ctx->flags |= E2F_FLAG_ABORT;
201 for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
202 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
204 if (e2fsck_dir_will_be_rehashed(ctx, dx_dir->ino) ||
205 dx_dir->numblocks == 0)
207 clear_problem_context(&pctx);
209 pctx.dir = dx_dir->ino;
210 dx_db = dx_dir->dx_block;
211 if (dx_db->flags & DX_FLAG_REFERENCED)
212 dx_db->flags |= DX_FLAG_DUP_REF;
214 dx_db->flags |= DX_FLAG_REFERENCED;
216 * Find all of the first and last leaf blocks, and
217 * update their parent's min and max hash values
219 update_parents(dx_dir, DX_DIRBLOCK_LEAF);
221 /* for 3 level htree: update 2 level parent's min
222 * and max hash values */
223 update_parents(dx_dir, DX_DIRBLOCK_NODE);
225 for (b=0, dx_db = dx_dir->dx_block;
226 b < dx_dir->numblocks;
229 pctx.group = dx_db->parent;
231 if (!(dx_db->flags & DX_FLAG_FIRST) &&
232 (dx_db->min_hash < dx_db->node_min_hash)) {
233 pctx.blk = dx_db->min_hash;
234 pctx.blk2 = dx_db->node_min_hash;
235 code = PR_2_HTREE_MIN_HASH;
236 fix_problem(ctx, code, &pctx);
239 if (dx_db->type == DX_DIRBLOCK_LEAF) {
240 depth = htree_depth(dx_dir, dx_db);
241 if (depth != dx_dir->depth) {
242 pctx.num = dx_dir->depth;
243 code = PR_2_HTREE_BAD_DEPTH;
244 fix_problem(ctx, code, &pctx);
249 * This test doesn't apply for the root block
253 (dx_db->max_hash > dx_db->node_max_hash)) {
254 pctx.blk = dx_db->max_hash;
255 pctx.blk2 = dx_db->node_max_hash;
256 code = PR_2_HTREE_MAX_HASH;
257 fix_problem(ctx, code, &pctx);
260 if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
261 code = PR_2_HTREE_NOTREF;
262 fix_problem(ctx, code, &pctx);
264 } else if (dx_db->flags & DX_FLAG_DUP_REF) {
265 code = PR_2_HTREE_DUPREF;
266 fix_problem(ctx, code, &pctx);
270 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
271 clear_htree(ctx, dx_dir->ino);
272 dx_dir->numblocks = 0;
275 e2fsck_free_dx_dir_info(ctx);
277 ext2fs_free_mem(&buf);
278 ext2fs_free_dblist(fs->dblist);
280 if (ctx->inode_bad_map) {
281 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
282 ctx->inode_bad_map = 0;
284 if (ctx->inode_reg_map) {
285 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
286 ctx->inode_reg_map = 0;
288 if (ctx->encrypted_dirs) {
289 ext2fs_u32_list_free(ctx->encrypted_dirs);
290 ctx->encrypted_dirs = 0;
293 clear_problem_context(&pctx);
294 if (ctx->large_files) {
295 if (!ext2fs_has_feature_large_file(sb) &&
296 fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
297 ext2fs_set_feature_large_file(sb);
298 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
299 ext2fs_mark_super_dirty(fs);
301 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
302 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
303 ext2fs_update_dynamic_rev(fs);
304 ext2fs_mark_super_dirty(fs);
308 print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io);
310 ext2fs_free_mem(&buf);
313 #define MAX_DEPTH 32000
314 static short htree_depth(struct dx_dir_info *dx_dir,
315 struct dx_dirblock_info *dx_db)
319 while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
320 dx_db = &dx_dir->dx_block[dx_db->parent];
326 static int dict_de_cmp(const void *a, const void *b)
328 const struct ext2_dir_entry *de_a, *de_b;
331 de_a = (const struct ext2_dir_entry *) a;
332 a_len = ext2fs_dirent_name_len(de_a);
333 de_b = (const struct ext2_dir_entry *) b;
334 b_len = ext2fs_dirent_name_len(de_b);
337 return (a_len - b_len);
339 return memcmp(de_a->name, de_b->name, a_len);
343 * This is special sort function that makes sure that directory blocks
344 * with a dirblock of zero are sorted to the beginning of the list.
345 * This guarantees that the root node of the htree directories are
346 * processed first, so we know what hash version to use.
348 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
350 const struct ext2_db_entry2 *db_a =
351 (const struct ext2_db_entry2 *) a;
352 const struct ext2_db_entry2 *db_b =
353 (const struct ext2_db_entry2 *) b;
355 if (db_a->blockcnt && !db_b->blockcnt)
358 if (!db_a->blockcnt && db_b->blockcnt)
361 if (db_a->blk != db_b->blk)
362 return (int) (db_a->blk - db_b->blk);
364 if (db_a->ino != db_b->ino)
365 return (int) (db_a->ino - db_b->ino);
367 return (int) (db_a->blockcnt - db_b->blockcnt);
372 * Make sure the first entry in the directory is '.', and that the
373 * directory entry is sane.
375 static int check_dot(e2fsck_t ctx,
376 struct ext2_dir_entry *dirent,
377 ext2_ino_t ino, struct problem_context *pctx)
379 struct ext2_dir_entry *nextdir;
380 unsigned int rec_len, new_len;
383 problem_t problem = 0;
386 problem = PR_2_MISSING_DOT;
387 else if ((ext2fs_dirent_name_len(dirent) != 1) ||
388 (dirent->name[0] != '.'))
389 problem = PR_2_1ST_NOT_DOT;
390 else if (dirent->name[1] != '\0')
391 problem = PR_2_DOT_NULL_TERM;
393 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
395 if (fix_problem(ctx, problem, pctx)) {
397 rec_len = dirent->rec_len = 12;
399 ext2fs_dirent_set_name_len(dirent, 1);
400 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
401 dirent->name[0] = '.';
402 dirent->name[1] = '\0';
407 if (dirent->inode != ino) {
408 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
414 new_len = rec_len - 12;
417 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
418 nextdir = (struct ext2_dir_entry *)
419 ((char *) dirent + 12);
420 dirent->rec_len = 12;
421 (void) ext2fs_set_rec_len(ctx->fs, new_len,
424 ext2fs_dirent_set_name_len(nextdir, 0);
425 ext2fs_dirent_set_file_type(nextdir,
435 * Make sure the second entry in the directory is '..', and that the
436 * directory entry is sane. We do not check the inode number of '..'
437 * here; this gets done in pass 3.
439 static int check_dotdot(e2fsck_t ctx,
440 struct ext2_dir_entry *dirent,
441 ext2_ino_t ino, struct problem_context *pctx)
443 problem_t problem = 0;
444 unsigned int rec_len;
447 problem = PR_2_MISSING_DOT_DOT;
448 else if ((ext2fs_dirent_name_len(dirent) != 2) ||
449 (dirent->name[0] != '.') ||
450 (dirent->name[1] != '.'))
451 problem = PR_2_2ND_NOT_DOT_DOT;
452 else if (dirent->name[2] != '\0')
453 problem = PR_2_DOT_DOT_NULL_TERM;
455 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
457 if (fix_problem(ctx, problem, pctx)) {
459 dirent->rec_len = 12;
461 * Note: we don't have the parent inode just
462 * yet, so we will fill it in with the root
463 * inode. This will get fixed in pass 3.
465 dirent->inode = EXT2_ROOT_INO;
466 ext2fs_dirent_set_name_len(dirent, 2);
467 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
468 dirent->name[0] = '.';
469 dirent->name[1] = '.';
470 dirent->name[2] = '\0';
475 if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) {
476 fix_problem(ctx, PR_2_NO_DIRINFO, pctx);
483 * Check to make sure a directory entry doesn't contain any illegal
486 static int check_name(e2fsck_t ctx,
487 struct ext2_dir_entry *dirent,
488 struct problem_context *pctx)
494 for ( i = 0; i < ext2fs_dirent_name_len(dirent); i++) {
495 if (dirent->name[i] != '/' && dirent->name[i] != '\0')
498 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
501 dirent->name[i] = '.';
507 static int encrypted_check_name(e2fsck_t ctx,
508 struct ext2_dir_entry *dirent,
509 struct problem_context *pctx)
511 if (ext2fs_dirent_name_len(dirent) < EXT4_CRYPTO_BLOCK_SIZE) {
512 if (fix_problem(ctx, PR_2_BAD_ENCRYPTED_NAME, pctx)) {
516 ext2fs_unmark_valid(ctx->fs);
522 * Check the directory filetype (if present)
524 static _INLINE_ int check_filetype(e2fsck_t ctx,
525 struct ext2_dir_entry *dirent,
526 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
527 struct problem_context *pctx)
529 int filetype = ext2fs_dirent_file_type(dirent);
530 int should_be = EXT2_FT_UNKNOWN;
531 struct ext2_inode inode;
533 if (!ext2fs_has_feature_filetype(ctx->fs->super)) {
535 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
537 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
541 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) {
542 should_be = EXT2_FT_DIR;
543 } else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map,
545 should_be = EXT2_FT_REG_FILE;
546 } else if (ctx->inode_bad_map &&
547 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
551 e2fsck_read_inode(ctx, dirent->inode, &inode,
553 should_be = ext2_file_type(inode.i_mode);
555 if (filetype == should_be)
557 pctx->num = should_be;
559 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
563 ext2fs_dirent_set_file_type(dirent, should_be);
567 static void parse_int_node(ext2_filsys fs,
568 struct ext2_db_entry2 *db,
569 struct check_dir_struct *cd,
570 struct dx_dir_info *dx_dir,
571 char *block_buf, int failed_csum)
573 struct ext2_dx_root_info *root;
574 struct ext2_dx_entry *ent;
575 struct ext2_dx_countlimit *limit;
576 struct dx_dirblock_info *dx_db;
577 int i, expect_limit, count;
579 ext2_dirhash_t min_hash = 0xffffffff;
580 ext2_dirhash_t max_hash = 0;
581 ext2_dirhash_t hash = 0, prev_hash;
584 if (db->blockcnt == 0) {
585 root = (struct ext2_dx_root_info *) (block_buf + 24);
588 printf("Root node dump:\n");
589 printf("\t Reserved zero: %u\n", root->reserved_zero);
590 printf("\t Hash Version: %u\n", root->hash_version);
591 printf("\t Info length: %u\n", root->info_length);
592 printf("\t Indirect levels: %u\n", root->indirect_levels);
593 printf("\t Flags: %x\n", root->unused_flags);
596 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
599 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
600 fix_problem(cd->ctx, PR_2_HTREE_ROOT_CSUM_INVALID,
604 ent = (struct ext2_dx_entry *) (block_buf+8);
607 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
608 fix_problem(cd->ctx, PR_2_HTREE_NODE_CSUM_INVALID,
613 limit = (struct ext2_dx_countlimit *) ent;
616 printf("Number of entries (count): %d\n",
617 ext2fs_le16_to_cpu(limit->count));
618 printf("Number of entries (limit): %d\n",
619 ext2fs_le16_to_cpu(limit->limit));
622 count = ext2fs_le16_to_cpu(limit->count);
623 if (ext2fs_has_feature_metadata_csum(fs->super))
624 csum_size = sizeof(struct ext2_dx_tail);
625 expect_limit = (fs->blocksize -
626 (csum_size + ((char *) ent - block_buf))) /
627 sizeof(struct ext2_dx_entry);
628 if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
629 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
630 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
633 if (count > expect_limit) {
634 cd->pctx.num = count;
635 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
637 count = expect_limit;
640 for (i=0; i < count; i++) {
642 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
644 printf("Entry #%d: Hash 0x%08x, block %u\n", i,
645 hash, ext2fs_le32_to_cpu(ent[i].block));
647 blk = ext2fs_le32_to_cpu(ent[i].block) & EXT4_DX_BLOCK_MASK;
648 /* Check to make sure the block is valid */
649 if (blk >= (blk_t) dx_dir->numblocks) {
651 if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
656 if (hash < prev_hash &&
657 fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
659 dx_db = &dx_dir->dx_block[blk];
660 if (dx_db->flags & DX_FLAG_REFERENCED) {
661 dx_db->flags |= DX_FLAG_DUP_REF;
663 dx_db->flags |= DX_FLAG_REFERENCED;
664 dx_db->parent = db->blockcnt;
668 i ? (ext2fs_le32_to_cpu(ent[i-1].block) &
669 EXT4_DX_BLOCK_MASK) : 0;
675 dx_db->node_min_hash = hash;
677 dx_db->node_max_hash =
678 ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
680 dx_db->node_max_hash = 0xfffffffe;
681 dx_db->flags |= DX_FLAG_LAST;
684 dx_db->flags |= DX_FLAG_FIRST;
687 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
688 db->blockcnt, min_hash, max_hash);
690 dx_db = &dx_dir->dx_block[db->blockcnt];
691 dx_db->min_hash = min_hash;
692 dx_db->max_hash = max_hash;
696 clear_htree(cd->ctx, cd->pctx.ino);
697 dx_dir->numblocks = 0;
698 e2fsck_rehash_dir_later(cd->ctx, cd->pctx.ino);
702 * Given a busted directory, try to salvage it somehow.
705 static void salvage_directory(ext2_filsys fs,
706 struct ext2_dir_entry *dirent,
707 struct ext2_dir_entry *prev,
708 unsigned int *offset,
709 unsigned int block_len)
711 char *cp = (char *) dirent;
713 unsigned int rec_len, prev_rec_len;
714 unsigned int name_len;
717 * If the space left for the entry is too small to be an entry,
718 * we can't access dirent's fields, so plumb in the values needed
719 * so that the previous entry absorbs this one.
721 if (block_len - *offset < EXT2_DIR_ENTRY_HEADER_LEN) {
723 rec_len = block_len - *offset;
725 name_len = ext2fs_dirent_name_len(dirent);
726 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
728 left = block_len - *offset - rec_len;
731 * Special case of directory entry of size 8: copy what's left
732 * of the directory block up to cover up the invalid hole.
734 if ((left >= 12) && (rec_len == EXT2_DIR_ENTRY_HEADER_LEN)) {
735 memmove(cp, cp+EXT2_DIR_ENTRY_HEADER_LEN, left);
736 memset(cp + left, 0, EXT2_DIR_ENTRY_HEADER_LEN);
740 * If the directory entry overruns the end of the directory
741 * block, and the name is small enough to fit, then adjust the
745 ((int) rec_len + left > EXT2_DIR_ENTRY_HEADER_LEN) &&
746 ((int) name_len + EXT2_DIR_ENTRY_HEADER_LEN <= (int) rec_len + left) &&
747 dirent->inode <= fs->super->s_inodes_count &&
748 strnlen(dirent->name, name_len) == name_len) {
749 (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent);
753 * If the record length of the directory entry is a multiple
754 * of four, and not too big, such that it is valid, let the
755 * previous directory entry absorb the invalid one.
757 if (prev && rec_len && (rec_len % 4) == 0 &&
758 (*offset + rec_len <= block_len)) {
759 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
760 prev_rec_len += rec_len;
761 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
766 * Default salvage method --- kill all of the directory
767 * entries for the rest of the block. We will either try to
768 * absorb it into the previous directory entry, or create a
769 * new empty directory entry the rest of the directory block.
772 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
773 prev_rec_len += block_len - *offset;
774 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
775 *offset = fs->blocksize;
777 rec_len = block_len - *offset;
778 (void) ext2fs_set_rec_len(fs, rec_len, dirent);
779 ext2fs_dirent_set_name_len(dirent, 0);
780 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
785 #define NEXT_DIRENT(d) ((void *)((char *)(d) + (d)->rec_len))
786 static errcode_t insert_dirent_tail(ext2_filsys fs, void *dirbuf)
788 struct ext2_dir_entry *d;
790 struct ext2_dir_entry_tail *t;
793 top = EXT2_DIRENT_TAIL(dirbuf, fs->blocksize);
795 while (d->rec_len && !(d->rec_len & 0x3) && NEXT_DIRENT(d) <= top)
799 unsigned int min_size = EXT2_DIR_REC_LEN(
800 ext2fs_dirent_name_len(dirbuf));
801 if (min_size > (char *)top - (char *)d)
802 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
803 d->rec_len = (char *)top - (char *)d;
806 t = (struct ext2_dir_entry_tail *)top;
807 if (t->det_reserved_zero1 ||
808 t->det_rec_len != sizeof(struct ext2_dir_entry_tail) ||
809 t->det_reserved_name_len != EXT2_DIR_NAME_LEN_CSUM)
810 ext2fs_initialize_dirent_tail(fs, t);
816 static errcode_t fix_inline_dir_size(e2fsck_t ctx, ext2_ino_t ino,
817 size_t *inline_data_size,
818 struct problem_context *pctx,
821 ext2_filsys fs = ctx->fs;
822 struct ext2_inode inode;
823 size_t new_size, old_size;
826 old_size = *inline_data_size;
828 * If there's not enough bytes to start the "second" dir block
829 * (in the EA space) then truncate everything to the first block.
831 if (old_size > EXT4_MIN_INLINE_DATA_SIZE &&
832 old_size < EXT4_MIN_INLINE_DATA_SIZE +
833 EXT2_DIR_REC_LEN(1)) {
834 old_size = EXT4_MIN_INLINE_DATA_SIZE;
837 /* Increase to the next four-byte boundary for salvaging */
838 new_size = old_size + (4 - (old_size & 3));
839 memset(buf + old_size, 0, new_size - old_size);
840 retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size);
841 if (retval == EXT2_ET_INLINE_DATA_NO_SPACE) {
842 /* Or we can't, so truncate. */
844 retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size);
846 if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED,
854 if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED,
863 retval = ext2fs_read_inode(fs, ino, &inode);
867 retval = ext2fs_inode_size_set(fs, &inode, new_size);
871 inode.i_flags &= ~EXT4_INLINE_DATA_FL;
872 retval = ext2fs_write_inode(fs, ino, &inode);
875 *inline_data_size = new_size;
881 static int check_dir_block2(ext2_filsys fs,
882 struct ext2_db_entry2 *db,
886 struct check_dir_struct *cd = priv_data;
888 if (cd->ra_entries && cd->list_offset >= cd->next_ra_off) {
889 err = e2fsck_readahead_dblist(fs,
890 E2FSCK_RA_DBLIST_IGNORE_BLOCKCNT,
892 cd->list_offset + cd->ra_entries / 8,
896 cd->next_ra_off = cd->list_offset + (cd->ra_entries * 7 / 8);
899 err = check_dir_block(fs, db, priv_data);
904 static int check_dir_block(ext2_filsys fs,
905 struct ext2_db_entry2 *db,
908 struct dx_dir_info *dx_dir;
909 struct dx_dirblock_info *dx_db = 0;
910 struct ext2_dir_entry *dirent, *prev, dot, dotdot;
912 unsigned int offset = 0;
913 int dir_modified = 0;
915 unsigned int rec_len;
916 blk64_t block_nr = db->blk;
917 ext2_ino_t ino = db->ino;
918 ext2_ino_t subdir_parent;
920 struct check_dir_struct *cd;
924 struct ext2_dx_root_info *root;
925 struct ext2_dx_countlimit *limit;
926 static dict_t de_dict;
927 struct problem_context pctx;
930 int dx_csum_size = 0, de_csum_size = 0;
933 size_t inline_data_size = 0;
936 size_t max_block_size;
938 static char *eop_read_dirblock = NULL;
940 cd = (struct check_dir_struct *) priv_data;
941 ibuf = buf = cd->buf;
944 if (ctx->flags & E2F_FLAG_RUN_RETURN)
947 if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
950 if (ext2fs_has_feature_metadata_csum(fs->super)) {
951 dx_csum_size = sizeof(struct ext2_dx_tail);
952 de_csum_size = sizeof(struct ext2_dir_entry_tail);
955 if (ext2fs_has_feature_filetype(fs->super))
956 filetype = EXT2_FT_DIR << 8;
959 * Make sure the inode is still in use (could have been
960 * deleted in the duplicate/bad blocks pass.
962 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino)))
966 cd->pctx.blk = block_nr;
967 cd->pctx.blkcount = db->blockcnt;
972 if (ext2fs_has_feature_inline_data(fs->super)) {
975 ec = ext2fs_inline_data_size(fs, ino, &inline_data_size);
976 if (ec && ec != EXT2_ET_NO_INLINE_DATA)
980 /* This will allow (at some point in the future) to punch out empty
981 * directory blocks and reduce the space used by a directory that grows
982 * very large and then the files are deleted. For now, all that is
983 * needed is to avoid e2fsck filling in these holes as part of
985 if (db->blk == 0 && ext2fs_has_feature_largedir(fs->super) &&
986 !ext2fs_has_feature_inline_data(fs->super))
989 if (db->blk == 0 && !inline_data_size) {
990 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
1000 if (ctx->dirs_to_hash &&
1001 ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
1005 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
1009 if (!eop_read_dirblock)
1010 eop_read_dirblock = (char *) _("reading directory block");
1011 ehandler_operation(eop_read_dirblock);
1012 if (inline_data_size) {
1013 memset(buf, 0, fs->blocksize - inline_data_size);
1014 cd->pctx.errcode = ext2fs_inline_data_get(fs, ino, 0, buf, 0);
1015 if (cd->pctx.errcode)
1016 goto inline_read_fail;
1017 #ifdef WORDS_BIGENDIAN
1019 goto skip_first_read_swab;
1020 *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf));
1021 cd->pctx.errcode = ext2fs_dirent_swab_in2(fs,
1022 buf + EXT4_INLINE_DATA_DOTDOT_SIZE,
1023 EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DATA_DOTDOT_SIZE,
1025 if (cd->pctx.errcode)
1026 goto inline_read_fail;
1027 skip_first_read_swab:
1028 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE ||
1030 goto inline_read_fail;
1031 cd->pctx.errcode = ext2fs_dirent_swab_in2(fs,
1032 buf + EXT4_MIN_INLINE_DATA_SIZE,
1033 inline_data_size - EXT4_MIN_INLINE_DATA_SIZE,
1037 cd->pctx.errcode = ext2fs_read_dir_block4(fs, block_nr,
1041 pctx.num = inline_data_size;
1042 if (((inline_data_size & 3) ||
1043 (inline_data_size > EXT4_MIN_INLINE_DATA_SIZE &&
1044 inline_data_size < EXT4_MIN_INLINE_DATA_SIZE +
1045 EXT2_DIR_REC_LEN(1))) &&
1046 fix_problem(ctx, PR_2_BAD_INLINE_DIR_SIZE, &pctx)) {
1047 errcode_t err = fix_inline_dir_size(ctx, ino,
1048 &inline_data_size, &pctx,
1051 return DIRENT_ABORT;
1054 ehandler_operation(0);
1055 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
1056 cd->pctx.errcode = 0; /* We'll handle this ourselves */
1057 else if (cd->pctx.errcode == EXT2_ET_DIR_CSUM_INVALID) {
1058 cd->pctx.errcode = 0; /* We'll handle this ourselves */
1061 if (cd->pctx.errcode) {
1063 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
1064 ctx->flags |= E2F_FLAG_ABORT;
1065 return DIRENT_ABORT;
1067 ext2fs_new_dir_block(fs, db->blockcnt == 0 ? ino : 0,
1068 EXT2_ROOT_INO, &buf2);
1069 memcpy(buf, buf2, fs->blocksize);
1070 ext2fs_free_mem(&buf2);
1072 dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
1073 if (dx_dir && dx_dir->numblocks) {
1074 if (db->blockcnt >= dx_dir->numblocks) {
1076 if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK,
1078 clear_htree(ctx, ino);
1079 dx_dir->numblocks = 0;
1083 fatal_error(ctx, _("Can not continue."));
1085 dx_db = &dx_dir->dx_block[db->blockcnt];
1086 dx_db->type = DX_DIRBLOCK_LEAF;
1087 dx_db->phys = block_nr;
1088 dx_db->min_hash = ~0;
1089 dx_db->max_hash = 0;
1091 dirent = (struct ext2_dir_entry *) buf;
1092 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1093 limit = (struct ext2_dx_countlimit *) (buf+8);
1094 if (db->blockcnt == 0) {
1095 root = (struct ext2_dx_root_info *) (buf + 24);
1096 dx_db->type = DX_DIRBLOCK_ROOT;
1097 dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
1098 if ((root->reserved_zero ||
1099 root->info_length < 8 ||
1100 root->indirect_levels >=
1101 ext2_dir_htree_level(fs)) &&
1102 fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
1103 clear_htree(ctx, ino);
1104 dx_dir->numblocks = 0;
1107 dx_dir->hashversion = root->hash_version;
1108 if ((dx_dir->hashversion <= EXT2_HASH_TEA) &&
1109 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
1110 dx_dir->hashversion += 3;
1111 dx_dir->depth = root->indirect_levels + 1;
1112 } else if ((dirent->inode == 0) &&
1113 (rec_len == fs->blocksize) &&
1114 (ext2fs_dirent_name_len(dirent) == 0) &&
1115 (ext2fs_le16_to_cpu(limit->limit) ==
1116 ((fs->blocksize - (8 + dx_csum_size)) /
1117 sizeof(struct ext2_dx_entry)))) {
1118 dx_db->type = DX_DIRBLOCK_NODE;
1120 is_leaf = dx_db ? (dx_db->type == DX_DIRBLOCK_LEAF) : 0;
1124 /* Leaf node with no space for csum? Rebuild dirs in pass 3A. */
1125 if (is_leaf && !inline_data_size && failed_csum &&
1126 !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
1128 if (e2fsck_dir_will_be_rehashed(ctx, ino)) {
1132 if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_MISSING_CSUM,
1135 e2fsck_rehash_dir_later(ctx, ino);
1139 /* htree nodes don't use fake dirents to store checksums */
1144 if (inline_data_size) {
1146 buf += EXT4_MIN_INLINE_DATA_SIZE;
1147 max_block_size = inline_data_size - EXT4_MIN_INLINE_DATA_SIZE;
1148 /* Zero-length second block, just exit */
1149 if (max_block_size == 0)
1152 max_block_size = EXT4_MIN_INLINE_DATA_SIZE;
1155 max_block_size = fs->blocksize - de_csum_size;
1157 if (ctx->encrypted_dirs)
1158 encrypted = ext2fs_u32_list_test(ctx->encrypted_dirs, ino);
1160 dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
1164 ext2_ino_t first_unused_inode;
1165 unsigned int name_len;
1168 if (!inline_data_size || dot_state > 1) {
1169 dirent = (struct ext2_dir_entry *) (buf + offset);
1171 * If there's not even space for the entry header,
1172 * force salvaging this dir.
1174 if (max_block_size - offset < EXT2_DIR_ENTRY_HEADER_LEN)
1175 rec_len = EXT2_DIR_REC_LEN(1);
1177 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1178 cd->pctx.dirent = dirent;
1179 cd->pctx.num = offset;
1180 if ((offset + rec_len > max_block_size) ||
1182 ((rec_len % 4) != 0) ||
1183 (((unsigned) ext2fs_dirent_name_len(dirent) + EXT2_DIR_ENTRY_HEADER_LEN) > rec_len)) {
1184 if (fix_problem(ctx, PR_2_DIR_CORRUPTED,
1186 #ifdef WORDS_BIGENDIAN
1188 * On big-endian systems, if the dirent
1189 * swap routine finds a rec_len that it
1190 * doesn't like, it continues
1191 * processing the block as if rec_len
1192 * == EXT2_DIR_ENTRY_HEADER_LEN. This means that the name
1193 * field gets byte swapped, which means
1194 * that salvage will not detect the
1195 * correct name length (unless the name
1196 * has a length that's an exact
1197 * multiple of four bytes), and it'll
1198 * discard the entry (unnecessarily)
1199 * and the rest of the dirent block.
1200 * Therefore, swap the rest of the
1201 * block back to disk order, run
1202 * salvage, and re-swap anything after
1203 * the salvaged dirent.
1205 int need_reswab = 0;
1206 if (rec_len < EXT2_DIR_ENTRY_HEADER_LEN || rec_len % 4) {
1208 ext2fs_dirent_swab_in2(fs,
1209 ((char *)dirent) + EXT2_DIR_ENTRY_HEADER_LEN,
1210 max_block_size - offset - EXT2_DIR_ENTRY_HEADER_LEN,
1214 salvage_directory(fs, dirent, prev,
1217 #ifdef WORDS_BIGENDIAN
1219 (void) ext2fs_get_rec_len(fs,
1221 ext2fs_dirent_swab_in2(fs,
1222 ((char *)dirent) + offset + rec_len,
1223 max_block_size - offset - rec_len,
1230 goto abort_free_dict;
1233 if (dot_state == 0) {
1234 memset(&dot, 0, sizeof(dot));
1236 dirent->inode = ino;
1237 dirent->rec_len = EXT2_DIR_REC_LEN(1);
1238 dirent->name_len = 1 | filetype;
1239 dirent->name[0] = '.';
1240 } else if (dot_state == 1) {
1241 memset(&dotdot, 0, sizeof(dotdot));
1244 ((struct ext2_dir_entry *)buf)->inode;
1245 dirent->rec_len = EXT2_DIR_REC_LEN(2);
1246 dirent->name_len = 2 | filetype;
1247 dirent->name[0] = '.';
1248 dirent->name[1] = '.';
1250 fatal_error(ctx, _("Can not continue."));
1252 cd->pctx.dirent = dirent;
1253 cd->pctx.num = offset;
1256 if (dot_state == 0) {
1257 if (check_dot(ctx, dirent, ino, &cd->pctx))
1259 } else if (dot_state == 1) {
1260 ret = check_dotdot(ctx, dirent, ino, &cd->pctx);
1262 goto abort_free_dict;
1265 } else if (dirent->inode == ino) {
1266 problem = PR_2_LINK_DOT;
1267 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
1277 * Make sure the inode listed is a legal one.
1279 name_len = ext2fs_dirent_name_len(dirent);
1280 if (((dirent->inode != EXT2_ROOT_INO) &&
1281 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
1282 (dirent->inode > fs->super->s_inodes_count)) {
1283 problem = PR_2_BAD_INO;
1284 } else if (ctx->inode_bb_map &&
1285 (ext2fs_test_inode_bitmap2(ctx->inode_bb_map,
1288 * If the inode is in a bad block, offer to
1291 problem = PR_2_BB_INODE;
1292 } else if ((dot_state > 1) && (name_len == 1) &&
1293 (dirent->name[0] == '.')) {
1295 * If there's a '.' entry in anything other
1296 * than the first directory entry, it's a
1297 * duplicate entry that should be removed.
1299 problem = PR_2_DUP_DOT;
1300 } else if ((dot_state > 1) && (name_len == 2) &&
1301 (dirent->name[0] == '.') &&
1302 (dirent->name[1] == '.')) {
1304 * If there's a '..' entry in anything other
1305 * than the second directory entry, it's a
1306 * duplicate entry that should be removed.
1308 problem = PR_2_DUP_DOT_DOT;
1309 } else if ((dot_state > 1) &&
1310 (dirent->inode == EXT2_ROOT_INO)) {
1312 * Don't allow links to the root directory.
1313 * We check this specially to make sure we
1314 * catch this error case even if the root
1315 * directory hasn't been created yet.
1317 problem = PR_2_LINK_ROOT;
1318 } else if ((dot_state > 1) && (name_len == 0)) {
1320 * Don't allow zero-length directory names.
1322 problem = PR_2_NULL_NAME;
1326 if (fix_problem(ctx, problem, &cd->pctx)) {
1331 ext2fs_unmark_valid(fs);
1332 if (problem == PR_2_BAD_INO)
1338 * If the inode was marked as having bad fields in
1339 * pass1, process it and offer to fix/clear it.
1340 * (We wait until now so that we can display the
1341 * pathname to the user.)
1343 if (ctx->inode_bad_map &&
1344 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
1346 if (e2fsck_process_bad_inode(ctx, ino,
1348 buf + fs->blocksize)) {
1353 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1354 return DIRENT_ABORT;
1357 group = ext2fs_group_of_ino(fs, dirent->inode);
1358 first_unused_inode = group * fs->super->s_inodes_per_group +
1359 1 + fs->super->s_inodes_per_group -
1360 ext2fs_bg_itable_unused(fs, group);
1361 cd->pctx.group = group;
1364 * Check if the inode was missed out because
1365 * _INODE_UNINIT flag was set or bg_itable_unused was
1366 * incorrect. If so, clear the _INODE_UNINIT flag and
1367 * restart e2fsck. In the future it would be nice if
1368 * we could call a function in pass1.c that checks the
1369 * newly visible inodes.
1371 if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) {
1372 pctx.num = dirent->inode;
1373 if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT,
1375 ext2fs_bg_flags_clear(fs, group,
1376 EXT2_BG_INODE_UNINIT);
1377 ext2fs_mark_super_dirty(fs);
1378 ctx->flags |= E2F_FLAG_RESTART_LATER;
1380 ext2fs_unmark_valid(fs);
1381 if (problem == PR_2_BAD_INO)
1384 } else if (dirent->inode >= first_unused_inode) {
1385 pctx.num = dirent->inode;
1386 if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){
1387 ext2fs_bg_itable_unused_set(fs, group, 0);
1388 ext2fs_mark_super_dirty(fs);
1389 ctx->flags |= E2F_FLAG_RESTART_LATER;
1391 ext2fs_unmark_valid(fs);
1392 if (problem == PR_2_BAD_INO)
1398 * Offer to clear unused inodes; if we are going to be
1399 * restarting the scan due to bg_itable_unused being
1400 * wrong, then don't clear any inodes to avoid zapping
1401 * inodes that were skipped during pass1 due to an
1402 * incorrect bg_itable_unused; we'll get any real
1403 * problems after we restart.
1405 if (!(ctx->flags & E2F_FLAG_RESTART_LATER) &&
1406 !(ext2fs_test_inode_bitmap2(ctx->inode_used_map,
1408 problem = PR_2_UNUSED_INODE;
1411 if (fix_problem(ctx, problem, &cd->pctx)) {
1416 ext2fs_unmark_valid(fs);
1417 if (problem == PR_2_BAD_INO)
1422 if (!encrypted && check_name(ctx, dirent, &cd->pctx))
1425 if (encrypted && (dot_state) > 1 &&
1426 encrypted_check_name(ctx, dirent, &cd->pctx)) {
1431 if (check_filetype(ctx, dirent, ino, &cd->pctx))
1435 if (dx_dir->casefolded_hash)
1436 hash_flags = EXT4_CASEFOLD_FL;
1438 ext2fs_dirhash2(dx_dir->hashversion, dirent->name,
1439 ext2fs_dirent_name_len(dirent),
1440 fs->encoding, hash_flags,
1441 fs->super->s_hash_seed, &hash, 0);
1442 if (hash < dx_db->min_hash)
1443 dx_db->min_hash = hash;
1444 if (hash > dx_db->max_hash)
1445 dx_db->max_hash = hash;
1449 * If this is a directory, then mark its parent in its
1450 * dir_info structure. If the parent field is already
1451 * filled in, then this directory has more than one
1452 * hard link. We assume the first link is correct,
1453 * and ask the user if he/she wants to clear this one.
1455 if ((dot_state > 1) &&
1456 (ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
1458 if (e2fsck_dir_info_get_parent(ctx, dirent->inode,
1460 cd->pctx.ino = dirent->inode;
1461 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
1462 goto abort_free_dict;
1464 if (subdir_parent) {
1465 cd->pctx.ino2 = subdir_parent;
1466 if (fix_problem(ctx, PR_2_LINK_DIR,
1474 (void) e2fsck_dir_info_set_parent(ctx,
1475 dirent->inode, ino);
1481 } else if (dict_lookup(&de_dict, dirent)) {
1482 clear_problem_context(&pctx);
1484 pctx.dirent = dirent;
1485 fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
1486 e2fsck_rehash_dir_later(ctx, ino);
1489 dict_alloc_insert(&de_dict, dirent, dirent);
1491 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1494 ctx->fs_links_count++;
1495 ctx->fs_total_count++;
1499 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1500 if (!inline_data_size || dot_state > 1) {
1503 if (dot_state == 1) {
1506 * If we get here, we're checking an inline
1507 * directory and we've just checked a (fake)
1508 * dotdot entry that we created on the stack.
1509 * Therefore set 'prev' to NULL so that if we
1510 * call salvage_directory on the next entry,
1511 * it won't try to absorb the next entry into
1512 * the on-stack dotdot entry.
1518 } while (offset < max_block_size);
1524 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1525 db->blockcnt, dx_db->type,
1526 dx_db->min_hash, dx_db->max_hash);
1528 cd->pctx.dir = cd->pctx.ino;
1529 if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1530 (dx_db->type == DX_DIRBLOCK_NODE))
1531 parse_int_node(fs, db, cd, dx_dir, buf, failed_csum);
1534 if (offset != max_block_size) {
1535 cd->pctx.num = rec_len + offset - max_block_size;
1536 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1537 dirent->rec_len = cd->pctx.num;
1542 int flags, will_rehash;
1543 /* leaf block with no tail? Rehash dirs later. */
1544 if (ext2fs_has_feature_metadata_csum(fs->super) &&
1546 !inline_data_size &&
1547 !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
1548 if (insert_dirent_tail(fs, buf) == 0)
1550 e2fsck_rehash_dir_later(ctx, ino);
1554 will_rehash = e2fsck_dir_will_be_rehashed(ctx, ino);
1556 flags = ctx->fs->flags;
1557 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1559 if (inline_data_size) {
1561 #ifdef WORDS_BIGENDIAN
1563 goto skip_first_write_swab;
1564 *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf));
1565 cd->pctx.errcode = ext2fs_dirent_swab_out2(fs,
1566 buf + EXT4_INLINE_DATA_DOTDOT_SIZE,
1567 EXT4_MIN_INLINE_DATA_SIZE -
1568 EXT4_INLINE_DATA_DOTDOT_SIZE,
1570 if (cd->pctx.errcode)
1571 goto skip_second_write_swab;
1572 skip_first_write_swab:
1573 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE ||
1575 goto skip_second_write_swab;
1576 cd->pctx.errcode = ext2fs_dirent_swab_out2(fs,
1577 buf + EXT4_MIN_INLINE_DATA_SIZE,
1579 EXT4_MIN_INLINE_DATA_SIZE,
1581 skip_second_write_swab:
1582 if (cd->pctx.errcode &&
1583 !fix_problem(ctx, PR_2_WRITE_DIRBLOCK, &cd->pctx))
1584 goto abort_free_dict;
1587 ext2fs_inline_data_set(fs, ino, 0, buf,
1590 cd->pctx.errcode = ext2fs_write_dir_block4(fs, block_nr,
1593 ctx->fs->flags = (flags &
1594 EXT2_FLAG_IGNORE_CSUM_ERRORS) |
1596 ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
1597 if (cd->pctx.errcode) {
1598 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
1600 goto abort_free_dict;
1602 ext2fs_mark_changed(fs);
1603 } else if (is_leaf && failed_csum && !dir_modified) {
1605 * If a leaf node that fails csum makes it this far without
1606 * alteration, ask the user if the checksum should be fixed.
1608 if (fix_problem(ctx, PR_2_LEAF_NODE_ONLY_CSUM_INVALID,
1612 dict_free_nodes(&de_dict);
1615 ctx->flags |= E2F_FLAG_ABORT;
1616 dict_free_nodes(&de_dict);
1617 return DIRENT_ABORT;
1623 blk64_t last_cluster;
1627 * This function is called to deallocate a block, and is an interator
1628 * functioned called by deallocate inode via ext2fs_iterate_block().
1630 static int deallocate_inode_block(ext2_filsys fs,
1632 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1633 blk64_t ref_block EXT2FS_ATTR((unused)),
1634 int ref_offset EXT2FS_ATTR((unused)),
1637 struct del_block *p = priv_data;
1638 blk64_t cluster = EXT2FS_B2C(fs, *block_nr);
1643 if (cluster == p->last_cluster)
1646 p->last_cluster = cluster;
1647 if ((*block_nr < fs->super->s_first_data_block) ||
1648 (*block_nr >= ext2fs_blocks_count(fs->super)))
1651 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
1657 * This function deallocates an inode
1659 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
1661 ext2_filsys fs = ctx->fs;
1662 struct ext2_inode inode;
1663 struct problem_context pctx;
1665 struct del_block del_block;
1667 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1668 clear_problem_context(&pctx);
1672 * Fix up the bitmaps...
1674 e2fsck_read_bitmaps(ctx);
1675 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1677 if (ext2fs_file_acl_block(fs, &inode) &&
1678 ext2fs_has_feature_xattr(fs->super)) {
1679 pctx.errcode = ext2fs_adjust_ea_refcount3(fs,
1680 ext2fs_file_acl_block(fs, &inode),
1681 block_buf, -1, &count, ino);
1682 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1687 pctx.blk = ext2fs_file_acl_block(fs, &inode);
1688 fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1689 ctx->flags |= E2F_FLAG_ABORT;
1693 ext2fs_block_alloc_stats2(fs,
1694 ext2fs_file_acl_block(fs, &inode), -1);
1696 ext2fs_file_acl_block_set(fs, &inode, 0);
1699 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1702 /* Inline data inodes don't have blocks to iterate */
1703 if (inode.i_flags & EXT4_INLINE_DATA_FL)
1706 if (LINUX_S_ISREG(inode.i_mode) &&
1707 ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode)))
1710 del_block.ctx = ctx;
1712 del_block.last_cluster = 0;
1713 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1714 deallocate_inode_block,
1717 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
1718 ctx->flags |= E2F_FLAG_ABORT;
1722 /* Inode may have changed by block_iterate, so reread it */
1723 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1724 e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
1728 * This function clears the htree flag on an inode
1730 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1732 struct ext2_inode inode;
1734 e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1735 inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1736 e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
1737 if (ctx->dirs_to_hash)
1738 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1742 int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1743 ext2_ino_t ino, char *buf)
1745 ext2_filsys fs = ctx->fs;
1746 struct ext2_inode inode;
1747 int inode_modified = 0;
1749 unsigned char *frag, *fsize;
1750 struct problem_context pctx;
1751 problem_t problem = 0;
1753 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
1755 clear_problem_context(&pctx);
1758 pctx.inode = &inode;
1760 if (ext2fs_file_acl_block(fs, &inode) &&
1761 !ext2fs_has_feature_xattr(fs->super)) {
1762 if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
1763 ext2fs_file_acl_block_set(fs, &inode, 0);
1769 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1770 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1771 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
1772 !(LINUX_S_ISSOCK(inode.i_mode)))
1773 problem = PR_2_BAD_MODE;
1774 else if (LINUX_S_ISCHR(inode.i_mode)
1775 && !e2fsck_pass1_check_device_inode(fs, &inode))
1776 problem = PR_2_BAD_CHAR_DEV;
1777 else if (LINUX_S_ISBLK(inode.i_mode)
1778 && !e2fsck_pass1_check_device_inode(fs, &inode))
1779 problem = PR_2_BAD_BLOCK_DEV;
1780 else if (LINUX_S_ISFIFO(inode.i_mode)
1781 && !e2fsck_pass1_check_device_inode(fs, &inode))
1782 problem = PR_2_BAD_FIFO;
1783 else if (LINUX_S_ISSOCK(inode.i_mode)
1784 && !e2fsck_pass1_check_device_inode(fs, &inode))
1785 problem = PR_2_BAD_SOCKET;
1786 else if (LINUX_S_ISLNK(inode.i_mode)
1787 && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) {
1788 problem = PR_2_INVALID_SYMLINK;
1792 if (fix_problem(ctx, problem, &pctx)) {
1793 deallocate_inode(ctx, ino, 0);
1794 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1802 if (inode.i_faddr) {
1803 if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1810 switch (fs->super->s_creator_os) {
1812 frag = &inode.osd2.hurd2.h_i_frag;
1813 fsize = &inode.osd2.hurd2.h_i_fsize;
1818 if (frag && *frag) {
1820 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
1827 if (fsize && *fsize) {
1829 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
1837 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1838 !ext2fs_has_feature_huge_file(fs->super) &&
1839 (inode.osd2.linux2.l_i_blocks_hi != 0)) {
1840 pctx.num = inode.osd2.linux2.l_i_blocks_hi;
1841 if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) {
1842 inode.osd2.linux2.l_i_blocks_hi = 0;
1847 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1848 !ext2fs_has_feature_64bit(fs->super) &&
1849 inode.osd2.linux2.l_i_file_acl_high != 0) {
1850 pctx.num = inode.osd2.linux2.l_i_file_acl_high;
1851 if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) {
1852 inode.osd2.linux2.l_i_file_acl_high = 0;
1858 if (ext2fs_file_acl_block(fs, &inode) &&
1859 ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) ||
1860 (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) {
1861 if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
1862 ext2fs_file_acl_block_set(fs, &inode, 0);
1867 if (inode.i_size_high && !ext2fs_has_feature_largedir(fs->super) &&
1868 LINUX_S_ISDIR(inode.i_mode)) {
1869 if (fix_problem(ctx, PR_2_DIR_SIZE_HIGH_ZERO, &pctx)) {
1870 inode.i_size_high = 0;
1877 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
1878 if (!not_fixed && ctx->inode_bad_map)
1879 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
1884 * allocate_dir_block --- this function allocates a new directory
1885 * block for a particular inode; this is done if a directory has
1886 * a "hole" in it, or if a directory has a illegal block number
1887 * that was zeroed out and now needs to be replaced.
1889 static int allocate_dir_block(e2fsck_t ctx,
1890 struct ext2_db_entry2 *db,
1891 char *buf EXT2FS_ATTR((unused)),
1892 struct problem_context *pctx)
1894 ext2_filsys fs = ctx->fs;
1897 struct ext2_inode inode;
1899 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
1903 * Read the inode and block bitmaps in; we'll be messing with
1906 e2fsck_read_bitmaps(ctx);
1909 * First, find a free block
1911 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1912 pctx->errcode = ext2fs_map_cluster_block(fs, db->ino, &inode,
1913 db->blockcnt, &blk);
1914 if (pctx->errcode || blk == 0) {
1915 blk = ext2fs_find_inode_goal(fs, db->ino, &inode, db->blockcnt);
1916 pctx->errcode = ext2fs_new_block2(fs, blk,
1917 ctx->block_found_map, &blk);
1918 if (pctx->errcode) {
1919 pctx->str = "ext2fs_new_block";
1920 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1924 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1925 ext2fs_mark_block_bitmap2(fs->block_map, blk);
1926 ext2fs_mark_bb_dirty(fs);
1929 * Now let's create the actual data block for the inode
1932 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
1934 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1935 EXT2_ROOT_INO, &block);
1937 if (pctx->errcode) {
1938 pctx->str = "ext2fs_new_dir_block";
1939 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1943 pctx->errcode = ext2fs_write_dir_block4(fs, blk, block, 0, db->ino);
1944 ext2fs_free_mem(&block);
1945 if (pctx->errcode) {
1946 pctx->str = "ext2fs_write_dir_block";
1947 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1952 * Update the inode block count
1954 ext2fs_iblk_add_blocks(fs, &inode, 1);
1955 if (EXT2_I_SIZE(&inode) < ((__u64) db->blockcnt+1) * fs->blocksize) {
1956 pctx->errcode = ext2fs_inode_size_set(fs, &inode,
1957 (db->blockcnt+1) * fs->blocksize);
1958 if (pctx->errcode) {
1959 pctx->str = "ext2fs_inode_size_set";
1960 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1964 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
1967 * Finally, update the block pointers for the inode
1970 pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET,
1971 db->blockcnt, 0, &blk);
1972 if (pctx->errcode) {
1973 pctx->str = "ext2fs_block_iterate";
1974 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);