]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/pass1b.c
libext2fs: don't break when ext2fs_clear_generic_bmap() for 32-bit bitmaps
[thirdparty/e2fsprogs.git] / e2fsck / pass1b.c
CommitLineData
3839e657
TT
1/*
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.
efc6f628 7 *
3839e657
TT
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.
efc6f628 20 *
21c84b71
TT
21 * 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%
efc6f628 27 *
3839e657
TT
28 */
29
d1154eb4 30#include "config.h"
3839e657 31#include <time.h>
50e1e10f
TT
32#ifdef HAVE_ERRNO_H
33#include <errno.h>
34#endif
3839e657 35
0c193f82
TT
36#ifdef HAVE_INTTYPES_H
37#include <inttypes.h>
38#endif
39
d2ee56d8 40#ifndef HAVE_INTPTR_T
9c07dc00 41typedef long intptr_t;
d2ee56d8
TT
42#endif
43
0c193f82
TT
44/* Needed for architectures where sizeof(int) != sizeof(void *) */
45#define INT_TO_VOIDPTR(val) ((void *)(intptr_t)(val))
46#define VOIDPTR_TO_INT(ptr) ((int)(intptr_t)(ptr))
47
3839e657
TT
48#include <et/com_err.h>
49#include "e2fsck.h"
50
21c84b71 51#include "problem.h"
838e773e 52#include "dict.h"
21c84b71 53
342d847d
TT
54/* Define an extension to the ext2 library's block count information */
55#define BLOCK_COUNT_EXTATTR (-5)
56
f51b4d33
TT
57struct cluster_el {
58 blk64_t cluster;
59 struct cluster_el *next;
838e773e
TT
60};
61
62struct inode_el {
63 ext2_ino_t inode;
64 struct inode_el *next;
65};
66
f51b4d33 67struct dup_cluster {
3839e657 68 int num_bad;
838e773e 69 struct inode_el *inode_list;
3839e657
TT
70};
71
72/*
73 * This structure stores information about a particular inode which
74 * is sharing blocks with other inodes. This information is collected
75 * to display to the user, so that the user knows what files he or she
76 * is dealing with, when trying to decide how to resolve the conflict
77 * of multiply-claimed blocks.
78 */
79struct dup_inode {
838e773e 80 ext2_ino_t dir;
21c84b71
TT
81 int num_dupblocks;
82 struct ext2_inode inode;
f51b4d33 83 struct cluster_el *cluster_list;
3839e657
TT
84};
85
a63745e8
VAH
86static int process_pass1b_block(ext2_filsys fs, blk64_t *blocknr,
87 e2_blkcnt_t blockcnt, blk64_t ref_blk,
133a56dc 88 int ref_offset, void *priv_data);
838e773e
TT
89static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
90 struct dup_inode *dp, char *block_buf);
91static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
92 struct dup_inode *dp, char* block_buf);
f51b4d33
TT
93static int check_if_fs_block(e2fsck_t ctx, blk64_t test_block);
94static int check_if_fs_cluster(e2fsck_t ctx, blk64_t cluster);
80c5d7e4 95
1b6bf175
TT
96static void pass1b(e2fsck_t ctx, char *block_buf);
97static void pass1c(e2fsck_t ctx, char *block_buf);
98static void pass1d(e2fsck_t ctx, char *block_buf);
3839e657 99
3839e657 100static int dup_inode_count = 0;
81cae650 101static int dup_inode_founddir = 0;
3839e657 102
f51b4d33 103static dict_t clstr_dict, ino_dict;
838e773e 104
f3db3566 105static ext2fs_inode_bitmap inode_dup_map;
3839e657 106
838e773e
TT
107static int dict_int_cmp(const void *a, const void *b)
108{
0c193f82 109 intptr_t ia, ib;
838e773e 110
0c193f82
TT
111 ia = (intptr_t)a;
112 ib = (intptr_t)b;
838e773e
TT
113
114 return (ia-ib);
115}
116
117/*
118 * Add a duplicate block record
119 */
f51b4d33 120static void add_dupe(e2fsck_t ctx, ext2_ino_t ino, blk64_t cluster,
838e773e
TT
121 struct ext2_inode *inode)
122{
123 dnode_t *n;
f51b4d33 124 struct dup_cluster *db;
838e773e 125 struct dup_inode *di;
f51b4d33 126 struct cluster_el *cluster_el;
838e773e
TT
127 struct inode_el *ino_el;
128
f51b4d33 129 n = dict_lookup(&clstr_dict, INT_TO_VOIDPTR(cluster));
838e773e 130 if (n)
f51b4d33 131 db = (struct dup_cluster *) dnode_get(n);
838e773e 132 else {
f51b4d33
TT
133 db = (struct dup_cluster *) e2fsck_allocate_memory(ctx,
134 sizeof(struct dup_cluster), "duplicate cluster header");
838e773e
TT
135 db->num_bad = 0;
136 db->inode_list = 0;
f51b4d33 137 dict_alloc_insert(&clstr_dict, INT_TO_VOIDPTR(cluster), db);
838e773e
TT
138 }
139 ino_el = (struct inode_el *) e2fsck_allocate_memory(ctx,
140 sizeof(struct inode_el), "inode element");
141 ino_el->inode = ino;
142 ino_el->next = db->inode_list;
143 db->inode_list = ino_el;
144 db->num_bad++;
145
0c193f82 146 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino));
838e773e
TT
147 if (n)
148 di = (struct dup_inode *) dnode_get(n);
149 else {
150 di = (struct dup_inode *) e2fsck_allocate_memory(ctx,
151 sizeof(struct dup_inode), "duplicate inode header");
81cae650
JG
152 if (ino == EXT2_ROOT_INO) {
153 di->dir = EXT2_ROOT_INO;
154 dup_inode_founddir++;
155 } else
156 di->dir = 0;
157
838e773e 158 di->num_dupblocks = 0;
f51b4d33 159 di->cluster_list = 0;
838e773e 160 di->inode = *inode;
0c193f82 161 dict_alloc_insert(&ino_dict, INT_TO_VOIDPTR(ino), di);
838e773e 162 }
f51b4d33
TT
163 cluster_el = (struct cluster_el *) e2fsck_allocate_memory(ctx,
164 sizeof(struct cluster_el), "cluster element");
165 cluster_el->cluster = cluster;
166 cluster_el->next = di->cluster_list;
167 di->cluster_list = cluster_el;
838e773e
TT
168 di->num_dupblocks++;
169}
170
171/*
172 * Free a duplicate inode record
173 */
efc6f628 174static void inode_dnode_free(dnode_t *node,
54434927 175 void *context EXT2FS_ATTR((unused)))
838e773e
TT
176{
177 struct dup_inode *di;
f51b4d33 178 struct cluster_el *p, *next;
838e773e
TT
179
180 di = (struct dup_inode *) dnode_get(node);
f51b4d33 181 for (p = di->cluster_list; p; p = next) {
838e773e
TT
182 next = p->next;
183 free(p);
184 }
23f75f6e 185 free(di);
838e773e
TT
186 free(node);
187}
188
189/*
f51b4d33 190 * Free a duplicate cluster record
838e773e 191 */
f51b4d33
TT
192static void cluster_dnode_free(dnode_t *node,
193 void *context EXT2FS_ATTR((unused)))
838e773e 194{
f51b4d33 195 struct dup_cluster *dc;
838e773e
TT
196 struct inode_el *p, *next;
197
f51b4d33
TT
198 dc = (struct dup_cluster *) dnode_get(node);
199 for (p = dc->inode_list; p; p = next) {
838e773e
TT
200 next = p->next;
201 free(p);
202 }
f51b4d33 203 free(dc);
838e773e
TT
204 free(node);
205}
206
207
3839e657
TT
208/*
209 * Main procedure for handling duplicate blocks
210 */
08b21301 211void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
3839e657 212{
1b6bf175 213 ext2_filsys fs = ctx->fs;
1b6bf175 214 struct problem_context pctx;
fea390e2
KC
215#ifdef RESOURCE_TRACK
216 struct resource_track rtrack;
217#endif
1b6bf175
TT
218
219 clear_problem_context(&pctx);
efc6f628 220
1b6bf175 221 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
0c4a0726 222 _("multiply claimed inode map"), &inode_dup_map);
1b6bf175
TT
223 if (pctx.errcode) {
224 fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
f8188fff
TT
225 ctx->flags |= E2F_FLAG_ABORT;
226 return;
3839e657 227 }
838e773e
TT
228
229 dict_init(&ino_dict, DICTCOUNT_T_MAX, dict_int_cmp);
f51b4d33 230 dict_init(&clstr_dict, DICTCOUNT_T_MAX, dict_int_cmp);
838e773e 231 dict_set_allocator(&ino_dict, NULL, inode_dnode_free, NULL);
f51b4d33 232 dict_set_allocator(&clstr_dict, NULL, cluster_dnode_free, NULL);
efc6f628 233
fea390e2 234 init_resource_track(&rtrack, ctx->fs->io);
1b6bf175 235 pass1b(ctx, block_buf);
9facd076 236 print_resource_track(ctx, "Pass 1b", &rtrack, ctx->fs->io);
fea390e2 237
fea390e2 238 init_resource_track(&rtrack, ctx->fs->io);
1b6bf175 239 pass1c(ctx, block_buf);
9facd076 240 print_resource_track(ctx, "Pass 1c", &rtrack, ctx->fs->io);
fea390e2 241
fea390e2 242 init_resource_track(&rtrack, ctx->fs->io);
1b6bf175 243 pass1d(ctx, block_buf);
9facd076 244 print_resource_track(ctx, "Pass 1d", &rtrack, ctx->fs->io);
fea390e2 245
3839e657
TT
246 /*
247 * Time to free all of the accumulated data structures that we
248 * don't need anymore.
249 */
838e773e 250 dict_free_nodes(&ino_dict);
f51b4d33 251 dict_free_nodes(&clstr_dict);
23f75f6e 252 ext2fs_free_inode_bitmap(inode_dup_map);
3839e657
TT
253}
254
255/*
256 * Scan the inodes looking for inodes that contain duplicate blocks.
257 */
258struct process_block_struct {
838e773e 259 e2fsck_t ctx;
86c627ec
TT
260 ext2_ino_t ino;
261 int dup_blocks;
b23f2f4d 262 blk64_t cur_cluster;
838e773e 263 struct ext2_inode *inode;
1b6bf175 264 struct problem_context *pctx;
3839e657
TT
265};
266
08b21301 267static void pass1b(e2fsck_t ctx, char *block_buf)
3839e657 268{
1b6bf175 269 ext2_filsys fs = ctx->fs;
86c627ec 270 ext2_ino_t ino;
3839e657
TT
271 struct ext2_inode inode;
272 ext2_inode_scan scan;
3839e657 273 struct process_block_struct pb;
1b6bf175 274 struct problem_context pctx;
efc6f628 275
1b6bf175 276 clear_problem_context(&pctx);
efc6f628 277
151786fc
TT
278 if (!(ctx->options & E2F_OPT_PREEN))
279 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
1b6bf175
TT
280 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
281 &scan);
282 if (pctx.errcode) {
283 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
f8188fff
TT
284 ctx->flags |= E2F_FLAG_ABORT;
285 return;
3839e657 286 }
1b6bf175
TT
287 ctx->stashed_inode = &inode;
288 pb.ctx = ctx;
289 pb.pctx = &pctx;
133a56dc 290 pctx.str = "pass1b";
d237a78e 291 while (1) {
0f5eba75
AD
292 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
293 if (e2fsck_mmp_update(fs))
294 fatal_error(ctx, 0);
295 }
d237a78e
TT
296 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
297 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
298 continue;
299 if (pctx.errcode) {
300 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
301 ctx->flags |= E2F_FLAG_ABORT;
302 return;
303 }
304 if (!ino)
305 break;
1b6bf175 306 pctx.ino = ctx->stashed_ino = ino;
3839e657 307 if ((ino != EXT2_BAD_INO) &&
c5d2f50d 308 !ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino))
d237a78e 309 continue;
3839e657
TT
310
311 pb.ino = ino;
312 pb.dup_blocks = 0;
838e773e 313 pb.inode = &inode;
b23f2f4d 314 pb.cur_cluster = ~0;
0684a4f3 315
0c80c44b 316 if (ext2fs_inode_has_valid_blocks2(fs, &inode) ||
0684a4f3 317 (ino == EXT2_BAD_INO))
a63745e8 318 pctx.errcode = ext2fs_block_iterate3(fs, ino,
15d482ba
TT
319 BLOCK_FLAG_READ_ONLY, block_buf,
320 process_pass1b_block, &pb);
7501ce3e
ES
321 /* If the feature is not set, attrs will be cleared later anyway */
322 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) &&
0c80c44b
TT
323 ext2fs_file_acl_block(fs, &inode)) {
324 blk64_t blk = ext2fs_file_acl_block(fs, &inode);
a63745e8 325 process_pass1b_block(fs, &blk,
342d847d 326 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
0c80c44b 327 ext2fs_file_acl_block_set(fs, &inode, blk);
7501ce3e 328 }
3839e657 329 if (pb.dup_blocks) {
1b6bf175 330 end_problem_latch(ctx, PR_LATCH_DBLOCK);
838e773e
TT
331 if (ino >= EXT2_FIRST_INODE(fs->super) ||
332 ino == EXT2_ROOT_INO)
3839e657
TT
333 dup_inode_count++;
334 }
133a56dc
TT
335 if (pctx.errcode)
336 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
3839e657
TT
337 }
338 ext2fs_close_inode_scan(scan);
71d521c6 339 e2fsck_use_inode_shortcuts(ctx, 0);
3839e657
TT
340}
341
54434927 342static int process_pass1b_block(ext2_filsys fs EXT2FS_ATTR((unused)),
a63745e8 343 blk64_t *block_nr,
b23f2f4d 344 e2_blkcnt_t blockcnt,
a63745e8 345 blk64_t ref_blk EXT2FS_ATTR((unused)),
54434927 346 int ref_offset EXT2FS_ATTR((unused)),
53ef44c4 347 void *priv_data)
3839e657
TT
348{
349 struct process_block_struct *p;
1b6bf175 350 e2fsck_t ctx;
b23f2f4d 351 blk64_t lc;
3839e657 352
1917875f 353 if (HOLE_BLKADDR(*block_nr))
3839e657 354 return 0;
54dc7ca2 355 p = (struct process_block_struct *) priv_data;
1b6bf175 356 ctx = p->ctx;
b23f2f4d 357 lc = EXT2FS_B2C(fs, blockcnt);
efc6f628 358
c5d2f50d 359 if (!ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr))
b23f2f4d 360 goto finish;
efc6f628 361
838e773e
TT
362 /* OK, this is a duplicate block */
363 if (p->ino != EXT2_BAD_INO) {
364 p->pctx->blk = *block_nr;
365 fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
3839e657 366 }
838e773e 367 p->dup_blocks++;
c5d2f50d 368 ext2fs_mark_inode_bitmap2(inode_dup_map, p->ino);
838e773e 369
b23f2f4d
TT
370 if (lc != p->cur_cluster)
371 add_dupe(ctx, p->ino, EXT2FS_B2C(fs, *block_nr), p->inode);
efc6f628 372
b23f2f4d
TT
373finish:
374 p->cur_cluster = lc;
3839e657
TT
375 return 0;
376}
377
3839e657
TT
378/*
379 * Pass 1c: Scan directories for inodes with duplicate blocks. This
380 * is used so that we can print pathnames when prompting the user for
381 * what to do.
382 */
21c84b71 383struct search_dir_struct {
3839e657 384 int count;
86c627ec
TT
385 ext2_ino_t first_inode;
386 ext2_ino_t max_inode;
3839e657
TT
387};
388
86c627ec 389static int search_dirent_proc(ext2_ino_t dir, int entry,
21c84b71 390 struct ext2_dir_entry *dirent,
efc6f628 391 int offset EXT2FS_ATTR((unused)),
54434927 392 int blocksize EXT2FS_ATTR((unused)),
efc6f628 393 char *buf EXT2FS_ATTR((unused)),
54434927 394 void *priv_data)
21c84b71 395{
54dc7ca2 396 struct search_dir_struct *sd;
21c84b71 397 struct dup_inode *p;
838e773e 398 dnode_t *n;
54dc7ca2
TT
399
400 sd = (struct search_dir_struct *) priv_data;
401
521e3685
TT
402 if (dirent->inode > sd->max_inode)
403 /* Should abort this inode, but not everything */
efc6f628 404 return 0;
521e3685 405
838e773e 406 if ((dirent->inode < sd->first_inode) || (entry < DIRENT_OTHER_FILE) ||
c5d2f50d 407 !ext2fs_test_inode_bitmap2(inode_dup_map, dirent->inode))
21c84b71
TT
408 return 0;
409
0c193f82 410 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(dirent->inode));
838e773e 411 if (!n)
21c84b71 412 return 0;
838e773e 413 p = (struct dup_inode *) dnode_get(n);
3d51ff87
JG
414 if (!p->dir) {
415 p->dir = dir;
416 sd->count--;
417 }
21c84b71
TT
418
419 return(sd->count ? 0 : DIRENT_ABORT);
420}
421
422
08b21301 423static void pass1c(e2fsck_t ctx, char *block_buf)
3839e657 424{
1b6bf175 425 ext2_filsys fs = ctx->fs;
21c84b71 426 struct search_dir_struct sd;
1b6bf175
TT
427 struct problem_context pctx;
428
429 clear_problem_context(&pctx);
3839e657 430
151786fc
TT
431 if (!(ctx->options & E2F_OPT_PREEN))
432 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
3839e657 433
3839e657
TT
434 /*
435 * Search through all directories to translate inodes to names
436 * (by searching for the containing directory for that inode.)
437 */
81cae650 438 sd.count = dup_inode_count - dup_inode_founddir;
21c84b71 439 sd.first_inode = EXT2_FIRST_INODE(fs->super);
521e3685 440 sd.max_inode = fs->super->s_inodes_count;
21c84b71
TT
441 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
442 search_dirent_proc, &sd);
efc6f628 443}
3839e657 444
1b6bf175 445static void pass1d(e2fsck_t ctx, char *block_buf)
3839e657 446{
1b6bf175 447 ext2_filsys fs = ctx->fs;
838e773e 448 struct dup_inode *p, *t;
f51b4d33 449 struct dup_cluster *q;
838e773e 450 ext2_ino_t *shared, ino;
3839e657
TT
451 int shared_len;
452 int i;
3839e657 453 int file_ok;
521e3685 454 int meta_data = 0;
21c84b71 455 struct problem_context pctx;
838e773e 456 dnode_t *n, *m;
f51b4d33 457 struct cluster_el *s;
838e773e 458 struct inode_el *r;
efc6f628 459
1b6bf175 460 clear_problem_context(&pctx);
efc6f628 461
151786fc
TT
462 if (!(ctx->options & E2F_OPT_PREEN))
463 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
f8188fff 464 e2fsck_read_bitmaps(ctx);
3839e657 465
838e773e 466 pctx.num = dup_inode_count; /* dict_count(&ino_dict); */
1b6bf175 467 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
86c627ec 468 shared = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
838e773e 469 sizeof(ext2_ino_t) * dict_count(&ino_dict),
54dc7ca2 470 "Shared inode list");
838e773e
TT
471 for (n = dict_first(&ino_dict); n; n = dict_next(&ino_dict, n)) {
472 p = (struct dup_inode *) dnode_get(n);
3839e657
TT
473 shared_len = 0;
474 file_ok = 1;
0c193f82 475 ino = (ext2_ino_t)VOIDPTR_TO_INT(dnode_getkey(n));
5e916143 476 if (ino == EXT2_BAD_INO || ino == EXT2_RESIZE_INO)
3839e657
TT
477 continue;
478
479 /*
838e773e
TT
480 * Find all of the inodes which share blocks with this
481 * one. First we find all of the duplicate blocks
482 * belonging to this inode, and then search each block
483 * get the list of inodes, and merge them together.
3839e657 484 */
f51b4d33
TT
485 for (s = p->cluster_list; s; s = s->next) {
486 m = dict_lookup(&clstr_dict,
487 INT_TO_VOIDPTR(s->cluster));
838e773e
TT
488 if (!m)
489 continue; /* Should never happen... */
f51b4d33 490 q = (struct dup_cluster *) dnode_get(m);
3839e657
TT
491 if (q->num_bad > 1)
492 file_ok = 0;
f51b4d33 493 if (check_if_fs_cluster(ctx, s->cluster)) {
521e3685
TT
494 file_ok = 0;
495 meta_data = 1;
496 }
efc6f628 497
3839e657
TT
498 /*
499 * Add all inodes used by this block to the
500 * shared[] --- which is a unique list, so
501 * if an inode is already in shared[], don't
502 * add it again.
503 */
838e773e
TT
504 for (r = q->inode_list; r; r = r->next) {
505 if (r->inode == ino)
3839e657
TT
506 continue;
507 for (i = 0; i < shared_len; i++)
838e773e 508 if (shared[i] == r->inode)
3839e657
TT
509 break;
510 if (i == shared_len) {
838e773e 511 shared[shared_len++] = r->inode;
3839e657
TT
512 }
513 }
514 }
21c84b71
TT
515
516 /*
517 * Report the inode that we are working on
518 */
21c84b71 519 pctx.inode = &p->inode;
838e773e 520 pctx.ino = ino;
21c84b71
TT
521 pctx.dir = p->dir;
522 pctx.blkcount = p->num_dupblocks;
521e3685 523 pctx.num = meta_data ? shared_len+1 : shared_len;
1b6bf175 524 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
21c84b71
TT
525 pctx.blkcount = 0;
526 pctx.num = 0;
efc6f628 527
521e3685 528 if (meta_data)
1b6bf175 529 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
efc6f628 530
3839e657 531 for (i = 0; i < shared_len; i++) {
0c193f82 532 m = dict_lookup(&ino_dict, INT_TO_VOIDPTR(shared[i]));
838e773e
TT
533 if (!m)
534 continue; /* should never happen */
535 t = (struct dup_inode *) dnode_get(m);
21c84b71
TT
536 /*
537 * Report the inode that we are sharing with
538 */
838e773e
TT
539 pctx.inode = &t->inode;
540 pctx.ino = shared[i];
541 pctx.dir = t->dir;
1b6bf175 542 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
3839e657
TT
543 }
544 if (file_ok) {
1b6bf175 545 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
3839e657
TT
546 continue;
547 }
1b6bf175 548 if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
838e773e 549 pctx.errcode = clone_file(ctx, ino, p, block_buf);
1b6bf175
TT
550 if (pctx.errcode)
551 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
552 else
3839e657 553 continue;
3839e657 554 }
1b6bf175 555 if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
838e773e 556 delete_file(ctx, ino, p, block_buf);
3839e657
TT
557 else
558 ext2fs_unmark_valid(fs);
3839e657 559 }
c4e3d3f3 560 ext2fs_free_mem(&shared);
3839e657
TT
561}
562
7abb2bdc
TT
563/*
564 * Drop the refcount on the dup_block structure, and clear the entry
565 * in the block_dup_map if appropriate.
566 */
f51b4d33
TT
567static void decrement_badcount(e2fsck_t ctx, blk64_t block,
568 struct dup_cluster *p)
7abb2bdc
TT
569{
570 p->num_bad--;
571 if (p->num_bad <= 0 ||
f51b4d33
TT
572 (p->num_bad == 1 && !check_if_fs_block(ctx, block))) {
573 if (check_if_fs_cluster(ctx, EXT2FS_B2C(ctx->fs, block)))
574 return;
c5d2f50d 575 ext2fs_unmark_block_bitmap2(ctx->block_dup_map, block);
f51b4d33 576 }
7abb2bdc
TT
577}
578
3839e657 579static int delete_file_block(ext2_filsys fs,
a63745e8 580 blk64_t *block_nr,
b23f2f4d 581 e2_blkcnt_t blockcnt,
a63745e8 582 blk64_t ref_block EXT2FS_ATTR((unused)),
54434927 583 int ref_offset EXT2FS_ATTR((unused)),
54dc7ca2 584 void *priv_data)
3839e657 585{
54dc7ca2 586 struct process_block_struct *pb;
f51b4d33 587 struct dup_cluster *p;
838e773e 588 dnode_t *n;
1b6bf175 589 e2fsck_t ctx;
b23f2f4d 590 blk64_t c, lc;
1b6bf175 591
54dc7ca2 592 pb = (struct process_block_struct *) priv_data;
1b6bf175 593 ctx = pb->ctx;
3839e657 594
1917875f 595 if (HOLE_BLKADDR(*block_nr))
3839e657
TT
596 return 0;
597
f51b4d33 598 c = EXT2FS_B2C(fs, *block_nr);
b23f2f4d 599 lc = EXT2FS_B2C(fs, blockcnt);
c5d2f50d 600 if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
f51b4d33 601 n = dict_lookup(&clstr_dict, INT_TO_VOIDPTR(c));
838e773e 602 if (n) {
f51b4d33 603 p = (struct dup_cluster *) dnode_get(n);
b23f2f4d
TT
604 if (lc != pb->cur_cluster)
605 decrement_badcount(ctx, *block_nr, p);
3839e657
TT
606 } else
607 com_err("delete_file_block", 0,
a63745e8 608 _("internal error: can't find dup_blk for %llu\n"),
3839e657
TT
609 *block_nr);
610 } else {
c5d2f50d 611 ext2fs_unmark_block_bitmap2(ctx->block_found_map, *block_nr);
48f23054 612 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
624e4a64 613 pb->dup_blocks++;
3839e657 614 }
b23f2f4d 615 pb->cur_cluster = lc;
efc6f628 616
3839e657
TT
617 return 0;
618}
efc6f628 619
838e773e
TT
620static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
621 struct dup_inode *dp, char* block_buf)
3839e657 622{
1b6bf175 623 ext2_filsys fs = ctx->fs;
3839e657
TT
624 struct process_block_struct pb;
625 struct ext2_inode inode;
133a56dc 626 struct problem_context pctx;
0684a4f3 627 unsigned int count;
3839e657 628
133a56dc 629 clear_problem_context(&pctx);
838e773e 630 pctx.ino = pb.ino = ino;
624e4a64 631 pb.dup_blocks = 0;
1b6bf175 632 pb.ctx = ctx;
133a56dc 633 pctx.str = "delete_file";
b23f2f4d 634 pb.cur_cluster = ~0;
133a56dc 635
0684a4f3 636 e2fsck_read_inode(ctx, ino, &inode, "delete_file");
0c80c44b 637 if (ext2fs_inode_has_valid_blocks2(fs, &inode))
a63745e8 638 pctx.errcode = ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_READ_ONLY,
15d482ba 639 block_buf, delete_file_block, &pb);
133a56dc
TT
640 if (pctx.errcode)
641 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
1b6bf175 642 if (ctx->inode_bad_map)
c5d2f50d 643 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
0684a4f3 644 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
624e4a64
AK
645 quota_data_sub(ctx->qctx, &inode, ino, pb.dup_blocks * fs->blocksize);
646 quota_data_inodes(ctx->qctx, &inode, ino, -1);
0684a4f3
TT
647
648 /* Inode may have changed by block_iterate, so reread it */
838e773e 649 e2fsck_read_inode(ctx, ino, &inode, "delete_file");
e3df15ab 650 e2fsck_clear_inode(ctx, ino, &inode, 0, "delete_file");
0c80c44b 651 if (ext2fs_file_acl_block(fs, &inode) &&
0684a4f3
TT
652 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
653 count = 1;
6dc64392 654 pctx.errcode = ext2fs_adjust_ea_refcount2(fs,
0c80c44b 655 ext2fs_file_acl_block(fs, &inode),
0684a4f3
TT
656 block_buf, -1, &count);
657 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
658 pctx.errcode = 0;
659 count = 1;
660 }
661 if (pctx.errcode) {
0c80c44b 662 pctx.blk = ext2fs_file_acl_block(fs, &inode);
0684a4f3
TT
663 fix_problem(ctx, PR_1B_ADJ_EA_REFCOUNT, &pctx);
664 }
665 /*
666 * If the count is zero, then arrange to have the
667 * block deleted. If the block is in the block_dup_map,
668 * also call delete_file_block since it will take care
669 * of keeping the accounting straight.
670 */
671 if ((count == 0) ||
c5d2f50d 672 ext2fs_test_block_bitmap2(ctx->block_dup_map,
0c80c44b
TT
673 ext2fs_file_acl_block(fs, &inode))) {
674 blk64_t blk = ext2fs_file_acl_block(fs, &inode);
a63745e8 675 delete_file_block(fs, &blk,
0684a4f3 676 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
0c80c44b 677 ext2fs_file_acl_block_set(fs, &inode, blk);
624e4a64 678 quota_data_sub(ctx->qctx, &inode, ino, fs->blocksize);
a63745e8 679 }
0684a4f3 680 }
3839e657
TT
681}
682
683struct clone_struct {
684 errcode_t errcode;
f51b4d33
TT
685 blk64_t dup_cluster;
686 blk64_t alloc_block;
86c627ec 687 ext2_ino_t dir;
3839e657 688 char *buf;
1b6bf175 689 e2fsck_t ctx;
3839e657
TT
690};
691
692static int clone_file_block(ext2_filsys fs,
a63745e8 693 blk64_t *block_nr,
133a56dc 694 e2_blkcnt_t blockcnt,
a63745e8 695 blk64_t ref_block EXT2FS_ATTR((unused)),
54434927 696 int ref_offset EXT2FS_ATTR((unused)),
54dc7ca2 697 void *priv_data)
3839e657 698{
f51b4d33 699 struct dup_cluster *p;
c5d2f50d 700 blk64_t new_block;
3839e657 701 errcode_t retval;
54dc7ca2 702 struct clone_struct *cs = (struct clone_struct *) priv_data;
838e773e 703 dnode_t *n;
1b6bf175 704 e2fsck_t ctx;
f51b4d33
TT
705 blk64_t c;
706 int is_meta = 0;
3839e657 707
1b6bf175 708 ctx = cs->ctx;
efc6f628 709
1917875f 710 if (HOLE_BLKADDR(*block_nr))
3839e657
TT
711 return 0;
712
f51b4d33
TT
713 c = EXT2FS_B2C(fs, blockcnt);
714 if (check_if_fs_cluster(ctx, EXT2FS_B2C(fs, *block_nr)))
715 is_meta = 1;
716
b23f2f4d
TT
717 if (c == cs->dup_cluster && cs->alloc_block) {
718 new_block = cs->alloc_block;
719 goto got_block;
720 }
721
722 if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
f51b4d33
TT
723 n = dict_lookup(&clstr_dict,
724 INT_TO_VOIDPTR(EXT2FS_B2C(fs, *block_nr)));
725 if (!n) {
726 com_err("clone_file_block", 0,
727 _("internal error: can't find dup_blk for %llu\n"),
728 *block_nr);
729 return 0;
730 }
731
732 p = (struct dup_cluster *) dnode_get(n);
733 if (!is_meta)
734 decrement_badcount(ctx, *block_nr, p);
735
f51b4d33
TT
736 cs->dup_cluster = c;
737
738 retval = ext2fs_new_block2(fs, 0, ctx->block_found_map,
739 &new_block);
740 if (retval) {
741 cs->errcode = retval;
742 return BLOCK_ABORT;
743 }
744 cs->alloc_block = new_block;
745
746 got_block:
747 new_block &= ~EXT2FS_CLUSTER_MASK(fs);
748 new_block += EXT2FS_CLUSTER_MASK(fs) & blockcnt;
749 if (cs->dir && (blockcnt >= 0)) {
750 retval = ext2fs_set_dir_block2(fs->dblist,
751 cs->dir, new_block, blockcnt);
3839e657
TT
752 if (retval) {
753 cs->errcode = retval;
754 return BLOCK_ABORT;
755 }
f51b4d33 756 }
7b63fff9 757#if 0
f51b4d33
TT
758 printf("Cloning block #%lld from %llu to %llu\n",
759 blockcnt, *block_nr, new_block);
7b63fff9 760#endif
f51b4d33
TT
761 retval = io_channel_read_blk64(fs->io, *block_nr, 1, cs->buf);
762 if (retval) {
763 cs->errcode = retval;
764 return BLOCK_ABORT;
765 }
766 retval = io_channel_write_blk64(fs->io, new_block, 1, cs->buf);
767 if (retval) {
768 cs->errcode = retval;
769 return BLOCK_ABORT;
770 }
771 *block_nr = new_block;
772 ext2fs_mark_block_bitmap2(ctx->block_found_map, new_block);
773 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
774 return BLOCK_CHANGED;
3839e657
TT
775 }
776 return 0;
777}
efc6f628 778
838e773e
TT
779static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
780 struct dup_inode *dp, char* block_buf)
3839e657 781{
1b6bf175 782 ext2_filsys fs = ctx->fs;
3839e657
TT
783 errcode_t retval;
784 struct clone_struct cs;
133a56dc 785 struct problem_context pctx;
a63745e8 786 blk64_t blk, new_blk;
838e773e
TT
787 dnode_t *n;
788 struct inode_el *ino_el;
f51b4d33 789 struct dup_cluster *dc;
838e773e 790 struct dup_inode *di;
3839e657 791
133a56dc 792 clear_problem_context(&pctx);
3839e657 793 cs.errcode = 0;
521e3685 794 cs.dir = 0;
b23f2f4d 795 cs.dup_cluster = ~0;
f51b4d33 796 cs.alloc_block = 0;
1b6bf175 797 cs.ctx = ctx;
c4e3d3f3 798 retval = ext2fs_get_mem(fs->blocksize, &cs.buf);
08b21301
TT
799 if (retval)
800 return retval;
521e3685 801
c5d2f50d 802 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, ino))
838e773e 803 cs.dir = ino;
133a56dc 804
838e773e 805 pctx.ino = ino;
133a56dc 806 pctx.str = "clone_file";
0c80c44b 807 if (ext2fs_inode_has_valid_blocks2(fs, &dp->inode))
a63745e8 808 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
0684a4f3 809 clone_file_block, &cs);
3839e657 810 ext2fs_mark_bb_dirty(fs);
133a56dc
TT
811 if (pctx.errcode) {
812 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
7abb2bdc
TT
813 retval = pctx.errcode;
814 goto errout;
3839e657
TT
815 }
816 if (cs.errcode) {
622f5f27 817 com_err("clone_file", cs.errcode,
0c4a0726 818 _("returned from clone_file_block"));
7abb2bdc
TT
819 retval = cs.errcode;
820 goto errout;
3839e657 821 }
0684a4f3
TT
822 /* The inode may have changed on disk, so we have to re-read it */
823 e2fsck_read_inode(ctx, ino, &dp->inode, "clone file EA");
0c80c44b 824 blk = ext2fs_file_acl_block(fs, &dp->inode);
a63745e8
VAH
825 new_blk = blk;
826 if (blk && (clone_file_block(fs, &new_blk,
7abb2bdc
TT
827 BLOCK_COUNT_EXTATTR, 0, 0, &cs) ==
828 BLOCK_CHANGED)) {
0c80c44b 829 ext2fs_file_acl_block_set(fs, &dp->inode, new_blk);
838e773e 830 e2fsck_write_inode(ctx, ino, &dp->inode, "clone file EA");
342d847d
TT
831 /*
832 * If we cloned the EA block, find all other inodes
833 * which refered to that EA block, and modify
834 * them to point to the new EA block.
835 */
f51b4d33
TT
836 n = dict_lookup(&clstr_dict,
837 INT_TO_VOIDPTR(EXT2FS_B2C(fs, blk)));
538e654c 838 if (!n) {
efc6f628 839 com_err("clone_file", 0,
538e654c 840 _("internal error: couldn't lookup EA "
a63745e8 841 "block record for %llu"), blk);
538e654c
BB
842 retval = 0; /* OK to stumble on... */
843 goto errout;
844 }
f51b4d33
TT
845 dc = (struct dup_cluster *) dnode_get(n);
846 for (ino_el = dc->inode_list; ino_el; ino_el = ino_el->next) {
838e773e 847 if (ino_el->inode == ino)
342d847d 848 continue;
0c193f82 849 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino_el->inode));
538e654c 850 if (!n) {
efc6f628 851 com_err("clone_file", 0,
538e654c 852 _("internal error: couldn't lookup EA "
efc6f628 853 "inode record for %u"),
538e654c
BB
854 ino_el->inode);
855 retval = 0; /* OK to stumble on... */
856 goto errout;
857 }
838e773e 858 di = (struct dup_inode *) dnode_get(n);
0c80c44b
TT
859 if (ext2fs_file_acl_block(fs, &di->inode) == blk) {
860 ext2fs_file_acl_block_set(fs, &di->inode,
861 ext2fs_file_acl_block(fs, &dp->inode));
838e773e 862 e2fsck_write_inode(ctx, ino_el->inode,
0684a4f3 863 &di->inode, "clone file EA");
f51b4d33 864 decrement_badcount(ctx, blk, dc);
7abb2bdc 865 }
342d847d
TT
866 }
867 }
7abb2bdc
TT
868 retval = 0;
869errout:
c4e3d3f3 870 ext2fs_free_mem(&cs.buf);
7abb2bdc 871 return retval;
3839e657 872}
80c5d7e4
TT
873
874/*
875 * This routine returns 1 if a block overlaps with one of the superblocks,
876 * group descriptors, inode bitmaps, or block bitmaps.
877 */
6dc64392 878static int check_if_fs_block(e2fsck_t ctx, blk64_t test_block)
80c5d7e4
TT
879{
880 ext2_filsys fs = ctx->fs;
6dc64392 881 blk64_t first_block;
54434927 882 dgrp_t i;
efc6f628 883
bb1a46a4 884 first_block = fs->super->s_first_data_block;
80c5d7e4
TT
885 for (i = 0; i < fs->group_desc_count; i++) {
886
bb1a46a4 887 /* Check superblocks/block group descriptors */
80c5d7e4 888 if (ext2fs_bg_has_super(fs, i)) {
bb1a46a4
ES
889 if (test_block >= first_block &&
890 (test_block <= first_block + fs->desc_blocks))
80c5d7e4
TT
891 return 1;
892 }
efc6f628 893
80c5d7e4 894 /* Check the inode table */
d7cca6b0
VAH
895 if ((ext2fs_inode_table_loc(fs, i)) &&
896 (test_block >= ext2fs_inode_table_loc(fs, i)) &&
897 (test_block < (ext2fs_inode_table_loc(fs, i) +
80c5d7e4
TT
898 fs->inode_blocks_per_group)))
899 return 1;
900
901 /* Check the bitmap blocks */
d7cca6b0
VAH
902 if ((test_block == ext2fs_block_bitmap_loc(fs, i)) ||
903 (test_block == ext2fs_inode_bitmap_loc(fs, i)))
80c5d7e4 904 return 1;
efc6f628 905
bb1a46a4 906 first_block += fs->super->s_blocks_per_group;
80c5d7e4
TT
907 }
908 return 0;
909}
f51b4d33
TT
910
911/*
912 * This routine returns 1 if a cluster overlaps with one of the superblocks,
913 * group descriptors, inode bitmaps, or block bitmaps.
914 */
915static int check_if_fs_cluster(e2fsck_t ctx, blk64_t cluster)
916{
917 ext2_filsys fs = ctx->fs;
918 blk64_t first_block;
919 dgrp_t i;
920
921 first_block = fs->super->s_first_data_block;
922 for (i = 0; i < fs->group_desc_count; i++) {
923
924 /* Check superblocks/block group descriptors */
925 if (ext2fs_bg_has_super(fs, i)) {
926 if (cluster >= EXT2FS_B2C(fs, first_block) &&
927 (cluster <= EXT2FS_B2C(fs, first_block +
928 fs->desc_blocks)))
929 return 1;
930 }
931
932 /* Check the inode table */
933 if ((ext2fs_inode_table_loc(fs, i)) &&
934 (cluster >= EXT2FS_B2C(fs,
935 ext2fs_inode_table_loc(fs, i))) &&
936 (cluster <= EXT2FS_B2C(fs,
937 ext2fs_inode_table_loc(fs, i) +
938 fs->inode_blocks_per_group - 1)))
939 return 1;
940
941 /* Check the bitmap blocks */
942 if ((cluster == EXT2FS_B2C(fs,
943 ext2fs_block_bitmap_loc(fs, i))) ||
944 (cluster == EXT2FS_B2C(fs,
945 ext2fs_inode_bitmap_loc(fs, i))))
946 return 1;
947
948 first_block += fs->super->s_blocks_per_group;
949 }
950 return 0;
951}