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>
52 #include "support/dict.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_large 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_large *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);
248 if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
249 (ctx->options & E2F_OPT_UNSHARE_BLOCKS)) {
251 * If we successfully managed to unshare all blocks, unset the
252 * shared block feature.
255 int result = ext2fs_find_first_set_block_bitmap2(
257 ctx->fs->super->s_first_data_block,
258 ext2fs_blocks_count(ctx->fs->super) - 1,
260 if (result == ENOENT && !(ctx->options & E2F_OPT_NO)) {
261 ext2fs_clear_feature_shared_blocks(ctx->fs->super);
262 ext2fs_mark_super_dirty(ctx->fs);
267 * Time to free all of the accumulated data structures that we
268 * don't need anymore.
270 dict_free_nodes(&ino_dict);
271 dict_free_nodes(&clstr_dict);
272 ext2fs_free_inode_bitmap(inode_dup_map);
276 * Scan the inodes looking for inodes that contain duplicate blocks.
278 struct process_block_struct {
282 blk64_t cur_cluster, phys_cluster;
284 struct ext2_inode_large *inode;
285 struct problem_context *pctx;
288 static void pass1b(e2fsck_t ctx, char *block_buf)
290 ext2_filsys fs = ctx->fs;
292 struct ext2_inode_large inode;
293 ext2_inode_scan scan;
294 struct process_block_struct pb;
295 struct problem_context pctx;
298 clear_problem_context(&pctx);
300 if (!(ctx->options & E2F_OPT_PREEN))
301 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
302 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
305 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
306 ctx->flags |= E2F_FLAG_ABORT;
309 ctx->stashed_inode = EXT2_INODE(&inode);
314 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
315 if (e2fsck_mmp_update(fs))
318 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
319 EXT2_INODE(&inode), sizeof(inode));
320 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
324 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
325 ctx->flags |= E2F_FLAG_ABORT;
330 pctx.ino = ctx->stashed_ino = ino;
331 if ((ino != EXT2_BAD_INO) &&
332 !ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino))
339 pb.phys_cluster = ~0;
341 pb.pctx->blk = pb.pctx->blk2 = 0;
343 if (ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(&inode)) ||
344 (ino == EXT2_BAD_INO))
345 pctx.errcode = ext2fs_block_iterate3(fs, ino,
346 BLOCK_FLAG_READ_ONLY, block_buf,
347 process_pass1b_block, &pb);
348 /* If the feature is not set, attrs will be cleared later anyway */
349 if (ext2fs_has_feature_xattr(fs->super) &&
350 ext2fs_file_acl_block(fs, EXT2_INODE(&inode))) {
351 blk64_t blk = ext2fs_file_acl_block(fs, EXT2_INODE(&inode));
352 process_pass1b_block(fs, &blk,
353 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
354 ext2fs_file_acl_block_set(fs, EXT2_INODE(&inode), blk);
357 if (ino != EXT2_BAD_INO) {
358 op = pctx.blk == pctx.blk2 ?
359 PR_1B_DUP_BLOCK : PR_1B_DUP_RANGE;
360 fix_problem(ctx, op, pb.pctx);
362 end_problem_latch(ctx, PR_LATCH_DBLOCK);
363 if (ino >= EXT2_FIRST_INODE(fs->super) ||
364 ino == EXT2_ROOT_INO)
368 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
370 ext2fs_close_inode_scan(scan);
371 e2fsck_use_inode_shortcuts(ctx, 0);
374 static int process_pass1b_block(ext2_filsys fs EXT2FS_ATTR((unused)),
376 e2_blkcnt_t blockcnt,
377 blk64_t ref_blk EXT2FS_ATTR((unused)),
378 int ref_offset EXT2FS_ATTR((unused)),
381 struct process_block_struct *p;
388 p = (struct process_block_struct *) priv_data;
390 lc = EXT2FS_B2C(fs, blockcnt);
391 pc = EXT2FS_B2C(fs, *block_nr);
393 if (!ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr))
396 /* OK, this is a duplicate block */
397 if (p->ino != EXT2_BAD_INO) {
398 if (p->last_blk + 1 != *block_nr) {
400 op = p->pctx->blk == p->pctx->blk2 ?
403 fix_problem(ctx, op, p->pctx);
405 p->pctx->blk = *block_nr;
407 p->pctx->blk2 = *block_nr;
408 p->last_blk = *block_nr;
411 ext2fs_mark_inode_bitmap2(inode_dup_map, p->ino);
414 * Qualifications for submitting a block for duplicate processing:
415 * It's an extent/indirect block (and has a negative logical offset);
416 * we've crossed a logical cluster boundary; or the physical cluster
417 * suddenly changed, which indicates that blocks in a logical cluster
418 * are mapped to multiple physical clusters.
420 if (blockcnt < 0 || lc != p->cur_cluster || pc != p->phys_cluster)
421 add_dupe(ctx, p->ino, EXT2FS_B2C(fs, *block_nr), p->inode);
425 p->phys_cluster = pc;
430 * Pass 1c: Scan directories for inodes with duplicate blocks. This
431 * is used so that we can print pathnames when prompting the user for
434 struct search_dir_struct {
436 ext2_ino_t first_inode;
437 ext2_ino_t max_inode;
440 static int search_dirent_proc(ext2_ino_t dir, int entry,
441 struct ext2_dir_entry *dirent,
442 int offset EXT2FS_ATTR((unused)),
443 int blocksize EXT2FS_ATTR((unused)),
444 char *buf EXT2FS_ATTR((unused)),
447 struct search_dir_struct *sd;
451 sd = (struct search_dir_struct *) priv_data;
453 if (dirent->inode > sd->max_inode)
454 /* Should abort this inode, but not everything */
457 if ((dirent->inode < sd->first_inode) || (entry < DIRENT_OTHER_FILE) ||
458 !ext2fs_test_inode_bitmap2(inode_dup_map, dirent->inode))
461 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(dirent->inode));
464 p = (struct dup_inode *) dnode_get(n);
470 return(sd->count ? 0 : DIRENT_ABORT);
474 static void pass1c(e2fsck_t ctx, char *block_buf)
476 ext2_filsys fs = ctx->fs;
477 struct search_dir_struct sd;
478 struct problem_context pctx;
480 clear_problem_context(&pctx);
482 if (!(ctx->options & E2F_OPT_PREEN))
483 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
486 * Search through all directories to translate inodes to names
487 * (by searching for the containing directory for that inode.)
489 sd.count = dup_inode_count - dup_inode_founddir;
490 sd.first_inode = EXT2_FIRST_INODE(fs->super);
491 sd.max_inode = fs->super->s_inodes_count;
492 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
493 search_dirent_proc, &sd);
496 static void pass1d(e2fsck_t ctx, char *block_buf)
498 ext2_filsys fs = ctx->fs;
499 struct dup_inode *p, *t;
500 struct dup_cluster *q;
501 ext2_ino_t *shared, ino;
506 struct problem_context pctx;
508 struct cluster_el *s;
511 clear_problem_context(&pctx);
513 if (!(ctx->options & E2F_OPT_PREEN))
514 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
515 e2fsck_read_bitmaps(ctx);
517 pctx.num = dup_inode_count; /* dict_count(&ino_dict); */
518 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
519 shared = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
520 sizeof(ext2_ino_t) * dict_count(&ino_dict),
521 "Shared inode list");
522 for (n = dict_first(&ino_dict); n; n = dict_next(&ino_dict, n)) {
523 p = (struct dup_inode *) dnode_get(n);
526 ino = (ext2_ino_t)VOIDPTR_TO_INT(dnode_getkey(n));
527 if (ino == EXT2_BAD_INO || ino == EXT2_RESIZE_INO)
531 * Find all of the inodes which share blocks with this
532 * one. First we find all of the duplicate blocks
533 * belonging to this inode, and then search each block
534 * get the list of inodes, and merge them together.
536 for (s = p->cluster_list; s; s = s->next) {
537 m = dict_lookup(&clstr_dict,
538 INT_TO_VOIDPTR(s->cluster));
540 continue; /* Should never happen... */
541 q = (struct dup_cluster *) dnode_get(m);
544 if (check_if_fs_cluster(ctx, s->cluster)) {
550 * Add all inodes used by this block to the
551 * shared[] --- which is a unique list, so
552 * if an inode is already in shared[], don't
555 for (r = q->inode_list; r; r = r->next) {
558 for (i = 0; i < shared_len; i++)
559 if (shared[i] == r->inode)
561 if (i == shared_len) {
562 shared[shared_len++] = r->inode;
568 * Report the inode that we are working on
570 pctx.inode = EXT2_INODE(&p->inode);
573 pctx.blkcount = p->num_dupblocks;
574 pctx.num = meta_data ? shared_len+1 : shared_len;
575 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
580 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
582 for (i = 0; i < shared_len; i++) {
583 m = dict_lookup(&ino_dict, INT_TO_VOIDPTR(shared[i]));
585 continue; /* should never happen */
586 t = (struct dup_inode *) dnode_get(m);
588 * Report the inode that we are sharing with
590 pctx.inode = EXT2_INODE(&t->inode);
591 pctx.ino = shared[i];
593 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
596 * Even if the file shares blocks with itself, we still need to
599 if (file_ok && (meta_data ? shared_len+1 : shared_len) != 0) {
600 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
603 if ((ctx->options & E2F_OPT_UNSHARE_BLOCKS) ||
604 fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
605 pctx.errcode = clone_file(ctx, ino, p, block_buf);
607 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
612 * Note: When unsharing blocks, we don't prompt to delete
613 * files. If the clone operation fails than the unshare
614 * operation should fail too.
616 if (!(ctx->options & E2F_OPT_UNSHARE_BLOCKS) &&
617 fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
618 delete_file(ctx, ino, p, block_buf);
620 ext2fs_unmark_valid(fs);
622 ext2fs_free_mem(&shared);
626 * Drop the refcount on the dup_block structure, and clear the entry
627 * in the block_dup_map if appropriate.
629 static void decrement_badcount(e2fsck_t ctx, blk64_t block,
630 struct dup_cluster *p)
633 if (p->num_bad <= 0 ||
634 (p->num_bad == 1 && !check_if_fs_block(ctx, block))) {
635 if (check_if_fs_cluster(ctx, EXT2FS_B2C(ctx->fs, block)))
637 ext2fs_unmark_block_bitmap2(ctx->block_dup_map, block);
641 static int delete_file_block(ext2_filsys fs,
643 e2_blkcnt_t blockcnt,
644 blk64_t ref_block EXT2FS_ATTR((unused)),
645 int ref_offset EXT2FS_ATTR((unused)),
648 struct process_block_struct *pb;
649 struct dup_cluster *p;
654 pb = (struct process_block_struct *) priv_data;
660 c = EXT2FS_B2C(fs, *block_nr);
661 lc = EXT2FS_B2C(fs, blockcnt);
662 if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
663 n = dict_lookup(&clstr_dict, INT_TO_VOIDPTR(c));
665 if (lc != pb->cur_cluster) {
666 p = (struct dup_cluster *) dnode_get(n);
667 decrement_badcount(ctx, *block_nr, p);
671 com_err("delete_file_block", 0,
672 _("internal error: can't find dup_blk for %llu\n"),
675 if ((*block_nr % EXT2FS_CLUSTER_RATIO(ctx->fs)) == 0)
676 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
679 pb->cur_cluster = lc;
684 static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
685 struct dup_inode *dp, char* block_buf)
687 ext2_filsys fs = ctx->fs;
688 struct process_block_struct pb;
689 struct problem_context pctx;
692 clear_problem_context(&pctx);
693 pctx.ino = pb.ino = ino;
696 pctx.str = "delete_file";
699 if (ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(&dp->inode)))
700 pctx.errcode = ext2fs_block_iterate3(fs, ino,
701 BLOCK_FLAG_READ_ONLY,
703 delete_file_block, &pb);
705 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
706 if (ctx->inode_bad_map)
707 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
708 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(dp->inode.i_mode));
709 quota_data_sub(ctx->qctx, &dp->inode, ino,
710 pb.dup_blocks * fs->blocksize);
711 quota_data_inodes(ctx->qctx, &dp->inode, ino, -1);
713 /* Inode may have changed by block_iterate, so reread it */
714 e2fsck_read_inode_full(ctx, ino, EXT2_INODE(&dp->inode),
715 sizeof(dp->inode), "delete_file");
716 e2fsck_clear_inode(ctx, ino, EXT2_INODE(&dp->inode), 0, "delete_file");
717 if (ext2fs_file_acl_block(fs, EXT2_INODE(&dp->inode)) &&
718 ext2fs_has_feature_xattr(fs->super)) {
719 blk64_t file_acl_block = ext2fs_file_acl_block(fs,
720 EXT2_INODE(&dp->inode));
723 pctx.errcode = ext2fs_adjust_ea_refcount3(fs, file_acl_block,
724 block_buf, -1, &count, ino);
725 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
730 pctx.blk = file_acl_block;
731 fix_problem(ctx, PR_1B_ADJ_EA_REFCOUNT, &pctx);
734 * If the count is zero, then arrange to have the
735 * block deleted. If the block is in the block_dup_map,
736 * also call delete_file_block since it will take care
737 * of keeping the accounting straight.
740 ext2fs_test_block_bitmap2(ctx->block_dup_map,
742 delete_file_block(fs, &file_acl_block,
743 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
744 ext2fs_file_acl_block_set(fs, EXT2_INODE(&dp->inode),
746 quota_data_sub(ctx->qctx, &dp->inode, ino,
752 struct clone_struct {
759 struct ext2_inode_large *inode;
761 struct dup_cluster *save_dup_cluster;
762 blk64_t save_blocknr;
766 * Decrement the bad count *after* we've shown that (a) we can allocate a
767 * replacement block and (b) remap the file blocks. Unfortunately, there's no
768 * way to find out if the remap succeeded until either the next
769 * clone_file_block() call (an error when remapping the block after returning
770 * BLOCK_CHANGED will halt the iteration) or after block_iterate() returns.
771 * Otherwise, it's possible that we decrease the badcount once in preparation
772 * to remap, then the remap fails (either we can't find a replacement block or
773 * we have to split the extent tree and can't find a new extent block), so we
774 * delete the file, which decreases the badcount again.
776 static void deferred_dec_badcount(struct clone_struct *cs)
778 if (!cs->save_dup_cluster)
780 decrement_badcount(cs->ctx, cs->save_blocknr, cs->save_dup_cluster);
781 cs->save_dup_cluster = NULL;
784 static int clone_file_block(ext2_filsys fs,
786 e2_blkcnt_t blockcnt,
787 blk64_t ref_block EXT2FS_ATTR((unused)),
788 int ref_offset EXT2FS_ATTR((unused)),
791 struct dup_cluster *p = NULL;
794 struct clone_struct *cs = (struct clone_struct *) priv_data;
799 int should_write = 1;
802 deferred_dec_badcount(cs);
807 if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
808 (ctx->options & E2F_OPT_UNSHARE_BLOCKS) &&
809 (ctx->options & E2F_OPT_NO))
812 c = EXT2FS_B2C(fs, blockcnt);
813 if (check_if_fs_cluster(ctx, EXT2FS_B2C(fs, *block_nr)))
816 if (c == cs->dup_cluster && cs->alloc_block) {
817 new_block = cs->alloc_block;
821 if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
822 n = dict_lookup(&clstr_dict,
823 INT_TO_VOIDPTR(EXT2FS_B2C(fs, *block_nr)));
825 com_err("clone_file_block", 0,
826 _("internal error: can't find dup_blk for %llu\n"),
831 p = (struct dup_cluster *) dnode_get(n);
835 * Let's try an implied cluster allocation. If we get the same
836 * cluster back, then we need to find a new block; otherwise,
837 * we're merely fixing the problem of one logical cluster being
838 * mapped to multiple physical clusters.
841 retval = ext2fs_map_cluster_block(fs, cs->ino,
842 EXT2_INODE(cs->inode),
843 blockcnt, &new_block);
844 if (retval == 0 && new_block != 0 &&
845 EXT2FS_B2C(ctx->fs, new_block) !=
846 EXT2FS_B2C(ctx->fs, *block_nr))
847 goto cluster_alloc_ok;
848 retval = ext2fs_new_block2(fs, 0, ctx->block_found_map,
851 cs->errcode = retval;
854 if (ext2fs_has_feature_shared_blocks(fs->super)) {
856 * Update the block stats so we don't get a prompt to fix block
857 * counts in the final pass.
859 ext2fs_block_alloc_stats2(fs, new_block, +1);
862 cs->alloc_block = new_block;
865 new_block &= ~EXT2FS_CLUSTER_MASK(fs);
866 new_block += EXT2FS_CLUSTER_MASK(fs) & blockcnt;
867 if (cs->dir && (blockcnt >= 0)) {
868 retval = ext2fs_set_dir_block2(fs->dblist,
869 cs->dir, new_block, blockcnt);
871 cs->errcode = retval;
876 printf("Cloning block #%lld from %llu to %llu\n",
877 blockcnt, *block_nr, new_block);
879 retval = io_channel_read_blk64(fs->io, *block_nr, 1, cs->buf);
881 cs->errcode = retval;
885 retval = io_channel_write_blk64(fs->io, new_block, 1, cs->buf);
887 cs->errcode = retval;
891 cs->save_dup_cluster = (is_meta ? NULL : p);
892 cs->save_blocknr = *block_nr;
893 *block_nr = new_block;
894 ext2fs_mark_block_bitmap2(ctx->block_found_map, new_block);
895 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
898 /* Don't try to change extent information; we want e2fsck to
903 return BLOCK_CHANGED;
908 static errcode_t clone_file(e2fsck_t ctx, ext2_ino_t ino,
909 struct dup_inode *dp, char* block_buf)
911 ext2_filsys fs = ctx->fs;
913 struct clone_struct cs;
914 struct problem_context pctx;
915 blk64_t blk, new_blk;
917 struct inode_el *ino_el;
918 struct dup_cluster *dc;
919 struct dup_inode *di;
921 clear_problem_context(&pctx);
928 cs.inode = &dp->inode;
929 cs.save_dup_cluster = NULL;
931 retval = ext2fs_get_mem(fs->blocksize, &cs.buf);
935 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, ino))
939 pctx.str = "clone_file";
940 if (ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(&dp->inode)))
941 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
942 clone_file_block, &cs);
943 deferred_dec_badcount(&cs);
944 ext2fs_mark_bb_dirty(fs);
946 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
947 retval = pctx.errcode;
951 com_err("clone_file", cs.errcode, "%s",
952 _("returned from clone_file_block"));
956 /* The inode may have changed on disk, so we have to re-read it */
957 e2fsck_read_inode_full(ctx, ino, EXT2_INODE(&dp->inode),
958 sizeof(dp->inode), "clone file EA");
959 blk = ext2fs_file_acl_block(fs, EXT2_INODE(&dp->inode));
961 if (blk && (clone_file_block(fs, &new_blk,
962 BLOCK_COUNT_EXTATTR, 0, 0, &cs) ==
964 ext2fs_file_acl_block_set(fs, EXT2_INODE(&dp->inode), new_blk);
965 e2fsck_write_inode_full(ctx, ino, EXT2_INODE(&dp->inode),
966 sizeof(dp->inode), "clone file EA");
968 * If we cloned the EA block, find all other inodes
969 * which referred to that EA block, and modify
970 * them to point to the new EA block.
972 n = dict_lookup(&clstr_dict,
973 INT_TO_VOIDPTR(EXT2FS_B2C(fs, blk)));
975 com_err("clone_file", 0,
976 _("internal error: couldn't lookup EA "
977 "block record for %llu"), blk);
978 retval = 0; /* OK to stumble on... */
981 dc = (struct dup_cluster *) dnode_get(n);
982 for (ino_el = dc->inode_list; ino_el; ino_el = ino_el->next) {
983 if (ino_el->inode == ino)
985 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino_el->inode));
987 com_err("clone_file", 0,
988 _("internal error: couldn't lookup EA "
989 "inode record for %u"),
991 retval = 0; /* OK to stumble on... */
994 di = (struct dup_inode *) dnode_get(n);
995 if (ext2fs_file_acl_block(fs,
996 EXT2_INODE(&di->inode)) == blk) {
997 ext2fs_file_acl_block_set(fs,
998 EXT2_INODE(&di->inode),
999 ext2fs_file_acl_block(fs, EXT2_INODE(&dp->inode)));
1000 e2fsck_write_inode_full(ctx, ino_el->inode,
1001 EXT2_INODE(&di->inode),
1002 sizeof(di->inode), "clone file EA");
1003 decrement_badcount(ctx, blk, dc);
1009 ext2fs_free_mem(&cs.buf);
1014 * This routine returns 1 if a block overlaps with one of the superblocks,
1015 * group descriptors, inode bitmaps, or block bitmaps.
1017 static int check_if_fs_block(e2fsck_t ctx, blk64_t test_block)
1019 ext2_filsys fs = ctx->fs;
1020 blk64_t first_block;
1023 first_block = fs->super->s_first_data_block;
1024 for (i = 0; i < fs->group_desc_count; i++) {
1026 /* Check superblocks/block group descriptors */
1027 if (ext2fs_bg_has_super(fs, i)) {
1028 if (test_block >= first_block &&
1029 (test_block <= first_block + fs->desc_blocks))
1033 /* Check the inode table */
1034 if ((ext2fs_inode_table_loc(fs, i)) &&
1035 (test_block >= ext2fs_inode_table_loc(fs, i)) &&
1036 (test_block < (ext2fs_inode_table_loc(fs, i) +
1037 fs->inode_blocks_per_group)))
1040 /* Check the bitmap blocks */
1041 if ((test_block == ext2fs_block_bitmap_loc(fs, i)) ||
1042 (test_block == ext2fs_inode_bitmap_loc(fs, i)))
1045 first_block += fs->super->s_blocks_per_group;
1051 * This routine returns 1 if a cluster overlaps with one of the superblocks,
1052 * group descriptors, inode bitmaps, or block bitmaps.
1054 static int check_if_fs_cluster(e2fsck_t ctx, blk64_t cluster)
1056 ext2_filsys fs = ctx->fs;
1057 blk64_t first_block;
1060 first_block = fs->super->s_first_data_block;
1061 for (i = 0; i < fs->group_desc_count; i++) {
1063 /* Check superblocks/block group descriptors */
1064 if (ext2fs_bg_has_super(fs, i)) {
1065 if (cluster >= EXT2FS_B2C(fs, first_block) &&
1066 (cluster <= EXT2FS_B2C(fs, first_block +
1071 /* Check the inode table */
1072 if ((ext2fs_inode_table_loc(fs, i)) &&
1073 (cluster >= EXT2FS_B2C(fs,
1074 ext2fs_inode_table_loc(fs, i))) &&
1075 (cluster <= EXT2FS_B2C(fs,
1076 ext2fs_inode_table_loc(fs, i) +
1077 fs->inode_blocks_per_group - 1)))
1080 /* Check the bitmap blocks */
1081 if ((cluster == EXT2FS_B2C(fs,
1082 ext2fs_block_bitmap_loc(fs, i))) ||
1083 (cluster == EXT2FS_B2C(fs,
1084 ext2fs_inode_bitmap_loc(fs, i))))
1087 first_block += fs->super->s_blocks_per_group;