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
{
265 struct ext2_inode
*inode
;
266 struct problem_context
*pctx
;
269 static void pass1b(e2fsck_t ctx
, char *block_buf
)
271 ext2_filsys fs
= ctx
->fs
;
273 struct ext2_inode inode
;
274 ext2_inode_scan scan
;
275 struct process_block_struct pb
;
276 struct problem_context pctx
;
278 clear_problem_context(&pctx
);
280 if (!(ctx
->options
& E2F_OPT_PREEN
))
281 fix_problem(ctx
, PR_1B_PASS_HEADER
, &pctx
);
282 pctx
.errcode
= ext2fs_open_inode_scan(fs
, ctx
->inode_buffer_blocks
,
285 fix_problem(ctx
, PR_1B_ISCAN_ERROR
, &pctx
);
286 ctx
->flags
|= E2F_FLAG_ABORT
;
289 ctx
->stashed_inode
= &inode
;
294 if (ino
% (fs
->super
->s_inodes_per_group
* 4) == 1) {
295 if (e2fsck_mmp_update(fs
))
298 pctx
.errcode
= ext2fs_get_next_inode(scan
, &ino
, &inode
);
299 if (pctx
.errcode
== EXT2_ET_BAD_BLOCK_IN_INODE_TABLE
)
303 fix_problem(ctx
, PR_1B_ISCAN_ERROR
, &pctx
);
304 ctx
->flags
|= E2F_FLAG_ABORT
;
309 pctx
.ino
= ctx
->stashed_ino
= ino
;
310 if ((ino
!= EXT2_BAD_INO
) &&
311 !ext2fs_test_inode_bitmap2(ctx
->inode_used_map
, ino
))
319 if (ext2fs_inode_has_valid_blocks2(fs
, &inode
) ||
320 (ino
== EXT2_BAD_INO
))
321 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
,
322 BLOCK_FLAG_READ_ONLY
, block_buf
,
323 process_pass1b_block
, &pb
);
324 /* If the feature is not set, attrs will be cleared later anyway */
325 if ((fs
->super
->s_feature_compat
& EXT2_FEATURE_COMPAT_EXT_ATTR
) &&
326 ext2fs_file_acl_block(fs
, &inode
)) {
327 blk64_t blk
= ext2fs_file_acl_block(fs
, &inode
);
328 process_pass1b_block(fs
, &blk
,
329 BLOCK_COUNT_EXTATTR
, 0, 0, &pb
);
330 ext2fs_file_acl_block_set(fs
, &inode
, blk
);
333 end_problem_latch(ctx
, PR_LATCH_DBLOCK
);
334 if (ino
>= EXT2_FIRST_INODE(fs
->super
) ||
335 ino
== EXT2_ROOT_INO
)
339 fix_problem(ctx
, PR_1B_BLOCK_ITERATE
, &pctx
);
341 ext2fs_close_inode_scan(scan
);
342 e2fsck_use_inode_shortcuts(ctx
, 0);
345 static int process_pass1b_block(ext2_filsys fs
EXT2FS_ATTR((unused
)),
347 e2_blkcnt_t blockcnt
,
348 blk64_t ref_blk
EXT2FS_ATTR((unused
)),
349 int ref_offset
EXT2FS_ATTR((unused
)),
352 struct process_block_struct
*p
;
356 if (HOLE_BLKADDR(*block_nr
))
358 p
= (struct process_block_struct
*) priv_data
;
360 lc
= EXT2FS_B2C(fs
, blockcnt
);
362 if (!ext2fs_test_block_bitmap2(ctx
->block_dup_map
, *block_nr
))
365 /* OK, this is a duplicate block */
366 if (p
->ino
!= EXT2_BAD_INO
) {
367 p
->pctx
->blk
= *block_nr
;
368 fix_problem(ctx
, PR_1B_DUP_BLOCK
, p
->pctx
);
371 ext2fs_mark_inode_bitmap2(inode_dup_map
, p
->ino
);
373 if (lc
!= p
->cur_cluster
)
374 add_dupe(ctx
, p
->ino
, EXT2FS_B2C(fs
, *block_nr
), p
->inode
);
382 * Pass 1c: Scan directories for inodes with duplicate blocks. This
383 * is used so that we can print pathnames when prompting the user for
386 struct search_dir_struct
{
388 ext2_ino_t first_inode
;
389 ext2_ino_t max_inode
;
392 static int search_dirent_proc(ext2_ino_t dir
, int entry
,
393 struct ext2_dir_entry
*dirent
,
394 int offset
EXT2FS_ATTR((unused
)),
395 int blocksize
EXT2FS_ATTR((unused
)),
396 char *buf
EXT2FS_ATTR((unused
)),
399 struct search_dir_struct
*sd
;
403 sd
= (struct search_dir_struct
*) priv_data
;
405 if (dirent
->inode
> sd
->max_inode
)
406 /* Should abort this inode, but not everything */
409 if ((dirent
->inode
< sd
->first_inode
) || (entry
< DIRENT_OTHER_FILE
) ||
410 !ext2fs_test_inode_bitmap2(inode_dup_map
, dirent
->inode
))
413 n
= dict_lookup(&ino_dict
, INT_TO_VOIDPTR(dirent
->inode
));
416 p
= (struct dup_inode
*) dnode_get(n
);
422 return(sd
->count
? 0 : DIRENT_ABORT
);
426 static void pass1c(e2fsck_t ctx
, char *block_buf
)
428 ext2_filsys fs
= ctx
->fs
;
429 struct search_dir_struct sd
;
430 struct problem_context pctx
;
432 clear_problem_context(&pctx
);
434 if (!(ctx
->options
& E2F_OPT_PREEN
))
435 fix_problem(ctx
, PR_1C_PASS_HEADER
, &pctx
);
438 * Search through all directories to translate inodes to names
439 * (by searching for the containing directory for that inode.)
441 sd
.count
= dup_inode_count
- dup_inode_founddir
;
442 sd
.first_inode
= EXT2_FIRST_INODE(fs
->super
);
443 sd
.max_inode
= fs
->super
->s_inodes_count
;
444 ext2fs_dblist_dir_iterate(fs
->dblist
, 0, block_buf
,
445 search_dirent_proc
, &sd
);
448 static void pass1d(e2fsck_t ctx
, char *block_buf
)
450 ext2_filsys fs
= ctx
->fs
;
451 struct dup_inode
*p
, *t
;
452 struct dup_cluster
*q
;
453 ext2_ino_t
*shared
, ino
;
458 struct problem_context pctx
;
460 struct cluster_el
*s
;
463 clear_problem_context(&pctx
);
465 if (!(ctx
->options
& E2F_OPT_PREEN
))
466 fix_problem(ctx
, PR_1D_PASS_HEADER
, &pctx
);
467 e2fsck_read_bitmaps(ctx
);
469 pctx
.num
= dup_inode_count
; /* dict_count(&ino_dict); */
470 fix_problem(ctx
, PR_1D_NUM_DUP_INODES
, &pctx
);
471 shared
= (ext2_ino_t
*) e2fsck_allocate_memory(ctx
,
472 sizeof(ext2_ino_t
) * dict_count(&ino_dict
),
473 "Shared inode list");
474 for (n
= dict_first(&ino_dict
); n
; n
= dict_next(&ino_dict
, n
)) {
475 p
= (struct dup_inode
*) dnode_get(n
);
478 ino
= (ext2_ino_t
)VOIDPTR_TO_INT(dnode_getkey(n
));
479 if (ino
== EXT2_BAD_INO
|| ino
== EXT2_RESIZE_INO
)
483 * Find all of the inodes which share blocks with this
484 * one. First we find all of the duplicate blocks
485 * belonging to this inode, and then search each block
486 * get the list of inodes, and merge them together.
488 for (s
= p
->cluster_list
; s
; s
= s
->next
) {
489 m
= dict_lookup(&clstr_dict
,
490 INT_TO_VOIDPTR(s
->cluster
));
492 continue; /* Should never happen... */
493 q
= (struct dup_cluster
*) dnode_get(m
);
496 if (check_if_fs_cluster(ctx
, s
->cluster
)) {
502 * Add all inodes used by this block to the
503 * shared[] --- which is a unique list, so
504 * if an inode is already in shared[], don't
507 for (r
= q
->inode_list
; r
; r
= r
->next
) {
510 for (i
= 0; i
< shared_len
; i
++)
511 if (shared
[i
] == r
->inode
)
513 if (i
== shared_len
) {
514 shared
[shared_len
++] = r
->inode
;
520 * Report the inode that we are working on
522 pctx
.inode
= &p
->inode
;
525 pctx
.blkcount
= p
->num_dupblocks
;
526 pctx
.num
= meta_data
? shared_len
+1 : shared_len
;
527 fix_problem(ctx
, PR_1D_DUP_FILE
, &pctx
);
532 fix_problem(ctx
, PR_1D_SHARE_METADATA
, &pctx
);
534 for (i
= 0; i
< shared_len
; i
++) {
535 m
= dict_lookup(&ino_dict
, INT_TO_VOIDPTR(shared
[i
]));
537 continue; /* should never happen */
538 t
= (struct dup_inode
*) dnode_get(m
);
540 * Report the inode that we are sharing with
542 pctx
.inode
= &t
->inode
;
543 pctx
.ino
= shared
[i
];
545 fix_problem(ctx
, PR_1D_DUP_FILE_LIST
, &pctx
);
548 fix_problem(ctx
, PR_1D_DUP_BLOCKS_DEALT
, &pctx
);
551 if (fix_problem(ctx
, PR_1D_CLONE_QUESTION
, &pctx
)) {
552 pctx
.errcode
= clone_file(ctx
, ino
, p
, block_buf
);
554 fix_problem(ctx
, PR_1D_CLONE_ERROR
, &pctx
);
558 if (fix_problem(ctx
, PR_1D_DELETE_QUESTION
, &pctx
))
559 delete_file(ctx
, ino
, p
, block_buf
);
561 ext2fs_unmark_valid(fs
);
563 ext2fs_free_mem(&shared
);
567 * Drop the refcount on the dup_block structure, and clear the entry
568 * in the block_dup_map if appropriate.
570 static void decrement_badcount(e2fsck_t ctx
, blk64_t block
,
571 struct dup_cluster
*p
)
574 if (p
->num_bad
<= 0 ||
575 (p
->num_bad
== 1 && !check_if_fs_block(ctx
, block
))) {
576 if (check_if_fs_cluster(ctx
, EXT2FS_B2C(ctx
->fs
, block
)))
578 ext2fs_unmark_block_bitmap2(ctx
->block_dup_map
, block
);
582 static int delete_file_block(ext2_filsys fs
,
584 e2_blkcnt_t blockcnt
,
585 blk64_t ref_block
EXT2FS_ATTR((unused
)),
586 int ref_offset
EXT2FS_ATTR((unused
)),
589 struct process_block_struct
*pb
;
590 struct dup_cluster
*p
;
595 pb
= (struct process_block_struct
*) priv_data
;
598 if (HOLE_BLKADDR(*block_nr
))
601 c
= EXT2FS_B2C(fs
, *block_nr
);
602 lc
= EXT2FS_B2C(fs
, blockcnt
);
603 if (ext2fs_test_block_bitmap2(ctx
->block_dup_map
, *block_nr
)) {
604 n
= dict_lookup(&clstr_dict
, INT_TO_VOIDPTR(c
));
606 p
= (struct dup_cluster
*) dnode_get(n
);
607 if (lc
!= pb
->cur_cluster
)
608 decrement_badcount(ctx
, *block_nr
, p
);
610 com_err("delete_file_block", 0,
611 _("internal error: can't find dup_blk for %llu\n"),
614 ext2fs_unmark_block_bitmap2(ctx
->block_found_map
, *block_nr
);
615 ext2fs_block_alloc_stats2(fs
, *block_nr
, -1);
618 pb
->cur_cluster
= lc
;
623 static void delete_file(e2fsck_t ctx
, ext2_ino_t ino
,
624 struct dup_inode
*dp
, char* block_buf
)
626 ext2_filsys fs
= ctx
->fs
;
627 struct process_block_struct pb
;
628 struct problem_context pctx
;
631 clear_problem_context(&pctx
);
632 pctx
.ino
= pb
.ino
= ino
;
635 pctx
.str
= "delete_file";
638 if (ext2fs_inode_has_valid_blocks2(fs
, &dp
->inode
))
639 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
,
640 BLOCK_FLAG_READ_ONLY
,
642 delete_file_block
, &pb
);
644 fix_problem(ctx
, PR_1B_BLOCK_ITERATE
, &pctx
);
645 if (ctx
->inode_bad_map
)
646 ext2fs_unmark_inode_bitmap2(ctx
->inode_bad_map
, ino
);
647 ext2fs_inode_alloc_stats2(fs
, ino
, -1, LINUX_S_ISDIR(dp
->inode
.i_mode
));
648 quota_data_sub(ctx
->qctx
, &dp
->inode
, ino
,
649 pb
.dup_blocks
* fs
->blocksize
);
650 quota_data_inodes(ctx
->qctx
, &dp
->inode
, ino
, -1);
652 /* Inode may have changed by block_iterate, so reread it */
653 e2fsck_read_inode(ctx
, ino
, &dp
->inode
, "delete_file");
654 e2fsck_clear_inode(ctx
, ino
, &dp
->inode
, 0, "delete_file");
655 if (ext2fs_file_acl_block(fs
, &dp
->inode
) &&
656 (fs
->super
->s_feature_compat
& EXT2_FEATURE_COMPAT_EXT_ATTR
)) {
658 pctx
.errcode
= ext2fs_adjust_ea_refcount2(fs
,
659 ext2fs_file_acl_block(fs
, &dp
->inode
),
660 block_buf
, -1, &count
);
661 if (pctx
.errcode
== EXT2_ET_BAD_EA_BLOCK_NUM
) {
666 pctx
.blk
= ext2fs_file_acl_block(fs
, &dp
->inode
);
667 fix_problem(ctx
, PR_1B_ADJ_EA_REFCOUNT
, &pctx
);
670 * If the count is zero, then arrange to have the
671 * block deleted. If the block is in the block_dup_map,
672 * also call delete_file_block since it will take care
673 * of keeping the accounting straight.
676 ext2fs_test_block_bitmap2(ctx
->block_dup_map
,
677 ext2fs_file_acl_block(fs
, &dp
->inode
))) {
678 blk64_t blk
= ext2fs_file_acl_block(fs
, &dp
->inode
);
679 delete_file_block(fs
, &blk
,
680 BLOCK_COUNT_EXTATTR
, 0, 0, &pb
);
681 ext2fs_file_acl_block_set(fs
, &dp
->inode
, blk
);
682 quota_data_sub(ctx
->qctx
, &dp
->inode
, ino
, fs
->blocksize
);
687 struct clone_struct
{
696 static int clone_file_block(ext2_filsys fs
,
698 e2_blkcnt_t blockcnt
,
699 blk64_t ref_block
EXT2FS_ATTR((unused
)),
700 int ref_offset
EXT2FS_ATTR((unused
)),
703 struct dup_cluster
*p
;
706 struct clone_struct
*cs
= (struct clone_struct
*) priv_data
;
714 if (HOLE_BLKADDR(*block_nr
))
717 c
= EXT2FS_B2C(fs
, blockcnt
);
718 if (check_if_fs_cluster(ctx
, EXT2FS_B2C(fs
, *block_nr
)))
721 if (c
== cs
->dup_cluster
&& cs
->alloc_block
) {
722 new_block
= cs
->alloc_block
;
726 if (ext2fs_test_block_bitmap2(ctx
->block_dup_map
, *block_nr
)) {
727 n
= dict_lookup(&clstr_dict
,
728 INT_TO_VOIDPTR(EXT2FS_B2C(fs
, *block_nr
)));
730 com_err("clone_file_block", 0,
731 _("internal error: can't find dup_blk for %llu\n"),
736 p
= (struct dup_cluster
*) dnode_get(n
);
738 decrement_badcount(ctx
, *block_nr
, p
);
742 retval
= ext2fs_new_block2(fs
, 0, ctx
->block_found_map
,
745 cs
->errcode
= retval
;
748 cs
->alloc_block
= new_block
;
751 new_block
&= ~EXT2FS_CLUSTER_MASK(fs
);
752 new_block
+= EXT2FS_CLUSTER_MASK(fs
) & blockcnt
;
753 if (cs
->dir
&& (blockcnt
>= 0)) {
754 retval
= ext2fs_set_dir_block2(fs
->dblist
,
755 cs
->dir
, new_block
, blockcnt
);
757 cs
->errcode
= retval
;
762 printf("Cloning block #%lld from %llu to %llu\n",
763 blockcnt
, *block_nr
, new_block
);
765 retval
= io_channel_read_blk64(fs
->io
, *block_nr
, 1, cs
->buf
);
767 cs
->errcode
= retval
;
770 retval
= io_channel_write_blk64(fs
->io
, new_block
, 1, cs
->buf
);
772 cs
->errcode
= retval
;
775 *block_nr
= new_block
;
776 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, new_block
);
777 ext2fs_mark_block_bitmap2(fs
->block_map
, new_block
);
778 return BLOCK_CHANGED
;
783 static errcode_t
clone_file(e2fsck_t ctx
, ext2_ino_t ino
,
784 struct dup_inode
*dp
, char* block_buf
)
786 ext2_filsys fs
= ctx
->fs
;
788 struct clone_struct cs
;
789 struct problem_context pctx
;
790 blk64_t blk
, new_blk
;
792 struct inode_el
*ino_el
;
793 struct dup_cluster
*dc
;
794 struct dup_inode
*di
;
796 clear_problem_context(&pctx
);
802 retval
= ext2fs_get_mem(fs
->blocksize
, &cs
.buf
);
806 if (ext2fs_test_inode_bitmap2(ctx
->inode_dir_map
, ino
))
810 pctx
.str
= "clone_file";
811 if (ext2fs_inode_has_valid_blocks2(fs
, &dp
->inode
))
812 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
, 0, block_buf
,
813 clone_file_block
, &cs
);
814 ext2fs_mark_bb_dirty(fs
);
816 fix_problem(ctx
, PR_1B_BLOCK_ITERATE
, &pctx
);
817 retval
= pctx
.errcode
;
821 com_err("clone_file", cs
.errcode
, "%s",
822 _("returned from clone_file_block"));
826 /* The inode may have changed on disk, so we have to re-read it */
827 e2fsck_read_inode(ctx
, ino
, &dp
->inode
, "clone file EA");
828 blk
= ext2fs_file_acl_block(fs
, &dp
->inode
);
830 if (blk
&& (clone_file_block(fs
, &new_blk
,
831 BLOCK_COUNT_EXTATTR
, 0, 0, &cs
) ==
833 ext2fs_file_acl_block_set(fs
, &dp
->inode
, new_blk
);
834 e2fsck_write_inode(ctx
, ino
, &dp
->inode
, "clone file EA");
836 * If we cloned the EA block, find all other inodes
837 * which refered to that EA block, and modify
838 * them to point to the new EA block.
840 n
= dict_lookup(&clstr_dict
,
841 INT_TO_VOIDPTR(EXT2FS_B2C(fs
, blk
)));
843 com_err("clone_file", 0,
844 _("internal error: couldn't lookup EA "
845 "block record for %llu"), blk
);
846 retval
= 0; /* OK to stumble on... */
849 dc
= (struct dup_cluster
*) dnode_get(n
);
850 for (ino_el
= dc
->inode_list
; ino_el
; ino_el
= ino_el
->next
) {
851 if (ino_el
->inode
== ino
)
853 n
= dict_lookup(&ino_dict
, INT_TO_VOIDPTR(ino_el
->inode
));
855 com_err("clone_file", 0,
856 _("internal error: couldn't lookup EA "
857 "inode record for %u"),
859 retval
= 0; /* OK to stumble on... */
862 di
= (struct dup_inode
*) dnode_get(n
);
863 if (ext2fs_file_acl_block(fs
, &di
->inode
) == blk
) {
864 ext2fs_file_acl_block_set(fs
, &di
->inode
,
865 ext2fs_file_acl_block(fs
, &dp
->inode
));
866 e2fsck_write_inode(ctx
, ino_el
->inode
,
867 &di
->inode
, "clone file EA");
868 decrement_badcount(ctx
, blk
, dc
);
874 ext2fs_free_mem(&cs
.buf
);
879 * This routine returns 1 if a block overlaps with one of the superblocks,
880 * group descriptors, inode bitmaps, or block bitmaps.
882 static int check_if_fs_block(e2fsck_t ctx
, blk64_t test_block
)
884 ext2_filsys fs
= ctx
->fs
;
888 first_block
= fs
->super
->s_first_data_block
;
889 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
891 /* Check superblocks/block group descriptors */
892 if (ext2fs_bg_has_super(fs
, i
)) {
893 if (test_block
>= first_block
&&
894 (test_block
<= first_block
+ fs
->desc_blocks
))
898 /* Check the inode table */
899 if ((ext2fs_inode_table_loc(fs
, i
)) &&
900 (test_block
>= ext2fs_inode_table_loc(fs
, i
)) &&
901 (test_block
< (ext2fs_inode_table_loc(fs
, i
) +
902 fs
->inode_blocks_per_group
)))
905 /* Check the bitmap blocks */
906 if ((test_block
== ext2fs_block_bitmap_loc(fs
, i
)) ||
907 (test_block
== ext2fs_inode_bitmap_loc(fs
, i
)))
910 first_block
+= fs
->super
->s_blocks_per_group
;
916 * This routine returns 1 if a cluster overlaps with one of the superblocks,
917 * group descriptors, inode bitmaps, or block bitmaps.
919 static int check_if_fs_cluster(e2fsck_t ctx
, blk64_t cluster
)
921 ext2_filsys fs
= ctx
->fs
;
925 first_block
= fs
->super
->s_first_data_block
;
926 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
928 /* Check superblocks/block group descriptors */
929 if (ext2fs_bg_has_super(fs
, i
)) {
930 if (cluster
>= EXT2FS_B2C(fs
, first_block
) &&
931 (cluster
<= EXT2FS_B2C(fs
, first_block
+
936 /* Check the inode table */
937 if ((ext2fs_inode_table_loc(fs
, i
)) &&
938 (cluster
>= EXT2FS_B2C(fs
,
939 ext2fs_inode_table_loc(fs
, i
))) &&
940 (cluster
<= EXT2FS_B2C(fs
,
941 ext2fs_inode_table_loc(fs
, i
) +
942 fs
->inode_blocks_per_group
- 1)))
945 /* Check the bitmap blocks */
946 if ((cluster
== EXT2FS_B2C(fs
,
947 ext2fs_block_bitmap_loc(fs
, i
))) ||
948 (cluster
== EXT2FS_B2C(fs
,
949 ext2fs_inode_bitmap_loc(fs
, i
))))
952 first_block
+= fs
->super
->s_blocks_per_group
;