2 * pass1b.c --- Pass #1b of e2fsck
4 * This file contains pass1B, pass1C, and pass1D of e2fsck. They are
5 * only invoked if pass 1 discovered blocks which are in use by more
8 * Pass1B scans the data blocks of all the inodes again, generating a
9 * complete list of duplicate blocks and which inodes have claimed
12 * Pass1C does a tree-traversal of the filesystem, to determine the
13 * parent directories of these inodes. This step is necessary so that
14 * e2fsck can print out the pathnames of affected inodes.
16 * Pass1D is a reconciliation pass. For each inode with duplicate
17 * blocks, the user is prompted if s/he would like to clone the file
18 * (so that the file gets a fresh copy of the duplicated blocks) or
19 * simply to delete the file.
21 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
24 * This file may be redistributed under the terms of the GNU Public
35 #ifdef HAVE_INTTYPES_H
40 typedef long intptr_t;
43 /* Needed for architectures where sizeof(int) != sizeof(void *) */
44 #define INT_TO_VOIDPTR(val) ((void *)(intptr_t)(val))
45 #define VOIDPTR_TO_INT(ptr) ((int)(intptr_t)(ptr))
47 #include <et/com_err.h>
53 /* Define an extension to the ext2 library's block count information */
54 #define BLOCK_COUNT_EXTATTR (-5)
58 struct block_el *next;
63 struct inode_el *next;
68 struct inode_el *inode_list;
72 * This structure stores information about a particular inode which
73 * is sharing blocks with other inodes. This information is collected
74 * to display to the user, so that the user knows what files he or she
75 * is dealing with, when trying to decide how to resolve the conflict
76 * of multiply-claimed blocks.
81 struct ext2_inode inode;
82 struct block_el *block_list;
85 static int process_pass1b_block(ext2_filsys fs, blk_t *blocknr,
86 e2_blkcnt_t blockcnt, blk_t ref_blk,
87 int ref_offset, void *priv_data);
88 static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
89 struct dup_inode *dp, char *block_buf);
90 static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
91 struct dup_inode *dp, char* block_buf);
92 static int check_if_fs_block(e2fsck_t ctx, blk_t test_blk);
94 static void pass1b(e2fsck_t ctx, char *block_buf);
95 static void pass1c(e2fsck_t ctx, char *block_buf);
96 static void pass1d(e2fsck_t ctx, char *block_buf);
98 static int dup_inode_count = 0;
100 static dict_t blk_dict, ino_dict;
102 static ext2fs_inode_bitmap inode_dup_map;
104 static int dict_int_cmp(const void *a, const void *b)
115 * Add a duplicate block record
117 static void add_dupe(e2fsck_t ctx, ext2_ino_t ino, blk_t blk,
118 struct ext2_inode *inode)
121 struct dup_block *db;
122 struct dup_inode *di;
123 struct block_el *blk_el;
124 struct inode_el *ino_el;
126 n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(blk));
128 db = (struct dup_block *) dnode_get(n);
130 db = (struct dup_block *) e2fsck_allocate_memory(ctx,
131 sizeof(struct dup_block), "duplicate block header");
134 dict_alloc_insert(&blk_dict, INT_TO_VOIDPTR(blk), db);
136 ino_el = (struct inode_el *) e2fsck_allocate_memory(ctx,
137 sizeof(struct inode_el), "inode element");
139 ino_el->next = db->inode_list;
140 db->inode_list = ino_el;
143 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino));
145 di = (struct dup_inode *) dnode_get(n);
147 di = (struct dup_inode *) e2fsck_allocate_memory(ctx,
148 sizeof(struct dup_inode), "duplicate inode header");
149 di->dir = (ino == EXT2_ROOT_INO) ? EXT2_ROOT_INO : 0 ;
150 di->num_dupblocks = 0;
153 dict_alloc_insert(&ino_dict, INT_TO_VOIDPTR(ino), di);
155 blk_el = (struct block_el *) e2fsck_allocate_memory(ctx,
156 sizeof(struct block_el), "block element");
158 blk_el->next = di->block_list;
159 di->block_list = blk_el;
164 * Free a duplicate inode record
166 static void inode_dnode_free(dnode_t *node,
167 void *context EXT2FS_ATTR((unused)))
169 struct dup_inode *di;
170 struct block_el *p, *next;
172 di = (struct dup_inode *) dnode_get(node);
173 for (p = di->block_list; p; p = next) {
181 * Free a duplicate block record
183 static void block_dnode_free(dnode_t *node,
184 void *context EXT2FS_ATTR((unused)))
186 struct dup_block *db;
187 struct inode_el *p, *next;
189 db = (struct dup_block *) dnode_get(node);
190 for (p = db->inode_list; p; p = next) {
199 * Main procedure for handling duplicate blocks
201 void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
203 ext2_filsys fs = ctx->fs;
204 struct problem_context pctx;
206 clear_problem_context(&pctx);
208 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
209 _("multiply claimed inode map"), &inode_dup_map);
211 fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
212 ctx->flags |= E2F_FLAG_ABORT;
216 dict_init(&ino_dict, DICTCOUNT_T_MAX, dict_int_cmp);
217 dict_init(&blk_dict, DICTCOUNT_T_MAX, dict_int_cmp);
218 dict_set_allocator(&ino_dict, NULL, inode_dnode_free, NULL);
219 dict_set_allocator(&blk_dict, NULL, block_dnode_free, NULL);
221 pass1b(ctx, block_buf);
222 pass1c(ctx, block_buf);
223 pass1d(ctx, block_buf);
226 * Time to free all of the accumulated data structures that we
227 * don't need anymore.
229 dict_free_nodes(&ino_dict);
230 dict_free_nodes(&blk_dict);
234 * Scan the inodes looking for inodes that contain duplicate blocks.
236 struct process_block_struct {
240 struct ext2_inode *inode;
241 struct problem_context *pctx;
244 static void pass1b(e2fsck_t ctx, char *block_buf)
246 ext2_filsys fs = ctx->fs;
248 struct ext2_inode inode;
249 ext2_inode_scan scan;
250 struct process_block_struct pb;
251 struct problem_context pctx;
253 clear_problem_context(&pctx);
255 if (!(ctx->options & E2F_OPT_PREEN))
256 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
257 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
260 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
261 ctx->flags |= E2F_FLAG_ABORT;
264 ctx->stashed_inode = &inode;
269 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
270 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
273 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
274 ctx->flags |= E2F_FLAG_ABORT;
279 pctx.ino = ctx->stashed_ino = ino;
280 if ((ino != EXT2_BAD_INO) &&
281 !ext2fs_test_inode_bitmap(ctx->inode_used_map, ino))
288 if (ext2fs_inode_has_valid_blocks(&inode) ||
289 (ino == EXT2_BAD_INO))
290 pctx.errcode = ext2fs_block_iterate2(fs, ino,
291 0, block_buf, process_pass1b_block, &pb);
292 if (inode.i_file_acl)
293 process_pass1b_block(fs, &inode.i_file_acl,
294 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
296 end_problem_latch(ctx, PR_LATCH_DBLOCK);
297 if (ino >= EXT2_FIRST_INODE(fs->super) ||
298 ino == EXT2_ROOT_INO)
302 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
304 ext2fs_close_inode_scan(scan);
305 e2fsck_use_inode_shortcuts(ctx, 0);
308 static int process_pass1b_block(ext2_filsys fs EXT2FS_ATTR((unused)),
310 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
311 blk_t ref_blk EXT2FS_ATTR((unused)),
312 int ref_offset EXT2FS_ATTR((unused)),
315 struct process_block_struct *p;
318 if (HOLE_BLKADDR(*block_nr))
320 p = (struct process_block_struct *) priv_data;
323 if (!ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr))
326 /* OK, this is a duplicate block */
327 if (p->ino != EXT2_BAD_INO) {
328 p->pctx->blk = *block_nr;
329 fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
332 ext2fs_mark_inode_bitmap(inode_dup_map, p->ino);
334 add_dupe(ctx, p->ino, *block_nr, p->inode);
340 * Pass 1c: Scan directories for inodes with duplicate blocks. This
341 * is used so that we can print pathnames when prompting the user for
344 struct search_dir_struct {
346 ext2_ino_t first_inode;
347 ext2_ino_t max_inode;
350 static int search_dirent_proc(ext2_ino_t dir, int entry,
351 struct ext2_dir_entry *dirent,
352 int offset EXT2FS_ATTR((unused)),
353 int blocksize EXT2FS_ATTR((unused)),
354 char *buf EXT2FS_ATTR((unused)),
357 struct search_dir_struct *sd;
361 sd = (struct search_dir_struct *) priv_data;
363 if (dirent->inode > sd->max_inode)
364 /* Should abort this inode, but not everything */
367 if ((dirent->inode < sd->first_inode) || (entry < DIRENT_OTHER_FILE) ||
368 !ext2fs_test_inode_bitmap(inode_dup_map, dirent->inode))
371 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(dirent->inode));
374 p = (struct dup_inode *) dnode_get(n);
378 return(sd->count ? 0 : DIRENT_ABORT);
382 static void pass1c(e2fsck_t ctx, char *block_buf)
384 ext2_filsys fs = ctx->fs;
385 struct search_dir_struct sd;
386 struct problem_context pctx;
388 clear_problem_context(&pctx);
390 if (!(ctx->options & E2F_OPT_PREEN))
391 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
394 * Search through all directories to translate inodes to names
395 * (by searching for the containing directory for that inode.)
397 sd.count = dup_inode_count;
398 sd.first_inode = EXT2_FIRST_INODE(fs->super);
399 sd.max_inode = fs->super->s_inodes_count;
400 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
401 search_dirent_proc, &sd);
404 static void pass1d(e2fsck_t ctx, char *block_buf)
406 ext2_filsys fs = ctx->fs;
407 struct dup_inode *p, *t;
409 ext2_ino_t *shared, ino;
414 struct problem_context pctx;
419 clear_problem_context(&pctx);
421 if (!(ctx->options & E2F_OPT_PREEN))
422 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
423 e2fsck_read_bitmaps(ctx);
425 pctx.num = dup_inode_count; /* dict_count(&ino_dict); */
426 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
427 shared = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
428 sizeof(ext2_ino_t) * dict_count(&ino_dict),
429 "Shared inode list");
430 for (n = dict_first(&ino_dict); n; n = dict_next(&ino_dict, n)) {
431 p = (struct dup_inode *) dnode_get(n);
434 ino = (ext2_ino_t)VOIDPTR_TO_INT(dnode_getkey(n));
435 if (ino == EXT2_BAD_INO || ino == EXT2_RESIZE_INO)
439 * Find all of the inodes which share blocks with this
440 * one. First we find all of the duplicate blocks
441 * belonging to this inode, and then search each block
442 * get the list of inodes, and merge them together.
444 for (s = p->block_list; s; s = s->next) {
445 m = dict_lookup(&blk_dict, INT_TO_VOIDPTR(s->block));
447 continue; /* Should never happen... */
448 q = (struct dup_block *) dnode_get(m);
451 if (check_if_fs_block(ctx, s->block)) {
457 * Add all inodes used by this block to the
458 * shared[] --- which is a unique list, so
459 * if an inode is already in shared[], don't
462 for (r = q->inode_list; r; r = r->next) {
465 for (i = 0; i < shared_len; i++)
466 if (shared[i] == r->inode)
468 if (i == shared_len) {
469 shared[shared_len++] = r->inode;
475 * Report the inode that we are working on
477 pctx.inode = &p->inode;
480 pctx.blkcount = p->num_dupblocks;
481 pctx.num = meta_data ? shared_len+1 : shared_len;
482 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
487 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
489 for (i = 0; i < shared_len; i++) {
490 m = dict_lookup(&ino_dict, INT_TO_VOIDPTR(shared[i]));
492 continue; /* should never happen */
493 t = (struct dup_inode *) dnode_get(m);
495 * Report the inode that we are sharing with
497 pctx.inode = &t->inode;
498 pctx.ino = shared[i];
500 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
503 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
506 if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
507 pctx.errcode = clone_file(ctx, ino, p, block_buf);
509 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
513 if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
514 delete_file(ctx, ino, p, block_buf);
516 ext2fs_unmark_valid(fs);
518 ext2fs_free_mem(&shared);
522 * Drop the refcount on the dup_block structure, and clear the entry
523 * in the block_dup_map if appropriate.
525 static void decrement_badcount(e2fsck_t ctx, blk_t block, struct dup_block *p)
528 if (p->num_bad <= 0 ||
529 (p->num_bad == 1 && !check_if_fs_block(ctx, block)))
530 ext2fs_unmark_block_bitmap(ctx->block_dup_map, block);
533 static int delete_file_block(ext2_filsys fs,
535 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
536 blk_t ref_block EXT2FS_ATTR((unused)),
537 int ref_offset EXT2FS_ATTR((unused)),
540 struct process_block_struct *pb;
545 pb = (struct process_block_struct *) priv_data;
548 if (HOLE_BLKADDR(*block_nr))
551 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
552 n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(*block_nr));
554 p = (struct dup_block *) dnode_get(n);
555 decrement_badcount(ctx, *block_nr, p);
557 com_err("delete_file_block", 0,
558 _("internal error; can't find dup_blk for %u\n"),
561 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
562 ext2fs_block_alloc_stats(fs, *block_nr, -1);
568 static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
569 struct dup_inode *dp, char* block_buf)
571 ext2_filsys fs = ctx->fs;
572 struct process_block_struct pb;
573 struct ext2_inode inode;
574 struct problem_context pctx;
577 clear_problem_context(&pctx);
578 pctx.ino = pb.ino = ino;
579 pb.dup_blocks = dp->num_dupblocks;
581 pctx.str = "delete_file";
583 e2fsck_read_inode(ctx, ino, &inode, "delete_file");
584 if (ext2fs_inode_has_valid_blocks(&inode))
585 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
586 delete_file_block, &pb);
588 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
589 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
590 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
591 if (ctx->inode_bad_map)
592 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
593 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
595 /* Inode may have changed by block_iterate, so reread it */
596 e2fsck_read_inode(ctx, ino, &inode, "delete_file");
597 inode.i_links_count = 0;
598 inode.i_dtime = ctx->now;
599 if (inode.i_file_acl &&
600 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
602 pctx.errcode = ext2fs_adjust_ea_refcount(fs, inode.i_file_acl,
603 block_buf, -1, &count);
604 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
609 pctx.blk = inode.i_file_acl;
610 fix_problem(ctx, PR_1B_ADJ_EA_REFCOUNT, &pctx);
613 * If the count is zero, then arrange to have the
614 * block deleted. If the block is in the block_dup_map,
615 * also call delete_file_block since it will take care
616 * of keeping the accounting straight.
619 ext2fs_test_block_bitmap(ctx->block_dup_map,
621 delete_file_block(fs, &inode.i_file_acl,
622 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
624 e2fsck_write_inode(ctx, ino, &inode, "delete_file");
627 struct clone_struct {
634 static int clone_file_block(ext2_filsys fs,
636 e2_blkcnt_t blockcnt,
637 blk_t ref_block EXT2FS_ATTR((unused)),
638 int ref_offset EXT2FS_ATTR((unused)),
644 struct clone_struct *cs = (struct clone_struct *) priv_data;
650 if (HOLE_BLKADDR(*block_nr))
653 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
654 n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(*block_nr));
656 p = (struct dup_block *) dnode_get(n);
657 retval = ext2fs_new_block(fs, 0, ctx->block_found_map,
660 cs->errcode = retval;
663 if (cs->dir && (blockcnt >= 0)) {
664 retval = ext2fs_set_dir_block(fs->dblist,
665 cs->dir, new_block, blockcnt);
667 cs->errcode = retval;
672 printf("Cloning block %u to %u\n", *block_nr,
675 retval = io_channel_read_blk(fs->io, *block_nr, 1,
678 cs->errcode = retval;
681 retval = io_channel_write_blk(fs->io, new_block, 1,
684 cs->errcode = retval;
687 decrement_badcount(ctx, *block_nr, p);
688 *block_nr = new_block;
689 ext2fs_mark_block_bitmap(ctx->block_found_map,
691 ext2fs_mark_block_bitmap(fs->block_map, new_block);
692 return BLOCK_CHANGED;
694 com_err("clone_file_block", 0,
695 _("internal error; can't find dup_blk for %u\n"),
701 static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
702 struct dup_inode *dp, char* block_buf)
704 ext2_filsys fs = ctx->fs;
706 struct clone_struct cs;
707 struct problem_context pctx;
710 struct inode_el *ino_el;
711 struct dup_block *db;
712 struct dup_inode *di;
714 clear_problem_context(&pctx);
718 retval = ext2fs_get_mem(fs->blocksize, &cs.buf);
722 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, ino))
726 pctx.str = "clone_file";
727 if (ext2fs_inode_has_valid_blocks(&dp->inode))
728 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
729 clone_file_block, &cs);
730 ext2fs_mark_bb_dirty(fs);
732 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
733 retval = pctx.errcode;
737 com_err("clone_file", cs.errcode,
738 _("returned from clone_file_block"));
742 /* The inode may have changed on disk, so we have to re-read it */
743 e2fsck_read_inode(ctx, ino, &dp->inode, "clone file EA");
744 blk = dp->inode.i_file_acl;
745 if (blk && (clone_file_block(fs, &dp->inode.i_file_acl,
746 BLOCK_COUNT_EXTATTR, 0, 0, &cs) ==
748 e2fsck_write_inode(ctx, ino, &dp->inode, "clone file EA");
750 * If we cloned the EA block, find all other inodes
751 * which refered to that EA block, and modify
752 * them to point to the new EA block.
754 n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(blk));
755 db = (struct dup_block *) dnode_get(n);
756 for (ino_el = db->inode_list; ino_el; ino_el = ino_el->next) {
757 if (ino_el->inode == ino)
759 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino_el->inode));
760 di = (struct dup_inode *) dnode_get(n);
761 if (di->inode.i_file_acl == blk) {
762 di->inode.i_file_acl = dp->inode.i_file_acl;
763 e2fsck_write_inode(ctx, ino_el->inode,
764 &di->inode, "clone file EA");
765 decrement_badcount(ctx, blk, db);
771 ext2fs_free_mem(&cs.buf);
776 * This routine returns 1 if a block overlaps with one of the superblocks,
777 * group descriptors, inode bitmaps, or block bitmaps.
779 static int check_if_fs_block(e2fsck_t ctx, blk_t test_block)
781 ext2_filsys fs = ctx->fs;
785 first_block = fs->super->s_first_data_block;
786 for (i = 0; i < fs->group_desc_count; i++) {
788 /* Check superblocks/block group descriptors */
789 if (ext2fs_bg_has_super(fs, i)) {
790 if (test_block >= first_block &&
791 (test_block <= first_block + fs->desc_blocks))
795 /* Check the inode table */
796 if ((fs->group_desc[i].bg_inode_table) &&
797 (test_block >= fs->group_desc[i].bg_inode_table) &&
798 (test_block < (fs->group_desc[i].bg_inode_table +
799 fs->inode_blocks_per_group)))
802 /* Check the bitmap blocks */
803 if ((test_block == fs->group_desc[i].bg_block_bitmap) ||
804 (test_block == fs->group_desc[i].bg_inode_bitmap))
807 first_block += fs->super->s_blocks_per_group;