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