blob: 932a171e9572958e86cde407899dd23c053d9287 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04007 *
Theodore Ts'o3839e651997-04-26 13:21:57 +00008 * 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.
Theodore Ts'oefc6f622008-08-27 23:07:54 -040020 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +000021 * 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'oefc6f622008-08-27 23:07:54 -040027 *
Theodore Ts'o3839e651997-04-26 13:21:57 +000028 */
29
30#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000031#ifdef HAVE_ERRNO_H
32#include <errno.h>
33#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000034
Theodore Ts'o0c193f82003-08-01 14:26:23 -040035#ifdef HAVE_INTTYPES_H
36#include <inttypes.h>
37#endif
38
Theodore Ts'od2ee56d2005-01-09 00:57:45 -050039#ifndef HAVE_INTPTR_T
Theodore Ts'o9c07dc02006-05-29 11:06:16 -040040typedef long intptr_t;
Theodore Ts'od2ee56d2005-01-09 00:57:45 -050041#endif
42
Theodore Ts'o0c193f82003-08-01 14:26:23 -040043/* Needed for architectures where sizeof(int) != sizeof(void *) */
44#define INT_TO_VOIDPTR(val) ((void *)(intptr_t)(val))
45#define VOIDPTR_TO_INT(ptr) ((int)(intptr_t)(ptr))
46
Theodore Ts'o3839e651997-04-26 13:21:57 +000047#include <et/com_err.h>
48#include "e2fsck.h"
49
Theodore Ts'o21c84b71997-04-29 16:15:03 +000050#include "problem.h"
Theodore Ts'o838e7732002-08-01 12:37:00 -040051#include "dict.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000052
Theodore Ts'o342d8472001-07-02 11:54:09 -040053/* Define an extension to the ext2 library's block count information */
54#define BLOCK_COUNT_EXTATTR (-5)
55
JP Abgralle0ed7402014-03-19 19:08:39 -070056struct cluster_el {
57 blk64_t cluster;
58 struct cluster_el *next;
Theodore Ts'o3839e651997-04-26 13:21:57 +000059};
60
Theodore Ts'o838e7732002-08-01 12:37:00 -040061struct inode_el {
62 ext2_ino_t inode;
63 struct inode_el *next;
64};
65
JP Abgralle0ed7402014-03-19 19:08:39 -070066struct dup_cluster {
Theodore Ts'o838e7732002-08-01 12:37:00 -040067 int num_bad;
68 struct inode_el *inode_list;
69};
Theodore Ts'o342d8472001-07-02 11:54:09 -040070
Theodore Ts'o3839e651997-04-26 13:21:57 +000071/*
72 * This structure stores information about a particular inode which
73 * is sharing blocks with other inodes. This information is collected
74 * to display to the user, so that the user knows what files he or she
75 * is dealing with, when trying to decide how to resolve the conflict
76 * of multiply-claimed blocks.
77 */
78struct dup_inode {
Theodore Ts'o838e7732002-08-01 12:37:00 -040079 ext2_ino_t dir;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000080 int num_dupblocks;
81 struct ext2_inode inode;
JP Abgralle0ed7402014-03-19 19:08:39 -070082 struct cluster_el *cluster_list;
Theodore Ts'o3839e651997-04-26 13:21:57 +000083};
84
JP Abgralle0ed7402014-03-19 19:08:39 -070085static int process_pass1b_block(ext2_filsys fs, blk64_t *blocknr,
86 e2_blkcnt_t blockcnt, blk64_t ref_blk,
Theodore Ts'o133a56d2000-11-17 05:40:49 +000087 int ref_offset, void *priv_data);
Theodore Ts'o838e7732002-08-01 12:37:00 -040088static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
89 struct dup_inode *dp, char *block_buf);
JP Abgralle0ed7402014-03-19 19:08:39 -070090static errcode_t clone_file(e2fsck_t ctx, ext2_ino_t ino,
91 struct dup_inode *dp, char* block_buf);
92static int check_if_fs_block(e2fsck_t ctx, blk64_t test_block);
93static int check_if_fs_cluster(e2fsck_t ctx, blk64_t cluster);
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +000094
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000095static void pass1b(e2fsck_t ctx, char *block_buf);
96static void pass1c(e2fsck_t ctx, char *block_buf);
97static void pass1d(e2fsck_t ctx, char *block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +000098
Theodore Ts'o3839e651997-04-26 13:21:57 +000099static int dup_inode_count = 0;
Jim Garlick81cae652007-04-20 08:14:27 -0400100static int dup_inode_founddir = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101
JP Abgralle0ed7402014-03-19 19:08:39 -0700102static dict_t clstr_dict, ino_dict;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400103
Theodore Ts'of3db3561997-04-26 13:34:30 +0000104static ext2fs_inode_bitmap inode_dup_map;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105
Theodore Ts'o838e7732002-08-01 12:37:00 -0400106static int dict_int_cmp(const void *a, const void *b)
107{
Theodore Ts'o0c193f82003-08-01 14:26:23 -0400108 intptr_t ia, ib;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400109
Theodore Ts'o0c193f82003-08-01 14:26:23 -0400110 ia = (intptr_t)a;
111 ib = (intptr_t)b;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400112
113 return (ia-ib);
114}
115
116/*
117 * Add a duplicate block record
118 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700119static void add_dupe(e2fsck_t ctx, ext2_ino_t ino, blk64_t cluster,
Theodore Ts'o838e7732002-08-01 12:37:00 -0400120 struct ext2_inode *inode)
121{
122 dnode_t *n;
JP Abgralle0ed7402014-03-19 19:08:39 -0700123 struct dup_cluster *db;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400124 struct dup_inode *di;
JP Abgralle0ed7402014-03-19 19:08:39 -0700125 struct cluster_el *cluster_el;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400126 struct inode_el *ino_el;
127
JP Abgralle0ed7402014-03-19 19:08:39 -0700128 n = dict_lookup(&clstr_dict, INT_TO_VOIDPTR(cluster));
Theodore Ts'o838e7732002-08-01 12:37:00 -0400129 if (n)
JP Abgralle0ed7402014-03-19 19:08:39 -0700130 db = (struct dup_cluster *) dnode_get(n);
Theodore Ts'o838e7732002-08-01 12:37:00 -0400131 else {
JP Abgralle0ed7402014-03-19 19:08:39 -0700132 db = (struct dup_cluster *) e2fsck_allocate_memory(ctx,
133 sizeof(struct dup_cluster), "duplicate cluster header");
Theodore Ts'o838e7732002-08-01 12:37:00 -0400134 db->num_bad = 0;
135 db->inode_list = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700136 dict_alloc_insert(&clstr_dict, INT_TO_VOIDPTR(cluster), db);
Theodore Ts'o838e7732002-08-01 12:37:00 -0400137 }
138 ino_el = (struct inode_el *) e2fsck_allocate_memory(ctx,
139 sizeof(struct inode_el), "inode element");
140 ino_el->inode = ino;
141 ino_el->next = db->inode_list;
142 db->inode_list = ino_el;
143 db->num_bad++;
144
Theodore Ts'o0c193f82003-08-01 14:26:23 -0400145 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino));
Theodore Ts'o838e7732002-08-01 12:37:00 -0400146 if (n)
147 di = (struct dup_inode *) dnode_get(n);
148 else {
149 di = (struct dup_inode *) e2fsck_allocate_memory(ctx,
150 sizeof(struct dup_inode), "duplicate inode header");
Jim Garlick81cae652007-04-20 08:14:27 -0400151 if (ino == EXT2_ROOT_INO) {
152 di->dir = EXT2_ROOT_INO;
153 dup_inode_founddir++;
154 } else
155 di->dir = 0;
156
Theodore Ts'o838e7732002-08-01 12:37:00 -0400157 di->num_dupblocks = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700158 di->cluster_list = 0;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400159 di->inode = *inode;
Theodore Ts'o0c193f82003-08-01 14:26:23 -0400160 dict_alloc_insert(&ino_dict, INT_TO_VOIDPTR(ino), di);
Theodore Ts'o838e7732002-08-01 12:37:00 -0400161 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700162 cluster_el = (struct cluster_el *) e2fsck_allocate_memory(ctx,
163 sizeof(struct cluster_el), "cluster element");
164 cluster_el->cluster = cluster;
165 cluster_el->next = di->cluster_list;
166 di->cluster_list = cluster_el;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400167 di->num_dupblocks++;
168}
169
170/*
171 * Free a duplicate inode record
172 */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400173static void inode_dnode_free(dnode_t *node,
Theodore Ts'o54434922003-12-07 01:28:50 -0500174 void *context EXT2FS_ATTR((unused)))
Theodore Ts'o838e7732002-08-01 12:37:00 -0400175{
176 struct dup_inode *di;
JP Abgralle0ed7402014-03-19 19:08:39 -0700177 struct cluster_el *p, *next;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400178
179 di = (struct dup_inode *) dnode_get(node);
JP Abgralle0ed7402014-03-19 19:08:39 -0700180 for (p = di->cluster_list; p; p = next) {
Theodore Ts'o838e7732002-08-01 12:37:00 -0400181 next = p->next;
182 free(p);
183 }
Theodore Ts'o23f75f62009-06-15 03:50:07 -0400184 free(di);
Theodore Ts'o838e7732002-08-01 12:37:00 -0400185 free(node);
186}
187
188/*
JP Abgralle0ed7402014-03-19 19:08:39 -0700189 * Free a duplicate cluster record
Theodore Ts'o838e7732002-08-01 12:37:00 -0400190 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700191static void cluster_dnode_free(dnode_t *node,
192 void *context EXT2FS_ATTR((unused)))
Theodore Ts'o838e7732002-08-01 12:37:00 -0400193{
JP Abgralle0ed7402014-03-19 19:08:39 -0700194 struct dup_cluster *dc;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400195 struct inode_el *p, *next;
196
JP Abgralle0ed7402014-03-19 19:08:39 -0700197 dc = (struct dup_cluster *) dnode_get(node);
198 for (p = dc->inode_list; p; p = next) {
Theodore Ts'o838e7732002-08-01 12:37:00 -0400199 next = p->next;
200 free(p);
201 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700202 free(dc);
Theodore Ts'o838e7732002-08-01 12:37:00 -0400203 free(node);
204}
205
206
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207/*
208 * Main procedure for handling duplicate blocks
209 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000210void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000212 ext2_filsys fs = ctx->fs;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000213 struct problem_context pctx;
Ken Chenfea390e2009-05-04 20:12:30 -0700214#ifdef RESOURCE_TRACK
215 struct resource_track rtrack;
216#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000217
218 clear_problem_context(&pctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400219
JP Abgralle0ed7402014-03-19 19:08:39 -0700220 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
221 _("multiply claimed inode map"),
222 EXT2FS_BMAP64_RBTREE, "inode_dup_map",
223 &inode_dup_map);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000224 if (pctx.errcode) {
225 fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000226 ctx->flags |= E2F_FLAG_ABORT;
227 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000228 }
Theodore Ts'o838e7732002-08-01 12:37:00 -0400229
230 dict_init(&ino_dict, DICTCOUNT_T_MAX, dict_int_cmp);
JP Abgralle0ed7402014-03-19 19:08:39 -0700231 dict_init(&clstr_dict, DICTCOUNT_T_MAX, dict_int_cmp);
Theodore Ts'o838e7732002-08-01 12:37:00 -0400232 dict_set_allocator(&ino_dict, NULL, inode_dnode_free, NULL);
JP Abgralle0ed7402014-03-19 19:08:39 -0700233 dict_set_allocator(&clstr_dict, NULL, cluster_dnode_free, NULL);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400234
Ken Chenfea390e2009-05-04 20:12:30 -0700235 init_resource_track(&rtrack, ctx->fs->io);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000236 pass1b(ctx, block_buf);
Ken Chen9facd072009-05-28 09:55:10 -0400237 print_resource_track(ctx, "Pass 1b", &rtrack, ctx->fs->io);
Ken Chenfea390e2009-05-04 20:12:30 -0700238
Ken Chenfea390e2009-05-04 20:12:30 -0700239 init_resource_track(&rtrack, ctx->fs->io);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000240 pass1c(ctx, block_buf);
Ken Chen9facd072009-05-28 09:55:10 -0400241 print_resource_track(ctx, "Pass 1c", &rtrack, ctx->fs->io);
Ken Chenfea390e2009-05-04 20:12:30 -0700242
Ken Chenfea390e2009-05-04 20:12:30 -0700243 init_resource_track(&rtrack, ctx->fs->io);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000244 pass1d(ctx, block_buf);
Ken Chen9facd072009-05-28 09:55:10 -0400245 print_resource_track(ctx, "Pass 1d", &rtrack, ctx->fs->io);
Ken Chenfea390e2009-05-04 20:12:30 -0700246
Theodore Ts'o3839e651997-04-26 13:21:57 +0000247 /*
248 * Time to free all of the accumulated data structures that we
249 * don't need anymore.
250 */
Theodore Ts'o838e7732002-08-01 12:37:00 -0400251 dict_free_nodes(&ino_dict);
JP Abgralle0ed7402014-03-19 19:08:39 -0700252 dict_free_nodes(&clstr_dict);
Theodore Ts'o23f75f62009-06-15 03:50:07 -0400253 ext2fs_free_inode_bitmap(inode_dup_map);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254}
255
256/*
257 * Scan the inodes looking for inodes that contain duplicate blocks.
258 */
259struct process_block_struct {
Theodore Ts'o838e7732002-08-01 12:37:00 -0400260 e2fsck_t ctx;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000261 ext2_ino_t ino;
262 int dup_blocks;
JP Abgralle0ed7402014-03-19 19:08:39 -0700263 blk64_t cur_cluster;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400264 struct ext2_inode *inode;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000265 struct problem_context *pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266};
267
Theodore Ts'o08b21301997-11-03 19:42:40 +0000268static void pass1b(e2fsck_t ctx, char *block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000270 ext2_filsys fs = ctx->fs;
JP Abgralle0ed7402014-03-19 19:08:39 -0700271 ext2_ino_t ino = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000272 struct ext2_inode inode;
273 ext2_inode_scan scan;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274 struct process_block_struct pb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000275 struct problem_context pctx;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400276
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000277 clear_problem_context(&pctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400278
Theodore Ts'o151786f2004-02-22 15:41:11 -0500279 if (!(ctx->options & E2F_OPT_PREEN))
280 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000281 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
282 &scan);
283 if (pctx.errcode) {
284 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000285 ctx->flags |= E2F_FLAG_ABORT;
286 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000287 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000288 ctx->stashed_inode = &inode;
289 pb.ctx = ctx;
290 pb.pctx = &pctx;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000291 pctx.str = "pass1b";
Theodore Ts'od237a782002-10-03 01:09:35 -0400292 while (1) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700293 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
294 if (e2fsck_mmp_update(fs))
295 fatal_error(ctx, 0);
296 }
Theodore Ts'od237a782002-10-03 01:09:35 -0400297 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
298 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
299 continue;
300 if (pctx.errcode) {
301 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
302 ctx->flags |= E2F_FLAG_ABORT;
303 return;
304 }
305 if (!ino)
306 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000307 pctx.ino = ctx->stashed_ino = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308 if ((ino != EXT2_BAD_INO) &&
JP Abgralle0ed7402014-03-19 19:08:39 -0700309 !ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino))
Theodore Ts'od237a782002-10-03 01:09:35 -0400310 continue;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000311
312 pb.ino = ino;
313 pb.dup_blocks = 0;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400314 pb.inode = &inode;
JP Abgralle0ed7402014-03-19 19:08:39 -0700315 pb.cur_cluster = ~0;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400316
JP Abgralle0ed7402014-03-19 19:08:39 -0700317 if (ext2fs_inode_has_valid_blocks2(fs, &inode) ||
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400318 (ino == EXT2_BAD_INO))
JP Abgralle0ed7402014-03-19 19:08:39 -0700319 pctx.errcode = ext2fs_block_iterate3(fs, ino,
Theodore Ts'o15d482b2007-08-20 21:31:11 -0400320 BLOCK_FLAG_READ_ONLY, block_buf,
321 process_pass1b_block, &pb);
JP Abgralle0ed7402014-03-19 19:08:39 -0700322 /* If the feature is not set, attrs will be cleared later anyway */
323 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) &&
324 ext2fs_file_acl_block(fs, &inode)) {
325 blk64_t blk = ext2fs_file_acl_block(fs, &inode);
326 process_pass1b_block(fs, &blk,
Theodore Ts'o342d8472001-07-02 11:54:09 -0400327 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
JP Abgralle0ed7402014-03-19 19:08:39 -0700328 ext2fs_file_acl_block_set(fs, &inode, blk);
329 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000330 if (pb.dup_blocks) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000331 end_problem_latch(ctx, PR_LATCH_DBLOCK);
Theodore Ts'o838e7732002-08-01 12:37:00 -0400332 if (ino >= EXT2_FIRST_INODE(fs->super) ||
333 ino == EXT2_ROOT_INO)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000334 dup_inode_count++;
335 }
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000336 if (pctx.errcode)
337 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000338 }
339 ext2fs_close_inode_scan(scan);
Theodore Ts'o71d521c2001-06-01 19:29:36 +0000340 e2fsck_use_inode_shortcuts(ctx, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000341}
342
Theodore Ts'o54434922003-12-07 01:28:50 -0500343static int process_pass1b_block(ext2_filsys fs EXT2FS_ATTR((unused)),
JP Abgralle0ed7402014-03-19 19:08:39 -0700344 blk64_t *block_nr,
345 e2_blkcnt_t blockcnt,
346 blk64_t ref_blk EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500347 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000348 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000349{
350 struct process_block_struct *p;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000351 e2fsck_t ctx;
JP Abgralle0ed7402014-03-19 19:08:39 -0700352 blk64_t lc;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000353
Theodore Ts'o19178752000-02-11 15:55:07 +0000354 if (HOLE_BLKADDR(*block_nr))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000355 return 0;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000356 p = (struct process_block_struct *) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000357 ctx = p->ctx;
JP Abgralle0ed7402014-03-19 19:08:39 -0700358 lc = EXT2FS_B2C(fs, blockcnt);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400359
JP Abgralle0ed7402014-03-19 19:08:39 -0700360 if (!ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr))
361 goto finish;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400362
Theodore Ts'o838e7732002-08-01 12:37:00 -0400363 /* OK, this is a duplicate block */
364 if (p->ino != EXT2_BAD_INO) {
365 p->pctx->blk = *block_nr;
366 fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000367 }
Theodore Ts'o838e7732002-08-01 12:37:00 -0400368 p->dup_blocks++;
JP Abgralle0ed7402014-03-19 19:08:39 -0700369 ext2fs_mark_inode_bitmap2(inode_dup_map, p->ino);
Theodore Ts'o838e7732002-08-01 12:37:00 -0400370
JP Abgralle0ed7402014-03-19 19:08:39 -0700371 if (lc != p->cur_cluster)
372 add_dupe(ctx, p->ino, EXT2FS_B2C(fs, *block_nr), p->inode);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400373
JP Abgralle0ed7402014-03-19 19:08:39 -0700374finish:
375 p->cur_cluster = lc;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000376 return 0;
377}
378
379/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000380 * Pass 1c: Scan directories for inodes with duplicate blocks. This
381 * is used so that we can print pathnames when prompting the user for
382 * what to do.
383 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000384struct search_dir_struct {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000385 int count;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000386 ext2_ino_t first_inode;
387 ext2_ino_t max_inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000388};
389
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000390static int search_dirent_proc(ext2_ino_t dir, int entry,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000391 struct ext2_dir_entry *dirent,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400392 int offset EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500393 int blocksize EXT2FS_ATTR((unused)),
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400394 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500395 void *priv_data)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000396{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000397 struct search_dir_struct *sd;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000398 struct dup_inode *p;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400399 dnode_t *n;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000400
401 sd = (struct search_dir_struct *) priv_data;
402
Theodore Ts'o521e3681997-04-29 17:48:10 +0000403 if (dirent->inode > sd->max_inode)
404 /* Should abort this inode, but not everything */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400405 return 0;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000406
Theodore Ts'o838e7732002-08-01 12:37:00 -0400407 if ((dirent->inode < sd->first_inode) || (entry < DIRENT_OTHER_FILE) ||
JP Abgralle0ed7402014-03-19 19:08:39 -0700408 !ext2fs_test_inode_bitmap2(inode_dup_map, dirent->inode))
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000409 return 0;
410
Theodore Ts'o0c193f82003-08-01 14:26:23 -0400411 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(dirent->inode));
Theodore Ts'o838e7732002-08-01 12:37:00 -0400412 if (!n)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000413 return 0;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400414 p = (struct dup_inode *) dnode_get(n);
Jim Garlick3d51ff82007-04-10 22:55:09 -0400415 if (!p->dir) {
416 p->dir = dir;
417 sd->count--;
418 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000419
420 return(sd->count ? 0 : DIRENT_ABORT);
421}
422
423
Theodore Ts'o08b21301997-11-03 19:42:40 +0000424static void pass1c(e2fsck_t ctx, char *block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000425{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000426 ext2_filsys fs = ctx->fs;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000427 struct search_dir_struct sd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000428 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000429
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000430 clear_problem_context(&pctx);
431
Theodore Ts'o151786f2004-02-22 15:41:11 -0500432 if (!(ctx->options & E2F_OPT_PREEN))
433 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000434
435 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000436 * Search through all directories to translate inodes to names
437 * (by searching for the containing directory for that inode.)
438 */
Jim Garlick81cae652007-04-20 08:14:27 -0400439 sd.count = dup_inode_count - dup_inode_founddir;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000440 sd.first_inode = EXT2_FIRST_INODE(fs->super);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000441 sd.max_inode = fs->super->s_inodes_count;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000442 ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
443 search_dirent_proc, &sd);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400444}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000446static void pass1d(e2fsck_t ctx, char *block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000447{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000448 ext2_filsys fs = ctx->fs;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400449 struct dup_inode *p, *t;
JP Abgralle0ed7402014-03-19 19:08:39 -0700450 struct dup_cluster *q;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400451 ext2_ino_t *shared, ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000452 int shared_len;
453 int i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000454 int file_ok;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000455 int meta_data = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000456 struct problem_context pctx;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400457 dnode_t *n, *m;
JP Abgralle0ed7402014-03-19 19:08:39 -0700458 struct cluster_el *s;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400459 struct inode_el *r;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400460
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000461 clear_problem_context(&pctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400462
Theodore Ts'o151786f2004-02-22 15:41:11 -0500463 if (!(ctx->options & E2F_OPT_PREEN))
464 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000465 e2fsck_read_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000466
Theodore Ts'o838e7732002-08-01 12:37:00 -0400467 pctx.num = dup_inode_count; /* dict_count(&ino_dict); */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000468 fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000469 shared = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
Theodore Ts'o838e7732002-08-01 12:37:00 -0400470 sizeof(ext2_ino_t) * dict_count(&ino_dict),
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000471 "Shared inode list");
Theodore Ts'o838e7732002-08-01 12:37:00 -0400472 for (n = dict_first(&ino_dict); n; n = dict_next(&ino_dict, n)) {
473 p = (struct dup_inode *) dnode_get(n);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000474 shared_len = 0;
475 file_ok = 1;
Theodore Ts'o0c193f82003-08-01 14:26:23 -0400476 ino = (ext2_ino_t)VOIDPTR_TO_INT(dnode_getkey(n));
Theodore Ts'o5e916142005-04-14 17:10:14 -0400477 if (ino == EXT2_BAD_INO || ino == EXT2_RESIZE_INO)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478 continue;
479
480 /*
Theodore Ts'o838e7732002-08-01 12:37:00 -0400481 * Find all of the inodes which share blocks with this
482 * one. First we find all of the duplicate blocks
483 * belonging to this inode, and then search each block
484 * get the list of inodes, and merge them together.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700486 for (s = p->cluster_list; s; s = s->next) {
487 m = dict_lookup(&clstr_dict,
488 INT_TO_VOIDPTR(s->cluster));
Theodore Ts'o838e7732002-08-01 12:37:00 -0400489 if (!m)
490 continue; /* Should never happen... */
JP Abgralle0ed7402014-03-19 19:08:39 -0700491 q = (struct dup_cluster *) dnode_get(m);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000492 if (q->num_bad > 1)
493 file_ok = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700494 if (check_if_fs_cluster(ctx, s->cluster)) {
Theodore Ts'o521e3681997-04-29 17:48:10 +0000495 file_ok = 0;
496 meta_data = 1;
497 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400498
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499 /*
500 * Add all inodes used by this block to the
501 * shared[] --- which is a unique list, so
502 * if an inode is already in shared[], don't
503 * add it again.
504 */
Theodore Ts'o838e7732002-08-01 12:37:00 -0400505 for (r = q->inode_list; r; r = r->next) {
506 if (r->inode == ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000507 continue;
508 for (i = 0; i < shared_len; i++)
Theodore Ts'o838e7732002-08-01 12:37:00 -0400509 if (shared[i] == r->inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510 break;
511 if (i == shared_len) {
Theodore Ts'o838e7732002-08-01 12:37:00 -0400512 shared[shared_len++] = r->inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000513 }
514 }
515 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000516
517 /*
518 * Report the inode that we are working on
519 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000520 pctx.inode = &p->inode;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400521 pctx.ino = ino;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000522 pctx.dir = p->dir;
523 pctx.blkcount = p->num_dupblocks;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000524 pctx.num = meta_data ? shared_len+1 : shared_len;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000525 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000526 pctx.blkcount = 0;
527 pctx.num = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400528
Theodore Ts'o521e3681997-04-29 17:48:10 +0000529 if (meta_data)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000530 fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400531
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532 for (i = 0; i < shared_len; i++) {
Theodore Ts'o0c193f82003-08-01 14:26:23 -0400533 m = dict_lookup(&ino_dict, INT_TO_VOIDPTR(shared[i]));
Theodore Ts'o838e7732002-08-01 12:37:00 -0400534 if (!m)
535 continue; /* should never happen */
536 t = (struct dup_inode *) dnode_get(m);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000537 /*
538 * Report the inode that we are sharing with
539 */
Theodore Ts'o838e7732002-08-01 12:37:00 -0400540 pctx.inode = &t->inode;
541 pctx.ino = shared[i];
542 pctx.dir = t->dir;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000543 fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 }
545 if (file_ok) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000546 fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000547 continue;
548 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000549 if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
Theodore Ts'o838e7732002-08-01 12:37:00 -0400550 pctx.errcode = clone_file(ctx, ino, p, block_buf);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000551 if (pctx.errcode)
552 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
553 else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000554 continue;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000556 if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
Theodore Ts'o838e7732002-08-01 12:37:00 -0400557 delete_file(ctx, ino, p, block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000558 else
559 ext2fs_unmark_valid(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000560 }
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400561 ext2fs_free_mem(&shared);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000562}
563
Theodore Ts'o7abb2bd2001-07-07 13:20:34 -0400564/*
565 * Drop the refcount on the dup_block structure, and clear the entry
566 * in the block_dup_map if appropriate.
567 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700568static void decrement_badcount(e2fsck_t ctx, blk64_t block,
569 struct dup_cluster *p)
Theodore Ts'o7abb2bd2001-07-07 13:20:34 -0400570{
571 p->num_bad--;
572 if (p->num_bad <= 0 ||
JP Abgralle0ed7402014-03-19 19:08:39 -0700573 (p->num_bad == 1 && !check_if_fs_block(ctx, block))) {
574 if (check_if_fs_cluster(ctx, EXT2FS_B2C(ctx->fs, block)))
575 return;
576 ext2fs_unmark_block_bitmap2(ctx->block_dup_map, block);
577 }
Theodore Ts'o7abb2bd2001-07-07 13:20:34 -0400578}
579
Theodore Ts'o3839e651997-04-26 13:21:57 +0000580static int delete_file_block(ext2_filsys fs,
JP Abgralle0ed7402014-03-19 19:08:39 -0700581 blk64_t *block_nr,
582 e2_blkcnt_t blockcnt,
583 blk64_t ref_block EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500584 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000585 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000586{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000587 struct process_block_struct *pb;
JP Abgralle0ed7402014-03-19 19:08:39 -0700588 struct dup_cluster *p;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400589 dnode_t *n;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000590 e2fsck_t ctx;
JP Abgralle0ed7402014-03-19 19:08:39 -0700591 blk64_t c, lc;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000592
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000593 pb = (struct process_block_struct *) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000594 ctx = pb->ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000595
Theodore Ts'o19178752000-02-11 15:55:07 +0000596 if (HOLE_BLKADDR(*block_nr))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 return 0;
598
JP Abgralle0ed7402014-03-19 19:08:39 -0700599 c = EXT2FS_B2C(fs, *block_nr);
600 lc = EXT2FS_B2C(fs, blockcnt);
601 if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
602 n = dict_lookup(&clstr_dict, INT_TO_VOIDPTR(c));
Theodore Ts'o838e7732002-08-01 12:37:00 -0400603 if (n) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700604 p = (struct dup_cluster *) dnode_get(n);
605 if (lc != pb->cur_cluster)
606 decrement_badcount(ctx, *block_nr, p);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000607 } else
608 com_err("delete_file_block", 0,
JP Abgralle0ed7402014-03-19 19:08:39 -0700609 _("internal error: can't find dup_blk for %llu\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000610 *block_nr);
611 } else {
JP Abgralle0ed7402014-03-19 19:08:39 -0700612 ext2fs_unmark_block_bitmap2(ctx->block_found_map, *block_nr);
613 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
614 pb->dup_blocks++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000615 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700616 pb->cur_cluster = lc;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400617
Theodore Ts'o3839e651997-04-26 13:21:57 +0000618 return 0;
619}
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400620
Theodore Ts'o838e7732002-08-01 12:37:00 -0400621static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
622 struct dup_inode *dp, char* block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000624 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000625 struct process_block_struct pb;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000626 struct problem_context pctx;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400627 unsigned int count;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000629 clear_problem_context(&pctx);
Theodore Ts'o838e7732002-08-01 12:37:00 -0400630 pctx.ino = pb.ino = ino;
JP Abgralle0ed7402014-03-19 19:08:39 -0700631 pb.dup_blocks = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000632 pb.ctx = ctx;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000633 pctx.str = "delete_file";
JP Abgralle0ed7402014-03-19 19:08:39 -0700634 pb.cur_cluster = ~0;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000635
JP Abgralle0ed7402014-03-19 19:08:39 -0700636 if (ext2fs_inode_has_valid_blocks2(fs, &dp->inode))
637 pctx.errcode = ext2fs_block_iterate3(fs, ino,
638 BLOCK_FLAG_READ_ONLY,
639 block_buf,
640 delete_file_block, &pb);
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000641 if (pctx.errcode)
642 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000643 if (ctx->inode_bad_map)
JP Abgralle0ed7402014-03-19 19:08:39 -0700644 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
645 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(dp->inode.i_mode));
646 quota_data_sub(ctx->qctx, &dp->inode, ino,
647 pb.dup_blocks * fs->blocksize);
648 quota_data_inodes(ctx->qctx, &dp->inode, ino, -1);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400649
650 /* Inode may have changed by block_iterate, so reread it */
JP Abgralle0ed7402014-03-19 19:08:39 -0700651 e2fsck_read_inode(ctx, ino, &dp->inode, "delete_file");
652 e2fsck_clear_inode(ctx, ino, &dp->inode, 0, "delete_file");
653 if (ext2fs_file_acl_block(fs, &dp->inode) &&
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400654 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
655 count = 1;
JP Abgralle0ed7402014-03-19 19:08:39 -0700656 pctx.errcode = ext2fs_adjust_ea_refcount2(fs,
657 ext2fs_file_acl_block(fs, &dp->inode),
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400658 block_buf, -1, &count);
659 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
660 pctx.errcode = 0;
661 count = 1;
662 }
663 if (pctx.errcode) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700664 pctx.blk = ext2fs_file_acl_block(fs, &dp->inode);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400665 fix_problem(ctx, PR_1B_ADJ_EA_REFCOUNT, &pctx);
666 }
667 /*
668 * If the count is zero, then arrange to have the
669 * block deleted. If the block is in the block_dup_map,
670 * also call delete_file_block since it will take care
671 * of keeping the accounting straight.
672 */
673 if ((count == 0) ||
JP Abgralle0ed7402014-03-19 19:08:39 -0700674 ext2fs_test_block_bitmap2(ctx->block_dup_map,
675 ext2fs_file_acl_block(fs, &dp->inode))) {
676 blk64_t blk = ext2fs_file_acl_block(fs, &dp->inode);
677 delete_file_block(fs, &blk,
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400678 BLOCK_COUNT_EXTATTR, 0, 0, &pb);
JP Abgralle0ed7402014-03-19 19:08:39 -0700679 ext2fs_file_acl_block_set(fs, &dp->inode, blk);
680 quota_data_sub(ctx->qctx, &dp->inode, ino, fs->blocksize);
681 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400682 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000683}
684
685struct clone_struct {
686 errcode_t errcode;
JP Abgralle0ed7402014-03-19 19:08:39 -0700687 blk64_t dup_cluster;
688 blk64_t alloc_block;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000689 ext2_ino_t dir;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000690 char *buf;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000691 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000692};
693
694static int clone_file_block(ext2_filsys fs,
JP Abgralle0ed7402014-03-19 19:08:39 -0700695 blk64_t *block_nr,
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000696 e2_blkcnt_t blockcnt,
JP Abgralle0ed7402014-03-19 19:08:39 -0700697 blk64_t ref_block EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500698 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000699 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000700{
JP Abgralle0ed7402014-03-19 19:08:39 -0700701 struct dup_cluster *p;
702 blk64_t new_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000703 errcode_t retval;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000704 struct clone_struct *cs = (struct clone_struct *) priv_data;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400705 dnode_t *n;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000706 e2fsck_t ctx;
JP Abgralle0ed7402014-03-19 19:08:39 -0700707 blk64_t c;
708 int is_meta = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000709
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000710 ctx = cs->ctx;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400711
Theodore Ts'o19178752000-02-11 15:55:07 +0000712 if (HOLE_BLKADDR(*block_nr))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000713 return 0;
714
JP Abgralle0ed7402014-03-19 19:08:39 -0700715 c = EXT2FS_B2C(fs, blockcnt);
716 if (check_if_fs_cluster(ctx, EXT2FS_B2C(fs, *block_nr)))
717 is_meta = 1;
718
719 if (c == cs->dup_cluster && cs->alloc_block) {
720 new_block = cs->alloc_block;
721 goto got_block;
722 }
723
724 if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
725 n = dict_lookup(&clstr_dict,
726 INT_TO_VOIDPTR(EXT2FS_B2C(fs, *block_nr)));
727 if (!n) {
JP Abgrall65f0aab2014-03-06 13:50:20 -0800728 com_err("clone_file_block", 0,
JP Abgralle0ed7402014-03-19 19:08:39 -0700729 _("internal error: can't find dup_blk for %llu\n"),
JP Abgrall65f0aab2014-03-06 13:50:20 -0800730 *block_nr);
JP Abgralle0ed7402014-03-19 19:08:39 -0700731 return 0;
732 }
733
734 p = (struct dup_cluster *) dnode_get(n);
735 if (!is_meta)
736 decrement_badcount(ctx, *block_nr, p);
737
738 cs->dup_cluster = c;
739
740 retval = ext2fs_new_block2(fs, 0, ctx->block_found_map,
741 &new_block);
742 if (retval) {
743 cs->errcode = retval;
744 return BLOCK_ABORT;
745 }
746 cs->alloc_block = new_block;
747
748 got_block:
749 new_block &= ~EXT2FS_CLUSTER_MASK(fs);
750 new_block += EXT2FS_CLUSTER_MASK(fs) & blockcnt;
751 if (cs->dir && (blockcnt >= 0)) {
752 retval = ext2fs_set_dir_block2(fs->dblist,
753 cs->dir, new_block, blockcnt);
754 if (retval) {
755 cs->errcode = retval;
756 return BLOCK_ABORT;
757 }
758 }
759#if 0
760 printf("Cloning block #%lld from %llu to %llu\n",
761 blockcnt, *block_nr, new_block);
762#endif
763 retval = io_channel_read_blk64(fs->io, *block_nr, 1, cs->buf);
764 if (retval) {
765 cs->errcode = retval;
766 return BLOCK_ABORT;
767 }
768 retval = io_channel_write_blk64(fs->io, new_block, 1, cs->buf);
769 if (retval) {
770 cs->errcode = retval;
771 return BLOCK_ABORT;
772 }
773 *block_nr = new_block;
774 ext2fs_mark_block_bitmap2(ctx->block_found_map, new_block);
775 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
776 return BLOCK_CHANGED;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000777 }
778 return 0;
779}
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400780
JP Abgralle0ed7402014-03-19 19:08:39 -0700781static errcode_t clone_file(e2fsck_t ctx, ext2_ino_t ino,
782 struct dup_inode *dp, char* block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000783{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000784 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000785 errcode_t retval;
786 struct clone_struct cs;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000787 struct problem_context pctx;
JP Abgralle0ed7402014-03-19 19:08:39 -0700788 blk64_t blk, new_blk;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400789 dnode_t *n;
790 struct inode_el *ino_el;
JP Abgralle0ed7402014-03-19 19:08:39 -0700791 struct dup_cluster *dc;
Theodore Ts'o838e7732002-08-01 12:37:00 -0400792 struct dup_inode *di;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000793
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000794 clear_problem_context(&pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000795 cs.errcode = 0;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000796 cs.dir = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700797 cs.dup_cluster = ~0;
798 cs.alloc_block = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000799 cs.ctx = ctx;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400800 retval = ext2fs_get_mem(fs->blocksize, &cs.buf);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000801 if (retval)
802 return retval;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000803
JP Abgralle0ed7402014-03-19 19:08:39 -0700804 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, ino))
Theodore Ts'o838e7732002-08-01 12:37:00 -0400805 cs.dir = ino;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000806
Theodore Ts'o838e7732002-08-01 12:37:00 -0400807 pctx.ino = ino;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000808 pctx.str = "clone_file";
JP Abgralle0ed7402014-03-19 19:08:39 -0700809 if (ext2fs_inode_has_valid_blocks2(fs, &dp->inode))
810 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400811 clone_file_block, &cs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000812 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000813 if (pctx.errcode) {
814 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
Theodore Ts'o7abb2bd2001-07-07 13:20:34 -0400815 retval = pctx.errcode;
816 goto errout;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000817 }
818 if (cs.errcode) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700819 com_err("clone_file", cs.errcode, "%s",
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000820 _("returned from clone_file_block"));
Theodore Ts'o7abb2bd2001-07-07 13:20:34 -0400821 retval = cs.errcode;
822 goto errout;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000823 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400824 /* The inode may have changed on disk, so we have to re-read it */
825 e2fsck_read_inode(ctx, ino, &dp->inode, "clone file EA");
JP Abgralle0ed7402014-03-19 19:08:39 -0700826 blk = ext2fs_file_acl_block(fs, &dp->inode);
827 new_blk = blk;
828 if (blk && (clone_file_block(fs, &new_blk,
Theodore Ts'o7abb2bd2001-07-07 13:20:34 -0400829 BLOCK_COUNT_EXTATTR, 0, 0, &cs) ==
830 BLOCK_CHANGED)) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700831 ext2fs_file_acl_block_set(fs, &dp->inode, new_blk);
Theodore Ts'o838e7732002-08-01 12:37:00 -0400832 e2fsck_write_inode(ctx, ino, &dp->inode, "clone file EA");
Theodore Ts'o342d8472001-07-02 11:54:09 -0400833 /*
834 * If we cloned the EA block, find all other inodes
835 * which refered to that EA block, and modify
836 * them to point to the new EA block.
837 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700838 n = dict_lookup(&clstr_dict,
839 INT_TO_VOIDPTR(EXT2FS_B2C(fs, blk)));
Brian Behlendorf538e6542007-03-19 08:52:10 -0400840 if (!n) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400841 com_err("clone_file", 0,
Brian Behlendorf538e6542007-03-19 08:52:10 -0400842 _("internal error: couldn't lookup EA "
JP Abgralle0ed7402014-03-19 19:08:39 -0700843 "block record for %llu"), blk);
Brian Behlendorf538e6542007-03-19 08:52:10 -0400844 retval = 0; /* OK to stumble on... */
845 goto errout;
846 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700847 dc = (struct dup_cluster *) dnode_get(n);
848 for (ino_el = dc->inode_list; ino_el; ino_el = ino_el->next) {
Theodore Ts'o838e7732002-08-01 12:37:00 -0400849 if (ino_el->inode == ino)
Theodore Ts'o342d8472001-07-02 11:54:09 -0400850 continue;
Theodore Ts'o0c193f82003-08-01 14:26:23 -0400851 n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino_el->inode));
Brian Behlendorf538e6542007-03-19 08:52:10 -0400852 if (!n) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400853 com_err("clone_file", 0,
Brian Behlendorf538e6542007-03-19 08:52:10 -0400854 _("internal error: couldn't lookup EA "
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400855 "inode record for %u"),
Brian Behlendorf538e6542007-03-19 08:52:10 -0400856 ino_el->inode);
857 retval = 0; /* OK to stumble on... */
858 goto errout;
859 }
Theodore Ts'o838e7732002-08-01 12:37:00 -0400860 di = (struct dup_inode *) dnode_get(n);
JP Abgralle0ed7402014-03-19 19:08:39 -0700861 if (ext2fs_file_acl_block(fs, &di->inode) == blk) {
862 ext2fs_file_acl_block_set(fs, &di->inode,
863 ext2fs_file_acl_block(fs, &dp->inode));
Theodore Ts'o838e7732002-08-01 12:37:00 -0400864 e2fsck_write_inode(ctx, ino_el->inode,
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400865 &di->inode, "clone file EA");
JP Abgralle0ed7402014-03-19 19:08:39 -0700866 decrement_badcount(ctx, blk, dc);
Theodore Ts'o7abb2bd2001-07-07 13:20:34 -0400867 }
Theodore Ts'o342d8472001-07-02 11:54:09 -0400868 }
869 }
Theodore Ts'o7abb2bd2001-07-07 13:20:34 -0400870 retval = 0;
871errout:
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400872 ext2fs_free_mem(&cs.buf);
Theodore Ts'o7abb2bd2001-07-07 13:20:34 -0400873 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000874}
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +0000875
876/*
877 * This routine returns 1 if a block overlaps with one of the superblocks,
878 * group descriptors, inode bitmaps, or block bitmaps.
879 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700880static int check_if_fs_block(e2fsck_t ctx, blk64_t test_block)
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +0000881{
882 ext2_filsys fs = ctx->fs;
JP Abgralle0ed7402014-03-19 19:08:39 -0700883 blk64_t first_block;
Theodore Ts'o54434922003-12-07 01:28:50 -0500884 dgrp_t i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400885
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400886 first_block = fs->super->s_first_data_block;
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +0000887 for (i = 0; i < fs->group_desc_count; i++) {
888
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400889 /* Check superblocks/block group descriptors */
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +0000890 if (ext2fs_bg_has_super(fs, i)) {
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400891 if (test_block >= first_block &&
892 (test_block <= first_block + fs->desc_blocks))
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +0000893 return 1;
894 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400895
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +0000896 /* Check the inode table */
JP Abgralle0ed7402014-03-19 19:08:39 -0700897 if ((ext2fs_inode_table_loc(fs, i)) &&
898 (test_block >= ext2fs_inode_table_loc(fs, i)) &&
899 (test_block < (ext2fs_inode_table_loc(fs, i) +
Theodore Ts'o80c5d7e2000-02-08 23:19:32 +0000900 fs->inode_blocks_per_group)))
901 return 1;
902
903 /* Check the bitmap blocks */
JP Abgralle0ed7402014-03-19 19:08:39 -0700904 if ((test_block == ext2fs_block_bitmap_loc(fs, i)) ||
905 (test_block == ext2fs_inode_bitmap_loc(fs, i)))
906 return 1;
907
908 first_block += fs->super->s_blocks_per_group;
909 }
910 return 0;
911}
912
913/*
914 * This routine returns 1 if a cluster overlaps with one of the superblocks,
915 * group descriptors, inode bitmaps, or block bitmaps.
916 */
917static int check_if_fs_cluster(e2fsck_t ctx, blk64_t cluster)
918{
919 ext2_filsys fs = ctx->fs;
920 blk64_t first_block;
921 dgrp_t i;
922
923 first_block = fs->super->s_first_data_block;
924 for (i = 0; i < fs->group_desc_count; i++) {
925
926 /* Check superblocks/block group descriptors */
927 if (ext2fs_bg_has_super(fs, i)) {
928 if (cluster >= EXT2FS_B2C(fs, first_block) &&
929 (cluster <= EXT2FS_B2C(fs, first_block +
930 fs->desc_blocks)))
931 return 1;
932 }
933
934 /* Check the inode table */
935 if ((ext2fs_inode_table_loc(fs, i)) &&
936 (cluster >= EXT2FS_B2C(fs,
937 ext2fs_inode_table_loc(fs, i))) &&
938 (cluster <= EXT2FS_B2C(fs,
939 ext2fs_inode_table_loc(fs, i) +
940 fs->inode_blocks_per_group - 1)))
941 return 1;
942
943 /* Check the bitmap blocks */
944 if ((cluster == EXT2FS_B2C(fs,
945 ext2fs_block_bitmap_loc(fs, i))) ||
946 (cluster == EXT2FS_B2C(fs,
947 ext2fs_inode_bitmap_loc(fs, i))))
Theodore Ts'of51b4d32011-07-09 22:56:22 -0400948 return 1;
949
950 first_block += fs->super->s_blocks_per_group;
951 }
952 return 0;
953}