xref: /freebsd-src/sys/fs/ext2fs/ext2_htree.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
19824e4adSPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d63027b6SPedro F. Giffuni  *
49824e4adSPedro F. Giffuni  * Copyright (c) 2010, 2012 Zheng Liu <lz@freebsd.org>
59824e4adSPedro F. Giffuni  * Copyright (c) 2012, Vyacheslav Matyushin
69824e4adSPedro F. Giffuni  * All rights reserved.
79824e4adSPedro F. Giffuni  *
89824e4adSPedro F. Giffuni  * Redistribution and use in source and binary forms, with or without
99824e4adSPedro F. Giffuni  * modification, are permitted provided that the following conditions
109824e4adSPedro F. Giffuni  * are met:
119824e4adSPedro F. Giffuni  * 1. Redistributions of source code must retain the above copyright
129824e4adSPedro F. Giffuni  *    notice, this list of conditions and the following disclaimer.
139824e4adSPedro F. Giffuni  * 2. Redistributions in binary form must reproduce the above copyright
149824e4adSPedro F. Giffuni  *    notice, this list of conditions and the following disclaimer in the
159824e4adSPedro F. Giffuni  *    documentation and/or other materials provided with the distribution.
169824e4adSPedro F. Giffuni  *
179824e4adSPedro F. Giffuni  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189824e4adSPedro F. Giffuni  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199824e4adSPedro F. Giffuni  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209824e4adSPedro F. Giffuni  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219824e4adSPedro F. Giffuni  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229824e4adSPedro F. Giffuni  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239824e4adSPedro F. Giffuni  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249824e4adSPedro F. Giffuni  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259824e4adSPedro F. Giffuni  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269824e4adSPedro F. Giffuni  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279824e4adSPedro F. Giffuni  * SUCH DAMAGE.
289824e4adSPedro F. Giffuni  */
299824e4adSPedro F. Giffuni 
309824e4adSPedro F. Giffuni #include <sys/param.h>
319824e4adSPedro F. Giffuni #include <sys/endian.h>
329824e4adSPedro F. Giffuni #include <sys/systm.h>
339824e4adSPedro F. Giffuni #include <sys/namei.h>
349824e4adSPedro F. Giffuni #include <sys/bio.h>
359824e4adSPedro F. Giffuni #include <sys/buf.h>
369824e4adSPedro F. Giffuni #include <sys/endian.h>
379824e4adSPedro F. Giffuni #include <sys/mount.h>
389824e4adSPedro F. Giffuni #include <sys/vnode.h>
399824e4adSPedro F. Giffuni #include <sys/malloc.h>
409824e4adSPedro F. Giffuni #include <sys/dirent.h>
41ebc94b66SFedor Uporov #include <sys/sdt.h>
429824e4adSPedro F. Giffuni #include <sys/sysctl.h>
439824e4adSPedro F. Giffuni 
449824e4adSPedro F. Giffuni #include <ufs/ufs/dir.h>
459824e4adSPedro F. Giffuni 
46e06e5241SFedor Uporov #include <fs/ext2fs/fs.h>
479824e4adSPedro F. Giffuni #include <fs/ext2fs/inode.h>
489824e4adSPedro F. Giffuni #include <fs/ext2fs/ext2_mount.h>
499824e4adSPedro F. Giffuni #include <fs/ext2fs/ext2fs.h>
509824e4adSPedro F. Giffuni #include <fs/ext2fs/fs.h>
519824e4adSPedro F. Giffuni #include <fs/ext2fs/ext2_extern.h>
529824e4adSPedro F. Giffuni #include <fs/ext2fs/ext2_dinode.h>
539824e4adSPedro F. Giffuni #include <fs/ext2fs/ext2_dir.h>
549824e4adSPedro F. Giffuni #include <fs/ext2fs/htree.h>
559824e4adSPedro F. Giffuni 
56ebc94b66SFedor Uporov SDT_PROVIDER_DECLARE(ext2fs);
57ebc94b66SFedor Uporov /*
58ebc94b66SFedor Uporov  * ext2fs trace probe:
59ebc94b66SFedor Uporov  * arg0: verbosity. Higher numbers give more verbose messages
60ebc94b66SFedor Uporov  * arg1: Textual message
61ebc94b66SFedor Uporov  */
62ebc94b66SFedor Uporov SDT_PROBE_DEFINE2(ext2fs, , trace, htree, "int", "char*");
63ebc94b66SFedor Uporov 
649824e4adSPedro F. Giffuni static void	ext2_append_entry(char *block, uint32_t blksize,
659824e4adSPedro F. Giffuni 		    struct ext2fs_direct_2 *last_entry,
666d4a4ed7SFedor Uporov 		    struct ext2fs_direct_2 *new_entry, int csum_size);
679824e4adSPedro F. Giffuni static int	ext2_htree_append_block(struct vnode *vp, char *data,
689824e4adSPedro F. Giffuni 		    struct componentname *cnp, uint32_t blksize);
699824e4adSPedro F. Giffuni static int	ext2_htree_check_next(struct inode *ip, uint32_t hash,
709824e4adSPedro F. Giffuni 		    const char *name, struct ext2fs_htree_lookup_info *info);
719824e4adSPedro F. Giffuni static int	ext2_htree_cmp_sort_entry(const void *e1, const void *e2);
729824e4adSPedro F. Giffuni static int	ext2_htree_find_leaf(struct inode *ip, const char *name,
739824e4adSPedro F. Giffuni 		    int namelen, uint32_t *hash, uint8_t *hash_version,
749824e4adSPedro F. Giffuni 		    struct ext2fs_htree_lookup_info *info);
759824e4adSPedro F. Giffuni static uint32_t ext2_htree_get_block(struct ext2fs_htree_entry *ep);
769824e4adSPedro F. Giffuni static uint16_t	ext2_htree_get_count(struct ext2fs_htree_entry *ep);
779824e4adSPedro F. Giffuni static uint32_t ext2_htree_get_hash(struct ext2fs_htree_entry *ep);
789824e4adSPedro F. Giffuni static uint16_t	ext2_htree_get_limit(struct ext2fs_htree_entry *ep);
799824e4adSPedro F. Giffuni static void	ext2_htree_insert_entry_to_level(struct ext2fs_htree_lookup_level *level,
809824e4adSPedro F. Giffuni 		    uint32_t hash, uint32_t blk);
819824e4adSPedro F. Giffuni static void	ext2_htree_insert_entry(struct ext2fs_htree_lookup_info *info,
829824e4adSPedro F. Giffuni 		    uint32_t hash, uint32_t blk);
839824e4adSPedro F. Giffuni static uint32_t	ext2_htree_node_limit(struct inode *ip);
849824e4adSPedro F. Giffuni static void	ext2_htree_set_block(struct ext2fs_htree_entry *ep,
859824e4adSPedro F. Giffuni 		    uint32_t blk);
869824e4adSPedro F. Giffuni static void	ext2_htree_set_count(struct ext2fs_htree_entry *ep,
879824e4adSPedro F. Giffuni 		    uint16_t cnt);
889824e4adSPedro F. Giffuni static void	ext2_htree_set_hash(struct ext2fs_htree_entry *ep,
899824e4adSPedro F. Giffuni 		    uint32_t hash);
909824e4adSPedro F. Giffuni static void	ext2_htree_set_limit(struct ext2fs_htree_entry *ep,
919824e4adSPedro F. Giffuni 		    uint16_t limit);
926d4a4ed7SFedor Uporov static int	ext2_htree_split_dirblock(struct inode *ip,
936d4a4ed7SFedor Uporov 		    char *block1, char *block2, uint32_t blksize,
946d4a4ed7SFedor Uporov 		    uint32_t *hash_seed, uint8_t hash_version,
959824e4adSPedro F. Giffuni 		    uint32_t *split_hash, struct  ext2fs_direct_2 *entry);
969824e4adSPedro F. Giffuni static void	ext2_htree_release(struct ext2fs_htree_lookup_info *info);
979824e4adSPedro F. Giffuni static uint32_t	ext2_htree_root_limit(struct inode *ip, int len);
986d4a4ed7SFedor Uporov static int	ext2_htree_writebuf(struct inode *ip,
996d4a4ed7SFedor Uporov 		    struct ext2fs_htree_lookup_info *info);
1009824e4adSPedro F. Giffuni 
1019824e4adSPedro F. Giffuni int
ext2_htree_has_idx(struct inode * ip)1029824e4adSPedro F. Giffuni ext2_htree_has_idx(struct inode *ip)
1039824e4adSPedro F. Giffuni {
1049824e4adSPedro F. Giffuni 	if (EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_DIRHASHINDEX) &&
1059b58c801SPedro F. Giffuni 	    ip->i_flag & IN_E3INDEX)
1069824e4adSPedro F. Giffuni 		return (1);
1079824e4adSPedro F. Giffuni 	else
1089824e4adSPedro F. Giffuni 		return (0);
1099824e4adSPedro F. Giffuni }
1109824e4adSPedro F. Giffuni 
1119824e4adSPedro F. Giffuni static int
ext2_htree_check_next(struct inode * ip,uint32_t hash,const char * name,struct ext2fs_htree_lookup_info * info)1129824e4adSPedro F. Giffuni ext2_htree_check_next(struct inode *ip, uint32_t hash, const char *name,
1139824e4adSPedro F. Giffuni     struct ext2fs_htree_lookup_info *info)
1149824e4adSPedro F. Giffuni {
1159824e4adSPedro F. Giffuni 	struct vnode *vp = ITOV(ip);
1169824e4adSPedro F. Giffuni 	struct ext2fs_htree_lookup_level *level;
1179824e4adSPedro F. Giffuni 	struct buf *bp;
1189824e4adSPedro F. Giffuni 	uint32_t next_hash;
1199824e4adSPedro F. Giffuni 	int idx = info->h_levels_num - 1;
1209824e4adSPedro F. Giffuni 	int levels = 0;
1219824e4adSPedro F. Giffuni 
1229824e4adSPedro F. Giffuni 	do {
1239824e4adSPedro F. Giffuni 		level = &info->h_levels[idx];
1249824e4adSPedro F. Giffuni 		level->h_entry++;
1259824e4adSPedro F. Giffuni 		if (level->h_entry < level->h_entries +
1269824e4adSPedro F. Giffuni 		    ext2_htree_get_count(level->h_entries))
1279824e4adSPedro F. Giffuni 			break;
1289824e4adSPedro F. Giffuni 		if (idx == 0)
1299824e4adSPedro F. Giffuni 			return (0);
1309824e4adSPedro F. Giffuni 		idx--;
1319824e4adSPedro F. Giffuni 		levels++;
1329824e4adSPedro F. Giffuni 	} while (1);
1339824e4adSPedro F. Giffuni 
1349824e4adSPedro F. Giffuni 	next_hash = ext2_htree_get_hash(level->h_entry);
1359824e4adSPedro F. Giffuni 	if ((hash & 1) == 0) {
1369824e4adSPedro F. Giffuni 		if (hash != (next_hash & ~1))
1379824e4adSPedro F. Giffuni 			return (0);
1389824e4adSPedro F. Giffuni 	}
1399824e4adSPedro F. Giffuni 
1409824e4adSPedro F. Giffuni 	while (levels > 0) {
1419824e4adSPedro F. Giffuni 		levels--;
1429824e4adSPedro F. Giffuni 		if (ext2_blkatoff(vp, ext2_htree_get_block(level->h_entry) *
1439824e4adSPedro F. Giffuni 		    ip->i_e2fs->e2fs_bsize, NULL, &bp) != 0)
1449824e4adSPedro F. Giffuni 			return (0);
1459824e4adSPedro F. Giffuni 		level = &info->h_levels[idx + 1];
1469824e4adSPedro F. Giffuni 		brelse(level->h_bp);
1479824e4adSPedro F. Giffuni 		level->h_bp = bp;
1489824e4adSPedro F. Giffuni 		level->h_entry = level->h_entries =
1499824e4adSPedro F. Giffuni 		    ((struct ext2fs_htree_node *)bp->b_data)->h_entries;
1509824e4adSPedro F. Giffuni 	}
1519824e4adSPedro F. Giffuni 
1529824e4adSPedro F. Giffuni 	return (1);
1539824e4adSPedro F. Giffuni }
1549824e4adSPedro F. Giffuni 
1559824e4adSPedro F. Giffuni static uint32_t
ext2_htree_get_block(struct ext2fs_htree_entry * ep)1569824e4adSPedro F. Giffuni ext2_htree_get_block(struct ext2fs_htree_entry *ep)
1579824e4adSPedro F. Giffuni {
158cd3acfe7SFedor Uporov 	return (le32toh(ep->h_blk) & 0x00FFFFFF);
1599824e4adSPedro F. Giffuni }
1609824e4adSPedro F. Giffuni 
1619824e4adSPedro F. Giffuni static void
ext2_htree_set_block(struct ext2fs_htree_entry * ep,uint32_t blk)1629824e4adSPedro F. Giffuni ext2_htree_set_block(struct ext2fs_htree_entry *ep, uint32_t blk)
1639824e4adSPedro F. Giffuni {
164cd3acfe7SFedor Uporov 	ep->h_blk = htole32(blk);
1659824e4adSPedro F. Giffuni }
1669824e4adSPedro F. Giffuni 
1679824e4adSPedro F. Giffuni static uint16_t
ext2_htree_get_count(struct ext2fs_htree_entry * ep)1689824e4adSPedro F. Giffuni ext2_htree_get_count(struct ext2fs_htree_entry *ep)
1699824e4adSPedro F. Giffuni {
170cd3acfe7SFedor Uporov 	return (le16toh(((struct ext2fs_htree_count *)(ep))->h_entries_num));
1719824e4adSPedro F. Giffuni }
1729824e4adSPedro F. Giffuni 
1739824e4adSPedro F. Giffuni static void
ext2_htree_set_count(struct ext2fs_htree_entry * ep,uint16_t cnt)1749824e4adSPedro F. Giffuni ext2_htree_set_count(struct ext2fs_htree_entry *ep, uint16_t cnt)
1759824e4adSPedro F. Giffuni {
176cd3acfe7SFedor Uporov 	((struct ext2fs_htree_count *)(ep))->h_entries_num = htole16(cnt);
1779824e4adSPedro F. Giffuni }
1789824e4adSPedro F. Giffuni 
1799824e4adSPedro F. Giffuni static uint32_t
ext2_htree_get_hash(struct ext2fs_htree_entry * ep)1809824e4adSPedro F. Giffuni ext2_htree_get_hash(struct ext2fs_htree_entry *ep)
1819824e4adSPedro F. Giffuni {
182cd3acfe7SFedor Uporov 	return (le32toh(ep->h_hash));
1839824e4adSPedro F. Giffuni }
1849824e4adSPedro F. Giffuni 
1859824e4adSPedro F. Giffuni static uint16_t
ext2_htree_get_limit(struct ext2fs_htree_entry * ep)1869824e4adSPedro F. Giffuni ext2_htree_get_limit(struct ext2fs_htree_entry *ep)
1879824e4adSPedro F. Giffuni {
188cd3acfe7SFedor Uporov 	return (le16toh(((struct ext2fs_htree_count *)(ep))->h_entries_max));
1899824e4adSPedro F. Giffuni }
1909824e4adSPedro F. Giffuni 
1919824e4adSPedro F. Giffuni static void
ext2_htree_set_hash(struct ext2fs_htree_entry * ep,uint32_t hash)1929824e4adSPedro F. Giffuni ext2_htree_set_hash(struct ext2fs_htree_entry *ep, uint32_t hash)
1939824e4adSPedro F. Giffuni {
194cd3acfe7SFedor Uporov 	ep->h_hash = htole32(hash);
1959824e4adSPedro F. Giffuni }
1969824e4adSPedro F. Giffuni 
1979824e4adSPedro F. Giffuni static void
ext2_htree_set_limit(struct ext2fs_htree_entry * ep,uint16_t limit)1989824e4adSPedro F. Giffuni ext2_htree_set_limit(struct ext2fs_htree_entry *ep, uint16_t limit)
1999824e4adSPedro F. Giffuni {
200cd3acfe7SFedor Uporov 	((struct ext2fs_htree_count *)(ep))->h_entries_max = htole16(limit);
2019824e4adSPedro F. Giffuni }
2029824e4adSPedro F. Giffuni 
2039824e4adSPedro F. Giffuni static void
ext2_htree_release(struct ext2fs_htree_lookup_info * info)2049824e4adSPedro F. Giffuni ext2_htree_release(struct ext2fs_htree_lookup_info *info)
2059824e4adSPedro F. Giffuni {
2064cb92c4cSPedro F. Giffuni 	u_int i;
2079824e4adSPedro F. Giffuni 
2089824e4adSPedro F. Giffuni 	for (i = 0; i < info->h_levels_num; i++) {
2099824e4adSPedro F. Giffuni 		struct buf *bp = info->h_levels[i].h_bp;
210bf9a211dSPedro F. Giffuni 
2119824e4adSPedro F. Giffuni 		if (bp != NULL)
2129824e4adSPedro F. Giffuni 			brelse(bp);
2139824e4adSPedro F. Giffuni 	}
2149824e4adSPedro F. Giffuni }
2159824e4adSPedro F. Giffuni 
2169824e4adSPedro F. Giffuni static uint32_t
ext2_htree_root_limit(struct inode * ip,int len)2179824e4adSPedro F. Giffuni ext2_htree_root_limit(struct inode *ip, int len)
2189824e4adSPedro F. Giffuni {
2196d4a4ed7SFedor Uporov 	struct m_ext2fs *fs;
2209824e4adSPedro F. Giffuni 	uint32_t space;
2219824e4adSPedro F. Giffuni 
2226d4a4ed7SFedor Uporov 	fs = ip->i_e2fs;
2239824e4adSPedro F. Giffuni 	space = ip->i_e2fs->e2fs_bsize - EXT2_DIR_REC_LEN(1) -
2249824e4adSPedro F. Giffuni 	    EXT2_DIR_REC_LEN(2) - len;
2256d4a4ed7SFedor Uporov 
2266d4a4ed7SFedor Uporov 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
2276d4a4ed7SFedor Uporov 		space -= sizeof(struct ext2fs_htree_tail);
2286d4a4ed7SFedor Uporov 
2299824e4adSPedro F. Giffuni 	return (space / sizeof(struct ext2fs_htree_entry));
2309824e4adSPedro F. Giffuni }
2319824e4adSPedro F. Giffuni 
2329824e4adSPedro F. Giffuni static uint32_t
ext2_htree_node_limit(struct inode * ip)2339824e4adSPedro F. Giffuni ext2_htree_node_limit(struct inode *ip)
2349824e4adSPedro F. Giffuni {
2359824e4adSPedro F. Giffuni 	struct m_ext2fs *fs;
2369824e4adSPedro F. Giffuni 	uint32_t space;
2379824e4adSPedro F. Giffuni 
2389824e4adSPedro F. Giffuni 	fs = ip->i_e2fs;
2399824e4adSPedro F. Giffuni 	space = fs->e2fs_bsize - EXT2_DIR_REC_LEN(0);
2409824e4adSPedro F. Giffuni 
2416d4a4ed7SFedor Uporov 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
2426d4a4ed7SFedor Uporov 		space -= sizeof(struct ext2fs_htree_tail);
2436d4a4ed7SFedor Uporov 
2449824e4adSPedro F. Giffuni 	return (space / sizeof(struct ext2fs_htree_entry));
2459824e4adSPedro F. Giffuni }
2469824e4adSPedro F. Giffuni 
247cd3acfe7SFedor Uporov static void
ext2_get_hash_seed(struct ext2fs * es,uint32_t * seed)248cd3acfe7SFedor Uporov ext2_get_hash_seed(struct ext2fs* es, uint32_t* seed)
249cd3acfe7SFedor Uporov {
250cd3acfe7SFedor Uporov 
251cd3acfe7SFedor Uporov 	for (int i = 0; i < 4; i++)
252cd3acfe7SFedor Uporov 		seed[i] = le32toh(es->e3fs_hash_seed[i]);
253cd3acfe7SFedor Uporov }
254cd3acfe7SFedor Uporov 
2559824e4adSPedro F. Giffuni static int
ext2_htree_find_leaf(struct inode * ip,const char * name,int namelen,uint32_t * hash,uint8_t * hash_ver,struct ext2fs_htree_lookup_info * info)2569824e4adSPedro F. Giffuni ext2_htree_find_leaf(struct inode *ip, const char *name, int namelen,
2579824e4adSPedro F. Giffuni     uint32_t *hash, uint8_t *hash_ver,
2589824e4adSPedro F. Giffuni     struct ext2fs_htree_lookup_info *info)
2599824e4adSPedro F. Giffuni {
2609824e4adSPedro F. Giffuni 	struct vnode *vp;
2619824e4adSPedro F. Giffuni 	struct ext2fs *fs;
2629824e4adSPedro F. Giffuni 	struct m_ext2fs *m_fs;
2639824e4adSPedro F. Giffuni 	struct buf *bp = NULL;
2649824e4adSPedro F. Giffuni 	struct ext2fs_htree_root *rootp;
2659824e4adSPedro F. Giffuni 	struct ext2fs_htree_entry *entp, *start, *end, *middle, *found;
2669824e4adSPedro F. Giffuni 	struct ext2fs_htree_lookup_level *level_info;
2679824e4adSPedro F. Giffuni 	uint32_t hash_major = 0, hash_minor = 0;
2689824e4adSPedro F. Giffuni 	uint32_t levels, cnt;
269cd3acfe7SFedor Uporov 	uint32_t hash_seed[4];
2709824e4adSPedro F. Giffuni 	uint8_t hash_version;
2719824e4adSPedro F. Giffuni 
2729824e4adSPedro F. Giffuni 	if (name == NULL || info == NULL)
2739824e4adSPedro F. Giffuni 		return (-1);
2749824e4adSPedro F. Giffuni 
2759824e4adSPedro F. Giffuni 	vp = ITOV(ip);
2769824e4adSPedro F. Giffuni 	fs = ip->i_e2fs->e2fs;
2779824e4adSPedro F. Giffuni 	m_fs = ip->i_e2fs;
2789824e4adSPedro F. Giffuni 
2799824e4adSPedro F. Giffuni 	if (ext2_blkatoff(vp, 0, NULL, &bp) != 0)
2809824e4adSPedro F. Giffuni 		return (-1);
2819824e4adSPedro F. Giffuni 
2829824e4adSPedro F. Giffuni 	info->h_levels_num = 1;
2839824e4adSPedro F. Giffuni 	info->h_levels[0].h_bp = bp;
2849824e4adSPedro F. Giffuni 	rootp = (struct ext2fs_htree_root *)bp->b_data;
2859824e4adSPedro F. Giffuni 	if (rootp->h_info.h_hash_version != EXT2_HTREE_LEGACY &&
2869824e4adSPedro F. Giffuni 	    rootp->h_info.h_hash_version != EXT2_HTREE_HALF_MD4 &&
2879824e4adSPedro F. Giffuni 	    rootp->h_info.h_hash_version != EXT2_HTREE_TEA)
2889824e4adSPedro F. Giffuni 		goto error;
2899824e4adSPedro F. Giffuni 
2909824e4adSPedro F. Giffuni 	hash_version = rootp->h_info.h_hash_version;
2919824e4adSPedro F. Giffuni 	if (hash_version <= EXT2_HTREE_TEA)
2929824e4adSPedro F. Giffuni 		hash_version += m_fs->e2fs_uhash;
2939824e4adSPedro F. Giffuni 	*hash_ver = hash_version;
2949824e4adSPedro F. Giffuni 
295cd3acfe7SFedor Uporov 	ext2_get_hash_seed(fs, hash_seed);
296cd3acfe7SFedor Uporov 	ext2_htree_hash(name, namelen, hash_seed,
2979824e4adSPedro F. Giffuni 	    hash_version, &hash_major, &hash_minor);
2989824e4adSPedro F. Giffuni 	*hash = hash_major;
2999824e4adSPedro F. Giffuni 
3009824e4adSPedro F. Giffuni 	if ((levels = rootp->h_info.h_ind_levels) > 1)
3019824e4adSPedro F. Giffuni 		goto error;
3029824e4adSPedro F. Giffuni 
3039824e4adSPedro F. Giffuni 	entp = (struct ext2fs_htree_entry *)(((char *)&rootp->h_info) +
3049824e4adSPedro F. Giffuni 	    rootp->h_info.h_info_len);
3059824e4adSPedro F. Giffuni 
3069824e4adSPedro F. Giffuni 	if (ext2_htree_get_limit(entp) !=
3079824e4adSPedro F. Giffuni 	    ext2_htree_root_limit(ip, rootp->h_info.h_info_len))
3089824e4adSPedro F. Giffuni 		goto error;
3099824e4adSPedro F. Giffuni 
3109824e4adSPedro F. Giffuni 	while (1) {
3119824e4adSPedro F. Giffuni 		cnt = ext2_htree_get_count(entp);
3129824e4adSPedro F. Giffuni 		if (cnt == 0 || cnt > ext2_htree_get_limit(entp))
3139824e4adSPedro F. Giffuni 			goto error;
3149824e4adSPedro F. Giffuni 
3159824e4adSPedro F. Giffuni 		start = entp + 1;
3169824e4adSPedro F. Giffuni 		end = entp + cnt - 1;
3179824e4adSPedro F. Giffuni 		while (start <= end) {
3189824e4adSPedro F. Giffuni 			middle = start + (end - start) / 2;
3199824e4adSPedro F. Giffuni 			if (ext2_htree_get_hash(middle) > hash_major)
3209824e4adSPedro F. Giffuni 				end = middle - 1;
3219824e4adSPedro F. Giffuni 			else
3229824e4adSPedro F. Giffuni 				start = middle + 1;
3239824e4adSPedro F. Giffuni 		}
3249824e4adSPedro F. Giffuni 		found = start - 1;
3259824e4adSPedro F. Giffuni 
3269824e4adSPedro F. Giffuni 		level_info = &(info->h_levels[info->h_levels_num - 1]);
3279824e4adSPedro F. Giffuni 		level_info->h_bp = bp;
3289824e4adSPedro F. Giffuni 		level_info->h_entries = entp;
3299824e4adSPedro F. Giffuni 		level_info->h_entry = found;
3309824e4adSPedro F. Giffuni 		if (levels == 0)
3319824e4adSPedro F. Giffuni 			return (0);
3329824e4adSPedro F. Giffuni 		levels--;
3339824e4adSPedro F. Giffuni 		if (ext2_blkatoff(vp,
3349824e4adSPedro F. Giffuni 		    ext2_htree_get_block(found) * m_fs->e2fs_bsize,
3359824e4adSPedro F. Giffuni 		    NULL, &bp) != 0)
3369824e4adSPedro F. Giffuni 			goto error;
3379824e4adSPedro F. Giffuni 		entp = ((struct ext2fs_htree_node *)bp->b_data)->h_entries;
3389824e4adSPedro F. Giffuni 		info->h_levels_num++;
3399824e4adSPedro F. Giffuni 		info->h_levels[info->h_levels_num - 1].h_bp = bp;
3409824e4adSPedro F. Giffuni 	}
3419824e4adSPedro F. Giffuni 
3429824e4adSPedro F. Giffuni error:
3439824e4adSPedro F. Giffuni 	ext2_htree_release(info);
3449824e4adSPedro F. Giffuni 	return (-1);
3459824e4adSPedro F. Giffuni }
3469824e4adSPedro F. Giffuni 
3479824e4adSPedro F. Giffuni /*
3489824e4adSPedro F. Giffuni  * Try to lookup a directory entry in HTree index
3499824e4adSPedro F. Giffuni  */
3509824e4adSPedro F. Giffuni int
ext2_htree_lookup(struct inode * ip,const char * name,int namelen,struct buf ** bpp,int * entryoffp,doff_t * offp,doff_t * prevoffp,doff_t * endusefulp,struct ext2fs_searchslot * ss)3519824e4adSPedro F. Giffuni ext2_htree_lookup(struct inode *ip, const char *name, int namelen,
3529824e4adSPedro F. Giffuni     struct buf **bpp, int *entryoffp, doff_t *offp,
3539824e4adSPedro F. Giffuni     doff_t *prevoffp, doff_t *endusefulp,
3549824e4adSPedro F. Giffuni     struct ext2fs_searchslot *ss)
3559824e4adSPedro F. Giffuni {
3569824e4adSPedro F. Giffuni 	struct vnode *vp;
3579824e4adSPedro F. Giffuni 	struct ext2fs_htree_lookup_info info;
3589824e4adSPedro F. Giffuni 	struct ext2fs_htree_entry *leaf_node;
3599824e4adSPedro F. Giffuni 	struct m_ext2fs *m_fs;
3609824e4adSPedro F. Giffuni 	struct buf *bp;
3619824e4adSPedro F. Giffuni 	uint32_t blk;
3629824e4adSPedro F. Giffuni 	uint32_t dirhash;
3639824e4adSPedro F. Giffuni 	uint32_t bsize;
3649824e4adSPedro F. Giffuni 	uint8_t hash_version;
3659824e4adSPedro F. Giffuni 	int search_next;
3669824e4adSPedro F. Giffuni 	int found = 0;
3679824e4adSPedro F. Giffuni 
3689824e4adSPedro F. Giffuni 	m_fs = ip->i_e2fs;
3699824e4adSPedro F. Giffuni 	bsize = m_fs->e2fs_bsize;
3709824e4adSPedro F. Giffuni 	vp = ITOV(ip);
3719824e4adSPedro F. Giffuni 
3729824e4adSPedro F. Giffuni 	/* TODO: print error msg because we don't lookup '.' and '..' */
3739824e4adSPedro F. Giffuni 
3749824e4adSPedro F. Giffuni 	memset(&info, 0, sizeof(info));
3759824e4adSPedro F. Giffuni 	if (ext2_htree_find_leaf(ip, name, namelen, &dirhash,
3769824e4adSPedro F. Giffuni 	    &hash_version, &info))
3779824e4adSPedro F. Giffuni 		return (-1);
3789824e4adSPedro F. Giffuni 
3799824e4adSPedro F. Giffuni 	do {
3809824e4adSPedro F. Giffuni 		leaf_node = info.h_levels[info.h_levels_num - 1].h_entry;
3819824e4adSPedro F. Giffuni 		blk = ext2_htree_get_block(leaf_node);
3829824e4adSPedro F. Giffuni 		if (ext2_blkatoff(vp, blk * bsize, NULL, &bp) != 0) {
3839824e4adSPedro F. Giffuni 			ext2_htree_release(&info);
3849824e4adSPedro F. Giffuni 			return (-1);
3859824e4adSPedro F. Giffuni 		}
3869824e4adSPedro F. Giffuni 
3879824e4adSPedro F. Giffuni 		*offp = blk * bsize;
3889824e4adSPedro F. Giffuni 		*entryoffp = 0;
3899824e4adSPedro F. Giffuni 		*prevoffp = blk * bsize;
3909824e4adSPedro F. Giffuni 		*endusefulp = blk * bsize;
3919824e4adSPedro F. Giffuni 
3929824e4adSPedro F. Giffuni 		if (ss->slotstatus == NONE) {
3939824e4adSPedro F. Giffuni 			ss->slotoffset = -1;
3949824e4adSPedro F. Giffuni 			ss->slotfreespace = 0;
3959824e4adSPedro F. Giffuni 		}
3969824e4adSPedro F. Giffuni 
3979824e4adSPedro F. Giffuni 		if (ext2_search_dirblock(ip, bp->b_data, &found,
3989824e4adSPedro F. Giffuni 		    name, namelen, entryoffp, offp, prevoffp,
3999824e4adSPedro F. Giffuni 		    endusefulp, ss) != 0) {
4009824e4adSPedro F. Giffuni 			brelse(bp);
4019824e4adSPedro F. Giffuni 			ext2_htree_release(&info);
4029824e4adSPedro F. Giffuni 			return (-1);
4039824e4adSPedro F. Giffuni 		}
4049824e4adSPedro F. Giffuni 
4059824e4adSPedro F. Giffuni 		if (found) {
4069824e4adSPedro F. Giffuni 			*bpp = bp;
4079824e4adSPedro F. Giffuni 			ext2_htree_release(&info);
4089824e4adSPedro F. Giffuni 			return (0);
4099824e4adSPedro F. Giffuni 		}
4109824e4adSPedro F. Giffuni 
4119824e4adSPedro F. Giffuni 		brelse(bp);
4129824e4adSPedro F. Giffuni 		search_next = ext2_htree_check_next(ip, dirhash, name, &info);
4139824e4adSPedro F. Giffuni 	} while (search_next);
4149824e4adSPedro F. Giffuni 
4159824e4adSPedro F. Giffuni 	ext2_htree_release(&info);
4169824e4adSPedro F. Giffuni 	return (ENOENT);
4179824e4adSPedro F. Giffuni }
4189824e4adSPedro F. Giffuni 
4199824e4adSPedro F. Giffuni static int
ext2_htree_append_block(struct vnode * vp,char * data,struct componentname * cnp,uint32_t blksize)4209824e4adSPedro F. Giffuni ext2_htree_append_block(struct vnode *vp, char *data,
4219824e4adSPedro F. Giffuni     struct componentname *cnp, uint32_t blksize)
4229824e4adSPedro F. Giffuni {
4239824e4adSPedro F. Giffuni 	struct iovec aiov;
4249824e4adSPedro F. Giffuni 	struct uio auio;
4259824e4adSPedro F. Giffuni 	struct inode *dp = VTOI(vp);
4269824e4adSPedro F. Giffuni 	uint64_t cursize, newsize;
4279824e4adSPedro F. Giffuni 	int error;
4289824e4adSPedro F. Giffuni 
4299824e4adSPedro F. Giffuni 	cursize = roundup(dp->i_size, blksize);
4309824e4adSPedro F. Giffuni 	newsize = cursize + blksize;
4319824e4adSPedro F. Giffuni 
4329824e4adSPedro F. Giffuni 	auio.uio_offset = cursize;
4339824e4adSPedro F. Giffuni 	auio.uio_resid = blksize;
4349824e4adSPedro F. Giffuni 	aiov.iov_len = blksize;
4359824e4adSPedro F. Giffuni 	aiov.iov_base = data;
4369824e4adSPedro F. Giffuni 	auio.uio_iov = &aiov;
4379824e4adSPedro F. Giffuni 	auio.uio_iovcnt = 1;
4389824e4adSPedro F. Giffuni 	auio.uio_rw = UIO_WRITE;
4399824e4adSPedro F. Giffuni 	auio.uio_segflg = UIO_SYSSPACE;
440284cf3f1SMateusz Guzik 	auio.uio_td = NULL;
4419824e4adSPedro F. Giffuni 	error = VOP_WRITE(vp, &auio, IO_SYNC, cnp->cn_cred);
4429824e4adSPedro F. Giffuni 	if (!error)
4439824e4adSPedro F. Giffuni 		dp->i_size = newsize;
4449824e4adSPedro F. Giffuni 
4459824e4adSPedro F. Giffuni 	return (error);
4469824e4adSPedro F. Giffuni }
4479824e4adSPedro F. Giffuni 
4489824e4adSPedro F. Giffuni static int
ext2_htree_writebuf(struct inode * ip,struct ext2fs_htree_lookup_info * info)4496d4a4ed7SFedor Uporov ext2_htree_writebuf(struct inode* ip, struct ext2fs_htree_lookup_info *info)
4509824e4adSPedro F. Giffuni {
4519824e4adSPedro F. Giffuni 	int i, error;
4529824e4adSPedro F. Giffuni 
4539824e4adSPedro F. Giffuni 	for (i = 0; i < info->h_levels_num; i++) {
4549824e4adSPedro F. Giffuni 		struct buf *bp = info->h_levels[i].h_bp;
4556d4a4ed7SFedor Uporov 		ext2_dx_csum_set(ip, (struct ext2fs_direct_2 *)bp->b_data);
4569824e4adSPedro F. Giffuni 		error = bwrite(bp);
4579824e4adSPedro F. Giffuni 		if (error)
4589824e4adSPedro F. Giffuni 			return (error);
4599824e4adSPedro F. Giffuni 	}
4609824e4adSPedro F. Giffuni 
4619824e4adSPedro F. Giffuni 	return (0);
4629824e4adSPedro F. Giffuni }
4639824e4adSPedro F. Giffuni 
4649824e4adSPedro F. Giffuni static void
ext2_htree_insert_entry_to_level(struct ext2fs_htree_lookup_level * level,uint32_t hash,uint32_t blk)4659824e4adSPedro F. Giffuni ext2_htree_insert_entry_to_level(struct ext2fs_htree_lookup_level *level,
4669824e4adSPedro F. Giffuni     uint32_t hash, uint32_t blk)
4679824e4adSPedro F. Giffuni {
4689824e4adSPedro F. Giffuni 	struct ext2fs_htree_entry *target;
4699824e4adSPedro F. Giffuni 	int entries_num;
4709824e4adSPedro F. Giffuni 
4719824e4adSPedro F. Giffuni 	target = level->h_entry + 1;
4729824e4adSPedro F. Giffuni 	entries_num = ext2_htree_get_count(level->h_entries);
4739824e4adSPedro F. Giffuni 
4749824e4adSPedro F. Giffuni 	memmove(target + 1, target, (char *)(level->h_entries + entries_num) -
4759824e4adSPedro F. Giffuni 	    (char *)target);
4769824e4adSPedro F. Giffuni 	ext2_htree_set_block(target, blk);
4779824e4adSPedro F. Giffuni 	ext2_htree_set_hash(target, hash);
4789824e4adSPedro F. Giffuni 	ext2_htree_set_count(level->h_entries, entries_num + 1);
4799824e4adSPedro F. Giffuni }
4809824e4adSPedro F. Giffuni 
4819824e4adSPedro F. Giffuni /*
4829824e4adSPedro F. Giffuni  * Insert an index entry to the index node.
4839824e4adSPedro F. Giffuni  */
4849824e4adSPedro F. Giffuni static void
ext2_htree_insert_entry(struct ext2fs_htree_lookup_info * info,uint32_t hash,uint32_t blk)4859824e4adSPedro F. Giffuni ext2_htree_insert_entry(struct ext2fs_htree_lookup_info *info,
4869824e4adSPedro F. Giffuni     uint32_t hash, uint32_t blk)
4879824e4adSPedro F. Giffuni {
4889824e4adSPedro F. Giffuni 	struct ext2fs_htree_lookup_level *level;
4899824e4adSPedro F. Giffuni 
4909824e4adSPedro F. Giffuni 	level = &info->h_levels[info->h_levels_num - 1];
4919824e4adSPedro F. Giffuni 	ext2_htree_insert_entry_to_level(level, hash, blk);
4929824e4adSPedro F. Giffuni }
4939824e4adSPedro F. Giffuni 
4949824e4adSPedro F. Giffuni /*
4959824e4adSPedro F. Giffuni  * Compare two entry sort descriptors by name hash value.
4969824e4adSPedro F. Giffuni  * This is used together with qsort.
4979824e4adSPedro F. Giffuni  */
4989824e4adSPedro F. Giffuni static int
ext2_htree_cmp_sort_entry(const void * e1,const void * e2)4999824e4adSPedro F. Giffuni ext2_htree_cmp_sort_entry(const void *e1, const void *e2)
5009824e4adSPedro F. Giffuni {
5019824e4adSPedro F. Giffuni 	const struct ext2fs_htree_sort_entry *entry1, *entry2;
5029824e4adSPedro F. Giffuni 
5039824e4adSPedro F. Giffuni 	entry1 = (const struct ext2fs_htree_sort_entry *)e1;
5049824e4adSPedro F. Giffuni 	entry2 = (const struct ext2fs_htree_sort_entry *)e2;
5059824e4adSPedro F. Giffuni 
506cd3acfe7SFedor Uporov 	if (le32toh(entry1->h_hash) < le32toh(entry2->h_hash))
5079824e4adSPedro F. Giffuni 		return (-1);
508cd3acfe7SFedor Uporov 	if (le32toh(entry1->h_hash) > le32toh(entry2->h_hash))
5099824e4adSPedro F. Giffuni 		return (1);
5109824e4adSPedro F. Giffuni 	return (0);
5119824e4adSPedro F. Giffuni }
5129824e4adSPedro F. Giffuni 
5139824e4adSPedro F. Giffuni /*
5149824e4adSPedro F. Giffuni  * Append an entry to the end of the directory block.
5159824e4adSPedro F. Giffuni  */
5169824e4adSPedro F. Giffuni static void
ext2_append_entry(char * block,uint32_t blksize,struct ext2fs_direct_2 * last_entry,struct ext2fs_direct_2 * new_entry,int csum_size)5179824e4adSPedro F. Giffuni ext2_append_entry(char *block, uint32_t blksize,
5189824e4adSPedro F. Giffuni     struct ext2fs_direct_2 *last_entry,
5196d4a4ed7SFedor Uporov     struct ext2fs_direct_2 *new_entry, int csum_size)
5209824e4adSPedro F. Giffuni {
5219824e4adSPedro F. Giffuni 	uint16_t entry_len;
5229824e4adSPedro F. Giffuni 
5239824e4adSPedro F. Giffuni 	entry_len = EXT2_DIR_REC_LEN(last_entry->e2d_namlen);
524cd3acfe7SFedor Uporov 	last_entry->e2d_reclen = htole16(entry_len);
5259824e4adSPedro F. Giffuni 	last_entry = (struct ext2fs_direct_2 *)((char *)last_entry + entry_len);
526cd3acfe7SFedor Uporov 	new_entry->e2d_reclen = htole16(block + blksize - (char *)last_entry -
527cd3acfe7SFedor Uporov 	    csum_size);
5289824e4adSPedro F. Giffuni 	memcpy(last_entry, new_entry, EXT2_DIR_REC_LEN(new_entry->e2d_namlen));
5299824e4adSPedro F. Giffuni }
5309824e4adSPedro F. Giffuni 
5319824e4adSPedro F. Giffuni /*
5329824e4adSPedro F. Giffuni  * Move half of entries from the old directory block to the new one.
5339824e4adSPedro F. Giffuni  */
5349824e4adSPedro F. Giffuni static int
ext2_htree_split_dirblock(struct inode * ip,char * block1,char * block2,uint32_t blksize,uint32_t * hash_seed,uint8_t hash_version,uint32_t * split_hash,struct ext2fs_direct_2 * entry)5356d4a4ed7SFedor Uporov ext2_htree_split_dirblock(struct inode *ip, char *block1, char *block2,
5366d4a4ed7SFedor Uporov     uint32_t blksize, uint32_t *hash_seed, uint8_t hash_version,
5379824e4adSPedro F. Giffuni     uint32_t *split_hash, struct ext2fs_direct_2 *entry)
5389824e4adSPedro F. Giffuni {
5396d4a4ed7SFedor Uporov 	struct m_ext2fs *fs;
5409824e4adSPedro F. Giffuni 	int entry_cnt = 0;
5416d4a4ed7SFedor Uporov 	int size = 0, csum_size = 0;
5429824e4adSPedro F. Giffuni 	int i, k;
5439824e4adSPedro F. Giffuni 	uint32_t offset;
5449824e4adSPedro F. Giffuni 	uint16_t entry_len = 0;
5459824e4adSPedro F. Giffuni 	uint32_t entry_hash;
5469824e4adSPedro F. Giffuni 	struct ext2fs_direct_2 *ep, *last;
5479824e4adSPedro F. Giffuni 	char *dest;
5489824e4adSPedro F. Giffuni 	struct ext2fs_htree_sort_entry *sort_info;
5499824e4adSPedro F. Giffuni 
5506d4a4ed7SFedor Uporov 	fs = ip->i_e2fs;
5519824e4adSPedro F. Giffuni 	ep = (struct ext2fs_direct_2 *)block1;
5529824e4adSPedro F. Giffuni 	dest = block2;
5539824e4adSPedro F. Giffuni 	sort_info = (struct ext2fs_htree_sort_entry *)
5549824e4adSPedro F. Giffuni 	    ((char *)block2 + blksize);
5559824e4adSPedro F. Giffuni 
5566d4a4ed7SFedor Uporov 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
5576d4a4ed7SFedor Uporov 		csum_size = sizeof(struct ext2fs_direct_tail);
5586d4a4ed7SFedor Uporov 
5599824e4adSPedro F. Giffuni 	/*
5609824e4adSPedro F. Giffuni 	 * Calculate name hash value for the entry which is to be added.
5619824e4adSPedro F. Giffuni 	 */
5629824e4adSPedro F. Giffuni 	ext2_htree_hash(entry->e2d_name, entry->e2d_namlen, hash_seed,
5639824e4adSPedro F. Giffuni 	    hash_version, &entry_hash, NULL);
5649824e4adSPedro F. Giffuni 
5659824e4adSPedro F. Giffuni 	/*
5669824e4adSPedro F. Giffuni 	 * Fill in directory entry sort descriptors.
5679824e4adSPedro F. Giffuni 	 */
5686d4a4ed7SFedor Uporov 	while ((char *)ep < block1 + blksize - csum_size) {
569cd3acfe7SFedor Uporov 		if (le32toh(ep->e2d_ino) && ep->e2d_namlen) {
5709824e4adSPedro F. Giffuni 			entry_cnt++;
5719824e4adSPedro F. Giffuni 			sort_info--;
5729824e4adSPedro F. Giffuni 			sort_info->h_size = ep->e2d_reclen;
573cd3acfe7SFedor Uporov 			sort_info->h_offset = htole16((char *)ep - block1);
5749824e4adSPedro F. Giffuni 			ext2_htree_hash(ep->e2d_name, ep->e2d_namlen,
5759824e4adSPedro F. Giffuni 			    hash_seed, hash_version,
5769824e4adSPedro F. Giffuni 			    &sort_info->h_hash, NULL);
577cd3acfe7SFedor Uporov 			sort_info->h_hash = htole32(sort_info->h_hash);
5789824e4adSPedro F. Giffuni 		}
5799824e4adSPedro F. Giffuni 		ep = (struct ext2fs_direct_2 *)
580cd3acfe7SFedor Uporov 		    ((char *)ep + le16toh(ep->e2d_reclen));
5819824e4adSPedro F. Giffuni 	}
5829824e4adSPedro F. Giffuni 
5839824e4adSPedro F. Giffuni 	/*
5849824e4adSPedro F. Giffuni 	 * Sort directory entry descriptors by name hash value.
5859824e4adSPedro F. Giffuni 	 */
5869824e4adSPedro F. Giffuni 	qsort(sort_info, entry_cnt, sizeof(struct ext2fs_htree_sort_entry),
5879824e4adSPedro F. Giffuni 	    ext2_htree_cmp_sort_entry);
5889824e4adSPedro F. Giffuni 
5899824e4adSPedro F. Giffuni 	/*
5909824e4adSPedro F. Giffuni 	 * Count the number of entries to move to directory block 2.
5919824e4adSPedro F. Giffuni 	 */
5929824e4adSPedro F. Giffuni 	for (i = entry_cnt - 1; i >= 0; i--) {
593cd3acfe7SFedor Uporov 		if (le16toh(sort_info[i].h_size) + size > blksize / 2)
5949824e4adSPedro F. Giffuni 			break;
595cd3acfe7SFedor Uporov 		size += le16toh(sort_info[i].h_size);
5969824e4adSPedro F. Giffuni 	}
5979824e4adSPedro F. Giffuni 
598cd3acfe7SFedor Uporov 	*split_hash = le32toh(sort_info[i + 1].h_hash);
5999824e4adSPedro F. Giffuni 
6009824e4adSPedro F. Giffuni 	/*
6019824e4adSPedro F. Giffuni 	 * Set collision bit.
6029824e4adSPedro F. Giffuni 	 */
603cd3acfe7SFedor Uporov 	if (*split_hash == le32toh(sort_info[i].h_hash))
6049824e4adSPedro F. Giffuni 		*split_hash += 1;
6059824e4adSPedro F. Giffuni 
6069824e4adSPedro F. Giffuni 	/*
6079824e4adSPedro F. Giffuni 	 * Move half of directory entries from block 1 to block 2.
6089824e4adSPedro F. Giffuni 	 */
6099824e4adSPedro F. Giffuni 	for (k = i + 1; k < entry_cnt; k++) {
6109824e4adSPedro F. Giffuni 		ep = (struct ext2fs_direct_2 *)((char *)block1 +
611cd3acfe7SFedor Uporov 		    le16toh(sort_info[k].h_offset));
6129824e4adSPedro F. Giffuni 		entry_len = EXT2_DIR_REC_LEN(ep->e2d_namlen);
6139824e4adSPedro F. Giffuni 		memcpy(dest, ep, entry_len);
614cd3acfe7SFedor Uporov 		((struct ext2fs_direct_2 *)dest)->e2d_reclen =
615cd3acfe7SFedor Uporov 		    htole16(entry_len);
6169824e4adSPedro F. Giffuni 		/* Mark directory entry as unused. */
6179824e4adSPedro F. Giffuni 		ep->e2d_ino = 0;
6189824e4adSPedro F. Giffuni 		dest += entry_len;
6199824e4adSPedro F. Giffuni 	}
6209824e4adSPedro F. Giffuni 	dest -= entry_len;
6219824e4adSPedro F. Giffuni 
6229824e4adSPedro F. Giffuni 	/* Shrink directory entries in block 1. */
6239824e4adSPedro F. Giffuni 	last = (struct ext2fs_direct_2 *)block1;
6249824e4adSPedro F. Giffuni 	entry_len = 0;
6256d4a4ed7SFedor Uporov 	for (offset = 0; offset < blksize - csum_size; ) {
6269824e4adSPedro F. Giffuni 		ep = (struct ext2fs_direct_2 *)(block1 + offset);
627cd3acfe7SFedor Uporov 		offset += le16toh(ep->e2d_reclen);
628cd3acfe7SFedor Uporov 		if (le32toh(ep->e2d_ino)) {
6299824e4adSPedro F. Giffuni 			last = (struct ext2fs_direct_2 *)
6309824e4adSPedro F. Giffuni 			    ((char *)last + entry_len);
6319824e4adSPedro F. Giffuni 			entry_len = EXT2_DIR_REC_LEN(ep->e2d_namlen);
6329824e4adSPedro F. Giffuni 			memcpy((void *)last, (void *)ep, entry_len);
633cd3acfe7SFedor Uporov 			last->e2d_reclen = htole16(entry_len);
6349824e4adSPedro F. Giffuni 		}
6359824e4adSPedro F. Giffuni 	}
6369824e4adSPedro F. Giffuni 
6379824e4adSPedro F. Giffuni 	if (entry_hash >= *split_hash) {
6389824e4adSPedro F. Giffuni 		/* Add entry to block 2. */
6399824e4adSPedro F. Giffuni 		ext2_append_entry(block2, blksize,
6406d4a4ed7SFedor Uporov 		    (struct ext2fs_direct_2 *)dest, entry, csum_size);
6419824e4adSPedro F. Giffuni 
6429824e4adSPedro F. Giffuni 		/* Adjust length field of last entry of block 1. */
643cd3acfe7SFedor Uporov 		last->e2d_reclen = htole16(block1 + blksize - (char *)last -
644cd3acfe7SFedor Uporov 		    csum_size);
6459824e4adSPedro F. Giffuni 	} else {
6469824e4adSPedro F. Giffuni 		/* Add entry to block 1. */
6476d4a4ed7SFedor Uporov 		ext2_append_entry(block1, blksize, last, entry, csum_size);
6489824e4adSPedro F. Giffuni 
6499824e4adSPedro F. Giffuni 		/* Adjust length field of last entry of block 2. */
6509824e4adSPedro F. Giffuni 		((struct ext2fs_direct_2 *)dest)->e2d_reclen =
651cd3acfe7SFedor Uporov 		    htole16(block2 + blksize - dest - csum_size);
6526d4a4ed7SFedor Uporov 	}
6536d4a4ed7SFedor Uporov 
6546d4a4ed7SFedor Uporov 	if (csum_size) {
6556d4a4ed7SFedor Uporov 		ext2_init_dirent_tail(EXT2_DIRENT_TAIL(block1, blksize));
6566d4a4ed7SFedor Uporov 		ext2_init_dirent_tail(EXT2_DIRENT_TAIL(block2, blksize));
6579824e4adSPedro F. Giffuni 	}
6589824e4adSPedro F. Giffuni 
6599824e4adSPedro F. Giffuni 	return (0);
6609824e4adSPedro F. Giffuni }
6619824e4adSPedro F. Giffuni 
6629824e4adSPedro F. Giffuni /*
6639824e4adSPedro F. Giffuni  * Create an HTree index for a directory
6649824e4adSPedro F. Giffuni  */
6659824e4adSPedro F. Giffuni int
ext2_htree_create_index(struct vnode * vp,struct componentname * cnp,struct ext2fs_direct_2 * new_entry)6669824e4adSPedro F. Giffuni ext2_htree_create_index(struct vnode *vp, struct componentname *cnp,
6679824e4adSPedro F. Giffuni     struct ext2fs_direct_2 *new_entry)
6689824e4adSPedro F. Giffuni {
6699824e4adSPedro F. Giffuni 	struct buf *bp = NULL;
6709824e4adSPedro F. Giffuni 	struct inode *dp;
6719824e4adSPedro F. Giffuni 	struct ext2fs *fs;
6729824e4adSPedro F. Giffuni 	struct m_ext2fs *m_fs;
6739824e4adSPedro F. Giffuni 	struct ext2fs_direct_2 *ep, *dotdot;
6749824e4adSPedro F. Giffuni 	struct ext2fs_htree_root *root;
6759824e4adSPedro F. Giffuni 	struct ext2fs_htree_lookup_info info;
6769824e4adSPedro F. Giffuni 	uint32_t blksize, dirlen, split_hash;
677cd3acfe7SFedor Uporov 	uint32_t hash_seed[4];
6789824e4adSPedro F. Giffuni 	uint8_t hash_version;
6799824e4adSPedro F. Giffuni 	char *buf1 = NULL;
6809824e4adSPedro F. Giffuni 	char *buf2 = NULL;
6819824e4adSPedro F. Giffuni 	int error = 0;
6829824e4adSPedro F. Giffuni 
6839824e4adSPedro F. Giffuni 	dp = VTOI(vp);
6849824e4adSPedro F. Giffuni 	fs = dp->i_e2fs->e2fs;
6859824e4adSPedro F. Giffuni 	m_fs = dp->i_e2fs;
6869824e4adSPedro F. Giffuni 	blksize = m_fs->e2fs_bsize;
6879824e4adSPedro F. Giffuni 
6889824e4adSPedro F. Giffuni 	buf1 = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
6899824e4adSPedro F. Giffuni 	buf2 = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
6909824e4adSPedro F. Giffuni 
6919824e4adSPedro F. Giffuni 	if ((error = ext2_blkatoff(vp, 0, NULL, &bp)) != 0)
6929824e4adSPedro F. Giffuni 		goto out;
6939824e4adSPedro F. Giffuni 
6949824e4adSPedro F. Giffuni 	root = (struct ext2fs_htree_root *)bp->b_data;
6959824e4adSPedro F. Giffuni 	dotdot = (struct ext2fs_direct_2 *)((char *)&(root->h_dotdot));
696cd3acfe7SFedor Uporov 	ep = (struct ext2fs_direct_2 *)((char *)dotdot +
697cd3acfe7SFedor Uporov 	    le16toh(dotdot->e2d_reclen));
6989824e4adSPedro F. Giffuni 	dirlen = (char *)root + blksize - (char *)ep;
6999824e4adSPedro F. Giffuni 	memcpy(buf1, ep, dirlen);
7009824e4adSPedro F. Giffuni 	ep = (struct ext2fs_direct_2 *)buf1;
7019824e4adSPedro F. Giffuni 	while ((char *)ep < buf1 + dirlen)
7029824e4adSPedro F. Giffuni 		ep = (struct ext2fs_direct_2 *)
703cd3acfe7SFedor Uporov 		    ((char *)ep + le16toh(ep->e2d_reclen));
704cd3acfe7SFedor Uporov 	ep->e2d_reclen = htole16(buf1 + blksize - (char *)ep);
7059824e4adSPedro F. Giffuni 
7069b58c801SPedro F. Giffuni 	dp->i_flag |= IN_E3INDEX;
7079824e4adSPedro F. Giffuni 
7089824e4adSPedro F. Giffuni 	/*
7099824e4adSPedro F. Giffuni 	 * Initialize index root.
7109824e4adSPedro F. Giffuni 	 */
711cd3acfe7SFedor Uporov 	dotdot->e2d_reclen = htole16(blksize - EXT2_DIR_REC_LEN(1));
7129824e4adSPedro F. Giffuni 	memset(&root->h_info, 0, sizeof(root->h_info));
7139824e4adSPedro F. Giffuni 	root->h_info.h_hash_version = fs->e3fs_def_hash_version;
7149824e4adSPedro F. Giffuni 	root->h_info.h_info_len = sizeof(root->h_info);
7159824e4adSPedro F. Giffuni 	ext2_htree_set_block(root->h_entries, 1);
7169824e4adSPedro F. Giffuni 	ext2_htree_set_count(root->h_entries, 1);
7179824e4adSPedro F. Giffuni 	ext2_htree_set_limit(root->h_entries,
7189824e4adSPedro F. Giffuni 	    ext2_htree_root_limit(dp, sizeof(root->h_info)));
7199824e4adSPedro F. Giffuni 
7209824e4adSPedro F. Giffuni 	memset(&info, 0, sizeof(info));
7219824e4adSPedro F. Giffuni 	info.h_levels_num = 1;
7229824e4adSPedro F. Giffuni 	info.h_levels[0].h_entries = root->h_entries;
7239824e4adSPedro F. Giffuni 	info.h_levels[0].h_entry = root->h_entries;
7249824e4adSPedro F. Giffuni 
7259824e4adSPedro F. Giffuni 	hash_version = root->h_info.h_hash_version;
7269824e4adSPedro F. Giffuni 	if (hash_version <= EXT2_HTREE_TEA)
7279824e4adSPedro F. Giffuni 		hash_version += m_fs->e2fs_uhash;
728cd3acfe7SFedor Uporov 	ext2_get_hash_seed(fs, hash_seed);
729cd3acfe7SFedor Uporov 	ext2_htree_split_dirblock(dp, buf1, buf2, blksize, hash_seed,
7309824e4adSPedro F. Giffuni 	    hash_version, &split_hash, new_entry);
7319824e4adSPedro F. Giffuni 	ext2_htree_insert_entry(&info, split_hash, 2);
7329824e4adSPedro F. Giffuni 
7339824e4adSPedro F. Giffuni 	/*
7349824e4adSPedro F. Giffuni 	 * Write directory block 0.
7359824e4adSPedro F. Giffuni 	 */
7366d4a4ed7SFedor Uporov 	ext2_dx_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
7379824e4adSPedro F. Giffuni 	if (DOINGASYNC(vp)) {
7389824e4adSPedro F. Giffuni 		bdwrite(bp);
7399824e4adSPedro F. Giffuni 		error = 0;
7409824e4adSPedro F. Giffuni 	} else {
7419824e4adSPedro F. Giffuni 		error = bwrite(bp);
7429824e4adSPedro F. Giffuni 	}
7439824e4adSPedro F. Giffuni 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
7449824e4adSPedro F. Giffuni 	if (error)
7459824e4adSPedro F. Giffuni 		goto out;
7469824e4adSPedro F. Giffuni 
7479824e4adSPedro F. Giffuni 	/*
7489824e4adSPedro F. Giffuni 	 * Write directory block 1.
7499824e4adSPedro F. Giffuni 	 */
7506d4a4ed7SFedor Uporov 	ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)buf1);
7519824e4adSPedro F. Giffuni 	error = ext2_htree_append_block(vp, buf1, cnp, blksize);
7529824e4adSPedro F. Giffuni 	if (error)
7539824e4adSPedro F. Giffuni 		goto out1;
7549824e4adSPedro F. Giffuni 
7559824e4adSPedro F. Giffuni 	/*
7569824e4adSPedro F. Giffuni 	 * Write directory block 2.
7579824e4adSPedro F. Giffuni 	 */
7586d4a4ed7SFedor Uporov 	ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)buf2);
7599824e4adSPedro F. Giffuni 	error = ext2_htree_append_block(vp, buf2, cnp, blksize);
7609824e4adSPedro F. Giffuni 
7619824e4adSPedro F. Giffuni 	free(buf1, M_TEMP);
7629824e4adSPedro F. Giffuni 	free(buf2, M_TEMP);
7639824e4adSPedro F. Giffuni 	return (error);
7649824e4adSPedro F. Giffuni out:
7659824e4adSPedro F. Giffuni 	if (bp != NULL)
7669824e4adSPedro F. Giffuni 		brelse(bp);
7679824e4adSPedro F. Giffuni out1:
7689824e4adSPedro F. Giffuni 	free(buf1, M_TEMP);
7699824e4adSPedro F. Giffuni 	free(buf2, M_TEMP);
7709824e4adSPedro F. Giffuni 	return (error);
7719824e4adSPedro F. Giffuni }
7729824e4adSPedro F. Giffuni 
7739824e4adSPedro F. Giffuni /*
7749824e4adSPedro F. Giffuni  * Add an entry to the directory using htree index.
7759824e4adSPedro F. Giffuni  */
7769824e4adSPedro F. Giffuni int
ext2_htree_add_entry(struct vnode * dvp,struct ext2fs_direct_2 * entry,struct componentname * cnp)7779824e4adSPedro F. Giffuni ext2_htree_add_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry,
7789824e4adSPedro F. Giffuni     struct componentname *cnp)
7799824e4adSPedro F. Giffuni {
7809824e4adSPedro F. Giffuni 	struct ext2fs_htree_entry *entries, *leaf_node;
7819824e4adSPedro F. Giffuni 	struct ext2fs_htree_lookup_info info;
7829824e4adSPedro F. Giffuni 	struct buf *bp = NULL;
7839824e4adSPedro F. Giffuni 	struct ext2fs *fs;
7849824e4adSPedro F. Giffuni 	struct m_ext2fs *m_fs;
7859824e4adSPedro F. Giffuni 	struct inode *ip;
7869824e4adSPedro F. Giffuni 	uint16_t ent_num;
7879824e4adSPedro F. Giffuni 	uint32_t dirhash, split_hash;
7889824e4adSPedro F. Giffuni 	uint32_t blksize, blknum;
7899824e4adSPedro F. Giffuni 	uint64_t cursize, dirsize;
790cd3acfe7SFedor Uporov 	uint32_t hash_seed[4];
7919824e4adSPedro F. Giffuni 	uint8_t hash_version;
7929824e4adSPedro F. Giffuni 	char *newdirblock = NULL;
7939824e4adSPedro F. Giffuni 	char *newidxblock = NULL;
7949824e4adSPedro F. Giffuni 	struct ext2fs_htree_node *dst_node;
7959824e4adSPedro F. Giffuni 	struct ext2fs_htree_entry *dst_entries;
7969824e4adSPedro F. Giffuni 	struct ext2fs_htree_entry *root_entires;
7979824e4adSPedro F. Giffuni 	struct buf *dst_bp = NULL;
7989824e4adSPedro F. Giffuni 	int error, write_bp = 0, write_dst_bp = 0, write_info = 0;
7999824e4adSPedro F. Giffuni 
8009824e4adSPedro F. Giffuni 	ip = VTOI(dvp);
8019824e4adSPedro F. Giffuni 	m_fs = ip->i_e2fs;
8029824e4adSPedro F. Giffuni 	fs = m_fs->e2fs;
8039824e4adSPedro F. Giffuni 	blksize = m_fs->e2fs_bsize;
8049824e4adSPedro F. Giffuni 
8059824e4adSPedro F. Giffuni 	if (ip->i_count != 0)
8069824e4adSPedro F. Giffuni 		return ext2_add_entry(dvp, entry);
8079824e4adSPedro F. Giffuni 
8089824e4adSPedro F. Giffuni 	/* Target directory block is full, split it */
8099824e4adSPedro F. Giffuni 	memset(&info, 0, sizeof(info));
8109824e4adSPedro F. Giffuni 	error = ext2_htree_find_leaf(ip, entry->e2d_name, entry->e2d_namlen,
8119824e4adSPedro F. Giffuni 	    &dirhash, &hash_version, &info);
8129824e4adSPedro F. Giffuni 	if (error)
8139824e4adSPedro F. Giffuni 		return (error);
8149824e4adSPedro F. Giffuni 
8159824e4adSPedro F. Giffuni 	entries = info.h_levels[info.h_levels_num - 1].h_entries;
8169824e4adSPedro F. Giffuni 	ent_num = ext2_htree_get_count(entries);
8179824e4adSPedro F. Giffuni 	if (ent_num == ext2_htree_get_limit(entries)) {
8189824e4adSPedro F. Giffuni 		/* Split the index node. */
8199824e4adSPedro F. Giffuni 		root_entires = info.h_levels[0].h_entries;
8209824e4adSPedro F. Giffuni 		newidxblock = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
8219824e4adSPedro F. Giffuni 		dst_node = (struct ext2fs_htree_node *)newidxblock;
8229824e4adSPedro F. Giffuni 		memset(&dst_node->h_fake_dirent, 0,
8239824e4adSPedro F. Giffuni 		    sizeof(dst_node->h_fake_dirent));
824cd3acfe7SFedor Uporov 		dst_node->h_fake_dirent.e2d_reclen = htole16(blksize);
8259824e4adSPedro F. Giffuni 
8269824e4adSPedro F. Giffuni 		cursize = roundup(ip->i_size, blksize);
8279824e4adSPedro F. Giffuni 		dirsize = cursize + blksize;
8289824e4adSPedro F. Giffuni 		blknum = dirsize / blksize - 1;
82917c7b27fSFedor Uporov 		ext2_dx_csum_set(ip, (struct ext2fs_direct_2 *)newidxblock);
8309824e4adSPedro F. Giffuni 		error = ext2_htree_append_block(dvp, newidxblock,
8319824e4adSPedro F. Giffuni 		    cnp, blksize);
8329824e4adSPedro F. Giffuni 		if (error)
8339824e4adSPedro F. Giffuni 			goto finish;
8349824e4adSPedro F. Giffuni 		error = ext2_blkatoff(dvp, cursize, NULL, &dst_bp);
8359824e4adSPedro F. Giffuni 		if (error)
8369824e4adSPedro F. Giffuni 			goto finish;
8379824e4adSPedro F. Giffuni 		dst_node = (struct ext2fs_htree_node *)dst_bp->b_data;
8389824e4adSPedro F. Giffuni 		dst_entries = dst_node->h_entries;
8399824e4adSPedro F. Giffuni 
8409824e4adSPedro F. Giffuni 		if (info.h_levels_num == 2) {
8419824e4adSPedro F. Giffuni 			uint16_t src_ent_num, dst_ent_num;
8429824e4adSPedro F. Giffuni 
8439824e4adSPedro F. Giffuni 			if (ext2_htree_get_count(root_entires) ==
8449824e4adSPedro F. Giffuni 			    ext2_htree_get_limit(root_entires)) {
845ebc94b66SFedor Uporov 				SDT_PROBE2(ext2fs, , trace, htree, 1,
846ebc94b66SFedor Uporov 				    "directory index is full");
8479824e4adSPedro F. Giffuni 				error = EIO;
8489824e4adSPedro F. Giffuni 				goto finish;
8499824e4adSPedro F. Giffuni 			}
8509824e4adSPedro F. Giffuni 
8519824e4adSPedro F. Giffuni 			src_ent_num = ent_num / 2;
8529824e4adSPedro F. Giffuni 			dst_ent_num = ent_num - src_ent_num;
8539824e4adSPedro F. Giffuni 			split_hash = ext2_htree_get_hash(entries + src_ent_num);
8549824e4adSPedro F. Giffuni 
8559824e4adSPedro F. Giffuni 			/* Move half of index entries to the new index node */
8569824e4adSPedro F. Giffuni 			memcpy(dst_entries, entries + src_ent_num,
8579824e4adSPedro F. Giffuni 			    dst_ent_num * sizeof(struct ext2fs_htree_entry));
8589824e4adSPedro F. Giffuni 			ext2_htree_set_count(entries, src_ent_num);
8599824e4adSPedro F. Giffuni 			ext2_htree_set_count(dst_entries, dst_ent_num);
8609824e4adSPedro F. Giffuni 			ext2_htree_set_limit(dst_entries,
8619824e4adSPedro F. Giffuni 			    ext2_htree_node_limit(ip));
8629824e4adSPedro F. Giffuni 
8639824e4adSPedro F. Giffuni 			if (info.h_levels[1].h_entry >= entries + src_ent_num) {
8649824e4adSPedro F. Giffuni 				struct buf *tmp = info.h_levels[1].h_bp;
865bf9a211dSPedro F. Giffuni 
8669824e4adSPedro F. Giffuni 				info.h_levels[1].h_bp = dst_bp;
8679824e4adSPedro F. Giffuni 				dst_bp = tmp;
8689824e4adSPedro F. Giffuni 
8699824e4adSPedro F. Giffuni 				info.h_levels[1].h_entry =
8709824e4adSPedro F. Giffuni 				    info.h_levels[1].h_entry -
8719824e4adSPedro F. Giffuni 				    (entries + src_ent_num) +
8729824e4adSPedro F. Giffuni 				    dst_entries;
8739824e4adSPedro F. Giffuni 				info.h_levels[1].h_entries = dst_entries;
8749824e4adSPedro F. Giffuni 			}
8759824e4adSPedro F. Giffuni 			ext2_htree_insert_entry_to_level(&info.h_levels[0],
8769824e4adSPedro F. Giffuni 			    split_hash, blknum);
8779824e4adSPedro F. Giffuni 
8789824e4adSPedro F. Giffuni 			/* Write new index node to disk */
8796d4a4ed7SFedor Uporov 			ext2_dx_csum_set(ip,
8806d4a4ed7SFedor Uporov 			    (struct ext2fs_direct_2 *)dst_bp->b_data);
8819824e4adSPedro F. Giffuni 			error = bwrite(dst_bp);
8829824e4adSPedro F. Giffuni 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
8839824e4adSPedro F. Giffuni 			if (error)
8849824e4adSPedro F. Giffuni 				goto finish;
8859824e4adSPedro F. Giffuni 			write_dst_bp = 1;
8869824e4adSPedro F. Giffuni 		} else {
8879824e4adSPedro F. Giffuni 			/* Create second level for htree index */
8889824e4adSPedro F. Giffuni 			struct ext2fs_htree_root *idx_root;
8899824e4adSPedro F. Giffuni 
8909824e4adSPedro F. Giffuni 			memcpy(dst_entries, entries,
8919824e4adSPedro F. Giffuni 			    ent_num * sizeof(struct ext2fs_htree_entry));
8929824e4adSPedro F. Giffuni 			ext2_htree_set_limit(dst_entries,
8939824e4adSPedro F. Giffuni 			    ext2_htree_node_limit(ip));
8949824e4adSPedro F. Giffuni 
8959824e4adSPedro F. Giffuni 			idx_root = (struct ext2fs_htree_root *)
8969824e4adSPedro F. Giffuni 			    info.h_levels[0].h_bp->b_data;
8979824e4adSPedro F. Giffuni 			idx_root->h_info.h_ind_levels = 1;
8989824e4adSPedro F. Giffuni 
8999824e4adSPedro F. Giffuni 			ext2_htree_set_count(entries, 1);
9009824e4adSPedro F. Giffuni 			ext2_htree_set_block(entries, blknum);
9019824e4adSPedro F. Giffuni 
9029824e4adSPedro F. Giffuni 			info.h_levels_num = 2;
9039824e4adSPedro F. Giffuni 			info.h_levels[1].h_entries = dst_entries;
9049824e4adSPedro F. Giffuni 			info.h_levels[1].h_entry = info.h_levels[0].h_entry -
9059824e4adSPedro F. Giffuni 			    info.h_levels[0].h_entries + dst_entries;
9069824e4adSPedro F. Giffuni 			info.h_levels[1].h_bp = dst_bp;
9079824e4adSPedro F. Giffuni 			dst_bp = NULL;
9089824e4adSPedro F. Giffuni 		}
9099824e4adSPedro F. Giffuni 	}
9109824e4adSPedro F. Giffuni 
9119824e4adSPedro F. Giffuni 	leaf_node = info.h_levels[info.h_levels_num - 1].h_entry;
9129824e4adSPedro F. Giffuni 	blknum = ext2_htree_get_block(leaf_node);
9139824e4adSPedro F. Giffuni 	error = ext2_blkatoff(dvp, blknum * blksize, NULL, &bp);
9149824e4adSPedro F. Giffuni 	if (error)
9159824e4adSPedro F. Giffuni 		goto finish;
9169824e4adSPedro F. Giffuni 
9179824e4adSPedro F. Giffuni 	/* Split target directory block */
9189824e4adSPedro F. Giffuni 	newdirblock = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
919cd3acfe7SFedor Uporov 	ext2_get_hash_seed(fs, hash_seed);
9206d4a4ed7SFedor Uporov 	ext2_htree_split_dirblock(ip, (char *)bp->b_data, newdirblock, blksize,
921cd3acfe7SFedor Uporov 	    hash_seed, hash_version, &split_hash, entry);
9229824e4adSPedro F. Giffuni 	cursize = roundup(ip->i_size, blksize);
9239824e4adSPedro F. Giffuni 	dirsize = cursize + blksize;
9249824e4adSPedro F. Giffuni 	blknum = dirsize / blksize - 1;
9259824e4adSPedro F. Giffuni 
9269824e4adSPedro F. Giffuni 	/* Add index entry for the new directory block */
9279824e4adSPedro F. Giffuni 	ext2_htree_insert_entry(&info, split_hash, blknum);
9289824e4adSPedro F. Giffuni 
9299824e4adSPedro F. Giffuni 	/* Write the new directory block to the end of the directory */
9306d4a4ed7SFedor Uporov 	ext2_dirent_csum_set(ip, (struct ext2fs_direct_2 *)newdirblock);
9319824e4adSPedro F. Giffuni 	error = ext2_htree_append_block(dvp, newdirblock, cnp, blksize);
9329824e4adSPedro F. Giffuni 	if (error)
9339824e4adSPedro F. Giffuni 		goto finish;
9349824e4adSPedro F. Giffuni 
9359824e4adSPedro F. Giffuni 	/* Write the target directory block */
9366d4a4ed7SFedor Uporov 	ext2_dirent_csum_set(ip, (struct ext2fs_direct_2 *)bp->b_data);
9379824e4adSPedro F. Giffuni 	error = bwrite(bp);
9389824e4adSPedro F. Giffuni 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
9399824e4adSPedro F. Giffuni 	if (error)
9409824e4adSPedro F. Giffuni 		goto finish;
9419824e4adSPedro F. Giffuni 	write_bp = 1;
9429824e4adSPedro F. Giffuni 
9439824e4adSPedro F. Giffuni 	/* Write the index block */
9446d4a4ed7SFedor Uporov 	error = ext2_htree_writebuf(ip, &info);
9459824e4adSPedro F. Giffuni 	if (!error)
9469824e4adSPedro F. Giffuni 		write_info = 1;
9479824e4adSPedro F. Giffuni 
9489824e4adSPedro F. Giffuni finish:
9499824e4adSPedro F. Giffuni 	if (dst_bp != NULL && !write_dst_bp)
9509824e4adSPedro F. Giffuni 		brelse(dst_bp);
9519824e4adSPedro F. Giffuni 	if (bp != NULL && !write_bp)
9529824e4adSPedro F. Giffuni 		brelse(bp);
9539824e4adSPedro F. Giffuni 	if (newdirblock != NULL)
9549824e4adSPedro F. Giffuni 		free(newdirblock, M_TEMP);
9559824e4adSPedro F. Giffuni 	if (newidxblock != NULL)
9569824e4adSPedro F. Giffuni 		free(newidxblock, M_TEMP);
9579824e4adSPedro F. Giffuni 	if (!write_info)
9589824e4adSPedro F. Giffuni 		ext2_htree_release(&info);
9599824e4adSPedro F. Giffuni 	return (error);
9609824e4adSPedro F. Giffuni }
961