]>
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; | |
70f4632b | 213 | unsigned int he_a_len, he_b_len; |
b7a00563 | 214 | int ret; |
b0700a1b TT |
215 | int min_len; |
216 | ||
70f4632b JK |
217 | he_a_len = ext2fs_dirent_name_len(he_a->dir); |
218 | he_b_len = ext2fs_dirent_name_len(he_b->dir); | |
219 | min_len = he_a_len; | |
220 | if (min_len > he_b_len) | |
221 | min_len = he_b_len; | |
b0700a1b TT |
222 | |
223 | ret = strncmp(he_a->dir->name, he_b->dir->name, min_len); | |
224 | if (ret == 0) { | |
70f4632b | 225 | if (he_a_len > he_b_len) |
b7a00563 | 226 | ret = 1; |
70f4632b | 227 | else if (he_a_len < he_b_len) |
b7a00563 TT |
228 | ret = -1; |
229 | else | |
12dd69f5 | 230 | ret = he_b->dir->inode - he_a->dir->inode; |
b7a00563 TT |
231 | } |
232 | return ret; | |
233 | } | |
234 | ||
850d05e9 | 235 | /* Used for sorting the hash entry */ |
b0700a1b | 236 | static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b) |
850d05e9 TT |
237 | { |
238 | const struct hash_entry *he_a = (const struct hash_entry *) a; | |
239 | const struct hash_entry *he_b = (const struct hash_entry *) b; | |
240 | int ret; | |
efc6f628 | 241 | |
b0700a1b TT |
242 | if (he_a->hash > he_b->hash) |
243 | ret = 1; | |
244 | else if (he_a->hash < he_b->hash) | |
245 | ret = -1; | |
246 | else { | |
247 | if (he_a->minor_hash > he_b->minor_hash) | |
850d05e9 | 248 | ret = 1; |
b0700a1b | 249 | else if (he_a->minor_hash < he_b->minor_hash) |
850d05e9 TT |
250 | ret = -1; |
251 | else | |
b0700a1b | 252 | ret = name_cmp(a, b); |
850d05e9 TT |
253 | } |
254 | return ret; | |
255 | } | |
256 | ||
efc6f628 | 257 | static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir, |
b7a00563 TT |
258 | int blocks) |
259 | { | |
260 | void *new_mem; | |
261 | ||
262 | if (outdir->max) { | |
263 | new_mem = realloc(outdir->buf, blocks * fs->blocksize); | |
264 | if (!new_mem) | |
265 | return ENOMEM; | |
266 | outdir->buf = new_mem; | |
267 | new_mem = realloc(outdir->hashes, | |
268 | blocks * sizeof(ext2_dirhash_t)); | |
269 | if (!new_mem) | |
270 | return ENOMEM; | |
271 | outdir->hashes = new_mem; | |
272 | } else { | |
273 | outdir->buf = malloc(blocks * fs->blocksize); | |
274 | outdir->hashes = malloc(blocks * sizeof(ext2_dirhash_t)); | |
275 | outdir->num = 0; | |
276 | } | |
277 | outdir->max = blocks; | |
278 | return 0; | |
279 | } | |
280 | ||
281 | static void free_out_dir(struct out_dir *outdir) | |
282 | { | |
45e338f5 JM |
283 | free(outdir->buf); |
284 | free(outdir->hashes); | |
b7a00563 TT |
285 | outdir->max = 0; |
286 | outdir->num =0; | |
287 | } | |
288 | ||
850d05e9 | 289 | static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir, |
b7a00563 TT |
290 | char ** ret) |
291 | { | |
292 | errcode_t retval; | |
293 | ||
294 | if (outdir->num >= outdir->max) { | |
295 | retval = alloc_size_dir(fs, outdir, outdir->max + 50); | |
296 | if (retval) | |
297 | return retval; | |
298 | } | |
299 | *ret = outdir->buf + (outdir->num++ * fs->blocksize); | |
850d05e9 | 300 | memset(*ret, 0, fs->blocksize); |
b7a00563 TT |
301 | return 0; |
302 | } | |
303 | ||
b0700a1b TT |
304 | /* |
305 | * This function is used to make a unique filename. We do this by | |
306 | * appending ~0, and then incrementing the number. However, we cannot | |
307 | * expand the length of the filename beyond the padding available in | |
308 | * the directory entry. | |
309 | */ | |
70f4632b | 310 | static void mutate_name(char *str, unsigned int *len) |
b0700a1b TT |
311 | { |
312 | int i; | |
70f4632b | 313 | unsigned int l = *len; |
efc6f628 | 314 | |
b0700a1b TT |
315 | /* |
316 | * First check to see if it looks the name has been mutated | |
317 | * already | |
318 | */ | |
319 | for (i = l-1; i > 0; i--) { | |
320 | if (!isdigit(str[i])) | |
321 | break; | |
322 | } | |
323 | if ((i == l-1) || (str[i] != '~')) { | |
324 | if (((l-1) & 3) < 2) | |
325 | l += 2; | |
326 | else | |
327 | l = (l+3) & ~3; | |
328 | str[l-2] = '~'; | |
329 | str[l-1] = '0'; | |
70f4632b | 330 | *len = l; |
b0700a1b TT |
331 | return; |
332 | } | |
333 | for (i = l-1; i >= 0; i--) { | |
334 | if (isdigit(str[i])) { | |
335 | if (str[i] == '9') | |
336 | str[i] = '0'; | |
337 | else { | |
338 | str[i]++; | |
339 | return; | |
340 | } | |
341 | continue; | |
342 | } | |
343 | if (i == 1) { | |
344 | if (str[0] == 'z') | |
345 | str[0] = 'A'; | |
346 | else if (str[0] == 'Z') { | |
347 | str[0] = '~'; | |
348 | str[1] = '0'; | |
349 | } else | |
350 | str[0]++; | |
351 | } else if (i > 0) { | |
352 | str[i] = '1'; | |
353 | str[i-1] = '~'; | |
354 | } else { | |
355 | if (str[0] == '~') | |
356 | str[0] = 'a'; | |
efc6f628 | 357 | else |
b0700a1b TT |
358 | str[0]++; |
359 | } | |
360 | break; | |
361 | } | |
362 | } | |
363 | ||
364 | static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs, | |
365 | ext2_ino_t ino, | |
366 | struct fill_dir_struct *fd) | |
367 | { | |
368 | struct problem_context pctx; | |
369 | struct hash_entry *ent, *prev; | |
370 | int i, j; | |
371 | int fixed = 0; | |
372 | char new_name[256]; | |
70f4632b | 373 | unsigned int new_len; |
f77704e4 | 374 | int hash_alg; |
efc6f628 | 375 | |
b0700a1b TT |
376 | clear_problem_context(&pctx); |
377 | pctx.ino = ino; | |
378 | ||
f77704e4 TT |
379 | hash_alg = fs->super->s_def_hash_version; |
380 | if ((hash_alg <= EXT2_HASH_TEA) && | |
381 | (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH)) | |
382 | hash_alg += 3; | |
383 | ||
b0700a1b TT |
384 | for (i=1; i < fd->num_array; i++) { |
385 | ent = fd->harray + i; | |
386 | prev = ent - 1; | |
387 | if (!ent->dir->inode || | |
70f4632b JK |
388 | (ext2fs_dirent_name_len(ent->dir) != |
389 | ext2fs_dirent_name_len(prev->dir)) || | |
390 | strncmp(ent->dir->name, prev->dir->name, | |
391 | ext2fs_dirent_name_len(ent->dir))) | |
b0700a1b TT |
392 | continue; |
393 | pctx.dirent = ent->dir; | |
394 | if ((ent->dir->inode == prev->dir->inode) && | |
395 | fix_problem(ctx, PR_2_DUPLICATE_DIRENT, &pctx)) { | |
396 | e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1); | |
397 | ent->dir->inode = 0; | |
398 | fixed++; | |
399 | continue; | |
400 | } | |
70f4632b JK |
401 | new_len = ext2fs_dirent_name_len(ent->dir); |
402 | memcpy(new_name, ent->dir->name, new_len); | |
b0700a1b TT |
403 | mutate_name(new_name, &new_len); |
404 | for (j=0; j < fd->num_array; j++) { | |
405 | if ((i==j) || | |
70f4632b JK |
406 | (new_len != |
407 | ext2fs_dirent_name_len(fd->harray[j].dir)) || | |
408 | strncmp(new_name, fd->harray[j].dir->name, new_len)) | |
b0700a1b TT |
409 | continue; |
410 | mutate_name(new_name, &new_len); | |
efc6f628 | 411 | |
b0700a1b TT |
412 | j = -1; |
413 | } | |
70f4632b | 414 | new_name[new_len] = 0; |
b0700a1b TT |
415 | pctx.str = new_name; |
416 | if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) { | |
70f4632b JK |
417 | memcpy(ent->dir->name, new_name, new_len); |
418 | ext2fs_dirent_set_name_len(ent->dir, new_len); | |
437651ad TT |
419 | ext2fs_dirhash(hash_alg, new_name, new_len, |
420 | fs->super->s_hash_seed, | |
421 | &ent->hash, &ent->minor_hash); | |
b0700a1b TT |
422 | fixed++; |
423 | } | |
424 | } | |
425 | return fixed; | |
426 | } | |
427 | ||
b7a00563 | 428 | |
7dca4c88 | 429 | static errcode_t copy_dir_entries(e2fsck_t ctx, |
850d05e9 | 430 | struct fill_dir_struct *fd, |
fd9ca825 | 431 | struct out_dir *outdir) |
850d05e9 | 432 | { |
7dca4c88 | 433 | ext2_filsys fs = ctx->fs; |
850d05e9 TT |
434 | errcode_t retval; |
435 | char *block_start; | |
436 | struct hash_entry *ent; | |
437 | struct ext2_dir_entry *dirent; | |
68477355 TT |
438 | unsigned int rec_len, prev_rec_len, left, slack, offset; |
439 | int i; | |
850d05e9 | 440 | ext2_dirhash_t prev_hash; |
81683c6a DW |
441 | int csum_size = 0; |
442 | struct ext2_dir_entry_tail *t; | |
7dca4c88 TT |
443 | |
444 | if (ctx->htree_slack_percentage == 255) { | |
445 | profile_get_uint(ctx->profile, "options", | |
446 | "indexed_dir_slack_percentage", | |
447 | 0, 20, | |
448 | &ctx->htree_slack_percentage); | |
449 | if (ctx->htree_slack_percentage > 100) | |
450 | ctx->htree_slack_percentage = 20; | |
451 | } | |
efc6f628 | 452 | |
86f3b6cf | 453 | if (ext2fs_has_feature_metadata_csum(fs->super)) |
81683c6a DW |
454 | csum_size = sizeof(struct ext2_dir_entry_tail); |
455 | ||
850d05e9 TT |
456 | outdir->max = 0; |
457 | retval = alloc_size_dir(fs, outdir, | |
458 | (fd->dir_size / fs->blocksize) + 2); | |
459 | if (retval) | |
460 | return retval; | |
461 | outdir->num = fd->compress ? 0 : 1; | |
462 | offset = 0; | |
463 | outdir->hashes[0] = 0; | |
464 | prev_hash = 1; | |
465 | if ((retval = get_next_block(fs, outdir, &block_start))) | |
466 | return retval; | |
467 | dirent = (struct ext2_dir_entry *) block_start; | |
8a480350 | 468 | prev_rec_len = 0; |
cf5301d7 | 469 | rec_len = 0; |
81683c6a | 470 | left = fs->blocksize - csum_size; |
7dca4c88 | 471 | slack = fd->compress ? 12 : |
81683c6a | 472 | ((fs->blocksize - csum_size) * ctx->htree_slack_percentage)/100; |
7dca4c88 TT |
473 | if (slack < 12) |
474 | slack = 12; | |
cf5301d7 | 475 | for (i = 0; i < fd->num_array; i++) { |
850d05e9 | 476 | ent = fd->harray + i; |
b0700a1b TT |
477 | if (ent->dir->inode == 0) |
478 | continue; | |
70f4632b | 479 | rec_len = EXT2_DIR_REC_LEN(ext2fs_dirent_name_len(ent->dir)); |
850d05e9 | 480 | if (rec_len > left) { |
8a480350 TT |
481 | if (left) { |
482 | left += prev_rec_len; | |
483 | retval = ext2fs_set_rec_len(fs, left, dirent); | |
484 | if (retval) | |
485 | return retval; | |
486 | } | |
81683c6a DW |
487 | if (csum_size) { |
488 | t = EXT2_DIRENT_TAIL(block_start, | |
489 | fs->blocksize); | |
490 | ext2fs_initialize_dirent_tail(fs, t); | |
491 | } | |
850d05e9 TT |
492 | if ((retval = get_next_block(fs, outdir, |
493 | &block_start))) | |
494 | return retval; | |
fe5b72d1 | 495 | offset = 0; |
850d05e9 | 496 | } |
81683c6a | 497 | left = (fs->blocksize - csum_size) - offset; |
fe5b72d1 | 498 | dirent = (struct ext2_dir_entry *) (block_start + offset); |
850d05e9 TT |
499 | if (offset == 0) { |
500 | if (ent->hash == prev_hash) | |
501 | outdir->hashes[outdir->num-1] = ent->hash | 1; | |
502 | else | |
503 | outdir->hashes[outdir->num-1] = ent->hash; | |
504 | } | |
505 | dirent->inode = ent->dir->inode; | |
70f4632b JK |
506 | ext2fs_dirent_set_name_len(dirent, |
507 | ext2fs_dirent_name_len(ent->dir)); | |
508 | ext2fs_dirent_set_file_type(dirent, | |
509 | ext2fs_dirent_file_type(ent->dir)); | |
8a480350 TT |
510 | retval = ext2fs_set_rec_len(fs, rec_len, dirent); |
511 | if (retval) | |
512 | return retval; | |
513 | prev_rec_len = rec_len; | |
70f4632b JK |
514 | memcpy(dirent->name, ent->dir->name, |
515 | ext2fs_dirent_name_len(dirent)); | |
850d05e9 TT |
516 | offset += rec_len; |
517 | left -= rec_len; | |
7dca4c88 | 518 | if (left < slack) { |
8a480350 TT |
519 | prev_rec_len += left; |
520 | retval = ext2fs_set_rec_len(fs, prev_rec_len, dirent); | |
521 | if (retval) | |
522 | return retval; | |
850d05e9 | 523 | offset += left; |
cf3909ed | 524 | left = 0; |
850d05e9 TT |
525 | } |
526 | prev_hash = ent->hash; | |
527 | } | |
528 | if (left) | |
8a480350 | 529 | retval = ext2fs_set_rec_len(fs, rec_len + left, dirent); |
81683c6a DW |
530 | if (csum_size) { |
531 | t = EXT2_DIRENT_TAIL(block_start, fs->blocksize); | |
532 | ext2fs_initialize_dirent_tail(fs, t); | |
533 | } | |
850d05e9 | 534 | |
8a480350 | 535 | return retval; |
850d05e9 TT |
536 | } |
537 | ||
538 | ||
539 | static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf, | |
b7a00563 TT |
540 | ext2_ino_t ino, ext2_ino_t parent) |
541 | { | |
542 | struct ext2_dir_entry *dir; | |
543 | struct ext2_dx_root_info *root; | |
544 | struct ext2_dx_countlimit *limits; | |
850d05e9 | 545 | int filetype = 0; |
07307114 | 546 | int csum_size = 0; |
b7a00563 | 547 | |
86f3b6cf | 548 | if (ext2fs_has_feature_filetype(fs->super)) |
70f4632b | 549 | filetype = EXT2_FT_DIR; |
efc6f628 | 550 | |
b7a00563 TT |
551 | memset(buf, 0, fs->blocksize); |
552 | dir = (struct ext2_dir_entry *) buf; | |
553 | dir->inode = ino; | |
554 | dir->name[0] = '.'; | |
70f4632b JK |
555 | ext2fs_dirent_set_name_len(dir, 1); |
556 | ext2fs_dirent_set_file_type(dir, filetype); | |
b7a00563 TT |
557 | dir->rec_len = 12; |
558 | dir = (struct ext2_dir_entry *) (buf + 12); | |
559 | dir->inode = parent; | |
560 | dir->name[0] = '.'; | |
561 | dir->name[1] = '.'; | |
70f4632b JK |
562 | ext2fs_dirent_set_name_len(dir, 2); |
563 | ext2fs_dirent_set_file_type(dir, filetype); | |
b7a00563 | 564 | dir->rec_len = fs->blocksize - 12; |
efc6f628 | 565 | |
b7a00563 TT |
566 | root = (struct ext2_dx_root_info *) (buf+24); |
567 | root->reserved_zero = 0; | |
568 | root->hash_version = fs->super->s_def_hash_version; | |
569 | root->info_length = 8; | |
570 | root->indirect_levels = 0; | |
571 | root->unused_flags = 0; | |
572 | ||
86f3b6cf | 573 | if (ext2fs_has_feature_metadata_csum(fs->super)) |
07307114 DW |
574 | csum_size = sizeof(struct ext2_dx_tail); |
575 | ||
b7a00563 | 576 | limits = (struct ext2_dx_countlimit *) (buf+32); |
07307114 DW |
577 | limits->limit = (fs->blocksize - (32 + csum_size)) / |
578 | sizeof(struct ext2_dx_entry); | |
b7a00563 TT |
579 | limits->count = 0; |
580 | ||
581 | return root; | |
582 | } | |
583 | ||
584 | ||
850d05e9 | 585 | static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf) |
b7a00563 TT |
586 | { |
587 | struct ext2_dir_entry *dir; | |
588 | struct ext2_dx_countlimit *limits; | |
07307114 | 589 | int csum_size = 0; |
b7a00563 TT |
590 | |
591 | memset(buf, 0, fs->blocksize); | |
592 | dir = (struct ext2_dir_entry *) buf; | |
593 | dir->inode = 0; | |
8a480350 | 594 | (void) ext2fs_set_rec_len(fs, fs->blocksize, dir); |
efc6f628 | 595 | |
86f3b6cf | 596 | if (ext2fs_has_feature_metadata_csum(fs->super)) |
07307114 DW |
597 | csum_size = sizeof(struct ext2_dx_tail); |
598 | ||
b7a00563 | 599 | limits = (struct ext2_dx_countlimit *) (buf+8); |
07307114 DW |
600 | limits->limit = (fs->blocksize - (8 + csum_size)) / |
601 | sizeof(struct ext2_dx_entry); | |
b7a00563 TT |
602 | limits->count = 0; |
603 | ||
604 | return (struct ext2_dx_entry *) limits; | |
605 | } | |
606 | ||
850d05e9 TT |
607 | /* |
608 | * This function takes the leaf nodes which have been written in | |
609 | * outdir, and populates the root node and any necessary interior nodes. | |
610 | */ | |
611 | static errcode_t calculate_tree(ext2_filsys fs, | |
612 | struct out_dir *outdir, | |
613 | ext2_ino_t ino, | |
614 | ext2_ino_t parent) | |
615 | { | |
616 | struct ext2_dx_root_info *root_info; | |
617 | struct ext2_dx_entry *root, *dx_ent = 0; | |
618 | struct ext2_dx_countlimit *root_limit, *limit; | |
619 | errcode_t retval; | |
620 | char * block_start; | |
621 | int i, c1, c2, nblks; | |
622 | int limit_offset, root_offset; | |
efc6f628 | 623 | |
850d05e9 TT |
624 | root_info = set_root_node(fs, outdir->buf, ino, parent); |
625 | root_offset = limit_offset = ((char *) root_info - outdir->buf) + | |
626 | root_info->info_length; | |
627 | root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset); | |
628 | c1 = root_limit->limit; | |
629 | nblks = outdir->num; | |
630 | ||
631 | /* Write out the pointer blocks */ | |
632 | if (nblks-1 <= c1) { | |
633 | /* Just write out the root block, and we're done */ | |
634 | root = (struct ext2_dx_entry *) (outdir->buf + root_offset); | |
635 | for (i=1; i < nblks; i++) { | |
636 | root->block = ext2fs_cpu_to_le32(i); | |
637 | if (i != 1) | |
638 | root->hash = | |
639 | ext2fs_cpu_to_le32(outdir->hashes[i]); | |
640 | root++; | |
641 | c1--; | |
642 | } | |
643 | } else { | |
644 | c2 = 0; | |
645 | limit = 0; | |
646 | root_info->indirect_levels = 1; | |
647 | for (i=1; i < nblks; i++) { | |
648 | if (c1 == 0) | |
649 | return ENOSPC; | |
650 | if (c2 == 0) { | |
651 | if (limit) | |
efc6f628 | 652 | limit->limit = limit->count = |
850d05e9 TT |
653 | ext2fs_cpu_to_le16(limit->limit); |
654 | root = (struct ext2_dx_entry *) | |
655 | (outdir->buf + root_offset); | |
656 | root->block = ext2fs_cpu_to_le32(outdir->num); | |
657 | if (i != 1) | |
658 | root->hash = | |
659 | ext2fs_cpu_to_le32(outdir->hashes[i]); | |
660 | if ((retval = get_next_block(fs, outdir, | |
661 | &block_start))) | |
662 | return retval; | |
663 | dx_ent = set_int_node(fs, block_start); | |
664 | limit = (struct ext2_dx_countlimit *) dx_ent; | |
665 | c2 = limit->limit; | |
666 | root_offset += sizeof(struct ext2_dx_entry); | |
667 | c1--; | |
668 | } | |
669 | dx_ent->block = ext2fs_cpu_to_le32(i); | |
670 | if (c2 != limit->limit) | |
671 | dx_ent->hash = | |
672 | ext2fs_cpu_to_le32(outdir->hashes[i]); | |
673 | dx_ent++; | |
674 | c2--; | |
675 | } | |
676 | limit->count = ext2fs_cpu_to_le16(limit->limit - c2); | |
677 | limit->limit = ext2fs_cpu_to_le16(limit->limit); | |
678 | } | |
679 | root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset); | |
680 | root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1); | |
681 | root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit); | |
682 | ||
683 | return 0; | |
684 | } | |
b7a00563 TT |
685 | |
686 | struct write_dir_struct { | |
687 | struct out_dir *outdir; | |
688 | errcode_t err; | |
19961cd0 | 689 | ext2_ino_t ino; |
b7a00563 | 690 | e2fsck_t ctx; |
81683c6a | 691 | ext2_ino_t dir; |
b7a00563 TT |
692 | }; |
693 | ||
694 | /* | |
850d05e9 | 695 | * Helper function which writes out a directory block. |
b7a00563 TT |
696 | */ |
697 | static int write_dir_block(ext2_filsys fs, | |
6dc64392 | 698 | blk64_t *block_nr, |
b7a00563 | 699 | e2_blkcnt_t blockcnt, |
6dc64392 | 700 | blk64_t ref_block EXT2FS_ATTR((unused)), |
efc6f628 | 701 | int ref_offset EXT2FS_ATTR((unused)), |
b7a00563 TT |
702 | void *priv_data) |
703 | { | |
704 | struct write_dir_struct *wd = (struct write_dir_struct *) priv_data; | |
6dc64392 | 705 | blk64_t blk; |
82372e32 | 706 | char *dir, *buf = 0; |
b7a00563 | 707 | |
19961cd0 AD |
708 | #ifdef REHASH_DEBUG |
709 | printf("%u: write_dir_block %lld:%lld", wd->ino, blockcnt, *block_nr); | |
710 | #endif | |
94676ef2 | 711 | if ((*block_nr == 0) || (blockcnt < 0)) { |
19961cd0 AD |
712 | #ifdef REHASH_DEBUG |
713 | printf(" - skip\n"); | |
714 | #endif | |
82372e32 | 715 | return 0; |
19961cd0 | 716 | } |
82372e32 TT |
717 | if (blockcnt < wd->outdir->num) |
718 | dir = wd->outdir->buf + (blockcnt * fs->blocksize); | |
719 | else if (wd->ctx->lost_and_found == wd->dir) { | |
720 | /* Don't release any extra directory blocks for lost+found */ | |
721 | wd->err = ext2fs_new_dir_block(fs, 0, 0, &buf); | |
722 | if (wd->err) | |
723 | return BLOCK_ABORT; | |
724 | dir = buf; | |
725 | wd->outdir->num++; | |
726 | } else { | |
94676ef2 TT |
727 | /* Don't free blocks at the end of the directory, they |
728 | * will be truncated by the caller. */ | |
19961cd0 AD |
729 | #ifdef REHASH_DEBUG |
730 | printf(" - not freed\n"); | |
731 | #endif | |
732 | return 0; | |
b7a00563 | 733 | } |
81683c6a | 734 | wd->err = ext2fs_write_dir_block4(fs, *block_nr, dir, 0, wd->dir); |
82372e32 TT |
735 | if (buf) |
736 | ext2fs_free_mem(&buf); | |
737 | ||
19961cd0 AD |
738 | #ifdef REHASH_DEBUG |
739 | printf(" - write (%d)\n", wd->err); | |
740 | #endif | |
b7a00563 TT |
741 | if (wd->err) |
742 | return BLOCK_ABORT; | |
743 | return 0; | |
744 | } | |
745 | ||
b7a00563 | 746 | static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs, |
850d05e9 | 747 | struct out_dir *outdir, |
e228d700 DW |
748 | ext2_ino_t ino, struct ext2_inode *inode, |
749 | int compress) | |
b7a00563 TT |
750 | { |
751 | struct write_dir_struct wd; | |
752 | errcode_t retval; | |
b7a00563 TT |
753 | |
754 | retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num); | |
755 | if (retval) | |
756 | return retval; | |
757 | ||
758 | wd.outdir = outdir; | |
759 | wd.err = 0; | |
19961cd0 | 760 | wd.ino = ino; |
b7a00563 | 761 | wd.ctx = ctx; |
81683c6a | 762 | wd.dir = ino; |
b7a00563 | 763 | |
19961cd0 | 764 | retval = ext2fs_block_iterate3(fs, ino, 0, NULL, |
b7a00563 TT |
765 | write_dir_block, &wd); |
766 | if (retval) | |
767 | return retval; | |
768 | if (wd.err) | |
769 | return wd.err; | |
770 | ||
e228d700 | 771 | e2fsck_read_inode(ctx, ino, inode, "rehash_dir"); |
e70ae99e | 772 | if (compress) |
e228d700 | 773 | inode->i_flags &= ~EXT2_INDEX_FL; |
e70ae99e | 774 | else |
e228d700 | 775 | inode->i_flags |= EXT2_INDEX_FL; |
19961cd0 AD |
776 | #ifdef REHASH_DEBUG |
777 | printf("%u: set inode size to %u blocks = %u bytes\n", | |
778 | ino, outdir->num, outdir->num * fs->blocksize); | |
779 | #endif | |
94676ef2 | 780 | retval = ext2fs_inode_size_set(fs, inode, (ext2_off64_t)outdir->num * |
19961cd0 | 781 | fs->blocksize); |
97c607b1 DW |
782 | if (retval) |
783 | return retval; | |
b7a00563 | 784 | |
19961cd0 | 785 | /* ext2fs_punch() calls ext2fs_write_inode() which writes the size */ |
94676ef2 | 786 | return ext2fs_punch(fs, ino, inode, NULL, outdir->num, ~0ULL); |
b7a00563 TT |
787 | } |
788 | ||
e228d700 DW |
789 | errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino, |
790 | struct problem_context *pctx) | |
b7a00563 TT |
791 | { |
792 | ext2_filsys fs = ctx->fs; | |
793 | errcode_t retval; | |
794 | struct ext2_inode inode; | |
850d05e9 | 795 | char *dir_buf = 0; |
19961cd0 AD |
796 | struct fill_dir_struct fd = { NULL }; |
797 | struct out_dir outdir = { 0 }; | |
efc6f628 | 798 | |
b7a00563 | 799 | e2fsck_read_inode(ctx, ino, &inode, "rehash_dir"); |
b7a00563 | 800 | |
86f3b6cf | 801 | if (ext2fs_has_feature_inline_data(fs->super) && |
81ac00d0 DW |
802 | (inode.i_flags & EXT4_INLINE_DATA_FL)) |
803 | return 0; | |
804 | ||
b7a00563 | 805 | retval = ENOMEM; |
b7a00563 TT |
806 | dir_buf = malloc(inode.i_size); |
807 | if (!dir_buf) | |
808 | goto errout; | |
809 | ||
810 | fd.max_array = inode.i_size / 32; | |
b7a00563 TT |
811 | fd.harray = malloc(fd.max_array * sizeof(struct hash_entry)); |
812 | if (!fd.harray) | |
813 | goto errout; | |
814 | ||
19961cd0 | 815 | fd.ino = ino; |
b7a00563 TT |
816 | fd.ctx = ctx; |
817 | fd.buf = dir_buf; | |
818 | fd.inode = &inode; | |
81683c6a | 819 | fd.dir = ino; |
86f3b6cf | 820 | if (!ext2fs_has_feature_dir_index(fs->super) || |
e70ae99e | 821 | (inode.i_size / fs->blocksize) < 2) |
850d05e9 | 822 | fd.compress = 1; |
b7a00563 TT |
823 | fd.parent = 0; |
824 | ||
f4e14505 | 825 | retry_nohash: |
b7a00563 | 826 | /* Read in the entire directory into memory */ |
6dc64392 | 827 | retval = ext2fs_block_iterate3(fs, ino, 0, 0, |
b7a00563 TT |
828 | fill_dir_block, &fd); |
829 | if (fd.err) { | |
830 | retval = fd.err; | |
831 | goto errout; | |
832 | } | |
833 | ||
f4e14505 TT |
834 | /* |
835 | * If the entries read are less than a block, then don't index | |
836 | * the directory | |
837 | */ | |
838 | if (!fd.compress && (fd.dir_size < (fs->blocksize - 24))) { | |
839 | fd.compress = 1; | |
840 | fd.dir_size = 0; | |
841 | fd.num_array = 0; | |
842 | goto retry_nohash; | |
843 | } | |
844 | ||
b7a00563 TT |
845 | #if 0 |
846 | printf("%d entries (%d bytes) found in inode %d\n", | |
847 | fd.num_array, fd.dir_size, ino); | |
848 | #endif | |
849 | ||
850d05e9 | 850 | /* Sort the list */ |
b0700a1b | 851 | resort: |
c23b2cc4 | 852 | if (fd.compress && fd.num_array > 1) |
53fbfb2b TT |
853 | qsort(fd.harray+2, fd.num_array-2, sizeof(struct hash_entry), |
854 | hash_cmp); | |
855 | else | |
856 | qsort(fd.harray, fd.num_array, sizeof(struct hash_entry), | |
857 | hash_cmp); | |
850d05e9 | 858 | |
b0700a1b TT |
859 | /* |
860 | * Look for duplicates | |
861 | */ | |
862 | if (duplicate_search_and_fix(ctx, fs, ino, &fd)) | |
863 | goto resort; | |
864 | ||
1d2eef42 TT |
865 | if (ctx->options & E2F_OPT_NO) { |
866 | retval = 0; | |
867 | goto errout; | |
868 | } | |
869 | ||
b71e0183 | 870 | /* Sort non-hashed directories by inode number */ |
c23b2cc4 | 871 | if (fd.compress && fd.num_array > 1) |
b71e0183 TT |
872 | qsort(fd.harray+2, fd.num_array-2, |
873 | sizeof(struct hash_entry), ino_cmp); | |
874 | ||
850d05e9 TT |
875 | /* |
876 | * Copy the directory entries. In a htree directory these | |
877 | * will become the leaf nodes. | |
878 | */ | |
fd9ca825 | 879 | retval = copy_dir_entries(ctx, &fd, &outdir); |
b7a00563 TT |
880 | if (retval) |
881 | goto errout; | |
efc6f628 | 882 | |
b7a00563 TT |
883 | free(dir_buf); dir_buf = 0; |
884 | ||
850d05e9 TT |
885 | if (!fd.compress) { |
886 | /* Calculate the interior nodes */ | |
887 | retval = calculate_tree(fs, &outdir, ino, fd.parent); | |
888 | if (retval) | |
889 | goto errout; | |
b7a00563 | 890 | } |
efc6f628 | 891 | |
e228d700 | 892 | retval = write_directory(ctx, fs, &outdir, ino, &inode, fd.compress); |
b7a00563 TT |
893 | if (retval) |
894 | goto errout; | |
895 | ||
e228d700 DW |
896 | if (ctx->options & E2F_OPT_CONVERT_BMAP) |
897 | retval = e2fsck_rebuild_extents_later(ctx, ino); | |
898 | else | |
899 | retval = e2fsck_check_rebuild_extents(ctx, ino, &inode, pctx); | |
b7a00563 | 900 | errout: |
45e338f5 JM |
901 | free(dir_buf); |
902 | free(fd.harray); | |
850d05e9 | 903 | |
b7a00563 TT |
904 | free_out_dir(&outdir); |
905 | return retval; | |
906 | } | |
907 | ||
908 | void e2fsck_rehash_directories(e2fsck_t ctx) | |
909 | { | |
b7a00563 | 910 | struct problem_context pctx; |
850d05e9 TT |
911 | #ifdef RESOURCE_TRACK |
912 | struct resource_track rtrack; | |
913 | #endif | |
914 | struct dir_info *dir; | |
915 | ext2_u32_iterate iter; | |
28db82a8 | 916 | struct dir_info_iter * dirinfo_iter = 0; |
850d05e9 TT |
917 | ext2_ino_t ino; |
918 | errcode_t retval; | |
e3507739 | 919 | int cur, max, all_dirs, first = 1; |
850d05e9 | 920 | |
6d96b00d | 921 | init_resource_track(&rtrack, ctx->fs->io); |
850d05e9 TT |
922 | all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS; |
923 | ||
924 | if (!ctx->dirs_to_hash && !all_dirs) | |
b7a00563 TT |
925 | return; |
926 | ||
82372e32 TT |
927 | (void) e2fsck_get_lost_and_found(ctx, 0); |
928 | ||
b7a00563 | 929 | clear_problem_context(&pctx); |
850d05e9 | 930 | |
b0700a1b TT |
931 | cur = 0; |
932 | if (all_dirs) { | |
28db82a8 | 933 | dirinfo_iter = e2fsck_dir_info_iter_begin(ctx); |
b0700a1b TT |
934 | max = e2fsck_get_num_dirinfo(ctx); |
935 | } else { | |
efc6f628 | 936 | retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash, |
850d05e9 TT |
937 | &iter); |
938 | if (retval) { | |
939 | pctx.errcode = retval; | |
940 | fix_problem(ctx, PR_3A_OPTIMIZE_ITER, &pctx); | |
941 | return; | |
942 | } | |
b0700a1b | 943 | max = ext2fs_u32_list_count(ctx->dirs_to_hash); |
b7a00563 | 944 | } |
850d05e9 TT |
945 | while (1) { |
946 | if (all_dirs) { | |
efc6f628 | 947 | if ((dir = e2fsck_dir_info_iter(ctx, |
28db82a8 | 948 | dirinfo_iter)) == 0) |
850d05e9 TT |
949 | break; |
950 | ino = dir->ino; | |
951 | } else { | |
952 | if (!ext2fs_u32_list_iterate(iter, &ino)) | |
953 | break; | |
954 | } | |
07307114 | 955 | |
b7a00563 TT |
956 | pctx.dir = ino; |
957 | if (first) { | |
958 | fix_problem(ctx, PR_3A_PASS_HEADER, &pctx); | |
959 | first = 0; | |
960 | } | |
b0700a1b | 961 | #if 0 |
850d05e9 | 962 | fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx); |
b0700a1b | 963 | #endif |
e228d700 | 964 | pctx.errcode = e2fsck_rehash_dir(ctx, ino, &pctx); |
b7a00563 | 965 | if (pctx.errcode) { |
850d05e9 TT |
966 | end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR); |
967 | fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx); | |
b7a00563 | 968 | } |
52734dc5 TT |
969 | if (ctx->progress && !ctx->progress_fd) |
970 | e2fsck_simple_progress(ctx, "Rebuilding directory", | |
1d2eef42 | 971 | 100.0 * (float) (++cur) / (float) max, ino); |
b7a00563 | 972 | } |
850d05e9 | 973 | end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR); |
28db82a8 TT |
974 | if (all_dirs) |
975 | e2fsck_dir_info_iter_end(ctx, dirinfo_iter); | |
976 | else | |
850d05e9 | 977 | ext2fs_u32_list_iterate_end(iter); |
efc6f628 | 978 | |
850d05e9 TT |
979 | if (ctx->dirs_to_hash) |
980 | ext2fs_u32_list_free(ctx->dirs_to_hash); | |
b7a00563 | 981 | ctx->dirs_to_hash = 0; |
850d05e9 | 982 | |
9facd076 | 983 | print_resource_track(ctx, "Pass 3A", &rtrack, ctx->fs->io); |
b7a00563 | 984 | } |