xref: /onnv-gate/usr/src/uts/common/fs/hsfs/hsfs_node.c (revision 12273:63678502e95e)
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*12273SCasper.Dik@Sun.COM  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  * Directory operations for High Sierra filesystem
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/t_lock.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/cred.h>
340Sstevel@tonic-gate #include <sys/user.h>
350Sstevel@tonic-gate #include <sys/vfs.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include <sys/vnode.h>
380Sstevel@tonic-gate #include <sys/mode.h>
390Sstevel@tonic-gate #include <sys/dnlc.h>
400Sstevel@tonic-gate #include <sys/cmn_err.h>
410Sstevel@tonic-gate #include <sys/fbuf.h>
420Sstevel@tonic-gate #include <sys/kmem.h>
430Sstevel@tonic-gate #include <sys/policy.h>
44494Sfrankho #include <sys/sunddi.h>
450Sstevel@tonic-gate #include <vm/hat.h>
460Sstevel@tonic-gate #include <vm/as.h>
470Sstevel@tonic-gate #include <vm/pvn.h>
480Sstevel@tonic-gate #include <vm/seg.h>
490Sstevel@tonic-gate #include <vm/seg_map.h>
500Sstevel@tonic-gate #include <vm/seg_kmem.h>
510Sstevel@tonic-gate #include <vm/page.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include <sys/fs/hsfs_spec.h>
540Sstevel@tonic-gate #include <sys/fs/hsfs_isospec.h>
550Sstevel@tonic-gate #include <sys/fs/hsfs_node.h>
560Sstevel@tonic-gate #include <sys/fs/hsfs_impl.h>
570Sstevel@tonic-gate #include <sys/fs/hsfs_susp.h>
580Sstevel@tonic-gate #include <sys/fs/hsfs_rrip.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <sys/sysinfo.h>
610Sstevel@tonic-gate #include <sys/sysmacros.h>
620Sstevel@tonic-gate #include <sys/errno.h>
630Sstevel@tonic-gate #include <sys/debug.h>
640Sstevel@tonic-gate #include <fs/fs_subr.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * This macro expects a name that ends in '.' and returns TRUE if the
680Sstevel@tonic-gate  * name is not "." or ".."
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate #define	CAN_TRUNCATE_DOT(name, namelen)	\
710Sstevel@tonic-gate 		(namelen > 1 && (namelen > 2 || name[0] != '.'))
720Sstevel@tonic-gate 
730Sstevel@tonic-gate enum dirblock_result { FOUND_ENTRY, WENT_PAST, HIT_END };
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * These values determine whether we will try to read a file or dir;
770Sstevel@tonic-gate  * they may be patched via /etc/system to allow users to read
780Sstevel@tonic-gate  * record-oriented files.
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate int ide_prohibited = IDE_PROHIBITED;
810Sstevel@tonic-gate int hde_prohibited = HDE_PROHIBITED;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * This variable determines if the HSFS code will use the
850Sstevel@tonic-gate  * directory name lookup cache. The default is for the cache to be used.
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate static int hsfs_use_dnlc = 1;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * This variable determines whether strict ISO-9660 directory ordering
910Sstevel@tonic-gate  * is to be assumed.  If false (which it is by default), then when
920Sstevel@tonic-gate  * searching a directory of an ISO-9660 disk, we do not expect the
930Sstevel@tonic-gate  * entries to be sorted (as the spec requires), and so cannot terminate
940Sstevel@tonic-gate  * the search early.  Unfortunately, some vendors are producing
950Sstevel@tonic-gate  * non-compliant disks.  This variable exists to revert to the old
960Sstevel@tonic-gate  * behavior in case someone relies on this. This option is expected to be
970Sstevel@tonic-gate  * removed at some point in the future.
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  * Use "set hsfs:strict_iso9660_ordering = 1" in /etc/system to override.
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate static int strict_iso9660_ordering = 0;
1020Sstevel@tonic-gate 
1034866Sfrankho /*
1044866Sfrankho  * This tunable allows us to ignore inode numbers from rrip-1.12.
1054866Sfrankho  * In this case, we fall back to our default inode algorithm.
1064866Sfrankho  */
1074866Sfrankho int use_rrip_inodes = 1;
1084866Sfrankho 
1090Sstevel@tonic-gate static void hs_hsnode_cache_reclaim(void *unused);
1100Sstevel@tonic-gate static void hs_addfreeb(struct hsfs *fsp, struct hsnode *hp);
1110Sstevel@tonic-gate static enum dirblock_result process_dirblock(struct fbuf *fbp, uint_t *offset,
1120Sstevel@tonic-gate 	uint_t last_offset, char *nm, int nmlen, struct hsfs *fsp,
1130Sstevel@tonic-gate 	struct hsnode *dhp, struct vnode *dvp, struct vnode **vpp,
1142900Sfrankho 	int *error);
1150Sstevel@tonic-gate static int strip_trailing(struct hsfs *fsp, char *nm, int len);
1162900Sfrankho static int hs_namelen(struct hsfs *fsp, char *nm, int len);
1170Sstevel@tonic-gate static int uppercase_cp(char *from, char *to, int size);
1182900Sfrankho static void hs_log_bogus_joliet_warning(void);
1192900Sfrankho static int hs_iso_copy(char *from, char *to, int size);
1202900Sfrankho static int32_t hs_ucs2_2_utf8(uint16_t c_16, uint8_t *s_8);
1212900Sfrankho static int hs_utf8_trunc(uint8_t *str, int len);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * hs_access
1250Sstevel@tonic-gate  * Return 0 if the desired access may be granted.
1260Sstevel@tonic-gate  * Otherwise return error code.
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate int
hs_access(struct vnode * vp,mode_t m,struct cred * cred)1290Sstevel@tonic-gate hs_access(struct vnode *vp, mode_t m, struct cred *cred)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	struct hsnode *hp;
1320Sstevel@tonic-gate 	int	shift = 0;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	/*
1350Sstevel@tonic-gate 	 * Write access cannot be granted for a read-only medium
1360Sstevel@tonic-gate 	 */
1370Sstevel@tonic-gate 	if ((m & VWRITE) && !IS_DEVVP(vp))
1380Sstevel@tonic-gate 		return (EROFS);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	hp = VTOH(vp);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/*
1430Sstevel@tonic-gate 	 * XXX - For now, use volume protections.
1440Sstevel@tonic-gate 	 *  Also, always grant EXEC access for directories
1450Sstevel@tonic-gate 	 *  if READ access is granted.
1460Sstevel@tonic-gate 	 */
1470Sstevel@tonic-gate 	if ((vp->v_type == VDIR) && (m & VEXEC)) {
1480Sstevel@tonic-gate 		m &= ~VEXEC;
1490Sstevel@tonic-gate 		m |= VREAD;
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (crgetuid(cred) != hp->hs_dirent.uid) {
1530Sstevel@tonic-gate 		shift += 3;
1540Sstevel@tonic-gate 		if (!groupmember((uid_t)hp->hs_dirent.gid, cred))
1550Sstevel@tonic-gate 			shift += 3;
1560Sstevel@tonic-gate 	}
157*12273SCasper.Dik@Sun.COM 	return (secpolicy_vnode_access2(cred, vp, hp->hs_dirent.uid,
158*12273SCasper.Dik@Sun.COM 	    hp->hs_dirent.mode << shift, m));
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate #if ((HS_HASHSIZE & (HS_HASHSIZE - 1)) == 0)
1620Sstevel@tonic-gate #define	HS_HASH(l)	((uint_t)(l) & (HS_HASHSIZE - 1))
1630Sstevel@tonic-gate #else
1640Sstevel@tonic-gate #define	HS_HASH(l)	((uint_t)(l) % HS_HASHSIZE)
1650Sstevel@tonic-gate #endif
1660Sstevel@tonic-gate #define	HS_HPASH(hp)	HS_HASH((hp)->hs_nodeid)
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * The tunable nhsnode is now a threshold for a dynamically allocated
1700Sstevel@tonic-gate  * pool of hsnodes, not the size of a statically allocated table.
1710Sstevel@tonic-gate  * When the number of hsnodes for a particular file system exceeds
1720Sstevel@tonic-gate  * nhsnode, the allocate and free logic will try to reduce the number
1730Sstevel@tonic-gate  * of allocated nodes by returning unreferenced nodes to the kmem_cache
1740Sstevel@tonic-gate  * instead of putting them on the file system's private free list.
1750Sstevel@tonic-gate  */
1760Sstevel@tonic-gate int nhsnode = HS_HSNODESPACE / sizeof (struct hsnode);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate struct kmem_cache *hsnode_cache;  /* free hsnode cache */
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * Initialize the cache of free hsnodes.
1820Sstevel@tonic-gate  */
1830Sstevel@tonic-gate void
hs_init_hsnode_cache(void)1840Sstevel@tonic-gate hs_init_hsnode_cache(void)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	/*
1870Sstevel@tonic-gate 	 * A kmem_cache is used for the hsnodes
1880Sstevel@tonic-gate 	 * No constructor because hsnodes are initialised by bzeroing.
1890Sstevel@tonic-gate 	 */
1900Sstevel@tonic-gate 	hsnode_cache = kmem_cache_create("hsfs_hsnode_cache",
1910Sstevel@tonic-gate 	    sizeof (struct hsnode), 0, NULL,
1920Sstevel@tonic-gate 	    NULL, hs_hsnode_cache_reclaim, NULL, NULL, 0);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /*
1962900Sfrankho  * Destroy the cache of free hsnodes.
1972900Sfrankho  */
1982900Sfrankho void
hs_fini_hsnode_cache(void)1992900Sfrankho hs_fini_hsnode_cache(void)
2002900Sfrankho {
2012900Sfrankho 	kmem_cache_destroy(hsnode_cache);
2022900Sfrankho }
2032900Sfrankho 
2042900Sfrankho /*
2050Sstevel@tonic-gate  * System is short on memory, free up as much as possible
2060Sstevel@tonic-gate  */
2070Sstevel@tonic-gate /*ARGSUSED*/
2080Sstevel@tonic-gate static void
hs_hsnode_cache_reclaim(void * unused)2090Sstevel@tonic-gate hs_hsnode_cache_reclaim(void *unused)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	struct hsfs *fsp;
2120Sstevel@tonic-gate 	struct hsnode *hp;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	/*
2150Sstevel@tonic-gate 	 * For each vfs in the hs_mounttab list
2160Sstevel@tonic-gate 	 */
2170Sstevel@tonic-gate 	mutex_enter(&hs_mounttab_lock);
2180Sstevel@tonic-gate 	for (fsp = hs_mounttab; fsp != NULL; fsp = fsp->hsfs_next) {
2190Sstevel@tonic-gate 		/*
2200Sstevel@tonic-gate 		 * Purge the dnlc of all hsfs entries
2210Sstevel@tonic-gate 		 */
2220Sstevel@tonic-gate 		(void) dnlc_purge_vfsp(fsp->hsfs_vfs, 0);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 		/*
2250Sstevel@tonic-gate 		 * For each entry in the free chain
2260Sstevel@tonic-gate 		 */
2270Sstevel@tonic-gate 		rw_enter(&fsp->hsfs_hash_lock, RW_WRITER);
2280Sstevel@tonic-gate 		mutex_enter(&fsp->hsfs_free_lock);
2290Sstevel@tonic-gate 		for (hp = fsp->hsfs_free_f; hp != NULL; hp = fsp->hsfs_free_f) {
2300Sstevel@tonic-gate 			/*
2310Sstevel@tonic-gate 			 * Remove from chain
2320Sstevel@tonic-gate 			 */
2330Sstevel@tonic-gate 			fsp->hsfs_free_f = hp->hs_freef;
2340Sstevel@tonic-gate 			if (fsp->hsfs_free_f != NULL) {
2350Sstevel@tonic-gate 				fsp->hsfs_free_f->hs_freeb = NULL;
2360Sstevel@tonic-gate 			} else {
2370Sstevel@tonic-gate 				fsp->hsfs_free_b = NULL;
2380Sstevel@tonic-gate 			}
2390Sstevel@tonic-gate 			/*
2400Sstevel@tonic-gate 			 * Free the node. Force it to be fully freed
2410Sstevel@tonic-gate 			 * by setting the 3rd arg (nopage) to 1.
2420Sstevel@tonic-gate 			 */
2430Sstevel@tonic-gate 			hs_freenode(HTOV(hp), fsp, 1);
2440Sstevel@tonic-gate 		}
2450Sstevel@tonic-gate 		mutex_exit(&fsp->hsfs_free_lock);
2460Sstevel@tonic-gate 		rw_exit(&fsp->hsfs_hash_lock);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 	mutex_exit(&hs_mounttab_lock);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  * Add an hsnode to the end of the free list.
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate static void
hs_addfreeb(struct hsfs * fsp,struct hsnode * hp)2550Sstevel@tonic-gate hs_addfreeb(struct hsfs *fsp, struct hsnode *hp)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	struct hsnode *ep;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	vn_invalid(HTOV(hp));
2600Sstevel@tonic-gate 	mutex_enter(&fsp->hsfs_free_lock);
2610Sstevel@tonic-gate 	ep = fsp->hsfs_free_b;
2620Sstevel@tonic-gate 	fsp->hsfs_free_b = hp;		/* hp is the last entry in free list */
2630Sstevel@tonic-gate 	hp->hs_freef = NULL;
2640Sstevel@tonic-gate 	hp->hs_freeb = ep;		/* point at previous last entry */
2650Sstevel@tonic-gate 	if (ep == NULL)
2660Sstevel@tonic-gate 		fsp->hsfs_free_f = hp;	/* hp is only entry in free list */
2670Sstevel@tonic-gate 	else
2680Sstevel@tonic-gate 		ep->hs_freef = hp;	/* point previous last entry at hp */
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	mutex_exit(&fsp->hsfs_free_lock);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate  * Get an hsnode from the front of the free list.
2750Sstevel@tonic-gate  * Must be called with write hsfs_hash_lock held.
2760Sstevel@tonic-gate  */
2770Sstevel@tonic-gate static struct hsnode *
hs_getfree(struct hsfs * fsp)2780Sstevel@tonic-gate hs_getfree(struct hsfs *fsp)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	struct hsnode *hp, **tp;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&fsp->hsfs_hash_lock));
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	/*
2850Sstevel@tonic-gate 	 * If the number of currently-allocated hsnodes is less than
2860Sstevel@tonic-gate 	 * the hsnode count threshold (nhsnode), or if there are no
2870Sstevel@tonic-gate 	 * nodes on the file system's local free list (which acts as a
2880Sstevel@tonic-gate 	 * cache), call kmem_cache_alloc to get a new hsnode from
2890Sstevel@tonic-gate 	 * kernel memory.
2900Sstevel@tonic-gate 	 */
2910Sstevel@tonic-gate 	mutex_enter(&fsp->hsfs_free_lock);
2920Sstevel@tonic-gate 	if ((fsp->hsfs_nohsnode < nhsnode) || (fsp->hsfs_free_f == NULL)) {
2930Sstevel@tonic-gate 		mutex_exit(&fsp->hsfs_free_lock);
2940Sstevel@tonic-gate 		hp = kmem_cache_alloc(hsnode_cache, KM_SLEEP);
2950Sstevel@tonic-gate 		fsp->hsfs_nohsnode++;
2960Sstevel@tonic-gate 		bzero((caddr_t)hp, sizeof (*hp));
2970Sstevel@tonic-gate 		hp->hs_vnode = vn_alloc(KM_SLEEP);
2980Sstevel@tonic-gate 		return (hp);
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 	hp = fsp->hsfs_free_f;
3010Sstevel@tonic-gate 	/* hp cannot be NULL, since we already checked this above */
3020Sstevel@tonic-gate 	fsp->hsfs_free_f = hp->hs_freef;
3030Sstevel@tonic-gate 	if (fsp->hsfs_free_f != NULL)
3040Sstevel@tonic-gate 		fsp->hsfs_free_f->hs_freeb = NULL;
3050Sstevel@tonic-gate 	else
3060Sstevel@tonic-gate 		fsp->hsfs_free_b = NULL;
3070Sstevel@tonic-gate 	mutex_exit(&fsp->hsfs_free_lock);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	for (tp = &fsp->hsfs_hash[HS_HPASH(hp)]; *tp != NULL;
3104866Sfrankho 	    tp = &(*tp)->hs_hash) {
3110Sstevel@tonic-gate 		if (*tp == hp) {
3120Sstevel@tonic-gate 			struct vnode *vp;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 			vp = HTOV(hp);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 			/*
3170Sstevel@tonic-gate 			 * file is no longer referenced, destroy all old pages
3180Sstevel@tonic-gate 			 */
3190Sstevel@tonic-gate 			if (vn_has_cached_data(vp))
3200Sstevel@tonic-gate 				/*
3210Sstevel@tonic-gate 				 * pvn_vplist_dirty will abort all old pages
3220Sstevel@tonic-gate 				 */
3230Sstevel@tonic-gate 				(void) pvn_vplist_dirty(vp, (u_offset_t)0,
3244866Sfrankho 				    hsfs_putapage, B_INVAL,
3254866Sfrankho 				    (struct cred *)NULL);
3260Sstevel@tonic-gate 			*tp = hp->hs_hash;
3270Sstevel@tonic-gate 			break;
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 	if (hp->hs_dirent.sym_link != (char *)NULL) {
3310Sstevel@tonic-gate 		kmem_free(hp->hs_dirent.sym_link,
3324866Sfrankho 		    (size_t)(hp->hs_dirent.ext_size + 1));
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	mutex_destroy(&hp->hs_contents_lock);
3360Sstevel@tonic-gate 	{
3370Sstevel@tonic-gate 		vnode_t	*vp;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 		vp = hp->hs_vnode;
3400Sstevel@tonic-gate 		bzero((caddr_t)hp, sizeof (*hp));
3410Sstevel@tonic-gate 		hp->hs_vnode = vp;
3420Sstevel@tonic-gate 		vn_reinit(vp);
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 	return (hp);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate /*
3480Sstevel@tonic-gate  * Remove an hsnode from the free list.
3490Sstevel@tonic-gate  */
3500Sstevel@tonic-gate static void
hs_remfree(struct hsfs * fsp,struct hsnode * hp)3510Sstevel@tonic-gate hs_remfree(struct hsfs *fsp, struct hsnode *hp)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	mutex_enter(&fsp->hsfs_free_lock);
3540Sstevel@tonic-gate 	if (hp->hs_freef != NULL)
3550Sstevel@tonic-gate 		hp->hs_freef->hs_freeb = hp->hs_freeb;
3560Sstevel@tonic-gate 	else
3570Sstevel@tonic-gate 		fsp->hsfs_free_b = hp->hs_freeb;
3580Sstevel@tonic-gate 	if (hp->hs_freeb != NULL)
3590Sstevel@tonic-gate 		hp->hs_freeb->hs_freef = hp->hs_freef;
3600Sstevel@tonic-gate 	else
3610Sstevel@tonic-gate 		fsp->hsfs_free_f = hp->hs_freef;
3620Sstevel@tonic-gate 	mutex_exit(&fsp->hsfs_free_lock);
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate  * Look for hsnode in hash list.
3674866Sfrankho  * If the inode number is != HS_DUMMY_INO (16), then only the inode
3684866Sfrankho  * number is used for the check.
3694866Sfrankho  * If the inode number is == HS_DUMMY_INO, we additionally always
3704866Sfrankho  * check the directory offset for the file to avoid caching the
3714866Sfrankho  * meta data for all zero sized to the first zero sized file that
3724866Sfrankho  * was touched.
3734866Sfrankho  *
3740Sstevel@tonic-gate  * If found, reactivate it if inactive.
3754866Sfrankho  *
3760Sstevel@tonic-gate  * Must be entered with hsfs_hash_lock held.
3770Sstevel@tonic-gate  */
3780Sstevel@tonic-gate struct vnode *
hs_findhash(ino64_t nodeid,uint_t lbn,uint_t off,struct vfs * vfsp)3794866Sfrankho hs_findhash(ino64_t nodeid, uint_t lbn, uint_t off, struct vfs *vfsp)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	struct hsnode *tp;
3820Sstevel@tonic-gate 	struct hsfs *fsp;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vfsp);
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&fsp->hsfs_hash_lock));
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	for (tp = fsp->hsfs_hash[HS_HASH(nodeid)]; tp != NULL;
3890Sstevel@tonic-gate 	    tp = tp->hs_hash) {
3900Sstevel@tonic-gate 		if (tp->hs_nodeid == nodeid) {
3910Sstevel@tonic-gate 			struct vnode *vp;
3920Sstevel@tonic-gate 
3934866Sfrankho 			if (nodeid == HS_DUMMY_INO) {
3944866Sfrankho 				/*
3954866Sfrankho 				 * If this is the dummy inode number, look for
3964866Sfrankho 				 * matching dir_lbn and dir_off.
3974866Sfrankho 				 */
3984866Sfrankho 				for (; tp != NULL; tp = tp->hs_hash) {
3994866Sfrankho 					if (tp->hs_nodeid == nodeid &&
4004866Sfrankho 					    tp->hs_dir_lbn == lbn &&
4014866Sfrankho 					    tp->hs_dir_off == off)
4024866Sfrankho 						break;
4034866Sfrankho 				}
4044866Sfrankho 				if (tp == NULL)
4054866Sfrankho 					return (NULL);
4064866Sfrankho 			}
4074866Sfrankho 
4080Sstevel@tonic-gate 			mutex_enter(&tp->hs_contents_lock);
4090Sstevel@tonic-gate 			vp = HTOV(tp);
4100Sstevel@tonic-gate 			VN_HOLD(vp);
4110Sstevel@tonic-gate 			if ((tp->hs_flags & HREF) == 0) {
4120Sstevel@tonic-gate 				tp->hs_flags |= HREF;
4130Sstevel@tonic-gate 				/*
4140Sstevel@tonic-gate 				 * reactivating a free hsnode:
4150Sstevel@tonic-gate 				 * remove from free list
4160Sstevel@tonic-gate 				 */
4170Sstevel@tonic-gate 				hs_remfree(fsp, tp);
4180Sstevel@tonic-gate 			}
4190Sstevel@tonic-gate 			mutex_exit(&tp->hs_contents_lock);
4200Sstevel@tonic-gate 			return (vp);
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 	return (NULL);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate static void
hs_addhash(struct hsfs * fsp,struct hsnode * hp)4270Sstevel@tonic-gate hs_addhash(struct hsfs *fsp, struct hsnode *hp)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	ulong_t hashno;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&fsp->hsfs_hash_lock));
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	hashno = HS_HPASH(hp);
4340Sstevel@tonic-gate 	hp->hs_hash = fsp->hsfs_hash[hashno];
4350Sstevel@tonic-gate 	fsp->hsfs_hash[hashno] = hp;
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate /*
4390Sstevel@tonic-gate  * Destroy all old pages and free the hsnodes
4400Sstevel@tonic-gate  * Return 1 if busy (a hsnode is still referenced).
4410Sstevel@tonic-gate  */
4420Sstevel@tonic-gate int
hs_synchash(struct vfs * vfsp)4430Sstevel@tonic-gate hs_synchash(struct vfs *vfsp)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate 	struct hsfs *fsp;
4460Sstevel@tonic-gate 	int i;
4470Sstevel@tonic-gate 	struct hsnode *hp, *nhp;
4480Sstevel@tonic-gate 	int busy = 0;
4490Sstevel@tonic-gate 	struct vnode *vp, *rvp;
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vfsp);
4520Sstevel@tonic-gate 	rvp = fsp->hsfs_rootvp;
4530Sstevel@tonic-gate 	/* make sure no one can come in */
4540Sstevel@tonic-gate 	rw_enter(&fsp->hsfs_hash_lock, RW_WRITER);
4550Sstevel@tonic-gate 	for (i = 0; i < HS_HASHSIZE; i++) {
4560Sstevel@tonic-gate 		for (hp = fsp->hsfs_hash[i]; hp != NULL; hp = hp->hs_hash) {
4570Sstevel@tonic-gate 			vp = HTOV(hp);
4580Sstevel@tonic-gate 			if ((hp->hs_flags & HREF) && (vp != rvp ||
4594866Sfrankho 			    (vp == rvp && vp->v_count > 1))) {
4600Sstevel@tonic-gate 				busy = 1;
4610Sstevel@tonic-gate 				continue;
4620Sstevel@tonic-gate 			}
4630Sstevel@tonic-gate 			if (vn_has_cached_data(vp))
4640Sstevel@tonic-gate 				(void) pvn_vplist_dirty(vp, (u_offset_t)0,
4654866Sfrankho 				    hsfs_putapage, B_INVAL,
4664866Sfrankho 				    (struct cred *)NULL);
4670Sstevel@tonic-gate 		}
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 	if (busy) {
4700Sstevel@tonic-gate 		rw_exit(&fsp->hsfs_hash_lock);
4710Sstevel@tonic-gate 		return (1);
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	/* now free the hsnodes */
4750Sstevel@tonic-gate 	for (i = 0; i < HS_HASHSIZE; i++) {
4760Sstevel@tonic-gate 		for (hp = fsp->hsfs_hash[i]; hp != NULL; hp = nhp) {
4770Sstevel@tonic-gate 			nhp = hp->hs_hash;
4780Sstevel@tonic-gate 			/*
4790Sstevel@tonic-gate 			 * We know there are no pages associated with
4800Sstevel@tonic-gate 			 * all the hsnodes (they've all been released
4810Sstevel@tonic-gate 			 * above). So remove from free list and
4820Sstevel@tonic-gate 			 * free the entry with nopage set.
4830Sstevel@tonic-gate 			 */
4840Sstevel@tonic-gate 			vp = HTOV(hp);
4850Sstevel@tonic-gate 			if (vp != rvp) {
4860Sstevel@tonic-gate 				hs_remfree(fsp, hp);
4870Sstevel@tonic-gate 				hs_freenode(vp, fsp, 1);
4880Sstevel@tonic-gate 			}
4890Sstevel@tonic-gate 		}
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	ASSERT(fsp->hsfs_nohsnode == 1);
4930Sstevel@tonic-gate 	rw_exit(&fsp->hsfs_hash_lock);
4940Sstevel@tonic-gate 	/* release the root hsnode, this should free the final hsnode */
4950Sstevel@tonic-gate 	VN_RELE(rvp);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	return (0);
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate  * hs_makenode
5020Sstevel@tonic-gate  *
5030Sstevel@tonic-gate  * Construct an hsnode.
5040Sstevel@tonic-gate  * Caller specifies the directory entry, the block number and offset
5050Sstevel@tonic-gate  * of the directory entry, and the vfs pointer.
5060Sstevel@tonic-gate  * note: off is the sector offset, not lbn offset
5070Sstevel@tonic-gate  * if NULL is returned implies file system hsnode table full
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate struct vnode *
hs_makenode(struct hs_direntry * dp,uint_t lbn,uint_t off,struct vfs * vfsp)5100Sstevel@tonic-gate hs_makenode(
5110Sstevel@tonic-gate 	struct hs_direntry *dp,
5120Sstevel@tonic-gate 	uint_t lbn,
5130Sstevel@tonic-gate 	uint_t off,
5140Sstevel@tonic-gate 	struct vfs *vfsp)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	struct hsnode *hp;
5170Sstevel@tonic-gate 	struct vnode *vp;
5180Sstevel@tonic-gate 	struct hs_volume *hvp;
5190Sstevel@tonic-gate 	struct vnode *newvp;
5200Sstevel@tonic-gate 	struct hsfs *fsp;
5210Sstevel@tonic-gate 	ino64_t nodeid;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vfsp);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	/*
5264866Sfrankho 	 * Construct the data that allows us to re-read the meta data without
5274866Sfrankho 	 * knowing the name of the file: in the case of a directory
5280Sstevel@tonic-gate 	 * entry, this should point to the canonical dirent, the "."
5290Sstevel@tonic-gate 	 * directory entry for the directory.  This dirent is pointed
5300Sstevel@tonic-gate 	 * to by all directory entries for that dir (including the ".")
5310Sstevel@tonic-gate 	 * entry itself.
5320Sstevel@tonic-gate 	 * In the case of a file, simply point to the dirent for that
5334866Sfrankho 	 * file (there are hard links in Rock Ridge, so we need to use
5344866Sfrankho 	 * different data to contruct the node id).
5350Sstevel@tonic-gate 	 */
5360Sstevel@tonic-gate 	if (dp->type == VDIR) {
5370Sstevel@tonic-gate 		lbn = dp->ext_lbn;
5380Sstevel@tonic-gate 		off = 0;
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	/*
5420Sstevel@tonic-gate 	 * Normalize lbn and off before creating a nodeid
5430Sstevel@tonic-gate 	 * and before storing them in a hs_node structure
5440Sstevel@tonic-gate 	 */
5450Sstevel@tonic-gate 	hvp = &fsp->hsfs_vol;
5460Sstevel@tonic-gate 	lbn += off >> hvp->lbn_shift;
5470Sstevel@tonic-gate 	off &= hvp->lbn_maxoffset;
5484866Sfrankho 	/*
5494866Sfrankho 	 * If the media carries rrip-v1.12 or newer, and we trust the inodes
5504866Sfrankho 	 * from the rrip data (use_rrip_inodes != 0), use that data. If the
5514866Sfrankho 	 * media has been created by a recent mkisofs version, we may trust
5524866Sfrankho 	 * all numbers in the starting extent number; otherwise, we cannot
5534866Sfrankho 	 * do this for zero sized files and symlinks, because if we did we'd
5544866Sfrankho 	 * end up mapping all of them to the same node.
5554866Sfrankho 	 * We use HS_DUMMY_INO in this case and make sure that we will not
5564866Sfrankho 	 * map all files to the same meta data.
5574866Sfrankho 	 */
5584866Sfrankho 	if (dp->inode != 0 && use_rrip_inodes) {
5594866Sfrankho 		nodeid = dp->inode;
5604866Sfrankho 	} else if ((dp->ext_size == 0 || dp->sym_link != (char *)NULL) &&
5614866Sfrankho 	    (fsp->hsfs_flags & HSFSMNT_INODE) == 0) {
5624866Sfrankho 		nodeid = HS_DUMMY_INO;
5634866Sfrankho 	} else {
5644866Sfrankho 		nodeid = dp->ext_lbn;
5654866Sfrankho 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	/* look for hsnode in cache first */
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	rw_enter(&fsp->hsfs_hash_lock, RW_READER);
5700Sstevel@tonic-gate 
5714866Sfrankho 	if ((vp = hs_findhash(nodeid, lbn, off, vfsp)) == NULL) {
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 		/*
5740Sstevel@tonic-gate 		 * Not in cache.  However, someone else may have come
5750Sstevel@tonic-gate 		 * to the same conclusion and just put one in.	Upgrade
5760Sstevel@tonic-gate 		 * our lock to a write lock and look again.
5770Sstevel@tonic-gate 		 */
5780Sstevel@tonic-gate 		rw_exit(&fsp->hsfs_hash_lock);
5790Sstevel@tonic-gate 		rw_enter(&fsp->hsfs_hash_lock, RW_WRITER);
5800Sstevel@tonic-gate 
5814866Sfrankho 		if ((vp = hs_findhash(nodeid, lbn, off, vfsp)) == NULL) {
5820Sstevel@tonic-gate 			/*
5830Sstevel@tonic-gate 			 * Now we are really sure that the hsnode is not
5840Sstevel@tonic-gate 			 * in the cache.  Get one off freelist or else
5850Sstevel@tonic-gate 			 * allocate one. Either way get a bzeroed hsnode.
5860Sstevel@tonic-gate 			 */
5870Sstevel@tonic-gate 			hp = hs_getfree(fsp);
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 			bcopy((caddr_t)dp, (caddr_t)&hp->hs_dirent,
5904866Sfrankho 			    sizeof (*dp));
5910Sstevel@tonic-gate 			/*
5920Sstevel@tonic-gate 			 * We've just copied this pointer into hs_dirent,
5930Sstevel@tonic-gate 			 * and don't want 2 references to same symlink.
5940Sstevel@tonic-gate 			 */
5950Sstevel@tonic-gate 			dp->sym_link = (char *)NULL;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 			/*
5980Sstevel@tonic-gate 			 * No need to hold any lock because hsnode is not
5990Sstevel@tonic-gate 			 * yet in the hash chain.
6000Sstevel@tonic-gate 			 */
6010Sstevel@tonic-gate 			mutex_init(&hp->hs_contents_lock, NULL, MUTEX_DEFAULT,
6020Sstevel@tonic-gate 			    NULL);
6030Sstevel@tonic-gate 			hp->hs_dir_lbn = lbn;
6040Sstevel@tonic-gate 			hp->hs_dir_off = off;
6050Sstevel@tonic-gate 			hp->hs_nodeid = nodeid;
6060Sstevel@tonic-gate 			hp->hs_seq = 0;
6075312Smg147109 			hp->hs_prev_offset = 0;
6085312Smg147109 			hp->hs_num_contig = 0;
6095312Smg147109 			hp->hs_ra_bytes = 0;
6100Sstevel@tonic-gate 			hp->hs_flags = HREF;
6110Sstevel@tonic-gate 			if (off > HS_SECTOR_SIZE)
6120Sstevel@tonic-gate 				cmn_err(CE_WARN, "hs_makenode: bad offset");
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 			vp = HTOV(hp);
6150Sstevel@tonic-gate 			vp->v_vfsp = vfsp;
6160Sstevel@tonic-gate 			vp->v_type = dp->type;
6170Sstevel@tonic-gate 			vp->v_rdev = dp->r_dev;
6180Sstevel@tonic-gate 			vn_setops(vp, hsfs_vnodeops);
6190Sstevel@tonic-gate 			vp->v_data = (caddr_t)hp;
6200Sstevel@tonic-gate 			vn_exists(vp);
6210Sstevel@tonic-gate 			/*
6220Sstevel@tonic-gate 			 * if it's a device, call specvp
6230Sstevel@tonic-gate 			 */
6240Sstevel@tonic-gate 			if (IS_DEVVP(vp)) {
6250Sstevel@tonic-gate 				rw_exit(&fsp->hsfs_hash_lock);
6260Sstevel@tonic-gate 				newvp = specvp(vp, vp->v_rdev, vp->v_type,
6274866Sfrankho 				    CRED());
6280Sstevel@tonic-gate 				if (newvp == NULL)
6294866Sfrankho 					cmn_err(CE_NOTE,
6304866Sfrankho 					    "hs_makenode: specvp failed");
6310Sstevel@tonic-gate 				VN_RELE(vp);
6320Sstevel@tonic-gate 				return (newvp);
6330Sstevel@tonic-gate 			}
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 			hs_addhash(fsp, hp);
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 		}
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	if (dp->sym_link != (char *)NULL) {
6410Sstevel@tonic-gate 		kmem_free(dp->sym_link, (size_t)(dp->ext_size + 1));
6420Sstevel@tonic-gate 		dp->sym_link = (char *)NULL;
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	rw_exit(&fsp->hsfs_hash_lock);
6460Sstevel@tonic-gate 	return (vp);
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate /*
6500Sstevel@tonic-gate  * hs_freenode
6510Sstevel@tonic-gate  *
6520Sstevel@tonic-gate  * Deactivate an hsnode.
6530Sstevel@tonic-gate  * Leave it on the hash list but put it on the free list.
6540Sstevel@tonic-gate  * If the vnode does not have any pages, release the hsnode to the
6550Sstevel@tonic-gate  * kmem_cache using kmem_cache_free, else put in back of the free list.
6560Sstevel@tonic-gate  *
6570Sstevel@tonic-gate  * This function can be called with the hsfs_free_lock held, but only
6580Sstevel@tonic-gate  * when the code is guaranteed to go through the path where the
6590Sstevel@tonic-gate  * node is freed entirely, and not the path where the node could go back
6600Sstevel@tonic-gate  * on the free list (and where the free lock would need to be acquired).
6610Sstevel@tonic-gate  */
6620Sstevel@tonic-gate void
hs_freenode(vnode_t * vp,struct hsfs * fsp,int nopage)6630Sstevel@tonic-gate hs_freenode(vnode_t *vp, struct hsfs *fsp, int nopage)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	struct hsnode **tp;
6660Sstevel@tonic-gate 	struct hsnode *hp = VTOH(vp);
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&fsp->hsfs_hash_lock));
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	if (nopage || (fsp->hsfs_nohsnode >= nhsnode)) {
6710Sstevel@tonic-gate 		/* remove this node from the hash list, if it's there */
6720Sstevel@tonic-gate 		for (tp = &fsp->hsfs_hash[HS_HPASH(hp)]; *tp != NULL;
6734866Sfrankho 		    tp = &(*tp)->hs_hash) {
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 			if (*tp == hp) {
6760Sstevel@tonic-gate 				*tp = hp->hs_hash;
6770Sstevel@tonic-gate 				break;
6780Sstevel@tonic-gate 			}
6790Sstevel@tonic-gate 		}
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 		if (hp->hs_dirent.sym_link != (char *)NULL) {
6820Sstevel@tonic-gate 			kmem_free(hp->hs_dirent.sym_link,
6834866Sfrankho 			    (size_t)(hp->hs_dirent.ext_size + 1));
6840Sstevel@tonic-gate 			hp->hs_dirent.sym_link = NULL;
6850Sstevel@tonic-gate 		}
6860Sstevel@tonic-gate 		if (vn_has_cached_data(vp)) {
6870Sstevel@tonic-gate 			/* clean all old pages */
6880Sstevel@tonic-gate 			(void) pvn_vplist_dirty(vp, (u_offset_t)0,
6890Sstevel@tonic-gate 			    hsfs_putapage, B_INVAL, (struct cred *)NULL);
6900Sstevel@tonic-gate 			/* XXX - can we remove pages by fiat like this??? */
6910Sstevel@tonic-gate 			vp->v_pages = NULL;
6920Sstevel@tonic-gate 		}
6930Sstevel@tonic-gate 		mutex_destroy(&hp->hs_contents_lock);
6940Sstevel@tonic-gate 		vn_invalid(vp);
6950Sstevel@tonic-gate 		vn_free(vp);
6960Sstevel@tonic-gate 		kmem_cache_free(hsnode_cache, hp);
6970Sstevel@tonic-gate 		fsp->hsfs_nohsnode--;
6980Sstevel@tonic-gate 		return;
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 	hs_addfreeb(fsp, hp); /* add to back of free list */
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate /*
7040Sstevel@tonic-gate  * hs_remakenode
7050Sstevel@tonic-gate  *
7060Sstevel@tonic-gate  * Reconstruct a vnode given the location of its directory entry.
7070Sstevel@tonic-gate  * Caller specifies the the block number and offset
7080Sstevel@tonic-gate  * of the directory entry, and the vfs pointer.
7090Sstevel@tonic-gate  * Returns an error code or 0.
7100Sstevel@tonic-gate  */
7110Sstevel@tonic-gate int
hs_remakenode(uint_t lbn,uint_t off,struct vfs * vfsp,struct vnode ** vpp)7120Sstevel@tonic-gate hs_remakenode(uint_t lbn, uint_t off, struct vfs *vfsp,
7130Sstevel@tonic-gate     struct vnode **vpp)
7140Sstevel@tonic-gate {
7150Sstevel@tonic-gate 	struct buf *secbp;
7160Sstevel@tonic-gate 	struct hsfs *fsp;
7170Sstevel@tonic-gate 	uint_t secno;
7180Sstevel@tonic-gate 	uchar_t *dirp;
7190Sstevel@tonic-gate 	struct hs_direntry hd;
7200Sstevel@tonic-gate 	int error;
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	/* Convert to sector and offset */
7230Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vfsp);
7240Sstevel@tonic-gate 	if (off > HS_SECTOR_SIZE) {
7250Sstevel@tonic-gate 		cmn_err(CE_WARN, "hs_remakenode: bad offset");
7260Sstevel@tonic-gate 		error = EINVAL;
7270Sstevel@tonic-gate 		goto end;
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 	secno = LBN_TO_SEC(lbn, vfsp);
7300Sstevel@tonic-gate 	secbp = bread(fsp->hsfs_devvp->v_rdev, secno * 4, HS_SECTOR_SIZE);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	error = geterror(secbp);
7330Sstevel@tonic-gate 	if (error != 0) {
7340Sstevel@tonic-gate 		cmn_err(CE_NOTE, "hs_remakenode: bread: error=(%d)", error);
7350Sstevel@tonic-gate 		goto end;
7360Sstevel@tonic-gate 	}
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	dirp = (uchar_t *)secbp->b_un.b_addr;
7392900Sfrankho 	error = hs_parsedir(fsp, &dirp[off], &hd, (char *)NULL, (int *)NULL,
7404866Sfrankho 	    HS_SECTOR_SIZE - off);
7410Sstevel@tonic-gate 	if (!error) {
7420Sstevel@tonic-gate 		*vpp = hs_makenode(&hd, lbn, off, vfsp);
7430Sstevel@tonic-gate 		if (*vpp == NULL)
7440Sstevel@tonic-gate 			error = ENFILE;
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate end:
7480Sstevel@tonic-gate 	brelse(secbp);
7490Sstevel@tonic-gate 	return (error);
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate /*
7540Sstevel@tonic-gate  * hs_dirlook
7550Sstevel@tonic-gate  *
7560Sstevel@tonic-gate  * Look for a given name in a given directory.
7570Sstevel@tonic-gate  * If found, construct an hsnode for it.
7580Sstevel@tonic-gate  */
7590Sstevel@tonic-gate int
hs_dirlook(struct vnode * dvp,char * name,int namlen,struct vnode ** vpp,struct cred * cred)7600Sstevel@tonic-gate hs_dirlook(
7610Sstevel@tonic-gate 	struct vnode	*dvp,
7620Sstevel@tonic-gate 	char		*name,
7630Sstevel@tonic-gate 	int		namlen,		/* length of 'name' */
7640Sstevel@tonic-gate 	struct vnode	**vpp,
7650Sstevel@tonic-gate 	struct cred	*cred)
7660Sstevel@tonic-gate {
7670Sstevel@tonic-gate 	struct hsnode *dhp;
7680Sstevel@tonic-gate 	struct hsfs	*fsp;
7690Sstevel@tonic-gate 	int		error = 0;
7700Sstevel@tonic-gate 	uint_t		offset;		/* real offset in directory */
7714866Sfrankho 	uint_t		last_offset;	/* last index in directory */
7720Sstevel@tonic-gate 	char		*cmpname;	/* case-folded name */
7730Sstevel@tonic-gate 	int		cmpname_size;	/* how much memory we allocate for it */
7740Sstevel@tonic-gate 	int		cmpnamelen;
7750Sstevel@tonic-gate 	int		adhoc_search;	/* did we start at begin of dir? */
7760Sstevel@tonic-gate 	int		end;
7770Sstevel@tonic-gate 	uint_t		hsoffset;
7780Sstevel@tonic-gate 	struct fbuf	*fbp;
7790Sstevel@tonic-gate 	int		bytes_wanted;
7800Sstevel@tonic-gate 	int		dirsiz;
7810Sstevel@tonic-gate 	int		is_rrip;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	if (dvp->v_type != VDIR)
7840Sstevel@tonic-gate 		return (ENOTDIR);
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	if (error = hs_access(dvp, (mode_t)VEXEC, cred))
7870Sstevel@tonic-gate 		return (error);
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	if (hsfs_use_dnlc && (*vpp = dnlc_lookup(dvp, name)))
7900Sstevel@tonic-gate 		return (0);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	dhp = VTOH(dvp);
7930Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(dvp->v_vfsp);
7942900Sfrankho 	is_rrip = IS_RRIP_IMPLEMENTED(fsp);
7952900Sfrankho 
7962900Sfrankho 	/*
7972900Sfrankho 	 * name == "^A" is illegal for ISO-9660 and Joliet as '..' is '\1' on
7982900Sfrankho 	 * disk. It is no problem for Rock Ridge as RR uses '.' and '..'.
7992900Sfrankho 	 * XXX It could be OK for Joliet also (because namelen == 1 is
8002900Sfrankho 	 * XXX impossible for UCS-2) but then we need a better compare algorith.
8012900Sfrankho 	 */
8022900Sfrankho 	if (!is_rrip && *name == '\1' && namlen == 1)
8032900Sfrankho 		return (EINVAL);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	cmpname_size = (int)(fsp->hsfs_namemax + 1);
8060Sstevel@tonic-gate 	cmpname = kmem_alloc((size_t)cmpname_size, KM_SLEEP);
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	if (namlen >= cmpname_size)
8090Sstevel@tonic-gate 		namlen = cmpname_size - 1;
8100Sstevel@tonic-gate 	/*
8110Sstevel@tonic-gate 	 * For the purposes of comparing the name against dir entries,
8120Sstevel@tonic-gate 	 * fold it to upper case.
8130Sstevel@tonic-gate 	 */
8140Sstevel@tonic-gate 	if (is_rrip) {
815494Sfrankho 		(void) strlcpy(cmpname, name, cmpname_size);
8160Sstevel@tonic-gate 		cmpnamelen = namlen;
8170Sstevel@tonic-gate 	} else {
8180Sstevel@tonic-gate 		/*
8190Sstevel@tonic-gate 		 * If we don't consider a trailing dot as part of the filename,
8200Sstevel@tonic-gate 		 * remove it from the specified name
8210Sstevel@tonic-gate 		 */
8220Sstevel@tonic-gate 		if ((fsp->hsfs_flags & HSFSMNT_NOTRAILDOT) &&
8234866Sfrankho 		    name[namlen-1] == '.' &&
8244866Sfrankho 		    CAN_TRUNCATE_DOT(name, namlen))
8250Sstevel@tonic-gate 			name[--namlen] = '\0';
8262900Sfrankho 		if (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2 ||
8272900Sfrankho 		    fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
8282900Sfrankho 			cmpnamelen = hs_iso_copy(name, cmpname, namlen);
8292900Sfrankho 		} else {
8302900Sfrankho 			cmpnamelen = hs_uppercase_copy(name, cmpname, namlen);
8312900Sfrankho 		}
8320Sstevel@tonic-gate 	}
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	/* make sure dirent is filled up with all info */
8350Sstevel@tonic-gate 	if (dhp->hs_dirent.ext_size == 0)
8360Sstevel@tonic-gate 		hs_filldirent(dvp, &dhp->hs_dirent);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	/*
8390Sstevel@tonic-gate 	 * No lock is needed - hs_offset is used as starting
8400Sstevel@tonic-gate 	 * point for searching the directory.
8410Sstevel@tonic-gate 	 */
8420Sstevel@tonic-gate 	offset = dhp->hs_offset;
8430Sstevel@tonic-gate 	hsoffset = offset;
8440Sstevel@tonic-gate 	adhoc_search = (offset != 0);
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	end = dhp->hs_dirent.ext_size;
8470Sstevel@tonic-gate 	dirsiz = end;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate tryagain:
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	while (offset < end) {
852494Sfrankho 		bytes_wanted = MIN(MAXBSIZE, dirsiz - (offset & MAXBMASK));
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 		error = fbread(dvp, (offset_t)(offset & MAXBMASK),
8554866Sfrankho 		    (unsigned int)bytes_wanted, S_READ, &fbp);
8560Sstevel@tonic-gate 		if (error)
8570Sstevel@tonic-gate 			goto done;
8580Sstevel@tonic-gate 
859494Sfrankho 		last_offset = (offset & MAXBMASK) + fbp->fb_count;
8600Sstevel@tonic-gate 
861494Sfrankho 		switch (process_dirblock(fbp, &offset, last_offset,
8622900Sfrankho 		    cmpname, cmpnamelen, fsp, dhp, dvp, vpp, &error)) {
8630Sstevel@tonic-gate 		case FOUND_ENTRY:
8640Sstevel@tonic-gate 			/* found an entry, either correct or not */
8650Sstevel@tonic-gate 			goto done;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 		case WENT_PAST:
8680Sstevel@tonic-gate 			/*
8690Sstevel@tonic-gate 			 * If we get here we know we didn't find it on the
8700Sstevel@tonic-gate 			 * first pass. If adhoc_search, then we started a
8710Sstevel@tonic-gate 			 * bit into the dir, and need to wrap around and
8720Sstevel@tonic-gate 			 * search the first entries.  If not, then we started
8730Sstevel@tonic-gate 			 * at the beginning and didn't find it.
8740Sstevel@tonic-gate 			 */
8750Sstevel@tonic-gate 			if (adhoc_search) {
8760Sstevel@tonic-gate 				offset = 0;
8770Sstevel@tonic-gate 				end = hsoffset;
8780Sstevel@tonic-gate 				adhoc_search = 0;
8790Sstevel@tonic-gate 				goto tryagain;
8800Sstevel@tonic-gate 			}
8810Sstevel@tonic-gate 			error = ENOENT;
8820Sstevel@tonic-gate 			goto done;
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 		case HIT_END:
8850Sstevel@tonic-gate 			goto tryagain;
8860Sstevel@tonic-gate 		}
8870Sstevel@tonic-gate 	}
8880Sstevel@tonic-gate 	/*
8890Sstevel@tonic-gate 	 * End of all dir blocks, didn't find entry.
8900Sstevel@tonic-gate 	 */
8910Sstevel@tonic-gate 	if (adhoc_search) {
8920Sstevel@tonic-gate 		offset = 0;
8930Sstevel@tonic-gate 		end = hsoffset;
8940Sstevel@tonic-gate 		adhoc_search = 0;
8950Sstevel@tonic-gate 		goto tryagain;
8960Sstevel@tonic-gate 	}
8970Sstevel@tonic-gate 	error = ENOENT;
8980Sstevel@tonic-gate done:
8990Sstevel@tonic-gate 	/*
9000Sstevel@tonic-gate 	 * If we found the entry, add it to the DNLC
9010Sstevel@tonic-gate 	 * If the entry is a device file (assuming we support Rock Ridge),
9020Sstevel@tonic-gate 	 * we enter the device vnode to the cache since that is what
9030Sstevel@tonic-gate 	 * is in *vpp.
9040Sstevel@tonic-gate 	 * That is ok since the CD-ROM is read-only, so (dvp,name) will
9050Sstevel@tonic-gate 	 * always point to the same device.
9060Sstevel@tonic-gate 	 */
9070Sstevel@tonic-gate 	if (hsfs_use_dnlc && !error)
9080Sstevel@tonic-gate 		dnlc_enter(dvp, name, *vpp);
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	kmem_free(cmpname, (size_t)cmpname_size);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	return (error);
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate /*
9160Sstevel@tonic-gate  * hs_parsedir
9170Sstevel@tonic-gate  *
9180Sstevel@tonic-gate  * Parse a Directory Record into an hs_direntry structure.
9190Sstevel@tonic-gate  * High Sierra and ISO directory are almost the same
9200Sstevel@tonic-gate  * except the flag and date
9210Sstevel@tonic-gate  */
9220Sstevel@tonic-gate int
hs_parsedir(struct hsfs * fsp,uchar_t * dirp,struct hs_direntry * hdp,char * dnp,int * dnlen,int last_offset)9230Sstevel@tonic-gate hs_parsedir(
9240Sstevel@tonic-gate 	struct hsfs		*fsp,
9250Sstevel@tonic-gate 	uchar_t			*dirp,
9260Sstevel@tonic-gate 	struct hs_direntry	*hdp,
9270Sstevel@tonic-gate 	char			*dnp,
9282900Sfrankho 	int			*dnlen,
9294866Sfrankho 	int			last_offset)	/* last offset in dirp */
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate 	char	*on_disk_name;
9320Sstevel@tonic-gate 	int	on_disk_namelen;
9332900Sfrankho 	int	on_disk_dirlen;
9340Sstevel@tonic-gate 	uchar_t	flags;
9350Sstevel@tonic-gate 	int	namelen;
9360Sstevel@tonic-gate 	int	error;
9370Sstevel@tonic-gate 	int	name_change_flag = 0;	/* set if name was gotten in SUA */
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	hdp->ext_lbn = HDE_EXT_LBN(dirp);
9400Sstevel@tonic-gate 	hdp->ext_size = HDE_EXT_SIZE(dirp);
9410Sstevel@tonic-gate 	hdp->xar_len = HDE_XAR_LEN(dirp);
9420Sstevel@tonic-gate 	hdp->intlf_sz = HDE_INTRLV_SIZE(dirp);
9430Sstevel@tonic-gate 	hdp->intlf_sk = HDE_INTRLV_SKIP(dirp);
9440Sstevel@tonic-gate 	hdp->sym_link = (char *)NULL;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	if (fsp->hsfs_vol_type == HS_VOL_TYPE_HS) {
9470Sstevel@tonic-gate 		flags = HDE_FLAGS(dirp);
9480Sstevel@tonic-gate 		hs_parse_dirdate(HDE_cdate(dirp), &hdp->cdate);
9490Sstevel@tonic-gate 		hs_parse_dirdate(HDE_cdate(dirp), &hdp->adate);
9500Sstevel@tonic-gate 		hs_parse_dirdate(HDE_cdate(dirp), &hdp->mdate);
9510Sstevel@tonic-gate 		if ((flags & hde_prohibited) == 0) {
9520Sstevel@tonic-gate 			/*
9530Sstevel@tonic-gate 			 * Skip files with the associated bit set.
9540Sstevel@tonic-gate 			 */
9550Sstevel@tonic-gate 			if (flags & HDE_ASSOCIATED)
9560Sstevel@tonic-gate 				return (EAGAIN);
9570Sstevel@tonic-gate 			hdp->type = VREG;
9580Sstevel@tonic-gate 			hdp->mode = HFREG;
9590Sstevel@tonic-gate 			hdp->nlink = 1;
9600Sstevel@tonic-gate 		} else if ((flags & hde_prohibited) == HDE_DIRECTORY) {
9610Sstevel@tonic-gate 			hdp->type = VDIR;
9620Sstevel@tonic-gate 			hdp->mode = HFDIR;
9630Sstevel@tonic-gate 			hdp->nlink = 2;
9640Sstevel@tonic-gate 		} else {
9650Sstevel@tonic-gate 			hs_log_bogus_disk_warning(fsp,
966494Sfrankho 			    HSFS_ERR_UNSUP_TYPE, flags);
9670Sstevel@tonic-gate 			return (EINVAL);
9680Sstevel@tonic-gate 		}
9690Sstevel@tonic-gate 		hdp->uid = fsp -> hsfs_vol.vol_uid;
9700Sstevel@tonic-gate 		hdp->gid = fsp -> hsfs_vol.vol_gid;
9710Sstevel@tonic-gate 		hdp->mode = hdp-> mode | (fsp -> hsfs_vol.vol_prot & 0777);
9722900Sfrankho 	} else if ((fsp->hsfs_vol_type == HS_VOL_TYPE_ISO) ||
9734866Sfrankho 	    (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2) ||
9744866Sfrankho 	    (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET)) {
9752900Sfrankho 
9760Sstevel@tonic-gate 		flags = IDE_FLAGS(dirp);
9770Sstevel@tonic-gate 		hs_parse_dirdate(IDE_cdate(dirp), &hdp->cdate);
9780Sstevel@tonic-gate 		hs_parse_dirdate(IDE_cdate(dirp), &hdp->adate);
9790Sstevel@tonic-gate 		hs_parse_dirdate(IDE_cdate(dirp), &hdp->mdate);
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 		if ((flags & ide_prohibited) == 0) {
9820Sstevel@tonic-gate 			/*
9830Sstevel@tonic-gate 			 * Skip files with the associated bit set.
9840Sstevel@tonic-gate 			 */
9850Sstevel@tonic-gate 			if (flags & IDE_ASSOCIATED)
9860Sstevel@tonic-gate 				return (EAGAIN);
9870Sstevel@tonic-gate 			hdp->type = VREG;
9880Sstevel@tonic-gate 			hdp->mode = HFREG;
9890Sstevel@tonic-gate 			hdp->nlink = 1;
9900Sstevel@tonic-gate 		} else if ((flags & ide_prohibited) == IDE_DIRECTORY) {
9910Sstevel@tonic-gate 			hdp->type = VDIR;
9920Sstevel@tonic-gate 			hdp->mode = HFDIR;
9930Sstevel@tonic-gate 			hdp->nlink = 2;
9940Sstevel@tonic-gate 		} else {
9950Sstevel@tonic-gate 			hs_log_bogus_disk_warning(fsp,
996494Sfrankho 			    HSFS_ERR_UNSUP_TYPE, flags);
9970Sstevel@tonic-gate 			return (EINVAL);
9980Sstevel@tonic-gate 		}
9990Sstevel@tonic-gate 		hdp->uid = fsp -> hsfs_vol.vol_uid;
10000Sstevel@tonic-gate 		hdp->gid = fsp -> hsfs_vol.vol_gid;
10010Sstevel@tonic-gate 		hdp->mode = hdp-> mode | (fsp -> hsfs_vol.vol_prot & 0777);
10024866Sfrankho 		hdp->inode = 0;		/* initialize with 0, then check rrip */
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 		/*
10050Sstevel@tonic-gate 		 * Having this all filled in, let's see if we have any
10060Sstevel@tonic-gate 		 * SUA susp to look at.
10070Sstevel@tonic-gate 		 */
10080Sstevel@tonic-gate 		if (IS_SUSP_IMPLEMENTED(fsp)) {
10090Sstevel@tonic-gate 			error = parse_sua((uchar_t *)dnp, dnlen,
10104866Sfrankho 			    &name_change_flag, dirp, last_offset,
10114866Sfrankho 			    hdp, fsp,
10124866Sfrankho 			    (uchar_t *)NULL, NULL);
10130Sstevel@tonic-gate 			if (error) {
10140Sstevel@tonic-gate 				if (hdp->sym_link) {
10150Sstevel@tonic-gate 					kmem_free(hdp->sym_link,
10164866Sfrankho 					    (size_t)(hdp->ext_size + 1));
10170Sstevel@tonic-gate 					hdp->sym_link = (char *)NULL;
10180Sstevel@tonic-gate 				}
10190Sstevel@tonic-gate 				return (error);
10200Sstevel@tonic-gate 			}
10210Sstevel@tonic-gate 		}
10220Sstevel@tonic-gate 	}
10230Sstevel@tonic-gate 	hdp->xar_prot = (HDE_PROTECTION & flags) != 0;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate #if dontskip
10260Sstevel@tonic-gate 	if (hdp->xar_len > 0) {
10270Sstevel@tonic-gate 		cmn_err(CE_NOTE, "hsfs: extended attributes not supported");
10280Sstevel@tonic-gate 		return (EINVAL);
10290Sstevel@tonic-gate 	}
10300Sstevel@tonic-gate #endif
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	/* check interleaf size and skip factor */
10330Sstevel@tonic-gate 	/* must both be zero or non-zero */
10340Sstevel@tonic-gate 	if (hdp->intlf_sz + hdp->intlf_sk) {
10350Sstevel@tonic-gate 		if ((hdp->intlf_sz == 0) || (hdp->intlf_sk == 0)) {
10360Sstevel@tonic-gate 			cmn_err(CE_NOTE,
10374866Sfrankho 			    "hsfs: interleaf size or skip factor error");
10380Sstevel@tonic-gate 			return (EINVAL);
10390Sstevel@tonic-gate 		}
10400Sstevel@tonic-gate 		if (hdp->ext_size == 0) {
10410Sstevel@tonic-gate 			cmn_err(CE_NOTE,
10420Sstevel@tonic-gate 			    "hsfs: interleaving specified on zero length file");
10430Sstevel@tonic-gate 			return (EINVAL);
10440Sstevel@tonic-gate 		}
10450Sstevel@tonic-gate 	}
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	if (HDE_VOL_SET(dirp) != 1) {
10480Sstevel@tonic-gate 		if (fsp->hsfs_vol.vol_set_size != 1 &&
10490Sstevel@tonic-gate 		    fsp->hsfs_vol.vol_set_size != HDE_VOL_SET(dirp)) {
10500Sstevel@tonic-gate 			cmn_err(CE_NOTE, "hsfs: multivolume file?");
10510Sstevel@tonic-gate 			return (EINVAL);
10520Sstevel@tonic-gate 		}
10530Sstevel@tonic-gate 	}
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	/*
10560Sstevel@tonic-gate 	 * If the name changed, then the NM field for RRIP was hit and
10570Sstevel@tonic-gate 	 * we should not copy the name again, just return.
10580Sstevel@tonic-gate 	 */
10590Sstevel@tonic-gate 	if (NAME_HAS_CHANGED(name_change_flag))
10600Sstevel@tonic-gate 		return (0);
10610Sstevel@tonic-gate 
1062494Sfrankho 	/*
1063494Sfrankho 	 * Fall back to the ISO name. Note that as in process_dirblock,
1064494Sfrankho 	 * the on-disk filename length must be validated against ISO
1065494Sfrankho 	 * limits - which, in case of RR present but no RR name found,
1066494Sfrankho 	 * are NOT identical to fsp->hsfs_namemax on this filesystem.
1067494Sfrankho 	 */
10680Sstevel@tonic-gate 	on_disk_name = (char *)HDE_name(dirp);
10690Sstevel@tonic-gate 	on_disk_namelen = (int)HDE_NAME_LEN(dirp);
10702900Sfrankho 	on_disk_dirlen = (int)HDE_DIR_LEN(dirp);
10710Sstevel@tonic-gate 
10722900Sfrankho 	if (on_disk_dirlen < HDE_ROOT_DIR_REC_SIZE ||
10732900Sfrankho 	    ((on_disk_dirlen > last_offset) ||
10742900Sfrankho 	    ((HDE_FDESIZE + on_disk_namelen) > on_disk_dirlen))) {
10754866Sfrankho 		hs_log_bogus_disk_warning(fsp,
10764866Sfrankho 		    HSFS_ERR_BAD_DIR_ENTRY, 0);
10772900Sfrankho 		return (EINVAL);
10782900Sfrankho 	}
10792900Sfrankho 
10802900Sfrankho 	if (on_disk_namelen > fsp->hsfs_namelen &&
10812900Sfrankho 	    hs_namelen(fsp, on_disk_name, on_disk_namelen) >
10824866Sfrankho 	    fsp->hsfs_namelen) {
10832900Sfrankho 		hs_log_bogus_disk_warning(fsp,
10844866Sfrankho 		    fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET ?
10854866Sfrankho 		    HSFS_ERR_BAD_JOLIET_FILE_LEN :
10864866Sfrankho 		    HSFS_ERR_BAD_FILE_LEN, 0);
10870Sstevel@tonic-gate 	}
10882900Sfrankho 	if (on_disk_namelen > ISO_NAMELEN_V2_MAX)
10892900Sfrankho 		on_disk_namelen = fsp->hsfs_namemax;	/* Paranoia */
10902900Sfrankho 
10910Sstevel@tonic-gate 	if (dnp != NULL) {
10922900Sfrankho 		if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
10932900Sfrankho 			namelen = hs_jnamecopy(on_disk_name, dnp,
10944866Sfrankho 			    on_disk_namelen, fsp->hsfs_namemax,
10954866Sfrankho 			    fsp->hsfs_flags);
10962900Sfrankho 			/*
10972900Sfrankho 			 * A negative return value means that the file name
10982900Sfrankho 			 * has been truncated to fsp->hsfs_namemax.
10992900Sfrankho 			 */
11002900Sfrankho 			if (namelen < 0) {
11012900Sfrankho 				namelen = -namelen;
11022900Sfrankho 				hs_log_bogus_disk_warning(fsp,
11034866Sfrankho 				    HSFS_ERR_TRUNC_JOLIET_FILE_LEN, 0);
11042900Sfrankho 			}
11052900Sfrankho 		} else {
11062900Sfrankho 			/*
11072900Sfrankho 			 * HS_VOL_TYPE_ISO && HS_VOL_TYPE_ISO_V2
11082900Sfrankho 			 */
11092900Sfrankho 			namelen = hs_namecopy(on_disk_name, dnp,
11104866Sfrankho 			    on_disk_namelen, fsp->hsfs_flags);
11112900Sfrankho 		}
11122900Sfrankho 		if (namelen == 0)
11132900Sfrankho 			return (EINVAL);
11140Sstevel@tonic-gate 		if ((fsp->hsfs_flags & HSFSMNT_NOTRAILDOT) &&
11150Sstevel@tonic-gate 		    dnp[ namelen-1 ] == '.' && CAN_TRUNCATE_DOT(dnp, namelen))
11160Sstevel@tonic-gate 			dnp[ --namelen ] = '\0';
11170Sstevel@tonic-gate 	} else
11180Sstevel@tonic-gate 		namelen = on_disk_namelen;
11190Sstevel@tonic-gate 	if (dnlen != NULL)
11200Sstevel@tonic-gate 		*dnlen = namelen;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	return (0);
11230Sstevel@tonic-gate }
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate /*
11260Sstevel@tonic-gate  * hs_namecopy
11270Sstevel@tonic-gate  *
11280Sstevel@tonic-gate  * Parse a file/directory name into UNIX form.
11290Sstevel@tonic-gate  * Delete trailing blanks, upper-to-lower case, add NULL terminator.
11300Sstevel@tonic-gate  * Returns the (possibly new) length.
11312900Sfrankho  *
11322900Sfrankho  * Called from hsfs_readdir() via hs_parsedir()
11330Sstevel@tonic-gate  */
11340Sstevel@tonic-gate int
hs_namecopy(char * from,char * to,int size,ulong_t flags)11350Sstevel@tonic-gate hs_namecopy(char *from, char *to, int size, ulong_t flags)
11360Sstevel@tonic-gate {
11370Sstevel@tonic-gate 	uint_t i;
11380Sstevel@tonic-gate 	uchar_t c;
11390Sstevel@tonic-gate 	int lastspace;
11400Sstevel@tonic-gate 	int maplc;
11412900Sfrankho 	int trailspace;
11422900Sfrankho 	int version;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	/* special handling for '.' and '..' */
11450Sstevel@tonic-gate 	if (size == 1) {
11460Sstevel@tonic-gate 		if (*from == '\0') {
11470Sstevel@tonic-gate 			*to++ = '.';
11480Sstevel@tonic-gate 			*to = '\0';
11490Sstevel@tonic-gate 			return (1);
11500Sstevel@tonic-gate 		} else if (*from == '\1') {
11510Sstevel@tonic-gate 			*to++ = '.';
11520Sstevel@tonic-gate 			*to++ = '.';
11530Sstevel@tonic-gate 			*to = '\0';
11540Sstevel@tonic-gate 			return (2);
11550Sstevel@tonic-gate 		}
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	maplc = (flags & HSFSMNT_NOMAPLCASE) == 0;
11592900Sfrankho 	trailspace = (flags & HSFSMNT_NOTRAILSPACE) == 0;
11602900Sfrankho 	version = (flags & HSFSMNT_NOVERSION) == 0;
11610Sstevel@tonic-gate 	for (i = 0, lastspace = -1; i < size; i++) {
11620Sstevel@tonic-gate 		c = from[i];
11632900Sfrankho 		if (c == ';' && version)
11640Sstevel@tonic-gate 			break;
11652900Sfrankho 		if (c <= ' ' && !trailspace) {
11660Sstevel@tonic-gate 			if (lastspace == -1)
11670Sstevel@tonic-gate 				lastspace = i;
11680Sstevel@tonic-gate 		} else
11690Sstevel@tonic-gate 			lastspace = -1;
11700Sstevel@tonic-gate 		if (maplc && (c >= 'A') && (c <= 'Z'))
11710Sstevel@tonic-gate 			c += 'a' - 'A';
11720Sstevel@tonic-gate 		to[i] = c;
11730Sstevel@tonic-gate 	}
11740Sstevel@tonic-gate 	if (lastspace != -1)
11750Sstevel@tonic-gate 		i = lastspace;
11760Sstevel@tonic-gate 	to[i] = '\0';
11770Sstevel@tonic-gate 	return (i);
11780Sstevel@tonic-gate }
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate /*
11812900Sfrankho  * hs_jnamecopy
11822900Sfrankho  *
11832900Sfrankho  * This is the Joliet variant of hs_namecopy()
11842900Sfrankho  *
11852900Sfrankho  * Parse a UCS-2 Joliet file/directory name into UNIX form.
11862900Sfrankho  * Add NULL terminator.
11872900Sfrankho  * Returns the new length.
11882900Sfrankho  *
11892900Sfrankho  * Called from hsfs_readdir() via hs_parsedir()
11902900Sfrankho  */
11912900Sfrankho int
hs_jnamecopy(char * from,char * to,int size,int maxsize,ulong_t flags)11922900Sfrankho hs_jnamecopy(char *from, char *to, int size, int maxsize, ulong_t flags)
11932900Sfrankho {
11942900Sfrankho 	uint_t i;
11952900Sfrankho 	uint_t len;
11962900Sfrankho 	uint16_t c;
11972900Sfrankho 	int	amt;
11982900Sfrankho 	int	version;
11992900Sfrankho 
12002900Sfrankho 	/* special handling for '.' and '..' */
12012900Sfrankho 	if (size == 1) {
12022900Sfrankho 		if (*from == '\0') {
12032900Sfrankho 			*to++ = '.';
12042900Sfrankho 			*to = '\0';
12052900Sfrankho 			return (1);
12062900Sfrankho 		} else if (*from == '\1') {
12072900Sfrankho 			*to++ = '.';
12082900Sfrankho 			*to++ = '.';
12092900Sfrankho 			*to = '\0';
12102900Sfrankho 			return (2);
12112900Sfrankho 		}
12122900Sfrankho 	}
12132900Sfrankho 
12142900Sfrankho 	version = (flags & HSFSMNT_NOVERSION) == 0;
12152900Sfrankho 	for (i = 0, len = 0; i < size; i++) {
12162900Sfrankho 		c = (from[i++] & 0xFF) << 8;
12172900Sfrankho 		c |= from[i] & 0xFF;
12182900Sfrankho 		if (c == ';' && version)
12192900Sfrankho 			break;
12202900Sfrankho 
12212900Sfrankho 		if (len > (maxsize-3)) {
12222900Sfrankho 			if (c < 0x80)
12232900Sfrankho 				amt = 1;
12242900Sfrankho 			else if (c < 0x800)
12252900Sfrankho 				amt = 2;
12262900Sfrankho 			else
12272900Sfrankho 				amt = 3;
12282900Sfrankho 			if ((len+amt) > maxsize) {
12292900Sfrankho 				to[len] = '\0';
12302900Sfrankho 				return (-len);
12312900Sfrankho 			}
12322900Sfrankho 		}
12332900Sfrankho 		amt = hs_ucs2_2_utf8(c, (uint8_t *)&to[len]);
12342900Sfrankho 		if (amt == 0) {
12352900Sfrankho 			hs_log_bogus_joliet_warning(); /* should never happen */
12362900Sfrankho 			return (0);
12372900Sfrankho 		}
12382900Sfrankho 		len += amt;
12392900Sfrankho 	}
12402900Sfrankho 	to[len] = '\0';
12412900Sfrankho 	return (len);
12422900Sfrankho }
12432900Sfrankho 
12442900Sfrankho /*
12450Sstevel@tonic-gate  * map a filename to upper case;
12460Sstevel@tonic-gate  * return 1 if found lowercase character
12472900Sfrankho  *
12482900Sfrankho  * Called from process_dirblock()
12492900Sfrankho  * via hsfs_lookup() -> hs_dirlook() -> process_dirblock()
12502900Sfrankho  * to create an intermedia name from on disk file names for
12512900Sfrankho  * comparing names.
12520Sstevel@tonic-gate  */
12530Sstevel@tonic-gate static int
uppercase_cp(char * from,char * to,int size)12540Sstevel@tonic-gate uppercase_cp(char *from, char *to, int size)
12550Sstevel@tonic-gate {
12560Sstevel@tonic-gate 	uint_t i;
12570Sstevel@tonic-gate 	uchar_t c;
12580Sstevel@tonic-gate 	uchar_t had_lc = 0;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
12610Sstevel@tonic-gate 		c = *from++;
12620Sstevel@tonic-gate 		if ((c >= 'a') && (c <= 'z')) {
12630Sstevel@tonic-gate 			c -= ('a' - 'A');
12640Sstevel@tonic-gate 			had_lc = 1;
12650Sstevel@tonic-gate 		}
12660Sstevel@tonic-gate 		*to++ = c;
12670Sstevel@tonic-gate 	}
12680Sstevel@tonic-gate 	return (had_lc);
12690Sstevel@tonic-gate }
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate /*
12722900Sfrankho  * This is the Joliet variant of uppercase_cp()
12732900Sfrankho  *
12742900Sfrankho  * map a UCS-2 filename to UTF-8;
12752900Sfrankho  * return new length
12762900Sfrankho  *
12772900Sfrankho  * Called from process_dirblock()
12782900Sfrankho  * via hsfs_lookup() -> hs_dirlook() -> process_dirblock()
12792900Sfrankho  * to create an intermedia name from on disk file names for
12802900Sfrankho  * comparing names.
12812900Sfrankho  */
12822900Sfrankho int
hs_joliet_cp(char * from,char * to,int size)12832900Sfrankho hs_joliet_cp(char *from, char *to, int size)
12842900Sfrankho {
12852900Sfrankho 	uint_t		i;
12862900Sfrankho 	uint16_t	c;
12872900Sfrankho 	int		len = 0;
12882900Sfrankho 	int		amt;
12892900Sfrankho 
12902900Sfrankho 	/* special handling for '\0' and '\1' */
12912900Sfrankho 	if (size == 1) {
12922900Sfrankho 		*to = *from;
12932900Sfrankho 		return (1);
12942900Sfrankho 	}
12952900Sfrankho 	for (i = 0; i < size; i += 2) {
12962900Sfrankho 		c = (*from++ & 0xFF) << 8;
12972900Sfrankho 		c |= *from++ & 0xFF;
12982900Sfrankho 
12992900Sfrankho 		amt = hs_ucs2_2_utf8(c, (uint8_t *)to);
13002900Sfrankho 		if (amt == 0) {
13012900Sfrankho 			hs_log_bogus_joliet_warning(); /* should never happen */
13022900Sfrankho 			return (0);
13032900Sfrankho 		}
13042900Sfrankho 
13052900Sfrankho 		to  += amt;
13062900Sfrankho 		len += amt;
13072900Sfrankho 	}
13082900Sfrankho 	return (len);
13092900Sfrankho }
13102900Sfrankho 
13112900Sfrankho static void
hs_log_bogus_joliet_warning(void)13122900Sfrankho hs_log_bogus_joliet_warning(void)
13132900Sfrankho {
13142900Sfrankho 	static int	warned = 0;
13152900Sfrankho 
13162900Sfrankho 	if (warned)
13172900Sfrankho 		return;
13182900Sfrankho 	warned = 1;
13192900Sfrankho 	cmn_err(CE_CONT, "hsfs: Warning: "
13204866Sfrankho 	    "file name contains bad UCS-2 chacarter\n");
13212900Sfrankho }
13222900Sfrankho 
13232900Sfrankho 
13242900Sfrankho /*
13250Sstevel@tonic-gate  * hs_uppercase_copy
13260Sstevel@tonic-gate  *
13272900Sfrankho  * Convert a UNIX-style name into its HSFS equivalent
13282900Sfrankho  * replacing '.' and '..' with '\0' and '\1'.
13290Sstevel@tonic-gate  * Map to upper case.
13300Sstevel@tonic-gate  * Returns the (possibly new) length.
13312900Sfrankho  *
13322900Sfrankho  * Called from hs_dirlook() and rrip_namecopy()
13332900Sfrankho  * to create an intermediate name from the callers name from hsfs_lookup()
13342900Sfrankho  * XXX Is the call from rrip_namecopy() OK?
13350Sstevel@tonic-gate  */
13360Sstevel@tonic-gate int
hs_uppercase_copy(char * from,char * to,int size)13370Sstevel@tonic-gate hs_uppercase_copy(char *from, char *to, int size)
13380Sstevel@tonic-gate {
13390Sstevel@tonic-gate 	uint_t i;
13400Sstevel@tonic-gate 	uchar_t c;
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	/* special handling for '.' and '..' */
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	if (size == 1 && *from == '.') {
13450Sstevel@tonic-gate 		*to = '\0';
13460Sstevel@tonic-gate 		return (1);
13470Sstevel@tonic-gate 	} else if (size == 2 && *from == '.' && *(from+1) == '.') {
13480Sstevel@tonic-gate 		*to = '\1';
13490Sstevel@tonic-gate 		return (1);
13500Sstevel@tonic-gate 	}
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
13530Sstevel@tonic-gate 		c = *from++;
13540Sstevel@tonic-gate 		if ((c >= 'a') && (c <= 'z'))
13550Sstevel@tonic-gate 			c = c - 'a' + 'A';
13560Sstevel@tonic-gate 		*to++ = c;
13570Sstevel@tonic-gate 	}
13580Sstevel@tonic-gate 	return (size);
13590Sstevel@tonic-gate }
13600Sstevel@tonic-gate 
13612900Sfrankho /*
13622900Sfrankho  * hs_iso_copy
13632900Sfrankho  *
13642900Sfrankho  * This is the Joliet/ISO-9660:1999 variant of hs_uppercase_copy()
13652900Sfrankho  *
13662900Sfrankho  * Convert a UTF-8 UNIX-style name into its UTF-8 Joliet/ISO equivalent
13672900Sfrankho  * replacing '.' and '..' with '\0' and '\1'.
13682900Sfrankho  * Returns the (possibly new) length.
13692900Sfrankho  *
13702900Sfrankho  * Called from hs_dirlook()
13712900Sfrankho  * to create an intermediate name from the callers name from hsfs_lookup()
13722900Sfrankho  */
13732900Sfrankho static int
hs_iso_copy(char * from,char * to,int size)13742900Sfrankho hs_iso_copy(char *from, char *to, int size)
13752900Sfrankho {
13762900Sfrankho 	uint_t i;
13772900Sfrankho 	uchar_t c;
13782900Sfrankho 
13792900Sfrankho 	/* special handling for '.' and '..' */
13802900Sfrankho 
13812900Sfrankho 	if (size == 1 && *from == '.') {
13822900Sfrankho 		*to = '\0';
13832900Sfrankho 		return (1);
13842900Sfrankho 	} else if (size == 2 && *from == '.' && *(from+1) == '.') {
13852900Sfrankho 		*to = '\1';
13862900Sfrankho 		return (1);
13872900Sfrankho 	}
13882900Sfrankho 
13892900Sfrankho 	for (i = 0; i < size; i++) {
13902900Sfrankho 		c = *from++;
13912900Sfrankho 		*to++ = c;
13922900Sfrankho 	}
13932900Sfrankho 	return (size);
13942900Sfrankho }
13952900Sfrankho 
13960Sstevel@tonic-gate void
hs_filldirent(struct vnode * vp,struct hs_direntry * hdp)13970Sstevel@tonic-gate hs_filldirent(struct vnode *vp, struct hs_direntry *hdp)
13980Sstevel@tonic-gate {
13990Sstevel@tonic-gate 	struct buf *secbp;
14000Sstevel@tonic-gate 	uint_t	secno;
14010Sstevel@tonic-gate 	offset_t secoff;
14020Sstevel@tonic-gate 	struct hsfs *fsp;
14030Sstevel@tonic-gate 	uchar_t *secp;
14040Sstevel@tonic-gate 	int	error;
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	if (vp->v_type != VDIR) {
14070Sstevel@tonic-gate 		cmn_err(CE_WARN, "hsfs_filldirent: vp (0x%p) not a directory",
14084866Sfrankho 		    (void *)vp);
14090Sstevel@tonic-gate 		return;
14100Sstevel@tonic-gate 	}
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vp ->v_vfsp);
14130Sstevel@tonic-gate 	secno = LBN_TO_SEC(hdp->ext_lbn+hdp->xar_len, vp->v_vfsp);
14140Sstevel@tonic-gate 	secoff = LBN_TO_BYTE(hdp->ext_lbn+hdp->xar_len, vp->v_vfsp) &
14154866Sfrankho 	    MAXHSOFFSET;
14160Sstevel@tonic-gate 	secbp = bread(fsp->hsfs_devvp->v_rdev, secno * 4, HS_SECTOR_SIZE);
14170Sstevel@tonic-gate 	error = geterror(secbp);
14180Sstevel@tonic-gate 	if (error != 0) {
14190Sstevel@tonic-gate 		cmn_err(CE_NOTE, "hs_filldirent: bread: error=(%d)", error);
14200Sstevel@tonic-gate 		goto end;
14210Sstevel@tonic-gate 	}
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	secp = (uchar_t *)secbp->b_un.b_addr;
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	/* quick check */
14260Sstevel@tonic-gate 	if (hdp->ext_lbn != HDE_EXT_LBN(&secp[secoff])) {
14270Sstevel@tonic-gate 		cmn_err(CE_NOTE, "hsfs_filldirent: dirent not match");
14280Sstevel@tonic-gate 		/* keep on going */
14290Sstevel@tonic-gate 	}
14302900Sfrankho 	(void) hs_parsedir(fsp, &secp[secoff], hdp, (char *)NULL,
14314866Sfrankho 	    (int *)NULL, HS_SECTOR_SIZE - secoff);
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate end:
14340Sstevel@tonic-gate 	brelse(secbp);
14350Sstevel@tonic-gate }
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate /*
14380Sstevel@tonic-gate  * Look through a directory block for a matching entry.
14390Sstevel@tonic-gate  * Note: this routine does an fbrelse() on the buffer passed in.
14400Sstevel@tonic-gate  */
14410Sstevel@tonic-gate static enum dirblock_result
process_dirblock(struct fbuf * fbp,uint_t * offset,uint_t last_offset,char * nm,int nmlen,struct hsfs * fsp,struct hsnode * dhp,struct vnode * dvp,struct vnode ** vpp,int * error)14420Sstevel@tonic-gate process_dirblock(
14430Sstevel@tonic-gate 	struct fbuf	*fbp,		/* buffer containing dirblk */
14440Sstevel@tonic-gate 	uint_t		*offset,	/* lower index */
14450Sstevel@tonic-gate 	uint_t		last_offset,	/* upper index */
14460Sstevel@tonic-gate 	char		*nm,		/* upcase nm to compare against */
14470Sstevel@tonic-gate 	int		nmlen,		/* length of name */
14480Sstevel@tonic-gate 	struct hsfs	*fsp,
14490Sstevel@tonic-gate 	struct hsnode	*dhp,
14500Sstevel@tonic-gate 	struct vnode	*dvp,
14510Sstevel@tonic-gate 	struct vnode	**vpp,
14522900Sfrankho 	int		*error)		/* return value: errno */
14530Sstevel@tonic-gate {
14540Sstevel@tonic-gate 	uchar_t		*blkp = (uchar_t *)fbp->fb_addr; /* dir block */
14550Sstevel@tonic-gate 	char		*dname;		/* name in directory entry */
14560Sstevel@tonic-gate 	int		dnamelen;	/* length of name */
14570Sstevel@tonic-gate 	struct hs_direntry hd;
14580Sstevel@tonic-gate 	int		hdlen;
14594866Sfrankho 	uchar_t		*dirp;		/* the directory entry */
14600Sstevel@tonic-gate 	int		res;
14610Sstevel@tonic-gate 	int		parsedir_res;
14622900Sfrankho 	int		is_rrip;
14630Sstevel@tonic-gate 	size_t		rrip_name_size;
1464494Sfrankho 	int		rr_namelen = 0;
14650Sstevel@tonic-gate 	char		*rrip_name_str = NULL;
14660Sstevel@tonic-gate 	char		*rrip_tmp_name = NULL;
14670Sstevel@tonic-gate 	enum dirblock_result err = 0;
14680Sstevel@tonic-gate 	int 		did_fbrelse = 0;
14692900Sfrankho 	char		uppercase_name[JOLIET_NAMELEN_MAX*3 + 1]; /* 331 */
14700Sstevel@tonic-gate 
1471494Sfrankho #define	PD_return(retval)	\
1472494Sfrankho 	{ err = retval; goto do_ret; }		/* return after cleanup */
1473494Sfrankho #define	rel_offset(offset)	\
1474494Sfrankho 	((offset) & MAXBOFFSET)			/* index into cur blk */
14752900Sfrankho #define	RESTORE_NM(tmp, orig)	\
14762900Sfrankho 	if (is_rrip && *(tmp) != '\0') \
14772900Sfrankho 		(void) strcpy((orig), (tmp))
14780Sstevel@tonic-gate 
14792900Sfrankho 	is_rrip = IS_RRIP_IMPLEMENTED(fsp);
14800Sstevel@tonic-gate 	if (is_rrip) {
14810Sstevel@tonic-gate 		rrip_name_size = RRIP_FILE_NAMELEN + 1;
14820Sstevel@tonic-gate 		rrip_name_str = kmem_alloc(rrip_name_size, KM_SLEEP);
14830Sstevel@tonic-gate 		rrip_tmp_name = kmem_alloc(rrip_name_size, KM_SLEEP);
14840Sstevel@tonic-gate 		rrip_name_str[0] = '\0';
14850Sstevel@tonic-gate 		rrip_tmp_name[0] = '\0';
14860Sstevel@tonic-gate 	}
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 	while (*offset < last_offset) {
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 		/*
14910Sstevel@tonic-gate 		 * Directory Entries cannot span sectors.
1492494Sfrankho 		 *
1493494Sfrankho 		 * Unused bytes at the end of each sector are zeroed
1494494Sfrankho 		 * according to ISO9660, but we cannot rely on this
1495494Sfrankho 		 * since both media failures and maliciously corrupted
1496494Sfrankho 		 * media may return arbitrary values.
1497494Sfrankho 		 * We therefore have to check for consistency:
1498494Sfrankho 		 * The size of a directory entry must be at least
1499494Sfrankho 		 * 34 bytes (the size of the directory entry metadata),
1500494Sfrankho 		 * or zero (indicating the end-of-sector condition).
1501494Sfrankho 		 * For a non-zero directory entry size of less than
1502494Sfrankho 		 * 34 Bytes, log a warning.
1503494Sfrankho 		 * In any case, skip the rest of this sector and
1504494Sfrankho 		 * continue with the next.
15050Sstevel@tonic-gate 		 */
15060Sstevel@tonic-gate 		hdlen = (int)((uchar_t)
1507494Sfrankho 		    HDE_DIR_LEN(&blkp[rel_offset(*offset)]));
15080Sstevel@tonic-gate 
1509494Sfrankho 		if (hdlen < HDE_ROOT_DIR_REC_SIZE ||
1510494Sfrankho 		    *offset + hdlen > last_offset) {
1511494Sfrankho 			/*
1512494Sfrankho 			 * Advance to the next sector boundary
1513494Sfrankho 			 */
1514494Sfrankho 			*offset = roundup(*offset + 1, HS_SECTOR_SIZE);
1515494Sfrankho 			if (hdlen)
1516494Sfrankho 				hs_log_bogus_disk_warning(fsp,
1517494Sfrankho 				    HSFS_ERR_TRAILING_JUNK, 0);
1518494Sfrankho 			continue;
15190Sstevel@tonic-gate 		}
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 		bzero(&hd, sizeof (hd));
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 		/*
1524494Sfrankho 		 * Check the filename length in the ISO record for
1525494Sfrankho 		 * plausibility and reset it to a safe value, in case
1526494Sfrankho 		 * the name length byte is out of range. Since the ISO
1527494Sfrankho 		 * name will be used as fallback if the rockridge name
1528494Sfrankho 		 * is invalid/nonexistant, we must make sure not to
1529494Sfrankho 		 * blow the bounds and initialize dnamelen to a sensible
1530494Sfrankho 		 * value within the limits of ISO9660.
1531494Sfrankho 		 * In addition to that, the ISO filename is part of the
1532494Sfrankho 		 * directory entry. If the filename length is too large
1533494Sfrankho 		 * to fit, the record is invalid and we'll advance to
1534494Sfrankho 		 * the next.
15350Sstevel@tonic-gate 		 */
15360Sstevel@tonic-gate 		dirp = &blkp[rel_offset(*offset)];
15370Sstevel@tonic-gate 		dname = (char *)HDE_name(dirp);
15380Sstevel@tonic-gate 		dnamelen = (int)((uchar_t)HDE_NAME_LEN(dirp));
15392900Sfrankho 		/*
15402900Sfrankho 		 * If the directory entry extends beyond the end of the
15412900Sfrankho 		 * block, it must be invalid. Skip it.
15422900Sfrankho 		 */
1543494Sfrankho 		if (dnamelen > hdlen - HDE_FDESIZE) {
15440Sstevel@tonic-gate 			hs_log_bogus_disk_warning(fsp,
15452900Sfrankho 			    HSFS_ERR_BAD_DIR_ENTRY, 0);
1546494Sfrankho 			goto skip_rec;
15472900Sfrankho 		} else if (dnamelen > fsp->hsfs_namelen &&
15484866Sfrankho 		    hs_namelen(fsp, dname, dnamelen) > fsp->hsfs_namelen) {
1549494Sfrankho 			hs_log_bogus_disk_warning(fsp,
15504866Sfrankho 			    fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET ?
15514866Sfrankho 			    HSFS_ERR_BAD_JOLIET_FILE_LEN :
15524866Sfrankho 			    HSFS_ERR_BAD_FILE_LEN, 0);
15530Sstevel@tonic-gate 		}
15542900Sfrankho 		if (dnamelen > ISO_NAMELEN_V2_MAX)
15552900Sfrankho 			dnamelen = fsp->hsfs_namemax;	/* Paranoia */
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 		/*
15580Sstevel@tonic-gate 		 * If the rock ridge is implemented, then we copy the name
15590Sstevel@tonic-gate 		 * from the SUA area to rrip_name_str. If no Alternate
15600Sstevel@tonic-gate 		 * name is found, then use the uppercase NM in the
15610Sstevel@tonic-gate 		 * rrip_name_str char array.
15620Sstevel@tonic-gate 		 */
15630Sstevel@tonic-gate 		if (is_rrip) {
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 			rrip_name_str[0] = '\0';
15660Sstevel@tonic-gate 			rr_namelen = rrip_namecopy(nm, &rrip_name_str[0],
15674866Sfrankho 			    &rrip_tmp_name[0], dirp, last_offset - *offset,
15684866Sfrankho 			    fsp, &hd);
15690Sstevel@tonic-gate 			if (hd.sym_link) {
15700Sstevel@tonic-gate 				kmem_free(hd.sym_link,
15710Sstevel@tonic-gate 				    (size_t)(hd.ext_size+1));
15720Sstevel@tonic-gate 				hd.sym_link = (char *)NULL;
15730Sstevel@tonic-gate 			}
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 			if (rr_namelen != -1) {
15760Sstevel@tonic-gate 				dname = (char *)&rrip_name_str[0];
15770Sstevel@tonic-gate 				dnamelen = rr_namelen;
15780Sstevel@tonic-gate 			}
15790Sstevel@tonic-gate 		}
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 		if (!is_rrip || rr_namelen == -1) {
15820Sstevel@tonic-gate 			/* use iso name instead */
15830Sstevel@tonic-gate 
15842900Sfrankho 			int i = -1;
15850Sstevel@tonic-gate 			/*
15860Sstevel@tonic-gate 			 * make sure that we get rid of ';' in the dname of
15870Sstevel@tonic-gate 			 * an iso direntry, as we should have no knowledge
15880Sstevel@tonic-gate 			 * of file versions.
15892900Sfrankho 			 *
15902900Sfrankho 			 * XXX This is done the wrong way: it does not take
15912900Sfrankho 			 * XXX care of the fact that the version string is
15922900Sfrankho 			 * XXX a decimal number in the range 1 to 32767.
15930Sstevel@tonic-gate 			 */
15942900Sfrankho 			if ((fsp->hsfs_flags & HSFSMNT_NOVERSION) == 0) {
15952900Sfrankho 				if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
15962900Sfrankho 					for (i = dnamelen - 1; i > 0; i -= 2) {
15972900Sfrankho 						if (dname[i] == ';' &&
15982900Sfrankho 						    dname[i-1] == '\0') {
15992900Sfrankho 							--i;
16002900Sfrankho 							break;
16012900Sfrankho 						}
16022900Sfrankho 					}
16032900Sfrankho 				} else {
16042900Sfrankho 					for (i = dnamelen - 1; i > 0; i--) {
16052900Sfrankho 						if (dname[i] == ';')
16062900Sfrankho 							break;
16072900Sfrankho 					}
16082900Sfrankho 				}
16092900Sfrankho 			}
16102900Sfrankho 			if (i > 0) {
16112900Sfrankho 				dnamelen = i;
16122900Sfrankho 			} else if (fsp->hsfs_vol_type != HS_VOL_TYPE_ISO_V2 &&
16134866Sfrankho 			    fsp->hsfs_vol_type != HS_VOL_TYPE_JOLIET) {
16142900Sfrankho 				dnamelen = strip_trailing(fsp, dname, dnamelen);
16152900Sfrankho 			}
16160Sstevel@tonic-gate 
16172900Sfrankho 			ASSERT(dnamelen < sizeof (uppercase_name));
16180Sstevel@tonic-gate 
16192900Sfrankho 			if (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2) {
16202900Sfrankho 				(void) strncpy(uppercase_name, dname, dnamelen);
16212900Sfrankho 			} else if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
16222900Sfrankho 				dnamelen = hs_joliet_cp(dname, uppercase_name,
16234866Sfrankho 				    dnamelen);
16242900Sfrankho 			} else if (uppercase_cp(dname, uppercase_name,
16254866Sfrankho 			    dnamelen)) {
16260Sstevel@tonic-gate 				hs_log_bogus_disk_warning(fsp,
1627494Sfrankho 				    HSFS_ERR_LOWER_CASE_NM, 0);
16282900Sfrankho 			}
16290Sstevel@tonic-gate 			dname = uppercase_name;
1630494Sfrankho 			if (!is_rrip &&
1631494Sfrankho 			    (fsp->hsfs_flags & HSFSMNT_NOTRAILDOT) &&
1632494Sfrankho 			    dname[dnamelen - 1] == '.' &&
1633494Sfrankho 			    CAN_TRUNCATE_DOT(dname, dnamelen))
1634494Sfrankho 				dname[--dnamelen] = '\0';
16350Sstevel@tonic-gate 		}
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 		/*
16380Sstevel@tonic-gate 		 * Quickly screen for a non-matching entry, but not for RRIP.
16390Sstevel@tonic-gate 		 * This test doesn't work for lowercase vs. uppercase names.
16400Sstevel@tonic-gate 		 */
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 		/* if we saw a lower case name we can't do this test either */
16430Sstevel@tonic-gate 		if (strict_iso9660_ordering && !is_rrip &&
1644494Sfrankho 		    !HSFS_HAVE_LOWER_CASE(fsp) && *nm < *dname) {
16450Sstevel@tonic-gate 			RESTORE_NM(rrip_tmp_name, nm);
16460Sstevel@tonic-gate 			PD_return(WENT_PAST)
16470Sstevel@tonic-gate 		}
16480Sstevel@tonic-gate 
1649494Sfrankho 		if (*nm != *dname || nmlen != dnamelen)
1650494Sfrankho 			goto skip_rec;
16510Sstevel@tonic-gate 
16522900Sfrankho 		if ((res = bcmp(dname, nm, nmlen)) == 0) {
16530Sstevel@tonic-gate 			/* name matches */
1654494Sfrankho 			parsedir_res = hs_parsedir(fsp, dirp, &hd,
16552900Sfrankho 			    (char *)NULL, (int *)NULL,
16564866Sfrankho 			    last_offset - *offset);
16570Sstevel@tonic-gate 			if (!parsedir_res) {
16580Sstevel@tonic-gate 				uint_t lbn;	/* logical block number */
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 				lbn = dhp->hs_dirent.ext_lbn +
1661494Sfrankho 				    dhp->hs_dirent.xar_len;
16620Sstevel@tonic-gate 				/*
16630Sstevel@tonic-gate 				 * Need to do an fbrelse() on the buffer,
16640Sstevel@tonic-gate 				 * as hs_makenode() may try to acquire
16650Sstevel@tonic-gate 				 * hs_hashlock, which may not be required
16660Sstevel@tonic-gate 				 * while a page is locked.
16670Sstevel@tonic-gate 				 */
16680Sstevel@tonic-gate 				fbrelse(fbp, S_READ);
16690Sstevel@tonic-gate 				did_fbrelse = 1;
1670494Sfrankho 				*vpp = hs_makenode(&hd, lbn, *offset,
1671494Sfrankho 				    dvp->v_vfsp);
16720Sstevel@tonic-gate 				if (*vpp == NULL) {
16730Sstevel@tonic-gate 					*error = ENFILE;
16740Sstevel@tonic-gate 					RESTORE_NM(rrip_tmp_name, nm);
16750Sstevel@tonic-gate 					PD_return(FOUND_ENTRY)
16760Sstevel@tonic-gate 				}
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 				dhp->hs_offset = *offset;
16790Sstevel@tonic-gate 				RESTORE_NM(rrip_tmp_name, nm);
16800Sstevel@tonic-gate 				PD_return(FOUND_ENTRY)
16810Sstevel@tonic-gate 			} else if (parsedir_res != EAGAIN) {
16820Sstevel@tonic-gate 				/* improper dir entry */
16830Sstevel@tonic-gate 				*error = parsedir_res;
16840Sstevel@tonic-gate 				RESTORE_NM(rrip_tmp_name, nm);
16850Sstevel@tonic-gate 				PD_return(FOUND_ENTRY)
16860Sstevel@tonic-gate 			}
16870Sstevel@tonic-gate 		} else if (strict_iso9660_ordering && !is_rrip &&
16884866Sfrankho 		    !HSFS_HAVE_LOWER_CASE(fsp) && res < 0) {
16890Sstevel@tonic-gate 			/* name < dir entry */
16900Sstevel@tonic-gate 			RESTORE_NM(rrip_tmp_name, nm);
16910Sstevel@tonic-gate 			PD_return(WENT_PAST)
16920Sstevel@tonic-gate 		}
16930Sstevel@tonic-gate 		/*
16940Sstevel@tonic-gate 		 * name > dir entry,
16950Sstevel@tonic-gate 		 * look at next one.
16960Sstevel@tonic-gate 		 */
1697494Sfrankho skip_rec:
16980Sstevel@tonic-gate 		*offset += hdlen;
16990Sstevel@tonic-gate 		RESTORE_NM(rrip_tmp_name, nm);
17000Sstevel@tonic-gate 	}
17010Sstevel@tonic-gate 	PD_return(HIT_END)
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate do_ret:
17040Sstevel@tonic-gate 	if (rrip_name_str)
17050Sstevel@tonic-gate 		kmem_free(rrip_name_str, rrip_name_size);
17060Sstevel@tonic-gate 	if (rrip_tmp_name)
17070Sstevel@tonic-gate 		kmem_free(rrip_tmp_name, rrip_name_size);
1708494Sfrankho 	if (!did_fbrelse)
17090Sstevel@tonic-gate 		fbrelse(fbp, S_READ);
17100Sstevel@tonic-gate 	return (err);
17110Sstevel@tonic-gate #undef PD_return
17122900Sfrankho #undef RESTORE_NM
17130Sstevel@tonic-gate }
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate /*
17160Sstevel@tonic-gate  * Strip trailing nulls or spaces from the name;
17170Sstevel@tonic-gate  * return adjusted length.  If we find such junk,
17180Sstevel@tonic-gate  * log a non-conformant disk message.
17190Sstevel@tonic-gate  */
17200Sstevel@tonic-gate static int
strip_trailing(struct hsfs * fsp,char * nm,int len)17210Sstevel@tonic-gate strip_trailing(struct hsfs *fsp, char *nm, int len)
17220Sstevel@tonic-gate {
17230Sstevel@tonic-gate 	char *c;
17240Sstevel@tonic-gate 	int trailing_junk = 0;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	for (c = nm + len - 1; c > nm; c--) {
17270Sstevel@tonic-gate 		if (*c == ' ' || *c == '\0')
17280Sstevel@tonic-gate 			trailing_junk = 1;
17290Sstevel@tonic-gate 		else
17300Sstevel@tonic-gate 			break;
17310Sstevel@tonic-gate 	}
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 	if (trailing_junk)
17340Sstevel@tonic-gate 		hs_log_bogus_disk_warning(fsp, HSFS_ERR_TRAILING_JUNK, 0);
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	return ((int)(c - nm + 1));
17370Sstevel@tonic-gate }
17382900Sfrankho 
17392900Sfrankho static int
hs_namelen(struct hsfs * fsp,char * nm,int len)17402900Sfrankho hs_namelen(struct hsfs *fsp, char *nm, int len)
17412900Sfrankho {
17422900Sfrankho 	char	*p = nm + len;
17432900Sfrankho 
17442900Sfrankho 	if (fsp->hsfs_vol_type == HS_VOL_TYPE_ISO_V2) {
17452900Sfrankho 		return (len);
17462900Sfrankho 	} else if (fsp->hsfs_vol_type == HS_VOL_TYPE_JOLIET) {
17472900Sfrankho 		uint16_t c;
17482900Sfrankho 
17492900Sfrankho 		while (--p > &nm[1]) {
17502900Sfrankho 			c = *p;
17512900Sfrankho 			c |= *--p * 256;
17522900Sfrankho 			if (c == ';')
17532900Sfrankho 				return (p - nm);
17542900Sfrankho 			if (c < '0' || c > '9') {
17552900Sfrankho 				p++;
17562900Sfrankho 				return (p - nm);
17572900Sfrankho 			}
17582900Sfrankho 		}
17592900Sfrankho 	} else {
17602900Sfrankho 		char	c;
17612900Sfrankho 
17622900Sfrankho 		while (--p > nm) {
17632900Sfrankho 			c = *p;
17642900Sfrankho 			if (c == ';')
17652900Sfrankho 				return (p - nm);
17662900Sfrankho 			if (c < '0' || c > '9') {
17672900Sfrankho 				p++;
17682900Sfrankho 				return (p - nm);
17692900Sfrankho 			}
17702900Sfrankho 		}
17712900Sfrankho 	}
17722900Sfrankho 	return (len);
17732900Sfrankho }
17742900Sfrankho 
17752900Sfrankho /*
17762900Sfrankho  * Take a UCS-2 character and convert
17772900Sfrankho  * it into a utf8 character.
17782900Sfrankho  * A 0 will be returned if the conversion fails
17792900Sfrankho  *
17802900Sfrankho  * See http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
17812900Sfrankho  *
17822900Sfrankho  * The code has been taken from udfs/udf_subr.c
17832900Sfrankho  */
17842900Sfrankho static uint8_t hs_first_byte_mark[7] =
17852900Sfrankho 			{ 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
17862900Sfrankho static int32_t
hs_ucs2_2_utf8(uint16_t c_16,uint8_t * s_8)17872900Sfrankho hs_ucs2_2_utf8(uint16_t c_16, uint8_t *s_8)
17882900Sfrankho {
17892900Sfrankho 	int32_t nc;
17902900Sfrankho 	uint32_t c_32;
17912900Sfrankho 	uint32_t byte_mask = 0xBF;
17922900Sfrankho 	uint32_t byte_mark = 0x80;
17932900Sfrankho 
17942900Sfrankho 	/*
17952900Sfrankho 	 * Convert the 16-bit character to a 32-bit character
17962900Sfrankho 	 */
17972900Sfrankho 	c_32 = c_16;
17982900Sfrankho 
17992900Sfrankho 	/*
18002900Sfrankho 	 * By here the 16-bit character is converted
18012900Sfrankho 	 * to a 32-bit wide character
18022900Sfrankho 	 */
18032900Sfrankho 	if (c_32 < 0x80) {
18042900Sfrankho 		nc = 1;
18052900Sfrankho 	} else if (c_32 < 0x800) {
18062900Sfrankho 		nc = 2;
18072900Sfrankho 	} else if (c_32 < 0x10000) {
18082900Sfrankho 		nc = 3;
18092900Sfrankho 	} else if (c_32 < 0x200000) {
18102900Sfrankho 		nc = 4;
18112900Sfrankho 	} else if (c_32 < 0x4000000) {
18122900Sfrankho 		nc = 5;
18132900Sfrankho 	} else if (c_32 <= 0x7FFFFFFF) {	/* avoid signed overflow */
18142900Sfrankho 		nc = 6;
18152900Sfrankho 	} else {
18162900Sfrankho 		nc = 0;
18172900Sfrankho 	}
18182900Sfrankho 	s_8 += nc;
18192900Sfrankho 	switch (nc) {
18202900Sfrankho 		case 6 :
18212900Sfrankho 			*(--s_8) = (c_32 | byte_mark)  & byte_mask;
18222900Sfrankho 			c_32 >>= 6;
18232900Sfrankho 			/* FALLTHROUGH */
18242900Sfrankho 		case 5 :
18252900Sfrankho 			*(--s_8) = (c_32 | byte_mark)  & byte_mask;
18262900Sfrankho 			c_32 >>= 6;
18272900Sfrankho 			/* FALLTHROUGH */
18282900Sfrankho 		case 4 :
18292900Sfrankho 			*(--s_8) = (c_32 | byte_mark)  & byte_mask;
18302900Sfrankho 			c_32 >>= 6;
18312900Sfrankho 			/* FALLTHROUGH */
18322900Sfrankho 		case 3 :
18332900Sfrankho 			*(--s_8) = (c_32 | byte_mark)  & byte_mask;
18342900Sfrankho 			c_32 >>= 6;
18352900Sfrankho 			/* FALLTHROUGH */
18362900Sfrankho 		case 2 :
18372900Sfrankho 			*(--s_8) = (c_32 | byte_mark)  & byte_mask;
18382900Sfrankho 			c_32 >>= 6;
18392900Sfrankho 			/* FALLTHROUGH */
18402900Sfrankho 		case 1 :
18412900Sfrankho 			*(--s_8) = c_32 | hs_first_byte_mark[nc];
18422900Sfrankho 	}
18432900Sfrankho 	return (nc);
18442900Sfrankho }
1845