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