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