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