xref: /onnv-gate/usr/src/uts/common/fs/hsfs/hsfs_node.c (revision 4866:34d9e5474e96)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52900Sfrankho  * Common Development and Distribution License (the "License").
62900Sfrankho  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*4866Sfrankho  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Directory operations for High Sierra filesystem
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/t_lock.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/systm.h>
360Sstevel@tonic-gate #include <sys/cred.h>
370Sstevel@tonic-gate #include <sys/user.h>
380Sstevel@tonic-gate #include <sys/vfs.h>
390Sstevel@tonic-gate #include <sys/stat.h>
400Sstevel@tonic-gate #include <sys/vnode.h>
410Sstevel@tonic-gate #include <sys/mode.h>
420Sstevel@tonic-gate #include <sys/dnlc.h>
430Sstevel@tonic-gate #include <sys/cmn_err.h>
440Sstevel@tonic-gate #include <sys/fbuf.h>
450Sstevel@tonic-gate #include <sys/kmem.h>
460Sstevel@tonic-gate #include <sys/policy.h>
47494Sfrankho #include <sys/sunddi.h>
480Sstevel@tonic-gate #include <vm/hat.h>
490Sstevel@tonic-gate #include <vm/as.h>
500Sstevel@tonic-gate #include <vm/pvn.h>
510Sstevel@tonic-gate #include <vm/seg.h>
520Sstevel@tonic-gate #include <vm/seg_map.h>
530Sstevel@tonic-gate #include <vm/seg_kmem.h>
540Sstevel@tonic-gate #include <vm/page.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include <sys/fs/hsfs_spec.h>
570Sstevel@tonic-gate #include <sys/fs/hsfs_isospec.h>
580Sstevel@tonic-gate #include <sys/fs/hsfs_node.h>
590Sstevel@tonic-gate #include <sys/fs/hsfs_impl.h>
600Sstevel@tonic-gate #include <sys/fs/hsfs_susp.h>
610Sstevel@tonic-gate #include <sys/fs/hsfs_rrip.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <sys/sysinfo.h>
640Sstevel@tonic-gate #include <sys/sysmacros.h>
650Sstevel@tonic-gate #include <sys/errno.h>
660Sstevel@tonic-gate #include <sys/debug.h>
670Sstevel@tonic-gate #include <fs/fs_subr.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * This macro expects a name that ends in '.' and returns TRUE if the
710Sstevel@tonic-gate  * name is not "." or ".."
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate #define	CAN_TRUNCATE_DOT(name, namelen)	\
740Sstevel@tonic-gate 		(namelen > 1 && (namelen > 2 || name[0] != '.'))
750Sstevel@tonic-gate 
760Sstevel@tonic-gate enum dirblock_result { FOUND_ENTRY, WENT_PAST, HIT_END };
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * These values determine whether we will try to read a file or dir;
800Sstevel@tonic-gate  * they may be patched via /etc/system to allow users to read
810Sstevel@tonic-gate  * record-oriented files.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate int ide_prohibited = IDE_PROHIBITED;
840Sstevel@tonic-gate int hde_prohibited = HDE_PROHIBITED;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * This variable determines if the HSFS code will use the
880Sstevel@tonic-gate  * directory name lookup cache. The default is for the cache to be used.
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate static int hsfs_use_dnlc = 1;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * This variable determines whether strict ISO-9660 directory ordering
940Sstevel@tonic-gate  * is to be assumed.  If false (which it is by default), then when
950Sstevel@tonic-gate  * searching a directory of an ISO-9660 disk, we do not expect the
960Sstevel@tonic-gate  * entries to be sorted (as the spec requires), and so cannot terminate
970Sstevel@tonic-gate  * the search early.  Unfortunately, some vendors are producing
980Sstevel@tonic-gate  * non-compliant disks.  This variable exists to revert to the old
990Sstevel@tonic-gate  * behavior in case someone relies on this. This option is expected to be
1000Sstevel@tonic-gate  * removed at some point in the future.
1010Sstevel@tonic-gate  *
1020Sstevel@tonic-gate  * Use "set hsfs:strict_iso9660_ordering = 1" in /etc/system to override.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate static int strict_iso9660_ordering = 0;
1050Sstevel@tonic-gate 
106*4866Sfrankho /*
107*4866Sfrankho  * This tunable allows us to ignore inode numbers from rrip-1.12.
108*4866Sfrankho  * In this case, we fall back to our default inode algorithm.
109*4866Sfrankho  */
110*4866Sfrankho int use_rrip_inodes = 1;
111*4866Sfrankho 
1120Sstevel@tonic-gate static void hs_hsnode_cache_reclaim(void *unused);
1130Sstevel@tonic-gate static void hs_addfreeb(struct hsfs *fsp, struct hsnode *hp);
1140Sstevel@tonic-gate static enum dirblock_result process_dirblock(struct fbuf *fbp, uint_t *offset,
1150Sstevel@tonic-gate 	uint_t last_offset, char *nm, int nmlen, struct hsfs *fsp,
1160Sstevel@tonic-gate 	struct hsnode *dhp, struct vnode *dvp, struct vnode **vpp,
1172900Sfrankho 	int *error);
1180Sstevel@tonic-gate static int strip_trailing(struct hsfs *fsp, char *nm, int len);
1192900Sfrankho static int hs_namelen(struct hsfs *fsp, char *nm, int len);
1200Sstevel@tonic-gate static int uppercase_cp(char *from, char *to, int size);
1212900Sfrankho static void hs_log_bogus_joliet_warning(void);
1222900Sfrankho static int hs_iso_copy(char *from, char *to, int size);
1232900Sfrankho static int32_t hs_ucs2_2_utf8(uint16_t c_16, uint8_t *s_8);
1242900Sfrankho static int hs_utf8_trunc(uint8_t *str, int len);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * hs_access
1280Sstevel@tonic-gate  * Return 0 if the desired access may be granted.
1290Sstevel@tonic-gate  * Otherwise return error code.
1300Sstevel@tonic-gate  */
1310Sstevel@tonic-gate int
1320Sstevel@tonic-gate hs_access(struct vnode *vp, mode_t m, struct cred *cred)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	struct hsnode *hp;
1350Sstevel@tonic-gate 	int	shift = 0;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	/*
1380Sstevel@tonic-gate 	 * Write access cannot be granted for a read-only medium
1390Sstevel@tonic-gate 	 */
1400Sstevel@tonic-gate 	if ((m & VWRITE) && !IS_DEVVP(vp))
1410Sstevel@tonic-gate 		return (EROFS);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	hp = VTOH(vp);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * XXX - For now, use volume protections.
1470Sstevel@tonic-gate 	 *  Also, always grant EXEC access for directories
1480Sstevel@tonic-gate 	 *  if READ access is granted.
1490Sstevel@tonic-gate 	 */
1500Sstevel@tonic-gate 	if ((vp->v_type == VDIR) && (m & VEXEC)) {
1510Sstevel@tonic-gate 		m &= ~VEXEC;
1520Sstevel@tonic-gate 		m |= VREAD;
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (crgetuid(cred) != hp->hs_dirent.uid) {
1560Sstevel@tonic-gate 		shift += 3;
1570Sstevel@tonic-gate 		if (!groupmember((uid_t)hp->hs_dirent.gid, cred))
1580Sstevel@tonic-gate 			shift += 3;
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	m &= ~(hp->hs_dirent.mode << shift);
1610Sstevel@tonic-gate 	if (m != 0)
1620Sstevel@tonic-gate 		return (secpolicy_vnode_access(cred, vp, hp->hs_dirent.uid, m));
1630Sstevel@tonic-gate 	return (0);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate #if ((HS_HASHSIZE & (HS_HASHSIZE - 1)) == 0)
1670Sstevel@tonic-gate #define	HS_HASH(l)	((uint_t)(l) & (HS_HASHSIZE - 1))
1680Sstevel@tonic-gate #else
1690Sstevel@tonic-gate #define	HS_HASH(l)	((uint_t)(l) % HS_HASHSIZE)
1700Sstevel@tonic-gate #endif
1710Sstevel@tonic-gate #define	HS_HPASH(hp)	HS_HASH((hp)->hs_nodeid)
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * The tunable nhsnode is now a threshold for a dynamically allocated
1750Sstevel@tonic-gate  * pool of hsnodes, not the size of a statically allocated table.
1760Sstevel@tonic-gate  * When the number of hsnodes for a particular file system exceeds
1770Sstevel@tonic-gate  * nhsnode, the allocate and free logic will try to reduce the number
1780Sstevel@tonic-gate  * of allocated nodes by returning unreferenced nodes to the kmem_cache
1790Sstevel@tonic-gate  * instead of putting them on the file system's private free list.
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate int nhsnode = HS_HSNODESPACE / sizeof (struct hsnode);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate struct kmem_cache *hsnode_cache;  /* free hsnode cache */
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * Initialize the cache of free hsnodes.
1870Sstevel@tonic-gate  */
1880Sstevel@tonic-gate void
1890Sstevel@tonic-gate hs_init_hsnode_cache(void)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 	/*
1920Sstevel@tonic-gate 	 * A kmem_cache is used for the hsnodes
1930Sstevel@tonic-gate 	 * No constructor because hsnodes are initialised by bzeroing.
1940Sstevel@tonic-gate 	 */
1950Sstevel@tonic-gate 	hsnode_cache = kmem_cache_create("hsfs_hsnode_cache",
1960Sstevel@tonic-gate 	    sizeof (struct hsnode), 0, NULL,
1970Sstevel@tonic-gate 	    NULL, hs_hsnode_cache_reclaim, NULL, NULL, 0);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate /*
2012900Sfrankho  * Destroy the cache of free hsnodes.
2022900Sfrankho  */
2032900Sfrankho void
2042900Sfrankho hs_fini_hsnode_cache(void)
2052900Sfrankho {
2062900Sfrankho 	kmem_cache_destroy(hsnode_cache);
2072900Sfrankho }
2082900Sfrankho 
2092900Sfrankho /*
2100Sstevel@tonic-gate  * System is short on memory, free up as much as possible
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate /*ARGSUSED*/
2130Sstevel@tonic-gate static void
2140Sstevel@tonic-gate hs_hsnode_cache_reclaim(void *unused)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	struct hsfs *fsp;
2170Sstevel@tonic-gate 	struct hsnode *hp;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	/*
2200Sstevel@tonic-gate 	 * For each vfs in the hs_mounttab list
2210Sstevel@tonic-gate 	 */
2220Sstevel@tonic-gate 	mutex_enter(&hs_mounttab_lock);
2230Sstevel@tonic-gate 	for (fsp = hs_mounttab; fsp != NULL; fsp = fsp->hsfs_next) {
2240Sstevel@tonic-gate 		/*
2250Sstevel@tonic-gate 		 * Purge the dnlc of all hsfs entries
2260Sstevel@tonic-gate 		 */
2270Sstevel@tonic-gate 		(void) dnlc_purge_vfsp(fsp->hsfs_vfs, 0);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 		/*
2300Sstevel@tonic-gate 		 * For each entry in the free chain
2310Sstevel@tonic-gate 		 */
2320Sstevel@tonic-gate 		rw_enter(&fsp->hsfs_hash_lock, RW_WRITER);
2330Sstevel@tonic-gate 		mutex_enter(&fsp->hsfs_free_lock);
2340Sstevel@tonic-gate 		for (hp = fsp->hsfs_free_f; hp != NULL; hp = fsp->hsfs_free_f) {
2350Sstevel@tonic-gate 			/*
2360Sstevel@tonic-gate 			 * Remove from chain
2370Sstevel@tonic-gate 			 */
2380Sstevel@tonic-gate 			fsp->hsfs_free_f = hp->hs_freef;
2390Sstevel@tonic-gate 			if (fsp->hsfs_free_f != NULL) {
2400Sstevel@tonic-gate 				fsp->hsfs_free_f->hs_freeb = NULL;
2410Sstevel@tonic-gate 			} else {
2420Sstevel@tonic-gate 				fsp->hsfs_free_b = NULL;
2430Sstevel@tonic-gate 			}
2440Sstevel@tonic-gate 			/*
2450Sstevel@tonic-gate 			 * Free the node. Force it to be fully freed
2460Sstevel@tonic-gate 			 * by setting the 3rd arg (nopage) to 1.
2470Sstevel@tonic-gate 			 */
2480Sstevel@tonic-gate 			hs_freenode(HTOV(hp), fsp, 1);
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 		mutex_exit(&fsp->hsfs_free_lock);
2510Sstevel@tonic-gate 		rw_exit(&fsp->hsfs_hash_lock);
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 	mutex_exit(&hs_mounttab_lock);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate  * Add an hsnode to the end of the free list.
2580Sstevel@tonic-gate  */
2590Sstevel@tonic-gate static void
2600Sstevel@tonic-gate hs_addfreeb(struct hsfs *fsp, struct hsnode *hp)
2610Sstevel@tonic-gate {
2620Sstevel@tonic-gate 	struct hsnode *ep;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	vn_invalid(HTOV(hp));
2650Sstevel@tonic-gate 	mutex_enter(&fsp->hsfs_free_lock);
2660Sstevel@tonic-gate 	ep = fsp->hsfs_free_b;
2670Sstevel@tonic-gate 	fsp->hsfs_free_b = hp;		/* hp is the last entry in free list */
2680Sstevel@tonic-gate 	hp->hs_freef = NULL;
2690Sstevel@tonic-gate 	hp->hs_freeb = ep;		/* point at previous last entry */
2700Sstevel@tonic-gate 	if (ep == NULL)
2710Sstevel@tonic-gate 		fsp->hsfs_free_f = hp;	/* hp is only entry in free list */
2720Sstevel@tonic-gate 	else
2730Sstevel@tonic-gate 		ep->hs_freef = hp;	/* point previous last entry at hp */
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	mutex_exit(&fsp->hsfs_free_lock);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * Get an hsnode from the front of the free list.
2800Sstevel@tonic-gate  * Must be called with write hsfs_hash_lock held.
2810Sstevel@tonic-gate  */
2820Sstevel@tonic-gate static struct hsnode *
2830Sstevel@tonic-gate hs_getfree(struct hsfs *fsp)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	struct hsnode *hp, **tp;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&fsp->hsfs_hash_lock));
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	/*
2900Sstevel@tonic-gate 	 * If the number of currently-allocated hsnodes is less than
2910Sstevel@tonic-gate 	 * the hsnode count threshold (nhsnode), or if there are no
2920Sstevel@tonic-gate 	 * nodes on the file system's local free list (which acts as a
2930Sstevel@tonic-gate 	 * cache), call kmem_cache_alloc to get a new hsnode from
2940Sstevel@tonic-gate 	 * kernel memory.
2950Sstevel@tonic-gate 	 */
2960Sstevel@tonic-gate 	mutex_enter(&fsp->hsfs_free_lock);
2970Sstevel@tonic-gate 	if ((fsp->hsfs_nohsnode < nhsnode) || (fsp->hsfs_free_f == NULL)) {
2980Sstevel@tonic-gate 		mutex_exit(&fsp->hsfs_free_lock);
2990Sstevel@tonic-gate 		hp = kmem_cache_alloc(hsnode_cache, KM_SLEEP);
3000Sstevel@tonic-gate 		fsp->hsfs_nohsnode++;
3010Sstevel@tonic-gate 		bzero((caddr_t)hp, sizeof (*hp));
3020Sstevel@tonic-gate 		hp->hs_vnode = vn_alloc(KM_SLEEP);
3030Sstevel@tonic-gate 		return (hp);
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	hp = fsp->hsfs_free_f;
3060Sstevel@tonic-gate 	/* hp cannot be NULL, since we already checked this above */
3070Sstevel@tonic-gate 	fsp->hsfs_free_f = hp->hs_freef;
3080Sstevel@tonic-gate 	if (fsp->hsfs_free_f != NULL)
3090Sstevel@tonic-gate 		fsp->hsfs_free_f->hs_freeb = NULL;
3100Sstevel@tonic-gate 	else
3110Sstevel@tonic-gate 		fsp->hsfs_free_b = NULL;
3120Sstevel@tonic-gate 	mutex_exit(&fsp->hsfs_free_lock);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	for (tp = &fsp->hsfs_hash[HS_HPASH(hp)]; *tp != NULL;
315*4866Sfrankho 	    tp = &(*tp)->hs_hash) {
3160Sstevel@tonic-gate 		if (*tp == hp) {
3170Sstevel@tonic-gate 			struct vnode *vp;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 			vp = HTOV(hp);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 			/*
3220Sstevel@tonic-gate 			 * file is no longer referenced, destroy all old pages
3230Sstevel@tonic-gate 			 */
3240Sstevel@tonic-gate 			if (vn_has_cached_data(vp))
3250Sstevel@tonic-gate 				/*
3260Sstevel@tonic-gate 				 * pvn_vplist_dirty will abort all old pages
3270Sstevel@tonic-gate 				 */
3280Sstevel@tonic-gate 				(void) pvn_vplist_dirty(vp, (u_offset_t)0,
329*4866Sfrankho 				    hsfs_putapage, B_INVAL,
330*4866Sfrankho 				    (struct cred *)NULL);
3310Sstevel@tonic-gate 			*tp = hp->hs_hash;
3320Sstevel@tonic-gate 			break;
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 	if (hp->hs_dirent.sym_link != (char *)NULL) {
3360Sstevel@tonic-gate 		kmem_free(hp->hs_dirent.sym_link,
337*4866Sfrankho 		    (size_t)(hp->hs_dirent.ext_size + 1));
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	mutex_destroy(&hp->hs_contents_lock);
3410Sstevel@tonic-gate 	{
3420Sstevel@tonic-gate 		vnode_t	*vp;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 		vp = hp->hs_vnode;
3450Sstevel@tonic-gate 		bzero((caddr_t)hp, sizeof (*hp));
3460Sstevel@tonic-gate 		hp->hs_vnode = vp;
3470Sstevel@tonic-gate 		vn_reinit(vp);
3480Sstevel@tonic-gate 	}
3490Sstevel@tonic-gate 	return (hp);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate /*
3530Sstevel@tonic-gate  * Remove an hsnode from the free list.
3540Sstevel@tonic-gate  */
3550Sstevel@tonic-gate static void
3560Sstevel@tonic-gate hs_remfree(struct hsfs *fsp, struct hsnode *hp)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	mutex_enter(&fsp->hsfs_free_lock);
3590Sstevel@tonic-gate 	if (hp->hs_freef != NULL)
3600Sstevel@tonic-gate 		hp->hs_freef->hs_freeb = hp->hs_freeb;
3610Sstevel@tonic-gate 	else
3620Sstevel@tonic-gate 		fsp->hsfs_free_b = hp->hs_freeb;
3630Sstevel@tonic-gate 	if (hp->hs_freeb != NULL)
3640Sstevel@tonic-gate 		hp->hs_freeb->hs_freef = hp->hs_freef;
3650Sstevel@tonic-gate 	else
3660Sstevel@tonic-gate 		fsp->hsfs_free_f = hp->hs_freef;
3670Sstevel@tonic-gate 	mutex_exit(&fsp->hsfs_free_lock);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate  * Look for hsnode in hash list.
372*4866Sfrankho  * If the inode number is != HS_DUMMY_INO (16), then only the inode
373*4866Sfrankho  * number is used for the check.
374*4866Sfrankho  * If the inode number is == HS_DUMMY_INO, we additionally always
375*4866Sfrankho  * check the directory offset for the file to avoid caching the
376*4866Sfrankho  * meta data for all zero sized to the first zero sized file that
377*4866Sfrankho  * was touched.
378*4866Sfrankho  *
3790Sstevel@tonic-gate  * If found, reactivate it if inactive.
380*4866Sfrankho  *
3810Sstevel@tonic-gate  * Must be entered with hsfs_hash_lock held.
3820Sstevel@tonic-gate  */
3830Sstevel@tonic-gate struct vnode *
384*4866Sfrankho hs_findhash(ino64_t nodeid, uint_t lbn, uint_t off, struct vfs *vfsp)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate 	struct hsnode *tp;
3870Sstevel@tonic-gate 	struct hsfs *fsp;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vfsp);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&fsp->hsfs_hash_lock));
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	for (tp = fsp->hsfs_hash[HS_HASH(nodeid)]; tp != NULL;
3940Sstevel@tonic-gate 	    tp = tp->hs_hash) {
3950Sstevel@tonic-gate 		if (tp->hs_nodeid == nodeid) {
3960Sstevel@tonic-gate 			struct vnode *vp;
3970Sstevel@tonic-gate 
398*4866Sfrankho 			if (nodeid == HS_DUMMY_INO) {
399*4866Sfrankho 				/*
400*4866Sfrankho 				 * If this is the dummy inode number, look for
401*4866Sfrankho 				 * matching dir_lbn and dir_off.
402*4866Sfrankho 				 */
403*4866Sfrankho 				for (; tp != NULL; tp = tp->hs_hash) {
404*4866Sfrankho 					if (tp->hs_nodeid == nodeid &&
405*4866Sfrankho 					    tp->hs_dir_lbn == lbn &&
406*4866Sfrankho 					    tp->hs_dir_off == off)
407*4866Sfrankho 						break;
408*4866Sfrankho 				}
409*4866Sfrankho 				if (tp == NULL)
410*4866Sfrankho 					return (NULL);
411*4866Sfrankho 			}
412*4866Sfrankho 
4130Sstevel@tonic-gate 			mutex_enter(&tp->hs_contents_lock);
4140Sstevel@tonic-gate 			vp = HTOV(tp);
4150Sstevel@tonic-gate 			VN_HOLD(vp);
4160Sstevel@tonic-gate 			if ((tp->hs_flags & HREF) == 0) {
4170Sstevel@tonic-gate 				tp->hs_flags |= HREF;
4180Sstevel@tonic-gate 				/*
4190Sstevel@tonic-gate 				 * reactivating a free hsnode:
4200Sstevel@tonic-gate 				 * remove from free list
4210Sstevel@tonic-gate 				 */
4220Sstevel@tonic-gate 				hs_remfree(fsp, tp);
4230Sstevel@tonic-gate 			}
4240Sstevel@tonic-gate 			mutex_exit(&tp->hs_contents_lock);
4250Sstevel@tonic-gate 			return (vp);
4260Sstevel@tonic-gate 		}
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 	return (NULL);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate static void
4320Sstevel@tonic-gate hs_addhash(struct hsfs *fsp, struct hsnode *hp)
4330Sstevel@tonic-gate {
4340Sstevel@tonic-gate 	ulong_t hashno;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&fsp->hsfs_hash_lock));
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	hashno = HS_HPASH(hp);
4390Sstevel@tonic-gate 	hp->hs_hash = fsp->hsfs_hash[hashno];
4400Sstevel@tonic-gate 	fsp->hsfs_hash[hashno] = hp;
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate  * Destroy all old pages and free the hsnodes
4450Sstevel@tonic-gate  * Return 1 if busy (a hsnode is still referenced).
4460Sstevel@tonic-gate  */
4470Sstevel@tonic-gate int
4480Sstevel@tonic-gate hs_synchash(struct vfs *vfsp)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 	struct hsfs *fsp;
4510Sstevel@tonic-gate 	int i;
4520Sstevel@tonic-gate 	struct hsnode *hp, *nhp;
4530Sstevel@tonic-gate 	int busy = 0;
4540Sstevel@tonic-gate 	struct vnode *vp, *rvp;
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vfsp);
4570Sstevel@tonic-gate 	rvp = fsp->hsfs_rootvp;
4580Sstevel@tonic-gate 	/* make sure no one can come in */
4590Sstevel@tonic-gate 	rw_enter(&fsp->hsfs_hash_lock, RW_WRITER);
4600Sstevel@tonic-gate 	for (i = 0; i < HS_HASHSIZE; i++) {
4610Sstevel@tonic-gate 		for (hp = fsp->hsfs_hash[i]; hp != NULL; hp = hp->hs_hash) {
4620Sstevel@tonic-gate 			vp = HTOV(hp);
4630Sstevel@tonic-gate 			if ((hp->hs_flags & HREF) && (vp != rvp ||
464*4866Sfrankho 			    (vp == rvp && vp->v_count > 1))) {
4650Sstevel@tonic-gate 				busy = 1;
4660Sstevel@tonic-gate 				continue;
4670Sstevel@tonic-gate 			}
4680Sstevel@tonic-gate 			if (vn_has_cached_data(vp))
4690Sstevel@tonic-gate 				(void) pvn_vplist_dirty(vp, (u_offset_t)0,
470*4866Sfrankho 				    hsfs_putapage, B_INVAL,
471*4866Sfrankho 				    (struct cred *)NULL);
4720Sstevel@tonic-gate 		}
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 	if (busy) {
4750Sstevel@tonic-gate 		rw_exit(&fsp->hsfs_hash_lock);
4760Sstevel@tonic-gate 		return (1);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/* now free the hsnodes */
4800Sstevel@tonic-gate 	for (i = 0; i < HS_HASHSIZE; i++) {
4810Sstevel@tonic-gate 		for (hp = fsp->hsfs_hash[i]; hp != NULL; hp = nhp) {
4820Sstevel@tonic-gate 			nhp = hp->hs_hash;
4830Sstevel@tonic-gate 			/*
4840Sstevel@tonic-gate 			 * We know there are no pages associated with
4850Sstevel@tonic-gate 			 * all the hsnodes (they've all been released
4860Sstevel@tonic-gate 			 * above). So remove from free list and
4870Sstevel@tonic-gate 			 * free the entry with nopage set.
4880Sstevel@tonic-gate 			 */
4890Sstevel@tonic-gate 			vp = HTOV(hp);
4900Sstevel@tonic-gate 			if (vp != rvp) {
4910Sstevel@tonic-gate 				hs_remfree(fsp, hp);
4920Sstevel@tonic-gate 				hs_freenode(vp, fsp, 1);
4930Sstevel@tonic-gate 			}
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	ASSERT(fsp->hsfs_nohsnode == 1);
4980Sstevel@tonic-gate 	rw_exit(&fsp->hsfs_hash_lock);
4990Sstevel@tonic-gate 	/* release the root hsnode, this should free the final hsnode */
5000Sstevel@tonic-gate 	VN_RELE(rvp);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	return (0);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate /*
5060Sstevel@tonic-gate  * hs_makenode
5070Sstevel@tonic-gate  *
5080Sstevel@tonic-gate  * Construct an hsnode.
5090Sstevel@tonic-gate  * Caller specifies the directory entry, the block number and offset
5100Sstevel@tonic-gate  * of the directory entry, and the vfs pointer.
5110Sstevel@tonic-gate  * note: off is the sector offset, not lbn offset
5120Sstevel@tonic-gate  * if NULL is returned implies file system hsnode table full
5130Sstevel@tonic-gate  */
5140Sstevel@tonic-gate struct vnode *
5150Sstevel@tonic-gate hs_makenode(
5160Sstevel@tonic-gate 	struct hs_direntry *dp,
5170Sstevel@tonic-gate 	uint_t lbn,
5180Sstevel@tonic-gate 	uint_t off,
5190Sstevel@tonic-gate 	struct vfs *vfsp)
5200Sstevel@tonic-gate {
5210Sstevel@tonic-gate 	struct hsnode *hp;
5220Sstevel@tonic-gate 	struct vnode *vp;
5230Sstevel@tonic-gate 	struct hs_volume *hvp;
5240Sstevel@tonic-gate 	struct vnode *newvp;
5250Sstevel@tonic-gate 	struct hsfs *fsp;
5260Sstevel@tonic-gate 	ino64_t nodeid;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vfsp);
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	/*
531*4866Sfrankho 	 * Construct the data that allows us to re-read the meta data without
532*4866Sfrankho 	 * knowing the name of the file: in the case of a directory
5330Sstevel@tonic-gate 	 * entry, this should point to the canonical dirent, the "."
5340Sstevel@tonic-gate 	 * directory entry for the directory.  This dirent is pointed
5350Sstevel@tonic-gate 	 * to by all directory entries for that dir (including the ".")
5360Sstevel@tonic-gate 	 * entry itself.
5370Sstevel@tonic-gate 	 * In the case of a file, simply point to the dirent for that
538*4866Sfrankho 	 * file (there are hard links in Rock Ridge, so we need to use
539*4866Sfrankho 	 * different data to contruct the node id).
5400Sstevel@tonic-gate 	 */
5410Sstevel@tonic-gate 	if (dp->type == VDIR) {
5420Sstevel@tonic-gate 		lbn = dp->ext_lbn;
5430Sstevel@tonic-gate 		off = 0;
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	/*
5470Sstevel@tonic-gate 	 * Normalize lbn and off before creating a nodeid
5480Sstevel@tonic-gate 	 * and before storing them in a hs_node structure
5490Sstevel@tonic-gate 	 */
5500Sstevel@tonic-gate 	hvp = &fsp->hsfs_vol;
5510Sstevel@tonic-gate 	lbn += off >> hvp->lbn_shift;
5520Sstevel@tonic-gate 	off &= hvp->lbn_maxoffset;
553*4866Sfrankho 	/*
554*4866Sfrankho 	 * If the media carries rrip-v1.12 or newer, and we trust the inodes
555*4866Sfrankho 	 * from the rrip data (use_rrip_inodes != 0), use that data. If the
556*4866Sfrankho 	 * media has been created by a recent mkisofs version, we may trust
557*4866Sfrankho 	 * all numbers in the starting extent number; otherwise, we cannot
558*4866Sfrankho 	 * do this for zero sized files and symlinks, because if we did we'd
559*4866Sfrankho 	 * end up mapping all of them to the same node.
560*4866Sfrankho 	 * We use HS_DUMMY_INO in this case and make sure that we will not
561*4866Sfrankho 	 * map all files to the same meta data.
562*4866Sfrankho 	 */
563*4866Sfrankho 	if (dp->inode != 0 && use_rrip_inodes) {
564*4866Sfrankho 		nodeid = dp->inode;
565*4866Sfrankho 	} else if ((dp->ext_size == 0 || dp->sym_link != (char *)NULL) &&
566*4866Sfrankho 	    (fsp->hsfs_flags & HSFSMNT_INODE) == 0) {
567*4866Sfrankho 		nodeid = HS_DUMMY_INO;
568*4866Sfrankho 	} else {
569*4866Sfrankho 		nodeid = dp->ext_lbn;
570*4866Sfrankho 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	/* look for hsnode in cache first */
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	rw_enter(&fsp->hsfs_hash_lock, RW_READER);
5750Sstevel@tonic-gate 
576*4866Sfrankho 	if ((vp = hs_findhash(nodeid, lbn, off, vfsp)) == NULL) {
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 		/*
5790Sstevel@tonic-gate 		 * Not in cache.  However, someone else may have come
5800Sstevel@tonic-gate 		 * to the same conclusion and just put one in.	Upgrade
5810Sstevel@tonic-gate 		 * our lock to a write lock and look again.
5820Sstevel@tonic-gate 		 */
5830Sstevel@tonic-gate 		rw_exit(&fsp->hsfs_hash_lock);
5840Sstevel@tonic-gate 		rw_enter(&fsp->hsfs_hash_lock, RW_WRITER);
5850Sstevel@tonic-gate 
586*4866Sfrankho 		if ((vp = hs_findhash(nodeid, lbn, off, vfsp)) == NULL) {
5870Sstevel@tonic-gate 			/*
5880Sstevel@tonic-gate 			 * Now we are really sure that the hsnode is not
5890Sstevel@tonic-gate 			 * in the cache.  Get one off freelist or else
5900Sstevel@tonic-gate 			 * allocate one. Either way get a bzeroed hsnode.
5910Sstevel@tonic-gate 			 */
5920Sstevel@tonic-gate 			hp = hs_getfree(fsp);
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 			bcopy((caddr_t)dp, (caddr_t)&hp->hs_dirent,
595*4866Sfrankho 			    sizeof (*dp));
5960Sstevel@tonic-gate 			/*
5970Sstevel@tonic-gate 			 * We've just copied this pointer into hs_dirent,
5980Sstevel@tonic-gate 			 * and don't want 2 references to same symlink.
5990Sstevel@tonic-gate 			 */
6000Sstevel@tonic-gate 			dp->sym_link = (char *)NULL;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 			/*
6030Sstevel@tonic-gate 			 * No need to hold any lock because hsnode is not
6040Sstevel@tonic-gate 			 * yet in the hash chain.
6050Sstevel@tonic-gate 			 */
6060Sstevel@tonic-gate 			mutex_init(&hp->hs_contents_lock, NULL, MUTEX_DEFAULT,
6070Sstevel@tonic-gate 			    NULL);
6080Sstevel@tonic-gate 			hp->hs_dir_lbn = lbn;
6090Sstevel@tonic-gate 			hp->hs_dir_off = off;
6100Sstevel@tonic-gate 			hp->hs_nodeid = nodeid;
6110Sstevel@tonic-gate 			hp->hs_seq = 0;
6120Sstevel@tonic-gate 			hp->hs_flags = HREF;
6130Sstevel@tonic-gate 			if (off > HS_SECTOR_SIZE)
6140Sstevel@tonic-gate 				cmn_err(CE_WARN, "hs_makenode: bad offset");
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 			vp = HTOV(hp);
6170Sstevel@tonic-gate 			vp->v_vfsp = vfsp;
6180Sstevel@tonic-gate 			vp->v_type = dp->type;
6190Sstevel@tonic-gate 			vp->v_rdev = dp->r_dev;
6200Sstevel@tonic-gate 			vn_setops(vp, hsfs_vnodeops);
6210Sstevel@tonic-gate 			vp->v_data = (caddr_t)hp;
6220Sstevel@tonic-gate 			vn_exists(vp);
6230Sstevel@tonic-gate 			/*
6240Sstevel@tonic-gate 			 * if it's a device, call specvp
6250Sstevel@tonic-gate 			 */
6260Sstevel@tonic-gate 			if (IS_DEVVP(vp)) {
6270Sstevel@tonic-gate 				rw_exit(&fsp->hsfs_hash_lock);
6280Sstevel@tonic-gate 				newvp = specvp(vp, vp->v_rdev, vp->v_type,
629*4866Sfrankho 				    CRED());
6300Sstevel@tonic-gate 				if (newvp == NULL)
631*4866Sfrankho 					cmn_err(CE_NOTE,
632*4866Sfrankho 					    "hs_makenode: specvp failed");
6330Sstevel@tonic-gate 				VN_RELE(vp);
6340Sstevel@tonic-gate 				return (newvp);
6350Sstevel@tonic-gate 			}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 			hs_addhash(fsp, hp);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 		}
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	if (dp->sym_link != (char *)NULL) {
6430Sstevel@tonic-gate 		kmem_free(dp->sym_link, (size_t)(dp->ext_size + 1));
6440Sstevel@tonic-gate 		dp->sym_link = (char *)NULL;
6450Sstevel@tonic-gate 	}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	rw_exit(&fsp->hsfs_hash_lock);
6480Sstevel@tonic-gate 	return (vp);
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate  * hs_freenode
6530Sstevel@tonic-gate  *
6540Sstevel@tonic-gate  * Deactivate an hsnode.
6550Sstevel@tonic-gate  * Leave it on the hash list but put it on the free list.
6560Sstevel@tonic-gate  * If the vnode does not have any pages, release the hsnode to the
6570Sstevel@tonic-gate  * kmem_cache using kmem_cache_free, else put in back of the free list.
6580Sstevel@tonic-gate  *
6590Sstevel@tonic-gate  * This function can be called with the hsfs_free_lock held, but only
6600Sstevel@tonic-gate  * when the code is guaranteed to go through the path where the
6610Sstevel@tonic-gate  * node is freed entirely, and not the path where the node could go back
6620Sstevel@tonic-gate  * on the free list (and where the free lock would need to be acquired).
6630Sstevel@tonic-gate  */
6640Sstevel@tonic-gate void
6650Sstevel@tonic-gate hs_freenode(vnode_t *vp, struct hsfs *fsp, int nopage)
6660Sstevel@tonic-gate {
6670Sstevel@tonic-gate 	struct hsnode **tp;
6680Sstevel@tonic-gate 	struct hsnode *hp = VTOH(vp);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&fsp->hsfs_hash_lock));
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if (nopage || (fsp->hsfs_nohsnode >= nhsnode)) {
6730Sstevel@tonic-gate 		/* remove this node from the hash list, if it's there */
6740Sstevel@tonic-gate 		for (tp = &fsp->hsfs_hash[HS_HPASH(hp)]; *tp != NULL;
675*4866Sfrankho 		    tp = &(*tp)->hs_hash) {
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 			if (*tp == hp) {
6780Sstevel@tonic-gate 				*tp = hp->hs_hash;
6790Sstevel@tonic-gate 				break;
6800Sstevel@tonic-gate 			}
6810Sstevel@tonic-gate 		}
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		if (hp->hs_dirent.sym_link != (char *)NULL) {
6840Sstevel@tonic-gate 			kmem_free(hp->hs_dirent.sym_link,
685*4866Sfrankho 			    (size_t)(hp->hs_dirent.ext_size + 1));
6860Sstevel@tonic-gate 			hp->hs_dirent.sym_link = NULL;
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 		if (vn_has_cached_data(vp)) {
6890Sstevel@tonic-gate 			/* clean all old pages */
6900Sstevel@tonic-gate 			(void) pvn_vplist_dirty(vp, (u_offset_t)0,
6910Sstevel@tonic-gate 			    hsfs_putapage, B_INVAL, (struct cred *)NULL);
6920Sstevel@tonic-gate 			/* XXX - can we remove pages by fiat like this??? */
6930Sstevel@tonic-gate 			vp->v_pages = NULL;
6940Sstevel@tonic-gate 		}
6950Sstevel@tonic-gate 		mutex_destroy(&hp->hs_contents_lock);
6960Sstevel@tonic-gate 		vn_invalid(vp);
6970Sstevel@tonic-gate 		vn_free(vp);
6980Sstevel@tonic-gate 		kmem_cache_free(hsnode_cache, hp);
6990Sstevel@tonic-gate 		fsp->hsfs_nohsnode--;
7000Sstevel@tonic-gate 		return;
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 	hs_addfreeb(fsp, hp); /* add to back of free list */
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate /*
7060Sstevel@tonic-gate  * hs_remakenode
7070Sstevel@tonic-gate  *
7080Sstevel@tonic-gate  * Reconstruct a vnode given the location of its directory entry.
7090Sstevel@tonic-gate  * Caller specifies the the block number and offset
7100Sstevel@tonic-gate  * of the directory entry, and the vfs pointer.
7110Sstevel@tonic-gate  * Returns an error code or 0.
7120Sstevel@tonic-gate  */
7130Sstevel@tonic-gate int
7140Sstevel@tonic-gate hs_remakenode(uint_t lbn, uint_t off, struct vfs *vfsp,
7150Sstevel@tonic-gate     struct vnode **vpp)
7160Sstevel@tonic-gate {
7170Sstevel@tonic-gate 	struct buf *secbp;
7180Sstevel@tonic-gate 	struct hsfs *fsp;
7190Sstevel@tonic-gate 	uint_t secno;
7200Sstevel@tonic-gate 	uchar_t *dirp;
7210Sstevel@tonic-gate 	struct hs_direntry hd;
7220Sstevel@tonic-gate 	int error;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	/* Convert to sector and offset */
7250Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vfsp);
7260Sstevel@tonic-gate 	if (off > HS_SECTOR_SIZE) {
7270Sstevel@tonic-gate 		cmn_err(CE_WARN, "hs_remakenode: bad offset");
7280Sstevel@tonic-gate 		error = EINVAL;
7290Sstevel@tonic-gate 		goto end;
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 	secno = LBN_TO_SEC(lbn, vfsp);
7320Sstevel@tonic-gate 	secbp = bread(fsp->hsfs_devvp->v_rdev, secno * 4, HS_SECTOR_SIZE);
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	error = geterror(secbp);
7350Sstevel@tonic-gate 	if (error != 0) {
7360Sstevel@tonic-gate 		cmn_err(CE_NOTE, "hs_remakenode: bread: error=(%d)", error);
7370Sstevel@tonic-gate 		goto end;
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	dirp = (uchar_t *)secbp->b_un.b_addr;
7412900Sfrankho 	error = hs_parsedir(fsp, &dirp[off], &hd, (char *)NULL, (int *)NULL,
742*4866Sfrankho 	    HS_SECTOR_SIZE - off);
7430Sstevel@tonic-gate 	if (!error) {
7440Sstevel@tonic-gate 		*vpp = hs_makenode(&hd, lbn, off, vfsp);
7450Sstevel@tonic-gate 		if (*vpp == NULL)
7460Sstevel@tonic-gate 			error = ENFILE;
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate end:
7500Sstevel@tonic-gate 	brelse(secbp);
7510Sstevel@tonic-gate 	return (error);
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate /*
7560Sstevel@tonic-gate  * hs_dirlook
7570Sstevel@tonic-gate  *
7580Sstevel@tonic-gate  * Look for a given name in a given directory.
7590Sstevel@tonic-gate  * If found, construct an hsnode for it.
7600Sstevel@tonic-gate  */
7610Sstevel@tonic-gate int
7620Sstevel@tonic-gate hs_dirlook(
7630Sstevel@tonic-gate 	struct vnode	*dvp,
7640Sstevel@tonic-gate 	char		*name,
7650Sstevel@tonic-gate 	int		namlen,		/* length of 'name' */
7660Sstevel@tonic-gate 	struct vnode	**vpp,
7670Sstevel@tonic-gate 	struct cred	*cred)
7680Sstevel@tonic-gate {
7690Sstevel@tonic-gate 	struct hsnode *dhp;
7700Sstevel@tonic-gate 	struct hsfs	*fsp;
7710Sstevel@tonic-gate 	int		error = 0;
7720Sstevel@tonic-gate 	uint_t		offset;		/* real offset in directory */
773*4866Sfrankho 	uint_t		last_offset;	/* last index in directory */
7740Sstevel@tonic-gate 	char		*cmpname;	/* case-folded name */
7750Sstevel@tonic-gate 	int		cmpname_size;	/* how much memory we allocate for it */
7760Sstevel@tonic-gate 	int		cmpnamelen;
7770Sstevel@tonic-gate 	int		adhoc_search;	/* did we start at begin of dir? */
7780Sstevel@tonic-gate 	int		end;
7790Sstevel@tonic-gate 	uint_t		hsoffset;
7800Sstevel@tonic-gate 	struct fbuf	*fbp;
7810Sstevel@tonic-gate 	int		bytes_wanted;
7820Sstevel@tonic-gate 	int		dirsiz;
7830Sstevel@tonic-gate 	int		is_rrip;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	if (dvp->v_type != VDIR)
7860Sstevel@tonic-gate 		return (ENOTDIR);
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	if (error = hs_access(dvp, (mode_t)VEXEC, cred))
7890Sstevel@tonic-gate 		return (error);
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	if (hsfs_use_dnlc && (*vpp = dnlc_lookup(dvp, name)))
7920Sstevel@tonic-gate 		return (0);
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	dhp = VTOH(dvp);
7950Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(dvp->v_vfsp);
7962900Sfrankho 	is_rrip = IS_RRIP_IMPLEMENTED(fsp);
7972900Sfrankho 
7982900Sfrankho 	/*
7992900Sfrankho 	 * name == "^A" is illegal for ISO-9660 and Joliet as '..' is '\1' on
8002900Sfrankho 	 * disk. It is no problem for Rock Ridge as RR uses '.' and '..'.
8012900Sfrankho 	 * XXX It could be OK for Joliet also (because namelen == 1 is
8022900Sfrankho 	 * XXX impossible for UCS-2) but then we need a better compare algorith.
8032900Sfrankho 	 */
8042900Sfrankho 	if (!is_rrip && *name == '\1' && namlen == 1)
8052900Sfrankho 		return (EINVAL);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	cmpname_size = (int)(fsp->hsfs_namemax + 1);
8080Sstevel@tonic-gate 	cmpname = kmem_alloc((size_t)cmpname_size, KM_SLEEP);
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	if (namlen >= cmpname_size)
8110Sstevel@tonic-gate 		namlen = cmpname_size - 1;
8120Sstevel@tonic-gate 	/*
8130Sstevel@tonic-gate 	 * For the purposes of comparing the name against dir entries,
8140Sstevel@tonic-gate 	 * fold it to upper case.
8150Sstevel@tonic-gate 	 */
8160Sstevel@tonic-gate 	if (is_rrip) {
817494Sfrankho 		(void) strlcpy(cmpname, name, cmpname_size);
8180Sstevel@tonic-gate 		cmpnamelen = namlen;
8190Sstevel@tonic-gate 	} else {
8200Sstevel@tonic-gate 		/*
8210Sstevel@tonic-gate 		 * If we don't consider a trailing dot as part of the filename,
8220Sstevel@tonic-gate 		 * remove it from the specified name
8230Sstevel@tonic-gate 		 */
8240Sstevel@tonic-gate 		if ((fsp->hsfs_flags & HSFSMNT_NOTRAILDOT) &&
825*4866Sfrankho 		    name[namlen-1] == '.' &&
826*4866Sfrankho 		    CAN_TRUNCATE_DOT(name, namlen))
8270Sstevel@tonic-gate 			name[--namlen] = '\0';
8282900Sfrankho 		if (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2 ||
8292900Sfrankho 		    fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
8302900Sfrankho 			cmpnamelen = hs_iso_copy(name, cmpname, namlen);
8312900Sfrankho 		} else {
8322900Sfrankho 			cmpnamelen = hs_uppercase_copy(name, cmpname, namlen);
8332900Sfrankho 		}
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	/* make sure dirent is filled up with all info */
8370Sstevel@tonic-gate 	if (dhp->hs_dirent.ext_size == 0)
8380Sstevel@tonic-gate 		hs_filldirent(dvp, &dhp->hs_dirent);
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	/*
8410Sstevel@tonic-gate 	 * No lock is needed - hs_offset is used as starting
8420Sstevel@tonic-gate 	 * point for searching the directory.
8430Sstevel@tonic-gate 	 */
8440Sstevel@tonic-gate 	offset = dhp->hs_offset;
8450Sstevel@tonic-gate 	hsoffset = offset;
8460Sstevel@tonic-gate 	adhoc_search = (offset != 0);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	end = dhp->hs_dirent.ext_size;
8490Sstevel@tonic-gate 	dirsiz = end;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate tryagain:
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	while (offset < end) {
854494Sfrankho 		bytes_wanted = MIN(MAXBSIZE, dirsiz - (offset & MAXBMASK));
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 		error = fbread(dvp, (offset_t)(offset & MAXBMASK),
857*4866Sfrankho 		    (unsigned int)bytes_wanted, S_READ, &fbp);
8580Sstevel@tonic-gate 		if (error)
8590Sstevel@tonic-gate 			goto done;
8600Sstevel@tonic-gate 
861494Sfrankho 		last_offset = (offset & MAXBMASK) + fbp->fb_count;
8620Sstevel@tonic-gate 
863494Sfrankho 		switch (process_dirblock(fbp, &offset, last_offset,
8642900Sfrankho 		    cmpname, cmpnamelen, fsp, dhp, dvp, vpp, &error)) {
8650Sstevel@tonic-gate 		case FOUND_ENTRY:
8660Sstevel@tonic-gate 			/* found an entry, either correct or not */
8670Sstevel@tonic-gate 			goto done;
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 		case WENT_PAST:
8700Sstevel@tonic-gate 			/*
8710Sstevel@tonic-gate 			 * If we get here we know we didn't find it on the
8720Sstevel@tonic-gate 			 * first pass. If adhoc_search, then we started a
8730Sstevel@tonic-gate 			 * bit into the dir, and need to wrap around and
8740Sstevel@tonic-gate 			 * search the first entries.  If not, then we started
8750Sstevel@tonic-gate 			 * at the beginning and didn't find it.
8760Sstevel@tonic-gate 			 */
8770Sstevel@tonic-gate 			if (adhoc_search) {
8780Sstevel@tonic-gate 				offset = 0;
8790Sstevel@tonic-gate 				end = hsoffset;
8800Sstevel@tonic-gate 				adhoc_search = 0;
8810Sstevel@tonic-gate 				goto tryagain;
8820Sstevel@tonic-gate 			}
8830Sstevel@tonic-gate 			error = ENOENT;
8840Sstevel@tonic-gate 			goto done;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 		case HIT_END:
8870Sstevel@tonic-gate 			goto tryagain;
8880Sstevel@tonic-gate 		}
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 	/*
8910Sstevel@tonic-gate 	 * End of all dir blocks, didn't find entry.
8920Sstevel@tonic-gate 	 */
8930Sstevel@tonic-gate 	if (adhoc_search) {
8940Sstevel@tonic-gate 		offset = 0;
8950Sstevel@tonic-gate 		end = hsoffset;
8960Sstevel@tonic-gate 		adhoc_search = 0;
8970Sstevel@tonic-gate 		goto tryagain;
8980Sstevel@tonic-gate 	}
8990Sstevel@tonic-gate 	error = ENOENT;
9000Sstevel@tonic-gate done:
9010Sstevel@tonic-gate 	/*
9020Sstevel@tonic-gate 	 * If we found the entry, add it to the DNLC
9030Sstevel@tonic-gate 	 * If the entry is a device file (assuming we support Rock Ridge),
9040Sstevel@tonic-gate 	 * we enter the device vnode to the cache since that is what
9050Sstevel@tonic-gate 	 * is in *vpp.
9060Sstevel@tonic-gate 	 * That is ok since the CD-ROM is read-only, so (dvp,name) will
9070Sstevel@tonic-gate 	 * always point to the same device.
9080Sstevel@tonic-gate 	 */
9090Sstevel@tonic-gate 	if (hsfs_use_dnlc && !error)
9100Sstevel@tonic-gate 		dnlc_enter(dvp, name, *vpp);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	kmem_free(cmpname, (size_t)cmpname_size);
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	return (error);
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate /*
9180Sstevel@tonic-gate  * hs_parsedir
9190Sstevel@tonic-gate  *
9200Sstevel@tonic-gate  * Parse a Directory Record into an hs_direntry structure.
9210Sstevel@tonic-gate  * High Sierra and ISO directory are almost the same
9220Sstevel@tonic-gate  * except the flag and date
9230Sstevel@tonic-gate  */
9240Sstevel@tonic-gate int
9250Sstevel@tonic-gate hs_parsedir(
9260Sstevel@tonic-gate 	struct hsfs		*fsp,
9270Sstevel@tonic-gate 	uchar_t			*dirp,
9280Sstevel@tonic-gate 	struct hs_direntry	*hdp,
9290Sstevel@tonic-gate 	char			*dnp,
9302900Sfrankho 	int			*dnlen,
931*4866Sfrankho 	int			last_offset)	/* last offset in dirp */
9320Sstevel@tonic-gate {
9330Sstevel@tonic-gate 	char	*on_disk_name;
9340Sstevel@tonic-gate 	int	on_disk_namelen;
9352900Sfrankho 	int	on_disk_dirlen;
9360Sstevel@tonic-gate 	uchar_t	flags;
9370Sstevel@tonic-gate 	int	namelen;
9380Sstevel@tonic-gate 	int	error;
9390Sstevel@tonic-gate 	int	name_change_flag = 0;	/* set if name was gotten in SUA */
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	hdp->ext_lbn = HDE_EXT_LBN(dirp);
9420Sstevel@tonic-gate 	hdp->ext_size = HDE_EXT_SIZE(dirp);
9430Sstevel@tonic-gate 	hdp->xar_len = HDE_XAR_LEN(dirp);
9440Sstevel@tonic-gate 	hdp->intlf_sz = HDE_INTRLV_SIZE(dirp);
9450Sstevel@tonic-gate 	hdp->intlf_sk = HDE_INTRLV_SKIP(dirp);
9460Sstevel@tonic-gate 	hdp->sym_link = (char *)NULL;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	if (fsp->hsfs_vol_type == HS_VOL_TYPE_HS) {
9490Sstevel@tonic-gate 		flags = HDE_FLAGS(dirp);
9500Sstevel@tonic-gate 		hs_parse_dirdate(HDE_cdate(dirp), &hdp->cdate);
9510Sstevel@tonic-gate 		hs_parse_dirdate(HDE_cdate(dirp), &hdp->adate);
9520Sstevel@tonic-gate 		hs_parse_dirdate(HDE_cdate(dirp), &hdp->mdate);
9530Sstevel@tonic-gate 		if ((flags & hde_prohibited) == 0) {
9540Sstevel@tonic-gate 			/*
9550Sstevel@tonic-gate 			 * Skip files with the associated bit set.
9560Sstevel@tonic-gate 			 */
9570Sstevel@tonic-gate 			if (flags & HDE_ASSOCIATED)
9580Sstevel@tonic-gate 				return (EAGAIN);
9590Sstevel@tonic-gate 			hdp->type = VREG;
9600Sstevel@tonic-gate 			hdp->mode = HFREG;
9610Sstevel@tonic-gate 			hdp->nlink = 1;
9620Sstevel@tonic-gate 		} else if ((flags & hde_prohibited) == HDE_DIRECTORY) {
9630Sstevel@tonic-gate 			hdp->type = VDIR;
9640Sstevel@tonic-gate 			hdp->mode = HFDIR;
9650Sstevel@tonic-gate 			hdp->nlink = 2;
9660Sstevel@tonic-gate 		} else {
9670Sstevel@tonic-gate 			hs_log_bogus_disk_warning(fsp,
968494Sfrankho 			    HSFS_ERR_UNSUP_TYPE, flags);
9690Sstevel@tonic-gate 			return (EINVAL);
9700Sstevel@tonic-gate 		}
9710Sstevel@tonic-gate 		hdp->uid = fsp -> hsfs_vol.vol_uid;
9720Sstevel@tonic-gate 		hdp->gid = fsp -> hsfs_vol.vol_gid;
9730Sstevel@tonic-gate 		hdp->mode = hdp-> mode | (fsp -> hsfs_vol.vol_prot & 0777);
9742900Sfrankho 	} else if ((fsp->hsfs_vol_type == HS_VOL_TYPE_ISO) ||
975*4866Sfrankho 	    (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2) ||
976*4866Sfrankho 	    (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET)) {
9772900Sfrankho 
9780Sstevel@tonic-gate 		flags = IDE_FLAGS(dirp);
9790Sstevel@tonic-gate 		hs_parse_dirdate(IDE_cdate(dirp), &hdp->cdate);
9800Sstevel@tonic-gate 		hs_parse_dirdate(IDE_cdate(dirp), &hdp->adate);
9810Sstevel@tonic-gate 		hs_parse_dirdate(IDE_cdate(dirp), &hdp->mdate);
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 		if ((flags & ide_prohibited) == 0) {
9840Sstevel@tonic-gate 			/*
9850Sstevel@tonic-gate 			 * Skip files with the associated bit set.
9860Sstevel@tonic-gate 			 */
9870Sstevel@tonic-gate 			if (flags & IDE_ASSOCIATED)
9880Sstevel@tonic-gate 				return (EAGAIN);
9890Sstevel@tonic-gate 			hdp->type = VREG;
9900Sstevel@tonic-gate 			hdp->mode = HFREG;
9910Sstevel@tonic-gate 			hdp->nlink = 1;
9920Sstevel@tonic-gate 		} else if ((flags & ide_prohibited) == IDE_DIRECTORY) {
9930Sstevel@tonic-gate 			hdp->type = VDIR;
9940Sstevel@tonic-gate 			hdp->mode = HFDIR;
9950Sstevel@tonic-gate 			hdp->nlink = 2;
9960Sstevel@tonic-gate 		} else {
9970Sstevel@tonic-gate 			hs_log_bogus_disk_warning(fsp,
998494Sfrankho 			    HSFS_ERR_UNSUP_TYPE, flags);
9990Sstevel@tonic-gate 			return (EINVAL);
10000Sstevel@tonic-gate 		}
10010Sstevel@tonic-gate 		hdp->uid = fsp -> hsfs_vol.vol_uid;
10020Sstevel@tonic-gate 		hdp->gid = fsp -> hsfs_vol.vol_gid;
10030Sstevel@tonic-gate 		hdp->mode = hdp-> mode | (fsp -> hsfs_vol.vol_prot & 0777);
1004*4866Sfrankho 		hdp->inode = 0;		/* initialize with 0, then check rrip */
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 		/*
10070Sstevel@tonic-gate 		 * Having this all filled in, let's see if we have any
10080Sstevel@tonic-gate 		 * SUA susp to look at.
10090Sstevel@tonic-gate 		 */
10100Sstevel@tonic-gate 		if (IS_SUSP_IMPLEMENTED(fsp)) {
10110Sstevel@tonic-gate 			error = parse_sua((uchar_t *)dnp, dnlen,
1012*4866Sfrankho 			    &name_change_flag, dirp, last_offset,
1013*4866Sfrankho 			    hdp, fsp,
1014*4866Sfrankho 			    (uchar_t *)NULL, NULL);
10150Sstevel@tonic-gate 			if (error) {
10160Sstevel@tonic-gate 				if (hdp->sym_link) {
10170Sstevel@tonic-gate 					kmem_free(hdp->sym_link,
1018*4866Sfrankho 					    (size_t)(hdp->ext_size + 1));
10190Sstevel@tonic-gate 					hdp->sym_link = (char *)NULL;
10200Sstevel@tonic-gate 				}
10210Sstevel@tonic-gate 				return (error);
10220Sstevel@tonic-gate 			}
10230Sstevel@tonic-gate 		}
10240Sstevel@tonic-gate 	}
10250Sstevel@tonic-gate 	hdp->xar_prot = (HDE_PROTECTION & flags) != 0;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate #if dontskip
10280Sstevel@tonic-gate 	if (hdp->xar_len > 0) {
10290Sstevel@tonic-gate 		cmn_err(CE_NOTE, "hsfs: extended attributes not supported");
10300Sstevel@tonic-gate 		return (EINVAL);
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate #endif
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	/* check interleaf size and skip factor */
10350Sstevel@tonic-gate 	/* must both be zero or non-zero */
10360Sstevel@tonic-gate 	if (hdp->intlf_sz + hdp->intlf_sk) {
10370Sstevel@tonic-gate 		if ((hdp->intlf_sz == 0) || (hdp->intlf_sk == 0)) {
10380Sstevel@tonic-gate 			cmn_err(CE_NOTE,
1039*4866Sfrankho 			    "hsfs: interleaf size or skip factor error");
10400Sstevel@tonic-gate 			return (EINVAL);
10410Sstevel@tonic-gate 		}
10420Sstevel@tonic-gate 		if (hdp->ext_size == 0) {
10430Sstevel@tonic-gate 			cmn_err(CE_NOTE,
10440Sstevel@tonic-gate 			    "hsfs: interleaving specified on zero length file");
10450Sstevel@tonic-gate 			return (EINVAL);
10460Sstevel@tonic-gate 		}
10470Sstevel@tonic-gate 	}
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	if (HDE_VOL_SET(dirp) != 1) {
10500Sstevel@tonic-gate 		if (fsp->hsfs_vol.vol_set_size != 1 &&
10510Sstevel@tonic-gate 		    fsp->hsfs_vol.vol_set_size != HDE_VOL_SET(dirp)) {
10520Sstevel@tonic-gate 			cmn_err(CE_NOTE, "hsfs: multivolume file?");
10530Sstevel@tonic-gate 			return (EINVAL);
10540Sstevel@tonic-gate 		}
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	/*
10580Sstevel@tonic-gate 	 * If the name changed, then the NM field for RRIP was hit and
10590Sstevel@tonic-gate 	 * we should not copy the name again, just return.
10600Sstevel@tonic-gate 	 */
10610Sstevel@tonic-gate 	if (NAME_HAS_CHANGED(name_change_flag))
10620Sstevel@tonic-gate 		return (0);
10630Sstevel@tonic-gate 
1064494Sfrankho 	/*
1065494Sfrankho 	 * Fall back to the ISO name. Note that as in process_dirblock,
1066494Sfrankho 	 * the on-disk filename length must be validated against ISO
1067494Sfrankho 	 * limits - which, in case of RR present but no RR name found,
1068494Sfrankho 	 * are NOT identical to fsp->hsfs_namemax on this filesystem.
1069494Sfrankho 	 */
10700Sstevel@tonic-gate 	on_disk_name = (char *)HDE_name(dirp);
10710Sstevel@tonic-gate 	on_disk_namelen = (int)HDE_NAME_LEN(dirp);
10722900Sfrankho 	on_disk_dirlen = (int)HDE_DIR_LEN(dirp);
10730Sstevel@tonic-gate 
10742900Sfrankho 	if (on_disk_dirlen < HDE_ROOT_DIR_REC_SIZE ||
10752900Sfrankho 	    ((on_disk_dirlen > last_offset) ||
10762900Sfrankho 	    ((HDE_FDESIZE + on_disk_namelen) > on_disk_dirlen))) {
1077*4866Sfrankho 		hs_log_bogus_disk_warning(fsp,
1078*4866Sfrankho 		    HSFS_ERR_BAD_DIR_ENTRY, 0);
10792900Sfrankho 		return (EINVAL);
10802900Sfrankho 	}
10812900Sfrankho 
10822900Sfrankho 	if (on_disk_namelen > fsp->hsfs_namelen &&
10832900Sfrankho 	    hs_namelen(fsp, on_disk_name, on_disk_namelen) >
1084*4866Sfrankho 	    fsp->hsfs_namelen) {
10852900Sfrankho 		hs_log_bogus_disk_warning(fsp,
1086*4866Sfrankho 		    fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET ?
1087*4866Sfrankho 		    HSFS_ERR_BAD_JOLIET_FILE_LEN :
1088*4866Sfrankho 		    HSFS_ERR_BAD_FILE_LEN, 0);
10890Sstevel@tonic-gate 	}
10902900Sfrankho 	if (on_disk_namelen > ISO_NAMELEN_V2_MAX)
10912900Sfrankho 		on_disk_namelen = fsp->hsfs_namemax;	/* Paranoia */
10922900Sfrankho 
10930Sstevel@tonic-gate 	if (dnp != NULL) {
10942900Sfrankho 		if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
10952900Sfrankho 			namelen = hs_jnamecopy(on_disk_name, dnp,
1096*4866Sfrankho 			    on_disk_namelen, fsp->hsfs_namemax,
1097*4866Sfrankho 			    fsp->hsfs_flags);
10982900Sfrankho 			/*
10992900Sfrankho 			 * A negative return value means that the file name
11002900Sfrankho 			 * has been truncated to fsp->hsfs_namemax.
11012900Sfrankho 			 */
11022900Sfrankho 			if (namelen < 0) {
11032900Sfrankho 				namelen = -namelen;
11042900Sfrankho 				hs_log_bogus_disk_warning(fsp,
1105*4866Sfrankho 				    HSFS_ERR_TRUNC_JOLIET_FILE_LEN, 0);
11062900Sfrankho 			}
11072900Sfrankho 		} else {
11082900Sfrankho 			/*
11092900Sfrankho 			 * HS_VOL_TYPE_ISO && HS_VOL_TYPE_ISO_V2
11102900Sfrankho 			 */
11112900Sfrankho 			namelen = hs_namecopy(on_disk_name, dnp,
1112*4866Sfrankho 			    on_disk_namelen, fsp->hsfs_flags);
11132900Sfrankho 		}
11142900Sfrankho 		if (namelen == 0)
11152900Sfrankho 			return (EINVAL);
11160Sstevel@tonic-gate 		if ((fsp->hsfs_flags & HSFSMNT_NOTRAILDOT) &&
11170Sstevel@tonic-gate 		    dnp[ namelen-1 ] == '.' && CAN_TRUNCATE_DOT(dnp, namelen))
11180Sstevel@tonic-gate 			dnp[ --namelen ] = '\0';
11190Sstevel@tonic-gate 	} else
11200Sstevel@tonic-gate 		namelen = on_disk_namelen;
11210Sstevel@tonic-gate 	if (dnlen != NULL)
11220Sstevel@tonic-gate 		*dnlen = namelen;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	return (0);
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate /*
11280Sstevel@tonic-gate  * hs_namecopy
11290Sstevel@tonic-gate  *
11300Sstevel@tonic-gate  * Parse a file/directory name into UNIX form.
11310Sstevel@tonic-gate  * Delete trailing blanks, upper-to-lower case, add NULL terminator.
11320Sstevel@tonic-gate  * Returns the (possibly new) length.
11332900Sfrankho  *
11342900Sfrankho  * Called from hsfs_readdir() via hs_parsedir()
11350Sstevel@tonic-gate  */
11360Sstevel@tonic-gate int
11370Sstevel@tonic-gate hs_namecopy(char *from, char *to, int size, ulong_t flags)
11380Sstevel@tonic-gate {
11390Sstevel@tonic-gate 	uint_t i;
11400Sstevel@tonic-gate 	uchar_t c;
11410Sstevel@tonic-gate 	int lastspace;
11420Sstevel@tonic-gate 	int maplc;
11432900Sfrankho 	int trailspace;
11442900Sfrankho 	int version;
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	/* special handling for '.' and '..' */
11470Sstevel@tonic-gate 	if (size == 1) {
11480Sstevel@tonic-gate 		if (*from == '\0') {
11490Sstevel@tonic-gate 			*to++ = '.';
11500Sstevel@tonic-gate 			*to = '\0';
11510Sstevel@tonic-gate 			return (1);
11520Sstevel@tonic-gate 		} else if (*from == '\1') {
11530Sstevel@tonic-gate 			*to++ = '.';
11540Sstevel@tonic-gate 			*to++ = '.';
11550Sstevel@tonic-gate 			*to = '\0';
11560Sstevel@tonic-gate 			return (2);
11570Sstevel@tonic-gate 		}
11580Sstevel@tonic-gate 	}
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	maplc = (flags & HSFSMNT_NOMAPLCASE) == 0;
11612900Sfrankho 	trailspace = (flags & HSFSMNT_NOTRAILSPACE) == 0;
11622900Sfrankho 	version = (flags & HSFSMNT_NOVERSION) == 0;
11630Sstevel@tonic-gate 	for (i = 0, lastspace = -1; i < size; i++) {
11640Sstevel@tonic-gate 		c = from[i];
11652900Sfrankho 		if (c == ';' && version)
11660Sstevel@tonic-gate 			break;
11672900Sfrankho 		if (c <= ' ' && !trailspace) {
11680Sstevel@tonic-gate 			if (lastspace == -1)
11690Sstevel@tonic-gate 				lastspace = i;
11700Sstevel@tonic-gate 		} else
11710Sstevel@tonic-gate 			lastspace = -1;
11720Sstevel@tonic-gate 		if (maplc && (c >= 'A') && (c <= 'Z'))
11730Sstevel@tonic-gate 			c += 'a' - 'A';
11740Sstevel@tonic-gate 		to[i] = c;
11750Sstevel@tonic-gate 	}
11760Sstevel@tonic-gate 	if (lastspace != -1)
11770Sstevel@tonic-gate 		i = lastspace;
11780Sstevel@tonic-gate 	to[i] = '\0';
11790Sstevel@tonic-gate 	return (i);
11800Sstevel@tonic-gate }
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate /*
11832900Sfrankho  * hs_jnamecopy
11842900Sfrankho  *
11852900Sfrankho  * This is the Joliet variant of hs_namecopy()
11862900Sfrankho  *
11872900Sfrankho  * Parse a UCS-2 Joliet file/directory name into UNIX form.
11882900Sfrankho  * Add NULL terminator.
11892900Sfrankho  * Returns the new length.
11902900Sfrankho  *
11912900Sfrankho  * Called from hsfs_readdir() via hs_parsedir()
11922900Sfrankho  */
11932900Sfrankho int
11942900Sfrankho hs_jnamecopy(char *from, char *to, int size, int maxsize, ulong_t flags)
11952900Sfrankho {
11962900Sfrankho 	uint_t i;
11972900Sfrankho 	uint_t len;
11982900Sfrankho 	uint16_t c;
11992900Sfrankho 	int	amt;
12002900Sfrankho 	int	version;
12012900Sfrankho 
12022900Sfrankho 	/* special handling for '.' and '..' */
12032900Sfrankho 	if (size == 1) {
12042900Sfrankho 		if (*from == '\0') {
12052900Sfrankho 			*to++ = '.';
12062900Sfrankho 			*to = '\0';
12072900Sfrankho 			return (1);
12082900Sfrankho 		} else if (*from == '\1') {
12092900Sfrankho 			*to++ = '.';
12102900Sfrankho 			*to++ = '.';
12112900Sfrankho 			*to = '\0';
12122900Sfrankho 			return (2);
12132900Sfrankho 		}
12142900Sfrankho 	}
12152900Sfrankho 
12162900Sfrankho 	version = (flags & HSFSMNT_NOVERSION) == 0;
12172900Sfrankho 	for (i = 0, len = 0; i < size; i++) {
12182900Sfrankho 		c = (from[i++] & 0xFF) << 8;
12192900Sfrankho 		c |= from[i] & 0xFF;
12202900Sfrankho 		if (c == ';' && version)
12212900Sfrankho 			break;
12222900Sfrankho 
12232900Sfrankho 		if (len > (maxsize-3)) {
12242900Sfrankho 			if (c < 0x80)
12252900Sfrankho 				amt = 1;
12262900Sfrankho 			else if (c < 0x800)
12272900Sfrankho 				amt = 2;
12282900Sfrankho 			else
12292900Sfrankho 				amt = 3;
12302900Sfrankho 			if ((len+amt) > maxsize) {
12312900Sfrankho 				to[len] = '\0';
12322900Sfrankho 				return (-len);
12332900Sfrankho 			}
12342900Sfrankho 		}
12352900Sfrankho 		amt = hs_ucs2_2_utf8(c, (uint8_t *)&to[len]);
12362900Sfrankho 		if (amt == 0) {
12372900Sfrankho 			hs_log_bogus_joliet_warning(); /* should never happen */
12382900Sfrankho 			return (0);
12392900Sfrankho 		}
12402900Sfrankho 		len += amt;
12412900Sfrankho 	}
12422900Sfrankho 	to[len] = '\0';
12432900Sfrankho 	return (len);
12442900Sfrankho }
12452900Sfrankho 
12462900Sfrankho /*
12470Sstevel@tonic-gate  * map a filename to upper case;
12480Sstevel@tonic-gate  * return 1 if found lowercase character
12492900Sfrankho  *
12502900Sfrankho  * Called from process_dirblock()
12512900Sfrankho  * via hsfs_lookup() -> hs_dirlook() -> process_dirblock()
12522900Sfrankho  * to create an intermedia name from on disk file names for
12532900Sfrankho  * comparing names.
12540Sstevel@tonic-gate  */
12550Sstevel@tonic-gate static int
12560Sstevel@tonic-gate uppercase_cp(char *from, char *to, int size)
12570Sstevel@tonic-gate {
12580Sstevel@tonic-gate 	uint_t i;
12590Sstevel@tonic-gate 	uchar_t c;
12600Sstevel@tonic-gate 	uchar_t had_lc = 0;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
12630Sstevel@tonic-gate 		c = *from++;
12640Sstevel@tonic-gate 		if ((c >= 'a') && (c <= 'z')) {
12650Sstevel@tonic-gate 			c -= ('a' - 'A');
12660Sstevel@tonic-gate 			had_lc = 1;
12670Sstevel@tonic-gate 		}
12680Sstevel@tonic-gate 		*to++ = c;
12690Sstevel@tonic-gate 	}
12700Sstevel@tonic-gate 	return (had_lc);
12710Sstevel@tonic-gate }
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate /*
12742900Sfrankho  * This is the Joliet variant of uppercase_cp()
12752900Sfrankho  *
12762900Sfrankho  * map a UCS-2 filename to UTF-8;
12772900Sfrankho  * return new length
12782900Sfrankho  *
12792900Sfrankho  * Called from process_dirblock()
12802900Sfrankho  * via hsfs_lookup() -> hs_dirlook() -> process_dirblock()
12812900Sfrankho  * to create an intermedia name from on disk file names for
12822900Sfrankho  * comparing names.
12832900Sfrankho  */
12842900Sfrankho int
12852900Sfrankho hs_joliet_cp(char *from, char *to, int size)
12862900Sfrankho {
12872900Sfrankho 	uint_t		i;
12882900Sfrankho 	uint16_t	c;
12892900Sfrankho 	int		len = 0;
12902900Sfrankho 	int		amt;
12912900Sfrankho 
12922900Sfrankho 	/* special handling for '\0' and '\1' */
12932900Sfrankho 	if (size == 1) {
12942900Sfrankho 		*to = *from;
12952900Sfrankho 		return (1);
12962900Sfrankho 	}
12972900Sfrankho 	for (i = 0; i < size; i += 2) {
12982900Sfrankho 		c = (*from++ & 0xFF) << 8;
12992900Sfrankho 		c |= *from++ & 0xFF;
13002900Sfrankho 
13012900Sfrankho 		amt = hs_ucs2_2_utf8(c, (uint8_t *)to);
13022900Sfrankho 		if (amt == 0) {
13032900Sfrankho 			hs_log_bogus_joliet_warning(); /* should never happen */
13042900Sfrankho 			return (0);
13052900Sfrankho 		}
13062900Sfrankho 
13072900Sfrankho 		to  += amt;
13082900Sfrankho 		len += amt;
13092900Sfrankho 	}
13102900Sfrankho 	return (len);
13112900Sfrankho }
13122900Sfrankho 
13132900Sfrankho static void
13142900Sfrankho hs_log_bogus_joliet_warning(void)
13152900Sfrankho {
13162900Sfrankho 	static int	warned = 0;
13172900Sfrankho 
13182900Sfrankho 	if (warned)
13192900Sfrankho 		return;
13202900Sfrankho 	warned = 1;
13212900Sfrankho 	cmn_err(CE_CONT, "hsfs: Warning: "
1322*4866Sfrankho 	    "file name contains bad UCS-2 chacarter\n");
13232900Sfrankho }
13242900Sfrankho 
13252900Sfrankho 
13262900Sfrankho /*
13270Sstevel@tonic-gate  * hs_uppercase_copy
13280Sstevel@tonic-gate  *
13292900Sfrankho  * Convert a UNIX-style name into its HSFS equivalent
13302900Sfrankho  * replacing '.' and '..' with '\0' and '\1'.
13310Sstevel@tonic-gate  * Map to upper case.
13320Sstevel@tonic-gate  * Returns the (possibly new) length.
13332900Sfrankho  *
13342900Sfrankho  * Called from hs_dirlook() and rrip_namecopy()
13352900Sfrankho  * to create an intermediate name from the callers name from hsfs_lookup()
13362900Sfrankho  * XXX Is the call from rrip_namecopy() OK?
13370Sstevel@tonic-gate  */
13380Sstevel@tonic-gate int
13390Sstevel@tonic-gate hs_uppercase_copy(char *from, char *to, int size)
13400Sstevel@tonic-gate {
13410Sstevel@tonic-gate 	uint_t i;
13420Sstevel@tonic-gate 	uchar_t c;
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	/* special handling for '.' and '..' */
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	if (size == 1 && *from == '.') {
13470Sstevel@tonic-gate 		*to = '\0';
13480Sstevel@tonic-gate 		return (1);
13490Sstevel@tonic-gate 	} else if (size == 2 && *from == '.' && *(from+1) == '.') {
13500Sstevel@tonic-gate 		*to = '\1';
13510Sstevel@tonic-gate 		return (1);
13520Sstevel@tonic-gate 	}
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
13550Sstevel@tonic-gate 		c = *from++;
13560Sstevel@tonic-gate 		if ((c >= 'a') && (c <= 'z'))
13570Sstevel@tonic-gate 			c = c - 'a' + 'A';
13580Sstevel@tonic-gate 		*to++ = c;
13590Sstevel@tonic-gate 	}
13600Sstevel@tonic-gate 	return (size);
13610Sstevel@tonic-gate }
13620Sstevel@tonic-gate 
13632900Sfrankho /*
13642900Sfrankho  * hs_iso_copy
13652900Sfrankho  *
13662900Sfrankho  * This is the Joliet/ISO-9660:1999 variant of hs_uppercase_copy()
13672900Sfrankho  *
13682900Sfrankho  * Convert a UTF-8 UNIX-style name into its UTF-8 Joliet/ISO equivalent
13692900Sfrankho  * replacing '.' and '..' with '\0' and '\1'.
13702900Sfrankho  * Returns the (possibly new) length.
13712900Sfrankho  *
13722900Sfrankho  * Called from hs_dirlook()
13732900Sfrankho  * to create an intermediate name from the callers name from hsfs_lookup()
13742900Sfrankho  */
13752900Sfrankho static int
13762900Sfrankho hs_iso_copy(char *from, char *to, int size)
13772900Sfrankho {
13782900Sfrankho 	uint_t i;
13792900Sfrankho 	uchar_t c;
13802900Sfrankho 
13812900Sfrankho 	/* special handling for '.' and '..' */
13822900Sfrankho 
13832900Sfrankho 	if (size == 1 && *from == '.') {
13842900Sfrankho 		*to = '\0';
13852900Sfrankho 		return (1);
13862900Sfrankho 	} else if (size == 2 && *from == '.' && *(from+1) == '.') {
13872900Sfrankho 		*to = '\1';
13882900Sfrankho 		return (1);
13892900Sfrankho 	}
13902900Sfrankho 
13912900Sfrankho 	for (i = 0; i < size; i++) {
13922900Sfrankho 		c = *from++;
13932900Sfrankho 		*to++ = c;
13942900Sfrankho 	}
13952900Sfrankho 	return (size);
13962900Sfrankho }
13972900Sfrankho 
13980Sstevel@tonic-gate void
13990Sstevel@tonic-gate hs_filldirent(struct vnode *vp, struct hs_direntry *hdp)
14000Sstevel@tonic-gate {
14010Sstevel@tonic-gate 	struct buf *secbp;
14020Sstevel@tonic-gate 	uint_t	secno;
14030Sstevel@tonic-gate 	offset_t secoff;
14040Sstevel@tonic-gate 	struct hsfs *fsp;
14050Sstevel@tonic-gate 	uchar_t *secp;
14060Sstevel@tonic-gate 	int	error;
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	if (vp->v_type != VDIR) {
14090Sstevel@tonic-gate 		cmn_err(CE_WARN, "hsfs_filldirent: vp (0x%p) not a directory",
1410*4866Sfrankho 		    (void *)vp);
14110Sstevel@tonic-gate 		return;
14120Sstevel@tonic-gate 	}
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vp ->v_vfsp);
14150Sstevel@tonic-gate 	secno = LBN_TO_SEC(hdp->ext_lbn+hdp->xar_len, vp->v_vfsp);
14160Sstevel@tonic-gate 	secoff = LBN_TO_BYTE(hdp->ext_lbn+hdp->xar_len, vp->v_vfsp) &
1417*4866Sfrankho 	    MAXHSOFFSET;
14180Sstevel@tonic-gate 	secbp = bread(fsp->hsfs_devvp->v_rdev, secno * 4, HS_SECTOR_SIZE);
14190Sstevel@tonic-gate 	error = geterror(secbp);
14200Sstevel@tonic-gate 	if (error != 0) {
14210Sstevel@tonic-gate 		cmn_err(CE_NOTE, "hs_filldirent: bread: error=(%d)", error);
14220Sstevel@tonic-gate 		goto end;
14230Sstevel@tonic-gate 	}
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	secp = (uchar_t *)secbp->b_un.b_addr;
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	/* quick check */
14280Sstevel@tonic-gate 	if (hdp->ext_lbn != HDE_EXT_LBN(&secp[secoff])) {
14290Sstevel@tonic-gate 		cmn_err(CE_NOTE, "hsfs_filldirent: dirent not match");
14300Sstevel@tonic-gate 		/* keep on going */
14310Sstevel@tonic-gate 	}
14322900Sfrankho 	(void) hs_parsedir(fsp, &secp[secoff], hdp, (char *)NULL,
1433*4866Sfrankho 	    (int *)NULL, HS_SECTOR_SIZE - secoff);
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate end:
14360Sstevel@tonic-gate 	brelse(secbp);
14370Sstevel@tonic-gate }
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate /*
14400Sstevel@tonic-gate  * Look through a directory block for a matching entry.
14410Sstevel@tonic-gate  * Note: this routine does an fbrelse() on the buffer passed in.
14420Sstevel@tonic-gate  */
14430Sstevel@tonic-gate static enum dirblock_result
14440Sstevel@tonic-gate process_dirblock(
14450Sstevel@tonic-gate 	struct fbuf	*fbp,		/* buffer containing dirblk */
14460Sstevel@tonic-gate 	uint_t		*offset,	/* lower index */
14470Sstevel@tonic-gate 	uint_t		last_offset,	/* upper index */
14480Sstevel@tonic-gate 	char		*nm,		/* upcase nm to compare against */
14490Sstevel@tonic-gate 	int		nmlen,		/* length of name */
14500Sstevel@tonic-gate 	struct hsfs	*fsp,
14510Sstevel@tonic-gate 	struct hsnode	*dhp,
14520Sstevel@tonic-gate 	struct vnode	*dvp,
14530Sstevel@tonic-gate 	struct vnode	**vpp,
14542900Sfrankho 	int		*error)		/* return value: errno */
14550Sstevel@tonic-gate {
14560Sstevel@tonic-gate 	uchar_t		*blkp = (uchar_t *)fbp->fb_addr; /* dir block */
14570Sstevel@tonic-gate 	char		*dname;		/* name in directory entry */
14580Sstevel@tonic-gate 	int		dnamelen;	/* length of name */
14590Sstevel@tonic-gate 	struct hs_direntry hd;
14600Sstevel@tonic-gate 	int		hdlen;
1461*4866Sfrankho 	uchar_t		*dirp;		/* the directory entry */
14620Sstevel@tonic-gate 	int		res;
14630Sstevel@tonic-gate 	int		parsedir_res;
14642900Sfrankho 	int		is_rrip;
14650Sstevel@tonic-gate 	size_t		rrip_name_size;
1466494Sfrankho 	int		rr_namelen = 0;
14670Sstevel@tonic-gate 	char		*rrip_name_str = NULL;
14680Sstevel@tonic-gate 	char		*rrip_tmp_name = NULL;
14690Sstevel@tonic-gate 	enum dirblock_result err = 0;
14700Sstevel@tonic-gate 	int 		did_fbrelse = 0;
14712900Sfrankho 	char		uppercase_name[JOLIET_NAMELEN_MAX*3 + 1]; /* 331 */
14720Sstevel@tonic-gate 
1473494Sfrankho #define	PD_return(retval)	\
1474494Sfrankho 	{ err = retval; goto do_ret; }		/* return after cleanup */
1475494Sfrankho #define	rel_offset(offset)	\
1476494Sfrankho 	((offset) & MAXBOFFSET)			/* index into cur blk */
14772900Sfrankho #define	RESTORE_NM(tmp, orig)	\
14782900Sfrankho 	if (is_rrip && *(tmp) != '\0') \
14792900Sfrankho 		(void) strcpy((orig), (tmp))
14800Sstevel@tonic-gate 
14812900Sfrankho 	is_rrip = IS_RRIP_IMPLEMENTED(fsp);
14820Sstevel@tonic-gate 	if (is_rrip) {
14830Sstevel@tonic-gate 		rrip_name_size = RRIP_FILE_NAMELEN + 1;
14840Sstevel@tonic-gate 		rrip_name_str = kmem_alloc(rrip_name_size, KM_SLEEP);
14850Sstevel@tonic-gate 		rrip_tmp_name = kmem_alloc(rrip_name_size, KM_SLEEP);
14860Sstevel@tonic-gate 		rrip_name_str[0] = '\0';
14870Sstevel@tonic-gate 		rrip_tmp_name[0] = '\0';
14880Sstevel@tonic-gate 	}
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	while (*offset < last_offset) {
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 		/*
14930Sstevel@tonic-gate 		 * Directory Entries cannot span sectors.
1494494Sfrankho 		 *
1495494Sfrankho 		 * Unused bytes at the end of each sector are zeroed
1496494Sfrankho 		 * according to ISO9660, but we cannot rely on this
1497494Sfrankho 		 * since both media failures and maliciously corrupted
1498494Sfrankho 		 * media may return arbitrary values.
1499494Sfrankho 		 * We therefore have to check for consistency:
1500494Sfrankho 		 * The size of a directory entry must be at least
1501494Sfrankho 		 * 34 bytes (the size of the directory entry metadata),
1502494Sfrankho 		 * or zero (indicating the end-of-sector condition).
1503494Sfrankho 		 * For a non-zero directory entry size of less than
1504494Sfrankho 		 * 34 Bytes, log a warning.
1505494Sfrankho 		 * In any case, skip the rest of this sector and
1506494Sfrankho 		 * continue with the next.
15070Sstevel@tonic-gate 		 */
15080Sstevel@tonic-gate 		hdlen = (int)((uchar_t)
1509494Sfrankho 		    HDE_DIR_LEN(&blkp[rel_offset(*offset)]));
15100Sstevel@tonic-gate 
1511494Sfrankho 		if (hdlen < HDE_ROOT_DIR_REC_SIZE ||
1512494Sfrankho 		    *offset + hdlen > last_offset) {
1513494Sfrankho 			/*
1514494Sfrankho 			 * Advance to the next sector boundary
1515494Sfrankho 			 */
1516494Sfrankho 			*offset = roundup(*offset + 1, HS_SECTOR_SIZE);
1517494Sfrankho 			if (hdlen)
1518494Sfrankho 				hs_log_bogus_disk_warning(fsp,
1519494Sfrankho 				    HSFS_ERR_TRAILING_JUNK, 0);
1520494Sfrankho 			continue;
15210Sstevel@tonic-gate 		}
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 		bzero(&hd, sizeof (hd));
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 		/*
1526494Sfrankho 		 * Check the filename length in the ISO record for
1527494Sfrankho 		 * plausibility and reset it to a safe value, in case
1528494Sfrankho 		 * the name length byte is out of range. Since the ISO
1529494Sfrankho 		 * name will be used as fallback if the rockridge name
1530494Sfrankho 		 * is invalid/nonexistant, we must make sure not to
1531494Sfrankho 		 * blow the bounds and initialize dnamelen to a sensible
1532494Sfrankho 		 * value within the limits of ISO9660.
1533494Sfrankho 		 * In addition to that, the ISO filename is part of the
1534494Sfrankho 		 * directory entry. If the filename length is too large
1535494Sfrankho 		 * to fit, the record is invalid and we'll advance to
1536494Sfrankho 		 * the next.
15370Sstevel@tonic-gate 		 */
15380Sstevel@tonic-gate 		dirp = &blkp[rel_offset(*offset)];
15390Sstevel@tonic-gate 		dname = (char *)HDE_name(dirp);
15400Sstevel@tonic-gate 		dnamelen = (int)((uchar_t)HDE_NAME_LEN(dirp));
15412900Sfrankho 		/*
15422900Sfrankho 		 * If the directory entry extends beyond the end of the
15432900Sfrankho 		 * block, it must be invalid. Skip it.
15442900Sfrankho 		 */
1545494Sfrankho 		if (dnamelen > hdlen - HDE_FDESIZE) {
15460Sstevel@tonic-gate 			hs_log_bogus_disk_warning(fsp,
15472900Sfrankho 			    HSFS_ERR_BAD_DIR_ENTRY, 0);
1548494Sfrankho 			goto skip_rec;
15492900Sfrankho 		} else if (dnamelen > fsp->hsfs_namelen &&
1550*4866Sfrankho 		    hs_namelen(fsp, dname, dnamelen) > fsp->hsfs_namelen) {
1551494Sfrankho 			hs_log_bogus_disk_warning(fsp,
1552*4866Sfrankho 			    fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET ?
1553*4866Sfrankho 			    HSFS_ERR_BAD_JOLIET_FILE_LEN :
1554*4866Sfrankho 			    HSFS_ERR_BAD_FILE_LEN, 0);
15550Sstevel@tonic-gate 		}
15562900Sfrankho 		if (dnamelen > ISO_NAMELEN_V2_MAX)
15572900Sfrankho 			dnamelen = fsp->hsfs_namemax;	/* Paranoia */
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 		/*
15600Sstevel@tonic-gate 		 * If the rock ridge is implemented, then we copy the name
15610Sstevel@tonic-gate 		 * from the SUA area to rrip_name_str. If no Alternate
15620Sstevel@tonic-gate 		 * name is found, then use the uppercase NM in the
15630Sstevel@tonic-gate 		 * rrip_name_str char array.
15640Sstevel@tonic-gate 		 */
15650Sstevel@tonic-gate 		if (is_rrip) {
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 			rrip_name_str[0] = '\0';
15680Sstevel@tonic-gate 			rr_namelen = rrip_namecopy(nm, &rrip_name_str[0],
1569*4866Sfrankho 			    &rrip_tmp_name[0], dirp, last_offset - *offset,
1570*4866Sfrankho 			    fsp, &hd);
15710Sstevel@tonic-gate 			if (hd.sym_link) {
15720Sstevel@tonic-gate 				kmem_free(hd.sym_link,
15730Sstevel@tonic-gate 				    (size_t)(hd.ext_size+1));
15740Sstevel@tonic-gate 				hd.sym_link = (char *)NULL;
15750Sstevel@tonic-gate 			}
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 			if (rr_namelen != -1) {
15780Sstevel@tonic-gate 				dname = (char *)&rrip_name_str[0];
15790Sstevel@tonic-gate 				dnamelen = rr_namelen;
15800Sstevel@tonic-gate 			}
15810Sstevel@tonic-gate 		}
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 		if (!is_rrip || rr_namelen == -1) {
15840Sstevel@tonic-gate 			/* use iso name instead */
15850Sstevel@tonic-gate 
15862900Sfrankho 			int i = -1;
15870Sstevel@tonic-gate 			/*
15880Sstevel@tonic-gate 			 * make sure that we get rid of ';' in the dname of
15890Sstevel@tonic-gate 			 * an iso direntry, as we should have no knowledge
15900Sstevel@tonic-gate 			 * of file versions.
15912900Sfrankho 			 *
15922900Sfrankho 			 * XXX This is done the wrong way: it does not take
15932900Sfrankho 			 * XXX care of the fact that the version string is
15942900Sfrankho 			 * XXX a decimal number in the range 1 to 32767.
15950Sstevel@tonic-gate 			 */
15962900Sfrankho 			if ((fsp->hsfs_flags & HSFSMNT_NOVERSION) == 0) {
15972900Sfrankho 				if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
15982900Sfrankho 					for (i = dnamelen - 1; i > 0; i -= 2) {
15992900Sfrankho 						if (dname[i] == ';' &&
16002900Sfrankho 						    dname[i-1] == '\0') {
16012900Sfrankho 							--i;
16022900Sfrankho 							break;
16032900Sfrankho 						}
16042900Sfrankho 					}
16052900Sfrankho 				} else {
16062900Sfrankho 					for (i = dnamelen - 1; i > 0; i--) {
16072900Sfrankho 						if (dname[i] == ';')
16082900Sfrankho 							break;
16092900Sfrankho 					}
16102900Sfrankho 				}
16112900Sfrankho 			}
16122900Sfrankho 			if (i > 0) {
16132900Sfrankho 				dnamelen = i;
16142900Sfrankho 			} else if (fsp->hsfs_vol_type != HS_VOL_TYPE_ISO_V2 &&
1615*4866Sfrankho 			    fsp->hsfs_vol_type != HS_VOL_TYPE_JOLIET) {
16162900Sfrankho 				dnamelen = strip_trailing(fsp, dname, dnamelen);
16172900Sfrankho 			}
16180Sstevel@tonic-gate 
16192900Sfrankho 			ASSERT(dnamelen < sizeof (uppercase_name));
16200Sstevel@tonic-gate 
16212900Sfrankho 			if (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2) {
16222900Sfrankho 				(void) strncpy(uppercase_name, dname, dnamelen);
16232900Sfrankho 			} else if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
16242900Sfrankho 				dnamelen = hs_joliet_cp(dname, uppercase_name,
1625*4866Sfrankho 				    dnamelen);
16262900Sfrankho 			} else if (uppercase_cp(dname, uppercase_name,
1627*4866Sfrankho 			    dnamelen)) {
16280Sstevel@tonic-gate 				hs_log_bogus_disk_warning(fsp,
1629494Sfrankho 				    HSFS_ERR_LOWER_CASE_NM, 0);
16302900Sfrankho 			}
16310Sstevel@tonic-gate 			dname = uppercase_name;
1632494Sfrankho 			if (!is_rrip &&
1633494Sfrankho 			    (fsp->hsfs_flags & HSFSMNT_NOTRAILDOT) &&
1634494Sfrankho 			    dname[dnamelen - 1] == '.' &&
1635494Sfrankho 			    CAN_TRUNCATE_DOT(dname, dnamelen))
1636494Sfrankho 				dname[--dnamelen] = '\0';
16370Sstevel@tonic-gate 		}
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 		/*
16400Sstevel@tonic-gate 		 * Quickly screen for a non-matching entry, but not for RRIP.
16410Sstevel@tonic-gate 		 * This test doesn't work for lowercase vs. uppercase names.
16420Sstevel@tonic-gate 		 */
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 		/* if we saw a lower case name we can't do this test either */
16450Sstevel@tonic-gate 		if (strict_iso9660_ordering && !is_rrip &&
1646494Sfrankho 		    !HSFS_HAVE_LOWER_CASE(fsp) && *nm < *dname) {
16470Sstevel@tonic-gate 			RESTORE_NM(rrip_tmp_name, nm);
16480Sstevel@tonic-gate 			PD_return(WENT_PAST)
16490Sstevel@tonic-gate 		}
16500Sstevel@tonic-gate 
1651494Sfrankho 		if (*nm != *dname || nmlen != dnamelen)
1652494Sfrankho 			goto skip_rec;
16530Sstevel@tonic-gate 
16542900Sfrankho 		if ((res = bcmp(dname, nm, nmlen)) == 0) {
16550Sstevel@tonic-gate 			/* name matches */
1656494Sfrankho 			parsedir_res = hs_parsedir(fsp, dirp, &hd,
16572900Sfrankho 			    (char *)NULL, (int *)NULL,
1658*4866Sfrankho 			    last_offset - *offset);
16590Sstevel@tonic-gate 			if (!parsedir_res) {
16600Sstevel@tonic-gate 				uint_t lbn;	/* logical block number */
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 				lbn = dhp->hs_dirent.ext_lbn +
1663494Sfrankho 				    dhp->hs_dirent.xar_len;
16640Sstevel@tonic-gate 				/*
16650Sstevel@tonic-gate 				 * Need to do an fbrelse() on the buffer,
16660Sstevel@tonic-gate 				 * as hs_makenode() may try to acquire
16670Sstevel@tonic-gate 				 * hs_hashlock, which may not be required
16680Sstevel@tonic-gate 				 * while a page is locked.
16690Sstevel@tonic-gate 				 */
16700Sstevel@tonic-gate 				fbrelse(fbp, S_READ);
16710Sstevel@tonic-gate 				did_fbrelse = 1;
1672494Sfrankho 				*vpp = hs_makenode(&hd, lbn, *offset,
1673494Sfrankho 				    dvp->v_vfsp);
16740Sstevel@tonic-gate 				if (*vpp == NULL) {
16750Sstevel@tonic-gate 					*error = ENFILE;
16760Sstevel@tonic-gate 					RESTORE_NM(rrip_tmp_name, nm);
16770Sstevel@tonic-gate 					PD_return(FOUND_ENTRY)
16780Sstevel@tonic-gate 				}
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 				dhp->hs_offset = *offset;
16810Sstevel@tonic-gate 				RESTORE_NM(rrip_tmp_name, nm);
16820Sstevel@tonic-gate 				PD_return(FOUND_ENTRY)
16830Sstevel@tonic-gate 			} else if (parsedir_res != EAGAIN) {
16840Sstevel@tonic-gate 				/* improper dir entry */
16850Sstevel@tonic-gate 				*error = parsedir_res;
16860Sstevel@tonic-gate 				RESTORE_NM(rrip_tmp_name, nm);
16870Sstevel@tonic-gate 				PD_return(FOUND_ENTRY)
16880Sstevel@tonic-gate 			}
16890Sstevel@tonic-gate 		} else if (strict_iso9660_ordering && !is_rrip &&
1690*4866Sfrankho 		    !HSFS_HAVE_LOWER_CASE(fsp) && res < 0) {
16910Sstevel@tonic-gate 			/* name < dir entry */
16920Sstevel@tonic-gate 			RESTORE_NM(rrip_tmp_name, nm);
16930Sstevel@tonic-gate 			PD_return(WENT_PAST)
16940Sstevel@tonic-gate 		}
16950Sstevel@tonic-gate 		/*
16960Sstevel@tonic-gate 		 * name > dir entry,
16970Sstevel@tonic-gate 		 * look at next one.
16980Sstevel@tonic-gate 		 */
1699494Sfrankho skip_rec:
17000Sstevel@tonic-gate 		*offset += hdlen;
17010Sstevel@tonic-gate 		RESTORE_NM(rrip_tmp_name, nm);
17020Sstevel@tonic-gate 	}
17030Sstevel@tonic-gate 	PD_return(HIT_END)
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate do_ret:
17060Sstevel@tonic-gate 	if (rrip_name_str)
17070Sstevel@tonic-gate 		kmem_free(rrip_name_str, rrip_name_size);
17080Sstevel@tonic-gate 	if (rrip_tmp_name)
17090Sstevel@tonic-gate 		kmem_free(rrip_tmp_name, rrip_name_size);
1710494Sfrankho 	if (!did_fbrelse)
17110Sstevel@tonic-gate 		fbrelse(fbp, S_READ);
17120Sstevel@tonic-gate 	return (err);
17130Sstevel@tonic-gate #undef PD_return
17142900Sfrankho #undef RESTORE_NM
17150Sstevel@tonic-gate }
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate /*
17180Sstevel@tonic-gate  * Strip trailing nulls or spaces from the name;
17190Sstevel@tonic-gate  * return adjusted length.  If we find such junk,
17200Sstevel@tonic-gate  * log a non-conformant disk message.
17210Sstevel@tonic-gate  */
17220Sstevel@tonic-gate static int
17230Sstevel@tonic-gate strip_trailing(struct hsfs *fsp, char *nm, int len)
17240Sstevel@tonic-gate {
17250Sstevel@tonic-gate 	char *c;
17260Sstevel@tonic-gate 	int trailing_junk = 0;
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	for (c = nm + len - 1; c > nm; c--) {
17290Sstevel@tonic-gate 		if (*c == ' ' || *c == '\0')
17300Sstevel@tonic-gate 			trailing_junk = 1;
17310Sstevel@tonic-gate 		else
17320Sstevel@tonic-gate 			break;
17330Sstevel@tonic-gate 	}
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	if (trailing_junk)
17360Sstevel@tonic-gate 		hs_log_bogus_disk_warning(fsp, HSFS_ERR_TRAILING_JUNK, 0);
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 	return ((int)(c - nm + 1));
17390Sstevel@tonic-gate }
17402900Sfrankho 
17412900Sfrankho static int
17422900Sfrankho hs_namelen(struct hsfs *fsp, char *nm, int len)
17432900Sfrankho {
17442900Sfrankho 	char	*p = nm + len;
17452900Sfrankho 
17462900Sfrankho 	if (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2) {
17472900Sfrankho 		return (len);
17482900Sfrankho 	} else if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
17492900Sfrankho 		uint16_t c;
17502900Sfrankho 
17512900Sfrankho 		while (--p > &nm[1]) {
17522900Sfrankho 			c = *p;
17532900Sfrankho 			c |= *--p * 256;
17542900Sfrankho 			if (c == ';')
17552900Sfrankho 				return (p - nm);
17562900Sfrankho 			if (c < '0' || c > '9') {
17572900Sfrankho 				p++;
17582900Sfrankho 				return (p - nm);
17592900Sfrankho 			}
17602900Sfrankho 		}
17612900Sfrankho 	} else {
17622900Sfrankho 		char	c;
17632900Sfrankho 
17642900Sfrankho 		while (--p > nm) {
17652900Sfrankho 			c = *p;
17662900Sfrankho 			if (c == ';')
17672900Sfrankho 				return (p - nm);
17682900Sfrankho 			if (c < '0' || c > '9') {
17692900Sfrankho 				p++;
17702900Sfrankho 				return (p - nm);
17712900Sfrankho 			}
17722900Sfrankho 		}
17732900Sfrankho 	}
17742900Sfrankho 	return (len);
17752900Sfrankho }
17762900Sfrankho 
17772900Sfrankho /*
17782900Sfrankho  * Take a UCS-2 character and convert
17792900Sfrankho  * it into a utf8 character.
17802900Sfrankho  * A 0 will be returned if the conversion fails
17812900Sfrankho  *
17822900Sfrankho  * See http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
17832900Sfrankho  *
17842900Sfrankho  * The code has been taken from udfs/udf_subr.c
17852900Sfrankho  */
17862900Sfrankho static uint8_t hs_first_byte_mark[7] =
17872900Sfrankho 			{ 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
17882900Sfrankho static int32_t
17892900Sfrankho hs_ucs2_2_utf8(uint16_t c_16, uint8_t *s_8)
17902900Sfrankho {
17912900Sfrankho 	int32_t nc;
17922900Sfrankho 	uint32_t c_32;
17932900Sfrankho 	uint32_t byte_mask = 0xBF;
17942900Sfrankho 	uint32_t byte_mark = 0x80;
17952900Sfrankho 
17962900Sfrankho 	/*
17972900Sfrankho 	 * Convert the 16-bit character to a 32-bit character
17982900Sfrankho 	 */
17992900Sfrankho 	c_32 = c_16;
18002900Sfrankho 
18012900Sfrankho 	/*
18022900Sfrankho 	 * By here the 16-bit character is converted
18032900Sfrankho 	 * to a 32-bit wide character
18042900Sfrankho 	 */
18052900Sfrankho 	if (c_32 < 0x80) {
18062900Sfrankho 		nc = 1;
18072900Sfrankho 	} else if (c_32 < 0x800) {
18082900Sfrankho 		nc = 2;
18092900Sfrankho 	} else if (c_32 < 0x10000) {
18102900Sfrankho 		nc = 3;
18112900Sfrankho 	} else if (c_32 < 0x200000) {
18122900Sfrankho 		nc = 4;
18132900Sfrankho 	} else if (c_32 < 0x4000000) {
18142900Sfrankho 		nc = 5;
18152900Sfrankho 	} else if (c_32 <= 0x7FFFFFFF) {	/* avoid signed overflow */
18162900Sfrankho 		nc = 6;
18172900Sfrankho 	} else {
18182900Sfrankho 		nc = 0;
18192900Sfrankho 	}
18202900Sfrankho 	s_8 += nc;
18212900Sfrankho 	switch (nc) {
18222900Sfrankho 		case 6 :
18232900Sfrankho 			*(--s_8) = (c_32 | byte_mark)  & byte_mask;
18242900Sfrankho 			c_32 >>= 6;
18252900Sfrankho 			/* FALLTHROUGH */
18262900Sfrankho 		case 5 :
18272900Sfrankho 			*(--s_8) = (c_32 | byte_mark)  & byte_mask;
18282900Sfrankho 			c_32 >>= 6;
18292900Sfrankho 			/* FALLTHROUGH */
18302900Sfrankho 		case 4 :
18312900Sfrankho 			*(--s_8) = (c_32 | byte_mark)  & byte_mask;
18322900Sfrankho 			c_32 >>= 6;
18332900Sfrankho 			/* FALLTHROUGH */
18342900Sfrankho 		case 3 :
18352900Sfrankho 			*(--s_8) = (c_32 | byte_mark)  & byte_mask;
18362900Sfrankho 			c_32 >>= 6;
18372900Sfrankho 			/* FALLTHROUGH */
18382900Sfrankho 		case 2 :
18392900Sfrankho 			*(--s_8) = (c_32 | byte_mark)  & byte_mask;
18402900Sfrankho 			c_32 >>= 6;
18412900Sfrankho 			/* FALLTHROUGH */
18422900Sfrankho 		case 1 :
18432900Sfrankho 			*(--s_8) = c_32 | hs_first_byte_mark[nc];
18442900Sfrankho 	}
18452900Sfrankho 	return (nc);
18462900Sfrankho }
1847