]>
Commit | Line | Data |
---|---|---|
3839e657 TT |
1 | /* |
2 | * pass2.c --- check directory structure | |
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 2 of e2fsck iterates through all active directory inodes, and |
12 | * applies to following tests to each directory entry in the directory | |
13 | * blocks in the inodes: | |
14 | * | |
15 | * - The length of the directory entry (rec_len) should be at | |
16 | * least 8 bytes, and no more than the remaining space | |
17 | * left in the directory block. | |
18 | * - The length of the name in the directory entry (name_len) | |
efc6f628 | 19 | * should be less than (rec_len - 8). |
3839e657 TT |
20 | * - The inode number in the directory entry should be within |
21 | * legal bounds. | |
22 | * - The inode number should refer to a in-use inode. | |
23 | * - The first entry should be '.', and its inode should be | |
24 | * the inode of the directory. | |
25 | * - The second entry should be '..'. | |
26 | * | |
27 | * To minimize disk seek time, the directory blocks are processed in | |
28 | * sorted order of block numbers. | |
29 | * | |
30 | * Pass 2 also collects the following information: | |
31 | * - The inode numbers of the subdirectories for each directory. | |
32 | * | |
33 | * Pass 2 relies on the following information from previous passes: | |
34 | * - The directory information collected in pass 1. | |
35 | * - The inode_used_map bitmap | |
36 | * - The inode_bad_map bitmap | |
37 | * - The inode_dir_map bitmap | |
3839e657 TT |
38 | * |
39 | * Pass 2 frees the following data structures | |
40 | * - The inode_bad_map bitmap | |
aa4115a4 | 41 | * - The inode_reg_map bitmap |
3839e657 TT |
42 | */ |
43 | ||
b969b1b8 | 44 | #define _GNU_SOURCE 1 /* get strnlen() */ |
d1154eb4 | 45 | #include "config.h" |
48e6e813 TT |
46 | #include <string.h> |
47 | ||
3839e657 | 48 | #include "e2fsck.h" |
21c84b71 | 49 | #include "problem.h" |
0926668d | 50 | #include "dict.h" |
3839e657 | 51 | |
aa4115a4 TT |
52 | #ifdef NO_INLINE_FUNCS |
53 | #define _INLINE_ | |
54 | #else | |
55 | #define _INLINE_ inline | |
56 | #endif | |
57 | ||
ad4fa466 | 58 | /* #define DX_DEBUG */ |
8fdc9985 | 59 | |
3839e657 TT |
60 | /* |
61 | * Keeps track of how many times an inode is referenced. | |
62 | */ | |
4035f40b | 63 | static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf); |
3839e657 | 64 | static int check_dir_block(ext2_filsys fs, |
6dc64392 | 65 | struct ext2_db_entry2 *dir_blocks_info, |
54dc7ca2 | 66 | void *priv_data); |
1b6bf175 | 67 | static int allocate_dir_block(e2fsck_t ctx, |
6dc64392 | 68 | struct ext2_db_entry2 *dir_blocks_info, |
21c84b71 | 69 | char *buf, struct problem_context *pctx); |
8fdc9985 | 70 | static void clear_htree(e2fsck_t ctx, ext2_ino_t ino); |
ad4fa466 TT |
71 | static int htree_depth(struct dx_dir_info *dx_dir, |
72 | struct dx_dirblock_info *dx_db); | |
ea1959f0 | 73 | static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b); |
3839e657 | 74 | |
21c84b71 TT |
75 | struct check_dir_struct { |
76 | char *buf; | |
77 | struct problem_context pctx; | |
f8188fff | 78 | int count, max; |
1b6bf175 | 79 | e2fsck_t ctx; |
efc6f628 | 80 | }; |
21c84b71 | 81 | |
08b21301 | 82 | void e2fsck_pass2(e2fsck_t ctx) |
3839e657 | 83 | { |
a4742691 TT |
84 | struct ext2_super_block *sb = ctx->fs->super; |
85 | struct problem_context pctx; | |
86 | ext2_filsys fs = ctx->fs; | |
87 | char *buf; | |
8bf191e8 | 88 | #ifdef RESOURCE_TRACK |
3839e657 | 89 | struct resource_track rtrack; |
8bf191e8 | 90 | #endif |
21c84b71 | 91 | struct check_dir_struct cd; |
8fdc9985 TT |
92 | struct dx_dir_info *dx_dir; |
93 | struct dx_dirblock_info *dx_db, *dx_parent; | |
830b44f4 | 94 | unsigned int save_type; |
54434927 | 95 | int b; |
ad4fa466 | 96 | int i, depth; |
8fdc9985 TT |
97 | problem_t code; |
98 | int bad_dir; | |
99 | ||
6d96b00d | 100 | init_resource_track(&rtrack, ctx->fs->io); |
1b6bf175 TT |
101 | clear_problem_context(&cd.pctx); |
102 | ||
3839e657 TT |
103 | #ifdef MTRACE |
104 | mtrace_print("Pass 2"); | |
105 | #endif | |
106 | ||
1b6bf175 TT |
107 | if (!(ctx->options & E2F_OPT_PREEN)) |
108 | fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx); | |
109 | ||
efc6f628 | 110 | e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT, |
34b9f796 TT |
111 | &ctx->inode_count); |
112 | if (ctx->inode_count) | |
113 | cd.pctx.errcode = 0; | |
830b44f4 TT |
114 | else { |
115 | e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, | |
116 | "inode_count", &save_type); | |
efc6f628 | 117 | cd.pctx.errcode = ext2fs_create_icount2(fs, |
34b9f796 | 118 | EXT2_ICOUNT_OPT_INCREMENT, |
1b6bf175 TT |
119 | 0, ctx->inode_link_info, |
120 | &ctx->inode_count); | |
830b44f4 TT |
121 | fs->default_bitmap_type = save_type; |
122 | } | |
1b6bf175 TT |
123 | if (cd.pctx.errcode) { |
124 | fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx); | |
08b21301 TT |
125 | ctx->flags |= E2F_FLAG_ABORT; |
126 | return; | |
21c84b71 | 127 | } |
bcf9c5d4 | 128 | buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize, |
54dc7ca2 | 129 | "directory scan buffer"); |
3839e657 | 130 | |
21c84b71 TT |
131 | /* |
132 | * Set up the parent pointer for the root directory, if | |
133 | * present. (If the root directory is not present, we will | |
134 | * create it in pass 3.) | |
135 | */ | |
28db82a8 | 136 | (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO); |
21c84b71 TT |
137 | |
138 | cd.buf = buf; | |
1b6bf175 | 139 | cd.ctx = ctx; |
f75c28de | 140 | cd.count = 1; |
6dc64392 | 141 | cd.max = ext2fs_dblist_count2(fs->dblist); |
f75c28de TT |
142 | |
143 | if (ctx->progress) | |
144 | (void) (ctx->progress)(ctx, 2, 0, cd.max); | |
ea1959f0 TT |
145 | |
146 | if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) | |
6dc64392 | 147 | ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp); |
efc6f628 | 148 | |
6dc64392 VAH |
149 | cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_block, |
150 | &cd); | |
49a7360b | 151 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART) |
08b21301 | 152 | return; |
6267ee49 AD |
153 | |
154 | if (ctx->flags & E2F_FLAG_RESTART_LATER) { | |
155 | ctx->flags |= E2F_FLAG_RESTART; | |
156 | return; | |
157 | } | |
158 | ||
1b6bf175 TT |
159 | if (cd.pctx.errcode) { |
160 | fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx); | |
08b21301 TT |
161 | ctx->flags |= E2F_FLAG_ABORT; |
162 | return; | |
7ac02a5e | 163 | } |
8fdc9985 TT |
164 | |
165 | #ifdef ENABLE_HTREE | |
166 | for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) { | |
4cae0452 TT |
167 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
168 | return; | |
62acaa1d | 169 | if (dx_dir->numblocks == 0) |
8fdc9985 TT |
170 | continue; |
171 | clear_problem_context(&pctx); | |
172 | bad_dir = 0; | |
173 | pctx.dir = dx_dir->ino; | |
174 | dx_db = dx_dir->dx_block; | |
175 | if (dx_db->flags & DX_FLAG_REFERENCED) | |
176 | dx_db->flags |= DX_FLAG_DUP_REF; | |
177 | else | |
178 | dx_db->flags |= DX_FLAG_REFERENCED; | |
179 | /* | |
180 | * Find all of the first and last leaf blocks, and | |
181 | * update their parent's min and max hash values | |
182 | */ | |
183 | for (b=0, dx_db = dx_dir->dx_block; | |
184 | b < dx_dir->numblocks; | |
185 | b++, dx_db++) { | |
186 | if ((dx_db->type != DX_DIRBLOCK_LEAF) || | |
187 | !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST))) | |
188 | continue; | |
189 | dx_parent = &dx_dir->dx_block[dx_db->parent]; | |
190 | /* | |
191 | * XXX Make sure dx_parent->min_hash > dx_db->min_hash | |
192 | */ | |
193 | if (dx_db->flags & DX_FLAG_FIRST) | |
194 | dx_parent->min_hash = dx_db->min_hash; | |
195 | /* | |
196 | * XXX Make sure dx_parent->max_hash < dx_db->max_hash | |
197 | */ | |
198 | if (dx_db->flags & DX_FLAG_LAST) | |
199 | dx_parent->max_hash = dx_db->max_hash; | |
200 | } | |
efc6f628 | 201 | |
8fdc9985 TT |
202 | for (b=0, dx_db = dx_dir->dx_block; |
203 | b < dx_dir->numblocks; | |
204 | b++, dx_db++) { | |
205 | pctx.blkcount = b; | |
206 | pctx.group = dx_db->parent; | |
207 | code = 0; | |
208 | if (!(dx_db->flags & DX_FLAG_FIRST) && | |
209 | (dx_db->min_hash < dx_db->node_min_hash)) { | |
210 | pctx.blk = dx_db->min_hash; | |
211 | pctx.blk2 = dx_db->node_min_hash; | |
212 | code = PR_2_HTREE_MIN_HASH; | |
213 | fix_problem(ctx, code, &pctx); | |
214 | bad_dir++; | |
215 | } | |
ad4fa466 TT |
216 | if (dx_db->type == DX_DIRBLOCK_LEAF) { |
217 | depth = htree_depth(dx_dir, dx_db); | |
218 | if (depth != dx_dir->depth) { | |
e5e12db9 | 219 | pctx.num = dx_dir->depth; |
ad4fa466 TT |
220 | code = PR_2_HTREE_BAD_DEPTH; |
221 | fix_problem(ctx, code, &pctx); | |
222 | bad_dir++; | |
223 | } | |
224 | } | |
8fdc9985 | 225 | /* |
efc6f628 | 226 | * This test doesn't apply for the root block |
8fdc9985 TT |
227 | * at block #0 |
228 | */ | |
229 | if (b && | |
230 | (dx_db->max_hash > dx_db->node_max_hash)) { | |
231 | pctx.blk = dx_db->max_hash; | |
232 | pctx.blk2 = dx_db->node_max_hash; | |
233 | code = PR_2_HTREE_MAX_HASH; | |
234 | fix_problem(ctx, code, &pctx); | |
503f9e7f | 235 | bad_dir++; |
8fdc9985 TT |
236 | } |
237 | if (!(dx_db->flags & DX_FLAG_REFERENCED)) { | |
238 | code = PR_2_HTREE_NOTREF; | |
239 | fix_problem(ctx, code, &pctx); | |
240 | bad_dir++; | |
241 | } else if (dx_db->flags & DX_FLAG_DUP_REF) { | |
242 | code = PR_2_HTREE_DUPREF; | |
243 | fix_problem(ctx, code, &pctx); | |
244 | bad_dir++; | |
245 | } | |
8fdc9985 TT |
246 | } |
247 | if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) { | |
248 | clear_htree(ctx, dx_dir->ino); | |
62acaa1d | 249 | dx_dir->numblocks = 0; |
8fdc9985 | 250 | } |
8fdc9985 | 251 | } |
23f75f6e | 252 | e2fsck_free_dx_dir_info(ctx); |
8fdc9985 | 253 | #endif |
c4e3d3f3 | 254 | ext2fs_free_mem(&buf); |
21c84b71 TT |
255 | ext2fs_free_dblist(fs->dblist); |
256 | ||
1b6bf175 TT |
257 | if (ctx->inode_bad_map) { |
258 | ext2fs_free_inode_bitmap(ctx->inode_bad_map); | |
259 | ctx->inode_bad_map = 0; | |
3839e657 | 260 | } |
aa4115a4 TT |
261 | if (ctx->inode_reg_map) { |
262 | ext2fs_free_inode_bitmap(ctx->inode_reg_map); | |
263 | ctx->inode_reg_map = 0; | |
264 | } | |
a4742691 TT |
265 | |
266 | clear_problem_context(&pctx); | |
267 | if (ctx->large_files) { | |
268 | if (!(sb->s_feature_ro_compat & | |
269 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE) && | |
270 | fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) { | |
271 | sb->s_feature_ro_compat |= | |
272 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE; | |
0cfce7f7 | 273 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
a4742691 TT |
274 | ext2fs_mark_super_dirty(fs); |
275 | } | |
276 | if (sb->s_rev_level == EXT2_GOOD_OLD_REV && | |
277 | fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) { | |
278 | ext2fs_update_dynamic_rev(fs); | |
279 | ext2fs_mark_super_dirty(fs); | |
280 | } | |
a4742691 | 281 | } |
efc6f628 | 282 | |
9facd076 | 283 | print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io); |
3839e657 TT |
284 | } |
285 | ||
ad4fa466 TT |
286 | #define MAX_DEPTH 32000 |
287 | static int htree_depth(struct dx_dir_info *dx_dir, | |
288 | struct dx_dirblock_info *dx_db) | |
289 | { | |
290 | int depth = 0; | |
291 | ||
292 | while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) { | |
293 | dx_db = &dx_dir->dx_block[dx_db->parent]; | |
294 | depth++; | |
295 | } | |
296 | return depth; | |
297 | } | |
298 | ||
0926668d TT |
299 | static int dict_de_cmp(const void *a, const void *b) |
300 | { | |
520ead37 | 301 | const struct ext2_dir_entry *de_a, *de_b; |
0926668d TT |
302 | int a_len, b_len; |
303 | ||
520ead37 | 304 | de_a = (const struct ext2_dir_entry *) a; |
70f4632b | 305 | a_len = ext2fs_dirent_name_len(de_a); |
520ead37 | 306 | de_b = (const struct ext2_dir_entry *) b; |
70f4632b | 307 | b_len = ext2fs_dirent_name_len(de_b); |
0926668d TT |
308 | |
309 | if (a_len != b_len) | |
310 | return (a_len - b_len); | |
311 | ||
312 | return strncmp(de_a->name, de_b->name, a_len); | |
313 | } | |
ad4fa466 | 314 | |
ea1959f0 TT |
315 | /* |
316 | * This is special sort function that makes sure that directory blocks | |
317 | * with a dirblock of zero are sorted to the beginning of the list. | |
318 | * This guarantees that the root node of the htree directories are | |
319 | * processed first, so we know what hash version to use. | |
320 | */ | |
321 | static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b) | |
322 | { | |
6dc64392 VAH |
323 | const struct ext2_db_entry2 *db_a = |
324 | (const struct ext2_db_entry2 *) a; | |
325 | const struct ext2_db_entry2 *db_b = | |
326 | (const struct ext2_db_entry2 *) b; | |
ea1959f0 TT |
327 | |
328 | if (db_a->blockcnt && !db_b->blockcnt) | |
329 | return 1; | |
330 | ||
331 | if (!db_a->blockcnt && db_b->blockcnt) | |
332 | return -1; | |
efc6f628 | 333 | |
ea1959f0 TT |
334 | if (db_a->blk != db_b->blk) |
335 | return (int) (db_a->blk - db_b->blk); | |
efc6f628 | 336 | |
ea1959f0 TT |
337 | if (db_a->ino != db_b->ino) |
338 | return (int) (db_a->ino - db_b->ino); | |
339 | ||
340 | return (int) (db_a->blockcnt - db_b->blockcnt); | |
341 | } | |
342 | ||
343 | ||
3839e657 TT |
344 | /* |
345 | * Make sure the first entry in the directory is '.', and that the | |
346 | * directory entry is sane. | |
347 | */ | |
1b6bf175 | 348 | static int check_dot(e2fsck_t ctx, |
3839e657 | 349 | struct ext2_dir_entry *dirent, |
86c627ec | 350 | ext2_ino_t ino, struct problem_context *pctx) |
3839e657 TT |
351 | { |
352 | struct ext2_dir_entry *nextdir; | |
8a480350 | 353 | unsigned int rec_len, new_len; |
3c7c6d73 TT |
354 | int status = 0; |
355 | int created = 0; | |
356 | problem_t problem = 0; | |
efc6f628 | 357 | |
21c84b71 TT |
358 | if (!dirent->inode) |
359 | problem = PR_2_MISSING_DOT; | |
70f4632b | 360 | else if ((ext2fs_dirent_name_len(dirent) != 1) || |
21c84b71 TT |
361 | (dirent->name[0] != '.')) |
362 | problem = PR_2_1ST_NOT_DOT; | |
363 | else if (dirent->name[1] != '\0') | |
364 | problem = PR_2_DOT_NULL_TERM; | |
5dd77dbe | 365 | |
8a480350 | 366 | (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len); |
21c84b71 | 367 | if (problem) { |
1b6bf175 | 368 | if (fix_problem(ctx, problem, pctx)) { |
5dd77dbe TT |
369 | if (rec_len < 12) |
370 | rec_len = dirent->rec_len = 12; | |
3839e657 | 371 | dirent->inode = ino; |
70f4632b JK |
372 | ext2fs_dirent_set_name_len(dirent, 1); |
373 | ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN); | |
3839e657 | 374 | dirent->name[0] = '.'; |
21c84b71 | 375 | dirent->name[1] = '\0'; |
3839e657 TT |
376 | status = 1; |
377 | created = 1; | |
3839e657 TT |
378 | } |
379 | } | |
3839e657 | 380 | if (dirent->inode != ino) { |
1b6bf175 | 381 | if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) { |
3839e657 TT |
382 | dirent->inode = ino; |
383 | status = 1; | |
21c84b71 | 384 | } |
3839e657 | 385 | } |
5dd77dbe TT |
386 | if (rec_len > 12) { |
387 | new_len = rec_len - 12; | |
3839e657 | 388 | if (new_len > 12) { |
3839e657 | 389 | if (created || |
f8188fff | 390 | fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) { |
3839e657 TT |
391 | nextdir = (struct ext2_dir_entry *) |
392 | ((char *) dirent + 12); | |
393 | dirent->rec_len = 12; | |
8a480350 TT |
394 | (void) ext2fs_set_rec_len(ctx->fs, new_len, |
395 | nextdir); | |
3839e657 | 396 | nextdir->inode = 0; |
70f4632b JK |
397 | ext2fs_dirent_set_name_len(nextdir, 0); |
398 | ext2fs_dirent_set_file_type(nextdir, | |
399 | EXT2_FT_UNKNOWN); | |
3839e657 TT |
400 | status = 1; |
401 | } | |
402 | } | |
403 | } | |
404 | return status; | |
405 | } | |
406 | ||
407 | /* | |
408 | * Make sure the second entry in the directory is '..', and that the | |
409 | * directory entry is sane. We do not check the inode number of '..' | |
410 | * here; this gets done in pass 3. | |
411 | */ | |
1b6bf175 | 412 | static int check_dotdot(e2fsck_t ctx, |
3839e657 | 413 | struct ext2_dir_entry *dirent, |
28db82a8 | 414 | ext2_ino_t ino, struct problem_context *pctx) |
3839e657 | 415 | { |
3c7c6d73 | 416 | problem_t problem = 0; |
cf5301d7 | 417 | unsigned int rec_len; |
efc6f628 | 418 | |
21c84b71 TT |
419 | if (!dirent->inode) |
420 | problem = PR_2_MISSING_DOT_DOT; | |
70f4632b | 421 | else if ((ext2fs_dirent_name_len(dirent) != 2) || |
21c84b71 TT |
422 | (dirent->name[0] != '.') || |
423 | (dirent->name[1] != '.')) | |
424 | problem = PR_2_2ND_NOT_DOT_DOT; | |
425 | else if (dirent->name[2] != '\0') | |
426 | problem = PR_2_DOT_DOT_NULL_TERM; | |
427 | ||
8a480350 | 428 | (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len); |
21c84b71 | 429 | if (problem) { |
1b6bf175 | 430 | if (fix_problem(ctx, problem, pctx)) { |
5dd77dbe | 431 | if (rec_len < 12) |
21c84b71 | 432 | dirent->rec_len = 12; |
3839e657 TT |
433 | /* |
434 | * Note: we don't have the parent inode just | |
435 | * yet, so we will fill it in with the root | |
436 | * inode. This will get fixed in pass 3. | |
437 | */ | |
438 | dirent->inode = EXT2_ROOT_INO; | |
70f4632b JK |
439 | ext2fs_dirent_set_name_len(dirent, 2); |
440 | ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN); | |
3839e657 TT |
441 | dirent->name[0] = '.'; |
442 | dirent->name[1] = '.'; | |
21c84b71 | 443 | dirent->name[2] = '\0'; |
3839e657 | 444 | return 1; |
efc6f628 | 445 | } |
3839e657 TT |
446 | return 0; |
447 | } | |
28db82a8 TT |
448 | if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) { |
449 | fix_problem(ctx, PR_2_NO_DIRINFO, pctx); | |
450 | return -1; | |
451 | } | |
3839e657 TT |
452 | return 0; |
453 | } | |
454 | ||
455 | /* | |
456 | * Check to make sure a directory entry doesn't contain any illegal | |
457 | * characters. | |
458 | */ | |
1b6bf175 | 459 | static int check_name(e2fsck_t ctx, |
3839e657 | 460 | struct ext2_dir_entry *dirent, |
efc6f628 | 461 | ext2_ino_t dir_ino EXT2FS_ATTR((unused)), |
54434927 | 462 | struct problem_context *pctx) |
3839e657 TT |
463 | { |
464 | int i; | |
465 | int fixup = -1; | |
3839e657 | 466 | int ret = 0; |
efc6f628 | 467 | |
70f4632b | 468 | for ( i = 0; i < ext2fs_dirent_name_len(dirent); i++) { |
3839e657 TT |
469 | if (dirent->name[i] == '/' || dirent->name[i] == '\0') { |
470 | if (fixup < 0) { | |
1b6bf175 | 471 | fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx); |
3839e657 TT |
472 | } |
473 | if (fixup) { | |
474 | dirent->name[i] = '.'; | |
475 | ret = 1; | |
21c84b71 | 476 | } |
3839e657 TT |
477 | } |
478 | } | |
479 | return ret; | |
480 | } | |
481 | ||
aa4115a4 TT |
482 | /* |
483 | * Check the directory filetype (if present) | |
484 | */ | |
485 | static _INLINE_ int check_filetype(e2fsck_t ctx, | |
54434927 TT |
486 | struct ext2_dir_entry *dirent, |
487 | ext2_ino_t dir_ino EXT2FS_ATTR((unused)), | |
488 | struct problem_context *pctx) | |
aa4115a4 | 489 | { |
70f4632b | 490 | int filetype = ext2fs_dirent_file_type(dirent); |
aa4115a4 TT |
491 | int should_be = EXT2_FT_UNKNOWN; |
492 | struct ext2_inode inode; | |
493 | ||
494 | if (!(ctx->fs->super->s_feature_incompat & | |
7847c1d4 TT |
495 | EXT2_FEATURE_INCOMPAT_FILETYPE)) { |
496 | if (filetype == 0 || | |
497 | !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx)) | |
498 | return 0; | |
70f4632b | 499 | ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN); |
7847c1d4 TT |
500 | return 1; |
501 | } | |
aa4115a4 | 502 | |
c5d2f50d | 503 | if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) { |
aa4115a4 | 504 | should_be = EXT2_FT_DIR; |
c5d2f50d | 505 | } else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map, |
aa4115a4 TT |
506 | dirent->inode)) { |
507 | should_be = EXT2_FT_REG_FILE; | |
508 | } else if (ctx->inode_bad_map && | |
c5d2f50d | 509 | ext2fs_test_inode_bitmap2(ctx->inode_bad_map, |
aa4115a4 TT |
510 | dirent->inode)) |
511 | should_be = 0; | |
512 | else { | |
513 | e2fsck_read_inode(ctx, dirent->inode, &inode, | |
514 | "check_filetype"); | |
6fdc7a32 | 515 | should_be = ext2_file_type(inode.i_mode); |
aa4115a4 TT |
516 | } |
517 | if (filetype == should_be) | |
518 | return 0; | |
519 | pctx->num = should_be; | |
520 | ||
521 | if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE, | |
522 | pctx) == 0) | |
523 | return 0; | |
efc6f628 | 524 | |
70f4632b | 525 | ext2fs_dirent_set_file_type(dirent, should_be); |
aa4115a4 TT |
526 | return 1; |
527 | } | |
528 | ||
8fdc9985 TT |
529 | #ifdef ENABLE_HTREE |
530 | static void parse_int_node(ext2_filsys fs, | |
6dc64392 | 531 | struct ext2_db_entry2 *db, |
8fdc9985 TT |
532 | struct check_dir_struct *cd, |
533 | struct dx_dir_info *dx_dir, | |
07307114 | 534 | char *block_buf, int failed_csum) |
8fdc9985 TT |
535 | { |
536 | struct ext2_dx_root_info *root; | |
537 | struct ext2_dx_entry *ent; | |
538 | struct ext2_dx_countlimit *limit; | |
539 | struct dx_dirblock_info *dx_db; | |
ad4fa466 | 540 | int i, expect_limit, count; |
8fdc9985 TT |
541 | blk_t blk; |
542 | ext2_dirhash_t min_hash = 0xffffffff; | |
543 | ext2_dirhash_t max_hash = 0; | |
ad4fa466 | 544 | ext2_dirhash_t hash = 0, prev_hash; |
07307114 | 545 | int csum_size = 0; |
8fdc9985 TT |
546 | |
547 | if (db->blockcnt == 0) { | |
548 | root = (struct ext2_dx_root_info *) (block_buf + 24); | |
efc6f628 | 549 | |
8fdc9985 TT |
550 | #ifdef DX_DEBUG |
551 | printf("Root node dump:\n"); | |
8deb80a5 | 552 | printf("\t Reserved zero: %u\n", root->reserved_zero); |
8fdc9985 TT |
553 | printf("\t Hash Version: %d\n", root->hash_version); |
554 | printf("\t Info length: %d\n", root->info_length); | |
555 | printf("\t Indirect levels: %d\n", root->indirect_levels); | |
556 | printf("\t Flags: %d\n", root->unused_flags); | |
557 | #endif | |
558 | ||
559 | ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length); | |
07307114 DW |
560 | |
561 | if (failed_csum && | |
562 | (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) || | |
563 | fix_problem(cd->ctx, PR_2_HTREE_ROOT_CSUM_INVALID, | |
564 | &cd->pctx))) | |
565 | goto clear_and_exit; | |
8fdc9985 TT |
566 | } else { |
567 | ent = (struct ext2_dx_entry *) (block_buf+8); | |
07307114 DW |
568 | |
569 | if (failed_csum && | |
570 | (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) || | |
571 | fix_problem(cd->ctx, PR_2_HTREE_NODE_CSUM_INVALID, | |
572 | &cd->pctx))) | |
573 | goto clear_and_exit; | |
8fdc9985 | 574 | } |
07307114 | 575 | |
8fdc9985 TT |
576 | limit = (struct ext2_dx_countlimit *) ent; |
577 | ||
578 | #ifdef DX_DEBUG | |
efc6f628 | 579 | printf("Number of entries (count): %d\n", |
8132d840 | 580 | ext2fs_le16_to_cpu(limit->count)); |
efc6f628 | 581 | printf("Number of entries (limit): %d\n", |
8132d840 | 582 | ext2fs_le16_to_cpu(limit->limit)); |
8fdc9985 TT |
583 | #endif |
584 | ||
8132d840 | 585 | count = ext2fs_le16_to_cpu(limit->count); |
07307114 DW |
586 | if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, |
587 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
588 | csum_size = sizeof(struct ext2_dx_tail); | |
589 | expect_limit = (fs->blocksize - | |
590 | (csum_size + ((char *) ent - block_buf))) / | |
591 | sizeof(struct ext2_dx_entry); | |
8132d840 TT |
592 | if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) { |
593 | cd->pctx.num = ext2fs_le16_to_cpu(limit->limit); | |
ad4fa466 TT |
594 | if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx)) |
595 | goto clear_and_exit; | |
596 | } | |
8132d840 TT |
597 | if (count > expect_limit) { |
598 | cd->pctx.num = count; | |
ad4fa466 TT |
599 | if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx)) |
600 | goto clear_and_exit; | |
601 | count = expect_limit; | |
602 | } | |
efc6f628 | 603 | |
ad4fa466 TT |
604 | for (i=0; i < count; i++) { |
605 | prev_hash = hash; | |
8132d840 | 606 | hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0; |
8fdc9985 | 607 | #ifdef DX_DEBUG |
8deb80a5 | 608 | printf("Entry #%d: Hash 0x%08x, block %u\n", i, |
8132d840 | 609 | hash, ext2fs_le32_to_cpu(ent[i].block)); |
8fdc9985 | 610 | #endif |
8132d840 | 611 | blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff; |
8fdc9985 | 612 | /* Check to make sure the block is valid */ |
977ac873 | 613 | if (blk >= (blk_t) dx_dir->numblocks) { |
b7a00563 | 614 | cd->pctx.blk = blk; |
8fdc9985 | 615 | if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK, |
ad4fa466 TT |
616 | &cd->pctx)) |
617 | goto clear_and_exit; | |
977ac873 | 618 | continue; |
8fdc9985 | 619 | } |
ad4fa466 TT |
620 | if (hash < prev_hash && |
621 | fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx)) | |
622 | goto clear_and_exit; | |
8fdc9985 TT |
623 | dx_db = &dx_dir->dx_block[blk]; |
624 | if (dx_db->flags & DX_FLAG_REFERENCED) { | |
625 | dx_db->flags |= DX_FLAG_DUP_REF; | |
626 | } else { | |
627 | dx_db->flags |= DX_FLAG_REFERENCED; | |
628 | dx_db->parent = db->blockcnt; | |
629 | } | |
630 | if (hash < min_hash) | |
631 | min_hash = hash; | |
632 | if (hash > max_hash) | |
633 | max_hash = hash; | |
634 | dx_db->node_min_hash = hash; | |
8132d840 | 635 | if ((i+1) < count) |
efc6f628 | 636 | dx_db->node_max_hash = |
8132d840 | 637 | ext2fs_le32_to_cpu(ent[i+1].hash) & ~1; |
8fdc9985 TT |
638 | else { |
639 | dx_db->node_max_hash = 0xfffffffe; | |
640 | dx_db->flags |= DX_FLAG_LAST; | |
641 | } | |
642 | if (i == 0) | |
643 | dx_db->flags |= DX_FLAG_FIRST; | |
644 | } | |
645 | #ifdef DX_DEBUG | |
646 | printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n", | |
647 | db->blockcnt, min_hash, max_hash); | |
648 | #endif | |
649 | dx_db = &dx_dir->dx_block[db->blockcnt]; | |
650 | dx_db->min_hash = min_hash; | |
651 | dx_db->max_hash = max_hash; | |
ad4fa466 TT |
652 | return; |
653 | ||
654 | clear_and_exit: | |
655 | clear_htree(cd->ctx, cd->pctx.ino); | |
656 | dx_dir->numblocks = 0; | |
07307114 | 657 | e2fsck_rehash_dir_later(cd->ctx, cd->pctx.ino); |
8fdc9985 TT |
658 | } |
659 | #endif /* ENABLE_HTREE */ | |
aa4115a4 | 660 | |
e70ae99e TT |
661 | /* |
662 | * Given a busted directory, try to salvage it somehow. | |
efc6f628 | 663 | * |
e70ae99e | 664 | */ |
ad4fa466 | 665 | static void salvage_directory(ext2_filsys fs, |
e70ae99e TT |
666 | struct ext2_dir_entry *dirent, |
667 | struct ext2_dir_entry *prev, | |
82ad476d DW |
668 | unsigned int *offset, |
669 | unsigned int block_len) | |
e70ae99e TT |
670 | { |
671 | char *cp = (char *) dirent; | |
8a480350 TT |
672 | int left; |
673 | unsigned int rec_len, prev_rec_len; | |
70f4632b | 674 | unsigned int name_len = ext2fs_dirent_name_len(dirent); |
e70ae99e | 675 | |
8a480350 | 676 | (void) ext2fs_get_rec_len(fs, dirent, &rec_len); |
82ad476d | 677 | left = block_len - *offset - rec_len; |
5dd77dbe | 678 | |
e70ae99e TT |
679 | /* |
680 | * Special case of directory entry of size 8: copy what's left | |
681 | * of the directory block up to cover up the invalid hole. | |
682 | */ | |
5dd77dbe | 683 | if ((left >= 12) && (rec_len == 8)) { |
e70ae99e TT |
684 | memmove(cp, cp+8, left); |
685 | memset(cp + left, 0, 8); | |
ad4fa466 TT |
686 | return; |
687 | } | |
688 | /* | |
689 | * If the directory entry overruns the end of the directory | |
690 | * block, and the name is small enough to fit, then adjust the | |
691 | * record length. | |
692 | */ | |
693 | if ((left < 0) && | |
8a480350 | 694 | ((int) rec_len + left > 8) && |
68477355 | 695 | ((int) name_len + 8 <= (int) rec_len + left) && |
ad4fa466 TT |
696 | dirent->inode <= fs->super->s_inodes_count && |
697 | strnlen(dirent->name, name_len) == name_len) { | |
8a480350 | 698 | (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent); |
ad4fa466 | 699 | return; |
e70ae99e TT |
700 | } |
701 | /* | |
575307cc KS |
702 | * If the record length of the directory entry is a multiple |
703 | * of four, and not too big, such that it is valid, let the | |
704 | * previous directory entry absorb the invalid one. | |
e70ae99e | 705 | */ |
5dd77dbe | 706 | if (prev && rec_len && (rec_len % 4) == 0 && |
82ad476d | 707 | (*offset + rec_len <= block_len)) { |
8a480350 TT |
708 | (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len); |
709 | prev_rec_len += rec_len; | |
710 | (void) ext2fs_set_rec_len(fs, prev_rec_len, prev); | |
5dd77dbe | 711 | *offset += rec_len; |
ad4fa466 | 712 | return; |
e70ae99e TT |
713 | } |
714 | /* | |
715 | * Default salvage method --- kill all of the directory | |
716 | * entries for the rest of the block. We will either try to | |
717 | * absorb it into the previous directory entry, or create a | |
718 | * new empty directory entry the rest of the directory block. | |
719 | */ | |
720 | if (prev) { | |
8a480350 | 721 | (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len); |
82ad476d | 722 | prev_rec_len += block_len - *offset; |
8a480350 | 723 | (void) ext2fs_set_rec_len(fs, prev_rec_len, prev); |
ad4fa466 | 724 | *offset = fs->blocksize; |
e70ae99e | 725 | } else { |
82ad476d | 726 | rec_len = block_len - *offset; |
8a480350 | 727 | (void) ext2fs_set_rec_len(fs, rec_len, dirent); |
70f4632b JK |
728 | ext2fs_dirent_set_name_len(dirent, 0); |
729 | ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN); | |
e70ae99e | 730 | dirent->inode = 0; |
e70ae99e | 731 | } |
e70ae99e TT |
732 | } |
733 | ||
6582dbe9 ZL |
734 | static int is_last_entry(ext2_filsys fs, int inline_data_size, |
735 | unsigned int offset, int csum_size) | |
736 | { | |
737 | if (inline_data_size) | |
738 | return (offset < inline_data_size); | |
739 | else | |
740 | return (offset < fs->blocksize - csum_size); | |
741 | } | |
742 | ||
17641bf2 DW |
743 | static errcode_t insert_dirent_tail(ext2_filsys fs, void *dirbuf) |
744 | { | |
745 | struct ext2_dir_entry *d; | |
746 | void *top; | |
747 | struct ext2_dir_entry_tail *t; | |
748 | unsigned int rec_len; | |
749 | ||
750 | d = dirbuf; | |
751 | top = EXT2_DIRENT_TAIL(dirbuf, fs->blocksize); | |
752 | ||
753 | rec_len = d->rec_len; | |
754 | while (rec_len && !(rec_len & 0x3)) { | |
755 | d = (struct ext2_dir_entry *)(((char *)d) + rec_len); | |
756 | if (((void *)d) + d->rec_len >= top) | |
757 | break; | |
758 | rec_len = d->rec_len; | |
759 | } | |
760 | ||
761 | if (d != top) { | |
762 | size_t min_size = EXT2_DIR_REC_LEN( | |
763 | ext2fs_dirent_name_len(dirbuf)); | |
764 | if (min_size > d->rec_len - sizeof(struct ext2_dir_entry_tail)) | |
765 | return EXT2_ET_DIR_NO_SPACE_FOR_CSUM; | |
766 | d->rec_len -= sizeof(struct ext2_dir_entry_tail); | |
767 | } | |
768 | ||
769 | t = (struct ext2_dir_entry_tail *)top; | |
770 | if (t->det_reserved_zero1 || | |
771 | t->det_rec_len != sizeof(struct ext2_dir_entry_tail) || | |
772 | t->det_reserved_name_len != EXT2_DIR_NAME_LEN_CSUM) | |
773 | ext2fs_initialize_dirent_tail(fs, t); | |
774 | ||
775 | return 0; | |
776 | } | |
777 | ||
3839e657 | 778 | static int check_dir_block(ext2_filsys fs, |
6dc64392 | 779 | struct ext2_db_entry2 *db, |
54dc7ca2 | 780 | void *priv_data) |
3839e657 | 781 | { |
8fdc9985 TT |
782 | struct dx_dir_info *dx_dir; |
783 | #ifdef ENABLE_HTREE | |
784 | struct dx_dirblock_info *dx_db = 0; | |
785 | #endif /* ENABLE_HTREE */ | |
6582dbe9 | 786 | struct ext2_dir_entry *dirent, *prev, dot, dotdot; |
8fdc9985 | 787 | ext2_dirhash_t hash; |
54434927 | 788 | unsigned int offset = 0; |
3839e657 | 789 | int dir_modified = 0; |
21c84b71 | 790 | int dot_state; |
03fa6f8a | 791 | unsigned int rec_len; |
6dc64392 | 792 | blk64_t block_nr = db->blk; |
86c627ec | 793 | ext2_ino_t ino = db->ino; |
28db82a8 | 794 | ext2_ino_t subdir_parent; |
21c84b71 | 795 | __u16 links; |
54dc7ca2 | 796 | struct check_dir_struct *cd; |
1b6bf175 TT |
797 | char *buf; |
798 | e2fsck_t ctx; | |
3c7c6d73 | 799 | problem_t problem; |
ea1959f0 | 800 | struct ext2_dx_root_info *root; |
e8254bfd | 801 | struct ext2_dx_countlimit *limit; |
0926668d TT |
802 | static dict_t de_dict; |
803 | struct problem_context pctx; | |
804 | int dups_found = 0; | |
28db82a8 | 805 | int ret; |
e8548796 | 806 | int dx_csum_size = 0, de_csum_size = 0; |
81683c6a | 807 | int failed_csum = 0; |
e8548796 | 808 | int is_leaf = 1; |
24997f1c | 809 | size_t inline_data_size = 0; |
6582dbe9 | 810 | int filetype = 0; |
1b6bf175 | 811 | |
54dc7ca2 | 812 | cd = (struct check_dir_struct *) priv_data; |
1b6bf175 TT |
813 | buf = cd->buf; |
814 | ctx = cd->ctx; | |
f8188fff | 815 | |
49a7360b | 816 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART) |
4cae0452 | 817 | return DIRENT_ABORT; |
efc6f628 | 818 | |
4cae0452 TT |
819 | if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max)) |
820 | return DIRENT_ABORT; | |
efc6f628 | 821 | |
07307114 | 822 | if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, |
e8548796 | 823 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { |
07307114 | 824 | dx_csum_size = sizeof(struct ext2_dx_tail); |
e8548796 DW |
825 | de_csum_size = sizeof(struct ext2_dir_entry_tail); |
826 | } | |
07307114 | 827 | |
6582dbe9 ZL |
828 | if (EXT2_HAS_INCOMPAT_FEATURE(fs->super, |
829 | EXT2_FEATURE_INCOMPAT_FILETYPE)) | |
830 | filetype = EXT2_FT_DIR << 8; | |
831 | ||
3839e657 | 832 | /* |
efc6f628 | 833 | * Make sure the inode is still in use (could have been |
3839e657 TT |
834 | * deleted in the duplicate/bad blocks pass. |
835 | */ | |
c5d2f50d | 836 | if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino))) |
3839e657 | 837 | return 0; |
50e1e10f | 838 | |
21c84b71 TT |
839 | cd->pctx.ino = ino; |
840 | cd->pctx.blk = block_nr; | |
841 | cd->pctx.blkcount = db->blockcnt; | |
842 | cd->pctx.ino2 = 0; | |
843 | cd->pctx.dirent = 0; | |
844 | cd->pctx.num = 0; | |
845 | ||
6582dbe9 ZL |
846 | if (EXT2_HAS_INCOMPAT_FEATURE(fs->super, |
847 | EXT4_FEATURE_INCOMPAT_INLINE_DATA)) { | |
848 | errcode_t ec; | |
849 | ||
850 | ec = ext2fs_inline_data_size(fs, ino, &inline_data_size); | |
851 | if (ec && ec != EXT2_ET_NO_INLINE_DATA) | |
852 | return DIRENT_ABORT; | |
853 | } | |
854 | ||
855 | if (db->blk == 0 && !inline_data_size) { | |
1b6bf175 | 856 | if (allocate_dir_block(ctx, db, buf, &cd->pctx)) |
50e1e10f TT |
857 | return 0; |
858 | block_nr = db->blk; | |
859 | } | |
efc6f628 | 860 | |
3839e657 TT |
861 | if (db->blockcnt) |
862 | dot_state = 2; | |
863 | else | |
864 | dot_state = 0; | |
865 | ||
0926668d TT |
866 | if (ctx->dirs_to_hash && |
867 | ext2fs_u32_list_test(ctx->dirs_to_hash, ino)) | |
868 | dups_found++; | |
869 | ||
3839e657 | 870 | #if 0 |
f3db3566 | 871 | printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr, |
3839e657 TT |
872 | db->blockcnt, ino); |
873 | #endif | |
efc6f628 | 874 | |
f85a9ae6 | 875 | ehandler_operation(_("reading directory block")); |
6582dbe9 ZL |
876 | if (inline_data_size) |
877 | cd->pctx.errcode = ext2fs_inline_data_get(fs, ino, 0, buf, 0); | |
878 | else | |
879 | cd->pctx.errcode = ext2fs_read_dir_block4(fs, block_nr, | |
880 | buf, 0, ino); | |
e94bc631 | 881 | ehandler_operation(0); |
b9852cd8 TT |
882 | if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED) |
883 | cd->pctx.errcode = 0; /* We'll handle this ourselves */ | |
81683c6a DW |
884 | else if (cd->pctx.errcode == EXT2_ET_DIR_CSUM_INVALID) { |
885 | cd->pctx.errcode = 0; /* We'll handle this ourselves */ | |
886 | failed_csum = 1; | |
887 | } | |
1b6bf175 | 888 | if (cd->pctx.errcode) { |
e8548796 | 889 | char *buf2; |
08b21301 TT |
890 | if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) { |
891 | ctx->flags |= E2F_FLAG_ABORT; | |
892 | return DIRENT_ABORT; | |
893 | } | |
e8548796 DW |
894 | ext2fs_new_dir_block(fs, db->blockcnt == 0 ? ino : 0, |
895 | EXT2_ROOT_INO, &buf2); | |
896 | memcpy(buf, buf2, fs->blocksize); | |
897 | ext2fs_free_mem(&buf2); | |
3839e657 | 898 | } |
8fdc9985 TT |
899 | #ifdef ENABLE_HTREE |
900 | dx_dir = e2fsck_get_dx_dir_info(ctx, ino); | |
62acaa1d | 901 | if (dx_dir && dx_dir->numblocks) { |
8fdc9985 | 902 | if (db->blockcnt >= dx_dir->numblocks) { |
efc6f628 | 903 | if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK, |
d45edec0 TT |
904 | &pctx)) { |
905 | clear_htree(ctx, ino); | |
906 | dx_dir->numblocks = 0; | |
907 | dx_db = 0; | |
908 | goto out_htree; | |
909 | } | |
910 | fatal_error(ctx, _("Can not continue.")); | |
8fdc9985 TT |
911 | } |
912 | dx_db = &dx_dir->dx_block[db->blockcnt]; | |
913 | dx_db->type = DX_DIRBLOCK_LEAF; | |
914 | dx_db->phys = block_nr; | |
915 | dx_db->min_hash = ~0; | |
916 | dx_db->max_hash = 0; | |
efc6f628 | 917 | |
8fdc9985 | 918 | dirent = (struct ext2_dir_entry *) buf; |
8a480350 | 919 | (void) ext2fs_get_rec_len(fs, dirent, &rec_len); |
e8254bfd | 920 | limit = (struct ext2_dx_countlimit *) (buf+8); |
8fdc9985 | 921 | if (db->blockcnt == 0) { |
ea1959f0 | 922 | root = (struct ext2_dx_root_info *) (buf + 24); |
8fdc9985 TT |
923 | dx_db->type = DX_DIRBLOCK_ROOT; |
924 | dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST; | |
ea1959f0 TT |
925 | if ((root->reserved_zero || |
926 | root->info_length < 8 || | |
927 | root->indirect_levels > 1) && | |
928 | fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) { | |
929 | clear_htree(ctx, ino); | |
930 | dx_dir->numblocks = 0; | |
931 | dx_db = 0; | |
f77704e4 | 932 | } |
ea1959f0 | 933 | dx_dir->hashversion = root->hash_version; |
f77704e4 TT |
934 | if ((dx_dir->hashversion <= EXT2_HASH_TEA) && |
935 | (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH)) | |
936 | dx_dir->hashversion += 3; | |
ad4fa466 | 937 | dx_dir->depth = root->indirect_levels + 1; |
8fdc9985 | 938 | } else if ((dirent->inode == 0) && |
5dd77dbe | 939 | (rec_len == fs->blocksize) && |
70f4632b | 940 | (ext2fs_dirent_name_len(dirent) == 0) && |
efc6f628 | 941 | (ext2fs_le16_to_cpu(limit->limit) == |
07307114 | 942 | ((fs->blocksize - (8 + dx_csum_size)) / |
8132d840 | 943 | sizeof(struct ext2_dx_entry)))) |
8fdc9985 | 944 | dx_db->type = DX_DIRBLOCK_NODE; |
e8548796 | 945 | is_leaf = 0; |
8fdc9985 | 946 | } |
d45edec0 | 947 | out_htree: |
8fdc9985 | 948 | #endif /* ENABLE_HTREE */ |
3839e657 | 949 | |
e8548796 | 950 | /* Verify checksum. */ |
6582dbe9 | 951 | if (is_leaf && de_csum_size && !inline_data_size) { |
e8548796 DW |
952 | /* No space for csum? Rebuild dirs in pass 3A. */ |
953 | if (!ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) { | |
954 | de_csum_size = 0; | |
955 | if (e2fsck_dir_will_be_rehashed(ctx, ino)) | |
956 | goto skip_checksum; | |
957 | if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_MISSING_CSUM, | |
958 | &cd->pctx)) | |
959 | goto skip_checksum; | |
960 | e2fsck_rehash_dir_later(ctx, ino); | |
961 | goto skip_checksum; | |
962 | } | |
963 | if (failed_csum) { | |
964 | char *buf2; | |
965 | if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_CSUM_INVALID, | |
966 | &cd->pctx)) | |
967 | goto skip_checksum; | |
968 | ext2fs_new_dir_block(fs, | |
969 | db->blockcnt == 0 ? ino : 0, | |
970 | EXT2_ROOT_INO, &buf2); | |
971 | memcpy(buf, buf2, fs->blocksize); | |
972 | ext2fs_free_mem(&buf2); | |
973 | dir_modified++; | |
974 | failed_csum = 0; | |
975 | } | |
976 | } | |
977 | /* htree nodes don't use fake dirents to store checksums */ | |
978 | if (!is_leaf) | |
979 | de_csum_size = 0; | |
980 | ||
981 | skip_checksum: | |
0926668d | 982 | dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp); |
e70ae99e | 983 | prev = 0; |
3839e657 | 984 | do { |
3971bfe8 | 985 | dgrp_t group; |
49a7360b | 986 | ext2_ino_t first_unused_inode; |
70f4632b | 987 | unsigned int name_len; |
49a7360b | 988 | |
1b6bf175 | 989 | problem = 0; |
6582dbe9 ZL |
990 | if (!inline_data_size || dot_state > 1) { |
991 | dirent = (struct ext2_dir_entry *) (buf + offset); | |
992 | (void) ext2fs_get_rec_len(fs, dirent, &rec_len); | |
993 | cd->pctx.dirent = dirent; | |
994 | cd->pctx.num = offset; | |
995 | if (((offset + rec_len) > fs->blocksize) || | |
996 | (rec_len < 12) || | |
997 | ((rec_len % 4) != 0) || | |
998 | ((ext2fs_dirent_name_len(dirent) + 8) > rec_len)) { | |
82ad476d DW |
999 | if (fix_problem(ctx, PR_2_DIR_CORRUPTED, |
1000 | &cd->pctx)) { | |
1001 | salvage_directory(fs, dirent, prev, | |
1002 | &offset, | |
1003 | fs->blocksize - | |
1004 | de_csum_size); | |
6582dbe9 ZL |
1005 | dir_modified++; |
1006 | continue; | |
1007 | } else | |
1008 | goto abort_free_dict; | |
1009 | } | |
1010 | } else { | |
1011 | if (dot_state == 0) { | |
1012 | memset(&dot, 0, sizeof(dot)); | |
1013 | dirent = ˙ | |
1014 | dirent->inode = ino; | |
1015 | dirent->rec_len = EXT2_DIR_REC_LEN(1); | |
1016 | dirent->name_len = 1 | filetype; | |
1017 | dirent->name[0] = '.'; | |
1018 | } else if (dot_state == 1) { | |
1019 | memset(&dotdot, 0, sizeof(dotdot)); | |
1020 | dirent = &dotdot; | |
1021 | dirent->inode = | |
1022 | ((struct ext2_dir_entry *)buf)->inode; | |
1023 | dirent->rec_len = EXT2_DIR_REC_LEN(2); | |
1024 | dirent->name_len = 2 | filetype; | |
1025 | dirent->name[0] = '.'; | |
1026 | dirent->name[1] = '.'; | |
1027 | } else { | |
1028 | fatal_error(ctx, _("Can not continue.")); | |
1029 | } | |
1030 | cd->pctx.dirent = dirent; | |
1031 | cd->pctx.num = offset; | |
3839e657 | 1032 | } |
50e1e10f | 1033 | |
e70ae99e | 1034 | if (dot_state == 0) { |
1b6bf175 | 1035 | if (check_dot(ctx, dirent, ino, &cd->pctx)) |
3839e657 | 1036 | dir_modified++; |
e70ae99e | 1037 | } else if (dot_state == 1) { |
28db82a8 TT |
1038 | ret = check_dotdot(ctx, dirent, ino, &cd->pctx); |
1039 | if (ret < 0) | |
0926668d | 1040 | goto abort_free_dict; |
28db82a8 | 1041 | if (ret) |
3839e657 TT |
1042 | dir_modified++; |
1043 | } else if (dirent->inode == ino) { | |
1b6bf175 TT |
1044 | problem = PR_2_LINK_DOT; |
1045 | if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) { | |
3839e657 TT |
1046 | dirent->inode = 0; |
1047 | dir_modified++; | |
21c84b71 | 1048 | goto next; |
3839e657 TT |
1049 | } |
1050 | } | |
efc6f628 | 1051 | if (!dirent->inode) |
3839e657 | 1052 | goto next; |
efc6f628 | 1053 | |
3839e657 TT |
1054 | /* |
1055 | * Make sure the inode listed is a legal one. | |
efc6f628 | 1056 | */ |
70f4632b | 1057 | name_len = ext2fs_dirent_name_len(dirent); |
3839e657 | 1058 | if (((dirent->inode != EXT2_ROOT_INO) && |
7f88b043 | 1059 | (dirent->inode < EXT2_FIRST_INODE(fs->super))) || |
3839e657 | 1060 | (dirent->inode > fs->super->s_inodes_count)) { |
1b6bf175 | 1061 | problem = PR_2_BAD_INO; |
1b6bf175 | 1062 | } else if (ctx->inode_bb_map && |
c5d2f50d | 1063 | (ext2fs_test_inode_bitmap2(ctx->inode_bb_map, |
1b6bf175 TT |
1064 | dirent->inode))) { |
1065 | /* | |
1066 | * If the inode is in a bad block, offer to | |
1067 | * clear it. | |
1068 | */ | |
1069 | problem = PR_2_BB_INODE; | |
70f4632b | 1070 | } else if ((dot_state > 1) && (name_len == 1) && |
1b6bf175 TT |
1071 | (dirent->name[0] == '.')) { |
1072 | /* | |
1073 | * If there's a '.' entry in anything other | |
1074 | * than the first directory entry, it's a | |
1075 | * duplicate entry that should be removed. | |
1076 | */ | |
1077 | problem = PR_2_DUP_DOT; | |
70f4632b | 1078 | } else if ((dot_state > 1) && (name_len == 2) && |
efc6f628 | 1079 | (dirent->name[0] == '.') && |
1b6bf175 TT |
1080 | (dirent->name[1] == '.')) { |
1081 | /* | |
1082 | * If there's a '..' entry in anything other | |
1083 | * than the second directory entry, it's a | |
1084 | * duplicate entry that should be removed. | |
1085 | */ | |
1086 | problem = PR_2_DUP_DOT_DOT; | |
e70ae99e | 1087 | } else if ((dot_state > 1) && |
1b6bf175 TT |
1088 | (dirent->inode == EXT2_ROOT_INO)) { |
1089 | /* | |
1090 | * Don't allow links to the root directory. | |
1091 | * We check this specially to make sure we | |
1092 | * catch this error case even if the root | |
1093 | * directory hasn't been created yet. | |
1094 | */ | |
1095 | problem = PR_2_LINK_ROOT; | |
70f4632b | 1096 | } else if ((dot_state > 1) && (name_len == 0)) { |
c40db6d5 TT |
1097 | /* |
1098 | * Don't allow zero-length directory names. | |
1099 | */ | |
1100 | problem = PR_2_NULL_NAME; | |
21c84b71 TT |
1101 | } |
1102 | ||
1b6bf175 TT |
1103 | if (problem) { |
1104 | if (fix_problem(ctx, problem, &cd->pctx)) { | |
21c84b71 TT |
1105 | dirent->inode = 0; |
1106 | dir_modified++; | |
1107 | goto next; | |
1b6bf175 TT |
1108 | } else { |
1109 | ext2fs_unmark_valid(fs); | |
1110 | if (problem == PR_2_BAD_INO) | |
1111 | goto next; | |
21c84b71 | 1112 | } |
3839e657 TT |
1113 | } |
1114 | ||
1115 | /* | |
1116 | * If the inode was marked as having bad fields in | |
1117 | * pass1, process it and offer to fix/clear it. | |
1118 | * (We wait until now so that we can display the | |
1119 | * pathname to the user.) | |
1120 | */ | |
1b6bf175 | 1121 | if (ctx->inode_bad_map && |
c5d2f50d | 1122 | ext2fs_test_inode_bitmap2(ctx->inode_bad_map, |
3839e657 | 1123 | dirent->inode)) { |
e72a9ba3 | 1124 | if (e2fsck_process_bad_inode(ctx, ino, |
bcf9c5d4 TT |
1125 | dirent->inode, |
1126 | buf + fs->blocksize)) { | |
3839e657 TT |
1127 | dirent->inode = 0; |
1128 | dir_modified++; | |
1129 | goto next; | |
1130 | } | |
a02ce9df | 1131 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
08b21301 | 1132 | return DIRENT_ABORT; |
3839e657 TT |
1133 | } |
1134 | ||
49a7360b JS |
1135 | group = ext2fs_group_of_ino(fs, dirent->inode); |
1136 | first_unused_inode = group * fs->super->s_inodes_per_group + | |
1137 | 1 + fs->super->s_inodes_per_group - | |
d7cca6b0 | 1138 | ext2fs_bg_itable_unused(fs, group); |
49a7360b JS |
1139 | cd->pctx.group = group; |
1140 | ||
1141 | /* | |
42e89ce7 TT |
1142 | * Check if the inode was missed out because |
1143 | * _INODE_UNINIT flag was set or bg_itable_unused was | |
1144 | * incorrect. If so, clear the _INODE_UNINIT flag and | |
1145 | * restart e2fsck. In the future it would be nice if | |
1146 | * we could call a function in pass1.c that checks the | |
1147 | * newly visible inodes. | |
49a7360b | 1148 | */ |
cd65a24e | 1149 | if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) { |
6267ee49 | 1150 | pctx.num = dirent->inode; |
49a7360b JS |
1151 | if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT, |
1152 | &cd->pctx)){ | |
e633b58a | 1153 | ext2fs_bg_flags_clear(fs, group, |
732c8cd5 | 1154 | EXT2_BG_INODE_UNINIT); |
42e89ce7 | 1155 | ext2fs_mark_super_dirty(fs); |
6267ee49 | 1156 | ctx->flags |= E2F_FLAG_RESTART_LATER; |
49a7360b JS |
1157 | } else { |
1158 | ext2fs_unmark_valid(fs); | |
1159 | if (problem == PR_2_BAD_INO) | |
1160 | goto next; | |
1161 | } | |
1162 | } else if (dirent->inode >= first_unused_inode) { | |
6267ee49 | 1163 | pctx.num = dirent->inode; |
49a7360b | 1164 | if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){ |
d7cca6b0 | 1165 | ext2fs_bg_itable_unused_set(fs, group, 0); |
49a7360b | 1166 | ext2fs_mark_super_dirty(fs); |
6267ee49 | 1167 | ctx->flags |= E2F_FLAG_RESTART_LATER; |
49a7360b JS |
1168 | } else { |
1169 | ext2fs_unmark_valid(fs); | |
1170 | if (problem == PR_2_BAD_INO) | |
1171 | goto next; | |
1172 | } | |
1173 | } | |
1174 | ||
0433c1f1 TT |
1175 | /* |
1176 | * Offer to clear unused inodes; if we are going to be | |
1177 | * restarting the scan due to bg_itable_unused being | |
1178 | * wrong, then don't clear any inodes to avoid zapping | |
1179 | * inodes that were skipped during pass1 due to an | |
1180 | * incorrect bg_itable_unused; we'll get any real | |
1181 | * problems after we restart. | |
1182 | */ | |
1183 | if (!(ctx->flags & E2F_FLAG_RESTART_LATER) && | |
97d26ce9 TT |
1184 | !(ext2fs_test_inode_bitmap2(ctx->inode_used_map, |
1185 | dirent->inode))) | |
49a7360b | 1186 | problem = PR_2_UNUSED_INODE; |
49a7360b JS |
1187 | |
1188 | if (problem) { | |
1189 | if (fix_problem(ctx, problem, &cd->pctx)) { | |
1190 | dirent->inode = 0; | |
1191 | dir_modified++; | |
1192 | goto next; | |
1193 | } else { | |
1194 | ext2fs_unmark_valid(fs); | |
1195 | if (problem == PR_2_BAD_INO) | |
1196 | goto next; | |
1197 | } | |
1198 | } | |
1199 | ||
1b6bf175 TT |
1200 | if (check_name(ctx, dirent, ino, &cd->pctx)) |
1201 | dir_modified++; | |
1202 | ||
aa4115a4 TT |
1203 | if (check_filetype(ctx, dirent, ino, &cd->pctx)) |
1204 | dir_modified++; | |
1205 | ||
8fdc9985 TT |
1206 | #ifdef ENABLE_HTREE |
1207 | if (dx_db) { | |
1208 | ext2fs_dirhash(dx_dir->hashversion, dirent->name, | |
70f4632b | 1209 | ext2fs_dirent_name_len(dirent), |
503f9e7f | 1210 | fs->super->s_hash_seed, &hash, 0); |
8fdc9985 TT |
1211 | if (hash < dx_db->min_hash) |
1212 | dx_db->min_hash = hash; | |
1213 | if (hash > dx_db->max_hash) | |
1214 | dx_db->max_hash = hash; | |
1215 | } | |
1216 | #endif | |
1217 | ||
3839e657 TT |
1218 | /* |
1219 | * If this is a directory, then mark its parent in its | |
1220 | * dir_info structure. If the parent field is already | |
1221 | * filled in, then this directory has more than one | |
1222 | * hard link. We assume the first link is correct, | |
1223 | * and ask the user if he/she wants to clear this one. | |
1224 | */ | |
e70ae99e | 1225 | if ((dot_state > 1) && |
c5d2f50d | 1226 | (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, |
3839e657 | 1227 | dirent->inode))) { |
28db82a8 TT |
1228 | if (e2fsck_dir_info_get_parent(ctx, dirent->inode, |
1229 | &subdir_parent)) { | |
1b6bf175 TT |
1230 | cd->pctx.ino = dirent->inode; |
1231 | fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx); | |
0926668d | 1232 | goto abort_free_dict; |
3839e657 | 1233 | } |
28db82a8 TT |
1234 | if (subdir_parent) { |
1235 | cd->pctx.ino2 = subdir_parent; | |
1b6bf175 | 1236 | if (fix_problem(ctx, PR_2_LINK_DIR, |
21c84b71 | 1237 | &cd->pctx)) { |
3839e657 TT |
1238 | dirent->inode = 0; |
1239 | dir_modified++; | |
1240 | goto next; | |
21c84b71 TT |
1241 | } |
1242 | cd->pctx.ino2 = 0; | |
28db82a8 | 1243 | } else { |
efc6f628 | 1244 | (void) e2fsck_dir_info_set_parent(ctx, |
28db82a8 TT |
1245 | dirent->inode, ino); |
1246 | } | |
3839e657 | 1247 | } |
0926668d TT |
1248 | |
1249 | if (dups_found) { | |
1250 | ; | |
1251 | } else if (dict_lookup(&de_dict, dirent)) { | |
1252 | clear_problem_context(&pctx); | |
1253 | pctx.ino = ino; | |
1254 | pctx.dirent = dirent; | |
1255 | fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx); | |
e8548796 | 1256 | e2fsck_rehash_dir_later(ctx, ino); |
0926668d TT |
1257 | dups_found++; |
1258 | } else | |
1259 | dict_alloc_insert(&de_dict, dirent, dirent); | |
efc6f628 | 1260 | |
1b6bf175 TT |
1261 | ext2fs_icount_increment(ctx->inode_count, dirent->inode, |
1262 | &links); | |
21c84b71 | 1263 | if (links > 1) |
1b6bf175 TT |
1264 | ctx->fs_links_count++; |
1265 | ctx->fs_total_count++; | |
3839e657 | 1266 | next: |
e70ae99e | 1267 | prev = dirent; |
5dd77dbe | 1268 | if (dir_modified) |
8a480350 | 1269 | (void) ext2fs_get_rec_len(fs, dirent, &rec_len); |
6582dbe9 ZL |
1270 | if (!inline_data_size || dot_state > 1) { |
1271 | offset += rec_len; | |
1272 | } else { | |
1273 | if (dot_state == 1) | |
1274 | offset = 4; | |
1275 | } | |
e70ae99e | 1276 | dot_state++; |
6582dbe9 | 1277 | } while (is_last_entry(fs, inline_data_size, offset, de_csum_size)); |
3839e657 TT |
1278 | #if 0 |
1279 | printf("\n"); | |
1280 | #endif | |
8fdc9985 TT |
1281 | #ifdef ENABLE_HTREE |
1282 | if (dx_db) { | |
1283 | #ifdef DX_DEBUG | |
1284 | printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n", | |
1285 | db->blockcnt, dx_db->type, | |
1286 | dx_db->min_hash, dx_db->max_hash); | |
1287 | #endif | |
b7a00563 | 1288 | cd->pctx.dir = cd->pctx.ino; |
8fdc9985 TT |
1289 | if ((dx_db->type == DX_DIRBLOCK_ROOT) || |
1290 | (dx_db->type == DX_DIRBLOCK_NODE)) | |
81683c6a | 1291 | parse_int_node(fs, db, cd, dx_dir, buf, failed_csum); |
8fdc9985 TT |
1292 | } |
1293 | #endif /* ENABLE_HTREE */ | |
e8548796 | 1294 | |
6582dbe9 ZL |
1295 | if (inline_data_size) { |
1296 | if (offset != inline_data_size) { | |
1297 | cd->pctx.num = rec_len + offset - inline_data_size; | |
1298 | if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) { | |
1299 | dirent->rec_len = cd->pctx.num; | |
1300 | dir_modified++; | |
1301 | } | |
1302 | } | |
1303 | } else { | |
1304 | if (offset != fs->blocksize - de_csum_size) { | |
1305 | cd->pctx.num = rec_len - (fs->blocksize - de_csum_size) + | |
1306 | offset; | |
1307 | if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) { | |
1308 | dirent->rec_len = cd->pctx.num; | |
1309 | dir_modified++; | |
1310 | } | |
1b6bf175 | 1311 | } |
3839e657 TT |
1312 | } |
1313 | if (dir_modified) { | |
e8548796 DW |
1314 | /* leaf block with no tail? Rehash dirs later. */ |
1315 | if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, | |
1316 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) && | |
1317 | is_leaf && | |
17641bf2 DW |
1318 | !inline_data_size && |
1319 | !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) { | |
1320 | if (insert_dirent_tail(fs, buf) == 0) | |
1321 | goto write_and_fix; | |
e8548796 | 1322 | e2fsck_rehash_dir_later(ctx, ino); |
17641bf2 | 1323 | } |
e8548796 DW |
1324 | |
1325 | write_and_fix: | |
1326 | if (e2fsck_dir_will_be_rehashed(ctx, ino)) | |
1327 | ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS; | |
6582dbe9 ZL |
1328 | if (inline_data_size) { |
1329 | cd->pctx.errcode = | |
1330 | ext2fs_inline_data_set(fs, ino, 0, buf, | |
1331 | inline_data_size); | |
1332 | } else | |
1333 | cd->pctx.errcode = ext2fs_write_dir_block4(fs, block_nr, | |
1334 | buf, 0, ino); | |
e8548796 DW |
1335 | if (e2fsck_dir_will_be_rehashed(ctx, ino)) |
1336 | ctx->fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS; | |
1b6bf175 | 1337 | if (cd->pctx.errcode) { |
08b21301 | 1338 | if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK, |
0926668d TT |
1339 | &cd->pctx)) |
1340 | goto abort_free_dict; | |
3839e657 TT |
1341 | } |
1342 | ext2fs_mark_changed(fs); | |
e8548796 DW |
1343 | } else if (is_leaf && failed_csum && !dir_modified) { |
1344 | /* | |
1345 | * If a leaf node that fails csum makes it this far without | |
1346 | * alteration, ask the user if the checksum should be fixed. | |
1347 | */ | |
1348 | if (fix_problem(ctx, PR_2_LEAF_NODE_ONLY_CSUM_INVALID, | |
1349 | &cd->pctx)) | |
1350 | goto write_and_fix; | |
3839e657 | 1351 | } |
0926668d | 1352 | dict_free_nodes(&de_dict); |
3839e657 | 1353 | return 0; |
0926668d | 1354 | abort_free_dict: |
0926668d | 1355 | ctx->flags |= E2F_FLAG_ABORT; |
49a7360b | 1356 | dict_free_nodes(&de_dict); |
0926668d | 1357 | return DIRENT_ABORT; |
3839e657 TT |
1358 | } |
1359 | ||
624e4a64 AK |
1360 | struct del_block { |
1361 | e2fsck_t ctx; | |
1362 | e2_blkcnt_t num; | |
1363 | }; | |
1364 | ||
3839e657 TT |
1365 | /* |
1366 | * This function is called to deallocate a block, and is an interator | |
1367 | * functioned called by deallocate inode via ext2fs_iterate_block(). | |
1368 | */ | |
1369 | static int deallocate_inode_block(ext2_filsys fs, | |
6dc64392 | 1370 | blk64_t *block_nr, |
54434927 | 1371 | e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), |
6dc64392 | 1372 | blk64_t ref_block EXT2FS_ATTR((unused)), |
54434927 | 1373 | int ref_offset EXT2FS_ATTR((unused)), |
133a56dc | 1374 | void *priv_data) |
3839e657 | 1375 | { |
624e4a64 | 1376 | struct del_block *p = priv_data; |
efc6f628 | 1377 | |
1917875f | 1378 | if (HOLE_BLKADDR(*block_nr)) |
3839e657 | 1379 | return 0; |
1ba7a2f2 | 1380 | if ((*block_nr < fs->super->s_first_data_block) || |
4efbac6f | 1381 | (*block_nr >= ext2fs_blocks_count(fs->super))) |
1ba7a2f2 | 1382 | return 0; |
8dd650ab DW |
1383 | if ((*block_nr % EXT2FS_CLUSTER_RATIO(fs)) == 0) |
1384 | ext2fs_block_alloc_stats2(fs, *block_nr, -1); | |
624e4a64 | 1385 | p->num++; |
3839e657 TT |
1386 | return 0; |
1387 | } | |
efc6f628 | 1388 | |
3839e657 TT |
1389 | /* |
1390 | * This fuction deallocates an inode | |
1391 | */ | |
4035f40b | 1392 | static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf) |
3839e657 | 1393 | { |
1b6bf175 | 1394 | ext2_filsys fs = ctx->fs; |
3839e657 | 1395 | struct ext2_inode inode; |
1b6bf175 | 1396 | struct problem_context pctx; |
0684a4f3 | 1397 | __u32 count; |
624e4a64 | 1398 | struct del_block del_block; |
efc6f628 | 1399 | |
08b21301 | 1400 | e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode"); |
1b6bf175 TT |
1401 | clear_problem_context(&pctx); |
1402 | pctx.ino = ino; | |
f3db3566 | 1403 | |
3839e657 TT |
1404 | /* |
1405 | * Fix up the bitmaps... | |
1406 | */ | |
f8188fff | 1407 | e2fsck_read_bitmaps(ctx); |
0684a4f3 TT |
1408 | ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode)); |
1409 | ||
0c80c44b | 1410 | if (ext2fs_file_acl_block(fs, &inode) && |
0684a4f3 | 1411 | (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) { |
39f5659a DW |
1412 | pctx.errcode = ext2fs_adjust_ea_refcount3(fs, |
1413 | ext2fs_file_acl_block(fs, &inode), | |
1414 | block_buf, -1, &count, ino); | |
0684a4f3 TT |
1415 | if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) { |
1416 | pctx.errcode = 0; | |
1417 | count = 1; | |
1418 | } | |
1419 | if (pctx.errcode) { | |
0c80c44b | 1420 | pctx.blk = ext2fs_file_acl_block(fs, &inode); |
0684a4f3 TT |
1421 | fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx); |
1422 | ctx->flags |= E2F_FLAG_ABORT; | |
1423 | return; | |
1424 | } | |
1425 | if (count == 0) { | |
48f23054 | 1426 | ext2fs_block_alloc_stats2(fs, |
0c80c44b | 1427 | ext2fs_file_acl_block(fs, &inode), -1); |
0684a4f3 | 1428 | } |
0c80c44b | 1429 | ext2fs_file_acl_block_set(fs, &inode, 0); |
0684a4f3 | 1430 | } |
3839e657 | 1431 | |
0c80c44b | 1432 | if (!ext2fs_inode_has_valid_blocks2(fs, &inode)) |
c8ec2bad | 1433 | goto clear_inode; |
a4742691 | 1434 | |
3b6c0938 DW |
1435 | if (LINUX_S_ISREG(inode.i_mode) && |
1436 | ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode))) | |
a4742691 TT |
1437 | ctx->large_files--; |
1438 | ||
624e4a64 AK |
1439 | del_block.ctx = ctx; |
1440 | del_block.num = 0; | |
6dc64392 | 1441 | pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf, |
624e4a64 AK |
1442 | deallocate_inode_block, |
1443 | &del_block); | |
1b6bf175 TT |
1444 | if (pctx.errcode) { |
1445 | fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx); | |
08b21301 TT |
1446 | ctx->flags |= E2F_FLAG_ABORT; |
1447 | return; | |
1b6bf175 | 1448 | } |
c8ec2bad TT |
1449 | clear_inode: |
1450 | /* Inode may have changed by block_iterate, so reread it */ | |
1451 | e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode"); | |
1452 | e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode"); | |
3839e657 TT |
1453 | } |
1454 | ||
8fdc9985 TT |
1455 | /* |
1456 | * This fuction clears the htree flag on an inode | |
1457 | */ | |
1458 | static void clear_htree(e2fsck_t ctx, ext2_ino_t ino) | |
1459 | { | |
1460 | struct ext2_inode inode; | |
efc6f628 | 1461 | |
8fdc9985 TT |
1462 | e2fsck_read_inode(ctx, ino, &inode, "clear_htree"); |
1463 | inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL; | |
1464 | e2fsck_write_inode(ctx, ino, &inode, "clear_htree"); | |
b7a00563 TT |
1465 | if (ctx->dirs_to_hash) |
1466 | ext2fs_u32_list_add(ctx->dirs_to_hash, ino); | |
8fdc9985 TT |
1467 | } |
1468 | ||
1469 | ||
f404167d TT |
1470 | int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir, |
1471 | ext2_ino_t ino, char *buf) | |
3839e657 | 1472 | { |
1b6bf175 | 1473 | ext2_filsys fs = ctx->fs; |
3839e657 | 1474 | struct ext2_inode inode; |
3839e657 | 1475 | int inode_modified = 0; |
6c313fd4 | 1476 | int not_fixed = 0; |
1e3472c5 | 1477 | unsigned char *frag, *fsize; |
21c84b71 | 1478 | struct problem_context pctx; |
3c7c6d73 | 1479 | problem_t problem = 0; |
3839e657 | 1480 | |
08b21301 | 1481 | e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode"); |
21c84b71 TT |
1482 | |
1483 | clear_problem_context(&pctx); | |
1484 | pctx.ino = ino; | |
1485 | pctx.dir = dir; | |
1486 | pctx.inode = &inode; | |
1487 | ||
0c80c44b | 1488 | if (ext2fs_file_acl_block(fs, &inode) && |
f76344fb TT |
1489 | !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) { |
1490 | if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) { | |
0c80c44b | 1491 | ext2fs_file_acl_block_set(fs, &inode, 0); |
f76344fb TT |
1492 | inode_modified++; |
1493 | } else | |
1494 | not_fixed++; | |
1495 | } | |
6c313fd4 | 1496 | |
50e1e10f TT |
1497 | if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) && |
1498 | !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) && | |
1499 | !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) && | |
08b21301 TT |
1500 | !(LINUX_S_ISSOCK(inode.i_mode))) |
1501 | problem = PR_2_BAD_MODE; | |
fdbdea09 | 1502 | else if (LINUX_S_ISCHR(inode.i_mode) |
0684a4f3 | 1503 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
08b21301 | 1504 | problem = PR_2_BAD_CHAR_DEV; |
fdbdea09 | 1505 | else if (LINUX_S_ISBLK(inode.i_mode) |
0684a4f3 | 1506 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
08b21301 | 1507 | problem = PR_2_BAD_BLOCK_DEV; |
fdbdea09 | 1508 | else if (LINUX_S_ISFIFO(inode.i_mode) |
0684a4f3 | 1509 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
1dde43f0 | 1510 | problem = PR_2_BAD_FIFO; |
fdbdea09 | 1511 | else if (LINUX_S_ISSOCK(inode.i_mode) |
0684a4f3 | 1512 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
1dde43f0 | 1513 | problem = PR_2_BAD_SOCKET; |
fdbdea09 | 1514 | else if (LINUX_S_ISLNK(inode.i_mode) |
7cadc577 | 1515 | && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) { |
bcf9c5d4 | 1516 | problem = PR_2_INVALID_SYMLINK; |
67052a8a | 1517 | } |
1dde43f0 | 1518 | |
08b21301 TT |
1519 | if (problem) { |
1520 | if (fix_problem(ctx, problem, &pctx)) { | |
1b6bf175 | 1521 | deallocate_inode(ctx, ino, 0); |
a02ce9df | 1522 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
08b21301 | 1523 | return 0; |
7cf73dcd | 1524 | return 1; |
6c313fd4 TT |
1525 | } else |
1526 | not_fixed++; | |
08b21301 | 1527 | problem = 0; |
7cf73dcd | 1528 | } |
efc6f628 | 1529 | |
6c313fd4 TT |
1530 | if (inode.i_faddr) { |
1531 | if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) { | |
1532 | inode.i_faddr = 0; | |
1533 | inode_modified++; | |
1534 | } else | |
1535 | not_fixed++; | |
3839e657 | 1536 | } |
1e3472c5 TT |
1537 | |
1538 | switch (fs->super->s_creator_os) { | |
1e3472c5 TT |
1539 | case EXT2_OS_HURD: |
1540 | frag = &inode.osd2.hurd2.h_i_frag; | |
1541 | fsize = &inode.osd2.hurd2.h_i_fsize; | |
1542 | break; | |
1e3472c5 TT |
1543 | default: |
1544 | frag = fsize = 0; | |
1545 | } | |
21c84b71 TT |
1546 | if (frag && *frag) { |
1547 | pctx.num = *frag; | |
1b6bf175 | 1548 | if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) { |
21c84b71 TT |
1549 | *frag = 0; |
1550 | inode_modified++; | |
7e0282c5 TT |
1551 | } else |
1552 | not_fixed++; | |
21c84b71 TT |
1553 | pctx.num = 0; |
1554 | } | |
1555 | if (fsize && *fsize) { | |
1556 | pctx.num = *fsize; | |
1b6bf175 | 1557 | if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) { |
21c84b71 TT |
1558 | *fsize = 0; |
1559 | inode_modified++; | |
6c313fd4 TT |
1560 | } else |
1561 | not_fixed++; | |
21c84b71 TT |
1562 | pctx.num = 0; |
1563 | } | |
1564 | ||
5d17119d | 1565 | if ((fs->super->s_creator_os == EXT2_OS_LINUX) && |
efc6f628 | 1566 | !(fs->super->s_feature_ro_compat & |
5d17119d TT |
1567 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE) && |
1568 | (inode.osd2.linux2.l_i_blocks_hi != 0)) { | |
1569 | pctx.num = inode.osd2.linux2.l_i_blocks_hi; | |
1570 | if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) { | |
1571 | inode.osd2.linux2.l_i_blocks_hi = 0; | |
1572 | inode_modified++; | |
1573 | } | |
1574 | } | |
1575 | ||
911ec626 TT |
1576 | if (!(fs->super->s_feature_incompat & |
1577 | EXT4_FEATURE_INCOMPAT_64BIT) && | |
1578 | inode.osd2.linux2.l_i_file_acl_high != 0) { | |
1579 | pctx.num = inode.osd2.linux2.l_i_file_acl_high; | |
1580 | if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) { | |
1581 | inode.osd2.linux2.l_i_file_acl_high = 0; | |
1582 | inode_modified++; | |
1583 | } else | |
1584 | not_fixed++; | |
1585 | } | |
1586 | ||
0c80c44b TT |
1587 | if (ext2fs_file_acl_block(fs, &inode) && |
1588 | ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) || | |
1589 | (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) { | |
6c313fd4 | 1590 | if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) { |
0c80c44b | 1591 | ext2fs_file_acl_block_set(fs, &inode, 0); |
6c313fd4 TT |
1592 | inode_modified++; |
1593 | } else | |
1594 | not_fixed++; | |
342d847d | 1595 | } |
21c84b71 | 1596 | if (inode.i_dir_acl && |
6c313fd4 TT |
1597 | LINUX_S_ISDIR(inode.i_mode)) { |
1598 | if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) { | |
1599 | inode.i_dir_acl = 0; | |
1600 | inode_modified++; | |
1601 | } else | |
1602 | not_fixed++; | |
21c84b71 | 1603 | } |
6c313fd4 | 1604 | |
f3db3566 | 1605 | if (inode_modified) |
08b21301 | 1606 | e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode"); |
f76344fb | 1607 | if (!not_fixed && ctx->inode_bad_map) |
c5d2f50d | 1608 | ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino); |
3839e657 TT |
1609 | return 0; |
1610 | } | |
1611 | ||
50e1e10f TT |
1612 | |
1613 | /* | |
1614 | * allocate_dir_block --- this function allocates a new directory | |
1615 | * block for a particular inode; this is done if a directory has | |
1616 | * a "hole" in it, or if a directory has a illegal block number | |
1617 | * that was zeroed out and now needs to be replaced. | |
1618 | */ | |
1b6bf175 | 1619 | static int allocate_dir_block(e2fsck_t ctx, |
6dc64392 | 1620 | struct ext2_db_entry2 *db, |
efc6f628 | 1621 | char *buf EXT2FS_ATTR((unused)), |
54434927 | 1622 | struct problem_context *pctx) |
50e1e10f | 1623 | { |
1b6bf175 | 1624 | ext2_filsys fs = ctx->fs; |
ff11309e | 1625 | blk64_t blk = 0; |
50e1e10f TT |
1626 | char *block; |
1627 | struct ext2_inode inode; | |
50e1e10f | 1628 | |
1b6bf175 | 1629 | if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0) |
50e1e10f TT |
1630 | return 1; |
1631 | ||
1632 | /* | |
1633 | * Read the inode and block bitmaps in; we'll be messing with | |
1634 | * them. | |
1635 | */ | |
f8188fff | 1636 | e2fsck_read_bitmaps(ctx); |
efc6f628 | 1637 | |
50e1e10f TT |
1638 | /* |
1639 | * First, find a free block | |
1640 | */ | |
ff11309e DW |
1641 | e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block"); |
1642 | pctx->errcode = ext2fs_map_cluster_block(fs, db->ino, &inode, | |
1643 | db->blockcnt, &blk); | |
1644 | if (pctx->errcode || blk == 0) { | |
1645 | pctx->errcode = ext2fs_new_block2(fs, 0, | |
1646 | ctx->block_found_map, &blk); | |
1647 | if (pctx->errcode) { | |
1648 | pctx->str = "ext2fs_new_block"; | |
1649 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); | |
1650 | return 1; | |
1651 | } | |
50e1e10f | 1652 | } |
c5d2f50d VAH |
1653 | ext2fs_mark_block_bitmap2(ctx->block_found_map, blk); |
1654 | ext2fs_mark_block_bitmap2(fs->block_map, blk); | |
50e1e10f TT |
1655 | ext2fs_mark_bb_dirty(fs); |
1656 | ||
1657 | /* | |
1658 | * Now let's create the actual data block for the inode | |
1659 | */ | |
1660 | if (db->blockcnt) | |
1b6bf175 | 1661 | pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block); |
50e1e10f | 1662 | else |
1b6bf175 TT |
1663 | pctx->errcode = ext2fs_new_dir_block(fs, db->ino, |
1664 | EXT2_ROOT_INO, &block); | |
50e1e10f | 1665 | |
1b6bf175 TT |
1666 | if (pctx->errcode) { |
1667 | pctx->str = "ext2fs_new_dir_block"; | |
1668 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); | |
50e1e10f TT |
1669 | return 1; |
1670 | } | |
1671 | ||
81683c6a | 1672 | pctx->errcode = ext2fs_write_dir_block4(fs, blk, block, 0, db->ino); |
c4e3d3f3 | 1673 | ext2fs_free_mem(&block); |
1b6bf175 TT |
1674 | if (pctx->errcode) { |
1675 | pctx->str = "ext2fs_write_dir_block"; | |
1676 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); | |
50e1e10f TT |
1677 | return 1; |
1678 | } | |
1679 | ||
1680 | /* | |
1681 | * Update the inode block count | |
1682 | */ | |
1ca1059f | 1683 | ext2fs_iblk_add_blocks(fs, &inode, 1); |
97c607b1 DW |
1684 | if (EXT2_I_SIZE(&inode) < (db->blockcnt+1) * fs->blocksize) { |
1685 | pctx->errcode = ext2fs_inode_size_set(fs, &inode, | |
1686 | (db->blockcnt+1) * fs->blocksize); | |
1687 | if (pctx->errcode) { | |
1688 | pctx->str = "ext2fs_inode_size_set"; | |
1689 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); | |
1690 | return 1; | |
1691 | } | |
1692 | } | |
08b21301 | 1693 | e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block"); |
50e1e10f TT |
1694 | |
1695 | /* | |
1696 | * Finally, update the block pointers for the inode | |
1697 | */ | |
1698 | db->blk = blk; | |
2d07b3ad TT |
1699 | pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET, |
1700 | db->blockcnt, 0, &blk); | |
1b6bf175 TT |
1701 | if (pctx->errcode) { |
1702 | pctx->str = "ext2fs_block_iterate"; | |
1703 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); | |
50e1e10f TT |
1704 | return 1; |
1705 | } | |
1706 | ||
1707 | return 0; | |
1708 | } |