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
47 #ifdef NO_INLINE_FUNCS
50 #define _INLINE_ inline
56 * Keeps track of how many times an inode is referenced.
58 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf);
59 static int check_dir_block(ext2_filsys fs,
60 struct ext2_db_entry *dir_blocks_info,
62 static int allocate_dir_block(e2fsck_t ctx,
63 struct ext2_db_entry *dir_blocks_info,
64 char *buf, struct problem_context *pctx);
65 static int update_dir_block(ext2_filsys fs,
71 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino);
73 struct check_dir_struct {
75 struct problem_context pctx;
80 void e2fsck_pass2(e2fsck_t ctx)
82 struct ext2_super_block *sb = ctx->fs->super;
83 struct problem_context pctx;
84 ext2_filsys fs = ctx->fs;
87 struct resource_track rtrack;
90 struct check_dir_struct cd;
91 struct dx_dir_info *dx_dir;
92 struct dx_dirblock_info *dx_db, *dx_parent;
99 init_resource_track(&rtrack);
102 clear_problem_context(&cd.pctx);
105 mtrace_print("Pass 2");
108 if (!(ctx->options & E2F_OPT_PREEN))
109 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
111 cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
112 0, ctx->inode_link_info,
114 if (cd.pctx.errcode) {
115 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
116 ctx->flags |= E2F_FLAG_ABORT;
119 buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
120 "directory scan buffer");
123 * Set up the parent pointer for the root directory, if
124 * present. (If the root directory is not present, we will
125 * create it in pass 3.)
127 dir = e2fsck_get_dir_info(ctx, EXT2_ROOT_INO);
129 dir->parent = EXT2_ROOT_INO;
134 cd.max = ext2fs_dblist_count(fs->dblist);
137 (void) (ctx->progress)(ctx, 2, 0, cd.max);
139 cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
141 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
143 if (cd.pctx.errcode) {
144 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
145 ctx->flags |= E2F_FLAG_ABORT;
150 for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
151 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
153 if (dx_dir->numblocks == 0)
155 clear_problem_context(&pctx);
157 pctx.dir = dx_dir->ino;
158 dx_db = dx_dir->dx_block;
159 if (dx_db->flags & DX_FLAG_REFERENCED)
160 dx_db->flags |= DX_FLAG_DUP_REF;
162 dx_db->flags |= DX_FLAG_REFERENCED;
164 * Find all of the first and last leaf blocks, and
165 * update their parent's min and max hash values
167 for (b=0, dx_db = dx_dir->dx_block;
168 b < dx_dir->numblocks;
170 if ((dx_db->type != DX_DIRBLOCK_LEAF) ||
171 !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST)))
173 dx_parent = &dx_dir->dx_block[dx_db->parent];
175 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
177 if (dx_db->flags & DX_FLAG_FIRST)
178 dx_parent->min_hash = dx_db->min_hash;
180 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
182 if (dx_db->flags & DX_FLAG_LAST)
183 dx_parent->max_hash = dx_db->max_hash;
186 for (b=0, dx_db = dx_dir->dx_block;
187 b < dx_dir->numblocks;
190 pctx.group = dx_db->parent;
192 if (!(dx_db->flags & DX_FLAG_FIRST) &&
193 (dx_db->min_hash < dx_db->node_min_hash)) {
194 pctx.blk = dx_db->min_hash;
195 pctx.blk2 = dx_db->node_min_hash;
196 code = PR_2_HTREE_MIN_HASH;
197 fix_problem(ctx, code, &pctx);
201 * This test doesn't apply for the root block
205 (dx_db->max_hash > dx_db->node_max_hash)) {
206 pctx.blk = dx_db->max_hash;
207 pctx.blk2 = dx_db->node_max_hash;
208 code = PR_2_HTREE_MAX_HASH;
209 fix_problem(ctx, code, &pctx);
212 if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
213 code = PR_2_HTREE_NOTREF;
214 fix_problem(ctx, code, &pctx);
216 } else if (dx_db->flags & DX_FLAG_DUP_REF) {
217 code = PR_2_HTREE_DUPREF;
218 fix_problem(ctx, code, &pctx);
224 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
225 clear_htree(ctx, dx_dir->ino);
226 dx_dir->numblocks = 0;
228 #ifdef ENABLE_HTREE_CLEAR
229 if (dx_dir->numblocks) {
230 fix_problem(ctx, PR_2_HTREE_FCLR, &pctx);
231 clear_htree(ctx, dx_dir->ino);
232 dx_dir->numblocks = 0;
237 ext2fs_free_mem((void **) &buf);
238 ext2fs_free_dblist(fs->dblist);
240 if (ctx->inode_bad_map) {
241 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
242 ctx->inode_bad_map = 0;
244 if (ctx->inode_reg_map) {
245 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
246 ctx->inode_reg_map = 0;
249 clear_problem_context(&pctx);
250 if (ctx->large_files) {
251 if (!(sb->s_feature_ro_compat &
252 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
253 fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
254 sb->s_feature_ro_compat |=
255 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
256 ext2fs_mark_super_dirty(fs);
258 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
259 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
260 ext2fs_update_dynamic_rev(fs);
261 ext2fs_mark_super_dirty(fs);
263 } else if (!ctx->large_files &&
264 (sb->s_feature_ro_compat &
265 EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
266 if (fs->flags & EXT2_FLAG_RW) {
267 sb->s_feature_ro_compat &=
268 ~EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
269 ext2fs_mark_super_dirty(fs);
273 #ifdef RESOURCE_TRACK
274 if (ctx->options & E2F_OPT_TIME2) {
275 e2fsck_clear_progbar(ctx);
276 print_resource_track("Pass 2", &rtrack);
282 * Make sure the first entry in the directory is '.', and that the
283 * directory entry is sane.
285 static int check_dot(e2fsck_t ctx,
286 struct ext2_dir_entry *dirent,
287 ext2_ino_t ino, struct problem_context *pctx)
289 struct ext2_dir_entry *nextdir;
296 problem = PR_2_MISSING_DOT;
297 else if (((dirent->name_len & 0xFF) != 1) ||
298 (dirent->name[0] != '.'))
299 problem = PR_2_1ST_NOT_DOT;
300 else if (dirent->name[1] != '\0')
301 problem = PR_2_DOT_NULL_TERM;
304 if (fix_problem(ctx, problem, pctx)) {
305 if (dirent->rec_len < 12)
306 dirent->rec_len = 12;
308 dirent->name_len = 1;
309 dirent->name[0] = '.';
310 dirent->name[1] = '\0';
315 if (dirent->inode != ino) {
316 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
321 if (dirent->rec_len > 12) {
322 new_len = dirent->rec_len - 12;
325 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
326 nextdir = (struct ext2_dir_entry *)
327 ((char *) dirent + 12);
328 dirent->rec_len = 12;
329 nextdir->rec_len = new_len;
331 nextdir->name_len = 0;
340 * Make sure the second entry in the directory is '..', and that the
341 * directory entry is sane. We do not check the inode number of '..'
342 * here; this gets done in pass 3.
344 static int check_dotdot(e2fsck_t ctx,
345 struct ext2_dir_entry *dirent,
346 struct dir_info *dir, struct problem_context *pctx)
351 problem = PR_2_MISSING_DOT_DOT;
352 else if (((dirent->name_len & 0xFF) != 2) ||
353 (dirent->name[0] != '.') ||
354 (dirent->name[1] != '.'))
355 problem = PR_2_2ND_NOT_DOT_DOT;
356 else if (dirent->name[2] != '\0')
357 problem = PR_2_DOT_DOT_NULL_TERM;
360 if (fix_problem(ctx, problem, pctx)) {
361 if (dirent->rec_len < 12)
362 dirent->rec_len = 12;
364 * Note: we don't have the parent inode just
365 * yet, so we will fill it in with the root
366 * inode. This will get fixed in pass 3.
368 dirent->inode = EXT2_ROOT_INO;
369 dirent->name_len = 2;
370 dirent->name[0] = '.';
371 dirent->name[1] = '.';
372 dirent->name[2] = '\0';
377 dir->dotdot = dirent->inode;
382 * Check to make sure a directory entry doesn't contain any illegal
385 static int check_name(e2fsck_t ctx,
386 struct ext2_dir_entry *dirent,
387 ext2_ino_t dir_ino, struct problem_context *pctx)
393 for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
394 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
396 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
399 dirent->name[i] = '.';
408 * Check the directory filetype (if present)
410 static _INLINE_ int check_filetype(e2fsck_t ctx,
411 struct ext2_dir_entry *dirent,
412 ext2_ino_t dir_ino, struct problem_context *pctx)
414 int filetype = dirent->name_len >> 8;
415 int should_be = EXT2_FT_UNKNOWN;
416 struct ext2_inode inode;
418 if (!(ctx->fs->super->s_feature_incompat &
419 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
421 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
423 dirent->name_len = dirent->name_len & 0xFF;
427 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dirent->inode)) {
428 should_be = EXT2_FT_DIR;
429 } else if (ext2fs_test_inode_bitmap(ctx->inode_reg_map,
431 should_be = EXT2_FT_REG_FILE;
432 } else if (ctx->inode_bad_map &&
433 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
437 e2fsck_read_inode(ctx, dirent->inode, &inode,
439 should_be = ext2_file_type(inode.i_mode);
441 if (filetype == should_be)
443 pctx->num = should_be;
445 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
449 dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8;
454 static void parse_int_node(ext2_filsys fs,
455 struct ext2_db_entry *db,
456 struct check_dir_struct *cd,
457 struct dx_dir_info *dx_dir,
460 struct ext2_dx_root_info *root;
461 struct ext2_dx_entry *ent;
462 struct ext2_dx_countlimit *limit;
463 struct dx_dirblock_info *dx_db;
466 ext2_dirhash_t min_hash = 0xffffffff;
467 ext2_dirhash_t max_hash = 0;
468 ext2_dirhash_t hash = 0;
470 if (db->blockcnt == 0) {
471 root = (struct ext2_dx_root_info *) (block_buf + 24);
474 printf("Root node dump:\n");
475 printf("\t Reserved zero: %d\n", root->reserved_zero);
476 printf("\t Hash Version: %d\n", root->hash_version);
477 printf("\t Info length: %d\n", root->info_length);
478 printf("\t Indirect levels: %d\n", root->indirect_levels);
479 printf("\t Flags: %d\n", root->unused_flags);
482 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
484 ent = (struct ext2_dx_entry *) (block_buf+8);
486 limit = (struct ext2_dx_countlimit *) ent;
489 printf("Number of entries (count): %d\n", limit->count);
490 printf("Number of entries (limit): %d\n", limit->limit);
493 for (i=0; i < limit->count; i++) {
494 hash = i ? (ent[i].hash & ~1) : 0;
496 * XXX Check to make make sure the hash[i] < hash[i+1]
499 printf("Entry #%d: Hash 0x%08x, block %d\n", i,
502 blk = ent[i].block & 0x0ffffff;
503 /* Check to make sure the block is valid */
504 if (blk > dx_dir->numblocks) {
506 if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
508 clear_htree(cd->ctx, cd->pctx.ino);
509 dx_dir->numblocks = 0;
513 dx_db = &dx_dir->dx_block[blk];
514 if (dx_db->flags & DX_FLAG_REFERENCED) {
515 dx_db->flags |= DX_FLAG_DUP_REF;
517 dx_db->flags |= DX_FLAG_REFERENCED;
518 dx_db->parent = db->blockcnt;
524 dx_db->node_min_hash = hash;
525 if ((i+1) < limit->count)
526 dx_db->node_max_hash = (ent[i+1].hash & ~1);
528 dx_db->node_max_hash = 0xfffffffe;
529 dx_db->flags |= DX_FLAG_LAST;
532 dx_db->flags |= DX_FLAG_FIRST;
535 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
536 db->blockcnt, min_hash, max_hash);
538 dx_db = &dx_dir->dx_block[db->blockcnt];
539 dx_db->min_hash = min_hash;
540 dx_db->max_hash = max_hash;
542 #endif /* ENABLE_HTREE */
544 static int check_dir_block(ext2_filsys fs,
545 struct ext2_db_entry *db,
548 struct dir_info *subdir, *dir;
549 struct dx_dir_info *dx_dir;
551 struct dx_dirblock_info *dx_db = 0;
552 #endif /* ENABLE_HTREE */
553 struct ext2_dir_entry *dirent;
556 int dir_modified = 0;
558 blk_t block_nr = db->blk;
559 ext2_ino_t ino = db->ino;
561 struct check_dir_struct *cd;
566 cd = (struct check_dir_struct *) priv_data;
570 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
573 if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
577 * Make sure the inode is still in use (could have been
578 * deleted in the duplicate/bad blocks pass.
580 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino)))
584 cd->pctx.blk = block_nr;
585 cd->pctx.blkcount = db->blockcnt;
591 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
602 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
606 cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
607 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
608 cd->pctx.errcode = 0; /* We'll handle this ourselves */
609 if (cd->pctx.errcode) {
610 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
611 ctx->flags |= E2F_FLAG_ABORT;
614 memset(buf, 0, fs->blocksize);
617 dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
618 if (dx_dir && dx_dir->numblocks) {
619 if (db->blockcnt >= dx_dir->numblocks) {
620 printf("XXX should never happen!!!\n");
623 dx_db = &dx_dir->dx_block[db->blockcnt];
624 dx_db->type = DX_DIRBLOCK_LEAF;
625 dx_db->phys = block_nr;
626 dx_db->min_hash = ~0;
629 dirent = (struct ext2_dir_entry *) buf;
631 * XXX we need to check to make sure the root
632 * directory block is actually valid!
634 if (db->blockcnt == 0) {
635 dx_db->type = DX_DIRBLOCK_ROOT;
636 dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
637 } else if ((dirent->inode == 0) &&
638 (dirent->rec_len == fs->blocksize))
639 dx_db->type = DX_DIRBLOCK_NODE;
641 #endif /* ENABLE_HTREE */
646 dirent = (struct ext2_dir_entry *) (buf + offset);
647 cd->pctx.dirent = dirent;
648 cd->pctx.num = offset;
649 if (((offset + dirent->rec_len) > fs->blocksize) ||
650 (dirent->rec_len < 12) ||
651 ((dirent->rec_len % 4) != 0) ||
652 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
653 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
654 dirent->rec_len = fs->blocksize - offset;
655 dirent->name_len = 0;
661 if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) {
662 if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
663 dirent->name_len = EXT2_NAME_LEN;
668 if (dot_state == 1) {
669 if (check_dot(ctx, dirent, ino, &cd->pctx))
671 } else if (dot_state == 2) {
672 dir = e2fsck_get_dir_info(ctx, ino);
674 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
675 ctx->flags |= E2F_FLAG_ABORT;
678 if (check_dotdot(ctx, dirent, dir, &cd->pctx))
680 } else if (dirent->inode == ino) {
681 problem = PR_2_LINK_DOT;
682 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
692 * Make sure the inode listed is a legal one.
694 if (((dirent->inode != EXT2_ROOT_INO) &&
695 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
696 (dirent->inode > fs->super->s_inodes_count)) {
697 problem = PR_2_BAD_INO;
698 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
701 * If the inode is unused, offer to clear it.
703 problem = PR_2_UNUSED_INODE;
704 } else if (ctx->inode_bb_map &&
705 (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
708 * If the inode is in a bad block, offer to
711 problem = PR_2_BB_INODE;
712 } else if ((dot_state > 2) &&
713 ((dirent->name_len & 0xFF) == 1) &&
714 (dirent->name[0] == '.')) {
716 * If there's a '.' entry in anything other
717 * than the first directory entry, it's a
718 * duplicate entry that should be removed.
720 problem = PR_2_DUP_DOT;
721 } else if ((dot_state > 2) &&
722 ((dirent->name_len & 0xFF) == 2) &&
723 (dirent->name[0] == '.') &&
724 (dirent->name[1] == '.')) {
726 * If there's a '..' entry in anything other
727 * than the second directory entry, it's a
728 * duplicate entry that should be removed.
730 problem = PR_2_DUP_DOT_DOT;
731 } else if ((dot_state > 2) &&
732 (dirent->inode == EXT2_ROOT_INO)) {
734 * Don't allow links to the root directory.
735 * We check this specially to make sure we
736 * catch this error case even if the root
737 * directory hasn't been created yet.
739 problem = PR_2_LINK_ROOT;
740 } else if ((dot_state > 2) &&
741 (dirent->name_len & 0xFF) == 0) {
743 * Don't allow zero-length directory names.
745 problem = PR_2_NULL_NAME;
749 if (fix_problem(ctx, problem, &cd->pctx)) {
754 ext2fs_unmark_valid(fs);
755 if (problem == PR_2_BAD_INO)
761 * If the inode was marked as having bad fields in
762 * pass1, process it and offer to fix/clear it.
763 * (We wait until now so that we can display the
764 * pathname to the user.)
766 if (ctx->inode_bad_map &&
767 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
769 if (e2fsck_process_bad_inode(ctx, ino,
771 buf + fs->blocksize)) {
776 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
780 if (check_name(ctx, dirent, ino, &cd->pctx))
783 if (check_filetype(ctx, dirent, ino, &cd->pctx))
788 ext2fs_dirhash(dx_dir->hashversion, dirent->name,
789 (dirent->name_len & 0xFF),
790 fs->super->s_hash_seed, &hash, 0);
791 if (hash < dx_db->min_hash)
792 dx_db->min_hash = hash;
793 if (hash > dx_db->max_hash)
794 dx_db->max_hash = hash;
799 * If this is a directory, then mark its parent in its
800 * dir_info structure. If the parent field is already
801 * filled in, then this directory has more than one
802 * hard link. We assume the first link is correct,
803 * and ask the user if he/she wants to clear this one.
805 if ((dot_state > 2) &&
806 (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
808 subdir = e2fsck_get_dir_info(ctx, dirent->inode);
810 cd->pctx.ino = dirent->inode;
811 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
812 ctx->flags |= E2F_FLAG_ABORT;
815 if (subdir->parent) {
816 cd->pctx.ino2 = subdir->parent;
817 if (fix_problem(ctx, PR_2_LINK_DIR,
825 subdir->parent = ino;
828 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
831 ctx->fs_links_count++;
832 ctx->fs_total_count++;
834 offset += dirent->rec_len;
835 } while (offset < fs->blocksize);
842 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
843 db->blockcnt, dx_db->type,
844 dx_db->min_hash, dx_db->max_hash);
846 cd->pctx.dir = cd->pctx.ino;
847 if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
848 (dx_db->type == DX_DIRBLOCK_NODE))
849 parse_int_node(fs, db, cd, dx_dir, buf);
851 #endif /* ENABLE_HTREE */
852 if (offset != fs->blocksize) {
853 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
854 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
855 dirent->rec_len = cd->pctx.num;
860 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
861 if (cd->pctx.errcode) {
862 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
864 ctx->flags |= E2F_FLAG_ABORT;
868 ext2fs_mark_changed(fs);
874 * This function is called to deallocate a block, and is an interator
875 * functioned called by deallocate inode via ext2fs_iterate_block().
877 static int deallocate_inode_block(ext2_filsys fs,
879 e2_blkcnt_t blockcnt,
884 e2fsck_t ctx = (e2fsck_t) priv_data;
886 if (HOLE_BLKADDR(*block_nr))
888 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
889 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
894 * This fuction deallocates an inode
896 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
898 ext2_filsys fs = ctx->fs;
899 struct ext2_inode inode;
900 struct problem_context pctx;
902 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
903 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
904 inode.i_links_count = 0;
905 inode.i_dtime = time(0);
906 e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
907 clear_problem_context(&pctx);
911 * Fix up the bitmaps...
913 e2fsck_read_bitmaps(ctx);
914 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
915 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
916 if (ctx->inode_bad_map)
917 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
918 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
919 ext2fs_mark_ib_dirty(fs);
921 if (!ext2fs_inode_has_valid_blocks(&inode))
924 if (LINUX_S_ISREG(inode.i_mode) &&
925 (inode.i_size_high || inode.i_size & 0x80000000UL))
928 if (inode.i_file_acl) {
929 ext2fs_unmark_block_bitmap(ctx->block_found_map,
931 ext2fs_unmark_block_bitmap(fs->block_map, inode.i_file_acl);
934 ext2fs_mark_bb_dirty(fs);
935 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
936 deallocate_inode_block, ctx);
938 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
939 ctx->flags |= E2F_FLAG_ABORT;
945 * This fuction clears the htree flag on an inode
947 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
949 struct ext2_inode inode;
950 struct problem_context pctx;
952 e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
953 inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
954 e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
955 if (ctx->dirs_to_hash)
956 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
960 extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
961 ext2_ino_t ino, char *buf)
963 ext2_filsys fs = ctx->fs;
964 struct ext2_inode inode;
965 int inode_modified = 0;
966 unsigned char *frag, *fsize;
967 struct problem_context pctx;
970 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
972 clear_problem_context(&pctx);
977 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
978 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
979 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
980 !(LINUX_S_ISSOCK(inode.i_mode)))
981 problem = PR_2_BAD_MODE;
982 else if (LINUX_S_ISCHR(inode.i_mode)
983 && !e2fsck_pass1_check_device_inode(&inode))
984 problem = PR_2_BAD_CHAR_DEV;
985 else if (LINUX_S_ISBLK(inode.i_mode)
986 && !e2fsck_pass1_check_device_inode(&inode))
987 problem = PR_2_BAD_BLOCK_DEV;
988 else if (LINUX_S_ISFIFO(inode.i_mode)
989 && !e2fsck_pass1_check_device_inode(&inode))
990 problem = PR_2_BAD_FIFO;
991 else if (LINUX_S_ISSOCK(inode.i_mode)
992 && !e2fsck_pass1_check_device_inode(&inode))
993 problem = PR_2_BAD_SOCKET;
994 else if (LINUX_S_ISLNK(inode.i_mode)
995 && !e2fsck_pass1_check_symlink(fs, &inode, buf)) {
996 problem = PR_2_INVALID_SYMLINK;
1000 if (fix_problem(ctx, problem, &pctx)) {
1001 deallocate_inode(ctx, ino, 0);
1002 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1009 if (inode.i_faddr &&
1010 fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1015 switch (fs->super->s_creator_os) {
1017 frag = &inode.osd2.linux2.l_i_frag;
1018 fsize = &inode.osd2.linux2.l_i_fsize;
1021 frag = &inode.osd2.hurd2.h_i_frag;
1022 fsize = &inode.osd2.hurd2.h_i_fsize;
1025 frag = &inode.osd2.masix2.m_i_frag;
1026 fsize = &inode.osd2.masix2.m_i_fsize;
1031 if (frag && *frag) {
1033 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
1039 if (fsize && *fsize) {
1041 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
1048 if (inode.i_file_acl &&
1049 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) &&
1050 fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
1051 inode.i_file_acl = 0;
1054 if (inode.i_file_acl &&
1055 ((inode.i_file_acl < fs->super->s_first_data_block) ||
1056 (inode.i_file_acl >= fs->super->s_blocks_count)) &&
1057 fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
1058 inode.i_file_acl = 0;
1061 if (inode.i_dir_acl &&
1062 LINUX_S_ISDIR(inode.i_mode) &&
1063 fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1064 inode.i_dir_acl = 0;
1068 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
1074 * allocate_dir_block --- this function allocates a new directory
1075 * block for a particular inode; this is done if a directory has
1076 * a "hole" in it, or if a directory has a illegal block number
1077 * that was zeroed out and now needs to be replaced.
1079 static int allocate_dir_block(e2fsck_t ctx,
1080 struct ext2_db_entry *db,
1081 char *buf, struct problem_context *pctx)
1083 ext2_filsys fs = ctx->fs;
1086 struct ext2_inode inode;
1088 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
1092 * Read the inode and block bitmaps in; we'll be messing with
1095 e2fsck_read_bitmaps(ctx);
1098 * First, find a free block
1100 pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
1101 if (pctx->errcode) {
1102 pctx->str = "ext2fs_new_block";
1103 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1106 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1107 ext2fs_mark_block_bitmap(fs->block_map, blk);
1108 ext2fs_mark_bb_dirty(fs);
1111 * Now let's create the actual data block for the inode
1114 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
1116 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1117 EXT2_ROOT_INO, &block);
1119 if (pctx->errcode) {
1120 pctx->str = "ext2fs_new_dir_block";
1121 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1125 pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
1126 ext2fs_free_mem((void **) &block);
1127 if (pctx->errcode) {
1128 pctx->str = "ext2fs_write_dir_block";
1129 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1134 * Update the inode block count
1136 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1137 inode.i_blocks += fs->blocksize / 512;
1138 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
1139 inode.i_size = (db->blockcnt+1) * fs->blocksize;
1140 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
1143 * Finally, update the block pointers for the inode
1146 pctx->errcode = ext2fs_block_iterate2(fs, db->ino, BLOCK_FLAG_HOLE,
1147 0, update_dir_block, db);
1148 if (pctx->errcode) {
1149 pctx->str = "ext2fs_block_iterate";
1150 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1158 * This is a helper function for allocate_dir_block().
1160 static int update_dir_block(ext2_filsys fs,
1162 e2_blkcnt_t blockcnt,
1167 struct ext2_db_entry *db;
1169 db = (struct ext2_db_entry *) priv_data;
1170 if (db->blockcnt == (int) blockcnt) {
1171 *block_nr = db->blk;
1172 return BLOCK_CHANGED;