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