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