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 void pass1b(e2fsck_t ctx, char *block_buf);
96 static void pass1c(e2fsck_t ctx, char *block_buf);
97 static void pass1d(e2fsck_t ctx, char *block_buf);
99 static struct dup_block *dup_blk = 0;
100 static struct dup_inode *dup_ino = 0;
101 static int dup_inode_count = 0;
103 static ext2fs_inode_bitmap inode_dup_map;
106 * Main procedure for handling duplicate blocks
108 void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
110 ext2_filsys fs = ctx->fs;
111 struct dup_block *p, *q, *next_p, *next_q;
112 struct dup_inode *r, *next_r;
113 struct problem_context pctx;
115 clear_problem_context(&pctx);
117 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
118 "multiply claimed inode map", &inode_dup_map);
120 fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
121 ctx->flags |= E2F_FLAG_ABORT;
125 pass1b(ctx, block_buf);
126 pass1c(ctx, block_buf);
127 pass1d(ctx, block_buf);
130 * Time to free all of the accumulated data structures that we
131 * don't need anymore.
133 ext2fs_free_inode_bitmap(inode_dup_map); inode_dup_map = 0;
134 ext2fs_free_block_bitmap(ctx->block_dup_map); ctx->block_dup_map = 0;
135 for (p = dup_blk; p; p = next_p) {
136 next_p = p->next_block;
137 for (q = p; q; q = next_q) {
138 next_q = q->next_inode;
139 ext2fs_free_mem((void **) &q);
142 for (r = dup_ino; r; r = next_r) {
144 ext2fs_free_mem((void **) &r);
149 * Scan the inodes looking for inodes that contain duplicate blocks.
151 struct process_block_struct {
155 struct problem_context *pctx;
158 static void pass1b(e2fsck_t ctx, char *block_buf)
160 ext2_filsys fs = ctx->fs;
162 struct ext2_inode inode;
163 ext2_inode_scan scan;
165 struct process_block_struct pb;
166 struct dup_inode *dp;
167 struct problem_context pctx;
169 clear_problem_context(&pctx);
171 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
172 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
175 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
176 ctx->flags |= E2F_FLAG_ABORT;
179 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
181 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
182 ctx->flags |= E2F_FLAG_ABORT;
185 ctx->stashed_inode = &inode;
189 pctx.ino = ctx->stashed_ino = ino;
190 if ((ino != EXT2_BAD_INO) &&
191 (!ext2fs_test_inode_bitmap(ctx->inode_used_map, ino) ||
192 !ext2fs_inode_has_valid_blocks(&inode)))
197 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
198 process_pass1b_block, &pb);
200 end_problem_latch(ctx, PR_LATCH_DBLOCK);
201 dp = (struct dup_inode *) e2fsck_allocate_memory(ctx,
202 sizeof(struct dup_inode),
203 "duplicate inode record");
207 dp->num_dupblocks = pb.dup_blocks;
210 if (ino != EXT2_BAD_INO)
214 com_err(ctx->program_name, retval,
215 "while calling ext2fs_block_iterate in pass1b");
218 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
219 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
222 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
223 ctx->flags |= E2F_FLAG_ABORT;
227 ext2fs_close_inode_scan(scan);
229 fs->check_directory = 0;
232 int process_pass1b_block(ext2_filsys fs,
237 struct process_block_struct *p;
238 struct dup_block *dp, *q, *r;
244 p = (struct process_block_struct *) priv_data;
247 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
248 /* OK, this is a duplicate block */
249 if (p->ino != EXT2_BAD_INO) {
250 p->pctx->blk = *block_nr;
251 fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
254 ext2fs_mark_block_bitmap(ctx->block_dup_map, *block_nr);
255 ext2fs_mark_inode_bitmap(inode_dup_map, p->ino);
256 dp = (struct dup_block *) e2fsck_allocate_memory(ctx,
257 sizeof(struct dup_block),
258 "duplicate block record");
259 dp->block = *block_nr;
264 if (q->block == *block_nr)
269 dp->next_inode = q->next_inode;
272 dp->next_block = dup_blk;
277 * Set the num_bad field
279 for (q = dup_blk; q; q = q->next_block) {
281 for (r = q; r; r = r->next_inode)
289 * Pass 1c: Scan directories for inodes with duplicate blocks. This
290 * is used so that we can print pathnames when prompting the user for
293 struct search_dir_struct {
299 static int search_dirent_proc(ino_t dir, int entry,
300 struct ext2_dir_entry *dirent,
301 int offset, int blocksize,
302 char *buf, void *priv_data)
304 struct search_dir_struct *sd;
307 sd = (struct search_dir_struct *) priv_data;
309 if (dirent->inode > sd->max_inode)
310 /* Should abort this inode, but not everything */
313 if (!dirent->inode || (entry < DIRENT_OTHER_FILE) ||
314 !ext2fs_test_inode_bitmap(inode_dup_map, dirent->inode))
317 for (p = dup_ino; p; p = p->next) {
318 if ((p->ino >= sd->first_inode) &&
319 (p->ino == dirent->inode))
329 return(sd->count ? 0 : DIRENT_ABORT);
333 static void pass1c(e2fsck_t ctx, char *block_buf)
335 ext2_filsys fs = ctx->fs;
337 int inodes_left = dup_inode_count;
338 struct search_dir_struct sd;
339 struct problem_context pctx;
341 clear_problem_context(&pctx);
343 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
346 * First check to see if any of the inodes with dup blocks is
347 * a special inode. (Note that the bad block inode isn't
350 for (p = dup_ino; p; p = p->next) {
351 if ((p->ino < EXT2_FIRST_INODE(fs->super)) &&
352 (p->ino != EXT2_BAD_INO))
357 * Search through all directories to translate inodes to names
358 * (by searching for the containing directory for that inode.)
360 sd.count = inodes_left;
361 sd.first_inode = EXT2_FIRST_INODE(fs->super);
362 sd.max_inode = fs->super->s_inodes_count;
363 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
364 search_dirent_proc, &sd);
367 static void pass1d(e2fsck_t ctx, char *block_buf)
369 ext2_filsys fs = ctx->fs;
370 struct dup_inode *p, *s;
371 struct dup_block *q, *r;
377 struct problem_context pctx;
379 clear_problem_context(&pctx);
381 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
382 e2fsck_read_bitmaps(ctx);
384 pctx.num = dup_inode_count;
385 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
386 shared = (ino_t *) e2fsck_allocate_memory(ctx,
387 sizeof(ino_t) * dup_inode_count,
388 "Shared inode list");
389 for (p = dup_ino; p; p = p->next) {
392 if (p->ino == EXT2_BAD_INO)
396 * Search through the duplicate records to see which
397 * inodes share blocks with this one
399 for (q = dup_blk; q; q = q->next_block) {
401 * See if this block is used by this inode.
402 * If it isn't, continue.
404 for (r = q; r; r = r->next_inode)
405 if (r->ino == p->ino)
411 if (ext2fs_test_block_bitmap(ctx->block_illegal_map,
418 * Add all inodes used by this block to the
419 * shared[] --- which is a unique list, so
420 * if an inode is already in shared[], don't
423 for (r = q; r; r = r->next_inode) {
424 if (r->ino == p->ino)
426 for (i = 0; i < shared_len; i++)
427 if (shared[i] == r->ino)
429 if (i == shared_len) {
430 shared[shared_len++] = r->ino;
436 * Report the inode that we are working on
438 pctx.inode = &p->inode;
441 pctx.blkcount = p->num_dupblocks;
442 pctx.num = meta_data ? shared_len+1 : shared_len;
443 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
448 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
450 for (i = 0; i < shared_len; i++) {
451 for (s = dup_ino; s; s = s->next)
452 if (s->ino == shared[i])
457 * Report the inode that we are sharing with
459 pctx.inode = &s->inode;
462 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
465 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
468 if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
469 pctx.errcode = clone_file(ctx, p, block_buf);
471 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
475 if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
476 delete_file(ctx, p, block_buf);
478 ext2fs_unmark_valid(fs);
480 ext2fs_free_mem((void **) &shared);
483 static int delete_file_block(ext2_filsys fs,
488 struct process_block_struct *pb;
492 pb = (struct process_block_struct *) priv_data;
498 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
499 for (p = dup_blk; p; p = p->next_block)
500 if (p->block == *block_nr)
505 ext2fs_unmark_block_bitmap(ctx->block_dup_map,
508 com_err("delete_file_block", 0,
509 "internal error; can't find dup_blk for %d\n",
512 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
513 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
519 static void delete_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
521 ext2_filsys fs = ctx->fs;
523 struct process_block_struct pb;
524 struct ext2_inode inode;
527 pb.dup_blocks = dp->num_dupblocks;
530 retval = ext2fs_block_iterate(fs, dp->ino, 0, block_buf,
531 delete_file_block, &pb);
533 com_err("delete_file", retval,
534 "while calling ext2fs_block_iterate for inode %d",
536 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, dp->ino);
537 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, dp->ino);
538 if (ctx->inode_bad_map)
539 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, dp->ino);
540 ext2fs_unmark_inode_bitmap(fs->inode_map, dp->ino);
541 ext2fs_mark_ib_dirty(fs);
542 ext2fs_mark_bb_dirty(fs);
543 e2fsck_read_inode(ctx, dp->ino, &inode, "delete_file");
544 inode.i_links_count = 0;
545 inode.i_dtime = time(0);
546 e2fsck_write_inode(ctx, dp->ino, &inode, "delete_file");
549 struct clone_struct {
556 static int clone_file_block(ext2_filsys fs,
564 struct clone_struct *cs = (struct clone_struct *) priv_data;
572 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
573 for (p = dup_blk; p; p = p->next_block)
574 if (p->block == *block_nr)
577 retval = ext2fs_new_block(fs, 0, ctx->block_found_map,
580 cs->errcode = retval;
584 retval = ext2fs_set_dir_block(fs->dblist,
585 cs->dir, new_block, blockcnt);
587 cs->errcode = retval;
591 retval = io_channel_read_blk(fs->io, *block_nr, 1,
594 cs->errcode = retval;
597 retval = io_channel_write_blk(fs->io, new_block, 1,
600 cs->errcode = retval;
605 ext2fs_unmark_block_bitmap(ctx->block_dup_map,
607 *block_nr = new_block;
608 ext2fs_mark_block_bitmap(ctx->block_found_map,
610 ext2fs_mark_block_bitmap(fs->block_map, new_block);
611 return BLOCK_CHANGED;
613 com_err("clone_file_block", 0,
614 "internal error; can't find dup_blk for %d\n",
620 static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
622 ext2_filsys fs = ctx->fs;
624 struct clone_struct cs;
629 retval = ext2fs_get_mem(fs->blocksize, (void **) &cs.buf);
633 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dp->ino))
636 retval = ext2fs_block_iterate(fs, dp->ino, 0, block_buf,
637 clone_file_block, &cs);
638 ext2fs_mark_bb_dirty(fs);
639 ext2fs_free_mem((void **) &cs.buf);
641 com_err("clone_file", retval,
642 "while calling ext2fs_block_iterate for inode %d",
647 com_err("clone_file", cs.errcode,
648 "returned from clone_file_block");