Whamcloud - gitweb
LU-13717 sec: support encrypted files handling in pfsck mode
[tools/e2fsprogs.git] / e2fsck / pass1.c
1 /*
2  * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
3  *
4  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  *
11  * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12  * and applies the following tests to each inode:
13  *
14  *      - The mode field of the inode must be legal.
15  *      - The size and block count fields of the inode are correct.
16  *      - A data block must not be used by another inode
17  *
18  * Pass 1 also gathers the collects the following information:
19  *
20  *      - A bitmap of which inodes are in use.          (inode_used_map)
21  *      - A bitmap of which inodes are directories.     (inode_dir_map)
22  *      - A bitmap of which inodes are regular files.   (inode_reg_map)
23  *      - An icount mechanism is used to keep track of
24  *        inodes with bad fields and its badness        (ctx->inode_badness)
25  *      - A bitmap of which inodes are in bad blocks.   (inode_bb_map)
26  *      - A bitmap of which inodes are imagic inodes.   (inode_imagic_map)
27  *      - A bitmap of which inodes are casefolded.      (inode_casefold_map)
28  *      - A bitmap of which inodes need to be expanded  (expand_eisize_map)
29  *      - A bitmap of which blocks are in use.          (block_found_map)
30  *      - A bitmap of which blocks are in use by two inodes     (block_dup_map)
31  *      - The data blocks of the directory inodes.      (dir_map)
32  *      - Ref counts for ea_inodes.                     (ea_inode_refs)
33  *      - The encryption policy ID of each encrypted inode. (encrypted_files)
34  *
35  * Pass 1 is designed to stash away enough information so that the
36  * other passes should not need to read in the inode information
37  * during the normal course of a filesystem check.  (Although if an
38  * inconsistency is detected, other passes may need to read in an
39  * inode to fix it.)
40  *
41  * Note that pass 1B will be invoked if there are any duplicate blocks
42  * found.
43  */
44
45 #define _GNU_SOURCE 1 /* get strnlen() */
46 #include "config.h"
47 #include <string.h>
48 #include <time.h>
49 #ifdef HAVE_ERRNO_H
50 #include <errno.h>
51 #endif
52 #include <assert.h>
53 #ifdef HAVE_PTHREAD
54 #include <pthread.h>
55 #endif
56
57 #include "e2fsck.h"
58 #include <ext2fs/ext2_ext_attr.h>
59 /* todo remove this finally */
60 #include <ext2fs/ext2fsP.h>
61 #include <e2p/e2p.h>
62
63 #include "problem.h"
64
65 #ifdef NO_INLINE_FUNCS
66 #define _INLINE_
67 #else
68 #define _INLINE_ inline
69 #endif
70
71 #undef DEBUG
72
73 struct ea_quota {
74         blk64_t blocks;
75         __u64 inodes;
76 };
77
78 static int process_block(ext2_filsys fs, blk64_t        *blocknr,
79                          e2_blkcnt_t blockcnt, blk64_t ref_blk,
80                          int ref_offset, void *priv_data);
81 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
82                              e2_blkcnt_t blockcnt, blk64_t ref_blk,
83                              int ref_offset, void *priv_data);
84 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
85                          char *block_buf,
86                          const struct ea_quota *ea_ibody_quota);
87 static void mark_table_blocks(e2fsck_t ctx);
88 static void alloc_bb_map(e2fsck_t ctx);
89 static void alloc_imagic_map(e2fsck_t ctx);
90 static void add_casefolded_dir(e2fsck_t ctx, ino_t ino);
91 static void handle_fs_bad_blocks(e2fsck_t ctx);
92 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
93 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
94                                   dgrp_t group, void * priv_data);
95 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
96                                     char *block_buf, int adjust_sign);
97 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
98
99 struct process_block_struct {
100         ext2_ino_t      ino;
101         unsigned        is_dir:1, is_reg:1, clear:1, suppress:1,
102                                 fragmented:1, compressed:1, bbcheck:1,
103                                 inode_modified:1;
104         blk64_t         num_blocks;
105         blk64_t         max_blocks;
106         blk64_t         last_block;
107         e2_blkcnt_t     last_init_lblock;
108         e2_blkcnt_t     last_db_block;
109         int             num_illegal_blocks;
110         blk64_t         previous_block;
111         struct ext2_inode *inode;
112         struct problem_context *pctx;
113         ext2fs_block_bitmap fs_meta_blocks;
114         e2fsck_t        ctx;
115         blk64_t         next_lblock;
116         struct extent_tree_info eti;
117 };
118
119 struct process_inode_block {
120         ext2_ino_t ino;
121         struct ea_quota ea_ibody_quota;
122         struct ext2_inode_large inode;
123 };
124
125 struct scan_callback_struct {
126         e2fsck_t                         ctx;
127         char                            *block_buf;
128         struct process_inode_block      *inodes_to_process;
129         int                             *process_inode_count;
130 };
131
132 static void process_inodes(e2fsck_t ctx, char *block_buf,
133                            struct process_inode_block *inodes_to_process,
134                            int *process_inode_count);
135
136 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
137                             EXT2_MIN_BLOCK_LOG_SIZE + 1];
138
139 /*
140  * Check to make sure a device inode is real.  Returns 1 if the device
141  * checks out, 0 if not.
142  *
143  * Note: this routine is now also used to check FIFO's and Sockets,
144  * since they have the same requirement; the i_block fields should be
145  * zero.
146  */
147 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
148                                     struct ext2_inode *inode)
149 {
150         int     i;
151
152         /*
153          * If the index or extents flag is set, then this is a bogus
154          * device/fifo/socket
155          */
156         if (inode->i_flags & (EXT2_INDEX_FL | EXT4_EXTENTS_FL))
157                 return 0;
158
159         /*
160          * We should be able to do the test below all the time, but
161          * because the kernel doesn't forcibly clear the device
162          * inode's additional i_block fields, there are some rare
163          * occasions when a legitimate device inode will have non-zero
164          * additional i_block fields.  So for now, we only complain
165          * when the immutable flag is set, which should never happen
166          * for devices.  (And that's when the problem is caused, since
167          * you can't set or clear immutable flags for devices.)  Once
168          * the kernel has been fixed we can change this...
169          */
170         if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
171                 for (i=4; i < EXT2_N_BLOCKS; i++)
172                         if (inode->i_block[i])
173                                 return 0;
174         }
175         return 1;
176 }
177
178 /*
179  * Check to make sure a symlink inode is real.  Returns 1 if the symlink
180  * checks out, 0 if not.
181  */
182 static int check_symlink(e2fsck_t ctx, struct problem_context *pctx,
183                          ext2_ino_t ino, struct ext2_inode *inode, char *buf)
184 {
185         unsigned int buflen;
186         unsigned int len;
187         blk64_t blk;
188
189         if ((inode->i_size_high || inode->i_size == 0) ||
190             (inode->i_flags & EXT2_INDEX_FL))
191                 return 0;
192
193         if (inode->i_flags & EXT4_INLINE_DATA_FL) {
194                 size_t inline_size;
195
196                 if (inode->i_flags & EXT4_EXTENTS_FL)
197                         return 0;
198                 if (ext2fs_inline_data_size(ctx->fs, ino, &inline_size))
199                         return 0;
200                 if (inode->i_size != inline_size)
201                         return 0;
202
203                 return 1;
204         }
205
206         if (ext2fs_is_fast_symlink(inode)) {
207                 if (inode->i_flags & EXT4_EXTENTS_FL)
208                         return 0;
209                 buf = (char *)inode->i_block;
210                 buflen = sizeof(inode->i_block);
211         } else {
212                 ext2_extent_handle_t    handle;
213                 struct ext2_extent_info info;
214                 struct ext2fs_extent    extent;
215                 int i;
216
217                 if (inode->i_flags & EXT4_EXTENTS_FL) {
218                         if (ext2fs_extent_open2(ctx->fs, ino, inode, &handle))
219                                 return 0;
220                         if (ext2fs_extent_get_info(handle, &info) ||
221                             (info.num_entries != 1) ||
222                             (info.max_depth != 0)) {
223                                 ext2fs_extent_free(handle);
224                                 return 0;
225                         }
226                         if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT,
227                                               &extent) ||
228                             (extent.e_lblk != 0) ||
229                             (extent.e_len != 1)) {
230                                 ext2fs_extent_free(handle);
231                                 return 0;
232                         }
233                         blk = extent.e_pblk;
234                         ext2fs_extent_free(handle);
235                 } else {
236                         blk = inode->i_block[0];
237
238                         for (i = 1; i < EXT2_N_BLOCKS; i++)
239                                 if (inode->i_block[i])
240                                         return 0;
241                 }
242
243                 if (blk < ctx->fs->super->s_first_data_block ||
244                     blk >= ext2fs_blocks_count(ctx->fs->super))
245                         return 0;
246
247                 if (io_channel_read_blk64(ctx->fs->io, blk, 1, buf))
248                         return 0;
249
250                 buflen = ctx->fs->blocksize;
251         }
252
253         if (inode->i_flags & EXT4_ENCRYPT_FL)
254                 len = ext2fs_le16_to_cpu(*(__u16 *)buf) + 2;
255         else {
256                 len = strnlen(buf, buflen);
257
258                 /* Add missing NUL terminator at end of symlink (LU-1540),
259                  * but only offer to fix this in pass1, not from pass2. */
260                 if (len > inode->i_size && pctx != NULL &&
261                     fix_problem(ctx, PR_1_SYMLINK_NUL, pctx)) {
262                         buf[inode->i_size] = '\0';
263                         if (ext2fs_is_fast_symlink(inode)) {
264                                 e2fsck_write_inode(ctx, ino,
265                                                    inode, "check_ext_attr");
266                         } else {
267                                 if (io_channel_write_blk64(ctx->fs->io,
268                                                            blk, 1, buf))
269                                         return 0;
270                         }
271                         len = inode->i_size;
272                 }
273         }
274
275         if (len >= buflen)
276                 return 0;
277
278         if (len != inode->i_size)
279                 return 0;
280
281         return 1;
282 }
283
284 int e2fsck_pass1_check_symlink(e2fsck_t ctx, ext2_ino_t ino,
285                                struct ext2_inode *inode, char *buf)
286 {
287         return check_symlink(ctx, NULL, ino, inode, buf);
288 }
289
290 /*
291  * If the extents or inlinedata flags are set on the inode, offer to clear 'em.
292  */
293 #define BAD_SPECIAL_FLAGS (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)
294 static void check_extents_inlinedata(e2fsck_t ctx,
295                                      struct problem_context *pctx)
296 {
297         if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
298                 return;
299
300         if (!fix_problem(ctx, PR_1_SPECIAL_EXTENTS_IDATA, pctx))
301                 return;
302
303         pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
304         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
305 }
306 #undef BAD_SPECIAL_FLAGS
307
308 /*
309  * If the immutable (or append-only) flag is set on the inode, offer
310  * to clear it.
311  */
312 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
313 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
314 {
315         if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
316                 return;
317
318         if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
319                 return;
320
321         pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
322         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
323 }
324
325 /*
326  * If device, fifo or socket, check size is zero -- if not offer to
327  * clear it
328  */
329 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
330 {
331         struct ext2_inode *inode = pctx->inode;
332
333         if (EXT2_I_SIZE(inode) == 0)
334                 return;
335
336         if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
337                 return;
338
339         ext2fs_inode_size_set(ctx->fs, inode, 0);
340         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
341 }
342
343 /*
344  * For a given size, calculate how many blocks would be charged towards quota.
345  */
346 static blk64_t size_to_quota_blocks(ext2_filsys fs, size_t size)
347 {
348         blk64_t clusters;
349
350         clusters = DIV_ROUND_UP(size, fs->blocksize << fs->cluster_ratio_bits);
351         return EXT2FS_C2B(fs, clusters);
352 }
353
354 /*
355  * Check validity of EA inode. Return 0 if EA inode is valid, otherwise return
356  * the problem code.
357  */
358 static problem_t check_large_ea_inode(e2fsck_t ctx,
359                                       struct ext2_ext_attr_entry *entry,
360                                       struct problem_context *pctx,
361                                       blk64_t *quota_blocks)
362 {
363         struct ext2_inode inode;
364         __u32 hash;
365         errcode_t retval;
366
367         /* Check if inode is within valid range */
368         if ((entry->e_value_inum < EXT2_FIRST_INODE(ctx->fs->super)) ||
369             (entry->e_value_inum > ctx->fs->super->s_inodes_count)) {
370                 pctx->num = entry->e_value_inum;
371                 return PR_1_ATTR_VALUE_EA_INODE;
372         }
373
374         e2fsck_read_inode(ctx, entry->e_value_inum, &inode, "pass1");
375
376         retval = ext2fs_ext_attr_hash_entry2(ctx->fs, entry, NULL, &hash);
377         if (retval) {
378                 com_err("check_large_ea_inode", retval,
379                         _("while hashing entry with e_value_inum = %u"),
380                         entry->e_value_inum);
381                 fatal_error(ctx, 0);
382         }
383
384         if (hash == entry->e_hash) {
385                 *quota_blocks = size_to_quota_blocks(ctx->fs,
386                                                      entry->e_value_size);
387         } else {
388                 /* This might be an old Lustre-style ea_inode reference. */
389                 if (inode.i_mtime == pctx->ino &&
390                     inode.i_generation == pctx->inode->i_generation) {
391                         *quota_blocks = 0;
392                 } else {
393                         /* If target inode is also missing EA_INODE flag,
394                          * this is likely to be a bad reference.
395                          */
396                         if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
397                                 pctx->num = entry->e_value_inum;
398                                 return PR_1_ATTR_VALUE_EA_INODE;
399                         } else {
400                                 pctx->num = entry->e_hash;
401                                 return PR_1_ATTR_HASH;
402                         }
403                 }
404         }
405
406         if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
407                 pctx->num = entry->e_value_inum;
408                 if (fix_problem(ctx, PR_1_ATTR_SET_EA_INODE_FL, pctx)) {
409                         inode.i_flags |= EXT4_EA_INODE_FL;
410                         e2fsck_pass1_fix_lock(ctx);
411                         ext2fs_write_inode(ctx->fs, entry->e_value_inum,
412                                            &inode);
413                         e2fsck_pass1_fix_unlock(ctx);
414                 } else {
415                         return PR_1_ATTR_NO_EA_INODE_FL;
416                 }
417         }
418         return 0;
419 }
420
421 static void inc_ea_inode_refs(e2fsck_t ctx, struct problem_context *pctx,
422                               struct ext2_ext_attr_entry *first, void *end)
423 {
424         struct ext2_ext_attr_entry *entry;
425
426         for (entry = first;
427              (void *)entry < end && !EXT2_EXT_IS_LAST_ENTRY(entry);
428              entry = EXT2_EXT_ATTR_NEXT(entry)) {
429                 if (!entry->e_value_inum)
430                         continue;
431                 if (!ctx->ea_inode_refs) {
432                         pctx->errcode = ea_refcount_create(0,
433                                                            &ctx->ea_inode_refs);
434                         if (pctx->errcode) {
435                                 pctx->num = 4;
436                                 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
437                                 ctx->flags |= E2F_FLAG_ABORT;
438                                 return;
439                         }
440                 }
441                 ea_refcount_increment(ctx->ea_inode_refs, entry->e_value_inum,
442                                       0);
443         }
444 }
445
446 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx,
447                               struct ea_quota *ea_ibody_quota)
448 {
449         struct ext2_super_block *sb = ctx->fs->super;
450         struct ext2_inode_large *inode;
451         struct ext2_ext_attr_entry *entry;
452         char *start, *header, *end;
453         unsigned int storage_size, remain;
454         problem_t problem = 0;
455         region_t region = 0;
456
457         ea_ibody_quota->blocks = 0;
458         ea_ibody_quota->inodes = 0;
459
460         inode = (struct ext2_inode_large *) pctx->inode;
461         storage_size = EXT2_INODE_SIZE(ctx->fs->super) -
462                 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize;
463         header = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
464                  inode->i_extra_isize;
465         end = header + storage_size;
466         entry = &IHDR(inode)->h_first_entry[0];
467         start = (char *)entry;
468
469         /* scan all entry's headers first */
470
471         /* take finish entry 0UL into account */
472         remain = storage_size - sizeof(__u32);
473
474         region = region_create(0, storage_size);
475         if (!region) {
476                 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
477                 problem = 0;
478                 ctx->flags |= E2F_FLAG_ABORT;
479                 return;
480         }
481         if (region_allocate(region, 0, sizeof(__u32))) {
482                 problem = PR_1_INODE_EA_ALLOC_COLLISION;
483                 goto fix;
484         }
485
486         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
487                !EXT2_EXT_IS_LAST_ENTRY(entry)) {
488                 __u32 hash;
489
490                 if (region_allocate(region, (char *)entry - (char *)header,
491                                     EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
492                         problem = PR_1_INODE_EA_ALLOC_COLLISION;
493                         goto fix;
494                 }
495
496                 /* header eats this space */
497                 remain -= sizeof(struct ext2_ext_attr_entry);
498
499                 /* is attribute name valid? */
500                 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
501                         pctx->num = entry->e_name_len;
502                         problem = PR_1_ATTR_NAME_LEN;
503                         goto fix;
504                 }
505
506                 /* attribute len eats this space */
507                 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
508
509                 if (entry->e_value_inum == 0) {
510                         /* check value size */
511                         if (entry->e_value_size > remain) {
512                                 pctx->num = entry->e_value_size;
513                                 problem = PR_1_ATTR_VALUE_SIZE;
514                                 goto fix;
515                         }
516
517                         if (entry->e_value_size &&
518                             region_allocate(region,
519                                             sizeof(__u32) + entry->e_value_offs,
520                                             EXT2_EXT_ATTR_SIZE(
521                                                 entry->e_value_size))) {
522                                 problem = PR_1_INODE_EA_ALLOC_COLLISION;
523                                 goto fix;
524                         }
525
526                         hash = ext2fs_ext_attr_hash_entry(entry,
527                                                           start + entry->e_value_offs);
528
529                         /* e_hash may be 0 in older inode's ea */
530                         if (entry->e_hash != 0 && entry->e_hash != hash) {
531                                 pctx->num = entry->e_hash;
532                                 problem = PR_1_ATTR_HASH;
533                                 goto fix;
534                         }
535                 } else {
536                         blk64_t quota_blocks;
537
538                         problem = check_large_ea_inode(ctx, entry, pctx,
539                                                        &quota_blocks);
540                         if (problem != 0)
541                                 goto fix;
542
543                         ea_ibody_quota->blocks += quota_blocks;
544                         ea_ibody_quota->inodes++;
545                 }
546
547                 /* If EA value is stored in external inode then it does not
548                  * consume space here */
549                 if (entry->e_value_inum == 0)
550                         remain -= entry->e_value_size;
551
552                 entry = EXT2_EXT_ATTR_NEXT(entry);
553         }
554
555         if (region_allocate(region, (char *)entry - (char *)header,
556                             sizeof(__u32))) {
557                 problem = PR_1_INODE_EA_ALLOC_COLLISION;
558                 goto fix;
559         }
560 fix:
561         if (region)
562                 region_free(region);
563         /*
564          * it seems like a corruption. it's very unlikely we could repair
565          * EA(s) in automatic fashion -bzzz
566          */
567         if (problem == 0 || !fix_problem(ctx, problem, pctx)) {
568                 inc_ea_inode_refs(ctx, pctx,
569                                   (struct ext2_ext_attr_entry *)start, end);
570                 return;
571         }
572
573         /* simply remove all possible EA(s) */
574         *((__u32 *)header) = 0UL;
575         e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
576                                 EXT2_INODE_SIZE(sb), "pass1");
577         ea_ibody_quota->blocks = 0;
578         ea_ibody_quota->inodes = 0;
579 }
580
581 static int check_inode_extra_negative_epoch(__u32 xtime, __u32 extra) {
582         return (xtime & (1U << 31)) != 0 &&
583                 (extra & EXT4_EPOCH_MASK) == EXT4_EPOCH_MASK;
584 }
585
586 #define CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, xtime) \
587         check_inode_extra_negative_epoch(inode->i_##xtime, \
588                                          inode->i_##xtime##_extra)
589
590 /* When today's date is earlier than 2242, we assume that atimes,
591  * ctimes, crtimes, and mtimes with years in the range 2310..2378 are
592  * actually pre-1970 dates mis-encoded.
593  */
594 #define EXT4_EXTRA_NEGATIVE_DATE_CUTOFF 2 * (1LL << 32)
595
596 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx,
597                                     struct ea_quota *ea_ibody_quota)
598 {
599         struct ext2_super_block *sb = ctx->fs->super;
600         struct ext2_inode_large *inode;
601         __u32 *eamagic;
602         int min, max, dirty = 0;
603
604         ea_ibody_quota->blocks = 0;
605         ea_ibody_quota->inodes = 0;
606
607         inode = (struct ext2_inode_large *) pctx->inode;
608         if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
609                 /* this isn't large inode. so, nothing to check */
610                 return;
611         }
612
613 #if 0
614         printf("inode #%u, i_extra_size %d\n", pctx->ino,
615                         inode->i_extra_isize);
616 #endif
617         /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
618         min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
619         max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
620         /*
621          * For now we will allow i_extra_isize to be 0, but really
622          * implementations should never allow i_extra_isize to be 0
623          */
624         if (inode->i_extra_isize &&
625             (inode->i_extra_isize < min || inode->i_extra_isize > max ||
626              inode->i_extra_isize & 3)) {
627                 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
628                         return;
629                 if (inode->i_extra_isize < min || inode->i_extra_isize > max)
630                         inode->i_extra_isize = ctx->want_extra_isize;
631                 else
632                         inode->i_extra_isize = (inode->i_extra_isize + 3) & ~3;
633                 dirty = 1;
634
635                 goto out;
636         }
637
638         /* check if there is no place for an EA header */
639         if (inode->i_extra_isize >= max - sizeof(__u32))
640                 return;
641
642         eamagic = &IHDR(inode)->h_magic;
643         if (*eamagic != EXT2_EXT_ATTR_MAGIC &&
644             (ctx->flags & E2F_FLAG_EXPAND_EISIZE) &&
645             (inode->i_extra_isize < ctx->want_extra_isize)) {
646                 fix_problem(ctx, PR_1_EXPAND_EISIZE, pctx);
647                 memset((char *)inode + EXT2_GOOD_OLD_INODE_SIZE, 0,
648                         EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE);
649                 inode->i_extra_isize = ctx->want_extra_isize;
650                 dirty = 1;
651                 if (inode->i_extra_isize < ctx->min_extra_isize)
652                         ctx->min_extra_isize = inode->i_extra_isize;
653         }
654
655         if (*eamagic == EXT2_EXT_ATTR_MAGIC)
656                 check_ea_in_inode(ctx, pctx, ea_ibody_quota);
657
658         if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_crtime, ctx->time_fudge))
659                 e2fsck_mark_inode_bad(ctx, pctx, PR_1_CRTIME_BAD);
660         else if (EXT4_XTIME_ANCIENT(ctx, sb, inode->i_crtime, ctx->time_fudge))
661                 e2fsck_mark_inode_bad(ctx, pctx, PR_1_CRTIME_BAD);
662         /*
663          * If the inode's extended atime (ctime, crtime, mtime) is stored in
664          * the old, invalid format, repair it.
665          */
666         if (((sizeof(time_t) <= 4) ||
667              (((sizeof(time_t) > 4) &&
668                ctx->now < EXT4_EXTRA_NEGATIVE_DATE_CUTOFF))) &&
669             (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime) ||
670              CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime) ||
671              CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime) ||
672              CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))) {
673
674                 if (!fix_problem_bad(ctx, PR_1_EA_TIME_OUT_OF_RANGE, pctx, 2))
675                         return;
676
677                 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime))
678                         inode->i_atime_extra &= ~EXT4_EPOCH_MASK;
679                 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime))
680                         inode->i_ctime_extra &= ~EXT4_EPOCH_MASK;
681                 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime))
682                         inode->i_crtime_extra &= ~EXT4_EPOCH_MASK;
683                 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))
684                         inode->i_mtime_extra &= ~EXT4_EPOCH_MASK;
685                 dirty = 1;
686         }
687
688 out:
689         if (dirty)
690                 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
691                                         EXT2_INODE_SIZE(sb), "pass1");
692 }
693
694 static _INLINE_ int is_blocks_used(e2fsck_t ctx, blk64_t block,
695                                    unsigned int num)
696 {
697         int retval;
698
699         /* used to avoid duplicate output from below */
700         retval = ext2fs_test_block_bitmap_range2_valid(ctx->block_found_map,
701                                                        block, num);
702         if (!retval)
703                 return 0;
704
705         retval = ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num);
706         if (retval) {
707                 e2fsck_pass1_block_map_r_lock(ctx);
708                 if (ctx->global_ctx)
709                         retval = ext2fs_test_block_bitmap_range2(
710                                         ctx->global_ctx->block_found_map, block, num);
711                 e2fsck_pass1_block_map_r_unlock(ctx);
712                 if (retval)
713                         return 0;
714         }
715
716         return 1;
717 }
718
719 /*
720  * Check to see if the inode might really be a directory, despite i_mode
721  *
722  * This is a lot of complexity for something for which I'm not really
723  * convinced happens frequently in the wild.  If for any reason this
724  * causes any problems, take this code out.
725  * [tytso:20070331.0827EDT]
726  */
727 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
728                                 char *buf)
729 {
730         struct ext2_inode *inode = pctx->inode;
731         struct ext2_dir_entry   *dirent;
732         errcode_t               retval;
733         blk64_t                 blk;
734         unsigned int            i, rec_len, not_device = 0;
735         int                     extent_fs;
736         int                     inlinedata_fs;
737
738         /*
739          * If the mode looks OK, we believe it.  If the first block in
740          * the i_block array is 0, this cannot be a directory. If the
741          * inode is extent-mapped, it is still the case that the latter
742          * cannot be 0 - the magic number in the extent header would make
743          * it nonzero.
744          */
745         if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
746             LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
747                 return;
748
749         /*
750          * Check the block numbers in the i_block array for validity:
751          * zero blocks are skipped (but the first one cannot be zero -
752          * see above), other blocks are checked against the first and
753          * max data blocks (from the the superblock) and against the
754          * block bitmap. Any invalid block found means this cannot be
755          * a directory.
756          *
757          * If there are non-zero blocks past the fourth entry, then
758          * this cannot be a device file: we remember that for the next
759          * check.
760          *
761          * For extent mapped files, we don't do any sanity checking:
762          * just try to get the phys block of logical block 0 and run
763          * with it.
764          *
765          * For inline data files, we just try to get the size of inline
766          * data.  If it's true, we will treat it as a directory.
767          */
768
769         extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
770         inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
771         if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL)) {
772                 size_t size;
773                 __u32 dotdot;
774                 unsigned int rec_len2;
775                 struct ext2_dir_entry de;
776
777                 if (ext2fs_inline_data_size(ctx->fs, pctx->ino, &size))
778                         return;
779                 /*
780                  * If the size isn't a multiple of 4, it's probably not a
781                  * directory??
782                  */
783                 if (size & 3)
784                         return;
785                 /*
786                  * If the first 10 bytes don't look like a directory entry,
787                  * it's probably not a directory.
788                  */
789                 memcpy(&dotdot, inode->i_block, sizeof(dotdot));
790                 memcpy(&de, ((char *)inode->i_block) + EXT4_INLINE_DATA_DOTDOT_SIZE,
791                        EXT2_DIR_NAME_LEN(0));
792                 dotdot = ext2fs_le32_to_cpu(dotdot);
793                 de.inode = ext2fs_le32_to_cpu(de.inode);
794                 de.rec_len = ext2fs_le16_to_cpu(de.rec_len);
795                 ext2fs_get_rec_len(ctx->fs, &de, &rec_len2);
796                 if (dotdot >= ctx->fs->super->s_inodes_count ||
797                     (dotdot < EXT2_FIRST_INO(ctx->fs->super) &&
798                      dotdot != EXT2_ROOT_INO) ||
799                     de.inode >= ctx->fs->super->s_inodes_count ||
800                     (de.inode < EXT2_FIRST_INO(ctx->fs->super) &&
801                      de.inode != 0) ||
802                     rec_len2 > EXT4_MIN_INLINE_DATA_SIZE -
803                               EXT4_INLINE_DATA_DOTDOT_SIZE)
804                         return;
805                 /* device files never have a "system.data" entry */
806                 goto isdir;
807         } else if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
808                 /* extent mapped */
809                 if  (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
810                                  &blk))
811                         return;
812                 /* device files are never extent mapped */
813                 not_device++;
814         } else {
815                 for (i=0; i < EXT2_N_BLOCKS; i++) {
816                         blk = inode->i_block[i];
817                         if (!blk)
818                                 continue;
819                         if (i >= 4)
820                                 not_device++;
821
822                         if (blk < ctx->fs->super->s_first_data_block ||
823                             blk >= ext2fs_blocks_count(ctx->fs->super) ||
824                             is_blocks_used(ctx, blk, 1))
825                                 return; /* Invalid block, can't be dir */
826                 }
827                 blk = inode->i_block[0];
828         }
829
830         /*
831          * If the mode says this is a device file and the i_links_count field
832          * is sane and we have not ruled it out as a device file previously,
833          * we declare it a device file, not a directory.
834          */
835         if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
836             (inode->i_links_count == 1) && !not_device)
837                 return;
838
839         /* read the first block */
840         ehandler_operation(_("reading directory block"));
841         retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
842         ehandler_operation(0);
843         if (retval)
844                 return;
845
846         dirent = (struct ext2_dir_entry *) buf;
847         retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
848         if (retval)
849                 return;
850         if ((ext2fs_dirent_name_len(dirent) != 1) ||
851             (dirent->name[0] != '.') ||
852             (dirent->inode != pctx->ino) ||
853             (rec_len < 12) ||
854             (rec_len % 4) ||
855             (rec_len >= ctx->fs->blocksize - 12))
856                 return;
857
858         dirent = (struct ext2_dir_entry *) (buf + rec_len);
859         retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
860         if (retval)
861                 return;
862         if ((ext2fs_dirent_name_len(dirent) != 2) ||
863             (dirent->name[0] != '.') ||
864             (dirent->name[1] != '.') ||
865             (rec_len < 12) ||
866             (rec_len % 4))
867                 return;
868
869 isdir:
870         if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
871                 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
872                 e2fsck_write_inode_full(ctx, pctx->ino, inode,
873                                         EXT2_INODE_SIZE(ctx->fs->super),
874                                         "check_is_really_dir");
875         }
876 }
877
878 extern errcode_t e2fsck_setup_icount(e2fsck_t ctx, const char *icount_name,
879                                      int flags, ext2_icount_t hint,
880                                      ext2_icount_t *ret)
881 {
882         unsigned int            threshold;
883         unsigned int            save_type;
884         ext2_ino_t              num_dirs;
885         errcode_t               retval;
886         char                    *tdb_dir;
887         int                     enable;
888
889         *ret = 0;
890
891         profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
892                            &tdb_dir);
893         profile_get_uint(ctx->profile, "scratch_files",
894                          "numdirs_threshold", 0, 0, &threshold);
895         profile_get_boolean(ctx->profile, "scratch_files",
896                             "icount", 0, 1, &enable);
897
898         retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
899         if (retval)
900                 num_dirs = 1024;        /* Guess */
901
902         if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
903             (!threshold || num_dirs > threshold)) {
904                 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir,
905                                                   flags, ret);
906                 if (retval == 0)
907                         return 0;
908         }
909         e2fsck_set_bitmap_type(ctx->fs, EXT2FS_BMAP64_RBTREE, icount_name,
910                                &save_type);
911         if (ctx->options & E2F_OPT_ICOUNT_FULLMAP)
912                 flags |= EXT2_ICOUNT_OPT_FULLMAP;
913         retval = ext2fs_create_icount2(ctx->fs, flags, 0, hint, ret);
914         ctx->fs->default_bitmap_type = save_type;
915         return retval;
916 }
917
918 static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
919                                             e2fsck_t ctx,
920                                             struct problem_context *pctx)
921 {
922         errcode_t retval;
923         struct ext2_inode_large inode;
924
925         /*
926          * Reread inode.  If we don't see checksum error, then this inode
927          * has been fixed elsewhere.
928          */
929         ctx->stashed_ino = 0;
930         retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
931                                         sizeof(inode));
932         if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
933                 return retval;
934         if (!retval)
935                 return 0;
936
937         /*
938          * Checksum still doesn't match.  That implies that the inode passes
939          * all the sanity checks, so maybe the checksum is simply corrupt.
940          * See if the user will go for fixing that.
941          */
942         if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
943                 return 0;
944
945
946         e2fsck_pass1_fix_lock(ctx);
947         retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
948                                          sizeof(inode));
949         e2fsck_pass1_fix_unlock(ctx);
950         return retval;
951 }
952
953 int e2fsck_pass1_delete_attr(e2fsck_t ctx, struct ext2_inode_large *inode,
954                              struct problem_context *pctx, int needed_size)
955 {
956         struct ext2_ext_attr_header *header;
957         struct ext2_ext_attr_entry *entry_ino, *entry_blk = NULL, *entry;
958         char *start, name[4096], block_buf[4096];
959         int len, index = EXT2_ATTR_INDEX_USER, entry_size, ea_size;
960         int in_inode = 1, error;
961         unsigned int freed_bytes = inode->i_extra_isize;
962
963         entry_ino = &IHDR(inode)->h_first_entry[0];
964         start = (char *)entry_ino;
965
966         if (inode->i_file_acl) {
967                 error = ext2fs_read_ext_attr(ctx->fs, inode->i_file_acl,
968                                              block_buf);
969                 /* We have already checked this block, shouldn't happen */
970                 if (error) {
971                         fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
972                         return 0;
973                 }
974                 header = BHDR(block_buf);
975                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
976                         fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
977                         return 0;
978                 }
979
980                 entry_blk = (struct ext2_ext_attr_entry *)(header+1);
981         }
982         entry = entry_ino;
983         len = sizeof(entry->e_name);
984         entry_size = ext2fs_attr_get_next_attr(entry, index, name, len, 1);
985
986         while (freed_bytes < needed_size) {
987                 if (entry_size && name[0] != '\0') {
988                         pctx->str = name;
989                         if (fix_problem(ctx, PR_1_EISIZE_DELETE_EA, pctx)) {
990                                 ea_size = EXT2_EXT_ATTR_LEN(entry->e_name_len) +
991                                           EXT2_EXT_ATTR_SIZE(entry->e_value_size);
992                                 error = ext2fs_attr_set(ctx->fs, pctx->ino,
993                                                         (struct ext2_inode *)inode,
994                                                         index, name, 0, 0, 0);
995                                 if (!error)
996                                         freed_bytes += ea_size;
997                         }
998                 }
999                 len = sizeof(entry->e_name);
1000                 entry_size = ext2fs_attr_get_next_attr(entry, index,name,len,0);
1001                 entry = EXT2_EXT_ATTR_NEXT(entry);
1002                 if (EXT2_EXT_IS_LAST_ENTRY(entry)) {
1003                         if (in_inode) {
1004                                 entry = entry_blk;
1005                                 len = sizeof(entry->e_name);
1006                                 entry_size = ext2fs_attr_get_next_attr(entry,
1007                                                         index, name, len, 1);
1008                                 in_inode = 0;
1009                         } else {
1010                                 index += 1;
1011                                 in_inode = 1;
1012                                 if (!entry && index < EXT2_ATTR_INDEX_MAX)
1013                                         entry = (struct ext2_ext_attr_entry *)start;
1014                                 else
1015                                         return freed_bytes;
1016                         }
1017                 }
1018         }
1019
1020         return freed_bytes;
1021 }
1022
1023 int e2fsck_pass1_expand_eisize(e2fsck_t ctx, struct ext2_inode_large *inode,
1024                                struct problem_context *pctx)
1025 {
1026         int needed_size = 0, retval, ret = EXT2_EXPAND_EISIZE_UNSAFE;
1027         static int message;
1028
1029 retry:
1030         retval = ext2fs_expand_extra_isize(ctx->fs, pctx->ino, inode,
1031                                            ctx->want_extra_isize, &ret,
1032                                            &needed_size);
1033         if (ret & EXT2_EXPAND_EISIZE_NEW_BLOCK)
1034                 goto mark_expand_eisize_map;
1035         if (!retval) {
1036                 e2fsck_write_inode_full(ctx, pctx->ino,
1037                                         (struct ext2_inode *)inode,
1038                                         EXT2_INODE_SIZE(ctx->fs->super),
1039                                         "pass1");
1040                 return 0;
1041         }
1042
1043         if (ret & EXT2_EXPAND_EISIZE_NOSPC) {
1044                 if (ctx->options & (E2F_OPT_PREEN | E2F_OPT_YES)) {
1045                         fix_problem(ctx, PR_1_EA_BLK_NOSPC, pctx);
1046                         ctx->flags |= E2F_FLAG_ABORT;
1047                         return -1;
1048                 }
1049
1050                 if (!message) {
1051                         pctx->num = ctx->fs->super->s_min_extra_isize;
1052                         fix_problem(ctx, PR_1_EXPAND_EISIZE_WARNING, pctx);
1053                         message = 1;
1054                 }
1055 delete_EA:
1056                 retval = e2fsck_pass1_delete_attr(ctx, inode, pctx,
1057                                                   needed_size);
1058                 if (retval >= ctx->want_extra_isize)
1059                         goto retry;
1060
1061                 needed_size -= retval;
1062
1063                 /*
1064                  * We loop here until either the user deletes EA(s) or
1065                  * EXTRA_ISIZE feature is disabled.
1066                  */
1067                 if (fix_problem(ctx, PR_1_CLEAR_EXTRA_ISIZE, pctx)) {
1068                         ctx->fs->super->s_feature_ro_compat &=
1069                                         ~EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE;
1070                         ext2fs_mark_super_dirty(ctx->fs);
1071                 } else {
1072                         goto delete_EA;
1073                 }
1074                 ctx->fs_unexpanded_inodes++;
1075
1076                 /* No EA was deleted, inode cannot be expanded */
1077                 return -1;
1078         }
1079
1080 mark_expand_eisize_map:
1081         if (!ctx->expand_eisize_map) {
1082                 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1083                                          _("expand extrz isize map"),
1084                                          &ctx->expand_eisize_map);
1085                 if (pctx->errcode) {
1086                         fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
1087                                     pctx);
1088                         exit(1);
1089                 }
1090         }
1091
1092         /* Add this inode to the expand_eisize_map */
1093         ext2fs_mark_inode_bitmap2(ctx->expand_eisize_map, pctx->ino);
1094         return 0;
1095 }
1096
1097 static void reserve_block_for_root_repair(e2fsck_t ctx)
1098 {
1099         blk64_t         blk = 0;
1100         errcode_t       err;
1101         ext2_filsys     fs = ctx->fs;
1102
1103         ctx->root_repair_block = 0;
1104         if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
1105                 return;
1106
1107         err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1108         if (err)
1109                 return;
1110         ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1111         ctx->root_repair_block = blk;
1112 }
1113
1114 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
1115 {
1116         blk64_t         blk = 0;
1117         errcode_t       err;
1118         ext2_filsys     fs = ctx->fs;
1119         static const char name[] = "lost+found";
1120         ext2_ino_t      ino;
1121
1122         ctx->lnf_repair_block = 0;
1123         if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
1124                 return;
1125
1126         err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1127         if (err)
1128                 return;
1129         ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1130         ctx->lnf_repair_block = blk;
1131         return;
1132 }
1133
1134 static errcode_t get_inline_data_ea_size(ext2_filsys fs, ext2_ino_t ino,
1135                                          size_t *sz)
1136 {
1137         void *p;
1138         struct ext2_xattr_handle *handle;
1139         errcode_t retval;
1140
1141         retval = ext2fs_xattrs_open(fs, ino, &handle);
1142         if (retval)
1143                 return retval;
1144
1145         retval = ext2fs_xattrs_read(handle);
1146         if (retval)
1147                 goto err;
1148
1149         retval = ext2fs_xattr_get(handle, "system.data", &p, sz);
1150         if (retval)
1151                 goto err;
1152         ext2fs_free_mem(&p);
1153 err:
1154         (void) ext2fs_xattrs_close(&handle);
1155         return retval;
1156 }
1157
1158 int e2fsck_fix_bad_inode(e2fsck_t ctx, struct problem_context *pctx)
1159 {
1160         __u16 badness;
1161         int rc = 0;
1162
1163         if (!ctx->inode_badness)
1164                 return 0;
1165
1166         if (ext2fs_icount_fetch(ctx->inode_badness, pctx->ino, &badness))
1167                 return 0;
1168
1169         if ((badness & ~BADNESS_BAD_MODE) > ctx->inode_badness_threshold) {
1170                 __u64 pctx_num_sav = pctx->num;
1171
1172                 pctx->num = badness;
1173                 rc = fix_problem_bad(ctx, PR_1B_INODE_TOOBAD, pctx, 0);
1174                 pctx->num = pctx_num_sav;
1175         }
1176
1177         return rc;
1178 }
1179
1180 static void finish_processing_inode(e2fsck_t ctx, ext2_ino_t ino,
1181                                     struct problem_context *pctx,
1182                                     int failed_csum)
1183 {
1184         if (!failed_csum)
1185                 return;
1186
1187         /*
1188          * If the inode failed the checksum and the user didn't
1189          * clear the inode, test the checksum again -- if it still
1190          * fails, ask the user if the checksum should be corrected.
1191          */
1192         pctx->errcode = recheck_bad_inode_checksum(ctx->fs, ino, ctx, pctx);
1193         if (pctx->errcode)
1194                 ctx->flags |= E2F_FLAG_ABORT;
1195 }
1196 #define FINISH_INODE_LOOP(ctx, ino, pctx, failed_csum) \
1197         do { \
1198                 finish_processing_inode((ctx), (ino), (pctx), (failed_csum)); \
1199                 if (e2fsck_should_abort(ctx)) { \
1200                         e2fsck_pass1_check_unlock(ctx); \
1201                         return; \
1202                 } \
1203         } while (0)
1204
1205 static int could_be_block_map(ext2_filsys fs, struct ext2_inode *inode)
1206 {
1207         __u32 x;
1208         int i;
1209
1210         for (i = 0; i < EXT2_N_BLOCKS; i++) {
1211                 x = inode->i_block[i];
1212 #ifdef WORDS_BIGENDIAN
1213                 x = ext2fs_swab32(x);
1214 #endif
1215                 if (x >= ext2fs_blocks_count(fs->super))
1216                         return 0;
1217         }
1218
1219         return 1;
1220 }
1221
1222 /*
1223  * Figure out what to do with an inode that has both extents and inline data
1224  * inode flags set.  Returns -1 if we decide to erase the inode, 0 otherwise.
1225  */
1226 static int fix_inline_data_extents_file(e2fsck_t ctx,
1227                                         ext2_ino_t ino,
1228                                         struct ext2_inode *inode,
1229                                         int inode_size,
1230                                         struct problem_context *pctx)
1231 {
1232         size_t max_inline_ea_size;
1233         ext2_filsys fs = ctx->fs;
1234         int dirty = 0;
1235
1236         /* Both feature flags not set?  Just run the regular checks */
1237         if (!ext2fs_has_feature_extents(fs->super) &&
1238             !ext2fs_has_feature_inline_data(fs->super))
1239                 return 0;
1240
1241         /* Clear both flags if it's a special file */
1242         if (LINUX_S_ISCHR(inode->i_mode) ||
1243             LINUX_S_ISBLK(inode->i_mode) ||
1244             LINUX_S_ISFIFO(inode->i_mode) ||
1245             LINUX_S_ISSOCK(inode->i_mode)) {
1246                 check_extents_inlinedata(ctx, pctx);
1247                 return 0;
1248         }
1249
1250         /* If it looks like an extent tree, try to clear inlinedata */
1251         if (ext2fs_extent_header_verify(inode->i_block,
1252                                  sizeof(inode->i_block)) == 0 &&
1253             fix_problem(ctx, PR_1_CLEAR_INLINE_DATA_FOR_EXTENT, pctx)) {
1254                 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1255                 dirty = 1;
1256                 goto out;
1257         }
1258
1259         /* If it looks short enough to be inline data, try to clear extents */
1260         if (inode_size > EXT2_GOOD_OLD_INODE_SIZE)
1261                 max_inline_ea_size = inode_size -
1262                                      (EXT2_GOOD_OLD_INODE_SIZE +
1263                                       ((struct ext2_inode_large *)inode)->i_extra_isize);
1264         else
1265                 max_inline_ea_size = 0;
1266         if (EXT2_I_SIZE(inode) <
1267             EXT4_MIN_INLINE_DATA_SIZE + max_inline_ea_size &&
1268             fix_problem(ctx, PR_1_CLEAR_EXTENT_FOR_INLINE_DATA, pctx)) {
1269                 inode->i_flags &= ~EXT4_EXTENTS_FL;
1270                 dirty = 1;
1271                 goto out;
1272         }
1273
1274         /*
1275          * Too big for inline data, but no evidence of extent tree -
1276          * maybe it's a block map file?  If the mappings all look valid?
1277          */
1278         if (could_be_block_map(fs, inode) &&
1279             fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_FLAGS, pctx)) {
1280 #ifdef WORDS_BIGENDIAN
1281                 int i;
1282
1283                 for (i = 0; i < EXT2_N_BLOCKS; i++)
1284                         inode->i_block[i] = ext2fs_swab32(inode->i_block[i]);
1285 #endif
1286
1287                 inode->i_flags &= ~(EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL);
1288                 dirty = 1;
1289                 goto out;
1290         }
1291
1292         /* Oh well, just clear the busted inode. */
1293         if (fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_INODE, pctx)) {
1294                 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1295                 return -1;
1296         }
1297
1298 out:
1299         if (dirty)
1300                 e2fsck_write_inode(ctx, ino, inode, "pass1");
1301
1302         return 0;
1303 }
1304
1305 static void pass1_readahead(e2fsck_t ctx, dgrp_t *group, ext2_ino_t *next_ino)
1306 {
1307         ext2_ino_t inodes_in_group = 0, inodes_per_block, inodes_per_buffer;
1308         dgrp_t start = *group, grp, grp_end = ctx->fs->group_desc_count;
1309         blk64_t blocks_to_read = 0;
1310         errcode_t err = EXT2_ET_INVALID_ARGUMENT;
1311
1312 #ifdef HAVE_PTHREAD
1313         if (ctx->fs->fs_num_threads > 1)
1314                 grp_end = ctx->thread_info.et_group_end;
1315 #endif
1316         if (ctx->readahead_kb == 0)
1317                 goto out;
1318
1319         /* Keep iterating groups until we have enough to readahead */
1320         inodes_per_block = EXT2_INODES_PER_BLOCK(ctx->fs->super);
1321         for (grp = start; grp < grp_end; grp++) {
1322                 if (ext2fs_bg_flags_test(ctx->fs, grp, EXT2_BG_INODE_UNINIT))
1323                         continue;
1324                 inodes_in_group = ctx->fs->super->s_inodes_per_group -
1325                                         ext2fs_bg_itable_unused(ctx->fs, grp);
1326                 blocks_to_read += (inodes_in_group + inodes_per_block - 1) /
1327                                         inodes_per_block;
1328                 if (blocks_to_read * ctx->fs->blocksize >
1329                     ctx->readahead_kb * 1024)
1330                         break;
1331         }
1332
1333         err = e2fsck_readahead(ctx->fs, E2FSCK_READA_ITABLE, start,
1334                                grp - start + 1);
1335         if (err == EAGAIN) {
1336                 ctx->readahead_kb /= 2;
1337                 err = 0;
1338         }
1339
1340 out:
1341         if (err) {
1342                 /* Error; disable itable readahead */
1343                 *group = ctx->fs->group_desc_count;
1344                 *next_ino = ctx->fs->super->s_inodes_count;
1345         } else {
1346                 /*
1347                  * Don't do more readahead until we've reached the first inode
1348                  * of the last inode scan buffer block for the last group.
1349                  */
1350                 *group = grp + 1;
1351                 inodes_per_buffer = (ctx->inode_buffer_blocks ?
1352                                      ctx->inode_buffer_blocks :
1353                                      EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS) *
1354                                     ctx->fs->blocksize /
1355                                     EXT2_INODE_SIZE(ctx->fs->super);
1356                 inodes_in_group--;
1357                 *next_ino = inodes_in_group -
1358                             (inodes_in_group % inodes_per_buffer) + 1 +
1359                             (grp * ctx->fs->super->s_inodes_per_group);
1360         }
1361 }
1362
1363 /*
1364  * Check if the passed ino is one of the used superblock quota inodes.
1365  *
1366  * Before the quota inodes were journaled, older superblock quota inodes
1367  * were just regular files in the filesystem and not reserved inodes.  This
1368  * checks if the passed ino is one of the s_*_quota_inum superblock fields,
1369  * which may not always be the same as the EXT4_*_QUOTA_INO fields.
1370  */
1371 static int quota_inum_is_super(struct ext2_super_block *sb, ext2_ino_t ino)
1372 {
1373         enum quota_type qtype;
1374
1375         for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1376                 if (*quota_sb_inump(sb, qtype) == ino)
1377                         return 1;
1378
1379         return 0;
1380 }
1381
1382 /*
1383  * Check if the passed ino is one of the reserved quota inodes.
1384  * This checks if the inode number is one of the reserved EXT4_*_QUOTA_INO
1385  * inodes.  These inodes may or may not be in use by the quota feature.
1386  */
1387 static int quota_inum_is_reserved(ext2_filsys fs, ext2_ino_t ino)
1388 {
1389         enum quota_type qtype;
1390
1391         for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1392                 if (quota_type2inum(qtype, fs->super) == ino)
1393                         return 1;
1394
1395         return 0;
1396 }
1397
1398 static int e2fsck_should_abort(e2fsck_t ctx)
1399 {
1400         e2fsck_t global_ctx;
1401
1402         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1403                 return 1;
1404
1405         if (ctx->global_ctx) {
1406                 global_ctx = ctx->global_ctx;
1407                 if (global_ctx->flags & E2F_FLAG_SIGNAL_MASK)
1408                         return 1;
1409         }
1410         return 0;
1411 }
1412
1413 static void init_ext2_max_sizes()
1414 {
1415         int     i;
1416         __u64   max_sizes;
1417
1418         /*
1419          * Init ext2_max_sizes which will be immutable and shared between
1420          * threads
1421          */
1422 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
1423
1424         for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
1425                 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
1426                 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
1427                 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
1428                 max_sizes = (max_sizes * (1UL << i));
1429                 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
1430         }
1431 #undef EXT2_BPP
1432 }
1433
1434 #ifdef HAVE_PTHREAD
1435 /* TODO: tdb needs to be handled properly for multiple threads*/
1436 static int multiple_threads_supported(e2fsck_t ctx)
1437 {
1438 #ifdef  CONFIG_TDB
1439         unsigned int            threshold;
1440         ext2_ino_t              num_dirs;
1441         errcode_t               retval;
1442         char                    *tdb_dir;
1443         int                     enable;
1444
1445         profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
1446                            &tdb_dir);
1447         profile_get_uint(ctx->profile, "scratch_files",
1448                          "numdirs_threshold", 0, 0, &threshold);
1449         profile_get_boolean(ctx->profile, "scratch_files",
1450                             "icount", 0, 1, &enable);
1451
1452         retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
1453         if (retval)
1454                 num_dirs = 1024;        /* Guess */
1455
1456         /* tdb is unsupported now */
1457         if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
1458             (!threshold || num_dirs > threshold))
1459                 return 0;
1460 #endif
1461         return 1;
1462 }
1463
1464 /**
1465  * Even though we could specify number of threads,
1466  * but it might be more than the whole filesystem
1467  * block groups, correct it here.
1468  */
1469 static void e2fsck_pass1_set_thread_num(e2fsck_t ctx)
1470 {
1471         unsigned flexbg_size = 1;
1472         ext2_filsys fs = ctx->fs;
1473         int num_threads = ctx->pfs_num_threads;
1474         int max_threads;
1475
1476         if (num_threads < 1) {
1477                 num_threads = 1;
1478                 goto out;
1479         }
1480
1481         if (!multiple_threads_supported(ctx)) {
1482                 num_threads = 1;
1483                 fprintf(stderr, "Fall through single thread for pass1 "
1484                         "because tdb could not handle properly\n");
1485                 goto out;
1486         }
1487
1488         if (ext2fs_has_feature_flex_bg(fs->super))
1489                 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
1490         max_threads = fs->group_desc_count / flexbg_size;
1491         if (max_threads == 0)
1492                 max_threads = 1;
1493         if (max_threads > E2FSCK_MAX_THREADS)
1494                 max_threads = E2FSCK_MAX_THREADS;
1495
1496         if (num_threads > max_threads) {
1497                 fprintf(stderr, "Use max possible thread num: %d instead\n",
1498                                 max_threads);
1499                 num_threads = max_threads;
1500         }
1501 out:
1502         ctx->pfs_num_threads = num_threads;
1503         ctx->fs->fs_num_threads = num_threads;
1504 }
1505 #endif
1506
1507 /*
1508  * We need call mark_table_blocks() before multiple
1509  * thread start, since all known system blocks should be
1510  * marked and checked later.
1511  */
1512 static errcode_t e2fsck_pass1_prepare(e2fsck_t ctx)
1513 {
1514         struct problem_context pctx;
1515         ext2_filsys fs = ctx->fs;
1516         unsigned long long readahead_kb;
1517
1518         init_ext2_max_sizes();
1519 #ifdef HAVE_PTHREAD
1520         e2fsck_pass1_set_thread_num(ctx);
1521 #endif
1522         /* If we can do readahead, figure out how many groups to pull in. */
1523         if (!e2fsck_can_readahead(ctx->fs))
1524                 ctx->readahead_kb = 0;
1525         else if (ctx->readahead_kb == ~0ULL)
1526                 ctx->readahead_kb = e2fsck_guess_readahead(ctx->fs);
1527
1528 #ifdef HAVE_PTHREAD
1529         /* don't use more than 1/10 of memory for threads checking */
1530         readahead_kb = get_memory_size() / (10 * ctx->pfs_num_threads);
1531         /* maybe better disable RA if this is too small? */
1532         if (ctx->readahead_kb > readahead_kb)
1533                 ctx->readahead_kb = readahead_kb;
1534 #endif
1535         clear_problem_context(&pctx);
1536         if (!(ctx->options & E2F_OPT_PREEN))
1537                 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1538
1539         pctx.errcode = e2fsck_allocate_subcluster_bitmap(ctx->fs,
1540                         _("in-use block map"), EXT2FS_BMAP64_RBTREE,
1541                         "block_found_map", &ctx->block_found_map);
1542         if (pctx.errcode) {
1543                 pctx.num = 1;
1544                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1545                 ctx->flags |= E2F_FLAG_ABORT;
1546                 return pctx.errcode;
1547         }
1548         pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1549                         _("metadata block map"), EXT2FS_BMAP64_RBTREE,
1550                         "block_metadata_map", &ctx->block_metadata_map);
1551         if (pctx.errcode) {
1552                 pctx.num = 1;
1553                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1554                 ctx->flags |= E2F_FLAG_ABORT;
1555                 return pctx.errcode;
1556         }
1557
1558         mark_table_blocks(ctx);
1559         pctx.errcode = ext2fs_convert_subcluster_bitmap(ctx->fs,
1560                                                 &ctx->block_found_map);
1561         if (pctx.errcode) {
1562                 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1563                 ctx->flags |= E2F_FLAG_ABORT;
1564                 return pctx.errcode;
1565         }
1566
1567         pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1568                         _("multiply claimed block map"),
1569                         EXT2FS_BMAP64_RBTREE, "block_dup_map",
1570                         &ctx->block_dup_map);
1571         if (pctx.errcode) {
1572                 pctx.num = 3;
1573                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1574                             &pctx);
1575                 /* Should never get here */
1576                 ctx->flags |= E2F_FLAG_ABORT;
1577                 return pctx.errcode;
1578         }
1579
1580         if (ext2fs_has_feature_mmp(fs->super) &&
1581             fs->super->s_mmp_block > fs->super->s_first_data_block &&
1582             fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1583                 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1584                                           fs->super->s_mmp_block);
1585 #ifdef  HAVE_PTHREAD
1586         pthread_rwlock_init(&ctx->fs_fix_rwlock, NULL);
1587         pthread_rwlock_init(&ctx->fs_block_map_rwlock, NULL);
1588         if (ctx->pfs_num_threads > 1)
1589                 ctx->fs_need_locking = 1;
1590 #endif
1591
1592         return 0;
1593 }
1594
1595 static void e2fsck_pass1_post(e2fsck_t ctx)
1596 {
1597         struct problem_context pctx;
1598         ext2_filsys fs = ctx->fs;
1599         char *block_buf;
1600
1601         if (e2fsck_should_abort(ctx))
1602                 return;
1603
1604         block_buf = (char *)e2fsck_allocate_memory(ctx, ctx->fs->blocksize * 3,
1605                                               "block interate buffer");
1606         reserve_block_for_root_repair(ctx);
1607         reserve_block_for_lnf_repair(ctx);
1608
1609         /*
1610          * If any extended attribute blocks' reference counts need to
1611          * be adjusted, either up (ctx->refcount_extra), or down
1612          * (ctx->refcount), then fix them.
1613          */
1614         if (ctx->refcount) {
1615                 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1616                 ea_refcount_free(ctx->refcount);
1617                 ctx->refcount = 0;
1618         }
1619         if (ctx->refcount_extra) {
1620                 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1621                                         block_buf, +1);
1622                 ea_refcount_free(ctx->refcount_extra);
1623                 ctx->refcount_extra = 0;
1624         }
1625
1626         if (ctx->invalid_bitmaps)
1627                 handle_fs_bad_blocks(ctx);
1628
1629         /* We don't need the block_ea_map any more */
1630         if (ctx->block_ea_map) {
1631                 ext2fs_free_block_bitmap(ctx->block_ea_map);
1632                 ctx->block_ea_map = 0;
1633         }
1634
1635         if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1636                 struct ext2_inode *inode;
1637                 int inode_size = EXT2_INODE_SIZE(fs->super);
1638                 inode = e2fsck_allocate_memory(ctx, inode_size,
1639                                                "scratch inode");
1640
1641                 clear_problem_context(&pctx);
1642                 pctx.errcode = ext2fs_create_resize_inode(fs);
1643                 if (pctx.errcode) {
1644                         if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1645                                          &pctx)) {
1646                                 ctx->flags |= E2F_FLAG_ABORT;
1647                                 ext2fs_free_mem(&inode);
1648                                 ext2fs_free_mem(&block_buf);
1649                                 return;
1650                         }
1651                         pctx.errcode = 0;
1652                 }
1653                 if (!pctx.errcode) {
1654                         e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1655                                           "recreate inode");
1656                         inode->i_mtime = ctx->now;
1657                         e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1658                                            "recreate inode");
1659                 }
1660                 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1661                 ext2fs_free_mem(&inode);
1662         }
1663
1664         if (ctx->flags & E2F_FLAG_RESTART) {
1665                 ext2fs_free_mem(&block_buf);
1666                 return;
1667         }
1668
1669         if (ctx->block_dup_map) {
1670                 if (!(ctx->flags & E2F_FLAG_DUP_BLOCK)) {
1671                         ext2fs_free_mem(&block_buf);
1672                         return;
1673                 }
1674                 if (ctx->options & E2F_OPT_PREEN) {
1675                         clear_problem_context(&pctx);
1676                         fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1677                 }
1678                 e2fsck_pass1_dupblocks(ctx, block_buf);
1679                 ext2fs_free_mem(&block_buf);
1680                 ctx->flags &= ~E2F_FLAG_DUP_BLOCK;
1681         }
1682
1683         ctx->flags |= E2F_FLAG_ALLOC_OK;
1684 }
1685
1686
1687 /*
1688  * Lustre FS creates special inodes - precreated objects.
1689  * They are zero-sized and have special attributes:
1690  * mode |= S_ISUID | S_ISGID;
1691  * valid |= LA_ATIME | LA_MTIME | LA_CTIME;
1692  * atime = 0;
1693  * mtime = 0;
1694  * ctime = 0;
1695  */
1696 static int precreated_object(struct ext2_inode *inode)
1697 {
1698         if (((inode->i_mode & (S_ISUID | S_ISGID)) == (S_ISUID | S_ISGID)) &&
1699              inode->i_ctime == 0)
1700                 return 1;
1701         return 0;
1702 }
1703
1704 void e2fsck_pass1_run(e2fsck_t ctx)
1705 {
1706         int     i;
1707         ext2_filsys fs = ctx->fs;
1708         ext2_ino_t      ino = 0;
1709         struct ext2_inode *inode = NULL;
1710         ext2_inode_scan scan = NULL;
1711         char            *block_buf = NULL;
1712 #ifdef RESOURCE_TRACK
1713         struct resource_track   rtrack;
1714 #endif
1715         unsigned char   frag, fsize;
1716         struct          problem_context pctx;
1717         struct          scan_callback_struct scan_struct;
1718         struct ext2_super_block *sb = ctx->fs->super;
1719         const char      *old_op;
1720         const char      *eop_next_inode = _("getting next inode from scan");
1721         int             imagic_fs, extent_fs, inlinedata_fs, casefold_fs;
1722         int             low_dtime_check = 1;
1723         unsigned int    inode_size = EXT2_INODE_SIZE(fs->super);
1724         unsigned int    bufsize;
1725         int             failed_csum = 0;
1726         ext2_ino_t      ino_threshold = 0;
1727         dgrp_t          ra_group = 0;
1728         struct ea_quota ea_ibody_quota;
1729         struct process_inode_block *inodes_to_process;
1730         int             process_inode_count, check_mmp;
1731         e2fsck_t        global_ctx = ctx->global_ctx ? ctx->global_ctx : ctx;
1732         int             inode_exp = 0;
1733
1734         init_resource_track(&rtrack, ctx->fs->io);
1735         clear_problem_context(&pctx);
1736
1737         pass1_readahead(ctx, &ra_group, &ino_threshold);
1738         if (ext2fs_has_feature_dir_index(fs->super) &&
1739             !(ctx->options & E2F_OPT_NO)) {
1740                 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
1741                         ctx->dirs_to_hash = 0;
1742         }
1743
1744 #ifdef MTRACE
1745         mtrace_print("Pass 1");
1746 #endif
1747
1748         imagic_fs = ext2fs_has_feature_imagic_inodes(sb);
1749         extent_fs = ext2fs_has_feature_extents(sb);
1750         inlinedata_fs = ext2fs_has_feature_inline_data(sb);
1751         casefold_fs = ext2fs_has_feature_casefold(sb);
1752
1753         /*
1754          * Allocate bitmaps structures
1755          */
1756         pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
1757                                                     EXT2FS_BMAP64_RBTREE,
1758                                                     "inode_used_map",
1759                                                     &ctx->inode_used_map);
1760         if (pctx.errcode) {
1761                 pctx.num = 1;
1762                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1763                 ctx->flags |= E2F_FLAG_ABORT;
1764                 return;
1765         }
1766         pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1767                         _("directory inode map"),
1768                         ctx->global_ctx ? EXT2FS_BMAP64_RBTREE :
1769                         EXT2FS_BMAP64_AUTODIR,
1770                         "inode_dir_map", &ctx->inode_dir_map);
1771         if (pctx.errcode) {
1772                 pctx.num = 2;
1773                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1774                 ctx->flags |= E2F_FLAG_ABORT;
1775                 return;
1776         }
1777         pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1778                         _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
1779                         "inode_reg_map", &ctx->inode_reg_map);
1780         if (pctx.errcode) {
1781                 pctx.num = 6;
1782                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1783                 ctx->flags |= E2F_FLAG_ABORT;
1784                 return;
1785         }
1786         if (casefold_fs) {
1787                 pctx.errcode =
1788                         e2fsck_allocate_inode_bitmap(fs,
1789                                                      _("inode casefold map"),
1790                                                      EXT2FS_BMAP64_RBTREE,
1791                                                      "inode_casefold_map",
1792                                                      &ctx->inode_casefold_map);
1793                 if (pctx.errcode) {
1794                         pctx.num = 1;
1795                         fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1796                         ctx->flags |= E2F_FLAG_ABORT;
1797                         return;
1798                 }
1799         }
1800         pctx.errcode = e2fsck_setup_icount(ctx, "inode_link_info", 0, NULL,
1801                                            &ctx->inode_link_info);
1802         if (pctx.errcode) {
1803                 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1804                 ctx->flags |= E2F_FLAG_ABORT;
1805                 return;
1806         }
1807         bufsize = inode_size;
1808         if (bufsize < sizeof(struct ext2_inode_large))
1809                 bufsize = sizeof(struct ext2_inode_large);
1810         inode = (struct ext2_inode *)
1811                 e2fsck_allocate_memory(ctx, bufsize, "scratch inode");
1812
1813         inodes_to_process = (struct process_inode_block *)
1814                 e2fsck_allocate_memory(ctx,
1815                                        (ctx->process_inode_size *
1816                                         sizeof(struct process_inode_block)),
1817                                        "array of inodes to process");
1818         process_inode_count = 0;
1819
1820         pctx.errcode = ext2fs_init_dblist(fs, 0);
1821         if (pctx.errcode) {
1822                 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1823                 ctx->flags |= E2F_FLAG_ABORT;
1824                 goto endit;
1825         }
1826
1827         /*
1828          * If the last orphan field is set, clear it, since the pass1
1829          * processing will automatically find and clear the orphans.
1830          * In the future, we may want to try using the last_orphan
1831          * linked list ourselves, but for now, we clear it so that the
1832          * ext3 mount code won't get confused.
1833          */
1834         if (!(ctx->options & E2F_OPT_READONLY)) {
1835                 if (fs->super->s_last_orphan) {
1836                         fs->super->s_last_orphan = 0;
1837                         ext2fs_mark_super_dirty(fs);
1838                 }
1839         }
1840
1841         block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1842                                                     "block interate buffer");
1843         if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1844                 e2fsck_use_inode_shortcuts(ctx, 1);
1845         e2fsck_intercept_block_allocations(ctx);
1846         old_op = ehandler_operation(_("opening inode scan"));
1847         pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1848                                               &scan);
1849         ehandler_operation(old_op);
1850         if (pctx.errcode) {
1851                 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1852                 ctx->flags |= E2F_FLAG_ABORT;
1853                 goto endit;
1854         }
1855         ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE |
1856                                       EXT2_SF_WARN_GARBAGE_INODES, 0);
1857         ctx->stashed_inode = inode;
1858         scan_struct.ctx = ctx;
1859         scan_struct.block_buf = block_buf;
1860         scan_struct.inodes_to_process = inodes_to_process;
1861         scan_struct.process_inode_count = &process_inode_count;
1862         ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1863         if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1864                                               ctx->fs->group_desc_count)))
1865                 goto endit;
1866         if ((fs->super->s_wtime &&
1867              fs->super->s_wtime < fs->super->s_inodes_count) ||
1868             (fs->super->s_mtime &&
1869              fs->super->s_mtime < fs->super->s_inodes_count) ||
1870             (fs->super->s_mkfs_time &&
1871              fs->super->s_mkfs_time < fs->super->s_inodes_count))
1872                 low_dtime_check = 0;
1873
1874         /* Set up ctx->lost_and_found if possible */
1875         (void) e2fsck_get_lost_and_found(ctx, 0);
1876
1877 #ifdef HAVE_PTHREAD
1878         if (ctx->global_ctx) {
1879                 if (ctx->options & E2F_OPT_DEBUG &&
1880                     ctx->options & E2F_OPT_MULTITHREAD)
1881                         log_out(ctx, "jumping to group %u\n",
1882                                 ctx->thread_info.et_group_start);
1883                 pctx.errcode = ext2fs_inode_scan_goto_blockgroup(scan,
1884                                         ctx->thread_info.et_group_start);
1885                 if (pctx.errcode) {
1886                         fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1887                         ctx->flags |= E2F_FLAG_ABORT;
1888                         goto endit;
1889                 }
1890         }
1891 #endif
1892
1893         while (1) {
1894                 check_mmp = 0;
1895                 e2fsck_pass1_check_lock(ctx);
1896 #ifdef  HAVE_PTHREAD
1897                 if (!global_ctx->mmp_update_thread) {
1898                         e2fsck_pass1_block_map_w_lock(ctx);
1899                         if (!global_ctx->mmp_update_thread) {
1900                                 global_ctx->mmp_update_thread =
1901                                         ctx->thread_info.et_thread_index + 1;
1902                                 check_mmp = 1;
1903                         }
1904                         e2fsck_pass1_block_map_w_unlock(ctx);
1905                 }
1906
1907                 /* only one active thread could update mmp block. */
1908                 e2fsck_pass1_block_map_r_lock(ctx);
1909                 if (global_ctx->mmp_update_thread ==
1910                     ctx->thread_info.et_thread_index + 1)
1911                         check_mmp = 1;
1912                 e2fsck_pass1_block_map_r_unlock(ctx);
1913 #else
1914                 check_mmp = 1;
1915 #endif
1916
1917                 if (check_mmp && (ino % (fs->super->s_inodes_per_group * 4) == 1)) {
1918                         if (e2fsck_mmp_update(fs))
1919                                 fatal_error(ctx, 0);
1920                 }
1921                 old_op = ehandler_operation(eop_next_inode);
1922                 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1923                                                           inode, inode_size);
1924                 if (ino > ino_threshold)
1925                         pass1_readahead(ctx, &ra_group, &ino_threshold);
1926                 ehandler_operation(old_op);
1927                 if (e2fsck_should_abort(ctx)) {
1928                         e2fsck_pass1_check_unlock(ctx);
1929                         goto endit;
1930                 }
1931                 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1932                         /*
1933                          * If badblocks says badblocks is bad, offer to clear
1934                          * the list, update the in-core bb list, and restart
1935                          * the inode scan.
1936                          */
1937                         if (ino == EXT2_BAD_INO &&
1938                             fix_problem(ctx, PR_1_BADBLOCKS_IN_BADBLOCKS,
1939                                         &pctx)) {
1940                                 errcode_t err;
1941
1942                                 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1943                                 ext2fs_badblocks_list_free(ctx->fs->badblocks);
1944                                 ctx->fs->badblocks = NULL;
1945                                 err = ext2fs_read_bb_inode(ctx->fs,
1946                                                         &ctx->fs->badblocks);
1947                                 if (err) {
1948                                         fix_problem(ctx, PR_1_ISCAN_ERROR,
1949                                                     &pctx);
1950                                         ctx->flags |= E2F_FLAG_ABORT;
1951                                         e2fsck_pass1_check_unlock(ctx);
1952                                         goto endit;
1953                                 } else
1954                                         ctx->flags |= E2F_FLAG_RESTART;
1955                                 err = ext2fs_inode_scan_goto_blockgroup(scan,
1956                                                                         0);
1957                                 if (err) {
1958                                         fix_problem(ctx, PR_1_ISCAN_ERROR,
1959                                                     &pctx);
1960                                         ctx->flags |= E2F_FLAG_ABORT;
1961                                         e2fsck_pass1_check_unlock(ctx);
1962                                         goto endit;
1963                                 }
1964                                 e2fsck_pass1_check_unlock(ctx);
1965                                 continue;
1966                         }
1967                         if (!ctx->inode_bb_map)
1968                                 alloc_bb_map(ctx);
1969                         ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1970                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1971                         e2fsck_pass1_check_unlock(ctx);
1972                         continue;
1973                 }
1974                 if (pctx.errcode == EXT2_ET_SCAN_FINISHED) {
1975                         e2fsck_pass1_check_unlock(ctx);
1976                         break;
1977                 }
1978                 if (pctx.errcode &&
1979                     pctx.errcode != EXT2_ET_INODE_CSUM_INVALID &&
1980                     pctx.errcode != EXT2_ET_INODE_IS_GARBAGE) {
1981                         fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1982                         ctx->flags |= E2F_FLAG_ABORT;
1983                         e2fsck_pass1_check_unlock(ctx);
1984                         goto endit;
1985                 }
1986                 if (!ino) {
1987                         e2fsck_pass1_check_unlock(ctx);
1988                         break;
1989                 }
1990 #ifdef HAVE_PTHREAD
1991                 if (ctx->global_ctx)
1992                         ctx->thread_info.et_inode_number++;
1993 #endif
1994                 pctx.ino = ino;
1995                 pctx.inode = inode;
1996                 ctx->stashed_ino = ino;
1997
1998                 /* Clear trashed inode? */
1999                 if (pctx.errcode == EXT2_ET_INODE_IS_GARBAGE &&
2000                     inode->i_links_count > 0 &&
2001                     fix_problem(ctx, PR_1_INODE_IS_GARBAGE, &pctx)) {
2002                         pctx.errcode = 0;
2003                         e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2004                 }
2005                 failed_csum = pctx.errcode != 0;
2006
2007                 /*
2008                  * Check for inodes who might have been part of the
2009                  * orphaned list linked list.  They should have gotten
2010                  * dealt with by now, unless the list had somehow been
2011                  * corrupted.
2012                  *
2013                  * FIXME: In the future, inodes which are still in use
2014                  * (and which are therefore) pending truncation should
2015                  * be handled specially.  Right now we just clear the
2016                  * dtime field, and the normal e2fsck handling of
2017                  * inodes where i_size and the inode blocks are
2018                  * inconsistent is to fix i_size, instead of releasing
2019                  * the extra blocks.  This won't catch the inodes that
2020                  * was at the end of the orphan list, but it's better
2021                  * than nothing.  The right answer is that there
2022                  * shouldn't be any bugs in the orphan list handling.  :-)
2023                  */
2024                 if (inode->i_dtime && low_dtime_check &&
2025                     inode->i_dtime < ctx->fs->super->s_inodes_count) {
2026                         if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
2027                                 inode->i_dtime = inode->i_links_count ?
2028                                         0 : ctx->now;
2029                                 e2fsck_write_inode(ctx, ino, inode,
2030                                                    "pass1");
2031                                 failed_csum = 0;
2032                         }
2033                 }
2034
2035                 if (inode->i_links_count) {
2036                         pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
2037                                            ino, inode->i_links_count);
2038                         if (pctx.errcode) {
2039                                 pctx.num = inode->i_links_count;
2040                                 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
2041                                 ctx->flags |= E2F_FLAG_ABORT;
2042                                 e2fsck_pass1_check_unlock(ctx);
2043                                 goto endit;
2044                         }
2045                 } else if ((ino >= EXT2_FIRST_INODE(fs->super)) &&
2046                            !quota_inum_is_reserved(fs, ino)) {
2047                         if (!inode->i_dtime && inode->i_mode) {
2048                                 if (fix_problem(ctx,
2049                                             PR_1_ZERO_DTIME, &pctx)) {
2050                                         inode->i_dtime = ctx->now;
2051                                         e2fsck_write_inode(ctx, ino, inode,
2052                                                            "pass1");
2053                                         failed_csum = 0;
2054                                 }
2055                         }
2056                         FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2057                         e2fsck_pass1_check_unlock(ctx);
2058                         continue;
2059                 }
2060
2061                 if ((inode->i_flags & EXT4_CASEFOLD_FL) &&
2062                     ((!LINUX_S_ISDIR(inode->i_mode) &&
2063                       fix_problem(ctx, PR_1_CASEFOLD_NONDIR, &pctx)) ||
2064                      (!casefold_fs &&
2065                       fix_problem(ctx, PR_1_CASEFOLD_FEATURE, &pctx)))) {
2066                         inode->i_flags &= ~EXT4_CASEFOLD_FL;
2067                         e2fsck_write_inode(ctx, ino, inode, "pass1");
2068                 }
2069
2070                 /* Conflicting inlinedata/extents inode flags? */
2071                 if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
2072                     (inode->i_flags & EXT4_EXTENTS_FL)) {
2073                         int res = fix_inline_data_extents_file(ctx, ino, inode,
2074                                                                inode_size,
2075                                                                &pctx);
2076                         if (res < 0) {
2077                                 /* skip FINISH_INODE_LOOP */
2078                                 e2fsck_pass1_check_unlock(ctx);
2079                                 continue;
2080                         }
2081                 }
2082
2083                 /* Test for incorrect inline_data flags settings. */
2084                 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && !inlinedata_fs &&
2085                     (ino >= EXT2_FIRST_INODE(fs->super))) {
2086                         size_t size = 0;
2087
2088                         pctx.errcode = get_inline_data_ea_size(fs, ino, &size);
2089                         if (!pctx.errcode &&
2090                             fix_problem(ctx, PR_1_INLINE_DATA_FEATURE, &pctx)) {
2091                                 e2fsck_pass1_fix_lock(ctx);
2092                                 ext2fs_set_feature_inline_data(sb);
2093                                 ext2fs_mark_super_dirty(fs);
2094                                 e2fsck_pass1_fix_unlock(ctx);
2095                                 inlinedata_fs = 1;
2096                         } else if (fix_problem(ctx, PR_1_INLINE_DATA_SET, &pctx)) {
2097                                 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2098                                 /* skip FINISH_INODE_LOOP */
2099                                 e2fsck_pass1_check_unlock(ctx);
2100                                 continue;
2101                         }
2102                 }
2103
2104                 /* Test for inline data flag but no attr */
2105                 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && inlinedata_fs &&
2106                     (ino >= EXT2_FIRST_INODE(fs->super))) {
2107                         size_t size = 0;
2108                         errcode_t err;
2109                         int flags;
2110
2111                         flags = fs->flags;
2112                         if (failed_csum)
2113                                 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2114                         err = get_inline_data_ea_size(fs, ino, &size);
2115                         fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
2116                                     (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
2117
2118                         switch (err) {
2119                         case 0:
2120                                 /* Everything is awesome... */
2121                                 break;
2122                         case EXT2_ET_BAD_EA_BLOCK_NUM:
2123                         case EXT2_ET_BAD_EA_HASH:
2124                         case EXT2_ET_BAD_EA_HEADER:
2125                         case EXT2_ET_EA_BAD_NAME_LEN:
2126                         case EXT2_ET_EA_BAD_VALUE_SIZE:
2127                         case EXT2_ET_EA_KEY_NOT_FOUND:
2128                         case EXT2_ET_EA_NO_SPACE:
2129                         case EXT2_ET_MISSING_EA_FEATURE:
2130                         case EXT2_ET_INLINE_DATA_CANT_ITERATE:
2131                         case EXT2_ET_INLINE_DATA_NO_BLOCK:
2132                         case EXT2_ET_INLINE_DATA_NO_SPACE:
2133                         case EXT2_ET_NO_INLINE_DATA:
2134                         case EXT2_ET_EXT_ATTR_CSUM_INVALID:
2135                         case EXT2_ET_EA_BAD_VALUE_OFFSET:
2136                         case EXT2_ET_EA_INODE_CORRUPTED:
2137                                 /* broken EA or no system.data EA; truncate */
2138                                 if (fix_problem(ctx, PR_1_INLINE_DATA_NO_ATTR,
2139                                                 &pctx)) {
2140                                         err = ext2fs_inode_size_set(fs, inode, 0);
2141                                         if (err) {
2142                                                 pctx.errcode = err;
2143                                                 ctx->flags |= E2F_FLAG_ABORT;
2144                                                 e2fsck_pass1_check_unlock(ctx);
2145                                                 goto endit;
2146                                         }
2147                                         inode->i_flags &= ~EXT4_INLINE_DATA_FL;
2148                                         memset(&inode->i_block, 0,
2149                                                sizeof(inode->i_block));
2150                                         e2fsck_write_inode(ctx, ino, inode,
2151                                                            "pass1");
2152                                         failed_csum = 0;
2153                                 }
2154                                 break;
2155                         default:
2156                                 /* Some other kind of non-xattr error? */
2157                                 pctx.errcode = err;
2158                                 ctx->flags |= E2F_FLAG_ABORT;
2159                                 e2fsck_pass1_check_unlock(ctx);
2160                                 goto endit;
2161                         }
2162                 }
2163
2164                 /*
2165                  * Test for incorrect extent flag settings.
2166                  *
2167                  * On big-endian machines we must be careful:
2168                  * When the inode is read, the i_block array is not swapped
2169                  * if the extent flag is set.  Therefore if we are testing
2170                  * for or fixing a wrongly-set flag, we must potentially
2171                  * (un)swap before testing, or after fixing.
2172                  */
2173
2174                 /*
2175                  * In this case the extents flag was set when read, so
2176                  * extent_header_verify is ok.  If the inode is cleared,
2177                  * no need to swap... so no extra swapping here.
2178                  */
2179                 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
2180                     (inode->i_links_count || (ino == EXT2_BAD_INO) ||
2181                      (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
2182                         if ((ext2fs_extent_header_verify(inode->i_block,
2183                                                  sizeof(inode->i_block)) == 0) &&
2184                             fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
2185                                 e2fsck_pass1_fix_lock(ctx);
2186                                 ext2fs_set_feature_extents(sb);
2187                                 ext2fs_mark_super_dirty(fs);
2188                                 extent_fs = 1;
2189                                 e2fsck_pass1_fix_unlock(ctx);
2190                         } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
2191                         clear_inode:
2192                                 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2193                                 if (ino == EXT2_BAD_INO)
2194                                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
2195                                                                  ino);
2196                                 /* skip FINISH_INODE_LOOP */
2197                                 e2fsck_pass1_check_unlock(ctx);
2198                                 continue;
2199                         }
2200                 }
2201
2202                 /*
2203                  * For big-endian machines:
2204                  * If the inode didn't have the extents flag set when it
2205                  * was read, then the i_blocks array was swapped.  To test
2206                  * as an extents header, we must swap it back first.
2207                  * IF we then set the extents flag, the entire i_block
2208                  * array must be un/re-swapped to make it proper extents data.
2209                  */
2210                 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
2211                     (inode->i_links_count || (ino == EXT2_BAD_INO) ||
2212                      (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
2213                     (LINUX_S_ISREG(inode->i_mode) ||
2214                      LINUX_S_ISDIR(inode->i_mode))) {
2215                         void *ehp;
2216 #ifdef WORDS_BIGENDIAN
2217                         __u32 tmp_block[EXT2_N_BLOCKS];
2218
2219                         for (i = 0; i < EXT2_N_BLOCKS; i++)
2220                                 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
2221                         ehp = tmp_block;
2222 #else
2223                         ehp = inode->i_block;
2224 #endif
2225                         if ((ext2fs_extent_header_verify(ehp,
2226                                          sizeof(inode->i_block)) == 0) &&
2227                             (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
2228                                 inode->i_flags |= EXT4_EXTENTS_FL;
2229 #ifdef WORDS_BIGENDIAN
2230                                 memcpy(inode->i_block, tmp_block,
2231                                        sizeof(inode->i_block));
2232 #endif
2233                                 e2fsck_write_inode(ctx, ino, inode, "pass1");
2234                                 failed_csum = 0;
2235                         }
2236                 }
2237
2238                 if (ino == EXT2_BAD_INO) {
2239                         struct process_block_struct pb;
2240
2241                         if ((failed_csum || inode->i_mode || inode->i_uid ||
2242                              inode->i_gid || inode->i_links_count ||
2243                              (inode->i_flags & EXT4_INLINE_DATA_FL) ||
2244                              inode->i_file_acl) &&
2245                             fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
2246                                 memset(inode, 0, sizeof(struct ext2_inode));
2247                                 e2fsck_write_inode(ctx, ino, inode,
2248                                                    "clear bad inode");
2249                                 failed_csum = 0;
2250                         }
2251
2252                         e2fsck_pass1_block_map_r_lock(ctx);
2253                         pctx.errcode = ext2fs_copy_bitmap(ctx->global_ctx ?
2254                                         ctx->global_ctx->block_found_map :
2255                                         ctx->block_found_map, &pb.fs_meta_blocks);
2256                         e2fsck_pass1_block_map_r_unlock(ctx);
2257                         if (pctx.errcode) {
2258                                 pctx.num = 4;
2259                                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
2260                                 ctx->flags |= E2F_FLAG_ABORT;
2261                                 e2fsck_pass1_check_unlock(ctx);
2262                                 goto endit;
2263                         }
2264                         pb.ino = EXT2_BAD_INO;
2265                         pb.num_blocks = pb.last_block = 0;
2266                         pb.last_db_block = -1;
2267                         pb.num_illegal_blocks = 0;
2268                         pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
2269                         pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
2270                         pb.inode = inode;
2271                         pb.pctx = &pctx;
2272                         pb.ctx = ctx;
2273                         pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
2274                                      block_buf, process_bad_block, &pb);
2275                         ext2fs_free_block_bitmap(pb.fs_meta_blocks);
2276                         if (pctx.errcode) {
2277                                 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
2278                                 ctx->flags |= E2F_FLAG_ABORT;
2279                                 e2fsck_pass1_check_unlock(ctx);
2280                                 goto endit;
2281                         }
2282                         if (pb.bbcheck)
2283                                 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
2284                                 ctx->flags |= E2F_FLAG_ABORT;
2285                                 e2fsck_pass1_check_unlock(ctx);
2286                                 goto endit;
2287                         }
2288                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2289                         clear_problem_context(&pctx);
2290                         FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2291                         e2fsck_pass1_check_unlock(ctx);
2292                         continue;
2293                 } else if (ino == EXT2_ROOT_INO) {
2294                         /*
2295                          * Make sure the root inode is a directory; if
2296                          * not, offer to clear it.  It will be
2297                          * regenerated in pass #3.
2298                          */
2299                         if (!LINUX_S_ISDIR(inode->i_mode)) {
2300                                 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
2301                                         goto clear_inode;
2302                         }
2303                         /*
2304                          * If dtime is set, offer to clear it.  mke2fs
2305                          * version 0.2b created filesystems with the
2306                          * dtime field set for the root and lost+found
2307                          * directories.  We won't worry about
2308                          * /lost+found, since that can be regenerated
2309                          * easily.  But we will fix the root directory
2310                          * as a special case.
2311                          */
2312                         if (inode->i_dtime && inode->i_links_count) {
2313                                 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
2314                                         inode->i_dtime = 0;
2315                                         e2fsck_write_inode(ctx, ino, inode,
2316                                                            "pass1");
2317                                         failed_csum = 0;
2318                                 }
2319                         }
2320                 } else if (ino == EXT2_JOURNAL_INO) {
2321                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2322                         if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
2323                                 if (!LINUX_S_ISREG(inode->i_mode) &&
2324                                     fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
2325                                                 &pctx)) {
2326                                         inode->i_mode = LINUX_S_IFREG;
2327                                         e2fsck_write_inode(ctx, ino, inode,
2328                                                            "pass1");
2329                                         failed_csum = 0;
2330                                 }
2331                                 check_blocks(ctx, &pctx, block_buf, NULL);
2332                                 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2333                                 e2fsck_pass1_check_unlock(ctx);
2334                                 continue;
2335                         }
2336                         if ((inode->i_links_count ||
2337                              inode->i_blocks || inode->i_block[0]) &&
2338                             fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
2339                                         &pctx)) {
2340                                 memset(inode, 0, inode_size);
2341                                 ext2fs_icount_store(ctx->inode_link_info,
2342                                                     ino, 0);
2343                                 e2fsck_write_inode_full(ctx, ino, inode,
2344                                                         inode_size, "pass1");
2345                                 failed_csum = 0;
2346                         }
2347                 } else if (quota_inum_is_reserved(fs, ino)) {
2348                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2349                         if (ext2fs_has_feature_quota(fs->super) &&
2350                             quota_inum_is_super(fs->super, ino)) {
2351                                 if (!LINUX_S_ISREG(inode->i_mode) &&
2352                                     fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
2353                                                         &pctx)) {
2354                                         inode->i_mode = LINUX_S_IFREG;
2355                                         e2fsck_write_inode(ctx, ino, inode,
2356                                                         "pass1");
2357                                         failed_csum = 0;
2358                                 }
2359                                 check_blocks(ctx, &pctx, block_buf, NULL);
2360                                 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2361                                 e2fsck_pass1_check_unlock(ctx);
2362                                 continue;
2363                         }
2364                         if ((inode->i_links_count ||
2365                              inode->i_blocks || inode->i_block[0]) &&
2366                             fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
2367                                         &pctx)) {
2368                                 memset(inode, 0, inode_size);
2369                                 ext2fs_icount_store(ctx->inode_link_info,
2370                                                     ino, 0);
2371                                 e2fsck_write_inode_full(ctx, ino, inode,
2372                                                         inode_size, "pass1");
2373                                 failed_csum = 0;
2374                         }
2375                 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
2376                         problem_t problem = 0;
2377
2378                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2379                         if (ino == EXT2_BOOT_LOADER_INO) {
2380                                 if (LINUX_S_ISDIR(inode->i_mode))
2381                                         problem = PR_1_RESERVED_BAD_MODE;
2382                         } else if (ino == EXT2_RESIZE_INO) {
2383                                 if (inode->i_mode &&
2384                                     !LINUX_S_ISREG(inode->i_mode))
2385                                         problem = PR_1_RESERVED_BAD_MODE;
2386                         } else {
2387                                 if (inode->i_mode != 0)
2388                                         problem = PR_1_RESERVED_BAD_MODE;
2389                         }
2390                         if (problem) {
2391                                 if (fix_problem(ctx, problem, &pctx)) {
2392                                         inode->i_mode = 0;
2393                                         e2fsck_write_inode(ctx, ino, inode,
2394                                                            "pass1");
2395                                         failed_csum = 0;
2396                                 }
2397                         }
2398                         check_blocks(ctx, &pctx, block_buf, NULL);
2399                         FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2400                         e2fsck_pass1_check_unlock(ctx);
2401                         continue;
2402                 }
2403
2404                 if (!inode->i_links_count) {
2405                         FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2406                         e2fsck_pass1_check_unlock(ctx);
2407                         continue;
2408                 }
2409                 /*
2410                  * n.b.  0.3c ext2fs code didn't clear i_links_count for
2411                  * deleted files.  Oops.
2412                  *
2413                  * Since all new ext2 implementations get this right,
2414                  * we now assume that the case of non-zero
2415                  * i_links_count and non-zero dtime means that we
2416                  * should keep the file, not delete it.
2417                  *
2418                  */
2419                 if (inode->i_dtime) {
2420                         if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
2421                                 inode->i_dtime = 0;
2422                                 e2fsck_write_inode(ctx, ino, inode, "pass1");
2423                                 failed_csum = 0;
2424                         }
2425                 }
2426
2427                 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2428                 switch (fs->super->s_creator_os) {
2429                     case EXT2_OS_HURD:
2430                         frag = inode->osd2.hurd2.h_i_frag;
2431                         fsize = inode->osd2.hurd2.h_i_fsize;
2432                         break;
2433                     default:
2434                         frag = fsize = 0;
2435                 }
2436
2437                 /* Fixed in pass2, e2fsck_process_bad_inode(). */
2438                 if (inode->i_faddr || frag || fsize ||
2439                     (!ext2fs_has_feature_largedir(fs->super) &&
2440                      LINUX_S_ISDIR(inode->i_mode) && inode->i_size_high))
2441                         e2fsck_mark_inode_bad(ctx, &pctx,
2442                                               PR_2_DIR_SIZE_HIGH_ZERO);
2443                 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2444                     !ext2fs_has_feature_64bit(fs->super) &&
2445                     inode->osd2.linux2.l_i_file_acl_high != 0)
2446                         e2fsck_mark_inode_bad(ctx, &pctx,
2447                                               PR_2_I_FILE_ACL_HI_ZERO);
2448                 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2449                     !ext2fs_has_feature_huge_file(fs->super) &&
2450                     (inode->osd2.linux2.l_i_blocks_hi != 0))
2451                         e2fsck_mark_inode_bad(ctx, &pctx, PR_2_BLOCKS_HI_ZERO);
2452                 if (inode->i_flags & EXT2_IMAGIC_FL) {
2453                         if (imagic_fs) {
2454                                 if (!ctx->inode_imagic_map)
2455                                         alloc_imagic_map(ctx);
2456                                 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
2457                                                          ino);
2458                         } else {
2459                                 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
2460                                         inode->i_flags &= ~EXT2_IMAGIC_FL;
2461                                         e2fsck_write_inode(ctx, ino,
2462                                                            inode, "pass1");
2463                                         failed_csum = 0;
2464                                 }
2465                         }
2466                 }
2467
2468                 check_inode_extra_space(ctx, &pctx, &ea_ibody_quota);
2469                 check_is_really_dir(ctx, &pctx, block_buf);
2470
2471                 /*
2472                  * ext2fs_inode_has_valid_blocks2 does not actually look
2473                  * at i_block[] values, so not endian-sensitive here.
2474                  */
2475                 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
2476                     LINUX_S_ISLNK(inode->i_mode) &&
2477                     !ext2fs_inode_has_valid_blocks2(fs, inode) &&
2478                     fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
2479                         inode->i_flags &= ~EXT4_EXTENTS_FL;
2480                         e2fsck_write_inode(ctx, ino, inode, "pass1");
2481                         failed_csum = 0;
2482                 }
2483
2484                 if ((inode->i_flags & EXT4_ENCRYPT_FL) &&
2485                     add_encrypted_file(ctx, &pctx) < 0)
2486                         goto clear_inode;
2487
2488                 if (casefold_fs && inode->i_flags & EXT4_CASEFOLD_FL)
2489                         ext2fs_mark_inode_bitmap2(ctx->inode_casefold_map, ino);
2490
2491                 if (LINUX_S_ISDIR(inode->i_mode)) {
2492                         ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
2493                         e2fsck_add_dir_info(ctx, ino, 0);
2494                         ctx->fs_directory_count++;
2495                         if (inode->i_flags & EXT4_CASEFOLD_FL)
2496                                 add_casefolded_dir(ctx, ino);
2497                 } else if (LINUX_S_ISREG (inode->i_mode)) {
2498                         ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
2499                         ctx->fs_regular_count++;
2500                 } else if (LINUX_S_ISCHR (inode->i_mode) &&
2501                            e2fsck_pass1_check_device_inode(fs, inode)) {
2502                         check_extents_inlinedata(ctx, &pctx);
2503                         check_immutable(ctx, &pctx);
2504                         check_size(ctx, &pctx);
2505                         ctx->fs_chardev_count++;
2506                 } else if (LINUX_S_ISBLK (inode->i_mode) &&
2507                            e2fsck_pass1_check_device_inode(fs, inode)) {
2508                         check_extents_inlinedata(ctx, &pctx);
2509                         check_immutable(ctx, &pctx);
2510                         check_size(ctx, &pctx);
2511                         ctx->fs_blockdev_count++;
2512                 } else if (LINUX_S_ISLNK (inode->i_mode) &&
2513                            check_symlink(ctx, &pctx, ino, inode, block_buf)) {
2514                         check_immutable(ctx, &pctx);
2515                         ctx->fs_symlinks_count++;
2516                         if (inode->i_flags & EXT4_INLINE_DATA_FL) {
2517                                 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2518                                 e2fsck_pass1_check_unlock(ctx);
2519                                 continue;
2520                         } else if (ext2fs_is_fast_symlink(inode)) {
2521                                 ctx->fs_fast_symlinks_count++;
2522                                 check_blocks(ctx, &pctx, block_buf,
2523                                              &ea_ibody_quota);
2524                                 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2525                                 e2fsck_pass1_check_unlock(ctx);
2526                                 continue;
2527                         }
2528                 }
2529                 else if (LINUX_S_ISFIFO (inode->i_mode) &&
2530                          e2fsck_pass1_check_device_inode(fs, inode)) {
2531                         check_extents_inlinedata(ctx, &pctx);
2532                         check_immutable(ctx, &pctx);
2533                         check_size(ctx, &pctx);
2534                         ctx->fs_fifo_count++;
2535                 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
2536                            e2fsck_pass1_check_device_inode(fs, inode)) {
2537                         check_extents_inlinedata(ctx, &pctx);
2538                         check_immutable(ctx, &pctx);
2539                         check_size(ctx, &pctx);
2540                         ctx->fs_sockets_count++;
2541                 } else {
2542                         e2fsck_mark_inode_bad(ctx, &pctx, PR_2_BAD_MODE);
2543                 }
2544
2545                 /* Future atime/mtime may be valid in rare cases, but are more
2546                  * likely to indicate corruption.  Don't try to fix timestamps,
2547                  * but take into consideration whether inode is corrupted.  If
2548                  * no other problems with the inode, probably it is OK. */
2549                 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_atime, ctx->time_fudge))
2550                         e2fsck_mark_inode_bad(ctx, &pctx, PR_1_INODE_BAD_TIME);
2551                 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_mtime, ctx->time_fudge))
2552                         e2fsck_mark_inode_bad(ctx, &pctx, PR_1_INODE_BAD_TIME);
2553
2554                 /* Since ctime cannot be set directly from userspace, consider
2555                  * very old/future values worse than a bad atime/mtime. Same for
2556                  * crtime, but it is checked in check_inode_extra_space(). */
2557                 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_ctime, ctx->time_fudge))
2558                         e2fsck_mark_inode_badder(ctx, &pctx,
2559                                                  PR_1_INODE_BAD_TIME);
2560                 else if (!precreated_object(inode) &&
2561                          EXT4_XTIME_ANCIENT(ctx, sb, inode->i_ctime,
2562                                             ctx->time_fudge))
2563                         e2fsck_mark_inode_badder(ctx, &pctx,
2564                                                  PR_1_INODE_BAD_TIME);
2565
2566                 /* no restart if clearing bad inode before block processing */
2567                 if (e2fsck_fix_bad_inode(ctx, &pctx)) {
2568                         e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2569                         goto next_unlock;
2570                 }
2571
2572                 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2573                     !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
2574                         if (inode->i_block[EXT2_IND_BLOCK])
2575                                 ctx->fs_ind_count++;
2576                         if (inode->i_block[EXT2_DIND_BLOCK])
2577                                 ctx->fs_dind_count++;
2578                         if (inode->i_block[EXT2_TIND_BLOCK])
2579                                 ctx->fs_tind_count++;
2580                 }
2581                 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2582                     !(inode->i_flags & EXT4_INLINE_DATA_FL) &&
2583                     (inode->i_block[EXT2_IND_BLOCK] ||
2584                      inode->i_block[EXT2_DIND_BLOCK] ||
2585                      inode->i_block[EXT2_TIND_BLOCK] ||
2586                      ext2fs_file_acl_block(fs, inode))) {
2587                         struct process_inode_block *itp;
2588
2589                         itp = &inodes_to_process[process_inode_count];
2590                         itp->ino = ino;
2591                         itp->ea_ibody_quota = ea_ibody_quota;
2592                         if (inode_size < sizeof(struct ext2_inode_large))
2593                                 memcpy(&itp->inode, inode, inode_size);
2594                         else
2595                                 memcpy(&itp->inode, inode, sizeof(itp->inode));
2596                         process_inode_count++;
2597                 } else
2598                         check_blocks(ctx, &pctx, block_buf, &ea_ibody_quota);
2599
2600                 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2601
2602                 if (ctx->flags & E2F_FLAG_EXPAND_EISIZE) {
2603                         struct ext2_inode_large *inode_l;
2604
2605                         inode_l = (struct ext2_inode_large *)inode;
2606
2607                         if (inode_l->i_extra_isize < ctx->want_extra_isize) {
2608                                 fix_problem(ctx, PR_1_EXPAND_EISIZE, &pctx);
2609                                 inode_exp = e2fsck_pass1_expand_eisize(ctx,
2610                                                                        inode_l,
2611                                                                        &pctx);
2612                         }
2613                         if ((inode_l->i_extra_isize < ctx->min_extra_isize) &&
2614                             inode_exp == 0)
2615                                 ctx->min_extra_isize = inode_l->i_extra_isize;
2616                 }
2617
2618                 if (e2fsck_should_abort(ctx)) {
2619                         e2fsck_pass1_check_unlock(ctx);
2620                         goto endit;
2621                 }
2622
2623                 if (process_inode_count >= ctx->process_inode_size) {
2624                         process_inodes(ctx, block_buf, inodes_to_process,
2625                                        &process_inode_count);
2626
2627                         if (e2fsck_should_abort(ctx)) {
2628                                 e2fsck_pass1_check_unlock(ctx);
2629                                 goto endit;
2630                         }
2631                 }
2632         next_unlock:
2633                 e2fsck_pass1_check_unlock(ctx);
2634         }
2635         process_inodes(ctx, block_buf, inodes_to_process,
2636                        &process_inode_count);
2637         ext2fs_close_inode_scan(scan);
2638         scan = NULL;
2639
2640         if (ctx->ea_block_quota_blocks) {
2641                 ea_refcount_free(ctx->ea_block_quota_blocks);
2642                 ctx->ea_block_quota_blocks = 0;
2643         }
2644
2645         if (ctx->ea_block_quota_inodes) {
2646                 ea_refcount_free(ctx->ea_block_quota_inodes);
2647                 ctx->ea_block_quota_inodes = 0;
2648         }
2649
2650         if (ctx->flags & E2F_FLAG_RESTART) {
2651                 /*
2652                  * Only the master copy of the superblock and block
2653                  * group descriptors are going to be written during a
2654                  * restart, so set the superblock to be used to be the
2655                  * master superblock.
2656                  */
2657                 ctx->use_superblock = 0;
2658                 goto endit;
2659         }
2660
2661         if (ctx->large_dirs && !ext2fs_has_feature_largedir(ctx->fs->super)) {
2662                 ext2_filsys fs = ctx->fs;
2663
2664                 if (fix_problem(ctx, PR_2_FEATURE_LARGE_DIRS, &pctx)) {
2665                         ext2fs_set_feature_largedir(fs->super);
2666                         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2667                         ext2fs_mark_super_dirty(fs);
2668                 }
2669                 if (fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
2670                     fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
2671                         ext2fs_update_dynamic_rev(fs);
2672                         ext2fs_mark_super_dirty(fs);
2673                 }
2674         }
2675
2676         ctx->flags |= E2F_FLAG_ALLOC_OK;
2677         ext2fs_free_mem(&inodes_to_process);
2678 endit:
2679         e2fsck_use_inode_shortcuts(ctx, 0);
2680         ext2fs_free_mem(&inodes_to_process);
2681         inodes_to_process = 0;
2682
2683         if (scan)
2684                 ext2fs_close_inode_scan(scan);
2685         if (block_buf)
2686                 ext2fs_free_mem(&block_buf);
2687         if (inode)
2688                 ext2fs_free_mem(&inode);
2689
2690         /*
2691          * The l+f inode may have been cleared, so zap it now and
2692          * later passes will recalculate it if necessary
2693          */
2694         ctx->lost_and_found = 0;
2695
2696         if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
2697                 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
2698         else
2699                 ctx->invalid_bitmaps++;
2700 #ifdef  HAVE_PTHREAD
2701         /* reset update_thread after this thread exit */
2702         e2fsck_pass1_block_map_w_lock(ctx);
2703         if (check_mmp)
2704                 global_ctx->mmp_update_thread = 0;
2705         e2fsck_pass1_block_map_w_unlock(ctx);
2706 #endif
2707 }
2708
2709 #ifdef HAVE_PTHREAD
2710 static errcode_t e2fsck_pass1_copy_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2711                                           ext2fs_generic_bitmap *dest)
2712 {
2713         errcode_t ret;
2714
2715         ret = ext2fs_copy_bitmap(*src, dest);
2716         if (ret)
2717                 return ret;
2718
2719         (*dest)->fs = fs;
2720
2721         return 0;
2722 }
2723
2724 static void e2fsck_pass1_free_bitmap(ext2fs_generic_bitmap *bitmap)
2725 {
2726         if (*bitmap) {
2727                 ext2fs_free_generic_bmap(*bitmap);
2728                 *bitmap = NULL;
2729         }
2730
2731 }
2732
2733 static errcode_t e2fsck_pass1_merge_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2734                                           ext2fs_generic_bitmap *dest)
2735 {
2736         errcode_t ret = 0;
2737
2738         if (*src) {
2739                 if (*dest == NULL) {
2740                         *dest = *src;
2741                         *src = NULL;
2742                 } else {
2743                         ret = ext2fs_merge_bitmap(*src, *dest, NULL, NULL);
2744                         if (ret)
2745                                 return ret;
2746                 }
2747                 (*dest)->fs = fs;
2748         }
2749
2750         return 0;
2751 }
2752
2753 static errcode_t e2fsck_pass1_copy_fs(ext2_filsys dest, e2fsck_t src_context,
2754                                       ext2_filsys src)
2755 {
2756         errcode_t       retval;
2757
2758         memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2759         dest->inode_map = NULL;
2760         dest->block_map = NULL;
2761         dest->badblocks = NULL;
2762         if (dest->dblist)
2763                 dest->dblist->fs = dest;
2764         if (src->block_map) {
2765                 retval = e2fsck_pass1_copy_bitmap(dest, &src->block_map,
2766                                                   &dest->block_map);
2767                 if (retval)
2768                         return retval;
2769         }
2770         if (src->inode_map) {
2771                 retval = e2fsck_pass1_copy_bitmap(dest, &src->inode_map,
2772                                                   &dest->inode_map);
2773                 if (retval)
2774                         return retval;
2775         }
2776
2777         if (src->badblocks) {
2778                 retval = ext2fs_badblocks_copy(src->badblocks,
2779                                                &dest->badblocks);
2780                 if (retval)
2781                         return retval;
2782         }
2783
2784         /* disable it for now */
2785         src_context->openfs_flags &= ~EXT2_FLAG_EXCLUSIVE;
2786         retval = ext2fs_open_channel(dest, src_context->io_options,
2787                                      src_context->io_manager,
2788                                      src_context->openfs_flags,
2789                                      src->io->block_size);
2790         if (retval)
2791                 return retval;
2792
2793         /* Block size might not be default */
2794         io_channel_set_blksize(dest->io, src->io->block_size);
2795         ehandler_init(dest->io);
2796
2797         assert(dest->io->magic == src->io->magic);
2798         assert(dest->io->manager == src->io->manager);
2799         assert(strcmp(dest->io->name, src->io->name) == 0);
2800         assert(dest->io->block_size == src->io->block_size);
2801         assert(dest->io->read_error == src->io->read_error);
2802         assert(dest->io->write_error == src->io->write_error);
2803         assert(dest->io->refcount == src->io->refcount);
2804         assert(dest->io->flags == src->io->flags);
2805         assert(dest->io->app_data == dest);
2806         assert(src->io->app_data == src);
2807         assert(dest->io->align == src->io->align);
2808
2809         /* The data should be written to disk immediately */
2810         dest->io->flags |= CHANNEL_FLAGS_WRITETHROUGH;
2811         /* icache will be rebuilt if needed, so do not copy from @src */
2812         src->icache = NULL;
2813         return 0;
2814 }
2815
2816 static int e2fsck_pass1_merge_fs(ext2_filsys dest, ext2_filsys src)
2817 {
2818         struct ext2_inode_cache *icache = dest->icache;
2819         errcode_t retval = 0;
2820         io_channel dest_io;
2821         io_channel dest_image_io;
2822         ext2fs_inode_bitmap inode_map;
2823         ext2fs_block_bitmap block_map;
2824         ext2_badblocks_list badblocks;
2825         ext2_dblist dblist;
2826         int flags;
2827         e2fsck_t dest_ctx = dest->priv_data;
2828
2829         dest_io = dest->io;
2830         dest_image_io = dest->image_io;
2831         inode_map = dest->inode_map;
2832         block_map = dest->block_map;
2833         badblocks = dest->badblocks;
2834         dblist = dest->dblist;
2835         flags = dest->flags;
2836
2837         memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2838         dest->io = dest_io;
2839         dest->image_io = dest_image_io;
2840         dest->icache = icache;
2841         dest->inode_map = inode_map;
2842         dest->block_map = block_map;
2843         dest->badblocks = badblocks;
2844         dest->dblist = dblist;
2845         dest->priv_data = dest_ctx;
2846         if (dest->dblist)
2847                 dest->dblist->fs = dest;
2848         dest->flags = src->flags | flags;
2849         if (!(src->flags & EXT2_FLAG_VALID) || !(flags & EXT2_FLAG_VALID))
2850                 ext2fs_unmark_valid(dest);
2851
2852         if (src->icache) {
2853                 ext2fs_free_inode_cache(src->icache);
2854                 src->icache = NULL;
2855         }
2856
2857         retval = e2fsck_pass1_merge_bitmap(dest, &src->inode_map,
2858                                            &dest->inode_map);
2859         if (retval)
2860                 goto out;
2861
2862         retval = e2fsck_pass1_merge_bitmap(dest, &src->block_map,
2863                                           &dest->block_map);
2864         if (retval)
2865                 goto out;
2866
2867         if (src->dblist) {
2868                 if (dest->dblist) {
2869                         retval = ext2fs_merge_dblist(src->dblist,
2870                                                      dest->dblist);
2871                         if (retval)
2872                                 goto out;
2873                 } else {
2874                         dest->dblist = src->dblist;
2875                         dest->dblist->fs = dest;
2876                         src->dblist = NULL;
2877                 }
2878         }
2879
2880         if (src->badblocks) {
2881                 if (dest->badblocks == NULL)
2882                         retval = ext2fs_badblocks_copy(src->badblocks,
2883                                                        &dest->badblocks);
2884                 else
2885                         retval = ext2fs_badblocks_merge(src->badblocks,
2886                                                         dest->badblocks);
2887         }
2888 out:
2889         io_channel_close(src->io);
2890         if (src->inode_map)
2891                 ext2fs_free_generic_bmap(src->inode_map);
2892         if (src->block_map)
2893                 ext2fs_free_generic_bmap(src->block_map);
2894         if (src->badblocks)
2895                 ext2fs_badblocks_list_free(src->badblocks);
2896         if (src->dblist)
2897                 ext2fs_free_dblist(src->dblist);
2898
2899         return retval;
2900 }
2901
2902 static void e2fsck_pass1_copy_invalid_bitmaps(e2fsck_t global_ctx,
2903                                               e2fsck_t thread_ctx)
2904 {
2905         dgrp_t i, j;
2906         dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2907         dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2908         dgrp_t total = grp_end - grp_start;
2909
2910         thread_ctx->invalid_inode_bitmap_flag =
2911                         e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2912                                                 "invalid_inode_bitmap");
2913         thread_ctx->invalid_block_bitmap_flag =
2914                         e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2915                                                "invalid_block_bitmap");
2916         thread_ctx->invalid_inode_table_flag =
2917                         e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2918                                                "invalid_inode_table");
2919
2920         memcpy(thread_ctx->invalid_block_bitmap_flag,
2921                &global_ctx->invalid_block_bitmap_flag[grp_start],
2922                total * sizeof(int));
2923         memcpy(thread_ctx->invalid_inode_bitmap_flag,
2924                &global_ctx->invalid_inode_bitmap_flag[grp_start],
2925                total * sizeof(int));
2926         memcpy(thread_ctx->invalid_inode_table_flag,
2927                &global_ctx->invalid_inode_table_flag[grp_start],
2928                total * sizeof(int));
2929
2930         thread_ctx->invalid_bitmaps = 0;
2931         for (i = grp_start, j = 0; i < grp_end; i++, j++) {
2932                 if (thread_ctx->invalid_block_bitmap_flag[j])
2933                         thread_ctx->invalid_bitmaps++;
2934                 if (thread_ctx->invalid_inode_bitmap_flag[j])
2935                         thread_ctx->invalid_bitmaps++;
2936                 if (thread_ctx->invalid_inode_table_flag[j])
2937                         thread_ctx->invalid_bitmaps++;
2938         }
2939 }
2940
2941 static void e2fsck_pass1_merge_invalid_bitmaps(e2fsck_t global_ctx,
2942                                                e2fsck_t thread_ctx)
2943 {
2944         dgrp_t i, j;
2945         dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2946         dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2947         dgrp_t total = grp_end - grp_start;
2948
2949         memcpy(&global_ctx->invalid_block_bitmap_flag[grp_start],
2950                thread_ctx->invalid_block_bitmap_flag, total * sizeof(int));
2951         memcpy(&global_ctx->invalid_inode_bitmap_flag[grp_start],
2952                thread_ctx->invalid_inode_bitmap_flag, total * sizeof(int));
2953         memcpy(&global_ctx->invalid_inode_table_flag[grp_start],
2954                thread_ctx->invalid_inode_table_flag, total * sizeof(int));
2955         global_ctx->invalid_bitmaps += thread_ctx->invalid_bitmaps;
2956 }
2957
2958 static errcode_t e2fsck_pass1_thread_prepare(e2fsck_t global_ctx, e2fsck_t *thread_ctx,
2959                                              int thread_index, int num_threads,
2960                                              dgrp_t average_group)
2961 {
2962         errcode_t               retval;
2963         e2fsck_t                thread_context;
2964         ext2_filsys             thread_fs;
2965         ext2_filsys             global_fs = global_ctx->fs;
2966         struct e2fsck_thread    *tinfo;
2967
2968         assert(global_ctx->inode_used_map == NULL);
2969         assert(global_ctx->inode_dir_map == NULL);
2970         assert(global_ctx->inode_bb_map == NULL);
2971         assert(global_ctx->inode_imagic_map == NULL);
2972         assert(global_ctx->inode_reg_map == NULL);
2973         assert(global_ctx->inodes_to_rebuild == NULL);
2974
2975         assert(global_ctx->block_found_map != NULL);
2976         assert(global_ctx->block_metadata_map != NULL);
2977         assert(global_ctx->block_dup_map != NULL);
2978         assert(global_ctx->block_ea_map == NULL);
2979         assert(global_ctx->fs->dblist == NULL);
2980
2981         retval = ext2fs_get_mem(sizeof(struct e2fsck_struct), &thread_context);
2982         if (retval) {
2983                 com_err(global_ctx->program_name, retval, "while allocating memory");
2984                 return retval;
2985         }
2986         memcpy(thread_context, global_ctx, sizeof(struct e2fsck_struct));
2987         thread_context->block_dup_map = NULL;
2988         thread_context->casefolded_dirs = NULL;
2989         thread_context->expand_eisize_map = NULL;
2990         thread_context->inode_badness = NULL;
2991
2992         retval = e2fsck_allocate_block_bitmap(global_ctx->fs,
2993                                 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
2994                                 "block_found_map", &thread_context->block_found_map);
2995         if (retval)
2996                 goto out_context;
2997
2998         thread_context->global_ctx = global_ctx;
2999         retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &thread_fs);
3000         if (retval) {
3001                 com_err(global_ctx->program_name, retval, "while allocating memory");
3002                 goto out_context;
3003         }
3004
3005         io_channel_flush_cleanup(global_fs->io);
3006         retval = e2fsck_pass1_copy_fs(thread_fs, global_ctx, global_fs);
3007         if (retval) {
3008                 com_err(global_ctx->program_name, retval, "while copying fs");
3009                 goto out_fs;
3010         }
3011         thread_fs->priv_data = thread_context;
3012
3013         thread_context->thread_info.et_thread_index = thread_index;
3014         set_up_logging(thread_context);
3015
3016         tinfo = &thread_context->thread_info;
3017         tinfo->et_group_start = average_group * thread_index;
3018         if (thread_index == global_fs->fs_num_threads - 1)
3019                 tinfo->et_group_end = thread_fs->group_desc_count;
3020         else
3021                 tinfo->et_group_end = average_group * (thread_index + 1);
3022         tinfo->et_group_next = tinfo->et_group_start;
3023         tinfo->et_inode_number = 0;
3024         tinfo->et_log_buf[0] = '\0';
3025         tinfo->et_log_length = 0;
3026         if (thread_context->options & E2F_OPT_MULTITHREAD)
3027                 log_out(thread_context, _("Scan group range [%d, %d)\n"),
3028                         tinfo->et_group_start, tinfo->et_group_end);
3029         thread_context->fs = thread_fs;
3030         retval = quota_init_context(&thread_context->qctx, thread_fs, 0);
3031         if (retval) {
3032                 com_err(global_ctx->program_name, retval,
3033                         "while init quota context");
3034                 goto out_fs;
3035         }
3036         *thread_ctx = thread_context;
3037         e2fsck_pass1_copy_invalid_bitmaps(global_ctx, thread_context);
3038         return 0;
3039 out_fs:
3040         ext2fs_free_mem(&thread_fs);
3041 out_context:
3042         if (thread_context->block_found_map)
3043                 ext2fs_free_mem(&thread_context->block_found_map);
3044         ext2fs_free_mem(&thread_context);
3045         return retval;
3046 }
3047
3048 static void e2fsck_pass1_merge_dir_info(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3049 {
3050         if (thread_ctx->dir_info == NULL)
3051                 return;
3052
3053         if (global_ctx->dir_info == NULL) {
3054                 global_ctx->dir_info = thread_ctx->dir_info;
3055                 thread_ctx->dir_info = NULL;
3056                 return;
3057         }
3058
3059         e2fsck_merge_dir_info(global_ctx, thread_ctx->dir_info,
3060                               global_ctx->dir_info);
3061 }
3062
3063 static void e2fsck_pass1_merge_dx_dir(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3064 {
3065         if (thread_ctx->dx_dir_info == NULL)
3066                 return;
3067
3068         if (global_ctx->dx_dir_info == NULL) {
3069                 global_ctx->dx_dir_info = thread_ctx->dx_dir_info;
3070                 global_ctx->dx_dir_info_size = thread_ctx->dx_dir_info_size;
3071                 global_ctx->dx_dir_info_count = thread_ctx->dx_dir_info_count;
3072                 thread_ctx->dx_dir_info = NULL;
3073                 return;
3074         }
3075
3076         e2fsck_merge_dx_dir(global_ctx, thread_ctx);
3077 }
3078
3079 static int e2fsck_pass1_merge_encrypted_info(e2fsck_t global_ctx,
3080                                               e2fsck_t thread_ctx)
3081 {
3082         if (thread_ctx->encrypted_files == NULL)
3083                 return 0;
3084
3085         if (global_ctx->encrypted_files == NULL) {
3086                 global_ctx->encrypted_files = thread_ctx->encrypted_files;
3087                 thread_ctx->encrypted_files = NULL;
3088                 return 0;
3089         }
3090
3091         return e2fsck_merge_encrypted_info(global_ctx,
3092                                            thread_ctx->encrypted_files,
3093                                            global_ctx->encrypted_files);
3094 }
3095
3096 static inline errcode_t
3097 e2fsck_pass1_merge_icount(ext2_icount_t *dest_icount,
3098                           ext2_icount_t *src_icount)
3099 {
3100         if (*src_icount) {
3101                 if (*dest_icount == NULL) {
3102                         *dest_icount = *src_icount;
3103                         *src_icount = NULL;
3104                 } else {
3105                         errcode_t ret;
3106
3107                         ret = ext2fs_icount_merge(*src_icount,
3108                                                   *dest_icount);
3109                         if (ret)
3110                                 return ret;
3111                 }
3112         }
3113
3114         return 0;
3115 }
3116
3117 static errcode_t e2fsck_pass1_merge_icounts(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3118 {
3119         errcode_t ret;
3120
3121         ret = e2fsck_pass1_merge_icount(&global_ctx->inode_count,
3122                                         &thread_ctx->inode_count);
3123         if (ret)
3124                 return ret;
3125         ret = e2fsck_pass1_merge_icount(&global_ctx->inode_link_info,
3126                                         &thread_ctx->inode_link_info);
3127         if (ret)
3128                 return ret;
3129
3130         ret = e2fsck_pass1_merge_icount(&global_ctx->inode_badness,
3131                                         &thread_ctx->inode_badness);
3132
3133         return ret;
3134 }
3135
3136 static errcode_t e2fsck_pass1_merge_dirs_to_hash(e2fsck_t global_ctx,
3137                                                  e2fsck_t thread_ctx)
3138 {
3139         errcode_t retval = 0;
3140
3141         if (!thread_ctx->dirs_to_hash)
3142                 return 0;
3143
3144         if (!global_ctx->dirs_to_hash)
3145                 retval = ext2fs_badblocks_copy(thread_ctx->dirs_to_hash,
3146                                                &global_ctx->dirs_to_hash);
3147         else
3148                 retval = ext2fs_badblocks_merge(thread_ctx->dirs_to_hash,
3149                                                 global_ctx->dirs_to_hash);
3150
3151         return retval;
3152 }
3153
3154 static errcode_t e2fsck_pass1_merge_ea_inode_refs(e2fsck_t global_ctx,
3155                                                   e2fsck_t thread_ctx)
3156 {
3157         ea_value_t count;
3158         blk64_t blk;
3159         errcode_t retval;
3160
3161         if (!thread_ctx->ea_inode_refs)
3162                 return 0;
3163
3164         if (!global_ctx->ea_inode_refs) {
3165                 global_ctx->ea_inode_refs = thread_ctx->ea_inode_refs;
3166                 thread_ctx->ea_inode_refs = NULL;
3167                 return 0;
3168         }
3169
3170         ea_refcount_intr_begin(thread_ctx->ea_inode_refs);
3171         while (1) {
3172                 if ((blk = ea_refcount_intr_next(thread_ctx->ea_inode_refs,
3173                                                  &count)) == 0)
3174                         break;
3175                 if (!global_ctx->block_ea_map ||
3176                     !ext2fs_fast_test_block_bitmap2(global_ctx->block_ea_map,
3177                                                     blk)) {
3178                         retval = ea_refcount_store(global_ctx->ea_inode_refs,
3179                                                    blk, count);
3180                         if (retval)
3181                                 return retval;
3182                 }
3183         }
3184
3185         return retval;
3186 }
3187
3188 static ea_value_t ea_refcount_usage(e2fsck_t ctx, blk64_t blk,
3189                                     ea_value_t *orig)
3190 {
3191         ea_value_t count_cur;
3192         ea_value_t count_extra = 0;
3193         ea_value_t count_orig;
3194
3195         ea_refcount_fetch(ctx->refcount_orig, blk, &count_orig);
3196         ea_refcount_fetch(ctx->refcount, blk, &count_cur);
3197         /* most of time this is not needed */
3198         if (ctx->refcount_extra && count_cur == 0)
3199                 ea_refcount_fetch(ctx->refcount_extra, blk, &count_extra);
3200
3201         if (!count_orig)
3202                 count_orig = *orig;
3203         else if (orig)
3204                 *orig = count_orig;
3205
3206         return count_orig + count_extra - count_cur;
3207 }
3208
3209 static errcode_t e2fsck_pass1_merge_ea_refcount(e2fsck_t global_ctx,
3210                                                 e2fsck_t thread_ctx)
3211 {
3212         ea_value_t count;
3213         blk64_t blk;
3214         errcode_t retval = 0;
3215
3216         if (!thread_ctx->refcount)
3217                 return 0;
3218
3219         if (!global_ctx->refcount) {
3220                 global_ctx->refcount = thread_ctx->refcount;
3221                 thread_ctx->refcount = NULL;
3222                 global_ctx->refcount_extra = thread_ctx->refcount;
3223                 thread_ctx->refcount_extra = NULL;
3224                 return 0;
3225         }
3226
3227         ea_refcount_intr_begin(thread_ctx->refcount);
3228         while (1) {
3229                 if ((blk = ea_refcount_intr_next(thread_ctx->refcount,
3230                                                  &count)) == 0)
3231                         break;
3232                 /**
3233                  * this EA has never seen before, so just store its
3234                  * refcount and refcount_extra into global_ctx if needed.
3235                  */
3236                 if (!global_ctx->block_ea_map ||
3237                     !ext2fs_fast_test_block_bitmap2(global_ctx->block_ea_map,
3238                                                     blk)) {
3239                         ea_value_t extra;
3240
3241                         retval = ea_refcount_store(global_ctx->refcount,
3242                                                    blk, count);
3243                         if (retval)
3244                                 return retval;
3245
3246                         if (count > 0 || !thread_ctx->refcount_extra)
3247                                 continue;
3248                         ea_refcount_fetch(thread_ctx->refcount_extra, blk,
3249                                           &extra);
3250                         if (extra == 0)
3251                                 continue;
3252
3253                         if (!global_ctx->refcount_extra) {
3254                                 retval = ea_refcount_create(0,
3255                                                 &global_ctx->refcount_extra);
3256                                 if (retval)
3257                                         return retval;
3258                         }
3259                         retval = ea_refcount_store(global_ctx->refcount_extra,
3260                                                    blk, extra);
3261                         if (retval)
3262                                 return retval;
3263                 } else {
3264                         ea_value_t orig;
3265                         ea_value_t thread_usage;
3266                         ea_value_t global_usage;
3267                         ea_value_t new;
3268
3269                         thread_usage = ea_refcount_usage(thread_ctx,
3270                                                          blk, &orig);
3271                         global_usage = ea_refcount_usage(global_ctx,
3272                                                          blk, &orig);
3273                         if (thread_usage + global_usage <= orig) {
3274                                 new = orig - thread_usage - global_usage;
3275                                 retval = ea_refcount_store(global_ctx->refcount,
3276                                                            blk, new);
3277                                 if (retval)
3278                                         return retval;
3279                                 continue;
3280                         }
3281                         /* update it is as zero */
3282                         retval = ea_refcount_store(global_ctx->refcount,
3283                                                    blk, 0);
3284                         if (retval)
3285                                 return retval;
3286                         /* Ooops, this EA was referenced more than it stated */
3287                         if (!global_ctx->refcount_extra) {
3288                                 retval = ea_refcount_create(0,
3289                                                 &global_ctx->refcount_extra);
3290                                 if (retval)
3291                                         return retval;
3292                         }
3293                         new = global_usage + thread_usage - orig;
3294                         retval = ea_refcount_store(global_ctx->refcount_extra,
3295                                                    blk, new);
3296                         if (retval)
3297                                 return retval;
3298                 }
3299         }
3300
3301         return retval;
3302 }
3303
3304 static errcode_t e2fsck_pass1_merge_casefolded_dirs(e2fsck_t global_ctx,
3305                                                    e2fsck_t thread_ctx)
3306 {
3307         errcode_t retval = 0;
3308
3309         if (!thread_ctx->casefolded_dirs)
3310                 return 0;
3311
3312         if (!global_ctx->casefolded_dirs)
3313                 retval = ext2fs_badblocks_copy(thread_ctx->casefolded_dirs,
3314                                                &global_ctx->casefolded_dirs);
3315         else
3316                 retval = ext2fs_badblocks_merge(thread_ctx->casefolded_dirs,
3317                                                 global_ctx->casefolded_dirs);
3318
3319         return retval;
3320 }
3321
3322 static errcode_t e2fsck_pass1_merge_context(e2fsck_t global_ctx,
3323                                             e2fsck_t thread_ctx)
3324 {
3325         ext2_filsys global_fs = global_ctx->fs;
3326         errcode_t retval;
3327         int i;
3328
3329         global_ctx->fs_directory_count += thread_ctx->fs_directory_count;
3330         global_ctx->fs_regular_count += thread_ctx->fs_regular_count;
3331         global_ctx->fs_blockdev_count += thread_ctx->fs_blockdev_count;
3332         global_ctx->fs_chardev_count += thread_ctx->fs_chardev_count;
3333         global_ctx->fs_links_count += thread_ctx->fs_links_count;
3334         global_ctx->fs_symlinks_count += thread_ctx->fs_symlinks_count;
3335         global_ctx->fs_fast_symlinks_count += thread_ctx->fs_fast_symlinks_count;
3336         global_ctx->fs_fifo_count += thread_ctx->fs_fifo_count;
3337         global_ctx->fs_total_count += thread_ctx->fs_total_count;
3338         global_ctx->fs_badblocks_count += thread_ctx->fs_badblocks_count;
3339         global_ctx->fs_sockets_count += thread_ctx->fs_sockets_count;
3340         global_ctx->fs_ind_count += thread_ctx->fs_ind_count;
3341         global_ctx->fs_dind_count += thread_ctx->fs_dind_count;
3342         global_ctx->fs_tind_count += thread_ctx->fs_tind_count;
3343         global_ctx->fs_fragmented += thread_ctx->fs_fragmented;
3344         global_ctx->fs_fragmented_dir += thread_ctx->fs_fragmented_dir;
3345         global_ctx->large_files += thread_ctx->large_files;
3346         /* threads might enable E2F_OPT_YES */
3347         global_ctx->options |= thread_ctx->options;
3348         global_ctx->flags |= thread_ctx->flags;
3349         /*
3350          * The l+f inode may have been cleared, so zap it now and
3351          * later passes will recalculate it if necessary
3352          */
3353         global_ctx->lost_and_found = 0;
3354         /* merge extent depth count */
3355         for (i = 0; i < MAX_EXTENT_DEPTH_COUNT; i++)
3356                 global_ctx->extent_depth_count[i] +=
3357                         thread_ctx->extent_depth_count[i];
3358
3359         e2fsck_pass1_merge_dir_info(global_ctx, thread_ctx);
3360         e2fsck_pass1_merge_dx_dir(global_ctx, thread_ctx);
3361         retval = e2fsck_pass1_merge_encrypted_info(global_ctx, thread_ctx);
3362         if (retval) {
3363                 com_err(global_ctx->program_name, 0,
3364                         _("while merging encrypted info\n"));
3365                 return retval;
3366         }
3367
3368         retval = e2fsck_pass1_merge_fs(global_ctx->fs, thread_ctx->fs);
3369         if (retval) {
3370                 com_err(global_ctx->program_name, 0, _("while merging fs\n"));
3371                 return retval;
3372         }
3373         retval = e2fsck_pass1_merge_icounts(global_ctx, thread_ctx);
3374         if (retval) {
3375                 com_err(global_ctx->program_name, 0,
3376                         _("while merging icounts\n"));
3377                 return retval;
3378         }
3379
3380         retval = e2fsck_pass1_merge_dirs_to_hash(global_ctx, thread_ctx);
3381         if (retval) {
3382                 com_err(global_ctx->program_name, 0,
3383                         _("while merging dirs to hash\n"));
3384                 return retval;
3385         }
3386
3387         e2fsck_pass1_merge_ea_inode_refs(global_ctx, thread_ctx);
3388         e2fsck_pass1_merge_ea_refcount(global_ctx, thread_ctx);
3389         retval = quota_merge_and_update_usage(global_ctx->qctx,
3390                                               thread_ctx->qctx);
3391         if (retval)
3392                 return retval;
3393
3394         retval = e2fsck_pass1_merge_casefolded_dirs(global_ctx, thread_ctx);
3395         if (retval) {
3396                 com_err(global_ctx->program_name, 0,
3397                         _("while merging casefolded dirs\n"));
3398                 return retval;
3399         }
3400
3401         e2fsck_pass1_merge_invalid_bitmaps(global_ctx, thread_ctx);
3402
3403         if (thread_ctx->min_extra_isize < global_ctx->min_extra_isize)
3404                 global_ctx->min_extra_isize = thread_ctx->min_extra_isize;
3405
3406         retval = e2fsck_pass1_merge_bitmap(global_fs,
3407                                 &thread_ctx->inode_used_map,
3408                                 &global_ctx->inode_used_map);
3409         if (retval)
3410                 return retval;
3411
3412         retval = e2fsck_pass1_merge_bitmap(global_fs,
3413                                         &thread_ctx->inode_dir_map,
3414                                         &global_ctx->inode_dir_map);
3415         if (retval)
3416                 return retval;
3417         retval = e2fsck_pass1_merge_bitmap(global_fs,
3418                                 &thread_ctx->inode_bb_map,
3419                                 &global_ctx->inode_bb_map);
3420         if (retval)
3421                 return retval;
3422         retval = e2fsck_pass1_merge_bitmap(global_fs,
3423                                 &thread_ctx->inode_imagic_map,
3424                                 &global_ctx->inode_imagic_map);
3425         if (retval)
3426                 return retval;
3427         retval = e2fsck_pass1_merge_bitmap(global_fs,
3428                                 &thread_ctx->inode_reg_map,
3429                                 &global_ctx->inode_reg_map);
3430         if (retval)
3431                 return retval;
3432         retval = e2fsck_pass1_merge_bitmap(global_fs,
3433                                 &thread_ctx->inodes_to_rebuild,
3434                                 &global_ctx->inodes_to_rebuild);
3435         if (retval)
3436                 return retval;
3437         retval = e2fsck_pass1_merge_bitmap(global_fs,
3438                                 &thread_ctx->block_ea_map,
3439                                 &global_ctx->block_ea_map);
3440         if (retval)
3441                 return retval;
3442
3443         retval = e2fsck_pass1_merge_bitmap(global_fs,
3444                                 &thread_ctx->expand_eisize_map,
3445                                 &global_ctx->expand_eisize_map);
3446         if (retval)
3447                 return retval;
3448
3449         if (ext2fs_has_feature_shared_blocks(global_fs->super) &&
3450             !(global_ctx->options & E2F_OPT_UNSHARE_BLOCKS))
3451                 return 0;
3452         /*
3453          * This need be done after merging block_ea_map
3454          * because ea block might be shared, we need exclude
3455          * them from dup blocks.
3456          */
3457         e2fsck_pass1_block_map_w_lock(thread_ctx);
3458         retval = ext2fs_merge_bitmap(thread_ctx->block_found_map,
3459                                      global_ctx->block_found_map,
3460                                      global_ctx->block_dup_map,
3461                                      global_ctx->block_ea_map);
3462         e2fsck_pass1_block_map_w_unlock(thread_ctx);
3463         if (retval == EEXIST)
3464                 global_ctx->flags |= E2F_FLAG_DUP_BLOCK;
3465
3466         return 0;
3467 }
3468
3469 static int e2fsck_pass1_thread_join(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3470 {
3471         errcode_t       retval;
3472
3473         retval = e2fsck_pass1_merge_context(global_ctx, thread_ctx);
3474         ext2fs_free_mem(&thread_ctx->fs);
3475         if (thread_ctx->logf)
3476                 fclose(thread_ctx->logf);
3477         if (thread_ctx->problem_logf) {
3478                 fputs("</problem_log>\n", thread_ctx->problem_logf);
3479                 fclose(thread_ctx->problem_logf);
3480         }
3481
3482         quota_release_context(&thread_ctx->qctx);
3483         /*
3484          * @block_metadata_map and @block_dup_map are
3485          * shared, so we don't free them.
3486          */
3487         thread_ctx->block_metadata_map = NULL;
3488         thread_ctx->block_dup_map = NULL;
3489         e2fsck_reset_context(thread_ctx);
3490         ext2fs_free_mem(&thread_ctx);
3491
3492         return retval;
3493 }
3494
3495 static int e2fsck_pass1_threads_join(e2fsck_t global_ctx)
3496 {
3497         errcode_t rc;
3498         errcode_t ret = 0;
3499         struct e2fsck_thread_info *infos = global_ctx->infos;
3500         struct e2fsck_thread_info *pinfo;
3501         int num_threads = global_ctx->pfs_num_threads;
3502         int i;
3503
3504         /* merge invalid bitmaps will recalculate it */
3505         global_ctx->invalid_bitmaps = 0;
3506         for (i = 0; i < num_threads; i++) {
3507                 pinfo = &infos[i];
3508
3509                 if (!pinfo->eti_started)
3510                         continue;
3511
3512                 rc = pthread_join(pinfo->eti_thread_id, NULL);
3513                 if (rc) {
3514                         com_err(global_ctx->program_name, rc,
3515                                 _("while joining thread\n"));
3516                         if (ret == 0)
3517                                 ret = rc;
3518                 }
3519                 rc = e2fsck_pass1_thread_join(global_ctx, infos[i].eti_thread_ctx);
3520                 if (rc) {
3521                         com_err(global_ctx->program_name, rc,
3522                                 _("while joining pass1 thread\n"));
3523                         if (ret == 0)
3524                                 ret = rc;
3525                 }
3526         }
3527         free(infos);
3528         global_ctx->infos = NULL;
3529
3530         return ret;
3531 }
3532
3533 static void *e2fsck_pass1_thread(void *arg)
3534 {
3535         struct e2fsck_thread_info       *info = arg;
3536         e2fsck_t                         thread_ctx = info->eti_thread_ctx;
3537 #ifdef DEBUG_THREADS
3538         struct e2fsck_thread_debug      *thread_debug = info->eti_debug;
3539 #endif
3540
3541 #ifdef DEBUG_THREADS
3542         pthread_mutex_lock(&thread_debug->etd_mutex);
3543         while (info->eti_thread_index > thread_debug->etd_finished_threads) {
3544                 pthread_cond_wait(&thread_debug->etd_cond,
3545                                   &thread_debug->etd_mutex);
3546         }
3547         pthread_mutex_unlock(&thread_debug->etd_mutex);
3548 #endif
3549
3550 #ifdef HAVE_SETJMP_H
3551         /*
3552          * When fatal_error() happens, jump to here. The thread
3553          * context's flags will be saved, but its abort_loc will
3554          * be overwritten by original jump buffer for the later
3555          * tests.
3556          */
3557         if (setjmp(thread_ctx->abort_loc)) {
3558                 thread_ctx->flags &= ~E2F_FLAG_SETJMP_OK;
3559                 goto out;
3560         }
3561         thread_ctx->flags |= E2F_FLAG_SETJMP_OK;
3562 #endif
3563
3564         e2fsck_pass1_run(thread_ctx);
3565
3566 out:
3567         if (thread_ctx->options & E2F_OPT_MULTITHREAD)
3568                 log_out(thread_ctx,
3569                         _("Scanned group range [%u, %u), inodes %u\n"),
3570                         thread_ctx->thread_info.et_group_start,
3571                         thread_ctx->thread_info.et_group_end,
3572                         thread_ctx->thread_info.et_inode_number);
3573
3574 #ifdef DEBUG_THREADS
3575         pthread_mutex_lock(&thread_debug->etd_mutex);
3576         thread_debug->etd_finished_threads++;
3577         pthread_cond_broadcast(&thread_debug->etd_cond);
3578         pthread_mutex_unlock(&thread_debug->etd_mutex);
3579 #endif
3580
3581         return NULL;
3582 }
3583
3584 static dgrp_t ext2fs_get_avg_group(ext2_filsys fs)
3585 {
3586 #ifdef HAVE_PTHREAD
3587         dgrp_t average_group;
3588         unsigned flexbg_size;
3589
3590         if (fs->fs_num_threads <= 1)
3591                 return fs->group_desc_count;
3592
3593         average_group = fs->group_desc_count / fs->fs_num_threads;
3594         if (average_group <= 1)
3595                 return 1;
3596
3597         if (ext2fs_has_feature_flex_bg(fs->super)) {
3598                 int times = 1;
3599
3600                 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3601                 if (average_group % flexbg_size) {
3602                         times = average_group / flexbg_size;
3603                         average_group = times * flexbg_size;
3604                 }
3605         }
3606
3607         return average_group;
3608 #else
3609         return fs->group_desc_count;
3610 #endif
3611 }
3612
3613 static int e2fsck_pass1_threads_start(e2fsck_t global_ctx)
3614 {
3615         struct e2fsck_thread_info       *infos;
3616         pthread_attr_t                   attr;
3617         errcode_t                        retval;
3618         errcode_t                        ret;
3619         struct e2fsck_thread_info       *tmp_pinfo;
3620         int                              i;
3621         e2fsck_t                         thread_ctx;
3622         dgrp_t                           average_group;
3623         int num_threads = global_ctx->pfs_num_threads;
3624 #ifdef DEBUG_THREADS
3625         struct e2fsck_thread_debug       thread_debug =
3626                 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0};
3627
3628         thread_debug.etd_finished_threads = 0;
3629 #endif
3630
3631         retval = pthread_attr_init(&attr);
3632         if (retval) {
3633                 com_err(global_ctx->program_name, retval,
3634                         _("while setting pthread attribute\n"));
3635                 return retval;
3636         }
3637
3638         infos = calloc(num_threads, sizeof(struct e2fsck_thread_info));
3639         if (infos == NULL) {
3640                 retval = -ENOMEM;
3641                 com_err(global_ctx->program_name, retval,
3642                         _("while allocating memory for threads\n"));
3643                 pthread_attr_destroy(&attr);
3644                 return retval;
3645         }
3646         global_ctx->infos = infos;
3647
3648         average_group = ext2fs_get_avg_group(global_ctx->fs);
3649         for (i = 0; i < num_threads; i++) {
3650                 tmp_pinfo = &infos[i];
3651                 tmp_pinfo->eti_thread_index = i;
3652 #ifdef DEBUG_THREADS
3653                 tmp_pinfo->eti_debug = &thread_debug;
3654 #endif
3655                 retval = e2fsck_pass1_thread_prepare(global_ctx, &thread_ctx,
3656                                                      i, num_threads,
3657                                                      average_group);
3658                 if (retval) {
3659                         com_err(global_ctx->program_name, retval,
3660                                 _("while preparing pass1 thread\n"));
3661                         break;
3662                 }
3663                 tmp_pinfo->eti_thread_ctx = thread_ctx;
3664
3665                 retval = pthread_create(&tmp_pinfo->eti_thread_id, &attr,
3666                                         &e2fsck_pass1_thread, tmp_pinfo);
3667                 if (retval) {
3668                         com_err(global_ctx->program_name, retval,
3669                                 _("while creating thread\n"));
3670                         e2fsck_pass1_thread_join(global_ctx, thread_ctx);
3671                         break;
3672                 }
3673
3674                 tmp_pinfo->eti_started = 1;
3675         }
3676
3677         /* destroy the thread attribute object, since it is no longer needed */
3678         ret = pthread_attr_destroy(&attr);
3679         if (ret) {
3680                 com_err(global_ctx->program_name, ret,
3681                         _("while destroying thread attribute\n"));
3682                 if (retval == 0)
3683                         retval = ret;
3684         }
3685
3686         if (retval) {
3687                 e2fsck_pass1_threads_join(global_ctx);
3688                 return retval;
3689         }
3690         return 0;
3691 }
3692
3693 static void e2fsck_pass1_multithread(e2fsck_t global_ctx)
3694 {
3695         errcode_t retval;
3696
3697         retval = e2fsck_pass1_threads_start(global_ctx);
3698         if (retval) {
3699                 com_err(global_ctx->program_name, retval,
3700                         _("while starting pass1 threads\n"));
3701                 goto out_abort;
3702         }
3703
3704         retval = e2fsck_pass1_threads_join(global_ctx);
3705         if (retval) {
3706                 com_err(global_ctx->program_name, retval,
3707                         _("while joining pass1 threads\n"));
3708                 goto out_abort;
3709         }
3710         return;
3711 out_abort:
3712         global_ctx->flags |= E2F_FLAG_ABORT;
3713         return;
3714 }
3715 #endif
3716
3717 void e2fsck_pass1(e2fsck_t ctx)
3718 {
3719         errcode_t retval;
3720         int need_single = 1;
3721
3722         retval = e2fsck_pass1_prepare(ctx);
3723         if (retval)
3724                 return;
3725 #ifdef HAVE_PTHREAD
3726         if (ctx->pfs_num_threads > 1 || ctx->options & E2F_OPT_MULTITHREAD) {
3727                 need_single = 0;
3728                 e2fsck_pass1_multithread(ctx);
3729         }
3730         /* No lock is needed at this time */
3731         ctx->fs_need_locking = 0;
3732 #endif
3733         if (need_single)
3734                 e2fsck_pass1_run(ctx);
3735         e2fsck_pass1_post(ctx);
3736 }
3737
3738 #undef FINISH_INODE_LOOP
3739
3740 /*
3741  * When the inode_scan routines call this callback at the end of the
3742  * glock group, call process_inodes.
3743  */
3744 static errcode_t scan_callback(ext2_filsys fs,
3745                                ext2_inode_scan scan EXT2FS_ATTR((unused)),
3746                                dgrp_t group, void * priv_data)
3747 {
3748         struct scan_callback_struct *scan_struct;
3749         e2fsck_t ctx;
3750         dgrp_t cur = group + 1;
3751         struct e2fsck_thread *tinfo;
3752         struct e2fsck_thread_info *pinfo, *infos;
3753         int i;
3754
3755         scan_struct = (struct scan_callback_struct *) priv_data;
3756         ctx = scan_struct->ctx;
3757
3758         process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf,
3759                        scan_struct->inodes_to_process,
3760                        scan_struct->process_inode_count);
3761
3762 #ifdef HAVE_PTHREAD
3763         if (ctx->global_ctx) {
3764                 cur = 0;
3765                 infos = ctx->global_ctx->infos;
3766                 for (i = 0; i < ctx->global_ctx->pfs_num_threads; i++) {
3767                         pinfo = &infos[i];
3768
3769                         if (!pinfo->eti_started)
3770                                 continue;
3771
3772                         tinfo = &pinfo->eti_thread_ctx->thread_info;
3773                         if (ctx == pinfo->eti_thread_ctx)
3774                                 cur += group + 1 - tinfo->et_group_start;
3775                         else
3776                                 cur += tinfo->et_group_next -
3777                                         tinfo->et_group_start;
3778                 }
3779         }
3780 #endif
3781
3782         if (ctx->progress)
3783                 if ((ctx->progress)(ctx, 1, cur,
3784                                     ctx->fs->group_desc_count))
3785                         return EXT2_ET_CANCEL_REQUESTED;
3786
3787 #ifdef HAVE_PTHREAD
3788         if (ctx->global_ctx) {
3789                 tinfo = &ctx->thread_info;
3790                 tinfo->et_group_next++;
3791                 if (ctx->options & E2F_OPT_DEBUG &&
3792                     ctx->options & E2F_OPT_MULTITHREAD)
3793                         log_out(ctx, _("group %d finished\n"),
3794                                 tinfo->et_group_next);
3795                 if (tinfo->et_group_next >= tinfo->et_group_end)
3796                         return EXT2_ET_SCAN_FINISHED;
3797         }
3798 #endif
3799
3800         return 0;
3801 }
3802
3803 /*
3804  * Process the inodes in the "inodes to process" list.
3805  */
3806 static void process_inodes(e2fsck_t ctx, char *block_buf,
3807                            struct process_inode_block *inodes_to_process,
3808                            int *process_inode_count)
3809 {
3810         int                     i;
3811         struct ext2_inode       *old_stashed_inode;
3812         ext2_ino_t              old_stashed_ino;
3813         const char              *old_operation;
3814         char                    buf[80];
3815         struct problem_context  pctx;
3816
3817 #if 0
3818         printf("begin process_inodes: ");
3819 #endif
3820         if (*process_inode_count == 0)
3821                 return;
3822         old_operation = ehandler_operation(0);
3823         old_stashed_inode = ctx->stashed_inode;
3824         old_stashed_ino = ctx->stashed_ino;
3825         qsort(inodes_to_process, *process_inode_count,
3826                       sizeof(struct process_inode_block), process_inode_cmp);
3827         clear_problem_context(&pctx);
3828         for (i=0; i < *process_inode_count; i++) {
3829                 pctx.inode = ctx->stashed_inode =
3830                         (struct ext2_inode *) &inodes_to_process[i].inode;
3831                 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
3832
3833 #if 0
3834                 printf("%u ", pctx.ino);
3835 #endif
3836                 sprintf(buf, _("reading indirect blocks of inode %u"),
3837                         pctx.ino);
3838                 ehandler_operation(buf);
3839                 check_blocks(ctx, &pctx, block_buf,
3840                              &inodes_to_process[i].ea_ibody_quota);
3841                 if (e2fsck_should_abort(ctx))
3842                         break;
3843         }
3844         ctx->stashed_inode = old_stashed_inode;
3845         ctx->stashed_ino = old_stashed_ino;
3846         *process_inode_count = 0;
3847 #if 0
3848         printf("end process inodes\n");
3849 #endif
3850         ehandler_operation(old_operation);
3851 }
3852
3853 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
3854 {
3855         const struct process_inode_block *ib_a =
3856                 (const struct process_inode_block *) a;
3857         const struct process_inode_block *ib_b =
3858                 (const struct process_inode_block *) b;
3859         int     ret;
3860
3861         ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
3862                ib_b->inode.i_block[EXT2_IND_BLOCK]);
3863         if (ret == 0)
3864                 /*
3865                  * We only call process_inodes() for non-extent
3866                  * inodes, so it's OK to pass NULL to
3867                  * ext2fs_file_acl_block() here.
3868                  */
3869                 ret = ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_a->inode)) -
3870                         ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_b->inode));
3871         if (ret == 0)
3872                 ret = ib_a->ino - ib_b->ino;
3873         return ret;
3874 }
3875
3876 /*
3877  * Mark an inode as being bad and increment its badness counter.
3878  */
3879 void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, struct problem_context *pctx,
3880                                __u32 code, int badness, const char *func,
3881                                const int line)
3882 {
3883         __u16 badness_before, badness_after;
3884         __u64 pctx_num_sav = pctx->num;
3885
3886         if (!ctx->inode_badness_threshold)      /* badness is disabled */
3887                 return;
3888
3889         if (!ctx->inode_badness) {
3890                 errcode_t retval;
3891
3892                 retval = ext2fs_create_icount2(ctx->fs, 0, 0, NULL,
3893                                                &ctx->inode_badness);
3894                 if (retval) {
3895                         pctx->errcode = retval;
3896                         fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, pctx);
3897                         ctx->flags |= E2F_FLAG_ABORT;
3898                         return;
3899                 }
3900         }
3901         ext2fs_icount_fetch(ctx->inode_badness, pctx->ino, &badness_before);
3902         if (badness + badness_before > BADNESS_MAX)
3903                 badness_after = BADNESS_MAX;
3904         else if (badness < 0 && badness_before < -badness)
3905                 badness_after = 0;
3906         else
3907                 badness_after = badness_before + badness;
3908         ext2fs_icount_store(ctx->inode_badness, pctx->ino, badness_after);
3909
3910         if (ctx->options & E2F_OPT_DEBUG)
3911                 log_out(ctx,
3912                         "%s:%d: increase inode %lu badness %u to %u for %x\n",
3913                         func, line, (unsigned long)pctx->ino, badness_before,
3914                         badness_after, code);
3915 }
3916
3917 static void add_casefolded_dir(e2fsck_t ctx, ino_t ino)
3918 {
3919         struct          problem_context pctx;
3920
3921         if (!ctx->casefolded_dirs) {
3922                 pctx.errcode = ext2fs_u32_list_create(&ctx->casefolded_dirs, 0);
3923                 if (pctx.errcode)
3924                         goto error;
3925         }
3926         pctx.errcode = ext2fs_u32_list_add(ctx->casefolded_dirs, ino);
3927         if (pctx.errcode == 0)
3928                 return;
3929 error:
3930         fix_problem(ctx, PR_1_ALLOCATE_CASEFOLDED_DIRLIST, &pctx);
3931         /* Should never get here */
3932         ctx->flags |= E2F_FLAG_ABORT;
3933 }
3934
3935 /*
3936  * This procedure will allocate the inode "bb" (badblock) map table
3937  */
3938 static void alloc_bb_map(e2fsck_t ctx)
3939 {
3940         struct          problem_context pctx;
3941
3942         clear_problem_context(&pctx);
3943         pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3944                         _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
3945                         "inode_bb_map", &ctx->inode_bb_map);
3946         if (pctx.errcode) {
3947                 pctx.num = 4;
3948                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3949                 /* Should never get here */
3950                 ctx->flags |= E2F_FLAG_ABORT;
3951                 return;
3952         }
3953 }
3954
3955 /*
3956  * This procedure will allocate the inode imagic table
3957  */
3958 static void alloc_imagic_map(e2fsck_t ctx)
3959 {
3960         struct          problem_context pctx;
3961
3962         clear_problem_context(&pctx);
3963         pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3964                         _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
3965                         "inode_imagic_map", &ctx->inode_imagic_map);
3966         if (pctx.errcode) {
3967                 pctx.num = 5;
3968                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3969                 /* Should never get here */
3970                 ctx->flags |= E2F_FLAG_ABORT;
3971                 return;
3972         }
3973 }
3974
3975 /*
3976  * Marks a block as in use, setting the dup_map if it's been set
3977  * already.  Called by process_block and process_bad_block.
3978  *
3979  * WARNING: Assumes checks have already been done to make sure block
3980  * is valid.  This is true in both process_block and process_bad_block.
3981  */
3982 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
3983 {
3984         struct problem_context pctx;
3985         e2fsck_t global_ctx = ctx->global_ctx ? ctx->global_ctx : ctx;
3986
3987         clear_problem_context(&pctx);
3988
3989         if (is_blocks_used(ctx, block, 1)) {
3990                 if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
3991                     !(ctx->options & E2F_OPT_UNSHARE_BLOCKS)) {
3992                         return;
3993                 }
3994                 ctx->flags |= E2F_FLAG_DUP_BLOCK;
3995                 e2fsck_pass1_block_map_w_lock(ctx);
3996                 ext2fs_fast_mark_block_bitmap2(global_ctx->block_dup_map, block);
3997                 e2fsck_pass1_block_map_w_unlock(ctx);
3998         } else {
3999                 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
4000         }
4001 }
4002
4003 /*
4004  * When cluster size is greater than one block, it is caller's responsibility
4005  * to make sure block parameter starts at a cluster boundary.
4006  */
4007 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
4008                                       unsigned int num)
4009 {
4010         if (!is_blocks_used(ctx, block, num)) {
4011                 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
4012         } else {
4013                 unsigned int i;
4014
4015                 for (i = 0; i < num; i += EXT2FS_CLUSTER_RATIO(ctx->fs))
4016                         mark_block_used(ctx, block + i);
4017         }
4018 }
4019
4020 static errcode_t _INLINE_ e2fsck_write_ext_attr3(e2fsck_t ctx, blk64_t block,
4021                                                  void *inbuf, ext2_ino_t inum)
4022 {
4023         errcode_t retval;
4024         ext2_filsys fs = ctx->fs;
4025
4026         e2fsck_pass1_fix_lock(ctx);
4027         retval = ext2fs_write_ext_attr3(fs, block, inbuf, inum);
4028         e2fsck_pass1_fix_unlock(ctx);
4029
4030         return retval;
4031 }
4032 /*
4033  * Adjust the extended attribute block's reference counts at the end
4034  * of pass 1, either by subtracting out references for EA blocks that
4035  * are still referenced in ctx->refcount, or by adding references for
4036  * EA blocks that had extra references as accounted for in
4037  * ctx->refcount_extra.
4038  */
4039 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
4040                                     char *block_buf, int adjust_sign)
4041 {
4042         struct ext2_ext_attr_header     *header;
4043         struct problem_context          pctx;
4044         ext2_filsys                     fs = ctx->fs;
4045         blk64_t                         blk;
4046         __u32                           should_be;
4047         ea_value_t                      count;
4048
4049         clear_problem_context(&pctx);
4050
4051         ea_refcount_intr_begin(refcount);
4052         while (1) {
4053                 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
4054                         break;
4055                 pctx.blk = blk;
4056                 pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
4057                                                      pctx.ino);
4058                 /* We already checked this block, shouldn't happen */
4059                 if (pctx.errcode) {
4060                         fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
4061                         return;
4062                 }
4063                 header = BHDR(block_buf);
4064                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
4065                         fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
4066                         return;
4067                 }
4068
4069                 pctx.blkcount = header->h_refcount;
4070                 should_be = header->h_refcount + adjust_sign * (int)count;
4071                 pctx.num = should_be;
4072                 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
4073                         header->h_refcount = should_be;
4074                         pctx.errcode = e2fsck_write_ext_attr3(ctx, blk,
4075                                                              block_buf,
4076                                                              pctx.ino);
4077                         if (pctx.errcode) {
4078                                 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
4079                                             &pctx);
4080                                 continue;
4081                         }
4082                 }
4083         }
4084 }
4085
4086 /*
4087  * Handle processing the extended attribute blocks
4088  */
4089 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
4090                            char *block_buf, struct ea_quota *ea_block_quota)
4091 {
4092         ext2_filsys fs = ctx->fs;
4093         ext2_ino_t      ino = pctx->ino;
4094         struct ext2_inode *inode = pctx->inode;
4095         blk64_t         blk;
4096         char *          end;
4097         struct ext2_ext_attr_header *header;
4098         struct ext2_ext_attr_entry *first, *entry;
4099         blk64_t         quota_blocks = EXT2FS_C2B(fs, 1);
4100         __u64           quota_inodes = 0;
4101         region_t        region = 0;
4102         int             failed_csum = 0;
4103
4104         ea_block_quota->blocks = 0;
4105         ea_block_quota->inodes = 0;
4106
4107         blk = ext2fs_file_acl_block(fs, inode);
4108         if (blk == 0)
4109                 return 0;
4110
4111         /*
4112          * If the Extended attribute flag isn't set, then a non-zero
4113          * file acl means that the inode is corrupted.
4114          *
4115          * Or if the extended attribute block is an invalid block,
4116          * then the inode is also corrupted.
4117          */
4118         if (!ext2fs_has_feature_xattr(fs->super) ||
4119             (blk < fs->super->s_first_data_block) ||
4120             (blk >= ext2fs_blocks_count(fs->super))) {
4121                 /* Fixed in pass2, e2fsck_process_bad_inode(). */
4122                 e2fsck_mark_inode_bad(ctx, pctx, PR_2_FILE_ACL_ZERO);
4123                 return 0;
4124         }
4125
4126         /* If ea bitmap hasn't been allocated, create it */
4127         if (!ctx->block_ea_map) {
4128                 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
4129                                         _("ext attr block map"),
4130                                         EXT2FS_BMAP64_RBTREE, "block_ea_map",
4131                                         &ctx->block_ea_map);
4132                 if (pctx->errcode) {
4133                         pctx->num = 2;
4134                         fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
4135                         ctx->flags |= E2F_FLAG_ABORT;
4136                         return 0;
4137                 }
4138         }
4139
4140         /* Create the EA refcount structure if necessary */
4141         if (!ctx->refcount) {
4142                 pctx->errcode = ea_refcount_create(0,
4143                                         &ctx->refcount_orig);
4144                 if (pctx->errcode) {
4145                         pctx->num = 1;
4146                         fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4147                         ctx->flags |= E2F_FLAG_ABORT;
4148                         return 0;
4149                 }
4150
4151                 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
4152                 if (pctx->errcode) {
4153                         pctx->num = 1;
4154                         fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4155                         ctx->flags |= E2F_FLAG_ABORT;
4156                         return 0;
4157                 }
4158         }
4159
4160 #if 0
4161         /* Debugging text */
4162         printf("Inode %u has EA block %u\n", ino, blk);
4163 #endif
4164
4165         /* Have we seen this EA block before? */
4166         if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
4167                 ea_block_quota->blocks = EXT2FS_C2B(fs, 1);
4168                 ea_block_quota->inodes = 0;
4169
4170                 if (ctx->ea_block_quota_blocks) {
4171                         ea_refcount_fetch(ctx->ea_block_quota_blocks, blk,
4172                                           &quota_blocks);
4173                         if (quota_blocks)
4174                                 ea_block_quota->blocks = quota_blocks;
4175                 }
4176
4177                 if (ctx->ea_block_quota_inodes)
4178                         ea_refcount_fetch(ctx->ea_block_quota_inodes, blk,
4179                                           &ea_block_quota->inodes);
4180
4181                 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
4182                         return 1;
4183                 /* Ooops, this EA was referenced more than it stated */
4184                 if (!ctx->refcount_extra) {
4185                         pctx->errcode = ea_refcount_create(0,
4186                                            &ctx->refcount_extra);
4187                         if (pctx->errcode) {
4188                                 pctx->num = 2;
4189                                 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4190                                 ctx->flags |= E2F_FLAG_ABORT;
4191                                 return 0;
4192                         }
4193                 }
4194                 ea_refcount_increment(ctx->refcount_extra, blk, 0);
4195                 return 1;
4196         }
4197
4198         /*
4199          * OK, we haven't seen this EA block yet.  So we need to
4200          * validate it
4201          */
4202         pctx->blk = blk;
4203         pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
4204         if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
4205                 pctx->errcode = 0;
4206                 failed_csum = 1;
4207         } else if (pctx->errcode == EXT2_ET_BAD_EA_HEADER)
4208                 pctx->errcode = 0;
4209
4210         if (pctx->errcode &&
4211             fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) {
4212                 pctx->errcode = 0;
4213                 goto clear_extattr;
4214         }
4215         header = BHDR(block_buf);
4216         pctx->blk = ext2fs_file_acl_block(fs, inode);
4217         if (((ctx->ext_attr_ver == 1) &&
4218              (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
4219             ((ctx->ext_attr_ver == 2) &&
4220              (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
4221                 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
4222                         goto clear_extattr;
4223         }
4224
4225         if (header->h_blocks != 1) {
4226                 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
4227                         goto clear_extattr;
4228         }
4229
4230         if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
4231                 goto clear_extattr;
4232
4233         region = region_create(0, fs->blocksize);
4234         if (!region) {
4235                 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
4236                 ctx->flags |= E2F_FLAG_ABORT;
4237                 return 0;
4238         }
4239         if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
4240                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4241                         goto clear_extattr;
4242         }
4243
4244         first = (struct ext2_ext_attr_entry *)(header+1);
4245         end = block_buf + fs->blocksize;
4246         entry = first;
4247         while ((char *)entry < end && *(__u32 *)entry) {
4248                 __u32 hash;
4249
4250                 if (region_allocate(region, (char *)entry - (char *)header,
4251                                    EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
4252                         if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4253                                 goto clear_extattr;
4254                         break;
4255                 }
4256                 if ((ctx->ext_attr_ver == 1 &&
4257                      (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
4258                     (ctx->ext_attr_ver == 2 &&
4259                      entry->e_name_index == 0)) {
4260                         if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
4261                                 goto clear_extattr;
4262                         break;
4263                 }
4264                 if (entry->e_value_inum == 0) {
4265                         if (entry->e_value_offs + entry->e_value_size >
4266                             fs->blocksize) {
4267                                 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
4268                                         goto clear_extattr;
4269                                 break;
4270                         }
4271                         if (entry->e_value_size &&
4272                             region_allocate(region, entry->e_value_offs,
4273                                             EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
4274                                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
4275                                                 pctx))
4276                                         goto clear_extattr;
4277                         }
4278
4279                         hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
4280                                                           entry->e_value_offs);
4281
4282                         if (entry->e_hash != hash) {
4283                                 pctx->num = entry->e_hash;
4284                                 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
4285                                         goto clear_extattr;
4286                                 entry->e_hash = hash;
4287                         }
4288                 } else {
4289                         problem_t problem;
4290                         blk64_t entry_quota_blocks;
4291
4292                         problem = check_large_ea_inode(ctx, entry, pctx,
4293                                                        &entry_quota_blocks);
4294                         if (problem && fix_problem(ctx, problem, pctx))
4295                                 goto clear_extattr;
4296
4297                         quota_blocks += entry_quota_blocks;
4298                         quota_inodes++;
4299                 }
4300
4301                 entry = EXT2_EXT_ATTR_NEXT(entry);
4302         }
4303         if (region_allocate(region, (char *)entry - (char *)header, 4)) {
4304                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4305                         goto clear_extattr;
4306         }
4307         region_free(region);
4308
4309         /*
4310          * We only get here if there was no other errors that were fixed.
4311          * If there was a checksum fail, ask to correct it.
4312          */
4313         if (failed_csum &&
4314             fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
4315                 pctx->errcode = e2fsck_write_ext_attr3(ctx, blk, block_buf,
4316                                                        pctx->ino);
4317                 if (pctx->errcode)
4318                         return 0;
4319         }
4320
4321         if (quota_blocks != EXT2FS_C2B(fs, 1U)) {
4322                 if (!ctx->ea_block_quota_blocks) {
4323                         pctx->errcode = ea_refcount_create(0,
4324                                                 &ctx->ea_block_quota_blocks);
4325                         if (pctx->errcode) {
4326                                 pctx->num = 3;
4327                                 goto refcount_fail;
4328                         }
4329                 }
4330                 ea_refcount_store(ctx->ea_block_quota_blocks, blk,
4331                                   quota_blocks);
4332         }
4333
4334         if (quota_inodes) {
4335                 if (!ctx->ea_block_quota_inodes) {
4336                         pctx->errcode = ea_refcount_create(0,
4337                                                 &ctx->ea_block_quota_inodes);
4338                         if (pctx->errcode) {
4339                                 pctx->num = 4;
4340 refcount_fail:
4341                                 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4342                                 ctx->flags |= E2F_FLAG_ABORT;
4343                                 return 0;
4344                         }
4345                 }
4346
4347                 ea_refcount_store(ctx->ea_block_quota_inodes, blk,
4348                                   quota_inodes);
4349         }
4350         ea_block_quota->blocks = quota_blocks;
4351         ea_block_quota->inodes = quota_inodes;
4352
4353         inc_ea_inode_refs(ctx, pctx, first, end);
4354         ea_refcount_store(ctx->refcount, blk, header->h_refcount - 1);
4355         ea_refcount_store(ctx->refcount_orig, blk, header->h_refcount);
4356         /**
4357          * It might be racy that this block has been merged in the
4358          * global found map.
4359          */
4360         if (!is_blocks_used(ctx, blk, 1))
4361                 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, blk);
4362         ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
4363         return 1;
4364
4365 clear_extattr:
4366         if (region)
4367                 region_free(region);
4368         ext2fs_file_acl_block_set(fs, inode, 0);
4369         e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
4370         return 0;
4371 }
4372
4373 /* Returns 1 if bad htree, 0 if OK */
4374 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
4375                         ext2_ino_t ino, struct ext2_inode *inode,
4376                         char *block_buf)
4377 {
4378         struct ext2_dx_root_info        *root;
4379         ext2_filsys                     fs = ctx->fs;
4380         errcode_t                       retval;
4381         blk64_t                         blk;
4382
4383         if ((!LINUX_S_ISDIR(inode->i_mode) &&
4384              fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
4385             (!ext2fs_has_feature_dir_index(fs->super) &&
4386              fix_problem(ctx, PR_1_HTREE_SET, pctx)))
4387                 return 1;
4388
4389         pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
4390
4391         if ((pctx->errcode) ||
4392             (blk == 0) ||
4393             (blk < fs->super->s_first_data_block) ||
4394             (blk >= ext2fs_blocks_count(fs->super))) {
4395                 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4396                         return 1;
4397                 else
4398                         return 0;
4399         }
4400
4401         retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
4402         if (retval) {
4403                 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4404                         return 1;
4405         }
4406
4407         /* XXX should check that beginning matches a directory */
4408         root = get_ext2_dx_root_info(fs, block_buf);
4409
4410         if ((root->reserved_zero || root->info_length < 8) &&
4411             fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4412                 return 1;
4413
4414         pctx->num = root->hash_version;
4415         if ((root->hash_version != EXT2_HASH_LEGACY) &&
4416             (root->hash_version != EXT2_HASH_HALF_MD4) &&
4417             (root->hash_version != EXT2_HASH_TEA) &&
4418             (root->hash_version != EXT2_HASH_SIPHASH) &&
4419             fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
4420                 return 1;
4421
4422         if (ext4_hash_in_dirent(inode)) {
4423                 if (root->hash_version != EXT2_HASH_SIPHASH &&
4424                     fix_problem(ctx, PR_1_HTREE_NEEDS_SIPHASH, pctx))
4425                         return 1;
4426         } else {
4427                 if (root->hash_version == EXT2_HASH_SIPHASH &&
4428                    fix_problem(ctx, PR_1_HTREE_CANNOT_SIPHASH, pctx))
4429                         return 1;
4430         }
4431
4432         if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
4433             fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
4434                 return 1;
4435
4436         pctx->num = root->indirect_levels;
4437         /* if htree level is clearly too high, consider it to be broken */
4438         if (root->indirect_levels > EXT4_HTREE_LEVEL &&
4439             fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
4440                 return 1;
4441
4442         /* if level is only maybe too high, LARGE_DIR feature could be unset */
4443         if (root->indirect_levels > ext2_dir_htree_level(fs) &&
4444             !ext2fs_has_feature_largedir(fs->super)) {
4445                 int blockbits = EXT2_BLOCK_SIZE_BITS(fs->super) + 10;
4446                 int idx_pb = 1 << (blockbits - 3);
4447
4448                 /* compare inode size/blocks vs. max-sized 2-level htree */
4449                 if (EXT2_I_SIZE(pctx->inode) <
4450                     (idx_pb - 1) * (idx_pb - 2) << blockbits &&
4451                     pctx->inode->i_blocks <
4452                     (idx_pb - 1) * (idx_pb - 2) << (blockbits - 9) &&
4453                     fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
4454                         return 1;
4455         }
4456
4457         if (root->indirect_levels > EXT4_HTREE_LEVEL_COMPAT ||
4458             ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
4459                 ctx->large_dirs++;
4460
4461         return 0;
4462 }
4463
4464 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
4465                         struct ext2_inode *inode, int restart_flag,
4466                         const char *source)
4467 {
4468         inode->i_flags = 0;
4469         inode->i_links_count = 0;
4470         ext2fs_icount_store(ctx->inode_link_info, ino, 0);
4471         inode->i_dtime = ctx->now;
4472
4473         /*
4474          * If a special inode has such rotten block mappings that we
4475          * want to clear the whole inode, be sure to actually zap
4476          * the block maps because i_links_count isn't checked for
4477          * special inodes, and we'll end up right back here the next
4478          * time we run fsck.
4479          */
4480         if (ino < EXT2_FIRST_INODE(ctx->fs->super))
4481                 memset(inode->i_block, 0, sizeof(inode->i_block));
4482
4483         ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
4484         ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
4485         if (ctx->inode_reg_map)
4486                 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
4487         if (ctx->inode_badness)
4488                 ext2fs_icount_store(ctx->inode_badness, ino, 0);
4489
4490         /*
4491          * If the inode was partially accounted for before processing
4492          * was aborted, we need to restart the pass 1 scan.
4493          */
4494         ctx->flags |= restart_flag;
4495
4496         if (ino == EXT2_BAD_INO)
4497                 memset(inode, 0, sizeof(struct ext2_inode));
4498
4499         e2fsck_write_inode(ctx, ino, inode, source);
4500 }
4501
4502 /*
4503  * Use the multiple-blocks reclamation code to fix alignment problems in
4504  * a bigalloc filesystem.  We want a logical cluster to map to *only* one
4505  * physical cluster, and we want the block offsets within that cluster to
4506  * line up.
4507  */
4508 static int has_unaligned_cluster_map(e2fsck_t ctx,
4509                                      blk64_t last_pblk, blk64_t last_lblk,
4510                                      blk64_t pblk, blk64_t lblk)
4511 {
4512         blk64_t cluster_mask;
4513
4514         if (!ctx->fs->cluster_ratio_bits)
4515                 return 0;
4516         cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
4517
4518         /*
4519          * If the block in the logical cluster doesn't align with the block in
4520          * the physical cluster...
4521          */
4522         if ((lblk & cluster_mask) != (pblk & cluster_mask))
4523                 return 1;
4524
4525         /*
4526          * If we cross a physical cluster boundary within a logical cluster...
4527          */
4528         if (last_pblk && (lblk & cluster_mask) != 0 &&
4529             EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
4530             EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
4531                 return 1;
4532
4533         return 0;
4534 }
4535
4536 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
4537                              struct process_block_struct *pb,
4538                              blk64_t start_block, blk64_t end_block,
4539                              blk64_t eof_block,
4540                              ext2_extent_handle_t ehandle,
4541                              int try_repairs)
4542 {
4543         struct ext2fs_extent    extent;
4544         blk64_t                 blk, last_lblk;
4545         unsigned int            i, n;
4546         int                     is_dir, is_leaf;
4547         problem_t               problem;
4548         struct ext2_extent_info info;
4549         int                     failed_csum = 0;
4550
4551         if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID)
4552                 failed_csum = 1;
4553
4554         pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
4555         if (pctx->errcode)
4556                 return;
4557         if (!(ctx->options & E2F_OPT_FIXES_ONLY) &&
4558             !pb->eti.force_rebuild) {
4559                 struct extent_tree_level *etl;
4560
4561                 etl = pb->eti.ext_info + info.curr_level;
4562                 etl->num_extents += info.num_entries;
4563                 etl->max_extents += info.max_entries;
4564                 /*
4565                  * Implementation wart: Splitting extent blocks when appending
4566                  * will leave the old block with one free entry.  Therefore
4567                  * unless the node is totally full, pretend that a non-root
4568                  * extent block can hold one fewer entry than it actually does,
4569                  * so that we don't repeatedly rebuild the extent tree.
4570                  */
4571                 if (info.curr_level && info.num_entries < info.max_entries)
4572                         etl->max_extents--;
4573         }
4574
4575         pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
4576                                           &extent);
4577         while ((pctx->errcode == 0 ||
4578                 pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
4579                info.num_entries-- > 0) {
4580                 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
4581                 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
4582                 last_lblk = extent.e_lblk + extent.e_len - 1;
4583
4584                 problem = 0;
4585                 pctx->blk = extent.e_pblk;
4586                 pctx->blk2 = extent.e_lblk;
4587                 pctx->num = extent.e_len;
4588                 pctx->blkcount = extent.e_lblk + extent.e_len;
4589
4590                 if (extent.e_pblk == 0 ||
4591                     extent.e_pblk < ctx->fs->super->s_first_data_block ||
4592                     extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
4593                         problem = PR_1_EXTENT_BAD_START_BLK;
4594                 else if (extent.e_lblk < start_block)
4595                         problem = PR_1_OUT_OF_ORDER_EXTENTS;
4596                 else if ((end_block && last_lblk > end_block) &&
4597                          !(last_lblk > eof_block &&
4598                            ((extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) ||
4599                             (pctx->inode->i_flags & EXT4_VERITY_FL))))
4600                         problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
4601                 else if (is_leaf && extent.e_len == 0)
4602                         problem = PR_1_EXTENT_LENGTH_ZERO;
4603                 else if (is_leaf &&
4604                          (extent.e_pblk + extent.e_len) >
4605                          ext2fs_blocks_count(ctx->fs->super))
4606                         problem = PR_1_EXTENT_ENDS_BEYOND;
4607                 else if (is_leaf && is_dir && !pctx->inode->i_size_high &&
4608                          !ext2fs_has_feature_largedir(ctx->fs->super) &&
4609                          ((extent.e_lblk + extent.e_len) >
4610                           (1U << (21 - ctx->fs->super->s_log_block_size))))
4611                         problem = PR_1_TOOBIG_DIR;
4612
4613                 if (is_leaf && problem == 0 && extent.e_len > 0) {
4614 #if 0
4615                         printf("extent_region(ino=%u, expect=%llu, "
4616                                "lblk=%llu, len=%u)\n", pb->ino,
4617                                (unsigned long long) pb->next_lblock,
4618                                (unsigned long long) extent.e_lblk,
4619                                extent.e_len);
4620 #endif
4621                         if (extent.e_lblk < pb->next_lblock)
4622                                 problem = PR_1_EXTENT_COLLISION;
4623                         else if (extent.e_lblk + extent.e_len > pb->next_lblock)
4624                                 pb->next_lblock = extent.e_lblk + extent.e_len;
4625                 }
4626
4627                 /*
4628                  * Uninitialized blocks in a directory?  Clear the flag and
4629                  * we'll interpret the blocks later.
4630                  */
4631                 if (try_repairs && is_dir && problem == 0 &&
4632                     (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4633                     fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
4634                         e2fsck_pass1_fix_lock(ctx);
4635                         extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4636                         pb->inode_modified = 1;
4637                         pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4638                                                               &extent);
4639                         e2fsck_pass1_fix_unlock(ctx);
4640                         if (pctx->errcode)
4641                                 return;
4642                         failed_csum = 0;
4643                 }
4644 #ifdef CONFIG_DEVELOPER_FEATURES
4645                 if (try_repairs && !is_dir && problem == 0 &&
4646                     (ctx->options & E2F_OPT_CLEAR_UNINIT) &&
4647                     (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4648                     fix_problem(ctx, PR_1_CLEAR_UNINIT_EXTENT, pctx)) {
4649                         extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4650                         pb->inode_modified = 1;
4651                         pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4652                                                               &extent);
4653                         if (pctx->errcode)
4654                                 return;
4655                         failed_csum = 0;
4656                 }
4657 #endif
4658                 if (try_repairs && problem) {
4659 report_problem:
4660                         /* Record badness only if extent is within inode */
4661                         if (fix_problem_bad(ctx, problem, pctx,
4662                                             info.curr_level == 0)) {
4663                                 if (ctx->invalid_bitmaps) {
4664                                         /*
4665                                          * If fsck knows the bitmaps are bad,
4666                                          * skip to the next extent and
4667                                          * try to clear this extent again
4668                                          * after fixing the bitmaps, by
4669                                          * restarting fsck.
4670                                          */
4671                                         pctx->errcode = ext2fs_extent_get(
4672                                                           ehandle,
4673                                                           EXT2_EXTENT_NEXT_SIB,
4674                                                           &extent);
4675                                         ctx->flags |= E2F_FLAG_RESTART_LATER;
4676                                         if (pctx->errcode ==
4677                                                     EXT2_ET_NO_CURRENT_NODE) {
4678                                                 pctx->errcode = 0;
4679                                                 break;
4680                                         }
4681                                         continue;
4682                                 }
4683                                 e2fsck_pass1_fix_lock(ctx);
4684                                 e2fsck_read_bitmaps(ctx);
4685                                 pb->inode_modified = 1;
4686                                 pctx->errcode =
4687                                         ext2fs_extent_delete(ehandle, 0);
4688                                 e2fsck_pass1_fix_unlock(ctx);
4689                                 if (pctx->errcode) {
4690                                         pctx->str = "ext2fs_extent_delete";
4691                                         return;
4692                                 }
4693                                 e2fsck_pass1_fix_lock(ctx);
4694                                 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4695                                 e2fsck_pass1_fix_unlock(ctx);
4696                                 if (pctx->errcode &&
4697                                     pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
4698                                         pctx->str = "ext2fs_extent_fix_parents";
4699                                         return;
4700                                 }
4701                                 pctx->errcode = ext2fs_extent_get(ehandle,
4702                                                                   EXT2_EXTENT_CURRENT,
4703                                                                   &extent);
4704                                 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
4705                                         pctx->errcode = 0;
4706                                         break;
4707                                 }
4708                                 failed_csum = 0;
4709                                 continue;
4710                         }
4711                         goto next;
4712                 }
4713
4714                 if (!is_leaf) {
4715                         blk64_t lblk = extent.e_lblk;
4716                         int next_try_repairs = 1;
4717
4718                         blk = extent.e_pblk;
4719
4720                         /*
4721                          * If this lower extent block collides with critical
4722                          * metadata, don't try to repair the damage.  Pass 1b
4723                          * will reallocate the block; then we can try again.
4724                          */
4725                         if (pb->ino != EXT2_RESIZE_INO &&
4726                             extent.e_pblk < ctx->fs->super->s_blocks_count &&
4727                             ext2fs_test_block_bitmap2(ctx->block_metadata_map,
4728                                                       extent.e_pblk)) {
4729                                 next_try_repairs = 0;
4730                                 pctx->blk = blk;
4731                                 fix_problem_bad(ctx,
4732                                             PR_1_CRITICAL_METADATA_COLLISION,
4733                                             pctx, 2);
4734                                 if ((ctx->options & E2F_OPT_NO) == 0)
4735                                         ctx->flags |= E2F_FLAG_RESTART_LATER;
4736                         }
4737                         pctx->errcode = ext2fs_extent_get(ehandle,
4738                                                   EXT2_EXTENT_DOWN, &extent);
4739                         if (pctx->errcode &&
4740                             pctx->errcode != EXT2_ET_EXTENT_CSUM_INVALID) {
4741                                 pctx->str = "EXT2_EXTENT_DOWN";
4742                                 problem = PR_1_EXTENT_HEADER_INVALID;
4743                                 if (!next_try_repairs)
4744                                         return;
4745                                 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
4746                                         goto report_problem;
4747                                 return;
4748                         }
4749                         /* The next extent should match this index's logical start */
4750                         if (extent.e_lblk != lblk) {
4751                                 struct ext2_extent_info e_info;
4752
4753                                 pctx->errcode = ext2fs_extent_get_info(ehandle,
4754                                                                        &e_info);
4755                                 if (pctx->errcode) {
4756                                         pctx->str = "ext2fs_extent_get_info";
4757                                         return;
4758                                 }
4759                                 pctx->blk = lblk;
4760                                 pctx->blk2 = extent.e_lblk;
4761                                 pctx->num = e_info.curr_level - 1;
4762                                 problem = PR_1_EXTENT_INDEX_START_INVALID;
4763                                 if (fix_problem(ctx, problem, pctx)) {
4764                                         e2fsck_pass1_fix_lock(ctx);
4765                                         pb->inode_modified = 1;
4766                                         pctx->errcode =
4767                                                 ext2fs_extent_fix_parents(ehandle);
4768                                         e2fsck_pass1_fix_unlock(ctx);
4769                                         if (pctx->errcode) {
4770                                                 pctx->str = "ext2fs_extent_fix_parents";
4771                                                 return;
4772                                         }
4773                                 }
4774                         }
4775                         scan_extent_node(ctx, pctx, pb, extent.e_lblk,
4776                                          last_lblk, eof_block, ehandle,
4777                                          next_try_repairs);
4778                         if (pctx->errcode)
4779                                 return;
4780                         pctx->errcode = ext2fs_extent_get(ehandle,
4781                                                   EXT2_EXTENT_UP, &extent);
4782                         if (pctx->errcode) {
4783                                 pctx->str = "EXT2_EXTENT_UP";
4784                                 return;
4785                         }
4786                         mark_block_used(ctx, blk);
4787                         pb->num_blocks++;
4788                         goto next;
4789                 }
4790
4791                 if ((pb->previous_block != 0) &&
4792                     (pb->previous_block+1 != extent.e_pblk)) {
4793                         if (ctx->options & E2F_OPT_FRAGCHECK) {
4794                                 char type = '?';
4795
4796                                 if (pb->is_dir)
4797                                         type = 'd';
4798                                 else if (pb->is_reg)
4799                                         type = 'f';
4800
4801                                 printf(("%6lu(%c): expecting %6lu "
4802                                         "actual extent "
4803                                         "phys %6lu log %lu len %lu\n"),
4804                                        (unsigned long) pctx->ino, type,
4805                                        (unsigned long) pb->previous_block+1,
4806                                        (unsigned long) extent.e_pblk,
4807                                        (unsigned long) extent.e_lblk,
4808                                        (unsigned long) extent.e_len);
4809                         }
4810                         pb->fragmented = 1;
4811                 }
4812                 /*
4813                  * If we notice a gap in the logical block mappings of an
4814                  * extent-mapped directory, offer to close the hole by
4815                  * moving the logical block down, otherwise we'll go mad in
4816                  * pass 3 allocating empty directory blocks to fill the hole.
4817                  */
4818                 if (try_repairs && is_dir &&
4819                     pb->last_block + 1 < extent.e_lblk) {
4820                         blk64_t new_lblk;
4821
4822                         new_lblk = pb->last_block + 1;
4823                         if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
4824                                 new_lblk = ((new_lblk +
4825                                              EXT2FS_CLUSTER_RATIO(ctx->fs) - 1) &
4826                                             ~EXT2FS_CLUSTER_MASK(ctx->fs)) |
4827                                            (extent.e_pblk &
4828                                             EXT2FS_CLUSTER_MASK(ctx->fs));
4829                         pctx->blk = extent.e_lblk;
4830                         pctx->blk2 = new_lblk;
4831                         if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
4832                                 e2fsck_pass1_fix_lock(ctx);
4833                                 extent.e_lblk = new_lblk;
4834                                 pb->inode_modified = 1;
4835                                 pctx->errcode = ext2fs_extent_replace(ehandle,
4836                                                                 0, &extent);
4837                                 e2fsck_pass1_fix_unlock(ctx);
4838                                 if (pctx->errcode) {
4839                                         pctx->errcode = 0;
4840                                         goto alloc_later;
4841                                 }
4842                                 e2fsck_pass1_fix_lock(ctx);
4843                                 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4844                                 e2fsck_pass1_fix_unlock(ctx);
4845                                 if (pctx->errcode)
4846                                         goto failed_add_dir_block;
4847                                 pctx->errcode = ext2fs_extent_goto(ehandle,
4848                                                                 extent.e_lblk);
4849                                 if (pctx->errcode)
4850                                         goto failed_add_dir_block;
4851                                 last_lblk = extent.e_lblk + extent.e_len - 1;
4852                                 failed_csum = 0;
4853                         }
4854                 }
4855 alloc_later:
4856                 if (is_dir) {
4857                         while (++pb->last_db_block <
4858                                (e2_blkcnt_t) extent.e_lblk) {
4859                                 pctx->errcode = ext2fs_add_dir_block2(
4860                                                         ctx->fs->dblist,
4861                                                         pb->ino, 0,
4862                                                         pb->last_db_block);
4863                                 if (pctx->errcode) {
4864                                         pctx->blk = 0;
4865                                         pctx->num = pb->last_db_block;
4866                                         goto failed_add_dir_block;
4867                                 }
4868                         }
4869
4870                         for (i = 0; i < extent.e_len; i++) {
4871                                 pctx->errcode = ext2fs_add_dir_block2(
4872                                                         ctx->fs->dblist,
4873                                                         pctx->ino,
4874                                                         extent.e_pblk + i,
4875                                                         extent.e_lblk + i);
4876                                 if (pctx->errcode) {
4877                                         pctx->blk = extent.e_pblk + i;
4878                                         pctx->num = extent.e_lblk + i;
4879                                 failed_add_dir_block:
4880                                         fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
4881                                         /* Should never get here */
4882                                         ctx->flags |= E2F_FLAG_ABORT;
4883                                         return;
4884                                 }
4885                         }
4886                         if (extent.e_len > 0)
4887                                 pb->last_db_block = extent.e_lblk + extent.e_len - 1;
4888                 }
4889                 if (has_unaligned_cluster_map(ctx, pb->previous_block,
4890                                               pb->last_block,
4891                                               extent.e_pblk,
4892                                               extent.e_lblk)) {
4893                         for (i = 0; i < extent.e_len; i++) {
4894                                 pctx->blk = extent.e_lblk + i;
4895                                 pctx->blk2 = extent.e_pblk + i;
4896                                 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
4897                                 mark_block_used(ctx, extent.e_pblk + i);
4898                                 mark_block_used(ctx, extent.e_pblk + i);
4899                         }
4900                 }
4901
4902                 /*
4903                  * Check whether first cluster got marked in previous iteration.
4904                  */
4905                 if (ctx->fs->cluster_ratio_bits &&
4906                     pb->previous_block &&
4907                     (EXT2FS_B2C(ctx->fs, extent.e_pblk) ==
4908                      EXT2FS_B2C(ctx->fs, pb->previous_block)))
4909                         /* Set blk to the beginning of next cluster. */
4910                         blk = EXT2FS_C2B(
4911                                 ctx->fs,
4912                                 EXT2FS_B2C(ctx->fs, extent.e_pblk) + 1);
4913                 else
4914                         /* Set blk to the beginning of current cluster. */
4915                         blk = EXT2FS_C2B(ctx->fs,
4916                                          EXT2FS_B2C(ctx->fs, extent.e_pblk));
4917
4918                 if (blk < extent.e_pblk + extent.e_len) {
4919                         mark_blocks_used(ctx, blk,
4920                                          extent.e_pblk + extent.e_len - blk);
4921                         n = DIV_ROUND_UP(extent.e_pblk + extent.e_len - blk,
4922                                          EXT2FS_CLUSTER_RATIO(ctx->fs));
4923                         pb->num_blocks += n;
4924                 }
4925                 pb->last_block = extent.e_lblk + extent.e_len - 1;
4926                 pb->previous_block = extent.e_pblk + extent.e_len - 1;
4927                 start_block = pb->last_block = last_lblk;
4928                 if (is_leaf && !is_dir &&
4929                     !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
4930                         pb->last_init_lblock = last_lblk;
4931         next:
4932                 pctx->errcode = ext2fs_extent_get(ehandle,
4933                                                   EXT2_EXTENT_NEXT_SIB,
4934                                                   &extent);
4935         }
4936
4937         /* Failed csum but passes checks?  Ask to fix checksum. */
4938         if (failed_csum &&
4939             fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID, pctx)) {
4940                 e2fsck_pass1_fix_lock(ctx);
4941                 pb->inode_modified = 1;
4942                 pctx->errcode = ext2fs_extent_replace(ehandle, 0, &extent);
4943                 e2fsck_pass1_fix_unlock(ctx);
4944                 if (pctx->errcode)
4945                         return;
4946         }
4947
4948         if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
4949                 pctx->errcode = 0;
4950 }
4951
4952 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
4953                                  struct process_block_struct *pb)
4954 {
4955         struct ext2_extent_info info;
4956         struct ext2_inode       *inode = pctx->inode;
4957         ext2_extent_handle_t    ehandle;
4958         ext2_filsys             fs = ctx->fs;
4959         ext2_ino_t              ino = pctx->ino;
4960         errcode_t               retval;
4961         blk64_t                 eof_lblk;
4962         struct ext3_extent_header       *eh;
4963
4964         /* Check for a proper extent header... */
4965         eh = (struct ext3_extent_header *) &inode->i_block[0];
4966         retval = ext2fs_extent_header_verify(eh, sizeof(inode->i_block));
4967         if (retval) {
4968                 if (fix_problem(ctx, PR_1_MISSING_EXTENT_HEADER, pctx))
4969                         e2fsck_clear_inode(ctx, ino, inode, 0,
4970                                            "check_blocks_extents");
4971                 pctx->errcode = 0;
4972                 return;
4973         }
4974
4975         /* ...since this function doesn't fail if i_block is zeroed. */
4976         pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
4977         if (pctx->errcode) {
4978                 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
4979                         e2fsck_clear_inode(ctx, ino, inode, 0,
4980                                            "check_blocks_extents");
4981                 pctx->errcode = 0;
4982                 return;
4983         }
4984
4985         retval = ext2fs_extent_get_info(ehandle, &info);
4986         if (retval == 0) {
4987                 int max_depth = info.max_depth;
4988
4989                 if (max_depth >= MAX_EXTENT_DEPTH_COUNT)
4990                         max_depth = MAX_EXTENT_DEPTH_COUNT-1;
4991                 ctx->extent_depth_count[max_depth]++;
4992         }
4993
4994         /* Check maximum extent depth */
4995         pctx->blk = info.max_depth;
4996         pctx->blk2 = ext2fs_max_extent_depth(ehandle);
4997         if (pctx->blk2 < pctx->blk &&
4998             fix_problem(ctx, PR_1_EXTENT_BAD_MAX_DEPTH, pctx))
4999                 pb->eti.force_rebuild = 1;
5000
5001         /* Can we collect extent tree level stats? */
5002         pctx->blk = MAX_EXTENT_DEPTH_COUNT;
5003         if (pctx->blk2 > pctx->blk)
5004                 fix_problem(ctx, PR_1E_MAX_EXTENT_TREE_DEPTH, pctx);
5005         memset(pb->eti.ext_info, 0, sizeof(pb->eti.ext_info));
5006         pb->eti.ino = pb->ino;
5007
5008         pb->next_lblock = 0;
5009
5010         eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
5011                 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
5012         scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle, 1);
5013         if (pctx->errcode &&
5014             fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
5015                 pb->num_blocks = 0;
5016                 inode->i_blocks = 0;
5017                 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
5018                                    "check_blocks_extents");
5019                 pctx->errcode = 0;
5020         }
5021         ext2fs_extent_free(ehandle);
5022
5023         /* Rebuild unless it's a dir and we're rehashing it */
5024         if (LINUX_S_ISDIR(inode->i_mode) &&
5025             e2fsck_dir_will_be_rehashed(ctx, ino))
5026                 return;
5027
5028         if (ctx->options & E2F_OPT_CONVERT_BMAP)
5029                 e2fsck_rebuild_extents_later(ctx, ino);
5030         else
5031                 e2fsck_should_rebuild_extents(ctx, pctx, &pb->eti, &info);
5032 }
5033
5034 /*
5035  * In fact we don't need to check blocks for an inode with inline data
5036  * because this inode doesn't have any blocks.  In this function all
5037  * we need to do is add this inode into dblist when it is a directory.
5038  */
5039 static void check_blocks_inline_data(e2fsck_t ctx, struct problem_context *pctx,
5040                                      struct process_block_struct *pb)
5041 {
5042         int     flags;
5043         size_t  inline_data_size = 0;
5044
5045         if (!pb->is_dir) {
5046                 pctx->errcode = 0;
5047                 return;
5048         }
5049
5050         /* Process the dirents in i_block[] as the "first" block. */
5051         pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 0);
5052         if (pctx->errcode)
5053                 goto err;
5054
5055         /* Process the dirents in the EA as a "second" block. */
5056         flags = ctx->fs->flags;
5057         ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
5058         pctx->errcode = ext2fs_inline_data_size(ctx->fs, pb->ino,
5059                                                 &inline_data_size);
5060         ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
5061                          (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5062         if (pctx->errcode) {
5063                 pctx->errcode = 0;
5064                 return;
5065         }
5066
5067         if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE)
5068                 return;
5069
5070         pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 1);
5071         if (pctx->errcode)
5072                 goto err;
5073
5074         return;
5075 err:
5076         pctx->blk = 0;
5077         pctx->num = 0;
5078         fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
5079         ctx->flags |= E2F_FLAG_ABORT;
5080 }
5081
5082 /*
5083  * This subroutine is called on each inode to account for all of the
5084  * blocks used by that inode.
5085  */
5086 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
5087                          char *block_buf, const struct ea_quota *ea_ibody_quota)
5088 {
5089         ext2_filsys fs = ctx->fs;
5090         struct process_block_struct pb;
5091         ext2_ino_t      ino = pctx->ino;
5092         struct ext2_inode *inode = pctx->inode;
5093         unsigned        bad_size = 0;
5094         int             dirty_inode = 0;
5095         int             extent_fs;
5096         int             inlinedata_fs;
5097         __u64           size;
5098         struct ea_quota ea_block_quota;
5099
5100         pb.ino = ino;
5101         pb.num_blocks = EXT2FS_B2C(ctx->fs,
5102                                    ea_ibody_quota ? ea_ibody_quota->blocks : 0);
5103         pb.last_block = ~0;
5104         pb.last_init_lblock = -1;
5105         pb.last_db_block = -1;
5106         pb.num_illegal_blocks = 0;
5107         pb.suppress = 0; pb.clear = 0;
5108         pb.fragmented = 0;
5109         pb.compressed = 0;
5110         pb.previous_block = 0;
5111         pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
5112         pb.is_reg = LINUX_S_ISREG(inode->i_mode);
5113         pb.max_blocks = 1U << (31 - fs->super->s_log_block_size);
5114         pb.inode = inode;
5115         pb.pctx = pctx;
5116         pb.ctx = ctx;
5117         pb.inode_modified = 0;
5118         pb.eti.force_rebuild = 0;
5119         pctx->ino = ino;
5120         pctx->errcode = 0;
5121
5122         extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
5123         inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
5124
5125         if (check_ext_attr(ctx, pctx, block_buf, &ea_block_quota)) {
5126                 if (e2fsck_should_abort(ctx))
5127                         goto out;
5128                 pb.num_blocks += EXT2FS_B2C(ctx->fs, ea_block_quota.blocks);
5129         }
5130
5131         if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL))
5132                 check_blocks_inline_data(ctx, pctx, &pb);
5133         else if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
5134                 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
5135                         check_blocks_extents(ctx, pctx, &pb);
5136                 else {
5137                         int flags;
5138                         /*
5139                          * If we've modified the inode, write it out before
5140                          * iterate() tries to use it.
5141                          */
5142                         if (dirty_inode) {
5143                                 e2fsck_write_inode(ctx, ino, inode,
5144                                                    "check_blocks");
5145                                 dirty_inode = 0;
5146                         }
5147                         flags = fs->flags;
5148                         fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
5149                         pctx->errcode = ext2fs_block_iterate3(fs, ino,
5150                                                 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
5151                                                 block_buf, process_block, &pb);
5152                         /*
5153                          * We do not have uninitialized extents in non extent
5154                          * files.
5155                          */
5156                         pb.last_init_lblock = pb.last_block;
5157                         /*
5158                          * If iterate() changed a block mapping, we have to
5159                          * re-read the inode.  If we decide to clear the
5160                          * inode after clearing some stuff, we'll re-write the
5161                          * bad mappings into the inode!
5162                          */
5163                         if (pb.inode_modified)
5164                                 e2fsck_read_inode(ctx, ino, inode,
5165                                                   "check_blocks");
5166                         fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
5167                                     (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5168
5169                         if (ctx->options & E2F_OPT_CONVERT_BMAP) {
5170 #ifdef DEBUG
5171                                 printf("bmap rebuild ino=%d\n", ino);
5172 #endif
5173                                 if (!LINUX_S_ISDIR(inode->i_mode) ||
5174                                     !e2fsck_dir_will_be_rehashed(ctx, ino))
5175                                         e2fsck_rebuild_extents_later(ctx, ino);
5176                         }
5177                 }
5178         }
5179         end_problem_latch(ctx, PR_LATCH_BLOCK);
5180         end_problem_latch(ctx, PR_LATCH_TOOBIG);
5181         if (e2fsck_should_abort(ctx))
5182                 goto out;
5183         if (pctx->errcode)
5184                 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
5185
5186         if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
5187                 if (LINUX_S_ISDIR(inode->i_mode))
5188                         ctx->fs_fragmented_dir++;
5189                 else
5190                         ctx->fs_fragmented++;
5191         }
5192
5193         if (pb.clear) {
5194                 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
5195                                    "check_blocks");
5196                 return;
5197         }
5198
5199         if (inode->i_flags & EXT2_INDEX_FL) {
5200                 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
5201                         inode->i_flags &= ~EXT2_INDEX_FL;
5202                         dirty_inode++;
5203                 } else {
5204                         e2fsck_add_dx_dir(ctx, ino, inode, pb.last_block+1);
5205                 }
5206         }
5207
5208         if (!pb.num_blocks && pb.is_dir &&
5209             !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
5210                 /*
5211                  * The mode might be in-correct. Increasing the badness by
5212                  * small amount won't hurt much.
5213                  */
5214                 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
5215                         e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
5216                         ctx->fs_directory_count--;
5217                         return;
5218                 }
5219         }
5220
5221         if (ino != quota_type2inum(PRJQUOTA, fs->super) &&
5222             (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) &&
5223             !(inode->i_flags & EXT4_EA_INODE_FL)) {
5224                 quota_data_add(ctx->qctx, (struct ext2_inode_large *) inode,
5225                                ino,
5226                                pb.num_blocks * EXT2_CLUSTER_SIZE(fs->super));
5227                 quota_data_inodes(ctx->qctx, (struct ext2_inode_large *) inode,
5228                                   ino, (ea_ibody_quota ?
5229                                         ea_ibody_quota->inodes : 0) +
5230                                                 ea_block_quota.inodes + 1);
5231         }
5232
5233         if (!ext2fs_has_feature_huge_file(fs->super) ||
5234             !(inode->i_flags & EXT4_HUGE_FILE_FL))
5235                 pb.num_blocks *= (fs->blocksize / 512);
5236         pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
5237 #if 0
5238         printf("inode %u, i_size = %u, last_block = %llu, i_blocks=%llu, num_blocks = %llu\n",
5239                ino, inode->i_size, (unsigned long long) pb.last_block,
5240                (unsigned long long) ext2fs_inode_i_blocks(fs, inode),
5241                (unsigned long long) pb.num_blocks);
5242 #endif
5243         size = EXT2_I_SIZE(inode);
5244         if (pb.is_dir) {
5245                 unsigned nblock = size >> EXT2_BLOCK_SIZE_BITS(fs->super);
5246                 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
5247                         int flags;
5248                         size_t sz = 0;
5249                         errcode_t err;
5250
5251                         flags = ctx->fs->flags;
5252                         ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
5253                         err = ext2fs_inline_data_size(ctx->fs, pctx->ino,
5254                                                       &sz);
5255                         ctx->fs->flags = (flags &
5256                                           EXT2_FLAG_IGNORE_CSUM_ERRORS) |
5257                                          (ctx->fs->flags &
5258                                           ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5259                         if (err || sz != size) {
5260                                 bad_size = 7;
5261                                 pctx->num = sz;
5262                         }
5263                 } else if (size & (fs->blocksize - 1))
5264                         bad_size = 5;
5265                 else if (nblock > (pb.last_block + 1))
5266                         bad_size = 1;
5267                 else if (nblock < (pb.last_block + 1)) {
5268                         if (((pb.last_block + 1) - nblock) >
5269                             fs->super->s_prealloc_dir_blocks)
5270                                 bad_size = 2;
5271                 }
5272         } else {
5273                 if ((pb.last_init_lblock >= 0) &&
5274                     /* Do not allow initialized allocated blocks past i_size*/
5275                     (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
5276                     !(inode->i_flags & EXT4_VERITY_FL))
5277                         bad_size = 3;
5278                 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
5279                          size > ext2_max_sizes[fs->super->s_log_block_size])
5280                         /* too big for a direct/indirect-mapped file */
5281                         bad_size = 4;
5282                 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
5283                          size >
5284                          ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
5285                         /* too big for an extent-based file - 32bit ee_block */
5286                         bad_size = 6;
5287         }
5288         /* i_size for symlinks is checked elsewhere */
5289         if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
5290                 /* Did inline_data set pctx->num earlier? */
5291                 if (bad_size != 7)
5292                         pctx->num = (pb.last_block + 1) * fs->blocksize;
5293                 pctx->group = bad_size;
5294                 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
5295                         ext2fs_inode_size_set(fs, inode, pctx->num);
5296                         if (EXT2_I_SIZE(inode) == 0 &&
5297                             (inode->i_flags & EXT4_INLINE_DATA_FL)) {
5298                                 memset(inode->i_block, 0,
5299                                        sizeof(inode->i_block));
5300                                 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
5301                         }
5302                         dirty_inode++;
5303                 }
5304                 pctx->num = 0;
5305         }
5306         if (LINUX_S_ISREG(inode->i_mode) &&
5307             ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
5308                 ctx->large_files++;
5309         if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
5310             ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
5311              (ext2fs_has_feature_huge_file(fs->super) &&
5312               (inode->i_flags & EXT4_HUGE_FILE_FL) &&
5313               (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
5314                 pctx->num = pb.num_blocks;
5315                 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
5316                         inode->i_blocks = pb.num_blocks;
5317                         inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
5318                         dirty_inode++;
5319                 }
5320                 pctx->num = 0;
5321         }
5322
5323         /*
5324          * The kernel gets mad if we ask it to allocate bigalloc clusters to
5325          * a block mapped file, so rebuild it as an extent file.  We can skip
5326          * symlinks because they're never rewritten.
5327          */
5328         if (ext2fs_has_feature_bigalloc(fs->super) &&
5329             (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode)) &&
5330             ext2fs_inode_data_blocks2(fs, inode) > 0 &&
5331             (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INO(fs->super)) &&
5332             !(inode->i_flags & (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)) &&
5333             fix_problem(ctx, PR_1_NO_BIGALLOC_BLOCKMAP_FILES, pctx)) {
5334                 pctx->errcode = e2fsck_rebuild_extents_later(ctx, ino);
5335                 if (pctx->errcode)
5336                         goto out;
5337         }
5338
5339         if (ctx->dirs_to_hash && pb.is_dir &&
5340             !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
5341             !(inode->i_flags & EXT2_INDEX_FL) &&
5342             ((inode->i_size / fs->blocksize) >= 3))
5343                 e2fsck_rehash_dir_later(ctx, ino);
5344
5345 out:
5346         /* need restart if clearing bad inode after block processing */
5347         if (e2fsck_fix_bad_inode(ctx, pctx))
5348                 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
5349                                    "check_blocks_bad");
5350         else if (dirty_inode)
5351                 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
5352 }
5353
5354 #if 0
5355 /*
5356  * Helper function called by process block when an illegal block is
5357  * found.  It returns a description about why the block is illegal
5358  */
5359 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
5360 {
5361         blk64_t super;
5362         int     i;
5363         static char     problem[80];
5364
5365         super = fs->super->s_first_data_block;
5366         strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
5367         if (block < super) {
5368                 sprintf(problem, "< FIRSTBLOCK (%u)", super);
5369                 return(problem);
5370         } else if (block >= ext2fs_blocks_count(fs->super)) {
5371                 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
5372                 return(problem);
5373         }
5374         for (i = 0; i < fs->group_desc_count; i++) {
5375                 if (block == super) {
5376                         sprintf(problem, "is the superblock in group %d", i);
5377                         break;
5378                 }
5379                 if (block > super &&
5380                     block <= (super + fs->desc_blocks)) {
5381                         sprintf(problem, "is in the group descriptors "
5382                                 "of group %d", i);
5383                         break;
5384                 }
5385                 if (block == ext2fs_block_bitmap_loc(fs, i)) {
5386                         sprintf(problem, "is the block bitmap of group %d", i);
5387                         break;
5388                 }
5389                 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
5390                         sprintf(problem, "is the inode bitmap of group %d", i);
5391                         break;
5392                 }
5393                 if (block >= ext2fs_inode_table_loc(fs, i) &&
5394                     (block < ext2fs_inode_table_loc(fs, i)
5395                      + fs->inode_blocks_per_group)) {
5396                         sprintf(problem, "is in the inode table of group %d",
5397                                 i);
5398                         break;
5399                 }
5400                 super += fs->super->s_blocks_per_group;
5401         }
5402         return(problem);
5403 }
5404 #endif
5405
5406 /*
5407  * This is a helper function for check_blocks().
5408  */
5409 static int process_block(ext2_filsys fs,
5410                   blk64_t       *block_nr,
5411                   e2_blkcnt_t blockcnt,
5412                   blk64_t ref_block EXT2FS_ATTR((unused)),
5413                   int ref_offset EXT2FS_ATTR((unused)),
5414                   void *priv_data)
5415 {
5416         struct process_block_struct *p;
5417         struct problem_context *pctx;
5418         blk64_t blk = *block_nr;
5419         int     ret_code = 0;
5420         problem_t       problem = 0;
5421         e2fsck_t        ctx;
5422
5423         p = (struct process_block_struct *) priv_data;
5424         pctx = p->pctx;
5425         ctx = p->ctx;
5426
5427         /*
5428          * For a directory, add logical block zero for processing even if it's
5429          * not mapped or we'll be perennially stuck with broken "." and ".."
5430          * entries.
5431          */
5432         if (p->is_dir && blockcnt == 0 && blk == 0) {
5433                 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
5434                 if (pctx->errcode) {
5435                         pctx->blk = blk;
5436                         pctx->num = blockcnt;
5437                         goto failed_add_dir_block;
5438                 }
5439                 p->last_db_block++;
5440         }
5441
5442         if (blk == 0)
5443                 return 0;
5444
5445 #if 0
5446         printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
5447                blockcnt);
5448 #endif
5449
5450         /*
5451          * Simplistic fragmentation check.  We merely require that the
5452          * file be contiguous.  (Which can never be true for really
5453          * big files that are greater than a block group.)
5454          */
5455         if (p->previous_block && p->ino != EXT2_RESIZE_INO) {
5456                 if (p->previous_block+1 != blk) {
5457                         if (ctx->options & E2F_OPT_FRAGCHECK) {
5458                                 char type = '?';
5459
5460                                 if (p->is_dir)
5461                                         type = 'd';
5462                                 else if (p->is_reg)
5463                                         type = 'f';
5464
5465                                 printf(_("%6lu(%c): expecting %6lu "
5466                                          "got phys %6lu (blkcnt %lld)\n"),
5467                                        (unsigned long) pctx->ino, type,
5468                                        (unsigned long) p->previous_block+1,
5469                                        (unsigned long) blk,
5470                                        (long long) blockcnt);
5471                         }
5472                         p->fragmented = 1;
5473                 }
5474         }
5475
5476         if (p->is_dir && !ext2fs_has_feature_largedir(fs->super) &&
5477             !pctx->inode->i_size_high &&
5478             blockcnt > (1 << (21 - fs->super->s_log_block_size)))
5479                 problem = PR_1_TOOBIG_DIR;
5480         if (p->is_dir && p->num_blocks + 1 >= p->max_blocks)
5481                 problem = PR_1_TOOBIG_DIR;
5482         if (p->is_reg && p->num_blocks + 1 >= p->max_blocks)
5483                 problem = PR_1_TOOBIG_REG;
5484         if (!p->is_dir && !p->is_reg && blockcnt > 0)
5485                 problem = PR_1_TOOBIG_SYMLINK;
5486
5487         if (blk < fs->super->s_first_data_block ||
5488             blk >= ext2fs_blocks_count(fs->super))
5489                 problem = PR_1_ILLEGAL_BLOCK_NUM;
5490
5491         /*
5492          * If this IND/DIND/TIND block is squatting atop some critical metadata
5493          * (group descriptors, superblock, bitmap, inode table), any write to
5494          * "fix" mapping problems will destroy the metadata.  We'll let pass 1b
5495          * fix that and restart fsck.
5496          */
5497         if (blockcnt < 0 &&
5498             p->ino != EXT2_RESIZE_INO &&
5499             blk < ctx->fs->super->s_blocks_count &&
5500             ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk)) {
5501                 pctx->blk = blk;
5502                 fix_problem_bad(ctx, PR_1_CRITICAL_METADATA_COLLISION, pctx, 2);
5503                 if ((ctx->options & E2F_OPT_NO) == 0)
5504                         ctx->flags |= E2F_FLAG_RESTART_LATER;
5505         }
5506
5507         if (problem) {
5508                 p->num_illegal_blocks++;
5509                 /*
5510                  * A bit of subterfuge here -- we're trying to fix a block
5511                  * mapping, but the IND/DIND/TIND block could have collided
5512                  * with some critical metadata.  So, fix the in-core mapping so
5513                  * iterate won't go insane, but return 0 instead of
5514                  * BLOCK_CHANGED so that it won't write the remapping out to
5515                  * our multiply linked block.
5516                  *
5517                  * Even if we previously determined that an *IND block
5518                  * conflicts with critical metadata, we must still try to
5519                  * iterate the *IND block as if it is an *IND block to find and
5520                  * mark the blocks it points to.  Better to be overly cautious
5521                  * with the used_blocks map so that we don't move the *IND
5522                  * block to a block that's really in use!
5523                  */
5524                 if (p->ino != EXT2_RESIZE_INO &&
5525                     ref_block != 0 &&
5526                     ext2fs_test_block_bitmap2(ctx->block_metadata_map,
5527                                               ref_block)) {
5528                         *block_nr = 0;
5529                         return 0;
5530                 }
5531                 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
5532                         if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
5533                                 p->clear = 1;
5534                                 return BLOCK_ABORT;
5535                         }
5536                         if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
5537                                 p->suppress = 1;
5538                                 set_latch_flags(PR_LATCH_BLOCK,
5539                                                 PRL_SUPPRESS, 0);
5540                         }
5541                 }
5542                 pctx->blk = blk;
5543                 pctx->blkcount = blockcnt;
5544                 if (fix_problem(ctx, problem, pctx)) {
5545                         blk = *block_nr = 0;
5546                         ret_code = BLOCK_CHANGED;
5547                         p->inode_modified = 1;
5548                         /*
5549                          * If the directory block is too big and is beyond the
5550                          * end of the FS, don't bother trying to add it for
5551                          * processing -- the kernel would never have created a
5552                          * directory this large, and we risk an ENOMEM abort.
5553                          * In any case, the toobig handler for extent-based
5554                          * directories also doesn't feed toobig blocks to
5555                          * pass 2.
5556                          */
5557                         if (problem == PR_1_TOOBIG_DIR)
5558                                 return ret_code;
5559                         goto mark_dir;
5560                 } else
5561                         return 0;
5562         }
5563
5564         if (p->ino == EXT2_RESIZE_INO) {
5565                 /*
5566                  * The resize inode has already be sanity checked
5567                  * during pass #0 (the superblock checks).  All we
5568                  * have to do is mark the double indirect block as
5569                  * being in use; all of the other blocks are handled
5570                  * by mark_table_blocks()).
5571                  */
5572                 if (blockcnt == BLOCK_COUNT_DIND)
5573                         mark_block_used(ctx, blk);
5574                 p->num_blocks++;
5575         } else if (!(ctx->fs->cluster_ratio_bits &&
5576                      p->previous_block &&
5577                      (EXT2FS_B2C(ctx->fs, blk) ==
5578                       EXT2FS_B2C(ctx->fs, p->previous_block)) &&
5579                      (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
5580                      ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
5581                 mark_block_used(ctx, blk);
5582                 p->num_blocks++;
5583         } else if (has_unaligned_cluster_map(ctx, p->previous_block,
5584                                              p->last_block, blk, blockcnt)) {
5585                 pctx->blk = blockcnt;
5586                 pctx->blk2 = blk;
5587                 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
5588                 mark_block_used(ctx, blk);
5589                 mark_block_used(ctx, blk);
5590         }
5591         if (blockcnt >= 0)
5592                 p->last_block = blockcnt;
5593         p->previous_block = blk;
5594 mark_dir:
5595         if (p->is_dir && (blockcnt >= 0)) {
5596                 while (++p->last_db_block < blockcnt) {
5597                         pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
5598                                                               p->ino, 0,
5599                                                               p->last_db_block);
5600                         if (pctx->errcode) {
5601                                 pctx->blk = 0;
5602                                 pctx->num = p->last_db_block;
5603                                 goto failed_add_dir_block;
5604                         }
5605                 }
5606                 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
5607                                                       blk, blockcnt);
5608                 if (pctx->errcode) {
5609                         pctx->blk = blk;
5610                         pctx->num = blockcnt;
5611                 failed_add_dir_block:
5612                         fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
5613                         /* Should never get here */
5614                         ctx->flags |= E2F_FLAG_ABORT;
5615                         return BLOCK_ABORT;
5616                 }
5617         }
5618         return ret_code;
5619 }
5620
5621 static int process_bad_block(ext2_filsys fs,
5622                       blk64_t *block_nr,
5623                       e2_blkcnt_t blockcnt,
5624                       blk64_t ref_block EXT2FS_ATTR((unused)),
5625                       int ref_offset EXT2FS_ATTR((unused)),
5626                       void *priv_data)
5627 {
5628         struct process_block_struct *p;
5629         blk64_t         blk = *block_nr;
5630         blk64_t         first_block;
5631         dgrp_t          i;
5632         struct problem_context *pctx;
5633         e2fsck_t        ctx;
5634
5635         if (!blk)
5636                 return 0;
5637
5638         p = (struct process_block_struct *) priv_data;
5639         ctx = p->ctx;
5640         pctx = p->pctx;
5641
5642         pctx->ino = EXT2_BAD_INO;
5643         pctx->blk = blk;
5644         pctx->blkcount = blockcnt;
5645
5646         if ((blk < fs->super->s_first_data_block) ||
5647             (blk >= ext2fs_blocks_count(fs->super))) {
5648                 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
5649                         *block_nr = 0;
5650                         return BLOCK_CHANGED;
5651                 } else
5652                         return 0;
5653         }
5654
5655         if (blockcnt < 0) {
5656                 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
5657                         p->bbcheck = 1;
5658                         if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
5659                                 *block_nr = 0;
5660                                 return BLOCK_CHANGED;
5661                         }
5662                 } else if (is_blocks_used(ctx, blk, 1)) {
5663                         p->bbcheck = 1;
5664                         if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
5665                                         pctx)) {
5666                                 *block_nr = 0;
5667                                 return BLOCK_CHANGED;
5668                         }
5669                         if (e2fsck_should_abort(ctx))
5670                                 return BLOCK_ABORT;
5671                 } else {
5672                         mark_block_used(ctx, blk);
5673                 }
5674                 return 0;
5675         }
5676 #if 0
5677         printf ("DEBUG: Marking %u as bad.\n", blk);
5678 #endif
5679         ctx->fs_badblocks_count++;
5680         /*
5681          * If the block is not used, then mark it as used and return.
5682          * If it is already marked as found, this must mean that
5683          * there's an overlap between the filesystem table blocks
5684          * (bitmaps and inode table) and the bad block list.
5685          */
5686         if (!is_blocks_used(ctx, blk, 1)) {
5687                 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
5688                 return 0;
5689         }
5690         /*
5691          * Try to find the where the filesystem block was used...
5692          */
5693         first_block = fs->super->s_first_data_block;
5694
5695         for (i = 0; i < fs->group_desc_count; i++ ) {
5696                 pctx->group = i;
5697                 pctx->blk = blk;
5698                 if (!ext2fs_bg_has_super(fs, i))
5699                         goto skip_super;
5700                 if (blk == first_block) {
5701                         if (i == 0) {
5702                                 if (fix_problem(ctx,
5703                                                 PR_1_BAD_PRIMARY_SUPERBLOCK,
5704                                                 pctx)) {
5705                                         *block_nr = 0;
5706                                         return BLOCK_CHANGED;
5707                                 }
5708                                 return 0;
5709                         }
5710                         fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
5711                         return 0;
5712                 }
5713                 if ((blk > first_block) &&
5714                     (blk <= first_block + fs->desc_blocks)) {
5715                         if (i == 0) {
5716                                 pctx->blk = *block_nr;
5717                                 if (fix_problem(ctx,
5718                         PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
5719                                         *block_nr = 0;
5720                                         return BLOCK_CHANGED;
5721                                 }
5722                                 return 0;
5723                         }
5724                         fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
5725                         return 0;
5726                 }
5727         skip_super:
5728                 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
5729                         if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
5730                                 ctx->invalid_block_bitmap_flag[i]++;
5731                                 ctx->invalid_bitmaps++;
5732                         }
5733                         return 0;
5734                 }
5735                 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
5736                         if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
5737                                 ctx->invalid_inode_bitmap_flag[i]++;
5738                                 ctx->invalid_bitmaps++;
5739                         }
5740                         return 0;
5741                 }
5742                 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
5743                     (blk < (ext2fs_inode_table_loc(fs, i) +
5744                             fs->inode_blocks_per_group))) {
5745                         /*
5746                          * If there are bad blocks in the inode table,
5747                          * the inode scan code will try to do
5748                          * something reasonable automatically.
5749                          */
5750                         return 0;
5751                 }
5752                 first_block += fs->super->s_blocks_per_group;
5753         }
5754         /*
5755          * If we've gotten to this point, then the only
5756          * possibility is that the bad block inode meta data
5757          * is using a bad block.
5758          */
5759         if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
5760             (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
5761             (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
5762                 p->bbcheck = 1;
5763                 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
5764                         *block_nr = 0;
5765                         return BLOCK_CHANGED;
5766                 }
5767                 if (e2fsck_should_abort(ctx))
5768                         return BLOCK_ABORT;
5769                 return 0;
5770         }
5771
5772         pctx->group = -1;
5773
5774         /* Warn user that the block wasn't claimed */
5775         fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
5776
5777         return 0;
5778 }
5779
5780 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
5781                             const char *name, int num, blk64_t *new_block)
5782 {
5783         ext2_filsys fs = ctx->fs;
5784         dgrp_t          last_grp;
5785         blk64_t         old_block = *new_block;
5786         blk64_t         last_block;
5787         dgrp_t          flexbg;
5788         unsigned        flexbg_size;
5789         int             i, is_flexbg;
5790         char            *buf;
5791         struct problem_context  pctx;
5792
5793         clear_problem_context(&pctx);
5794
5795         pctx.group = group;
5796         pctx.blk = old_block;
5797         pctx.str = name;
5798
5799         /*
5800          * For flex_bg filesystems, first try to allocate the metadata
5801          * within the flex_bg, and if that fails then try finding the
5802          * space anywhere in the filesystem.
5803          */
5804         is_flexbg = ext2fs_has_feature_flex_bg(fs->super);
5805         if (is_flexbg) {
5806                 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
5807                 flexbg = group / flexbg_size;
5808                 first_block = ext2fs_group_first_block2(fs,
5809                                                         flexbg_size * flexbg);
5810                 last_grp = group | (flexbg_size - 1);
5811                 if (last_grp >= fs->group_desc_count)
5812                         last_grp = fs->group_desc_count - 1;
5813                 last_block = ext2fs_group_last_block2(fs, last_grp);
5814         } else
5815                 last_block = ext2fs_group_last_block2(fs, group);
5816         pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
5817                                                num, ctx->block_found_map,
5818                                                new_block);
5819         if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
5820                 pctx.errcode = ext2fs_get_free_blocks2(fs,
5821                                 fs->super->s_first_data_block,
5822                                 ext2fs_blocks_count(fs->super),
5823                                 num, ctx->block_found_map, new_block);
5824         if (pctx.errcode) {
5825                 pctx.num = num;
5826                 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
5827                 ext2fs_unmark_valid(fs);
5828                 ctx->flags |= E2F_FLAG_ABORT;
5829                 return;
5830         }
5831         pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
5832         if (pctx.errcode) {
5833                 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
5834                 ext2fs_unmark_valid(fs);
5835                 ctx->flags |= E2F_FLAG_ABORT;
5836                 return;
5837         }
5838         ext2fs_mark_super_dirty(fs);
5839         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
5840         pctx.blk2 = *new_block;
5841         fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
5842                           PR_1_RELOC_TO), &pctx);
5843         pctx.blk2 = 0;
5844         for (i = 0; i < num; i++) {
5845                 pctx.blk = i;
5846                 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
5847                 if (old_block) {
5848                         pctx.errcode = io_channel_read_blk64(fs->io,
5849                                    old_block + i, 1, buf);
5850                         if (pctx.errcode)
5851                                 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
5852                         pctx.blk = (*new_block) + i;
5853                         pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
5854                                                               1, buf);
5855                 } else {
5856                         pctx.blk = (*new_block) + i;
5857                         pctx.errcode = ext2fs_zero_blocks2(fs, pctx.blk, 1,
5858                                                            NULL, NULL);
5859                 }
5860
5861                 if (pctx.errcode)
5862                         fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
5863         }
5864         ext2fs_free_mem(&buf);
5865 }
5866
5867 /*
5868  * This routine gets called at the end of pass 1 if bad blocks are
5869  * detected in the superblock, group descriptors, inode_bitmaps, or
5870  * block bitmaps.  At this point, all of the blocks have been mapped
5871  * out, so we can try to allocate new block(s) to replace the bad
5872  * blocks.
5873  */
5874 static void handle_fs_bad_blocks(e2fsck_t ctx)
5875 {
5876         ext2_filsys fs = ctx->fs;
5877         dgrp_t          i;
5878         blk64_t         first_block;
5879         blk64_t         new_blk;
5880
5881         for (i = 0; i < fs->group_desc_count; i++) {
5882                 first_block = ext2fs_group_first_block2(fs, i);
5883
5884                 if (ctx->invalid_block_bitmap_flag[i]) {
5885                         new_blk = ext2fs_block_bitmap_loc(fs, i);
5886                         new_table_block(ctx, first_block, i, _("block bitmap"),
5887                                         1, &new_blk);
5888                         ext2fs_block_bitmap_loc_set(fs, i, new_blk);
5889                 }
5890                 if (ctx->invalid_inode_bitmap_flag[i]) {
5891                         new_blk = ext2fs_inode_bitmap_loc(fs, i);
5892                         new_table_block(ctx, first_block, i, _("inode bitmap"),
5893                                         1, &new_blk);
5894                         ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
5895                 }
5896                 if (ctx->invalid_inode_table_flag[i]) {
5897                         new_blk = ext2fs_inode_table_loc(fs, i);
5898                         new_table_block(ctx, first_block, i, _("inode table"),
5899                                         fs->inode_blocks_per_group,
5900                                         &new_blk);
5901                         ext2fs_inode_table_loc_set(fs, i, new_blk);
5902                         ctx->flags |= E2F_FLAG_RESTART;
5903                 }
5904         }
5905         ctx->invalid_bitmaps = 0;
5906 }
5907
5908 /*
5909  * This routine marks all blocks which are used by the superblock,
5910  * group descriptors, inode bitmaps, and block bitmaps.
5911  */
5912 static void mark_table_blocks(e2fsck_t ctx)
5913 {
5914         ext2_filsys fs = ctx->fs;
5915         blk64_t b;
5916         dgrp_t  i;
5917         unsigned int    j;
5918         struct problem_context pctx;
5919
5920         clear_problem_context(&pctx);
5921
5922         for (i = 0; i < fs->group_desc_count; i++) {
5923                 pctx.group = i;
5924
5925                 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
5926                 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_metadata_map);
5927
5928                 /*
5929                  * Mark the blocks used for the inode table
5930                  */
5931                 if (ext2fs_inode_table_loc(fs, i)) {
5932                         for (j = 0, b = ext2fs_inode_table_loc(fs, i);
5933                              j < fs->inode_blocks_per_group;
5934                              j++, b++) {
5935                                 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5936                                                              b)) {
5937                                         pctx.blk = b;
5938                                         if (!ctx->invalid_inode_table_flag[i] &&
5939                                             fix_problem(ctx,
5940                                                 PR_1_ITABLE_CONFLICT, &pctx)) {
5941                                                 ctx->invalid_inode_table_flag[i]++;
5942                                                 ctx->invalid_bitmaps++;
5943                                         }
5944                                 } else {
5945                                     ext2fs_mark_block_bitmap2(
5946                                                 ctx->block_found_map, b);
5947                                     ext2fs_mark_block_bitmap2(
5948                                                 ctx->block_metadata_map, b);
5949                                 }
5950                         }
5951                 }
5952
5953                 /*
5954                  * Mark block used for the block bitmap
5955                  */
5956                 if (ext2fs_block_bitmap_loc(fs, i)) {
5957                         if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5958                                      ext2fs_block_bitmap_loc(fs, i))) {
5959                                 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
5960                                 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
5961                                         ctx->invalid_block_bitmap_flag[i]++;
5962                                         ctx->invalid_bitmaps++;
5963                                 }
5964                         } else {
5965                             ext2fs_mark_block_bitmap2(ctx->block_found_map,
5966                                      ext2fs_block_bitmap_loc(fs, i));
5967                             ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
5968                                      ext2fs_block_bitmap_loc(fs, i));
5969                         }
5970                 }
5971                 /*
5972                  * Mark block used for the inode bitmap
5973                  */
5974                 if (ext2fs_inode_bitmap_loc(fs, i)) {
5975                         if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5976                                      ext2fs_inode_bitmap_loc(fs, i))) {
5977                                 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
5978                                 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
5979                                         ctx->invalid_inode_bitmap_flag[i]++;
5980                                         ctx->invalid_bitmaps++;
5981                                 }
5982                         } else {
5983                             ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
5984                                      ext2fs_inode_bitmap_loc(fs, i));
5985                             ext2fs_mark_block_bitmap2(ctx->block_found_map,
5986                                      ext2fs_inode_bitmap_loc(fs, i));
5987                         }
5988                 }
5989         }
5990 }
5991
5992 /*
5993  * These subroutines short circuits ext2fs_get_blocks and
5994  * ext2fs_check_directory; we use them since we already have the inode
5995  * structure, so there's no point in letting the ext2fs library read
5996  * the inode again.
5997  */
5998 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
5999                                   blk_t *blocks)
6000 {
6001         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6002         int     i;
6003
6004         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
6005                 return EXT2_ET_CALLBACK_NOTHANDLED;
6006
6007         for (i=0; i < EXT2_N_BLOCKS; i++)
6008                 blocks[i] = ctx->stashed_inode->i_block[i];
6009         return 0;
6010 }
6011
6012 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
6013                                   struct ext2_inode *inode)
6014 {
6015         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6016
6017         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
6018                 return EXT2_ET_CALLBACK_NOTHANDLED;
6019         *inode = *ctx->stashed_inode;
6020         return 0;
6021 }
6022
6023 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
6024                             struct ext2_inode *inode)
6025 {
6026         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6027
6028         if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
6029                 (inode != ctx->stashed_inode))
6030                 *ctx->stashed_inode = *inode;
6031         return EXT2_ET_CALLBACK_NOTHANDLED;
6032 }
6033
6034 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
6035 {
6036         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6037
6038         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
6039                 return EXT2_ET_CALLBACK_NOTHANDLED;
6040
6041         if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
6042                 return EXT2_ET_NO_DIRECTORY;
6043         return 0;
6044 }
6045
6046 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
6047                                         blk64_t *ret)
6048 {
6049         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6050         errcode_t       retval;
6051         blk64_t         new_block;
6052
6053         if (ctx->block_found_map) {
6054                 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
6055                                            &new_block);
6056                 if (retval)
6057                         return retval;
6058                 if (fs->block_map) {
6059                         ext2fs_mark_block_bitmap2(fs->block_map, new_block);
6060                         ext2fs_mark_bb_dirty(fs);
6061                 }
6062         } else {
6063                 if (!fs->block_map) {
6064                         retval = ext2fs_read_block_bitmap(fs);
6065                         if (retval)
6066                                 return retval;
6067                 }
6068
6069                 retval = ext2fs_new_block2(fs, goal, fs->block_map, &new_block);
6070                 if (retval)
6071                         return retval;
6072         }
6073
6074         *ret = new_block;
6075         return (0);
6076 }
6077
6078 static errcode_t e2fsck_new_range(ext2_filsys fs, int flags, blk64_t goal,
6079                                   blk64_t len, blk64_t *pblk, blk64_t *plen)
6080 {
6081         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6082         errcode_t       retval;
6083
6084         if (ctx->block_found_map)
6085                 return ext2fs_new_range(fs, flags, goal, len,
6086                                         ctx->block_found_map, pblk, plen);
6087
6088         if (!fs->block_map) {
6089                 retval = ext2fs_read_block_bitmap(fs);
6090                 if (retval)
6091                         return retval;
6092         }
6093
6094         return ext2fs_new_range(fs, flags, goal, len, fs->block_map,
6095                                 pblk, plen);
6096 }
6097
6098 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
6099 {
6100         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6101
6102         /* Never free a critical metadata block */
6103         if (ctx->block_found_map &&
6104             ctx->block_metadata_map &&
6105             inuse < 0 &&
6106             ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk))
6107                 return;
6108
6109         if (ctx->block_found_map) {
6110                 if (inuse > 0)
6111                         ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
6112                 else
6113                         ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
6114         }
6115 }
6116
6117 static void e2fsck_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
6118                                            blk_t num, int inuse)
6119 {
6120         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6121
6122         /* Never free a critical metadata block */
6123         if (ctx->block_found_map &&
6124             ctx->block_metadata_map &&
6125             inuse < 0 &&
6126             ext2fs_test_block_bitmap_range2(ctx->block_metadata_map, blk, num))
6127                 return;
6128
6129         if (ctx->block_found_map) {
6130                 if (inuse > 0)
6131                         ext2fs_mark_block_bitmap_range2(ctx->block_found_map,
6132                                                         blk, num);
6133                 else
6134                         ext2fs_unmark_block_bitmap_range2(ctx->block_found_map,
6135                                                         blk, num);
6136         }
6137 }
6138
6139 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
6140 {
6141         ext2_filsys fs = ctx->fs;
6142
6143         if (use_shortcuts) {
6144                 fs->get_blocks = pass1_get_blocks;
6145                 fs->check_directory = pass1_check_directory;
6146                 fs->read_inode = pass1_read_inode;
6147                 fs->write_inode = pass1_write_inode;
6148                 ctx->stashed_ino = 0;
6149         } else {
6150                 fs->get_blocks = 0;
6151                 fs->check_directory = 0;
6152                 fs->read_inode = 0;
6153                 fs->write_inode = 0;
6154         }
6155 }
6156
6157 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
6158 {
6159         ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
6160         ext2fs_set_block_alloc_stats_callback(ctx->fs,
6161                                                 e2fsck_block_alloc_stats, 0);
6162         ext2fs_set_new_range_callback(ctx->fs, e2fsck_new_range, NULL);
6163         ext2fs_set_block_alloc_stats_range_callback(ctx->fs,
6164                                         e2fsck_block_alloc_stats_range, NULL);
6165 }