xref: /titanic_50/usr/src/uts/common/syscall/sendfile.c (revision b3528fae2e7d55d6f5555957b7734028555131b8)
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 /*
23*b3528faeSVasumathi Sundaram - Sun Microsystems  * Copyright 2009 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>
4774024373Spr14459 #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 *);
722c9e429eSbrutus extern int nl7c_sendfilev(struct sonode *, u_offset_t *, struct sendfilevec *,
732c9e429eSbrutus 		int, ssize_t *);
743b3d24f3SJayakara Kini extern int snf_segmap(file_t *, vnode_t *, u_offset_t, u_offset_t, ssize_t *,
753b3d24f3SJayakara Kini 		boolean_t);
760f1702c5SYu Xiangning extern sotpi_info_t *sotpi_sototpi(struct sonode *);
7774024373Spr14459 
787c478bd9Sstevel@tonic-gate #define	SEND_MAX_CHUNK	16
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * 64 bit offsets for 32 bit applications only running either on
837c478bd9Sstevel@tonic-gate  * 64 bit kernel or 32 bit kernel. For 32 bit apps, we can't transfer
847c478bd9Sstevel@tonic-gate  * more than 2GB of data.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate int
877c478bd9Sstevel@tonic-gate sendvec_chunk64(file_t *fp, u_offset_t *fileoff, struct ksendfilevec64 *sfv,
887c478bd9Sstevel@tonic-gate     int copy_cnt, ssize32_t *count)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	struct vnode *vp;
917c478bd9Sstevel@tonic-gate 	ushort_t fflag;
927c478bd9Sstevel@tonic-gate 	int ioflag;
937c478bd9Sstevel@tonic-gate 	size32_t cnt;
947c478bd9Sstevel@tonic-gate 	ssize32_t sfv_len;
957c478bd9Sstevel@tonic-gate 	ssize32_t tmpcount;
967c478bd9Sstevel@tonic-gate 	u_offset_t sfv_off;
977c478bd9Sstevel@tonic-gate 	struct uio auio;
987c478bd9Sstevel@tonic-gate 	struct iovec aiov;
997c478bd9Sstevel@tonic-gate 	int i, error;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
1027c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
1037c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING))
1067c478bd9Sstevel@tonic-gate 			return (EINTR);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 		/*
1097c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
1107c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
1117c478bd9Sstevel@tonic-gate 		 */
1127c478bd9Sstevel@tonic-gate 		sfv_len = (ssize32_t)sfv->sfv_len;
1137c478bd9Sstevel@tonic-gate 
114e116a42fSPrakash Jalan 		if (sfv_len == 0) {
115e116a42fSPrakash Jalan 			sfv++;
1167c478bd9Sstevel@tonic-gate 			continue;
117e116a42fSPrakash Jalan 		}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		if (sfv_len < 0)
1207c478bd9Sstevel@tonic-gate 			return (EINVAL);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG) {
1237c478bd9Sstevel@tonic-gate 			if (*fileoff >= curproc->p_fsz_ctl) {
1247c478bd9Sstevel@tonic-gate 				mutex_enter(&curproc->p_lock);
1257c478bd9Sstevel@tonic-gate 				(void) rctl_action(
1267c478bd9Sstevel@tonic-gate 				    rctlproc_legacy[RLIMIT_FSIZE],
1277c478bd9Sstevel@tonic-gate 				    curproc->p_rctls, curproc, RCA_SAFE);
1287c478bd9Sstevel@tonic-gate 				mutex_exit(&curproc->p_lock);
1297c478bd9Sstevel@tonic-gate 				return (EFBIG);
1307c478bd9Sstevel@tonic-gate 			}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 			if (*fileoff >= OFFSET_MAX(fp))
1337c478bd9Sstevel@tonic-gate 				return (EFBIG);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 			if (*fileoff + sfv_len > OFFSET_MAX(fp))
1367c478bd9Sstevel@tonic-gate 				return (EINVAL);
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		tmpcount = *count + sfv_len;
1407c478bd9Sstevel@tonic-gate 		if (tmpcount < 0)
1417c478bd9Sstevel@tonic-gate 			return (EINVAL);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		sfv_off = sfv->sfv_off;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_DEFAULT;
1467c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
1477c478bd9Sstevel@tonic-gate 			aiov.iov_len = sfv_len;
1487c478bd9Sstevel@tonic-gate 			aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
1497c478bd9Sstevel@tonic-gate 			auio.uio_loffset = *fileoff;
1507c478bd9Sstevel@tonic-gate 			auio.uio_iovcnt = 1;
1517c478bd9Sstevel@tonic-gate 			auio.uio_resid = sfv_len;
1527c478bd9Sstevel@tonic-gate 			auio.uio_iov = &aiov;
1537c478bd9Sstevel@tonic-gate 			auio.uio_segflg = UIO_USERSPACE;
1547c478bd9Sstevel@tonic-gate 			auio.uio_llimit = curproc->p_fsz_ctl;
1557c478bd9Sstevel@tonic-gate 			auio.uio_fmode = fflag;
1567c478bd9Sstevel@tonic-gate 			ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
1577c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
1587c478bd9Sstevel@tonic-gate 				error = VOP_WRITE(vp, &auio, ioflag,
1597c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
1607c478bd9Sstevel@tonic-gate 				cnt = sfv_len - auio.uio_resid;
1617c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
1627c478bd9Sstevel@tonic-gate 				ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
1637c478bd9Sstevel@tonic-gate 				if (vp->v_type == VREG)
1647c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
1657c478bd9Sstevel@tonic-gate 				*count += cnt;
1667c478bd9Sstevel@tonic-gate 				if (error != 0)
1677c478bd9Sstevel@tonic-gate 					return (error);
1687c478bd9Sstevel@tonic-gate 			}
1697c478bd9Sstevel@tonic-gate 		} else {
1707c478bd9Sstevel@tonic-gate 			file_t	*ffp;
1717c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
1727c478bd9Sstevel@tonic-gate 			size_t	size;
1737c478bd9Sstevel@tonic-gate 			caddr_t	ptr;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL)
1767c478bd9Sstevel@tonic-gate 				return (EBADF);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
1797c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
1807c478bd9Sstevel@tonic-gate 				return (EBADF);
1817c478bd9Sstevel@tonic-gate 			}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
1847c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
1857c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
1867c478bd9Sstevel@tonic-gate 				return (EINVAL);
1877c478bd9Sstevel@tonic-gate 			}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 			/*
1907c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
1917c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
1927c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
1937c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
1947c478bd9Sstevel@tonic-gate 			 */
195bfb244baSpr14459 			if (vn_compare(vp, readvp)) {
1967c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
1977c478bd9Sstevel@tonic-gate 				return (EINVAL);
1987c478bd9Sstevel@tonic-gate 			}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 			/*
201*b3528faeSVasumathi Sundaram - Sun Microsystems 			 * Optimize the regular file over
202*b3528faeSVasumathi Sundaram - Sun Microsystems 			 * the socket case.
203*b3528faeSVasumathi Sundaram - Sun Microsystems 			 */
204*b3528faeSVasumathi Sundaram - Sun Microsystems 			if (vp->v_type == VSOCK) {
205*b3528faeSVasumathi Sundaram - Sun Microsystems 				error = sosendfile64(fp, ffp, sfv, count);
206*b3528faeSVasumathi Sundaram - Sun Microsystems 				if (error)
207*b3528faeSVasumathi Sundaram - Sun Microsystems 					return (error);
208*b3528faeSVasumathi Sundaram - Sun Microsystems 				sfv++;
209*b3528faeSVasumathi Sundaram - Sun Microsystems 				continue;
210*b3528faeSVasumathi Sundaram - Sun Microsystems 			}
211*b3528faeSVasumathi Sundaram - Sun Microsystems 
212*b3528faeSVasumathi Sundaram - Sun Microsystems 			/*
2137c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
2147c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
2157c478bd9Sstevel@tonic-gate 			 */
216*b3528faeSVasumathi Sundaram - Sun Microsystems 			if (readvp < vp) {
217*b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
218*b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
219*b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
220*b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
221*b3528faeSVasumathi Sundaram - Sun Microsystems 			} else {
222*b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
223*b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
224*b3528faeSVasumathi 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) {
230*b3528faeSVasumathi 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;
242*b3528faeSVasumathi Sundaram - Sun Microsystems 			ptr = kmem_alloc(size, KM_NOSLEEP);
243*b3528faeSVasumathi Sundaram - Sun Microsystems 			if (ptr == NULL) {
244*b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
245*b3528faeSVasumathi Sundaram - Sun Microsystems 				releasef(sfv->sfv_fd);
246*b3528faeSVasumathi Sundaram - Sun Microsystems 				return (ENOMEM);
247*b3528faeSVasumathi 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);
275*b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
276*b3528faeSVasumathi 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);
294*b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
295*b3528faeSVasumathi 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);
330*b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
331*b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
3327c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
3337c478bd9Sstevel@tonic-gate 					return (error);
3347c478bd9Sstevel@tonic-gate 				}
3357c478bd9Sstevel@tonic-gate 			}
336*b3528faeSVasumathi 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 
3457c478bd9Sstevel@tonic-gate ssize32_t
3467c478bd9Sstevel@tonic-gate sendvec64(file_t *fp, const struct ksendfilevec64 *vec, int sfvcnt,
3477c478bd9Sstevel@tonic-gate 	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;
358*b3528faeSVasumathi 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 
382*b3528faeSVasumathi 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 
3927c478bd9Sstevel@tonic-gate int
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) ?
4087c478bd9Sstevel@tonic-gate 	    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) {
4337c478bd9Sstevel@tonic-gate 		wroff = (int)vp->v_stream->sd_wroff;
434c28749e9Skais 		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 
556bfb244baSpr14459 			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 
567*b3528faeSVasumathi Sundaram - Sun Microsystems 			if (readvp < vp) {
568*b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
569*b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
570*b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
571*b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
572*b3528faeSVasumathi Sundaram - Sun Microsystems 			} else {
573*b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
574*b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
575*b3528faeSVasumathi Sundaram - Sun Microsystems 			}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 			/* Same checks as in pread */
5787c478bd9Sstevel@tonic-gate 			if (sfv_off > maxoff) {
579*b3528faeSVasumathi 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) {
597*b3528faeSVasumathi Sundaram - Sun Microsystems 						VOP_RWUNLOCK(readvp,
598*b3528faeSVasumathi 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 					 */
635*b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
636*b3528faeSVasumathi 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) {
649*b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
650*b3528faeSVasumathi 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 			}
662*b3528faeSVasumathi 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)
6727c478bd9Sstevel@tonic-gate 			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 
6827c478bd9Sstevel@tonic-gate int
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) ?
6987c478bd9Sstevel@tonic-gate 	    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;
704c28749e9Skais 	size_t  extra;
705c28749e9Skais 	int maxblk, wroff, tail_len;
706c28749e9Skais 	struct sonode *so;
707c28749e9Skais 	stdata_t *stp;
7080f1702c5SYu Xiangning 	struct nmsghdr msg;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
7117c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
7127c478bd9Sstevel@tonic-gate 
713c28749e9Skais 	if (vp->v_type == VSOCK) {
714c28749e9Skais 		so = VTOSO(vp);
7150f1702c5SYu Xiangning 		if (vp->v_stream != NULL) {
716c28749e9Skais 			stp = vp->v_stream;
717c28749e9Skais 			wroff = (int)stp->sd_wroff;
718c28749e9Skais 			tail_len = (int)stp->sd_tail;
719c28749e9Skais 			maxblk = (int)stp->sd_maxblk;
7200f1702c5SYu Xiangning 		} else {
7210f1702c5SYu Xiangning 			stp = NULL;
7220f1702c5SYu Xiangning 			wroff = so->so_proto_props.sopp_wroff;
7230f1702c5SYu Xiangning 			tail_len = so->so_proto_props.sopp_tail;
7240f1702c5SYu Xiangning 			maxblk = so->so_proto_props.sopp_maxblk;
7250f1702c5SYu Xiangning 		}
726c28749e9Skais 		extra = wroff + tail_len;
727c28749e9Skais 	}
728c28749e9Skais 
7290f1702c5SYu Xiangning 	bzero(&msg, sizeof (msg));
7307c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_DEFAULT;
7317c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
7327c478bd9Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING))
7337c478bd9Sstevel@tonic-gate 			return (EINTR);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 		/*
7367c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
7377c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
7387c478bd9Sstevel@tonic-gate 		 */
7397c478bd9Sstevel@tonic-gate 		sfv_len = (ssize_t)sfv->sfv_len;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		if (sfv_len == 0) {
7427c478bd9Sstevel@tonic-gate 			sfv++;
7437c478bd9Sstevel@tonic-gate 			continue;
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG) {
7477c478bd9Sstevel@tonic-gate 			if (*fileoff >= curproc->p_fsz_ctl) {
7487c478bd9Sstevel@tonic-gate 				mutex_enter(&curproc->p_lock);
7497c478bd9Sstevel@tonic-gate 				(void) rctl_action(
7507c478bd9Sstevel@tonic-gate 				    rctlproc_legacy[RLIMIT_FSIZE],
7517c478bd9Sstevel@tonic-gate 				    curproc->p_rctls, curproc, RCA_SAFE);
7527c478bd9Sstevel@tonic-gate 				mutex_exit(&curproc->p_lock);
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 				return (EFBIG);
7557c478bd9Sstevel@tonic-gate 			}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 			if (*fileoff >= maxoff)
7587c478bd9Sstevel@tonic-gate 				return (EFBIG);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 			if (*fileoff + sfv_len > maxoff)
7617c478bd9Sstevel@tonic-gate 				return (EINVAL);
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		/* Check for overflow */
7657c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
7667c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_ILP32) {
7677c478bd9Sstevel@tonic-gate 			if (((ssize32_t)(*count + sfv_len)) < 0)
7687c478bd9Sstevel@tonic-gate 				return (EINVAL);
7697c478bd9Sstevel@tonic-gate 		} else
7707c478bd9Sstevel@tonic-gate #endif
7717c478bd9Sstevel@tonic-gate 		if ((*count + sfv_len) < 0)
7727c478bd9Sstevel@tonic-gate 			return (EINVAL);
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 		sfv_off = (u_offset_t)(ulong_t)sfv->sfv_off;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
7777c478bd9Sstevel@tonic-gate 			if (vp->v_type == VSOCK) {
778e116a42fSPrakash Jalan 				while (sfv_len > 0) {
779e116a42fSPrakash Jalan 					size_t iov_len;
7807c478bd9Sstevel@tonic-gate 
781e116a42fSPrakash Jalan 					iov_len = sfv_len;
7820f1702c5SYu Xiangning 					if (!SOCK_IS_NONSTR(so) &&
7830f1702c5SYu Xiangning 					    SOTOTPI(so)->sti_kssl_ctx != NULL)
784e116a42fSPrakash Jalan 						iov_len = MIN(iov_len, maxblk);
7857c478bd9Sstevel@tonic-gate 
786e116a42fSPrakash Jalan 					aiov.iov_len = iov_len;
787e116a42fSPrakash Jalan 					aiov.iov_base =
788e116a42fSPrakash Jalan 					    (caddr_t)(uintptr_t)sfv_off;
789e116a42fSPrakash Jalan 
790e116a42fSPrakash Jalan 					auio.uio_iov = &aiov;
791e116a42fSPrakash Jalan 					auio.uio_iovcnt = 1;
792e116a42fSPrakash Jalan 					auio.uio_loffset = *fileoff;
793e116a42fSPrakash Jalan 					auio.uio_segflg = UIO_USERSPACE;
794e116a42fSPrakash Jalan 					auio.uio_fmode = fflag;
795e116a42fSPrakash Jalan 					auio.uio_llimit = curproc->p_fsz_ctl;
796e116a42fSPrakash Jalan 					auio.uio_resid = iov_len;
797e116a42fSPrakash Jalan 
798e116a42fSPrakash Jalan 					dmp = allocb(iov_len + extra, BPRI_HI);
7997c478bd9Sstevel@tonic-gate 					if (dmp == NULL)
8007c478bd9Sstevel@tonic-gate 						return (ENOMEM);
801e116a42fSPrakash Jalan 					dmp->b_wptr = dmp->b_rptr =
802e116a42fSPrakash Jalan 					    dmp->b_rptr + wroff;
8037c478bd9Sstevel@tonic-gate 					error = uiomove((caddr_t)dmp->b_wptr,
804e116a42fSPrakash Jalan 					    iov_len, UIO_WRITE, &auio);
8057c478bd9Sstevel@tonic-gate 					if (error != 0) {
8067c478bd9Sstevel@tonic-gate 						freeb(dmp);
8077c478bd9Sstevel@tonic-gate 						return (error);
8087c478bd9Sstevel@tonic-gate 					}
809e116a42fSPrakash Jalan 					dmp->b_wptr += iov_len;
8100f1702c5SYu Xiangning 					error = socket_sendmblk(VTOSO(vp),
8110f1702c5SYu Xiangning 					    &msg, fflag, CRED(), &dmp);
8120f1702c5SYu Xiangning 
8137c478bd9Sstevel@tonic-gate 					if (error != 0) {
8140f1702c5SYu Xiangning 						if (dmp != NULL)
8157c478bd9Sstevel@tonic-gate 							freeb(dmp);
8167c478bd9Sstevel@tonic-gate 						return (error);
8177c478bd9Sstevel@tonic-gate 					}
8187c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
819e116a42fSPrakash Jalan 					    (ulong_t)iov_len;
820e116a42fSPrakash Jalan 					*count += iov_len;
821e116a42fSPrakash Jalan 					sfv_len -= iov_len;
822e116a42fSPrakash Jalan 					sfv_off += iov_len;
823e116a42fSPrakash Jalan 				}
8247c478bd9Sstevel@tonic-gate 			} else {
825e116a42fSPrakash Jalan 				aiov.iov_len = sfv_len;
826e116a42fSPrakash Jalan 				aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
827e116a42fSPrakash Jalan 
828e116a42fSPrakash Jalan 				auio.uio_iov = &aiov;
829e116a42fSPrakash Jalan 				auio.uio_iovcnt = 1;
830e116a42fSPrakash Jalan 				auio.uio_loffset = *fileoff;
831e116a42fSPrakash Jalan 				auio.uio_segflg = UIO_USERSPACE;
832e116a42fSPrakash Jalan 				auio.uio_fmode = fflag;
833e116a42fSPrakash Jalan 				auio.uio_llimit = curproc->p_fsz_ctl;
834e116a42fSPrakash Jalan 				auio.uio_resid = sfv_len;
835e116a42fSPrakash Jalan 
8367c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
8377c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
8387c478bd9Sstevel@tonic-gate 				while (sfv_len > 0) {
8397c478bd9Sstevel@tonic-gate 					error = VOP_WRITE(vp, &auio, ioflag,
8407c478bd9Sstevel@tonic-gate 					    fp->f_cred, NULL);
8417c478bd9Sstevel@tonic-gate 					cnt = sfv_len - auio.uio_resid;
8427c478bd9Sstevel@tonic-gate 					sfv_len -= cnt;
8437c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
8447c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
8457c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
8467c478bd9Sstevel@tonic-gate 					*count += cnt;
8477c478bd9Sstevel@tonic-gate 					if (error != 0)
8487c478bd9Sstevel@tonic-gate 						return (error);
8497c478bd9Sstevel@tonic-gate 				}
8507c478bd9Sstevel@tonic-gate 			}
8517c478bd9Sstevel@tonic-gate 		} else {
8527f9ee850Sblu 			int segmapit = 0;
8537c478bd9Sstevel@tonic-gate 			file_t	*ffp;
8547c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
855bfb244baSpr14459 			struct vnode *realvp;
8567c478bd9Sstevel@tonic-gate 			size_t	size;
8577c478bd9Sstevel@tonic-gate 			caddr_t	ptr;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL)
8607c478bd9Sstevel@tonic-gate 				return (EBADF);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
8637c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
8647c478bd9Sstevel@tonic-gate 				return (EBADF);
8657c478bd9Sstevel@tonic-gate 			}
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
868da6c28aaSamw 			if (VOP_REALVP(readvp, &realvp, NULL) == 0)
869bfb244baSpr14459 				readvp = realvp;
8707c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
8717c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
8727c478bd9Sstevel@tonic-gate 				return (EINVAL);
8737c478bd9Sstevel@tonic-gate 			}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 			/*
8767c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
8777c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
8787c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
8797c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
8807c478bd9Sstevel@tonic-gate 			 */
881bfb244baSpr14459 			if (vn_compare(vp, readvp)) {
8827c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
8837c478bd9Sstevel@tonic-gate 				return (EINVAL);
8847c478bd9Sstevel@tonic-gate 			}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 			/*
8877c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
8887c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
8897c478bd9Sstevel@tonic-gate 			 */
890*b3528faeSVasumathi Sundaram - Sun Microsystems 			if (readvp < vp) {
891*b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
892*b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
893*b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
894*b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
895*b3528faeSVasumathi Sundaram - Sun Microsystems 			} else {
896*b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
897*b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
898*b3528faeSVasumathi Sundaram - Sun Microsystems 			}
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 			/* Same checks as in pread */
9017c478bd9Sstevel@tonic-gate 			if (sfv_off > maxoff) {
902*b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
9037c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
9047c478bd9Sstevel@tonic-gate 				return (EINVAL);
9057c478bd9Sstevel@tonic-gate 			}
9067c478bd9Sstevel@tonic-gate 			if (sfv_off + sfv_len > maxoff) {
9077c478bd9Sstevel@tonic-gate 				sfv_len = (ssize_t)((offset_t)maxoff -
9087c478bd9Sstevel@tonic-gate 				    sfv_off);
9097c478bd9Sstevel@tonic-gate 			}
9107c478bd9Sstevel@tonic-gate 			/* Find the native blocksize to transfer data */
9117c478bd9Sstevel@tonic-gate 			size = MIN(vp->v_vfsp->vfs_bsize,
9127c478bd9Sstevel@tonic-gate 			    readvp->v_vfsp->vfs_bsize);
9137c478bd9Sstevel@tonic-gate 			size = sfv_len < size ? sfv_len : size;
9147c478bd9Sstevel@tonic-gate 
9151574ae68Sjohansen 			if (vp->v_type != VSOCK) {
91674024373Spr14459 				segmapit = 0;
9171574ae68Sjohansen 				buf = kmem_alloc(size, KM_NOSLEEP);
9181574ae68Sjohansen 				if (buf == NULL) {
919*b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
920*b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
9211574ae68Sjohansen 					releasef(sfv->sfv_fd);
9221574ae68Sjohansen 					return (ENOMEM);
9231574ae68Sjohansen 				}
924c28749e9Skais 			} else {
9250f1702c5SYu Xiangning 				uint_t	copyflag;
9260f1702c5SYu Xiangning 
9270f1702c5SYu Xiangning 				copyflag = stp != NULL ? stp->sd_copyflag :
9280f1702c5SYu Xiangning 				    so->so_proto_props.sopp_zcopyflag;
929c28749e9Skais 				/*
930c28749e9Skais 				 * For sockets acting as an SSL proxy, we
931c28749e9Skais 				 * need to adjust the size to the maximum
932c28749e9Skais 				 * SSL record size set in the stream head.
933c28749e9Skais 				 */
9340f1702c5SYu Xiangning 				if (!SOCK_IS_NONSTR(so) &&
9350f1702c5SYu Xiangning 				    _SOTOTPI(so)->sti_kssl_ctx != NULL)
936c28749e9Skais 					size = MIN(size, maxblk);
93774024373Spr14459 
93874024373Spr14459 				if (vn_has_flocks(readvp) ||
93974024373Spr14459 				    readvp->v_flag & VNOMAP ||
9400f1702c5SYu Xiangning 				    copyflag & STZCVMUNSAFE) {
94174024373Spr14459 					segmapit = 0;
9420f1702c5SYu Xiangning 				} else if (copyflag & STZCVMSAFE) {
94374024373Spr14459 					segmapit = 1;
94474024373Spr14459 				} else {
94574024373Spr14459 					int on = 1;
9460f1702c5SYu Xiangning 					if (socket_setsockopt(VTOSO(vp),
94774024373Spr14459 					    SOL_SOCKET, SO_SND_COPYAVOID,
9480f1702c5SYu Xiangning 					    &on, sizeof (on), CRED()) == 0)
94974024373Spr14459 					segmapit = 1;
95074024373Spr14459 				}
95174024373Spr14459 			}
95274024373Spr14459 
95374024373Spr14459 			if (segmapit) {
95474024373Spr14459 				boolean_t nowait;
95574024373Spr14459 
95674024373Spr14459 				nowait = (sfv->sfv_flag & SFV_NOWAIT) != 0;
95774024373Spr14459 				error = snf_segmap(fp, readvp, sfv_off,
9583b3d24f3SJayakara Kini 				    (u_offset_t)sfv_len, (ssize_t *)&cnt,
9593b3d24f3SJayakara Kini 				    nowait);
96074024373Spr14459 				releasef(sfv->sfv_fd);
96174024373Spr14459 				*count += cnt;
96274024373Spr14459 				if (error)
96374024373Spr14459 					return (error);
96474024373Spr14459 				sfv++;
96574024373Spr14459 				continue;
9661574ae68Sjohansen 			}
9671574ae68Sjohansen 
9687c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
9697c478bd9Sstevel@tonic-gate 				size_t	iov_len;
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 				iov_len = MIN(size, sfv_len);
9727c478bd9Sstevel@tonic-gate 
9731574ae68Sjohansen 				if (vp->v_type == VSOCK) {
974c28749e9Skais 					dmp = allocb(iov_len + extra, BPRI_HI);
9757c478bd9Sstevel@tonic-gate 					if (dmp == NULL) {
976*b3528faeSVasumathi Sundaram - Sun Microsystems 						VOP_RWUNLOCK(readvp,
977*b3528faeSVasumathi Sundaram - Sun Microsystems 						    V_WRITELOCK_FALSE, NULL);
9787c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
9797c478bd9Sstevel@tonic-gate 						return (ENOMEM);
9807c478bd9Sstevel@tonic-gate 					}
981c28749e9Skais 					dmp->b_wptr = dmp->b_rptr =
982c28749e9Skais 					    dmp->b_rptr + wroff;
9837c478bd9Sstevel@tonic-gate 					ptr = (caddr_t)dmp->b_rptr;
9841574ae68Sjohansen 				} else {
9851574ae68Sjohansen 					ptr = buf;
9861574ae68Sjohansen 				}
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 				aiov.iov_base = ptr;
9897c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
9907c478bd9Sstevel@tonic-gate 				auio.uio_loffset = sfv_off;
9917c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
9927c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
9937c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
9947c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
9957c478bd9Sstevel@tonic-gate 				auio.uio_llimit = MAXOFFSET_T;
9967c478bd9Sstevel@tonic-gate 				auio.uio_fmode = ffp->f_flag;
9977c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
9987c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 				/*
10017c478bd9Sstevel@tonic-gate 				 * If read sync is not asked for,
10027c478bd9Sstevel@tonic-gate 				 * filter sync flags
10037c478bd9Sstevel@tonic-gate 				 */
10047c478bd9Sstevel@tonic-gate 				if ((ioflag & FRSYNC) == 0)
10057c478bd9Sstevel@tonic-gate 					ioflag &= ~(FSYNC|FDSYNC);
10067c478bd9Sstevel@tonic-gate 				error = VOP_READ(readvp, &auio, ioflag,
10077c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
10087c478bd9Sstevel@tonic-gate 				if (error != 0) {
10097c478bd9Sstevel@tonic-gate 					/*
10107c478bd9Sstevel@tonic-gate 					 * If we were reading a pipe (currently
10117c478bd9Sstevel@tonic-gate 					 * not implemented), we may now lose
10127c478bd9Sstevel@tonic-gate 					 * data.
10137c478bd9Sstevel@tonic-gate 					 */
10141574ae68Sjohansen 					if (vp->v_type == VSOCK)
10157c478bd9Sstevel@tonic-gate 						freeb(dmp);
10161574ae68Sjohansen 					else
10171574ae68Sjohansen 						kmem_free(buf, size);
1018*b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
1019*b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
10207c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
10217c478bd9Sstevel@tonic-gate 					return (error);
10227c478bd9Sstevel@tonic-gate 				}
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 				/*
10257c478bd9Sstevel@tonic-gate 				 * Check how much data was really read.
10267c478bd9Sstevel@tonic-gate 				 * Decrement the 'len' and increment the
10277c478bd9Sstevel@tonic-gate 				 * 'off' appropriately.
10287c478bd9Sstevel@tonic-gate 				 */
10297c478bd9Sstevel@tonic-gate 				cnt = iov_len - auio.uio_resid;
10307c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
10311574ae68Sjohansen 					if (vp->v_type == VSOCK)
10327c478bd9Sstevel@tonic-gate 						freeb(dmp);
10331574ae68Sjohansen 					else
10341574ae68Sjohansen 						kmem_free(buf, size);
1035*b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
1036*b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
10377c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
10387c478bd9Sstevel@tonic-gate 					return (EINVAL);
10397c478bd9Sstevel@tonic-gate 				}
10407c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
10417c478bd9Sstevel@tonic-gate 				sfv_off += cnt;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 				if (vp->v_type == VSOCK) {
10447c478bd9Sstevel@tonic-gate 					dmp->b_wptr = dmp->b_rptr + cnt;
10457c478bd9Sstevel@tonic-gate 
10460f1702c5SYu Xiangning 					error = socket_sendmblk(VTOSO(vp),
10470f1702c5SYu Xiangning 					    &msg, fflag, CRED(), &dmp);
10480f1702c5SYu Xiangning 
10497c478bd9Sstevel@tonic-gate 					if (error != 0) {
10500f1702c5SYu Xiangning 						if (dmp != NULL)
10517c478bd9Sstevel@tonic-gate 							freeb(dmp);
1052*b3528faeSVasumathi Sundaram - Sun Microsystems 						VOP_RWUNLOCK(readvp,
1053*b3528faeSVasumathi Sundaram - Sun Microsystems 						    V_WRITELOCK_FALSE, NULL);
10547c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
10557c478bd9Sstevel@tonic-gate 						return (error);
10567c478bd9Sstevel@tonic-gate 					}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
10597c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
10607c478bd9Sstevel@tonic-gate 					*count += cnt;
10617c478bd9Sstevel@tonic-gate 				} else {
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 					aiov.iov_base = ptr;
10647c478bd9Sstevel@tonic-gate 					aiov.iov_len = cnt;
10657c478bd9Sstevel@tonic-gate 					auio.uio_loffset = *fileoff;
10667c478bd9Sstevel@tonic-gate 					auio.uio_resid = cnt;
1067a490cd42SPrakash Jalan 					auio.uio_iov = &aiov;
1068a490cd42SPrakash Jalan 					auio.uio_iovcnt = 1;
10697c478bd9Sstevel@tonic-gate 					auio.uio_segflg = UIO_SYSSPACE;
10707c478bd9Sstevel@tonic-gate 					auio.uio_llimit = curproc->p_fsz_ctl;
10717c478bd9Sstevel@tonic-gate 					auio.uio_fmode = fflag;
10727c478bd9Sstevel@tonic-gate 					ioflag = auio.uio_fmode &
10737c478bd9Sstevel@tonic-gate 					    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
10747c478bd9Sstevel@tonic-gate 					error = VOP_WRITE(vp, &auio, ioflag,
10757c478bd9Sstevel@tonic-gate 					    fp->f_cred, NULL);
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 					/*
10787c478bd9Sstevel@tonic-gate 					 * Check how much data was written.
10797c478bd9Sstevel@tonic-gate 					 * Increment the 'len' and decrement the
10807c478bd9Sstevel@tonic-gate 					 * 'off' if all the data was not
10817c478bd9Sstevel@tonic-gate 					 * written.
10827c478bd9Sstevel@tonic-gate 					 */
10837c478bd9Sstevel@tonic-gate 					cnt -= auio.uio_resid;
10847c478bd9Sstevel@tonic-gate 					sfv_len += auio.uio_resid;
10857c478bd9Sstevel@tonic-gate 					sfv_off -= auio.uio_resid;
10867c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
10877c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
10887c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
10897c478bd9Sstevel@tonic-gate 					*count += cnt;
10907c478bd9Sstevel@tonic-gate 					if (error != 0) {
10912db6c79fSstevel 						kmem_free(buf, size);
1092*b3528faeSVasumathi Sundaram - Sun Microsystems 						VOP_RWUNLOCK(readvp,
1093*b3528faeSVasumathi Sundaram - Sun Microsystems 						    V_WRITELOCK_FALSE, NULL);
10947c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
10957c478bd9Sstevel@tonic-gate 						return (error);
10967c478bd9Sstevel@tonic-gate 					}
10977c478bd9Sstevel@tonic-gate 				}
10987c478bd9Sstevel@tonic-gate 			}
10991574ae68Sjohansen 			if (buf) {
11001574ae68Sjohansen 				kmem_free(buf, size);
11011574ae68Sjohansen 				buf = NULL;
11021574ae68Sjohansen 			}
1103*b3528faeSVasumathi Sundaram - Sun Microsystems 			VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
11047c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
11057c478bd9Sstevel@tonic-gate 		}
11067c478bd9Sstevel@tonic-gate 		sfv++;
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 	return (0);
11097c478bd9Sstevel@tonic-gate }
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate ssize_t
11127c478bd9Sstevel@tonic-gate sendfilev(int opcode, int fildes, const struct sendfilevec *vec, int sfvcnt,
11137c478bd9Sstevel@tonic-gate     size_t *xferred)
11147c478bd9Sstevel@tonic-gate {
1115311e3d4cSgeorges 	int error = 0;
1116311e3d4cSgeorges 	int first_vector_error = 0;
11177c478bd9Sstevel@tonic-gate 	file_t *fp;
11187c478bd9Sstevel@tonic-gate 	struct vnode *vp;
11197c478bd9Sstevel@tonic-gate 	struct sonode *so;
11207c478bd9Sstevel@tonic-gate 	u_offset_t fileoff;
11217c478bd9Sstevel@tonic-gate 	int copy_cnt;
11227c478bd9Sstevel@tonic-gate 	const struct sendfilevec *copy_vec;
11237c478bd9Sstevel@tonic-gate 	struct sendfilevec sfv[SEND_MAX_CHUNK];
11247c478bd9Sstevel@tonic-gate 	ssize_t count = 0;
11257c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11267c478bd9Sstevel@tonic-gate 	struct ksendfilevec32 sfv32[SEND_MAX_CHUNK];
11277c478bd9Sstevel@tonic-gate #endif
11280a8433caSrshoaib 	ssize_t total_size;
11297c478bd9Sstevel@tonic-gate 	int i;
11307c478bd9Sstevel@tonic-gate 	boolean_t is_sock = B_FALSE;
11317c478bd9Sstevel@tonic-gate 	int maxblk = 0;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	if (sfvcnt <= 0)
11347c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	if ((fp = getf(fildes)) == NULL)
11377c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	if (((fp->f_flag) & FWRITE) == 0) {
11407c478bd9Sstevel@tonic-gate 		error = EBADF;
11417c478bd9Sstevel@tonic-gate 		goto err;
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	fileoff = fp->f_offset;
11457c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	switch (vp->v_type) {
11487c478bd9Sstevel@tonic-gate 	case VSOCK:
11497c478bd9Sstevel@tonic-gate 		so = VTOSO(vp);
11507c478bd9Sstevel@tonic-gate 		is_sock = B_TRUE;
11510f1702c5SYu Xiangning 		if (SOCK_IS_NONSTR(so)) {
11520f1702c5SYu Xiangning 			maxblk = so->so_proto_props.sopp_maxblk;
11537c478bd9Sstevel@tonic-gate 		} else {
11547c478bd9Sstevel@tonic-gate 			maxblk = (int)vp->v_stream->sd_maxblk;
11557c478bd9Sstevel@tonic-gate 		}
11567c478bd9Sstevel@tonic-gate 		break;
11577c478bd9Sstevel@tonic-gate 	case VREG:
11587c478bd9Sstevel@tonic-gate 		break;
11597c478bd9Sstevel@tonic-gate 	default:
11607c478bd9Sstevel@tonic-gate 		error = EINVAL;
11617c478bd9Sstevel@tonic-gate 		goto err;
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	switch (opcode) {
11657c478bd9Sstevel@tonic-gate 	case SENDFILEV :
11667c478bd9Sstevel@tonic-gate 		break;
11677c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
11687c478bd9Sstevel@tonic-gate 	case SENDFILEV64 :
11697c478bd9Sstevel@tonic-gate 		return (sendvec64(fp, (struct ksendfilevec64 *)vec, sfvcnt,
11707c478bd9Sstevel@tonic-gate 		    (size32_t *)xferred, fildes));
11717c478bd9Sstevel@tonic-gate #endif
11727c478bd9Sstevel@tonic-gate 	default :
11737c478bd9Sstevel@tonic-gate 		error = ENOSYS;
11747c478bd9Sstevel@tonic-gate 		break;
11757c478bd9Sstevel@tonic-gate 	}
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
11787c478bd9Sstevel@tonic-gate 	copy_vec = vec;
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	do {
11810a8433caSrshoaib 		total_size = 0;
11827c478bd9Sstevel@tonic-gate 		copy_cnt = MIN(sfvcnt, SEND_MAX_CHUNK);
11837c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11847c478bd9Sstevel@tonic-gate 		/* 32-bit callers need to have their iovec expanded. */
11857c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_ILP32) {
11867c478bd9Sstevel@tonic-gate 			if (copyin(copy_vec, sfv32,
11877c478bd9Sstevel@tonic-gate 			    copy_cnt * sizeof (ksendfilevec32_t))) {
11887c478bd9Sstevel@tonic-gate 				error = EFAULT;
11897c478bd9Sstevel@tonic-gate 				break;
11907c478bd9Sstevel@tonic-gate 			}
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 			for (i = 0; i < copy_cnt; i++) {
11937c478bd9Sstevel@tonic-gate 				sfv[i].sfv_fd = sfv32[i].sfv_fd;
11947c478bd9Sstevel@tonic-gate 				sfv[i].sfv_off =
11957c478bd9Sstevel@tonic-gate 				    (off_t)(uint32_t)sfv32[i].sfv_off;
11967c478bd9Sstevel@tonic-gate 				sfv[i].sfv_len = (size_t)sfv32[i].sfv_len;
11977c478bd9Sstevel@tonic-gate 				total_size += sfv[i].sfv_len;
11987c478bd9Sstevel@tonic-gate 				sfv[i].sfv_flag = sfv32[i].sfv_flag;
1199311e3d4cSgeorges 				/*
1200311e3d4cSgeorges 				 * Individual elements of the vector must not
1201311e3d4cSgeorges 				 * wrap or overflow, as later math is signed.
1202311e3d4cSgeorges 				 * Equally total_size needs to be checked after
1203311e3d4cSgeorges 				 * each vector is added in, to be sure that
1204311e3d4cSgeorges 				 * rogue values haven't overflowed the counter.
1205311e3d4cSgeorges 				 */
1206311e3d4cSgeorges 				if (((ssize32_t)sfv[i].sfv_len < 0) ||
1207311e3d4cSgeorges 				    ((ssize32_t)total_size < 0)) {
1208311e3d4cSgeorges 					/*
1209311e3d4cSgeorges 					 * Truncate the vector to send data
1210311e3d4cSgeorges 					 * described by elements before the
1211311e3d4cSgeorges 					 * error.
1212311e3d4cSgeorges 					 */
1213311e3d4cSgeorges 					copy_cnt = i;
1214311e3d4cSgeorges 					first_vector_error = EINVAL;
1215311e3d4cSgeorges 					/* total_size can't be trusted */
1216311e3d4cSgeorges 					if ((ssize32_t)total_size < 0)
1217311e3d4cSgeorges 						error = EINVAL;
1218311e3d4cSgeorges 					break;
12197c478bd9Sstevel@tonic-gate 				}
1220311e3d4cSgeorges 			}
1221311e3d4cSgeorges 			/* Nothing to do, process errors */
1222311e3d4cSgeorges 			if (copy_cnt == 0)
1223311e3d4cSgeorges 				break;
1224311e3d4cSgeorges 
12257c478bd9Sstevel@tonic-gate 		} else {
12267c478bd9Sstevel@tonic-gate #endif
12277c478bd9Sstevel@tonic-gate 			if (copyin(copy_vec, sfv,
12287c478bd9Sstevel@tonic-gate 			    copy_cnt * sizeof (sendfilevec_t))) {
12297c478bd9Sstevel@tonic-gate 				error = EFAULT;
12307c478bd9Sstevel@tonic-gate 				break;
12317c478bd9Sstevel@tonic-gate 			}
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 			for (i = 0; i < copy_cnt; i++) {
12347c478bd9Sstevel@tonic-gate 				total_size += sfv[i].sfv_len;
1235311e3d4cSgeorges 				/*
1236311e3d4cSgeorges 				 * Individual elements of the vector must not
1237311e3d4cSgeorges 				 * wrap or overflow, as later math is signed.
1238311e3d4cSgeorges 				 * Equally total_size needs to be checked after
1239311e3d4cSgeorges 				 * each vector is added in, to be sure that
1240311e3d4cSgeorges 				 * rogue values haven't overflowed the counter.
1241311e3d4cSgeorges 				 */
1242311e3d4cSgeorges 				if (((ssize_t)sfv[i].sfv_len < 0) ||
1243311e3d4cSgeorges 				    (total_size < 0)) {
1244311e3d4cSgeorges 					/*
1245311e3d4cSgeorges 					 * Truncate the vector to send data
1246311e3d4cSgeorges 					 * described by elements before the
1247311e3d4cSgeorges 					 * error.
1248311e3d4cSgeorges 					 */
1249311e3d4cSgeorges 					copy_cnt = i;
1250311e3d4cSgeorges 					first_vector_error = EINVAL;
1251311e3d4cSgeorges 					/* total_size can't be trusted */
1252311e3d4cSgeorges 					if (total_size < 0)
1253311e3d4cSgeorges 						error = EINVAL;
1254311e3d4cSgeorges 					break;
12557c478bd9Sstevel@tonic-gate 				}
1256311e3d4cSgeorges 			}
1257311e3d4cSgeorges 			/* Nothing to do, process errors */
1258311e3d4cSgeorges 			if (copy_cnt == 0)
1259311e3d4cSgeorges 				break;
12607c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
12617c478bd9Sstevel@tonic-gate 		}
12627c478bd9Sstevel@tonic-gate #endif
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 		/*
12657c478bd9Sstevel@tonic-gate 		 * The task between deciding to use sendvec_small_chunk
12667c478bd9Sstevel@tonic-gate 		 * and sendvec_chunk is dependant on multiple things:
12677c478bd9Sstevel@tonic-gate 		 *
12687c478bd9Sstevel@tonic-gate 		 * i) latency is important for smaller files. So if the
12697c478bd9Sstevel@tonic-gate 		 * data is smaller than 'tcp_slow_start_initial' times
12707c478bd9Sstevel@tonic-gate 		 * maxblk, then use sendvec_small_chunk which creates
12713b3d24f3SJayakara Kini 		 * maxblk size mblks and chains them together and sends
12727c478bd9Sstevel@tonic-gate 		 * them to TCP in one shot. It also leaves 'wroff' size
12737c478bd9Sstevel@tonic-gate 		 * space for the headers in each mblk.
12747c478bd9Sstevel@tonic-gate 		 *
12757c478bd9Sstevel@tonic-gate 		 * ii) for total size bigger than 'tcp_slow_start_initial'
12767c478bd9Sstevel@tonic-gate 		 * time maxblk, its probably real file data which is
12777c478bd9Sstevel@tonic-gate 		 * dominating. So its better to use sendvec_chunk because
12787c478bd9Sstevel@tonic-gate 		 * performance goes to dog if we don't do pagesize reads.
12797c478bd9Sstevel@tonic-gate 		 * sendvec_chunk will do pagesize reads and write them
12807c478bd9Sstevel@tonic-gate 		 * in pagesize mblks to TCP.
12817c478bd9Sstevel@tonic-gate 		 *
12827c478bd9Sstevel@tonic-gate 		 * Side Notes: A write to file has not been optimized.
12837c478bd9Sstevel@tonic-gate 		 * Future zero copy code will plugin into sendvec_chunk
12847c478bd9Sstevel@tonic-gate 		 * only because doing zero copy for files smaller then
12857c478bd9Sstevel@tonic-gate 		 * pagesize is useless.
12867c478bd9Sstevel@tonic-gate 		 *
12877c478bd9Sstevel@tonic-gate 		 * Note, if socket has NL7C enabled then call NL7C's
12882c9e429eSbrutus 		 * senfilev() function to consume the sfv[].
12897c478bd9Sstevel@tonic-gate 		 */
12907c478bd9Sstevel@tonic-gate 		if (is_sock) {
12910f1702c5SYu Xiangning 			if (!SOCK_IS_NONSTR(so) &&
12920f1702c5SYu Xiangning 			    _SOTOTPI(so)->sti_nl7c_flags != 0) {
12932c9e429eSbrutus 				error = nl7c_sendfilev(so, &fileoff,
12942c9e429eSbrutus 				    sfv, copy_cnt, &count);
12950f1702c5SYu Xiangning 			} else if ((total_size <= (4 * maxblk)) &&
12960f1702c5SYu Xiangning 			    error == 0) {
12977c478bd9Sstevel@tonic-gate 				error = sendvec_small_chunk(fp,
12987c478bd9Sstevel@tonic-gate 				    &fileoff, sfv, copy_cnt,
12997c478bd9Sstevel@tonic-gate 				    total_size, maxblk, &count);
13000f1702c5SYu Xiangning 			} else {
13017c478bd9Sstevel@tonic-gate 				error = sendvec_chunk(fp, &fileoff,
13027c478bd9Sstevel@tonic-gate 				    sfv, copy_cnt, &count);
13037c478bd9Sstevel@tonic-gate 			}
13047c478bd9Sstevel@tonic-gate 		} else {
13057c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_type == VREG);
13067c478bd9Sstevel@tonic-gate 			error = sendvec_chunk(fp, &fileoff, sfv, copy_cnt,
13077c478bd9Sstevel@tonic-gate 			    &count);
13087c478bd9Sstevel@tonic-gate 		}
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
13127c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32)
13137c478bd9Sstevel@tonic-gate 		copy_vec = (const struct sendfilevec *)((char *)copy_vec +
13147c478bd9Sstevel@tonic-gate 		    (copy_cnt * sizeof (ksendfilevec32_t)));
13157c478bd9Sstevel@tonic-gate 	else
13167c478bd9Sstevel@tonic-gate #endif
13177c478bd9Sstevel@tonic-gate 		copy_vec += copy_cnt;
13187c478bd9Sstevel@tonic-gate 		sfvcnt -= copy_cnt;
1319311e3d4cSgeorges 
1320311e3d4cSgeorges 	/* Process all vector members up to first error */
1321311e3d4cSgeorges 	} while ((sfvcnt > 0) && first_vector_error == 0 && error == 0);
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG)
13247c478bd9Sstevel@tonic-gate 		fp->f_offset += count;
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
13297c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32) {
13307c478bd9Sstevel@tonic-gate 		ssize32_t count32 = (ssize32_t)count;
13317c478bd9Sstevel@tonic-gate 		if (copyout(&count32, xferred, sizeof (count32)))
13327c478bd9Sstevel@tonic-gate 			error = EFAULT;
13337c478bd9Sstevel@tonic-gate 		releasef(fildes);
13347c478bd9Sstevel@tonic-gate 		if (error != 0)
13357c478bd9Sstevel@tonic-gate 			return (set_errno(error));
1336311e3d4cSgeorges 		if (first_vector_error != 0)
1337311e3d4cSgeorges 			return (set_errno(first_vector_error));
13387c478bd9Sstevel@tonic-gate 		return (count32);
13397c478bd9Sstevel@tonic-gate 	}
13407c478bd9Sstevel@tonic-gate #endif
13417c478bd9Sstevel@tonic-gate 	if (copyout(&count, xferred, sizeof (count)))
13427c478bd9Sstevel@tonic-gate 		error = EFAULT;
13437c478bd9Sstevel@tonic-gate 	releasef(fildes);
13447c478bd9Sstevel@tonic-gate 	if (error != 0)
13457c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1346311e3d4cSgeorges 	if (first_vector_error != 0)
1347311e3d4cSgeorges 		return (set_errno(first_vector_error));
13487c478bd9Sstevel@tonic-gate 	return (count);
13497c478bd9Sstevel@tonic-gate err:
13507c478bd9Sstevel@tonic-gate 	ASSERT(error != 0);
13517c478bd9Sstevel@tonic-gate 	releasef(fildes);
13527c478bd9Sstevel@tonic-gate 	return (set_errno(error));
13537c478bd9Sstevel@tonic-gate }
1354