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() */
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
;
98 init_resource_track(&rtrack
, ctx
->fs
->io
);
99 clear_problem_context(&cd
.pctx
);
102 mtrace_print("Pass 2");
105 if (!(ctx
->options
& E2F_OPT_PREEN
))
106 fix_problem(ctx
, PR_2_PASS_HEADER
, &cd
.pctx
);
108 e2fsck_setup_tdb_icount(ctx
, EXT2_ICOUNT_OPT_INCREMENT
,
110 if (ctx
->inode_count
)
113 cd
.pctx
.errcode
= ext2fs_create_icount2(fs
,
114 EXT2_ICOUNT_OPT_INCREMENT
,
115 0, ctx
->inode_link_info
,
117 if (cd
.pctx
.errcode
) {
118 fix_problem(ctx
, PR_2_ALLOCATE_ICOUNT
, &cd
.pctx
);
119 ctx
->flags
|= E2F_FLAG_ABORT
;
122 buf
= (char *) e2fsck_allocate_memory(ctx
, 2*fs
->blocksize
,
123 "directory scan buffer");
126 * Set up the parent pointer for the root directory, if
127 * present. (If the root directory is not present, we will
128 * create it in pass 3.)
130 (void) e2fsck_dir_info_set_parent(ctx
, EXT2_ROOT_INO
, EXT2_ROOT_INO
);
135 cd
.max
= ext2fs_dblist_count2(fs
->dblist
);
138 (void) (ctx
->progress
)(ctx
, 2, 0, cd
.max
);
140 if (fs
->super
->s_feature_compat
& EXT2_FEATURE_COMPAT_DIR_INDEX
)
141 ext2fs_dblist_sort2(fs
->dblist
, special_dir_block_cmp
);
143 cd
.pctx
.errcode
= ext2fs_dblist_iterate2(fs
->dblist
, check_dir_block
,
145 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
|| ctx
->flags
& E2F_FLAG_RESTART
)
148 if (ctx
->flags
& E2F_FLAG_RESTART_LATER
) {
149 ctx
->flags
|= E2F_FLAG_RESTART
;
153 if (cd
.pctx
.errcode
) {
154 fix_problem(ctx
, PR_2_DBLIST_ITERATE
, &cd
.pctx
);
155 ctx
->flags
|= E2F_FLAG_ABORT
;
160 for (i
=0; (dx_dir
= e2fsck_dx_dir_info_iter(ctx
, &i
)) != 0;) {
161 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
163 if (dx_dir
->numblocks
== 0)
165 clear_problem_context(&pctx
);
167 pctx
.dir
= dx_dir
->ino
;
168 dx_db
= dx_dir
->dx_block
;
169 if (dx_db
->flags
& DX_FLAG_REFERENCED
)
170 dx_db
->flags
|= DX_FLAG_DUP_REF
;
172 dx_db
->flags
|= DX_FLAG_REFERENCED
;
174 * Find all of the first and last leaf blocks, and
175 * update their parent's min and max hash values
177 for (b
=0, dx_db
= dx_dir
->dx_block
;
178 b
< dx_dir
->numblocks
;
180 if ((dx_db
->type
!= DX_DIRBLOCK_LEAF
) ||
181 !(dx_db
->flags
& (DX_FLAG_FIRST
| DX_FLAG_LAST
)))
183 dx_parent
= &dx_dir
->dx_block
[dx_db
->parent
];
185 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
187 if (dx_db
->flags
& DX_FLAG_FIRST
)
188 dx_parent
->min_hash
= dx_db
->min_hash
;
190 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
192 if (dx_db
->flags
& DX_FLAG_LAST
)
193 dx_parent
->max_hash
= dx_db
->max_hash
;
196 for (b
=0, dx_db
= dx_dir
->dx_block
;
197 b
< dx_dir
->numblocks
;
200 pctx
.group
= dx_db
->parent
;
202 if (!(dx_db
->flags
& DX_FLAG_FIRST
) &&
203 (dx_db
->min_hash
< dx_db
->node_min_hash
)) {
204 pctx
.blk
= dx_db
->min_hash
;
205 pctx
.blk2
= dx_db
->node_min_hash
;
206 code
= PR_2_HTREE_MIN_HASH
;
207 fix_problem(ctx
, code
, &pctx
);
210 if (dx_db
->type
== DX_DIRBLOCK_LEAF
) {
211 depth
= htree_depth(dx_dir
, dx_db
);
212 if (depth
!= dx_dir
->depth
) {
213 pctx
.num
= dx_dir
->depth
;
214 code
= PR_2_HTREE_BAD_DEPTH
;
215 fix_problem(ctx
, code
, &pctx
);
220 * This test doesn't apply for the root block
224 (dx_db
->max_hash
> dx_db
->node_max_hash
)) {
225 pctx
.blk
= dx_db
->max_hash
;
226 pctx
.blk2
= dx_db
->node_max_hash
;
227 code
= PR_2_HTREE_MAX_HASH
;
228 fix_problem(ctx
, code
, &pctx
);
231 if (!(dx_db
->flags
& DX_FLAG_REFERENCED
)) {
232 code
= PR_2_HTREE_NOTREF
;
233 fix_problem(ctx
, code
, &pctx
);
235 } else if (dx_db
->flags
& DX_FLAG_DUP_REF
) {
236 code
= PR_2_HTREE_DUPREF
;
237 fix_problem(ctx
, code
, &pctx
);
241 if (bad_dir
&& fix_problem(ctx
, PR_2_HTREE_CLEAR
, &pctx
)) {
242 clear_htree(ctx
, dx_dir
->ino
);
243 dx_dir
->numblocks
= 0;
246 e2fsck_free_dx_dir_info(ctx
);
248 ext2fs_free_mem(&buf
);
249 ext2fs_free_dblist(fs
->dblist
);
251 if (ctx
->inode_bad_map
) {
252 ext2fs_free_inode_bitmap(ctx
->inode_bad_map
);
253 ctx
->inode_bad_map
= 0;
255 if (ctx
->inode_reg_map
) {
256 ext2fs_free_inode_bitmap(ctx
->inode_reg_map
);
257 ctx
->inode_reg_map
= 0;
260 clear_problem_context(&pctx
);
261 if (ctx
->large_files
) {
262 if (!(sb
->s_feature_ro_compat
&
263 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
) &&
264 fix_problem(ctx
, PR_2_FEATURE_LARGE_FILES
, &pctx
)) {
265 sb
->s_feature_ro_compat
|=
266 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
;
267 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
268 ext2fs_mark_super_dirty(fs
);
270 if (sb
->s_rev_level
== EXT2_GOOD_OLD_REV
&&
271 fix_problem(ctx
, PR_1_FS_REV_LEVEL
, &pctx
)) {
272 ext2fs_update_dynamic_rev(fs
);
273 ext2fs_mark_super_dirty(fs
);
277 print_resource_track(ctx
, _("Pass 2"), &rtrack
, fs
->io
);
280 #define MAX_DEPTH 32000
281 static int htree_depth(struct dx_dir_info
*dx_dir
,
282 struct dx_dirblock_info
*dx_db
)
286 while (dx_db
->type
!= DX_DIRBLOCK_ROOT
&& depth
< MAX_DEPTH
) {
287 dx_db
= &dx_dir
->dx_block
[dx_db
->parent
];
293 static int dict_de_cmp(const void *a
, const void *b
)
295 const struct ext2_dir_entry
*de_a
, *de_b
;
298 de_a
= (const struct ext2_dir_entry
*) a
;
299 a_len
= de_a
->name_len
& 0xFF;
300 de_b
= (const struct ext2_dir_entry
*) b
;
301 b_len
= de_b
->name_len
& 0xFF;
304 return (a_len
- b_len
);
306 return strncmp(de_a
->name
, de_b
->name
, a_len
);
310 * This is special sort function that makes sure that directory blocks
311 * with a dirblock of zero are sorted to the beginning of the list.
312 * This guarantees that the root node of the htree directories are
313 * processed first, so we know what hash version to use.
315 static EXT2_QSORT_TYPE
special_dir_block_cmp(const void *a
, const void *b
)
317 const struct ext2_db_entry2
*db_a
=
318 (const struct ext2_db_entry2
*) a
;
319 const struct ext2_db_entry2
*db_b
=
320 (const struct ext2_db_entry2
*) b
;
322 if (db_a
->blockcnt
&& !db_b
->blockcnt
)
325 if (!db_a
->blockcnt
&& db_b
->blockcnt
)
328 if (db_a
->blk
!= db_b
->blk
)
329 return (int) (db_a
->blk
- db_b
->blk
);
331 if (db_a
->ino
!= db_b
->ino
)
332 return (int) (db_a
->ino
- db_b
->ino
);
334 return (int) (db_a
->blockcnt
- db_b
->blockcnt
);
339 * Make sure the first entry in the directory is '.', and that the
340 * directory entry is sane.
342 static int check_dot(e2fsck_t ctx
,
343 struct ext2_dir_entry
*dirent
,
344 ext2_ino_t ino
, struct problem_context
*pctx
)
346 struct ext2_dir_entry
*nextdir
;
347 unsigned int rec_len
, new_len
;
353 problem
= PR_2_MISSING_DOT
;
354 else if (((dirent
->name_len
& 0xFF) != 1) ||
355 (dirent
->name
[0] != '.'))
356 problem
= PR_2_1ST_NOT_DOT
;
357 else if (dirent
->name
[1] != '\0')
358 problem
= PR_2_DOT_NULL_TERM
;
360 (void) ext2fs_get_rec_len(ctx
->fs
, dirent
, &rec_len
);
362 if (fix_problem(ctx
, problem
, pctx
)) {
364 rec_len
= dirent
->rec_len
= 12;
366 dirent
->name_len
= 1;
367 dirent
->name
[0] = '.';
368 dirent
->name
[1] = '\0';
373 if (dirent
->inode
!= ino
) {
374 if (fix_problem(ctx
, PR_2_BAD_INODE_DOT
, pctx
)) {
380 new_len
= rec_len
- 12;
383 fix_problem(ctx
, PR_2_SPLIT_DOT
, pctx
)) {
384 nextdir
= (struct ext2_dir_entry
*)
385 ((char *) dirent
+ 12);
386 dirent
->rec_len
= 12;
387 (void) ext2fs_set_rec_len(ctx
->fs
, new_len
,
390 nextdir
->name_len
= 0;
399 * Make sure the second entry in the directory is '..', and that the
400 * directory entry is sane. We do not check the inode number of '..'
401 * here; this gets done in pass 3.
403 static int check_dotdot(e2fsck_t ctx
,
404 struct ext2_dir_entry
*dirent
,
405 ext2_ino_t ino
, struct problem_context
*pctx
)
408 unsigned int rec_len
;
411 problem
= PR_2_MISSING_DOT_DOT
;
412 else if (((dirent
->name_len
& 0xFF) != 2) ||
413 (dirent
->name
[0] != '.') ||
414 (dirent
->name
[1] != '.'))
415 problem
= PR_2_2ND_NOT_DOT_DOT
;
416 else if (dirent
->name
[2] != '\0')
417 problem
= PR_2_DOT_DOT_NULL_TERM
;
419 (void) ext2fs_get_rec_len(ctx
->fs
, dirent
, &rec_len
);
421 if (fix_problem(ctx
, problem
, pctx
)) {
423 dirent
->rec_len
= 12;
425 * Note: we don't have the parent inode just
426 * yet, so we will fill it in with the root
427 * inode. This will get fixed in pass 3.
429 dirent
->inode
= EXT2_ROOT_INO
;
430 dirent
->name_len
= 2;
431 dirent
->name
[0] = '.';
432 dirent
->name
[1] = '.';
433 dirent
->name
[2] = '\0';
438 if (e2fsck_dir_info_set_dotdot(ctx
, ino
, dirent
->inode
)) {
439 fix_problem(ctx
, PR_2_NO_DIRINFO
, pctx
);
446 * Check to make sure a directory entry doesn't contain any illegal
449 static int check_name(e2fsck_t ctx
,
450 struct ext2_dir_entry
*dirent
,
451 ext2_ino_t dir_ino
EXT2FS_ATTR((unused
)),
452 struct problem_context
*pctx
)
458 for ( i
= 0; i
< (dirent
->name_len
& 0xFF); i
++) {
459 if (dirent
->name
[i
] == '/' || dirent
->name
[i
] == '\0') {
461 fixup
= fix_problem(ctx
, PR_2_BAD_NAME
, pctx
);
464 dirent
->name
[i
] = '.';
473 * Check the directory filetype (if present)
475 static _INLINE_
int check_filetype(e2fsck_t ctx
,
476 struct ext2_dir_entry
*dirent
,
477 ext2_ino_t dir_ino
EXT2FS_ATTR((unused
)),
478 struct problem_context
*pctx
)
480 int filetype
= dirent
->name_len
>> 8;
481 int should_be
= EXT2_FT_UNKNOWN
;
482 struct ext2_inode inode
;
484 if (!(ctx
->fs
->super
->s_feature_incompat
&
485 EXT2_FEATURE_INCOMPAT_FILETYPE
)) {
487 !fix_problem(ctx
, PR_2_CLEAR_FILETYPE
, pctx
))
489 dirent
->name_len
= dirent
->name_len
& 0xFF;
493 if (ext2fs_test_inode_bitmap2(ctx
->inode_dir_map
, dirent
->inode
)) {
494 should_be
= EXT2_FT_DIR
;
495 } else if (ext2fs_test_inode_bitmap2(ctx
->inode_reg_map
,
497 should_be
= EXT2_FT_REG_FILE
;
498 } else if (ctx
->inode_bad_map
&&
499 ext2fs_test_inode_bitmap2(ctx
->inode_bad_map
,
503 e2fsck_read_inode(ctx
, dirent
->inode
, &inode
,
505 should_be
= ext2_file_type(inode
.i_mode
);
507 if (filetype
== should_be
)
509 pctx
->num
= should_be
;
511 if (fix_problem(ctx
, filetype
? PR_2_BAD_FILETYPE
: PR_2_SET_FILETYPE
,
515 dirent
->name_len
= (dirent
->name_len
& 0xFF) | should_be
<< 8;
520 static void parse_int_node(ext2_filsys fs
,
521 struct ext2_db_entry2
*db
,
522 struct check_dir_struct
*cd
,
523 struct dx_dir_info
*dx_dir
,
526 struct ext2_dx_root_info
*root
;
527 struct ext2_dx_entry
*ent
;
528 struct ext2_dx_countlimit
*limit
;
529 struct dx_dirblock_info
*dx_db
;
530 int i
, expect_limit
, count
;
532 ext2_dirhash_t min_hash
= 0xffffffff;
533 ext2_dirhash_t max_hash
= 0;
534 ext2_dirhash_t hash
= 0, prev_hash
;
536 if (db
->blockcnt
== 0) {
537 root
= (struct ext2_dx_root_info
*) (block_buf
+ 24);
540 printf("Root node dump:\n");
541 printf("\t Reserved zero: %u\n", root
->reserved_zero
);
542 printf("\t Hash Version: %d\n", root
->hash_version
);
543 printf("\t Info length: %d\n", root
->info_length
);
544 printf("\t Indirect levels: %d\n", root
->indirect_levels
);
545 printf("\t Flags: %d\n", root
->unused_flags
);
548 ent
= (struct ext2_dx_entry
*) (block_buf
+ 24 + root
->info_length
);
550 ent
= (struct ext2_dx_entry
*) (block_buf
+8);
552 limit
= (struct ext2_dx_countlimit
*) ent
;
555 printf("Number of entries (count): %d\n",
556 ext2fs_le16_to_cpu(limit
->count
));
557 printf("Number of entries (limit): %d\n",
558 ext2fs_le16_to_cpu(limit
->limit
));
561 count
= ext2fs_le16_to_cpu(limit
->count
);
562 expect_limit
= (fs
->blocksize
- ((char *) ent
- block_buf
)) /
563 sizeof(struct ext2_dx_entry
);
564 if (ext2fs_le16_to_cpu(limit
->limit
) != expect_limit
) {
565 cd
->pctx
.num
= ext2fs_le16_to_cpu(limit
->limit
);
566 if (fix_problem(cd
->ctx
, PR_2_HTREE_BAD_LIMIT
, &cd
->pctx
))
569 if (count
> expect_limit
) {
570 cd
->pctx
.num
= count
;
571 if (fix_problem(cd
->ctx
, PR_2_HTREE_BAD_COUNT
, &cd
->pctx
))
573 count
= expect_limit
;
576 for (i
=0; i
< count
; i
++) {
578 hash
= i
? (ext2fs_le32_to_cpu(ent
[i
].hash
) & ~1) : 0;
580 printf("Entry #%d: Hash 0x%08x, block %u\n", i
,
581 hash
, ext2fs_le32_to_cpu(ent
[i
].block
));
583 blk
= ext2fs_le32_to_cpu(ent
[i
].block
) & 0x0ffffff;
584 /* Check to make sure the block is valid */
585 if (blk
>= (blk_t
) dx_dir
->numblocks
) {
587 if (fix_problem(cd
->ctx
, PR_2_HTREE_BADBLK
,
592 if (hash
< prev_hash
&&
593 fix_problem(cd
->ctx
, PR_2_HTREE_HASH_ORDER
, &cd
->pctx
))
595 dx_db
= &dx_dir
->dx_block
[blk
];
596 if (dx_db
->flags
& DX_FLAG_REFERENCED
) {
597 dx_db
->flags
|= DX_FLAG_DUP_REF
;
599 dx_db
->flags
|= DX_FLAG_REFERENCED
;
600 dx_db
->parent
= db
->blockcnt
;
606 dx_db
->node_min_hash
= hash
;
608 dx_db
->node_max_hash
=
609 ext2fs_le32_to_cpu(ent
[i
+1].hash
) & ~1;
611 dx_db
->node_max_hash
= 0xfffffffe;
612 dx_db
->flags
|= DX_FLAG_LAST
;
615 dx_db
->flags
|= DX_FLAG_FIRST
;
618 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
619 db
->blockcnt
, min_hash
, max_hash
);
621 dx_db
= &dx_dir
->dx_block
[db
->blockcnt
];
622 dx_db
->min_hash
= min_hash
;
623 dx_db
->max_hash
= max_hash
;
627 clear_htree(cd
->ctx
, cd
->pctx
.ino
);
628 dx_dir
->numblocks
= 0;
630 #endif /* ENABLE_HTREE */
633 * Given a busted directory, try to salvage it somehow.
636 static void salvage_directory(ext2_filsys fs
,
637 struct ext2_dir_entry
*dirent
,
638 struct ext2_dir_entry
*prev
,
639 unsigned int *offset
)
641 char *cp
= (char *) dirent
;
643 unsigned int rec_len
, prev_rec_len
;
644 unsigned int name_len
= dirent
->name_len
& 0xFF;
646 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
647 left
= fs
->blocksize
- *offset
- rec_len
;
650 * Special case of directory entry of size 8: copy what's left
651 * of the directory block up to cover up the invalid hole.
653 if ((left
>= 12) && (rec_len
== 8)) {
654 memmove(cp
, cp
+8, left
);
655 memset(cp
+ left
, 0, 8);
659 * If the directory entry overruns the end of the directory
660 * block, and the name is small enough to fit, then adjust the
664 ((int) rec_len
+ left
> 8) &&
665 (name_len
+ 8 <= (int) rec_len
+ left
) &&
666 dirent
->inode
<= fs
->super
->s_inodes_count
&&
667 strnlen(dirent
->name
, name_len
) == name_len
) {
668 (void) ext2fs_set_rec_len(fs
, (int) rec_len
+ left
, dirent
);
672 * If the record length of the directory entry is a multiple
673 * of four, and not too big, such that it is valid, let the
674 * previous directory entry absorb the invalid one.
676 if (prev
&& rec_len
&& (rec_len
% 4) == 0 &&
677 (*offset
+ rec_len
<= fs
->blocksize
)) {
678 (void) ext2fs_get_rec_len(fs
, prev
, &prev_rec_len
);
679 prev_rec_len
+= rec_len
;
680 (void) ext2fs_set_rec_len(fs
, prev_rec_len
, prev
);
685 * Default salvage method --- kill all of the directory
686 * entries for the rest of the block. We will either try to
687 * absorb it into the previous directory entry, or create a
688 * new empty directory entry the rest of the directory block.
691 (void) ext2fs_get_rec_len(fs
, prev
, &prev_rec_len
);
692 prev_rec_len
+= fs
->blocksize
- *offset
;
693 (void) ext2fs_set_rec_len(fs
, prev_rec_len
, prev
);
694 *offset
= fs
->blocksize
;
696 rec_len
= fs
->blocksize
- *offset
;
697 (void) ext2fs_set_rec_len(fs
, rec_len
, dirent
);
698 dirent
->name_len
= 0;
703 static int check_dir_block(ext2_filsys fs
,
704 struct ext2_db_entry2
*db
,
707 struct dx_dir_info
*dx_dir
;
709 struct dx_dirblock_info
*dx_db
= 0;
710 #endif /* ENABLE_HTREE */
711 struct ext2_dir_entry
*dirent
, *prev
;
713 unsigned int offset
= 0;
715 int dir_modified
= 0;
717 unsigned int rec_len
;
718 blk64_t block_nr
= db
->blk
;
719 ext2_ino_t ino
= db
->ino
;
720 ext2_ino_t subdir_parent
;
722 struct check_dir_struct
*cd
;
726 struct ext2_dx_root_info
*root
;
727 struct ext2_dx_countlimit
*limit
;
728 static dict_t de_dict
;
729 struct problem_context pctx
;
733 cd
= (struct check_dir_struct
*) priv_data
;
737 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
|| ctx
->flags
& E2F_FLAG_RESTART
)
740 if (ctx
->progress
&& (ctx
->progress
)(ctx
, 2, cd
->count
++, cd
->max
))
744 * Make sure the inode is still in use (could have been
745 * deleted in the duplicate/bad blocks pass.
747 if (!(ext2fs_test_inode_bitmap2(ctx
->inode_used_map
, ino
)))
751 cd
->pctx
.blk
= block_nr
;
752 cd
->pctx
.blkcount
= db
->blockcnt
;
758 if (allocate_dir_block(ctx
, db
, buf
, &cd
->pctx
))
768 if (ctx
->dirs_to_hash
&&
769 ext2fs_u32_list_test(ctx
->dirs_to_hash
, ino
))
773 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr
,
777 old_op
= ehandler_operation(_("reading directory block"));
778 cd
->pctx
.errcode
= ext2fs_read_dir_block3(fs
, block_nr
, buf
, 0);
779 ehandler_operation(0);
780 if (cd
->pctx
.errcode
== EXT2_ET_DIR_CORRUPTED
)
781 cd
->pctx
.errcode
= 0; /* We'll handle this ourselves */
782 if (cd
->pctx
.errcode
) {
783 if (!fix_problem(ctx
, PR_2_READ_DIRBLOCK
, &cd
->pctx
)) {
784 ctx
->flags
|= E2F_FLAG_ABORT
;
787 memset(buf
, 0, fs
->blocksize
);
790 dx_dir
= e2fsck_get_dx_dir_info(ctx
, ino
);
791 if (dx_dir
&& dx_dir
->numblocks
) {
792 if (db
->blockcnt
>= dx_dir
->numblocks
) {
793 if (fix_problem(ctx
, PR_2_UNEXPECTED_HTREE_BLOCK
,
795 clear_htree(ctx
, ino
);
796 dx_dir
->numblocks
= 0;
800 fatal_error(ctx
, _("Can not continue."));
802 dx_db
= &dx_dir
->dx_block
[db
->blockcnt
];
803 dx_db
->type
= DX_DIRBLOCK_LEAF
;
804 dx_db
->phys
= block_nr
;
805 dx_db
->min_hash
= ~0;
808 dirent
= (struct ext2_dir_entry
*) buf
;
809 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
810 limit
= (struct ext2_dx_countlimit
*) (buf
+8);
811 if (db
->blockcnt
== 0) {
812 root
= (struct ext2_dx_root_info
*) (buf
+ 24);
813 dx_db
->type
= DX_DIRBLOCK_ROOT
;
814 dx_db
->flags
|= DX_FLAG_FIRST
| DX_FLAG_LAST
;
815 if ((root
->reserved_zero
||
816 root
->info_length
< 8 ||
817 root
->indirect_levels
> 1) &&
818 fix_problem(ctx
, PR_2_HTREE_BAD_ROOT
, &cd
->pctx
)) {
819 clear_htree(ctx
, ino
);
820 dx_dir
->numblocks
= 0;
823 dx_dir
->hashversion
= root
->hash_version
;
824 if ((dx_dir
->hashversion
<= EXT2_HASH_TEA
) &&
825 (fs
->super
->s_flags
& EXT2_FLAGS_UNSIGNED_HASH
))
826 dx_dir
->hashversion
+= 3;
827 dx_dir
->depth
= root
->indirect_levels
+ 1;
828 } else if ((dirent
->inode
== 0) &&
829 (rec_len
== fs
->blocksize
) &&
830 (dirent
->name_len
== 0) &&
831 (ext2fs_le16_to_cpu(limit
->limit
) ==
833 sizeof(struct ext2_dx_entry
))))
834 dx_db
->type
= DX_DIRBLOCK_NODE
;
837 #endif /* ENABLE_HTREE */
839 dict_init(&de_dict
, DICTCOUNT_T_MAX
, dict_de_cmp
);
843 ext2_ino_t first_unused_inode
;
846 dirent
= (struct ext2_dir_entry
*) (buf
+ offset
);
847 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
848 cd
->pctx
.dirent
= dirent
;
849 cd
->pctx
.num
= offset
;
850 if (((offset
+ rec_len
) > fs
->blocksize
) ||
852 ((rec_len
% 4) != 0) ||
853 (((dirent
->name_len
& (unsigned) 0xFF)+8) > rec_len
)) {
854 if (fix_problem(ctx
, PR_2_DIR_CORRUPTED
, &cd
->pctx
)) {
855 salvage_directory(fs
, dirent
, prev
, &offset
);
859 goto abort_free_dict
;
861 if ((dirent
->name_len
& 0xFF) > EXT2_NAME_LEN
) {
862 if (fix_problem(ctx
, PR_2_FILENAME_LONG
, &cd
->pctx
)) {
863 dirent
->name_len
= EXT2_NAME_LEN
;
868 if (dot_state
== 0) {
869 if (check_dot(ctx
, dirent
, ino
, &cd
->pctx
))
871 } else if (dot_state
== 1) {
872 ret
= check_dotdot(ctx
, dirent
, ino
, &cd
->pctx
);
874 goto abort_free_dict
;
877 } else if (dirent
->inode
== ino
) {
878 problem
= PR_2_LINK_DOT
;
879 if (fix_problem(ctx
, PR_2_LINK_DOT
, &cd
->pctx
)) {
889 * Make sure the inode listed is a legal one.
891 if (((dirent
->inode
!= EXT2_ROOT_INO
) &&
892 (dirent
->inode
< EXT2_FIRST_INODE(fs
->super
))) ||
893 (dirent
->inode
> fs
->super
->s_inodes_count
)) {
894 problem
= PR_2_BAD_INO
;
895 } else if (ctx
->inode_bb_map
&&
896 (ext2fs_test_inode_bitmap2(ctx
->inode_bb_map
,
899 * If the inode is in a bad block, offer to
902 problem
= PR_2_BB_INODE
;
903 } else if ((dot_state
> 1) &&
904 ((dirent
->name_len
& 0xFF) == 1) &&
905 (dirent
->name
[0] == '.')) {
907 * If there's a '.' entry in anything other
908 * than the first directory entry, it's a
909 * duplicate entry that should be removed.
911 problem
= PR_2_DUP_DOT
;
912 } else if ((dot_state
> 1) &&
913 ((dirent
->name_len
& 0xFF) == 2) &&
914 (dirent
->name
[0] == '.') &&
915 (dirent
->name
[1] == '.')) {
917 * If there's a '..' entry in anything other
918 * than the second directory entry, it's a
919 * duplicate entry that should be removed.
921 problem
= PR_2_DUP_DOT_DOT
;
922 } else if ((dot_state
> 1) &&
923 (dirent
->inode
== EXT2_ROOT_INO
)) {
925 * Don't allow links to the root directory.
926 * We check this specially to make sure we
927 * catch this error case even if the root
928 * directory hasn't been created yet.
930 problem
= PR_2_LINK_ROOT
;
931 } else if ((dot_state
> 1) &&
932 (dirent
->name_len
& 0xFF) == 0) {
934 * Don't allow zero-length directory names.
936 problem
= PR_2_NULL_NAME
;
940 if (fix_problem(ctx
, problem
, &cd
->pctx
)) {
945 ext2fs_unmark_valid(fs
);
946 if (problem
== PR_2_BAD_INO
)
952 * If the inode was marked as having bad fields in
953 * pass1, process it and offer to fix/clear it.
954 * (We wait until now so that we can display the
955 * pathname to the user.)
957 if (ctx
->inode_bad_map
&&
958 ext2fs_test_inode_bitmap2(ctx
->inode_bad_map
,
960 if (e2fsck_process_bad_inode(ctx
, ino
,
962 buf
+ fs
->blocksize
)) {
967 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
971 group
= ext2fs_group_of_ino(fs
, dirent
->inode
);
972 first_unused_inode
= group
* fs
->super
->s_inodes_per_group
+
973 1 + fs
->super
->s_inodes_per_group
-
974 ext2fs_bg_itable_unused(fs
, group
);
975 cd
->pctx
.group
= group
;
978 * Check if the inode was missed out because
979 * _INODE_UNINIT flag was set or bg_itable_unused was
980 * incorrect. If so, clear the _INODE_UNINIT flag and
981 * restart e2fsck. In the future it would be nice if
982 * we could call a function in pass1.c that checks the
983 * newly visible inodes.
985 if (ext2fs_bg_flags_test(fs
, group
, EXT2_BG_INODE_UNINIT
)) {
986 pctx
.num
= dirent
->inode
;
987 if (fix_problem(ctx
, PR_2_INOREF_BG_INO_UNINIT
,
989 ext2fs_bg_flags_clear(fs
, group
,
990 EXT2_BG_INODE_UNINIT
);
991 ext2fs_mark_super_dirty(fs
);
992 ctx
->flags
|= E2F_FLAG_RESTART_LATER
;
994 ext2fs_unmark_valid(fs
);
995 if (problem
== PR_2_BAD_INO
)
998 } else if (dirent
->inode
>= first_unused_inode
) {
999 pctx
.num
= dirent
->inode
;
1000 if (fix_problem(ctx
, PR_2_INOREF_IN_UNUSED
, &cd
->pctx
)){
1001 ext2fs_bg_itable_unused_set(fs
, group
, 0);
1002 ext2fs_mark_super_dirty(fs
);
1003 ctx
->flags
|= E2F_FLAG_RESTART_LATER
;
1005 ext2fs_unmark_valid(fs
);
1006 if (problem
== PR_2_BAD_INO
)
1012 * Offer to clear unused inodes; if we are going to be
1013 * restarting the scan due to bg_itable_unused being
1014 * wrong, then don't clear any inodes to avoid zapping
1015 * inodes that were skipped during pass1 due to an
1016 * incorrect bg_itable_unused; we'll get any real
1017 * problems after we restart.
1019 if (!(ctx
->flags
& E2F_FLAG_RESTART_LATER
) &&
1020 !(ext2fs_test_inode_bitmap2(ctx
->inode_used_map
,
1022 problem
= PR_2_UNUSED_INODE
;
1025 if (fix_problem(ctx
, problem
, &cd
->pctx
)) {
1030 ext2fs_unmark_valid(fs
);
1031 if (problem
== PR_2_BAD_INO
)
1036 if (check_name(ctx
, dirent
, ino
, &cd
->pctx
))
1039 if (check_filetype(ctx
, dirent
, ino
, &cd
->pctx
))
1044 ext2fs_dirhash(dx_dir
->hashversion
, dirent
->name
,
1045 (dirent
->name_len
& 0xFF),
1046 fs
->super
->s_hash_seed
, &hash
, 0);
1047 if (hash
< dx_db
->min_hash
)
1048 dx_db
->min_hash
= hash
;
1049 if (hash
> dx_db
->max_hash
)
1050 dx_db
->max_hash
= hash
;
1055 * If this is a directory, then mark its parent in its
1056 * dir_info structure. If the parent field is already
1057 * filled in, then this directory has more than one
1058 * hard link. We assume the first link is correct,
1059 * and ask the user if he/she wants to clear this one.
1061 if ((dot_state
> 1) &&
1062 (ext2fs_test_inode_bitmap2(ctx
->inode_dir_map
,
1064 if (e2fsck_dir_info_get_parent(ctx
, dirent
->inode
,
1066 cd
->pctx
.ino
= dirent
->inode
;
1067 fix_problem(ctx
, PR_2_NO_DIRINFO
, &cd
->pctx
);
1068 goto abort_free_dict
;
1070 if (subdir_parent
) {
1071 cd
->pctx
.ino2
= subdir_parent
;
1072 if (fix_problem(ctx
, PR_2_LINK_DIR
,
1080 (void) e2fsck_dir_info_set_parent(ctx
,
1081 dirent
->inode
, ino
);
1087 } else if (dict_lookup(&de_dict
, dirent
)) {
1088 clear_problem_context(&pctx
);
1090 pctx
.dirent
= dirent
;
1091 fix_problem(ctx
, PR_2_REPORT_DUP_DIRENT
, &pctx
);
1092 if (!ctx
->dirs_to_hash
)
1093 ext2fs_u32_list_create(&ctx
->dirs_to_hash
, 50);
1094 if (ctx
->dirs_to_hash
)
1095 ext2fs_u32_list_add(ctx
->dirs_to_hash
, ino
);
1098 dict_alloc_insert(&de_dict
, dirent
, dirent
);
1100 ext2fs_icount_increment(ctx
->inode_count
, dirent
->inode
,
1103 ctx
->fs_links_count
++;
1104 ctx
->fs_total_count
++;
1108 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
1111 } while (offset
< fs
->blocksize
);
1118 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1119 db
->blockcnt
, dx_db
->type
,
1120 dx_db
->min_hash
, dx_db
->max_hash
);
1122 cd
->pctx
.dir
= cd
->pctx
.ino
;
1123 if ((dx_db
->type
== DX_DIRBLOCK_ROOT
) ||
1124 (dx_db
->type
== DX_DIRBLOCK_NODE
))
1125 parse_int_node(fs
, db
, cd
, dx_dir
, buf
);
1127 #endif /* ENABLE_HTREE */
1128 if (offset
!= fs
->blocksize
) {
1129 cd
->pctx
.num
= rec_len
- fs
->blocksize
+ offset
;
1130 if (fix_problem(ctx
, PR_2_FINAL_RECLEN
, &cd
->pctx
)) {
1131 dirent
->rec_len
= cd
->pctx
.num
;
1136 cd
->pctx
.errcode
= ext2fs_write_dir_block(fs
, block_nr
, buf
);
1137 if (cd
->pctx
.errcode
) {
1138 if (!fix_problem(ctx
, PR_2_WRITE_DIRBLOCK
,
1140 goto abort_free_dict
;
1142 ext2fs_mark_changed(fs
);
1144 dict_free_nodes(&de_dict
);
1147 ctx
->flags
|= E2F_FLAG_ABORT
;
1148 dict_free_nodes(&de_dict
);
1149 return DIRENT_ABORT
;
1153 * This function is called to deallocate a block, and is an interator
1154 * functioned called by deallocate inode via ext2fs_iterate_block().
1156 static int deallocate_inode_block(ext2_filsys fs
,
1158 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1159 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1160 int ref_offset
EXT2FS_ATTR((unused
)),
1163 e2fsck_t ctx
= (e2fsck_t
) priv_data
;
1165 if (HOLE_BLKADDR(*block_nr
))
1167 if ((*block_nr
< fs
->super
->s_first_data_block
) ||
1168 (*block_nr
>= ext2fs_blocks_count(fs
->super
)))
1170 ext2fs_unmark_block_bitmap2(ctx
->block_found_map
, *block_nr
);
1171 ext2fs_block_alloc_stats2(fs
, *block_nr
, -1);
1176 * This fuction deallocates an inode
1178 static void deallocate_inode(e2fsck_t ctx
, ext2_ino_t ino
, char* block_buf
)
1180 ext2_filsys fs
= ctx
->fs
;
1181 struct ext2_inode inode
;
1182 struct problem_context pctx
;
1185 e2fsck_read_inode(ctx
, ino
, &inode
, "deallocate_inode");
1186 e2fsck_clear_inode(ctx
, ino
, &inode
, 0, "deallocate_inode");
1187 clear_problem_context(&pctx
);
1191 * Fix up the bitmaps...
1193 e2fsck_read_bitmaps(ctx
);
1194 ext2fs_inode_alloc_stats2(fs
, ino
, -1, LINUX_S_ISDIR(inode
.i_mode
));
1196 if (ext2fs_file_acl_block(&inode
) &&
1197 (fs
->super
->s_feature_compat
& EXT2_FEATURE_COMPAT_EXT_ATTR
)) {
1198 pctx
.errcode
= ext2fs_adjust_ea_refcount2(fs
, ext2fs_file_acl_block(&inode
),
1199 block_buf
, -1, &count
);
1200 if (pctx
.errcode
== EXT2_ET_BAD_EA_BLOCK_NUM
) {
1205 pctx
.blk
= ext2fs_file_acl_block(&inode
);
1206 fix_problem(ctx
, PR_2_ADJ_EA_REFCOUNT
, &pctx
);
1207 ctx
->flags
|= E2F_FLAG_ABORT
;
1211 ext2fs_unmark_block_bitmap2(ctx
->block_found_map
,
1212 ext2fs_file_acl_block(&inode
));
1213 ext2fs_block_alloc_stats2(fs
,
1214 ext2fs_file_acl_block(&inode
),
1217 ext2fs_file_acl_block_set(&inode
, 0);
1220 if (!ext2fs_inode_has_valid_blocks(&inode
))
1223 if (LINUX_S_ISREG(inode
.i_mode
) && EXT2_I_SIZE(&inode
) >= 0x80000000UL
)
1226 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
, 0, block_buf
,
1227 deallocate_inode_block
, ctx
);
1229 fix_problem(ctx
, PR_2_DEALLOC_INODE
, &pctx
);
1230 ctx
->flags
|= E2F_FLAG_ABORT
;
1236 * This fuction clears the htree flag on an inode
1238 static void clear_htree(e2fsck_t ctx
, ext2_ino_t ino
)
1240 struct ext2_inode inode
;
1242 e2fsck_read_inode(ctx
, ino
, &inode
, "clear_htree");
1243 inode
.i_flags
= inode
.i_flags
& ~EXT2_INDEX_FL
;
1244 e2fsck_write_inode(ctx
, ino
, &inode
, "clear_htree");
1245 if (ctx
->dirs_to_hash
)
1246 ext2fs_u32_list_add(ctx
->dirs_to_hash
, ino
);
1250 extern int e2fsck_process_bad_inode(e2fsck_t ctx
, ext2_ino_t dir
,
1251 ext2_ino_t ino
, char *buf
)
1253 ext2_filsys fs
= ctx
->fs
;
1254 struct ext2_inode inode
;
1255 int inode_modified
= 0;
1257 unsigned char *frag
, *fsize
;
1258 struct problem_context pctx
;
1261 e2fsck_read_inode(ctx
, ino
, &inode
, "process_bad_inode");
1263 clear_problem_context(&pctx
);
1266 pctx
.inode
= &inode
;
1268 if (ext2fs_file_acl_block(&inode
) &&
1269 !(fs
->super
->s_feature_compat
& EXT2_FEATURE_COMPAT_EXT_ATTR
)) {
1270 if (fix_problem(ctx
, PR_2_FILE_ACL_ZERO
, &pctx
)) {
1271 ext2fs_file_acl_block_set(&inode
, 0);
1277 if (!LINUX_S_ISDIR(inode
.i_mode
) && !LINUX_S_ISREG(inode
.i_mode
) &&
1278 !LINUX_S_ISCHR(inode
.i_mode
) && !LINUX_S_ISBLK(inode
.i_mode
) &&
1279 !LINUX_S_ISLNK(inode
.i_mode
) && !LINUX_S_ISFIFO(inode
.i_mode
) &&
1280 !(LINUX_S_ISSOCK(inode
.i_mode
)))
1281 problem
= PR_2_BAD_MODE
;
1282 else if (LINUX_S_ISCHR(inode
.i_mode
)
1283 && !e2fsck_pass1_check_device_inode(fs
, &inode
))
1284 problem
= PR_2_BAD_CHAR_DEV
;
1285 else if (LINUX_S_ISBLK(inode
.i_mode
)
1286 && !e2fsck_pass1_check_device_inode(fs
, &inode
))
1287 problem
= PR_2_BAD_BLOCK_DEV
;
1288 else if (LINUX_S_ISFIFO(inode
.i_mode
)
1289 && !e2fsck_pass1_check_device_inode(fs
, &inode
))
1290 problem
= PR_2_BAD_FIFO
;
1291 else if (LINUX_S_ISSOCK(inode
.i_mode
)
1292 && !e2fsck_pass1_check_device_inode(fs
, &inode
))
1293 problem
= PR_2_BAD_SOCKET
;
1294 else if (LINUX_S_ISLNK(inode
.i_mode
)
1295 && !e2fsck_pass1_check_symlink(fs
, ino
, &inode
, buf
)) {
1296 problem
= PR_2_INVALID_SYMLINK
;
1300 if (fix_problem(ctx
, problem
, &pctx
)) {
1301 deallocate_inode(ctx
, ino
, 0);
1302 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
1310 if (inode
.i_faddr
) {
1311 if (fix_problem(ctx
, PR_2_FADDR_ZERO
, &pctx
)) {
1318 switch (fs
->super
->s_creator_os
) {
1320 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
1321 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
1326 if (frag
&& *frag
) {
1328 if (fix_problem(ctx
, PR_2_FRAG_ZERO
, &pctx
)) {
1335 if (fsize
&& *fsize
) {
1337 if (fix_problem(ctx
, PR_2_FSIZE_ZERO
, &pctx
)) {
1345 if ((fs
->super
->s_creator_os
== EXT2_OS_LINUX
) &&
1346 !(fs
->super
->s_feature_ro_compat
&
1347 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
) &&
1348 (inode
.osd2
.linux2
.l_i_blocks_hi
!= 0)) {
1349 pctx
.num
= inode
.osd2
.linux2
.l_i_blocks_hi
;
1350 if (fix_problem(ctx
, PR_2_BLOCKS_HI_ZERO
, &pctx
)) {
1351 inode
.osd2
.linux2
.l_i_blocks_hi
= 0;
1356 if (!(fs
->super
->s_feature_incompat
&
1357 EXT4_FEATURE_INCOMPAT_64BIT
) &&
1358 inode
.osd2
.linux2
.l_i_file_acl_high
!= 0) {
1359 pctx
.num
= inode
.osd2
.linux2
.l_i_file_acl_high
;
1360 if (fix_problem(ctx
, PR_2_I_FILE_ACL_HI_ZERO
, &pctx
)) {
1361 inode
.osd2
.linux2
.l_i_file_acl_high
= 0;
1367 if (ext2fs_file_acl_block(&inode
) &&
1368 ((ext2fs_file_acl_block(&inode
) < fs
->super
->s_first_data_block
) ||
1369 (ext2fs_file_acl_block(&inode
) >= ext2fs_blocks_count(fs
->super
)))) {
1370 if (fix_problem(ctx
, PR_2_FILE_ACL_BAD
, &pctx
)) {
1371 ext2fs_file_acl_block_set(&inode
, 0);
1376 if (inode
.i_dir_acl
&&
1377 LINUX_S_ISDIR(inode
.i_mode
)) {
1378 if (fix_problem(ctx
, PR_2_DIR_ACL_ZERO
, &pctx
)) {
1379 inode
.i_dir_acl
= 0;
1386 e2fsck_write_inode(ctx
, ino
, &inode
, "process_bad_inode");
1387 if (!not_fixed
&& ctx
->inode_bad_map
)
1388 ext2fs_unmark_inode_bitmap2(ctx
->inode_bad_map
, ino
);
1394 * allocate_dir_block --- this function allocates a new directory
1395 * block for a particular inode; this is done if a directory has
1396 * a "hole" in it, or if a directory has a illegal block number
1397 * that was zeroed out and now needs to be replaced.
1399 static int allocate_dir_block(e2fsck_t ctx
,
1400 struct ext2_db_entry2
*db
,
1401 char *buf
EXT2FS_ATTR((unused
)),
1402 struct problem_context
*pctx
)
1404 ext2_filsys fs
= ctx
->fs
;
1407 struct ext2_inode inode
;
1409 if (fix_problem(ctx
, PR_2_DIRECTORY_HOLE
, pctx
) == 0)
1413 * Read the inode and block bitmaps in; we'll be messing with
1416 e2fsck_read_bitmaps(ctx
);
1419 * First, find a free block
1421 pctx
->errcode
= ext2fs_new_block2(fs
, 0, ctx
->block_found_map
, &blk
);
1422 if (pctx
->errcode
) {
1423 pctx
->str
= "ext2fs_new_block";
1424 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);
1427 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, blk
);
1428 ext2fs_mark_block_bitmap2(fs
->block_map
, blk
);
1429 ext2fs_mark_bb_dirty(fs
);
1432 * Now let's create the actual data block for the inode
1435 pctx
->errcode
= ext2fs_new_dir_block(fs
, 0, 0, &block
);
1437 pctx
->errcode
= ext2fs_new_dir_block(fs
, db
->ino
,
1438 EXT2_ROOT_INO
, &block
);
1440 if (pctx
->errcode
) {
1441 pctx
->str
= "ext2fs_new_dir_block";
1442 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);
1446 pctx
->errcode
= ext2fs_write_dir_block(fs
, blk
, block
);
1447 ext2fs_free_mem(&block
);
1448 if (pctx
->errcode
) {
1449 pctx
->str
= "ext2fs_write_dir_block";
1450 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);
1455 * Update the inode block count
1457 e2fsck_read_inode(ctx
, db
->ino
, &inode
, "allocate_dir_block");
1458 ext2fs_iblk_add_blocks(fs
, &inode
, 1);
1459 if (inode
.i_size
< (db
->blockcnt
+1) * fs
->blocksize
)
1460 inode
.i_size
= (db
->blockcnt
+1) * fs
->blocksize
;
1461 e2fsck_write_inode(ctx
, db
->ino
, &inode
, "allocate_dir_block");
1464 * Finally, update the block pointers for the inode
1467 pctx
->errcode
= ext2fs_bmap2(fs
, db
->ino
, &inode
, 0, BMAP_SET
,
1468 db
->blockcnt
, 0, &blk
);
1469 if (pctx
->errcode
) {
1470 pctx
->str
= "ext2fs_block_iterate";
1471 fix_problem(ctx
, PR_2_ALLOC_DIRBOCK
, pctx
);