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