]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/pass2.c
libext2fs: add checksums to the end of directory leaf nodes
[thirdparty/e2fsprogs.git] / e2fsck / pass2.c
CommitLineData
3839e657
TT
1/*
2 * pass2.c --- check directory structure
efc6f628 3 *
21c84b71
TT
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
efc6f628 10 *
3839e657
TT
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:
14 *
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)
efc6f628 19 * should be less than (rec_len - 8).
3839e657
TT
20 * - The inode number in the directory entry should be within
21 * legal bounds.
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 '..'.
26 *
27 * To minimize disk seek time, the directory blocks are processed in
28 * sorted order of block numbers.
29 *
30 * Pass 2 also collects the following information:
31 * - The inode numbers of the subdirectories for each directory.
32 *
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
3839e657
TT
38 *
39 * Pass 2 frees the following data structures
40 * - The inode_bad_map bitmap
aa4115a4 41 * - The inode_reg_map bitmap
3839e657
TT
42 */
43
b969b1b8 44#define _GNU_SOURCE 1 /* get strnlen() */
d1154eb4 45#include "config.h"
48e6e813
TT
46#include <string.h>
47
3839e657 48#include "e2fsck.h"
21c84b71 49#include "problem.h"
0926668d 50#include "dict.h"
3839e657 51
aa4115a4
TT
52#ifdef NO_INLINE_FUNCS
53#define _INLINE_
54#else
55#define _INLINE_ inline
56#endif
57
ad4fa466 58/* #define DX_DEBUG */
8fdc9985 59
3839e657
TT
60/*
61 * Keeps track of how many times an inode is referenced.
62 */
4035f40b 63static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf);
3839e657 64static int check_dir_block(ext2_filsys fs,
6dc64392 65 struct ext2_db_entry2 *dir_blocks_info,
54dc7ca2 66 void *priv_data);
1b6bf175 67static int allocate_dir_block(e2fsck_t ctx,
6dc64392 68 struct ext2_db_entry2 *dir_blocks_info,
21c84b71 69 char *buf, struct problem_context *pctx);
8fdc9985 70static void clear_htree(e2fsck_t ctx, ext2_ino_t ino);
ad4fa466
TT
71static int htree_depth(struct dx_dir_info *dx_dir,
72 struct dx_dirblock_info *dx_db);
ea1959f0 73static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b);
3839e657 74
21c84b71
TT
75struct check_dir_struct {
76 char *buf;
77 struct problem_context pctx;
f8188fff 78 int count, max;
1b6bf175 79 e2fsck_t ctx;
efc6f628 80};
21c84b71 81
08b21301 82void e2fsck_pass2(e2fsck_t ctx)
3839e657 83{
a4742691
TT
84 struct ext2_super_block *sb = ctx->fs->super;
85 struct problem_context pctx;
86 ext2_filsys fs = ctx->fs;
87 char *buf;
8bf191e8 88#ifdef RESOURCE_TRACK
3839e657 89 struct resource_track rtrack;
8bf191e8 90#endif
21c84b71 91 struct check_dir_struct cd;
8fdc9985
TT
92 struct dx_dir_info *dx_dir;
93 struct dx_dirblock_info *dx_db, *dx_parent;
830b44f4 94 unsigned int save_type;
54434927 95 int b;
ad4fa466 96 int i, depth;
8fdc9985
TT
97 problem_t code;
98 int bad_dir;
99
6d96b00d 100 init_resource_track(&rtrack, ctx->fs->io);
1b6bf175
TT
101 clear_problem_context(&cd.pctx);
102
3839e657
TT
103#ifdef MTRACE
104 mtrace_print("Pass 2");
105#endif
106
1b6bf175
TT
107 if (!(ctx->options & E2F_OPT_PREEN))
108 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
109
efc6f628 110 e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT,
34b9f796
TT
111 &ctx->inode_count);
112 if (ctx->inode_count)
113 cd.pctx.errcode = 0;
830b44f4
TT
114 else {
115 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
116 "inode_count", &save_type);
efc6f628 117 cd.pctx.errcode = ext2fs_create_icount2(fs,
34b9f796 118 EXT2_ICOUNT_OPT_INCREMENT,
1b6bf175
TT
119 0, ctx->inode_link_info,
120 &ctx->inode_count);
830b44f4
TT
121 fs->default_bitmap_type = save_type;
122 }
1b6bf175
TT
123 if (cd.pctx.errcode) {
124 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
08b21301
TT
125 ctx->flags |= E2F_FLAG_ABORT;
126 return;
21c84b71 127 }
bcf9c5d4 128 buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
54dc7ca2 129 "directory scan buffer");
3839e657 130
21c84b71
TT
131 /*
132 * Set up the parent pointer for the root directory, if
133 * present. (If the root directory is not present, we will
134 * create it in pass 3.)
135 */
28db82a8 136 (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
21c84b71
TT
137
138 cd.buf = buf;
1b6bf175 139 cd.ctx = ctx;
f75c28de 140 cd.count = 1;
6dc64392 141 cd.max = ext2fs_dblist_count2(fs->dblist);
f75c28de
TT
142
143 if (ctx->progress)
144 (void) (ctx->progress)(ctx, 2, 0, cd.max);
ea1959f0
TT
145
146 if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX)
6dc64392 147 ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp);
efc6f628 148
6dc64392
VAH
149 cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_block,
150 &cd);
49a7360b 151 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
08b21301 152 return;
6267ee49
AD
153
154 if (ctx->flags & E2F_FLAG_RESTART_LATER) {
155 ctx->flags |= E2F_FLAG_RESTART;
156 return;
157 }
158
1b6bf175
TT
159 if (cd.pctx.errcode) {
160 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
08b21301
TT
161 ctx->flags |= E2F_FLAG_ABORT;
162 return;
7ac02a5e 163 }
8fdc9985
TT
164
165#ifdef ENABLE_HTREE
166 for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
4cae0452
TT
167 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
168 return;
62acaa1d 169 if (dx_dir->numblocks == 0)
8fdc9985
TT
170 continue;
171 clear_problem_context(&pctx);
172 bad_dir = 0;
173 pctx.dir = dx_dir->ino;
174 dx_db = dx_dir->dx_block;
175 if (dx_db->flags & DX_FLAG_REFERENCED)
176 dx_db->flags |= DX_FLAG_DUP_REF;
177 else
178 dx_db->flags |= DX_FLAG_REFERENCED;
179 /*
180 * Find all of the first and last leaf blocks, and
181 * update their parent's min and max hash values
182 */
183 for (b=0, dx_db = dx_dir->dx_block;
184 b < dx_dir->numblocks;
185 b++, dx_db++) {
186 if ((dx_db->type != DX_DIRBLOCK_LEAF) ||
187 !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST)))
188 continue;
189 dx_parent = &dx_dir->dx_block[dx_db->parent];
190 /*
191 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
192 */
193 if (dx_db->flags & DX_FLAG_FIRST)
194 dx_parent->min_hash = dx_db->min_hash;
195 /*
196 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
197 */
198 if (dx_db->flags & DX_FLAG_LAST)
199 dx_parent->max_hash = dx_db->max_hash;
200 }
efc6f628 201
8fdc9985
TT
202 for (b=0, dx_db = dx_dir->dx_block;
203 b < dx_dir->numblocks;
204 b++, dx_db++) {
205 pctx.blkcount = b;
206 pctx.group = dx_db->parent;
207 code = 0;
208 if (!(dx_db->flags & DX_FLAG_FIRST) &&
209 (dx_db->min_hash < dx_db->node_min_hash)) {
210 pctx.blk = dx_db->min_hash;
211 pctx.blk2 = dx_db->node_min_hash;
212 code = PR_2_HTREE_MIN_HASH;
213 fix_problem(ctx, code, &pctx);
214 bad_dir++;
215 }
ad4fa466
TT
216 if (dx_db->type == DX_DIRBLOCK_LEAF) {
217 depth = htree_depth(dx_dir, dx_db);
218 if (depth != dx_dir->depth) {
e5e12db9 219 pctx.num = dx_dir->depth;
ad4fa466
TT
220 code = PR_2_HTREE_BAD_DEPTH;
221 fix_problem(ctx, code, &pctx);
222 bad_dir++;
223 }
224 }
8fdc9985 225 /*
efc6f628 226 * This test doesn't apply for the root block
8fdc9985
TT
227 * at block #0
228 */
229 if (b &&
230 (dx_db->max_hash > dx_db->node_max_hash)) {
231 pctx.blk = dx_db->max_hash;
232 pctx.blk2 = dx_db->node_max_hash;
233 code = PR_2_HTREE_MAX_HASH;
234 fix_problem(ctx, code, &pctx);
503f9e7f 235 bad_dir++;
8fdc9985
TT
236 }
237 if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
238 code = PR_2_HTREE_NOTREF;
239 fix_problem(ctx, code, &pctx);
240 bad_dir++;
241 } else if (dx_db->flags & DX_FLAG_DUP_REF) {
242 code = PR_2_HTREE_DUPREF;
243 fix_problem(ctx, code, &pctx);
244 bad_dir++;
245 }
8fdc9985
TT
246 }
247 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
248 clear_htree(ctx, dx_dir->ino);
62acaa1d 249 dx_dir->numblocks = 0;
8fdc9985 250 }
8fdc9985 251 }
23f75f6e 252 e2fsck_free_dx_dir_info(ctx);
8fdc9985 253#endif
c4e3d3f3 254 ext2fs_free_mem(&buf);
21c84b71
TT
255 ext2fs_free_dblist(fs->dblist);
256
1b6bf175
TT
257 if (ctx->inode_bad_map) {
258 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
259 ctx->inode_bad_map = 0;
3839e657 260 }
aa4115a4
TT
261 if (ctx->inode_reg_map) {
262 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
263 ctx->inode_reg_map = 0;
264 }
a4742691
TT
265
266 clear_problem_context(&pctx);
267 if (ctx->large_files) {
268 if (!(sb->s_feature_ro_compat &
269 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
270 fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
271 sb->s_feature_ro_compat |=
272 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
0cfce7f7 273 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
a4742691
TT
274 ext2fs_mark_super_dirty(fs);
275 }
276 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
277 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
278 ext2fs_update_dynamic_rev(fs);
279 ext2fs_mark_super_dirty(fs);
280 }
a4742691 281 }
efc6f628 282
9facd076 283 print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io);
3839e657
TT
284}
285
ad4fa466
TT
286#define MAX_DEPTH 32000
287static int htree_depth(struct dx_dir_info *dx_dir,
288 struct dx_dirblock_info *dx_db)
289{
290 int depth = 0;
291
292 while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
293 dx_db = &dx_dir->dx_block[dx_db->parent];
294 depth++;
295 }
296 return depth;
297}
298
0926668d
TT
299static int dict_de_cmp(const void *a, const void *b)
300{
520ead37 301 const struct ext2_dir_entry *de_a, *de_b;
0926668d
TT
302 int a_len, b_len;
303
520ead37 304 de_a = (const struct ext2_dir_entry *) a;
0926668d 305 a_len = de_a->name_len & 0xFF;
520ead37 306 de_b = (const struct ext2_dir_entry *) b;
0926668d
TT
307 b_len = de_b->name_len & 0xFF;
308
309 if (a_len != b_len)
310 return (a_len - b_len);
311
312 return strncmp(de_a->name, de_b->name, a_len);
313}
ad4fa466 314
ea1959f0
TT
315/*
316 * This is special sort function that makes sure that directory blocks
317 * with a dirblock of zero are sorted to the beginning of the list.
318 * This guarantees that the root node of the htree directories are
319 * processed first, so we know what hash version to use.
320 */
321static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
322{
6dc64392
VAH
323 const struct ext2_db_entry2 *db_a =
324 (const struct ext2_db_entry2 *) a;
325 const struct ext2_db_entry2 *db_b =
326 (const struct ext2_db_entry2 *) b;
ea1959f0
TT
327
328 if (db_a->blockcnt && !db_b->blockcnt)
329 return 1;
330
331 if (!db_a->blockcnt && db_b->blockcnt)
332 return -1;
efc6f628 333
ea1959f0
TT
334 if (db_a->blk != db_b->blk)
335 return (int) (db_a->blk - db_b->blk);
efc6f628 336
ea1959f0
TT
337 if (db_a->ino != db_b->ino)
338 return (int) (db_a->ino - db_b->ino);
339
340 return (int) (db_a->blockcnt - db_b->blockcnt);
341}
342
343
3839e657
TT
344/*
345 * Make sure the first entry in the directory is '.', and that the
346 * directory entry is sane.
347 */
1b6bf175 348static int check_dot(e2fsck_t ctx,
3839e657 349 struct ext2_dir_entry *dirent,
86c627ec 350 ext2_ino_t ino, struct problem_context *pctx)
3839e657
TT
351{
352 struct ext2_dir_entry *nextdir;
8a480350 353 unsigned int rec_len, new_len;
3839e657
TT
354 int status = 0;
355 int created = 0;
21c84b71 356 int problem = 0;
efc6f628 357
21c84b71
TT
358 if (!dirent->inode)
359 problem = PR_2_MISSING_DOT;
b6f79831 360 else if (((dirent->name_len & 0xFF) != 1) ||
21c84b71
TT
361 (dirent->name[0] != '.'))
362 problem = PR_2_1ST_NOT_DOT;
363 else if (dirent->name[1] != '\0')
364 problem = PR_2_DOT_NULL_TERM;
5dd77dbe 365
8a480350 366 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
21c84b71 367 if (problem) {
1b6bf175 368 if (fix_problem(ctx, problem, pctx)) {
5dd77dbe
TT
369 if (rec_len < 12)
370 rec_len = dirent->rec_len = 12;
3839e657
TT
371 dirent->inode = ino;
372 dirent->name_len = 1;
373 dirent->name[0] = '.';
21c84b71 374 dirent->name[1] = '\0';
3839e657
TT
375 status = 1;
376 created = 1;
3839e657
TT
377 }
378 }
3839e657 379 if (dirent->inode != ino) {
1b6bf175 380 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
3839e657
TT
381 dirent->inode = ino;
382 status = 1;
21c84b71 383 }
3839e657 384 }
5dd77dbe
TT
385 if (rec_len > 12) {
386 new_len = rec_len - 12;
3839e657 387 if (new_len > 12) {
3839e657 388 if (created ||
f8188fff 389 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
3839e657
TT
390 nextdir = (struct ext2_dir_entry *)
391 ((char *) dirent + 12);
392 dirent->rec_len = 12;
8a480350
TT
393 (void) ext2fs_set_rec_len(ctx->fs, new_len,
394 nextdir);
3839e657
TT
395 nextdir->inode = 0;
396 nextdir->name_len = 0;
397 status = 1;
398 }
399 }
400 }
401 return status;
402}
403
404/*
405 * Make sure the second entry in the directory is '..', and that the
406 * directory entry is sane. We do not check the inode number of '..'
407 * here; this gets done in pass 3.
408 */
1b6bf175 409static int check_dotdot(e2fsck_t ctx,
3839e657 410 struct ext2_dir_entry *dirent,
28db82a8 411 ext2_ino_t ino, struct problem_context *pctx)
3839e657 412{
cf5301d7
AD
413 int problem = 0;
414 unsigned int rec_len;
efc6f628 415
21c84b71
TT
416 if (!dirent->inode)
417 problem = PR_2_MISSING_DOT_DOT;
b6f79831 418 else if (((dirent->name_len & 0xFF) != 2) ||
21c84b71
TT
419 (dirent->name[0] != '.') ||
420 (dirent->name[1] != '.'))
421 problem = PR_2_2ND_NOT_DOT_DOT;
422 else if (dirent->name[2] != '\0')
423 problem = PR_2_DOT_DOT_NULL_TERM;
424
8a480350 425 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
21c84b71 426 if (problem) {
1b6bf175 427 if (fix_problem(ctx, problem, pctx)) {
5dd77dbe 428 if (rec_len < 12)
21c84b71 429 dirent->rec_len = 12;
3839e657
TT
430 /*
431 * Note: we don't have the parent inode just
432 * yet, so we will fill it in with the root
433 * inode. This will get fixed in pass 3.
434 */
435 dirent->inode = EXT2_ROOT_INO;
436 dirent->name_len = 2;
437 dirent->name[0] = '.';
438 dirent->name[1] = '.';
21c84b71 439 dirent->name[2] = '\0';
3839e657 440 return 1;
efc6f628 441 }
3839e657
TT
442 return 0;
443 }
28db82a8
TT
444 if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) {
445 fix_problem(ctx, PR_2_NO_DIRINFO, pctx);
446 return -1;
447 }
3839e657
TT
448 return 0;
449}
450
451/*
452 * Check to make sure a directory entry doesn't contain any illegal
453 * characters.
454 */
1b6bf175 455static int check_name(e2fsck_t ctx,
3839e657 456 struct ext2_dir_entry *dirent,
efc6f628 457 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
54434927 458 struct problem_context *pctx)
3839e657
TT
459{
460 int i;
461 int fixup = -1;
3839e657 462 int ret = 0;
efc6f628 463
b6f79831 464 for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
3839e657
TT
465 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
466 if (fixup < 0) {
1b6bf175 467 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
3839e657
TT
468 }
469 if (fixup) {
470 dirent->name[i] = '.';
471 ret = 1;
21c84b71 472 }
3839e657
TT
473 }
474 }
475 return ret;
476}
477
aa4115a4
TT
478/*
479 * Check the directory filetype (if present)
480 */
481static _INLINE_ int check_filetype(e2fsck_t ctx,
54434927
TT
482 struct ext2_dir_entry *dirent,
483 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
484 struct problem_context *pctx)
aa4115a4
TT
485{
486 int filetype = dirent->name_len >> 8;
487 int should_be = EXT2_FT_UNKNOWN;
488 struct ext2_inode inode;
489
490 if (!(ctx->fs->super->s_feature_incompat &
7847c1d4
TT
491 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
492 if (filetype == 0 ||
493 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
494 return 0;
495 dirent->name_len = dirent->name_len & 0xFF;
496 return 1;
497 }
aa4115a4 498
c5d2f50d 499 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) {
aa4115a4 500 should_be = EXT2_FT_DIR;
c5d2f50d 501 } else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map,
aa4115a4
TT
502 dirent->inode)) {
503 should_be = EXT2_FT_REG_FILE;
504 } else if (ctx->inode_bad_map &&
c5d2f50d 505 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
aa4115a4
TT
506 dirent->inode))
507 should_be = 0;
508 else {
509 e2fsck_read_inode(ctx, dirent->inode, &inode,
510 "check_filetype");
6fdc7a32 511 should_be = ext2_file_type(inode.i_mode);
aa4115a4
TT
512 }
513 if (filetype == should_be)
514 return 0;
515 pctx->num = should_be;
516
517 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
518 pctx) == 0)
519 return 0;
efc6f628 520
aa4115a4
TT
521 dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8;
522 return 1;
523}
524
8fdc9985
TT
525#ifdef ENABLE_HTREE
526static void parse_int_node(ext2_filsys fs,
6dc64392 527 struct ext2_db_entry2 *db,
8fdc9985
TT
528 struct check_dir_struct *cd,
529 struct dx_dir_info *dx_dir,
07307114 530 char *block_buf, int failed_csum)
8fdc9985
TT
531{
532 struct ext2_dx_root_info *root;
533 struct ext2_dx_entry *ent;
534 struct ext2_dx_countlimit *limit;
535 struct dx_dirblock_info *dx_db;
ad4fa466 536 int i, expect_limit, count;
8fdc9985
TT
537 blk_t blk;
538 ext2_dirhash_t min_hash = 0xffffffff;
539 ext2_dirhash_t max_hash = 0;
ad4fa466 540 ext2_dirhash_t hash = 0, prev_hash;
07307114 541 int csum_size = 0;
8fdc9985
TT
542
543 if (db->blockcnt == 0) {
544 root = (struct ext2_dx_root_info *) (block_buf + 24);
efc6f628 545
8fdc9985
TT
546#ifdef DX_DEBUG
547 printf("Root node dump:\n");
8deb80a5 548 printf("\t Reserved zero: %u\n", root->reserved_zero);
8fdc9985
TT
549 printf("\t Hash Version: %d\n", root->hash_version);
550 printf("\t Info length: %d\n", root->info_length);
551 printf("\t Indirect levels: %d\n", root->indirect_levels);
552 printf("\t Flags: %d\n", root->unused_flags);
553#endif
554
555 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
07307114
DW
556
557 if (failed_csum &&
558 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
559 fix_problem(cd->ctx, PR_2_HTREE_ROOT_CSUM_INVALID,
560 &cd->pctx)))
561 goto clear_and_exit;
8fdc9985
TT
562 } else {
563 ent = (struct ext2_dx_entry *) (block_buf+8);
07307114
DW
564
565 if (failed_csum &&
566 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
567 fix_problem(cd->ctx, PR_2_HTREE_NODE_CSUM_INVALID,
568 &cd->pctx)))
569 goto clear_and_exit;
8fdc9985 570 }
07307114 571
8fdc9985
TT
572 limit = (struct ext2_dx_countlimit *) ent;
573
574#ifdef DX_DEBUG
efc6f628 575 printf("Number of entries (count): %d\n",
8132d840 576 ext2fs_le16_to_cpu(limit->count));
efc6f628 577 printf("Number of entries (limit): %d\n",
8132d840 578 ext2fs_le16_to_cpu(limit->limit));
8fdc9985
TT
579#endif
580
8132d840 581 count = ext2fs_le16_to_cpu(limit->count);
07307114
DW
582 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
583 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
584 csum_size = sizeof(struct ext2_dx_tail);
585 expect_limit = (fs->blocksize -
586 (csum_size + ((char *) ent - block_buf))) /
587 sizeof(struct ext2_dx_entry);
8132d840
TT
588 if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
589 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
ad4fa466
TT
590 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
591 goto clear_and_exit;
592 }
8132d840
TT
593 if (count > expect_limit) {
594 cd->pctx.num = count;
ad4fa466
TT
595 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
596 goto clear_and_exit;
597 count = expect_limit;
598 }
efc6f628 599
ad4fa466
TT
600 for (i=0; i < count; i++) {
601 prev_hash = hash;
8132d840 602 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
8fdc9985 603#ifdef DX_DEBUG
8deb80a5 604 printf("Entry #%d: Hash 0x%08x, block %u\n", i,
8132d840 605 hash, ext2fs_le32_to_cpu(ent[i].block));
8fdc9985 606#endif
8132d840 607 blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff;
8fdc9985 608 /* Check to make sure the block is valid */
977ac873 609 if (blk >= (blk_t) dx_dir->numblocks) {
b7a00563 610 cd->pctx.blk = blk;
8fdc9985 611 if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
ad4fa466
TT
612 &cd->pctx))
613 goto clear_and_exit;
977ac873 614 continue;
8fdc9985 615 }
ad4fa466
TT
616 if (hash < prev_hash &&
617 fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
618 goto clear_and_exit;
8fdc9985
TT
619 dx_db = &dx_dir->dx_block[blk];
620 if (dx_db->flags & DX_FLAG_REFERENCED) {
621 dx_db->flags |= DX_FLAG_DUP_REF;
622 } else {
623 dx_db->flags |= DX_FLAG_REFERENCED;
624 dx_db->parent = db->blockcnt;
625 }
626 if (hash < min_hash)
627 min_hash = hash;
628 if (hash > max_hash)
629 max_hash = hash;
630 dx_db->node_min_hash = hash;
8132d840 631 if ((i+1) < count)
efc6f628 632 dx_db->node_max_hash =
8132d840 633 ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
8fdc9985
TT
634 else {
635 dx_db->node_max_hash = 0xfffffffe;
636 dx_db->flags |= DX_FLAG_LAST;
637 }
638 if (i == 0)
639 dx_db->flags |= DX_FLAG_FIRST;
640 }
641#ifdef DX_DEBUG
642 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
643 db->blockcnt, min_hash, max_hash);
644#endif
645 dx_db = &dx_dir->dx_block[db->blockcnt];
646 dx_db->min_hash = min_hash;
647 dx_db->max_hash = max_hash;
ad4fa466
TT
648 return;
649
650clear_and_exit:
651 clear_htree(cd->ctx, cd->pctx.ino);
652 dx_dir->numblocks = 0;
07307114 653 e2fsck_rehash_dir_later(cd->ctx, cd->pctx.ino);
8fdc9985
TT
654}
655#endif /* ENABLE_HTREE */
aa4115a4 656
e70ae99e
TT
657/*
658 * Given a busted directory, try to salvage it somehow.
efc6f628 659 *
e70ae99e 660 */
ad4fa466 661static void salvage_directory(ext2_filsys fs,
e70ae99e
TT
662 struct ext2_dir_entry *dirent,
663 struct ext2_dir_entry *prev,
54434927 664 unsigned int *offset)
e70ae99e
TT
665{
666 char *cp = (char *) dirent;
8a480350
TT
667 int left;
668 unsigned int rec_len, prev_rec_len;
642935c0 669 unsigned int name_len = dirent->name_len & 0xFF;
e70ae99e 670
8a480350 671 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
5dd77dbe
TT
672 left = fs->blocksize - *offset - rec_len;
673
e70ae99e
TT
674 /*
675 * Special case of directory entry of size 8: copy what's left
676 * of the directory block up to cover up the invalid hole.
677 */
5dd77dbe 678 if ((left >= 12) && (rec_len == 8)) {
e70ae99e
TT
679 memmove(cp, cp+8, left);
680 memset(cp + left, 0, 8);
ad4fa466
TT
681 return;
682 }
683 /*
684 * If the directory entry overruns the end of the directory
685 * block, and the name is small enough to fit, then adjust the
686 * record length.
687 */
688 if ((left < 0) &&
8a480350
TT
689 ((int) rec_len + left > 8) &&
690 (name_len + 8 <= (int) rec_len + left) &&
ad4fa466
TT
691 dirent->inode <= fs->super->s_inodes_count &&
692 strnlen(dirent->name, name_len) == name_len) {
8a480350 693 (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent);
ad4fa466 694 return;
e70ae99e
TT
695 }
696 /*
575307cc
KS
697 * If the record length of the directory entry is a multiple
698 * of four, and not too big, such that it is valid, let the
699 * previous directory entry absorb the invalid one.
e70ae99e 700 */
5dd77dbe
TT
701 if (prev && rec_len && (rec_len % 4) == 0 &&
702 (*offset + rec_len <= fs->blocksize)) {
8a480350
TT
703 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
704 prev_rec_len += rec_len;
705 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
5dd77dbe 706 *offset += rec_len;
ad4fa466 707 return;
e70ae99e
TT
708 }
709 /*
710 * Default salvage method --- kill all of the directory
711 * entries for the rest of the block. We will either try to
712 * absorb it into the previous directory entry, or create a
713 * new empty directory entry the rest of the directory block.
714 */
715 if (prev) {
8a480350
TT
716 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
717 prev_rec_len += fs->blocksize - *offset;
718 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
ad4fa466 719 *offset = fs->blocksize;
e70ae99e 720 } else {
8a480350
TT
721 rec_len = fs->blocksize - *offset;
722 (void) ext2fs_set_rec_len(fs, rec_len, dirent);
e70ae99e
TT
723 dirent->name_len = 0;
724 dirent->inode = 0;
e70ae99e 725 }
e70ae99e
TT
726}
727
3839e657 728static int check_dir_block(ext2_filsys fs,
6dc64392 729 struct ext2_db_entry2 *db,
54dc7ca2 730 void *priv_data)
3839e657 731{
8fdc9985
TT
732 struct dx_dir_info *dx_dir;
733#ifdef ENABLE_HTREE
734 struct dx_dirblock_info *dx_db = 0;
735#endif /* ENABLE_HTREE */
e70ae99e 736 struct ext2_dir_entry *dirent, *prev;
8fdc9985 737 ext2_dirhash_t hash;
54434927 738 unsigned int offset = 0;
3839e657 739 int dir_modified = 0;
21c84b71 740 int dot_state;
03fa6f8a 741 unsigned int rec_len;
6dc64392 742 blk64_t block_nr = db->blk;
86c627ec 743 ext2_ino_t ino = db->ino;
28db82a8 744 ext2_ino_t subdir_parent;
21c84b71 745 __u16 links;
54dc7ca2 746 struct check_dir_struct *cd;
1b6bf175
TT
747 char *buf;
748 e2fsck_t ctx;
749 int problem;
ea1959f0 750 struct ext2_dx_root_info *root;
e8254bfd 751 struct ext2_dx_countlimit *limit;
0926668d
TT
752 static dict_t de_dict;
753 struct problem_context pctx;
754 int dups_found = 0;
28db82a8 755 int ret;
07307114 756 int dx_csum_size = 0;
81683c6a 757 int failed_csum = 0;
1b6bf175 758
54dc7ca2 759 cd = (struct check_dir_struct *) priv_data;
1b6bf175
TT
760 buf = cd->buf;
761 ctx = cd->ctx;
f8188fff 762
49a7360b 763 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
4cae0452 764 return DIRENT_ABORT;
efc6f628 765
4cae0452
TT
766 if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
767 return DIRENT_ABORT;
efc6f628 768
07307114
DW
769 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
770 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
771 dx_csum_size = sizeof(struct ext2_dx_tail);
772
3839e657 773 /*
efc6f628 774 * Make sure the inode is still in use (could have been
3839e657
TT
775 * deleted in the duplicate/bad blocks pass.
776 */
c5d2f50d 777 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino)))
3839e657 778 return 0;
50e1e10f 779
21c84b71
TT
780 cd->pctx.ino = ino;
781 cd->pctx.blk = block_nr;
782 cd->pctx.blkcount = db->blockcnt;
783 cd->pctx.ino2 = 0;
784 cd->pctx.dirent = 0;
785 cd->pctx.num = 0;
786
50e1e10f 787 if (db->blk == 0) {
1b6bf175 788 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
50e1e10f
TT
789 return 0;
790 block_nr = db->blk;
791 }
efc6f628 792
3839e657
TT
793 if (db->blockcnt)
794 dot_state = 2;
795 else
796 dot_state = 0;
797
0926668d
TT
798 if (ctx->dirs_to_hash &&
799 ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
800 dups_found++;
801
3839e657 802#if 0
f3db3566 803 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
3839e657
TT
804 db->blockcnt, ino);
805#endif
efc6f628 806
f85a9ae6 807 ehandler_operation(_("reading directory block"));
81683c6a 808 cd->pctx.errcode = ext2fs_read_dir_block4(fs, block_nr, buf, 0, ino);
e94bc631 809 ehandler_operation(0);
b9852cd8
TT
810 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
811 cd->pctx.errcode = 0; /* We'll handle this ourselves */
81683c6a
DW
812 else if (cd->pctx.errcode == EXT2_ET_DIR_CSUM_INVALID) {
813 cd->pctx.errcode = 0; /* We'll handle this ourselves */
814 failed_csum = 1;
815 }
1b6bf175 816 if (cd->pctx.errcode) {
08b21301
TT
817 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
818 ctx->flags |= E2F_FLAG_ABORT;
819 return DIRENT_ABORT;
820 }
1b6bf175 821 memset(buf, 0, fs->blocksize);
3839e657 822 }
8fdc9985
TT
823#ifdef ENABLE_HTREE
824 dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
62acaa1d 825 if (dx_dir && dx_dir->numblocks) {
8fdc9985 826 if (db->blockcnt >= dx_dir->numblocks) {
efc6f628 827 if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK,
d45edec0
TT
828 &pctx)) {
829 clear_htree(ctx, ino);
830 dx_dir->numblocks = 0;
831 dx_db = 0;
832 goto out_htree;
833 }
834 fatal_error(ctx, _("Can not continue."));
8fdc9985
TT
835 }
836 dx_db = &dx_dir->dx_block[db->blockcnt];
837 dx_db->type = DX_DIRBLOCK_LEAF;
838 dx_db->phys = block_nr;
839 dx_db->min_hash = ~0;
840 dx_db->max_hash = 0;
efc6f628 841
8fdc9985 842 dirent = (struct ext2_dir_entry *) buf;
8a480350 843 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
e8254bfd 844 limit = (struct ext2_dx_countlimit *) (buf+8);
8fdc9985 845 if (db->blockcnt == 0) {
ea1959f0 846 root = (struct ext2_dx_root_info *) (buf + 24);
8fdc9985
TT
847 dx_db->type = DX_DIRBLOCK_ROOT;
848 dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
ea1959f0
TT
849 if ((root->reserved_zero ||
850 root->info_length < 8 ||
851 root->indirect_levels > 1) &&
852 fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
853 clear_htree(ctx, ino);
854 dx_dir->numblocks = 0;
855 dx_db = 0;
f77704e4 856 }
ea1959f0 857 dx_dir->hashversion = root->hash_version;
f77704e4
TT
858 if ((dx_dir->hashversion <= EXT2_HASH_TEA) &&
859 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
860 dx_dir->hashversion += 3;
ad4fa466 861 dx_dir->depth = root->indirect_levels + 1;
8fdc9985 862 } else if ((dirent->inode == 0) &&
5dd77dbe 863 (rec_len == fs->blocksize) &&
e8254bfd 864 (dirent->name_len == 0) &&
efc6f628 865 (ext2fs_le16_to_cpu(limit->limit) ==
07307114 866 ((fs->blocksize - (8 + dx_csum_size)) /
8132d840 867 sizeof(struct ext2_dx_entry))))
8fdc9985
TT
868 dx_db->type = DX_DIRBLOCK_NODE;
869 }
d45edec0 870out_htree:
8fdc9985 871#endif /* ENABLE_HTREE */
3839e657 872
0926668d 873 dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
e70ae99e 874 prev = 0;
3839e657 875 do {
49a7360b
JS
876 int group;
877 ext2_ino_t first_unused_inode;
878
1b6bf175 879 problem = 0;
3839e657 880 dirent = (struct ext2_dir_entry *) (buf + offset);
8a480350 881 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
21c84b71
TT
882 cd->pctx.dirent = dirent;
883 cd->pctx.num = offset;
5dd77dbe
TT
884 if (((offset + rec_len) > fs->blocksize) ||
885 (rec_len < 12) ||
886 ((rec_len % 4) != 0) ||
03fa6f8a 887 (((dirent->name_len & (unsigned) 0xFF)+8) > rec_len)) {
1b6bf175 888 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
ad4fa466 889 salvage_directory(fs, dirent, prev, &offset);
3839e657 890 dir_modified++;
e70ae99e 891 continue;
21c84b71 892 } else
0926668d 893 goto abort_free_dict;
3839e657 894 }
50e1e10f 895
e70ae99e 896 if (dot_state == 0) {
1b6bf175 897 if (check_dot(ctx, dirent, ino, &cd->pctx))
3839e657 898 dir_modified++;
e70ae99e 899 } else if (dot_state == 1) {
28db82a8
TT
900 ret = check_dotdot(ctx, dirent, ino, &cd->pctx);
901 if (ret < 0)
0926668d 902 goto abort_free_dict;
28db82a8 903 if (ret)
3839e657
TT
904 dir_modified++;
905 } else if (dirent->inode == ino) {
1b6bf175
TT
906 problem = PR_2_LINK_DOT;
907 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
3839e657
TT
908 dirent->inode = 0;
909 dir_modified++;
21c84b71 910 goto next;
3839e657
TT
911 }
912 }
efc6f628 913 if (!dirent->inode)
3839e657 914 goto next;
efc6f628 915
3839e657
TT
916 /*
917 * Make sure the inode listed is a legal one.
efc6f628 918 */
3839e657 919 if (((dirent->inode != EXT2_ROOT_INO) &&
7f88b043 920 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
3839e657 921 (dirent->inode > fs->super->s_inodes_count)) {
1b6bf175 922 problem = PR_2_BAD_INO;
1b6bf175 923 } else if (ctx->inode_bb_map &&
c5d2f50d 924 (ext2fs_test_inode_bitmap2(ctx->inode_bb_map,
1b6bf175
TT
925 dirent->inode))) {
926 /*
927 * If the inode is in a bad block, offer to
928 * clear it.
929 */
930 problem = PR_2_BB_INODE;
e70ae99e 931 } else if ((dot_state > 1) &&
b6f79831 932 ((dirent->name_len & 0xFF) == 1) &&
1b6bf175
TT
933 (dirent->name[0] == '.')) {
934 /*
935 * If there's a '.' entry in anything other
936 * than the first directory entry, it's a
937 * duplicate entry that should be removed.
938 */
939 problem = PR_2_DUP_DOT;
e70ae99e 940 } else if ((dot_state > 1) &&
b6f79831 941 ((dirent->name_len & 0xFF) == 2) &&
efc6f628 942 (dirent->name[0] == '.') &&
1b6bf175
TT
943 (dirent->name[1] == '.')) {
944 /*
945 * If there's a '..' entry in anything other
946 * than the second directory entry, it's a
947 * duplicate entry that should be removed.
948 */
949 problem = PR_2_DUP_DOT_DOT;
e70ae99e 950 } else if ((dot_state > 1) &&
1b6bf175
TT
951 (dirent->inode == EXT2_ROOT_INO)) {
952 /*
953 * Don't allow links to the root directory.
954 * We check this specially to make sure we
955 * catch this error case even if the root
956 * directory hasn't been created yet.
957 */
958 problem = PR_2_LINK_ROOT;
e70ae99e 959 } else if ((dot_state > 1) &&
c40db6d5
TT
960 (dirent->name_len & 0xFF) == 0) {
961 /*
962 * Don't allow zero-length directory names.
963 */
964 problem = PR_2_NULL_NAME;
21c84b71
TT
965 }
966
1b6bf175
TT
967 if (problem) {
968 if (fix_problem(ctx, problem, &cd->pctx)) {
21c84b71
TT
969 dirent->inode = 0;
970 dir_modified++;
971 goto next;
1b6bf175
TT
972 } else {
973 ext2fs_unmark_valid(fs);
974 if (problem == PR_2_BAD_INO)
975 goto next;
21c84b71 976 }
3839e657
TT
977 }
978
979 /*
980 * If the inode was marked as having bad fields in
981 * pass1, process it and offer to fix/clear it.
982 * (We wait until now so that we can display the
983 * pathname to the user.)
984 */
1b6bf175 985 if (ctx->inode_bad_map &&
c5d2f50d 986 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
3839e657 987 dirent->inode)) {
e72a9ba3 988 if (e2fsck_process_bad_inode(ctx, ino,
bcf9c5d4
TT
989 dirent->inode,
990 buf + fs->blocksize)) {
3839e657
TT
991 dirent->inode = 0;
992 dir_modified++;
993 goto next;
994 }
a02ce9df 995 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
08b21301 996 return DIRENT_ABORT;
3839e657
TT
997 }
998
49a7360b
JS
999 group = ext2fs_group_of_ino(fs, dirent->inode);
1000 first_unused_inode = group * fs->super->s_inodes_per_group +
1001 1 + fs->super->s_inodes_per_group -
d7cca6b0 1002 ext2fs_bg_itable_unused(fs, group);
49a7360b
JS
1003 cd->pctx.group = group;
1004
1005 /*
42e89ce7
TT
1006 * Check if the inode was missed out because
1007 * _INODE_UNINIT flag was set or bg_itable_unused was
1008 * incorrect. If so, clear the _INODE_UNINIT flag and
1009 * restart e2fsck. In the future it would be nice if
1010 * we could call a function in pass1.c that checks the
1011 * newly visible inodes.
49a7360b 1012 */
cd65a24e 1013 if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) {
6267ee49 1014 pctx.num = dirent->inode;
49a7360b
JS
1015 if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT,
1016 &cd->pctx)){
e633b58a 1017 ext2fs_bg_flags_clear(fs, group,
732c8cd5 1018 EXT2_BG_INODE_UNINIT);
42e89ce7 1019 ext2fs_mark_super_dirty(fs);
6267ee49 1020 ctx->flags |= E2F_FLAG_RESTART_LATER;
49a7360b
JS
1021 } else {
1022 ext2fs_unmark_valid(fs);
1023 if (problem == PR_2_BAD_INO)
1024 goto next;
1025 }
1026 } else if (dirent->inode >= first_unused_inode) {
6267ee49 1027 pctx.num = dirent->inode;
49a7360b 1028 if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){
d7cca6b0 1029 ext2fs_bg_itable_unused_set(fs, group, 0);
49a7360b 1030 ext2fs_mark_super_dirty(fs);
6267ee49 1031 ctx->flags |= E2F_FLAG_RESTART_LATER;
49a7360b
JS
1032 } else {
1033 ext2fs_unmark_valid(fs);
1034 if (problem == PR_2_BAD_INO)
1035 goto next;
1036 }
1037 }
1038
0433c1f1
TT
1039 /*
1040 * Offer to clear unused inodes; if we are going to be
1041 * restarting the scan due to bg_itable_unused being
1042 * wrong, then don't clear any inodes to avoid zapping
1043 * inodes that were skipped during pass1 due to an
1044 * incorrect bg_itable_unused; we'll get any real
1045 * problems after we restart.
1046 */
1047 if (!(ctx->flags & E2F_FLAG_RESTART_LATER) &&
97d26ce9
TT
1048 !(ext2fs_test_inode_bitmap2(ctx->inode_used_map,
1049 dirent->inode)))
49a7360b 1050 problem = PR_2_UNUSED_INODE;
49a7360b
JS
1051
1052 if (problem) {
1053 if (fix_problem(ctx, problem, &cd->pctx)) {
1054 dirent->inode = 0;
1055 dir_modified++;
1056 goto next;
1057 } else {
1058 ext2fs_unmark_valid(fs);
1059 if (problem == PR_2_BAD_INO)
1060 goto next;
1061 }
1062 }
1063
1b6bf175
TT
1064 if (check_name(ctx, dirent, ino, &cd->pctx))
1065 dir_modified++;
1066
aa4115a4
TT
1067 if (check_filetype(ctx, dirent, ino, &cd->pctx))
1068 dir_modified++;
1069
8fdc9985
TT
1070#ifdef ENABLE_HTREE
1071 if (dx_db) {
1072 ext2fs_dirhash(dx_dir->hashversion, dirent->name,
503f9e7f
TT
1073 (dirent->name_len & 0xFF),
1074 fs->super->s_hash_seed, &hash, 0);
8fdc9985
TT
1075 if (hash < dx_db->min_hash)
1076 dx_db->min_hash = hash;
1077 if (hash > dx_db->max_hash)
1078 dx_db->max_hash = hash;
1079 }
1080#endif
1081
3839e657
TT
1082 /*
1083 * If this is a directory, then mark its parent in its
1084 * dir_info structure. If the parent field is already
1085 * filled in, then this directory has more than one
1086 * hard link. We assume the first link is correct,
1087 * and ask the user if he/she wants to clear this one.
1088 */
e70ae99e 1089 if ((dot_state > 1) &&
c5d2f50d 1090 (ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
3839e657 1091 dirent->inode))) {
28db82a8
TT
1092 if (e2fsck_dir_info_get_parent(ctx, dirent->inode,
1093 &subdir_parent)) {
1b6bf175
TT
1094 cd->pctx.ino = dirent->inode;
1095 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
0926668d 1096 goto abort_free_dict;
3839e657 1097 }
28db82a8
TT
1098 if (subdir_parent) {
1099 cd->pctx.ino2 = subdir_parent;
1b6bf175 1100 if (fix_problem(ctx, PR_2_LINK_DIR,
21c84b71 1101 &cd->pctx)) {
3839e657
TT
1102 dirent->inode = 0;
1103 dir_modified++;
1104 goto next;
21c84b71
TT
1105 }
1106 cd->pctx.ino2 = 0;
28db82a8 1107 } else {
efc6f628 1108 (void) e2fsck_dir_info_set_parent(ctx,
28db82a8
TT
1109 dirent->inode, ino);
1110 }
3839e657 1111 }
0926668d
TT
1112
1113 if (dups_found) {
1114 ;
1115 } else if (dict_lookup(&de_dict, dirent)) {
1116 clear_problem_context(&pctx);
1117 pctx.ino = ino;
1118 pctx.dirent = dirent;
1119 fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
1120 if (!ctx->dirs_to_hash)
1121 ext2fs_u32_list_create(&ctx->dirs_to_hash, 50);
1122 if (ctx->dirs_to_hash)
1123 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1124 dups_found++;
1125 } else
1126 dict_alloc_insert(&de_dict, dirent, dirent);
efc6f628 1127
1b6bf175
TT
1128 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1129 &links);
21c84b71 1130 if (links > 1)
1b6bf175
TT
1131 ctx->fs_links_count++;
1132 ctx->fs_total_count++;
3839e657 1133 next:
e70ae99e 1134 prev = dirent;
5dd77dbe 1135 if (dir_modified)
8a480350 1136 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
5dd77dbe 1137 offset += rec_len;
e70ae99e 1138 dot_state++;
3839e657
TT
1139 } while (offset < fs->blocksize);
1140#if 0
1141 printf("\n");
1142#endif
8fdc9985
TT
1143#ifdef ENABLE_HTREE
1144 if (dx_db) {
1145#ifdef DX_DEBUG
1146 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1147 db->blockcnt, dx_db->type,
1148 dx_db->min_hash, dx_db->max_hash);
1149#endif
b7a00563 1150 cd->pctx.dir = cd->pctx.ino;
8fdc9985
TT
1151 if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1152 (dx_db->type == DX_DIRBLOCK_NODE))
81683c6a 1153 parse_int_node(fs, db, cd, dx_dir, buf, failed_csum);
8fdc9985
TT
1154 }
1155#endif /* ENABLE_HTREE */
3839e657 1156 if (offset != fs->blocksize) {
5dd77dbe 1157 cd->pctx.num = rec_len - fs->blocksize + offset;
1b6bf175
TT
1158 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1159 dirent->rec_len = cd->pctx.num;
1160 dir_modified++;
1161 }
3839e657
TT
1162 }
1163 if (dir_modified) {
81683c6a
DW
1164 cd->pctx.errcode = ext2fs_write_dir_block4(fs, block_nr, buf,
1165 0, ino);
1b6bf175 1166 if (cd->pctx.errcode) {
08b21301 1167 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
0926668d
TT
1168 &cd->pctx))
1169 goto abort_free_dict;
3839e657
TT
1170 }
1171 ext2fs_mark_changed(fs);
1172 }
0926668d 1173 dict_free_nodes(&de_dict);
3839e657 1174 return 0;
0926668d 1175abort_free_dict:
0926668d 1176 ctx->flags |= E2F_FLAG_ABORT;
49a7360b 1177 dict_free_nodes(&de_dict);
0926668d 1178 return DIRENT_ABORT;
3839e657
TT
1179}
1180
624e4a64
AK
1181struct del_block {
1182 e2fsck_t ctx;
1183 e2_blkcnt_t num;
1184};
1185
3839e657
TT
1186/*
1187 * This function is called to deallocate a block, and is an interator
1188 * functioned called by deallocate inode via ext2fs_iterate_block().
1189 */
1190static int deallocate_inode_block(ext2_filsys fs,
6dc64392 1191 blk64_t *block_nr,
54434927 1192 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
6dc64392 1193 blk64_t ref_block EXT2FS_ATTR((unused)),
54434927 1194 int ref_offset EXT2FS_ATTR((unused)),
133a56dc 1195 void *priv_data)
3839e657 1196{
624e4a64 1197 struct del_block *p = priv_data;
efc6f628 1198
1917875f 1199 if (HOLE_BLKADDR(*block_nr))
3839e657 1200 return 0;
1ba7a2f2 1201 if ((*block_nr < fs->super->s_first_data_block) ||
4efbac6f 1202 (*block_nr >= ext2fs_blocks_count(fs->super)))
1ba7a2f2 1203 return 0;
624e4a64 1204 ext2fs_unmark_block_bitmap2(p->ctx->block_found_map, *block_nr);
48f23054 1205 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
624e4a64 1206 p->num++;
3839e657
TT
1207 return 0;
1208}
efc6f628 1209
3839e657
TT
1210/*
1211 * This fuction deallocates an inode
1212 */
4035f40b 1213static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
3839e657 1214{
1b6bf175 1215 ext2_filsys fs = ctx->fs;
3839e657 1216 struct ext2_inode inode;
1b6bf175 1217 struct problem_context pctx;
0684a4f3 1218 __u32 count;
624e4a64 1219 struct del_block del_block;
efc6f628 1220
08b21301 1221 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
e3df15ab 1222 e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
1b6bf175
TT
1223 clear_problem_context(&pctx);
1224 pctx.ino = ino;
f3db3566 1225
3839e657
TT
1226 /*
1227 * Fix up the bitmaps...
1228 */
f8188fff 1229 e2fsck_read_bitmaps(ctx);
0684a4f3
TT
1230 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1231
0c80c44b 1232 if (ext2fs_file_acl_block(fs, &inode) &&
0684a4f3 1233 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
0c80c44b
TT
1234 pctx.errcode = ext2fs_adjust_ea_refcount2(fs,
1235 ext2fs_file_acl_block(fs, &inode),
1236 block_buf, -1, &count);
0684a4f3
TT
1237 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1238 pctx.errcode = 0;
1239 count = 1;
1240 }
1241 if (pctx.errcode) {
0c80c44b 1242 pctx.blk = ext2fs_file_acl_block(fs, &inode);
0684a4f3
TT
1243 fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1244 ctx->flags |= E2F_FLAG_ABORT;
1245 return;
1246 }
1247 if (count == 0) {
c5d2f50d 1248 ext2fs_unmark_block_bitmap2(ctx->block_found_map,
0c80c44b 1249 ext2fs_file_acl_block(fs, &inode));
48f23054 1250 ext2fs_block_alloc_stats2(fs,
0c80c44b 1251 ext2fs_file_acl_block(fs, &inode), -1);
0684a4f3 1252 }
0c80c44b 1253 ext2fs_file_acl_block_set(fs, &inode, 0);
0684a4f3 1254 }
3839e657 1255
0c80c44b 1256 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
3839e657 1257 return;
a4742691 1258
0bd0e593 1259 if (LINUX_S_ISREG(inode.i_mode) && EXT2_I_SIZE(&inode) >= 0x80000000UL)
a4742691
TT
1260 ctx->large_files--;
1261
624e4a64
AK
1262 del_block.ctx = ctx;
1263 del_block.num = 0;
6dc64392 1264 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
624e4a64
AK
1265 deallocate_inode_block,
1266 &del_block);
1b6bf175
TT
1267 if (pctx.errcode) {
1268 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
08b21301
TT
1269 ctx->flags |= E2F_FLAG_ABORT;
1270 return;
1b6bf175 1271 }
3839e657
TT
1272}
1273
8fdc9985
TT
1274/*
1275 * This fuction clears the htree flag on an inode
1276 */
1277static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1278{
1279 struct ext2_inode inode;
efc6f628 1280
8fdc9985
TT
1281 e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1282 inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1283 e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
b7a00563
TT
1284 if (ctx->dirs_to_hash)
1285 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
8fdc9985
TT
1286}
1287
1288
86c627ec 1289extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
bcf9c5d4 1290 ext2_ino_t ino, char *buf)
3839e657 1291{
1b6bf175 1292 ext2_filsys fs = ctx->fs;
3839e657 1293 struct ext2_inode inode;
3839e657 1294 int inode_modified = 0;
6c313fd4 1295 int not_fixed = 0;
1e3472c5 1296 unsigned char *frag, *fsize;
21c84b71 1297 struct problem_context pctx;
08b21301 1298 int problem = 0;
3839e657 1299
08b21301 1300 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
21c84b71
TT
1301
1302 clear_problem_context(&pctx);
1303 pctx.ino = ino;
1304 pctx.dir = dir;
1305 pctx.inode = &inode;
1306
0c80c44b 1307 if (ext2fs_file_acl_block(fs, &inode) &&
f76344fb
TT
1308 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1309 if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
0c80c44b 1310 ext2fs_file_acl_block_set(fs, &inode, 0);
f76344fb
TT
1311 inode_modified++;
1312 } else
1313 not_fixed++;
1314 }
6c313fd4 1315
50e1e10f
TT
1316 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1317 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1318 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
08b21301
TT
1319 !(LINUX_S_ISSOCK(inode.i_mode)))
1320 problem = PR_2_BAD_MODE;
fdbdea09 1321 else if (LINUX_S_ISCHR(inode.i_mode)
0684a4f3 1322 && !e2fsck_pass1_check_device_inode(fs, &inode))
08b21301 1323 problem = PR_2_BAD_CHAR_DEV;
fdbdea09 1324 else if (LINUX_S_ISBLK(inode.i_mode)
0684a4f3 1325 && !e2fsck_pass1_check_device_inode(fs, &inode))
08b21301 1326 problem = PR_2_BAD_BLOCK_DEV;
fdbdea09 1327 else if (LINUX_S_ISFIFO(inode.i_mode)
0684a4f3 1328 && !e2fsck_pass1_check_device_inode(fs, &inode))
1dde43f0 1329 problem = PR_2_BAD_FIFO;
fdbdea09 1330 else if (LINUX_S_ISSOCK(inode.i_mode)
0684a4f3 1331 && !e2fsck_pass1_check_device_inode(fs, &inode))
1dde43f0 1332 problem = PR_2_BAD_SOCKET;
fdbdea09 1333 else if (LINUX_S_ISLNK(inode.i_mode)
7cadc577 1334 && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) {
bcf9c5d4 1335 problem = PR_2_INVALID_SYMLINK;
67052a8a 1336 }
1dde43f0 1337
08b21301
TT
1338 if (problem) {
1339 if (fix_problem(ctx, problem, &pctx)) {
1b6bf175 1340 deallocate_inode(ctx, ino, 0);
a02ce9df 1341 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
08b21301 1342 return 0;
7cf73dcd 1343 return 1;
6c313fd4
TT
1344 } else
1345 not_fixed++;
08b21301 1346 problem = 0;
7cf73dcd 1347 }
efc6f628 1348
6c313fd4
TT
1349 if (inode.i_faddr) {
1350 if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1351 inode.i_faddr = 0;
1352 inode_modified++;
1353 } else
1354 not_fixed++;
3839e657 1355 }
1e3472c5
TT
1356
1357 switch (fs->super->s_creator_os) {
1e3472c5
TT
1358 case EXT2_OS_HURD:
1359 frag = &inode.osd2.hurd2.h_i_frag;
1360 fsize = &inode.osd2.hurd2.h_i_fsize;
1361 break;
1e3472c5
TT
1362 default:
1363 frag = fsize = 0;
1364 }
21c84b71
TT
1365 if (frag && *frag) {
1366 pctx.num = *frag;
1b6bf175 1367 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
21c84b71
TT
1368 *frag = 0;
1369 inode_modified++;
7e0282c5
TT
1370 } else
1371 not_fixed++;
21c84b71
TT
1372 pctx.num = 0;
1373 }
1374 if (fsize && *fsize) {
1375 pctx.num = *fsize;
1b6bf175 1376 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
21c84b71
TT
1377 *fsize = 0;
1378 inode_modified++;
6c313fd4
TT
1379 } else
1380 not_fixed++;
21c84b71
TT
1381 pctx.num = 0;
1382 }
1383
5d17119d 1384 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
efc6f628 1385 !(fs->super->s_feature_ro_compat &
5d17119d
TT
1386 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1387 (inode.osd2.linux2.l_i_blocks_hi != 0)) {
1388 pctx.num = inode.osd2.linux2.l_i_blocks_hi;
1389 if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) {
1390 inode.osd2.linux2.l_i_blocks_hi = 0;
1391 inode_modified++;
1392 }
1393 }
1394
911ec626
TT
1395 if (!(fs->super->s_feature_incompat &
1396 EXT4_FEATURE_INCOMPAT_64BIT) &&
1397 inode.osd2.linux2.l_i_file_acl_high != 0) {
1398 pctx.num = inode.osd2.linux2.l_i_file_acl_high;
1399 if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) {
1400 inode.osd2.linux2.l_i_file_acl_high = 0;
1401 inode_modified++;
1402 } else
1403 not_fixed++;
1404 }
1405
0c80c44b
TT
1406 if (ext2fs_file_acl_block(fs, &inode) &&
1407 ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) ||
1408 (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) {
6c313fd4 1409 if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
0c80c44b 1410 ext2fs_file_acl_block_set(fs, &inode, 0);
6c313fd4
TT
1411 inode_modified++;
1412 } else
1413 not_fixed++;
342d847d 1414 }
21c84b71 1415 if (inode.i_dir_acl &&
6c313fd4
TT
1416 LINUX_S_ISDIR(inode.i_mode)) {
1417 if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1418 inode.i_dir_acl = 0;
1419 inode_modified++;
1420 } else
1421 not_fixed++;
21c84b71 1422 }
6c313fd4 1423
f3db3566 1424 if (inode_modified)
08b21301 1425 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
f76344fb 1426 if (!not_fixed && ctx->inode_bad_map)
c5d2f50d 1427 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
3839e657
TT
1428 return 0;
1429}
1430
50e1e10f
TT
1431
1432/*
1433 * allocate_dir_block --- this function allocates a new directory
1434 * block for a particular inode; this is done if a directory has
1435 * a "hole" in it, or if a directory has a illegal block number
1436 * that was zeroed out and now needs to be replaced.
1437 */
1b6bf175 1438static int allocate_dir_block(e2fsck_t ctx,
6dc64392 1439 struct ext2_db_entry2 *db,
efc6f628 1440 char *buf EXT2FS_ATTR((unused)),
54434927 1441 struct problem_context *pctx)
50e1e10f 1442{
1b6bf175 1443 ext2_filsys fs = ctx->fs;
c5d2f50d 1444 blk64_t blk;
50e1e10f
TT
1445 char *block;
1446 struct ext2_inode inode;
50e1e10f 1447
1b6bf175 1448 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
50e1e10f
TT
1449 return 1;
1450
1451 /*
1452 * Read the inode and block bitmaps in; we'll be messing with
1453 * them.
1454 */
f8188fff 1455 e2fsck_read_bitmaps(ctx);
efc6f628 1456
50e1e10f
TT
1457 /*
1458 * First, find a free block
1459 */
c5d2f50d 1460 pctx->errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1b6bf175
TT
1461 if (pctx->errcode) {
1462 pctx->str = "ext2fs_new_block";
1463 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
50e1e10f
TT
1464 return 1;
1465 }
c5d2f50d
VAH
1466 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1467 ext2fs_mark_block_bitmap2(fs->block_map, blk);
50e1e10f
TT
1468 ext2fs_mark_bb_dirty(fs);
1469
1470 /*
1471 * Now let's create the actual data block for the inode
1472 */
1473 if (db->blockcnt)
1b6bf175 1474 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
50e1e10f 1475 else
1b6bf175
TT
1476 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1477 EXT2_ROOT_INO, &block);
50e1e10f 1478
1b6bf175
TT
1479 if (pctx->errcode) {
1480 pctx->str = "ext2fs_new_dir_block";
1481 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
50e1e10f
TT
1482 return 1;
1483 }
1484
81683c6a 1485 pctx->errcode = ext2fs_write_dir_block4(fs, blk, block, 0, db->ino);
c4e3d3f3 1486 ext2fs_free_mem(&block);
1b6bf175
TT
1487 if (pctx->errcode) {
1488 pctx->str = "ext2fs_write_dir_block";
1489 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
50e1e10f
TT
1490 return 1;
1491 }
1492
1493 /*
1494 * Update the inode block count
1495 */
08b21301 1496 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1ca1059f 1497 ext2fs_iblk_add_blocks(fs, &inode, 1);
50e1e10f
TT
1498 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
1499 inode.i_size = (db->blockcnt+1) * fs->blocksize;
08b21301 1500 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
50e1e10f
TT
1501
1502 /*
1503 * Finally, update the block pointers for the inode
1504 */
1505 db->blk = blk;
2d07b3ad
TT
1506 pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET,
1507 db->blockcnt, 0, &blk);
1b6bf175
TT
1508 if (pctx->errcode) {
1509 pctx->str = "ext2fs_block_iterate";
1510 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
50e1e10f
TT
1511 return 1;
1512 }
1513
1514 return 0;
1515}