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