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