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