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