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