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