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