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