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 *
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%
27 *
28 */
29
30#include <time.h>
31#ifdef HAVE_ERRNO_H
32#include <errno.h>
33#endif
34
35#ifdef HAVE_INTTYPES_H
36#include <inttypes.h>
37#endif
38
39#ifndef HAVE_INTPTR_T
40typedef long intptr_t;
41#endif
42
43/* 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
47#include <et/com_err.h>
48#include "e2fsck.h"
49
50#include "problem.h"
51#include "dict.h"
52
53/* Define an extension to the ext2 library's block count information */
54#define BLOCK_COUNT_EXTATTR	(-5)
55
56struct block_el {
57	blk_t	block;
58	struct block_el *next;
59};
60
61struct inode_el {
62	ext2_ino_t	inode;
63	struct inode_el *next;
64};
65
66struct dup_block {
67	int		num_bad;
68	struct inode_el *inode_list;
69};
70
71/*
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 {
79	ext2_ino_t		dir;
80	int			num_dupblocks;
81	struct ext2_inode	inode;
82	struct block_el		*block_list;
83};
84
85static int process_pass1b_block(ext2_filsys fs, blk_t	*blocknr,
86				e2_blkcnt_t blockcnt, blk_t ref_blk,
87				int ref_offset, void *priv_data);
88static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
89			struct dup_inode *dp, char *block_buf);
90static int 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, blk_t test_blk);
93
94static void pass1b(e2fsck_t ctx, char *block_buf);
95static void pass1c(e2fsck_t ctx, char *block_buf);
96static void pass1d(e2fsck_t ctx, char *block_buf);
97
98static int dup_inode_count = 0;
99static int dup_inode_founddir = 0;
100
101static dict_t blk_dict, ino_dict;
102
103static ext2fs_inode_bitmap inode_dup_map;
104
105static int dict_int_cmp(const void *a, const void *b)
106{
107	intptr_t	ia, ib;
108
109	ia = (intptr_t)a;
110	ib = (intptr_t)b;
111
112	return (ia-ib);
113}
114
115/*
116 * Add a duplicate block record
117 */
118static void add_dupe(e2fsck_t ctx, ext2_ino_t ino, blk_t blk,
119		     struct ext2_inode *inode)
120{
121	dnode_t	*n;
122	struct dup_block	*db;
123	struct dup_inode	*di;
124	struct block_el		*blk_el;
125	struct inode_el 	*ino_el;
126
127	n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(blk));
128	if (n)
129		db = (struct dup_block *) dnode_get(n);
130	else {
131		db = (struct dup_block *) e2fsck_allocate_memory(ctx,
132			 sizeof(struct dup_block), "duplicate block header");
133		db->num_bad = 0;
134		db->inode_list = 0;
135		dict_alloc_insert(&blk_dict, INT_TO_VOIDPTR(blk), db);
136	}
137	ino_el = (struct inode_el *) e2fsck_allocate_memory(ctx,
138			 sizeof(struct inode_el), "inode element");
139	ino_el->inode = ino;
140	ino_el->next = db->inode_list;
141	db->inode_list = ino_el;
142	db->num_bad++;
143
144	n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino));
145	if (n)
146		di = (struct dup_inode *) dnode_get(n);
147	else {
148		di = (struct dup_inode *) e2fsck_allocate_memory(ctx,
149			 sizeof(struct dup_inode), "duplicate inode header");
150		if (ino == EXT2_ROOT_INO) {
151			di->dir = EXT2_ROOT_INO;
152			dup_inode_founddir++;
153		} else
154			di->dir = 0;
155
156		di->num_dupblocks = 0;
157		di->block_list = 0;
158		di->inode = *inode;
159		dict_alloc_insert(&ino_dict, INT_TO_VOIDPTR(ino), di);
160	}
161	blk_el = (struct block_el *) e2fsck_allocate_memory(ctx,
162			 sizeof(struct block_el), "block element");
163	blk_el->block = blk;
164	blk_el->next = di->block_list;
165	di->block_list = blk_el;
166	di->num_dupblocks++;
167}
168
169/*
170 * Free a duplicate inode record
171 */
172static void inode_dnode_free(dnode_t *node,
173			     void *context EXT2FS_ATTR((unused)))
174{
175	struct dup_inode	*di;
176	struct block_el		*p, *next;
177
178	di = (struct dup_inode *) dnode_get(node);
179	for (p = di->block_list; p; p = next) {
180		next = p->next;
181		free(p);
182	}
183	free(di);
184	free(node);
185}
186
187/*
188 * Free a duplicate block record
189 */
190static void block_dnode_free(dnode_t *node,
191			     void *context EXT2FS_ATTR((unused)))
192{
193	struct dup_block	*db;
194	struct inode_el		*p, *next;
195
196	db = (struct dup_block *) dnode_get(node);
197	for (p = db->inode_list; p; p = next) {
198		next = p->next;
199		free(p);
200	}
201	free(db);
202	free(node);
203}
204
205
206/*
207 * Main procedure for handling duplicate blocks
208 */
209void e2fsck_pass1_dupblocks(e2fsck_t ctx, char *block_buf)
210{
211	ext2_filsys 		fs = ctx->fs;
212	struct problem_context	pctx;
213#ifdef RESOURCE_TRACK
214	struct resource_track	rtrack;
215#endif
216
217	clear_problem_context(&pctx);
218
219	pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
220		      _("multiply claimed inode map"), &inode_dup_map);
221	if (pctx.errcode) {
222		fix_problem(ctx, PR_1B_ALLOCATE_IBITMAP_ERROR, &pctx);
223		ctx->flags |= E2F_FLAG_ABORT;
224		return;
225	}
226
227	dict_init(&ino_dict, DICTCOUNT_T_MAX, dict_int_cmp);
228	dict_init(&blk_dict, DICTCOUNT_T_MAX, dict_int_cmp);
229	dict_set_allocator(&ino_dict, NULL, inode_dnode_free, NULL);
230	dict_set_allocator(&blk_dict, NULL, block_dnode_free, NULL);
231
232	init_resource_track(&rtrack, ctx->fs->io);
233	pass1b(ctx, block_buf);
234	print_resource_track(ctx, "Pass 1b", &rtrack, ctx->fs->io);
235
236	init_resource_track(&rtrack, ctx->fs->io);
237	pass1c(ctx, block_buf);
238	print_resource_track(ctx, "Pass 1c", &rtrack, ctx->fs->io);
239
240	init_resource_track(&rtrack, ctx->fs->io);
241	pass1d(ctx, block_buf);
242	print_resource_track(ctx, "Pass 1d", &rtrack, ctx->fs->io);
243
244	/*
245	 * Time to free all of the accumulated data structures that we
246	 * don't need anymore.
247	 */
248	dict_free_nodes(&ino_dict);
249	dict_free_nodes(&blk_dict);
250	ext2fs_free_inode_bitmap(inode_dup_map);
251}
252
253/*
254 * Scan the inodes looking for inodes that contain duplicate blocks.
255 */
256struct process_block_struct {
257	e2fsck_t	ctx;
258	ext2_ino_t	ino;
259	int		dup_blocks;
260	struct ext2_inode *inode;
261	struct problem_context *pctx;
262};
263
264static void pass1b(e2fsck_t ctx, char *block_buf)
265{
266	ext2_filsys fs = ctx->fs;
267	ext2_ino_t ino;
268	struct ext2_inode inode;
269	ext2_inode_scan	scan;
270	struct process_block_struct pb;
271	struct problem_context pctx;
272
273	clear_problem_context(&pctx);
274
275	if (!(ctx->options & E2F_OPT_PREEN))
276		fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
277	pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
278					      &scan);
279	if (pctx.errcode) {
280		fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
281		ctx->flags |= E2F_FLAG_ABORT;
282		return;
283	}
284	ctx->stashed_inode = &inode;
285	pb.ctx = ctx;
286	pb.pctx = &pctx;
287	pctx.str = "pass1b";
288	while (1) {
289		pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
290		if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
291			continue;
292		if (pctx.errcode) {
293			fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
294			ctx->flags |= E2F_FLAG_ABORT;
295			return;
296		}
297		if (!ino)
298			break;
299		pctx.ino = ctx->stashed_ino = ino;
300		if ((ino != EXT2_BAD_INO) &&
301		    !ext2fs_test_inode_bitmap(ctx->inode_used_map, ino))
302			continue;
303
304		pb.ino = ino;
305		pb.dup_blocks = 0;
306		pb.inode = &inode;
307
308		if (ext2fs_inode_has_valid_blocks(&inode) ||
309		    (ino == EXT2_BAD_INO))
310			pctx.errcode = ext2fs_block_iterate2(fs, ino,
311					     BLOCK_FLAG_READ_ONLY, block_buf,
312					     process_pass1b_block, &pb);
313		if (inode.i_file_acl)
314			process_pass1b_block(fs, &inode.i_file_acl,
315					     BLOCK_COUNT_EXTATTR, 0, 0, &pb);
316		if (pb.dup_blocks) {
317			end_problem_latch(ctx, PR_LATCH_DBLOCK);
318			if (ino >= EXT2_FIRST_INODE(fs->super) ||
319			    ino == EXT2_ROOT_INO)
320				dup_inode_count++;
321		}
322		if (pctx.errcode)
323			fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
324	}
325	ext2fs_close_inode_scan(scan);
326	e2fsck_use_inode_shortcuts(ctx, 0);
327}
328
329static int process_pass1b_block(ext2_filsys fs EXT2FS_ATTR((unused)),
330				blk_t	*block_nr,
331				e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
332				blk_t ref_blk EXT2FS_ATTR((unused)),
333				int ref_offset EXT2FS_ATTR((unused)),
334				void *priv_data)
335{
336	struct process_block_struct *p;
337	e2fsck_t ctx;
338
339	if (HOLE_BLKADDR(*block_nr))
340		return 0;
341	p = (struct process_block_struct *) priv_data;
342	ctx = p->ctx;
343
344	if (!ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr))
345		return 0;
346
347	/* OK, this is a duplicate block */
348	if (p->ino != EXT2_BAD_INO) {
349		p->pctx->blk = *block_nr;
350		fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
351	}
352	p->dup_blocks++;
353	ext2fs_mark_inode_bitmap(inode_dup_map, p->ino);
354
355	add_dupe(ctx, p->ino, *block_nr, p->inode);
356
357	return 0;
358}
359
360/*
361 * Pass 1c: Scan directories for inodes with duplicate blocks.  This
362 * is used so that we can print pathnames when prompting the user for
363 * what to do.
364 */
365struct search_dir_struct {
366	int		count;
367	ext2_ino_t	first_inode;
368	ext2_ino_t	max_inode;
369};
370
371static int search_dirent_proc(ext2_ino_t dir, int entry,
372			      struct ext2_dir_entry *dirent,
373			      int offset EXT2FS_ATTR((unused)),
374			      int blocksize EXT2FS_ATTR((unused)),
375			      char *buf EXT2FS_ATTR((unused)),
376			      void *priv_data)
377{
378	struct search_dir_struct *sd;
379	struct dup_inode	*p;
380	dnode_t			*n;
381
382	sd = (struct search_dir_struct *) priv_data;
383
384	if (dirent->inode > sd->max_inode)
385		/* Should abort this inode, but not everything */
386		return 0;
387
388	if ((dirent->inode < sd->first_inode) || (entry < DIRENT_OTHER_FILE) ||
389	    !ext2fs_test_inode_bitmap(inode_dup_map, dirent->inode))
390		return 0;
391
392	n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(dirent->inode));
393	if (!n)
394		return 0;
395	p = (struct dup_inode *) dnode_get(n);
396	if (!p->dir) {
397		p->dir = dir;
398		sd->count--;
399	}
400
401	return(sd->count ? 0 : DIRENT_ABORT);
402}
403
404
405static void pass1c(e2fsck_t ctx, char *block_buf)
406{
407	ext2_filsys fs = ctx->fs;
408	struct search_dir_struct sd;
409	struct problem_context pctx;
410
411	clear_problem_context(&pctx);
412
413	if (!(ctx->options & E2F_OPT_PREEN))
414		fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
415
416	/*
417	 * Search through all directories to translate inodes to names
418	 * (by searching for the containing directory for that inode.)
419	 */
420	sd.count = dup_inode_count - dup_inode_founddir;
421	sd.first_inode = EXT2_FIRST_INODE(fs->super);
422	sd.max_inode = fs->super->s_inodes_count;
423	ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
424				  search_dirent_proc, &sd);
425}
426
427static void pass1d(e2fsck_t ctx, char *block_buf)
428{
429	ext2_filsys fs = ctx->fs;
430	struct dup_inode	*p, *t;
431	struct dup_block	*q;
432	ext2_ino_t		*shared, ino;
433	int	shared_len;
434	int	i;
435	int	file_ok;
436	int	meta_data = 0;
437	struct problem_context pctx;
438	dnode_t	*n, *m;
439	struct block_el	*s;
440	struct inode_el *r;
441
442	clear_problem_context(&pctx);
443
444	if (!(ctx->options & E2F_OPT_PREEN))
445		fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
446	e2fsck_read_bitmaps(ctx);
447
448	pctx.num = dup_inode_count; /* dict_count(&ino_dict); */
449	fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
450	shared = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
451				sizeof(ext2_ino_t) * dict_count(&ino_dict),
452				"Shared inode list");
453	for (n = dict_first(&ino_dict); n; n = dict_next(&ino_dict, n)) {
454		p = (struct dup_inode *) dnode_get(n);
455		shared_len = 0;
456		file_ok = 1;
457		ino = (ext2_ino_t)VOIDPTR_TO_INT(dnode_getkey(n));
458		if (ino == EXT2_BAD_INO || ino == EXT2_RESIZE_INO)
459			continue;
460
461		/*
462		 * Find all of the inodes which share blocks with this
463		 * one.  First we find all of the duplicate blocks
464		 * belonging to this inode, and then search each block
465		 * get the list of inodes, and merge them together.
466		 */
467		for (s = p->block_list; s; s = s->next) {
468			m = dict_lookup(&blk_dict, INT_TO_VOIDPTR(s->block));
469			if (!m)
470				continue; /* Should never happen... */
471			q = (struct dup_block *) dnode_get(m);
472			if (q->num_bad > 1)
473				file_ok = 0;
474			if (check_if_fs_block(ctx, s->block)) {
475				file_ok = 0;
476				meta_data = 1;
477			}
478
479			/*
480			 * Add all inodes used by this block to the
481			 * shared[] --- which is a unique list, so
482			 * if an inode is already in shared[], don't
483			 * add it again.
484			 */
485			for (r = q->inode_list; r; r = r->next) {
486				if (r->inode == ino)
487					continue;
488				for (i = 0; i < shared_len; i++)
489					if (shared[i] == r->inode)
490						break;
491				if (i == shared_len) {
492					shared[shared_len++] = r->inode;
493				}
494			}
495		}
496
497		/*
498		 * Report the inode that we are working on
499		 */
500		pctx.inode = &p->inode;
501		pctx.ino = ino;
502		pctx.dir = p->dir;
503		pctx.blkcount = p->num_dupblocks;
504		pctx.num = meta_data ? shared_len+1 : shared_len;
505		fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
506		pctx.blkcount = 0;
507		pctx.num = 0;
508
509		if (meta_data)
510			fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
511
512		for (i = 0; i < shared_len; i++) {
513			m = dict_lookup(&ino_dict, INT_TO_VOIDPTR(shared[i]));
514			if (!m)
515				continue; /* should never happen */
516			t = (struct dup_inode *) dnode_get(m);
517			/*
518			 * Report the inode that we are sharing with
519			 */
520			pctx.inode = &t->inode;
521			pctx.ino = shared[i];
522			pctx.dir = t->dir;
523			fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
524		}
525		if (file_ok) {
526			fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
527			continue;
528		}
529		if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
530			pctx.errcode = clone_file(ctx, ino, p, block_buf);
531			if (pctx.errcode)
532				fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
533			else
534				continue;
535		}
536		if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
537			delete_file(ctx, ino, p, block_buf);
538		else
539			ext2fs_unmark_valid(fs);
540	}
541	ext2fs_free_mem(&shared);
542}
543
544/*
545 * Drop the refcount on the dup_block structure, and clear the entry
546 * in the block_dup_map if appropriate.
547 */
548static void decrement_badcount(e2fsck_t ctx, blk_t block, struct dup_block *p)
549{
550	p->num_bad--;
551	if (p->num_bad <= 0 ||
552	    (p->num_bad == 1 && !check_if_fs_block(ctx, block)))
553		ext2fs_unmark_block_bitmap(ctx->block_dup_map, block);
554}
555
556static int delete_file_block(ext2_filsys fs,
557			     blk_t	*block_nr,
558			     e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
559			     blk_t ref_block EXT2FS_ATTR((unused)),
560			     int ref_offset EXT2FS_ATTR((unused)),
561			     void *priv_data)
562{
563	struct process_block_struct *pb;
564	struct dup_block *p;
565	dnode_t	*n;
566	e2fsck_t ctx;
567
568	pb = (struct process_block_struct *) priv_data;
569	ctx = pb->ctx;
570
571	if (HOLE_BLKADDR(*block_nr))
572		return 0;
573
574	if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
575		n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(*block_nr));
576		if (n) {
577			p = (struct dup_block *) dnode_get(n);
578			decrement_badcount(ctx, *block_nr, p);
579		} else
580			com_err("delete_file_block", 0,
581			    _("internal error: can't find dup_blk for %u\n"),
582				*block_nr);
583	} else {
584		ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
585		ext2fs_block_alloc_stats(fs, *block_nr, -1);
586	}
587
588	return 0;
589}
590
591static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
592			struct dup_inode *dp, char* block_buf)
593{
594	ext2_filsys fs = ctx->fs;
595	struct process_block_struct pb;
596	struct ext2_inode	inode;
597	struct problem_context	pctx;
598	unsigned int		count;
599
600	clear_problem_context(&pctx);
601	pctx.ino = pb.ino = ino;
602	pb.dup_blocks = dp->num_dupblocks;
603	pb.ctx = ctx;
604	pctx.str = "delete_file";
605
606	e2fsck_read_inode(ctx, ino, &inode, "delete_file");
607	if (ext2fs_inode_has_valid_blocks(&inode))
608		pctx.errcode = ext2fs_block_iterate2(fs, ino, BLOCK_FLAG_READ_ONLY,
609						     block_buf, delete_file_block, &pb);
610	if (pctx.errcode)
611		fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
612	if (ctx->inode_bad_map)
613		ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
614	ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
615
616	/* Inode may have changed by block_iterate, so reread it */
617	e2fsck_read_inode(ctx, ino, &inode, "delete_file");
618	e2fsck_clear_inode(ctx, ino, &inode, 0, "delete_file");
619	if (inode.i_file_acl &&
620	    (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
621		count = 1;
622		pctx.errcode = ext2fs_adjust_ea_refcount(fs, inode.i_file_acl,
623						   block_buf, -1, &count);
624		if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
625			pctx.errcode = 0;
626			count = 1;
627		}
628		if (pctx.errcode) {
629			pctx.blk = inode.i_file_acl;
630			fix_problem(ctx, PR_1B_ADJ_EA_REFCOUNT, &pctx);
631		}
632		/*
633		 * If the count is zero, then arrange to have the
634		 * block deleted.  If the block is in the block_dup_map,
635		 * also call delete_file_block since it will take care
636		 * of keeping the accounting straight.
637		 */
638		if ((count == 0) ||
639		    ext2fs_test_block_bitmap(ctx->block_dup_map,
640					     inode.i_file_acl))
641			delete_file_block(fs, &inode.i_file_acl,
642					  BLOCK_COUNT_EXTATTR, 0, 0, &pb);
643	}
644}
645
646struct clone_struct {
647	errcode_t	errcode;
648	ext2_ino_t	dir;
649	char	*buf;
650	e2fsck_t ctx;
651};
652
653static int clone_file_block(ext2_filsys fs,
654			    blk_t	*block_nr,
655			    e2_blkcnt_t blockcnt,
656			    blk_t ref_block EXT2FS_ATTR((unused)),
657			    int ref_offset EXT2FS_ATTR((unused)),
658			    void *priv_data)
659{
660	struct dup_block *p;
661	blk_t	new_block;
662	errcode_t	retval;
663	struct clone_struct *cs = (struct clone_struct *) priv_data;
664	dnode_t *n;
665	e2fsck_t ctx;
666
667	ctx = cs->ctx;
668
669	if (HOLE_BLKADDR(*block_nr))
670		return 0;
671
672	if (ext2fs_test_block_bitmap(ctx->block_dup_map, *block_nr)) {
673		n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(*block_nr));
674		if (n) {
675			p = (struct dup_block *) dnode_get(n);
676			retval = ext2fs_new_block(fs, 0, ctx->block_found_map,
677						  &new_block);
678			if (retval) {
679				cs->errcode = retval;
680				return BLOCK_ABORT;
681			}
682			if (cs->dir && (blockcnt >= 0)) {
683				retval = ext2fs_set_dir_block(fs->dblist,
684				      cs->dir, new_block, blockcnt);
685				if (retval) {
686					cs->errcode = retval;
687					return BLOCK_ABORT;
688				}
689			}
690#if 0
691			printf("Cloning block %u to %u\n", *block_nr,
692			       new_block);
693#endif
694			retval = io_channel_read_blk(fs->io, *block_nr, 1,
695						     cs->buf);
696			if (retval) {
697				cs->errcode = retval;
698				return BLOCK_ABORT;
699			}
700			retval = io_channel_write_blk(fs->io, new_block, 1,
701						      cs->buf);
702			if (retval) {
703				cs->errcode = retval;
704				return BLOCK_ABORT;
705			}
706			decrement_badcount(ctx, *block_nr, p);
707			*block_nr = new_block;
708			ext2fs_mark_block_bitmap(ctx->block_found_map,
709						 new_block);
710			ext2fs_mark_block_bitmap(fs->block_map, new_block);
711			return BLOCK_CHANGED;
712		} else
713			com_err("clone_file_block", 0,
714			    _("internal error: can't find dup_blk for %u\n"),
715				*block_nr);
716	}
717	return 0;
718}
719
720static int clone_file(e2fsck_t ctx, ext2_ino_t ino,
721		      struct dup_inode *dp, char* block_buf)
722{
723	ext2_filsys fs = ctx->fs;
724	errcode_t	retval;
725	struct clone_struct cs;
726	struct problem_context	pctx;
727	blk_t		blk;
728	dnode_t		*n;
729	struct inode_el	*ino_el;
730	struct dup_block	*db;
731	struct dup_inode	*di;
732
733	clear_problem_context(&pctx);
734	cs.errcode = 0;
735	cs.dir = 0;
736	cs.ctx = ctx;
737	retval = ext2fs_get_mem(fs->blocksize, &cs.buf);
738	if (retval)
739		return retval;
740
741	if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, ino))
742		cs.dir = ino;
743
744	pctx.ino = ino;
745	pctx.str = "clone_file";
746	if (ext2fs_inode_has_valid_blocks(&dp->inode))
747		pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
748						     clone_file_block, &cs);
749	ext2fs_mark_bb_dirty(fs);
750	if (pctx.errcode) {
751		fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
752		retval = pctx.errcode;
753		goto errout;
754	}
755	if (cs.errcode) {
756		com_err("clone_file", cs.errcode,
757			_("returned from clone_file_block"));
758		retval = cs.errcode;
759		goto errout;
760	}
761	/* The inode may have changed on disk, so we have to re-read it */
762	e2fsck_read_inode(ctx, ino, &dp->inode, "clone file EA");
763	blk = dp->inode.i_file_acl;
764	if (blk && (clone_file_block(fs, &dp->inode.i_file_acl,
765				     BLOCK_COUNT_EXTATTR, 0, 0, &cs) ==
766		    BLOCK_CHANGED)) {
767		e2fsck_write_inode(ctx, ino, &dp->inode, "clone file EA");
768		/*
769		 * If we cloned the EA block, find all other inodes
770		 * which refered to that EA block, and modify
771		 * them to point to the new EA block.
772		 */
773		n = dict_lookup(&blk_dict, INT_TO_VOIDPTR(blk));
774		if (!n) {
775			com_err("clone_file", 0,
776				_("internal error: couldn't lookup EA "
777				  "block record for %u"), blk);
778			retval = 0; /* OK to stumble on... */
779			goto errout;
780		}
781		db = (struct dup_block *) dnode_get(n);
782		for (ino_el = db->inode_list; ino_el; ino_el = ino_el->next) {
783			if (ino_el->inode == ino)
784				continue;
785			n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino_el->inode));
786			if (!n) {
787				com_err("clone_file", 0,
788					_("internal error: couldn't lookup EA "
789					  "inode record for %u"),
790					ino_el->inode);
791				retval = 0; /* OK to stumble on... */
792				goto errout;
793			}
794			di = (struct dup_inode *) dnode_get(n);
795			if (di->inode.i_file_acl == blk) {
796				di->inode.i_file_acl = dp->inode.i_file_acl;
797				e2fsck_write_inode(ctx, ino_el->inode,
798					   &di->inode, "clone file EA");
799				decrement_badcount(ctx, blk, db);
800			}
801		}
802	}
803	retval = 0;
804errout:
805	ext2fs_free_mem(&cs.buf);
806	return retval;
807}
808
809/*
810 * This routine returns 1 if a block overlaps with one of the superblocks,
811 * group descriptors, inode bitmaps, or block bitmaps.
812 */
813static int check_if_fs_block(e2fsck_t ctx, blk_t test_block)
814{
815	ext2_filsys fs = ctx->fs;
816	blk_t	first_block;
817	dgrp_t	i;
818
819	first_block = fs->super->s_first_data_block;
820	for (i = 0; i < fs->group_desc_count; i++) {
821
822		/* Check superblocks/block group descriptors */
823		if (ext2fs_bg_has_super(fs, i)) {
824			if (test_block >= first_block &&
825			    (test_block <= first_block + fs->desc_blocks))
826				return 1;
827		}
828
829		/* Check the inode table */
830		if ((fs->group_desc[i].bg_inode_table) &&
831		    (test_block >= fs->group_desc[i].bg_inode_table) &&
832		    (test_block < (fs->group_desc[i].bg_inode_table +
833				   fs->inode_blocks_per_group)))
834			return 1;
835
836		/* Check the bitmap blocks */
837		if ((test_block == fs->group_desc[i].bg_block_bitmap) ||
838		    (test_block == fs->group_desc[i].bg_inode_bitmap))
839			return 1;
840
841		first_block += fs->super->s_blocks_per_group;
842	}
843	return 0;
844}
845