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