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_block(ext2_filsys fs,
65 struct ext2_db_entry2 *dir_blocks_info,
67 static int allocate_dir_block(e2fsck_t ctx,
68 struct ext2_db_entry2 *dir_blocks_info,
69 char *buf, struct problem_context *pctx);
70 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino);
71 static int htree_depth(struct dx_dir_info *dx_dir,
72 struct dx_dirblock_info *dx_db);
73 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b);
75 struct check_dir_struct {
77 struct problem_context pctx;
82 void e2fsck_pass2(e2fsck_t ctx)
84 struct ext2_super_block *sb = ctx->fs->super;
85 struct problem_context pctx;
86 ext2_filsys fs = ctx->fs;
89 struct resource_track rtrack;
91 struct check_dir_struct cd;
92 struct dx_dir_info *dx_dir;
93 struct dx_dirblock_info *dx_db, *dx_parent;
94 unsigned int save_type;
100 init_resource_track(&rtrack, ctx->fs->io);
101 clear_problem_context(&cd.pctx);
104 mtrace_print("Pass 2");
107 if (!(ctx->options & E2F_OPT_PREEN))
108 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
110 e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT,
112 if (ctx->inode_count)
115 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
116 "inode_count", &save_type);
117 cd.pctx.errcode = ext2fs_create_icount2(fs,
118 EXT2_ICOUNT_OPT_INCREMENT,
119 0, ctx->inode_link_info,
121 fs->default_bitmap_type = save_type;
123 if (cd.pctx.errcode) {
124 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
125 ctx->flags |= E2F_FLAG_ABORT;
128 buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
129 "directory scan buffer");
132 * Set up the parent pointer for the root directory, if
133 * present. (If the root directory is not present, we will
134 * create it in pass 3.)
136 (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
141 cd.max = ext2fs_dblist_count2(fs->dblist);
144 (void) (ctx->progress)(ctx, 2, 0, cd.max);
146 if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX)
147 ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp);
149 cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_block,
151 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
154 if (ctx->flags & E2F_FLAG_RESTART_LATER) {
155 ctx->flags |= E2F_FLAG_RESTART;
159 if (cd.pctx.errcode) {
160 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
161 ctx->flags |= E2F_FLAG_ABORT;
166 for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
167 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
169 if (dx_dir->numblocks == 0)
171 clear_problem_context(&pctx);
173 pctx.dir = dx_dir->ino;
174 dx_db = dx_dir->dx_block;
175 if (dx_db->flags & DX_FLAG_REFERENCED)
176 dx_db->flags |= DX_FLAG_DUP_REF;
178 dx_db->flags |= DX_FLAG_REFERENCED;
180 * Find all of the first and last leaf blocks, and
181 * update their parent's min and max hash values
183 for (b=0, dx_db = dx_dir->dx_block;
184 b < dx_dir->numblocks;
186 if ((dx_db->type != DX_DIRBLOCK_LEAF) ||
187 !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST)))
189 dx_parent = &dx_dir->dx_block[dx_db->parent];
191 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
193 if (dx_db->flags & DX_FLAG_FIRST)
194 dx_parent->min_hash = dx_db->min_hash;
196 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
198 if (dx_db->flags & DX_FLAG_LAST)
199 dx_parent->max_hash = dx_db->max_hash;
202 for (b=0, dx_db = dx_dir->dx_block;
203 b < dx_dir->numblocks;
206 pctx.group = dx_db->parent;
208 if (!(dx_db->flags & DX_FLAG_FIRST) &&
209 (dx_db->min_hash < dx_db->node_min_hash)) {
210 pctx.blk = dx_db->min_hash;
211 pctx.blk2 = dx_db->node_min_hash;
212 code = PR_2_HTREE_MIN_HASH;
213 fix_problem(ctx, code, &pctx);
216 if (dx_db->type == DX_DIRBLOCK_LEAF) {
217 depth = htree_depth(dx_dir, dx_db);
218 if (depth != dx_dir->depth) {
219 pctx.num = dx_dir->depth;
220 code = PR_2_HTREE_BAD_DEPTH;
221 fix_problem(ctx, code, &pctx);
226 * This test doesn't apply for the root block
230 (dx_db->max_hash > dx_db->node_max_hash)) {
231 pctx.blk = dx_db->max_hash;
232 pctx.blk2 = dx_db->node_max_hash;
233 code = PR_2_HTREE_MAX_HASH;
234 fix_problem(ctx, code, &pctx);
237 if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
238 code = PR_2_HTREE_NOTREF;
239 fix_problem(ctx, code, &pctx);
241 } else if (dx_db->flags & DX_FLAG_DUP_REF) {
242 code = PR_2_HTREE_DUPREF;
243 fix_problem(ctx, code, &pctx);
247 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
248 clear_htree(ctx, dx_dir->ino);
249 dx_dir->numblocks = 0;
252 e2fsck_free_dx_dir_info(ctx);
254 ext2fs_free_mem(&buf);
255 ext2fs_free_dblist(fs->dblist);
257 if (ctx->inode_bad_map) {
258 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
259 ctx->inode_bad_map = 0;
261 if (ctx->inode_reg_map) {
262 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
263 ctx->inode_reg_map = 0;
266 clear_problem_context(&pctx);
267 if (ctx->large_files) {
268 if (!(sb->s_feature_ro_compat &
269 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
270 fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
271 sb->s_feature_ro_compat |=
272 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
273 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
274 ext2fs_mark_super_dirty(fs);
276 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
277 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
278 ext2fs_update_dynamic_rev(fs);
279 ext2fs_mark_super_dirty(fs);
283 print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io);
286 #define MAX_DEPTH 32000
287 static int htree_depth(struct dx_dir_info *dx_dir,
288 struct dx_dirblock_info *dx_db)
292 while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
293 dx_db = &dx_dir->dx_block[dx_db->parent];
299 static int dict_de_cmp(const void *a, const void *b)
301 const struct ext2_dir_entry *de_a, *de_b;
304 de_a = (const struct ext2_dir_entry *) a;
305 a_len = ext2fs_dirent_name_len(de_a);
306 de_b = (const struct ext2_dir_entry *) b;
307 b_len = ext2fs_dirent_name_len(de_b);
310 return (a_len - b_len);
312 return strncmp(de_a->name, de_b->name, a_len);
316 * This is special sort function that makes sure that directory blocks
317 * with a dirblock of zero are sorted to the beginning of the list.
318 * This guarantees that the root node of the htree directories are
319 * processed first, so we know what hash version to use.
321 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
323 const struct ext2_db_entry2 *db_a =
324 (const struct ext2_db_entry2 *) a;
325 const struct ext2_db_entry2 *db_b =
326 (const struct ext2_db_entry2 *) b;
328 if (db_a->blockcnt && !db_b->blockcnt)
331 if (!db_a->blockcnt && db_b->blockcnt)
334 if (db_a->blk != db_b->blk)
335 return (int) (db_a->blk - db_b->blk);
337 if (db_a->ino != db_b->ino)
338 return (int) (db_a->ino - db_b->ino);
340 return (int) (db_a->blockcnt - db_b->blockcnt);
345 * Make sure the first entry in the directory is '.', and that the
346 * directory entry is sane.
348 static int check_dot(e2fsck_t ctx,
349 struct ext2_dir_entry *dirent,
350 ext2_ino_t ino, struct problem_context *pctx)
352 struct ext2_dir_entry *nextdir;
353 unsigned int rec_len, new_len;
356 problem_t problem = 0;
359 problem = PR_2_MISSING_DOT;
360 else if ((ext2fs_dirent_name_len(dirent) != 1) ||
361 (dirent->name[0] != '.'))
362 problem = PR_2_1ST_NOT_DOT;
363 else if (dirent->name[1] != '\0')
364 problem = PR_2_DOT_NULL_TERM;
366 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
368 if (fix_problem(ctx, problem, pctx)) {
370 rec_len = dirent->rec_len = 12;
372 ext2fs_dirent_set_name_len(dirent, 1);
373 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
374 dirent->name[0] = '.';
375 dirent->name[1] = '\0';
380 if (dirent->inode != ino) {
381 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
387 new_len = rec_len - 12;
390 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
391 nextdir = (struct ext2_dir_entry *)
392 ((char *) dirent + 12);
393 dirent->rec_len = 12;
394 (void) ext2fs_set_rec_len(ctx->fs, new_len,
397 ext2fs_dirent_set_name_len(nextdir, 0);
398 ext2fs_dirent_set_file_type(nextdir,
408 * Make sure the second entry in the directory is '..', and that the
409 * directory entry is sane. We do not check the inode number of '..'
410 * here; this gets done in pass 3.
412 static int check_dotdot(e2fsck_t ctx,
413 struct ext2_dir_entry *dirent,
414 ext2_ino_t ino, struct problem_context *pctx)
416 problem_t problem = 0;
417 unsigned int rec_len;
420 problem = PR_2_MISSING_DOT_DOT;
421 else if ((ext2fs_dirent_name_len(dirent) != 2) ||
422 (dirent->name[0] != '.') ||
423 (dirent->name[1] != '.'))
424 problem = PR_2_2ND_NOT_DOT_DOT;
425 else if (dirent->name[2] != '\0')
426 problem = PR_2_DOT_DOT_NULL_TERM;
428 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
430 if (fix_problem(ctx, problem, pctx)) {
432 dirent->rec_len = 12;
434 * Note: we don't have the parent inode just
435 * yet, so we will fill it in with the root
436 * inode. This will get fixed in pass 3.
438 dirent->inode = EXT2_ROOT_INO;
439 ext2fs_dirent_set_name_len(dirent, 2);
440 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
441 dirent->name[0] = '.';
442 dirent->name[1] = '.';
443 dirent->name[2] = '\0';
448 if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) {
449 fix_problem(ctx, PR_2_NO_DIRINFO, pctx);
456 * Check to make sure a directory entry doesn't contain any illegal
459 static int check_name(e2fsck_t ctx,
460 struct ext2_dir_entry *dirent,
461 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
462 struct problem_context *pctx)
468 for ( i = 0; i < ext2fs_dirent_name_len(dirent); i++) {
469 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
471 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
474 dirent->name[i] = '.';
483 * Check the directory filetype (if present)
485 static _INLINE_ int check_filetype(e2fsck_t ctx,
486 struct ext2_dir_entry *dirent,
487 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
488 struct problem_context *pctx)
490 int filetype = ext2fs_dirent_file_type(dirent);
491 int should_be = EXT2_FT_UNKNOWN;
492 struct ext2_inode inode;
494 if (!(ctx->fs->super->s_feature_incompat &
495 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
497 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
499 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
503 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) {
504 should_be = EXT2_FT_DIR;
505 } else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map,
507 should_be = EXT2_FT_REG_FILE;
508 } else if (ctx->inode_bad_map &&
509 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
513 e2fsck_read_inode(ctx, dirent->inode, &inode,
515 should_be = ext2_file_type(inode.i_mode);
517 if (filetype == should_be)
519 pctx->num = should_be;
521 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
525 ext2fs_dirent_set_file_type(dirent, should_be);
530 static void parse_int_node(ext2_filsys fs,
531 struct ext2_db_entry2 *db,
532 struct check_dir_struct *cd,
533 struct dx_dir_info *dx_dir,
534 char *block_buf, int failed_csum)
536 struct ext2_dx_root_info *root;
537 struct ext2_dx_entry *ent;
538 struct ext2_dx_countlimit *limit;
539 struct dx_dirblock_info *dx_db;
540 int i, expect_limit, count;
542 ext2_dirhash_t min_hash = 0xffffffff;
543 ext2_dirhash_t max_hash = 0;
544 ext2_dirhash_t hash = 0, prev_hash;
547 if (db->blockcnt == 0) {
548 root = (struct ext2_dx_root_info *) (block_buf + 24);
551 printf("Root node dump:\n");
552 printf("\t Reserved zero: %u\n", root->reserved_zero);
553 printf("\t Hash Version: %d\n", root->hash_version);
554 printf("\t Info length: %d\n", root->info_length);
555 printf("\t Indirect levels: %d\n", root->indirect_levels);
556 printf("\t Flags: %d\n", root->unused_flags);
559 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
562 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
563 fix_problem(cd->ctx, PR_2_HTREE_ROOT_CSUM_INVALID,
567 ent = (struct ext2_dx_entry *) (block_buf+8);
570 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
571 fix_problem(cd->ctx, PR_2_HTREE_NODE_CSUM_INVALID,
576 limit = (struct ext2_dx_countlimit *) ent;
579 printf("Number of entries (count): %d\n",
580 ext2fs_le16_to_cpu(limit->count));
581 printf("Number of entries (limit): %d\n",
582 ext2fs_le16_to_cpu(limit->limit));
585 count = ext2fs_le16_to_cpu(limit->count);
586 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
587 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
588 csum_size = sizeof(struct ext2_dx_tail);
589 expect_limit = (fs->blocksize -
590 (csum_size + ((char *) ent - block_buf))) /
591 sizeof(struct ext2_dx_entry);
592 if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
593 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
594 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
597 if (count > expect_limit) {
598 cd->pctx.num = count;
599 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
601 count = expect_limit;
604 for (i=0; i < count; i++) {
606 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
608 printf("Entry #%d: Hash 0x%08x, block %u\n", i,
609 hash, ext2fs_le32_to_cpu(ent[i].block));
611 blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff;
612 /* Check to make sure the block is valid */
613 if (blk >= (blk_t) dx_dir->numblocks) {
615 if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
620 if (hash < prev_hash &&
621 fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
623 dx_db = &dx_dir->dx_block[blk];
624 if (dx_db->flags & DX_FLAG_REFERENCED) {
625 dx_db->flags |= DX_FLAG_DUP_REF;
627 dx_db->flags |= DX_FLAG_REFERENCED;
628 dx_db->parent = db->blockcnt;
634 dx_db->node_min_hash = hash;
636 dx_db->node_max_hash =
637 ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
639 dx_db->node_max_hash = 0xfffffffe;
640 dx_db->flags |= DX_FLAG_LAST;
643 dx_db->flags |= DX_FLAG_FIRST;
646 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
647 db->blockcnt, min_hash, max_hash);
649 dx_db = &dx_dir->dx_block[db->blockcnt];
650 dx_db->min_hash = min_hash;
651 dx_db->max_hash = max_hash;
655 clear_htree(cd->ctx, cd->pctx.ino);
656 dx_dir->numblocks = 0;
657 e2fsck_rehash_dir_later(cd->ctx, cd->pctx.ino);
659 #endif /* ENABLE_HTREE */
662 * Given a busted directory, try to salvage it somehow.
665 static void salvage_directory(ext2_filsys fs,
666 struct ext2_dir_entry *dirent,
667 struct ext2_dir_entry *prev,
668 unsigned int *offset)
670 char *cp = (char *) dirent;
672 unsigned int rec_len, prev_rec_len;
673 unsigned int name_len = ext2fs_dirent_name_len(dirent);
675 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
676 left = fs->blocksize - *offset - rec_len;
679 * Special case of directory entry of size 8: copy what's left
680 * of the directory block up to cover up the invalid hole.
682 if ((left >= 12) && (rec_len == 8)) {
683 memmove(cp, cp+8, left);
684 memset(cp + left, 0, 8);
688 * If the directory entry overruns the end of the directory
689 * block, and the name is small enough to fit, then adjust the
693 ((int) rec_len + left > 8) &&
694 ((int) name_len + 8 <= (int) rec_len + left) &&
695 dirent->inode <= fs->super->s_inodes_count &&
696 strnlen(dirent->name, name_len) == name_len) {
697 (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent);
701 * If the record length of the directory entry is a multiple
702 * of four, and not too big, such that it is valid, let the
703 * previous directory entry absorb the invalid one.
705 if (prev && rec_len && (rec_len % 4) == 0 &&
706 (*offset + rec_len <= fs->blocksize)) {
707 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
708 prev_rec_len += rec_len;
709 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
714 * Default salvage method --- kill all of the directory
715 * entries for the rest of the block. We will either try to
716 * absorb it into the previous directory entry, or create a
717 * new empty directory entry the rest of the directory block.
720 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
721 prev_rec_len += fs->blocksize - *offset;
722 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
723 *offset = fs->blocksize;
725 rec_len = fs->blocksize - *offset;
726 (void) ext2fs_set_rec_len(fs, rec_len, dirent);
727 ext2fs_dirent_set_name_len(dirent, 0);
728 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
733 static int is_last_entry(ext2_filsys fs, int inline_data_size,
734 unsigned int offset, int csum_size)
736 if (inline_data_size)
737 return (offset < inline_data_size);
739 return (offset < fs->blocksize - csum_size);
742 static errcode_t insert_dirent_tail(ext2_filsys fs, void *dirbuf)
744 struct ext2_dir_entry *d;
746 struct ext2_dir_entry_tail *t;
747 unsigned int rec_len;
750 top = EXT2_DIRENT_TAIL(dirbuf, fs->blocksize);
752 rec_len = d->rec_len;
753 while (rec_len && !(rec_len & 0x3)) {
754 d = (struct ext2_dir_entry *)(((char *)d) + rec_len);
755 if (((void *)d) + d->rec_len >= top)
757 rec_len = d->rec_len;
761 size_t min_size = EXT2_DIR_REC_LEN(
762 ext2fs_dirent_name_len(dirbuf));
763 if (min_size > d->rec_len - sizeof(struct ext2_dir_entry_tail))
764 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
765 d->rec_len -= sizeof(struct ext2_dir_entry_tail);
768 t = (struct ext2_dir_entry_tail *)top;
769 if (t->det_reserved_zero1 ||
770 t->det_rec_len != sizeof(struct ext2_dir_entry_tail) ||
771 t->det_reserved_name_len != EXT2_DIR_NAME_LEN_CSUM)
772 ext2fs_initialize_dirent_tail(fs, t);
777 static int check_dir_block(ext2_filsys fs,
778 struct ext2_db_entry2 *db,
781 struct dx_dir_info *dx_dir;
783 struct dx_dirblock_info *dx_db = 0;
784 #endif /* ENABLE_HTREE */
785 struct ext2_dir_entry *dirent, *prev, dot, dotdot;
787 unsigned int offset = 0;
788 int dir_modified = 0;
790 unsigned int rec_len;
791 blk64_t block_nr = db->blk;
792 ext2_ino_t ino = db->ino;
793 ext2_ino_t subdir_parent;
795 struct check_dir_struct *cd;
799 struct ext2_dx_root_info *root;
800 struct ext2_dx_countlimit *limit;
801 static dict_t de_dict;
802 struct problem_context pctx;
805 int dx_csum_size = 0, de_csum_size = 0;
808 size_t inline_data_size = 0;
811 cd = (struct check_dir_struct *) priv_data;
815 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
818 if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
821 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
822 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
823 dx_csum_size = sizeof(struct ext2_dx_tail);
824 de_csum_size = sizeof(struct ext2_dir_entry_tail);
827 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
828 EXT2_FEATURE_INCOMPAT_FILETYPE))
829 filetype = EXT2_FT_DIR << 8;
832 * Make sure the inode is still in use (could have been
833 * deleted in the duplicate/bad blocks pass.
835 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino)))
839 cd->pctx.blk = block_nr;
840 cd->pctx.blkcount = db->blockcnt;
845 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
846 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
849 ec = ext2fs_inline_data_size(fs, ino, &inline_data_size);
850 if (ec && ec != EXT2_ET_NO_INLINE_DATA)
854 if (db->blk == 0 && !inline_data_size) {
855 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
865 if (ctx->dirs_to_hash &&
866 ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
870 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
874 ehandler_operation(_("reading directory block"));
875 if (inline_data_size)
876 cd->pctx.errcode = ext2fs_inline_data_get(fs, ino, 0, buf, 0);
878 cd->pctx.errcode = ext2fs_read_dir_block4(fs, block_nr,
880 ehandler_operation(0);
881 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
882 cd->pctx.errcode = 0; /* We'll handle this ourselves */
883 else if (cd->pctx.errcode == EXT2_ET_DIR_CSUM_INVALID) {
884 cd->pctx.errcode = 0; /* We'll handle this ourselves */
887 if (cd->pctx.errcode) {
889 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
890 ctx->flags |= E2F_FLAG_ABORT;
893 ext2fs_new_dir_block(fs, db->blockcnt == 0 ? ino : 0,
894 EXT2_ROOT_INO, &buf2);
895 memcpy(buf, buf2, fs->blocksize);
896 ext2fs_free_mem(&buf2);
899 dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
900 if (dx_dir && dx_dir->numblocks) {
901 if (db->blockcnt >= dx_dir->numblocks) {
902 if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK,
904 clear_htree(ctx, ino);
905 dx_dir->numblocks = 0;
909 fatal_error(ctx, _("Can not continue."));
911 dx_db = &dx_dir->dx_block[db->blockcnt];
912 dx_db->type = DX_DIRBLOCK_LEAF;
913 dx_db->phys = block_nr;
914 dx_db->min_hash = ~0;
917 dirent = (struct ext2_dir_entry *) buf;
918 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
919 limit = (struct ext2_dx_countlimit *) (buf+8);
920 if (db->blockcnt == 0) {
921 root = (struct ext2_dx_root_info *) (buf + 24);
922 dx_db->type = DX_DIRBLOCK_ROOT;
923 dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
924 if ((root->reserved_zero ||
925 root->info_length < 8 ||
926 root->indirect_levels > 1) &&
927 fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
928 clear_htree(ctx, ino);
929 dx_dir->numblocks = 0;
932 dx_dir->hashversion = root->hash_version;
933 if ((dx_dir->hashversion <= EXT2_HASH_TEA) &&
934 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
935 dx_dir->hashversion += 3;
936 dx_dir->depth = root->indirect_levels + 1;
937 } else if ((dirent->inode == 0) &&
938 (rec_len == fs->blocksize) &&
939 (ext2fs_dirent_name_len(dirent) == 0) &&
940 (ext2fs_le16_to_cpu(limit->limit) ==
941 ((fs->blocksize - (8 + dx_csum_size)) /
942 sizeof(struct ext2_dx_entry))))
943 dx_db->type = DX_DIRBLOCK_NODE;
947 #endif /* ENABLE_HTREE */
949 /* Verify checksum. */
950 if (is_leaf && de_csum_size && !inline_data_size) {
951 /* No space for csum? Rebuild dirs in pass 3A. */
952 if (!ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
954 if (e2fsck_dir_will_be_rehashed(ctx, ino))
956 if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_MISSING_CSUM,
959 e2fsck_rehash_dir_later(ctx, ino);
964 if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_CSUM_INVALID,
967 ext2fs_new_dir_block(fs,
968 db->blockcnt == 0 ? ino : 0,
969 EXT2_ROOT_INO, &buf2);
970 memcpy(buf, buf2, fs->blocksize);
971 ext2fs_free_mem(&buf2);
976 /* htree nodes don't use fake dirents to store checksums */
981 dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
985 ext2_ino_t first_unused_inode;
986 unsigned int name_len;
989 if (!inline_data_size || dot_state > 1) {
990 dirent = (struct ext2_dir_entry *) (buf + offset);
991 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
992 cd->pctx.dirent = dirent;
993 cd->pctx.num = offset;
994 if (((offset + rec_len) > fs->blocksize) ||
996 ((rec_len % 4) != 0) ||
997 ((ext2fs_dirent_name_len(dirent) + 8) > rec_len)) {
998 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
999 salvage_directory(fs, dirent, prev, &offset);
1003 goto abort_free_dict;
1006 if (dot_state == 0) {
1007 memset(&dot, 0, sizeof(dot));
1009 dirent->inode = ino;
1010 dirent->rec_len = EXT2_DIR_REC_LEN(1);
1011 dirent->name_len = 1 | filetype;
1012 dirent->name[0] = '.';
1013 } else if (dot_state == 1) {
1014 memset(&dotdot, 0, sizeof(dotdot));
1017 ((struct ext2_dir_entry *)buf)->inode;
1018 dirent->rec_len = EXT2_DIR_REC_LEN(2);
1019 dirent->name_len = 2 | filetype;
1020 dirent->name[0] = '.';
1021 dirent->name[1] = '.';
1023 fatal_error(ctx, _("Can not continue."));
1025 cd->pctx.dirent = dirent;
1026 cd->pctx.num = offset;
1029 if (dot_state == 0) {
1030 if (check_dot(ctx, dirent, ino, &cd->pctx))
1032 } else if (dot_state == 1) {
1033 ret = check_dotdot(ctx, dirent, ino, &cd->pctx);
1035 goto abort_free_dict;
1038 } else if (dirent->inode == ino) {
1039 problem = PR_2_LINK_DOT;
1040 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
1050 * Make sure the inode listed is a legal one.
1052 name_len = ext2fs_dirent_name_len(dirent);
1053 if (((dirent->inode != EXT2_ROOT_INO) &&
1054 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
1055 (dirent->inode > fs->super->s_inodes_count)) {
1056 problem = PR_2_BAD_INO;
1057 } else if (ctx->inode_bb_map &&
1058 (ext2fs_test_inode_bitmap2(ctx->inode_bb_map,
1061 * If the inode is in a bad block, offer to
1064 problem = PR_2_BB_INODE;
1065 } else if ((dot_state > 1) && (name_len == 1) &&
1066 (dirent->name[0] == '.')) {
1068 * If there's a '.' entry in anything other
1069 * than the first directory entry, it's a
1070 * duplicate entry that should be removed.
1072 problem = PR_2_DUP_DOT;
1073 } else if ((dot_state > 1) && (name_len == 2) &&
1074 (dirent->name[0] == '.') &&
1075 (dirent->name[1] == '.')) {
1077 * If there's a '..' entry in anything other
1078 * than the second directory entry, it's a
1079 * duplicate entry that should be removed.
1081 problem = PR_2_DUP_DOT_DOT;
1082 } else if ((dot_state > 1) &&
1083 (dirent->inode == EXT2_ROOT_INO)) {
1085 * Don't allow links to the root directory.
1086 * We check this specially to make sure we
1087 * catch this error case even if the root
1088 * directory hasn't been created yet.
1090 problem = PR_2_LINK_ROOT;
1091 } else if ((dot_state > 1) && (name_len == 0)) {
1093 * Don't allow zero-length directory names.
1095 problem = PR_2_NULL_NAME;
1099 if (fix_problem(ctx, problem, &cd->pctx)) {
1104 ext2fs_unmark_valid(fs);
1105 if (problem == PR_2_BAD_INO)
1111 * If the inode was marked as having bad fields in
1112 * pass1, process it and offer to fix/clear it.
1113 * (We wait until now so that we can display the
1114 * pathname to the user.)
1116 if (ctx->inode_bad_map &&
1117 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
1119 if (e2fsck_process_bad_inode(ctx, ino,
1121 buf + fs->blocksize)) {
1126 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1127 return DIRENT_ABORT;
1130 group = ext2fs_group_of_ino(fs, dirent->inode);
1131 first_unused_inode = group * fs->super->s_inodes_per_group +
1132 1 + fs->super->s_inodes_per_group -
1133 ext2fs_bg_itable_unused(fs, group);
1134 cd->pctx.group = group;
1137 * Check if the inode was missed out because
1138 * _INODE_UNINIT flag was set or bg_itable_unused was
1139 * incorrect. If so, clear the _INODE_UNINIT flag and
1140 * restart e2fsck. In the future it would be nice if
1141 * we could call a function in pass1.c that checks the
1142 * newly visible inodes.
1144 if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) {
1145 pctx.num = dirent->inode;
1146 if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT,
1148 ext2fs_bg_flags_clear(fs, group,
1149 EXT2_BG_INODE_UNINIT);
1150 ext2fs_mark_super_dirty(fs);
1151 ctx->flags |= E2F_FLAG_RESTART_LATER;
1153 ext2fs_unmark_valid(fs);
1154 if (problem == PR_2_BAD_INO)
1157 } else if (dirent->inode >= first_unused_inode) {
1158 pctx.num = dirent->inode;
1159 if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){
1160 ext2fs_bg_itable_unused_set(fs, group, 0);
1161 ext2fs_mark_super_dirty(fs);
1162 ctx->flags |= E2F_FLAG_RESTART_LATER;
1164 ext2fs_unmark_valid(fs);
1165 if (problem == PR_2_BAD_INO)
1171 * Offer to clear unused inodes; if we are going to be
1172 * restarting the scan due to bg_itable_unused being
1173 * wrong, then don't clear any inodes to avoid zapping
1174 * inodes that were skipped during pass1 due to an
1175 * incorrect bg_itable_unused; we'll get any real
1176 * problems after we restart.
1178 if (!(ctx->flags & E2F_FLAG_RESTART_LATER) &&
1179 !(ext2fs_test_inode_bitmap2(ctx->inode_used_map,
1181 problem = PR_2_UNUSED_INODE;
1184 if (fix_problem(ctx, problem, &cd->pctx)) {
1189 ext2fs_unmark_valid(fs);
1190 if (problem == PR_2_BAD_INO)
1195 if (check_name(ctx, dirent, ino, &cd->pctx))
1198 if (check_filetype(ctx, dirent, ino, &cd->pctx))
1203 ext2fs_dirhash(dx_dir->hashversion, dirent->name,
1204 ext2fs_dirent_name_len(dirent),
1205 fs->super->s_hash_seed, &hash, 0);
1206 if (hash < dx_db->min_hash)
1207 dx_db->min_hash = hash;
1208 if (hash > dx_db->max_hash)
1209 dx_db->max_hash = hash;
1214 * If this is a directory, then mark its parent in its
1215 * dir_info structure. If the parent field is already
1216 * filled in, then this directory has more than one
1217 * hard link. We assume the first link is correct,
1218 * and ask the user if he/she wants to clear this one.
1220 if ((dot_state > 1) &&
1221 (ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
1223 if (e2fsck_dir_info_get_parent(ctx, dirent->inode,
1225 cd->pctx.ino = dirent->inode;
1226 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
1227 goto abort_free_dict;
1229 if (subdir_parent) {
1230 cd->pctx.ino2 = subdir_parent;
1231 if (fix_problem(ctx, PR_2_LINK_DIR,
1239 (void) e2fsck_dir_info_set_parent(ctx,
1240 dirent->inode, ino);
1246 } else if (dict_lookup(&de_dict, dirent)) {
1247 clear_problem_context(&pctx);
1249 pctx.dirent = dirent;
1250 fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
1251 e2fsck_rehash_dir_later(ctx, ino);
1254 dict_alloc_insert(&de_dict, dirent, dirent);
1256 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1259 ctx->fs_links_count++;
1260 ctx->fs_total_count++;
1264 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1265 if (!inline_data_size || dot_state > 1) {
1272 } while (is_last_entry(fs, inline_data_size, offset, de_csum_size));
1279 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1280 db->blockcnt, dx_db->type,
1281 dx_db->min_hash, dx_db->max_hash);
1283 cd->pctx.dir = cd->pctx.ino;
1284 if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1285 (dx_db->type == DX_DIRBLOCK_NODE))
1286 parse_int_node(fs, db, cd, dx_dir, buf, failed_csum);
1288 #endif /* ENABLE_HTREE */
1290 if (inline_data_size) {
1291 if (offset != inline_data_size) {
1292 cd->pctx.num = rec_len + offset - inline_data_size;
1293 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1294 dirent->rec_len = cd->pctx.num;
1299 if (offset != fs->blocksize - de_csum_size) {
1300 cd->pctx.num = rec_len - (fs->blocksize - de_csum_size) +
1302 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1303 dirent->rec_len = cd->pctx.num;
1309 /* leaf block with no tail? Rehash dirs later. */
1310 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
1311 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
1313 !inline_data_size &&
1314 !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
1315 if (insert_dirent_tail(fs, buf) == 0)
1317 e2fsck_rehash_dir_later(ctx, ino);
1321 if (e2fsck_dir_will_be_rehashed(ctx, ino))
1322 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1323 if (inline_data_size) {
1325 ext2fs_inline_data_set(fs, ino, 0, buf,
1328 cd->pctx.errcode = ext2fs_write_dir_block4(fs, block_nr,
1330 if (e2fsck_dir_will_be_rehashed(ctx, ino))
1331 ctx->fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1332 if (cd->pctx.errcode) {
1333 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
1335 goto abort_free_dict;
1337 ext2fs_mark_changed(fs);
1338 } else if (is_leaf && failed_csum && !dir_modified) {
1340 * If a leaf node that fails csum makes it this far without
1341 * alteration, ask the user if the checksum should be fixed.
1343 if (fix_problem(ctx, PR_2_LEAF_NODE_ONLY_CSUM_INVALID,
1347 dict_free_nodes(&de_dict);
1350 ctx->flags |= E2F_FLAG_ABORT;
1351 dict_free_nodes(&de_dict);
1352 return DIRENT_ABORT;
1361 * This function is called to deallocate a block, and is an interator
1362 * functioned called by deallocate inode via ext2fs_iterate_block().
1364 static int deallocate_inode_block(ext2_filsys fs,
1366 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1367 blk64_t ref_block EXT2FS_ATTR((unused)),
1368 int ref_offset EXT2FS_ATTR((unused)),
1371 struct del_block *p = priv_data;
1373 if (HOLE_BLKADDR(*block_nr))
1375 if ((*block_nr < fs->super->s_first_data_block) ||
1376 (*block_nr >= ext2fs_blocks_count(fs->super)))
1378 if ((*block_nr % EXT2FS_CLUSTER_RATIO(fs)) == 0)
1379 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
1385 * This fuction deallocates an inode
1387 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
1389 ext2_filsys fs = ctx->fs;
1390 struct ext2_inode inode;
1391 struct problem_context pctx;
1393 struct del_block del_block;
1395 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1396 clear_problem_context(&pctx);
1400 * Fix up the bitmaps...
1402 e2fsck_read_bitmaps(ctx);
1403 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1405 if (ext2fs_file_acl_block(fs, &inode) &&
1406 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1407 pctx.errcode = ext2fs_adjust_ea_refcount3(fs,
1408 ext2fs_file_acl_block(fs, &inode),
1409 block_buf, -1, &count, ino);
1410 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1415 pctx.blk = ext2fs_file_acl_block(fs, &inode);
1416 fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1417 ctx->flags |= E2F_FLAG_ABORT;
1421 ext2fs_block_alloc_stats2(fs,
1422 ext2fs_file_acl_block(fs, &inode), -1);
1424 ext2fs_file_acl_block_set(fs, &inode, 0);
1427 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1430 if (LINUX_S_ISREG(inode.i_mode) &&
1431 ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode)))
1434 del_block.ctx = ctx;
1436 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1437 deallocate_inode_block,
1440 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
1441 ctx->flags |= E2F_FLAG_ABORT;
1445 /* Inode may have changed by block_iterate, so reread it */
1446 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1447 e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
1451 * This fuction clears the htree flag on an inode
1453 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1455 struct ext2_inode inode;
1457 e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1458 inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1459 e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
1460 if (ctx->dirs_to_hash)
1461 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1465 int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1466 ext2_ino_t ino, char *buf)
1468 ext2_filsys fs = ctx->fs;
1469 struct ext2_inode inode;
1470 int inode_modified = 0;
1472 unsigned char *frag, *fsize;
1473 struct problem_context pctx;
1474 problem_t problem = 0;
1476 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
1478 clear_problem_context(&pctx);
1481 pctx.inode = &inode;
1483 if (ext2fs_file_acl_block(fs, &inode) &&
1484 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1485 if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
1486 ext2fs_file_acl_block_set(fs, &inode, 0);
1492 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1493 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1494 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
1495 !(LINUX_S_ISSOCK(inode.i_mode)))
1496 problem = PR_2_BAD_MODE;
1497 else if (LINUX_S_ISCHR(inode.i_mode)
1498 && !e2fsck_pass1_check_device_inode(fs, &inode))
1499 problem = PR_2_BAD_CHAR_DEV;
1500 else if (LINUX_S_ISBLK(inode.i_mode)
1501 && !e2fsck_pass1_check_device_inode(fs, &inode))
1502 problem = PR_2_BAD_BLOCK_DEV;
1503 else if (LINUX_S_ISFIFO(inode.i_mode)
1504 && !e2fsck_pass1_check_device_inode(fs, &inode))
1505 problem = PR_2_BAD_FIFO;
1506 else if (LINUX_S_ISSOCK(inode.i_mode)
1507 && !e2fsck_pass1_check_device_inode(fs, &inode))
1508 problem = PR_2_BAD_SOCKET;
1509 else if (LINUX_S_ISLNK(inode.i_mode)
1510 && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) {
1511 problem = PR_2_INVALID_SYMLINK;
1515 if (fix_problem(ctx, problem, &pctx)) {
1516 deallocate_inode(ctx, ino, 0);
1517 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1525 if (inode.i_faddr) {
1526 if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1533 switch (fs->super->s_creator_os) {
1535 frag = &inode.osd2.hurd2.h_i_frag;
1536 fsize = &inode.osd2.hurd2.h_i_fsize;
1541 if (frag && *frag) {
1543 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
1550 if (fsize && *fsize) {
1552 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
1560 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1561 !(fs->super->s_feature_ro_compat &
1562 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1563 (inode.osd2.linux2.l_i_blocks_hi != 0)) {
1564 pctx.num = inode.osd2.linux2.l_i_blocks_hi;
1565 if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) {
1566 inode.osd2.linux2.l_i_blocks_hi = 0;
1571 if (!(fs->super->s_feature_incompat &
1572 EXT4_FEATURE_INCOMPAT_64BIT) &&
1573 inode.osd2.linux2.l_i_file_acl_high != 0) {
1574 pctx.num = inode.osd2.linux2.l_i_file_acl_high;
1575 if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) {
1576 inode.osd2.linux2.l_i_file_acl_high = 0;
1582 if (ext2fs_file_acl_block(fs, &inode) &&
1583 ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) ||
1584 (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) {
1585 if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
1586 ext2fs_file_acl_block_set(fs, &inode, 0);
1591 if (inode.i_dir_acl &&
1592 LINUX_S_ISDIR(inode.i_mode)) {
1593 if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1594 inode.i_dir_acl = 0;
1601 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
1602 if (!not_fixed && ctx->inode_bad_map)
1603 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
1609 * allocate_dir_block --- this function allocates a new directory
1610 * block for a particular inode; this is done if a directory has
1611 * a "hole" in it, or if a directory has a illegal block number
1612 * that was zeroed out and now needs to be replaced.
1614 static int allocate_dir_block(e2fsck_t ctx,
1615 struct ext2_db_entry2 *db,
1616 char *buf EXT2FS_ATTR((unused)),
1617 struct problem_context *pctx)
1619 ext2_filsys fs = ctx->fs;
1622 struct ext2_inode inode;
1624 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
1628 * Read the inode and block bitmaps in; we'll be messing with
1631 e2fsck_read_bitmaps(ctx);
1634 * First, find a free block
1636 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1637 pctx->errcode = ext2fs_map_cluster_block(fs, db->ino, &inode,
1638 db->blockcnt, &blk);
1639 if (pctx->errcode || blk == 0) {
1640 pctx->errcode = ext2fs_new_block2(fs, 0,
1641 ctx->block_found_map, &blk);
1642 if (pctx->errcode) {
1643 pctx->str = "ext2fs_new_block";
1644 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1648 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1649 ext2fs_mark_block_bitmap2(fs->block_map, blk);
1650 ext2fs_mark_bb_dirty(fs);
1653 * Now let's create the actual data block for the inode
1656 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
1658 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1659 EXT2_ROOT_INO, &block);
1661 if (pctx->errcode) {
1662 pctx->str = "ext2fs_new_dir_block";
1663 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1667 pctx->errcode = ext2fs_write_dir_block4(fs, blk, block, 0, db->ino);
1668 ext2fs_free_mem(&block);
1669 if (pctx->errcode) {
1670 pctx->str = "ext2fs_write_dir_block";
1671 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1676 * Update the inode block count
1678 ext2fs_iblk_add_blocks(fs, &inode, 1);
1679 if (EXT2_I_SIZE(&inode) < (db->blockcnt+1) * fs->blocksize) {
1680 pctx->errcode = ext2fs_inode_size_set(fs, &inode,
1681 (db->blockcnt+1) * fs->blocksize);
1682 if (pctx->errcode) {
1683 pctx->str = "ext2fs_inode_size_set";
1684 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1688 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
1691 * Finally, update the block pointers for the inode
1694 pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET,
1695 db->blockcnt, 0, &blk);
1696 if (pctx->errcode) {
1697 pctx->str = "ext2fs_block_iterate";
1698 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);