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