]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - e2fsck/pass2.c
fix e2fsck error message for bad htree depth
[thirdparty/e2fsprogs.git] / e2fsck / pass2.c
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 <string.h>
46
47 #include "e2fsck.h"
48 #include "problem.h"
49 #include "dict.h"
50
51 #ifdef NO_INLINE_FUNCS
52 #define _INLINE_
53 #else
54 #define _INLINE_ inline
55 #endif
56
57 /* #define DX_DEBUG */
58
59 /*
60 * Keeps track of how many times an inode is referenced.
61 */
62 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf);
63 static int check_dir_block(ext2_filsys fs,
64 struct ext2_db_entry *dir_blocks_info,
65 void *priv_data);
66 static int allocate_dir_block(e2fsck_t ctx,
67 struct ext2_db_entry *dir_blocks_info,
68 char *buf, struct problem_context *pctx);
69 static int update_dir_block(ext2_filsys fs,
70 blk_t *block_nr,
71 e2_blkcnt_t blockcnt,
72 blk_t ref_block,
73 int ref_offset,
74 void *priv_data);
75 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino);
76 static int htree_depth(struct dx_dir_info *dx_dir,
77 struct dx_dirblock_info *dx_db);
78 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b);
79
80 struct check_dir_struct {
81 char *buf;
82 struct problem_context pctx;
83 int count, max;
84 e2fsck_t ctx;
85 };
86
87 void e2fsck_pass2(e2fsck_t ctx)
88 {
89 struct ext2_super_block *sb = ctx->fs->super;
90 struct problem_context pctx;
91 ext2_filsys fs = ctx->fs;
92 char *buf;
93 #ifdef RESOURCE_TRACK
94 struct resource_track rtrack;
95 #endif
96 struct check_dir_struct cd;
97 struct dx_dir_info *dx_dir;
98 struct dx_dirblock_info *dx_db, *dx_parent;
99 int b;
100 int i, depth;
101 problem_t code;
102 int bad_dir;
103
104 #ifdef RESOURCE_TRACK
105 init_resource_track(&rtrack, ctx->fs->io);
106 #endif
107
108 clear_problem_context(&cd.pctx);
109
110 #ifdef MTRACE
111 mtrace_print("Pass 2");
112 #endif
113
114 if (!(ctx->options & E2F_OPT_PREEN))
115 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
116
117 e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT,
118 &ctx->inode_count);
119 if (ctx->inode_count)
120 cd.pctx.errcode = 0;
121 else
122 cd.pctx.errcode = ext2fs_create_icount2(fs,
123 EXT2_ICOUNT_OPT_INCREMENT,
124 0, ctx->inode_link_info,
125 &ctx->inode_count);
126 if (cd.pctx.errcode) {
127 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
128 ctx->flags |= E2F_FLAG_ABORT;
129 return;
130 }
131 buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
132 "directory scan buffer");
133
134 /*
135 * Set up the parent pointer for the root directory, if
136 * present. (If the root directory is not present, we will
137 * create it in pass 3.)
138 */
139 (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
140
141 cd.buf = buf;
142 cd.ctx = ctx;
143 cd.count = 1;
144 cd.max = ext2fs_dblist_count(fs->dblist);
145
146 if (ctx->progress)
147 (void) (ctx->progress)(ctx, 2, 0, cd.max);
148
149 if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX)
150 ext2fs_dblist_sort(fs->dblist, special_dir_block_cmp);
151
152 cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
153 &cd);
154 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
155 return;
156 if (cd.pctx.errcode) {
157 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
158 ctx->flags |= E2F_FLAG_ABORT;
159 return;
160 }
161
162 #ifdef ENABLE_HTREE
163 for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
164 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
165 return;
166 if (dx_dir->numblocks == 0)
167 continue;
168 clear_problem_context(&pctx);
169 bad_dir = 0;
170 pctx.dir = dx_dir->ino;
171 dx_db = dx_dir->dx_block;
172 if (dx_db->flags & DX_FLAG_REFERENCED)
173 dx_db->flags |= DX_FLAG_DUP_REF;
174 else
175 dx_db->flags |= DX_FLAG_REFERENCED;
176 /*
177 * Find all of the first and last leaf blocks, and
178 * update their parent's min and max hash values
179 */
180 for (b=0, dx_db = dx_dir->dx_block;
181 b < dx_dir->numblocks;
182 b++, dx_db++) {
183 if ((dx_db->type != DX_DIRBLOCK_LEAF) ||
184 !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST)))
185 continue;
186 dx_parent = &dx_dir->dx_block[dx_db->parent];
187 /*
188 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
189 */
190 if (dx_db->flags & DX_FLAG_FIRST)
191 dx_parent->min_hash = dx_db->min_hash;
192 /*
193 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
194 */
195 if (dx_db->flags & DX_FLAG_LAST)
196 dx_parent->max_hash = dx_db->max_hash;
197 }
198
199 for (b=0, dx_db = dx_dir->dx_block;
200 b < dx_dir->numblocks;
201 b++, dx_db++) {
202 pctx.blkcount = b;
203 pctx.group = dx_db->parent;
204 code = 0;
205 if (!(dx_db->flags & DX_FLAG_FIRST) &&
206 (dx_db->min_hash < dx_db->node_min_hash)) {
207 pctx.blk = dx_db->min_hash;
208 pctx.blk2 = dx_db->node_min_hash;
209 code = PR_2_HTREE_MIN_HASH;
210 fix_problem(ctx, code, &pctx);
211 bad_dir++;
212 }
213 if (dx_db->type == DX_DIRBLOCK_LEAF) {
214 depth = htree_depth(dx_dir, dx_db);
215 if (depth != dx_dir->depth) {
216 pctx.num = dx_dir->depth;
217 code = PR_2_HTREE_BAD_DEPTH;
218 fix_problem(ctx, code, &pctx);
219 bad_dir++;
220 }
221 }
222 /*
223 * This test doesn't apply for the root block
224 * at block #0
225 */
226 if (b &&
227 (dx_db->max_hash > dx_db->node_max_hash)) {
228 pctx.blk = dx_db->max_hash;
229 pctx.blk2 = dx_db->node_max_hash;
230 code = PR_2_HTREE_MAX_HASH;
231 fix_problem(ctx, code, &pctx);
232 bad_dir++;
233 }
234 if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
235 code = PR_2_HTREE_NOTREF;
236 fix_problem(ctx, code, &pctx);
237 bad_dir++;
238 } else if (dx_db->flags & DX_FLAG_DUP_REF) {
239 code = PR_2_HTREE_DUPREF;
240 fix_problem(ctx, code, &pctx);
241 bad_dir++;
242 }
243 if (code == 0)
244 continue;
245 }
246 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
247 clear_htree(ctx, dx_dir->ino);
248 dx_dir->numblocks = 0;
249 }
250 }
251 #endif
252 ext2fs_free_mem(&buf);
253 ext2fs_free_dblist(fs->dblist);
254
255 if (ctx->inode_bad_map) {
256 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
257 ctx->inode_bad_map = 0;
258 }
259 if (ctx->inode_reg_map) {
260 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
261 ctx->inode_reg_map = 0;
262 }
263
264 clear_problem_context(&pctx);
265 if (ctx->large_files) {
266 if (!(sb->s_feature_ro_compat &
267 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
268 fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
269 sb->s_feature_ro_compat |=
270 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
271 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
272 ext2fs_mark_super_dirty(fs);
273 }
274 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
275 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
276 ext2fs_update_dynamic_rev(fs);
277 ext2fs_mark_super_dirty(fs);
278 }
279 }
280
281 #ifdef RESOURCE_TRACK
282 if (ctx->options & E2F_OPT_TIME2) {
283 e2fsck_clear_progbar(ctx);
284 print_resource_track(_("Pass 2"), &rtrack, fs->io);
285 }
286 #endif
287 }
288
289 #define MAX_DEPTH 32000
290 static int htree_depth(struct dx_dir_info *dx_dir,
291 struct dx_dirblock_info *dx_db)
292 {
293 int depth = 0;
294
295 while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
296 dx_db = &dx_dir->dx_block[dx_db->parent];
297 depth++;
298 }
299 return depth;
300 }
301
302 static int dict_de_cmp(const void *a, const void *b)
303 {
304 const struct ext2_dir_entry *de_a, *de_b;
305 int a_len, b_len;
306
307 de_a = (const struct ext2_dir_entry *) a;
308 a_len = de_a->name_len & 0xFF;
309 de_b = (const struct ext2_dir_entry *) b;
310 b_len = de_b->name_len & 0xFF;
311
312 if (a_len != b_len)
313 return (a_len - b_len);
314
315 return strncmp(de_a->name, de_b->name, a_len);
316 }
317
318 /*
319 * This is special sort function that makes sure that directory blocks
320 * with a dirblock of zero are sorted to the beginning of the list.
321 * This guarantees that the root node of the htree directories are
322 * processed first, so we know what hash version to use.
323 */
324 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
325 {
326 const struct ext2_db_entry *db_a =
327 (const struct ext2_db_entry *) a;
328 const struct ext2_db_entry *db_b =
329 (const struct ext2_db_entry *) b;
330
331 if (db_a->blockcnt && !db_b->blockcnt)
332 return 1;
333
334 if (!db_a->blockcnt && db_b->blockcnt)
335 return -1;
336
337 if (db_a->blk != db_b->blk)
338 return (int) (db_a->blk - db_b->blk);
339
340 if (db_a->ino != db_b->ino)
341 return (int) (db_a->ino - db_b->ino);
342
343 return (int) (db_a->blockcnt - db_b->blockcnt);
344 }
345
346
347 /*
348 * Make sure the first entry in the directory is '.', and that the
349 * directory entry is sane.
350 */
351 static int check_dot(e2fsck_t ctx,
352 struct ext2_dir_entry *dirent,
353 ext2_ino_t ino, struct problem_context *pctx)
354 {
355 struct ext2_dir_entry *nextdir;
356 int status = 0;
357 int created = 0;
358 int new_len;
359 int problem = 0;
360
361 if (!dirent->inode)
362 problem = PR_2_MISSING_DOT;
363 else if (((dirent->name_len & 0xFF) != 1) ||
364 (dirent->name[0] != '.'))
365 problem = PR_2_1ST_NOT_DOT;
366 else if (dirent->name[1] != '\0')
367 problem = PR_2_DOT_NULL_TERM;
368
369 if (problem) {
370 if (fix_problem(ctx, problem, pctx)) {
371 if (dirent->rec_len < 12)
372 dirent->rec_len = 12;
373 dirent->inode = ino;
374 dirent->name_len = 1;
375 dirent->name[0] = '.';
376 dirent->name[1] = '\0';
377 status = 1;
378 created = 1;
379 }
380 }
381 if (dirent->inode != ino) {
382 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
383 dirent->inode = ino;
384 status = 1;
385 }
386 }
387 if (dirent->rec_len > 12) {
388 new_len = dirent->rec_len - 12;
389 if (new_len > 12) {
390 if (created ||
391 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
392 nextdir = (struct ext2_dir_entry *)
393 ((char *) dirent + 12);
394 dirent->rec_len = 12;
395 nextdir->rec_len = new_len;
396 nextdir->inode = 0;
397 nextdir->name_len = 0;
398 status = 1;
399 }
400 }
401 }
402 return status;
403 }
404
405 /*
406 * Make sure the second entry in the directory is '..', and that the
407 * directory entry is sane. We do not check the inode number of '..'
408 * here; this gets done in pass 3.
409 */
410 static int check_dotdot(e2fsck_t ctx,
411 struct ext2_dir_entry *dirent,
412 ext2_ino_t ino, struct problem_context *pctx)
413 {
414 int problem = 0;
415
416 if (!dirent->inode)
417 problem = PR_2_MISSING_DOT_DOT;
418 else if (((dirent->name_len & 0xFF) != 2) ||
419 (dirent->name[0] != '.') ||
420 (dirent->name[1] != '.'))
421 problem = PR_2_2ND_NOT_DOT_DOT;
422 else if (dirent->name[2] != '\0')
423 problem = PR_2_DOT_DOT_NULL_TERM;
424
425 if (problem) {
426 if (fix_problem(ctx, problem, pctx)) {
427 if (dirent->rec_len < 12)
428 dirent->rec_len = 12;
429 /*
430 * Note: we don't have the parent inode just
431 * yet, so we will fill it in with the root
432 * inode. This will get fixed in pass 3.
433 */
434 dirent->inode = EXT2_ROOT_INO;
435 dirent->name_len = 2;
436 dirent->name[0] = '.';
437 dirent->name[1] = '.';
438 dirent->name[2] = '\0';
439 return 1;
440 }
441 return 0;
442 }
443 if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) {
444 fix_problem(ctx, PR_2_NO_DIRINFO, pctx);
445 return -1;
446 }
447 return 0;
448 }
449
450 /*
451 * Check to make sure a directory entry doesn't contain any illegal
452 * characters.
453 */
454 static int check_name(e2fsck_t ctx,
455 struct ext2_dir_entry *dirent,
456 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
457 struct problem_context *pctx)
458 {
459 int i;
460 int fixup = -1;
461 int ret = 0;
462
463 for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
464 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
465 if (fixup < 0) {
466 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
467 }
468 if (fixup) {
469 dirent->name[i] = '.';
470 ret = 1;
471 }
472 }
473 }
474 return ret;
475 }
476
477 /*
478 * Check the directory filetype (if present)
479 */
480 static _INLINE_ int check_filetype(e2fsck_t ctx,
481 struct ext2_dir_entry *dirent,
482 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
483 struct problem_context *pctx)
484 {
485 int filetype = dirent->name_len >> 8;
486 int should_be = EXT2_FT_UNKNOWN;
487 struct ext2_inode inode;
488
489 if (!(ctx->fs->super->s_feature_incompat &
490 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
491 if (filetype == 0 ||
492 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
493 return 0;
494 dirent->name_len = dirent->name_len & 0xFF;
495 return 1;
496 }
497
498 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dirent->inode)) {
499 should_be = EXT2_FT_DIR;
500 } else if (ext2fs_test_inode_bitmap(ctx->inode_reg_map,
501 dirent->inode)) {
502 should_be = EXT2_FT_REG_FILE;
503 } else if (ctx->inode_bad_map &&
504 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
505 dirent->inode))
506 should_be = 0;
507 else {
508 e2fsck_read_inode(ctx, dirent->inode, &inode,
509 "check_filetype");
510 should_be = ext2_file_type(inode.i_mode);
511 }
512 if (filetype == should_be)
513 return 0;
514 pctx->num = should_be;
515
516 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
517 pctx) == 0)
518 return 0;
519
520 dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8;
521 return 1;
522 }
523
524 #ifdef ENABLE_HTREE
525 static void parse_int_node(ext2_filsys fs,
526 struct ext2_db_entry *db,
527 struct check_dir_struct *cd,
528 struct dx_dir_info *dx_dir,
529 char *block_buf)
530 {
531 struct ext2_dx_root_info *root;
532 struct ext2_dx_entry *ent;
533 struct ext2_dx_countlimit *limit;
534 struct dx_dirblock_info *dx_db;
535 int i, expect_limit, count;
536 blk_t blk;
537 ext2_dirhash_t min_hash = 0xffffffff;
538 ext2_dirhash_t max_hash = 0;
539 ext2_dirhash_t hash = 0, prev_hash;
540
541 if (db->blockcnt == 0) {
542 root = (struct ext2_dx_root_info *) (block_buf + 24);
543
544 #ifdef DX_DEBUG
545 printf("Root node dump:\n");
546 printf("\t Reserved zero: %u\n", root->reserved_zero);
547 printf("\t Hash Version: %d\n", root->hash_version);
548 printf("\t Info length: %d\n", root->info_length);
549 printf("\t Indirect levels: %d\n", root->indirect_levels);
550 printf("\t Flags: %d\n", root->unused_flags);
551 #endif
552
553 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
554 } else {
555 ent = (struct ext2_dx_entry *) (block_buf+8);
556 }
557 limit = (struct ext2_dx_countlimit *) ent;
558
559 #ifdef DX_DEBUG
560 printf("Number of entries (count): %d\n",
561 ext2fs_le16_to_cpu(limit->count));
562 printf("Number of entries (limit): %d\n",
563 ext2fs_le16_to_cpu(limit->limit));
564 #endif
565
566 count = ext2fs_le16_to_cpu(limit->count);
567 expect_limit = (fs->blocksize - ((char *) ent - block_buf)) /
568 sizeof(struct ext2_dx_entry);
569 if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
570 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
571 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
572 goto clear_and_exit;
573 }
574 if (count > expect_limit) {
575 cd->pctx.num = count;
576 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
577 goto clear_and_exit;
578 count = expect_limit;
579 }
580
581 for (i=0; i < count; i++) {
582 prev_hash = hash;
583 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
584 #ifdef DX_DEBUG
585 printf("Entry #%d: Hash 0x%08x, block %u\n", i,
586 hash, ext2fs_le32_to_cpu(ent[i].block));
587 #endif
588 blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff;
589 /* Check to make sure the block is valid */
590 if (blk >= (blk_t) dx_dir->numblocks) {
591 cd->pctx.blk = blk;
592 if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
593 &cd->pctx))
594 goto clear_and_exit;
595 continue;
596 }
597 if (hash < prev_hash &&
598 fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
599 goto clear_and_exit;
600 dx_db = &dx_dir->dx_block[blk];
601 if (dx_db->flags & DX_FLAG_REFERENCED) {
602 dx_db->flags |= DX_FLAG_DUP_REF;
603 } else {
604 dx_db->flags |= DX_FLAG_REFERENCED;
605 dx_db->parent = db->blockcnt;
606 }
607 if (hash < min_hash)
608 min_hash = hash;
609 if (hash > max_hash)
610 max_hash = hash;
611 dx_db->node_min_hash = hash;
612 if ((i+1) < count)
613 dx_db->node_max_hash =
614 ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
615 else {
616 dx_db->node_max_hash = 0xfffffffe;
617 dx_db->flags |= DX_FLAG_LAST;
618 }
619 if (i == 0)
620 dx_db->flags |= DX_FLAG_FIRST;
621 }
622 #ifdef DX_DEBUG
623 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
624 db->blockcnt, min_hash, max_hash);
625 #endif
626 dx_db = &dx_dir->dx_block[db->blockcnt];
627 dx_db->min_hash = min_hash;
628 dx_db->max_hash = max_hash;
629 return;
630
631 clear_and_exit:
632 clear_htree(cd->ctx, cd->pctx.ino);
633 dx_dir->numblocks = 0;
634 }
635 #endif /* ENABLE_HTREE */
636
637 /*
638 * Given a busted directory, try to salvage it somehow.
639 *
640 */
641 static void salvage_directory(ext2_filsys fs,
642 struct ext2_dir_entry *dirent,
643 struct ext2_dir_entry *prev,
644 unsigned int *offset)
645 {
646 char *cp = (char *) dirent;
647 int left = fs->blocksize - *offset - dirent->rec_len;
648 unsigned int name_len = dirent->name_len & 0xFF;
649
650 /*
651 * Special case of directory entry of size 8: copy what's left
652 * of the directory block up to cover up the invalid hole.
653 */
654 if ((left >= 12) && (dirent->rec_len == 8)) {
655 memmove(cp, cp+8, left);
656 memset(cp + left, 0, 8);
657 return;
658 }
659 /*
660 * If the directory entry overruns the end of the directory
661 * block, and the name is small enough to fit, then adjust the
662 * record length.
663 */
664 if ((left < 0) &&
665 (name_len + 8 <= dirent->rec_len + (unsigned) left) &&
666 dirent->inode <= fs->super->s_inodes_count &&
667 strnlen(dirent->name, name_len) == name_len) {
668 dirent->rec_len += left;
669 return;
670 }
671 /*
672 * If the record length of the directory entry is a multiple
673 * of four, and not too big, such that it is valid, let the
674 * previous directory entry absorb the invalid one.
675 */
676 if (prev && dirent->rec_len && (dirent->rec_len % 4) == 0 &&
677 (*offset + dirent->rec_len <= fs->blocksize)) {
678 prev->rec_len += dirent->rec_len;
679 *offset += dirent->rec_len;
680 return;
681 }
682 /*
683 * Default salvage method --- kill all of the directory
684 * entries for the rest of the block. We will either try to
685 * absorb it into the previous directory entry, or create a
686 * new empty directory entry the rest of the directory block.
687 */
688 if (prev) {
689 prev->rec_len += fs->blocksize - *offset;
690 *offset = fs->blocksize;
691 } else {
692 dirent->rec_len = fs->blocksize - *offset;
693 dirent->name_len = 0;
694 dirent->inode = 0;
695 }
696 }
697
698 static int check_dir_block(ext2_filsys fs,
699 struct ext2_db_entry *db,
700 void *priv_data)
701 {
702 struct dx_dir_info *dx_dir;
703 #ifdef ENABLE_HTREE
704 struct dx_dirblock_info *dx_db = 0;
705 #endif /* ENABLE_HTREE */
706 struct ext2_dir_entry *dirent, *prev;
707 ext2_dirhash_t hash;
708 unsigned int offset = 0;
709 const char * old_op;
710 int dir_modified = 0;
711 int dot_state;
712 blk_t block_nr = db->blk;
713 ext2_ino_t ino = db->ino;
714 ext2_ino_t subdir_parent;
715 __u16 links;
716 struct check_dir_struct *cd;
717 char *buf;
718 e2fsck_t ctx;
719 int problem;
720 struct ext2_dx_root_info *root;
721 struct ext2_dx_countlimit *limit;
722 static dict_t de_dict;
723 struct problem_context pctx;
724 int dups_found = 0;
725 int ret;
726
727 cd = (struct check_dir_struct *) priv_data;
728 buf = cd->buf;
729 ctx = cd->ctx;
730
731 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
732 return DIRENT_ABORT;
733
734 if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
735 return DIRENT_ABORT;
736
737 /*
738 * Make sure the inode is still in use (could have been
739 * deleted in the duplicate/bad blocks pass.
740 */
741 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino)))
742 return 0;
743
744 cd->pctx.ino = ino;
745 cd->pctx.blk = block_nr;
746 cd->pctx.blkcount = db->blockcnt;
747 cd->pctx.ino2 = 0;
748 cd->pctx.dirent = 0;
749 cd->pctx.num = 0;
750
751 if (db->blk == 0) {
752 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
753 return 0;
754 block_nr = db->blk;
755 }
756
757 if (db->blockcnt)
758 dot_state = 2;
759 else
760 dot_state = 0;
761
762 if (ctx->dirs_to_hash &&
763 ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
764 dups_found++;
765
766 #if 0
767 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
768 db->blockcnt, ino);
769 #endif
770
771 old_op = ehandler_operation(_("reading directory block"));
772 cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
773 ehandler_operation(0);
774 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
775 cd->pctx.errcode = 0; /* We'll handle this ourselves */
776 if (cd->pctx.errcode) {
777 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
778 ctx->flags |= E2F_FLAG_ABORT;
779 return DIRENT_ABORT;
780 }
781 memset(buf, 0, fs->blocksize);
782 }
783 #ifdef ENABLE_HTREE
784 dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
785 if (dx_dir && dx_dir->numblocks) {
786 if (db->blockcnt >= dx_dir->numblocks) {
787 if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK,
788 &pctx)) {
789 clear_htree(ctx, ino);
790 dx_dir->numblocks = 0;
791 dx_db = 0;
792 goto out_htree;
793 }
794 fatal_error(ctx, _("Can not continue."));
795 }
796 dx_db = &dx_dir->dx_block[db->blockcnt];
797 dx_db->type = DX_DIRBLOCK_LEAF;
798 dx_db->phys = block_nr;
799 dx_db->min_hash = ~0;
800 dx_db->max_hash = 0;
801
802 dirent = (struct ext2_dir_entry *) buf;
803 limit = (struct ext2_dx_countlimit *) (buf+8);
804 if (db->blockcnt == 0) {
805 root = (struct ext2_dx_root_info *) (buf + 24);
806 dx_db->type = DX_DIRBLOCK_ROOT;
807 dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
808 if ((root->reserved_zero ||
809 root->info_length < 8 ||
810 root->indirect_levels > 1) &&
811 fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
812 clear_htree(ctx, ino);
813 dx_dir->numblocks = 0;
814 dx_db = 0;
815 }
816 dx_dir->hashversion = root->hash_version;
817 if ((dx_dir->hashversion <= EXT2_HASH_TEA) &&
818 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
819 dx_dir->hashversion += 3;
820 dx_dir->depth = root->indirect_levels + 1;
821 } else if ((dirent->inode == 0) &&
822 (dirent->rec_len == fs->blocksize) &&
823 (dirent->name_len == 0) &&
824 (ext2fs_le16_to_cpu(limit->limit) ==
825 ((fs->blocksize-8) /
826 sizeof(struct ext2_dx_entry))))
827 dx_db->type = DX_DIRBLOCK_NODE;
828 }
829 out_htree:
830 #endif /* ENABLE_HTREE */
831
832 dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
833 prev = 0;
834 do {
835 int group;
836 ext2_ino_t first_unused_inode;
837
838 problem = 0;
839 dirent = (struct ext2_dir_entry *) (buf + offset);
840 cd->pctx.dirent = dirent;
841 cd->pctx.num = offset;
842 if (((offset + dirent->rec_len) > fs->blocksize) ||
843 (dirent->rec_len < 12) ||
844 ((dirent->rec_len % 4) != 0) ||
845 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
846 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
847 salvage_directory(fs, dirent, prev, &offset);
848 dir_modified++;
849 continue;
850 } else
851 goto abort_free_dict;
852 }
853 if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) {
854 if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
855 dirent->name_len = EXT2_NAME_LEN;
856 dir_modified++;
857 }
858 }
859
860 if (dot_state == 0) {
861 if (check_dot(ctx, dirent, ino, &cd->pctx))
862 dir_modified++;
863 } else if (dot_state == 1) {
864 ret = check_dotdot(ctx, dirent, ino, &cd->pctx);
865 if (ret < 0)
866 goto abort_free_dict;
867 if (ret)
868 dir_modified++;
869 } else if (dirent->inode == ino) {
870 problem = PR_2_LINK_DOT;
871 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
872 dirent->inode = 0;
873 dir_modified++;
874 goto next;
875 }
876 }
877 if (!dirent->inode)
878 goto next;
879
880 /*
881 * Make sure the inode listed is a legal one.
882 */
883 if (((dirent->inode != EXT2_ROOT_INO) &&
884 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
885 (dirent->inode > fs->super->s_inodes_count)) {
886 problem = PR_2_BAD_INO;
887 } else if (ctx->inode_bb_map &&
888 (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
889 dirent->inode))) {
890 /*
891 * If the inode is in a bad block, offer to
892 * clear it.
893 */
894 problem = PR_2_BB_INODE;
895 } else if ((dot_state > 1) &&
896 ((dirent->name_len & 0xFF) == 1) &&
897 (dirent->name[0] == '.')) {
898 /*
899 * If there's a '.' entry in anything other
900 * than the first directory entry, it's a
901 * duplicate entry that should be removed.
902 */
903 problem = PR_2_DUP_DOT;
904 } else if ((dot_state > 1) &&
905 ((dirent->name_len & 0xFF) == 2) &&
906 (dirent->name[0] == '.') &&
907 (dirent->name[1] == '.')) {
908 /*
909 * If there's a '..' entry in anything other
910 * than the second directory entry, it's a
911 * duplicate entry that should be removed.
912 */
913 problem = PR_2_DUP_DOT_DOT;
914 } else if ((dot_state > 1) &&
915 (dirent->inode == EXT2_ROOT_INO)) {
916 /*
917 * Don't allow links to the root directory.
918 * We check this specially to make sure we
919 * catch this error case even if the root
920 * directory hasn't been created yet.
921 */
922 problem = PR_2_LINK_ROOT;
923 } else if ((dot_state > 1) &&
924 (dirent->name_len & 0xFF) == 0) {
925 /*
926 * Don't allow zero-length directory names.
927 */
928 problem = PR_2_NULL_NAME;
929 }
930
931 if (problem) {
932 if (fix_problem(ctx, problem, &cd->pctx)) {
933 dirent->inode = 0;
934 dir_modified++;
935 goto next;
936 } else {
937 ext2fs_unmark_valid(fs);
938 if (problem == PR_2_BAD_INO)
939 goto next;
940 }
941 }
942
943 /*
944 * If the inode was marked as having bad fields in
945 * pass1, process it and offer to fix/clear it.
946 * (We wait until now so that we can display the
947 * pathname to the user.)
948 */
949 if (ctx->inode_bad_map &&
950 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
951 dirent->inode)) {
952 if (e2fsck_process_bad_inode(ctx, ino,
953 dirent->inode,
954 buf + fs->blocksize)) {
955 dirent->inode = 0;
956 dir_modified++;
957 goto next;
958 }
959 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
960 return DIRENT_ABORT;
961 }
962
963 group = ext2fs_group_of_ino(fs, dirent->inode);
964 first_unused_inode = group * fs->super->s_inodes_per_group +
965 1 + fs->super->s_inodes_per_group -
966 fs->group_desc[group].bg_itable_unused;
967 cd->pctx.group = group;
968
969 /*
970 * Check if the inode was missed out because _INODE_UNINIT
971 * flag was set or bg_itable_unused was incorrect.
972 * If that is the case restart e2fsck.
973 * XXX Optimisations TODO:
974 * 1. only restart e2fsck once
975 * 2. only exposed inodes are checked again.
976 */
977 if (fs->group_desc[group].bg_flags & EXT2_BG_INODE_UNINIT) {
978 if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT,
979 &cd->pctx)){
980 fs->group_desc[group].bg_flags &=
981 ~EXT2_BG_INODE_UNINIT;
982 ctx->flags |= E2F_FLAG_RESTART |
983 E2F_FLAG_SIGNAL_MASK;
984 } else {
985 ext2fs_unmark_valid(fs);
986 if (problem == PR_2_BAD_INO)
987 goto next;
988 }
989 } else if (dirent->inode >= first_unused_inode) {
990 if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){
991 fs->group_desc[group].bg_itable_unused = 0;
992 fs->group_desc[group].bg_flags &=
993 ~EXT2_BG_INODE_UNINIT;
994 ext2fs_mark_super_dirty(fs);
995 ctx->flags |= E2F_FLAG_RESTART;
996 goto restart_fsck;
997 } else {
998 ext2fs_unmark_valid(fs);
999 if (problem == PR_2_BAD_INO)
1000 goto next;
1001 }
1002 }
1003
1004 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
1005 dirent->inode))) {
1006 /*
1007 * If the inode is unused, offer to clear it.
1008 */
1009 problem = PR_2_UNUSED_INODE;
1010 }
1011
1012 if (problem) {
1013 if (fix_problem(ctx, problem, &cd->pctx)) {
1014 dirent->inode = 0;
1015 dir_modified++;
1016 goto next;
1017 } else {
1018 ext2fs_unmark_valid(fs);
1019 if (problem == PR_2_BAD_INO)
1020 goto next;
1021 }
1022 }
1023
1024 if (check_name(ctx, dirent, ino, &cd->pctx))
1025 dir_modified++;
1026
1027 if (check_filetype(ctx, dirent, ino, &cd->pctx))
1028 dir_modified++;
1029
1030 #ifdef ENABLE_HTREE
1031 if (dx_db) {
1032 ext2fs_dirhash(dx_dir->hashversion, dirent->name,
1033 (dirent->name_len & 0xFF),
1034 fs->super->s_hash_seed, &hash, 0);
1035 if (hash < dx_db->min_hash)
1036 dx_db->min_hash = hash;
1037 if (hash > dx_db->max_hash)
1038 dx_db->max_hash = hash;
1039 }
1040 #endif
1041
1042 /*
1043 * If this is a directory, then mark its parent in its
1044 * dir_info structure. If the parent field is already
1045 * filled in, then this directory has more than one
1046 * hard link. We assume the first link is correct,
1047 * and ask the user if he/she wants to clear this one.
1048 */
1049 if ((dot_state > 1) &&
1050 (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
1051 dirent->inode))) {
1052 if (e2fsck_dir_info_get_parent(ctx, dirent->inode,
1053 &subdir_parent)) {
1054 cd->pctx.ino = dirent->inode;
1055 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
1056 goto abort_free_dict;
1057 }
1058 if (subdir_parent) {
1059 cd->pctx.ino2 = subdir_parent;
1060 if (fix_problem(ctx, PR_2_LINK_DIR,
1061 &cd->pctx)) {
1062 dirent->inode = 0;
1063 dir_modified++;
1064 goto next;
1065 }
1066 cd->pctx.ino2 = 0;
1067 } else {
1068 (void) e2fsck_dir_info_set_parent(ctx,
1069 dirent->inode, ino);
1070 }
1071 }
1072
1073 if (dups_found) {
1074 ;
1075 } else if (dict_lookup(&de_dict, dirent)) {
1076 clear_problem_context(&pctx);
1077 pctx.ino = ino;
1078 pctx.dirent = dirent;
1079 fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
1080 if (!ctx->dirs_to_hash)
1081 ext2fs_u32_list_create(&ctx->dirs_to_hash, 50);
1082 if (ctx->dirs_to_hash)
1083 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1084 dups_found++;
1085 } else
1086 dict_alloc_insert(&de_dict, dirent, dirent);
1087
1088 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1089 &links);
1090 if (links > 1)
1091 ctx->fs_links_count++;
1092 ctx->fs_total_count++;
1093 next:
1094 prev = dirent;
1095 offset += dirent->rec_len;
1096 dot_state++;
1097 } while (offset < fs->blocksize);
1098 #if 0
1099 printf("\n");
1100 #endif
1101 #ifdef ENABLE_HTREE
1102 if (dx_db) {
1103 #ifdef DX_DEBUG
1104 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1105 db->blockcnt, dx_db->type,
1106 dx_db->min_hash, dx_db->max_hash);
1107 #endif
1108 cd->pctx.dir = cd->pctx.ino;
1109 if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1110 (dx_db->type == DX_DIRBLOCK_NODE))
1111 parse_int_node(fs, db, cd, dx_dir, buf);
1112 }
1113 #endif /* ENABLE_HTREE */
1114 if (offset != fs->blocksize) {
1115 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
1116 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1117 dirent->rec_len = cd->pctx.num;
1118 dir_modified++;
1119 }
1120 }
1121 if (dir_modified) {
1122 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
1123 if (cd->pctx.errcode) {
1124 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
1125 &cd->pctx))
1126 goto abort_free_dict;
1127 }
1128 ext2fs_mark_changed(fs);
1129 }
1130 dict_free_nodes(&de_dict);
1131 return 0;
1132 abort_free_dict:
1133 ctx->flags |= E2F_FLAG_ABORT;
1134 restart_fsck:
1135 dict_free_nodes(&de_dict);
1136 return DIRENT_ABORT;
1137 }
1138
1139 /*
1140 * This function is called to deallocate a block, and is an interator
1141 * functioned called by deallocate inode via ext2fs_iterate_block().
1142 */
1143 static int deallocate_inode_block(ext2_filsys fs,
1144 blk_t *block_nr,
1145 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1146 blk_t ref_block EXT2FS_ATTR((unused)),
1147 int ref_offset EXT2FS_ATTR((unused)),
1148 void *priv_data)
1149 {
1150 e2fsck_t ctx = (e2fsck_t) priv_data;
1151
1152 if (HOLE_BLKADDR(*block_nr))
1153 return 0;
1154 if ((*block_nr < fs->super->s_first_data_block) ||
1155 (*block_nr >= fs->super->s_blocks_count))
1156 return 0;
1157 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
1158 ext2fs_block_alloc_stats(fs, *block_nr, -1);
1159 return 0;
1160 }
1161
1162 /*
1163 * This fuction deallocates an inode
1164 */
1165 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
1166 {
1167 ext2_filsys fs = ctx->fs;
1168 struct ext2_inode inode;
1169 struct problem_context pctx;
1170 __u32 count;
1171
1172 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1173 e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
1174 clear_problem_context(&pctx);
1175 pctx.ino = ino;
1176
1177 /*
1178 * Fix up the bitmaps...
1179 */
1180 e2fsck_read_bitmaps(ctx);
1181 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1182
1183 if (inode.i_file_acl &&
1184 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1185 pctx.errcode = ext2fs_adjust_ea_refcount(fs, inode.i_file_acl,
1186 block_buf, -1, &count);
1187 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1188 pctx.errcode = 0;
1189 count = 1;
1190 }
1191 if (pctx.errcode) {
1192 pctx.blk = inode.i_file_acl;
1193 fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1194 ctx->flags |= E2F_FLAG_ABORT;
1195 return;
1196 }
1197 if (count == 0) {
1198 ext2fs_unmark_block_bitmap(ctx->block_found_map,
1199 inode.i_file_acl);
1200 ext2fs_block_alloc_stats(fs, inode.i_file_acl, -1);
1201 }
1202 inode.i_file_acl = 0;
1203 }
1204
1205 if (!ext2fs_inode_has_valid_blocks(&inode))
1206 return;
1207
1208 if (LINUX_S_ISREG(inode.i_mode) &&
1209 (inode.i_size_high || inode.i_size & 0x80000000UL))
1210 ctx->large_files--;
1211
1212 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
1213 deallocate_inode_block, ctx);
1214 if (pctx.errcode) {
1215 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
1216 ctx->flags |= E2F_FLAG_ABORT;
1217 return;
1218 }
1219 }
1220
1221 /*
1222 * This fuction clears the htree flag on an inode
1223 */
1224 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1225 {
1226 struct ext2_inode inode;
1227
1228 e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1229 inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1230 e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
1231 if (ctx->dirs_to_hash)
1232 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1233 }
1234
1235
1236 extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1237 ext2_ino_t ino, char *buf)
1238 {
1239 ext2_filsys fs = ctx->fs;
1240 struct ext2_inode inode;
1241 int inode_modified = 0;
1242 int not_fixed = 0;
1243 unsigned char *frag, *fsize;
1244 struct problem_context pctx;
1245 int problem = 0;
1246
1247 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
1248
1249 clear_problem_context(&pctx);
1250 pctx.ino = ino;
1251 pctx.dir = dir;
1252 pctx.inode = &inode;
1253
1254 if (inode.i_file_acl &&
1255 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1256 if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
1257 inode.i_file_acl = 0;
1258 inode_modified++;
1259 } else
1260 not_fixed++;
1261 }
1262
1263 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1264 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1265 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
1266 !(LINUX_S_ISSOCK(inode.i_mode)))
1267 problem = PR_2_BAD_MODE;
1268 else if (LINUX_S_ISCHR(inode.i_mode)
1269 && !e2fsck_pass1_check_device_inode(fs, &inode))
1270 problem = PR_2_BAD_CHAR_DEV;
1271 else if (LINUX_S_ISBLK(inode.i_mode)
1272 && !e2fsck_pass1_check_device_inode(fs, &inode))
1273 problem = PR_2_BAD_BLOCK_DEV;
1274 else if (LINUX_S_ISFIFO(inode.i_mode)
1275 && !e2fsck_pass1_check_device_inode(fs, &inode))
1276 problem = PR_2_BAD_FIFO;
1277 else if (LINUX_S_ISSOCK(inode.i_mode)
1278 && !e2fsck_pass1_check_device_inode(fs, &inode))
1279 problem = PR_2_BAD_SOCKET;
1280 else if (LINUX_S_ISLNK(inode.i_mode)
1281 && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) {
1282 problem = PR_2_INVALID_SYMLINK;
1283 }
1284
1285 if (problem) {
1286 if (fix_problem(ctx, problem, &pctx)) {
1287 deallocate_inode(ctx, ino, 0);
1288 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1289 return 0;
1290 return 1;
1291 } else
1292 not_fixed++;
1293 problem = 0;
1294 }
1295
1296 if (inode.i_faddr) {
1297 if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1298 inode.i_faddr = 0;
1299 inode_modified++;
1300 } else
1301 not_fixed++;
1302 }
1303
1304 switch (fs->super->s_creator_os) {
1305 case EXT2_OS_HURD:
1306 frag = &inode.osd2.hurd2.h_i_frag;
1307 fsize = &inode.osd2.hurd2.h_i_fsize;
1308 break;
1309 default:
1310 frag = fsize = 0;
1311 }
1312 if (frag && *frag) {
1313 pctx.num = *frag;
1314 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
1315 *frag = 0;
1316 inode_modified++;
1317 } else
1318 not_fixed++;
1319 pctx.num = 0;
1320 }
1321 if (fsize && *fsize) {
1322 pctx.num = *fsize;
1323 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
1324 *fsize = 0;
1325 inode_modified++;
1326 } else
1327 not_fixed++;
1328 pctx.num = 0;
1329 }
1330
1331 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1332 !(fs->super->s_feature_ro_compat &
1333 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1334 (inode.osd2.linux2.l_i_blocks_hi != 0)) {
1335 pctx.num = inode.osd2.linux2.l_i_blocks_hi;
1336 if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) {
1337 inode.osd2.linux2.l_i_blocks_hi = 0;
1338 inode_modified++;
1339 }
1340 }
1341
1342 if (inode.i_file_acl &&
1343 ((inode.i_file_acl < fs->super->s_first_data_block) ||
1344 (inode.i_file_acl >= fs->super->s_blocks_count))) {
1345 if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
1346 inode.i_file_acl = 0;
1347 inode_modified++;
1348 } else
1349 not_fixed++;
1350 }
1351 if (inode.i_dir_acl &&
1352 LINUX_S_ISDIR(inode.i_mode)) {
1353 if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1354 inode.i_dir_acl = 0;
1355 inode_modified++;
1356 } else
1357 not_fixed++;
1358 }
1359
1360 if (inode_modified)
1361 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
1362 if (!not_fixed && ctx->inode_bad_map)
1363 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
1364 return 0;
1365 }
1366
1367
1368 /*
1369 * allocate_dir_block --- this function allocates a new directory
1370 * block for a particular inode; this is done if a directory has
1371 * a "hole" in it, or if a directory has a illegal block number
1372 * that was zeroed out and now needs to be replaced.
1373 */
1374 static int allocate_dir_block(e2fsck_t ctx,
1375 struct ext2_db_entry *db,
1376 char *buf EXT2FS_ATTR((unused)),
1377 struct problem_context *pctx)
1378 {
1379 ext2_filsys fs = ctx->fs;
1380 blk_t blk;
1381 char *block;
1382 struct ext2_inode inode;
1383
1384 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
1385 return 1;
1386
1387 /*
1388 * Read the inode and block bitmaps in; we'll be messing with
1389 * them.
1390 */
1391 e2fsck_read_bitmaps(ctx);
1392
1393 /*
1394 * First, find a free block
1395 */
1396 pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
1397 if (pctx->errcode) {
1398 pctx->str = "ext2fs_new_block";
1399 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1400 return 1;
1401 }
1402 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1403 ext2fs_mark_block_bitmap(fs->block_map, blk);
1404 ext2fs_mark_bb_dirty(fs);
1405
1406 /*
1407 * Now let's create the actual data block for the inode
1408 */
1409 if (db->blockcnt)
1410 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
1411 else
1412 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1413 EXT2_ROOT_INO, &block);
1414
1415 if (pctx->errcode) {
1416 pctx->str = "ext2fs_new_dir_block";
1417 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1418 return 1;
1419 }
1420
1421 pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
1422 ext2fs_free_mem(&block);
1423 if (pctx->errcode) {
1424 pctx->str = "ext2fs_write_dir_block";
1425 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1426 return 1;
1427 }
1428
1429 /*
1430 * Update the inode block count
1431 */
1432 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1433 ext2fs_iblk_add_blocks(fs, &inode, 1);
1434 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
1435 inode.i_size = (db->blockcnt+1) * fs->blocksize;
1436 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
1437
1438 /*
1439 * Finally, update the block pointers for the inode
1440 */
1441 db->blk = blk;
1442 pctx->errcode = ext2fs_block_iterate2(fs, db->ino, BLOCK_FLAG_HOLE,
1443 0, update_dir_block, db);
1444 if (pctx->errcode) {
1445 pctx->str = "ext2fs_block_iterate";
1446 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1447 return 1;
1448 }
1449
1450 return 0;
1451 }
1452
1453 /*
1454 * This is a helper function for allocate_dir_block().
1455 */
1456 static int update_dir_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1457 blk_t *block_nr,
1458 e2_blkcnt_t blockcnt,
1459 blk_t ref_block EXT2FS_ATTR((unused)),
1460 int ref_offset EXT2FS_ATTR((unused)),
1461 void *priv_data)
1462 {
1463 struct ext2_db_entry *db;
1464
1465 db = (struct ext2_db_entry *) priv_data;
1466 if (db->blockcnt == (int) blockcnt) {
1467 *block_nr = db->blk;
1468 return BLOCK_CHANGED;
1469 }
1470 return 0;
1471 }