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