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_badness bitmap
37 * - The inode_dir_map bitmap
39 * Pass 2 frees the following data structures
40 * - The inode_reg_map bitmap
43 #define _GNU_SOURCE 1 /* get strnlen() */
51 #ifdef NO_INLINE_FUNCS
54 #define _INLINE_ inline
57 /* #define DX_DEBUG */
60 * Keeps track of how many times an inode is referenced.
62 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf);
63 static int check_dir_block(ext2_filsys fs,
64 struct ext2_db_entry2 *dir_blocks_info,
66 static int allocate_dir_block(e2fsck_t ctx,
67 struct ext2_db_entry2 *dir_blocks_info,
68 char *buf, struct problem_context *pctx);
69 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino);
70 static int htree_depth(struct dx_dir_info *dx_dir,
71 struct dx_dirblock_info *dx_db);
72 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b);
74 struct check_dir_struct {
76 struct problem_context pctx;
81 void e2fsck_pass2(e2fsck_t ctx)
83 struct ext2_super_block *sb = ctx->fs->super;
84 struct problem_context pctx;
85 ext2_filsys fs = ctx->fs;
88 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;
93 unsigned int save_type;
99 init_resource_track(&rtrack, ctx->fs->io);
100 clear_problem_context(&cd.pctx);
103 mtrace_print("Pass 2");
106 if (!(ctx->options & E2F_OPT_PREEN))
107 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
109 e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT,
111 if (ctx->inode_count)
114 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
115 "inode_count", &save_type);
116 cd.pctx.errcode = ext2fs_create_icount2(fs,
117 EXT2_ICOUNT_OPT_INCREMENT,
118 0, ctx->inode_link_info,
120 fs->default_bitmap_type = save_type;
122 if (cd.pctx.errcode) {
123 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
124 ctx->flags |= E2F_FLAG_ABORT;
127 buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
128 "directory scan buffer");
131 * Set up the parent pointer for the root directory, if
132 * present. (If the root directory is not present, we will
133 * create it in pass 3.)
135 (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
140 cd.max = ext2fs_dblist_count2(fs->dblist);
143 (void) (ctx->progress)(ctx, 2, 0, cd.max);
145 if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX)
146 ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp);
148 cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_block,
150 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
153 if (ctx->flags & E2F_FLAG_RESTART_LATER) {
154 ctx->flags |= E2F_FLAG_RESTART;
158 if (cd.pctx.errcode) {
159 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
160 ctx->flags |= E2F_FLAG_ABORT;
165 for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
166 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
168 if (dx_dir->numblocks == 0)
170 clear_problem_context(&pctx);
172 pctx.dir = dx_dir->ino;
173 dx_db = dx_dir->dx_block;
174 if (dx_db->flags & DX_FLAG_REFERENCED)
175 dx_db->flags |= DX_FLAG_DUP_REF;
177 dx_db->flags |= DX_FLAG_REFERENCED;
179 * Find all of the first and last leaf blocks, and
180 * update their parent's min and max hash values
182 for (b=0, dx_db = dx_dir->dx_block;
183 b < dx_dir->numblocks;
185 if ((dx_db->type != DX_DIRBLOCK_LEAF) ||
186 !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST)))
188 dx_parent = &dx_dir->dx_block[dx_db->parent];
190 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
192 if (dx_db->flags & DX_FLAG_FIRST)
193 dx_parent->min_hash = dx_db->min_hash;
195 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
197 if (dx_db->flags & DX_FLAG_LAST)
198 dx_parent->max_hash = dx_db->max_hash;
201 for (b=0, dx_db = dx_dir->dx_block;
202 b < dx_dir->numblocks;
205 pctx.group = dx_db->parent;
207 if (!(dx_db->flags & DX_FLAG_FIRST) &&
208 (dx_db->min_hash < dx_db->node_min_hash)) {
209 pctx.blk = dx_db->min_hash;
210 pctx.blk2 = dx_db->node_min_hash;
211 code = PR_2_HTREE_MIN_HASH;
212 fix_problem(ctx, code, &pctx);
215 if (dx_db->type == DX_DIRBLOCK_LEAF) {
216 depth = htree_depth(dx_dir, dx_db);
217 if (depth != dx_dir->depth) {
218 pctx.num = dx_dir->depth;
219 code = PR_2_HTREE_BAD_DEPTH;
220 fix_problem(ctx, code, &pctx);
225 * This test doesn't apply for the root block
229 (dx_db->max_hash > dx_db->node_max_hash)) {
230 pctx.blk = dx_db->max_hash;
231 pctx.blk2 = dx_db->node_max_hash;
232 code = PR_2_HTREE_MAX_HASH;
233 fix_problem(ctx, code, &pctx);
236 if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
237 code = PR_2_HTREE_NOTREF;
238 fix_problem(ctx, code, &pctx);
240 } else if (dx_db->flags & DX_FLAG_DUP_REF) {
241 code = PR_2_HTREE_DUPREF;
242 fix_problem(ctx, code, &pctx);
246 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
247 clear_htree(ctx, dx_dir->ino);
248 dx_dir->numblocks = 0;
251 e2fsck_free_dx_dir_info(ctx);
253 ext2fs_free_mem(&buf);
254 ext2fs_free_dblist(fs->dblist);
256 if (ctx->inode_reg_map) {
257 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
258 ctx->inode_reg_map = 0;
261 clear_problem_context(&pctx);
262 if (ctx->large_files) {
263 if (!(sb->s_feature_ro_compat &
264 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
265 fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
266 sb->s_feature_ro_compat |=
267 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
268 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
269 ext2fs_mark_super_dirty(fs);
271 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
272 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
273 ext2fs_update_dynamic_rev(fs);
274 ext2fs_mark_super_dirty(fs);
278 print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io);
281 #define MAX_DEPTH 32000
282 static int htree_depth(struct dx_dir_info *dx_dir,
283 struct dx_dirblock_info *dx_db)
287 while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
288 dx_db = &dx_dir->dx_block[dx_db->parent];
294 static int dict_de_cmp(const void *a, const void *b)
296 const struct ext2_dir_entry *de_a, *de_b;
299 de_a = (const struct ext2_dir_entry *) a;
300 a_len = de_a->name_len & 0xFF;
301 de_b = (const struct ext2_dir_entry *) b;
302 b_len = de_b->name_len & 0xFF;
305 return (a_len - b_len);
307 return strncmp(de_a->name, de_b->name, a_len);
311 * This is special sort function that makes sure that directory blocks
312 * with a dirblock of zero are sorted to the beginning of the list.
313 * This guarantees that the root node of the htree directories are
314 * processed first, so we know what hash version to use.
316 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
318 const struct ext2_db_entry2 *db_a =
319 (const struct ext2_db_entry2 *) a;
320 const struct ext2_db_entry2 *db_b =
321 (const struct ext2_db_entry2 *) b;
323 if (db_a->blockcnt && !db_b->blockcnt)
326 if (!db_a->blockcnt && db_b->blockcnt)
329 if (db_a->blk != db_b->blk)
330 return (int) (db_a->blk - db_b->blk);
332 if (db_a->ino != db_b->ino)
333 return (int) (db_a->ino - db_b->ino);
335 return (int) (db_a->blockcnt - db_b->blockcnt);
338 void ext2_fix_dirent_dirdata(struct ext2_dir_entry *de)
340 __u16 file_type = de->name_len & (EXT2_FT_MASK << 8);
341 __u8 de_flags = (de->name_len >> 8) & ~EXT2_FT_MASK;
342 __u8 name_len = de->name_len & EXT2_NAME_LEN;
346 for (i = 0; i < 4; i++) {
347 __u8 flags = new_flag | (1 << i) << 4;
349 /* new_flag is accumulating flags that are set in de_flags
350 * and still fit inside rec_len. ext2_get_dirent_dirdata_size()
351 * returns the size of all the dirdata entries in flags, and
352 * chops off any that are beyond rec_len. */
353 if ((de_flags & flags) == flags) {
354 int dirdatalen = ext2_get_dirent_dirdata_size(de,flags);
355 int rlen = __EXT2_DIR_REC_LEN(name_len + dirdatalen);
357 if (rlen > de->rec_len)
364 de->name_len = name_len | file_type | (new_flag << 8);
368 * check for dirent data in ext3 dirent.
369 * return 0 if dirent data is ok.
370 * return 1 if dirent data does not exist.
371 * return 2 if dirent was modified due to error.
373 int e2fsck_check_dirent_data(e2fsck_t ctx, struct ext2_dir_entry *de,
374 unsigned int offset, struct problem_context *pctx)
376 if (!(ctx->fs->super->s_feature_incompat &
377 EXT4_FEATURE_INCOMPAT_DIRDATA)) {
378 if ((de->name_len >> 8) & ~EXT2_FT_MASK) {
379 /* clear dirent extra data flags. */
380 if (fix_problem(ctx, PR_2_CLEAR_DIRDATA, pctx)) {
381 de->name_len &= (EXT2_FT_MASK << 8) |
388 if ((de->name_len >> 8) & ~EXT2_FT_MASK) {
389 if (de->rec_len >= EXT2_DIR_REC_LEN(de) ||
390 de->rec_len + offset == EXT2_BLOCK_SIZE(ctx->fs->super)) {
391 if (ext2_get_dirent_dirdata_size(de,
392 EXT2_DIRENT_LUFID) ==
393 EXT2_DIRENT_LUFID_SIZE)
396 /* just clear dirent data flags for now, we should fix FID data
397 * in lustre specific pass.
399 if (fix_problem(ctx, PR_2_CLEAR_DIRDATA, pctx)) {
400 ext2_fix_dirent_dirdata(de);
401 if (ext2_get_dirent_dirdata_size(de,
402 EXT2_DIRENT_LUFID) !=
403 EXT2_DIRENT_LUFID_SIZE)
404 de->name_len &= ~(EXT2_DIRENT_LUFID << 8);
412 * Make sure the first entry in the directory is '.', and that the
413 * directory entry is sane.
415 static int check_dot(e2fsck_t ctx,
416 struct ext2_dir_entry *dirent, unsigned int offset,
417 ext2_ino_t ino, struct problem_context *pctx)
419 struct ext2_dir_entry *nextdir;
420 unsigned int rec_len, new_len;
423 problem_t problem = 0;
427 problem = PR_2_MISSING_DOT;
428 else if (((dirent->name_len & 0xFF) != 1) ||
429 (dirent->name[0] != '.'))
430 problem = PR_2_1ST_NOT_DOT;
431 else if (dirent->name[1] != '\0')
432 problem = PR_2_DOT_NULL_TERM;
434 dir_data_error = e2fsck_check_dirent_data(ctx, dirent, offset, pctx);
436 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
438 if (fix_problem(ctx, problem, pctx)) {
439 if (rec_len < 12 && dir_data_error)
440 rec_len = dirent->rec_len = 12;
442 dirent->name_len = 1;
443 dirent->name[0] = '.';
444 dirent->name[1] = '\0';
449 if (dirent->inode != ino) {
450 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
455 if (rec_len > 12 && dir_data_error) {
456 new_len = rec_len - 12;
459 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
460 nextdir = (struct ext2_dir_entry *)
461 ((char *) dirent + 12);
462 dirent->rec_len = 12;
463 (void) ext2fs_set_rec_len(ctx->fs, new_len,
466 nextdir->name_len = 0;
475 * Make sure the second entry in the directory is '..', and that the
476 * directory entry is sane. We do not check the inode number of '..'
477 * here; this gets done in pass 3.
479 static int check_dotdot(e2fsck_t ctx,
480 struct ext2_dir_entry *dirent, unsigned int offset,
481 ext2_ino_t ino, struct problem_context *pctx)
483 problem_t problem = 0;
484 unsigned int rec_len;
488 problem = PR_2_MISSING_DOT_DOT;
489 else if (((dirent->name_len & 0xFF) != 2) ||
490 (dirent->name[0] != '.') ||
491 (dirent->name[1] != '.'))
492 problem = PR_2_2ND_NOT_DOT_DOT;
493 else if (dirent->name[2] != '\0')
494 problem = PR_2_DOT_DOT_NULL_TERM;
496 dir_data_error = e2fsck_check_dirent_data(ctx, dirent, offset, pctx);
498 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
500 if (fix_problem(ctx, problem, pctx)) {
501 if (rec_len < 12 && dir_data_error)
502 dirent->rec_len = 12;
504 * Note: we don't have the parent inode just
505 * yet, so we will fill it in with the root
506 * inode. This will get fixed in pass 3.
508 dirent->inode = EXT2_ROOT_INO;
509 dirent->name_len = 2;
510 dirent->name[0] = '.';
511 dirent->name[1] = '.';
512 dirent->name[2] = '\0';
517 if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) {
518 fix_problem(ctx, PR_2_NO_DIRINFO, pctx);
525 * Check to make sure a directory entry doesn't contain any illegal
528 static int check_name(e2fsck_t ctx,
529 struct ext2_dir_entry *dirent,
530 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
531 struct problem_context *pctx)
537 for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
538 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
540 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
543 dirent->name[i] = '.';
552 * Check the directory filetype (if present)
554 static _INLINE_ int check_filetype(e2fsck_t ctx,
555 struct ext2_dir_entry *dirent,
556 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
557 struct problem_context *pctx)
559 int filetype = dirent->name_len >> 8;
560 int should_be = EXT2_FT_UNKNOWN;
562 struct ext2_inode inode;
565 if (ctx->fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_DIRDATA) {
566 dirdata = filetype & ~EXT2_FT_MASK;
567 filetype = filetype & EXT2_FT_MASK;
570 if (!(ctx->fs->super->s_feature_incompat &
571 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
573 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
575 dirent->name_len = dirent->name_len & 0xFF;
579 if (ctx->inode_badness)
580 ext2fs_icount_fetch(ctx->inode_badness, dirent->inode,
583 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) {
584 should_be = EXT2_FT_DIR;
585 } else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map,
587 should_be = EXT2_FT_REG_FILE;
588 } else if (ctx->inode_badness && result >= BADNESS_BAD_MODE) {
591 e2fsck_read_inode(ctx, dirent->inode, &inode,
593 should_be = ext2_file_type(inode.i_mode);
595 if (filetype == should_be)
597 pctx->num = should_be;
599 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
603 dirent->name_len = (dirent->name_len & 0xFF) |
604 (dirdata | should_be) << 8;
609 static void parse_int_node(ext2_filsys fs,
610 struct ext2_db_entry2 *db,
611 struct check_dir_struct *cd,
612 struct dx_dir_info *dx_dir,
615 struct ext2_dx_root_info *root;
616 struct ext2_dx_entry *ent;
617 struct ext2_dx_countlimit *limit;
618 struct dx_dirblock_info *dx_db;
619 int i, expect_limit, count;
621 ext2_dirhash_t min_hash = 0xffffffff;
622 ext2_dirhash_t max_hash = 0;
623 ext2_dirhash_t hash = 0, prev_hash;
625 if (db->blockcnt == 0) {
626 root = get_ext2_dx_root_info(fs, block_buf);
629 printf("Root node dump:\n");
630 printf("\t Reserved zero: %u\n", root->reserved_zero);
631 printf("\t Hash Version: %d\n", root->hash_version);
632 printf("\t Info length: %d\n", root->info_length);
633 printf("\t Indirect levels: %d\n", root->indirect_levels);
634 printf("\t Flags: %d\n", root->unused_flags);
637 ent = (struct ext2_dx_entry *)((char *)root +
640 ent = (struct ext2_dx_entry *)(block_buf + 8);
642 limit = (struct ext2_dx_countlimit *)ent;
645 printf("Number of entries (count): %d\n",
646 ext2fs_le16_to_cpu(limit->count));
647 printf("Number of entries (limit): %d\n",
648 ext2fs_le16_to_cpu(limit->limit));
651 count = ext2fs_le16_to_cpu(limit->count);
652 expect_limit = (fs->blocksize - ((char *) ent - block_buf)) /
653 sizeof(struct ext2_dx_entry);
654 if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
655 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
656 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
659 if (count > expect_limit) {
660 cd->pctx.num = count;
661 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
663 count = expect_limit;
666 for (i=0; i < count; i++) {
668 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
670 printf("Entry #%d: Hash 0x%08x, block %u\n", i,
671 hash, ext2fs_le32_to_cpu(ent[i].block));
673 blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff;
674 /* Check to make sure the block is valid */
675 if (blk >= (blk_t) dx_dir->numblocks) {
677 if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
682 if (hash < prev_hash &&
683 fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
685 dx_db = &dx_dir->dx_block[blk];
686 if (dx_db->flags & DX_FLAG_REFERENCED) {
687 dx_db->flags |= DX_FLAG_DUP_REF;
689 dx_db->flags |= DX_FLAG_REFERENCED;
690 dx_db->parent = db->blockcnt;
696 dx_db->node_min_hash = hash;
698 dx_db->node_max_hash =
699 ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
701 dx_db->node_max_hash = 0xfffffffe;
702 dx_db->flags |= DX_FLAG_LAST;
705 dx_db->flags |= DX_FLAG_FIRST;
708 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
709 db->blockcnt, min_hash, max_hash);
711 dx_db = &dx_dir->dx_block[db->blockcnt];
712 dx_db->min_hash = min_hash;
713 dx_db->max_hash = max_hash;
717 clear_htree(cd->ctx, cd->pctx.ino);
718 dx_dir->numblocks = 0;
720 #endif /* ENABLE_HTREE */
723 * Given a busted directory, try to salvage it somehow.
726 static void salvage_directory(ext2_filsys fs,
727 struct ext2_dir_entry *dirent,
728 struct ext2_dir_entry *prev,
729 unsigned int *offset)
731 char *cp = (char *) dirent;
733 unsigned int rec_len, prev_rec_len;
734 unsigned int name_len = dirent->name_len & 0xFF;
736 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
737 left = fs->blocksize - *offset - rec_len;
740 * Special case of directory entry of size 8: copy what's left
741 * of the directory block up to cover up the invalid hole.
743 if ((left >= 12) && (rec_len == 8)) {
744 memmove(cp, cp+8, left);
745 memset(cp + left, 0, 8);
749 * If the directory entry overruns the end of the directory
750 * block, and the name is small enough to fit, then adjust the
754 ((int) rec_len + left > 8) &&
755 ((int) name_len + 8 <= (int) rec_len + left) &&
756 dirent->inode <= fs->super->s_inodes_count &&
757 strnlen(dirent->name, name_len) == name_len) {
758 (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent);
762 * If the record length of the directory entry is a multiple
763 * of four, and not too big, such that it is valid, let the
764 * previous directory entry absorb the invalid one.
766 if (prev && rec_len && (rec_len % 4) == 0 &&
767 (*offset + rec_len <= fs->blocksize)) {
768 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
769 prev_rec_len += rec_len;
770 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
775 * Default salvage method --- kill all of the directory
776 * entries for the rest of the block. We will either try to
777 * absorb it into the previous directory entry, or create a
778 * new empty directory entry the rest of the directory block.
781 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
782 prev_rec_len += fs->blocksize - *offset;
783 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
784 *offset = fs->blocksize;
786 rec_len = fs->blocksize - *offset;
787 (void) ext2fs_set_rec_len(fs, rec_len, dirent);
788 dirent->name_len = 0;
793 static int check_dir_block(ext2_filsys fs,
794 struct ext2_db_entry2 *db,
797 struct dx_dir_info *dx_dir;
799 struct dx_dirblock_info *dx_db = 0;
800 #endif /* ENABLE_HTREE */
801 struct ext2_dir_entry *dirent, *prev;
803 unsigned int offset = 0;
804 int dir_modified = 0;
806 unsigned int rec_len;
807 blk64_t block_nr = db->blk;
808 ext2_ino_t ino = db->ino;
809 ext2_ino_t subdir_parent;
811 struct check_dir_struct *cd;
815 struct ext2_dx_root_info *root;
816 struct ext2_dx_countlimit *limit;
817 static dict_t de_dict;
818 struct problem_context pctx;
822 cd = (struct check_dir_struct *) priv_data;
826 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
829 if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
833 * Make sure the inode is still in use (could have been
834 * deleted in the duplicate/bad blocks pass.
836 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino)))
840 cd->pctx.blk = block_nr;
841 cd->pctx.blkcount = db->blockcnt;
847 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
857 if (ctx->dirs_to_hash &&
858 ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
862 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
866 ehandler_operation(_("reading directory block"));
867 cd->pctx.errcode = ext2fs_read_dir_block3(fs, block_nr, buf, 0);
868 ehandler_operation(0);
869 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
870 cd->pctx.errcode = 0; /* We'll handle this ourselves */
871 if (cd->pctx.errcode) {
872 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
873 ctx->flags |= E2F_FLAG_ABORT;
876 memset(buf, 0, fs->blocksize);
879 dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
880 if (dx_dir && dx_dir->numblocks) {
881 if (db->blockcnt >= dx_dir->numblocks) {
883 if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK,
885 clear_htree(ctx, ino);
886 dx_dir->numblocks = 0;
890 fatal_error(ctx, _("Can not continue."));
892 dx_db = &dx_dir->dx_block[db->blockcnt];
893 dx_db->type = DX_DIRBLOCK_LEAF;
894 dx_db->phys = block_nr;
895 dx_db->min_hash = ~0;
898 dirent = (struct ext2_dir_entry *) buf;
899 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
900 limit = (struct ext2_dx_countlimit *) (buf+8);
901 if (db->blockcnt == 0) {
902 root = get_ext2_dx_root_info(fs, buf);
903 dx_db->type = DX_DIRBLOCK_ROOT;
904 dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
905 if ((root->reserved_zero ||
906 root->info_length < 8 ||
907 root->indirect_levels > 1) &&
908 fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
909 clear_htree(ctx, ino);
910 dx_dir->numblocks = 0;
913 dx_dir->hashversion = root->hash_version;
914 if ((dx_dir->hashversion <= EXT2_HASH_TEA) &&
915 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
916 dx_dir->hashversion += 3;
917 dx_dir->depth = root->indirect_levels + 1;
918 } else if ((dirent->inode == 0) &&
919 (rec_len == fs->blocksize) &&
920 (dirent->name_len == 0) &&
921 (ext2fs_le16_to_cpu(limit->limit) ==
923 sizeof(struct ext2_dx_entry))))
924 dx_db->type = DX_DIRBLOCK_NODE;
927 #endif /* ENABLE_HTREE */
929 dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
933 ext2_ino_t first_unused_inode;
936 dirent = (struct ext2_dir_entry *) (buf + offset);
937 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
938 cd->pctx.dirent = dirent;
939 cd->pctx.num = offset;
940 if (((offset + rec_len) > fs->blocksize) ||
942 ((rec_len % 4) != 0) ||
943 (((dirent->name_len & (unsigned) 0xFF)+8) > rec_len)) {
944 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
945 salvage_directory(fs, dirent, prev, &offset);
949 goto abort_free_dict;
952 if (dot_state == 0) {
953 if (check_dot(ctx, dirent, offset, ino, &cd->pctx))
955 } else if (dot_state == 1) {
956 ret = check_dotdot(ctx, dirent, offset, ino, &cd->pctx);
958 goto abort_free_dict;
961 } else if (dirent->inode == ino) {
962 problem = PR_2_LINK_DOT;
963 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
972 ret = e2fsck_check_dirent_data(ctx, dirent, offset, &cd->pctx);
977 * Make sure the inode listed is a legal one.
979 if (((dirent->inode != EXT2_ROOT_INO) &&
980 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
981 (dirent->inode > fs->super->s_inodes_count)) {
982 problem = PR_2_BAD_INO;
983 } else if (ctx->inode_bb_map &&
984 (ext2fs_test_inode_bitmap2(ctx->inode_bb_map,
987 * If the inode is in a bad block, offer to
990 problem = PR_2_BB_INODE;
991 } else if ((dot_state > 1) &&
992 ((dirent->name_len & 0xFF) == 1) &&
993 (dirent->name[0] == '.')) {
995 * If there's a '.' entry in anything other
996 * than the first directory entry, it's a
997 * duplicate entry that should be removed.
999 problem = PR_2_DUP_DOT;
1000 } else if ((dot_state > 1) &&
1001 ((dirent->name_len & 0xFF) == 2) &&
1002 (dirent->name[0] == '.') &&
1003 (dirent->name[1] == '.')) {
1005 * If there's a '..' entry in anything other
1006 * than the second directory entry, it's a
1007 * duplicate entry that should be removed.
1009 problem = PR_2_DUP_DOT_DOT;
1010 } else if ((dot_state > 1) &&
1011 (dirent->inode == EXT2_ROOT_INO)) {
1013 * Don't allow links to the root directory.
1014 * We check this specially to make sure we
1015 * catch this error case even if the root
1016 * directory hasn't been created yet.
1018 problem = PR_2_LINK_ROOT;
1019 } else if ((dot_state > 1) &&
1020 (dirent->name_len & 0xFF) == 0) {
1022 * Don't allow zero-length directory names.
1024 problem = PR_2_NULL_NAME;
1028 if (fix_problem(ctx, problem, &cd->pctx)) {
1033 ext2fs_unmark_valid(fs);
1034 if (problem == PR_2_BAD_INO)
1040 * If the inode was marked as having bad fields in
1041 * pass1, process it and offer to fix/clear it.
1042 * (We wait until now so that we can display the
1043 * pathname to the user.)
1045 if (ctx->inode_badness &&
1046 ext2fs_icount_is_set(ctx->inode_badness, dirent->inode)) {
1047 if (e2fsck_process_bad_inode(ctx, ino, dirent->inode,
1048 buf + fs->blocksize)) {
1053 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1054 return DIRENT_ABORT;
1057 group = ext2fs_group_of_ino(fs, dirent->inode);
1058 first_unused_inode = group * fs->super->s_inodes_per_group +
1059 1 + fs->super->s_inodes_per_group -
1060 ext2fs_bg_itable_unused(fs, group);
1061 cd->pctx.group = group;
1064 * Check if the inode was missed out because
1065 * _INODE_UNINIT flag was set or bg_itable_unused was
1066 * incorrect. If so, clear the _INODE_UNINIT flag and
1067 * restart e2fsck. In the future it would be nice if
1068 * we could call a function in pass1.c that checks the
1069 * newly visible inodes.
1071 if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) {
1072 pctx.num = dirent->inode;
1073 if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT,
1075 ext2fs_bg_flags_clear(fs, group,
1076 EXT2_BG_INODE_UNINIT);
1077 ext2fs_mark_super_dirty(fs);
1078 ctx->flags |= E2F_FLAG_RESTART_LATER;
1080 ext2fs_unmark_valid(fs);
1081 if (problem == PR_2_BAD_INO)
1084 } else if (dirent->inode >= first_unused_inode) {
1085 pctx.num = dirent->inode;
1086 if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){
1087 ext2fs_bg_itable_unused_set(fs, group, 0);
1088 ext2fs_mark_super_dirty(fs);
1089 ctx->flags |= E2F_FLAG_RESTART_LATER;
1091 ext2fs_unmark_valid(fs);
1092 if (problem == PR_2_BAD_INO)
1098 * Offer to clear unused inodes; if we are going to be
1099 * restarting the scan due to bg_itable_unused being
1100 * wrong, then don't clear any inodes to avoid zapping
1101 * inodes that were skipped during pass1 due to an
1102 * incorrect bg_itable_unused; we'll get any real
1103 * problems after we restart.
1105 if (!(ctx->flags & E2F_FLAG_RESTART_LATER) &&
1106 !(ext2fs_test_inode_bitmap2(ctx->inode_used_map,
1108 problem = PR_2_UNUSED_INODE;
1111 if (fix_problem(ctx, problem, &cd->pctx)) {
1116 ext2fs_unmark_valid(fs);
1117 if (problem == PR_2_BAD_INO)
1122 if (check_name(ctx, dirent, ino, &cd->pctx))
1125 if (check_filetype(ctx, dirent, ino, &cd->pctx))
1130 ext2fs_dirhash(dx_dir->hashversion, dirent->name,
1131 (dirent->name_len & 0xFF),
1132 fs->super->s_hash_seed, &hash, 0);
1133 if (hash < dx_db->min_hash)
1134 dx_db->min_hash = hash;
1135 if (hash > dx_db->max_hash)
1136 dx_db->max_hash = hash;
1141 * If this is a directory, then mark its parent in its
1142 * dir_info structure. If the parent field is already
1143 * filled in, then this directory has more than one
1144 * hard link. We assume the first link is correct,
1145 * and ask the user if he/she wants to clear this one.
1147 if ((dot_state > 1) &&
1148 (ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
1150 if (e2fsck_dir_info_get_parent(ctx, dirent->inode,
1152 cd->pctx.ino = dirent->inode;
1153 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
1154 goto abort_free_dict;
1156 if (subdir_parent) {
1157 cd->pctx.ino2 = subdir_parent;
1158 if (fix_problem(ctx, PR_2_LINK_DIR,
1166 (void) e2fsck_dir_info_set_parent(ctx,
1167 dirent->inode, ino);
1173 } else if (dict_lookup(&de_dict, dirent)) {
1174 clear_problem_context(&pctx);
1176 pctx.dirent = dirent;
1177 fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
1178 if (!ctx->dirs_to_hash)
1179 ext2fs_u32_list_create(&ctx->dirs_to_hash, 50);
1180 if (ctx->dirs_to_hash)
1181 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1184 dict_alloc_insert(&de_dict, dirent, dirent);
1186 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1189 ctx->fs_links_count++;
1190 ctx->fs_total_count++;
1194 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1197 } while (offset < fs->blocksize);
1204 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1205 db->blockcnt, dx_db->type,
1206 dx_db->min_hash, dx_db->max_hash);
1208 cd->pctx.dir = cd->pctx.ino;
1209 if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1210 (dx_db->type == DX_DIRBLOCK_NODE))
1211 parse_int_node(fs, db, cd, dx_dir, buf);
1213 #endif /* ENABLE_HTREE */
1214 if (offset != fs->blocksize) {
1215 cd->pctx.num = rec_len - fs->blocksize + offset;
1216 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1217 dirent->rec_len = cd->pctx.num;
1222 cd->pctx.errcode = ext2fs_write_dir_block3(fs, block_nr, buf, 0);
1223 if (cd->pctx.errcode) {
1224 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
1226 goto abort_free_dict;
1228 ext2fs_mark_changed(fs);
1230 dict_free_nodes(&de_dict);
1233 ctx->flags |= E2F_FLAG_ABORT;
1234 dict_free_nodes(&de_dict);
1235 return DIRENT_ABORT;
1244 * This function is called to deallocate a block, and is an interator
1245 * functioned called by deallocate inode via ext2fs_iterate_block().
1247 static int deallocate_inode_block(ext2_filsys fs,
1249 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1250 blk64_t ref_block EXT2FS_ATTR((unused)),
1251 int ref_offset EXT2FS_ATTR((unused)),
1254 struct del_block *p = priv_data;
1256 if (HOLE_BLKADDR(*block_nr))
1258 if ((*block_nr < fs->super->s_first_data_block) ||
1259 (*block_nr >= ext2fs_blocks_count(fs->super)))
1261 if ((*block_nr % EXT2FS_CLUSTER_RATIO(fs)) == 0)
1262 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
1268 * This fuction deallocates an inode
1270 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
1272 ext2_filsys fs = ctx->fs;
1273 struct ext2_inode inode;
1274 struct problem_context pctx;
1276 struct del_block del_block;
1279 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1280 /* ext2fs_block_iterate2() depends on the extents flags */
1281 if (inode.i_flags & EXT4_EXTENTS_FL)
1283 e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
1285 inode.i_flags |= EXT4_EXTENTS_FL;
1286 e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
1288 clear_problem_context(&pctx);
1292 * Fix up the bitmaps...
1294 e2fsck_read_bitmaps(ctx);
1295 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1297 if (ext2fs_file_acl_block(fs, &inode) &&
1298 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1299 pctx.errcode = ext2fs_adjust_ea_refcount2(fs,
1300 ext2fs_file_acl_block(fs, &inode),
1301 block_buf, -1, &count);
1302 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1307 pctx.blk = ext2fs_file_acl_block(fs, &inode);
1308 fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1309 ctx->flags |= E2F_FLAG_ABORT;
1313 ext2fs_block_alloc_stats2(fs,
1314 ext2fs_file_acl_block(fs, &inode), -1);
1316 if (ctx->inode_badness)
1317 ext2fs_icount_store(ctx->inode_badness, ino, 0);
1318 ext2fs_file_acl_block_set(fs, &inode, 0);
1321 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1324 if (LINUX_S_ISREG(inode.i_mode) &&
1325 ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode)))
1328 del_block.ctx = ctx;
1330 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1331 deallocate_inode_block,
1334 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
1335 ctx->flags |= E2F_FLAG_ABORT;
1339 /* Inode may have changed by block_iterate, so reread it */
1340 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1341 e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
1345 * This fuction clears the htree flag on an inode
1347 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1349 struct ext2_inode inode;
1351 e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1352 inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1353 e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
1354 if (ctx->dirs_to_hash)
1355 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1359 int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1360 ext2_ino_t ino, char *buf)
1362 ext2_filsys fs = ctx->fs;
1363 struct ext2_inode inode;
1364 int inode_modified = 0;
1366 unsigned char *frag, *fsize;
1367 struct problem_context pctx;
1368 problem_t problem = 0;
1371 if (ctx->inode_badness)
1372 ext2fs_icount_fetch(ctx->inode_badness, ino, &badness);
1373 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
1375 clear_problem_context(&pctx);
1378 pctx.inode = &inode;
1380 if (ext2fs_file_acl_block(fs, &inode) &&
1381 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1382 if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
1383 ext2fs_file_acl_block_set(fs, &inode, 0);
1387 badness += BADNESS_NORMAL;
1390 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1391 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1392 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
1393 !(LINUX_S_ISSOCK(inode.i_mode)))
1394 problem = PR_2_BAD_MODE;
1395 else if (LINUX_S_ISCHR(inode.i_mode)
1396 && !e2fsck_pass1_check_device_inode(fs, &inode))
1397 problem = PR_2_BAD_CHAR_DEV;
1398 else if (LINUX_S_ISBLK(inode.i_mode)
1399 && !e2fsck_pass1_check_device_inode(fs, &inode))
1400 problem = PR_2_BAD_BLOCK_DEV;
1401 else if (LINUX_S_ISFIFO(inode.i_mode)
1402 && !e2fsck_pass1_check_device_inode(fs, &inode))
1403 problem = PR_2_BAD_FIFO;
1404 else if (LINUX_S_ISSOCK(inode.i_mode)
1405 && !e2fsck_pass1_check_device_inode(fs, &inode))
1406 problem = PR_2_BAD_SOCKET;
1407 else if (LINUX_S_ISLNK(inode.i_mode) &&
1408 !e2fsck_pass1_check_symlink(ctx, ino, &inode, buf)) {
1409 problem = PR_2_INVALID_SYMLINK;
1413 if (fix_problem(ctx, problem, &pctx)) {
1414 deallocate_inode(ctx, ino, 0);
1415 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1422 * A high value is associated with bad mode in order to detect
1423 * that mode was corrupt in check_filetype()
1425 badness += BADNESS_BAD_MODE;
1428 if (inode.i_faddr) {
1429 if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1434 badness += BADNESS_NORMAL;
1437 switch (fs->super->s_creator_os) {
1439 frag = &inode.osd2.hurd2.h_i_frag;
1440 fsize = &inode.osd2.hurd2.h_i_fsize;
1445 if (frag && *frag) {
1447 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
1452 badness += BADNESS_NORMAL;
1455 if (fsize && *fsize) {
1457 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
1462 badness += BADNESS_NORMAL;
1466 /* In pass1 these conditions were used to mark inode bad so that
1467 * it calls e2fsck_process_bad_inode and make an extensive check
1468 * plus prompt for action to be taken. To compensate for badness
1469 * incremented in pass1 by this condition, decrease it.
1471 if ((inode.i_faddr || frag || fsize ||
1472 (LINUX_S_ISDIR(inode.i_mode) && inode.i_dir_acl)) ||
1473 (inode.i_file_acl &&
1474 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1475 (inode.i_file_acl < fs->super->s_first_data_block) ||
1476 (inode.i_file_acl >= fs->super->s_blocks_count)))) {
1477 /* badness can be 0 if called from pass4. */
1479 badness -= BADNESS_NORMAL;
1482 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1483 !(fs->super->s_feature_ro_compat &
1484 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1485 (inode.osd2.linux2.l_i_blocks_hi != 0)) {
1486 pctx.num = inode.osd2.linux2.l_i_blocks_hi;
1487 if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) {
1488 inode.osd2.linux2.l_i_blocks_hi = 0;
1491 /* Badness was increased in pass1 for this condition */
1492 /* badness += BADNESS_NORMAL; */
1495 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1496 !(fs->super->s_feature_incompat &
1497 EXT4_FEATURE_INCOMPAT_64BIT) &&
1498 inode.osd2.linux2.l_i_file_acl_high != 0) {
1499 pctx.num = inode.osd2.linux2.l_i_file_acl_high;
1500 if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) {
1501 inode.osd2.linux2.l_i_file_acl_high = 0;
1505 badness += BADNESS_NORMAL;
1508 if (ext2fs_file_acl_block(fs, &inode) &&
1509 ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) ||
1510 (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) {
1511 if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
1512 ext2fs_file_acl_block_set(fs, &inode, 0);
1516 badness += BADNESS_NORMAL;
1518 if (inode.i_dir_acl &&
1519 LINUX_S_ISDIR(inode.i_mode)) {
1520 if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1521 inode.i_dir_acl = 0;
1525 badness += BADNESS_NORMAL;
1529 * The high value due to BADNESS_BAD_MODE should not delete the inode.
1531 if (ctx->inode_badness && (badness - (badness >= BADNESS_BAD_MODE ?
1532 BADNESS_BAD_MODE : 0)) >=
1533 ctx->inode_badness_threshold) {
1535 if (fix_problem(ctx, PR_2_INODE_TOOBAD, &pctx)) {
1536 deallocate_inode(ctx, ino, 0);
1537 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1545 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
1546 if (ctx->inode_badness)
1547 ext2fs_icount_store(ctx->inode_badness, ino, 0);
1553 * allocate_dir_block --- this function allocates a new directory
1554 * block for a particular inode; this is done if a directory has
1555 * a "hole" in it, or if a directory has a illegal block number
1556 * that was zeroed out and now needs to be replaced.
1558 static int allocate_dir_block(e2fsck_t ctx,
1559 struct ext2_db_entry2 *db,
1560 char *buf EXT2FS_ATTR((unused)),
1561 struct problem_context *pctx)
1563 ext2_filsys fs = ctx->fs;
1566 struct ext2_inode inode;
1568 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
1572 * Read the inode and block bitmaps in; we'll be messing with
1575 e2fsck_read_bitmaps(ctx);
1578 * First, find a free block
1580 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1581 pctx->errcode = ext2fs_map_cluster_block(fs, db->ino, &inode,
1582 db->blockcnt, &blk);
1583 if (pctx->errcode || blk == 0) {
1584 pctx->errcode = ext2fs_new_block2(fs, 0,
1585 ctx->block_found_map, &blk);
1586 if (pctx->errcode) {
1587 pctx->str = "ext2fs_new_block";
1588 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1592 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1593 ext2fs_mark_block_bitmap2(fs->block_map, blk);
1594 ext2fs_mark_bb_dirty(fs);
1597 * Now let's create the actual data block for the inode
1600 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
1602 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1603 EXT2_ROOT_INO, &block);
1605 if (pctx->errcode) {
1606 pctx->str = "ext2fs_new_dir_block";
1607 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1611 pctx->errcode = ext2fs_write_dir_block3(fs, blk, block, 0);
1612 ext2fs_free_mem(&block);
1613 if (pctx->errcode) {
1614 pctx->str = "ext2fs_write_dir_block";
1615 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1620 * Update the inode block count
1622 ext2fs_iblk_add_blocks(fs, &inode, 1);
1623 if (EXT2_I_SIZE(&inode) < (db->blockcnt+1) * fs->blocksize) {
1624 pctx->errcode = ext2fs_inode_size_set(fs, &inode,
1625 (db->blockcnt+1) * fs->blocksize);
1626 if (pctx->errcode) {
1627 pctx->str = "ext2fs_inode_size_set";
1628 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1632 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
1635 * Finally, update the block pointers for the inode
1638 pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET,
1639 db->blockcnt, 0, &blk);
1640 if (pctx->errcode) {
1641 pctx->str = "ext2fs_block_iterate";
1642 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);