Whamcloud - gitweb
e2fsck: update i_blocks accounting for ea_inode feature
[tools/e2fsprogs.git] / e2fsck / pass1.c
1 /*
2  * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
3  *
4  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  *
11  * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12  * and applies the following tests to each inode:
13  *
14  *      - The mode field of the inode must be legal.
15  *      - The size and block count fields of the inode are correct.
16  *      - A data block must not be used by another inode
17  *
18  * Pass 1 also gathers the collects the following information:
19  *
20  *      - A bitmap of which inodes are in use.          (inode_used_map)
21  *      - A bitmap of which inodes are directories.     (inode_dir_map)
22  *      - A bitmap of which inodes are regular files.   (inode_reg_map)
23  *      - A bitmap of which inodes have bad fields.     (inode_bad_map)
24  *      - A bitmap of which inodes are in bad blocks.   (inode_bb_map)
25  *      - A bitmap of which inodes are imagic inodes.   (inode_imagic_map)
26  *      - A bitmap of which blocks are in use.          (block_found_map)
27  *      - A bitmap of which blocks are in use by two inodes     (block_dup_map)
28  *      - The data blocks of the directory inodes.      (dir_map)
29  *      - A bitmap of EA inodes.                        (inode_ea_map)
30  *
31  * Pass 1 is designed to stash away enough information so that the
32  * other passes should not need to read in the inode information
33  * during the normal course of a filesystem check.  (Althogh if an
34  * inconsistency is detected, other passes may need to read in an
35  * inode to fix it.)
36  *
37  * Note that pass 1B will be invoked if there are any duplicate blocks
38  * found.
39  */
40
41 #define _GNU_SOURCE 1 /* get strnlen() */
42 #include "config.h"
43 #include <string.h>
44 #include <time.h>
45 #ifdef HAVE_ERRNO_H
46 #include <errno.h>
47 #endif
48
49 #include "e2fsck.h"
50 #include <ext2fs/ext2_ext_attr.h>
51
52 #include "problem.h"
53
54 #ifdef NO_INLINE_FUNCS
55 #define _INLINE_
56 #else
57 #define _INLINE_ inline
58 #endif
59
60 #undef DEBUG
61
62 static int process_block(ext2_filsys fs, blk64_t        *blocknr,
63                          e2_blkcnt_t blockcnt, blk64_t ref_blk,
64                          int ref_offset, void *priv_data);
65 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
66                              e2_blkcnt_t blockcnt, blk64_t ref_blk,
67                              int ref_offset, void *priv_data);
68 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
69                          char *block_buf, blk64_t ea_ibody_quota_blocks);
70 static void mark_table_blocks(e2fsck_t ctx);
71 static void alloc_bb_map(e2fsck_t ctx);
72 static void alloc_imagic_map(e2fsck_t ctx);
73 static void mark_inode_bad(e2fsck_t ctx, ino_t ino);
74 static void add_encrypted_dir(e2fsck_t ctx, ino_t ino);
75 static void handle_fs_bad_blocks(e2fsck_t ctx);
76 static void process_inodes(e2fsck_t ctx, char *block_buf);
77 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
78 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
79                                   dgrp_t group, void * priv_data);
80 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
81                                     char *block_buf, int adjust_sign);
82 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
83
84 struct process_block_struct {
85         ext2_ino_t      ino;
86         unsigned        is_dir:1, is_reg:1, clear:1, suppress:1,
87                                 fragmented:1, compressed:1, bbcheck:1,
88                                 inode_modified:1;
89         blk64_t         num_blocks;
90         blk64_t         max_blocks;
91         blk64_t         last_block;
92         e2_blkcnt_t     last_init_lblock;
93         e2_blkcnt_t     last_db_block;
94         int             num_illegal_blocks;
95         blk64_t         previous_block;
96         struct ext2_inode *inode;
97         struct problem_context *pctx;
98         ext2fs_block_bitmap fs_meta_blocks;
99         e2fsck_t        ctx;
100         region_t        region;
101         struct extent_tree_info eti;
102 };
103
104 struct process_inode_block {
105         ext2_ino_t ino;
106         blk64_t ea_ibody_quota_blocks;
107         struct ext2_inode_large inode;
108 };
109
110 struct scan_callback_struct {
111         e2fsck_t        ctx;
112         char            *block_buf;
113 };
114
115 /*
116  * For the inodes to process list.
117  */
118 static struct process_inode_block *inodes_to_process;
119 static int process_inode_count;
120
121 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
122                             EXT2_MIN_BLOCK_LOG_SIZE + 1];
123
124 /*
125  * Free all memory allocated by pass1 in preparation for restarting
126  * things.
127  */
128 static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
129 {
130         ext2fs_free_mem(&inodes_to_process);
131         inodes_to_process = 0;
132 }
133
134 /*
135  * Check to make sure a device inode is real.  Returns 1 if the device
136  * checks out, 0 if not.
137  *
138  * Note: this routine is now also used to check FIFO's and Sockets,
139  * since they have the same requirement; the i_block fields should be
140  * zero.
141  */
142 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
143                                     struct ext2_inode *inode)
144 {
145         int     i;
146
147         /*
148          * If the index flag is set, then this is a bogus
149          * device/fifo/socket
150          */
151         if (inode->i_flags & EXT2_INDEX_FL)
152                 return 0;
153
154         /*
155          * We should be able to do the test below all the time, but
156          * because the kernel doesn't forcibly clear the device
157          * inode's additional i_block fields, there are some rare
158          * occasions when a legitimate device inode will have non-zero
159          * additional i_block fields.  So for now, we only complain
160          * when the immutable flag is set, which should never happen
161          * for devices.  (And that's when the problem is caused, since
162          * you can't set or clear immutable flags for devices.)  Once
163          * the kernel has been fixed we can change this...
164          */
165         if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
166                 for (i=4; i < EXT2_N_BLOCKS; i++)
167                         if (inode->i_block[i])
168                                 return 0;
169         }
170         return 1;
171 }
172
173 /*
174  * Check to make sure a symlink inode is real.  Returns 1 if the symlink
175  * checks out, 0 if not.
176  */
177 int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
178                                struct ext2_inode *inode, char *buf)
179 {
180         unsigned int len;
181         int i;
182         blk64_t blocks;
183         ext2_extent_handle_t    handle;
184         struct ext2_extent_info info;
185         struct ext2fs_extent    extent;
186
187         if ((inode->i_size_high || inode->i_size == 0) ||
188             (inode->i_flags & EXT2_INDEX_FL))
189                 return 0;
190
191         if (inode->i_flags & EXT4_EXTENTS_FL) {
192                 if (inode->i_flags & EXT4_INLINE_DATA_FL)
193                         return 0;
194                 if (inode->i_size > fs->blocksize)
195                         return 0;
196                 if (ext2fs_extent_open2(fs, ino, inode, &handle))
197                         return 0;
198                 i = 0;
199                 if (ext2fs_extent_get_info(handle, &info) ||
200                     (info.num_entries != 1) ||
201                     (info.max_depth != 0))
202                         goto exit_extent;
203                 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) ||
204                     (extent.e_lblk != 0) ||
205                     (extent.e_len != 1) ||
206                     (extent.e_pblk < fs->super->s_first_data_block) ||
207                     (extent.e_pblk >= ext2fs_blocks_count(fs->super)))
208                         goto exit_extent;
209                 i = 1;
210         exit_extent:
211                 ext2fs_extent_free(handle);
212                 return i;
213         }
214
215         if (inode->i_flags & EXT4_INLINE_DATA_FL) {
216                 size_t inline_size;
217
218                 if (ext2fs_inline_data_size(fs, ino, &inline_size))
219                         return 0;
220                 if (inode->i_size != inline_size)
221                         return 0;
222
223                 return 1;
224         }
225
226         blocks = ext2fs_inode_data_blocks2(fs, inode);
227         if (blocks) {
228                 if (inode->i_flags & EXT4_INLINE_DATA_FL)
229                         return 0;
230                 if ((inode->i_size >= fs->blocksize) ||
231                     (blocks != fs->blocksize >> 9) ||
232                     (inode->i_block[0] < fs->super->s_first_data_block) ||
233                     (inode->i_block[0] >= ext2fs_blocks_count(fs->super)))
234                         return 0;
235
236                 for (i = 1; i < EXT2_N_BLOCKS; i++)
237                         if (inode->i_block[i])
238                                 return 0;
239
240                 if (io_channel_read_blk64(fs->io, inode->i_block[0], 1, buf))
241                         return 0;
242
243                 if (inode->i_flags & EXT4_ENCRYPT_FL) {
244                         len = ext2fs_le32_to_cpu(*((__u32 *)buf)) + 4;
245                 } else {
246                         len = strnlen(buf, fs->blocksize);
247                 }
248                 if (len == fs->blocksize)
249                         return 0;
250         } else if (inode->i_flags & EXT4_INLINE_DATA_FL) {
251                 char *inline_buf = NULL;
252                 size_t inline_sz = 0;
253
254                 if (ext2fs_inline_data_size(fs, ino, &inline_sz))
255                         return 0;
256                 if (inode->i_size != inline_sz)
257                         return 0;
258                 if (ext2fs_get_mem(inline_sz + 1, &inline_buf))
259                         return 0;
260                 i = 0;
261                 if (ext2fs_inline_data_get(fs, ino, inode, inline_buf, NULL))
262                         goto exit_inline;
263                 inline_buf[inline_sz] = 0;
264                 len = strnlen(inline_buf, inline_sz);
265                 if (len != inline_sz)
266                         goto exit_inline;
267                 i = 1;
268 exit_inline:
269                 ext2fs_free_mem(&inline_buf);
270                 return i;
271         } else {
272                 if (inode->i_size >= sizeof(inode->i_block))
273                         return 0;
274
275                 len = strnlen((char *)inode->i_block, sizeof(inode->i_block));
276                 if (len == sizeof(inode->i_block))
277                         return 0;
278         }
279         if (len != inode->i_size)
280                 if ((inode->i_flags & EXT4_ENCRYPT_FL) == 0)
281                         return 0;
282         return 1;
283 }
284
285 /*
286  * If the extents or inlinedata flags are set on the inode, offer to clear 'em.
287  */
288 #define BAD_SPECIAL_FLAGS (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)
289 static void check_extents_inlinedata(e2fsck_t ctx,
290                                      struct problem_context *pctx)
291 {
292         if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
293                 return;
294
295         if (!fix_problem(ctx, PR_1_SPECIAL_EXTENTS_IDATA, pctx))
296                 return;
297
298         pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
299         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
300 }
301 #undef BAD_SPECIAL_FLAGS
302
303 /*
304  * If the immutable (or append-only) flag is set on the inode, offer
305  * to clear it.
306  */
307 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
308 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
309 {
310         if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
311                 return;
312
313         if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
314                 return;
315
316         pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
317         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
318 }
319
320 /*
321  * If device, fifo or socket, check size is zero -- if not offer to
322  * clear it
323  */
324 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
325 {
326         struct ext2_inode *inode = pctx->inode;
327
328         if (EXT2_I_SIZE(inode) == 0)
329                 return;
330
331         if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
332                 return;
333
334         ext2fs_inode_size_set(ctx->fs, inode, 0);
335         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
336 }
337
338 static void mark_inode_ea_map(e2fsck_t ctx, struct problem_context *pctx,
339                               ext2_ino_t ino)
340 {
341         if (!ctx->inode_ea_map) {
342                 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
343                                          _("EA inode map"),
344                                          &ctx->inode_ea_map);
345                 if (pctx->errcode) {
346                         fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
347                                     pctx);
348                         exit(1);
349                 }
350         }
351
352         ext2fs_mark_inode_bitmap2(ctx->inode_ea_map, ino);
353 }
354
355 /*
356  * For a given size, calculate how many blocks would be charged towards quota.
357  */
358 static blk64_t size_to_quota_blocks(ext2_filsys fs, size_t size)
359 {
360         blk64_t clusters;
361
362         clusters = DIV_ROUND_UP(size, fs->blocksize << fs->cluster_ratio_bits);
363         return EXT2FS_C2B(fs, clusters);
364 }
365
366 /*
367  * Check validity of EA inode. Return 0 if EA inode is valid, otherwise return
368  * the problem code.
369  */
370 static problem_t check_large_ea_inode(e2fsck_t ctx,
371                                       struct ext2_ext_attr_entry *entry,
372                                       struct problem_context *pctx,
373                                       blk64_t *quota_blocks)
374 {
375         struct ext2_inode inode;
376         __u32 hash;
377         errcode_t retval;
378
379         /* Check if inode is within valid range */
380         if ((entry->e_value_inum < EXT2_FIRST_INODE(ctx->fs->super)) ||
381             (entry->e_value_inum > ctx->fs->super->s_inodes_count)) {
382                 pctx->num = entry->e_value_inum;
383                 return PR_1_ATTR_VALUE_EA_INODE;
384         }
385
386         e2fsck_read_inode(ctx, entry->e_value_inum, &inode, "pass1");
387
388         retval = ext2fs_ext_attr_hash_entry2(ctx->fs, entry, NULL, &hash);
389         if (retval) {
390                 com_err("check_large_ea_inode", retval,
391                         _("while hashing entry with e_value_inum = %u"),
392                         entry->e_value_inum);
393                 fatal_error(ctx, 0);
394         }
395
396         if (hash == entry->e_hash) {
397                 *quota_blocks = size_to_quota_blocks(ctx->fs,
398                                                      entry->e_value_size);
399         } else {
400                 /* This might be an old Lustre-style ea_inode reference. */
401                 if (inode.i_mtime == pctx->ino &&
402                     inode.i_generation == pctx->inode->i_generation) {
403                         *quota_blocks = 0;
404                 } else {
405                         /* If target inode is also missing EA_INODE flag,
406                          * this is likely to be a bad reference.
407                          */
408                         if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
409                                 pctx->num = entry->e_value_inum;
410                                 return PR_1_ATTR_VALUE_EA_INODE;
411                         } else {
412                                 pctx->num = entry->e_hash;
413                                 return PR_1_ATTR_HASH;
414                         }
415                 }
416         }
417
418         if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
419                 pctx->num = entry->e_value_inum;
420                 if (fix_problem(ctx, PR_1_ATTR_SET_EA_INODE_FL, pctx)) {
421                         inode.i_flags |= EXT4_EA_INODE_FL;
422                         ext2fs_write_inode(ctx->fs, entry->e_value_inum,
423                                            &inode);
424                 } else {
425                         return PR_1_ATTR_NO_EA_INODE_FL;
426                 }
427         }
428         return 0;
429 }
430
431 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx,
432                               blk64_t *ea_ibody_quota_blocks)
433 {
434         struct ext2_super_block *sb = ctx->fs->super;
435         struct ext2_inode_large *inode;
436         struct ext2_ext_attr_entry *entry;
437         char *start, *header;
438         unsigned int storage_size, remain;
439         problem_t problem = 0;
440         region_t region = 0;
441         blk64_t quota_blocks = 0;
442
443         *ea_ibody_quota_blocks = 0;
444
445         inode = (struct ext2_inode_large *) pctx->inode;
446         storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
447                 inode->i_extra_isize;
448         header = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
449                  inode->i_extra_isize;
450         start = header + sizeof(__u32);
451         entry = (struct ext2_ext_attr_entry *) start;
452
453         /* scan all entry's headers first */
454
455         /* take finish entry 0UL into account */
456         remain = storage_size - sizeof(__u32);
457
458         region = region_create(0, storage_size);
459         if (!region) {
460                 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
461                 problem = 0;
462                 ctx->flags |= E2F_FLAG_ABORT;
463                 return;
464         }
465         if (region_allocate(region, 0, sizeof(__u32))) {
466                 problem = PR_1_INODE_EA_ALLOC_COLLISION;
467                 goto fix;
468         }
469
470         while (remain >= sizeof(struct ext2_ext_attr_entry) &&
471                !EXT2_EXT_IS_LAST_ENTRY(entry)) {
472                 __u32 hash;
473
474                 if (region_allocate(region, (char *)entry - (char *)header,
475                                     EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
476                         problem = PR_1_INODE_EA_ALLOC_COLLISION;
477                         goto fix;
478                 }
479
480                 /* header eats this space */
481                 remain -= sizeof(struct ext2_ext_attr_entry);
482
483                 /* is attribute name valid? */
484                 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
485                         pctx->num = entry->e_name_len;
486                         problem = PR_1_ATTR_NAME_LEN;
487                         goto fix;
488                 }
489
490                 /* attribute len eats this space */
491                 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
492
493                 if (entry->e_value_inum == 0) {
494                         /* check value size */
495                         if (entry->e_value_size > remain) {
496                                 pctx->num = entry->e_value_size;
497                                 problem = PR_1_ATTR_VALUE_SIZE;
498                                 goto fix;
499                         }
500
501                         if (entry->e_value_size &&
502                             region_allocate(region,
503                                             sizeof(__u32) + entry->e_value_offs,
504                                             EXT2_EXT_ATTR_SIZE(
505                                                 entry->e_value_size))) {
506                                 problem = PR_1_INODE_EA_ALLOC_COLLISION;
507                                 goto fix;
508                         }
509
510                         hash = ext2fs_ext_attr_hash_entry(entry,
511                                                           start + entry->e_value_offs);
512
513                         /* e_hash may be 0 in older inode's ea */
514                         if (entry->e_hash != 0 && entry->e_hash != hash) {
515                                 pctx->num = entry->e_hash;
516                                 problem = PR_1_ATTR_HASH;
517                                 goto fix;
518                         }
519                 } else {
520                         blk64_t entry_quota_blocks;
521
522                         problem = check_large_ea_inode(ctx, entry, pctx,
523                                                        &entry_quota_blocks);
524                         if (problem != 0)
525                                 goto fix;
526
527                         mark_inode_ea_map(ctx, pctx, entry->e_value_inum);
528                         quota_blocks += entry_quota_blocks;
529                 }
530
531                 /* If EA value is stored in external inode then it does not
532                  * consume space here */
533                 if (entry->e_value_inum == 0)
534                         remain -= entry->e_value_size;
535
536                 entry = EXT2_EXT_ATTR_NEXT(entry);
537         }
538
539         if (region_allocate(region, (char *)entry - (char *)header,
540                             sizeof(__u32))) {
541                 problem = PR_1_INODE_EA_ALLOC_COLLISION;
542                 goto fix;
543         }
544 fix:
545         if (region)
546                 region_free(region);
547         /*
548          * it seems like a corruption. it's very unlikely we could repair
549          * EA(s) in automatic fashion -bzzz
550          */
551         if (problem == 0 || !fix_problem(ctx, problem, pctx)) {
552                 *ea_ibody_quota_blocks = quota_blocks;
553                 return;
554         }
555
556         /* simply remove all possible EA(s) */
557         *((__u32 *)header) = 0UL;
558         e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
559                                 EXT2_INODE_SIZE(sb), "pass1");
560 }
561
562 static int check_inode_extra_negative_epoch(__u32 xtime, __u32 extra) {
563         return (xtime & (1U << 31)) != 0 &&
564                 (extra & EXT4_EPOCH_MASK) == EXT4_EPOCH_MASK;
565 }
566
567 #define CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, xtime) \
568         check_inode_extra_negative_epoch(inode->i_##xtime, \
569                                          inode->i_##xtime##_extra)
570
571 /* When today's date is earlier than 2242, we assume that atimes,
572  * ctimes, crtimes, and mtimes with years in the range 2310..2378 are
573  * actually pre-1970 dates mis-encoded.
574  */
575 #define EXT4_EXTRA_NEGATIVE_DATE_CUTOFF 2 * (1LL << 32)
576
577 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx,
578                                     blk64_t *ea_ibody_quota_blocks)
579 {
580         struct ext2_super_block *sb = ctx->fs->super;
581         struct ext2_inode_large *inode;
582         __u32 *eamagic;
583         int min, max;
584
585         *ea_ibody_quota_blocks = 0;
586
587         inode = (struct ext2_inode_large *) pctx->inode;
588         if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
589                 /* this isn't large inode. so, nothing to check */
590                 return;
591         }
592
593 #if 0
594         printf("inode #%u, i_extra_size %d\n", pctx->ino,
595                         inode->i_extra_isize);
596 #endif
597         /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
598         min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
599         max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
600         /*
601          * For now we will allow i_extra_isize to be 0, but really
602          * implementations should never allow i_extra_isize to be 0
603          */
604         if (inode->i_extra_isize &&
605             (inode->i_extra_isize < min || inode->i_extra_isize > max ||
606              inode->i_extra_isize & 3)) {
607                 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
608                         return;
609                 if (inode->i_extra_isize < min || inode->i_extra_isize > max)
610                         inode->i_extra_isize = sb->s_want_extra_isize;
611                 else
612                         inode->i_extra_isize = (inode->i_extra_isize + 3) & ~3;
613                 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
614                                         EXT2_INODE_SIZE(sb), "pass1");
615         }
616
617         /* check if there is no place for an EA header */
618         if (inode->i_extra_isize >= max - sizeof(__u32))
619                 return;
620
621         eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
622                         inode->i_extra_isize);
623         if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
624                 /* it seems inode has an extended attribute(s) in body */
625                 check_ea_in_inode(ctx, pctx, ea_ibody_quota_blocks);
626         }
627
628         /*
629          * If the inode's extended atime (ctime, crtime, mtime) is stored in
630          * the old, invalid format, repair it.
631          */
632         if (((sizeof(time_t) <= 4) ||
633              (((sizeof(time_t) > 4) &&
634                ctx->now < EXT4_EXTRA_NEGATIVE_DATE_CUTOFF))) &&
635             (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime) ||
636              CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime) ||
637              CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime) ||
638              CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))) {
639
640                 if (!fix_problem(ctx, PR_1_EA_TIME_OUT_OF_RANGE, pctx))
641                         return;
642
643                 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime))
644                         inode->i_atime_extra &= ~EXT4_EPOCH_MASK;
645                 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime))
646                         inode->i_ctime_extra &= ~EXT4_EPOCH_MASK;
647                 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime))
648                         inode->i_crtime_extra &= ~EXT4_EPOCH_MASK;
649                 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))
650                         inode->i_mtime_extra &= ~EXT4_EPOCH_MASK;
651                 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
652                                         EXT2_INODE_SIZE(sb), "pass1");
653         }
654
655 }
656
657 /*
658  * Check to see if the inode might really be a directory, despite i_mode
659  *
660  * This is a lot of complexity for something for which I'm not really
661  * convinced happens frequently in the wild.  If for any reason this
662  * causes any problems, take this code out.
663  * [tytso:20070331.0827EDT]
664  */
665 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
666                                 char *buf)
667 {
668         struct ext2_inode *inode = pctx->inode;
669         struct ext2_dir_entry   *dirent;
670         errcode_t               retval;
671         blk64_t                 blk;
672         unsigned int            i, rec_len, not_device = 0;
673         int                     extent_fs;
674         int                     inlinedata_fs;
675
676         /*
677          * If the mode looks OK, we believe it.  If the first block in
678          * the i_block array is 0, this cannot be a directory. If the
679          * inode is extent-mapped, it is still the case that the latter
680          * cannot be 0 - the magic number in the extent header would make
681          * it nonzero.
682          */
683         if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
684             LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
685                 return;
686
687         /* 
688          * Check the block numbers in the i_block array for validity:
689          * zero blocks are skipped (but the first one cannot be zero -
690          * see above), other blocks are checked against the first and
691          * max data blocks (from the the superblock) and against the
692          * block bitmap. Any invalid block found means this cannot be
693          * a directory.
694          * 
695          * If there are non-zero blocks past the fourth entry, then
696          * this cannot be a device file: we remember that for the next
697          * check.
698          *
699          * For extent mapped files, we don't do any sanity checking:
700          * just try to get the phys block of logical block 0 and run
701          * with it.
702          *
703          * For inline data files, we just try to get the size of inline
704          * data.  If it's true, we will treat it as a directory.
705          */
706
707         extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
708         inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
709         if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL)) {
710                 size_t size;
711                 __u32 dotdot;
712                 unsigned int rec_len2;
713                 struct ext2_dir_entry de;
714
715                 if (ext2fs_inline_data_size(ctx->fs, pctx->ino, &size))
716                         return;
717                 /*
718                  * If the size isn't a multiple of 4, it's probably not a
719                  * directory??
720                  */
721                 if (size & 3)
722                         return;
723                 /*
724                  * If the first 10 bytes don't look like a directory entry,
725                  * it's probably not a directory.
726                  */
727                 memcpy(&dotdot, inode->i_block, sizeof(dotdot));
728                 memcpy(&de, ((char *)inode->i_block) + EXT4_INLINE_DATA_DOTDOT_SIZE,
729                        EXT2_DIR_REC_LEN(0));
730                 dotdot = ext2fs_le32_to_cpu(dotdot);
731                 de.inode = ext2fs_le32_to_cpu(de.inode);
732                 de.rec_len = ext2fs_le16_to_cpu(de.rec_len);
733                 ext2fs_get_rec_len(ctx->fs, &de, &rec_len2);
734                 if (dotdot >= ctx->fs->super->s_inodes_count ||
735                     (dotdot < EXT2_FIRST_INO(ctx->fs->super) &&
736                      dotdot != EXT2_ROOT_INO) ||
737                     de.inode >= ctx->fs->super->s_inodes_count ||
738                     (de.inode < EXT2_FIRST_INO(ctx->fs->super) &&
739                      de.inode != 0) ||
740                     rec_len2 > EXT4_MIN_INLINE_DATA_SIZE -
741                               EXT4_INLINE_DATA_DOTDOT_SIZE)
742                         return;
743                 /* device files never have a "system.data" entry */
744                 goto isdir;
745         } else if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
746                 /* extent mapped */
747                 if  (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
748                                  &blk))
749                         return;
750                 /* device files are never extent mapped */
751                 not_device++;
752         } else {
753                 for (i=0; i < EXT2_N_BLOCKS; i++) {
754                         blk = inode->i_block[i];
755                         if (!blk)
756                                 continue;
757                         if (i >= 4)
758                                 not_device++;
759
760                         if (blk < ctx->fs->super->s_first_data_block ||
761                             blk >= ext2fs_blocks_count(ctx->fs->super) ||
762                             ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
763                                                            blk))
764                                 return; /* Invalid block, can't be dir */
765                 }
766                 blk = inode->i_block[0];
767         }
768
769         /*
770          * If the mode says this is a device file and the i_links_count field
771          * is sane and we have not ruled it out as a device file previously,
772          * we declare it a device file, not a directory.
773          */
774         if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
775             (inode->i_links_count == 1) && !not_device)
776                 return;
777
778         /* read the first block */
779         ehandler_operation(_("reading directory block"));
780         retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
781         ehandler_operation(0);
782         if (retval)
783                 return;
784
785         dirent = (struct ext2_dir_entry *) buf;
786         retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
787         if (retval)
788                 return;
789         if ((ext2fs_dirent_name_len(dirent) != 1) ||
790             (dirent->name[0] != '.') ||
791             (dirent->inode != pctx->ino) ||
792             (rec_len < 12) ||
793             (rec_len % 4) ||
794             (rec_len >= ctx->fs->blocksize - 12))
795                 return;
796
797         dirent = (struct ext2_dir_entry *) (buf + rec_len);
798         retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
799         if (retval)
800                 return;
801         if ((ext2fs_dirent_name_len(dirent) != 2) ||
802             (dirent->name[0] != '.') ||
803             (dirent->name[1] != '.') ||
804             (rec_len < 12) ||
805             (rec_len % 4))
806                 return;
807
808 isdir:
809         if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
810                 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
811                 e2fsck_write_inode_full(ctx, pctx->ino, inode,
812                                         EXT2_INODE_SIZE(ctx->fs->super),
813                                         "check_is_really_dir");
814         }
815 }
816
817 extern errcode_t e2fsck_setup_icount(e2fsck_t ctx, const char *icount_name,
818                                      int flags, ext2_icount_t hint,
819                                      ext2_icount_t *ret)
820 {
821         unsigned int            threshold;
822         unsigned int            save_type;
823         ext2_ino_t              num_dirs;
824         errcode_t               retval;
825         char                    *tdb_dir;
826         int                     enable;
827
828         *ret = 0;
829
830         profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
831                            &tdb_dir);
832         profile_get_uint(ctx->profile, "scratch_files",
833                          "numdirs_threshold", 0, 0, &threshold);
834         profile_get_boolean(ctx->profile, "scratch_files",
835                             "icount", 0, 1, &enable);
836
837         retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
838         if (retval)
839                 num_dirs = 1024;        /* Guess */
840
841         if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
842             (!threshold || num_dirs > threshold)) {
843                 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir,
844                                                   flags, ret);
845                 if (retval == 0)
846                         return 0;
847         }
848         e2fsck_set_bitmap_type(ctx->fs, EXT2FS_BMAP64_RBTREE, icount_name,
849                                &save_type);
850         retval = ext2fs_create_icount2(ctx->fs, flags, 0, hint, ret);
851         ctx->fs->default_bitmap_type = save_type;
852         return retval;
853 }
854
855 static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
856                                             e2fsck_t ctx,
857                                             struct problem_context *pctx)
858 {
859         errcode_t retval;
860         struct ext2_inode_large inode;
861
862         /*
863          * Reread inode.  If we don't see checksum error, then this inode
864          * has been fixed elsewhere.
865          */
866         ctx->stashed_ino = 0;
867         retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
868                                         sizeof(inode));
869         if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
870                 return retval;
871         if (!retval)
872                 return 0;
873
874         /*
875          * Checksum still doesn't match.  That implies that the inode passes
876          * all the sanity checks, so maybe the checksum is simply corrupt.
877          * See if the user will go for fixing that.
878          */
879         if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
880                 return 0;
881
882         retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
883                                          sizeof(inode));
884         return retval;
885 }
886
887 static void reserve_block_for_root_repair(e2fsck_t ctx)
888 {
889         blk64_t         blk = 0;
890         errcode_t       err;
891         ext2_filsys     fs = ctx->fs;
892
893         ctx->root_repair_block = 0;
894         if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
895                 return;
896
897         err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
898         if (err)
899                 return;
900         ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
901         ctx->root_repair_block = blk;
902 }
903
904 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
905 {
906         blk64_t         blk = 0;
907         errcode_t       err;
908         ext2_filsys     fs = ctx->fs;
909         static const char name[] = "lost+found";
910         ext2_ino_t      ino;
911
912         ctx->lnf_repair_block = 0;
913         if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
914                 return;
915
916         err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
917         if (err)
918                 return;
919         ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
920         ctx->lnf_repair_block = blk;
921 }
922
923 static errcode_t get_inline_data_ea_size(ext2_filsys fs, ext2_ino_t ino,
924                                          size_t *sz)
925 {
926         void *p;
927         struct ext2_xattr_handle *handle;
928         errcode_t retval;
929
930         retval = ext2fs_xattrs_open(fs, ino, &handle);
931         if (retval)
932                 return retval;
933
934         retval = ext2fs_xattrs_read(handle);
935         if (retval)
936                 goto err;
937
938         retval = ext2fs_xattr_get(handle, "system.data", &p, sz);
939         if (retval)
940                 goto err;
941         ext2fs_free_mem(&p);
942 err:
943         (void) ext2fs_xattrs_close(&handle);
944         return retval;
945 }
946
947 static void finish_processing_inode(e2fsck_t ctx, ext2_ino_t ino,
948                                     struct problem_context *pctx,
949                                     int failed_csum)
950 {
951         if (!failed_csum)
952                 return;
953
954         /*
955          * If the inode failed the checksum and the user didn't
956          * clear the inode, test the checksum again -- if it still
957          * fails, ask the user if the checksum should be corrected.
958          */
959         pctx->errcode = recheck_bad_inode_checksum(ctx->fs, ino, ctx, pctx);
960         if (pctx->errcode)
961                 ctx->flags |= E2F_FLAG_ABORT;
962 }
963 #define FINISH_INODE_LOOP(ctx, ino, pctx, failed_csum) \
964         do { \
965                 finish_processing_inode((ctx), (ino), (pctx), (failed_csum)); \
966                 if ((ctx)->flags & E2F_FLAG_ABORT) \
967                         return; \
968         } while (0)
969
970 static int could_be_block_map(ext2_filsys fs, struct ext2_inode *inode)
971 {
972         __u32 x;
973         int i;
974
975         for (i = 0; i < EXT2_N_BLOCKS; i++) {
976                 x = inode->i_block[i];
977 #ifdef WORDS_BIGENDIAN
978                 x = ext2fs_swab32(x);
979 #endif
980                 if (x >= ext2fs_blocks_count(fs->super))
981                         return 0;
982         }
983
984         return 1;
985 }
986
987 /*
988  * Figure out what to do with an inode that has both extents and inline data
989  * inode flags set.  Returns -1 if we decide to erase the inode, 0 otherwise.
990  */
991 static int fix_inline_data_extents_file(e2fsck_t ctx,
992                                         ext2_ino_t ino,
993                                         struct ext2_inode *inode,
994                                         int inode_size,
995                                         struct problem_context *pctx)
996 {
997         size_t max_inline_ea_size;
998         ext2_filsys fs = ctx->fs;
999         int dirty = 0;
1000
1001         /* Both feature flags not set?  Just run the regular checks */
1002         if (!ext2fs_has_feature_extents(fs->super) &&
1003             !ext2fs_has_feature_inline_data(fs->super))
1004                 return 0;
1005
1006         /* Clear both flags if it's a special file */
1007         if (LINUX_S_ISCHR(inode->i_mode) ||
1008             LINUX_S_ISBLK(inode->i_mode) ||
1009             LINUX_S_ISFIFO(inode->i_mode) ||
1010             LINUX_S_ISSOCK(inode->i_mode)) {
1011                 check_extents_inlinedata(ctx, pctx);
1012                 return 0;
1013         }
1014
1015         /* If it looks like an extent tree, try to clear inlinedata */
1016         if (ext2fs_extent_header_verify(inode->i_block,
1017                                  sizeof(inode->i_block)) == 0 &&
1018             fix_problem(ctx, PR_1_CLEAR_INLINE_DATA_FOR_EXTENT, pctx)) {
1019                 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1020                 dirty = 1;
1021                 goto out;
1022         }
1023
1024         /* If it looks short enough to be inline data, try to clear extents */
1025         if (inode_size > EXT2_GOOD_OLD_INODE_SIZE)
1026                 max_inline_ea_size = inode_size -
1027                                      (EXT2_GOOD_OLD_INODE_SIZE +
1028                                       ((struct ext2_inode_large *)inode)->i_extra_isize);
1029         else
1030                 max_inline_ea_size = 0;
1031         if (EXT2_I_SIZE(inode) <
1032             EXT4_MIN_INLINE_DATA_SIZE + max_inline_ea_size &&
1033             fix_problem(ctx, PR_1_CLEAR_EXTENT_FOR_INLINE_DATA, pctx)) {
1034                 inode->i_flags &= ~EXT4_EXTENTS_FL;
1035                 dirty = 1;
1036                 goto out;
1037         }
1038
1039         /*
1040          * Too big for inline data, but no evidence of extent tree -
1041          * maybe it's a block map file?  If the mappings all look valid?
1042          */
1043         if (could_be_block_map(fs, inode) &&
1044             fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_FLAGS, pctx)) {
1045 #ifdef WORDS_BIGENDIAN
1046                 int i;
1047
1048                 for (i = 0; i < EXT2_N_BLOCKS; i++)
1049                         inode->i_block[i] = ext2fs_swab32(inode->i_block[i]);
1050 #endif
1051
1052                 inode->i_flags &= ~(EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL);
1053                 dirty = 1;
1054                 goto out;
1055         }
1056
1057         /* Oh well, just clear the busted inode. */
1058         if (fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_INODE, pctx)) {
1059                 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1060                 return -1;
1061         }
1062
1063 out:
1064         if (dirty)
1065                 e2fsck_write_inode(ctx, ino, inode, "pass1");
1066
1067         return 0;
1068 }
1069
1070 static void pass1_readahead(e2fsck_t ctx, dgrp_t *group, ext2_ino_t *next_ino)
1071 {
1072         ext2_ino_t inodes_in_group = 0, inodes_per_block, inodes_per_buffer;
1073         dgrp_t start = *group, grp;
1074         blk64_t blocks_to_read = 0;
1075         errcode_t err = EXT2_ET_INVALID_ARGUMENT;
1076
1077         if (ctx->readahead_kb == 0)
1078                 goto out;
1079
1080         /* Keep iterating groups until we have enough to readahead */
1081         inodes_per_block = EXT2_INODES_PER_BLOCK(ctx->fs->super);
1082         for (grp = start; grp < ctx->fs->group_desc_count; grp++) {
1083                 if (ext2fs_bg_flags_test(ctx->fs, grp, EXT2_BG_INODE_UNINIT))
1084                         continue;
1085                 inodes_in_group = ctx->fs->super->s_inodes_per_group -
1086                                         ext2fs_bg_itable_unused(ctx->fs, grp);
1087                 blocks_to_read += (inodes_in_group + inodes_per_block - 1) /
1088                                         inodes_per_block;
1089                 if (blocks_to_read * ctx->fs->blocksize >
1090                     ctx->readahead_kb * 1024)
1091                         break;
1092         }
1093
1094         err = e2fsck_readahead(ctx->fs, E2FSCK_READA_ITABLE, start,
1095                                grp - start + 1);
1096         if (err == EAGAIN) {
1097                 ctx->readahead_kb /= 2;
1098                 err = 0;
1099         }
1100
1101 out:
1102         if (err) {
1103                 /* Error; disable itable readahead */
1104                 *group = ctx->fs->group_desc_count;
1105                 *next_ino = ctx->fs->super->s_inodes_count;
1106         } else {
1107                 /*
1108                  * Don't do more readahead until we've reached the first inode
1109                  * of the last inode scan buffer block for the last group.
1110                  */
1111                 *group = grp + 1;
1112                 inodes_per_buffer = (ctx->inode_buffer_blocks ?
1113                                      ctx->inode_buffer_blocks :
1114                                      EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS) *
1115                                     ctx->fs->blocksize /
1116                                     EXT2_INODE_SIZE(ctx->fs->super);
1117                 inodes_in_group--;
1118                 *next_ino = inodes_in_group -
1119                             (inodes_in_group % inodes_per_buffer) + 1 +
1120                             (grp * ctx->fs->super->s_inodes_per_group);
1121         }
1122 }
1123
1124 /*
1125  * Check if the passed ino is one of the used superblock quota inodes.
1126  *
1127  * Before the quota inodes were journaled, older superblock quota inodes
1128  * were just regular files in the filesystem and not reserved inodes.  This
1129  * checks if the passed ino is one of the s_*_quota_inum superblock fields,
1130  * which may not always be the same as the EXT4_*_QUOTA_INO fields.
1131  */
1132 static int quota_inum_is_super(struct ext2_super_block *sb, ext2_ino_t ino)
1133 {
1134         enum quota_type qtype;
1135
1136         for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1137                 if (*quota_sb_inump(sb, qtype) == ino)
1138                         return 1;
1139
1140         return 0;
1141 }
1142
1143 /*
1144  * Check if the passed ino is one of the reserved quota inodes.
1145  * This checks if the inode number is one of the reserved EXT4_*_QUOTA_INO
1146  * inodes.  These inodes may or may not be in use by the quota feature.
1147  */
1148 static int quota_inum_is_reserved(ext2_filsys fs, ext2_ino_t ino)
1149 {
1150         enum quota_type qtype;
1151
1152         for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1153                 if (quota_type2inum(qtype, fs->super) == ino)
1154                         return 1;
1155
1156         return 0;
1157 }
1158
1159 void e2fsck_pass1(e2fsck_t ctx)
1160 {
1161         int     i;
1162         __u64   max_sizes;
1163         ext2_filsys fs = ctx->fs;
1164         ext2_ino_t      ino = 0;
1165         struct ext2_inode *inode = NULL;
1166         ext2_inode_scan scan = NULL;
1167         char            *block_buf = NULL;
1168 #ifdef RESOURCE_TRACK
1169         struct resource_track   rtrack;
1170 #endif
1171         unsigned char   frag, fsize;
1172         struct          problem_context pctx;
1173         struct          scan_callback_struct scan_struct;
1174         struct ext2_super_block *sb = ctx->fs->super;
1175         const char      *old_op;
1176         int             imagic_fs, extent_fs, inlinedata_fs;
1177         int             low_dtime_check = 1;
1178         int             inode_size = EXT2_INODE_SIZE(fs->super);
1179         int             bufsize;
1180         int             failed_csum = 0;
1181         ext2_ino_t      ino_threshold = 0;
1182         dgrp_t          ra_group = 0;
1183         blk64_t         ea_ibody_quota_blocks;
1184
1185         init_resource_track(&rtrack, ctx->fs->io);
1186         clear_problem_context(&pctx);
1187
1188         /* If we can do readahead, figure out how many groups to pull in. */
1189         if (!e2fsck_can_readahead(ctx->fs))
1190                 ctx->readahead_kb = 0;
1191         else if (ctx->readahead_kb == ~0ULL)
1192                 ctx->readahead_kb = e2fsck_guess_readahead(ctx->fs);
1193         pass1_readahead(ctx, &ra_group, &ino_threshold);
1194
1195         if (!(ctx->options & E2F_OPT_PREEN))
1196                 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1197
1198         if (ext2fs_has_feature_dir_index(fs->super) &&
1199             !(ctx->options & E2F_OPT_NO)) {
1200                 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
1201                         ctx->dirs_to_hash = 0;
1202         }
1203
1204 #ifdef MTRACE
1205         mtrace_print("Pass 1");
1206 #endif
1207
1208 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
1209
1210         for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
1211                 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
1212                 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
1213                 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
1214                 max_sizes = (max_sizes * (1UL << i));
1215                 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
1216         }
1217 #undef EXT2_BPP
1218
1219         imagic_fs = ext2fs_has_feature_imagic_inodes(sb);
1220         extent_fs = ext2fs_has_feature_extents(sb);
1221         inlinedata_fs = ext2fs_has_feature_inline_data(sb);
1222
1223         /*
1224          * Allocate bitmaps structures
1225          */
1226         pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
1227                                                     EXT2FS_BMAP64_RBTREE,
1228                                                     "inode_used_map",
1229                                                     &ctx->inode_used_map);
1230         if (pctx.errcode) {
1231                 pctx.num = 1;
1232                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1233                 ctx->flags |= E2F_FLAG_ABORT;
1234                 return;
1235         }
1236         pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1237                         _("directory inode map"),
1238                         EXT2FS_BMAP64_AUTODIR,
1239                         "inode_dir_map", &ctx->inode_dir_map);
1240         if (pctx.errcode) {
1241                 pctx.num = 2;
1242                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1243                 ctx->flags |= E2F_FLAG_ABORT;
1244                 return;
1245         }
1246         pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1247                         _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
1248                         "inode_reg_map", &ctx->inode_reg_map);
1249         if (pctx.errcode) {
1250                 pctx.num = 6;
1251                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1252                 ctx->flags |= E2F_FLAG_ABORT;
1253                 return;
1254         }
1255         pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
1256                         _("in-use block map"), EXT2FS_BMAP64_RBTREE,
1257                         "block_found_map", &ctx->block_found_map);
1258         if (pctx.errcode) {
1259                 pctx.num = 1;
1260                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1261                 ctx->flags |= E2F_FLAG_ABORT;
1262                 return;
1263         }
1264         pctx.errcode = e2fsck_allocate_block_bitmap(fs,
1265                         _("metadata block map"), EXT2FS_BMAP64_RBTREE,
1266                         "block_metadata_map", &ctx->block_metadata_map);
1267         if (pctx.errcode) {
1268                 pctx.num = 1;
1269                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1270                 ctx->flags |= E2F_FLAG_ABORT;
1271                 return;
1272         }
1273         pctx.errcode = e2fsck_setup_icount(ctx, "inode_link_info", 0, NULL,
1274                                            &ctx->inode_link_info);
1275         if (pctx.errcode) {
1276                 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1277                 ctx->flags |= E2F_FLAG_ABORT;
1278                 return;
1279         }
1280         bufsize = inode_size;
1281         if (bufsize < sizeof(struct ext2_inode_large))
1282                 bufsize = sizeof(struct ext2_inode_large);
1283         inode = (struct ext2_inode *)
1284                 e2fsck_allocate_memory(ctx, bufsize, "scratch inode");
1285
1286         inodes_to_process = (struct process_inode_block *)
1287                 e2fsck_allocate_memory(ctx,
1288                                        (ctx->process_inode_size *
1289                                         sizeof(struct process_inode_block)),
1290                                        "array of inodes to process");
1291         process_inode_count = 0;
1292
1293         pctx.errcode = ext2fs_init_dblist(fs, 0);
1294         if (pctx.errcode) {
1295                 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1296                 ctx->flags |= E2F_FLAG_ABORT;
1297                 goto endit;
1298         }
1299
1300         /*
1301          * If the last orphan field is set, clear it, since the pass1
1302          * processing will automatically find and clear the orphans.
1303          * In the future, we may want to try using the last_orphan
1304          * linked list ourselves, but for now, we clear it so that the
1305          * ext3 mount code won't get confused.
1306          */
1307         if (!(ctx->options & E2F_OPT_READONLY)) {
1308                 if (fs->super->s_last_orphan) {
1309                         fs->super->s_last_orphan = 0;
1310                         ext2fs_mark_super_dirty(fs);
1311                 }
1312         }
1313
1314         mark_table_blocks(ctx);
1315         pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
1316                                                 &ctx->block_found_map);
1317         if (pctx.errcode) {
1318                 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1319                 ctx->flags |= E2F_FLAG_ABORT;
1320                 goto endit;
1321         }
1322         block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1323                                                     "block interate buffer");
1324         if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1325                 e2fsck_use_inode_shortcuts(ctx, 1);
1326         e2fsck_intercept_block_allocations(ctx);
1327         old_op = ehandler_operation(_("opening inode scan"));
1328         pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1329                                               &scan);
1330         ehandler_operation(old_op);
1331         if (pctx.errcode) {
1332                 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1333                 ctx->flags |= E2F_FLAG_ABORT;
1334                 goto endit;
1335         }
1336         ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE |
1337                                       EXT2_SF_WARN_GARBAGE_INODES, 0);
1338         ctx->stashed_inode = inode;
1339         scan_struct.ctx = ctx;
1340         scan_struct.block_buf = block_buf;
1341         ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1342         if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1343                                               ctx->fs->group_desc_count)))
1344                 goto endit;
1345         if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
1346             (fs->super->s_mtime < fs->super->s_inodes_count) ||
1347             (fs->super->s_mkfs_time &&
1348              fs->super->s_mkfs_time < fs->super->s_inodes_count))
1349                 low_dtime_check = 0;
1350
1351         if (ext2fs_has_feature_mmp(fs->super) &&
1352             fs->super->s_mmp_block > fs->super->s_first_data_block &&
1353             fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1354                 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1355                                           fs->super->s_mmp_block);
1356
1357         /* Set up ctx->lost_and_found if possible */
1358         (void) e2fsck_get_lost_and_found(ctx, 0);
1359
1360         while (1) {
1361                 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
1362                         if (e2fsck_mmp_update(fs))
1363                                 fatal_error(ctx, 0);
1364                 }
1365                 old_op = ehandler_operation(_("getting next inode from scan"));
1366                 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1367                                                           inode, inode_size);
1368                 if (ino > ino_threshold)
1369                         pass1_readahead(ctx, &ra_group, &ino_threshold);
1370                 ehandler_operation(old_op);
1371                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1372                         goto endit;
1373                 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1374                         /*
1375                          * If badblocks says badblocks is bad, offer to clear
1376                          * the list, update the in-core bb list, and restart
1377                          * the inode scan.
1378                          */
1379                         if (ino == EXT2_BAD_INO &&
1380                             fix_problem(ctx, PR_1_BADBLOCKS_IN_BADBLOCKS,
1381                                         &pctx)) {
1382                                 errcode_t err;
1383
1384                                 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1385                                 ext2fs_badblocks_list_free(ctx->fs->badblocks);
1386                                 ctx->fs->badblocks = NULL;
1387                                 err = ext2fs_read_bb_inode(ctx->fs,
1388                                                         &ctx->fs->badblocks);
1389                                 if (err) {
1390                                         fix_problem(ctx, PR_1_ISCAN_ERROR,
1391                                                     &pctx);
1392                                         ctx->flags |= E2F_FLAG_ABORT;
1393                                         goto endit;
1394                                 }
1395                                 err = ext2fs_inode_scan_goto_blockgroup(scan,
1396                                                                         0);
1397                                 if (err) {
1398                                         fix_problem(ctx, PR_1_ISCAN_ERROR,
1399                                                     &pctx);
1400                                         ctx->flags |= E2F_FLAG_ABORT;
1401                                         goto endit;
1402                                 }
1403                                 continue;
1404                         }
1405                         if (!ctx->inode_bb_map)
1406                                 alloc_bb_map(ctx);
1407                         ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1408                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1409                         continue;
1410                 }
1411                 if (pctx.errcode &&
1412                     pctx.errcode != EXT2_ET_INODE_CSUM_INVALID &&
1413                     pctx.errcode != EXT2_ET_INODE_IS_GARBAGE) {
1414                         fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1415                         ctx->flags |= E2F_FLAG_ABORT;
1416                         goto endit;
1417                 }
1418                 if (!ino)
1419                         break;
1420                 pctx.ino = ino;
1421                 pctx.inode = inode;
1422                 ctx->stashed_ino = ino;
1423
1424                 /* Clear trashed inode? */
1425                 if (pctx.errcode == EXT2_ET_INODE_IS_GARBAGE &&
1426                     inode->i_links_count > 0 &&
1427                     fix_problem(ctx, PR_1_INODE_IS_GARBAGE, &pctx)) {
1428                         pctx.errcode = 0;
1429                         e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1430                 }
1431                 failed_csum = pctx.errcode != 0;
1432
1433                 /*
1434                  * Check for inodes who might have been part of the
1435                  * orphaned list linked list.  They should have gotten
1436                  * dealt with by now, unless the list had somehow been
1437                  * corrupted.
1438                  *
1439                  * FIXME: In the future, inodes which are still in use
1440                  * (and which are therefore) pending truncation should
1441                  * be handled specially.  Right now we just clear the
1442                  * dtime field, and the normal e2fsck handling of
1443                  * inodes where i_size and the inode blocks are
1444                  * inconsistent is to fix i_size, instead of releasing
1445                  * the extra blocks.  This won't catch the inodes that
1446                  * was at the end of the orphan list, but it's better
1447                  * than nothing.  The right answer is that there
1448                  * shouldn't be any bugs in the orphan list handling.  :-)
1449                  */
1450                 if (inode->i_dtime && low_dtime_check &&
1451                     inode->i_dtime < ctx->fs->super->s_inodes_count) {
1452                         if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1453                                 inode->i_dtime = inode->i_links_count ?
1454                                         0 : ctx->now;
1455                                 e2fsck_write_inode(ctx, ino, inode,
1456                                                    "pass1");
1457                                 failed_csum = 0;
1458                         }
1459                 }
1460
1461                 if (inode->i_links_count) {
1462                         pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
1463                                            ino, inode->i_links_count);
1464                         if (pctx.errcode) {
1465                                 pctx.num = inode->i_links_count;
1466                                 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
1467                                 ctx->flags |= E2F_FLAG_ABORT;
1468                                 goto endit;
1469                         }
1470                 } else if ((ino >= EXT2_FIRST_INODE(fs->super)) &&
1471                            !quota_inum_is_reserved(fs, ino)) {
1472                         if (!inode->i_dtime && inode->i_mode) {
1473                                 if (fix_problem(ctx,
1474                                             PR_1_ZERO_DTIME, &pctx)) {
1475                                         inode->i_dtime = ctx->now;
1476                                         e2fsck_write_inode(ctx, ino, inode,
1477                                                            "pass1");
1478                                         failed_csum = 0;
1479                                 }
1480                         }
1481                         FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1482                         continue;
1483                 }
1484
1485                 /* Conflicting inlinedata/extents inode flags? */
1486                 if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
1487                     (inode->i_flags & EXT4_EXTENTS_FL)) {
1488                         int res = fix_inline_data_extents_file(ctx, ino, inode,
1489                                                                inode_size,
1490                                                                &pctx);
1491                         if (res < 0) {
1492                                 /* skip FINISH_INODE_LOOP */
1493                                 continue;
1494                         }
1495                 }
1496
1497                 /* Test for incorrect inline_data flags settings. */
1498                 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && !inlinedata_fs &&
1499                     (ino >= EXT2_FIRST_INODE(fs->super))) {
1500                         size_t size = 0;
1501
1502                         pctx.errcode = ext2fs_inline_data_size(fs, ino, &size);
1503                         if (!pctx.errcode && size &&
1504                             fix_problem(ctx, PR_1_INLINE_DATA_FEATURE, &pctx)) {
1505                                 ext2fs_set_feature_inline_data(sb);
1506                                 ext2fs_mark_super_dirty(fs);
1507                                 inlinedata_fs = 1;
1508                         } else if (fix_problem(ctx, PR_1_INLINE_DATA_SET, &pctx)) {
1509                                 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1510                                 /* skip FINISH_INODE_LOOP */
1511                                 continue;
1512                         }
1513                 }
1514
1515                 /* Test for inline data flag but no attr */
1516                 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && inlinedata_fs &&
1517                     (ino >= EXT2_FIRST_INODE(fs->super))) {
1518                         size_t size = 0;
1519                         errcode_t err;
1520                         int flags;
1521
1522                         flags = fs->flags;
1523                         if (failed_csum)
1524                                 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1525                         err = get_inline_data_ea_size(fs, ino, &size);
1526                         fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
1527                                     (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
1528
1529                         switch (err) {
1530                         case 0:
1531                                 /* Everything is awesome... */
1532                                 break;
1533                         case EXT2_ET_BAD_EA_BLOCK_NUM:
1534                         case EXT2_ET_BAD_EA_HASH:
1535                         case EXT2_ET_BAD_EA_HEADER:
1536                         case EXT2_ET_EA_BAD_NAME_LEN:
1537                         case EXT2_ET_EA_BAD_VALUE_SIZE:
1538                         case EXT2_ET_EA_KEY_NOT_FOUND:
1539                         case EXT2_ET_EA_NO_SPACE:
1540                         case EXT2_ET_MISSING_EA_FEATURE:
1541                         case EXT2_ET_INLINE_DATA_CANT_ITERATE:
1542                         case EXT2_ET_INLINE_DATA_NO_BLOCK:
1543                         case EXT2_ET_INLINE_DATA_NO_SPACE:
1544                         case EXT2_ET_NO_INLINE_DATA:
1545                         case EXT2_ET_EXT_ATTR_CSUM_INVALID:
1546                         case EXT2_ET_EA_BAD_VALUE_OFFSET:
1547                                 /* broken EA or no system.data EA; truncate */
1548                                 if (fix_problem(ctx, PR_1_INLINE_DATA_NO_ATTR,
1549                                                 &pctx)) {
1550                                         err = ext2fs_inode_size_set(fs, inode, 0);
1551                                         if (err) {
1552                                                 pctx.errcode = err;
1553                                                 ctx->flags |= E2F_FLAG_ABORT;
1554                                                 goto endit;
1555                                         }
1556                                         inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1557                                         memset(&inode->i_block, 0,
1558                                                sizeof(inode->i_block));
1559                                         e2fsck_write_inode(ctx, ino, inode,
1560                                                            "pass1");
1561                                         failed_csum = 0;
1562                                 }
1563                                 break;
1564                         default:
1565                                 /* Some other kind of non-xattr error? */
1566                                 pctx.errcode = err;
1567                                 ctx->flags |= E2F_FLAG_ABORT;
1568                                 goto endit;
1569                         }
1570                 }
1571
1572                 /*
1573                  * Test for incorrect extent flag settings.
1574                  *
1575                  * On big-endian machines we must be careful:
1576                  * When the inode is read, the i_block array is not swapped
1577                  * if the extent flag is set.  Therefore if we are testing
1578                  * for or fixing a wrongly-set flag, we must potentially
1579                  * (un)swap before testing, or after fixing.
1580                  */
1581
1582                 /*
1583                  * In this case the extents flag was set when read, so
1584                  * extent_header_verify is ok.  If the inode is cleared,
1585                  * no need to swap... so no extra swapping here.
1586                  */
1587                 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
1588                     (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1589                      (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
1590                         if ((ext2fs_extent_header_verify(inode->i_block,
1591                                                  sizeof(inode->i_block)) == 0) &&
1592                             fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
1593                                 ext2fs_set_feature_extents(sb);
1594                                 ext2fs_mark_super_dirty(fs);
1595                                 extent_fs = 1;
1596                         } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
1597                         clear_inode:
1598                                 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1599                                 if (ino == EXT2_BAD_INO)
1600                                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
1601                                                                  ino);
1602                                 /* skip FINISH_INODE_LOOP */
1603                                 continue;
1604                         }
1605                 }
1606
1607                 /*
1608                  * For big-endian machines:
1609                  * If the inode didn't have the extents flag set when it
1610                  * was read, then the i_blocks array was swapped.  To test
1611                  * as an extents header, we must swap it back first.
1612                  * IF we then set the extents flag, the entire i_block
1613                  * array must be un/re-swapped to make it proper extents data.
1614                  */
1615                 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
1616                     (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1617                      (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
1618                     (LINUX_S_ISREG(inode->i_mode) ||
1619                      LINUX_S_ISDIR(inode->i_mode))) {
1620                         void *ehp;
1621 #ifdef WORDS_BIGENDIAN
1622                         __u32 tmp_block[EXT2_N_BLOCKS];
1623
1624                         for (i = 0; i < EXT2_N_BLOCKS; i++)
1625                                 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
1626                         ehp = tmp_block;
1627 #else
1628                         ehp = inode->i_block;
1629 #endif
1630                         if ((ext2fs_extent_header_verify(ehp,
1631                                          sizeof(inode->i_block)) == 0) &&
1632                             (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
1633                                 inode->i_flags |= EXT4_EXTENTS_FL;
1634 #ifdef WORDS_BIGENDIAN
1635                                 memcpy(inode->i_block, tmp_block,
1636                                        sizeof(inode->i_block));
1637 #endif
1638                                 e2fsck_write_inode(ctx, ino, inode, "pass1");
1639                                 failed_csum = 0;
1640                         }
1641                 }
1642
1643                 if (ino == EXT2_BAD_INO) {
1644                         struct process_block_struct pb;
1645
1646                         if ((failed_csum || inode->i_mode || inode->i_uid ||
1647                              inode->i_gid || inode->i_links_count ||
1648                              (inode->i_flags & EXT4_INLINE_DATA_FL) ||
1649                              inode->i_file_acl) &&
1650                             fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
1651                                 memset(inode, 0, sizeof(struct ext2_inode));
1652                                 e2fsck_write_inode(ctx, ino, inode,
1653                                                    "clear bad inode");
1654                                 failed_csum = 0;
1655                         }
1656
1657                         pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
1658                                                           &pb.fs_meta_blocks);
1659                         if (pctx.errcode) {
1660                                 pctx.num = 4;
1661                                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1662                                 ctx->flags |= E2F_FLAG_ABORT;
1663                                 goto endit;
1664                         }
1665                         pb.ino = EXT2_BAD_INO;
1666                         pb.num_blocks = pb.last_block = 0;
1667                         pb.last_db_block = -1;
1668                         pb.num_illegal_blocks = 0;
1669                         pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
1670                         pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
1671                         pb.inode = inode;
1672                         pb.pctx = &pctx;
1673                         pb.ctx = ctx;
1674                         pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
1675                                      block_buf, process_bad_block, &pb);
1676                         ext2fs_free_block_bitmap(pb.fs_meta_blocks);
1677                         if (pctx.errcode) {
1678                                 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
1679                                 ctx->flags |= E2F_FLAG_ABORT;
1680                                 goto endit;
1681                         }
1682                         if (pb.bbcheck)
1683                                 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
1684                                 ctx->flags |= E2F_FLAG_ABORT;
1685                                 goto endit;
1686                         }
1687                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1688                         clear_problem_context(&pctx);
1689                         FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1690                         continue;
1691                 } else if (ino == EXT2_ROOT_INO) {
1692                         /*
1693                          * Make sure the root inode is a directory; if
1694                          * not, offer to clear it.  It will be
1695                          * regnerated in pass #3.
1696                          */
1697                         if (!LINUX_S_ISDIR(inode->i_mode)) {
1698                                 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
1699                                         goto clear_inode;
1700                         }
1701                         /*
1702                          * If dtime is set, offer to clear it.  mke2fs
1703                          * version 0.2b created filesystems with the
1704                          * dtime field set for the root and lost+found
1705                          * directories.  We won't worry about
1706                          * /lost+found, since that can be regenerated
1707                          * easily.  But we will fix the root directory
1708                          * as a special case.
1709                          */
1710                         if (inode->i_dtime && inode->i_links_count) {
1711                                 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
1712                                         inode->i_dtime = 0;
1713                                         e2fsck_write_inode(ctx, ino, inode,
1714                                                            "pass1");
1715                                         failed_csum = 0;
1716                                 }
1717                         }
1718                 } else if (ino == EXT2_JOURNAL_INO) {
1719                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1720                         if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
1721                                 if (!LINUX_S_ISREG(inode->i_mode) &&
1722                                     fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
1723                                                 &pctx)) {
1724                                         inode->i_mode = LINUX_S_IFREG;
1725                                         e2fsck_write_inode(ctx, ino, inode,
1726                                                            "pass1");
1727                                         failed_csum = 0;
1728                                 }
1729                                 check_blocks(ctx, &pctx, block_buf, 0);
1730                                 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1731                                 continue;
1732                         }
1733                         if ((inode->i_links_count ||
1734                              inode->i_blocks || inode->i_block[0]) &&
1735                             fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
1736                                         &pctx)) {
1737                                 memset(inode, 0, inode_size);
1738                                 ext2fs_icount_store(ctx->inode_link_info,
1739                                                     ino, 0);
1740                                 e2fsck_write_inode_full(ctx, ino, inode,
1741                                                         inode_size, "pass1");
1742                                 failed_csum = 0;
1743                         }
1744                 } else if (quota_inum_is_reserved(fs, ino)) {
1745                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1746                         if (ext2fs_has_feature_quota(fs->super) &&
1747                             quota_inum_is_super(fs->super, ino)) {
1748                                 if (!LINUX_S_ISREG(inode->i_mode) &&
1749                                     fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
1750                                                         &pctx)) {
1751                                         inode->i_mode = LINUX_S_IFREG;
1752                                         e2fsck_write_inode(ctx, ino, inode,
1753                                                         "pass1");
1754                                         failed_csum = 0;
1755                                 }
1756                                 check_blocks(ctx, &pctx, block_buf, 0);
1757                                 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1758                                 continue;
1759                         }
1760                         if ((inode->i_links_count ||
1761                              inode->i_blocks || inode->i_block[0]) &&
1762                             fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
1763                                         &pctx)) {
1764                                 memset(inode, 0, inode_size);
1765                                 ext2fs_icount_store(ctx->inode_link_info,
1766                                                     ino, 0);
1767                                 e2fsck_write_inode_full(ctx, ino, inode,
1768                                                         inode_size, "pass1");
1769                                 failed_csum = 0;
1770                         }
1771                 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
1772                         problem_t problem = 0;
1773
1774                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1775                         if (ino == EXT2_BOOT_LOADER_INO) {
1776                                 if (LINUX_S_ISDIR(inode->i_mode))
1777                                         problem = PR_1_RESERVED_BAD_MODE;
1778                         } else if (ino == EXT2_RESIZE_INO) {
1779                                 if (inode->i_mode &&
1780                                     !LINUX_S_ISREG(inode->i_mode))
1781                                         problem = PR_1_RESERVED_BAD_MODE;
1782                         } else {
1783                                 if (inode->i_mode != 0)
1784                                         problem = PR_1_RESERVED_BAD_MODE;
1785                         }
1786                         if (problem) {
1787                                 if (fix_problem(ctx, problem, &pctx)) {
1788                                         inode->i_mode = 0;
1789                                         e2fsck_write_inode(ctx, ino, inode,
1790                                                            "pass1");
1791                                         failed_csum = 0;
1792                                 }
1793                         }
1794                         check_blocks(ctx, &pctx, block_buf, 0);
1795                         FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1796                         continue;
1797                 }
1798
1799                 if (!inode->i_links_count) {
1800                         FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1801                         continue;
1802                 }
1803                 /*
1804                  * n.b.  0.3c ext2fs code didn't clear i_links_count for
1805                  * deleted files.  Oops.
1806                  *
1807                  * Since all new ext2 implementations get this right,
1808                  * we now assume that the case of non-zero
1809                  * i_links_count and non-zero dtime means that we
1810                  * should keep the file, not delete it.
1811                  *
1812                  */
1813                 if (inode->i_dtime) {
1814                         if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1815                                 inode->i_dtime = 0;
1816                                 e2fsck_write_inode(ctx, ino, inode, "pass1");
1817                                 failed_csum = 0;
1818                         }
1819                 }
1820
1821                 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1822                 switch (fs->super->s_creator_os) {
1823                     case EXT2_OS_HURD:
1824                         frag = inode->osd2.hurd2.h_i_frag;
1825                         fsize = inode->osd2.hurd2.h_i_fsize;
1826                         break;
1827                     default:
1828                         frag = fsize = 0;
1829                 }
1830
1831                 if (inode->i_faddr || frag || fsize ||
1832                     (!ext2fs_has_feature_largedir(fs->super) &&
1833                     (LINUX_S_ISDIR(inode->i_mode) && inode->i_size_high)))
1834                         mark_inode_bad(ctx, ino);
1835                 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
1836                     !ext2fs_has_feature_64bit(fs->super) &&
1837                     inode->osd2.linux2.l_i_file_acl_high != 0)
1838                         mark_inode_bad(ctx, ino);
1839                 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
1840                     !ext2fs_has_feature_huge_file(fs->super) &&
1841                     (inode->osd2.linux2.l_i_blocks_hi != 0))
1842                         mark_inode_bad(ctx, ino);
1843                 if (inode->i_flags & EXT2_IMAGIC_FL) {
1844                         if (imagic_fs) {
1845                                 if (!ctx->inode_imagic_map)
1846                                         alloc_imagic_map(ctx);
1847                                 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1848                                                          ino);
1849                         } else {
1850                                 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1851                                         inode->i_flags &= ~EXT2_IMAGIC_FL;
1852                                         e2fsck_write_inode(ctx, ino,
1853                                                            inode, "pass1");
1854                                         failed_csum = 0;
1855                                 }
1856                         }
1857                 }
1858
1859                 check_inode_extra_space(ctx, &pctx, &ea_ibody_quota_blocks);
1860                 check_is_really_dir(ctx, &pctx, block_buf);
1861
1862                 /*
1863                  * ext2fs_inode_has_valid_blocks2 does not actually look
1864                  * at i_block[] values, so not endian-sensitive here.
1865                  */
1866                 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1867                     LINUX_S_ISLNK(inode->i_mode) &&
1868                     !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1869                     fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1870                         inode->i_flags &= ~EXT4_EXTENTS_FL;
1871                         e2fsck_write_inode(ctx, ino, inode, "pass1");
1872                         failed_csum = 0;
1873                 }
1874
1875                 if (LINUX_S_ISDIR(inode->i_mode)) {
1876                         ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1877                         e2fsck_add_dir_info(ctx, ino, 0);
1878                         ctx->fs_directory_count++;
1879                         if (inode->i_flags & EXT4_ENCRYPT_FL)
1880                                 add_encrypted_dir(ctx, ino);
1881                 } else if (LINUX_S_ISREG (inode->i_mode)) {
1882                         ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1883                         ctx->fs_regular_count++;
1884                 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1885                            e2fsck_pass1_check_device_inode(fs, inode)) {
1886                         check_extents_inlinedata(ctx, &pctx);
1887                         check_immutable(ctx, &pctx);
1888                         check_size(ctx, &pctx);
1889                         ctx->fs_chardev_count++;
1890                 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1891                            e2fsck_pass1_check_device_inode(fs, inode)) {
1892                         check_extents_inlinedata(ctx, &pctx);
1893                         check_immutable(ctx, &pctx);
1894                         check_size(ctx, &pctx);
1895                         ctx->fs_blockdev_count++;
1896                 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1897                            e2fsck_pass1_check_symlink(fs, ino, inode,
1898                                                       block_buf)) {
1899                         check_immutable(ctx, &pctx);
1900                         ctx->fs_symlinks_count++;
1901                         if (inode->i_flags & EXT4_INLINE_DATA_FL) {
1902                                 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1903                                 continue;
1904                         } else if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1905                                 ctx->fs_fast_symlinks_count++;
1906                                 check_blocks(ctx, &pctx, block_buf,
1907                                              ea_ibody_quota_blocks);
1908                                 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1909                                 continue;
1910                         }
1911                 }
1912                 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1913                          e2fsck_pass1_check_device_inode(fs, inode)) {
1914                         check_extents_inlinedata(ctx, &pctx);
1915                         check_immutable(ctx, &pctx);
1916                         check_size(ctx, &pctx);
1917                         ctx->fs_fifo_count++;
1918                 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1919                            e2fsck_pass1_check_device_inode(fs, inode)) {
1920                         check_extents_inlinedata(ctx, &pctx);
1921                         check_immutable(ctx, &pctx);
1922                         check_size(ctx, &pctx);
1923                         ctx->fs_sockets_count++;
1924                 } else
1925                         mark_inode_bad(ctx, ino);
1926                 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1927                     !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
1928                         if (inode->i_block[EXT2_IND_BLOCK])
1929                                 ctx->fs_ind_count++;
1930                         if (inode->i_block[EXT2_DIND_BLOCK])
1931                                 ctx->fs_dind_count++;
1932                         if (inode->i_block[EXT2_TIND_BLOCK])
1933                                 ctx->fs_tind_count++;
1934                 }
1935                 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1936                     !(inode->i_flags & EXT4_INLINE_DATA_FL) &&
1937                     (inode->i_block[EXT2_IND_BLOCK] ||
1938                      inode->i_block[EXT2_DIND_BLOCK] ||
1939                      inode->i_block[EXT2_TIND_BLOCK] ||
1940                      ext2fs_file_acl_block(fs, inode))) {
1941                         struct process_inode_block *itp;
1942
1943                         itp = &inodes_to_process[process_inode_count];
1944                         itp->ino = ino;
1945                         itp->ea_ibody_quota_blocks = ea_ibody_quota_blocks;
1946                         if (inode_size < sizeof(struct ext2_inode_large))
1947                                 memcpy(&itp->inode, inode, inode_size);
1948                         else
1949                                 memcpy(&itp->inode, inode, sizeof(itp->inode));
1950                         process_inode_count++;
1951                 } else
1952                         check_blocks(ctx, &pctx, block_buf,
1953                                      ea_ibody_quota_blocks);
1954
1955                 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1956
1957                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1958                         goto endit;
1959
1960                 if (process_inode_count >= ctx->process_inode_size) {
1961                         process_inodes(ctx, block_buf);
1962
1963                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1964                                 goto endit;
1965                 }
1966         }
1967         process_inodes(ctx, block_buf);
1968         ext2fs_close_inode_scan(scan);
1969         scan = NULL;
1970
1971         reserve_block_for_root_repair(ctx);
1972         reserve_block_for_lnf_repair(ctx);
1973
1974         /*
1975          * If any extended attribute blocks' reference counts need to
1976          * be adjusted, either up (ctx->refcount_extra), or down
1977          * (ctx->refcount), then fix them.
1978          */
1979         if (ctx->refcount) {
1980                 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1981                 ea_refcount_free(ctx->refcount);
1982                 ctx->refcount = 0;
1983         }
1984         if (ctx->refcount_extra) {
1985                 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1986                                         block_buf, +1);
1987                 ea_refcount_free(ctx->refcount_extra);
1988                 ctx->refcount_extra = 0;
1989         }
1990
1991         if (ctx->invalid_bitmaps)
1992                 handle_fs_bad_blocks(ctx);
1993
1994         /* We don't need the block_ea_map any more */
1995         if (ctx->block_ea_map) {
1996                 ext2fs_free_block_bitmap(ctx->block_ea_map);
1997                 ctx->block_ea_map = 0;
1998         }
1999
2000         if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
2001                 clear_problem_context(&pctx);
2002                 pctx.errcode = ext2fs_create_resize_inode(fs);
2003                 if (pctx.errcode) {
2004                         if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
2005                                          &pctx)) {
2006                                 ctx->flags |= E2F_FLAG_ABORT;
2007                                 goto endit;
2008                         }
2009                         pctx.errcode = 0;
2010                 }
2011                 if (!pctx.errcode) {
2012                         e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
2013                                           "recreate inode");
2014                         inode->i_mtime = ctx->now;
2015                         e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
2016                                            "recreate inode");
2017                 }
2018                 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
2019         }
2020
2021         if (ctx->flags & E2F_FLAG_RESTART) {
2022                 /*
2023                  * Only the master copy of the superblock and block
2024                  * group descriptors are going to be written during a
2025                  * restart, so set the superblock to be used to be the
2026                  * master superblock.
2027                  */
2028                 ctx->use_superblock = 0;
2029                 unwind_pass1(fs);
2030                 goto endit;
2031         }
2032
2033         if (ctx->block_dup_map) {
2034                 if (ctx->options & E2F_OPT_PREEN) {
2035                         clear_problem_context(&pctx);
2036                         fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
2037                 }
2038                 e2fsck_pass1_dupblocks(ctx, block_buf);
2039         }
2040         ctx->flags |= E2F_FLAG_ALLOC_OK;
2041         ext2fs_free_mem(&inodes_to_process);
2042 endit:
2043         e2fsck_use_inode_shortcuts(ctx, 0);
2044
2045         if (scan)
2046                 ext2fs_close_inode_scan(scan);
2047         if (block_buf)
2048                 ext2fs_free_mem(&block_buf);
2049         if (inode)
2050                 ext2fs_free_mem(&inode);
2051
2052         /*
2053          * The l+f inode may have been cleared, so zap it now and
2054          * later passes will recalculate it if necessary
2055          */
2056         ctx->lost_and_found = 0;
2057
2058         if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
2059                 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
2060         else
2061                 ctx->invalid_bitmaps++;
2062 }
2063 #undef FINISH_INODE_LOOP
2064
2065 /*
2066  * When the inode_scan routines call this callback at the end of the
2067  * glock group, call process_inodes.
2068  */
2069 static errcode_t scan_callback(ext2_filsys fs,
2070                                ext2_inode_scan scan EXT2FS_ATTR((unused)),
2071                                dgrp_t group, void * priv_data)
2072 {
2073         struct scan_callback_struct *scan_struct;
2074         e2fsck_t ctx;
2075
2076         scan_struct = (struct scan_callback_struct *) priv_data;
2077         ctx = scan_struct->ctx;
2078
2079         process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
2080
2081         if (ctx->progress)
2082                 if ((ctx->progress)(ctx, 1, group+1,
2083                                     ctx->fs->group_desc_count))
2084                         return EXT2_ET_CANCEL_REQUESTED;
2085
2086         return 0;
2087 }
2088
2089 /*
2090  * Process the inodes in the "inodes to process" list.
2091  */
2092 static void process_inodes(e2fsck_t ctx, char *block_buf)
2093 {
2094         int                     i;
2095         struct ext2_inode       *old_stashed_inode;
2096         ext2_ino_t              old_stashed_ino;
2097         const char              *old_operation;
2098         char                    buf[80];
2099         struct problem_context  pctx;
2100
2101 #if 0
2102         printf("begin process_inodes: ");
2103 #endif
2104         if (process_inode_count == 0)
2105                 return;
2106         old_operation = ehandler_operation(0);
2107         old_stashed_inode = ctx->stashed_inode;
2108         old_stashed_ino = ctx->stashed_ino;
2109         qsort(inodes_to_process, process_inode_count,
2110                       sizeof(struct process_inode_block), process_inode_cmp);
2111         clear_problem_context(&pctx);
2112         for (i=0; i < process_inode_count; i++) {
2113                 pctx.inode = ctx->stashed_inode =
2114                         (struct ext2_inode *) &inodes_to_process[i].inode;
2115                 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
2116
2117 #if 0
2118                 printf("%u ", pctx.ino);
2119 #endif
2120                 sprintf(buf, _("reading indirect blocks of inode %u"),
2121                         pctx.ino);
2122                 ehandler_operation(buf);
2123                 check_blocks(ctx, &pctx, block_buf,
2124                              inodes_to_process[i].ea_ibody_quota_blocks);
2125                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2126                         break;
2127         }
2128         ctx->stashed_inode = old_stashed_inode;
2129         ctx->stashed_ino = old_stashed_ino;
2130         process_inode_count = 0;
2131 #if 0
2132         printf("end process inodes\n");
2133 #endif
2134         ehandler_operation(old_operation);
2135 }
2136
2137 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
2138 {
2139         const struct process_inode_block *ib_a =
2140                 (const struct process_inode_block *) a;
2141         const struct process_inode_block *ib_b =
2142                 (const struct process_inode_block *) b;
2143         int     ret;
2144
2145         ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
2146                ib_b->inode.i_block[EXT2_IND_BLOCK]);
2147         if (ret == 0)
2148                 /*
2149                  * We only call process_inodes() for non-extent
2150                  * inodes, so it's OK to pass NULL to
2151                  * ext2fs_file_acl_block() here.
2152                  */
2153                 ret = ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_a->inode)) -
2154                         ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_b->inode));
2155         if (ret == 0)
2156                 ret = ib_a->ino - ib_b->ino;
2157         return ret;
2158 }
2159
2160 /*
2161  * Mark an inode as being bad in some what
2162  */
2163 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
2164 {
2165         struct          problem_context pctx;
2166
2167         if (!ctx->inode_bad_map) {
2168                 clear_problem_context(&pctx);
2169
2170                 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2171                                 _("bad inode map"), EXT2FS_BMAP64_RBTREE,
2172                                 "inode_bad_map", &ctx->inode_bad_map);
2173                 if (pctx.errcode) {
2174                         pctx.num = 3;
2175                         fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2176                         /* Should never get here */
2177                         ctx->flags |= E2F_FLAG_ABORT;
2178                         return;
2179                 }
2180         }
2181         ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
2182 }
2183
2184 static void add_encrypted_dir(e2fsck_t ctx, ino_t ino)
2185 {
2186         struct          problem_context pctx;
2187
2188         if (!ctx->encrypted_dirs) {
2189                 pctx.errcode = ext2fs_u32_list_create(&ctx->encrypted_dirs, 0);
2190                 if (pctx.errcode)
2191                         goto error;
2192         }
2193         pctx.errcode = ext2fs_u32_list_add(ctx->encrypted_dirs, ino);
2194         if (pctx.errcode == 0)
2195                 return;
2196 error:
2197         fix_problem(ctx, PR_1_ALLOCATE_ENCRYPTED_DIRLIST, &pctx);
2198         /* Should never get here */
2199         ctx->flags |= E2F_FLAG_ABORT;
2200 }
2201
2202 /*
2203  * This procedure will allocate the inode "bb" (badblock) map table
2204  */
2205 static void alloc_bb_map(e2fsck_t ctx)
2206 {
2207         struct          problem_context pctx;
2208
2209         clear_problem_context(&pctx);
2210         pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2211                         _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
2212                         "inode_bb_map", &ctx->inode_bb_map);
2213         if (pctx.errcode) {
2214                 pctx.num = 4;
2215                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2216                 /* Should never get here */
2217                 ctx->flags |= E2F_FLAG_ABORT;
2218                 return;
2219         }
2220 }
2221
2222 /*
2223  * This procedure will allocate the inode imagic table
2224  */
2225 static void alloc_imagic_map(e2fsck_t ctx)
2226 {
2227         struct          problem_context pctx;
2228
2229         clear_problem_context(&pctx);
2230         pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2231                         _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
2232                         "inode_imagic_map", &ctx->inode_imagic_map);
2233         if (pctx.errcode) {
2234                 pctx.num = 5;
2235                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2236                 /* Should never get here */
2237                 ctx->flags |= E2F_FLAG_ABORT;
2238                 return;
2239         }
2240 }
2241
2242 /*
2243  * Marks a block as in use, setting the dup_map if it's been set
2244  * already.  Called by process_block and process_bad_block.
2245  *
2246  * WARNING: Assumes checks have already been done to make sure block
2247  * is valid.  This is true in both process_block and process_bad_block.
2248  */
2249 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
2250 {
2251         struct          problem_context pctx;
2252
2253         clear_problem_context(&pctx);
2254
2255         if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
2256                 if (!ctx->block_dup_map) {
2257                         pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
2258                                         _("multiply claimed block map"),
2259                                         EXT2FS_BMAP64_RBTREE, "block_dup_map",
2260                                         &ctx->block_dup_map);
2261                         if (pctx.errcode) {
2262                                 pctx.num = 3;
2263                                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
2264                                             &pctx);
2265                                 /* Should never get here */
2266                                 ctx->flags |= E2F_FLAG_ABORT;
2267                                 return;
2268                         }
2269                 }
2270                 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
2271         } else {
2272                 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
2273         }
2274 }
2275
2276 /*
2277  * When cluster size is greater than one block, it is caller's responsibility
2278  * to make sure block parameter starts at a cluster boundary.
2279  */
2280 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
2281                                       unsigned int num)
2282 {
2283         if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
2284                 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
2285         else {
2286                 int i;
2287                 for (i = 0; i < num; i += EXT2FS_CLUSTER_RATIO(ctx->fs))
2288                         mark_block_used(ctx, block + i);
2289         }
2290 }
2291
2292 /*
2293  * Adjust the extended attribute block's reference counts at the end
2294  * of pass 1, either by subtracting out references for EA blocks that
2295  * are still referenced in ctx->refcount, or by adding references for
2296  * EA blocks that had extra references as accounted for in
2297  * ctx->refcount_extra.
2298  */
2299 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
2300                                     char *block_buf, int adjust_sign)
2301 {
2302         struct ext2_ext_attr_header     *header;
2303         struct problem_context          pctx;
2304         ext2_filsys                     fs = ctx->fs;
2305         blk64_t                         blk;
2306         __u32                           should_be;
2307         ea_value_t                      count;
2308
2309         clear_problem_context(&pctx);
2310
2311         ea_refcount_intr_begin(refcount);
2312         while (1) {
2313                 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
2314                         break;
2315                 pctx.blk = blk;
2316                 pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
2317                                                      pctx.ino);
2318                 if (pctx.errcode) {
2319                         fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
2320                         return;
2321                 }
2322                 header = (struct ext2_ext_attr_header *) block_buf;
2323                 pctx.blkcount = header->h_refcount;
2324                 should_be = header->h_refcount + adjust_sign * (int)count;
2325                 pctx.num = should_be;
2326                 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
2327                         header->h_refcount = should_be;
2328                         pctx.errcode = ext2fs_write_ext_attr3(fs, blk,
2329                                                              block_buf,
2330                                                              pctx.ino);
2331                         if (pctx.errcode) {
2332                                 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
2333                                             &pctx);
2334                                 continue;
2335                         }
2336                 }
2337         }
2338 }
2339
2340 /*
2341  * Handle processing the extended attribute blocks
2342  */
2343 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
2344                            char *block_buf, blk64_t *ea_block_quota_blocks)
2345 {
2346         ext2_filsys fs = ctx->fs;
2347         ext2_ino_t      ino = pctx->ino;
2348         struct ext2_inode *inode = pctx->inode;
2349         blk64_t         blk;
2350         char *          end;
2351         struct ext2_ext_attr_header *header;
2352         struct ext2_ext_attr_entry *entry;
2353         blk64_t         quota_blocks = EXT2FS_C2B(fs, 1);
2354         region_t        region = 0;
2355         int             failed_csum = 0;
2356
2357         blk = ext2fs_file_acl_block(fs, inode);
2358         if (blk == 0)
2359                 return 0;
2360
2361         /*
2362          * If the Extended attribute flag isn't set, then a non-zero
2363          * file acl means that the inode is corrupted.
2364          *
2365          * Or if the extended attribute block is an invalid block,
2366          * then the inode is also corrupted.
2367          */
2368         if (!ext2fs_has_feature_xattr(fs->super) ||
2369             (blk < fs->super->s_first_data_block) ||
2370             (blk >= ext2fs_blocks_count(fs->super))) {
2371                 mark_inode_bad(ctx, ino);
2372                 return 0;
2373         }
2374
2375         /* If ea bitmap hasn't been allocated, create it */
2376         if (!ctx->block_ea_map) {
2377                 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
2378                                         _("ext attr block map"),
2379                                         EXT2FS_BMAP64_RBTREE, "block_ea_map",
2380                                         &ctx->block_ea_map);
2381                 if (pctx->errcode) {
2382                         pctx->num = 2;
2383                         fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
2384                         ctx->flags |= E2F_FLAG_ABORT;
2385                         return 0;
2386                 }
2387         }
2388
2389         /* Create the EA refcount structure if necessary */
2390         if (!ctx->refcount) {
2391                 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
2392                 if (pctx->errcode) {
2393                         pctx->num = 1;
2394                         fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2395                         ctx->flags |= E2F_FLAG_ABORT;
2396                         return 0;
2397                 }
2398         }
2399
2400 #if 0
2401         /* Debugging text */
2402         printf("Inode %u has EA block %u\n", ino, blk);
2403 #endif
2404
2405         /* Have we seen this EA block before? */
2406         if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
2407                 if (ctx->ea_block_quota)
2408                         ea_refcount_fetch(ctx->ea_block_quota, blk,
2409                                           ea_block_quota_blocks);
2410                 else
2411                         *ea_block_quota_blocks = 0;
2412
2413                 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
2414                         return 1;
2415                 /* Ooops, this EA was referenced more than it stated */
2416                 if (!ctx->refcount_extra) {
2417                         pctx->errcode = ea_refcount_create(0,
2418                                            &ctx->refcount_extra);
2419                         if (pctx->errcode) {
2420                                 pctx->num = 2;
2421                                 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2422                                 ctx->flags |= E2F_FLAG_ABORT;
2423                                 return 0;
2424                         }
2425                 }
2426                 ea_refcount_increment(ctx->refcount_extra, blk, 0);
2427                 return 1;
2428         }
2429
2430         /*
2431          * OK, we haven't seen this EA block yet.  So we need to
2432          * validate it
2433          */
2434         pctx->blk = blk;
2435         pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
2436         if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
2437                 pctx->errcode = 0;
2438                 failed_csum = 1;
2439         } else if (pctx->errcode == EXT2_ET_BAD_EA_HEADER)
2440                 pctx->errcode = 0;
2441
2442         if (pctx->errcode &&
2443             fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) {
2444                 pctx->errcode = 0;
2445                 goto clear_extattr;
2446         }
2447         header = (struct ext2_ext_attr_header *) block_buf;
2448         pctx->blk = ext2fs_file_acl_block(fs, inode);
2449         if (((ctx->ext_attr_ver == 1) &&
2450              (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
2451             ((ctx->ext_attr_ver == 2) &&
2452              (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
2453                 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
2454                         goto clear_extattr;
2455         }
2456
2457         if (header->h_blocks != 1) {
2458                 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
2459                         goto clear_extattr;
2460         }
2461
2462         if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
2463                 goto clear_extattr;
2464
2465         region = region_create(0, fs->blocksize);
2466         if (!region) {
2467                 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
2468                 ctx->flags |= E2F_FLAG_ABORT;
2469                 return 0;
2470         }
2471         if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
2472                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2473                         goto clear_extattr;
2474         }
2475
2476         entry = (struct ext2_ext_attr_entry *)(header+1);
2477         end = block_buf + fs->blocksize;
2478         while ((char *)entry < end && *(__u32 *)entry) {
2479                 __u32 hash;
2480
2481                 if (region_allocate(region, (char *)entry - (char *)header,
2482                                    EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
2483                         if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2484                                 goto clear_extattr;
2485                         break;
2486                 }
2487                 if ((ctx->ext_attr_ver == 1 &&
2488                      (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
2489                     (ctx->ext_attr_ver == 2 &&
2490                      entry->e_name_index == 0)) {
2491                         if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
2492                                 goto clear_extattr;
2493                         break;
2494                 }
2495                 if (entry->e_value_inum == 0) {
2496                         if (entry->e_value_offs + entry->e_value_size >
2497                             fs->blocksize) {
2498                                 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
2499                                         goto clear_extattr;
2500                                 break;
2501                         }
2502                         if (entry->e_value_size &&
2503                             region_allocate(region, entry->e_value_offs,
2504                                             EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
2505                                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
2506                                                 pctx))
2507                                         goto clear_extattr;
2508                         }
2509
2510                         hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
2511                                                           entry->e_value_offs);
2512
2513                         if (entry->e_hash != hash) {
2514                                 pctx->num = entry->e_hash;
2515                                 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
2516                                         goto clear_extattr;
2517                                 entry->e_hash = hash;
2518                         }
2519                 } else {
2520                         problem_t problem;
2521                         blk64_t entry_quota_blocks;
2522
2523                         problem = check_large_ea_inode(ctx, entry, pctx,
2524                                                        &entry_quota_blocks);
2525                         if (problem == 0)
2526                                 mark_inode_ea_map(ctx, pctx,
2527                                                   entry->e_value_inum);
2528                         else if (fix_problem(ctx, problem, pctx))
2529                                 goto clear_extattr;
2530
2531                         quota_blocks += entry_quota_blocks;
2532                 }
2533
2534                 entry = EXT2_EXT_ATTR_NEXT(entry);
2535         }
2536         if (region_allocate(region, (char *)entry - (char *)header, 4)) {
2537                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2538                         goto clear_extattr;
2539         }
2540         region_free(region);
2541
2542         /*
2543          * We only get here if there was no other errors that were fixed.
2544          * If there was a checksum fail, ask to correct it.
2545          */
2546         if (failed_csum &&
2547             fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
2548                 pctx->errcode = ext2fs_write_ext_attr3(fs, blk, block_buf,
2549                                                        pctx->ino);
2550                 if (pctx->errcode)
2551                         return 0;
2552         }
2553
2554         *ea_block_quota_blocks = quota_blocks;
2555         if (quota_blocks) {
2556                 if (!ctx->ea_block_quota) {
2557                         pctx->errcode = ea_refcount_create(0,
2558                                                         &ctx->ea_block_quota);
2559                         if (pctx->errcode) {
2560                                 pctx->num = 3;
2561                                 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2562                                 ctx->flags |= E2F_FLAG_ABORT;
2563                                 return 0;
2564                         }
2565                 }
2566                 ea_refcount_store(ctx->ea_block_quota, blk, quota_blocks);
2567         }
2568         ea_refcount_store(ctx->refcount, blk, header->h_refcount - 1);
2569         mark_block_used(ctx, blk);
2570         ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
2571         return 1;
2572
2573 clear_extattr:
2574         if (region)
2575                 region_free(region);
2576         ext2fs_file_acl_block_set(fs, inode, 0);
2577         e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
2578         return 0;
2579 }
2580
2581 /* Returns 1 if bad htree, 0 if OK */
2582 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
2583                         ext2_ino_t ino, struct ext2_inode *inode,
2584                         char *block_buf)
2585 {
2586         struct ext2_dx_root_info        *root;
2587         ext2_filsys                     fs = ctx->fs;
2588         errcode_t                       retval;
2589         blk64_t                         blk;
2590
2591         if ((!LINUX_S_ISDIR(inode->i_mode) &&
2592              fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
2593             (!ext2fs_has_feature_dir_index(fs->super) &&
2594              fix_problem(ctx, PR_1_HTREE_SET, pctx)))
2595                 return 1;
2596
2597         pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
2598
2599         if ((pctx->errcode) ||
2600             (blk == 0) ||
2601             (blk < fs->super->s_first_data_block) ||
2602             (blk >= ext2fs_blocks_count(fs->super))) {
2603                 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2604                         return 1;
2605                 else
2606                         return 0;
2607         }
2608
2609         retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
2610         if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2611                 return 1;
2612
2613         /* XXX should check that beginning matches a directory */
2614         root = (struct ext2_dx_root_info *) (block_buf + 24);
2615
2616         if ((root->reserved_zero || root->info_length < 8) &&
2617             fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2618                 return 1;
2619
2620         pctx->num = root->hash_version;
2621         if ((root->hash_version != EXT2_HASH_LEGACY) &&
2622             (root->hash_version != EXT2_HASH_HALF_MD4) &&
2623             (root->hash_version != EXT2_HASH_TEA) &&
2624             fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
2625                 return 1;
2626
2627         if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
2628             fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
2629                 return 1;
2630
2631         pctx->num = root->indirect_levels;
2632         if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&
2633             fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
2634                 return 1;
2635
2636         return 0;
2637 }
2638
2639 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
2640                         struct ext2_inode *inode, int restart_flag,
2641                         const char *source)
2642 {
2643         inode->i_flags = 0;
2644         inode->i_links_count = 0;
2645         ext2fs_icount_store(ctx->inode_link_info, ino, 0);
2646         inode->i_dtime = ctx->now;
2647
2648         /*
2649          * If a special inode has such rotten block mappings that we
2650          * want to clear the whole inode, be sure to actually zap
2651          * the block maps because i_links_count isn't checked for
2652          * special inodes, and we'll end up right back here the next
2653          * time we run fsck.
2654          */
2655         if (ino < EXT2_FIRST_INODE(ctx->fs->super))
2656                 memset(inode->i_block, 0, sizeof(inode->i_block));
2657
2658         ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
2659         ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
2660         if (ctx->inode_reg_map)
2661                 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
2662         if (ctx->inode_bad_map)
2663                 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
2664
2665         /*
2666          * If the inode was partially accounted for before processing
2667          * was aborted, we need to restart the pass 1 scan.
2668          */
2669         ctx->flags |= restart_flag;
2670
2671         if (ino == EXT2_BAD_INO)
2672                 memset(inode, 0, sizeof(struct ext2_inode));
2673
2674         e2fsck_write_inode(ctx, ino, inode, source);
2675 }
2676
2677 /*
2678  * Use the multiple-blocks reclamation code to fix alignment problems in
2679  * a bigalloc filesystem.  We want a logical cluster to map to *only* one
2680  * physical cluster, and we want the block offsets within that cluster to
2681  * line up.
2682  */
2683 static int has_unaligned_cluster_map(e2fsck_t ctx,
2684                                      blk64_t last_pblk, blk64_t last_lblk,
2685                                      blk64_t pblk, blk64_t lblk)
2686 {
2687         blk64_t cluster_mask;
2688
2689         if (!ctx->fs->cluster_ratio_bits)
2690                 return 0;
2691         cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
2692
2693         /*
2694          * If the block in the logical cluster doesn't align with the block in
2695          * the physical cluster...
2696          */
2697         if ((lblk & cluster_mask) != (pblk & cluster_mask))
2698                 return 1;
2699
2700         /*
2701          * If we cross a physical cluster boundary within a logical cluster...
2702          */
2703         if (last_pblk && (lblk & cluster_mask) != 0 &&
2704             EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
2705             EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
2706                 return 1;
2707
2708         return 0;
2709 }
2710
2711 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
2712                              struct process_block_struct *pb,
2713                              blk64_t start_block, blk64_t end_block,
2714                              blk64_t eof_block,
2715                              ext2_extent_handle_t ehandle,
2716                              int try_repairs)
2717 {
2718         struct ext2fs_extent    extent;
2719         blk64_t                 blk, last_lblk;
2720         unsigned int            i, n;
2721         int                     is_dir, is_leaf;
2722         problem_t               problem;
2723         struct ext2_extent_info info;
2724         int                     failed_csum = 0;
2725
2726         if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID)
2727                 failed_csum = 1;
2728
2729         pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
2730         if (pctx->errcode)
2731                 return;
2732         if (!(ctx->options & E2F_OPT_FIXES_ONLY) &&
2733             !pb->eti.force_rebuild) {
2734                 struct extent_tree_level *etl;
2735
2736                 etl = pb->eti.ext_info + info.curr_level;
2737                 etl->num_extents += info.num_entries;
2738                 etl->max_extents += info.max_entries;
2739                 /*
2740                  * Implementation wart: Splitting extent blocks when appending
2741                  * will leave the old block with one free entry.  Therefore
2742                  * unless the node is totally full, pretend that a non-root
2743                  * extent block can hold one fewer entry than it actually does,
2744                  * so that we don't repeatedly rebuild the extent tree.
2745                  */
2746                 if (info.curr_level && info.num_entries < info.max_entries)
2747                         etl->max_extents--;
2748         }
2749
2750         pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
2751                                           &extent);
2752         while ((pctx->errcode == 0 ||
2753                 pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
2754                info.num_entries-- > 0) {
2755                 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
2756                 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
2757                 last_lblk = extent.e_lblk + extent.e_len - 1;
2758
2759                 problem = 0;
2760                 pctx->blk = extent.e_pblk;
2761                 pctx->blk2 = extent.e_lblk;
2762                 pctx->num = extent.e_len;
2763                 pctx->blkcount = extent.e_lblk + extent.e_len;
2764
2765                 if (extent.e_pblk == 0 ||
2766                     extent.e_pblk < ctx->fs->super->s_first_data_block ||
2767                     extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
2768                         problem = PR_1_EXTENT_BAD_START_BLK;
2769                 else if (extent.e_lblk < start_block)
2770                         problem = PR_1_OUT_OF_ORDER_EXTENTS;
2771                 else if ((end_block && last_lblk > end_block) &&
2772                          (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT &&
2773                                 last_lblk > eof_block)))
2774                         problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
2775                 else if (is_leaf && extent.e_len == 0)
2776                         problem = PR_1_EXTENT_LENGTH_ZERO;
2777                 else if (is_leaf &&
2778                          (extent.e_pblk + extent.e_len) >
2779                          ext2fs_blocks_count(ctx->fs->super))
2780                         problem = PR_1_EXTENT_ENDS_BEYOND;
2781                 else if (is_leaf && is_dir &&
2782                          ((extent.e_lblk + extent.e_len) >
2783                           (1U << (21 - ctx->fs->super->s_log_block_size))))
2784                         problem = PR_1_TOOBIG_DIR;
2785
2786                 if (is_leaf && problem == 0 && extent.e_len > 0 &&
2787                     region_allocate(pb->region, extent.e_lblk, extent.e_len))
2788                         problem = PR_1_EXTENT_COLLISION;
2789
2790                 /*
2791                  * Uninitialized blocks in a directory?  Clear the flag and
2792                  * we'll interpret the blocks later.
2793                  */
2794                 if (try_repairs && is_dir && problem == 0 &&
2795                     (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
2796                     fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
2797                         extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
2798                         pb->inode_modified = 1;
2799                         pctx->errcode = ext2fs_extent_replace(ehandle, 0,
2800                                                               &extent);
2801                         if (pctx->errcode)
2802                                 return;
2803                         failed_csum = 0;
2804                 }
2805
2806                 if (try_repairs && problem) {
2807 report_problem:
2808                         if (fix_problem(ctx, problem, pctx)) {
2809                                 if (ctx->invalid_bitmaps) {
2810                                         /*
2811                                          * If fsck knows the bitmaps are bad,
2812                                          * skip to the next extent and
2813                                          * try to clear this extent again
2814                                          * after fixing the bitmaps, by
2815                                          * restarting fsck.
2816                                          */
2817                                         pctx->errcode = ext2fs_extent_get(
2818                                                           ehandle,
2819                                                           EXT2_EXTENT_NEXT_SIB,
2820                                                           &extent);
2821                                         ctx->flags |= E2F_FLAG_RESTART_LATER;
2822                                         if (pctx->errcode ==
2823                                                     EXT2_ET_NO_CURRENT_NODE) {
2824                                                 pctx->errcode = 0;
2825                                                 break;
2826                                         }
2827                                         continue;
2828                                 }
2829                                 e2fsck_read_bitmaps(ctx);
2830                                 pb->inode_modified = 1;
2831                                 pctx->errcode =
2832                                         ext2fs_extent_delete(ehandle, 0);
2833                                 if (pctx->errcode) {
2834                                         pctx->str = "ext2fs_extent_delete";
2835                                         return;
2836                                 }
2837                                 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2838                                 if (pctx->errcode &&
2839                                     pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
2840                                         pctx->str = "ext2fs_extent_fix_parents";
2841                                         return;
2842                                 }
2843                                 pctx->errcode = ext2fs_extent_get(ehandle,
2844                                                                   EXT2_EXTENT_CURRENT,
2845                                                                   &extent);
2846                                 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
2847                                         pctx->errcode = 0;
2848                                         break;
2849                                 }
2850                                 failed_csum = 0;
2851                                 continue;
2852                         }
2853                         goto next;
2854                 }
2855
2856                 if (!is_leaf) {
2857                         blk64_t lblk = extent.e_lblk;
2858                         int next_try_repairs = 1;
2859
2860                         blk = extent.e_pblk;
2861
2862                         /*
2863                          * If this lower extent block collides with critical
2864                          * metadata, don't try to repair the damage.  Pass 1b
2865                          * will reallocate the block; then we can try again.
2866                          */
2867                         if (pb->ino != EXT2_RESIZE_INO &&
2868                             ext2fs_test_block_bitmap2(ctx->block_metadata_map,
2869                                                       extent.e_pblk)) {
2870                                 next_try_repairs = 0;
2871                                 pctx->blk = blk;
2872                                 fix_problem(ctx,
2873                                             PR_1_CRITICAL_METADATA_COLLISION,
2874                                             pctx);
2875                                 ctx->flags |= E2F_FLAG_RESTART_LATER;
2876                         }
2877                         pctx->errcode = ext2fs_extent_get(ehandle,
2878                                                   EXT2_EXTENT_DOWN, &extent);
2879                         if (pctx->errcode &&
2880                             pctx->errcode != EXT2_ET_EXTENT_CSUM_INVALID) {
2881                                 pctx->str = "EXT2_EXTENT_DOWN";
2882                                 problem = PR_1_EXTENT_HEADER_INVALID;
2883                                 if (!next_try_repairs)
2884                                         return;
2885                                 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
2886                                         goto report_problem;
2887                                 return;
2888                         }
2889                         /* The next extent should match this index's logical start */
2890                         if (extent.e_lblk != lblk) {
2891                                 struct ext2_extent_info e_info;
2892
2893                                 ext2fs_extent_get_info(ehandle, &e_info);
2894                                 pctx->blk = lblk;
2895                                 pctx->blk2 = extent.e_lblk;
2896                                 pctx->num = e_info.curr_level - 1;
2897                                 problem = PR_1_EXTENT_INDEX_START_INVALID;
2898                                 if (fix_problem(ctx, problem, pctx)) {
2899                                         pb->inode_modified = 1;
2900                                         pctx->errcode =
2901                                                 ext2fs_extent_fix_parents(ehandle);
2902                                         if (pctx->errcode) {
2903                                                 pctx->str = "ext2fs_extent_fix_parents";
2904                                                 return;
2905                                         }
2906                                 }
2907                         }
2908                         scan_extent_node(ctx, pctx, pb, extent.e_lblk,
2909                                          last_lblk, eof_block, ehandle,
2910                                          next_try_repairs);
2911                         if (pctx->errcode)
2912                                 return;
2913                         pctx->errcode = ext2fs_extent_get(ehandle,
2914                                                   EXT2_EXTENT_UP, &extent);
2915                         if (pctx->errcode) {
2916                                 pctx->str = "EXT2_EXTENT_UP";
2917                                 return;
2918                         }
2919                         mark_block_used(ctx, blk);
2920                         pb->num_blocks++;
2921                         goto next;
2922                 }
2923
2924                 if ((pb->previous_block != 0) &&
2925                     (pb->previous_block+1 != extent.e_pblk)) {
2926                         if (ctx->options & E2F_OPT_FRAGCHECK) {
2927                                 char type = '?';
2928
2929                                 if (pb->is_dir)
2930                                         type = 'd';
2931                                 else if (pb->is_reg)
2932                                         type = 'f';
2933
2934                                 printf(("%6lu(%c): expecting %6lu "
2935                                         "actual extent "
2936                                         "phys %6lu log %lu len %lu\n"),
2937                                        (unsigned long) pctx->ino, type,
2938                                        (unsigned long) pb->previous_block+1,
2939                                        (unsigned long) extent.e_pblk,
2940                                        (unsigned long) extent.e_lblk,
2941                                        (unsigned long) extent.e_len);
2942                         }
2943                         pb->fragmented = 1;
2944                 }
2945                 /*
2946                  * If we notice a gap in the logical block mappings of an
2947                  * extent-mapped directory, offer to close the hole by
2948                  * moving the logical block down, otherwise we'll go mad in
2949                  * pass 3 allocating empty directory blocks to fill the hole.
2950                  */
2951                 if (try_repairs && is_dir &&
2952                     pb->last_block + 1 < extent.e_lblk) {
2953                         blk64_t new_lblk;
2954
2955                         new_lblk = pb->last_block + 1;
2956                         if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
2957                                 new_lblk = ((new_lblk +
2958                                              EXT2FS_CLUSTER_RATIO(ctx->fs) - 1) &
2959                                             ~EXT2FS_CLUSTER_MASK(ctx->fs)) |
2960                                            (extent.e_pblk &
2961                                             EXT2FS_CLUSTER_MASK(ctx->fs));
2962                         pctx->blk = extent.e_lblk;
2963                         pctx->blk2 = new_lblk;
2964                         if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
2965                                 extent.e_lblk = new_lblk;
2966                                 pb->inode_modified = 1;
2967                                 pctx->errcode = ext2fs_extent_replace(ehandle,
2968                                                                 0, &extent);
2969                                 if (pctx->errcode) {
2970                                         pctx->errcode = 0;
2971                                         goto alloc_later;
2972                                 }
2973                                 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2974                                 if (pctx->errcode)
2975                                         goto failed_add_dir_block;
2976                                 pctx->errcode = ext2fs_extent_goto(ehandle,
2977                                                                 extent.e_lblk);
2978                                 if (pctx->errcode)
2979                                         goto failed_add_dir_block;
2980                                 last_lblk = extent.e_lblk + extent.e_len - 1;
2981                                 failed_csum = 0;
2982                         }
2983                 }
2984 alloc_later:
2985                 if (is_dir) {
2986                         while (++pb->last_db_block <
2987                                (e2_blkcnt_t) extent.e_lblk) {
2988                                 pctx->errcode = ext2fs_add_dir_block2(
2989                                                         ctx->fs->dblist,
2990                                                         pb->ino, 0,
2991                                                         pb->last_db_block);
2992                                 if (pctx->errcode) {
2993                                         pctx->blk = 0;
2994                                         pctx->num = pb->last_db_block;
2995                                         goto failed_add_dir_block;
2996                                 }
2997                         }
2998
2999                         for (i = 0; i < extent.e_len; i++) {
3000                                 pctx->errcode = ext2fs_add_dir_block2(
3001                                                         ctx->fs->dblist,
3002                                                         pctx->ino,
3003                                                         extent.e_pblk + i,
3004                                                         extent.e_lblk + i);
3005                                 if (pctx->errcode) {
3006                                         pctx->blk = extent.e_pblk + i;
3007                                         pctx->num = extent.e_lblk + i;
3008                                 failed_add_dir_block:
3009                                         fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3010                                         /* Should never get here */
3011                                         ctx->flags |= E2F_FLAG_ABORT;
3012                                         return;
3013                                 }
3014                         }
3015                         if (extent.e_len > 0)
3016                                 pb->last_db_block = extent.e_lblk + extent.e_len - 1;
3017                 }
3018                 if (has_unaligned_cluster_map(ctx, pb->previous_block,
3019                                               pb->last_block,
3020                                               extent.e_pblk,
3021                                               extent.e_lblk)) {
3022                         for (i = 0; i < extent.e_len; i++) {
3023                                 pctx->blk = extent.e_lblk + i;
3024                                 pctx->blk2 = extent.e_pblk + i;
3025                                 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
3026                                 mark_block_used(ctx, extent.e_pblk + i);
3027                                 mark_block_used(ctx, extent.e_pblk + i);
3028                         }
3029                 }
3030
3031                 /*
3032                  * Check whether first cluster got marked in previous iteration.
3033                  */
3034                 if (ctx->fs->cluster_ratio_bits &&
3035                     pb->previous_block &&
3036                     (EXT2FS_B2C(ctx->fs, extent.e_pblk) ==
3037                      EXT2FS_B2C(ctx->fs, pb->previous_block)))
3038                         /* Set blk to the beginning of next cluster. */
3039                         blk = EXT2FS_C2B(
3040                                 ctx->fs,
3041                                 EXT2FS_B2C(ctx->fs, extent.e_pblk) + 1);
3042                 else
3043                         /* Set blk to the beginning of current cluster. */
3044                         blk = EXT2FS_C2B(ctx->fs,
3045                                          EXT2FS_B2C(ctx->fs, extent.e_pblk));
3046
3047                 if (blk < extent.e_pblk + extent.e_len) {
3048                         mark_blocks_used(ctx, blk,
3049                                          extent.e_pblk + extent.e_len - blk);
3050                         n = DIV_ROUND_UP(extent.e_pblk + extent.e_len - blk,
3051                                          EXT2FS_CLUSTER_RATIO(ctx->fs));
3052                         pb->num_blocks += n;
3053                 }
3054                 pb->last_block = extent.e_lblk + extent.e_len - 1;
3055                 pb->previous_block = extent.e_pblk + extent.e_len - 1;
3056                 start_block = pb->last_block = last_lblk;
3057                 if (is_leaf && !is_dir &&
3058                     !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
3059                         pb->last_init_lblock = last_lblk;
3060         next:
3061                 pctx->errcode = ext2fs_extent_get(ehandle,
3062                                                   EXT2_EXTENT_NEXT_SIB,
3063                                                   &extent);
3064         }
3065
3066         /* Failed csum but passes checks?  Ask to fix checksum. */
3067         if (failed_csum &&
3068             fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID, pctx)) {
3069                 pb->inode_modified = 1;
3070                 pctx->errcode = ext2fs_extent_replace(ehandle, 0, &extent);
3071                 if (pctx->errcode)
3072                         return;
3073         }
3074
3075         if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
3076                 pctx->errcode = 0;
3077 }
3078
3079 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
3080                                  struct process_block_struct *pb)
3081 {
3082         struct ext2_extent_info info;
3083         struct ext2_inode       *inode = pctx->inode;
3084         ext2_extent_handle_t    ehandle;
3085         ext2_filsys             fs = ctx->fs;
3086         ext2_ino_t              ino = pctx->ino;
3087         errcode_t               retval;
3088         blk64_t                 eof_lblk;
3089         struct ext3_extent_header       *eh;
3090
3091         /* Check for a proper extent header... */
3092         eh = (struct ext3_extent_header *) &inode->i_block[0];
3093         retval = ext2fs_extent_header_verify(eh, sizeof(inode->i_block));
3094         if (retval) {
3095                 if (fix_problem(ctx, PR_1_MISSING_EXTENT_HEADER, pctx))
3096                         e2fsck_clear_inode(ctx, ino, inode, 0,
3097                                            "check_blocks_extents");
3098                 pctx->errcode = 0;
3099                 return;
3100         }
3101
3102         /* ...since this function doesn't fail if i_block is zeroed. */
3103         pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
3104         if (pctx->errcode) {
3105                 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
3106                         e2fsck_clear_inode(ctx, ino, inode, 0,
3107                                            "check_blocks_extents");
3108                 pctx->errcode = 0;
3109                 return;
3110         }
3111
3112         retval = ext2fs_extent_get_info(ehandle, &info);
3113         if (retval == 0) {
3114                 int max_depth = info.max_depth;
3115
3116                 if (max_depth >= MAX_EXTENT_DEPTH_COUNT)
3117                         max_depth = MAX_EXTENT_DEPTH_COUNT-1;
3118                 ctx->extent_depth_count[max_depth]++;
3119         }
3120
3121         /* Check maximum extent depth */
3122         pctx->blk = info.max_depth;
3123         pctx->blk2 = ext2fs_max_extent_depth(ehandle);
3124         if (pctx->blk2 < pctx->blk &&
3125             fix_problem(ctx, PR_1_EXTENT_BAD_MAX_DEPTH, pctx))
3126                 pb->eti.force_rebuild = 1;
3127
3128         /* Can we collect extent tree level stats? */
3129         pctx->blk = MAX_EXTENT_DEPTH_COUNT;
3130         if (pctx->blk2 > pctx->blk)
3131                 fix_problem(ctx, PR_1E_MAX_EXTENT_TREE_DEPTH, pctx);
3132         memset(pb->eti.ext_info, 0, sizeof(pb->eti.ext_info));
3133         pb->eti.ino = pb->ino;
3134
3135         pb->region = region_create(0, info.max_lblk);
3136         if (!pb->region) {
3137                 ext2fs_extent_free(ehandle);
3138                 fix_problem(ctx, PR_1_EXTENT_ALLOC_REGION_ABORT, pctx);
3139                 ctx->flags |= E2F_FLAG_ABORT;
3140                 return;
3141         }
3142
3143         eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
3144                 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
3145         scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle, 1);
3146         if (pctx->errcode &&
3147             fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
3148                 pb->num_blocks = 0;
3149                 inode->i_blocks = 0;
3150                 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
3151                                    "check_blocks_extents");
3152                 pctx->errcode = 0;
3153         }
3154         region_free(pb->region);
3155         pb->region = NULL;
3156         ext2fs_extent_free(ehandle);
3157
3158         /* Rebuild unless it's a dir and we're rehashing it */
3159         if (LINUX_S_ISDIR(inode->i_mode) &&
3160             e2fsck_dir_will_be_rehashed(ctx, ino))
3161                 return;
3162
3163         if (ctx->options & E2F_OPT_CONVERT_BMAP)
3164                 e2fsck_rebuild_extents_later(ctx, ino);
3165         else
3166                 e2fsck_should_rebuild_extents(ctx, pctx, &pb->eti, &info);
3167 }
3168
3169 /*
3170  * In fact we don't need to check blocks for an inode with inline data
3171  * because this inode doesn't have any blocks.  In this function all
3172  * we need to do is add this inode into dblist when it is a directory.
3173  */
3174 static void check_blocks_inline_data(e2fsck_t ctx, struct problem_context *pctx,
3175                                      struct process_block_struct *pb)
3176 {
3177         int     flags;
3178         size_t  inline_data_size = 0;
3179
3180         if (!pb->is_dir) {
3181                 pctx->errcode = 0;
3182                 return;
3183         }
3184
3185         /* Process the dirents in i_block[] as the "first" block. */
3186         pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 0);
3187         if (pctx->errcode)
3188                 goto err;
3189
3190         /* Process the dirents in the EA as a "second" block. */
3191         flags = ctx->fs->flags;
3192         ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3193         pctx->errcode = ext2fs_inline_data_size(ctx->fs, pb->ino,
3194                                                 &inline_data_size);
3195         ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3196                          (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3197         if (pctx->errcode) {
3198                 pctx->errcode = 0;
3199                 return;
3200         }
3201
3202         if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE)
3203                 return;
3204
3205         pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 1);
3206         if (pctx->errcode)
3207                 goto err;
3208
3209         return;
3210 err:
3211         pctx->blk = 0;
3212         pctx->num = 0;
3213         fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3214         ctx->flags |= E2F_FLAG_ABORT;
3215 }
3216
3217 /*
3218  * This subroutine is called on each inode to account for all of the
3219  * blocks used by that inode.
3220  */
3221 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
3222                          char *block_buf, blk64_t ea_ibody_quota_blocks)
3223 {
3224         ext2_filsys fs = ctx->fs;
3225         struct process_block_struct pb;
3226         ext2_ino_t      ino = pctx->ino;
3227         struct ext2_inode *inode = pctx->inode;
3228         unsigned        bad_size = 0;
3229         int             dirty_inode = 0;
3230         int             extent_fs;
3231         int             inlinedata_fs;
3232         __u64           size;
3233         blk64_t         ea_block_quota_blocks = 0;
3234
3235         pb.ino = ino;
3236         pb.num_blocks = EXT2FS_B2C(ctx->fs, ea_ibody_quota_blocks);
3237         pb.last_block = ~0;
3238         pb.last_init_lblock = -1;
3239         pb.last_db_block = -1;
3240         pb.num_illegal_blocks = 0;
3241         pb.suppress = 0; pb.clear = 0;
3242         pb.fragmented = 0;
3243         pb.compressed = 0;
3244         pb.previous_block = 0;
3245         pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
3246         pb.is_reg = LINUX_S_ISREG(inode->i_mode);
3247         pb.max_blocks = 1U << (31 - fs->super->s_log_block_size);
3248         pb.inode = inode;
3249         pb.pctx = pctx;
3250         pb.ctx = ctx;
3251         pb.inode_modified = 0;
3252         pb.eti.force_rebuild = 0;
3253         pctx->ino = ino;
3254         pctx->errcode = 0;
3255
3256         extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
3257         inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
3258
3259         if (check_ext_attr(ctx, pctx, block_buf, &ea_block_quota_blocks)) {
3260                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3261                         goto out;
3262                 pb.num_blocks += EXT2FS_B2C(ctx->fs, ea_block_quota_blocks);
3263         }
3264
3265         if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL))
3266                 check_blocks_inline_data(ctx, pctx, &pb);
3267         else if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
3268                 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
3269                         check_blocks_extents(ctx, pctx, &pb);
3270                 else {
3271                         int flags;
3272                         /*
3273                          * If we've modified the inode, write it out before
3274                          * iterate() tries to use it.
3275                          */
3276                         if (dirty_inode) {
3277                                 e2fsck_write_inode(ctx, ino, inode,
3278                                                    "check_blocks");
3279                                 dirty_inode = 0;
3280                         }
3281                         flags = fs->flags;
3282                         fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3283                         pctx->errcode = ext2fs_block_iterate3(fs, ino,
3284                                                 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
3285                                                 block_buf, process_block, &pb);
3286                         /*
3287                          * We do not have uninitialized extents in non extent
3288                          * files.
3289                          */
3290                         pb.last_init_lblock = pb.last_block;
3291                         /*
3292                          * If iterate() changed a block mapping, we have to
3293                          * re-read the inode.  If we decide to clear the
3294                          * inode after clearing some stuff, we'll re-write the
3295                          * bad mappings into the inode!
3296                          */
3297                         if (pb.inode_modified)
3298                                 e2fsck_read_inode(ctx, ino, inode,
3299                                                   "check_blocks");
3300                         fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3301                                     (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3302
3303                         if (ctx->options & E2F_OPT_CONVERT_BMAP) {
3304 #ifdef DEBUG
3305                                 printf("bmap rebuild ino=%d\n", ino);
3306 #endif
3307                                 if (!LINUX_S_ISDIR(inode->i_mode) ||
3308                                     !e2fsck_dir_will_be_rehashed(ctx, ino))
3309                                         e2fsck_rebuild_extents_later(ctx, ino);
3310                         }
3311                 }
3312         }
3313         end_problem_latch(ctx, PR_LATCH_BLOCK);
3314         end_problem_latch(ctx, PR_LATCH_TOOBIG);
3315         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3316                 goto out;
3317         if (pctx->errcode)
3318                 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
3319
3320         if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
3321                 if (LINUX_S_ISDIR(inode->i_mode))
3322                         ctx->fs_fragmented_dir++;
3323                 else
3324                         ctx->fs_fragmented++;
3325         }
3326
3327         if (pb.clear) {
3328                 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
3329                                    "check_blocks");
3330                 return;
3331         }
3332
3333         if (inode->i_flags & EXT2_INDEX_FL) {
3334                 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
3335                         inode->i_flags &= ~EXT2_INDEX_FL;
3336                         dirty_inode++;
3337                 } else {
3338                         e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
3339                 }
3340         }
3341
3342         if (!pb.num_blocks && pb.is_dir &&
3343             !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
3344                 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
3345                         e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
3346                         ctx->fs_directory_count--;
3347                         return;
3348                 }
3349         }
3350
3351         if (ino != quota_type2inum(PRJQUOTA, fs->super) &&
3352             (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super))) {
3353                 quota_data_add(ctx->qctx, (struct ext2_inode_large *) inode,
3354                                ino,
3355                                pb.num_blocks * EXT2_CLUSTER_SIZE(fs->super));
3356                 quota_data_inodes(ctx->qctx, (struct ext2_inode_large *) inode,
3357                                   ino, +1);
3358         }
3359
3360         if (!ext2fs_has_feature_huge_file(fs->super) ||
3361             !(inode->i_flags & EXT4_HUGE_FILE_FL))
3362                 pb.num_blocks *= (fs->blocksize / 512);
3363         pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
3364 #if 0
3365         printf("inode %u, i_size = %u, last_block = %llu, i_blocks=%llu, num_blocks = %llu\n",
3366                ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
3367                pb.num_blocks);
3368 #endif
3369         if (pb.is_dir) {
3370                 unsigned nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
3371                 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
3372                         int flags;
3373                         size_t sz = 0;
3374                         errcode_t err;
3375
3376                         flags = ctx->fs->flags;
3377                         ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3378                         err = ext2fs_inline_data_size(ctx->fs, pctx->ino,
3379                                                       &sz);
3380                         ctx->fs->flags = (flags &
3381                                           EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3382                                          (ctx->fs->flags &
3383                                           ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3384                         if (err || sz != inode->i_size) {
3385                                 bad_size = 7;
3386                                 pctx->num = sz;
3387                         }
3388                 } else if (inode->i_size & (fs->blocksize - 1))
3389                         bad_size = 5;
3390                 else if (nblock > (pb.last_block + 1))
3391                         bad_size = 1;
3392                 else if (nblock < (pb.last_block + 1)) {
3393                         if (((pb.last_block + 1) - nblock) >
3394                             fs->super->s_prealloc_dir_blocks)
3395                                 bad_size = 2;
3396                 }
3397         } else {
3398                 e2_blkcnt_t blkpg = ctx->blocks_per_page;
3399
3400                 size = EXT2_I_SIZE(inode);
3401                 if ((pb.last_init_lblock >= 0) &&
3402                     /* allow allocated blocks to end of PAGE_SIZE */
3403                     (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
3404                     (pb.last_init_lblock / blkpg * blkpg != pb.last_init_lblock ||
3405                      size < (__u64)(pb.last_init_lblock & ~(blkpg-1)) *
3406                      fs->blocksize))
3407                         bad_size = 3;
3408                 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
3409                          size > ext2_max_sizes[fs->super->s_log_block_size])
3410                         /* too big for a direct/indirect-mapped file */
3411                         bad_size = 4;
3412                 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
3413                          size >
3414                          ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
3415                         /* too big for an extent-based file - 32bit ee_block */
3416                         bad_size = 6;
3417         }
3418         /* i_size for symlinks is checked elsewhere */
3419         if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
3420                 /* Did inline_data set pctx->num earlier? */
3421                 if (bad_size != 7)
3422                         pctx->num = (pb.last_block + 1) * fs->blocksize;
3423                 pctx->group = bad_size;
3424                 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
3425                         if (LINUX_S_ISDIR(inode->i_mode))
3426                                 pctx->num &= 0xFFFFFFFFULL;
3427                         ext2fs_inode_size_set(fs, inode, pctx->num);
3428                         if (EXT2_I_SIZE(inode) == 0 &&
3429                             (inode->i_flags & EXT4_INLINE_DATA_FL)) {
3430                                 memset(inode->i_block, 0,
3431                                        sizeof(inode->i_block));
3432                                 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
3433                         }
3434                         dirty_inode++;
3435                 }
3436                 pctx->num = 0;
3437         }
3438         if (LINUX_S_ISREG(inode->i_mode) &&
3439             ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
3440                 ctx->large_files++;
3441         if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
3442             ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
3443              (ext2fs_has_feature_huge_file(fs->super) &&
3444               (inode->i_flags & EXT4_HUGE_FILE_FL) &&
3445               (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
3446                 pctx->num = pb.num_blocks;
3447                 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
3448                         inode->i_blocks = pb.num_blocks;
3449                         inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
3450                         dirty_inode++;
3451                 }
3452                 pctx->num = 0;
3453         }
3454
3455         /*
3456          * The kernel gets mad if we ask it to allocate bigalloc clusters to
3457          * a block mapped file, so rebuild it as an extent file.  We can skip
3458          * symlinks because they're never rewritten.
3459          */
3460         if (ext2fs_has_feature_bigalloc(fs->super) &&
3461             (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode)) &&
3462             ext2fs_inode_data_blocks2(fs, inode) > 0 &&
3463             (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INO(fs->super)) &&
3464             !(inode->i_flags & (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)) &&
3465             fix_problem(ctx, PR_1_NO_BIGALLOC_BLOCKMAP_FILES, pctx)) {
3466                 pctx->errcode = e2fsck_rebuild_extents_later(ctx, ino);
3467                 if (pctx->errcode)
3468                         goto out;
3469         }
3470
3471         if (ctx->dirs_to_hash && pb.is_dir &&
3472             !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
3473             !(inode->i_flags & EXT2_INDEX_FL) &&
3474             ((inode->i_size / fs->blocksize) >= 3))
3475                 e2fsck_rehash_dir_later(ctx, ino);
3476
3477 out:
3478         if (dirty_inode)
3479                 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
3480 }
3481
3482 #if 0
3483 /*
3484  * Helper function called by process block when an illegal block is
3485  * found.  It returns a description about why the block is illegal
3486  */
3487 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
3488 {
3489         blk64_t super;
3490         int     i;
3491         static char     problem[80];
3492
3493         super = fs->super->s_first_data_block;
3494         strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
3495         if (block < super) {
3496                 sprintf(problem, "< FIRSTBLOCK (%u)", super);
3497                 return(problem);
3498         } else if (block >= ext2fs_blocks_count(fs->super)) {
3499                 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
3500                 return(problem);
3501         }
3502         for (i = 0; i < fs->group_desc_count; i++) {
3503                 if (block == super) {
3504                         sprintf(problem, "is the superblock in group %d", i);
3505                         break;
3506                 }
3507                 if (block > super &&
3508                     block <= (super + fs->desc_blocks)) {
3509                         sprintf(problem, "is in the group descriptors "
3510                                 "of group %d", i);
3511                         break;
3512                 }
3513                 if (block == ext2fs_block_bitmap_loc(fs, i)) {
3514                         sprintf(problem, "is the block bitmap of group %d", i);
3515                         break;
3516                 }
3517                 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
3518                         sprintf(problem, "is the inode bitmap of group %d", i);
3519                         break;
3520                 }
3521                 if (block >= ext2fs_inode_table_loc(fs, i) &&
3522                     (block < ext2fs_inode_table_loc(fs, i)
3523                      + fs->inode_blocks_per_group)) {
3524                         sprintf(problem, "is in the inode table of group %d",
3525                                 i);
3526                         break;
3527                 }
3528                 super += fs->super->s_blocks_per_group;
3529         }
3530         return(problem);
3531 }
3532 #endif
3533
3534 /*
3535  * This is a helper function for check_blocks().
3536  */
3537 static int process_block(ext2_filsys fs,
3538                   blk64_t       *block_nr,
3539                   e2_blkcnt_t blockcnt,
3540                   blk64_t ref_block EXT2FS_ATTR((unused)),
3541                   int ref_offset EXT2FS_ATTR((unused)),
3542                   void *priv_data)
3543 {
3544         struct process_block_struct *p;
3545         struct problem_context *pctx;
3546         blk64_t blk = *block_nr;
3547         int     ret_code = 0;
3548         problem_t       problem = 0;
3549         e2fsck_t        ctx;
3550
3551         p = (struct process_block_struct *) priv_data;
3552         pctx = p->pctx;
3553         ctx = p->ctx;
3554
3555         /*
3556          * For a directory, add logical block zero for processing even if it's
3557          * not mapped or we'll be perennially stuck with broken "." and ".."
3558          * entries.
3559          */
3560         if (p->is_dir && blockcnt == 0 && blk == 0) {
3561                 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
3562                 if (pctx->errcode) {
3563                         pctx->blk = blk;
3564                         pctx->num = blockcnt;
3565                         goto failed_add_dir_block;
3566                 }
3567                 p->last_db_block++;
3568         }
3569
3570         if (blk == 0)
3571                 return 0;
3572
3573 #if 0
3574         printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
3575                blockcnt);
3576 #endif
3577
3578         /*
3579          * Simplistic fragmentation check.  We merely require that the
3580          * file be contiguous.  (Which can never be true for really
3581          * big files that are greater than a block group.)
3582          */
3583         if (p->previous_block && p->ino != EXT2_RESIZE_INO) {
3584                 if (p->previous_block+1 != blk) {
3585                         if (ctx->options & E2F_OPT_FRAGCHECK) {
3586                                 char type = '?';
3587
3588                                 if (p->is_dir)
3589                                         type = 'd';
3590                                 else if (p->is_reg)
3591                                         type = 'f';
3592
3593                                 printf(_("%6lu(%c): expecting %6lu "
3594                                          "got phys %6lu (blkcnt %lld)\n"),
3595                                        (unsigned long) pctx->ino, type,
3596                                        (unsigned long) p->previous_block+1,
3597                                        (unsigned long) blk,
3598                                        blockcnt);
3599                         }
3600                         p->fragmented = 1;
3601                 }
3602         }
3603
3604         if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
3605                 problem = PR_1_TOOBIG_DIR;
3606         if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
3607                 problem = PR_1_TOOBIG_REG;
3608         if (!p->is_dir && !p->is_reg && blockcnt > 0)
3609                 problem = PR_1_TOOBIG_SYMLINK;
3610
3611         if (blk < fs->super->s_first_data_block ||
3612             blk >= ext2fs_blocks_count(fs->super))
3613                 problem = PR_1_ILLEGAL_BLOCK_NUM;
3614
3615         /*
3616          * If this IND/DIND/TIND block is squatting atop some critical metadata
3617          * (group descriptors, superblock, bitmap, inode table), any write to
3618          * "fix" mapping problems will destroy the metadata.  We'll let pass 1b
3619          * fix that and restart fsck.
3620          */
3621         if (blockcnt < 0 &&
3622             p->ino != EXT2_RESIZE_INO &&
3623             ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk)) {
3624                 pctx->blk = blk;
3625                 fix_problem(ctx, PR_1_CRITICAL_METADATA_COLLISION, pctx);
3626                 ctx->flags |= E2F_FLAG_RESTART_LATER;
3627         }
3628
3629         if (problem) {
3630                 p->num_illegal_blocks++;
3631                 /*
3632                  * A bit of subterfuge here -- we're trying to fix a block
3633                  * mapping, but the IND/DIND/TIND block could have collided
3634                  * with some critical metadata.  So, fix the in-core mapping so
3635                  * iterate won't go insane, but return 0 instead of
3636                  * BLOCK_CHANGED so that it won't write the remapping out to
3637                  * our multiply linked block.
3638                  *
3639                  * Even if we previously determined that an *IND block
3640                  * conflicts with critical metadata, we must still try to
3641                  * iterate the *IND block as if it is an *IND block to find and
3642                  * mark the blocks it points to.  Better to be overly cautious
3643                  * with the used_blocks map so that we don't move the *IND
3644                  * block to a block that's really in use!
3645                  */
3646                 if (p->ino != EXT2_RESIZE_INO &&
3647                     ref_block != 0 &&
3648                     ext2fs_test_block_bitmap2(ctx->block_metadata_map,
3649                                               ref_block)) {
3650                         *block_nr = 0;
3651                         return 0;
3652                 }
3653                 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
3654                         if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
3655                                 p->clear = 1;
3656                                 return BLOCK_ABORT;
3657                         }
3658                         if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
3659                                 p->suppress = 1;
3660                                 set_latch_flags(PR_LATCH_BLOCK,
3661                                                 PRL_SUPPRESS, 0);
3662                         }
3663                 }
3664                 pctx->blk = blk;
3665                 pctx->blkcount = blockcnt;
3666                 if (fix_problem(ctx, problem, pctx)) {
3667                         blk = *block_nr = 0;
3668                         ret_code = BLOCK_CHANGED;
3669                         p->inode_modified = 1;
3670                         /*
3671                          * If the directory block is too big and is beyond the
3672                          * end of the FS, don't bother trying to add it for
3673                          * processing -- the kernel would never have created a
3674                          * directory this large, and we risk an ENOMEM abort.
3675                          * In any case, the toobig handler for extent-based
3676                          * directories also doesn't feed toobig blocks to
3677                          * pass 2.
3678                          */
3679                         if (problem == PR_1_TOOBIG_DIR)
3680                                 return ret_code;
3681                         goto mark_dir;
3682                 } else
3683                         return 0;
3684         }
3685
3686         if (p->ino == EXT2_RESIZE_INO) {
3687                 /*
3688                  * The resize inode has already be sanity checked
3689                  * during pass #0 (the superblock checks).  All we
3690                  * have to do is mark the double indirect block as
3691                  * being in use; all of the other blocks are handled
3692                  * by mark_table_blocks()).
3693                  */
3694                 if (blockcnt == BLOCK_COUNT_DIND)
3695                         mark_block_used(ctx, blk);
3696                 p->num_blocks++;
3697         } else if (!(ctx->fs->cluster_ratio_bits &&
3698                      p->previous_block &&
3699                      (EXT2FS_B2C(ctx->fs, blk) ==
3700                       EXT2FS_B2C(ctx->fs, p->previous_block)) &&
3701                      (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
3702                      ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
3703                 mark_block_used(ctx, blk);
3704                 p->num_blocks++;
3705         } else if (has_unaligned_cluster_map(ctx, p->previous_block,
3706                                              p->last_block, blk, blockcnt)) {
3707                 pctx->blk = blockcnt;
3708                 pctx->blk2 = blk;
3709                 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
3710                 mark_block_used(ctx, blk);
3711                 mark_block_used(ctx, blk);
3712         }
3713         if (blockcnt >= 0)
3714                 p->last_block = blockcnt;
3715         p->previous_block = blk;
3716 mark_dir:
3717         if (p->is_dir && (blockcnt >= 0)) {
3718                 while (++p->last_db_block < blockcnt) {
3719                         pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
3720                                                               p->ino, 0,
3721                                                               p->last_db_block);
3722                         if (pctx->errcode) {
3723                                 pctx->blk = 0;
3724                                 pctx->num = p->last_db_block;
3725                                 goto failed_add_dir_block;
3726                         }
3727                 }
3728                 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
3729                                                       blk, blockcnt);
3730                 if (pctx->errcode) {
3731                         pctx->blk = blk;
3732                         pctx->num = blockcnt;
3733                 failed_add_dir_block:
3734                         fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3735                         /* Should never get here */
3736                         ctx->flags |= E2F_FLAG_ABORT;
3737                         return BLOCK_ABORT;
3738                 }
3739         }
3740         return ret_code;
3741 }
3742
3743 static int process_bad_block(ext2_filsys fs,
3744                       blk64_t *block_nr,
3745                       e2_blkcnt_t blockcnt,
3746                       blk64_t ref_block EXT2FS_ATTR((unused)),
3747                       int ref_offset EXT2FS_ATTR((unused)),
3748                       void *priv_data)
3749 {
3750         struct process_block_struct *p;
3751         blk64_t         blk = *block_nr;
3752         blk64_t         first_block;
3753         dgrp_t          i;
3754         struct problem_context *pctx;
3755         e2fsck_t        ctx;
3756
3757         if (!blk)
3758                 return 0;
3759
3760         p = (struct process_block_struct *) priv_data;
3761         ctx = p->ctx;
3762         pctx = p->pctx;
3763
3764         pctx->ino = EXT2_BAD_INO;
3765         pctx->blk = blk;
3766         pctx->blkcount = blockcnt;
3767
3768         if ((blk < fs->super->s_first_data_block) ||
3769             (blk >= ext2fs_blocks_count(fs->super))) {
3770                 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
3771                         *block_nr = 0;
3772                         return BLOCK_CHANGED;
3773                 } else
3774                         return 0;
3775         }
3776
3777         if (blockcnt < 0) {
3778                 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
3779                         p->bbcheck = 1;
3780                         if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
3781                                 *block_nr = 0;
3782                                 return BLOCK_CHANGED;
3783                         }
3784                 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3785                                                     blk)) {
3786                         p->bbcheck = 1;
3787                         if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
3788                                         pctx)) {
3789                                 *block_nr = 0;
3790                                 return BLOCK_CHANGED;
3791                         }
3792                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3793                                 return BLOCK_ABORT;
3794                 } else
3795                         mark_block_used(ctx, blk);
3796                 return 0;
3797         }
3798 #if 0
3799         printf ("DEBUG: Marking %u as bad.\n", blk);
3800 #endif
3801         ctx->fs_badblocks_count++;
3802         /*
3803          * If the block is not used, then mark it as used and return.
3804          * If it is already marked as found, this must mean that
3805          * there's an overlap between the filesystem table blocks
3806          * (bitmaps and inode table) and the bad block list.
3807          */
3808         if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
3809                 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
3810                 return 0;
3811         }
3812         /*
3813          * Try to find the where the filesystem block was used...
3814          */
3815         first_block = fs->super->s_first_data_block;
3816
3817         for (i = 0; i < fs->group_desc_count; i++ ) {
3818                 pctx->group = i;
3819                 pctx->blk = blk;
3820                 if (!ext2fs_bg_has_super(fs, i))
3821                         goto skip_super;
3822                 if (blk == first_block) {
3823                         if (i == 0) {
3824                                 if (fix_problem(ctx,
3825                                                 PR_1_BAD_PRIMARY_SUPERBLOCK,
3826                                                 pctx)) {
3827                                         *block_nr = 0;
3828                                         return BLOCK_CHANGED;
3829                                 }
3830                                 return 0;
3831                         }
3832                         fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
3833                         return 0;
3834                 }
3835                 if ((blk > first_block) &&
3836                     (blk <= first_block + fs->desc_blocks)) {
3837                         if (i == 0) {
3838                                 pctx->blk = *block_nr;
3839                                 if (fix_problem(ctx,
3840                         PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
3841                                         *block_nr = 0;
3842                                         return BLOCK_CHANGED;
3843                                 }
3844                                 return 0;
3845                         }
3846                         fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
3847                         return 0;
3848                 }
3849         skip_super:
3850                 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
3851                         if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
3852                                 ctx->invalid_block_bitmap_flag[i]++;
3853                                 ctx->invalid_bitmaps++;
3854                         }
3855                         return 0;
3856                 }
3857                 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
3858                         if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
3859                                 ctx->invalid_inode_bitmap_flag[i]++;
3860                                 ctx->invalid_bitmaps++;
3861                         }
3862                         return 0;
3863                 }
3864                 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
3865                     (blk < (ext2fs_inode_table_loc(fs, i) +
3866                             fs->inode_blocks_per_group))) {
3867                         /*
3868                          * If there are bad blocks in the inode table,
3869                          * the inode scan code will try to do
3870                          * something reasonable automatically.
3871                          */
3872                         return 0;
3873                 }
3874                 first_block += fs->super->s_blocks_per_group;
3875         }
3876         /*
3877          * If we've gotten to this point, then the only
3878          * possibility is that the bad block inode meta data
3879          * is using a bad block.
3880          */
3881         if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
3882             (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
3883             (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
3884                 p->bbcheck = 1;
3885                 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
3886                         *block_nr = 0;
3887                         return BLOCK_CHANGED;
3888                 }
3889                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3890                         return BLOCK_ABORT;
3891                 return 0;
3892         }
3893
3894         pctx->group = -1;
3895
3896         /* Warn user that the block wasn't claimed */
3897         fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
3898
3899         return 0;
3900 }
3901
3902 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
3903                             const char *name, int num, blk64_t *new_block)
3904 {
3905         ext2_filsys fs = ctx->fs;
3906         dgrp_t          last_grp;
3907         blk64_t         old_block = *new_block;
3908         blk64_t         last_block;
3909         dgrp_t          flexbg;
3910         unsigned        flexbg_size;
3911         int             i, is_flexbg;
3912         char            *buf;
3913         struct problem_context  pctx;
3914
3915         clear_problem_context(&pctx);
3916
3917         pctx.group = group;
3918         pctx.blk = old_block;
3919         pctx.str = name;
3920
3921         /*
3922          * For flex_bg filesystems, first try to allocate the metadata
3923          * within the flex_bg, and if that fails then try finding the
3924          * space anywhere in the filesystem.
3925          */
3926         is_flexbg = ext2fs_has_feature_flex_bg(fs->super);
3927         if (is_flexbg) {
3928                 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3929                 flexbg = group / flexbg_size;
3930                 first_block = ext2fs_group_first_block2(fs,
3931                                                         flexbg_size * flexbg);
3932                 last_grp = group | (flexbg_size - 1);
3933                 if (last_grp >= fs->group_desc_count)
3934                         last_grp = fs->group_desc_count - 1;
3935                 last_block = ext2fs_group_last_block2(fs, last_grp);
3936         } else
3937                 last_block = ext2fs_group_last_block2(fs, group);
3938         pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
3939                                                num, ctx->block_found_map,
3940                                                new_block);
3941         if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
3942                 pctx.errcode = ext2fs_get_free_blocks2(fs,
3943                                 fs->super->s_first_data_block,
3944                                 ext2fs_blocks_count(fs->super),
3945                                 num, ctx->block_found_map, new_block);
3946         if (pctx.errcode) {
3947                 pctx.num = num;
3948                 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
3949                 ext2fs_unmark_valid(fs);
3950                 ctx->flags |= E2F_FLAG_ABORT;
3951                 return;
3952         }
3953         pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
3954         if (pctx.errcode) {
3955                 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
3956                 ext2fs_unmark_valid(fs);
3957                 ctx->flags |= E2F_FLAG_ABORT;
3958                 return;
3959         }
3960         ext2fs_mark_super_dirty(fs);
3961         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
3962         pctx.blk2 = *new_block;
3963         fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
3964                           PR_1_RELOC_TO), &pctx);
3965         pctx.blk2 = 0;
3966         for (i = 0; i < num; i++) {
3967                 pctx.blk = i;
3968                 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
3969                 if (old_block) {
3970                         pctx.errcode = io_channel_read_blk64(fs->io,
3971                                    old_block + i, 1, buf);
3972                         if (pctx.errcode)
3973                                 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
3974                         pctx.blk = (*new_block) + i;
3975                         pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
3976                                                               1, buf);
3977                 } else {
3978                         pctx.blk = (*new_block) + i;
3979                         pctx.errcode = ext2fs_zero_blocks2(fs, pctx.blk, 1,
3980                                                            NULL, NULL);
3981                 }
3982
3983                 if (pctx.errcode)
3984                         fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
3985         }
3986         ext2fs_free_mem(&buf);
3987 }
3988
3989 /*
3990  * This routine gets called at the end of pass 1 if bad blocks are
3991  * detected in the superblock, group descriptors, inode_bitmaps, or
3992  * block bitmaps.  At this point, all of the blocks have been mapped
3993  * out, so we can try to allocate new block(s) to replace the bad
3994  * blocks.
3995  */
3996 static void handle_fs_bad_blocks(e2fsck_t ctx)
3997 {
3998         ext2_filsys fs = ctx->fs;
3999         dgrp_t          i;
4000         blk64_t         first_block;
4001         blk64_t         new_blk;
4002
4003         for (i = 0; i < fs->group_desc_count; i++) {
4004                 first_block = ext2fs_group_first_block2(fs, i);
4005
4006                 if (ctx->invalid_block_bitmap_flag[i]) {
4007                         new_blk = ext2fs_block_bitmap_loc(fs, i);
4008                         new_table_block(ctx, first_block, i, _("block bitmap"),
4009                                         1, &new_blk);
4010                         ext2fs_block_bitmap_loc_set(fs, i, new_blk);
4011                 }
4012                 if (ctx->invalid_inode_bitmap_flag[i]) {
4013                         new_blk = ext2fs_inode_bitmap_loc(fs, i);
4014                         new_table_block(ctx, first_block, i, _("inode bitmap"),
4015                                         1, &new_blk);
4016                         ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
4017                 }
4018                 if (ctx->invalid_inode_table_flag[i]) {
4019                         new_blk = ext2fs_inode_table_loc(fs, i);
4020                         new_table_block(ctx, first_block, i, _("inode table"),
4021                                         fs->inode_blocks_per_group,
4022                                         &new_blk);
4023                         ext2fs_inode_table_loc_set(fs, i, new_blk);
4024                         ctx->flags |= E2F_FLAG_RESTART;
4025                 }
4026         }
4027         ctx->invalid_bitmaps = 0;
4028 }
4029
4030 /*
4031  * This routine marks all blocks which are used by the superblock,
4032  * group descriptors, inode bitmaps, and block bitmaps.
4033  */
4034 static void mark_table_blocks(e2fsck_t ctx)
4035 {
4036         ext2_filsys fs = ctx->fs;
4037         blk64_t b;
4038         dgrp_t  i;
4039         unsigned int    j;
4040         struct problem_context pctx;
4041
4042         clear_problem_context(&pctx);
4043
4044         for (i = 0; i < fs->group_desc_count; i++) {
4045                 pctx.group = i;
4046
4047                 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
4048                 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_metadata_map);
4049
4050                 /*
4051                  * Mark the blocks used for the inode table
4052                  */
4053                 if (ext2fs_inode_table_loc(fs, i)) {
4054                         for (j = 0, b = ext2fs_inode_table_loc(fs, i);
4055                              j < fs->inode_blocks_per_group;
4056                              j++, b++) {
4057                                 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
4058                                                              b)) {
4059                                         pctx.blk = b;
4060                                         if (!ctx->invalid_inode_table_flag[i] &&
4061                                             fix_problem(ctx,
4062                                                 PR_1_ITABLE_CONFLICT, &pctx)) {
4063                                                 ctx->invalid_inode_table_flag[i]++;
4064                                                 ctx->invalid_bitmaps++;
4065                                         }
4066                                 } else {
4067                                     ext2fs_mark_block_bitmap2(
4068                                                 ctx->block_found_map, b);
4069                                     ext2fs_mark_block_bitmap2(
4070                                                 ctx->block_metadata_map, b);
4071                                 }
4072                         }
4073                 }
4074
4075                 /*
4076                  * Mark block used for the block bitmap
4077                  */
4078                 if (ext2fs_block_bitmap_loc(fs, i)) {
4079                         if (ext2fs_test_block_bitmap2(ctx->block_found_map,
4080                                      ext2fs_block_bitmap_loc(fs, i))) {
4081                                 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
4082                                 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
4083                                         ctx->invalid_block_bitmap_flag[i]++;
4084                                         ctx->invalid_bitmaps++;
4085                                 }
4086                         } else {
4087                             ext2fs_mark_block_bitmap2(ctx->block_found_map,
4088                                      ext2fs_block_bitmap_loc(fs, i));
4089                             ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
4090                                      ext2fs_block_bitmap_loc(fs, i));
4091                         }
4092                 }
4093                 /*
4094                  * Mark block used for the inode bitmap
4095                  */
4096                 if (ext2fs_inode_bitmap_loc(fs, i)) {
4097                         if (ext2fs_test_block_bitmap2(ctx->block_found_map,
4098                                      ext2fs_inode_bitmap_loc(fs, i))) {
4099                                 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
4100                                 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
4101                                         ctx->invalid_inode_bitmap_flag[i]++;
4102                                         ctx->invalid_bitmaps++;
4103                                 }
4104                         } else {
4105                             ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
4106                                      ext2fs_inode_bitmap_loc(fs, i));
4107                             ext2fs_mark_block_bitmap2(ctx->block_found_map,
4108                                      ext2fs_inode_bitmap_loc(fs, i));
4109                         }
4110                 }
4111         }
4112 }
4113
4114 /*
4115  * Thes subroutines short circuits ext2fs_get_blocks and
4116  * ext2fs_check_directory; we use them since we already have the inode
4117  * structure, so there's no point in letting the ext2fs library read
4118  * the inode again.
4119  */
4120 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
4121                                   blk_t *blocks)
4122 {
4123         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4124         int     i;
4125
4126         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
4127                 return EXT2_ET_CALLBACK_NOTHANDLED;
4128
4129         for (i=0; i < EXT2_N_BLOCKS; i++)
4130                 blocks[i] = ctx->stashed_inode->i_block[i];
4131         return 0;
4132 }
4133
4134 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
4135                                   struct ext2_inode *inode)
4136 {
4137         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4138
4139         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
4140                 return EXT2_ET_CALLBACK_NOTHANDLED;
4141         *inode = *ctx->stashed_inode;
4142         return 0;
4143 }
4144
4145 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
4146                             struct ext2_inode *inode)
4147 {
4148         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4149
4150         if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
4151                 (inode != ctx->stashed_inode))
4152                 *ctx->stashed_inode = *inode;
4153         return EXT2_ET_CALLBACK_NOTHANDLED;
4154 }
4155
4156 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
4157 {
4158         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4159
4160         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
4161                 return EXT2_ET_CALLBACK_NOTHANDLED;
4162
4163         if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
4164                 return EXT2_ET_NO_DIRECTORY;
4165         return 0;
4166 }
4167
4168 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
4169                                         blk64_t *ret)
4170 {
4171         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4172         errcode_t       retval;
4173         blk64_t         new_block;
4174
4175         if (ctx->block_found_map) {
4176                 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
4177                                            &new_block);
4178                 if (retval)
4179                         return retval;
4180                 if (fs->block_map) {
4181                         ext2fs_mark_block_bitmap2(fs->block_map, new_block);
4182                         ext2fs_mark_bb_dirty(fs);
4183                 }
4184         } else {
4185                 if (!fs->block_map) {
4186                         retval = ext2fs_read_block_bitmap(fs);
4187                         if (retval)
4188                                 return retval;
4189                 }
4190
4191                 retval = ext2fs_new_block2(fs, goal, fs->block_map, &new_block);
4192                 if (retval)
4193                         return retval;
4194         }
4195
4196         *ret = new_block;
4197         return (0);
4198 }
4199
4200 static errcode_t e2fsck_new_range(ext2_filsys fs, int flags, blk64_t goal,
4201                                   blk64_t len, blk64_t *pblk, blk64_t *plen)
4202 {
4203         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4204         errcode_t       retval;
4205
4206         if (ctx->block_found_map)
4207                 return ext2fs_new_range(fs, flags, goal, len,
4208                                         ctx->block_found_map, pblk, plen);
4209
4210         if (!fs->block_map) {
4211                 retval = ext2fs_read_block_bitmap(fs);
4212                 if (retval)
4213                         return retval;
4214         }
4215
4216         return ext2fs_new_range(fs, flags, goal, len, fs->block_map,
4217                                 pblk, plen);
4218 }
4219
4220 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
4221 {
4222         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4223
4224         /* Never free a critical metadata block */
4225         if (ctx->block_found_map &&
4226             ctx->block_metadata_map &&
4227             inuse < 0 &&
4228             ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk))
4229                 return;
4230
4231         if (ctx->block_found_map) {
4232                 if (inuse > 0)
4233                         ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
4234                 else
4235                         ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
4236         }
4237 }
4238
4239 static void e2fsck_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
4240                                            blk_t num, int inuse)
4241 {
4242         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4243
4244         /* Never free a critical metadata block */
4245         if (ctx->block_found_map &&
4246             ctx->block_metadata_map &&
4247             inuse < 0 &&
4248             ext2fs_test_block_bitmap_range2(ctx->block_metadata_map, blk, num))
4249                 return;
4250
4251         if (ctx->block_found_map) {
4252                 if (inuse > 0)
4253                         ext2fs_mark_block_bitmap_range2(ctx->block_found_map,
4254                                                         blk, num);
4255                 else
4256                         ext2fs_unmark_block_bitmap_range2(ctx->block_found_map,
4257                                                         blk, num);
4258         }
4259 }
4260
4261 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
4262 {
4263         ext2_filsys fs = ctx->fs;
4264
4265         if (use_shortcuts) {
4266                 fs->get_blocks = pass1_get_blocks;
4267                 fs->check_directory = pass1_check_directory;
4268                 fs->read_inode = pass1_read_inode;
4269                 fs->write_inode = pass1_write_inode;
4270                 ctx->stashed_ino = 0;
4271         } else {
4272                 fs->get_blocks = 0;
4273                 fs->check_directory = 0;
4274                 fs->read_inode = 0;
4275                 fs->write_inode = 0;
4276         }
4277 }
4278
4279 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
4280 {
4281         ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
4282         ext2fs_set_block_alloc_stats_callback(ctx->fs,
4283                                                 e2fsck_block_alloc_stats, 0);
4284         ext2fs_set_new_range_callback(ctx->fs, e2fsck_new_range, NULL);
4285         ext2fs_set_block_alloc_stats_range_callback(ctx->fs,
4286                                         e2fsck_block_alloc_stats_range, NULL);
4287 }