xref: /onnv-gate/usr/src/uts/common/fs/hsfs/hsfs_vnops.c (revision 5331:3047ad28a67b)
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
51349Speterte  * Common Development and Distribution License (the "License").
61349Speterte  * 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 /*
223898Srsb  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Vnode operations for the High Sierra filesystem
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/t_lock.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/time.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/resource.h>
390Sstevel@tonic-gate #include <sys/signal.h>
400Sstevel@tonic-gate #include <sys/cred.h>
410Sstevel@tonic-gate #include <sys/user.h>
420Sstevel@tonic-gate #include <sys/buf.h>
430Sstevel@tonic-gate #include <sys/vfs.h>
443898Srsb #include <sys/vfs_opreg.h>
450Sstevel@tonic-gate #include <sys/stat.h>
460Sstevel@tonic-gate #include <sys/vnode.h>
470Sstevel@tonic-gate #include <sys/mode.h>
480Sstevel@tonic-gate #include <sys/proc.h>
490Sstevel@tonic-gate #include <sys/disp.h>
500Sstevel@tonic-gate #include <sys/file.h>
510Sstevel@tonic-gate #include <sys/fcntl.h>
520Sstevel@tonic-gate #include <sys/flock.h>
530Sstevel@tonic-gate #include <sys/kmem.h>
540Sstevel@tonic-gate #include <sys/uio.h>
550Sstevel@tonic-gate #include <sys/conf.h>
560Sstevel@tonic-gate #include <sys/errno.h>
570Sstevel@tonic-gate #include <sys/mman.h>
580Sstevel@tonic-gate #include <sys/pathname.h>
590Sstevel@tonic-gate #include <sys/debug.h>
600Sstevel@tonic-gate #include <sys/vmsystm.h>
610Sstevel@tonic-gate #include <sys/cmn_err.h>
620Sstevel@tonic-gate #include <sys/fbuf.h>
630Sstevel@tonic-gate #include <sys/dirent.h>
640Sstevel@tonic-gate #include <sys/errno.h>
655312Smg147109 #include <sys/dkio.h>
665312Smg147109 #include <sys/cmn_err.h>
675312Smg147109 #include <sys/atomic.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <vm/hat.h>
700Sstevel@tonic-gate #include <vm/page.h>
710Sstevel@tonic-gate #include <vm/pvn.h>
720Sstevel@tonic-gate #include <vm/as.h>
730Sstevel@tonic-gate #include <vm/seg.h>
740Sstevel@tonic-gate #include <vm/seg_map.h>
750Sstevel@tonic-gate #include <vm/seg_kmem.h>
760Sstevel@tonic-gate #include <vm/seg_vn.h>
770Sstevel@tonic-gate #include <vm/rm.h>
780Sstevel@tonic-gate #include <vm/page.h>
790Sstevel@tonic-gate #include <sys/swap.h>
805312Smg147109 #include <sys/avl.h>
815312Smg147109 #include <sys/sunldi.h>
825312Smg147109 #include <sys/ddi.h>
835312Smg147109 #include <sys/sunddi.h>
845312Smg147109 #include <sys/sdt.h>
855312Smg147109 
865312Smg147109 /*
875312Smg147109  * For struct modlinkage
885312Smg147109  */
895312Smg147109 #include <sys/modctl.h>
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #include <sys/fs/hsfs_spec.h>
920Sstevel@tonic-gate #include <sys/fs/hsfs_node.h>
930Sstevel@tonic-gate #include <sys/fs/hsfs_impl.h>
940Sstevel@tonic-gate #include <sys/fs/hsfs_susp.h>
950Sstevel@tonic-gate #include <sys/fs/hsfs_rrip.h>
960Sstevel@tonic-gate 
970Sstevel@tonic-gate #include <fs/fs_subr.h>
980Sstevel@tonic-gate 
995312Smg147109 /* # of contiguous requests to detect sequential access pattern */
1005312Smg147109 static int seq_contig_requests = 2;
1015312Smg147109 
1025312Smg147109 /*
1035312Smg147109  * This is the max number os taskq threads that will be created
1045312Smg147109  * if required. Since we are using a Dynamic TaskQ by default only
1055312Smg147109  * one thread is created initially.
1065312Smg147109  *
1075312Smg147109  * NOTE: In the usual hsfs use case this per fs instance number
1085312Smg147109  * of taskq threads should not place any undue load on a system.
1095312Smg147109  * Even on an unusual system with say 100 CDROM drives, 800 threads
1105312Smg147109  * will not be created unless all the drives are loaded and all
1115312Smg147109  * of them are saturated with I/O at the same time! If there is at
1125312Smg147109  * all a complaint of system load due to such an unusual case it
1135312Smg147109  * should be easy enough to change to one per-machine Dynamic TaskQ
1145312Smg147109  * for all hsfs mounts with a nthreads of say 32.
1155312Smg147109  */
1165312Smg147109 static int hsfs_taskq_nthreads = 8;	/* # of taskq threads per fs */
1175312Smg147109 
1185312Smg147109 /* Min count of adjacent bufs that will avoid buf coalescing */
1195312Smg147109 static int hsched_coalesce_min = 2;
1205312Smg147109 
1215312Smg147109 /*
1225312Smg147109  * Kmem caches for heavily used small allocations. Using these kmem
1235312Smg147109  * caches provides a factor of 3 reduction in system time and greatly
1245312Smg147109  * aids overall throughput esp. on SPARC.
1255312Smg147109  */
1265312Smg147109 struct kmem_cache *hio_cache;
1275312Smg147109 struct kmem_cache *hio_info_cache;
1285312Smg147109 
1294866Sfrankho /*
1304866Sfrankho  * This tunable allows us to ignore inode numbers from rrip-1.12.
1314866Sfrankho  * In this case, we fall back to our default inode algorithm.
1324866Sfrankho  */
1334866Sfrankho extern int use_rrip_inodes;
1344866Sfrankho 
1355312Smg147109 /*
1365312Smg147109  * Free behind logic from UFS to tame our thirst for
1375312Smg147109  * the page cache.
1385312Smg147109  * See usr/src/uts/common/fs/ufs/ufs_vnops.c for more
1395312Smg147109  * explanation.
1405312Smg147109  */
1415312Smg147109 static int	freebehind = 1;
1425312Smg147109 static int	smallfile = 0;
1435312Smg147109 static int	cache_read_ahead = 0;
1445312Smg147109 static u_offset_t smallfile64 = 32 * 1024;
1455312Smg147109 #define	SMALLFILE1_D 1000
1465312Smg147109 #define	SMALLFILE2_D 10
1475312Smg147109 static u_offset_t smallfile1 = 32 * 1024;
1485312Smg147109 static u_offset_t smallfile2 = 32 * 1024;
1495312Smg147109 static clock_t smallfile_update = 0; /* when to recompute */
1505312Smg147109 static uint_t smallfile1_d = SMALLFILE1_D;
1515312Smg147109 static uint_t smallfile2_d = SMALLFILE2_D;
1525312Smg147109 
1535312Smg147109 static int hsched_deadline_compare(const void *x1, const void *x2);
1545312Smg147109 static int hsched_offset_compare(const void *x1, const void *x2);
1555312Smg147109 static void hsched_enqueue_io(struct hsfs *fsp, struct hio *hsio, int ra);
1565312Smg147109 int hsched_invoke_strategy(struct hsfs *fsp);
1574866Sfrankho 
1580Sstevel@tonic-gate /* ARGSUSED */
1590Sstevel@tonic-gate static int
160*5331Samw hsfs_fsync(vnode_t *cp,
161*5331Samw 	int syncflag,
162*5331Samw 	cred_t *cred,
163*5331Samw 	caller_context_t *ct)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	return (0);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*ARGSUSED*/
1700Sstevel@tonic-gate static int
171*5331Samw hsfs_read(struct vnode *vp,
172*5331Samw 	struct uio *uiop,
173*5331Samw 	int ioflag,
174*5331Samw 	struct cred *cred,
1750Sstevel@tonic-gate 	struct caller_context *ct)
1760Sstevel@tonic-gate {
177206Speterte 	caddr_t base;
178206Speterte 	offset_t diff;
179206Speterte 	int error;
1800Sstevel@tonic-gate 	struct hsnode *hp;
181206Speterte 	uint_t filesize;
1825312Smg147109 	int dofree;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	hp = VTOH(vp);
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * if vp is of type VDIR, make sure dirent
1870Sstevel@tonic-gate 	 * is filled up with all info (because of ptbl)
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
1900Sstevel@tonic-gate 		if (hp->hs_dirent.ext_size == 0)
1910Sstevel@tonic-gate 			hs_filldirent(vp, &hp->hs_dirent);
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 	filesize = hp->hs_dirent.ext_size;
1940Sstevel@tonic-gate 
195206Speterte 	/* Sanity checks. */
196206Speterte 	if (uiop->uio_resid == 0 ||		/* No data wanted. */
1971349Speterte 	    uiop->uio_loffset > HS_MAXFILEOFF ||	/* Offset too big. */
198206Speterte 	    uiop->uio_loffset >= filesize)	/* Past EOF. */
199206Speterte 		return (0);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	do {
202206Speterte 		/*
203206Speterte 		 * We want to ask for only the "right" amount of data.
204206Speterte 		 * In this case that means:-
205206Speterte 		 *
206206Speterte 		 * We can't get data from beyond our EOF. If asked,
207206Speterte 		 * we will give a short read.
208206Speterte 		 *
209206Speterte 		 * segmap_getmapflt returns buffers of MAXBSIZE bytes.
210206Speterte 		 * These buffers are always MAXBSIZE aligned.
211206Speterte 		 * If our starting offset is not MAXBSIZE aligned,
212206Speterte 		 * we can only ask for less than MAXBSIZE bytes.
213206Speterte 		 *
214206Speterte 		 * If our requested offset and length are such that
215206Speterte 		 * they belong in different MAXBSIZE aligned slots
216206Speterte 		 * then we'll be making more than one call on
217206Speterte 		 * segmap_getmapflt.
218206Speterte 		 *
219206Speterte 		 * This diagram shows the variables we use and their
220206Speterte 		 * relationships.
221206Speterte 		 *
222206Speterte 		 * |<-----MAXBSIZE----->|
223206Speterte 		 * +--------------------------...+
224206Speterte 		 * |.....mapon->|<--n-->|....*...|EOF
225206Speterte 		 * +--------------------------...+
226206Speterte 		 * uio_loffset->|
227206Speterte 		 * uio_resid....|<---------->|
228206Speterte 		 * diff.........|<-------------->|
229206Speterte 		 *
230206Speterte 		 * So, in this case our offset is not aligned
231206Speterte 		 * and our request takes us outside of the
232206Speterte 		 * MAXBSIZE window. We will break this up into
233206Speterte 		 * two segmap_getmapflt calls.
234206Speterte 		 */
235206Speterte 		size_t nbytes;
236206Speterte 		offset_t mapon;
237206Speterte 		size_t n;
238206Speterte 		uint_t flags;
2390Sstevel@tonic-gate 
240206Speterte 		mapon = uiop->uio_loffset & MAXBOFFSET;
241206Speterte 		diff = filesize - uiop->uio_loffset;
242206Speterte 		nbytes = (size_t)MIN(MAXBSIZE - mapon, uiop->uio_resid);
243206Speterte 		n = MIN(diff, nbytes);
244206Speterte 		if (n <= 0) {
245206Speterte 			/* EOF or request satisfied. */
246206Speterte 			return (0);
2470Sstevel@tonic-gate 		}
2480Sstevel@tonic-gate 
2495312Smg147109 		/*
2505312Smg147109 		 * Freebehind computation taken from:
2515312Smg147109 		 * usr/src/uts/common/fs/ufs/ufs_vnops.c
2525312Smg147109 		 */
2535312Smg147109 		if (drv_hztousec(ddi_get_lbolt()) >= smallfile_update) {
2545312Smg147109 			uint64_t percpufreeb;
2555312Smg147109 			if (smallfile1_d == 0) smallfile1_d = SMALLFILE1_D;
2565312Smg147109 			if (smallfile2_d == 0) smallfile2_d = SMALLFILE2_D;
2575312Smg147109 			percpufreeb = ptob((uint64_t)freemem) / ncpus_online;
2585312Smg147109 			smallfile1 = percpufreeb / smallfile1_d;
2595312Smg147109 			smallfile2 = percpufreeb / smallfile2_d;
2605312Smg147109 			smallfile1 = MAX(smallfile1, smallfile);
2615312Smg147109 			smallfile1 = MAX(smallfile1, smallfile64);
2625312Smg147109 			smallfile2 = MAX(smallfile1, smallfile2);
2635312Smg147109 			smallfile_update = drv_hztousec(ddi_get_lbolt())
2645312Smg147109 			    + 1000000;
2655312Smg147109 		}
2665312Smg147109 
2675312Smg147109 		dofree = freebehind &&
2685312Smg147109 		    hp->hs_prev_offset == uiop->uio_loffset &&
2695312Smg147109 		    hp->hs_ra_bytes > 0;
2705312Smg147109 
271206Speterte 		base = segmap_getmapflt(segkmap, vp,
272206Speterte 		    (u_offset_t)uiop->uio_loffset, n, 1, S_READ);
2730Sstevel@tonic-gate 
274206Speterte 		error = uiomove(base + mapon, n, UIO_READ, uiop);
275206Speterte 
2760Sstevel@tonic-gate 		if (error == 0) {
2770Sstevel@tonic-gate 			/*
2780Sstevel@tonic-gate 			 * if read a whole block, or read to eof,
2790Sstevel@tonic-gate 			 *  won't need this buffer again soon.
2800Sstevel@tonic-gate 			 */
281206Speterte 			if (n + mapon == MAXBSIZE ||
282206Speterte 			    uiop->uio_loffset == filesize)
2830Sstevel@tonic-gate 				flags = SM_DONTNEED;
2840Sstevel@tonic-gate 			else
2850Sstevel@tonic-gate 				flags = 0;
2865312Smg147109 
2875312Smg147109 			if (dofree) {
2885312Smg147109 				flags = SM_FREE | SM_ASYNC;
2895312Smg147109 				if ((cache_read_ahead == 0) &&
2905312Smg147109 				    uiop->uio_loffset > smallfile2)
2915312Smg147109 					flags |=  SM_DONTNEED;
2925312Smg147109 			}
2935312Smg147109 
2940Sstevel@tonic-gate 			error = segmap_release(segkmap, base, flags);
2950Sstevel@tonic-gate 		} else
2960Sstevel@tonic-gate 			(void) segmap_release(segkmap, base, 0);
2970Sstevel@tonic-gate 	} while (error == 0 && uiop->uio_resid > 0);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	return (error);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /*ARGSUSED2*/
3030Sstevel@tonic-gate static int
3040Sstevel@tonic-gate hsfs_getattr(
3050Sstevel@tonic-gate 	struct vnode *vp,
3060Sstevel@tonic-gate 	struct vattr *vap,
3070Sstevel@tonic-gate 	int flags,
308*5331Samw 	struct cred *cred,
309*5331Samw 	caller_context_t *ct)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	struct hsnode *hp;
3120Sstevel@tonic-gate 	struct vfs *vfsp;
3130Sstevel@tonic-gate 	struct hsfs *fsp;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	hp = VTOH(vp);
3160Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vp->v_vfsp);
3170Sstevel@tonic-gate 	vfsp = vp->v_vfsp;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	if ((hp->hs_dirent.ext_size == 0) && (vp->v_type == VDIR)) {
3200Sstevel@tonic-gate 		hs_filldirent(vp, &hp->hs_dirent);
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 	vap->va_type = IFTOVT(hp->hs_dirent.mode);
3230Sstevel@tonic-gate 	vap->va_mode = hp->hs_dirent.mode;
3240Sstevel@tonic-gate 	vap->va_uid = hp->hs_dirent.uid;
3250Sstevel@tonic-gate 	vap->va_gid = hp->hs_dirent.gid;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	vap->va_fsid = vfsp->vfs_dev;
3280Sstevel@tonic-gate 	vap->va_nodeid = (ino64_t)hp->hs_nodeid;
3290Sstevel@tonic-gate 	vap->va_nlink = hp->hs_dirent.nlink;
3300Sstevel@tonic-gate 	vap->va_size =	(offset_t)hp->hs_dirent.ext_size;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	vap->va_atime.tv_sec = hp->hs_dirent.adate.tv_sec;
3330Sstevel@tonic-gate 	vap->va_atime.tv_nsec = hp->hs_dirent.adate.tv_usec*1000;
3340Sstevel@tonic-gate 	vap->va_mtime.tv_sec = hp->hs_dirent.mdate.tv_sec;
3350Sstevel@tonic-gate 	vap->va_mtime.tv_nsec = hp->hs_dirent.mdate.tv_usec*1000;
3360Sstevel@tonic-gate 	vap->va_ctime.tv_sec = hp->hs_dirent.cdate.tv_sec;
3370Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = hp->hs_dirent.cdate.tv_usec*1000;
3380Sstevel@tonic-gate 	if (vp->v_type == VCHR || vp->v_type == VBLK)
3390Sstevel@tonic-gate 		vap->va_rdev = hp->hs_dirent.r_dev;
3400Sstevel@tonic-gate 	else
3410Sstevel@tonic-gate 		vap->va_rdev = 0;
3420Sstevel@tonic-gate 	vap->va_blksize = vfsp->vfs_bsize;
3430Sstevel@tonic-gate 	/* no. of blocks = no. of data blocks + no. of xar blocks */
3440Sstevel@tonic-gate 	vap->va_nblocks = (fsblkcnt64_t)howmany(vap->va_size + (u_longlong_t)
3450Sstevel@tonic-gate 	    (hp->hs_dirent.xar_len << fsp->hsfs_vol.lbn_shift), DEV_BSIZE);
3460Sstevel@tonic-gate 	vap->va_seq = hp->hs_seq;
3470Sstevel@tonic-gate 	return (0);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate /*ARGSUSED*/
3510Sstevel@tonic-gate static int
352*5331Samw hsfs_readlink(struct vnode *vp,
353*5331Samw 	struct uio *uiop,
354*5331Samw 	struct cred *cred,
355*5331Samw 	caller_context_t *ct)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	struct hsnode *hp;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (vp->v_type != VLNK)
3600Sstevel@tonic-gate 		return (EINVAL);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	hp = VTOH(vp);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	if (hp->hs_dirent.sym_link == (char *)NULL)
3650Sstevel@tonic-gate 		return (ENOENT);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	return (uiomove(hp->hs_dirent.sym_link,
3680Sstevel@tonic-gate 	    (size_t)MIN(hp->hs_dirent.ext_size,
3690Sstevel@tonic-gate 	    uiop->uio_resid), UIO_READ, uiop));
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate /*ARGSUSED*/
3730Sstevel@tonic-gate static void
374*5331Samw hsfs_inactive(struct vnode *vp,
375*5331Samw 	struct cred *cred,
376*5331Samw 	caller_context_t *ct)
3770Sstevel@tonic-gate {
3780Sstevel@tonic-gate 	struct hsnode *hp;
3790Sstevel@tonic-gate 	struct hsfs *fsp;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	int nopage;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	hp = VTOH(vp);
3840Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vp->v_vfsp);
3850Sstevel@tonic-gate 	/*
3860Sstevel@tonic-gate 	 * Note: acquiring and holding v_lock for quite a while
3870Sstevel@tonic-gate 	 * here serializes on the vnode; this is unfortunate, but
3880Sstevel@tonic-gate 	 * likely not to overly impact performance, as the underlying
3890Sstevel@tonic-gate 	 * device (CDROM drive) is quite slow.
3900Sstevel@tonic-gate 	 */
3910Sstevel@tonic-gate 	rw_enter(&fsp->hsfs_hash_lock, RW_WRITER);
3920Sstevel@tonic-gate 	mutex_enter(&hp->hs_contents_lock);
3930Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	if (vp->v_count < 1) {
3960Sstevel@tonic-gate 		panic("hsfs_inactive: v_count < 1");
3970Sstevel@tonic-gate 		/*NOTREACHED*/
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	if (vp->v_count > 1 || (hp->hs_flags & HREF) == 0) {
4010Sstevel@tonic-gate 		vp->v_count--;	/* release hold from vn_rele */
4020Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
4030Sstevel@tonic-gate 		mutex_exit(&hp->hs_contents_lock);
4040Sstevel@tonic-gate 		rw_exit(&fsp->hsfs_hash_lock);
4050Sstevel@tonic-gate 		return;
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 	vp->v_count--;	/* release hold from vn_rele */
4080Sstevel@tonic-gate 	if (vp->v_count == 0) {
4090Sstevel@tonic-gate 		/*
4100Sstevel@tonic-gate 		 * Free the hsnode.
4110Sstevel@tonic-gate 		 * If there are no pages associated with the
4120Sstevel@tonic-gate 		 * hsnode, give it back to the kmem_cache,
4130Sstevel@tonic-gate 		 * else put at the end of this file system's
4140Sstevel@tonic-gate 		 * internal free list.
4150Sstevel@tonic-gate 		 */
4160Sstevel@tonic-gate 		nopage = !vn_has_cached_data(vp);
4170Sstevel@tonic-gate 		hp->hs_flags = 0;
4180Sstevel@tonic-gate 		/*
4190Sstevel@tonic-gate 		 * exit these locks now, since hs_freenode may
4200Sstevel@tonic-gate 		 * kmem_free the hsnode and embedded vnode
4210Sstevel@tonic-gate 		 */
4220Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
4230Sstevel@tonic-gate 		mutex_exit(&hp->hs_contents_lock);
4240Sstevel@tonic-gate 		hs_freenode(vp, fsp, nopage);
4250Sstevel@tonic-gate 	} else {
4260Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
4270Sstevel@tonic-gate 		mutex_exit(&hp->hs_contents_lock);
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 	rw_exit(&fsp->hsfs_hash_lock);
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate /*ARGSUSED*/
4340Sstevel@tonic-gate static int
4350Sstevel@tonic-gate hsfs_lookup(
4360Sstevel@tonic-gate 	struct vnode *dvp,
4370Sstevel@tonic-gate 	char *nm,
4380Sstevel@tonic-gate 	struct vnode **vpp,
4390Sstevel@tonic-gate 	struct pathname *pnp,
4400Sstevel@tonic-gate 	int flags,
4410Sstevel@tonic-gate 	struct vnode *rdir,
442*5331Samw 	struct cred *cred,
443*5331Samw 	caller_context_t *ct,
444*5331Samw 	int *direntflags,
445*5331Samw 	pathname_t *realpnp)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate 	int error;
4480Sstevel@tonic-gate 	int namelen = (int)strlen(nm);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	if (*nm == '\0') {
4510Sstevel@tonic-gate 		VN_HOLD(dvp);
4520Sstevel@tonic-gate 		*vpp = dvp;
4530Sstevel@tonic-gate 		return (0);
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	/*
4570Sstevel@tonic-gate 	 * If we're looking for ourself, life is simple.
4580Sstevel@tonic-gate 	 */
4590Sstevel@tonic-gate 	if (namelen == 1 && *nm == '.') {
4600Sstevel@tonic-gate 		if (error = hs_access(dvp, (mode_t)VEXEC, cred))
4610Sstevel@tonic-gate 			return (error);
4620Sstevel@tonic-gate 		VN_HOLD(dvp);
4630Sstevel@tonic-gate 		*vpp = dvp;
4640Sstevel@tonic-gate 		return (0);
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	return (hs_dirlook(dvp, nm, namelen, vpp, cred));
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate /*ARGSUSED*/
4720Sstevel@tonic-gate static int
4730Sstevel@tonic-gate hsfs_readdir(
474*5331Samw 	struct vnode		*vp,
475*5331Samw 	struct uio		*uiop,
476*5331Samw 	struct cred		*cred,
477*5331Samw 	int			*eofp,
478*5331Samw 	caller_context_t	*ct,
479*5331Samw 	int			flags)
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate 	struct hsnode	*dhp;
4820Sstevel@tonic-gate 	struct hsfs	*fsp;
4830Sstevel@tonic-gate 	struct hs_direntry hd;
4840Sstevel@tonic-gate 	struct dirent64	*nd;
4850Sstevel@tonic-gate 	int		error;
4860Sstevel@tonic-gate 	uint_t		offset;		/* real offset in directory */
4870Sstevel@tonic-gate 	uint_t		dirsiz;		/* real size of directory */
4880Sstevel@tonic-gate 	uchar_t		*blkp;
4890Sstevel@tonic-gate 	int		hdlen;		/* length of hs directory entry */
4900Sstevel@tonic-gate 	long		ndlen;		/* length of dirent entry */
4910Sstevel@tonic-gate 	int		bytes_wanted;
4920Sstevel@tonic-gate 	size_t		bufsize;	/* size of dirent buffer */
4930Sstevel@tonic-gate 	char		*outbuf;	/* ptr to dirent buffer */
4940Sstevel@tonic-gate 	char		*dname;
4950Sstevel@tonic-gate 	int		dnamelen;
4960Sstevel@tonic-gate 	size_t		dname_size;
4970Sstevel@tonic-gate 	struct fbuf	*fbp;
4980Sstevel@tonic-gate 	uint_t		last_offset;	/* last index into current dir block */
4990Sstevel@tonic-gate 	ino64_t		dirino;	/* temporary storage before storing in dirent */
5000Sstevel@tonic-gate 	off_t		diroff;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	dhp = VTOH(vp);
5030Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vp->v_vfsp);
5040Sstevel@tonic-gate 	if (dhp->hs_dirent.ext_size == 0)
5050Sstevel@tonic-gate 		hs_filldirent(vp, &dhp->hs_dirent);
5060Sstevel@tonic-gate 	dirsiz = dhp->hs_dirent.ext_size;
5070Sstevel@tonic-gate 	if (uiop->uio_loffset >= dirsiz) {	/* at or beyond EOF */
5080Sstevel@tonic-gate 		if (eofp)
5090Sstevel@tonic-gate 			*eofp = 1;
5100Sstevel@tonic-gate 		return (0);
5110Sstevel@tonic-gate 	}
5121349Speterte 	ASSERT(uiop->uio_loffset <= HS_MAXFILEOFF);
5131349Speterte 	offset = uiop->uio_loffset;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	dname_size = fsp->hsfs_namemax + 1;	/* 1 for the ending NUL */
5160Sstevel@tonic-gate 	dname = kmem_alloc(dname_size, KM_SLEEP);
5170Sstevel@tonic-gate 	bufsize = uiop->uio_resid + sizeof (struct dirent64);
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	outbuf = kmem_alloc(bufsize, KM_SLEEP);
5200Sstevel@tonic-gate 	nd = (struct dirent64 *)outbuf;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	while (offset < dirsiz) {
523494Sfrankho 		bytes_wanted = MIN(MAXBSIZE, dirsiz - (offset & MAXBMASK));
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		error = fbread(vp, (offset_t)(offset & MAXBMASK),
5264866Sfrankho 		    (unsigned int)bytes_wanted, S_READ, &fbp);
5270Sstevel@tonic-gate 		if (error)
5280Sstevel@tonic-gate 			goto done;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 		blkp = (uchar_t *)fbp->fb_addr;
531494Sfrankho 		last_offset = (offset & MAXBMASK) + fbp->fb_count;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate #define	rel_offset(offset) ((offset) & MAXBOFFSET)	/* index into blkp */
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 		while (offset < last_offset) {
5360Sstevel@tonic-gate 			/*
537494Sfrankho 			 * Very similar validation code is found in
538494Sfrankho 			 * process_dirblock(), hsfs_node.c.
539494Sfrankho 			 * For an explanation, see there.
540494Sfrankho 			 * It may make sense for the future to
541494Sfrankho 			 * "consolidate" the code in hs_parsedir(),
542494Sfrankho 			 * process_dirblock() and hsfs_readdir() into
543494Sfrankho 			 * a single utility function.
5440Sstevel@tonic-gate 			 */
5450Sstevel@tonic-gate 			hdlen = (int)((uchar_t)
5464866Sfrankho 			    HDE_DIR_LEN(&blkp[rel_offset(offset)]));
547494Sfrankho 			if (hdlen < HDE_ROOT_DIR_REC_SIZE ||
548494Sfrankho 			    offset + hdlen > last_offset) {
5490Sstevel@tonic-gate 				/*
550494Sfrankho 				 * advance to next sector boundary
5510Sstevel@tonic-gate 				 */
552494Sfrankho 				offset = roundup(offset + 1, HS_SECTOR_SIZE);
553494Sfrankho 				if (hdlen)
554494Sfrankho 					hs_log_bogus_disk_warning(fsp,
555494Sfrankho 					    HSFS_ERR_TRAILING_JUNK, 0);
556494Sfrankho 
557494Sfrankho 				continue;
5580Sstevel@tonic-gate 			}
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 			bzero(&hd, sizeof (hd));
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 			/*
5630Sstevel@tonic-gate 			 * Just ignore invalid directory entries.
5640Sstevel@tonic-gate 			 * XXX - maybe hs_parsedir() will detect EXISTENCE bit
5650Sstevel@tonic-gate 			 */
5660Sstevel@tonic-gate 			if (!hs_parsedir(fsp, &blkp[rel_offset(offset)],
5674866Sfrankho 			    &hd, dname, &dnamelen, last_offset - offset)) {
5680Sstevel@tonic-gate 				/*
5690Sstevel@tonic-gate 				 * Determine if there is enough room
5700Sstevel@tonic-gate 				 */
5710Sstevel@tonic-gate 				ndlen = (long)DIRENT64_RECLEN((dnamelen));
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 				if ((ndlen + ((char *)nd - outbuf)) >
5740Sstevel@tonic-gate 				    uiop->uio_resid) {
5750Sstevel@tonic-gate 					fbrelse(fbp, S_READ);
5760Sstevel@tonic-gate 					goto done; /* output buffer full */
5770Sstevel@tonic-gate 				}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 				diroff = offset + hdlen;
5800Sstevel@tonic-gate 				/*
5814866Sfrankho 				 * If the media carries rrip-v1.12 or newer,
5824866Sfrankho 				 * and we trust the inodes from the rrip data
5834866Sfrankho 				 * (use_rrip_inodes != 0), use that data. If the
5844866Sfrankho 				 * media has been created by a recent mkisofs
5854866Sfrankho 				 * version, we may trust all numbers in the
5864866Sfrankho 				 * starting extent number; otherwise, we cannot
5874866Sfrankho 				 * do this for zero sized files and symlinks,
5884866Sfrankho 				 * because if we did we'd end up mapping all of
5894866Sfrankho 				 * them to the same node. We use HS_DUMMY_INO
5904866Sfrankho 				 * in this case and make sure that we will not
5914866Sfrankho 				 * map all files to the same meta data.
5920Sstevel@tonic-gate 				 */
5934866Sfrankho 				if (hd.inode != 0 && use_rrip_inodes) {
5944866Sfrankho 					dirino = hd.inode;
5954866Sfrankho 				} else if ((hd.ext_size == 0 ||
5964866Sfrankho 				    hd.sym_link != (char *)NULL) &&
5974866Sfrankho 				    (fsp->hsfs_flags & HSFSMNT_INODE) == 0) {
5984866Sfrankho 					dirino = HS_DUMMY_INO;
5990Sstevel@tonic-gate 				} else {
6004866Sfrankho 					dirino = hd.ext_lbn;
6010Sstevel@tonic-gate 				}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 				/* strncpy(9f) will zero uninitialized bytes */
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 				ASSERT(strlen(dname) + 1 <=
6060Sstevel@tonic-gate 				    DIRENT64_NAMELEN(ndlen));
6070Sstevel@tonic-gate 				(void) strncpy(nd->d_name, dname,
6080Sstevel@tonic-gate 				    DIRENT64_NAMELEN(ndlen));
6090Sstevel@tonic-gate 				nd->d_reclen = (ushort_t)ndlen;
6100Sstevel@tonic-gate 				nd->d_off = (offset_t)diroff;
6110Sstevel@tonic-gate 				nd->d_ino = dirino;
6120Sstevel@tonic-gate 				nd = (struct dirent64 *)((char *)nd + ndlen);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 				/*
6150Sstevel@tonic-gate 				 * free up space allocated for symlink
6160Sstevel@tonic-gate 				 */
6170Sstevel@tonic-gate 				if (hd.sym_link != (char *)NULL) {
6180Sstevel@tonic-gate 					kmem_free(hd.sym_link,
6190Sstevel@tonic-gate 					    (size_t)(hd.ext_size+1));
6200Sstevel@tonic-gate 					hd.sym_link = (char *)NULL;
6210Sstevel@tonic-gate 				}
6220Sstevel@tonic-gate 			}
6230Sstevel@tonic-gate 			offset += hdlen;
6240Sstevel@tonic-gate 		}
6250Sstevel@tonic-gate 		fbrelse(fbp, S_READ);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	/*
6290Sstevel@tonic-gate 	 * Got here for one of the following reasons:
6300Sstevel@tonic-gate 	 *	1) outbuf is full (error == 0)
6310Sstevel@tonic-gate 	 *	2) end of directory reached (error == 0)
6320Sstevel@tonic-gate 	 *	3) error reading directory sector (error != 0)
6330Sstevel@tonic-gate 	 *	4) directory entry crosses sector boundary (error == 0)
6340Sstevel@tonic-gate 	 *
6350Sstevel@tonic-gate 	 * If any directory entries have been copied, don't report
6360Sstevel@tonic-gate 	 * case 4.  Instead, return the valid directory entries.
6370Sstevel@tonic-gate 	 *
6380Sstevel@tonic-gate 	 * If no entries have been copied, report the error.
6390Sstevel@tonic-gate 	 * If case 4, this will be indistiguishable from EOF.
6400Sstevel@tonic-gate 	 */
6410Sstevel@tonic-gate done:
6420Sstevel@tonic-gate 	ndlen = ((char *)nd - outbuf);
6430Sstevel@tonic-gate 	if (ndlen != 0) {
6440Sstevel@tonic-gate 		error = uiomove(outbuf, (size_t)ndlen, UIO_READ, uiop);
6451349Speterte 		uiop->uio_loffset = offset;
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 	kmem_free(dname, dname_size);
6480Sstevel@tonic-gate 	kmem_free(outbuf, bufsize);
6490Sstevel@tonic-gate 	if (eofp && error == 0)
6501349Speterte 		*eofp = (uiop->uio_loffset >= dirsiz);
6510Sstevel@tonic-gate 	return (error);
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate 
654*5331Samw /*ARGSUSED2*/
6550Sstevel@tonic-gate static int
656*5331Samw hsfs_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate 	struct hsnode *hp;
6590Sstevel@tonic-gate 	struct hsfid *fid;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	if (fidp->fid_len < (sizeof (*fid) - sizeof (fid->hf_len))) {
6620Sstevel@tonic-gate 		fidp->fid_len = sizeof (*fid) - sizeof (fid->hf_len);
6630Sstevel@tonic-gate 		return (ENOSPC);
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	fid = (struct hsfid *)fidp;
6670Sstevel@tonic-gate 	fid->hf_len = sizeof (*fid) - sizeof (fid->hf_len);
6680Sstevel@tonic-gate 	hp = VTOH(vp);
6690Sstevel@tonic-gate 	mutex_enter(&hp->hs_contents_lock);
6700Sstevel@tonic-gate 	fid->hf_dir_lbn = hp->hs_dir_lbn;
6710Sstevel@tonic-gate 	fid->hf_dir_off = (ushort_t)hp->hs_dir_off;
6724866Sfrankho 	fid->hf_ino = hp->hs_nodeid;
6730Sstevel@tonic-gate 	mutex_exit(&hp->hs_contents_lock);
6740Sstevel@tonic-gate 	return (0);
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate /*ARGSUSED*/
6780Sstevel@tonic-gate static int
679*5331Samw hsfs_open(struct vnode **vpp,
680*5331Samw 	int flag,
681*5331Samw 	struct cred *cred,
682*5331Samw 	caller_context_t *ct)
6830Sstevel@tonic-gate {
6840Sstevel@tonic-gate 	return (0);
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate /*ARGSUSED*/
6880Sstevel@tonic-gate static int
6890Sstevel@tonic-gate hsfs_close(
6900Sstevel@tonic-gate 	struct vnode *vp,
6910Sstevel@tonic-gate 	int flag,
6920Sstevel@tonic-gate 	int count,
6930Sstevel@tonic-gate 	offset_t offset,
694*5331Samw 	struct cred *cred,
695*5331Samw 	caller_context_t *ct)
6960Sstevel@tonic-gate {
6970Sstevel@tonic-gate 	(void) cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
6980Sstevel@tonic-gate 	cleanshares(vp, ttoproc(curthread)->p_pid);
6990Sstevel@tonic-gate 	return (0);
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate /*ARGSUSED2*/
7030Sstevel@tonic-gate static int
704*5331Samw hsfs_access(struct vnode *vp,
705*5331Samw 	int mode,
706*5331Samw 	int flags,
707*5331Samw 	cred_t *cred,
708*5331Samw 	caller_context_t *ct)
7090Sstevel@tonic-gate {
7100Sstevel@tonic-gate 	return (hs_access(vp, (mode_t)mode, cred));
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate /*
7140Sstevel@tonic-gate  * the seek time of a CD-ROM is very slow, and data transfer
7150Sstevel@tonic-gate  * rate is even worse (max. 150K per sec).  The design
7160Sstevel@tonic-gate  * decision is to reduce access to cd-rom as much as possible,
7170Sstevel@tonic-gate  * and to transfer a sizable block (read-ahead) of data at a time.
7180Sstevel@tonic-gate  * UFS style of read ahead one block at a time is not appropriate,
7190Sstevel@tonic-gate  * and is not supported
7200Sstevel@tonic-gate  */
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate /*
7230Sstevel@tonic-gate  * KLUSTSIZE should be a multiple of PAGESIZE and <= MAXPHYS.
7240Sstevel@tonic-gate  */
7250Sstevel@tonic-gate #define	KLUSTSIZE	(56 * 1024)
7260Sstevel@tonic-gate /* we don't support read ahead */
7270Sstevel@tonic-gate int hsfs_lostpage;	/* no. of times we lost original page */
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate /*
7300Sstevel@tonic-gate  * Used to prevent biodone() from releasing buf resources that
7310Sstevel@tonic-gate  * we didn't allocate in quite the usual way.
7320Sstevel@tonic-gate  */
7330Sstevel@tonic-gate /*ARGSUSED*/
7340Sstevel@tonic-gate int
7350Sstevel@tonic-gate hsfs_iodone(struct buf *bp)
7360Sstevel@tonic-gate {
7370Sstevel@tonic-gate 	sema_v(&bp->b_io);
7380Sstevel@tonic-gate 	return (0);
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate /*
7425312Smg147109  * The taskq thread that invokes the scheduling function to ensure
7435312Smg147109  * that all readaheads are complete and cleans up the associated
7445312Smg147109  * memory and releases the page lock.
7455312Smg147109  */
7465312Smg147109 void
7475312Smg147109 hsfs_ra_task(void *arg)
7485312Smg147109 {
7495312Smg147109 	struct hio_info *info = arg;
7505312Smg147109 	uint_t count;
7515312Smg147109 	struct buf *wbuf;
7525312Smg147109 
7535312Smg147109 	ASSERT(info->pp != NULL);
7545312Smg147109 
7555312Smg147109 	for (count = 0; count < info->bufsused; count++) {
7565312Smg147109 		wbuf = &(info->bufs[count]);
7575312Smg147109 
7585312Smg147109 		DTRACE_PROBE1(hsfs_io_wait_ra, struct buf *, wbuf);
7595312Smg147109 		while (sema_tryp(&(info->sema[count])) == 0) {
7605312Smg147109 			if (hsched_invoke_strategy(info->fsp)) {
7615312Smg147109 				sema_p(&(info->sema[count]));
7625312Smg147109 				break;
7635312Smg147109 			}
7645312Smg147109 		}
7655312Smg147109 		sema_destroy(&(info->sema[count]));
7665312Smg147109 		DTRACE_PROBE1(hsfs_io_done_ra, struct buf *, wbuf);
7675312Smg147109 		biofini(&(info->bufs[count]));
7685312Smg147109 	}
7695312Smg147109 	for (count = 0; count < info->bufsused; count++) {
7705312Smg147109 		if (info->vas[count] != NULL) {
7715312Smg147109 			ppmapout(info->vas[count]);
7725312Smg147109 		}
7735312Smg147109 	}
7745312Smg147109 	kmem_free(info->vas, info->bufcnt * sizeof (caddr_t));
7755312Smg147109 	kmem_free(info->bufs, info->bufcnt * sizeof (struct buf));
7765312Smg147109 	kmem_free(info->sema, info->bufcnt * sizeof (ksema_t));
7775312Smg147109 
7785312Smg147109 	pvn_read_done(info->pp, 0);
7795312Smg147109 	kmem_cache_free(hio_info_cache, info);
7805312Smg147109 }
7815312Smg147109 
7825312Smg147109 /*
7835312Smg147109  * Submit asynchronous readahead requests to the I/O scheduler
7845312Smg147109  * depending on the number of pages to read ahead. These requests
7855312Smg147109  * are asynchronous to the calling thread but I/O requests issued
7865312Smg147109  * subsequently by other threads with higher LBNs must wait for
7875312Smg147109  * these readaheads to complete since we have a single ordered
7885312Smg147109  * I/O pipeline. Thus these readaheads are semi-asynchronous.
7895312Smg147109  * A TaskQ handles waiting for the readaheads to complete.
7905312Smg147109  *
7915312Smg147109  * This function is mostly a copy of hsfs_getapage but somewhat
7925312Smg147109  * simpler. A readahead request is aborted if page allocation
7935312Smg147109  * fails.
7945312Smg147109  */
7955312Smg147109 /*ARGSUSED*/
7965312Smg147109 static int
7975312Smg147109 hsfs_getpage_ra(
7985312Smg147109 	struct vnode *vp,
7995312Smg147109 	u_offset_t off,
8005312Smg147109 	struct seg *seg,
8015312Smg147109 	caddr_t addr,
8025312Smg147109 	struct hsnode *hp,
8035312Smg147109 	struct hsfs *fsp,
8045312Smg147109 	int	xarsiz,
8055312Smg147109 	offset_t	bof,
8065312Smg147109 	int	chunk_lbn_count,
8075312Smg147109 	int	chunk_data_bytes)
8085312Smg147109 {
8095312Smg147109 	struct buf *bufs;
8105312Smg147109 	caddr_t *vas;
8115312Smg147109 	caddr_t va;
8125312Smg147109 	struct page *pp, *searchp, *lastp;
8135312Smg147109 	struct vnode *devvp;
8145312Smg147109 	ulong_t	byte_offset;
8155312Smg147109 	size_t	io_len_tmp;
8165312Smg147109 	uint_t	io_off, io_len;
8175312Smg147109 	uint_t	xlen;
8185312Smg147109 	uint_t	filsiz;
8195312Smg147109 	uint_t	secsize;
8205312Smg147109 	uint_t	bufcnt;
8215312Smg147109 	uint_t	bufsused;
8225312Smg147109 	uint_t	count;
8235312Smg147109 	uint_t	io_end;
8245312Smg147109 	uint_t	which_chunk_lbn;
8255312Smg147109 	uint_t	offset_lbn;
8265312Smg147109 	uint_t	offset_extra;
8275312Smg147109 	offset_t	offset_bytes;
8285312Smg147109 	uint_t	remaining_bytes;
8295312Smg147109 	uint_t	extension;
8305312Smg147109 	int	remainder;	/* must be signed */
8315312Smg147109 	diskaddr_t driver_block;
8325312Smg147109 	u_offset_t io_off_tmp;
8335312Smg147109 	ksema_t	*fio_done;
8345312Smg147109 	struct hio_info *info;
8355312Smg147109 	size_t len;
8365312Smg147109 
8375312Smg147109 	ASSERT(fsp->hqueue != NULL);
8385312Smg147109 
8395312Smg147109 	if (addr >= seg->s_base + seg->s_size) {
8405312Smg147109 		return (-1);
8415312Smg147109 	}
8425312Smg147109 
8435312Smg147109 	devvp = fsp->hsfs_devvp;
8445312Smg147109 	secsize = fsp->hsfs_vol.lbn_size;  /* bytes per logical block */
8455312Smg147109 
8465312Smg147109 	/* file data size */
8475312Smg147109 	filsiz = hp->hs_dirent.ext_size;
8485312Smg147109 
8495312Smg147109 	if (off >= filsiz)
8505312Smg147109 		return (0);
8515312Smg147109 
8525312Smg147109 	extension = 0;
8535312Smg147109 	pp = NULL;
8545312Smg147109 
8555312Smg147109 	extension += hp->hs_ra_bytes;
8565312Smg147109 
8575312Smg147109 	/*
8585312Smg147109 	 * Some cd writers don't write sectors that aren't used.  Also,
8595312Smg147109 	 * there's no point in reading sectors we'll never look at.  So,
8605312Smg147109 	 * if we're asked to go beyond the end of a file, truncate to the
8615312Smg147109 	 * length of that file.
8625312Smg147109 	 *
8635312Smg147109 	 * Additionally, this behaviour is required by section 6.4.5 of
8645312Smg147109 	 * ISO 9660:1988(E).
8655312Smg147109 	 */
8665312Smg147109 	len = MIN(extension ? extension : PAGESIZE, filsiz - off);
8675312Smg147109 
8685312Smg147109 	/* A little paranoia */
8695312Smg147109 	if (len <= 0)
8705312Smg147109 		return (-1);
8715312Smg147109 
8725312Smg147109 	/*
8735312Smg147109 	 * After all that, make sure we're asking for things in units
8745312Smg147109 	 * that bdev_strategy() will understand (see bug 4202551).
8755312Smg147109 	 */
8765312Smg147109 	len = roundup(len, DEV_BSIZE);
8775312Smg147109 
8785312Smg147109 	pp = pvn_read_kluster(vp, off, seg, addr, &io_off_tmp,
8795312Smg147109 	    &io_len_tmp, off, len, 1);
8805312Smg147109 
8815312Smg147109 	if (pp == NULL) {
8825312Smg147109 		hp->hs_num_contig = 0;
8835312Smg147109 		hp->hs_ra_bytes = 0;
8845312Smg147109 		hp->hs_prev_offset = 0;
8855312Smg147109 		return (-1);
8865312Smg147109 	}
8875312Smg147109 
8885312Smg147109 	io_off = (uint_t)io_off_tmp;
8895312Smg147109 	io_len = (uint_t)io_len_tmp;
8905312Smg147109 
8915312Smg147109 	/* check for truncation */
8925312Smg147109 	/*
8935312Smg147109 	 * xxx Clean up and return EIO instead?
8945312Smg147109 	 * xxx Ought to go to u_offset_t for everything, but we
8955312Smg147109 	 * xxx call lots of things that want uint_t arguments.
8965312Smg147109 	 */
8975312Smg147109 	ASSERT(io_off == io_off_tmp);
8985312Smg147109 
8995312Smg147109 	/*
9005312Smg147109 	 * get enough buffers for worst-case scenario
9015312Smg147109 	 * (i.e., no coalescing possible).
9025312Smg147109 	 */
9035312Smg147109 	bufcnt = (len + secsize - 1) / secsize;
9045312Smg147109 	bufs = kmem_alloc(bufcnt * sizeof (struct buf), KM_SLEEP);
9055312Smg147109 	vas = kmem_alloc(bufcnt * sizeof (caddr_t), KM_SLEEP);
9065312Smg147109 
9075312Smg147109 	/*
9085312Smg147109 	 * Allocate a array of semaphores since we are doing I/O
9095312Smg147109 	 * scheduling.
9105312Smg147109 	 */
9115312Smg147109 	fio_done = kmem_alloc(bufcnt * sizeof (ksema_t), KM_SLEEP);
9125312Smg147109 
9135312Smg147109 	/*
9145312Smg147109 	 * If our filesize is not an integer multiple of PAGESIZE,
9155312Smg147109 	 * we zero that part of the last page that's between EOF and
9165312Smg147109 	 * the PAGESIZE boundary.
9175312Smg147109 	 */
9185312Smg147109 	xlen = io_len & PAGEOFFSET;
9195312Smg147109 	if (xlen != 0)
9205312Smg147109 		pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
9215312Smg147109 
9225312Smg147109 	DTRACE_PROBE2(hsfs_readahead, struct vnode *, vp, uint_t, io_len);
9235312Smg147109 
9245312Smg147109 	va = NULL;
9255312Smg147109 	lastp = NULL;
9265312Smg147109 	searchp = pp;
9275312Smg147109 	io_end = io_off + io_len;
9285312Smg147109 	for (count = 0, byte_offset = io_off;
9295312Smg147109 	    byte_offset < io_end;
9305312Smg147109 	    count++) {
9315312Smg147109 		ASSERT(count < bufcnt);
9325312Smg147109 
9335312Smg147109 		bioinit(&bufs[count]);
9345312Smg147109 		bufs[count].b_edev = devvp->v_rdev;
9355312Smg147109 		bufs[count].b_dev = cmpdev(devvp->v_rdev);
9365312Smg147109 		bufs[count].b_flags = B_NOCACHE|B_BUSY|B_READ;
9375312Smg147109 		bufs[count].b_iodone = hsfs_iodone;
9385312Smg147109 		bufs[count].b_vp = vp;
9395312Smg147109 		bufs[count].b_file = vp;
9405312Smg147109 
9415312Smg147109 		/* Compute disk address for interleaving. */
9425312Smg147109 
9435312Smg147109 		/* considered without skips */
9445312Smg147109 		which_chunk_lbn = byte_offset / chunk_data_bytes;
9455312Smg147109 
9465312Smg147109 		/* factor in skips */
9475312Smg147109 		offset_lbn = which_chunk_lbn * chunk_lbn_count;
9485312Smg147109 
9495312Smg147109 		/* convert to physical byte offset for lbn */
9505312Smg147109 		offset_bytes = LBN_TO_BYTE(offset_lbn, vp->v_vfsp);
9515312Smg147109 
9525312Smg147109 		/* don't forget offset into lbn */
9535312Smg147109 		offset_extra = byte_offset % chunk_data_bytes;
9545312Smg147109 
9555312Smg147109 		/* get virtual block number for driver */
9565312Smg147109 		driver_block = lbtodb(bof + xarsiz
9575312Smg147109 		    + offset_bytes + offset_extra);
9585312Smg147109 
9595312Smg147109 		if (lastp != searchp) {
9605312Smg147109 			/* this branch taken first time through loop */
9615312Smg147109 			va = vas[count] = ppmapin(searchp, PROT_WRITE,
9625312Smg147109 			    (caddr_t)-1);
9635312Smg147109 			/* ppmapin() guarantees not to return NULL */
9645312Smg147109 		} else {
9655312Smg147109 			vas[count] = NULL;
9665312Smg147109 		}
9675312Smg147109 
9685312Smg147109 		bufs[count].b_un.b_addr = va + byte_offset % PAGESIZE;
9695312Smg147109 		bufs[count].b_offset =
9705312Smg147109 		    (offset_t)(byte_offset - io_off + off);
9715312Smg147109 
9725312Smg147109 		/*
9735312Smg147109 		 * We specifically use the b_lblkno member here
9745312Smg147109 		 * as even in the 32 bit world driver_block can
9755312Smg147109 		 * get very large in line with the ISO9660 spec.
9765312Smg147109 		 */
9775312Smg147109 
9785312Smg147109 		bufs[count].b_lblkno = driver_block;
9795312Smg147109 
9805312Smg147109 		remaining_bytes = ((which_chunk_lbn + 1) * chunk_data_bytes)
9815312Smg147109 		    - byte_offset;
9825312Smg147109 
9835312Smg147109 		/*
9845312Smg147109 		 * remaining_bytes can't be zero, as we derived
9855312Smg147109 		 * which_chunk_lbn directly from byte_offset.
9865312Smg147109 		 */
9875312Smg147109 		if ((remaining_bytes + byte_offset) < (off + len)) {
9885312Smg147109 			/* coalesce-read the rest of the chunk */
9895312Smg147109 			bufs[count].b_bcount = remaining_bytes;
9905312Smg147109 		} else {
9915312Smg147109 			/* get the final bits */
9925312Smg147109 			bufs[count].b_bcount = off + len - byte_offset;
9935312Smg147109 		}
9945312Smg147109 
9955312Smg147109 		remainder = PAGESIZE - (byte_offset % PAGESIZE);
9965312Smg147109 		if (bufs[count].b_bcount > remainder) {
9975312Smg147109 			bufs[count].b_bcount = remainder;
9985312Smg147109 		}
9995312Smg147109 
10005312Smg147109 		bufs[count].b_bufsize = bufs[count].b_bcount;
10015312Smg147109 		if (((offset_t)byte_offset + bufs[count].b_bcount) >
10025312Smg147109 		    HS_MAXFILEOFF) {
10035312Smg147109 			break;
10045312Smg147109 		}
10055312Smg147109 		byte_offset += bufs[count].b_bcount;
10065312Smg147109 
10075312Smg147109 		/*
10085312Smg147109 		 * We are scheduling I/O so we need to enqueue
10095312Smg147109 		 * requests rather than calling bdev_strategy
10105312Smg147109 		 * here. A later invocation of the scheduling
10115312Smg147109 		 * function will take care of doing the actual
10125312Smg147109 		 * I/O as it selects requests from the queue as
10135312Smg147109 		 * per the scheduling logic.
10145312Smg147109 		 */
10155312Smg147109 		struct hio *hsio = kmem_cache_alloc(hio_cache,
10165312Smg147109 		    KM_SLEEP);
10175312Smg147109 
10185312Smg147109 		sema_init(&fio_done[count], 0, NULL,
10195312Smg147109 		    SEMA_DEFAULT, NULL);
10205312Smg147109 		hsio->bp = &bufs[count];
10215312Smg147109 		hsio->sema = &fio_done[count];
10225312Smg147109 		hsio->io_lblkno = bufs[count].b_lblkno;
10235312Smg147109 		hsio->nblocks = howmany(hsio->bp->b_bcount,
10245312Smg147109 		    DEV_BSIZE);
10255312Smg147109 
10265312Smg147109 		/* used for deadline */
10275312Smg147109 		hsio->io_timestamp = drv_hztousec(ddi_get_lbolt());
10285312Smg147109 
10295312Smg147109 		/* for I/O coalescing */
10305312Smg147109 		hsio->contig_chain = NULL;
10315312Smg147109 		hsched_enqueue_io(fsp, hsio, 1);
10325312Smg147109 
10335312Smg147109 		lwp_stat_update(LWP_STAT_INBLK, 1);
10345312Smg147109 		lastp = searchp;
10355312Smg147109 		if ((remainder - bufs[count].b_bcount) < 1) {
10365312Smg147109 			searchp = searchp->p_next;
10375312Smg147109 		}
10385312Smg147109 	}
10395312Smg147109 
10405312Smg147109 	bufsused = count;
10415312Smg147109 	info = kmem_cache_alloc(hio_info_cache, KM_SLEEP);
10425312Smg147109 	info->bufs = bufs;
10435312Smg147109 	info->vas = vas;
10445312Smg147109 	info->sema = fio_done;
10455312Smg147109 	info->bufsused = bufsused;
10465312Smg147109 	info->bufcnt = bufcnt;
10475312Smg147109 	info->fsp = fsp;
10485312Smg147109 	info->pp = pp;
10495312Smg147109 
10505312Smg147109 	(void) taskq_dispatch(fsp->hqueue->ra_task,
10515312Smg147109 	    hsfs_ra_task, info, KM_SLEEP);
10525312Smg147109 	/*
10535312Smg147109 	 * The I/O locked pages are unlocked in our taskq thread.
10545312Smg147109 	 */
10555312Smg147109 	return (0);
10565312Smg147109 }
10575312Smg147109 
10585312Smg147109 /*
10590Sstevel@tonic-gate  * Each file may have a different interleaving on disk.  This makes
10600Sstevel@tonic-gate  * things somewhat interesting.  The gist is that there are some
10610Sstevel@tonic-gate  * number of contiguous data sectors, followed by some other number
10620Sstevel@tonic-gate  * of contiguous skip sectors.  The sum of those two sets of sectors
10630Sstevel@tonic-gate  * defines the interleave size.  Unfortunately, it means that we generally
10640Sstevel@tonic-gate  * can't simply read N sectors starting at a given offset to satisfy
10650Sstevel@tonic-gate  * any given request.
10660Sstevel@tonic-gate  *
10670Sstevel@tonic-gate  * What we do is get the relevant memory pages via pvn_read_kluster(),
10680Sstevel@tonic-gate  * then stride through the interleaves, setting up a buf for each
10690Sstevel@tonic-gate  * sector that needs to be brought in.  Instead of kmem_alloc'ing
10700Sstevel@tonic-gate  * space for the sectors, though, we just point at the appropriate
10710Sstevel@tonic-gate  * spot in the relevant page for each of them.  This saves us a bunch
10720Sstevel@tonic-gate  * of copying.
10735312Smg147109  *
10745312Smg147109  * NOTICE: The code below in hsfs_getapage is mostly same as the code
10755312Smg147109  *         in hsfs_getpage_ra above (with some omissions). If you are
10765312Smg147109  *         making any change to this function, please also look at
10775312Smg147109  *         hsfs_getpage_ra.
10780Sstevel@tonic-gate  */
10790Sstevel@tonic-gate /*ARGSUSED*/
10800Sstevel@tonic-gate static int
10810Sstevel@tonic-gate hsfs_getapage(
10820Sstevel@tonic-gate 	struct vnode *vp,
10830Sstevel@tonic-gate 	u_offset_t off,
10840Sstevel@tonic-gate 	size_t len,
10850Sstevel@tonic-gate 	uint_t *protp,
10860Sstevel@tonic-gate 	struct page *pl[],
10870Sstevel@tonic-gate 	size_t plsz,
10880Sstevel@tonic-gate 	struct seg *seg,
10890Sstevel@tonic-gate 	caddr_t addr,
10900Sstevel@tonic-gate 	enum seg_rw rw,
10910Sstevel@tonic-gate 	struct cred *cred)
10920Sstevel@tonic-gate {
10930Sstevel@tonic-gate 	struct hsnode *hp;
10940Sstevel@tonic-gate 	struct hsfs *fsp;
10950Sstevel@tonic-gate 	int	err;
10960Sstevel@tonic-gate 	struct buf *bufs;
10970Sstevel@tonic-gate 	caddr_t *vas;
10980Sstevel@tonic-gate 	caddr_t va;
10990Sstevel@tonic-gate 	struct page *pp, *searchp, *lastp;
11000Sstevel@tonic-gate 	page_t	*pagefound;
11010Sstevel@tonic-gate 	offset_t	bof;
11020Sstevel@tonic-gate 	struct vnode *devvp;
11030Sstevel@tonic-gate 	ulong_t	byte_offset;
11040Sstevel@tonic-gate 	size_t	io_len_tmp;
11050Sstevel@tonic-gate 	uint_t	io_off, io_len;
11060Sstevel@tonic-gate 	uint_t	xlen;
11070Sstevel@tonic-gate 	uint_t	filsiz;
11080Sstevel@tonic-gate 	uint_t	secsize;
11090Sstevel@tonic-gate 	uint_t	bufcnt;
11100Sstevel@tonic-gate 	uint_t	bufsused;
11110Sstevel@tonic-gate 	uint_t	count;
11120Sstevel@tonic-gate 	uint_t	io_end;
11130Sstevel@tonic-gate 	uint_t	which_chunk_lbn;
11140Sstevel@tonic-gate 	uint_t	offset_lbn;
11150Sstevel@tonic-gate 	uint_t	offset_extra;
11160Sstevel@tonic-gate 	offset_t	offset_bytes;
11170Sstevel@tonic-gate 	uint_t	remaining_bytes;
11180Sstevel@tonic-gate 	uint_t	extension;
11190Sstevel@tonic-gate 	int	remainder;	/* must be signed */
11200Sstevel@tonic-gate 	int	chunk_lbn_count;
11210Sstevel@tonic-gate 	int	chunk_data_bytes;
11220Sstevel@tonic-gate 	int	xarsiz;
11230Sstevel@tonic-gate 	diskaddr_t driver_block;
11240Sstevel@tonic-gate 	u_offset_t io_off_tmp;
11255312Smg147109 	ksema_t *fio_done;
11265312Smg147109 	int	calcdone;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	/*
11290Sstevel@tonic-gate 	 * We don't support asynchronous operation at the moment, so
11300Sstevel@tonic-gate 	 * just pretend we did it.  If the pages are ever actually
11310Sstevel@tonic-gate 	 * needed, they'll get brought in then.
11320Sstevel@tonic-gate 	 */
11330Sstevel@tonic-gate 	if (pl == NULL)
11340Sstevel@tonic-gate 		return (0);
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	hp = VTOH(vp);
11370Sstevel@tonic-gate 	fsp = VFS_TO_HSFS(vp->v_vfsp);
11380Sstevel@tonic-gate 	devvp = fsp->hsfs_devvp;
11390Sstevel@tonic-gate 	secsize = fsp->hsfs_vol.lbn_size;  /* bytes per logical block */
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	/* file data size */
11420Sstevel@tonic-gate 	filsiz = hp->hs_dirent.ext_size;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	/* disk addr for start of file */
11450Sstevel@tonic-gate 	bof = LBN_TO_BYTE((offset_t)hp->hs_dirent.ext_lbn, vp->v_vfsp);
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	/* xarsiz byte must be skipped for data */
11480Sstevel@tonic-gate 	xarsiz = hp->hs_dirent.xar_len << fsp->hsfs_vol.lbn_shift;
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	/* how many logical blocks in an interleave (data+skip) */
11510Sstevel@tonic-gate 	chunk_lbn_count = hp->hs_dirent.intlf_sz + hp->hs_dirent.intlf_sk;
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	if (chunk_lbn_count == 0) {
11540Sstevel@tonic-gate 		chunk_lbn_count = 1;
11550Sstevel@tonic-gate 	}
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	/*
11580Sstevel@tonic-gate 	 * Convert interleaving size into bytes.  The zero case
11590Sstevel@tonic-gate 	 * (no interleaving) optimization is handled as a side-
11600Sstevel@tonic-gate 	 * effect of the read-ahead logic.
11610Sstevel@tonic-gate 	 */
11620Sstevel@tonic-gate 	if (hp->hs_dirent.intlf_sz == 0) {
11630Sstevel@tonic-gate 		chunk_data_bytes = LBN_TO_BYTE(1, vp->v_vfsp);
11645312Smg147109 		/*
11655312Smg147109 		 * Optimization: If our pagesize is a multiple of LBN
11665312Smg147109 		 * bytes, we can avoid breaking up a page into individual
11675312Smg147109 		 * lbn-sized requests.
11685312Smg147109 		 */
11695312Smg147109 		if (PAGESIZE % chunk_data_bytes == 0) {
11705312Smg147109 			chunk_lbn_count = BYTE_TO_LBN(PAGESIZE, vp->v_vfsp);
11715312Smg147109 			chunk_data_bytes = PAGESIZE;
11725312Smg147109 		}
11730Sstevel@tonic-gate 	} else {
11744866Sfrankho 		chunk_data_bytes =
11754866Sfrankho 		    LBN_TO_BYTE(hp->hs_dirent.intlf_sz, vp->v_vfsp);
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate reread:
11790Sstevel@tonic-gate 	err = 0;
11800Sstevel@tonic-gate 	pagefound = 0;
11815312Smg147109 	calcdone = 0;
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 	/*
11840Sstevel@tonic-gate 	 * Do some read-ahead.  This mostly saves us a bit of
11850Sstevel@tonic-gate 	 * system cpu time more than anything else when doing
11860Sstevel@tonic-gate 	 * sequential reads.  At some point, could do the
11870Sstevel@tonic-gate 	 * read-ahead asynchronously which might gain us something
11880Sstevel@tonic-gate 	 * on wall time, but it seems unlikely....
11890Sstevel@tonic-gate 	 *
11900Sstevel@tonic-gate 	 * We do the easy case here, which is to read through
11910Sstevel@tonic-gate 	 * the end of the chunk, minus whatever's at the end that
11920Sstevel@tonic-gate 	 * won't exactly fill a page.
11930Sstevel@tonic-gate 	 */
11945312Smg147109 	if (hp->hs_ra_bytes > 0 && chunk_data_bytes != PAGESIZE) {
11955312Smg147109 		which_chunk_lbn = (off + len) / chunk_data_bytes;
11965312Smg147109 		extension = ((which_chunk_lbn + 1) * chunk_data_bytes) - off;
11975312Smg147109 		extension -= (extension % PAGESIZE);
1198206Speterte 	} else {
11995312Smg147109 		extension = roundup(len, PAGESIZE);
12000Sstevel@tonic-gate 	}
12010Sstevel@tonic-gate 
12025312Smg147109 	atomic_inc_64(&fsp->total_pages_requested);
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	pp = NULL;
12050Sstevel@tonic-gate again:
12060Sstevel@tonic-gate 	/* search for page in buffer */
12070Sstevel@tonic-gate 	if ((pagefound = page_exists(vp, off)) == 0) {
12080Sstevel@tonic-gate 		/*
12090Sstevel@tonic-gate 		 * Need to really do disk IO to get the page.
12100Sstevel@tonic-gate 		 */
12115312Smg147109 		if (!calcdone) {
12125312Smg147109 			extension += hp->hs_ra_bytes;
12135312Smg147109 
12145312Smg147109 			/*
12155312Smg147109 			 * Some cd writers don't write sectors that aren't
12165312Smg147109 			 * used. Also, there's no point in reading sectors
12175312Smg147109 			 * we'll never look at.  So, if we're asked to go
12185312Smg147109 			 * beyond the end of a file, truncate to the length
12195312Smg147109 			 * of that file.
12205312Smg147109 			 *
12215312Smg147109 			 * Additionally, this behaviour is required by section
12225312Smg147109 			 * 6.4.5 of ISO 9660:1988(E).
12235312Smg147109 			 */
12245312Smg147109 			len = MIN(extension ? extension : PAGESIZE,
12255312Smg147109 			    filsiz - off);
12265312Smg147109 
12275312Smg147109 			/* A little paranoia. */
12285312Smg147109 			ASSERT(len > 0);
12295312Smg147109 
12305312Smg147109 			/*
12315312Smg147109 			 * After all that, make sure we're asking for things
12325312Smg147109 			 * in units that bdev_strategy() will understand
12335312Smg147109 			 * (see bug 4202551).
12345312Smg147109 			 */
12355312Smg147109 			len = roundup(len, DEV_BSIZE);
12365312Smg147109 			calcdone = 1;
12375312Smg147109 		}
12385312Smg147109 
12390Sstevel@tonic-gate 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off_tmp,
12400Sstevel@tonic-gate 		    &io_len_tmp, off, len, 0);
12410Sstevel@tonic-gate 
12425312Smg147109 		if (pp == NULL) {
12435312Smg147109 			/*
12445312Smg147109 			 * Pressure on memory, roll back readahead
12455312Smg147109 			 */
12465312Smg147109 			hp->hs_num_contig = 0;
12475312Smg147109 			hp->hs_ra_bytes = 0;
12485312Smg147109 			hp->hs_prev_offset = 0;
12490Sstevel@tonic-gate 			goto again;
12505312Smg147109 		}
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 		io_off = (uint_t)io_off_tmp;
12530Sstevel@tonic-gate 		io_len = (uint_t)io_len_tmp;
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 		/* check for truncation */
12560Sstevel@tonic-gate 		/*
12570Sstevel@tonic-gate 		 * xxx Clean up and return EIO instead?
12580Sstevel@tonic-gate 		 * xxx Ought to go to u_offset_t for everything, but we
12590Sstevel@tonic-gate 		 * xxx call lots of things that want uint_t arguments.
12600Sstevel@tonic-gate 		 */
12610Sstevel@tonic-gate 		ASSERT(io_off == io_off_tmp);
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 		/*
12640Sstevel@tonic-gate 		 * get enough buffers for worst-case scenario
12650Sstevel@tonic-gate 		 * (i.e., no coalescing possible).
12660Sstevel@tonic-gate 		 */
12670Sstevel@tonic-gate 		bufcnt = (len + secsize - 1) / secsize;
12680Sstevel@tonic-gate 		bufs = kmem_zalloc(bufcnt * sizeof (struct buf), KM_SLEEP);
12690Sstevel@tonic-gate 		vas = kmem_alloc(bufcnt * sizeof (caddr_t), KM_SLEEP);
12705312Smg147109 
12715312Smg147109 		/*
12725312Smg147109 		 * Allocate a array of semaphores if we are doing I/O
12735312Smg147109 		 * scheduling.
12745312Smg147109 		 */
12755312Smg147109 		if (fsp->hqueue != NULL)
12765312Smg147109 			fio_done = kmem_alloc(bufcnt * sizeof (ksema_t),
12775312Smg147109 			    KM_SLEEP);
12780Sstevel@tonic-gate 		for (count = 0; count < bufcnt; count++) {
12795312Smg147109 			bioinit(&bufs[count]);
12800Sstevel@tonic-gate 			bufs[count].b_edev = devvp->v_rdev;
12810Sstevel@tonic-gate 			bufs[count].b_dev = cmpdev(devvp->v_rdev);
12820Sstevel@tonic-gate 			bufs[count].b_flags = B_NOCACHE|B_BUSY|B_READ;
12830Sstevel@tonic-gate 			bufs[count].b_iodone = hsfs_iodone;
12840Sstevel@tonic-gate 			bufs[count].b_vp = vp;
12850Sstevel@tonic-gate 			bufs[count].b_file = vp;
12860Sstevel@tonic-gate 		}
12870Sstevel@tonic-gate 
1288206Speterte 		/*
1289206Speterte 		 * If our filesize is not an integer multiple of PAGESIZE,
1290206Speterte 		 * we zero that part of the last page that's between EOF and
1291206Speterte 		 * the PAGESIZE boundary.
1292206Speterte 		 */
12930Sstevel@tonic-gate 		xlen = io_len & PAGEOFFSET;
12940Sstevel@tonic-gate 		if (xlen != 0)
12950Sstevel@tonic-gate 			pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 		va = NULL;
12980Sstevel@tonic-gate 		lastp = NULL;
12990Sstevel@tonic-gate 		searchp = pp;
13000Sstevel@tonic-gate 		io_end = io_off + io_len;
13010Sstevel@tonic-gate 		for (count = 0, byte_offset = io_off;
13024866Sfrankho 		    byte_offset < io_end; count++) {
13030Sstevel@tonic-gate 			ASSERT(count < bufcnt);
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 			/* Compute disk address for interleaving. */
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 			/* considered without skips */
13080Sstevel@tonic-gate 			which_chunk_lbn = byte_offset / chunk_data_bytes;
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 			/* factor in skips */
13110Sstevel@tonic-gate 			offset_lbn = which_chunk_lbn * chunk_lbn_count;
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 			/* convert to physical byte offset for lbn */
13140Sstevel@tonic-gate 			offset_bytes = LBN_TO_BYTE(offset_lbn, vp->v_vfsp);
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 			/* don't forget offset into lbn */
13170Sstevel@tonic-gate 			offset_extra = byte_offset % chunk_data_bytes;
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 			/* get virtual block number for driver */
13204866Sfrankho 			driver_block =
13214866Sfrankho 			    lbtodb(bof + xarsiz + offset_bytes + offset_extra);
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 			if (lastp != searchp) {
13240Sstevel@tonic-gate 				/* this branch taken first time through loop */
13254866Sfrankho 				va = vas[count] =
13264866Sfrankho 				    ppmapin(searchp, PROT_WRITE, (caddr_t)-1);
13270Sstevel@tonic-gate 				/* ppmapin() guarantees not to return NULL */
13280Sstevel@tonic-gate 			} else {
13290Sstevel@tonic-gate 				vas[count] = NULL;
13300Sstevel@tonic-gate 			}
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 			bufs[count].b_un.b_addr = va + byte_offset % PAGESIZE;
13330Sstevel@tonic-gate 			bufs[count].b_offset =
13340Sstevel@tonic-gate 			    (offset_t)(byte_offset - io_off + off);
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 			/*
13370Sstevel@tonic-gate 			 * We specifically use the b_lblkno member here
13380Sstevel@tonic-gate 			 * as even in the 32 bit world driver_block can
13390Sstevel@tonic-gate 			 * get very large in line with the ISO9660 spec.
13400Sstevel@tonic-gate 			 */
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 			bufs[count].b_lblkno = driver_block;
13430Sstevel@tonic-gate 
13444866Sfrankho 			remaining_bytes =
13454866Sfrankho 			    ((which_chunk_lbn + 1) * chunk_data_bytes)
13464866Sfrankho 			    - byte_offset;
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 			/*
13490Sstevel@tonic-gate 			 * remaining_bytes can't be zero, as we derived
13500Sstevel@tonic-gate 			 * which_chunk_lbn directly from byte_offset.
13510Sstevel@tonic-gate 			 */
13521349Speterte 			if ((remaining_bytes + byte_offset) < (off + len)) {
13530Sstevel@tonic-gate 				/* coalesce-read the rest of the chunk */
13540Sstevel@tonic-gate 				bufs[count].b_bcount = remaining_bytes;
13550Sstevel@tonic-gate 			} else {
13560Sstevel@tonic-gate 				/* get the final bits */
13570Sstevel@tonic-gate 				bufs[count].b_bcount = off + len - byte_offset;
13580Sstevel@tonic-gate 			}
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 			/*
13610Sstevel@tonic-gate 			 * It would be nice to do multiple pages'
13620Sstevel@tonic-gate 			 * worth at once here when the opportunity
13630Sstevel@tonic-gate 			 * arises, as that has been shown to improve
13640Sstevel@tonic-gate 			 * our wall time.  However, to do that
13650Sstevel@tonic-gate 			 * requires that we use the pageio subsystem,
13660Sstevel@tonic-gate 			 * which doesn't mix well with what we're
13670Sstevel@tonic-gate 			 * already using here.  We can't use pageio
13680Sstevel@tonic-gate 			 * all the time, because that subsystem
13690Sstevel@tonic-gate 			 * assumes that a page is stored in N
13700Sstevel@tonic-gate 			 * contiguous blocks on the device.
13710Sstevel@tonic-gate 			 * Interleaving violates that assumption.
13725312Smg147109 			 *
13735312Smg147109 			 * Update: This is now not so big a problem
13745312Smg147109 			 * because of the I/O scheduler sitting below
13755312Smg147109 			 * that can re-order and coalesce I/O requests.
13760Sstevel@tonic-gate 			 */
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 			remainder = PAGESIZE - (byte_offset % PAGESIZE);
13790Sstevel@tonic-gate 			if (bufs[count].b_bcount > remainder) {
13800Sstevel@tonic-gate 				bufs[count].b_bcount = remainder;
13810Sstevel@tonic-gate 			}
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 			bufs[count].b_bufsize = bufs[count].b_bcount;
13841349Speterte 			if (((offset_t)byte_offset + bufs[count].b_bcount) >
13854866Sfrankho 			    HS_MAXFILEOFF) {
13861349Speterte 				break;
13871349Speterte 			}
13880Sstevel@tonic-gate 			byte_offset += bufs[count].b_bcount;
13890Sstevel@tonic-gate 
13905312Smg147109 			if (fsp->hqueue == NULL) {
13915312Smg147109 				(void) bdev_strategy(&bufs[count]);
13925312Smg147109 
13935312Smg147109 			} else {
13945312Smg147109 				/*
13955312Smg147109 				 * We are scheduling I/O so we need to enqueue
13965312Smg147109 				 * requests rather than calling bdev_strategy
13975312Smg147109 				 * here. A later invocation of the scheduling
13985312Smg147109 				 * function will take care of doing the actual
13995312Smg147109 				 * I/O as it selects requests from the queue as
14005312Smg147109 				 * per the scheduling logic.
14015312Smg147109 				 */
14025312Smg147109 				struct hio *hsio = kmem_cache_alloc(hio_cache,
14035312Smg147109 				    KM_SLEEP);
14045312Smg147109 
14055312Smg147109 				sema_init(&fio_done[count], 0, NULL,
14065312Smg147109 				    SEMA_DEFAULT, NULL);
14075312Smg147109 				hsio->bp = &bufs[count];
14085312Smg147109 				hsio->sema = &fio_done[count];
14095312Smg147109 				hsio->io_lblkno = bufs[count].b_lblkno;
14105312Smg147109 				hsio->nblocks = howmany(hsio->bp->b_bcount,
14115312Smg147109 				    DEV_BSIZE);
14125312Smg147109 
14135312Smg147109 				/* used for deadline */
14145312Smg147109 				hsio->io_timestamp =
14155312Smg147109 				    drv_hztousec(ddi_get_lbolt());
14165312Smg147109 
14175312Smg147109 				/* for I/O coalescing */
14185312Smg147109 				hsio->contig_chain = NULL;
14195312Smg147109 				hsched_enqueue_io(fsp, hsio, 0);
14205312Smg147109 			}
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_INBLK, 1);
14230Sstevel@tonic-gate 			lastp = searchp;
14240Sstevel@tonic-gate 			if ((remainder - bufs[count].b_bcount) < 1) {
14250Sstevel@tonic-gate 				searchp = searchp->p_next;
14260Sstevel@tonic-gate 			}
14270Sstevel@tonic-gate 		}
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 		bufsused = count;
14300Sstevel@tonic-gate 		/* Now wait for everything to come in */
14315312Smg147109 		if (fsp->hqueue == NULL) {
14325312Smg147109 			for (count = 0; count < bufsused; count++) {
14335312Smg147109 				if (err == 0) {
14345312Smg147109 					err = biowait(&bufs[count]);
14355312Smg147109 				} else
14365312Smg147109 					(void) biowait(&bufs[count]);
14375312Smg147109 			}
14385312Smg147109 		} else {
14395312Smg147109 			for (count = 0; count < bufsused; count++) {
14405312Smg147109 				struct buf *wbuf;
14415312Smg147109 
14425312Smg147109 				/*
14435312Smg147109 				 * Invoke scheduling function till our buf
14445312Smg147109 				 * is processed. In doing this it might
14455312Smg147109 				 * process bufs enqueued by other threads
14465312Smg147109 				 * which is good.
14475312Smg147109 				 */
14485312Smg147109 				wbuf = &bufs[count];
14495312Smg147109 				DTRACE_PROBE1(hsfs_io_wait, struct buf *, wbuf);
14505312Smg147109 				while (sema_tryp(&fio_done[count]) == 0) {
14515312Smg147109 					/*
14525312Smg147109 					 * hsched_invoke_strategy will return 1
14535312Smg147109 					 * if the I/O queue is empty. This means
14545312Smg147109 					 * that there is another thread who has
14555312Smg147109 					 * issued our buf and is waiting. So we
14565312Smg147109 					 * just block instead of spinning.
14575312Smg147109 					 */
14585312Smg147109 					if (hsched_invoke_strategy(fsp)) {
14595312Smg147109 						sema_p(&fio_done[count]);
14605312Smg147109 						break;
14615312Smg147109 					}
14625312Smg147109 				}
14635312Smg147109 				sema_destroy(&fio_done[count]);
14645312Smg147109 				DTRACE_PROBE1(hsfs_io_done, struct buf *, wbuf);
14655312Smg147109 
14665312Smg147109 				if (err == 0) {
14675312Smg147109 					err = geterror(wbuf);
14685312Smg147109 				}
14695312Smg147109 			}
14705312Smg147109 			kmem_free(fio_done, bufcnt * sizeof (ksema_t));
14710Sstevel@tonic-gate 		}
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 		/* Don't leak resources */
14740Sstevel@tonic-gate 		for (count = 0; count < bufcnt; count++) {
14755312Smg147109 			biofini(&bufs[count]);
14760Sstevel@tonic-gate 			if (count < bufsused && vas[count] != NULL) {
14770Sstevel@tonic-gate 				ppmapout(vas[count]);
14780Sstevel@tonic-gate 			}
14790Sstevel@tonic-gate 		}
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 		kmem_free(vas, bufcnt * sizeof (caddr_t));
14820Sstevel@tonic-gate 		kmem_free(bufs, bufcnt * sizeof (struct buf));
14830Sstevel@tonic-gate 	}
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	if (err) {
14860Sstevel@tonic-gate 		pvn_read_done(pp, B_ERROR);
14870Sstevel@tonic-gate 		return (err);
14880Sstevel@tonic-gate 	}
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	/*
14910Sstevel@tonic-gate 	 * Lock the requested page, and the one after it if possible.
14920Sstevel@tonic-gate 	 * Don't bother if our caller hasn't given us a place to stash
14930Sstevel@tonic-gate 	 * the page pointers, since otherwise we'd lock pages that would
14940Sstevel@tonic-gate 	 * never get unlocked.
14950Sstevel@tonic-gate 	 */
14960Sstevel@tonic-gate 	if (pagefound) {
14970Sstevel@tonic-gate 		int index;
14980Sstevel@tonic-gate 		ulong_t soff;
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 		/*
15010Sstevel@tonic-gate 		 * Make sure it's in memory before we say it's here.
15020Sstevel@tonic-gate 		 */
15030Sstevel@tonic-gate 		if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) {
15040Sstevel@tonic-gate 			hsfs_lostpage++;
15050Sstevel@tonic-gate 			goto reread;
15060Sstevel@tonic-gate 		}
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 		pl[0] = pp;
15090Sstevel@tonic-gate 		index = 1;
15105312Smg147109 		atomic_inc_64(&fsp->cache_read_pages);
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 		/*
15130Sstevel@tonic-gate 		 * Try to lock the next page, if it exists, without
15140Sstevel@tonic-gate 		 * blocking.
15150Sstevel@tonic-gate 		 */
15160Sstevel@tonic-gate 		plsz -= PAGESIZE;
15170Sstevel@tonic-gate 		/* LINTED (plsz is unsigned) */
15180Sstevel@tonic-gate 		for (soff = off + PAGESIZE; plsz > 0;
15190Sstevel@tonic-gate 		    soff += PAGESIZE, plsz -= PAGESIZE) {
15200Sstevel@tonic-gate 			pp = page_lookup_nowait(vp, (u_offset_t)soff,
15214866Sfrankho 			    SE_SHARED);
15220Sstevel@tonic-gate 			if (pp == NULL)
15230Sstevel@tonic-gate 				break;
15240Sstevel@tonic-gate 			pl[index++] = pp;
15250Sstevel@tonic-gate 		}
15260Sstevel@tonic-gate 		pl[index] = NULL;
15275312Smg147109 
15285312Smg147109 		/*
15295312Smg147109 		 * Schedule a semi-asynchronous readahead if we are
15305312Smg147109 		 * accessing the last cached page for the current
15315312Smg147109 		 * file.
15325312Smg147109 		 *
15335312Smg147109 		 * Doing this here means that readaheads will be
15345312Smg147109 		 * issued only if cache-hits occur. This is an advantage
15355312Smg147109 		 * since cache-hits would mean that readahead is giving
15365312Smg147109 		 * the desired benefit. If cache-hits do not occur there
15375312Smg147109 		 * is no point in reading ahead of time - the system
15385312Smg147109 		 * is loaded anyway.
15395312Smg147109 		 */
15405312Smg147109 		if (fsp->hqueue != NULL &&
15415312Smg147109 		    hp->hs_prev_offset - off == PAGESIZE &&
15425312Smg147109 		    hp->hs_prev_offset < filsiz &&
15435312Smg147109 		    hp->hs_ra_bytes > 0 &&
15445312Smg147109 		    !page_exists(vp, hp->hs_prev_offset)) {
15455312Smg147109 			(void) hsfs_getpage_ra(vp, hp->hs_prev_offset, seg,
15465312Smg147109 			    addr + PAGESIZE, hp, fsp, xarsiz, bof,
15475312Smg147109 			    chunk_lbn_count, chunk_data_bytes);
15485312Smg147109 		}
15495312Smg147109 
15500Sstevel@tonic-gate 		return (0);
15510Sstevel@tonic-gate 	}
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	if (pp != NULL) {
15540Sstevel@tonic-gate 		pvn_plist_init(pp, pl, plsz, off, io_len, rw);
15550Sstevel@tonic-gate 	}
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	return (err);
15580Sstevel@tonic-gate }
15590Sstevel@tonic-gate 
1560*5331Samw /*ARGSUSED*/
15610Sstevel@tonic-gate static int
15620Sstevel@tonic-gate hsfs_getpage(
15630Sstevel@tonic-gate 	struct vnode *vp,
15640Sstevel@tonic-gate 	offset_t off,
15650Sstevel@tonic-gate 	size_t len,
15660Sstevel@tonic-gate 	uint_t *protp,
15670Sstevel@tonic-gate 	struct page *pl[],
15680Sstevel@tonic-gate 	size_t plsz,
15690Sstevel@tonic-gate 	struct seg *seg,
15700Sstevel@tonic-gate 	caddr_t addr,
15710Sstevel@tonic-gate 	enum seg_rw rw,
1572*5331Samw 	struct cred *cred,
1573*5331Samw 	caller_context_t *ct)
15740Sstevel@tonic-gate {
15750Sstevel@tonic-gate 	int err;
15760Sstevel@tonic-gate 	uint_t filsiz;
15775312Smg147109 	struct hsfs *fsp;
15785312Smg147109 	struct hsnode *hp;
15795312Smg147109 
15805312Smg147109 	fsp = VFS_TO_HSFS(vp->v_vfsp);
15815312Smg147109 	hp = VTOH(vp);
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	/* does not support write */
15840Sstevel@tonic-gate 	if (rw == S_WRITE) {
15850Sstevel@tonic-gate 		panic("write attempt on READ ONLY HSFS");
15860Sstevel@tonic-gate 		/*NOTREACHED*/
15870Sstevel@tonic-gate 	}
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
15900Sstevel@tonic-gate 		return (ENOSYS);
15910Sstevel@tonic-gate 	}
15920Sstevel@tonic-gate 
15931349Speterte 	ASSERT(off <= HS_MAXFILEOFF);
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	/*
15960Sstevel@tonic-gate 	 * Determine file data size for EOF check.
15970Sstevel@tonic-gate 	 */
15980Sstevel@tonic-gate 	filsiz = hp->hs_dirent.ext_size;
15990Sstevel@tonic-gate 	if ((off + len) > (offset_t)(filsiz + PAGEOFFSET) && seg != segkmap)
16000Sstevel@tonic-gate 		return (EFAULT);	/* beyond EOF */
16010Sstevel@tonic-gate 
16025312Smg147109 	/*
16035312Smg147109 	 * Async Read-ahead computation.
16045312Smg147109 	 * This attempts to detect sequential access pattern and
16055312Smg147109 	 * enables reading extra pages ahead of time.
16065312Smg147109 	 */
16075312Smg147109 	if (fsp->hqueue != NULL) {
16085312Smg147109 		/*
16095312Smg147109 		 * This check for sequential access also takes into
16105312Smg147109 		 * account segmap weirdness when reading in chunks
16115312Smg147109 		 * less than the segmap size of 8K.
16125312Smg147109 		 */
16135312Smg147109 		if (hp->hs_prev_offset == off || (off <
16145312Smg147109 		    hp->hs_prev_offset && off + MAX(len, PAGESIZE)
16155312Smg147109 		    >= hp->hs_prev_offset)) {
16165312Smg147109 			if (hp->hs_num_contig <
16175312Smg147109 			    (seq_contig_requests - 1)) {
16185312Smg147109 				hp->hs_num_contig++;
16195312Smg147109 
16205312Smg147109 			} else {
16215312Smg147109 				/*
16225312Smg147109 				 * We increase readahead quantum till
16235312Smg147109 				 * a predefined max. max_readahead_bytes
16245312Smg147109 				 * is a multiple of PAGESIZE.
16255312Smg147109 				 */
16265312Smg147109 				if (hp->hs_ra_bytes <
16275312Smg147109 				    fsp->hqueue->max_ra_bytes) {
16285312Smg147109 					hp->hs_ra_bytes += PAGESIZE;
16295312Smg147109 				}
16305312Smg147109 			}
16315312Smg147109 		} else {
16325312Smg147109 			/*
16335312Smg147109 			 * Not contiguous so reduce read ahead counters.
16345312Smg147109 			 */
16355312Smg147109 			if (hp->hs_ra_bytes > 0)
16365312Smg147109 				hp->hs_ra_bytes -= PAGESIZE;
16375312Smg147109 
16385312Smg147109 			if (hp->hs_ra_bytes <= 0) {
16395312Smg147109 				hp->hs_ra_bytes = 0;
16405312Smg147109 				if (hp->hs_num_contig > 0)
16415312Smg147109 					hp->hs_num_contig--;
16425312Smg147109 			}
16435312Smg147109 		}
16445312Smg147109 		/*
16455312Smg147109 		 * Length must be rounded up to page boundary.
16465312Smg147109 		 * since we read in units of pages.
16475312Smg147109 		 */
16485312Smg147109 		hp->hs_prev_offset = off + roundup(len, PAGESIZE);
16495312Smg147109 		DTRACE_PROBE1(hsfs_compute_ra, struct hsnode *, hp);
16505312Smg147109 	}
16510Sstevel@tonic-gate 	if (protp != NULL)
16520Sstevel@tonic-gate 		*protp = PROT_ALL;
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	if (len <= PAGESIZE)
16550Sstevel@tonic-gate 		err = hsfs_getapage(vp, (u_offset_t)off, len, protp, pl, plsz,
16560Sstevel@tonic-gate 		    seg, addr, rw, cred);
16570Sstevel@tonic-gate 	else
16580Sstevel@tonic-gate 		err = pvn_getpages(hsfs_getapage, vp, off, len, protp,
16590Sstevel@tonic-gate 		    pl, plsz, seg, addr, rw, cred);
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	return (err);
16620Sstevel@tonic-gate }
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate /*
16670Sstevel@tonic-gate  * This function should never be called. We need to have it to pass
16680Sstevel@tonic-gate  * it as an argument to other functions.
16690Sstevel@tonic-gate  */
16700Sstevel@tonic-gate /*ARGSUSED*/
16710Sstevel@tonic-gate int
16720Sstevel@tonic-gate hsfs_putapage(
16730Sstevel@tonic-gate 	vnode_t		*vp,
16740Sstevel@tonic-gate 	page_t		*pp,
16750Sstevel@tonic-gate 	u_offset_t	*offp,
16760Sstevel@tonic-gate 	size_t		*lenp,
16770Sstevel@tonic-gate 	int		flags,
16780Sstevel@tonic-gate 	cred_t		*cr)
16790Sstevel@tonic-gate {
16800Sstevel@tonic-gate 	/* should never happen - just destroy it */
16810Sstevel@tonic-gate 	cmn_err(CE_NOTE, "hsfs_putapage: dirty HSFS page");
16820Sstevel@tonic-gate 	pvn_write_done(pp, B_ERROR | B_WRITE | B_INVAL | B_FORCE | flags);
16830Sstevel@tonic-gate 	return (0);
16840Sstevel@tonic-gate }
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate /*
16880Sstevel@tonic-gate  * The only flags we support are B_INVAL, B_FREE and B_DONTNEED.
16890Sstevel@tonic-gate  * B_INVAL is set by:
16900Sstevel@tonic-gate  *
16910Sstevel@tonic-gate  *	1) the MC_SYNC command of memcntl(2) to support the MS_INVALIDATE flag.
16920Sstevel@tonic-gate  *	2) the MC_ADVISE command of memcntl(2) with the MADV_DONTNEED advice
16930Sstevel@tonic-gate  *	   which translates to an MC_SYNC with the MS_INVALIDATE flag.
16940Sstevel@tonic-gate  *
16950Sstevel@tonic-gate  * The B_FREE (as well as the B_DONTNEED) flag is set when the
16960Sstevel@tonic-gate  * MADV_SEQUENTIAL advice has been used. VOP_PUTPAGE is invoked
16970Sstevel@tonic-gate  * from SEGVN to release pages behind a pagefault.
16980Sstevel@tonic-gate  */
16990Sstevel@tonic-gate /*ARGSUSED*/
17000Sstevel@tonic-gate static int
17010Sstevel@tonic-gate hsfs_putpage(
1702*5331Samw 	struct vnode		*vp,
1703*5331Samw 	offset_t		off,
1704*5331Samw 	size_t			len,
1705*5331Samw 	int			flags,
1706*5331Samw 	struct cred		*cr,
1707*5331Samw 	caller_context_t	*ct)
17080Sstevel@tonic-gate {
17090Sstevel@tonic-gate 	int error = 0;
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 	if (vp->v_count == 0) {
17120Sstevel@tonic-gate 		panic("hsfs_putpage: bad v_count");
17130Sstevel@tonic-gate 		/*NOTREACHED*/
17140Sstevel@tonic-gate 	}
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
17170Sstevel@tonic-gate 		return (ENOSYS);
17180Sstevel@tonic-gate 
17191349Speterte 	ASSERT(off <= HS_MAXFILEOFF);
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	if (!vn_has_cached_data(vp))	/* no pages mapped */
17220Sstevel@tonic-gate 		return (0);
17230Sstevel@tonic-gate 
17244866Sfrankho 	if (len == 0) {		/* from 'off' to EOF */
17254866Sfrankho 		error = pvn_vplist_dirty(vp, off, hsfs_putapage, flags, cr);
17264866Sfrankho 	} else {
17270Sstevel@tonic-gate 		offset_t end_off = off + len;
17280Sstevel@tonic-gate 		offset_t file_size = VTOH(vp)->hs_dirent.ext_size;
17290Sstevel@tonic-gate 		offset_t io_off;
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 		file_size = (file_size + PAGESIZE - 1) & PAGEMASK;
17320Sstevel@tonic-gate 		if (end_off > file_size)
17330Sstevel@tonic-gate 			end_off = file_size;
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 		for (io_off = off; io_off < end_off; io_off += PAGESIZE) {
17360Sstevel@tonic-gate 			page_t *pp;
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 			/*
17390Sstevel@tonic-gate 			 * We insist on getting the page only if we are
17400Sstevel@tonic-gate 			 * about to invalidate, free or write it and
17410Sstevel@tonic-gate 			 * the B_ASYNC flag is not set.
17420Sstevel@tonic-gate 			 */
17430Sstevel@tonic-gate 			if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
17440Sstevel@tonic-gate 				pp = page_lookup(vp, io_off,
17454866Sfrankho 				    (flags & (B_INVAL | B_FREE)) ?
17464866Sfrankho 				    SE_EXCL : SE_SHARED);
17470Sstevel@tonic-gate 			} else {
17480Sstevel@tonic-gate 				pp = page_lookup_nowait(vp, io_off,
17494866Sfrankho 				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
17500Sstevel@tonic-gate 			}
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 			if (pp == NULL)
17530Sstevel@tonic-gate 				continue;
17545312Smg147109 
17550Sstevel@tonic-gate 			/*
17560Sstevel@tonic-gate 			 * Normally pvn_getdirty() should return 0, which
17570Sstevel@tonic-gate 			 * impies that it has done the job for us.
17580Sstevel@tonic-gate 			 * The shouldn't-happen scenario is when it returns 1.
17590Sstevel@tonic-gate 			 * This means that the page has been modified and
17600Sstevel@tonic-gate 			 * needs to be put back.
17610Sstevel@tonic-gate 			 * Since we can't write on a CD, we fake a failed
17620Sstevel@tonic-gate 			 * I/O and force pvn_write_done() to destroy the page.
17630Sstevel@tonic-gate 			 */
17640Sstevel@tonic-gate 			if (pvn_getdirty(pp, flags) == 1) {
17650Sstevel@tonic-gate 				cmn_err(CE_NOTE,
17664866Sfrankho 				    "hsfs_putpage: dirty HSFS page");
17670Sstevel@tonic-gate 				pvn_write_done(pp, flags |
17680Sstevel@tonic-gate 				    B_ERROR | B_WRITE | B_INVAL | B_FORCE);
17690Sstevel@tonic-gate 			}
17700Sstevel@tonic-gate 		}
17710Sstevel@tonic-gate 	}
17720Sstevel@tonic-gate 	return (error);
17730Sstevel@tonic-gate }
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate /*ARGSUSED*/
17770Sstevel@tonic-gate static int
17780Sstevel@tonic-gate hsfs_map(
17790Sstevel@tonic-gate 	struct vnode *vp,
17800Sstevel@tonic-gate 	offset_t off,
17810Sstevel@tonic-gate 	struct as *as,
17820Sstevel@tonic-gate 	caddr_t *addrp,
17830Sstevel@tonic-gate 	size_t len,
17840Sstevel@tonic-gate 	uchar_t prot,
17850Sstevel@tonic-gate 	uchar_t maxprot,
17860Sstevel@tonic-gate 	uint_t flags,
1787*5331Samw 	struct cred *cred,
1788*5331Samw 	caller_context_t *ct)
17890Sstevel@tonic-gate {
17900Sstevel@tonic-gate 	struct segvn_crargs vn_a;
17910Sstevel@tonic-gate 	int error;
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	/* VFS_RECORD(vp->v_vfsp, VS_MAP, VS_CALL); */
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
17960Sstevel@tonic-gate 		return (ENOSYS);
17970Sstevel@tonic-gate 
17981349Speterte 	if (off > HS_MAXFILEOFF || off < 0 ||
17991349Speterte 	    (off + len) < 0 || (off + len) > HS_MAXFILEOFF)
1800143Speterte 		return (ENXIO);
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 	if (vp->v_type != VREG) {
18030Sstevel@tonic-gate 		return (ENODEV);
18040Sstevel@tonic-gate 	}
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 	/*
18070Sstevel@tonic-gate 	 * If file is being locked, disallow mapping.
18080Sstevel@tonic-gate 	 */
18090Sstevel@tonic-gate 	if (vn_has_mandatory_locks(vp, VTOH(vp)->hs_dirent.mode))
18100Sstevel@tonic-gate 		return (EAGAIN);
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 	as_rangelock(as);
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 	if ((flags & MAP_FIXED) == 0) {
18150Sstevel@tonic-gate 		map_addr(addrp, len, off, 1, flags);
18160Sstevel@tonic-gate 		if (*addrp == NULL) {
18170Sstevel@tonic-gate 			as_rangeunlock(as);
18180Sstevel@tonic-gate 			return (ENOMEM);
18190Sstevel@tonic-gate 		}
18200Sstevel@tonic-gate 	} else {
18210Sstevel@tonic-gate 		/*
18220Sstevel@tonic-gate 		 * User specified address - blow away any previous mappings
18230Sstevel@tonic-gate 		 */
18240Sstevel@tonic-gate 		(void) as_unmap(as, *addrp, len);
18250Sstevel@tonic-gate 	}
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 	vn_a.vp = vp;
18280Sstevel@tonic-gate 	vn_a.offset = off;
18290Sstevel@tonic-gate 	vn_a.type = flags & MAP_TYPE;
18300Sstevel@tonic-gate 	vn_a.prot = prot;
18310Sstevel@tonic-gate 	vn_a.maxprot = maxprot;
18320Sstevel@tonic-gate 	vn_a.flags = flags & ~MAP_TYPE;
18330Sstevel@tonic-gate 	vn_a.cred = cred;
18340Sstevel@tonic-gate 	vn_a.amp = NULL;
18350Sstevel@tonic-gate 	vn_a.szc = 0;
18360Sstevel@tonic-gate 	vn_a.lgrp_mem_policy_flags = 0;
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
18390Sstevel@tonic-gate 	as_rangeunlock(as);
18400Sstevel@tonic-gate 	return (error);
18410Sstevel@tonic-gate }
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate /* ARGSUSED */
18440Sstevel@tonic-gate static int
18450Sstevel@tonic-gate hsfs_addmap(
18460Sstevel@tonic-gate 	struct vnode *vp,
18470Sstevel@tonic-gate 	offset_t off,
18480Sstevel@tonic-gate 	struct as *as,
18490Sstevel@tonic-gate 	caddr_t addr,
18500Sstevel@tonic-gate 	size_t len,
18510Sstevel@tonic-gate 	uchar_t prot,
18520Sstevel@tonic-gate 	uchar_t maxprot,
18530Sstevel@tonic-gate 	uint_t flags,
1854*5331Samw 	struct cred *cr,
1855*5331Samw 	caller_context_t *ct)
18560Sstevel@tonic-gate {
18570Sstevel@tonic-gate 	struct hsnode *hp;
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
18600Sstevel@tonic-gate 		return (ENOSYS);
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 	hp = VTOH(vp);
18630Sstevel@tonic-gate 	mutex_enter(&hp->hs_contents_lock);
18640Sstevel@tonic-gate 	hp->hs_mapcnt += btopr(len);
18650Sstevel@tonic-gate 	mutex_exit(&hp->hs_contents_lock);
18660Sstevel@tonic-gate 	return (0);
18670Sstevel@tonic-gate }
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate /*ARGSUSED*/
18700Sstevel@tonic-gate static int
18710Sstevel@tonic-gate hsfs_delmap(
18720Sstevel@tonic-gate 	struct vnode *vp,
18730Sstevel@tonic-gate 	offset_t off,
18740Sstevel@tonic-gate 	struct as *as,
18750Sstevel@tonic-gate 	caddr_t addr,
18760Sstevel@tonic-gate 	size_t len,
18770Sstevel@tonic-gate 	uint_t prot,
18780Sstevel@tonic-gate 	uint_t maxprot,
18790Sstevel@tonic-gate 	uint_t flags,
1880*5331Samw 	struct cred *cr,
1881*5331Samw 	caller_context_t *ct)
18820Sstevel@tonic-gate {
18830Sstevel@tonic-gate 	struct hsnode *hp;
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
18860Sstevel@tonic-gate 		return (ENOSYS);
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	hp = VTOH(vp);
18890Sstevel@tonic-gate 	mutex_enter(&hp->hs_contents_lock);
18900Sstevel@tonic-gate 	hp->hs_mapcnt -= btopr(len);	/* Count released mappings */
18910Sstevel@tonic-gate 	ASSERT(hp->hs_mapcnt >= 0);
18920Sstevel@tonic-gate 	mutex_exit(&hp->hs_contents_lock);
18930Sstevel@tonic-gate 	return (0);
18940Sstevel@tonic-gate }
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate /* ARGSUSED */
18970Sstevel@tonic-gate static int
1898*5331Samw hsfs_seek(
1899*5331Samw 	struct vnode *vp,
1900*5331Samw 	offset_t ooff,
1901*5331Samw 	offset_t *noffp,
1902*5331Samw 	caller_context_t *ct)
19030Sstevel@tonic-gate {
19040Sstevel@tonic-gate 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
19050Sstevel@tonic-gate }
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate /* ARGSUSED */
19080Sstevel@tonic-gate static int
19090Sstevel@tonic-gate hsfs_frlock(
19100Sstevel@tonic-gate 	struct vnode *vp,
19110Sstevel@tonic-gate 	int cmd,
19120Sstevel@tonic-gate 	struct flock64 *bfp,
19130Sstevel@tonic-gate 	int flag,
19140Sstevel@tonic-gate 	offset_t offset,
19150Sstevel@tonic-gate 	struct flk_callback *flk_cbp,
1916*5331Samw 	cred_t *cr,
1917*5331Samw 	caller_context_t *ct)
19180Sstevel@tonic-gate {
19190Sstevel@tonic-gate 	struct hsnode *hp = VTOH(vp);
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	/*
19220Sstevel@tonic-gate 	 * If the file is being mapped, disallow fs_frlock.
19230Sstevel@tonic-gate 	 * We are not holding the hs_contents_lock while checking
19240Sstevel@tonic-gate 	 * hs_mapcnt because the current locking strategy drops all
19250Sstevel@tonic-gate 	 * locks before calling fs_frlock.
19260Sstevel@tonic-gate 	 * So, hs_mapcnt could change before we enter fs_frlock making
19270Sstevel@tonic-gate 	 * it meaningless to have held hs_contents_lock in the first place.
19280Sstevel@tonic-gate 	 */
19290Sstevel@tonic-gate 	if (hp->hs_mapcnt > 0 && MANDLOCK(vp, hp->hs_dirent.mode))
19300Sstevel@tonic-gate 		return (EAGAIN);
19310Sstevel@tonic-gate 
1932*5331Samw 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
19330Sstevel@tonic-gate }
19340Sstevel@tonic-gate 
19355312Smg147109 static int
19365312Smg147109 hsched_deadline_compare(const void *x1, const void *x2)
19375312Smg147109 {
19385312Smg147109 	const struct hio *h1 = x1;
19395312Smg147109 	const struct hio *h2 = x2;
19405312Smg147109 
19415312Smg147109 	if (h1->io_timestamp < h2->io_timestamp)
19425312Smg147109 		return (-1);
19435312Smg147109 	if (h1->io_timestamp > h2->io_timestamp)
19445312Smg147109 		return (1);
19455312Smg147109 
19465312Smg147109 	if (h1->io_lblkno < h2->io_lblkno)
19475312Smg147109 		return (-1);
19485312Smg147109 	if (h1->io_lblkno > h2->io_lblkno)
19495312Smg147109 		return (1);
19505312Smg147109 
19515312Smg147109 	if (h1 < h2)
19525312Smg147109 		return (-1);
19535312Smg147109 	if (h1 > h2)
19545312Smg147109 		return (1);
19555312Smg147109 
19565312Smg147109 	return (0);
19575312Smg147109 }
19585312Smg147109 
19595312Smg147109 static int
19605312Smg147109 hsched_offset_compare(const void *x1, const void *x2)
19615312Smg147109 {
19625312Smg147109 	const struct hio *h1 = x1;
19635312Smg147109 	const struct hio *h2 = x2;
19645312Smg147109 
19655312Smg147109 	if (h1->io_lblkno < h2->io_lblkno)
19665312Smg147109 		return (-1);
19675312Smg147109 	if (h1->io_lblkno > h2->io_lblkno)
19685312Smg147109 		return (1);
19695312Smg147109 
19705312Smg147109 	if (h1 < h2)
19715312Smg147109 		return (-1);
19725312Smg147109 	if (h1 > h2)
19735312Smg147109 		return (1);
19745312Smg147109 
19755312Smg147109 	return (0);
19765312Smg147109 }
19775312Smg147109 
19785312Smg147109 void
19795312Smg147109 hsched_init_caches(void)
19805312Smg147109 {
19815312Smg147109 	hio_cache = kmem_cache_create("hsfs_hio_cache",
19825312Smg147109 	    sizeof (struct hio), 0, NULL,
19835312Smg147109 	    NULL, NULL, NULL, NULL, 0);
19845312Smg147109 
19855312Smg147109 	hio_info_cache = kmem_cache_create("hsfs_hio_info_cache",
19865312Smg147109 	    sizeof (struct hio_info), 0, NULL,
19875312Smg147109 	    NULL, NULL, NULL, NULL, 0);
19885312Smg147109 }
19895312Smg147109 
19905312Smg147109 void
19915312Smg147109 hsched_fini_caches(void)
19925312Smg147109 {
19935312Smg147109 	kmem_cache_destroy(hio_cache);
19945312Smg147109 	kmem_cache_destroy(hio_info_cache);
19955312Smg147109 }
19965312Smg147109 
19975312Smg147109 /*
19985312Smg147109  * Initialize I/O scheduling structures. This is called via hsfs_mount
19995312Smg147109  */
20005312Smg147109 void
20015312Smg147109 hsched_init(struct hsfs *fsp, int fsid, struct modlinkage *modlinkage)
20025312Smg147109 {
20035312Smg147109 	struct hsfs_queue *hqueue = fsp->hqueue;
20045312Smg147109 	struct vnode *vp = fsp->hsfs_devvp;
20055312Smg147109 
20065312Smg147109 	/* TaskQ name of the form: hsched_task_ + stringof(int) */
20075312Smg147109 	char namebuf[23];
20085312Smg147109 	int error, err;
20095312Smg147109 	struct dk_cinfo info;
20105312Smg147109 	ldi_handle_t lh;
20115312Smg147109 	ldi_ident_t li;
20125312Smg147109 
20135312Smg147109 	/*
20145312Smg147109 	 * Default maxtransfer = 16k chunk
20155312Smg147109 	 */
20165312Smg147109 	hqueue->dev_maxtransfer = 16384;
20175312Smg147109 
20185312Smg147109 	/*
20195312Smg147109 	 * Try to fetch the maximum device transfer size. This is used to
20205312Smg147109 	 * ensure that a coalesced block does not exceed the maxtransfer.
20215312Smg147109 	 */
20225312Smg147109 	err  = ldi_ident_from_mod(modlinkage, &li);
20235312Smg147109 	if (err) {
20245312Smg147109 		cmn_err(CE_NOTE, "hsched_init: Querying device failed");
20255312Smg147109 		cmn_err(CE_NOTE, "hsched_init: ldi_ident_from_mod err=%d\n",
20265312Smg147109 		    err);
20275312Smg147109 		goto set_ra;
20285312Smg147109 	}
20295312Smg147109 
20305312Smg147109 	err = ldi_open_by_dev(&(vp->v_rdev), OTYP_CHR, FREAD, CRED(), &lh, li);
20315312Smg147109 	ldi_ident_release(li);
20325312Smg147109 	if (err) {
20335312Smg147109 		cmn_err(CE_NOTE, "hsched_init: Querying device failed");
20345312Smg147109 		cmn_err(CE_NOTE, "hsched_init: ldi_open err=%d\n", err);
20355312Smg147109 		goto set_ra;
20365312Smg147109 	}
20375312Smg147109 
20385312Smg147109 	error = ldi_ioctl(lh, DKIOCINFO, (intptr_t)&info, FKIOCTL,
20395312Smg147109 	    CRED(), &err);
20405312Smg147109 	err = ldi_close(lh, FREAD, CRED());
20415312Smg147109 	if (err) {
20425312Smg147109 		cmn_err(CE_NOTE, "hsched_init: Querying device failed");
20435312Smg147109 		cmn_err(CE_NOTE, "hsched_init: ldi_close err=%d\n", err);
20445312Smg147109 	}
20455312Smg147109 
20465312Smg147109 	if (error == 0) {
20475312Smg147109 		hqueue->dev_maxtransfer = ldbtob(info.dki_maxtransfer);
20485312Smg147109 	}
20495312Smg147109 
20505312Smg147109 set_ra:
20515312Smg147109 	/*
20525312Smg147109 	 * Max size of data to read ahead for sequential access pattern.
20535312Smg147109 	 * Conservative to avoid letting the underlying CD drive to spin
20545312Smg147109 	 * down, in case the application is reading slowly.
20555312Smg147109 	 * We read ahead upto a max of 4 pages.
20565312Smg147109 	 */
20575312Smg147109 	hqueue->max_ra_bytes = PAGESIZE * 8;
20585312Smg147109 
20595312Smg147109 	mutex_init(&(hqueue->hsfs_queue_lock), NULL, MUTEX_DEFAULT, NULL);
20605312Smg147109 	mutex_init(&(hqueue->strategy_lock), NULL, MUTEX_DEFAULT, NULL);
20615312Smg147109 	avl_create(&(hqueue->read_tree), hsched_offset_compare,
20625312Smg147109 	    sizeof (struct hio), offsetof(struct hio, io_offset_node));
20635312Smg147109 	avl_create(&(hqueue->deadline_tree), hsched_deadline_compare,
20645312Smg147109 	    sizeof (struct hio), offsetof(struct hio, io_deadline_node));
20655312Smg147109 
20665312Smg147109 	(void) snprintf(namebuf, sizeof (namebuf), "hsched_task_%d", fsid);
20675312Smg147109 	hqueue->ra_task = taskq_create(namebuf, hsfs_taskq_nthreads,
20685312Smg147109 	    minclsyspri + 2, 1, 104857600 / PAGESIZE, TASKQ_DYNAMIC);
20695312Smg147109 
20705312Smg147109 	hqueue->next = NULL;
20715312Smg147109 	hqueue->nbuf = kmem_zalloc(sizeof (struct buf), KM_SLEEP);
20725312Smg147109 }
20735312Smg147109 
20745312Smg147109 void
20755312Smg147109 hsched_fini(struct hsfs_queue *hqueue)
20765312Smg147109 {
20775312Smg147109 	if (hqueue != NULL) {
20785312Smg147109 		avl_destroy(&(hqueue->read_tree));
20795312Smg147109 		avl_destroy(&(hqueue->deadline_tree));
20805312Smg147109 		mutex_destroy(&(hqueue->hsfs_queue_lock));
20815312Smg147109 		mutex_destroy(&(hqueue->strategy_lock));
20825312Smg147109 
20835312Smg147109 		/*
20845312Smg147109 		 * If there are any existing readahead threads running
20855312Smg147109 		 * taskq_destroy will wait for them to finish.
20865312Smg147109 		 */
20875312Smg147109 		taskq_destroy(hqueue->ra_task);
20885312Smg147109 		if (hqueue->next != NULL) {
20895312Smg147109 			kmem_cache_free(hio_cache, hqueue->next);
20905312Smg147109 		}
20915312Smg147109 		kmem_free(hqueue->nbuf, sizeof (struct buf));
20925312Smg147109 	}
20935312Smg147109 }
20945312Smg147109 
20955312Smg147109 /*
20965312Smg147109  * Determine if two I/O requests are adjacent to each other so
20975312Smg147109  * that they can coalesced.
20985312Smg147109  */
20995312Smg147109 #define	IS_ADJACENT(io, nio) \
21005312Smg147109 	(((io)->io_lblkno + (io)->nblocks == (nio)->io_lblkno) && \
21015312Smg147109 	(io)->bp->b_edev == (nio)->bp->b_edev)
21025312Smg147109 
21035312Smg147109 /*
21045312Smg147109  * This performs the actual I/O scheduling logic. We use the Circular
21055312Smg147109  * Look algorithm here. Sort the I/O requests in ascending order of
21065312Smg147109  * logical block number and process them starting with the lowest
21075312Smg147109  * numbered block and progressing towards higher block numbers in the
21085312Smg147109  * queue. Once there are no more higher numbered blocks, start again
21095312Smg147109  * with the lowest one. This is good for CD/DVD as you keep moving
21105312Smg147109  * the head in one direction along the outward spiral track and avoid
21115312Smg147109  * too many seeks as much as possible. The re-ordering also allows
21125312Smg147109  * us to coalesce adjacent requests into one larger request.
21135312Smg147109  * This is thus essentially a 1-way Elevator with front merging.
21145312Smg147109  *
21155312Smg147109  * In addition each read request here has a deadline and will be
21165312Smg147109  * processed out of turn if the deadline (500ms) expires.
21175312Smg147109  *
21185312Smg147109  * This function is necessarily serialized via hqueue->strategy_lock.
21195312Smg147109  * This function sits just below hsfs_getapage and processes all read
21205312Smg147109  * requests orginating from that function.
21215312Smg147109  */
21225312Smg147109 int
21235312Smg147109 hsched_invoke_strategy(struct hsfs *fsp)
21245312Smg147109 {
21255312Smg147109 	struct hsfs_queue *hqueue;
21265312Smg147109 	struct buf *nbuf;
21275312Smg147109 	struct hio *fio, *nio, *tio, *prev, *last;
21285312Smg147109 	size_t bsize, soffset, offset, data;
21295312Smg147109 	int bioret, bufcount;
21305312Smg147109 	struct vnode *fvp;
21315312Smg147109 	ksema_t *io_done;
21325312Smg147109 	caddr_t iodata;
21335312Smg147109 
21345312Smg147109 	hqueue = fsp->hqueue;
21355312Smg147109 	mutex_enter(&hqueue->strategy_lock);
21365312Smg147109 	mutex_enter(&hqueue->hsfs_queue_lock);
21375312Smg147109 
21385312Smg147109 	/*
21395312Smg147109 	 * Check for Deadline expiration first
21405312Smg147109 	 */
21415312Smg147109 	fio = avl_first(&hqueue->deadline_tree);
21425312Smg147109 
21435312Smg147109 	/*
21445312Smg147109 	 * Paranoid check for empty I/O queue. Both deadline
21455312Smg147109 	 * and read trees contain same data sorted in different
21465312Smg147109 	 * ways. So empty deadline tree = empty read tree.
21475312Smg147109 	 */
21485312Smg147109 	if (fio == NULL) {
21495312Smg147109 		/*
21505312Smg147109 		 * Remove the sentinel if there was one.
21515312Smg147109 		 */
21525312Smg147109 		if (hqueue->next != NULL) {
21535312Smg147109 			avl_remove(&hqueue->read_tree, hqueue->next);
21545312Smg147109 			kmem_cache_free(hio_cache, hqueue->next);
21555312Smg147109 			hqueue->next = NULL;
21565312Smg147109 		}
21575312Smg147109 		mutex_exit(&hqueue->hsfs_queue_lock);
21585312Smg147109 		mutex_exit(&hqueue->strategy_lock);
21595312Smg147109 		return (1);
21605312Smg147109 	}
21615312Smg147109 
21625312Smg147109 	if (drv_hztousec(ddi_get_lbolt()) - fio->io_timestamp
21635312Smg147109 	    < HSFS_READ_DEADLINE) {
21645312Smg147109 		/*
21655312Smg147109 		 * Apply standard scheduling logic. This uses the
21665312Smg147109 		 * C-LOOK approach. Process I/O requests in ascending
21675312Smg147109 		 * order of logical block address till no subsequent
21685312Smg147109 		 * higher numbered block request remains. Then start
21695312Smg147109 		 * again from the lowest numbered block in the queue.
21705312Smg147109 		 *
21715312Smg147109 		 * We do this cheaply here by means of a sentinel.
21725312Smg147109 		 * The last processed I/O structure from the previous
21735312Smg147109 		 * invocation of this func, is left dangling in the
21745312Smg147109 		 * read_tree so that we can easily scan to the next
21755312Smg147109 		 * higher numbered request and remove the sentinel.
21765312Smg147109 		 */
21775312Smg147109 		fio = NULL;
21785312Smg147109 		if (hqueue->next != NULL) {
21795312Smg147109 			fio = AVL_NEXT(&hqueue->read_tree, hqueue->next);
21805312Smg147109 			avl_remove(&hqueue->read_tree, hqueue->next);
21815312Smg147109 			kmem_cache_free(hio_cache, hqueue->next);
21825312Smg147109 			hqueue->next = NULL;
21835312Smg147109 		}
21845312Smg147109 		if (fio == NULL) {
21855312Smg147109 			fio = avl_first(&hqueue->read_tree);
21865312Smg147109 		}
21875312Smg147109 	} else if (hqueue->next != NULL) {
21885312Smg147109 		DTRACE_PROBE1(hsfs_deadline_expiry, struct hio *, fio);
21895312Smg147109 
21905312Smg147109 		avl_remove(&hqueue->read_tree, hqueue->next);
21915312Smg147109 		kmem_cache_free(hio_cache, hqueue->next);
21925312Smg147109 		hqueue->next = NULL;
21935312Smg147109 	}
21945312Smg147109 
21955312Smg147109 	/*
21965312Smg147109 	 * In addition we try to coalesce contiguous
21975312Smg147109 	 * requests into one bigger request.
21985312Smg147109 	 */
21995312Smg147109 	bufcount = 1;
22005312Smg147109 	bsize = ldbtob(fio->nblocks);
22015312Smg147109 	fvp = fio->bp->b_file;
22025312Smg147109 	nio = AVL_NEXT(&hqueue->read_tree, fio);
22035312Smg147109 	tio = fio;
22045312Smg147109 	while (nio != NULL && IS_ADJACENT(tio, nio) &&
22055312Smg147109 	    bsize < hqueue->dev_maxtransfer) {
22065312Smg147109 		avl_remove(&hqueue->deadline_tree, tio);
22075312Smg147109 		avl_remove(&hqueue->read_tree, tio);
22085312Smg147109 		tio->contig_chain = nio;
22095312Smg147109 		bsize += ldbtob(nio->nblocks);
22105312Smg147109 		prev = tio;
22115312Smg147109 		tio = nio;
22125312Smg147109 
22135312Smg147109 		/*
22145312Smg147109 		 * This check is required to detect the case where
22155312Smg147109 		 * we are merging adjacent buffers belonging to
22165312Smg147109 		 * different files. fvp is used to set the b_file
22175312Smg147109 		 * parameter in the coalesced buf. b_file is used
22185312Smg147109 		 * by DTrace so we do not want DTrace to accrue
22195312Smg147109 		 * requests to two different files to any one file.
22205312Smg147109 		 */
22215312Smg147109 		if (fvp && tio->bp->b_file != fvp) {
22225312Smg147109 			fvp = NULL;
22235312Smg147109 		}
22245312Smg147109 
22255312Smg147109 		nio = AVL_NEXT(&hqueue->read_tree, nio);
22265312Smg147109 		bufcount++;
22275312Smg147109 	}
22285312Smg147109 
22295312Smg147109 	/*
22305312Smg147109 	 * tio is not removed from the read_tree as it serves as a sentinel
22315312Smg147109 	 * to cheaply allow us to scan to the next higher numbered I/O
22325312Smg147109 	 * request.
22335312Smg147109 	 */
22345312Smg147109 	hqueue->next = tio;
22355312Smg147109 	avl_remove(&hqueue->deadline_tree, tio);
22365312Smg147109 	mutex_exit(&hqueue->hsfs_queue_lock);
22375312Smg147109 	DTRACE_PROBE3(hsfs_io_dequeued, struct hio *, fio, int, bufcount,
22385312Smg147109 	    size_t, bsize);
22395312Smg147109 
22405312Smg147109 	/*
22415312Smg147109 	 * The benefit of coalescing occurs if the the savings in I/O outweighs
22425312Smg147109 	 * the cost of doing the additional work below.
22435312Smg147109 	 * It was observed that coalescing 2 buffers results in diminishing
22445312Smg147109 	 * returns, so we do coalescing if we have >2 adjacent bufs.
22455312Smg147109 	 */
22465312Smg147109 	if (bufcount > hsched_coalesce_min) {
22475312Smg147109 		/*
22485312Smg147109 		 * We have coalesced blocks. First allocate mem and buf for
22495312Smg147109 		 * the entire coalesced chunk.
22505312Smg147109 		 * Since we are guaranteed single-threaded here we pre-allocate
22515312Smg147109 		 * one buf at mount time and that is re-used every time. This
22525312Smg147109 		 * is a synthesized buf structure that uses kmem_alloced chunk.
22535312Smg147109 		 * Not quite a normal buf attached to pages.
22545312Smg147109 		 */
22555312Smg147109 		fsp->coalesced_bytes += bsize;
22565312Smg147109 		nbuf = hqueue->nbuf;
22575312Smg147109 		bioinit(nbuf);
22585312Smg147109 		nbuf->b_edev = fio->bp->b_edev;
22595312Smg147109 		nbuf->b_dev = fio->bp->b_dev;
22605312Smg147109 		nbuf->b_flags = fio->bp->b_flags;
22615312Smg147109 		nbuf->b_iodone = fio->bp->b_iodone;
22625312Smg147109 		iodata = kmem_alloc(bsize, KM_SLEEP);
22635312Smg147109 		nbuf->b_un.b_addr = iodata;
22645312Smg147109 		nbuf->b_lblkno = fio->bp->b_lblkno;
22655312Smg147109 		nbuf->b_vp = fvp;
22665312Smg147109 		nbuf->b_file = fvp;
22675312Smg147109 		nbuf->b_bcount = bsize;
22685312Smg147109 		nbuf->b_bufsize = bsize;
22695312Smg147109 		nbuf->b_resid = bsize;
22705312Smg147109 
22715312Smg147109 		DTRACE_PROBE3(hsfs_coalesced_io_start, struct hio *, fio, int,
22725312Smg147109 		    bufcount, size_t, bsize);
22735312Smg147109 
22745312Smg147109 		/*
22755312Smg147109 		 * Perform I/O for the coalesced block.
22765312Smg147109 		 */
22775312Smg147109 		(void) bdev_strategy(nbuf);
22785312Smg147109 
22795312Smg147109 		/*
22805312Smg147109 		 * Duplicate the last IO node to leave the sentinel alone.
22815312Smg147109 		 * The sentinel is freed in the next invocation of this
22825312Smg147109 		 * function.
22835312Smg147109 		 */
22845312Smg147109 		prev->contig_chain = kmem_cache_alloc(hio_cache, KM_SLEEP);
22855312Smg147109 		prev->contig_chain->bp = tio->bp;
22865312Smg147109 		prev->contig_chain->sema = tio->sema;
22875312Smg147109 		tio = prev->contig_chain;
22885312Smg147109 		tio->contig_chain = NULL;
22895312Smg147109 		soffset = ldbtob(fio->bp->b_lblkno);
22905312Smg147109 		nio = fio;
22915312Smg147109 
22925312Smg147109 		bioret = biowait(nbuf);
22935312Smg147109 		data = bsize - nbuf->b_resid;
22945312Smg147109 		biofini(nbuf);
22955312Smg147109 		mutex_exit(&hqueue->strategy_lock);
22965312Smg147109 
22975312Smg147109 		/*
22985312Smg147109 		 * We use the b_resid parameter to detect how much
22995312Smg147109 		 * data was succesfully transferred. We will signal
23005312Smg147109 		 * a success to all the fully retrieved actual bufs
23015312Smg147109 		 * before coalescing, rest is signaled as error,
23025312Smg147109 		 * if any.
23035312Smg147109 		 */
23045312Smg147109 		tio = nio;
23055312Smg147109 		DTRACE_PROBE3(hsfs_coalesced_io_done, struct hio *, nio,
23065312Smg147109 		    int, bioret, size_t, data);
23075312Smg147109 
23085312Smg147109 		/*
23095312Smg147109 		 * Copy data and signal success to all the bufs
23105312Smg147109 		 * which can be fully satisfied from b_resid.
23115312Smg147109 		 */
23125312Smg147109 		while (nio != NULL && data >= nio->bp->b_bcount) {
23135312Smg147109 			offset = ldbtob(nio->bp->b_lblkno) - soffset;
23145312Smg147109 			bcopy(iodata + offset, nio->bp->b_un.b_addr,
23155312Smg147109 			    nio->bp->b_bcount);
23165312Smg147109 			data -= nio->bp->b_bcount;
23175312Smg147109 			bioerror(nio->bp, 0);
23185312Smg147109 			biodone(nio->bp);
23195312Smg147109 			sema_v(nio->sema);
23205312Smg147109 			tio = nio;
23215312Smg147109 			nio = nio->contig_chain;
23225312Smg147109 			kmem_cache_free(hio_cache, tio);
23235312Smg147109 		}
23245312Smg147109 
23255312Smg147109 		/*
23265312Smg147109 		 * Signal error to all the leftover bufs (if any)
23275312Smg147109 		 * after b_resid data is exhausted.
23285312Smg147109 		 */
23295312Smg147109 		while (nio != NULL) {
23305312Smg147109 			nio->bp->b_resid = nio->bp->b_bcount - data;
23315312Smg147109 			bzero(nio->bp->b_un.b_addr + data, nio->bp->b_resid);
23325312Smg147109 			bioerror(nio->bp, bioret);
23335312Smg147109 			biodone(nio->bp);
23345312Smg147109 			sema_v(nio->sema);
23355312Smg147109 			tio = nio;
23365312Smg147109 			nio = nio->contig_chain;
23375312Smg147109 			kmem_cache_free(hio_cache, tio);
23385312Smg147109 			data = 0;
23395312Smg147109 		}
23405312Smg147109 		kmem_free(iodata, bsize);
23415312Smg147109 	} else {
23425312Smg147109 
23435312Smg147109 		nbuf = tio->bp;
23445312Smg147109 		io_done = tio->sema;
23455312Smg147109 		nio = fio;
23465312Smg147109 		last = tio;
23475312Smg147109 
23485312Smg147109 		while (nio != NULL) {
23495312Smg147109 			(void) bdev_strategy(nio->bp);
23505312Smg147109 			nio = nio->contig_chain;
23515312Smg147109 		}
23525312Smg147109 		nio = fio;
23535312Smg147109 		mutex_exit(&hqueue->strategy_lock);
23545312Smg147109 
23555312Smg147109 		while (nio != NULL) {
23565312Smg147109 			if (nio == last) {
23575312Smg147109 				(void) biowait(nbuf);
23585312Smg147109 				sema_v(io_done);
23595312Smg147109 				break;
23605312Smg147109 				/* sentinel last not freed. See above. */
23615312Smg147109 			} else {
23625312Smg147109 				(void) biowait(nio->bp);
23635312Smg147109 				sema_v(nio->sema);
23645312Smg147109 			}
23655312Smg147109 			tio = nio;
23665312Smg147109 			nio = nio->contig_chain;
23675312Smg147109 			kmem_cache_free(hio_cache, tio);
23685312Smg147109 		}
23695312Smg147109 	}
23705312Smg147109 	return (0);
23715312Smg147109 }
23725312Smg147109 
23735312Smg147109 /*
23745312Smg147109  * Insert an I/O request in the I/O scheduler's pipeline
23755312Smg147109  * Using AVL tree makes it easy to reorder the I/O request
23765312Smg147109  * based on logical block number.
23775312Smg147109  */
23785312Smg147109 static void
23795312Smg147109 hsched_enqueue_io(struct hsfs *fsp, struct hio *hsio, int ra)
23805312Smg147109 {
23815312Smg147109 	struct hsfs_queue *hqueue = fsp->hqueue;
23825312Smg147109 
23835312Smg147109 	mutex_enter(&hqueue->hsfs_queue_lock);
23845312Smg147109 
23855312Smg147109 	fsp->physical_read_bytes += hsio->bp->b_bcount;
23865312Smg147109 	if (ra)
23875312Smg147109 		fsp->readahead_bytes += hsio->bp->b_bcount;
23885312Smg147109 
23895312Smg147109 	avl_add(&hqueue->deadline_tree, hsio);
23905312Smg147109 	avl_add(&hqueue->read_tree, hsio);
23915312Smg147109 
23925312Smg147109 	DTRACE_PROBE3(hsfs_io_enqueued, struct hio *, hsio,
23935312Smg147109 	    struct hsfs_queue *, hqueue, int, ra);
23945312Smg147109 
23955312Smg147109 	mutex_exit(&hqueue->hsfs_queue_lock);
23965312Smg147109 }
23975312Smg147109 
23982900Sfrankho /* ARGSUSED */
23992900Sfrankho static int
2400*5331Samw hsfs_pathconf(struct vnode *vp,
2401*5331Samw 	int cmd,
2402*5331Samw 	ulong_t *valp,
2403*5331Samw 	struct cred *cr,
2404*5331Samw 	caller_context_t *ct)
24052900Sfrankho {
24062900Sfrankho 	struct hsfs	*fsp;
24072900Sfrankho 
24082900Sfrankho 	int		error = 0;
24092900Sfrankho 
24102900Sfrankho 	switch (cmd) {
24112900Sfrankho 
24122900Sfrankho 	case _PC_NAME_MAX:
24132900Sfrankho 		fsp = VFS_TO_HSFS(vp->v_vfsp);
24142900Sfrankho 		*valp = fsp->hsfs_namemax;
24152900Sfrankho 		break;
24162900Sfrankho 
24172900Sfrankho 	case _PC_FILESIZEBITS:
24182900Sfrankho 		*valp = 33;	/* Without multi extent support: 4 GB - 2k */
24192900Sfrankho 		break;
24202900Sfrankho 
24212900Sfrankho 	default:
2422*5331Samw 		error = fs_pathconf(vp, cmd, valp, cr, ct);
24232900Sfrankho 	}
24242900Sfrankho 
24252900Sfrankho 	return (error);
24262900Sfrankho }
24272900Sfrankho 
24282900Sfrankho 
24292900Sfrankho 
24300Sstevel@tonic-gate const fs_operation_def_t hsfs_vnodeops_template[] = {
24313898Srsb 	VOPNAME_OPEN,		{ .vop_open = hsfs_open },
24323898Srsb 	VOPNAME_CLOSE,		{ .vop_close = hsfs_close },
24333898Srsb 	VOPNAME_READ,		{ .vop_read = hsfs_read },
24343898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = hsfs_getattr },
24353898Srsb 	VOPNAME_ACCESS,		{ .vop_access = hsfs_access },
24363898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = hsfs_lookup },
24373898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = hsfs_readdir },
24383898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = hsfs_readlink },
24393898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = hsfs_fsync },
24403898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = hsfs_inactive },
24413898Srsb 	VOPNAME_FID,		{ .vop_fid = hsfs_fid },
24423898Srsb 	VOPNAME_SEEK,		{ .vop_seek = hsfs_seek },
24433898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = hsfs_frlock },
24443898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = hsfs_getpage },
24453898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = hsfs_putpage },
24463898Srsb 	VOPNAME_MAP,		{ .vop_map = hsfs_map },
24473898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = hsfs_addmap },
24483898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = hsfs_delmap },
24493898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = hsfs_pathconf },
24503898Srsb 	NULL,			NULL
24510Sstevel@tonic-gate };
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate struct vnodeops *hsfs_vnodeops;
2454