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