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