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 *private);
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 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);
124 pass1b(ctx, block_buf);
125 pass1c(ctx, block_buf);
126 pass1d(ctx, block_buf);
129 * Time to free all of the accumulated data structures that we
130 * don't need anymore.
132 ext2fs_free_inode_bitmap(inode_dup_map); inode_dup_map = 0;
133 ext2fs_free_block_bitmap(ctx->block_dup_map); ctx->block_dup_map = 0;
134 for (p = dup_blk; p; p = next_p) {
135 next_p = p->next_block;
136 for (q = p; q; q = next_q) {
137 next_q = q->next_inode;
141 for (r = dup_ino; r; r = next_r) {
148 * Scan the inodes looking for inodes that contain duplicate blocks.
150 struct process_block_struct {
154 struct problem_context *pctx;
157 void pass1b(e2fsck_t ctx, char *block_buf)
159 ext2_filsys fs = ctx->fs;
161 struct ext2_inode inode;
162 ext2_inode_scan scan;
164 struct process_block_struct pb;
165 struct dup_inode *dp;
166 struct problem_context pctx;
168 clear_problem_context(&pctx);
170 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
171 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
174 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
177 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
179 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
182 ctx->stashed_inode = &inode;
186 pctx.ino = ctx->stashed_ino = ino;
187 if ((ino != EXT2_BAD_INO) &&
188 (!ext2fs_test_inode_bitmap(ctx->inode_used_map, ino) ||
189 !ext2fs_inode_has_valid_blocks(&inode)))
194 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
195 process_pass1b_block, &pb);
197 end_problem_latch(ctx, PR_LATCH_DBLOCK);
198 dp = allocate_memory(sizeof(struct dup_inode),
199 "duplicate inode record");
203 dp->num_dupblocks = pb.dup_blocks;
206 if (ino != EXT2_BAD_INO)
210 com_err(ctx->program_name, retval,
211 "while calling ext2fs_block_iterate in pass1b");
214 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
215 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
218 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
222 ext2fs_close_inode_scan(scan);
224 fs->check_directory = 0;
227 int process_pass1b_block(ext2_filsys fs,
232 struct process_block_struct *p;
233 struct dup_block *dp, *q, *r;
239 p = (struct process_block_struct *) private;
242 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
243 /* OK, this is a duplicate block */
244 if (p->ino != EXT2_BAD_INO) {
245 p->pctx->blk = *block_nr;
246 fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
249 ext2fs_mark_block_bitmap(ctx->block_dup_map, *block_nr);
250 ext2fs_mark_inode_bitmap(inode_dup_map, p->ino);
251 dp = allocate_memory(sizeof(struct dup_block),
252 "duplicate block record");
253 dp->block = *block_nr;
258 if (q->block == *block_nr)
263 dp->next_inode = q->next_inode;
266 dp->next_block = dup_blk;
271 * Set the num_bad field
273 for (q = dup_blk; q; q = q->next_block) {
275 for (r = q; r; r = r->next_inode)
283 * Pass 1c: Scan directories for inodes with duplicate blocks. This
284 * is used so that we can print pathnames when prompting the user for
287 struct search_dir_struct {
293 static int search_dirent_proc(ino_t dir, int entry,
294 struct ext2_dir_entry *dirent,
295 int offset, int blocksize,
296 char *buf, void *private)
298 struct search_dir_struct *sd = private;
301 if (dirent->inode > sd->max_inode)
302 /* Should abort this inode, but not everything */
305 if (!dirent->inode || (entry < DIRENT_OTHER_FILE) ||
306 !ext2fs_test_inode_bitmap(inode_dup_map, dirent->inode))
309 for (p = dup_ino; p; p = p->next) {
310 if ((p->ino >= sd->first_inode) &&
311 (p->ino == dirent->inode))
321 return(sd->count ? 0 : DIRENT_ABORT);
325 void pass1c(e2fsck_t ctx, char *block_buf)
327 ext2_filsys fs = ctx->fs;
329 int inodes_left = dup_inode_count;
330 struct search_dir_struct sd;
331 struct problem_context pctx;
333 clear_problem_context(&pctx);
335 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
338 * First check to see if any of the inodes with dup blocks is
339 * a special inode. (Note that the bad block inode isn't
342 for (p = dup_ino; p; p = p->next) {
343 if ((p->ino < EXT2_FIRST_INODE(fs->super)) &&
344 (p->ino != EXT2_BAD_INO))
349 * Search through all directories to translate inodes to names
350 * (by searching for the containing directory for that inode.)
352 sd.count = inodes_left;
353 sd.first_inode = EXT2_FIRST_INODE(fs->super);
354 sd.max_inode = fs->super->s_inodes_count;
355 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
356 search_dirent_proc, &sd);
359 static void pass1d(e2fsck_t ctx, char *block_buf)
361 ext2_filsys fs = ctx->fs;
362 struct dup_inode *p, *s;
363 struct dup_block *q, *r;
369 struct problem_context pctx;
371 clear_problem_context(&pctx);
373 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
376 pctx.num = dup_inode_count;
377 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
378 shared = allocate_memory(sizeof(ino_t) * dup_inode_count,
379 "Shared inode list");
380 for (p = dup_ino; p; p = p->next) {
383 if (p->ino == EXT2_BAD_INO)
387 * Search through the duplicate records to see which
388 * inodes share blocks with this one
390 for (q = dup_blk; q; q = q->next_block) {
392 * See if this block is used by this inode.
393 * If it isn't, continue.
395 for (r = q; r; r = r->next_inode)
396 if (r->ino == p->ino)
402 if (ext2fs_test_block_bitmap(ctx->block_illegal_map,
409 * Add all inodes used by this block to the
410 * shared[] --- which is a unique list, so
411 * if an inode is already in shared[], don't
414 for (r = q; r; r = r->next_inode) {
415 if (r->ino == p->ino)
417 for (i = 0; i < shared_len; i++)
418 if (shared[i] == r->ino)
420 if (i == shared_len) {
421 shared[shared_len++] = r->ino;
427 * Report the inode that we are working on
429 pctx.inode = &p->inode;
432 pctx.blkcount = p->num_dupblocks;
433 pctx.num = meta_data ? shared_len+1 : shared_len;
434 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
439 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
441 for (i = 0; i < shared_len; i++) {
442 for (s = dup_ino; s; s = s->next)
443 if (s->ino == shared[i])
448 * Report the inode that we are sharing with
450 pctx.inode = &s->inode;
453 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
456 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
459 if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
460 pctx.errcode = clone_file(ctx, p, block_buf);
462 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
466 if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
467 delete_file(ctx, p, block_buf);
469 ext2fs_unmark_valid(fs);
474 static int delete_file_block(ext2_filsys fs,
479 struct process_block_struct *pb = private;
488 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
489 for (p = dup_blk; p; p = p->next_block)
490 if (p->block == *block_nr)
495 ext2fs_unmark_block_bitmap(ctx->block_dup_map,
498 com_err("delete_file_block", 0,
499 "internal error; can't find dup_blk for %d\n",
502 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
503 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
509 static void delete_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
511 ext2_filsys fs = ctx->fs;
513 struct process_block_struct pb;
514 struct ext2_inode inode;
517 pb.dup_blocks = dp->num_dupblocks;
520 retval = ext2fs_block_iterate(fs, dp->ino, 0, block_buf,
521 delete_file_block, &pb);
523 com_err("delete_file", retval,
524 "while calling ext2fs_block_iterate for inode %d",
526 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, dp->ino);
527 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, dp->ino);
528 if (ctx->inode_bad_map)
529 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, dp->ino);
530 ext2fs_unmark_inode_bitmap(fs->inode_map, dp->ino);
531 ext2fs_mark_ib_dirty(fs);
532 ext2fs_mark_bb_dirty(fs);
533 e2fsck_read_inode(fs, dp->ino, &inode, "delete_file");
534 inode.i_links_count = 0;
535 inode.i_dtime = time(0);
536 e2fsck_write_inode(fs, dp->ino, &inode, "delete_file");
539 struct clone_struct {
546 static int clone_file_block(ext2_filsys fs,
554 struct clone_struct *cs = (struct clone_struct *) private;
562 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
563 for (p = dup_blk; p; p = p->next_block)
564 if (p->block == *block_nr)
567 retval = ext2fs_new_block(fs, 0, ctx->block_found_map,
570 cs->errcode = retval;
574 retval = ext2fs_set_dir_block(fs->dblist,
575 cs->dir, new_block, blockcnt);
577 cs->errcode = retval;
581 retval = io_channel_read_blk(fs->io, *block_nr, 1,
584 cs->errcode = retval;
587 retval = io_channel_write_blk(fs->io, new_block, 1,
590 cs->errcode = retval;
595 ext2fs_unmark_block_bitmap(ctx->block_dup_map,
597 *block_nr = new_block;
598 ext2fs_mark_block_bitmap(ctx->block_found_map,
600 ext2fs_mark_block_bitmap(fs->block_map, new_block);
601 return BLOCK_CHANGED;
603 com_err("clone_file_block", 0,
604 "internal error; can't find dup_blk for %d\n",
610 static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
612 ext2_filsys fs = ctx->fs;
614 struct clone_struct cs;
619 cs.buf = malloc(fs->blocksize);
623 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dp->ino))
626 retval = ext2fs_block_iterate(fs, dp->ino, 0, block_buf,
627 clone_file_block, &cs);
628 ext2fs_mark_bb_dirty(fs);
631 com_err("clone_file", retval,
632 "while calling ext2fs_block_iterate for inode %d",
637 com_err("clone_file", retval,
638 "returned from clone_file_block");