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 /*
233e95bd4aSAnders Persson  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
24*15f90b02SGarrett D'Amore  * Copyright 2022 Garrett D'Amore
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>
560f1702c5SYu Xiangning #include <fs/sockfs/sockcommon.h>
570f1702c5SYu Xiangning #include <fs/sockfs/socktpi.h>
5845916cd2Sjpk 
597c478bd9Sstevel@tonic-gate #include <netinet/in.h>
607c478bd9Sstevel@tonic-gate #include <sys/sendfile.h>
617c478bd9Sstevel@tonic-gate #include <sys/un.h>
627c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
637c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <inet/common.h>
667c478bd9Sstevel@tonic-gate #include <inet/ip.h>
677c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
687c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate extern int sosendfile64(file_t *, file_t *, const struct ksendfilevec64 *,
717c478bd9Sstevel@tonic-gate 		ssize32_t *);
723b3d24f3SJayakara Kini extern int snf_segmap(file_t *, vnode_t *, u_offset_t, u_offset_t, ssize_t *,
733b3d24f3SJayakara Kini 		boolean_t);
740f1702c5SYu Xiangning extern sotpi_info_t *sotpi_sototpi(struct sonode *);
7574024373Spr 
767c478bd9Sstevel@tonic-gate #define	SEND_MAX_CHUNK	16
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * 64 bit offsets for 32 bit applications only running either on
817c478bd9Sstevel@tonic-gate  * 64 bit kernel or 32 bit kernel. For 32 bit apps, we can't transfer
827c478bd9Sstevel@tonic-gate  * more than 2GB of data.
837c478bd9Sstevel@tonic-gate  */
84940f8eceSRobert Mustacchi static int
sendvec_chunk64(file_t * fp,u_offset_t * fileoff,struct ksendfilevec64 * sfv,int copy_cnt,ssize32_t * count)857c478bd9Sstevel@tonic-gate sendvec_chunk64(file_t *fp, u_offset_t *fileoff, struct ksendfilevec64 *sfv,
867c478bd9Sstevel@tonic-gate     int copy_cnt, ssize32_t *count)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	struct vnode *vp;
897c478bd9Sstevel@tonic-gate 	ushort_t fflag;
907c478bd9Sstevel@tonic-gate 	int ioflag;
917c478bd9Sstevel@tonic-gate 	size32_t cnt;
927c478bd9Sstevel@tonic-gate 	ssize32_t sfv_len;
937c478bd9Sstevel@tonic-gate 	ssize32_t tmpcount;
947c478bd9Sstevel@tonic-gate 	u_offset_t sfv_off;
957c478bd9Sstevel@tonic-gate 	struct uio auio;
967c478bd9Sstevel@tonic-gate 	struct iovec aiov;
977c478bd9Sstevel@tonic-gate 	int i, error;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
1007c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
1017c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING))
1047c478bd9Sstevel@tonic-gate 			return (EINTR);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 		/*
1077c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
1087c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
1097c478bd9Sstevel@tonic-gate 		 */
1107c478bd9Sstevel@tonic-gate 		sfv_len = (ssize32_t)sfv->sfv_len;
1117c478bd9Sstevel@tonic-gate 
112e116a42fSPrakash Jalan 		if (sfv_len == 0) {
113e116a42fSPrakash Jalan 			sfv++;
1147c478bd9Sstevel@tonic-gate 			continue;
115e116a42fSPrakash Jalan 		}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 		if (sfv_len < 0)
1187c478bd9Sstevel@tonic-gate 			return (EINVAL);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG) {
1217c478bd9Sstevel@tonic-gate 			if (*fileoff >= curproc->p_fsz_ctl) {
1227c478bd9Sstevel@tonic-gate 				mutex_enter(&curproc->p_lock);
1237c478bd9Sstevel@tonic-gate 				(void) rctl_action(
1247c478bd9Sstevel@tonic-gate 				    rctlproc_legacy[RLIMIT_FSIZE],
1257c478bd9Sstevel@tonic-gate 				    curproc->p_rctls, curproc, RCA_SAFE);
1267c478bd9Sstevel@tonic-gate 				mutex_exit(&curproc->p_lock);
1277c478bd9Sstevel@tonic-gate 				return (EFBIG);
1287c478bd9Sstevel@tonic-gate 			}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 			if (*fileoff >= OFFSET_MAX(fp))
1317c478bd9Sstevel@tonic-gate 				return (EFBIG);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 			if (*fileoff + sfv_len > OFFSET_MAX(fp))
1347c478bd9Sstevel@tonic-gate 				return (EINVAL);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		tmpcount = *count + sfv_len;
1387c478bd9Sstevel@tonic-gate 		if (tmpcount < 0)
1397c478bd9Sstevel@tonic-gate 			return (EINVAL);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		sfv_off = sfv->sfv_off;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_DEFAULT;
1447c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
1457c478bd9Sstevel@tonic-gate 			aiov.iov_len = sfv_len;
1467c478bd9Sstevel@tonic-gate 			aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
1477c478bd9Sstevel@tonic-gate 			auio.uio_loffset = *fileoff;
1487c478bd9Sstevel@tonic-gate 			auio.uio_iovcnt = 1;
1497c478bd9Sstevel@tonic-gate 			auio.uio_resid = sfv_len;
1507c478bd9Sstevel@tonic-gate 			auio.uio_iov = &aiov;
1517c478bd9Sstevel@tonic-gate 			auio.uio_segflg = UIO_USERSPACE;
1527c478bd9Sstevel@tonic-gate 			auio.uio_llimit = curproc->p_fsz_ctl;
1537c478bd9Sstevel@tonic-gate 			auio.uio_fmode = fflag;
1547c478bd9Sstevel@tonic-gate 			ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
1557c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
1567c478bd9Sstevel@tonic-gate 				error = VOP_WRITE(vp, &auio, ioflag,
1577c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
1587c478bd9Sstevel@tonic-gate 				cnt = sfv_len - auio.uio_resid;
1597c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
1607c478bd9Sstevel@tonic-gate 				ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
1617c478bd9Sstevel@tonic-gate 				if (vp->v_type == VREG)
1627c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
1637c478bd9Sstevel@tonic-gate 				*count += cnt;
1647c478bd9Sstevel@tonic-gate 				if (error != 0)
1657c478bd9Sstevel@tonic-gate 					return (error);
1667c478bd9Sstevel@tonic-gate 			}
1677c478bd9Sstevel@tonic-gate 		} else {
1687c478bd9Sstevel@tonic-gate 			file_t	*ffp;
1697c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
1707c478bd9Sstevel@tonic-gate 			size_t	size;
1717c478bd9Sstevel@tonic-gate 			caddr_t	ptr;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL)
1747c478bd9Sstevel@tonic-gate 				return (EBADF);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
1777c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
1787c478bd9Sstevel@tonic-gate 				return (EBADF);
1797c478bd9Sstevel@tonic-gate 			}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
1827c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
1837c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
1847c478bd9Sstevel@tonic-gate 				return (EINVAL);
1857c478bd9Sstevel@tonic-gate 			}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 			/*
1887c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
1897c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
1907c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
1917c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
1927c478bd9Sstevel@tonic-gate 			 */
193bfb244baSpr 			if (vn_compare(vp, readvp)) {
1947c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
1957c478bd9Sstevel@tonic-gate 				return (EINVAL);
1967c478bd9Sstevel@tonic-gate 			}
1977c478bd9Sstevel@tonic-gate 
198b3528faeSVasumathi Sundaram - Sun Microsystems 			/*
199b3528faeSVasumathi Sundaram - Sun Microsystems 			 * Optimize the regular file over
200b3528faeSVasumathi Sundaram - Sun Microsystems 			 * the socket case.
201b3528faeSVasumathi Sundaram - Sun Microsystems 			 */
202b3528faeSVasumathi Sundaram - Sun Microsystems 			if (vp->v_type == VSOCK) {
2037baf8047SVasumathi Sundaram - Sun Microsystems 				error = sosendfile64(fp, ffp, sfv,
2047baf8047SVasumathi Sundaram - Sun Microsystems 				    (ssize32_t *)&cnt);
2057baf8047SVasumathi Sundaram - Sun Microsystems 				*count += cnt;
206b3528faeSVasumathi Sundaram - Sun Microsystems 				if (error)
207b3528faeSVasumathi Sundaram - Sun Microsystems 					return (error);
208b3528faeSVasumathi Sundaram - Sun Microsystems 				sfv++;
209b3528faeSVasumathi Sundaram - Sun Microsystems 				continue;
210b3528faeSVasumathi Sundaram - Sun Microsystems 			}
211b3528faeSVasumathi Sundaram - Sun Microsystems 
2127c478bd9Sstevel@tonic-gate 			/*
2137c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
2147c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
2157c478bd9Sstevel@tonic-gate 			 */
216b3528faeSVasumathi Sundaram - Sun Microsystems 			if (readvp < vp) {
217b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
218b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
219b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
220b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
221b3528faeSVasumathi Sundaram - Sun Microsystems 			} else {
222b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
223b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
224b3528faeSVasumathi Sundaram - Sun Microsystems 			}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 			/*
2277c478bd9Sstevel@tonic-gate 			 * Same checks as in pread64.
2287c478bd9Sstevel@tonic-gate 			 */
2297c478bd9Sstevel@tonic-gate 			if (sfv_off > MAXOFFSET_T) {
230b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
2317c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
2327c478bd9Sstevel@tonic-gate 				return (EINVAL);
2337c478bd9Sstevel@tonic-gate 			}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 			if (sfv_off + sfv_len > MAXOFFSET_T)
2367c478bd9Sstevel@tonic-gate 				sfv_len = (ssize32_t)(MAXOFFSET_T - sfv_off);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 			/* Find the native blocksize to transfer data */
2397c478bd9Sstevel@tonic-gate 			size = MIN(vp->v_vfsp->vfs_bsize,
2407c478bd9Sstevel@tonic-gate 			    readvp->v_vfsp->vfs_bsize);
2417c478bd9Sstevel@tonic-gate 			size = sfv_len < size ? sfv_len : size;
242b3528faeSVasumathi Sundaram - Sun Microsystems 			ptr = kmem_alloc(size, KM_NOSLEEP);
243b3528faeSVasumathi Sundaram - Sun Microsystems 			if (ptr == NULL) {
244b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
245b3528faeSVasumathi Sundaram - Sun Microsystems 				releasef(sfv->sfv_fd);
246b3528faeSVasumathi Sundaram - Sun Microsystems 				return (ENOMEM);
247b3528faeSVasumathi Sundaram - Sun Microsystems 			}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
2507c478bd9Sstevel@tonic-gate 				size_t	iov_len;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 				iov_len = MIN(size, sfv_len);
2537c478bd9Sstevel@tonic-gate 				aiov.iov_base = ptr;
2547c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
2557c478bd9Sstevel@tonic-gate 				auio.uio_loffset = sfv_off;
2567c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
2577c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
2587c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
2597c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
2607c478bd9Sstevel@tonic-gate 				auio.uio_llimit = MAXOFFSET_T;
2617c478bd9Sstevel@tonic-gate 				auio.uio_fmode = ffp->f_flag;
2627c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
2637c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 				/*
2667c478bd9Sstevel@tonic-gate 				 * If read sync is not asked for,
2677c478bd9Sstevel@tonic-gate 				 * filter sync flags
2687c478bd9Sstevel@tonic-gate 				 */
2697c478bd9Sstevel@tonic-gate 				if ((ioflag & FRSYNC) == 0)
2707c478bd9Sstevel@tonic-gate 					ioflag &= ~(FSYNC|FDSYNC);
2717c478bd9Sstevel@tonic-gate 				error = VOP_READ(readvp, &auio, ioflag,
2727c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
2737c478bd9Sstevel@tonic-gate 				if (error) {
2747c478bd9Sstevel@tonic-gate 					kmem_free(ptr, size);
275b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
276b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
2777c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
2787c478bd9Sstevel@tonic-gate 					return (error);
2797c478bd9Sstevel@tonic-gate 				}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 				/*
2827c478bd9Sstevel@tonic-gate 				 * Check how must data was really read.
2837c478bd9Sstevel@tonic-gate 				 * Decrement the 'len' and increment the
2847c478bd9Sstevel@tonic-gate 				 * 'off' appropriately.
2857c478bd9Sstevel@tonic-gate 				 */
2867c478bd9Sstevel@tonic-gate 				cnt = iov_len - auio.uio_resid;
2877c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
2887c478bd9Sstevel@tonic-gate 					/*
2897c478bd9Sstevel@tonic-gate 					 * If we were reading a pipe (currently
2907c478bd9Sstevel@tonic-gate 					 * not implemented), we may now lose
2917c478bd9Sstevel@tonic-gate 					 * data.
2927c478bd9Sstevel@tonic-gate 					 */
2937c478bd9Sstevel@tonic-gate 					kmem_free(ptr, size);
294b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
295b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
2967c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
2977c478bd9Sstevel@tonic-gate 					return (EINVAL);
2987c478bd9Sstevel@tonic-gate 				}
2997c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
3007c478bd9Sstevel@tonic-gate 				sfv_off += cnt;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 				aiov.iov_base = ptr;
3037c478bd9Sstevel@tonic-gate 				aiov.iov_len = cnt;
3047c478bd9Sstevel@tonic-gate 				auio.uio_loffset = *fileoff;
305a490cd42SPrakash Jalan 				auio.uio_iov = &aiov;
306a490cd42SPrakash Jalan 				auio.uio_iovcnt = 1;
3077c478bd9Sstevel@tonic-gate 				auio.uio_resid = cnt;
3087c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
3097c478bd9Sstevel@tonic-gate 				auio.uio_llimit = curproc->p_fsz_ctl;
3107c478bd9Sstevel@tonic-gate 				auio.uio_fmode = fflag;
3117c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
3127c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
3137c478bd9Sstevel@tonic-gate 				error = VOP_WRITE(vp, &auio, ioflag,
3147c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 				/*
3177c478bd9Sstevel@tonic-gate 				 * Check how much data was written. Increment
3187c478bd9Sstevel@tonic-gate 				 * the 'len' and decrement the 'off' if all
3197c478bd9Sstevel@tonic-gate 				 * the data was not written.
3207c478bd9Sstevel@tonic-gate 				 */
3217c478bd9Sstevel@tonic-gate 				cnt -= auio.uio_resid;
3227c478bd9Sstevel@tonic-gate 				sfv_len += auio.uio_resid;
3237c478bd9Sstevel@tonic-gate 				sfv_off -= auio.uio_resid;
3247c478bd9Sstevel@tonic-gate 				ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
3257c478bd9Sstevel@tonic-gate 				if (vp->v_type == VREG)
3267c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
3277c478bd9Sstevel@tonic-gate 				*count += cnt;
3287c478bd9Sstevel@tonic-gate 				if (error != 0) {
3297c478bd9Sstevel@tonic-gate 					kmem_free(ptr, size);
330b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
331b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
3327c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
3337c478bd9Sstevel@tonic-gate 					return (error);
3347c478bd9Sstevel@tonic-gate 				}
3357c478bd9Sstevel@tonic-gate 			}
336b3528faeSVasumathi Sundaram - Sun Microsystems 			VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
3377c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
3387c478bd9Sstevel@tonic-gate 			kmem_free(ptr, size);
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 		sfv++;
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 	return (0);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
345940f8eceSRobert Mustacchi static ssize32_t
sendvec64(file_t * fp,const struct ksendfilevec64 * vec,int sfvcnt,size32_t * xferred,int fildes)3467c478bd9Sstevel@tonic-gate sendvec64(file_t *fp, const struct ksendfilevec64 *vec, int sfvcnt,
347c99fb8b3SToomas Soome     size32_t *xferred, int fildes)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	u_offset_t		fileoff;
3507c478bd9Sstevel@tonic-gate 	int			copy_cnt;
3517c478bd9Sstevel@tonic-gate 	const struct ksendfilevec64 *copy_vec;
3527c478bd9Sstevel@tonic-gate 	struct ksendfilevec64 sfv[SEND_MAX_CHUNK];
3537c478bd9Sstevel@tonic-gate 	struct vnode *vp;
3547c478bd9Sstevel@tonic-gate 	int error;
3557c478bd9Sstevel@tonic-gate 	ssize32_t count = 0;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
358b3528faeSVasumathi Sundaram - Sun Microsystems 	(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	copy_vec = vec;
3617c478bd9Sstevel@tonic-gate 	fileoff = fp->f_offset;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	do {
3647c478bd9Sstevel@tonic-gate 		copy_cnt = MIN(sfvcnt, SEND_MAX_CHUNK);
3657c478bd9Sstevel@tonic-gate 		if (copyin(copy_vec, sfv, copy_cnt *
3667c478bd9Sstevel@tonic-gate 		    sizeof (struct ksendfilevec64))) {
3677c478bd9Sstevel@tonic-gate 			error = EFAULT;
3687c478bd9Sstevel@tonic-gate 			break;
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 		error = sendvec_chunk64(fp, &fileoff, sfv, copy_cnt, &count);
3727c478bd9Sstevel@tonic-gate 		if (error != 0)
3737c478bd9Sstevel@tonic-gate 			break;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		copy_vec += copy_cnt;
3767c478bd9Sstevel@tonic-gate 		sfvcnt -= copy_cnt;
3777c478bd9Sstevel@tonic-gate 	} while (sfvcnt > 0);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG)
3807c478bd9Sstevel@tonic-gate 		fp->f_offset += count;
3817c478bd9Sstevel@tonic-gate 
382b3528faeSVasumathi Sundaram - Sun Microsystems 	VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
3837c478bd9Sstevel@tonic-gate 	if (copyout(&count, xferred, sizeof (count)))
3847c478bd9Sstevel@tonic-gate 		error = EFAULT;
3857c478bd9Sstevel@tonic-gate 	releasef(fildes);
3867c478bd9Sstevel@tonic-gate 	if (error != 0)
3877c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3887c478bd9Sstevel@tonic-gate 	return (count);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate #endif
3917c478bd9Sstevel@tonic-gate 
392940f8eceSRobert Mustacchi static int
sendvec_small_chunk(file_t * fp,u_offset_t * fileoff,struct sendfilevec * sfv,int copy_cnt,ssize_t total_size,int maxblk,ssize_t * count)3937c478bd9Sstevel@tonic-gate sendvec_small_chunk(file_t *fp, u_offset_t *fileoff, struct sendfilevec *sfv,
3947c478bd9Sstevel@tonic-gate     int copy_cnt, ssize_t total_size, int maxblk, ssize_t *count)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	struct vnode *vp;
3977c478bd9Sstevel@tonic-gate 	struct uio auio;
3987c478bd9Sstevel@tonic-gate 	struct iovec aiov;
3997c478bd9Sstevel@tonic-gate 	ushort_t fflag;
4007c478bd9Sstevel@tonic-gate 	int ioflag;
4017c478bd9Sstevel@tonic-gate 	int i, error;
4027c478bd9Sstevel@tonic-gate 	size_t cnt;
4037c478bd9Sstevel@tonic-gate 	ssize_t sfv_len;
4047c478bd9Sstevel@tonic-gate 	u_offset_t sfv_off;
4057c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4067c478bd9Sstevel@tonic-gate 	model_t model = get_udatamodel();
4077c478bd9Sstevel@tonic-gate 	u_offset_t maxoff = (model == DATAMODEL_ILP32) ?
408311e3d4cSgeorges 	    MAXOFF32_T : MAXOFFSET_T;
4097c478bd9Sstevel@tonic-gate #else
4107c478bd9Sstevel@tonic-gate 	const u_offset_t maxoff = MAXOFF32_T;
4117c478bd9Sstevel@tonic-gate #endif
4127c478bd9Sstevel@tonic-gate 	mblk_t *dmp = NULL;
4137c478bd9Sstevel@tonic-gate 	int wroff;
4147c478bd9Sstevel@tonic-gate 	int buf_left = 0;
4157c478bd9Sstevel@tonic-gate 	size_t	iov_len;
4167c478bd9Sstevel@tonic-gate 	mblk_t  *head, *tmp;
4177c478bd9Sstevel@tonic-gate 	size_t  size = total_size;
418c28749e9Skais 	size_t  extra;
419c28749e9Skais 	int tail_len;
4200f1702c5SYu Xiangning 	struct nmsghdr msg;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
4237c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VSOCK);
4267c478bd9Sstevel@tonic-gate 	ASSERT(maxblk > 0);
4277c478bd9Sstevel@tonic-gate 
428e116a42fSPrakash Jalan 	/* If nothing to send, return */
429e116a42fSPrakash Jalan 	if (total_size == 0)
430e116a42fSPrakash Jalan 		return (0);
431e116a42fSPrakash Jalan 
4320f1702c5SYu Xiangning 	if (vp->v_stream != NULL) {
4330f1702c5SYu Xiangning 		wroff = (int)vp->v_stream->sd_wroff;
4340f1702c5SYu Xiangning 		tail_len = (int)vp->v_stream->sd_tail;
4350f1702c5SYu Xiangning 	} else {
4360f1702c5SYu Xiangning 		struct sonode *so;
4370f1702c5SYu Xiangning 
4380f1702c5SYu Xiangning 		so = VTOSO(vp);
4390f1702c5SYu Xiangning 		wroff = so->so_proto_props.sopp_wroff;
4400f1702c5SYu Xiangning 		tail_len = so->so_proto_props.sopp_tail;
4410f1702c5SYu Xiangning 	}
4420f1702c5SYu Xiangning 
443c28749e9Skais 	extra = wroff + tail_len;
444c28749e9Skais 
4457c478bd9Sstevel@tonic-gate 	buf_left = MIN(total_size, maxblk);
446c28749e9Skais 	head = dmp = allocb(buf_left + extra, BPRI_HI);
4477c478bd9Sstevel@tonic-gate 	if (head == NULL)
4487c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4497c478bd9Sstevel@tonic-gate 	head->b_wptr = head->b_rptr = head->b_rptr + wroff;
4500f1702c5SYu Xiangning 	bzero(&msg, sizeof (msg));
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_DEFAULT;
4537c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
454311e3d4cSgeorges 		if (ISSIG(curthread, JUSTLOOKING)) {
455311e3d4cSgeorges 			freemsg(head);
4567c478bd9Sstevel@tonic-gate 			return (EINTR);
457311e3d4cSgeorges 		}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		/*
4607c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
4617c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
4627c478bd9Sstevel@tonic-gate 		 */
4637c478bd9Sstevel@tonic-gate 		sfv_len = (ssize_t)sfv->sfv_len;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		if (sfv_len == 0) {
4667c478bd9Sstevel@tonic-gate 			sfv++;
4677c478bd9Sstevel@tonic-gate 			continue;
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		/* Check for overflow */
4717c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4727c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_ILP32) {
473311e3d4cSgeorges 			if (((ssize32_t)(*count + sfv_len)) < 0) {
474311e3d4cSgeorges 				freemsg(head);
4757c478bd9Sstevel@tonic-gate 				return (EINVAL);
476311e3d4cSgeorges 			}
4777c478bd9Sstevel@tonic-gate 		} else
4787c478bd9Sstevel@tonic-gate #endif
479311e3d4cSgeorges 		if ((*count + sfv_len) < 0) {
480311e3d4cSgeorges 			freemsg(head);
4817c478bd9Sstevel@tonic-gate 			return (EINVAL);
482311e3d4cSgeorges 		}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		sfv_off = (u_offset_t)(ulong_t)sfv->sfv_off;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
4877c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
4887c478bd9Sstevel@tonic-gate 				if (buf_left == 0) {
4897c478bd9Sstevel@tonic-gate 					tmp = dmp;
4907c478bd9Sstevel@tonic-gate 					buf_left = MIN(total_size, maxblk);
4917c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
492c28749e9Skais 					dmp = allocb(buf_left + extra, BPRI_HI);
4937c478bd9Sstevel@tonic-gate 					if (dmp == NULL) {
4947c478bd9Sstevel@tonic-gate 						freemsg(head);
4957c478bd9Sstevel@tonic-gate 						return (ENOMEM);
4967c478bd9Sstevel@tonic-gate 					}
4977c478bd9Sstevel@tonic-gate 					dmp->b_wptr = dmp->b_rptr =
4987c478bd9Sstevel@tonic-gate 					    dmp->b_rptr + wroff;
4997c478bd9Sstevel@tonic-gate 					tmp->b_cont = dmp;
5007c478bd9Sstevel@tonic-gate 				} else {
5017c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
5027c478bd9Sstevel@tonic-gate 				}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
5057c478bd9Sstevel@tonic-gate 				aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
5067c478bd9Sstevel@tonic-gate 				auio.uio_loffset = *fileoff;
5077c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
5087c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
5097c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
5107c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_USERSPACE;
5117c478bd9Sstevel@tonic-gate 				auio.uio_llimit = curproc->p_fsz_ctl;
5127c478bd9Sstevel@tonic-gate 				auio.uio_fmode = fflag;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 				buf_left -= iov_len;
5157c478bd9Sstevel@tonic-gate 				total_size -= iov_len;
5167c478bd9Sstevel@tonic-gate 				sfv_len -= iov_len;
5177c478bd9Sstevel@tonic-gate 				sfv_off += iov_len;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 				error = uiomove((caddr_t)dmp->b_wptr,
5207c478bd9Sstevel@tonic-gate 				    iov_len, UIO_WRITE, &auio);
5217c478bd9Sstevel@tonic-gate 				if (error != 0) {
5227c478bd9Sstevel@tonic-gate 					freemsg(head);
5237c478bd9Sstevel@tonic-gate 					return (error);
5247c478bd9Sstevel@tonic-gate 				}
5257c478bd9Sstevel@tonic-gate 				dmp->b_wptr += iov_len;
5267c478bd9Sstevel@tonic-gate 			}
5277c478bd9Sstevel@tonic-gate 		} else {
5287c478bd9Sstevel@tonic-gate 			file_t	*ffp;
5297c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL) {
5327c478bd9Sstevel@tonic-gate 				freemsg(head);
5337c478bd9Sstevel@tonic-gate 				return (EBADF);
5347c478bd9Sstevel@tonic-gate 			}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
5377c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
5387c478bd9Sstevel@tonic-gate 				freemsg(head);
5397c478bd9Sstevel@tonic-gate 				return (EACCES);
5407c478bd9Sstevel@tonic-gate 			}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
5437c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
5447c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
5457c478bd9Sstevel@tonic-gate 				freemsg(head);
5467c478bd9Sstevel@tonic-gate 				return (EINVAL);
5477c478bd9Sstevel@tonic-gate 			}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 			/*
5507c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
5517c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
5527c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
5537c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
5547c478bd9Sstevel@tonic-gate 			 */
5557c478bd9Sstevel@tonic-gate 
556bfb244baSpr 			if (vn_compare(vp, readvp)) {
5577c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
5587c478bd9Sstevel@tonic-gate 				freemsg(head);
5597c478bd9Sstevel@tonic-gate 				return (EINVAL);
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 			/*
5637c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
5647c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
5657c478bd9Sstevel@tonic-gate 			 */
5667c478bd9Sstevel@tonic-gate 
567b3528faeSVasumathi Sundaram - Sun Microsystems 			if (readvp < vp) {
568b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
569b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
570b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
571b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
572b3528faeSVasumathi Sundaram - Sun Microsystems 			} else {
573b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
574b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
575b3528faeSVasumathi Sundaram - Sun Microsystems 			}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 			/* Same checks as in pread */
5787c478bd9Sstevel@tonic-gate 			if (sfv_off > maxoff) {
579b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
5807c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
5817c478bd9Sstevel@tonic-gate 				freemsg(head);
5827c478bd9Sstevel@tonic-gate 				return (EINVAL);
5837c478bd9Sstevel@tonic-gate 			}
5847c478bd9Sstevel@tonic-gate 			if (sfv_off + sfv_len > maxoff) {
5850a8433caSrshoaib 				total_size -= (sfv_off + sfv_len - maxoff);
5867c478bd9Sstevel@tonic-gate 				sfv_len = (ssize_t)((offset_t)maxoff -
5877c478bd9Sstevel@tonic-gate 				    sfv_off);
5887c478bd9Sstevel@tonic-gate 			}
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
5917c478bd9Sstevel@tonic-gate 				if (buf_left == 0) {
5927c478bd9Sstevel@tonic-gate 					tmp = dmp;
5937c478bd9Sstevel@tonic-gate 					buf_left = MIN(total_size, maxblk);
5947c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
595c28749e9Skais 					dmp = allocb(buf_left + extra, BPRI_HI);
5967c478bd9Sstevel@tonic-gate 					if (dmp == NULL) {
597b3528faeSVasumathi Sundaram - Sun Microsystems 						VOP_RWUNLOCK(readvp,
598b3528faeSVasumathi Sundaram - Sun Microsystems 						    V_WRITELOCK_FALSE, NULL);
5997c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
6007c478bd9Sstevel@tonic-gate 						freemsg(head);
6017c478bd9Sstevel@tonic-gate 						return (ENOMEM);
6027c478bd9Sstevel@tonic-gate 					}
6037c478bd9Sstevel@tonic-gate 					dmp->b_wptr = dmp->b_rptr =
6047c478bd9Sstevel@tonic-gate 					    dmp->b_rptr + wroff;
6057c478bd9Sstevel@tonic-gate 					tmp->b_cont = dmp;
6067c478bd9Sstevel@tonic-gate 				} else {
6077c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
6087c478bd9Sstevel@tonic-gate 				}
6097c478bd9Sstevel@tonic-gate 				aiov.iov_base = (caddr_t)dmp->b_wptr;
6107c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
6117c478bd9Sstevel@tonic-gate 				auio.uio_loffset = sfv_off;
6127c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
6137c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
6147c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
6157c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
6167c478bd9Sstevel@tonic-gate 				auio.uio_llimit = MAXOFFSET_T;
6177c478bd9Sstevel@tonic-gate 				auio.uio_fmode = ffp->f_flag;
6187c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
6197c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 				/*
6227c478bd9Sstevel@tonic-gate 				 * If read sync is not asked for,
6237c478bd9Sstevel@tonic-gate 				 * filter sync flags
6247c478bd9Sstevel@tonic-gate 				 */
6257c478bd9Sstevel@tonic-gate 				if ((ioflag & FRSYNC) == 0)
6267c478bd9Sstevel@tonic-gate 					ioflag &= ~(FSYNC|FDSYNC);
6277c478bd9Sstevel@tonic-gate 				error = VOP_READ(readvp, &auio, ioflag,
6287c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
6297c478bd9Sstevel@tonic-gate 				if (error != 0) {
6307c478bd9Sstevel@tonic-gate 					/*
6317c478bd9Sstevel@tonic-gate 					 * If we were reading a pipe (currently
6327c478bd9Sstevel@tonic-gate 					 * not implemented), we may now loose
6337c478bd9Sstevel@tonic-gate 					 * data.
6347c478bd9Sstevel@tonic-gate 					 */
635b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
636b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
6377c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
6387c478bd9Sstevel@tonic-gate 					freemsg(head);
6397c478bd9Sstevel@tonic-gate 					return (error);
6407c478bd9Sstevel@tonic-gate 				}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 				/*
6437c478bd9Sstevel@tonic-gate 				 * Check how much data was really read.
6447c478bd9Sstevel@tonic-gate 				 * Decrement the 'len' and increment the
6457c478bd9Sstevel@tonic-gate 				 * 'off' appropriately.
6467c478bd9Sstevel@tonic-gate 				 */
6477c478bd9Sstevel@tonic-gate 				cnt = iov_len - auio.uio_resid;
6487c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
649b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
650b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
6517c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
6527c478bd9Sstevel@tonic-gate 					freemsg(head);
6537c478bd9Sstevel@tonic-gate 					return (EINVAL);
6547c478bd9Sstevel@tonic-gate 				}
6557c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
6567c478bd9Sstevel@tonic-gate 				sfv_off += cnt;
6577c478bd9Sstevel@tonic-gate 				total_size -= cnt;
6587c478bd9Sstevel@tonic-gate 				buf_left -= cnt;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 				dmp->b_wptr += cnt;
6617c478bd9Sstevel@tonic-gate 			}
662b3528faeSVasumathi Sundaram - Sun Microsystems 			VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
6637c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
6647c478bd9Sstevel@tonic-gate 		}
6657c478bd9Sstevel@tonic-gate 		sfv++;
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	ASSERT(total_size == 0);
6690f1702c5SYu Xiangning 	error = socket_sendmblk(VTOSO(vp), &msg, fflag, CRED(), &head);
6707c478bd9Sstevel@tonic-gate 	if (error != 0) {
6710f1702c5SYu Xiangning 		if (head != NULL)
6720f1702c5SYu Xiangning 			freemsg(head);
6737c478bd9Sstevel@tonic-gate 		return (error);
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_ru.ioch += (ulong_t)size;
6767c478bd9Sstevel@tonic-gate 	*count += size;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	return (0);
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 
682940f8eceSRobert Mustacchi static int
sendvec_chunk(file_t * fp,u_offset_t * fileoff,struct sendfilevec * sfv,int copy_cnt,ssize_t * count)6837c478bd9Sstevel@tonic-gate sendvec_chunk(file_t *fp, u_offset_t *fileoff, struct sendfilevec *sfv,
6847c478bd9Sstevel@tonic-gate     int copy_cnt, ssize_t *count)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	struct vnode *vp;
6877c478bd9Sstevel@tonic-gate 	struct uio auio;
6887c478bd9Sstevel@tonic-gate 	struct iovec aiov;
6897c478bd9Sstevel@tonic-gate 	ushort_t fflag;
6907c478bd9Sstevel@tonic-gate 	int ioflag;
6917c478bd9Sstevel@tonic-gate 	int i, error;
6927c478bd9Sstevel@tonic-gate 	size_t cnt;
6937c478bd9Sstevel@tonic-gate 	ssize_t sfv_len;
6947c478bd9Sstevel@tonic-gate 	u_offset_t sfv_off;
6957c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
6967c478bd9Sstevel@tonic-gate 	model_t model = get_udatamodel();
6977c478bd9Sstevel@tonic-gate 	u_offset_t maxoff = (model == DATAMODEL_ILP32) ?
698311e3d4cSgeorges 	    MAXOFF32_T : MAXOFFSET_T;
6997c478bd9Sstevel@tonic-gate #else
7007c478bd9Sstevel@tonic-gate 	const u_offset_t maxoff = MAXOFF32_T;
7017c478bd9Sstevel@tonic-gate #endif
7021574ae68Sjohansen 	mblk_t	*dmp = NULL;
7031574ae68Sjohansen 	char	*buf = NULL;
704c6f039c7SToomas Soome 	size_t  extra = 0;
705c28749e9Skais 	int maxblk, wroff, tail_len;
706c28749e9Skais 	struct sonode *so;
707c28749e9Skais 	stdata_t *stp;
7080f1702c5SYu Xiangning 	struct nmsghdr msg;
7097c478bd9Sstevel@tonic-gate 
710c6f039c7SToomas Soome 	maxblk = 0;
711c6f039c7SToomas Soome 	wroff = 0;
7127c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
7137c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
714c6f039c7SToomas Soome 	so = NULL;
715c6f039c7SToomas Soome 	stp = NULL;
7167c478bd9Sstevel@tonic-gate 
717c28749e9Skais 	if (vp->v_type == VSOCK) {
718c28749e9Skais 		so = VTOSO(vp);
7190f1702c5SYu Xiangning 		if (vp->v_stream != NULL) {
7200f1702c5SYu Xiangning 			stp = vp->v_stream;
7210f1702c5SYu Xiangning 			wroff = (int)stp->sd_wroff;
7220f1702c5SYu Xiangning 			tail_len = (int)stp->sd_tail;
7230f1702c5SYu Xiangning 			maxblk = (int)stp->sd_maxblk;
7240f1702c5SYu Xiangning 		} else {
7250f1702c5SYu Xiangning 			stp = NULL;
7260f1702c5SYu Xiangning 			wroff = so->so_proto_props.sopp_wroff;
7270f1702c5SYu Xiangning 			tail_len = so->so_proto_props.sopp_tail;
7280f1702c5SYu Xiangning 			maxblk = so->so_proto_props.sopp_maxblk;
7290f1702c5SYu Xiangning 		}
730c28749e9Skais 		extra = wroff + tail_len;
731c28749e9Skais 	}
732c28749e9Skais 
7330f1702c5SYu Xiangning 	bzero(&msg, sizeof (msg));
7347c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_DEFAULT;
7357c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
7367c478bd9Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING))
7377c478bd9Sstevel@tonic-gate 			return (EINTR);
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		/*
7407c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
7417c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
7427c478bd9Sstevel@tonic-gate 		 */
7437c478bd9Sstevel@tonic-gate 		sfv_len = (ssize_t)sfv->sfv_len;
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 		if (sfv_len == 0) {
7467c478bd9Sstevel@tonic-gate 			sfv++;
7477c478bd9Sstevel@tonic-gate 			continue;
7487c478bd9Sstevel@tonic-gate 		}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG) {
7517c478bd9Sstevel@tonic-gate 			if (*fileoff >= curproc->p_fsz_ctl) {
7527c478bd9Sstevel@tonic-gate 				mutex_enter(&curproc->p_lock);
7537c478bd9Sstevel@tonic-gate 				(void) rctl_action(
7547c478bd9Sstevel@tonic-gate 				    rctlproc_legacy[RLIMIT_FSIZE],
7557c478bd9Sstevel@tonic-gate 				    curproc->p_rctls, curproc, RCA_SAFE);
7567c478bd9Sstevel@tonic-gate 				mutex_exit(&curproc->p_lock);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 				return (EFBIG);
7597c478bd9Sstevel@tonic-gate 			}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 			if (*fileoff >= maxoff)
7627c478bd9Sstevel@tonic-gate 				return (EFBIG);
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 			if (*fileoff + sfv_len > maxoff)
7657c478bd9Sstevel@tonic-gate 				return (EINVAL);
7667c478bd9Sstevel@tonic-gate 		}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		/* Check for overflow */
7697c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
7707c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_ILP32) {
7717c478bd9Sstevel@tonic-gate 			if (((ssize32_t)(*count + sfv_len)) < 0)
7727c478bd9Sstevel@tonic-gate 				return (EINVAL);
7737c478bd9Sstevel@tonic-gate 		} else
7747c478bd9Sstevel@tonic-gate #endif
7757c478bd9Sstevel@tonic-gate 		if ((*count + sfv_len) < 0)
7767c478bd9Sstevel@tonic-gate 			return (EINVAL);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		sfv_off = (u_offset_t)(ulong_t)sfv->sfv_off;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
7817c478bd9Sstevel@tonic-gate 			if (vp->v_type == VSOCK) {
782e116a42fSPrakash Jalan 				while (sfv_len > 0) {
783e116a42fSPrakash Jalan 					size_t iov_len;
784e116a42fSPrakash Jalan 
785e116a42fSPrakash Jalan 					iov_len = sfv_len;
7863e95bd4aSAnders Persson 					/*
7873e95bd4aSAnders Persson 					 * Socket filters can limit the mblk
7883e95bd4aSAnders Persson 					 * size, so limit reads to maxblk if
7893e95bd4aSAnders Persson 					 * there are filters present.
7903e95bd4aSAnders Persson 					 */
791dd49f125SAnders Persson 					if (so->so_filter_active > 0 &&
792dd49f125SAnders Persson 					    maxblk != INFPSZ)
793e116a42fSPrakash Jalan 						iov_len = MIN(iov_len, maxblk);
794e116a42fSPrakash Jalan 
795e116a42fSPrakash Jalan 					aiov.iov_len = iov_len;
796e116a42fSPrakash Jalan 					aiov.iov_base =
797e116a42fSPrakash Jalan 					    (caddr_t)(uintptr_t)sfv_off;
798e116a42fSPrakash Jalan 
799e116a42fSPrakash Jalan 					auio.uio_iov = &aiov;
800e116a42fSPrakash Jalan 					auio.uio_iovcnt = 1;
801e116a42fSPrakash Jalan 					auio.uio_loffset = *fileoff;
802e116a42fSPrakash Jalan 					auio.uio_segflg = UIO_USERSPACE;
803e116a42fSPrakash Jalan 					auio.uio_fmode = fflag;
804e116a42fSPrakash Jalan 					auio.uio_llimit = curproc->p_fsz_ctl;
805e116a42fSPrakash Jalan 					auio.uio_resid = iov_len;
806e116a42fSPrakash Jalan 
807e116a42fSPrakash Jalan 					dmp = allocb(iov_len + extra, BPRI_HI);
808e116a42fSPrakash Jalan 					if (dmp == NULL)
809e116a42fSPrakash Jalan 						return (ENOMEM);
810e116a42fSPrakash Jalan 					dmp->b_wptr = dmp->b_rptr =
811e116a42fSPrakash Jalan 					    dmp->b_rptr + wroff;
812e116a42fSPrakash Jalan 					error = uiomove((caddr_t)dmp->b_wptr,
813e116a42fSPrakash Jalan 					    iov_len, UIO_WRITE, &auio);
814e116a42fSPrakash Jalan 					if (error != 0) {
815e116a42fSPrakash Jalan 						freeb(dmp);
816e116a42fSPrakash Jalan 						return (error);
817e116a42fSPrakash Jalan 					}
818e116a42fSPrakash Jalan 					dmp->b_wptr += iov_len;
8190f1702c5SYu Xiangning 					error = socket_sendmblk(VTOSO(vp),
8200f1702c5SYu Xiangning 					    &msg, fflag, CRED(), &dmp);
8210f1702c5SYu Xiangning 
822e116a42fSPrakash Jalan 					if (error != 0) {
8230f1702c5SYu Xiangning 						if (dmp != NULL)
8240f1702c5SYu Xiangning 							freeb(dmp);
825e116a42fSPrakash Jalan 						return (error);
826e116a42fSPrakash Jalan 					}
827e116a42fSPrakash Jalan 					ttolwp(curthread)->lwp_ru.ioch +=
828e116a42fSPrakash Jalan 					    (ulong_t)iov_len;
829e116a42fSPrakash Jalan 					*count += iov_len;
830e116a42fSPrakash Jalan 					sfv_len -= iov_len;
831e116a42fSPrakash Jalan 					sfv_off += iov_len;
8327c478bd9Sstevel@tonic-gate 				}
8337c478bd9Sstevel@tonic-gate 			} else {
834e116a42fSPrakash Jalan 				aiov.iov_len = sfv_len;
835e116a42fSPrakash Jalan 				aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
836e116a42fSPrakash Jalan 
837e116a42fSPrakash Jalan 				auio.uio_iov = &aiov;
838e116a42fSPrakash Jalan 				auio.uio_iovcnt = 1;
839e116a42fSPrakash Jalan 				auio.uio_loffset = *fileoff;
840e116a42fSPrakash Jalan 				auio.uio_segflg = UIO_USERSPACE;
841e116a42fSPrakash Jalan 				auio.uio_fmode = fflag;
842e116a42fSPrakash Jalan 				auio.uio_llimit = curproc->p_fsz_ctl;
843e116a42fSPrakash Jalan 				auio.uio_resid = sfv_len;
844e116a42fSPrakash Jalan 
8457c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
8467c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
8477c478bd9Sstevel@tonic-gate 				while (sfv_len > 0) {
8487c478bd9Sstevel@tonic-gate 					error = VOP_WRITE(vp, &auio, ioflag,
8497c478bd9Sstevel@tonic-gate 					    fp->f_cred, NULL);
8507c478bd9Sstevel@tonic-gate 					cnt = sfv_len - auio.uio_resid;
8517c478bd9Sstevel@tonic-gate 					sfv_len -= cnt;
8527c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
8537c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
8547c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
8557c478bd9Sstevel@tonic-gate 					*count += cnt;
8567c478bd9Sstevel@tonic-gate 					if (error != 0)
8577c478bd9Sstevel@tonic-gate 						return (error);
8587c478bd9Sstevel@tonic-gate 				}
8597c478bd9Sstevel@tonic-gate 			}
8607c478bd9Sstevel@tonic-gate 		} else {
8617f9ee850Sblu 			int segmapit = 0;
8627c478bd9Sstevel@tonic-gate 			file_t	*ffp;
8637c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
864bfb244baSpr 			struct vnode *realvp;
8657c478bd9Sstevel@tonic-gate 			size_t	size;
8667c478bd9Sstevel@tonic-gate 			caddr_t	ptr;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL)
8697c478bd9Sstevel@tonic-gate 				return (EBADF);
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
8727c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
8737c478bd9Sstevel@tonic-gate 				return (EBADF);
8747c478bd9Sstevel@tonic-gate 			}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
877da6c28aaSamw 			if (VOP_REALVP(readvp, &realvp, NULL) == 0)
878bfb244baSpr 				readvp = realvp;
8797c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
8807c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
8817c478bd9Sstevel@tonic-gate 				return (EINVAL);
8827c478bd9Sstevel@tonic-gate 			}
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 			/*
8857c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
8867c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
8877c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
8887c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
8897c478bd9Sstevel@tonic-gate 			 */
890bfb244baSpr 			if (vn_compare(vp, readvp)) {
8917c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
8927c478bd9Sstevel@tonic-gate 				return (EINVAL);
8937c478bd9Sstevel@tonic-gate 			}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 			/*
8967c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
8977c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
8987c478bd9Sstevel@tonic-gate 			 */
899b3528faeSVasumathi Sundaram - Sun Microsystems 			if (readvp < vp) {
900b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
901b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
902b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
903b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
904b3528faeSVasumathi Sundaram - Sun Microsystems 			} else {
905b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
906b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
907b3528faeSVasumathi Sundaram - Sun Microsystems 			}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 			/* Same checks as in pread */
9107c478bd9Sstevel@tonic-gate 			if (sfv_off > maxoff) {
911b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
9127c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
9137c478bd9Sstevel@tonic-gate 				return (EINVAL);
9147c478bd9Sstevel@tonic-gate 			}
9157c478bd9Sstevel@tonic-gate 			if (sfv_off + sfv_len > maxoff) {
9167c478bd9Sstevel@tonic-gate 				sfv_len = (ssize_t)((offset_t)maxoff -
9177c478bd9Sstevel@tonic-gate 				    sfv_off);
9187c478bd9Sstevel@tonic-gate 			}
9197c478bd9Sstevel@tonic-gate 			/* Find the native blocksize to transfer data */
9207c478bd9Sstevel@tonic-gate 			size = MIN(vp->v_vfsp->vfs_bsize,
9217c478bd9Sstevel@tonic-gate 			    readvp->v_vfsp->vfs_bsize);
9227c478bd9Sstevel@tonic-gate 			size = sfv_len < size ? sfv_len : size;
9237c478bd9Sstevel@tonic-gate 
9241574ae68Sjohansen 			if (vp->v_type != VSOCK) {
92574024373Spr 				segmapit = 0;
9261574ae68Sjohansen 				buf = kmem_alloc(size, KM_NOSLEEP);
9271574ae68Sjohansen 				if (buf == NULL) {
928b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
929b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
9301574ae68Sjohansen 					releasef(sfv->sfv_fd);
9311574ae68Sjohansen 					return (ENOMEM);
9321574ae68Sjohansen 				}
933c28749e9Skais 			} else {
9340f1702c5SYu Xiangning 				uint_t	copyflag;
9350f1702c5SYu Xiangning 
9360f1702c5SYu Xiangning 				copyflag = stp != NULL ? stp->sd_copyflag :
9370f1702c5SYu Xiangning 				    so->so_proto_props.sopp_zcopyflag;
9383e95bd4aSAnders Persson 
939c28749e9Skais 				/*
9403e95bd4aSAnders Persson 				 * Socket filters can limit the mblk size,
9413e95bd4aSAnders Persson 				 * so limit reads to maxblk if there are
9423e95bd4aSAnders Persson 				 * filters present.
943c28749e9Skais 				 */
944dd49f125SAnders Persson 				if (so->so_filter_active > 0 &&
945dd49f125SAnders Persson 				    maxblk != INFPSZ)
946c28749e9Skais 					size = MIN(size, maxblk);
94774024373Spr 
94874024373Spr 				if (vn_has_flocks(readvp) ||
94974024373Spr 				    readvp->v_flag & VNOMAP ||
9500f1702c5SYu Xiangning 				    copyflag & STZCVMUNSAFE) {
95174024373Spr 					segmapit = 0;
9520f1702c5SYu Xiangning 				} else if (copyflag & STZCVMSAFE) {
95374024373Spr 					segmapit = 1;
95474024373Spr 				} else {
95574024373Spr 					int on = 1;
9560f1702c5SYu Xiangning 					if (socket_setsockopt(VTOSO(vp),
95774024373Spr 					    SOL_SOCKET, SO_SND_COPYAVOID,
9580f1702c5SYu Xiangning 					    &on, sizeof (on), CRED()) == 0)
9598cd31312SMarcel Telka 						segmapit = 1;
96074024373Spr 				}
96174024373Spr 			}
96274024373Spr 
96374024373Spr 			if (segmapit) {
9648cd31312SMarcel Telka 				struct vattr va;
96574024373Spr 				boolean_t nowait;
96674024373Spr 
9678cd31312SMarcel Telka 				va.va_mask = AT_SIZE;
9688cd31312SMarcel Telka 				error = VOP_GETATTR(readvp, &va, 0, kcred,
9698cd31312SMarcel Telka 				    NULL);
9708cd31312SMarcel Telka 				if (error != 0 || sfv_off >= va.va_size) {
9718cd31312SMarcel Telka 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
9728cd31312SMarcel Telka 					    NULL);
9738cd31312SMarcel Telka 					releasef(sfv->sfv_fd);
9748cd31312SMarcel Telka 					return (error);
9758cd31312SMarcel Telka 				}
9768cd31312SMarcel Telka 				/* Read as much as possible. */
9778cd31312SMarcel Telka 				if (sfv_off + sfv_len > va.va_size)
9788cd31312SMarcel Telka 					sfv_len = va.va_size - sfv_off;
9798cd31312SMarcel Telka 
98074024373Spr 				nowait = (sfv->sfv_flag & SFV_NOWAIT) != 0;
98174024373Spr 				error = snf_segmap(fp, readvp, sfv_off,
9823b3d24f3SJayakara Kini 				    (u_offset_t)sfv_len, (ssize_t *)&cnt,
9833b3d24f3SJayakara Kini 				    nowait);
98474024373Spr 				releasef(sfv->sfv_fd);
98574024373Spr 				*count += cnt;
98674024373Spr 				if (error)
98774024373Spr 					return (error);
98874024373Spr 				sfv++;
98974024373Spr 				continue;
9901574ae68Sjohansen 			}
9911574ae68Sjohansen 
9927c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
9937c478bd9Sstevel@tonic-gate 				size_t	iov_len;
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 				iov_len = MIN(size, sfv_len);
9967c478bd9Sstevel@tonic-gate 
9971574ae68Sjohansen 				if (vp->v_type == VSOCK) {
998c28749e9Skais 					dmp = allocb(iov_len + extra, BPRI_HI);
9991574ae68Sjohansen 					if (dmp == NULL) {
1000b3528faeSVasumathi Sundaram - Sun Microsystems 						VOP_RWUNLOCK(readvp,
1001b3528faeSVasumathi Sundaram - Sun Microsystems 						    V_WRITELOCK_FALSE, NULL);
10021574ae68Sjohansen 						releasef(sfv->sfv_fd);
10031574ae68Sjohansen 						return (ENOMEM);
10041574ae68Sjohansen 					}
1005c28749e9Skais 					dmp->b_wptr = dmp->b_rptr =
1006c28749e9Skais 					    dmp->b_rptr + wroff;
10071574ae68Sjohansen 					ptr = (caddr_t)dmp->b_rptr;
10081574ae68Sjohansen 				} else {
10091574ae68Sjohansen 					ptr = buf;
10107c478bd9Sstevel@tonic-gate 				}
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 				aiov.iov_base = ptr;
10137c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
10147c478bd9Sstevel@tonic-gate 				auio.uio_loffset = sfv_off;
10157c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
10167c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
10177c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
10187c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
10197c478bd9Sstevel@tonic-gate 				auio.uio_llimit = MAXOFFSET_T;
10207c478bd9Sstevel@tonic-gate 				auio.uio_fmode = ffp->f_flag;
10217c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
10227c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 				/*
10257c478bd9Sstevel@tonic-gate 				 * If read sync is not asked for,
10267c478bd9Sstevel@tonic-gate 				 * filter sync flags
10277c478bd9Sstevel@tonic-gate 				 */
10287c478bd9Sstevel@tonic-gate 				if ((ioflag & FRSYNC) == 0)
10297c478bd9Sstevel@tonic-gate 					ioflag &= ~(FSYNC|FDSYNC);
10307c478bd9Sstevel@tonic-gate 				error = VOP_READ(readvp, &auio, ioflag,
10317c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
10327c478bd9Sstevel@tonic-gate 				if (error != 0) {
10337c478bd9Sstevel@tonic-gate 					/*
10347c478bd9Sstevel@tonic-gate 					 * If we were reading a pipe (currently
10357c478bd9Sstevel@tonic-gate 					 * not implemented), we may now lose
10367c478bd9Sstevel@tonic-gate 					 * data.
10377c478bd9Sstevel@tonic-gate 					 */
10381574ae68Sjohansen 					if (vp->v_type == VSOCK)
10391574ae68Sjohansen 						freeb(dmp);
10401574ae68Sjohansen 					else
10411574ae68Sjohansen 						kmem_free(buf, size);
1042b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
1043b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
10447c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
10457c478bd9Sstevel@tonic-gate 					return (error);
10467c478bd9Sstevel@tonic-gate 				}
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 				/*
10497c478bd9Sstevel@tonic-gate 				 * Check how much data was really read.
10507c478bd9Sstevel@tonic-gate 				 * Decrement the 'len' and increment the
10517c478bd9Sstevel@tonic-gate 				 * 'off' appropriately.
10527c478bd9Sstevel@tonic-gate 				 */
10537c478bd9Sstevel@tonic-gate 				cnt = iov_len - auio.uio_resid;
10547c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
10551574ae68Sjohansen 					if (vp->v_type == VSOCK)
10561574ae68Sjohansen 						freeb(dmp);
10571574ae68Sjohansen 					else
10581574ae68Sjohansen 						kmem_free(buf, size);
1059b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
1060b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
10617c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
10627c478bd9Sstevel@tonic-gate 					return (EINVAL);
10637c478bd9Sstevel@tonic-gate 				}
10647c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
10657c478bd9Sstevel@tonic-gate 				sfv_off += cnt;
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 				if (vp->v_type == VSOCK) {
10687c478bd9Sstevel@tonic-gate 					dmp->b_wptr = dmp->b_rptr + cnt;
10697c478bd9Sstevel@tonic-gate 
10700f1702c5SYu Xiangning 					error = socket_sendmblk(VTOSO(vp),
10710f1702c5SYu Xiangning 					    &msg, fflag, CRED(), &dmp);
10720f1702c5SYu Xiangning 
10737c478bd9Sstevel@tonic-gate 					if (error != 0) {
10740f1702c5SYu Xiangning 						if (dmp != NULL)
10750f1702c5SYu Xiangning 							freeb(dmp);
1076b3528faeSVasumathi Sundaram - Sun Microsystems 						VOP_RWUNLOCK(readvp,
1077b3528faeSVasumathi Sundaram - Sun Microsystems 						    V_WRITELOCK_FALSE, NULL);
10787c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
10797c478bd9Sstevel@tonic-gate 						return (error);
10807c478bd9Sstevel@tonic-gate 					}
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
10837c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
10847c478bd9Sstevel@tonic-gate 					*count += cnt;
10857c478bd9Sstevel@tonic-gate 				} else {
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 					aiov.iov_base = ptr;
10887c478bd9Sstevel@tonic-gate 					aiov.iov_len = cnt;
10897c478bd9Sstevel@tonic-gate 					auio.uio_loffset = *fileoff;
10907c478bd9Sstevel@tonic-gate 					auio.uio_resid = cnt;
1091a490cd42SPrakash Jalan 					auio.uio_iov = &aiov;
1092a490cd42SPrakash Jalan 					auio.uio_iovcnt = 1;
10937c478bd9Sstevel@tonic-gate 					auio.uio_segflg = UIO_SYSSPACE;
10947c478bd9Sstevel@tonic-gate 					auio.uio_llimit = curproc->p_fsz_ctl;
10957c478bd9Sstevel@tonic-gate 					auio.uio_fmode = fflag;
10967c478bd9Sstevel@tonic-gate 					ioflag = auio.uio_fmode &
10977c478bd9Sstevel@tonic-gate 					    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
10987c478bd9Sstevel@tonic-gate 					error = VOP_WRITE(vp, &auio, ioflag,
10997c478bd9Sstevel@tonic-gate 					    fp->f_cred, NULL);
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 					/*
11027c478bd9Sstevel@tonic-gate 					 * Check how much data was written.
11037c478bd9Sstevel@tonic-gate 					 * Increment the 'len' and decrement the
11047c478bd9Sstevel@tonic-gate 					 * 'off' if all the data was not
11057c478bd9Sstevel@tonic-gate 					 * written.
11067c478bd9Sstevel@tonic-gate 					 */
11077c478bd9Sstevel@tonic-gate 					cnt -= auio.uio_resid;
11087c478bd9Sstevel@tonic-gate 					sfv_len += auio.uio_resid;
11097c478bd9Sstevel@tonic-gate 					sfv_off -= auio.uio_resid;
11107c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
11117c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
11127c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
11137c478bd9Sstevel@tonic-gate 					*count += cnt;
11147c478bd9Sstevel@tonic-gate 					if (error != 0) {
11152db6c79fSstevel 						kmem_free(buf, size);
1116b3528faeSVasumathi Sundaram - Sun Microsystems 						VOP_RWUNLOCK(readvp,
1117b3528faeSVasumathi Sundaram - Sun Microsystems 						    V_WRITELOCK_FALSE, NULL);
11187c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
11197c478bd9Sstevel@tonic-gate 						return (error);
11207c478bd9Sstevel@tonic-gate 					}
11217c478bd9Sstevel@tonic-gate 				}
11227c478bd9Sstevel@tonic-gate 			}
11231574ae68Sjohansen 			if (buf) {
11241574ae68Sjohansen 				kmem_free(buf, size);
11251574ae68Sjohansen 				buf = NULL;
11261574ae68Sjohansen 			}
1127b3528faeSVasumathi Sundaram - Sun Microsystems 			VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
11287c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
11297c478bd9Sstevel@tonic-gate 		}
11307c478bd9Sstevel@tonic-gate 		sfv++;
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 	return (0);
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate ssize_t
sendfilev(int opcode,int fildes,const struct sendfilevec * vec,int sfvcnt,size_t * xferred)11367c478bd9Sstevel@tonic-gate sendfilev(int opcode, int fildes, const struct sendfilevec *vec, int sfvcnt,
11377c478bd9Sstevel@tonic-gate     size_t *xferred)
11387c478bd9Sstevel@tonic-gate {
1139311e3d4cSgeorges 	int error = 0;
1140311e3d4cSgeorges 	int first_vector_error = 0;
11417c478bd9Sstevel@tonic-gate 	file_t *fp;
11427c478bd9Sstevel@tonic-gate 	struct vnode *vp;
1143c6f039c7SToomas Soome 	struct sonode *so = NULL;
11447c478bd9Sstevel@tonic-gate 	u_offset_t fileoff;
11457c478bd9Sstevel@tonic-gate 	int copy_cnt;
11467c478bd9Sstevel@tonic-gate 	const struct sendfilevec *copy_vec;
11477c478bd9Sstevel@tonic-gate 	struct sendfilevec sfv[SEND_MAX_CHUNK];
11487c478bd9Sstevel@tonic-gate 	ssize_t count = 0;
11497c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11507c478bd9Sstevel@tonic-gate 	struct ksendfilevec32 sfv32[SEND_MAX_CHUNK];
11517c478bd9Sstevel@tonic-gate #endif
11520a8433caSrshoaib 	ssize_t total_size;
11537c478bd9Sstevel@tonic-gate 	int i;
11547c478bd9Sstevel@tonic-gate 	boolean_t is_sock = B_FALSE;
11557c478bd9Sstevel@tonic-gate 	int maxblk = 0;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	if (sfvcnt <= 0)
11587c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	if ((fp = getf(fildes)) == NULL)
11617c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	if (((fp->f_flag) & FWRITE) == 0) {
11647c478bd9Sstevel@tonic-gate 		error = EBADF;
11657c478bd9Sstevel@tonic-gate 		goto err;
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	fileoff = fp->f_offset;
11697c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	switch (vp->v_type) {
11727c478bd9Sstevel@tonic-gate 	case VSOCK:
11737c478bd9Sstevel@tonic-gate 		so = VTOSO(vp);
11747c478bd9Sstevel@tonic-gate 		is_sock = B_TRUE;
11750f1702c5SYu Xiangning 		if (SOCK_IS_NONSTR(so)) {
11760f1702c5SYu Xiangning 			maxblk = so->so_proto_props.sopp_maxblk;
11770f1702c5SYu Xiangning 		} else {
11780f1702c5SYu Xiangning 			maxblk = (int)vp->v_stream->sd_maxblk;
11797c478bd9Sstevel@tonic-gate 		}
1180940f8eceSRobert Mustacchi 
1181940f8eceSRobert Mustacchi 		/*
1182940f8eceSRobert Mustacchi 		 * We need to make sure that the socket that we're sending on
1183940f8eceSRobert Mustacchi 		 * supports sendfile behavior. sockfs doesn't know that the APIs
1184940f8eceSRobert Mustacchi 		 * we want to use are coming from sendfile, so we can't rely on
1185940f8eceSRobert Mustacchi 		 * it to check for us.
1186940f8eceSRobert Mustacchi 		 */
1187940f8eceSRobert Mustacchi 		if ((so->so_mode & SM_SENDFILESUPP) == 0) {
1188940f8eceSRobert Mustacchi 			error = EOPNOTSUPP;
1189940f8eceSRobert Mustacchi 			goto err;
1190940f8eceSRobert Mustacchi 		}
11917c478bd9Sstevel@tonic-gate 		break;
11927c478bd9Sstevel@tonic-gate 	case VREG:
11937c478bd9Sstevel@tonic-gate 		break;
11947c478bd9Sstevel@tonic-gate 	default:
11957c478bd9Sstevel@tonic-gate 		error = EINVAL;
11967c478bd9Sstevel@tonic-gate 		goto err;
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	switch (opcode) {
12007c478bd9Sstevel@tonic-gate 	case SENDFILEV :
12017c478bd9Sstevel@tonic-gate 		break;
12027c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
12037c478bd9Sstevel@tonic-gate 	case SENDFILEV64 :
12047c478bd9Sstevel@tonic-gate 		return (sendvec64(fp, (struct ksendfilevec64 *)vec, sfvcnt,
12057c478bd9Sstevel@tonic-gate 		    (size32_t *)xferred, fildes));
12067c478bd9Sstevel@tonic-gate #endif
12077c478bd9Sstevel@tonic-gate 	default :
12087c478bd9Sstevel@tonic-gate 		error = ENOSYS;
12097c478bd9Sstevel@tonic-gate 		break;
12107c478bd9Sstevel@tonic-gate 	}
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
12137c478bd9Sstevel@tonic-gate 	copy_vec = vec;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	do {
12160a8433caSrshoaib 		total_size = 0;
12177c478bd9Sstevel@tonic-gate 		copy_cnt = MIN(sfvcnt, SEND_MAX_CHUNK);
12187c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
12197c478bd9Sstevel@tonic-gate 		/* 32-bit callers need to have their iovec expanded. */
12207c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_ILP32) {
12217c478bd9Sstevel@tonic-gate 			if (copyin(copy_vec, sfv32,
12227c478bd9Sstevel@tonic-gate 			    copy_cnt * sizeof (ksendfilevec32_t))) {
12237c478bd9Sstevel@tonic-gate 				error = EFAULT;
12247c478bd9Sstevel@tonic-gate 				break;
12257c478bd9Sstevel@tonic-gate 			}
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 			for (i = 0; i < copy_cnt; i++) {
12287c478bd9Sstevel@tonic-gate 				sfv[i].sfv_fd = sfv32[i].sfv_fd;
12297c478bd9Sstevel@tonic-gate 				sfv[i].sfv_off =
1230311e3d4cSgeorges 				    (off_t)(uint32_t)sfv32[i].sfv_off;
12317c478bd9Sstevel@tonic-gate 				sfv[i].sfv_len = (size_t)sfv32[i].sfv_len;
12327c478bd9Sstevel@tonic-gate 				total_size += sfv[i].sfv_len;
12337c478bd9Sstevel@tonic-gate 				sfv[i].sfv_flag = sfv32[i].sfv_flag;
1234311e3d4cSgeorges 				/*
1235311e3d4cSgeorges 				 * Individual elements of the vector must not
1236311e3d4cSgeorges 				 * wrap or overflow, as later math is signed.
1237311e3d4cSgeorges 				 * Equally total_size needs to be checked after
1238311e3d4cSgeorges 				 * each vector is added in, to be sure that
1239311e3d4cSgeorges 				 * rogue values haven't overflowed the counter.
1240311e3d4cSgeorges 				 */
1241311e3d4cSgeorges 				if (((ssize32_t)sfv[i].sfv_len < 0) ||
1242311e3d4cSgeorges 				    ((ssize32_t)total_size < 0)) {
1243311e3d4cSgeorges 					/*
1244311e3d4cSgeorges 					 * Truncate the vector to send data
1245311e3d4cSgeorges 					 * described by elements before the
1246311e3d4cSgeorges 					 * error.
1247311e3d4cSgeorges 					 */
1248311e3d4cSgeorges 					copy_cnt = i;
1249311e3d4cSgeorges 					first_vector_error = EINVAL;
1250311e3d4cSgeorges 					/* total_size can't be trusted */
1251311e3d4cSgeorges 					if ((ssize32_t)total_size < 0)
1252311e3d4cSgeorges 						error = EINVAL;
1253311e3d4cSgeorges 					break;
1254311e3d4cSgeorges 				}
12557c478bd9Sstevel@tonic-gate 			}
1256311e3d4cSgeorges 			/* Nothing to do, process errors */
1257311e3d4cSgeorges 			if (copy_cnt == 0)
1258311e3d4cSgeorges 				break;
1259311e3d4cSgeorges 
12607c478bd9Sstevel@tonic-gate 		} else {
12617c478bd9Sstevel@tonic-gate #endif
12627c478bd9Sstevel@tonic-gate 			if (copyin(copy_vec, sfv,
12637c478bd9Sstevel@tonic-gate 			    copy_cnt * sizeof (sendfilevec_t))) {
12647c478bd9Sstevel@tonic-gate 				error = EFAULT;
12657c478bd9Sstevel@tonic-gate 				break;
12667c478bd9Sstevel@tonic-gate 			}
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 			for (i = 0; i < copy_cnt; i++) {
12697c478bd9Sstevel@tonic-gate 				total_size += sfv[i].sfv_len;
1270311e3d4cSgeorges 				/*
1271311e3d4cSgeorges 				 * Individual elements of the vector must not
1272311e3d4cSgeorges 				 * wrap or overflow, as later math is signed.
1273311e3d4cSgeorges 				 * Equally total_size needs to be checked after
1274311e3d4cSgeorges 				 * each vector is added in, to be sure that
1275311e3d4cSgeorges 				 * rogue values haven't overflowed the counter.
1276311e3d4cSgeorges 				 */
1277311e3d4cSgeorges 				if (((ssize_t)sfv[i].sfv_len < 0) ||
1278311e3d4cSgeorges 				    (total_size < 0)) {
1279311e3d4cSgeorges 					/*
1280311e3d4cSgeorges 					 * Truncate the vector to send data
1281311e3d4cSgeorges 					 * described by elements before the
1282311e3d4cSgeorges 					 * error.
1283311e3d4cSgeorges 					 */
1284311e3d4cSgeorges 					copy_cnt = i;
1285311e3d4cSgeorges 					first_vector_error = EINVAL;
1286311e3d4cSgeorges 					/* total_size can't be trusted */
1287311e3d4cSgeorges 					if (total_size < 0)
1288311e3d4cSgeorges 						error = EINVAL;
1289311e3d4cSgeorges 					break;
1290311e3d4cSgeorges 				}
12917c478bd9Sstevel@tonic-gate 			}
1292311e3d4cSgeorges 			/* Nothing to do, process errors */
1293311e3d4cSgeorges 			if (copy_cnt == 0)
1294311e3d4cSgeorges 				break;
12957c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
12967c478bd9Sstevel@tonic-gate 		}
12977c478bd9Sstevel@tonic-gate #endif
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 		/*
13007c478bd9Sstevel@tonic-gate 		 * The task between deciding to use sendvec_small_chunk
13017c478bd9Sstevel@tonic-gate 		 * and sendvec_chunk is dependant on multiple things:
13027c478bd9Sstevel@tonic-gate 		 *
13037c478bd9Sstevel@tonic-gate 		 * i) latency is important for smaller files. So if the
13047c478bd9Sstevel@tonic-gate 		 * data is smaller than 'tcp_slow_start_initial' times
13057c478bd9Sstevel@tonic-gate 		 * maxblk, then use sendvec_small_chunk which creates
13063b3d24f3SJayakara Kini 		 * maxblk size mblks and chains them together and sends
13077c478bd9Sstevel@tonic-gate 		 * them to TCP in one shot. It also leaves 'wroff' size
13087c478bd9Sstevel@tonic-gate 		 * space for the headers in each mblk.
13097c478bd9Sstevel@tonic-gate 		 *
13107c478bd9Sstevel@tonic-gate 		 * ii) for total size bigger than 'tcp_slow_start_initial'
13117c478bd9Sstevel@tonic-gate 		 * time maxblk, its probably real file data which is
13127c478bd9Sstevel@tonic-gate 		 * dominating. So its better to use sendvec_chunk because
13137c478bd9Sstevel@tonic-gate 		 * performance goes to dog if we don't do pagesize reads.
13147c478bd9Sstevel@tonic-gate 		 * sendvec_chunk will do pagesize reads and write them
13157c478bd9Sstevel@tonic-gate 		 * in pagesize mblks to TCP.
13167c478bd9Sstevel@tonic-gate 		 *
13177c478bd9Sstevel@tonic-gate 		 * Side Notes: A write to file has not been optimized.
13187c478bd9Sstevel@tonic-gate 		 * Future zero copy code will plugin into sendvec_chunk
13197c478bd9Sstevel@tonic-gate 		 * only because doing zero copy for files smaller then
13207c478bd9Sstevel@tonic-gate 		 * pagesize is useless.
13217c478bd9Sstevel@tonic-gate 		 */
13227c478bd9Sstevel@tonic-gate 		if (is_sock) {
1323*15f90b02SGarrett D'Amore 			if ((total_size <= (4 * maxblk)) &&
13240f1702c5SYu Xiangning 			    error == 0) {
13250f1702c5SYu Xiangning 				error = sendvec_small_chunk(fp,
13260f1702c5SYu Xiangning 				    &fileoff, sfv, copy_cnt,
13270f1702c5SYu Xiangning 				    total_size, maxblk, &count);
13280f1702c5SYu Xiangning 			} else {
13290f1702c5SYu Xiangning 				error = sendvec_chunk(fp, &fileoff,
13300f1702c5SYu Xiangning 				    sfv, copy_cnt, &count);
13317c478bd9Sstevel@tonic-gate 			}
13327c478bd9Sstevel@tonic-gate 		} else {
13337c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_type == VREG);
13347c478bd9Sstevel@tonic-gate 			error = sendvec_chunk(fp, &fileoff, sfv, copy_cnt,
13357c478bd9Sstevel@tonic-gate 			    &count);
13367c478bd9Sstevel@tonic-gate 		}
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1340c99fb8b3SToomas Soome 		if (get_udatamodel() == DATAMODEL_ILP32) {
1341c99fb8b3SToomas Soome 			copy_vec = (const struct sendfilevec *)
1342c99fb8b3SToomas Soome 			    ((char *)copy_vec +
1343c99fb8b3SToomas Soome 			    (copy_cnt * sizeof (ksendfilevec32_t)));
1344c99fb8b3SToomas Soome 		} else
13457c478bd9Sstevel@tonic-gate #endif
1346c99fb8b3SToomas Soome 			copy_vec += copy_cnt;
13477c478bd9Sstevel@tonic-gate 		sfvcnt -= copy_cnt;
1348311e3d4cSgeorges 
1349311e3d4cSgeorges 	/* Process all vector members up to first error */
1350311e3d4cSgeorges 	} while ((sfvcnt > 0) && first_vector_error == 0 && error == 0);
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG)
13537c478bd9Sstevel@tonic-gate 		fp->f_offset += count;
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
13587c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32) {
13597c478bd9Sstevel@tonic-gate 		ssize32_t count32 = (ssize32_t)count;
13607c478bd9Sstevel@tonic-gate 		if (copyout(&count32, xferred, sizeof (count32)))
13617c478bd9Sstevel@tonic-gate 			error = EFAULT;
13627c478bd9Sstevel@tonic-gate 		releasef(fildes);
13637c478bd9Sstevel@tonic-gate 		if (error != 0)
13647c478bd9Sstevel@tonic-gate 			return (set_errno(error));
1365311e3d4cSgeorges 		if (first_vector_error != 0)
1366311e3d4cSgeorges 			return (set_errno(first_vector_error));
13677c478bd9Sstevel@tonic-gate 		return (count32);
13687c478bd9Sstevel@tonic-gate 	}
13697c478bd9Sstevel@tonic-gate #endif
13707c478bd9Sstevel@tonic-gate 	if (copyout(&count, xferred, sizeof (count)))
13717c478bd9Sstevel@tonic-gate 		error = EFAULT;
13727c478bd9Sstevel@tonic-gate 	releasef(fildes);
13737c478bd9Sstevel@tonic-gate 	if (error != 0)
13747c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1375311e3d4cSgeorges 	if (first_vector_error != 0)
1376311e3d4cSgeorges 		return (set_errno(first_vector_error));
13777c478bd9Sstevel@tonic-gate 	return (count);
13787c478bd9Sstevel@tonic-gate err:
13797c478bd9Sstevel@tonic-gate 	ASSERT(error != 0);
13807c478bd9Sstevel@tonic-gate 	releasef(fildes);
13817c478bd9Sstevel@tonic-gate 	return (set_errno(error));
13827c478bd9Sstevel@tonic-gate }
1383