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