Whamcloud - gitweb
Add support for the half-MD4 HTREE hash.
[tools/e2fsprogs.git] / e2fsck / pass1.c
1 /*
2  * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
3  * 
4  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  * 
11  * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12  * and applies the following tests to each inode:
13  *
14  *      - The mode field of the inode must be legal.
15  *      - The size and block count fields of the inode are correct.
16  *      - A data block must not be used by another inode
17  *
18  * Pass 1 also gathers the collects the following information:
19  *
20  *      - A bitmap of which inodes are in use.          (inode_used_map)
21  *      - A bitmap of which inodes are directories.     (inode_dir_map)
22  *      - A bitmap of which inodes are regular files.   (inode_reg_map)
23  *      - A bitmap of which inodes have bad fields.     (inode_bad_map)
24  *      - A bitmap of which inodes are in bad blocks.   (inode_bb_map)
25  *      - A bitmap of which inodes are imagic inodes.   (inode_imagic_map)
26  *      - A bitmap of which blocks are in use.          (block_found_map)
27  *      - A bitmap of which blocks are in use by two inodes     (block_dup_map)
28  *      - The data blocks of the directory inodes.      (dir_map)
29  *
30  * Pass 1 is designed to stash away enough information so that the
31  * other passes should not need to read in the inode information
32  * during the normal course of a filesystem check.  (Althogh if an
33  * inconsistency is detected, other passes may need to read in an
34  * inode to fix it.)
35  *
36  * Note that pass 1B will be invoked if there are any duplicate blocks
37  * found.
38  */
39
40 #include <time.h>
41 #ifdef HAVE_ERRNO_H
42 #include <errno.h>
43 #endif
44
45 #include "e2fsck.h"
46 #include <ext2fs/ext2_ext_attr.h>
47
48 #include "problem.h"
49
50 #ifdef NO_INLINE_FUNCS
51 #define _INLINE_
52 #else
53 #define _INLINE_ inline
54 #endif
55
56 static int process_block(ext2_filsys fs, blk_t  *blocknr,
57                          e2_blkcnt_t blockcnt, blk_t ref_blk, 
58                          int ref_offset, void *priv_data);
59 static int process_bad_block(ext2_filsys fs, blk_t *block_nr,
60                              e2_blkcnt_t blockcnt, blk_t ref_blk,
61                              int ref_offset, void *priv_data);
62 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
63                          char *block_buf);
64 static void mark_table_blocks(e2fsck_t ctx);
65 static void alloc_bb_map(e2fsck_t ctx);
66 static void alloc_imagic_map(e2fsck_t ctx);
67 static void mark_inode_bad(e2fsck_t ctx, ino_t ino);
68 static void handle_fs_bad_blocks(e2fsck_t ctx);
69 static void process_inodes(e2fsck_t ctx, char *block_buf);
70 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
71 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
72                                   dgrp_t group, void * priv_data);
73 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount, 
74                                     char *block_buf, int adjust_sign);
75 /* static char *describe_illegal_block(ext2_filsys fs, blk_t block); */
76
77 struct process_block_struct {
78         ext2_ino_t      ino;
79         int             is_dir:1, is_reg:1, clear:1, suppress:1,
80                                 fragmented:1, compressed:1;
81         blk_t           num_blocks;
82         blk_t           max_blocks;
83         e2_blkcnt_t     last_block;
84         int             num_illegal_blocks;
85         blk_t           previous_block;
86         struct ext2_inode *inode;
87         struct problem_context *pctx;
88         e2fsck_t        ctx;
89 };
90
91 struct process_inode_block {
92         ext2_ino_t ino;
93         struct ext2_inode inode;
94 };
95
96 struct scan_callback_struct {
97         e2fsck_t        ctx;
98         char            *block_buf;
99 };
100
101 /*
102  * For the inodes to process list.
103  */
104 static struct process_inode_block *inodes_to_process;
105 static int process_inode_count;
106
107 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
108                             EXT2_MIN_BLOCK_LOG_SIZE + 1];
109
110 /*
111  * Free all memory allocated by pass1 in preparation for restarting
112  * things.
113  */
114 static void unwind_pass1(ext2_filsys fs)
115 {
116         ext2fs_free_mem((void **) &inodes_to_process);
117         inodes_to_process = 0;
118 }
119
120 /*
121  * Check to make sure a device inode is real.  Returns 1 if the device
122  * checks out, 0 if not.
123  *
124  * Note: this routine is now also used to check FIFO's and Sockets,
125  * since they have the same requirement; the i_block fields should be
126  * zero. 
127  */
128 int e2fsck_pass1_check_device_inode(struct ext2_inode *inode)
129 {
130         int     i;
131
132         /*
133          * If i_blocks is non-zero, or the index flag is set, then
134          * this is a bogus device/fifo/socket
135          */
136         if (inode->i_blocks || (inode->i_flags & EXT2_INDEX_FL))
137                 return 0;
138
139         /*
140          * We should be able to do the test below all the time, but
141          * because the kernel doesn't forcibly clear the device
142          * inode's additional i_block fields, there are some rare
143          * occasions when a legitimate device inode will have non-zero
144          * additional i_block fields.  So for now, we only complain
145          * when the immutable flag is set, which should never happen
146          * for devices.  (And that's when the problem is caused, since
147          * you can't set or clear immutable flags for devices.)  Once
148          * the kernel has been fixed we can change this...
149          */
150         if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
151                 for (i=4; i < EXT2_N_BLOCKS; i++) 
152                         if (inode->i_block[i])
153                                 return 0;
154         }
155         return 1;
156 }
157
158 #ifndef HAVE_STRNLEN
159 /*
160  * Incredibly, libc5 doesn't appear to have strnlen.  So we have to
161  * provide our own.
162  */
163 static int strnlen(const char * s, int count)
164 {
165         const char *cp = s;
166
167         while (count-- && *cp)
168                 cp++;
169         return cp - s;
170 }
171 #endif
172
173 /*
174  * Check to make sure a symlink inode is real.  Returns 1 if the symlink
175  * checks out, 0 if not.
176  */
177 int e2fsck_pass1_check_symlink(ext2_filsys fs, struct ext2_inode *inode,
178                                char *buf)
179 {
180         int len;
181         int i;
182
183         if ((inode->i_size_high || inode->i_size == 0) ||
184             (inode->i_flags & EXT2_INDEX_FL))
185                 return 0;
186
187         if (inode->i_blocks) {
188                 if ((inode->i_size >= fs->blocksize) ||
189                     (inode->i_blocks != fs->blocksize >> 9) ||
190                     (inode->i_block[0] < fs->super->s_first_data_block) ||
191                     (inode->i_block[0] >= fs->super->s_blocks_count))
192                         return 0;
193
194                 for (i = 1; i < EXT2_N_BLOCKS; i++)
195                         if (inode->i_block[i])
196                                 return 0;
197
198                 if (io_channel_read_blk(fs->io, inode->i_block[0], 1, buf))
199                         return 0;
200
201                 len = strnlen(buf, fs->blocksize);
202                 if (len == fs->blocksize)
203                         return 0;
204         } else {
205                 if (inode->i_size >= sizeof(inode->i_block))
206                         return 0;
207
208                 len = strnlen((char *)inode->i_block, sizeof(inode->i_block));
209                 if (len == sizeof(inode->i_block))
210                         return 0;
211         }
212         if (len != inode->i_size)
213                 return 0;
214         return 1;
215 }
216
217 /*
218  * If the immutable (or append-only) flag is set on the inode, offer
219  * to clear it.
220  */
221 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
222 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
223 {
224         if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
225                 return;
226
227         if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
228                 return;
229
230         pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
231         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
232 }
233
234 /*
235  * If device, fifo or socket, check size is zero -- if not offer to
236  * clear it
237  */
238 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
239 {
240         struct ext2_inode *inode = pctx->inode;
241         
242         if ((inode->i_size == 0) && (inode->i_size_high == 0))
243                 return;
244         
245         if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
246                 return;
247         
248         inode->i_size = 0;
249         inode->i_size_high = 0;
250         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
251 }
252         
253
254 void e2fsck_pass1(e2fsck_t ctx)
255 {
256         int     i;
257         __u64   max_sizes;
258         ext2_filsys fs = ctx->fs;
259         ext2_ino_t      ino;
260         struct ext2_inode inode;
261         ext2_inode_scan scan;
262         char            *block_buf;
263 #ifdef RESOURCE_TRACK
264         struct resource_track   rtrack;
265 #endif
266         unsigned char   frag, fsize;
267         struct          problem_context pctx;
268         struct          scan_callback_struct scan_struct;
269         struct ext2_super_block *sb = ctx->fs->super;
270         int             imagic_fs;
271         
272 #ifdef RESOURCE_TRACK
273         init_resource_track(&rtrack);
274 #endif
275         clear_problem_context(&pctx);
276
277         if (!(ctx->options & E2F_OPT_PREEN))
278                 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
279
280 #ifdef MTRACE
281         mtrace_print("Pass 1");
282 #endif
283
284 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
285
286         for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
287                 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
288                 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
289                 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
290                 max_sizes = (max_sizes * (1UL << i)) - 1;
291                 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
292         }
293 #undef EXT2_BPP
294
295         imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
296
297         /*
298          * Allocate bitmaps structures
299          */
300         pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
301                                               &ctx->inode_used_map);
302         if (pctx.errcode) {
303                 pctx.num = 1;
304                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
305                 ctx->flags |= E2F_FLAG_ABORT;
306                 return;
307         }
308         pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
309                                 _("directory inode map"), &ctx->inode_dir_map);
310         if (pctx.errcode) {
311                 pctx.num = 2;
312                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
313                 ctx->flags |= E2F_FLAG_ABORT;
314                 return;
315         }
316         pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
317                         _("regular file inode map"), &ctx->inode_reg_map);
318         if (pctx.errcode) {
319                 pctx.num = 6;
320                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
321                 ctx->flags |= E2F_FLAG_ABORT;
322                 return;
323         }
324         pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
325                                               &ctx->block_found_map);
326         if (pctx.errcode) {
327                 pctx.num = 1;
328                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
329                 ctx->flags |= E2F_FLAG_ABORT;
330                 return;
331         }
332         pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
333                                              &ctx->inode_link_info);
334         if (pctx.errcode) {
335                 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
336                 ctx->flags |= E2F_FLAG_ABORT;
337                 return;
338         }
339         inodes_to_process = (struct process_inode_block *)
340                 e2fsck_allocate_memory(ctx,
341                                        (ctx->process_inode_size *
342                                         sizeof(struct process_inode_block)),
343                                        "array of inodes to process");
344         process_inode_count = 0;
345
346         pctx.errcode = ext2fs_init_dblist(fs, 0);
347         if (pctx.errcode) {
348                 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
349                 ctx->flags |= E2F_FLAG_ABORT;
350                 return;
351         }
352
353         /*
354          * If the last orphan field is set, clear it, since the pass1
355          * processing will automatically find and clear the orphans.
356          * In the future, we may want to try using the last_orphan
357          * linked list ourselves, but for now, we clear it so that the
358          * ext3 mount code won't get confused.
359          */
360         if (!(ctx->options & E2F_OPT_READONLY)) {
361                 if (fs->super->s_last_orphan) {
362                         fs->super->s_last_orphan = 0;
363                         ext2fs_mark_super_dirty(fs);
364                 }
365         }
366
367         mark_table_blocks(ctx);
368         block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
369                                                     "block interate buffer");
370         e2fsck_use_inode_shortcuts(ctx, 1);
371         ehandler_operation(_("doing inode scan"));
372         pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks, 
373                                               &scan);
374         if (pctx.errcode) {
375                 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
376                 ctx->flags |= E2F_FLAG_ABORT;
377                 return;
378         }
379         ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
380         pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
381         if (pctx.errcode) {
382                 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
383                 ctx->flags |= E2F_FLAG_ABORT;
384                 return;
385         }
386         ctx->stashed_inode = &inode;
387         scan_struct.ctx = ctx;
388         scan_struct.block_buf = block_buf;
389         ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
390         if (ctx->progress)
391                 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
392                         return;
393         while (ino) {
394                 pctx.ino = ino;
395                 pctx.inode = &inode;
396                 ctx->stashed_ino = ino;
397                 if (inode.i_links_count) {
398                         pctx.errcode = ext2fs_icount_store(ctx->inode_link_info, 
399                                            ino, inode.i_links_count);
400                         if (pctx.errcode) {
401                                 pctx.num = inode.i_links_count;
402                                 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
403                                 ctx->flags |= E2F_FLAG_ABORT;
404                                 return;
405                         }
406                 }
407                 if (ino == EXT2_BAD_INO) {
408                         struct process_block_struct pb;
409                         
410                         pb.ino = EXT2_BAD_INO;
411                         pb.num_blocks = pb.last_block = 0;
412                         pb.num_illegal_blocks = 0;
413                         pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
414                         pb.is_reg = 0; pb.fragmented = 0;
415                         pb.inode = &inode;
416                         pb.pctx = &pctx;
417                         pb.ctx = ctx;
418                         pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, 
419                                      block_buf, process_bad_block, &pb);
420                         if (pctx.errcode) {
421                                 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
422                                 ctx->flags |= E2F_FLAG_ABORT;
423                                 return;
424                         }
425                         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
426                         clear_problem_context(&pctx);
427                         goto next;
428                 } else if (ino == EXT2_ROOT_INO) {
429                         /*
430                          * Make sure the root inode is a directory; if
431                          * not, offer to clear it.  It will be
432                          * regnerated in pass #3.
433                          */
434                         if (!LINUX_S_ISDIR(inode.i_mode)) {
435                                 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) {
436                                         inode.i_dtime = time(0);
437                                         inode.i_links_count = 0;
438                                         ext2fs_icount_store(ctx->inode_link_info,
439                                                             ino, 0);
440                                         e2fsck_write_inode(ctx, ino, &inode,
441                                                            "pass1");
442                                 }
443
444                         }
445                         /*
446                          * If dtime is set, offer to clear it.  mke2fs
447                          * version 0.2b created filesystems with the
448                          * dtime field set for the root and lost+found
449                          * directories.  We won't worry about
450                          * /lost+found, since that can be regenerated
451                          * easily.  But we will fix the root directory
452                          * as a special case.
453                          */
454                         if (inode.i_dtime && inode.i_links_count) {
455                                 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
456                                         inode.i_dtime = 0;
457                                         e2fsck_write_inode(ctx, ino, &inode,
458                                                            "pass1");
459                                 }
460                         }
461                 } else if (ino == EXT2_JOURNAL_INO) {
462                         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
463                         if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
464                                 if (!LINUX_S_ISREG(inode.i_mode) &&
465                                     fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
466                                                 &pctx)) {
467                                         inode.i_mode = LINUX_S_IFREG;
468                                         e2fsck_write_inode(ctx, ino, &inode,
469                                                            "pass1");
470                                 }
471                                 check_blocks(ctx, &pctx, block_buf);
472                                 goto next;
473                         }
474                         if ((inode.i_links_count || inode.i_blocks ||
475                              inode.i_blocks || inode.i_block[0]) &&
476                             fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR, 
477                                         &pctx)) {
478                                 memset(&inode, 0, sizeof(inode));
479                                 ext2fs_icount_store(ctx->inode_link_info,
480                                                     ino, 0);
481                                 e2fsck_write_inode(ctx, ino, &inode, "pass1");
482                         }
483                 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
484                         int     problem = 0;
485                         
486                         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
487                         if (ino == EXT2_BOOT_LOADER_INO) {
488                                 if (LINUX_S_ISDIR(inode.i_mode))
489                                         problem = PR_1_RESERVED_BAD_MODE;
490                         } else if (ino == EXT2_RESIZE_INO) {
491                                 if (inode.i_mode &&
492                                     !LINUX_S_ISREG(inode.i_mode))
493                                         problem = PR_1_RESERVED_BAD_MODE;
494                         } else {
495                                 if (inode.i_mode != 0)
496                                         problem = PR_1_RESERVED_BAD_MODE;
497                         }
498                         if (problem) {
499                                 if (fix_problem(ctx, problem, &pctx)) {
500                                         inode.i_mode = 0;
501                                         e2fsck_write_inode(ctx, ino, &inode,
502                                                            "pass1");
503                                 }
504                         }
505                         check_blocks(ctx, &pctx, block_buf);
506                         goto next;
507                 }
508                 /*
509                  * Check for inodes who might have been part of the
510                  * orphaned list linked list.  They should have gotten
511                  * dealt with by now, unless the list had somehow been
512                  * corrupted.
513                  * 
514                  * FIXME: In the future, inodes which are still in use
515                  * (and which are therefore) pending truncation should
516                  * be handled specially.  Right now we just clear the
517                  * dtime field, and the normal e2fsck handling of
518                  * inodes where i_size and the inode blocks are
519                  * inconsistent is to fix i_size, instead of releasing
520                  * the extra blocks.  This won't catch the inodes that
521                  * was at the end of the orphan list, but it's better
522                  * than nothing.  The right answer is that there
523                  * shouldn't be any bugs in the orphan list handling.  :-)
524                  */
525                 if (inode.i_dtime &&
526                     inode.i_dtime < ctx->fs->super->s_inodes_count) {
527                         if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
528                                 inode.i_dtime = inode.i_links_count ?
529                                         0 : time(0);
530                                 e2fsck_write_inode(ctx, ino, &inode,
531                                                    "pass1");
532                         }
533                 }
534                 
535                 /*
536                  * This code assumes that deleted inodes have
537                  * i_links_count set to 0.  
538                  */
539                 if (!inode.i_links_count) {
540                         if (!inode.i_dtime && inode.i_mode) {
541                                 if (fix_problem(ctx,
542                                             PR_1_ZERO_DTIME, &pctx)) {
543                                         inode.i_dtime = time(0);
544                                         e2fsck_write_inode(ctx, ino, &inode,
545                                                            "pass1");
546                                 }
547                         }
548                         goto next;
549                 }
550                 /*
551                  * n.b.  0.3c ext2fs code didn't clear i_links_count for
552                  * deleted files.  Oops.
553                  *
554                  * Since all new ext2 implementations get this right,
555                  * we now assume that the case of non-zero
556                  * i_links_count and non-zero dtime means that we
557                  * should keep the file, not delete it.
558                  * 
559                  */
560                 if (inode.i_dtime) {
561                         if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
562                                 inode.i_dtime = 0;
563                                 e2fsck_write_inode(ctx, ino, &inode, "pass1");
564                         }
565                 }
566                 
567                 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
568                 switch (fs->super->s_creator_os) {
569                     case EXT2_OS_LINUX:
570                         frag = inode.osd2.linux2.l_i_frag;
571                         fsize = inode.osd2.linux2.l_i_fsize;
572                         break;
573                     case EXT2_OS_HURD:
574                         frag = inode.osd2.hurd2.h_i_frag;
575                         fsize = inode.osd2.hurd2.h_i_fsize;
576                         break;
577                     case EXT2_OS_MASIX:
578                         frag = inode.osd2.masix2.m_i_frag;
579                         fsize = inode.osd2.masix2.m_i_fsize;
580                         break;
581                     default:
582                         frag = fsize = 0;
583                 }
584                 
585                 if (inode.i_faddr || frag || fsize ||
586                     (LINUX_S_ISDIR(inode.i_mode) && inode.i_dir_acl))
587                         mark_inode_bad(ctx, ino);
588                 if (inode.i_flags & EXT2_IMAGIC_FL) {
589                         if (imagic_fs) {
590                                 if (!ctx->inode_imagic_map)
591                                         alloc_imagic_map(ctx);
592                                 ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
593                                                          ino);
594                         } else {
595                                 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
596                                         inode.i_flags &= ~EXT2_IMAGIC_FL;
597                                         e2fsck_write_inode(ctx, ino,
598                                                            &inode, "pass1");
599                                 }
600                         }
601                 }
602                 
603                 if (LINUX_S_ISDIR(inode.i_mode)) {
604                         ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
605                         e2fsck_add_dir_info(ctx, ino, 0);
606                         ctx->fs_directory_count++;
607                 } else if (LINUX_S_ISREG (inode.i_mode)) {
608                         ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
609                         ctx->fs_regular_count++;
610                 } else if (LINUX_S_ISCHR (inode.i_mode) &&
611                            e2fsck_pass1_check_device_inode(&inode)) {
612                         check_immutable(ctx, &pctx);
613                         check_size(ctx, &pctx);
614                         ctx->fs_chardev_count++;
615                 } else if (LINUX_S_ISBLK (inode.i_mode) &&
616                            e2fsck_pass1_check_device_inode(&inode)) {
617                         check_immutable(ctx, &pctx);
618                         check_size(ctx, &pctx);
619                         ctx->fs_blockdev_count++;
620                 } else if (LINUX_S_ISLNK (inode.i_mode) &&
621                            e2fsck_pass1_check_symlink(fs, &inode, block_buf)) {
622                         check_immutable(ctx, &pctx);
623                         ctx->fs_symlinks_count++;
624                         if (!inode.i_blocks) {
625                                 ctx->fs_fast_symlinks_count++;
626                                 goto next;
627                         }
628                 }
629                 else if (LINUX_S_ISFIFO (inode.i_mode) &&
630                          e2fsck_pass1_check_device_inode(&inode)) {
631                         check_immutable(ctx, &pctx);
632                         check_size(ctx, &pctx);
633                         ctx->fs_fifo_count++;
634                 } else if ((LINUX_S_ISSOCK (inode.i_mode)) &&
635                            e2fsck_pass1_check_device_inode(&inode)) {
636                         check_immutable(ctx, &pctx);
637                         check_size(ctx, &pctx);
638                         ctx->fs_sockets_count++;
639                 } else
640                         mark_inode_bad(ctx, ino);
641                 if (inode.i_block[EXT2_IND_BLOCK])
642                         ctx->fs_ind_count++;
643                 if (inode.i_block[EXT2_DIND_BLOCK])
644                         ctx->fs_dind_count++;
645                 if (inode.i_block[EXT2_TIND_BLOCK])
646                         ctx->fs_tind_count++;
647                 if (inode.i_block[EXT2_IND_BLOCK] ||
648                     inode.i_block[EXT2_DIND_BLOCK] ||
649                     inode.i_block[EXT2_TIND_BLOCK] ||
650                     inode.i_file_acl) {
651                         inodes_to_process[process_inode_count].ino = ino;
652                         inodes_to_process[process_inode_count].inode = inode;
653                         process_inode_count++;
654                 } else
655                         check_blocks(ctx, &pctx, block_buf);
656
657                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
658                         return;
659
660                 if (process_inode_count >= ctx->process_inode_size) {
661                         process_inodes(ctx, block_buf);
662
663                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
664                                 return;
665                 }
666         next:
667                 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
668                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
669                         return;
670                 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
671                         if (!ctx->inode_bb_map)
672                                 alloc_bb_map(ctx);
673                         ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
674                         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
675                         goto next;
676                 }
677                 if (pctx.errcode) {
678                         fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
679                         ctx->flags |= E2F_FLAG_ABORT;
680                         return;
681                 }
682         }
683         process_inodes(ctx, block_buf);
684         ext2fs_close_inode_scan(scan);
685         ehandler_operation(0);
686
687         /*
688          * If any extended attribute blocks' reference counts need to
689          * be adjusted, either up (ctx->refcount_extra), or down
690          * (ctx->refcount), then fix them.
691          */
692         if (ctx->refcount) {
693                 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
694                 ea_refcount_free(ctx->refcount);
695                 ctx->refcount = 0;
696         }
697         if (ctx->refcount_extra) {
698                 adjust_extattr_refcount(ctx, ctx->refcount_extra,
699                                         block_buf, +1);
700                 ea_refcount_free(ctx->refcount_extra);
701                 ctx->refcount_extra = 0;
702         }
703                 
704         if (ctx->invalid_bitmaps)
705                 handle_fs_bad_blocks(ctx);
706
707         /* We don't need the block_ea_map any more */
708         if (ctx->block_ea_map) {
709                 ext2fs_free_block_bitmap(ctx->block_ea_map);
710                 ctx->block_ea_map = 0;
711         }
712
713         if (ctx->flags & E2F_FLAG_RESTART) {
714                 /*
715                  * Only the master copy of the superblock and block
716                  * group descriptors are going to be written during a
717                  * restart, so set the superblock to be used to be the
718                  * master superblock.
719                  */
720                 ctx->use_superblock = 0;
721                 unwind_pass1(fs);
722                 goto endit;
723         }
724
725         if (ctx->block_dup_map) {
726                 if (ctx->options & E2F_OPT_PREEN) {
727                         clear_problem_context(&pctx);
728                         fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
729                 }
730                 e2fsck_pass1_dupblocks(ctx, block_buf);
731         }
732         ext2fs_free_mem((void **) &inodes_to_process);
733 endit:
734         e2fsck_use_inode_shortcuts(ctx, 0);
735         
736         ext2fs_free_mem((void **) &block_buf);
737
738 #ifdef RESOURCE_TRACK
739         if (ctx->options & E2F_OPT_TIME2) {
740                 e2fsck_clear_progbar(ctx);
741                 print_resource_track(_("Pass 1"), &rtrack);
742         }
743 #endif
744 }
745
746 /*
747  * When the inode_scan routines call this callback at the end of the
748  * glock group, call process_inodes.
749  */
750 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
751                                dgrp_t group, void * priv_data)
752 {
753         struct scan_callback_struct *scan_struct;
754         e2fsck_t ctx;
755
756         scan_struct = (struct scan_callback_struct *) priv_data;
757         ctx = scan_struct->ctx;
758         
759         process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
760
761         if (ctx->progress)
762                 if ((ctx->progress)(ctx, 1, group+1,
763                                     ctx->fs->group_desc_count))
764                         return EXT2_ET_CANCEL_REQUESTED;
765
766         return 0;
767 }
768
769 /*
770  * Process the inodes in the "inodes to process" list.
771  */
772 static void process_inodes(e2fsck_t ctx, char *block_buf)
773 {
774         int                     i;
775         struct ext2_inode       *old_stashed_inode;
776         ext2_ino_t              old_stashed_ino;
777         const char              *old_operation;
778         char                    buf[80];
779         struct problem_context  pctx;
780         
781 #if 0
782         printf("begin process_inodes: ");
783 #endif
784         if (process_inode_count == 0)
785                 return;
786         old_operation = ehandler_operation(0);
787         old_stashed_inode = ctx->stashed_inode;
788         old_stashed_ino = ctx->stashed_ino;
789         qsort(inodes_to_process, process_inode_count,
790                       sizeof(struct process_inode_block), process_inode_cmp);
791         clear_problem_context(&pctx);
792         for (i=0; i < process_inode_count; i++) {
793                 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
794                 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
795                 
796 #if 0
797                 printf("%u ", pctx.ino);
798 #endif
799                 sprintf(buf, _("reading indirect blocks of inode %u"),
800                         pctx.ino);
801                 ehandler_operation(buf);
802                 check_blocks(ctx, &pctx, block_buf);
803                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
804                         break;
805         }
806         ctx->stashed_inode = old_stashed_inode;
807         ctx->stashed_ino = old_stashed_ino;
808         process_inode_count = 0;
809 #if 0
810         printf("end process inodes\n");
811 #endif
812         ehandler_operation(old_operation);
813 }
814
815 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
816 {
817         const struct process_inode_block *ib_a =
818                 (const struct process_inode_block *) a;
819         const struct process_inode_block *ib_b =
820                 (const struct process_inode_block *) b;
821         int     ret;
822         
823         ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
824                ib_b->inode.i_block[EXT2_IND_BLOCK]);
825         if (ret == 0)
826                 ret = ib_a->inode.i_file_acl - ib_b->inode.i_file_acl;
827         return ret;
828 }
829
830 /*
831  * Mark an inode as being bad in some what
832  */
833 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
834 {
835         struct          problem_context pctx;
836
837         if (!ctx->inode_bad_map) {
838                 clear_problem_context(&pctx);
839         
840                 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
841                             _("bad inode map"), &ctx->inode_bad_map);
842                 if (pctx.errcode) {
843                         pctx.num = 3;
844                         fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
845                         /* Should never get here */
846                         ctx->flags |= E2F_FLAG_ABORT;
847                         return;
848                 }
849         }
850         ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
851 }
852
853
854 /*
855  * This procedure will allocate the inode "bb" (badblock) map table
856  */
857 static void alloc_bb_map(e2fsck_t ctx)
858 {
859         struct          problem_context pctx;
860         
861         clear_problem_context(&pctx);
862         pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
863                                               _("inode in bad block map"),
864                                               &ctx->inode_bb_map);
865         if (pctx.errcode) {
866                 pctx.num = 4;
867                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
868                 /* Should never get here */
869                 ctx->flags |= E2F_FLAG_ABORT;
870                 return;
871         }
872 }
873
874 /*
875  * This procedure will allocate the inode imagic table
876  */
877 static void alloc_imagic_map(e2fsck_t ctx)
878 {
879         struct          problem_context pctx;
880         
881         clear_problem_context(&pctx);
882         pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
883                                               _("imagic inode map"),
884                                               &ctx->inode_imagic_map);
885         if (pctx.errcode) {
886                 pctx.num = 5;
887                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
888                 /* Should never get here */
889                 ctx->flags |= E2F_FLAG_ABORT;
890                 return;
891         }
892 }
893
894 /*
895  * Marks a block as in use, setting the dup_map if it's been set
896  * already.  Called by process_block and process_bad_block.
897  *
898  * WARNING: Assumes checks have already been done to make sure block
899  * is valid.  This is true in both process_block and process_bad_block.
900  */
901 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
902 {
903         struct          problem_context pctx;
904         
905         clear_problem_context(&pctx);
906         
907         if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
908                 if (!ctx->block_dup_map) {
909                         pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
910                               _("multiply claimed block map"),
911                               &ctx->block_dup_map);
912                         if (pctx.errcode) {
913                                 pctx.num = 3;
914                                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, 
915                                             &pctx);
916                                 /* Should never get here */
917                                 ctx->flags |= E2F_FLAG_ABORT;
918                                 return;
919                         }
920                 }
921                 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
922         } else {
923                 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
924         }
925 }
926
927 /*
928  * Adjust the extended attribute block's reference counts at the end
929  * of pass 1, either by subtracting out references for EA blocks that
930  * are still referenced in ctx->refcount, or by adding references for
931  * EA blocks that had extra references as accounted for in
932  * ctx->refcount_extra.
933  */
934 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount, 
935                                     char *block_buf, int adjust_sign)
936 {
937         struct ext2_ext_attr_header     *header;
938         struct problem_context          pctx;
939         ext2_filsys                     fs = ctx->fs;
940         blk_t                           blk;
941         __u32                           should_be;
942         int                             count;
943
944         clear_problem_context(&pctx);
945         
946         ea_refcount_intr_begin(refcount);
947         while (1) {
948                 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
949                         break;
950                 pctx.blk = blk;
951                 pctx.errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
952                 if (pctx.errcode) {
953                         fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
954                         return;
955                 }
956                 header = (struct ext2_ext_attr_header *) block_buf;
957                 pctx.blkcount = header->h_refcount;
958                 should_be = header->h_refcount + adjust_sign * count;
959                 pctx.num = should_be;
960                 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
961                         header->h_refcount = should_be;
962                         pctx.errcode = ext2fs_write_ext_attr(fs, blk,
963                                                              block_buf);
964                         if (pctx.errcode) {
965                                 fix_problem(ctx, PR_1_EXTATTR_WRITE, &pctx);
966                                 continue;
967                         }
968                 }
969         }
970 }
971
972 /*
973  * Handle processing the extended attribute blocks
974  */
975 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
976                            char *block_buf)
977 {
978         ext2_filsys fs = ctx->fs;
979         ext2_ino_t      ino = pctx->ino;
980         struct ext2_inode *inode = pctx->inode;
981         blk_t           blk;
982         char *          end;
983         struct ext2_ext_attr_header *header;
984         struct ext2_ext_attr_entry *entry;
985         int             count;
986         region_t        region;
987         int             ext_attr_ver;
988         
989         blk = inode->i_file_acl;
990         if (blk == 0)
991                 return 0;
992
993         /*
994          * If the Extended attribute flag isn't set, then a non-zero
995          * file acl means that the inode is corrupted.
996          *
997          * Or if the extended attribute block is an invalid block,
998          * then the inode is also corrupted.
999          */
1000         if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1001             (blk < fs->super->s_first_data_block) ||
1002             (blk >= fs->super->s_blocks_count)) {
1003                 mark_inode_bad(ctx, ino);
1004                 return 0;
1005         }
1006
1007         /* If ea bitmap hasn't been allocated, create it */
1008         if (!ctx->block_ea_map) {
1009                 pctx->errcode = ext2fs_allocate_block_bitmap(fs,
1010                                                       _("ext attr block map"),
1011                                                       &ctx->block_ea_map);
1012                 if (pctx->errcode) {
1013                         pctx->num = 2;
1014                         fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1015                         ctx->flags |= E2F_FLAG_ABORT;
1016                         return 0;
1017                 }
1018         }
1019
1020         /* Create the EA refcount structure if necessary */
1021         if (!ctx->refcount) {
1022                 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1023                 if (pctx->errcode) {
1024                         pctx->num = 1;
1025                         fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1026                         ctx->flags |= E2F_FLAG_ABORT;
1027                         return 0;
1028                 }
1029         }
1030
1031 #if 0
1032         /* Debugging text */
1033         printf("Inode %u has EA block %u\n", ino, blk);
1034 #endif
1035
1036         /* Have we seen this EA block before? */
1037         if (ext2fs_fast_test_block_bitmap(ctx->block_ea_map, blk)) {
1038                 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1039                         return 1;
1040                 /* Ooops, this EA was referenced more than it stated */
1041                 if (!ctx->refcount_extra) {
1042                         pctx->errcode = ea_refcount_create(0,
1043                                            &ctx->refcount_extra);
1044                         if (pctx->errcode) {
1045                                 pctx->num = 2;
1046                                 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1047                                 ctx->flags |= E2F_FLAG_ABORT;
1048                                 return 0;
1049                         }
1050                 }
1051                 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1052                 return 1;
1053         }
1054         
1055         /*
1056          * OK, we haven't seen this EA block yet.  So we need to
1057          * validate it
1058          */
1059         pctx->blk = blk;
1060         pctx->errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
1061         if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1062                 goto clear_extattr;
1063         header = (struct ext2_ext_attr_header *) block_buf;
1064         pctx->blk = inode->i_file_acl;
1065         if (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)
1066                 ext_attr_ver = 1;
1067         if (header->h_magic != EXT2_EXT_ATTR_MAGIC)
1068                 ext_attr_ver = 2;
1069         else if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1070                 goto clear_extattr;
1071
1072         if (header->h_blocks != 1) {
1073                 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1074                         goto clear_extattr;
1075         }
1076
1077         region = region_create(0, fs->blocksize);
1078         if (!region) {
1079                 fix_problem(ctx, PR_1_EA_ALLOC_REGION, pctx);
1080                 ctx->flags |= E2F_FLAG_ABORT;
1081                 return 0;
1082         }
1083         if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1084                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1085                         goto clear_extattr;
1086         }
1087         
1088         entry = (struct ext2_ext_attr_entry *)(header+1);
1089         end = block_buf + fs->blocksize;
1090         while ((char *)entry < end && *(__u32 *)entry) {
1091                 if (region_allocate(region, (char *)entry - (char *)header,
1092                                    EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1093                         if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1094                                 goto clear_extattr;
1095                 }
1096                 if ((ext_attr_ver == 1 &&
1097                      (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1098                     (ext_attr_ver == 2 &&
1099                      entry->e_name_index == 0)) {
1100                         if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
1101                                 goto clear_extattr;
1102                 }
1103                 if (entry->e_value_block != 0) {
1104                         if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1105                                 goto clear_extattr;
1106                 }
1107                 if (entry->e_value_size &&
1108                     region_allocate(region, entry->e_value_offs,
1109                                     EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
1110                         if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1111                                 goto clear_extattr;
1112                 }
1113                 entry = EXT2_EXT_ATTR_NEXT(entry);
1114         }
1115         if (region_allocate(region, (char *)entry - (char *)header, 4)) {
1116                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1117                         goto clear_extattr;
1118         }
1119         region_free(region);
1120
1121         count = header->h_refcount - 1;
1122         if (count)
1123                 ea_refcount_store(ctx->refcount, blk, count);
1124         mark_block_used(ctx, blk);
1125         ext2fs_fast_mark_block_bitmap(ctx->block_ea_map, blk);
1126         
1127         return 1;
1128
1129 clear_extattr:
1130         inode->i_file_acl = 0;
1131         e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
1132         return 0;
1133 }
1134
1135 /* Returns 1 if bad htree, 0 if OK */
1136 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
1137                         ext2_ino_t ino, struct ext2_inode *inode,
1138                         char *block_buf)
1139 {
1140         struct ext2_dx_root_info        *root;
1141         ext2_filsys                     fs = ctx->fs;
1142         errcode_t                       retval;
1143         blk_t                           blk;
1144
1145         if ((!LINUX_S_ISDIR(inode->i_mode) &&
1146              fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
1147             (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
1148              fix_problem(ctx, PR_1_HTREE_SET, pctx)))
1149                 return 1;
1150
1151         blk = inode->i_block[0];
1152         if (((blk == 0) ||
1153              (blk < fs->super->s_first_data_block) ||
1154              (blk >= fs->super->s_blocks_count)) &&
1155             fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1156                 return 1;
1157
1158         retval = io_channel_read_blk(fs->io, blk, 1, block_buf);
1159         if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1160                 return 1;
1161         
1162         /* XXX should check that beginning matches a directory */
1163         root = (struct ext2_dx_root_info *) (block_buf + 24);
1164
1165         if ((root->reserved_zero || root->info_length < 8) &&
1166             fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1167                 return 1;
1168
1169         pctx->num = root->hash_version;
1170         if ((root->hash_version != EXT2_HASH_LEGACY) &&
1171             (root->hash_version != EXT2_HASH_HALF_MD4) &&
1172             fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
1173                 return 1;
1174                 
1175         if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
1176             fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
1177                 return 1;
1178
1179         pctx->num = root->indirect_levels;
1180         if ((root->indirect_levels > 1) &&
1181             fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
1182                 return 1;
1183         
1184         return 0;
1185 }
1186
1187 /*
1188  * This subroutine is called on each inode to account for all of the
1189  * blocks used by that inode.
1190  */
1191 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
1192                          char *block_buf)
1193 {
1194         ext2_filsys fs = ctx->fs;
1195         struct process_block_struct pb;
1196         ext2_ino_t      ino = pctx->ino;
1197         struct ext2_inode *inode = pctx->inode;
1198         int             bad_size = 0;
1199         int             dirty_inode = 0;
1200         __u64           size;
1201         
1202         if (!ext2fs_inode_has_valid_blocks(inode))
1203                 return;
1204         
1205         pb.ino = ino;
1206         pb.num_blocks = pb.last_block = 0;
1207         pb.num_illegal_blocks = 0;
1208         pb.suppress = 0; pb.clear = 0;
1209         pb.fragmented = 0;
1210         pb.compressed = 0;
1211         pb.previous_block = 0;
1212         pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1213         pb.is_reg = LINUX_S_ISREG(inode->i_mode);
1214         pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
1215         pb.inode = inode;
1216         pb.pctx = pctx;
1217         pb.ctx = ctx;
1218         pctx->ino = ino;
1219
1220         if (inode->i_flags & EXT2_COMPRBLK_FL) {
1221                 if (fs->super->s_feature_incompat &
1222                     EXT2_FEATURE_INCOMPAT_COMPRESSION)
1223                         pb.compressed = 1;
1224                 else {
1225                         if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
1226                                 inode->i_flags &= ~EXT2_COMPRBLK_FL;
1227                                 dirty_inode++;
1228                         }
1229                 }
1230         }
1231
1232         pctx->errcode = ext2fs_block_iterate2(fs, ino,
1233                                        pb.is_dir ? BLOCK_FLAG_HOLE : 0,
1234                                        block_buf, process_block, &pb);
1235         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1236                 goto out;
1237         end_problem_latch(ctx, PR_LATCH_BLOCK);
1238         end_problem_latch(ctx, PR_LATCH_TOOBIG);
1239         if (pctx->errcode)
1240                 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
1241
1242         if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
1243                 ctx->fs_fragmented++;
1244
1245         if (pb.clear) {
1246                 inode->i_links_count = 0;
1247                 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1248                 inode->i_dtime = time(0);
1249                 dirty_inode++;
1250                 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1251                 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1252                 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1253                 /*
1254                  * The inode was probably partially accounted for
1255                  * before processing was aborted, so we need to
1256                  * restart the pass 1 scan.
1257                  */
1258                 ctx->flags |= E2F_FLAG_RESTART;
1259                 goto out;
1260         }
1261         
1262         if (inode->i_flags & EXT2_INDEX_FL) {
1263                 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
1264                         inode->i_flags &= ~EXT2_INDEX_FL;
1265                         dirty_inode++;
1266                 } else {
1267 #ifdef ENABLE_HTREE
1268                         e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
1269 #endif
1270                 }
1271         }
1272         
1273         if (inode->i_file_acl && check_ext_attr(ctx, pctx, block_buf))
1274                 pb.num_blocks++;
1275
1276         pb.num_blocks *= (fs->blocksize / 512);
1277 #if 0
1278         printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
1279                ino, inode->i_size, pb.last_block, inode->i_blocks,
1280                pb.num_blocks);
1281 #endif
1282         if (!pb.num_blocks && pb.is_dir) {
1283                 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
1284                         inode->i_links_count = 0;
1285                         ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1286                         inode->i_dtime = time(0);
1287                         dirty_inode++;
1288                         ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1289                         ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1290                         ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1291                         ctx->fs_directory_count--;
1292                         pb.is_dir = 0;
1293                 }
1294         }
1295         if (pb.is_dir) {
1296                 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
1297                 if (nblock > (pb.last_block + 1))
1298                         bad_size = 1;
1299                 else if (nblock < (pb.last_block + 1)) {
1300                         if (((pb.last_block + 1) - nblock) >
1301                             fs->super->s_prealloc_dir_blocks)
1302                                 bad_size = 2;
1303                 }
1304         } else {
1305                 if (!LINUX_S_ISREG(inode->i_mode) && inode->i_size_high)
1306                         bad_size = 5;
1307                 size = inode->i_size | ((__u64) inode->i_size_high << 32);
1308                 if ((size < pb.last_block * fs->blocksize))
1309                         bad_size = 3;
1310                 else if (size > ext2_max_sizes[fs->super->s_log_block_size])
1311                         bad_size = 4;
1312         }
1313         if (bad_size) {
1314                 pctx->num = (pb.last_block+1) * fs->blocksize;
1315                 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
1316                         inode->i_size = pctx->num;
1317                         if (LINUX_S_ISREG(inode->i_mode))
1318                                 inode->i_size_high = pctx->num >> 32;
1319                         dirty_inode++;
1320                 }
1321                 pctx->num = 0;
1322         }
1323         if (LINUX_S_ISREG(inode->i_mode) &&
1324             (inode->i_size_high || inode->i_size & 0x80000000UL))
1325                 ctx->large_files++;
1326         if (pb.num_blocks != inode->i_blocks) {
1327                 pctx->num = pb.num_blocks;
1328                 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
1329                         inode->i_blocks = pb.num_blocks;
1330                         dirty_inode++;
1331                 }
1332                 pctx->num = 0;
1333         }
1334 out:
1335         if (dirty_inode)
1336                 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
1337 }
1338
1339 #if 0
1340 /*
1341  * Helper function called by process block when an illegal block is
1342  * found.  It returns a description about why the block is illegal
1343  */
1344 static char *describe_illegal_block(ext2_filsys fs, blk_t block)
1345 {
1346         blk_t   super;
1347         int     i;
1348         static char     problem[80];
1349
1350         super = fs->super->s_first_data_block;
1351         strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
1352         if (block < super) {
1353                 sprintf(problem, "< FIRSTBLOCK (%u)", super);
1354                 return(problem);
1355         } else if (block >= fs->super->s_blocks_count) {
1356                 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
1357                 return(problem);
1358         }
1359         for (i = 0; i < fs->group_desc_count; i++) {
1360                 if (block == super) {
1361                         sprintf(problem, "is the superblock in group %d", i);
1362                         break;
1363                 }
1364                 if (block > super &&
1365                     block <= (super + fs->desc_blocks)) {
1366                         sprintf(problem, "is in the group descriptors "
1367                                 "of group %d", i);
1368                         break;
1369                 }
1370                 if (block == fs->group_desc[i].bg_block_bitmap) {
1371                         sprintf(problem, "is the block bitmap of group %d", i);
1372                         break;
1373                 }
1374                 if (block == fs->group_desc[i].bg_inode_bitmap) {
1375                         sprintf(problem, "is the inode bitmap of group %d", i);
1376                         break;
1377                 }
1378                 if (block >= fs->group_desc[i].bg_inode_table &&
1379                     (block < fs->group_desc[i].bg_inode_table
1380                      + fs->inode_blocks_per_group)) {
1381                         sprintf(problem, "is in the inode table of group %d",
1382                                 i);
1383                         break;
1384                 }
1385                 super += fs->super->s_blocks_per_group;
1386         }
1387         return(problem);
1388 }
1389 #endif
1390
1391 /*
1392  * This is a helper function for check_blocks().
1393  */
1394 static int process_block(ext2_filsys fs,
1395                   blk_t *block_nr,
1396                   e2_blkcnt_t blockcnt,
1397                   blk_t ref_block,
1398                   int ref_offset, 
1399                   void *priv_data)
1400 {
1401         struct process_block_struct *p;
1402         struct problem_context *pctx;
1403         blk_t   blk = *block_nr;
1404         int     ret_code = 0;
1405         int     problem = 0;
1406         e2fsck_t        ctx;
1407
1408         p = (struct process_block_struct *) priv_data;
1409         pctx = p->pctx;
1410         ctx = p->ctx;
1411
1412         if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
1413                 /* todo: Check that the comprblk_fl is high, that the
1414                    blkaddr pattern looks right (all non-holes up to
1415                    first EXT2FS_COMPRESSED_BLKADDR, then all
1416                    EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
1417                    that the feature_incompat bit is high, and that the
1418                    inode is a regular file.  If we're doing a "full
1419                    check" (a concept introduced to e2fsck by e2compr,
1420                    meaning that we look at data blocks as well as
1421                    metadata) then call some library routine that
1422                    checks the compressed data.  I'll have to think
1423                    about this, because one particularly important
1424                    problem to be able to fix is to recalculate the
1425                    cluster size if necessary.  I think that perhaps
1426                    we'd better do most/all e2compr-specific checks
1427                    separately, after the non-e2compr checks.  If not
1428                    doing a full check, it may be useful to test that
1429                    the personality is linux; e.g. if it isn't then
1430                    perhaps this really is just an illegal block. */
1431                 return 0;
1432         }
1433
1434         if (blk == 0) {
1435                 if (p->is_dir == 0) {
1436                         /*
1437                          * Should never happen, since only directories
1438                          * get called with BLOCK_FLAG_HOLE
1439                          */
1440 #if DEBUG_E2FSCK
1441                         printf("process_block() called with blk == 0, "
1442                                "blockcnt=%d, inode %lu???\n",
1443                                blockcnt, p->ino);
1444 #endif
1445                         return 0;
1446                 }
1447                 if (blockcnt < 0)
1448                         return 0;
1449                 if (blockcnt * fs->blocksize < p->inode->i_size) {
1450 #if 0
1451                         printf("Missing block (#%d) in directory inode %lu!\n",
1452                                blockcnt, p->ino);
1453 #endif
1454                         goto mark_dir;
1455                 }
1456                 return 0;
1457         }
1458
1459 #if 0
1460         printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
1461                blockcnt);
1462 #endif
1463         
1464         /*
1465          * Simplistic fragmentation check.  We merely require that the
1466          * file be contiguous.  (Which can never be true for really
1467          * big files that are greater than a block group.)
1468          */
1469         if (!HOLE_BLKADDR(p->previous_block)) {
1470                 if (p->previous_block+1 != blk)
1471                         p->fragmented = 1;
1472         }
1473         p->previous_block = blk;
1474
1475         if (p->is_dir && blockcnt > (1 << (15 - fs->super->s_log_block_size)))
1476                 problem = PR_1_TOOBIG_DIR;
1477         if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
1478                 problem = PR_1_TOOBIG_REG;
1479         if (!p->is_dir && !p->is_reg && blockcnt > 0)
1480                 problem = PR_1_TOOBIG_SYMLINK;
1481             
1482         if (blk < fs->super->s_first_data_block ||
1483             blk >= fs->super->s_blocks_count)
1484                 problem = PR_1_ILLEGAL_BLOCK_NUM;
1485
1486         if (problem) {
1487                 p->num_illegal_blocks++;
1488                 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
1489                         if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
1490                                 p->clear = 1;
1491                                 return BLOCK_ABORT;
1492                         }
1493                         if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
1494                                 p->suppress = 1;
1495                                 set_latch_flags(PR_LATCH_BLOCK,
1496                                                 PRL_SUPPRESS, 0);
1497                         }
1498                 }
1499                 pctx->blk = blk;
1500                 pctx->blkcount = blockcnt;
1501                 if (fix_problem(ctx, problem, pctx)) {
1502                         blk = *block_nr = 0;
1503                         ret_code = BLOCK_CHANGED;
1504                         goto mark_dir;
1505                 } else
1506                         return 0;
1507         }
1508
1509         mark_block_used(ctx, blk);
1510         p->num_blocks++;
1511         if (blockcnt >= 0)
1512                 p->last_block = blockcnt;
1513 mark_dir:
1514         if (p->is_dir && (blockcnt >= 0)) {
1515                 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
1516                                                     blk, blockcnt);
1517                 if (pctx->errcode) {
1518                         pctx->blk = blk;
1519                         pctx->num = blockcnt;
1520                         fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1521                         /* Should never get here */
1522                         ctx->flags |= E2F_FLAG_ABORT;
1523                         return BLOCK_ABORT;
1524                 }
1525         }
1526         return ret_code;
1527 }
1528
1529 static void bad_block_indirect(e2fsck_t ctx, blk_t blk)
1530 {
1531         struct problem_context pctx;
1532
1533         clear_problem_context(&pctx);
1534         /*
1535          * Prompt to see if we should continue or not.
1536          */
1537         if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, &pctx))
1538                 ctx->flags |= E2F_FLAG_ABORT;
1539 }
1540
1541 static int process_bad_block(ext2_filsys fs,
1542                       blk_t *block_nr,
1543                       e2_blkcnt_t blockcnt,
1544                       blk_t ref_block,
1545                       int ref_offset,
1546                       void *priv_data)
1547 {
1548         struct process_block_struct *p;
1549         blk_t           blk = *block_nr;
1550         int             first_block;
1551         int             i;
1552         struct problem_context *pctx;
1553         e2fsck_t        ctx;
1554
1555         /*
1556          * Note: This function processes blocks for the bad blocks
1557          * inode, which is never compressed.  So we don't use HOLE_BLKADDR().
1558          */
1559
1560         if (!blk)
1561                 return 0;
1562         
1563         p = (struct process_block_struct *) priv_data;
1564         ctx = p->ctx;
1565         pctx = p->pctx;
1566         
1567         pctx->ino = EXT2_BAD_INO;
1568         pctx->blk = blk;
1569         pctx->blkcount = blockcnt;
1570
1571         if ((blk < fs->super->s_first_data_block) ||
1572             (blk >= fs->super->s_blocks_count)) {
1573                 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
1574                         *block_nr = 0;
1575                         return BLOCK_CHANGED;
1576                 } else
1577                         return 0;
1578         }
1579
1580         if (blockcnt < 0) {
1581                 if (ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1582                         bad_block_indirect(ctx, blk);
1583                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1584                                 return BLOCK_ABORT;
1585                 } else
1586                         mark_block_used(ctx, blk);
1587                 return 0;
1588         }
1589 #if 0 
1590         printf ("DEBUG: Marking %u as bad.\n", blk);
1591 #endif
1592         ctx->fs_badblocks_count++;
1593         /*
1594          * If the block is not used, then mark it as used and return.
1595          * If it is already marked as found, this must mean that
1596          * there's an overlap between the filesystem table blocks
1597          * (bitmaps and inode table) and the bad block list.
1598          */
1599         if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1600                 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1601                 return 0;
1602         }
1603         /*
1604          * Try to find the where the filesystem block was used...
1605          */
1606         first_block = fs->super->s_first_data_block;
1607         
1608         for (i = 0; i < fs->group_desc_count; i++ ) {
1609                 pctx->group = i;
1610                 pctx->blk = blk;
1611                 if (!ext2fs_bg_has_super(fs, i))
1612                         goto skip_super;
1613                 if (blk == first_block) {
1614                         if (i == 0) {
1615                                 if (fix_problem(ctx,
1616                                                 PR_1_BAD_PRIMARY_SUPERBLOCK,
1617                                                 pctx)) {
1618                                         *block_nr = 0;
1619                                         return BLOCK_CHANGED;
1620                                 }
1621                                 return 0;
1622                         }
1623                         fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
1624                         return 0;
1625                 }
1626                 if ((blk > first_block) &&
1627                     (blk <= first_block + fs->desc_blocks)) {
1628                         if (i == 0) {
1629                                 pctx->blk = *block_nr;
1630                                 if (fix_problem(ctx,
1631                         PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
1632                                         *block_nr = 0;
1633                                         return BLOCK_CHANGED;
1634                                 }
1635                                 return 0;
1636                         }
1637                         fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
1638                         return 0;
1639                 }
1640         skip_super:
1641                 if (blk == fs->group_desc[i].bg_block_bitmap) {
1642                         if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
1643                                 ctx->invalid_block_bitmap_flag[i]++;
1644                                 ctx->invalid_bitmaps++;
1645                         }
1646                         return 0;
1647                 }
1648                 if (blk == fs->group_desc[i].bg_inode_bitmap) {
1649                         if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
1650                                 ctx->invalid_inode_bitmap_flag[i]++;
1651                                 ctx->invalid_bitmaps++;
1652                         }
1653                         return 0;
1654                 }
1655                 if ((blk >= fs->group_desc[i].bg_inode_table) &&
1656                     (blk < (fs->group_desc[i].bg_inode_table +
1657                             fs->inode_blocks_per_group))) {
1658                         /*
1659                          * If there are bad blocks in the inode table,
1660                          * the inode scan code will try to do
1661                          * something reasonable automatically.
1662                          */
1663                         return 0;
1664                 }
1665                 first_block += fs->super->s_blocks_per_group;
1666         }
1667         /*
1668          * If we've gotten to this point, then the only
1669          * possibility is that the bad block inode meta data
1670          * is using a bad block.
1671          */
1672         if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
1673             p->inode->i_block[EXT2_DIND_BLOCK]) {
1674                 bad_block_indirect(ctx, blk);
1675                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1676                         return BLOCK_ABORT;
1677                 return 0;
1678         }
1679
1680         pctx->group = -1;
1681
1682         /* Warn user that the block wasn't claimed */
1683         fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
1684
1685         return 0;
1686 }
1687
1688 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group, 
1689                             const char *name, int num, blk_t *new_block)
1690 {
1691         ext2_filsys fs = ctx->fs;
1692         blk_t           old_block = *new_block;
1693         int             i;
1694         char            *buf;
1695         struct problem_context  pctx;
1696
1697         clear_problem_context(&pctx);
1698
1699         pctx.group = group;
1700         pctx.blk = old_block;
1701         pctx.str = name;
1702
1703         pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
1704                         first_block + fs->super->s_blocks_per_group,
1705                                         num, ctx->block_found_map, new_block);
1706         if (pctx.errcode) {
1707                 pctx.num = num;
1708                 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
1709                 ext2fs_unmark_valid(fs);
1710                 return;
1711         }
1712         pctx.errcode = ext2fs_get_mem(fs->blocksize, (void **) &buf);
1713         if (pctx.errcode) {
1714                 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
1715                 ext2fs_unmark_valid(fs);
1716                 return;
1717         }
1718         ext2fs_mark_super_dirty(fs);
1719         pctx.blk2 = *new_block;
1720         fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
1721                           PR_1_RELOC_TO), &pctx);
1722         pctx.blk2 = 0;
1723         for (i = 0; i < num; i++) {
1724                 pctx.blk = i;
1725                 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
1726                 if (old_block) {
1727                         pctx.errcode = io_channel_read_blk(fs->io,
1728                                    old_block + i, 1, buf);
1729                         if (pctx.errcode)
1730                                 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
1731                 } else
1732                         memset(buf, 0, fs->blocksize);
1733
1734                 pctx.blk = (*new_block) + i;
1735                 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
1736                                               1, buf);
1737                 if (pctx.errcode)
1738                         fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
1739         }
1740         ext2fs_free_mem((void **) &buf);
1741 }
1742
1743 /*
1744  * This routine gets called at the end of pass 1 if bad blocks are
1745  * detected in the superblock, group descriptors, inode_bitmaps, or
1746  * block bitmaps.  At this point, all of the blocks have been mapped
1747  * out, so we can try to allocate new block(s) to replace the bad
1748  * blocks.
1749  */
1750 static void handle_fs_bad_blocks(e2fsck_t ctx)
1751 {
1752         ext2_filsys fs = ctx->fs;
1753         int             i;
1754         int             first_block = fs->super->s_first_data_block;
1755
1756         for (i = 0; i < fs->group_desc_count; i++) {
1757                 if (ctx->invalid_block_bitmap_flag[i]) {
1758                         new_table_block(ctx, first_block, i, _("block bitmap"),
1759                                         1, &fs->group_desc[i].bg_block_bitmap);
1760                 }
1761                 if (ctx->invalid_inode_bitmap_flag[i]) {
1762                         new_table_block(ctx, first_block, i, _("inode bitmap"),
1763                                         1, &fs->group_desc[i].bg_inode_bitmap);
1764                 }
1765                 if (ctx->invalid_inode_table_flag[i]) {
1766                         new_table_block(ctx, first_block, i, _("inode table"),
1767                                         fs->inode_blocks_per_group, 
1768                                         &fs->group_desc[i].bg_inode_table);
1769                         ctx->flags |= E2F_FLAG_RESTART;
1770                 }
1771                 first_block += fs->super->s_blocks_per_group;
1772         }
1773         ctx->invalid_bitmaps = 0;
1774 }
1775
1776 /*
1777  * This routine marks all blocks which are used by the superblock,
1778  * group descriptors, inode bitmaps, and block bitmaps.
1779  */
1780 static void mark_table_blocks(e2fsck_t ctx)
1781 {
1782         ext2_filsys fs = ctx->fs;
1783         blk_t   block, b;
1784         int     i,j;
1785         struct problem_context pctx;
1786         
1787         clear_problem_context(&pctx);
1788         
1789         block = fs->super->s_first_data_block;
1790         for (i = 0; i < fs->group_desc_count; i++) {
1791                 pctx.group = i;
1792
1793                 if (ext2fs_bg_has_super(fs, i)) {
1794                         /*
1795                          * Mark this group's copy of the superblock
1796                          */
1797                         ext2fs_mark_block_bitmap(ctx->block_found_map, block);
1798                 
1799                         /*
1800                          * Mark this group's copy of the descriptors
1801                          */
1802                         for (j = 0; j < fs->desc_blocks; j++) {
1803                                 ext2fs_mark_block_bitmap(ctx->block_found_map,
1804                                                          block + j + 1);
1805                         }
1806                 }
1807                 
1808                 /*
1809                  * Mark the blocks used for the inode table
1810                  */
1811                 if (fs->group_desc[i].bg_inode_table) {
1812                         for (j = 0, b = fs->group_desc[i].bg_inode_table;
1813                              j < fs->inode_blocks_per_group;
1814                              j++, b++) {
1815                                 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1816                                                              b)) {
1817                                         pctx.blk = b;
1818                                         if (fix_problem(ctx,
1819                                                 PR_1_ITABLE_CONFLICT, &pctx)) {
1820                                                 ctx->invalid_inode_table_flag[i]++;
1821                                                 ctx->invalid_bitmaps++;
1822                                         }
1823                                 } else {
1824                                     ext2fs_mark_block_bitmap(ctx->block_found_map,
1825                                                              b);
1826                                 }
1827                         }
1828                 }
1829                             
1830                 /*
1831                  * Mark block used for the block bitmap 
1832                  */
1833                 if (fs->group_desc[i].bg_block_bitmap) {
1834                         if (ext2fs_test_block_bitmap(ctx->block_found_map,
1835                                      fs->group_desc[i].bg_block_bitmap)) {
1836                                 pctx.blk = fs->group_desc[i].bg_block_bitmap;
1837                                 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
1838                                         ctx->invalid_block_bitmap_flag[i]++;
1839                                         ctx->invalid_bitmaps++;
1840                                 }
1841                         } else {
1842                             ext2fs_mark_block_bitmap(ctx->block_found_map,
1843                                      fs->group_desc[i].bg_block_bitmap);
1844                     }
1845                         
1846                 }
1847                 /*
1848                  * Mark block used for the inode bitmap 
1849                  */
1850                 if (fs->group_desc[i].bg_inode_bitmap) {
1851                         if (ext2fs_test_block_bitmap(ctx->block_found_map,
1852                                      fs->group_desc[i].bg_inode_bitmap)) {
1853                                 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
1854                                 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
1855                                         ctx->invalid_inode_bitmap_flag[i]++;
1856                                         ctx->invalid_bitmaps++;
1857                                 } 
1858                         } else {
1859                             ext2fs_mark_block_bitmap(ctx->block_found_map,
1860                                      fs->group_desc[i].bg_inode_bitmap);
1861                         }
1862                 }
1863                 block += fs->super->s_blocks_per_group;
1864         }
1865 }
1866         
1867 /*
1868  * Thes subroutines short circuits ext2fs_get_blocks and
1869  * ext2fs_check_directory; we use them since we already have the inode
1870  * structure, so there's no point in letting the ext2fs library read
1871  * the inode again.
1872  */
1873 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
1874                                   blk_t *blocks)
1875 {
1876         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1877         int     i;
1878         
1879         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
1880                 return EXT2_ET_CALLBACK_NOTHANDLED;
1881
1882         for (i=0; i < EXT2_N_BLOCKS; i++)
1883                 blocks[i] = ctx->stashed_inode->i_block[i];
1884         return 0;
1885 }
1886
1887 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
1888                                   struct ext2_inode *inode)
1889 {
1890         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1891
1892         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
1893                 return EXT2_ET_CALLBACK_NOTHANDLED;
1894         *inode = *ctx->stashed_inode;
1895         return 0;
1896 }
1897
1898 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
1899                             struct ext2_inode *inode)
1900 {
1901         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1902
1903         if ((ino == ctx->stashed_ino) && ctx->stashed_inode)
1904                 *ctx->stashed_inode = *inode;
1905         return EXT2_ET_CALLBACK_NOTHANDLED;
1906 }
1907
1908 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
1909 {
1910         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1911
1912         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
1913                 return EXT2_ET_CALLBACK_NOTHANDLED;
1914
1915         if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
1916                 return EXT2_ET_NO_DIRECTORY;
1917         return 0;
1918 }
1919
1920 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
1921 {
1922         ext2_filsys fs = ctx->fs;
1923
1924         if (bool) {
1925                 fs->get_blocks = pass1_get_blocks;
1926                 fs->check_directory = pass1_check_directory;
1927                 fs->read_inode = pass1_read_inode;
1928                 fs->write_inode = pass1_write_inode;
1929                 ctx->stashed_ino = 0;
1930         } else {
1931                 fs->get_blocks = 0;
1932                 fs->check_directory = 0;
1933                 fs->read_inode = 0;
1934                 fs->write_inode = 0;
1935         }
1936 }