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