Whamcloud - gitweb
LU-12403 build: add support for RHEL/CentOS 8
[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 "support/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_large 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_large *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         if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
249             (ctx->options & E2F_OPT_UNSHARE_BLOCKS)) {
250                 /*
251                  * If we successfully managed to unshare all blocks, unset the
252                  * shared block feature.
253                  */
254                 blk64_t next;
255                 int result = ext2fs_find_first_set_block_bitmap2(
256                         ctx->block_dup_map,
257                         ctx->fs->super->s_first_data_block,
258                         ext2fs_blocks_count(ctx->fs->super) - 1,
259                         &next);
260                 if (result == ENOENT && !(ctx->options & E2F_OPT_NO)) {
261                         ext2fs_clear_feature_shared_blocks(ctx->fs->super);
262                         ext2fs_mark_super_dirty(ctx->fs);
263                 }
264         }
265
266         /*
267          * Time to free all of the accumulated data structures that we
268          * don't need anymore.
269          */
270         dict_free_nodes(&ino_dict);
271         dict_free_nodes(&clstr_dict);
272         ext2fs_free_inode_bitmap(inode_dup_map);
273 }
274
275 /*
276  * Scan the inodes looking for inodes that contain duplicate blocks.
277  */
278 struct process_block_struct {
279         e2fsck_t        ctx;
280         ext2_ino_t      ino;
281         int             dup_blocks;
282         blk64_t         cur_cluster, phys_cluster;
283         blk64_t         last_blk;
284         struct ext2_inode_large *inode;
285         struct problem_context *pctx;
286 };
287
288 static void pass1b(e2fsck_t ctx, char *block_buf)
289 {
290         ext2_filsys fs = ctx->fs;
291         ext2_ino_t ino = 0;
292         struct ext2_inode_large inode;
293         ext2_inode_scan scan;
294         struct process_block_struct pb;
295         struct problem_context pctx;
296         problem_t op;
297
298         clear_problem_context(&pctx);
299
300         if (!(ctx->options & E2F_OPT_PREEN))
301                 fix_problem(ctx, PR_1B_PASS_HEADER, &pctx);
302         pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
303                                               &scan);
304         if (pctx.errcode) {
305                 fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
306                 ctx->flags |= E2F_FLAG_ABORT;
307                 return;
308         }
309         ctx->stashed_inode = EXT2_INODE(&inode);
310         pb.ctx = ctx;
311         pb.pctx = &pctx;
312         pctx.str = "pass1b";
313         while (1) {
314                 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
315                         if (e2fsck_mmp_update(fs))
316                                 fatal_error(ctx, 0);
317                 }
318                 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
319                                 EXT2_INODE(&inode), sizeof(inode));
320                 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
321                         continue;
322                 if (pctx.errcode) {
323                         pctx.ino = ino;
324                         fix_problem(ctx, PR_1B_ISCAN_ERROR, &pctx);
325                         ctx->flags |= E2F_FLAG_ABORT;
326                         return;
327                 }
328                 if (!ino)
329                         break;
330                 pctx.ino = ctx->stashed_ino = ino;
331                 if ((ino != EXT2_BAD_INO) &&
332                     !ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino))
333                         continue;
334
335                 pb.ino = ino;
336                 pb.dup_blocks = 0;
337                 pb.inode = &inode;
338                 pb.cur_cluster = ~0;
339                 pb.phys_cluster = ~0;
340                 pb.last_blk = 0;
341                 pb.pctx->blk = pb.pctx->blk2 = 0;
342
343                 if (ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(&inode)) ||
344                     (ino == EXT2_BAD_INO))
345                         pctx.errcode = ext2fs_block_iterate3(fs, ino,
346                                              BLOCK_FLAG_READ_ONLY, block_buf,
347                                              process_pass1b_block, &pb);
348                 /* If the feature is not set, attrs will be cleared later anyway */
349                 if (ext2fs_has_feature_xattr(fs->super) &&
350                     ext2fs_file_acl_block(fs, EXT2_INODE(&inode))) {
351                         blk64_t blk = ext2fs_file_acl_block(fs, EXT2_INODE(&inode));
352                         process_pass1b_block(fs, &blk,
353                                              BLOCK_COUNT_EXTATTR, 0, 0, &pb);
354                         ext2fs_file_acl_block_set(fs, EXT2_INODE(&inode), blk);
355                 }
356                 if (pb.dup_blocks) {
357                         if (ino != EXT2_BAD_INO) {
358                                 op = pctx.blk == pctx.blk2 ?
359                                         PR_1B_DUP_BLOCK : PR_1B_DUP_RANGE;
360                                 fix_problem(ctx, op, pb.pctx);
361                         }
362                         end_problem_latch(ctx, PR_LATCH_DBLOCK);
363                         if (ino >= EXT2_FIRST_INODE(fs->super) ||
364                             ino == EXT2_ROOT_INO)
365                                 dup_inode_count++;
366                 }
367                 if (pctx.errcode)
368                         fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
369         }
370         ext2fs_close_inode_scan(scan);
371         e2fsck_use_inode_shortcuts(ctx, 0);
372 }
373
374 static int process_pass1b_block(ext2_filsys fs EXT2FS_ATTR((unused)),
375                                 blk64_t *block_nr,
376                                 e2_blkcnt_t blockcnt,
377                                 blk64_t ref_blk EXT2FS_ATTR((unused)),
378                                 int ref_offset EXT2FS_ATTR((unused)),
379                                 void *priv_data)
380 {
381         struct process_block_struct *p;
382         e2fsck_t ctx;
383         blk64_t lc, pc;
384         problem_t op;
385
386         if (*block_nr == 0)
387                 return 0;
388         p = (struct process_block_struct *) priv_data;
389         ctx = p->ctx;
390         lc = EXT2FS_B2C(fs, blockcnt);
391         pc = EXT2FS_B2C(fs, *block_nr);
392
393         if (!ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr))
394                 goto finish;
395
396         /* OK, this is a duplicate block */
397         if (p->ino != EXT2_BAD_INO) {
398                 if (p->last_blk + 1 != *block_nr) {
399                         if (p->last_blk) {
400                                 op = p->pctx->blk == p->pctx->blk2 ?
401                                                 PR_1B_DUP_BLOCK :
402                                                 PR_1B_DUP_RANGE;
403                                 fix_problem(ctx, op, p->pctx);
404                         }
405                         p->pctx->blk = *block_nr;
406                 }
407                 p->pctx->blk2 = *block_nr;
408                 p->last_blk = *block_nr;
409         }
410         p->dup_blocks++;
411         ext2fs_mark_inode_bitmap2(inode_dup_map, p->ino);
412
413         /*
414          * Qualifications for submitting a block for duplicate processing:
415          * It's an extent/indirect block (and has a negative logical offset);
416          * we've crossed a logical cluster boundary; or the physical cluster
417          * suddenly changed, which indicates that blocks in a logical cluster
418          * are mapped to multiple physical clusters.
419          */
420         if (blockcnt < 0 || lc != p->cur_cluster || pc != p->phys_cluster)
421                 add_dupe(ctx, p->ino, EXT2FS_B2C(fs, *block_nr), p->inode);
422
423 finish:
424         p->cur_cluster = lc;
425         p->phys_cluster = pc;
426         return 0;
427 }
428
429 /*
430  * Pass 1c: Scan directories for inodes with duplicate blocks.  This
431  * is used so that we can print pathnames when prompting the user for
432  * what to do.
433  */
434 struct search_dir_struct {
435         int             count;
436         ext2_ino_t      first_inode;
437         ext2_ino_t      max_inode;
438 };
439
440 static int search_dirent_proc(ext2_ino_t dir, int entry,
441                               struct ext2_dir_entry *dirent,
442                               int offset EXT2FS_ATTR((unused)),
443                               int blocksize EXT2FS_ATTR((unused)),
444                               char *buf EXT2FS_ATTR((unused)),
445                               void *priv_data)
446 {
447         struct search_dir_struct *sd;
448         struct dup_inode        *p;
449         dnode_t                 *n;
450
451         sd = (struct search_dir_struct *) priv_data;
452
453         if (dirent->inode > sd->max_inode)
454                 /* Should abort this inode, but not everything */
455                 return 0;
456
457         if ((dirent->inode < sd->first_inode) || (entry < DIRENT_OTHER_FILE) ||
458             !ext2fs_test_inode_bitmap2(inode_dup_map, dirent->inode))
459                 return 0;
460
461         n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(dirent->inode));
462         if (!n)
463                 return 0;
464         p = (struct dup_inode *) dnode_get(n);
465         if (!p->dir) {
466                 p->dir = dir;
467                 sd->count--;
468         }
469
470         return(sd->count ? 0 : DIRENT_ABORT);
471 }
472
473
474 static void pass1c(e2fsck_t ctx, char *block_buf)
475 {
476         ext2_filsys fs = ctx->fs;
477         struct search_dir_struct sd;
478         struct problem_context pctx;
479
480         clear_problem_context(&pctx);
481
482         if (!(ctx->options & E2F_OPT_PREEN))
483                 fix_problem(ctx, PR_1C_PASS_HEADER, &pctx);
484
485         /*
486          * Search through all directories to translate inodes to names
487          * (by searching for the containing directory for that inode.)
488          */
489         sd.count = dup_inode_count - dup_inode_founddir;
490         sd.first_inode = EXT2_FIRST_INODE(fs->super);
491         sd.max_inode = fs->super->s_inodes_count;
492         ext2fs_dblist_dir_iterate(fs->dblist, 0, block_buf,
493                                   search_dirent_proc, &sd);
494 }
495
496 static void pass1d(e2fsck_t ctx, char *block_buf)
497 {
498         ext2_filsys fs = ctx->fs;
499         struct dup_inode        *p, *t;
500         struct dup_cluster      *q;
501         ext2_ino_t              *shared, ino;
502         int     shared_len;
503         int     i;
504         int     file_ok;
505         int     meta_data = 0;
506         struct problem_context pctx;
507         dnode_t *n, *m;
508         struct cluster_el       *s;
509         struct inode_el *r;
510
511         clear_problem_context(&pctx);
512
513         if (!(ctx->options & E2F_OPT_PREEN))
514                 fix_problem(ctx, PR_1D_PASS_HEADER, &pctx);
515         e2fsck_read_bitmaps(ctx);
516
517         pctx.num = dup_inode_count; /* dict_count(&ino_dict); */
518         fix_problem(ctx, PR_1D_NUM_DUP_INODES, &pctx);
519         shared = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
520                                 sizeof(ext2_ino_t) * dict_count(&ino_dict),
521                                 "Shared inode list");
522         for (n = dict_first(&ino_dict); n; n = dict_next(&ino_dict, n)) {
523                 p = (struct dup_inode *) dnode_get(n);
524                 shared_len = 0;
525                 file_ok = 1;
526                 ino = (ext2_ino_t)VOIDPTR_TO_INT(dnode_getkey(n));
527                 if (ino == EXT2_BAD_INO || ino == EXT2_RESIZE_INO)
528                         continue;
529
530                 /*
531                  * Find all of the inodes which share blocks with this
532                  * one.  First we find all of the duplicate blocks
533                  * belonging to this inode, and then search each block
534                  * get the list of inodes, and merge them together.
535                  */
536                 for (s = p->cluster_list; s; s = s->next) {
537                         m = dict_lookup(&clstr_dict,
538                                         INT_TO_VOIDPTR(s->cluster));
539                         if (!m)
540                                 continue; /* Should never happen... */
541                         q = (struct dup_cluster *) dnode_get(m);
542                         if (q->num_bad > 1)
543                                 file_ok = 0;
544                         if (q->num_bad == 1 && (ctx->clone == E2F_CLONE_ZERO ||
545                             ctx->shared != E2F_SHARED_PRESERVE))
546                                 file_ok = 0;
547                         if (check_if_fs_cluster(ctx, s->cluster)) {
548                                 file_ok = 0;
549                                 meta_data = 1;
550                         }
551
552                         /*
553                          * Add all inodes used by this block to the
554                          * shared[] --- which is a unique list, so
555                          * if an inode is already in shared[], don't
556                          * add it again.
557                          */
558                         for (r = q->inode_list; r; r = r->next) {
559                                 if (r->inode == ino)
560                                         continue;
561                                 for (i = 0; i < shared_len; i++)
562                                         if (shared[i] == r->inode)
563                                                 break;
564                                 if (i == shared_len) {
565                                         shared[shared_len++] = r->inode;
566                                 }
567                         }
568                 }
569
570                 /*
571                  * Report the inode that we are working on
572                  */
573                 pctx.inode = EXT2_INODE(&p->inode);
574                 pctx.ino = ino;
575                 pctx.dir = p->dir;
576                 pctx.blkcount = p->num_dupblocks;
577                 pctx.num = meta_data ? shared_len+1 : shared_len;
578                 fix_problem(ctx, PR_1D_DUP_FILE, &pctx);
579                 pctx.blkcount = 0;
580                 pctx.num = 0;
581
582                 if (meta_data)
583                         fix_problem(ctx, PR_1D_SHARE_METADATA, &pctx);
584
585                 for (i = 0; i < shared_len; i++) {
586                         m = dict_lookup(&ino_dict, INT_TO_VOIDPTR(shared[i]));
587                         if (!m)
588                                 continue; /* should never happen */
589                         t = (struct dup_inode *) dnode_get(m);
590                         /*
591                          * Report the inode that we are sharing with
592                          */
593                         pctx.inode = EXT2_INODE(&t->inode);
594                         pctx.ino = shared[i];
595                         pctx.dir = t->dir;
596                         fix_problem(ctx, PR_1D_DUP_FILE_LIST, &pctx);
597                 }
598                 /*
599                  * Even if the file shares blocks with itself, we still need to
600                  * clone the blocks.
601                  */
602                 if (file_ok && (meta_data ? shared_len+1 : shared_len) != 0) {
603                         fix_problem(ctx, PR_1D_DUP_BLOCKS_DEALT, &pctx);
604                         continue;
605                 }
606                 if (ctx->shared != E2F_SHARED_DELETE &&
607                     ((ctx->options & E2F_OPT_UNSHARE_BLOCKS) ||
608                     fix_problem(ctx, PR_1D_CLONE_QUESTION, &pctx))) {
609                         pctx.errcode = clone_file(ctx, ino, p, block_buf);
610                         if (pctx.errcode) {
611                                 fix_problem(ctx, PR_1D_CLONE_ERROR, &pctx);
612                                 goto delete;
613                         }
614                         if (ctx->shared == E2F_SHARED_LPF &&
615                             fix_problem(ctx, PR_1D_DISCONNECT_QUESTION, &pctx)){
616                                 pctx.errcode = ext2fs_unlink(fs, p->dir,
617                                                              NULL, ino, 0);
618                                 if (pctx.errcode) {
619                                         fix_problem(ctx, PR_1D_DISCONNECT_ERROR,
620                                                     &pctx);
621                                         goto delete;
622                                 }
623                         }
624                         continue;
625                 }
626 delete:
627                 /*
628                  * Note: When unsharing blocks, we don't prompt to delete
629                  * files. If the clone operation fails than the unshare
630                  * operation should fail too.
631                  */
632                 if (!(ctx->options & E2F_OPT_UNSHARE_BLOCKS) &&
633                     fix_problem(ctx, PR_1D_DELETE_QUESTION, &pctx))
634                         delete_file(ctx, ino, p, block_buf);
635                 else
636                         ext2fs_unmark_valid(fs);
637         }
638         ext2fs_free_mem(&shared);
639 }
640
641 /*
642  * Drop the refcount on the dup_block structure, and clear the entry
643  * in the block_dup_map if appropriate.
644  */
645 static void decrement_badcount(e2fsck_t ctx, blk64_t block,
646                                struct dup_cluster *p)
647 {
648         p->num_bad--;
649         if (p->num_bad <= 0 ||
650             (p->num_bad == 1 && !check_if_fs_block(ctx, block) &&
651             ctx->clone == E2F_CLONE_DUP)) {
652                 if (check_if_fs_cluster(ctx, EXT2FS_B2C(ctx->fs, block)))
653                         return;
654                 ext2fs_unmark_block_bitmap2(ctx->block_dup_map, block);
655         }
656 }
657
658 static int delete_file_block(ext2_filsys fs,
659                              blk64_t    *block_nr,
660                              e2_blkcnt_t blockcnt,
661                              blk64_t ref_block EXT2FS_ATTR((unused)),
662                              int ref_offset EXT2FS_ATTR((unused)),
663                              void *priv_data)
664 {
665         struct process_block_struct *pb;
666         struct dup_cluster *p;
667         dnode_t *n;
668         e2fsck_t ctx;
669         blk64_t c, lc;
670
671         pb = (struct process_block_struct *) priv_data;
672         ctx = pb->ctx;
673
674         if (*block_nr == 0)
675                 return 0;
676
677         c = EXT2FS_B2C(fs, *block_nr);
678         lc = EXT2FS_B2C(fs, blockcnt);
679         if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
680                 n = dict_lookup(&clstr_dict, INT_TO_VOIDPTR(c));
681                 if (n) {
682                         if (lc != pb->cur_cluster) {
683                                 p = (struct dup_cluster *) dnode_get(n);
684                                 decrement_badcount(ctx, *block_nr, p);
685                                 pb->dup_blocks++;
686                         }
687                 } else
688                         com_err("delete_file_block", 0,
689                             _("internal error: can't find dup_blk for %llu\n"),
690                                 *block_nr);
691         } else {
692                 if ((*block_nr % EXT2FS_CLUSTER_RATIO(ctx->fs)) == 0)
693                         ext2fs_block_alloc_stats2(fs, *block_nr, -1);
694                 pb->dup_blocks++;
695         }
696         pb->cur_cluster = lc;
697
698         return 0;
699 }
700
701 static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
702                         struct dup_inode *dp, char* block_buf)
703 {
704         ext2_filsys fs = ctx->fs;
705         struct process_block_struct pb;
706         struct problem_context  pctx;
707         unsigned int            count;
708
709         clear_problem_context(&pctx);
710         pctx.ino = pb.ino = ino;
711         pb.dup_blocks = 0;
712         pb.ctx = ctx;
713         pctx.str = "delete_file";
714         pb.cur_cluster = ~0;
715
716         if (ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(&dp->inode)))
717                 pctx.errcode = ext2fs_block_iterate3(fs, ino,
718                                                      BLOCK_FLAG_READ_ONLY,
719                                                      block_buf,
720                                                      delete_file_block, &pb);
721         if (pctx.errcode)
722                 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
723         if (ctx->inode_badness)
724                 e2fsck_mark_inode_bad(ctx, ino, 0);
725         ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(dp->inode.i_mode));
726         quota_data_sub(ctx->qctx, &dp->inode, ino,
727                        pb.dup_blocks * fs->blocksize);
728         quota_data_inodes(ctx->qctx, &dp->inode, ino, -1);
729
730         /* Inode may have changed by block_iterate, so reread it */
731         e2fsck_read_inode_full(ctx, ino, EXT2_INODE(&dp->inode),
732                                sizeof(dp->inode), "delete_file");
733         e2fsck_clear_inode(ctx, ino, EXT2_INODE(&dp->inode), 0, "delete_file");
734         if (ext2fs_file_acl_block(fs, EXT2_INODE(&dp->inode)) &&
735             ext2fs_has_feature_xattr(fs->super)) {
736                 blk64_t file_acl_block = ext2fs_file_acl_block(fs,
737                                                 EXT2_INODE(&dp->inode));
738
739                 count = 1;
740                 pctx.errcode = ext2fs_adjust_ea_refcount3(fs, file_acl_block,
741                                         block_buf, -1, &count, ino);
742                 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
743                         pctx.errcode = 0;
744                         count = 1;
745                 }
746                 if (pctx.errcode) {
747                         pctx.blk = file_acl_block;
748                         fix_problem(ctx, PR_1B_ADJ_EA_REFCOUNT, &pctx);
749                 }
750                 /*
751                  * If the count is zero, then arrange to have the
752                  * block deleted.  If the block is in the block_dup_map,
753                  * also call delete_file_block since it will take care
754                  * of keeping the accounting straight.
755                  */
756                 if ((count == 0) ||
757                     ext2fs_test_block_bitmap2(ctx->block_dup_map,
758                                               file_acl_block)) {
759                         delete_file_block(fs, &file_acl_block,
760                                           BLOCK_COUNT_EXTATTR, 0, 0, &pb);
761                         ext2fs_file_acl_block_set(fs, EXT2_INODE(&dp->inode),
762                                                   file_acl_block);
763                         quota_data_sub(ctx->qctx, &dp->inode, ino,
764                                        fs->blocksize);
765                 }
766         }
767 }
768
769 struct clone_struct {
770         errcode_t       errcode;
771         blk64_t         dup_cluster;
772         blk64_t         alloc_block;
773         ext2_ino_t      dir, ino;
774         char    *buf;
775         e2fsck_t ctx;
776         struct ext2_inode_large *inode;
777
778         struct dup_cluster *save_dup_cluster;
779         blk64_t save_blocknr;
780 };
781
782 /*
783  * Decrement the bad count *after* we've shown that (a) we can allocate a
784  * replacement block and (b) remap the file blocks.  Unfortunately, there's no
785  * way to find out if the remap succeeded until either the next
786  * clone_file_block() call (an error when remapping the block after returning
787  * BLOCK_CHANGED will halt the iteration) or after block_iterate() returns.
788  * Otherwise, it's possible that we decrease the badcount once in preparation
789  * to remap, then the remap fails (either we can't find a replacement block or
790  * we have to split the extent tree and can't find a new extent block), so we
791  * delete the file, which decreases the badcount again.
792  */
793 static void deferred_dec_badcount(struct clone_struct *cs)
794 {
795         if (!cs->save_dup_cluster)
796                 return;
797         decrement_badcount(cs->ctx, cs->save_blocknr, cs->save_dup_cluster);
798         if (cs->ctx->clone == E2F_CLONE_ZERO &&
799             cs->save_dup_cluster->num_bad == 0) {
800                 ext2fs_unmark_block_bitmap2(cs->ctx->block_found_map,
801                                             cs->save_blocknr);
802                 ext2fs_block_alloc_stats(cs->ctx->fs, cs->save_blocknr, -1);
803         }
804         cs->save_dup_cluster = NULL;
805 }
806
807 static int clone_file_block(ext2_filsys fs,
808                             blk64_t     *block_nr,
809                             e2_blkcnt_t blockcnt,
810                             blk64_t ref_block EXT2FS_ATTR((unused)),
811                             int ref_offset EXT2FS_ATTR((unused)),
812                             void *priv_data)
813 {
814         struct dup_cluster *p = NULL;
815         blk64_t new_block;
816         errcode_t       retval;
817         struct clone_struct *cs = (struct clone_struct *) priv_data;
818         dnode_t *n;
819         e2fsck_t ctx;
820         blk64_t c;
821         int is_meta = 0;
822         int should_write = 1;
823
824         ctx = cs->ctx;
825         deferred_dec_badcount(cs);
826
827         if (*block_nr == 0)
828                 return 0;
829
830         if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
831             (ctx->options & E2F_OPT_UNSHARE_BLOCKS) &&
832             (ctx->options & E2F_OPT_NO))
833                 should_write = 0;
834
835         c = EXT2FS_B2C(fs, blockcnt);
836         if (check_if_fs_cluster(ctx, EXT2FS_B2C(fs, *block_nr)))
837                 is_meta = 1;
838
839         if (c == cs->dup_cluster && cs->alloc_block) {
840                 new_block = cs->alloc_block;
841                 goto got_block;
842         }
843
844         if (ext2fs_test_block_bitmap2(ctx->block_dup_map, *block_nr)) {
845                 n = dict_lookup(&clstr_dict,
846                                 INT_TO_VOIDPTR(EXT2FS_B2C(fs, *block_nr)));
847                 if (!n) {
848                         com_err("clone_file_block", 0,
849                             _("internal error: can't find dup_blk for %llu\n"),
850                                 *block_nr);
851                         return 0;
852                 }
853
854                 p = (struct dup_cluster *) dnode_get(n);
855
856                 cs->dup_cluster = c;
857                 /*
858                  * Let's try an implied cluster allocation.  If we get the same
859                  * cluster back, then we need to find a new block; otherwise,
860                  * we're merely fixing the problem of one logical cluster being
861                  * mapped to multiple physical clusters.
862                  */
863                 new_block = 0;
864                 retval = ext2fs_map_cluster_block(fs, cs->ino,
865                                                   EXT2_INODE(cs->inode),
866                                                   blockcnt, &new_block);
867                 if (retval == 0 && new_block != 0 &&
868                     EXT2FS_B2C(ctx->fs, new_block) !=
869                     EXT2FS_B2C(ctx->fs, *block_nr))
870                         goto cluster_alloc_ok;
871                 retval = ext2fs_new_block2(fs, 0, ctx->block_found_map,
872                                            &new_block);
873                 if (retval) {
874                         cs->errcode = retval;
875                         return BLOCK_ABORT;
876                 }
877                 if (ext2fs_has_feature_shared_blocks(fs->super)) {
878                         /*
879                          * Update the block stats so we don't get a prompt to fix block
880                          * counts in the final pass.
881                          */
882                         ext2fs_block_alloc_stats2(fs, new_block, +1);
883                 }
884 cluster_alloc_ok:
885                 cs->alloc_block = new_block;
886
887         got_block:
888                 new_block &= ~EXT2FS_CLUSTER_MASK(fs);
889                 new_block += EXT2FS_CLUSTER_MASK(fs) & blockcnt;
890                 if (cs->dir && (blockcnt >= 0)) {
891                         retval = ext2fs_set_dir_block2(fs->dblist,
892                                         cs->dir, new_block, blockcnt);
893                         if (retval) {
894                                 cs->errcode = retval;
895                                 return BLOCK_ABORT;
896                         }
897                 }
898 #if 0
899                 printf("Cloning block #%lld from %llu to %llu\n",
900                        blockcnt, *block_nr, new_block);
901 #endif
902                 if (ctx->clone == E2F_CLONE_ZERO) {
903                         memset(cs->buf, 0, fs->blocksize);
904                 } else {
905                         retval = io_channel_read_blk64(fs->io, *block_nr, 1,
906                                                        cs->buf);
907                         if (retval) {
908                                 cs->errcode = retval;
909                                 return BLOCK_ABORT;
910                         }
911                 }
912                 if (should_write) {
913                         retval = io_channel_write_blk64(fs->io, new_block, 1, cs->buf);
914                         if (retval) {
915                                 cs->errcode = retval;
916                                 return BLOCK_ABORT;
917                         }
918                 }
919                 cs->save_dup_cluster = (is_meta ? NULL : p);
920                 cs->save_blocknr = *block_nr;
921                 *block_nr = new_block;
922                 ext2fs_mark_block_bitmap2(ctx->block_found_map, new_block);
923                 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
924
925                 if (!should_write) {
926                         /* Don't try to change extent information; we want e2fsck to
927                          * return success.
928                          */
929                         return 0;
930                 }
931                 return BLOCK_CHANGED;
932         }
933         return 0;
934 }
935
936 static errcode_t clone_file(e2fsck_t ctx, ext2_ino_t ino,
937                             struct dup_inode *dp, char* block_buf)
938 {
939         ext2_filsys fs = ctx->fs;
940         errcode_t       retval;
941         struct clone_struct cs;
942         struct problem_context  pctx;
943         blk64_t         blk, new_blk;
944         dnode_t         *n;
945         struct inode_el *ino_el;
946         struct dup_cluster      *dc;
947         struct dup_inode        *di;
948
949         clear_problem_context(&pctx);
950         cs.errcode = 0;
951         cs.dir = 0;
952         cs.dup_cluster = ~0;
953         cs.alloc_block = 0;
954         cs.ctx = ctx;
955         cs.ino = ino;
956         cs.inode = &dp->inode;
957         cs.save_dup_cluster = NULL;
958         cs.save_blocknr = 0;
959         retval = ext2fs_get_mem(fs->blocksize, &cs.buf);
960         if (retval)
961                 return retval;
962
963         if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, ino))
964                 cs.dir = ino;
965
966         pctx.ino = ino;
967         pctx.str = "clone_file";
968         if (ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(&dp->inode)))
969                 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
970                                                      clone_file_block, &cs);
971         deferred_dec_badcount(&cs);
972         ext2fs_mark_bb_dirty(fs);
973         if (pctx.errcode) {
974                 fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
975                 retval = pctx.errcode;
976                 goto errout;
977         }
978         if (cs.errcode) {
979                 com_err("clone_file", cs.errcode, "%s",
980                         _("returned from clone_file_block"));
981                 retval = cs.errcode;
982                 goto errout;
983         }
984         /* The inode may have changed on disk, so we have to re-read it */
985         e2fsck_read_inode_full(ctx, ino, EXT2_INODE(&dp->inode),
986                                sizeof(dp->inode), "clone file EA");
987         blk = ext2fs_file_acl_block(fs, EXT2_INODE(&dp->inode));
988         new_blk = blk;
989         if (blk && (clone_file_block(fs, &new_blk,
990                                      BLOCK_COUNT_EXTATTR, 0, 0, &cs) ==
991                     BLOCK_CHANGED)) {
992                 ext2fs_file_acl_block_set(fs, EXT2_INODE(&dp->inode), new_blk);
993                 e2fsck_write_inode_full(ctx, ino, EXT2_INODE(&dp->inode),
994                                         sizeof(dp->inode), "clone file EA");
995                 /*
996                  * If we cloned the EA block, find all other inodes
997                  * which referred to that EA block, and modify
998                  * them to point to the new EA block.
999                  */
1000                 n = dict_lookup(&clstr_dict,
1001                                 INT_TO_VOIDPTR(EXT2FS_B2C(fs, blk)));
1002                 if (!n) {
1003                         com_err("clone_file", 0,
1004                                 _("internal error: couldn't lookup EA "
1005                                   "block record for %llu"), blk);
1006                         retval = 0; /* OK to stumble on... */
1007                         goto errout;
1008                 }
1009                 dc = (struct dup_cluster *) dnode_get(n);
1010                 for (ino_el = dc->inode_list; ino_el; ino_el = ino_el->next) {
1011                         if (ino_el->inode == ino)
1012                                 continue;
1013                         n = dict_lookup(&ino_dict, INT_TO_VOIDPTR(ino_el->inode));
1014                         if (!n) {
1015                                 com_err("clone_file", 0,
1016                                         _("internal error: couldn't lookup EA "
1017                                           "inode record for %u"),
1018                                         ino_el->inode);
1019                                 retval = 0; /* OK to stumble on... */
1020                                 goto errout;
1021                         }
1022                         di = (struct dup_inode *) dnode_get(n);
1023                         if (ext2fs_file_acl_block(fs,
1024                                         EXT2_INODE(&di->inode)) == blk) {
1025                                 ext2fs_file_acl_block_set(fs,
1026                                         EXT2_INODE(&di->inode),
1027                                         ext2fs_file_acl_block(fs, EXT2_INODE(&dp->inode)));
1028                                 e2fsck_write_inode_full(ctx, ino_el->inode,
1029                                         EXT2_INODE(&di->inode),
1030                                         sizeof(di->inode), "clone file EA");
1031                                 decrement_badcount(ctx, blk, dc);
1032                         }
1033                 }
1034         }
1035         retval = 0;
1036 errout:
1037         ext2fs_free_mem(&cs.buf);
1038         return retval;
1039 }
1040
1041 /*
1042  * This routine returns 1 if a block overlaps with one of the superblocks,
1043  * group descriptors, inode bitmaps, or block bitmaps.
1044  */
1045 static int check_if_fs_block(e2fsck_t ctx, blk64_t test_block)
1046 {
1047         ext2_filsys fs = ctx->fs;
1048         blk64_t first_block;
1049         dgrp_t  i;
1050
1051         first_block = fs->super->s_first_data_block;
1052         for (i = 0; i < fs->group_desc_count; i++) {
1053
1054                 /* Check superblocks/block group descriptors */
1055                 if (ext2fs_bg_has_super(fs, i)) {
1056                         if (test_block >= first_block &&
1057                             (test_block <= first_block + fs->desc_blocks))
1058                                 return 1;
1059                 }
1060
1061                 /* Check the inode table */
1062                 if ((ext2fs_inode_table_loc(fs, i)) &&
1063                     (test_block >= ext2fs_inode_table_loc(fs, i)) &&
1064                     (test_block < (ext2fs_inode_table_loc(fs, i) +
1065                                    fs->inode_blocks_per_group)))
1066                         return 1;
1067
1068                 /* Check the bitmap blocks */
1069                 if ((test_block == ext2fs_block_bitmap_loc(fs, i)) ||
1070                     (test_block == ext2fs_inode_bitmap_loc(fs, i)))
1071                         return 1;
1072
1073                 first_block += fs->super->s_blocks_per_group;
1074         }
1075         return 0;
1076 }
1077
1078 /*
1079  * This routine returns 1 if a cluster overlaps with one of the superblocks,
1080  * group descriptors, inode bitmaps, or block bitmaps.
1081  */
1082 static int check_if_fs_cluster(e2fsck_t ctx, blk64_t cluster)
1083 {
1084         ext2_filsys fs = ctx->fs;
1085         blk64_t first_block;
1086         dgrp_t  i;
1087
1088         first_block = fs->super->s_first_data_block;
1089         for (i = 0; i < fs->group_desc_count; i++) {
1090
1091                 /* Check superblocks/block group descriptors */
1092                 if (ext2fs_bg_has_super(fs, i)) {
1093                         if (cluster >= EXT2FS_B2C(fs, first_block) &&
1094                             (cluster <= EXT2FS_B2C(fs, first_block +
1095                                                    fs->desc_blocks)))
1096                                 return 1;
1097                 }
1098
1099                 /* Check the inode table */
1100                 if ((ext2fs_inode_table_loc(fs, i)) &&
1101                     (cluster >= EXT2FS_B2C(fs,
1102                                            ext2fs_inode_table_loc(fs, i))) &&
1103                     (cluster <= EXT2FS_B2C(fs,
1104                                            ext2fs_inode_table_loc(fs, i) +
1105                                            fs->inode_blocks_per_group - 1)))
1106                         return 1;
1107
1108                 /* Check the bitmap blocks */
1109                 if ((cluster == EXT2FS_B2C(fs,
1110                                            ext2fs_block_bitmap_loc(fs, i))) ||
1111                     (cluster == EXT2FS_B2C(fs,
1112                                            ext2fs_inode_bitmap_loc(fs, i))))
1113                         return 1;
1114
1115                 first_block += fs->super->s_blocks_per_group;
1116         }
1117         return 0;
1118 }