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