Whamcloud - gitweb
Merge branch 'cl/remove-masix' into next
[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 EXT2FS_ATTR((unused)), 
132                                     struct ext2_inode *inode)
133 {
134         int     i;
135
136         /*
137          * If the index flag is set, then this is a bogus
138          * device/fifo/socket
139          */
140         if (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 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
245 {
246         struct ext2_super_block *sb = ctx->fs->super;
247         struct ext2_inode_large *inode;
248         struct ext2_ext_attr_entry *entry;
249         char *start, *end;
250         unsigned int storage_size, remain;
251         int problem = 0;
252
253         inode = (struct ext2_inode_large *) pctx->inode;
254         storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
255                 inode->i_extra_isize;
256         start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
257                 inode->i_extra_isize + sizeof(__u32);
258         end = (char *) inode + EXT2_INODE_SIZE(ctx->fs->super);
259         entry = (struct ext2_ext_attr_entry *) start;
260
261         /* scan all entry's headers first */
262
263         /* take finish entry 0UL into account */
264         remain = storage_size - sizeof(__u32); 
265
266         while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
267
268                 /* header eats this space */
269                 remain -= sizeof(struct ext2_ext_attr_entry);
270                 
271                 /* is attribute name valid? */
272                 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
273                         pctx->num = entry->e_name_len;
274                         problem = PR_1_ATTR_NAME_LEN;
275                         goto fix;
276                 }
277
278                 /* attribute len eats this space */
279                 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
280
281                 /* check value size */
282                 if (entry->e_value_size == 0 || entry->e_value_size > remain) {
283                         pctx->num = entry->e_value_size;
284                         problem = PR_1_ATTR_VALUE_SIZE;
285                         goto fix;
286                 }
287
288                 /* e_value_block must be 0 in inode's ea */
289                 if (entry->e_value_block != 0) {
290                         pctx->num = entry->e_value_block;
291                         problem = PR_1_ATTR_VALUE_BLOCK;
292                         goto fix;
293                 }
294                 
295                 /* e_hash must be 0 in inode's ea */
296                 if (entry->e_hash != 0) {
297                         pctx->num = entry->e_hash;
298                         problem = PR_1_ATTR_HASH;
299                         goto fix;
300                 }
301
302                 remain -= entry->e_value_size;
303
304                 entry = EXT2_EXT_ATTR_NEXT(entry);
305         }
306 fix:
307         /*
308          * it seems like a corruption. it's very unlikely we could repair
309          * EA(s) in automatic fashion -bzzz
310          */
311 #if 0
312         problem = PR_1_ATTR_HASH;
313 #endif
314         if (problem == 0 || !fix_problem(ctx, problem, pctx))
315                 return;
316
317         /* simple remove all possible EA(s) */
318         *((__u32 *)start) = 0UL;
319         e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
320                                 EXT2_INODE_SIZE(sb), "pass1");
321 }
322
323 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
324 {
325         struct ext2_super_block *sb = ctx->fs->super;
326         struct ext2_inode_large *inode;
327         __u32 *eamagic;
328         int min, max;
329
330         inode = (struct ext2_inode_large *) pctx->inode;
331         if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
332                 /* this isn't large inode. so, nothing to check */
333                 return;
334         }
335
336 #if 0
337         printf("inode #%u, i_extra_size %d\n", pctx->ino,
338                         inode->i_extra_isize);
339 #endif  
340         /* i_extra_isize must cover i_extra_isize + i_pad1 at least */
341         min = sizeof(inode->i_extra_isize) + sizeof(inode->i_pad1);
342         max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
343         /* 
344          * For now we will allow i_extra_isize to be 0, but really
345          * implementations should never allow i_extra_isize to be 0
346          */
347         if (inode->i_extra_isize &&
348             (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
349                 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
350                         return;
351                 inode->i_extra_isize = min;
352                 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
353                                         EXT2_INODE_SIZE(sb), "pass1");
354                 return;
355         }
356
357         eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
358                         inode->i_extra_isize);
359         if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
360                 /* it seems inode has an extended attribute(s) in body */
361                 check_ea_in_inode(ctx, pctx);
362         }
363 }
364
365 /* 
366  * Check to see if the inode might really be a directory, despite i_mode
367  *
368  * This is a lot of complexity for something for which I'm not really
369  * convinced happens frequently in the wild.  If for any reason this
370  * causes any problems, take this code out.
371  * [tytso:20070331.0827EDT]
372  */
373 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
374                                 char *buf)
375 {
376         struct ext2_inode *inode = pctx->inode;
377         struct ext2_dir_entry   *dirent;
378         const char              *old_op;
379         errcode_t               retval;
380         blk_t                   blk;
381         int                     i, not_device = 0;
382
383         if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
384             LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
385                 return;
386
387         for (i=0; i < EXT2_N_BLOCKS; i++) {
388                 blk = inode->i_block[i];
389                 if (!blk)
390                         continue;
391                 if (i >= 4)
392                         not_device++;
393
394                 if (blk < ctx->fs->super->s_first_data_block ||
395                     blk >= ctx->fs->super->s_blocks_count ||
396                     ext2fs_fast_test_block_bitmap(ctx->block_found_map, blk))
397                         return; /* Invalid block, can't be dir */
398         }
399
400         if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) && 
401             (inode->i_links_count == 1) && !not_device)
402                 return;
403
404         old_op = ehandler_operation(_("reading directory block"));
405         retval = ext2fs_read_dir_block(ctx->fs, inode->i_block[0], buf);
406         ehandler_operation(0);
407         if (retval)
408                 return;
409
410         dirent = (struct ext2_dir_entry *) buf;
411         if (((dirent->name_len & 0xFF) != 1) ||
412             (dirent->name[0] != '.') ||
413             (dirent->inode != pctx->ino) ||
414             (dirent->rec_len < 12) ||
415             (dirent->rec_len % 4) ||
416             (dirent->rec_len >= ctx->fs->blocksize - 12))
417                 return;
418
419         dirent = (struct ext2_dir_entry *) (buf + dirent->rec_len);
420         if (((dirent->name_len & 0xFF) != 2) ||
421             (dirent->name[0] != '.') ||
422             (dirent->name[1] != '.') ||
423             (dirent->rec_len < 12) ||
424             (dirent->rec_len % 4))
425                 return;
426
427         if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
428                 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
429                 e2fsck_write_inode_full(ctx, pctx->ino, inode, 
430                                         EXT2_INODE_SIZE(ctx->fs->super), 
431                                         "check_is_really_dir");
432         }
433 }
434
435 extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags, 
436                                     ext2_icount_t *ret)
437 {
438         unsigned int            threshold;
439         ext2_ino_t              num_dirs;
440         errcode_t               retval;
441         char                    *tdb_dir;
442         int                     enable;
443
444         *ret = 0;
445
446         profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
447                            &tdb_dir);
448         profile_get_uint(ctx->profile, "scratch_files",
449                          "numdirs_threshold", 0, 0, &threshold);
450         profile_get_boolean(ctx->profile, "scratch_files",
451                             "icount", 0, 1, &enable);
452
453         retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
454         if (retval)
455                 num_dirs = 1024;        /* Guess */
456
457         if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
458             (threshold && num_dirs <= threshold))
459                 return;
460
461         retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
462         if (retval)
463                 *ret = 0;
464 }
465
466 void e2fsck_pass1(e2fsck_t ctx)
467 {
468         int     i;
469         __u64   max_sizes;
470         ext2_filsys fs = ctx->fs;
471         ext2_ino_t      ino;
472         struct ext2_inode *inode;
473         ext2_inode_scan scan;
474         char            *block_buf;
475 #ifdef RESOURCE_TRACK
476         struct resource_track   rtrack;
477 #endif
478         unsigned char   frag, fsize;
479         struct          problem_context pctx;
480         struct          scan_callback_struct scan_struct;
481         struct ext2_super_block *sb = ctx->fs->super;
482         const char      *old_op;
483         int             imagic_fs;
484         int             busted_fs_time = 0;
485         int             inode_size;
486         
487 #ifdef RESOURCE_TRACK
488         init_resource_track(&rtrack, ctx->fs->io);
489 #endif
490         clear_problem_context(&pctx);
491
492         if (!(ctx->options & E2F_OPT_PREEN))
493                 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
494
495         if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
496             !(ctx->options & E2F_OPT_NO)) {
497                 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
498                         ctx->dirs_to_hash = 0;
499         }
500
501 #ifdef MTRACE
502         mtrace_print("Pass 1");
503 #endif
504
505 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
506
507         for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
508                 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
509                 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
510                 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
511                 max_sizes = (max_sizes * (1UL << i)) - 1;
512                 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
513         }
514 #undef EXT2_BPP
515
516         imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
517
518         /*
519          * Allocate bitmaps structures
520          */
521         pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
522                                               &ctx->inode_used_map);
523         if (pctx.errcode) {
524                 pctx.num = 1;
525                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
526                 ctx->flags |= E2F_FLAG_ABORT;
527                 return;
528         }
529         pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
530                                 _("directory inode map"), &ctx->inode_dir_map);
531         if (pctx.errcode) {
532                 pctx.num = 2;
533                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
534                 ctx->flags |= E2F_FLAG_ABORT;
535                 return;
536         }
537         pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
538                         _("regular file inode map"), &ctx->inode_reg_map);
539         if (pctx.errcode) {
540                 pctx.num = 6;
541                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
542                 ctx->flags |= E2F_FLAG_ABORT;
543                 return;
544         }
545         pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
546                                               &ctx->block_found_map);
547         if (pctx.errcode) {
548                 pctx.num = 1;
549                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
550                 ctx->flags |= E2F_FLAG_ABORT;
551                 return;
552         }
553         e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
554         if (!ctx->inode_link_info)
555                 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
556                                                      &ctx->inode_link_info);
557         if (pctx.errcode) {
558                 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
559                 ctx->flags |= E2F_FLAG_ABORT;
560                 return;
561         }
562         inode_size = EXT2_INODE_SIZE(fs->super);
563         inode = (struct ext2_inode *)
564                 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
565
566         inodes_to_process = (struct process_inode_block *)
567                 e2fsck_allocate_memory(ctx,
568                                        (ctx->process_inode_size *
569                                         sizeof(struct process_inode_block)),
570                                        "array of inodes to process");
571         process_inode_count = 0;
572
573         pctx.errcode = ext2fs_init_dblist(fs, 0);
574         if (pctx.errcode) {
575                 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
576                 ctx->flags |= E2F_FLAG_ABORT;
577                 ext2fs_free_mem(&inode);
578                 return;
579         }
580
581         /*
582          * If the last orphan field is set, clear it, since the pass1
583          * processing will automatically find and clear the orphans.
584          * In the future, we may want to try using the last_orphan
585          * linked list ourselves, but for now, we clear it so that the
586          * ext3 mount code won't get confused.
587          */
588         if (!(ctx->options & E2F_OPT_READONLY)) {
589                 if (fs->super->s_last_orphan) {
590                         fs->super->s_last_orphan = 0;
591                         ext2fs_mark_super_dirty(fs);
592                 }
593         }
594
595         mark_table_blocks(ctx);
596         block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
597                                                     "block interate buffer");
598         e2fsck_use_inode_shortcuts(ctx, 1);
599         old_op = ehandler_operation(_("opening inode scan"));
600         pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks, 
601                                               &scan);
602         ehandler_operation(old_op);
603         if (pctx.errcode) {
604                 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
605                 ctx->flags |= E2F_FLAG_ABORT;
606                 ext2fs_free_mem(&block_buf);
607                 ext2fs_free_mem(&inode);
608                 return;
609         }
610         ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
611         ctx->stashed_inode = inode;
612         scan_struct.ctx = ctx;
613         scan_struct.block_buf = block_buf;
614         ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
615         if (ctx->progress)
616                 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
617                         return;
618         if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
619             (fs->super->s_mtime < fs->super->s_inodes_count))
620                 busted_fs_time = 1;
621
622         while (1) {
623                 old_op = ehandler_operation(_("getting next inode from scan"));
624                 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino, 
625                                                           inode, inode_size);
626                 ehandler_operation(old_op);
627                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
628                         return;
629                 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
630                         if (!ctx->inode_bb_map)
631                                 alloc_bb_map(ctx);
632                         ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
633                         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
634                         continue;
635                 }
636                 if (pctx.errcode) {
637                         fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
638                         ctx->flags |= E2F_FLAG_ABORT;
639                         return;
640                 }
641                 if (!ino)
642                         break;
643                 pctx.ino = ino;
644                 pctx.inode = inode;
645                 ctx->stashed_ino = ino;
646                 if (inode->i_links_count) {
647                         pctx.errcode = ext2fs_icount_store(ctx->inode_link_info, 
648                                            ino, inode->i_links_count);
649                         if (pctx.errcode) {
650                                 pctx.num = inode->i_links_count;
651                                 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
652                                 ctx->flags |= E2F_FLAG_ABORT;
653                                 return;
654                         }
655                 }
656                 if (ino == EXT2_BAD_INO) {
657                         struct process_block_struct pb;
658                         
659                         pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
660                                                           &pb.fs_meta_blocks);
661                         if (pctx.errcode) {
662                                 pctx.num = 4;
663                                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
664                                 ctx->flags |= E2F_FLAG_ABORT;
665                                 return;
666                         }
667                         pb.ino = EXT2_BAD_INO;
668                         pb.num_blocks = pb.last_block = 0;
669                         pb.num_illegal_blocks = 0;
670                         pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
671                         pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
672                         pb.inode = inode;
673                         pb.pctx = &pctx;
674                         pb.ctx = ctx;
675                         pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, 
676                                      block_buf, process_bad_block, &pb);
677                         ext2fs_free_block_bitmap(pb.fs_meta_blocks);
678                         if (pctx.errcode) {
679                                 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
680                                 ctx->flags |= E2F_FLAG_ABORT;
681                                 return;
682                         }
683                         if (pb.bbcheck)
684                                 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
685                                 ctx->flags |= E2F_FLAG_ABORT;
686                                 return;
687                         }
688                         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
689                         clear_problem_context(&pctx);
690                         continue;
691                 } else if (ino == EXT2_ROOT_INO) {
692                         /*
693                          * Make sure the root inode is a directory; if
694                          * not, offer to clear it.  It will be
695                          * regnerated in pass #3.
696                          */
697                         if (!LINUX_S_ISDIR(inode->i_mode)) {
698                                 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) {
699                                         inode->i_dtime = ctx->now;
700                                         inode->i_links_count = 0;
701                                         ext2fs_icount_store(ctx->inode_link_info,
702                                                             ino, 0);
703                                         e2fsck_write_inode(ctx, ino, inode,
704                                                            "pass1");
705                                 }
706
707                         }
708                         /*
709                          * If dtime is set, offer to clear it.  mke2fs
710                          * version 0.2b created filesystems with the
711                          * dtime field set for the root and lost+found
712                          * directories.  We won't worry about
713                          * /lost+found, since that can be regenerated
714                          * easily.  But we will fix the root directory
715                          * as a special case.
716                          */
717                         if (inode->i_dtime && inode->i_links_count) {
718                                 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
719                                         inode->i_dtime = 0;
720                                         e2fsck_write_inode(ctx, ino, inode,
721                                                            "pass1");
722                                 }
723                         }
724                 } else if (ino == EXT2_JOURNAL_INO) {
725                         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
726                         if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
727                                 if (!LINUX_S_ISREG(inode->i_mode) &&
728                                     fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
729                                                 &pctx)) {
730                                         inode->i_mode = LINUX_S_IFREG;
731                                         e2fsck_write_inode(ctx, ino, inode,
732                                                            "pass1");
733                                 }
734                                 check_blocks(ctx, &pctx, block_buf);
735                                 continue;
736                         }
737                         if ((inode->i_links_count || inode->i_blocks ||
738                              inode->i_blocks || inode->i_block[0]) &&
739                             fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR, 
740                                         &pctx)) {
741                                 memset(inode, 0, inode_size);
742                                 ext2fs_icount_store(ctx->inode_link_info,
743                                                     ino, 0);
744                                 e2fsck_write_inode_full(ctx, ino, inode, 
745                                                         inode_size, "pass1");
746                         }
747                 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
748                         int     problem = 0;
749                         
750                         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
751                         if (ino == EXT2_BOOT_LOADER_INO) {
752                                 if (LINUX_S_ISDIR(inode->i_mode))
753                                         problem = PR_1_RESERVED_BAD_MODE;
754                         } else if (ino == EXT2_RESIZE_INO) {
755                                 if (inode->i_mode &&
756                                     !LINUX_S_ISREG(inode->i_mode))
757                                         problem = PR_1_RESERVED_BAD_MODE;
758                         } else {
759                                 if (inode->i_mode != 0)
760                                         problem = PR_1_RESERVED_BAD_MODE;
761                         }
762                         if (problem) {
763                                 if (fix_problem(ctx, problem, &pctx)) {
764                                         inode->i_mode = 0;
765                                         e2fsck_write_inode(ctx, ino, inode,
766                                                            "pass1");
767                                 }
768                         }
769                         check_blocks(ctx, &pctx, block_buf);
770                         continue;
771                 }
772                 /*
773                  * Check for inodes who might have been part of the
774                  * orphaned list linked list.  They should have gotten
775                  * dealt with by now, unless the list had somehow been
776                  * corrupted.
777                  * 
778                  * FIXME: In the future, inodes which are still in use
779                  * (and which are therefore) pending truncation should
780                  * be handled specially.  Right now we just clear the
781                  * dtime field, and the normal e2fsck handling of
782                  * inodes where i_size and the inode blocks are
783                  * inconsistent is to fix i_size, instead of releasing
784                  * the extra blocks.  This won't catch the inodes that
785                  * was at the end of the orphan list, but it's better
786                  * than nothing.  The right answer is that there
787                  * shouldn't be any bugs in the orphan list handling.  :-)
788                  */
789                 if (inode->i_dtime && !busted_fs_time &&
790                     inode->i_dtime < ctx->fs->super->s_inodes_count) {
791                         if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
792                                 inode->i_dtime = inode->i_links_count ?
793                                         0 : ctx->now;
794                                 e2fsck_write_inode(ctx, ino, inode,
795                                                    "pass1");
796                         }
797                 }
798                 
799                 /*
800                  * This code assumes that deleted inodes have
801                  * i_links_count set to 0.  
802                  */
803                 if (!inode->i_links_count) {
804                         if (!inode->i_dtime && inode->i_mode) {
805                                 if (fix_problem(ctx,
806                                             PR_1_ZERO_DTIME, &pctx)) {
807                                         inode->i_dtime = ctx->now;
808                                         e2fsck_write_inode(ctx, ino, inode,
809                                                            "pass1");
810                                 }
811                         }
812                         continue;
813                 }
814                 /*
815                  * n.b.  0.3c ext2fs code didn't clear i_links_count for
816                  * deleted files.  Oops.
817                  *
818                  * Since all new ext2 implementations get this right,
819                  * we now assume that the case of non-zero
820                  * i_links_count and non-zero dtime means that we
821                  * should keep the file, not delete it.
822                  * 
823                  */
824                 if (inode->i_dtime) {
825                         if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
826                                 inode->i_dtime = 0;
827                                 e2fsck_write_inode(ctx, ino, inode, "pass1");
828                         }
829                 }
830                 
831                 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
832                 switch (fs->super->s_creator_os) {
833                     case EXT2_OS_HURD:
834                         frag = inode->osd2.hurd2.h_i_frag;
835                         fsize = inode->osd2.hurd2.h_i_fsize;
836                         break;
837                     default:
838                         frag = fsize = 0;
839                 }
840                 
841                 if (inode->i_faddr || frag || fsize ||
842                     (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
843                         mark_inode_bad(ctx, ino);
844                 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
845                     !(fs->super->s_feature_ro_compat & 
846                       EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
847                     (inode->osd2.linux2.l_i_blocks_hi != 0))
848                         mark_inode_bad(ctx, ino);
849                 if (inode->i_flags & EXT2_IMAGIC_FL) {
850                         if (imagic_fs) {
851                                 if (!ctx->inode_imagic_map)
852                                         alloc_imagic_map(ctx);
853                                 ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
854                                                          ino);
855                         } else {
856                                 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
857                                         inode->i_flags &= ~EXT2_IMAGIC_FL;
858                                         e2fsck_write_inode(ctx, ino,
859                                                            inode, "pass1");
860                                 }
861                         }
862                 }
863
864                 check_inode_extra_space(ctx, &pctx);
865                 check_is_really_dir(ctx, &pctx, block_buf);
866
867                 if (LINUX_S_ISDIR(inode->i_mode)) {
868                         ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
869                         e2fsck_add_dir_info(ctx, ino, 0);
870                         ctx->fs_directory_count++;
871                 } else if (LINUX_S_ISREG (inode->i_mode)) {
872                         ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
873                         ctx->fs_regular_count++;
874                 } else if (LINUX_S_ISCHR (inode->i_mode) &&
875                            e2fsck_pass1_check_device_inode(fs, inode)) {
876                         check_immutable(ctx, &pctx);
877                         check_size(ctx, &pctx);
878                         ctx->fs_chardev_count++;
879                 } else if (LINUX_S_ISBLK (inode->i_mode) &&
880                            e2fsck_pass1_check_device_inode(fs, inode)) {
881                         check_immutable(ctx, &pctx);
882                         check_size(ctx, &pctx);
883                         ctx->fs_blockdev_count++;
884                 } else if (LINUX_S_ISLNK (inode->i_mode) &&
885                            e2fsck_pass1_check_symlink(fs, inode, block_buf)) {
886                         check_immutable(ctx, &pctx);
887                         ctx->fs_symlinks_count++;
888                         if (ext2fs_inode_data_blocks(fs, inode) == 0) {
889                                 ctx->fs_fast_symlinks_count++;
890                                 check_blocks(ctx, &pctx, block_buf);
891                                 continue;
892                         }
893                 }
894                 else if (LINUX_S_ISFIFO (inode->i_mode) &&
895                          e2fsck_pass1_check_device_inode(fs, inode)) {
896                         check_immutable(ctx, &pctx);
897                         check_size(ctx, &pctx);
898                         ctx->fs_fifo_count++;
899                 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
900                            e2fsck_pass1_check_device_inode(fs, inode)) {
901                         check_immutable(ctx, &pctx);
902                         check_size(ctx, &pctx);
903                         ctx->fs_sockets_count++;
904                 } else
905                         mark_inode_bad(ctx, ino);
906                 if (inode->i_block[EXT2_IND_BLOCK])
907                         ctx->fs_ind_count++;
908                 if (inode->i_block[EXT2_DIND_BLOCK])
909                         ctx->fs_dind_count++;
910                 if (inode->i_block[EXT2_TIND_BLOCK])
911                         ctx->fs_tind_count++;
912                 if (inode->i_block[EXT2_IND_BLOCK] ||
913                     inode->i_block[EXT2_DIND_BLOCK] ||
914                     inode->i_block[EXT2_TIND_BLOCK] ||
915                     inode->i_file_acl) {
916                         inodes_to_process[process_inode_count].ino = ino;
917                         inodes_to_process[process_inode_count].inode = *inode;
918                         process_inode_count++;
919                 } else
920                         check_blocks(ctx, &pctx, block_buf);
921
922                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
923                         return;
924
925                 if (process_inode_count >= ctx->process_inode_size) {
926                         process_inodes(ctx, block_buf);
927
928                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
929                                 return;
930                 }
931         }
932         process_inodes(ctx, block_buf);
933         ext2fs_close_inode_scan(scan);
934
935         /*
936          * If any extended attribute blocks' reference counts need to
937          * be adjusted, either up (ctx->refcount_extra), or down
938          * (ctx->refcount), then fix them.
939          */
940         if (ctx->refcount) {
941                 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
942                 ea_refcount_free(ctx->refcount);
943                 ctx->refcount = 0;
944         }
945         if (ctx->refcount_extra) {
946                 adjust_extattr_refcount(ctx, ctx->refcount_extra,
947                                         block_buf, +1);
948                 ea_refcount_free(ctx->refcount_extra);
949                 ctx->refcount_extra = 0;
950         }
951                 
952         if (ctx->invalid_bitmaps)
953                 handle_fs_bad_blocks(ctx);
954
955         /* We don't need the block_ea_map any more */
956         if (ctx->block_ea_map) {
957                 ext2fs_free_block_bitmap(ctx->block_ea_map);
958                 ctx->block_ea_map = 0;
959         }
960
961         if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
962                 ext2fs_block_bitmap save_bmap;
963
964                 save_bmap = fs->block_map;
965                 fs->block_map = ctx->block_found_map;
966                 clear_problem_context(&pctx);
967                 pctx.errcode = ext2fs_create_resize_inode(fs);
968                 if (pctx.errcode) {
969                         fix_problem(ctx, PR_1_RESIZE_INODE_CREATE, &pctx);
970                         /* Should never get here */
971                         ctx->flags |= E2F_FLAG_ABORT;
972                         return;
973                 }
974                 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
975                                   "recreate inode");
976                 inode->i_mtime = ctx->now;
977                 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode, 
978                                    "recreate inode");
979                 fs->block_map = save_bmap;
980                 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
981         }
982                        
983         if (ctx->flags & E2F_FLAG_RESTART) {
984                 /*
985                  * Only the master copy of the superblock and block
986                  * group descriptors are going to be written during a
987                  * restart, so set the superblock to be used to be the
988                  * master superblock.
989                  */
990                 ctx->use_superblock = 0;
991                 unwind_pass1(fs);
992                 goto endit;
993         }
994
995         if (ctx->block_dup_map) {
996                 if (ctx->options & E2F_OPT_PREEN) {
997                         clear_problem_context(&pctx);
998                         fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
999                 }
1000                 e2fsck_pass1_dupblocks(ctx, block_buf);
1001         }
1002         ext2fs_free_mem(&inodes_to_process);
1003 endit:
1004         e2fsck_use_inode_shortcuts(ctx, 0);
1005         
1006         ext2fs_free_mem(&block_buf);
1007         ext2fs_free_mem(&inode);
1008
1009 #ifdef RESOURCE_TRACK
1010         if (ctx->options & E2F_OPT_TIME2) {
1011                 e2fsck_clear_progbar(ctx);
1012                 print_resource_track(_("Pass 1"), &rtrack, ctx->fs->io);
1013         }
1014 #endif
1015 }
1016
1017 /*
1018  * When the inode_scan routines call this callback at the end of the
1019  * glock group, call process_inodes.
1020  */
1021 static errcode_t scan_callback(ext2_filsys fs, 
1022                                ext2_inode_scan scan EXT2FS_ATTR((unused)),
1023                                dgrp_t group, void * priv_data)
1024 {
1025         struct scan_callback_struct *scan_struct;
1026         e2fsck_t ctx;
1027
1028         scan_struct = (struct scan_callback_struct *) priv_data;
1029         ctx = scan_struct->ctx;
1030         
1031         process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1032
1033         if (ctx->progress)
1034                 if ((ctx->progress)(ctx, 1, group+1,
1035                                     ctx->fs->group_desc_count))
1036                         return EXT2_ET_CANCEL_REQUESTED;
1037
1038         return 0;
1039 }
1040
1041 /*
1042  * Process the inodes in the "inodes to process" list.
1043  */
1044 static void process_inodes(e2fsck_t ctx, char *block_buf)
1045 {
1046         int                     i;
1047         struct ext2_inode       *old_stashed_inode;
1048         ext2_ino_t              old_stashed_ino;
1049         const char              *old_operation;
1050         char                    buf[80];
1051         struct problem_context  pctx;
1052         
1053 #if 0
1054         printf("begin process_inodes: ");
1055 #endif
1056         if (process_inode_count == 0)
1057                 return;
1058         old_operation = ehandler_operation(0);
1059         old_stashed_inode = ctx->stashed_inode;
1060         old_stashed_ino = ctx->stashed_ino;
1061         qsort(inodes_to_process, process_inode_count,
1062                       sizeof(struct process_inode_block), process_inode_cmp);
1063         clear_problem_context(&pctx);
1064         for (i=0; i < process_inode_count; i++) {
1065                 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1066                 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1067                 
1068 #if 0
1069                 printf("%u ", pctx.ino);
1070 #endif
1071                 sprintf(buf, _("reading indirect blocks of inode %u"),
1072                         pctx.ino);
1073                 ehandler_operation(buf);
1074                 check_blocks(ctx, &pctx, block_buf);
1075                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1076                         break;
1077         }
1078         ctx->stashed_inode = old_stashed_inode;
1079         ctx->stashed_ino = old_stashed_ino;
1080         process_inode_count = 0;
1081 #if 0
1082         printf("end process inodes\n");
1083 #endif
1084         ehandler_operation(old_operation);
1085 }
1086
1087 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1088 {
1089         const struct process_inode_block *ib_a =
1090                 (const struct process_inode_block *) a;
1091         const struct process_inode_block *ib_b =
1092                 (const struct process_inode_block *) b;
1093         int     ret;
1094         
1095         ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1096                ib_b->inode.i_block[EXT2_IND_BLOCK]);
1097         if (ret == 0)
1098                 ret = ib_a->inode.i_file_acl - ib_b->inode.i_file_acl;
1099         return ret;
1100 }
1101
1102 /*
1103  * Mark an inode as being bad in some what
1104  */
1105 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
1106 {
1107         struct          problem_context pctx;
1108
1109         if (!ctx->inode_bad_map) {
1110                 clear_problem_context(&pctx);
1111         
1112                 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1113                             _("bad inode map"), &ctx->inode_bad_map);
1114                 if (pctx.errcode) {
1115                         pctx.num = 3;
1116                         fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1117                         /* Should never get here */
1118                         ctx->flags |= E2F_FLAG_ABORT;
1119                         return;
1120                 }
1121         }
1122         ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
1123 }
1124
1125
1126 /*
1127  * This procedure will allocate the inode "bb" (badblock) map table
1128  */
1129 static void alloc_bb_map(e2fsck_t ctx)
1130 {
1131         struct          problem_context pctx;
1132         
1133         clear_problem_context(&pctx);
1134         pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1135                                               _("inode in bad block map"),
1136                                               &ctx->inode_bb_map);
1137         if (pctx.errcode) {
1138                 pctx.num = 4;
1139                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1140                 /* Should never get here */
1141                 ctx->flags |= E2F_FLAG_ABORT;
1142                 return;
1143         }
1144 }
1145
1146 /*
1147  * This procedure will allocate the inode imagic table
1148  */
1149 static void alloc_imagic_map(e2fsck_t ctx)
1150 {
1151         struct          problem_context pctx;
1152         
1153         clear_problem_context(&pctx);
1154         pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1155                                               _("imagic inode map"),
1156                                               &ctx->inode_imagic_map);
1157         if (pctx.errcode) {
1158                 pctx.num = 5;
1159                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1160                 /* Should never get here */
1161                 ctx->flags |= E2F_FLAG_ABORT;
1162                 return;
1163         }
1164 }
1165
1166 /*
1167  * Marks a block as in use, setting the dup_map if it's been set
1168  * already.  Called by process_block and process_bad_block.
1169  *
1170  * WARNING: Assumes checks have already been done to make sure block
1171  * is valid.  This is true in both process_block and process_bad_block.
1172  */
1173 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
1174 {
1175         struct          problem_context pctx;
1176         
1177         clear_problem_context(&pctx);
1178         
1179         if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
1180                 if (!ctx->block_dup_map) {
1181                         pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
1182                               _("multiply claimed block map"),
1183                               &ctx->block_dup_map);
1184                         if (pctx.errcode) {
1185                                 pctx.num = 3;
1186                                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, 
1187                                             &pctx);
1188                                 /* Should never get here */
1189                                 ctx->flags |= E2F_FLAG_ABORT;
1190                                 return;
1191                         }
1192                 }
1193                 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
1194         } else {
1195                 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
1196         }
1197 }
1198
1199 /*
1200  * Adjust the extended attribute block's reference counts at the end
1201  * of pass 1, either by subtracting out references for EA blocks that
1202  * are still referenced in ctx->refcount, or by adding references for
1203  * EA blocks that had extra references as accounted for in
1204  * ctx->refcount_extra.
1205  */
1206 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount, 
1207                                     char *block_buf, int adjust_sign)
1208 {
1209         struct ext2_ext_attr_header     *header;
1210         struct problem_context          pctx;
1211         ext2_filsys                     fs = ctx->fs;
1212         blk_t                           blk;
1213         __u32                           should_be;
1214         int                             count;
1215
1216         clear_problem_context(&pctx);
1217         
1218         ea_refcount_intr_begin(refcount);
1219         while (1) {
1220                 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1221                         break;
1222                 pctx.blk = blk;
1223                 pctx.errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
1224                 if (pctx.errcode) {
1225                         fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1226                         return;
1227                 }
1228                 header = (struct ext2_ext_attr_header *) block_buf;
1229                 pctx.blkcount = header->h_refcount;
1230                 should_be = header->h_refcount + adjust_sign * count;
1231                 pctx.num = should_be;
1232                 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1233                         header->h_refcount = should_be;
1234                         pctx.errcode = ext2fs_write_ext_attr(fs, blk,
1235                                                              block_buf);
1236                         if (pctx.errcode) {
1237                                 fix_problem(ctx, PR_1_EXTATTR_WRITE, &pctx);
1238                                 continue;
1239                         }
1240                 }
1241         }
1242 }
1243
1244 /*
1245  * Handle processing the extended attribute blocks
1246  */
1247 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1248                            char *block_buf)
1249 {
1250         ext2_filsys fs = ctx->fs;
1251         ext2_ino_t      ino = pctx->ino;
1252         struct ext2_inode *inode = pctx->inode;
1253         blk_t           blk;
1254         char *          end;
1255         struct ext2_ext_attr_header *header;
1256         struct ext2_ext_attr_entry *entry;
1257         int             count;
1258         region_t        region = 0;
1259
1260         blk = inode->i_file_acl;
1261         if (blk == 0)
1262                 return 0;
1263
1264         /*
1265          * If the Extended attribute flag isn't set, then a non-zero
1266          * file acl means that the inode is corrupted.
1267          *
1268          * Or if the extended attribute block is an invalid block,
1269          * then the inode is also corrupted.
1270          */
1271         if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1272             (blk < fs->super->s_first_data_block) ||
1273             (blk >= fs->super->s_blocks_count)) {
1274                 mark_inode_bad(ctx, ino);
1275                 return 0;
1276         }
1277
1278         /* If ea bitmap hasn't been allocated, create it */
1279         if (!ctx->block_ea_map) {
1280                 pctx->errcode = ext2fs_allocate_block_bitmap(fs,
1281                                                       _("ext attr block map"),
1282                                                       &ctx->block_ea_map);
1283                 if (pctx->errcode) {
1284                         pctx->num = 2;
1285                         fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1286                         ctx->flags |= E2F_FLAG_ABORT;
1287                         return 0;
1288                 }
1289         }
1290
1291         /* Create the EA refcount structure if necessary */
1292         if (!ctx->refcount) {
1293                 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1294                 if (pctx->errcode) {
1295                         pctx->num = 1;
1296                         fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1297                         ctx->flags |= E2F_FLAG_ABORT;
1298                         return 0;
1299                 }
1300         }
1301
1302 #if 0
1303         /* Debugging text */
1304         printf("Inode %u has EA block %u\n", ino, blk);
1305 #endif
1306
1307         /* Have we seen this EA block before? */
1308         if (ext2fs_fast_test_block_bitmap(ctx->block_ea_map, blk)) {
1309                 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1310                         return 1;
1311                 /* Ooops, this EA was referenced more than it stated */
1312                 if (!ctx->refcount_extra) {
1313                         pctx->errcode = ea_refcount_create(0,
1314                                            &ctx->refcount_extra);
1315                         if (pctx->errcode) {
1316                                 pctx->num = 2;
1317                                 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1318                                 ctx->flags |= E2F_FLAG_ABORT;
1319                                 return 0;
1320                         }
1321                 }
1322                 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1323                 return 1;
1324         }
1325
1326         /*
1327          * OK, we haven't seen this EA block yet.  So we need to
1328          * validate it
1329          */
1330         pctx->blk = blk;
1331         pctx->errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
1332         if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1333                 goto clear_extattr;
1334         header = (struct ext2_ext_attr_header *) block_buf;
1335         pctx->blk = inode->i_file_acl;
1336         if (((ctx->ext_attr_ver == 1) &&
1337              (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
1338             ((ctx->ext_attr_ver == 2) &&
1339              (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
1340                 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1341                         goto clear_extattr;
1342         }
1343
1344         if (header->h_blocks != 1) {
1345                 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1346                         goto clear_extattr;
1347         }
1348
1349         region = region_create(0, fs->blocksize);
1350         if (!region) {
1351                 fix_problem(ctx, PR_1_EA_ALLOC_REGION, pctx);
1352                 ctx->flags |= E2F_FLAG_ABORT;
1353                 return 0;
1354         }
1355         if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1356                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1357                         goto clear_extattr;
1358         }
1359
1360         entry = (struct ext2_ext_attr_entry *)(header+1);
1361         end = block_buf + fs->blocksize;
1362         while ((char *)entry < end && *(__u32 *)entry) {
1363                 if (region_allocate(region, (char *)entry - (char *)header,
1364                                    EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1365                         if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1366                                 goto clear_extattr;
1367                 }
1368                 if ((ctx->ext_attr_ver == 1 &&
1369                      (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1370                     (ctx->ext_attr_ver == 2 &&
1371                      entry->e_name_index == 0)) {
1372                         if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
1373                                 goto clear_extattr;
1374                 }
1375                 if (entry->e_value_block != 0) {
1376                         if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1377                                 goto clear_extattr;
1378                 }
1379                 if (entry->e_value_offs + entry->e_value_size > fs->blocksize) {
1380                         if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1381                                 goto clear_extattr;
1382                         break;
1383                 }
1384                 if (entry->e_value_size &&
1385                     region_allocate(region, entry->e_value_offs,
1386                                     EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
1387                         if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1388                                 goto clear_extattr;
1389                 }
1390                 entry = EXT2_EXT_ATTR_NEXT(entry);
1391         }
1392         if (region_allocate(region, (char *)entry - (char *)header, 4)) {
1393                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1394                         goto clear_extattr;
1395         }
1396         region_free(region);
1397
1398         count = header->h_refcount - 1;
1399         if (count)
1400                 ea_refcount_store(ctx->refcount, blk, count);
1401         mark_block_used(ctx, blk);
1402         ext2fs_fast_mark_block_bitmap(ctx->block_ea_map, blk);
1403         return 1;
1404
1405 clear_extattr:
1406         if (region)
1407                 region_free(region);
1408         inode->i_file_acl = 0;
1409         e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
1410         return 0;
1411 }
1412
1413 /* Returns 1 if bad htree, 0 if OK */
1414 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
1415                         ext2_ino_t ino EXT2FS_ATTR((unused)),
1416                         struct ext2_inode *inode,
1417                         char *block_buf)
1418 {
1419         struct ext2_dx_root_info        *root;
1420         ext2_filsys                     fs = ctx->fs;
1421         errcode_t                       retval;
1422         blk_t                           blk;
1423
1424         if ((!LINUX_S_ISDIR(inode->i_mode) &&
1425              fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
1426             (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
1427              fix_problem(ctx, PR_1_HTREE_SET, pctx)))
1428                 return 1;
1429
1430         blk = inode->i_block[0];
1431         if (((blk == 0) ||
1432              (blk < fs->super->s_first_data_block) ||
1433              (blk >= fs->super->s_blocks_count)) &&
1434             fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1435                 return 1;
1436
1437         retval = io_channel_read_blk(fs->io, blk, 1, block_buf);
1438         if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1439                 return 1;
1440         
1441         /* XXX should check that beginning matches a directory */
1442         root = (struct ext2_dx_root_info *) (block_buf + 24);
1443
1444         if ((root->reserved_zero || root->info_length < 8) &&
1445             fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1446                 return 1;
1447
1448         pctx->num = root->hash_version;
1449         if ((root->hash_version != EXT2_HASH_LEGACY) &&
1450             (root->hash_version != EXT2_HASH_HALF_MD4) &&
1451             (root->hash_version != EXT2_HASH_TEA) &&
1452             fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
1453                 return 1;
1454                 
1455         if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
1456             fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
1457                 return 1;
1458
1459         pctx->num = root->indirect_levels;
1460         if ((root->indirect_levels > 1) &&
1461             fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
1462                 return 1;
1463         
1464         return 0;
1465 }
1466
1467 /*
1468  * This subroutine is called on each inode to account for all of the
1469  * blocks used by that inode.
1470  */
1471 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
1472                          char *block_buf)
1473 {
1474         ext2_filsys fs = ctx->fs;
1475         struct process_block_struct pb;
1476         ext2_ino_t      ino = pctx->ino;
1477         struct ext2_inode *inode = pctx->inode;
1478         int             bad_size = 0;
1479         int             dirty_inode = 0;
1480         __u64           size;
1481         
1482         pb.ino = ino;
1483         pb.num_blocks = 0;
1484         pb.last_block = -1;
1485         pb.num_illegal_blocks = 0;
1486         pb.suppress = 0; pb.clear = 0;
1487         pb.fragmented = 0;
1488         pb.compressed = 0;
1489         pb.previous_block = 0;
1490         pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1491         pb.is_reg = LINUX_S_ISREG(inode->i_mode);
1492         pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
1493         pb.inode = inode;
1494         pb.pctx = pctx;
1495         pb.ctx = ctx;
1496         pctx->ino = ino;
1497         pctx->errcode = 0;
1498
1499         if (inode->i_flags & EXT2_COMPRBLK_FL) {
1500                 if (fs->super->s_feature_incompat &
1501                     EXT2_FEATURE_INCOMPAT_COMPRESSION)
1502                         pb.compressed = 1;
1503                 else {
1504                         if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
1505                                 inode->i_flags &= ~EXT2_COMPRBLK_FL;
1506                                 dirty_inode++;
1507                         }
1508                 }
1509         }
1510
1511         if (inode->i_file_acl && check_ext_attr(ctx, pctx, block_buf))
1512                 pb.num_blocks++;
1513
1514         if (ext2fs_inode_has_valid_blocks(inode))
1515                 pctx->errcode = ext2fs_block_iterate2(fs, ino,
1516                                        pb.is_dir ? BLOCK_FLAG_HOLE : 0,
1517                                        block_buf, process_block, &pb);
1518         end_problem_latch(ctx, PR_LATCH_BLOCK);
1519         end_problem_latch(ctx, PR_LATCH_TOOBIG);
1520         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1521                 goto out;
1522         if (pctx->errcode)
1523                 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
1524
1525         if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
1526                 ctx->fs_fragmented++;
1527
1528         if (pb.clear) {
1529                 inode->i_links_count = 0;
1530                 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1531                 inode->i_dtime = ctx->now;
1532                 dirty_inode++;
1533                 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1534                 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1535                 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1536                 /*
1537                  * The inode was probably partially accounted for
1538                  * before processing was aborted, so we need to
1539                  * restart the pass 1 scan.
1540                  */
1541                 ctx->flags |= E2F_FLAG_RESTART;
1542                 goto out;
1543         }
1544         
1545         if (inode->i_flags & EXT2_INDEX_FL) {
1546                 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
1547                         inode->i_flags &= ~EXT2_INDEX_FL;
1548                         dirty_inode++;
1549                 } else {
1550 #ifdef ENABLE_HTREE
1551                         e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
1552 #endif
1553                 }
1554         }
1555         if (ctx->dirs_to_hash && pb.is_dir &&
1556             !(inode->i_flags & EXT2_INDEX_FL) &&
1557             ((inode->i_size / fs->blocksize) >= 3))
1558                 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1559                 
1560         if (!pb.num_blocks && pb.is_dir) {
1561                 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
1562                         inode->i_links_count = 0;
1563                         ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1564                         inode->i_dtime = ctx->now;
1565                         dirty_inode++;
1566                         ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1567                         ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1568                         ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1569                         ctx->fs_directory_count--;
1570                         goto out;
1571                 }
1572         }
1573
1574         pb.num_blocks *= (fs->blocksize / 512);
1575 #if 0
1576         printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
1577                ino, inode->i_size, pb.last_block, inode->i_blocks,
1578                pb.num_blocks);
1579 #endif
1580         if (pb.is_dir) {
1581                 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
1582                 if (nblock > (pb.last_block + 1))
1583                         bad_size = 1;
1584                 else if (nblock < (pb.last_block + 1)) {
1585                         if (((pb.last_block + 1) - nblock) >
1586                             fs->super->s_prealloc_dir_blocks)
1587                                 bad_size = 2;
1588                 }
1589         } else {
1590                 e2_blkcnt_t blkpg = ctx->blocks_per_page;
1591
1592                 size = EXT2_I_SIZE(inode);
1593                 if ((pb.last_block >= 0) &&
1594                     /* allow allocated blocks to end of PAGE_SIZE */
1595                     (size < (__u64)pb.last_block * fs->blocksize) &&
1596                     (pb.last_block / blkpg * blkpg != pb.last_block ||
1597                      size < (__u64)(pb.last_block & ~(blkpg-1)) *fs->blocksize))
1598                         bad_size = 3;
1599                 else if (size > ext2_max_sizes[fs->super->s_log_block_size])
1600                         bad_size = 4;
1601         }
1602         /* i_size for symlinks is checked elsewhere */
1603         if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
1604                 pctx->num = (pb.last_block+1) * fs->blocksize;
1605                 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
1606                         inode->i_size = pctx->num;
1607                         if (!LINUX_S_ISDIR(inode->i_mode))
1608                                 inode->i_size_high = pctx->num >> 32;
1609                         dirty_inode++;
1610                 }
1611                 pctx->num = 0;
1612         }
1613         if (LINUX_S_ISREG(inode->i_mode) &&
1614             (inode->i_size_high || inode->i_size & 0x80000000UL))
1615                 ctx->large_files++;
1616         if (pb.num_blocks != inode->i_blocks) {
1617                 pctx->num = pb.num_blocks;
1618                 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
1619                         inode->i_blocks = pb.num_blocks;
1620                         dirty_inode++;
1621                 }
1622                 pctx->num = 0;
1623         }
1624 out:
1625         if (dirty_inode)
1626                 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
1627 }
1628
1629 #if 0
1630 /*
1631  * Helper function called by process block when an illegal block is
1632  * found.  It returns a description about why the block is illegal
1633  */
1634 static char *describe_illegal_block(ext2_filsys fs, blk_t block)
1635 {
1636         blk_t   super;
1637         int     i;
1638         static char     problem[80];
1639
1640         super = fs->super->s_first_data_block;
1641         strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
1642         if (block < super) {
1643                 sprintf(problem, "< FIRSTBLOCK (%u)", super);
1644                 return(problem);
1645         } else if (block >= fs->super->s_blocks_count) {
1646                 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
1647                 return(problem);
1648         }
1649         for (i = 0; i < fs->group_desc_count; i++) {
1650                 if (block == super) {
1651                         sprintf(problem, "is the superblock in group %d", i);
1652                         break;
1653                 }
1654                 if (block > super &&
1655                     block <= (super + fs->desc_blocks)) {
1656                         sprintf(problem, "is in the group descriptors "
1657                                 "of group %d", i);
1658                         break;
1659                 }
1660                 if (block == fs->group_desc[i].bg_block_bitmap) {
1661                         sprintf(problem, "is the block bitmap of group %d", i);
1662                         break;
1663                 }
1664                 if (block == fs->group_desc[i].bg_inode_bitmap) {
1665                         sprintf(problem, "is the inode bitmap of group %d", i);
1666                         break;
1667                 }
1668                 if (block >= fs->group_desc[i].bg_inode_table &&
1669                     (block < fs->group_desc[i].bg_inode_table
1670                      + fs->inode_blocks_per_group)) {
1671                         sprintf(problem, "is in the inode table of group %d",
1672                                 i);
1673                         break;
1674                 }
1675                 super += fs->super->s_blocks_per_group;
1676         }
1677         return(problem);
1678 }
1679 #endif
1680
1681 /*
1682  * This is a helper function for check_blocks().
1683  */
1684 static int process_block(ext2_filsys fs,
1685                   blk_t *block_nr,
1686                   e2_blkcnt_t blockcnt,
1687                   blk_t ref_block EXT2FS_ATTR((unused)),
1688                   int ref_offset EXT2FS_ATTR((unused)),
1689                   void *priv_data)
1690 {
1691         struct process_block_struct *p;
1692         struct problem_context *pctx;
1693         blk_t   blk = *block_nr;
1694         int     ret_code = 0;
1695         int     problem = 0;
1696         e2fsck_t        ctx;
1697
1698         p = (struct process_block_struct *) priv_data;
1699         pctx = p->pctx;
1700         ctx = p->ctx;
1701
1702         if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
1703                 /* todo: Check that the comprblk_fl is high, that the
1704                    blkaddr pattern looks right (all non-holes up to
1705                    first EXT2FS_COMPRESSED_BLKADDR, then all
1706                    EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
1707                    that the feature_incompat bit is high, and that the
1708                    inode is a regular file.  If we're doing a "full
1709                    check" (a concept introduced to e2fsck by e2compr,
1710                    meaning that we look at data blocks as well as
1711                    metadata) then call some library routine that
1712                    checks the compressed data.  I'll have to think
1713                    about this, because one particularly important
1714                    problem to be able to fix is to recalculate the
1715                    cluster size if necessary.  I think that perhaps
1716                    we'd better do most/all e2compr-specific checks
1717                    separately, after the non-e2compr checks.  If not
1718                    doing a full check, it may be useful to test that
1719                    the personality is linux; e.g. if it isn't then
1720                    perhaps this really is just an illegal block. */
1721                 return 0;
1722         }
1723
1724         if (blk == 0) {
1725                 if (p->is_dir == 0) {
1726                         /*
1727                          * Should never happen, since only directories
1728                          * get called with BLOCK_FLAG_HOLE
1729                          */
1730 #if DEBUG_E2FSCK
1731                         printf("process_block() called with blk == 0, "
1732                                "blockcnt=%d, inode %lu???\n",
1733                                blockcnt, p->ino);
1734 #endif
1735                         return 0;
1736                 }
1737                 if (blockcnt < 0)
1738                         return 0;
1739                 if (blockcnt * fs->blocksize < p->inode->i_size) {
1740 #if 0
1741                         printf("Missing block (#%d) in directory inode %lu!\n",
1742                                blockcnt, p->ino);
1743 #endif
1744                         goto mark_dir;
1745                 }
1746                 return 0;
1747         }
1748
1749 #if 0
1750         printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
1751                blockcnt);
1752 #endif
1753         
1754         /*
1755          * Simplistic fragmentation check.  We merely require that the
1756          * file be contiguous.  (Which can never be true for really
1757          * big files that are greater than a block group.)
1758          */
1759         if (!HOLE_BLKADDR(p->previous_block)) {
1760                 if (p->previous_block+1 != blk)
1761                         p->fragmented = 1;
1762         }
1763         p->previous_block = blk;
1764
1765         if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
1766                 problem = PR_1_TOOBIG_DIR;
1767         if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
1768                 problem = PR_1_TOOBIG_REG;
1769         if (!p->is_dir && !p->is_reg && blockcnt > 0)
1770                 problem = PR_1_TOOBIG_SYMLINK;
1771             
1772         if (blk < fs->super->s_first_data_block ||
1773             blk >= fs->super->s_blocks_count)
1774                 problem = PR_1_ILLEGAL_BLOCK_NUM;
1775
1776         if (problem) {
1777                 p->num_illegal_blocks++;
1778                 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
1779                         if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
1780                                 p->clear = 1;
1781                                 return BLOCK_ABORT;
1782                         }
1783                         if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
1784                                 p->suppress = 1;
1785                                 set_latch_flags(PR_LATCH_BLOCK,
1786                                                 PRL_SUPPRESS, 0);
1787                         }
1788                 }
1789                 pctx->blk = blk;
1790                 pctx->blkcount = blockcnt;
1791                 if (fix_problem(ctx, problem, pctx)) {
1792                         blk = *block_nr = 0;
1793                         ret_code = BLOCK_CHANGED;
1794                         goto mark_dir;
1795                 } else
1796                         return 0;
1797         }
1798
1799         if (p->ino == EXT2_RESIZE_INO) {
1800                 /* 
1801                  * The resize inode has already be sanity checked
1802                  * during pass #0 (the superblock checks).  All we
1803                  * have to do is mark the double indirect block as
1804                  * being in use; all of the other blocks are handled
1805                  * by mark_table_blocks()).
1806                  */
1807                 if (blockcnt == BLOCK_COUNT_DIND)
1808                         mark_block_used(ctx, blk);
1809         } else
1810                 mark_block_used(ctx, blk);
1811         p->num_blocks++;
1812         if (blockcnt >= 0)
1813                 p->last_block = blockcnt;
1814 mark_dir:
1815         if (p->is_dir && (blockcnt >= 0)) {
1816                 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
1817                                                     blk, blockcnt);
1818                 if (pctx->errcode) {
1819                         pctx->blk = blk;
1820                         pctx->num = blockcnt;
1821                         fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1822                         /* Should never get here */
1823                         ctx->flags |= E2F_FLAG_ABORT;
1824                         return BLOCK_ABORT;
1825                 }
1826         }
1827         return ret_code;
1828 }
1829
1830 static int process_bad_block(ext2_filsys fs,
1831                       blk_t *block_nr,
1832                       e2_blkcnt_t blockcnt,
1833                       blk_t ref_block EXT2FS_ATTR((unused)),
1834                       int ref_offset EXT2FS_ATTR((unused)),
1835                       void *priv_data)
1836 {
1837         struct process_block_struct *p;
1838         blk_t           blk = *block_nr;
1839         blk_t           first_block;
1840         dgrp_t          i;
1841         struct problem_context *pctx;
1842         e2fsck_t        ctx;
1843
1844         /*
1845          * Note: This function processes blocks for the bad blocks
1846          * inode, which is never compressed.  So we don't use HOLE_BLKADDR().
1847          */
1848
1849         if (!blk)
1850                 return 0;
1851         
1852         p = (struct process_block_struct *) priv_data;
1853         ctx = p->ctx;
1854         pctx = p->pctx;
1855         
1856         pctx->ino = EXT2_BAD_INO;
1857         pctx->blk = blk;
1858         pctx->blkcount = blockcnt;
1859
1860         if ((blk < fs->super->s_first_data_block) ||
1861             (blk >= fs->super->s_blocks_count)) {
1862                 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
1863                         *block_nr = 0;
1864                         return BLOCK_CHANGED;
1865                 } else
1866                         return 0;
1867         }
1868
1869         if (blockcnt < 0) {
1870                 if (ext2fs_test_block_bitmap(p->fs_meta_blocks, blk)) {
1871                         p->bbcheck = 1;
1872                         if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
1873                                 *block_nr = 0;
1874                                 return BLOCK_CHANGED;
1875                         }
1876                 } else if (ext2fs_test_block_bitmap(ctx->block_found_map, 
1877                                                     blk)) {
1878                         p->bbcheck = 1;
1879                         if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, 
1880                                         pctx)) {
1881                                 *block_nr = 0;
1882                                 return BLOCK_CHANGED;
1883                         }
1884                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1885                                 return BLOCK_ABORT;
1886                 } else
1887                         mark_block_used(ctx, blk);
1888                 return 0;
1889         }
1890 #if 0 
1891         printf ("DEBUG: Marking %u as bad.\n", blk);
1892 #endif
1893         ctx->fs_badblocks_count++;
1894         /*
1895          * If the block is not used, then mark it as used and return.
1896          * If it is already marked as found, this must mean that
1897          * there's an overlap between the filesystem table blocks
1898          * (bitmaps and inode table) and the bad block list.
1899          */
1900         if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1901                 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1902                 return 0;
1903         }
1904         /*
1905          * Try to find the where the filesystem block was used...
1906          */
1907         first_block = fs->super->s_first_data_block;
1908         
1909         for (i = 0; i < fs->group_desc_count; i++ ) {
1910                 pctx->group = i;
1911                 pctx->blk = blk;
1912                 if (!ext2fs_bg_has_super(fs, i))
1913                         goto skip_super;
1914                 if (blk == first_block) {
1915                         if (i == 0) {
1916                                 if (fix_problem(ctx,
1917                                                 PR_1_BAD_PRIMARY_SUPERBLOCK,
1918                                                 pctx)) {
1919                                         *block_nr = 0;
1920                                         return BLOCK_CHANGED;
1921                                 }
1922                                 return 0;
1923                         }
1924                         fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
1925                         return 0;
1926                 }
1927                 if ((blk > first_block) &&
1928                     (blk <= first_block + fs->desc_blocks)) {
1929                         if (i == 0) {
1930                                 pctx->blk = *block_nr;
1931                                 if (fix_problem(ctx,
1932                         PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
1933                                         *block_nr = 0;
1934                                         return BLOCK_CHANGED;
1935                                 }
1936                                 return 0;
1937                         }
1938                         fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
1939                         return 0;
1940                 }
1941         skip_super:
1942                 if (blk == fs->group_desc[i].bg_block_bitmap) {
1943                         if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
1944                                 ctx->invalid_block_bitmap_flag[i]++;
1945                                 ctx->invalid_bitmaps++;
1946                         }
1947                         return 0;
1948                 }
1949                 if (blk == fs->group_desc[i].bg_inode_bitmap) {
1950                         if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
1951                                 ctx->invalid_inode_bitmap_flag[i]++;
1952                                 ctx->invalid_bitmaps++;
1953                         }
1954                         return 0;
1955                 }
1956                 if ((blk >= fs->group_desc[i].bg_inode_table) &&
1957                     (blk < (fs->group_desc[i].bg_inode_table +
1958                             fs->inode_blocks_per_group))) {
1959                         /*
1960                          * If there are bad blocks in the inode table,
1961                          * the inode scan code will try to do
1962                          * something reasonable automatically.
1963                          */
1964                         return 0;
1965                 }
1966                 first_block += fs->super->s_blocks_per_group;
1967         }
1968         /*
1969          * If we've gotten to this point, then the only
1970          * possibility is that the bad block inode meta data
1971          * is using a bad block.
1972          */
1973         if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
1974             (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
1975             (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
1976                 p->bbcheck = 1;
1977                 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
1978                         *block_nr = 0;
1979                         return BLOCK_CHANGED;
1980                 }
1981                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1982                         return BLOCK_ABORT;
1983                 return 0;
1984         }
1985
1986         pctx->group = -1;
1987
1988         /* Warn user that the block wasn't claimed */
1989         fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
1990
1991         return 0;
1992 }
1993
1994 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group, 
1995                             const char *name, int num, blk_t *new_block)
1996 {
1997         ext2_filsys fs = ctx->fs;
1998         blk_t           old_block = *new_block;
1999         blk_t           last_block;
2000         int             i;
2001         char            *buf;
2002         struct problem_context  pctx;
2003
2004         clear_problem_context(&pctx);
2005
2006         pctx.group = group;
2007         pctx.blk = old_block;
2008         pctx.str = name;
2009
2010         last_block = ext2fs_group_last_block(fs, group);
2011         pctx.errcode = ext2fs_get_free_blocks(fs, first_block, last_block,
2012                                         num, ctx->block_found_map, new_block);
2013         if (pctx.errcode) {
2014                 pctx.num = num;
2015                 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
2016                 ext2fs_unmark_valid(fs);
2017                 return;
2018         }
2019         pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
2020         if (pctx.errcode) {
2021                 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
2022                 ext2fs_unmark_valid(fs);
2023                 return;
2024         }
2025         ext2fs_mark_super_dirty(fs);
2026         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2027         pctx.blk2 = *new_block;
2028         fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
2029                           PR_1_RELOC_TO), &pctx);
2030         pctx.blk2 = 0;
2031         for (i = 0; i < num; i++) {
2032                 pctx.blk = i;
2033                 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
2034                 if (old_block) {
2035                         pctx.errcode = io_channel_read_blk(fs->io,
2036                                    old_block + i, 1, buf);
2037                         if (pctx.errcode)
2038                                 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
2039                 } else
2040                         memset(buf, 0, fs->blocksize);
2041
2042                 pctx.blk = (*new_block) + i;
2043                 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
2044                                               1, buf);
2045                 if (pctx.errcode)
2046                         fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
2047         }
2048         ext2fs_free_mem(&buf);
2049 }
2050
2051 /*
2052  * This routine gets called at the end of pass 1 if bad blocks are
2053  * detected in the superblock, group descriptors, inode_bitmaps, or
2054  * block bitmaps.  At this point, all of the blocks have been mapped
2055  * out, so we can try to allocate new block(s) to replace the bad
2056  * blocks.
2057  */
2058 static void handle_fs_bad_blocks(e2fsck_t ctx)
2059 {
2060         ext2_filsys fs = ctx->fs;
2061         dgrp_t          i;
2062         blk_t           first_block;
2063
2064         for (i = 0; i < fs->group_desc_count; i++) {
2065                 first_block = ext2fs_group_first_block(fs, i);
2066
2067                 if (ctx->invalid_block_bitmap_flag[i]) {
2068                         new_table_block(ctx, first_block, i, _("block bitmap"),
2069                                         1, &fs->group_desc[i].bg_block_bitmap);
2070                 }
2071                 if (ctx->invalid_inode_bitmap_flag[i]) {
2072                         new_table_block(ctx, first_block, i, _("inode bitmap"),
2073                                         1, &fs->group_desc[i].bg_inode_bitmap);
2074                 }
2075                 if (ctx->invalid_inode_table_flag[i]) {
2076                         new_table_block(ctx, first_block, i, _("inode table"),
2077                                         fs->inode_blocks_per_group, 
2078                                         &fs->group_desc[i].bg_inode_table);
2079                         ctx->flags |= E2F_FLAG_RESTART;
2080                 }
2081         }
2082         ctx->invalid_bitmaps = 0;
2083 }
2084
2085 /*
2086  * This routine marks all blocks which are used by the superblock,
2087  * group descriptors, inode bitmaps, and block bitmaps.
2088  */
2089 static void mark_table_blocks(e2fsck_t ctx)
2090 {
2091         ext2_filsys fs = ctx->fs;
2092         blk_t   b;
2093         dgrp_t  i;
2094         int     j;
2095         struct problem_context pctx;
2096         
2097         clear_problem_context(&pctx);
2098         
2099         for (i = 0; i < fs->group_desc_count; i++) {
2100                 pctx.group = i;
2101
2102                 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
2103
2104                 /*
2105                  * Mark the blocks used for the inode table
2106                  */
2107                 if (fs->group_desc[i].bg_inode_table) {
2108                         for (j = 0, b = fs->group_desc[i].bg_inode_table;
2109                              j < fs->inode_blocks_per_group;
2110                              j++, b++) {
2111                                 if (ext2fs_test_block_bitmap(ctx->block_found_map,
2112                                                              b)) {
2113                                         pctx.blk = b;
2114                                         if (fix_problem(ctx,
2115                                                 PR_1_ITABLE_CONFLICT, &pctx)) {
2116                                                 ctx->invalid_inode_table_flag[i]++;
2117                                                 ctx->invalid_bitmaps++;
2118                                         }
2119                                 } else {
2120                                     ext2fs_mark_block_bitmap(ctx->block_found_map,
2121                                                              b);
2122                                 }
2123                         }
2124                 }
2125                             
2126                 /*
2127                  * Mark block used for the block bitmap 
2128                  */
2129                 if (fs->group_desc[i].bg_block_bitmap) {
2130                         if (ext2fs_test_block_bitmap(ctx->block_found_map,
2131                                      fs->group_desc[i].bg_block_bitmap)) {
2132                                 pctx.blk = fs->group_desc[i].bg_block_bitmap;
2133                                 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
2134                                         ctx->invalid_block_bitmap_flag[i]++;
2135                                         ctx->invalid_bitmaps++;
2136                                 }
2137                         } else {
2138                             ext2fs_mark_block_bitmap(ctx->block_found_map,
2139                                      fs->group_desc[i].bg_block_bitmap);
2140                     }
2141                         
2142                 }
2143                 /*
2144                  * Mark block used for the inode bitmap 
2145                  */
2146                 if (fs->group_desc[i].bg_inode_bitmap) {
2147                         if (ext2fs_test_block_bitmap(ctx->block_found_map,
2148                                      fs->group_desc[i].bg_inode_bitmap)) {
2149                                 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
2150                                 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
2151                                         ctx->invalid_inode_bitmap_flag[i]++;
2152                                         ctx->invalid_bitmaps++;
2153                                 } 
2154                         } else {
2155                             ext2fs_mark_block_bitmap(ctx->block_found_map,
2156                                      fs->group_desc[i].bg_inode_bitmap);
2157                         }
2158                 }
2159         }
2160 }
2161         
2162 /*
2163  * Thes subroutines short circuits ext2fs_get_blocks and
2164  * ext2fs_check_directory; we use them since we already have the inode
2165  * structure, so there's no point in letting the ext2fs library read
2166  * the inode again.
2167  */
2168 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
2169                                   blk_t *blocks)
2170 {
2171         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2172         int     i;
2173         
2174         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2175                 return EXT2_ET_CALLBACK_NOTHANDLED;
2176
2177         for (i=0; i < EXT2_N_BLOCKS; i++)
2178                 blocks[i] = ctx->stashed_inode->i_block[i];
2179         return 0;
2180 }
2181
2182 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
2183                                   struct ext2_inode *inode)
2184 {
2185         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2186
2187         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2188                 return EXT2_ET_CALLBACK_NOTHANDLED;
2189         *inode = *ctx->stashed_inode;
2190         return 0;
2191 }
2192
2193 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
2194                             struct ext2_inode *inode)
2195 {
2196         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2197
2198         if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
2199                 (inode != ctx->stashed_inode))
2200                 *ctx->stashed_inode = *inode;
2201         return EXT2_ET_CALLBACK_NOTHANDLED;
2202 }
2203
2204 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
2205 {
2206         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2207
2208         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2209                 return EXT2_ET_CALLBACK_NOTHANDLED;
2210
2211         if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
2212                 return EXT2_ET_NO_DIRECTORY;
2213         return 0;
2214 }
2215
2216 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
2217 {
2218         ext2_filsys fs = ctx->fs;
2219
2220         if (bool) {
2221                 fs->get_blocks = pass1_get_blocks;
2222                 fs->check_directory = pass1_check_directory;
2223                 fs->read_inode = pass1_read_inode;
2224                 fs->write_inode = pass1_write_inode;
2225                 ctx->stashed_ino = 0;
2226         } else {
2227                 fs->get_blocks = 0;
2228                 fs->check_directory = 0;
2229                 fs->read_inode = 0;
2230                 fs->write_inode = 0;
2231         }
2232 }