Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / e2fsck / pass1.c
1 /*
2  * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
3  * 
4  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  * 
11  * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12  * and applies the following tests to each inode:
13  *
14  *      - The mode field of the inode must be legal.
15  *      - The size and block count fields of the inode are correct.
16  *      - A data block must not be used by another inode
17  *
18  * Pass 1 also gathers the collects the following information:
19  *
20  *      - A bitmap of which inodes are in use.          (inode_used_map)
21  *      - A bitmap of which inodes are directories.     (inode_dir_map)
22  *      - A bitmap of which inodes are regular files.   (inode_reg_map)
23  *      - A bitmap of which inodes have bad fields.     (inode_bad_map)
24  *      - A bitmap of which inodes are in bad blocks.   (inode_bb_map)
25  *      - A bitmap of which inodes are imagic inodes.   (inode_imagic_map)
26  *      - A bitmap of which blocks are in use.          (block_found_map)
27  *      - A bitmap of which blocks are in use by two inodes     (block_dup_map)
28  *      - The data blocks of the directory inodes.      (dir_map)
29  *
30  * Pass 1 is designed to stash away enough information so that the
31  * other passes should not need to read in the inode information
32  * during the normal course of a filesystem check.  (Althogh if an
33  * inconsistency is detected, other passes may need to read in an
34  * inode to fix it.)
35  *
36  * Note that pass 1B will be invoked if there are any duplicate blocks
37  * found.
38  */
39
40 #include <time.h>
41 #ifdef HAVE_ERRNO_H
42 #include <errno.h>
43 #endif
44
45 #include "e2fsck.h"
46 #include "problem.h"
47
48 #ifdef NO_INLINE_FUNCS
49 #define _INLINE_
50 #else
51 #define _INLINE_ inline
52 #endif
53
54 static int process_block(ext2_filsys fs, blk_t  *blocknr,
55                          e2_blkcnt_t blockcnt, blk_t ref_blk, 
56                          int ref_offset, void *priv_data);
57 static int process_bad_block(ext2_filsys fs, blk_t *block_nr,
58                              e2_blkcnt_t blockcnt, blk_t ref_blk,
59                              int ref_offset, void *priv_data);
60 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
61                          char *block_buf);
62 static void mark_table_blocks(e2fsck_t ctx);
63 static void alloc_bad_map(e2fsck_t ctx);
64 static void alloc_bb_map(e2fsck_t ctx);
65 static void alloc_imagic_map(e2fsck_t ctx);
66 static void handle_fs_bad_blocks(e2fsck_t ctx);
67 static void process_inodes(e2fsck_t ctx, char *block_buf);
68 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
69 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
70                                   dgrp_t group, void * priv_data);
71 /* static char *describe_illegal_block(ext2_filsys fs, blk_t block); */
72
73 struct process_block_struct {
74         ino_t           ino;
75         int             is_dir:1, clear:1, suppress:1,
76                                 fragmented:1, compressed:1;
77         blk_t           num_blocks;
78         e2_blkcnt_t     last_block;
79         int             num_illegal_blocks;
80         blk_t           previous_block;
81         struct ext2_inode *inode;
82         struct problem_context *pctx;
83         e2fsck_t        ctx;
84 };
85
86 struct process_inode_block {
87         ino_t   ino;
88         struct ext2_inode inode;
89 };
90
91 struct scan_callback_struct {
92         e2fsck_t        ctx;
93         char            *block_buf;
94 };
95
96 /*
97  * For the inodes to process list.
98  */
99 static struct process_inode_block *inodes_to_process;
100 static int process_inode_count;
101
102 static __u64 ext2_max_sizes[4];
103
104 /*
105  * Free all memory allocated by pass1 in preparation for restarting
106  * things.
107  */
108 static void unwind_pass1(ext2_filsys fs)
109 {
110         ext2fs_free_mem((void **) &inodes_to_process);
111         inodes_to_process = 0;
112 }
113
114 /*
115  * Check to make sure a device inode is real.  Returns 1 if the device
116  * checks out, 0 if not.
117  *
118  * Note: this routine is now also used to check FIFO's and Sockets,
119  * since they have the same requirement; the i_block fields should be
120  * zero. 
121  */
122 int e2fsck_pass1_check_device_inode(struct ext2_inode *inode)
123 {
124         int     i;
125
126         /*
127          * We should be able to do the test below all the time, but
128          * because the kernel doesn't forcibly clear the device
129          * inode's additional i_block fields, there are some rare
130          * occasions when a legitimate device inode will have non-zero
131          * additional i_block fields.  So for now, we only complain
132          * when the immutable flag is set, which should never happen
133          * for devices.  (And that's when the problem is caused, since
134          * you can't set or clear immutable flags for devices.)  Once
135          * the kernel has been fixed we can change this...
136          */
137         if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
138                 for (i=4; i < EXT2_N_BLOCKS; i++) 
139                         if (inode->i_block[i])
140                                 return 0;
141         }
142         return 1;
143 }
144
145 /*
146  * If the immutable (or append-only) flag is set on the inode, offer
147  * to clear it.
148  */
149 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
150 {
151         if (!(pctx->inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)))
152                 return;
153
154         if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
155                 return;
156
157         pctx->inode->i_flags &= ~((EXT2_IMMUTABLE_FL | EXT2_APPEND_FL));
158         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
159 }
160
161 /*
162  * If device, fifo or socket, check size is zero -- if not offer to
163  * clear it
164  */
165 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
166 {
167         struct ext2_inode *inode = pctx->inode;
168         
169         if ((LINUX_S_ISBLK(inode->i_mode) ||
170              LINUX_S_ISCHR(inode->i_mode) ||
171              LINUX_S_ISFIFO(inode->i_mode) ||
172              LINUX_S_ISSOCK(inode->i_mode)) && 
173             !inode->i_size)
174                 return;
175         
176         if(!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
177                 return;
178         
179         inode->i_size = 0;
180         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
181 }
182         
183
184 void e2fsck_pass1(e2fsck_t ctx)
185 {
186         int     i;
187         __u64   max_sizes;
188         ext2_filsys fs = ctx->fs;
189         ino_t   ino;
190         struct ext2_inode inode;
191         ext2_inode_scan scan;
192         char            *block_buf;
193 #ifdef RESOURCE_TRACK
194         struct resource_track   rtrack;
195 #endif
196         unsigned char   frag, fsize;
197         struct          problem_context pctx;
198         struct          scan_callback_struct scan_struct;
199         struct ext2_super_block *sb = ctx->fs->super;
200         int             imagic_fs;
201         
202 #ifdef RESOURCE_TRACK
203         init_resource_track(&rtrack);
204 #endif
205         clear_problem_context(&pctx);
206
207         if (!(ctx->options & E2F_OPT_PREEN))
208                 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
209
210 #ifdef MTRACE
211         mtrace_print("Pass 1");
212 #endif
213
214 #define EXT2_BPP(bits) (1UL << ((bits) - 2))
215
216         for (i=0; i < 4; i++) {
217                 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(10+i);
218                 max_sizes = max_sizes + EXT2_BPP(10+i) * EXT2_BPP(10+i);
219                 max_sizes = (max_sizes +
220                              (__u64) EXT2_BPP(10+i) * EXT2_BPP(10+i) *
221                              EXT2_BPP(10+i));
222                 max_sizes = (max_sizes * (1UL << (10+i))) - 1;
223                 ext2_max_sizes[i] = max_sizes;
224         }
225 #undef EXT2_BPP
226
227         imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
228
229         /*
230          * Allocate bitmaps structures
231          */
232         pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
233                                               &ctx->inode_used_map);
234         if (pctx.errcode) {
235                 pctx.num = 1;
236                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
237                 ctx->flags |= E2F_FLAG_ABORT;
238                 return;
239         }
240         pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
241                                 _("directory inode map"), &ctx->inode_dir_map);
242         if (pctx.errcode) {
243                 pctx.num = 2;
244                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
245                 ctx->flags |= E2F_FLAG_ABORT;
246                 return;
247         }
248         pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
249                         _("regular file inode map"), &ctx->inode_reg_map);
250         if (pctx.errcode) {
251                 pctx.num = 6;
252                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
253                 ctx->flags |= E2F_FLAG_ABORT;
254                 return;
255         }
256         pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
257                                               &ctx->block_found_map);
258         if (pctx.errcode) {
259                 pctx.num = 1;
260                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
261                 ctx->flags |= E2F_FLAG_ABORT;
262                 return;
263         }
264         pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
265                                              &ctx->inode_link_info);
266         if (pctx.errcode) {
267                 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
268                 ctx->flags |= E2F_FLAG_ABORT;
269                 return;
270         }
271         inodes_to_process = (struct process_inode_block *)
272                 e2fsck_allocate_memory(ctx,
273                                        (ctx->process_inode_size *
274                                         sizeof(struct process_inode_block)),
275                                        "array of inodes to process");
276         process_inode_count = 0;
277
278         pctx.errcode = ext2fs_init_dblist(fs, 0);
279         if (pctx.errcode) {
280                 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
281                 ctx->flags |= E2F_FLAG_ABORT;
282                 return;
283         }
284
285         /*
286          * If the last orphan field is set, clear it, since the pass1
287          * processing will automatically find and clear the orphans.
288          * In the future, we may want to try using the last_orphan
289          * linked list ourselves, but for now, we clear it so that the
290          * ext3 mount code won't get confused.
291          */
292         if (!(ctx->options & E2F_OPT_READONLY)) {
293                 if (fs->super->s_last_orphan) {
294                         fs->super->s_last_orphan = 0;
295                         ext2fs_mark_super_dirty(fs);
296                 }
297         }
298
299         mark_table_blocks(ctx);
300         block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
301                                                     "block interate buffer");
302         e2fsck_use_inode_shortcuts(ctx, 1);
303         ehandler_operation(_("doing inode scan"));
304         pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks, 
305                                               &scan);
306         if (pctx.errcode) {
307                 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
308                 ctx->flags |= E2F_FLAG_ABORT;
309                 return;
310         }
311         ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
312         pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
313         if (pctx.errcode) {
314                 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
315                 ctx->flags |= E2F_FLAG_ABORT;
316                 return;
317         }
318         ctx->stashed_inode = &inode;
319         scan_struct.ctx = ctx;
320         scan_struct.block_buf = block_buf;
321         ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
322         if (ctx->progress)
323                 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
324                         return;
325         while (ino) {
326                 pctx.ino = ino;
327                 pctx.inode = &inode;
328                 ctx->stashed_ino = ino;
329                 if (inode.i_links_count) {
330                         pctx.errcode = ext2fs_icount_store(ctx->inode_link_info, 
331                                            ino, inode.i_links_count);
332                         if (pctx.errcode) {
333                                 pctx.num = inode.i_links_count;
334                                 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
335                                 ctx->flags |= E2F_FLAG_ABORT;
336                                 return;
337                         }
338                 }
339                 if (ino == EXT2_BAD_INO) {
340                         struct process_block_struct pb;
341                         
342                         pb.ino = EXT2_BAD_INO;
343                         pb.num_blocks = pb.last_block = 0;
344                         pb.num_illegal_blocks = 0;
345                         pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
346                         pb.fragmented = 0;
347                         pb.inode = &inode;
348                         pb.pctx = &pctx;
349                         pb.ctx = ctx;
350                         pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, 
351                                      block_buf, process_bad_block, &pb);
352                         if (pctx.errcode) {
353                                 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
354                                 ctx->flags |= E2F_FLAG_ABORT;
355                                 return;
356                         }
357                         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
358                         clear_problem_context(&pctx);
359                         goto next;
360                 }
361                 if (ino == EXT2_ROOT_INO) {
362                         /*
363                          * Make sure the root inode is a directory; if
364                          * not, offer to clear it.  It will be
365                          * regnerated in pass #3.
366                          */
367                         if (!LINUX_S_ISDIR(inode.i_mode)) {
368                                 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) {
369                                         inode.i_dtime = time(0);
370                                         inode.i_links_count = 0;
371                                         ext2fs_icount_store(ctx->inode_link_info,
372                                                             ino, 0);
373                                         e2fsck_write_inode(ctx, ino, &inode,
374                                                            "pass1");
375                                 }
376
377                         }
378                         /*
379                          * If dtime is set, offer to clear it.  mke2fs
380                          * version 0.2b created filesystems with the
381                          * dtime field set for the root and lost+found
382                          * directories.  We won't worry about
383                          * /lost+found, since that can be regenerated
384                          * easily.  But we will fix the root directory
385                          * as a special case.
386                          */
387                         if (inode.i_dtime && inode.i_links_count) {
388                                 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
389                                         inode.i_dtime = 0;
390                                         e2fsck_write_inode(ctx, ino, &inode,
391                                                            "pass1");
392                                 }
393                         }
394                 }
395                 if (ino == EXT2_JOURNAL_INO) {
396                         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
397                         if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
398                                 check_blocks(ctx, &pctx, block_buf);
399                                 goto next;
400                         }
401                         if ((inode.i_blocks || inode.i_block[0]) &&
402                             fix_problem(ctx, PR1_JOURNAL_INODE_NOT_CLEAR, 
403                                         &pctx)) {
404                                 memset(&inode, 0, sizeof(inode));
405                                 e2fsck_write_inode(ctx, ino, &inode, "pass1");
406                         }
407                 }
408                 if ((ino != EXT2_ROOT_INO) &&
409                     (ino < EXT2_FIRST_INODE(fs->super))) {
410                         int     problem = 0;
411                         
412                         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
413                         if (ino == EXT2_BOOT_LOADER_INO) {
414                                 if (LINUX_S_ISDIR(inode.i_mode))
415                                         problem = PR_1_RESERVED_BAD_MODE;
416                         } else {
417                                 if (inode.i_mode != 0)
418                                         problem = PR_1_RESERVED_BAD_MODE;
419                         }
420                         if (problem) {
421                                 if (fix_problem(ctx, problem, &pctx)) {
422                                         inode.i_mode = 0;
423                                         e2fsck_write_inode(ctx, ino, &inode,
424                                                            "pass1");
425                                 }
426                         }
427                         check_blocks(ctx, &pctx, block_buf);
428                         goto next;
429                 }
430                 /*
431                  * This code assumes that deleted inodes have
432                  * i_links_count set to 0.  
433                  */
434                 if (!inode.i_links_count) {
435                         if (!inode.i_dtime && inode.i_mode) {
436                                 if (fix_problem(ctx,
437                                             PR_1_ZERO_DTIME, &pctx)) {
438                                         inode.i_dtime = time(0);
439                                         e2fsck_write_inode(ctx, ino, &inode,
440                                                            "pass1");
441                                 }
442                         }
443                         goto next;
444                 }
445                 /*
446                  * n.b.  0.3c ext2fs code didn't clear i_links_count for
447                  * deleted files.  Oops.
448                  *
449                  * Since all new ext2 implementations get this right,
450                  * we now assume that the case of non-zero
451                  * i_links_count and non-zero dtime means that we
452                  * should keep the file, not delete it.
453                  * 
454                  */
455                 if (inode.i_dtime) {
456                         if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
457                                 inode.i_dtime = 0;
458                                 e2fsck_write_inode(ctx, ino, &inode, "pass1");
459                         }
460                 }
461                 
462                 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
463                 switch (fs->super->s_creator_os) {
464                     case EXT2_OS_LINUX:
465                         frag = inode.osd2.linux2.l_i_frag;
466                         fsize = inode.osd2.linux2.l_i_fsize;
467                         break;
468                     case EXT2_OS_HURD:
469                         frag = inode.osd2.hurd2.h_i_frag;
470                         fsize = inode.osd2.hurd2.h_i_fsize;
471                         break;
472                     case EXT2_OS_MASIX:
473                         frag = inode.osd2.masix2.m_i_frag;
474                         fsize = inode.osd2.masix2.m_i_fsize;
475                         break;
476                     default:
477                         frag = fsize = 0;
478                 }
479                 
480                 if (inode.i_faddr || frag || fsize
481                     || inode.i_file_acl ||
482                     (LINUX_S_ISDIR(inode.i_mode) && inode.i_dir_acl)) {
483                         if (!ctx->inode_bad_map)
484                                 alloc_bad_map(ctx);
485                         ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
486                 }
487                 if (inode.i_flags & EXT2_IMAGIC_FL) {
488                         if (imagic_fs) {
489                                 if (!ctx->inode_imagic_map)
490                                         alloc_imagic_map(ctx);
491                                 ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
492                                                          ino);
493                         } else {
494                                 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
495                                         inode.i_flags &= ~EXT2_IMAGIC_FL;
496                                         e2fsck_write_inode(ctx, ino,
497                                                            &inode, "pass1");
498                                 }
499                         }
500                 }
501                 
502                 if (LINUX_S_ISDIR(inode.i_mode)) {
503                         ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
504                         e2fsck_add_dir_info(ctx, ino, 0);
505                         ctx->fs_directory_count++;
506                 } else if (LINUX_S_ISREG (inode.i_mode)) {
507                         ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
508                         ctx->fs_regular_count++;
509                 } else if (LINUX_S_ISCHR (inode.i_mode) &&
510                            e2fsck_pass1_check_device_inode(&inode)) {
511                         check_immutable(ctx, &pctx);
512                         check_size(ctx, &pctx);
513                         ctx->fs_chardev_count++;
514                 } else if (LINUX_S_ISBLK (inode.i_mode) &&
515                            e2fsck_pass1_check_device_inode(&inode)) {
516                         check_immutable(ctx, &pctx);
517                         check_size(ctx, &pctx);
518                         ctx->fs_blockdev_count++;
519                 } else if (LINUX_S_ISLNK (inode.i_mode)) {
520                         ctx->fs_symlinks_count++;
521                         if (!inode.i_blocks) {
522                                 ctx->fs_fast_symlinks_count++;
523                                 goto next;
524                         }
525                 }
526                 else if (LINUX_S_ISFIFO (inode.i_mode) &&
527                          e2fsck_pass1_check_device_inode(&inode)) {
528                         check_immutable(ctx, &pctx);
529                         check_size(ctx, &pctx);
530                         ctx->fs_fifo_count++;
531                 } else if ((LINUX_S_ISSOCK (inode.i_mode)) &&
532                            e2fsck_pass1_check_device_inode(&inode)) {
533                         check_immutable(ctx, &pctx);
534                         check_size(ctx, &pctx);
535                         ctx->fs_sockets_count++;
536                 } else {
537                         if (!ctx->inode_bad_map)
538                                 alloc_bad_map(ctx);
539                         ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
540                 }
541                 if (inode.i_block[EXT2_IND_BLOCK])
542                         ctx->fs_ind_count++;
543                 if (inode.i_block[EXT2_DIND_BLOCK])
544                         ctx->fs_dind_count++;
545                 if (inode.i_block[EXT2_TIND_BLOCK])
546                         ctx->fs_tind_count++;
547                 if (inode.i_block[EXT2_IND_BLOCK] ||
548                     inode.i_block[EXT2_DIND_BLOCK] ||
549                     inode.i_block[EXT2_TIND_BLOCK]) {
550                         inodes_to_process[process_inode_count].ino = ino;
551                         inodes_to_process[process_inode_count].inode = inode;
552                         process_inode_count++;
553                 } else
554                         check_blocks(ctx, &pctx, block_buf);
555
556                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
557                         return;
558
559                 if (process_inode_count >= ctx->process_inode_size) {
560                         process_inodes(ctx, block_buf);
561
562                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
563                                 return;
564                 }
565         next:
566                 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
567                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
568                         return;
569                 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
570                         if (!ctx->inode_bb_map)
571                                 alloc_bb_map(ctx);
572                         ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
573                         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
574                         goto next;
575                 }
576                 if (pctx.errcode) {
577                         fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
578                         ctx->flags |= E2F_FLAG_ABORT;
579                         return;
580                 }
581         }
582         process_inodes(ctx, block_buf);
583         ext2fs_close_inode_scan(scan);
584         ehandler_operation(0);
585
586         if (ctx->invalid_bitmaps)
587                 handle_fs_bad_blocks(ctx);
588
589         if (ctx->flags & E2F_FLAG_RESTART) {
590                 /*
591                  * Only the master copy of the superblock and block
592                  * group descriptors are going to be written during a
593                  * restart, so set the superblock to be used to be the
594                  * master superblock.
595                  */
596                 ctx->use_superblock = 0;
597                 unwind_pass1(fs);
598                 goto endit;
599         }
600
601         if (ctx->block_dup_map) {
602                 if (ctx->options & E2F_OPT_PREEN) {
603                         clear_problem_context(&pctx);
604                         fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
605                 }
606                 e2fsck_pass1_dupblocks(ctx, block_buf);
607         }
608         ext2fs_free_mem((void **) &inodes_to_process);
609 endit:
610         e2fsck_use_inode_shortcuts(ctx, 0);
611         
612         ext2fs_free_mem((void **) &block_buf);
613
614         if (ctx->large_files) {
615                 if (!(sb->s_feature_ro_compat &
616                       EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
617                     fix_problem(ctx, PR_1_FEATURE_LARGE_FILES, &pctx)) {
618                         sb->s_feature_ro_compat |=
619                                 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
620                         ext2fs_mark_super_dirty(fs);
621                 }
622                 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
623                     fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
624                         ext2fs_update_dynamic_rev(fs);
625                         ext2fs_mark_super_dirty(fs);
626                 }
627         } else if (!ctx->large_files &&
628             (sb->s_feature_ro_compat &
629               EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
630                 if (fs->flags & EXT2_FLAG_RW) {
631                         sb->s_feature_ro_compat &= 
632                                 ~EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
633                         ext2fs_mark_super_dirty(fs);
634                 }
635         }
636         
637 #ifdef RESOURCE_TRACK
638         if (ctx->options & E2F_OPT_TIME2) {
639                 e2fsck_clear_progbar(ctx);
640                 print_resource_track(_("Pass 1"), &rtrack);
641         }
642 #endif
643 }
644
645 /*
646  * When the inode_scan routines call this callback at the end of the
647  * glock group, call process_inodes.
648  */
649 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
650                                dgrp_t group, void * priv_data)
651 {
652         struct scan_callback_struct *scan_struct;
653         e2fsck_t ctx;
654
655         scan_struct = (struct scan_callback_struct *) priv_data;
656         ctx = scan_struct->ctx;
657         
658         process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
659
660         if (ctx->progress)
661                 if ((ctx->progress)(ctx, 1, group+1,
662                                     ctx->fs->group_desc_count))
663                         return EXT2_ET_CANCEL_REQUESTED;
664
665         return 0;
666 }
667
668 /*
669  * Process the inodes in the "inodes to process" list.
670  */
671 static void process_inodes(e2fsck_t ctx, char *block_buf)
672 {
673         int                     i;
674         struct ext2_inode       *old_stashed_inode;
675         ino_t                   old_stashed_ino;
676         const char              *old_operation;
677         char                    buf[80];
678         struct problem_context  pctx;
679         
680 #if 0
681         printf("begin process_inodes: ");
682 #endif
683         if (process_inode_count == 0)
684                 return;
685         old_operation = ehandler_operation(0);
686         old_stashed_inode = ctx->stashed_inode;
687         old_stashed_ino = ctx->stashed_ino;
688         qsort(inodes_to_process, process_inode_count,
689                       sizeof(struct process_inode_block), process_inode_cmp);
690         clear_problem_context(&pctx);
691         for (i=0; i < process_inode_count; i++) {
692                 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
693                 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
694                 
695 #if 0
696                 printf("%u ", pctx.ino);
697 #endif
698                 sprintf(buf, _("reading indirect blocks of inode %lu"),
699                         pctx.ino);
700                 ehandler_operation(buf);
701                 check_blocks(ctx, &pctx, block_buf);
702                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
703                         break;
704         }
705         ctx->stashed_inode = old_stashed_inode;
706         ctx->stashed_ino = old_stashed_ino;
707         process_inode_count = 0;
708 #if 0
709         printf("end process inodes\n");
710 #endif
711         ehandler_operation(old_operation);
712 }
713
714 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
715 {
716         const struct process_inode_block *ib_a =
717                 (const struct process_inode_block *) a;
718         const struct process_inode_block *ib_b =
719                 (const struct process_inode_block *) b;
720
721         return (ib_a->inode.i_block[EXT2_IND_BLOCK] -
722                 ib_b->inode.i_block[EXT2_IND_BLOCK]);
723 }
724
725 /*
726  * This procedure will allocate the inode bad map table
727  */
728 static void alloc_bad_map(e2fsck_t ctx)
729 {
730         struct          problem_context pctx;
731         
732         clear_problem_context(&pctx);
733         
734         pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs, _("bad inode map"),
735                                               &ctx->inode_bad_map);
736         if (pctx.errcode) {
737                 pctx.num = 3;
738                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
739                 /* Should never get here */
740                 ctx->flags |= E2F_FLAG_ABORT;
741                 return;
742         }
743 }
744
745 /*
746  * This procedure will allocate the inode "bb" (badblock) map table
747  */
748 static void alloc_bb_map(e2fsck_t ctx)
749 {
750         struct          problem_context pctx;
751         
752         clear_problem_context(&pctx);
753         pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
754                                               _("inode in bad block map"),
755                                               &ctx->inode_bb_map);
756         if (pctx.errcode) {
757                 pctx.num = 4;
758                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
759                 /* Should never get here */
760                 ctx->flags |= E2F_FLAG_ABORT;
761                 return;
762         }
763 }
764
765 /*
766  * This procedure will allocate the inode imagic table
767  */
768 static void alloc_imagic_map(e2fsck_t ctx)
769 {
770         struct          problem_context pctx;
771         
772         clear_problem_context(&pctx);
773         pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
774                                               _("imagic inode map"),
775                                               &ctx->inode_imagic_map);
776         if (pctx.errcode) {
777                 pctx.num = 5;
778                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
779                 /* Should never get here */
780                 ctx->flags |= E2F_FLAG_ABORT;
781                 return;
782         }
783 }
784
785 /*
786  * Marks a block as in use, setting the dup_map if it's been set
787  * already.  Called by process_block and process_bad_block.
788  *
789  * WARNING: Assumes checks have already been done to make sure block
790  * is valid.  This is true in both process_block and process_bad_block.
791  */
792 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
793 {
794         struct          problem_context pctx;
795         
796         clear_problem_context(&pctx);
797         
798         if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
799                 if (!ctx->block_dup_map) {
800                         pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
801                               _("multiply claimed block map"),
802                               &ctx->block_dup_map);
803                         if (pctx.errcode) {
804                                 pctx.num = 3;
805                                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, 
806                                             &pctx);
807                                 /* Should never get here */
808                                 ctx->flags |= E2F_FLAG_ABORT;
809                                 return;
810                         }
811                 }
812                 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
813         } else {
814                 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
815         }
816 }
817
818 /*
819  * This subroutine is called on each inode to account for all of the
820  * blocks used by that inode.
821  */
822 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
823                          char *block_buf)
824 {
825         ext2_filsys fs = ctx->fs;
826         struct process_block_struct pb;
827         ino_t           ino = pctx->ino;
828         struct ext2_inode *inode = pctx->inode;
829         int             bad_size = 0;
830         __u64           size;
831         
832         if (!ext2fs_inode_has_valid_blocks(pctx->inode))
833                 return;
834         
835         pb.ino = ino;
836         pb.num_blocks = pb.last_block = 0;
837         pb.num_illegal_blocks = 0;
838         pb.suppress = 0; pb.clear = 0;
839         pb.fragmented = 0;
840         pb.compressed = 0;
841         pb.previous_block = 0;
842         pb.is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
843         pb.inode = inode;
844         pb.pctx = pctx;
845         pb.ctx = ctx;
846         pctx->ino = ino;
847
848         if (inode->i_flags & EXT2_COMPRBLK_FL) {
849                 if (fs->super->s_feature_incompat &
850                     EXT2_FEATURE_INCOMPAT_COMPRESSION)
851                         pb.compressed = 1;
852                 else {
853                         if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
854                                 inode->i_flags &= ~EXT2_COMPRBLK_FL;
855                                 e2fsck_write_inode(ctx, ino, inode,
856                                                    "check_blocks");
857                         }
858                 }
859         }
860
861         pctx->errcode = ext2fs_block_iterate2(fs, ino,
862                                        pb.is_dir ? BLOCK_FLAG_HOLE : 0,
863                                        block_buf, process_block, &pb);
864         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
865                 return;
866         end_problem_latch(ctx, PR_LATCH_BLOCK);
867         if (pctx->errcode)
868                 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
869
870         if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
871                 ctx->fs_fragmented++;
872
873         if (pb.clear) {
874                 e2fsck_read_inode(ctx, ino, inode, "check_blocks");
875                 inode->i_links_count = 0;
876                 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
877                 inode->i_dtime = time(0);
878                 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
879                 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
880                 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
881                 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
882                 /*
883                  * The inode was probably partially accounted for
884                  * before processing was aborted, so we need to
885                  * restart the pass 1 scan.
886                  */
887                 ctx->flags |= E2F_FLAG_RESTART;
888                 return;
889         }
890
891         pb.num_blocks *= (fs->blocksize / 512);
892 #if 0
893         printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
894                ino, inode->i_size, pb.last_block, inode->i_blocks,
895                pb.num_blocks);
896 #endif
897         if (!pb.num_blocks && pb.is_dir) {
898                 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
899                         inode->i_links_count = 0;
900                         ext2fs_icount_store(ctx->inode_link_info, ino, 0);
901                         inode->i_dtime = time(0);
902                         e2fsck_write_inode(ctx, ino, inode, "check_blocks");
903                         ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
904                         ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
905                         ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
906                         ctx->fs_directory_count--;
907                         pb.is_dir = 0;
908                 }
909         }
910         if (pb.is_dir) {
911                 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
912                 if ((nblock > (pb.last_block + 1)) ||
913                     ((inode->i_size & (fs->blocksize-1)) != 0))
914                         bad_size = 1;
915                 else if (nblock < (pb.last_block + 1)) {
916                         if (((pb.last_block + 1) - nblock) >
917                             fs->super->s_prealloc_dir_blocks)
918                                 bad_size = 2;
919                 }
920         } else {
921                 size = inode->i_size + ((__u64) inode->i_size_high << 32);
922                 if ((size < pb.last_block * fs->blocksize))
923                         bad_size = 3;
924                 else if (size > ext2_max_sizes[fs->super->s_log_block_size])
925                         bad_size = 4;
926         }
927         if (bad_size) {
928                 pctx->num = (pb.last_block+1) * fs->blocksize;
929                 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
930                         inode->i_size = pctx->num;
931                         if (!pb.is_dir)
932                                 inode->i_size_high = pctx->num >> 32;
933                         e2fsck_write_inode(ctx, ino, inode, "check_blocks");
934                 }
935                 pctx->num = 0;
936         }
937         if (!pb.is_dir && inode->i_size_high)
938                 ctx->large_files++;
939         if (pb.num_blocks != inode->i_blocks) {
940                 pctx->num = pb.num_blocks;
941                 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
942                         inode->i_blocks = pb.num_blocks;
943                         e2fsck_write_inode(ctx, ino, inode, "check_blocks");
944                 }
945                 pctx->num = 0;
946         }
947 }
948
949 #if 0
950 /*
951  * Helper function called by process block when an illegal block is
952  * found.  It returns a description about why the block is illegal
953  */
954 static char *describe_illegal_block(ext2_filsys fs, blk_t block)
955 {
956         blk_t   super;
957         int     i;
958         static char     problem[80];
959
960         super = fs->super->s_first_data_block;
961         strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
962         if (block < super) {
963                 sprintf(problem, "< FIRSTBLOCK (%u)", super);
964                 return(problem);
965         } else if (block >= fs->super->s_blocks_count) {
966                 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
967                 return(problem);
968         }
969         for (i = 0; i < fs->group_desc_count; i++) {
970                 if (block == super) {
971                         sprintf(problem, "is the superblock in group %d", i);
972                         break;
973                 }
974                 if (block > super &&
975                     block <= (super + fs->desc_blocks)) {
976                         sprintf(problem, "is in the group descriptors "
977                                 "of group %d", i);
978                         break;
979                 }
980                 if (block == fs->group_desc[i].bg_block_bitmap) {
981                         sprintf(problem, "is the block bitmap of group %d", i);
982                         break;
983                 }
984                 if (block == fs->group_desc[i].bg_inode_bitmap) {
985                         sprintf(problem, "is the inode bitmap of group %d", i);
986                         break;
987                 }
988                 if (block >= fs->group_desc[i].bg_inode_table &&
989                     (block < fs->group_desc[i].bg_inode_table
990                      + fs->inode_blocks_per_group)) {
991                         sprintf(problem, "is in the inode table of group %d",
992                                 i);
993                         break;
994                 }
995                 super += fs->super->s_blocks_per_group;
996         }
997         return(problem);
998 }
999 #endif
1000
1001 /*
1002  * This is a helper function for check_blocks().
1003  */
1004 static int process_block(ext2_filsys fs,
1005                   blk_t *block_nr,
1006                   e2_blkcnt_t blockcnt,
1007                   blk_t ref_block,
1008                   int ref_offset, 
1009                   void *priv_data)
1010 {
1011         struct process_block_struct *p;
1012         struct problem_context *pctx;
1013         blk_t   blk = *block_nr;
1014         int     ret_code = 0;
1015         int     problem = 0;
1016         e2fsck_t        ctx;
1017
1018         p = (struct process_block_struct *) priv_data;
1019         pctx = p->pctx;
1020         ctx = p->ctx;
1021
1022         if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
1023                 /* todo: Check that the comprblk_fl is high, that the
1024                    blkaddr pattern looks right (all non-holes up to
1025                    first EXT2FS_COMPRESSED_BLKADDR, then all
1026                    EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
1027                    that the feature_incompat bit is high, and that the
1028                    inode is a regular file.  If we're doing a "full
1029                    check" (a concept introduced to e2fsck by e2compr,
1030                    meaning that we look at data blocks as well as
1031                    metadata) then call some library routine that
1032                    checks the compressed data.  I'll have to think
1033                    about this, because one particularly important
1034                    problem to be able to fix is to recalculate the
1035                    cluster size if necessary.  I think that perhaps
1036                    we'd better do most/all e2compr-specific checks
1037                    separately, after the non-e2compr checks.  If not
1038                    doing a full check, it may be useful to test that
1039                    the personality is linux; e.g. if it isn't then
1040                    perhaps this really is just an illegal block. */
1041                 return 0;
1042         }
1043         
1044         if (blk == 0) {
1045                 if (p->is_dir == 0) {
1046                         /*
1047                          * Should never happen, since only directories
1048                          * get called with BLOCK_FLAG_HOLE
1049                          */
1050 #if DEBUG_E2FSCK
1051                         printf("process_block() called with blk == 0, "
1052                                "blockcnt=%d, inode %lu???\n",
1053                                blockcnt, p->ino);
1054 #endif
1055                         return 0;
1056                 }
1057                 if (blockcnt < 0)
1058                         return 0;
1059                 if (blockcnt * fs->blocksize < p->inode->i_size) {
1060 #if 0
1061                         printf("Missing block (#%d) in directory inode %lu!\n",
1062                                blockcnt, p->ino);
1063 #endif
1064                         goto mark_dir;
1065                 }
1066                 return 0;
1067         }
1068
1069 #if 0
1070         printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
1071                blockcnt);
1072 #endif
1073         
1074         /*
1075          * Simplistic fragmentation check.  We merely require that the
1076          * file be contiguous.  (Which can never be true for really
1077          * big files that are greater than a block group.)
1078          */
1079         if (!HOLE_BLKADDR(p->previous_block)) {
1080                 if (p->previous_block+1 != blk)
1081                         p->fragmented = 1;
1082         }
1083         p->previous_block = blk;
1084         
1085         if (blk < fs->super->s_first_data_block ||
1086             blk >= fs->super->s_blocks_count)
1087                 problem = PR_1_ILLEGAL_BLOCK_NUM;
1088
1089         if (problem) {
1090                 p->num_illegal_blocks++;
1091                 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
1092                         if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
1093                                 p->clear = 1;
1094                                 return BLOCK_ABORT;
1095                         }
1096                         if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
1097                                 p->suppress = 1;
1098                                 set_latch_flags(PR_LATCH_BLOCK,
1099                                                 PRL_SUPPRESS, 0);
1100                         }
1101                 }
1102                 pctx->blk = blk;
1103                 pctx->blkcount = blockcnt;
1104                 if (fix_problem(ctx, problem, pctx)) {
1105                         blk = *block_nr = 0;
1106                         ret_code = BLOCK_CHANGED;
1107                         goto mark_dir;
1108                 } else
1109                         return 0;
1110                 pctx->blk = 0;
1111                 pctx->blkcount = -1;
1112         }
1113
1114         mark_block_used(ctx, blk);
1115         p->num_blocks++;
1116         if (blockcnt >= 0)
1117                 p->last_block = blockcnt;
1118 mark_dir:
1119         if (p->is_dir && (blockcnt >= 0)) {
1120                 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
1121                                                     blk, blockcnt);
1122                 if (pctx->errcode) {
1123                         pctx->blk = blk;
1124                         pctx->num = blockcnt;
1125                         fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1126                         /* Should never get here */
1127                         ctx->flags |= E2F_FLAG_ABORT;
1128                         return BLOCK_ABORT;
1129                 }
1130         }
1131         return ret_code;
1132 }
1133
1134 static void bad_block_indirect(e2fsck_t ctx, blk_t blk)
1135 {
1136         struct problem_context pctx;
1137
1138         clear_problem_context(&pctx);
1139         /*
1140          * Prompt to see if we should continue or not.
1141          */
1142         if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, &pctx))
1143                 ctx->flags |= E2F_FLAG_ABORT;
1144 }
1145
1146 static int process_bad_block(ext2_filsys fs,
1147                       blk_t *block_nr,
1148                       e2_blkcnt_t blockcnt,
1149                       blk_t ref_block,
1150                       int ref_offset,
1151                       void *priv_data)
1152 {
1153         struct process_block_struct *p;
1154         blk_t           blk = *block_nr;
1155         int             first_block;
1156         int             i;
1157         struct problem_context *pctx;
1158         e2fsck_t        ctx;
1159
1160         /*
1161          * Note: This function processes blocks for the bad blocks
1162          * inode, which is never compressed.  So we don't use HOLE_BLKADDR().
1163          */
1164
1165         if (!blk)
1166                 return 0;
1167         
1168         p = (struct process_block_struct *) priv_data;
1169         ctx = p->ctx;
1170         pctx = p->pctx;
1171         
1172         pctx->ino = EXT2_BAD_INO;
1173         pctx->blk = blk;
1174         pctx->blkcount = blockcnt;
1175
1176         if ((blk < fs->super->s_first_data_block) ||
1177             (blk >= fs->super->s_blocks_count)) {
1178                 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
1179                         *block_nr = 0;
1180                         return BLOCK_CHANGED;
1181                 } else
1182                         return 0;
1183         }
1184
1185         if (blockcnt < 0) {
1186                 if (ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1187                         bad_block_indirect(ctx, blk);
1188                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1189                                 return BLOCK_ABORT;
1190                 } else
1191                         mark_block_used(ctx, blk);
1192                 return 0;
1193         }
1194 #if 0 
1195         printf ("DEBUG: Marking %u as bad.\n", blk);
1196 #endif
1197         ctx->fs_badblocks_count++;
1198         /*
1199          * If the block is not used, then mark it as used and return.
1200          * If it is already marked as found, this must mean that
1201          * there's an overlap between the filesystem table blocks
1202          * (bitmaps and inode table) and the bad block list.
1203          */
1204         if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1205                 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1206                 return 0;
1207         }
1208         /*
1209          * Try to find the where the filesystem block was used...
1210          */
1211         first_block = fs->super->s_first_data_block;
1212         
1213         for (i = 0; i < fs->group_desc_count; i++ ) {
1214                 pctx->group = i;
1215                 pctx->blk = blk;
1216                 if (!ext2fs_bg_has_super(fs, i))
1217                         goto skip_super;
1218                 if (blk == first_block) {
1219                         if (i == 0) {
1220                                 if (fix_problem(ctx,
1221                                                 PR_1_BAD_PRIMARY_SUPERBLOCK,
1222                                                 pctx)) {
1223                                         *block_nr = 0;
1224                                         return BLOCK_CHANGED;
1225                                 }
1226                                 return 0;
1227                         }
1228                         fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
1229                         return 0;
1230                 }
1231                 if ((blk > first_block) &&
1232                     (blk <= first_block + fs->desc_blocks)) {
1233                         if (i == 0) {
1234                                 pctx->blk = *block_nr;
1235                                 if (fix_problem(ctx,
1236                         PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
1237                                         *block_nr = 0;
1238                                         return BLOCK_CHANGED;
1239                                 }
1240                                 return 0;
1241                         }
1242                         fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
1243                         return 0;
1244                 }
1245         skip_super:
1246                 if (blk == fs->group_desc[i].bg_block_bitmap) {
1247                         if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
1248                                 ctx->invalid_block_bitmap_flag[i]++;
1249                                 ctx->invalid_bitmaps++;
1250                         }
1251                         return 0;
1252                 }
1253                 if (blk == fs->group_desc[i].bg_inode_bitmap) {
1254                         if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
1255                                 ctx->invalid_inode_bitmap_flag[i]++;
1256                                 ctx->invalid_bitmaps++;
1257                         }
1258                         return 0;
1259                 }
1260                 if ((blk >= fs->group_desc[i].bg_inode_table) &&
1261                     (blk < (fs->group_desc[i].bg_inode_table +
1262                             fs->inode_blocks_per_group))) {
1263                         /*
1264                          * If there are bad blocks in the inode table,
1265                          * the inode scan code will try to do
1266                          * something reasonable automatically.
1267                          */
1268                         return 0;
1269                 }
1270                 first_block += fs->super->s_blocks_per_group;
1271         }
1272         /*
1273          * If we've gotten to this point, then the only
1274          * possibility is that the bad block inode meta data
1275          * is using a bad block.
1276          */
1277         if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
1278             p->inode->i_block[EXT2_DIND_BLOCK]) {
1279                 bad_block_indirect(ctx, blk);
1280                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1281                         return BLOCK_ABORT;
1282                 return 0;
1283         }
1284
1285         pctx->group = -1;
1286
1287         /* Warn user that the block wasn't claimed */
1288         fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
1289
1290         return 0;
1291 }
1292
1293 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group, 
1294                             const char *name, int num, blk_t *new_block)
1295 {
1296         ext2_filsys fs = ctx->fs;
1297         blk_t           old_block = *new_block;
1298         int             i;
1299         char            *buf;
1300         struct problem_context  pctx;
1301
1302         clear_problem_context(&pctx);
1303
1304         pctx.group = group;
1305         pctx.blk = old_block;
1306         pctx.str = name;
1307
1308         pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
1309                         first_block + fs->super->s_blocks_per_group,
1310                                         num, ctx->block_found_map, new_block);
1311         if (pctx.errcode) {
1312                 pctx.num = num;
1313                 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
1314                 ext2fs_unmark_valid(fs);
1315                 return;
1316         }
1317         pctx.errcode = ext2fs_get_mem(fs->blocksize, (void **) &buf);
1318         if (pctx.errcode) {
1319                 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
1320                 ext2fs_unmark_valid(fs);
1321                 return;
1322         }
1323         ext2fs_mark_super_dirty(fs);
1324         pctx.blk2 = *new_block;
1325         fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
1326                           PR_1_RELOC_TO), &pctx);
1327         pctx.blk2 = 0;
1328         for (i = 0; i < num; i++) {
1329                 pctx.blk = i;
1330                 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
1331                 if (old_block) {
1332                         pctx.errcode = io_channel_read_blk(fs->io,
1333                                    old_block + i, 1, buf);
1334                         if (pctx.errcode)
1335                                 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
1336                 } else
1337                         memset(buf, 0, fs->blocksize);
1338
1339                 pctx.blk = (*new_block) + i;
1340                 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
1341                                               1, buf);
1342                 if (pctx.errcode)
1343                         fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
1344         }
1345         ext2fs_free_mem((void **) &buf);
1346 }
1347
1348 /*
1349  * This routine gets called at the end of pass 1 if bad blocks are
1350  * detected in the superblock, group descriptors, inode_bitmaps, or
1351  * block bitmaps.  At this point, all of the blocks have been mapped
1352  * out, so we can try to allocate new block(s) to replace the bad
1353  * blocks.
1354  */
1355 static void handle_fs_bad_blocks(e2fsck_t ctx)
1356 {
1357         ext2_filsys fs = ctx->fs;
1358         int             i;
1359         int             first_block = fs->super->s_first_data_block;
1360
1361         for (i = 0; i < fs->group_desc_count; i++) {
1362                 if (ctx->invalid_block_bitmap_flag[i]) {
1363                         new_table_block(ctx, first_block, i, _("block bitmap"),
1364                                         1, &fs->group_desc[i].bg_block_bitmap);
1365                 }
1366                 if (ctx->invalid_inode_bitmap_flag[i]) {
1367                         new_table_block(ctx, first_block, i, _("inode bitmap"),
1368                                         1, &fs->group_desc[i].bg_inode_bitmap);
1369                 }
1370                 if (ctx->invalid_inode_table_flag[i]) {
1371                         new_table_block(ctx, first_block, i, _("inode table"),
1372                                         fs->inode_blocks_per_group, 
1373                                         &fs->group_desc[i].bg_inode_table);
1374                         ctx->flags |= E2F_FLAG_RESTART;
1375                 }
1376                 first_block += fs->super->s_blocks_per_group;
1377         }
1378         ctx->invalid_bitmaps = 0;
1379 }
1380
1381 /*
1382  * This routine marks all blocks which are used by the superblock,
1383  * group descriptors, inode bitmaps, and block bitmaps.
1384  */
1385 static void mark_table_blocks(e2fsck_t ctx)
1386 {
1387         ext2_filsys fs = ctx->fs;
1388         blk_t   block, b;
1389         int     i,j;
1390         struct problem_context pctx;
1391         
1392         clear_problem_context(&pctx);
1393         
1394         block = fs->super->s_first_data_block;
1395         for (i = 0; i < fs->group_desc_count; i++) {
1396                 pctx.group = i;
1397
1398                 if (ext2fs_bg_has_super(fs, i)) {
1399                         /*
1400                          * Mark this group's copy of the superblock
1401                          */
1402                         ext2fs_mark_block_bitmap(ctx->block_found_map, block);
1403                 
1404                         /*
1405                          * Mark this group's copy of the descriptors
1406                          */
1407                         for (j = 0; j < fs->desc_blocks; j++) {
1408                                 ext2fs_mark_block_bitmap(ctx->block_found_map,
1409                                                          block + j + 1);
1410                         }
1411                 }
1412                 
1413                 /*
1414                  * Mark the blocks used for the inode table
1415                  */
1416                 if (fs->group_desc[i].bg_inode_table) {
1417                         for (j = 0, b = fs->group_desc[i].bg_inode_table;
1418                              j < fs->inode_blocks_per_group;
1419                              j++, b++) {
1420                                 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1421                                                              b)) {
1422                                         pctx.blk = b;
1423                                         if (fix_problem(ctx,
1424                                                 PR_1_ITABLE_CONFLICT, &pctx)) {
1425                                                 ctx->invalid_inode_table_flag[i]++;
1426                                                 ctx->invalid_bitmaps++;
1427                                         }
1428                                 } else {
1429                                     ext2fs_mark_block_bitmap(ctx->block_found_map,
1430                                                              b);
1431                                 }
1432                         }
1433                 }
1434                             
1435                 /*
1436                  * Mark block used for the block bitmap 
1437                  */
1438                 if (fs->group_desc[i].bg_block_bitmap) {
1439                         if (ext2fs_test_block_bitmap(ctx->block_found_map,
1440                                      fs->group_desc[i].bg_block_bitmap)) {
1441                                 pctx.blk = fs->group_desc[i].bg_block_bitmap;
1442                                 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
1443                                         ctx->invalid_block_bitmap_flag[i]++;
1444                                         ctx->invalid_bitmaps++;
1445                                 }
1446                         } else {
1447                             ext2fs_mark_block_bitmap(ctx->block_found_map,
1448                                      fs->group_desc[i].bg_block_bitmap);
1449                     }
1450                         
1451                 }
1452                 /*
1453                  * Mark block used for the inode bitmap 
1454                  */
1455                 if (fs->group_desc[i].bg_inode_bitmap) {
1456                         if (ext2fs_test_block_bitmap(ctx->block_found_map,
1457                                      fs->group_desc[i].bg_inode_bitmap)) {
1458                                 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
1459                                 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
1460                                         ctx->invalid_inode_bitmap_flag[i]++;
1461                                         ctx->invalid_bitmaps++;
1462                                 } 
1463                         } else {
1464                             ext2fs_mark_block_bitmap(ctx->block_found_map,
1465                                      fs->group_desc[i].bg_inode_bitmap);
1466                         }
1467                 }
1468                 block += fs->super->s_blocks_per_group;
1469         }
1470 }
1471         
1472 /*
1473  * Thes subroutines short circuits ext2fs_get_blocks and
1474  * ext2fs_check_directory; we use them since we already have the inode
1475  * structure, so there's no point in letting the ext2fs library read
1476  * the inode again.
1477  */
1478 static errcode_t pass1_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks)
1479 {
1480         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1481         int     i;
1482         
1483         if (ino != ctx->stashed_ino)
1484                 return EXT2_ET_CALLBACK_NOTHANDLED;
1485
1486         for (i=0; i < EXT2_N_BLOCKS; i++)
1487                 blocks[i] = ctx->stashed_inode->i_block[i];
1488         return 0;
1489 }
1490
1491 static errcode_t pass1_read_inode(ext2_filsys fs, ino_t ino,
1492                                   struct ext2_inode *inode)
1493 {
1494         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1495
1496         if (ino != ctx->stashed_ino)
1497                 return EXT2_ET_CALLBACK_NOTHANDLED;
1498         *inode = *ctx->stashed_inode;
1499         return 0;
1500 }
1501
1502 static errcode_t pass1_write_inode(ext2_filsys fs, ino_t ino,
1503                             struct ext2_inode *inode)
1504 {
1505         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1506
1507         if (ino == ctx->stashed_ino)
1508                 *ctx->stashed_inode = *inode;
1509         return EXT2_ET_CALLBACK_NOTHANDLED;
1510 }
1511
1512 static errcode_t pass1_check_directory(ext2_filsys fs, ino_t ino)
1513 {
1514         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1515
1516         if (ino != ctx->stashed_ino)
1517                 return EXT2_ET_CALLBACK_NOTHANDLED;
1518
1519         if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
1520                 return EXT2_ET_NO_DIRECTORY;
1521         return 0;
1522 }
1523
1524 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
1525 {
1526         ext2_filsys fs = ctx->fs;
1527
1528         if (bool) {
1529                 fs->get_blocks = pass1_get_blocks;
1530                 fs->check_directory = pass1_check_directory;
1531                 fs->read_inode = pass1_read_inode;
1532                 fs->write_inode = pass1_write_inode;
1533                 ctx->stashed_ino = 0;
1534         } else {
1535                 fs->get_blocks = 0;
1536                 fs->check_directory = 0;
1537                 fs->read_inode = 0;
1538                 fs->write_inode = 0;
1539         }
1540 }
1541
1542