]>
Commit | Line | Data |
---|---|---|
3839e657 TT |
1 | /* |
2 | * pass2.c --- check directory structure | |
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% | |
3839e657 TT |
10 | * |
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) | |
19 | * should be less than (rec_len - 8). | |
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() */ |
48e6e813 TT |
45 | #include <string.h> |
46 | ||
3839e657 | 47 | #include "e2fsck.h" |
21c84b71 | 48 | #include "problem.h" |
0926668d | 49 | #include "dict.h" |
3839e657 | 50 | |
aa4115a4 TT |
51 | #ifdef NO_INLINE_FUNCS |
52 | #define _INLINE_ | |
53 | #else | |
54 | #define _INLINE_ inline | |
55 | #endif | |
56 | ||
ad4fa466 | 57 | /* #define DX_DEBUG */ |
8fdc9985 | 58 | |
3839e657 TT |
59 | /* |
60 | * Keeps track of how many times an inode is referenced. | |
61 | */ | |
4035f40b | 62 | static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf); |
3839e657 | 63 | static int check_dir_block(ext2_filsys fs, |
21c84b71 | 64 | struct ext2_db_entry *dir_blocks_info, |
54dc7ca2 | 65 | void *priv_data); |
1b6bf175 | 66 | static int allocate_dir_block(e2fsck_t ctx, |
21c84b71 TT |
67 | struct ext2_db_entry *dir_blocks_info, |
68 | char *buf, struct problem_context *pctx); | |
50e1e10f TT |
69 | static int update_dir_block(ext2_filsys fs, |
70 | blk_t *block_nr, | |
133a56dc | 71 | e2_blkcnt_t blockcnt, |
4035f40b TT |
72 | blk_t ref_block, |
73 | int ref_offset, | |
74 | void *priv_data); | |
8fdc9985 | 75 | static void clear_htree(e2fsck_t ctx, ext2_ino_t ino); |
ad4fa466 TT |
76 | static int htree_depth(struct dx_dir_info *dx_dir, |
77 | struct dx_dirblock_info *dx_db); | |
ea1959f0 | 78 | static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b); |
3839e657 | 79 | |
21c84b71 TT |
80 | struct check_dir_struct { |
81 | char *buf; | |
82 | struct problem_context pctx; | |
f8188fff | 83 | int count, max; |
1b6bf175 | 84 | e2fsck_t ctx; |
21c84b71 TT |
85 | }; |
86 | ||
08b21301 | 87 | void e2fsck_pass2(e2fsck_t ctx) |
3839e657 | 88 | { |
a4742691 TT |
89 | struct ext2_super_block *sb = ctx->fs->super; |
90 | struct problem_context pctx; | |
91 | ext2_filsys fs = ctx->fs; | |
92 | char *buf; | |
8bf191e8 | 93 | #ifdef RESOURCE_TRACK |
3839e657 | 94 | struct resource_track rtrack; |
8bf191e8 | 95 | #endif |
21c84b71 | 96 | struct check_dir_struct cd; |
8fdc9985 TT |
97 | struct dx_dir_info *dx_dir; |
98 | struct dx_dirblock_info *dx_db, *dx_parent; | |
54434927 | 99 | int b; |
ad4fa466 | 100 | int i, depth; |
8fdc9985 TT |
101 | problem_t code; |
102 | int bad_dir; | |
103 | ||
8bf191e8 | 104 | #ifdef RESOURCE_TRACK |
6d96b00d | 105 | init_resource_track(&rtrack, ctx->fs->io); |
8bf191e8 | 106 | #endif |
3839e657 | 107 | |
1b6bf175 TT |
108 | clear_problem_context(&cd.pctx); |
109 | ||
3839e657 TT |
110 | #ifdef MTRACE |
111 | mtrace_print("Pass 2"); | |
112 | #endif | |
113 | ||
1b6bf175 TT |
114 | if (!(ctx->options & E2F_OPT_PREEN)) |
115 | fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx); | |
116 | ||
34b9f796 TT |
117 | e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT, |
118 | &ctx->inode_count); | |
119 | if (ctx->inode_count) | |
120 | cd.pctx.errcode = 0; | |
121 | else | |
122 | cd.pctx.errcode = ext2fs_create_icount2(fs, | |
123 | EXT2_ICOUNT_OPT_INCREMENT, | |
1b6bf175 TT |
124 | 0, ctx->inode_link_info, |
125 | &ctx->inode_count); | |
126 | if (cd.pctx.errcode) { | |
127 | fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx); | |
08b21301 TT |
128 | ctx->flags |= E2F_FLAG_ABORT; |
129 | return; | |
21c84b71 | 130 | } |
bcf9c5d4 | 131 | buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize, |
54dc7ca2 | 132 | "directory scan buffer"); |
3839e657 | 133 | |
21c84b71 TT |
134 | /* |
135 | * Set up the parent pointer for the root directory, if | |
136 | * present. (If the root directory is not present, we will | |
137 | * create it in pass 3.) | |
138 | */ | |
28db82a8 | 139 | (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO); |
21c84b71 TT |
140 | |
141 | cd.buf = buf; | |
1b6bf175 | 142 | cd.ctx = ctx; |
f75c28de | 143 | cd.count = 1; |
f8188fff | 144 | cd.max = ext2fs_dblist_count(fs->dblist); |
f75c28de TT |
145 | |
146 | if (ctx->progress) | |
147 | (void) (ctx->progress)(ctx, 2, 0, cd.max); | |
ea1959f0 TT |
148 | |
149 | if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) | |
150 | ext2fs_dblist_sort(fs->dblist, special_dir_block_cmp); | |
21c84b71 | 151 | |
1b6bf175 TT |
152 | cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block, |
153 | &cd); | |
49a7360b | 154 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART) |
08b21301 | 155 | return; |
1b6bf175 TT |
156 | if (cd.pctx.errcode) { |
157 | fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx); | |
08b21301 TT |
158 | ctx->flags |= E2F_FLAG_ABORT; |
159 | return; | |
7ac02a5e | 160 | } |
8fdc9985 TT |
161 | |
162 | #ifdef ENABLE_HTREE | |
163 | for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) { | |
4cae0452 TT |
164 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
165 | return; | |
62acaa1d | 166 | if (dx_dir->numblocks == 0) |
8fdc9985 TT |
167 | continue; |
168 | clear_problem_context(&pctx); | |
169 | bad_dir = 0; | |
170 | pctx.dir = dx_dir->ino; | |
171 | dx_db = dx_dir->dx_block; | |
172 | if (dx_db->flags & DX_FLAG_REFERENCED) | |
173 | dx_db->flags |= DX_FLAG_DUP_REF; | |
174 | else | |
175 | dx_db->flags |= DX_FLAG_REFERENCED; | |
176 | /* | |
177 | * Find all of the first and last leaf blocks, and | |
178 | * update their parent's min and max hash values | |
179 | */ | |
180 | for (b=0, dx_db = dx_dir->dx_block; | |
181 | b < dx_dir->numblocks; | |
182 | b++, dx_db++) { | |
183 | if ((dx_db->type != DX_DIRBLOCK_LEAF) || | |
184 | !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST))) | |
185 | continue; | |
186 | dx_parent = &dx_dir->dx_block[dx_db->parent]; | |
187 | /* | |
188 | * XXX Make sure dx_parent->min_hash > dx_db->min_hash | |
189 | */ | |
190 | if (dx_db->flags & DX_FLAG_FIRST) | |
191 | dx_parent->min_hash = dx_db->min_hash; | |
192 | /* | |
193 | * XXX Make sure dx_parent->max_hash < dx_db->max_hash | |
194 | */ | |
195 | if (dx_db->flags & DX_FLAG_LAST) | |
196 | dx_parent->max_hash = dx_db->max_hash; | |
197 | } | |
198 | ||
199 | for (b=0, dx_db = dx_dir->dx_block; | |
200 | b < dx_dir->numblocks; | |
201 | b++, dx_db++) { | |
202 | pctx.blkcount = b; | |
203 | pctx.group = dx_db->parent; | |
204 | code = 0; | |
205 | if (!(dx_db->flags & DX_FLAG_FIRST) && | |
206 | (dx_db->min_hash < dx_db->node_min_hash)) { | |
207 | pctx.blk = dx_db->min_hash; | |
208 | pctx.blk2 = dx_db->node_min_hash; | |
209 | code = PR_2_HTREE_MIN_HASH; | |
210 | fix_problem(ctx, code, &pctx); | |
211 | bad_dir++; | |
212 | } | |
ad4fa466 TT |
213 | if (dx_db->type == DX_DIRBLOCK_LEAF) { |
214 | depth = htree_depth(dx_dir, dx_db); | |
215 | if (depth != dx_dir->depth) { | |
e5e12db9 | 216 | pctx.num = dx_dir->depth; |
ad4fa466 TT |
217 | code = PR_2_HTREE_BAD_DEPTH; |
218 | fix_problem(ctx, code, &pctx); | |
219 | bad_dir++; | |
220 | } | |
221 | } | |
8fdc9985 TT |
222 | /* |
223 | * This test doesn't apply for the root block | |
224 | * at block #0 | |
225 | */ | |
226 | if (b && | |
227 | (dx_db->max_hash > dx_db->node_max_hash)) { | |
228 | pctx.blk = dx_db->max_hash; | |
229 | pctx.blk2 = dx_db->node_max_hash; | |
230 | code = PR_2_HTREE_MAX_HASH; | |
231 | fix_problem(ctx, code, &pctx); | |
503f9e7f | 232 | bad_dir++; |
8fdc9985 TT |
233 | } |
234 | if (!(dx_db->flags & DX_FLAG_REFERENCED)) { | |
235 | code = PR_2_HTREE_NOTREF; | |
236 | fix_problem(ctx, code, &pctx); | |
237 | bad_dir++; | |
238 | } else if (dx_db->flags & DX_FLAG_DUP_REF) { | |
239 | code = PR_2_HTREE_DUPREF; | |
240 | fix_problem(ctx, code, &pctx); | |
241 | bad_dir++; | |
242 | } | |
243 | if (code == 0) | |
244 | continue; | |
245 | } | |
246 | if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) { | |
247 | clear_htree(ctx, dx_dir->ino); | |
62acaa1d | 248 | dx_dir->numblocks = 0; |
8fdc9985 | 249 | } |
8fdc9985 TT |
250 | } |
251 | #endif | |
c4e3d3f3 | 252 | ext2fs_free_mem(&buf); |
21c84b71 TT |
253 | ext2fs_free_dblist(fs->dblist); |
254 | ||
1b6bf175 TT |
255 | if (ctx->inode_bad_map) { |
256 | ext2fs_free_inode_bitmap(ctx->inode_bad_map); | |
257 | ctx->inode_bad_map = 0; | |
3839e657 | 258 | } |
aa4115a4 TT |
259 | if (ctx->inode_reg_map) { |
260 | ext2fs_free_inode_bitmap(ctx->inode_reg_map); | |
261 | ctx->inode_reg_map = 0; | |
262 | } | |
a4742691 TT |
263 | |
264 | clear_problem_context(&pctx); | |
265 | if (ctx->large_files) { | |
266 | if (!(sb->s_feature_ro_compat & | |
267 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE) && | |
268 | fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) { | |
269 | sb->s_feature_ro_compat |= | |
270 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE; | |
0cfce7f7 | 271 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
a4742691 TT |
272 | ext2fs_mark_super_dirty(fs); |
273 | } | |
274 | if (sb->s_rev_level == EXT2_GOOD_OLD_REV && | |
275 | fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) { | |
276 | ext2fs_update_dynamic_rev(fs); | |
277 | ext2fs_mark_super_dirty(fs); | |
278 | } | |
a4742691 TT |
279 | } |
280 | ||
8bf191e8 | 281 | #ifdef RESOURCE_TRACK |
5596defa TT |
282 | if (ctx->options & E2F_OPT_TIME2) { |
283 | e2fsck_clear_progbar(ctx); | |
6d96b00d | 284 | print_resource_track(_("Pass 2"), &rtrack, fs->io); |
5596defa | 285 | } |
8bf191e8 | 286 | #endif |
3839e657 TT |
287 | } |
288 | ||
ad4fa466 TT |
289 | #define MAX_DEPTH 32000 |
290 | static int htree_depth(struct dx_dir_info *dx_dir, | |
291 | struct dx_dirblock_info *dx_db) | |
292 | { | |
293 | int depth = 0; | |
294 | ||
295 | while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) { | |
296 | dx_db = &dx_dir->dx_block[dx_db->parent]; | |
297 | depth++; | |
298 | } | |
299 | return depth; | |
300 | } | |
301 | ||
0926668d TT |
302 | static int dict_de_cmp(const void *a, const void *b) |
303 | { | |
520ead37 | 304 | const struct ext2_dir_entry *de_a, *de_b; |
0926668d TT |
305 | int a_len, b_len; |
306 | ||
520ead37 | 307 | de_a = (const struct ext2_dir_entry *) a; |
0926668d | 308 | a_len = de_a->name_len & 0xFF; |
520ead37 | 309 | de_b = (const struct ext2_dir_entry *) b; |
0926668d TT |
310 | b_len = de_b->name_len & 0xFF; |
311 | ||
312 | if (a_len != b_len) | |
313 | return (a_len - b_len); | |
314 | ||
315 | return strncmp(de_a->name, de_b->name, a_len); | |
316 | } | |
ad4fa466 | 317 | |
ea1959f0 TT |
318 | /* |
319 | * This is special sort function that makes sure that directory blocks | |
320 | * with a dirblock of zero are sorted to the beginning of the list. | |
321 | * This guarantees that the root node of the htree directories are | |
322 | * processed first, so we know what hash version to use. | |
323 | */ | |
324 | static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b) | |
325 | { | |
326 | const struct ext2_db_entry *db_a = | |
327 | (const struct ext2_db_entry *) a; | |
328 | const struct ext2_db_entry *db_b = | |
329 | (const struct ext2_db_entry *) b; | |
330 | ||
331 | if (db_a->blockcnt && !db_b->blockcnt) | |
332 | return 1; | |
333 | ||
334 | if (!db_a->blockcnt && db_b->blockcnt) | |
335 | return -1; | |
336 | ||
337 | if (db_a->blk != db_b->blk) | |
338 | return (int) (db_a->blk - db_b->blk); | |
339 | ||
340 | if (db_a->ino != db_b->ino) | |
341 | return (int) (db_a->ino - db_b->ino); | |
342 | ||
343 | return (int) (db_a->blockcnt - db_b->blockcnt); | |
344 | } | |
345 | ||
346 | ||
3839e657 TT |
347 | /* |
348 | * Make sure the first entry in the directory is '.', and that the | |
349 | * directory entry is sane. | |
350 | */ | |
1b6bf175 | 351 | static int check_dot(e2fsck_t ctx, |
3839e657 | 352 | struct ext2_dir_entry *dirent, |
86c627ec | 353 | ext2_ino_t ino, struct problem_context *pctx) |
3839e657 TT |
354 | { |
355 | struct ext2_dir_entry *nextdir; | |
356 | int status = 0; | |
357 | int created = 0; | |
5dd77dbe | 358 | int rec_len, new_len; |
21c84b71 | 359 | int problem = 0; |
3839e657 | 360 | |
21c84b71 TT |
361 | if (!dirent->inode) |
362 | problem = PR_2_MISSING_DOT; | |
b6f79831 | 363 | else if (((dirent->name_len & 0xFF) != 1) || |
21c84b71 TT |
364 | (dirent->name[0] != '.')) |
365 | problem = PR_2_1ST_NOT_DOT; | |
366 | else if (dirent->name[1] != '\0') | |
367 | problem = PR_2_DOT_NULL_TERM; | |
5dd77dbe TT |
368 | |
369 | rec_len = (dirent->rec_len || ctx->fs->blocksize < 65536) ? | |
370 | dirent->rec_len : 65536; | |
21c84b71 | 371 | if (problem) { |
1b6bf175 | 372 | if (fix_problem(ctx, problem, pctx)) { |
5dd77dbe TT |
373 | if (rec_len < 12) |
374 | rec_len = dirent->rec_len = 12; | |
3839e657 TT |
375 | dirent->inode = ino; |
376 | dirent->name_len = 1; | |
377 | dirent->name[0] = '.'; | |
21c84b71 | 378 | dirent->name[1] = '\0'; |
3839e657 TT |
379 | status = 1; |
380 | created = 1; | |
3839e657 TT |
381 | } |
382 | } | |
3839e657 | 383 | if (dirent->inode != ino) { |
1b6bf175 | 384 | if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) { |
3839e657 TT |
385 | dirent->inode = ino; |
386 | status = 1; | |
21c84b71 | 387 | } |
3839e657 | 388 | } |
5dd77dbe TT |
389 | if (rec_len > 12) { |
390 | new_len = rec_len - 12; | |
3839e657 | 391 | if (new_len > 12) { |
3839e657 | 392 | if (created || |
f8188fff | 393 | fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) { |
3839e657 TT |
394 | nextdir = (struct ext2_dir_entry *) |
395 | ((char *) dirent + 12); | |
396 | dirent->rec_len = 12; | |
397 | nextdir->rec_len = new_len; | |
398 | nextdir->inode = 0; | |
399 | nextdir->name_len = 0; | |
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 | { |
5dd77dbe | 416 | int rec_len, problem = 0; |
3839e657 | 417 | |
21c84b71 TT |
418 | if (!dirent->inode) |
419 | problem = PR_2_MISSING_DOT_DOT; | |
b6f79831 | 420 | else if (((dirent->name_len & 0xFF) != 2) || |
21c84b71 TT |
421 | (dirent->name[0] != '.') || |
422 | (dirent->name[1] != '.')) | |
423 | problem = PR_2_2ND_NOT_DOT_DOT; | |
424 | else if (dirent->name[2] != '\0') | |
425 | problem = PR_2_DOT_DOT_NULL_TERM; | |
426 | ||
5dd77dbe TT |
427 | rec_len = (dirent->rec_len || ctx->fs->blocksize < 65536) ? |
428 | dirent->rec_len : 65536; | |
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; | |
439 | dirent->name_len = 2; | |
440 | dirent->name[0] = '.'; | |
441 | dirent->name[1] = '.'; | |
21c84b71 | 442 | dirent->name[2] = '\0'; |
3839e657 | 443 | return 1; |
21c84b71 | 444 | } |
3839e657 TT |
445 | return 0; |
446 | } | |
28db82a8 TT |
447 | if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) { |
448 | fix_problem(ctx, PR_2_NO_DIRINFO, pctx); | |
449 | return -1; | |
450 | } | |
3839e657 TT |
451 | return 0; |
452 | } | |
453 | ||
454 | /* | |
455 | * Check to make sure a directory entry doesn't contain any illegal | |
456 | * characters. | |
457 | */ | |
1b6bf175 | 458 | static int check_name(e2fsck_t ctx, |
3839e657 | 459 | struct ext2_dir_entry *dirent, |
54434927 TT |
460 | ext2_ino_t dir_ino EXT2FS_ATTR((unused)), |
461 | struct problem_context *pctx) | |
3839e657 TT |
462 | { |
463 | int i; | |
464 | int fixup = -1; | |
3839e657 | 465 | int ret = 0; |
3839e657 | 466 | |
b6f79831 | 467 | for ( i = 0; i < (dirent->name_len & 0xFF); i++) { |
3839e657 TT |
468 | if (dirent->name[i] == '/' || dirent->name[i] == '\0') { |
469 | if (fixup < 0) { | |
1b6bf175 | 470 | fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx); |
3839e657 TT |
471 | } |
472 | if (fixup) { | |
473 | dirent->name[i] = '.'; | |
474 | ret = 1; | |
21c84b71 | 475 | } |
3839e657 TT |
476 | } |
477 | } | |
478 | return ret; | |
479 | } | |
480 | ||
aa4115a4 TT |
481 | /* |
482 | * Check the directory filetype (if present) | |
483 | */ | |
484 | static _INLINE_ int check_filetype(e2fsck_t ctx, | |
54434927 TT |
485 | struct ext2_dir_entry *dirent, |
486 | ext2_ino_t dir_ino EXT2FS_ATTR((unused)), | |
487 | struct problem_context *pctx) | |
aa4115a4 TT |
488 | { |
489 | int filetype = dirent->name_len >> 8; | |
490 | int should_be = EXT2_FT_UNKNOWN; | |
491 | struct ext2_inode inode; | |
492 | ||
493 | if (!(ctx->fs->super->s_feature_incompat & | |
7847c1d4 TT |
494 | EXT2_FEATURE_INCOMPAT_FILETYPE)) { |
495 | if (filetype == 0 || | |
496 | !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx)) | |
497 | return 0; | |
498 | dirent->name_len = dirent->name_len & 0xFF; | |
499 | return 1; | |
500 | } | |
aa4115a4 TT |
501 | |
502 | if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dirent->inode)) { | |
503 | should_be = EXT2_FT_DIR; | |
504 | } else if (ext2fs_test_inode_bitmap(ctx->inode_reg_map, | |
505 | dirent->inode)) { | |
506 | should_be = EXT2_FT_REG_FILE; | |
507 | } else if (ctx->inode_bad_map && | |
508 | ext2fs_test_inode_bitmap(ctx->inode_bad_map, | |
509 | dirent->inode)) | |
510 | should_be = 0; | |
511 | else { | |
512 | e2fsck_read_inode(ctx, dirent->inode, &inode, | |
513 | "check_filetype"); | |
6fdc7a32 | 514 | should_be = ext2_file_type(inode.i_mode); |
aa4115a4 TT |
515 | } |
516 | if (filetype == should_be) | |
517 | return 0; | |
518 | pctx->num = should_be; | |
519 | ||
520 | if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE, | |
521 | pctx) == 0) | |
522 | return 0; | |
523 | ||
524 | dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8; | |
525 | return 1; | |
526 | } | |
527 | ||
8fdc9985 TT |
528 | #ifdef ENABLE_HTREE |
529 | static void parse_int_node(ext2_filsys fs, | |
530 | struct ext2_db_entry *db, | |
531 | struct check_dir_struct *cd, | |
532 | struct dx_dir_info *dx_dir, | |
533 | char *block_buf) | |
534 | { | |
535 | struct ext2_dx_root_info *root; | |
536 | struct ext2_dx_entry *ent; | |
537 | struct ext2_dx_countlimit *limit; | |
538 | struct dx_dirblock_info *dx_db; | |
ad4fa466 | 539 | int i, expect_limit, count; |
8fdc9985 TT |
540 | blk_t blk; |
541 | ext2_dirhash_t min_hash = 0xffffffff; | |
542 | ext2_dirhash_t max_hash = 0; | |
ad4fa466 | 543 | ext2_dirhash_t hash = 0, prev_hash; |
8fdc9985 TT |
544 | |
545 | if (db->blockcnt == 0) { | |
546 | root = (struct ext2_dx_root_info *) (block_buf + 24); | |
547 | ||
548 | #ifdef DX_DEBUG | |
549 | printf("Root node dump:\n"); | |
8deb80a5 | 550 | printf("\t Reserved zero: %u\n", root->reserved_zero); |
8fdc9985 TT |
551 | printf("\t Hash Version: %d\n", root->hash_version); |
552 | printf("\t Info length: %d\n", root->info_length); | |
553 | printf("\t Indirect levels: %d\n", root->indirect_levels); | |
554 | printf("\t Flags: %d\n", root->unused_flags); | |
555 | #endif | |
556 | ||
557 | ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length); | |
558 | } else { | |
559 | ent = (struct ext2_dx_entry *) (block_buf+8); | |
560 | } | |
561 | limit = (struct ext2_dx_countlimit *) ent; | |
562 | ||
563 | #ifdef DX_DEBUG | |
8132d840 TT |
564 | printf("Number of entries (count): %d\n", |
565 | ext2fs_le16_to_cpu(limit->count)); | |
566 | printf("Number of entries (limit): %d\n", | |
567 | ext2fs_le16_to_cpu(limit->limit)); | |
8fdc9985 TT |
568 | #endif |
569 | ||
8132d840 | 570 | count = ext2fs_le16_to_cpu(limit->count); |
ad4fa466 TT |
571 | expect_limit = (fs->blocksize - ((char *) ent - block_buf)) / |
572 | sizeof(struct ext2_dx_entry); | |
8132d840 TT |
573 | if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) { |
574 | cd->pctx.num = ext2fs_le16_to_cpu(limit->limit); | |
ad4fa466 TT |
575 | if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx)) |
576 | goto clear_and_exit; | |
577 | } | |
8132d840 TT |
578 | if (count > expect_limit) { |
579 | cd->pctx.num = count; | |
ad4fa466 TT |
580 | if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx)) |
581 | goto clear_and_exit; | |
582 | count = expect_limit; | |
583 | } | |
584 | ||
585 | for (i=0; i < count; i++) { | |
586 | prev_hash = hash; | |
8132d840 | 587 | hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0; |
8fdc9985 | 588 | #ifdef DX_DEBUG |
8deb80a5 | 589 | printf("Entry #%d: Hash 0x%08x, block %u\n", i, |
8132d840 | 590 | hash, ext2fs_le32_to_cpu(ent[i].block)); |
8fdc9985 | 591 | #endif |
8132d840 | 592 | blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff; |
8fdc9985 | 593 | /* Check to make sure the block is valid */ |
977ac873 | 594 | if (blk >= (blk_t) dx_dir->numblocks) { |
b7a00563 | 595 | cd->pctx.blk = blk; |
8fdc9985 | 596 | if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK, |
ad4fa466 TT |
597 | &cd->pctx)) |
598 | goto clear_and_exit; | |
977ac873 | 599 | continue; |
8fdc9985 | 600 | } |
ad4fa466 TT |
601 | if (hash < prev_hash && |
602 | fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx)) | |
603 | goto clear_and_exit; | |
8fdc9985 TT |
604 | dx_db = &dx_dir->dx_block[blk]; |
605 | if (dx_db->flags & DX_FLAG_REFERENCED) { | |
606 | dx_db->flags |= DX_FLAG_DUP_REF; | |
607 | } else { | |
608 | dx_db->flags |= DX_FLAG_REFERENCED; | |
609 | dx_db->parent = db->blockcnt; | |
610 | } | |
611 | if (hash < min_hash) | |
612 | min_hash = hash; | |
613 | if (hash > max_hash) | |
614 | max_hash = hash; | |
615 | dx_db->node_min_hash = hash; | |
8132d840 TT |
616 | if ((i+1) < count) |
617 | dx_db->node_max_hash = | |
618 | ext2fs_le32_to_cpu(ent[i+1].hash) & ~1; | |
8fdc9985 TT |
619 | else { |
620 | dx_db->node_max_hash = 0xfffffffe; | |
621 | dx_db->flags |= DX_FLAG_LAST; | |
622 | } | |
623 | if (i == 0) | |
624 | dx_db->flags |= DX_FLAG_FIRST; | |
625 | } | |
626 | #ifdef DX_DEBUG | |
627 | printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n", | |
628 | db->blockcnt, min_hash, max_hash); | |
629 | #endif | |
630 | dx_db = &dx_dir->dx_block[db->blockcnt]; | |
631 | dx_db->min_hash = min_hash; | |
632 | dx_db->max_hash = max_hash; | |
ad4fa466 TT |
633 | return; |
634 | ||
635 | clear_and_exit: | |
636 | clear_htree(cd->ctx, cd->pctx.ino); | |
637 | dx_dir->numblocks = 0; | |
8fdc9985 TT |
638 | } |
639 | #endif /* ENABLE_HTREE */ | |
aa4115a4 | 640 | |
e70ae99e TT |
641 | /* |
642 | * Given a busted directory, try to salvage it somehow. | |
643 | * | |
644 | */ | |
ad4fa466 | 645 | static void salvage_directory(ext2_filsys fs, |
e70ae99e TT |
646 | struct ext2_dir_entry *dirent, |
647 | struct ext2_dir_entry *prev, | |
54434927 | 648 | unsigned int *offset) |
e70ae99e TT |
649 | { |
650 | char *cp = (char *) dirent; | |
5dd77dbe | 651 | int left, rec_len; |
642935c0 | 652 | unsigned int name_len = dirent->name_len & 0xFF; |
e70ae99e | 653 | |
5dd77dbe TT |
654 | rec_len = (dirent->rec_len || fs->blocksize < 65536) ? |
655 | dirent->rec_len : 65536; | |
656 | left = fs->blocksize - *offset - rec_len; | |
657 | ||
e70ae99e TT |
658 | /* |
659 | * Special case of directory entry of size 8: copy what's left | |
660 | * of the directory block up to cover up the invalid hole. | |
661 | */ | |
5dd77dbe | 662 | if ((left >= 12) && (rec_len == 8)) { |
e70ae99e TT |
663 | memmove(cp, cp+8, left); |
664 | memset(cp + left, 0, 8); | |
ad4fa466 TT |
665 | return; |
666 | } | |
667 | /* | |
668 | * If the directory entry overruns the end of the directory | |
669 | * block, and the name is small enough to fit, then adjust the | |
670 | * record length. | |
671 | */ | |
672 | if ((left < 0) && | |
5dd77dbe | 673 | (name_len + 8 <= rec_len + (unsigned) left) && |
ad4fa466 TT |
674 | dirent->inode <= fs->super->s_inodes_count && |
675 | strnlen(dirent->name, name_len) == name_len) { | |
676 | dirent->rec_len += left; | |
677 | return; | |
e70ae99e TT |
678 | } |
679 | /* | |
575307cc KS |
680 | * If the record length of the directory entry is a multiple |
681 | * of four, and not too big, such that it is valid, let the | |
682 | * previous directory entry absorb the invalid one. | |
e70ae99e | 683 | */ |
5dd77dbe TT |
684 | if (prev && rec_len && (rec_len % 4) == 0 && |
685 | (*offset + rec_len <= fs->blocksize)) { | |
686 | prev->rec_len += rec_len; | |
687 | *offset += rec_len; | |
ad4fa466 | 688 | return; |
e70ae99e TT |
689 | } |
690 | /* | |
691 | * Default salvage method --- kill all of the directory | |
692 | * entries for the rest of the block. We will either try to | |
693 | * absorb it into the previous directory entry, or create a | |
694 | * new empty directory entry the rest of the directory block. | |
695 | */ | |
696 | if (prev) { | |
ad4fa466 TT |
697 | prev->rec_len += fs->blocksize - *offset; |
698 | *offset = fs->blocksize; | |
e70ae99e | 699 | } else { |
ad4fa466 | 700 | dirent->rec_len = fs->blocksize - *offset; |
e70ae99e TT |
701 | dirent->name_len = 0; |
702 | dirent->inode = 0; | |
e70ae99e | 703 | } |
e70ae99e TT |
704 | } |
705 | ||
3839e657 | 706 | static int check_dir_block(ext2_filsys fs, |
21c84b71 | 707 | struct ext2_db_entry *db, |
54dc7ca2 | 708 | void *priv_data) |
3839e657 | 709 | { |
8fdc9985 TT |
710 | struct dx_dir_info *dx_dir; |
711 | #ifdef ENABLE_HTREE | |
712 | struct dx_dirblock_info *dx_db = 0; | |
713 | #endif /* ENABLE_HTREE */ | |
e70ae99e | 714 | struct ext2_dir_entry *dirent, *prev; |
8fdc9985 | 715 | ext2_dirhash_t hash; |
54434927 | 716 | unsigned int offset = 0; |
e94bc631 | 717 | const char * old_op; |
3839e657 | 718 | int dir_modified = 0; |
21c84b71 | 719 | int dot_state; |
5dd77dbe | 720 | int rec_len; |
3839e657 | 721 | blk_t block_nr = db->blk; |
86c627ec | 722 | ext2_ino_t ino = db->ino; |
28db82a8 | 723 | ext2_ino_t subdir_parent; |
21c84b71 | 724 | __u16 links; |
54dc7ca2 | 725 | struct check_dir_struct *cd; |
1b6bf175 TT |
726 | char *buf; |
727 | e2fsck_t ctx; | |
728 | int problem; | |
ea1959f0 | 729 | struct ext2_dx_root_info *root; |
e8254bfd | 730 | struct ext2_dx_countlimit *limit; |
0926668d TT |
731 | static dict_t de_dict; |
732 | struct problem_context pctx; | |
733 | int dups_found = 0; | |
28db82a8 | 734 | int ret; |
1b6bf175 | 735 | |
54dc7ca2 | 736 | cd = (struct check_dir_struct *) priv_data; |
1b6bf175 TT |
737 | buf = cd->buf; |
738 | ctx = cd->ctx; | |
f8188fff | 739 | |
49a7360b | 740 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART) |
4cae0452 TT |
741 | return DIRENT_ABORT; |
742 | ||
743 | if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max)) | |
744 | return DIRENT_ABORT; | |
21c84b71 | 745 | |
3839e657 TT |
746 | /* |
747 | * Make sure the inode is still in use (could have been | |
748 | * deleted in the duplicate/bad blocks pass. | |
749 | */ | |
1b6bf175 | 750 | if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino))) |
3839e657 | 751 | return 0; |
50e1e10f | 752 | |
21c84b71 TT |
753 | cd->pctx.ino = ino; |
754 | cd->pctx.blk = block_nr; | |
755 | cd->pctx.blkcount = db->blockcnt; | |
756 | cd->pctx.ino2 = 0; | |
757 | cd->pctx.dirent = 0; | |
758 | cd->pctx.num = 0; | |
759 | ||
50e1e10f | 760 | if (db->blk == 0) { |
1b6bf175 | 761 | if (allocate_dir_block(ctx, db, buf, &cd->pctx)) |
50e1e10f TT |
762 | return 0; |
763 | block_nr = db->blk; | |
764 | } | |
3839e657 TT |
765 | |
766 | if (db->blockcnt) | |
767 | dot_state = 2; | |
768 | else | |
769 | dot_state = 0; | |
770 | ||
0926668d TT |
771 | if (ctx->dirs_to_hash && |
772 | ext2fs_u32_list_test(ctx->dirs_to_hash, ino)) | |
773 | dups_found++; | |
774 | ||
3839e657 | 775 | #if 0 |
f3db3566 | 776 | printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr, |
3839e657 TT |
777 | db->blockcnt, ino); |
778 | #endif | |
779 | ||
e94bc631 | 780 | old_op = ehandler_operation(_("reading directory block")); |
1b6bf175 | 781 | cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf); |
e94bc631 | 782 | ehandler_operation(0); |
b9852cd8 TT |
783 | if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED) |
784 | cd->pctx.errcode = 0; /* We'll handle this ourselves */ | |
1b6bf175 | 785 | if (cd->pctx.errcode) { |
08b21301 TT |
786 | if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) { |
787 | ctx->flags |= E2F_FLAG_ABORT; | |
788 | return DIRENT_ABORT; | |
789 | } | |
1b6bf175 | 790 | memset(buf, 0, fs->blocksize); |
3839e657 | 791 | } |
8fdc9985 TT |
792 | #ifdef ENABLE_HTREE |
793 | dx_dir = e2fsck_get_dx_dir_info(ctx, ino); | |
62acaa1d | 794 | if (dx_dir && dx_dir->numblocks) { |
8fdc9985 | 795 | if (db->blockcnt >= dx_dir->numblocks) { |
d45edec0 TT |
796 | if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK, |
797 | &pctx)) { | |
798 | clear_htree(ctx, ino); | |
799 | dx_dir->numblocks = 0; | |
800 | dx_db = 0; | |
801 | goto out_htree; | |
802 | } | |
803 | fatal_error(ctx, _("Can not continue.")); | |
8fdc9985 TT |
804 | } |
805 | dx_db = &dx_dir->dx_block[db->blockcnt]; | |
806 | dx_db->type = DX_DIRBLOCK_LEAF; | |
807 | dx_db->phys = block_nr; | |
808 | dx_db->min_hash = ~0; | |
809 | dx_db->max_hash = 0; | |
810 | ||
811 | dirent = (struct ext2_dir_entry *) buf; | |
5dd77dbe TT |
812 | rec_len = (dirent->rec_len || fs->blocksize < 65536) ? |
813 | dirent->rec_len : 65536; | |
e8254bfd | 814 | limit = (struct ext2_dx_countlimit *) (buf+8); |
8fdc9985 | 815 | if (db->blockcnt == 0) { |
ea1959f0 | 816 | root = (struct ext2_dx_root_info *) (buf + 24); |
8fdc9985 TT |
817 | dx_db->type = DX_DIRBLOCK_ROOT; |
818 | dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST; | |
ea1959f0 TT |
819 | if ((root->reserved_zero || |
820 | root->info_length < 8 || | |
821 | root->indirect_levels > 1) && | |
822 | fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) { | |
823 | clear_htree(ctx, ino); | |
824 | dx_dir->numblocks = 0; | |
825 | dx_db = 0; | |
f77704e4 | 826 | } |
ea1959f0 | 827 | dx_dir->hashversion = root->hash_version; |
f77704e4 TT |
828 | if ((dx_dir->hashversion <= EXT2_HASH_TEA) && |
829 | (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH)) | |
830 | dx_dir->hashversion += 3; | |
ad4fa466 | 831 | dx_dir->depth = root->indirect_levels + 1; |
8fdc9985 | 832 | } else if ((dirent->inode == 0) && |
5dd77dbe | 833 | (rec_len == fs->blocksize) && |
e8254bfd | 834 | (dirent->name_len == 0) && |
8132d840 TT |
835 | (ext2fs_le16_to_cpu(limit->limit) == |
836 | ((fs->blocksize-8) / | |
837 | sizeof(struct ext2_dx_entry)))) | |
8fdc9985 TT |
838 | dx_db->type = DX_DIRBLOCK_NODE; |
839 | } | |
d45edec0 | 840 | out_htree: |
8fdc9985 | 841 | #endif /* ENABLE_HTREE */ |
3839e657 | 842 | |
0926668d | 843 | dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp); |
e70ae99e | 844 | prev = 0; |
3839e657 | 845 | do { |
49a7360b JS |
846 | int group; |
847 | ext2_ino_t first_unused_inode; | |
848 | ||
1b6bf175 | 849 | problem = 0; |
3839e657 | 850 | dirent = (struct ext2_dir_entry *) (buf + offset); |
5dd77dbe TT |
851 | rec_len = (dirent->rec_len || fs->blocksize < 65536) ? |
852 | dirent->rec_len : 65536; | |
21c84b71 TT |
853 | cd->pctx.dirent = dirent; |
854 | cd->pctx.num = offset; | |
5dd77dbe TT |
855 | if (((offset + rec_len) > fs->blocksize) || |
856 | (rec_len < 12) || | |
857 | ((rec_len % 4) != 0) || | |
858 | (((dirent->name_len & 0xFF)+8) > rec_len)) { | |
1b6bf175 | 859 | if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) { |
ad4fa466 | 860 | salvage_directory(fs, dirent, prev, &offset); |
3839e657 | 861 | dir_modified++; |
e70ae99e | 862 | continue; |
21c84b71 | 863 | } else |
0926668d | 864 | goto abort_free_dict; |
3839e657 | 865 | } |
b6f79831 | 866 | if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) { |
1b6bf175 | 867 | if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) { |
50e1e10f TT |
868 | dirent->name_len = EXT2_NAME_LEN; |
869 | dir_modified++; | |
870 | } | |
50e1e10f TT |
871 | } |
872 | ||
e70ae99e | 873 | if (dot_state == 0) { |
1b6bf175 | 874 | if (check_dot(ctx, dirent, ino, &cd->pctx)) |
3839e657 | 875 | dir_modified++; |
e70ae99e | 876 | } else if (dot_state == 1) { |
28db82a8 TT |
877 | ret = check_dotdot(ctx, dirent, ino, &cd->pctx); |
878 | if (ret < 0) | |
0926668d | 879 | goto abort_free_dict; |
28db82a8 | 880 | if (ret) |
3839e657 TT |
881 | dir_modified++; |
882 | } else if (dirent->inode == ino) { | |
1b6bf175 TT |
883 | problem = PR_2_LINK_DOT; |
884 | if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) { | |
3839e657 TT |
885 | dirent->inode = 0; |
886 | dir_modified++; | |
21c84b71 | 887 | goto next; |
3839e657 TT |
888 | } |
889 | } | |
890 | if (!dirent->inode) | |
891 | goto next; | |
892 | ||
3839e657 TT |
893 | /* |
894 | * Make sure the inode listed is a legal one. | |
895 | */ | |
896 | if (((dirent->inode != EXT2_ROOT_INO) && | |
7f88b043 | 897 | (dirent->inode < EXT2_FIRST_INODE(fs->super))) || |
3839e657 | 898 | (dirent->inode > fs->super->s_inodes_count)) { |
1b6bf175 | 899 | problem = PR_2_BAD_INO; |
1b6bf175 TT |
900 | } else if (ctx->inode_bb_map && |
901 | (ext2fs_test_inode_bitmap(ctx->inode_bb_map, | |
902 | dirent->inode))) { | |
903 | /* | |
904 | * If the inode is in a bad block, offer to | |
905 | * clear it. | |
906 | */ | |
907 | problem = PR_2_BB_INODE; | |
e70ae99e | 908 | } else if ((dot_state > 1) && |
b6f79831 | 909 | ((dirent->name_len & 0xFF) == 1) && |
1b6bf175 TT |
910 | (dirent->name[0] == '.')) { |
911 | /* | |
912 | * If there's a '.' entry in anything other | |
913 | * than the first directory entry, it's a | |
914 | * duplicate entry that should be removed. | |
915 | */ | |
916 | problem = PR_2_DUP_DOT; | |
e70ae99e | 917 | } else if ((dot_state > 1) && |
b6f79831 | 918 | ((dirent->name_len & 0xFF) == 2) && |
1b6bf175 TT |
919 | (dirent->name[0] == '.') && |
920 | (dirent->name[1] == '.')) { | |
921 | /* | |
922 | * If there's a '..' entry in anything other | |
923 | * than the second directory entry, it's a | |
924 | * duplicate entry that should be removed. | |
925 | */ | |
926 | problem = PR_2_DUP_DOT_DOT; | |
e70ae99e | 927 | } else if ((dot_state > 1) && |
1b6bf175 TT |
928 | (dirent->inode == EXT2_ROOT_INO)) { |
929 | /* | |
930 | * Don't allow links to the root directory. | |
931 | * We check this specially to make sure we | |
932 | * catch this error case even if the root | |
933 | * directory hasn't been created yet. | |
934 | */ | |
935 | problem = PR_2_LINK_ROOT; | |
e70ae99e | 936 | } else if ((dot_state > 1) && |
c40db6d5 TT |
937 | (dirent->name_len & 0xFF) == 0) { |
938 | /* | |
939 | * Don't allow zero-length directory names. | |
940 | */ | |
941 | problem = PR_2_NULL_NAME; | |
21c84b71 TT |
942 | } |
943 | ||
1b6bf175 TT |
944 | if (problem) { |
945 | if (fix_problem(ctx, problem, &cd->pctx)) { | |
21c84b71 TT |
946 | dirent->inode = 0; |
947 | dir_modified++; | |
948 | goto next; | |
1b6bf175 TT |
949 | } else { |
950 | ext2fs_unmark_valid(fs); | |
951 | if (problem == PR_2_BAD_INO) | |
952 | goto next; | |
21c84b71 | 953 | } |
3839e657 TT |
954 | } |
955 | ||
956 | /* | |
957 | * If the inode was marked as having bad fields in | |
958 | * pass1, process it and offer to fix/clear it. | |
959 | * (We wait until now so that we can display the | |
960 | * pathname to the user.) | |
961 | */ | |
1b6bf175 TT |
962 | if (ctx->inode_bad_map && |
963 | ext2fs_test_inode_bitmap(ctx->inode_bad_map, | |
3839e657 | 964 | dirent->inode)) { |
e72a9ba3 | 965 | if (e2fsck_process_bad_inode(ctx, ino, |
bcf9c5d4 TT |
966 | dirent->inode, |
967 | buf + fs->blocksize)) { | |
3839e657 TT |
968 | dirent->inode = 0; |
969 | dir_modified++; | |
970 | goto next; | |
971 | } | |
a02ce9df | 972 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
08b21301 | 973 | return DIRENT_ABORT; |
3839e657 TT |
974 | } |
975 | ||
49a7360b JS |
976 | group = ext2fs_group_of_ino(fs, dirent->inode); |
977 | first_unused_inode = group * fs->super->s_inodes_per_group + | |
978 | 1 + fs->super->s_inodes_per_group - | |
979 | fs->group_desc[group].bg_itable_unused; | |
980 | cd->pctx.group = group; | |
981 | ||
982 | /* | |
983 | * Check if the inode was missed out because _INODE_UNINIT | |
984 | * flag was set or bg_itable_unused was incorrect. | |
985 | * If that is the case restart e2fsck. | |
986 | * XXX Optimisations TODO: | |
987 | * 1. only restart e2fsck once | |
988 | * 2. only exposed inodes are checked again. | |
989 | */ | |
990 | if (fs->group_desc[group].bg_flags & EXT2_BG_INODE_UNINIT) { | |
991 | if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT, | |
992 | &cd->pctx)){ | |
993 | fs->group_desc[group].bg_flags &= | |
994 | ~EXT2_BG_INODE_UNINIT; | |
995 | ctx->flags |= E2F_FLAG_RESTART | | |
996 | E2F_FLAG_SIGNAL_MASK; | |
997 | } else { | |
998 | ext2fs_unmark_valid(fs); | |
999 | if (problem == PR_2_BAD_INO) | |
1000 | goto next; | |
1001 | } | |
1002 | } else if (dirent->inode >= first_unused_inode) { | |
1003 | if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){ | |
1004 | fs->group_desc[group].bg_itable_unused = 0; | |
1005 | fs->group_desc[group].bg_flags &= | |
1006 | ~EXT2_BG_INODE_UNINIT; | |
1007 | ext2fs_mark_super_dirty(fs); | |
1008 | ctx->flags |= E2F_FLAG_RESTART; | |
1009 | goto restart_fsck; | |
1010 | } else { | |
1011 | ext2fs_unmark_valid(fs); | |
1012 | if (problem == PR_2_BAD_INO) | |
1013 | goto next; | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, | |
1018 | dirent->inode))) { | |
1019 | /* | |
1020 | * If the inode is unused, offer to clear it. | |
1021 | */ | |
1022 | problem = PR_2_UNUSED_INODE; | |
1023 | } | |
1024 | ||
1025 | if (problem) { | |
1026 | if (fix_problem(ctx, problem, &cd->pctx)) { | |
1027 | dirent->inode = 0; | |
1028 | dir_modified++; | |
1029 | goto next; | |
1030 | } else { | |
1031 | ext2fs_unmark_valid(fs); | |
1032 | if (problem == PR_2_BAD_INO) | |
1033 | goto next; | |
1034 | } | |
1035 | } | |
1036 | ||
1b6bf175 TT |
1037 | if (check_name(ctx, dirent, ino, &cd->pctx)) |
1038 | dir_modified++; | |
1039 | ||
aa4115a4 TT |
1040 | if (check_filetype(ctx, dirent, ino, &cd->pctx)) |
1041 | dir_modified++; | |
1042 | ||
8fdc9985 TT |
1043 | #ifdef ENABLE_HTREE |
1044 | if (dx_db) { | |
1045 | ext2fs_dirhash(dx_dir->hashversion, dirent->name, | |
503f9e7f TT |
1046 | (dirent->name_len & 0xFF), |
1047 | fs->super->s_hash_seed, &hash, 0); | |
8fdc9985 TT |
1048 | if (hash < dx_db->min_hash) |
1049 | dx_db->min_hash = hash; | |
1050 | if (hash > dx_db->max_hash) | |
1051 | dx_db->max_hash = hash; | |
1052 | } | |
1053 | #endif | |
1054 | ||
3839e657 TT |
1055 | /* |
1056 | * If this is a directory, then mark its parent in its | |
1057 | * dir_info structure. If the parent field is already | |
1058 | * filled in, then this directory has more than one | |
1059 | * hard link. We assume the first link is correct, | |
1060 | * and ask the user if he/she wants to clear this one. | |
1061 | */ | |
e70ae99e | 1062 | if ((dot_state > 1) && |
1b6bf175 | 1063 | (ext2fs_test_inode_bitmap(ctx->inode_dir_map, |
3839e657 | 1064 | dirent->inode))) { |
28db82a8 TT |
1065 | if (e2fsck_dir_info_get_parent(ctx, dirent->inode, |
1066 | &subdir_parent)) { | |
1b6bf175 TT |
1067 | cd->pctx.ino = dirent->inode; |
1068 | fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx); | |
0926668d | 1069 | goto abort_free_dict; |
3839e657 | 1070 | } |
28db82a8 TT |
1071 | if (subdir_parent) { |
1072 | cd->pctx.ino2 = subdir_parent; | |
1b6bf175 | 1073 | if (fix_problem(ctx, PR_2_LINK_DIR, |
21c84b71 | 1074 | &cd->pctx)) { |
3839e657 TT |
1075 | dirent->inode = 0; |
1076 | dir_modified++; | |
1077 | goto next; | |
21c84b71 TT |
1078 | } |
1079 | cd->pctx.ino2 = 0; | |
28db82a8 TT |
1080 | } else { |
1081 | (void) e2fsck_dir_info_set_parent(ctx, | |
1082 | dirent->inode, ino); | |
1083 | } | |
3839e657 | 1084 | } |
0926668d TT |
1085 | |
1086 | if (dups_found) { | |
1087 | ; | |
1088 | } else if (dict_lookup(&de_dict, dirent)) { | |
1089 | clear_problem_context(&pctx); | |
1090 | pctx.ino = ino; | |
1091 | pctx.dirent = dirent; | |
1092 | fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx); | |
1093 | if (!ctx->dirs_to_hash) | |
1094 | ext2fs_u32_list_create(&ctx->dirs_to_hash, 50); | |
1095 | if (ctx->dirs_to_hash) | |
1096 | ext2fs_u32_list_add(ctx->dirs_to_hash, ino); | |
1097 | dups_found++; | |
1098 | } else | |
1099 | dict_alloc_insert(&de_dict, dirent, dirent); | |
3839e657 | 1100 | |
1b6bf175 TT |
1101 | ext2fs_icount_increment(ctx->inode_count, dirent->inode, |
1102 | &links); | |
21c84b71 | 1103 | if (links > 1) |
1b6bf175 TT |
1104 | ctx->fs_links_count++; |
1105 | ctx->fs_total_count++; | |
3839e657 | 1106 | next: |
e70ae99e | 1107 | prev = dirent; |
5dd77dbe TT |
1108 | if (dir_modified) |
1109 | rec_len = (dirent->rec_len || fs->blocksize < 65536) ? | |
1110 | dirent->rec_len : 65536; | |
1111 | offset += rec_len; | |
e70ae99e | 1112 | dot_state++; |
3839e657 TT |
1113 | } while (offset < fs->blocksize); |
1114 | #if 0 | |
1115 | printf("\n"); | |
1116 | #endif | |
8fdc9985 TT |
1117 | #ifdef ENABLE_HTREE |
1118 | if (dx_db) { | |
1119 | #ifdef DX_DEBUG | |
1120 | printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n", | |
1121 | db->blockcnt, dx_db->type, | |
1122 | dx_db->min_hash, dx_db->max_hash); | |
1123 | #endif | |
b7a00563 | 1124 | cd->pctx.dir = cd->pctx.ino; |
8fdc9985 TT |
1125 | if ((dx_db->type == DX_DIRBLOCK_ROOT) || |
1126 | (dx_db->type == DX_DIRBLOCK_NODE)) | |
1127 | parse_int_node(fs, db, cd, dx_dir, buf); | |
1128 | } | |
1129 | #endif /* ENABLE_HTREE */ | |
3839e657 | 1130 | if (offset != fs->blocksize) { |
5dd77dbe | 1131 | cd->pctx.num = rec_len - fs->blocksize + offset; |
1b6bf175 TT |
1132 | if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) { |
1133 | dirent->rec_len = cd->pctx.num; | |
1134 | dir_modified++; | |
1135 | } | |
3839e657 TT |
1136 | } |
1137 | if (dir_modified) { | |
1b6bf175 TT |
1138 | cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf); |
1139 | if (cd->pctx.errcode) { | |
08b21301 | 1140 | if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK, |
0926668d TT |
1141 | &cd->pctx)) |
1142 | goto abort_free_dict; | |
3839e657 TT |
1143 | } |
1144 | ext2fs_mark_changed(fs); | |
1145 | } | |
0926668d | 1146 | dict_free_nodes(&de_dict); |
3839e657 | 1147 | return 0; |
0926668d | 1148 | abort_free_dict: |
0926668d | 1149 | ctx->flags |= E2F_FLAG_ABORT; |
49a7360b JS |
1150 | restart_fsck: |
1151 | dict_free_nodes(&de_dict); | |
0926668d | 1152 | return DIRENT_ABORT; |
3839e657 TT |
1153 | } |
1154 | ||
1155 | /* | |
1156 | * This function is called to deallocate a block, and is an interator | |
1157 | * functioned called by deallocate inode via ext2fs_iterate_block(). | |
1158 | */ | |
1159 | static int deallocate_inode_block(ext2_filsys fs, | |
133a56dc | 1160 | blk_t *block_nr, |
54434927 TT |
1161 | e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), |
1162 | blk_t ref_block EXT2FS_ATTR((unused)), | |
1163 | int ref_offset EXT2FS_ATTR((unused)), | |
133a56dc | 1164 | void *priv_data) |
3839e657 | 1165 | { |
54dc7ca2 | 1166 | e2fsck_t ctx = (e2fsck_t) priv_data; |
1b6bf175 | 1167 | |
1917875f | 1168 | if (HOLE_BLKADDR(*block_nr)) |
3839e657 | 1169 | return 0; |
1ba7a2f2 TT |
1170 | if ((*block_nr < fs->super->s_first_data_block) || |
1171 | (*block_nr >= fs->super->s_blocks_count)) | |
1172 | return 0; | |
1b6bf175 | 1173 | ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr); |
0684a4f3 | 1174 | ext2fs_block_alloc_stats(fs, *block_nr, -1); |
3839e657 TT |
1175 | return 0; |
1176 | } | |
1177 | ||
1178 | /* | |
1179 | * This fuction deallocates an inode | |
1180 | */ | |
4035f40b | 1181 | static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf) |
3839e657 | 1182 | { |
1b6bf175 | 1183 | ext2_filsys fs = ctx->fs; |
3839e657 | 1184 | struct ext2_inode inode; |
1b6bf175 | 1185 | struct problem_context pctx; |
0684a4f3 | 1186 | __u32 count; |
1b6bf175 | 1187 | |
08b21301 | 1188 | e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode"); |
e3df15ab | 1189 | e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode"); |
1b6bf175 TT |
1190 | clear_problem_context(&pctx); |
1191 | pctx.ino = ino; | |
f3db3566 | 1192 | |
3839e657 TT |
1193 | /* |
1194 | * Fix up the bitmaps... | |
1195 | */ | |
f8188fff | 1196 | e2fsck_read_bitmaps(ctx); |
0684a4f3 TT |
1197 | ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode)); |
1198 | ||
1199 | if (inode.i_file_acl && | |
1200 | (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) { | |
1201 | pctx.errcode = ext2fs_adjust_ea_refcount(fs, inode.i_file_acl, | |
1202 | block_buf, -1, &count); | |
1203 | if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) { | |
1204 | pctx.errcode = 0; | |
1205 | count = 1; | |
1206 | } | |
1207 | if (pctx.errcode) { | |
1208 | pctx.blk = inode.i_file_acl; | |
1209 | fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx); | |
1210 | ctx->flags |= E2F_FLAG_ABORT; | |
1211 | return; | |
1212 | } | |
1213 | if (count == 0) { | |
1214 | ext2fs_unmark_block_bitmap(ctx->block_found_map, | |
1215 | inode.i_file_acl); | |
1216 | ext2fs_block_alloc_stats(fs, inode.i_file_acl, -1); | |
1217 | } | |
1218 | inode.i_file_acl = 0; | |
1219 | } | |
3839e657 | 1220 | |
21c84b71 | 1221 | if (!ext2fs_inode_has_valid_blocks(&inode)) |
3839e657 | 1222 | return; |
a4742691 | 1223 | |
b94a052a | 1224 | if (LINUX_S_ISREG(inode.i_mode) && |
a4742691 TT |
1225 | (inode.i_size_high || inode.i_size & 0x80000000UL)) |
1226 | ctx->large_files--; | |
1227 | ||
133a56dc | 1228 | pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf, |
1b6bf175 TT |
1229 | deallocate_inode_block, ctx); |
1230 | if (pctx.errcode) { | |
1231 | fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx); | |
08b21301 TT |
1232 | ctx->flags |= E2F_FLAG_ABORT; |
1233 | return; | |
1b6bf175 | 1234 | } |
3839e657 TT |
1235 | } |
1236 | ||
8fdc9985 TT |
1237 | /* |
1238 | * This fuction clears the htree flag on an inode | |
1239 | */ | |
1240 | static void clear_htree(e2fsck_t ctx, ext2_ino_t ino) | |
1241 | { | |
1242 | struct ext2_inode inode; | |
8fdc9985 TT |
1243 | |
1244 | e2fsck_read_inode(ctx, ino, &inode, "clear_htree"); | |
1245 | inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL; | |
1246 | e2fsck_write_inode(ctx, ino, &inode, "clear_htree"); | |
b7a00563 TT |
1247 | if (ctx->dirs_to_hash) |
1248 | ext2fs_u32_list_add(ctx->dirs_to_hash, ino); | |
8fdc9985 TT |
1249 | } |
1250 | ||
1251 | ||
86c627ec | 1252 | extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir, |
bcf9c5d4 | 1253 | ext2_ino_t ino, char *buf) |
3839e657 | 1254 | { |
1b6bf175 | 1255 | ext2_filsys fs = ctx->fs; |
3839e657 | 1256 | struct ext2_inode inode; |
3839e657 | 1257 | int inode_modified = 0; |
6c313fd4 | 1258 | int not_fixed = 0; |
1e3472c5 | 1259 | unsigned char *frag, *fsize; |
21c84b71 | 1260 | struct problem_context pctx; |
08b21301 | 1261 | int problem = 0; |
3839e657 | 1262 | |
08b21301 | 1263 | e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode"); |
21c84b71 TT |
1264 | |
1265 | clear_problem_context(&pctx); | |
1266 | pctx.ino = ino; | |
1267 | pctx.dir = dir; | |
1268 | pctx.inode = &inode; | |
1269 | ||
6c313fd4 | 1270 | if (inode.i_file_acl && |
f76344fb TT |
1271 | !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) { |
1272 | if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) { | |
1273 | inode.i_file_acl = 0; | |
f76344fb TT |
1274 | inode_modified++; |
1275 | } else | |
1276 | not_fixed++; | |
1277 | } | |
6c313fd4 | 1278 | |
50e1e10f TT |
1279 | if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) && |
1280 | !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) && | |
1281 | !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) && | |
08b21301 TT |
1282 | !(LINUX_S_ISSOCK(inode.i_mode))) |
1283 | problem = PR_2_BAD_MODE; | |
fdbdea09 | 1284 | else if (LINUX_S_ISCHR(inode.i_mode) |
0684a4f3 | 1285 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
08b21301 | 1286 | problem = PR_2_BAD_CHAR_DEV; |
fdbdea09 | 1287 | else if (LINUX_S_ISBLK(inode.i_mode) |
0684a4f3 | 1288 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
08b21301 | 1289 | problem = PR_2_BAD_BLOCK_DEV; |
fdbdea09 | 1290 | else if (LINUX_S_ISFIFO(inode.i_mode) |
0684a4f3 | 1291 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
1dde43f0 | 1292 | problem = PR_2_BAD_FIFO; |
fdbdea09 | 1293 | else if (LINUX_S_ISSOCK(inode.i_mode) |
0684a4f3 | 1294 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
1dde43f0 | 1295 | problem = PR_2_BAD_SOCKET; |
fdbdea09 | 1296 | else if (LINUX_S_ISLNK(inode.i_mode) |
7cadc577 | 1297 | && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) { |
bcf9c5d4 | 1298 | problem = PR_2_INVALID_SYMLINK; |
67052a8a | 1299 | } |
1dde43f0 | 1300 | |
08b21301 TT |
1301 | if (problem) { |
1302 | if (fix_problem(ctx, problem, &pctx)) { | |
1b6bf175 | 1303 | deallocate_inode(ctx, ino, 0); |
a02ce9df | 1304 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
08b21301 | 1305 | return 0; |
7cf73dcd | 1306 | return 1; |
6c313fd4 TT |
1307 | } else |
1308 | not_fixed++; | |
08b21301 | 1309 | problem = 0; |
7cf73dcd | 1310 | } |
7cf73dcd | 1311 | |
6c313fd4 TT |
1312 | if (inode.i_faddr) { |
1313 | if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) { | |
1314 | inode.i_faddr = 0; | |
1315 | inode_modified++; | |
1316 | } else | |
1317 | not_fixed++; | |
3839e657 | 1318 | } |
1e3472c5 TT |
1319 | |
1320 | switch (fs->super->s_creator_os) { | |
1e3472c5 TT |
1321 | case EXT2_OS_HURD: |
1322 | frag = &inode.osd2.hurd2.h_i_frag; | |
1323 | fsize = &inode.osd2.hurd2.h_i_fsize; | |
1324 | break; | |
1e3472c5 TT |
1325 | default: |
1326 | frag = fsize = 0; | |
1327 | } | |
21c84b71 TT |
1328 | if (frag && *frag) { |
1329 | pctx.num = *frag; | |
1b6bf175 | 1330 | if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) { |
21c84b71 TT |
1331 | *frag = 0; |
1332 | inode_modified++; | |
7e0282c5 TT |
1333 | } else |
1334 | not_fixed++; | |
21c84b71 TT |
1335 | pctx.num = 0; |
1336 | } | |
1337 | if (fsize && *fsize) { | |
1338 | pctx.num = *fsize; | |
1b6bf175 | 1339 | if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) { |
21c84b71 TT |
1340 | *fsize = 0; |
1341 | inode_modified++; | |
6c313fd4 TT |
1342 | } else |
1343 | not_fixed++; | |
21c84b71 TT |
1344 | pctx.num = 0; |
1345 | } | |
1346 | ||
5d17119d TT |
1347 | if ((fs->super->s_creator_os == EXT2_OS_LINUX) && |
1348 | !(fs->super->s_feature_ro_compat & | |
1349 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE) && | |
1350 | (inode.osd2.linux2.l_i_blocks_hi != 0)) { | |
1351 | pctx.num = inode.osd2.linux2.l_i_blocks_hi; | |
1352 | if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) { | |
1353 | inode.osd2.linux2.l_i_blocks_hi = 0; | |
1354 | inode_modified++; | |
1355 | } | |
1356 | } | |
1357 | ||
342d847d TT |
1358 | if (inode.i_file_acl && |
1359 | ((inode.i_file_acl < fs->super->s_first_data_block) || | |
6c313fd4 TT |
1360 | (inode.i_file_acl >= fs->super->s_blocks_count))) { |
1361 | if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) { | |
1362 | inode.i_file_acl = 0; | |
1363 | inode_modified++; | |
1364 | } else | |
1365 | not_fixed++; | |
342d847d | 1366 | } |
21c84b71 | 1367 | if (inode.i_dir_acl && |
6c313fd4 TT |
1368 | LINUX_S_ISDIR(inode.i_mode)) { |
1369 | if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) { | |
1370 | inode.i_dir_acl = 0; | |
1371 | inode_modified++; | |
1372 | } else | |
1373 | not_fixed++; | |
21c84b71 | 1374 | } |
6c313fd4 | 1375 | |
f3db3566 | 1376 | if (inode_modified) |
08b21301 | 1377 | e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode"); |
f76344fb | 1378 | if (!not_fixed && ctx->inode_bad_map) |
6c313fd4 | 1379 | ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino); |
3839e657 TT |
1380 | return 0; |
1381 | } | |
1382 | ||
50e1e10f TT |
1383 | |
1384 | /* | |
1385 | * allocate_dir_block --- this function allocates a new directory | |
1386 | * block for a particular inode; this is done if a directory has | |
1387 | * a "hole" in it, or if a directory has a illegal block number | |
1388 | * that was zeroed out and now needs to be replaced. | |
1389 | */ | |
1b6bf175 | 1390 | static int allocate_dir_block(e2fsck_t ctx, |
21c84b71 | 1391 | struct ext2_db_entry *db, |
54434927 TT |
1392 | char *buf EXT2FS_ATTR((unused)), |
1393 | struct problem_context *pctx) | |
50e1e10f | 1394 | { |
1b6bf175 | 1395 | ext2_filsys fs = ctx->fs; |
50e1e10f TT |
1396 | blk_t blk; |
1397 | char *block; | |
1398 | struct ext2_inode inode; | |
50e1e10f | 1399 | |
1b6bf175 | 1400 | if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0) |
50e1e10f TT |
1401 | return 1; |
1402 | ||
1403 | /* | |
1404 | * Read the inode and block bitmaps in; we'll be messing with | |
1405 | * them. | |
1406 | */ | |
f8188fff | 1407 | e2fsck_read_bitmaps(ctx); |
50e1e10f TT |
1408 | |
1409 | /* | |
1410 | * First, find a free block | |
1411 | */ | |
1b6bf175 TT |
1412 | pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk); |
1413 | if (pctx->errcode) { | |
1414 | pctx->str = "ext2fs_new_block"; | |
1415 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); | |
50e1e10f TT |
1416 | return 1; |
1417 | } | |
1b6bf175 | 1418 | ext2fs_mark_block_bitmap(ctx->block_found_map, blk); |
50e1e10f TT |
1419 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
1420 | ext2fs_mark_bb_dirty(fs); | |
1421 | ||
1422 | /* | |
1423 | * Now let's create the actual data block for the inode | |
1424 | */ | |
1425 | if (db->blockcnt) | |
1b6bf175 | 1426 | pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block); |
50e1e10f | 1427 | else |
1b6bf175 TT |
1428 | pctx->errcode = ext2fs_new_dir_block(fs, db->ino, |
1429 | EXT2_ROOT_INO, &block); | |
50e1e10f | 1430 | |
1b6bf175 TT |
1431 | if (pctx->errcode) { |
1432 | pctx->str = "ext2fs_new_dir_block"; | |
1433 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); | |
50e1e10f TT |
1434 | return 1; |
1435 | } | |
1436 | ||
1b6bf175 | 1437 | pctx->errcode = ext2fs_write_dir_block(fs, blk, block); |
c4e3d3f3 | 1438 | ext2fs_free_mem(&block); |
1b6bf175 TT |
1439 | if (pctx->errcode) { |
1440 | pctx->str = "ext2fs_write_dir_block"; | |
1441 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); | |
50e1e10f TT |
1442 | return 1; |
1443 | } | |
1444 | ||
1445 | /* | |
1446 | * Update the inode block count | |
1447 | */ | |
08b21301 | 1448 | e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block"); |
1ca1059f | 1449 | ext2fs_iblk_add_blocks(fs, &inode, 1); |
50e1e10f TT |
1450 | if (inode.i_size < (db->blockcnt+1) * fs->blocksize) |
1451 | inode.i_size = (db->blockcnt+1) * fs->blocksize; | |
08b21301 | 1452 | e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block"); |
50e1e10f TT |
1453 | |
1454 | /* | |
1455 | * Finally, update the block pointers for the inode | |
1456 | */ | |
1457 | db->blk = blk; | |
133a56dc | 1458 | pctx->errcode = ext2fs_block_iterate2(fs, db->ino, BLOCK_FLAG_HOLE, |
50e1e10f | 1459 | 0, update_dir_block, db); |
1b6bf175 TT |
1460 | if (pctx->errcode) { |
1461 | pctx->str = "ext2fs_block_iterate"; | |
1462 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); | |
50e1e10f TT |
1463 | return 1; |
1464 | } | |
1465 | ||
1466 | return 0; | |
1467 | } | |
1468 | ||
1469 | /* | |
1470 | * This is a helper function for allocate_dir_block(). | |
1471 | */ | |
54434927 | 1472 | static int update_dir_block(ext2_filsys fs EXT2FS_ATTR((unused)), |
50e1e10f | 1473 | blk_t *block_nr, |
133a56dc | 1474 | e2_blkcnt_t blockcnt, |
54434927 TT |
1475 | blk_t ref_block EXT2FS_ATTR((unused)), |
1476 | int ref_offset EXT2FS_ATTR((unused)), | |
54dc7ca2 | 1477 | void *priv_data) |
50e1e10f | 1478 | { |
54dc7ca2 | 1479 | struct ext2_db_entry *db; |
50e1e10f | 1480 | |
54dc7ca2 | 1481 | db = (struct ext2_db_entry *) priv_data; |
133a56dc | 1482 | if (db->blockcnt == (int) blockcnt) { |
50e1e10f TT |
1483 | *block_nr = db->blk; |
1484 | return BLOCK_CHANGED; | |
1485 | } | |
1486 | return 0; | |
1487 | } |