• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "config.h"
49 #include <string.h>
50 #include <ctype.h>
51 #include <errno.h>
52 #include "e2fsck.h"
53 #include "problem.h"
54 
55 /* Schedule a dir to be rebuilt during pass 3A. */
e2fsck_rehash_dir_later(e2fsck_t ctx,ext2_ino_t ino)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. */
e2fsck_dir_will_be_rehashed(e2fsck_t ctx,ext2_ino_t ino)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 
74 #undef REHASH_DEBUG
75 
76 struct fill_dir_struct {
77 	char *buf;
78 	struct ext2_inode *inode;
79 	ext2_ino_t ino;
80 	errcode_t err;
81 	e2fsck_t ctx;
82 	struct hash_entry *harray;
83 	int max_array, num_array;
84 	unsigned int dir_size;
85 	int compress;
86 	ino_t parent;
87 	ext2_ino_t dir;
88 };
89 
90 struct hash_entry {
91 	ext2_dirhash_t	hash;
92 	ext2_dirhash_t	minor_hash;
93 	ino_t		ino;
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 #define DOTDOT_OFFSET 12
105 
is_fake_entry(ext2_filsys fs,int lblk,unsigned int offset)106 static int is_fake_entry(ext2_filsys fs, int lblk, unsigned int offset)
107 {
108 	/* Entries in the first block before this value refer to . or .. */
109 	if (lblk == 0 && offset <= DOTDOT_OFFSET)
110 		return 1;
111 	/* Check if this is likely the csum entry */
112 	if (ext2fs_has_feature_metadata_csum(fs->super) &&
113 	    (offset & (fs->blocksize - 1)) ==
114 			    fs->blocksize - sizeof(struct ext2_dir_entry_tail))
115 		return 1;
116 	return 0;
117 }
118 
fill_dir_block(ext2_filsys fs,blk64_t * block_nr,e2_blkcnt_t blockcnt,blk64_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)119 static int fill_dir_block(ext2_filsys fs,
120 			  blk64_t *block_nr,
121 			  e2_blkcnt_t blockcnt,
122 			  blk64_t ref_block EXT2FS_ATTR((unused)),
123 			  int ref_offset EXT2FS_ATTR((unused)),
124 			  void *priv_data)
125 {
126 	struct fill_dir_struct	*fd = (struct fill_dir_struct *) priv_data;
127 	struct hash_entry 	*new_array, *ent;
128 	struct ext2_dir_entry 	*dirent;
129 	char			*dir;
130 	unsigned int		offset, dir_offset, rec_len, name_len;
131 	int			hash_alg, hash_flags, hash_in_entry;
132 
133 	if (blockcnt < 0)
134 		return 0;
135 
136 	offset = blockcnt * fs->blocksize;
137 	if (offset + fs->blocksize > fd->inode->i_size) {
138 		fd->err = EXT2_ET_DIR_CORRUPTED;
139 		return BLOCK_ABORT;
140 	}
141 
142 	dir = (fd->buf+offset);
143 	if (*block_nr == 0) {
144 		memset(dir, 0, fs->blocksize);
145 		dirent = (struct ext2_dir_entry *) dir;
146 		(void) ext2fs_set_rec_len(fs, fs->blocksize, dirent);
147 	} else {
148 		int flags = fs->flags;
149 		fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
150 		fd->err = ext2fs_read_dir_block4(fs, *block_nr, dir, 0,
151 						 fd->dir);
152 		fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
153 			    (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
154 		if (fd->err)
155 			return BLOCK_ABORT;
156 	}
157 	hash_flags = fd->inode->i_flags & EXT4_CASEFOLD_FL;
158 	hash_in_entry = ext4_hash_in_dirent(fd->inode);
159 	hash_alg = fs->super->s_def_hash_version;
160 	if ((hash_alg <= EXT2_HASH_TEA) &&
161 	    (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
162 		hash_alg += 3;
163 	/* While the directory block is "hot", index it. */
164 	dir_offset = 0;
165 	while (dir_offset < fs->blocksize) {
166 		int min_rec = EXT2_DIR_ENTRY_HEADER_LEN;
167 		int extended = hash_in_entry && !is_fake_entry(fs, blockcnt, dir_offset);
168 
169 		if (extended)
170 			min_rec += EXT2_DIR_ENTRY_HASH_LEN;
171 		dirent = (struct ext2_dir_entry *) (dir + dir_offset);
172 		(void) ext2fs_get_rec_len(fs, dirent, &rec_len);
173 		name_len = ext2fs_dirent_name_len(dirent);
174 		if (((dir_offset + rec_len) > fs->blocksize) ||
175 		    (rec_len < min_rec) ||
176 		    ((rec_len % 4) != 0) ||
177 		    (name_len + min_rec > rec_len)) {
178 			fd->err = EXT2_ET_DIR_CORRUPTED;
179 			return BLOCK_ABORT;
180 		}
181 		dir_offset += rec_len;
182 		if (dirent->inode == 0)
183 			continue;
184 		if (!fd->compress && (name_len == 1) &&
185 		    (dirent->name[0] == '.'))
186 			continue;
187 		if (!fd->compress && (name_len == 2) &&
188 		    (dirent->name[0] == '.') && (dirent->name[1] == '.')) {
189 			fd->parent = dirent->inode;
190 			continue;
191 		}
192 		if (fd->num_array >= fd->max_array) {
193 			new_array = realloc(fd->harray,
194 			    sizeof(struct hash_entry) * (fd->max_array+500));
195 			if (!new_array) {
196 				fd->err = ENOMEM;
197 				return BLOCK_ABORT;
198 			}
199 			fd->harray = new_array;
200 			fd->max_array += 500;
201 		}
202 		ent = fd->harray + fd->num_array++;
203 		ent->dir = dirent;
204 		fd->dir_size += ext2fs_dir_rec_len(name_len, extended);
205 		ent->ino = dirent->inode;
206 		if (extended) {
207 			ent->hash = EXT2_DIRENT_HASH(dirent);
208 			ent->minor_hash = EXT2_DIRENT_MINOR_HASH(dirent);
209 		} else if (fd->compress) {
210 			ent->hash = ent->minor_hash = 0;
211 		} else {
212 			fd->err = ext2fs_dirhash2(hash_alg,
213 						  dirent->name, name_len,
214 						  fs->encoding, hash_flags,
215 						  fs->super->s_hash_seed,
216 						  &ent->hash, &ent->minor_hash);
217 			if (fd->err)
218 				return BLOCK_ABORT;
219 		}
220 	}
221 
222 	return 0;
223 }
224 
225 /* Used for sorting the hash entry */
ino_cmp(const void * a,const void * b)226 static EXT2_QSORT_TYPE ino_cmp(const void *a, const void *b)
227 {
228 	const struct hash_entry *he_a = (const struct hash_entry *) a;
229 	const struct hash_entry *he_b = (const struct hash_entry *) b;
230 
231 	return (he_a->ino - he_b->ino);
232 }
233 
234 /* Used for sorting the hash entry */
name_cmp(const void * a,const void * b)235 static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
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 	unsigned int he_a_len, he_b_len, min_len;
240 	int	ret;
241 
242 	he_a_len = ext2fs_dirent_name_len(he_a->dir);
243 	he_b_len = ext2fs_dirent_name_len(he_b->dir);
244 	min_len = he_a_len;
245 	if (min_len > he_b_len)
246 		min_len = he_b_len;
247 
248 	ret = memcmp(he_a->dir->name, he_b->dir->name, min_len);
249 	if (ret == 0) {
250 		if (he_a_len > he_b_len)
251 			ret = 1;
252 		else if (he_a_len < he_b_len)
253 			ret = -1;
254 		else
255 			ret = he_b->dir->inode - he_a->dir->inode;
256 	}
257 	return ret;
258 }
259 
260 /* Used for sorting the hash entry */
hash_cmp(const void * a,const void * b)261 static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b)
262 {
263 	const struct hash_entry *he_a = (const struct hash_entry *) a;
264 	const struct hash_entry *he_b = (const struct hash_entry *) b;
265 	int	ret;
266 
267 	if (he_a->hash > he_b->hash)
268 		ret = 1;
269 	else if (he_a->hash < he_b->hash)
270 		ret = -1;
271 	else {
272 		if (he_a->minor_hash > he_b->minor_hash)
273 			ret = 1;
274 		else if (he_a->minor_hash < he_b->minor_hash)
275 			ret = -1;
276 		else
277 			ret = name_cmp(a, b);
278 	}
279 	return ret;
280 }
281 
alloc_size_dir(ext2_filsys fs,struct out_dir * outdir,int blocks)282 static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir,
283 				int blocks)
284 {
285 	void			*new_mem;
286 
287 	if (outdir->max) {
288 		new_mem = realloc(outdir->buf, blocks * fs->blocksize);
289 		if (!new_mem)
290 			return ENOMEM;
291 		outdir->buf = new_mem;
292 		new_mem = realloc(outdir->hashes,
293 				  blocks * sizeof(ext2_dirhash_t));
294 		if (!new_mem)
295 			return ENOMEM;
296 		outdir->hashes = new_mem;
297 	} else {
298 		outdir->buf = malloc(blocks * fs->blocksize);
299 		outdir->hashes = malloc(blocks * sizeof(ext2_dirhash_t));
300 		outdir->num = 0;
301 	}
302 	outdir->max = blocks;
303 	return 0;
304 }
305 
free_out_dir(struct out_dir * outdir)306 static void free_out_dir(struct out_dir *outdir)
307 {
308 	free(outdir->buf);
309 	free(outdir->hashes);
310 	outdir->max = 0;
311 	outdir->num =0;
312 }
313 
get_next_block(ext2_filsys fs,struct out_dir * outdir,char ** ret)314 static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir,
315 			 char ** ret)
316 {
317 	errcode_t	retval;
318 
319 	if (outdir->num >= outdir->max) {
320 		retval = alloc_size_dir(fs, outdir, outdir->max + 50);
321 		if (retval)
322 			return retval;
323 	}
324 	*ret = outdir->buf + (outdir->num++ * fs->blocksize);
325 	memset(*ret, 0, fs->blocksize);
326 	return 0;
327 }
328 
329 /*
330  * This function is used to make a unique filename.  We do this by
331  * appending ~0, and then incrementing the number.  However, we cannot
332  * expand the length of the filename beyond the padding available in
333  * the directory entry.
334  */
mutate_name(char * str,unsigned int * len)335 static void mutate_name(char *str, unsigned int *len)
336 {
337 	int i;
338 	unsigned int l = *len;
339 
340 	/*
341 	 * First check to see if it looks the name has been mutated
342 	 * already
343 	 */
344 	for (i = l-1; i > 0; i--) {
345 		if (!isdigit(str[i]))
346 			break;
347 	}
348 	if ((i == (int)l - 1) || (str[i] != '~')) {
349 		if (((l-1) & 3) < 2)
350 			l += 2;
351 		else
352 			l = (l+3) & ~3;
353 		str[l-2] = '~';
354 		str[l-1] = '0';
355 		*len = l;
356 		return;
357 	}
358 	for (i = l-1; i >= 0; i--) {
359 		if (isdigit(str[i])) {
360 			if (str[i] == '9')
361 				str[i] = '0';
362 			else {
363 				str[i]++;
364 				return;
365 			}
366 			continue;
367 		}
368 		if (i == 1) {
369 			if (str[0] == 'z')
370 				str[0] = 'A';
371 			else if (str[0] == 'Z') {
372 				str[0] = '~';
373 				str[1] = '0';
374 			} else
375 				str[0]++;
376 		} else if (i > 0) {
377 			str[i] = '1';
378 			str[i-1] = '~';
379 		} else {
380 			if (str[0] == '~')
381 				str[0] = 'a';
382 			else
383 				str[0]++;
384 		}
385 		break;
386 	}
387 }
388 
duplicate_search_and_fix(e2fsck_t ctx,ext2_filsys fs,ext2_ino_t ino,struct fill_dir_struct * fd)389 static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
390 				    ext2_ino_t ino,
391 				    struct fill_dir_struct *fd)
392 {
393 	struct problem_context	pctx;
394 	struct hash_entry 	*ent, *prev;
395 	int			i, j;
396 	int			fixed = 0;
397 	char			new_name[256];
398 	unsigned int		new_len;
399 	int			hash_alg;
400 	int hash_flags = fd->inode->i_flags & EXT4_CASEFOLD_FL;
401 
402 	clear_problem_context(&pctx);
403 	pctx.ino = ino;
404 
405 	hash_alg = fs->super->s_def_hash_version;
406 	if ((hash_alg <= EXT2_HASH_TEA) &&
407 	    (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
408 		hash_alg += 3;
409 
410 	for (i=1; i < fd->num_array; i++) {
411 		ent = fd->harray + i;
412 		prev = ent - 1;
413 		if (!ent->dir->inode ||
414 		    (ext2fs_dirent_name_len(ent->dir) !=
415 		     ext2fs_dirent_name_len(prev->dir)) ||
416 		    memcmp(ent->dir->name, prev->dir->name,
417 			     ext2fs_dirent_name_len(ent->dir)))
418 			continue;
419 		pctx.dirent = ent->dir;
420 		if ((ent->dir->inode == prev->dir->inode) &&
421 		    fix_problem(ctx, PR_2_DUPLICATE_DIRENT, &pctx)) {
422 			e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1);
423 			ent->dir->inode = 0;
424 			fixed++;
425 			continue;
426 		}
427 		/* Can't alter encrypted name without key, so just drop it */
428 		if (fd->inode->i_flags & EXT4_ENCRYPT_FL) {
429 			if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE_NO_RENAME, &pctx)) {
430 				e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1);
431 				ent->dir->inode = 0;
432 				fixed++;
433 				continue;
434 			}
435 		}
436 		new_len = ext2fs_dirent_name_len(ent->dir);
437 		memcpy(new_name, ent->dir->name, new_len);
438 		mutate_name(new_name, &new_len);
439 		for (j=0; j < fd->num_array; j++) {
440 			if ((i==j) ||
441 			    (new_len !=
442 			     (unsigned) ext2fs_dirent_name_len(fd->harray[j].dir)) ||
443 			    memcmp(new_name, fd->harray[j].dir->name, new_len))
444 				continue;
445 			mutate_name(new_name, &new_len);
446 
447 			j = -1;
448 		}
449 		new_name[new_len] = 0;
450 		pctx.str = new_name;
451 		if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) {
452 			memcpy(ent->dir->name, new_name, new_len);
453 			ext2fs_dirent_set_name_len(ent->dir, new_len);
454 			ext2fs_dirhash2(hash_alg, new_name, new_len,
455 					fs->encoding, hash_flags,
456 					fs->super->s_hash_seed,
457 					&ent->hash, &ent->minor_hash);
458 			fixed++;
459 		}
460 	}
461 	return fixed;
462 }
463 
464 
copy_dir_entries(e2fsck_t ctx,struct fill_dir_struct * fd,struct out_dir * outdir)465 static errcode_t copy_dir_entries(e2fsck_t ctx,
466 				  struct fill_dir_struct *fd,
467 				  struct out_dir *outdir)
468 {
469 	ext2_filsys 		fs = ctx->fs;
470 	errcode_t		retval;
471 	char			*block_start;
472 	struct hash_entry 	*ent;
473 	struct ext2_dir_entry	*dirent;
474 	unsigned int		rec_len, prev_rec_len, left, slack, offset;
475 	int			i;
476 	ext2_dirhash_t		prev_hash;
477 	int			csum_size = 0;
478 	struct			ext2_dir_entry_tail *t;
479 	int hash_in_entry = ext4_hash_in_dirent(fd->inode);
480 	int min_rec_len = ext2fs_dir_rec_len(1, hash_in_entry);
481 
482 	if (ctx->htree_slack_percentage == 255) {
483 		profile_get_uint(ctx->profile, "options",
484 				 "indexed_dir_slack_percentage",
485 				 0, 20,
486 				 &ctx->htree_slack_percentage);
487 		if (ctx->htree_slack_percentage > 100)
488 			ctx->htree_slack_percentage = 20;
489 	}
490 
491 	if (ext2fs_has_feature_metadata_csum(fs->super))
492 		csum_size = sizeof(struct ext2_dir_entry_tail);
493 
494 	outdir->max = 0;
495 	retval = alloc_size_dir(fs, outdir,
496 				(fd->dir_size / fs->blocksize) + 2);
497 	if (retval)
498 		return retval;
499 	outdir->num = fd->compress ? 0 : 1;
500 	offset = 0;
501 	outdir->hashes[0] = 0;
502 	prev_hash = 1;
503 	if ((retval = get_next_block(fs, outdir, &block_start)))
504 		return retval;
505 	dirent = (struct ext2_dir_entry *) block_start;
506 	prev_rec_len = 0;
507 	rec_len = 0;
508 	left = fs->blocksize - csum_size;
509 	slack = fd->compress ? min_rec_len :
510 		((fs->blocksize - csum_size) * ctx->htree_slack_percentage)/100;
511 	if (slack < min_rec_len)
512 		slack = min_rec_len;
513 	for (i = 0; i < fd->num_array; i++) {
514 		ent = fd->harray + i;
515 		if (ent->dir->inode == 0)
516 			continue;
517 		rec_len = ext2fs_dir_rec_len(ext2fs_dirent_name_len(ent->dir),
518 					     hash_in_entry);
519 		if (rec_len > left) {
520 			if (left) {
521 				left += prev_rec_len;
522 				retval = ext2fs_set_rec_len(fs, left, dirent);
523 				if (retval)
524 					return retval;
525 			}
526 			if (csum_size) {
527 				t = EXT2_DIRENT_TAIL(block_start,
528 						     fs->blocksize);
529 				ext2fs_initialize_dirent_tail(fs, t);
530 			}
531 			if ((retval = get_next_block(fs, outdir,
532 						      &block_start)))
533 				return retval;
534 			offset = 0;
535 		}
536 		left = (fs->blocksize - csum_size) - offset;
537 		dirent = (struct ext2_dir_entry *) (block_start + offset);
538 		if (offset == 0) {
539 			if (ent->hash == prev_hash)
540 				outdir->hashes[outdir->num-1] = ent->hash | 1;
541 			else
542 				outdir->hashes[outdir->num-1] = ent->hash;
543 		}
544 		dirent->inode = ent->dir->inode;
545 		ext2fs_dirent_set_name_len(dirent,
546 					   ext2fs_dirent_name_len(ent->dir));
547 		ext2fs_dirent_set_file_type(dirent,
548 					    ext2fs_dirent_file_type(ent->dir));
549 		retval = ext2fs_set_rec_len(fs, rec_len, dirent);
550 		if (retval)
551 			return retval;
552 		prev_rec_len = rec_len;
553 		memcpy(dirent->name, ent->dir->name,
554 		       ext2fs_dirent_name_len(dirent));
555 		if (hash_in_entry) {
556 			EXT2_DIRENT_HASHES(dirent)->hash = ext2fs_cpu_to_le32(ent->hash);
557 			EXT2_DIRENT_HASHES(dirent)->minor_hash =
558 							ext2fs_cpu_to_le32(ent->minor_hash);
559 		}
560 		offset += rec_len;
561 		left -= rec_len;
562 		if (left < slack) {
563 			prev_rec_len += left;
564 			retval = ext2fs_set_rec_len(fs, prev_rec_len, dirent);
565 			if (retval)
566 				return retval;
567 			offset += left;
568 			left = 0;
569 		}
570 		prev_hash = ent->hash;
571 	}
572 	if (left)
573 		retval = ext2fs_set_rec_len(fs, rec_len + left, dirent);
574 	if (csum_size) {
575 		t = EXT2_DIRENT_TAIL(block_start, fs->blocksize);
576 		ext2fs_initialize_dirent_tail(fs, t);
577 	}
578 
579 	return retval;
580 }
581 
582 
set_root_node(ext2_filsys fs,char * buf,ext2_ino_t ino,ext2_ino_t parent,struct ext2_inode * inode)583 static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
584 				    ext2_ino_t ino, ext2_ino_t parent,
585 				    struct ext2_inode *inode)
586 {
587 	struct ext2_dir_entry 		*dir;
588 	struct ext2_dx_root_info  	*root;
589 	struct ext2_dx_countlimit	*limits;
590 	int				filetype = 0;
591 	int				csum_size = 0;
592 
593 	if (ext2fs_has_feature_filetype(fs->super))
594 		filetype = EXT2_FT_DIR;
595 
596 	memset(buf, 0, fs->blocksize);
597 	dir = (struct ext2_dir_entry *) buf;
598 	dir->inode = ino;
599 	dir->name[0] = '.';
600 	ext2fs_dirent_set_name_len(dir, 1);
601 	ext2fs_dirent_set_file_type(dir, filetype);
602 	dir->rec_len = 12;
603 	dir = (struct ext2_dir_entry *) (buf + 12);
604 	dir->inode = parent;
605 	dir->name[0] = '.';
606 	dir->name[1] = '.';
607 	ext2fs_dirent_set_name_len(dir, 2);
608 	ext2fs_dirent_set_file_type(dir, filetype);
609 	dir->rec_len = fs->blocksize - 12;
610 
611 	root = (struct ext2_dx_root_info *) (buf+24);
612 	root->reserved_zero = 0;
613 	if (ext4_hash_in_dirent(inode))
614 		root->hash_version = EXT2_HASH_SIPHASH;
615 	else
616 		root->hash_version = fs->super->s_def_hash_version;
617 	root->info_length = 8;
618 	root->indirect_levels = 0;
619 	root->unused_flags = 0;
620 
621 	if (ext2fs_has_feature_metadata_csum(fs->super))
622 		csum_size = sizeof(struct ext2_dx_tail);
623 
624 	limits = (struct ext2_dx_countlimit *) (buf+32);
625 	limits->limit = (fs->blocksize - (32 + csum_size)) /
626 			sizeof(struct ext2_dx_entry);
627 	limits->count = 0;
628 
629 	return root;
630 }
631 
632 
set_int_node(ext2_filsys fs,char * buf)633 static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf)
634 {
635 	struct ext2_dir_entry 		*dir;
636 	struct ext2_dx_countlimit	*limits;
637 	int				csum_size = 0;
638 
639 	memset(buf, 0, fs->blocksize);
640 	dir = (struct ext2_dir_entry *) buf;
641 	dir->inode = 0;
642 	(void) ext2fs_set_rec_len(fs, fs->blocksize, dir);
643 
644 	if (ext2fs_has_feature_metadata_csum(fs->super))
645 		csum_size = sizeof(struct ext2_dx_tail);
646 
647 	limits = (struct ext2_dx_countlimit *) (buf+8);
648 	limits->limit = (fs->blocksize - (8 + csum_size)) /
649 			sizeof(struct ext2_dx_entry);
650 	limits->count = 0;
651 
652 	return (struct ext2_dx_entry *) limits;
653 }
654 
alloc_blocks(ext2_filsys fs,struct ext2_dx_countlimit ** limit,struct ext2_dx_entry ** prev_ent,struct ext2_dx_entry ** next_ent,int * prev_offset,int * next_offset,struct out_dir * outdir,int i,int * prev_count,int * next_count)655 static int alloc_blocks(ext2_filsys fs,
656 			struct ext2_dx_countlimit **limit,
657 			struct ext2_dx_entry **prev_ent,
658 			struct ext2_dx_entry **next_ent,
659 			int *prev_offset, int *next_offset,
660 			struct out_dir *outdir, int i,
661 			int *prev_count, int *next_count)
662 {
663 	errcode_t	retval;
664 	char		*block_start;
665 
666 	if (*limit)
667 		(*limit)->limit = (*limit)->count =
668 			ext2fs_cpu_to_le16((*limit)->limit);
669 	*prev_ent = (struct ext2_dx_entry *) (outdir->buf + *prev_offset);
670 	(*prev_ent)->block = ext2fs_cpu_to_le32(outdir->num);
671 
672 	if (i != 1)
673 		(*prev_ent)->hash =
674 			ext2fs_cpu_to_le32(outdir->hashes[i]);
675 
676 	retval = get_next_block(fs, outdir, &block_start);
677 	if (retval)
678 		return retval;
679 
680 	*next_ent = set_int_node(fs, block_start);
681 	*limit = (struct ext2_dx_countlimit *)(*next_ent);
682 	if (next_offset)
683 		*next_offset = ((char *) *next_ent - outdir->buf);
684 
685 	*next_count = (*limit)->limit;
686 	(*prev_offset) += sizeof(struct ext2_dx_entry);
687 	(*prev_count)--;
688 
689 	return 0;
690 }
691 
692 /*
693  * This function takes the leaf nodes which have been written in
694  * outdir, and populates the root node and any necessary interior nodes.
695  */
calculate_tree(ext2_filsys fs,struct out_dir * outdir,ext2_ino_t ino,ext2_ino_t parent,struct ext2_inode * inode)696 static errcode_t calculate_tree(ext2_filsys fs,
697 				struct out_dir *outdir,
698 				ext2_ino_t ino,
699 				ext2_ino_t parent,
700 				struct ext2_inode *inode)
701 {
702 	struct ext2_dx_root_info	*root_info;
703 	struct ext2_dx_entry		*root, *int_ent, *dx_ent = 0;
704 	struct ext2_dx_countlimit	*root_limit, *int_limit, *limit;
705 	errcode_t			retval;
706 	int				i, c1, c2, c3, nblks;
707 	int				limit_offset, int_offset, root_offset;
708 
709 	root_info = set_root_node(fs, outdir->buf, ino, parent, inode);
710 	root_offset = limit_offset = ((char *) root_info - outdir->buf) +
711 		root_info->info_length;
712 	root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
713 	c1 = root_limit->limit;
714 	nblks = outdir->num;
715 
716 	/* Write out the pointer blocks */
717 	if (nblks - 1 <= c1) {
718 		/* Just write out the root block, and we're done */
719 		root = (struct ext2_dx_entry *) (outdir->buf + root_offset);
720 		for (i=1; i < nblks; i++) {
721 			root->block = ext2fs_cpu_to_le32(i);
722 			if (i != 1)
723 				root->hash =
724 					ext2fs_cpu_to_le32(outdir->hashes[i]);
725 			root++;
726 			c1--;
727 		}
728 	} else if (nblks - 1 <= ext2fs_htree_intnode_maxrecs(fs, c1)) {
729 		c2 = 0;
730 		limit = NULL;
731 		root_info->indirect_levels = 1;
732 		for (i=1; i < nblks; i++) {
733 			if (c2 == 0 && c1 == 0)
734 				return ENOSPC;
735 			if (c2 == 0) {
736 				retval = alloc_blocks(fs, &limit, &root,
737 						      &dx_ent, &root_offset,
738 						      NULL, outdir, i, &c1,
739 						      &c2);
740 				if (retval)
741 					return retval;
742 			}
743 			dx_ent->block = ext2fs_cpu_to_le32(i);
744 			if (c2 != limit->limit)
745 				dx_ent->hash =
746 					ext2fs_cpu_to_le32(outdir->hashes[i]);
747 			dx_ent++;
748 			c2--;
749 		}
750 		limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
751 		limit->limit = ext2fs_cpu_to_le16(limit->limit);
752 	} else {
753 		c2 = 0;
754 		c3 = 0;
755 		limit = NULL;
756 		int_limit = 0;
757 		root_info->indirect_levels = 2;
758 		for (i = 1; i < nblks; i++) {
759 			if (c3 == 0 && c2 == 0 && c1 == 0)
760 				return ENOSPC;
761 			if (c3 == 0 && c2 == 0) {
762 				retval = alloc_blocks(fs, &int_limit, &root,
763 						      &int_ent, &root_offset,
764 						      &int_offset, outdir, i,
765 						      &c1, &c2);
766 				if (retval)
767 					return retval;
768 			}
769 			if (c3 == 0) {
770 				retval = alloc_blocks(fs, &limit, &int_ent,
771 						      &dx_ent, &int_offset,
772 						      NULL, outdir, i, &c2,
773 						      &c3);
774 				if (retval)
775 					return retval;
776 
777 			}
778 			dx_ent->block = ext2fs_cpu_to_le32(i);
779 			if (c3 != limit->limit)
780 				dx_ent->hash =
781 					ext2fs_cpu_to_le32(outdir->hashes[i]);
782 			dx_ent++;
783 			c3--;
784 		}
785 		int_limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
786 		int_limit->limit = ext2fs_cpu_to_le16(limit->limit);
787 
788 		limit->count = ext2fs_cpu_to_le16(limit->limit - c3);
789 		limit->limit = ext2fs_cpu_to_le16(limit->limit);
790 
791 	}
792 	root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
793 	root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1);
794 	root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit);
795 
796 	return 0;
797 }
798 
799 struct write_dir_struct {
800 	struct out_dir *outdir;
801 	errcode_t	err;
802 	ext2_ino_t	ino;
803 	e2fsck_t	ctx;
804 	ext2_ino_t	dir;
805 };
806 
807 /*
808  * Helper function which writes out a directory block.
809  */
write_dir_block(ext2_filsys fs,blk64_t * block_nr,e2_blkcnt_t blockcnt,blk64_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)810 static int write_dir_block(ext2_filsys fs,
811 			   blk64_t *block_nr,
812 			   e2_blkcnt_t blockcnt,
813 			   blk64_t ref_block EXT2FS_ATTR((unused)),
814 			   int ref_offset EXT2FS_ATTR((unused)),
815 			   void *priv_data)
816 {
817 	struct write_dir_struct	*wd = (struct write_dir_struct *) priv_data;
818 	char	*dir, *buf = 0;
819 
820 #ifdef REHASH_DEBUG
821 	printf("%u: write_dir_block %lld:%lld", wd->ino, blockcnt, *block_nr);
822 #endif
823 	if ((*block_nr == 0) || (blockcnt < 0)) {
824 #ifdef REHASH_DEBUG
825 		printf(" - skip\n");
826 #endif
827 		return 0;
828 	}
829 	if (blockcnt < wd->outdir->num)
830 		dir = wd->outdir->buf + (blockcnt * fs->blocksize);
831 	else if (wd->ctx->lost_and_found == wd->dir) {
832 		/* Don't release any extra directory blocks for lost+found */
833 		wd->err = ext2fs_new_dir_block(fs, 0, 0, &buf);
834 		if (wd->err)
835 			return BLOCK_ABORT;
836 		dir = buf;
837 		wd->outdir->num++;
838 	} else {
839 		/* Don't free blocks at the end of the directory, they
840 		 * will be truncated by the caller. */
841 #ifdef REHASH_DEBUG
842 		printf(" - not freed\n");
843 #endif
844 		return 0;
845 	}
846 	wd->err = ext2fs_write_dir_block4(fs, *block_nr, dir, 0, wd->dir);
847 	if (buf)
848 		ext2fs_free_mem(&buf);
849 
850 #ifdef REHASH_DEBUG
851 	printf(" - write (%d)\n", wd->err);
852 #endif
853 	if (wd->err)
854 		return BLOCK_ABORT;
855 	return 0;
856 }
857 
write_directory(e2fsck_t ctx,ext2_filsys fs,struct out_dir * outdir,ext2_ino_t ino,struct ext2_inode * inode,int compress)858 static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs,
859 				 struct out_dir *outdir,
860 				 ext2_ino_t ino, struct ext2_inode *inode,
861 				 int compress)
862 {
863 	struct write_dir_struct wd;
864 	errcode_t	retval;
865 
866 	retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num);
867 	if (retval)
868 		return retval;
869 
870 	wd.outdir = outdir;
871 	wd.err = 0;
872 	wd.ino = ino;
873 	wd.ctx = ctx;
874 	wd.dir = ino;
875 
876 	retval = ext2fs_block_iterate3(fs, ino, 0, NULL,
877 				       write_dir_block, &wd);
878 	if (retval)
879 		return retval;
880 	if (wd.err)
881 		return wd.err;
882 
883 	e2fsck_read_inode(ctx, ino, inode, "rehash_dir");
884 	if (compress)
885 		inode->i_flags &= ~EXT2_INDEX_FL;
886 	else
887 		inode->i_flags |= EXT2_INDEX_FL;
888 #ifdef REHASH_DEBUG
889 	printf("%u: set inode size to %u blocks = %u bytes\n",
890 	       ino, outdir->num, outdir->num * fs->blocksize);
891 #endif
892 	retval = ext2fs_inode_size_set(fs, inode, (ext2_off64_t)outdir->num *
893 						   fs->blocksize);
894 	if (retval)
895 		return retval;
896 
897 	/* ext2fs_punch() calls ext2fs_write_inode() which writes the size */
898 	return ext2fs_punch(fs, ino, inode, NULL, outdir->num, ~0ULL);
899 }
900 
e2fsck_rehash_dir(e2fsck_t ctx,ext2_ino_t ino,struct problem_context * pctx)901 errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino,
902 			    struct problem_context *pctx)
903 {
904 	ext2_filsys 		fs = ctx->fs;
905 	errcode_t		retval;
906 	struct ext2_inode 	inode;
907 	char			*dir_buf = 0;
908 	struct fill_dir_struct	fd = { NULL, NULL, 0, 0, 0, NULL,
909 				       0, 0, 0, 0, 0, 0 };
910 	struct out_dir		outdir = { 0, 0, 0, 0 };
911 
912 	e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
913 
914 	if (ext2fs_has_feature_inline_data(fs->super) &&
915 	   (inode.i_flags & EXT4_INLINE_DATA_FL))
916 		return 0;
917 
918 	retval = ENOMEM;
919 	dir_buf = malloc(inode.i_size);
920 	if (!dir_buf)
921 		goto errout;
922 
923 	fd.max_array = inode.i_size / 32;
924 	fd.harray = malloc(fd.max_array * sizeof(struct hash_entry));
925 	if (!fd.harray)
926 		goto errout;
927 
928 	fd.ino = ino;
929 	fd.ctx = ctx;
930 	fd.buf = dir_buf;
931 	fd.inode = &inode;
932 	fd.dir = ino;
933 	if (!ext2fs_has_feature_dir_index(fs->super) ||
934 	    (inode.i_size / fs->blocksize) < 2)
935 		fd.compress = 1;
936 	fd.parent = 0;
937 
938 retry_nohash:
939 	/* Read in the entire directory into memory */
940 	retval = ext2fs_block_iterate3(fs, ino, 0, 0,
941 				       fill_dir_block, &fd);
942 	if (fd.err) {
943 		retval = fd.err;
944 		goto errout;
945 	}
946 
947 	/*
948 	 * If the entries read are less than a block, then don't index
949 	 * the directory
950 	 */
951 	if (!fd.compress && (fd.dir_size < (fs->blocksize - 24))) {
952 		fd.compress = 1;
953 		fd.dir_size = 0;
954 		fd.num_array = 0;
955 		goto retry_nohash;
956 	}
957 
958 #if 0
959 	printf("%d entries (%d bytes) found in inode %d\n",
960 	       fd.num_array, fd.dir_size, ino);
961 #endif
962 
963 	/* Sort the list */
964 resort:
965 	if (fd.compress && fd.num_array > 1)
966 		qsort(fd.harray+2, fd.num_array-2, sizeof(struct hash_entry),
967 		      hash_cmp);
968 	else
969 		qsort(fd.harray, fd.num_array, sizeof(struct hash_entry),
970 		      hash_cmp);
971 
972 	/*
973 	 * Look for duplicates
974 	 */
975 	if (duplicate_search_and_fix(ctx, fs, ino, &fd))
976 		goto resort;
977 
978 	if (ctx->options & E2F_OPT_NO) {
979 		retval = 0;
980 		goto errout;
981 	}
982 
983 	/* Sort non-hashed directories by inode number */
984 	if (fd.compress && fd.num_array > 1)
985 		qsort(fd.harray+2, fd.num_array-2,
986 		      sizeof(struct hash_entry), ino_cmp);
987 
988 	/*
989 	 * Copy the directory entries.  In a htree directory these
990 	 * will become the leaf nodes.
991 	 */
992 	retval = copy_dir_entries(ctx, &fd, &outdir);
993 	if (retval)
994 		goto errout;
995 
996 	free(dir_buf); dir_buf = 0;
997 
998 	if (!fd.compress) {
999 		/* Calculate the interior nodes */
1000 		retval = calculate_tree(fs, &outdir, ino, fd.parent, fd.inode);
1001 		if (retval)
1002 			goto errout;
1003 	}
1004 
1005 	retval = write_directory(ctx, fs, &outdir, ino, &inode, fd.compress);
1006 	if (retval)
1007 		goto errout;
1008 
1009 	if (ctx->options & E2F_OPT_CONVERT_BMAP)
1010 		retval = e2fsck_rebuild_extents_later(ctx, ino);
1011 	else
1012 		retval = e2fsck_check_rebuild_extents(ctx, ino, &inode, pctx);
1013 errout:
1014 	free(dir_buf);
1015 	free(fd.harray);
1016 
1017 	free_out_dir(&outdir);
1018 	return retval;
1019 }
1020 
e2fsck_rehash_directories(e2fsck_t ctx)1021 void e2fsck_rehash_directories(e2fsck_t ctx)
1022 {
1023 	struct problem_context	pctx;
1024 #ifdef RESOURCE_TRACK
1025 	struct resource_track	rtrack;
1026 #endif
1027 	struct dir_info		*dir;
1028 	ext2_u32_iterate 	iter;
1029 	struct dir_info_iter *	dirinfo_iter = 0;
1030 	ext2_ino_t		ino;
1031 	errcode_t		retval;
1032 	int			cur, max, all_dirs, first = 1;
1033 
1034 	init_resource_track(&rtrack, ctx->fs->io);
1035 	all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS;
1036 
1037 	if (!ctx->dirs_to_hash && !all_dirs)
1038 		return;
1039 
1040 	(void) e2fsck_get_lost_and_found(ctx, 0);
1041 
1042 	clear_problem_context(&pctx);
1043 
1044 	cur = 0;
1045 	if (all_dirs) {
1046 		dirinfo_iter = e2fsck_dir_info_iter_begin(ctx);
1047 		max = e2fsck_get_num_dirinfo(ctx);
1048 	} else {
1049 		retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash,
1050 						       &iter);
1051 		if (retval) {
1052 			pctx.errcode = retval;
1053 			fix_problem(ctx, PR_3A_OPTIMIZE_ITER, &pctx);
1054 			return;
1055 		}
1056 		max = ext2fs_u32_list_count(ctx->dirs_to_hash);
1057 	}
1058 	while (1) {
1059 		if (all_dirs) {
1060 			if ((dir = e2fsck_dir_info_iter(ctx,
1061 							dirinfo_iter)) == 0)
1062 				break;
1063 			ino = dir->ino;
1064 		} else {
1065 			if (!ext2fs_u32_list_iterate(iter, &ino))
1066 				break;
1067 		}
1068 
1069 		pctx.dir = ino;
1070 		if (first) {
1071 			fix_problem(ctx, PR_3A_PASS_HEADER, &pctx);
1072 			first = 0;
1073 		}
1074 #if 0
1075 		fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx);
1076 #endif
1077 		pctx.errcode = e2fsck_rehash_dir(ctx, ino, &pctx);
1078 		if (pctx.errcode) {
1079 			end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
1080 			fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx);
1081 		}
1082 		if (ctx->progress && !ctx->progress_fd)
1083 			e2fsck_simple_progress(ctx, "Rebuilding directory",
1084 			       100.0 * (float) (++cur) / (float) max, ino);
1085 	}
1086 	end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
1087 	if (all_dirs)
1088 		e2fsck_dir_info_iter_end(ctx, dirinfo_iter);
1089 	else
1090 		ext2fs_u32_list_iterate_end(iter);
1091 
1092 	if (ctx->dirs_to_hash)
1093 		ext2fs_u32_list_free(ctx->dirs_to_hash);
1094 	ctx->dirs_to_hash = 0;
1095 
1096 	print_resource_track(ctx, "Pass 3A", &rtrack, ctx->fs->io);
1097 }
1098