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