Whamcloud - gitweb
Merge branch 'maint' into next
[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;
265         struct ext2_inode *inode;
266         struct problem_context *pctx;
267 };
268
269 static void pass1b(e2fsck_t ctx, char *block_buf)
270 {
271         ext2_filsys fs = ctx->fs;
272         ext2_ino_t ino = 0;
273         struct ext2_inode inode;
274         ext2_inode_scan scan;
275         struct process_block_struct pb;
276         struct problem_context pctx;
277
278         clear_problem_context(&pctx);
279
280         if (!(ctx->options & E2F_OPT_PREEN))
281                 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
282         pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
283                                               &scan);
284         if (pctx.errcode) {
285                 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
286                 ctx->flags |= E2F_FLAG_ABORT;
287                 return;
288         }
289         ctx->stashed_inode = &inode;
290         pb.ctx = ctx;
291         pb.pctx = &pctx;
292         pctx.str = "pass1b";
293         while (1) {
294                 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
295                         if (e2fsck_mmp_update(fs))
296                                 fatal_error(ctx, 0);
297                 }
298                 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
299                 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
300                         continue;
301                 if (pctx.errcode) {
302                         fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
303                         ctx->flags |= E2F_FLAG_ABORT;
304                         return;
305                 }
306                 if (!ino)
307                         break;
308                 pctx.ino = ctx->stashed_ino = ino;
309                 if ((ino != EXT2_BAD_INO) &&
310                     !ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino))
311                         continue;
312
313                 pb.ino = ino;
314                 pb.dup_blocks = 0;
315                 pb.inode = &inode;
316                 pb.cur_cluster = ~0;
317
318                 if (ext2fs_inode_has_valid_blocks2(fs, &inode) ||
319                     (ino == EXT2_BAD_INO))
320                         pctx.errcode = ext2fs_block_iterate3(fs, ino,
321                                              BLOCK_FLAG_READ_ONLY, block_buf,
322                                              process_pass1b_block, &pb);
323                 /* If the feature is not set, attrs will be cleared later anyway */
324                 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) &&
325                     ext2fs_file_acl_block(fs, &inode)) {
326                         blk64_t blk = ext2fs_file_acl_block(fs, &inode);
327                         process_pass1b_block(fs, &blk,
328                                              BLOCK_COUNT_EXTATTR, 0, 0, &pb);
329                         ext2fs_file_acl_block_set(fs, &inode, blk);
330                 }
331                 if (pb.dup_blocks) {
332                         end_problem_latch(ctx, PR_LATCH_DBLOCK);
333                         if (ino >= EXT2_FIRST_INODE(fs->super) ||
334                             ino == EXT2_ROOT_INO)
335                                 dup_inode_count++;
336                 }
337                 if (pctx.errcode)
338                         fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
339         }
340         ext2fs_close_inode_scan(scan);
341         e2fsck_use_inode_shortcuts(ctx, 0);
342 }
343
344 static int process_pass1b_block(ext2_filsys fs EXT2FS_ATTR((unused)),
345                                 blk64_t *block_nr,
346                                 e2_blkcnt_t blockcnt,
347                                 blk64_t ref_blk EXT2FS_ATTR((unused)),
348                                 int ref_offset EXT2FS_ATTR((unused)),
349                                 void *priv_data)
350 {
351         struct process_block_struct *p;
352         e2fsck_t ctx;
353         blk64_t lc;
354
355         if (HOLE_BLKADDR(*block_nr))
356                 return 0;
357         p = (struct process_block_struct *) priv_data;
358         ctx = p->ctx;
359         lc = EXT2FS_B2C(fs, blockcnt);
360
361         if (!ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr))
362                 goto finish;
363
364         /* OK, this is a duplicate block */
365         if (p->ino != EXT2_BAD_INO) {
366                 p->pctx->blk = *block_nr;
367                 fix_problem(ctx, PR_1B_DUP_BLOCK, p->pctx);
368         }
369         p->dup_blocks++;
370         ext2fs_mark_inode_bitmap2(inode_dup_map, p->ino);
371
372         if (lc != p->cur_cluster)
373                 add_dupe(ctx, p->ino, EXT2FS_B2C(fs, *block_nr), p->inode);
374
375 finish:
376         p->cur_cluster = lc;
377         return 0;
378 }
379
380 /*
381  * Pass 1c: Scan directories for inodes with duplicate blocks.  This
382  * is used so that we can print pathnames when prompting the user for
383  * what to do.
384  */
385 struct search_dir_struct {
386         int             count;
387         ext2_ino_t      first_inode;
388         ext2_ino_t      max_inode;
389 };
390
391 static int search_dirent_proc(ext2_ino_t dir, int entry,
392                               struct ext2_dir_entry *dirent,
393                               int offset EXT2FS_ATTR((unused)),
394                               int blocksize EXT2FS_ATTR((unused)),
395                               char *buf EXT2FS_ATTR((unused)),
396                               void *priv_data)
397 {
398         struct search_dir_struct *sd;
399         struct dup_inode        *p;
400         dnode_t                 *n;
401
402         sd = (struct search_dir_struct *) priv_data;
403
404         if (dirent->inode > sd->max_inode)
405                 /* Should abort this inode, but not everything */
406                 return 0;
407
408         if ((dirent->inode < sd->first_inode) || (entry < DIRENT_OTHER_FILE) ||
409             !ext2fs_test_inode_bitmap2(inode_dup_map, dirent->inode))
410                 return 0;
411
412         n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(dirent->inode));
413         if (!n)
414                 return 0;
415         p = (struct dup_inode *) dnode_get(n);
416         if (!p->dir) {
417                 p->dir = dir;
418                 sd->count--;
419         }
420
421         return(sd->count ? 0 : DIRENT_ABORT);
422 }
423
424
425 static void pass1c(e2fsck_t ctx, char *block_buf)
426 {
427         ext2_filsys fs = ctx->fs;
428         struct search_dir_struct sd;
429         struct problem_context pctx;
430
431         clear_problem_context(&pctx);
432
433         if (!(ctx->options & E2F_OPT_PREEN))
434                 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
435
436         /*
437          * Search through all directories to translate inodes to names
438          * (by searching for the containing directory for that inode.)
439          */
440         sd.count = dup_inode_count - dup_inode_founddir;
441         sd.first_inode = EXT2_FIRST_INODE(fs->super);
442         sd.max_inode = fs->super->s_inodes_count;
443         ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
444                                   search_dirent_proc, &sd);
445 }
446
447 static void pass1d(e2fsck_t ctx, char *block_buf)
448 {
449         ext2_filsys fs = ctx->fs;
450         struct dup_inode        *p, *t;
451         struct dup_cluster      *q;
452         ext2_ino_t              *shared, ino;
453         int     shared_len;
454         int     i;
455         int     file_ok;
456         int     meta_data = 0;
457         struct problem_context pctx;
458         dnode_t *n, *m;
459         struct cluster_el       *s;
460         struct inode_el *r;
461
462         clear_problem_context(&pctx);
463
464         if (!(ctx->options & E2F_OPT_PREEN))
465                 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
466         e2fsck_read_bitmaps(ctx);
467
468         pctx.num = dup_inode_count; /* dict_count(&ino_dict); */
469         fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
470         shared = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
471                                 sizeof(ext2_ino_t) * dict_count(&ino_dict),
472                                 "Shared inode list");
473         for (n = dict_first(&ino_dict); n; n = dict_next(&ino_dict, n)) {
474                 p = (struct dup_inode *) dnode_get(n);
475                 shared_len = 0;
476                 file_ok = 1;
477                 ino = (ext2_ino_t)VOIDPTR_TO_INT(dnode_getkey(n));
478                 if (ino == EXT2_BAD_INO || ino == EXT2_RESIZE_INO)
479                         continue;
480
481                 /*
482                  * Find all of the inodes which share blocks with this
483                  * one.  First we find all of the duplicate blocks
484                  * belonging to this inode, and then search each block
485                  * get the list of inodes, and merge them together.
486                  */
487                 for (s = p->cluster_list; s; s = s->next) {
488                         m = dict_lookup(&clstr_dict,
489                                         INT_TO_VOIDPTR(s->cluster));
490                         if (!m)
491                                 continue; /* Should never happen... */
492                         q = (struct dup_cluster *) dnode_get(m);
493                         if (q->num_bad > 1)
494                                 file_ok = 0;
495                         if (check_if_fs_cluster(ctx, s->cluster)) {
496                                 file_ok = 0;
497                                 meta_data = 1;
498                         }
499
500                         /*
501                          * Add all inodes used by this block to the
502                          * shared[] --- which is a unique list, so
503                          * if an inode is already in shared[], don't
504                          * add it again.
505                          */
506                         for (r = q->inode_list; r; r = r->next) {
507                                 if (r->inode == ino)
508                                         continue;
509                                 for (i = 0; i < shared_len; i++)
510                                         if (shared[i] == r->inode)
511                                                 break;
512                                 if (i == shared_len) {
513                                         shared[shared_len++] = r->inode;
514                                 }
515                         }
516                 }
517
518                 /*
519                  * Report the inode that we are working on
520                  */
521                 pctx.inode = &p->inode;
522                 pctx.ino = ino;
523                 pctx.dir = p->dir;
524                 pctx.blkcount = p->num_dupblocks;
525                 pctx.num = meta_data ? shared_len+1 : shared_len;
526                 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
527                 pctx.blkcount = 0;
528                 pctx.num = 0;
529
530                 if (meta_data)
531                         fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
532
533                 for (i = 0; i < shared_len; i++) {
534                         m = dict_lookup(&ino_dict, INT_TO_VOIDPTR(shared[i]));
535                         if (!m)
536                                 continue; /* should never happen */
537                         t = (struct dup_inode *) dnode_get(m);
538                         /*
539                          * Report the inode that we are sharing with
540                          */
541                         pctx.inode = &t->inode;
542                         pctx.ino = shared[i];
543                         pctx.dir = t->dir;
544                         fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
545                 }
546                 if (file_ok) {
547                         fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
548                         continue;
549                 }
550                 if (fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx)) {
551                         pctx.errcode = clone_file(ctx, ino, p, block_buf);
552                         if (pctx.errcode)
553                                 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
554                         else
555                                 continue;
556                 }
557                 if (fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
558                         delete_file(ctx, ino, p, block_buf);
559                 else
560                         ext2fs_unmark_valid(fs);
561         }
562         ext2fs_free_mem(&shared);
563 }
564
565 /*
566  * Drop the refcount on the dup_block structure, and clear the entry
567  * in the block_dup_map if appropriate.
568  */
569 static void decrement_badcount(e2fsck_t ctx, blk64_t block,
570                                struct dup_cluster *p)
571 {
572         p->num_bad--;
573         if (p->num_bad <= 0 ||
574             (p->num_bad == 1 && !check_if_fs_block(ctx, block))) {
575                 if (check_if_fs_cluster(ctx, EXT2FS_B2C(ctx->fs, block)))
576                         return;
577                 ext2fs_unmark_block_bitmap2(ctx->block_dup_map, block);
578         }
579 }
580
581 static int delete_file_block(ext2_filsys fs,
582                              blk64_t    *block_nr,
583                              e2_blkcnt_t blockcnt,
584                              blk64_t ref_block EXT2FS_ATTR((unused)),
585                              int ref_offset EXT2FS_ATTR((unused)),
586                              void *priv_data)
587 {
588         struct process_block_struct *pb;
589         struct dup_cluster *p;
590         dnode_t *n;
591         e2fsck_t ctx;
592         blk64_t c, lc;
593
594         pb = (struct process_block_struct *) priv_data;
595         ctx = pb->ctx;
596
597         if (HOLE_BLKADDR(*block_nr))
598                 return 0;
599
600         c = EXT2FS_B2C(fs, *block_nr);
601         lc = EXT2FS_B2C(fs, blockcnt);
602         if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
603                 n = dict_lookup(&clstr_dict, INT_TO_VOIDPTR(c));
604                 if (n) {
605                         p = (struct dup_cluster *) dnode_get(n);
606                         if (lc != pb->cur_cluster)
607                                 decrement_badcount(ctx, *block_nr, p);
608                 } else
609                         com_err("delete_file_block", 0,
610                             _("internal error: can't find dup_blk for %llu\n"),
611                                 *block_nr);
612         } else {
613                 ext2fs_unmark_block_bitmap2(ctx->block_found_map, *block_nr);
614                 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
615                 pb->dup_blocks++;
616         }
617         pb->cur_cluster = lc;
618
619         return 0;
620 }
621
622 static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
623                         struct dup_inode *dp, char* block_buf)
624 {
625         ext2_filsys fs = ctx->fs;
626         struct process_block_struct pb;
627         struct problem_context  pctx;
628         unsigned int            count;
629
630         clear_problem_context(&pctx);
631         pctx.ino = pb.ino = ino;
632         pb.dup_blocks = 0;
633         pb.ctx = ctx;
634         pctx.str = "delete_file";
635         pb.cur_cluster = ~0;
636
637         if (ext2fs_inode_has_valid_blocks2(fs, &dp->inode))
638                 pctx.errcode = ext2fs_block_iterate3(fs, ino,
639                                                      BLOCK_FLAG_READ_ONLY,
640                                                      block_buf,
641                                                      delete_file_block, &pb);
642         if (pctx.errcode)
643                 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
644         if (ctx->inode_bad_map)
645                 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
646         ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(dp->inode.i_mode));
647         quota_data_sub(ctx->qctx, &dp->inode, ino,
648                        pb.dup_blocks * fs->blocksize);
649         quota_data_inodes(ctx->qctx, &dp->inode, ino, -1);
650
651         /* Inode may have changed by block_iterate, so reread it */
652         e2fsck_read_inode(ctx, ino, &dp->inode, "delete_file");
653         e2fsck_clear_inode(ctx, ino, &dp->inode, 0, "delete_file");
654         if (ext2fs_file_acl_block(fs, &dp->inode) &&
655             (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
656                 count = 1;
657                 pctx.errcode = ext2fs_adjust_ea_refcount3(fs,
658                                         ext2fs_file_acl_block(fs, &dp->inode),
659                                         block_buf, -1, &count, ino);
660                 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
661                         pctx.errcode = 0;
662                         count = 1;
663                 }
664                 if (pctx.errcode) {
665                         pctx.blk = ext2fs_file_acl_block(fs, &dp->inode);
666                         fix_problem(ctx, PR_1B_ADJ_EA_REFCOUNT, &pctx);
667                 }
668                 /*
669                  * If the count is zero, then arrange to have the
670                  * block deleted.  If the block is in the block_dup_map,
671                  * also call delete_file_block since it will take care
672                  * of keeping the accounting straight.
673                  */
674                 if ((count == 0) ||
675                     ext2fs_test_block_bitmap2(ctx->block_dup_map,
676                                         ext2fs_file_acl_block(fs, &dp->inode))) {
677                         blk64_t blk = ext2fs_file_acl_block(fs, &dp->inode);
678                         delete_file_block(fs, &blk,
679                                           BLOCK_COUNT_EXTATTR, 0, 0, &pb);
680                         ext2fs_file_acl_block_set(fs, &dp->inode, blk);
681                         quota_data_sub(ctx->qctx, &dp->inode, ino, fs->blocksize);
682                 }
683         }
684 }
685
686 struct clone_struct {
687         errcode_t       errcode;
688         blk64_t         dup_cluster;
689         blk64_t         alloc_block;
690         ext2_ino_t      dir;
691         char    *buf;
692         e2fsck_t ctx;
693 };
694
695 static int clone_file_block(ext2_filsys fs,
696                             blk64_t     *block_nr,
697                             e2_blkcnt_t blockcnt,
698                             blk64_t ref_block EXT2FS_ATTR((unused)),
699                             int ref_offset EXT2FS_ATTR((unused)),
700                             void *priv_data)
701 {
702         struct dup_cluster *p;
703         blk64_t new_block;
704         errcode_t       retval;
705         struct clone_struct *cs = (struct clone_struct *) priv_data;
706         dnode_t *n;
707         e2fsck_t ctx;
708         blk64_t c;
709         int is_meta = 0;
710
711         ctx = cs->ctx;
712
713         if (HOLE_BLKADDR(*block_nr))
714                 return 0;
715
716         c = EXT2FS_B2C(fs, blockcnt);
717         if (check_if_fs_cluster(ctx, EXT2FS_B2C(fs, *block_nr)))
718                 is_meta = 1;
719
720         if (c == cs->dup_cluster && cs->alloc_block) {
721                 new_block = cs->alloc_block;
722                 goto got_block;
723         }
724
725         if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
726                 n = dict_lookup(&clstr_dict,
727                                 INT_TO_VOIDPTR(EXT2FS_B2C(fs, *block_nr)));
728                 if (!n) {
729                         com_err("clone_file_block", 0,
730                             _("internal error: can't find dup_blk for %llu\n"),
731                                 *block_nr);
732                         return 0;
733                 }
734
735                 p = (struct dup_cluster *) dnode_get(n);
736                 if (!is_meta)
737                         decrement_badcount(ctx, *block_nr, p);
738
739                 cs->dup_cluster = c;
740
741                 retval = ext2fs_new_block2(fs, 0, ctx->block_found_map,
742                                            &new_block);
743                 if (retval) {
744                         cs->errcode = retval;
745                         return BLOCK_ABORT;
746                 }
747                 cs->alloc_block = new_block;
748
749         got_block:
750                 new_block &= ~EXT2FS_CLUSTER_MASK(fs);
751                 new_block += EXT2FS_CLUSTER_MASK(fs) & blockcnt;
752                 if (cs->dir && (blockcnt >= 0)) {
753                         retval = ext2fs_set_dir_block2(fs->dblist,
754                                         cs->dir, new_block, blockcnt);
755                         if (retval) {
756                                 cs->errcode = retval;
757                                 return BLOCK_ABORT;
758                         }
759                 }
760 #if 0
761                 printf("Cloning block #%lld from %llu to %llu\n",
762                        blockcnt, *block_nr, new_block);
763 #endif
764                 retval = io_channel_read_blk64(fs->io, *block_nr, 1, cs->buf);
765                 if (retval) {
766                         cs->errcode = retval;
767                         return BLOCK_ABORT;
768                 }
769                 retval = io_channel_write_blk64(fs->io, new_block, 1, cs->buf);
770                 if (retval) {
771                         cs->errcode = retval;
772                         return BLOCK_ABORT;
773                 }
774                 *block_nr = new_block;
775                 ext2fs_mark_block_bitmap2(ctx->block_found_map, new_block);
776                 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
777                 return BLOCK_CHANGED;
778         }
779         return 0;
780 }
781
782 static errcode_t clone_file(e2fsck_t ctx, ext2_ino_t ino,
783                             struct dup_inode *dp, char* block_buf)
784 {
785         ext2_filsys fs = ctx->fs;
786         errcode_t       retval;
787         struct clone_struct cs;
788         struct problem_context  pctx;
789         blk64_t         blk, new_blk;
790         dnode_t         *n;
791         struct inode_el *ino_el;
792         struct dup_cluster      *dc;
793         struct dup_inode        *di;
794
795         clear_problem_context(&pctx);
796         cs.errcode = 0;
797         cs.dir = 0;
798         cs.dup_cluster = ~0;
799         cs.alloc_block = 0;
800         cs.ctx = ctx;
801         retval = ext2fs_get_mem(fs->blocksize, &cs.buf);
802         if (retval)
803                 return retval;
804
805         if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, ino))
806                 cs.dir = ino;
807
808         pctx.ino = ino;
809         pctx.str = "clone_file";
810         if (ext2fs_inode_has_valid_blocks2(fs, &dp->inode))
811                 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
812                                                      clone_file_block, &cs);
813         ext2fs_mark_bb_dirty(fs);
814         if (pctx.errcode) {
815                 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
816                 retval = pctx.errcode;
817                 goto errout;
818         }
819         if (cs.errcode) {
820                 com_err("clone_file", cs.errcode,
821                         _("returned from clone_file_block"));
822                 retval = cs.errcode;
823                 goto errout;
824         }
825         /* The inode may have changed on disk, so we have to re-read it */
826         e2fsck_read_inode(ctx, ino, &dp->inode, "clone file EA");
827         blk = ext2fs_file_acl_block(fs, &dp->inode);
828         new_blk = blk;
829         if (blk && (clone_file_block(fs, &new_blk,
830                                      BLOCK_COUNT_EXTATTR, 0, 0, &cs) ==
831                     BLOCK_CHANGED)) {
832                 ext2fs_file_acl_block_set(fs, &dp->inode, new_blk);
833                 e2fsck_write_inode(ctx, ino, &dp->inode, "clone file EA");
834                 /*
835                  * If we cloned the EA block, find all other inodes
836                  * which refered to that EA block, and modify
837                  * them to point to the new EA block.
838                  */
839                 n = dict_lookup(&clstr_dict,
840                                 INT_TO_VOIDPTR(EXT2FS_B2C(fs, blk)));
841                 if (!n) {
842                         com_err("clone_file", 0,
843                                 _("internal error: couldn't lookup EA "
844                                   "block record for %llu"), blk);
845                         retval = 0; /* OK to stumble on... */
846                         goto errout;
847                 }
848                 dc = (struct dup_cluster *) dnode_get(n);
849                 for (ino_el = dc->inode_list; ino_el; ino_el = ino_el->next) {
850                         if (ino_el->inode == ino)
851                                 continue;
852                         n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino_el->inode));
853                         if (!n) {
854                                 com_err("clone_file", 0,
855                                         _("internal error: couldn't lookup EA "
856                                           "inode record for %u"),
857                                         ino_el->inode);
858                                 retval = 0; /* OK to stumble on... */
859                                 goto errout;
860                         }
861                         di = (struct dup_inode *) dnode_get(n);
862                         if (ext2fs_file_acl_block(fs, &di->inode) == blk) {
863                                 ext2fs_file_acl_block_set(fs, &di->inode,
864                                         ext2fs_file_acl_block(fs, &dp->inode));
865                                 e2fsck_write_inode(ctx, ino_el->inode,
866                                            &di->inode, "clone file EA");
867                                 decrement_badcount(ctx, blk, dc);
868                         }
869                 }
870         }
871         retval = 0;
872 errout:
873         ext2fs_free_mem(&cs.buf);
874         return retval;
875 }
876
877 /*
878  * This routine returns 1 if a block overlaps with one of the superblocks,
879  * group descriptors, inode bitmaps, or block bitmaps.
880  */
881 static int check_if_fs_block(e2fsck_t ctx, blk64_t test_block)
882 {
883         ext2_filsys fs = ctx->fs;
884         blk64_t first_block;
885         dgrp_t  i;
886
887         first_block = fs->super->s_first_data_block;
888         for (i = 0; i < fs->group_desc_count; i++) {
889
890                 /* Check superblocks/block group descriptors */
891                 if (ext2fs_bg_has_super(fs, i)) {
892                         if (test_block >= first_block &&
893                             (test_block <= first_block + fs->desc_blocks))
894                                 return 1;
895                 }
896
897                 /* Check the inode table */
898                 if ((ext2fs_inode_table_loc(fs, i)) &&
899                     (test_block >= ext2fs_inode_table_loc(fs, i)) &&
900                     (test_block < (ext2fs_inode_table_loc(fs, i) +
901                                    fs->inode_blocks_per_group)))
902                         return 1;
903
904                 /* Check the bitmap blocks */
905                 if ((test_block == ext2fs_block_bitmap_loc(fs, i)) ||
906                     (test_block == ext2fs_inode_bitmap_loc(fs, i)))
907                         return 1;
908
909                 first_block += fs->super->s_blocks_per_group;
910         }
911         return 0;
912 }
913
914 /*
915  * This routine returns 1 if a cluster overlaps with one of the superblocks,
916  * group descriptors, inode bitmaps, or block bitmaps.
917  */
918 static int check_if_fs_cluster(e2fsck_t ctx, blk64_t cluster)
919 {
920         ext2_filsys fs = ctx->fs;
921         blk64_t first_block;
922         dgrp_t  i;
923
924         first_block = fs->super->s_first_data_block;
925         for (i = 0; i < fs->group_desc_count; i++) {
926
927                 /* Check superblocks/block group descriptors */
928                 if (ext2fs_bg_has_super(fs, i)) {
929                         if (cluster >= EXT2FS_B2C(fs, first_block) &&
930                             (cluster <= EXT2FS_B2C(fs, first_block +
931                                                    fs->desc_blocks)))
932                                 return 1;
933                 }
934
935                 /* Check the inode table */
936                 if ((ext2fs_inode_table_loc(fs, i)) &&
937                     (cluster >= EXT2FS_B2C(fs,
938                                            ext2fs_inode_table_loc(fs, i))) &&
939                     (cluster <= EXT2FS_B2C(fs,
940                                            ext2fs_inode_table_loc(fs, i) +
941                                            fs->inode_blocks_per_group - 1)))
942                         return 1;
943
944                 /* Check the bitmap blocks */
945                 if ((cluster == EXT2FS_B2C(fs,
946                                            ext2fs_block_bitmap_loc(fs, i))) ||
947                     (cluster == EXT2FS_B2C(fs,
948                                            ext2fs_inode_bitmap_loc(fs, i))))
949                         return 1;
950
951                 first_block += fs->super->s_blocks_per_group;
952         }
953         return 0;
954 }