blob: 5db39ab21597e74f1202a54a22c5d6136c136568 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * pass1b.c --- Pass #1b of e2fsck
3 *
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
6 * than one inode.
7 *
8 * Pass1B scans the data blocks of all the inodes again, generating a
9 * complete list of duplicate blocks and which inodes have claimed
10 * them.
11 *
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.
15 *
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.
20 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +000021 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
22 *
23 * %Begin-Header%
24 * This file may be redistributed under the terms of the GNU Public
25 * License.
26 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000027 *
28 */
29
30#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000031#ifdef HAVE_ERRNO_H
32#include <errno.h>
33#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000034
35#include <et/com_err.h>
36#include "e2fsck.h"
37
Theodore Ts'o21c84b71997-04-29 16:15:03 +000038#include "problem.h"
39
Theodore Ts'o3839e651997-04-26 13:21:57 +000040/*
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.
45 *
46 * The linked list structure is as follows:
47 *
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
51 * | | |
52 * V V V
53 * block #34 block #35 block #47
54 * inode #14 inode #15 inode #23
55 * |
56 * V
57 * block #34
58 * inode #15
59 *
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.
65 */
66struct dup_block {
67 blk_t block; /* Block number */
68 ino_t ino; /* Inode number */
69 int num_bad;
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;
74};
75
76/*
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.
82 */
83struct dup_inode {
Theodore Ts'o21c84b71997-04-29 16:15:03 +000084 ino_t ino, dir;
85 int num_dupblocks;
86 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +000087 struct dup_inode *next;
88};
89
Theodore Ts'o3839e651997-04-26 13:21:57 +000090static int process_pass1b_block(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +000091 int blockcnt, void *priv_data);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000092static void delete_file(e2fsck_t ctx, struct dup_inode *dp,
Theodore Ts'o3839e651997-04-26 13:21:57 +000093 char *block_buf);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000094static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf);
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +000095static int check_if_fs_block(e2fsck_t ctx, blk_t test_blk);
96
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000097static void pass1b(e2fsck_t ctx, char *block_buf);
98static void pass1c(e2fsck_t ctx, char *block_buf);
99static void pass1d(e2fsck_t ctx, char *block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100
101static struct dup_block *dup_blk = 0;
102static struct dup_inode *dup_ino = 0;
103static int dup_inode_count = 0;
104
Theodore Ts'of3db3561997-04-26 13:34:30 +0000105static ext2fs_inode_bitmap inode_dup_map;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106
107/*
108 * Main procedure for handling duplicate blocks
109 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000110void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000112 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000113 struct dup_block *p, *q, *next_p, *next_q;
114 struct dup_inode *r, *next_r;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000115 struct problem_context pctx;
116
117 clear_problem_context(&pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000118
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000119 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000120 _("multiply claimed inode map"), &inode_dup_map);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000121 if (pctx.errcode) {
122 fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000123 ctx->flags |= E2F_FLAG_ABORT;
124 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000125 }
126
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000127 pass1b(ctx, block_buf);
128 pass1c(ctx, block_buf);
129 pass1d(ctx, block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000130
131 /*
132 * Time to free all of the accumulated data structures that we
133 * don't need anymore.
134 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000135 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;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000137 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;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000141 ext2fs_free_mem((void **) &q);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000142 }
143 }
144 for (r = dup_ino; r; r = next_r) {
145 next_r = r->next;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000146 ext2fs_free_mem((void **) &r);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000147 }
148}
149
150/*
151 * Scan the inodes looking for inodes that contain duplicate blocks.
152 */
153struct process_block_struct {
154 ino_t ino;
155 int dup_blocks;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000156 e2fsck_t ctx;
157 struct problem_context *pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000158};
159
Theodore Ts'o08b21301997-11-03 19:42:40 +0000160static void pass1b(e2fsck_t ctx, char *block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000161{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000162 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000163 ino_t ino;
164 struct ext2_inode inode;
165 ext2_inode_scan scan;
166 errcode_t retval;
167 struct process_block_struct pb;
168 struct dup_inode *dp;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000169 struct problem_context pctx;
170
171 clear_problem_context(&pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000173 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
174 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
175 &scan);
176 if (pctx.errcode) {
177 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000178 ctx->flags |= E2F_FLAG_ABORT;
179 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000180 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000181 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
182 if (pctx.errcode) {
183 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000184 ctx->flags |= E2F_FLAG_ABORT;
185 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000186 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000187 ctx->stashed_inode = &inode;
188 pb.ctx = ctx;
189 pb.pctx = &pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190 while (ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000191 pctx.ino = ctx->stashed_ino = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000192 if ((ino != EXT2_BAD_INO) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000193 (!ext2fs_test_inode_bitmap(ctx->inode_used_map, ino) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000194 !ext2fs_inode_has_valid_blocks(&inode)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 goto next;
196
197 pb.ino = ino;
198 pb.dup_blocks = 0;
199 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
200 process_pass1b_block, &pb);
201 if (pb.dup_blocks) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000202 end_problem_latch(ctx, PR_LATCH_DBLOCK);
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000203 dp = (struct dup_inode *) e2fsck_allocate_memory(ctx,
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000204 sizeof(struct dup_inode),
205 "duplicate inode record");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000206 dp->ino = ino;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000207 dp->dir = 0;
208 dp->inode = inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209 dp->num_dupblocks = pb.dup_blocks;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000210 dp->next = dup_ino;
211 dup_ino = dp;
212 if (ino != EXT2_BAD_INO)
213 dup_inode_count++;
214 }
215 if (retval)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000216 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000217 _("while calling ext2fs_block_iterate in pass1b"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218
219 next:
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000220 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
221 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000222 goto next;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000223 if (pctx.errcode) {
224 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000225 ctx->flags |= E2F_FLAG_ABORT;
226 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000227 }
228 }
229 ext2fs_close_inode_scan(scan);
230 fs->get_blocks = 0;
231 fs->check_directory = 0;
232}
233
234int process_pass1b_block(ext2_filsys fs,
235 blk_t *block_nr,
236 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000237 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238{
239 struct process_block_struct *p;
240 struct dup_block *dp, *q, *r;
241 int i;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000242 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000243
Theodore Ts'o19178752000-02-11 15:55:07 +0000244 if (HOLE_BLKADDR(*block_nr))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000245 return 0;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000246 p = (struct process_block_struct *) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000247 ctx = p->ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000249 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000250 /* OK, this is a duplicate block */
251 if (p->ino != EXT2_BAD_INO) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000252 p->pctx->blk = *block_nr;
253 fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254 }
255 p->dup_blocks++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000256 ext2fs_mark_block_bitmap(ctx->block_dup_map, *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000257 ext2fs_mark_inode_bitmap(inode_dup_map, p->ino);
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000258 dp = (struct dup_block *) e2fsck_allocate_memory(ctx,
259 sizeof(struct dup_block),
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000260 "duplicate block record");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000261 dp->block = *block_nr;
262 dp->ino = p->ino;
263 dp->num_bad = 0;
264 q = dup_blk;
265 while (q) {
266 if (q->block == *block_nr)
267 break;
268 q = q->next_block;
269 }
270 if (q) {
271 dp->next_inode = q->next_inode;
272 q->next_inode = dp;
273 } else {
274 dp->next_block = dup_blk;
275 dup_blk = dp;
276 }
277 }
278 /*
279 * Set the num_bad field
280 */
281 for (q = dup_blk; q; q = q->next_block) {
282 i = 0;
283 for (r = q; r; r = r->next_inode)
284 i++;
285 q->num_bad = i;
286 }
287 return 0;
288}
289
290/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291 * Pass 1c: Scan directories for inodes with duplicate blocks. This
292 * is used so that we can print pathnames when prompting the user for
293 * what to do.
294 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000295struct search_dir_struct {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000296 int count;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000297 ino_t first_inode;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000298 ino_t max_inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000299};
300
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000301static int search_dirent_proc(ino_t dir, int entry,
302 struct ext2_dir_entry *dirent,
303 int offset, int blocksize,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000304 char *buf, void *priv_data)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000305{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000306 struct search_dir_struct *sd;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000307 struct dup_inode *p;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000308
309 sd = (struct search_dir_struct *) priv_data;
310
Theodore Ts'o521e3681997-04-29 17:48:10 +0000311 if (dirent->inode > sd->max_inode)
312 /* Should abort this inode, but not everything */
313 return 0;
314
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000315 if (!dirent->inode || (entry < DIRENT_OTHER_FILE) ||
316 !ext2fs_test_inode_bitmap(inode_dup_map, dirent->inode))
317 return 0;
318
319 for (p = dup_ino; p; p = p->next) {
320 if ((p->ino >= sd->first_inode) &&
321 (p->ino == dirent->inode))
322 break;
323 }
324
325 if (!p || p->dir)
326 return 0;
327
328 p->dir = dir;
329 sd->count--;
330
331 return(sd->count ? 0 : DIRENT_ABORT);
332}
333
334
Theodore Ts'o08b21301997-11-03 19:42:40 +0000335static void pass1c(e2fsck_t ctx, char *block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000336{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000337 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000338 struct dup_inode *p;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000339 int inodes_left = dup_inode_count;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000340 struct search_dir_struct sd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000341 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000342
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000343 clear_problem_context(&pctx);
344
345 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346
347 /*
348 * First check to see if any of the inodes with dup blocks is
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000349 * a special inode. (Note that the bad block inode isn't
350 * counted.)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000351 */
352 for (p = dup_ino; p; p = p->next) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000353 if ((p->ino < EXT2_FIRST_INODE(fs->super)) &&
354 (p->ino != EXT2_BAD_INO))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000355 inodes_left--;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000356 }
357
358 /*
359 * Search through all directories to translate inodes to names
360 * (by searching for the containing directory for that inode.)
361 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000362 sd.count = inodes_left;
363 sd.first_inode = EXT2_FIRST_INODE(fs->super);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000364 sd.max_inode = fs->super->s_inodes_count;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000365 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
366 search_dirent_proc, &sd);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000367}
368
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000369static void pass1d(e2fsck_t ctx, char *block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000370{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000371 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000372 struct dup_inode *p, *s;
373 struct dup_block *q, *r;
374 ino_t *shared;
375 int shared_len;
376 int i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000377 int file_ok;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000378 int meta_data = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000379 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000380
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000381 clear_problem_context(&pctx);
382
383 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000384 e2fsck_read_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000385
386 pctx.num = dup_inode_count;
387 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000388 shared = (ino_t *) e2fsck_allocate_memory(ctx,
389 sizeof(ino_t) * dup_inode_count,
390 "Shared inode list");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000391 for (p = dup_ino; p; p = p->next) {
392 shared_len = 0;
393 file_ok = 1;
394 if (p->ino == EXT2_BAD_INO)
395 continue;
396
397 /*
398 * Search through the duplicate records to see which
399 * inodes share blocks with this one
400 */
401 for (q = dup_blk; q; q = q->next_block) {
402 /*
403 * See if this block is used by this inode.
404 * If it isn't, continue.
405 */
406 for (r = q; r; r = r->next_inode)
407 if (r->ino == p->ino)
408 break;
409 if (!r)
410 continue;
411 if (q->num_bad > 1)
412 file_ok = 0;
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +0000413 if (check_if_fs_block(ctx, q->block)) {
Theodore Ts'o521e3681997-04-29 17:48:10 +0000414 file_ok = 0;
415 meta_data = 1;
416 }
417
Theodore Ts'o3839e651997-04-26 13:21:57 +0000418 /*
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
422 * add it again.
423 */
424 for (r = q; r; r = r->next_inode) {
425 if (r->ino == p->ino)
426 continue;
427 for (i = 0; i < shared_len; i++)
428 if (shared[i] == r->ino)
429 break;
430 if (i == shared_len) {
431 shared[shared_len++] = r->ino;
432 }
433 }
434 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000435
436 /*
437 * Report the inode that we are working on
438 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000439 pctx.inode = &p->inode;
440 pctx.ino = p->ino;
441 pctx.dir = p->dir;
442 pctx.blkcount = p->num_dupblocks;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000443 pctx.num = meta_data ? shared_len+1 : shared_len;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000444 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000445 pctx.blkcount = 0;
446 pctx.num = 0;
447
Theodore Ts'o521e3681997-04-29 17:48:10 +0000448 if (meta_data)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000449 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000450
Theodore Ts'o3839e651997-04-26 13:21:57 +0000451 for (i = 0; i < shared_len; i++) {
452 for (s = dup_ino; s; s = s->next)
453 if (s->ino == shared[i])
454 break;
455 if (!s)
456 continue;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000457 /*
458 * Report the inode that we are sharing with
459 */
460 pctx.inode = &s->inode;
461 pctx.ino = s->ino;
462 pctx.dir = s->dir;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000463 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000464 }
465 if (file_ok) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000466 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467 continue;
468 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000469 if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
470 pctx.errcode = clone_file(ctx, p, block_buf);
471 if (pctx.errcode)
472 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
473 else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000474 continue;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000476 if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
477 delete_file(ctx, p, block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478 else
479 ext2fs_unmark_valid(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000480 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000481 ext2fs_free_mem((void **) &shared);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000482}
483
484static int delete_file_block(ext2_filsys fs,
485 blk_t *block_nr,
486 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000487 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000489 struct process_block_struct *pb;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490 struct dup_block *p;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000491 e2fsck_t ctx;
492
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000493 pb = (struct process_block_struct *) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000494 ctx = pb->ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000495
Theodore Ts'o19178752000-02-11 15:55:07 +0000496 if (HOLE_BLKADDR(*block_nr))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000497 return 0;
498
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000499 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000500 for (p = dup_blk; p; p = p->next_block)
501 if (p->block == *block_nr)
502 break;
503 if (p) {
504 p->num_bad--;
505 if (p->num_bad == 1)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000506 ext2fs_unmark_block_bitmap(ctx->block_dup_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000507 *block_nr);
508 } else
509 com_err("delete_file_block", 0,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000510 _("internal error; can't find dup_blk for %d\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000511 *block_nr);
512 } else {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000513 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000514 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 }
516
517 return 0;
518}
519
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000520static void delete_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000522 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000523 errcode_t retval;
524 struct process_block_struct pb;
525 struct ext2_inode inode;
526
527 pb.ino = dp->ino;
528 pb.dup_blocks = dp->num_dupblocks;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000529 pb.ctx = ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530
531 retval = ext2fs_block_iterate(fs, dp->ino, 0, block_buf,
532 delete_file_block, &pb);
533 if (retval)
534 com_err("delete_file", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000535 _("while calling ext2fs_block_iterate for inode %d"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 dp->ino);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000537 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);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000541 ext2fs_unmark_inode_bitmap(fs->inode_map, dp->ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000542 ext2fs_mark_ib_dirty(fs);
543 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000544 e2fsck_read_inode(ctx, dp->ino, &inode, "delete_file");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 inode.i_links_count = 0;
546 inode.i_dtime = time(0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000547 e2fsck_write_inode(ctx, dp->ino, &inode, "delete_file");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000548}
549
550struct clone_struct {
551 errcode_t errcode;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000552 ino_t dir;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553 char *buf;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000554 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555};
556
557static int clone_file_block(ext2_filsys fs,
558 blk_t *block_nr,
559 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000560 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000561{
562 struct dup_block *p;
563 blk_t new_block;
564 errcode_t retval;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000565 struct clone_struct *cs = (struct clone_struct *) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000566 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000568 ctx = cs->ctx;
569
Theodore Ts'o19178752000-02-11 15:55:07 +0000570 if (HOLE_BLKADDR(*block_nr))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000571 return 0;
572
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000573 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000574 for (p = dup_blk; p; p = p->next_block)
575 if (p->block == *block_nr)
576 break;
577 if (p) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000578 retval = ext2fs_new_block(fs, 0, ctx->block_found_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000579 &new_block);
580 if (retval) {
581 cs->errcode = retval;
582 return BLOCK_ABORT;
583 }
Theodore Ts'o521e3681997-04-29 17:48:10 +0000584 if (cs->dir) {
585 retval = ext2fs_set_dir_block(fs->dblist,
586 cs->dir, new_block, blockcnt);
587 if (retval) {
588 cs->errcode = retval;
589 return BLOCK_ABORT;
590 }
591 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592 retval = io_channel_read_blk(fs->io, *block_nr, 1,
593 cs->buf);
594 if (retval) {
595 cs->errcode = retval;
596 return BLOCK_ABORT;
597 }
598 retval = io_channel_write_blk(fs->io, new_block, 1,
599 cs->buf);
600 if (retval) {
601 cs->errcode = retval;
602 return BLOCK_ABORT;
603 }
604 p->num_bad--;
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000605 if (p->num_bad == 1 &&
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +0000606 !check_if_fs_block(ctx, *block_nr))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000607 ext2fs_unmark_block_bitmap(ctx->block_dup_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000608 *block_nr);
609 *block_nr = new_block;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000610 ext2fs_mark_block_bitmap(ctx->block_found_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000611 new_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000612 ext2fs_mark_block_bitmap(fs->block_map, new_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000613 return BLOCK_CHANGED;
614 } else
615 com_err("clone_file_block", 0,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000616 _("internal error; can't find dup_blk for %d\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000617 *block_nr);
618 }
619 return 0;
620}
621
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000622static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000624 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000625 errcode_t retval;
626 struct clone_struct cs;
627
628 cs.errcode = 0;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000629 cs.dir = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000630 cs.ctx = ctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000631 retval = ext2fs_get_mem(fs->blocksize, (void **) &cs.buf);
632 if (retval)
633 return retval;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000634
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000635 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dp->ino))
Theodore Ts'o521e3681997-04-29 17:48:10 +0000636 cs.dir = dp->ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000637
638 retval = ext2fs_block_iterate(fs, dp->ino, 0, block_buf,
639 clone_file_block, &cs);
640 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000641 ext2fs_free_mem((void **) &cs.buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000642 if (retval) {
643 com_err("clone_file", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000644 _("while calling ext2fs_block_iterate for inode %d"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000645 dp->ino);
646 return retval;
647 }
648 if (cs.errcode) {
Theodore Ts'o622f5f21997-10-24 04:18:21 +0000649 com_err("clone_file", cs.errcode,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000650 _("returned from clone_file_block"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651 return retval;
652 }
653 return 0;
654}
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +0000655
656/*
657 * This routine returns 1 if a block overlaps with one of the superblocks,
658 * group descriptors, inode bitmaps, or block bitmaps.
659 */
660static int check_if_fs_block(e2fsck_t ctx, blk_t test_block)
661{
662 ext2_filsys fs = ctx->fs;
663 blk_t block;
664 int i;
665
666 block = fs->super->s_first_data_block;
667 for (i = 0; i < fs->group_desc_count; i++) {
668
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))
673 return 1;
674 }
675
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)))
681 return 1;
682
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))
686 return 1;
687
688 block += fs->super->s_blocks_per_group;
689 }
690 return 0;
691}