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