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