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