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