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;
99 static int dup_inode_founddir = 0;
101 static dict_t blk_dict, ino_dict;
103 static ext2fs_inode_bitmap inode_dup_map;
105 static int dict_int_cmp(const void *a, const void *b)
116 * Add a duplicate block record
118 static void add_dupe(e2fsck_t ctx, ext2_ino_t ino, blk_t blk,
119 struct ext2_inode *inode)
122 struct dup_block *db;
123 struct dup_inode *di;
124 struct block_el *blk_el;
125 struct inode_el *ino_el;
127 n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(blk));
129 db = (struct dup_block *) dnode_get(n);
131 db = (struct dup_block *) e2fsck_allocate_memory(ctx,
132 sizeof(struct dup_block), "duplicate block header");
135 dict_alloc_insert(&blk_dict, INT_TO_VOIDPTR(blk), db);
137 ino_el = (struct inode_el *) e2fsck_allocate_memory(ctx,
138 sizeof(struct inode_el), "inode element");
140 ino_el->next = db->inode_list;
141 db->inode_list = ino_el;
144 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino));
146 di = (struct dup_inode *) dnode_get(n);
148 di = (struct dup_inode *) e2fsck_allocate_memory(ctx,
149 sizeof(struct dup_inode), "duplicate inode header");
150 if (ino == EXT2_ROOT_INO) {
151 di->dir = EXT2_ROOT_INO;
152 dup_inode_founddir++;
156 di->num_dupblocks = 0;
159 dict_alloc_insert(&ino_dict, INT_TO_VOIDPTR(ino), di);
161 blk_el = (struct block_el *) e2fsck_allocate_memory(ctx,
162 sizeof(struct block_el), "block element");
164 blk_el->next = di->block_list;
165 di->block_list = blk_el;
170 * Free a duplicate inode record
172 static void inode_dnode_free(dnode_t *node,
173 void *context EXT2FS_ATTR((unused)))
175 struct dup_inode *di;
176 struct block_el *p, *next;
178 di = (struct dup_inode *) dnode_get(node);
179 for (p = di->block_list; p; p = next) {
187 * Free a duplicate block record
189 static void block_dnode_free(dnode_t *node,
190 void *context EXT2FS_ATTR((unused)))
192 struct dup_block *db;
193 struct inode_el *p, *next;
195 db = (struct dup_block *) dnode_get(node);
196 for (p = db->inode_list; p; p = next) {
205 * Main procedure for handling duplicate blocks
207 void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
209 ext2_filsys fs = ctx->fs;
210 struct problem_context pctx;
212 clear_problem_context(&pctx);
214 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
215 _("multiply claimed inode map"), &inode_dup_map);
217 fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
218 ctx->flags |= E2F_FLAG_ABORT;
222 dict_init(&ino_dict, DICTCOUNT_T_MAX, dict_int_cmp);
223 dict_init(&blk_dict, DICTCOUNT_T_MAX, dict_int_cmp);
224 dict_set_allocator(&ino_dict, NULL, inode_dnode_free, NULL);
225 dict_set_allocator(&blk_dict, NULL, block_dnode_free, NULL);
227 pass1b(ctx, block_buf);
228 pass1c(ctx, block_buf);
229 pass1d(ctx, block_buf);
232 * Time to free all of the accumulated data structures that we
233 * don't need anymore.
235 dict_free_nodes(&ino_dict);
236 dict_free_nodes(&blk_dict);
240 * Scan the inodes looking for inodes that contain duplicate blocks.
242 struct process_block_struct {
246 struct ext2_inode *inode;
247 struct problem_context *pctx;
250 static void pass1b(e2fsck_t ctx, char *block_buf)
252 ext2_filsys fs = ctx->fs;
254 struct ext2_inode inode;
255 ext2_inode_scan scan;
256 struct process_block_struct pb;
257 struct problem_context pctx;
259 clear_problem_context(&pctx);
261 if (!(ctx->options & E2F_OPT_PREEN))
262 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
263 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
266 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
267 ctx->flags |= E2F_FLAG_ABORT;
270 ctx->stashed_inode = &inode;
275 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
276 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
279 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
280 ctx->flags |= E2F_FLAG_ABORT;
285 pctx.ino = ctx->stashed_ino = ino;
286 if ((ino != EXT2_BAD_INO) &&
287 !ext2fs_test_inode_bitmap(ctx->inode_used_map, ino))
294 if (ext2fs_inode_has_valid_blocks(&inode) ||
295 (ino == EXT2_BAD_INO))
296 pctx.errcode = ext2fs_block_iterate2(fs, ino,
297 0, block_buf, process_pass1b_block, &pb);
298 if (inode.i_file_acl)
299 process_pass1b_block(fs, &inode.i_file_acl,
300 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
302 end_problem_latch(ctx, PR_LATCH_DBLOCK);
303 if (ino >= EXT2_FIRST_INODE(fs->super) ||
304 ino == EXT2_ROOT_INO)
308 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
310 ext2fs_close_inode_scan(scan);
311 e2fsck_use_inode_shortcuts(ctx, 0);
314 static int process_pass1b_block(ext2_filsys fs EXT2FS_ATTR((unused)),
316 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
317 blk_t ref_blk EXT2FS_ATTR((unused)),
318 int ref_offset EXT2FS_ATTR((unused)),
321 struct process_block_struct *p;
324 if (HOLE_BLKADDR(*block_nr))
326 p = (struct process_block_struct *) priv_data;
329 if (!ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr))
332 /* OK, this is a duplicate block */
333 if (p->ino != EXT2_BAD_INO) {
334 p->pctx->blk = *block_nr;
335 fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
338 ext2fs_mark_inode_bitmap(inode_dup_map, p->ino);
340 add_dupe(ctx, p->ino, *block_nr, p->inode);
346 * Pass 1c: Scan directories for inodes with duplicate blocks. This
347 * is used so that we can print pathnames when prompting the user for
350 struct search_dir_struct {
352 ext2_ino_t first_inode;
353 ext2_ino_t max_inode;
356 static int search_dirent_proc(ext2_ino_t dir, int entry,
357 struct ext2_dir_entry *dirent,
358 int offset EXT2FS_ATTR((unused)),
359 int blocksize EXT2FS_ATTR((unused)),
360 char *buf EXT2FS_ATTR((unused)),
363 struct search_dir_struct *sd;
367 sd = (struct search_dir_struct *) priv_data;
369 if (dirent->inode > sd->max_inode)
370 /* Should abort this inode, but not everything */
373 if ((dirent->inode < sd->first_inode) || (entry < DIRENT_OTHER_FILE) ||
374 !ext2fs_test_inode_bitmap(inode_dup_map, dirent->inode))
377 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(dirent->inode));
380 p = (struct dup_inode *) dnode_get(n);
386 return(sd->count ? 0 : DIRENT_ABORT);
390 static void pass1c(e2fsck_t ctx, char *block_buf)
392 ext2_filsys fs = ctx->fs;
393 struct search_dir_struct sd;
394 struct problem_context pctx;
396 clear_problem_context(&pctx);
398 if (!(ctx->options & E2F_OPT_PREEN))
399 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
402 * Search through all directories to translate inodes to names
403 * (by searching for the containing directory for that inode.)
405 sd.count = dup_inode_count - dup_inode_founddir;
406 sd.first_inode = EXT2_FIRST_INODE(fs->super);
407 sd.max_inode = fs->super->s_inodes_count;
408 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
409 search_dirent_proc, &sd);
412 static void pass1d(e2fsck_t ctx, char *block_buf)
414 ext2_filsys fs = ctx->fs;
415 struct dup_inode *p, *t;
417 ext2_ino_t *shared, ino;
422 struct problem_context pctx;
427 clear_problem_context(&pctx);
429 if (!(ctx->options & E2F_OPT_PREEN))
430 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
431 e2fsck_read_bitmaps(ctx);
433 pctx.num = dup_inode_count; /* dict_count(&ino_dict); */
434 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
435 shared = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
436 sizeof(ext2_ino_t) * dict_count(&ino_dict),
437 "Shared inode list");
438 for (n = dict_first(&ino_dict); n; n = dict_next(&ino_dict, n)) {
439 p = (struct dup_inode *) dnode_get(n);
442 ino = (ext2_ino_t)VOIDPTR_TO_INT(dnode_getkey(n));
443 if (ino == EXT2_BAD_INO || ino == EXT2_RESIZE_INO)
447 * Find all of the inodes which share blocks with this
448 * one. First we find all of the duplicate blocks
449 * belonging to this inode, and then search each block
450 * get the list of inodes, and merge them together.
452 for (s = p->block_list; s; s = s->next) {
453 m = dict_lookup(&blk_dict, INT_TO_VOIDPTR(s->block));
455 continue; /* Should never happen... */
456 q = (struct dup_block *) dnode_get(m);
459 if (check_if_fs_block(ctx, s->block)) {
465 * Add all inodes used by this block to the
466 * shared[] --- which is a unique list, so
467 * if an inode is already in shared[], don't
470 for (r = q->inode_list; r; r = r->next) {
473 for (i = 0; i < shared_len; i++)
474 if (shared[i] == r->inode)
476 if (i == shared_len) {
477 shared[shared_len++] = r->inode;
483 * Report the inode that we are working on
485 pctx.inode = &p->inode;
488 pctx.blkcount = p->num_dupblocks;
489 pctx.num = meta_data ? shared_len+1 : shared_len;
490 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
495 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
497 for (i = 0; i < shared_len; i++) {
498 m = dict_lookup(&ino_dict, INT_TO_VOIDPTR(shared[i]));
500 continue; /* should never happen */
501 t = (struct dup_inode *) dnode_get(m);
503 * Report the inode that we are sharing with
505 pctx.inode = &t->inode;
506 pctx.ino = shared[i];
508 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
511 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
514 if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
515 pctx.errcode = clone_file(ctx, ino, p, block_buf);
517 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
521 if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
522 delete_file(ctx, ino, p, block_buf);
524 ext2fs_unmark_valid(fs);
526 ext2fs_free_mem(&shared);
530 * Drop the refcount on the dup_block structure, and clear the entry
531 * in the block_dup_map if appropriate.
533 static void decrement_badcount(e2fsck_t ctx, blk_t block, struct dup_block *p)
536 if (p->num_bad <= 0 ||
537 (p->num_bad == 1 && !check_if_fs_block(ctx, block)))
538 ext2fs_unmark_block_bitmap(ctx->block_dup_map, block);
541 static int delete_file_block(ext2_filsys fs,
543 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
544 blk_t ref_block EXT2FS_ATTR((unused)),
545 int ref_offset EXT2FS_ATTR((unused)),
548 struct process_block_struct *pb;
553 pb = (struct process_block_struct *) priv_data;
556 if (HOLE_BLKADDR(*block_nr))
559 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
560 n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(*block_nr));
562 p = (struct dup_block *) dnode_get(n);
563 decrement_badcount(ctx, *block_nr, p);
565 com_err("delete_file_block", 0,
566 _("internal error; can't find dup_blk for %u\n"),
569 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
570 ext2fs_block_alloc_stats(fs, *block_nr, -1);
576 static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
577 struct dup_inode *dp, char* block_buf)
579 ext2_filsys fs = ctx->fs;
580 struct process_block_struct pb;
581 struct ext2_inode inode;
582 struct problem_context pctx;
585 clear_problem_context(&pctx);
586 pctx.ino = pb.ino = ino;
587 pb.dup_blocks = dp->num_dupblocks;
589 pctx.str = "delete_file";
591 e2fsck_read_inode(ctx, ino, &inode, "delete_file");
592 if (ext2fs_inode_has_valid_blocks(&inode))
593 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
594 delete_file_block, &pb);
596 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
597 if (ctx->inode_bad_map)
598 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
599 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
601 /* Inode may have changed by block_iterate, so reread it */
602 e2fsck_read_inode(ctx, ino, &inode, "delete_file");
603 e2fsck_clear_inode(ctx, ino, &inode, 0, "delete_file");
604 if (inode.i_file_acl &&
605 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
607 pctx.errcode = ext2fs_adjust_ea_refcount(fs, inode.i_file_acl,
608 block_buf, -1, &count);
609 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
614 pctx.blk = inode.i_file_acl;
615 fix_problem(ctx, PR_1B_ADJ_EA_REFCOUNT, &pctx);
618 * If the count is zero, then arrange to have the
619 * block deleted. If the block is in the block_dup_map,
620 * also call delete_file_block since it will take care
621 * of keeping the accounting straight.
624 ext2fs_test_block_bitmap(ctx->block_dup_map,
626 delete_file_block(fs, &inode.i_file_acl,
627 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
631 struct clone_struct {
638 static int clone_file_block(ext2_filsys fs,
640 e2_blkcnt_t blockcnt,
641 blk_t ref_block EXT2FS_ATTR((unused)),
642 int ref_offset EXT2FS_ATTR((unused)),
648 struct clone_struct *cs = (struct clone_struct *) priv_data;
654 if (HOLE_BLKADDR(*block_nr))
657 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
658 n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(*block_nr));
660 p = (struct dup_block *) dnode_get(n);
661 retval = ext2fs_new_block(fs, 0, ctx->block_found_map,
664 cs->errcode = retval;
667 if (cs->dir && (blockcnt >= 0)) {
668 retval = ext2fs_set_dir_block(fs->dblist,
669 cs->dir, new_block, blockcnt);
671 cs->errcode = retval;
676 printf("Cloning block %u to %u\n", *block_nr,
679 retval = io_channel_read_blk(fs->io, *block_nr, 1,
682 cs->errcode = retval;
685 retval = io_channel_write_blk(fs->io, new_block, 1,
688 cs->errcode = retval;
691 decrement_badcount(ctx, *block_nr, p);
692 *block_nr = new_block;
693 ext2fs_mark_block_bitmap(ctx->block_found_map,
695 ext2fs_mark_block_bitmap(fs->block_map, new_block);
696 return BLOCK_CHANGED;
698 com_err("clone_file_block", 0,
699 _("internal error; can't find dup_blk for %u\n"),
705 static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
706 struct dup_inode *dp, char* block_buf)
708 ext2_filsys fs = ctx->fs;
710 struct clone_struct cs;
711 struct problem_context pctx;
714 struct inode_el *ino_el;
715 struct dup_block *db;
716 struct dup_inode *di;
718 clear_problem_context(&pctx);
722 retval = ext2fs_get_mem(fs->blocksize, &cs.buf);
726 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, ino))
730 pctx.str = "clone_file";
731 if (ext2fs_inode_has_valid_blocks(&dp->inode))
732 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
733 clone_file_block, &cs);
734 ext2fs_mark_bb_dirty(fs);
736 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
737 retval = pctx.errcode;
741 com_err("clone_file", cs.errcode,
742 _("returned from clone_file_block"));
746 /* The inode may have changed on disk, so we have to re-read it */
747 e2fsck_read_inode(ctx, ino, &dp->inode, "clone file EA");
748 blk = dp->inode.i_file_acl;
749 if (blk && (clone_file_block(fs, &dp->inode.i_file_acl,
750 BLOCK_COUNT_EXTATTR, 0, 0, &cs) ==
752 e2fsck_write_inode(ctx, ino, &dp->inode, "clone file EA");
754 * If we cloned the EA block, find all other inodes
755 * which refered to that EA block, and modify
756 * them to point to the new EA block.
758 n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(blk));
760 com_err("clone_file", 0,
761 _("internal error: couldn't lookup EA "
762 "block record for %u"), blk);
763 retval = 0; /* OK to stumble on... */
766 db = (struct dup_block *) dnode_get(n);
767 for (ino_el = db->inode_list; ino_el; ino_el = ino_el->next) {
768 if (ino_el->inode == ino)
770 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino_el->inode));
772 com_err("clone_file", 0,
773 _("internal error: couldn't lookup EA "
774 "inode record for %u"),
776 retval = 0; /* OK to stumble on... */
779 di = (struct dup_inode *) dnode_get(n);
780 if (di->inode.i_file_acl == blk) {
781 di->inode.i_file_acl = dp->inode.i_file_acl;
782 e2fsck_write_inode(ctx, ino_el->inode,
783 &di->inode, "clone file EA");
784 decrement_badcount(ctx, blk, db);
790 ext2fs_free_mem(&cs.buf);
795 * This routine returns 1 if a block overlaps with one of the superblocks,
796 * group descriptors, inode bitmaps, or block bitmaps.
798 static int check_if_fs_block(e2fsck_t ctx, blk_t test_block)
800 ext2_filsys fs = ctx->fs;
804 first_block = fs->super->s_first_data_block;
805 for (i = 0; i < fs->group_desc_count; i++) {
807 /* Check superblocks/block group descriptors */
808 if (ext2fs_bg_has_super(fs, i)) {
809 if (test_block >= first_block &&
810 (test_block <= first_block + fs->desc_blocks))
814 /* Check the inode table */
815 if ((fs->group_desc[i].bg_inode_table) &&
816 (test_block >= fs->group_desc[i].bg_inode_table) &&
817 (test_block < (fs->group_desc[i].bg_inode_table +
818 fs->inode_blocks_per_group)))
821 /* Check the bitmap blocks */
822 if ((test_block == fs->group_desc[i].bg_block_bitmap) ||
823 (test_block == fs->group_desc[i].bg_inode_bitmap))
826 first_block += fs->super->s_blocks_per_group;