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