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