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