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