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
{
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
)
304 fix_problem(ctx
, PR_1B_ISCAN_ERROR
, &pctx
);
305 ctx
->flags
|= E2F_FLAG_ABORT
;
310 pctx
.ino
= ctx
->stashed_ino
= ino
;
311 if ((ino
!= EXT2_BAD_INO
) &&
312 !ext2fs_test_inode_bitmap2(ctx
->inode_used_map
, ino
))
320 pb
.pctx
->blk
= pb
.pctx
->blk2
= 0;
322 if (ext2fs_inode_has_valid_blocks2(fs
, &inode
) ||
323 (ino
== EXT2_BAD_INO
))
324 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
,
325 BLOCK_FLAG_READ_ONLY
, block_buf
,
326 process_pass1b_block
, &pb
);
327 /* If the feature is not set, attrs will be cleared later anyway */
328 if ((fs
->super
->s_feature_compat
& EXT2_FEATURE_COMPAT_EXT_ATTR
) &&
329 ext2fs_file_acl_block(fs
, &inode
)) {
330 blk64_t blk
= ext2fs_file_acl_block(fs
, &inode
);
331 process_pass1b_block(fs
, &blk
,
332 BLOCK_COUNT_EXTATTR
, 0, 0, &pb
);
333 ext2fs_file_acl_block_set(fs
, &inode
, blk
);
336 if (ino
!= EXT2_BAD_INO
) {
337 op
= pctx
.blk
== pctx
.blk2
?
338 PR_1B_DUP_BLOCK
: PR_1B_DUP_RANGE
;
339 fix_problem(ctx
, op
, pb
.pctx
);
341 end_problem_latch(ctx
, PR_LATCH_DBLOCK
);
342 if (ino
>= EXT2_FIRST_INODE(fs
->super
) ||
343 ino
== EXT2_ROOT_INO
)
347 fix_problem(ctx
, PR_1B_BLOCK_ITERATE
, &pctx
);
349 ext2fs_close_inode_scan(scan
);
350 e2fsck_use_inode_shortcuts(ctx
, 0);
353 static int process_pass1b_block(ext2_filsys fs
EXT2FS_ATTR((unused
)),
355 e2_blkcnt_t blockcnt
,
356 blk64_t ref_blk
EXT2FS_ATTR((unused
)),
357 int ref_offset
EXT2FS_ATTR((unused
)),
360 struct process_block_struct
*p
;
365 if (HOLE_BLKADDR(*block_nr
))
367 p
= (struct process_block_struct
*) priv_data
;
369 lc
= EXT2FS_B2C(fs
, blockcnt
);
371 if (!ext2fs_test_block_bitmap2(ctx
->block_dup_map
, *block_nr
))
374 /* OK, this is a duplicate block */
375 if (p
->ino
!= EXT2_BAD_INO
) {
376 if (p
->last_blk
+ 1 != *block_nr
) {
378 op
= p
->pctx
->blk
== p
->pctx
->blk2
?
381 fix_problem(ctx
, op
, p
->pctx
);
383 p
->pctx
->blk
= *block_nr
;
385 p
->pctx
->blk2
= *block_nr
;
386 p
->last_blk
= *block_nr
;
389 ext2fs_mark_inode_bitmap2(inode_dup_map
, p
->ino
);
391 if (lc
!= p
->cur_cluster
)
392 add_dupe(ctx
, p
->ino
, EXT2FS_B2C(fs
, *block_nr
), p
->inode
);
400 * Pass 1c: Scan directories for inodes with duplicate blocks. This
401 * is used so that we can print pathnames when prompting the user for
404 struct search_dir_struct
{
406 ext2_ino_t first_inode
;
407 ext2_ino_t max_inode
;
410 static int search_dirent_proc(ext2_ino_t dir
, int entry
,
411 struct ext2_dir_entry
*dirent
,
412 int offset
EXT2FS_ATTR((unused
)),
413 int blocksize
EXT2FS_ATTR((unused
)),
414 char *buf
EXT2FS_ATTR((unused
)),
417 struct search_dir_struct
*sd
;
421 sd
= (struct search_dir_struct
*) priv_data
;
423 if (dirent
->inode
> sd
->max_inode
)
424 /* Should abort this inode, but not everything */
427 if ((dirent
->inode
< sd
->first_inode
) || (entry
< DIRENT_OTHER_FILE
) ||
428 !ext2fs_test_inode_bitmap2(inode_dup_map
, dirent
->inode
))
431 n
= dict_lookup(&ino_dict
, INT_TO_VOIDPTR(dirent
->inode
));
434 p
= (struct dup_inode
*) dnode_get(n
);
440 return(sd
->count
? 0 : DIRENT_ABORT
);
444 static void pass1c(e2fsck_t ctx
, char *block_buf
)
446 ext2_filsys fs
= ctx
->fs
;
447 struct search_dir_struct sd
;
448 struct problem_context pctx
;
450 clear_problem_context(&pctx
);
452 if (!(ctx
->options
& E2F_OPT_PREEN
))
453 fix_problem(ctx
, PR_1C_PASS_HEADER
, &pctx
);
456 * Search through all directories to translate inodes to names
457 * (by searching for the containing directory for that inode.)
459 sd
.count
= dup_inode_count
- dup_inode_founddir
;
460 sd
.first_inode
= EXT2_FIRST_INODE(fs
->super
);
461 sd
.max_inode
= fs
->super
->s_inodes_count
;
462 ext2fs_dblist_dir_iterate(fs
->dblist
, 0, block_buf
,
463 search_dirent_proc
, &sd
);
466 static void pass1d(e2fsck_t ctx
, char *block_buf
)
468 ext2_filsys fs
= ctx
->fs
;
469 struct dup_inode
*p
, *t
;
470 struct dup_cluster
*q
;
471 ext2_ino_t
*shared
, ino
;
476 struct problem_context pctx
;
478 struct cluster_el
*s
;
481 clear_problem_context(&pctx
);
483 if (!(ctx
->options
& E2F_OPT_PREEN
))
484 fix_problem(ctx
, PR_1D_PASS_HEADER
, &pctx
);
485 e2fsck_read_bitmaps(ctx
);
487 pctx
.num
= dup_inode_count
; /* dict_count(&ino_dict); */
488 fix_problem(ctx
, PR_1D_NUM_DUP_INODES
, &pctx
);
489 shared
= (ext2_ino_t
*) e2fsck_allocate_memory(ctx
,
490 sizeof(ext2_ino_t
) * dict_count(&ino_dict
),
491 "Shared inode list");
492 for (n
= dict_first(&ino_dict
); n
; n
= dict_next(&ino_dict
, n
)) {
493 p
= (struct dup_inode
*) dnode_get(n
);
496 ino
= (ext2_ino_t
)VOIDPTR_TO_INT(dnode_getkey(n
));
497 if (ino
== EXT2_BAD_INO
|| ino
== EXT2_RESIZE_INO
)
501 * Find all of the inodes which share blocks with this
502 * one. First we find all of the duplicate blocks
503 * belonging to this inode, and then search each block
504 * get the list of inodes, and merge them together.
506 for (s
= p
->cluster_list
; s
; s
= s
->next
) {
507 m
= dict_lookup(&clstr_dict
,
508 INT_TO_VOIDPTR(s
->cluster
));
510 continue; /* Should never happen... */
511 q
= (struct dup_cluster
*) dnode_get(m
);
514 if (check_if_fs_cluster(ctx
, s
->cluster
)) {
520 * Add all inodes used by this block to the
521 * shared[] --- which is a unique list, so
522 * if an inode is already in shared[], don't
525 for (r
= q
->inode_list
; r
; r
= r
->next
) {
528 for (i
= 0; i
< shared_len
; i
++)
529 if (shared
[i
] == r
->inode
)
531 if (i
== shared_len
) {
532 shared
[shared_len
++] = r
->inode
;
538 * Report the inode that we are working on
540 pctx
.inode
= &p
->inode
;
543 pctx
.blkcount
= p
->num_dupblocks
;
544 pctx
.num
= meta_data
? shared_len
+1 : shared_len
;
545 fix_problem(ctx
, PR_1D_DUP_FILE
, &pctx
);
550 fix_problem(ctx
, PR_1D_SHARE_METADATA
, &pctx
);
552 for (i
= 0; i
< shared_len
; i
++) {
553 m
= dict_lookup(&ino_dict
, INT_TO_VOIDPTR(shared
[i
]));
555 continue; /* should never happen */
556 t
= (struct dup_inode
*) dnode_get(m
);
558 * Report the inode that we are sharing with
560 pctx
.inode
= &t
->inode
;
561 pctx
.ino
= shared
[i
];
563 fix_problem(ctx
, PR_1D_DUP_FILE_LIST
, &pctx
);
566 fix_problem(ctx
, PR_1D_DUP_BLOCKS_DEALT
, &pctx
);
569 if (fix_problem(ctx
, PR_1D_CLONE_QUESTION
, &pctx
)) {
570 pctx
.errcode
= clone_file(ctx
, ino
, p
, block_buf
);
572 fix_problem(ctx
, PR_1D_CLONE_ERROR
, &pctx
);
576 if (fix_problem(ctx
, PR_1D_DELETE_QUESTION
, &pctx
))
577 delete_file(ctx
, ino
, p
, block_buf
);
579 ext2fs_unmark_valid(fs
);
581 ext2fs_free_mem(&shared
);
585 * Drop the refcount on the dup_block structure, and clear the entry
586 * in the block_dup_map if appropriate.
588 static void decrement_badcount(e2fsck_t ctx
, blk64_t block
,
589 struct dup_cluster
*p
)
592 if (p
->num_bad
<= 0 ||
593 (p
->num_bad
== 1 && !check_if_fs_block(ctx
, block
))) {
594 if (check_if_fs_cluster(ctx
, EXT2FS_B2C(ctx
->fs
, block
)))
596 ext2fs_unmark_block_bitmap2(ctx
->block_dup_map
, block
);
600 static int delete_file_block(ext2_filsys fs
,
602 e2_blkcnt_t blockcnt
,
603 blk64_t ref_block
EXT2FS_ATTR((unused
)),
604 int ref_offset
EXT2FS_ATTR((unused
)),
607 struct process_block_struct
*pb
;
608 struct dup_cluster
*p
;
613 pb
= (struct process_block_struct
*) priv_data
;
616 if (HOLE_BLKADDR(*block_nr
))
619 c
= EXT2FS_B2C(fs
, *block_nr
);
620 lc
= EXT2FS_B2C(fs
, blockcnt
);
621 if (ext2fs_test_block_bitmap2(ctx
->block_dup_map
, *block_nr
)) {
622 n
= dict_lookup(&clstr_dict
, INT_TO_VOIDPTR(c
));
624 p
= (struct dup_cluster
*) dnode_get(n
);
625 if (lc
!= pb
->cur_cluster
)
626 decrement_badcount(ctx
, *block_nr
, p
);
628 com_err("delete_file_block", 0,
629 _("internal error: can't find dup_blk for %llu\n"),
632 ext2fs_unmark_block_bitmap2(ctx
->block_found_map
, *block_nr
);
633 ext2fs_block_alloc_stats2(fs
, *block_nr
, -1);
636 pb
->cur_cluster
= lc
;
641 static void delete_file(e2fsck_t ctx
, ext2_ino_t ino
,
642 struct dup_inode
*dp
, char* block_buf
)
644 ext2_filsys fs
= ctx
->fs
;
645 struct process_block_struct pb
;
646 struct problem_context pctx
;
649 clear_problem_context(&pctx
);
650 pctx
.ino
= pb
.ino
= ino
;
653 pctx
.str
= "delete_file";
656 if (ext2fs_inode_has_valid_blocks2(fs
, &dp
->inode
))
657 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
,
658 BLOCK_FLAG_READ_ONLY
,
660 delete_file_block
, &pb
);
662 fix_problem(ctx
, PR_1B_BLOCK_ITERATE
, &pctx
);
663 if (ctx
->inode_bad_map
)
664 ext2fs_unmark_inode_bitmap2(ctx
->inode_bad_map
, ino
);
665 ext2fs_inode_alloc_stats2(fs
, ino
, -1, LINUX_S_ISDIR(dp
->inode
.i_mode
));
666 quota_data_sub(ctx
->qctx
, &dp
->inode
, ino
,
667 pb
.dup_blocks
* fs
->blocksize
);
668 quota_data_inodes(ctx
->qctx
, &dp
->inode
, ino
, -1);
670 /* Inode may have changed by block_iterate, so reread it */
671 e2fsck_read_inode(ctx
, ino
, &dp
->inode
, "delete_file");
672 e2fsck_clear_inode(ctx
, ino
, &dp
->inode
, 0, "delete_file");
673 if (ext2fs_file_acl_block(fs
, &dp
->inode
) &&
674 (fs
->super
->s_feature_compat
& EXT2_FEATURE_COMPAT_EXT_ATTR
)) {
676 pctx
.errcode
= ext2fs_adjust_ea_refcount3(fs
,
677 ext2fs_file_acl_block(fs
, &dp
->inode
),
678 block_buf
, -1, &count
, ino
);
679 if (pctx
.errcode
== EXT2_ET_BAD_EA_BLOCK_NUM
) {
684 pctx
.blk
= ext2fs_file_acl_block(fs
, &dp
->inode
);
685 fix_problem(ctx
, PR_1B_ADJ_EA_REFCOUNT
, &pctx
);
688 * If the count is zero, then arrange to have the
689 * block deleted. If the block is in the block_dup_map,
690 * also call delete_file_block since it will take care
691 * of keeping the accounting straight.
694 ext2fs_test_block_bitmap2(ctx
->block_dup_map
,
695 ext2fs_file_acl_block(fs
, &dp
->inode
))) {
696 blk64_t blk
= ext2fs_file_acl_block(fs
, &dp
->inode
);
697 delete_file_block(fs
, &blk
,
698 BLOCK_COUNT_EXTATTR
, 0, 0, &pb
);
699 ext2fs_file_acl_block_set(fs
, &dp
->inode
, blk
);
700 quota_data_sub(ctx
->qctx
, &dp
->inode
, ino
, fs
->blocksize
);
705 struct clone_struct
{
714 static int clone_file_block(ext2_filsys fs
,
716 e2_blkcnt_t blockcnt
,
717 blk64_t ref_block
EXT2FS_ATTR((unused
)),
718 int ref_offset
EXT2FS_ATTR((unused
)),
721 struct dup_cluster
*p
;
724 struct clone_struct
*cs
= (struct clone_struct
*) priv_data
;
732 if (HOLE_BLKADDR(*block_nr
))
735 c
= EXT2FS_B2C(fs
, blockcnt
);
736 if (check_if_fs_cluster(ctx
, EXT2FS_B2C(fs
, *block_nr
)))
739 if (c
== cs
->dup_cluster
&& cs
->alloc_block
) {
740 new_block
= cs
->alloc_block
;
744 if (ext2fs_test_block_bitmap2(ctx
->block_dup_map
, *block_nr
)) {
745 n
= dict_lookup(&clstr_dict
,
746 INT_TO_VOIDPTR(EXT2FS_B2C(fs
, *block_nr
)));
748 com_err("clone_file_block", 0,
749 _("internal error: can't find dup_blk for %llu\n"),
754 p
= (struct dup_cluster
*) dnode_get(n
);
756 decrement_badcount(ctx
, *block_nr
, p
);
760 retval
= ext2fs_new_block2(fs
, 0, ctx
->block_found_map
,
763 cs
->errcode
= retval
;
766 cs
->alloc_block
= new_block
;
769 new_block
&= ~EXT2FS_CLUSTER_MASK(fs
);
770 new_block
+= EXT2FS_CLUSTER_MASK(fs
) & blockcnt
;
771 if (cs
->dir
&& (blockcnt
>= 0)) {
772 retval
= ext2fs_set_dir_block2(fs
->dblist
,
773 cs
->dir
, new_block
, blockcnt
);
775 cs
->errcode
= retval
;
780 printf("Cloning block #%lld from %llu to %llu\n",
781 blockcnt
, *block_nr
, new_block
);
783 retval
= io_channel_read_blk64(fs
->io
, *block_nr
, 1, cs
->buf
);
785 cs
->errcode
= retval
;
788 retval
= io_channel_write_blk64(fs
->io
, new_block
, 1, cs
->buf
);
790 cs
->errcode
= retval
;
793 *block_nr
= new_block
;
794 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, new_block
);
795 ext2fs_mark_block_bitmap2(fs
->block_map
, new_block
);
796 return BLOCK_CHANGED
;
801 static errcode_t
clone_file(e2fsck_t ctx
, ext2_ino_t ino
,
802 struct dup_inode
*dp
, char* block_buf
)
804 ext2_filsys fs
= ctx
->fs
;
806 struct clone_struct cs
;
807 struct problem_context pctx
;
808 blk64_t blk
, new_blk
;
810 struct inode_el
*ino_el
;
811 struct dup_cluster
*dc
;
812 struct dup_inode
*di
;
814 clear_problem_context(&pctx
);
820 retval
= ext2fs_get_mem(fs
->blocksize
, &cs
.buf
);
824 if (ext2fs_test_inode_bitmap2(ctx
->inode_dir_map
, ino
))
828 pctx
.str
= "clone_file";
829 if (ext2fs_inode_has_valid_blocks2(fs
, &dp
->inode
))
830 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
, 0, block_buf
,
831 clone_file_block
, &cs
);
832 ext2fs_mark_bb_dirty(fs
);
834 fix_problem(ctx
, PR_1B_BLOCK_ITERATE
, &pctx
);
835 retval
= pctx
.errcode
;
839 com_err("clone_file", cs
.errcode
, "%s",
840 _("returned from clone_file_block"));
844 /* The inode may have changed on disk, so we have to re-read it */
845 e2fsck_read_inode(ctx
, ino
, &dp
->inode
, "clone file EA");
846 blk
= ext2fs_file_acl_block(fs
, &dp
->inode
);
848 if (blk
&& (clone_file_block(fs
, &new_blk
,
849 BLOCK_COUNT_EXTATTR
, 0, 0, &cs
) ==
851 ext2fs_file_acl_block_set(fs
, &dp
->inode
, new_blk
);
852 e2fsck_write_inode(ctx
, ino
, &dp
->inode
, "clone file EA");
854 * If we cloned the EA block, find all other inodes
855 * which refered to that EA block, and modify
856 * them to point to the new EA block.
858 n
= dict_lookup(&clstr_dict
,
859 INT_TO_VOIDPTR(EXT2FS_B2C(fs
, blk
)));
861 com_err("clone_file", 0,
862 _("internal error: couldn't lookup EA "
863 "block record for %llu"), blk
);
864 retval
= 0; /* OK to stumble on... */
867 dc
= (struct dup_cluster
*) dnode_get(n
);
868 for (ino_el
= dc
->inode_list
; ino_el
; ino_el
= ino_el
->next
) {
869 if (ino_el
->inode
== ino
)
871 n
= dict_lookup(&ino_dict
, INT_TO_VOIDPTR(ino_el
->inode
));
873 com_err("clone_file", 0,
874 _("internal error: couldn't lookup EA "
875 "inode record for %u"),
877 retval
= 0; /* OK to stumble on... */
880 di
= (struct dup_inode
*) dnode_get(n
);
881 if (ext2fs_file_acl_block(fs
, &di
->inode
) == blk
) {
882 ext2fs_file_acl_block_set(fs
, &di
->inode
,
883 ext2fs_file_acl_block(fs
, &dp
->inode
));
884 e2fsck_write_inode(ctx
, ino_el
->inode
,
885 &di
->inode
, "clone file EA");
886 decrement_badcount(ctx
, blk
, dc
);
892 ext2fs_free_mem(&cs
.buf
);
897 * This routine returns 1 if a block overlaps with one of the superblocks,
898 * group descriptors, inode bitmaps, or block bitmaps.
900 static int check_if_fs_block(e2fsck_t ctx
, blk64_t test_block
)
902 ext2_filsys fs
= ctx
->fs
;
906 first_block
= fs
->super
->s_first_data_block
;
907 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
909 /* Check superblocks/block group descriptors */
910 if (ext2fs_bg_has_super(fs
, i
)) {
911 if (test_block
>= first_block
&&
912 (test_block
<= first_block
+ fs
->desc_blocks
))
916 /* Check the inode table */
917 if ((ext2fs_inode_table_loc(fs
, i
)) &&
918 (test_block
>= ext2fs_inode_table_loc(fs
, i
)) &&
919 (test_block
< (ext2fs_inode_table_loc(fs
, i
) +
920 fs
->inode_blocks_per_group
)))
923 /* Check the bitmap blocks */
924 if ((test_block
== ext2fs_block_bitmap_loc(fs
, i
)) ||
925 (test_block
== ext2fs_inode_bitmap_loc(fs
, i
)))
928 first_block
+= fs
->super
->s_blocks_per_group
;
934 * This routine returns 1 if a cluster overlaps with one of the superblocks,
935 * group descriptors, inode bitmaps, or block bitmaps.
937 static int check_if_fs_cluster(e2fsck_t ctx
, blk64_t cluster
)
939 ext2_filsys fs
= ctx
->fs
;
943 first_block
= fs
->super
->s_first_data_block
;
944 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
946 /* Check superblocks/block group descriptors */
947 if (ext2fs_bg_has_super(fs
, i
)) {
948 if (cluster
>= EXT2FS_B2C(fs
, first_block
) &&
949 (cluster
<= EXT2FS_B2C(fs
, first_block
+
954 /* Check the inode table */
955 if ((ext2fs_inode_table_loc(fs
, i
)) &&
956 (cluster
>= EXT2FS_B2C(fs
,
957 ext2fs_inode_table_loc(fs
, i
))) &&
958 (cluster
<= EXT2FS_B2C(fs
,
959 ext2fs_inode_table_loc(fs
, i
) +
960 fs
->inode_blocks_per_group
- 1)))
963 /* Check the bitmap blocks */
964 if ((cluster
== EXT2FS_B2C(fs
,
965 ext2fs_block_bitmap_loc(fs
, i
))) ||
966 (cluster
== EXT2FS_B2C(fs
,
967 ext2fs_inode_bitmap_loc(fs
, i
))))
970 first_block
+= fs
->super
->s_blocks_per_group
;