blob: 5aab10905d98c606de2e2b0c7ce8a2f81a780bc8 [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 */
Theodore Ts'o86c627e2001-01-11 15:12:14 +000068 ext2_ino_t ino; /* Inode number */
Theodore Ts'o3839e651997-04-26 13:21:57 +000069 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'o86c627e2001-01-11 15:12:14 +000084 ext2_ino_t ino, dir;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000085 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'o133a56d2000-11-17 05:40:49 +000091 e2_blkcnt_t blockcnt, blk_t ref_blk,
92 int ref_offset, void *priv_data);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000093static void delete_file(e2fsck_t ctx, struct dup_inode *dp,
Theodore Ts'o3839e651997-04-26 13:21:57 +000094 char *block_buf);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000095static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf);
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +000096static int check_if_fs_block(e2fsck_t ctx, blk_t test_blk);
97
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000098static void pass1b(e2fsck_t ctx, char *block_buf);
99static void pass1c(e2fsck_t ctx, char *block_buf);
100static void pass1d(e2fsck_t ctx, char *block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101
102static struct dup_block *dup_blk = 0;
103static struct dup_inode *dup_ino = 0;
104static int dup_inode_count = 0;
105
Theodore Ts'of3db3561997-04-26 13:34:30 +0000106static ext2fs_inode_bitmap inode_dup_map;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107
108/*
109 * Main procedure for handling duplicate blocks
110 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000111void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000112{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000113 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114 struct dup_block *p, *q, *next_p, *next_q;
115 struct dup_inode *r, *next_r;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000116 struct problem_context pctx;
117
118 clear_problem_context(&pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000119
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000120 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000121 _("multiply claimed inode map"), &inode_dup_map);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000122 if (pctx.errcode) {
123 fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000124 ctx->flags |= E2F_FLAG_ABORT;
125 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000126 }
127
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000128 pass1b(ctx, block_buf);
129 pass1c(ctx, block_buf);
130 pass1d(ctx, block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000131
132 /*
133 * Time to free all of the accumulated data structures that we
134 * don't need anymore.
135 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000136 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;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138 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;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000142 ext2fs_free_mem((void **) &q);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000143 }
144 }
145 for (r = dup_ino; r; r = next_r) {
146 next_r = r->next;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000147 ext2fs_free_mem((void **) &r);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000148 }
149}
150
151/*
152 * Scan the inodes looking for inodes that contain duplicate blocks.
153 */
154struct process_block_struct {
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000155 ext2_ino_t ino;
156 int dup_blocks;
157 e2fsck_t ctx;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000158 struct problem_context *pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000159};
160
Theodore Ts'o08b21301997-11-03 19:42:40 +0000161static void pass1b(e2fsck_t ctx, char *block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000162{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000163 ext2_filsys fs = ctx->fs;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000164 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165 struct ext2_inode inode;
166 ext2_inode_scan scan;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000167 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'o133a56d2000-11-17 05:40:49 +0000190 pctx.str = "pass1b";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000191 while (ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000192 pctx.ino = ctx->stashed_ino = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193 if ((ino != EXT2_BAD_INO) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000194 (!ext2fs_test_inode_bitmap(ctx->inode_used_map, ino) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000195 !ext2fs_inode_has_valid_blocks(&inode)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000196 goto next;
197
198 pb.ino = ino;
199 pb.dup_blocks = 0;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000200 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000201 process_pass1b_block, &pb);
202 if (pb.dup_blocks) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000203 end_problem_latch(ctx, PR_LATCH_DBLOCK);
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000204 dp = (struct dup_inode *) e2fsck_allocate_memory(ctx,
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000205 sizeof(struct dup_inode),
206 "duplicate inode record");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 dp->ino = ino;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000208 dp->dir = 0;
209 dp->inode = inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000210 dp->num_dupblocks = pb.dup_blocks;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211 dp->next = dup_ino;
212 dup_ino = dp;
213 if (ino != EXT2_BAD_INO)
214 dup_inode_count++;
215 }
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000216 if (pctx.errcode)
217 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218 next:
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000219 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
220 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000221 goto next;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000222 if (pctx.errcode) {
223 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000224 ctx->flags |= E2F_FLAG_ABORT;
225 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226 }
227 }
228 ext2fs_close_inode_scan(scan);
229 fs->get_blocks = 0;
230 fs->check_directory = 0;
231}
232
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000233static int process_pass1b_block(ext2_filsys fs,
234 blk_t *block_nr,
235 e2_blkcnt_t blockcnt,
236 blk_t ref_blk,
237 int ref_offset,
238 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239{
240 struct process_block_struct *p;
241 struct dup_block *dp, *q, *r;
242 int i;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000243 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000244
Theodore Ts'o19178752000-02-11 15:55:07 +0000245 if (HOLE_BLKADDR(*block_nr))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000246 return 0;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000247 p = (struct process_block_struct *) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000248 ctx = p->ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000249
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000250 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000251 /* OK, this is a duplicate block */
252 if (p->ino != EXT2_BAD_INO) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000253 p->pctx->blk = *block_nr;
254 fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000255 }
256 p->dup_blocks++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000257 ext2fs_mark_block_bitmap(ctx->block_dup_map, *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000258 ext2fs_mark_inode_bitmap(inode_dup_map, p->ino);
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000259 dp = (struct dup_block *) e2fsck_allocate_memory(ctx,
260 sizeof(struct dup_block),
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000261 "duplicate block record");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000262 dp->block = *block_nr;
263 dp->ino = p->ino;
264 dp->num_bad = 0;
265 q = dup_blk;
266 while (q) {
267 if (q->block == *block_nr)
268 break;
269 q = q->next_block;
270 }
271 if (q) {
272 dp->next_inode = q->next_inode;
273 q->next_inode = dp;
274 } else {
275 dp->next_block = dup_blk;
276 dup_blk = dp;
277 }
278 }
279 /*
280 * Set the num_bad field
281 */
282 for (q = dup_blk; q; q = q->next_block) {
283 i = 0;
284 for (r = q; r; r = r->next_inode)
285 i++;
286 q->num_bad = i;
287 }
288 return 0;
289}
290
291/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000292 * Pass 1c: Scan directories for inodes with duplicate blocks. This
293 * is used so that we can print pathnames when prompting the user for
294 * what to do.
295 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000296struct search_dir_struct {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000297 int count;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000298 ext2_ino_t first_inode;
299 ext2_ino_t max_inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300};
301
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000302static int search_dirent_proc(ext2_ino_t dir, int entry,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000303 struct ext2_dir_entry *dirent,
304 int offset, int blocksize,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000305 char *buf, void *priv_data)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000306{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000307 struct search_dir_struct *sd;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000308 struct dup_inode *p;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000309
310 sd = (struct search_dir_struct *) priv_data;
311
Theodore Ts'o521e3681997-04-29 17:48:10 +0000312 if (dirent->inode > sd->max_inode)
313 /* Should abort this inode, but not everything */
314 return 0;
315
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000316 if (!dirent->inode || (entry < DIRENT_OTHER_FILE) ||
317 !ext2fs_test_inode_bitmap(inode_dup_map, dirent->inode))
318 return 0;
319
320 for (p = dup_ino; p; p = p->next) {
321 if ((p->ino >= sd->first_inode) &&
322 (p->ino == dirent->inode))
323 break;
324 }
325
326 if (!p || p->dir)
327 return 0;
328
329 p->dir = dir;
330 sd->count--;
331
332 return(sd->count ? 0 : DIRENT_ABORT);
333}
334
335
Theodore Ts'o08b21301997-11-03 19:42:40 +0000336static void pass1c(e2fsck_t ctx, char *block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000337{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000338 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000339 struct dup_inode *p;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000340 int inodes_left = dup_inode_count;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000341 struct search_dir_struct sd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000342 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000343
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000344 clear_problem_context(&pctx);
345
346 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000347
348 /*
349 * First check to see if any of the inodes with dup blocks is
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000350 * a special inode. (Note that the bad block inode isn't
351 * counted.)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000352 */
353 for (p = dup_ino; p; p = p->next) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000354 if ((p->ino < EXT2_FIRST_INODE(fs->super)) &&
355 (p->ino != EXT2_BAD_INO))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000356 inodes_left--;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000357 }
358
359 /*
360 * Search through all directories to translate inodes to names
361 * (by searching for the containing directory for that inode.)
362 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000363 sd.count = inodes_left;
364 sd.first_inode = EXT2_FIRST_INODE(fs->super);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000365 sd.max_inode = fs->super->s_inodes_count;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000366 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
367 search_dirent_proc, &sd);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000368}
369
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000370static void pass1d(e2fsck_t ctx, char *block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000371{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000372 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000373 struct dup_inode *p, *s;
374 struct dup_block *q, *r;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000375 ext2_ino_t *shared;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000376 int shared_len;
377 int i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000378 int file_ok;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000379 int meta_data = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000380 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000381
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000382 clear_problem_context(&pctx);
383
384 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000385 e2fsck_read_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000386
387 pctx.num = dup_inode_count;
388 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000389 shared = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
390 sizeof(ext2_ino_t) * dup_inode_count,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000391 "Shared inode list");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392 for (p = dup_ino; p; p = p->next) {
393 shared_len = 0;
394 file_ok = 1;
395 if (p->ino == EXT2_BAD_INO)
396 continue;
397
398 /*
399 * Search through the duplicate records to see which
400 * inodes share blocks with this one
401 */
402 for (q = dup_blk; q; q = q->next_block) {
403 /*
404 * See if this block is used by this inode.
405 * If it isn't, continue.
406 */
407 for (r = q; r; r = r->next_inode)
408 if (r->ino == p->ino)
409 break;
410 if (!r)
411 continue;
412 if (q->num_bad > 1)
413 file_ok = 0;
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +0000414 if (check_if_fs_block(ctx, q->block)) {
Theodore Ts'o521e3681997-04-29 17:48:10 +0000415 file_ok = 0;
416 meta_data = 1;
417 }
418
Theodore Ts'o3839e651997-04-26 13:21:57 +0000419 /*
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
423 * add it again.
424 */
425 for (r = q; r; r = r->next_inode) {
426 if (r->ino == p->ino)
427 continue;
428 for (i = 0; i < shared_len; i++)
429 if (shared[i] == r->ino)
430 break;
431 if (i == shared_len) {
432 shared[shared_len++] = r->ino;
433 }
434 }
435 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000436
437 /*
438 * Report the inode that we are working on
439 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000440 pctx.inode = &p->inode;
441 pctx.ino = p->ino;
442 pctx.dir = p->dir;
443 pctx.blkcount = p->num_dupblocks;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000444 pctx.num = meta_data ? shared_len+1 : shared_len;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000445 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000446 pctx.blkcount = 0;
447 pctx.num = 0;
448
Theodore Ts'o521e3681997-04-29 17:48:10 +0000449 if (meta_data)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000450 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000451
Theodore Ts'o3839e651997-04-26 13:21:57 +0000452 for (i = 0; i < shared_len; i++) {
453 for (s = dup_ino; s; s = s->next)
454 if (s->ino == shared[i])
455 break;
456 if (!s)
457 continue;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000458 /*
459 * Report the inode that we are sharing with
460 */
461 pctx.inode = &s->inode;
462 pctx.ino = s->ino;
463 pctx.dir = s->dir;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000464 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000465 }
466 if (file_ok) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000467 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000468 continue;
469 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000470 if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
471 pctx.errcode = clone_file(ctx, p, block_buf);
472 if (pctx.errcode)
473 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
474 else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 continue;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000476 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000477 if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
478 delete_file(ctx, p, block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000479 else
480 ext2fs_unmark_valid(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000481 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000482 ext2fs_free_mem((void **) &shared);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000483}
484
485static int delete_file_block(ext2_filsys fs,
486 blk_t *block_nr,
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000487 e2_blkcnt_t blockcnt,
488 blk_t ref_block,
489 int ref_offset,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000490 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000491{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000492 struct process_block_struct *pb;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000493 struct dup_block *p;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000494 e2fsck_t ctx;
495
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000496 pb = (struct process_block_struct *) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000497 ctx = pb->ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498
Theodore Ts'o19178752000-02-11 15:55:07 +0000499 if (HOLE_BLKADDR(*block_nr))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000500 return 0;
501
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000502 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503 for (p = dup_blk; p; p = p->next_block)
504 if (p->block == *block_nr)
505 break;
506 if (p) {
507 p->num_bad--;
508 if (p->num_bad == 1)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000509 ext2fs_unmark_block_bitmap(ctx->block_dup_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510 *block_nr);
511 } else
512 com_err("delete_file_block", 0,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000513 _("internal error; can't find dup_blk for %d\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514 *block_nr);
515 } else {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000516 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000517 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518 }
519
520 return 0;
521}
522
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000523static void delete_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000525 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 struct process_block_struct pb;
527 struct ext2_inode inode;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000528 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000529
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000530 clear_problem_context(&pctx);
531 pctx.ino = pb.ino = dp->ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532 pb.dup_blocks = dp->num_dupblocks;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000533 pb.ctx = ctx;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000534 pctx.str = "delete_file";
535
536 pctx.errcode = ext2fs_block_iterate2(fs, dp->ino, 0, block_buf,
537 delete_file_block, &pb);
538 if (pctx.errcode)
539 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000540 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);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000544 ext2fs_unmark_inode_bitmap(fs->inode_map, dp->ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 ext2fs_mark_ib_dirty(fs);
546 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000547 e2fsck_read_inode(ctx, dp->ino, &inode, "delete_file");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000548 inode.i_links_count = 0;
549 inode.i_dtime = time(0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000550 e2fsck_write_inode(ctx, dp->ino, &inode, "delete_file");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000551}
552
553struct clone_struct {
554 errcode_t errcode;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000555 ext2_ino_t dir;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000556 char *buf;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000557 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000558};
559
560static int clone_file_block(ext2_filsys fs,
561 blk_t *block_nr,
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000562 e2_blkcnt_t blockcnt,
563 blk_t ref_block,
564 int ref_offset,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000565 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000566{
567 struct dup_block *p;
568 blk_t new_block;
569 errcode_t retval;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000570 struct clone_struct *cs = (struct clone_struct *) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000571 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000572
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000573 ctx = cs->ctx;
574
Theodore Ts'o19178752000-02-11 15:55:07 +0000575 if (HOLE_BLKADDR(*block_nr))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 return 0;
577
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000578 if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000579 for (p = dup_blk; p; p = p->next_block)
580 if (p->block == *block_nr)
581 break;
582 if (p) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000583 retval = ext2fs_new_block(fs, 0, ctx->block_found_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000584 &new_block);
585 if (retval) {
586 cs->errcode = retval;
587 return BLOCK_ABORT;
588 }
Theodore Ts'o521e3681997-04-29 17:48:10 +0000589 if (cs->dir) {
590 retval = ext2fs_set_dir_block(fs->dblist,
591 cs->dir, new_block, blockcnt);
592 if (retval) {
593 cs->errcode = retval;
594 return BLOCK_ABORT;
595 }
596 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 retval = io_channel_read_blk(fs->io, *block_nr, 1,
598 cs->buf);
599 if (retval) {
600 cs->errcode = retval;
601 return BLOCK_ABORT;
602 }
603 retval = io_channel_write_blk(fs->io, new_block, 1,
604 cs->buf);
605 if (retval) {
606 cs->errcode = retval;
607 return BLOCK_ABORT;
608 }
609 p->num_bad--;
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000610 if (p->num_bad == 1 &&
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +0000611 !check_if_fs_block(ctx, *block_nr))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000612 ext2fs_unmark_block_bitmap(ctx->block_dup_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000613 *block_nr);
614 *block_nr = new_block;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000615 ext2fs_mark_block_bitmap(ctx->block_found_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616 new_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000617 ext2fs_mark_block_bitmap(fs->block_map, new_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000618 return BLOCK_CHANGED;
619 } else
620 com_err("clone_file_block", 0,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000621 _("internal error; can't find dup_blk for %d\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000622 *block_nr);
623 }
624 return 0;
625}
626
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000627static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000629 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000630 errcode_t retval;
631 struct clone_struct cs;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000632 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000633
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000634 clear_problem_context(&pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000635 cs.errcode = 0;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000636 cs.dir = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000637 cs.ctx = ctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000638 retval = ext2fs_get_mem(fs->blocksize, (void **) &cs.buf);
639 if (retval)
640 return retval;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000641
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000642 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dp->ino))
Theodore Ts'o521e3681997-04-29 17:48:10 +0000643 cs.dir = dp->ino;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000644
645 pctx.ino = dp->ino;
646 pctx.str = "clone_file";
647 pctx.errcode = ext2fs_block_iterate2(fs, dp->ino, 0, block_buf,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000648 clone_file_block, &cs);
649 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000650 ext2fs_free_mem((void **) &cs.buf);
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000651 if (pctx.errcode) {
652 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
653 return pctx.errcode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000654 }
655 if (cs.errcode) {
Theodore Ts'o622f5f21997-10-24 04:18:21 +0000656 com_err("clone_file", cs.errcode,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000657 _("returned from clone_file_block"));
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000658 return cs.errcode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000659 }
660 return 0;
661}
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +0000662
663/*
664 * This routine returns 1 if a block overlaps with one of the superblocks,
665 * group descriptors, inode bitmaps, or block bitmaps.
666 */
667static int check_if_fs_block(e2fsck_t ctx, blk_t test_block)
668{
669 ext2_filsys fs = ctx->fs;
670 blk_t block;
671 int i;
672
673 block = fs->super->s_first_data_block;
674 for (i = 0; i < fs->group_desc_count; i++) {
675
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))
680 return 1;
681 }
682
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)))
688 return 1;
689
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))
693 return 1;
694
695 block += fs->super->s_blocks_per_group;
696 }
697 return 0;
698}