2 * pass2.c --- check directory structure
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o
7 * This file may be redistributed under the terms of the GNU Public
11 * Pass 2 of e2fsck iterates through all active directory inodes, and
12 * applies to following tests to each directory entry in the directory
13 * blocks in the inodes:
15 * - The length of the directory entry (rec_len) should be at
16 * least 8 bytes, and no more than the remaining space
17 * left in the directory block.
18 * - The length of the name in the directory entry (name_len)
19 * should be less than (rec_len - 8).
20 * - The inode number in the directory entry should be within
22 * - The inode number should refer to a in-use inode.
23 * - The first entry should be '.', and its inode should be
24 * the inode of the directory.
25 * - The second entry should be '..'.
27 * To minimize disk seek time, the directory blocks are processed in
28 * sorted order of block numbers.
30 * Pass 2 also collects the following information:
31 * - The inode numbers of the subdirectories for each directory.
33 * Pass 2 relies on the following information from previous passes:
34 * - The directory information collected in pass 1.
35 * - The inode_used_map bitmap
36 * - The inode_bad_map bitmap
37 * - The inode_dir_map bitmap
39 * Pass 2 frees the following data structures
40 * - The inode_bad_map bitmap
41 * - The inode_reg_map bitmap
44 #define _GNU_SOURCE 1 /* get strnlen() */
52 #ifdef NO_INLINE_FUNCS
55 #define _INLINE_ inline
58 /* #define DX_DEBUG */
61 * Keeps track of how many times an inode is referenced.
63 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf);
64 static int check_dir_block2(ext2_filsys fs,
65 struct ext2_db_entry2 *dir_blocks_info,
67 static int check_dir_block(ext2_filsys fs,
68 struct ext2_db_entry2 *dir_blocks_info,
70 static int allocate_dir_block(e2fsck_t ctx,
71 struct ext2_db_entry2 *dir_blocks_info,
72 char *buf, struct problem_context *pctx);
73 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino);
74 static int htree_depth(struct dx_dir_info *dx_dir,
75 struct dx_dirblock_info *dx_db);
76 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b);
78 struct check_dir_struct {
80 struct problem_context pctx;
83 unsigned long long list_offset;
84 unsigned long long ra_entries;
85 unsigned long long next_ra_off;
88 void e2fsck_pass2(e2fsck_t ctx)
90 struct ext2_super_block *sb = ctx->fs->super;
91 struct problem_context pctx;
92 ext2_filsys fs = ctx->fs;
95 struct resource_track rtrack;
97 struct check_dir_struct cd;
98 struct dx_dir_info *dx_dir;
99 struct dx_dirblock_info *dx_db, *dx_parent;
100 unsigned int save_type;
105 int (*check_dir_func)(ext2_filsys fs,
106 struct ext2_db_entry2 *dir_blocks_info,
109 init_resource_track(&rtrack, ctx->fs->io);
110 clear_problem_context(&cd.pctx);
113 mtrace_print("Pass 2");
116 if (!(ctx->options & E2F_OPT_PREEN))
117 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
119 e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT,
121 if (ctx->inode_count)
124 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
125 "inode_count", &save_type);
126 cd.pctx.errcode = ext2fs_create_icount2(fs,
127 EXT2_ICOUNT_OPT_INCREMENT,
128 0, ctx->inode_link_info,
130 fs->default_bitmap_type = save_type;
132 if (cd.pctx.errcode) {
133 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
134 ctx->flags |= E2F_FLAG_ABORT;
137 buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
138 "directory scan buffer");
141 * Set up the parent pointer for the root directory, if
142 * present. (If the root directory is not present, we will
143 * create it in pass 3.)
145 (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
150 cd.max = ext2fs_dblist_count2(fs->dblist);
152 cd.ra_entries = ctx->readahead_kb * 1024 / ctx->fs->blocksize;
156 (void) (ctx->progress)(ctx, 2, 0, cd.max);
158 if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX)
159 ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp);
161 check_dir_func = cd.ra_entries ? check_dir_block2 : check_dir_block;
162 cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_func,
164 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
167 if (ctx->flags & E2F_FLAG_RESTART_LATER) {
168 ctx->flags |= E2F_FLAG_RESTART;
172 if (cd.pctx.errcode) {
173 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
174 ctx->flags |= E2F_FLAG_ABORT;
178 for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
179 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
181 if (e2fsck_dir_will_be_rehashed(ctx, dx_dir->ino) ||
182 dx_dir->numblocks == 0)
184 clear_problem_context(&pctx);
186 pctx.dir = dx_dir->ino;
187 dx_db = dx_dir->dx_block;
188 if (dx_db->flags & DX_FLAG_REFERENCED)
189 dx_db->flags |= DX_FLAG_DUP_REF;
191 dx_db->flags |= DX_FLAG_REFERENCED;
193 * Find all of the first and last leaf blocks, and
194 * update their parent's min and max hash values
196 for (b=0, dx_db = dx_dir->dx_block;
197 b < dx_dir->numblocks;
199 if ((dx_db->type != DX_DIRBLOCK_LEAF) ||
200 !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST)))
202 dx_parent = &dx_dir->dx_block[dx_db->parent];
204 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
206 if (dx_db->flags & DX_FLAG_FIRST)
207 dx_parent->min_hash = dx_db->min_hash;
209 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
211 if (dx_db->flags & DX_FLAG_LAST)
212 dx_parent->max_hash = dx_db->max_hash;
215 for (b=0, dx_db = dx_dir->dx_block;
216 b < dx_dir->numblocks;
219 pctx.group = dx_db->parent;
221 if (!(dx_db->flags & DX_FLAG_FIRST) &&
222 (dx_db->min_hash < dx_db->node_min_hash)) {
223 pctx.blk = dx_db->min_hash;
224 pctx.blk2 = dx_db->node_min_hash;
225 code = PR_2_HTREE_MIN_HASH;
226 fix_problem(ctx, code, &pctx);
229 if (dx_db->type == DX_DIRBLOCK_LEAF) {
230 depth = htree_depth(dx_dir, dx_db);
231 if (depth != dx_dir->depth) {
232 pctx.num = dx_dir->depth;
233 code = PR_2_HTREE_BAD_DEPTH;
234 fix_problem(ctx, code, &pctx);
239 * This test doesn't apply for the root block
243 (dx_db->max_hash > dx_db->node_max_hash)) {
244 pctx.blk = dx_db->max_hash;
245 pctx.blk2 = dx_db->node_max_hash;
246 code = PR_2_HTREE_MAX_HASH;
247 fix_problem(ctx, code, &pctx);
250 if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
251 code = PR_2_HTREE_NOTREF;
252 fix_problem(ctx, code, &pctx);
254 } else if (dx_db->flags & DX_FLAG_DUP_REF) {
255 code = PR_2_HTREE_DUPREF;
256 fix_problem(ctx, code, &pctx);
260 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
261 clear_htree(ctx, dx_dir->ino);
262 dx_dir->numblocks = 0;
265 e2fsck_free_dx_dir_info(ctx);
267 ext2fs_free_mem(&buf);
268 ext2fs_free_dblist(fs->dblist);
270 if (ctx->inode_bad_map) {
271 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
272 ctx->inode_bad_map = 0;
274 if (ctx->inode_reg_map) {
275 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
276 ctx->inode_reg_map = 0;
278 if (ctx->encrypted_dirs) {
279 ext2fs_u32_list_free(ctx->encrypted_dirs);
280 ctx->encrypted_dirs = 0;
283 clear_problem_context(&pctx);
284 if (ctx->large_files) {
285 if (!(sb->s_feature_ro_compat &
286 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
287 fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
288 sb->s_feature_ro_compat |=
289 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
290 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
291 ext2fs_mark_super_dirty(fs);
293 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
294 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
295 ext2fs_update_dynamic_rev(fs);
296 ext2fs_mark_super_dirty(fs);
300 print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io);
303 #define MAX_DEPTH 32000
304 static int htree_depth(struct dx_dir_info *dx_dir,
305 struct dx_dirblock_info *dx_db)
309 while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
310 dx_db = &dx_dir->dx_block[dx_db->parent];
316 static int dict_de_cmp(const void *a, const void *b)
318 const struct ext2_dir_entry *de_a, *de_b;
321 de_a = (const struct ext2_dir_entry *) a;
322 a_len = ext2fs_dirent_name_len(de_a);
323 de_b = (const struct ext2_dir_entry *) b;
324 b_len = ext2fs_dirent_name_len(de_b);
327 return (a_len - b_len);
329 return memcmp(de_a->name, de_b->name, a_len);
333 * This is special sort function that makes sure that directory blocks
334 * with a dirblock of zero are sorted to the beginning of the list.
335 * This guarantees that the root node of the htree directories are
336 * processed first, so we know what hash version to use.
338 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
340 const struct ext2_db_entry2 *db_a =
341 (const struct ext2_db_entry2 *) a;
342 const struct ext2_db_entry2 *db_b =
343 (const struct ext2_db_entry2 *) b;
345 if (db_a->blockcnt && !db_b->blockcnt)
348 if (!db_a->blockcnt && db_b->blockcnt)
351 if (db_a->blk != db_b->blk)
352 return (int) (db_a->blk - db_b->blk);
354 if (db_a->ino != db_b->ino)
355 return (int) (db_a->ino - db_b->ino);
357 return (int) (db_a->blockcnt - db_b->blockcnt);
362 * Make sure the first entry in the directory is '.', and that the
363 * directory entry is sane.
365 static int check_dot(e2fsck_t ctx,
366 struct ext2_dir_entry *dirent,
367 ext2_ino_t ino, struct problem_context *pctx)
369 struct ext2_dir_entry *nextdir;
370 unsigned int rec_len, new_len;
373 problem_t problem = 0;
376 problem = PR_2_MISSING_DOT;
377 else if ((ext2fs_dirent_name_len(dirent) != 1) ||
378 (dirent->name[0] != '.'))
379 problem = PR_2_1ST_NOT_DOT;
380 else if (dirent->name[1] != '\0')
381 problem = PR_2_DOT_NULL_TERM;
383 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
385 if (fix_problem(ctx, problem, pctx)) {
387 rec_len = dirent->rec_len = 12;
389 ext2fs_dirent_set_name_len(dirent, 1);
390 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
391 dirent->name[0] = '.';
392 dirent->name[1] = '\0';
397 if (dirent->inode != ino) {
398 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
404 new_len = rec_len - 12;
407 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
408 nextdir = (struct ext2_dir_entry *)
409 ((char *) dirent + 12);
410 dirent->rec_len = 12;
411 (void) ext2fs_set_rec_len(ctx->fs, new_len,
414 ext2fs_dirent_set_name_len(nextdir, 0);
415 ext2fs_dirent_set_file_type(nextdir,
425 * Make sure the second entry in the directory is '..', and that the
426 * directory entry is sane. We do not check the inode number of '..'
427 * here; this gets done in pass 3.
429 static int check_dotdot(e2fsck_t ctx,
430 struct ext2_dir_entry *dirent,
431 ext2_ino_t ino, struct problem_context *pctx)
433 problem_t problem = 0;
434 unsigned int rec_len;
437 problem = PR_2_MISSING_DOT_DOT;
438 else if ((ext2fs_dirent_name_len(dirent) != 2) ||
439 (dirent->name[0] != '.') ||
440 (dirent->name[1] != '.'))
441 problem = PR_2_2ND_NOT_DOT_DOT;
442 else if (dirent->name[2] != '\0')
443 problem = PR_2_DOT_DOT_NULL_TERM;
445 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
447 if (fix_problem(ctx, problem, pctx)) {
449 dirent->rec_len = 12;
451 * Note: we don't have the parent inode just
452 * yet, so we will fill it in with the root
453 * inode. This will get fixed in pass 3.
455 dirent->inode = EXT2_ROOT_INO;
456 ext2fs_dirent_set_name_len(dirent, 2);
457 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
458 dirent->name[0] = '.';
459 dirent->name[1] = '.';
460 dirent->name[2] = '\0';
465 if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) {
466 fix_problem(ctx, PR_2_NO_DIRINFO, pctx);
473 * Check to make sure a directory entry doesn't contain any illegal
476 static int check_name(e2fsck_t ctx,
477 struct ext2_dir_entry *dirent,
479 struct problem_context *pctx)
485 for ( i = 0; i < ext2fs_dirent_name_len(dirent); i++) {
486 if (dirent->name[i] != '/' && dirent->name[i] != '\0')
489 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
492 dirent->name[i] = '.';
499 * Check the directory filetype (if present)
501 static _INLINE_ int check_filetype(e2fsck_t ctx,
502 struct ext2_dir_entry *dirent,
503 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
504 struct problem_context *pctx)
506 int filetype = ext2fs_dirent_file_type(dirent);
507 int should_be = EXT2_FT_UNKNOWN;
508 struct ext2_inode inode;
510 if (!(ctx->fs->super->s_feature_incompat &
511 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
513 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
515 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
519 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) {
520 should_be = EXT2_FT_DIR;
521 } else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map,
523 should_be = EXT2_FT_REG_FILE;
524 } else if (ctx->inode_bad_map &&
525 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
529 e2fsck_read_inode(ctx, dirent->inode, &inode,
531 should_be = ext2_file_type(inode.i_mode);
533 if (filetype == should_be)
535 pctx->num = should_be;
537 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
541 ext2fs_dirent_set_file_type(dirent, should_be);
545 static void parse_int_node(ext2_filsys fs,
546 struct ext2_db_entry2 *db,
547 struct check_dir_struct *cd,
548 struct dx_dir_info *dx_dir,
549 char *block_buf, int failed_csum)
551 struct ext2_dx_root_info *root;
552 struct ext2_dx_entry *ent;
553 struct ext2_dx_countlimit *limit;
554 struct dx_dirblock_info *dx_db;
555 int i, expect_limit, count;
557 ext2_dirhash_t min_hash = 0xffffffff;
558 ext2_dirhash_t max_hash = 0;
559 ext2_dirhash_t hash = 0, prev_hash;
562 if (db->blockcnt == 0) {
563 root = (struct ext2_dx_root_info *) (block_buf + 24);
566 printf("Root node dump:\n");
567 printf("\t Reserved zero: %u\n", root->reserved_zero);
568 printf("\t Hash Version: %d\n", root->hash_version);
569 printf("\t Info length: %d\n", root->info_length);
570 printf("\t Indirect levels: %d\n", root->indirect_levels);
571 printf("\t Flags: %d\n", root->unused_flags);
574 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
577 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
578 fix_problem(cd->ctx, PR_2_HTREE_ROOT_CSUM_INVALID,
582 ent = (struct ext2_dx_entry *) (block_buf+8);
585 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
586 fix_problem(cd->ctx, PR_2_HTREE_NODE_CSUM_INVALID,
591 limit = (struct ext2_dx_countlimit *) ent;
594 printf("Number of entries (count): %d\n",
595 ext2fs_le16_to_cpu(limit->count));
596 printf("Number of entries (limit): %d\n",
597 ext2fs_le16_to_cpu(limit->limit));
600 count = ext2fs_le16_to_cpu(limit->count);
601 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
602 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
603 csum_size = sizeof(struct ext2_dx_tail);
604 expect_limit = (fs->blocksize -
605 (csum_size + ((char *) ent - block_buf))) /
606 sizeof(struct ext2_dx_entry);
607 if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
608 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
609 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
612 if (count > expect_limit) {
613 cd->pctx.num = count;
614 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
616 count = expect_limit;
619 for (i=0; i < count; i++) {
621 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
623 printf("Entry #%d: Hash 0x%08x, block %u\n", i,
624 hash, ext2fs_le32_to_cpu(ent[i].block));
626 blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff;
627 /* Check to make sure the block is valid */
628 if (blk >= (blk_t) dx_dir->numblocks) {
630 if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
635 if (hash < prev_hash &&
636 fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
638 dx_db = &dx_dir->dx_block[blk];
639 if (dx_db->flags & DX_FLAG_REFERENCED) {
640 dx_db->flags |= DX_FLAG_DUP_REF;
642 dx_db->flags |= DX_FLAG_REFERENCED;
643 dx_db->parent = db->blockcnt;
649 dx_db->node_min_hash = hash;
651 dx_db->node_max_hash =
652 ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
654 dx_db->node_max_hash = 0xfffffffe;
655 dx_db->flags |= DX_FLAG_LAST;
658 dx_db->flags |= DX_FLAG_FIRST;
661 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
662 db->blockcnt, min_hash, max_hash);
664 dx_db = &dx_dir->dx_block[db->blockcnt];
665 dx_db->min_hash = min_hash;
666 dx_db->max_hash = max_hash;
670 clear_htree(cd->ctx, cd->pctx.ino);
671 dx_dir->numblocks = 0;
672 e2fsck_rehash_dir_later(cd->ctx, cd->pctx.ino);
676 * Given a busted directory, try to salvage it somehow.
679 static void salvage_directory(ext2_filsys fs,
680 struct ext2_dir_entry *dirent,
681 struct ext2_dir_entry *prev,
682 unsigned int *offset,
683 unsigned int block_len)
685 char *cp = (char *) dirent;
687 unsigned int rec_len, prev_rec_len;
688 unsigned int name_len;
691 * If the space left for the entry is too small to be an entry,
692 * we can't access dirent's fields, so plumb in the values needed
693 * so that the previous entry absorbs this one.
695 if (block_len - *offset < EXT2_DIR_ENTRY_HEADER_LEN) {
697 rec_len = block_len - *offset;
699 name_len = ext2fs_dirent_name_len(dirent);
700 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
702 left = block_len - *offset - rec_len;
705 * Special case of directory entry of size 8: copy what's left
706 * of the directory block up to cover up the invalid hole.
708 if ((left >= 12) && (rec_len == EXT2_DIR_ENTRY_HEADER_LEN)) {
709 memmove(cp, cp+EXT2_DIR_ENTRY_HEADER_LEN, left);
710 memset(cp + left, 0, EXT2_DIR_ENTRY_HEADER_LEN);
714 * If the directory entry overruns the end of the directory
715 * block, and the name is small enough to fit, then adjust the
719 ((int) rec_len + left > EXT2_DIR_ENTRY_HEADER_LEN) &&
720 ((int) name_len + EXT2_DIR_ENTRY_HEADER_LEN <= (int) rec_len + left) &&
721 dirent->inode <= fs->super->s_inodes_count &&
722 strnlen(dirent->name, name_len) == name_len) {
723 (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent);
727 * If the record length of the directory entry is a multiple
728 * of four, and not too big, such that it is valid, let the
729 * previous directory entry absorb the invalid one.
731 if (prev && rec_len && (rec_len % 4) == 0 &&
732 (*offset + rec_len <= block_len)) {
733 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
734 prev_rec_len += rec_len;
735 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
740 * Default salvage method --- kill all of the directory
741 * entries for the rest of the block. We will either try to
742 * absorb it into the previous directory entry, or create a
743 * new empty directory entry the rest of the directory block.
746 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
747 prev_rec_len += block_len - *offset;
748 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
749 *offset = fs->blocksize;
751 rec_len = block_len - *offset;
752 (void) ext2fs_set_rec_len(fs, rec_len, dirent);
753 ext2fs_dirent_set_name_len(dirent, 0);
754 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
759 #define NEXT_DIRENT(d) ((void *)((char *)(d) + (d)->rec_len))
760 static errcode_t insert_dirent_tail(ext2_filsys fs, void *dirbuf)
762 struct ext2_dir_entry *d;
764 struct ext2_dir_entry_tail *t;
767 top = EXT2_DIRENT_TAIL(dirbuf, fs->blocksize);
769 while (d->rec_len && !(d->rec_len & 0x3) && NEXT_DIRENT(d) <= top)
773 size_t min_size = EXT2_DIR_REC_LEN(
774 ext2fs_dirent_name_len(dirbuf));
775 if (min_size > top - (void *)d)
776 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
777 d->rec_len = top - (void *)d;
780 t = (struct ext2_dir_entry_tail *)top;
781 if (t->det_reserved_zero1 ||
782 t->det_rec_len != sizeof(struct ext2_dir_entry_tail) ||
783 t->det_reserved_name_len != EXT2_DIR_NAME_LEN_CSUM)
784 ext2fs_initialize_dirent_tail(fs, t);
790 static errcode_t fix_inline_dir_size(e2fsck_t ctx, ext2_ino_t ino,
791 size_t *inline_data_size,
792 struct problem_context *pctx,
795 ext2_filsys fs = ctx->fs;
796 struct ext2_inode inode;
797 size_t new_size, old_size;
800 old_size = *inline_data_size;
802 * If there's not enough bytes to start the "second" dir block
803 * (in the EA space) then truncate everything to the first block.
805 if (old_size > EXT4_MIN_INLINE_DATA_SIZE &&
806 old_size < EXT4_MIN_INLINE_DATA_SIZE +
807 EXT2_DIR_REC_LEN(1)) {
808 old_size = EXT4_MIN_INLINE_DATA_SIZE;
811 /* Increase to the next four-byte boundary for salvaging */
812 new_size = old_size + (4 - (old_size & 3));
813 memset(buf + old_size, 0, new_size - old_size);
814 retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size);
815 if (retval == EXT2_ET_INLINE_DATA_NO_SPACE) {
816 /* Or we can't, so truncate. */
818 retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size);
820 if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED,
828 if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED,
837 retval = ext2fs_read_inode(fs, ino, &inode);
841 retval = ext2fs_inode_size_set(fs, &inode, new_size);
845 inode.i_flags &= ~EXT4_INLINE_DATA_FL;
846 retval = ext2fs_write_inode(fs, ino, &inode);
849 *inline_data_size = new_size;
855 static int check_dir_block2(ext2_filsys fs,
856 struct ext2_db_entry2 *db,
860 struct check_dir_struct *cd = priv_data;
862 if (cd->ra_entries && cd->list_offset >= cd->next_ra_off) {
863 err = e2fsck_readahead_dblist(fs,
864 E2FSCK_RA_DBLIST_IGNORE_BLOCKCNT,
866 cd->list_offset + cd->ra_entries / 8,
870 cd->next_ra_off = cd->list_offset + (cd->ra_entries * 7 / 8);
873 err = check_dir_block(fs, db, priv_data);
878 static int check_dir_block(ext2_filsys fs,
879 struct ext2_db_entry2 *db,
882 struct dx_dir_info *dx_dir;
883 struct dx_dirblock_info *dx_db = 0;
884 struct ext2_dir_entry *dirent, *prev, dot, dotdot;
886 unsigned int offset = 0;
887 int dir_modified = 0;
889 unsigned int rec_len;
890 blk64_t block_nr = db->blk;
891 ext2_ino_t ino = db->ino;
892 ext2_ino_t subdir_parent;
894 struct check_dir_struct *cd;
898 struct ext2_dx_root_info *root;
899 struct ext2_dx_countlimit *limit;
900 static dict_t de_dict;
901 struct problem_context pctx;
904 int dx_csum_size = 0, de_csum_size = 0;
907 size_t inline_data_size = 0;
910 size_t max_block_size;
912 cd = (struct check_dir_struct *) priv_data;
913 ibuf = buf = cd->buf;
916 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
919 if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
922 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
923 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
924 dx_csum_size = sizeof(struct ext2_dx_tail);
925 de_csum_size = sizeof(struct ext2_dir_entry_tail);
928 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
929 EXT2_FEATURE_INCOMPAT_FILETYPE))
930 filetype = EXT2_FT_DIR << 8;
933 * Make sure the inode is still in use (could have been
934 * deleted in the duplicate/bad blocks pass.
936 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino)))
940 cd->pctx.blk = block_nr;
941 cd->pctx.blkcount = db->blockcnt;
946 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
947 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
950 ec = ext2fs_inline_data_size(fs, ino, &inline_data_size);
951 if (ec && ec != EXT2_ET_NO_INLINE_DATA)
955 if (db->blk == 0 && !inline_data_size) {
956 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
966 if (ctx->dirs_to_hash &&
967 ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
971 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
975 ehandler_operation(_("reading directory block"));
976 if (inline_data_size) {
977 memset(buf, 0, fs->blocksize - inline_data_size);
978 cd->pctx.errcode = ext2fs_inline_data_get(fs, ino, 0, buf, 0);
979 if (cd->pctx.errcode)
980 goto inline_read_fail;
981 #ifdef WORDS_BIGENDIAN
983 goto skip_first_read_swab;
984 *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf));
985 cd->pctx.errcode = ext2fs_dirent_swab_in2(fs,
986 buf + EXT4_INLINE_DATA_DOTDOT_SIZE,
987 EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DATA_DOTDOT_SIZE,
989 if (cd->pctx.errcode)
990 goto inline_read_fail;
991 skip_first_read_swab:
992 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE ||
994 goto inline_read_fail;
995 cd->pctx.errcode = ext2fs_dirent_swab_in2(fs,
996 buf + EXT4_MIN_INLINE_DATA_SIZE,
997 inline_data_size - EXT4_MIN_INLINE_DATA_SIZE,
1001 cd->pctx.errcode = ext2fs_read_dir_block4(fs, block_nr,
1005 pctx.num = inline_data_size;
1006 if (((inline_data_size & 3) ||
1007 (inline_data_size > EXT4_MIN_INLINE_DATA_SIZE &&
1008 inline_data_size < EXT4_MIN_INLINE_DATA_SIZE +
1009 EXT2_DIR_REC_LEN(1))) &&
1010 fix_problem(ctx, PR_2_BAD_INLINE_DIR_SIZE, &pctx)) {
1011 errcode_t err = fix_inline_dir_size(ctx, ino,
1012 &inline_data_size, &pctx,
1015 return DIRENT_ABORT;
1018 ehandler_operation(0);
1019 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
1020 cd->pctx.errcode = 0; /* We'll handle this ourselves */
1021 else if (cd->pctx.errcode == EXT2_ET_DIR_CSUM_INVALID) {
1022 cd->pctx.errcode = 0; /* We'll handle this ourselves */
1025 if (cd->pctx.errcode) {
1027 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
1028 ctx->flags |= E2F_FLAG_ABORT;
1029 return DIRENT_ABORT;
1031 ext2fs_new_dir_block(fs, db->blockcnt == 0 ? ino : 0,
1032 EXT2_ROOT_INO, &buf2);
1033 memcpy(buf, buf2, fs->blocksize);
1034 ext2fs_free_mem(&buf2);
1036 dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
1037 if (dx_dir && dx_dir->numblocks) {
1038 if (db->blockcnt >= dx_dir->numblocks) {
1040 if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK,
1042 clear_htree(ctx, ino);
1043 dx_dir->numblocks = 0;
1047 fatal_error(ctx, _("Can not continue."));
1049 dx_db = &dx_dir->dx_block[db->blockcnt];
1050 dx_db->type = DX_DIRBLOCK_LEAF;
1051 dx_db->phys = block_nr;
1052 dx_db->min_hash = ~0;
1053 dx_db->max_hash = 0;
1055 dirent = (struct ext2_dir_entry *) buf;
1056 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1057 limit = (struct ext2_dx_countlimit *) (buf+8);
1058 if (db->blockcnt == 0) {
1059 root = (struct ext2_dx_root_info *) (buf + 24);
1060 dx_db->type = DX_DIRBLOCK_ROOT;
1061 dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
1062 if ((root->reserved_zero ||
1063 root->info_length < 8 ||
1064 root->indirect_levels > 1) &&
1065 fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
1066 clear_htree(ctx, ino);
1067 dx_dir->numblocks = 0;
1070 dx_dir->hashversion = root->hash_version;
1071 if ((dx_dir->hashversion <= EXT2_HASH_TEA) &&
1072 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
1073 dx_dir->hashversion += 3;
1074 dx_dir->depth = root->indirect_levels + 1;
1075 } else if ((dirent->inode == 0) &&
1076 (rec_len == fs->blocksize) &&
1077 (ext2fs_dirent_name_len(dirent) == 0) &&
1078 (ext2fs_le16_to_cpu(limit->limit) ==
1079 ((fs->blocksize - (8 + dx_csum_size)) /
1080 sizeof(struct ext2_dx_entry))))
1081 dx_db->type = DX_DIRBLOCK_NODE;
1086 /* Leaf node with no space for csum? Rebuild dirs in pass 3A. */
1087 if (is_leaf && !inline_data_size && failed_csum &&
1088 !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
1090 if (e2fsck_dir_will_be_rehashed(ctx, ino)) {
1094 if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_MISSING_CSUM,
1097 e2fsck_rehash_dir_later(ctx, ino);
1101 /* htree nodes don't use fake dirents to store checksums */
1106 if (inline_data_size) {
1108 buf += EXT4_MIN_INLINE_DATA_SIZE;
1109 max_block_size = inline_data_size - EXT4_MIN_INLINE_DATA_SIZE;
1110 /* Zero-length second block, just exit */
1111 if (max_block_size == 0)
1114 max_block_size = EXT4_MIN_INLINE_DATA_SIZE;
1117 max_block_size = fs->blocksize - de_csum_size;
1119 if (ctx->encrypted_dirs)
1120 encrypted = ext2fs_u32_list_test(ctx->encrypted_dirs, ino);
1122 dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
1126 ext2_ino_t first_unused_inode;
1127 unsigned int name_len;
1130 if (!inline_data_size || dot_state > 1) {
1131 dirent = (struct ext2_dir_entry *) (buf + offset);
1133 * If there's not even space for the entry header,
1134 * force salvaging this dir.
1136 if (max_block_size - offset < EXT2_DIR_ENTRY_HEADER_LEN)
1137 rec_len = EXT2_DIR_REC_LEN(1);
1139 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1140 cd->pctx.dirent = dirent;
1141 cd->pctx.num = offset;
1142 if ((offset + rec_len > max_block_size) ||
1144 ((rec_len % 4) != 0) ||
1145 ((ext2fs_dirent_name_len(dirent) + EXT2_DIR_ENTRY_HEADER_LEN) > rec_len)) {
1146 if (fix_problem(ctx, PR_2_DIR_CORRUPTED,
1148 #ifdef WORDS_BIGENDIAN
1150 * On big-endian systems, if the dirent
1151 * swap routine finds a rec_len that it
1152 * doesn't like, it continues
1153 * processing the block as if rec_len
1154 * == EXT2_DIR_ENTRY_HEADER_LEN. This means that the name
1155 * field gets byte swapped, which means
1156 * that salvage will not detect the
1157 * correct name length (unless the name
1158 * has a length that's an exact
1159 * multiple of four bytes), and it'll
1160 * discard the entry (unnecessarily)
1161 * and the rest of the dirent block.
1162 * Therefore, swap the rest of the
1163 * block back to disk order, run
1164 * salvage, and re-swap anything after
1165 * the salvaged dirent.
1167 int need_reswab = 0;
1168 if (rec_len < EXT2_DIR_ENTRY_HEADER_LEN || rec_len % 4) {
1170 ext2fs_dirent_swab_in2(fs,
1171 ((char *)dirent) + EXT2_DIR_ENTRY_HEADER_LEN,
1172 max_block_size - offset - EXT2_DIR_ENTRY_HEADER_LEN,
1176 salvage_directory(fs, dirent, prev,
1179 #ifdef WORDS_BIGENDIAN
1181 (void) ext2fs_get_rec_len(fs,
1183 ext2fs_dirent_swab_in2(fs,
1184 ((char *)dirent) + offset + rec_len,
1185 max_block_size - offset - rec_len,
1192 goto abort_free_dict;
1195 if (dot_state == 0) {
1196 memset(&dot, 0, sizeof(dot));
1198 dirent->inode = ino;
1199 dirent->rec_len = EXT2_DIR_REC_LEN(1);
1200 dirent->name_len = 1 | filetype;
1201 dirent->name[0] = '.';
1202 } else if (dot_state == 1) {
1203 memset(&dotdot, 0, sizeof(dotdot));
1206 ((struct ext2_dir_entry *)buf)->inode;
1207 dirent->rec_len = EXT2_DIR_REC_LEN(2);
1208 dirent->name_len = 2 | filetype;
1209 dirent->name[0] = '.';
1210 dirent->name[1] = '.';
1212 fatal_error(ctx, _("Can not continue."));
1214 cd->pctx.dirent = dirent;
1215 cd->pctx.num = offset;
1218 if (dot_state == 0) {
1219 if (check_dot(ctx, dirent, ino, &cd->pctx))
1221 } else if (dot_state == 1) {
1222 ret = check_dotdot(ctx, dirent, ino, &cd->pctx);
1224 goto abort_free_dict;
1227 } else if (dirent->inode == ino) {
1228 problem = PR_2_LINK_DOT;
1229 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
1239 * Make sure the inode listed is a legal one.
1241 name_len = ext2fs_dirent_name_len(dirent);
1242 if (((dirent->inode != EXT2_ROOT_INO) &&
1243 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
1244 (dirent->inode > fs->super->s_inodes_count)) {
1245 problem = PR_2_BAD_INO;
1246 } else if (ctx->inode_bb_map &&
1247 (ext2fs_test_inode_bitmap2(ctx->inode_bb_map,
1250 * If the inode is in a bad block, offer to
1253 problem = PR_2_BB_INODE;
1254 } else if ((dot_state > 1) && (name_len == 1) &&
1255 (dirent->name[0] == '.')) {
1257 * If there's a '.' entry in anything other
1258 * than the first directory entry, it's a
1259 * duplicate entry that should be removed.
1261 problem = PR_2_DUP_DOT;
1262 } else if ((dot_state > 1) && (name_len == 2) &&
1263 (dirent->name[0] == '.') &&
1264 (dirent->name[1] == '.')) {
1266 * If there's a '..' entry in anything other
1267 * than the second directory entry, it's a
1268 * duplicate entry that should be removed.
1270 problem = PR_2_DUP_DOT_DOT;
1271 } else if ((dot_state > 1) &&
1272 (dirent->inode == EXT2_ROOT_INO)) {
1274 * Don't allow links to the root directory.
1275 * We check this specially to make sure we
1276 * catch this error case even if the root
1277 * directory hasn't been created yet.
1279 problem = PR_2_LINK_ROOT;
1280 } else if ((dot_state > 1) && (name_len == 0)) {
1282 * Don't allow zero-length directory names.
1284 problem = PR_2_NULL_NAME;
1288 if (fix_problem(ctx, problem, &cd->pctx)) {
1293 ext2fs_unmark_valid(fs);
1294 if (problem == PR_2_BAD_INO)
1300 * If the inode was marked as having bad fields in
1301 * pass1, process it and offer to fix/clear it.
1302 * (We wait until now so that we can display the
1303 * pathname to the user.)
1305 if (ctx->inode_bad_map &&
1306 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
1308 if (e2fsck_process_bad_inode(ctx, ino,
1310 buf + fs->blocksize)) {
1315 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1316 return DIRENT_ABORT;
1319 group = ext2fs_group_of_ino(fs, dirent->inode);
1320 first_unused_inode = group * fs->super->s_inodes_per_group +
1321 1 + fs->super->s_inodes_per_group -
1322 ext2fs_bg_itable_unused(fs, group);
1323 cd->pctx.group = group;
1326 * Check if the inode was missed out because
1327 * _INODE_UNINIT flag was set or bg_itable_unused was
1328 * incorrect. If so, clear the _INODE_UNINIT flag and
1329 * restart e2fsck. In the future it would be nice if
1330 * we could call a function in pass1.c that checks the
1331 * newly visible inodes.
1333 if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) {
1334 pctx.num = dirent->inode;
1335 if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT,
1337 ext2fs_bg_flags_clear(fs, group,
1338 EXT2_BG_INODE_UNINIT);
1339 ext2fs_mark_super_dirty(fs);
1340 ctx->flags |= E2F_FLAG_RESTART_LATER;
1342 ext2fs_unmark_valid(fs);
1343 if (problem == PR_2_BAD_INO)
1346 } else if (dirent->inode >= first_unused_inode) {
1347 pctx.num = dirent->inode;
1348 if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){
1349 ext2fs_bg_itable_unused_set(fs, group, 0);
1350 ext2fs_mark_super_dirty(fs);
1351 ctx->flags |= E2F_FLAG_RESTART_LATER;
1353 ext2fs_unmark_valid(fs);
1354 if (problem == PR_2_BAD_INO)
1360 * Offer to clear unused inodes; if we are going to be
1361 * restarting the scan due to bg_itable_unused being
1362 * wrong, then don't clear any inodes to avoid zapping
1363 * inodes that were skipped during pass1 due to an
1364 * incorrect bg_itable_unused; we'll get any real
1365 * problems after we restart.
1367 if (!(ctx->flags & E2F_FLAG_RESTART_LATER) &&
1368 !(ext2fs_test_inode_bitmap2(ctx->inode_used_map,
1370 problem = PR_2_UNUSED_INODE;
1373 if (fix_problem(ctx, problem, &cd->pctx)) {
1378 ext2fs_unmark_valid(fs);
1379 if (problem == PR_2_BAD_INO)
1384 if (!encrypted && check_name(ctx, dirent, ino, &cd->pctx))
1387 if (check_filetype(ctx, dirent, ino, &cd->pctx))
1391 ext2fs_dirhash(dx_dir->hashversion, dirent->name,
1392 ext2fs_dirent_name_len(dirent),
1393 fs->super->s_hash_seed, &hash, 0);
1394 if (hash < dx_db->min_hash)
1395 dx_db->min_hash = hash;
1396 if (hash > dx_db->max_hash)
1397 dx_db->max_hash = hash;
1401 * If this is a directory, then mark its parent in its
1402 * dir_info structure. If the parent field is already
1403 * filled in, then this directory has more than one
1404 * hard link. We assume the first link is correct,
1405 * and ask the user if he/she wants to clear this one.
1407 if ((dot_state > 1) &&
1408 (ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
1410 if (e2fsck_dir_info_get_parent(ctx, dirent->inode,
1412 cd->pctx.ino = dirent->inode;
1413 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
1414 goto abort_free_dict;
1416 if (subdir_parent) {
1417 cd->pctx.ino2 = subdir_parent;
1418 if (fix_problem(ctx, PR_2_LINK_DIR,
1426 (void) e2fsck_dir_info_set_parent(ctx,
1427 dirent->inode, ino);
1433 } else if (dict_lookup(&de_dict, dirent)) {
1434 clear_problem_context(&pctx);
1436 pctx.dirent = dirent;
1437 fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
1438 e2fsck_rehash_dir_later(ctx, ino);
1441 dict_alloc_insert(&de_dict, dirent, dirent);
1443 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1446 ctx->fs_links_count++;
1447 ctx->fs_total_count++;
1451 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1452 if (!inline_data_size || dot_state > 1) {
1455 if (dot_state == 1) {
1458 * If we get here, we're checking an inline
1459 * directory and we've just checked a (fake)
1460 * dotdot entry that we created on the stack.
1461 * Therefore set 'prev' to NULL so that if we
1462 * call salvage_directory on the next entry,
1463 * it won't try to absorb the next entry into
1464 * the on-stack dotdot entry.
1470 } while (offset < max_block_size);
1476 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1477 db->blockcnt, dx_db->type,
1478 dx_db->min_hash, dx_db->max_hash);
1480 cd->pctx.dir = cd->pctx.ino;
1481 if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1482 (dx_db->type == DX_DIRBLOCK_NODE))
1483 parse_int_node(fs, db, cd, dx_dir, buf, failed_csum);
1486 if (offset != max_block_size) {
1487 cd->pctx.num = rec_len + offset - max_block_size;
1488 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1489 dirent->rec_len = cd->pctx.num;
1494 int flags, will_rehash;
1495 /* leaf block with no tail? Rehash dirs later. */
1496 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
1497 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
1499 !inline_data_size &&
1500 !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
1501 if (insert_dirent_tail(fs, buf) == 0)
1503 e2fsck_rehash_dir_later(ctx, ino);
1507 will_rehash = e2fsck_dir_will_be_rehashed(ctx, ino);
1509 flags = ctx->fs->flags;
1510 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1512 if (inline_data_size) {
1514 #ifdef WORDS_BIGENDIAN
1516 goto skip_first_write_swab;
1517 *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf));
1518 cd->pctx.errcode = ext2fs_dirent_swab_out2(fs,
1519 buf + EXT4_INLINE_DATA_DOTDOT_SIZE,
1520 EXT4_MIN_INLINE_DATA_SIZE -
1521 EXT4_INLINE_DATA_DOTDOT_SIZE,
1523 if (cd->pctx.errcode)
1524 goto skip_second_write_swab;
1525 skip_first_write_swab:
1526 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE ||
1528 goto skip_second_write_swab;
1529 cd->pctx.errcode = ext2fs_dirent_swab_out2(fs,
1530 buf + EXT4_MIN_INLINE_DATA_SIZE,
1532 EXT4_MIN_INLINE_DATA_SIZE,
1534 skip_second_write_swab:
1535 if (cd->pctx.errcode &&
1536 !fix_problem(ctx, PR_2_WRITE_DIRBLOCK, &cd->pctx))
1537 goto abort_free_dict;
1540 ext2fs_inline_data_set(fs, ino, 0, buf,
1543 cd->pctx.errcode = ext2fs_write_dir_block4(fs, block_nr,
1546 ctx->fs->flags = (flags &
1547 EXT2_FLAG_IGNORE_CSUM_ERRORS) |
1549 ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
1550 if (cd->pctx.errcode) {
1551 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
1553 goto abort_free_dict;
1555 ext2fs_mark_changed(fs);
1556 } else if (is_leaf && failed_csum && !dir_modified) {
1558 * If a leaf node that fails csum makes it this far without
1559 * alteration, ask the user if the checksum should be fixed.
1561 if (fix_problem(ctx, PR_2_LEAF_NODE_ONLY_CSUM_INVALID,
1565 dict_free_nodes(&de_dict);
1568 ctx->flags |= E2F_FLAG_ABORT;
1569 dict_free_nodes(&de_dict);
1570 return DIRENT_ABORT;
1579 * This function is called to deallocate a block, and is an interator
1580 * functioned called by deallocate inode via ext2fs_iterate_block().
1582 static int deallocate_inode_block(ext2_filsys fs,
1584 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1585 blk64_t ref_block EXT2FS_ATTR((unused)),
1586 int ref_offset EXT2FS_ATTR((unused)),
1589 struct del_block *p = priv_data;
1593 if ((*block_nr < fs->super->s_first_data_block) ||
1594 (*block_nr >= ext2fs_blocks_count(fs->super)))
1596 if ((*block_nr % EXT2FS_CLUSTER_RATIO(fs)) == 0)
1597 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
1603 * This fuction deallocates an inode
1605 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
1607 ext2_filsys fs = ctx->fs;
1608 struct ext2_inode inode;
1609 struct problem_context pctx;
1611 struct del_block del_block;
1613 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1614 clear_problem_context(&pctx);
1618 * Fix up the bitmaps...
1620 e2fsck_read_bitmaps(ctx);
1621 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1623 if (ext2fs_file_acl_block(fs, &inode) &&
1624 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1625 pctx.errcode = ext2fs_adjust_ea_refcount3(fs,
1626 ext2fs_file_acl_block(fs, &inode),
1627 block_buf, -1, &count, ino);
1628 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1633 pctx.blk = ext2fs_file_acl_block(fs, &inode);
1634 fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1635 ctx->flags |= E2F_FLAG_ABORT;
1639 ext2fs_block_alloc_stats2(fs,
1640 ext2fs_file_acl_block(fs, &inode), -1);
1642 ext2fs_file_acl_block_set(fs, &inode, 0);
1645 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1648 /* Inline data inodes don't have blocks to iterate */
1649 if (inode.i_flags & EXT4_INLINE_DATA_FL)
1652 if (LINUX_S_ISREG(inode.i_mode) &&
1653 ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode)))
1656 del_block.ctx = ctx;
1658 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1659 deallocate_inode_block,
1662 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
1663 ctx->flags |= E2F_FLAG_ABORT;
1667 /* Inode may have changed by block_iterate, so reread it */
1668 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1669 e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
1673 * This fuction clears the htree flag on an inode
1675 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1677 struct ext2_inode inode;
1679 e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1680 inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1681 e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
1682 if (ctx->dirs_to_hash)
1683 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1687 int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1688 ext2_ino_t ino, char *buf)
1690 ext2_filsys fs = ctx->fs;
1691 struct ext2_inode inode;
1692 int inode_modified = 0;
1694 unsigned char *frag, *fsize;
1695 struct problem_context pctx;
1696 problem_t problem = 0;
1698 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
1700 clear_problem_context(&pctx);
1703 pctx.inode = &inode;
1705 if (ext2fs_file_acl_block(fs, &inode) &&
1706 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1707 if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
1708 ext2fs_file_acl_block_set(fs, &inode, 0);
1714 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1715 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1716 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
1717 !(LINUX_S_ISSOCK(inode.i_mode)))
1718 problem = PR_2_BAD_MODE;
1719 else if (LINUX_S_ISCHR(inode.i_mode)
1720 && !e2fsck_pass1_check_device_inode(fs, &inode))
1721 problem = PR_2_BAD_CHAR_DEV;
1722 else if (LINUX_S_ISBLK(inode.i_mode)
1723 && !e2fsck_pass1_check_device_inode(fs, &inode))
1724 problem = PR_2_BAD_BLOCK_DEV;
1725 else if (LINUX_S_ISFIFO(inode.i_mode)
1726 && !e2fsck_pass1_check_device_inode(fs, &inode))
1727 problem = PR_2_BAD_FIFO;
1728 else if (LINUX_S_ISSOCK(inode.i_mode)
1729 && !e2fsck_pass1_check_device_inode(fs, &inode))
1730 problem = PR_2_BAD_SOCKET;
1731 else if (LINUX_S_ISLNK(inode.i_mode)
1732 && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) {
1733 problem = PR_2_INVALID_SYMLINK;
1737 if (fix_problem(ctx, problem, &pctx)) {
1738 deallocate_inode(ctx, ino, 0);
1739 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1747 if (inode.i_faddr) {
1748 if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1755 switch (fs->super->s_creator_os) {
1757 frag = &inode.osd2.hurd2.h_i_frag;
1758 fsize = &inode.osd2.hurd2.h_i_fsize;
1763 if (frag && *frag) {
1765 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
1772 if (fsize && *fsize) {
1774 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
1782 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1783 !(fs->super->s_feature_ro_compat &
1784 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1785 (inode.osd2.linux2.l_i_blocks_hi != 0)) {
1786 pctx.num = inode.osd2.linux2.l_i_blocks_hi;
1787 if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) {
1788 inode.osd2.linux2.l_i_blocks_hi = 0;
1793 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1794 !(fs->super->s_feature_incompat &
1795 EXT4_FEATURE_INCOMPAT_64BIT) &&
1796 inode.osd2.linux2.l_i_file_acl_high != 0) {
1797 pctx.num = inode.osd2.linux2.l_i_file_acl_high;
1798 if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) {
1799 inode.osd2.linux2.l_i_file_acl_high = 0;
1805 if (ext2fs_file_acl_block(fs, &inode) &&
1806 ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) ||
1807 (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) {
1808 if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
1809 ext2fs_file_acl_block_set(fs, &inode, 0);
1814 if (inode.i_dir_acl &&
1815 LINUX_S_ISDIR(inode.i_mode)) {
1816 if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1817 inode.i_dir_acl = 0;
1824 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
1825 if (!not_fixed && ctx->inode_bad_map)
1826 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
1831 * allocate_dir_block --- this function allocates a new directory
1832 * block for a particular inode; this is done if a directory has
1833 * a "hole" in it, or if a directory has a illegal block number
1834 * that was zeroed out and now needs to be replaced.
1836 static int allocate_dir_block(e2fsck_t ctx,
1837 struct ext2_db_entry2 *db,
1838 char *buf EXT2FS_ATTR((unused)),
1839 struct problem_context *pctx)
1841 ext2_filsys fs = ctx->fs;
1844 struct ext2_inode inode;
1846 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
1850 * Read the inode and block bitmaps in; we'll be messing with
1853 e2fsck_read_bitmaps(ctx);
1856 * First, find a free block
1858 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1859 pctx->errcode = ext2fs_map_cluster_block(fs, db->ino, &inode,
1860 db->blockcnt, &blk);
1861 if (pctx->errcode || blk == 0) {
1862 blk = ext2fs_find_inode_goal(fs, db->ino, &inode, db->blockcnt);
1863 pctx->errcode = ext2fs_new_block2(fs, blk,
1864 ctx->block_found_map, &blk);
1865 if (pctx->errcode) {
1866 pctx->str = "ext2fs_new_block";
1867 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1871 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1872 ext2fs_mark_block_bitmap2(fs->block_map, blk);
1873 ext2fs_mark_bb_dirty(fs);
1876 * Now let's create the actual data block for the inode
1879 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
1881 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1882 EXT2_ROOT_INO, &block);
1884 if (pctx->errcode) {
1885 pctx->str = "ext2fs_new_dir_block";
1886 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1890 pctx->errcode = ext2fs_write_dir_block4(fs, blk, block, 0, db->ino);
1891 ext2fs_free_mem(&block);
1892 if (pctx->errcode) {
1893 pctx->str = "ext2fs_write_dir_block";
1894 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1899 * Update the inode block count
1901 ext2fs_iblk_add_blocks(fs, &inode, 1);
1902 if (EXT2_I_SIZE(&inode) < (db->blockcnt+1) * fs->blocksize) {
1903 pctx->errcode = ext2fs_inode_size_set(fs, &inode,
1904 (db->blockcnt+1) * fs->blocksize);
1905 if (pctx->errcode) {
1906 pctx->str = "ext2fs_inode_size_set";
1907 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1911 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
1914 * Finally, update the block pointers for the inode
1917 pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET,
1918 db->blockcnt, 0, &blk);
1919 if (pctx->errcode) {
1920 pctx->str = "ext2fs_block_iterate";
1921 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);