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 <string.h>
42 #include <time.h>
43 #ifdef HAVE_ERRNO_H
44 #include <errno.h>
45 #endif
46
47 #include "e2fsck.h"
48 #include <ext2fs/ext2_ext_attr.h>
49
50 #include "problem.h"
51
52 #ifdef NO_INLINE_FUNCS
53 #define _INLINE_
54 #else
55 #define _INLINE_ inline
56 #endif
57
58 static int process_block(ext2_filsys fs, blk_t *blocknr,
59 e2_blkcnt_t blockcnt, blk_t ref_blk,
60 int ref_offset, void *priv_data);
61 static int process_bad_block(ext2_filsys fs, blk_t *block_nr,
62 e2_blkcnt_t blockcnt, blk_t ref_blk,
63 int ref_offset, void *priv_data);
64 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
65 char *block_buf);
66 static void mark_table_blocks(e2fsck_t ctx);
67 static void alloc_bb_map(e2fsck_t ctx);
68 static void alloc_imagic_map(e2fsck_t ctx);
69 static void mark_inode_bad(e2fsck_t ctx, ino_t ino);
70 static void handle_fs_bad_blocks(e2fsck_t ctx);
71 static void process_inodes(e2fsck_t ctx, char *block_buf);
72 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
73 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
74 dgrp_t group, void * priv_data);
75 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
76 char *block_buf, int adjust_sign);
77 /* static char *describe_illegal_block(ext2_filsys fs, blk_t block); */
78
79 struct process_block_struct {
80 ext2_ino_t ino;
81 unsigned is_dir:1, is_reg:1, clear:1, suppress:1,
82 fragmented:1, compressed:1, bbcheck:1;
83 blk64_t num_blocks;
84 blk_t max_blocks;
85 e2_blkcnt_t last_block;
86 e2_blkcnt_t last_db_block;
87 int num_illegal_blocks;
88 blk_t previous_block;
89 struct ext2_inode *inode;
90 struct problem_context *pctx;
91 ext2fs_block_bitmap fs_meta_blocks;
92 e2fsck_t ctx;
93 };
94
95 struct process_inode_block {
96 ext2_ino_t ino;
97 struct ext2_inode inode;
98 };
99
100 struct scan_callback_struct {
101 e2fsck_t ctx;
102 char *block_buf;
103 };
104
105 /*
106 * For the inodes to process list.
107 */
108 static struct process_inode_block *inodes_to_process;
109 static int process_inode_count;
110
111 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
112 EXT2_MIN_BLOCK_LOG_SIZE + 1];
113
114 /*
115 * Free all memory allocated by pass1 in preparation for restarting
116 * things.
117 */
unwind_pass1(ext2_filsys fs EXT2FS_ATTR ((unused)))118 static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
119 {
120 ext2fs_free_mem(&inodes_to_process);
121 inodes_to_process = 0;
122 }
123
124 /*
125 * Check to make sure a device inode is real. Returns 1 if the device
126 * checks out, 0 if not.
127 *
128 * Note: this routine is now also used to check FIFO's and Sockets,
129 * since they have the same requirement; the i_block fields should be
130 * zero.
131 */
e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR ((unused)),struct ext2_inode * inode)132 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
133 struct ext2_inode *inode)
134 {
135 int i;
136
137 /*
138 * If the index flag is set, then this is a bogus
139 * device/fifo/socket
140 */
141 if (inode->i_flags & EXT2_INDEX_FL)
142 return 0;
143
144 /*
145 * We should be able to do the test below all the time, but
146 * because the kernel doesn't forcibly clear the device
147 * inode's additional i_block fields, there are some rare
148 * occasions when a legitimate device inode will have non-zero
149 * additional i_block fields. So for now, we only complain
150 * when the immutable flag is set, which should never happen
151 * for devices. (And that's when the problem is caused, since
152 * you can't set or clear immutable flags for devices.) Once
153 * the kernel has been fixed we can change this...
154 */
155 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
156 for (i=4; i < EXT2_N_BLOCKS; i++)
157 if (inode->i_block[i])
158 return 0;
159 }
160 return 1;
161 }
162
163 /*
164 * Check to make sure a symlink inode is real. Returns 1 if the symlink
165 * checks out, 0 if not.
166 */
e2fsck_pass1_check_symlink(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,char * buf)167 int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
168 struct ext2_inode *inode, char *buf)
169 {
170 unsigned int len;
171 int i;
172 blk_t blocks;
173 ext2_extent_handle_t handle;
174 struct ext2_extent_info info;
175 struct ext2fs_extent extent;
176
177 if ((inode->i_size_high || inode->i_size == 0) ||
178 (inode->i_flags & EXT2_INDEX_FL))
179 return 0;
180
181 if (inode->i_flags & EXT4_EXTENTS_FL) {
182 if (inode->i_size > fs->blocksize)
183 return 0;
184 if (ext2fs_extent_open2(fs, ino, inode, &handle))
185 return 0;
186 i = 0;
187 if (ext2fs_extent_get_info(handle, &info) ||
188 (info.num_entries != 1) ||
189 (info.max_depth != 0))
190 goto exit_extent;
191 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) ||
192 (extent.e_lblk != 0) ||
193 (extent.e_len != 1) ||
194 (extent.e_pblk < fs->super->s_first_data_block) ||
195 (extent.e_pblk >= fs->super->s_blocks_count))
196 goto exit_extent;
197 i = 1;
198 exit_extent:
199 ext2fs_extent_free(handle);
200 return i;
201 }
202
203 blocks = ext2fs_inode_data_blocks(fs, inode);
204 if (blocks) {
205 if ((inode->i_size >= fs->blocksize) ||
206 (blocks != fs->blocksize >> 9) ||
207 (inode->i_block[0] < fs->super->s_first_data_block) ||
208 (inode->i_block[0] >= fs->super->s_blocks_count))
209 return 0;
210
211 for (i = 1; i < EXT2_N_BLOCKS; i++)
212 if (inode->i_block[i])
213 return 0;
214
215 if (io_channel_read_blk(fs->io, inode->i_block[0], 1, buf))
216 return 0;
217
218 len = strnlen(buf, fs->blocksize);
219 if (len == fs->blocksize)
220 return 0;
221 } else {
222 if (inode->i_size >= sizeof(inode->i_block))
223 return 0;
224
225 len = strnlen((char *)inode->i_block, sizeof(inode->i_block));
226 if (len == sizeof(inode->i_block))
227 return 0;
228 }
229 if (len != inode->i_size)
230 return 0;
231 return 1;
232 }
233
234 /*
235 * If the immutable (or append-only) flag is set on the inode, offer
236 * to clear it.
237 */
238 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
check_immutable(e2fsck_t ctx,struct problem_context * pctx)239 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
240 {
241 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
242 return;
243
244 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
245 return;
246
247 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
248 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
249 }
250
251 /*
252 * If device, fifo or socket, check size is zero -- if not offer to
253 * clear it
254 */
check_size(e2fsck_t ctx,struct problem_context * pctx)255 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
256 {
257 struct ext2_inode *inode = pctx->inode;
258
259 if ((inode->i_size == 0) && (inode->i_size_high == 0))
260 return;
261
262 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
263 return;
264
265 inode->i_size = 0;
266 inode->i_size_high = 0;
267 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
268 }
269
check_ea_in_inode(e2fsck_t ctx,struct problem_context * pctx)270 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
271 {
272 struct ext2_super_block *sb = ctx->fs->super;
273 struct ext2_inode_large *inode;
274 struct ext2_ext_attr_entry *entry;
275 char *start, *end;
276 unsigned int storage_size, remain;
277 int problem = 0;
278
279 inode = (struct ext2_inode_large *) pctx->inode;
280 storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
281 inode->i_extra_isize;
282 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
283 inode->i_extra_isize + sizeof(__u32);
284 end = (char *) inode + EXT2_INODE_SIZE(ctx->fs->super);
285 entry = (struct ext2_ext_attr_entry *) start;
286
287 /* scan all entry's headers first */
288
289 /* take finish entry 0UL into account */
290 remain = storage_size - sizeof(__u32);
291
292 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
293 __u32 hash;
294
295 /* header eats this space */
296 remain -= sizeof(struct ext2_ext_attr_entry);
297
298 /* is attribute name valid? */
299 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
300 pctx->num = entry->e_name_len;
301 problem = PR_1_ATTR_NAME_LEN;
302 goto fix;
303 }
304
305 /* attribute len eats this space */
306 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
307
308 /* check value size */
309 if (entry->e_value_size == 0 || entry->e_value_size > remain) {
310 pctx->num = entry->e_value_size;
311 problem = PR_1_ATTR_VALUE_SIZE;
312 goto fix;
313 }
314
315 /* e_value_block must be 0 in inode's ea */
316 if (entry->e_value_block != 0) {
317 pctx->num = entry->e_value_block;
318 problem = PR_1_ATTR_VALUE_BLOCK;
319 goto fix;
320 }
321
322 hash = ext2fs_ext_attr_hash_entry(entry,
323 start + entry->e_value_offs);
324
325 /* e_hash may be 0 in older inode's ea */
326 if (entry->e_hash != 0 && entry->e_hash != hash) {
327 pctx->num = entry->e_hash;
328 problem = PR_1_ATTR_HASH;
329 goto fix;
330 }
331
332 remain -= entry->e_value_size;
333
334 entry = EXT2_EXT_ATTR_NEXT(entry);
335 }
336 fix:
337 /*
338 * it seems like a corruption. it's very unlikely we could repair
339 * EA(s) in automatic fashion -bzzz
340 */
341 if (problem == 0 || !fix_problem(ctx, problem, pctx))
342 return;
343
344 /* simply remove all possible EA(s) */
345 *((__u32 *)start) = 0UL;
346 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
347 EXT2_INODE_SIZE(sb), "pass1");
348 }
349
check_inode_extra_space(e2fsck_t ctx,struct problem_context * pctx)350 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
351 {
352 struct ext2_super_block *sb = ctx->fs->super;
353 struct ext2_inode_large *inode;
354 __u32 *eamagic;
355 int min, max;
356
357 inode = (struct ext2_inode_large *) pctx->inode;
358 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
359 /* this isn't large inode. so, nothing to check */
360 return;
361 }
362
363 #if 0
364 printf("inode #%u, i_extra_size %d\n", pctx->ino,
365 inode->i_extra_isize);
366 #endif
367 /* i_extra_isize must cover i_extra_isize + i_pad1 at least */
368 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_pad1);
369 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
370 /*
371 * For now we will allow i_extra_isize to be 0, but really
372 * implementations should never allow i_extra_isize to be 0
373 */
374 if (inode->i_extra_isize &&
375 (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
376 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
377 return;
378 inode->i_extra_isize = min;
379 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
380 EXT2_INODE_SIZE(sb), "pass1");
381 return;
382 }
383
384 eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
385 inode->i_extra_isize);
386 if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
387 /* it seems inode has an extended attribute(s) in body */
388 check_ea_in_inode(ctx, pctx);
389 }
390 }
391
392 /*
393 * Check to see if the inode might really be a directory, despite i_mode
394 *
395 * This is a lot of complexity for something for which I'm not really
396 * convinced happens frequently in the wild. If for any reason this
397 * causes any problems, take this code out.
398 * [tytso:20070331.0827EDT]
399 */
check_is_really_dir(e2fsck_t ctx,struct problem_context * pctx,char * buf)400 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
401 char *buf)
402 {
403 struct ext2_inode *inode = pctx->inode;
404 struct ext2_dir_entry *dirent;
405 const char *old_op;
406 errcode_t retval;
407 blk_t blk;
408 blk64_t first_dir_blk;
409 unsigned int i, rec_len, not_device = 0;
410 int extent_fs;
411
412 /*
413 * If the mode looks OK, we believe it. If the first block in
414 * the i_block array is 0, this cannot be a directory. If the
415 * inode is extent-mapped, it is still the case that the latter
416 * cannot be 0 - the magic number in the extent header would make
417 * it nonzero.
418 */
419 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
420 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
421 return;
422
423 /*
424 * Check the block numbers in the i_block array for validity:
425 * zero blocks are skipped (but the first one cannot be zero -
426 * see above), other blocks are checked against the first and
427 * max data blocks (from the the superblock) and against the
428 * block bitmap. Any invalid block found means this cannot be
429 * a directory.
430 *
431 * If there are non-zero blocks past the fourth entry, then
432 * this cannot be a device file: we remember that for the next
433 * check.
434 *
435 * For extent mapped files, we don't do any sanity checking:
436 * just try to get the phys block of logical block 0 and run
437 * with it.
438 */
439
440 extent_fs = (ctx->fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
441 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
442 /* extent mapped */
443 if (ext2fs_bmap(ctx->fs, pctx->ino, inode, 0, 0, 0,
444 &blk))
445 return;
446 /* device files are never extent mapped */
447 not_device++;
448 } else {
449 for (i=0; i < EXT2_N_BLOCKS; i++) {
450 blk = inode->i_block[i];
451 if (!blk)
452 continue;
453 if (i >= 4)
454 not_device++;
455
456 if (blk < ctx->fs->super->s_first_data_block ||
457 blk >= ctx->fs->super->s_blocks_count ||
458 ext2fs_fast_test_block_bitmap(ctx->block_found_map,
459 blk))
460 return; /* Invalid block, can't be dir */
461 }
462 blk = inode->i_block[0];
463 }
464
465 /*
466 * If the mode says this is a device file and the i_links_count field
467 * is sane and we have not ruled it out as a device file previously,
468 * we declare it a device file, not a directory.
469 */
470 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
471 (inode->i_links_count == 1) && !not_device)
472 return;
473
474 /* read the first block */
475 old_op = ehandler_operation(_("reading directory block"));
476 retval = ext2fs_read_dir_block(ctx->fs, blk, buf);
477 ehandler_operation(0);
478 if (retval)
479 return;
480
481 dirent = (struct ext2_dir_entry *) buf;
482 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
483 if (retval)
484 return;
485 if (((dirent->name_len & 0xFF) != 1) ||
486 (dirent->name[0] != '.') ||
487 (dirent->inode != pctx->ino) ||
488 (rec_len < 12) ||
489 (rec_len % 4) ||
490 (rec_len >= ctx->fs->blocksize - 12))
491 return;
492
493 dirent = (struct ext2_dir_entry *) (buf + rec_len);
494 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
495 if (retval)
496 return;
497 if (((dirent->name_len & 0xFF) != 2) ||
498 (dirent->name[0] != '.') ||
499 (dirent->name[1] != '.') ||
500 (rec_len < 12) ||
501 (rec_len % 4))
502 return;
503
504 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
505 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
506 e2fsck_write_inode_full(ctx, pctx->ino, inode,
507 EXT2_INODE_SIZE(ctx->fs->super),
508 "check_is_really_dir");
509 }
510 }
511
e2fsck_setup_tdb_icount(e2fsck_t ctx,int flags,ext2_icount_t * ret)512 extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
513 ext2_icount_t *ret)
514 {
515 unsigned int threshold;
516 ext2_ino_t num_dirs;
517 errcode_t retval;
518 char *tdb_dir;
519 int enable;
520
521 *ret = 0;
522
523 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
524 &tdb_dir);
525 profile_get_uint(ctx->profile, "scratch_files",
526 "numdirs_threshold", 0, 0, &threshold);
527 profile_get_boolean(ctx->profile, "scratch_files",
528 "icount", 0, 1, &enable);
529
530 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
531 if (retval)
532 num_dirs = 1024; /* Guess */
533
534 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
535 (threshold && num_dirs <= threshold))
536 return;
537
538 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
539 if (retval)
540 *ret = 0;
541 }
542
e2fsck_pass1(e2fsck_t ctx)543 void e2fsck_pass1(e2fsck_t ctx)
544 {
545 int i;
546 __u64 max_sizes;
547 ext2_filsys fs = ctx->fs;
548 ext2_ino_t ino;
549 struct ext2_inode *inode;
550 ext2_inode_scan scan;
551 char *block_buf;
552 #ifdef RESOURCE_TRACK
553 struct resource_track rtrack;
554 #endif
555 unsigned char frag, fsize;
556 struct problem_context pctx;
557 struct scan_callback_struct scan_struct;
558 struct ext2_super_block *sb = ctx->fs->super;
559 const char *old_op;
560 int imagic_fs, extent_fs;
561 int busted_fs_time = 0;
562 int inode_size;
563
564 init_resource_track(&rtrack, ctx->fs->io);
565 clear_problem_context(&pctx);
566
567 if (!(ctx->options & E2F_OPT_PREEN))
568 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
569
570 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
571 !(ctx->options & E2F_OPT_NO)) {
572 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
573 ctx->dirs_to_hash = 0;
574 }
575
576 #ifdef MTRACE
577 mtrace_print("Pass 1");
578 #endif
579
580 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
581
582 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
583 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
584 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
585 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
586 max_sizes = (max_sizes * (1UL << i)) - 1;
587 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
588 }
589 #undef EXT2_BPP
590
591 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
592 extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
593
594 /*
595 * Allocate bitmaps structures
596 */
597 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
598 &ctx->inode_used_map);
599 if (pctx.errcode) {
600 pctx.num = 1;
601 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
602 ctx->flags |= E2F_FLAG_ABORT;
603 return;
604 }
605 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
606 _("directory inode map"), &ctx->inode_dir_map);
607 if (pctx.errcode) {
608 pctx.num = 2;
609 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
610 ctx->flags |= E2F_FLAG_ABORT;
611 return;
612 }
613 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
614 _("regular file inode map"), &ctx->inode_reg_map);
615 if (pctx.errcode) {
616 pctx.num = 6;
617 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
618 ctx->flags |= E2F_FLAG_ABORT;
619 return;
620 }
621 pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
622 &ctx->block_found_map);
623 if (pctx.errcode) {
624 pctx.num = 1;
625 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
626 ctx->flags |= E2F_FLAG_ABORT;
627 return;
628 }
629 e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
630 if (!ctx->inode_link_info)
631 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
632 &ctx->inode_link_info);
633 if (pctx.errcode) {
634 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
635 ctx->flags |= E2F_FLAG_ABORT;
636 return;
637 }
638 inode_size = EXT2_INODE_SIZE(fs->super);
639 inode = (struct ext2_inode *)
640 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
641
642 inodes_to_process = (struct process_inode_block *)
643 e2fsck_allocate_memory(ctx,
644 (ctx->process_inode_size *
645 sizeof(struct process_inode_block)),
646 "array of inodes to process");
647 process_inode_count = 0;
648
649 pctx.errcode = ext2fs_init_dblist(fs, 0);
650 if (pctx.errcode) {
651 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
652 ctx->flags |= E2F_FLAG_ABORT;
653 ext2fs_free_mem(&inode);
654 return;
655 }
656
657 /*
658 * If the last orphan field is set, clear it, since the pass1
659 * processing will automatically find and clear the orphans.
660 * In the future, we may want to try using the last_orphan
661 * linked list ourselves, but for now, we clear it so that the
662 * ext3 mount code won't get confused.
663 */
664 if (!(ctx->options & E2F_OPT_READONLY)) {
665 if (fs->super->s_last_orphan) {
666 fs->super->s_last_orphan = 0;
667 ext2fs_mark_super_dirty(fs);
668 }
669 }
670
671 mark_table_blocks(ctx);
672 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
673 "block interate buffer");
674 e2fsck_use_inode_shortcuts(ctx, 1);
675 old_op = ehandler_operation(_("opening inode scan"));
676 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
677 &scan);
678 ehandler_operation(old_op);
679 if (pctx.errcode) {
680 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
681 ctx->flags |= E2F_FLAG_ABORT;
682 ext2fs_free_mem(&block_buf);
683 ext2fs_free_mem(&inode);
684 return;
685 }
686 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
687 ctx->stashed_inode = inode;
688 scan_struct.ctx = ctx;
689 scan_struct.block_buf = block_buf;
690 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
691 if (ctx->progress)
692 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
693 return;
694 if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
695 (fs->super->s_mtime < fs->super->s_inodes_count))
696 busted_fs_time = 1;
697
698 while (1) {
699 old_op = ehandler_operation(_("getting next inode from scan"));
700 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
701 inode, inode_size);
702 ehandler_operation(old_op);
703 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
704 return;
705 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
706 if (!ctx->inode_bb_map)
707 alloc_bb_map(ctx);
708 ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
709 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
710 continue;
711 }
712 if (pctx.errcode) {
713 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
714 ctx->flags |= E2F_FLAG_ABORT;
715 return;
716 }
717 if (!ino)
718 break;
719 pctx.ino = ino;
720 pctx.inode = inode;
721 ctx->stashed_ino = ino;
722 if (inode->i_links_count) {
723 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
724 ino, inode->i_links_count);
725 if (pctx.errcode) {
726 pctx.num = inode->i_links_count;
727 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
728 ctx->flags |= E2F_FLAG_ABORT;
729 return;
730 }
731 }
732
733 /*
734 * Test for incorrect extent flag settings.
735 *
736 * On big-endian machines we must be careful:
737 * When the inode is read, the i_block array is not swapped
738 * if the extent flag is set. Therefore if we are testing
739 * for or fixing a wrongly-set flag, we must potentially
740 * (un)swap before testing, or after fixing.
741 */
742
743 /*
744 * In this case the extents flag was set when read, so
745 * extent_header_verify is ok. If the inode is cleared,
746 * no need to swap... so no extra swapping here.
747 */
748 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
749 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
750 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
751 if ((ext2fs_extent_header_verify(inode->i_block,
752 sizeof(inode->i_block)) == 0) &&
753 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
754 sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS;
755 ext2fs_mark_super_dirty(fs);
756 extent_fs = 1;
757 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
758 clear_inode:
759 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
760 if (ino == EXT2_BAD_INO)
761 ext2fs_mark_inode_bitmap(ctx->inode_used_map,
762 ino);
763 continue;
764 }
765 }
766
767 /*
768 * For big-endian machines:
769 * If the inode didn't have the extents flag set when it
770 * was read, then the i_blocks array was swapped. To test
771 * as an extents header, we must swap it back first.
772 * IF we then set the extents flag, the entire i_block
773 * array must be un/re-swapped to make it proper extents data.
774 */
775 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
776 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
777 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
778 (LINUX_S_ISREG(inode->i_mode) ||
779 LINUX_S_ISDIR(inode->i_mode))) {
780 void *ehp;
781 #ifdef WORDS_BIGENDIAN
782 __u32 tmp_block[EXT2_N_BLOCKS];
783
784 for (i = 0; i < EXT2_N_BLOCKS; i++)
785 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
786 ehp = tmp_block;
787 #else
788 ehp = inode->i_block;
789 #endif
790 if ((ext2fs_extent_header_verify(ehp,
791 sizeof(inode->i_block)) == 0) &&
792 (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
793 inode->i_flags |= EXT4_EXTENTS_FL;
794 #ifdef WORDS_BIGENDIAN
795 memcpy(inode->i_block, tmp_block,
796 sizeof(inode->i_block));
797 #endif
798 e2fsck_write_inode(ctx, ino, inode, "pass1");
799 }
800 }
801
802 if (ino == EXT2_BAD_INO) {
803 struct process_block_struct pb;
804
805 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
806 &pb.fs_meta_blocks);
807 if (pctx.errcode) {
808 pctx.num = 4;
809 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
810 ctx->flags |= E2F_FLAG_ABORT;
811 return;
812 }
813 pb.ino = EXT2_BAD_INO;
814 pb.num_blocks = pb.last_block = 0;
815 pb.last_db_block = -1;
816 pb.num_illegal_blocks = 0;
817 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
818 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
819 pb.inode = inode;
820 pb.pctx = &pctx;
821 pb.ctx = ctx;
822 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0,
823 block_buf, process_bad_block, &pb);
824 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
825 if (pctx.errcode) {
826 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
827 ctx->flags |= E2F_FLAG_ABORT;
828 return;
829 }
830 if (pb.bbcheck)
831 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
832 ctx->flags |= E2F_FLAG_ABORT;
833 return;
834 }
835 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
836 clear_problem_context(&pctx);
837 continue;
838 } else if (ino == EXT2_ROOT_INO) {
839 /*
840 * Make sure the root inode is a directory; if
841 * not, offer to clear it. It will be
842 * regnerated in pass #3.
843 */
844 if (!LINUX_S_ISDIR(inode->i_mode)) {
845 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
846 goto clear_inode;
847 }
848 /*
849 * If dtime is set, offer to clear it. mke2fs
850 * version 0.2b created filesystems with the
851 * dtime field set for the root and lost+found
852 * directories. We won't worry about
853 * /lost+found, since that can be regenerated
854 * easily. But we will fix the root directory
855 * as a special case.
856 */
857 if (inode->i_dtime && inode->i_links_count) {
858 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
859 inode->i_dtime = 0;
860 e2fsck_write_inode(ctx, ino, inode,
861 "pass1");
862 }
863 }
864 } else if (ino == EXT2_JOURNAL_INO) {
865 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
866 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
867 if (!LINUX_S_ISREG(inode->i_mode) &&
868 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
869 &pctx)) {
870 inode->i_mode = LINUX_S_IFREG;
871 e2fsck_write_inode(ctx, ino, inode,
872 "pass1");
873 }
874 check_blocks(ctx, &pctx, block_buf);
875 continue;
876 }
877 if ((inode->i_links_count || inode->i_blocks ||
878 inode->i_blocks || inode->i_block[0]) &&
879 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
880 &pctx)) {
881 memset(inode, 0, inode_size);
882 ext2fs_icount_store(ctx->inode_link_info,
883 ino, 0);
884 e2fsck_write_inode_full(ctx, ino, inode,
885 inode_size, "pass1");
886 }
887 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
888 int problem = 0;
889
890 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
891 if (ino == EXT2_BOOT_LOADER_INO) {
892 if (LINUX_S_ISDIR(inode->i_mode))
893 problem = PR_1_RESERVED_BAD_MODE;
894 } else if (ino == EXT2_RESIZE_INO) {
895 if (inode->i_mode &&
896 !LINUX_S_ISREG(inode->i_mode))
897 problem = PR_1_RESERVED_BAD_MODE;
898 } else {
899 if (inode->i_mode != 0)
900 problem = PR_1_RESERVED_BAD_MODE;
901 }
902 if (problem) {
903 if (fix_problem(ctx, problem, &pctx)) {
904 inode->i_mode = 0;
905 e2fsck_write_inode(ctx, ino, inode,
906 "pass1");
907 }
908 }
909 check_blocks(ctx, &pctx, block_buf);
910 continue;
911 }
912 /*
913 * Check for inodes who might have been part of the
914 * orphaned list linked list. They should have gotten
915 * dealt with by now, unless the list had somehow been
916 * corrupted.
917 *
918 * FIXME: In the future, inodes which are still in use
919 * (and which are therefore) pending truncation should
920 * be handled specially. Right now we just clear the
921 * dtime field, and the normal e2fsck handling of
922 * inodes where i_size and the inode blocks are
923 * inconsistent is to fix i_size, instead of releasing
924 * the extra blocks. This won't catch the inodes that
925 * was at the end of the orphan list, but it's better
926 * than nothing. The right answer is that there
927 * shouldn't be any bugs in the orphan list handling. :-)
928 */
929 if (inode->i_dtime && !busted_fs_time &&
930 inode->i_dtime < ctx->fs->super->s_inodes_count) {
931 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
932 inode->i_dtime = inode->i_links_count ?
933 0 : ctx->now;
934 e2fsck_write_inode(ctx, ino, inode,
935 "pass1");
936 }
937 }
938
939 /*
940 * This code assumes that deleted inodes have
941 * i_links_count set to 0.
942 */
943 if (!inode->i_links_count) {
944 if (!inode->i_dtime && inode->i_mode) {
945 if (fix_problem(ctx,
946 PR_1_ZERO_DTIME, &pctx)) {
947 inode->i_dtime = ctx->now;
948 e2fsck_write_inode(ctx, ino, inode,
949 "pass1");
950 }
951 }
952 continue;
953 }
954 /*
955 * n.b. 0.3c ext2fs code didn't clear i_links_count for
956 * deleted files. Oops.
957 *
958 * Since all new ext2 implementations get this right,
959 * we now assume that the case of non-zero
960 * i_links_count and non-zero dtime means that we
961 * should keep the file, not delete it.
962 *
963 */
964 if (inode->i_dtime) {
965 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
966 inode->i_dtime = 0;
967 e2fsck_write_inode(ctx, ino, inode, "pass1");
968 }
969 }
970
971 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
972 switch (fs->super->s_creator_os) {
973 case EXT2_OS_HURD:
974 frag = inode->osd2.hurd2.h_i_frag;
975 fsize = inode->osd2.hurd2.h_i_fsize;
976 break;
977 default:
978 frag = fsize = 0;
979 }
980
981 if (inode->i_faddr || frag || fsize ||
982 (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
983 mark_inode_bad(ctx, ino);
984 if (!(fs->super->s_feature_incompat &
985 EXT4_FEATURE_INCOMPAT_64BIT) &&
986 inode->osd2.linux2.l_i_file_acl_high != 0)
987 mark_inode_bad(ctx, ino);
988 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
989 !(fs->super->s_feature_ro_compat &
990 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
991 (inode->osd2.linux2.l_i_blocks_hi != 0))
992 mark_inode_bad(ctx, ino);
993 if (inode->i_flags & EXT2_IMAGIC_FL) {
994 if (imagic_fs) {
995 if (!ctx->inode_imagic_map)
996 alloc_imagic_map(ctx);
997 ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
998 ino);
999 } else {
1000 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1001 inode->i_flags &= ~EXT2_IMAGIC_FL;
1002 e2fsck_write_inode(ctx, ino,
1003 inode, "pass1");
1004 }
1005 }
1006 }
1007
1008 check_inode_extra_space(ctx, &pctx);
1009 check_is_really_dir(ctx, &pctx, block_buf);
1010
1011 /*
1012 * ext2fs_inode_has_valid_blocks does not actually look
1013 * at i_block[] values, so not endian-sensitive here.
1014 */
1015 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1016 LINUX_S_ISLNK(inode->i_mode) &&
1017 !ext2fs_inode_has_valid_blocks(inode) &&
1018 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1019 inode->i_flags &= ~EXT4_EXTENTS_FL;
1020 e2fsck_write_inode(ctx, ino, inode, "pass1");
1021 }
1022
1023 if (LINUX_S_ISDIR(inode->i_mode)) {
1024 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
1025 e2fsck_add_dir_info(ctx, ino, 0);
1026 ctx->fs_directory_count++;
1027 } else if (LINUX_S_ISREG (inode->i_mode)) {
1028 ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
1029 ctx->fs_regular_count++;
1030 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1031 e2fsck_pass1_check_device_inode(fs, inode)) {
1032 check_immutable(ctx, &pctx);
1033 check_size(ctx, &pctx);
1034 ctx->fs_chardev_count++;
1035 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1036 e2fsck_pass1_check_device_inode(fs, inode)) {
1037 check_immutable(ctx, &pctx);
1038 check_size(ctx, &pctx);
1039 ctx->fs_blockdev_count++;
1040 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1041 e2fsck_pass1_check_symlink(fs, ino, inode,
1042 block_buf)) {
1043 check_immutable(ctx, &pctx);
1044 ctx->fs_symlinks_count++;
1045 if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1046 ctx->fs_fast_symlinks_count++;
1047 check_blocks(ctx, &pctx, block_buf);
1048 continue;
1049 }
1050 }
1051 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1052 e2fsck_pass1_check_device_inode(fs, inode)) {
1053 check_immutable(ctx, &pctx);
1054 check_size(ctx, &pctx);
1055 ctx->fs_fifo_count++;
1056 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1057 e2fsck_pass1_check_device_inode(fs, inode)) {
1058 check_immutable(ctx, &pctx);
1059 check_size(ctx, &pctx);
1060 ctx->fs_sockets_count++;
1061 } else
1062 mark_inode_bad(ctx, ino);
1063 if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
1064 if (inode->i_block[EXT2_IND_BLOCK])
1065 ctx->fs_ind_count++;
1066 if (inode->i_block[EXT2_DIND_BLOCK])
1067 ctx->fs_dind_count++;
1068 if (inode->i_block[EXT2_TIND_BLOCK])
1069 ctx->fs_tind_count++;
1070 }
1071 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1072 (inode->i_block[EXT2_IND_BLOCK] ||
1073 inode->i_block[EXT2_DIND_BLOCK] ||
1074 inode->i_block[EXT2_TIND_BLOCK] ||
1075 inode->i_file_acl)) {
1076 inodes_to_process[process_inode_count].ino = ino;
1077 inodes_to_process[process_inode_count].inode = *inode;
1078 process_inode_count++;
1079 } else
1080 check_blocks(ctx, &pctx, block_buf);
1081
1082 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1083 return;
1084
1085 if (process_inode_count >= ctx->process_inode_size) {
1086 process_inodes(ctx, block_buf);
1087
1088 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1089 return;
1090 }
1091 }
1092 process_inodes(ctx, block_buf);
1093 ext2fs_close_inode_scan(scan);
1094
1095 /*
1096 * If any extended attribute blocks' reference counts need to
1097 * be adjusted, either up (ctx->refcount_extra), or down
1098 * (ctx->refcount), then fix them.
1099 */
1100 if (ctx->refcount) {
1101 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1102 ea_refcount_free(ctx->refcount);
1103 ctx->refcount = 0;
1104 }
1105 if (ctx->refcount_extra) {
1106 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1107 block_buf, +1);
1108 ea_refcount_free(ctx->refcount_extra);
1109 ctx->refcount_extra = 0;
1110 }
1111
1112 if (ctx->invalid_bitmaps)
1113 handle_fs_bad_blocks(ctx);
1114
1115 /* We don't need the block_ea_map any more */
1116 if (ctx->block_ea_map) {
1117 ext2fs_free_block_bitmap(ctx->block_ea_map);
1118 ctx->block_ea_map = 0;
1119 }
1120
1121 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1122 ext2fs_block_bitmap save_bmap;
1123
1124 save_bmap = fs->block_map;
1125 fs->block_map = ctx->block_found_map;
1126 clear_problem_context(&pctx);
1127 pctx.errcode = ext2fs_create_resize_inode(fs);
1128 if (pctx.errcode) {
1129 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1130 &pctx)) {
1131 ctx->flags |= E2F_FLAG_ABORT;
1132 return;
1133 }
1134 pctx.errcode = 0;
1135 }
1136 if (!pctx.errcode) {
1137 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1138 "recreate inode");
1139 inode->i_mtime = ctx->now;
1140 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1141 "recreate inode");
1142 }
1143 fs->block_map = save_bmap;
1144 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1145 }
1146
1147 if (ctx->flags & E2F_FLAG_RESTART) {
1148 /*
1149 * Only the master copy of the superblock and block
1150 * group descriptors are going to be written during a
1151 * restart, so set the superblock to be used to be the
1152 * master superblock.
1153 */
1154 ctx->use_superblock = 0;
1155 unwind_pass1(fs);
1156 goto endit;
1157 }
1158
1159 if (ctx->block_dup_map) {
1160 if (ctx->options & E2F_OPT_PREEN) {
1161 clear_problem_context(&pctx);
1162 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1163 }
1164 e2fsck_pass1_dupblocks(ctx, block_buf);
1165 }
1166 ext2fs_free_mem(&inodes_to_process);
1167 endit:
1168 e2fsck_use_inode_shortcuts(ctx, 0);
1169
1170 ext2fs_free_mem(&block_buf);
1171 ext2fs_free_mem(&inode);
1172
1173 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
1174 }
1175
1176 /*
1177 * When the inode_scan routines call this callback at the end of the
1178 * glock group, call process_inodes.
1179 */
scan_callback(ext2_filsys fs,ext2_inode_scan scan EXT2FS_ATTR ((unused)),dgrp_t group,void * priv_data)1180 static errcode_t scan_callback(ext2_filsys fs,
1181 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1182 dgrp_t group, void * priv_data)
1183 {
1184 struct scan_callback_struct *scan_struct;
1185 e2fsck_t ctx;
1186
1187 scan_struct = (struct scan_callback_struct *) priv_data;
1188 ctx = scan_struct->ctx;
1189
1190 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1191
1192 if (ctx->progress)
1193 if ((ctx->progress)(ctx, 1, group+1,
1194 ctx->fs->group_desc_count))
1195 return EXT2_ET_CANCEL_REQUESTED;
1196
1197 return 0;
1198 }
1199
1200 /*
1201 * Process the inodes in the "inodes to process" list.
1202 */
process_inodes(e2fsck_t ctx,char * block_buf)1203 static void process_inodes(e2fsck_t ctx, char *block_buf)
1204 {
1205 int i;
1206 struct ext2_inode *old_stashed_inode;
1207 ext2_ino_t old_stashed_ino;
1208 const char *old_operation;
1209 char buf[80];
1210 struct problem_context pctx;
1211
1212 #if 0
1213 printf("begin process_inodes: ");
1214 #endif
1215 if (process_inode_count == 0)
1216 return;
1217 old_operation = ehandler_operation(0);
1218 old_stashed_inode = ctx->stashed_inode;
1219 old_stashed_ino = ctx->stashed_ino;
1220 qsort(inodes_to_process, process_inode_count,
1221 sizeof(struct process_inode_block), process_inode_cmp);
1222 clear_problem_context(&pctx);
1223 for (i=0; i < process_inode_count; i++) {
1224 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1225 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1226
1227 #if 0
1228 printf("%u ", pctx.ino);
1229 #endif
1230 sprintf(buf, _("reading indirect blocks of inode %u"),
1231 pctx.ino);
1232 ehandler_operation(buf);
1233 check_blocks(ctx, &pctx, block_buf);
1234 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1235 break;
1236 }
1237 ctx->stashed_inode = old_stashed_inode;
1238 ctx->stashed_ino = old_stashed_ino;
1239 process_inode_count = 0;
1240 #if 0
1241 printf("end process inodes\n");
1242 #endif
1243 ehandler_operation(old_operation);
1244 }
1245
process_inode_cmp(const void * a,const void * b)1246 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1247 {
1248 const struct process_inode_block *ib_a =
1249 (const struct process_inode_block *) a;
1250 const struct process_inode_block *ib_b =
1251 (const struct process_inode_block *) b;
1252 int ret;
1253
1254 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1255 ib_b->inode.i_block[EXT2_IND_BLOCK]);
1256 if (ret == 0)
1257 ret = ib_a->inode.i_file_acl - ib_b->inode.i_file_acl;
1258 if (ret == 0)
1259 ret = ib_a->ino - ib_b->ino;
1260 return ret;
1261 }
1262
1263 /*
1264 * Mark an inode as being bad in some what
1265 */
mark_inode_bad(e2fsck_t ctx,ino_t ino)1266 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
1267 {
1268 struct problem_context pctx;
1269
1270 if (!ctx->inode_bad_map) {
1271 clear_problem_context(&pctx);
1272
1273 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1274 _("bad inode map"), &ctx->inode_bad_map);
1275 if (pctx.errcode) {
1276 pctx.num = 3;
1277 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1278 /* Should never get here */
1279 ctx->flags |= E2F_FLAG_ABORT;
1280 return;
1281 }
1282 }
1283 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
1284 }
1285
1286
1287 /*
1288 * This procedure will allocate the inode "bb" (badblock) map table
1289 */
alloc_bb_map(e2fsck_t ctx)1290 static void alloc_bb_map(e2fsck_t ctx)
1291 {
1292 struct problem_context pctx;
1293
1294 clear_problem_context(&pctx);
1295 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1296 _("inode in bad block map"),
1297 &ctx->inode_bb_map);
1298 if (pctx.errcode) {
1299 pctx.num = 4;
1300 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1301 /* Should never get here */
1302 ctx->flags |= E2F_FLAG_ABORT;
1303 return;
1304 }
1305 }
1306
1307 /*
1308 * This procedure will allocate the inode imagic table
1309 */
alloc_imagic_map(e2fsck_t ctx)1310 static void alloc_imagic_map(e2fsck_t ctx)
1311 {
1312 struct problem_context pctx;
1313
1314 clear_problem_context(&pctx);
1315 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1316 _("imagic inode map"),
1317 &ctx->inode_imagic_map);
1318 if (pctx.errcode) {
1319 pctx.num = 5;
1320 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1321 /* Should never get here */
1322 ctx->flags |= E2F_FLAG_ABORT;
1323 return;
1324 }
1325 }
1326
1327 /*
1328 * Marks a block as in use, setting the dup_map if it's been set
1329 * already. Called by process_block and process_bad_block.
1330 *
1331 * WARNING: Assumes checks have already been done to make sure block
1332 * is valid. This is true in both process_block and process_bad_block.
1333 */
mark_block_used(e2fsck_t ctx,blk_t block)1334 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
1335 {
1336 struct problem_context pctx;
1337
1338 clear_problem_context(&pctx);
1339
1340 if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
1341 if (!ctx->block_dup_map) {
1342 pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
1343 _("multiply claimed block map"),
1344 &ctx->block_dup_map);
1345 if (pctx.errcode) {
1346 pctx.num = 3;
1347 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1348 &pctx);
1349 /* Should never get here */
1350 ctx->flags |= E2F_FLAG_ABORT;
1351 return;
1352 }
1353 }
1354 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
1355 } else {
1356 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
1357 }
1358 }
1359
1360 /*
1361 * Adjust the extended attribute block's reference counts at the end
1362 * of pass 1, either by subtracting out references for EA blocks that
1363 * are still referenced in ctx->refcount, or by adding references for
1364 * EA blocks that had extra references as accounted for in
1365 * ctx->refcount_extra.
1366 */
adjust_extattr_refcount(e2fsck_t ctx,ext2_refcount_t refcount,char * block_buf,int adjust_sign)1367 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
1368 char *block_buf, int adjust_sign)
1369 {
1370 struct ext2_ext_attr_header *header;
1371 struct problem_context pctx;
1372 ext2_filsys fs = ctx->fs;
1373 blk_t blk;
1374 __u32 should_be;
1375 int count;
1376
1377 clear_problem_context(&pctx);
1378
1379 ea_refcount_intr_begin(refcount);
1380 while (1) {
1381 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1382 break;
1383 pctx.blk = blk;
1384 pctx.errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
1385 if (pctx.errcode) {
1386 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1387 return;
1388 }
1389 header = (struct ext2_ext_attr_header *) block_buf;
1390 pctx.blkcount = header->h_refcount;
1391 should_be = header->h_refcount + adjust_sign * count;
1392 pctx.num = should_be;
1393 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1394 header->h_refcount = should_be;
1395 pctx.errcode = ext2fs_write_ext_attr(fs, blk,
1396 block_buf);
1397 if (pctx.errcode) {
1398 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
1399 &pctx);
1400 continue;
1401 }
1402 }
1403 }
1404 }
1405
1406 /*
1407 * Handle processing the extended attribute blocks
1408 */
check_ext_attr(e2fsck_t ctx,struct problem_context * pctx,char * block_buf)1409 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1410 char *block_buf)
1411 {
1412 ext2_filsys fs = ctx->fs;
1413 ext2_ino_t ino = pctx->ino;
1414 struct ext2_inode *inode = pctx->inode;
1415 blk_t blk;
1416 char * end;
1417 struct ext2_ext_attr_header *header;
1418 struct ext2_ext_attr_entry *entry;
1419 int count;
1420 region_t region = 0;
1421
1422 blk = inode->i_file_acl;
1423 if (blk == 0)
1424 return 0;
1425
1426 /*
1427 * If the Extended attribute flag isn't set, then a non-zero
1428 * file acl means that the inode is corrupted.
1429 *
1430 * Or if the extended attribute block is an invalid block,
1431 * then the inode is also corrupted.
1432 */
1433 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1434 (blk < fs->super->s_first_data_block) ||
1435 (blk >= fs->super->s_blocks_count)) {
1436 mark_inode_bad(ctx, ino);
1437 return 0;
1438 }
1439
1440 /* If ea bitmap hasn't been allocated, create it */
1441 if (!ctx->block_ea_map) {
1442 pctx->errcode = ext2fs_allocate_block_bitmap(fs,
1443 _("ext attr block map"),
1444 &ctx->block_ea_map);
1445 if (pctx->errcode) {
1446 pctx->num = 2;
1447 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1448 ctx->flags |= E2F_FLAG_ABORT;
1449 return 0;
1450 }
1451 }
1452
1453 /* Create the EA refcount structure if necessary */
1454 if (!ctx->refcount) {
1455 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1456 if (pctx->errcode) {
1457 pctx->num = 1;
1458 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1459 ctx->flags |= E2F_FLAG_ABORT;
1460 return 0;
1461 }
1462 }
1463
1464 #if 0
1465 /* Debugging text */
1466 printf("Inode %u has EA block %u\n", ino, blk);
1467 #endif
1468
1469 /* Have we seen this EA block before? */
1470 if (ext2fs_fast_test_block_bitmap(ctx->block_ea_map, blk)) {
1471 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1472 return 1;
1473 /* Ooops, this EA was referenced more than it stated */
1474 if (!ctx->refcount_extra) {
1475 pctx->errcode = ea_refcount_create(0,
1476 &ctx->refcount_extra);
1477 if (pctx->errcode) {
1478 pctx->num = 2;
1479 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1480 ctx->flags |= E2F_FLAG_ABORT;
1481 return 0;
1482 }
1483 }
1484 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1485 return 1;
1486 }
1487
1488 /*
1489 * OK, we haven't seen this EA block yet. So we need to
1490 * validate it
1491 */
1492 pctx->blk = blk;
1493 pctx->errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
1494 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1495 goto clear_extattr;
1496 header = (struct ext2_ext_attr_header *) block_buf;
1497 pctx->blk = inode->i_file_acl;
1498 if (((ctx->ext_attr_ver == 1) &&
1499 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
1500 ((ctx->ext_attr_ver == 2) &&
1501 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
1502 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1503 goto clear_extattr;
1504 }
1505
1506 if (header->h_blocks != 1) {
1507 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1508 goto clear_extattr;
1509 }
1510
1511 region = region_create(0, fs->blocksize);
1512 if (!region) {
1513 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
1514 ctx->flags |= E2F_FLAG_ABORT;
1515 return 0;
1516 }
1517 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1518 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1519 goto clear_extattr;
1520 }
1521
1522 entry = (struct ext2_ext_attr_entry *)(header+1);
1523 end = block_buf + fs->blocksize;
1524 while ((char *)entry < end && *(__u32 *)entry) {
1525 __u32 hash;
1526
1527 if (region_allocate(region, (char *)entry - (char *)header,
1528 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1529 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1530 goto clear_extattr;
1531 break;
1532 }
1533 if ((ctx->ext_attr_ver == 1 &&
1534 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1535 (ctx->ext_attr_ver == 2 &&
1536 entry->e_name_index == 0)) {
1537 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
1538 goto clear_extattr;
1539 break;
1540 }
1541 if (entry->e_value_block != 0) {
1542 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1543 goto clear_extattr;
1544 }
1545 if (entry->e_value_offs + entry->e_value_size > fs->blocksize) {
1546 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1547 goto clear_extattr;
1548 break;
1549 }
1550 if (entry->e_value_size &&
1551 region_allocate(region, entry->e_value_offs,
1552 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
1553 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1554 goto clear_extattr;
1555 }
1556
1557 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
1558 entry->e_value_offs);
1559
1560 if (entry->e_hash != hash) {
1561 pctx->num = entry->e_hash;
1562 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
1563 goto clear_extattr;
1564 entry->e_hash = hash;
1565 }
1566
1567 entry = EXT2_EXT_ATTR_NEXT(entry);
1568 }
1569 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
1570 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1571 goto clear_extattr;
1572 }
1573 region_free(region);
1574
1575 count = header->h_refcount - 1;
1576 if (count)
1577 ea_refcount_store(ctx->refcount, blk, count);
1578 mark_block_used(ctx, blk);
1579 ext2fs_fast_mark_block_bitmap(ctx->block_ea_map, blk);
1580 return 1;
1581
1582 clear_extattr:
1583 if (region)
1584 region_free(region);
1585 inode->i_file_acl = 0;
1586 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
1587 return 0;
1588 }
1589
1590 /* Returns 1 if bad htree, 0 if OK */
handle_htree(e2fsck_t ctx,struct problem_context * pctx,ext2_ino_t ino,struct ext2_inode * inode,char * block_buf)1591 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
1592 ext2_ino_t ino, struct ext2_inode *inode,
1593 char *block_buf)
1594 {
1595 struct ext2_dx_root_info *root;
1596 ext2_filsys fs = ctx->fs;
1597 errcode_t retval;
1598 blk_t blk;
1599
1600 if ((!LINUX_S_ISDIR(inode->i_mode) &&
1601 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
1602 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
1603 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
1604 return 1;
1605
1606 pctx->errcode = ext2fs_bmap(fs, ino, inode, 0, 0, 0, &blk);
1607
1608 if ((pctx->errcode) ||
1609 (blk == 0) ||
1610 (blk < fs->super->s_first_data_block) ||
1611 (blk >= fs->super->s_blocks_count)) {
1612 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1613 return 1;
1614 else
1615 return 0;
1616 }
1617
1618 retval = io_channel_read_blk(fs->io, blk, 1, block_buf);
1619 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1620 return 1;
1621
1622 /* XXX should check that beginning matches a directory */
1623 root = (struct ext2_dx_root_info *) (block_buf + 24);
1624
1625 if ((root->reserved_zero || root->info_length < 8) &&
1626 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1627 return 1;
1628
1629 pctx->num = root->hash_version;
1630 if ((root->hash_version != EXT2_HASH_LEGACY) &&
1631 (root->hash_version != EXT2_HASH_HALF_MD4) &&
1632 (root->hash_version != EXT2_HASH_TEA) &&
1633 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
1634 return 1;
1635
1636 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
1637 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
1638 return 1;
1639
1640 pctx->num = root->indirect_levels;
1641 if ((root->indirect_levels > 1) &&
1642 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
1643 return 1;
1644
1645 return 0;
1646 }
1647
e2fsck_clear_inode(e2fsck_t ctx,ext2_ino_t ino,struct ext2_inode * inode,int restart_flag,const char * source)1648 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
1649 struct ext2_inode *inode, int restart_flag,
1650 const char *source)
1651 {
1652 inode->i_flags = 0;
1653 inode->i_links_count = 0;
1654 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1655 inode->i_dtime = ctx->now;
1656
1657 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1658 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1659 if (ctx->inode_reg_map)
1660 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1661 if (ctx->inode_bad_map)
1662 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
1663
1664 /*
1665 * If the inode was partially accounted for before processing
1666 * was aborted, we need to restart the pass 1 scan.
1667 */
1668 ctx->flags |= restart_flag;
1669
1670 e2fsck_write_inode(ctx, ino, inode, source);
1671 }
1672
scan_extent_node(e2fsck_t ctx,struct problem_context * pctx,struct process_block_struct * pb,blk64_t start_block,ext2_extent_handle_t ehandle)1673 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
1674 struct process_block_struct *pb,
1675 blk64_t start_block,
1676 ext2_extent_handle_t ehandle)
1677 {
1678 struct ext2fs_extent extent;
1679 blk_t blk;
1680 e2_blkcnt_t blockcnt;
1681 unsigned int i;
1682 int is_dir, is_leaf;
1683 errcode_t problem;
1684 struct ext2_extent_info info;
1685
1686 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
1687 if (pctx->errcode)
1688 return;
1689
1690 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
1691 &extent);
1692 while (!pctx->errcode && info.num_entries-- > 0) {
1693 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
1694 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
1695
1696 problem = 0;
1697 if (extent.e_pblk == 0 ||
1698 extent.e_pblk < ctx->fs->super->s_first_data_block ||
1699 extent.e_pblk >= ctx->fs->super->s_blocks_count)
1700 problem = PR_1_EXTENT_BAD_START_BLK;
1701 else if (extent.e_lblk < start_block)
1702 problem = PR_1_OUT_OF_ORDER_EXTENTS;
1703 else if (is_leaf &&
1704 (extent.e_pblk + extent.e_len) >
1705 ctx->fs->super->s_blocks_count)
1706 problem = PR_1_EXTENT_ENDS_BEYOND;
1707
1708 if (problem) {
1709 report_problem:
1710 pctx->blk = extent.e_pblk;
1711 pctx->blk2 = extent.e_lblk;
1712 pctx->num = extent.e_len;
1713 if (fix_problem(ctx, problem, pctx)) {
1714 e2fsck_read_bitmaps(ctx);
1715 pctx->errcode =
1716 ext2fs_extent_delete(ehandle, 0);
1717 if (pctx->errcode) {
1718 pctx->str = "ext2fs_extent_delete";
1719 return;
1720 }
1721 pctx->errcode = ext2fs_extent_get(ehandle,
1722 EXT2_EXTENT_CURRENT,
1723 &extent);
1724 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
1725 pctx->errcode = 0;
1726 break;
1727 }
1728 continue;
1729 }
1730 goto next;
1731 }
1732
1733 if (!is_leaf) {
1734 blk = extent.e_pblk;
1735 pctx->errcode = ext2fs_extent_get(ehandle,
1736 EXT2_EXTENT_DOWN, &extent);
1737 if (pctx->errcode) {
1738 pctx->str = "EXT2_EXTENT_DOWN";
1739 problem = PR_1_EXTENT_HEADER_INVALID;
1740 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
1741 goto report_problem;
1742 return;
1743 }
1744 scan_extent_node(ctx, pctx, pb, extent.e_lblk, ehandle);
1745 if (pctx->errcode)
1746 return;
1747 pctx->errcode = ext2fs_extent_get(ehandle,
1748 EXT2_EXTENT_UP, &extent);
1749 if (pctx->errcode) {
1750 pctx->str = "EXT2_EXTENT_UP";
1751 return;
1752 }
1753 mark_block_used(ctx, blk);
1754 pb->num_blocks++;
1755 goto next;
1756 }
1757
1758 if ((pb->previous_block != 0) &&
1759 (pb->previous_block+1 != extent.e_pblk)) {
1760 if (ctx->options & E2F_OPT_FRAGCHECK) {
1761 char type = '?';
1762
1763 if (pb->is_dir)
1764 type = 'd';
1765 else if (pb->is_reg)
1766 type = 'f';
1767
1768 printf(("%6lu(%c): expecting %6lu "
1769 "actual extent "
1770 "phys %6lu log %lu len %lu\n"),
1771 (unsigned long) pctx->ino, type,
1772 (unsigned long) pb->previous_block+1,
1773 (unsigned long) extent.e_pblk,
1774 (unsigned long) extent.e_lblk,
1775 (unsigned long) extent.e_len);
1776 }
1777 pb->fragmented = 1;
1778 }
1779 while (is_dir && ++pb->last_db_block < extent.e_lblk) {
1780 pctx->errcode = ext2fs_add_dir_block(ctx->fs->dblist,
1781 pb->ino, 0,
1782 pb->last_db_block);
1783 if (pctx->errcode) {
1784 pctx->blk = 0;
1785 pctx->num = pb->last_db_block;
1786 goto failed_add_dir_block;
1787 }
1788 }
1789 for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
1790 i < extent.e_len;
1791 blk++, blockcnt++, i++) {
1792 mark_block_used(ctx, blk);
1793
1794 if (is_dir) {
1795 pctx->errcode = ext2fs_add_dir_block(ctx->fs->dblist, pctx->ino, blk, blockcnt);
1796 if (pctx->errcode) {
1797 pctx->blk = blk;
1798 pctx->num = blockcnt;
1799 failed_add_dir_block:
1800 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1801 /* Should never get here */
1802 ctx->flags |= E2F_FLAG_ABORT;
1803 return;
1804 }
1805 }
1806 }
1807 if (is_dir && extent.e_len > 0)
1808 pb->last_db_block = blockcnt - 1;
1809 pb->num_blocks += extent.e_len;
1810 pb->previous_block = extent.e_pblk + extent.e_len - 1;
1811 start_block = pb->last_block = extent.e_lblk + extent.e_len - 1;
1812 next:
1813 pctx->errcode = ext2fs_extent_get(ehandle,
1814 EXT2_EXTENT_NEXT_SIB,
1815 &extent);
1816 }
1817 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
1818 pctx->errcode = 0;
1819 }
1820
check_blocks_extents(e2fsck_t ctx,struct problem_context * pctx,struct process_block_struct * pb)1821 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
1822 struct process_block_struct *pb)
1823 {
1824 struct ext2_extent_info info;
1825 struct ext2_inode *inode = pctx->inode;
1826 ext2_extent_handle_t ehandle;
1827 ext2_filsys fs = ctx->fs;
1828 ext2_ino_t ino = pctx->ino;
1829 errcode_t retval;
1830
1831 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
1832 if (pctx->errcode) {
1833 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
1834 e2fsck_clear_inode(ctx, ino, inode, 0,
1835 "check_blocks_extents");
1836 pctx->errcode = 0;
1837 return;
1838 }
1839
1840 retval = ext2fs_extent_get_info(ehandle, &info);
1841 if (retval == 0) {
1842 if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT)
1843 info.max_depth = MAX_EXTENT_DEPTH_COUNT-1;
1844 ctx->extent_depth_count[info.max_depth]++;
1845 }
1846
1847 scan_extent_node(ctx, pctx, pb, 0, ehandle);
1848 if (pctx->errcode &&
1849 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
1850 pb->num_blocks = 0;
1851 inode->i_blocks = 0;
1852 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
1853 "check_blocks_extents");
1854 pctx->errcode = 0;
1855 }
1856 ext2fs_extent_free(ehandle);
1857 }
1858
ext2fs_inode_i_blocks(ext2_filsys fs,struct ext2_inode * inode)1859 static blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
1860 struct ext2_inode *inode)
1861 {
1862 return (inode->i_blocks |
1863 (fs->super->s_feature_ro_compat &
1864 EXT4_FEATURE_RO_COMPAT_HUGE_FILE ?
1865 (__u64)inode->osd2.linux2.l_i_blocks_hi << 32 : 0));
1866 }
1867
1868 /*
1869 * This subroutine is called on each inode to account for all of the
1870 * blocks used by that inode.
1871 */
check_blocks(e2fsck_t ctx,struct problem_context * pctx,char * block_buf)1872 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
1873 char *block_buf)
1874 {
1875 ext2_filsys fs = ctx->fs;
1876 struct process_block_struct pb;
1877 ext2_ino_t ino = pctx->ino;
1878 struct ext2_inode *inode = pctx->inode;
1879 int bad_size = 0;
1880 int dirty_inode = 0;
1881 int extent_fs;
1882 __u64 size;
1883
1884 pb.ino = ino;
1885 pb.num_blocks = 0;
1886 pb.last_block = -1;
1887 pb.last_db_block = -1;
1888 pb.num_illegal_blocks = 0;
1889 pb.suppress = 0; pb.clear = 0;
1890 pb.fragmented = 0;
1891 pb.compressed = 0;
1892 pb.previous_block = 0;
1893 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1894 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
1895 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
1896 pb.inode = inode;
1897 pb.pctx = pctx;
1898 pb.ctx = ctx;
1899 pctx->ino = ino;
1900 pctx->errcode = 0;
1901
1902 extent_fs = (ctx->fs->super->s_feature_incompat &
1903 EXT3_FEATURE_INCOMPAT_EXTENTS);
1904
1905 if (inode->i_flags & EXT2_COMPRBLK_FL) {
1906 if (fs->super->s_feature_incompat &
1907 EXT2_FEATURE_INCOMPAT_COMPRESSION)
1908 pb.compressed = 1;
1909 else {
1910 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
1911 inode->i_flags &= ~EXT2_COMPRBLK_FL;
1912 dirty_inode++;
1913 }
1914 }
1915 }
1916
1917 if (inode->i_file_acl && check_ext_attr(ctx, pctx, block_buf)) {
1918 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1919 goto out;
1920 pb.num_blocks++;
1921 }
1922
1923 if (ext2fs_inode_has_valid_blocks(inode)) {
1924 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
1925 check_blocks_extents(ctx, pctx, &pb);
1926 else
1927 pctx->errcode = ext2fs_block_iterate2(fs, ino,
1928 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
1929 block_buf, process_block, &pb);
1930 }
1931 end_problem_latch(ctx, PR_LATCH_BLOCK);
1932 end_problem_latch(ctx, PR_LATCH_TOOBIG);
1933 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1934 goto out;
1935 if (pctx->errcode)
1936 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
1937
1938 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
1939 if (LINUX_S_ISDIR(inode->i_mode))
1940 ctx->fs_fragmented_dir++;
1941 else
1942 ctx->fs_fragmented++;
1943 }
1944
1945 if (pb.clear) {
1946 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
1947 "check_blocks");
1948 return;
1949 }
1950
1951 if (inode->i_flags & EXT2_INDEX_FL) {
1952 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
1953 inode->i_flags &= ~EXT2_INDEX_FL;
1954 dirty_inode++;
1955 } else {
1956 #ifdef ENABLE_HTREE
1957 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
1958 #endif
1959 }
1960 }
1961
1962 if (!pb.num_blocks && pb.is_dir) {
1963 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
1964 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
1965 ctx->fs_directory_count--;
1966 return;
1967 }
1968 }
1969
1970 if (!(fs->super->s_feature_ro_compat &
1971 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
1972 !(inode->i_flags & EXT4_HUGE_FILE_FL))
1973 pb.num_blocks *= (fs->blocksize / 512);
1974 #if 0
1975 printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
1976 ino, inode->i_size, pb.last_block, inode->i_blocks,
1977 pb.num_blocks);
1978 #endif
1979 if (pb.is_dir) {
1980 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
1981 if (inode->i_size & (fs->blocksize - 1))
1982 bad_size = 5;
1983 else if (nblock > (pb.last_block + 1))
1984 bad_size = 1;
1985 else if (nblock < (pb.last_block + 1)) {
1986 if (((pb.last_block + 1) - nblock) >
1987 fs->super->s_prealloc_dir_blocks)
1988 bad_size = 2;
1989 }
1990 } else {
1991 e2_blkcnt_t blkpg = ctx->blocks_per_page;
1992
1993 size = EXT2_I_SIZE(inode);
1994 if ((pb.last_block >= 0) &&
1995 /* allow allocated blocks to end of PAGE_SIZE */
1996 (size < (__u64)pb.last_block * fs->blocksize) &&
1997 (pb.last_block / blkpg * blkpg != pb.last_block ||
1998 size < (__u64)(pb.last_block & ~(blkpg-1)) *fs->blocksize) &&
1999 !(inode->i_flags & EXT4_EOFBLOCKS_FL))
2000 bad_size = 3;
2001 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2002 size > ext2_max_sizes[fs->super->s_log_block_size])
2003 /* too big for a direct/indirect-mapped file */
2004 bad_size = 4;
2005 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2006 size >
2007 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
2008 /* too big for an extent-based file - 32bit ee_block */
2009 bad_size = 6;
2010
2011 /*
2012 * Check to see if the EOFBLOCKS flag is set where it
2013 * doesn't need to be.
2014 */
2015 if ((inode->i_flags & EXT4_EOFBLOCKS_FL) &&
2016 (size >= (((__u64)pb.last_block + 1) * fs->blocksize))) {
2017 pctx->blkcount = pb.last_block;
2018 if (fix_problem(ctx, PR_1_EOFBLOCKS_FL_SET, pctx)) {
2019 inode->i_flags &= ~EXT4_EOFBLOCKS_FL;
2020 dirty_inode++;
2021 }
2022 }
2023 }
2024 /* i_size for symlinks is checked elsewhere */
2025 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
2026 pctx->num = (pb.last_block+1) * fs->blocksize;
2027 pctx->group = bad_size;
2028 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
2029 inode->i_size = pctx->num;
2030 if (!LINUX_S_ISDIR(inode->i_mode))
2031 inode->i_size_high = pctx->num >> 32;
2032 dirty_inode++;
2033 }
2034 pctx->num = 0;
2035 }
2036 if (LINUX_S_ISREG(inode->i_mode) &&
2037 (inode->i_size_high || inode->i_size & 0x80000000UL))
2038 ctx->large_files++;
2039 if ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
2040 ((fs->super->s_feature_ro_compat &
2041 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
2042 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
2043 (inode->osd2.linux2.l_i_blocks_hi != 0))) {
2044 pctx->num = pb.num_blocks;
2045 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
2046 inode->i_blocks = pb.num_blocks;
2047 inode->osd2.linux2.l_i_blocks_hi = 0;
2048 dirty_inode++;
2049 }
2050 pctx->num = 0;
2051 }
2052
2053 if (ctx->dirs_to_hash && pb.is_dir &&
2054 !(inode->i_flags & EXT2_INDEX_FL) &&
2055 ((inode->i_size / fs->blocksize) >= 3))
2056 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
2057
2058 out:
2059 if (dirty_inode)
2060 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
2061 }
2062
2063 #if 0
2064 /*
2065 * Helper function called by process block when an illegal block is
2066 * found. It returns a description about why the block is illegal
2067 */
2068 static char *describe_illegal_block(ext2_filsys fs, blk_t block)
2069 {
2070 blk_t super;
2071 int i;
2072 static char problem[80];
2073
2074 super = fs->super->s_first_data_block;
2075 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
2076 if (block < super) {
2077 sprintf(problem, "< FIRSTBLOCK (%u)", super);
2078 return(problem);
2079 } else if (block >= fs->super->s_blocks_count) {
2080 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
2081 return(problem);
2082 }
2083 for (i = 0; i < fs->group_desc_count; i++) {
2084 if (block == super) {
2085 sprintf(problem, "is the superblock in group %d", i);
2086 break;
2087 }
2088 if (block > super &&
2089 block <= (super + fs->desc_blocks)) {
2090 sprintf(problem, "is in the group descriptors "
2091 "of group %d", i);
2092 break;
2093 }
2094 if (block == fs->group_desc[i].bg_block_bitmap) {
2095 sprintf(problem, "is the block bitmap of group %d", i);
2096 break;
2097 }
2098 if (block == fs->group_desc[i].bg_inode_bitmap) {
2099 sprintf(problem, "is the inode bitmap of group %d", i);
2100 break;
2101 }
2102 if (block >= fs->group_desc[i].bg_inode_table &&
2103 (block < fs->group_desc[i].bg_inode_table
2104 + fs->inode_blocks_per_group)) {
2105 sprintf(problem, "is in the inode table of group %d",
2106 i);
2107 break;
2108 }
2109 super += fs->super->s_blocks_per_group;
2110 }
2111 return(problem);
2112 }
2113 #endif
2114
2115 /*
2116 * This is a helper function for check_blocks().
2117 */
process_block(ext2_filsys fs,blk_t * block_nr,e2_blkcnt_t blockcnt,blk_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)2118 static int process_block(ext2_filsys fs,
2119 blk_t *block_nr,
2120 e2_blkcnt_t blockcnt,
2121 blk_t ref_block EXT2FS_ATTR((unused)),
2122 int ref_offset EXT2FS_ATTR((unused)),
2123 void *priv_data)
2124 {
2125 struct process_block_struct *p;
2126 struct problem_context *pctx;
2127 blk_t blk = *block_nr;
2128 int ret_code = 0;
2129 int problem = 0;
2130 e2fsck_t ctx;
2131
2132 p = (struct process_block_struct *) priv_data;
2133 pctx = p->pctx;
2134 ctx = p->ctx;
2135
2136 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
2137 /* todo: Check that the comprblk_fl is high, that the
2138 blkaddr pattern looks right (all non-holes up to
2139 first EXT2FS_COMPRESSED_BLKADDR, then all
2140 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
2141 that the feature_incompat bit is high, and that the
2142 inode is a regular file. If we're doing a "full
2143 check" (a concept introduced to e2fsck by e2compr,
2144 meaning that we look at data blocks as well as
2145 metadata) then call some library routine that
2146 checks the compressed data. I'll have to think
2147 about this, because one particularly important
2148 problem to be able to fix is to recalculate the
2149 cluster size if necessary. I think that perhaps
2150 we'd better do most/all e2compr-specific checks
2151 separately, after the non-e2compr checks. If not
2152 doing a full check, it may be useful to test that
2153 the personality is linux; e.g. if it isn't then
2154 perhaps this really is just an illegal block. */
2155 return 0;
2156 }
2157
2158 if (blk == 0)
2159 return 0;
2160
2161 #if 0
2162 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
2163 blockcnt);
2164 #endif
2165
2166 /*
2167 * Simplistic fragmentation check. We merely require that the
2168 * file be contiguous. (Which can never be true for really
2169 * big files that are greater than a block group.)
2170 */
2171 if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
2172 if (p->previous_block+1 != blk) {
2173 if (ctx->options & E2F_OPT_FRAGCHECK) {
2174 char type = '?';
2175
2176 if (p->is_dir)
2177 type = 'd';
2178 else if (p->is_reg)
2179 type = 'f';
2180
2181 printf(_("%6lu(%c): expecting %6lu "
2182 "got phys %6lu (blkcnt %lld)\n"),
2183 (unsigned long) pctx->ino, type,
2184 (unsigned long) p->previous_block+1,
2185 (unsigned long) blk,
2186 blockcnt);
2187 }
2188 p->fragmented = 1;
2189 }
2190 }
2191 p->previous_block = blk;
2192
2193 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
2194 problem = PR_1_TOOBIG_DIR;
2195 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
2196 problem = PR_1_TOOBIG_REG;
2197 if (!p->is_dir && !p->is_reg && blockcnt > 0)
2198 problem = PR_1_TOOBIG_SYMLINK;
2199
2200 if (blk < fs->super->s_first_data_block ||
2201 blk >= fs->super->s_blocks_count)
2202 problem = PR_1_ILLEGAL_BLOCK_NUM;
2203
2204 if (problem) {
2205 p->num_illegal_blocks++;
2206 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
2207 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
2208 p->clear = 1;
2209 return BLOCK_ABORT;
2210 }
2211 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
2212 p->suppress = 1;
2213 set_latch_flags(PR_LATCH_BLOCK,
2214 PRL_SUPPRESS, 0);
2215 }
2216 }
2217 pctx->blk = blk;
2218 pctx->blkcount = blockcnt;
2219 if (fix_problem(ctx, problem, pctx)) {
2220 blk = *block_nr = 0;
2221 ret_code = BLOCK_CHANGED;
2222 goto mark_dir;
2223 } else
2224 return 0;
2225 }
2226
2227 if (p->ino == EXT2_RESIZE_INO) {
2228 /*
2229 * The resize inode has already be sanity checked
2230 * during pass #0 (the superblock checks). All we
2231 * have to do is mark the double indirect block as
2232 * being in use; all of the other blocks are handled
2233 * by mark_table_blocks()).
2234 */
2235 if (blockcnt == BLOCK_COUNT_DIND)
2236 mark_block_used(ctx, blk);
2237 } else
2238 mark_block_used(ctx, blk);
2239 p->num_blocks++;
2240 if (blockcnt >= 0)
2241 p->last_block = blockcnt;
2242 mark_dir:
2243 if (p->is_dir && (blockcnt >= 0)) {
2244 while (++p->last_db_block < blockcnt) {
2245 pctx->errcode = ext2fs_add_dir_block(fs->dblist,
2246 p->ino, 0,
2247 p->last_db_block);
2248 if (pctx->errcode) {
2249 pctx->blk = 0;
2250 pctx->num = p->last_db_block;
2251 goto failed_add_dir_block;
2252 }
2253 }
2254 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
2255 blk, blockcnt);
2256 if (pctx->errcode) {
2257 pctx->blk = blk;
2258 pctx->num = blockcnt;
2259 failed_add_dir_block:
2260 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2261 /* Should never get here */
2262 ctx->flags |= E2F_FLAG_ABORT;
2263 return BLOCK_ABORT;
2264 }
2265 }
2266 return ret_code;
2267 }
2268
process_bad_block(ext2_filsys fs,blk_t * block_nr,e2_blkcnt_t blockcnt,blk_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)2269 static int process_bad_block(ext2_filsys fs,
2270 blk_t *block_nr,
2271 e2_blkcnt_t blockcnt,
2272 blk_t ref_block EXT2FS_ATTR((unused)),
2273 int ref_offset EXT2FS_ATTR((unused)),
2274 void *priv_data)
2275 {
2276 struct process_block_struct *p;
2277 blk_t blk = *block_nr;
2278 blk_t first_block;
2279 dgrp_t i;
2280 struct problem_context *pctx;
2281 e2fsck_t ctx;
2282
2283 /*
2284 * Note: This function processes blocks for the bad blocks
2285 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
2286 */
2287
2288 if (!blk)
2289 return 0;
2290
2291 p = (struct process_block_struct *) priv_data;
2292 ctx = p->ctx;
2293 pctx = p->pctx;
2294
2295 pctx->ino = EXT2_BAD_INO;
2296 pctx->blk = blk;
2297 pctx->blkcount = blockcnt;
2298
2299 if ((blk < fs->super->s_first_data_block) ||
2300 (blk >= fs->super->s_blocks_count)) {
2301 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
2302 *block_nr = 0;
2303 return BLOCK_CHANGED;
2304 } else
2305 return 0;
2306 }
2307
2308 if (blockcnt < 0) {
2309 if (ext2fs_test_block_bitmap(p->fs_meta_blocks, blk)) {
2310 p->bbcheck = 1;
2311 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
2312 *block_nr = 0;
2313 return BLOCK_CHANGED;
2314 }
2315 } else if (ext2fs_test_block_bitmap(ctx->block_found_map,
2316 blk)) {
2317 p->bbcheck = 1;
2318 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
2319 pctx)) {
2320 *block_nr = 0;
2321 return BLOCK_CHANGED;
2322 }
2323 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2324 return BLOCK_ABORT;
2325 } else
2326 mark_block_used(ctx, blk);
2327 return 0;
2328 }
2329 #if 0
2330 printf ("DEBUG: Marking %u as bad.\n", blk);
2331 #endif
2332 ctx->fs_badblocks_count++;
2333 /*
2334 * If the block is not used, then mark it as used and return.
2335 * If it is already marked as found, this must mean that
2336 * there's an overlap between the filesystem table blocks
2337 * (bitmaps and inode table) and the bad block list.
2338 */
2339 if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
2340 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
2341 return 0;
2342 }
2343 /*
2344 * Try to find the where the filesystem block was used...
2345 */
2346 first_block = fs->super->s_first_data_block;
2347
2348 for (i = 0; i < fs->group_desc_count; i++ ) {
2349 pctx->group = i;
2350 pctx->blk = blk;
2351 if (!ext2fs_bg_has_super(fs, i))
2352 goto skip_super;
2353 if (blk == first_block) {
2354 if (i == 0) {
2355 if (fix_problem(ctx,
2356 PR_1_BAD_PRIMARY_SUPERBLOCK,
2357 pctx)) {
2358 *block_nr = 0;
2359 return BLOCK_CHANGED;
2360 }
2361 return 0;
2362 }
2363 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
2364 return 0;
2365 }
2366 if ((blk > first_block) &&
2367 (blk <= first_block + fs->desc_blocks)) {
2368 if (i == 0) {
2369 pctx->blk = *block_nr;
2370 if (fix_problem(ctx,
2371 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
2372 *block_nr = 0;
2373 return BLOCK_CHANGED;
2374 }
2375 return 0;
2376 }
2377 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
2378 return 0;
2379 }
2380 skip_super:
2381 if (blk == fs->group_desc[i].bg_block_bitmap) {
2382 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
2383 ctx->invalid_block_bitmap_flag[i]++;
2384 ctx->invalid_bitmaps++;
2385 }
2386 return 0;
2387 }
2388 if (blk == fs->group_desc[i].bg_inode_bitmap) {
2389 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
2390 ctx->invalid_inode_bitmap_flag[i]++;
2391 ctx->invalid_bitmaps++;
2392 }
2393 return 0;
2394 }
2395 if ((blk >= fs->group_desc[i].bg_inode_table) &&
2396 (blk < (fs->group_desc[i].bg_inode_table +
2397 fs->inode_blocks_per_group))) {
2398 /*
2399 * If there are bad blocks in the inode table,
2400 * the inode scan code will try to do
2401 * something reasonable automatically.
2402 */
2403 return 0;
2404 }
2405 first_block += fs->super->s_blocks_per_group;
2406 }
2407 /*
2408 * If we've gotten to this point, then the only
2409 * possibility is that the bad block inode meta data
2410 * is using a bad block.
2411 */
2412 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
2413 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
2414 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
2415 p->bbcheck = 1;
2416 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
2417 *block_nr = 0;
2418 return BLOCK_CHANGED;
2419 }
2420 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2421 return BLOCK_ABORT;
2422 return 0;
2423 }
2424
2425 pctx->group = -1;
2426
2427 /* Warn user that the block wasn't claimed */
2428 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
2429
2430 return 0;
2431 }
2432
new_table_block(e2fsck_t ctx,blk_t first_block,int group,const char * name,int num,blk_t * new_block)2433 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
2434 const char *name, int num, blk_t *new_block)
2435 {
2436 ext2_filsys fs = ctx->fs;
2437 dgrp_t last_grp;
2438 blk_t old_block = *new_block;
2439 blk_t last_block;
2440 int i, is_flexbg, flexbg, flexbg_size;
2441 char *buf;
2442 struct problem_context pctx;
2443
2444 clear_problem_context(&pctx);
2445
2446 pctx.group = group;
2447 pctx.blk = old_block;
2448 pctx.str = name;
2449
2450 /*
2451 * For flex_bg filesystems, first try to allocate the metadata
2452 * within the flex_bg, and if that fails then try finding the
2453 * space anywhere in the filesystem.
2454 */
2455 is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2456 EXT4_FEATURE_INCOMPAT_FLEX_BG);
2457 if (is_flexbg) {
2458 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2459 flexbg = group / flexbg_size;
2460 first_block = ext2fs_group_first_block(fs,
2461 flexbg_size * flexbg);
2462 last_grp = group | (flexbg_size - 1);
2463 if (last_grp > fs->group_desc_count)
2464 last_grp = fs->group_desc_count;
2465 last_block = ext2fs_group_last_block(fs, last_grp);
2466 } else
2467 last_block = ext2fs_group_last_block(fs, group);
2468 pctx.errcode = ext2fs_get_free_blocks(fs, first_block, last_block,
2469 num, ctx->block_found_map,
2470 new_block);
2471 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
2472 pctx.errcode = ext2fs_get_free_blocks(fs,
2473 fs->super->s_first_data_block,
2474 fs->super->s_blocks_count,
2475 num, ctx->block_found_map, new_block);
2476 if (pctx.errcode) {
2477 pctx.num = num;
2478 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
2479 ext2fs_unmark_valid(fs);
2480 ctx->flags |= E2F_FLAG_ABORT;
2481 return;
2482 }
2483 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
2484 if (pctx.errcode) {
2485 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
2486 ext2fs_unmark_valid(fs);
2487 ctx->flags |= E2F_FLAG_ABORT;
2488 return;
2489 }
2490 ext2fs_mark_super_dirty(fs);
2491 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2492 pctx.blk2 = *new_block;
2493 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
2494 PR_1_RELOC_TO), &pctx);
2495 pctx.blk2 = 0;
2496 for (i = 0; i < num; i++) {
2497 pctx.blk = i;
2498 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
2499 if (old_block) {
2500 pctx.errcode = io_channel_read_blk(fs->io,
2501 old_block + i, 1, buf);
2502 if (pctx.errcode)
2503 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
2504 } else
2505 memset(buf, 0, fs->blocksize);
2506
2507 pctx.blk = (*new_block) + i;
2508 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
2509 1, buf);
2510 if (pctx.errcode)
2511 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
2512 }
2513 ext2fs_free_mem(&buf);
2514 }
2515
2516 /*
2517 * This routine gets called at the end of pass 1 if bad blocks are
2518 * detected in the superblock, group descriptors, inode_bitmaps, or
2519 * block bitmaps. At this point, all of the blocks have been mapped
2520 * out, so we can try to allocate new block(s) to replace the bad
2521 * blocks.
2522 */
handle_fs_bad_blocks(e2fsck_t ctx)2523 static void handle_fs_bad_blocks(e2fsck_t ctx)
2524 {
2525 ext2_filsys fs = ctx->fs;
2526 dgrp_t i;
2527 blk_t first_block;
2528
2529 for (i = 0; i < fs->group_desc_count; i++) {
2530 first_block = ext2fs_group_first_block(fs, i);
2531
2532 if (ctx->invalid_block_bitmap_flag[i]) {
2533 new_table_block(ctx, first_block, i, _("block bitmap"),
2534 1, &fs->group_desc[i].bg_block_bitmap);
2535 }
2536 if (ctx->invalid_inode_bitmap_flag[i]) {
2537 new_table_block(ctx, first_block, i, _("inode bitmap"),
2538 1, &fs->group_desc[i].bg_inode_bitmap);
2539 }
2540 if (ctx->invalid_inode_table_flag[i]) {
2541 new_table_block(ctx, first_block, i, _("inode table"),
2542 fs->inode_blocks_per_group,
2543 &fs->group_desc[i].bg_inode_table);
2544 ctx->flags |= E2F_FLAG_RESTART;
2545 }
2546 }
2547 ctx->invalid_bitmaps = 0;
2548 }
2549
2550 /*
2551 * This routine marks all blocks which are used by the superblock,
2552 * group descriptors, inode bitmaps, and block bitmaps.
2553 */
mark_table_blocks(e2fsck_t ctx)2554 static void mark_table_blocks(e2fsck_t ctx)
2555 {
2556 ext2_filsys fs = ctx->fs;
2557 blk_t b;
2558 dgrp_t i;
2559 int j;
2560 struct problem_context pctx;
2561
2562 clear_problem_context(&pctx);
2563
2564 for (i = 0; i < fs->group_desc_count; i++) {
2565 pctx.group = i;
2566
2567 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
2568
2569 /*
2570 * Mark the blocks used for the inode table
2571 */
2572 if (fs->group_desc[i].bg_inode_table) {
2573 for (j = 0, b = fs->group_desc[i].bg_inode_table;
2574 j < fs->inode_blocks_per_group;
2575 j++, b++) {
2576 if (ext2fs_test_block_bitmap(ctx->block_found_map,
2577 b)) {
2578 pctx.blk = b;
2579 if (!ctx->invalid_inode_table_flag[i] &&
2580 fix_problem(ctx,
2581 PR_1_ITABLE_CONFLICT, &pctx)) {
2582 ctx->invalid_inode_table_flag[i]++;
2583 ctx->invalid_bitmaps++;
2584 }
2585 } else {
2586 ext2fs_mark_block_bitmap(ctx->block_found_map,
2587 b);
2588 }
2589 }
2590 }
2591
2592 /*
2593 * Mark block used for the block bitmap
2594 */
2595 if (fs->group_desc[i].bg_block_bitmap) {
2596 if (ext2fs_test_block_bitmap(ctx->block_found_map,
2597 fs->group_desc[i].bg_block_bitmap)) {
2598 pctx.blk = fs->group_desc[i].bg_block_bitmap;
2599 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
2600 ctx->invalid_block_bitmap_flag[i]++;
2601 ctx->invalid_bitmaps++;
2602 }
2603 } else {
2604 ext2fs_mark_block_bitmap(ctx->block_found_map,
2605 fs->group_desc[i].bg_block_bitmap);
2606 }
2607
2608 }
2609 /*
2610 * Mark block used for the inode bitmap
2611 */
2612 if (fs->group_desc[i].bg_inode_bitmap) {
2613 if (ext2fs_test_block_bitmap(ctx->block_found_map,
2614 fs->group_desc[i].bg_inode_bitmap)) {
2615 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
2616 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
2617 ctx->invalid_inode_bitmap_flag[i]++;
2618 ctx->invalid_bitmaps++;
2619 }
2620 } else {
2621 ext2fs_mark_block_bitmap(ctx->block_found_map,
2622 fs->group_desc[i].bg_inode_bitmap);
2623 }
2624 }
2625 }
2626 }
2627
2628 /*
2629 * Thes subroutines short circuits ext2fs_get_blocks and
2630 * ext2fs_check_directory; we use them since we already have the inode
2631 * structure, so there's no point in letting the ext2fs library read
2632 * the inode again.
2633 */
pass1_get_blocks(ext2_filsys fs,ext2_ino_t ino,blk_t * blocks)2634 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
2635 blk_t *blocks)
2636 {
2637 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2638 int i;
2639
2640 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2641 return EXT2_ET_CALLBACK_NOTHANDLED;
2642
2643 for (i=0; i < EXT2_N_BLOCKS; i++)
2644 blocks[i] = ctx->stashed_inode->i_block[i];
2645 return 0;
2646 }
2647
pass1_read_inode(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)2648 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
2649 struct ext2_inode *inode)
2650 {
2651 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2652
2653 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2654 return EXT2_ET_CALLBACK_NOTHANDLED;
2655 *inode = *ctx->stashed_inode;
2656 return 0;
2657 }
2658
pass1_write_inode(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)2659 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
2660 struct ext2_inode *inode)
2661 {
2662 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2663
2664 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
2665 (inode != ctx->stashed_inode))
2666 *ctx->stashed_inode = *inode;
2667 return EXT2_ET_CALLBACK_NOTHANDLED;
2668 }
2669
pass1_check_directory(ext2_filsys fs,ext2_ino_t ino)2670 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
2671 {
2672 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2673
2674 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2675 return EXT2_ET_CALLBACK_NOTHANDLED;
2676
2677 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
2678 return EXT2_ET_NO_DIRECTORY;
2679 return 0;
2680 }
2681
e2fsck_get_alloc_block(ext2_filsys fs,blk64_t goal,blk64_t * ret)2682 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
2683 blk64_t *ret)
2684 {
2685 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2686 errcode_t retval;
2687 blk_t new_block;
2688
2689 if (ctx->block_found_map) {
2690 retval = ext2fs_new_block(fs, (blk_t) goal,
2691 ctx->block_found_map, &new_block);
2692 if (retval)
2693 return retval;
2694 if (fs->block_map) {
2695 ext2fs_mark_block_bitmap(fs->block_map, new_block);
2696 ext2fs_mark_bb_dirty(fs);
2697 }
2698 } else {
2699 if (!fs->block_map) {
2700 retval = ext2fs_read_block_bitmap(fs);
2701 if (retval)
2702 return retval;
2703 }
2704
2705 retval = ext2fs_new_block(fs, (blk_t) goal, 0, &new_block);
2706 if (retval)
2707 return retval;
2708 }
2709
2710 *ret = new_block;
2711 return (0);
2712 }
2713
e2fsck_block_alloc_stats(ext2_filsys fs,blk64_t blk,int inuse)2714 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
2715 {
2716 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2717
2718 if (ctx->block_found_map) {
2719 if (inuse > 0)
2720 ext2fs_mark_block_bitmap(ctx->block_found_map,
2721 (blk_t) blk);
2722 else
2723 ext2fs_unmark_block_bitmap(ctx->block_found_map,
2724 (blk_t) blk);
2725 }
2726 }
2727
e2fsck_use_inode_shortcuts(e2fsck_t ctx,int bool)2728 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
2729 {
2730 ext2_filsys fs = ctx->fs;
2731
2732 if (bool) {
2733 fs->get_blocks = pass1_get_blocks;
2734 fs->check_directory = pass1_check_directory;
2735 fs->read_inode = pass1_read_inode;
2736 fs->write_inode = pass1_write_inode;
2737 ctx->stashed_ino = 0;
2738 ext2fs_set_alloc_block_callback(fs, e2fsck_get_alloc_block,
2739 0);
2740 ext2fs_set_block_alloc_stats_callback(fs,
2741 e2fsck_block_alloc_stats,
2742 0);
2743 } else {
2744 fs->get_blocks = 0;
2745 fs->check_directory = 0;
2746 fs->read_inode = 0;
2747 fs->write_inode = 0;
2748 }
2749 }
2750