Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * pass1b.c --- Pass #1b of e2fsck |
| 3 | * |
| 4 | * This file contains pass1B, pass1C, and pass1D of e2fsck. They are |
| 5 | * only invoked if pass 1 discovered blocks which are in use by more |
| 6 | * than one inode. |
| 7 | * |
| 8 | * Pass1B scans the data blocks of all the inodes again, generating a |
| 9 | * complete list of duplicate blocks and which inodes have claimed |
| 10 | * them. |
| 11 | * |
| 12 | * Pass1C does a tree-traversal of the filesystem, to determine the |
| 13 | * parent directories of these inodes. This step is necessary so that |
| 14 | * e2fsck can print out the pathnames of affected inodes. |
| 15 | * |
| 16 | * Pass1D is a reconciliation pass. For each inode with duplicate |
| 17 | * blocks, the user is prompted if s/he would like to clone the file |
| 18 | * (so that the file gets a fresh copy of the duplicated blocks) or |
| 19 | * simply to delete the file. |
| 20 | * |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 21 | * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o. |
| 22 | * |
| 23 | * %Begin-Header% |
| 24 | * This file may be redistributed under the terms of the GNU Public |
| 25 | * License. |
| 26 | * %End-Header% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <time.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 31 | #ifdef HAVE_ERRNO_H |
| 32 | #include <errno.h> |
| 33 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 34 | |
| 35 | #include <et/com_err.h> |
| 36 | #include "e2fsck.h" |
| 37 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 38 | #include "problem.h" |
| 39 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 40 | /* |
| 41 | * This is structure is allocated for each time that a block is |
| 42 | * claimed by more than one file. So if a particular block is claimed |
| 43 | * by 3 files, then three copies of this structure will be allocated, |
| 44 | * one for each conflict. |
| 45 | * |
| 46 | * The linked list structure is as follows: |
| 47 | * |
| 48 | * dup_blk --> block #34 --> block #35 --> block #47 |
| 49 | * inode #12 inode #14 inode #17 |
| 50 | * num_bad = 3 num_bad = 2 num_bad = 2 |
| 51 | * | | | |
| 52 | * V V V |
| 53 | * block #34 block #35 block #47 |
| 54 | * inode #14 inode #15 inode #23 |
| 55 | * | |
| 56 | * V |
| 57 | * block #34 |
| 58 | * inode #15 |
| 59 | * |
| 60 | * The num_bad field indicates how many inodes are sharing a |
| 61 | * particular block, and is only stored in the first element of the |
| 62 | * linked list for a particular block. As the block conflicts are |
| 63 | * resolved, num_bad is decremented; when it reaches 1, then we no |
| 64 | * longer need to worry about that block. |
| 65 | */ |
| 66 | struct dup_block { |
| 67 | blk_t block; /* Block number */ |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 68 | ext2_ino_t ino; /* Inode number */ |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 69 | int num_bad; |
| 70 | /* Pointer to next dup record with different block */ |
| 71 | struct dup_block *next_block; |
| 72 | /* Pointer to next dup record with different inode */ |
| 73 | struct dup_block *next_inode; |
| 74 | }; |
| 75 | |
| 76 | /* |
| 77 | * This structure stores information about a particular inode which |
| 78 | * is sharing blocks with other inodes. This information is collected |
| 79 | * to display to the user, so that the user knows what files he or she |
| 80 | * is dealing with, when trying to decide how to resolve the conflict |
| 81 | * of multiply-claimed blocks. |
| 82 | */ |
| 83 | struct dup_inode { |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 84 | ext2_ino_t ino, dir; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 85 | int num_dupblocks; |
| 86 | struct ext2_inode inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 87 | struct dup_inode *next; |
| 88 | }; |
| 89 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 90 | static int process_pass1b_block(ext2_filsys fs, blk_t *blocknr, |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 91 | e2_blkcnt_t blockcnt, blk_t ref_blk, |
| 92 | int ref_offset, void *priv_data); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 93 | static void delete_file(e2fsck_t ctx, struct dup_inode *dp, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 94 | char *block_buf); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 95 | static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf); |
Theodore Ts'o | 80c5d7e | 2000-02-08 23:19:32 +0000 | [diff] [blame] | 96 | static int check_if_fs_block(e2fsck_t ctx, blk_t test_blk); |
| 97 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 98 | static void pass1b(e2fsck_t ctx, char *block_buf); |
| 99 | static void pass1c(e2fsck_t ctx, char *block_buf); |
| 100 | static void pass1d(e2fsck_t ctx, char *block_buf); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 101 | |
| 102 | static struct dup_block *dup_blk = 0; |
| 103 | static struct dup_inode *dup_ino = 0; |
| 104 | static int dup_inode_count = 0; |
| 105 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 106 | static ext2fs_inode_bitmap inode_dup_map; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * Main procedure for handling duplicate blocks |
| 110 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 111 | void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 112 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 113 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 114 | struct dup_block *p, *q, *next_p, *next_q; |
| 115 | struct dup_inode *r, *next_r; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 116 | struct problem_context pctx; |
| 117 | |
| 118 | clear_problem_context(&pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 119 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 120 | pctx.errcode = ext2fs_allocate_inode_bitmap(fs, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 121 | _("multiply claimed inode map"), &inode_dup_map); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 122 | if (pctx.errcode) { |
| 123 | fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 124 | ctx->flags |= E2F_FLAG_ABORT; |
| 125 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 128 | pass1b(ctx, block_buf); |
| 129 | pass1c(ctx, block_buf); |
| 130 | pass1d(ctx, block_buf); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * Time to free all of the accumulated data structures that we |
| 134 | * don't need anymore. |
| 135 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 136 | ext2fs_free_inode_bitmap(inode_dup_map); inode_dup_map = 0; |
| 137 | ext2fs_free_block_bitmap(ctx->block_dup_map); ctx->block_dup_map = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 138 | for (p = dup_blk; p; p = next_p) { |
| 139 | next_p = p->next_block; |
| 140 | for (q = p; q; q = next_q) { |
| 141 | next_q = q->next_inode; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 142 | ext2fs_free_mem((void **) &q); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | for (r = dup_ino; r; r = next_r) { |
| 146 | next_r = r->next; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 147 | ext2fs_free_mem((void **) &r); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Scan the inodes looking for inodes that contain duplicate blocks. |
| 153 | */ |
| 154 | struct process_block_struct { |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 155 | ext2_ino_t ino; |
| 156 | int dup_blocks; |
| 157 | e2fsck_t ctx; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 158 | struct problem_context *pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 161 | static void pass1b(e2fsck_t ctx, char *block_buf) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 162 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 163 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 164 | ext2_ino_t ino; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 165 | struct ext2_inode inode; |
| 166 | ext2_inode_scan scan; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 167 | struct process_block_struct pb; |
| 168 | struct dup_inode *dp; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 169 | struct problem_context pctx; |
| 170 | |
| 171 | clear_problem_context(&pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 172 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 173 | fix_problem(ctx, PR_1B_PASS_HEADER, &pctx); |
| 174 | pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks, |
| 175 | &scan); |
| 176 | if (pctx.errcode) { |
| 177 | fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 178 | ctx->flags |= E2F_FLAG_ABORT; |
| 179 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 180 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 181 | pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode); |
| 182 | if (pctx.errcode) { |
| 183 | fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 184 | ctx->flags |= E2F_FLAG_ABORT; |
| 185 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 186 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 187 | ctx->stashed_inode = &inode; |
| 188 | pb.ctx = ctx; |
| 189 | pb.pctx = &pctx; |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 190 | pctx.str = "pass1b"; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 191 | while (ino) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 192 | pctx.ino = ctx->stashed_ino = ino; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 193 | if ((ino != EXT2_BAD_INO) && |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 194 | (!ext2fs_test_inode_bitmap(ctx->inode_used_map, ino) || |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 195 | !ext2fs_inode_has_valid_blocks(&inode))) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 196 | goto next; |
| 197 | |
| 198 | pb.ino = ino; |
| 199 | pb.dup_blocks = 0; |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 200 | pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 201 | process_pass1b_block, &pb); |
| 202 | if (pb.dup_blocks) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 203 | end_problem_latch(ctx, PR_LATCH_DBLOCK); |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 204 | dp = (struct dup_inode *) e2fsck_allocate_memory(ctx, |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 205 | sizeof(struct dup_inode), |
| 206 | "duplicate inode record"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 207 | dp->ino = ino; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 208 | dp->dir = 0; |
| 209 | dp->inode = inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 210 | dp->num_dupblocks = pb.dup_blocks; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 211 | dp->next = dup_ino; |
| 212 | dup_ino = dp; |
| 213 | if (ino != EXT2_BAD_INO) |
| 214 | dup_inode_count++; |
| 215 | } |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 216 | if (pctx.errcode) |
| 217 | fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 218 | next: |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 219 | pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode); |
| 220 | if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 221 | goto next; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 222 | if (pctx.errcode) { |
| 223 | fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 224 | ctx->flags |= E2F_FLAG_ABORT; |
| 225 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | ext2fs_close_inode_scan(scan); |
| 229 | fs->get_blocks = 0; |
| 230 | fs->check_directory = 0; |
| 231 | } |
| 232 | |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 233 | static int process_pass1b_block(ext2_filsys fs, |
| 234 | blk_t *block_nr, |
| 235 | e2_blkcnt_t blockcnt, |
| 236 | blk_t ref_blk, |
| 237 | int ref_offset, |
| 238 | void *priv_data) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 239 | { |
| 240 | struct process_block_struct *p; |
| 241 | struct dup_block *dp, *q, *r; |
| 242 | int i; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 243 | e2fsck_t ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 244 | |
Theodore Ts'o | 1917875 | 2000-02-11 15:55:07 +0000 | [diff] [blame] | 245 | if (HOLE_BLKADDR(*block_nr)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 246 | return 0; |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 247 | p = (struct process_block_struct *) priv_data; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 248 | ctx = p->ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 249 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 250 | if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 251 | /* OK, this is a duplicate block */ |
| 252 | if (p->ino != EXT2_BAD_INO) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 253 | p->pctx->blk = *block_nr; |
| 254 | fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 255 | } |
| 256 | p->dup_blocks++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 257 | ext2fs_mark_block_bitmap(ctx->block_dup_map, *block_nr); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 258 | ext2fs_mark_inode_bitmap(inode_dup_map, p->ino); |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 259 | dp = (struct dup_block *) e2fsck_allocate_memory(ctx, |
| 260 | sizeof(struct dup_block), |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 261 | "duplicate block record"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 262 | dp->block = *block_nr; |
| 263 | dp->ino = p->ino; |
| 264 | dp->num_bad = 0; |
| 265 | q = dup_blk; |
| 266 | while (q) { |
| 267 | if (q->block == *block_nr) |
| 268 | break; |
| 269 | q = q->next_block; |
| 270 | } |
| 271 | if (q) { |
| 272 | dp->next_inode = q->next_inode; |
| 273 | q->next_inode = dp; |
| 274 | } else { |
| 275 | dp->next_block = dup_blk; |
| 276 | dup_blk = dp; |
| 277 | } |
| 278 | } |
| 279 | /* |
| 280 | * Set the num_bad field |
| 281 | */ |
| 282 | for (q = dup_blk; q; q = q->next_block) { |
| 283 | i = 0; |
| 284 | for (r = q; r; r = r->next_inode) |
| 285 | i++; |
| 286 | q->num_bad = i; |
| 287 | } |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | /* |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 292 | * Pass 1c: Scan directories for inodes with duplicate blocks. This |
| 293 | * is used so that we can print pathnames when prompting the user for |
| 294 | * what to do. |
| 295 | */ |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 296 | struct search_dir_struct { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 297 | int count; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 298 | ext2_ino_t first_inode; |
| 299 | ext2_ino_t max_inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 302 | static int search_dirent_proc(ext2_ino_t dir, int entry, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 303 | struct ext2_dir_entry *dirent, |
| 304 | int offset, int blocksize, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 305 | char *buf, void *priv_data) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 306 | { |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 307 | struct search_dir_struct *sd; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 308 | struct dup_inode *p; |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 309 | |
| 310 | sd = (struct search_dir_struct *) priv_data; |
| 311 | |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 312 | if (dirent->inode > sd->max_inode) |
| 313 | /* Should abort this inode, but not everything */ |
| 314 | return 0; |
| 315 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 316 | if (!dirent->inode || (entry < DIRENT_OTHER_FILE) || |
| 317 | !ext2fs_test_inode_bitmap(inode_dup_map, dirent->inode)) |
| 318 | return 0; |
| 319 | |
| 320 | for (p = dup_ino; p; p = p->next) { |
| 321 | if ((p->ino >= sd->first_inode) && |
| 322 | (p->ino == dirent->inode)) |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | if (!p || p->dir) |
| 327 | return 0; |
| 328 | |
| 329 | p->dir = dir; |
| 330 | sd->count--; |
| 331 | |
| 332 | return(sd->count ? 0 : DIRENT_ABORT); |
| 333 | } |
| 334 | |
| 335 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 336 | static void pass1c(e2fsck_t ctx, char *block_buf) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 337 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 338 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 339 | struct dup_inode *p; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 340 | int inodes_left = dup_inode_count; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 341 | struct search_dir_struct sd; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 342 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 343 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 344 | clear_problem_context(&pctx); |
| 345 | |
| 346 | fix_problem(ctx, PR_1C_PASS_HEADER, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * First check to see if any of the inodes with dup blocks is |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 350 | * a special inode. (Note that the bad block inode isn't |
| 351 | * counted.) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 352 | */ |
| 353 | for (p = dup_ino; p; p = p->next) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 354 | if ((p->ino < EXT2_FIRST_INODE(fs->super)) && |
| 355 | (p->ino != EXT2_BAD_INO)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 356 | inodes_left--; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Search through all directories to translate inodes to names |
| 361 | * (by searching for the containing directory for that inode.) |
| 362 | */ |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 363 | sd.count = inodes_left; |
| 364 | sd.first_inode = EXT2_FIRST_INODE(fs->super); |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 365 | sd.max_inode = fs->super->s_inodes_count; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 366 | ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf, |
| 367 | search_dirent_proc, &sd); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 370 | static void pass1d(e2fsck_t ctx, char *block_buf) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 371 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 372 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 373 | struct dup_inode *p, *s; |
| 374 | struct dup_block *q, *r; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 375 | ext2_ino_t *shared; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 376 | int shared_len; |
| 377 | int i; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 378 | int file_ok; |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 379 | int meta_data = 0; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 380 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 381 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 382 | clear_problem_context(&pctx); |
| 383 | |
| 384 | fix_problem(ctx, PR_1D_PASS_HEADER, &pctx); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 385 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 386 | |
| 387 | pctx.num = dup_inode_count; |
| 388 | fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx); |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 389 | shared = (ext2_ino_t *) e2fsck_allocate_memory(ctx, |
| 390 | sizeof(ext2_ino_t) * dup_inode_count, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 391 | "Shared inode list"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 392 | for (p = dup_ino; p; p = p->next) { |
| 393 | shared_len = 0; |
| 394 | file_ok = 1; |
| 395 | if (p->ino == EXT2_BAD_INO) |
| 396 | continue; |
| 397 | |
| 398 | /* |
| 399 | * Search through the duplicate records to see which |
| 400 | * inodes share blocks with this one |
| 401 | */ |
| 402 | for (q = dup_blk; q; q = q->next_block) { |
| 403 | /* |
| 404 | * See if this block is used by this inode. |
| 405 | * If it isn't, continue. |
| 406 | */ |
| 407 | for (r = q; r; r = r->next_inode) |
| 408 | if (r->ino == p->ino) |
| 409 | break; |
| 410 | if (!r) |
| 411 | continue; |
| 412 | if (q->num_bad > 1) |
| 413 | file_ok = 0; |
Theodore Ts'o | 80c5d7e | 2000-02-08 23:19:32 +0000 | [diff] [blame] | 414 | if (check_if_fs_block(ctx, q->block)) { |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 415 | file_ok = 0; |
| 416 | meta_data = 1; |
| 417 | } |
| 418 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 419 | /* |
| 420 | * Add all inodes used by this block to the |
| 421 | * shared[] --- which is a unique list, so |
| 422 | * if an inode is already in shared[], don't |
| 423 | * add it again. |
| 424 | */ |
| 425 | for (r = q; r; r = r->next_inode) { |
| 426 | if (r->ino == p->ino) |
| 427 | continue; |
| 428 | for (i = 0; i < shared_len; i++) |
| 429 | if (shared[i] == r->ino) |
| 430 | break; |
| 431 | if (i == shared_len) { |
| 432 | shared[shared_len++] = r->ino; |
| 433 | } |
| 434 | } |
| 435 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * Report the inode that we are working on |
| 439 | */ |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 440 | pctx.inode = &p->inode; |
| 441 | pctx.ino = p->ino; |
| 442 | pctx.dir = p->dir; |
| 443 | pctx.blkcount = p->num_dupblocks; |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 444 | pctx.num = meta_data ? shared_len+1 : shared_len; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 445 | fix_problem(ctx, PR_1D_DUP_FILE, &pctx); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 446 | pctx.blkcount = 0; |
| 447 | pctx.num = 0; |
| 448 | |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 449 | if (meta_data) |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 450 | fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx); |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 451 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 452 | for (i = 0; i < shared_len; i++) { |
| 453 | for (s = dup_ino; s; s = s->next) |
| 454 | if (s->ino == shared[i]) |
| 455 | break; |
| 456 | if (!s) |
| 457 | continue; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 458 | /* |
| 459 | * Report the inode that we are sharing with |
| 460 | */ |
| 461 | pctx.inode = &s->inode; |
| 462 | pctx.ino = s->ino; |
| 463 | pctx.dir = s->dir; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 464 | fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 465 | } |
| 466 | if (file_ok) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 467 | fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 468 | continue; |
| 469 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 470 | if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) { |
| 471 | pctx.errcode = clone_file(ctx, p, block_buf); |
| 472 | if (pctx.errcode) |
| 473 | fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx); |
| 474 | else |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 475 | continue; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 476 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 477 | if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx)) |
| 478 | delete_file(ctx, p, block_buf); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 479 | else |
| 480 | ext2fs_unmark_valid(fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 481 | } |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 482 | ext2fs_free_mem((void **) &shared); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static int delete_file_block(ext2_filsys fs, |
| 486 | blk_t *block_nr, |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 487 | e2_blkcnt_t blockcnt, |
| 488 | blk_t ref_block, |
| 489 | int ref_offset, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 490 | void *priv_data) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 491 | { |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 492 | struct process_block_struct *pb; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 493 | struct dup_block *p; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 494 | e2fsck_t ctx; |
| 495 | |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 496 | pb = (struct process_block_struct *) priv_data; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 497 | ctx = pb->ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 498 | |
Theodore Ts'o | 1917875 | 2000-02-11 15:55:07 +0000 | [diff] [blame] | 499 | if (HOLE_BLKADDR(*block_nr)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 500 | return 0; |
| 501 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 502 | if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 503 | for (p = dup_blk; p; p = p->next_block) |
| 504 | if (p->block == *block_nr) |
| 505 | break; |
| 506 | if (p) { |
| 507 | p->num_bad--; |
| 508 | if (p->num_bad == 1) |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 509 | ext2fs_unmark_block_bitmap(ctx->block_dup_map, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 510 | *block_nr); |
| 511 | } else |
| 512 | com_err("delete_file_block", 0, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 513 | _("internal error; can't find dup_blk for %d\n"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 514 | *block_nr); |
| 515 | } else { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 516 | ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 517 | ext2fs_unmark_block_bitmap(fs->block_map, *block_nr); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 523 | static void delete_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 524 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 525 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 526 | struct process_block_struct pb; |
| 527 | struct ext2_inode inode; |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 528 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 529 | |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 530 | clear_problem_context(&pctx); |
| 531 | pctx.ino = pb.ino = dp->ino; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 532 | pb.dup_blocks = dp->num_dupblocks; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 533 | pb.ctx = ctx; |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 534 | pctx.str = "delete_file"; |
| 535 | |
| 536 | pctx.errcode = ext2fs_block_iterate2(fs, dp->ino, 0, block_buf, |
| 537 | delete_file_block, &pb); |
| 538 | if (pctx.errcode) |
| 539 | fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 540 | ext2fs_unmark_inode_bitmap(ctx->inode_used_map, dp->ino); |
| 541 | ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, dp->ino); |
| 542 | if (ctx->inode_bad_map) |
| 543 | ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, dp->ino); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 544 | ext2fs_unmark_inode_bitmap(fs->inode_map, dp->ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 545 | ext2fs_mark_ib_dirty(fs); |
| 546 | ext2fs_mark_bb_dirty(fs); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 547 | e2fsck_read_inode(ctx, dp->ino, &inode, "delete_file"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 548 | inode.i_links_count = 0; |
| 549 | inode.i_dtime = time(0); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 550 | e2fsck_write_inode(ctx, dp->ino, &inode, "delete_file"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | struct clone_struct { |
| 554 | errcode_t errcode; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 555 | ext2_ino_t dir; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 556 | char *buf; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 557 | e2fsck_t ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 558 | }; |
| 559 | |
| 560 | static int clone_file_block(ext2_filsys fs, |
| 561 | blk_t *block_nr, |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 562 | e2_blkcnt_t blockcnt, |
| 563 | blk_t ref_block, |
| 564 | int ref_offset, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 565 | void *priv_data) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 566 | { |
| 567 | struct dup_block *p; |
| 568 | blk_t new_block; |
| 569 | errcode_t retval; |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 570 | struct clone_struct *cs = (struct clone_struct *) priv_data; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 571 | e2fsck_t ctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 572 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 573 | ctx = cs->ctx; |
| 574 | |
Theodore Ts'o | 1917875 | 2000-02-11 15:55:07 +0000 | [diff] [blame] | 575 | if (HOLE_BLKADDR(*block_nr)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 576 | return 0; |
| 577 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 578 | if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 579 | for (p = dup_blk; p; p = p->next_block) |
| 580 | if (p->block == *block_nr) |
| 581 | break; |
| 582 | if (p) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 583 | retval = ext2fs_new_block(fs, 0, ctx->block_found_map, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 584 | &new_block); |
| 585 | if (retval) { |
| 586 | cs->errcode = retval; |
| 587 | return BLOCK_ABORT; |
| 588 | } |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 589 | if (cs->dir) { |
| 590 | retval = ext2fs_set_dir_block(fs->dblist, |
| 591 | cs->dir, new_block, blockcnt); |
| 592 | if (retval) { |
| 593 | cs->errcode = retval; |
| 594 | return BLOCK_ABORT; |
| 595 | } |
| 596 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 597 | retval = io_channel_read_blk(fs->io, *block_nr, 1, |
| 598 | cs->buf); |
| 599 | if (retval) { |
| 600 | cs->errcode = retval; |
| 601 | return BLOCK_ABORT; |
| 602 | } |
| 603 | retval = io_channel_write_blk(fs->io, new_block, 1, |
| 604 | cs->buf); |
| 605 | if (retval) { |
| 606 | cs->errcode = retval; |
| 607 | return BLOCK_ABORT; |
| 608 | } |
| 609 | p->num_bad--; |
Theodore Ts'o | c1faf9c | 1999-09-14 20:00:54 +0000 | [diff] [blame] | 610 | if (p->num_bad == 1 && |
Theodore Ts'o | 80c5d7e | 2000-02-08 23:19:32 +0000 | [diff] [blame] | 611 | !check_if_fs_block(ctx, *block_nr)) |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 612 | ext2fs_unmark_block_bitmap(ctx->block_dup_map, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 613 | *block_nr); |
| 614 | *block_nr = new_block; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 615 | ext2fs_mark_block_bitmap(ctx->block_found_map, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 616 | new_block); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 617 | ext2fs_mark_block_bitmap(fs->block_map, new_block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 618 | return BLOCK_CHANGED; |
| 619 | } else |
| 620 | com_err("clone_file_block", 0, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 621 | _("internal error; can't find dup_blk for %d\n"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 622 | *block_nr); |
| 623 | } |
| 624 | return 0; |
| 625 | } |
| 626 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 627 | static int clone_file(e2fsck_t ctx, struct dup_inode *dp, char* block_buf) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 628 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 629 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 630 | errcode_t retval; |
| 631 | struct clone_struct cs; |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 632 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 633 | |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 634 | clear_problem_context(&pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 635 | cs.errcode = 0; |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 636 | cs.dir = 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 637 | cs.ctx = ctx; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 638 | retval = ext2fs_get_mem(fs->blocksize, (void **) &cs.buf); |
| 639 | if (retval) |
| 640 | return retval; |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 641 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 642 | if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dp->ino)) |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 643 | cs.dir = dp->ino; |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 644 | |
| 645 | pctx.ino = dp->ino; |
| 646 | pctx.str = "clone_file"; |
| 647 | pctx.errcode = ext2fs_block_iterate2(fs, dp->ino, 0, block_buf, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 648 | clone_file_block, &cs); |
| 649 | ext2fs_mark_bb_dirty(fs); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 650 | ext2fs_free_mem((void **) &cs.buf); |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 651 | if (pctx.errcode) { |
| 652 | fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx); |
| 653 | return pctx.errcode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 654 | } |
| 655 | if (cs.errcode) { |
Theodore Ts'o | 622f5f2 | 1997-10-24 04:18:21 +0000 | [diff] [blame] | 656 | com_err("clone_file", cs.errcode, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 657 | _("returned from clone_file_block")); |
Theodore Ts'o | 133a56d | 2000-11-17 05:40:49 +0000 | [diff] [blame] | 658 | return cs.errcode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 659 | } |
| 660 | return 0; |
| 661 | } |
Theodore Ts'o | 80c5d7e | 2000-02-08 23:19:32 +0000 | [diff] [blame] | 662 | |
| 663 | /* |
| 664 | * This routine returns 1 if a block overlaps with one of the superblocks, |
| 665 | * group descriptors, inode bitmaps, or block bitmaps. |
| 666 | */ |
| 667 | static int check_if_fs_block(e2fsck_t ctx, blk_t test_block) |
| 668 | { |
| 669 | ext2_filsys fs = ctx->fs; |
| 670 | blk_t block; |
| 671 | int i; |
| 672 | |
| 673 | block = fs->super->s_first_data_block; |
| 674 | for (i = 0; i < fs->group_desc_count; i++) { |
| 675 | |
| 676 | /* Check superblocks/block group descriptros */ |
| 677 | if (ext2fs_bg_has_super(fs, i)) { |
| 678 | if (test_block >= block && |
| 679 | (test_block <= block + fs->desc_blocks)) |
| 680 | return 1; |
| 681 | } |
| 682 | |
| 683 | /* Check the inode table */ |
| 684 | if ((fs->group_desc[i].bg_inode_table) && |
| 685 | (test_block >= fs->group_desc[i].bg_inode_table) && |
| 686 | (test_block < (fs->group_desc[i].bg_inode_table + |
| 687 | fs->inode_blocks_per_group))) |
| 688 | return 1; |
| 689 | |
| 690 | /* Check the bitmap blocks */ |
| 691 | if ((test_block == fs->group_desc[i].bg_block_bitmap) || |
| 692 | (test_block == fs->group_desc[i].bg_inode_bitmap)) |
| 693 | return 1; |
| 694 | |
| 695 | block += fs->super->s_blocks_per_group; |
| 696 | } |
| 697 | return 0; |
| 698 | } |