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