17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Vnode operations for the High Sierra filesystem
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/time.h>
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
397c478bd9Sstevel@tonic-gate #include <sys/resource.h>
407c478bd9Sstevel@tonic-gate #include <sys/signal.h>
417c478bd9Sstevel@tonic-gate #include <sys/cred.h>
427c478bd9Sstevel@tonic-gate #include <sys/user.h>
437c478bd9Sstevel@tonic-gate #include <sys/buf.h>
447c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
477c478bd9Sstevel@tonic-gate #include <sys/mode.h>
487c478bd9Sstevel@tonic-gate #include <sys/proc.h>
497c478bd9Sstevel@tonic-gate #include <sys/disp.h>
507c478bd9Sstevel@tonic-gate #include <sys/file.h>
517c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
527c478bd9Sstevel@tonic-gate #include <sys/flock.h>
537c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
547c478bd9Sstevel@tonic-gate #include <sys/uio.h>
557c478bd9Sstevel@tonic-gate #include <sys/conf.h>
567c478bd9Sstevel@tonic-gate #include <sys/errno.h>
577c478bd9Sstevel@tonic-gate #include <sys/mman.h>
587c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
597c478bd9Sstevel@tonic-gate #include <sys/debug.h>
607c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
617c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
627c478bd9Sstevel@tonic-gate #include <sys/fbuf.h>
637c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
647c478bd9Sstevel@tonic-gate #include <sys/errno.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <vm/hat.h>
677c478bd9Sstevel@tonic-gate #include <vm/page.h>
687c478bd9Sstevel@tonic-gate #include <vm/pvn.h>
697c478bd9Sstevel@tonic-gate #include <vm/as.h>
707c478bd9Sstevel@tonic-gate #include <vm/seg.h>
717c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
727c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
737c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
747c478bd9Sstevel@tonic-gate #include <vm/rm.h>
757c478bd9Sstevel@tonic-gate #include <vm/page.h>
767c478bd9Sstevel@tonic-gate #include <sys/swap.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #include <sys/fs/hsfs_spec.h>
797c478bd9Sstevel@tonic-gate #include <sys/fs/hsfs_node.h>
807c478bd9Sstevel@tonic-gate #include <sys/fs/hsfs_impl.h>
817c478bd9Sstevel@tonic-gate #include <sys/fs/hsfs_susp.h>
827c478bd9Sstevel@tonic-gate #include <sys/fs/hsfs_rrip.h>
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* ARGSUSED */
877c478bd9Sstevel@tonic-gate static int
887c478bd9Sstevel@tonic-gate hsfs_fsync(vnode_t *cp, int syncflag, cred_t *cred)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	return (0);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
957c478bd9Sstevel@tonic-gate static int
967c478bd9Sstevel@tonic-gate hsfs_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cred,
977c478bd9Sstevel@tonic-gate 	struct caller_context *ct)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	caddr_t base;
100*8cd7c4fcSpeterte 	offset_t diff;
1017c478bd9Sstevel@tonic-gate 	int error;
102*8cd7c4fcSpeterte 	struct hsnode *hp;
103*8cd7c4fcSpeterte 	uint_t filesize;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	hp = VTOH(vp);
1067c478bd9Sstevel@tonic-gate 	/*
1077c478bd9Sstevel@tonic-gate 	 * if vp is of type VDIR, make sure dirent
1087c478bd9Sstevel@tonic-gate 	 * is filled up with all info (because of ptbl)
1097c478bd9Sstevel@tonic-gate 	 */
1107c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
1117c478bd9Sstevel@tonic-gate 		if (hp->hs_dirent.ext_size == 0)
1127c478bd9Sstevel@tonic-gate 			hs_filldirent(vp, &hp->hs_dirent);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	filesize = hp->hs_dirent.ext_size;
1157c478bd9Sstevel@tonic-gate 
116*8cd7c4fcSpeterte 	/* Sanity checks. */
117*8cd7c4fcSpeterte 	if (uiop->uio_resid == 0 ||		/* No data wanted. */
118*8cd7c4fcSpeterte 	    uiop->uio_loffset >= MAXOFF_T ||	/* Offset too big. */
119*8cd7c4fcSpeterte 	    uiop->uio_loffset >= filesize)	/* Past EOF. */
120*8cd7c4fcSpeterte 		return (0);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	do {
123*8cd7c4fcSpeterte 		/*
124*8cd7c4fcSpeterte 		 * We want to ask for only the "right" amount of data.
125*8cd7c4fcSpeterte 		 * In this case that means:-
126*8cd7c4fcSpeterte 		 *
127*8cd7c4fcSpeterte 		 * We can't get data from beyond our EOF. If asked,
128*8cd7c4fcSpeterte 		 * we will give a short read.
129*8cd7c4fcSpeterte 		 *
130*8cd7c4fcSpeterte 		 * segmap_getmapflt returns buffers of MAXBSIZE bytes.
131*8cd7c4fcSpeterte 		 * These buffers are always MAXBSIZE aligned.
132*8cd7c4fcSpeterte 		 * If our starting offset is not MAXBSIZE aligned,
133*8cd7c4fcSpeterte 		 * we can only ask for less than MAXBSIZE bytes.
134*8cd7c4fcSpeterte 		 *
135*8cd7c4fcSpeterte 		 * If our requested offset and length are such that
136*8cd7c4fcSpeterte 		 * they belong in different MAXBSIZE aligned slots
137*8cd7c4fcSpeterte 		 * then we'll be making more than one call on
138*8cd7c4fcSpeterte 		 * segmap_getmapflt.
139*8cd7c4fcSpeterte 		 *
140*8cd7c4fcSpeterte 		 * This diagram shows the variables we use and their
141*8cd7c4fcSpeterte 		 * relationships.
142*8cd7c4fcSpeterte 		 *
143*8cd7c4fcSpeterte 		 * |<-----MAXBSIZE----->|
144*8cd7c4fcSpeterte 		 * +--------------------------...+
145*8cd7c4fcSpeterte 		 * |.....mapon->|<--n-->|....*...|EOF
146*8cd7c4fcSpeterte 		 * +--------------------------...+
147*8cd7c4fcSpeterte 		 * uio_loffset->|
148*8cd7c4fcSpeterte 		 * uio_resid....|<---------->|
149*8cd7c4fcSpeterte 		 * diff.........|<-------------->|
150*8cd7c4fcSpeterte 		 *
151*8cd7c4fcSpeterte 		 * So, in this case our offset is not aligned
152*8cd7c4fcSpeterte 		 * and our request takes us outside of the
153*8cd7c4fcSpeterte 		 * MAXBSIZE window. We will break this up into
154*8cd7c4fcSpeterte 		 * two segmap_getmapflt calls.
155*8cd7c4fcSpeterte 		 */
156*8cd7c4fcSpeterte 		size_t nbytes;
157*8cd7c4fcSpeterte 		offset_t mapon;
158*8cd7c4fcSpeterte 		size_t n;
159*8cd7c4fcSpeterte 		uint_t flags;
160*8cd7c4fcSpeterte 
161*8cd7c4fcSpeterte 		mapon = uiop->uio_loffset & MAXBOFFSET;
162*8cd7c4fcSpeterte 		diff = filesize - uiop->uio_loffset;
163*8cd7c4fcSpeterte 		nbytes = (size_t)MIN(MAXBSIZE - mapon, uiop->uio_resid);
164*8cd7c4fcSpeterte 		n = MIN(diff, nbytes);
165*8cd7c4fcSpeterte 		if (n <= 0) {
166*8cd7c4fcSpeterte 			/* EOF or request satisfied. */
167*8cd7c4fcSpeterte 			return (0);
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 
170*8cd7c4fcSpeterte 		base = segmap_getmapflt(segkmap, vp,
171*8cd7c4fcSpeterte 		    (u_offset_t)uiop->uio_loffset, n, 1, S_READ);
172*8cd7c4fcSpeterte 
173*8cd7c4fcSpeterte 		error = uiomove(base + mapon, n, UIO_READ, uiop);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		if (error == 0) {
1767c478bd9Sstevel@tonic-gate 			/*
1777c478bd9Sstevel@tonic-gate 			 * if read a whole block, or read to eof,
1787c478bd9Sstevel@tonic-gate 			 *  won't need this buffer again soon.
1797c478bd9Sstevel@tonic-gate 			 */
180*8cd7c4fcSpeterte 			if (n + mapon == MAXBSIZE ||
181*8cd7c4fcSpeterte 			    uiop->uio_loffset == filesize)
1827c478bd9Sstevel@tonic-gate 				flags = SM_DONTNEED;
1837c478bd9Sstevel@tonic-gate 			else
1847c478bd9Sstevel@tonic-gate 				flags = 0;
1857c478bd9Sstevel@tonic-gate 			error = segmap_release(segkmap, base, flags);
1867c478bd9Sstevel@tonic-gate 		} else
1877c478bd9Sstevel@tonic-gate 			(void) segmap_release(segkmap, base, 0);
1887c478bd9Sstevel@tonic-gate 	} while (error == 0 && uiop->uio_resid > 0);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	return (error);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
1947c478bd9Sstevel@tonic-gate static int
1957c478bd9Sstevel@tonic-gate hsfs_getattr(
1967c478bd9Sstevel@tonic-gate 	struct vnode *vp,
1977c478bd9Sstevel@tonic-gate 	struct vattr *vap,
1987c478bd9Sstevel@tonic-gate 	int flags,
1997c478bd9Sstevel@tonic-gate 	struct cred *cred)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	struct hsnode *hp;
2027c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
2037c478bd9Sstevel@tonic-gate 	struct hsfs *fsp;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	hp = VTOH(vp);
2067c478bd9Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vp->v_vfsp);
2077c478bd9Sstevel@tonic-gate 	vfsp = vp->v_vfsp;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if ((hp->hs_dirent.ext_size == 0) && (vp->v_type == VDIR)) {
2107c478bd9Sstevel@tonic-gate 		hs_filldirent(vp, &hp->hs_dirent);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	vap->va_type = IFTOVT(hp->hs_dirent.mode);
2137c478bd9Sstevel@tonic-gate 	vap->va_mode = hp->hs_dirent.mode;
2147c478bd9Sstevel@tonic-gate 	vap->va_uid = hp->hs_dirent.uid;
2157c478bd9Sstevel@tonic-gate 	vap->va_gid = hp->hs_dirent.gid;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	vap->va_fsid = vfsp->vfs_dev;
2187c478bd9Sstevel@tonic-gate 	vap->va_nodeid = (ino64_t)hp->hs_nodeid;
2197c478bd9Sstevel@tonic-gate 	vap->va_nlink = hp->hs_dirent.nlink;
2207c478bd9Sstevel@tonic-gate 	vap->va_size =	(offset_t)hp->hs_dirent.ext_size;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_sec = hp->hs_dirent.adate.tv_sec;
2237c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_nsec = hp->hs_dirent.adate.tv_usec*1000;
2247c478bd9Sstevel@tonic-gate 	vap->va_mtime.tv_sec = hp->hs_dirent.mdate.tv_sec;
2257c478bd9Sstevel@tonic-gate 	vap->va_mtime.tv_nsec = hp->hs_dirent.mdate.tv_usec*1000;
2267c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_sec = hp->hs_dirent.cdate.tv_sec;
2277c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = hp->hs_dirent.cdate.tv_usec*1000;
2287c478bd9Sstevel@tonic-gate 	if (vp->v_type == VCHR || vp->v_type == VBLK)
2297c478bd9Sstevel@tonic-gate 		vap->va_rdev = hp->hs_dirent.r_dev;
2307c478bd9Sstevel@tonic-gate 	else
2317c478bd9Sstevel@tonic-gate 		vap->va_rdev = 0;
2327c478bd9Sstevel@tonic-gate 	vap->va_blksize = vfsp->vfs_bsize;
2337c478bd9Sstevel@tonic-gate 	/* no. of blocks = no. of data blocks + no. of xar blocks */
2347c478bd9Sstevel@tonic-gate 	vap->va_nblocks = (fsblkcnt64_t)howmany(vap->va_size + (u_longlong_t)
2357c478bd9Sstevel@tonic-gate 	    (hp->hs_dirent.xar_len << fsp->hsfs_vol.lbn_shift), DEV_BSIZE);
2367c478bd9Sstevel@tonic-gate 	vap->va_seq = hp->hs_seq;
2377c478bd9Sstevel@tonic-gate 	return (0);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2417c478bd9Sstevel@tonic-gate static int
2427c478bd9Sstevel@tonic-gate hsfs_readlink(struct vnode *vp, struct uio *uiop, struct cred *cred)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	struct hsnode *hp;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (vp->v_type != VLNK)
2477c478bd9Sstevel@tonic-gate 		return (EINVAL);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	hp = VTOH(vp);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (hp->hs_dirent.sym_link == (char *)NULL)
2527c478bd9Sstevel@tonic-gate 		return (ENOENT);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	return (uiomove(hp->hs_dirent.sym_link,
2557c478bd9Sstevel@tonic-gate 	    (size_t)MIN(hp->hs_dirent.ext_size,
2567c478bd9Sstevel@tonic-gate 	    uiop->uio_resid), UIO_READ, uiop));
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2607c478bd9Sstevel@tonic-gate static void
2617c478bd9Sstevel@tonic-gate hsfs_inactive(struct vnode *vp, struct cred *cred)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	struct hsnode *hp;
2647c478bd9Sstevel@tonic-gate 	struct hsfs *fsp;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	int nopage;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	hp = VTOH(vp);
2697c478bd9Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vp->v_vfsp);
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * Note: acquiring and holding v_lock for quite a while
2727c478bd9Sstevel@tonic-gate 	 * here serializes on the vnode; this is unfortunate, but
2737c478bd9Sstevel@tonic-gate 	 * likely not to overly impact performance, as the underlying
2747c478bd9Sstevel@tonic-gate 	 * device (CDROM drive) is quite slow.
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	rw_enter(&fsp->hsfs_hash_lock, RW_WRITER);
2777c478bd9Sstevel@tonic-gate 	mutex_enter(&hp->hs_contents_lock);
2787c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	if (vp->v_count < 1) {
2817c478bd9Sstevel@tonic-gate 		panic("hsfs_inactive: v_count < 1");
2827c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if (vp->v_count > 1 || (hp->hs_flags & HREF) == 0) {
2867c478bd9Sstevel@tonic-gate 		vp->v_count--;	/* release hold from vn_rele */
2877c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
2887c478bd9Sstevel@tonic-gate 		mutex_exit(&hp->hs_contents_lock);
2897c478bd9Sstevel@tonic-gate 		rw_exit(&fsp->hsfs_hash_lock);
2907c478bd9Sstevel@tonic-gate 		return;
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 	vp->v_count--;	/* release hold from vn_rele */
2937c478bd9Sstevel@tonic-gate 	if (vp->v_count == 0) {
2947c478bd9Sstevel@tonic-gate 		/*
2957c478bd9Sstevel@tonic-gate 		 * Free the hsnode.
2967c478bd9Sstevel@tonic-gate 		 * If there are no pages associated with the
2977c478bd9Sstevel@tonic-gate 		 * hsnode, give it back to the kmem_cache,
2987c478bd9Sstevel@tonic-gate 		 * else put at the end of this file system's
2997c478bd9Sstevel@tonic-gate 		 * internal free list.
3007c478bd9Sstevel@tonic-gate 		 */
3017c478bd9Sstevel@tonic-gate 		nopage = !vn_has_cached_data(vp);
3027c478bd9Sstevel@tonic-gate 		hp->hs_flags = 0;
3037c478bd9Sstevel@tonic-gate 		/*
3047c478bd9Sstevel@tonic-gate 		 * exit these locks now, since hs_freenode may
3057c478bd9Sstevel@tonic-gate 		 * kmem_free the hsnode and embedded vnode
3067c478bd9Sstevel@tonic-gate 		 */
3077c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
3087c478bd9Sstevel@tonic-gate 		mutex_exit(&hp->hs_contents_lock);
3097c478bd9Sstevel@tonic-gate 		hs_freenode(vp, fsp, nopage);
3107c478bd9Sstevel@tonic-gate 	} else {
3117c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
3127c478bd9Sstevel@tonic-gate 		mutex_exit(&hp->hs_contents_lock);
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 	rw_exit(&fsp->hsfs_hash_lock);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3197c478bd9Sstevel@tonic-gate static int
3207c478bd9Sstevel@tonic-gate hsfs_lookup(
3217c478bd9Sstevel@tonic-gate 	struct vnode *dvp,
3227c478bd9Sstevel@tonic-gate 	char *nm,
3237c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
3247c478bd9Sstevel@tonic-gate 	struct pathname *pnp,
3257c478bd9Sstevel@tonic-gate 	int flags,
3267c478bd9Sstevel@tonic-gate 	struct vnode *rdir,
3277c478bd9Sstevel@tonic-gate 	struct cred *cred)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	int error;
3307c478bd9Sstevel@tonic-gate 	int namelen = (int)strlen(nm);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (*nm == '\0') {
3337c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
3347c478bd9Sstevel@tonic-gate 		*vpp = dvp;
3357c478bd9Sstevel@tonic-gate 		return (0);
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	/*
3397c478bd9Sstevel@tonic-gate 	 * If we're looking for ourself, life is simple.
3407c478bd9Sstevel@tonic-gate 	 */
3417c478bd9Sstevel@tonic-gate 	if (namelen == 1 && *nm == '.') {
3427c478bd9Sstevel@tonic-gate 		if (error = hs_access(dvp, (mode_t)VEXEC, cred))
3437c478bd9Sstevel@tonic-gate 			return (error);
3447c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
3457c478bd9Sstevel@tonic-gate 		*vpp = dvp;
3467c478bd9Sstevel@tonic-gate 		return (0);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	return (hs_dirlook(dvp, nm, namelen, vpp, cred));
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3547c478bd9Sstevel@tonic-gate static int
3557c478bd9Sstevel@tonic-gate hsfs_readdir(
3567c478bd9Sstevel@tonic-gate 	struct vnode	*vp,
3577c478bd9Sstevel@tonic-gate 	struct uio	*uiop,
3587c478bd9Sstevel@tonic-gate 	struct cred	*cred,
3597c478bd9Sstevel@tonic-gate 	int		*eofp)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate 	struct hsnode	*dhp;
3627c478bd9Sstevel@tonic-gate 	struct hsfs	*fsp;
3637c478bd9Sstevel@tonic-gate 	struct hs_direntry hd;
3647c478bd9Sstevel@tonic-gate 	struct dirent64	*nd;
3657c478bd9Sstevel@tonic-gate 	int		error;
3667c478bd9Sstevel@tonic-gate 	uint_t		offset;		/* real offset in directory */
3677c478bd9Sstevel@tonic-gate 	uint_t		dirsiz;		/* real size of directory */
3687c478bd9Sstevel@tonic-gate 	uchar_t		*blkp;
3697c478bd9Sstevel@tonic-gate 	int		hdlen;		/* length of hs directory entry */
3707c478bd9Sstevel@tonic-gate 	long		ndlen;		/* length of dirent entry */
3717c478bd9Sstevel@tonic-gate 	int		bytes_wanted;
3727c478bd9Sstevel@tonic-gate 	size_t		bufsize;	/* size of dirent buffer */
3737c478bd9Sstevel@tonic-gate 	char		*outbuf;	/* ptr to dirent buffer */
3747c478bd9Sstevel@tonic-gate 	char		*dname;
3757c478bd9Sstevel@tonic-gate 	int		dnamelen;
3767c478bd9Sstevel@tonic-gate 	size_t		dname_size;
3777c478bd9Sstevel@tonic-gate 	struct fbuf	*fbp;
3787c478bd9Sstevel@tonic-gate 	uint_t		last_offset;	/* last index into current dir block */
3797c478bd9Sstevel@tonic-gate 	ulong_t		dir_lbn;	/* lbn of directory */
3807c478bd9Sstevel@tonic-gate 	ino64_t		dirino;	/* temporary storage before storing in dirent */
3817c478bd9Sstevel@tonic-gate 	off_t		diroff;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	dhp = VTOH(vp);
3847c478bd9Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vp->v_vfsp);
3857c478bd9Sstevel@tonic-gate 	if (dhp->hs_dirent.ext_size == 0)
3867c478bd9Sstevel@tonic-gate 		hs_filldirent(vp, &dhp->hs_dirent);
3877c478bd9Sstevel@tonic-gate 	dirsiz = dhp->hs_dirent.ext_size;
3887c478bd9Sstevel@tonic-gate 	dir_lbn = dhp->hs_dirent.ext_lbn;
3897c478bd9Sstevel@tonic-gate 	if (uiop->uio_loffset >= dirsiz) {	/* at or beyond EOF */
3907c478bd9Sstevel@tonic-gate 		if (eofp)
3917c478bd9Sstevel@tonic-gate 			*eofp = 1;
3927c478bd9Sstevel@tonic-gate 		return (0);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 	ASSERT(uiop->uio_loffset <= MAXOFF_T);
3957c478bd9Sstevel@tonic-gate 	offset = (uint_t)uiop->uio_offset;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	dname_size = fsp->hsfs_namemax + 1;	/* 1 for the ending NUL */
3987c478bd9Sstevel@tonic-gate 	dname = kmem_alloc(dname_size, KM_SLEEP);
3997c478bd9Sstevel@tonic-gate 	bufsize = uiop->uio_resid + sizeof (struct dirent64);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	outbuf = kmem_alloc(bufsize, KM_SLEEP);
4027c478bd9Sstevel@tonic-gate 	nd = (struct dirent64 *)outbuf;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	while (offset < dirsiz) {
4057c478bd9Sstevel@tonic-gate 		if ((offset & MAXBMASK) + MAXBSIZE > dirsiz)
4067c478bd9Sstevel@tonic-gate 			bytes_wanted = dirsiz - (offset & MAXBMASK);
4077c478bd9Sstevel@tonic-gate 		else
4087c478bd9Sstevel@tonic-gate 			bytes_wanted = MAXBSIZE;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		error = fbread(vp, (offset_t)(offset & MAXBMASK),
4117c478bd9Sstevel@tonic-gate 			(unsigned int)bytes_wanted, S_READ, &fbp);
4127c478bd9Sstevel@tonic-gate 		if (error)
4137c478bd9Sstevel@tonic-gate 			goto done;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 		blkp = (uchar_t *)fbp->fb_addr;
4167c478bd9Sstevel@tonic-gate 		last_offset = (offset & MAXBMASK) + fbp->fb_count - 1;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate #define	rel_offset(offset) ((offset) & MAXBOFFSET)	/* index into blkp */
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 		while (offset < last_offset) {
4217c478bd9Sstevel@tonic-gate 			/*
4227c478bd9Sstevel@tonic-gate 			 * Directory Entries cannot span sectors.
4237c478bd9Sstevel@tonic-gate 			 * Unused bytes at the end of each sector are zeroed.
4247c478bd9Sstevel@tonic-gate 			 * Therefore, detect this condition when the size
4257c478bd9Sstevel@tonic-gate 			 * field of the directory entry is zero.
4267c478bd9Sstevel@tonic-gate 			 */
4277c478bd9Sstevel@tonic-gate 			hdlen = (int)((uchar_t)
4287c478bd9Sstevel@tonic-gate 				HDE_DIR_LEN(&blkp[rel_offset(offset)]));
4297c478bd9Sstevel@tonic-gate 			if (hdlen == 0) {
4307c478bd9Sstevel@tonic-gate 				/* advance to next sector boundary */
4317c478bd9Sstevel@tonic-gate 				offset = (offset & MAXHSMASK) + HS_SECTOR_SIZE;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 				/*
4347c478bd9Sstevel@tonic-gate 				 * Have we reached the end of current block?
4357c478bd9Sstevel@tonic-gate 				 */
4367c478bd9Sstevel@tonic-gate 				if (offset > last_offset)
4377c478bd9Sstevel@tonic-gate 					break;
4387c478bd9Sstevel@tonic-gate 				else
4397c478bd9Sstevel@tonic-gate 					continue;
4407c478bd9Sstevel@tonic-gate 			}
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 			/* make sure this is nullified before  reading it */
4437c478bd9Sstevel@tonic-gate 			bzero(&hd, sizeof (hd));
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 			/*
4467c478bd9Sstevel@tonic-gate 			 * Just ignore invalid directory entries.
4477c478bd9Sstevel@tonic-gate 			 * XXX - maybe hs_parsedir() will detect EXISTENCE bit
4487c478bd9Sstevel@tonic-gate 			 */
4497c478bd9Sstevel@tonic-gate 			if (!hs_parsedir(fsp, &blkp[rel_offset(offset)],
4507c478bd9Sstevel@tonic-gate 				&hd, dname, &dnamelen)) {
4517c478bd9Sstevel@tonic-gate 				/*
4527c478bd9Sstevel@tonic-gate 				 * Determine if there is enough room
4537c478bd9Sstevel@tonic-gate 				 */
4547c478bd9Sstevel@tonic-gate 				ndlen = (long)DIRENT64_RECLEN((dnamelen));
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 				if ((ndlen + ((char *)nd - outbuf)) >
4577c478bd9Sstevel@tonic-gate 				    uiop->uio_resid) {
4587c478bd9Sstevel@tonic-gate 					fbrelse(fbp, S_READ);
4597c478bd9Sstevel@tonic-gate 					goto done; /* output buffer full */
4607c478bd9Sstevel@tonic-gate 				}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 				diroff = offset + hdlen;
4637c478bd9Sstevel@tonic-gate 				/*
4647c478bd9Sstevel@tonic-gate 				 * Generate nodeid.
4657c478bd9Sstevel@tonic-gate 				 * If a directory, nodeid points to the
4667c478bd9Sstevel@tonic-gate 				 * canonical dirent describing the directory:
4677c478bd9Sstevel@tonic-gate 				 * the dirent of the "." entry for the
4687c478bd9Sstevel@tonic-gate 				 * directory, which is pointed to by all
4697c478bd9Sstevel@tonic-gate 				 * dirents for that directory.
4707c478bd9Sstevel@tonic-gate 				 * Otherwise, nodeid points to dirent of file.
4717c478bd9Sstevel@tonic-gate 				 */
4727c478bd9Sstevel@tonic-gate 				if (hd.type == VDIR) {
4737c478bd9Sstevel@tonic-gate 					dirino = (ino64_t)
4747c478bd9Sstevel@tonic-gate 					    MAKE_NODEID(hd.ext_lbn, 0,
4757c478bd9Sstevel@tonic-gate 					    vp->v_vfsp);
4767c478bd9Sstevel@tonic-gate 				} else {
4777c478bd9Sstevel@tonic-gate 					struct hs_volume *hvp;
4787c478bd9Sstevel@tonic-gate 					offset_t lbn, off;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 					/*
4817c478bd9Sstevel@tonic-gate 					 * Normalize lbn and off
4827c478bd9Sstevel@tonic-gate 					 */
4837c478bd9Sstevel@tonic-gate 					hvp = &fsp->hsfs_vol;
4847c478bd9Sstevel@tonic-gate 					lbn = dir_lbn +
4857c478bd9Sstevel@tonic-gate 					    (offset >> hvp->lbn_shift);
4867c478bd9Sstevel@tonic-gate 					off = offset & hvp->lbn_maxoffset;
4877c478bd9Sstevel@tonic-gate 					dirino = (ino64_t)MAKE_NODEID(lbn,
4887c478bd9Sstevel@tonic-gate 					    off, vp->v_vfsp);
4897c478bd9Sstevel@tonic-gate 				}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 				/* strncpy(9f) will zero uninitialized bytes */
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 				ASSERT(strlen(dname) + 1 <=
4957c478bd9Sstevel@tonic-gate 				    DIRENT64_NAMELEN(ndlen));
4967c478bd9Sstevel@tonic-gate 				(void) strncpy(nd->d_name, dname,
4977c478bd9Sstevel@tonic-gate 				    DIRENT64_NAMELEN(ndlen));
4987c478bd9Sstevel@tonic-gate 				nd->d_reclen = (ushort_t)ndlen;
4997c478bd9Sstevel@tonic-gate 				nd->d_off = (offset_t)diroff;
5007c478bd9Sstevel@tonic-gate 				nd->d_ino = dirino;
5017c478bd9Sstevel@tonic-gate 				nd = (struct dirent64 *)((char *)nd + ndlen);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 				/*
5047c478bd9Sstevel@tonic-gate 				 * free up space allocated for symlink
5057c478bd9Sstevel@tonic-gate 				 */
5067c478bd9Sstevel@tonic-gate 				if (hd.sym_link != (char *)NULL) {
5077c478bd9Sstevel@tonic-gate 					kmem_free(hd.sym_link,
5087c478bd9Sstevel@tonic-gate 					    (size_t)(hd.ext_size+1));
5097c478bd9Sstevel@tonic-gate 					hd.sym_link = (char *)NULL;
5107c478bd9Sstevel@tonic-gate 				}
5117c478bd9Sstevel@tonic-gate 			}
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 			offset += hdlen;
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 		fbrelse(fbp, S_READ);
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	/*
5197c478bd9Sstevel@tonic-gate 	 * Got here for one of the following reasons:
5207c478bd9Sstevel@tonic-gate 	 *	1) outbuf is full (error == 0)
5217c478bd9Sstevel@tonic-gate 	 *	2) end of directory reached (error == 0)
5227c478bd9Sstevel@tonic-gate 	 *	3) error reading directory sector (error != 0)
5237c478bd9Sstevel@tonic-gate 	 *	4) directory entry crosses sector boundary (error == 0)
5247c478bd9Sstevel@tonic-gate 	 *
5257c478bd9Sstevel@tonic-gate 	 * If any directory entries have been copied, don't report
5267c478bd9Sstevel@tonic-gate 	 * case 4.  Instead, return the valid directory entries.
5277c478bd9Sstevel@tonic-gate 	 *
5287c478bd9Sstevel@tonic-gate 	 * If no entries have been copied, report the error.
5297c478bd9Sstevel@tonic-gate 	 * If case 4, this will be indistiguishable from EOF.
5307c478bd9Sstevel@tonic-gate 	 */
5317c478bd9Sstevel@tonic-gate done:
5327c478bd9Sstevel@tonic-gate 	ndlen = ((char *)nd - outbuf);
5337c478bd9Sstevel@tonic-gate 	if (ndlen != 0) {
5347c478bd9Sstevel@tonic-gate 		error = uiomove(outbuf, (size_t)ndlen, UIO_READ, uiop);
5357c478bd9Sstevel@tonic-gate 		uiop->uio_offset = offset;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 	kmem_free(dname, dname_size);
5387c478bd9Sstevel@tonic-gate 	kmem_free(outbuf, bufsize);
5397c478bd9Sstevel@tonic-gate 	if (eofp && error == 0)
5407c478bd9Sstevel@tonic-gate 		*eofp = (uiop->uio_offset >= dirsiz);
5417c478bd9Sstevel@tonic-gate 	return (error);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate static int
5457c478bd9Sstevel@tonic-gate hsfs_fid(struct vnode *vp, struct fid *fidp)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	struct hsnode *hp;
5487c478bd9Sstevel@tonic-gate 	struct hsfid *fid;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (fidp->fid_len < (sizeof (*fid) - sizeof (fid->hf_len))) {
5517c478bd9Sstevel@tonic-gate 		fidp->fid_len = sizeof (*fid) - sizeof (fid->hf_len);
5527c478bd9Sstevel@tonic-gate 		return (ENOSPC);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	fid = (struct hsfid *)fidp;
5567c478bd9Sstevel@tonic-gate 	fid->hf_len = sizeof (*fid) - sizeof (fid->hf_len);
5577c478bd9Sstevel@tonic-gate 	hp = VTOH(vp);
5587c478bd9Sstevel@tonic-gate 	mutex_enter(&hp->hs_contents_lock);
5597c478bd9Sstevel@tonic-gate 	fid->hf_dir_lbn = hp->hs_dir_lbn;
5607c478bd9Sstevel@tonic-gate 	fid->hf_dir_off = (ushort_t)hp->hs_dir_off;
5617c478bd9Sstevel@tonic-gate 	mutex_exit(&hp->hs_contents_lock);
5627c478bd9Sstevel@tonic-gate 	return (0);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5667c478bd9Sstevel@tonic-gate static int
5677c478bd9Sstevel@tonic-gate hsfs_open(struct vnode **vpp, int flag, struct cred *cred)
5687c478bd9Sstevel@tonic-gate {
5697c478bd9Sstevel@tonic-gate 	return (0);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5737c478bd9Sstevel@tonic-gate static int
5747c478bd9Sstevel@tonic-gate hsfs_close(
5757c478bd9Sstevel@tonic-gate 	struct vnode *vp,
5767c478bd9Sstevel@tonic-gate 	int flag,
5777c478bd9Sstevel@tonic-gate 	int count,
5787c478bd9Sstevel@tonic-gate 	offset_t offset,
5797c478bd9Sstevel@tonic-gate 	struct cred *cred)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	(void) cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
5827c478bd9Sstevel@tonic-gate 	cleanshares(vp, ttoproc(curthread)->p_pid);
5837c478bd9Sstevel@tonic-gate 	return (0);
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
5877c478bd9Sstevel@tonic-gate static int
5887c478bd9Sstevel@tonic-gate hsfs_access(struct vnode *vp, int mode, int flags, cred_t *cred)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	return (hs_access(vp, (mode_t)mode, cred));
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate /*
5947c478bd9Sstevel@tonic-gate  * the seek time of a CD-ROM is very slow, and data transfer
5957c478bd9Sstevel@tonic-gate  * rate is even worse (max. 150K per sec).  The design
5967c478bd9Sstevel@tonic-gate  * decision is to reduce access to cd-rom as much as possible,
5977c478bd9Sstevel@tonic-gate  * and to transfer a sizable block (read-ahead) of data at a time.
5987c478bd9Sstevel@tonic-gate  * UFS style of read ahead one block at a time is not appropriate,
5997c478bd9Sstevel@tonic-gate  * and is not supported
6007c478bd9Sstevel@tonic-gate  */
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /*
6037c478bd9Sstevel@tonic-gate  * KLUSTSIZE should be a multiple of PAGESIZE and <= MAXPHYS.
6047c478bd9Sstevel@tonic-gate  */
6057c478bd9Sstevel@tonic-gate #define	KLUSTSIZE	(56 * 1024)
6067c478bd9Sstevel@tonic-gate /* we don't support read ahead */
6077c478bd9Sstevel@tonic-gate int hsfs_lostpage;	/* no. of times we lost original page */
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate /*
6107c478bd9Sstevel@tonic-gate  * Used to prevent biodone() from releasing buf resources that
6117c478bd9Sstevel@tonic-gate  * we didn't allocate in quite the usual way.
6127c478bd9Sstevel@tonic-gate  */
6137c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6147c478bd9Sstevel@tonic-gate int
6157c478bd9Sstevel@tonic-gate hsfs_iodone(struct buf *bp)
6167c478bd9Sstevel@tonic-gate {
6177c478bd9Sstevel@tonic-gate 	sema_v(&bp->b_io);
6187c478bd9Sstevel@tonic-gate 	return (0);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate /*
6227c478bd9Sstevel@tonic-gate  * Each file may have a different interleaving on disk.  This makes
6237c478bd9Sstevel@tonic-gate  * things somewhat interesting.  The gist is that there are some
6247c478bd9Sstevel@tonic-gate  * number of contiguous data sectors, followed by some other number
6257c478bd9Sstevel@tonic-gate  * of contiguous skip sectors.  The sum of those two sets of sectors
6267c478bd9Sstevel@tonic-gate  * defines the interleave size.  Unfortunately, it means that we generally
6277c478bd9Sstevel@tonic-gate  * can't simply read N sectors starting at a given offset to satisfy
6287c478bd9Sstevel@tonic-gate  * any given request.
6297c478bd9Sstevel@tonic-gate  *
6307c478bd9Sstevel@tonic-gate  * What we do is get the relevant memory pages via pvn_read_kluster(),
6317c478bd9Sstevel@tonic-gate  * then stride through the interleaves, setting up a buf for each
6327c478bd9Sstevel@tonic-gate  * sector that needs to be brought in.  Instead of kmem_alloc'ing
6337c478bd9Sstevel@tonic-gate  * space for the sectors, though, we just point at the appropriate
6347c478bd9Sstevel@tonic-gate  * spot in the relevant page for each of them.  This saves us a bunch
6357c478bd9Sstevel@tonic-gate  * of copying.
6367c478bd9Sstevel@tonic-gate  */
6377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6387c478bd9Sstevel@tonic-gate static int
6397c478bd9Sstevel@tonic-gate hsfs_getapage(
6407c478bd9Sstevel@tonic-gate 	struct vnode *vp,
6417c478bd9Sstevel@tonic-gate 	u_offset_t off,
6427c478bd9Sstevel@tonic-gate 	size_t len,
6437c478bd9Sstevel@tonic-gate 	uint_t *protp,
6447c478bd9Sstevel@tonic-gate 	struct page *pl[],
6457c478bd9Sstevel@tonic-gate 	size_t plsz,
6467c478bd9Sstevel@tonic-gate 	struct seg *seg,
6477c478bd9Sstevel@tonic-gate 	caddr_t addr,
6487c478bd9Sstevel@tonic-gate 	enum seg_rw rw,
6497c478bd9Sstevel@tonic-gate 	struct cred *cred)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	struct hsnode *hp;
6527c478bd9Sstevel@tonic-gate 	struct hsfs *fsp;
6537c478bd9Sstevel@tonic-gate 	int	err;
6547c478bd9Sstevel@tonic-gate 	struct buf *bufs;
6557c478bd9Sstevel@tonic-gate 	caddr_t *vas;
6567c478bd9Sstevel@tonic-gate 	caddr_t va;
6577c478bd9Sstevel@tonic-gate 	struct page *pp, *searchp, *lastp;
6587c478bd9Sstevel@tonic-gate 	page_t	*pagefound;
6597c478bd9Sstevel@tonic-gate 	offset_t	bof;
6607c478bd9Sstevel@tonic-gate 	struct vnode *devvp;
6617c478bd9Sstevel@tonic-gate 	ulong_t	byte_offset;
6627c478bd9Sstevel@tonic-gate 	size_t	io_len_tmp;
6637c478bd9Sstevel@tonic-gate 	uint_t	io_off, io_len;
6647c478bd9Sstevel@tonic-gate 	uint_t	xlen;
6657c478bd9Sstevel@tonic-gate 	uint_t	filsiz;
6667c478bd9Sstevel@tonic-gate 	uint_t	secsize;
6677c478bd9Sstevel@tonic-gate 	uint_t	bufcnt;
6687c478bd9Sstevel@tonic-gate 	uint_t	bufsused;
6697c478bd9Sstevel@tonic-gate 	uint_t	count;
6707c478bd9Sstevel@tonic-gate 	uint_t	io_end;
6717c478bd9Sstevel@tonic-gate 	uint_t	which_chunk_lbn;
6727c478bd9Sstevel@tonic-gate 	uint_t	offset_lbn;
6737c478bd9Sstevel@tonic-gate 	uint_t	offset_extra;
6747c478bd9Sstevel@tonic-gate 	offset_t	offset_bytes;
6757c478bd9Sstevel@tonic-gate 	uint_t	remaining_bytes;
6767c478bd9Sstevel@tonic-gate 	uint_t	extension;
6777c478bd9Sstevel@tonic-gate 	int	remainder;	/* must be signed */
6787c478bd9Sstevel@tonic-gate 	int	chunk_lbn_count;
6797c478bd9Sstevel@tonic-gate 	int	chunk_data_bytes;
6807c478bd9Sstevel@tonic-gate 	int	xarsiz;
6817c478bd9Sstevel@tonic-gate 	diskaddr_t driver_block;
6827c478bd9Sstevel@tonic-gate 	u_offset_t io_off_tmp;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	/*
6857c478bd9Sstevel@tonic-gate 	 * We don't support asynchronous operation at the moment, so
6867c478bd9Sstevel@tonic-gate 	 * just pretend we did it.  If the pages are ever actually
6877c478bd9Sstevel@tonic-gate 	 * needed, they'll get brought in then.
6887c478bd9Sstevel@tonic-gate 	 */
6897c478bd9Sstevel@tonic-gate 	if (pl == NULL)
6907c478bd9Sstevel@tonic-gate 		return (0);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	hp = VTOH(vp);
6937c478bd9Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vp->v_vfsp);
6947c478bd9Sstevel@tonic-gate 	devvp = fsp->hsfs_devvp;
6957c478bd9Sstevel@tonic-gate 	secsize = fsp->hsfs_vol.lbn_size;  /* bytes per logical block */
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	/* file data size */
6987c478bd9Sstevel@tonic-gate 	filsiz = hp->hs_dirent.ext_size;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/* disk addr for start of file */
7017c478bd9Sstevel@tonic-gate 	bof = LBN_TO_BYTE((offset_t)hp->hs_dirent.ext_lbn, vp->v_vfsp);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/* xarsiz byte must be skipped for data */
7047c478bd9Sstevel@tonic-gate 	xarsiz = hp->hs_dirent.xar_len << fsp->hsfs_vol.lbn_shift;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/* how many logical blocks in an interleave (data+skip) */
7077c478bd9Sstevel@tonic-gate 	chunk_lbn_count = hp->hs_dirent.intlf_sz + hp->hs_dirent.intlf_sk;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	if (chunk_lbn_count == 0) {
7107c478bd9Sstevel@tonic-gate 		chunk_lbn_count = 1;
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	/*
7147c478bd9Sstevel@tonic-gate 	 * Convert interleaving size into bytes.  The zero case
7157c478bd9Sstevel@tonic-gate 	 * (no interleaving) optimization is handled as a side-
7167c478bd9Sstevel@tonic-gate 	 * effect of the read-ahead logic.
7177c478bd9Sstevel@tonic-gate 	 */
7187c478bd9Sstevel@tonic-gate 	if (hp->hs_dirent.intlf_sz == 0) {
7197c478bd9Sstevel@tonic-gate 		chunk_data_bytes = LBN_TO_BYTE(1, vp->v_vfsp);
7207c478bd9Sstevel@tonic-gate 	} else {
7217c478bd9Sstevel@tonic-gate 		chunk_data_bytes = LBN_TO_BYTE(hp->hs_dirent.intlf_sz,
7227c478bd9Sstevel@tonic-gate 			vp->v_vfsp);
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate reread:
7267c478bd9Sstevel@tonic-gate 	err = 0;
7277c478bd9Sstevel@tonic-gate 	pagefound = 0;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	/*
7307c478bd9Sstevel@tonic-gate 	 * Do some read-ahead.  This mostly saves us a bit of
7317c478bd9Sstevel@tonic-gate 	 * system cpu time more than anything else when doing
7327c478bd9Sstevel@tonic-gate 	 * sequential reads.  At some point, could do the
7337c478bd9Sstevel@tonic-gate 	 * read-ahead asynchronously which might gain us something
7347c478bd9Sstevel@tonic-gate 	 * on wall time, but it seems unlikely....
7357c478bd9Sstevel@tonic-gate 	 *
7367c478bd9Sstevel@tonic-gate 	 * We do the easy case here, which is to read through
7377c478bd9Sstevel@tonic-gate 	 * the end of the chunk, minus whatever's at the end that
7387c478bd9Sstevel@tonic-gate 	 * won't exactly fill a page.
7397c478bd9Sstevel@tonic-gate 	 */
7407c478bd9Sstevel@tonic-gate 	which_chunk_lbn = (off + len) / chunk_data_bytes;
7417c478bd9Sstevel@tonic-gate 	extension = ((which_chunk_lbn + 1) * chunk_data_bytes) - off;
7427c478bd9Sstevel@tonic-gate 	extension -= (extension % PAGESIZE);
7437c478bd9Sstevel@tonic-gate 	if (extension != 0 && extension < filsiz - off) {
7447c478bd9Sstevel@tonic-gate 		len = extension;
745*8cd7c4fcSpeterte 	} else {
746*8cd7c4fcSpeterte 		len = PAGESIZE;
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	/*
7497c478bd9Sstevel@tonic-gate 	 * Some cd writers don't write sectors that aren't used.  Also,
7507c478bd9Sstevel@tonic-gate 	 * there's no point in reading sectors we'll never look at.  So,
7517c478bd9Sstevel@tonic-gate 	 * if we're asked to go beyond the end of a file, truncate to the
7527c478bd9Sstevel@tonic-gate 	 * length of that file.
7537c478bd9Sstevel@tonic-gate 	 *
7547c478bd9Sstevel@tonic-gate 	 * Additionally, this behaviour is required by section 6.4.5 of
7557c478bd9Sstevel@tonic-gate 	 * ISO 9660:1988(E).
7567c478bd9Sstevel@tonic-gate 	 */
7577c478bd9Sstevel@tonic-gate 	if (len > (filsiz - off)) {
7587c478bd9Sstevel@tonic-gate 		len = filsiz - off;
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
761*8cd7c4fcSpeterte 	/* A little paranoia. */
762*8cd7c4fcSpeterte 	ASSERT(len > 0);
763*8cd7c4fcSpeterte 
7647c478bd9Sstevel@tonic-gate 	/*
7657c478bd9Sstevel@tonic-gate 	 * After all that, make sure we're asking for things in units
7667c478bd9Sstevel@tonic-gate 	 * that bdev_strategy() will understand (see bug 4202551).
7677c478bd9Sstevel@tonic-gate 	 */
7687c478bd9Sstevel@tonic-gate 	len = roundup(len, DEV_BSIZE);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	pp = NULL;
7717c478bd9Sstevel@tonic-gate again:
7727c478bd9Sstevel@tonic-gate 	/* search for page in buffer */
7737c478bd9Sstevel@tonic-gate 	if ((pagefound = page_exists(vp, off)) == 0) {
7747c478bd9Sstevel@tonic-gate 		/*
7757c478bd9Sstevel@tonic-gate 		 * Need to really do disk IO to get the page.
7767c478bd9Sstevel@tonic-gate 		 */
7777c478bd9Sstevel@tonic-gate 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off_tmp,
7787c478bd9Sstevel@tonic-gate 		    &io_len_tmp, off, len, 0);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 		if (pp == NULL)
7817c478bd9Sstevel@tonic-gate 			goto again;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 		io_off = (uint_t)io_off_tmp;
7847c478bd9Sstevel@tonic-gate 		io_len = (uint_t)io_len_tmp;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 		/* check for truncation */
7877c478bd9Sstevel@tonic-gate 		/*
7887c478bd9Sstevel@tonic-gate 		 * xxx Clean up and return EIO instead?
7897c478bd9Sstevel@tonic-gate 		 * xxx Ought to go to u_offset_t for everything, but we
7907c478bd9Sstevel@tonic-gate 		 * xxx call lots of things that want uint_t arguments.
7917c478bd9Sstevel@tonic-gate 		 */
7927c478bd9Sstevel@tonic-gate 		ASSERT(io_off == io_off_tmp);
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 		/*
7957c478bd9Sstevel@tonic-gate 		 * get enough buffers for worst-case scenario
7967c478bd9Sstevel@tonic-gate 		 * (i.e., no coalescing possible).
7977c478bd9Sstevel@tonic-gate 		 */
7987c478bd9Sstevel@tonic-gate 		bufcnt = (len + secsize - 1) / secsize;
7997c478bd9Sstevel@tonic-gate 		bufs = kmem_zalloc(bufcnt * sizeof (struct buf), KM_SLEEP);
8007c478bd9Sstevel@tonic-gate 		vas = kmem_alloc(bufcnt * sizeof (caddr_t), KM_SLEEP);
8017c478bd9Sstevel@tonic-gate 		for (count = 0; count < bufcnt; count++) {
8027c478bd9Sstevel@tonic-gate 			bufs[count].b_edev = devvp->v_rdev;
8037c478bd9Sstevel@tonic-gate 			bufs[count].b_dev = cmpdev(devvp->v_rdev);
8047c478bd9Sstevel@tonic-gate 			bufs[count].b_flags = B_NOCACHE|B_BUSY|B_READ;
8057c478bd9Sstevel@tonic-gate 			bufs[count].b_iodone = hsfs_iodone;
8067c478bd9Sstevel@tonic-gate 			bufs[count].b_vp = vp;
8077c478bd9Sstevel@tonic-gate 			bufs[count].b_file = vp;
8087c478bd9Sstevel@tonic-gate 			sema_init(&bufs[count].b_io, 0, NULL,
8097c478bd9Sstevel@tonic-gate 			    SEMA_DEFAULT, NULL);
8107c478bd9Sstevel@tonic-gate 			sema_init(&bufs[count].b_sem, 0, NULL,
8117c478bd9Sstevel@tonic-gate 			    SEMA_DEFAULT, NULL);
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 
814*8cd7c4fcSpeterte 		/*
815*8cd7c4fcSpeterte 		 * If our filesize is not an integer multiple of PAGESIZE,
816*8cd7c4fcSpeterte 		 * we zero that part of the last page that's between EOF and
817*8cd7c4fcSpeterte 		 * the PAGESIZE boundary.
818*8cd7c4fcSpeterte 		 */
8197c478bd9Sstevel@tonic-gate 		xlen = io_len & PAGEOFFSET;
8207c478bd9Sstevel@tonic-gate 		if (xlen != 0)
8217c478bd9Sstevel@tonic-gate 			pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		va = NULL;
8247c478bd9Sstevel@tonic-gate 		lastp = NULL;
8257c478bd9Sstevel@tonic-gate 		searchp = pp;
8267c478bd9Sstevel@tonic-gate 		io_end = io_off + io_len;
8277c478bd9Sstevel@tonic-gate 		for (count = 0, byte_offset = io_off;
8287c478bd9Sstevel@tonic-gate 			byte_offset < io_end;
8297c478bd9Sstevel@tonic-gate 			count++) {
8307c478bd9Sstevel@tonic-gate 			ASSERT(count < bufcnt);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 			/* Compute disk address for interleaving. */
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 			/* considered without skips */
8357c478bd9Sstevel@tonic-gate 			which_chunk_lbn = byte_offset / chunk_data_bytes;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 			/* factor in skips */
8387c478bd9Sstevel@tonic-gate 			offset_lbn = which_chunk_lbn * chunk_lbn_count;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 			/* convert to physical byte offset for lbn */
8417c478bd9Sstevel@tonic-gate 			offset_bytes = LBN_TO_BYTE(offset_lbn, vp->v_vfsp);
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 			/* don't forget offset into lbn */
8447c478bd9Sstevel@tonic-gate 			offset_extra = byte_offset % chunk_data_bytes;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 			/* get virtual block number for driver */
8477c478bd9Sstevel@tonic-gate 			driver_block = lbtodb(bof + xarsiz
8487c478bd9Sstevel@tonic-gate 				+ offset_bytes + offset_extra);
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 			if (lastp != searchp) {
8517c478bd9Sstevel@tonic-gate 				/* this branch taken first time through loop */
8527c478bd9Sstevel@tonic-gate 				va = vas[count]
8537c478bd9Sstevel@tonic-gate 					= ppmapin(searchp, PROT_WRITE,
8547c478bd9Sstevel@tonic-gate 						(caddr_t)-1);
8557c478bd9Sstevel@tonic-gate 				/* ppmapin() guarantees not to return NULL */
8567c478bd9Sstevel@tonic-gate 			} else {
8577c478bd9Sstevel@tonic-gate 				vas[count] = NULL;
8587c478bd9Sstevel@tonic-gate 			}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 			bufs[count].b_un.b_addr = va + byte_offset % PAGESIZE;
8617c478bd9Sstevel@tonic-gate 			bufs[count].b_offset =
8627c478bd9Sstevel@tonic-gate 			    (offset_t)(byte_offset - io_off + off);
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 			/*
8657c478bd9Sstevel@tonic-gate 			 * We specifically use the b_lblkno member here
8667c478bd9Sstevel@tonic-gate 			 * as even in the 32 bit world driver_block can
8677c478bd9Sstevel@tonic-gate 			 * get very large in line with the ISO9660 spec.
8687c478bd9Sstevel@tonic-gate 			 */
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 			bufs[count].b_lblkno = driver_block;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 			remaining_bytes = ((which_chunk_lbn + 1)
8737c478bd9Sstevel@tonic-gate 				* chunk_data_bytes)
8747c478bd9Sstevel@tonic-gate 				- byte_offset;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 			/*
8777c478bd9Sstevel@tonic-gate 			 * remaining_bytes can't be zero, as we derived
8787c478bd9Sstevel@tonic-gate 			 * which_chunk_lbn directly from byte_offset.
8797c478bd9Sstevel@tonic-gate 			 */
8807c478bd9Sstevel@tonic-gate 			if ((remaining_bytes+byte_offset) < (off+len)) {
8817c478bd9Sstevel@tonic-gate 				/* coalesce-read the rest of the chunk */
8827c478bd9Sstevel@tonic-gate 				bufs[count].b_bcount = remaining_bytes;
8837c478bd9Sstevel@tonic-gate 			} else {
8847c478bd9Sstevel@tonic-gate 				/* get the final bits */
8857c478bd9Sstevel@tonic-gate 				bufs[count].b_bcount = off + len - byte_offset;
8867c478bd9Sstevel@tonic-gate 			}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 			/*
8897c478bd9Sstevel@tonic-gate 			 * It would be nice to do multiple pages'
8907c478bd9Sstevel@tonic-gate 			 * worth at once here when the opportunity
8917c478bd9Sstevel@tonic-gate 			 * arises, as that has been shown to improve
8927c478bd9Sstevel@tonic-gate 			 * our wall time.  However, to do that
8937c478bd9Sstevel@tonic-gate 			 * requires that we use the pageio subsystem,
8947c478bd9Sstevel@tonic-gate 			 * which doesn't mix well with what we're
8957c478bd9Sstevel@tonic-gate 			 * already using here.  We can't use pageio
8967c478bd9Sstevel@tonic-gate 			 * all the time, because that subsystem
8977c478bd9Sstevel@tonic-gate 			 * assumes that a page is stored in N
8987c478bd9Sstevel@tonic-gate 			 * contiguous blocks on the device.
8997c478bd9Sstevel@tonic-gate 			 * Interleaving violates that assumption.
9007c478bd9Sstevel@tonic-gate 			 */
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 			remainder = PAGESIZE - (byte_offset % PAGESIZE);
9037c478bd9Sstevel@tonic-gate 			if (bufs[count].b_bcount > remainder) {
9047c478bd9Sstevel@tonic-gate 				bufs[count].b_bcount = remainder;
9057c478bd9Sstevel@tonic-gate 			}
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 			bufs[count].b_bufsize = bufs[count].b_bcount;
9087c478bd9Sstevel@tonic-gate 			byte_offset += bufs[count].b_bcount;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(&bufs[count]);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_INBLK, 1);
9137c478bd9Sstevel@tonic-gate 			lastp = searchp;
9147c478bd9Sstevel@tonic-gate 			if ((remainder - bufs[count].b_bcount) < 1) {
9157c478bd9Sstevel@tonic-gate 				searchp = searchp->p_next;
9167c478bd9Sstevel@tonic-gate 			}
9177c478bd9Sstevel@tonic-gate 		}
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 		bufsused = count;
9207c478bd9Sstevel@tonic-gate 		/* Now wait for everything to come in */
9217c478bd9Sstevel@tonic-gate 		for (count = 0; count < bufsused; count++) {
9227c478bd9Sstevel@tonic-gate 			if (err == 0) {
9237c478bd9Sstevel@tonic-gate 				err = biowait(&bufs[count]);
9247c478bd9Sstevel@tonic-gate 			} else
9257c478bd9Sstevel@tonic-gate 				(void) biowait(&bufs[count]);
9267c478bd9Sstevel@tonic-gate 		}
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 		/* Don't leak resources */
9297c478bd9Sstevel@tonic-gate 		for (count = 0; count < bufcnt; count++) {
9307c478bd9Sstevel@tonic-gate 			sema_destroy(&bufs[count].b_io);
9317c478bd9Sstevel@tonic-gate 			sema_destroy(&bufs[count].b_sem);
9327c478bd9Sstevel@tonic-gate 			if (count < bufsused && vas[count] != NULL) {
9337c478bd9Sstevel@tonic-gate 				ppmapout(vas[count]);
9347c478bd9Sstevel@tonic-gate 			}
9357c478bd9Sstevel@tonic-gate 		}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 		kmem_free(vas, bufcnt * sizeof (caddr_t));
9387c478bd9Sstevel@tonic-gate 		kmem_free(bufs, bufcnt * sizeof (struct buf));
9397c478bd9Sstevel@tonic-gate 	}
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	if (err) {
9427c478bd9Sstevel@tonic-gate 		pvn_read_done(pp, B_ERROR);
9437c478bd9Sstevel@tonic-gate 		return (err);
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	/*
9477c478bd9Sstevel@tonic-gate 	 * Lock the requested page, and the one after it if possible.
9487c478bd9Sstevel@tonic-gate 	 * Don't bother if our caller hasn't given us a place to stash
9497c478bd9Sstevel@tonic-gate 	 * the page pointers, since otherwise we'd lock pages that would
9507c478bd9Sstevel@tonic-gate 	 * never get unlocked.
9517c478bd9Sstevel@tonic-gate 	 */
9527c478bd9Sstevel@tonic-gate 	if (pagefound) {
9537c478bd9Sstevel@tonic-gate 		int index;
9547c478bd9Sstevel@tonic-gate 		ulong_t soff;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 		/*
9577c478bd9Sstevel@tonic-gate 		 * Make sure it's in memory before we say it's here.
9587c478bd9Sstevel@tonic-gate 		 */
9597c478bd9Sstevel@tonic-gate 		if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) {
9607c478bd9Sstevel@tonic-gate 			hsfs_lostpage++;
9617c478bd9Sstevel@tonic-gate 			goto reread;
9627c478bd9Sstevel@tonic-gate 		}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 		pl[0] = pp;
9657c478bd9Sstevel@tonic-gate 		index = 1;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		/*
9687c478bd9Sstevel@tonic-gate 		 * Try to lock the next page, if it exists, without
9697c478bd9Sstevel@tonic-gate 		 * blocking.
9707c478bd9Sstevel@tonic-gate 		 */
9717c478bd9Sstevel@tonic-gate 		plsz -= PAGESIZE;
9727c478bd9Sstevel@tonic-gate 		/* LINTED (plsz is unsigned) */
9737c478bd9Sstevel@tonic-gate 		for (soff = off + PAGESIZE; plsz > 0;
9747c478bd9Sstevel@tonic-gate 		    soff += PAGESIZE, plsz -= PAGESIZE) {
9757c478bd9Sstevel@tonic-gate 			pp = page_lookup_nowait(vp, (u_offset_t)soff,
9767c478bd9Sstevel@tonic-gate 					SE_SHARED);
9777c478bd9Sstevel@tonic-gate 			if (pp == NULL)
9787c478bd9Sstevel@tonic-gate 				break;
9797c478bd9Sstevel@tonic-gate 			pl[index++] = pp;
9807c478bd9Sstevel@tonic-gate 		}
9817c478bd9Sstevel@tonic-gate 		pl[index] = NULL;
9827c478bd9Sstevel@tonic-gate 		return (0);
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	if (pp != NULL) {
9867c478bd9Sstevel@tonic-gate 		pvn_plist_init(pp, pl, plsz, off, io_len, rw);
9877c478bd9Sstevel@tonic-gate 	}
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	return (err);
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate static int
9937c478bd9Sstevel@tonic-gate hsfs_getpage(
9947c478bd9Sstevel@tonic-gate 	struct vnode *vp,
9957c478bd9Sstevel@tonic-gate 	offset_t off,
9967c478bd9Sstevel@tonic-gate 	size_t len,
9977c478bd9Sstevel@tonic-gate 	uint_t *protp,
9987c478bd9Sstevel@tonic-gate 	struct page *pl[],
9997c478bd9Sstevel@tonic-gate 	size_t plsz,
10007c478bd9Sstevel@tonic-gate 	struct seg *seg,
10017c478bd9Sstevel@tonic-gate 	caddr_t addr,
10027c478bd9Sstevel@tonic-gate 	enum seg_rw rw,
10037c478bd9Sstevel@tonic-gate 	struct cred *cred)
10047c478bd9Sstevel@tonic-gate {
10057c478bd9Sstevel@tonic-gate 	int err;
10067c478bd9Sstevel@tonic-gate 	uint_t filsiz;
10077c478bd9Sstevel@tonic-gate 	struct hsnode *hp = VTOH(vp);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	/* does not support write */
10107c478bd9Sstevel@tonic-gate 	if (rw == S_WRITE) {
10117c478bd9Sstevel@tonic-gate 		panic("write attempt on READ ONLY HSFS");
10127c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
10167c478bd9Sstevel@tonic-gate 		return (ENOSYS);
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	ASSERT(off <= MAXOFF_T);
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	/*
10227c478bd9Sstevel@tonic-gate 	 * Determine file data size for EOF check.
10237c478bd9Sstevel@tonic-gate 	 */
10247c478bd9Sstevel@tonic-gate 	filsiz = hp->hs_dirent.ext_size;
10257c478bd9Sstevel@tonic-gate 	if ((off + len) > (offset_t)(filsiz + PAGEOFFSET) && seg != segkmap)
10267c478bd9Sstevel@tonic-gate 		return (EFAULT);	/* beyond EOF */
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	if (protp != NULL)
10297c478bd9Sstevel@tonic-gate 		*protp = PROT_ALL;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	if (len <= PAGESIZE)
10327c478bd9Sstevel@tonic-gate 		err = hsfs_getapage(vp, (u_offset_t)off, len, protp, pl, plsz,
10337c478bd9Sstevel@tonic-gate 		    seg, addr, rw, cred);
10347c478bd9Sstevel@tonic-gate 	else
10357c478bd9Sstevel@tonic-gate 		err = pvn_getpages(hsfs_getapage, vp, off, len, protp,
10367c478bd9Sstevel@tonic-gate 		    pl, plsz, seg, addr, rw, cred);
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	return (err);
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate /*
10447c478bd9Sstevel@tonic-gate  * This function should never be called. We need to have it to pass
10457c478bd9Sstevel@tonic-gate  * it as an argument to other functions.
10467c478bd9Sstevel@tonic-gate  */
10477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10487c478bd9Sstevel@tonic-gate int
10497c478bd9Sstevel@tonic-gate hsfs_putapage(
10507c478bd9Sstevel@tonic-gate 	vnode_t		*vp,
10517c478bd9Sstevel@tonic-gate 	page_t		*pp,
10527c478bd9Sstevel@tonic-gate 	u_offset_t	*offp,
10537c478bd9Sstevel@tonic-gate 	size_t		*lenp,
10547c478bd9Sstevel@tonic-gate 	int		flags,
10557c478bd9Sstevel@tonic-gate 	cred_t		*cr)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	/* should never happen - just destroy it */
10587c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "hsfs_putapage: dirty HSFS page");
10597c478bd9Sstevel@tonic-gate 	pvn_write_done(pp, B_ERROR | B_WRITE | B_INVAL | B_FORCE | flags);
10607c478bd9Sstevel@tonic-gate 	return (0);
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate /*
10657c478bd9Sstevel@tonic-gate  * The only flags we support are B_INVAL, B_FREE and B_DONTNEED.
10667c478bd9Sstevel@tonic-gate  * B_INVAL is set by:
10677c478bd9Sstevel@tonic-gate  *
10687c478bd9Sstevel@tonic-gate  *	1) the MC_SYNC command of memcntl(2) to support the MS_INVALIDATE flag.
10697c478bd9Sstevel@tonic-gate  *	2) the MC_ADVISE command of memcntl(2) with the MADV_DONTNEED advice
10707c478bd9Sstevel@tonic-gate  *	   which translates to an MC_SYNC with the MS_INVALIDATE flag.
10717c478bd9Sstevel@tonic-gate  *
10727c478bd9Sstevel@tonic-gate  * The B_FREE (as well as the B_DONTNEED) flag is set when the
10737c478bd9Sstevel@tonic-gate  * MADV_SEQUENTIAL advice has been used. VOP_PUTPAGE is invoked
10747c478bd9Sstevel@tonic-gate  * from SEGVN to release pages behind a pagefault.
10757c478bd9Sstevel@tonic-gate  */
10767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10777c478bd9Sstevel@tonic-gate static int
10787c478bd9Sstevel@tonic-gate hsfs_putpage(
10797c478bd9Sstevel@tonic-gate 	struct vnode	*vp,
10807c478bd9Sstevel@tonic-gate 	offset_t	off,
10817c478bd9Sstevel@tonic-gate 	size_t		len,
10827c478bd9Sstevel@tonic-gate 	int		flags,
10837c478bd9Sstevel@tonic-gate 	struct cred	*cr)
10847c478bd9Sstevel@tonic-gate {
10857c478bd9Sstevel@tonic-gate 	int error = 0;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	if (vp->v_count == 0) {
10887c478bd9Sstevel@tonic-gate 		panic("hsfs_putpage: bad v_count");
10897c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
10937c478bd9Sstevel@tonic-gate 		return (ENOSYS);
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	ASSERT(off <= MAXOFF_T);
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (!vn_has_cached_data(vp))	/* no pages mapped */
10987c478bd9Sstevel@tonic-gate 		return (0);
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	if (len == 0)		/* from 'off' to EOF */
11017c478bd9Sstevel@tonic-gate 		error = pvn_vplist_dirty(vp, off,
11027c478bd9Sstevel@tonic-gate 					hsfs_putapage, flags, cr);
11037c478bd9Sstevel@tonic-gate 	else {
11047c478bd9Sstevel@tonic-gate 		offset_t end_off = off + len;
11057c478bd9Sstevel@tonic-gate 		offset_t file_size = VTOH(vp)->hs_dirent.ext_size;
11067c478bd9Sstevel@tonic-gate 		offset_t io_off;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 		file_size = (file_size + PAGESIZE - 1) & PAGEMASK;
11097c478bd9Sstevel@tonic-gate 		if (end_off > file_size)
11107c478bd9Sstevel@tonic-gate 			end_off = file_size;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 		for (io_off = off; io_off < end_off; io_off += PAGESIZE) {
11137c478bd9Sstevel@tonic-gate 			page_t *pp;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 			/*
11167c478bd9Sstevel@tonic-gate 			 * We insist on getting the page only if we are
11177c478bd9Sstevel@tonic-gate 			 * about to invalidate, free or write it and
11187c478bd9Sstevel@tonic-gate 			 * the B_ASYNC flag is not set.
11197c478bd9Sstevel@tonic-gate 			 */
11207c478bd9Sstevel@tonic-gate 			if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
11217c478bd9Sstevel@tonic-gate 				pp = page_lookup(vp, io_off,
11227c478bd9Sstevel@tonic-gate 					(flags & (B_INVAL | B_FREE)) ?
11237c478bd9Sstevel@tonic-gate 					    SE_EXCL : SE_SHARED);
11247c478bd9Sstevel@tonic-gate 			} else {
11257c478bd9Sstevel@tonic-gate 				pp = page_lookup_nowait(vp, io_off,
11267c478bd9Sstevel@tonic-gate 					(flags & B_FREE) ? SE_EXCL : SE_SHARED);
11277c478bd9Sstevel@tonic-gate 			}
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 			if (pp == NULL)
11307c478bd9Sstevel@tonic-gate 				continue;
11317c478bd9Sstevel@tonic-gate 			/*
11327c478bd9Sstevel@tonic-gate 			 * Normally pvn_getdirty() should return 0, which
11337c478bd9Sstevel@tonic-gate 			 * impies that it has done the job for us.
11347c478bd9Sstevel@tonic-gate 			 * The shouldn't-happen scenario is when it returns 1.
11357c478bd9Sstevel@tonic-gate 			 * This means that the page has been modified and
11367c478bd9Sstevel@tonic-gate 			 * needs to be put back.
11377c478bd9Sstevel@tonic-gate 			 * Since we can't write on a CD, we fake a failed
11387c478bd9Sstevel@tonic-gate 			 * I/O and force pvn_write_done() to destroy the page.
11397c478bd9Sstevel@tonic-gate 			 */
11407c478bd9Sstevel@tonic-gate 			if (pvn_getdirty(pp, flags) == 1) {
11417c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE,
11427c478bd9Sstevel@tonic-gate 					"hsfs_putpage: dirty HSFS page");
11437c478bd9Sstevel@tonic-gate 				pvn_write_done(pp, flags |
11447c478bd9Sstevel@tonic-gate 				    B_ERROR | B_WRITE | B_INVAL | B_FORCE);
11457c478bd9Sstevel@tonic-gate 			}
11467c478bd9Sstevel@tonic-gate 		}
11477c478bd9Sstevel@tonic-gate 	}
11487c478bd9Sstevel@tonic-gate 	return (error);
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11537c478bd9Sstevel@tonic-gate static int
11547c478bd9Sstevel@tonic-gate hsfs_map(
11557c478bd9Sstevel@tonic-gate 	struct vnode *vp,
11567c478bd9Sstevel@tonic-gate 	offset_t off,
11577c478bd9Sstevel@tonic-gate 	struct as *as,
11587c478bd9Sstevel@tonic-gate 	caddr_t *addrp,
11597c478bd9Sstevel@tonic-gate 	size_t len,
11607c478bd9Sstevel@tonic-gate 	uchar_t prot,
11617c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
11627c478bd9Sstevel@tonic-gate 	uint_t flags,
11637c478bd9Sstevel@tonic-gate 	struct cred *cred)
11647c478bd9Sstevel@tonic-gate {
11657c478bd9Sstevel@tonic-gate 	struct segvn_crargs vn_a;
11667c478bd9Sstevel@tonic-gate 	int error;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	/* VFS_RECORD(vp->v_vfsp, VS_MAP, VS_CALL); */
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
11717c478bd9Sstevel@tonic-gate 		return (ENOSYS);
11727c478bd9Sstevel@tonic-gate 
1173cfa55013Speterte 	if (off > MAXOFF_T || off < 0 ||
1174cfa55013Speterte 	    (off + len) < 0 || (off + len) > MAXOFF_T)
1175cfa55013Speterte 		return (ENXIO);
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG) {
11787c478bd9Sstevel@tonic-gate 		return (ENODEV);
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	/*
11827c478bd9Sstevel@tonic-gate 	 * If file is being locked, disallow mapping.
11837c478bd9Sstevel@tonic-gate 	 */
11847c478bd9Sstevel@tonic-gate 	if (vn_has_mandatory_locks(vp, VTOH(vp)->hs_dirent.mode))
11857c478bd9Sstevel@tonic-gate 		return (EAGAIN);
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	as_rangelock(as);
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	if ((flags & MAP_FIXED) == 0) {
11907c478bd9Sstevel@tonic-gate 		map_addr(addrp, len, off, 1, flags);
11917c478bd9Sstevel@tonic-gate 		if (*addrp == NULL) {
11927c478bd9Sstevel@tonic-gate 			as_rangeunlock(as);
11937c478bd9Sstevel@tonic-gate 			return (ENOMEM);
11947c478bd9Sstevel@tonic-gate 		}
11957c478bd9Sstevel@tonic-gate 	} else {
11967c478bd9Sstevel@tonic-gate 		/*
11977c478bd9Sstevel@tonic-gate 		 * User specified address - blow away any previous mappings
11987c478bd9Sstevel@tonic-gate 		 */
11997c478bd9Sstevel@tonic-gate 		(void) as_unmap(as, *addrp, len);
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	vn_a.vp = vp;
12037c478bd9Sstevel@tonic-gate 	vn_a.offset = off;
12047c478bd9Sstevel@tonic-gate 	vn_a.type = flags & MAP_TYPE;
12057c478bd9Sstevel@tonic-gate 	vn_a.prot = prot;
12067c478bd9Sstevel@tonic-gate 	vn_a.maxprot = maxprot;
12077c478bd9Sstevel@tonic-gate 	vn_a.flags = flags & ~MAP_TYPE;
12087c478bd9Sstevel@tonic-gate 	vn_a.cred = cred;
12097c478bd9Sstevel@tonic-gate 	vn_a.amp = NULL;
12107c478bd9Sstevel@tonic-gate 	vn_a.szc = 0;
12117c478bd9Sstevel@tonic-gate 	vn_a.lgrp_mem_policy_flags = 0;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
12147c478bd9Sstevel@tonic-gate 	as_rangeunlock(as);
12157c478bd9Sstevel@tonic-gate 	return (error);
12167c478bd9Sstevel@tonic-gate }
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate /* ARGSUSED */
12197c478bd9Sstevel@tonic-gate static int
12207c478bd9Sstevel@tonic-gate hsfs_addmap(
12217c478bd9Sstevel@tonic-gate 	struct vnode *vp,
12227c478bd9Sstevel@tonic-gate 	offset_t off,
12237c478bd9Sstevel@tonic-gate 	struct as *as,
12247c478bd9Sstevel@tonic-gate 	caddr_t addr,
12257c478bd9Sstevel@tonic-gate 	size_t len,
12267c478bd9Sstevel@tonic-gate 	uchar_t prot,
12277c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
12287c478bd9Sstevel@tonic-gate 	uint_t flags,
12297c478bd9Sstevel@tonic-gate 	struct cred *cr)
12307c478bd9Sstevel@tonic-gate {
12317c478bd9Sstevel@tonic-gate 	struct hsnode *hp;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
12347c478bd9Sstevel@tonic-gate 		return (ENOSYS);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	hp = VTOH(vp);
12377c478bd9Sstevel@tonic-gate 	mutex_enter(&hp->hs_contents_lock);
12387c478bd9Sstevel@tonic-gate 	hp->hs_mapcnt += btopr(len);
12397c478bd9Sstevel@tonic-gate 	mutex_exit(&hp->hs_contents_lock);
12407c478bd9Sstevel@tonic-gate 	return (0);
12417c478bd9Sstevel@tonic-gate }
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12447c478bd9Sstevel@tonic-gate static int
12457c478bd9Sstevel@tonic-gate hsfs_delmap(
12467c478bd9Sstevel@tonic-gate 	struct vnode *vp,
12477c478bd9Sstevel@tonic-gate 	offset_t off,
12487c478bd9Sstevel@tonic-gate 	struct as *as,
12497c478bd9Sstevel@tonic-gate 	caddr_t addr,
12507c478bd9Sstevel@tonic-gate 	size_t len,
12517c478bd9Sstevel@tonic-gate 	uint_t prot,
12527c478bd9Sstevel@tonic-gate 	uint_t maxprot,
12537c478bd9Sstevel@tonic-gate 	uint_t flags,
12547c478bd9Sstevel@tonic-gate 	struct cred *cr)
12557c478bd9Sstevel@tonic-gate {
12567c478bd9Sstevel@tonic-gate 	struct hsnode *hp;
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
12597c478bd9Sstevel@tonic-gate 		return (ENOSYS);
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	hp = VTOH(vp);
12627c478bd9Sstevel@tonic-gate 	mutex_enter(&hp->hs_contents_lock);
12637c478bd9Sstevel@tonic-gate 	hp->hs_mapcnt -= btopr(len);	/* Count released mappings */
12647c478bd9Sstevel@tonic-gate 	ASSERT(hp->hs_mapcnt >= 0);
12657c478bd9Sstevel@tonic-gate 	mutex_exit(&hp->hs_contents_lock);
12667c478bd9Sstevel@tonic-gate 	return (0);
12677c478bd9Sstevel@tonic-gate }
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate /* ARGSUSED */
12707c478bd9Sstevel@tonic-gate static int
12717c478bd9Sstevel@tonic-gate hsfs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp)
12727c478bd9Sstevel@tonic-gate {
12737c478bd9Sstevel@tonic-gate 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
12747c478bd9Sstevel@tonic-gate }
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate /* ARGSUSED */
12777c478bd9Sstevel@tonic-gate static int
12787c478bd9Sstevel@tonic-gate hsfs_frlock(
12797c478bd9Sstevel@tonic-gate 	struct vnode *vp,
12807c478bd9Sstevel@tonic-gate 	int cmd,
12817c478bd9Sstevel@tonic-gate 	struct flock64 *bfp,
12827c478bd9Sstevel@tonic-gate 	int flag,
12837c478bd9Sstevel@tonic-gate 	offset_t offset,
12847c478bd9Sstevel@tonic-gate 	struct flk_callback *flk_cbp,
12857c478bd9Sstevel@tonic-gate 	cred_t *cr)
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate 	struct hsnode *hp = VTOH(vp);
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	/*
12907c478bd9Sstevel@tonic-gate 	 * If the file is being mapped, disallow fs_frlock.
12917c478bd9Sstevel@tonic-gate 	 * We are not holding the hs_contents_lock while checking
12927c478bd9Sstevel@tonic-gate 	 * hs_mapcnt because the current locking strategy drops all
12937c478bd9Sstevel@tonic-gate 	 * locks before calling fs_frlock.
12947c478bd9Sstevel@tonic-gate 	 * So, hs_mapcnt could change before we enter fs_frlock making
12957c478bd9Sstevel@tonic-gate 	 * it meaningless to have held hs_contents_lock in the first place.
12967c478bd9Sstevel@tonic-gate 	 */
12977c478bd9Sstevel@tonic-gate 	if (hp->hs_mapcnt > 0 && MANDLOCK(vp, hp->hs_dirent.mode))
12987c478bd9Sstevel@tonic-gate 		return (EAGAIN);
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr));
13017c478bd9Sstevel@tonic-gate }
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate const fs_operation_def_t hsfs_vnodeops_template[] = {
13047c478bd9Sstevel@tonic-gate 	VOPNAME_OPEN, hsfs_open,
13057c478bd9Sstevel@tonic-gate 	VOPNAME_CLOSE, hsfs_close,
13067c478bd9Sstevel@tonic-gate 	VOPNAME_READ, hsfs_read,
13077c478bd9Sstevel@tonic-gate 	VOPNAME_GETATTR, hsfs_getattr,
13087c478bd9Sstevel@tonic-gate 	VOPNAME_ACCESS, hsfs_access,
13097c478bd9Sstevel@tonic-gate 	VOPNAME_LOOKUP, hsfs_lookup,
13107c478bd9Sstevel@tonic-gate 	VOPNAME_READDIR, hsfs_readdir,
13117c478bd9Sstevel@tonic-gate 	VOPNAME_READLINK, hsfs_readlink,
13127c478bd9Sstevel@tonic-gate 	VOPNAME_FSYNC, hsfs_fsync,
13137c478bd9Sstevel@tonic-gate 	VOPNAME_INACTIVE, (fs_generic_func_p) hsfs_inactive,
13147c478bd9Sstevel@tonic-gate 	VOPNAME_FID, hsfs_fid,
13157c478bd9Sstevel@tonic-gate 	VOPNAME_SEEK, hsfs_seek,
13167c478bd9Sstevel@tonic-gate 	VOPNAME_FRLOCK, hsfs_frlock,
13177c478bd9Sstevel@tonic-gate 	VOPNAME_GETPAGE, hsfs_getpage,
13187c478bd9Sstevel@tonic-gate 	VOPNAME_PUTPAGE, hsfs_putpage,
13197c478bd9Sstevel@tonic-gate 	VOPNAME_MAP, (fs_generic_func_p) hsfs_map,
13207c478bd9Sstevel@tonic-gate 	VOPNAME_ADDMAP, (fs_generic_func_p) hsfs_addmap,
13217c478bd9Sstevel@tonic-gate 	VOPNAME_DELMAP, hsfs_delmap,
13227c478bd9Sstevel@tonic-gate 	NULL, NULL
13237c478bd9Sstevel@tonic-gate };
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate struct vnodeops *hsfs_vnodeops;
1326