xref: /titanic_41/usr/src/uts/common/syscall/sendfile.c (revision 4ef54b56bdf362198a4e308f45d5325af35fdc00)
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.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/systm.h>
307c478bd9Sstevel@tonic-gate #include <sys/buf.h>
317c478bd9Sstevel@tonic-gate #include <sys/conf.h>
327c478bd9Sstevel@tonic-gate #include <sys/cred.h>
337c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
367c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
377c478bd9Sstevel@tonic-gate #include <sys/debug.h>
387c478bd9Sstevel@tonic-gate #include <sys/errno.h>
397c478bd9Sstevel@tonic-gate #include <sys/time.h>
407c478bd9Sstevel@tonic-gate #include <sys/file.h>
417c478bd9Sstevel@tonic-gate #include <sys/open.h>
427c478bd9Sstevel@tonic-gate #include <sys/user.h>
437c478bd9Sstevel@tonic-gate #include <sys/termios.h>
447c478bd9Sstevel@tonic-gate #include <sys/stream.h>
457c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
4674024373Spr14459 #include <sys/sunddi.h>
477c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
487c478bd9Sstevel@tonic-gate #include <sys/flock.h>
497c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
507c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
517c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <sys/socket.h>
547c478bd9Sstevel@tonic-gate #include <sys/socketvar.h>
550f1702c5SYu Xiangning #include <fs/sockfs/sockcommon.h>
560f1702c5SYu Xiangning #include <fs/sockfs/socktpi.h>
5745916cd2Sjpk 
587c478bd9Sstevel@tonic-gate #include <netinet/in.h>
597c478bd9Sstevel@tonic-gate #include <sys/sendfile.h>
607c478bd9Sstevel@tonic-gate #include <sys/un.h>
617c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
627c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <inet/common.h>
657c478bd9Sstevel@tonic-gate #include <inet/ip.h>
667c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
677c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate extern int sosendfile64(file_t *, file_t *, const struct ksendfilevec64 *,
707c478bd9Sstevel@tonic-gate 		ssize32_t *);
712c9e429eSbrutus extern int nl7c_sendfilev(struct sonode *, u_offset_t *, struct sendfilevec *,
722c9e429eSbrutus 		int, ssize_t *);
733b3d24f3SJayakara Kini extern int snf_segmap(file_t *, vnode_t *, u_offset_t, u_offset_t, ssize_t *,
743b3d24f3SJayakara Kini 		boolean_t);
750f1702c5SYu Xiangning extern sotpi_info_t *sotpi_sototpi(struct sonode *);
7674024373Spr14459 
777c478bd9Sstevel@tonic-gate #define	SEND_MAX_CHUNK	16
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * 64 bit offsets for 32 bit applications only running either on
827c478bd9Sstevel@tonic-gate  * 64 bit kernel or 32 bit kernel. For 32 bit apps, we can't transfer
837c478bd9Sstevel@tonic-gate  * more than 2GB of data.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate int
sendvec_chunk64(file_t * fp,u_offset_t * fileoff,struct ksendfilevec64 * sfv,int copy_cnt,ssize32_t * count)867c478bd9Sstevel@tonic-gate sendvec_chunk64(file_t *fp, u_offset_t *fileoff, struct ksendfilevec64 *sfv,
877c478bd9Sstevel@tonic-gate     int copy_cnt, ssize32_t *count)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	struct vnode *vp;
907c478bd9Sstevel@tonic-gate 	ushort_t fflag;
917c478bd9Sstevel@tonic-gate 	int ioflag;
927c478bd9Sstevel@tonic-gate 	size32_t cnt;
937c478bd9Sstevel@tonic-gate 	ssize32_t sfv_len;
947c478bd9Sstevel@tonic-gate 	ssize32_t tmpcount;
957c478bd9Sstevel@tonic-gate 	u_offset_t sfv_off;
967c478bd9Sstevel@tonic-gate 	struct uio auio;
977c478bd9Sstevel@tonic-gate 	struct iovec aiov;
987c478bd9Sstevel@tonic-gate 	int i, error;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
1017c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
1027c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING))
1057c478bd9Sstevel@tonic-gate 			return (EINTR);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		/*
1087c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
1097c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
1107c478bd9Sstevel@tonic-gate 		 */
1117c478bd9Sstevel@tonic-gate 		sfv_len = (ssize32_t)sfv->sfv_len;
1127c478bd9Sstevel@tonic-gate 
113e116a42fSPrakash Jalan 		if (sfv_len == 0) {
114e116a42fSPrakash Jalan 			sfv++;
1157c478bd9Sstevel@tonic-gate 			continue;
116e116a42fSPrakash Jalan 		}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		if (sfv_len < 0)
1197c478bd9Sstevel@tonic-gate 			return (EINVAL);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG) {
1227c478bd9Sstevel@tonic-gate 			if (*fileoff >= curproc->p_fsz_ctl) {
1237c478bd9Sstevel@tonic-gate 				mutex_enter(&curproc->p_lock);
1247c478bd9Sstevel@tonic-gate 				(void) rctl_action(
1257c478bd9Sstevel@tonic-gate 				    rctlproc_legacy[RLIMIT_FSIZE],
1267c478bd9Sstevel@tonic-gate 				    curproc->p_rctls, curproc, RCA_SAFE);
1277c478bd9Sstevel@tonic-gate 				mutex_exit(&curproc->p_lock);
1287c478bd9Sstevel@tonic-gate 				return (EFBIG);
1297c478bd9Sstevel@tonic-gate 			}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 			if (*fileoff >= OFFSET_MAX(fp))
1327c478bd9Sstevel@tonic-gate 				return (EFBIG);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 			if (*fileoff + sfv_len > OFFSET_MAX(fp))
1357c478bd9Sstevel@tonic-gate 				return (EINVAL);
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		tmpcount = *count + sfv_len;
1397c478bd9Sstevel@tonic-gate 		if (tmpcount < 0)
1407c478bd9Sstevel@tonic-gate 			return (EINVAL);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		sfv_off = sfv->sfv_off;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_DEFAULT;
1457c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
1467c478bd9Sstevel@tonic-gate 			aiov.iov_len = sfv_len;
1477c478bd9Sstevel@tonic-gate 			aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
1487c478bd9Sstevel@tonic-gate 			auio.uio_loffset = *fileoff;
1497c478bd9Sstevel@tonic-gate 			auio.uio_iovcnt = 1;
1507c478bd9Sstevel@tonic-gate 			auio.uio_resid = sfv_len;
1517c478bd9Sstevel@tonic-gate 			auio.uio_iov = &aiov;
1527c478bd9Sstevel@tonic-gate 			auio.uio_segflg = UIO_USERSPACE;
1537c478bd9Sstevel@tonic-gate 			auio.uio_llimit = curproc->p_fsz_ctl;
1547c478bd9Sstevel@tonic-gate 			auio.uio_fmode = fflag;
1557c478bd9Sstevel@tonic-gate 			ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
1567c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
1577c478bd9Sstevel@tonic-gate 				error = VOP_WRITE(vp, &auio, ioflag,
1587c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
1597c478bd9Sstevel@tonic-gate 				cnt = sfv_len - auio.uio_resid;
1607c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
1617c478bd9Sstevel@tonic-gate 				ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
1627c478bd9Sstevel@tonic-gate 				if (vp->v_type == VREG)
1637c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
1647c478bd9Sstevel@tonic-gate 				*count += cnt;
1657c478bd9Sstevel@tonic-gate 				if (error != 0)
1667c478bd9Sstevel@tonic-gate 					return (error);
1677c478bd9Sstevel@tonic-gate 			}
1687c478bd9Sstevel@tonic-gate 		} else {
1697c478bd9Sstevel@tonic-gate 			file_t	*ffp;
1707c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
1717c478bd9Sstevel@tonic-gate 			size_t	size;
1727c478bd9Sstevel@tonic-gate 			caddr_t	ptr;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL)
1757c478bd9Sstevel@tonic-gate 				return (EBADF);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
1787c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
1797c478bd9Sstevel@tonic-gate 				return (EBADF);
1807c478bd9Sstevel@tonic-gate 			}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
1837c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
1847c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
1857c478bd9Sstevel@tonic-gate 				return (EINVAL);
1867c478bd9Sstevel@tonic-gate 			}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 			/*
1897c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
1907c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
1917c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
1927c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
1937c478bd9Sstevel@tonic-gate 			 */
194bfb244baSpr14459 			if (vn_compare(vp, readvp)) {
1957c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
1967c478bd9Sstevel@tonic-gate 				return (EINVAL);
1977c478bd9Sstevel@tonic-gate 			}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 			/*
200b3528faeSVasumathi Sundaram - Sun Microsystems 			 * Optimize the regular file over
201b3528faeSVasumathi Sundaram - Sun Microsystems 			 * the socket case.
202b3528faeSVasumathi Sundaram - Sun Microsystems 			 */
203b3528faeSVasumathi Sundaram - Sun Microsystems 			if (vp->v_type == VSOCK) {
2047baf8047SVasumathi Sundaram - Sun Microsystems 				error = sosendfile64(fp, ffp, sfv,
2057baf8047SVasumathi Sundaram - Sun Microsystems 				    (ssize32_t *)&cnt);
2067baf8047SVasumathi Sundaram - Sun Microsystems 				*count += cnt;
207b3528faeSVasumathi Sundaram - Sun Microsystems 				if (error)
208b3528faeSVasumathi Sundaram - Sun Microsystems 					return (error);
209b3528faeSVasumathi Sundaram - Sun Microsystems 				sfv++;
210b3528faeSVasumathi Sundaram - Sun Microsystems 				continue;
211b3528faeSVasumathi Sundaram - Sun Microsystems 			}
212b3528faeSVasumathi Sundaram - Sun Microsystems 
213b3528faeSVasumathi Sundaram - Sun Microsystems 			/*
2147c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
2157c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
2167c478bd9Sstevel@tonic-gate 			 */
217b3528faeSVasumathi Sundaram - Sun Microsystems 			if (readvp < vp) {
218b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
219b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
220b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
221b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
222b3528faeSVasumathi Sundaram - Sun Microsystems 			} else {
223b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
224b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
225b3528faeSVasumathi Sundaram - Sun Microsystems 			}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 			/*
2287c478bd9Sstevel@tonic-gate 			 * Same checks as in pread64.
2297c478bd9Sstevel@tonic-gate 			 */
2307c478bd9Sstevel@tonic-gate 			if (sfv_off > MAXOFFSET_T) {
231b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
2327c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
2337c478bd9Sstevel@tonic-gate 				return (EINVAL);
2347c478bd9Sstevel@tonic-gate 			}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 			if (sfv_off + sfv_len > MAXOFFSET_T)
2377c478bd9Sstevel@tonic-gate 				sfv_len = (ssize32_t)(MAXOFFSET_T - sfv_off);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 			/* Find the native blocksize to transfer data */
2407c478bd9Sstevel@tonic-gate 			size = MIN(vp->v_vfsp->vfs_bsize,
2417c478bd9Sstevel@tonic-gate 			    readvp->v_vfsp->vfs_bsize);
2427c478bd9Sstevel@tonic-gate 			size = sfv_len < size ? sfv_len : size;
243b3528faeSVasumathi Sundaram - Sun Microsystems 			ptr = kmem_alloc(size, KM_NOSLEEP);
244b3528faeSVasumathi Sundaram - Sun Microsystems 			if (ptr == NULL) {
245b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
246b3528faeSVasumathi Sundaram - Sun Microsystems 				releasef(sfv->sfv_fd);
247b3528faeSVasumathi Sundaram - Sun Microsystems 				return (ENOMEM);
248b3528faeSVasumathi Sundaram - Sun Microsystems 			}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
2517c478bd9Sstevel@tonic-gate 				size_t	iov_len;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 				iov_len = MIN(size, sfv_len);
2547c478bd9Sstevel@tonic-gate 				aiov.iov_base = ptr;
2557c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
2567c478bd9Sstevel@tonic-gate 				auio.uio_loffset = sfv_off;
2577c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
2587c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
2597c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
2607c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
2617c478bd9Sstevel@tonic-gate 				auio.uio_llimit = MAXOFFSET_T;
2627c478bd9Sstevel@tonic-gate 				auio.uio_fmode = ffp->f_flag;
2637c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
2647c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 				/*
2677c478bd9Sstevel@tonic-gate 				 * If read sync is not asked for,
2687c478bd9Sstevel@tonic-gate 				 * filter sync flags
2697c478bd9Sstevel@tonic-gate 				 */
2707c478bd9Sstevel@tonic-gate 				if ((ioflag & FRSYNC) == 0)
2717c478bd9Sstevel@tonic-gate 					ioflag &= ~(FSYNC|FDSYNC);
2727c478bd9Sstevel@tonic-gate 				error = VOP_READ(readvp, &auio, ioflag,
2737c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
2747c478bd9Sstevel@tonic-gate 				if (error) {
2757c478bd9Sstevel@tonic-gate 					kmem_free(ptr, size);
276b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
277b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
2787c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
2797c478bd9Sstevel@tonic-gate 					return (error);
2807c478bd9Sstevel@tonic-gate 				}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 				/*
2837c478bd9Sstevel@tonic-gate 				 * Check how must data was really read.
2847c478bd9Sstevel@tonic-gate 				 * Decrement the 'len' and increment the
2857c478bd9Sstevel@tonic-gate 				 * 'off' appropriately.
2867c478bd9Sstevel@tonic-gate 				 */
2877c478bd9Sstevel@tonic-gate 				cnt = iov_len - auio.uio_resid;
2887c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
2897c478bd9Sstevel@tonic-gate 					/*
2907c478bd9Sstevel@tonic-gate 					 * If we were reading a pipe (currently
2917c478bd9Sstevel@tonic-gate 					 * not implemented), we may now lose
2927c478bd9Sstevel@tonic-gate 					 * data.
2937c478bd9Sstevel@tonic-gate 					 */
2947c478bd9Sstevel@tonic-gate 					kmem_free(ptr, size);
295b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
296b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
2977c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
2987c478bd9Sstevel@tonic-gate 					return (EINVAL);
2997c478bd9Sstevel@tonic-gate 				}
3007c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
3017c478bd9Sstevel@tonic-gate 				sfv_off += cnt;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 				aiov.iov_base = ptr;
3047c478bd9Sstevel@tonic-gate 				aiov.iov_len = cnt;
3057c478bd9Sstevel@tonic-gate 				auio.uio_loffset = *fileoff;
306a490cd42SPrakash Jalan 				auio.uio_iov = &aiov;
307a490cd42SPrakash Jalan 				auio.uio_iovcnt = 1;
3087c478bd9Sstevel@tonic-gate 				auio.uio_resid = cnt;
3097c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
3107c478bd9Sstevel@tonic-gate 				auio.uio_llimit = curproc->p_fsz_ctl;
3117c478bd9Sstevel@tonic-gate 				auio.uio_fmode = fflag;
3127c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
3137c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
3147c478bd9Sstevel@tonic-gate 				error = VOP_WRITE(vp, &auio, ioflag,
3157c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 				/*
3187c478bd9Sstevel@tonic-gate 				 * Check how much data was written. Increment
3197c478bd9Sstevel@tonic-gate 				 * the 'len' and decrement the 'off' if all
3207c478bd9Sstevel@tonic-gate 				 * the data was not written.
3217c478bd9Sstevel@tonic-gate 				 */
3227c478bd9Sstevel@tonic-gate 				cnt -= auio.uio_resid;
3237c478bd9Sstevel@tonic-gate 				sfv_len += auio.uio_resid;
3247c478bd9Sstevel@tonic-gate 				sfv_off -= auio.uio_resid;
3257c478bd9Sstevel@tonic-gate 				ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
3267c478bd9Sstevel@tonic-gate 				if (vp->v_type == VREG)
3277c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
3287c478bd9Sstevel@tonic-gate 				*count += cnt;
3297c478bd9Sstevel@tonic-gate 				if (error != 0) {
3307c478bd9Sstevel@tonic-gate 					kmem_free(ptr, size);
331b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
332b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
3337c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
3347c478bd9Sstevel@tonic-gate 					return (error);
3357c478bd9Sstevel@tonic-gate 				}
3367c478bd9Sstevel@tonic-gate 			}
337b3528faeSVasumathi Sundaram - Sun Microsystems 			VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
3387c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
3397c478bd9Sstevel@tonic-gate 			kmem_free(ptr, size);
3407c478bd9Sstevel@tonic-gate 		}
3417c478bd9Sstevel@tonic-gate 		sfv++;
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	return (0);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate ssize32_t
sendvec64(file_t * fp,const struct ksendfilevec64 * vec,int sfvcnt,size32_t * xferred,int fildes)3477c478bd9Sstevel@tonic-gate sendvec64(file_t *fp, const struct ksendfilevec64 *vec, int sfvcnt,
3487c478bd9Sstevel@tonic-gate 	size32_t *xferred, int fildes)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	u_offset_t		fileoff;
3517c478bd9Sstevel@tonic-gate 	int			copy_cnt;
3527c478bd9Sstevel@tonic-gate 	const struct ksendfilevec64 *copy_vec;
3537c478bd9Sstevel@tonic-gate 	struct ksendfilevec64 sfv[SEND_MAX_CHUNK];
3547c478bd9Sstevel@tonic-gate 	struct vnode *vp;
3557c478bd9Sstevel@tonic-gate 	int error;
3567c478bd9Sstevel@tonic-gate 	ssize32_t count = 0;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
359b3528faeSVasumathi Sundaram - Sun Microsystems 	(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	copy_vec = vec;
3627c478bd9Sstevel@tonic-gate 	fileoff = fp->f_offset;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	do {
3657c478bd9Sstevel@tonic-gate 		copy_cnt = MIN(sfvcnt, SEND_MAX_CHUNK);
3667c478bd9Sstevel@tonic-gate 		if (copyin(copy_vec, sfv, copy_cnt *
3677c478bd9Sstevel@tonic-gate 		    sizeof (struct ksendfilevec64))) {
3687c478bd9Sstevel@tonic-gate 			error = EFAULT;
3697c478bd9Sstevel@tonic-gate 			break;
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		error = sendvec_chunk64(fp, &fileoff, sfv, copy_cnt, &count);
3737c478bd9Sstevel@tonic-gate 		if (error != 0)
3747c478bd9Sstevel@tonic-gate 			break;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 		copy_vec += copy_cnt;
3777c478bd9Sstevel@tonic-gate 		sfvcnt -= copy_cnt;
3787c478bd9Sstevel@tonic-gate 	} while (sfvcnt > 0);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG)
3817c478bd9Sstevel@tonic-gate 		fp->f_offset += count;
3827c478bd9Sstevel@tonic-gate 
383b3528faeSVasumathi Sundaram - Sun Microsystems 	VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
3847c478bd9Sstevel@tonic-gate 	if (copyout(&count, xferred, sizeof (count)))
3857c478bd9Sstevel@tonic-gate 		error = EFAULT;
3867c478bd9Sstevel@tonic-gate 	releasef(fildes);
3877c478bd9Sstevel@tonic-gate 	if (error != 0)
3887c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3897c478bd9Sstevel@tonic-gate 	return (count);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate #endif
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 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)3947c478bd9Sstevel@tonic-gate sendvec_small_chunk(file_t *fp, u_offset_t *fileoff, struct sendfilevec *sfv,
3957c478bd9Sstevel@tonic-gate     int copy_cnt, ssize_t total_size, int maxblk, ssize_t *count)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	struct vnode *vp;
3987c478bd9Sstevel@tonic-gate 	struct uio auio;
3997c478bd9Sstevel@tonic-gate 	struct iovec aiov;
4007c478bd9Sstevel@tonic-gate 	ushort_t fflag;
4017c478bd9Sstevel@tonic-gate 	int ioflag;
4027c478bd9Sstevel@tonic-gate 	int i, error;
4037c478bd9Sstevel@tonic-gate 	size_t cnt;
4047c478bd9Sstevel@tonic-gate 	ssize_t sfv_len;
4057c478bd9Sstevel@tonic-gate 	u_offset_t sfv_off;
4067c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4077c478bd9Sstevel@tonic-gate 	model_t model = get_udatamodel();
4087c478bd9Sstevel@tonic-gate 	u_offset_t maxoff = (model == DATAMODEL_ILP32) ?
4097c478bd9Sstevel@tonic-gate 	    MAXOFF32_T : MAXOFFSET_T;
4107c478bd9Sstevel@tonic-gate #else
4117c478bd9Sstevel@tonic-gate 	const u_offset_t maxoff = MAXOFF32_T;
4127c478bd9Sstevel@tonic-gate #endif
4137c478bd9Sstevel@tonic-gate 	mblk_t *dmp = NULL;
4147c478bd9Sstevel@tonic-gate 	int wroff;
4157c478bd9Sstevel@tonic-gate 	int buf_left = 0;
4167c478bd9Sstevel@tonic-gate 	size_t	iov_len;
4177c478bd9Sstevel@tonic-gate 	mblk_t  *head, *tmp;
4187c478bd9Sstevel@tonic-gate 	size_t  size = total_size;
419c28749e9Skais 	size_t  extra;
420c28749e9Skais 	int tail_len;
4210f1702c5SYu Xiangning 	struct nmsghdr msg;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
4247c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VSOCK);
4277c478bd9Sstevel@tonic-gate 	ASSERT(maxblk > 0);
4287c478bd9Sstevel@tonic-gate 
429e116a42fSPrakash Jalan 	/* If nothing to send, return */
430e116a42fSPrakash Jalan 	if (total_size == 0)
431e116a42fSPrakash Jalan 		return (0);
432e116a42fSPrakash Jalan 
4330f1702c5SYu Xiangning 	if (vp->v_stream != NULL) {
4347c478bd9Sstevel@tonic-gate 		wroff = (int)vp->v_stream->sd_wroff;
435c28749e9Skais 		tail_len = (int)vp->v_stream->sd_tail;
4360f1702c5SYu Xiangning 	} else {
4370f1702c5SYu Xiangning 		struct sonode *so;
4380f1702c5SYu Xiangning 
4390f1702c5SYu Xiangning 		so = VTOSO(vp);
4400f1702c5SYu Xiangning 		wroff = so->so_proto_props.sopp_wroff;
4410f1702c5SYu Xiangning 		tail_len = so->so_proto_props.sopp_tail;
4420f1702c5SYu Xiangning 	}
4430f1702c5SYu Xiangning 
444c28749e9Skais 	extra = wroff + tail_len;
445c28749e9Skais 
4467c478bd9Sstevel@tonic-gate 	buf_left = MIN(total_size, maxblk);
447c28749e9Skais 	head = dmp = allocb(buf_left + extra, BPRI_HI);
4487c478bd9Sstevel@tonic-gate 	if (head == NULL)
4497c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4507c478bd9Sstevel@tonic-gate 	head->b_wptr = head->b_rptr = head->b_rptr + wroff;
4510f1702c5SYu Xiangning 	bzero(&msg, sizeof (msg));
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_DEFAULT;
4547c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
455311e3d4cSgeorges 		if (ISSIG(curthread, JUSTLOOKING)) {
456311e3d4cSgeorges 			freemsg(head);
4577c478bd9Sstevel@tonic-gate 			return (EINTR);
458311e3d4cSgeorges 		}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		/*
4617c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
4627c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
4637c478bd9Sstevel@tonic-gate 		 */
4647c478bd9Sstevel@tonic-gate 		sfv_len = (ssize_t)sfv->sfv_len;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		if (sfv_len == 0) {
4677c478bd9Sstevel@tonic-gate 			sfv++;
4687c478bd9Sstevel@tonic-gate 			continue;
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		/* Check for overflow */
4727c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4737c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_ILP32) {
474311e3d4cSgeorges 			if (((ssize32_t)(*count + sfv_len)) < 0) {
475311e3d4cSgeorges 				freemsg(head);
4767c478bd9Sstevel@tonic-gate 				return (EINVAL);
477311e3d4cSgeorges 			}
4787c478bd9Sstevel@tonic-gate 		} else
4797c478bd9Sstevel@tonic-gate #endif
480311e3d4cSgeorges 		if ((*count + sfv_len) < 0) {
481311e3d4cSgeorges 			freemsg(head);
4827c478bd9Sstevel@tonic-gate 			return (EINVAL);
483311e3d4cSgeorges 		}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		sfv_off = (u_offset_t)(ulong_t)sfv->sfv_off;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
4887c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
4897c478bd9Sstevel@tonic-gate 				if (buf_left == 0) {
4907c478bd9Sstevel@tonic-gate 					tmp = dmp;
4917c478bd9Sstevel@tonic-gate 					buf_left = MIN(total_size, maxblk);
4927c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
493c28749e9Skais 					dmp = allocb(buf_left + extra, BPRI_HI);
4947c478bd9Sstevel@tonic-gate 					if (dmp == NULL) {
4957c478bd9Sstevel@tonic-gate 						freemsg(head);
4967c478bd9Sstevel@tonic-gate 						return (ENOMEM);
4977c478bd9Sstevel@tonic-gate 					}
4987c478bd9Sstevel@tonic-gate 					dmp->b_wptr = dmp->b_rptr =
4997c478bd9Sstevel@tonic-gate 					    dmp->b_rptr + wroff;
5007c478bd9Sstevel@tonic-gate 					tmp->b_cont = dmp;
5017c478bd9Sstevel@tonic-gate 				} else {
5027c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
5037c478bd9Sstevel@tonic-gate 				}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
5067c478bd9Sstevel@tonic-gate 				aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
5077c478bd9Sstevel@tonic-gate 				auio.uio_loffset = *fileoff;
5087c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
5097c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
5107c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
5117c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_USERSPACE;
5127c478bd9Sstevel@tonic-gate 				auio.uio_llimit = curproc->p_fsz_ctl;
5137c478bd9Sstevel@tonic-gate 				auio.uio_fmode = fflag;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 				buf_left -= iov_len;
5167c478bd9Sstevel@tonic-gate 				total_size -= iov_len;
5177c478bd9Sstevel@tonic-gate 				sfv_len -= iov_len;
5187c478bd9Sstevel@tonic-gate 				sfv_off += iov_len;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 				error = uiomove((caddr_t)dmp->b_wptr,
5217c478bd9Sstevel@tonic-gate 				    iov_len, UIO_WRITE, &auio);
5227c478bd9Sstevel@tonic-gate 				if (error != 0) {
5237c478bd9Sstevel@tonic-gate 					freemsg(head);
5247c478bd9Sstevel@tonic-gate 					return (error);
5257c478bd9Sstevel@tonic-gate 				}
5267c478bd9Sstevel@tonic-gate 				dmp->b_wptr += iov_len;
5277c478bd9Sstevel@tonic-gate 			}
5287c478bd9Sstevel@tonic-gate 		} else {
5297c478bd9Sstevel@tonic-gate 			file_t	*ffp;
5307c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL) {
5337c478bd9Sstevel@tonic-gate 				freemsg(head);
5347c478bd9Sstevel@tonic-gate 				return (EBADF);
5357c478bd9Sstevel@tonic-gate 			}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
5387c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
5397c478bd9Sstevel@tonic-gate 				freemsg(head);
5407c478bd9Sstevel@tonic-gate 				return (EACCES);
5417c478bd9Sstevel@tonic-gate 			}
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
5447c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
5457c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
5467c478bd9Sstevel@tonic-gate 				freemsg(head);
5477c478bd9Sstevel@tonic-gate 				return (EINVAL);
5487c478bd9Sstevel@tonic-gate 			}
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 			/*
5517c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
5527c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
5537c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
5547c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
5557c478bd9Sstevel@tonic-gate 			 */
5567c478bd9Sstevel@tonic-gate 
557bfb244baSpr14459 			if (vn_compare(vp, readvp)) {
5587c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
5597c478bd9Sstevel@tonic-gate 				freemsg(head);
5607c478bd9Sstevel@tonic-gate 				return (EINVAL);
5617c478bd9Sstevel@tonic-gate 			}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 			/*
5647c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
5657c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
5667c478bd9Sstevel@tonic-gate 			 */
5677c478bd9Sstevel@tonic-gate 
568b3528faeSVasumathi Sundaram - Sun Microsystems 			if (readvp < vp) {
569b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
570b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
571b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
572b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
573b3528faeSVasumathi Sundaram - Sun Microsystems 			} else {
574b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
575b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
576b3528faeSVasumathi Sundaram - Sun Microsystems 			}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 			/* Same checks as in pread */
5797c478bd9Sstevel@tonic-gate 			if (sfv_off > maxoff) {
580b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
5817c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
5827c478bd9Sstevel@tonic-gate 				freemsg(head);
5837c478bd9Sstevel@tonic-gate 				return (EINVAL);
5847c478bd9Sstevel@tonic-gate 			}
5857c478bd9Sstevel@tonic-gate 			if (sfv_off + sfv_len > maxoff) {
5860a8433caSrshoaib 				total_size -= (sfv_off + sfv_len - maxoff);
5877c478bd9Sstevel@tonic-gate 				sfv_len = (ssize_t)((offset_t)maxoff -
5887c478bd9Sstevel@tonic-gate 				    sfv_off);
5897c478bd9Sstevel@tonic-gate 			}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
5927c478bd9Sstevel@tonic-gate 				if (buf_left == 0) {
5937c478bd9Sstevel@tonic-gate 					tmp = dmp;
5947c478bd9Sstevel@tonic-gate 					buf_left = MIN(total_size, maxblk);
5957c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
596c28749e9Skais 					dmp = allocb(buf_left + extra, BPRI_HI);
5977c478bd9Sstevel@tonic-gate 					if (dmp == NULL) {
598b3528faeSVasumathi Sundaram - Sun Microsystems 						VOP_RWUNLOCK(readvp,
599b3528faeSVasumathi Sundaram - Sun Microsystems 						    V_WRITELOCK_FALSE, NULL);
6007c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
6017c478bd9Sstevel@tonic-gate 						freemsg(head);
6027c478bd9Sstevel@tonic-gate 						return (ENOMEM);
6037c478bd9Sstevel@tonic-gate 					}
6047c478bd9Sstevel@tonic-gate 					dmp->b_wptr = dmp->b_rptr =
6057c478bd9Sstevel@tonic-gate 					    dmp->b_rptr + wroff;
6067c478bd9Sstevel@tonic-gate 					tmp->b_cont = dmp;
6077c478bd9Sstevel@tonic-gate 				} else {
6087c478bd9Sstevel@tonic-gate 					iov_len = MIN(buf_left, sfv_len);
6097c478bd9Sstevel@tonic-gate 				}
6107c478bd9Sstevel@tonic-gate 				aiov.iov_base = (caddr_t)dmp->b_wptr;
6117c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
6127c478bd9Sstevel@tonic-gate 				auio.uio_loffset = sfv_off;
6137c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
6147c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
6157c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
6167c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
6177c478bd9Sstevel@tonic-gate 				auio.uio_llimit = MAXOFFSET_T;
6187c478bd9Sstevel@tonic-gate 				auio.uio_fmode = ffp->f_flag;
6197c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
6207c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 				/*
6237c478bd9Sstevel@tonic-gate 				 * If read sync is not asked for,
6247c478bd9Sstevel@tonic-gate 				 * filter sync flags
6257c478bd9Sstevel@tonic-gate 				 */
6267c478bd9Sstevel@tonic-gate 				if ((ioflag & FRSYNC) == 0)
6277c478bd9Sstevel@tonic-gate 					ioflag &= ~(FSYNC|FDSYNC);
6287c478bd9Sstevel@tonic-gate 				error = VOP_READ(readvp, &auio, ioflag,
6297c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
6307c478bd9Sstevel@tonic-gate 				if (error != 0) {
6317c478bd9Sstevel@tonic-gate 					/*
6327c478bd9Sstevel@tonic-gate 					 * If we were reading a pipe (currently
6337c478bd9Sstevel@tonic-gate 					 * not implemented), we may now loose
6347c478bd9Sstevel@tonic-gate 					 * data.
6357c478bd9Sstevel@tonic-gate 					 */
636b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
637b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
6387c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
6397c478bd9Sstevel@tonic-gate 					freemsg(head);
6407c478bd9Sstevel@tonic-gate 					return (error);
6417c478bd9Sstevel@tonic-gate 				}
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 				/*
6447c478bd9Sstevel@tonic-gate 				 * Check how much data was really read.
6457c478bd9Sstevel@tonic-gate 				 * Decrement the 'len' and increment the
6467c478bd9Sstevel@tonic-gate 				 * 'off' appropriately.
6477c478bd9Sstevel@tonic-gate 				 */
6487c478bd9Sstevel@tonic-gate 				cnt = iov_len - auio.uio_resid;
6497c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
650b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
651b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
6527c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
6537c478bd9Sstevel@tonic-gate 					freemsg(head);
6547c478bd9Sstevel@tonic-gate 					return (EINVAL);
6557c478bd9Sstevel@tonic-gate 				}
6567c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
6577c478bd9Sstevel@tonic-gate 				sfv_off += cnt;
6587c478bd9Sstevel@tonic-gate 				total_size -= cnt;
6597c478bd9Sstevel@tonic-gate 				buf_left -= cnt;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 				dmp->b_wptr += cnt;
6627c478bd9Sstevel@tonic-gate 			}
663b3528faeSVasumathi Sundaram - Sun Microsystems 			VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
6647c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate 		sfv++;
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	ASSERT(total_size == 0);
6700f1702c5SYu Xiangning 	error = socket_sendmblk(VTOSO(vp), &msg, fflag, CRED(), &head);
6717c478bd9Sstevel@tonic-gate 	if (error != 0) {
6720f1702c5SYu Xiangning 		if (head != NULL)
6737c478bd9Sstevel@tonic-gate 			freemsg(head);
6747c478bd9Sstevel@tonic-gate 		return (error);
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_ru.ioch += (ulong_t)size;
6777c478bd9Sstevel@tonic-gate 	*count += size;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	return (0);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate int
sendvec_chunk(file_t * fp,u_offset_t * fileoff,struct sendfilevec * sfv,int copy_cnt,ssize_t * count)6847c478bd9Sstevel@tonic-gate sendvec_chunk(file_t *fp, u_offset_t *fileoff, struct sendfilevec *sfv,
6857c478bd9Sstevel@tonic-gate     int copy_cnt, ssize_t *count)
6867c478bd9Sstevel@tonic-gate {
6877c478bd9Sstevel@tonic-gate 	struct vnode *vp;
6887c478bd9Sstevel@tonic-gate 	struct uio auio;
6897c478bd9Sstevel@tonic-gate 	struct iovec aiov;
6907c478bd9Sstevel@tonic-gate 	ushort_t fflag;
6917c478bd9Sstevel@tonic-gate 	int ioflag;
6927c478bd9Sstevel@tonic-gate 	int i, error;
6937c478bd9Sstevel@tonic-gate 	size_t cnt;
6947c478bd9Sstevel@tonic-gate 	ssize_t sfv_len;
6957c478bd9Sstevel@tonic-gate 	u_offset_t sfv_off;
6967c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
6977c478bd9Sstevel@tonic-gate 	model_t model = get_udatamodel();
6987c478bd9Sstevel@tonic-gate 	u_offset_t maxoff = (model == DATAMODEL_ILP32) ?
6997c478bd9Sstevel@tonic-gate 	    MAXOFF32_T : MAXOFFSET_T;
7007c478bd9Sstevel@tonic-gate #else
7017c478bd9Sstevel@tonic-gate 	const u_offset_t maxoff = MAXOFF32_T;
7027c478bd9Sstevel@tonic-gate #endif
7031574ae68Sjohansen 	mblk_t	*dmp = NULL;
7041574ae68Sjohansen 	char	*buf = NULL;
705c28749e9Skais 	size_t  extra;
706c28749e9Skais 	int maxblk, wroff, tail_len;
707c28749e9Skais 	struct sonode *so;
708c28749e9Skais 	stdata_t *stp;
7090f1702c5SYu Xiangning 	struct nmsghdr msg;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
7127c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
7137c478bd9Sstevel@tonic-gate 
714c28749e9Skais 	if (vp->v_type == VSOCK) {
715c28749e9Skais 		so = VTOSO(vp);
7160f1702c5SYu Xiangning 		if (vp->v_stream != NULL) {
717c28749e9Skais 			stp = vp->v_stream;
718c28749e9Skais 			wroff = (int)stp->sd_wroff;
719c28749e9Skais 			tail_len = (int)stp->sd_tail;
720c28749e9Skais 			maxblk = (int)stp->sd_maxblk;
7210f1702c5SYu Xiangning 		} else {
7220f1702c5SYu Xiangning 			stp = NULL;
7230f1702c5SYu Xiangning 			wroff = so->so_proto_props.sopp_wroff;
7240f1702c5SYu Xiangning 			tail_len = so->so_proto_props.sopp_tail;
7250f1702c5SYu Xiangning 			maxblk = so->so_proto_props.sopp_maxblk;
7260f1702c5SYu Xiangning 		}
727c28749e9Skais 		extra = wroff + tail_len;
728c28749e9Skais 	}
729c28749e9Skais 
7300f1702c5SYu Xiangning 	bzero(&msg, sizeof (msg));
7317c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_DEFAULT;
7327c478bd9Sstevel@tonic-gate 	for (i = 0; i < copy_cnt; i++) {
7337c478bd9Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING))
7347c478bd9Sstevel@tonic-gate 			return (EINTR);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		/*
7377c478bd9Sstevel@tonic-gate 		 * Do similar checks as "write" as we are writing
7387c478bd9Sstevel@tonic-gate 		 * sfv_len bytes into "vp".
7397c478bd9Sstevel@tonic-gate 		 */
7407c478bd9Sstevel@tonic-gate 		sfv_len = (ssize_t)sfv->sfv_len;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		if (sfv_len == 0) {
7437c478bd9Sstevel@tonic-gate 			sfv++;
7447c478bd9Sstevel@tonic-gate 			continue;
7457c478bd9Sstevel@tonic-gate 		}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG) {
7487c478bd9Sstevel@tonic-gate 			if (*fileoff >= curproc->p_fsz_ctl) {
7497c478bd9Sstevel@tonic-gate 				mutex_enter(&curproc->p_lock);
7507c478bd9Sstevel@tonic-gate 				(void) rctl_action(
7517c478bd9Sstevel@tonic-gate 				    rctlproc_legacy[RLIMIT_FSIZE],
7527c478bd9Sstevel@tonic-gate 				    curproc->p_rctls, curproc, RCA_SAFE);
7537c478bd9Sstevel@tonic-gate 				mutex_exit(&curproc->p_lock);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 				return (EFBIG);
7567c478bd9Sstevel@tonic-gate 			}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 			if (*fileoff >= maxoff)
7597c478bd9Sstevel@tonic-gate 				return (EFBIG);
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 			if (*fileoff + sfv_len > maxoff)
7627c478bd9Sstevel@tonic-gate 				return (EINVAL);
7637c478bd9Sstevel@tonic-gate 		}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 		/* Check for overflow */
7667c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
7677c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_ILP32) {
7687c478bd9Sstevel@tonic-gate 			if (((ssize32_t)(*count + sfv_len)) < 0)
7697c478bd9Sstevel@tonic-gate 				return (EINVAL);
7707c478bd9Sstevel@tonic-gate 		} else
7717c478bd9Sstevel@tonic-gate #endif
7727c478bd9Sstevel@tonic-gate 		if ((*count + sfv_len) < 0)
7737c478bd9Sstevel@tonic-gate 			return (EINVAL);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 		sfv_off = (u_offset_t)(ulong_t)sfv->sfv_off;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		if (sfv->sfv_fd == SFV_FD_SELF) {
7787c478bd9Sstevel@tonic-gate 			if (vp->v_type == VSOCK) {
779e116a42fSPrakash Jalan 				while (sfv_len > 0) {
780e116a42fSPrakash Jalan 					size_t iov_len;
7817c478bd9Sstevel@tonic-gate 
782e116a42fSPrakash Jalan 					iov_len = sfv_len;
7833e95bd4aSAnders Persson 					/*
7843e95bd4aSAnders Persson 					 * Socket filters can limit the mblk
7853e95bd4aSAnders Persson 					 * size, so limit reads to maxblk if
7863e95bd4aSAnders Persson 					 * there are filters present.
7873e95bd4aSAnders Persson 					 */
788dd49f125SAnders Persson 					if (so->so_filter_active > 0 &&
789dd49f125SAnders Persson 					    maxblk != INFPSZ)
790e116a42fSPrakash Jalan 						iov_len = MIN(iov_len, maxblk);
7917c478bd9Sstevel@tonic-gate 
792e116a42fSPrakash Jalan 					aiov.iov_len = iov_len;
793e116a42fSPrakash Jalan 					aiov.iov_base =
794e116a42fSPrakash Jalan 					    (caddr_t)(uintptr_t)sfv_off;
795e116a42fSPrakash Jalan 
796e116a42fSPrakash Jalan 					auio.uio_iov = &aiov;
797e116a42fSPrakash Jalan 					auio.uio_iovcnt = 1;
798e116a42fSPrakash Jalan 					auio.uio_loffset = *fileoff;
799e116a42fSPrakash Jalan 					auio.uio_segflg = UIO_USERSPACE;
800e116a42fSPrakash Jalan 					auio.uio_fmode = fflag;
801e116a42fSPrakash Jalan 					auio.uio_llimit = curproc->p_fsz_ctl;
802e116a42fSPrakash Jalan 					auio.uio_resid = iov_len;
803e116a42fSPrakash Jalan 
804e116a42fSPrakash Jalan 					dmp = allocb(iov_len + extra, BPRI_HI);
8057c478bd9Sstevel@tonic-gate 					if (dmp == NULL)
8067c478bd9Sstevel@tonic-gate 						return (ENOMEM);
807e116a42fSPrakash Jalan 					dmp->b_wptr = dmp->b_rptr =
808e116a42fSPrakash Jalan 					    dmp->b_rptr + wroff;
8097c478bd9Sstevel@tonic-gate 					error = uiomove((caddr_t)dmp->b_wptr,
810e116a42fSPrakash Jalan 					    iov_len, UIO_WRITE, &auio);
8117c478bd9Sstevel@tonic-gate 					if (error != 0) {
8127c478bd9Sstevel@tonic-gate 						freeb(dmp);
8137c478bd9Sstevel@tonic-gate 						return (error);
8147c478bd9Sstevel@tonic-gate 					}
815e116a42fSPrakash Jalan 					dmp->b_wptr += iov_len;
8160f1702c5SYu Xiangning 					error = socket_sendmblk(VTOSO(vp),
8170f1702c5SYu Xiangning 					    &msg, fflag, CRED(), &dmp);
8180f1702c5SYu Xiangning 
8197c478bd9Sstevel@tonic-gate 					if (error != 0) {
8200f1702c5SYu Xiangning 						if (dmp != NULL)
8217c478bd9Sstevel@tonic-gate 							freeb(dmp);
8227c478bd9Sstevel@tonic-gate 						return (error);
8237c478bd9Sstevel@tonic-gate 					}
8247c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
825e116a42fSPrakash Jalan 					    (ulong_t)iov_len;
826e116a42fSPrakash Jalan 					*count += iov_len;
827e116a42fSPrakash Jalan 					sfv_len -= iov_len;
828e116a42fSPrakash Jalan 					sfv_off += iov_len;
829e116a42fSPrakash Jalan 				}
8307c478bd9Sstevel@tonic-gate 			} else {
831e116a42fSPrakash Jalan 				aiov.iov_len = sfv_len;
832e116a42fSPrakash Jalan 				aiov.iov_base = (caddr_t)(uintptr_t)sfv_off;
833e116a42fSPrakash Jalan 
834e116a42fSPrakash Jalan 				auio.uio_iov = &aiov;
835e116a42fSPrakash Jalan 				auio.uio_iovcnt = 1;
836e116a42fSPrakash Jalan 				auio.uio_loffset = *fileoff;
837e116a42fSPrakash Jalan 				auio.uio_segflg = UIO_USERSPACE;
838e116a42fSPrakash Jalan 				auio.uio_fmode = fflag;
839e116a42fSPrakash Jalan 				auio.uio_llimit = curproc->p_fsz_ctl;
840e116a42fSPrakash Jalan 				auio.uio_resid = sfv_len;
841e116a42fSPrakash Jalan 
8427c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
8437c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
8447c478bd9Sstevel@tonic-gate 				while (sfv_len > 0) {
8457c478bd9Sstevel@tonic-gate 					error = VOP_WRITE(vp, &auio, ioflag,
8467c478bd9Sstevel@tonic-gate 					    fp->f_cred, NULL);
8477c478bd9Sstevel@tonic-gate 					cnt = sfv_len - auio.uio_resid;
8487c478bd9Sstevel@tonic-gate 					sfv_len -= cnt;
8497c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
8507c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
8517c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
8527c478bd9Sstevel@tonic-gate 					*count += cnt;
8537c478bd9Sstevel@tonic-gate 					if (error != 0)
8547c478bd9Sstevel@tonic-gate 						return (error);
8557c478bd9Sstevel@tonic-gate 				}
8567c478bd9Sstevel@tonic-gate 			}
8577c478bd9Sstevel@tonic-gate 		} else {
8587f9ee850Sblu 			int segmapit = 0;
8597c478bd9Sstevel@tonic-gate 			file_t	*ffp;
8607c478bd9Sstevel@tonic-gate 			vnode_t	*readvp;
861bfb244baSpr14459 			struct vnode *realvp;
8627c478bd9Sstevel@tonic-gate 			size_t	size;
8637c478bd9Sstevel@tonic-gate 			caddr_t	ptr;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 			if ((ffp = getf(sfv->sfv_fd)) == NULL)
8667c478bd9Sstevel@tonic-gate 				return (EBADF);
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 			if ((ffp->f_flag & FREAD) == 0) {
8697c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
8707c478bd9Sstevel@tonic-gate 				return (EBADF);
8717c478bd9Sstevel@tonic-gate 			}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 			readvp = ffp->f_vnode;
874da6c28aaSamw 			if (VOP_REALVP(readvp, &realvp, NULL) == 0)
875bfb244baSpr14459 				readvp = realvp;
8767c478bd9Sstevel@tonic-gate 			if (readvp->v_type != VREG) {
8777c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
8787c478bd9Sstevel@tonic-gate 				return (EINVAL);
8797c478bd9Sstevel@tonic-gate 			}
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 			/*
8827c478bd9Sstevel@tonic-gate 			 * No point reading and writing to same vp,
8837c478bd9Sstevel@tonic-gate 			 * as long as both are regular files. readvp is not
8847c478bd9Sstevel@tonic-gate 			 * locked; but since we got it from an open file the
8857c478bd9Sstevel@tonic-gate 			 * contents will be valid during the time of access.
8867c478bd9Sstevel@tonic-gate 			 */
887bfb244baSpr14459 			if (vn_compare(vp, readvp)) {
8887c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
8897c478bd9Sstevel@tonic-gate 				return (EINVAL);
8907c478bd9Sstevel@tonic-gate 			}
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 			/*
8937c478bd9Sstevel@tonic-gate 			 * Note: we assume readvp != vp. "vp" is already
8947c478bd9Sstevel@tonic-gate 			 * locked, and "readvp" must not be.
8957c478bd9Sstevel@tonic-gate 			 */
896b3528faeSVasumathi Sundaram - Sun Microsystems 			if (readvp < vp) {
897b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
898b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
899b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
900b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
901b3528faeSVasumathi Sundaram - Sun Microsystems 			} else {
902b3528faeSVasumathi Sundaram - Sun Microsystems 				(void) VOP_RWLOCK(readvp, V_WRITELOCK_FALSE,
903b3528faeSVasumathi Sundaram - Sun Microsystems 				    NULL);
904b3528faeSVasumathi Sundaram - Sun Microsystems 			}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 			/* Same checks as in pread */
9077c478bd9Sstevel@tonic-gate 			if (sfv_off > maxoff) {
908b3528faeSVasumathi Sundaram - Sun Microsystems 				VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
9097c478bd9Sstevel@tonic-gate 				releasef(sfv->sfv_fd);
9107c478bd9Sstevel@tonic-gate 				return (EINVAL);
9117c478bd9Sstevel@tonic-gate 			}
9127c478bd9Sstevel@tonic-gate 			if (sfv_off + sfv_len > maxoff) {
9137c478bd9Sstevel@tonic-gate 				sfv_len = (ssize_t)((offset_t)maxoff -
9147c478bd9Sstevel@tonic-gate 				    sfv_off);
9157c478bd9Sstevel@tonic-gate 			}
9167c478bd9Sstevel@tonic-gate 			/* Find the native blocksize to transfer data */
9177c478bd9Sstevel@tonic-gate 			size = MIN(vp->v_vfsp->vfs_bsize,
9187c478bd9Sstevel@tonic-gate 			    readvp->v_vfsp->vfs_bsize);
9197c478bd9Sstevel@tonic-gate 			size = sfv_len < size ? sfv_len : size;
9207c478bd9Sstevel@tonic-gate 
9211574ae68Sjohansen 			if (vp->v_type != VSOCK) {
92274024373Spr14459 				segmapit = 0;
9231574ae68Sjohansen 				buf = kmem_alloc(size, KM_NOSLEEP);
9241574ae68Sjohansen 				if (buf == NULL) {
925b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
926b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
9271574ae68Sjohansen 					releasef(sfv->sfv_fd);
9281574ae68Sjohansen 					return (ENOMEM);
9291574ae68Sjohansen 				}
930c28749e9Skais 			} else {
9310f1702c5SYu Xiangning 				uint_t	copyflag;
9320f1702c5SYu Xiangning 
9330f1702c5SYu Xiangning 				copyflag = stp != NULL ? stp->sd_copyflag :
9340f1702c5SYu Xiangning 				    so->so_proto_props.sopp_zcopyflag;
9353e95bd4aSAnders Persson 
936c28749e9Skais 				/*
9373e95bd4aSAnders Persson 				 * Socket filters can limit the mblk size,
9383e95bd4aSAnders Persson 				 * so limit reads to maxblk if there are
9393e95bd4aSAnders Persson 				 * filters present.
940c28749e9Skais 				 */
941dd49f125SAnders Persson 				if (so->so_filter_active > 0 &&
942dd49f125SAnders Persson 				    maxblk != INFPSZ)
943c28749e9Skais 					size = MIN(size, maxblk);
94474024373Spr14459 
94574024373Spr14459 				if (vn_has_flocks(readvp) ||
94674024373Spr14459 				    readvp->v_flag & VNOMAP ||
9470f1702c5SYu Xiangning 				    copyflag & STZCVMUNSAFE) {
94874024373Spr14459 					segmapit = 0;
9490f1702c5SYu Xiangning 				} else if (copyflag & STZCVMSAFE) {
95074024373Spr14459 					segmapit = 1;
95174024373Spr14459 				} else {
95274024373Spr14459 					int on = 1;
9530f1702c5SYu Xiangning 					if (socket_setsockopt(VTOSO(vp),
95474024373Spr14459 					    SOL_SOCKET, SO_SND_COPYAVOID,
9550f1702c5SYu Xiangning 					    &on, sizeof (on), CRED()) == 0)
95674024373Spr14459 						segmapit = 1;
95774024373Spr14459 				}
95874024373Spr14459 			}
95974024373Spr14459 
96074024373Spr14459 			if (segmapit) {
961*4ef54b56SMarcel Telka 				struct vattr va;
96274024373Spr14459 				boolean_t nowait;
96374024373Spr14459 
964*4ef54b56SMarcel Telka 				va.va_mask = AT_SIZE;
965*4ef54b56SMarcel Telka 				error = VOP_GETATTR(readvp, &va, 0, kcred,
966*4ef54b56SMarcel Telka 				    NULL);
967*4ef54b56SMarcel Telka 				if (error != 0 || sfv_off >= va.va_size) {
968*4ef54b56SMarcel Telka 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
969*4ef54b56SMarcel Telka 					    NULL);
970*4ef54b56SMarcel Telka 					releasef(sfv->sfv_fd);
971*4ef54b56SMarcel Telka 					return (error);
972*4ef54b56SMarcel Telka 				}
973*4ef54b56SMarcel Telka 				/* Read as much as possible. */
974*4ef54b56SMarcel Telka 				if (sfv_off + sfv_len > va.va_size)
975*4ef54b56SMarcel Telka 					sfv_len = va.va_size - sfv_off;
976*4ef54b56SMarcel Telka 
97774024373Spr14459 				nowait = (sfv->sfv_flag & SFV_NOWAIT) != 0;
97874024373Spr14459 				error = snf_segmap(fp, readvp, sfv_off,
9793b3d24f3SJayakara Kini 				    (u_offset_t)sfv_len, (ssize_t *)&cnt,
9803b3d24f3SJayakara Kini 				    nowait);
98174024373Spr14459 				releasef(sfv->sfv_fd);
98274024373Spr14459 				*count += cnt;
98374024373Spr14459 				if (error)
98474024373Spr14459 					return (error);
98574024373Spr14459 				sfv++;
98674024373Spr14459 				continue;
9871574ae68Sjohansen 			}
9881574ae68Sjohansen 
9897c478bd9Sstevel@tonic-gate 			while (sfv_len > 0) {
9907c478bd9Sstevel@tonic-gate 				size_t	iov_len;
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 				iov_len = MIN(size, sfv_len);
9937c478bd9Sstevel@tonic-gate 
9941574ae68Sjohansen 				if (vp->v_type == VSOCK) {
995c28749e9Skais 					dmp = allocb(iov_len + extra, BPRI_HI);
9967c478bd9Sstevel@tonic-gate 					if (dmp == NULL) {
997b3528faeSVasumathi Sundaram - Sun Microsystems 						VOP_RWUNLOCK(readvp,
998b3528faeSVasumathi Sundaram - Sun Microsystems 						    V_WRITELOCK_FALSE, NULL);
9997c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
10007c478bd9Sstevel@tonic-gate 						return (ENOMEM);
10017c478bd9Sstevel@tonic-gate 					}
1002c28749e9Skais 					dmp->b_wptr = dmp->b_rptr =
1003c28749e9Skais 					    dmp->b_rptr + wroff;
10047c478bd9Sstevel@tonic-gate 					ptr = (caddr_t)dmp->b_rptr;
10051574ae68Sjohansen 				} else {
10061574ae68Sjohansen 					ptr = buf;
10071574ae68Sjohansen 				}
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 				aiov.iov_base = ptr;
10107c478bd9Sstevel@tonic-gate 				aiov.iov_len = iov_len;
10117c478bd9Sstevel@tonic-gate 				auio.uio_loffset = sfv_off;
10127c478bd9Sstevel@tonic-gate 				auio.uio_iov = &aiov;
10137c478bd9Sstevel@tonic-gate 				auio.uio_iovcnt = 1;
10147c478bd9Sstevel@tonic-gate 				auio.uio_resid = iov_len;
10157c478bd9Sstevel@tonic-gate 				auio.uio_segflg = UIO_SYSSPACE;
10167c478bd9Sstevel@tonic-gate 				auio.uio_llimit = MAXOFFSET_T;
10177c478bd9Sstevel@tonic-gate 				auio.uio_fmode = ffp->f_flag;
10187c478bd9Sstevel@tonic-gate 				ioflag = auio.uio_fmode &
10197c478bd9Sstevel@tonic-gate 				    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 				/*
10227c478bd9Sstevel@tonic-gate 				 * If read sync is not asked for,
10237c478bd9Sstevel@tonic-gate 				 * filter sync flags
10247c478bd9Sstevel@tonic-gate 				 */
10257c478bd9Sstevel@tonic-gate 				if ((ioflag & FRSYNC) == 0)
10267c478bd9Sstevel@tonic-gate 					ioflag &= ~(FSYNC|FDSYNC);
10277c478bd9Sstevel@tonic-gate 				error = VOP_READ(readvp, &auio, ioflag,
10287c478bd9Sstevel@tonic-gate 				    fp->f_cred, NULL);
10297c478bd9Sstevel@tonic-gate 				if (error != 0) {
10307c478bd9Sstevel@tonic-gate 					/*
10317c478bd9Sstevel@tonic-gate 					 * If we were reading a pipe (currently
10327c478bd9Sstevel@tonic-gate 					 * not implemented), we may now lose
10337c478bd9Sstevel@tonic-gate 					 * data.
10347c478bd9Sstevel@tonic-gate 					 */
10351574ae68Sjohansen 					if (vp->v_type == VSOCK)
10367c478bd9Sstevel@tonic-gate 						freeb(dmp);
10371574ae68Sjohansen 					else
10381574ae68Sjohansen 						kmem_free(buf, size);
1039b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
1040b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
10417c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
10427c478bd9Sstevel@tonic-gate 					return (error);
10437c478bd9Sstevel@tonic-gate 				}
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 				/*
10467c478bd9Sstevel@tonic-gate 				 * Check how much data was really read.
10477c478bd9Sstevel@tonic-gate 				 * Decrement the 'len' and increment the
10487c478bd9Sstevel@tonic-gate 				 * 'off' appropriately.
10497c478bd9Sstevel@tonic-gate 				 */
10507c478bd9Sstevel@tonic-gate 				cnt = iov_len - auio.uio_resid;
10517c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
10521574ae68Sjohansen 					if (vp->v_type == VSOCK)
10537c478bd9Sstevel@tonic-gate 						freeb(dmp);
10541574ae68Sjohansen 					else
10551574ae68Sjohansen 						kmem_free(buf, size);
1056b3528faeSVasumathi Sundaram - Sun Microsystems 					VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE,
1057b3528faeSVasumathi Sundaram - Sun Microsystems 					    NULL);
10587c478bd9Sstevel@tonic-gate 					releasef(sfv->sfv_fd);
10597c478bd9Sstevel@tonic-gate 					return (EINVAL);
10607c478bd9Sstevel@tonic-gate 				}
10617c478bd9Sstevel@tonic-gate 				sfv_len -= cnt;
10627c478bd9Sstevel@tonic-gate 				sfv_off += cnt;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 				if (vp->v_type == VSOCK) {
10657c478bd9Sstevel@tonic-gate 					dmp->b_wptr = dmp->b_rptr + cnt;
10667c478bd9Sstevel@tonic-gate 
10670f1702c5SYu Xiangning 					error = socket_sendmblk(VTOSO(vp),
10680f1702c5SYu Xiangning 					    &msg, fflag, CRED(), &dmp);
10690f1702c5SYu Xiangning 
10707c478bd9Sstevel@tonic-gate 					if (error != 0) {
10710f1702c5SYu Xiangning 						if (dmp != NULL)
10727c478bd9Sstevel@tonic-gate 							freeb(dmp);
1073b3528faeSVasumathi Sundaram - Sun Microsystems 						VOP_RWUNLOCK(readvp,
1074b3528faeSVasumathi Sundaram - Sun Microsystems 						    V_WRITELOCK_FALSE, NULL);
10757c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
10767c478bd9Sstevel@tonic-gate 						return (error);
10777c478bd9Sstevel@tonic-gate 					}
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
10807c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
10817c478bd9Sstevel@tonic-gate 					*count += cnt;
10827c478bd9Sstevel@tonic-gate 				} else {
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 					aiov.iov_base = ptr;
10857c478bd9Sstevel@tonic-gate 					aiov.iov_len = cnt;
10867c478bd9Sstevel@tonic-gate 					auio.uio_loffset = *fileoff;
10877c478bd9Sstevel@tonic-gate 					auio.uio_resid = cnt;
1088a490cd42SPrakash Jalan 					auio.uio_iov = &aiov;
1089a490cd42SPrakash Jalan 					auio.uio_iovcnt = 1;
10907c478bd9Sstevel@tonic-gate 					auio.uio_segflg = UIO_SYSSPACE;
10917c478bd9Sstevel@tonic-gate 					auio.uio_llimit = curproc->p_fsz_ctl;
10927c478bd9Sstevel@tonic-gate 					auio.uio_fmode = fflag;
10937c478bd9Sstevel@tonic-gate 					ioflag = auio.uio_fmode &
10947c478bd9Sstevel@tonic-gate 					    (FAPPEND|FSYNC|FDSYNC|FRSYNC);
10957c478bd9Sstevel@tonic-gate 					error = VOP_WRITE(vp, &auio, ioflag,
10967c478bd9Sstevel@tonic-gate 					    fp->f_cred, NULL);
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 					/*
10997c478bd9Sstevel@tonic-gate 					 * Check how much data was written.
11007c478bd9Sstevel@tonic-gate 					 * Increment the 'len' and decrement the
11017c478bd9Sstevel@tonic-gate 					 * 'off' if all the data was not
11027c478bd9Sstevel@tonic-gate 					 * written.
11037c478bd9Sstevel@tonic-gate 					 */
11047c478bd9Sstevel@tonic-gate 					cnt -= auio.uio_resid;
11057c478bd9Sstevel@tonic-gate 					sfv_len += auio.uio_resid;
11067c478bd9Sstevel@tonic-gate 					sfv_off -= auio.uio_resid;
11077c478bd9Sstevel@tonic-gate 					ttolwp(curthread)->lwp_ru.ioch +=
11087c478bd9Sstevel@tonic-gate 					    (ulong_t)cnt;
11097c478bd9Sstevel@tonic-gate 					*fileoff += cnt;
11107c478bd9Sstevel@tonic-gate 					*count += cnt;
11117c478bd9Sstevel@tonic-gate 					if (error != 0) {
11122db6c79fSstevel 						kmem_free(buf, size);
1113b3528faeSVasumathi Sundaram - Sun Microsystems 						VOP_RWUNLOCK(readvp,
1114b3528faeSVasumathi Sundaram - Sun Microsystems 						    V_WRITELOCK_FALSE, NULL);
11157c478bd9Sstevel@tonic-gate 						releasef(sfv->sfv_fd);
11167c478bd9Sstevel@tonic-gate 						return (error);
11177c478bd9Sstevel@tonic-gate 					}
11187c478bd9Sstevel@tonic-gate 				}
11197c478bd9Sstevel@tonic-gate 			}
11201574ae68Sjohansen 			if (buf) {
11211574ae68Sjohansen 				kmem_free(buf, size);
11221574ae68Sjohansen 				buf = NULL;
11231574ae68Sjohansen 			}
1124b3528faeSVasumathi Sundaram - Sun Microsystems 			VOP_RWUNLOCK(readvp, V_WRITELOCK_FALSE, NULL);
11257c478bd9Sstevel@tonic-gate 			releasef(sfv->sfv_fd);
11267c478bd9Sstevel@tonic-gate 		}
11277c478bd9Sstevel@tonic-gate 		sfv++;
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 	return (0);
11307c478bd9Sstevel@tonic-gate }
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate ssize_t
sendfilev(int opcode,int fildes,const struct sendfilevec * vec,int sfvcnt,size_t * xferred)11337c478bd9Sstevel@tonic-gate sendfilev(int opcode, int fildes, const struct sendfilevec *vec, int sfvcnt,
11347c478bd9Sstevel@tonic-gate     size_t *xferred)
11357c478bd9Sstevel@tonic-gate {
1136311e3d4cSgeorges 	int error = 0;
1137311e3d4cSgeorges 	int first_vector_error = 0;
11387c478bd9Sstevel@tonic-gate 	file_t *fp;
11397c478bd9Sstevel@tonic-gate 	struct vnode *vp;
11407c478bd9Sstevel@tonic-gate 	struct sonode *so;
11417c478bd9Sstevel@tonic-gate 	u_offset_t fileoff;
11427c478bd9Sstevel@tonic-gate 	int copy_cnt;
11437c478bd9Sstevel@tonic-gate 	const struct sendfilevec *copy_vec;
11447c478bd9Sstevel@tonic-gate 	struct sendfilevec sfv[SEND_MAX_CHUNK];
11457c478bd9Sstevel@tonic-gate 	ssize_t count = 0;
11467c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11477c478bd9Sstevel@tonic-gate 	struct ksendfilevec32 sfv32[SEND_MAX_CHUNK];
11487c478bd9Sstevel@tonic-gate #endif
11490a8433caSrshoaib 	ssize_t total_size;
11507c478bd9Sstevel@tonic-gate 	int i;
11517c478bd9Sstevel@tonic-gate 	boolean_t is_sock = B_FALSE;
11527c478bd9Sstevel@tonic-gate 	int maxblk = 0;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	if (sfvcnt <= 0)
11557c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	if ((fp = getf(fildes)) == NULL)
11587c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	if (((fp->f_flag) & FWRITE) == 0) {
11617c478bd9Sstevel@tonic-gate 		error = EBADF;
11627c478bd9Sstevel@tonic-gate 		goto err;
11637c478bd9Sstevel@tonic-gate 	}
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	fileoff = fp->f_offset;
11667c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	switch (vp->v_type) {
11697c478bd9Sstevel@tonic-gate 	case VSOCK:
11707c478bd9Sstevel@tonic-gate 		so = VTOSO(vp);
11717c478bd9Sstevel@tonic-gate 		is_sock = B_TRUE;
11720f1702c5SYu Xiangning 		if (SOCK_IS_NONSTR(so)) {
11730f1702c5SYu Xiangning 			maxblk = so->so_proto_props.sopp_maxblk;
11747c478bd9Sstevel@tonic-gate 		} else {
11757c478bd9Sstevel@tonic-gate 			maxblk = (int)vp->v_stream->sd_maxblk;
11767c478bd9Sstevel@tonic-gate 		}
11777c478bd9Sstevel@tonic-gate 		break;
11787c478bd9Sstevel@tonic-gate 	case VREG:
11797c478bd9Sstevel@tonic-gate 		break;
11807c478bd9Sstevel@tonic-gate 	default:
11817c478bd9Sstevel@tonic-gate 		error = EINVAL;
11827c478bd9Sstevel@tonic-gate 		goto err;
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	switch (opcode) {
11867c478bd9Sstevel@tonic-gate 	case SENDFILEV :
11877c478bd9Sstevel@tonic-gate 		break;
11887c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
11897c478bd9Sstevel@tonic-gate 	case SENDFILEV64 :
11907c478bd9Sstevel@tonic-gate 		return (sendvec64(fp, (struct ksendfilevec64 *)vec, sfvcnt,
11917c478bd9Sstevel@tonic-gate 		    (size32_t *)xferred, fildes));
11927c478bd9Sstevel@tonic-gate #endif
11937c478bd9Sstevel@tonic-gate 	default :
11947c478bd9Sstevel@tonic-gate 		error = ENOSYS;
11957c478bd9Sstevel@tonic-gate 		break;
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
11997c478bd9Sstevel@tonic-gate 	copy_vec = vec;
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	do {
12020a8433caSrshoaib 		total_size = 0;
12037c478bd9Sstevel@tonic-gate 		copy_cnt = MIN(sfvcnt, SEND_MAX_CHUNK);
12047c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
12057c478bd9Sstevel@tonic-gate 		/* 32-bit callers need to have their iovec expanded. */
12067c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_ILP32) {
12077c478bd9Sstevel@tonic-gate 			if (copyin(copy_vec, sfv32,
12087c478bd9Sstevel@tonic-gate 			    copy_cnt * sizeof (ksendfilevec32_t))) {
12097c478bd9Sstevel@tonic-gate 				error = EFAULT;
12107c478bd9Sstevel@tonic-gate 				break;
12117c478bd9Sstevel@tonic-gate 			}
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 			for (i = 0; i < copy_cnt; i++) {
12147c478bd9Sstevel@tonic-gate 				sfv[i].sfv_fd = sfv32[i].sfv_fd;
12157c478bd9Sstevel@tonic-gate 				sfv[i].sfv_off =
12167c478bd9Sstevel@tonic-gate 				    (off_t)(uint32_t)sfv32[i].sfv_off;
12177c478bd9Sstevel@tonic-gate 				sfv[i].sfv_len = (size_t)sfv32[i].sfv_len;
12187c478bd9Sstevel@tonic-gate 				total_size += sfv[i].sfv_len;
12197c478bd9Sstevel@tonic-gate 				sfv[i].sfv_flag = sfv32[i].sfv_flag;
1220311e3d4cSgeorges 				/*
1221311e3d4cSgeorges 				 * Individual elements of the vector must not
1222311e3d4cSgeorges 				 * wrap or overflow, as later math is signed.
1223311e3d4cSgeorges 				 * Equally total_size needs to be checked after
1224311e3d4cSgeorges 				 * each vector is added in, to be sure that
1225311e3d4cSgeorges 				 * rogue values haven't overflowed the counter.
1226311e3d4cSgeorges 				 */
1227311e3d4cSgeorges 				if (((ssize32_t)sfv[i].sfv_len < 0) ||
1228311e3d4cSgeorges 				    ((ssize32_t)total_size < 0)) {
1229311e3d4cSgeorges 					/*
1230311e3d4cSgeorges 					 * Truncate the vector to send data
1231311e3d4cSgeorges 					 * described by elements before the
1232311e3d4cSgeorges 					 * error.
1233311e3d4cSgeorges 					 */
1234311e3d4cSgeorges 					copy_cnt = i;
1235311e3d4cSgeorges 					first_vector_error = EINVAL;
1236311e3d4cSgeorges 					/* total_size can't be trusted */
1237311e3d4cSgeorges 					if ((ssize32_t)total_size < 0)
1238311e3d4cSgeorges 						error = EINVAL;
1239311e3d4cSgeorges 					break;
12407c478bd9Sstevel@tonic-gate 				}
1241311e3d4cSgeorges 			}
1242311e3d4cSgeorges 			/* Nothing to do, process errors */
1243311e3d4cSgeorges 			if (copy_cnt == 0)
1244311e3d4cSgeorges 				break;
1245311e3d4cSgeorges 
12467c478bd9Sstevel@tonic-gate 		} else {
12477c478bd9Sstevel@tonic-gate #endif
12487c478bd9Sstevel@tonic-gate 			if (copyin(copy_vec, sfv,
12497c478bd9Sstevel@tonic-gate 			    copy_cnt * sizeof (sendfilevec_t))) {
12507c478bd9Sstevel@tonic-gate 				error = EFAULT;
12517c478bd9Sstevel@tonic-gate 				break;
12527c478bd9Sstevel@tonic-gate 			}
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 			for (i = 0; i < copy_cnt; i++) {
12557c478bd9Sstevel@tonic-gate 				total_size += sfv[i].sfv_len;
1256311e3d4cSgeorges 				/*
1257311e3d4cSgeorges 				 * Individual elements of the vector must not
1258311e3d4cSgeorges 				 * wrap or overflow, as later math is signed.
1259311e3d4cSgeorges 				 * Equally total_size needs to be checked after
1260311e3d4cSgeorges 				 * each vector is added in, to be sure that
1261311e3d4cSgeorges 				 * rogue values haven't overflowed the counter.
1262311e3d4cSgeorges 				 */
1263311e3d4cSgeorges 				if (((ssize_t)sfv[i].sfv_len < 0) ||
1264311e3d4cSgeorges 				    (total_size < 0)) {
1265311e3d4cSgeorges 					/*
1266311e3d4cSgeorges 					 * Truncate the vector to send data
1267311e3d4cSgeorges 					 * described by elements before the
1268311e3d4cSgeorges 					 * error.
1269311e3d4cSgeorges 					 */
1270311e3d4cSgeorges 					copy_cnt = i;
1271311e3d4cSgeorges 					first_vector_error = EINVAL;
1272311e3d4cSgeorges 					/* total_size can't be trusted */
1273311e3d4cSgeorges 					if (total_size < 0)
1274311e3d4cSgeorges 						error = EINVAL;
1275311e3d4cSgeorges 					break;
12767c478bd9Sstevel@tonic-gate 				}
1277311e3d4cSgeorges 			}
1278311e3d4cSgeorges 			/* Nothing to do, process errors */
1279311e3d4cSgeorges 			if (copy_cnt == 0)
1280311e3d4cSgeorges 				break;
12817c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
12827c478bd9Sstevel@tonic-gate 		}
12837c478bd9Sstevel@tonic-gate #endif
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 		/*
12867c478bd9Sstevel@tonic-gate 		 * The task between deciding to use sendvec_small_chunk
12877c478bd9Sstevel@tonic-gate 		 * and sendvec_chunk is dependant on multiple things:
12887c478bd9Sstevel@tonic-gate 		 *
12897c478bd9Sstevel@tonic-gate 		 * i) latency is important for smaller files. So if the
12907c478bd9Sstevel@tonic-gate 		 * data is smaller than 'tcp_slow_start_initial' times
12917c478bd9Sstevel@tonic-gate 		 * maxblk, then use sendvec_small_chunk which creates
12923b3d24f3SJayakara Kini 		 * maxblk size mblks and chains them together and sends
12937c478bd9Sstevel@tonic-gate 		 * them to TCP in one shot. It also leaves 'wroff' size
12947c478bd9Sstevel@tonic-gate 		 * space for the headers in each mblk.
12957c478bd9Sstevel@tonic-gate 		 *
12967c478bd9Sstevel@tonic-gate 		 * ii) for total size bigger than 'tcp_slow_start_initial'
12977c478bd9Sstevel@tonic-gate 		 * time maxblk, its probably real file data which is
12987c478bd9Sstevel@tonic-gate 		 * dominating. So its better to use sendvec_chunk because
12997c478bd9Sstevel@tonic-gate 		 * performance goes to dog if we don't do pagesize reads.
13007c478bd9Sstevel@tonic-gate 		 * sendvec_chunk will do pagesize reads and write them
13017c478bd9Sstevel@tonic-gate 		 * in pagesize mblks to TCP.
13027c478bd9Sstevel@tonic-gate 		 *
13037c478bd9Sstevel@tonic-gate 		 * Side Notes: A write to file has not been optimized.
13047c478bd9Sstevel@tonic-gate 		 * Future zero copy code will plugin into sendvec_chunk
13057c478bd9Sstevel@tonic-gate 		 * only because doing zero copy for files smaller then
13067c478bd9Sstevel@tonic-gate 		 * pagesize is useless.
13077c478bd9Sstevel@tonic-gate 		 *
13087c478bd9Sstevel@tonic-gate 		 * Note, if socket has NL7C enabled then call NL7C's
13092c9e429eSbrutus 		 * senfilev() function to consume the sfv[].
13107c478bd9Sstevel@tonic-gate 		 */
13117c478bd9Sstevel@tonic-gate 		if (is_sock) {
13120f1702c5SYu Xiangning 			if (!SOCK_IS_NONSTR(so) &&
13130f1702c5SYu Xiangning 			    _SOTOTPI(so)->sti_nl7c_flags != 0) {
13142c9e429eSbrutus 				error = nl7c_sendfilev(so, &fileoff,
13152c9e429eSbrutus 				    sfv, copy_cnt, &count);
13160f1702c5SYu Xiangning 			} else if ((total_size <= (4 * maxblk)) &&
13170f1702c5SYu Xiangning 			    error == 0) {
13187c478bd9Sstevel@tonic-gate 				error = sendvec_small_chunk(fp,
13197c478bd9Sstevel@tonic-gate 				    &fileoff, sfv, copy_cnt,
13207c478bd9Sstevel@tonic-gate 				    total_size, maxblk, &count);
13210f1702c5SYu Xiangning 			} else {
13227c478bd9Sstevel@tonic-gate 				error = sendvec_chunk(fp, &fileoff,
13237c478bd9Sstevel@tonic-gate 				    sfv, copy_cnt, &count);
13247c478bd9Sstevel@tonic-gate 			}
13257c478bd9Sstevel@tonic-gate 		} else {
13267c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_type == VREG);
13277c478bd9Sstevel@tonic-gate 			error = sendvec_chunk(fp, &fileoff, sfv, copy_cnt,
13287c478bd9Sstevel@tonic-gate 			    &count);
13297c478bd9Sstevel@tonic-gate 		}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
13337c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32)
13347c478bd9Sstevel@tonic-gate 		copy_vec = (const struct sendfilevec *)((char *)copy_vec +
13357c478bd9Sstevel@tonic-gate 		    (copy_cnt * sizeof (ksendfilevec32_t)));
13367c478bd9Sstevel@tonic-gate 	else
13377c478bd9Sstevel@tonic-gate #endif
13387c478bd9Sstevel@tonic-gate 		copy_vec += copy_cnt;
13397c478bd9Sstevel@tonic-gate 		sfvcnt -= copy_cnt;
1340311e3d4cSgeorges 
1341311e3d4cSgeorges 	/* Process all vector members up to first error */
1342311e3d4cSgeorges 	} while ((sfvcnt > 0) && first_vector_error == 0 && error == 0);
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG)
13457c478bd9Sstevel@tonic-gate 		fp->f_offset += count;
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
13507c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32) {
13517c478bd9Sstevel@tonic-gate 		ssize32_t count32 = (ssize32_t)count;
13527c478bd9Sstevel@tonic-gate 		if (copyout(&count32, xferred, sizeof (count32)))
13537c478bd9Sstevel@tonic-gate 			error = EFAULT;
13547c478bd9Sstevel@tonic-gate 		releasef(fildes);
13557c478bd9Sstevel@tonic-gate 		if (error != 0)
13567c478bd9Sstevel@tonic-gate 			return (set_errno(error));
1357311e3d4cSgeorges 		if (first_vector_error != 0)
1358311e3d4cSgeorges 			return (set_errno(first_vector_error));
13597c478bd9Sstevel@tonic-gate 		return (count32);
13607c478bd9Sstevel@tonic-gate 	}
13617c478bd9Sstevel@tonic-gate #endif
13627c478bd9Sstevel@tonic-gate 	if (copyout(&count, xferred, sizeof (count)))
13637c478bd9Sstevel@tonic-gate 		error = EFAULT;
13647c478bd9Sstevel@tonic-gate 	releasef(fildes);
13657c478bd9Sstevel@tonic-gate 	if (error != 0)
13667c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1367311e3d4cSgeorges 	if (first_vector_error != 0)
1368311e3d4cSgeorges 		return (set_errno(first_vector_error));
13697c478bd9Sstevel@tonic-gate 	return (count);
13707c478bd9Sstevel@tonic-gate err:
13717c478bd9Sstevel@tonic-gate 	ASSERT(error != 0);
13727c478bd9Sstevel@tonic-gate 	releasef(fildes);
13737c478bd9Sstevel@tonic-gate 	return (set_errno(error));
13747c478bd9Sstevel@tonic-gate }
1375