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