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