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>
40 /* Define an extension to the ext2 library's block count information */
41 #define BLOCK_COUNT_EXTATTR (-5)
44 * This is structure is allocated for each time that a block is
45 * claimed by more than one file. So if a particular block is claimed
46 * by 3 files, then three copies of this structure will be allocated,
47 * one for each conflict.
49 * The linked list structure is as follows:
51 * dup_blk --> block #34 --> block #35 --> block #47
52 * inode #12 inode #14 inode #17
53 * num_bad = 3 num_bad = 2 num_bad = 2
56 * block #34 block #35 block #47
57 * inode #14 inode #15 inode #23
63 * The num_bad field indicates how many inodes are sharing a
64 * particular block, and is only stored in the first element of the
65 * linked list for a particular block. As the block conflicts are
66 * resolved, num_bad is decremented; when it reaches 1, then we no
67 * longer need to worry about that block.
70 blk_t block; /* Block number */
71 ext2_ino_t ino; /* Inode number */
74 /* Pointer to next dup record with different block */
75 struct dup_block *next_block;
76 /* Pointer to next dup record with different inode */
77 struct dup_block *next_inode;
80 #define FLAG_EXTATTR (1)
83 * This structure stores information about a particular inode which
84 * is sharing blocks with other inodes. This information is collected
85 * to display to the user, so that the user knows what files he or she
86 * is dealing with, when trying to decide how to resolve the conflict
87 * of multiply-claimed blocks.
92 struct ext2_inode inode;
93 struct dup_inode *next;
96 static int process_pass1b_block(ext2_filsys fs, blk_t *blocknr,
97 e2_blkcnt_t blockcnt, blk_t ref_blk,
98 int ref_offset, void *priv_data);
99 static void delete_file(e2fsck_t ctx, struct dup_inode *dp,
101 static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf);
102 static int check_if_fs_block(e2fsck_t ctx, blk_t test_blk);
104 static void pass1b(e2fsck_t ctx, char *block_buf);
105 static void pass1c(e2fsck_t ctx, char *block_buf);
106 static void pass1d(e2fsck_t ctx, char *block_buf);
108 static struct dup_block *dup_blk = 0;
109 static struct dup_inode *dup_ino = 0;
110 static int dup_inode_count = 0;
112 static ext2fs_inode_bitmap inode_dup_map;
115 * Main procedure for handling duplicate blocks
117 void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
119 ext2_filsys fs = ctx->fs;
120 struct dup_block *p, *q, *next_p, *next_q;
121 struct dup_inode *r, *next_r;
122 struct problem_context pctx;
124 clear_problem_context(&pctx);
126 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
127 _("multiply claimed inode map"), &inode_dup_map);
129 fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
130 ctx->flags |= E2F_FLAG_ABORT;
134 pass1b(ctx, block_buf);
135 pass1c(ctx, block_buf);
136 pass1d(ctx, block_buf);
139 * Time to free all of the accumulated data structures that we
140 * don't need anymore.
142 ext2fs_free_inode_bitmap(inode_dup_map); inode_dup_map = 0;
143 ext2fs_free_block_bitmap(ctx->block_dup_map); ctx->block_dup_map = 0;
144 for (p = dup_blk; p; p = next_p) {
145 next_p = p->next_block;
146 for (q = p; q; q = next_q) {
147 next_q = q->next_inode;
148 ext2fs_free_mem((void **) &q);
151 for (r = dup_ino; r; r = next_r) {
153 ext2fs_free_mem((void **) &r);
158 * Scan the inodes looking for inodes that contain duplicate blocks.
160 struct process_block_struct {
164 struct problem_context *pctx;
167 static void pass1b(e2fsck_t ctx, char *block_buf)
169 ext2_filsys fs = ctx->fs;
171 struct ext2_inode inode;
172 ext2_inode_scan scan;
173 struct process_block_struct pb;
174 struct dup_inode *dp;
175 struct problem_context pctx;
177 clear_problem_context(&pctx);
179 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
180 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
183 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
184 ctx->flags |= E2F_FLAG_ABORT;
187 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
189 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
190 ctx->flags |= E2F_FLAG_ABORT;
193 ctx->stashed_inode = &inode;
198 pctx.ino = ctx->stashed_ino = ino;
199 if ((ino != EXT2_BAD_INO) &&
200 (!ext2fs_test_inode_bitmap(ctx->inode_used_map, ino) ||
201 !ext2fs_inode_has_valid_blocks(&inode)))
206 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
207 process_pass1b_block, &pb);
208 if (inode.i_file_acl)
209 process_pass1b_block(fs, &inode.i_file_acl,
210 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
212 end_problem_latch(ctx, PR_LATCH_DBLOCK);
213 dp = (struct dup_inode *) e2fsck_allocate_memory(ctx,
214 sizeof(struct dup_inode),
215 "duplicate inode record");
219 dp->num_dupblocks = pb.dup_blocks;
222 if (ino != EXT2_BAD_INO)
226 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
228 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
229 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
232 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
233 ctx->flags |= E2F_FLAG_ABORT;
237 ext2fs_close_inode_scan(scan);
238 e2fsck_use_inode_shortcuts(ctx, 0);
241 static int process_pass1b_block(ext2_filsys fs,
243 e2_blkcnt_t blockcnt,
248 struct process_block_struct *p;
249 struct dup_block *dp, *q, *r;
253 if (HOLE_BLKADDR(*block_nr))
255 p = (struct process_block_struct *) priv_data;
258 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
259 /* OK, this is a duplicate block */
260 if (p->ino != EXT2_BAD_INO) {
261 p->pctx->blk = *block_nr;
262 fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
265 ext2fs_mark_block_bitmap(ctx->block_dup_map, *block_nr);
266 ext2fs_mark_inode_bitmap(inode_dup_map, p->ino);
267 dp = (struct dup_block *) e2fsck_allocate_memory(ctx,
268 sizeof(struct dup_block),
269 "duplicate block record");
270 dp->block = *block_nr;
273 dp->flags = (blockcnt == BLOCK_COUNT_EXTATTR) ?
277 if (q->block == *block_nr)
282 dp->next_inode = q->next_inode;
285 dp->next_block = dup_blk;
290 * Set the num_bad field
292 for (q = dup_blk; q; q = q->next_block) {
294 for (r = q; r; r = r->next_inode)
302 * Pass 1c: Scan directories for inodes with duplicate blocks. This
303 * is used so that we can print pathnames when prompting the user for
306 struct search_dir_struct {
308 ext2_ino_t first_inode;
309 ext2_ino_t max_inode;
312 static int search_dirent_proc(ext2_ino_t dir, int entry,
313 struct ext2_dir_entry *dirent,
314 int offset, int blocksize,
315 char *buf, void *priv_data)
317 struct search_dir_struct *sd;
320 sd = (struct search_dir_struct *) priv_data;
322 if (dirent->inode > sd->max_inode)
323 /* Should abort this inode, but not everything */
326 if (!dirent->inode || (entry < DIRENT_OTHER_FILE) ||
327 !ext2fs_test_inode_bitmap(inode_dup_map, dirent->inode))
330 for (p = dup_ino; p; p = p->next) {
331 if ((p->ino >= sd->first_inode) &&
332 (p->ino == dirent->inode))
342 return(sd->count ? 0 : DIRENT_ABORT);
346 static void pass1c(e2fsck_t ctx, char *block_buf)
348 ext2_filsys fs = ctx->fs;
350 int inodes_left = dup_inode_count;
351 struct search_dir_struct sd;
352 struct problem_context pctx;
354 clear_problem_context(&pctx);
356 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
359 * First check to see if any of the inodes with dup blocks is
360 * a special inode. (Note that the bad block inode isn't
363 for (p = dup_ino; p; p = p->next) {
364 if ((p->ino < EXT2_FIRST_INODE(fs->super)) &&
365 (p->ino != EXT2_BAD_INO))
370 * Search through all directories to translate inodes to names
371 * (by searching for the containing directory for that inode.)
373 sd.count = inodes_left;
374 sd.first_inode = EXT2_FIRST_INODE(fs->super);
375 sd.max_inode = fs->super->s_inodes_count;
376 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
377 search_dirent_proc, &sd);
380 static void pass1d(e2fsck_t ctx, char *block_buf)
382 ext2_filsys fs = ctx->fs;
383 struct dup_inode *p, *s;
384 struct dup_block *q, *r;
390 struct problem_context pctx;
392 clear_problem_context(&pctx);
394 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
395 e2fsck_read_bitmaps(ctx);
397 pctx.num = dup_inode_count;
398 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
399 shared = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
400 sizeof(ext2_ino_t) * dup_inode_count,
401 "Shared inode list");
402 for (p = dup_ino; p; p = p->next) {
405 if (p->ino == EXT2_BAD_INO)
409 * Search through the duplicate records to see which
410 * inodes share blocks with this one
412 for (q = dup_blk; q; q = q->next_block) {
414 * See if this block is used by this inode.
415 * If it isn't, continue.
417 for (r = q; r; r = r->next_inode)
418 if (r->ino == p->ino)
424 if (check_if_fs_block(ctx, q->block)) {
430 * Add all inodes used by this block to the
431 * shared[] --- which is a unique list, so
432 * if an inode is already in shared[], don't
435 for (r = q; r; r = r->next_inode) {
436 if (r->ino == p->ino)
438 for (i = 0; i < shared_len; i++)
439 if (shared[i] == r->ino)
441 if (i == shared_len) {
442 shared[shared_len++] = r->ino;
448 * Report the inode that we are working on
450 pctx.inode = &p->inode;
453 pctx.blkcount = p->num_dupblocks;
454 pctx.num = meta_data ? shared_len+1 : shared_len;
455 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
460 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
462 for (i = 0; i < shared_len; i++) {
463 for (s = dup_ino; s; s = s->next)
464 if (s->ino == shared[i])
469 * Report the inode that we are sharing with
471 pctx.inode = &s->inode;
474 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
477 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
480 if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
481 pctx.errcode = clone_file(ctx, p, block_buf);
483 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
487 if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
488 delete_file(ctx, p, block_buf);
490 ext2fs_unmark_valid(fs);
492 ext2fs_free_mem((void **) &shared);
496 * Drop the refcount on the dup_block structure, and clear the entry
497 * in the block_dup_map if appropriate.
499 static void decrement_badcount(e2fsck_t ctx, struct dup_block *p)
502 if (p->num_bad <= 0 ||
503 (p->num_bad == 1 && !check_if_fs_block(ctx, p->block)))
504 ext2fs_unmark_block_bitmap(ctx->block_dup_map, p->block);
507 static int delete_file_block(ext2_filsys fs,
509 e2_blkcnt_t blockcnt,
514 struct process_block_struct *pb;
518 pb = (struct process_block_struct *) priv_data;
521 if (HOLE_BLKADDR(*block_nr))
524 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
525 for (p = dup_blk; p; p = p->next_block)
526 if (p->block == *block_nr)
529 decrement_badcount(ctx, p);
531 com_err("delete_file_block", 0,
532 _("internal error; can't find dup_blk for %d\n"),
535 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
536 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
542 static void delete_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
544 ext2_filsys fs = ctx->fs;
545 struct process_block_struct pb;
546 struct ext2_inode inode;
547 struct problem_context pctx;
549 clear_problem_context(&pctx);
550 pctx.ino = pb.ino = dp->ino;
551 pb.dup_blocks = dp->num_dupblocks;
553 pctx.str = "delete_file";
555 pctx.errcode = ext2fs_block_iterate2(fs, dp->ino, 0, block_buf,
556 delete_file_block, &pb);
558 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
559 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, dp->ino);
560 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, dp->ino);
561 if (ctx->inode_bad_map)
562 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, dp->ino);
563 ext2fs_unmark_inode_bitmap(fs->inode_map, dp->ino);
564 ext2fs_mark_ib_dirty(fs);
565 ext2fs_mark_bb_dirty(fs);
566 e2fsck_read_inode(ctx, dp->ino, &inode, "delete_file");
567 inode.i_links_count = 0;
568 inode.i_dtime = time(0);
569 if (inode.i_file_acl)
570 delete_file_block(fs, &inode.i_file_acl,
571 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
572 e2fsck_write_inode(ctx, dp->ino, &inode, "delete_file");
575 struct clone_struct {
582 static int clone_file_block(ext2_filsys fs,
584 e2_blkcnt_t blockcnt,
592 struct clone_struct *cs = (struct clone_struct *) priv_data;
597 if (HOLE_BLKADDR(*block_nr))
600 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
601 for (p = dup_blk; p; p = p->next_block)
602 if (p->block == *block_nr)
605 retval = ext2fs_new_block(fs, 0, ctx->block_found_map,
608 cs->errcode = retval;
611 if (cs->dir && (blockcnt >= 0)) {
612 retval = ext2fs_set_dir_block(fs->dblist,
613 cs->dir, new_block, blockcnt);
615 cs->errcode = retval;
619 retval = io_channel_read_blk(fs->io, *block_nr, 1,
622 cs->errcode = retval;
625 retval = io_channel_write_blk(fs->io, new_block, 1,
628 cs->errcode = retval;
631 decrement_badcount(ctx, p);
632 *block_nr = new_block;
633 ext2fs_mark_block_bitmap(ctx->block_found_map,
635 ext2fs_mark_block_bitmap(fs->block_map, new_block);
636 return BLOCK_CHANGED;
638 com_err("clone_file_block", 0,
639 _("internal error; can't find dup_blk for %d\n"),
645 static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
647 ext2_filsys fs = ctx->fs;
649 struct clone_struct cs;
650 struct problem_context pctx;
653 clear_problem_context(&pctx);
657 retval = ext2fs_get_mem(fs->blocksize, (void **) &cs.buf);
661 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dp->ino))
665 pctx.str = "clone_file";
666 pctx.errcode = ext2fs_block_iterate2(fs, dp->ino, 0, block_buf,
667 clone_file_block, &cs);
668 ext2fs_mark_bb_dirty(fs);
670 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
671 retval = pctx.errcode;
675 com_err("clone_file", cs.errcode,
676 _("returned from clone_file_block"));
680 blk = dp->inode.i_file_acl;
681 if (blk && (clone_file_block(fs, &dp->inode.i_file_acl,
682 BLOCK_COUNT_EXTATTR, 0, 0, &cs) ==
684 struct dup_block *p, *q;
688 * If we cloned the EA block, find all other inodes
689 * which refered to that EA block, and modify
690 * them to point to the new EA block.
692 for (p = dup_blk; p; p = p->next_block) {
696 for (q = p; q ; q = q->next_inode) {
697 if (!(q->flags & FLAG_EXTATTR))
699 for (r = dup_ino; r; r = r->next)
700 if (r->ino == q->ino)
703 r->inode.i_file_acl = dp->inode.i_file_acl;
704 e2fsck_write_inode(ctx, q->ino, &r->inode,
707 q->ino = 0; /* Should free the structure... */
708 decrement_badcount(ctx, p);
713 ext2fs_free_mem((void **) &cs.buf);
718 * This routine returns 1 if a block overlaps with one of the superblocks,
719 * group descriptors, inode bitmaps, or block bitmaps.
721 static int check_if_fs_block(e2fsck_t ctx, blk_t test_block)
723 ext2_filsys fs = ctx->fs;
727 block = fs->super->s_first_data_block;
728 for (i = 0; i < fs->group_desc_count; i++) {
730 /* Check superblocks/block group descriptros */
731 if (ext2fs_bg_has_super(fs, i)) {
732 if (test_block >= block &&
733 (test_block <= block + fs->desc_blocks))
737 /* Check the inode table */
738 if ((fs->group_desc[i].bg_inode_table) &&
739 (test_block >= fs->group_desc[i].bg_inode_table) &&
740 (test_block < (fs->group_desc[i].bg_inode_table +
741 fs->inode_blocks_per_group)))
744 /* Check the bitmap blocks */
745 if ((test_block == fs->group_desc[i].bg_block_bitmap) ||
746 (test_block == fs->group_desc[i].bg_inode_bitmap))
749 block += fs->super->s_blocks_per_group;