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