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) {
188 * Free a duplicate block record
190 static void block_dnode_free(dnode_t *node,
191 void *context EXT2FS_ATTR((unused)))
193 struct dup_block *db;
194 struct inode_el *p, *next;
196 db = (struct dup_block *) dnode_get(node);
197 for (p = db->inode_list; p; p = next) {
207 * Main procedure for handling duplicate blocks
209 void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
211 ext2_filsys fs = ctx->fs;
212 struct problem_context pctx;
213 #ifdef RESOURCE_TRACK
214 struct resource_track rtrack;
217 clear_problem_context(&pctx);
219 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
220 _("multiply claimed inode map"), &inode_dup_map);
222 fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
223 ctx->flags |= E2F_FLAG_ABORT;
227 dict_init(&ino_dict, DICTCOUNT_T_MAX, dict_int_cmp);
228 dict_init(&blk_dict, DICTCOUNT_T_MAX, dict_int_cmp);
229 dict_set_allocator(&ino_dict, NULL, inode_dnode_free, NULL);
230 dict_set_allocator(&blk_dict, NULL, block_dnode_free, NULL);
232 init_resource_track(&rtrack, ctx->fs->io);
233 pass1b(ctx, block_buf);
234 print_resource_track(ctx, "Pass 1b", &rtrack, ctx->fs->io);
236 init_resource_track(&rtrack, ctx->fs->io);
237 pass1c(ctx, block_buf);
238 print_resource_track(ctx, "Pass 1c", &rtrack, ctx->fs->io);
240 init_resource_track(&rtrack, ctx->fs->io);
241 pass1d(ctx, block_buf);
242 print_resource_track(ctx, "Pass 1d", &rtrack, ctx->fs->io);
245 * Time to free all of the accumulated data structures that we
246 * don't need anymore.
248 dict_free_nodes(&ino_dict);
249 dict_free_nodes(&blk_dict);
250 ext2fs_free_inode_bitmap(inode_dup_map);
254 * Scan the inodes looking for inodes that contain duplicate blocks.
256 struct process_block_struct {
260 struct ext2_inode *inode;
261 struct problem_context *pctx;
264 static void pass1b(e2fsck_t ctx, char *block_buf)
266 ext2_filsys fs = ctx->fs;
268 struct ext2_inode inode;
269 ext2_inode_scan scan;
270 struct process_block_struct pb;
271 struct problem_context pctx;
273 clear_problem_context(&pctx);
275 if (!(ctx->options & E2F_OPT_PREEN))
276 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
277 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
280 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
281 ctx->flags |= E2F_FLAG_ABORT;
284 ctx->stashed_inode = &inode;
289 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
290 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
293 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
294 ctx->flags |= E2F_FLAG_ABORT;
299 pctx.ino = ctx->stashed_ino = ino;
300 if ((ino != EXT2_BAD_INO) &&
301 !ext2fs_test_inode_bitmap(ctx->inode_used_map, ino))
308 if (ext2fs_inode_has_valid_blocks(&inode) ||
309 (ino == EXT2_BAD_INO))
310 pctx.errcode = ext2fs_block_iterate2(fs, ino,
311 BLOCK_FLAG_READ_ONLY, block_buf,
312 process_pass1b_block, &pb);
313 if (inode.i_file_acl)
314 process_pass1b_block(fs, &inode.i_file_acl,
315 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
317 end_problem_latch(ctx, PR_LATCH_DBLOCK);
318 if (ino >= EXT2_FIRST_INODE(fs->super) ||
319 ino == EXT2_ROOT_INO)
323 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
325 ext2fs_close_inode_scan(scan);
326 e2fsck_use_inode_shortcuts(ctx, 0);
329 static int process_pass1b_block(ext2_filsys fs EXT2FS_ATTR((unused)),
331 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
332 blk_t ref_blk EXT2FS_ATTR((unused)),
333 int ref_offset EXT2FS_ATTR((unused)),
336 struct process_block_struct *p;
339 if (HOLE_BLKADDR(*block_nr))
341 p = (struct process_block_struct *) priv_data;
344 if (!ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr))
347 /* OK, this is a duplicate block */
348 if (p->ino != EXT2_BAD_INO) {
349 p->pctx->blk = *block_nr;
350 fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
353 ext2fs_mark_inode_bitmap(inode_dup_map, p->ino);
355 add_dupe(ctx, p->ino, *block_nr, p->inode);
361 * Pass 1c: Scan directories for inodes with duplicate blocks. This
362 * is used so that we can print pathnames when prompting the user for
365 struct search_dir_struct {
367 ext2_ino_t first_inode;
368 ext2_ino_t max_inode;
371 static int search_dirent_proc(ext2_ino_t dir, int entry,
372 struct ext2_dir_entry *dirent,
373 int offset EXT2FS_ATTR((unused)),
374 int blocksize EXT2FS_ATTR((unused)),
375 char *buf EXT2FS_ATTR((unused)),
378 struct search_dir_struct *sd;
382 sd = (struct search_dir_struct *) priv_data;
384 if (dirent->inode > sd->max_inode)
385 /* Should abort this inode, but not everything */
388 if ((dirent->inode < sd->first_inode) || (entry < DIRENT_OTHER_FILE) ||
389 !ext2fs_test_inode_bitmap(inode_dup_map, dirent->inode))
392 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(dirent->inode));
395 p = (struct dup_inode *) dnode_get(n);
401 return(sd->count ? 0 : DIRENT_ABORT);
405 static void pass1c(e2fsck_t ctx, char *block_buf)
407 ext2_filsys fs = ctx->fs;
408 struct search_dir_struct sd;
409 struct problem_context pctx;
411 clear_problem_context(&pctx);
413 if (!(ctx->options & E2F_OPT_PREEN))
414 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
417 * Search through all directories to translate inodes to names
418 * (by searching for the containing directory for that inode.)
420 sd.count = dup_inode_count - dup_inode_founddir;
421 sd.first_inode = EXT2_FIRST_INODE(fs->super);
422 sd.max_inode = fs->super->s_inodes_count;
423 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
424 search_dirent_proc, &sd);
427 static void pass1d(e2fsck_t ctx, char *block_buf)
429 ext2_filsys fs = ctx->fs;
430 struct dup_inode *p, *t;
432 ext2_ino_t *shared, ino;
437 struct problem_context pctx;
442 clear_problem_context(&pctx);
444 if (!(ctx->options & E2F_OPT_PREEN))
445 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
446 e2fsck_read_bitmaps(ctx);
448 pctx.num = dup_inode_count; /* dict_count(&ino_dict); */
449 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
450 shared = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
451 sizeof(ext2_ino_t) * dict_count(&ino_dict),
452 "Shared inode list");
453 for (n = dict_first(&ino_dict); n; n = dict_next(&ino_dict, n)) {
454 p = (struct dup_inode *) dnode_get(n);
457 ino = (ext2_ino_t)VOIDPTR_TO_INT(dnode_getkey(n));
458 if (ino == EXT2_BAD_INO || ino == EXT2_RESIZE_INO)
462 * Find all of the inodes which share blocks with this
463 * one. First we find all of the duplicate blocks
464 * belonging to this inode, and then search each block
465 * get the list of inodes, and merge them together.
467 for (s = p->block_list; s; s = s->next) {
468 m = dict_lookup(&blk_dict, INT_TO_VOIDPTR(s->block));
470 continue; /* Should never happen... */
471 q = (struct dup_block *) dnode_get(m);
474 if (check_if_fs_block(ctx, s->block)) {
480 * Add all inodes used by this block to the
481 * shared[] --- which is a unique list, so
482 * if an inode is already in shared[], don't
485 for (r = q->inode_list; r; r = r->next) {
488 for (i = 0; i < shared_len; i++)
489 if (shared[i] == r->inode)
491 if (i == shared_len) {
492 shared[shared_len++] = r->inode;
498 * Report the inode that we are working on
500 pctx.inode = &p->inode;
503 pctx.blkcount = p->num_dupblocks;
504 pctx.num = meta_data ? shared_len+1 : shared_len;
505 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
510 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
512 for (i = 0; i < shared_len; i++) {
513 m = dict_lookup(&ino_dict, INT_TO_VOIDPTR(shared[i]));
515 continue; /* should never happen */
516 t = (struct dup_inode *) dnode_get(m);
518 * Report the inode that we are sharing with
520 pctx.inode = &t->inode;
521 pctx.ino = shared[i];
523 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
526 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
529 if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
530 pctx.errcode = clone_file(ctx, ino, p, block_buf);
532 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
536 if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
537 delete_file(ctx, ino, p, block_buf);
539 ext2fs_unmark_valid(fs);
541 ext2fs_free_mem(&shared);
545 * Drop the refcount on the dup_block structure, and clear the entry
546 * in the block_dup_map if appropriate.
548 static void decrement_badcount(e2fsck_t ctx, blk_t block, struct dup_block *p)
551 if (p->num_bad <= 0 ||
552 (p->num_bad == 1 && !check_if_fs_block(ctx, block)))
553 ext2fs_unmark_block_bitmap(ctx->block_dup_map, block);
556 static int delete_file_block(ext2_filsys fs,
558 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
559 blk_t ref_block EXT2FS_ATTR((unused)),
560 int ref_offset EXT2FS_ATTR((unused)),
563 struct process_block_struct *pb;
568 pb = (struct process_block_struct *) priv_data;
571 if (HOLE_BLKADDR(*block_nr))
574 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
575 n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(*block_nr));
577 p = (struct dup_block *) dnode_get(n);
578 decrement_badcount(ctx, *block_nr, p);
580 com_err("delete_file_block", 0,
581 _("internal error: can't find dup_blk for %u\n"),
584 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
585 ext2fs_block_alloc_stats(fs, *block_nr, -1);
591 static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
592 struct dup_inode *dp, char* block_buf)
594 ext2_filsys fs = ctx->fs;
595 struct process_block_struct pb;
596 struct ext2_inode inode;
597 struct problem_context pctx;
600 clear_problem_context(&pctx);
601 pctx.ino = pb.ino = ino;
602 pb.dup_blocks = dp->num_dupblocks;
604 pctx.str = "delete_file";
606 e2fsck_read_inode(ctx, ino, &inode, "delete_file");
607 if (ext2fs_inode_has_valid_blocks(&inode))
608 pctx.errcode = ext2fs_block_iterate2(fs, ino, BLOCK_FLAG_READ_ONLY,
609 block_buf, delete_file_block, &pb);
611 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
612 if (ctx->inode_bad_map)
613 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
614 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
616 /* Inode may have changed by block_iterate, so reread it */
617 e2fsck_read_inode(ctx, ino, &inode, "delete_file");
618 e2fsck_clear_inode(ctx, ino, &inode, 0, "delete_file");
619 if (inode.i_file_acl &&
620 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
622 pctx.errcode = ext2fs_adjust_ea_refcount(fs, inode.i_file_acl,
623 block_buf, -1, &count);
624 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
629 pctx.blk = inode.i_file_acl;
630 fix_problem(ctx, PR_1B_ADJ_EA_REFCOUNT, &pctx);
633 * If the count is zero, then arrange to have the
634 * block deleted. If the block is in the block_dup_map,
635 * also call delete_file_block since it will take care
636 * of keeping the accounting straight.
639 ext2fs_test_block_bitmap(ctx->block_dup_map,
641 delete_file_block(fs, &inode.i_file_acl,
642 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
646 struct clone_struct {
653 static int clone_file_block(ext2_filsys fs,
655 e2_blkcnt_t blockcnt,
656 blk_t ref_block EXT2FS_ATTR((unused)),
657 int ref_offset EXT2FS_ATTR((unused)),
663 struct clone_struct *cs = (struct clone_struct *) priv_data;
669 if (HOLE_BLKADDR(*block_nr))
672 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
673 n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(*block_nr));
675 p = (struct dup_block *) dnode_get(n);
676 retval = ext2fs_new_block(fs, 0, ctx->block_found_map,
679 cs->errcode = retval;
682 if (cs->dir && (blockcnt >= 0)) {
683 retval = ext2fs_set_dir_block(fs->dblist,
684 cs->dir, new_block, blockcnt);
686 cs->errcode = retval;
691 printf("Cloning block %u to %u\n", *block_nr,
694 retval = io_channel_read_blk(fs->io, *block_nr, 1,
697 cs->errcode = retval;
700 retval = io_channel_write_blk(fs->io, new_block, 1,
703 cs->errcode = retval;
706 decrement_badcount(ctx, *block_nr, p);
707 *block_nr = new_block;
708 ext2fs_mark_block_bitmap(ctx->block_found_map,
710 ext2fs_mark_block_bitmap(fs->block_map, new_block);
711 return BLOCK_CHANGED;
713 com_err("clone_file_block", 0,
714 _("internal error: can't find dup_blk for %u\n"),
720 static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
721 struct dup_inode *dp, char* block_buf)
723 ext2_filsys fs = ctx->fs;
725 struct clone_struct cs;
726 struct problem_context pctx;
729 struct inode_el *ino_el;
730 struct dup_block *db;
731 struct dup_inode *di;
733 clear_problem_context(&pctx);
737 retval = ext2fs_get_mem(fs->blocksize, &cs.buf);
741 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, ino))
745 pctx.str = "clone_file";
746 if (ext2fs_inode_has_valid_blocks(&dp->inode))
747 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
748 clone_file_block, &cs);
749 ext2fs_mark_bb_dirty(fs);
751 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
752 retval = pctx.errcode;
756 com_err("clone_file", cs.errcode,
757 _("returned from clone_file_block"));
761 /* The inode may have changed on disk, so we have to re-read it */
762 e2fsck_read_inode(ctx, ino, &dp->inode, "clone file EA");
763 blk = dp->inode.i_file_acl;
764 if (blk && (clone_file_block(fs, &dp->inode.i_file_acl,
765 BLOCK_COUNT_EXTATTR, 0, 0, &cs) ==
767 e2fsck_write_inode(ctx, ino, &dp->inode, "clone file EA");
769 * If we cloned the EA block, find all other inodes
770 * which refered to that EA block, and modify
771 * them to point to the new EA block.
773 n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(blk));
775 com_err("clone_file", 0,
776 _("internal error: couldn't lookup EA "
777 "block record for %u"), blk);
778 retval = 0; /* OK to stumble on... */
781 db = (struct dup_block *) dnode_get(n);
782 for (ino_el = db->inode_list; ino_el; ino_el = ino_el->next) {
783 if (ino_el->inode == ino)
785 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino_el->inode));
787 com_err("clone_file", 0,
788 _("internal error: couldn't lookup EA "
789 "inode record for %u"),
791 retval = 0; /* OK to stumble on... */
794 di = (struct dup_inode *) dnode_get(n);
795 if (di->inode.i_file_acl == blk) {
796 di->inode.i_file_acl = dp->inode.i_file_acl;
797 e2fsck_write_inode(ctx, ino_el->inode,
798 &di->inode, "clone file EA");
799 decrement_badcount(ctx, blk, db);
805 ext2fs_free_mem(&cs.buf);
810 * This routine returns 1 if a block overlaps with one of the superblocks,
811 * group descriptors, inode bitmaps, or block bitmaps.
813 static int check_if_fs_block(e2fsck_t ctx, blk_t test_block)
815 ext2_filsys fs = ctx->fs;
819 first_block = fs->super->s_first_data_block;
820 for (i = 0; i < fs->group_desc_count; i++) {
822 /* Check superblocks/block group descriptors */
823 if (ext2fs_bg_has_super(fs, i)) {
824 if (test_block >= first_block &&
825 (test_block <= first_block + fs->desc_blocks))
829 /* Check the inode table */
830 if ((fs->group_desc[i].bg_inode_table) &&
831 (test_block >= fs->group_desc[i].bg_inode_table) &&
832 (test_block < (fs->group_desc[i].bg_inode_table +
833 fs->inode_blocks_per_group)))
836 /* Check the bitmap blocks */
837 if ((test_block == fs->group_desc[i].bg_block_bitmap) ||
838 (test_block == fs->group_desc[i].bg_inode_bitmap))
841 first_block += fs->super->s_blocks_per_group;