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