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