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