2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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;
442 error = VOP_WRITE(vp, &auio, IO_SYNC, cnp->cn_cred);
444 dp->i_size = newsize;
450 ext2_htree_writebuf(struct inode* ip, struct ext2fs_htree_lookup_info *info)
454 for (i = 0; i < info->h_levels_num; i++) {
455 struct buf *bp = info->h_levels[i].h_bp;
456 ext2_dx_csum_set(ip, (struct ext2fs_direct_2 *)bp->b_data);
466 ext2_htree_insert_entry_to_level(struct ext2fs_htree_lookup_level *level,
467 uint32_t hash, uint32_t blk)
469 struct ext2fs_htree_entry *target;
472 target = level->h_entry + 1;
473 entries_num = ext2_htree_get_count(level->h_entries);
475 memmove(target + 1, target, (char *)(level->h_entries + entries_num) -
477 ext2_htree_set_block(target, blk);
478 ext2_htree_set_hash(target, hash);
479 ext2_htree_set_count(level->h_entries, entries_num + 1);
483 * Insert an index entry to the index node.
486 ext2_htree_insert_entry(struct ext2fs_htree_lookup_info *info,
487 uint32_t hash, uint32_t blk)
489 struct ext2fs_htree_lookup_level *level;
491 level = &info->h_levels[info->h_levels_num - 1];
492 ext2_htree_insert_entry_to_level(level, hash, blk);
496 * Compare two entry sort descriptors by name hash value.
497 * This is used together with qsort.
500 ext2_htree_cmp_sort_entry(const void *e1, const void *e2)
502 const struct ext2fs_htree_sort_entry *entry1, *entry2;
504 entry1 = (const struct ext2fs_htree_sort_entry *)e1;
505 entry2 = (const struct ext2fs_htree_sort_entry *)e2;
507 if (le32toh(entry1->h_hash) < le32toh(entry2->h_hash))
509 if (le32toh(entry1->h_hash) > le32toh(entry2->h_hash))
515 * Append an entry to the end of the directory block.
518 ext2_append_entry(char *block, uint32_t blksize,
519 struct ext2fs_direct_2 *last_entry,
520 struct ext2fs_direct_2 *new_entry, int csum_size)
524 entry_len = EXT2_DIR_REC_LEN(last_entry->e2d_namlen);
525 last_entry->e2d_reclen = htole16(entry_len);
526 last_entry = (struct ext2fs_direct_2 *)((char *)last_entry + entry_len);
527 new_entry->e2d_reclen = htole16(block + blksize - (char *)last_entry -
529 memcpy(last_entry, new_entry, EXT2_DIR_REC_LEN(new_entry->e2d_namlen));
533 * Move half of entries from the old directory block to the new one.
536 ext2_htree_split_dirblock(struct inode *ip, char *block1, char *block2,
537 uint32_t blksize, uint32_t *hash_seed, uint8_t hash_version,
538 uint32_t *split_hash, struct ext2fs_direct_2 *entry)
542 int size = 0, csum_size = 0;
545 uint16_t entry_len = 0;
547 struct ext2fs_direct_2 *ep, *last;
549 struct ext2fs_htree_sort_entry *sort_info;
552 ep = (struct ext2fs_direct_2 *)block1;
554 sort_info = (struct ext2fs_htree_sort_entry *)
555 ((char *)block2 + blksize);
557 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
558 csum_size = sizeof(struct ext2fs_direct_tail);
561 * Calculate name hash value for the entry which is to be added.
563 ext2_htree_hash(entry->e2d_name, entry->e2d_namlen, hash_seed,
564 hash_version, &entry_hash, NULL);
567 * Fill in directory entry sort descriptors.
569 while ((char *)ep < block1 + blksize - csum_size) {
570 if (le32toh(ep->e2d_ino) && ep->e2d_namlen) {
573 sort_info->h_size = ep->e2d_reclen;
574 sort_info->h_offset = htole16((char *)ep - block1);
575 ext2_htree_hash(ep->e2d_name, ep->e2d_namlen,
576 hash_seed, hash_version,
577 &sort_info->h_hash, NULL);
578 sort_info->h_hash = htole32(sort_info->h_hash);
580 ep = (struct ext2fs_direct_2 *)
581 ((char *)ep + le16toh(ep->e2d_reclen));
585 * Sort directory entry descriptors by name hash value.
587 qsort(sort_info, entry_cnt, sizeof(struct ext2fs_htree_sort_entry),
588 ext2_htree_cmp_sort_entry);
591 * Count the number of entries to move to directory block 2.
593 for (i = entry_cnt - 1; i >= 0; i--) {
594 if (le16toh(sort_info[i].h_size) + size > blksize / 2)
596 size += le16toh(sort_info[i].h_size);
599 *split_hash = le32toh(sort_info[i + 1].h_hash);
604 if (*split_hash == le32toh(sort_info[i].h_hash))
608 * Move half of directory entries from block 1 to block 2.
610 for (k = i + 1; k < entry_cnt; k++) {
611 ep = (struct ext2fs_direct_2 *)((char *)block1 +
612 le16toh(sort_info[k].h_offset));
613 entry_len = EXT2_DIR_REC_LEN(ep->e2d_namlen);
614 memcpy(dest, ep, entry_len);
615 ((struct ext2fs_direct_2 *)dest)->e2d_reclen =
617 /* Mark directory entry as unused. */
623 /* Shrink directory entries in block 1. */
624 last = (struct ext2fs_direct_2 *)block1;
626 for (offset = 0; offset < blksize - csum_size; ) {
627 ep = (struct ext2fs_direct_2 *)(block1 + offset);
628 offset += le16toh(ep->e2d_reclen);
629 if (le32toh(ep->e2d_ino)) {
630 last = (struct ext2fs_direct_2 *)
631 ((char *)last + entry_len);
632 entry_len = EXT2_DIR_REC_LEN(ep->e2d_namlen);
633 memcpy((void *)last, (void *)ep, entry_len);
634 last->e2d_reclen = htole16(entry_len);
638 if (entry_hash >= *split_hash) {
639 /* Add entry to block 2. */
640 ext2_append_entry(block2, blksize,
641 (struct ext2fs_direct_2 *)dest, entry, csum_size);
643 /* Adjust length field of last entry of block 1. */
644 last->e2d_reclen = htole16(block1 + blksize - (char *)last -
647 /* Add entry to block 1. */
648 ext2_append_entry(block1, blksize, last, entry, csum_size);
650 /* Adjust length field of last entry of block 2. */
651 ((struct ext2fs_direct_2 *)dest)->e2d_reclen =
652 htole16(block2 + blksize - dest - csum_size);
656 ext2_init_dirent_tail(EXT2_DIRENT_TAIL(block1, blksize));
657 ext2_init_dirent_tail(EXT2_DIRENT_TAIL(block2, blksize));
664 * Create an HTree index for a directory
667 ext2_htree_create_index(struct vnode *vp, struct componentname *cnp,
668 struct ext2fs_direct_2 *new_entry)
670 struct buf *bp = NULL;
673 struct m_ext2fs *m_fs;
674 struct ext2fs_direct_2 *ep, *dotdot;
675 struct ext2fs_htree_root *root;
676 struct ext2fs_htree_lookup_info info;
677 uint32_t blksize, dirlen, split_hash;
678 uint32_t hash_seed[4];
679 uint8_t hash_version;
685 fs = dp->i_e2fs->e2fs;
687 blksize = m_fs->e2fs_bsize;
689 buf1 = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
690 buf2 = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
692 if ((error = ext2_blkatoff(vp, 0, NULL, &bp)) != 0)
695 root = (struct ext2fs_htree_root *)bp->b_data;
696 dotdot = (struct ext2fs_direct_2 *)((char *)&(root->h_dotdot));
697 ep = (struct ext2fs_direct_2 *)((char *)dotdot +
698 le16toh(dotdot->e2d_reclen));
699 dirlen = (char *)root + blksize - (char *)ep;
700 memcpy(buf1, ep, dirlen);
701 ep = (struct ext2fs_direct_2 *)buf1;
702 while ((char *)ep < buf1 + dirlen)
703 ep = (struct ext2fs_direct_2 *)
704 ((char *)ep + le16toh(ep->e2d_reclen));
705 ep->e2d_reclen = htole16(buf1 + blksize - (char *)ep);
707 dp->i_flag |= IN_E3INDEX;
710 * Initialize index root.
712 dotdot->e2d_reclen = htole16(blksize - EXT2_DIR_REC_LEN(1));
713 memset(&root->h_info, 0, sizeof(root->h_info));
714 root->h_info.h_hash_version = fs->e3fs_def_hash_version;
715 root->h_info.h_info_len = sizeof(root->h_info);
716 ext2_htree_set_block(root->h_entries, 1);
717 ext2_htree_set_count(root->h_entries, 1);
718 ext2_htree_set_limit(root->h_entries,
719 ext2_htree_root_limit(dp, sizeof(root->h_info)));
721 memset(&info, 0, sizeof(info));
722 info.h_levels_num = 1;
723 info.h_levels[0].h_entries = root->h_entries;
724 info.h_levels[0].h_entry = root->h_entries;
726 hash_version = root->h_info.h_hash_version;
727 if (hash_version <= EXT2_HTREE_TEA)
728 hash_version += m_fs->e2fs_uhash;
729 ext2_get_hash_seed(fs, hash_seed);
730 ext2_htree_split_dirblock(dp, buf1, buf2, blksize, hash_seed,
731 hash_version, &split_hash, new_entry);
732 ext2_htree_insert_entry(&info, split_hash, 2);
735 * Write directory block 0.
737 ext2_dx_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
738 if (DOINGASYNC(vp)) {
744 dp->i_flag |= IN_CHANGE | IN_UPDATE;
749 * Write directory block 1.
751 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)buf1);
752 error = ext2_htree_append_block(vp, buf1, cnp, blksize);
757 * Write directory block 2.
759 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)buf2);
760 error = ext2_htree_append_block(vp, buf2, cnp, blksize);
775 * Add an entry to the directory using htree index.
778 ext2_htree_add_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry,
779 struct componentname *cnp)
781 struct ext2fs_htree_entry *entries, *leaf_node;
782 struct ext2fs_htree_lookup_info info;
783 struct buf *bp = NULL;
785 struct m_ext2fs *m_fs;
788 uint32_t dirhash, split_hash;
789 uint32_t blksize, blknum;
790 uint64_t cursize, dirsize;
791 uint32_t hash_seed[4];
792 uint8_t hash_version;
793 char *newdirblock = NULL;
794 char *newidxblock = NULL;
795 struct ext2fs_htree_node *dst_node;
796 struct ext2fs_htree_entry *dst_entries;
797 struct ext2fs_htree_entry *root_entires;
798 struct buf *dst_bp = NULL;
799 int error, write_bp = 0, write_dst_bp = 0, write_info = 0;
804 blksize = m_fs->e2fs_bsize;
806 if (ip->i_count != 0)
807 return ext2_add_entry(dvp, entry);
809 /* Target directory block is full, split it */
810 memset(&info, 0, sizeof(info));
811 error = ext2_htree_find_leaf(ip, entry->e2d_name, entry->e2d_namlen,
812 &dirhash, &hash_version, &info);
816 entries = info.h_levels[info.h_levels_num - 1].h_entries;
817 ent_num = ext2_htree_get_count(entries);
818 if (ent_num == ext2_htree_get_limit(entries)) {
819 /* Split the index node. */
820 root_entires = info.h_levels[0].h_entries;
821 newidxblock = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
822 dst_node = (struct ext2fs_htree_node *)newidxblock;
823 memset(&dst_node->h_fake_dirent, 0,
824 sizeof(dst_node->h_fake_dirent));
825 dst_node->h_fake_dirent.e2d_reclen = htole16(blksize);
827 cursize = roundup(ip->i_size, blksize);
828 dirsize = cursize + blksize;
829 blknum = dirsize / blksize - 1;
830 ext2_dx_csum_set(ip, (struct ext2fs_direct_2 *)newidxblock);
831 error = ext2_htree_append_block(dvp, newidxblock,
835 error = ext2_blkatoff(dvp, cursize, NULL, &dst_bp);
838 dst_node = (struct ext2fs_htree_node *)dst_bp->b_data;
839 dst_entries = dst_node->h_entries;
841 if (info.h_levels_num == 2) {
842 uint16_t src_ent_num, dst_ent_num;
844 if (ext2_htree_get_count(root_entires) ==
845 ext2_htree_get_limit(root_entires)) {
846 SDT_PROBE2(ext2fs, , trace, htree, 1,
847 "directory index is full");
852 src_ent_num = ent_num / 2;
853 dst_ent_num = ent_num - src_ent_num;
854 split_hash = ext2_htree_get_hash(entries + src_ent_num);
856 /* Move half of index entries to the new index node */
857 memcpy(dst_entries, entries + src_ent_num,
858 dst_ent_num * sizeof(struct ext2fs_htree_entry));
859 ext2_htree_set_count(entries, src_ent_num);
860 ext2_htree_set_count(dst_entries, dst_ent_num);
861 ext2_htree_set_limit(dst_entries,
862 ext2_htree_node_limit(ip));
864 if (info.h_levels[1].h_entry >= entries + src_ent_num) {
865 struct buf *tmp = info.h_levels[1].h_bp;
867 info.h_levels[1].h_bp = dst_bp;
870 info.h_levels[1].h_entry =
871 info.h_levels[1].h_entry -
872 (entries + src_ent_num) +
874 info.h_levels[1].h_entries = dst_entries;
876 ext2_htree_insert_entry_to_level(&info.h_levels[0],
879 /* Write new index node to disk */
881 (struct ext2fs_direct_2 *)dst_bp->b_data);
882 error = bwrite(dst_bp);
883 ip->i_flag |= IN_CHANGE | IN_UPDATE;
888 /* Create second level for htree index */
889 struct ext2fs_htree_root *idx_root;
891 memcpy(dst_entries, entries,
892 ent_num * sizeof(struct ext2fs_htree_entry));
893 ext2_htree_set_limit(dst_entries,
894 ext2_htree_node_limit(ip));
896 idx_root = (struct ext2fs_htree_root *)
897 info.h_levels[0].h_bp->b_data;
898 idx_root->h_info.h_ind_levels = 1;
900 ext2_htree_set_count(entries, 1);
901 ext2_htree_set_block(entries, blknum);
903 info.h_levels_num = 2;
904 info.h_levels[1].h_entries = dst_entries;
905 info.h_levels[1].h_entry = info.h_levels[0].h_entry -
906 info.h_levels[0].h_entries + dst_entries;
907 info.h_levels[1].h_bp = dst_bp;
912 leaf_node = info.h_levels[info.h_levels_num - 1].h_entry;
913 blknum = ext2_htree_get_block(leaf_node);
914 error = ext2_blkatoff(dvp, blknum * blksize, NULL, &bp);
918 /* Split target directory block */
919 newdirblock = malloc(blksize, M_TEMP, M_WAITOK | M_ZERO);
920 ext2_get_hash_seed(fs, hash_seed);
921 ext2_htree_split_dirblock(ip, (char *)bp->b_data, newdirblock, blksize,
922 hash_seed, hash_version, &split_hash, entry);
923 cursize = roundup(ip->i_size, blksize);
924 dirsize = cursize + blksize;
925 blknum = dirsize / blksize - 1;
927 /* Add index entry for the new directory block */
928 ext2_htree_insert_entry(&info, split_hash, blknum);
930 /* Write the new directory block to the end of the directory */
931 ext2_dirent_csum_set(ip, (struct ext2fs_direct_2 *)newdirblock);
932 error = ext2_htree_append_block(dvp, newdirblock, cnp, blksize);
936 /* Write the target directory block */
937 ext2_dirent_csum_set(ip, (struct ext2fs_direct_2 *)bp->b_data);
939 ip->i_flag |= IN_CHANGE | IN_UPDATE;
944 /* Write the index block */
945 error = ext2_htree_writebuf(ip, &info);
950 if (dst_bp != NULL && !write_dst_bp)
952 if (bp != NULL && !write_bp)
954 if (newdirblock != NULL)
955 free(newdirblock, M_TEMP);
956 if (newidxblock != NULL)
957 free(newidxblock, M_TEMP);
959 ext2_htree_release(&info);