2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2010, 2012 Zheng Liu <lz@freebsd.org>
5 * Copyright (c) 2012, Vyacheslav Matyushin
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
33 #include <sys/endian.h>
34 #include <sys/systm.h>
35 #include <sys/namei.h>
38 #include <sys/endian.h>
39 #include <sys/mount.h>
40 #include <sys/vnode.h>
41 #include <sys/malloc.h>
42 #include <sys/dirent.h>
44 #include <sys/sysctl.h>
46 #include <ufs/ufs/dir.h>
48 #include <fs/ext2fs/fs.h>
49 #include <fs/ext2fs/inode.h>
50 #include <fs/ext2fs/ext2_mount.h>
51 #include <fs/ext2fs/ext2fs.h>
52 #include <fs/ext2fs/fs.h>
53 #include <fs/ext2fs/ext2_extern.h>
54 #include <fs/ext2fs/ext2_dinode.h>
55 #include <fs/ext2fs/ext2_dir.h>
56 #include <fs/ext2fs/htree.h>
58 SDT_PROVIDER_DECLARE(ext2fs);
61 * arg0: verbosity. Higher numbers give more verbose messages
62 * arg1: Textual message
64 SDT_PROBE_DEFINE2(ext2fs, , trace, htree, "int", "char*");
66 static void ext2_append_entry(char *block, uint32_t blksize,
67 struct ext2fs_direct_2 *last_entry,
68 struct ext2fs_direct_2 *new_entry, int csum_size);
69 static int ext2_htree_append_block(struct vnode *vp, char *data,
70 struct componentname *cnp, uint32_t blksize);
71 static int ext2_htree_check_next(struct inode *ip, uint32_t hash,
72 const char *name, struct ext2fs_htree_lookup_info *info);
73 static int ext2_htree_cmp_sort_entry(const void *e1, const void *e2);
74 static int ext2_htree_find_leaf(struct inode *ip, const char *name,
75 int namelen, uint32_t *hash, uint8_t *hash_version,
76 struct ext2fs_htree_lookup_info *info);
77 static uint32_t ext2_htree_get_block(struct ext2fs_htree_entry *ep);
78 static uint16_t ext2_htree_get_count(struct ext2fs_htree_entry *ep);
79 static uint32_t ext2_htree_get_hash(struct ext2fs_htree_entry *ep);
80 static uint16_t ext2_htree_get_limit(struct ext2fs_htree_entry *ep);
81 static void ext2_htree_insert_entry_to_level(struct ext2fs_htree_lookup_level *level,
82 uint32_t hash, uint32_t blk);
83 static void ext2_htree_insert_entry(struct ext2fs_htree_lookup_info *info,
84 uint32_t hash, uint32_t blk);
85 static uint32_t ext2_htree_node_limit(struct inode *ip);
86 static void ext2_htree_set_block(struct ext2fs_htree_entry *ep,
88 static void ext2_htree_set_count(struct ext2fs_htree_entry *ep,
90 static void ext2_htree_set_hash(struct ext2fs_htree_entry *ep,
92 static void ext2_htree_set_limit(struct ext2fs_htree_entry *ep,
94 static int ext2_htree_split_dirblock(struct inode *ip,
95 char *block1, char *block2, uint32_t blksize,
96 uint32_t *hash_seed, uint8_t hash_version,
97 uint32_t *split_hash, struct ext2fs_direct_2 *entry);
98 static void ext2_htree_release(struct ext2fs_htree_lookup_info *info);
99 static uint32_t ext2_htree_root_limit(struct inode *ip, int len);
100 static int ext2_htree_writebuf(struct inode *ip,
101 struct ext2fs_htree_lookup_info *info);
104 ext2_htree_has_idx(struct inode *ip)
106 if (EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_DIRHASHINDEX) &&
107 ip->i_flag & IN_E3INDEX)
114 ext2_htree_check_next(struct inode *ip, uint32_t hash, const char *name,
115 struct ext2fs_htree_lookup_info *info)
117 struct vnode *vp = ITOV(ip);
118 struct ext2fs_htree_lookup_level *level;
121 int idx = info->h_levels_num - 1;
125 level = &info->h_levels[idx];
127 if (level->h_entry < level->h_entries +
128 ext2_htree_get_count(level->h_entries))
136 next_hash = ext2_htree_get_hash(level->h_entry);
137 if ((hash & 1) == 0) {
138 if (hash != (next_hash & ~1))
144 if (ext2_blkatoff(vp, ext2_htree_get_block(level->h_entry) *
145 ip->i_e2fs->e2fs_bsize, NULL, &bp) != 0)
147 level = &info->h_levels[idx + 1];
150 level->h_entry = level->h_entries =
151 ((struct ext2fs_htree_node *)bp->b_data)->h_entries;
158 ext2_htree_get_block(struct ext2fs_htree_entry *ep)
160 return (le32toh(ep->h_blk) & 0x00FFFFFF);
164 ext2_htree_set_block(struct ext2fs_htree_entry *ep, uint32_t blk)
166 ep->h_blk = htole32(blk);
170 ext2_htree_get_count(struct ext2fs_htree_entry *ep)
172 return (le16toh(((struct ext2fs_htree_count *)(ep))->h_entries_num));
176 ext2_htree_set_count(struct ext2fs_htree_entry *ep, uint16_t cnt)
178 ((struct ext2fs_htree_count *)(ep))->h_entries_num = htole16(cnt);
182 ext2_htree_get_hash(struct ext2fs_htree_entry *ep)
184 return (le32toh(ep->h_hash));
188 ext2_htree_get_limit(struct ext2fs_htree_entry *ep)
190 return (le16toh(((struct ext2fs_htree_count *)(ep))->h_entries_max));
194 ext2_htree_set_hash(struct ext2fs_htree_entry *ep, uint32_t hash)
196 ep->h_hash = htole32(hash);
200 ext2_htree_set_limit(struct ext2fs_htree_entry *ep, uint16_t limit)
202 ((struct ext2fs_htree_count *)(ep))->h_entries_max = htole16(limit);
206 ext2_htree_release(struct ext2fs_htree_lookup_info *info)
210 for (i = 0; i < info->h_levels_num; i++) {
211 struct buf *bp = info->h_levels[i].h_bp;
219 ext2_htree_root_limit(struct inode *ip, int len)
225 space = ip->i_e2fs->e2fs_bsize - EXT2_DIR_REC_LEN(1) -
226 EXT2_DIR_REC_LEN(2) - len;
228 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
229 space -= sizeof(struct ext2fs_htree_tail);
231 return (space / sizeof(struct ext2fs_htree_entry));
235 ext2_htree_node_limit(struct inode *ip)
241 space = fs->e2fs_bsize - EXT2_DIR_REC_LEN(0);
243 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
244 space -= sizeof(struct ext2fs_htree_tail);
246 return (space / sizeof(struct ext2fs_htree_entry));
250 ext2_get_hash_seed(struct ext2fs* es, uint32_t* seed)
253 for (int i = 0; i < 4; i++)
254 seed[i] = le32toh(es->e3fs_hash_seed[i]);
258 ext2_htree_find_leaf(struct inode *ip, const char *name, int namelen,
259 uint32_t *hash, uint8_t *hash_ver,
260 struct ext2fs_htree_lookup_info *info)
264 struct m_ext2fs *m_fs;
265 struct buf *bp = NULL;
266 struct ext2fs_htree_root *rootp;
267 struct ext2fs_htree_entry *entp, *start, *end, *middle, *found;
268 struct ext2fs_htree_lookup_level *level_info;
269 uint32_t hash_major = 0, hash_minor = 0;
270 uint32_t levels, cnt;
271 uint32_t hash_seed[4];
272 uint8_t hash_version;
274 if (name == NULL || info == NULL)
278 fs = ip->i_e2fs->e2fs;
281 if (ext2_blkatoff(vp, 0, NULL, &bp) != 0)
284 info->h_levels_num = 1;
285 info->h_levels[0].h_bp = bp;
286 rootp = (struct ext2fs_htree_root *)bp->b_data;
287 if (rootp->h_info.h_hash_version != EXT2_HTREE_LEGACY &&
288 rootp->h_info.h_hash_version != EXT2_HTREE_HALF_MD4 &&
289 rootp->h_info.h_hash_version != EXT2_HTREE_TEA)
292 hash_version = rootp->h_info.h_hash_version;
293 if (hash_version <= EXT2_HTREE_TEA)
294 hash_version += m_fs->e2fs_uhash;
295 *hash_ver = hash_version;
297 ext2_get_hash_seed(fs, hash_seed);
298 ext2_htree_hash(name, namelen, hash_seed,
299 hash_version, &hash_major, &hash_minor);
302 if ((levels = rootp->h_info.h_ind_levels) > 1)
305 entp = (struct ext2fs_htree_entry *)(((char *)&rootp->h_info) +
306 rootp->h_info.h_info_len);
308 if (ext2_htree_get_limit(entp) !=
309 ext2_htree_root_limit(ip, rootp->h_info.h_info_len))
313 cnt = ext2_htree_get_count(entp);
314 if (cnt == 0 || cnt > ext2_htree_get_limit(entp))
318 end = entp + cnt - 1;
319 while (start <= end) {
320 middle = start + (end - start) / 2;
321 if (ext2_htree_get_hash(middle) > hash_major)
328 level_info = &(info->h_levels[info->h_levels_num - 1]);
329 level_info->h_bp = bp;
330 level_info->h_entries = entp;
331 level_info->h_entry = found;
335 if (ext2_blkatoff(vp,
336 ext2_htree_get_block(found) * m_fs->e2fs_bsize,
339 entp = ((struct ext2fs_htree_node *)bp->b_data)->h_entries;
340 info->h_levels_num++;
341 info->h_levels[info->h_levels_num - 1].h_bp = bp;
345 ext2_htree_release(info);
350 * Try to lookup a directory entry in HTree index
353 ext2_htree_lookup(struct inode *ip, const char *name, int namelen,
354 struct buf **bpp, int *entryoffp, doff_t *offp,
355 doff_t *prevoffp, doff_t *endusefulp,
356 struct ext2fs_searchslot *ss)
359 struct ext2fs_htree_lookup_info info;
360 struct ext2fs_htree_entry *leaf_node;
361 struct m_ext2fs *m_fs;
366 uint8_t hash_version;
371 bsize = m_fs->e2fs_bsize;
374 /* TODO: print error msg because we don't lookup '.' and '..' */
376 memset(&info, 0, sizeof(info));
377 if (ext2_htree_find_leaf(ip, name, namelen, &dirhash,
378 &hash_version, &info))
382 leaf_node = info.h_levels[info.h_levels_num - 1].h_entry;
383 blk = ext2_htree_get_block(leaf_node);
384 if (ext2_blkatoff(vp, blk * bsize, NULL, &bp) != 0) {
385 ext2_htree_release(&info);
391 *prevoffp = blk * bsize;
392 *endusefulp = blk * bsize;
394 if (ss->slotstatus == NONE) {
396 ss->slotfreespace = 0;
399 if (ext2_search_dirblock(ip, bp->b_data, &found,
400 name, namelen, entryoffp, offp, prevoffp,
401 endusefulp, ss) != 0) {
403 ext2_htree_release(&info);
409 ext2_htree_release(&info);
414 search_next = ext2_htree_check_next(ip, dirhash, name, &info);
415 } while (search_next);
417 ext2_htree_release(&info);
422 ext2_htree_append_block(struct vnode *vp, char *data,
423 struct componentname *cnp, uint32_t blksize)
427 struct inode *dp = VTOI(vp);
428 uint64_t cursize, newsize;
431 cursize = roundup(dp->i_size, blksize);
432 newsize = cursize + blksize;
434 auio.uio_offset = cursize;
435 auio.uio_resid = blksize;
436 aiov.iov_len = blksize;
437 aiov.iov_base = data;
438 auio.uio_iov = &aiov;
440 auio.uio_rw = UIO_WRITE;
441 auio.uio_segflg = UIO_SYSSPACE;
443 error = VOP_WRITE(vp, &auio, IO_SYNC, cnp->cn_cred);
445 dp->i_size = newsize;
451 ext2_htree_writebuf(struct inode* ip, struct ext2fs_htree_lookup_info *info)
455 for (i = 0; i < info->h_levels_num; i++) {
456 struct buf *bp = info->h_levels[i].h_bp;
457 ext2_dx_csum_set(ip, (struct ext2fs_direct_2 *)bp->b_data);
467 ext2_htree_insert_entry_to_level(struct ext2fs_htree_lookup_level *level,
468 uint32_t hash, uint32_t blk)
470 struct ext2fs_htree_entry *target;
473 target = level->h_entry + 1;
474 entries_num = ext2_htree_get_count(level->h_entries);
476 memmove(target + 1, target, (char *)(level->h_entries + entries_num) -
478 ext2_htree_set_block(target, blk);
479 ext2_htree_set_hash(target, hash);
480 ext2_htree_set_count(level->h_entries, entries_num + 1);
484 * Insert an index entry to the index node.
487 ext2_htree_insert_entry(struct ext2fs_htree_lookup_info *info,
488 uint32_t hash, uint32_t blk)
490 struct ext2fs_htree_lookup_level *level;
492 level = &info->h_levels[info->h_levels_num - 1];
493 ext2_htree_insert_entry_to_level(level, hash, blk);
497 * Compare two entry sort descriptors by name hash value.
498 * This is used together with qsort.
501 ext2_htree_cmp_sort_entry(const void *e1, const void *e2)
503 const struct ext2fs_htree_sort_entry *entry1, *entry2;
505 entry1 = (const struct ext2fs_htree_sort_entry *)e1;
506 entry2 = (const struct ext2fs_htree_sort_entry *)e2;
508 if (le32toh(entry1->h_hash) < le32toh(entry2->h_hash))
510 if (le32toh(entry1->h_hash) > le32toh(entry2->h_hash))
516 * Append an entry to the end of the directory block.
519 ext2_append_entry(char *block, uint32_t blksize,
520 struct ext2fs_direct_2 *last_entry,
521 struct ext2fs_direct_2 *new_entry, int csum_size)
525 entry_len = EXT2_DIR_REC_LEN(last_entry->e2d_namlen);
526 last_entry->e2d_reclen = htole16(entry_len);
527 last_entry = (struct ext2fs_direct_2 *)((char *)last_entry + entry_len);
528 new_entry->e2d_reclen = htole16(block + blksize - (char *)last_entry -
530 memcpy(last_entry, new_entry, EXT2_DIR_REC_LEN(new_entry->e2d_namlen));
534 * Move half of entries from the old directory block to the new one.
537 ext2_htree_split_dirblock(struct inode *ip, char *block1, char *block2,
538 uint32_t blksize, uint32_t *hash_seed, uint8_t hash_version,
539 uint32_t *split_hash, struct ext2fs_direct_2 *entry)
543 int size = 0, csum_size = 0;
546 uint16_t entry_len = 0;
548 struct ext2fs_direct_2 *ep, *last;
550 struct ext2fs_htree_sort_entry *sort_info;
553 ep = (struct ext2fs_direct_2 *)block1;
555 sort_info = (struct ext2fs_htree_sort_entry *)
556 ((char *)block2 + blksize);
558 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
559 csum_size = sizeof(struct ext2fs_direct_tail);
562 * Calculate name hash value for the entry which is to be added.
564 ext2_htree_hash(entry->e2d_name, entry->e2d_namlen, hash_seed,
565 hash_version, &entry_hash, NULL);
568 * Fill in directory entry sort descriptors.
570 while ((char *)ep < block1 + blksize - csum_size) {
571 if (le32toh(ep->e2d_ino) && ep->e2d_namlen) {
574 sort_info->h_size = ep->e2d_reclen;
575 sort_info->h_offset = htole16((char *)ep - block1);
576 ext2_htree_hash(ep->e2d_name, ep->e2d_namlen,
577 hash_seed, hash_version,
578 &sort_info->h_hash, NULL);
579 sort_info->h_hash = htole32(sort_info->h_hash);
581 ep = (struct ext2fs_direct_2 *)
582 ((char *)ep + le16toh(ep->e2d_reclen));
586 * Sort directory entry descriptors by name hash value.
588 qsort(sort_info, entry_cnt, sizeof(struct ext2fs_htree_sort_entry),
589 ext2_htree_cmp_sort_entry);
592 * Count the number of entries to move to directory block 2.
594 for (i = entry_cnt - 1; i >= 0; i--) {
595 if (le16toh(sort_info[i].h_size) + size > blksize / 2)
597 size += le16toh(sort_info[i].h_size);
600 *split_hash = le32toh(sort_info[i + 1].h_hash);
605 if (*split_hash == le32toh(sort_info[i].h_hash))
609 * Move half of directory entries from block 1 to block 2.
611 for (k = i + 1; k < entry_cnt; k++) {
612 ep = (struct ext2fs_direct_2 *)((char *)block1 +
613 le16toh(sort_info[k].h_offset));
614 entry_len = EXT2_DIR_REC_LEN(ep->e2d_namlen);
615 memcpy(dest, ep, entry_len);
616 ((struct ext2fs_direct_2 *)dest)->e2d_reclen =
618 /* Mark directory entry as unused. */
624 /* Shrink directory entries in block 1. */
625 last = (struct ext2fs_direct_2 *)block1;
627 for (offset = 0; offset < blksize - csum_size; ) {
628 ep = (struct ext2fs_direct_2 *)(block1 + offset);
629 offset += le16toh(ep->e2d_reclen);
630 if (le32toh(ep->e2d_ino)) {
631 last = (struct ext2fs_direct_2 *)
632 ((char *)last + entry_len);
633 entry_len = EXT2_DIR_REC_LEN(ep->e2d_namlen);
634 memcpy((void *)last, (void *)ep, entry_len);
635 last->e2d_reclen = htole16(entry_len);
639 if (entry_hash >= *split_hash) {
640 /* Add entry to block 2. */
641 ext2_append_entry(block2, blksize,
642 (struct ext2fs_direct_2 *)dest, entry, csum_size);
644 /* Adjust length field of last entry of block 1. */
645 last->e2d_reclen = htole16(block1 + blksize - (char *)last -
648 /* Add entry to block 1. */
649 ext2_append_entry(block1, blksize, last, entry, csum_size);
651 /* Adjust length field of last entry of block 2. */
652 ((struct ext2fs_direct_2 *)dest)->e2d_reclen =
653 htole16(block2 + blksize - dest - csum_size);
657 ext2_init_dirent_tail(EXT2_DIRENT_TAIL(block1, blksize));
658 ext2_init_dirent_tail(EXT2_DIRENT_TAIL(block2, blksize));
665 * Create an HTree index for a directory
668 ext2_htree_create_index(struct vnode *vp, struct componentname *cnp,
669 struct ext2fs_direct_2 *new_entry)
671 struct buf *bp = NULL;
674 struct m_ext2fs *m_fs;
675 struct ext2fs_direct_2 *ep, *dotdot;
676 struct ext2fs_htree_root *root;
677 struct ext2fs_htree_lookup_info info;
678 uint32_t blksize, dirlen, split_hash;
679 uint32_t hash_seed[4];
680 uint8_t hash_version;
686 fs = dp->i_e2fs->e2fs;
688 blksize = m_fs->e2fs_bsize;
690 buf1 = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
691 buf2 = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
693 if ((error = ext2_blkatoff(vp, 0, NULL, &bp)) != 0)
696 root = (struct ext2fs_htree_root *)bp->b_data;
697 dotdot = (struct ext2fs_direct_2 *)((char *)&(root->h_dotdot));
698 ep = (struct ext2fs_direct_2 *)((char *)dotdot +
699 le16toh(dotdot->e2d_reclen));
700 dirlen = (char *)root + blksize - (char *)ep;
701 memcpy(buf1, ep, dirlen);
702 ep = (struct ext2fs_direct_2 *)buf1;
703 while ((char *)ep < buf1 + dirlen)
704 ep = (struct ext2fs_direct_2 *)
705 ((char *)ep + le16toh(ep->e2d_reclen));
706 ep->e2d_reclen = htole16(buf1 + blksize - (char *)ep);
708 dp->i_flag |= IN_E3INDEX;
711 * Initialize index root.
713 dotdot->e2d_reclen = htole16(blksize - EXT2_DIR_REC_LEN(1));
714 memset(&root->h_info, 0, sizeof(root->h_info));
715 root->h_info.h_hash_version = fs->e3fs_def_hash_version;
716 root->h_info.h_info_len = sizeof(root->h_info);
717 ext2_htree_set_block(root->h_entries, 1);
718 ext2_htree_set_count(root->h_entries, 1);
719 ext2_htree_set_limit(root->h_entries,
720 ext2_htree_root_limit(dp, sizeof(root->h_info)));
722 memset(&info, 0, sizeof(info));
723 info.h_levels_num = 1;
724 info.h_levels[0].h_entries = root->h_entries;
725 info.h_levels[0].h_entry = root->h_entries;
727 hash_version = root->h_info.h_hash_version;
728 if (hash_version <= EXT2_HTREE_TEA)
729 hash_version += m_fs->e2fs_uhash;
730 ext2_get_hash_seed(fs, hash_seed);
731 ext2_htree_split_dirblock(dp, buf1, buf2, blksize, hash_seed,
732 hash_version, &split_hash, new_entry);
733 ext2_htree_insert_entry(&info, split_hash, 2);
736 * Write directory block 0.
738 ext2_dx_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
739 if (DOINGASYNC(vp)) {
745 dp->i_flag |= IN_CHANGE | IN_UPDATE;
750 * Write directory block 1.
752 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)buf1);
753 error = ext2_htree_append_block(vp, buf1, cnp, blksize);
758 * Write directory block 2.
760 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)buf2);
761 error = ext2_htree_append_block(vp, buf2, cnp, blksize);
776 * Add an entry to the directory using htree index.
779 ext2_htree_add_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry,
780 struct componentname *cnp)
782 struct ext2fs_htree_entry *entries, *leaf_node;
783 struct ext2fs_htree_lookup_info info;
784 struct buf *bp = NULL;
786 struct m_ext2fs *m_fs;
789 uint32_t dirhash, split_hash;
790 uint32_t blksize, blknum;
791 uint64_t cursize, dirsize;
792 uint32_t hash_seed[4];
793 uint8_t hash_version;
794 char *newdirblock = NULL;
795 char *newidxblock = NULL;
796 struct ext2fs_htree_node *dst_node;
797 struct ext2fs_htree_entry *dst_entries;
798 struct ext2fs_htree_entry *root_entires;
799 struct buf *dst_bp = NULL;
800 int error, write_bp = 0, write_dst_bp = 0, write_info = 0;
805 blksize = m_fs->e2fs_bsize;
807 if (ip->i_count != 0)
808 return ext2_add_entry(dvp, entry);
810 /* Target directory block is full, split it */
811 memset(&info, 0, sizeof(info));
812 error = ext2_htree_find_leaf(ip, entry->e2d_name, entry->e2d_namlen,
813 &dirhash, &hash_version, &info);
817 entries = info.h_levels[info.h_levels_num - 1].h_entries;
818 ent_num = ext2_htree_get_count(entries);
819 if (ent_num == ext2_htree_get_limit(entries)) {
820 /* Split the index node. */
821 root_entires = info.h_levels[0].h_entries;
822 newidxblock = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
823 dst_node = (struct ext2fs_htree_node *)newidxblock;
824 memset(&dst_node->h_fake_dirent, 0,
825 sizeof(dst_node->h_fake_dirent));
826 dst_node->h_fake_dirent.e2d_reclen = htole16(blksize);
828 cursize = roundup(ip->i_size, blksize);
829 dirsize = cursize + blksize;
830 blknum = dirsize / blksize - 1;
831 ext2_dx_csum_set(ip, (struct ext2fs_direct_2 *)newidxblock);
832 error = ext2_htree_append_block(dvp, newidxblock,
836 error = ext2_blkatoff(dvp, cursize, NULL, &dst_bp);
839 dst_node = (struct ext2fs_htree_node *)dst_bp->b_data;
840 dst_entries = dst_node->h_entries;
842 if (info.h_levels_num == 2) {
843 uint16_t src_ent_num, dst_ent_num;
845 if (ext2_htree_get_count(root_entires) ==
846 ext2_htree_get_limit(root_entires)) {
847 SDT_PROBE2(ext2fs, , trace, htree, 1,
848 "directory index is full");
853 src_ent_num = ent_num / 2;
854 dst_ent_num = ent_num - src_ent_num;
855 split_hash = ext2_htree_get_hash(entries + src_ent_num);
857 /* Move half of index entries to the new index node */
858 memcpy(dst_entries, entries + src_ent_num,
859 dst_ent_num * sizeof(struct ext2fs_htree_entry));
860 ext2_htree_set_count(entries, src_ent_num);
861 ext2_htree_set_count(dst_entries, dst_ent_num);
862 ext2_htree_set_limit(dst_entries,
863 ext2_htree_node_limit(ip));
865 if (info.h_levels[1].h_entry >= entries + src_ent_num) {
866 struct buf *tmp = info.h_levels[1].h_bp;
868 info.h_levels[1].h_bp = dst_bp;
871 info.h_levels[1].h_entry =
872 info.h_levels[1].h_entry -
873 (entries + src_ent_num) +
875 info.h_levels[1].h_entries = dst_entries;
877 ext2_htree_insert_entry_to_level(&info.h_levels[0],
880 /* Write new index node to disk */
882 (struct ext2fs_direct_2 *)dst_bp->b_data);
883 error = bwrite(dst_bp);
884 ip->i_flag |= IN_CHANGE | IN_UPDATE;
889 /* Create second level for htree index */
890 struct ext2fs_htree_root *idx_root;
892 memcpy(dst_entries, entries,
893 ent_num * sizeof(struct ext2fs_htree_entry));
894 ext2_htree_set_limit(dst_entries,
895 ext2_htree_node_limit(ip));
897 idx_root = (struct ext2fs_htree_root *)
898 info.h_levels[0].h_bp->b_data;
899 idx_root->h_info.h_ind_levels = 1;
901 ext2_htree_set_count(entries, 1);
902 ext2_htree_set_block(entries, blknum);
904 info.h_levels_num = 2;
905 info.h_levels[1].h_entries = dst_entries;
906 info.h_levels[1].h_entry = info.h_levels[0].h_entry -
907 info.h_levels[0].h_entries + dst_entries;
908 info.h_levels[1].h_bp = dst_bp;
913 leaf_node = info.h_levels[info.h_levels_num - 1].h_entry;
914 blknum = ext2_htree_get_block(leaf_node);
915 error = ext2_blkatoff(dvp, blknum * blksize, NULL, &bp);
919 /* Split target directory block */
920 newdirblock = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
921 ext2_get_hash_seed(fs, hash_seed);
922 ext2_htree_split_dirblock(ip, (char *)bp->b_data, newdirblock, blksize,
923 hash_seed, hash_version, &split_hash, entry);
924 cursize = roundup(ip->i_size, blksize);
925 dirsize = cursize + blksize;
926 blknum = dirsize / blksize - 1;
928 /* Add index entry for the new directory block */
929 ext2_htree_insert_entry(&info, split_hash, blknum);
931 /* Write the new directory block to the end of the directory */
932 ext2_dirent_csum_set(ip, (struct ext2fs_direct_2 *)newdirblock);
933 error = ext2_htree_append_block(dvp, newdirblock, cnp, blksize);
937 /* Write the target directory block */
938 ext2_dirent_csum_set(ip, (struct ext2fs_direct_2 *)bp->b_data);
940 ip->i_flag |= IN_CHANGE | IN_UPDATE;
945 /* Write the index block */
946 error = ext2_htree_writebuf(ip, &info);
951 if (dst_bp != NULL && !write_dst_bp)
953 if (bp != NULL && !write_bp)
955 if (newdirblock != NULL)
956 free(newdirblock, M_TEMP);
957 if (newidxblock != NULL)
958 free(newidxblock, M_TEMP);
960 ext2_htree_release(&info);