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