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