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