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 #include <et/com_err.h>
41 * This is structure is allocated for each time that a block is
42 * claimed by more than one file. So if a particular block is claimed
43 * by 3 files, then three copies of this structure will be allocated,
44 * one for each conflict.
46 * The linked list structure is as follows:
48 * dup_blk --> block #34 --> block #35 --> block #47
49 * inode #12 inode #14 inode #17
50 * num_bad = 3 num_bad = 2 num_bad = 2
53 * block #34 block #35 block #47
54 * inode #14 inode #15 inode #23
60 * The num_bad field indicates how many inodes are sharing a
61 * particular block, and is only stored in the first element of the
62 * linked list for a particular block. As the block conflicts are
63 * resolved, num_bad is decremented; when it reaches 1, then we no
64 * longer need to worry about that block.
67 blk_t block; /* Block number */
68 ino_t ino; /* Inode number */
70 /* Pointer to next dup record with different block */
71 struct dup_block *next_block;
72 /* Pointer to next dup record with different inode */
73 struct dup_block *next_inode;
77 * This structure stores information about a particular inode which
78 * is sharing blocks with other inodes. This information is collected
79 * to display to the user, so that the user knows what files he or she
80 * is dealing with, when trying to decide how to resolve the conflict
81 * of multiply-claimed blocks.
86 struct ext2_inode inode;
87 struct dup_inode *next;
90 static int process_pass1b_block(ext2_filsys fs, blk_t *blocknr,
91 int blockcnt, void *priv_data);
92 static void delete_file(e2fsck_t ctx, struct dup_inode *dp,
94 static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf);
95 static int check_if_fs_block(e2fsck_t ctx, blk_t test_blk);
97 static void pass1b(e2fsck_t ctx, char *block_buf);
98 static void pass1c(e2fsck_t ctx, char *block_buf);
99 static void pass1d(e2fsck_t ctx, char *block_buf);
101 static struct dup_block *dup_blk = 0;
102 static struct dup_inode *dup_ino = 0;
103 static int dup_inode_count = 0;
105 static ext2fs_inode_bitmap inode_dup_map;
108 * Main procedure for handling duplicate blocks
110 void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
112 ext2_filsys fs = ctx->fs;
113 struct dup_block *p, *q, *next_p, *next_q;
114 struct dup_inode *r, *next_r;
115 struct problem_context pctx;
117 clear_problem_context(&pctx);
119 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
120 _("multiply claimed inode map"), &inode_dup_map);
122 fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
123 ctx->flags |= E2F_FLAG_ABORT;
127 pass1b(ctx, block_buf);
128 pass1c(ctx, block_buf);
129 pass1d(ctx, block_buf);
132 * Time to free all of the accumulated data structures that we
133 * don't need anymore.
135 ext2fs_free_inode_bitmap(inode_dup_map); inode_dup_map = 0;
136 ext2fs_free_block_bitmap(ctx->block_dup_map); ctx->block_dup_map = 0;
137 for (p = dup_blk; p; p = next_p) {
138 next_p = p->next_block;
139 for (q = p; q; q = next_q) {
140 next_q = q->next_inode;
141 ext2fs_free_mem((void **) &q);
144 for (r = dup_ino; r; r = next_r) {
146 ext2fs_free_mem((void **) &r);
151 * Scan the inodes looking for inodes that contain duplicate blocks.
153 struct process_block_struct {
157 struct problem_context *pctx;
160 static void pass1b(e2fsck_t ctx, char *block_buf)
162 ext2_filsys fs = ctx->fs;
164 struct ext2_inode inode;
165 ext2_inode_scan scan;
167 struct process_block_struct pb;
168 struct dup_inode *dp;
169 struct problem_context pctx;
171 clear_problem_context(&pctx);
173 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
174 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
177 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
178 ctx->flags |= E2F_FLAG_ABORT;
181 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
183 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
184 ctx->flags |= E2F_FLAG_ABORT;
187 ctx->stashed_inode = &inode;
191 pctx.ino = ctx->stashed_ino = ino;
192 if ((ino != EXT2_BAD_INO) &&
193 (!ext2fs_test_inode_bitmap(ctx->inode_used_map, ino) ||
194 !ext2fs_inode_has_valid_blocks(&inode)))
199 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
200 process_pass1b_block, &pb);
202 end_problem_latch(ctx, PR_LATCH_DBLOCK);
203 dp = (struct dup_inode *) e2fsck_allocate_memory(ctx,
204 sizeof(struct dup_inode),
205 "duplicate inode record");
209 dp->num_dupblocks = pb.dup_blocks;
212 if (ino != EXT2_BAD_INO)
216 com_err(ctx->program_name, retval,
217 _("while calling ext2fs_block_iterate in pass1b"));
220 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
221 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
224 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
225 ctx->flags |= E2F_FLAG_ABORT;
229 ext2fs_close_inode_scan(scan);
231 fs->check_directory = 0;
234 int process_pass1b_block(ext2_filsys fs,
239 struct process_block_struct *p;
240 struct dup_block *dp, *q, *r;
244 if (HOLE_BLKADDR(*block_nr))
246 p = (struct process_block_struct *) priv_data;
249 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
250 /* OK, this is a duplicate block */
251 if (p->ino != EXT2_BAD_INO) {
252 p->pctx->blk = *block_nr;
253 fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
256 ext2fs_mark_block_bitmap(ctx->block_dup_map, *block_nr);
257 ext2fs_mark_inode_bitmap(inode_dup_map, p->ino);
258 dp = (struct dup_block *) e2fsck_allocate_memory(ctx,
259 sizeof(struct dup_block),
260 "duplicate block record");
261 dp->block = *block_nr;
266 if (q->block == *block_nr)
271 dp->next_inode = q->next_inode;
274 dp->next_block = dup_blk;
279 * Set the num_bad field
281 for (q = dup_blk; q; q = q->next_block) {
283 for (r = q; r; r = r->next_inode)
291 * Pass 1c: Scan directories for inodes with duplicate blocks. This
292 * is used so that we can print pathnames when prompting the user for
295 struct search_dir_struct {
301 static int search_dirent_proc(ino_t dir, int entry,
302 struct ext2_dir_entry *dirent,
303 int offset, int blocksize,
304 char *buf, void *priv_data)
306 struct search_dir_struct *sd;
309 sd = (struct search_dir_struct *) priv_data;
311 if (dirent->inode > sd->max_inode)
312 /* Should abort this inode, but not everything */
315 if (!dirent->inode || (entry < DIRENT_OTHER_FILE) ||
316 !ext2fs_test_inode_bitmap(inode_dup_map, dirent->inode))
319 for (p = dup_ino; p; p = p->next) {
320 if ((p->ino >= sd->first_inode) &&
321 (p->ino == dirent->inode))
331 return(sd->count ? 0 : DIRENT_ABORT);
335 static void pass1c(e2fsck_t ctx, char *block_buf)
337 ext2_filsys fs = ctx->fs;
339 int inodes_left = dup_inode_count;
340 struct search_dir_struct sd;
341 struct problem_context pctx;
343 clear_problem_context(&pctx);
345 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
348 * First check to see if any of the inodes with dup blocks is
349 * a special inode. (Note that the bad block inode isn't
352 for (p = dup_ino; p; p = p->next) {
353 if ((p->ino < EXT2_FIRST_INODE(fs->super)) &&
354 (p->ino != EXT2_BAD_INO))
359 * Search through all directories to translate inodes to names
360 * (by searching for the containing directory for that inode.)
362 sd.count = inodes_left;
363 sd.first_inode = EXT2_FIRST_INODE(fs->super);
364 sd.max_inode = fs->super->s_inodes_count;
365 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
366 search_dirent_proc, &sd);
369 static void pass1d(e2fsck_t ctx, char *block_buf)
371 ext2_filsys fs = ctx->fs;
372 struct dup_inode *p, *s;
373 struct dup_block *q, *r;
379 struct problem_context pctx;
381 clear_problem_context(&pctx);
383 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
384 e2fsck_read_bitmaps(ctx);
386 pctx.num = dup_inode_count;
387 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
388 shared = (ino_t *) e2fsck_allocate_memory(ctx,
389 sizeof(ino_t) * dup_inode_count,
390 "Shared inode list");
391 for (p = dup_ino; p; p = p->next) {
394 if (p->ino == EXT2_BAD_INO)
398 * Search through the duplicate records to see which
399 * inodes share blocks with this one
401 for (q = dup_blk; q; q = q->next_block) {
403 * See if this block is used by this inode.
404 * If it isn't, continue.
406 for (r = q; r; r = r->next_inode)
407 if (r->ino == p->ino)
413 if (check_if_fs_block(ctx, q->block)) {
419 * Add all inodes used by this block to the
420 * shared[] --- which is a unique list, so
421 * if an inode is already in shared[], don't
424 for (r = q; r; r = r->next_inode) {
425 if (r->ino == p->ino)
427 for (i = 0; i < shared_len; i++)
428 if (shared[i] == r->ino)
430 if (i == shared_len) {
431 shared[shared_len++] = r->ino;
437 * Report the inode that we are working on
439 pctx.inode = &p->inode;
442 pctx.blkcount = p->num_dupblocks;
443 pctx.num = meta_data ? shared_len+1 : shared_len;
444 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
449 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
451 for (i = 0; i < shared_len; i++) {
452 for (s = dup_ino; s; s = s->next)
453 if (s->ino == shared[i])
458 * Report the inode that we are sharing with
460 pctx.inode = &s->inode;
463 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
466 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
469 if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
470 pctx.errcode = clone_file(ctx, p, block_buf);
472 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
476 if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
477 delete_file(ctx, p, block_buf);
479 ext2fs_unmark_valid(fs);
481 ext2fs_free_mem((void **) &shared);
484 static int delete_file_block(ext2_filsys fs,
489 struct process_block_struct *pb;
493 pb = (struct process_block_struct *) priv_data;
496 if (HOLE_BLKADDR(*block_nr))
499 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
500 for (p = dup_blk; p; p = p->next_block)
501 if (p->block == *block_nr)
506 ext2fs_unmark_block_bitmap(ctx->block_dup_map,
509 com_err("delete_file_block", 0,
510 _("internal error; can't find dup_blk for %d\n"),
513 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
514 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
520 static void delete_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
522 ext2_filsys fs = ctx->fs;
524 struct process_block_struct pb;
525 struct ext2_inode inode;
528 pb.dup_blocks = dp->num_dupblocks;
531 retval = ext2fs_block_iterate(fs, dp->ino, 0, block_buf,
532 delete_file_block, &pb);
534 com_err("delete_file", retval,
535 _("while calling ext2fs_block_iterate for inode %d"),
537 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, dp->ino);
538 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, dp->ino);
539 if (ctx->inode_bad_map)
540 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, dp->ino);
541 ext2fs_unmark_inode_bitmap(fs->inode_map, dp->ino);
542 ext2fs_mark_ib_dirty(fs);
543 ext2fs_mark_bb_dirty(fs);
544 e2fsck_read_inode(ctx, dp->ino, &inode, "delete_file");
545 inode.i_links_count = 0;
546 inode.i_dtime = time(0);
547 e2fsck_write_inode(ctx, dp->ino, &inode, "delete_file");
550 struct clone_struct {
557 static int clone_file_block(ext2_filsys fs,
565 struct clone_struct *cs = (struct clone_struct *) priv_data;
570 if (HOLE_BLKADDR(*block_nr))
573 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
574 for (p = dup_blk; p; p = p->next_block)
575 if (p->block == *block_nr)
578 retval = ext2fs_new_block(fs, 0, ctx->block_found_map,
581 cs->errcode = retval;
585 retval = ext2fs_set_dir_block(fs->dblist,
586 cs->dir, new_block, blockcnt);
588 cs->errcode = retval;
592 retval = io_channel_read_blk(fs->io, *block_nr, 1,
595 cs->errcode = retval;
598 retval = io_channel_write_blk(fs->io, new_block, 1,
601 cs->errcode = retval;
605 if (p->num_bad == 1 &&
606 !check_if_fs_block(ctx, *block_nr))
607 ext2fs_unmark_block_bitmap(ctx->block_dup_map,
609 *block_nr = new_block;
610 ext2fs_mark_block_bitmap(ctx->block_found_map,
612 ext2fs_mark_block_bitmap(fs->block_map, new_block);
613 return BLOCK_CHANGED;
615 com_err("clone_file_block", 0,
616 _("internal error; can't find dup_blk for %d\n"),
622 static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
624 ext2_filsys fs = ctx->fs;
626 struct clone_struct cs;
631 retval = ext2fs_get_mem(fs->blocksize, (void **) &cs.buf);
635 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dp->ino))
638 retval = ext2fs_block_iterate(fs, dp->ino, 0, block_buf,
639 clone_file_block, &cs);
640 ext2fs_mark_bb_dirty(fs);
641 ext2fs_free_mem((void **) &cs.buf);
643 com_err("clone_file", retval,
644 _("while calling ext2fs_block_iterate for inode %d"),
649 com_err("clone_file", cs.errcode,
650 _("returned from clone_file_block"));
657 * This routine returns 1 if a block overlaps with one of the superblocks,
658 * group descriptors, inode bitmaps, or block bitmaps.
660 static int check_if_fs_block(e2fsck_t ctx, blk_t test_block)
662 ext2_filsys fs = ctx->fs;
666 block = fs->super->s_first_data_block;
667 for (i = 0; i < fs->group_desc_count; i++) {
669 /* Check superblocks/block group descriptros */
670 if (ext2fs_bg_has_super(fs, i)) {
671 if (test_block >= block &&
672 (test_block <= block + fs->desc_blocks))
676 /* Check the inode table */
677 if ((fs->group_desc[i].bg_inode_table) &&
678 (test_block >= fs->group_desc[i].bg_inode_table) &&
679 (test_block < (fs->group_desc[i].bg_inode_table +
680 fs->inode_blocks_per_group)))
683 /* Check the bitmap blocks */
684 if ((test_block == fs->group_desc[i].bg_block_bitmap) ||
685 (test_block == fs->group_desc[i].bg_inode_bitmap))
688 block += fs->super->s_blocks_per_group;