]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/pass2.c
Eliminate unused parameter warnings from Android build
[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"
3dca12fb 50#include "support/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);
a5abfe03
DW
64static int check_dir_block2(ext2_filsys fs,
65 struct ext2_db_entry2 *dir_blocks_info,
66 void *priv_data);
3839e657 67static int check_dir_block(ext2_filsys fs,
6dc64392 68 struct ext2_db_entry2 *dir_blocks_info,
54dc7ca2 69 void *priv_data);
1b6bf175 70static int allocate_dir_block(e2fsck_t ctx,
6dc64392 71 struct ext2_db_entry2 *dir_blocks_info,
21c84b71 72 char *buf, struct problem_context *pctx);
8fdc9985 73static void clear_htree(e2fsck_t ctx, ext2_ino_t ino);
ad4fa466
TT
74static int htree_depth(struct dx_dir_info *dx_dir,
75 struct dx_dirblock_info *dx_db);
ea1959f0 76static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b);
3839e657 77
21c84b71
TT
78struct check_dir_struct {
79 char *buf;
80 struct problem_context pctx;
f8188fff 81 int count, max;
1b6bf175 82 e2fsck_t ctx;
a5abfe03
DW
83 unsigned long long list_offset;
84 unsigned long long ra_entries;
85 unsigned long long next_ra_off;
efc6f628 86};
21c84b71 87
08b21301 88void e2fsck_pass2(e2fsck_t ctx)
3839e657 89{
a4742691
TT
90 struct ext2_super_block *sb = ctx->fs->super;
91 struct problem_context pctx;
92 ext2_filsys fs = ctx->fs;
93 char *buf;
8bf191e8 94#ifdef RESOURCE_TRACK
3839e657 95 struct resource_track rtrack;
8bf191e8 96#endif
21c84b71 97 struct check_dir_struct cd;
8fdc9985
TT
98 struct dx_dir_info *dx_dir;
99 struct dx_dirblock_info *dx_db, *dx_parent;
830b44f4 100 unsigned int save_type;
54434927 101 int b;
ad4fa466 102 int i, depth;
8fdc9985
TT
103 problem_t code;
104 int bad_dir;
a5abfe03
DW
105 int (*check_dir_func)(ext2_filsys fs,
106 struct ext2_db_entry2 *dir_blocks_info,
107 void *priv_data);
8fdc9985 108
6d96b00d 109 init_resource_track(&rtrack, ctx->fs->io);
1b6bf175
TT
110 clear_problem_context(&cd.pctx);
111
3839e657
TT
112#ifdef MTRACE
113 mtrace_print("Pass 2");
114#endif
115
1b6bf175
TT
116 if (!(ctx->options & E2F_OPT_PREEN))
117 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
118
efc6f628 119 e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT,
34b9f796
TT
120 &ctx->inode_count);
121 if (ctx->inode_count)
122 cd.pctx.errcode = 0;
830b44f4
TT
123 else {
124 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
125 "inode_count", &save_type);
efc6f628 126 cd.pctx.errcode = ext2fs_create_icount2(fs,
34b9f796 127 EXT2_ICOUNT_OPT_INCREMENT,
1b6bf175
TT
128 0, ctx->inode_link_info,
129 &ctx->inode_count);
830b44f4
TT
130 fs->default_bitmap_type = save_type;
131 }
1b6bf175
TT
132 if (cd.pctx.errcode) {
133 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
08b21301
TT
134 ctx->flags |= E2F_FLAG_ABORT;
135 return;
21c84b71 136 }
bcf9c5d4 137 buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
54dc7ca2 138 "directory scan buffer");
3839e657 139
21c84b71
TT
140 /*
141 * Set up the parent pointer for the root directory, if
142 * present. (If the root directory is not present, we will
143 * create it in pass 3.)
144 */
28db82a8 145 (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
21c84b71
TT
146
147 cd.buf = buf;
1b6bf175 148 cd.ctx = ctx;
f75c28de 149 cd.count = 1;
6dc64392 150 cd.max = ext2fs_dblist_count2(fs->dblist);
a5abfe03
DW
151 cd.list_offset = 0;
152 cd.ra_entries = ctx->readahead_kb * 1024 / ctx->fs->blocksize;
153 cd.next_ra_off = 0;
f75c28de
TT
154
155 if (ctx->progress)
156 (void) (ctx->progress)(ctx, 2, 0, cd.max);
ea1959f0
TT
157
158 if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX)
6dc64392 159 ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp);
efc6f628 160
a5abfe03
DW
161 check_dir_func = cd.ra_entries ? check_dir_block2 : check_dir_block;
162 cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_func,
6dc64392 163 &cd);
49a7360b 164 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
08b21301 165 return;
6267ee49
AD
166
167 if (ctx->flags & E2F_FLAG_RESTART_LATER) {
168 ctx->flags |= E2F_FLAG_RESTART;
169 return;
170 }
171
1b6bf175
TT
172 if (cd.pctx.errcode) {
173 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
08b21301
TT
174 ctx->flags |= E2F_FLAG_ABORT;
175 return;
7ac02a5e 176 }
8fdc9985 177
8fdc9985 178 for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
4cae0452
TT
179 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
180 return;
d02d0195
DW
181 if (e2fsck_dir_will_be_rehashed(ctx, dx_dir->ino) ||
182 dx_dir->numblocks == 0)
8fdc9985
TT
183 continue;
184 clear_problem_context(&pctx);
185 bad_dir = 0;
186 pctx.dir = dx_dir->ino;
187 dx_db = dx_dir->dx_block;
188 if (dx_db->flags & DX_FLAG_REFERENCED)
189 dx_db->flags |= DX_FLAG_DUP_REF;
190 else
191 dx_db->flags |= DX_FLAG_REFERENCED;
192 /*
193 * Find all of the first and last leaf blocks, and
194 * update their parent's min and max hash values
195 */
196 for (b=0, dx_db = dx_dir->dx_block;
197 b < dx_dir->numblocks;
198 b++, dx_db++) {
199 if ((dx_db->type != DX_DIRBLOCK_LEAF) ||
200 !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST)))
201 continue;
202 dx_parent = &dx_dir->dx_block[dx_db->parent];
203 /*
204 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
205 */
206 if (dx_db->flags & DX_FLAG_FIRST)
207 dx_parent->min_hash = dx_db->min_hash;
208 /*
209 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
210 */
211 if (dx_db->flags & DX_FLAG_LAST)
212 dx_parent->max_hash = dx_db->max_hash;
213 }
efc6f628 214
8fdc9985
TT
215 for (b=0, dx_db = dx_dir->dx_block;
216 b < dx_dir->numblocks;
217 b++, dx_db++) {
218 pctx.blkcount = b;
219 pctx.group = dx_db->parent;
220 code = 0;
221 if (!(dx_db->flags & DX_FLAG_FIRST) &&
222 (dx_db->min_hash < dx_db->node_min_hash)) {
223 pctx.blk = dx_db->min_hash;
224 pctx.blk2 = dx_db->node_min_hash;
225 code = PR_2_HTREE_MIN_HASH;
226 fix_problem(ctx, code, &pctx);
227 bad_dir++;
228 }
ad4fa466
TT
229 if (dx_db->type == DX_DIRBLOCK_LEAF) {
230 depth = htree_depth(dx_dir, dx_db);
231 if (depth != dx_dir->depth) {
e5e12db9 232 pctx.num = dx_dir->depth;
ad4fa466
TT
233 code = PR_2_HTREE_BAD_DEPTH;
234 fix_problem(ctx, code, &pctx);
235 bad_dir++;
236 }
237 }
8fdc9985 238 /*
efc6f628 239 * This test doesn't apply for the root block
8fdc9985
TT
240 * at block #0
241 */
242 if (b &&
243 (dx_db->max_hash > dx_db->node_max_hash)) {
244 pctx.blk = dx_db->max_hash;
245 pctx.blk2 = dx_db->node_max_hash;
246 code = PR_2_HTREE_MAX_HASH;
247 fix_problem(ctx, code, &pctx);
503f9e7f 248 bad_dir++;
8fdc9985
TT
249 }
250 if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
251 code = PR_2_HTREE_NOTREF;
252 fix_problem(ctx, code, &pctx);
253 bad_dir++;
254 } else if (dx_db->flags & DX_FLAG_DUP_REF) {
255 code = PR_2_HTREE_DUPREF;
256 fix_problem(ctx, code, &pctx);
257 bad_dir++;
258 }
8fdc9985
TT
259 }
260 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
261 clear_htree(ctx, dx_dir->ino);
62acaa1d 262 dx_dir->numblocks = 0;
8fdc9985 263 }
8fdc9985 264 }
23f75f6e 265 e2fsck_free_dx_dir_info(ctx);
149640fa 266
c4e3d3f3 267 ext2fs_free_mem(&buf);
21c84b71
TT
268 ext2fs_free_dblist(fs->dblist);
269
1b6bf175
TT
270 if (ctx->inode_bad_map) {
271 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
272 ctx->inode_bad_map = 0;
3839e657 273 }
aa4115a4
TT
274 if (ctx->inode_reg_map) {
275 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
276 ctx->inode_reg_map = 0;
277 }
dbff534e
TT
278 if (ctx->encrypted_dirs) {
279 ext2fs_u32_list_free(ctx->encrypted_dirs);
280 ctx->encrypted_dirs = 0;
281 }
a4742691
TT
282
283 clear_problem_context(&pctx);
284 if (ctx->large_files) {
285 if (!(sb->s_feature_ro_compat &
286 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
287 fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
288 sb->s_feature_ro_compat |=
289 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
0cfce7f7 290 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
a4742691
TT
291 ext2fs_mark_super_dirty(fs);
292 }
293 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
294 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
295 ext2fs_update_dynamic_rev(fs);
296 ext2fs_mark_super_dirty(fs);
297 }
a4742691 298 }
efc6f628 299
9facd076 300 print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io);
3839e657
TT
301}
302
ad4fa466
TT
303#define MAX_DEPTH 32000
304static int htree_depth(struct dx_dir_info *dx_dir,
305 struct dx_dirblock_info *dx_db)
306{
307 int depth = 0;
308
309 while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
310 dx_db = &dx_dir->dx_block[dx_db->parent];
311 depth++;
312 }
313 return depth;
314}
315
0926668d
TT
316static int dict_de_cmp(const void *a, const void *b)
317{
520ead37 318 const struct ext2_dir_entry *de_a, *de_b;
0926668d
TT
319 int a_len, b_len;
320
520ead37 321 de_a = (const struct ext2_dir_entry *) a;
70f4632b 322 a_len = ext2fs_dirent_name_len(de_a);
520ead37 323 de_b = (const struct ext2_dir_entry *) b;
70f4632b 324 b_len = ext2fs_dirent_name_len(de_b);
0926668d
TT
325
326 if (a_len != b_len)
327 return (a_len - b_len);
328
baa14bd1 329 return memcmp(de_a->name, de_b->name, a_len);
0926668d 330}
ad4fa466 331
ea1959f0
TT
332/*
333 * This is special sort function that makes sure that directory blocks
334 * with a dirblock of zero are sorted to the beginning of the list.
335 * This guarantees that the root node of the htree directories are
336 * processed first, so we know what hash version to use.
337 */
338static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
339{
6dc64392
VAH
340 const struct ext2_db_entry2 *db_a =
341 (const struct ext2_db_entry2 *) a;
342 const struct ext2_db_entry2 *db_b =
343 (const struct ext2_db_entry2 *) b;
ea1959f0
TT
344
345 if (db_a->blockcnt && !db_b->blockcnt)
346 return 1;
347
348 if (!db_a->blockcnt && db_b->blockcnt)
349 return -1;
efc6f628 350
ea1959f0
TT
351 if (db_a->blk != db_b->blk)
352 return (int) (db_a->blk - db_b->blk);
efc6f628 353
ea1959f0
TT
354 if (db_a->ino != db_b->ino)
355 return (int) (db_a->ino - db_b->ino);
356
357 return (int) (db_a->blockcnt - db_b->blockcnt);
358}
359
360
3839e657
TT
361/*
362 * Make sure the first entry in the directory is '.', and that the
363 * directory entry is sane.
364 */
1b6bf175 365static int check_dot(e2fsck_t ctx,
3839e657 366 struct ext2_dir_entry *dirent,
86c627ec 367 ext2_ino_t ino, struct problem_context *pctx)
3839e657
TT
368{
369 struct ext2_dir_entry *nextdir;
8a480350 370 unsigned int rec_len, new_len;
3c7c6d73
TT
371 int status = 0;
372 int created = 0;
373 problem_t problem = 0;
efc6f628 374
21c84b71
TT
375 if (!dirent->inode)
376 problem = PR_2_MISSING_DOT;
70f4632b 377 else if ((ext2fs_dirent_name_len(dirent) != 1) ||
21c84b71
TT
378 (dirent->name[0] != '.'))
379 problem = PR_2_1ST_NOT_DOT;
380 else if (dirent->name[1] != '\0')
381 problem = PR_2_DOT_NULL_TERM;
5dd77dbe 382
8a480350 383 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
21c84b71 384 if (problem) {
1b6bf175 385 if (fix_problem(ctx, problem, pctx)) {
5dd77dbe
TT
386 if (rec_len < 12)
387 rec_len = dirent->rec_len = 12;
3839e657 388 dirent->inode = ino;
70f4632b
JK
389 ext2fs_dirent_set_name_len(dirent, 1);
390 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
3839e657 391 dirent->name[0] = '.';
21c84b71 392 dirent->name[1] = '\0';
3839e657
TT
393 status = 1;
394 created = 1;
3839e657
TT
395 }
396 }
3839e657 397 if (dirent->inode != ino) {
1b6bf175 398 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
3839e657
TT
399 dirent->inode = ino;
400 status = 1;
21c84b71 401 }
3839e657 402 }
5dd77dbe
TT
403 if (rec_len > 12) {
404 new_len = rec_len - 12;
3839e657 405 if (new_len > 12) {
3839e657 406 if (created ||
f8188fff 407 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
3839e657
TT
408 nextdir = (struct ext2_dir_entry *)
409 ((char *) dirent + 12);
410 dirent->rec_len = 12;
8a480350
TT
411 (void) ext2fs_set_rec_len(ctx->fs, new_len,
412 nextdir);
3839e657 413 nextdir->inode = 0;
70f4632b
JK
414 ext2fs_dirent_set_name_len(nextdir, 0);
415 ext2fs_dirent_set_file_type(nextdir,
416 EXT2_FT_UNKNOWN);
3839e657
TT
417 status = 1;
418 }
419 }
420 }
421 return status;
422}
423
424/*
425 * Make sure the second entry in the directory is '..', and that the
426 * directory entry is sane. We do not check the inode number of '..'
427 * here; this gets done in pass 3.
428 */
1b6bf175 429static int check_dotdot(e2fsck_t ctx,
3839e657 430 struct ext2_dir_entry *dirent,
28db82a8 431 ext2_ino_t ino, struct problem_context *pctx)
3839e657 432{
3c7c6d73 433 problem_t problem = 0;
cf5301d7 434 unsigned int rec_len;
efc6f628 435
21c84b71
TT
436 if (!dirent->inode)
437 problem = PR_2_MISSING_DOT_DOT;
70f4632b 438 else if ((ext2fs_dirent_name_len(dirent) != 2) ||
21c84b71
TT
439 (dirent->name[0] != '.') ||
440 (dirent->name[1] != '.'))
441 problem = PR_2_2ND_NOT_DOT_DOT;
442 else if (dirent->name[2] != '\0')
443 problem = PR_2_DOT_DOT_NULL_TERM;
444
8a480350 445 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
21c84b71 446 if (problem) {
1b6bf175 447 if (fix_problem(ctx, problem, pctx)) {
5dd77dbe 448 if (rec_len < 12)
21c84b71 449 dirent->rec_len = 12;
3839e657
TT
450 /*
451 * Note: we don't have the parent inode just
452 * yet, so we will fill it in with the root
453 * inode. This will get fixed in pass 3.
454 */
455 dirent->inode = EXT2_ROOT_INO;
70f4632b
JK
456 ext2fs_dirent_set_name_len(dirent, 2);
457 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
3839e657
TT
458 dirent->name[0] = '.';
459 dirent->name[1] = '.';
21c84b71 460 dirent->name[2] = '\0';
3839e657 461 return 1;
efc6f628 462 }
3839e657
TT
463 return 0;
464 }
28db82a8
TT
465 if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) {
466 fix_problem(ctx, PR_2_NO_DIRINFO, pctx);
467 return -1;
468 }
3839e657
TT
469 return 0;
470}
471
472/*
473 * Check to make sure a directory entry doesn't contain any illegal
474 * characters.
475 */
1b6bf175 476static int check_name(e2fsck_t ctx,
3839e657 477 struct ext2_dir_entry *dirent,
54434927 478 struct problem_context *pctx)
3839e657
TT
479{
480 int i;
481 int fixup = -1;
3839e657 482 int ret = 0;
efc6f628 483
70f4632b 484 for ( i = 0; i < ext2fs_dirent_name_len(dirent); i++) {
dbff534e
TT
485 if (dirent->name[i] != '/' && dirent->name[i] != '\0')
486 continue;
dbff534e
TT
487 if (fixup < 0)
488 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
489 if (fixup == 0)
490 return 0;
491 dirent->name[i] = '.';
492 ret = 1;
3839e657
TT
493 }
494 return ret;
495}
496
aa4115a4
TT
497/*
498 * Check the directory filetype (if present)
499 */
500static _INLINE_ int check_filetype(e2fsck_t ctx,
54434927
TT
501 struct ext2_dir_entry *dirent,
502 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
503 struct problem_context *pctx)
aa4115a4 504{
70f4632b 505 int filetype = ext2fs_dirent_file_type(dirent);
aa4115a4
TT
506 int should_be = EXT2_FT_UNKNOWN;
507 struct ext2_inode inode;
508
509 if (!(ctx->fs->super->s_feature_incompat &
7847c1d4
TT
510 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
511 if (filetype == 0 ||
512 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
513 return 0;
70f4632b 514 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
7847c1d4
TT
515 return 1;
516 }
aa4115a4 517
c5d2f50d 518 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) {
aa4115a4 519 should_be = EXT2_FT_DIR;
c5d2f50d 520 } else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map,
aa4115a4
TT
521 dirent->inode)) {
522 should_be = EXT2_FT_REG_FILE;
523 } else if (ctx->inode_bad_map &&
c5d2f50d 524 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
aa4115a4
TT
525 dirent->inode))
526 should_be = 0;
527 else {
528 e2fsck_read_inode(ctx, dirent->inode, &inode,
529 "check_filetype");
6fdc7a32 530 should_be = ext2_file_type(inode.i_mode);
aa4115a4
TT
531 }
532 if (filetype == should_be)
533 return 0;
534 pctx->num = should_be;
535
536 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
537 pctx) == 0)
538 return 0;
efc6f628 539
70f4632b 540 ext2fs_dirent_set_file_type(dirent, should_be);
aa4115a4
TT
541 return 1;
542}
543
8fdc9985 544static void parse_int_node(ext2_filsys fs,
6dc64392 545 struct ext2_db_entry2 *db,
8fdc9985
TT
546 struct check_dir_struct *cd,
547 struct dx_dir_info *dx_dir,
07307114 548 char *block_buf, int failed_csum)
8fdc9985
TT
549{
550 struct ext2_dx_root_info *root;
551 struct ext2_dx_entry *ent;
552 struct ext2_dx_countlimit *limit;
553 struct dx_dirblock_info *dx_db;
ad4fa466 554 int i, expect_limit, count;
8fdc9985
TT
555 blk_t blk;
556 ext2_dirhash_t min_hash = 0xffffffff;
557 ext2_dirhash_t max_hash = 0;
ad4fa466 558 ext2_dirhash_t hash = 0, prev_hash;
07307114 559 int csum_size = 0;
8fdc9985
TT
560
561 if (db->blockcnt == 0) {
562 root = (struct ext2_dx_root_info *) (block_buf + 24);
efc6f628 563
8fdc9985
TT
564#ifdef DX_DEBUG
565 printf("Root node dump:\n");
8deb80a5 566 printf("\t Reserved zero: %u\n", root->reserved_zero);
8fdc9985
TT
567 printf("\t Hash Version: %d\n", root->hash_version);
568 printf("\t Info length: %d\n", root->info_length);
569 printf("\t Indirect levels: %d\n", root->indirect_levels);
570 printf("\t Flags: %d\n", root->unused_flags);
571#endif
572
573 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
07307114
DW
574
575 if (failed_csum &&
576 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
577 fix_problem(cd->ctx, PR_2_HTREE_ROOT_CSUM_INVALID,
578 &cd->pctx)))
579 goto clear_and_exit;
8fdc9985
TT
580 } else {
581 ent = (struct ext2_dx_entry *) (block_buf+8);
07307114
DW
582
583 if (failed_csum &&
584 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
585 fix_problem(cd->ctx, PR_2_HTREE_NODE_CSUM_INVALID,
586 &cd->pctx)))
587 goto clear_and_exit;
8fdc9985 588 }
07307114 589
8fdc9985
TT
590 limit = (struct ext2_dx_countlimit *) ent;
591
592#ifdef DX_DEBUG
efc6f628 593 printf("Number of entries (count): %d\n",
8132d840 594 ext2fs_le16_to_cpu(limit->count));
efc6f628 595 printf("Number of entries (limit): %d\n",
8132d840 596 ext2fs_le16_to_cpu(limit->limit));
8fdc9985
TT
597#endif
598
8132d840 599 count = ext2fs_le16_to_cpu(limit->count);
07307114
DW
600 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
601 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
602 csum_size = sizeof(struct ext2_dx_tail);
603 expect_limit = (fs->blocksize -
604 (csum_size + ((char *) ent - block_buf))) /
605 sizeof(struct ext2_dx_entry);
8132d840
TT
606 if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
607 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
ad4fa466
TT
608 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
609 goto clear_and_exit;
610 }
8132d840
TT
611 if (count > expect_limit) {
612 cd->pctx.num = count;
ad4fa466
TT
613 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
614 goto clear_and_exit;
615 count = expect_limit;
616 }
efc6f628 617
ad4fa466
TT
618 for (i=0; i < count; i++) {
619 prev_hash = hash;
8132d840 620 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
8fdc9985 621#ifdef DX_DEBUG
8deb80a5 622 printf("Entry #%d: Hash 0x%08x, block %u\n", i,
8132d840 623 hash, ext2fs_le32_to_cpu(ent[i].block));
8fdc9985 624#endif
8132d840 625 blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff;
8fdc9985 626 /* Check to make sure the block is valid */
977ac873 627 if (blk >= (blk_t) dx_dir->numblocks) {
b7a00563 628 cd->pctx.blk = blk;
8fdc9985 629 if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
ad4fa466
TT
630 &cd->pctx))
631 goto clear_and_exit;
977ac873 632 continue;
8fdc9985 633 }
ad4fa466
TT
634 if (hash < prev_hash &&
635 fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
636 goto clear_and_exit;
8fdc9985
TT
637 dx_db = &dx_dir->dx_block[blk];
638 if (dx_db->flags & DX_FLAG_REFERENCED) {
639 dx_db->flags |= DX_FLAG_DUP_REF;
640 } else {
641 dx_db->flags |= DX_FLAG_REFERENCED;
642 dx_db->parent = db->blockcnt;
643 }
644 if (hash < min_hash)
645 min_hash = hash;
646 if (hash > max_hash)
647 max_hash = hash;
648 dx_db->node_min_hash = hash;
8132d840 649 if ((i+1) < count)
efc6f628 650 dx_db->node_max_hash =
8132d840 651 ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
8fdc9985
TT
652 else {
653 dx_db->node_max_hash = 0xfffffffe;
654 dx_db->flags |= DX_FLAG_LAST;
655 }
656 if (i == 0)
657 dx_db->flags |= DX_FLAG_FIRST;
658 }
659#ifdef DX_DEBUG
660 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
661 db->blockcnt, min_hash, max_hash);
662#endif
663 dx_db = &dx_dir->dx_block[db->blockcnt];
664 dx_db->min_hash = min_hash;
665 dx_db->max_hash = max_hash;
ad4fa466
TT
666 return;
667
668clear_and_exit:
669 clear_htree(cd->ctx, cd->pctx.ino);
670 dx_dir->numblocks = 0;
07307114 671 e2fsck_rehash_dir_later(cd->ctx, cd->pctx.ino);
8fdc9985 672}
aa4115a4 673
e70ae99e
TT
674/*
675 * Given a busted directory, try to salvage it somehow.
efc6f628 676 *
e70ae99e 677 */
ad4fa466 678static void salvage_directory(ext2_filsys fs,
e70ae99e
TT
679 struct ext2_dir_entry *dirent,
680 struct ext2_dir_entry *prev,
82ad476d
DW
681 unsigned int *offset,
682 unsigned int block_len)
e70ae99e
TT
683{
684 char *cp = (char *) dirent;
8a480350
TT
685 int left;
686 unsigned int rec_len, prev_rec_len;
4a3dc1f0 687 unsigned int name_len;
e70ae99e 688
4a3dc1f0
DW
689 /*
690 * If the space left for the entry is too small to be an entry,
691 * we can't access dirent's fields, so plumb in the values needed
692 * so that the previous entry absorbs this one.
693 */
694 if (block_len - *offset < EXT2_DIR_ENTRY_HEADER_LEN) {
695 name_len = 0;
696 rec_len = block_len - *offset;
697 } else {
698 name_len = ext2fs_dirent_name_len(dirent);
699 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
700 }
82ad476d 701 left = block_len - *offset - rec_len;
5dd77dbe 702
e70ae99e
TT
703 /*
704 * Special case of directory entry of size 8: copy what's left
705 * of the directory block up to cover up the invalid hole.
706 */
4a3dc1f0
DW
707 if ((left >= 12) && (rec_len == EXT2_DIR_ENTRY_HEADER_LEN)) {
708 memmove(cp, cp+EXT2_DIR_ENTRY_HEADER_LEN, left);
709 memset(cp + left, 0, EXT2_DIR_ENTRY_HEADER_LEN);
ad4fa466
TT
710 return;
711 }
712 /*
713 * If the directory entry overruns the end of the directory
714 * block, and the name is small enough to fit, then adjust the
715 * record length.
716 */
717 if ((left < 0) &&
4a3dc1f0
DW
718 ((int) rec_len + left > EXT2_DIR_ENTRY_HEADER_LEN) &&
719 ((int) name_len + EXT2_DIR_ENTRY_HEADER_LEN <= (int) rec_len + left) &&
ad4fa466
TT
720 dirent->inode <= fs->super->s_inodes_count &&
721 strnlen(dirent->name, name_len) == name_len) {
8a480350 722 (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent);
ad4fa466 723 return;
e70ae99e
TT
724 }
725 /*
575307cc
KS
726 * If the record length of the directory entry is a multiple
727 * of four, and not too big, such that it is valid, let the
728 * previous directory entry absorb the invalid one.
e70ae99e 729 */
5dd77dbe 730 if (prev && rec_len && (rec_len % 4) == 0 &&
82ad476d 731 (*offset + rec_len <= block_len)) {
8a480350
TT
732 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
733 prev_rec_len += rec_len;
734 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
5dd77dbe 735 *offset += rec_len;
ad4fa466 736 return;
e70ae99e
TT
737 }
738 /*
739 * Default salvage method --- kill all of the directory
740 * entries for the rest of the block. We will either try to
741 * absorb it into the previous directory entry, or create a
742 * new empty directory entry the rest of the directory block.
743 */
744 if (prev) {
8a480350 745 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
82ad476d 746 prev_rec_len += block_len - *offset;
8a480350 747 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
ad4fa466 748 *offset = fs->blocksize;
e70ae99e 749 } else {
82ad476d 750 rec_len = block_len - *offset;
8a480350 751 (void) ext2fs_set_rec_len(fs, rec_len, dirent);
70f4632b
JK
752 ext2fs_dirent_set_name_len(dirent, 0);
753 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
e70ae99e 754 dirent->inode = 0;
e70ae99e 755 }
e70ae99e
TT
756}
757
d3eb1502 758#define NEXT_DIRENT(d) ((void *)((char *)(d) + (d)->rec_len))
17641bf2
DW
759static errcode_t insert_dirent_tail(ext2_filsys fs, void *dirbuf)
760{
761 struct ext2_dir_entry *d;
762 void *top;
763 struct ext2_dir_entry_tail *t;
17641bf2
DW
764
765 d = dirbuf;
766 top = EXT2_DIRENT_TAIL(dirbuf, fs->blocksize);
767
d3eb1502
DW
768 while (d->rec_len && !(d->rec_len & 0x3) && NEXT_DIRENT(d) <= top)
769 d = NEXT_DIRENT(d);
17641bf2
DW
770
771 if (d != top) {
772 size_t min_size = EXT2_DIR_REC_LEN(
773 ext2fs_dirent_name_len(dirbuf));
d3eb1502 774 if (min_size > top - (void *)d)
17641bf2 775 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
d3eb1502 776 d->rec_len = top - (void *)d;
17641bf2
DW
777 }
778
779 t = (struct ext2_dir_entry_tail *)top;
780 if (t->det_reserved_zero1 ||
781 t->det_rec_len != sizeof(struct ext2_dir_entry_tail) ||
782 t->det_reserved_name_len != EXT2_DIR_NAME_LEN_CSUM)
783 ext2fs_initialize_dirent_tail(fs, t);
784
785 return 0;
786}
d3eb1502 787#undef NEXT_DIRENT
17641bf2 788
52b0c6e6
DW
789static errcode_t fix_inline_dir_size(e2fsck_t ctx, ext2_ino_t ino,
790 size_t *inline_data_size,
791 struct problem_context *pctx,
792 char *buf)
793{
794 ext2_filsys fs = ctx->fs;
795 struct ext2_inode inode;
796 size_t new_size, old_size;
797 errcode_t retval;
798
799 old_size = *inline_data_size;
0ac4b397
DW
800 /*
801 * If there's not enough bytes to start the "second" dir block
802 * (in the EA space) then truncate everything to the first block.
803 */
804 if (old_size > EXT4_MIN_INLINE_DATA_SIZE &&
805 old_size < EXT4_MIN_INLINE_DATA_SIZE +
806 EXT2_DIR_REC_LEN(1)) {
807 old_size = EXT4_MIN_INLINE_DATA_SIZE;
808 new_size = old_size;
809 } else
810 /* Increase to the next four-byte boundary for salvaging */
811 new_size = old_size + (4 - (old_size & 3));
52b0c6e6
DW
812 memset(buf + old_size, 0, new_size - old_size);
813 retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size);
814 if (retval == EXT2_ET_INLINE_DATA_NO_SPACE) {
0ac4b397 815 /* Or we can't, so truncate. */
52b0c6e6
DW
816 new_size -= 4;
817 retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size);
818 if (retval) {
819 if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED,
820 pctx)) {
821 new_size = 0;
822 goto write_inode;
823 }
824 goto err;
825 }
826 } else if (retval) {
827 if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED,
828 pctx)) {
829 new_size = 0;
830 goto write_inode;
831 }
832 goto err;
833 }
834
835write_inode:
836 retval = ext2fs_read_inode(fs, ino, &inode);
837 if (retval)
838 goto err;
839
840 retval = ext2fs_inode_size_set(fs, &inode, new_size);
841 if (retval)
842 goto err;
843 if (new_size == 0)
844 inode.i_flags &= ~EXT4_INLINE_DATA_FL;
845 retval = ext2fs_write_inode(fs, ino, &inode);
846 if (retval)
847 goto err;
848 *inline_data_size = new_size;
849
850err:
851 return retval;
852}
853
a5abfe03
DW
854static int check_dir_block2(ext2_filsys fs,
855 struct ext2_db_entry2 *db,
856 void *priv_data)
857{
858 int err;
859 struct check_dir_struct *cd = priv_data;
860
861 if (cd->ra_entries && cd->list_offset >= cd->next_ra_off) {
862 err = e2fsck_readahead_dblist(fs,
863 E2FSCK_RA_DBLIST_IGNORE_BLOCKCNT,
864 fs->dblist,
865 cd->list_offset + cd->ra_entries / 8,
866 cd->ra_entries);
867 if (err)
868 cd->ra_entries = 0;
869 cd->next_ra_off = cd->list_offset + (cd->ra_entries * 7 / 8);
870 }
871
872 err = check_dir_block(fs, db, priv_data);
873 cd->list_offset++;
874 return err;
875}
876
3839e657 877static int check_dir_block(ext2_filsys fs,
6dc64392 878 struct ext2_db_entry2 *db,
54dc7ca2 879 void *priv_data)
3839e657 880{
8fdc9985 881 struct dx_dir_info *dx_dir;
8fdc9985 882 struct dx_dirblock_info *dx_db = 0;
6582dbe9 883 struct ext2_dir_entry *dirent, *prev, dot, dotdot;
8fdc9985 884 ext2_dirhash_t hash;
54434927 885 unsigned int offset = 0;
3839e657 886 int dir_modified = 0;
21c84b71 887 int dot_state;
03fa6f8a 888 unsigned int rec_len;
6dc64392 889 blk64_t block_nr = db->blk;
86c627ec 890 ext2_ino_t ino = db->ino;
28db82a8 891 ext2_ino_t subdir_parent;
21c84b71 892 __u16 links;
54dc7ca2 893 struct check_dir_struct *cd;
0ac4b397 894 char *buf, *ibuf;
1b6bf175 895 e2fsck_t ctx;
3c7c6d73 896 problem_t problem;
ea1959f0 897 struct ext2_dx_root_info *root;
e8254bfd 898 struct ext2_dx_countlimit *limit;
0926668d
TT
899 static dict_t de_dict;
900 struct problem_context pctx;
901 int dups_found = 0;
28db82a8 902 int ret;
e8548796 903 int dx_csum_size = 0, de_csum_size = 0;
81683c6a 904 int failed_csum = 0;
e8548796 905 int is_leaf = 1;
24997f1c 906 size_t inline_data_size = 0;
6582dbe9 907 int filetype = 0;
62ad2480 908 int encrypted = 0;
0ac4b397 909 size_t max_block_size;
1b6bf175 910
54dc7ca2 911 cd = (struct check_dir_struct *) priv_data;
0ac4b397 912 ibuf = buf = cd->buf;
1b6bf175 913 ctx = cd->ctx;
f8188fff 914
49a7360b 915 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
4cae0452 916 return DIRENT_ABORT;
efc6f628 917
4cae0452
TT
918 if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
919 return DIRENT_ABORT;
efc6f628 920
07307114 921 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
e8548796 922 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
07307114 923 dx_csum_size = sizeof(struct ext2_dx_tail);
e8548796
DW
924 de_csum_size = sizeof(struct ext2_dir_entry_tail);
925 }
07307114 926
6582dbe9
ZL
927 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
928 EXT2_FEATURE_INCOMPAT_FILETYPE))
929 filetype = EXT2_FT_DIR << 8;
930
3839e657 931 /*
efc6f628 932 * Make sure the inode is still in use (could have been
3839e657
TT
933 * deleted in the duplicate/bad blocks pass.
934 */
c5d2f50d 935 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino)))
3839e657 936 return 0;
50e1e10f 937
21c84b71
TT
938 cd->pctx.ino = ino;
939 cd->pctx.blk = block_nr;
940 cd->pctx.blkcount = db->blockcnt;
941 cd->pctx.ino2 = 0;
942 cd->pctx.dirent = 0;
943 cd->pctx.num = 0;
944
6582dbe9
ZL
945 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
946 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
947 errcode_t ec;
948
949 ec = ext2fs_inline_data_size(fs, ino, &inline_data_size);
950 if (ec && ec != EXT2_ET_NO_INLINE_DATA)
951 return DIRENT_ABORT;
952 }
953
954 if (db->blk == 0 && !inline_data_size) {
1b6bf175 955 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
50e1e10f
TT
956 return 0;
957 block_nr = db->blk;
958 }
efc6f628 959
3839e657
TT
960 if (db->blockcnt)
961 dot_state = 2;
962 else
963 dot_state = 0;
964
0926668d
TT
965 if (ctx->dirs_to_hash &&
966 ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
967 dups_found++;
968
3839e657 969#if 0
f3db3566 970 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
3839e657
TT
971 db->blockcnt, ino);
972#endif
efc6f628 973
f85a9ae6 974 ehandler_operation(_("reading directory block"));
cd971869 975 if (inline_data_size) {
4a3dc1f0 976 memset(buf, 0, fs->blocksize - inline_data_size);
6582dbe9 977 cd->pctx.errcode = ext2fs_inline_data_get(fs, ino, 0, buf, 0);
cd971869
DW
978 if (cd->pctx.errcode)
979 goto inline_read_fail;
980#ifdef WORDS_BIGENDIAN
0ac4b397
DW
981 if (db->blockcnt)
982 goto skip_first_read_swab;
cd971869
DW
983 *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf));
984 cd->pctx.errcode = ext2fs_dirent_swab_in2(fs,
985 buf + EXT4_INLINE_DATA_DOTDOT_SIZE,
0ac4b397
DW
986 EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DATA_DOTDOT_SIZE,
987 0);
988 if (cd->pctx.errcode)
989 goto inline_read_fail;
990skip_first_read_swab:
991 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE ||
992 !db->blockcnt)
993 goto inline_read_fail;
994 cd->pctx.errcode = ext2fs_dirent_swab_in2(fs,
995 buf + EXT4_MIN_INLINE_DATA_SIZE,
996 inline_data_size - EXT4_MIN_INLINE_DATA_SIZE,
cd971869
DW
997 0);
998#endif
999 } else
6582dbe9
ZL
1000 cd->pctx.errcode = ext2fs_read_dir_block4(fs, block_nr,
1001 buf, 0, ino);
cd971869 1002inline_read_fail:
52b0c6e6
DW
1003 pctx.ino = ino;
1004 pctx.num = inline_data_size;
0ac4b397
DW
1005 if (((inline_data_size & 3) ||
1006 (inline_data_size > EXT4_MIN_INLINE_DATA_SIZE &&
1007 inline_data_size < EXT4_MIN_INLINE_DATA_SIZE +
1008 EXT2_DIR_REC_LEN(1))) &&
52b0c6e6
DW
1009 fix_problem(ctx, PR_2_BAD_INLINE_DIR_SIZE, &pctx)) {
1010 errcode_t err = fix_inline_dir_size(ctx, ino,
1011 &inline_data_size, &pctx,
1012 buf);
1013 if (err)
1014 return DIRENT_ABORT;
1015
1016 }
e94bc631 1017 ehandler_operation(0);
b9852cd8
TT
1018 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
1019 cd->pctx.errcode = 0; /* We'll handle this ourselves */
81683c6a
DW
1020 else if (cd->pctx.errcode == EXT2_ET_DIR_CSUM_INVALID) {
1021 cd->pctx.errcode = 0; /* We'll handle this ourselves */
1022 failed_csum = 1;
1023 }
1b6bf175 1024 if (cd->pctx.errcode) {
e8548796 1025 char *buf2;
08b21301
TT
1026 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
1027 ctx->flags |= E2F_FLAG_ABORT;
1028 return DIRENT_ABORT;
1029 }
e8548796
DW
1030 ext2fs_new_dir_block(fs, db->blockcnt == 0 ? ino : 0,
1031 EXT2_ROOT_INO, &buf2);
1032 memcpy(buf, buf2, fs->blocksize);
1033 ext2fs_free_mem(&buf2);
3839e657 1034 }
8fdc9985 1035 dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
62acaa1d 1036 if (dx_dir && dx_dir->numblocks) {
8fdc9985 1037 if (db->blockcnt >= dx_dir->numblocks) {
ea9085c7 1038 pctx.dir = ino;
efc6f628 1039 if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK,
d45edec0
TT
1040 &pctx)) {
1041 clear_htree(ctx, ino);
1042 dx_dir->numblocks = 0;
1043 dx_db = 0;
1044 goto out_htree;
1045 }
1046 fatal_error(ctx, _("Can not continue."));
8fdc9985
TT
1047 }
1048 dx_db = &dx_dir->dx_block[db->blockcnt];
1049 dx_db->type = DX_DIRBLOCK_LEAF;
1050 dx_db->phys = block_nr;
1051 dx_db->min_hash = ~0;
1052 dx_db->max_hash = 0;
efc6f628 1053
8fdc9985 1054 dirent = (struct ext2_dir_entry *) buf;
8a480350 1055 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
e8254bfd 1056 limit = (struct ext2_dx_countlimit *) (buf+8);
8fdc9985 1057 if (db->blockcnt == 0) {
ea1959f0 1058 root = (struct ext2_dx_root_info *) (buf + 24);
8fdc9985
TT
1059 dx_db->type = DX_DIRBLOCK_ROOT;
1060 dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
ea1959f0
TT
1061 if ((root->reserved_zero ||
1062 root->info_length < 8 ||
1063 root->indirect_levels > 1) &&
1064 fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
1065 clear_htree(ctx, ino);
1066 dx_dir->numblocks = 0;
1067 dx_db = 0;
f77704e4 1068 }
ea1959f0 1069 dx_dir->hashversion = root->hash_version;
f77704e4
TT
1070 if ((dx_dir->hashversion <= EXT2_HASH_TEA) &&
1071 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
1072 dx_dir->hashversion += 3;
ad4fa466 1073 dx_dir->depth = root->indirect_levels + 1;
8fdc9985 1074 } else if ((dirent->inode == 0) &&
5dd77dbe 1075 (rec_len == fs->blocksize) &&
70f4632b 1076 (ext2fs_dirent_name_len(dirent) == 0) &&
efc6f628 1077 (ext2fs_le16_to_cpu(limit->limit) ==
07307114 1078 ((fs->blocksize - (8 + dx_csum_size)) /
8132d840 1079 sizeof(struct ext2_dx_entry))))
8fdc9985 1080 dx_db->type = DX_DIRBLOCK_NODE;
e8548796 1081 is_leaf = 0;
8fdc9985 1082 }
d45edec0 1083out_htree:
3839e657 1084
7f43a46f
DW
1085 /* Leaf node with no space for csum? Rebuild dirs in pass 3A. */
1086 if (is_leaf && !inline_data_size && failed_csum &&
1087 !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
1088 de_csum_size = 0;
d02d0195
DW
1089 if (e2fsck_dir_will_be_rehashed(ctx, ino)) {
1090 failed_csum = 0;
1091 goto skip_checksum;
1092 }
1093 if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_MISSING_CSUM,
7f43a46f 1094 &cd->pctx))
e8548796 1095 goto skip_checksum;
7f43a46f 1096 e2fsck_rehash_dir_later(ctx, ino);
d02d0195 1097 failed_csum = 0;
7f43a46f 1098 goto skip_checksum;
e8548796
DW
1099 }
1100 /* htree nodes don't use fake dirents to store checksums */
1101 if (!is_leaf)
1102 de_csum_size = 0;
1103
1104skip_checksum:
0ac4b397
DW
1105 if (inline_data_size) {
1106 if (db->blockcnt) {
1107 buf += EXT4_MIN_INLINE_DATA_SIZE;
1108 max_block_size = inline_data_size - EXT4_MIN_INLINE_DATA_SIZE;
1109 /* Zero-length second block, just exit */
1110 if (max_block_size == 0)
1111 return 0;
1112 } else {
1113 max_block_size = EXT4_MIN_INLINE_DATA_SIZE;
1114 }
1115 } else
1116 max_block_size = fs->blocksize - de_csum_size;
1117
62ad2480
TT
1118 if (ctx->encrypted_dirs)
1119 encrypted = ext2fs_u32_list_test(ctx->encrypted_dirs, ino);
1120
0926668d 1121 dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
e70ae99e 1122 prev = 0;
3839e657 1123 do {
3971bfe8 1124 dgrp_t group;
49a7360b 1125 ext2_ino_t first_unused_inode;
70f4632b 1126 unsigned int name_len;
49a7360b 1127
1b6bf175 1128 problem = 0;
6582dbe9
ZL
1129 if (!inline_data_size || dot_state > 1) {
1130 dirent = (struct ext2_dir_entry *) (buf + offset);
4a3dc1f0
DW
1131 /*
1132 * If there's not even space for the entry header,
1133 * force salvaging this dir.
1134 */
1135 if (max_block_size - offset < EXT2_DIR_ENTRY_HEADER_LEN)
1136 rec_len = EXT2_DIR_REC_LEN(1);
1137 else
1138 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
6582dbe9
ZL
1139 cd->pctx.dirent = dirent;
1140 cd->pctx.num = offset;
4a3dc1f0 1141 if ((offset + rec_len > max_block_size) ||
6582dbe9
ZL
1142 (rec_len < 12) ||
1143 ((rec_len % 4) != 0) ||
4a3dc1f0 1144 ((ext2fs_dirent_name_len(dirent) + EXT2_DIR_ENTRY_HEADER_LEN) > rec_len)) {
82ad476d
DW
1145 if (fix_problem(ctx, PR_2_DIR_CORRUPTED,
1146 &cd->pctx)) {
4348709c
DW
1147#ifdef WORDS_BIGENDIAN
1148 /*
1149 * On big-endian systems, if the dirent
1150 * swap routine finds a rec_len that it
1151 * doesn't like, it continues
1152 * processing the block as if rec_len
4a3dc1f0 1153 * == EXT2_DIR_ENTRY_HEADER_LEN. This means that the name
4348709c
DW
1154 * field gets byte swapped, which means
1155 * that salvage will not detect the
1156 * correct name length (unless the name
1157 * has a length that's an exact
1158 * multiple of four bytes), and it'll
1159 * discard the entry (unnecessarily)
1160 * and the rest of the dirent block.
1161 * Therefore, swap the rest of the
1162 * block back to disk order, run
1163 * salvage, and re-swap anything after
1164 * the salvaged dirent.
1165 */
1166 int need_reswab = 0;
4a3dc1f0 1167 if (rec_len < EXT2_DIR_ENTRY_HEADER_LEN || rec_len % 4) {
4348709c
DW
1168 need_reswab = 1;
1169 ext2fs_dirent_swab_in2(fs,
4a3dc1f0
DW
1170 ((char *)dirent) + EXT2_DIR_ENTRY_HEADER_LEN,
1171 max_block_size - offset - EXT2_DIR_ENTRY_HEADER_LEN,
4348709c
DW
1172 0);
1173 }
1174#endif
82ad476d
DW
1175 salvage_directory(fs, dirent, prev,
1176 &offset,
3a748dfc 1177 max_block_size);
4348709c
DW
1178#ifdef WORDS_BIGENDIAN
1179 if (need_reswab) {
1180 (void) ext2fs_get_rec_len(fs,
1181 dirent, &rec_len);
1182 ext2fs_dirent_swab_in2(fs,
1183 ((char *)dirent) + offset + rec_len,
1184 max_block_size - offset - rec_len,
1185 0);
1186 }
1187#endif
6582dbe9
ZL
1188 dir_modified++;
1189 continue;
1190 } else
1191 goto abort_free_dict;
1192 }
1193 } else {
1194 if (dot_state == 0) {
1195 memset(&dot, 0, sizeof(dot));
1196 dirent = &dot;
1197 dirent->inode = ino;
1198 dirent->rec_len = EXT2_DIR_REC_LEN(1);
1199 dirent->name_len = 1 | filetype;
1200 dirent->name[0] = '.';
1201 } else if (dot_state == 1) {
1202 memset(&dotdot, 0, sizeof(dotdot));
1203 dirent = &dotdot;
1204 dirent->inode =
1205 ((struct ext2_dir_entry *)buf)->inode;
1206 dirent->rec_len = EXT2_DIR_REC_LEN(2);
1207 dirent->name_len = 2 | filetype;
1208 dirent->name[0] = '.';
1209 dirent->name[1] = '.';
1210 } else {
1211 fatal_error(ctx, _("Can not continue."));
1212 }
1213 cd->pctx.dirent = dirent;
1214 cd->pctx.num = offset;
3839e657 1215 }
50e1e10f 1216
e70ae99e 1217 if (dot_state == 0) {
1b6bf175 1218 if (check_dot(ctx, dirent, ino, &cd->pctx))
3839e657 1219 dir_modified++;
e70ae99e 1220 } else if (dot_state == 1) {
28db82a8
TT
1221 ret = check_dotdot(ctx, dirent, ino, &cd->pctx);
1222 if (ret < 0)
0926668d 1223 goto abort_free_dict;
28db82a8 1224 if (ret)
3839e657
TT
1225 dir_modified++;
1226 } else if (dirent->inode == ino) {
1b6bf175
TT
1227 problem = PR_2_LINK_DOT;
1228 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
3839e657
TT
1229 dirent->inode = 0;
1230 dir_modified++;
21c84b71 1231 goto next;
3839e657
TT
1232 }
1233 }
efc6f628 1234 if (!dirent->inode)
3839e657 1235 goto next;
efc6f628 1236
3839e657
TT
1237 /*
1238 * Make sure the inode listed is a legal one.
efc6f628 1239 */
70f4632b 1240 name_len = ext2fs_dirent_name_len(dirent);
3839e657 1241 if (((dirent->inode != EXT2_ROOT_INO) &&
7f88b043 1242 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
3839e657 1243 (dirent->inode > fs->super->s_inodes_count)) {
1b6bf175 1244 problem = PR_2_BAD_INO;
1b6bf175 1245 } else if (ctx->inode_bb_map &&
c5d2f50d 1246 (ext2fs_test_inode_bitmap2(ctx->inode_bb_map,
1b6bf175
TT
1247 dirent->inode))) {
1248 /*
1249 * If the inode is in a bad block, offer to
1250 * clear it.
1251 */
1252 problem = PR_2_BB_INODE;
70f4632b 1253 } else if ((dot_state > 1) && (name_len == 1) &&
1b6bf175
TT
1254 (dirent->name[0] == '.')) {
1255 /*
1256 * If there's a '.' entry in anything other
1257 * than the first directory entry, it's a
1258 * duplicate entry that should be removed.
1259 */
1260 problem = PR_2_DUP_DOT;
70f4632b 1261 } else if ((dot_state > 1) && (name_len == 2) &&
efc6f628 1262 (dirent->name[0] == '.') &&
1b6bf175
TT
1263 (dirent->name[1] == '.')) {
1264 /*
1265 * If there's a '..' entry in anything other
1266 * than the second directory entry, it's a
1267 * duplicate entry that should be removed.
1268 */
1269 problem = PR_2_DUP_DOT_DOT;
e70ae99e 1270 } else if ((dot_state > 1) &&
1b6bf175
TT
1271 (dirent->inode == EXT2_ROOT_INO)) {
1272 /*
1273 * Don't allow links to the root directory.
1274 * We check this specially to make sure we
1275 * catch this error case even if the root
1276 * directory hasn't been created yet.
1277 */
1278 problem = PR_2_LINK_ROOT;
70f4632b 1279 } else if ((dot_state > 1) && (name_len == 0)) {
c40db6d5
TT
1280 /*
1281 * Don't allow zero-length directory names.
1282 */
1283 problem = PR_2_NULL_NAME;
21c84b71
TT
1284 }
1285
1b6bf175
TT
1286 if (problem) {
1287 if (fix_problem(ctx, problem, &cd->pctx)) {
21c84b71
TT
1288 dirent->inode = 0;
1289 dir_modified++;
1290 goto next;
1b6bf175
TT
1291 } else {
1292 ext2fs_unmark_valid(fs);
1293 if (problem == PR_2_BAD_INO)
1294 goto next;
21c84b71 1295 }
3839e657
TT
1296 }
1297
1298 /*
1299 * If the inode was marked as having bad fields in
1300 * pass1, process it and offer to fix/clear it.
1301 * (We wait until now so that we can display the
1302 * pathname to the user.)
1303 */
1b6bf175 1304 if (ctx->inode_bad_map &&
c5d2f50d 1305 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
3839e657 1306 dirent->inode)) {
e72a9ba3 1307 if (e2fsck_process_bad_inode(ctx, ino,
bcf9c5d4
TT
1308 dirent->inode,
1309 buf + fs->blocksize)) {
3839e657
TT
1310 dirent->inode = 0;
1311 dir_modified++;
1312 goto next;
1313 }
a02ce9df 1314 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
08b21301 1315 return DIRENT_ABORT;
3839e657
TT
1316 }
1317
49a7360b
JS
1318 group = ext2fs_group_of_ino(fs, dirent->inode);
1319 first_unused_inode = group * fs->super->s_inodes_per_group +
1320 1 + fs->super->s_inodes_per_group -
d7cca6b0 1321 ext2fs_bg_itable_unused(fs, group);
49a7360b
JS
1322 cd->pctx.group = group;
1323
1324 /*
42e89ce7
TT
1325 * Check if the inode was missed out because
1326 * _INODE_UNINIT flag was set or bg_itable_unused was
1327 * incorrect. If so, clear the _INODE_UNINIT flag and
1328 * restart e2fsck. In the future it would be nice if
1329 * we could call a function in pass1.c that checks the
1330 * newly visible inodes.
49a7360b 1331 */
cd65a24e 1332 if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) {
6267ee49 1333 pctx.num = dirent->inode;
49a7360b
JS
1334 if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT,
1335 &cd->pctx)){
e633b58a 1336 ext2fs_bg_flags_clear(fs, group,
732c8cd5 1337 EXT2_BG_INODE_UNINIT);
42e89ce7 1338 ext2fs_mark_super_dirty(fs);
6267ee49 1339 ctx->flags |= E2F_FLAG_RESTART_LATER;
49a7360b
JS
1340 } else {
1341 ext2fs_unmark_valid(fs);
1342 if (problem == PR_2_BAD_INO)
1343 goto next;
1344 }
1345 } else if (dirent->inode >= first_unused_inode) {
6267ee49 1346 pctx.num = dirent->inode;
49a7360b 1347 if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){
d7cca6b0 1348 ext2fs_bg_itable_unused_set(fs, group, 0);
49a7360b 1349 ext2fs_mark_super_dirty(fs);
6267ee49 1350 ctx->flags |= E2F_FLAG_RESTART_LATER;
49a7360b
JS
1351 } else {
1352 ext2fs_unmark_valid(fs);
1353 if (problem == PR_2_BAD_INO)
1354 goto next;
1355 }
1356 }
1357
0433c1f1
TT
1358 /*
1359 * Offer to clear unused inodes; if we are going to be
1360 * restarting the scan due to bg_itable_unused being
1361 * wrong, then don't clear any inodes to avoid zapping
1362 * inodes that were skipped during pass1 due to an
1363 * incorrect bg_itable_unused; we'll get any real
1364 * problems after we restart.
1365 */
1366 if (!(ctx->flags & E2F_FLAG_RESTART_LATER) &&
97d26ce9
TT
1367 !(ext2fs_test_inode_bitmap2(ctx->inode_used_map,
1368 dirent->inode)))
49a7360b 1369 problem = PR_2_UNUSED_INODE;
49a7360b
JS
1370
1371 if (problem) {
1372 if (fix_problem(ctx, problem, &cd->pctx)) {
1373 dirent->inode = 0;
1374 dir_modified++;
1375 goto next;
1376 } else {
1377 ext2fs_unmark_valid(fs);
1378 if (problem == PR_2_BAD_INO)
1379 goto next;
1380 }
1381 }
1382
25f291c9 1383 if (!encrypted && check_name(ctx, dirent, &cd->pctx))
1b6bf175
TT
1384 dir_modified++;
1385
aa4115a4
TT
1386 if (check_filetype(ctx, dirent, ino, &cd->pctx))
1387 dir_modified++;
1388
8fdc9985 1389 if (dx_db) {
437651ad
TT
1390 ext2fs_dirhash(dx_dir->hashversion, dirent->name,
1391 ext2fs_dirent_name_len(dirent),
1392 fs->super->s_hash_seed, &hash, 0);
8fdc9985
TT
1393 if (hash < dx_db->min_hash)
1394 dx_db->min_hash = hash;
1395 if (hash > dx_db->max_hash)
1396 dx_db->max_hash = hash;
1397 }
8fdc9985 1398
3839e657
TT
1399 /*
1400 * If this is a directory, then mark its parent in its
1401 * dir_info structure. If the parent field is already
1402 * filled in, then this directory has more than one
1403 * hard link. We assume the first link is correct,
1404 * and ask the user if he/she wants to clear this one.
1405 */
e70ae99e 1406 if ((dot_state > 1) &&
c5d2f50d 1407 (ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
3839e657 1408 dirent->inode))) {
28db82a8
TT
1409 if (e2fsck_dir_info_get_parent(ctx, dirent->inode,
1410 &subdir_parent)) {
1b6bf175
TT
1411 cd->pctx.ino = dirent->inode;
1412 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
0926668d 1413 goto abort_free_dict;
3839e657 1414 }
28db82a8
TT
1415 if (subdir_parent) {
1416 cd->pctx.ino2 = subdir_parent;
1b6bf175 1417 if (fix_problem(ctx, PR_2_LINK_DIR,
21c84b71 1418 &cd->pctx)) {
3839e657
TT
1419 dirent->inode = 0;
1420 dir_modified++;
1421 goto next;
21c84b71
TT
1422 }
1423 cd->pctx.ino2 = 0;
28db82a8 1424 } else {
efc6f628 1425 (void) e2fsck_dir_info_set_parent(ctx,
28db82a8
TT
1426 dirent->inode, ino);
1427 }
3839e657 1428 }
0926668d
TT
1429
1430 if (dups_found) {
1431 ;
1432 } else if (dict_lookup(&de_dict, dirent)) {
1433 clear_problem_context(&pctx);
1434 pctx.ino = ino;
1435 pctx.dirent = dirent;
1436 fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
e8548796 1437 e2fsck_rehash_dir_later(ctx, ino);
0926668d
TT
1438 dups_found++;
1439 } else
1440 dict_alloc_insert(&de_dict, dirent, dirent);
efc6f628 1441
1b6bf175
TT
1442 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1443 &links);
21c84b71 1444 if (links > 1)
1b6bf175
TT
1445 ctx->fs_links_count++;
1446 ctx->fs_total_count++;
3839e657 1447 next:
e70ae99e 1448 prev = dirent;
5dd77dbe 1449 if (dir_modified)
8a480350 1450 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
6582dbe9
ZL
1451 if (!inline_data_size || dot_state > 1) {
1452 offset += rec_len;
1453 } else {
6698374c 1454 if (dot_state == 1) {
6582dbe9 1455 offset = 4;
6698374c
DW
1456 /*
1457 * If we get here, we're checking an inline
1458 * directory and we've just checked a (fake)
1459 * dotdot entry that we created on the stack.
1460 * Therefore set 'prev' to NULL so that if we
1461 * call salvage_directory on the next entry,
1462 * it won't try to absorb the next entry into
1463 * the on-stack dotdot entry.
1464 */
1465 prev = NULL;
1466 }
6582dbe9 1467 }
e70ae99e 1468 dot_state++;
0ac4b397 1469 } while (offset < max_block_size);
3839e657
TT
1470#if 0
1471 printf("\n");
1472#endif
8fdc9985
TT
1473 if (dx_db) {
1474#ifdef DX_DEBUG
1475 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1476 db->blockcnt, dx_db->type,
1477 dx_db->min_hash, dx_db->max_hash);
1478#endif
b7a00563 1479 cd->pctx.dir = cd->pctx.ino;
8fdc9985
TT
1480 if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1481 (dx_db->type == DX_DIRBLOCK_NODE))
81683c6a 1482 parse_int_node(fs, db, cd, dx_dir, buf, failed_csum);
8fdc9985 1483 }
e8548796 1484
0ac4b397
DW
1485 if (offset != max_block_size) {
1486 cd->pctx.num = rec_len + offset - max_block_size;
1487 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1488 dirent->rec_len = cd->pctx.num;
1489 dir_modified++;
1b6bf175 1490 }
3839e657
TT
1491 }
1492 if (dir_modified) {
2e9d8391 1493 int flags, will_rehash;
e8548796
DW
1494 /* leaf block with no tail? Rehash dirs later. */
1495 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
1496 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
1497 is_leaf &&
17641bf2
DW
1498 !inline_data_size &&
1499 !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
1500 if (insert_dirent_tail(fs, buf) == 0)
1501 goto write_and_fix;
e8548796 1502 e2fsck_rehash_dir_later(ctx, ino);
17641bf2 1503 }
e8548796
DW
1504
1505write_and_fix:
2e9d8391
DW
1506 will_rehash = e2fsck_dir_will_be_rehashed(ctx, ino);
1507 if (will_rehash) {
1508 flags = ctx->fs->flags;
e8548796 1509 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2e9d8391 1510 }
6582dbe9 1511 if (inline_data_size) {
0ac4b397 1512 buf = ibuf;
cd971869 1513#ifdef WORDS_BIGENDIAN
0ac4b397
DW
1514 if (db->blockcnt)
1515 goto skip_first_write_swab;
cd971869
DW
1516 *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf));
1517 cd->pctx.errcode = ext2fs_dirent_swab_out2(fs,
1518 buf + EXT4_INLINE_DATA_DOTDOT_SIZE,
0ac4b397 1519 EXT4_MIN_INLINE_DATA_SIZE -
cd971869
DW
1520 EXT4_INLINE_DATA_DOTDOT_SIZE,
1521 0);
0ac4b397
DW
1522 if (cd->pctx.errcode)
1523 goto skip_second_write_swab;
1524skip_first_write_swab:
1525 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE ||
1526 !db->blockcnt)
1527 goto skip_second_write_swab;
1528 cd->pctx.errcode = ext2fs_dirent_swab_out2(fs,
1529 buf + EXT4_MIN_INLINE_DATA_SIZE,
1530 inline_data_size -
1531 EXT4_MIN_INLINE_DATA_SIZE,
1532 0);
1533skip_second_write_swab:
cd971869
DW
1534 if (cd->pctx.errcode &&
1535 !fix_problem(ctx, PR_2_WRITE_DIRBLOCK, &cd->pctx))
1536 goto abort_free_dict;
1537#endif
6582dbe9
ZL
1538 cd->pctx.errcode =
1539 ext2fs_inline_data_set(fs, ino, 0, buf,
1540 inline_data_size);
1541 } else
1542 cd->pctx.errcode = ext2fs_write_dir_block4(fs, block_nr,
1543 buf, 0, ino);
2e9d8391
DW
1544 if (will_rehash)
1545 ctx->fs->flags = (flags &
1546 EXT2_FLAG_IGNORE_CSUM_ERRORS) |
1547 (ctx->fs->flags &
1548 ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
1b6bf175 1549 if (cd->pctx.errcode) {
08b21301 1550 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
0926668d
TT
1551 &cd->pctx))
1552 goto abort_free_dict;
3839e657
TT
1553 }
1554 ext2fs_mark_changed(fs);
e8548796
DW
1555 } else if (is_leaf && failed_csum && !dir_modified) {
1556 /*
1557 * If a leaf node that fails csum makes it this far without
1558 * alteration, ask the user if the checksum should be fixed.
1559 */
1560 if (fix_problem(ctx, PR_2_LEAF_NODE_ONLY_CSUM_INVALID,
1561 &cd->pctx))
1562 goto write_and_fix;
3839e657 1563 }
0926668d 1564 dict_free_nodes(&de_dict);
3839e657 1565 return 0;
0926668d 1566abort_free_dict:
0926668d 1567 ctx->flags |= E2F_FLAG_ABORT;
49a7360b 1568 dict_free_nodes(&de_dict);
0926668d 1569 return DIRENT_ABORT;
3839e657
TT
1570}
1571
624e4a64
AK
1572struct del_block {
1573 e2fsck_t ctx;
1574 e2_blkcnt_t num;
1575};
1576
3839e657
TT
1577/*
1578 * This function is called to deallocate a block, and is an interator
1579 * functioned called by deallocate inode via ext2fs_iterate_block().
1580 */
1581static int deallocate_inode_block(ext2_filsys fs,
6dc64392 1582 blk64_t *block_nr,
54434927 1583 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
6dc64392 1584 blk64_t ref_block EXT2FS_ATTR((unused)),
54434927 1585 int ref_offset EXT2FS_ATTR((unused)),
133a56dc 1586 void *priv_data)
3839e657 1587{
624e4a64 1588 struct del_block *p = priv_data;
efc6f628 1589
4a05268c 1590 if (*block_nr == 0)
3839e657 1591 return 0;
1ba7a2f2 1592 if ((*block_nr < fs->super->s_first_data_block) ||
4efbac6f 1593 (*block_nr >= ext2fs_blocks_count(fs->super)))
1ba7a2f2 1594 return 0;
8dd650ab
DW
1595 if ((*block_nr % EXT2FS_CLUSTER_RATIO(fs)) == 0)
1596 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
624e4a64 1597 p->num++;
3839e657
TT
1598 return 0;
1599}
efc6f628 1600
3839e657
TT
1601/*
1602 * This fuction deallocates an inode
1603 */
4035f40b 1604static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
3839e657 1605{
1b6bf175 1606 ext2_filsys fs = ctx->fs;
3839e657 1607 struct ext2_inode inode;
1b6bf175 1608 struct problem_context pctx;
0684a4f3 1609 __u32 count;
624e4a64 1610 struct del_block del_block;
efc6f628 1611
08b21301 1612 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1b6bf175
TT
1613 clear_problem_context(&pctx);
1614 pctx.ino = ino;
f3db3566 1615
3839e657
TT
1616 /*
1617 * Fix up the bitmaps...
1618 */
f8188fff 1619 e2fsck_read_bitmaps(ctx);
0684a4f3
TT
1620 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1621
0c80c44b 1622 if (ext2fs_file_acl_block(fs, &inode) &&
0684a4f3 1623 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
39f5659a
DW
1624 pctx.errcode = ext2fs_adjust_ea_refcount3(fs,
1625 ext2fs_file_acl_block(fs, &inode),
1626 block_buf, -1, &count, ino);
0684a4f3
TT
1627 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1628 pctx.errcode = 0;
1629 count = 1;
1630 }
1631 if (pctx.errcode) {
0c80c44b 1632 pctx.blk = ext2fs_file_acl_block(fs, &inode);
0684a4f3
TT
1633 fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1634 ctx->flags |= E2F_FLAG_ABORT;
1635 return;
1636 }
1637 if (count == 0) {
48f23054 1638 ext2fs_block_alloc_stats2(fs,
0c80c44b 1639 ext2fs_file_acl_block(fs, &inode), -1);
0684a4f3 1640 }
0c80c44b 1641 ext2fs_file_acl_block_set(fs, &inode, 0);
0684a4f3 1642 }
3839e657 1643
0c80c44b 1644 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
c8ec2bad 1645 goto clear_inode;
a4742691 1646
2ece8390
DW
1647 /* Inline data inodes don't have blocks to iterate */
1648 if (inode.i_flags & EXT4_INLINE_DATA_FL)
1649 goto clear_inode;
1650
3b6c0938
DW
1651 if (LINUX_S_ISREG(inode.i_mode) &&
1652 ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode)))
a4742691
TT
1653 ctx->large_files--;
1654
624e4a64
AK
1655 del_block.ctx = ctx;
1656 del_block.num = 0;
6dc64392 1657 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
624e4a64
AK
1658 deallocate_inode_block,
1659 &del_block);
1b6bf175
TT
1660 if (pctx.errcode) {
1661 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
08b21301
TT
1662 ctx->flags |= E2F_FLAG_ABORT;
1663 return;
1b6bf175 1664 }
c8ec2bad
TT
1665clear_inode:
1666 /* Inode may have changed by block_iterate, so reread it */
1667 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1668 e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
3839e657
TT
1669}
1670
8fdc9985
TT
1671/*
1672 * This fuction clears the htree flag on an inode
1673 */
1674static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1675{
1676 struct ext2_inode inode;
efc6f628 1677
8fdc9985
TT
1678 e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1679 inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1680 e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
b7a00563
TT
1681 if (ctx->dirs_to_hash)
1682 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
8fdc9985
TT
1683}
1684
1685
f404167d
TT
1686int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1687 ext2_ino_t ino, char *buf)
3839e657 1688{
1b6bf175 1689 ext2_filsys fs = ctx->fs;
3839e657 1690 struct ext2_inode inode;
3839e657 1691 int inode_modified = 0;
6c313fd4 1692 int not_fixed = 0;
1e3472c5 1693 unsigned char *frag, *fsize;
21c84b71 1694 struct problem_context pctx;
3c7c6d73 1695 problem_t problem = 0;
3839e657 1696
08b21301 1697 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
21c84b71
TT
1698
1699 clear_problem_context(&pctx);
1700 pctx.ino = ino;
1701 pctx.dir = dir;
1702 pctx.inode = &inode;
1703
0c80c44b 1704 if (ext2fs_file_acl_block(fs, &inode) &&
f76344fb
TT
1705 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1706 if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
0c80c44b 1707 ext2fs_file_acl_block_set(fs, &inode, 0);
f76344fb
TT
1708 inode_modified++;
1709 } else
1710 not_fixed++;
1711 }
6c313fd4 1712
50e1e10f
TT
1713 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1714 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1715 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
08b21301
TT
1716 !(LINUX_S_ISSOCK(inode.i_mode)))
1717 problem = PR_2_BAD_MODE;
fdbdea09 1718 else if (LINUX_S_ISCHR(inode.i_mode)
0684a4f3 1719 && !e2fsck_pass1_check_device_inode(fs, &inode))
08b21301 1720 problem = PR_2_BAD_CHAR_DEV;
fdbdea09 1721 else if (LINUX_S_ISBLK(inode.i_mode)
0684a4f3 1722 && !e2fsck_pass1_check_device_inode(fs, &inode))
08b21301 1723 problem = PR_2_BAD_BLOCK_DEV;
fdbdea09 1724 else if (LINUX_S_ISFIFO(inode.i_mode)
0684a4f3 1725 && !e2fsck_pass1_check_device_inode(fs, &inode))
1dde43f0 1726 problem = PR_2_BAD_FIFO;
fdbdea09 1727 else if (LINUX_S_ISSOCK(inode.i_mode)
0684a4f3 1728 && !e2fsck_pass1_check_device_inode(fs, &inode))
1dde43f0 1729 problem = PR_2_BAD_SOCKET;
fdbdea09 1730 else if (LINUX_S_ISLNK(inode.i_mode)
7cadc577 1731 && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) {
bcf9c5d4 1732 problem = PR_2_INVALID_SYMLINK;
67052a8a 1733 }
1dde43f0 1734
08b21301
TT
1735 if (problem) {
1736 if (fix_problem(ctx, problem, &pctx)) {
1b6bf175 1737 deallocate_inode(ctx, ino, 0);
a02ce9df 1738 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
08b21301 1739 return 0;
7cf73dcd 1740 return 1;
6c313fd4
TT
1741 } else
1742 not_fixed++;
08b21301 1743 problem = 0;
7cf73dcd 1744 }
efc6f628 1745
6c313fd4
TT
1746 if (inode.i_faddr) {
1747 if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1748 inode.i_faddr = 0;
1749 inode_modified++;
1750 } else
1751 not_fixed++;
3839e657 1752 }
1e3472c5
TT
1753
1754 switch (fs->super->s_creator_os) {
1e3472c5
TT
1755 case EXT2_OS_HURD:
1756 frag = &inode.osd2.hurd2.h_i_frag;
1757 fsize = &inode.osd2.hurd2.h_i_fsize;
1758 break;
1e3472c5
TT
1759 default:
1760 frag = fsize = 0;
1761 }
21c84b71
TT
1762 if (frag && *frag) {
1763 pctx.num = *frag;
1b6bf175 1764 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
21c84b71
TT
1765 *frag = 0;
1766 inode_modified++;
7e0282c5
TT
1767 } else
1768 not_fixed++;
21c84b71
TT
1769 pctx.num = 0;
1770 }
1771 if (fsize && *fsize) {
1772 pctx.num = *fsize;
1b6bf175 1773 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
21c84b71
TT
1774 *fsize = 0;
1775 inode_modified++;
6c313fd4
TT
1776 } else
1777 not_fixed++;
21c84b71
TT
1778 pctx.num = 0;
1779 }
1780
5d17119d 1781 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
efc6f628 1782 !(fs->super->s_feature_ro_compat &
5d17119d
TT
1783 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1784 (inode.osd2.linux2.l_i_blocks_hi != 0)) {
1785 pctx.num = inode.osd2.linux2.l_i_blocks_hi;
1786 if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) {
1787 inode.osd2.linux2.l_i_blocks_hi = 0;
1788 inode_modified++;
1789 }
1790 }
1791
36769c60
JW
1792 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1793 !(fs->super->s_feature_incompat &
911ec626
TT
1794 EXT4_FEATURE_INCOMPAT_64BIT) &&
1795 inode.osd2.linux2.l_i_file_acl_high != 0) {
1796 pctx.num = inode.osd2.linux2.l_i_file_acl_high;
1797 if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) {
1798 inode.osd2.linux2.l_i_file_acl_high = 0;
1799 inode_modified++;
1800 } else
1801 not_fixed++;
1802 }
1803
0c80c44b
TT
1804 if (ext2fs_file_acl_block(fs, &inode) &&
1805 ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) ||
1806 (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) {
6c313fd4 1807 if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
0c80c44b 1808 ext2fs_file_acl_block_set(fs, &inode, 0);
6c313fd4
TT
1809 inode_modified++;
1810 } else
1811 not_fixed++;
342d847d 1812 }
21c84b71 1813 if (inode.i_dir_acl &&
6c313fd4
TT
1814 LINUX_S_ISDIR(inode.i_mode)) {
1815 if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1816 inode.i_dir_acl = 0;
1817 inode_modified++;
1818 } else
1819 not_fixed++;
21c84b71 1820 }
6c313fd4 1821
f3db3566 1822 if (inode_modified)
08b21301 1823 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
f76344fb 1824 if (!not_fixed && ctx->inode_bad_map)
c5d2f50d 1825 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
3839e657
TT
1826 return 0;
1827}
1828
50e1e10f
TT
1829/*
1830 * allocate_dir_block --- this function allocates a new directory
1831 * block for a particular inode; this is done if a directory has
1832 * a "hole" in it, or if a directory has a illegal block number
1833 * that was zeroed out and now needs to be replaced.
1834 */
1b6bf175 1835static int allocate_dir_block(e2fsck_t ctx,
6dc64392 1836 struct ext2_db_entry2 *db,
efc6f628 1837 char *buf EXT2FS_ATTR((unused)),
54434927 1838 struct problem_context *pctx)
50e1e10f 1839{
1b6bf175 1840 ext2_filsys fs = ctx->fs;
ff11309e 1841 blk64_t blk = 0;
50e1e10f
TT
1842 char *block;
1843 struct ext2_inode inode;
50e1e10f 1844
1b6bf175 1845 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
50e1e10f
TT
1846 return 1;
1847
1848 /*
1849 * Read the inode and block bitmaps in; we'll be messing with
1850 * them.
1851 */
f8188fff 1852 e2fsck_read_bitmaps(ctx);
efc6f628 1853
50e1e10f
TT
1854 /*
1855 * First, find a free block
1856 */
ff11309e
DW
1857 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1858 pctx->errcode = ext2fs_map_cluster_block(fs, db->ino, &inode,
1859 db->blockcnt, &blk);
1860 if (pctx->errcode || blk == 0) {
7b486ec0
DW
1861 blk = ext2fs_find_inode_goal(fs, db->ino, &inode, db->blockcnt);
1862 pctx->errcode = ext2fs_new_block2(fs, blk,
ff11309e
DW
1863 ctx->block_found_map, &blk);
1864 if (pctx->errcode) {
1865 pctx->str = "ext2fs_new_block";
1866 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1867 return 1;
1868 }
50e1e10f 1869 }
c5d2f50d
VAH
1870 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1871 ext2fs_mark_block_bitmap2(fs->block_map, blk);
50e1e10f
TT
1872 ext2fs_mark_bb_dirty(fs);
1873
1874 /*
1875 * Now let's create the actual data block for the inode
1876 */
1877 if (db->blockcnt)
1b6bf175 1878 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
50e1e10f 1879 else
1b6bf175
TT
1880 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1881 EXT2_ROOT_INO, &block);
50e1e10f 1882
1b6bf175
TT
1883 if (pctx->errcode) {
1884 pctx->str = "ext2fs_new_dir_block";
1885 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
50e1e10f
TT
1886 return 1;
1887 }
1888
81683c6a 1889 pctx->errcode = ext2fs_write_dir_block4(fs, blk, block, 0, db->ino);
c4e3d3f3 1890 ext2fs_free_mem(&block);
1b6bf175
TT
1891 if (pctx->errcode) {
1892 pctx->str = "ext2fs_write_dir_block";
1893 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
50e1e10f
TT
1894 return 1;
1895 }
1896
1897 /*
1898 * Update the inode block count
1899 */
1ca1059f 1900 ext2fs_iblk_add_blocks(fs, &inode, 1);
97c607b1
DW
1901 if (EXT2_I_SIZE(&inode) < (db->blockcnt+1) * fs->blocksize) {
1902 pctx->errcode = ext2fs_inode_size_set(fs, &inode,
1903 (db->blockcnt+1) * fs->blocksize);
1904 if (pctx->errcode) {
1905 pctx->str = "ext2fs_inode_size_set";
1906 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1907 return 1;
1908 }
1909 }
08b21301 1910 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
50e1e10f
TT
1911
1912 /*
1913 * Finally, update the block pointers for the inode
1914 */
1915 db->blk = blk;
2d07b3ad
TT
1916 pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET,
1917 db->blockcnt, 0, &blk);
1b6bf175
TT
1918 if (pctx->errcode) {
1919 pctx->str = "ext2fs_block_iterate";
1920 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
50e1e10f
TT
1921 return 1;
1922 }
1923
1924 return 0;
1925}