17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52caf0dcdSrshoaib  * Common Development and Distribution License (the "License").
62caf0dcdSrshoaib  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
212caf0dcdSrshoaib 
227c478bd9Sstevel@tonic-gate /*
23311e3d4cSgeorges  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/buf.h>
327c478bd9Sstevel@tonic-gate #include <sys/conf.h>
337c478bd9Sstevel@tonic-gate #include <sys/cred.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
377c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
387c478bd9Sstevel@tonic-gate #include <sys/debug.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/time.h>
417c478bd9Sstevel@tonic-gate #include <sys/file.h>
427c478bd9Sstevel@tonic-gate #include <sys/open.h>
437c478bd9Sstevel@tonic-gate #include <sys/user.h>
447c478bd9Sstevel@tonic-gate #include <sys/termios.h>
457c478bd9Sstevel@tonic-gate #include <sys/stream.h>
467c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
4774024373Spr #include <sys/sunddi.h>
487c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
497c478bd9Sstevel@tonic-gate #include <sys/flock.h>
507c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
517c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
527c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <sys/socket.h>
557c478bd9Sstevel@tonic-gate #include <sys/socketvar.h>
5645916cd2Sjpk 
577c478bd9Sstevel@tonic-gate #include <netinet/in.h>
587c478bd9Sstevel@tonic-gate #include <sys/sendfile.h>
597c478bd9Sstevel@tonic-gate #include <sys/un.h>
607c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
617c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <inet/common.h>
647c478bd9Sstevel@tonic-gate #include <inet/ip.h>
657c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
667c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate extern int sosendfile64(file_t *, file_t *, const struct ksendfilevec64 *,
697c478bd9Sstevel@tonic-gate 		ssize32_t *);
702c9e429eSbrutus extern int nl7c_sendfilev(struct sonode *, u_offset_t *, struct sendfilevec *,
712c9e429eSbrutus 		int, ssize_t *);
723b3d24f3SJayakara Kini extern int snf_segmap(file_t *, vnode_t *, u_offset_t, u_offset_t, ssize_t *,
733b3d24f3SJayakara Kini 		boolean_t);
7474024373Spr 
7574024373Spr #define	readflg	(V_WRITELOCK_FALSE)
7674024373Spr #define	rwflag	(V_WRITELOCK_TRUE)
777c478bd9Sstevel@tonic-gate 
78ff550d0eSmasputra /*
79ff550d0eSmasputra  * kstrwritemp() has very similar semantics as that of strwrite().
80ff550d0eSmasputra  * The main difference is it obtains mblks from the caller and also
81ff550d0eSmasputra  * does not do any copy as done in strwrite() from user buffers to
82ff550d0eSmasputra  * kernel buffers.
83ff550d0eSmasputra  *
84ff550d0eSmasputra  * Currently, this routine is used by sendfile to send data allocated
85ff550d0eSmasputra  * within the kernel without any copying. This interface does not use the
86ff550d0eSmasputra  * synchronous stream interface as synch. stream interface implies
87ff550d0eSmasputra  * copying.
88ff550d0eSmasputra  */
89ff550d0eSmasputra int
90ff550d0eSmasputra kstrwritemp(struct vnode *vp, mblk_t *mp, ushort_t fmode)
91ff550d0eSmasputra {
92ff550d0eSmasputra 	struct stdata *stp;
93ff550d0eSmasputra 	struct queue *wqp;
94c28749e9Skais 	mblk_t *newmp;
95ff550d0eSmasputra 	char waitflag;
96ff550d0eSmasputra 	int tempmode;
97ff550d0eSmasputra 	int error = 0;
98ff550d0eSmasputra 	int done = 0;
99ff550d0eSmasputra 	struct sonode *so;
100ff550d0eSmasputra 	boolean_t direct;
101ff550d0eSmasputra 
102ff550d0eSmasputra 	ASSERT(vp->v_stream);
103ff550d0eSmasputra 	stp = vp->v_stream;
104ff550d0eSmasputra 
105ff550d0eSmasputra 	so = VTOSO(vp);
106ff550d0eSmasputra 	direct = (so->so_state & SS_DIRECT);
107ff550d0eSmasputra 
108ff550d0eSmasputra 	/*
109ff550d0eSmasputra 	 * This is the sockfs direct fast path. canputnext() need
110ff550d0eSmasputra 	 * not be accurate so we don't grab the sd_lock here. If
111ff550d0eSmasputra 	 * we get flow-controlled, we grab sd_lock just before the
112ff550d0eSmasputra 	 * do..while loop below to emulate what strwrite() does.
113ff550d0eSmasputra 	 */
114ff550d0eSmasputra 	wqp = stp->sd_wrq;
115ff550d0eSmasputra 	if (canputnext(wqp) && direct &&
116ff550d0eSmasputra 	    !(stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
117ff550d0eSmasputra 		return (sostream_direct(so, NULL, mp, CRED()));
118ff550d0eSmasputra 	} else if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
119ff550d0eSmasputra 		/* Fast check of flags before acquiring the lock */
120ff550d0eSmasputra 		mutex_enter(&stp->sd_lock);
121ff550d0eSmasputra 		error = strgeterr(stp, STWRERR|STRHUP|STPLEX, 0);
122ff550d0eSmasputra 		mutex_exit(&stp->sd_lock);
123ff550d0eSmasputra 		if (error != 0) {
124ff550d0eSmasputra 			if (!(stp->sd_flag & STPLEX) &&
125ff550d0eSmasputra 			    (stp->sd_wput_opt & SW_SIGPIPE)) {
126ff550d0eSmasputra 				tsignal(curthread, SIGPIPE);
127ff550d0eSmasputra 				error = EPIPE;
128ff550d0eSmasputra 			}
129ff550d0eSmasputra 			return (error);
130ff550d0eSmasputra 		}
131ff550d0eSmasputra 	}
132ff550d0eSmasputra 
133ff550d0eSmasputra 	waitflag = WRITEWAIT;
134ff550d0eSmasputra 	if (stp->sd_flag & OLDNDELAY)
135ff550d0eSmasputra 		tempmode = fmode & ~FNDELAY;
136ff550d0eSmasputra 	else
137ff550d0eSmasputra 		tempmode = fmode;
138ff550d0eSmasputra 
139ff550d0eSmasputra 	mutex_enter(&stp->sd_lock);
140ff550d0eSmasputra 	do {
141ff550d0eSmasputra 		if (canputnext(wqp)) {
142ff550d0eSmasputra 			mutex_exit(&stp->sd_lock);
143c28749e9Skais 			if (stp->sd_wputdatafunc != NULL) {
144c28749e9Skais 				newmp = (stp->sd_wputdatafunc)(vp, mp, NULL,
145c28749e9Skais 				    NULL, NULL, NULL);
146c28749e9Skais 				if (newmp == NULL) {
147c28749e9Skais 					/* The caller will free mp */
148c28749e9Skais 					return (ECOMM);
149c28749e9Skais 				}
150c28749e9Skais 				mp = newmp;
151c28749e9Skais 			}
152ff550d0eSmasputra 			putnext(wqp, mp);
153ff550d0eSmasputra 			return (0);
154ff550d0eSmasputra 		}
155ff550d0eSmasputra 		error = strwaitq(stp, waitflag, (ssize_t)0, tempmode, -1,
156ff550d0eSmasputra 		    &done);
157ff550d0eSmasputra 	} while (error == 0 && !done);
158ff550d0eSmasputra 
159ff550d0eSmasputra 	mutex_exit(&stp->sd_lock);
160ff550d0eSmasputra 	/*
161ff550d0eSmasputra 	 * EAGAIN tells the application to try again. ENOMEM
162ff550d0eSmasputra 	 * is returned only if the memory allocation size
163ff550d0eSmasputra 	 * exceeds the physical limits of the system. ENOMEM
164ff550d0eSmasputra 	 * can't be true here.
165ff550d0eSmasputra 	 */
166ff550d0eSmasputra 	if (error == ENOMEM)
167ff550d0eSmasputra 		error = EAGAIN;
168ff550d0eSmasputra 	return (error);
169ff550d0eSmasputra }
170ff550d0eSmasputra 
1717c478bd9Sstevel@tonic-gate #define	SEND_MAX_CHUNK	16
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * 64 bit offsets for 32 bit applications only running either on
1767c478bd9Sstevel@tonic-gate  * 64 bit kernel or 32 bit kernel. For 32 bit apps, we can't transfer
1777c478bd9Sstevel@tonic-gate  * more than 2GB of data.
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate int
1807c478bd9Sstevel@tonic-gate sendvec_chunk64(file_t *fp, u_offset_t *fileoff, struct ksendfilevec64 *sfv,
1817c478bd9Sstevel@tonic-gate     int copy_cnt, ssize32_t *count)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	struct vnode *vp;
1847c478bd9Sstevel@tonic-gate 	ushort_t fflag;
1857c478bd9Sstevel@tonic-gate 	int ioflag;
1867c478bd9Sstevel@tonic-gate 	size32_t cnt;
1877c478bd9Sstevel@tonic-gate 	ssize32_t sfv_len;
1887c478bd9Sstevel@tonic-gate 	ssize32_t tmpcount;
1897c478bd9Sstevel@tonic-gate 	u_offset_t sfv_off;
1907c478bd9Sstevel@tonic-gate 	struct uio auio;
1917c478bd9Sstevel@tonic-gate 	struct iovec aiov;
1927c478bd9Sstevel@tonic-gate 	int i, error;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
1957c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
1967c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING))
1997c478bd9Sstevel@tonic-gate 			return (EINTR);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 		/*
2027c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
2037c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
2047c478bd9Sstevel@tonic-gate 		 */
2057c478bd9Sstevel@tonic-gate 		sfv_len = (ssize32_t)sfv->sfv_len;
2067c478bd9Sstevel@tonic-gate 
207*e116a42fSPrakash Jalan 		if (sfv_len == 0) {
208*e116a42fSPrakash Jalan 			sfv++;
2097c478bd9Sstevel@tonic-gate 			continue;
210*e116a42fSPrakash Jalan 		}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		if (sfv_len < 0)
2137c478bd9Sstevel@tonic-gate 			return (EINVAL);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG) {
2167c478bd9Sstevel@tonic-gate 			if (*fileoff >= curproc->p_fsz_ctl) {
2177c478bd9Sstevel@tonic-gate 				mutex_enter(&curproc->p_lock);
2187c478bd9Sstevel@tonic-gate 				(void) rctl_action(
2197c478bd9Sstevel@tonic-gate 				    rctlproc_legacy[RLIMIT_FSIZE],
2207c478bd9Sstevel@tonic-gate 				    curproc->p_rctls, curproc, RCA_SAFE);
2217c478bd9Sstevel@tonic-gate 				mutex_exit(&curproc->p_lock);
2227c478bd9Sstevel@tonic-gate 				return (EFBIG);
2237c478bd9Sstevel@tonic-gate 			}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 			if (*fileoff >= OFFSET_MAX(fp))
2267c478bd9Sstevel@tonic-gate 				return (EFBIG);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 			if (*fileoff + sfv_len > OFFSET_MAX(fp))
2297c478bd9Sstevel@tonic-gate 				return (EINVAL);
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		tmpcount = *count + sfv_len;
2337c478bd9Sstevel@tonic-gate 		if (tmpcount < 0)
2347c478bd9Sstevel@tonic-gate 			return (EINVAL);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		sfv_off = sfv->sfv_off;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_DEFAULT;
2397c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
2407c478bd9Sstevel@tonic-gate 			aiov.iov_len = sfv_len;
2417c478bd9Sstevel@tonic-gate 			aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
2427c478bd9Sstevel@tonic-gate 			auio.uio_loffset = *fileoff;
2437c478bd9Sstevel@tonic-gate 			auio.uio_iovcnt = 1;
2447c478bd9Sstevel@tonic-gate 			auio.uio_resid = sfv_len;
2457c478bd9Sstevel@tonic-gate 			auio.uio_iov = &aiov;
2467c478bd9Sstevel@tonic-gate 			auio.uio_segflg = UIO_USERSPACE;
2477c478bd9Sstevel@tonic-gate 			auio.uio_llimit = curproc->p_fsz_ctl;
2487c478bd9Sstevel@tonic-gate 			auio.uio_fmode = fflag;
2497c478bd9Sstevel@tonic-gate 			ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
2507c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
2517c478bd9Sstevel@tonic-gate 				error = VOP_WRITE(vp, &auio, ioflag,
2527c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
2537c478bd9Sstevel@tonic-gate 				cnt = sfv_len - auio.uio_resid;
2547c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
2557c478bd9Sstevel@tonic-gate 				ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
2567c478bd9Sstevel@tonic-gate 				if (vp->v_type == VREG)
2577c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
2587c478bd9Sstevel@tonic-gate 				*count += cnt;
2597c478bd9Sstevel@tonic-gate 				if (error != 0)
2607c478bd9Sstevel@tonic-gate 					return (error);
2617c478bd9Sstevel@tonic-gate 			}
2627c478bd9Sstevel@tonic-gate 		} else {
2637c478bd9Sstevel@tonic-gate 			file_t	*ffp;
2647c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
2657c478bd9Sstevel@tonic-gate 			size_t	size;
2667c478bd9Sstevel@tonic-gate 			caddr_t	ptr;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL)
2697c478bd9Sstevel@tonic-gate 				return (EBADF);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
2727c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
2737c478bd9Sstevel@tonic-gate 				return (EBADF);
2747c478bd9Sstevel@tonic-gate 			}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
2777c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
2787c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
2797c478bd9Sstevel@tonic-gate 				return (EINVAL);
2807c478bd9Sstevel@tonic-gate 			}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 			/*
2837c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
2847c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
2857c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
2867c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
2877c478bd9Sstevel@tonic-gate 			 */
288bfb244baSpr 			if (vn_compare(vp, readvp)) {
2897c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
2907c478bd9Sstevel@tonic-gate 				return (EINVAL);
2917c478bd9Sstevel@tonic-gate 			}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 			/*
2947c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
2957c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
2967c478bd9Sstevel@tonic-gate 			 */
2977c478bd9Sstevel@tonic-gate 			(void) VOP_RWLOCK(readvp, readflg, NULL);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 			/*
3007c478bd9Sstevel@tonic-gate 			 * Same checks as in pread64.
3017c478bd9Sstevel@tonic-gate 			 */
3027c478bd9Sstevel@tonic-gate 			if (sfv_off > MAXOFFSET_T) {
3037c478bd9Sstevel@tonic-gate 				VOP_RWUNLOCK(readvp, readflg, NULL);
3047c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
3057c478bd9Sstevel@tonic-gate 				return (EINVAL);
3067c478bd9Sstevel@tonic-gate 			}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 			if (sfv_off + sfv_len > MAXOFFSET_T)
3097c478bd9Sstevel@tonic-gate 				sfv_len = (ssize32_t)(MAXOFFSET_T - sfv_off);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 			/* Find the native blocksize to transfer data */
3127c478bd9Sstevel@tonic-gate 			size = MIN(vp->v_vfsp->vfs_bsize,
3137c478bd9Sstevel@tonic-gate 			    readvp->v_vfsp->vfs_bsize);
3147c478bd9Sstevel@tonic-gate 			size = sfv_len < size ? sfv_len : size;
3157c478bd9Sstevel@tonic-gate 			ptr = kmem_alloc(size, KM_SLEEP);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
3187c478bd9Sstevel@tonic-gate 				size_t	iov_len;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 				iov_len = MIN(size, sfv_len);
3217c478bd9Sstevel@tonic-gate 				aiov.iov_base = ptr;
3227c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
3237c478bd9Sstevel@tonic-gate 				auio.uio_loffset = sfv_off;
3247c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
3257c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
3267c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
3277c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
3287c478bd9Sstevel@tonic-gate 				auio.uio_llimit = MAXOFFSET_T;
3297c478bd9Sstevel@tonic-gate 				auio.uio_fmode = ffp->f_flag;
3307c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
3317c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 				/*
3347c478bd9Sstevel@tonic-gate 				 * If read sync is not asked for,
3357c478bd9Sstevel@tonic-gate 				 * filter sync flags
3367c478bd9Sstevel@tonic-gate 				 */
3377c478bd9Sstevel@tonic-gate 				if ((ioflag & FRSYNC) == 0)
3387c478bd9Sstevel@tonic-gate 					ioflag &= ~(FSYNC|FDSYNC);
3397c478bd9Sstevel@tonic-gate 				error = VOP_READ(readvp, &auio, ioflag,
3407c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
3417c478bd9Sstevel@tonic-gate 				if (error) {
3427c478bd9Sstevel@tonic-gate 					kmem_free(ptr, size);
3437c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
3447c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
3457c478bd9Sstevel@tonic-gate 					return (error);
3467c478bd9Sstevel@tonic-gate 				}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 				/*
3497c478bd9Sstevel@tonic-gate 				 * Check how must data was really read.
3507c478bd9Sstevel@tonic-gate 				 * Decrement the 'len' and increment the
3517c478bd9Sstevel@tonic-gate 				 * 'off' appropriately.
3527c478bd9Sstevel@tonic-gate 				 */
3537c478bd9Sstevel@tonic-gate 				cnt = iov_len - auio.uio_resid;
3547c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
3557c478bd9Sstevel@tonic-gate 					/*
3567c478bd9Sstevel@tonic-gate 					 * If we were reading a pipe (currently
3577c478bd9Sstevel@tonic-gate 					 * not implemented), we may now lose
3587c478bd9Sstevel@tonic-gate 					 * data.
3597c478bd9Sstevel@tonic-gate 					 */
3607c478bd9Sstevel@tonic-gate 					kmem_free(ptr, size);
3617c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
3627c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
3637c478bd9Sstevel@tonic-gate 					return (EINVAL);
3647c478bd9Sstevel@tonic-gate 				}
3657c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
3667c478bd9Sstevel@tonic-gate 				sfv_off += cnt;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 				aiov.iov_base = ptr;
3697c478bd9Sstevel@tonic-gate 				aiov.iov_len = cnt;
3707c478bd9Sstevel@tonic-gate 				auio.uio_loffset = *fileoff;
371a490cd42SPrakash Jalan 				auio.uio_iov = &aiov;
372a490cd42SPrakash Jalan 				auio.uio_iovcnt = 1;
3737c478bd9Sstevel@tonic-gate 				auio.uio_resid = cnt;
3747c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
3757c478bd9Sstevel@tonic-gate 				auio.uio_llimit = curproc->p_fsz_ctl;
3767c478bd9Sstevel@tonic-gate 				auio.uio_fmode = fflag;
3777c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
3787c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
3797c478bd9Sstevel@tonic-gate 				error = VOP_WRITE(vp, &auio, ioflag,
3807c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 				/*
3837c478bd9Sstevel@tonic-gate 				 * Check how much data was written. Increment
3847c478bd9Sstevel@tonic-gate 				 * the 'len' and decrement the 'off' if all
3857c478bd9Sstevel@tonic-gate 				 * the data was not written.
3867c478bd9Sstevel@tonic-gate 				 */
3877c478bd9Sstevel@tonic-gate 				cnt -= auio.uio_resid;
3887c478bd9Sstevel@tonic-gate 				sfv_len += auio.uio_resid;
3897c478bd9Sstevel@tonic-gate 				sfv_off -= auio.uio_resid;
3907c478bd9Sstevel@tonic-gate 				ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
3917c478bd9Sstevel@tonic-gate 				if (vp->v_type == VREG)
3927c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
3937c478bd9Sstevel@tonic-gate 				*count += cnt;
3947c478bd9Sstevel@tonic-gate 				if (error != 0) {
3957c478bd9Sstevel@tonic-gate 					kmem_free(ptr, size);
3967c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
3977c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
3987c478bd9Sstevel@tonic-gate 					return (error);
3997c478bd9Sstevel@tonic-gate 				}
4007c478bd9Sstevel@tonic-gate 			}
4017c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(readvp, readflg, NULL);
4027c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
4037c478bd9Sstevel@tonic-gate 			kmem_free(ptr, size);
4047c478bd9Sstevel@tonic-gate 		}
4057c478bd9Sstevel@tonic-gate 		sfv++;
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 	return (0);
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate ssize32_t
4117c478bd9Sstevel@tonic-gate sendvec64(file_t *fp, const struct ksendfilevec64 *vec, int sfvcnt,
4127c478bd9Sstevel@tonic-gate 	size32_t *xferred, int fildes)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	u_offset_t		fileoff;
4157c478bd9Sstevel@tonic-gate 	int			copy_cnt;
4167c478bd9Sstevel@tonic-gate 	const struct ksendfilevec64 *copy_vec;
4177c478bd9Sstevel@tonic-gate 	struct ksendfilevec64 sfv[SEND_MAX_CHUNK];
4187c478bd9Sstevel@tonic-gate 	struct vnode *vp;
4197c478bd9Sstevel@tonic-gate 	int error;
4207c478bd9Sstevel@tonic-gate 	ssize32_t count = 0;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
4237c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	copy_vec = vec;
4267c478bd9Sstevel@tonic-gate 	fileoff = fp->f_offset;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	do {
4297c478bd9Sstevel@tonic-gate 		copy_cnt = MIN(sfvcnt, SEND_MAX_CHUNK);
4307c478bd9Sstevel@tonic-gate 		if (copyin(copy_vec, sfv, copy_cnt *
4317c478bd9Sstevel@tonic-gate 		    sizeof (struct ksendfilevec64))) {
4327c478bd9Sstevel@tonic-gate 			error = EFAULT;
4337c478bd9Sstevel@tonic-gate 			break;
4347c478bd9Sstevel@tonic-gate 		}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		/*
43774024373Spr 		 * Optimize the regular file over
4387c478bd9Sstevel@tonic-gate 		 * the socket case.
4397c478bd9Sstevel@tonic-gate 		 */
44074024373Spr 		if (vp->v_type == VSOCK && sfv->sfv_fd != SFV_FD_SELF) {
4417c478bd9Sstevel@tonic-gate 			file_t *rfp;
4427c478bd9Sstevel@tonic-gate 			vnode_t *rvp;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 			if ((rfp = getf(sfv->sfv_fd)) == NULL) {
4457c478bd9Sstevel@tonic-gate 				error = EBADF;
4467c478bd9Sstevel@tonic-gate 				break;
4477c478bd9Sstevel@tonic-gate 			}
4487c478bd9Sstevel@tonic-gate 			if ((rfp->f_flag & FREAD) == 0) {
4497c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
4507c478bd9Sstevel@tonic-gate 				error = EBADF;
4517c478bd9Sstevel@tonic-gate 				break;
4527c478bd9Sstevel@tonic-gate 			}
4537c478bd9Sstevel@tonic-gate 			rvp = rfp->f_vnode;
4547c478bd9Sstevel@tonic-gate 			if (rvp->v_type == VREG) {
4557c478bd9Sstevel@tonic-gate 				error = sosendfile64(fp, rfp, sfv, &count);
45674024373Spr 				if (error)
45774024373Spr 					break;
45874024373Spr 				copy_vec++;
45974024373Spr 				sfvcnt--;
46074024373Spr 				continue;
4617c478bd9Sstevel@tonic-gate 			}
4627c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 		error = sendvec_chunk64(fp, &fileoff, sfv, copy_cnt, &count);
4657c478bd9Sstevel@tonic-gate 		if (error != 0)
4667c478bd9Sstevel@tonic-gate 			break;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		copy_vec += copy_cnt;
4697c478bd9Sstevel@tonic-gate 		sfvcnt -= copy_cnt;
4707c478bd9Sstevel@tonic-gate 	} while (sfvcnt > 0);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG)
4737c478bd9Sstevel@tonic-gate 		fp->f_offset += count;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
4767c478bd9Sstevel@tonic-gate 	if (copyout(&count, xferred, sizeof (count)))
4777c478bd9Sstevel@tonic-gate 		error = EFAULT;
4787c478bd9Sstevel@tonic-gate 	releasef(fildes);
4797c478bd9Sstevel@tonic-gate 	if (error != 0)
4807c478bd9Sstevel@tonic-gate 		return (set_errno(error));
4817c478bd9Sstevel@tonic-gate 	return (count);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate #endif
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate int
4867c478bd9Sstevel@tonic-gate sendvec_small_chunk(file_t *fp, u_offset_t *fileoff, struct sendfilevec *sfv,
4877c478bd9Sstevel@tonic-gate     int copy_cnt, ssize_t total_size, int maxblk, ssize_t *count)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	struct vnode *vp;
4907c478bd9Sstevel@tonic-gate 	struct uio auio;
4917c478bd9Sstevel@tonic-gate 	struct iovec aiov;
4927c478bd9Sstevel@tonic-gate 	ushort_t fflag;
4937c478bd9Sstevel@tonic-gate 	int ioflag;
4947c478bd9Sstevel@tonic-gate 	int i, error;
4957c478bd9Sstevel@tonic-gate 	size_t cnt;
4967c478bd9Sstevel@tonic-gate 	ssize_t sfv_len;
4977c478bd9Sstevel@tonic-gate 	u_offset_t sfv_off;
4987c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4997c478bd9Sstevel@tonic-gate 	model_t model = get_udatamodel();
5007c478bd9Sstevel@tonic-gate 	u_offset_t maxoff = (model == DATAMODEL_ILP32) ?
501311e3d4cSgeorges 	    MAXOFF32_T : MAXOFFSET_T;
5027c478bd9Sstevel@tonic-gate #else
5037c478bd9Sstevel@tonic-gate 	const u_offset_t maxoff = MAXOFF32_T;
5047c478bd9Sstevel@tonic-gate #endif
5057c478bd9Sstevel@tonic-gate 	mblk_t *dmp = NULL;
5067c478bd9Sstevel@tonic-gate 	int wroff;
5077c478bd9Sstevel@tonic-gate 	int buf_left = 0;
5087c478bd9Sstevel@tonic-gate 	size_t	iov_len;
5097c478bd9Sstevel@tonic-gate 	mblk_t  *head, *tmp;
5107c478bd9Sstevel@tonic-gate 	size_t  size = total_size;
511c28749e9Skais 	size_t  extra;
512c28749e9Skais 	int tail_len;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
5157c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VSOCK);
5187c478bd9Sstevel@tonic-gate 	ASSERT(maxblk > 0);
5197c478bd9Sstevel@tonic-gate 
520*e116a42fSPrakash Jalan 	/* If nothing to send, return */
521*e116a42fSPrakash Jalan 	if (total_size == 0)
522*e116a42fSPrakash Jalan 		return (0);
523*e116a42fSPrakash Jalan 
5247c478bd9Sstevel@tonic-gate 	wroff = (int)vp->v_stream->sd_wroff;
525c28749e9Skais 	tail_len = (int)vp->v_stream->sd_tail;
526c28749e9Skais 	extra = wroff + tail_len;
527c28749e9Skais 
5287c478bd9Sstevel@tonic-gate 	buf_left = MIN(total_size, maxblk);
529c28749e9Skais 	head = dmp = allocb(buf_left + extra, BPRI_HI);
5307c478bd9Sstevel@tonic-gate 	if (head == NULL)
5317c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5327c478bd9Sstevel@tonic-gate 	head->b_wptr = head->b_rptr = head->b_rptr + wroff;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_DEFAULT;
5357c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
536311e3d4cSgeorges 		if (ISSIG(curthread, JUSTLOOKING)) {
537311e3d4cSgeorges 			freemsg(head);
5387c478bd9Sstevel@tonic-gate 			return (EINTR);
539311e3d4cSgeorges 		}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		/*
5427c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
5437c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
5447c478bd9Sstevel@tonic-gate 		 */
5457c478bd9Sstevel@tonic-gate 		sfv_len = (ssize_t)sfv->sfv_len;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		if (sfv_len == 0) {
5487c478bd9Sstevel@tonic-gate 			sfv++;
5497c478bd9Sstevel@tonic-gate 			continue;
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 		/* Check for overflow */
5537c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
5547c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_ILP32) {
555311e3d4cSgeorges 			if (((ssize32_t)(*count + sfv_len)) < 0) {
556311e3d4cSgeorges 				freemsg(head);
5577c478bd9Sstevel@tonic-gate 				return (EINVAL);
558311e3d4cSgeorges 			}
5597c478bd9Sstevel@tonic-gate 		} else
5607c478bd9Sstevel@tonic-gate #endif
561311e3d4cSgeorges 		if ((*count + sfv_len) < 0) {
562311e3d4cSgeorges 			freemsg(head);
5637c478bd9Sstevel@tonic-gate 			return (EINVAL);
564311e3d4cSgeorges 		}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 		sfv_off = (u_offset_t)(ulong_t)sfv->sfv_off;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
5697c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
5707c478bd9Sstevel@tonic-gate 				if (buf_left == 0) {
5717c478bd9Sstevel@tonic-gate 					tmp = dmp;
5727c478bd9Sstevel@tonic-gate 					buf_left = MIN(total_size, maxblk);
5737c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
574c28749e9Skais 					dmp = allocb(buf_left + extra, BPRI_HI);
5757c478bd9Sstevel@tonic-gate 					if (dmp == NULL) {
5767c478bd9Sstevel@tonic-gate 						freemsg(head);
5777c478bd9Sstevel@tonic-gate 						return (ENOMEM);
5787c478bd9Sstevel@tonic-gate 					}
5797c478bd9Sstevel@tonic-gate 					dmp->b_wptr = dmp->b_rptr =
5807c478bd9Sstevel@tonic-gate 					    dmp->b_rptr + wroff;
5817c478bd9Sstevel@tonic-gate 					tmp->b_cont = dmp;
5827c478bd9Sstevel@tonic-gate 				} else {
5837c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
5847c478bd9Sstevel@tonic-gate 				}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
5877c478bd9Sstevel@tonic-gate 				aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
5887c478bd9Sstevel@tonic-gate 				auio.uio_loffset = *fileoff;
5897c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
5907c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
5917c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
5927c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_USERSPACE;
5937c478bd9Sstevel@tonic-gate 				auio.uio_llimit = curproc->p_fsz_ctl;
5947c478bd9Sstevel@tonic-gate 				auio.uio_fmode = fflag;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 				buf_left -= iov_len;
5977c478bd9Sstevel@tonic-gate 				total_size -= iov_len;
5987c478bd9Sstevel@tonic-gate 				sfv_len -= iov_len;
5997c478bd9Sstevel@tonic-gate 				sfv_off += iov_len;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 				error = uiomove((caddr_t)dmp->b_wptr,
6027c478bd9Sstevel@tonic-gate 				    iov_len, UIO_WRITE, &auio);
6037c478bd9Sstevel@tonic-gate 				if (error != 0) {
6047c478bd9Sstevel@tonic-gate 					freemsg(head);
6057c478bd9Sstevel@tonic-gate 					return (error);
6067c478bd9Sstevel@tonic-gate 				}
6077c478bd9Sstevel@tonic-gate 				dmp->b_wptr += iov_len;
6087c478bd9Sstevel@tonic-gate 			}
6097c478bd9Sstevel@tonic-gate 		} else {
6107c478bd9Sstevel@tonic-gate 			file_t	*ffp;
6117c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL) {
6147c478bd9Sstevel@tonic-gate 				freemsg(head);
6157c478bd9Sstevel@tonic-gate 				return (EBADF);
6167c478bd9Sstevel@tonic-gate 			}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
6197c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
6207c478bd9Sstevel@tonic-gate 				freemsg(head);
6217c478bd9Sstevel@tonic-gate 				return (EACCES);
6227c478bd9Sstevel@tonic-gate 			}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
6257c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
6267c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
6277c478bd9Sstevel@tonic-gate 				freemsg(head);
6287c478bd9Sstevel@tonic-gate 				return (EINVAL);
6297c478bd9Sstevel@tonic-gate 			}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 			/*
6327c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
6337c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
6347c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
6357c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
6367c478bd9Sstevel@tonic-gate 			 */
6377c478bd9Sstevel@tonic-gate 
638bfb244baSpr 			if (vn_compare(vp, readvp)) {
6397c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
6407c478bd9Sstevel@tonic-gate 				freemsg(head);
6417c478bd9Sstevel@tonic-gate 				return (EINVAL);
6427c478bd9Sstevel@tonic-gate 			}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 			/*
6457c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
6467c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
6477c478bd9Sstevel@tonic-gate 			 */
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 			(void) VOP_RWLOCK(readvp, readflg, NULL);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 			/* Same checks as in pread */
6527c478bd9Sstevel@tonic-gate 			if (sfv_off > maxoff) {
6537c478bd9Sstevel@tonic-gate 				VOP_RWUNLOCK(readvp, readflg, NULL);
6547c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
6557c478bd9Sstevel@tonic-gate 				freemsg(head);
6567c478bd9Sstevel@tonic-gate 				return (EINVAL);
6577c478bd9Sstevel@tonic-gate 			}
6587c478bd9Sstevel@tonic-gate 			if (sfv_off + sfv_len > maxoff) {
6590a8433caSrshoaib 				total_size -= (sfv_off + sfv_len - maxoff);
6607c478bd9Sstevel@tonic-gate 				sfv_len = (ssize_t)((offset_t)maxoff -
6617c478bd9Sstevel@tonic-gate 				    sfv_off);
6627c478bd9Sstevel@tonic-gate 			}
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
6657c478bd9Sstevel@tonic-gate 				if (buf_left == 0) {
6667c478bd9Sstevel@tonic-gate 					tmp = dmp;
6677c478bd9Sstevel@tonic-gate 					buf_left = MIN(total_size, maxblk);
6687c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
669c28749e9Skais 					dmp = allocb(buf_left + extra, BPRI_HI);
6707c478bd9Sstevel@tonic-gate 					if (dmp == NULL) {
6717c478bd9Sstevel@tonic-gate 						VOP_RWUNLOCK(readvp, readflg,
672311e3d4cSgeorges 						    NULL);
6737c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
6747c478bd9Sstevel@tonic-gate 						freemsg(head);
6757c478bd9Sstevel@tonic-gate 						return (ENOMEM);
6767c478bd9Sstevel@tonic-gate 					}
6777c478bd9Sstevel@tonic-gate 					dmp->b_wptr = dmp->b_rptr =
6787c478bd9Sstevel@tonic-gate 					    dmp->b_rptr + wroff;
6797c478bd9Sstevel@tonic-gate 					tmp->b_cont = dmp;
6807c478bd9Sstevel@tonic-gate 				} else {
6817c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
6827c478bd9Sstevel@tonic-gate 				}
6837c478bd9Sstevel@tonic-gate 				aiov.iov_base = (caddr_t)dmp->b_wptr;
6847c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
6857c478bd9Sstevel@tonic-gate 				auio.uio_loffset = sfv_off;
6867c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
6877c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
6887c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
6897c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
6907c478bd9Sstevel@tonic-gate 				auio.uio_llimit = MAXOFFSET_T;
6917c478bd9Sstevel@tonic-gate 				auio.uio_fmode = ffp->f_flag;
6927c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
6937c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 				/*
6967c478bd9Sstevel@tonic-gate 				 * If read sync is not asked for,
6977c478bd9Sstevel@tonic-gate 				 * filter sync flags
6987c478bd9Sstevel@tonic-gate 				 */
6997c478bd9Sstevel@tonic-gate 				if ((ioflag & FRSYNC) == 0)
7007c478bd9Sstevel@tonic-gate 					ioflag &= ~(FSYNC|FDSYNC);
7017c478bd9Sstevel@tonic-gate 				error = VOP_READ(readvp, &auio, ioflag,
7027c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
7037c478bd9Sstevel@tonic-gate 				if (error != 0) {
7047c478bd9Sstevel@tonic-gate 					/*
7057c478bd9Sstevel@tonic-gate 					 * If we were reading a pipe (currently
7067c478bd9Sstevel@tonic-gate 					 * not implemented), we may now loose
7077c478bd9Sstevel@tonic-gate 					 * data.
7087c478bd9Sstevel@tonic-gate 					 */
7097c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
7107c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
7117c478bd9Sstevel@tonic-gate 					freemsg(head);
7127c478bd9Sstevel@tonic-gate 					return (error);
7137c478bd9Sstevel@tonic-gate 				}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 				/*
7167c478bd9Sstevel@tonic-gate 				 * Check how much data was really read.
7177c478bd9Sstevel@tonic-gate 				 * Decrement the 'len' and increment the
7187c478bd9Sstevel@tonic-gate 				 * 'off' appropriately.
7197c478bd9Sstevel@tonic-gate 				 */
7207c478bd9Sstevel@tonic-gate 				cnt = iov_len - auio.uio_resid;
7217c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
7227c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
7237c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
7247c478bd9Sstevel@tonic-gate 					freemsg(head);
7257c478bd9Sstevel@tonic-gate 					return (EINVAL);
7267c478bd9Sstevel@tonic-gate 				}
7277c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
7287c478bd9Sstevel@tonic-gate 				sfv_off += cnt;
7297c478bd9Sstevel@tonic-gate 				total_size -= cnt;
7307c478bd9Sstevel@tonic-gate 				buf_left -= cnt;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 				dmp->b_wptr += cnt;
7337c478bd9Sstevel@tonic-gate 			}
7347c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(readvp, readflg, NULL);
7357c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 		sfv++;
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	ASSERT(total_size == 0);
7417c478bd9Sstevel@tonic-gate 	error = kstrwritemp(vp, head, fflag);
7427c478bd9Sstevel@tonic-gate 	if (error != 0) {
7437c478bd9Sstevel@tonic-gate 		freemsg(head);
7447c478bd9Sstevel@tonic-gate 		return (error);
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_ru.ioch += (ulong_t)size;
7477c478bd9Sstevel@tonic-gate 	*count += size;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	return (0);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate int
7547c478bd9Sstevel@tonic-gate sendvec_chunk(file_t *fp, u_offset_t *fileoff, struct sendfilevec *sfv,
7557c478bd9Sstevel@tonic-gate     int copy_cnt, ssize_t *count)
7567c478bd9Sstevel@tonic-gate {
7577c478bd9Sstevel@tonic-gate 	struct vnode *vp;
7587c478bd9Sstevel@tonic-gate 	struct uio auio;
7597c478bd9Sstevel@tonic-gate 	struct iovec aiov;
7607c478bd9Sstevel@tonic-gate 	ushort_t fflag;
7617c478bd9Sstevel@tonic-gate 	int ioflag;
7627c478bd9Sstevel@tonic-gate 	int i, error;
7637c478bd9Sstevel@tonic-gate 	size_t cnt;
7647c478bd9Sstevel@tonic-gate 	ssize_t sfv_len;
7657c478bd9Sstevel@tonic-gate 	u_offset_t sfv_off;
7667c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
7677c478bd9Sstevel@tonic-gate 	model_t model = get_udatamodel();
7687c478bd9Sstevel@tonic-gate 	u_offset_t maxoff = (model == DATAMODEL_ILP32) ?
769311e3d4cSgeorges 	    MAXOFF32_T : MAXOFFSET_T;
7707c478bd9Sstevel@tonic-gate #else
7717c478bd9Sstevel@tonic-gate 	const u_offset_t maxoff = MAXOFF32_T;
7727c478bd9Sstevel@tonic-gate #endif
7731574ae68Sjohansen 	mblk_t	*dmp = NULL;
7741574ae68Sjohansen 	char	*buf = NULL;
775c28749e9Skais 	size_t  extra;
776c28749e9Skais 	int maxblk, wroff, tail_len;
777c28749e9Skais 	struct sonode *so;
778c28749e9Skais 	stdata_t *stp;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
7817c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
7827c478bd9Sstevel@tonic-gate 
783c28749e9Skais 	if (vp->v_type == VSOCK) {
784c28749e9Skais 		so = VTOSO(vp);
785c28749e9Skais 		stp = vp->v_stream;
786c28749e9Skais 		wroff = (int)stp->sd_wroff;
787c28749e9Skais 		tail_len = (int)stp->sd_tail;
788c28749e9Skais 		maxblk = (int)stp->sd_maxblk;
789c28749e9Skais 		extra = wroff + tail_len;
790c28749e9Skais 	}
791c28749e9Skais 
7927c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_DEFAULT;
7937c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
7947c478bd9Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING))
7957c478bd9Sstevel@tonic-gate 			return (EINTR);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 		/*
7987c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
7997c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
8007c478bd9Sstevel@tonic-gate 		 */
8017c478bd9Sstevel@tonic-gate 		sfv_len = (ssize_t)sfv->sfv_len;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 		if (sfv_len == 0) {
8047c478bd9Sstevel@tonic-gate 			sfv++;
8057c478bd9Sstevel@tonic-gate 			continue;
8067c478bd9Sstevel@tonic-gate 		}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG) {
8097c478bd9Sstevel@tonic-gate 			if (*fileoff >= curproc->p_fsz_ctl) {
8107c478bd9Sstevel@tonic-gate 				mutex_enter(&curproc->p_lock);
8117c478bd9Sstevel@tonic-gate 				(void) rctl_action(
8127c478bd9Sstevel@tonic-gate 				    rctlproc_legacy[RLIMIT_FSIZE],
8137c478bd9Sstevel@tonic-gate 				    curproc->p_rctls, curproc, RCA_SAFE);
8147c478bd9Sstevel@tonic-gate 				mutex_exit(&curproc->p_lock);
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 				return (EFBIG);
8177c478bd9Sstevel@tonic-gate 			}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 			if (*fileoff >= maxoff)
8207c478bd9Sstevel@tonic-gate 				return (EFBIG);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 			if (*fileoff + sfv_len > maxoff)
8237c478bd9Sstevel@tonic-gate 				return (EINVAL);
8247c478bd9Sstevel@tonic-gate 		}
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 		/* Check for overflow */
8277c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
8287c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_ILP32) {
8297c478bd9Sstevel@tonic-gate 			if (((ssize32_t)(*count + sfv_len)) < 0)
8307c478bd9Sstevel@tonic-gate 				return (EINVAL);
8317c478bd9Sstevel@tonic-gate 		} else
8327c478bd9Sstevel@tonic-gate #endif
8337c478bd9Sstevel@tonic-gate 		if ((*count + sfv_len) < 0)
8347c478bd9Sstevel@tonic-gate 			return (EINVAL);
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 		sfv_off = (u_offset_t)(ulong_t)sfv->sfv_off;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
8397c478bd9Sstevel@tonic-gate 			if (vp->v_type == VSOCK) {
840*e116a42fSPrakash Jalan 				while (sfv_len > 0) {
841*e116a42fSPrakash Jalan 					size_t iov_len;
842*e116a42fSPrakash Jalan 
843*e116a42fSPrakash Jalan 					iov_len = sfv_len;
844*e116a42fSPrakash Jalan 					if (so->so_kssl_ctx != NULL)
845*e116a42fSPrakash Jalan 						iov_len = MIN(iov_len, maxblk);
846*e116a42fSPrakash Jalan 
847*e116a42fSPrakash Jalan 					aiov.iov_len = iov_len;
848*e116a42fSPrakash Jalan 					aiov.iov_base =
849*e116a42fSPrakash Jalan 					    (caddr_t)(uintptr_t)sfv_off;
850*e116a42fSPrakash Jalan 
851*e116a42fSPrakash Jalan 					auio.uio_iov = &aiov;
852*e116a42fSPrakash Jalan 					auio.uio_iovcnt = 1;
853*e116a42fSPrakash Jalan 					auio.uio_loffset = *fileoff;
854*e116a42fSPrakash Jalan 					auio.uio_segflg = UIO_USERSPACE;
855*e116a42fSPrakash Jalan 					auio.uio_fmode = fflag;
856*e116a42fSPrakash Jalan 					auio.uio_llimit = curproc->p_fsz_ctl;
857*e116a42fSPrakash Jalan 					auio.uio_resid = iov_len;
858*e116a42fSPrakash Jalan 
859*e116a42fSPrakash Jalan 					dmp = allocb(iov_len + extra, BPRI_HI);
860*e116a42fSPrakash Jalan 					if (dmp == NULL)
861*e116a42fSPrakash Jalan 						return (ENOMEM);
862*e116a42fSPrakash Jalan 					dmp->b_wptr = dmp->b_rptr =
863*e116a42fSPrakash Jalan 					    dmp->b_rptr + wroff;
864*e116a42fSPrakash Jalan 					error = uiomove((caddr_t)dmp->b_wptr,
865*e116a42fSPrakash Jalan 					    iov_len, UIO_WRITE, &auio);
866*e116a42fSPrakash Jalan 					if (error != 0) {
867*e116a42fSPrakash Jalan 						freeb(dmp);
868*e116a42fSPrakash Jalan 						return (error);
869*e116a42fSPrakash Jalan 					}
870*e116a42fSPrakash Jalan 					dmp->b_wptr += iov_len;
871*e116a42fSPrakash Jalan 					error = kstrwritemp(vp, dmp, fflag);
872*e116a42fSPrakash Jalan 					if (error != 0) {
873*e116a42fSPrakash Jalan 						freeb(dmp);
874*e116a42fSPrakash Jalan 						return (error);
875*e116a42fSPrakash Jalan 					}
876*e116a42fSPrakash Jalan 					ttolwp(curthread)->lwp_ru.ioch +=
877*e116a42fSPrakash Jalan 					    (ulong_t)iov_len;
878*e116a42fSPrakash Jalan 					*count += iov_len;
879*e116a42fSPrakash Jalan 					sfv_len -= iov_len;
880*e116a42fSPrakash Jalan 					sfv_off += iov_len;
8817c478bd9Sstevel@tonic-gate 				}
8827c478bd9Sstevel@tonic-gate 			} else {
883*e116a42fSPrakash Jalan 				aiov.iov_len = sfv_len;
884*e116a42fSPrakash Jalan 				aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
885*e116a42fSPrakash Jalan 
886*e116a42fSPrakash Jalan 				auio.uio_iov = &aiov;
887*e116a42fSPrakash Jalan 				auio.uio_iovcnt = 1;
888*e116a42fSPrakash Jalan 				auio.uio_loffset = *fileoff;
889*e116a42fSPrakash Jalan 				auio.uio_segflg = UIO_USERSPACE;
890*e116a42fSPrakash Jalan 				auio.uio_fmode = fflag;
891*e116a42fSPrakash Jalan 				auio.uio_llimit = curproc->p_fsz_ctl;
892*e116a42fSPrakash Jalan 				auio.uio_resid = sfv_len;
893*e116a42fSPrakash Jalan 
8947c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
8957c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
8967c478bd9Sstevel@tonic-gate 				while (sfv_len > 0) {
8977c478bd9Sstevel@tonic-gate 					error = VOP_WRITE(vp, &auio, ioflag,
8987c478bd9Sstevel@tonic-gate 					    fp->f_cred, NULL);
8997c478bd9Sstevel@tonic-gate 					cnt = sfv_len - auio.uio_resid;
9007c478bd9Sstevel@tonic-gate 					sfv_len -= cnt;
9017c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
9027c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
9037c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
9047c478bd9Sstevel@tonic-gate 					*count += cnt;
9057c478bd9Sstevel@tonic-gate 					if (error != 0)
9067c478bd9Sstevel@tonic-gate 						return (error);
9077c478bd9Sstevel@tonic-gate 				}
9087c478bd9Sstevel@tonic-gate 			}
9097c478bd9Sstevel@tonic-gate 		} else {
9107f9ee850Sblu 			int segmapit = 0;
9117c478bd9Sstevel@tonic-gate 			file_t	*ffp;
9127c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
913bfb244baSpr 			struct vnode *realvp;
9147c478bd9Sstevel@tonic-gate 			size_t	size;
9157c478bd9Sstevel@tonic-gate 			caddr_t	ptr;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL)
9187c478bd9Sstevel@tonic-gate 				return (EBADF);
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
9217c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
9227c478bd9Sstevel@tonic-gate 				return (EBADF);
9237c478bd9Sstevel@tonic-gate 			}
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
926da6c28aaSamw 			if (VOP_REALVP(readvp, &realvp, NULL) == 0)
927bfb244baSpr 				readvp = realvp;
9287c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
9297c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
9307c478bd9Sstevel@tonic-gate 				return (EINVAL);
9317c478bd9Sstevel@tonic-gate 			}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 			/*
9347c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
9357c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
9367c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
9377c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
9387c478bd9Sstevel@tonic-gate 			 */
939bfb244baSpr 			if (vn_compare(vp, readvp)) {
9407c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
9417c478bd9Sstevel@tonic-gate 				return (EINVAL);
9427c478bd9Sstevel@tonic-gate 			}
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 			/*
9457c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
9467c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
9477c478bd9Sstevel@tonic-gate 			 */
9487c478bd9Sstevel@tonic-gate 			(void) VOP_RWLOCK(readvp, readflg, NULL);
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 			/* Same checks as in pread */
9517c478bd9Sstevel@tonic-gate 			if (sfv_off > maxoff) {
9527c478bd9Sstevel@tonic-gate 				VOP_RWUNLOCK(readvp, readflg, NULL);
9537c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
9547c478bd9Sstevel@tonic-gate 				return (EINVAL);
9557c478bd9Sstevel@tonic-gate 			}
9567c478bd9Sstevel@tonic-gate 			if (sfv_off + sfv_len > maxoff) {
9577c478bd9Sstevel@tonic-gate 				sfv_len = (ssize_t)((offset_t)maxoff -
9587c478bd9Sstevel@tonic-gate 				    sfv_off);
9597c478bd9Sstevel@tonic-gate 			}
9607c478bd9Sstevel@tonic-gate 			/* Find the native blocksize to transfer data */
9617c478bd9Sstevel@tonic-gate 			size = MIN(vp->v_vfsp->vfs_bsize,
9627c478bd9Sstevel@tonic-gate 			    readvp->v_vfsp->vfs_bsize);
9637c478bd9Sstevel@tonic-gate 			size = sfv_len < size ? sfv_len : size;
9647c478bd9Sstevel@tonic-gate 
9651574ae68Sjohansen 			if (vp->v_type != VSOCK) {
96674024373Spr 				segmapit = 0;
9671574ae68Sjohansen 				buf = kmem_alloc(size, KM_NOSLEEP);
9681574ae68Sjohansen 				if (buf == NULL) {
9691574ae68Sjohansen 					VOP_RWUNLOCK(readvp, readflg, NULL);
9701574ae68Sjohansen 					releasef(sfv->sfv_fd);
9711574ae68Sjohansen 					return (ENOMEM);
9721574ae68Sjohansen 				}
973c28749e9Skais 			} else {
974c28749e9Skais 				/*
975c28749e9Skais 				 * For sockets acting as an SSL proxy, we
976c28749e9Skais 				 * need to adjust the size to the maximum
977c28749e9Skais 				 * SSL record size set in the stream head.
978c28749e9Skais 				 */
979c28749e9Skais 				if (so->so_kssl_ctx != NULL)
980c28749e9Skais 					size = MIN(size, maxblk);
98174024373Spr 
98274024373Spr 				if (vn_has_flocks(readvp) ||
98374024373Spr 				    readvp->v_flag & VNOMAP ||
98474024373Spr 				    stp->sd_copyflag & STZCVMUNSAFE) {
98574024373Spr 					segmapit = 0;
98674024373Spr 				} else if (stp->sd_copyflag & STZCVMSAFE) {
98774024373Spr 					segmapit = 1;
98874024373Spr 				} else {
98974024373Spr 					int on = 1;
99074024373Spr 					if (SOP_SETSOCKOPT(VTOSO(vp),
99174024373Spr 					    SOL_SOCKET, SO_SND_COPYAVOID,
99274024373Spr 					    &on, sizeof (on)) == 0)
99374024373Spr 					segmapit = 1;
99474024373Spr 				}
99574024373Spr 			}
99674024373Spr 
99774024373Spr 			if (segmapit) {
99874024373Spr 				boolean_t nowait;
99974024373Spr 
100074024373Spr 				nowait = (sfv->sfv_flag & SFV_NOWAIT) != 0;
100174024373Spr 				error = snf_segmap(fp, readvp, sfv_off,
10023b3d24f3SJayakara Kini 				    (u_offset_t)sfv_len, (ssize_t *)&cnt,
10033b3d24f3SJayakara Kini 				    nowait);
100474024373Spr 				releasef(sfv->sfv_fd);
100574024373Spr 				*count += cnt;
100674024373Spr 				if (error)
100774024373Spr 					return (error);
100874024373Spr 				sfv++;
100974024373Spr 				continue;
10101574ae68Sjohansen 			}
10111574ae68Sjohansen 
10127c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
10137c478bd9Sstevel@tonic-gate 				size_t	iov_len;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 				iov_len = MIN(size, sfv_len);
10167c478bd9Sstevel@tonic-gate 
10171574ae68Sjohansen 				if (vp->v_type == VSOCK) {
1018c28749e9Skais 					dmp = allocb(iov_len + extra, BPRI_HI);
10191574ae68Sjohansen 					if (dmp == NULL) {
10201574ae68Sjohansen 						VOP_RWUNLOCK(readvp, readflg,
10211574ae68Sjohansen 						    NULL);
10221574ae68Sjohansen 						releasef(sfv->sfv_fd);
10231574ae68Sjohansen 						return (ENOMEM);
10241574ae68Sjohansen 					}
1025c28749e9Skais 					dmp->b_wptr = dmp->b_rptr =
1026c28749e9Skais 					    dmp->b_rptr + wroff;
10271574ae68Sjohansen 					ptr = (caddr_t)dmp->b_rptr;
10281574ae68Sjohansen 				} else {
10291574ae68Sjohansen 					ptr = buf;
10307c478bd9Sstevel@tonic-gate 				}
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 				aiov.iov_base = ptr;
10337c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
10347c478bd9Sstevel@tonic-gate 				auio.uio_loffset = sfv_off;
10357c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
10367c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
10377c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
10387c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
10397c478bd9Sstevel@tonic-gate 				auio.uio_llimit = MAXOFFSET_T;
10407c478bd9Sstevel@tonic-gate 				auio.uio_fmode = ffp->f_flag;
10417c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
10427c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 				/*
10457c478bd9Sstevel@tonic-gate 				 * If read sync is not asked for,
10467c478bd9Sstevel@tonic-gate 				 * filter sync flags
10477c478bd9Sstevel@tonic-gate 				 */
10487c478bd9Sstevel@tonic-gate 				if ((ioflag & FRSYNC) == 0)
10497c478bd9Sstevel@tonic-gate 					ioflag &= ~(FSYNC|FDSYNC);
10507c478bd9Sstevel@tonic-gate 				error = VOP_READ(readvp, &auio, ioflag,
10517c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
10527c478bd9Sstevel@tonic-gate 				if (error != 0) {
10537c478bd9Sstevel@tonic-gate 					/*
10547c478bd9Sstevel@tonic-gate 					 * If we were reading a pipe (currently
10557c478bd9Sstevel@tonic-gate 					 * not implemented), we may now lose
10567c478bd9Sstevel@tonic-gate 					 * data.
10577c478bd9Sstevel@tonic-gate 					 */
10581574ae68Sjohansen 					if (vp->v_type == VSOCK)
10591574ae68Sjohansen 						freeb(dmp);
10601574ae68Sjohansen 					else
10611574ae68Sjohansen 						kmem_free(buf, size);
10627c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
10637c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
10647c478bd9Sstevel@tonic-gate 					return (error);
10657c478bd9Sstevel@tonic-gate 				}
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 				/*
10687c478bd9Sstevel@tonic-gate 				 * Check how much data was really read.
10697c478bd9Sstevel@tonic-gate 				 * Decrement the 'len' and increment the
10707c478bd9Sstevel@tonic-gate 				 * 'off' appropriately.
10717c478bd9Sstevel@tonic-gate 				 */
10727c478bd9Sstevel@tonic-gate 				cnt = iov_len - auio.uio_resid;
10737c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
10741574ae68Sjohansen 					if (vp->v_type == VSOCK)
10751574ae68Sjohansen 						freeb(dmp);
10761574ae68Sjohansen 					else
10771574ae68Sjohansen 						kmem_free(buf, size);
10787c478bd9Sstevel@tonic-gate 					VOP_RWUNLOCK(readvp, readflg, NULL);
10797c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
10807c478bd9Sstevel@tonic-gate 					return (EINVAL);
10817c478bd9Sstevel@tonic-gate 				}
10827c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
10837c478bd9Sstevel@tonic-gate 				sfv_off += cnt;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 				if (vp->v_type == VSOCK) {
10867c478bd9Sstevel@tonic-gate 					dmp->b_wptr = dmp->b_rptr + cnt;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 					error = kstrwritemp(vp, dmp, fflag);
10897c478bd9Sstevel@tonic-gate 					if (error != 0) {
10907c478bd9Sstevel@tonic-gate 						freeb(dmp);
10917c478bd9Sstevel@tonic-gate 						VOP_RWUNLOCK(readvp, readflg,
1092311e3d4cSgeorges 						    NULL);
10937c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
10947c478bd9Sstevel@tonic-gate 						return (error);
10957c478bd9Sstevel@tonic-gate 					}
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
10987c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
10997c478bd9Sstevel@tonic-gate 					*count += cnt;
11007c478bd9Sstevel@tonic-gate 				} else {
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 					aiov.iov_base = ptr;
11037c478bd9Sstevel@tonic-gate 					aiov.iov_len = cnt;
11047c478bd9Sstevel@tonic-gate 					auio.uio_loffset = *fileoff;
11057c478bd9Sstevel@tonic-gate 					auio.uio_resid = cnt;
1106a490cd42SPrakash Jalan 					auio.uio_iov = &aiov;
1107a490cd42SPrakash Jalan 					auio.uio_iovcnt = 1;
11087c478bd9Sstevel@tonic-gate 					auio.uio_segflg = UIO_SYSSPACE;
11097c478bd9Sstevel@tonic-gate 					auio.uio_llimit = curproc->p_fsz_ctl;
11107c478bd9Sstevel@tonic-gate 					auio.uio_fmode = fflag;
11117c478bd9Sstevel@tonic-gate 					ioflag = auio.uio_fmode &
11127c478bd9Sstevel@tonic-gate 					    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
11137c478bd9Sstevel@tonic-gate 					error = VOP_WRITE(vp, &auio, ioflag,
11147c478bd9Sstevel@tonic-gate 					    fp->f_cred, NULL);
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 					/*
11177c478bd9Sstevel@tonic-gate 					 * Check how much data was written.
11187c478bd9Sstevel@tonic-gate 					 * Increment the 'len' and decrement the
11197c478bd9Sstevel@tonic-gate 					 * 'off' if all the data was not
11207c478bd9Sstevel@tonic-gate 					 * written.
11217c478bd9Sstevel@tonic-gate 					 */
11227c478bd9Sstevel@tonic-gate 					cnt -= auio.uio_resid;
11237c478bd9Sstevel@tonic-gate 					sfv_len += auio.uio_resid;
11247c478bd9Sstevel@tonic-gate 					sfv_off -= auio.uio_resid;
11257c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
11267c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
11277c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
11287c478bd9Sstevel@tonic-gate 					*count += cnt;
11297c478bd9Sstevel@tonic-gate 					if (error != 0) {
11302db6c79fSstevel 						kmem_free(buf, size);
11317c478bd9Sstevel@tonic-gate 						VOP_RWUNLOCK(readvp, readflg,
1132311e3d4cSgeorges 						    NULL);
11337c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
11347c478bd9Sstevel@tonic-gate 						return (error);
11357c478bd9Sstevel@tonic-gate 					}
11367c478bd9Sstevel@tonic-gate 				}
11377c478bd9Sstevel@tonic-gate 			}
11381574ae68Sjohansen 			if (buf) {
11391574ae68Sjohansen 				kmem_free(buf, size);
11401574ae68Sjohansen 				buf = NULL;
11411574ae68Sjohansen 			}
11427c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(readvp, readflg, NULL);
11437c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
11447c478bd9Sstevel@tonic-gate 		}
11457c478bd9Sstevel@tonic-gate 		sfv++;
11467c478bd9Sstevel@tonic-gate 	}
11477c478bd9Sstevel@tonic-gate 	return (0);
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate ssize_t
11517c478bd9Sstevel@tonic-gate sendfilev(int opcode, int fildes, const struct sendfilevec *vec, int sfvcnt,
11527c478bd9Sstevel@tonic-gate     size_t *xferred)
11537c478bd9Sstevel@tonic-gate {
1154311e3d4cSgeorges 	int error = 0;
1155311e3d4cSgeorges 	int first_vector_error = 0;
11567c478bd9Sstevel@tonic-gate 	file_t *fp;
11577c478bd9Sstevel@tonic-gate 	struct vnode *vp;
11587c478bd9Sstevel@tonic-gate 	struct sonode *so;
11597c478bd9Sstevel@tonic-gate 	u_offset_t fileoff;
11607c478bd9Sstevel@tonic-gate 	int copy_cnt;
11617c478bd9Sstevel@tonic-gate 	const struct sendfilevec *copy_vec;
11627c478bd9Sstevel@tonic-gate 	struct sendfilevec sfv[SEND_MAX_CHUNK];
11637c478bd9Sstevel@tonic-gate 	ssize_t count = 0;
11647c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11657c478bd9Sstevel@tonic-gate 	struct ksendfilevec32 sfv32[SEND_MAX_CHUNK];
11667c478bd9Sstevel@tonic-gate #endif
11670a8433caSrshoaib 	ssize_t total_size;
11687c478bd9Sstevel@tonic-gate 	int i;
11697c478bd9Sstevel@tonic-gate 	boolean_t is_sock = B_FALSE;
11707c478bd9Sstevel@tonic-gate 	int maxblk = 0;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	if (sfvcnt <= 0)
11737c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	if ((fp = getf(fildes)) == NULL)
11767c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	if (((fp->f_flag) & FWRITE) == 0) {
11797c478bd9Sstevel@tonic-gate 		error = EBADF;
11807c478bd9Sstevel@tonic-gate 		goto err;
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	fileoff = fp->f_offset;
11847c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	switch (vp->v_type) {
11877c478bd9Sstevel@tonic-gate 	case VSOCK:
11887c478bd9Sstevel@tonic-gate 		so = VTOSO(vp);
11897c478bd9Sstevel@tonic-gate 		/* sendfile not supported for SCTP */
11907c478bd9Sstevel@tonic-gate 		if (so->so_protocol == IPPROTO_SCTP) {
11917c478bd9Sstevel@tonic-gate 			error = EPROTONOSUPPORT;
11927c478bd9Sstevel@tonic-gate 			goto err;
11937c478bd9Sstevel@tonic-gate 		}
11947c478bd9Sstevel@tonic-gate 		is_sock = B_TRUE;
11957c478bd9Sstevel@tonic-gate 		switch (so->so_family) {
11967c478bd9Sstevel@tonic-gate 		case AF_INET:
11977c478bd9Sstevel@tonic-gate 		case AF_INET6:
11987c478bd9Sstevel@tonic-gate 			/*
11997c478bd9Sstevel@tonic-gate 			 * Make similar checks done in SOP_WRITE().
12007c478bd9Sstevel@tonic-gate 			 */
12017c478bd9Sstevel@tonic-gate 			if (so->so_state & SS_CANTSENDMORE) {
12027c478bd9Sstevel@tonic-gate 				tsignal(curthread, SIGPIPE);
12037c478bd9Sstevel@tonic-gate 				error = EPIPE;
12047c478bd9Sstevel@tonic-gate 				goto err;
12057c478bd9Sstevel@tonic-gate 			}
12067c478bd9Sstevel@tonic-gate 			if (so->so_type != SOCK_STREAM) {
12077c478bd9Sstevel@tonic-gate 				error = EOPNOTSUPP;
12087c478bd9Sstevel@tonic-gate 				goto err;
12097c478bd9Sstevel@tonic-gate 			}
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 			if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) !=
12127c478bd9Sstevel@tonic-gate 			    (SS_ISCONNECTED|SS_ISBOUND)) {
12137c478bd9Sstevel@tonic-gate 				error = ENOTCONN;
12147c478bd9Sstevel@tonic-gate 				goto err;
12157c478bd9Sstevel@tonic-gate 			}
12167c478bd9Sstevel@tonic-gate 
1217ff550d0eSmasputra 			if ((so->so_state & SS_DIRECT) &&
1218c28749e9Skais 			    (so->so_priv != NULL) &&
1219c28749e9Skais 			    (so->so_kssl_ctx == NULL)) {
12207c478bd9Sstevel@tonic-gate 				maxblk = ((tcp_t *)so->so_priv)->tcp_mss;
12217c478bd9Sstevel@tonic-gate 			} else {
12227c478bd9Sstevel@tonic-gate 				maxblk = (int)vp->v_stream->sd_maxblk;
12237c478bd9Sstevel@tonic-gate 			}
12247c478bd9Sstevel@tonic-gate 			break;
12257c478bd9Sstevel@tonic-gate 		default:
12267c478bd9Sstevel@tonic-gate 			error = EAFNOSUPPORT;
12277c478bd9Sstevel@tonic-gate 			goto err;
12287c478bd9Sstevel@tonic-gate 		}
12297c478bd9Sstevel@tonic-gate 		break;
12307c478bd9Sstevel@tonic-gate 	case VREG:
12317c478bd9Sstevel@tonic-gate 		break;
12327c478bd9Sstevel@tonic-gate 	default:
12337c478bd9Sstevel@tonic-gate 		error = EINVAL;
12347c478bd9Sstevel@tonic-gate 		goto err;
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	switch (opcode) {
12387c478bd9Sstevel@tonic-gate 	case SENDFILEV :
12397c478bd9Sstevel@tonic-gate 		break;
12407c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
12417c478bd9Sstevel@tonic-gate 	case SENDFILEV64 :
12427c478bd9Sstevel@tonic-gate 		return (sendvec64(fp, (struct ksendfilevec64 *)vec, sfvcnt,
12437c478bd9Sstevel@tonic-gate 		    (size32_t *)xferred, fildes));
12447c478bd9Sstevel@tonic-gate #endif
12457c478bd9Sstevel@tonic-gate 	default :
12467c478bd9Sstevel@tonic-gate 		error = ENOSYS;
12477c478bd9Sstevel@tonic-gate 		break;
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
12517c478bd9Sstevel@tonic-gate 	copy_vec = vec;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	do {
12540a8433caSrshoaib 		total_size = 0;
12557c478bd9Sstevel@tonic-gate 		copy_cnt = MIN(sfvcnt, SEND_MAX_CHUNK);
12567c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
12577c478bd9Sstevel@tonic-gate 		/* 32-bit callers need to have their iovec expanded. */
12587c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_ILP32) {
12597c478bd9Sstevel@tonic-gate 			if (copyin(copy_vec, sfv32,
12607c478bd9Sstevel@tonic-gate 			    copy_cnt * sizeof (ksendfilevec32_t))) {
12617c478bd9Sstevel@tonic-gate 				error = EFAULT;
12627c478bd9Sstevel@tonic-gate 				break;
12637c478bd9Sstevel@tonic-gate 			}
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 			for (i = 0; i < copy_cnt; i++) {
12667c478bd9Sstevel@tonic-gate 				sfv[i].sfv_fd = sfv32[i].sfv_fd;
12677c478bd9Sstevel@tonic-gate 				sfv[i].sfv_off =
1268311e3d4cSgeorges 				    (off_t)(uint32_t)sfv32[i].sfv_off;
12697c478bd9Sstevel@tonic-gate 				sfv[i].sfv_len = (size_t)sfv32[i].sfv_len;
12707c478bd9Sstevel@tonic-gate 				total_size += sfv[i].sfv_len;
12717c478bd9Sstevel@tonic-gate 				sfv[i].sfv_flag = sfv32[i].sfv_flag;
1272311e3d4cSgeorges 				/*
1273311e3d4cSgeorges 				 * Individual elements of the vector must not
1274311e3d4cSgeorges 				 * wrap or overflow, as later math is signed.
1275311e3d4cSgeorges 				 * Equally total_size needs to be checked after
1276311e3d4cSgeorges 				 * each vector is added in, to be sure that
1277311e3d4cSgeorges 				 * rogue values haven't overflowed the counter.
1278311e3d4cSgeorges 				 */
1279311e3d4cSgeorges 				if (((ssize32_t)sfv[i].sfv_len < 0) ||
1280311e3d4cSgeorges 				    ((ssize32_t)total_size < 0)) {
1281311e3d4cSgeorges 					/*
1282311e3d4cSgeorges 					 * Truncate the vector to send data
1283311e3d4cSgeorges 					 * described by elements before the
1284311e3d4cSgeorges 					 * error.
1285311e3d4cSgeorges 					 */
1286311e3d4cSgeorges 					copy_cnt = i;
1287311e3d4cSgeorges 					first_vector_error = EINVAL;
1288311e3d4cSgeorges 					/* total_size can't be trusted */
1289311e3d4cSgeorges 					if ((ssize32_t)total_size < 0)
1290311e3d4cSgeorges 						error = EINVAL;
1291311e3d4cSgeorges 					break;
1292311e3d4cSgeorges 				}
12937c478bd9Sstevel@tonic-gate 			}
1294311e3d4cSgeorges 			/* Nothing to do, process errors */
1295311e3d4cSgeorges 			if (copy_cnt == 0)
1296311e3d4cSgeorges 				break;
1297311e3d4cSgeorges 
12987c478bd9Sstevel@tonic-gate 		} else {
12997c478bd9Sstevel@tonic-gate #endif
13007c478bd9Sstevel@tonic-gate 			if (copyin(copy_vec, sfv,
13017c478bd9Sstevel@tonic-gate 			    copy_cnt * sizeof (sendfilevec_t))) {
13027c478bd9Sstevel@tonic-gate 				error = EFAULT;
13037c478bd9Sstevel@tonic-gate 				break;
13047c478bd9Sstevel@tonic-gate 			}
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 			for (i = 0; i < copy_cnt; i++) {
13077c478bd9Sstevel@tonic-gate 				total_size += sfv[i].sfv_len;
1308311e3d4cSgeorges 				/*
1309311e3d4cSgeorges 				 * Individual elements of the vector must not
1310311e3d4cSgeorges 				 * wrap or overflow, as later math is signed.
1311311e3d4cSgeorges 				 * Equally total_size needs to be checked after
1312311e3d4cSgeorges 				 * each vector is added in, to be sure that
1313311e3d4cSgeorges 				 * rogue values haven't overflowed the counter.
1314311e3d4cSgeorges 				 */
1315311e3d4cSgeorges 				if (((ssize_t)sfv[i].sfv_len < 0) ||
1316311e3d4cSgeorges 				    (total_size < 0)) {
1317311e3d4cSgeorges 					/*
1318311e3d4cSgeorges 					 * Truncate the vector to send data
1319311e3d4cSgeorges 					 * described by elements before the
1320311e3d4cSgeorges 					 * error.
1321311e3d4cSgeorges 					 */
1322311e3d4cSgeorges 					copy_cnt = i;
1323311e3d4cSgeorges 					first_vector_error = EINVAL;
1324311e3d4cSgeorges 					/* total_size can't be trusted */
1325311e3d4cSgeorges 					if (total_size < 0)
1326311e3d4cSgeorges 						error = EINVAL;
1327311e3d4cSgeorges 					break;
1328311e3d4cSgeorges 				}
13297c478bd9Sstevel@tonic-gate 			}
1330311e3d4cSgeorges 			/* Nothing to do, process errors */
1331311e3d4cSgeorges 			if (copy_cnt == 0)
1332311e3d4cSgeorges 				break;
13337c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
13347c478bd9Sstevel@tonic-gate 		}
13357c478bd9Sstevel@tonic-gate #endif
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 		/*
13387c478bd9Sstevel@tonic-gate 		 * The task between deciding to use sendvec_small_chunk
13397c478bd9Sstevel@tonic-gate 		 * and sendvec_chunk is dependant on multiple things:
13407c478bd9Sstevel@tonic-gate 		 *
13417c478bd9Sstevel@tonic-gate 		 * i) latency is important for smaller files. So if the
13427c478bd9Sstevel@tonic-gate 		 * data is smaller than 'tcp_slow_start_initial' times
13437c478bd9Sstevel@tonic-gate 		 * maxblk, then use sendvec_small_chunk which creates
13443b3d24f3SJayakara Kini 		 * maxblk size mblks and chains them together and sends
13457c478bd9Sstevel@tonic-gate 		 * them to TCP in one shot. It also leaves 'wroff' size
13467c478bd9Sstevel@tonic-gate 		 * space for the headers in each mblk.
13477c478bd9Sstevel@tonic-gate 		 *
13487c478bd9Sstevel@tonic-gate 		 * ii) for total size bigger than 'tcp_slow_start_initial'
13497c478bd9Sstevel@tonic-gate 		 * time maxblk, its probably real file data which is
13507c478bd9Sstevel@tonic-gate 		 * dominating. So its better to use sendvec_chunk because
13517c478bd9Sstevel@tonic-gate 		 * performance goes to dog if we don't do pagesize reads.
13527c478bd9Sstevel@tonic-gate 		 * sendvec_chunk will do pagesize reads and write them
13537c478bd9Sstevel@tonic-gate 		 * in pagesize mblks to TCP.
13547c478bd9Sstevel@tonic-gate 		 *
13557c478bd9Sstevel@tonic-gate 		 * Side Notes: A write to file has not been optimized.
13567c478bd9Sstevel@tonic-gate 		 * Future zero copy code will plugin into sendvec_chunk
13577c478bd9Sstevel@tonic-gate 		 * only because doing zero copy for files smaller then
13587c478bd9Sstevel@tonic-gate 		 * pagesize is useless.
13597c478bd9Sstevel@tonic-gate 		 *
13607c478bd9Sstevel@tonic-gate 		 * Note, if socket has NL7C enabled then call NL7C's
13612c9e429eSbrutus 		 * senfilev() function to consume the sfv[].
13627c478bd9Sstevel@tonic-gate 		 */
13637c478bd9Sstevel@tonic-gate 		if (is_sock) {
13647c478bd9Sstevel@tonic-gate 			switch (so->so_family) {
13657c478bd9Sstevel@tonic-gate 			case AF_INET:
13667c478bd9Sstevel@tonic-gate 			case AF_INET6:
13672c9e429eSbrutus 				if (so->so_nl7c_flags != 0)
13682c9e429eSbrutus 					error = nl7c_sendfilev(so, &fileoff,
13692c9e429eSbrutus 					    sfv, copy_cnt, &count);
1370311e3d4cSgeorges 				else if ((total_size <= (4 * maxblk)) &&
1371311e3d4cSgeorges 				    error == 0)
13727c478bd9Sstevel@tonic-gate 					error = sendvec_small_chunk(fp,
13737c478bd9Sstevel@tonic-gate 					    &fileoff, sfv, copy_cnt,
13747c478bd9Sstevel@tonic-gate 					    total_size, maxblk, &count);
13757c478bd9Sstevel@tonic-gate 				else
13767c478bd9Sstevel@tonic-gate 					error = sendvec_chunk(fp, &fileoff,
13777c478bd9Sstevel@tonic-gate 					    sfv, copy_cnt, &count);
13787c478bd9Sstevel@tonic-gate 				break;
13797c478bd9Sstevel@tonic-gate 			}
13807c478bd9Sstevel@tonic-gate 		} else {
13817c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_type == VREG);
13827c478bd9Sstevel@tonic-gate 			error = sendvec_chunk(fp, &fileoff, sfv, copy_cnt,
13837c478bd9Sstevel@tonic-gate 			    &count);
13847c478bd9Sstevel@tonic-gate 		}
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
13887c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32)
13897c478bd9Sstevel@tonic-gate 		copy_vec = (const struct sendfilevec *)((char *)copy_vec +
13907c478bd9Sstevel@tonic-gate 		    (copy_cnt * sizeof (ksendfilevec32_t)));
13917c478bd9Sstevel@tonic-gate 	else
13927c478bd9Sstevel@tonic-gate #endif
13937c478bd9Sstevel@tonic-gate 		copy_vec += copy_cnt;
13947c478bd9Sstevel@tonic-gate 		sfvcnt -= copy_cnt;
1395311e3d4cSgeorges 
1396311e3d4cSgeorges 	/* Process all vector members up to first error */
1397311e3d4cSgeorges 	} while ((sfvcnt > 0) && first_vector_error == 0 && error == 0);
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG)
14007c478bd9Sstevel@tonic-gate 		fp->f_offset += count;
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
14057c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32) {
14067c478bd9Sstevel@tonic-gate 		ssize32_t count32 = (ssize32_t)count;
14077c478bd9Sstevel@tonic-gate 		if (copyout(&count32, xferred, sizeof (count32)))
14087c478bd9Sstevel@tonic-gate 			error = EFAULT;
14097c478bd9Sstevel@tonic-gate 		releasef(fildes);
14107c478bd9Sstevel@tonic-gate 		if (error != 0)
14117c478bd9Sstevel@tonic-gate 			return (set_errno(error));
1412311e3d4cSgeorges 		if (first_vector_error != 0)
1413311e3d4cSgeorges 			return (set_errno(first_vector_error));
14147c478bd9Sstevel@tonic-gate 		return (count32);
14157c478bd9Sstevel@tonic-gate 	}
14167c478bd9Sstevel@tonic-gate #endif
14177c478bd9Sstevel@tonic-gate 	if (copyout(&count, xferred, sizeof (count)))
14187c478bd9Sstevel@tonic-gate 		error = EFAULT;
14197c478bd9Sstevel@tonic-gate 	releasef(fildes);
14207c478bd9Sstevel@tonic-gate 	if (error != 0)
14217c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1422311e3d4cSgeorges 	if (first_vector_error != 0)
1423311e3d4cSgeorges 		return (set_errno(first_vector_error));
14247c478bd9Sstevel@tonic-gate 	return (count);
14257c478bd9Sstevel@tonic-gate err:
14267c478bd9Sstevel@tonic-gate 	ASSERT(error != 0);
14277c478bd9Sstevel@tonic-gate 	releasef(fildes);
14287c478bd9Sstevel@tonic-gate 	return (set_errno(error));
14297c478bd9Sstevel@tonic-gate }
1430