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