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