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 e2_blkcnt_t blockcnt, blk_t ref_blk,
92 int ref_offset, void *priv_data);
93 static void delete_file(e2fsck_t ctx, struct dup_inode *dp,
95 static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf);
96 static int check_if_fs_block(e2fsck_t ctx, blk_t test_blk);
98 static void pass1b(e2fsck_t ctx, char *block_buf);
99 static void pass1c(e2fsck_t ctx, char *block_buf);
100 static void pass1d(e2fsck_t ctx, char *block_buf);
102 static struct dup_block *dup_blk = 0;
103 static struct dup_inode *dup_ino = 0;
104 static int dup_inode_count = 0;
106 static ext2fs_inode_bitmap inode_dup_map;
109 * Main procedure for handling duplicate blocks
111 void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
113 ext2_filsys fs = ctx->fs;
114 struct dup_block *p, *q, *next_p, *next_q;
115 struct dup_inode *r, *next_r;
116 struct problem_context pctx;
118 clear_problem_context(&pctx);
120 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
121 _("multiply claimed inode map"), &inode_dup_map);
123 fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
124 ctx->flags |= E2F_FLAG_ABORT;
128 pass1b(ctx, block_buf);
129 pass1c(ctx, block_buf);
130 pass1d(ctx, block_buf);
133 * Time to free all of the accumulated data structures that we
134 * don't need anymore.
136 ext2fs_free_inode_bitmap(inode_dup_map); inode_dup_map = 0;
137 ext2fs_free_block_bitmap(ctx->block_dup_map); ctx->block_dup_map = 0;
138 for (p = dup_blk; p; p = next_p) {
139 next_p = p->next_block;
140 for (q = p; q; q = next_q) {
141 next_q = q->next_inode;
142 ext2fs_free_mem((void **) &q);
145 for (r = dup_ino; r; r = next_r) {
147 ext2fs_free_mem((void **) &r);
152 * Scan the inodes looking for inodes that contain duplicate blocks.
154 struct process_block_struct {
158 struct problem_context *pctx;
161 static void pass1b(e2fsck_t ctx, char *block_buf)
163 ext2_filsys fs = ctx->fs;
165 struct ext2_inode inode;
166 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;
192 pctx.ino = ctx->stashed_ino = ino;
193 if ((ino != EXT2_BAD_INO) &&
194 (!ext2fs_test_inode_bitmap(ctx->inode_used_map, ino) ||
195 !ext2fs_inode_has_valid_blocks(&inode)))
200 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
201 process_pass1b_block, &pb);
203 end_problem_latch(ctx, PR_LATCH_DBLOCK);
204 dp = (struct dup_inode *) e2fsck_allocate_memory(ctx,
205 sizeof(struct dup_inode),
206 "duplicate inode record");
210 dp->num_dupblocks = pb.dup_blocks;
213 if (ino != EXT2_BAD_INO)
217 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
219 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
220 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
223 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
224 ctx->flags |= E2F_FLAG_ABORT;
228 ext2fs_close_inode_scan(scan);
230 fs->check_directory = 0;
233 static int process_pass1b_block(ext2_filsys fs,
235 e2_blkcnt_t blockcnt,
240 struct process_block_struct *p;
241 struct dup_block *dp, *q, *r;
245 if (HOLE_BLKADDR(*block_nr))
247 p = (struct process_block_struct *) priv_data;
250 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
251 /* OK, this is a duplicate block */
252 if (p->ino != EXT2_BAD_INO) {
253 p->pctx->blk = *block_nr;
254 fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
257 ext2fs_mark_block_bitmap(ctx->block_dup_map, *block_nr);
258 ext2fs_mark_inode_bitmap(inode_dup_map, p->ino);
259 dp = (struct dup_block *) e2fsck_allocate_memory(ctx,
260 sizeof(struct dup_block),
261 "duplicate block record");
262 dp->block = *block_nr;
267 if (q->block == *block_nr)
272 dp->next_inode = q->next_inode;
275 dp->next_block = dup_blk;
280 * Set the num_bad field
282 for (q = dup_blk; q; q = q->next_block) {
284 for (r = q; r; r = r->next_inode)
292 * Pass 1c: Scan directories for inodes with duplicate blocks. This
293 * is used so that we can print pathnames when prompting the user for
296 struct search_dir_struct {
302 static int search_dirent_proc(ino_t dir, int entry,
303 struct ext2_dir_entry *dirent,
304 int offset, int blocksize,
305 char *buf, void *priv_data)
307 struct search_dir_struct *sd;
310 sd = (struct search_dir_struct *) priv_data;
312 if (dirent->inode > sd->max_inode)
313 /* Should abort this inode, but not everything */
316 if (!dirent->inode || (entry < DIRENT_OTHER_FILE) ||
317 !ext2fs_test_inode_bitmap(inode_dup_map, dirent->inode))
320 for (p = dup_ino; p; p = p->next) {
321 if ((p->ino >= sd->first_inode) &&
322 (p->ino == dirent->inode))
332 return(sd->count ? 0 : DIRENT_ABORT);
336 static void pass1c(e2fsck_t ctx, char *block_buf)
338 ext2_filsys fs = ctx->fs;
340 int inodes_left = dup_inode_count;
341 struct search_dir_struct sd;
342 struct problem_context pctx;
344 clear_problem_context(&pctx);
346 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
349 * First check to see if any of the inodes with dup blocks is
350 * a special inode. (Note that the bad block inode isn't
353 for (p = dup_ino; p; p = p->next) {
354 if ((p->ino < EXT2_FIRST_INODE(fs->super)) &&
355 (p->ino != EXT2_BAD_INO))
360 * Search through all directories to translate inodes to names
361 * (by searching for the containing directory for that inode.)
363 sd.count = inodes_left;
364 sd.first_inode = EXT2_FIRST_INODE(fs->super);
365 sd.max_inode = fs->super->s_inodes_count;
366 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
367 search_dirent_proc, &sd);
370 static void pass1d(e2fsck_t ctx, char *block_buf)
372 ext2_filsys fs = ctx->fs;
373 struct dup_inode *p, *s;
374 struct dup_block *q, *r;
380 struct problem_context pctx;
382 clear_problem_context(&pctx);
384 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
385 e2fsck_read_bitmaps(ctx);
387 pctx.num = dup_inode_count;
388 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
389 shared = (ino_t *) e2fsck_allocate_memory(ctx,
390 sizeof(ino_t) * dup_inode_count,
391 "Shared inode list");
392 for (p = dup_ino; p; p = p->next) {
395 if (p->ino == EXT2_BAD_INO)
399 * Search through the duplicate records to see which
400 * inodes share blocks with this one
402 for (q = dup_blk; q; q = q->next_block) {
404 * See if this block is used by this inode.
405 * If it isn't, continue.
407 for (r = q; r; r = r->next_inode)
408 if (r->ino == p->ino)
414 if (check_if_fs_block(ctx, q->block)) {
420 * Add all inodes used by this block to the
421 * shared[] --- which is a unique list, so
422 * if an inode is already in shared[], don't
425 for (r = q; r; r = r->next_inode) {
426 if (r->ino == p->ino)
428 for (i = 0; i < shared_len; i++)
429 if (shared[i] == r->ino)
431 if (i == shared_len) {
432 shared[shared_len++] = r->ino;
438 * Report the inode that we are working on
440 pctx.inode = &p->inode;
443 pctx.blkcount = p->num_dupblocks;
444 pctx.num = meta_data ? shared_len+1 : shared_len;
445 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
450 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
452 for (i = 0; i < shared_len; i++) {
453 for (s = dup_ino; s; s = s->next)
454 if (s->ino == shared[i])
459 * Report the inode that we are sharing with
461 pctx.inode = &s->inode;
464 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
467 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
470 if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
471 pctx.errcode = clone_file(ctx, p, block_buf);
473 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
477 if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
478 delete_file(ctx, p, block_buf);
480 ext2fs_unmark_valid(fs);
482 ext2fs_free_mem((void **) &shared);
485 static int delete_file_block(ext2_filsys fs,
487 e2_blkcnt_t blockcnt,
492 struct process_block_struct *pb;
496 pb = (struct process_block_struct *) priv_data;
499 if (HOLE_BLKADDR(*block_nr))
502 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
503 for (p = dup_blk; p; p = p->next_block)
504 if (p->block == *block_nr)
509 ext2fs_unmark_block_bitmap(ctx->block_dup_map,
512 com_err("delete_file_block", 0,
513 _("internal error; can't find dup_blk for %d\n"),
516 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
517 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
523 static void delete_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
525 ext2_filsys fs = ctx->fs;
526 struct process_block_struct pb;
527 struct ext2_inode inode;
528 struct problem_context pctx;
530 clear_problem_context(&pctx);
531 pctx.ino = pb.ino = dp->ino;
532 pb.dup_blocks = dp->num_dupblocks;
534 pctx.str = "delete_file";
536 pctx.errcode = ext2fs_block_iterate2(fs, dp->ino, 0, block_buf,
537 delete_file_block, &pb);
539 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
540 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, dp->ino);
541 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, dp->ino);
542 if (ctx->inode_bad_map)
543 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, dp->ino);
544 ext2fs_unmark_inode_bitmap(fs->inode_map, dp->ino);
545 ext2fs_mark_ib_dirty(fs);
546 ext2fs_mark_bb_dirty(fs);
547 e2fsck_read_inode(ctx, dp->ino, &inode, "delete_file");
548 inode.i_links_count = 0;
549 inode.i_dtime = time(0);
550 e2fsck_write_inode(ctx, dp->ino, &inode, "delete_file");
553 struct clone_struct {
560 static int clone_file_block(ext2_filsys fs,
562 e2_blkcnt_t blockcnt,
570 struct clone_struct *cs = (struct clone_struct *) priv_data;
575 if (HOLE_BLKADDR(*block_nr))
578 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
579 for (p = dup_blk; p; p = p->next_block)
580 if (p->block == *block_nr)
583 retval = ext2fs_new_block(fs, 0, ctx->block_found_map,
586 cs->errcode = retval;
590 retval = ext2fs_set_dir_block(fs->dblist,
591 cs->dir, new_block, blockcnt);
593 cs->errcode = retval;
597 retval = io_channel_read_blk(fs->io, *block_nr, 1,
600 cs->errcode = retval;
603 retval = io_channel_write_blk(fs->io, new_block, 1,
606 cs->errcode = retval;
610 if (p->num_bad == 1 &&
611 !check_if_fs_block(ctx, *block_nr))
612 ext2fs_unmark_block_bitmap(ctx->block_dup_map,
614 *block_nr = new_block;
615 ext2fs_mark_block_bitmap(ctx->block_found_map,
617 ext2fs_mark_block_bitmap(fs->block_map, new_block);
618 return BLOCK_CHANGED;
620 com_err("clone_file_block", 0,
621 _("internal error; can't find dup_blk for %d\n"),
627 static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
629 ext2_filsys fs = ctx->fs;
631 struct clone_struct cs;
632 struct problem_context pctx;
634 clear_problem_context(&pctx);
638 retval = ext2fs_get_mem(fs->blocksize, (void **) &cs.buf);
642 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dp->ino))
646 pctx.str = "clone_file";
647 pctx.errcode = ext2fs_block_iterate2(fs, dp->ino, 0, block_buf,
648 clone_file_block, &cs);
649 ext2fs_mark_bb_dirty(fs);
650 ext2fs_free_mem((void **) &cs.buf);
652 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
656 com_err("clone_file", cs.errcode,
657 _("returned from clone_file_block"));
664 * This routine returns 1 if a block overlaps with one of the superblocks,
665 * group descriptors, inode bitmaps, or block bitmaps.
667 static int check_if_fs_block(e2fsck_t ctx, blk_t test_block)
669 ext2_filsys fs = ctx->fs;
673 block = fs->super->s_first_data_block;
674 for (i = 0; i < fs->group_desc_count; i++) {
676 /* Check superblocks/block group descriptros */
677 if (ext2fs_bg_has_super(fs, i)) {
678 if (test_block >= block &&
679 (test_block <= block + fs->desc_blocks))
683 /* Check the inode table */
684 if ((fs->group_desc[i].bg_inode_table) &&
685 (test_block >= fs->group_desc[i].bg_inode_table) &&
686 (test_block < (fs->group_desc[i].bg_inode_table +
687 fs->inode_blocks_per_group)))
690 /* Check the bitmap blocks */
691 if ((test_block == fs->group_desc[i].bg_block_bitmap) ||
692 (test_block == fs->group_desc[i].bg_inode_bitmap))
695 block += fs->super->s_blocks_per_group;