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