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