]>
Commit | Line | Data |
---|---|---|
b7a00563 TT |
1 | /* |
2 | * rehash.c --- rebuild hash tree directories | |
efc6f628 | 3 | * |
b7a00563 TT |
4 | * Copyright (C) 2002 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 | * |
b7a00563 TT |
11 | * This algorithm is designed for simplicity of implementation and to |
12 | * pack the directory as much as possible. It however requires twice | |
13 | * as much memory as the size of the directory. The maximum size | |
14 | * directory supported using a 4k blocksize is roughly a gigabyte, and | |
15 | * so there may very well be problems with machines that don't have | |
16 | * virtual memory, and obscenely large directories. | |
17 | * | |
18 | * An alternate algorithm which is much more disk intensive could be | |
19 | * written, and probably will need to be written in the future. The | |
20 | * design goals of such an algorithm are: (a) use (roughly) constant | |
21 | * amounts of memory, no matter how large the directory, (b) the | |
22 | * directory must be safe at all times, even if e2fsck is interrupted | |
23 | * in the middle, (c) we must use minimal amounts of extra disk | |
24 | * blocks. This pretty much requires an incremental approach, where | |
25 | * we are reading from one part of the directory, and inserting into | |
26 | * the front half. So the algorithm will have to keep track of a | |
27 | * moving block boundary between the new tree and the old tree, and | |
28 | * files will need to be moved from the old directory and inserted | |
29 | * into the new tree. If the new directory requires space which isn't | |
30 | * yet available, blocks from the beginning part of the old directory | |
31 | * may need to be moved to the end of the directory to make room for | |
32 | * the new tree: | |
33 | * | |
34 | * -------------------------------------------------------- | |
35 | * | new tree | | old tree | | |
36 | * -------------------------------------------------------- | |
37 | * ^ ptr ^ptr | |
38 | * tail new head old | |
efc6f628 | 39 | * |
b7a00563 TT |
40 | * This is going to be a pain in the tuckus to implement, and will |
41 | * require a lot more disk accesses. So I'm going to skip it for now; | |
42 | * it's only really going to be an issue for really, really big | |
43 | * filesystems (when we reach the level of tens of millions of files | |
44 | * in a single directory). It will probably be easier to simply | |
45 | * require that e2fsck use VM first. | |
46 | */ | |
47 | ||
d1154eb4 | 48 | #include "config.h" |
520ead37 TT |
49 | #include <string.h> |
50 | #include <ctype.h> | |
b7a00563 TT |
51 | #include <errno.h> |
52 | #include "e2fsck.h" | |
53 | #include "problem.h" | |
54 | ||
07307114 DW |
55 | /* Schedule a dir to be rebuilt during pass 3A. */ |
56 | void e2fsck_rehash_dir_later(e2fsck_t ctx, ext2_ino_t ino) | |
57 | { | |
58 | if (!ctx->dirs_to_hash) | |
59 | ext2fs_u32_list_create(&ctx->dirs_to_hash, 50); | |
60 | if (ctx->dirs_to_hash) | |
61 | ext2fs_u32_list_add(ctx->dirs_to_hash, ino); | |
62 | } | |
63 | ||
64 | /* Ask if a dir will be rebuilt during pass 3A. */ | |
65 | int e2fsck_dir_will_be_rehashed(e2fsck_t ctx, ext2_ino_t ino) | |
66 | { | |
67 | if (ctx->options & E2F_OPT_COMPRESS_DIRS) | |
68 | return 1; | |
69 | if (!ctx->dirs_to_hash) | |
70 | return 0; | |
71 | return ext2fs_u32_list_test(ctx->dirs_to_hash, ino); | |
72 | } | |
73 | ||
19961cd0 AD |
74 | #undef REHASH_DEBUG |
75 | ||
b7a00563 TT |
76 | struct fill_dir_struct { |
77 | char *buf; | |
78 | struct ext2_inode *inode; | |
62ad2480 | 79 | ext2_ino_t ino; |
974d57d3 | 80 | errcode_t err; |
b7a00563 TT |
81 | e2fsck_t ctx; |
82 | struct hash_entry *harray; | |
83 | int max_array, num_array; | |
68477355 | 84 | unsigned int dir_size; |
850d05e9 | 85 | int compress; |
b7a00563 | 86 | ino_t parent; |
81683c6a | 87 | ext2_ino_t dir; |
b7a00563 TT |
88 | }; |
89 | ||
90 | struct hash_entry { | |
91 | ext2_dirhash_t hash; | |
92 | ext2_dirhash_t minor_hash; | |
d66c3832 | 93 | ino_t ino; |
b7a00563 TT |
94 | struct ext2_dir_entry *dir; |
95 | }; | |
96 | ||
97 | struct out_dir { | |
98 | int num; | |
99 | int max; | |
100 | char *buf; | |
101 | ext2_dirhash_t *hashes; | |
102 | }; | |
103 | ||
104 | static int fill_dir_block(ext2_filsys fs, | |
6dc64392 | 105 | blk64_t *block_nr, |
b7a00563 | 106 | e2_blkcnt_t blockcnt, |
6dc64392 | 107 | blk64_t ref_block EXT2FS_ATTR((unused)), |
54434927 | 108 | int ref_offset EXT2FS_ATTR((unused)), |
b7a00563 TT |
109 | void *priv_data) |
110 | { | |
111 | struct fill_dir_struct *fd = (struct fill_dir_struct *) priv_data; | |
112 | struct hash_entry *new_array, *ent; | |
113 | struct ext2_dir_entry *dirent; | |
114 | char *dir; | |
70f4632b | 115 | unsigned int offset, dir_offset, rec_len, name_len; |
8a480350 | 116 | int hash_alg; |
efc6f628 | 117 | |
b7a00563 TT |
118 | if (blockcnt < 0) |
119 | return 0; | |
120 | ||
121 | offset = blockcnt * fs->blocksize; | |
122 | if (offset + fs->blocksize > fd->inode->i_size) { | |
123 | fd->err = EXT2_ET_DIR_CORRUPTED; | |
124 | return BLOCK_ABORT; | |
125 | } | |
62ad2480 | 126 | |
b7a00563 | 127 | dir = (fd->buf+offset); |
4a05268c | 128 | if (*block_nr == 0) { |
b7a00563 TT |
129 | memset(dir, 0, fs->blocksize); |
130 | dirent = (struct ext2_dir_entry *) dir; | |
8a480350 | 131 | (void) ext2fs_set_rec_len(fs, fs->blocksize, dirent); |
b7a00563 | 132 | } else { |
2e9d8391 | 133 | int flags = fs->flags; |
81683c6a DW |
134 | fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS; |
135 | fd->err = ext2fs_read_dir_block4(fs, *block_nr, dir, 0, | |
136 | fd->dir); | |
2e9d8391 DW |
137 | fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) | |
138 | (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS); | |
b7a00563 TT |
139 | if (fd->err) |
140 | return BLOCK_ABORT; | |
141 | } | |
f77704e4 TT |
142 | hash_alg = fs->super->s_def_hash_version; |
143 | if ((hash_alg <= EXT2_HASH_TEA) && | |
144 | (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH)) | |
145 | hash_alg += 3; | |
b7a00563 TT |
146 | /* While the directory block is "hot", index it. */ |
147 | dir_offset = 0; | |
148 | while (dir_offset < fs->blocksize) { | |
149 | dirent = (struct ext2_dir_entry *) (dir + dir_offset); | |
8a480350 | 150 | (void) ext2fs_get_rec_len(fs, dirent, &rec_len); |
70f4632b | 151 | name_len = ext2fs_dirent_name_len(dirent); |
5dd77dbe TT |
152 | if (((dir_offset + rec_len) > fs->blocksize) || |
153 | (rec_len < 8) || | |
154 | ((rec_len % 4) != 0) || | |
70f4632b | 155 | (name_len + 8 > rec_len)) { |
b7a00563 TT |
156 | fd->err = EXT2_ET_DIR_CORRUPTED; |
157 | return BLOCK_ABORT; | |
158 | } | |
5dd77dbe | 159 | dir_offset += rec_len; |
b7a00563 | 160 | if (dirent->inode == 0) |
850d05e9 | 161 | continue; |
70f4632b | 162 | if (!fd->compress && (name_len == 1) && |
850d05e9 TT |
163 | (dirent->name[0] == '.')) |
164 | continue; | |
70f4632b | 165 | if (!fd->compress && (name_len == 2) && |
b7a00563 TT |
166 | (dirent->name[0] == '.') && (dirent->name[1] == '.')) { |
167 | fd->parent = dirent->inode; | |
850d05e9 | 168 | continue; |
b7a00563 TT |
169 | } |
170 | if (fd->num_array >= fd->max_array) { | |
171 | new_array = realloc(fd->harray, | |
172 | sizeof(struct hash_entry) * (fd->max_array+500)); | |
173 | if (!new_array) { | |
174 | fd->err = ENOMEM; | |
175 | return BLOCK_ABORT; | |
176 | } | |
177 | fd->harray = new_array; | |
178 | fd->max_array += 500; | |
179 | } | |
850d05e9 | 180 | ent = fd->harray + fd->num_array++; |
b7a00563 | 181 | ent->dir = dirent; |
70f4632b | 182 | fd->dir_size += EXT2_DIR_REC_LEN(name_len); |
d66c3832 | 183 | ent->ino = dirent->inode; |
850d05e9 TT |
184 | if (fd->compress) |
185 | ent->hash = ent->minor_hash = 0; | |
186 | else { | |
437651ad TT |
187 | fd->err = ext2fs_dirhash(hash_alg, dirent->name, |
188 | name_len, | |
189 | fs->super->s_hash_seed, | |
190 | &ent->hash, &ent->minor_hash); | |
850d05e9 TT |
191 | if (fd->err) |
192 | return BLOCK_ABORT; | |
193 | } | |
b7a00563 | 194 | } |
efc6f628 | 195 | |
b7a00563 TT |
196 | return 0; |
197 | } | |
198 | ||
d66c3832 TT |
199 | /* Used for sorting the hash entry */ |
200 | static EXT2_QSORT_TYPE ino_cmp(const void *a, const void *b) | |
201 | { | |
202 | const struct hash_entry *he_a = (const struct hash_entry *) a; | |
203 | const struct hash_entry *he_b = (const struct hash_entry *) b; | |
204 | ||
205 | return (he_a->ino - he_b->ino); | |
206 | } | |
207 | ||
b7a00563 | 208 | /* Used for sorting the hash entry */ |
b0700a1b | 209 | static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b) |
b7a00563 TT |
210 | { |
211 | const struct hash_entry *he_a = (const struct hash_entry *) a; | |
212 | const struct hash_entry *he_b = (const struct hash_entry *) b; | |
62f9bd0e | 213 | unsigned int he_a_len, he_b_len, min_len; |
b7a00563 | 214 | int ret; |
b0700a1b | 215 | |
70f4632b JK |
216 | he_a_len = ext2fs_dirent_name_len(he_a->dir); |
217 | he_b_len = ext2fs_dirent_name_len(he_b->dir); | |
218 | min_len = he_a_len; | |
219 | if (min_len > he_b_len) | |
220 | min_len = he_b_len; | |
b0700a1b | 221 | |
5b8d7c51 | 222 | ret = memcmp(he_a->dir->name, he_b->dir->name, min_len); |
b0700a1b | 223 | if (ret == 0) { |
70f4632b | 224 | if (he_a_len > he_b_len) |
b7a00563 | 225 | ret = 1; |
70f4632b | 226 | else if (he_a_len < he_b_len) |
b7a00563 TT |
227 | ret = -1; |
228 | else | |
12dd69f5 | 229 | ret = he_b->dir->inode - he_a->dir->inode; |
b7a00563 TT |
230 | } |
231 | return ret; | |
232 | } | |
233 | ||
850d05e9 | 234 | /* Used for sorting the hash entry */ |
b0700a1b | 235 | static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b) |
850d05e9 TT |
236 | { |
237 | const struct hash_entry *he_a = (const struct hash_entry *) a; | |
238 | const struct hash_entry *he_b = (const struct hash_entry *) b; | |
239 | int ret; | |
efc6f628 | 240 | |
b0700a1b TT |
241 | if (he_a->hash > he_b->hash) |
242 | ret = 1; | |
243 | else if (he_a->hash < he_b->hash) | |
244 | ret = -1; | |
245 | else { | |
246 | if (he_a->minor_hash > he_b->minor_hash) | |
850d05e9 | 247 | ret = 1; |
b0700a1b | 248 | else if (he_a->minor_hash < he_b->minor_hash) |
850d05e9 TT |
249 | ret = -1; |
250 | else | |
b0700a1b | 251 | ret = name_cmp(a, b); |
850d05e9 TT |
252 | } |
253 | return ret; | |
254 | } | |
255 | ||
efc6f628 | 256 | static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir, |
b7a00563 TT |
257 | int blocks) |
258 | { | |
259 | void *new_mem; | |
260 | ||
261 | if (outdir->max) { | |
262 | new_mem = realloc(outdir->buf, blocks * fs->blocksize); | |
263 | if (!new_mem) | |
264 | return ENOMEM; | |
265 | outdir->buf = new_mem; | |
266 | new_mem = realloc(outdir->hashes, | |
267 | blocks * sizeof(ext2_dirhash_t)); | |
268 | if (!new_mem) | |
269 | return ENOMEM; | |
270 | outdir->hashes = new_mem; | |
271 | } else { | |
272 | outdir->buf = malloc(blocks * fs->blocksize); | |
273 | outdir->hashes = malloc(blocks * sizeof(ext2_dirhash_t)); | |
274 | outdir->num = 0; | |
275 | } | |
276 | outdir->max = blocks; | |
277 | return 0; | |
278 | } | |
279 | ||
280 | static void free_out_dir(struct out_dir *outdir) | |
281 | { | |
45e338f5 JM |
282 | free(outdir->buf); |
283 | free(outdir->hashes); | |
b7a00563 TT |
284 | outdir->max = 0; |
285 | outdir->num =0; | |
286 | } | |
287 | ||
850d05e9 | 288 | static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir, |
b7a00563 TT |
289 | char ** ret) |
290 | { | |
291 | errcode_t retval; | |
292 | ||
293 | if (outdir->num >= outdir->max) { | |
294 | retval = alloc_size_dir(fs, outdir, outdir->max + 50); | |
295 | if (retval) | |
296 | return retval; | |
297 | } | |
298 | *ret = outdir->buf + (outdir->num++ * fs->blocksize); | |
850d05e9 | 299 | memset(*ret, 0, fs->blocksize); |
b7a00563 TT |
300 | return 0; |
301 | } | |
302 | ||
b0700a1b TT |
303 | /* |
304 | * This function is used to make a unique filename. We do this by | |
305 | * appending ~0, and then incrementing the number. However, we cannot | |
306 | * expand the length of the filename beyond the padding available in | |
307 | * the directory entry. | |
308 | */ | |
70f4632b | 309 | static void mutate_name(char *str, unsigned int *len) |
b0700a1b | 310 | { |
62f9bd0e | 311 | int i; |
70f4632b | 312 | unsigned int l = *len; |
efc6f628 | 313 | |
b0700a1b TT |
314 | /* |
315 | * First check to see if it looks the name has been mutated | |
316 | * already | |
317 | */ | |
318 | for (i = l-1; i > 0; i--) { | |
319 | if (!isdigit(str[i])) | |
320 | break; | |
321 | } | |
62f9bd0e | 322 | if ((i == (int)l - 1) || (str[i] != '~')) { |
b0700a1b TT |
323 | if (((l-1) & 3) < 2) |
324 | l += 2; | |
325 | else | |
326 | l = (l+3) & ~3; | |
327 | str[l-2] = '~'; | |
328 | str[l-1] = '0'; | |
70f4632b | 329 | *len = l; |
b0700a1b TT |
330 | return; |
331 | } | |
332 | for (i = l-1; i >= 0; i--) { | |
333 | if (isdigit(str[i])) { | |
334 | if (str[i] == '9') | |
335 | str[i] = '0'; | |
336 | else { | |
337 | str[i]++; | |
338 | return; | |
339 | } | |
340 | continue; | |
341 | } | |
342 | if (i == 1) { | |
343 | if (str[0] == 'z') | |
344 | str[0] = 'A'; | |
345 | else if (str[0] == 'Z') { | |
346 | str[0] = '~'; | |
347 | str[1] = '0'; | |
348 | } else | |
349 | str[0]++; | |
350 | } else if (i > 0) { | |
351 | str[i] = '1'; | |
352 | str[i-1] = '~'; | |
353 | } else { | |
354 | if (str[0] == '~') | |
355 | str[0] = 'a'; | |
efc6f628 | 356 | else |
b0700a1b TT |
357 | str[0]++; |
358 | } | |
359 | break; | |
360 | } | |
361 | } | |
362 | ||
363 | static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs, | |
364 | ext2_ino_t ino, | |
365 | struct fill_dir_struct *fd) | |
366 | { | |
367 | struct problem_context pctx; | |
368 | struct hash_entry *ent, *prev; | |
369 | int i, j; | |
370 | int fixed = 0; | |
371 | char new_name[256]; | |
70f4632b | 372 | unsigned int new_len; |
f77704e4 | 373 | int hash_alg; |
efc6f628 | 374 | |
b0700a1b TT |
375 | clear_problem_context(&pctx); |
376 | pctx.ino = ino; | |
377 | ||
f77704e4 TT |
378 | hash_alg = fs->super->s_def_hash_version; |
379 | if ((hash_alg <= EXT2_HASH_TEA) && | |
380 | (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH)) | |
381 | hash_alg += 3; | |
382 | ||
b0700a1b TT |
383 | for (i=1; i < fd->num_array; i++) { |
384 | ent = fd->harray + i; | |
385 | prev = ent - 1; | |
386 | if (!ent->dir->inode || | |
70f4632b JK |
387 | (ext2fs_dirent_name_len(ent->dir) != |
388 | ext2fs_dirent_name_len(prev->dir)) || | |
5b8d7c51 | 389 | memcmp(ent->dir->name, prev->dir->name, |
70f4632b | 390 | ext2fs_dirent_name_len(ent->dir))) |
b0700a1b TT |
391 | continue; |
392 | pctx.dirent = ent->dir; | |
393 | if ((ent->dir->inode == prev->dir->inode) && | |
394 | fix_problem(ctx, PR_2_DUPLICATE_DIRENT, &pctx)) { | |
395 | e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1); | |
396 | ent->dir->inode = 0; | |
397 | fixed++; | |
398 | continue; | |
399 | } | |
70f4632b JK |
400 | new_len = ext2fs_dirent_name_len(ent->dir); |
401 | memcpy(new_name, ent->dir->name, new_len); | |
b0700a1b TT |
402 | mutate_name(new_name, &new_len); |
403 | for (j=0; j < fd->num_array; j++) { | |
404 | if ((i==j) || | |
70f4632b | 405 | (new_len != |
62f9bd0e | 406 | (unsigned) ext2fs_dirent_name_len(fd->harray[j].dir)) || |
5b8d7c51 | 407 | memcmp(new_name, fd->harray[j].dir->name, new_len)) |
b0700a1b TT |
408 | continue; |
409 | mutate_name(new_name, &new_len); | |
efc6f628 | 410 | |
b0700a1b TT |
411 | j = -1; |
412 | } | |
70f4632b | 413 | new_name[new_len] = 0; |
b0700a1b TT |
414 | pctx.str = new_name; |
415 | if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) { | |
70f4632b JK |
416 | memcpy(ent->dir->name, new_name, new_len); |
417 | ext2fs_dirent_set_name_len(ent->dir, new_len); | |
437651ad TT |
418 | ext2fs_dirhash(hash_alg, new_name, new_len, |
419 | fs->super->s_hash_seed, | |
420 | &ent->hash, &ent->minor_hash); | |
b0700a1b TT |
421 | fixed++; |
422 | } | |
423 | } | |
424 | return fixed; | |
425 | } | |
426 | ||
b7a00563 | 427 | |
7dca4c88 | 428 | static errcode_t copy_dir_entries(e2fsck_t ctx, |
850d05e9 | 429 | struct fill_dir_struct *fd, |
fd9ca825 | 430 | struct out_dir *outdir) |
850d05e9 | 431 | { |
7dca4c88 | 432 | ext2_filsys fs = ctx->fs; |
850d05e9 TT |
433 | errcode_t retval; |
434 | char *block_start; | |
435 | struct hash_entry *ent; | |
436 | struct ext2_dir_entry *dirent; | |
68477355 TT |
437 | unsigned int rec_len, prev_rec_len, left, slack, offset; |
438 | int i; | |
850d05e9 | 439 | ext2_dirhash_t prev_hash; |
81683c6a DW |
440 | int csum_size = 0; |
441 | struct ext2_dir_entry_tail *t; | |
7dca4c88 TT |
442 | |
443 | if (ctx->htree_slack_percentage == 255) { | |
444 | profile_get_uint(ctx->profile, "options", | |
445 | "indexed_dir_slack_percentage", | |
446 | 0, 20, | |
447 | &ctx->htree_slack_percentage); | |
448 | if (ctx->htree_slack_percentage > 100) | |
449 | ctx->htree_slack_percentage = 20; | |
450 | } | |
efc6f628 | 451 | |
86f3b6cf | 452 | if (ext2fs_has_feature_metadata_csum(fs->super)) |
81683c6a DW |
453 | csum_size = sizeof(struct ext2_dir_entry_tail); |
454 | ||
850d05e9 TT |
455 | outdir->max = 0; |
456 | retval = alloc_size_dir(fs, outdir, | |
457 | (fd->dir_size / fs->blocksize) + 2); | |
458 | if (retval) | |
459 | return retval; | |
460 | outdir->num = fd->compress ? 0 : 1; | |
461 | offset = 0; | |
462 | outdir->hashes[0] = 0; | |
463 | prev_hash = 1; | |
464 | if ((retval = get_next_block(fs, outdir, &block_start))) | |
465 | return retval; | |
466 | dirent = (struct ext2_dir_entry *) block_start; | |
8a480350 | 467 | prev_rec_len = 0; |
cf5301d7 | 468 | rec_len = 0; |
81683c6a | 469 | left = fs->blocksize - csum_size; |
7dca4c88 | 470 | slack = fd->compress ? 12 : |
81683c6a | 471 | ((fs->blocksize - csum_size) * ctx->htree_slack_percentage)/100; |
7dca4c88 TT |
472 | if (slack < 12) |
473 | slack = 12; | |
cf5301d7 | 474 | for (i = 0; i < fd->num_array; i++) { |
850d05e9 | 475 | ent = fd->harray + i; |
b0700a1b TT |
476 | if (ent->dir->inode == 0) |
477 | continue; | |
70f4632b | 478 | rec_len = EXT2_DIR_REC_LEN(ext2fs_dirent_name_len(ent->dir)); |
850d05e9 | 479 | if (rec_len > left) { |
8a480350 TT |
480 | if (left) { |
481 | left += prev_rec_len; | |
482 | retval = ext2fs_set_rec_len(fs, left, dirent); | |
483 | if (retval) | |
484 | return retval; | |
485 | } | |
81683c6a DW |
486 | if (csum_size) { |
487 | t = EXT2_DIRENT_TAIL(block_start, | |
488 | fs->blocksize); | |
489 | ext2fs_initialize_dirent_tail(fs, t); | |
490 | } | |
850d05e9 TT |
491 | if ((retval = get_next_block(fs, outdir, |
492 | &block_start))) | |
493 | return retval; | |
fe5b72d1 | 494 | offset = 0; |
850d05e9 | 495 | } |
81683c6a | 496 | left = (fs->blocksize - csum_size) - offset; |
fe5b72d1 | 497 | dirent = (struct ext2_dir_entry *) (block_start + offset); |
850d05e9 TT |
498 | if (offset == 0) { |
499 | if (ent->hash == prev_hash) | |
500 | outdir->hashes[outdir->num-1] = ent->hash | 1; | |
501 | else | |
502 | outdir->hashes[outdir->num-1] = ent->hash; | |
503 | } | |
504 | dirent->inode = ent->dir->inode; | |
70f4632b JK |
505 | ext2fs_dirent_set_name_len(dirent, |
506 | ext2fs_dirent_name_len(ent->dir)); | |
507 | ext2fs_dirent_set_file_type(dirent, | |
508 | ext2fs_dirent_file_type(ent->dir)); | |
8a480350 TT |
509 | retval = ext2fs_set_rec_len(fs, rec_len, dirent); |
510 | if (retval) | |
511 | return retval; | |
512 | prev_rec_len = rec_len; | |
70f4632b JK |
513 | memcpy(dirent->name, ent->dir->name, |
514 | ext2fs_dirent_name_len(dirent)); | |
850d05e9 TT |
515 | offset += rec_len; |
516 | left -= rec_len; | |
7dca4c88 | 517 | if (left < slack) { |
8a480350 TT |
518 | prev_rec_len += left; |
519 | retval = ext2fs_set_rec_len(fs, prev_rec_len, dirent); | |
520 | if (retval) | |
521 | return retval; | |
850d05e9 | 522 | offset += left; |
cf3909ed | 523 | left = 0; |
850d05e9 TT |
524 | } |
525 | prev_hash = ent->hash; | |
526 | } | |
527 | if (left) | |
8a480350 | 528 | retval = ext2fs_set_rec_len(fs, rec_len + left, dirent); |
81683c6a DW |
529 | if (csum_size) { |
530 | t = EXT2_DIRENT_TAIL(block_start, fs->blocksize); | |
531 | ext2fs_initialize_dirent_tail(fs, t); | |
532 | } | |
850d05e9 | 533 | |
8a480350 | 534 | return retval; |
850d05e9 TT |
535 | } |
536 | ||
537 | ||
538 | static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf, | |
b7a00563 TT |
539 | ext2_ino_t ino, ext2_ino_t parent) |
540 | { | |
541 | struct ext2_dir_entry *dir; | |
542 | struct ext2_dx_root_info *root; | |
543 | struct ext2_dx_countlimit *limits; | |
850d05e9 | 544 | int filetype = 0; |
07307114 | 545 | int csum_size = 0; |
b7a00563 | 546 | |
86f3b6cf | 547 | if (ext2fs_has_feature_filetype(fs->super)) |
70f4632b | 548 | filetype = EXT2_FT_DIR; |
efc6f628 | 549 | |
b7a00563 TT |
550 | memset(buf, 0, fs->blocksize); |
551 | dir = (struct ext2_dir_entry *) buf; | |
552 | dir->inode = ino; | |
553 | dir->name[0] = '.'; | |
70f4632b JK |
554 | ext2fs_dirent_set_name_len(dir, 1); |
555 | ext2fs_dirent_set_file_type(dir, filetype); | |
b7a00563 TT |
556 | dir->rec_len = 12; |
557 | dir = (struct ext2_dir_entry *) (buf + 12); | |
558 | dir->inode = parent; | |
559 | dir->name[0] = '.'; | |
560 | dir->name[1] = '.'; | |
70f4632b JK |
561 | ext2fs_dirent_set_name_len(dir, 2); |
562 | ext2fs_dirent_set_file_type(dir, filetype); | |
b7a00563 | 563 | dir->rec_len = fs->blocksize - 12; |
efc6f628 | 564 | |
b7a00563 TT |
565 | root = (struct ext2_dx_root_info *) (buf+24); |
566 | root->reserved_zero = 0; | |
567 | root->hash_version = fs->super->s_def_hash_version; | |
568 | root->info_length = 8; | |
569 | root->indirect_levels = 0; | |
570 | root->unused_flags = 0; | |
571 | ||
86f3b6cf | 572 | if (ext2fs_has_feature_metadata_csum(fs->super)) |
07307114 DW |
573 | csum_size = sizeof(struct ext2_dx_tail); |
574 | ||
b7a00563 | 575 | limits = (struct ext2_dx_countlimit *) (buf+32); |
07307114 DW |
576 | limits->limit = (fs->blocksize - (32 + csum_size)) / |
577 | sizeof(struct ext2_dx_entry); | |
b7a00563 TT |
578 | limits->count = 0; |
579 | ||
580 | return root; | |
581 | } | |
582 | ||
583 | ||
850d05e9 | 584 | static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf) |
b7a00563 TT |
585 | { |
586 | struct ext2_dir_entry *dir; | |
587 | struct ext2_dx_countlimit *limits; | |
07307114 | 588 | int csum_size = 0; |
b7a00563 TT |
589 | |
590 | memset(buf, 0, fs->blocksize); | |
591 | dir = (struct ext2_dir_entry *) buf; | |
592 | dir->inode = 0; | |
8a480350 | 593 | (void) ext2fs_set_rec_len(fs, fs->blocksize, dir); |
efc6f628 | 594 | |
86f3b6cf | 595 | if (ext2fs_has_feature_metadata_csum(fs->super)) |
07307114 DW |
596 | csum_size = sizeof(struct ext2_dx_tail); |
597 | ||
b7a00563 | 598 | limits = (struct ext2_dx_countlimit *) (buf+8); |
07307114 DW |
599 | limits->limit = (fs->blocksize - (8 + csum_size)) / |
600 | sizeof(struct ext2_dx_entry); | |
b7a00563 TT |
601 | limits->count = 0; |
602 | ||
603 | return (struct ext2_dx_entry *) limits; | |
604 | } | |
605 | ||
ae9efd05 AB |
606 | static int alloc_blocks(ext2_filsys fs, |
607 | struct ext2_dx_countlimit **limit, | |
608 | struct ext2_dx_entry **prev_ent, | |
609 | struct ext2_dx_entry **next_ent, | |
610 | int *prev_offset, int *next_offset, | |
611 | struct out_dir *outdir, int i, | |
612 | int *prev_count, int *next_count) | |
613 | { | |
614 | errcode_t retval; | |
615 | char *block_start; | |
616 | ||
617 | if (*limit) | |
618 | (*limit)->limit = (*limit)->count = | |
619 | ext2fs_cpu_to_le16((*limit)->limit); | |
620 | *prev_ent = (struct ext2_dx_entry *) (outdir->buf + *prev_offset); | |
621 | (*prev_ent)->block = ext2fs_cpu_to_le32(outdir->num); | |
622 | ||
623 | if (i != 1) | |
624 | (*prev_ent)->hash = | |
625 | ext2fs_cpu_to_le32(outdir->hashes[i]); | |
626 | ||
627 | retval = get_next_block(fs, outdir, &block_start); | |
628 | if (retval) | |
629 | return retval; | |
630 | ||
631 | *next_ent = set_int_node(fs, block_start); | |
632 | *limit = (struct ext2_dx_countlimit *)(*next_ent); | |
633 | if (next_offset) | |
634 | *next_offset = ((char *) *next_ent - outdir->buf); | |
635 | ||
636 | *next_count = (*limit)->limit; | |
637 | (*prev_offset) += sizeof(struct ext2_dx_entry); | |
638 | (*prev_count)--; | |
639 | ||
640 | return 0; | |
641 | } | |
642 | ||
850d05e9 TT |
643 | /* |
644 | * This function takes the leaf nodes which have been written in | |
645 | * outdir, and populates the root node and any necessary interior nodes. | |
646 | */ | |
647 | static errcode_t calculate_tree(ext2_filsys fs, | |
648 | struct out_dir *outdir, | |
649 | ext2_ino_t ino, | |
650 | ext2_ino_t parent) | |
651 | { | |
ae9efd05 AB |
652 | struct ext2_dx_root_info *root_info; |
653 | struct ext2_dx_entry *root, *int_ent, *dx_ent = 0; | |
654 | struct ext2_dx_countlimit *root_limit, *int_limit, *limit; | |
850d05e9 | 655 | errcode_t retval; |
ae9efd05 AB |
656 | int i, c1, c2, c3, nblks; |
657 | int limit_offset, int_offset, root_offset; | |
efc6f628 | 658 | |
850d05e9 TT |
659 | root_info = set_root_node(fs, outdir->buf, ino, parent); |
660 | root_offset = limit_offset = ((char *) root_info - outdir->buf) + | |
661 | root_info->info_length; | |
662 | root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset); | |
663 | c1 = root_limit->limit; | |
664 | nblks = outdir->num; | |
665 | ||
666 | /* Write out the pointer blocks */ | |
ae9efd05 | 667 | if (nblks - 1 <= c1) { |
850d05e9 TT |
668 | /* Just write out the root block, and we're done */ |
669 | root = (struct ext2_dx_entry *) (outdir->buf + root_offset); | |
670 | for (i=1; i < nblks; i++) { | |
671 | root->block = ext2fs_cpu_to_le32(i); | |
672 | if (i != 1) | |
673 | root->hash = | |
674 | ext2fs_cpu_to_le32(outdir->hashes[i]); | |
675 | root++; | |
676 | c1--; | |
677 | } | |
ae9efd05 | 678 | } else if (nblks - 1 <= ext2fs_htree_intnode_maxrecs(fs, c1)) { |
850d05e9 | 679 | c2 = 0; |
ae9efd05 | 680 | limit = NULL; |
850d05e9 TT |
681 | root_info->indirect_levels = 1; |
682 | for (i=1; i < nblks; i++) { | |
ae9efd05 | 683 | if (c2 == 0 && c1 == 0) |
850d05e9 TT |
684 | return ENOSPC; |
685 | if (c2 == 0) { | |
ae9efd05 AB |
686 | retval = alloc_blocks(fs, &limit, &root, |
687 | &dx_ent, &root_offset, | |
688 | NULL, outdir, i, &c1, | |
689 | &c2); | |
690 | if (retval) | |
850d05e9 | 691 | return retval; |
850d05e9 TT |
692 | } |
693 | dx_ent->block = ext2fs_cpu_to_le32(i); | |
694 | if (c2 != limit->limit) | |
695 | dx_ent->hash = | |
696 | ext2fs_cpu_to_le32(outdir->hashes[i]); | |
697 | dx_ent++; | |
698 | c2--; | |
699 | } | |
700 | limit->count = ext2fs_cpu_to_le16(limit->limit - c2); | |
701 | limit->limit = ext2fs_cpu_to_le16(limit->limit); | |
ae9efd05 AB |
702 | } else { |
703 | c2 = 0; | |
704 | c3 = 0; | |
705 | limit = NULL; | |
706 | int_limit = 0; | |
707 | root_info->indirect_levels = 2; | |
708 | for (i = 1; i < nblks; i++) { | |
709 | if (c3 == 0 && c2 == 0 && c1 == 0) | |
710 | return ENOSPC; | |
711 | if (c3 == 0 && c2 == 0) { | |
712 | retval = alloc_blocks(fs, &int_limit, &root, | |
713 | &int_ent, &root_offset, | |
714 | &int_offset, outdir, i, | |
715 | &c1, &c2); | |
716 | if (retval) | |
717 | return retval; | |
718 | } | |
719 | if (c3 == 0) { | |
720 | retval = alloc_blocks(fs, &limit, &int_ent, | |
721 | &dx_ent, &int_offset, | |
722 | NULL, outdir, i, &c2, | |
723 | &c3); | |
724 | if (retval) | |
725 | return retval; | |
726 | ||
727 | } | |
728 | dx_ent->block = ext2fs_cpu_to_le32(i); | |
729 | if (c3 != limit->limit) | |
730 | dx_ent->hash = | |
731 | ext2fs_cpu_to_le32(outdir->hashes[i]); | |
732 | dx_ent++; | |
733 | c3--; | |
734 | } | |
735 | int_limit->count = ext2fs_cpu_to_le16(limit->limit - c2); | |
736 | int_limit->limit = ext2fs_cpu_to_le16(limit->limit); | |
737 | ||
738 | limit->count = ext2fs_cpu_to_le16(limit->limit - c3); | |
739 | limit->limit = ext2fs_cpu_to_le16(limit->limit); | |
740 | ||
850d05e9 TT |
741 | } |
742 | root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset); | |
743 | root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1); | |
744 | root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit); | |
745 | ||
746 | return 0; | |
747 | } | |
b7a00563 TT |
748 | |
749 | struct write_dir_struct { | |
750 | struct out_dir *outdir; | |
751 | errcode_t err; | |
19961cd0 | 752 | ext2_ino_t ino; |
b7a00563 | 753 | e2fsck_t ctx; |
81683c6a | 754 | ext2_ino_t dir; |
b7a00563 TT |
755 | }; |
756 | ||
757 | /* | |
850d05e9 | 758 | * Helper function which writes out a directory block. |
b7a00563 TT |
759 | */ |
760 | static int write_dir_block(ext2_filsys fs, | |
6dc64392 | 761 | blk64_t *block_nr, |
b7a00563 | 762 | e2_blkcnt_t blockcnt, |
6dc64392 | 763 | blk64_t ref_block EXT2FS_ATTR((unused)), |
efc6f628 | 764 | int ref_offset EXT2FS_ATTR((unused)), |
b7a00563 TT |
765 | void *priv_data) |
766 | { | |
767 | struct write_dir_struct *wd = (struct write_dir_struct *) priv_data; | |
82372e32 | 768 | char *dir, *buf = 0; |
b7a00563 | 769 | |
19961cd0 AD |
770 | #ifdef REHASH_DEBUG |
771 | printf("%u: write_dir_block %lld:%lld", wd->ino, blockcnt, *block_nr); | |
772 | #endif | |
94676ef2 | 773 | if ((*block_nr == 0) || (blockcnt < 0)) { |
19961cd0 AD |
774 | #ifdef REHASH_DEBUG |
775 | printf(" - skip\n"); | |
776 | #endif | |
82372e32 | 777 | return 0; |
19961cd0 | 778 | } |
82372e32 TT |
779 | if (blockcnt < wd->outdir->num) |
780 | dir = wd->outdir->buf + (blockcnt * fs->blocksize); | |
781 | else if (wd->ctx->lost_and_found == wd->dir) { | |
782 | /* Don't release any extra directory blocks for lost+found */ | |
783 | wd->err = ext2fs_new_dir_block(fs, 0, 0, &buf); | |
784 | if (wd->err) | |
785 | return BLOCK_ABORT; | |
786 | dir = buf; | |
787 | wd->outdir->num++; | |
788 | } else { | |
94676ef2 TT |
789 | /* Don't free blocks at the end of the directory, they |
790 | * will be truncated by the caller. */ | |
19961cd0 AD |
791 | #ifdef REHASH_DEBUG |
792 | printf(" - not freed\n"); | |
793 | #endif | |
794 | return 0; | |
b7a00563 | 795 | } |
81683c6a | 796 | wd->err = ext2fs_write_dir_block4(fs, *block_nr, dir, 0, wd->dir); |
82372e32 TT |
797 | if (buf) |
798 | ext2fs_free_mem(&buf); | |
799 | ||
19961cd0 AD |
800 | #ifdef REHASH_DEBUG |
801 | printf(" - write (%d)\n", wd->err); | |
802 | #endif | |
b7a00563 TT |
803 | if (wd->err) |
804 | return BLOCK_ABORT; | |
805 | return 0; | |
806 | } | |
807 | ||
b7a00563 | 808 | static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs, |
850d05e9 | 809 | struct out_dir *outdir, |
e228d700 DW |
810 | ext2_ino_t ino, struct ext2_inode *inode, |
811 | int compress) | |
b7a00563 TT |
812 | { |
813 | struct write_dir_struct wd; | |
814 | errcode_t retval; | |
b7a00563 TT |
815 | |
816 | retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num); | |
817 | if (retval) | |
818 | return retval; | |
819 | ||
820 | wd.outdir = outdir; | |
821 | wd.err = 0; | |
19961cd0 | 822 | wd.ino = ino; |
b7a00563 | 823 | wd.ctx = ctx; |
81683c6a | 824 | wd.dir = ino; |
b7a00563 | 825 | |
19961cd0 | 826 | retval = ext2fs_block_iterate3(fs, ino, 0, NULL, |
b7a00563 TT |
827 | write_dir_block, &wd); |
828 | if (retval) | |
829 | return retval; | |
830 | if (wd.err) | |
831 | return wd.err; | |
832 | ||
e228d700 | 833 | e2fsck_read_inode(ctx, ino, inode, "rehash_dir"); |
e70ae99e | 834 | if (compress) |
e228d700 | 835 | inode->i_flags &= ~EXT2_INDEX_FL; |
e70ae99e | 836 | else |
e228d700 | 837 | inode->i_flags |= EXT2_INDEX_FL; |
19961cd0 AD |
838 | #ifdef REHASH_DEBUG |
839 | printf("%u: set inode size to %u blocks = %u bytes\n", | |
840 | ino, outdir->num, outdir->num * fs->blocksize); | |
841 | #endif | |
94676ef2 | 842 | retval = ext2fs_inode_size_set(fs, inode, (ext2_off64_t)outdir->num * |
19961cd0 | 843 | fs->blocksize); |
97c607b1 DW |
844 | if (retval) |
845 | return retval; | |
b7a00563 | 846 | |
19961cd0 | 847 | /* ext2fs_punch() calls ext2fs_write_inode() which writes the size */ |
94676ef2 | 848 | return ext2fs_punch(fs, ino, inode, NULL, outdir->num, ~0ULL); |
b7a00563 TT |
849 | } |
850 | ||
e228d700 DW |
851 | errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino, |
852 | struct problem_context *pctx) | |
b7a00563 TT |
853 | { |
854 | ext2_filsys fs = ctx->fs; | |
855 | errcode_t retval; | |
856 | struct ext2_inode inode; | |
850d05e9 | 857 | char *dir_buf = 0; |
478360f5 TT |
858 | struct fill_dir_struct fd = { NULL, NULL, 0, 0, 0, NULL, |
859 | 0, 0, 0, 0, 0, 0 }; | |
860 | struct out_dir outdir = { 0, 0, 0, 0 }; | |
efc6f628 | 861 | |
b7a00563 | 862 | e2fsck_read_inode(ctx, ino, &inode, "rehash_dir"); |
b7a00563 | 863 | |
86f3b6cf | 864 | if (ext2fs_has_feature_inline_data(fs->super) && |
81ac00d0 DW |
865 | (inode.i_flags & EXT4_INLINE_DATA_FL)) |
866 | return 0; | |
867 | ||
b7a00563 | 868 | retval = ENOMEM; |
b7a00563 TT |
869 | dir_buf = malloc(inode.i_size); |
870 | if (!dir_buf) | |
871 | goto errout; | |
872 | ||
873 | fd.max_array = inode.i_size / 32; | |
b7a00563 TT |
874 | fd.harray = malloc(fd.max_array * sizeof(struct hash_entry)); |
875 | if (!fd.harray) | |
876 | goto errout; | |
877 | ||
19961cd0 | 878 | fd.ino = ino; |
b7a00563 TT |
879 | fd.ctx = ctx; |
880 | fd.buf = dir_buf; | |
881 | fd.inode = &inode; | |
81683c6a | 882 | fd.dir = ino; |
86f3b6cf | 883 | if (!ext2fs_has_feature_dir_index(fs->super) || |
e70ae99e | 884 | (inode.i_size / fs->blocksize) < 2) |
850d05e9 | 885 | fd.compress = 1; |
b7a00563 TT |
886 | fd.parent = 0; |
887 | ||
f4e14505 | 888 | retry_nohash: |
b7a00563 | 889 | /* Read in the entire directory into memory */ |
6dc64392 | 890 | retval = ext2fs_block_iterate3(fs, ino, 0, 0, |
b7a00563 TT |
891 | fill_dir_block, &fd); |
892 | if (fd.err) { | |
893 | retval = fd.err; | |
894 | goto errout; | |
895 | } | |
896 | ||
f4e14505 TT |
897 | /* |
898 | * If the entries read are less than a block, then don't index | |
899 | * the directory | |
900 | */ | |
901 | if (!fd.compress && (fd.dir_size < (fs->blocksize - 24))) { | |
902 | fd.compress = 1; | |
903 | fd.dir_size = 0; | |
904 | fd.num_array = 0; | |
905 | goto retry_nohash; | |
906 | } | |
907 | ||
b7a00563 TT |
908 | #if 0 |
909 | printf("%d entries (%d bytes) found in inode %d\n", | |
910 | fd.num_array, fd.dir_size, ino); | |
911 | #endif | |
912 | ||
850d05e9 | 913 | /* Sort the list */ |
b0700a1b | 914 | resort: |
c23b2cc4 | 915 | if (fd.compress && fd.num_array > 1) |
53fbfb2b TT |
916 | qsort(fd.harray+2, fd.num_array-2, sizeof(struct hash_entry), |
917 | hash_cmp); | |
918 | else | |
919 | qsort(fd.harray, fd.num_array, sizeof(struct hash_entry), | |
920 | hash_cmp); | |
850d05e9 | 921 | |
b0700a1b TT |
922 | /* |
923 | * Look for duplicates | |
924 | */ | |
925 | if (duplicate_search_and_fix(ctx, fs, ino, &fd)) | |
926 | goto resort; | |
927 | ||
1d2eef42 TT |
928 | if (ctx->options & E2F_OPT_NO) { |
929 | retval = 0; | |
930 | goto errout; | |
931 | } | |
932 | ||
b71e0183 | 933 | /* Sort non-hashed directories by inode number */ |
c23b2cc4 | 934 | if (fd.compress && fd.num_array > 1) |
b71e0183 TT |
935 | qsort(fd.harray+2, fd.num_array-2, |
936 | sizeof(struct hash_entry), ino_cmp); | |
937 | ||
850d05e9 TT |
938 | /* |
939 | * Copy the directory entries. In a htree directory these | |
940 | * will become the leaf nodes. | |
941 | */ | |
fd9ca825 | 942 | retval = copy_dir_entries(ctx, &fd, &outdir); |
b7a00563 TT |
943 | if (retval) |
944 | goto errout; | |
efc6f628 | 945 | |
b7a00563 TT |
946 | free(dir_buf); dir_buf = 0; |
947 | ||
850d05e9 TT |
948 | if (!fd.compress) { |
949 | /* Calculate the interior nodes */ | |
950 | retval = calculate_tree(fs, &outdir, ino, fd.parent); | |
951 | if (retval) | |
952 | goto errout; | |
b7a00563 | 953 | } |
efc6f628 | 954 | |
e228d700 | 955 | retval = write_directory(ctx, fs, &outdir, ino, &inode, fd.compress); |
b7a00563 TT |
956 | if (retval) |
957 | goto errout; | |
958 | ||
e228d700 DW |
959 | if (ctx->options & E2F_OPT_CONVERT_BMAP) |
960 | retval = e2fsck_rebuild_extents_later(ctx, ino); | |
961 | else | |
962 | retval = e2fsck_check_rebuild_extents(ctx, ino, &inode, pctx); | |
b7a00563 | 963 | errout: |
45e338f5 JM |
964 | free(dir_buf); |
965 | free(fd.harray); | |
850d05e9 | 966 | |
b7a00563 TT |
967 | free_out_dir(&outdir); |
968 | return retval; | |
969 | } | |
970 | ||
971 | void e2fsck_rehash_directories(e2fsck_t ctx) | |
972 | { | |
b7a00563 | 973 | struct problem_context pctx; |
850d05e9 TT |
974 | #ifdef RESOURCE_TRACK |
975 | struct resource_track rtrack; | |
976 | #endif | |
977 | struct dir_info *dir; | |
978 | ext2_u32_iterate iter; | |
28db82a8 | 979 | struct dir_info_iter * dirinfo_iter = 0; |
850d05e9 TT |
980 | ext2_ino_t ino; |
981 | errcode_t retval; | |
e3507739 | 982 | int cur, max, all_dirs, first = 1; |
850d05e9 | 983 | |
6d96b00d | 984 | init_resource_track(&rtrack, ctx->fs->io); |
850d05e9 TT |
985 | all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS; |
986 | ||
987 | if (!ctx->dirs_to_hash && !all_dirs) | |
b7a00563 TT |
988 | return; |
989 | ||
82372e32 TT |
990 | (void) e2fsck_get_lost_and_found(ctx, 0); |
991 | ||
b7a00563 | 992 | clear_problem_context(&pctx); |
850d05e9 | 993 | |
b0700a1b TT |
994 | cur = 0; |
995 | if (all_dirs) { | |
28db82a8 | 996 | dirinfo_iter = e2fsck_dir_info_iter_begin(ctx); |
b0700a1b TT |
997 | max = e2fsck_get_num_dirinfo(ctx); |
998 | } else { | |
efc6f628 | 999 | retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash, |
850d05e9 TT |
1000 | &iter); |
1001 | if (retval) { | |
1002 | pctx.errcode = retval; | |
1003 | fix_problem(ctx, PR_3A_OPTIMIZE_ITER, &pctx); | |
1004 | return; | |
1005 | } | |
b0700a1b | 1006 | max = ext2fs_u32_list_count(ctx->dirs_to_hash); |
b7a00563 | 1007 | } |
850d05e9 TT |
1008 | while (1) { |
1009 | if (all_dirs) { | |
efc6f628 | 1010 | if ((dir = e2fsck_dir_info_iter(ctx, |
28db82a8 | 1011 | dirinfo_iter)) == 0) |
850d05e9 TT |
1012 | break; |
1013 | ino = dir->ino; | |
1014 | } else { | |
1015 | if (!ext2fs_u32_list_iterate(iter, &ino)) | |
1016 | break; | |
1017 | } | |
07307114 | 1018 | |
b7a00563 TT |
1019 | pctx.dir = ino; |
1020 | if (first) { | |
1021 | fix_problem(ctx, PR_3A_PASS_HEADER, &pctx); | |
1022 | first = 0; | |
1023 | } | |
b0700a1b | 1024 | #if 0 |
850d05e9 | 1025 | fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx); |
b0700a1b | 1026 | #endif |
e228d700 | 1027 | pctx.errcode = e2fsck_rehash_dir(ctx, ino, &pctx); |
b7a00563 | 1028 | if (pctx.errcode) { |
850d05e9 TT |
1029 | end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR); |
1030 | fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx); | |
b7a00563 | 1031 | } |
52734dc5 TT |
1032 | if (ctx->progress && !ctx->progress_fd) |
1033 | e2fsck_simple_progress(ctx, "Rebuilding directory", | |
1d2eef42 | 1034 | 100.0 * (float) (++cur) / (float) max, ino); |
b7a00563 | 1035 | } |
850d05e9 | 1036 | end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR); |
28db82a8 TT |
1037 | if (all_dirs) |
1038 | e2fsck_dir_info_iter_end(ctx, dirinfo_iter); | |
1039 | else | |
850d05e9 | 1040 | ext2fs_u32_list_iterate_end(iter); |
efc6f628 | 1041 | |
850d05e9 TT |
1042 | if (ctx->dirs_to_hash) |
1043 | ext2fs_u32_list_free(ctx->dirs_to_hash); | |
b7a00563 | 1044 | ctx->dirs_to_hash = 0; |
850d05e9 | 1045 | |
9facd076 | 1046 | print_resource_track(ctx, "Pass 3A", &rtrack, ctx->fs->io); |
b7a00563 | 1047 | } |