1cfe60390STomohiro Kusumi /*-
2cfe60390STomohiro Kusumi * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3cfe60390STomohiro Kusumi *
4cfe60390STomohiro Kusumi * Copyright (c) 2010, 2012 Zheng Liu <lz@freebsd.org>
5cfe60390STomohiro Kusumi * Copyright (c) 2012, Vyacheslav Matyushin
6cfe60390STomohiro Kusumi * All rights reserved.
7cfe60390STomohiro Kusumi *
8cfe60390STomohiro Kusumi * Redistribution and use in source and binary forms, with or without
9cfe60390STomohiro Kusumi * modification, are permitted provided that the following conditions
10cfe60390STomohiro Kusumi * are met:
11cfe60390STomohiro Kusumi * 1. Redistributions of source code must retain the above copyright
12cfe60390STomohiro Kusumi * notice, this list of conditions and the following disclaimer.
13cfe60390STomohiro Kusumi * 2. Redistributions in binary form must reproduce the above copyright
14cfe60390STomohiro Kusumi * notice, this list of conditions and the following disclaimer in the
15cfe60390STomohiro Kusumi * documentation and/or other materials provided with the distribution.
16cfe60390STomohiro Kusumi *
17cfe60390STomohiro Kusumi * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18cfe60390STomohiro Kusumi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19cfe60390STomohiro Kusumi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20cfe60390STomohiro Kusumi * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21cfe60390STomohiro Kusumi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22cfe60390STomohiro Kusumi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23cfe60390STomohiro Kusumi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24cfe60390STomohiro Kusumi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25cfe60390STomohiro Kusumi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26cfe60390STomohiro Kusumi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27cfe60390STomohiro Kusumi * SUCH DAMAGE.
28cfe60390STomohiro Kusumi *
29cfe60390STomohiro Kusumi * $FreeBSD$
30cfe60390STomohiro Kusumi */
31cfe60390STomohiro Kusumi
32cfe60390STomohiro Kusumi #include <sys/param.h>
33cfe60390STomohiro Kusumi #include <sys/endian.h>
34cfe60390STomohiro Kusumi #include <sys/systm.h>
35cfe60390STomohiro Kusumi #include <sys/namei.h>
36cfe60390STomohiro Kusumi #include <sys/bio.h>
37cfe60390STomohiro Kusumi #include <sys/buf.h>
38cfe60390STomohiro Kusumi #include <sys/endian.h>
39cfe60390STomohiro Kusumi #include <sys/mount.h>
40cfe60390STomohiro Kusumi #include <sys/vnode.h>
41cfe60390STomohiro Kusumi #include <sys/malloc.h>
42cfe60390STomohiro Kusumi #include <sys/dirent.h>
43cfe60390STomohiro Kusumi #include <sys/sysctl.h>
44cfe60390STomohiro Kusumi #include <sys/uio.h>
45cfe60390STomohiro Kusumi
46cfe60390STomohiro Kusumi #include <vfs/ufs/dir.h>
47cfe60390STomohiro Kusumi
48cfe60390STomohiro Kusumi #include <vfs/ext2fs/fs.h>
49cfe60390STomohiro Kusumi #include <vfs/ext2fs/inode.h>
50cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_mount.h>
51cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2fs.h>
52cfe60390STomohiro Kusumi #include <vfs/ext2fs/fs.h>
53cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_extern.h>
54cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_dinode.h>
55cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_dir.h>
56cfe60390STomohiro Kusumi #include <vfs/ext2fs/htree.h>
57cfe60390STomohiro Kusumi
58cfe60390STomohiro Kusumi SDT_PROVIDER_DECLARE(ext2fs);
59cfe60390STomohiro Kusumi /*
60cfe60390STomohiro Kusumi * ext2fs trace probe:
61cfe60390STomohiro Kusumi * arg0: verbosity. Higher numbers give more verbose messages
62cfe60390STomohiro Kusumi * arg1: Textual message
63cfe60390STomohiro Kusumi */
64cfe60390STomohiro Kusumi SDT_PROBE_DEFINE2(ext2fs, , trace, htree, "int", "char*");
65cfe60390STomohiro Kusumi
66cfe60390STomohiro Kusumi static void ext2_append_entry(char *block, uint32_t blksize,
67cfe60390STomohiro Kusumi struct ext2fs_direct_2 *last_entry,
68cfe60390STomohiro Kusumi struct ext2fs_direct_2 *new_entry, int csum_size);
69cfe60390STomohiro Kusumi static int ext2_htree_append_block(struct vnode *vp, char *data,
70cfe60390STomohiro Kusumi struct componentname *cnp, uint32_t blksize);
71cfe60390STomohiro Kusumi static int ext2_htree_check_next(struct inode *ip, uint32_t hash,
72cfe60390STomohiro Kusumi const char *name, struct ext2fs_htree_lookup_info *info);
73cfe60390STomohiro Kusumi static int ext2_htree_cmp_sort_entry(const void *e1, const void *e2);
74cfe60390STomohiro Kusumi static int ext2_htree_find_leaf(struct inode *ip, const char *name,
75cfe60390STomohiro Kusumi int namelen, uint32_t *hash, uint8_t *hash_version,
76cfe60390STomohiro Kusumi struct ext2fs_htree_lookup_info *info);
77cfe60390STomohiro Kusumi static uint32_t ext2_htree_get_block(struct ext2fs_htree_entry *ep);
78cfe60390STomohiro Kusumi static uint16_t ext2_htree_get_count(struct ext2fs_htree_entry *ep);
79cfe60390STomohiro Kusumi static uint32_t ext2_htree_get_hash(struct ext2fs_htree_entry *ep);
80cfe60390STomohiro Kusumi static uint16_t ext2_htree_get_limit(struct ext2fs_htree_entry *ep);
81cfe60390STomohiro Kusumi static void ext2_htree_insert_entry_to_level(struct ext2fs_htree_lookup_level *level,
82cfe60390STomohiro Kusumi uint32_t hash, uint32_t blk);
83cfe60390STomohiro Kusumi static void ext2_htree_insert_entry(struct ext2fs_htree_lookup_info *info,
84cfe60390STomohiro Kusumi uint32_t hash, uint32_t blk);
85cfe60390STomohiro Kusumi static uint32_t ext2_htree_node_limit(struct inode *ip);
86cfe60390STomohiro Kusumi static void ext2_htree_set_block(struct ext2fs_htree_entry *ep,
87cfe60390STomohiro Kusumi uint32_t blk);
88cfe60390STomohiro Kusumi static void ext2_htree_set_count(struct ext2fs_htree_entry *ep,
89cfe60390STomohiro Kusumi uint16_t cnt);
90cfe60390STomohiro Kusumi static void ext2_htree_set_hash(struct ext2fs_htree_entry *ep,
91cfe60390STomohiro Kusumi uint32_t hash);
92cfe60390STomohiro Kusumi static void ext2_htree_set_limit(struct ext2fs_htree_entry *ep,
93cfe60390STomohiro Kusumi uint16_t limit);
94cfe60390STomohiro Kusumi static int ext2_htree_split_dirblock(struct inode *ip,
95cfe60390STomohiro Kusumi char *block1, char *block2, uint32_t blksize,
96cfe60390STomohiro Kusumi uint32_t *hash_seed, uint8_t hash_version,
97cfe60390STomohiro Kusumi uint32_t *split_hash, struct ext2fs_direct_2 *entry);
98cfe60390STomohiro Kusumi static void ext2_htree_release(struct ext2fs_htree_lookup_info *info);
99cfe60390STomohiro Kusumi static uint32_t ext2_htree_root_limit(struct inode *ip, int len);
100cfe60390STomohiro Kusumi static int ext2_htree_writebuf(struct inode *ip,
101cfe60390STomohiro Kusumi struct ext2fs_htree_lookup_info *info);
102cfe60390STomohiro Kusumi
103cfe60390STomohiro Kusumi int
ext2_htree_has_idx(struct inode * ip)104cfe60390STomohiro Kusumi ext2_htree_has_idx(struct inode *ip)
105cfe60390STomohiro Kusumi {
106cfe60390STomohiro Kusumi if (EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_DIRHASHINDEX) &&
107cfe60390STomohiro Kusumi ip->i_flag & IN_E3INDEX)
108cfe60390STomohiro Kusumi return (1);
109cfe60390STomohiro Kusumi else
110cfe60390STomohiro Kusumi return (0);
111cfe60390STomohiro Kusumi }
112cfe60390STomohiro Kusumi
113cfe60390STomohiro Kusumi static int
ext2_htree_check_next(struct inode * ip,uint32_t hash,const char * name,struct ext2fs_htree_lookup_info * info)114cfe60390STomohiro Kusumi ext2_htree_check_next(struct inode *ip, uint32_t hash, const char *name,
115cfe60390STomohiro Kusumi struct ext2fs_htree_lookup_info *info)
116cfe60390STomohiro Kusumi {
117cfe60390STomohiro Kusumi struct vnode *vp = ITOV(ip);
118cfe60390STomohiro Kusumi struct ext2fs_htree_lookup_level *level;
119cfe60390STomohiro Kusumi struct buf *bp;
120cfe60390STomohiro Kusumi uint32_t next_hash;
121cfe60390STomohiro Kusumi int idx = info->h_levels_num - 1;
122cfe60390STomohiro Kusumi int levels = 0;
123cfe60390STomohiro Kusumi
124cfe60390STomohiro Kusumi do {
125cfe60390STomohiro Kusumi level = &info->h_levels[idx];
126cfe60390STomohiro Kusumi level->h_entry++;
127cfe60390STomohiro Kusumi if (level->h_entry < level->h_entries +
128cfe60390STomohiro Kusumi ext2_htree_get_count(level->h_entries))
129cfe60390STomohiro Kusumi break;
130cfe60390STomohiro Kusumi if (idx == 0)
131cfe60390STomohiro Kusumi return (0);
132cfe60390STomohiro Kusumi idx--;
133cfe60390STomohiro Kusumi levels++;
134cfe60390STomohiro Kusumi } while (1);
135cfe60390STomohiro Kusumi
136cfe60390STomohiro Kusumi next_hash = ext2_htree_get_hash(level->h_entry);
137cfe60390STomohiro Kusumi if ((hash & 1) == 0) {
138cfe60390STomohiro Kusumi if (hash != (next_hash & ~1))
139cfe60390STomohiro Kusumi return (0);
140cfe60390STomohiro Kusumi }
141cfe60390STomohiro Kusumi
142cfe60390STomohiro Kusumi while (levels > 0) {
143cfe60390STomohiro Kusumi levels--;
144cfe60390STomohiro Kusumi if (ext2_blkatoff(vp, ext2_htree_get_block(level->h_entry) *
145cfe60390STomohiro Kusumi ip->i_e2fs->e2fs_bsize, NULL, &bp) != 0)
146cfe60390STomohiro Kusumi return (0);
147cfe60390STomohiro Kusumi level = &info->h_levels[idx + 1];
148*e5b38eb5STomohiro Kusumi brelse(level->h_bp);
149cfe60390STomohiro Kusumi level->h_bp = bp;
150cfe60390STomohiro Kusumi level->h_entry = level->h_entries =
151cfe60390STomohiro Kusumi ((struct ext2fs_htree_node *)bp->b_data)->h_entries;
152cfe60390STomohiro Kusumi }
153cfe60390STomohiro Kusumi
154cfe60390STomohiro Kusumi return (1);
155cfe60390STomohiro Kusumi }
156cfe60390STomohiro Kusumi
157cfe60390STomohiro Kusumi static uint32_t
ext2_htree_get_block(struct ext2fs_htree_entry * ep)158cfe60390STomohiro Kusumi ext2_htree_get_block(struct ext2fs_htree_entry *ep)
159cfe60390STomohiro Kusumi {
160cfe60390STomohiro Kusumi return (le32toh(ep->h_blk) & 0x00FFFFFF);
161cfe60390STomohiro Kusumi }
162cfe60390STomohiro Kusumi
163cfe60390STomohiro Kusumi static void
ext2_htree_set_block(struct ext2fs_htree_entry * ep,uint32_t blk)164cfe60390STomohiro Kusumi ext2_htree_set_block(struct ext2fs_htree_entry *ep, uint32_t blk)
165cfe60390STomohiro Kusumi {
166cfe60390STomohiro Kusumi ep->h_blk = htole32(blk);
167cfe60390STomohiro Kusumi }
168cfe60390STomohiro Kusumi
169cfe60390STomohiro Kusumi static uint16_t
ext2_htree_get_count(struct ext2fs_htree_entry * ep)170cfe60390STomohiro Kusumi ext2_htree_get_count(struct ext2fs_htree_entry *ep)
171cfe60390STomohiro Kusumi {
172cfe60390STomohiro Kusumi return (le16toh(((struct ext2fs_htree_count *)(ep))->h_entries_num));
173cfe60390STomohiro Kusumi }
174cfe60390STomohiro Kusumi
175cfe60390STomohiro Kusumi static void
ext2_htree_set_count(struct ext2fs_htree_entry * ep,uint16_t cnt)176cfe60390STomohiro Kusumi ext2_htree_set_count(struct ext2fs_htree_entry *ep, uint16_t cnt)
177cfe60390STomohiro Kusumi {
178cfe60390STomohiro Kusumi ((struct ext2fs_htree_count *)(ep))->h_entries_num = htole16(cnt);
179cfe60390STomohiro Kusumi }
180cfe60390STomohiro Kusumi
181cfe60390STomohiro Kusumi static uint32_t
ext2_htree_get_hash(struct ext2fs_htree_entry * ep)182cfe60390STomohiro Kusumi ext2_htree_get_hash(struct ext2fs_htree_entry *ep)
183cfe60390STomohiro Kusumi {
184cfe60390STomohiro Kusumi return (le32toh(ep->h_hash));
185cfe60390STomohiro Kusumi }
186cfe60390STomohiro Kusumi
187cfe60390STomohiro Kusumi static uint16_t
ext2_htree_get_limit(struct ext2fs_htree_entry * ep)188cfe60390STomohiro Kusumi ext2_htree_get_limit(struct ext2fs_htree_entry *ep)
189cfe60390STomohiro Kusumi {
190cfe60390STomohiro Kusumi return (le16toh(((struct ext2fs_htree_count *)(ep))->h_entries_max));
191cfe60390STomohiro Kusumi }
192cfe60390STomohiro Kusumi
193cfe60390STomohiro Kusumi static void
ext2_htree_set_hash(struct ext2fs_htree_entry * ep,uint32_t hash)194cfe60390STomohiro Kusumi ext2_htree_set_hash(struct ext2fs_htree_entry *ep, uint32_t hash)
195cfe60390STomohiro Kusumi {
196cfe60390STomohiro Kusumi ep->h_hash = htole32(hash);
197cfe60390STomohiro Kusumi }
198cfe60390STomohiro Kusumi
199cfe60390STomohiro Kusumi static void
ext2_htree_set_limit(struct ext2fs_htree_entry * ep,uint16_t limit)200cfe60390STomohiro Kusumi ext2_htree_set_limit(struct ext2fs_htree_entry *ep, uint16_t limit)
201cfe60390STomohiro Kusumi {
202cfe60390STomohiro Kusumi ((struct ext2fs_htree_count *)(ep))->h_entries_max = htole16(limit);
203cfe60390STomohiro Kusumi }
204cfe60390STomohiro Kusumi
205cfe60390STomohiro Kusumi static void
ext2_htree_release(struct ext2fs_htree_lookup_info * info)206cfe60390STomohiro Kusumi ext2_htree_release(struct ext2fs_htree_lookup_info *info)
207cfe60390STomohiro Kusumi {
208cfe60390STomohiro Kusumi u_int i;
209cfe60390STomohiro Kusumi
210cfe60390STomohiro Kusumi for (i = 0; i < info->h_levels_num; i++) {
211cfe60390STomohiro Kusumi struct buf *bp = info->h_levels[i].h_bp;
212cfe60390STomohiro Kusumi
213cfe60390STomohiro Kusumi if (bp != NULL)
214*e5b38eb5STomohiro Kusumi brelse(bp);
215cfe60390STomohiro Kusumi }
216cfe60390STomohiro Kusumi }
217cfe60390STomohiro Kusumi
218cfe60390STomohiro Kusumi static uint32_t
ext2_htree_root_limit(struct inode * ip,int len)219cfe60390STomohiro Kusumi ext2_htree_root_limit(struct inode *ip, int len)
220cfe60390STomohiro Kusumi {
221cfe60390STomohiro Kusumi struct m_ext2fs *fs;
222cfe60390STomohiro Kusumi uint32_t space;
223cfe60390STomohiro Kusumi
224cfe60390STomohiro Kusumi fs = ip->i_e2fs;
225cfe60390STomohiro Kusumi space = ip->i_e2fs->e2fs_bsize - EXT2_DIR_REC_LEN(1) -
226cfe60390STomohiro Kusumi EXT2_DIR_REC_LEN(2) - len;
227cfe60390STomohiro Kusumi
228cfe60390STomohiro Kusumi if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
229cfe60390STomohiro Kusumi space -= sizeof(struct ext2fs_htree_tail);
230cfe60390STomohiro Kusumi
231cfe60390STomohiro Kusumi return (space / sizeof(struct ext2fs_htree_entry));
232cfe60390STomohiro Kusumi }
233cfe60390STomohiro Kusumi
234cfe60390STomohiro Kusumi static uint32_t
ext2_htree_node_limit(struct inode * ip)235cfe60390STomohiro Kusumi ext2_htree_node_limit(struct inode *ip)
236cfe60390STomohiro Kusumi {
237cfe60390STomohiro Kusumi struct m_ext2fs *fs;
238cfe60390STomohiro Kusumi uint32_t space;
239cfe60390STomohiro Kusumi
240cfe60390STomohiro Kusumi fs = ip->i_e2fs;
241cfe60390STomohiro Kusumi space = fs->e2fs_bsize - EXT2_DIR_REC_LEN(0);
242cfe60390STomohiro Kusumi
243cfe60390STomohiro Kusumi if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
244cfe60390STomohiro Kusumi space -= sizeof(struct ext2fs_htree_tail);
245cfe60390STomohiro Kusumi
246cfe60390STomohiro Kusumi return (space / sizeof(struct ext2fs_htree_entry));
247cfe60390STomohiro Kusumi }
248cfe60390STomohiro Kusumi
249cfe60390STomohiro Kusumi static void
ext2_get_hash_seed(struct ext2fs * es,uint32_t * seed)250cfe60390STomohiro Kusumi ext2_get_hash_seed(struct ext2fs* es, uint32_t* seed)
251cfe60390STomohiro Kusumi {
252cfe60390STomohiro Kusumi
253cfe60390STomohiro Kusumi for (int i = 0; i < 4; i++)
254cfe60390STomohiro Kusumi seed[i] = le32toh(es->e3fs_hash_seed[i]);
255cfe60390STomohiro Kusumi }
256cfe60390STomohiro Kusumi
257cfe60390STomohiro Kusumi 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)258cfe60390STomohiro Kusumi ext2_htree_find_leaf(struct inode *ip, const char *name, int namelen,
259cfe60390STomohiro Kusumi uint32_t *hash, uint8_t *hash_ver,
260cfe60390STomohiro Kusumi struct ext2fs_htree_lookup_info *info)
261cfe60390STomohiro Kusumi {
262cfe60390STomohiro Kusumi struct vnode *vp;
263cfe60390STomohiro Kusumi struct ext2fs *fs;
264cfe60390STomohiro Kusumi struct m_ext2fs *m_fs;
265cfe60390STomohiro Kusumi struct buf *bp = NULL;
266cfe60390STomohiro Kusumi struct ext2fs_htree_root *rootp;
267cfe60390STomohiro Kusumi struct ext2fs_htree_entry *entp, *start, *end, *middle, *found;
268cfe60390STomohiro Kusumi struct ext2fs_htree_lookup_level *level_info;
269cfe60390STomohiro Kusumi uint32_t hash_major = 0, hash_minor = 0;
270cfe60390STomohiro Kusumi uint32_t levels, cnt;
271cfe60390STomohiro Kusumi uint32_t hash_seed[4];
272cfe60390STomohiro Kusumi uint8_t hash_version;
273cfe60390STomohiro Kusumi
274cfe60390STomohiro Kusumi if (name == NULL || info == NULL)
275cfe60390STomohiro Kusumi return (-1);
276cfe60390STomohiro Kusumi
277cfe60390STomohiro Kusumi vp = ITOV(ip);
278cfe60390STomohiro Kusumi fs = ip->i_e2fs->e2fs;
279cfe60390STomohiro Kusumi m_fs = ip->i_e2fs;
280cfe60390STomohiro Kusumi
281cfe60390STomohiro Kusumi if (ext2_blkatoff(vp, 0, NULL, &bp) != 0)
282cfe60390STomohiro Kusumi return (-1);
283cfe60390STomohiro Kusumi
284cfe60390STomohiro Kusumi info->h_levels_num = 1;
285cfe60390STomohiro Kusumi info->h_levels[0].h_bp = bp;
286cfe60390STomohiro Kusumi rootp = (struct ext2fs_htree_root *)bp->b_data;
287cfe60390STomohiro Kusumi if (rootp->h_info.h_hash_version != EXT2_HTREE_LEGACY &&
288cfe60390STomohiro Kusumi rootp->h_info.h_hash_version != EXT2_HTREE_HALF_MD4 &&
289cfe60390STomohiro Kusumi rootp->h_info.h_hash_version != EXT2_HTREE_TEA)
290cfe60390STomohiro Kusumi goto error;
291cfe60390STomohiro Kusumi
292cfe60390STomohiro Kusumi hash_version = rootp->h_info.h_hash_version;
293cfe60390STomohiro Kusumi if (hash_version <= EXT2_HTREE_TEA)
294cfe60390STomohiro Kusumi hash_version += m_fs->e2fs_uhash;
295cfe60390STomohiro Kusumi *hash_ver = hash_version;
296cfe60390STomohiro Kusumi
297cfe60390STomohiro Kusumi ext2_get_hash_seed(fs, hash_seed);
298cfe60390STomohiro Kusumi ext2_htree_hash(name, namelen, hash_seed,
299cfe60390STomohiro Kusumi hash_version, &hash_major, &hash_minor);
300cfe60390STomohiro Kusumi *hash = hash_major;
301cfe60390STomohiro Kusumi
302cfe60390STomohiro Kusumi if ((levels = rootp->h_info.h_ind_levels) > 1)
303cfe60390STomohiro Kusumi goto error;
304cfe60390STomohiro Kusumi
305cfe60390STomohiro Kusumi entp = (struct ext2fs_htree_entry *)(((char *)&rootp->h_info) +
306cfe60390STomohiro Kusumi rootp->h_info.h_info_len);
307cfe60390STomohiro Kusumi
308cfe60390STomohiro Kusumi if (ext2_htree_get_limit(entp) !=
309cfe60390STomohiro Kusumi ext2_htree_root_limit(ip, rootp->h_info.h_info_len))
310cfe60390STomohiro Kusumi goto error;
311cfe60390STomohiro Kusumi
312cfe60390STomohiro Kusumi while (1) {
313cfe60390STomohiro Kusumi cnt = ext2_htree_get_count(entp);
314cfe60390STomohiro Kusumi if (cnt == 0 || cnt > ext2_htree_get_limit(entp))
315cfe60390STomohiro Kusumi goto error;
316cfe60390STomohiro Kusumi
317cfe60390STomohiro Kusumi start = entp + 1;
318cfe60390STomohiro Kusumi end = entp + cnt - 1;
319cfe60390STomohiro Kusumi while (start <= end) {
320cfe60390STomohiro Kusumi middle = start + (end - start) / 2;
321cfe60390STomohiro Kusumi if (ext2_htree_get_hash(middle) > hash_major)
322cfe60390STomohiro Kusumi end = middle - 1;
323cfe60390STomohiro Kusumi else
324cfe60390STomohiro Kusumi start = middle + 1;
325cfe60390STomohiro Kusumi }
326cfe60390STomohiro Kusumi found = start - 1;
327cfe60390STomohiro Kusumi
328cfe60390STomohiro Kusumi level_info = &(info->h_levels[info->h_levels_num - 1]);
329cfe60390STomohiro Kusumi level_info->h_bp = bp;
330cfe60390STomohiro Kusumi level_info->h_entries = entp;
331cfe60390STomohiro Kusumi level_info->h_entry = found;
332cfe60390STomohiro Kusumi if (levels == 0)
333cfe60390STomohiro Kusumi return (0);
334cfe60390STomohiro Kusumi levels--;
335cfe60390STomohiro Kusumi if (ext2_blkatoff(vp,
336cfe60390STomohiro Kusumi ext2_htree_get_block(found) * m_fs->e2fs_bsize,
337cfe60390STomohiro Kusumi NULL, &bp) != 0)
338cfe60390STomohiro Kusumi goto error;
339cfe60390STomohiro Kusumi entp = ((struct ext2fs_htree_node *)bp->b_data)->h_entries;
340cfe60390STomohiro Kusumi info->h_levels_num++;
341cfe60390STomohiro Kusumi info->h_levels[info->h_levels_num - 1].h_bp = bp;
342cfe60390STomohiro Kusumi }
343cfe60390STomohiro Kusumi
344cfe60390STomohiro Kusumi error:
345cfe60390STomohiro Kusumi ext2_htree_release(info);
346cfe60390STomohiro Kusumi return (-1);
347cfe60390STomohiro Kusumi }
348cfe60390STomohiro Kusumi
349cfe60390STomohiro Kusumi /*
350cfe60390STomohiro Kusumi * Try to lookup a directory entry in HTree index
351cfe60390STomohiro Kusumi */
352cfe60390STomohiro Kusumi 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)353cfe60390STomohiro Kusumi ext2_htree_lookup(struct inode *ip, const char *name, int namelen,
354cfe60390STomohiro Kusumi struct buf **bpp, int *entryoffp, doff_t *offp,
355cfe60390STomohiro Kusumi doff_t *prevoffp, doff_t *endusefulp,
356cfe60390STomohiro Kusumi struct ext2fs_searchslot *ss)
357cfe60390STomohiro Kusumi {
358cfe60390STomohiro Kusumi struct vnode *vp;
359cfe60390STomohiro Kusumi struct ext2fs_htree_lookup_info info;
360cfe60390STomohiro Kusumi struct ext2fs_htree_entry *leaf_node;
361cfe60390STomohiro Kusumi struct m_ext2fs *m_fs;
362cfe60390STomohiro Kusumi struct buf *bp;
363cfe60390STomohiro Kusumi uint32_t blk;
364cfe60390STomohiro Kusumi uint32_t dirhash;
365cfe60390STomohiro Kusumi uint32_t bsize;
366cfe60390STomohiro Kusumi uint8_t hash_version;
367cfe60390STomohiro Kusumi int search_next;
368cfe60390STomohiro Kusumi int found = 0;
369cfe60390STomohiro Kusumi
370cfe60390STomohiro Kusumi m_fs = ip->i_e2fs;
371cfe60390STomohiro Kusumi bsize = m_fs->e2fs_bsize;
372cfe60390STomohiro Kusumi vp = ITOV(ip);
373cfe60390STomohiro Kusumi
374cfe60390STomohiro Kusumi /* TODO: print error msg because we don't lookup '.' and '..' */
375cfe60390STomohiro Kusumi
376cfe60390STomohiro Kusumi memset(&info, 0, sizeof(info));
377cfe60390STomohiro Kusumi if (ext2_htree_find_leaf(ip, name, namelen, &dirhash,
378cfe60390STomohiro Kusumi &hash_version, &info))
379cfe60390STomohiro Kusumi return (-1);
380cfe60390STomohiro Kusumi
381cfe60390STomohiro Kusumi do {
382cfe60390STomohiro Kusumi leaf_node = info.h_levels[info.h_levels_num - 1].h_entry;
383cfe60390STomohiro Kusumi blk = ext2_htree_get_block(leaf_node);
384cfe60390STomohiro Kusumi if (ext2_blkatoff(vp, blk * bsize, NULL, &bp) != 0) {
385cfe60390STomohiro Kusumi ext2_htree_release(&info);
386cfe60390STomohiro Kusumi return (-1);
387cfe60390STomohiro Kusumi }
388cfe60390STomohiro Kusumi
389cfe60390STomohiro Kusumi *offp = blk * bsize;
390cfe60390STomohiro Kusumi *entryoffp = 0;
391cfe60390STomohiro Kusumi *prevoffp = blk * bsize;
392cfe60390STomohiro Kusumi *endusefulp = blk * bsize;
393cfe60390STomohiro Kusumi
394cfe60390STomohiro Kusumi if (ss->slotstatus == NONE) {
395cfe60390STomohiro Kusumi ss->slotoffset = -1;
396cfe60390STomohiro Kusumi ss->slotfreespace = 0;
397cfe60390STomohiro Kusumi }
398cfe60390STomohiro Kusumi
399cfe60390STomohiro Kusumi if (ext2_search_dirblock(ip, bp->b_data, &found,
400cfe60390STomohiro Kusumi name, namelen, entryoffp, offp, prevoffp,
401cfe60390STomohiro Kusumi endusefulp, ss) != 0) {
402*e5b38eb5STomohiro Kusumi brelse(bp);
403cfe60390STomohiro Kusumi ext2_htree_release(&info);
404cfe60390STomohiro Kusumi return (-1);
405cfe60390STomohiro Kusumi }
406cfe60390STomohiro Kusumi
407cfe60390STomohiro Kusumi if (found) {
408cfe60390STomohiro Kusumi *bpp = bp;
409cfe60390STomohiro Kusumi ext2_htree_release(&info);
410cfe60390STomohiro Kusumi return (0);
411cfe60390STomohiro Kusumi }
412cfe60390STomohiro Kusumi
413*e5b38eb5STomohiro Kusumi brelse(bp);
414cfe60390STomohiro Kusumi search_next = ext2_htree_check_next(ip, dirhash, name, &info);
415cfe60390STomohiro Kusumi } while (search_next);
416cfe60390STomohiro Kusumi
417cfe60390STomohiro Kusumi ext2_htree_release(&info);
418cfe60390STomohiro Kusumi return (ENOENT);
419cfe60390STomohiro Kusumi }
420cfe60390STomohiro Kusumi
421cfe60390STomohiro Kusumi static int
ext2_htree_append_block(struct vnode * vp,char * data,struct componentname * cnp,uint32_t blksize)422cfe60390STomohiro Kusumi ext2_htree_append_block(struct vnode *vp, char *data,
423cfe60390STomohiro Kusumi struct componentname *cnp, uint32_t blksize)
424cfe60390STomohiro Kusumi {
425cfe60390STomohiro Kusumi struct iovec aiov;
426cfe60390STomohiro Kusumi struct uio auio;
427cfe60390STomohiro Kusumi struct inode *dp = VTOI(vp);
428cfe60390STomohiro Kusumi uint64_t cursize, newsize;
429cfe60390STomohiro Kusumi int error;
430cfe60390STomohiro Kusumi
431cfe60390STomohiro Kusumi cursize = roundup(dp->i_size, blksize);
432cfe60390STomohiro Kusumi newsize = cursize + blksize;
433cfe60390STomohiro Kusumi
434cfe60390STomohiro Kusumi auio.uio_offset = cursize;
435cfe60390STomohiro Kusumi auio.uio_resid = blksize;
436cfe60390STomohiro Kusumi aiov.iov_len = blksize;
437cfe60390STomohiro Kusumi aiov.iov_base = data;
438cfe60390STomohiro Kusumi auio.uio_iov = &aiov;
439cfe60390STomohiro Kusumi auio.uio_iovcnt = 1;
440cfe60390STomohiro Kusumi auio.uio_rw = UIO_WRITE;
441cfe60390STomohiro Kusumi auio.uio_segflg = UIO_SYSSPACE;
442cfe60390STomohiro Kusumi error = VOP_WRITE(vp, &auio, IO_SYNC, cnp->cn_cred);
443cfe60390STomohiro Kusumi if (!error)
444cfe60390STomohiro Kusumi dp->i_size = newsize;
445cfe60390STomohiro Kusumi
446cfe60390STomohiro Kusumi return (error);
447cfe60390STomohiro Kusumi }
448cfe60390STomohiro Kusumi
449cfe60390STomohiro Kusumi static int
ext2_htree_writebuf(struct inode * ip,struct ext2fs_htree_lookup_info * info)450cfe60390STomohiro Kusumi ext2_htree_writebuf(struct inode* ip, struct ext2fs_htree_lookup_info *info)
451cfe60390STomohiro Kusumi {
452cfe60390STomohiro Kusumi int i, error;
453cfe60390STomohiro Kusumi
454cfe60390STomohiro Kusumi for (i = 0; i < info->h_levels_num; i++) {
455cfe60390STomohiro Kusumi struct buf *bp = info->h_levels[i].h_bp;
456cfe60390STomohiro Kusumi ext2_dx_csum_set(ip, (struct ext2fs_direct_2 *)bp->b_data);
457cfe60390STomohiro Kusumi error = bwrite(bp);
458cfe60390STomohiro Kusumi if (error)
459cfe60390STomohiro Kusumi return (error);
460cfe60390STomohiro Kusumi }
461cfe60390STomohiro Kusumi
462cfe60390STomohiro Kusumi return (0);
463cfe60390STomohiro Kusumi }
464cfe60390STomohiro Kusumi
465cfe60390STomohiro Kusumi static void
ext2_htree_insert_entry_to_level(struct ext2fs_htree_lookup_level * level,uint32_t hash,uint32_t blk)466cfe60390STomohiro Kusumi ext2_htree_insert_entry_to_level(struct ext2fs_htree_lookup_level *level,
467cfe60390STomohiro Kusumi uint32_t hash, uint32_t blk)
468cfe60390STomohiro Kusumi {
469cfe60390STomohiro Kusumi struct ext2fs_htree_entry *target;
470cfe60390STomohiro Kusumi int entries_num;
471cfe60390STomohiro Kusumi
472cfe60390STomohiro Kusumi target = level->h_entry + 1;
473cfe60390STomohiro Kusumi entries_num = ext2_htree_get_count(level->h_entries);
474cfe60390STomohiro Kusumi
475cfe60390STomohiro Kusumi memmove(target + 1, target, (char *)(level->h_entries + entries_num) -
476cfe60390STomohiro Kusumi (char *)target);
477cfe60390STomohiro Kusumi ext2_htree_set_block(target, blk);
478cfe60390STomohiro Kusumi ext2_htree_set_hash(target, hash);
479cfe60390STomohiro Kusumi ext2_htree_set_count(level->h_entries, entries_num + 1);
480cfe60390STomohiro Kusumi }
481cfe60390STomohiro Kusumi
482cfe60390STomohiro Kusumi /*
483cfe60390STomohiro Kusumi * Insert an index entry to the index node.
484cfe60390STomohiro Kusumi */
485cfe60390STomohiro Kusumi static void
ext2_htree_insert_entry(struct ext2fs_htree_lookup_info * info,uint32_t hash,uint32_t blk)486cfe60390STomohiro Kusumi ext2_htree_insert_entry(struct ext2fs_htree_lookup_info *info,
487cfe60390STomohiro Kusumi uint32_t hash, uint32_t blk)
488cfe60390STomohiro Kusumi {
489cfe60390STomohiro Kusumi struct ext2fs_htree_lookup_level *level;
490cfe60390STomohiro Kusumi
491cfe60390STomohiro Kusumi level = &info->h_levels[info->h_levels_num - 1];
492cfe60390STomohiro Kusumi ext2_htree_insert_entry_to_level(level, hash, blk);
493cfe60390STomohiro Kusumi }
494cfe60390STomohiro Kusumi
495cfe60390STomohiro Kusumi /*
496cfe60390STomohiro Kusumi * Compare two entry sort descriptors by name hash value.
497cfe60390STomohiro Kusumi * This is used together with qsort.
498cfe60390STomohiro Kusumi */
499cfe60390STomohiro Kusumi static int
ext2_htree_cmp_sort_entry(const void * e1,const void * e2)500cfe60390STomohiro Kusumi ext2_htree_cmp_sort_entry(const void *e1, const void *e2)
501cfe60390STomohiro Kusumi {
502cfe60390STomohiro Kusumi const struct ext2fs_htree_sort_entry *entry1, *entry2;
503cfe60390STomohiro Kusumi
504cfe60390STomohiro Kusumi entry1 = (const struct ext2fs_htree_sort_entry *)e1;
505cfe60390STomohiro Kusumi entry2 = (const struct ext2fs_htree_sort_entry *)e2;
506cfe60390STomohiro Kusumi
507cfe60390STomohiro Kusumi if (le32toh(entry1->h_hash) < le32toh(entry2->h_hash))
508cfe60390STomohiro Kusumi return (-1);
509cfe60390STomohiro Kusumi if (le32toh(entry1->h_hash) > le32toh(entry2->h_hash))
510cfe60390STomohiro Kusumi return (1);
511cfe60390STomohiro Kusumi return (0);
512cfe60390STomohiro Kusumi }
513cfe60390STomohiro Kusumi
514cfe60390STomohiro Kusumi /*
515cfe60390STomohiro Kusumi * Append an entry to the end of the directory block.
516cfe60390STomohiro Kusumi */
517cfe60390STomohiro Kusumi 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)518cfe60390STomohiro Kusumi ext2_append_entry(char *block, uint32_t blksize,
519cfe60390STomohiro Kusumi struct ext2fs_direct_2 *last_entry,
520cfe60390STomohiro Kusumi struct ext2fs_direct_2 *new_entry, int csum_size)
521cfe60390STomohiro Kusumi {
522cfe60390STomohiro Kusumi uint16_t entry_len;
523cfe60390STomohiro Kusumi
524cfe60390STomohiro Kusumi entry_len = EXT2_DIR_REC_LEN(last_entry->e2d_namlen);
525cfe60390STomohiro Kusumi last_entry->e2d_reclen = htole16(entry_len);
526cfe60390STomohiro Kusumi last_entry = (struct ext2fs_direct_2 *)((char *)last_entry + entry_len);
527cfe60390STomohiro Kusumi new_entry->e2d_reclen = htole16(block + blksize - (char *)last_entry -
528cfe60390STomohiro Kusumi csum_size);
529cfe60390STomohiro Kusumi memcpy(last_entry, new_entry, EXT2_DIR_REC_LEN(new_entry->e2d_namlen));
530cfe60390STomohiro Kusumi }
531cfe60390STomohiro Kusumi
532cfe60390STomohiro Kusumi /*
533cfe60390STomohiro Kusumi * Move half of entries from the old directory block to the new one.
534cfe60390STomohiro Kusumi */
535cfe60390STomohiro Kusumi 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)536cfe60390STomohiro Kusumi ext2_htree_split_dirblock(struct inode *ip, char *block1, char *block2,
537cfe60390STomohiro Kusumi uint32_t blksize, uint32_t *hash_seed, uint8_t hash_version,
538cfe60390STomohiro Kusumi uint32_t *split_hash, struct ext2fs_direct_2 *entry)
539cfe60390STomohiro Kusumi {
540cfe60390STomohiro Kusumi struct m_ext2fs *fs;
541cfe60390STomohiro Kusumi int entry_cnt = 0;
542cfe60390STomohiro Kusumi int size = 0, csum_size = 0;
543cfe60390STomohiro Kusumi int i, k;
544cfe60390STomohiro Kusumi uint32_t offset;
545cfe60390STomohiro Kusumi uint16_t entry_len = 0;
546cfe60390STomohiro Kusumi uint32_t entry_hash;
547cfe60390STomohiro Kusumi struct ext2fs_direct_2 *ep, *last;
548cfe60390STomohiro Kusumi char *dest;
549cfe60390STomohiro Kusumi struct ext2fs_htree_sort_entry *sort_info;
550cfe60390STomohiro Kusumi
551cfe60390STomohiro Kusumi fs = ip->i_e2fs;
552cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)block1;
553cfe60390STomohiro Kusumi dest = block2;
554cfe60390STomohiro Kusumi sort_info = (struct ext2fs_htree_sort_entry *)
555cfe60390STomohiro Kusumi ((char *)block2 + blksize);
556cfe60390STomohiro Kusumi
557cfe60390STomohiro Kusumi if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
558cfe60390STomohiro Kusumi csum_size = sizeof(struct ext2fs_direct_tail);
559cfe60390STomohiro Kusumi
560cfe60390STomohiro Kusumi /*
561cfe60390STomohiro Kusumi * Calculate name hash value for the entry which is to be added.
562cfe60390STomohiro Kusumi */
563cfe60390STomohiro Kusumi ext2_htree_hash(entry->e2d_name, entry->e2d_namlen, hash_seed,
564cfe60390STomohiro Kusumi hash_version, &entry_hash, NULL);
565cfe60390STomohiro Kusumi
566cfe60390STomohiro Kusumi /*
567cfe60390STomohiro Kusumi * Fill in directory entry sort descriptors.
568cfe60390STomohiro Kusumi */
569cfe60390STomohiro Kusumi while ((char *)ep < block1 + blksize - csum_size) {
570cfe60390STomohiro Kusumi if (le32toh(ep->e2d_ino) && ep->e2d_namlen) {
571cfe60390STomohiro Kusumi entry_cnt++;
572cfe60390STomohiro Kusumi sort_info--;
573cfe60390STomohiro Kusumi sort_info->h_size = ep->e2d_reclen;
574cfe60390STomohiro Kusumi sort_info->h_offset = htole16((char *)ep - block1);
575cfe60390STomohiro Kusumi ext2_htree_hash(ep->e2d_name, ep->e2d_namlen,
576cfe60390STomohiro Kusumi hash_seed, hash_version,
577cfe60390STomohiro Kusumi &sort_info->h_hash, NULL);
578cfe60390STomohiro Kusumi sort_info->h_hash = htole32(sort_info->h_hash);
579cfe60390STomohiro Kusumi }
580cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)
581cfe60390STomohiro Kusumi ((char *)ep + le16toh(ep->e2d_reclen));
582cfe60390STomohiro Kusumi }
583cfe60390STomohiro Kusumi
584cfe60390STomohiro Kusumi /*
585cfe60390STomohiro Kusumi * Sort directory entry descriptors by name hash value.
586cfe60390STomohiro Kusumi */
587cfe60390STomohiro Kusumi kqsort(sort_info, entry_cnt, sizeof(struct ext2fs_htree_sort_entry),
588cfe60390STomohiro Kusumi ext2_htree_cmp_sort_entry);
589cfe60390STomohiro Kusumi
590cfe60390STomohiro Kusumi /*
591cfe60390STomohiro Kusumi * Count the number of entries to move to directory block 2.
592cfe60390STomohiro Kusumi */
593cfe60390STomohiro Kusumi for (i = entry_cnt - 1; i >= 0; i--) {
594cfe60390STomohiro Kusumi if (le16toh(sort_info[i].h_size) + size > blksize / 2)
595cfe60390STomohiro Kusumi break;
596cfe60390STomohiro Kusumi size += le16toh(sort_info[i].h_size);
597cfe60390STomohiro Kusumi }
598cfe60390STomohiro Kusumi
599cfe60390STomohiro Kusumi *split_hash = le32toh(sort_info[i + 1].h_hash);
600cfe60390STomohiro Kusumi
601cfe60390STomohiro Kusumi /*
602cfe60390STomohiro Kusumi * Set collision bit.
603cfe60390STomohiro Kusumi */
604cfe60390STomohiro Kusumi if (*split_hash == le32toh(sort_info[i].h_hash))
605cfe60390STomohiro Kusumi *split_hash += 1;
606cfe60390STomohiro Kusumi
607cfe60390STomohiro Kusumi /*
608cfe60390STomohiro Kusumi * Move half of directory entries from block 1 to block 2.
609cfe60390STomohiro Kusumi */
610cfe60390STomohiro Kusumi for (k = i + 1; k < entry_cnt; k++) {
611cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)((char *)block1 +
612cfe60390STomohiro Kusumi le16toh(sort_info[k].h_offset));
613cfe60390STomohiro Kusumi entry_len = EXT2_DIR_REC_LEN(ep->e2d_namlen);
614cfe60390STomohiro Kusumi memcpy(dest, ep, entry_len);
615cfe60390STomohiro Kusumi ((struct ext2fs_direct_2 *)dest)->e2d_reclen =
616cfe60390STomohiro Kusumi htole16(entry_len);
617cfe60390STomohiro Kusumi /* Mark directory entry as unused. */
618cfe60390STomohiro Kusumi ep->e2d_ino = 0;
619cfe60390STomohiro Kusumi dest += entry_len;
620cfe60390STomohiro Kusumi }
621cfe60390STomohiro Kusumi dest -= entry_len;
622cfe60390STomohiro Kusumi
623cfe60390STomohiro Kusumi /* Shrink directory entries in block 1. */
624cfe60390STomohiro Kusumi last = (struct ext2fs_direct_2 *)block1;
625cfe60390STomohiro Kusumi entry_len = 0;
626cfe60390STomohiro Kusumi for (offset = 0; offset < blksize - csum_size; ) {
627cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)(block1 + offset);
628cfe60390STomohiro Kusumi offset += le16toh(ep->e2d_reclen);
629cfe60390STomohiro Kusumi if (le32toh(ep->e2d_ino)) {
630cfe60390STomohiro Kusumi last = (struct ext2fs_direct_2 *)
631cfe60390STomohiro Kusumi ((char *)last + entry_len);
632cfe60390STomohiro Kusumi entry_len = EXT2_DIR_REC_LEN(ep->e2d_namlen);
633cfe60390STomohiro Kusumi memcpy((void *)last, (void *)ep, entry_len);
634cfe60390STomohiro Kusumi last->e2d_reclen = htole16(entry_len);
635cfe60390STomohiro Kusumi }
636cfe60390STomohiro Kusumi }
637cfe60390STomohiro Kusumi
638cfe60390STomohiro Kusumi if (entry_hash >= *split_hash) {
639cfe60390STomohiro Kusumi /* Add entry to block 2. */
640cfe60390STomohiro Kusumi ext2_append_entry(block2, blksize,
641cfe60390STomohiro Kusumi (struct ext2fs_direct_2 *)dest, entry, csum_size);
642cfe60390STomohiro Kusumi
643cfe60390STomohiro Kusumi /* Adjust length field of last entry of block 1. */
644cfe60390STomohiro Kusumi last->e2d_reclen = htole16(block1 + blksize - (char *)last -
645cfe60390STomohiro Kusumi csum_size);
646cfe60390STomohiro Kusumi } else {
647cfe60390STomohiro Kusumi /* Add entry to block 1. */
648cfe60390STomohiro Kusumi ext2_append_entry(block1, blksize, last, entry, csum_size);
649cfe60390STomohiro Kusumi
650cfe60390STomohiro Kusumi /* Adjust length field of last entry of block 2. */
651cfe60390STomohiro Kusumi ((struct ext2fs_direct_2 *)dest)->e2d_reclen =
652cfe60390STomohiro Kusumi htole16(block2 + blksize - dest - csum_size);
653cfe60390STomohiro Kusumi }
654cfe60390STomohiro Kusumi
655cfe60390STomohiro Kusumi if (csum_size) {
656cfe60390STomohiro Kusumi ext2_init_dirent_tail(EXT2_DIRENT_TAIL(block1, blksize));
657cfe60390STomohiro Kusumi ext2_init_dirent_tail(EXT2_DIRENT_TAIL(block2, blksize));
658cfe60390STomohiro Kusumi }
659cfe60390STomohiro Kusumi
660cfe60390STomohiro Kusumi return (0);
661cfe60390STomohiro Kusumi }
662cfe60390STomohiro Kusumi
663cfe60390STomohiro Kusumi /*
664cfe60390STomohiro Kusumi * Create an HTree index for a directory
665cfe60390STomohiro Kusumi */
666cfe60390STomohiro Kusumi int
ext2_htree_create_index(struct vnode * vp,struct componentname * cnp,struct ext2fs_direct_2 * new_entry)667cfe60390STomohiro Kusumi ext2_htree_create_index(struct vnode *vp, struct componentname *cnp,
668cfe60390STomohiro Kusumi struct ext2fs_direct_2 *new_entry)
669cfe60390STomohiro Kusumi {
670cfe60390STomohiro Kusumi struct buf *bp = NULL;
671cfe60390STomohiro Kusumi struct inode *dp;
672cfe60390STomohiro Kusumi struct ext2fs *fs;
673cfe60390STomohiro Kusumi struct m_ext2fs *m_fs;
674cfe60390STomohiro Kusumi struct ext2fs_direct_2 *ep, *dotdot;
675cfe60390STomohiro Kusumi struct ext2fs_htree_root *root;
676cfe60390STomohiro Kusumi struct ext2fs_htree_lookup_info info;
677cfe60390STomohiro Kusumi uint32_t blksize, dirlen, split_hash;
678cfe60390STomohiro Kusumi uint32_t hash_seed[4];
679cfe60390STomohiro Kusumi uint8_t hash_version;
680cfe60390STomohiro Kusumi char *buf1 = NULL;
681cfe60390STomohiro Kusumi char *buf2 = NULL;
682cfe60390STomohiro Kusumi int error = 0;
683cfe60390STomohiro Kusumi
684cfe60390STomohiro Kusumi dp = VTOI(vp);
685cfe60390STomohiro Kusumi fs = dp->i_e2fs->e2fs;
686cfe60390STomohiro Kusumi m_fs = dp->i_e2fs;
687cfe60390STomohiro Kusumi blksize = m_fs->e2fs_bsize;
688cfe60390STomohiro Kusumi
689cfe60390STomohiro Kusumi buf1 = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
690cfe60390STomohiro Kusumi buf2 = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
691cfe60390STomohiro Kusumi
692cfe60390STomohiro Kusumi if ((error = ext2_blkatoff(vp, 0, NULL, &bp)) != 0)
693cfe60390STomohiro Kusumi goto out;
694cfe60390STomohiro Kusumi
695cfe60390STomohiro Kusumi root = (struct ext2fs_htree_root *)bp->b_data;
696cfe60390STomohiro Kusumi dotdot = (struct ext2fs_direct_2 *)((char *)&(root->h_dotdot));
697cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)((char *)dotdot +
698cfe60390STomohiro Kusumi le16toh(dotdot->e2d_reclen));
699cfe60390STomohiro Kusumi dirlen = (char *)root + blksize - (char *)ep;
700cfe60390STomohiro Kusumi memcpy(buf1, ep, dirlen);
701cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)buf1;
702cfe60390STomohiro Kusumi while ((char *)ep < buf1 + dirlen)
703cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)
704cfe60390STomohiro Kusumi ((char *)ep + le16toh(ep->e2d_reclen));
705cfe60390STomohiro Kusumi ep->e2d_reclen = htole16(buf1 + blksize - (char *)ep);
706cfe60390STomohiro Kusumi
707cfe60390STomohiro Kusumi dp->i_flag |= IN_E3INDEX;
708cfe60390STomohiro Kusumi
709cfe60390STomohiro Kusumi /*
710cfe60390STomohiro Kusumi * Initialize index root.
711cfe60390STomohiro Kusumi */
712cfe60390STomohiro Kusumi dotdot->e2d_reclen = htole16(blksize - EXT2_DIR_REC_LEN(1));
713cfe60390STomohiro Kusumi memset(&root->h_info, 0, sizeof(root->h_info));
714cfe60390STomohiro Kusumi root->h_info.h_hash_version = fs->e3fs_def_hash_version;
715cfe60390STomohiro Kusumi root->h_info.h_info_len = sizeof(root->h_info);
716cfe60390STomohiro Kusumi ext2_htree_set_block(root->h_entries, 1);
717cfe60390STomohiro Kusumi ext2_htree_set_count(root->h_entries, 1);
718cfe60390STomohiro Kusumi ext2_htree_set_limit(root->h_entries,
719cfe60390STomohiro Kusumi ext2_htree_root_limit(dp, sizeof(root->h_info)));
720cfe60390STomohiro Kusumi
721cfe60390STomohiro Kusumi memset(&info, 0, sizeof(info));
722cfe60390STomohiro Kusumi info.h_levels_num = 1;
723cfe60390STomohiro Kusumi info.h_levels[0].h_entries = root->h_entries;
724cfe60390STomohiro Kusumi info.h_levels[0].h_entry = root->h_entries;
725cfe60390STomohiro Kusumi
726cfe60390STomohiro Kusumi hash_version = root->h_info.h_hash_version;
727cfe60390STomohiro Kusumi if (hash_version <= EXT2_HTREE_TEA)
728cfe60390STomohiro Kusumi hash_version += m_fs->e2fs_uhash;
729cfe60390STomohiro Kusumi ext2_get_hash_seed(fs, hash_seed);
730cfe60390STomohiro Kusumi ext2_htree_split_dirblock(dp, buf1, buf2, blksize, hash_seed,
731cfe60390STomohiro Kusumi hash_version, &split_hash, new_entry);
732cfe60390STomohiro Kusumi ext2_htree_insert_entry(&info, split_hash, 2);
733cfe60390STomohiro Kusumi
734cfe60390STomohiro Kusumi /*
735cfe60390STomohiro Kusumi * Write directory block 0.
736cfe60390STomohiro Kusumi */
737cfe60390STomohiro Kusumi ext2_dx_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
738cfe60390STomohiro Kusumi if (DOINGASYNC(vp)) {
739cfe60390STomohiro Kusumi bdwrite(bp);
740cfe60390STomohiro Kusumi error = 0;
741cfe60390STomohiro Kusumi } else {
742cfe60390STomohiro Kusumi error = bwrite(bp);
743cfe60390STomohiro Kusumi }
744cfe60390STomohiro Kusumi dp->i_flag |= IN_CHANGE | IN_UPDATE;
745cfe60390STomohiro Kusumi if (error)
746cfe60390STomohiro Kusumi goto out;
747cfe60390STomohiro Kusumi
748cfe60390STomohiro Kusumi /*
749cfe60390STomohiro Kusumi * Write directory block 1.
750cfe60390STomohiro Kusumi */
751cfe60390STomohiro Kusumi ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)buf1);
752cfe60390STomohiro Kusumi error = ext2_htree_append_block(vp, buf1, cnp, blksize);
753cfe60390STomohiro Kusumi if (error)
754cfe60390STomohiro Kusumi goto out1;
755cfe60390STomohiro Kusumi
756cfe60390STomohiro Kusumi /*
757cfe60390STomohiro Kusumi * Write directory block 2.
758cfe60390STomohiro Kusumi */
759cfe60390STomohiro Kusumi ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)buf2);
760cfe60390STomohiro Kusumi error = ext2_htree_append_block(vp, buf2, cnp, blksize);
761cfe60390STomohiro Kusumi
762cfe60390STomohiro Kusumi free(buf1, M_TEMP);
763cfe60390STomohiro Kusumi free(buf2, M_TEMP);
764cfe60390STomohiro Kusumi return (error);
765cfe60390STomohiro Kusumi out:
766cfe60390STomohiro Kusumi if (bp != NULL)
767*e5b38eb5STomohiro Kusumi brelse(bp);
768cfe60390STomohiro Kusumi out1:
769cfe60390STomohiro Kusumi free(buf1, M_TEMP);
770cfe60390STomohiro Kusumi free(buf2, M_TEMP);
771cfe60390STomohiro Kusumi return (error);
772cfe60390STomohiro Kusumi }
773cfe60390STomohiro Kusumi
774cfe60390STomohiro Kusumi /*
775cfe60390STomohiro Kusumi * Add an entry to the directory using htree index.
776cfe60390STomohiro Kusumi */
777cfe60390STomohiro Kusumi int
ext2_htree_add_entry(struct vnode * dvp,struct ext2fs_direct_2 * entry,struct componentname * cnp)778cfe60390STomohiro Kusumi ext2_htree_add_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry,
779cfe60390STomohiro Kusumi struct componentname *cnp)
780cfe60390STomohiro Kusumi {
781cfe60390STomohiro Kusumi struct ext2fs_htree_entry *entries, *leaf_node;
782cfe60390STomohiro Kusumi struct ext2fs_htree_lookup_info info;
783cfe60390STomohiro Kusumi struct buf *bp = NULL;
784cfe60390STomohiro Kusumi struct ext2fs *fs;
785cfe60390STomohiro Kusumi struct m_ext2fs *m_fs;
786cfe60390STomohiro Kusumi struct inode *ip;
787cfe60390STomohiro Kusumi uint16_t ent_num;
788cfe60390STomohiro Kusumi uint32_t dirhash, split_hash;
789cfe60390STomohiro Kusumi uint32_t blksize, blknum;
790cfe60390STomohiro Kusumi uint64_t cursize, dirsize;
791cfe60390STomohiro Kusumi uint32_t hash_seed[4];
792cfe60390STomohiro Kusumi uint8_t hash_version;
793cfe60390STomohiro Kusumi char *newdirblock = NULL;
794cfe60390STomohiro Kusumi char *newidxblock = NULL;
795cfe60390STomohiro Kusumi struct ext2fs_htree_node *dst_node;
796cfe60390STomohiro Kusumi struct ext2fs_htree_entry *dst_entries;
797cfe60390STomohiro Kusumi struct ext2fs_htree_entry *root_entires;
798cfe60390STomohiro Kusumi struct buf *dst_bp = NULL;
799cfe60390STomohiro Kusumi int error, write_bp = 0, write_dst_bp = 0, write_info = 0;
800cfe60390STomohiro Kusumi
801cfe60390STomohiro Kusumi ip = VTOI(dvp);
802cfe60390STomohiro Kusumi m_fs = ip->i_e2fs;
803cfe60390STomohiro Kusumi fs = m_fs->e2fs;
804cfe60390STomohiro Kusumi blksize = m_fs->e2fs_bsize;
805cfe60390STomohiro Kusumi
806cfe60390STomohiro Kusumi if (ip->i_count != 0)
807cfe60390STomohiro Kusumi return ext2_add_entry(dvp, entry);
808cfe60390STomohiro Kusumi
809cfe60390STomohiro Kusumi /* Target directory block is full, split it */
810cfe60390STomohiro Kusumi memset(&info, 0, sizeof(info));
811cfe60390STomohiro Kusumi error = ext2_htree_find_leaf(ip, entry->e2d_name, entry->e2d_namlen,
812cfe60390STomohiro Kusumi &dirhash, &hash_version, &info);
813cfe60390STomohiro Kusumi if (error)
814cfe60390STomohiro Kusumi return (error);
815cfe60390STomohiro Kusumi
816cfe60390STomohiro Kusumi entries = info.h_levels[info.h_levels_num - 1].h_entries;
817cfe60390STomohiro Kusumi ent_num = ext2_htree_get_count(entries);
818cfe60390STomohiro Kusumi if (ent_num == ext2_htree_get_limit(entries)) {
819cfe60390STomohiro Kusumi /* Split the index node. */
820cfe60390STomohiro Kusumi root_entires = info.h_levels[0].h_entries;
821cfe60390STomohiro Kusumi newidxblock = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
822cfe60390STomohiro Kusumi dst_node = (struct ext2fs_htree_node *)newidxblock;
823cfe60390STomohiro Kusumi memset(&dst_node->h_fake_dirent, 0,
824cfe60390STomohiro Kusumi sizeof(dst_node->h_fake_dirent));
825cfe60390STomohiro Kusumi dst_node->h_fake_dirent.e2d_reclen = htole16(blksize);
826cfe60390STomohiro Kusumi
827cfe60390STomohiro Kusumi cursize = roundup(ip->i_size, blksize);
828cfe60390STomohiro Kusumi dirsize = cursize + blksize;
829cfe60390STomohiro Kusumi blknum = dirsize / blksize - 1;
830cfe60390STomohiro Kusumi ext2_dx_csum_set(ip, (struct ext2fs_direct_2 *)newidxblock);
831cfe60390STomohiro Kusumi error = ext2_htree_append_block(dvp, newidxblock,
832cfe60390STomohiro Kusumi cnp, blksize);
833cfe60390STomohiro Kusumi if (error)
834cfe60390STomohiro Kusumi goto finish;
835cfe60390STomohiro Kusumi error = ext2_blkatoff(dvp, cursize, NULL, &dst_bp);
836cfe60390STomohiro Kusumi if (error)
837cfe60390STomohiro Kusumi goto finish;
838cfe60390STomohiro Kusumi dst_node = (struct ext2fs_htree_node *)dst_bp->b_data;
839cfe60390STomohiro Kusumi dst_entries = dst_node->h_entries;
840cfe60390STomohiro Kusumi
841cfe60390STomohiro Kusumi if (info.h_levels_num == 2) {
842cfe60390STomohiro Kusumi uint16_t src_ent_num, dst_ent_num;
843cfe60390STomohiro Kusumi
844cfe60390STomohiro Kusumi if (ext2_htree_get_count(root_entires) ==
845cfe60390STomohiro Kusumi ext2_htree_get_limit(root_entires)) {
846cfe60390STomohiro Kusumi SDT_PROBE2(ext2fs, , trace, htree, 1,
847cfe60390STomohiro Kusumi "directory index is full");
848cfe60390STomohiro Kusumi error = EIO;
849cfe60390STomohiro Kusumi goto finish;
850cfe60390STomohiro Kusumi }
851cfe60390STomohiro Kusumi
852cfe60390STomohiro Kusumi src_ent_num = ent_num / 2;
853cfe60390STomohiro Kusumi dst_ent_num = ent_num - src_ent_num;
854cfe60390STomohiro Kusumi split_hash = ext2_htree_get_hash(entries + src_ent_num);
855cfe60390STomohiro Kusumi
856cfe60390STomohiro Kusumi /* Move half of index entries to the new index node */
857cfe60390STomohiro Kusumi memcpy(dst_entries, entries + src_ent_num,
858cfe60390STomohiro Kusumi dst_ent_num * sizeof(struct ext2fs_htree_entry));
859cfe60390STomohiro Kusumi ext2_htree_set_count(entries, src_ent_num);
860cfe60390STomohiro Kusumi ext2_htree_set_count(dst_entries, dst_ent_num);
861cfe60390STomohiro Kusumi ext2_htree_set_limit(dst_entries,
862cfe60390STomohiro Kusumi ext2_htree_node_limit(ip));
863cfe60390STomohiro Kusumi
864cfe60390STomohiro Kusumi if (info.h_levels[1].h_entry >= entries + src_ent_num) {
865cfe60390STomohiro Kusumi struct buf *tmp = info.h_levels[1].h_bp;
866cfe60390STomohiro Kusumi
867cfe60390STomohiro Kusumi info.h_levels[1].h_bp = dst_bp;
868cfe60390STomohiro Kusumi dst_bp = tmp;
869cfe60390STomohiro Kusumi
870cfe60390STomohiro Kusumi info.h_levels[1].h_entry =
871cfe60390STomohiro Kusumi info.h_levels[1].h_entry -
872cfe60390STomohiro Kusumi (entries + src_ent_num) +
873cfe60390STomohiro Kusumi dst_entries;
874cfe60390STomohiro Kusumi info.h_levels[1].h_entries = dst_entries;
875cfe60390STomohiro Kusumi }
876cfe60390STomohiro Kusumi ext2_htree_insert_entry_to_level(&info.h_levels[0],
877cfe60390STomohiro Kusumi split_hash, blknum);
878cfe60390STomohiro Kusumi
879cfe60390STomohiro Kusumi /* Write new index node to disk */
880cfe60390STomohiro Kusumi ext2_dx_csum_set(ip,
881cfe60390STomohiro Kusumi (struct ext2fs_direct_2 *)dst_bp->b_data);
882cfe60390STomohiro Kusumi error = bwrite(dst_bp);
883cfe60390STomohiro Kusumi ip->i_flag |= IN_CHANGE | IN_UPDATE;
884cfe60390STomohiro Kusumi if (error)
885cfe60390STomohiro Kusumi goto finish;
886cfe60390STomohiro Kusumi write_dst_bp = 1;
887cfe60390STomohiro Kusumi } else {
888cfe60390STomohiro Kusumi /* Create second level for htree index */
889cfe60390STomohiro Kusumi struct ext2fs_htree_root *idx_root;
890cfe60390STomohiro Kusumi
891cfe60390STomohiro Kusumi memcpy(dst_entries, entries,
892cfe60390STomohiro Kusumi ent_num * sizeof(struct ext2fs_htree_entry));
893cfe60390STomohiro Kusumi ext2_htree_set_limit(dst_entries,
894cfe60390STomohiro Kusumi ext2_htree_node_limit(ip));
895cfe60390STomohiro Kusumi
896cfe60390STomohiro Kusumi idx_root = (struct ext2fs_htree_root *)
897cfe60390STomohiro Kusumi info.h_levels[0].h_bp->b_data;
898cfe60390STomohiro Kusumi idx_root->h_info.h_ind_levels = 1;
899cfe60390STomohiro Kusumi
900cfe60390STomohiro Kusumi ext2_htree_set_count(entries, 1);
901cfe60390STomohiro Kusumi ext2_htree_set_block(entries, blknum);
902cfe60390STomohiro Kusumi
903cfe60390STomohiro Kusumi info.h_levels_num = 2;
904cfe60390STomohiro Kusumi info.h_levels[1].h_entries = dst_entries;
905cfe60390STomohiro Kusumi info.h_levels[1].h_entry = info.h_levels[0].h_entry -
906cfe60390STomohiro Kusumi info.h_levels[0].h_entries + dst_entries;
907cfe60390STomohiro Kusumi info.h_levels[1].h_bp = dst_bp;
908cfe60390STomohiro Kusumi dst_bp = NULL;
909cfe60390STomohiro Kusumi }
910cfe60390STomohiro Kusumi }
911cfe60390STomohiro Kusumi
912cfe60390STomohiro Kusumi leaf_node = info.h_levels[info.h_levels_num - 1].h_entry;
913cfe60390STomohiro Kusumi blknum = ext2_htree_get_block(leaf_node);
914cfe60390STomohiro Kusumi error = ext2_blkatoff(dvp, blknum * blksize, NULL, &bp);
915cfe60390STomohiro Kusumi if (error)
916cfe60390STomohiro Kusumi goto finish;
917cfe60390STomohiro Kusumi
918cfe60390STomohiro Kusumi /* Split target directory block */
919cfe60390STomohiro Kusumi newdirblock = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
920cfe60390STomohiro Kusumi ext2_get_hash_seed(fs, hash_seed);
921cfe60390STomohiro Kusumi ext2_htree_split_dirblock(ip, (char *)bp->b_data, newdirblock, blksize,
922cfe60390STomohiro Kusumi hash_seed, hash_version, &split_hash, entry);
923cfe60390STomohiro Kusumi cursize = roundup(ip->i_size, blksize);
924cfe60390STomohiro Kusumi dirsize = cursize + blksize;
925cfe60390STomohiro Kusumi blknum = dirsize / blksize - 1;
926cfe60390STomohiro Kusumi
927cfe60390STomohiro Kusumi /* Add index entry for the new directory block */
928cfe60390STomohiro Kusumi ext2_htree_insert_entry(&info, split_hash, blknum);
929cfe60390STomohiro Kusumi
930cfe60390STomohiro Kusumi /* Write the new directory block to the end of the directory */
931cfe60390STomohiro Kusumi ext2_dirent_csum_set(ip, (struct ext2fs_direct_2 *)newdirblock);
932cfe60390STomohiro Kusumi error = ext2_htree_append_block(dvp, newdirblock, cnp, blksize);
933cfe60390STomohiro Kusumi if (error)
934cfe60390STomohiro Kusumi goto finish;
935cfe60390STomohiro Kusumi
936cfe60390STomohiro Kusumi /* Write the target directory block */
937cfe60390STomohiro Kusumi ext2_dirent_csum_set(ip, (struct ext2fs_direct_2 *)bp->b_data);
938cfe60390STomohiro Kusumi error = bwrite(bp);
939cfe60390STomohiro Kusumi ip->i_flag |= IN_CHANGE | IN_UPDATE;
940cfe60390STomohiro Kusumi if (error)
941cfe60390STomohiro Kusumi goto finish;
942cfe60390STomohiro Kusumi write_bp = 1;
943cfe60390STomohiro Kusumi
944cfe60390STomohiro Kusumi /* Write the index block */
945cfe60390STomohiro Kusumi error = ext2_htree_writebuf(ip, &info);
946cfe60390STomohiro Kusumi if (!error)
947cfe60390STomohiro Kusumi write_info = 1;
948cfe60390STomohiro Kusumi
949cfe60390STomohiro Kusumi finish:
950cfe60390STomohiro Kusumi if (dst_bp != NULL && !write_dst_bp)
951*e5b38eb5STomohiro Kusumi brelse(dst_bp);
952cfe60390STomohiro Kusumi if (bp != NULL && !write_bp)
953*e5b38eb5STomohiro Kusumi brelse(bp);
954cfe60390STomohiro Kusumi if (newdirblock != NULL)
955cfe60390STomohiro Kusumi free(newdirblock, M_TEMP);
956cfe60390STomohiro Kusumi if (newidxblock != NULL)
957cfe60390STomohiro Kusumi free(newidxblock, M_TEMP);
958cfe60390STomohiro Kusumi if (!write_info)
959cfe60390STomohiro Kusumi ext2_htree_release(&info);
960cfe60390STomohiro Kusumi return (error);
961cfe60390STomohiro Kusumi }
962