2 * pass1b.c --- Pass #1b of e2fsck
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
8 * Pass1B scans the data blocks of all the inodes again, generating a
9 * complete list of duplicate blocks and which inodes have claimed
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.
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.
21 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
24 * This file may be redistributed under the terms of the GNU Public
36 #ifdef HAVE_INTTYPES_H
41 typedef long intptr_t;
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))
48 #include <et/com_err.h>
54 /* Define an extension to the ext2 library's block count information */
55 #define BLOCK_COUNT_EXTATTR (-5)
59 struct cluster_el
*next
;
64 struct inode_el
*next
;
69 struct inode_el
*inode_list
;
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.
82 struct ext2_inode inode
;
83 struct cluster_el
*cluster_list
;
86 static int process_pass1b_block(ext2_filsys fs
, blk64_t
*blocknr
,
87 e2_blkcnt_t blockcnt
, blk64_t ref_blk
,
88 int ref_offset
, void *priv_data
);
89 static void delete_file(e2fsck_t ctx
, ext2_ino_t ino
,
90 struct dup_inode
*dp
, char *block_buf
);
91 static errcode_t
clone_file(e2fsck_t ctx
, ext2_ino_t ino
,
92 struct dup_inode
*dp
, char* block_buf
);
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
);
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
);
100 static int dup_inode_count
= 0;
101 static int dup_inode_founddir
= 0;
103 static dict_t clstr_dict
, ino_dict
;
105 static ext2fs_inode_bitmap inode_dup_map
;
107 static int dict_int_cmp(const void *a
, const void *b
)
118 * Add a duplicate block record
120 static void add_dupe(e2fsck_t ctx
, ext2_ino_t ino
, blk64_t cluster
,
121 struct ext2_inode
*inode
)
124 struct dup_cluster
*db
;
125 struct dup_inode
*di
;
126 struct cluster_el
*cluster_el
;
127 struct inode_el
*ino_el
;
129 n
= dict_lookup(&clstr_dict
, INT_TO_VOIDPTR(cluster
));
131 db
= (struct dup_cluster
*) dnode_get(n
);
133 db
= (struct dup_cluster
*) e2fsck_allocate_memory(ctx
,
134 sizeof(struct dup_cluster
), "duplicate cluster header");
137 dict_alloc_insert(&clstr_dict
, INT_TO_VOIDPTR(cluster
), db
);
139 ino_el
= (struct inode_el
*) e2fsck_allocate_memory(ctx
,
140 sizeof(struct inode_el
), "inode element");
142 ino_el
->next
= db
->inode_list
;
143 db
->inode_list
= ino_el
;
146 n
= dict_lookup(&ino_dict
, INT_TO_VOIDPTR(ino
));
148 di
= (struct dup_inode
*) dnode_get(n
);
150 di
= (struct dup_inode
*) e2fsck_allocate_memory(ctx
,
151 sizeof(struct dup_inode
), "duplicate inode header");
152 if (ino
== EXT2_ROOT_INO
) {
153 di
->dir
= EXT2_ROOT_INO
;
154 dup_inode_founddir
++;
158 di
->num_dupblocks
= 0;
159 di
->cluster_list
= 0;
161 dict_alloc_insert(&ino_dict
, INT_TO_VOIDPTR(ino
), di
);
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
;
172 * Free a duplicate inode record
174 static void inode_dnode_free(dnode_t
*node
,
175 void *context
EXT2FS_ATTR((unused
)))
177 struct dup_inode
*di
;
178 struct cluster_el
*p
, *next
;
180 di
= (struct dup_inode
*) dnode_get(node
);
181 for (p
= di
->cluster_list
; p
; p
= next
) {
190 * Free a duplicate cluster record
192 static void cluster_dnode_free(dnode_t
*node
,
193 void *context
EXT2FS_ATTR((unused
)))
195 struct dup_cluster
*dc
;
196 struct inode_el
*p
, *next
;
198 dc
= (struct dup_cluster
*) dnode_get(node
);
199 for (p
= dc
->inode_list
; p
; p
= next
) {
209 * Main procedure for handling duplicate blocks
211 void e2fsck_pass1_dupblocks(e2fsck_t ctx
, char *block_buf
)
213 ext2_filsys fs
= ctx
->fs
;
214 struct problem_context pctx
;
215 #ifdef RESOURCE_TRACK
216 struct resource_track rtrack
;
219 clear_problem_context(&pctx
);
221 pctx
.errcode
= e2fsck_allocate_inode_bitmap(fs
,
222 _("multiply claimed inode map"),
223 EXT2FS_BMAP64_RBTREE
, "inode_dup_map",
226 fix_problem(ctx
, PR_1B_ALLOCATE_IBITMAP_ERROR
, &pctx
);
227 ctx
->flags
|= E2F_FLAG_ABORT
;
231 dict_init(&ino_dict
, DICTCOUNT_T_MAX
, dict_int_cmp
);
232 dict_init(&clstr_dict
, DICTCOUNT_T_MAX
, dict_int_cmp
);
233 dict_set_allocator(&ino_dict
, NULL
, inode_dnode_free
, NULL
);
234 dict_set_allocator(&clstr_dict
, NULL
, cluster_dnode_free
, NULL
);
236 init_resource_track(&rtrack
, ctx
->fs
->io
);
237 pass1b(ctx
, block_buf
);
238 print_resource_track(ctx
, "Pass 1b", &rtrack
, ctx
->fs
->io
);
240 init_resource_track(&rtrack
, ctx
->fs
->io
);
241 pass1c(ctx
, block_buf
);
242 print_resource_track(ctx
, "Pass 1c", &rtrack
, ctx
->fs
->io
);
244 init_resource_track(&rtrack
, ctx
->fs
->io
);
245 pass1d(ctx
, block_buf
);
246 print_resource_track(ctx
, "Pass 1d", &rtrack
, ctx
->fs
->io
);
249 * Time to free all of the accumulated data structures that we
250 * don't need anymore.
252 dict_free_nodes(&ino_dict
);
253 dict_free_nodes(&clstr_dict
);
254 ext2fs_free_inode_bitmap(inode_dup_map
);
258 * Scan the inodes looking for inodes that contain duplicate blocks.
260 struct process_block_struct
{
264 blk64_t cur_cluster
, phys_cluster
;
266 struct ext2_inode
*inode
;
267 struct problem_context
*pctx
;
270 static void pass1b(e2fsck_t ctx
, char *block_buf
)
272 ext2_filsys fs
= ctx
->fs
;
274 struct ext2_inode inode
;
275 ext2_inode_scan scan
;
276 struct process_block_struct pb
;
277 struct problem_context pctx
;
280 clear_problem_context(&pctx
);
282 if (!(ctx
->options
& E2F_OPT_PREEN
))
283 fix_problem(ctx
, PR_1B_PASS_HEADER
, &pctx
);
284 pctx
.errcode
= ext2fs_open_inode_scan(fs
, ctx
->inode_buffer_blocks
,
287 fix_problem(ctx
, PR_1B_ISCAN_ERROR
, &pctx
);
288 ctx
->flags
|= E2F_FLAG_ABORT
;
291 ctx
->stashed_inode
= &inode
;
296 if (ino
% (fs
->super
->s_inodes_per_group
* 4) == 1) {
297 if (e2fsck_mmp_update(fs
))
300 pctx
.errcode
= ext2fs_get_next_inode(scan
, &ino
, &inode
);
301 if (pctx
.errcode
== EXT2_ET_BAD_BLOCK_IN_INODE_TABLE
)
305 fix_problem(ctx
, PR_1B_ISCAN_ERROR
, &pctx
);
306 ctx
->flags
|= E2F_FLAG_ABORT
;
311 pctx
.ino
= ctx
->stashed_ino
= ino
;
312 if ((ino
!= EXT2_BAD_INO
) &&
313 !ext2fs_test_inode_bitmap2(ctx
->inode_used_map
, ino
))
320 pb
.phys_cluster
= ~0;
322 pb
.pctx
->blk
= pb
.pctx
->blk2
= 0;
324 if (ext2fs_inode_has_valid_blocks2(fs
, &inode
) ||
325 (ino
== EXT2_BAD_INO
))
326 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
,
327 BLOCK_FLAG_READ_ONLY
, block_buf
,
328 process_pass1b_block
, &pb
);
329 /* If the feature is not set, attrs will be cleared later anyway */
330 if ((fs
->super
->s_feature_compat
& EXT2_FEATURE_COMPAT_EXT_ATTR
) &&
331 ext2fs_file_acl_block(fs
, &inode
)) {
332 blk64_t blk
= ext2fs_file_acl_block(fs
, &inode
);
333 process_pass1b_block(fs
, &blk
,
334 BLOCK_COUNT_EXTATTR
, 0, 0, &pb
);
335 ext2fs_file_acl_block_set(fs
, &inode
, blk
);
338 if (ino
!= EXT2_BAD_INO
) {
339 op
= pctx
.blk
== pctx
.blk2
?
340 PR_1B_DUP_BLOCK
: PR_1B_DUP_RANGE
;
341 fix_problem(ctx
, op
, pb
.pctx
);
343 end_problem_latch(ctx
, PR_LATCH_DBLOCK
);
344 if (ino
>= EXT2_FIRST_INODE(fs
->super
) ||
345 ino
== EXT2_ROOT_INO
)
349 fix_problem(ctx
, PR_1B_BLOCK_ITERATE
, &pctx
);
351 ext2fs_close_inode_scan(scan
);
352 e2fsck_use_inode_shortcuts(ctx
, 0);
355 static int process_pass1b_block(ext2_filsys fs
EXT2FS_ATTR((unused
)),
357 e2_blkcnt_t blockcnt
,
358 blk64_t ref_blk
EXT2FS_ATTR((unused
)),
359 int ref_offset
EXT2FS_ATTR((unused
)),
362 struct process_block_struct
*p
;
367 if (HOLE_BLKADDR(*block_nr
))
369 p
= (struct process_block_struct
*) priv_data
;
371 lc
= EXT2FS_B2C(fs
, blockcnt
);
372 pc
= EXT2FS_B2C(fs
, *block_nr
);
374 if (!ext2fs_test_block_bitmap2(ctx
->block_dup_map
, *block_nr
))
377 /* OK, this is a duplicate block */
378 if (p
->ino
!= EXT2_BAD_INO
) {
379 if (p
->last_blk
+ 1 != *block_nr
) {
381 op
= p
->pctx
->blk
== p
->pctx
->blk2
?
384 fix_problem(ctx
, op
, p
->pctx
);
386 p
->pctx
->blk
= *block_nr
;
388 p
->pctx
->blk2
= *block_nr
;
389 p
->last_blk
= *block_nr
;
392 ext2fs_mark_inode_bitmap2(inode_dup_map
, p
->ino
);
395 * Qualifications for submitting a block for duplicate processing:
396 * It's an extent/indirect block (and has a negative logical offset);
397 * we've crossed a logical cluster boundary; or the physical cluster
398 * suddenly changed, which indicates that blocks in a logical cluster
399 * are mapped to multiple physical clusters.
401 if (blockcnt
< 0 || lc
!= p
->cur_cluster
|| pc
!= p
->phys_cluster
)
402 add_dupe(ctx
, p
->ino
, EXT2FS_B2C(fs
, *block_nr
), p
->inode
);
406 p
->phys_cluster
= pc
;
411 * Pass 1c: Scan directories for inodes with duplicate blocks. This
412 * is used so that we can print pathnames when prompting the user for
415 struct search_dir_struct
{
417 ext2_ino_t first_inode
;
418 ext2_ino_t max_inode
;
421 static int search_dirent_proc(ext2_ino_t dir
, int entry
,
422 struct ext2_dir_entry
*dirent
,
423 int offset
EXT2FS_ATTR((unused
)),
424 int blocksize
EXT2FS_ATTR((unused
)),
425 char *buf
EXT2FS_ATTR((unused
)),
428 struct search_dir_struct
*sd
;
432 sd
= (struct search_dir_struct
*) priv_data
;
434 if (dirent
->inode
> sd
->max_inode
)
435 /* Should abort this inode, but not everything */
438 if ((dirent
->inode
< sd
->first_inode
) || (entry
< DIRENT_OTHER_FILE
) ||
439 !ext2fs_test_inode_bitmap2(inode_dup_map
, dirent
->inode
))
442 n
= dict_lookup(&ino_dict
, INT_TO_VOIDPTR(dirent
->inode
));
445 p
= (struct dup_inode
*) dnode_get(n
);
451 return(sd
->count
? 0 : DIRENT_ABORT
);
455 static void pass1c(e2fsck_t ctx
, char *block_buf
)
457 ext2_filsys fs
= ctx
->fs
;
458 struct search_dir_struct sd
;
459 struct problem_context pctx
;
461 clear_problem_context(&pctx
);
463 if (!(ctx
->options
& E2F_OPT_PREEN
))
464 fix_problem(ctx
, PR_1C_PASS_HEADER
, &pctx
);
467 * Search through all directories to translate inodes to names
468 * (by searching for the containing directory for that inode.)
470 sd
.count
= dup_inode_count
- dup_inode_founddir
;
471 sd
.first_inode
= EXT2_FIRST_INODE(fs
->super
);
472 sd
.max_inode
= fs
->super
->s_inodes_count
;
473 ext2fs_dblist_dir_iterate(fs
->dblist
, 0, block_buf
,
474 search_dirent_proc
, &sd
);
477 static void pass1d(e2fsck_t ctx
, char *block_buf
)
479 ext2_filsys fs
= ctx
->fs
;
480 struct dup_inode
*p
, *t
;
481 struct dup_cluster
*q
;
482 ext2_ino_t
*shared
, ino
;
487 struct problem_context pctx
;
489 struct cluster_el
*s
;
492 clear_problem_context(&pctx
);
494 if (!(ctx
->options
& E2F_OPT_PREEN
))
495 fix_problem(ctx
, PR_1D_PASS_HEADER
, &pctx
);
496 e2fsck_read_bitmaps(ctx
);
498 pctx
.num
= dup_inode_count
; /* dict_count(&ino_dict); */
499 fix_problem(ctx
, PR_1D_NUM_DUP_INODES
, &pctx
);
500 shared
= (ext2_ino_t
*) e2fsck_allocate_memory(ctx
,
501 sizeof(ext2_ino_t
) * dict_count(&ino_dict
),
502 "Shared inode list");
503 for (n
= dict_first(&ino_dict
); n
; n
= dict_next(&ino_dict
, n
)) {
504 p
= (struct dup_inode
*) dnode_get(n
);
507 ino
= (ext2_ino_t
)VOIDPTR_TO_INT(dnode_getkey(n
));
508 if (ino
== EXT2_BAD_INO
|| ino
== EXT2_RESIZE_INO
)
512 * Find all of the inodes which share blocks with this
513 * one. First we find all of the duplicate blocks
514 * belonging to this inode, and then search each block
515 * get the list of inodes, and merge them together.
517 for (s
= p
->cluster_list
; s
; s
= s
->next
) {
518 m
= dict_lookup(&clstr_dict
,
519 INT_TO_VOIDPTR(s
->cluster
));
521 continue; /* Should never happen... */
522 q
= (struct dup_cluster
*) dnode_get(m
);
525 if (check_if_fs_cluster(ctx
, s
->cluster
)) {
531 * Add all inodes used by this block to the
532 * shared[] --- which is a unique list, so
533 * if an inode is already in shared[], don't
536 for (r
= q
->inode_list
; r
; r
= r
->next
) {
539 for (i
= 0; i
< shared_len
; i
++)
540 if (shared
[i
] == r
->inode
)
542 if (i
== shared_len
) {
543 shared
[shared_len
++] = r
->inode
;
549 * Report the inode that we are working on
551 pctx
.inode
= &p
->inode
;
554 pctx
.blkcount
= p
->num_dupblocks
;
555 pctx
.num
= meta_data
? shared_len
+1 : shared_len
;
556 fix_problem(ctx
, PR_1D_DUP_FILE
, &pctx
);
561 fix_problem(ctx
, PR_1D_SHARE_METADATA
, &pctx
);
563 for (i
= 0; i
< shared_len
; i
++) {
564 m
= dict_lookup(&ino_dict
, INT_TO_VOIDPTR(shared
[i
]));
566 continue; /* should never happen */
567 t
= (struct dup_inode
*) dnode_get(m
);
569 * Report the inode that we are sharing with
571 pctx
.inode
= &t
->inode
;
572 pctx
.ino
= shared
[i
];
574 fix_problem(ctx
, PR_1D_DUP_FILE_LIST
, &pctx
);
577 * Even if the file shares blocks with itself, we still need to
580 if (file_ok
&& (meta_data
? shared_len
+1 : shared_len
) != 0) {
581 fix_problem(ctx
, PR_1D_DUP_BLOCKS_DEALT
, &pctx
);
584 if (fix_problem(ctx
, PR_1D_CLONE_QUESTION
, &pctx
)) {
585 pctx
.errcode
= clone_file(ctx
, ino
, p
, block_buf
);
587 fix_problem(ctx
, PR_1D_CLONE_ERROR
, &pctx
);
591 if (fix_problem(ctx
, PR_1D_DELETE_QUESTION
, &pctx
))
592 delete_file(ctx
, ino
, p
, block_buf
);
594 ext2fs_unmark_valid(fs
);
596 ext2fs_free_mem(&shared
);
600 * Drop the refcount on the dup_block structure, and clear the entry
601 * in the block_dup_map if appropriate.
603 static void decrement_badcount(e2fsck_t ctx
, blk64_t block
,
604 struct dup_cluster
*p
)
607 if (p
->num_bad
<= 0 ||
608 (p
->num_bad
== 1 && !check_if_fs_block(ctx
, block
))) {
609 if (check_if_fs_cluster(ctx
, EXT2FS_B2C(ctx
->fs
, block
)))
611 ext2fs_unmark_block_bitmap2(ctx
->block_dup_map
, block
);
615 static int delete_file_block(ext2_filsys fs
,
617 e2_blkcnt_t blockcnt
,
618 blk64_t ref_block
EXT2FS_ATTR((unused
)),
619 int ref_offset
EXT2FS_ATTR((unused
)),
622 struct process_block_struct
*pb
;
623 struct dup_cluster
*p
;
628 pb
= (struct process_block_struct
*) priv_data
;
631 if (HOLE_BLKADDR(*block_nr
))
634 c
= EXT2FS_B2C(fs
, *block_nr
);
635 lc
= EXT2FS_B2C(fs
, blockcnt
);
636 if (ext2fs_test_block_bitmap2(ctx
->block_dup_map
, *block_nr
)) {
637 n
= dict_lookup(&clstr_dict
, INT_TO_VOIDPTR(c
));
639 p
= (struct dup_cluster
*) dnode_get(n
);
640 if (lc
!= pb
->cur_cluster
)
641 decrement_badcount(ctx
, *block_nr
, p
);
643 com_err("delete_file_block", 0,
644 _("internal error: can't find dup_blk for %llu\n"),
647 if ((*block_nr
% EXT2FS_CLUSTER_RATIO(ctx
->fs
)) == 0)
648 ext2fs_block_alloc_stats2(fs
, *block_nr
, -1);
651 pb
->cur_cluster
= lc
;
656 static void delete_file(e2fsck_t ctx
, ext2_ino_t ino
,
657 struct dup_inode
*dp
, char* block_buf
)
659 ext2_filsys fs
= ctx
->fs
;
660 struct process_block_struct pb
;
661 struct problem_context pctx
;
664 clear_problem_context(&pctx
);
665 pctx
.ino
= pb
.ino
= ino
;
668 pctx
.str
= "delete_file";
671 if (ext2fs_inode_has_valid_blocks2(fs
, &dp
->inode
))
672 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
,
673 BLOCK_FLAG_READ_ONLY
,
675 delete_file_block
, &pb
);
677 fix_problem(ctx
, PR_1B_BLOCK_ITERATE
, &pctx
);
678 if (ctx
->inode_bad_map
)
679 ext2fs_unmark_inode_bitmap2(ctx
->inode_bad_map
, ino
);
680 ext2fs_inode_alloc_stats2(fs
, ino
, -1, LINUX_S_ISDIR(dp
->inode
.i_mode
));
681 quota_data_sub(ctx
->qctx
, &dp
->inode
, ino
,
682 pb
.dup_blocks
* fs
->blocksize
);
683 quota_data_inodes(ctx
->qctx
, &dp
->inode
, ino
, -1);
685 /* Inode may have changed by block_iterate, so reread it */
686 e2fsck_read_inode(ctx
, ino
, &dp
->inode
, "delete_file");
687 e2fsck_clear_inode(ctx
, ino
, &dp
->inode
, 0, "delete_file");
688 if (ext2fs_file_acl_block(fs
, &dp
->inode
) &&
689 (fs
->super
->s_feature_compat
& EXT2_FEATURE_COMPAT_EXT_ATTR
)) {
691 pctx
.errcode
= ext2fs_adjust_ea_refcount3(fs
,
692 ext2fs_file_acl_block(fs
, &dp
->inode
),
693 block_buf
, -1, &count
, ino
);
694 if (pctx
.errcode
== EXT2_ET_BAD_EA_BLOCK_NUM
) {
699 pctx
.blk
= ext2fs_file_acl_block(fs
, &dp
->inode
);
700 fix_problem(ctx
, PR_1B_ADJ_EA_REFCOUNT
, &pctx
);
703 * If the count is zero, then arrange to have the
704 * block deleted. If the block is in the block_dup_map,
705 * also call delete_file_block since it will take care
706 * of keeping the accounting straight.
709 ext2fs_test_block_bitmap2(ctx
->block_dup_map
,
710 ext2fs_file_acl_block(fs
, &dp
->inode
))) {
711 blk64_t blk
= ext2fs_file_acl_block(fs
, &dp
->inode
);
712 delete_file_block(fs
, &blk
,
713 BLOCK_COUNT_EXTATTR
, 0, 0, &pb
);
714 ext2fs_file_acl_block_set(fs
, &dp
->inode
, blk
);
715 quota_data_sub(ctx
->qctx
, &dp
->inode
, ino
, fs
->blocksize
);
720 struct clone_struct
{
727 struct ext2_inode
*inode
;
730 static int clone_file_block(ext2_filsys fs
,
732 e2_blkcnt_t blockcnt
,
733 blk64_t ref_block
EXT2FS_ATTR((unused
)),
734 int ref_offset
EXT2FS_ATTR((unused
)),
737 struct dup_cluster
*p
;
740 struct clone_struct
*cs
= (struct clone_struct
*) priv_data
;
748 if (HOLE_BLKADDR(*block_nr
))
751 c
= EXT2FS_B2C(fs
, blockcnt
);
752 if (check_if_fs_cluster(ctx
, EXT2FS_B2C(fs
, *block_nr
)))
755 if (c
== cs
->dup_cluster
&& cs
->alloc_block
) {
756 new_block
= cs
->alloc_block
;
760 if (ext2fs_test_block_bitmap2(ctx
->block_dup_map
, *block_nr
)) {
761 n
= dict_lookup(&clstr_dict
,
762 INT_TO_VOIDPTR(EXT2FS_B2C(fs
, *block_nr
)));
764 com_err("clone_file_block", 0,
765 _("internal error: can't find dup_blk for %llu\n"),
770 p
= (struct dup_cluster
*) dnode_get(n
);
772 decrement_badcount(ctx
, *block_nr
, p
);
776 * Let's try an implied cluster allocation. If we get the same
777 * cluster back, then we need to find a new block; otherwise,
778 * we're merely fixing the problem of one logical cluster being
779 * mapped to multiple physical clusters.
782 retval
= ext2fs_map_cluster_block(fs
, cs
->ino
, cs
->inode
,
783 blockcnt
, &new_block
);
784 if (retval
== 0 && new_block
!= 0 &&
785 EXT2FS_B2C(ctx
->fs
, new_block
) !=
786 EXT2FS_B2C(ctx
->fs
, *block_nr
))
787 goto cluster_alloc_ok
;
788 retval
= ext2fs_new_block2(fs
, 0, ctx
->block_found_map
,
791 cs
->errcode
= retval
;
795 cs
->alloc_block
= new_block
;
798 new_block
&= ~EXT2FS_CLUSTER_MASK(fs
);
799 new_block
+= EXT2FS_CLUSTER_MASK(fs
) & blockcnt
;
800 if (cs
->dir
&& (blockcnt
>= 0)) {
801 retval
= ext2fs_set_dir_block2(fs
->dblist
,
802 cs
->dir
, new_block
, blockcnt
);
804 cs
->errcode
= retval
;
809 printf("Cloning block #%lld from %llu to %llu\n",
810 blockcnt
, *block_nr
, new_block
);
812 retval
= io_channel_read_blk64(fs
->io
, *block_nr
, 1, cs
->buf
);
814 cs
->errcode
= retval
;
817 retval
= io_channel_write_blk64(fs
->io
, new_block
, 1, cs
->buf
);
819 cs
->errcode
= retval
;
822 *block_nr
= new_block
;
823 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, new_block
);
824 ext2fs_mark_block_bitmap2(fs
->block_map
, new_block
);
825 return BLOCK_CHANGED
;
830 static errcode_t
clone_file(e2fsck_t ctx
, ext2_ino_t ino
,
831 struct dup_inode
*dp
, char* block_buf
)
833 ext2_filsys fs
= ctx
->fs
;
835 struct clone_struct cs
;
836 struct problem_context pctx
;
837 blk64_t blk
, new_blk
;
839 struct inode_el
*ino_el
;
840 struct dup_cluster
*dc
;
841 struct dup_inode
*di
;
843 clear_problem_context(&pctx
);
850 cs
.inode
= &dp
->inode
;
851 retval
= ext2fs_get_mem(fs
->blocksize
, &cs
.buf
);
855 if (ext2fs_test_inode_bitmap2(ctx
->inode_dir_map
, ino
))
859 pctx
.str
= "clone_file";
860 if (ext2fs_inode_has_valid_blocks2(fs
, &dp
->inode
))
861 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
, 0, block_buf
,
862 clone_file_block
, &cs
);
863 ext2fs_mark_bb_dirty(fs
);
865 fix_problem(ctx
, PR_1B_BLOCK_ITERATE
, &pctx
);
866 retval
= pctx
.errcode
;
870 com_err("clone_file", cs
.errcode
, "%s",
871 _("returned from clone_file_block"));
875 /* The inode may have changed on disk, so we have to re-read it */
876 e2fsck_read_inode(ctx
, ino
, &dp
->inode
, "clone file EA");
877 blk
= ext2fs_file_acl_block(fs
, &dp
->inode
);
879 if (blk
&& (clone_file_block(fs
, &new_blk
,
880 BLOCK_COUNT_EXTATTR
, 0, 0, &cs
) ==
882 ext2fs_file_acl_block_set(fs
, &dp
->inode
, new_blk
);
883 e2fsck_write_inode(ctx
, ino
, &dp
->inode
, "clone file EA");
885 * If we cloned the EA block, find all other inodes
886 * which refered to that EA block, and modify
887 * them to point to the new EA block.
889 n
= dict_lookup(&clstr_dict
,
890 INT_TO_VOIDPTR(EXT2FS_B2C(fs
, blk
)));
892 com_err("clone_file", 0,
893 _("internal error: couldn't lookup EA "
894 "block record for %llu"), blk
);
895 retval
= 0; /* OK to stumble on... */
898 dc
= (struct dup_cluster
*) dnode_get(n
);
899 for (ino_el
= dc
->inode_list
; ino_el
; ino_el
= ino_el
->next
) {
900 if (ino_el
->inode
== ino
)
902 n
= dict_lookup(&ino_dict
, INT_TO_VOIDPTR(ino_el
->inode
));
904 com_err("clone_file", 0,
905 _("internal error: couldn't lookup EA "
906 "inode record for %u"),
908 retval
= 0; /* OK to stumble on... */
911 di
= (struct dup_inode
*) dnode_get(n
);
912 if (ext2fs_file_acl_block(fs
, &di
->inode
) == blk
) {
913 ext2fs_file_acl_block_set(fs
, &di
->inode
,
914 ext2fs_file_acl_block(fs
, &dp
->inode
));
915 e2fsck_write_inode(ctx
, ino_el
->inode
,
916 &di
->inode
, "clone file EA");
917 decrement_badcount(ctx
, blk
, dc
);
923 ext2fs_free_mem(&cs
.buf
);
928 * This routine returns 1 if a block overlaps with one of the superblocks,
929 * group descriptors, inode bitmaps, or block bitmaps.
931 static int check_if_fs_block(e2fsck_t ctx
, blk64_t test_block
)
933 ext2_filsys fs
= ctx
->fs
;
937 first_block
= fs
->super
->s_first_data_block
;
938 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
940 /* Check superblocks/block group descriptors */
941 if (ext2fs_bg_has_super(fs
, i
)) {
942 if (test_block
>= first_block
&&
943 (test_block
<= first_block
+ fs
->desc_blocks
))
947 /* Check the inode table */
948 if ((ext2fs_inode_table_loc(fs
, i
)) &&
949 (test_block
>= ext2fs_inode_table_loc(fs
, i
)) &&
950 (test_block
< (ext2fs_inode_table_loc(fs
, i
) +
951 fs
->inode_blocks_per_group
)))
954 /* Check the bitmap blocks */
955 if ((test_block
== ext2fs_block_bitmap_loc(fs
, i
)) ||
956 (test_block
== ext2fs_inode_bitmap_loc(fs
, i
)))
959 first_block
+= fs
->super
->s_blocks_per_group
;
965 * This routine returns 1 if a cluster overlaps with one of the superblocks,
966 * group descriptors, inode bitmaps, or block bitmaps.
968 static int check_if_fs_cluster(e2fsck_t ctx
, blk64_t cluster
)
970 ext2_filsys fs
= ctx
->fs
;
974 first_block
= fs
->super
->s_first_data_block
;
975 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
977 /* Check superblocks/block group descriptors */
978 if (ext2fs_bg_has_super(fs
, i
)) {
979 if (cluster
>= EXT2FS_B2C(fs
, first_block
) &&
980 (cluster
<= EXT2FS_B2C(fs
, first_block
+
985 /* Check the inode table */
986 if ((ext2fs_inode_table_loc(fs
, i
)) &&
987 (cluster
>= EXT2FS_B2C(fs
,
988 ext2fs_inode_table_loc(fs
, i
))) &&
989 (cluster
<= EXT2FS_B2C(fs
,
990 ext2fs_inode_table_loc(fs
, i
) +
991 fs
->inode_blocks_per_group
- 1)))
994 /* Check the bitmap blocks */
995 if ((cluster
== EXT2FS_B2C(fs
,
996 ext2fs_block_bitmap_loc(fs
, i
))) ||
997 (cluster
== EXT2FS_B2C(fs
,
998 ext2fs_inode_bitmap_loc(fs
, i
))))
1001 first_block
+= fs
->super
->s_blocks_per_group
;