Whamcloud - gitweb
LU-7368 e2fsck: skip quota update when interrupted
[tools/e2fsprogs.git] / e2fsck / pass1.c
1 /*
2  * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
3  *
4  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  *
11  * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12  * and applies the following tests to each inode:
13  *
14  *      - The mode field of the inode must be legal.
15  *      - The size and block count fields of the inode are correct.
16  *      - A data block must not be used by another inode
17  *
18  * Pass 1 also gathers the collects the following information:
19  *
20  *      - A bitmap of which inodes are in use.          (inode_used_map)
21  *      - A bitmap of which inodes are directories.     (inode_dir_map)
22  *      - A bitmap of which inodes are regular files.   (inode_reg_map)
23  *      - An icount mechanism is used to keep track of
24  *        inodes with bad fields and its badness        (ctx->inode_badness)
25  *      - A bitmap of which inodes are in bad blocks.   (inode_bb_map)
26  *      - A bitmap of which inodes are imagic inodes.   (inode_imagic_map)
27  *      - A bitmap of which inodes need to be expanded  (expand_eisize_map)
28  *      - A bitmap of which blocks are in use.          (block_found_map)
29  *      - A bitmap of which blocks are in use by two inodes     (block_dup_map)
30  *      - The data blocks of the directory inodes.      (dir_map)
31  *      - A bitmap of EA inodes.                        (inode_ea_map)
32  *
33  * Pass 1 is designed to stash away enough information so that the
34  * other passes should not need to read in the inode information
35  * during the normal course of a filesystem check.  (Althogh if an
36  * inconsistency is detected, other passes may need to read in an
37  * inode to fix it.)
38  *
39  * Note that pass 1B will be invoked if there are any duplicate blocks
40  * found.
41  */
42
43 #define _GNU_SOURCE 1 /* get strnlen() */
44 #include "config.h"
45 #include <string.h>
46 #include <time.h>
47 #ifdef HAVE_ERRNO_H
48 #include <errno.h>
49 #endif
50
51 #include "e2fsck.h"
52 #include <ext2fs/ext2_ext_attr.h>
53
54 #include "problem.h"
55
56 #ifdef NO_INLINE_FUNCS
57 #define _INLINE_
58 #else
59 #define _INLINE_ inline
60 #endif
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);
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 handle_fs_bad_blocks(e2fsck_t ctx);
74 static void process_inodes(e2fsck_t ctx, char *block_buf);
75 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
76 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
77                                   dgrp_t group, void * priv_data);
78 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
79                                     char *block_buf, int adjust_sign);
80 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
81
82 struct process_block_struct {
83         ext2_ino_t      ino;
84         unsigned        is_dir:1, is_reg:1, clear:1, suppress:1,
85                                 fragmented:1, compressed:1, bbcheck:1,
86                                 inode_modified:1;
87         blk64_t         num_blocks;
88         blk64_t         max_blocks;
89         e2_blkcnt_t     last_block;
90         e2_blkcnt_t     last_init_lblock;
91         e2_blkcnt_t     last_db_block;
92         int             num_illegal_blocks;
93         blk64_t         previous_block;
94         struct ext2_inode *inode;
95         struct problem_context *pctx;
96         ext2fs_block_bitmap fs_meta_blocks;
97         e2fsck_t        ctx;
98 };
99
100 struct process_inode_block {
101         ext2_ino_t ino;
102         struct ext2_inode inode;
103 };
104
105 struct scan_callback_struct {
106         e2fsck_t        ctx;
107         char            *block_buf;
108 };
109
110 /*
111  * For the inodes to process list.
112  */
113 static struct process_inode_block *inodes_to_process;
114 static int process_inode_count;
115
116 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
117                             EXT2_MIN_BLOCK_LOG_SIZE + 1];
118
119 /*
120  * Free all memory allocated by pass1 in preparation for restarting
121  * things.
122  */
123 static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
124 {
125         ext2fs_free_mem(&inodes_to_process);
126         inodes_to_process = 0;
127 }
128
129 /*
130  * Check to make sure a device inode is real.  Returns 1 if the device
131  * checks out, 0 if not.
132  *
133  * Note: this routine is now also used to check FIFO's and Sockets,
134  * since they have the same requirement; the i_block fields should be
135  * zero.
136  */
137 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
138                                     struct ext2_inode *inode)
139 {
140         int     i;
141
142         /*
143          * If the index flag is set, then this is a bogus
144          * device/fifo/socket
145          */
146         if (inode->i_flags & (EXT2_INDEX_FL | EXT4_EXTENTS_FL))
147                 return 0;
148
149         /*
150          * We should be able to do the test below all the time, but
151          * because the kernel doesn't forcibly clear the device
152          * inode's additional i_block fields, there are some rare
153          * occasions when a legitimate device inode will have non-zero
154          * additional i_block fields.  So for now, we only complain
155          * when the immutable flag is set, which should never happen
156          * for devices.  (And that's when the problem is caused, since
157          * you can't set or clear immutable flags for devices.)  Once
158          * the kernel has been fixed we can change this...
159          */
160         if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
161                 for (i=4; i < EXT2_N_BLOCKS; i++)
162                         if (inode->i_block[i])
163                                 return 0;
164         }
165         return 1;
166 }
167
168 /*
169  * Check to make sure a symlink inode is real.  Returns 1 if the symlink
170  * checks out, 0 if not.
171  */
172 static int check_symlink(e2fsck_t ctx, struct problem_context *pctx,
173                          ext2_ino_t ino, struct ext2_inode *inode, char *buf)
174 {
175         blk64_t block, num_blocks;
176         unsigned len, limit;
177
178         if ((inode->i_size_high || inode->i_size == 0) ||
179             (inode->i_flags & EXT2_INDEX_FL))
180                 return 0;
181
182         num_blocks = ext2fs_inode_data_blocks2(ctx->fs, inode);
183         if (inode->i_flags & EXT4_EXTENTS_FL) { /* in extent-mapped block */
184                 struct ext2_extent_info info;
185                 ext2_extent_handle_t    handle;
186                 struct ext2fs_extent    extent;
187
188                 if (inode->i_size > ctx->fs->blocksize)
189                         return 0;
190                 if (ext2fs_extent_open2(ctx->fs, ino, inode, &handle))
191                         return 0;
192                 if (ext2fs_extent_get_info(handle, &info) ||
193                     (info.num_entries != 1) ||
194                     (info.max_depth != 0) ||
195
196                     ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) ||
197                     (extent.e_lblk != 0) ||
198                     (extent.e_len != 1)) {
199                         ext2fs_extent_free(handle);
200                         return 0;
201                 }
202
203                 block = extent.e_pblk;
204                 limit = ctx->fs->blocksize;
205
206                 ext2fs_extent_free(handle);
207         } else if (num_blocks > 0) {            /* in block-mapped block */
208                 int i;
209
210                 block = inode->i_block[0];
211                 limit = ctx->fs->blocksize;
212
213                 for (i = 1; i < EXT2_N_BLOCKS; i++)
214                         if (inode->i_block[i])
215                                 return 0;
216         } else {                                /* inside inode i_block[] */
217                 block = 0;
218                 limit = sizeof(inode->i_block);
219         }
220
221         if (inode->i_size >= limit)
222                 return 0;
223
224         if (num_blocks) {
225                 if ((num_blocks != ctx->fs->blocksize >> 9) || /* == 1 block */
226                     (block < ctx->fs->super->s_first_data_block) ||
227                     (block >= ext2fs_blocks_count(ctx->fs->super)))
228                         return 0;
229
230                 if (io_channel_read_blk64(ctx->fs->io, block, 1, buf))
231                         return 0;
232         } else {
233                 buf = (char *)inode->i_block;
234         }
235
236         len = strnlen(buf, limit);
237         /* Add missing NUL terminator at end of symlink (LU-1540),
238          * but only offer to fix this in pass1, not from pass2. */
239         if (len > inode->i_size && pctx != NULL &&
240             fix_problem(ctx, PR_1_SYMLINK_NUL, pctx)) {
241                 buf[inode->i_size] = '\0';
242                 if (num_blocks != 0) {
243                         if (io_channel_write_blk64(ctx->fs->io, block, 1, buf))
244                                 return 0;
245                 } else {
246                         e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
247                 }
248                 len = inode->i_size;
249         }
250         if (len != inode->i_size)
251                 return 0;
252
253         return 1;
254 }
255
256 int e2fsck_pass1_check_symlink(e2fsck_t ctx, ext2_ino_t ino,
257                                struct ext2_inode *inode, char *buf)
258 {
259         return check_symlink(ctx, NULL, ino, inode, buf);
260 }
261
262 /*
263  * If the immutable (or append-only) flag is set on the inode, offer
264  * to clear it.
265  */
266 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
267 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
268 {
269         if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
270                 return;
271
272         e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
273         if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
274                 return;
275
276         pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
277         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
278 }
279
280 /*
281  * If device, fifo or socket, check size is zero -- if not offer to
282  * clear it
283  */
284 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
285 {
286         struct ext2_inode *inode = pctx->inode;
287
288         if (EXT2_I_SIZE(inode) == 0)
289                 return;
290
291         e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
292         if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
293                 return;
294
295         ext2fs_inode_size_set(ctx->fs, inode, 0);
296         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
297 }
298
299 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
300 {
301         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
302
303         if (ctx->block_found_map) {
304                 if (inuse > 0)
305                         ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
306                 else
307                         ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
308         }
309 }
310
311 static void mark_inode_ea_map(e2fsck_t ctx, struct problem_context *pctx,
312                               ext2_ino_t ino)
313 {
314         if (!ctx->inode_ea_map) {
315                 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
316                                          _("EA inode map"),
317                                          &ctx->inode_ea_map);
318                 if (pctx->errcode) {
319                         fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
320                                     pctx);
321                         exit(1);
322                 }
323         }
324
325         ext2fs_mark_inode_bitmap2(ctx->inode_ea_map, ino);
326 }
327
328 /*
329  * Delete an EA entry. If this is the last entry to be deleted, then i_file_acl
330  * must have been freed, so we must update e2fsck block statistics and set
331  * i_file_acl_deleted.
332  * When we delete the entry successfully, this function returns 0, else
333  * non-zero value.
334  */
335
336 static int e2fsck_ea_entry_delete(e2fsck_t ctx,
337                                   struct ext2_ext_attr_entry *entry,
338                                   struct problem_context *pctx,
339                                   int *i_file_acl_deleted, problem_t prob)
340 {
341         blk_t i_file_acl = pctx->inode->i_file_acl;
342         int err = 1;
343
344         pctx->num = entry->e_value_inum;
345
346         if (fix_problem(ctx, prob, pctx)) {
347                 /* Delete corrupt EA entry */
348                 err = ext2fs_attr_set(ctx->fs, pctx->ino, pctx->inode,
349                                       entry->e_name_index, entry->e_name,
350                                       0, 0, 0);
351                 if (err == 0) {
352                         if (i_file_acl && pctx->inode->i_file_acl == 0) {
353                                 e2fsck_block_alloc_stats(ctx->fs, i_file_acl,
354                                                          -1);
355                                 *i_file_acl_deleted = 1;
356                         }
357                         return 0;
358                 }
359         }
360
361         return err;
362 }
363
364 /*
365  * Check validity of EA inode. Return 0 if EA inode is valid, nonzero otherwise.
366  */
367 static int check_large_ea_inode(e2fsck_t ctx, struct ext2_ext_attr_entry *entry,
368                                 struct problem_context *pctx,
369                                 int *i_file_acl_deleted)
370 {
371         struct ext2_inode inode;
372         int ret = 0;
373
374         /* Check if inode is within valid range */
375         if ((entry->e_value_inum < EXT2_FIRST_INODE(ctx->fs->super)) ||
376             (entry->e_value_inum > ctx->fs->super->s_inodes_count)) {
377                 ret = e2fsck_ea_entry_delete(ctx, entry, pctx,
378                                              i_file_acl_deleted,
379                                              PR_1_ATTR_VALUE_EA_INODE);
380                 /* If user refuses to delete this entry, caller may try to set
381                  * the bit for this out-of-bound inode in inode_ea_map, so
382                  * always return failure */
383                 return 1;
384         }
385
386         e2fsck_read_inode(ctx, entry->e_value_inum, &inode, "pass1");
387         if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
388                 /* If EXT4_EA_INODE_FL flag is not present but back-pointer
389                  * matches then we should set this flag */
390                 if (inode.i_mtime == pctx->ino &&
391                     inode.i_generation == pctx->inode->i_generation &&
392                     fix_problem(ctx, PR_1_ATTR_SET_EA_INODE_FL, pctx)) {
393                         inode.i_flags |= EXT4_EA_INODE_FL;
394                         ext2fs_write_inode(ctx->fs, entry->e_value_inum,&inode);
395                 } else {
396                         ret = e2fsck_ea_entry_delete(ctx, entry, pctx,
397                                                      i_file_acl_deleted,
398                                                      PR_1_ATTR_NO_EA_INODE_FL);
399                         goto out;
400                 }
401         } else if (inode.i_mtime != pctx->ino ||
402                    inode.i_generation != pctx->inode->i_generation) {
403                 ret = e2fsck_ea_entry_delete(ctx, entry, pctx,
404                                              i_file_acl_deleted,
405                                              PR_1_ATTR_INVAL_EA_INODE);
406                 goto out;
407         }
408
409 out:
410         return ret;
411 }
412
413 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
414 {
415         struct ext2_super_block *sb = ctx->fs->super;
416         struct ext2_inode_large *inode;
417         struct ext2_ext_attr_entry *entry;
418         char *start;
419         unsigned int storage_size, remain;
420         problem_t problem = 0;
421
422         inode = (struct ext2_inode_large *) pctx->inode;
423         storage_size = EXT2_INODE_SIZE(ctx->fs->super) -
424                 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize;
425         entry = &IHDR(inode)->h_first_entry[0];
426         start = (char *)entry;
427
428         /* scan all entry's headers first */
429
430         /* take finish entry 0UL into account */
431         remain = storage_size - sizeof(__u32);
432
433         while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
434                 __u32 hash;
435
436                 /* header eats this space */
437                 remain -= sizeof(struct ext2_ext_attr_entry);
438
439                 /* is attribute name valid? */
440                 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
441                         pctx->num = entry->e_name_len;
442                         problem = PR_1_ATTR_NAME_LEN;
443                         goto fix;
444                 }
445
446                 /* attribute len eats this space */
447                 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
448
449                 if (entry->e_value_inum == 0) {
450                         /* check value size */
451                         if (entry->e_value_size > remain) {
452                                 pctx->num = entry->e_value_size;
453                                 problem = PR_1_ATTR_VALUE_SIZE;
454                                 goto fix;
455                         }
456                 } else {
457                         int ret, tmp;
458
459                         ret = check_large_ea_inode(ctx, entry, pctx, &tmp);
460                         if (ret == 0)
461                                 mark_inode_ea_map(ctx, pctx,
462                                                   entry->e_value_inum);
463                 }
464
465                 /* Value size cannot be larger than EA space in inode */
466                 if (entry->e_value_offs > storage_size ||
467                     (entry->e_value_inum == 0 &&
468                     entry->e_value_offs + entry->e_value_size > storage_size)) {
469                         problem = PR_1_INODE_EA_BAD_VALUE;
470                         goto fix;
471                 }
472
473                 hash = ext2fs_ext_attr_hash_entry(entry,
474                                                   start + entry->e_value_offs);
475
476                 /* e_hash may be 0 in older inode's ea */
477                 if (entry->e_hash != 0 && entry->e_hash != hash) {
478                         pctx->num = entry->e_hash;
479                         problem = PR_1_ATTR_HASH;
480                         goto fix;
481                 }
482
483                 /* If EA value is stored in external inode then it does not
484                  * consume space here */
485                 if (entry->e_value_inum == 0)
486                         remain -= entry->e_value_size;
487
488                 entry = EXT2_EXT_ATTR_NEXT(entry);
489         }
490 fix:
491         /*
492          * it seems like a corruption. it's very unlikely we could repair
493          * EA(s) in automatic fashion -bzzz
494          */
495         if (problem == 0 || !fix_problem(ctx, problem, pctx))
496                 return;
497
498         /* simply remove all possible EA(s) */
499         *((__u32 *)start) = 0UL;
500         e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
501                                 EXT2_INODE_SIZE(sb), "pass1");
502 }
503
504 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
505 {
506         struct ext2_super_block *sb = ctx->fs->super;
507         struct ext2_inode_large *inode;
508         __u32 *eamagic;
509         int min, max, dirty = 0;
510
511         inode = (struct ext2_inode_large *) pctx->inode;
512         if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
513                 /* this isn't large inode. so, nothing to check */
514                 return;
515         }
516
517 #if 0
518         printf("inode #%u, i_extra_size %d\n", pctx->ino,
519                         inode->i_extra_isize);
520 #endif
521         /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
522         min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
523         max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
524         /*
525          * For now we will allow i_extra_isize to be 0, but really
526          * implementations should never allow i_extra_isize to be 0
527          */
528         if (inode->i_extra_isize &&
529             (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
530                 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
531                 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
532                         return;
533                 inode->i_extra_isize = ctx->want_extra_isize;
534                 dirty = 1;
535
536                 goto out;
537         }
538
539         if (EXT4_FITS_IN_INODE(inode, inode, i_crtime) &&
540             inode->i_crtime != 0 &&
541             (EXT4_XTIME_FUTURE(ctx, sb, inode->i_crtime, 2*ctx->time_fudge) ||
542              EXT4_XTIME_ANCIENT(ctx, sb, inode->i_crtime, 2*ctx->time_fudge))) {
543                 pctx->num = inode->i_crtime;
544                 if (fix_problem(ctx, PR_1_CRTIME_BAD, pctx)) {
545                         inode->i_crtime = 0;
546                         dirty = 1;
547                 }
548                 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_HIGH);
549         }
550
551         eamagic = &IHDR(inode)->h_magic;
552         if (*eamagic != EXT2_EXT_ATTR_MAGIC &&
553             (ctx->flags & E2F_FLAG_EXPAND_EISIZE) &&
554             (inode->i_extra_isize < ctx->want_extra_isize)) {
555                 fix_problem(ctx, PR_1_EXPAND_EISIZE, pctx);
556                 memset((char *)inode + EXT2_GOOD_OLD_INODE_SIZE, 0,
557                         EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE);
558                 inode->i_extra_isize = ctx->want_extra_isize;
559                 dirty = 1;
560                 if (inode->i_extra_isize < ctx->min_extra_isize)
561                         ctx->min_extra_isize = inode->i_extra_isize;
562         }
563
564         if (*eamagic == EXT2_EXT_ATTR_MAGIC)
565                 check_ea_in_inode(ctx, pctx);
566 out:
567         if (dirty)
568                 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
569                                         EXT2_INODE_SIZE(sb), "pass1");
570 }
571
572 /*
573  * Check to see if the inode might really be a directory, despite i_mode
574  *
575  * This is a lot of complexity for something for which I'm not really
576  * convinced happens frequently in the wild.  If for any reason this
577  * causes any problems, take this code out.
578  * [tytso:20070331.0827EDT]
579  */
580 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
581                                 char *buf)
582 {
583         struct ext2_inode *inode = pctx->inode;
584         struct ext2_dir_entry   *dirent;
585         errcode_t               retval;
586         blk64_t                 blk;
587         unsigned int            i, rec_len, not_device = 0;
588         int                     extent_fs;
589
590         /*
591          * If the mode looks OK, we believe it.  If the first block in
592          * the i_block array is 0, this cannot be a directory. If the
593          * inode is extent-mapped, it is still the case that the latter
594          * cannot be 0 - the magic number in the extent header would make
595          * it nonzero.
596          */
597         if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
598             LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
599                 return;
600
601         /* 
602          * Check the block numbers in the i_block array for validity:
603          * zero blocks are skipped (but the first one cannot be zero -
604          * see above), other blocks are checked against the first and
605          * max data blocks (from the the superblock) and against the
606          * block bitmap. Any invalid block found means this cannot be
607          * a directory.
608          * 
609          * If there are non-zero blocks past the fourth entry, then
610          * this cannot be a device file: we remember that for the next
611          * check.
612          *
613          * For extent mapped files, we don't do any sanity checking:
614          * just try to get the phys block of logical block 0 and run
615          * with it.
616          */
617
618         extent_fs = (ctx->fs->super->s_feature_incompat &
619                      EXT3_FEATURE_INCOMPAT_EXTENTS);
620         if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
621                 /* extent mapped */
622                 if  (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
623                                  &blk))
624                         return;
625                 /* device files are never extent mapped */
626                 not_device++;
627         } else {
628                 for (i=0; i < EXT2_N_BLOCKS; i++) {
629                         blk = inode->i_block[i];
630                         if (!blk)
631                                 continue;
632                         if (i >= 4)
633                                 not_device++;
634
635                         if (blk < ctx->fs->super->s_first_data_block ||
636                             blk >= ext2fs_blocks_count(ctx->fs->super) ||
637                             ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
638                                                            blk))
639                                 return; /* Invalid block, can't be dir */
640                 }
641                 blk = inode->i_block[0];
642         }
643
644         /*
645          * If the mode says this is a device file and the i_links_count field
646          * is sane and we have not ruled it out as a device file previously,
647          * we declare it a device file, not a directory.
648          */
649         if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
650             (inode->i_links_count == 1) && !not_device)
651                 return;
652
653         /* read the first block */
654         ehandler_operation(_("reading directory block"));
655         retval = ext2fs_read_dir_block3(ctx->fs, blk, buf, 0);
656         ehandler_operation(0);
657         if (retval)
658                 return;
659
660         dirent = (struct ext2_dir_entry *) buf;
661         retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
662         if (retval)
663                 return;
664         if (((dirent->name_len & 0xFF) != 1) ||
665             (dirent->name[0] != '.') ||
666             (dirent->inode != pctx->ino) ||
667             (rec_len < 12) ||
668             (rec_len % 4) ||
669             (rec_len >= ctx->fs->blocksize - 12))
670                 return;
671
672         dirent = (struct ext2_dir_entry *) (buf + rec_len);
673         retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
674         if (retval)
675                 return;
676         if (((dirent->name_len & 0xFF) != 2) ||
677             (dirent->name[0] != '.') ||
678             (dirent->name[1] != '.') ||
679             (rec_len < 12) ||
680             (rec_len % 4))
681                 return;
682
683         e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
684         if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
685                 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
686                 e2fsck_write_inode_full(ctx, pctx->ino, inode,
687                                         EXT2_INODE_SIZE(ctx->fs->super),
688                                         "check_is_really_dir");
689         }
690 }
691
692 void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
693                              ext2_icount_t *ret)
694 {
695         unsigned int            threshold;
696         ext2_ino_t              num_dirs;
697         errcode_t               retval;
698         char                    *tdb_dir;
699         int                     enable;
700
701         *ret = 0;
702
703         profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
704                            &tdb_dir);
705         profile_get_uint(ctx->profile, "scratch_files",
706                          "numdirs_threshold", 0, 0, &threshold);
707         profile_get_boolean(ctx->profile, "scratch_files",
708                             "icount", 0, 1, &enable);
709
710         retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
711         if (retval)
712                 num_dirs = 1024;        /* Guess */
713
714         if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
715             (threshold && num_dirs <= threshold))
716                 return;
717
718         retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
719         if (retval)
720                 *ret = 0;
721 }
722
723 int e2fsck_pass1_delete_attr(e2fsck_t ctx, struct ext2_inode_large *inode,
724                              struct problem_context *pctx, int needed_size)
725 {
726         struct ext2_ext_attr_header *header;
727         struct ext2_ext_attr_entry *entry_ino, *entry_blk = NULL, *entry;
728         char *start, name[4096], block_buf[4096];
729         int len, index = EXT2_ATTR_INDEX_USER, entry_size, ea_size;
730         int in_inode = 1, error;
731         unsigned int freed_bytes = inode->i_extra_isize;
732
733         entry_ino = &IHDR(inode)->h_first_entry[0];
734         start = (char *)entry_ino;
735
736         if (inode->i_file_acl) {
737                 error = ext2fs_read_ext_attr(ctx->fs, inode->i_file_acl,
738                                              block_buf);
739                 /* We have already checked this block, shouldn't happen */
740                 if (error) {
741                         fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
742                         return 0;
743                 }
744                 header = BHDR(block_buf);
745                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
746                         fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
747                         return 0;
748                 }
749
750                 entry_blk = (struct ext2_ext_attr_entry *)(header+1);
751         }
752         entry = entry_ino;
753         len = sizeof(entry->e_name);
754         entry_size = ext2fs_attr_get_next_attr(entry, index, name, len, 1);
755
756         while (freed_bytes < needed_size) {
757                 if (entry_size && name[0] != '\0') {
758                         pctx->str = name;
759                         if (fix_problem(ctx, PR_1_EISIZE_DELETE_EA, pctx)) {
760                                 ea_size = EXT2_EXT_ATTR_LEN(entry->e_name_len) +
761                                           EXT2_EXT_ATTR_SIZE(entry->e_value_size);
762                                 error = ext2fs_attr_set(ctx->fs, pctx->ino,
763                                                         (struct ext2_inode *)inode,
764                                                         index, name, 0, 0, 0);
765                                 if (!error)
766                                         freed_bytes += ea_size;
767                         }
768                 }
769                 len = sizeof(entry->e_name);
770                 entry_size = ext2fs_attr_get_next_attr(entry, index,name,len,0);
771                 entry = EXT2_EXT_ATTR_NEXT(entry);
772                 if (EXT2_EXT_IS_LAST_ENTRY(entry)) {
773                         if (in_inode) {
774                                 entry = entry_blk;
775                                 len = sizeof(entry->e_name);
776                                 entry_size = ext2fs_attr_get_next_attr(entry,
777                                                         index, name, len, 1);
778                                 in_inode = 0;
779                         } else {
780                                 index += 1;
781                                 in_inode = 1;
782                                 if (!entry && index < EXT2_ATTR_INDEX_MAX)
783                                         entry = (struct ext2_ext_attr_entry *)start;
784                                 else
785                                         return freed_bytes;
786                         }
787                 }
788         }
789
790         return freed_bytes;
791 }
792
793 int e2fsck_pass1_expand_eisize(e2fsck_t ctx, struct ext2_inode_large *inode,
794                                struct problem_context *pctx)
795 {
796         int needed_size = 0, retval, ret = EXT2_EXPAND_EISIZE_UNSAFE;
797         static int message;
798
799 retry:
800         retval = ext2fs_expand_extra_isize(ctx->fs, pctx->ino, inode,
801                                            ctx->want_extra_isize, &ret,
802                                            &needed_size);
803         if (ret & EXT2_EXPAND_EISIZE_NEW_BLOCK)
804                 goto mark_expand_eisize_map;
805         if (!retval) {
806                 e2fsck_write_inode_full(ctx, pctx->ino,
807                                         (struct ext2_inode *)inode,
808                                         EXT2_INODE_SIZE(ctx->fs->super),
809                                         "pass1");
810                 return 0;
811         }
812
813         if (ret & EXT2_EXPAND_EISIZE_NOSPC) {
814                 if (ctx->options & (E2F_OPT_PREEN | E2F_OPT_YES)) {
815                         fix_problem(ctx, PR_1_EA_BLK_NOSPC, pctx);
816                         ctx->flags |= E2F_FLAG_ABORT;
817                         return -1;
818                 }
819
820                 if (!message) {
821                         pctx->num = ctx->fs->super->s_min_extra_isize;
822                         fix_problem(ctx, PR_1_EXPAND_EISIZE_WARNING, pctx);
823                         message = 1;
824                 }
825 delete_EA:
826                 retval = e2fsck_pass1_delete_attr(ctx, inode, pctx,
827                                                   needed_size);
828                 if (retval >= ctx->want_extra_isize)
829                         goto retry;
830
831                 needed_size -= retval;
832
833                 /*
834                  * We loop here until either the user deletes EA(s) or
835                  * EXTRA_ISIZE feature is disabled.
836                  */
837                 if (fix_problem(ctx, PR_1_CLEAR_EXTRA_ISIZE, pctx)) {
838                         ctx->fs->super->s_feature_ro_compat &=
839                                         ~EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE;
840                         ext2fs_mark_super_dirty(ctx->fs);
841                 } else {
842                         goto delete_EA;
843                 }
844                 ctx->fs_unexpanded_inodes++;
845
846                 /* No EA was deleted, inode cannot be expanded */
847                 return -1;
848         }
849
850 mark_expand_eisize_map:
851         if (!ctx->expand_eisize_map) {
852                 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
853                                          _("expand extrz isize map"),
854                                          &ctx->expand_eisize_map);
855                 if (pctx->errcode) {
856                         fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
857                                     pctx);
858                         exit(1);
859                 }
860         }
861
862         /* Add this inode to the expand_eisize_map */
863         ext2fs_mark_inode_bitmap2(ctx->expand_eisize_map, pctx->ino);
864         return 0;
865 }
866
867 static void reserve_block_for_root_repair(e2fsck_t ctx)
868 {
869         blk64_t         blk = 0;
870         errcode_t       err;
871         ext2_filsys     fs = ctx->fs;
872
873         ctx->root_repair_block = 0;
874         if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
875                 return;
876
877         err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
878         if (err)
879                 return;
880         ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
881         ctx->root_repair_block = blk;
882 }
883
884 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
885 {
886         blk64_t         blk = 0;
887         errcode_t       err;
888         ext2_filsys     fs = ctx->fs;
889         static const char name[] = "lost+found";
890         ext2_ino_t      ino;
891
892         ctx->lnf_repair_block = 0;
893         if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
894                 return;
895
896         err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
897         if (err)
898                 return;
899         ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
900         ctx->lnf_repair_block = blk;
901 }
902
903 void e2fsck_pass1(e2fsck_t ctx)
904 {
905         int     i;
906         __u64   max_sizes;
907         ext2_filsys fs = ctx->fs;
908         ext2_ino_t      ino = 0;
909         struct ext2_inode *inode = NULL;
910         ext2_inode_scan scan = NULL;
911         char            *block_buf = NULL;
912 #ifdef RESOURCE_TRACK
913         struct resource_track   rtrack;
914 #endif
915         unsigned char   frag, fsize;
916         struct          problem_context pctx;
917         struct          scan_callback_struct scan_struct;
918         struct ext2_super_block *sb = ctx->fs->super;
919         const char      *old_op;
920         unsigned int    save_type;
921         int             imagic_fs, extent_fs;
922         int             low_dtime_check = 1;
923         int             inode_size;
924         int             inode_exp = 0;
925
926
927         init_resource_track(&rtrack, ctx->fs->io);
928         clear_problem_context(&pctx);
929
930         if (!(ctx->options & E2F_OPT_PREEN))
931                 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
932
933         if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
934             !(ctx->options & E2F_OPT_NO)) {
935                 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
936                         ctx->dirs_to_hash = 0;
937         }
938
939 #ifdef MTRACE
940         mtrace_print("Pass 1");
941 #endif
942
943 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
944
945         for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
946                 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
947                 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
948                 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
949                 max_sizes = (max_sizes * (1UL << i));
950                 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
951         }
952 #undef EXT2_BPP
953
954         imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
955         extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
956
957         /*
958          * Allocate bitmaps structures
959          */
960         pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
961                                                     EXT2FS_BMAP64_RBTREE,
962                                                     "inode_used_map",
963                                                     &ctx->inode_used_map);
964         if (pctx.errcode) {
965                 pctx.num = 1;
966                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
967                 ctx->flags |= E2F_FLAG_ABORT;
968                 return;
969         }
970         pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
971                         _("directory inode map"),
972                         EXT2FS_BMAP64_AUTODIR,
973                         "inode_dir_map", &ctx->inode_dir_map);
974         if (pctx.errcode) {
975                 pctx.num = 2;
976                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
977                 ctx->flags |= E2F_FLAG_ABORT;
978                 return;
979         }
980         pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
981                         _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
982                         "inode_reg_map", &ctx->inode_reg_map);
983         if (pctx.errcode) {
984                 pctx.num = 6;
985                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
986                 ctx->flags |= E2F_FLAG_ABORT;
987                 return;
988         }
989         pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
990                         _("in-use block map"), EXT2FS_BMAP64_RBTREE,
991                         "block_found_map", &ctx->block_found_map);
992         if (pctx.errcode) {
993                 pctx.num = 1;
994                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
995                 ctx->flags |= E2F_FLAG_ABORT;
996                 return;
997         }
998         e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
999         if (!ctx->inode_link_info) {
1000                 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
1001                                        "inode_link_info", &save_type);
1002                 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
1003                                                      &ctx->inode_link_info);
1004                 fs->default_bitmap_type = save_type;
1005         }
1006
1007         if (pctx.errcode) {
1008                 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1009                 ctx->flags |= E2F_FLAG_ABORT;
1010                 return;
1011         }
1012         inode_size = EXT2_INODE_SIZE(fs->super);
1013         inode = (struct ext2_inode *)
1014                 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
1015
1016         inodes_to_process = (struct process_inode_block *)
1017                 e2fsck_allocate_memory(ctx,
1018                                        (ctx->process_inode_size *
1019                                         sizeof(struct process_inode_block)),
1020                                        "array of inodes to process");
1021         process_inode_count = 0;
1022
1023         pctx.errcode = ext2fs_init_dblist(fs, 0);
1024         if (pctx.errcode) {
1025                 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1026                 ctx->flags |= E2F_FLAG_ABORT;
1027                 goto endit;
1028         }
1029
1030         /*
1031          * If the last orphan field is set, clear it, since the pass1
1032          * processing will automatically find and clear the orphans.
1033          * In the future, we may want to try using the last_orphan
1034          * linked list ourselves, but for now, we clear it so that the
1035          * ext3 mount code won't get confused.
1036          */
1037         if (!(ctx->options & E2F_OPT_READONLY)) {
1038                 if (fs->super->s_last_orphan) {
1039                         fs->super->s_last_orphan = 0;
1040                         ext2fs_mark_super_dirty(fs);
1041                 }
1042         }
1043
1044         mark_table_blocks(ctx);
1045         pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
1046                                                 &ctx->block_found_map);
1047         if (pctx.errcode) {
1048                 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1049                 ctx->flags |= E2F_FLAG_ABORT;
1050                 goto endit;
1051         }
1052         block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1053                                                     "block interate buffer");
1054         e2fsck_use_inode_shortcuts(ctx, 1);
1055         e2fsck_intercept_block_allocations(ctx);
1056         old_op = ehandler_operation(_("opening inode scan"));
1057         pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1058                                               &scan);
1059         ehandler_operation(old_op);
1060         if (pctx.errcode) {
1061                 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1062                 ctx->flags |= E2F_FLAG_ABORT;
1063                 goto endit;
1064         }
1065         ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
1066         ctx->stashed_inode = inode;
1067         scan_struct.ctx = ctx;
1068         scan_struct.block_buf = block_buf;
1069         ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1070         if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1071                                               ctx->fs->group_desc_count)))
1072                 goto endit;
1073         if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
1074             (fs->super->s_mtime < fs->super->s_inodes_count) ||
1075             (fs->super->s_mkfs_time &&
1076              fs->super->s_mkfs_time < fs->super->s_inodes_count))
1077                 low_dtime_check = 0;
1078
1079         if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
1080             fs->super->s_mmp_block > fs->super->s_first_data_block &&
1081             fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1082                 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1083                                           fs->super->s_mmp_block);
1084
1085         while (1) {
1086                 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
1087                         if (e2fsck_mmp_update(fs))
1088                                 fatal_error(ctx, 0);
1089                 }
1090                 old_op = ehandler_operation(_("getting next inode from scan"));
1091                 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1092                                                           inode, inode_size);
1093                 ehandler_operation(old_op);
1094                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1095                         goto endit;
1096                 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1097                         if (!ctx->inode_bb_map)
1098                                 alloc_bb_map(ctx);
1099                         ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1100                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1101                         continue;
1102                 }
1103                 if (pctx.errcode) {
1104                         fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1105                         ctx->flags |= E2F_FLAG_ABORT;
1106                         goto endit;
1107                 }
1108                 if (!ino)
1109                         break;
1110                 pctx.ino = ino;
1111                 pctx.inode = inode;
1112                 ctx->stashed_ino = ino;
1113                 if (inode->i_links_count) {
1114                         pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
1115                                            ino, inode->i_links_count);
1116                         if (pctx.errcode) {
1117                                 pctx.num = inode->i_links_count;
1118                                 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
1119                                 ctx->flags |= E2F_FLAG_ABORT;
1120                                 goto endit;
1121                         }
1122                 }
1123
1124                 /*
1125                  * Test for incorrect extent flag settings.
1126                  *
1127                  * On big-endian machines we must be careful:
1128                  * When the inode is read, the i_block array is not swapped
1129                  * if the extent flag is set.  Therefore if we are testing
1130                  * for or fixing a wrongly-set flag, we must potentially
1131                  * (un)swap before testing, or after fixing.
1132                  */
1133
1134                 /*
1135                  * In this case the extents flag was set when read, so
1136                  * extent_header_verify is ok.  If the inode is cleared,
1137                  * no need to swap... so no extra swapping here.
1138                  */
1139                 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
1140                     (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1141                      (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
1142                         if ((ext2fs_extent_header_verify(inode->i_block,
1143                                                  sizeof(inode->i_block)) == 0) &&
1144                             fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
1145                                 sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS;
1146                                 ext2fs_mark_super_dirty(fs);
1147                                 extent_fs = 1;
1148                         } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
1149                         clear_inode:
1150                                 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1151                                 if (ino == EXT2_BAD_INO)
1152                                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
1153                                                                  ino);
1154                                 continue;
1155                         }
1156                 }
1157
1158                 /*
1159                  * For big-endian machines:
1160                  * If the inode didn't have the extents flag set when it
1161                  * was read, then the i_blocks array was swapped.  To test
1162                  * as an extents header, we must swap it back first.
1163                  * IF we then set the extents flag, the entire i_block
1164                  * array must be un/re-swapped to make it proper extents data.
1165                  */
1166                 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
1167                     (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1168                      (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
1169                     (LINUX_S_ISREG(inode->i_mode) ||
1170                      LINUX_S_ISDIR(inode->i_mode))) {
1171                         void *ehp;
1172 #ifdef WORDS_BIGENDIAN
1173                         __u32 tmp_block[EXT2_N_BLOCKS];
1174
1175                         for (i = 0; i < EXT2_N_BLOCKS; i++)
1176                                 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
1177                         ehp = tmp_block;
1178 #else
1179                         ehp = inode->i_block;
1180 #endif
1181                         if ((ext2fs_extent_header_verify(ehp,
1182                                          sizeof(inode->i_block)) == 0)) {
1183                                 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1184                                 if (fix_problem(ctx, PR_1_UNSET_EXTENT_FL,
1185                                                 &pctx)) {
1186                                         inode->i_flags |= EXT4_EXTENTS_FL;
1187 #ifdef WORDS_BIGENDIAN
1188                                         memcpy(inode->i_block, tmp_block,
1189                                                sizeof(inode->i_block));
1190 #endif
1191                                         e2fsck_write_inode(ctx, ino, inode,
1192                                                            "pass1");
1193                                 }
1194                         }
1195                 }
1196
1197                 if (ino == EXT2_BAD_INO) {
1198                         struct process_block_struct pb;
1199
1200                         if ((inode->i_mode || inode->i_uid || inode->i_gid ||
1201                              inode->i_links_count || inode->i_file_acl) &&
1202                             fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
1203                                 memset(inode, 0, sizeof(struct ext2_inode));
1204                                 e2fsck_write_inode(ctx, ino, inode,
1205                                                    "clear bad inode");
1206                         }
1207
1208                         pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
1209                                                           &pb.fs_meta_blocks);
1210                         if (pctx.errcode) {
1211                                 pctx.num = 4;
1212                                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1213                                 ctx->flags |= E2F_FLAG_ABORT;
1214                                 goto endit;
1215                         }
1216                         pb.ino = EXT2_BAD_INO;
1217                         pb.num_blocks = pb.last_block = 0;
1218                         pb.last_db_block = -1;
1219                         pb.num_illegal_blocks = 0;
1220                         pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
1221                         pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
1222                         pb.inode = inode;
1223                         pb.pctx = &pctx;
1224                         pb.ctx = ctx;
1225                         pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
1226                                      block_buf, process_bad_block, &pb);
1227                         ext2fs_free_block_bitmap(pb.fs_meta_blocks);
1228                         if (pctx.errcode) {
1229                                 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
1230                                 ctx->flags |= E2F_FLAG_ABORT;
1231                                 goto endit;
1232                         }
1233                         if (pb.bbcheck)
1234                                 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
1235                                 ctx->flags |= E2F_FLAG_ABORT;
1236                                 goto endit;
1237                         }
1238                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1239                         clear_problem_context(&pctx);
1240                         continue;
1241                 } else if (ino == EXT2_ROOT_INO) {
1242                         /*
1243                          * Make sure the root inode is a directory; if
1244                          * not, offer to clear it.  It will be
1245                          * regnerated in pass #3.
1246                          */
1247                         if (!LINUX_S_ISDIR(inode->i_mode)) {
1248                                 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
1249                                         goto clear_inode;
1250                         }
1251                         /*
1252                          * If dtime is set, offer to clear it.  mke2fs
1253                          * version 0.2b created filesystems with the
1254                          * dtime field set for the root and lost+found
1255                          * directories.  We won't worry about
1256                          * /lost+found, since that can be regenerated
1257                          * easily.  But we will fix the root directory
1258                          * as a special case.
1259                          */
1260                         if (inode->i_dtime && inode->i_links_count) {
1261                                 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1262                                 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
1263                                         inode->i_dtime = 0;
1264                                         e2fsck_write_inode(ctx, ino, inode,
1265                                                            "pass1");
1266                                 }
1267                         }
1268                 } else if (ino == EXT2_JOURNAL_INO) {
1269                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1270                         if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
1271                                 if (!LINUX_S_ISREG(inode->i_mode) &&
1272                                     fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
1273                                                 &pctx)) {
1274                                         inode->i_mode = LINUX_S_IFREG;
1275                                         e2fsck_write_inode(ctx, ino, inode,
1276                                                            "pass1");
1277                                 }
1278                                 check_blocks(ctx, &pctx, block_buf);
1279                                 continue;
1280                         }
1281                         if ((inode->i_links_count ||
1282                              inode->i_blocks || inode->i_block[0]) &&
1283                             fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
1284                                         &pctx)) {
1285                                 memset(inode, 0, inode_size);
1286                                 ext2fs_icount_store(ctx->inode_link_info,
1287                                                     ino, 0);
1288                                 e2fsck_write_inode_full(ctx, ino, inode,
1289                                                         inode_size, "pass1");
1290                         }
1291                 } else if ((ino == EXT4_USR_QUOTA_INO) ||
1292                            (ino == EXT4_GRP_QUOTA_INO)) {
1293                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1294                         if ((fs->super->s_feature_ro_compat &
1295                                         EXT4_FEATURE_RO_COMPAT_QUOTA) &&
1296                             ((fs->super->s_usr_quota_inum == ino) ||
1297                              (fs->super->s_grp_quota_inum == ino))) {
1298                                 if (!LINUX_S_ISREG(inode->i_mode) &&
1299                                     fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
1300                                                         &pctx)) {
1301                                         inode->i_mode = LINUX_S_IFREG;
1302                                         e2fsck_write_inode(ctx, ino, inode,
1303                                                         "pass1");
1304                                 }
1305                                 check_blocks(ctx, &pctx, block_buf);
1306                                 continue;
1307                         }
1308                         if ((inode->i_links_count ||
1309                              inode->i_blocks || inode->i_block[0]) &&
1310                             fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
1311                                         &pctx)) {
1312                                 memset(inode, 0, inode_size);
1313                                 ext2fs_icount_store(ctx->inode_link_info,
1314                                                     ino, 0);
1315                                 e2fsck_write_inode_full(ctx, ino, inode,
1316                                                         inode_size, "pass1");
1317                         }
1318                 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
1319                         problem_t problem = 0;
1320
1321                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1322                         if (ino == EXT2_BOOT_LOADER_INO) {
1323                                 if (LINUX_S_ISDIR(inode->i_mode))
1324                                         problem = PR_1_RESERVED_BAD_MODE;
1325                         } else if (ino == EXT2_RESIZE_INO) {
1326                                 if (inode->i_mode &&
1327                                     !LINUX_S_ISREG(inode->i_mode))
1328                                         problem = PR_1_RESERVED_BAD_MODE;
1329                         } else {
1330                                 if (inode->i_mode != 0)
1331                                         problem = PR_1_RESERVED_BAD_MODE;
1332                         }
1333                         if (problem) {
1334                                 if (fix_problem(ctx, problem, &pctx)) {
1335                                         inode->i_mode = 0;
1336                                         e2fsck_write_inode(ctx, ino, inode,
1337                                                            "pass1");
1338                                 }
1339                         }
1340                         check_blocks(ctx, &pctx, block_buf);
1341                         continue;
1342                 }
1343
1344                 /*
1345                  * Check for inodes who might have been part of the
1346                  * orphaned list linked list.  They should have gotten
1347                  * dealt with by now, unless the list had somehow been
1348                  * corrupted.
1349                  *
1350                  * FIXME: In the future, inodes which are still in use
1351                  * (and which are therefore) pending truncation should
1352                  * be handled specially.  Right now we just clear the
1353                  * dtime field, and the normal e2fsck handling of
1354                  * inodes where i_size and the inode blocks are
1355                  * inconsistent is to fix i_size, instead of releasing
1356                  * the extra blocks.  This won't catch the inodes that
1357                  * was at the end of the orphan list, but it's better
1358                  * than nothing.  The right answer is that there
1359                  * shouldn't be any bugs in the orphan list handling.  :-)
1360                  */
1361                 if (inode->i_dtime && low_dtime_check &&
1362                     inode->i_dtime < ctx->fs->super->s_inodes_count) {
1363                         if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1364                                 inode->i_dtime = inode->i_links_count ?
1365                                         0 : ctx->now;
1366                                 e2fsck_write_inode(ctx, ino, inode,
1367                                                    "pass1");
1368                         }
1369                 }
1370
1371                 /*
1372                  * This code assumes that deleted inodes have
1373                  * i_links_count set to 0.
1374                  */
1375                 if (!inode->i_links_count) {
1376                         if (!inode->i_dtime && inode->i_mode) {
1377                                 if (fix_problem(ctx,
1378                                             PR_1_ZERO_DTIME, &pctx)) {
1379                                         inode->i_dtime = ctx->now;
1380                                         e2fsck_write_inode(ctx, ino, inode,
1381                                                            "pass1");
1382                                 }
1383                         }
1384                         continue;
1385                 }
1386                 /*
1387                  * n.b.  0.3c ext2fs code didn't clear i_links_count for
1388                  * deleted files.  Oops.
1389                  *
1390                  * Since all new ext2 implementations get this right,
1391                  * we now assume that the case of non-zero
1392                  * i_links_count and non-zero dtime means that we
1393                  * should keep the file, not delete it.
1394                  *
1395                  */
1396                 if (inode->i_dtime) {
1397                         e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1398                         if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1399                                 inode->i_dtime = 0;
1400                                 e2fsck_write_inode(ctx, ino, inode, "pass1");
1401                         }
1402                 }
1403
1404                 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1405                 switch (fs->super->s_creator_os) {
1406                     case EXT2_OS_HURD:
1407                         frag = inode->osd2.hurd2.h_i_frag;
1408                         fsize = inode->osd2.hurd2.h_i_fsize;
1409                         break;
1410                     default:
1411                         frag = fsize = 0;
1412                 }
1413
1414                 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1415                 if (inode->i_faddr || frag || fsize ||
1416                     (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
1417                         e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1418                 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1419                     !(fs->super->s_feature_incompat &
1420                       EXT4_FEATURE_INCOMPAT_64BIT) &&
1421                     inode->osd2.linux2.l_i_file_acl_high != 0)
1422                         e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1423                 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1424                     !(fs->super->s_feature_ro_compat &
1425                       EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1426                     (inode->osd2.linux2.l_i_blocks_hi != 0))
1427                         e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1428                 if (inode->i_flags & EXT2_IMAGIC_FL) {
1429                         if (imagic_fs) {
1430                                 if (!ctx->inode_imagic_map)
1431                                         alloc_imagic_map(ctx);
1432                                 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1433                                                          ino);
1434                         } else {
1435                                 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1436                                 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1437                                         inode->i_flags &= ~EXT2_IMAGIC_FL;
1438                                         e2fsck_write_inode(ctx, ino,
1439                                                            inode, "pass1");
1440                                 }
1441                         }
1442                 }
1443
1444                 check_inode_extra_space(ctx, &pctx);
1445                 check_is_really_dir(ctx, &pctx, block_buf);
1446
1447                 /*
1448                  * ext2fs_inode_has_valid_blocks2 does not actually look
1449                  * at i_block[] values, so not endian-sensitive here.
1450                  */
1451                 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1452                     LINUX_S_ISLNK(inode->i_mode) &&
1453                     !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1454                     fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1455                         inode->i_flags &= ~EXT4_EXTENTS_FL;
1456                         e2fsck_write_inode(ctx, ino, inode, "pass1");
1457                 }
1458
1459                 if (LINUX_S_ISDIR(inode->i_mode)) {
1460                         ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1461                         e2fsck_add_dir_info(ctx, ino, 0);
1462                         ctx->fs_directory_count++;
1463                 } else if (LINUX_S_ISREG (inode->i_mode)) {
1464                         ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1465                         ctx->fs_regular_count++;
1466                 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1467                            e2fsck_pass1_check_device_inode(fs, inode)) {
1468                         check_immutable(ctx, &pctx);
1469                         check_size(ctx, &pctx);
1470                         ctx->fs_chardev_count++;
1471                 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1472                            e2fsck_pass1_check_device_inode(fs, inode)) {
1473                         check_immutable(ctx, &pctx);
1474                         check_size(ctx, &pctx);
1475                         ctx->fs_blockdev_count++;
1476                 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1477                            check_symlink(ctx, &pctx, ino, inode, block_buf)) {
1478                         check_immutable(ctx, &pctx);
1479                         ctx->fs_symlinks_count++;
1480                         if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1481                                 ctx->fs_fast_symlinks_count++;
1482                                 check_blocks(ctx, &pctx, block_buf);
1483                                 continue;
1484                         }
1485                 }
1486                 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1487                          e2fsck_pass1_check_device_inode(fs, inode)) {
1488                         check_immutable(ctx, &pctx);
1489                         check_size(ctx, &pctx);
1490                         ctx->fs_fifo_count++;
1491                 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1492                            e2fsck_pass1_check_device_inode(fs, inode)) {
1493                         check_immutable(ctx, &pctx);
1494                         check_size(ctx, &pctx);
1495                         ctx->fs_sockets_count++;
1496                 } else {
1497                         e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1498                 }
1499
1500                 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_atime, ctx->time_fudge))
1501                         e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1502                 else if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_mtime,
1503                                            ctx->time_fudge))
1504                         e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1505
1506                 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_ctime, ctx->time_fudge))
1507                         e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1508                 else if (EXT4_XTIME_ANCIENT(ctx, sb, inode->i_ctime,
1509                                             ctx->time_fudge))
1510                         e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1511
1512                 /* i_crtime is checked in check_inode_extra_space() */
1513
1514                 if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
1515                         if (inode->i_block[EXT2_IND_BLOCK])
1516                                 ctx->fs_ind_count++;
1517                         if (inode->i_block[EXT2_DIND_BLOCK])
1518                                 ctx->fs_dind_count++;
1519                         if (inode->i_block[EXT2_TIND_BLOCK])
1520                                 ctx->fs_tind_count++;
1521                 }
1522                 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1523                     (inode->i_block[EXT2_IND_BLOCK] ||
1524                      inode->i_block[EXT2_DIND_BLOCK] ||
1525                      inode->i_block[EXT2_TIND_BLOCK] ||
1526                      ext2fs_file_acl_block(fs, inode))) {
1527                         inodes_to_process[process_inode_count].ino = ino;
1528                         inodes_to_process[process_inode_count].inode = *inode;
1529                         process_inode_count++;
1530                 } else
1531                         check_blocks(ctx, &pctx, block_buf);
1532
1533                 if (ctx->flags & E2F_FLAG_EXPAND_EISIZE) {
1534                         struct ext2_inode_large *inode_l;
1535
1536                         inode_l = (struct ext2_inode_large *)inode;
1537
1538                         if (inode_l->i_extra_isize < ctx->want_extra_isize) {
1539                                 fix_problem(ctx, PR_1_EXPAND_EISIZE, &pctx);
1540                                 inode_exp = e2fsck_pass1_expand_eisize(ctx,
1541                                                                        inode_l,
1542                                                                        &pctx);
1543                         }
1544                         if ((inode_l->i_extra_isize < ctx->min_extra_isize) &&
1545                             inode_exp == 0)
1546                                 ctx->min_extra_isize = inode_l->i_extra_isize;
1547                 }
1548
1549                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1550                         goto endit;
1551
1552                 if (process_inode_count >= ctx->process_inode_size) {
1553                         process_inodes(ctx, block_buf);
1554
1555                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1556                                 goto endit;
1557                 }
1558         }
1559         process_inodes(ctx, block_buf);
1560         ext2fs_close_inode_scan(scan);
1561         scan = NULL;
1562
1563         reserve_block_for_root_repair(ctx);
1564         reserve_block_for_lnf_repair(ctx);
1565
1566         /*
1567          * If any extended attribute blocks' reference counts need to
1568          * be adjusted, either up (ctx->refcount_extra), or down
1569          * (ctx->refcount), then fix them.
1570          */
1571         if (ctx->refcount) {
1572                 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1573                 ea_refcount_free(ctx->refcount);
1574                 ctx->refcount = 0;
1575         }
1576         if (ctx->refcount_extra) {
1577                 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1578                                         block_buf, +1);
1579                 ea_refcount_free(ctx->refcount_extra);
1580                 ctx->refcount_extra = 0;
1581         }
1582
1583         if (ctx->invalid_bitmaps)
1584                 handle_fs_bad_blocks(ctx);
1585
1586         /* We don't need the block_ea_map any more */
1587         if (ctx->block_ea_map) {
1588                 ext2fs_free_block_bitmap(ctx->block_ea_map);
1589                 ctx->block_ea_map = 0;
1590         }
1591
1592         if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1593                 clear_problem_context(&pctx);
1594                 pctx.errcode = ext2fs_create_resize_inode(fs);
1595                 if (pctx.errcode) {
1596                         if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1597                                          &pctx)) {
1598                                 ctx->flags |= E2F_FLAG_ABORT;
1599                                 goto endit;
1600                         }
1601                         pctx.errcode = 0;
1602                 }
1603                 if (!pctx.errcode) {
1604                         e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1605                                           "recreate inode");
1606                         inode->i_mtime = ctx->now;
1607                         e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1608                                            "recreate inode");
1609                 }
1610                 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1611         }
1612
1613         if (ctx->flags & E2F_FLAG_RESTART) {
1614                 /*
1615                  * Only the master copy of the superblock and block
1616                  * group descriptors are going to be written during a
1617                  * restart, so set the superblock to be used to be the
1618                  * master superblock.
1619                  */
1620                 ctx->use_superblock = 0;
1621                 unwind_pass1(fs);
1622                 goto endit;
1623         }
1624
1625         if (ctx->block_dup_map) {
1626                 if (ctx->options & E2F_OPT_PREEN) {
1627                         clear_problem_context(&pctx);
1628                         fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1629                 }
1630                 e2fsck_pass1_dupblocks(ctx, block_buf);
1631         }
1632         ext2fs_free_mem(&inodes_to_process);
1633 endit:
1634         e2fsck_use_inode_shortcuts(ctx, 0);
1635
1636         if (scan)
1637                 ext2fs_close_inode_scan(scan);
1638         if (block_buf)
1639                 ext2fs_free_mem(&block_buf);
1640         if (inode)
1641                 ext2fs_free_mem(&inode);
1642
1643         if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
1644                 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
1645         else
1646                 ctx->invalid_bitmaps++;
1647 }
1648
1649 /*
1650  * When the inode_scan routines call this callback at the end of the
1651  * glock group, call process_inodes.
1652  */
1653 static errcode_t scan_callback(ext2_filsys fs,
1654                                ext2_inode_scan scan EXT2FS_ATTR((unused)),
1655                                dgrp_t group, void * priv_data)
1656 {
1657         struct scan_callback_struct *scan_struct;
1658         e2fsck_t ctx;
1659
1660         scan_struct = (struct scan_callback_struct *) priv_data;
1661         ctx = scan_struct->ctx;
1662
1663         process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1664
1665         if (ctx->progress)
1666                 if ((ctx->progress)(ctx, 1, group+1,
1667                                     ctx->fs->group_desc_count))
1668                         return EXT2_ET_CANCEL_REQUESTED;
1669
1670         return 0;
1671 }
1672
1673 /*
1674  * Process the inodes in the "inodes to process" list.
1675  */
1676 static void process_inodes(e2fsck_t ctx, char *block_buf)
1677 {
1678         int                     i;
1679         struct ext2_inode       *old_stashed_inode;
1680         ext2_ino_t              old_stashed_ino;
1681         const char              *old_operation;
1682         char                    buf[80];
1683         struct problem_context  pctx;
1684
1685 #if 0
1686         printf("begin process_inodes: ");
1687 #endif
1688         if (process_inode_count == 0)
1689                 return;
1690         old_operation = ehandler_operation(0);
1691         old_stashed_inode = ctx->stashed_inode;
1692         old_stashed_ino = ctx->stashed_ino;
1693         qsort(inodes_to_process, process_inode_count,
1694                       sizeof(struct process_inode_block), process_inode_cmp);
1695         clear_problem_context(&pctx);
1696         for (i=0; i < process_inode_count; i++) {
1697                 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1698                 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1699
1700 #if 0
1701                 printf("%u ", pctx.ino);
1702 #endif
1703                 sprintf(buf, _("reading indirect blocks of inode %u"),
1704                         pctx.ino);
1705                 ehandler_operation(buf);
1706                 check_blocks(ctx, &pctx, block_buf);
1707                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1708                         break;
1709         }
1710         ctx->stashed_inode = old_stashed_inode;
1711         ctx->stashed_ino = old_stashed_ino;
1712         process_inode_count = 0;
1713 #if 0
1714         printf("end process inodes\n");
1715 #endif
1716         ehandler_operation(old_operation);
1717 }
1718
1719 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1720 {
1721         const struct process_inode_block *ib_a =
1722                 (const struct process_inode_block *) a;
1723         const struct process_inode_block *ib_b =
1724                 (const struct process_inode_block *) b;
1725         int     ret;
1726
1727         ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1728                ib_b->inode.i_block[EXT2_IND_BLOCK]);
1729         if (ret == 0)
1730                 /*
1731                  * We only call process_inodes() for non-extent
1732                  * inodes, so it's OK to pass NULL to
1733                  * ext2fs_file_acl_block() here.
1734                  */
1735                 ret = ext2fs_file_acl_block(0, &(ib_a->inode)) -
1736                         ext2fs_file_acl_block(0, &(ib_b->inode));
1737         if (ret == 0)
1738                 ret = ib_a->ino - ib_b->ino;
1739         return ret;
1740 }
1741
1742 /*
1743  * Mark an inode as being bad and increment its badness counter.
1744  */
1745 void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ino_t ino, int count,
1746                                const char *func, const int line)
1747 {
1748         struct          problem_context pctx;
1749         __u16           result;
1750
1751         if (!ctx->inode_badness) {
1752                 clear_problem_context(&pctx);
1753
1754                 pctx.errcode = ext2fs_create_icount2(ctx->fs, 0, 0, NULL,
1755                                                      &ctx->inode_badness);
1756                 if (pctx.errcode) {
1757                         fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1758                         ctx->flags |= E2F_FLAG_ABORT;
1759                         return;
1760                 }
1761         }
1762         ext2fs_icount_fetch(ctx->inode_badness, ino, &result);
1763         ext2fs_icount_store(ctx->inode_badness, ino, count + result);
1764
1765         if (ctx->options & E2F_OPT_DEBUG)
1766                 fprintf(stderr, "%s:%d: increase inode %lu badness %u to %u\n",
1767                         func, line, (unsigned long)ino, result, count + result);
1768 }
1769
1770
1771 /*
1772  * This procedure will allocate the inode "bb" (badblock) map table
1773  */
1774 static void alloc_bb_map(e2fsck_t ctx)
1775 {
1776         struct          problem_context pctx;
1777
1778         clear_problem_context(&pctx);
1779         pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1780                         _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
1781                         "inode_bb_map", &ctx->inode_bb_map);
1782         if (pctx.errcode) {
1783                 pctx.num = 4;
1784                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1785                 /* Should never get here */
1786                 ctx->flags |= E2F_FLAG_ABORT;
1787                 return;
1788         }
1789 }
1790
1791 /*
1792  * This procedure will allocate the inode imagic table
1793  */
1794 static void alloc_imagic_map(e2fsck_t ctx)
1795 {
1796         struct          problem_context pctx;
1797
1798         clear_problem_context(&pctx);
1799         pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1800                         _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
1801                         "inode_imagic_map", &ctx->inode_imagic_map);
1802         if (pctx.errcode) {
1803                 pctx.num = 5;
1804                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1805                 /* Should never get here */
1806                 ctx->flags |= E2F_FLAG_ABORT;
1807                 return;
1808         }
1809 }
1810
1811 /*
1812  * Marks a block as in use, setting the dup_map if it's been set
1813  * already.  Called by process_block and process_bad_block.
1814  *
1815  * WARNING: Assumes checks have already been done to make sure block
1816  * is valid.  This is true in both process_block and process_bad_block.
1817  */
1818 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
1819 {
1820         struct          problem_context pctx;
1821
1822         clear_problem_context(&pctx);
1823
1824         if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
1825                 if (!ctx->block_dup_map) {
1826                         pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1827                                         _("multiply claimed block map"),
1828                                         EXT2FS_BMAP64_RBTREE, "block_dup_map",
1829                                         &ctx->block_dup_map);
1830                         if (pctx.errcode) {
1831                                 pctx.num = 3;
1832                                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1833                                             &pctx);
1834                                 /* Should never get here */
1835                                 ctx->flags |= E2F_FLAG_ABORT;
1836                                 return;
1837                         }
1838                 }
1839                 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
1840         } else {
1841                 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
1842         }
1843 }
1844
1845 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
1846                                       unsigned int num)
1847 {
1848         if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
1849                 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
1850         else
1851                 while (num--)
1852                         mark_block_used(ctx, block++);
1853 }
1854
1855 /*
1856  * Adjust the extended attribute block's reference counts at the end
1857  * of pass 1, either by subtracting out references for EA blocks that
1858  * are still referenced in ctx->refcount, or by adding references for
1859  * EA blocks that had extra references as accounted for in
1860  * ctx->refcount_extra.
1861  */
1862 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
1863                                     char *block_buf, int adjust_sign)
1864 {
1865         struct ext2_ext_attr_header     *header;
1866         struct problem_context          pctx;
1867         ext2_filsys                     fs = ctx->fs;
1868         blk64_t                         blk;
1869         __u32                           should_be;
1870         int                             count;
1871
1872         clear_problem_context(&pctx);
1873
1874         ea_refcount_intr_begin(refcount);
1875         while (1) {
1876                 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1877                         break;
1878                 pctx.blk = blk;
1879                 pctx.errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1880                 /* We already checked this block, shouldn't happen */
1881                 if (pctx.errcode) {
1882                         fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1883                         return;
1884                 }
1885                 header = BHDR(block_buf);
1886                 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1887                         fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1888                         return;
1889                 }
1890
1891                 pctx.blkcount = header->h_refcount;
1892                 should_be = header->h_refcount + adjust_sign * count;
1893                 pctx.num = should_be;
1894                 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1895                         header->h_refcount = should_be;
1896                         pctx.errcode = ext2fs_write_ext_attr2(fs, blk,
1897                                                              block_buf);
1898                         if (pctx.errcode) {
1899                                 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
1900                                             &pctx);
1901                                 continue;
1902                         }
1903                 }
1904         }
1905 }
1906
1907 /*
1908  * Handle processing the extended attribute blocks
1909  */
1910 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1911                            char *block_buf)
1912 {
1913         ext2_filsys fs = ctx->fs;
1914         ext2_ino_t      ino = pctx->ino;
1915         struct ext2_inode *inode = pctx->inode;
1916         blk64_t         blk;
1917         char *          end;
1918         struct ext2_ext_attr_header *header;
1919         struct ext2_ext_attr_entry *entry;
1920         int             count;
1921         region_t        region = 0;
1922         int ret;
1923
1924         blk = ext2fs_file_acl_block(fs, inode);
1925         if (blk == 0)
1926                 return 0;
1927
1928         /*
1929          * If the Extended attribute flag isn't set, then a non-zero
1930          * file acl means that the inode is corrupted.
1931          *
1932          * Or if the extended attribute block is an invalid block,
1933          * then the inode is also corrupted.
1934          */
1935         if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1936             (blk < fs->super->s_first_data_block) ||
1937             (blk >= ext2fs_blocks_count(fs->super))) {
1938                 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1939                 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1940                 return 0;
1941         }
1942
1943         /* If ea bitmap hasn't been allocated, create it */
1944         if (!ctx->block_ea_map) {
1945                 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
1946                                         _("ext attr block map"),
1947                                         EXT2FS_BMAP64_RBTREE, "block_ea_map",
1948                                         &ctx->block_ea_map);
1949                 if (pctx->errcode) {
1950                         pctx->num = 2;
1951                         fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1952                         ctx->flags |= E2F_FLAG_ABORT;
1953                         return 0;
1954                 }
1955         }
1956
1957         /* Create the EA refcount structure if necessary */
1958         if (!ctx->refcount) {
1959                 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1960                 if (pctx->errcode) {
1961                         pctx->num = 1;
1962                         fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1963                         ctx->flags |= E2F_FLAG_ABORT;
1964                         return 0;
1965                 }
1966         }
1967
1968 #if 0
1969         /* Debugging text */
1970         printf("Inode %u has EA block %u\n", ino, blk);
1971 #endif
1972
1973         /* Have we seen this EA block before? */
1974         if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
1975                 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1976                         return 1;
1977                 /* Ooops, this EA was referenced more than it stated */
1978                 if (!ctx->refcount_extra) {
1979                         pctx->errcode = ea_refcount_create(0,
1980                                            &ctx->refcount_extra);
1981                         if (pctx->errcode) {
1982                                 pctx->num = 2;
1983                                 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1984                                 ctx->flags |= E2F_FLAG_ABORT;
1985                                 return 0;
1986                         }
1987                 }
1988                 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1989                 return 1;
1990         }
1991
1992         /*
1993          * OK, we haven't seen this EA block yet.  So we need to
1994          * validate it
1995          */
1996         pctx->blk = blk;
1997         pctx->errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1998         if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1999                 goto clear_extattr;
2000         header = BHDR(block_buf);
2001         pctx->blk = ext2fs_file_acl_block(fs, inode);
2002         if (((ctx->ext_attr_ver == 1) &&
2003              (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
2004             ((ctx->ext_attr_ver == 2) &&
2005              (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
2006                 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
2007                         goto clear_extattr;
2008         }
2009
2010         if (header->h_blocks != 1) {
2011                 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
2012                         goto clear_extattr;
2013         }
2014
2015         region = region_create(0, fs->blocksize);
2016         if (!region) {
2017                 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
2018                 ctx->flags |= E2F_FLAG_ABORT;
2019                 return 0;
2020         }
2021         if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
2022                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2023                         goto clear_extattr;
2024         }
2025
2026         entry = (struct ext2_ext_attr_entry *)(header+1);
2027         end = block_buf + fs->blocksize;
2028         while ((char *)entry < end && *(__u32 *)entry) {
2029                 __u32 hash;
2030
2031                 if (region_allocate(region, (char *)entry - (char *)header,
2032                                    EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
2033                         if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2034                                 goto clear_extattr;
2035                         break;
2036                 }
2037                 if ((ctx->ext_attr_ver == 1 &&
2038                      (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
2039                     (ctx->ext_attr_ver == 2 &&
2040                      entry->e_name_index == 0)) {
2041                         if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
2042                                 goto clear_extattr;
2043                         break;
2044                 }
2045                 if (entry->e_value_inum == 0) {
2046                         if (entry->e_value_offs + entry->e_value_size >
2047                             fs->blocksize) {
2048                                 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
2049                                         goto clear_extattr;
2050                                 break;
2051                         }
2052                         if (entry->e_value_size &&
2053                             region_allocate(region, entry->e_value_offs,
2054                                             EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
2055                                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
2056                                                 pctx))
2057                                         goto clear_extattr;
2058                         }
2059                 } else {
2060                         int i_file_acl_deleted = 0;
2061
2062                         ret = check_large_ea_inode(ctx, entry, pctx,
2063                                                    &i_file_acl_deleted);
2064                         if (ret == 0)
2065                                 mark_inode_ea_map(ctx, pctx,
2066                                                   entry->e_value_inum);
2067
2068                         if (i_file_acl_deleted)
2069                                 goto clear_extattr;
2070                 }
2071
2072                 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
2073                                                          entry->e_value_offs);
2074
2075                 if (entry->e_hash != hash) {
2076                         pctx->num = entry->e_hash;
2077                         if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
2078                                 goto clear_extattr;
2079                         entry->e_hash = hash;
2080                 }
2081
2082                 entry = EXT2_EXT_ATTR_NEXT(entry);
2083         }
2084         if (region_allocate(region, (char *)entry - (char *)header, 4)) {
2085                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2086                         goto clear_extattr;
2087         }
2088         region_free(region);
2089
2090         count = header->h_refcount - 1;
2091         if (count)
2092                 ea_refcount_store(ctx->refcount, blk, count);
2093         mark_block_used(ctx, blk);
2094         ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
2095         return 1;
2096
2097 clear_extattr:
2098         if (region)
2099                 region_free(region);
2100         ext2fs_file_acl_block_set(fs, inode, 0);
2101         e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
2102         return 0;
2103 }
2104
2105 /* Returns 1 if bad htree, 0 if OK */
2106 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
2107                         ext2_ino_t ino, struct ext2_inode *inode,
2108                         char *block_buf)
2109 {
2110         struct ext2_dx_root_info        *root;
2111         ext2_filsys                     fs = ctx->fs;
2112         errcode_t                       retval;
2113         blk64_t                         blk;
2114
2115         if ((!LINUX_S_ISDIR(inode->i_mode) &&
2116              fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
2117             (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX))) {
2118                 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2119                 if (fix_problem(ctx, PR_1_HTREE_SET, pctx))
2120                         return 1;
2121         }
2122
2123         pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
2124
2125         if ((pctx->errcode) ||
2126             (blk == 0) ||
2127             (blk < fs->super->s_first_data_block) ||
2128             (blk >= ext2fs_blocks_count(fs->super))) {
2129                 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2130                 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2131                         return 1;
2132                 else
2133                         return 0;
2134         }
2135
2136         retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
2137         if (retval) {
2138                 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2139                 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2140                         return 1;
2141         }
2142
2143         /* XXX should check that beginning matches a directory */
2144         root = get_ext2_dx_root_info(fs, block_buf);
2145
2146         if ((root->reserved_zero || root->info_length < 8) &&
2147             fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2148                 return 1;
2149
2150         pctx->num = root->hash_version;
2151         if ((root->hash_version != EXT2_HASH_LEGACY) &&
2152             (root->hash_version != EXT2_HASH_HALF_MD4) &&
2153             (root->hash_version != EXT2_HASH_TEA) &&
2154             fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
2155                 return 1;
2156
2157         if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
2158             fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
2159                 return 1;
2160
2161         pctx->num = root->indirect_levels;
2162         if ((root->indirect_levels > 1) &&
2163             fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
2164                 return 1;
2165
2166         return 0;
2167 }
2168
2169 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
2170                         struct ext2_inode *inode, int restart_flag,
2171                         const char *source)
2172 {
2173         inode->i_flags = 0;
2174         inode->i_links_count = 0;
2175         ext2fs_icount_store(ctx->inode_link_info, ino, 0);
2176         inode->i_dtime = ctx->now;
2177
2178         ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
2179         ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
2180         if (ctx->inode_reg_map)
2181                 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
2182         if (ctx->inode_badness)
2183                 ext2fs_icount_store(ctx->inode_badness, ino, 0);
2184
2185         /*
2186          * If the inode was partially accounted for before processing
2187          * was aborted, we need to restart the pass 1 scan.
2188          */
2189         ctx->flags |= restart_flag;
2190
2191         if (ino == EXT2_BAD_INO)
2192                 memset(inode, 0, sizeof(struct ext2_inode));
2193
2194         e2fsck_write_inode(ctx, ino, inode, source);
2195 }
2196
2197 /*
2198  * Use the multiple-blocks reclamation code to fix alignment problems in
2199  * a bigalloc filesystem.  We want a logical cluster to map to *only* one
2200  * physical cluster, and we want the block offsets within that cluster to
2201  * line up.
2202  */
2203 static int has_unaligned_cluster_map(e2fsck_t ctx,
2204                                      blk64_t last_pblk, e2_blkcnt_t last_lblk,
2205                                      blk64_t pblk, blk64_t lblk)
2206 {
2207         blk64_t cluster_mask;
2208
2209         if (!ctx->fs->cluster_ratio_bits)
2210                 return 0;
2211         cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
2212
2213         /*
2214          * If the block in the logical cluster doesn't align with the block in
2215          * the physical cluster...
2216          */
2217         if ((lblk & cluster_mask) != (pblk & cluster_mask))
2218                 return 1;
2219
2220         /*
2221          * If we cross a physical cluster boundary within a logical cluster...
2222          */
2223         if (last_pblk && (lblk & cluster_mask) != 0 &&
2224             EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
2225             EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
2226                 return 1;
2227
2228         return 0;
2229 }
2230
2231 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
2232                              struct process_block_struct *pb,
2233                              blk64_t start_block, blk64_t end_block,
2234                              blk64_t eof_block,
2235                              ext2_extent_handle_t ehandle)
2236 {
2237         struct ext2fs_extent    extent;
2238         blk64_t                 blk, last_lblk;
2239         e2_blkcnt_t             blockcnt;
2240         unsigned int            i;
2241         int                     is_dir, is_leaf;
2242         problem_t               problem;
2243         struct ext2_extent_info info;
2244
2245         pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
2246         if (pctx->errcode)
2247                 return;
2248
2249         pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
2250                                           &extent);
2251         while (!pctx->errcode && info.num_entries-- > 0) {
2252                 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
2253                 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
2254                 last_lblk = extent.e_lblk + extent.e_len - 1;
2255
2256                 problem = 0;
2257                 if (extent.e_pblk == 0 ||
2258                     extent.e_pblk < ctx->fs->super->s_first_data_block ||
2259                     extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
2260                         problem = PR_1_EXTENT_BAD_START_BLK;
2261                 else if (extent.e_lblk < start_block)
2262                         problem = PR_1_OUT_OF_ORDER_EXTENTS;
2263                 else if ((end_block && last_lblk > end_block) &&
2264                          (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT &&
2265                                 last_lblk > eof_block)))
2266                         problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
2267                 else if (is_leaf && extent.e_len == 0)
2268                         problem = PR_1_EXTENT_LENGTH_ZERO;
2269                 else if (is_leaf &&
2270                          (extent.e_pblk + extent.e_len) >
2271                          ext2fs_blocks_count(ctx->fs->super))
2272                         problem = PR_1_EXTENT_ENDS_BEYOND;
2273                 else if (is_leaf && is_dir &&
2274                          ((extent.e_lblk + extent.e_len) >
2275                           (1 << (21 - ctx->fs->super->s_log_block_size))))
2276                         problem = PR_1_TOOBIG_DIR;
2277
2278                 /*
2279                  * Uninitialized blocks in a directory?  Clear the flag and
2280                  * we'll interpret the blocks later.
2281                  */
2282                 if (is_dir && problem == 0 &&
2283                     (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
2284                     fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
2285                         extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
2286                         pb->inode_modified = 1;
2287                         pctx->errcode = ext2fs_extent_replace(ehandle, 0,
2288                                                               &extent);
2289                         if (pctx->errcode)
2290                                 return;
2291                 }
2292
2293                 if (problem) {
2294                         /* To ensure that extent is in inode */
2295                         if (info.curr_level == 0)
2296                                 e2fsck_mark_inode_bad(ctx, pctx->ino,
2297                                                       BADNESS_HIGH);
2298 report_problem:
2299                         pctx->blk = extent.e_pblk;
2300                         pctx->blk2 = extent.e_lblk;
2301                         pctx->num = extent.e_len;
2302                         pctx->blkcount = extent.e_lblk + extent.e_len;
2303                         if (fix_problem(ctx, problem, pctx)) {
2304                                 if (ctx->invalid_bitmaps) {
2305                                         /*
2306                                          * If fsck knows the bitmaps are bad,
2307                                          * skip to the next extent and
2308                                          * try to clear this extent again
2309                                          * after fixing the bitmaps, by
2310                                          * restarting fsck.
2311                                          */
2312                                         pctx->errcode = ext2fs_extent_get(
2313                                                           ehandle,
2314                                                           EXT2_EXTENT_NEXT_SIB,
2315                                                           &extent);
2316                                         ctx->flags |= E2F_FLAG_RESTART_LATER;
2317                                         if (pctx->errcode ==
2318                                                     EXT2_ET_NO_CURRENT_NODE) {
2319                                                 pctx->errcode = 0;
2320                                                 break;
2321                                         }
2322                                         continue;
2323                                 }
2324                                 e2fsck_read_bitmaps(ctx);
2325                                 pb->inode_modified = 1;
2326                                 pctx->errcode =
2327                                         ext2fs_extent_delete(ehandle, 0);
2328                                 if (pctx->errcode) {
2329                                         pctx->str = "ext2fs_extent_delete";
2330                                         return;
2331                                 }
2332                                 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2333                                 if (pctx->errcode &&
2334                                     pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
2335                                         pctx->str = "ext2fs_extent_fix_parents";
2336                                         return;
2337                                 }
2338                                 pctx->errcode = ext2fs_extent_get(ehandle,
2339                                                                   EXT2_EXTENT_CURRENT,
2340                                                                   &extent);
2341                                 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
2342                                         pctx->errcode = 0;
2343                                         break;
2344                                 }
2345                                 continue;
2346                         }
2347                         goto next;
2348                 }
2349
2350                 if (!is_leaf) {
2351                         blk64_t lblk = extent.e_lblk;
2352
2353                         blk = extent.e_pblk;
2354                         pctx->errcode = ext2fs_extent_get(ehandle,
2355                                                   EXT2_EXTENT_DOWN, &extent);
2356                         if (pctx->errcode) {
2357                                 pctx->str = "EXT2_EXTENT_DOWN";
2358                                 problem = PR_1_EXTENT_HEADER_INVALID;
2359                                 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
2360                                         goto report_problem;
2361                                 return;
2362                         }
2363                         /* The next extent should match this index's logical start */
2364                         if (extent.e_lblk != lblk) {
2365                                 struct ext2_extent_info e_info;
2366
2367                                 ext2fs_extent_get_info(ehandle, &e_info);
2368                                 pctx->blk = lblk;
2369                                 pctx->blk2 = extent.e_lblk;
2370                                 pctx->num = e_info.curr_level - 1;
2371                                 problem = PR_1_EXTENT_INDEX_START_INVALID;
2372                                 if (fix_problem(ctx, problem, pctx)) {
2373                                         pb->inode_modified = 1;
2374                                         pctx->errcode =
2375                                                 ext2fs_extent_fix_parents(ehandle);
2376                                         if (pctx->errcode) {
2377                                                 pctx->str = "ext2fs_extent_fix_parents";
2378                                                 return;
2379                                         }
2380                                 }
2381                         }
2382                         scan_extent_node(ctx, pctx, pb, extent.e_lblk,
2383                                          last_lblk, eof_block, ehandle);
2384                         if (pctx->errcode)
2385                                 return;
2386                         pctx->errcode = ext2fs_extent_get(ehandle,
2387                                                   EXT2_EXTENT_UP, &extent);
2388                         if (pctx->errcode) {
2389                                 pctx->str = "EXT2_EXTENT_UP";
2390                                 return;
2391                         }
2392                         mark_block_used(ctx, blk);
2393                         pb->num_blocks++;
2394                         goto next;
2395                 }
2396
2397                 if ((pb->previous_block != 0) &&
2398                     (pb->previous_block+1 != extent.e_pblk)) {
2399                         if (ctx->options & E2F_OPT_FRAGCHECK) {
2400                                 char type = '?';
2401
2402                                 if (pb->is_dir)
2403                                         type = 'd';
2404                                 else if (pb->is_reg)
2405                                         type = 'f';
2406
2407                                 printf(("%6lu(%c): expecting %6lu "
2408                                         "actual extent "
2409                                         "phys %6lu log %lu len %lu\n"),
2410                                        (unsigned long) pctx->ino, type,
2411                                        (unsigned long) pb->previous_block+1,
2412                                        (unsigned long) extent.e_pblk,
2413                                        (unsigned long) extent.e_lblk,
2414                                        (unsigned long) extent.e_len);
2415                         }
2416                         pb->fragmented = 1;
2417                 }
2418                 /*
2419                  * If we notice a gap in the logical block mappings of an
2420                  * extent-mapped directory, offer to close the hole by
2421                  * moving the logical block down, otherwise we'll go mad in
2422                  * pass 3 allocating empty directory blocks to fill the hole.
2423                  */
2424                 if (is_dir &&
2425                     pb->last_block + 1 < (e2_blkcnt_t)extent.e_lblk) {
2426                         blk64_t new_lblk;
2427
2428                         new_lblk = pb->last_block + 1;
2429                         if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
2430                                 new_lblk = ((new_lblk +
2431                                              EXT2FS_CLUSTER_RATIO(ctx->fs)) &
2432                                             EXT2FS_CLUSTER_MASK(ctx->fs)) |
2433                                            (extent.e_lblk &
2434                                             EXT2FS_CLUSTER_MASK(ctx->fs));
2435                         pctx->blk = extent.e_lblk;
2436                         pctx->blk2 = new_lblk;
2437                         if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
2438                                 extent.e_lblk = new_lblk;
2439                                 pb->inode_modified = 1;
2440                                 pctx->errcode = ext2fs_extent_replace(ehandle,
2441                                                                 0, &extent);
2442                                 if (pctx->errcode) {
2443                                         pctx->errcode = 0;
2444                                         goto alloc_later;
2445                                 }
2446                                 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2447                                 if (pctx->errcode)
2448                                         goto failed_add_dir_block;
2449                                 pctx->errcode = ext2fs_extent_goto(ehandle,
2450                                                                 extent.e_lblk);
2451                                 if (pctx->errcode)
2452                                         goto failed_add_dir_block;
2453                                 last_lblk = extent.e_lblk + extent.e_len - 1;
2454                         }
2455                 }
2456 alloc_later:
2457                 while (is_dir && (++pb->last_db_block <
2458                                   (e2_blkcnt_t) extent.e_lblk)) {
2459                         pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
2460                                                               pb->ino, 0,
2461                                                               pb->last_db_block);
2462                         if (pctx->errcode) {
2463                                 pctx->blk = 0;
2464                                 pctx->num = pb->last_db_block;
2465                                 goto failed_add_dir_block;
2466                         }
2467                 }
2468                 if (!ctx->fs->cluster_ratio_bits) {
2469                         mark_blocks_used(ctx, extent.e_pblk, extent.e_len);
2470                         pb->num_blocks += extent.e_len;
2471                 }
2472                 for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
2473                      i < extent.e_len;
2474                      blk++, blockcnt++, i++) {
2475                         if (ctx->fs->cluster_ratio_bits &&
2476                             !(pb->previous_block &&
2477                               (EXT2FS_B2C(ctx->fs, blk) ==
2478                                EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
2479                               (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2480                               ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2481                                 mark_block_used(ctx, blk);
2482                                 pb->num_blocks++;
2483                         }
2484                         if (has_unaligned_cluster_map(ctx, pb->previous_block,
2485                                                       pb->last_block, blk,
2486                                                       blockcnt)) {
2487                                 pctx->blk = blockcnt;
2488                                 pctx->blk2 = blk;
2489                                 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
2490                                 mark_block_used(ctx, blk);
2491                                 mark_block_used(ctx, blk);
2492                         }
2493                         pb->last_block = blockcnt;
2494                         pb->previous_block = blk;
2495
2496                         if (is_dir) {
2497                                 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt);
2498                                 if (pctx->errcode) {
2499                                         pctx->blk = blk;
2500                                         pctx->num = blockcnt;
2501                                 failed_add_dir_block:
2502                                         fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2503                                         /* Should never get here */
2504                                         ctx->flags |= E2F_FLAG_ABORT;
2505                                         return;
2506                                 }
2507                         }
2508                 }
2509                 if (is_dir && extent.e_len > 0)
2510                         pb->last_db_block = blockcnt - 1;
2511                 pb->previous_block = extent.e_pblk + extent.e_len - 1;
2512                 start_block = pb->last_block = last_lblk;
2513                 if (is_leaf && !is_dir &&
2514                     !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
2515                         pb->last_init_lblock = last_lblk;
2516         next:
2517                 pctx->errcode = ext2fs_extent_get(ehandle,
2518                                                   EXT2_EXTENT_NEXT_SIB,
2519                                                   &extent);
2520         }
2521         if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
2522                 pctx->errcode = 0;
2523 }
2524
2525 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
2526                                  struct process_block_struct *pb)
2527 {
2528         struct ext2_extent_info info;
2529         struct ext2_inode       *inode = pctx->inode;
2530         ext2_extent_handle_t    ehandle;
2531         ext2_filsys             fs = ctx->fs;
2532         ext2_ino_t              ino = pctx->ino;
2533         errcode_t               retval;
2534         blk64_t                 eof_lblk;
2535
2536         pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
2537         if (pctx->errcode) {
2538                 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
2539                         e2fsck_clear_inode(ctx, ino, inode, 0,
2540                                            "check_blocks_extents");
2541                 pctx->errcode = 0;
2542                 return;
2543         }
2544
2545         retval = ext2fs_extent_get_info(ehandle, &info);
2546         if (retval == 0) {
2547                 if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT)
2548                         info.max_depth = MAX_EXTENT_DEPTH_COUNT-1;
2549                 ctx->extent_depth_count[info.max_depth]++;
2550         }
2551
2552         eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
2553                 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
2554         scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle);
2555         if (pctx->errcode &&
2556             fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
2557                 pb->num_blocks = 0;
2558                 inode->i_blocks = 0;
2559                 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2560                                    "check_blocks_extents");
2561                 pctx->errcode = 0;
2562         }
2563         ext2fs_extent_free(ehandle);
2564 }
2565
2566 /*
2567  * This subroutine is called on each inode to account for all of the
2568  * blocks used by that inode.
2569  */
2570 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
2571                          char *block_buf)
2572 {
2573         ext2_filsys fs = ctx->fs;
2574         struct process_block_struct pb;
2575         ext2_ino_t      ino = pctx->ino;
2576         struct ext2_inode *inode = pctx->inode;
2577         unsigned        bad_size = 0;
2578         int             dirty_inode = 0;
2579         int             extent_fs;
2580         __u64           size;
2581
2582         pb.ino = ino;
2583         pb.num_blocks = 0;
2584         pb.last_block = -1;
2585         pb.last_init_lblock = -1;
2586         pb.last_db_block = -1;
2587         pb.num_illegal_blocks = 0;
2588         pb.suppress = 0; pb.clear = 0;
2589         pb.fragmented = 0;
2590         pb.compressed = 0;
2591         pb.previous_block = 0;
2592         pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2593         pb.is_reg = LINUX_S_ISREG(inode->i_mode);
2594         pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
2595         pb.inode = inode;
2596         pb.pctx = pctx;
2597         pb.ctx = ctx;
2598         pb.inode_modified = 0;
2599         pctx->ino = ino;
2600         pctx->errcode = 0;
2601
2602         extent_fs = (ctx->fs->super->s_feature_incompat &
2603                      EXT3_FEATURE_INCOMPAT_EXTENTS);
2604
2605         if (inode->i_flags & EXT2_COMPRBLK_FL) {
2606                 if (fs->super->s_feature_incompat &
2607                     EXT2_FEATURE_INCOMPAT_COMPRESSION)
2608                         pb.compressed = 1;
2609                 else {
2610                         e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2611                         if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
2612                                 inode->i_flags &= ~EXT2_COMPRBLK_FL;
2613                                 dirty_inode++;
2614                         }
2615                 }
2616         }
2617
2618         if (ext2fs_file_acl_block(fs, inode) &&
2619             check_ext_attr(ctx, pctx, block_buf)) {
2620                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2621                         goto out;
2622                 pb.num_blocks++;
2623         }
2624
2625         if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
2626                 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
2627                         check_blocks_extents(ctx, pctx, &pb);
2628                 else {
2629                         /*
2630                          * If we've modified the inode, write it out before
2631                          * iterate() tries to use it.
2632                          */
2633                         if (dirty_inode) {
2634                                 e2fsck_write_inode(ctx, ino, inode,
2635                                                    "check_blocks");
2636                                 dirty_inode = 0;
2637                         }
2638                         pctx->errcode = ext2fs_block_iterate3(fs, ino,
2639                                                 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
2640                                                 block_buf, process_block, &pb);
2641                         /*
2642                          * We do not have uninitialized extents in non extent
2643                          * files.
2644                          */
2645                         pb.last_init_lblock = pb.last_block;
2646                         /*
2647                          * If iterate() changed a block mapping, we have to
2648                          * re-read the inode.  If we decide to clear the
2649                          * inode after clearing some stuff, we'll re-write the
2650                          * bad mappings into the inode!
2651                          */
2652                         if (pb.inode_modified)
2653                                 e2fsck_read_inode(ctx, ino, inode,
2654                                                   "check_blocks");
2655                 }
2656         }
2657         end_problem_latch(ctx, PR_LATCH_BLOCK);
2658         end_problem_latch(ctx, PR_LATCH_TOOBIG);
2659         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2660                 goto out;
2661         if (pctx->errcode)
2662                 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
2663
2664         if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
2665                 if (LINUX_S_ISDIR(inode->i_mode))
2666                         ctx->fs_fragmented_dir++;
2667                 else
2668                         ctx->fs_fragmented++;
2669         }
2670
2671         if (pb.clear) {
2672                 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2673                                    "check_blocks");
2674                 return;
2675         }
2676
2677         if (inode->i_flags & EXT2_INDEX_FL) {
2678                 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
2679                         inode->i_flags &= ~EXT2_INDEX_FL;
2680                         dirty_inode++;
2681                 } else {
2682 #ifdef ENABLE_HTREE
2683                         e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
2684 #endif
2685                 }
2686         }
2687
2688         if (!pb.num_blocks && pb.is_dir) {
2689                 /*
2690                  * The mode might be in-correct. Increasing the badness by
2691                  * small amount won't hurt much.
2692                  */
2693                 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2694                 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
2695                         e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
2696                         ctx->fs_directory_count--;
2697                         return;
2698                 }
2699         }
2700
2701         if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) {
2702                 quota_data_add(ctx->qctx, inode, ino,
2703                                pb.num_blocks * fs->blocksize);
2704                 quota_data_inodes(ctx->qctx, inode, ino, +1);
2705         }
2706
2707         if (!(fs->super->s_feature_ro_compat &
2708               EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
2709             !(inode->i_flags & EXT4_HUGE_FILE_FL))
2710                 pb.num_blocks *= (fs->blocksize / 512);
2711         pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
2712 #if 0
2713         printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
2714                ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
2715                pb.num_blocks);
2716 #endif
2717         if (pb.is_dir) {
2718                 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
2719                 if (inode->i_size & (fs->blocksize - 1))
2720                         bad_size = 5;
2721                 else if (nblock > (pb.last_block + 1))
2722                         bad_size = 1;
2723                 else if (nblock < (pb.last_block + 1)) {
2724                         if (((pb.last_block + 1) - nblock) >
2725                             fs->super->s_prealloc_dir_blocks)
2726                                 bad_size = 2;
2727                 }
2728         } else {
2729                 e2_blkcnt_t blkpg = ctx->blocks_per_page;
2730
2731                 size = EXT2_I_SIZE(inode);
2732                 if ((pb.last_init_lblock >= 0) &&
2733                     /* if size is smaller than expected by the block count,
2734                      * allow allocated blocks to end of PAGE_SIZE.
2735                      * last_init_lblock is the last in-use block, so it is
2736                      * the minimum expected file size. */
2737                     (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
2738                     ((pb.last_init_lblock + 1) / blkpg * blkpg !=
2739                      (pb.last_init_lblock + 1) ||
2740                      size < (__u64)(pb.last_init_lblock & ~(blkpg - 1)) *
2741                      fs->blocksize))
2742                         bad_size = 3;
2743                 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2744                          size > ext2_max_sizes[fs->super->s_log_block_size])
2745                         /* too big for a direct/indirect-mapped file */
2746                         bad_size = 4;
2747                 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2748                          size >
2749                          ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
2750                         /* too big for an extent-based file - 32bit ee_block */
2751                         bad_size = 6;
2752         }
2753         /* i_size for symlinks is checked elsewhere */
2754         if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
2755                 pctx->num = (pb.last_block+1) * fs->blocksize;
2756                 pctx->group = bad_size;
2757                 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2758                 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
2759                         if (LINUX_S_ISDIR(inode->i_mode))
2760                                 pctx->num &= 0xFFFFFFFFULL;
2761                         ext2fs_inode_size_set(fs, inode, pctx->num);
2762                         dirty_inode++;
2763                 }
2764                 pctx->num = 0;
2765         }
2766         if (LINUX_S_ISREG(inode->i_mode) &&
2767             ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
2768                 ctx->large_files++;
2769         if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
2770             ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
2771              ((fs->super->s_feature_ro_compat &
2772                EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
2773               (inode->i_flags & EXT4_HUGE_FILE_FL) &&
2774               (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
2775                 pctx->num = pb.num_blocks;
2776                 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2777                 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
2778                         inode->i_blocks = pb.num_blocks;
2779                         inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
2780                         dirty_inode++;
2781                 }
2782                 pctx->num = 0;
2783         }
2784
2785         if (ctx->dirs_to_hash && pb.is_dir &&
2786             !(inode->i_flags & EXT2_INDEX_FL) &&
2787             ((inode->i_size / fs->blocksize) >= 3))
2788                 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
2789
2790 out:
2791         if (dirty_inode)
2792                 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
2793 }
2794
2795 #if 0
2796 /*
2797  * Helper function called by process block when an illegal block is
2798  * found.  It returns a description about why the block is illegal
2799  */
2800 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
2801 {
2802         blk64_t super;
2803         int     i;
2804         static char     problem[80];
2805
2806         super = fs->super->s_first_data_block;
2807         strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
2808         if (block < super) {
2809                 sprintf(problem, "< FIRSTBLOCK (%u)", super);
2810                 return(problem);
2811         } else if (block >= ext2fs_blocks_count(fs->super)) {
2812                 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
2813                 return(problem);
2814         }
2815         for (i = 0; i < fs->group_desc_count; i++) {
2816                 if (block == super) {
2817                         sprintf(problem, "is the superblock in group %d", i);
2818                         break;
2819                 }
2820                 if (block > super &&
2821                     block <= (super + fs->desc_blocks)) {
2822                         sprintf(problem, "is in the group descriptors "
2823                                 "of group %d", i);
2824                         break;
2825                 }
2826                 if (block == ext2fs_block_bitmap_loc(fs, i)) {
2827                         sprintf(problem, "is the block bitmap of group %d", i);
2828                         break;
2829                 }
2830                 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
2831                         sprintf(problem, "is the inode bitmap of group %d", i);
2832                         break;
2833                 }
2834                 if (block >= ext2fs_inode_table_loc(fs, i) &&
2835                     (block < ext2fs_inode_table_loc(fs, i)
2836                      + fs->inode_blocks_per_group)) {
2837                         sprintf(problem, "is in the inode table of group %d",
2838                                 i);
2839                         break;
2840                 }
2841                 super += fs->super->s_blocks_per_group;
2842         }
2843         return(problem);
2844 }
2845 #endif
2846
2847 /*
2848  * This is a helper function for check_blocks().
2849  */
2850 static int process_block(ext2_filsys fs,
2851                   blk64_t       *block_nr,
2852                   e2_blkcnt_t blockcnt,
2853                   blk64_t ref_block EXT2FS_ATTR((unused)),
2854                   int ref_offset EXT2FS_ATTR((unused)),
2855                   void *priv_data)
2856 {
2857         struct process_block_struct *p;
2858         struct problem_context *pctx;
2859         blk64_t blk = *block_nr;
2860         int     ret_code = 0;
2861         problem_t       problem = 0;
2862         e2fsck_t        ctx;
2863
2864         p = (struct process_block_struct *) priv_data;
2865         pctx = p->pctx;
2866         ctx = p->ctx;
2867
2868         if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
2869                 /* todo: Check that the comprblk_fl is high, that the
2870                    blkaddr pattern looks right (all non-holes up to
2871                    first EXT2FS_COMPRESSED_BLKADDR, then all
2872                    EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
2873                    that the feature_incompat bit is high, and that the
2874                    inode is a regular file.  If we're doing a "full
2875                    check" (a concept introduced to e2fsck by e2compr,
2876                    meaning that we look at data blocks as well as
2877                    metadata) then call some library routine that
2878                    checks the compressed data.  I'll have to think
2879                    about this, because one particularly important
2880                    problem to be able to fix is to recalculate the
2881                    cluster size if necessary.  I think that perhaps
2882                    we'd better do most/all e2compr-specific checks
2883                    separately, after the non-e2compr checks.  If not
2884                    doing a full check, it may be useful to test that
2885                    the personality is linux; e.g. if it isn't then
2886                    perhaps this really is just an illegal block. */
2887                 return 0;
2888         }
2889
2890         /*
2891          * For a directory, add logical block zero for processing even if it's
2892          * not mapped or we'll be perennially stuck with broken "." and ".."
2893          * entries.
2894          */
2895         if (p->is_dir && blockcnt == 0 && blk == 0) {
2896                 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
2897                 if (pctx->errcode) {
2898                         pctx->blk = blk;
2899                         pctx->num = blockcnt;
2900                         goto failed_add_dir_block;
2901                 }
2902                 p->last_db_block++;
2903         }
2904
2905         if (blk == 0)
2906                 return 0;
2907
2908 #if 0
2909         printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
2910                blockcnt);
2911 #endif
2912
2913         /*
2914          * Simplistic fragmentation check.  We merely require that the
2915          * file be contiguous.  (Which can never be true for really
2916          * big files that are greater than a block group.)
2917          */
2918         if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
2919                 if (p->previous_block+1 != blk) {
2920                         if (ctx->options & E2F_OPT_FRAGCHECK) {
2921                                 char type = '?';
2922
2923                                 if (p->is_dir)
2924                                         type = 'd';
2925                                 else if (p->is_reg)
2926                                         type = 'f';
2927
2928                                 printf(_("%6lu(%c): expecting %6lu "
2929                                          "got phys %6lu (blkcnt %lld)\n"),
2930                                        (unsigned long) pctx->ino, type,
2931                                        (unsigned long) p->previous_block+1,
2932                                        (unsigned long) blk,
2933                                        blockcnt);
2934                         }
2935                         p->fragmented = 1;
2936                 }
2937         }
2938
2939         if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
2940                 problem = PR_1_TOOBIG_DIR;
2941         if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
2942                 problem = PR_1_TOOBIG_REG;
2943         if (!p->is_dir && !p->is_reg && blockcnt > 0)
2944                 problem = PR_1_TOOBIG_SYMLINK;
2945
2946         if (blk < fs->super->s_first_data_block ||
2947             blk >= ext2fs_blocks_count(fs->super)) {
2948                 problem = PR_1_ILLEGAL_BLOCK_NUM;
2949                 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
2950         }
2951
2952         if (problem) {
2953                 p->num_illegal_blocks++;
2954                 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
2955                         if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
2956                                 p->clear = 1;
2957                                 return BLOCK_ABORT;
2958                         }
2959                         if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
2960                                 p->suppress = 1;
2961                                 set_latch_flags(PR_LATCH_BLOCK,
2962                                                 PRL_SUPPRESS, 0);
2963                         }
2964                 }
2965                 pctx->blk = blk;
2966                 pctx->blkcount = blockcnt;
2967                 if (fix_problem(ctx, problem, pctx)) {
2968                         blk = *block_nr = 0;
2969                         ret_code = BLOCK_CHANGED;
2970                         p->inode_modified = 1;
2971                         /*
2972                          * If the directory block is too big and is beyond the
2973                          * end of the FS, don't bother trying to add it for
2974                          * processing -- the kernel would never have created a
2975                          * directory this large, and we risk an ENOMEM abort.
2976                          * In any case, the toobig handler for extent-based
2977                          * directories also doesn't feed toobig blocks to
2978                          * pass 2.
2979                          */
2980                         if (problem == PR_1_TOOBIG_DIR)
2981                                 return ret_code;
2982                         goto mark_dir;
2983                 } else
2984                         return 0;
2985         }
2986
2987         if (p->ino == EXT2_RESIZE_INO) {
2988                 /*
2989                  * The resize inode has already be sanity checked
2990                  * during pass #0 (the superblock checks).  All we
2991                  * have to do is mark the double indirect block as
2992                  * being in use; all of the other blocks are handled
2993                  * by mark_table_blocks()).
2994                  */
2995                 if (blockcnt == BLOCK_COUNT_DIND)
2996                         mark_block_used(ctx, blk);
2997                 p->num_blocks++;
2998         } else if (!(ctx->fs->cluster_ratio_bits &&
2999                      p->previous_block &&
3000                      (EXT2FS_B2C(ctx->fs, blk) ==
3001                       EXT2FS_B2C(ctx->fs, p->previous_block)) &&
3002                      (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
3003                      ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
3004                 mark_block_used(ctx, blk);
3005                 p->num_blocks++;
3006         } else if (has_unaligned_cluster_map(ctx, p->previous_block,
3007                                              p->last_block, blk, blockcnt)) {
3008                 pctx->blk = blockcnt;
3009                 pctx->blk2 = blk;
3010                 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
3011                 mark_block_used(ctx, blk);
3012                 mark_block_used(ctx, blk);
3013         }
3014         if (blockcnt >= 0)
3015                 p->last_block = blockcnt;
3016         p->previous_block = blk;
3017 mark_dir:
3018         if (p->is_dir && (blockcnt >= 0)) {
3019                 while (++p->last_db_block < blockcnt) {
3020                         pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
3021                                                               p->ino, 0,
3022                                                               p->last_db_block);
3023                         if (pctx->errcode) {
3024                                 pctx->blk = 0;
3025                                 pctx->num = p->last_db_block;
3026                                 goto failed_add_dir_block;
3027                         }
3028                 }
3029                 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
3030                                                       blk, blockcnt);
3031                 if (pctx->errcode) {
3032                         pctx->blk = blk;
3033                         pctx->num = blockcnt;
3034                 failed_add_dir_block:
3035                         fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3036                         /* Should never get here */
3037                         ctx->flags |= E2F_FLAG_ABORT;
3038                         return BLOCK_ABORT;
3039                 }
3040         }
3041         return ret_code;
3042 }
3043
3044 static int process_bad_block(ext2_filsys fs,
3045                       blk64_t *block_nr,
3046                       e2_blkcnt_t blockcnt,
3047                       blk64_t ref_block EXT2FS_ATTR((unused)),
3048                       int ref_offset EXT2FS_ATTR((unused)),
3049                       void *priv_data)
3050 {
3051         struct process_block_struct *p;
3052         blk64_t         blk = *block_nr;
3053         blk64_t         first_block;
3054         dgrp_t          i;
3055         struct problem_context *pctx;
3056         e2fsck_t        ctx;
3057
3058         /*
3059          * Note: This function processes blocks for the bad blocks
3060          * inode, which is never compressed.  So we don't use HOLE_BLKADDR().
3061          */
3062
3063         if (!blk)
3064                 return 0;
3065
3066         p = (struct process_block_struct *) priv_data;
3067         ctx = p->ctx;
3068         pctx = p->pctx;
3069
3070         pctx->ino = EXT2_BAD_INO;
3071         pctx->blk = blk;
3072         pctx->blkcount = blockcnt;
3073
3074         if ((blk < fs->super->s_first_data_block) ||
3075             (blk >= ext2fs_blocks_count(fs->super))) {
3076                 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
3077                         *block_nr = 0;
3078                         return BLOCK_CHANGED;
3079                 } else
3080                         return 0;
3081         }
3082
3083         if (blockcnt < 0) {
3084                 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
3085                         p->bbcheck = 1;
3086                         if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
3087                                 *block_nr = 0;
3088                                 return BLOCK_CHANGED;
3089                         }
3090                 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3091                                                     blk)) {
3092                         p->bbcheck = 1;
3093                         if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
3094                                         pctx)) {
3095                                 *block_nr = 0;
3096                                 return BLOCK_CHANGED;
3097                         }
3098                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3099                                 return BLOCK_ABORT;
3100                 } else
3101                         mark_block_used(ctx, blk);
3102                 return 0;
3103         }
3104 #if 0
3105         printf ("DEBUG: Marking %u as bad.\n", blk);
3106 #endif
3107         ctx->fs_badblocks_count++;
3108         /*
3109          * If the block is not used, then mark it as used and return.
3110          * If it is already marked as found, this must mean that
3111          * there's an overlap between the filesystem table blocks
3112          * (bitmaps and inode table) and the bad block list.
3113          */
3114         if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
3115                 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
3116                 return 0;
3117         }
3118         /*
3119          * Try to find the where the filesystem block was used...
3120          */
3121         first_block = fs->super->s_first_data_block;
3122
3123         for (i = 0; i < fs->group_desc_count; i++ ) {
3124                 pctx->group = i;
3125                 pctx->blk = blk;
3126                 if (!ext2fs_bg_has_super(fs, i))
3127                         goto skip_super;
3128                 if (blk == first_block) {
3129                         if (i == 0) {
3130                                 if (fix_problem(ctx,
3131                                                 PR_1_BAD_PRIMARY_SUPERBLOCK,
3132                                                 pctx)) {
3133                                         *block_nr = 0;
3134                                         return BLOCK_CHANGED;
3135                                 }
3136                                 return 0;
3137                         }
3138                         fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
3139                         return 0;
3140                 }
3141                 if ((blk > first_block) &&
3142                     (blk <= first_block + fs->desc_blocks)) {
3143                         if (i == 0) {
3144                                 pctx->blk = *block_nr;
3145                                 if (fix_problem(ctx,
3146                         PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
3147                                         *block_nr = 0;
3148                                         return BLOCK_CHANGED;
3149                                 }
3150                                 return 0;
3151                         }
3152                         fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
3153                         return 0;
3154                 }
3155         skip_super:
3156                 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
3157                         if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
3158                                 ctx->invalid_block_bitmap_flag[i]++;
3159                                 ctx->invalid_bitmaps++;
3160                         }
3161                         return 0;
3162                 }
3163                 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
3164                         if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
3165                                 ctx->invalid_inode_bitmap_flag[i]++;
3166                                 ctx->invalid_bitmaps++;
3167                         }
3168                         return 0;
3169                 }
3170                 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
3171                     (blk < (ext2fs_inode_table_loc(fs, i) +
3172                             fs->inode_blocks_per_group))) {
3173                         /*
3174                          * If there are bad blocks in the inode table,
3175                          * the inode scan code will try to do
3176                          * something reasonable automatically.
3177                          */
3178                         return 0;
3179                 }
3180                 first_block += fs->super->s_blocks_per_group;
3181         }
3182         /*
3183          * If we've gotten to this point, then the only
3184          * possibility is that the bad block inode meta data
3185          * is using a bad block.
3186          */
3187         if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
3188             (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
3189             (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
3190                 p->bbcheck = 1;
3191                 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
3192                         *block_nr = 0;
3193                         return BLOCK_CHANGED;
3194                 }
3195                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3196                         return BLOCK_ABORT;
3197                 return 0;
3198         }
3199
3200         pctx->group = -1;
3201
3202         /* Warn user that the block wasn't claimed */
3203         fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
3204
3205         return 0;
3206 }
3207
3208 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
3209                             const char *name, int num, blk64_t *new_block)
3210 {
3211         ext2_filsys fs = ctx->fs;
3212         dgrp_t          last_grp;
3213         blk64_t         old_block = *new_block;
3214         blk64_t         last_block;
3215         dgrp_t          flexbg;
3216         unsigned        flexbg_size;
3217         int             i, is_flexbg;
3218         char            *buf;
3219         struct problem_context  pctx;
3220
3221         clear_problem_context(&pctx);
3222
3223         pctx.group = group;
3224         pctx.blk = old_block;
3225         pctx.str = name;
3226
3227         /*
3228          * For flex_bg filesystems, first try to allocate the metadata
3229          * within the flex_bg, and if that fails then try finding the
3230          * space anywhere in the filesystem.
3231          */
3232         is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super,
3233                                               EXT4_FEATURE_INCOMPAT_FLEX_BG);
3234         if (is_flexbg) {
3235                 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3236                 flexbg = group / flexbg_size;
3237                 first_block = ext2fs_group_first_block2(fs,
3238                                                         flexbg_size * flexbg);
3239                 last_grp = group | (flexbg_size - 1);
3240                 if (last_grp >= fs->group_desc_count)
3241                         last_grp = fs->group_desc_count - 1;
3242                 last_block = ext2fs_group_last_block2(fs, last_grp);
3243         } else
3244                 last_block = ext2fs_group_last_block2(fs, group);
3245         pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
3246                                                num, ctx->block_found_map,
3247                                                new_block);
3248         if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
3249                 pctx.errcode = ext2fs_get_free_blocks2(fs,
3250                                 fs->super->s_first_data_block,
3251                                 ext2fs_blocks_count(fs->super),
3252                                 num, ctx->block_found_map, new_block);
3253         if (pctx.errcode) {
3254                 pctx.num = num;
3255                 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
3256                 ext2fs_unmark_valid(fs);
3257                 ctx->flags |= E2F_FLAG_ABORT;
3258                 return;
3259         }
3260         pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
3261         if (pctx.errcode) {
3262                 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
3263                 ext2fs_unmark_valid(fs);
3264                 ctx->flags |= E2F_FLAG_ABORT;
3265                 return;
3266         }
3267         ext2fs_mark_super_dirty(fs);
3268         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
3269         pctx.blk2 = *new_block;
3270         fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
3271                           PR_1_RELOC_TO), &pctx);
3272         pctx.blk2 = 0;
3273         for (i = 0; i < num; i++) {
3274                 pctx.blk = i;
3275                 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
3276                 if (old_block) {
3277                         pctx.errcode = io_channel_read_blk64(fs->io,
3278                                    old_block + i, 1, buf);
3279                         if (pctx.errcode)
3280                                 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
3281                 } else
3282                         memset(buf, 0, fs->blocksize);
3283
3284                 pctx.blk = (*new_block) + i;
3285                 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
3286                                               1, buf);
3287                 if (pctx.errcode)
3288                         fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
3289         }
3290         ext2fs_free_mem(&buf);
3291 }
3292
3293 /*
3294  * This routine gets called at the end of pass 1 if bad blocks are
3295  * detected in the superblock, group descriptors, inode_bitmaps, or
3296  * block bitmaps.  At this point, all of the blocks have been mapped
3297  * out, so we can try to allocate new block(s) to replace the bad
3298  * blocks.
3299  */
3300 static void handle_fs_bad_blocks(e2fsck_t ctx)
3301 {
3302         ext2_filsys fs = ctx->fs;
3303         dgrp_t          i;
3304         blk64_t         first_block;
3305         blk64_t         new_blk;
3306
3307         for (i = 0; i < fs->group_desc_count; i++) {
3308                 first_block = ext2fs_group_first_block2(fs, i);
3309
3310                 if (ctx->invalid_block_bitmap_flag[i]) {
3311                         new_blk = ext2fs_block_bitmap_loc(fs, i);
3312                         new_table_block(ctx, first_block, i, _("block bitmap"),
3313                                         1, &new_blk);
3314                         ext2fs_block_bitmap_loc_set(fs, i, new_blk);
3315                 }
3316                 if (ctx->invalid_inode_bitmap_flag[i]) {
3317                         new_blk = ext2fs_inode_bitmap_loc(fs, i);
3318                         new_table_block(ctx, first_block, i, _("inode bitmap"),
3319                                         1, &new_blk);
3320                         ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
3321                 }
3322                 if (ctx->invalid_inode_table_flag[i]) {
3323                         new_blk = ext2fs_inode_table_loc(fs, i);
3324                         new_table_block(ctx, first_block, i, _("inode table"),
3325                                         fs->inode_blocks_per_group,
3326                                         &new_blk);
3327                         ext2fs_inode_table_loc_set(fs, i, new_blk);
3328                         ctx->flags |= E2F_FLAG_RESTART;
3329                 }
3330         }
3331         ctx->invalid_bitmaps = 0;
3332 }
3333
3334 /*
3335  * This routine marks all blocks which are used by the superblock,
3336  * group descriptors, inode bitmaps, and block bitmaps.
3337  */
3338 static void mark_table_blocks(e2fsck_t ctx)
3339 {
3340         ext2_filsys fs = ctx->fs;
3341         blk64_t b;
3342         dgrp_t  i;
3343         unsigned int    j;
3344         struct problem_context pctx;
3345
3346         clear_problem_context(&pctx);
3347
3348         for (i = 0; i < fs->group_desc_count; i++) {
3349                 pctx.group = i;
3350
3351                 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
3352
3353                 /*
3354                  * Mark the blocks used for the inode table
3355                  */
3356                 if (ext2fs_inode_table_loc(fs, i)) {
3357                         for (j = 0, b = ext2fs_inode_table_loc(fs, i);
3358                              j < fs->inode_blocks_per_group;
3359                              j++, b++) {
3360                                 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3361                                                              b)) {
3362                                         pctx.blk = b;
3363                                         if (!ctx->invalid_inode_table_flag[i] &&
3364                                             fix_problem(ctx,
3365                                                 PR_1_ITABLE_CONFLICT, &pctx)) {
3366                                                 ctx->invalid_inode_table_flag[i]++;
3367                                                 ctx->invalid_bitmaps++;
3368                                         }
3369                                 } else {
3370                                     ext2fs_mark_block_bitmap2(ctx->block_found_map,
3371                                                              b);
3372                                 }
3373                         }
3374                 }
3375
3376                 /*
3377                  * Mark block used for the block bitmap
3378                  */
3379                 if (ext2fs_block_bitmap_loc(fs, i)) {
3380                         if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3381                                      ext2fs_block_bitmap_loc(fs, i))) {
3382                                 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
3383                                 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
3384                                         ctx->invalid_block_bitmap_flag[i]++;
3385                                         ctx->invalid_bitmaps++;
3386                                 }
3387                         } else {
3388                             ext2fs_mark_block_bitmap2(ctx->block_found_map,
3389                                      ext2fs_block_bitmap_loc(fs, i));
3390                     }
3391
3392                 }
3393                 /*
3394                  * Mark block used for the inode bitmap
3395                  */
3396                 if (ext2fs_inode_bitmap_loc(fs, i)) {
3397                         if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3398                                      ext2fs_inode_bitmap_loc(fs, i))) {
3399                                 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
3400                                 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
3401                                         ctx->invalid_inode_bitmap_flag[i]++;
3402                                         ctx->invalid_bitmaps++;
3403                                 }
3404                         } else {
3405                             ext2fs_mark_block_bitmap2(ctx->block_found_map,
3406                                      ext2fs_inode_bitmap_loc(fs, i));
3407                         }
3408                 }
3409         }
3410 }
3411
3412 /*
3413  * Thes subroutines short circuits ext2fs_get_blocks and
3414  * ext2fs_check_directory; we use them since we already have the inode
3415  * structure, so there's no point in letting the ext2fs library read
3416  * the inode again.
3417  */
3418 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
3419                                   blk_t *blocks)
3420 {
3421         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3422         int     i;
3423
3424         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3425                 return EXT2_ET_CALLBACK_NOTHANDLED;
3426
3427         for (i=0; i < EXT2_N_BLOCKS; i++)
3428                 blocks[i] = ctx->stashed_inode->i_block[i];
3429         return 0;
3430 }
3431
3432 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
3433                                   struct ext2_inode *inode)
3434 {
3435         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3436
3437         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3438                 return EXT2_ET_CALLBACK_NOTHANDLED;
3439         *inode = *ctx->stashed_inode;
3440         return 0;
3441 }
3442
3443 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
3444                             struct ext2_inode *inode)
3445 {
3446         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3447
3448         if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
3449                 (inode != ctx->stashed_inode))
3450                 *ctx->stashed_inode = *inode;
3451         return EXT2_ET_CALLBACK_NOTHANDLED;
3452 }
3453
3454 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
3455 {
3456         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3457
3458         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3459                 return EXT2_ET_CALLBACK_NOTHANDLED;
3460
3461         if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
3462                 return EXT2_ET_NO_DIRECTORY;
3463         return 0;
3464 }
3465
3466 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
3467                                         blk64_t *ret)
3468 {
3469         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3470         errcode_t       retval;
3471         blk64_t         new_block;
3472
3473         if (ctx->block_found_map) {
3474                 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
3475                                            &new_block);
3476                 if (retval)
3477                         return retval;
3478                 if (fs->block_map) {
3479                         ext2fs_mark_block_bitmap2(fs->block_map, new_block);
3480                         ext2fs_mark_bb_dirty(fs);
3481                 }
3482         } else {
3483                 if (!fs->block_map) {
3484                         retval = ext2fs_read_block_bitmap(fs);
3485                         if (retval)
3486                                 return retval;
3487                 }
3488
3489                 retval = ext2fs_new_block2(fs, goal, 0, &new_block);
3490                 if (retval)
3491                         return retval;
3492         }
3493
3494         *ret = new_block;
3495         return (0);
3496 }
3497
3498 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
3499 {
3500         ext2_filsys fs = ctx->fs;
3501
3502         if (use_shortcuts) {
3503                 fs->get_blocks = pass1_get_blocks;
3504                 fs->check_directory = pass1_check_directory;
3505                 fs->read_inode = pass1_read_inode;
3506                 fs->write_inode = pass1_write_inode;
3507                 ctx->stashed_ino = 0;
3508         } else {
3509                 fs->get_blocks = 0;
3510                 fs->check_directory = 0;
3511                 fs->read_inode = 0;
3512                 fs->write_inode = 0;
3513         }
3514 }
3515
3516 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
3517 {
3518         ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
3519         ext2fs_set_block_alloc_stats_callback(ctx->fs,
3520                                                 e2fsck_block_alloc_stats, 0);
3521 }