]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/pass2.c
libext2fs: fix free block accounting for 64-bit file systems
[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;
3c7c6d73
TT
354 int status = 0;
355 int created = 0;
356 problem_t 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{
3c7c6d73 413 problem_t problem = 0;
cf5301d7 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,
530 char *block_buf)
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;
8fdc9985
TT
541
542 if (db->blockcnt == 0) {
543 root = (struct ext2_dx_root_info *) (block_buf + 24);
efc6f628 544
8fdc9985
TT
545#ifdef DX_DEBUG
546 printf("Root node dump:\n");
8deb80a5 547 printf("\t Reserved zero: %u\n", root->reserved_zero);
8fdc9985
TT
548 printf("\t Hash Version: %d\n", root->hash_version);
549 printf("\t Info length: %d\n", root->info_length);
550 printf("\t Indirect levels: %d\n", root->indirect_levels);
551 printf("\t Flags: %d\n", root->unused_flags);
552#endif
553
554 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
555 } else {
556 ent = (struct ext2_dx_entry *) (block_buf+8);
557 }
558 limit = (struct ext2_dx_countlimit *) ent;
559
560#ifdef DX_DEBUG
efc6f628 561 printf("Number of entries (count): %d\n",
8132d840 562 ext2fs_le16_to_cpu(limit->count));
efc6f628 563 printf("Number of entries (limit): %d\n",
8132d840 564 ext2fs_le16_to_cpu(limit->limit));
8fdc9985
TT
565#endif
566
8132d840 567 count = ext2fs_le16_to_cpu(limit->count);
ad4fa466
TT
568 expect_limit = (fs->blocksize - ((char *) ent - block_buf)) /
569 sizeof(struct ext2_dx_entry);
8132d840
TT
570 if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
571 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
ad4fa466
TT
572 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
573 goto clear_and_exit;
574 }
8132d840
TT
575 if (count > expect_limit) {
576 cd->pctx.num = count;
ad4fa466
TT
577 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
578 goto clear_and_exit;
579 count = expect_limit;
580 }
efc6f628 581
ad4fa466
TT
582 for (i=0; i < count; i++) {
583 prev_hash = hash;
8132d840 584 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
8fdc9985 585#ifdef DX_DEBUG
8deb80a5 586 printf("Entry #%d: Hash 0x%08x, block %u\n", i,
8132d840 587 hash, ext2fs_le32_to_cpu(ent[i].block));
8fdc9985 588#endif
8132d840 589 blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff;
8fdc9985 590 /* Check to make sure the block is valid */
977ac873 591 if (blk >= (blk_t) dx_dir->numblocks) {
b7a00563 592 cd->pctx.blk = blk;
8fdc9985 593 if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
ad4fa466
TT
594 &cd->pctx))
595 goto clear_and_exit;
977ac873 596 continue;
8fdc9985 597 }
ad4fa466
TT
598 if (hash < prev_hash &&
599 fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
600 goto clear_and_exit;
8fdc9985
TT
601 dx_db = &dx_dir->dx_block[blk];
602 if (dx_db->flags & DX_FLAG_REFERENCED) {
603 dx_db->flags |= DX_FLAG_DUP_REF;
604 } else {
605 dx_db->flags |= DX_FLAG_REFERENCED;
606 dx_db->parent = db->blockcnt;
607 }
608 if (hash < min_hash)
609 min_hash = hash;
610 if (hash > max_hash)
611 max_hash = hash;
612 dx_db->node_min_hash = hash;
8132d840 613 if ((i+1) < count)
efc6f628 614 dx_db->node_max_hash =
8132d840 615 ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
8fdc9985
TT
616 else {
617 dx_db->node_max_hash = 0xfffffffe;
618 dx_db->flags |= DX_FLAG_LAST;
619 }
620 if (i == 0)
621 dx_db->flags |= DX_FLAG_FIRST;
622 }
623#ifdef DX_DEBUG
624 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
625 db->blockcnt, min_hash, max_hash);
626#endif
627 dx_db = &dx_dir->dx_block[db->blockcnt];
628 dx_db->min_hash = min_hash;
629 dx_db->max_hash = max_hash;
ad4fa466
TT
630 return;
631
632clear_and_exit:
633 clear_htree(cd->ctx, cd->pctx.ino);
634 dx_dir->numblocks = 0;
8fdc9985
TT
635}
636#endif /* ENABLE_HTREE */
aa4115a4 637
e70ae99e
TT
638/*
639 * Given a busted directory, try to salvage it somehow.
efc6f628 640 *
e70ae99e 641 */
ad4fa466 642static void salvage_directory(ext2_filsys fs,
e70ae99e
TT
643 struct ext2_dir_entry *dirent,
644 struct ext2_dir_entry *prev,
54434927 645 unsigned int *offset)
e70ae99e
TT
646{
647 char *cp = (char *) dirent;
8a480350
TT
648 int left;
649 unsigned int rec_len, prev_rec_len;
642935c0 650 unsigned int name_len = dirent->name_len & 0xFF;
e70ae99e 651
8a480350 652 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
5dd77dbe
TT
653 left = fs->blocksize - *offset - rec_len;
654
e70ae99e
TT
655 /*
656 * Special case of directory entry of size 8: copy what's left
657 * of the directory block up to cover up the invalid hole.
658 */
5dd77dbe 659 if ((left >= 12) && (rec_len == 8)) {
e70ae99e
TT
660 memmove(cp, cp+8, left);
661 memset(cp + left, 0, 8);
ad4fa466
TT
662 return;
663 }
664 /*
665 * If the directory entry overruns the end of the directory
666 * block, and the name is small enough to fit, then adjust the
667 * record length.
668 */
669 if ((left < 0) &&
8a480350 670 ((int) rec_len + left > 8) &&
68477355 671 ((int) name_len + 8 <= (int) rec_len + left) &&
ad4fa466
TT
672 dirent->inode <= fs->super->s_inodes_count &&
673 strnlen(dirent->name, name_len) == name_len) {
8a480350 674 (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent);
ad4fa466 675 return;
e70ae99e
TT
676 }
677 /*
575307cc
KS
678 * If the record length of the directory entry is a multiple
679 * of four, and not too big, such that it is valid, let the
680 * previous directory entry absorb the invalid one.
e70ae99e 681 */
5dd77dbe
TT
682 if (prev && rec_len && (rec_len % 4) == 0 &&
683 (*offset + rec_len <= fs->blocksize)) {
8a480350
TT
684 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
685 prev_rec_len += rec_len;
686 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
5dd77dbe 687 *offset += rec_len;
ad4fa466 688 return;
e70ae99e
TT
689 }
690 /*
691 * Default salvage method --- kill all of the directory
692 * entries for the rest of the block. We will either try to
693 * absorb it into the previous directory entry, or create a
694 * new empty directory entry the rest of the directory block.
695 */
696 if (prev) {
8a480350
TT
697 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
698 prev_rec_len += fs->blocksize - *offset;
699 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
ad4fa466 700 *offset = fs->blocksize;
e70ae99e 701 } else {
8a480350
TT
702 rec_len = fs->blocksize - *offset;
703 (void) ext2fs_set_rec_len(fs, rec_len, dirent);
e70ae99e
TT
704 dirent->name_len = 0;
705 dirent->inode = 0;
e70ae99e 706 }
e70ae99e
TT
707}
708
3839e657 709static int check_dir_block(ext2_filsys fs,
6dc64392 710 struct ext2_db_entry2 *db,
54dc7ca2 711 void *priv_data)
3839e657 712{
8fdc9985
TT
713 struct dx_dir_info *dx_dir;
714#ifdef ENABLE_HTREE
715 struct dx_dirblock_info *dx_db = 0;
716#endif /* ENABLE_HTREE */
e70ae99e 717 struct ext2_dir_entry *dirent, *prev;
8fdc9985 718 ext2_dirhash_t hash;
54434927 719 unsigned int offset = 0;
3839e657 720 int dir_modified = 0;
21c84b71 721 int dot_state;
03fa6f8a 722 unsigned int rec_len;
6dc64392 723 blk64_t block_nr = db->blk;
86c627ec 724 ext2_ino_t ino = db->ino;
28db82a8 725 ext2_ino_t subdir_parent;
21c84b71 726 __u16 links;
54dc7ca2 727 struct check_dir_struct *cd;
1b6bf175
TT
728 char *buf;
729 e2fsck_t ctx;
3c7c6d73 730 problem_t problem;
ea1959f0 731 struct ext2_dx_root_info *root;
e8254bfd 732 struct ext2_dx_countlimit *limit;
0926668d
TT
733 static dict_t de_dict;
734 struct problem_context pctx;
735 int dups_found = 0;
28db82a8 736 int ret;
1b6bf175 737
54dc7ca2 738 cd = (struct check_dir_struct *) priv_data;
1b6bf175
TT
739 buf = cd->buf;
740 ctx = cd->ctx;
f8188fff 741
49a7360b 742 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
4cae0452 743 return DIRENT_ABORT;
efc6f628 744
4cae0452
TT
745 if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
746 return DIRENT_ABORT;
efc6f628 747
3839e657 748 /*
efc6f628 749 * Make sure the inode is still in use (could have been
3839e657
TT
750 * deleted in the duplicate/bad blocks pass.
751 */
c5d2f50d 752 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino)))
3839e657 753 return 0;
50e1e10f 754
21c84b71
TT
755 cd->pctx.ino = ino;
756 cd->pctx.blk = block_nr;
757 cd->pctx.blkcount = db->blockcnt;
758 cd->pctx.ino2 = 0;
759 cd->pctx.dirent = 0;
760 cd->pctx.num = 0;
761
50e1e10f 762 if (db->blk == 0) {
1b6bf175 763 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
50e1e10f
TT
764 return 0;
765 block_nr = db->blk;
766 }
efc6f628 767
3839e657
TT
768 if (db->blockcnt)
769 dot_state = 2;
770 else
771 dot_state = 0;
772
0926668d
TT
773 if (ctx->dirs_to_hash &&
774 ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
775 dups_found++;
776
3839e657 777#if 0
f3db3566 778 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
3839e657
TT
779 db->blockcnt, ino);
780#endif
efc6f628 781
f85a9ae6 782 ehandler_operation(_("reading directory block"));
6dc64392 783 cd->pctx.errcode = ext2fs_read_dir_block3(fs, block_nr, buf, 0);
e94bc631 784 ehandler_operation(0);
b9852cd8
TT
785 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
786 cd->pctx.errcode = 0; /* We'll handle this ourselves */
1b6bf175 787 if (cd->pctx.errcode) {
08b21301
TT
788 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
789 ctx->flags |= E2F_FLAG_ABORT;
790 return DIRENT_ABORT;
791 }
1b6bf175 792 memset(buf, 0, fs->blocksize);
3839e657 793 }
8fdc9985
TT
794#ifdef ENABLE_HTREE
795 dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
62acaa1d 796 if (dx_dir && dx_dir->numblocks) {
8fdc9985 797 if (db->blockcnt >= dx_dir->numblocks) {
efc6f628 798 if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK,
d45edec0
TT
799 &pctx)) {
800 clear_htree(ctx, ino);
801 dx_dir->numblocks = 0;
802 dx_db = 0;
803 goto out_htree;
804 }
805 fatal_error(ctx, _("Can not continue."));
8fdc9985
TT
806 }
807 dx_db = &dx_dir->dx_block[db->blockcnt];
808 dx_db->type = DX_DIRBLOCK_LEAF;
809 dx_db->phys = block_nr;
810 dx_db->min_hash = ~0;
811 dx_db->max_hash = 0;
efc6f628 812
8fdc9985 813 dirent = (struct ext2_dir_entry *) buf;
8a480350 814 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
e8254bfd 815 limit = (struct ext2_dx_countlimit *) (buf+8);
8fdc9985 816 if (db->blockcnt == 0) {
ea1959f0 817 root = (struct ext2_dx_root_info *) (buf + 24);
8fdc9985
TT
818 dx_db->type = DX_DIRBLOCK_ROOT;
819 dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
ea1959f0
TT
820 if ((root->reserved_zero ||
821 root->info_length < 8 ||
822 root->indirect_levels > 1) &&
823 fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
824 clear_htree(ctx, ino);
825 dx_dir->numblocks = 0;
826 dx_db = 0;
f77704e4 827 }
ea1959f0 828 dx_dir->hashversion = root->hash_version;
f77704e4
TT
829 if ((dx_dir->hashversion <= EXT2_HASH_TEA) &&
830 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
831 dx_dir->hashversion += 3;
ad4fa466 832 dx_dir->depth = root->indirect_levels + 1;
8fdc9985 833 } else if ((dirent->inode == 0) &&
5dd77dbe 834 (rec_len == fs->blocksize) &&
e8254bfd 835 (dirent->name_len == 0) &&
efc6f628
TT
836 (ext2fs_le16_to_cpu(limit->limit) ==
837 ((fs->blocksize-8) /
8132d840 838 sizeof(struct ext2_dx_entry))))
8fdc9985
TT
839 dx_db->type = DX_DIRBLOCK_NODE;
840 }
d45edec0 841out_htree:
8fdc9985 842#endif /* ENABLE_HTREE */
3839e657 843
0926668d 844 dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
e70ae99e 845 prev = 0;
3839e657 846 do {
3971bfe8 847 dgrp_t group;
49a7360b
JS
848 ext2_ino_t first_unused_inode;
849
1b6bf175 850 problem = 0;
3839e657 851 dirent = (struct ext2_dir_entry *) (buf + offset);
8a480350 852 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
21c84b71
TT
853 cd->pctx.dirent = dirent;
854 cd->pctx.num = offset;
5dd77dbe
TT
855 if (((offset + rec_len) > fs->blocksize) ||
856 (rec_len < 12) ||
857 ((rec_len % 4) != 0) ||
03fa6f8a 858 (((dirent->name_len & (unsigned) 0xFF)+8) > rec_len)) {
1b6bf175 859 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
ad4fa466 860 salvage_directory(fs, dirent, prev, &offset);
3839e657 861 dir_modified++;
e70ae99e 862 continue;
21c84b71 863 } else
0926668d 864 goto abort_free_dict;
3839e657 865 }
50e1e10f 866
e70ae99e 867 if (dot_state == 0) {
1b6bf175 868 if (check_dot(ctx, dirent, ino, &cd->pctx))
3839e657 869 dir_modified++;
e70ae99e 870 } else if (dot_state == 1) {
28db82a8
TT
871 ret = check_dotdot(ctx, dirent, ino, &cd->pctx);
872 if (ret < 0)
0926668d 873 goto abort_free_dict;
28db82a8 874 if (ret)
3839e657
TT
875 dir_modified++;
876 } else if (dirent->inode == ino) {
1b6bf175
TT
877 problem = PR_2_LINK_DOT;
878 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
3839e657
TT
879 dirent->inode = 0;
880 dir_modified++;
21c84b71 881 goto next;
3839e657
TT
882 }
883 }
efc6f628 884 if (!dirent->inode)
3839e657 885 goto next;
efc6f628 886
3839e657
TT
887 /*
888 * Make sure the inode listed is a legal one.
efc6f628 889 */
3839e657 890 if (((dirent->inode != EXT2_ROOT_INO) &&
7f88b043 891 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
3839e657 892 (dirent->inode > fs->super->s_inodes_count)) {
1b6bf175 893 problem = PR_2_BAD_INO;
1b6bf175 894 } else if (ctx->inode_bb_map &&
c5d2f50d 895 (ext2fs_test_inode_bitmap2(ctx->inode_bb_map,
1b6bf175
TT
896 dirent->inode))) {
897 /*
898 * If the inode is in a bad block, offer to
899 * clear it.
900 */
901 problem = PR_2_BB_INODE;
e70ae99e 902 } else if ((dot_state > 1) &&
b6f79831 903 ((dirent->name_len & 0xFF) == 1) &&
1b6bf175
TT
904 (dirent->name[0] == '.')) {
905 /*
906 * If there's a '.' entry in anything other
907 * than the first directory entry, it's a
908 * duplicate entry that should be removed.
909 */
910 problem = PR_2_DUP_DOT;
e70ae99e 911 } else if ((dot_state > 1) &&
b6f79831 912 ((dirent->name_len & 0xFF) == 2) &&
efc6f628 913 (dirent->name[0] == '.') &&
1b6bf175
TT
914 (dirent->name[1] == '.')) {
915 /*
916 * If there's a '..' entry in anything other
917 * than the second directory entry, it's a
918 * duplicate entry that should be removed.
919 */
920 problem = PR_2_DUP_DOT_DOT;
e70ae99e 921 } else if ((dot_state > 1) &&
1b6bf175
TT
922 (dirent->inode == EXT2_ROOT_INO)) {
923 /*
924 * Don't allow links to the root directory.
925 * We check this specially to make sure we
926 * catch this error case even if the root
927 * directory hasn't been created yet.
928 */
929 problem = PR_2_LINK_ROOT;
e70ae99e 930 } else if ((dot_state > 1) &&
c40db6d5
TT
931 (dirent->name_len & 0xFF) == 0) {
932 /*
933 * Don't allow zero-length directory names.
934 */
935 problem = PR_2_NULL_NAME;
21c84b71
TT
936 }
937
1b6bf175
TT
938 if (problem) {
939 if (fix_problem(ctx, problem, &cd->pctx)) {
21c84b71
TT
940 dirent->inode = 0;
941 dir_modified++;
942 goto next;
1b6bf175
TT
943 } else {
944 ext2fs_unmark_valid(fs);
945 if (problem == PR_2_BAD_INO)
946 goto next;
21c84b71 947 }
3839e657
TT
948 }
949
950 /*
951 * If the inode was marked as having bad fields in
952 * pass1, process it and offer to fix/clear it.
953 * (We wait until now so that we can display the
954 * pathname to the user.)
955 */
1b6bf175 956 if (ctx->inode_bad_map &&
c5d2f50d 957 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
3839e657 958 dirent->inode)) {
e72a9ba3 959 if (e2fsck_process_bad_inode(ctx, ino,
bcf9c5d4
TT
960 dirent->inode,
961 buf + fs->blocksize)) {
3839e657
TT
962 dirent->inode = 0;
963 dir_modified++;
964 goto next;
965 }
a02ce9df 966 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
08b21301 967 return DIRENT_ABORT;
3839e657
TT
968 }
969
49a7360b
JS
970 group = ext2fs_group_of_ino(fs, dirent->inode);
971 first_unused_inode = group * fs->super->s_inodes_per_group +
972 1 + fs->super->s_inodes_per_group -
d7cca6b0 973 ext2fs_bg_itable_unused(fs, group);
49a7360b
JS
974 cd->pctx.group = group;
975
976 /*
42e89ce7
TT
977 * Check if the inode was missed out because
978 * _INODE_UNINIT flag was set or bg_itable_unused was
979 * incorrect. If so, clear the _INODE_UNINIT flag and
980 * restart e2fsck. In the future it would be nice if
981 * we could call a function in pass1.c that checks the
982 * newly visible inodes.
49a7360b 983 */
cd65a24e 984 if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) {
6267ee49 985 pctx.num = dirent->inode;
49a7360b
JS
986 if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT,
987 &cd->pctx)){
e633b58a 988 ext2fs_bg_flags_clear(fs, group,
732c8cd5 989 EXT2_BG_INODE_UNINIT);
42e89ce7 990 ext2fs_mark_super_dirty(fs);
6267ee49 991 ctx->flags |= E2F_FLAG_RESTART_LATER;
49a7360b
JS
992 } else {
993 ext2fs_unmark_valid(fs);
994 if (problem == PR_2_BAD_INO)
995 goto next;
996 }
997 } else if (dirent->inode >= first_unused_inode) {
6267ee49 998 pctx.num = dirent->inode;
49a7360b 999 if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){
d7cca6b0 1000 ext2fs_bg_itable_unused_set(fs, group, 0);
49a7360b 1001 ext2fs_mark_super_dirty(fs);
6267ee49 1002 ctx->flags |= E2F_FLAG_RESTART_LATER;
49a7360b
JS
1003 } else {
1004 ext2fs_unmark_valid(fs);
1005 if (problem == PR_2_BAD_INO)
1006 goto next;
1007 }
1008 }
1009
0433c1f1
TT
1010 /*
1011 * Offer to clear unused inodes; if we are going to be
1012 * restarting the scan due to bg_itable_unused being
1013 * wrong, then don't clear any inodes to avoid zapping
1014 * inodes that were skipped during pass1 due to an
1015 * incorrect bg_itable_unused; we'll get any real
1016 * problems after we restart.
1017 */
1018 if (!(ctx->flags & E2F_FLAG_RESTART_LATER) &&
97d26ce9
TT
1019 !(ext2fs_test_inode_bitmap2(ctx->inode_used_map,
1020 dirent->inode)))
49a7360b 1021 problem = PR_2_UNUSED_INODE;
49a7360b
JS
1022
1023 if (problem) {
1024 if (fix_problem(ctx, problem, &cd->pctx)) {
1025 dirent->inode = 0;
1026 dir_modified++;
1027 goto next;
1028 } else {
1029 ext2fs_unmark_valid(fs);
1030 if (problem == PR_2_BAD_INO)
1031 goto next;
1032 }
1033 }
1034
1b6bf175
TT
1035 if (check_name(ctx, dirent, ino, &cd->pctx))
1036 dir_modified++;
1037
aa4115a4
TT
1038 if (check_filetype(ctx, dirent, ino, &cd->pctx))
1039 dir_modified++;
1040
8fdc9985
TT
1041#ifdef ENABLE_HTREE
1042 if (dx_db) {
1043 ext2fs_dirhash(dx_dir->hashversion, dirent->name,
503f9e7f
TT
1044 (dirent->name_len & 0xFF),
1045 fs->super->s_hash_seed, &hash, 0);
8fdc9985
TT
1046 if (hash < dx_db->min_hash)
1047 dx_db->min_hash = hash;
1048 if (hash > dx_db->max_hash)
1049 dx_db->max_hash = hash;
1050 }
1051#endif
1052
3839e657
TT
1053 /*
1054 * If this is a directory, then mark its parent in its
1055 * dir_info structure. If the parent field is already
1056 * filled in, then this directory has more than one
1057 * hard link. We assume the first link is correct,
1058 * and ask the user if he/she wants to clear this one.
1059 */
e70ae99e 1060 if ((dot_state > 1) &&
c5d2f50d 1061 (ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
3839e657 1062 dirent->inode))) {
28db82a8
TT
1063 if (e2fsck_dir_info_get_parent(ctx, dirent->inode,
1064 &subdir_parent)) {
1b6bf175
TT
1065 cd->pctx.ino = dirent->inode;
1066 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
0926668d 1067 goto abort_free_dict;
3839e657 1068 }
28db82a8
TT
1069 if (subdir_parent) {
1070 cd->pctx.ino2 = subdir_parent;
1b6bf175 1071 if (fix_problem(ctx, PR_2_LINK_DIR,
21c84b71 1072 &cd->pctx)) {
3839e657
TT
1073 dirent->inode = 0;
1074 dir_modified++;
1075 goto next;
21c84b71
TT
1076 }
1077 cd->pctx.ino2 = 0;
28db82a8 1078 } else {
efc6f628 1079 (void) e2fsck_dir_info_set_parent(ctx,
28db82a8
TT
1080 dirent->inode, ino);
1081 }
3839e657 1082 }
0926668d
TT
1083
1084 if (dups_found) {
1085 ;
1086 } else if (dict_lookup(&de_dict, dirent)) {
1087 clear_problem_context(&pctx);
1088 pctx.ino = ino;
1089 pctx.dirent = dirent;
1090 fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
1091 if (!ctx->dirs_to_hash)
1092 ext2fs_u32_list_create(&ctx->dirs_to_hash, 50);
1093 if (ctx->dirs_to_hash)
1094 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1095 dups_found++;
1096 } else
1097 dict_alloc_insert(&de_dict, dirent, dirent);
efc6f628 1098
1b6bf175
TT
1099 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1100 &links);
21c84b71 1101 if (links > 1)
1b6bf175
TT
1102 ctx->fs_links_count++;
1103 ctx->fs_total_count++;
3839e657 1104 next:
e70ae99e 1105 prev = dirent;
5dd77dbe 1106 if (dir_modified)
8a480350 1107 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
5dd77dbe 1108 offset += rec_len;
e70ae99e 1109 dot_state++;
3839e657
TT
1110 } while (offset < fs->blocksize);
1111#if 0
1112 printf("\n");
1113#endif
8fdc9985
TT
1114#ifdef ENABLE_HTREE
1115 if (dx_db) {
1116#ifdef DX_DEBUG
1117 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1118 db->blockcnt, dx_db->type,
1119 dx_db->min_hash, dx_db->max_hash);
1120#endif
b7a00563 1121 cd->pctx.dir = cd->pctx.ino;
8fdc9985
TT
1122 if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1123 (dx_db->type == DX_DIRBLOCK_NODE))
1124 parse_int_node(fs, db, cd, dx_dir, buf);
1125 }
1126#endif /* ENABLE_HTREE */
3839e657 1127 if (offset != fs->blocksize) {
5dd77dbe 1128 cd->pctx.num = rec_len - fs->blocksize + offset;
1b6bf175
TT
1129 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1130 dirent->rec_len = cd->pctx.num;
1131 dir_modified++;
1132 }
3839e657
TT
1133 }
1134 if (dir_modified) {
7bef6d52 1135 cd->pctx.errcode = ext2fs_write_dir_block3(fs, block_nr, buf, 0);
1b6bf175 1136 if (cd->pctx.errcode) {
08b21301 1137 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
0926668d
TT
1138 &cd->pctx))
1139 goto abort_free_dict;
3839e657
TT
1140 }
1141 ext2fs_mark_changed(fs);
1142 }
0926668d 1143 dict_free_nodes(&de_dict);
3839e657 1144 return 0;
0926668d 1145abort_free_dict:
0926668d 1146 ctx->flags |= E2F_FLAG_ABORT;
49a7360b 1147 dict_free_nodes(&de_dict);
0926668d 1148 return DIRENT_ABORT;
3839e657
TT
1149}
1150
624e4a64
AK
1151struct del_block {
1152 e2fsck_t ctx;
1153 e2_blkcnt_t num;
1154};
1155
3839e657
TT
1156/*
1157 * This function is called to deallocate a block, and is an interator
1158 * functioned called by deallocate inode via ext2fs_iterate_block().
1159 */
1160static int deallocate_inode_block(ext2_filsys fs,
6dc64392 1161 blk64_t *block_nr,
54434927 1162 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
6dc64392 1163 blk64_t ref_block EXT2FS_ATTR((unused)),
54434927 1164 int ref_offset EXT2FS_ATTR((unused)),
133a56dc 1165 void *priv_data)
3839e657 1166{
624e4a64 1167 struct del_block *p = priv_data;
efc6f628 1168
1917875f 1169 if (HOLE_BLKADDR(*block_nr))
3839e657 1170 return 0;
1ba7a2f2 1171 if ((*block_nr < fs->super->s_first_data_block) ||
4efbac6f 1172 (*block_nr >= ext2fs_blocks_count(fs->super)))
1ba7a2f2 1173 return 0;
48f23054 1174 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
624e4a64 1175 p->num++;
3839e657
TT
1176 return 0;
1177}
efc6f628 1178
3839e657
TT
1179/*
1180 * This fuction deallocates an inode
1181 */
4035f40b 1182static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
3839e657 1183{
1b6bf175 1184 ext2_filsys fs = ctx->fs;
3839e657 1185 struct ext2_inode inode;
1b6bf175 1186 struct problem_context pctx;
0684a4f3 1187 __u32 count;
624e4a64 1188 struct del_block del_block;
efc6f628 1189
08b21301 1190 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1b6bf175
TT
1191 clear_problem_context(&pctx);
1192 pctx.ino = ino;
f3db3566 1193
3839e657
TT
1194 /*
1195 * Fix up the bitmaps...
1196 */
f8188fff 1197 e2fsck_read_bitmaps(ctx);
0684a4f3
TT
1198 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1199
0c80c44b 1200 if (ext2fs_file_acl_block(fs, &inode) &&
0684a4f3 1201 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
0c80c44b
TT
1202 pctx.errcode = ext2fs_adjust_ea_refcount2(fs,
1203 ext2fs_file_acl_block(fs, &inode),
1204 block_buf, -1, &count);
0684a4f3
TT
1205 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1206 pctx.errcode = 0;
1207 count = 1;
1208 }
1209 if (pctx.errcode) {
0c80c44b 1210 pctx.blk = ext2fs_file_acl_block(fs, &inode);
0684a4f3
TT
1211 fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1212 ctx->flags |= E2F_FLAG_ABORT;
1213 return;
1214 }
1215 if (count == 0) {
48f23054 1216 ext2fs_block_alloc_stats2(fs,
0c80c44b 1217 ext2fs_file_acl_block(fs, &inode), -1);
0684a4f3 1218 }
0c80c44b 1219 ext2fs_file_acl_block_set(fs, &inode, 0);
0684a4f3 1220 }
3839e657 1221
0c80c44b 1222 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
c8ec2bad 1223 goto clear_inode;
a4742691 1224
3b6c0938
DW
1225 if (LINUX_S_ISREG(inode.i_mode) &&
1226 ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode)))
a4742691
TT
1227 ctx->large_files--;
1228
624e4a64
AK
1229 del_block.ctx = ctx;
1230 del_block.num = 0;
6dc64392 1231 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
624e4a64
AK
1232 deallocate_inode_block,
1233 &del_block);
1b6bf175
TT
1234 if (pctx.errcode) {
1235 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
08b21301
TT
1236 ctx->flags |= E2F_FLAG_ABORT;
1237 return;
1b6bf175 1238 }
c8ec2bad
TT
1239clear_inode:
1240 /* Inode may have changed by block_iterate, so reread it */
1241 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1242 e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
3839e657
TT
1243}
1244
8fdc9985
TT
1245/*
1246 * This fuction clears the htree flag on an inode
1247 */
1248static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1249{
1250 struct ext2_inode inode;
efc6f628 1251
8fdc9985
TT
1252 e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1253 inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1254 e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
b7a00563
TT
1255 if (ctx->dirs_to_hash)
1256 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
8fdc9985
TT
1257}
1258
1259
f404167d
TT
1260int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1261 ext2_ino_t ino, char *buf)
3839e657 1262{
1b6bf175 1263 ext2_filsys fs = ctx->fs;
3839e657 1264 struct ext2_inode inode;
3839e657 1265 int inode_modified = 0;
6c313fd4 1266 int not_fixed = 0;
1e3472c5 1267 unsigned char *frag, *fsize;
21c84b71 1268 struct problem_context pctx;
3c7c6d73 1269 problem_t problem = 0;
3839e657 1270
08b21301 1271 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
21c84b71
TT
1272
1273 clear_problem_context(&pctx);
1274 pctx.ino = ino;
1275 pctx.dir = dir;
1276 pctx.inode = &inode;
1277
0c80c44b 1278 if (ext2fs_file_acl_block(fs, &inode) &&
f76344fb
TT
1279 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1280 if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
0c80c44b 1281 ext2fs_file_acl_block_set(fs, &inode, 0);
f76344fb
TT
1282 inode_modified++;
1283 } else
1284 not_fixed++;
1285 }
6c313fd4 1286
50e1e10f
TT
1287 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1288 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1289 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
08b21301
TT
1290 !(LINUX_S_ISSOCK(inode.i_mode)))
1291 problem = PR_2_BAD_MODE;
fdbdea09 1292 else if (LINUX_S_ISCHR(inode.i_mode)
0684a4f3 1293 && !e2fsck_pass1_check_device_inode(fs, &inode))
08b21301 1294 problem = PR_2_BAD_CHAR_DEV;
fdbdea09 1295 else if (LINUX_S_ISBLK(inode.i_mode)
0684a4f3 1296 && !e2fsck_pass1_check_device_inode(fs, &inode))
08b21301 1297 problem = PR_2_BAD_BLOCK_DEV;
fdbdea09 1298 else if (LINUX_S_ISFIFO(inode.i_mode)
0684a4f3 1299 && !e2fsck_pass1_check_device_inode(fs, &inode))
1dde43f0 1300 problem = PR_2_BAD_FIFO;
fdbdea09 1301 else if (LINUX_S_ISSOCK(inode.i_mode)
0684a4f3 1302 && !e2fsck_pass1_check_device_inode(fs, &inode))
1dde43f0 1303 problem = PR_2_BAD_SOCKET;
fdbdea09 1304 else if (LINUX_S_ISLNK(inode.i_mode)
7cadc577 1305 && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) {
bcf9c5d4 1306 problem = PR_2_INVALID_SYMLINK;
67052a8a 1307 }
1dde43f0 1308
08b21301
TT
1309 if (problem) {
1310 if (fix_problem(ctx, problem, &pctx)) {
1b6bf175 1311 deallocate_inode(ctx, ino, 0);
a02ce9df 1312 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
08b21301 1313 return 0;
7cf73dcd 1314 return 1;
6c313fd4
TT
1315 } else
1316 not_fixed++;
08b21301 1317 problem = 0;
7cf73dcd 1318 }
efc6f628 1319
6c313fd4
TT
1320 if (inode.i_faddr) {
1321 if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1322 inode.i_faddr = 0;
1323 inode_modified++;
1324 } else
1325 not_fixed++;
3839e657 1326 }
1e3472c5
TT
1327
1328 switch (fs->super->s_creator_os) {
1e3472c5
TT
1329 case EXT2_OS_HURD:
1330 frag = &inode.osd2.hurd2.h_i_frag;
1331 fsize = &inode.osd2.hurd2.h_i_fsize;
1332 break;
1e3472c5
TT
1333 default:
1334 frag = fsize = 0;
1335 }
21c84b71
TT
1336 if (frag && *frag) {
1337 pctx.num = *frag;
1b6bf175 1338 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
21c84b71
TT
1339 *frag = 0;
1340 inode_modified++;
7e0282c5
TT
1341 } else
1342 not_fixed++;
21c84b71
TT
1343 pctx.num = 0;
1344 }
1345 if (fsize && *fsize) {
1346 pctx.num = *fsize;
1b6bf175 1347 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
21c84b71
TT
1348 *fsize = 0;
1349 inode_modified++;
6c313fd4
TT
1350 } else
1351 not_fixed++;
21c84b71
TT
1352 pctx.num = 0;
1353 }
1354
5d17119d 1355 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
efc6f628 1356 !(fs->super->s_feature_ro_compat &
5d17119d
TT
1357 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1358 (inode.osd2.linux2.l_i_blocks_hi != 0)) {
1359 pctx.num = inode.osd2.linux2.l_i_blocks_hi;
1360 if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) {
1361 inode.osd2.linux2.l_i_blocks_hi = 0;
1362 inode_modified++;
1363 }
1364 }
1365
911ec626
TT
1366 if (!(fs->super->s_feature_incompat &
1367 EXT4_FEATURE_INCOMPAT_64BIT) &&
1368 inode.osd2.linux2.l_i_file_acl_high != 0) {
1369 pctx.num = inode.osd2.linux2.l_i_file_acl_high;
1370 if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) {
1371 inode.osd2.linux2.l_i_file_acl_high = 0;
1372 inode_modified++;
1373 } else
1374 not_fixed++;
1375 }
1376
0c80c44b
TT
1377 if (ext2fs_file_acl_block(fs, &inode) &&
1378 ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) ||
1379 (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) {
6c313fd4 1380 if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
0c80c44b 1381 ext2fs_file_acl_block_set(fs, &inode, 0);
6c313fd4
TT
1382 inode_modified++;
1383 } else
1384 not_fixed++;
342d847d 1385 }
21c84b71 1386 if (inode.i_dir_acl &&
6c313fd4
TT
1387 LINUX_S_ISDIR(inode.i_mode)) {
1388 if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1389 inode.i_dir_acl = 0;
1390 inode_modified++;
1391 } else
1392 not_fixed++;
21c84b71 1393 }
6c313fd4 1394
f3db3566 1395 if (inode_modified)
08b21301 1396 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
f76344fb 1397 if (!not_fixed && ctx->inode_bad_map)
c5d2f50d 1398 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
3839e657
TT
1399 return 0;
1400}
1401
50e1e10f
TT
1402
1403/*
1404 * allocate_dir_block --- this function allocates a new directory
1405 * block for a particular inode; this is done if a directory has
1406 * a "hole" in it, or if a directory has a illegal block number
1407 * that was zeroed out and now needs to be replaced.
1408 */
1b6bf175 1409static int allocate_dir_block(e2fsck_t ctx,
6dc64392 1410 struct ext2_db_entry2 *db,
efc6f628 1411 char *buf EXT2FS_ATTR((unused)),
54434927 1412 struct problem_context *pctx)
50e1e10f 1413{
1b6bf175 1414 ext2_filsys fs = ctx->fs;
c5d2f50d 1415 blk64_t blk;
50e1e10f
TT
1416 char *block;
1417 struct ext2_inode inode;
50e1e10f 1418
1b6bf175 1419 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
50e1e10f
TT
1420 return 1;
1421
1422 /*
1423 * Read the inode and block bitmaps in; we'll be messing with
1424 * them.
1425 */
f8188fff 1426 e2fsck_read_bitmaps(ctx);
efc6f628 1427
50e1e10f
TT
1428 /*
1429 * First, find a free block
1430 */
c5d2f50d 1431 pctx->errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1b6bf175
TT
1432 if (pctx->errcode) {
1433 pctx->str = "ext2fs_new_block";
1434 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
50e1e10f
TT
1435 return 1;
1436 }
c5d2f50d
VAH
1437 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1438 ext2fs_mark_block_bitmap2(fs->block_map, blk);
50e1e10f
TT
1439 ext2fs_mark_bb_dirty(fs);
1440
1441 /*
1442 * Now let's create the actual data block for the inode
1443 */
1444 if (db->blockcnt)
1b6bf175 1445 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
50e1e10f 1446 else
1b6bf175
TT
1447 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1448 EXT2_ROOT_INO, &block);
50e1e10f 1449
1b6bf175
TT
1450 if (pctx->errcode) {
1451 pctx->str = "ext2fs_new_dir_block";
1452 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
50e1e10f
TT
1453 return 1;
1454 }
1455
7bef6d52 1456 pctx->errcode = ext2fs_write_dir_block3(fs, blk, block, 0);
c4e3d3f3 1457 ext2fs_free_mem(&block);
1b6bf175
TT
1458 if (pctx->errcode) {
1459 pctx->str = "ext2fs_write_dir_block";
1460 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
50e1e10f
TT
1461 return 1;
1462 }
1463
1464 /*
1465 * Update the inode block count
1466 */
08b21301 1467 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1ca1059f 1468 ext2fs_iblk_add_blocks(fs, &inode, 1);
50e1e10f
TT
1469 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
1470 inode.i_size = (db->blockcnt+1) * fs->blocksize;
08b21301 1471 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
50e1e10f
TT
1472
1473 /*
1474 * Finally, update the block pointers for the inode
1475 */
1476 db->blk = blk;
2d07b3ad
TT
1477 pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET,
1478 db->blockcnt, 0, &blk);
1b6bf175
TT
1479 if (pctx->errcode) {
1480 pctx->str = "ext2fs_block_iterate";
1481 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
50e1e10f
TT
1482 return 1;
1483 }
1484
1485 return 0;
1486}