]>
Commit | Line | Data |
---|---|---|
b7a00563 TT |
1 | /* |
2 | * rehash.c --- rebuild hash tree directories | |
3 | * | |
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% | |
10 | * | |
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 | |
39 | * | |
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 | ||
48 | #include <errno.h> | |
49 | #include "e2fsck.h" | |
50 | #include "problem.h" | |
51 | ||
52 | struct fill_dir_struct { | |
53 | char *buf; | |
54 | struct ext2_inode *inode; | |
55 | int err; | |
56 | e2fsck_t ctx; | |
57 | struct hash_entry *harray; | |
58 | int max_array, num_array; | |
59 | int dir_size; | |
850d05e9 | 60 | int compress; |
b7a00563 TT |
61 | ino_t parent; |
62 | }; | |
63 | ||
64 | struct hash_entry { | |
65 | ext2_dirhash_t hash; | |
66 | ext2_dirhash_t minor_hash; | |
67 | struct ext2_dir_entry *dir; | |
68 | }; | |
69 | ||
70 | struct out_dir { | |
71 | int num; | |
72 | int max; | |
73 | char *buf; | |
74 | ext2_dirhash_t *hashes; | |
75 | }; | |
76 | ||
77 | static int fill_dir_block(ext2_filsys fs, | |
78 | blk_t *block_nr, | |
79 | e2_blkcnt_t blockcnt, | |
80 | blk_t ref_block, | |
81 | int ref_offset, | |
82 | void *priv_data) | |
83 | { | |
84 | struct fill_dir_struct *fd = (struct fill_dir_struct *) priv_data; | |
85 | struct hash_entry *new_array, *ent; | |
86 | struct ext2_dir_entry *dirent; | |
87 | char *dir; | |
88 | int offset, dir_offset; | |
89 | ||
90 | if (blockcnt < 0) | |
91 | return 0; | |
92 | ||
93 | offset = blockcnt * fs->blocksize; | |
94 | if (offset + fs->blocksize > fd->inode->i_size) { | |
95 | fd->err = EXT2_ET_DIR_CORRUPTED; | |
96 | return BLOCK_ABORT; | |
97 | } | |
98 | dir = (fd->buf+offset); | |
99 | if (HOLE_BLKADDR(*block_nr)) { | |
100 | memset(dir, 0, fs->blocksize); | |
101 | dirent = (struct ext2_dir_entry *) dir; | |
102 | dirent->rec_len = fs->blocksize; | |
103 | } else { | |
104 | fd->err = ext2fs_read_dir_block(fs, *block_nr, dir); | |
105 | if (fd->err) | |
106 | return BLOCK_ABORT; | |
107 | } | |
108 | /* While the directory block is "hot", index it. */ | |
109 | dir_offset = 0; | |
110 | while (dir_offset < fs->blocksize) { | |
111 | dirent = (struct ext2_dir_entry *) (dir + dir_offset); | |
112 | if (((dir_offset + dirent->rec_len) > fs->blocksize) || | |
113 | (dirent->rec_len < 8) || | |
114 | ((dirent->rec_len % 4) != 0) || | |
115 | (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) { | |
116 | fd->err = EXT2_ET_DIR_CORRUPTED; | |
117 | return BLOCK_ABORT; | |
118 | } | |
850d05e9 | 119 | dir_offset += dirent->rec_len; |
b7a00563 | 120 | if (dirent->inode == 0) |
850d05e9 TT |
121 | continue; |
122 | if (!fd->compress && ((dirent->name_len&0xFF) == 1) && | |
123 | (dirent->name[0] == '.')) | |
124 | continue; | |
125 | if (!fd->compress && ((dirent->name_len&0xFF) == 2) && | |
b7a00563 TT |
126 | (dirent->name[0] == '.') && (dirent->name[1] == '.')) { |
127 | fd->parent = dirent->inode; | |
850d05e9 | 128 | continue; |
b7a00563 TT |
129 | } |
130 | if (fd->num_array >= fd->max_array) { | |
131 | new_array = realloc(fd->harray, | |
132 | sizeof(struct hash_entry) * (fd->max_array+500)); | |
133 | if (!new_array) { | |
134 | fd->err = ENOMEM; | |
135 | return BLOCK_ABORT; | |
136 | } | |
137 | fd->harray = new_array; | |
138 | fd->max_array += 500; | |
139 | } | |
850d05e9 | 140 | ent = fd->harray + fd->num_array++; |
b7a00563 | 141 | ent->dir = dirent; |
b7a00563 | 142 | fd->dir_size += EXT2_DIR_REC_LEN(dirent->name_len & 0xFF); |
850d05e9 TT |
143 | if (fd->compress) |
144 | ent->hash = ent->minor_hash = 0; | |
145 | else { | |
146 | fd->err = ext2fs_dirhash(fs->super->s_def_hash_version, | |
147 | dirent->name, | |
148 | dirent->name_len & 0xFF, | |
149 | fs->super->s_hash_seed, | |
150 | &ent->hash, &ent->minor_hash); | |
151 | if (fd->err) | |
152 | return BLOCK_ABORT; | |
153 | } | |
b7a00563 TT |
154 | } |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
159 | /* Used for sorting the hash entry */ | |
b0700a1b | 160 | static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b) |
b7a00563 TT |
161 | { |
162 | const struct hash_entry *he_a = (const struct hash_entry *) a; | |
163 | const struct hash_entry *he_b = (const struct hash_entry *) b; | |
164 | int ret; | |
b0700a1b TT |
165 | int min_len; |
166 | ||
167 | min_len = he_a->dir->name_len; | |
168 | if (min_len > he_b->dir->name_len) | |
169 | min_len = he_b->dir->name_len; | |
170 | ||
171 | ret = strncmp(he_a->dir->name, he_b->dir->name, min_len); | |
172 | if (ret == 0) { | |
173 | if (he_a->dir->name_len > he_b->dir->name_len) | |
b7a00563 | 174 | ret = 1; |
b0700a1b | 175 | else if (he_a->dir->name_len < he_b->dir->name_len) |
b7a00563 TT |
176 | ret = -1; |
177 | else | |
12dd69f5 | 178 | ret = he_b->dir->inode - he_a->dir->inode; |
b7a00563 TT |
179 | } |
180 | return ret; | |
181 | } | |
182 | ||
850d05e9 | 183 | /* Used for sorting the hash entry */ |
b0700a1b | 184 | static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b) |
850d05e9 TT |
185 | { |
186 | const struct hash_entry *he_a = (const struct hash_entry *) a; | |
187 | const struct hash_entry *he_b = (const struct hash_entry *) b; | |
188 | int ret; | |
b0700a1b TT |
189 | |
190 | if (he_a->hash > he_b->hash) | |
191 | ret = 1; | |
192 | else if (he_a->hash < he_b->hash) | |
193 | ret = -1; | |
194 | else { | |
195 | if (he_a->minor_hash > he_b->minor_hash) | |
850d05e9 | 196 | ret = 1; |
b0700a1b | 197 | else if (he_a->minor_hash < he_b->minor_hash) |
850d05e9 TT |
198 | ret = -1; |
199 | else | |
b0700a1b | 200 | ret = name_cmp(a, b); |
850d05e9 TT |
201 | } |
202 | return ret; | |
203 | } | |
204 | ||
b7a00563 TT |
205 | static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir, |
206 | int blocks) | |
207 | { | |
208 | void *new_mem; | |
209 | ||
210 | if (outdir->max) { | |
211 | new_mem = realloc(outdir->buf, blocks * fs->blocksize); | |
212 | if (!new_mem) | |
213 | return ENOMEM; | |
214 | outdir->buf = new_mem; | |
215 | new_mem = realloc(outdir->hashes, | |
216 | blocks * sizeof(ext2_dirhash_t)); | |
217 | if (!new_mem) | |
218 | return ENOMEM; | |
219 | outdir->hashes = new_mem; | |
220 | } else { | |
221 | outdir->buf = malloc(blocks * fs->blocksize); | |
222 | outdir->hashes = malloc(blocks * sizeof(ext2_dirhash_t)); | |
223 | outdir->num = 0; | |
224 | } | |
225 | outdir->max = blocks; | |
226 | return 0; | |
227 | } | |
228 | ||
229 | static void free_out_dir(struct out_dir *outdir) | |
230 | { | |
1d2eef42 TT |
231 | if (outdir->buf) |
232 | free(outdir->buf); | |
233 | if (outdir->hashes) | |
234 | free(outdir->hashes); | |
b7a00563 TT |
235 | outdir->max = 0; |
236 | outdir->num =0; | |
237 | } | |
238 | ||
850d05e9 | 239 | static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir, |
b7a00563 TT |
240 | char ** ret) |
241 | { | |
242 | errcode_t retval; | |
243 | ||
244 | if (outdir->num >= outdir->max) { | |
245 | retval = alloc_size_dir(fs, outdir, outdir->max + 50); | |
246 | if (retval) | |
247 | return retval; | |
248 | } | |
249 | *ret = outdir->buf + (outdir->num++ * fs->blocksize); | |
850d05e9 | 250 | memset(*ret, 0, fs->blocksize); |
b7a00563 TT |
251 | return 0; |
252 | } | |
253 | ||
b0700a1b TT |
254 | /* |
255 | * This function is used to make a unique filename. We do this by | |
256 | * appending ~0, and then incrementing the number. However, we cannot | |
257 | * expand the length of the filename beyond the padding available in | |
258 | * the directory entry. | |
259 | */ | |
260 | static void mutate_name(char *str, __u16 *len) | |
261 | { | |
262 | int i; | |
263 | __u16 l = *len & 0xFF, h = *len & 0xff00; | |
264 | ||
265 | /* | |
266 | * First check to see if it looks the name has been mutated | |
267 | * already | |
268 | */ | |
269 | for (i = l-1; i > 0; i--) { | |
270 | if (!isdigit(str[i])) | |
271 | break; | |
272 | } | |
273 | if ((i == l-1) || (str[i] != '~')) { | |
274 | if (((l-1) & 3) < 2) | |
275 | l += 2; | |
276 | else | |
277 | l = (l+3) & ~3; | |
278 | str[l-2] = '~'; | |
279 | str[l-1] = '0'; | |
280 | *len = l | h; | |
281 | return; | |
282 | } | |
283 | for (i = l-1; i >= 0; i--) { | |
284 | if (isdigit(str[i])) { | |
285 | if (str[i] == '9') | |
286 | str[i] = '0'; | |
287 | else { | |
288 | str[i]++; | |
289 | return; | |
290 | } | |
291 | continue; | |
292 | } | |
293 | if (i == 1) { | |
294 | if (str[0] == 'z') | |
295 | str[0] = 'A'; | |
296 | else if (str[0] == 'Z') { | |
297 | str[0] = '~'; | |
298 | str[1] = '0'; | |
299 | } else | |
300 | str[0]++; | |
301 | } else if (i > 0) { | |
302 | str[i] = '1'; | |
303 | str[i-1] = '~'; | |
304 | } else { | |
305 | if (str[0] == '~') | |
306 | str[0] = 'a'; | |
307 | else | |
308 | str[0]++; | |
309 | } | |
310 | break; | |
311 | } | |
312 | } | |
313 | ||
314 | static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs, | |
315 | ext2_ino_t ino, | |
316 | struct fill_dir_struct *fd) | |
317 | { | |
318 | struct problem_context pctx; | |
319 | struct hash_entry *ent, *prev; | |
320 | int i, j; | |
321 | int fixed = 0; | |
322 | char new_name[256]; | |
323 | __u16 new_len; | |
324 | ||
325 | clear_problem_context(&pctx); | |
326 | pctx.ino = ino; | |
327 | ||
328 | for (i=1; i < fd->num_array; i++) { | |
329 | ent = fd->harray + i; | |
330 | prev = ent - 1; | |
331 | if (!ent->dir->inode || | |
332 | ((ent->dir->name_len & 0xFF) != | |
333 | (prev->dir->name_len & 0xFF)) || | |
334 | (strncmp(ent->dir->name, prev->dir->name, | |
335 | ent->dir->name_len & 0xFF))) | |
336 | continue; | |
337 | pctx.dirent = ent->dir; | |
338 | if ((ent->dir->inode == prev->dir->inode) && | |
339 | fix_problem(ctx, PR_2_DUPLICATE_DIRENT, &pctx)) { | |
340 | e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1); | |
341 | ent->dir->inode = 0; | |
342 | fixed++; | |
343 | continue; | |
344 | } | |
345 | memcpy(new_name, ent->dir->name, ent->dir->name_len & 0xFF); | |
346 | new_len = ent->dir->name_len; | |
347 | mutate_name(new_name, &new_len); | |
348 | for (j=0; j < fd->num_array; j++) { | |
349 | if ((i==j) || | |
350 | ((ent->dir->name_len & 0xFF) != | |
351 | (fd->harray[j].dir->name_len & 0xFF)) || | |
352 | (strncmp(new_name, fd->harray[j].dir->name, | |
353 | new_len & 0xFF))) | |
354 | continue; | |
355 | mutate_name(new_name, &new_len); | |
356 | ||
357 | j = -1; | |
358 | } | |
359 | new_name[new_len & 0xFF] = 0; | |
360 | pctx.str = new_name; | |
361 | if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) { | |
362 | memcpy(ent->dir->name, new_name, new_len & 0xFF); | |
363 | ent->dir->name_len = new_len; | |
364 | ext2fs_dirhash(fs->super->s_def_hash_version, | |
365 | ent->dir->name, | |
366 | ent->dir->name_len & 0xFF, | |
367 | fs->super->s_hash_seed, | |
368 | &ent->hash, &ent->minor_hash); | |
369 | fixed++; | |
370 | } | |
371 | } | |
372 | return fixed; | |
373 | } | |
374 | ||
b7a00563 | 375 | |
850d05e9 TT |
376 | static errcode_t copy_dir_entries(ext2_filsys fs, |
377 | struct fill_dir_struct *fd, | |
378 | struct out_dir *outdir) | |
379 | { | |
380 | errcode_t retval; | |
381 | char *block_start; | |
382 | struct hash_entry *ent; | |
383 | struct ext2_dir_entry *dirent; | |
384 | int i, rec_len, left; | |
385 | ext2_dirhash_t prev_hash; | |
386 | int offset; | |
387 | ||
388 | outdir->max = 0; | |
389 | retval = alloc_size_dir(fs, outdir, | |
390 | (fd->dir_size / fs->blocksize) + 2); | |
391 | if (retval) | |
392 | return retval; | |
393 | outdir->num = fd->compress ? 0 : 1; | |
394 | offset = 0; | |
395 | outdir->hashes[0] = 0; | |
396 | prev_hash = 1; | |
397 | if ((retval = get_next_block(fs, outdir, &block_start))) | |
398 | return retval; | |
399 | dirent = (struct ext2_dir_entry *) block_start; | |
400 | left = fs->blocksize; | |
401 | for (i=0; i < fd->num_array; i++) { | |
402 | ent = fd->harray + i; | |
b0700a1b TT |
403 | if (ent->dir->inode == 0) |
404 | continue; | |
850d05e9 | 405 | rec_len = EXT2_DIR_REC_LEN(ent->dir->name_len & 0xFF); |
850d05e9 | 406 | if (rec_len > left) { |
fe5b72d1 TT |
407 | if (left) |
408 | dirent->rec_len += left; | |
850d05e9 TT |
409 | if ((retval = get_next_block(fs, outdir, |
410 | &block_start))) | |
411 | return retval; | |
fe5b72d1 | 412 | offset = 0; |
850d05e9 | 413 | } |
fe5b72d1 TT |
414 | left = fs->blocksize - offset; |
415 | dirent = (struct ext2_dir_entry *) (block_start + offset); | |
850d05e9 TT |
416 | if (offset == 0) { |
417 | if (ent->hash == prev_hash) | |
418 | outdir->hashes[outdir->num-1] = ent->hash | 1; | |
419 | else | |
420 | outdir->hashes[outdir->num-1] = ent->hash; | |
421 | } | |
422 | dirent->inode = ent->dir->inode; | |
423 | dirent->name_len = ent->dir->name_len; | |
424 | dirent->rec_len = rec_len; | |
425 | memcpy(dirent->name, ent->dir->name, dirent->name_len & 0xFF); | |
426 | offset += rec_len; | |
427 | left -= rec_len; | |
428 | if (left < 12) { | |
429 | dirent->rec_len += left; | |
430 | offset += left; | |
cf3909ed | 431 | left = 0; |
850d05e9 TT |
432 | } |
433 | prev_hash = ent->hash; | |
434 | } | |
435 | if (left) | |
436 | dirent->rec_len += left; | |
437 | ||
438 | return 0; | |
439 | } | |
440 | ||
441 | ||
442 | static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf, | |
b7a00563 TT |
443 | ext2_ino_t ino, ext2_ino_t parent) |
444 | { | |
445 | struct ext2_dir_entry *dir; | |
446 | struct ext2_dx_root_info *root; | |
447 | struct ext2_dx_countlimit *limits; | |
850d05e9 | 448 | int filetype = 0; |
b7a00563 TT |
449 | |
450 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE) | |
451 | filetype = EXT2_FT_DIR << 8; | |
452 | ||
453 | memset(buf, 0, fs->blocksize); | |
454 | dir = (struct ext2_dir_entry *) buf; | |
455 | dir->inode = ino; | |
456 | dir->name[0] = '.'; | |
457 | dir->name_len = 1 | filetype; | |
458 | dir->rec_len = 12; | |
459 | dir = (struct ext2_dir_entry *) (buf + 12); | |
460 | dir->inode = parent; | |
461 | dir->name[0] = '.'; | |
462 | dir->name[1] = '.'; | |
463 | dir->name_len = 2 | filetype; | |
464 | dir->rec_len = fs->blocksize - 12; | |
465 | ||
466 | root = (struct ext2_dx_root_info *) (buf+24); | |
467 | root->reserved_zero = 0; | |
468 | root->hash_version = fs->super->s_def_hash_version; | |
469 | root->info_length = 8; | |
470 | root->indirect_levels = 0; | |
471 | root->unused_flags = 0; | |
472 | ||
473 | limits = (struct ext2_dx_countlimit *) (buf+32); | |
474 | limits->limit = (fs->blocksize - 32) / sizeof(struct ext2_dx_entry); | |
475 | limits->count = 0; | |
476 | ||
477 | return root; | |
478 | } | |
479 | ||
480 | ||
850d05e9 | 481 | static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf) |
b7a00563 TT |
482 | { |
483 | struct ext2_dir_entry *dir; | |
484 | struct ext2_dx_countlimit *limits; | |
485 | ||
486 | memset(buf, 0, fs->blocksize); | |
487 | dir = (struct ext2_dir_entry *) buf; | |
488 | dir->inode = 0; | |
489 | dir->rec_len = fs->blocksize; | |
490 | ||
491 | limits = (struct ext2_dx_countlimit *) (buf+8); | |
492 | limits->limit = (fs->blocksize - 8) / sizeof(struct ext2_dx_entry); | |
493 | limits->count = 0; | |
494 | ||
495 | return (struct ext2_dx_entry *) limits; | |
496 | } | |
497 | ||
850d05e9 TT |
498 | /* |
499 | * This function takes the leaf nodes which have been written in | |
500 | * outdir, and populates the root node and any necessary interior nodes. | |
501 | */ | |
502 | static errcode_t calculate_tree(ext2_filsys fs, | |
503 | struct out_dir *outdir, | |
504 | ext2_ino_t ino, | |
505 | ext2_ino_t parent) | |
506 | { | |
507 | struct ext2_dx_root_info *root_info; | |
508 | struct ext2_dx_entry *root, *dx_ent = 0; | |
509 | struct ext2_dx_countlimit *root_limit, *limit; | |
510 | errcode_t retval; | |
511 | char * block_start; | |
512 | int i, c1, c2, nblks; | |
513 | int limit_offset, root_offset; | |
514 | ||
515 | root_info = set_root_node(fs, outdir->buf, ino, parent); | |
516 | root_offset = limit_offset = ((char *) root_info - outdir->buf) + | |
517 | root_info->info_length; | |
518 | root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset); | |
519 | c1 = root_limit->limit; | |
520 | nblks = outdir->num; | |
521 | ||
522 | /* Write out the pointer blocks */ | |
523 | if (nblks-1 <= c1) { | |
524 | /* Just write out the root block, and we're done */ | |
525 | root = (struct ext2_dx_entry *) (outdir->buf + root_offset); | |
526 | for (i=1; i < nblks; i++) { | |
527 | root->block = ext2fs_cpu_to_le32(i); | |
528 | if (i != 1) | |
529 | root->hash = | |
530 | ext2fs_cpu_to_le32(outdir->hashes[i]); | |
531 | root++; | |
532 | c1--; | |
533 | } | |
534 | } else { | |
535 | c2 = 0; | |
536 | limit = 0; | |
537 | root_info->indirect_levels = 1; | |
538 | for (i=1; i < nblks; i++) { | |
539 | if (c1 == 0) | |
540 | return ENOSPC; | |
541 | if (c2 == 0) { | |
542 | if (limit) | |
543 | limit->limit = limit->count = | |
544 | ext2fs_cpu_to_le16(limit->limit); | |
545 | root = (struct ext2_dx_entry *) | |
546 | (outdir->buf + root_offset); | |
547 | root->block = ext2fs_cpu_to_le32(outdir->num); | |
548 | if (i != 1) | |
549 | root->hash = | |
550 | ext2fs_cpu_to_le32(outdir->hashes[i]); | |
551 | if ((retval = get_next_block(fs, outdir, | |
552 | &block_start))) | |
553 | return retval; | |
554 | dx_ent = set_int_node(fs, block_start); | |
555 | limit = (struct ext2_dx_countlimit *) dx_ent; | |
556 | c2 = limit->limit; | |
557 | root_offset += sizeof(struct ext2_dx_entry); | |
558 | c1--; | |
559 | } | |
560 | dx_ent->block = ext2fs_cpu_to_le32(i); | |
561 | if (c2 != limit->limit) | |
562 | dx_ent->hash = | |
563 | ext2fs_cpu_to_le32(outdir->hashes[i]); | |
564 | dx_ent++; | |
565 | c2--; | |
566 | } | |
567 | limit->count = ext2fs_cpu_to_le16(limit->limit - c2); | |
568 | limit->limit = ext2fs_cpu_to_le16(limit->limit); | |
569 | } | |
570 | root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset); | |
571 | root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1); | |
572 | root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit); | |
573 | ||
574 | return 0; | |
575 | } | |
b7a00563 TT |
576 | |
577 | struct write_dir_struct { | |
578 | struct out_dir *outdir; | |
579 | errcode_t err; | |
580 | e2fsck_t ctx; | |
581 | int cleared; | |
582 | }; | |
583 | ||
584 | /* | |
850d05e9 | 585 | * Helper function which writes out a directory block. |
b7a00563 TT |
586 | */ |
587 | static int write_dir_block(ext2_filsys fs, | |
588 | blk_t *block_nr, | |
589 | e2_blkcnt_t blockcnt, | |
590 | blk_t ref_block, | |
591 | int ref_offset, | |
592 | void *priv_data) | |
593 | { | |
594 | struct write_dir_struct *wd = (struct write_dir_struct *) priv_data; | |
850d05e9 | 595 | blk_t blk; |
b7a00563 | 596 | char *dir; |
b7a00563 TT |
597 | |
598 | if (*block_nr == 0) | |
599 | return 0; | |
600 | if (blockcnt >= wd->outdir->num) { | |
601 | e2fsck_read_bitmaps(wd->ctx); | |
602 | blk = *block_nr; | |
603 | ext2fs_unmark_block_bitmap(wd->ctx->block_found_map, blk); | |
604 | ext2fs_block_alloc_stats(fs, blk, -1); | |
605 | *block_nr = 0; | |
606 | wd->cleared++; | |
607 | return BLOCK_CHANGED; | |
608 | } | |
609 | if (blockcnt < 0) | |
610 | return 0; | |
611 | ||
612 | dir = wd->outdir->buf + (blockcnt * fs->blocksize); | |
613 | wd->err = ext2fs_write_dir_block(fs, *block_nr, dir); | |
614 | if (wd->err) | |
615 | return BLOCK_ABORT; | |
616 | return 0; | |
617 | } | |
618 | ||
b7a00563 | 619 | static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs, |
850d05e9 TT |
620 | struct out_dir *outdir, |
621 | ext2_ino_t ino, int compress) | |
b7a00563 TT |
622 | { |
623 | struct write_dir_struct wd; | |
624 | errcode_t retval; | |
625 | struct ext2_inode inode; | |
626 | ||
627 | retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num); | |
628 | if (retval) | |
629 | return retval; | |
630 | ||
631 | wd.outdir = outdir; | |
632 | wd.err = 0; | |
633 | wd.ctx = ctx; | |
634 | wd.cleared = 0; | |
635 | ||
636 | retval = ext2fs_block_iterate2(fs, ino, 0, 0, | |
637 | write_dir_block, &wd); | |
638 | if (retval) | |
639 | return retval; | |
640 | if (wd.err) | |
641 | return wd.err; | |
642 | ||
643 | e2fsck_read_inode(ctx, ino, &inode, "rehash_dir"); | |
e70ae99e TT |
644 | if (compress) |
645 | inode.i_flags &= ~EXT2_INDEX_FL; | |
646 | else | |
850d05e9 | 647 | inode.i_flags |= EXT2_INDEX_FL; |
b7a00563 TT |
648 | inode.i_size = outdir->num * fs->blocksize; |
649 | inode.i_blocks -= (fs->blocksize / 512) * wd.cleared; | |
650 | e2fsck_write_inode(ctx, ino, &inode, "rehash_dir"); | |
651 | ||
652 | return 0; | |
653 | } | |
654 | ||
655 | errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino) | |
656 | { | |
657 | ext2_filsys fs = ctx->fs; | |
658 | errcode_t retval; | |
659 | struct ext2_inode inode; | |
850d05e9 | 660 | char *dir_buf = 0; |
b7a00563 | 661 | struct fill_dir_struct fd; |
b7a00563 | 662 | struct out_dir outdir; |
b7a00563 | 663 | |
1d2eef42 TT |
664 | outdir.max = outdir.num = 0; |
665 | outdir.buf = 0; | |
666 | outdir.hashes = 0; | |
b7a00563 | 667 | e2fsck_read_inode(ctx, ino, &inode, "rehash_dir"); |
b7a00563 TT |
668 | |
669 | retval = ENOMEM; | |
670 | fd.harray = 0; | |
671 | dir_buf = malloc(inode.i_size); | |
672 | if (!dir_buf) | |
673 | goto errout; | |
674 | ||
675 | fd.max_array = inode.i_size / 32; | |
676 | fd.num_array = 0; | |
677 | fd.harray = malloc(fd.max_array * sizeof(struct hash_entry)); | |
678 | if (!fd.harray) | |
679 | goto errout; | |
680 | ||
681 | fd.ctx = ctx; | |
682 | fd.buf = dir_buf; | |
683 | fd.inode = &inode; | |
684 | fd.err = 0; | |
685 | fd.dir_size = 0; | |
850d05e9 TT |
686 | fd.compress = 0; |
687 | if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) || | |
e70ae99e | 688 | (inode.i_size / fs->blocksize) < 2) |
850d05e9 | 689 | fd.compress = 1; |
b7a00563 TT |
690 | fd.parent = 0; |
691 | ||
692 | /* Read in the entire directory into memory */ | |
693 | retval = ext2fs_block_iterate2(fs, ino, 0, 0, | |
694 | fill_dir_block, &fd); | |
695 | if (fd.err) { | |
696 | retval = fd.err; | |
697 | goto errout; | |
698 | } | |
699 | ||
700 | #if 0 | |
701 | printf("%d entries (%d bytes) found in inode %d\n", | |
702 | fd.num_array, fd.dir_size, ino); | |
703 | #endif | |
704 | ||
850d05e9 | 705 | /* Sort the list */ |
b0700a1b | 706 | resort: |
850d05e9 TT |
707 | if (fd.compress) |
708 | qsort(fd.harray+2, fd.num_array-2, | |
709 | sizeof(struct hash_entry), name_cmp); | |
710 | else | |
711 | qsort(fd.harray, fd.num_array, | |
712 | sizeof(struct hash_entry), hash_cmp); | |
713 | ||
b0700a1b TT |
714 | /* |
715 | * Look for duplicates | |
716 | */ | |
717 | if (duplicate_search_and_fix(ctx, fs, ino, &fd)) | |
718 | goto resort; | |
719 | ||
1d2eef42 TT |
720 | if (ctx->options & E2F_OPT_NO) { |
721 | retval = 0; | |
722 | goto errout; | |
723 | } | |
724 | ||
850d05e9 TT |
725 | /* |
726 | * Copy the directory entries. In a htree directory these | |
727 | * will become the leaf nodes. | |
728 | */ | |
729 | retval = copy_dir_entries(fs, &fd, &outdir); | |
b7a00563 TT |
730 | if (retval) |
731 | goto errout; | |
850d05e9 | 732 | |
b7a00563 TT |
733 | free(dir_buf); dir_buf = 0; |
734 | ||
850d05e9 TT |
735 | if (!fd.compress) { |
736 | /* Calculate the interior nodes */ | |
737 | retval = calculate_tree(fs, &outdir, ino, fd.parent); | |
738 | if (retval) | |
739 | goto errout; | |
b7a00563 | 740 | } |
850d05e9 TT |
741 | |
742 | retval = write_directory(ctx, fs, &outdir, ino, fd.compress); | |
b7a00563 TT |
743 | if (retval) |
744 | goto errout; | |
745 | ||
746 | errout: | |
747 | if (dir_buf) | |
748 | free(dir_buf); | |
850d05e9 TT |
749 | if (fd.harray) |
750 | free(fd.harray); | |
751 | ||
b7a00563 TT |
752 | free_out_dir(&outdir); |
753 | return retval; | |
754 | } | |
755 | ||
756 | void e2fsck_rehash_directories(e2fsck_t ctx) | |
757 | { | |
b7a00563 | 758 | struct problem_context pctx; |
850d05e9 TT |
759 | #ifdef RESOURCE_TRACK |
760 | struct resource_track rtrack; | |
761 | #endif | |
762 | struct dir_info *dir; | |
763 | ext2_u32_iterate iter; | |
764 | ext2_ino_t ino; | |
765 | errcode_t retval; | |
b0700a1b | 766 | int i, cur, max, all_dirs, dir_index, first = 1; |
850d05e9 TT |
767 | |
768 | #ifdef RESOURCE_TRACK | |
769 | init_resource_track(&rtrack); | |
770 | #endif | |
b7a00563 | 771 | |
850d05e9 TT |
772 | all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS; |
773 | ||
774 | if (!ctx->dirs_to_hash && !all_dirs) | |
b7a00563 TT |
775 | return; |
776 | ||
850d05e9 | 777 | e2fsck_get_lost_and_found(ctx, 0); |
b7a00563 TT |
778 | |
779 | clear_problem_context(&pctx); | |
850d05e9 TT |
780 | |
781 | dir_index = ctx->fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX; | |
b0700a1b TT |
782 | cur = 0; |
783 | if (all_dirs) { | |
850d05e9 | 784 | i = 0; |
b0700a1b TT |
785 | max = e2fsck_get_num_dirinfo(ctx); |
786 | } else { | |
850d05e9 TT |
787 | retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash, |
788 | &iter); | |
789 | if (retval) { | |
790 | pctx.errcode = retval; | |
791 | fix_problem(ctx, PR_3A_OPTIMIZE_ITER, &pctx); | |
792 | return; | |
793 | } | |
b0700a1b | 794 | max = ext2fs_u32_list_count(ctx->dirs_to_hash); |
b7a00563 | 795 | } |
850d05e9 TT |
796 | while (1) { |
797 | if (all_dirs) { | |
798 | if ((dir = e2fsck_dir_info_iter(ctx, &i)) == 0) | |
799 | break; | |
800 | ino = dir->ino; | |
801 | } else { | |
802 | if (!ext2fs_u32_list_iterate(iter, &ino)) | |
803 | break; | |
804 | } | |
805 | if (ino == ctx->lost_and_found) | |
b7a00563 TT |
806 | continue; |
807 | pctx.dir = ino; | |
808 | if (first) { | |
809 | fix_problem(ctx, PR_3A_PASS_HEADER, &pctx); | |
810 | first = 0; | |
811 | } | |
b0700a1b | 812 | #if 0 |
850d05e9 | 813 | fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx); |
b0700a1b | 814 | #endif |
b7a00563 TT |
815 | pctx.errcode = e2fsck_rehash_dir(ctx, ino); |
816 | if (pctx.errcode) { | |
850d05e9 TT |
817 | end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR); |
818 | fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx); | |
b7a00563 | 819 | } |
52734dc5 TT |
820 | if (ctx->progress && !ctx->progress_fd) |
821 | e2fsck_simple_progress(ctx, "Rebuilding directory", | |
1d2eef42 | 822 | 100.0 * (float) (++cur) / (float) max, ino); |
b7a00563 | 823 | } |
850d05e9 TT |
824 | end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR); |
825 | if (!all_dirs) | |
826 | ext2fs_u32_list_iterate_end(iter); | |
b7a00563 | 827 | |
850d05e9 TT |
828 | if (ctx->dirs_to_hash) |
829 | ext2fs_u32_list_free(ctx->dirs_to_hash); | |
b7a00563 | 830 | ctx->dirs_to_hash = 0; |
850d05e9 TT |
831 | |
832 | #ifdef RESOURCE_TRACK | |
833 | if (ctx->options & E2F_OPT_TIME2) { | |
834 | e2fsck_clear_progbar(ctx); | |
835 | print_resource_track("Pass 3A", &rtrack); | |
836 | } | |
837 | #endif | |
b7a00563 | 838 | } |