]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/pass1b.c
Fix clang warnings on architectures with a 64-bit long
[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"
3dca12fb 52#include "support/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 81 int num_dupblocks;
bc1ec4b4 82 struct ext2_inode_large 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);
974d57d3
TT
91static errcode_t 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
b0930168 107static int dict_int_cmp(const void* cmp_ctx, const void *a, const void *b)
838e773e 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,
bc1ec4b4 121 struct ext2_inode_large *inode)
838e773e
TT
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 182 next = p->next;
70303df1 183 ext2fs_free_mem(&p);
838e773e 184 }
70303df1
AD
185 ext2fs_free_mem(&di);
186 ext2fs_free_mem(&node);
838e773e
TT
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 200 next = p->next;
70303df1 201 ext2fs_free_mem(&p);
838e773e 202 }
70303df1
AD
203 ext2fs_free_mem(&dc);
204 ext2fs_free_mem(&node);
838e773e
TT
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
830b44f4
TT
221 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
222 _("multiply claimed inode map"),
223 EXT2FS_BMAP64_RBTREE, "inode_dup_map",
224 &inode_dup_map);
1b6bf175
TT
225 if (pctx.errcode) {
226 fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
f8188fff
TT
227 ctx->flags |= E2F_FLAG_ABORT;
228 return;
3839e657 229 }
838e773e
TT
230
231 dict_init(&ino_dict, DICTCOUNT_T_MAX, dict_int_cmp);
f51b4d33 232 dict_init(&clstr_dict, DICTCOUNT_T_MAX, dict_int_cmp);
838e773e 233 dict_set_allocator(&ino_dict, NULL, inode_dnode_free, NULL);
f51b4d33 234 dict_set_allocator(&clstr_dict, NULL, cluster_dnode_free, NULL);
efc6f628 235
fea390e2 236 init_resource_track(&rtrack, ctx->fs->io);
1b6bf175 237 pass1b(ctx, block_buf);
9facd076 238 print_resource_track(ctx, "Pass 1b", &rtrack, ctx->fs->io);
fea390e2 239
fea390e2 240 init_resource_track(&rtrack, ctx->fs->io);
1b6bf175 241 pass1c(ctx, block_buf);
9facd076 242 print_resource_track(ctx, "Pass 1c", &rtrack, ctx->fs->io);
fea390e2 243
fea390e2 244 init_resource_track(&rtrack, ctx->fs->io);
1b6bf175 245 pass1d(ctx, block_buf);
9facd076 246 print_resource_track(ctx, "Pass 1d", &rtrack, ctx->fs->io);
fea390e2 247
91327df4
DA
248 if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
249 (ctx->options & E2F_OPT_UNSHARE_BLOCKS)) {
250 /*
251 * If we successfully managed to unshare all blocks, unset the
252 * shared block feature.
253 */
254 blk64_t next;
255 int result = ext2fs_find_first_set_block_bitmap2(
256 ctx->block_dup_map,
257 ctx->fs->super->s_first_data_block,
258 ext2fs_blocks_count(ctx->fs->super) - 1,
259 &next);
d90c7666 260 if (result == ENOENT && !(ctx->options & E2F_OPT_NO)) {
91327df4
DA
261 ext2fs_clear_feature_shared_blocks(ctx->fs->super);
262 ext2fs_mark_super_dirty(ctx->fs);
263 }
264 }
265
3839e657
TT
266 /*
267 * Time to free all of the accumulated data structures that we
268 * don't need anymore.
269 */
838e773e 270 dict_free_nodes(&ino_dict);
f51b4d33 271 dict_free_nodes(&clstr_dict);
23f75f6e 272 ext2fs_free_inode_bitmap(inode_dup_map);
3839e657
TT
273}
274
275/*
276 * Scan the inodes looking for inodes that contain duplicate blocks.
277 */
278struct process_block_struct {
838e773e 279 e2fsck_t ctx;
86c627ec
TT
280 ext2_ino_t ino;
281 int dup_blocks;
9a1d614d 282 blk64_t cur_cluster, phys_cluster;
28b966d7 283 blk64_t last_blk;
bc1ec4b4 284 struct ext2_inode_large *inode;
1b6bf175 285 struct problem_context *pctx;
3839e657
TT
286};
287
08b21301 288static void pass1b(e2fsck_t ctx, char *block_buf)
3839e657 289{
1b6bf175 290 ext2_filsys fs = ctx->fs;
24c91184 291 ext2_ino_t ino = 0;
bc1ec4b4 292 struct ext2_inode_large inode;
3839e657 293 ext2_inode_scan scan;
3839e657 294 struct process_block_struct pb;
1b6bf175 295 struct problem_context pctx;
28b966d7 296 problem_t op;
efc6f628 297
1b6bf175 298 clear_problem_context(&pctx);
efc6f628 299
151786fc
TT
300 if (!(ctx->options & E2F_OPT_PREEN))
301 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
1b6bf175
TT
302 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
303 &scan);
304 if (pctx.errcode) {
305 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
f8188fff
TT
306 ctx->flags |= E2F_FLAG_ABORT;
307 return;
3839e657 308 }
bc1ec4b4 309 ctx->stashed_inode = EXT2_INODE(&inode);
1b6bf175
TT
310 pb.ctx = ctx;
311 pb.pctx = &pctx;
133a56dc 312 pctx.str = "pass1b";
d237a78e 313 while (1) {
0f5eba75
AD
314 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
315 if (e2fsck_mmp_update(fs))
316 fatal_error(ctx, 0);
317 }
bc1ec4b4
TT
318 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
319 EXT2_INODE(&inode), sizeof(inode));
d237a78e
TT
320 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
321 continue;
322 if (pctx.errcode) {
88e172a9 323 pctx.ino = ino;
d237a78e
TT
324 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
325 ctx->flags |= E2F_FLAG_ABORT;
326 return;
327 }
328 if (!ino)
329 break;
1b6bf175 330 pctx.ino = ctx->stashed_ino = ino;
3839e657 331 if ((ino != EXT2_BAD_INO) &&
c5d2f50d 332 !ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino))
d237a78e 333 continue;
3839e657
TT
334
335 pb.ino = ino;
336 pb.dup_blocks = 0;
838e773e 337 pb.inode = &inode;
b23f2f4d 338 pb.cur_cluster = ~0;
9a1d614d 339 pb.phys_cluster = ~0;
28b966d7
DW
340 pb.last_blk = 0;
341 pb.pctx->blk = pb.pctx->blk2 = 0;
0684a4f3 342
bc1ec4b4 343 if (ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(&inode)) ||
0684a4f3 344 (ino == EXT2_BAD_INO))
a63745e8 345 pctx.errcode = ext2fs_block_iterate3(fs, ino,
15d482ba
TT
346 BLOCK_FLAG_READ_ONLY, block_buf,
347 process_pass1b_block, &pb);
7501ce3e 348 /* If the feature is not set, attrs will be cleared later anyway */
86f3b6cf 349 if (ext2fs_has_feature_xattr(fs->super) &&
bc1ec4b4
TT
350 ext2fs_file_acl_block(fs, EXT2_INODE(&inode))) {
351 blk64_t blk = ext2fs_file_acl_block(fs, EXT2_INODE(&inode));
a63745e8 352 process_pass1b_block(fs, &blk,
342d847d 353 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
bc1ec4b4 354 ext2fs_file_acl_block_set(fs, EXT2_INODE(&inode), blk);
7501ce3e 355 }
3839e657 356 if (pb.dup_blocks) {
28b966d7
DW
357 if (ino != EXT2_BAD_INO) {
358 op = pctx.blk == pctx.blk2 ?
359 PR_1B_DUP_BLOCK : PR_1B_DUP_RANGE;
360 fix_problem(ctx, op, pb.pctx);
361 }
1b6bf175 362 end_problem_latch(ctx, PR_LATCH_DBLOCK);
838e773e
TT
363 if (ino >= EXT2_FIRST_INODE(fs->super) ||
364 ino == EXT2_ROOT_INO)
3839e657
TT
365 dup_inode_count++;
366 }
133a56dc
TT
367 if (pctx.errcode)
368 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
3839e657
TT
369 }
370 ext2fs_close_inode_scan(scan);
71d521c6 371 e2fsck_use_inode_shortcuts(ctx, 0);
3839e657
TT
372}
373
54434927 374static int process_pass1b_block(ext2_filsys fs EXT2FS_ATTR((unused)),
a63745e8 375 blk64_t *block_nr,
b23f2f4d 376 e2_blkcnt_t blockcnt,
a63745e8 377 blk64_t ref_blk EXT2FS_ATTR((unused)),
54434927 378 int ref_offset EXT2FS_ATTR((unused)),
53ef44c4 379 void *priv_data)
3839e657
TT
380{
381 struct process_block_struct *p;
1b6bf175 382 e2fsck_t ctx;
9a1d614d 383 blk64_t lc, pc;
28b966d7 384 problem_t op;
3839e657 385
4a05268c 386 if (*block_nr == 0)
3839e657 387 return 0;
54dc7ca2 388 p = (struct process_block_struct *) priv_data;
1b6bf175 389 ctx = p->ctx;
b23f2f4d 390 lc = EXT2FS_B2C(fs, blockcnt);
9a1d614d 391 pc = EXT2FS_B2C(fs, *block_nr);
efc6f628 392
c5d2f50d 393 if (!ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr))
b23f2f4d 394 goto finish;
efc6f628 395
838e773e
TT
396 /* OK, this is a duplicate block */
397 if (p->ino != EXT2_BAD_INO) {
28b966d7
DW
398 if (p->last_blk + 1 != *block_nr) {
399 if (p->last_blk) {
400 op = p->pctx->blk == p->pctx->blk2 ?
401 PR_1B_DUP_BLOCK :
402 PR_1B_DUP_RANGE;
403 fix_problem(ctx, op, p->pctx);
404 }
405 p->pctx->blk = *block_nr;
406 }
407 p->pctx->blk2 = *block_nr;
408 p->last_blk = *block_nr;
3839e657 409 }
838e773e 410 p->dup_blocks++;
c5d2f50d 411 ext2fs_mark_inode_bitmap2(inode_dup_map, p->ino);
838e773e 412
9a1d614d
DW
413 /*
414 * Qualifications for submitting a block for duplicate processing:
415 * It's an extent/indirect block (and has a negative logical offset);
416 * we've crossed a logical cluster boundary; or the physical cluster
417 * suddenly changed, which indicates that blocks in a logical cluster
418 * are mapped to multiple physical clusters.
419 */
420 if (blockcnt < 0 || lc != p->cur_cluster || pc != p->phys_cluster)
b23f2f4d 421 add_dupe(ctx, p->ino, EXT2FS_B2C(fs, *block_nr), p->inode);
efc6f628 422
b23f2f4d
TT
423finish:
424 p->cur_cluster = lc;
9a1d614d 425 p->phys_cluster = pc;
3839e657
TT
426 return 0;
427}
428
3839e657
TT
429/*
430 * Pass 1c: Scan directories for inodes with duplicate blocks. This
431 * is used so that we can print pathnames when prompting the user for
432 * what to do.
433 */
21c84b71 434struct search_dir_struct {
3839e657 435 int count;
86c627ec
TT
436 ext2_ino_t first_inode;
437 ext2_ino_t max_inode;
3839e657
TT
438};
439
86c627ec 440static int search_dirent_proc(ext2_ino_t dir, int entry,
21c84b71 441 struct ext2_dir_entry *dirent,
efc6f628 442 int offset EXT2FS_ATTR((unused)),
54434927 443 int blocksize EXT2FS_ATTR((unused)),
efc6f628 444 char *buf EXT2FS_ATTR((unused)),
54434927 445 void *priv_data)
21c84b71 446{
54dc7ca2 447 struct search_dir_struct *sd;
21c84b71 448 struct dup_inode *p;
838e773e 449 dnode_t *n;
54dc7ca2
TT
450
451 sd = (struct search_dir_struct *) priv_data;
452
521e3685
TT
453 if (dirent->inode > sd->max_inode)
454 /* Should abort this inode, but not everything */
efc6f628 455 return 0;
521e3685 456
838e773e 457 if ((dirent->inode < sd->first_inode) || (entry < DIRENT_OTHER_FILE) ||
c5d2f50d 458 !ext2fs_test_inode_bitmap2(inode_dup_map, dirent->inode))
21c84b71
TT
459 return 0;
460
0c193f82 461 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(dirent->inode));
838e773e 462 if (!n)
21c84b71 463 return 0;
838e773e 464 p = (struct dup_inode *) dnode_get(n);
3d51ff87
JG
465 if (!p->dir) {
466 p->dir = dir;
467 sd->count--;
468 }
21c84b71
TT
469
470 return(sd->count ? 0 : DIRENT_ABORT);
471}
472
473
08b21301 474static void pass1c(e2fsck_t ctx, char *block_buf)
3839e657 475{
1b6bf175 476 ext2_filsys fs = ctx->fs;
21c84b71 477 struct search_dir_struct sd;
1b6bf175
TT
478 struct problem_context pctx;
479
480 clear_problem_context(&pctx);
3839e657 481
151786fc
TT
482 if (!(ctx->options & E2F_OPT_PREEN))
483 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
3839e657 484
3839e657
TT
485 /*
486 * Search through all directories to translate inodes to names
487 * (by searching for the containing directory for that inode.)
488 */
81cae650 489 sd.count = dup_inode_count - dup_inode_founddir;
21c84b71 490 sd.first_inode = EXT2_FIRST_INODE(fs->super);
521e3685 491 sd.max_inode = fs->super->s_inodes_count;
21c84b71
TT
492 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
493 search_dirent_proc, &sd);
efc6f628 494}
3839e657 495
1b6bf175 496static void pass1d(e2fsck_t ctx, char *block_buf)
3839e657 497{
1b6bf175 498 ext2_filsys fs = ctx->fs;
838e773e 499 struct dup_inode *p, *t;
f51b4d33 500 struct dup_cluster *q;
838e773e 501 ext2_ino_t *shared, ino;
3839e657
TT
502 int shared_len;
503 int i;
3839e657 504 int file_ok;
521e3685 505 int meta_data = 0;
21c84b71 506 struct problem_context pctx;
838e773e 507 dnode_t *n, *m;
f51b4d33 508 struct cluster_el *s;
838e773e 509 struct inode_el *r;
efc6f628 510
1b6bf175 511 clear_problem_context(&pctx);
efc6f628 512
151786fc
TT
513 if (!(ctx->options & E2F_OPT_PREEN))
514 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
f8188fff 515 e2fsck_read_bitmaps(ctx);
3839e657 516
838e773e 517 pctx.num = dup_inode_count; /* dict_count(&ino_dict); */
1b6bf175 518 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
86c627ec 519 shared = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
838e773e 520 sizeof(ext2_ino_t) * dict_count(&ino_dict),
54dc7ca2 521 "Shared inode list");
838e773e
TT
522 for (n = dict_first(&ino_dict); n; n = dict_next(&ino_dict, n)) {
523 p = (struct dup_inode *) dnode_get(n);
3839e657
TT
524 shared_len = 0;
525 file_ok = 1;
0c193f82 526 ino = (ext2_ino_t)VOIDPTR_TO_INT(dnode_getkey(n));
5e916143 527 if (ino == EXT2_BAD_INO || ino == EXT2_RESIZE_INO)
3839e657
TT
528 continue;
529
530 /*
838e773e
TT
531 * Find all of the inodes which share blocks with this
532 * one. First we find all of the duplicate blocks
533 * belonging to this inode, and then search each block
534 * get the list of inodes, and merge them together.
3839e657 535 */
f51b4d33
TT
536 for (s = p->cluster_list; s; s = s->next) {
537 m = dict_lookup(&clstr_dict,
538 INT_TO_VOIDPTR(s->cluster));
838e773e
TT
539 if (!m)
540 continue; /* Should never happen... */
f51b4d33 541 q = (struct dup_cluster *) dnode_get(m);
3839e657
TT
542 if (q->num_bad > 1)
543 file_ok = 0;
f51b4d33 544 if (check_if_fs_cluster(ctx, s->cluster)) {
521e3685
TT
545 file_ok = 0;
546 meta_data = 1;
547 }
efc6f628 548
3839e657
TT
549 /*
550 * Add all inodes used by this block to the
551 * shared[] --- which is a unique list, so
552 * if an inode is already in shared[], don't
553 * add it again.
554 */
838e773e
TT
555 for (r = q->inode_list; r; r = r->next) {
556 if (r->inode == ino)
3839e657
TT
557 continue;
558 for (i = 0; i < shared_len; i++)
838e773e 559 if (shared[i] == r->inode)
3839e657
TT
560 break;
561 if (i == shared_len) {
838e773e 562 shared[shared_len++] = r->inode;
3839e657
TT
563 }
564 }
565 }
21c84b71
TT
566
567 /*
568 * Report the inode that we are working on
569 */
bc1ec4b4 570 pctx.inode = EXT2_INODE(&p->inode);
838e773e 571 pctx.ino = ino;
21c84b71
TT
572 pctx.dir = p->dir;
573 pctx.blkcount = p->num_dupblocks;
521e3685 574 pctx.num = meta_data ? shared_len+1 : shared_len;
1b6bf175 575 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
21c84b71
TT
576 pctx.blkcount = 0;
577 pctx.num = 0;
efc6f628 578
521e3685 579 if (meta_data)
1b6bf175 580 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
efc6f628 581
3839e657 582 for (i = 0; i < shared_len; i++) {
0c193f82 583 m = dict_lookup(&ino_dict, INT_TO_VOIDPTR(shared[i]));
838e773e
TT
584 if (!m)
585 continue; /* should never happen */
586 t = (struct dup_inode *) dnode_get(m);
21c84b71
TT
587 /*
588 * Report the inode that we are sharing with
589 */
bc1ec4b4 590 pctx.inode = EXT2_INODE(&t->inode);
838e773e
TT
591 pctx.ino = shared[i];
592 pctx.dir = t->dir;
1b6bf175 593 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
3839e657 594 }
9a1d614d
DW
595 /*
596 * Even if the file shares blocks with itself, we still need to
597 * clone the blocks.
598 */
599 if (file_ok && (meta_data ? shared_len+1 : shared_len) != 0) {
1b6bf175 600 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
3839e657
TT
601 continue;
602 }
91327df4
DA
603 if ((ctx->options & E2F_OPT_UNSHARE_BLOCKS) ||
604 fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
838e773e 605 pctx.errcode = clone_file(ctx, ino, p, block_buf);
1b6bf175
TT
606 if (pctx.errcode)
607 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
608 else
3839e657 609 continue;
3839e657 610 }
91327df4
DA
611 /*
612 * Note: When unsharing blocks, we don't prompt to delete
613 * files. If the clone operation fails than the unshare
614 * operation should fail too.
615 */
616 if (!(ctx->options & E2F_OPT_UNSHARE_BLOCKS) &&
617 fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
838e773e 618 delete_file(ctx, ino, p, block_buf);
3839e657
TT
619 else
620 ext2fs_unmark_valid(fs);
3839e657 621 }
c4e3d3f3 622 ext2fs_free_mem(&shared);
3839e657
TT
623}
624
7abb2bdc
TT
625/*
626 * Drop the refcount on the dup_block structure, and clear the entry
627 * in the block_dup_map if appropriate.
628 */
f51b4d33
TT
629static void decrement_badcount(e2fsck_t ctx, blk64_t block,
630 struct dup_cluster *p)
7abb2bdc
TT
631{
632 p->num_bad--;
633 if (p->num_bad <= 0 ||
f51b4d33
TT
634 (p->num_bad == 1 && !check_if_fs_block(ctx, block))) {
635 if (check_if_fs_cluster(ctx, EXT2FS_B2C(ctx->fs, block)))
636 return;
c5d2f50d 637 ext2fs_unmark_block_bitmap2(ctx->block_dup_map, block);
f51b4d33 638 }
7abb2bdc
TT
639}
640
3839e657 641static int delete_file_block(ext2_filsys fs,
a63745e8 642 blk64_t *block_nr,
b23f2f4d 643 e2_blkcnt_t blockcnt,
a63745e8 644 blk64_t ref_block EXT2FS_ATTR((unused)),
54434927 645 int ref_offset EXT2FS_ATTR((unused)),
54dc7ca2 646 void *priv_data)
3839e657 647{
54dc7ca2 648 struct process_block_struct *pb;
f51b4d33 649 struct dup_cluster *p;
838e773e 650 dnode_t *n;
1b6bf175 651 e2fsck_t ctx;
b23f2f4d 652 blk64_t c, lc;
1b6bf175 653
54dc7ca2 654 pb = (struct process_block_struct *) priv_data;
1b6bf175 655 ctx = pb->ctx;
3839e657 656
4a05268c 657 if (*block_nr == 0)
3839e657
TT
658 return 0;
659
f51b4d33 660 c = EXT2FS_B2C(fs, *block_nr);
b23f2f4d 661 lc = EXT2FS_B2C(fs, blockcnt);
c5d2f50d 662 if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
f51b4d33 663 n = dict_lookup(&clstr_dict, INT_TO_VOIDPTR(c));
838e773e 664 if (n) {
b0f5fa88
EW
665 if (lc != pb->cur_cluster) {
666 p = (struct dup_cluster *) dnode_get(n);
b23f2f4d 667 decrement_badcount(ctx, *block_nr, p);
b0f5fa88
EW
668 pb->dup_blocks++;
669 }
3839e657
TT
670 } else
671 com_err("delete_file_block", 0,
a63745e8 672 _("internal error: can't find dup_blk for %llu\n"),
33b9a60c 673 (unsigned long long) *block_nr);
3839e657 674 } else {
8dd650ab
DW
675 if ((*block_nr % EXT2FS_CLUSTER_RATIO(ctx->fs)) == 0)
676 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
624e4a64 677 pb->dup_blocks++;
3839e657 678 }
b23f2f4d 679 pb->cur_cluster = lc;
efc6f628 680
3839e657
TT
681 return 0;
682}
efc6f628 683
838e773e
TT
684static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
685 struct dup_inode *dp, char* block_buf)
3839e657 686{
1b6bf175 687 ext2_filsys fs = ctx->fs;
3839e657 688 struct process_block_struct pb;
133a56dc 689 struct problem_context pctx;
0684a4f3 690 unsigned int count;
3839e657 691
133a56dc 692 clear_problem_context(&pctx);
838e773e 693 pctx.ino = pb.ino = ino;
624e4a64 694 pb.dup_blocks = 0;
1b6bf175 695 pb.ctx = ctx;
133a56dc 696 pctx.str = "delete_file";
b23f2f4d 697 pb.cur_cluster = ~0;
133a56dc 698
bc1ec4b4 699 if (ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(&dp->inode)))
68477355
TT
700 pctx.errcode = ext2fs_block_iterate3(fs, ino,
701 BLOCK_FLAG_READ_ONLY,
702 block_buf,
703 delete_file_block, &pb);
133a56dc
TT
704 if (pctx.errcode)
705 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
1b6bf175 706 if (ctx->inode_bad_map)
c5d2f50d 707 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
71ba1375
TT
708 if (ctx->inode_reg_map)
709 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
710 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
711 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
68477355
TT
712 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(dp->inode.i_mode));
713 quota_data_sub(ctx->qctx, &dp->inode, ino,
714 pb.dup_blocks * fs->blocksize);
715 quota_data_inodes(ctx->qctx, &dp->inode, ino, -1);
0684a4f3
TT
716
717 /* Inode may have changed by block_iterate, so reread it */
bc1ec4b4
TT
718 e2fsck_read_inode_full(ctx, ino, EXT2_INODE(&dp->inode),
719 sizeof(dp->inode), "delete_file");
720 e2fsck_clear_inode(ctx, ino, EXT2_INODE(&dp->inode), 0, "delete_file");
721 if (ext2fs_file_acl_block(fs, EXT2_INODE(&dp->inode)) &&
86f3b6cf 722 ext2fs_has_feature_xattr(fs->super)) {
bc1ec4b4
TT
723 blk64_t file_acl_block = ext2fs_file_acl_block(fs,
724 EXT2_INODE(&dp->inode));
725
0684a4f3 726 count = 1;
bc1ec4b4 727 pctx.errcode = ext2fs_adjust_ea_refcount3(fs, file_acl_block,
39f5659a 728 block_buf, -1, &count, ino);
0684a4f3
TT
729 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
730 pctx.errcode = 0;
731 count = 1;
732 }
733 if (pctx.errcode) {
bc1ec4b4 734 pctx.blk = file_acl_block;
0684a4f3
TT
735 fix_problem(ctx, PR_1B_ADJ_EA_REFCOUNT, &pctx);
736 }
737 /*
738 * If the count is zero, then arrange to have the
739 * block deleted. If the block is in the block_dup_map,
740 * also call delete_file_block since it will take care
741 * of keeping the accounting straight.
742 */
743 if ((count == 0) ||
c5d2f50d 744 ext2fs_test_block_bitmap2(ctx->block_dup_map,
bc1ec4b4
TT
745 file_acl_block)) {
746 delete_file_block(fs, &file_acl_block,
0684a4f3 747 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
bc1ec4b4
TT
748 ext2fs_file_acl_block_set(fs, EXT2_INODE(&dp->inode),
749 file_acl_block);
750 quota_data_sub(ctx->qctx, &dp->inode, ino,
751 fs->blocksize);
a63745e8 752 }
0684a4f3 753 }
3839e657
TT
754}
755
756struct clone_struct {
757 errcode_t errcode;
f51b4d33
TT
758 blk64_t dup_cluster;
759 blk64_t alloc_block;
9a1d614d 760 ext2_ino_t dir, ino;
3839e657 761 char *buf;
1b6bf175 762 e2fsck_t ctx;
bc1ec4b4 763 struct ext2_inode_large *inode;
09282b8a
DW
764
765 struct dup_cluster *save_dup_cluster;
766 blk64_t save_blocknr;
3839e657
TT
767};
768
09282b8a
DW
769/*
770 * Decrement the bad count *after* we've shown that (a) we can allocate a
771 * replacement block and (b) remap the file blocks. Unfortunately, there's no
772 * way to find out if the remap succeeded until either the next
773 * clone_file_block() call (an error when remapping the block after returning
774 * BLOCK_CHANGED will halt the iteration) or after block_iterate() returns.
775 * Otherwise, it's possible that we decrease the badcount once in preparation
776 * to remap, then the remap fails (either we can't find a replacement block or
777 * we have to split the extent tree and can't find a new extent block), so we
778 * delete the file, which decreases the badcount again.
779 */
780static void deferred_dec_badcount(struct clone_struct *cs)
781{
782 if (!cs->save_dup_cluster)
783 return;
784 decrement_badcount(cs->ctx, cs->save_blocknr, cs->save_dup_cluster);
785 cs->save_dup_cluster = NULL;
786}
787
3839e657 788static int clone_file_block(ext2_filsys fs,
a63745e8 789 blk64_t *block_nr,
133a56dc 790 e2_blkcnt_t blockcnt,
a63745e8 791 blk64_t ref_block EXT2FS_ATTR((unused)),
54434927 792 int ref_offset EXT2FS_ATTR((unused)),
54dc7ca2 793 void *priv_data)
3839e657 794{
09282b8a 795 struct dup_cluster *p = NULL;
c5d2f50d 796 blk64_t new_block;
3839e657 797 errcode_t retval;
54dc7ca2 798 struct clone_struct *cs = (struct clone_struct *) priv_data;
838e773e 799 dnode_t *n;
1b6bf175 800 e2fsck_t ctx;
f51b4d33
TT
801 blk64_t c;
802 int is_meta = 0;
d90c7666 803 int should_write = 1;
3839e657 804
1b6bf175 805 ctx = cs->ctx;
09282b8a 806 deferred_dec_badcount(cs);
efc6f628 807
4a05268c 808 if (*block_nr == 0)
3839e657
TT
809 return 0;
810
d90c7666
DA
811 if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
812 (ctx->options & E2F_OPT_UNSHARE_BLOCKS) &&
813 (ctx->options & E2F_OPT_NO))
814 should_write = 0;
815
f51b4d33
TT
816 c = EXT2FS_B2C(fs, blockcnt);
817 if (check_if_fs_cluster(ctx, EXT2FS_B2C(fs, *block_nr)))
818 is_meta = 1;
819
b23f2f4d
TT
820 if (c == cs->dup_cluster && cs->alloc_block) {
821 new_block = cs->alloc_block;
822 goto got_block;
823 }
824
825 if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
f51b4d33
TT
826 n = dict_lookup(&clstr_dict,
827 INT_TO_VOIDPTR(EXT2FS_B2C(fs, *block_nr)));
828 if (!n) {
829 com_err("clone_file_block", 0,
830 _("internal error: can't find dup_blk for %llu\n"),
33b9a60c 831 (unsigned long long) *block_nr);
f51b4d33
TT
832 return 0;
833 }
834
835 p = (struct dup_cluster *) dnode_get(n);
f51b4d33 836
f51b4d33 837 cs->dup_cluster = c;
9a1d614d
DW
838 /*
839 * Let's try an implied cluster allocation. If we get the same
840 * cluster back, then we need to find a new block; otherwise,
841 * we're merely fixing the problem of one logical cluster being
842 * mapped to multiple physical clusters.
843 */
844 new_block = 0;
bc1ec4b4
TT
845 retval = ext2fs_map_cluster_block(fs, cs->ino,
846 EXT2_INODE(cs->inode),
9a1d614d
DW
847 blockcnt, &new_block);
848 if (retval == 0 && new_block != 0 &&
849 EXT2FS_B2C(ctx->fs, new_block) !=
850 EXT2FS_B2C(ctx->fs, *block_nr))
851 goto cluster_alloc_ok;
f51b4d33
TT
852 retval = ext2fs_new_block2(fs, 0, ctx->block_found_map,
853 &new_block);
854 if (retval) {
855 cs->errcode = retval;
856 return BLOCK_ABORT;
857 }
91327df4
DA
858 if (ext2fs_has_feature_shared_blocks(fs->super)) {
859 /*
860 * Update the block stats so we don't get a prompt to fix block
861 * counts in the final pass.
862 */
863 ext2fs_block_alloc_stats2(fs, new_block, +1);
864 }
9a1d614d 865cluster_alloc_ok:
f51b4d33
TT
866 cs->alloc_block = new_block;
867
868 got_block:
869 new_block &= ~EXT2FS_CLUSTER_MASK(fs);
870 new_block += EXT2FS_CLUSTER_MASK(fs) & blockcnt;
871 if (cs->dir && (blockcnt >= 0)) {
872 retval = ext2fs_set_dir_block2(fs->dblist,
873 cs->dir, new_block, blockcnt);
3839e657
TT
874 if (retval) {
875 cs->errcode = retval;
876 return BLOCK_ABORT;
877 }
f51b4d33 878 }
7b63fff9 879#if 0
f51b4d33 880 printf("Cloning block #%lld from %llu to %llu\n",
33b9a60c
TT
881 blockcnt, (unsigned long long) *block_nr,
882 (unsigned long long) new_block);
7b63fff9 883#endif
f51b4d33
TT
884 retval = io_channel_read_blk64(fs->io, *block_nr, 1, cs->buf);
885 if (retval) {
886 cs->errcode = retval;
887 return BLOCK_ABORT;
888 }
d90c7666
DA
889 if (should_write) {
890 retval = io_channel_write_blk64(fs->io, new_block, 1, cs->buf);
891 if (retval) {
892 cs->errcode = retval;
893 return BLOCK_ABORT;
894 }
f51b4d33 895 }
09282b8a
DW
896 cs->save_dup_cluster = (is_meta ? NULL : p);
897 cs->save_blocknr = *block_nr;
f51b4d33
TT
898 *block_nr = new_block;
899 ext2fs_mark_block_bitmap2(ctx->block_found_map, new_block);
900 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
d90c7666
DA
901
902 if (!should_write) {
903 /* Don't try to change extent information; we want e2fsck to
904 * return success.
905 */
906 return 0;
907 }
f51b4d33 908 return BLOCK_CHANGED;
3839e657
TT
909 }
910 return 0;
911}
efc6f628 912
974d57d3
TT
913static errcode_t clone_file(e2fsck_t ctx, ext2_ino_t ino,
914 struct dup_inode *dp, char* block_buf)
3839e657 915{
1b6bf175 916 ext2_filsys fs = ctx->fs;
3839e657
TT
917 errcode_t retval;
918 struct clone_struct cs;
133a56dc 919 struct problem_context pctx;
a63745e8 920 blk64_t blk, new_blk;
838e773e
TT
921 dnode_t *n;
922 struct inode_el *ino_el;
f51b4d33 923 struct dup_cluster *dc;
838e773e 924 struct dup_inode *di;
3839e657 925
133a56dc 926 clear_problem_context(&pctx);
3839e657 927 cs.errcode = 0;
521e3685 928 cs.dir = 0;
b23f2f4d 929 cs.dup_cluster = ~0;
f51b4d33 930 cs.alloc_block = 0;
1b6bf175 931 cs.ctx = ctx;
9a1d614d
DW
932 cs.ino = ino;
933 cs.inode = &dp->inode;
09282b8a
DW
934 cs.save_dup_cluster = NULL;
935 cs.save_blocknr = 0;
c4e3d3f3 936 retval = ext2fs_get_mem(fs->blocksize, &cs.buf);
08b21301
TT
937 if (retval)
938 return retval;
521e3685 939
c5d2f50d 940 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, ino))
838e773e 941 cs.dir = ino;
133a56dc 942
838e773e 943 pctx.ino = ino;
133a56dc 944 pctx.str = "clone_file";
bc1ec4b4 945 if (ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(&dp->inode)))
a63745e8 946 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
0684a4f3 947 clone_file_block, &cs);
09282b8a 948 deferred_dec_badcount(&cs);
3839e657 949 ext2fs_mark_bb_dirty(fs);
133a56dc
TT
950 if (pctx.errcode) {
951 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
7abb2bdc
TT
952 retval = pctx.errcode;
953 goto errout;
3839e657
TT
954 }
955 if (cs.errcode) {
45ff69ff 956 com_err("clone_file", cs.errcode, "%s",
0c4a0726 957 _("returned from clone_file_block"));
7abb2bdc
TT
958 retval = cs.errcode;
959 goto errout;
3839e657 960 }
0684a4f3 961 /* The inode may have changed on disk, so we have to re-read it */
bc1ec4b4
TT
962 e2fsck_read_inode_full(ctx, ino, EXT2_INODE(&dp->inode),
963 sizeof(dp->inode), "clone file EA");
964 blk = ext2fs_file_acl_block(fs, EXT2_INODE(&dp->inode));
a63745e8
VAH
965 new_blk = blk;
966 if (blk && (clone_file_block(fs, &new_blk,
7abb2bdc
TT
967 BLOCK_COUNT_EXTATTR, 0, 0, &cs) ==
968 BLOCK_CHANGED)) {
bc1ec4b4
TT
969 ext2fs_file_acl_block_set(fs, EXT2_INODE(&dp->inode), new_blk);
970 e2fsck_write_inode_full(ctx, ino, EXT2_INODE(&dp->inode),
971 sizeof(dp->inode), "clone file EA");
342d847d
TT
972 /*
973 * If we cloned the EA block, find all other inodes
055866d8 974 * which referred to that EA block, and modify
342d847d
TT
975 * them to point to the new EA block.
976 */
f51b4d33
TT
977 n = dict_lookup(&clstr_dict,
978 INT_TO_VOIDPTR(EXT2FS_B2C(fs, blk)));
538e654c 979 if (!n) {
efc6f628 980 com_err("clone_file", 0,
538e654c 981 _("internal error: couldn't lookup EA "
33b9a60c
TT
982 "block record for %llu"),
983 (unsigned long long) blk);
538e654c
BB
984 retval = 0; /* OK to stumble on... */
985 goto errout;
986 }
f51b4d33
TT
987 dc = (struct dup_cluster *) dnode_get(n);
988 for (ino_el = dc->inode_list; ino_el; ino_el = ino_el->next) {
838e773e 989 if (ino_el->inode == ino)
342d847d 990 continue;
0c193f82 991 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino_el->inode));
538e654c 992 if (!n) {
efc6f628 993 com_err("clone_file", 0,
538e654c 994 _("internal error: couldn't lookup EA "
efc6f628 995 "inode record for %u"),
538e654c
BB
996 ino_el->inode);
997 retval = 0; /* OK to stumble on... */
998 goto errout;
999 }
838e773e 1000 di = (struct dup_inode *) dnode_get(n);
bc1ec4b4
TT
1001 if (ext2fs_file_acl_block(fs,
1002 EXT2_INODE(&di->inode)) == blk) {
1003 ext2fs_file_acl_block_set(fs,
1004 EXT2_INODE(&di->inode),
1005 ext2fs_file_acl_block(fs, EXT2_INODE(&dp->inode)));
1006 e2fsck_write_inode_full(ctx, ino_el->inode,
1007 EXT2_INODE(&di->inode),
1008 sizeof(di->inode), "clone file EA");
f51b4d33 1009 decrement_badcount(ctx, blk, dc);
7abb2bdc 1010 }
342d847d
TT
1011 }
1012 }
7abb2bdc
TT
1013 retval = 0;
1014errout:
c4e3d3f3 1015 ext2fs_free_mem(&cs.buf);
7abb2bdc 1016 return retval;
3839e657 1017}
80c5d7e4
TT
1018
1019/*
1020 * This routine returns 1 if a block overlaps with one of the superblocks,
1021 * group descriptors, inode bitmaps, or block bitmaps.
1022 */
6dc64392 1023static int check_if_fs_block(e2fsck_t ctx, blk64_t test_block)
80c5d7e4
TT
1024{
1025 ext2_filsys fs = ctx->fs;
6dc64392 1026 blk64_t first_block;
54434927 1027 dgrp_t i;
efc6f628 1028
bb1a46a4 1029 first_block = fs->super->s_first_data_block;
80c5d7e4
TT
1030 for (i = 0; i < fs->group_desc_count; i++) {
1031
bb1a46a4 1032 /* Check superblocks/block group descriptors */
80c5d7e4 1033 if (ext2fs_bg_has_super(fs, i)) {
bb1a46a4
ES
1034 if (test_block >= first_block &&
1035 (test_block <= first_block + fs->desc_blocks))
80c5d7e4
TT
1036 return 1;
1037 }
efc6f628 1038
80c5d7e4 1039 /* Check the inode table */
d7cca6b0
VAH
1040 if ((ext2fs_inode_table_loc(fs, i)) &&
1041 (test_block >= ext2fs_inode_table_loc(fs, i)) &&
1042 (test_block < (ext2fs_inode_table_loc(fs, i) +
80c5d7e4
TT
1043 fs->inode_blocks_per_group)))
1044 return 1;
1045
1046 /* Check the bitmap blocks */
d7cca6b0
VAH
1047 if ((test_block == ext2fs_block_bitmap_loc(fs, i)) ||
1048 (test_block == ext2fs_inode_bitmap_loc(fs, i)))
80c5d7e4 1049 return 1;
efc6f628 1050
bb1a46a4 1051 first_block += fs->super->s_blocks_per_group;
80c5d7e4
TT
1052 }
1053 return 0;
1054}
f51b4d33
TT
1055
1056/*
1057 * This routine returns 1 if a cluster overlaps with one of the superblocks,
1058 * group descriptors, inode bitmaps, or block bitmaps.
1059 */
1060static int check_if_fs_cluster(e2fsck_t ctx, blk64_t cluster)
1061{
1062 ext2_filsys fs = ctx->fs;
1063 blk64_t first_block;
1064 dgrp_t i;
1065
1066 first_block = fs->super->s_first_data_block;
1067 for (i = 0; i < fs->group_desc_count; i++) {
1068
1069 /* Check superblocks/block group descriptors */
1070 if (ext2fs_bg_has_super(fs, i)) {
1071 if (cluster >= EXT2FS_B2C(fs, first_block) &&
1072 (cluster <= EXT2FS_B2C(fs, first_block +
1073 fs->desc_blocks)))
1074 return 1;
1075 }
1076
1077 /* Check the inode table */
1078 if ((ext2fs_inode_table_loc(fs, i)) &&
1079 (cluster >= EXT2FS_B2C(fs,
1080 ext2fs_inode_table_loc(fs, i))) &&
1081 (cluster <= EXT2FS_B2C(fs,
1082 ext2fs_inode_table_loc(fs, i) +
1083 fs->inode_blocks_per_group - 1)))
1084 return 1;
1085
1086 /* Check the bitmap blocks */
1087 if ((cluster == EXT2FS_B2C(fs,
1088 ext2fs_block_bitmap_loc(fs, i))) ||
1089 (cluster == EXT2FS_B2C(fs,
1090 ext2fs_inode_bitmap_loc(fs, i))))
1091 return 1;
1092
1093 first_block += fs->super->s_blocks_per_group;
1094 }
1095 return 0;
1096}