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