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