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