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