xref: /titanic_52/usr/src/uts/common/syscall/rw.c (revision 4d86dd30d2f1154ded30a45ac7c3a29f7febabac)
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
513506d1eSmaybee  * Common Development and Distribution License (the "License").
613506d1eSmaybee  * 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  */
21*4d86dd30Sraf 
227c478bd9Sstevel@tonic-gate /*
23*4d86dd30Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/param.h>
387c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/inttypes.h>
417c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
427c478bd9Sstevel@tonic-gate #include <sys/cred.h>
437c478bd9Sstevel@tonic-gate #include <sys/user.h>
447c478bd9Sstevel@tonic-gate #include <sys/systm.h>
457c478bd9Sstevel@tonic-gate #include <sys/errno.h>
467c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
477c478bd9Sstevel@tonic-gate #include <sys/file.h>
487c478bd9Sstevel@tonic-gate #include <sys/proc.h>
497c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
507c478bd9Sstevel@tonic-gate #include <sys/uio.h>
517c478bd9Sstevel@tonic-gate #include <sys/debug.h>
527c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
537c478bd9Sstevel@tonic-gate #include <sys/nbmlock.h>
547c478bd9Sstevel@tonic-gate 
5513506d1eSmaybee #define	COPYOUT_MAX_CACHE	(1<<17)		/* 128K */
567c478bd9Sstevel@tonic-gate 
5713506d1eSmaybee size_t copyout_max_cached = COPYOUT_MAX_CACHE;	/* global so it's patchable */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * read, write, pread, pwrite, readv, and writev syscalls.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * 64-bit open:	all open's are large file opens.
637c478bd9Sstevel@tonic-gate  * Large Files: the behaviour of read depends on whether the fd
647c478bd9Sstevel@tonic-gate  *		corresponds to large open or not.
657c478bd9Sstevel@tonic-gate  * 32-bit open:	FOFFMAX flag not set.
667c478bd9Sstevel@tonic-gate  *		read until MAXOFF32_T - 1 and read at MAXOFF32_T returns
677c478bd9Sstevel@tonic-gate  *		EOVERFLOW if count is non-zero and if size of file
687c478bd9Sstevel@tonic-gate  *		is > MAXOFF32_T. If size of file is <= MAXOFF32_T read
697c478bd9Sstevel@tonic-gate  *		at >= MAXOFF32_T returns EOF.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * Native system call
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate ssize_t
767c478bd9Sstevel@tonic-gate read(int fdes, void *cbuf, size_t count)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	struct uio auio;
797c478bd9Sstevel@tonic-gate 	struct iovec aiov;
807c478bd9Sstevel@tonic-gate 	file_t *fp;
817c478bd9Sstevel@tonic-gate 	register vnode_t *vp;
827c478bd9Sstevel@tonic-gate 	struct cpu *cp;
837c478bd9Sstevel@tonic-gate 	int fflag, ioflag, rwflag;
847c478bd9Sstevel@tonic-gate 	ssize_t cnt, bcount;
857c478bd9Sstevel@tonic-gate 	int error = 0;
867c478bd9Sstevel@tonic-gate 	u_offset_t fileoff;
877c478bd9Sstevel@tonic-gate 	int in_crit = 0;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if ((cnt = (ssize_t)count) < 0)
907c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
917c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
927c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
937c478bd9Sstevel@tonic-gate 	if (((fflag = fp->f_flag) & FREAD) == 0) {
947c478bd9Sstevel@tonic-gate 		error = EBADF;
957c478bd9Sstevel@tonic-gate 		goto out;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG && cnt == 0) {
1007c478bd9Sstevel@tonic-gate 		goto out;
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	rwflag = 0;
1047c478bd9Sstevel@tonic-gate 	aiov.iov_base = cbuf;
1057c478bd9Sstevel@tonic-gate 	aiov.iov_len = cnt;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	/*
1087c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
1097c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with write() calls.
1107c478bd9Sstevel@tonic-gate 	 */
1117c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
1127c478bd9Sstevel@tonic-gate 		int svmand;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
1157c478bd9Sstevel@tonic-gate 		in_crit = 1;
1167c478bd9Sstevel@tonic-gate 		error = nbl_svmand(vp, fp->f_cred, &svmand);
1177c478bd9Sstevel@tonic-gate 		if (error != 0)
1187c478bd9Sstevel@tonic-gate 			goto out;
119da6c28aaSamw 		if (nbl_conflict(vp, NBL_READ, fp->f_offset, cnt, svmand,
120da6c28aaSamw 		    NULL)) {
1217c478bd9Sstevel@tonic-gate 			error = EACCES;
1227c478bd9Sstevel@tonic-gate 			goto out;
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	/*
1297c478bd9Sstevel@tonic-gate 	 * We do the following checks inside VOP_RWLOCK so as to
1307c478bd9Sstevel@tonic-gate 	 * prevent file size from changing while these checks are
1317c478bd9Sstevel@tonic-gate 	 * being done. Also, we load fp's offset to the local
1327c478bd9Sstevel@tonic-gate 	 * variable fileoff because we can have a parallel lseek
1337c478bd9Sstevel@tonic-gate 	 * going on (f_offset is not protected by any lock) which
1347c478bd9Sstevel@tonic-gate 	 * could change f_offset. We need to see the value only
1357c478bd9Sstevel@tonic-gate 	 * once here and take a decision. Seeing it more than once
1367c478bd9Sstevel@tonic-gate 	 * can lead to incorrect functionality.
1377c478bd9Sstevel@tonic-gate 	 */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	fileoff = (u_offset_t)fp->f_offset;
1407c478bd9Sstevel@tonic-gate 	if (fileoff >= OFFSET_MAX(fp) && (vp->v_type == VREG)) {
1417c478bd9Sstevel@tonic-gate 		struct vattr va;
1427c478bd9Sstevel@tonic-gate 		va.va_mask = AT_SIZE;
143da6c28aaSamw 		if ((error = VOP_GETATTR(vp, &va, 0, fp->f_cred, NULL)))  {
1447c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, rwflag, NULL);
1457c478bd9Sstevel@tonic-gate 			goto out;
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 		if (fileoff >= va.va_size) {
1487c478bd9Sstevel@tonic-gate 			cnt = 0;
1497c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, rwflag, NULL);
1507c478bd9Sstevel@tonic-gate 			goto out;
1517c478bd9Sstevel@tonic-gate 		} else {
1527c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
1537c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, rwflag, NULL);
1547c478bd9Sstevel@tonic-gate 			goto out;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 	if ((vp->v_type == VREG) &&
1587c478bd9Sstevel@tonic-gate 	    (fileoff + cnt > OFFSET_MAX(fp))) {
1597c478bd9Sstevel@tonic-gate 		cnt = (ssize_t)(OFFSET_MAX(fp) - fileoff);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 	auio.uio_loffset = fileoff;
1627c478bd9Sstevel@tonic-gate 	auio.uio_iov = &aiov;
1637c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
1647c478bd9Sstevel@tonic-gate 	auio.uio_resid = bcount = cnt;
1657c478bd9Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
1667c478bd9Sstevel@tonic-gate 	auio.uio_llimit = MAXOFFSET_T;
1677c478bd9Sstevel@tonic-gate 	auio.uio_fmode = fflag;
1687c478bd9Sstevel@tonic-gate 	/*
1697c478bd9Sstevel@tonic-gate 	 * Only use bypass caches when the count is large enough
1707c478bd9Sstevel@tonic-gate 	 */
17113506d1eSmaybee 	if (bcount <= copyout_max_cached)
1727c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_CACHED;
1737c478bd9Sstevel@tonic-gate 	else
1747c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_DEFAULT;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/* If read sync is not asked for, filter sync flags */
1797c478bd9Sstevel@tonic-gate 	if ((ioflag & FRSYNC) == 0)
1807c478bd9Sstevel@tonic-gate 		ioflag &= ~(FSYNC|FDSYNC);
1817c478bd9Sstevel@tonic-gate 	error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
1827c478bd9Sstevel@tonic-gate 	cnt -= auio.uio_resid;
1837c478bd9Sstevel@tonic-gate 	CPU_STATS_ENTER_K();
1847c478bd9Sstevel@tonic-gate 	cp = CPU;
1857c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, sysread, 1);
1867c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, readch, (ulong_t)cnt);
1877c478bd9Sstevel@tonic-gate 	CPU_STATS_EXIT_K();
1887c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (vp->v_type == VFIFO)	/* Backward compatibility */
1917c478bd9Sstevel@tonic-gate 		fp->f_offset = cnt;
1927c478bd9Sstevel@tonic-gate 	else if (((fp->f_flag & FAPPEND) == 0) ||
1937c478bd9Sstevel@tonic-gate 	    (vp->v_type != VREG) || (bcount != 0))	/* POSIX */
1947c478bd9Sstevel@tonic-gate 		fp->f_offset = auio.uio_loffset;
1957c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (error == EINTR && cnt != 0)
1987c478bd9Sstevel@tonic-gate 		error = 0;
1997c478bd9Sstevel@tonic-gate out:
2007c478bd9Sstevel@tonic-gate 	if (in_crit)
2017c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
2027c478bd9Sstevel@tonic-gate 	releasef(fdes);
2037c478bd9Sstevel@tonic-gate 	if (error)
2047c478bd9Sstevel@tonic-gate 		return (set_errno(error));
2057c478bd9Sstevel@tonic-gate 	return (cnt);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * Native system call
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate ssize_t
2127c478bd9Sstevel@tonic-gate write(int fdes, void *cbuf, size_t count)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	struct uio auio;
2157c478bd9Sstevel@tonic-gate 	struct iovec aiov;
2167c478bd9Sstevel@tonic-gate 	file_t *fp;
2177c478bd9Sstevel@tonic-gate 	register vnode_t *vp;
2187c478bd9Sstevel@tonic-gate 	struct cpu *cp;
2197c478bd9Sstevel@tonic-gate 	int fflag, ioflag, rwflag;
2207c478bd9Sstevel@tonic-gate 	ssize_t cnt, bcount;
2217c478bd9Sstevel@tonic-gate 	int error = 0;
2227c478bd9Sstevel@tonic-gate 	u_offset_t fileoff;
2237c478bd9Sstevel@tonic-gate 	int in_crit = 0;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if ((cnt = (ssize_t)count) < 0)
2267c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2277c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
2287c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
2297c478bd9Sstevel@tonic-gate 	if (((fflag = fp->f_flag) & FWRITE) == 0) {
2307c478bd9Sstevel@tonic-gate 		error = EBADF;
2317c478bd9Sstevel@tonic-gate 		goto out;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG && cnt == 0) {
2367c478bd9Sstevel@tonic-gate 		goto out;
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	rwflag = 1;
2407c478bd9Sstevel@tonic-gate 	aiov.iov_base = cbuf;
2417c478bd9Sstevel@tonic-gate 	aiov.iov_len = cnt;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/*
2447c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
2457c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with ufs.
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
2487c478bd9Sstevel@tonic-gate 		int svmand;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
2517c478bd9Sstevel@tonic-gate 		in_crit = 1;
2527c478bd9Sstevel@tonic-gate 		error = nbl_svmand(vp, fp->f_cred, &svmand);
2537c478bd9Sstevel@tonic-gate 		if (error != 0)
2547c478bd9Sstevel@tonic-gate 			goto out;
255da6c28aaSamw 		if (nbl_conflict(vp, NBL_WRITE, fp->f_offset, cnt, svmand,
256da6c28aaSamw 		    NULL)) {
2577c478bd9Sstevel@tonic-gate 			error = EACCES;
2587c478bd9Sstevel@tonic-gate 			goto out;
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	fileoff = fp->f_offset;
2657c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG) {
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		/*
2687c478bd9Sstevel@tonic-gate 		 * We raise psignal if write for >0 bytes causes
2697c478bd9Sstevel@tonic-gate 		 * it to exceed the ulimit.
2707c478bd9Sstevel@tonic-gate 		 */
2717c478bd9Sstevel@tonic-gate 		if (fileoff >= curproc->p_fsz_ctl) {
2727c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, rwflag, NULL);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 			mutex_enter(&curproc->p_lock);
2757c478bd9Sstevel@tonic-gate 			(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
2767c478bd9Sstevel@tonic-gate 			    curproc->p_rctls, curproc, RCA_UNSAFE_SIGINFO);
2777c478bd9Sstevel@tonic-gate 			mutex_exit(&curproc->p_lock);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 			error = EFBIG;
2807c478bd9Sstevel@tonic-gate 			goto out;
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate 		/*
2837c478bd9Sstevel@tonic-gate 		 * We return EFBIG if write is done at an offset
2847c478bd9Sstevel@tonic-gate 		 * greater than the offset maximum for this file structure.
2857c478bd9Sstevel@tonic-gate 		 */
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		if (fileoff >= OFFSET_MAX(fp)) {
2887c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, rwflag, NULL);
2897c478bd9Sstevel@tonic-gate 			error = EFBIG;
2907c478bd9Sstevel@tonic-gate 			goto out;
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 		/*
2937c478bd9Sstevel@tonic-gate 		 * Limit the bytes to be written  upto offset maximum for
2947c478bd9Sstevel@tonic-gate 		 * this open file structure.
2957c478bd9Sstevel@tonic-gate 		 */
2967c478bd9Sstevel@tonic-gate 		if (fileoff + cnt > OFFSET_MAX(fp))
2977c478bd9Sstevel@tonic-gate 			cnt = (ssize_t)(OFFSET_MAX(fp) - fileoff);
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 	auio.uio_loffset = fileoff;
3007c478bd9Sstevel@tonic-gate 	auio.uio_iov = &aiov;
3017c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
3027c478bd9Sstevel@tonic-gate 	auio.uio_resid = bcount = cnt;
3037c478bd9Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
3047c478bd9Sstevel@tonic-gate 	auio.uio_llimit = curproc->p_fsz_ctl;
3057c478bd9Sstevel@tonic-gate 	auio.uio_fmode = fflag;
3067c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_DEFAULT;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	error = VOP_WRITE(vp, &auio, ioflag, fp->f_cred, NULL);
3117c478bd9Sstevel@tonic-gate 	cnt -= auio.uio_resid;
3127c478bd9Sstevel@tonic-gate 	CPU_STATS_ENTER_K();
3137c478bd9Sstevel@tonic-gate 	cp = CPU;
3147c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, syswrite, 1);
3157c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, writech, (ulong_t)cnt);
3167c478bd9Sstevel@tonic-gate 	CPU_STATS_EXIT_K();
3177c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_ru.ioch += (ulong_t)cnt;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (vp->v_type == VFIFO)	/* Backward compatibility */
3207c478bd9Sstevel@tonic-gate 		fp->f_offset = cnt;
3217c478bd9Sstevel@tonic-gate 	else if (((fp->f_flag & FAPPEND) == 0) ||
3227c478bd9Sstevel@tonic-gate 	    (vp->v_type != VREG) || (bcount != 0))	/* POSIX */
3237c478bd9Sstevel@tonic-gate 		fp->f_offset = auio.uio_loffset;
3247c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (error == EINTR && cnt != 0)
3277c478bd9Sstevel@tonic-gate 		error = 0;
3287c478bd9Sstevel@tonic-gate out:
3297c478bd9Sstevel@tonic-gate 	if (in_crit)
3307c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
3317c478bd9Sstevel@tonic-gate 	releasef(fdes);
3327c478bd9Sstevel@tonic-gate 	if (error)
3337c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3347c478bd9Sstevel@tonic-gate 	return (cnt);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate ssize_t
3387c478bd9Sstevel@tonic-gate pread(int fdes, void *cbuf, size_t count, off_t offset)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	struct uio auio;
3417c478bd9Sstevel@tonic-gate 	struct iovec aiov;
3427c478bd9Sstevel@tonic-gate 	file_t *fp;
3437c478bd9Sstevel@tonic-gate 	register vnode_t *vp;
3447c478bd9Sstevel@tonic-gate 	struct cpu *cp;
3457c478bd9Sstevel@tonic-gate 	int fflag, ioflag, rwflag;
3467c478bd9Sstevel@tonic-gate 	ssize_t bcount;
3477c478bd9Sstevel@tonic-gate 	int error = 0;
3487c478bd9Sstevel@tonic-gate 	u_offset_t fileoff = (u_offset_t)(ulong_t)offset;
3497c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
3507c478bd9Sstevel@tonic-gate 	u_offset_t maxoff = get_udatamodel() == DATAMODEL_ILP32 ?
3517c478bd9Sstevel@tonic-gate 	    MAXOFF32_T : MAXOFFSET_T;
3527c478bd9Sstevel@tonic-gate #else
3537c478bd9Sstevel@tonic-gate 	const u_offset_t maxoff = MAXOFF32_T;
3547c478bd9Sstevel@tonic-gate #endif
3557c478bd9Sstevel@tonic-gate 	int in_crit = 0;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if ((bcount = (ssize_t)count) < 0)
3587c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
3617c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
3627c478bd9Sstevel@tonic-gate 	if (((fflag = fp->f_flag) & (FREAD)) == 0) {
3637c478bd9Sstevel@tonic-gate 		error = EBADF;
3647c478bd9Sstevel@tonic-gate 		goto out;
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	rwflag = 0;
3687c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG) {
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		if (bcount == 0)
3737c478bd9Sstevel@tonic-gate 			goto out;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		/*
3767c478bd9Sstevel@tonic-gate 		 * Return EINVAL if an invalid offset comes to pread.
3777c478bd9Sstevel@tonic-gate 		 * Negative offset from user will cause this error.
3787c478bd9Sstevel@tonic-gate 		 */
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 		if (fileoff > maxoff) {
3817c478bd9Sstevel@tonic-gate 			error = EINVAL;
3827c478bd9Sstevel@tonic-gate 			goto out;
3837c478bd9Sstevel@tonic-gate 		}
3847c478bd9Sstevel@tonic-gate 		/*
3857c478bd9Sstevel@tonic-gate 		 * Limit offset such that we don't read or write
3867c478bd9Sstevel@tonic-gate 		 * a file beyond the maximum offset representable in
3877c478bd9Sstevel@tonic-gate 		 * an off_t structure.
3887c478bd9Sstevel@tonic-gate 		 */
3897c478bd9Sstevel@tonic-gate 		if (fileoff + bcount > maxoff)
3907c478bd9Sstevel@tonic-gate 			bcount = (ssize_t)((offset_t)maxoff - fileoff);
3917c478bd9Sstevel@tonic-gate 	} else if (vp->v_type == VFIFO) {
3927c478bd9Sstevel@tonic-gate 		error = ESPIPE;
3937c478bd9Sstevel@tonic-gate 		goto out;
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
3987c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with ufs.
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
4017c478bd9Sstevel@tonic-gate 		int svmand;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
4047c478bd9Sstevel@tonic-gate 		in_crit = 1;
4057c478bd9Sstevel@tonic-gate 		error = nbl_svmand(vp, fp->f_cred, &svmand);
4067c478bd9Sstevel@tonic-gate 		if (error != 0)
4077c478bd9Sstevel@tonic-gate 			goto out;
408da6c28aaSamw 		if (nbl_conflict(vp, NBL_READ, fileoff, bcount, svmand,
409da6c28aaSamw 		    NULL)) {
4107c478bd9Sstevel@tonic-gate 			error = EACCES;
4117c478bd9Sstevel@tonic-gate 			goto out;
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	aiov.iov_base = cbuf;
4167c478bd9Sstevel@tonic-gate 	aiov.iov_len = bcount;
4177c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
4187c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG && fileoff == (u_offset_t)maxoff) {
4197c478bd9Sstevel@tonic-gate 		struct vattr va;
4207c478bd9Sstevel@tonic-gate 		va.va_mask = AT_SIZE;
421da6c28aaSamw 		if ((error = VOP_GETATTR(vp, &va, 0, fp->f_cred, NULL))) {
4227c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, rwflag, NULL);
4237c478bd9Sstevel@tonic-gate 			goto out;
4247c478bd9Sstevel@tonic-gate 		}
4257c478bd9Sstevel@tonic-gate 		VOP_RWUNLOCK(vp, rwflag, NULL);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 		/*
4287c478bd9Sstevel@tonic-gate 		 * We have to return EOF if fileoff is >= file size.
4297c478bd9Sstevel@tonic-gate 		 */
4307c478bd9Sstevel@tonic-gate 		if (fileoff >= va.va_size) {
4317c478bd9Sstevel@tonic-gate 			bcount = 0;
4327c478bd9Sstevel@tonic-gate 			goto out;
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		/*
4367c478bd9Sstevel@tonic-gate 		 * File is greater than or equal to maxoff and therefore
4377c478bd9Sstevel@tonic-gate 		 * we return EOVERFLOW.
4387c478bd9Sstevel@tonic-gate 		 */
4397c478bd9Sstevel@tonic-gate 		error = EOVERFLOW;
4407c478bd9Sstevel@tonic-gate 		goto out;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 	auio.uio_loffset = fileoff;
4437c478bd9Sstevel@tonic-gate 	auio.uio_iov = &aiov;
4447c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
4457c478bd9Sstevel@tonic-gate 	auio.uio_resid = bcount;
4467c478bd9Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
4477c478bd9Sstevel@tonic-gate 	auio.uio_llimit = MAXOFFSET_T;
4487c478bd9Sstevel@tonic-gate 	auio.uio_fmode = fflag;
4497c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	/* If read sync is not asked for, filter sync flags */
4547c478bd9Sstevel@tonic-gate 	if ((ioflag & FRSYNC) == 0)
4557c478bd9Sstevel@tonic-gate 		ioflag &= ~(FSYNC|FDSYNC);
4567c478bd9Sstevel@tonic-gate 	error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
4577c478bd9Sstevel@tonic-gate 	bcount -= auio.uio_resid;
4587c478bd9Sstevel@tonic-gate 	CPU_STATS_ENTER_K();
4597c478bd9Sstevel@tonic-gate 	cp = CPU;
4607c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, sysread, 1);
4617c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, readch, (ulong_t)bcount);
4627c478bd9Sstevel@tonic-gate 	CPU_STATS_EXIT_K();
4637c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_ru.ioch += (ulong_t)bcount;
4647c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	if (error == EINTR && bcount != 0)
4677c478bd9Sstevel@tonic-gate 		error = 0;
4687c478bd9Sstevel@tonic-gate out:
4697c478bd9Sstevel@tonic-gate 	if (in_crit)
4707c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
4717c478bd9Sstevel@tonic-gate 	releasef(fdes);
4727c478bd9Sstevel@tonic-gate 	if (error)
4737c478bd9Sstevel@tonic-gate 		return (set_errno(error));
4747c478bd9Sstevel@tonic-gate 	return (bcount);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate ssize_t
4787c478bd9Sstevel@tonic-gate pwrite(int fdes, void *cbuf, size_t count, off_t offset)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	struct uio auio;
4817c478bd9Sstevel@tonic-gate 	struct iovec aiov;
4827c478bd9Sstevel@tonic-gate 	file_t *fp;
4837c478bd9Sstevel@tonic-gate 	register vnode_t *vp;
4847c478bd9Sstevel@tonic-gate 	struct cpu *cp;
4857c478bd9Sstevel@tonic-gate 	int fflag, ioflag, rwflag;
4867c478bd9Sstevel@tonic-gate 	ssize_t bcount;
4877c478bd9Sstevel@tonic-gate 	int error = 0;
4887c478bd9Sstevel@tonic-gate 	u_offset_t fileoff = (u_offset_t)(ulong_t)offset;
4897c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4907c478bd9Sstevel@tonic-gate 	u_offset_t maxoff = get_udatamodel() == DATAMODEL_ILP32 ?
4917c478bd9Sstevel@tonic-gate 	    MAXOFF32_T : MAXOFFSET_T;
4927c478bd9Sstevel@tonic-gate #else
4937c478bd9Sstevel@tonic-gate 	const u_offset_t maxoff = MAXOFF32_T;
4947c478bd9Sstevel@tonic-gate #endif
4957c478bd9Sstevel@tonic-gate 	int in_crit = 0;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if ((bcount = (ssize_t)count) < 0)
4987c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
4997c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
5007c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
5017c478bd9Sstevel@tonic-gate 	if (((fflag = fp->f_flag) & (FWRITE)) == 0) {
5027c478bd9Sstevel@tonic-gate 		error = EBADF;
5037c478bd9Sstevel@tonic-gate 		goto out;
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	rwflag = 1;
5077c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG) {
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		if (bcount == 0)
5127c478bd9Sstevel@tonic-gate 			goto out;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		/*
5157c478bd9Sstevel@tonic-gate 		 * return EINVAL for offsets that cannot be
5167c478bd9Sstevel@tonic-gate 		 * represented in an off_t.
5177c478bd9Sstevel@tonic-gate 		 */
5187c478bd9Sstevel@tonic-gate 		if (fileoff > maxoff) {
5197c478bd9Sstevel@tonic-gate 			error = EINVAL;
5207c478bd9Sstevel@tonic-gate 			goto out;
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 		/*
5237c478bd9Sstevel@tonic-gate 		 * Take appropriate action if we are trying to write above the
5247c478bd9Sstevel@tonic-gate 		 * resource limit.
5257c478bd9Sstevel@tonic-gate 		 */
5267c478bd9Sstevel@tonic-gate 		if (fileoff >= curproc->p_fsz_ctl) {
5277c478bd9Sstevel@tonic-gate 			mutex_enter(&curproc->p_lock);
5287c478bd9Sstevel@tonic-gate 			(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
5297c478bd9Sstevel@tonic-gate 			    curproc->p_rctls, curproc, RCA_UNSAFE_SIGINFO);
5307c478bd9Sstevel@tonic-gate 			mutex_exit(&curproc->p_lock);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 			error = EFBIG;
5337c478bd9Sstevel@tonic-gate 			goto out;
5347c478bd9Sstevel@tonic-gate 		}
5357c478bd9Sstevel@tonic-gate 		/*
5367c478bd9Sstevel@tonic-gate 		 * Don't allow pwrite to cause file sizes to exceed
5377c478bd9Sstevel@tonic-gate 		 * maxoff.
5387c478bd9Sstevel@tonic-gate 		 */
5397c478bd9Sstevel@tonic-gate 		if (fileoff == maxoff) {
5407c478bd9Sstevel@tonic-gate 			error = EFBIG;
5417c478bd9Sstevel@tonic-gate 			goto out;
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 		if (fileoff + count > maxoff)
5447c478bd9Sstevel@tonic-gate 			bcount = (ssize_t)((u_offset_t)maxoff - fileoff);
5457c478bd9Sstevel@tonic-gate 	} else if (vp->v_type == VFIFO) {
5467c478bd9Sstevel@tonic-gate 		error = ESPIPE;
5477c478bd9Sstevel@tonic-gate 		goto out;
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	/*
5517c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
5527c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with ufs.
5537c478bd9Sstevel@tonic-gate 	 */
5547c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
5557c478bd9Sstevel@tonic-gate 		int svmand;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
5587c478bd9Sstevel@tonic-gate 		in_crit = 1;
5597c478bd9Sstevel@tonic-gate 		error = nbl_svmand(vp, fp->f_cred, &svmand);
5607c478bd9Sstevel@tonic-gate 		if (error != 0)
5617c478bd9Sstevel@tonic-gate 			goto out;
562da6c28aaSamw 		if (nbl_conflict(vp, NBL_WRITE, fileoff, bcount, svmand,
563da6c28aaSamw 		    NULL)) {
5647c478bd9Sstevel@tonic-gate 			error = EACCES;
5657c478bd9Sstevel@tonic-gate 			goto out;
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	aiov.iov_base = cbuf;
5707c478bd9Sstevel@tonic-gate 	aiov.iov_len = bcount;
5717c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
5727c478bd9Sstevel@tonic-gate 	auio.uio_loffset = fileoff;
5737c478bd9Sstevel@tonic-gate 	auio.uio_iov = &aiov;
5747c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
5757c478bd9Sstevel@tonic-gate 	auio.uio_resid = bcount;
5767c478bd9Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
5777c478bd9Sstevel@tonic-gate 	auio.uio_llimit = curproc->p_fsz_ctl;
5787c478bd9Sstevel@tonic-gate 	auio.uio_fmode = fflag;
5797c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
5807c478bd9Sstevel@tonic-gate 
581*4d86dd30Sraf 	/*
582*4d86dd30Sraf 	 * The SUSv4 POSIX specification states:
583*4d86dd30Sraf 	 *	The pwrite() function shall be equivalent to write(), except
584*4d86dd30Sraf 	 *	that it writes into a given position and does not change
585*4d86dd30Sraf 	 *	the file offset (regardless of whether O_APPEND is set).
586*4d86dd30Sraf 	 * To make this be true, we omit the FAPPEND flag from ioflag.
587*4d86dd30Sraf 	 */
588*4d86dd30Sraf 	ioflag = auio.uio_fmode & (FSYNC|FDSYNC|FRSYNC);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	error = VOP_WRITE(vp, &auio, ioflag, fp->f_cred, NULL);
5917c478bd9Sstevel@tonic-gate 	bcount -= auio.uio_resid;
5927c478bd9Sstevel@tonic-gate 	CPU_STATS_ENTER_K();
5937c478bd9Sstevel@tonic-gate 	cp = CPU;
5947c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, syswrite, 1);
5957c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, writech, (ulong_t)bcount);
5967c478bd9Sstevel@tonic-gate 	CPU_STATS_EXIT_K();
5977c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_ru.ioch += (ulong_t)bcount;
5987c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (error == EINTR && bcount != 0)
6017c478bd9Sstevel@tonic-gate 		error = 0;
6027c478bd9Sstevel@tonic-gate out:
6037c478bd9Sstevel@tonic-gate 	if (in_crit)
6047c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
6057c478bd9Sstevel@tonic-gate 	releasef(fdes);
6067c478bd9Sstevel@tonic-gate 	if (error)
6077c478bd9Sstevel@tonic-gate 		return (set_errno(error));
6087c478bd9Sstevel@tonic-gate 	return (bcount);
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate  * XXX -- The SVID refers to IOV_MAX, but doesn't define it.  Grrrr....
6137c478bd9Sstevel@tonic-gate  * XXX -- However, SVVS expects readv() and writev() to fail if
6147c478bd9Sstevel@tonic-gate  * XXX -- iovcnt > 16 (yes, it's hard-coded in the SVVS source),
6157c478bd9Sstevel@tonic-gate  * XXX -- so I guess that's the "interface".
6167c478bd9Sstevel@tonic-gate  */
6177c478bd9Sstevel@tonic-gate #define	DEF_IOV_MAX	16
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate ssize_t
6207c478bd9Sstevel@tonic-gate readv(int fdes, struct iovec *iovp, int iovcnt)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	struct uio auio;
6237c478bd9Sstevel@tonic-gate 	struct iovec aiov[DEF_IOV_MAX];
6247c478bd9Sstevel@tonic-gate 	file_t *fp;
6257c478bd9Sstevel@tonic-gate 	register vnode_t *vp;
6267c478bd9Sstevel@tonic-gate 	struct cpu *cp;
6277c478bd9Sstevel@tonic-gate 	int fflag, ioflag, rwflag;
6287c478bd9Sstevel@tonic-gate 	ssize_t count, bcount;
6297c478bd9Sstevel@tonic-gate 	int error = 0;
6307c478bd9Sstevel@tonic-gate 	int i;
6317c478bd9Sstevel@tonic-gate 	u_offset_t fileoff;
6327c478bd9Sstevel@tonic-gate 	int in_crit = 0;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	if (iovcnt <= 0 || iovcnt > DEF_IOV_MAX)
6357c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
6387c478bd9Sstevel@tonic-gate 	/*
6397c478bd9Sstevel@tonic-gate 	 * 32-bit callers need to have their iovec expanded,
6407c478bd9Sstevel@tonic-gate 	 * while ensuring that they can't move more than 2Gbytes
6417c478bd9Sstevel@tonic-gate 	 * of data in a single call.
6427c478bd9Sstevel@tonic-gate 	 */
6437c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32) {
6447c478bd9Sstevel@tonic-gate 		struct iovec32 aiov32[DEF_IOV_MAX];
6457c478bd9Sstevel@tonic-gate 		ssize32_t count32;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		if (copyin(iovp, aiov32, iovcnt * sizeof (struct iovec32)))
6487c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		count32 = 0;
6517c478bd9Sstevel@tonic-gate 		for (i = 0; i < iovcnt; i++) {
6527c478bd9Sstevel@tonic-gate 			ssize32_t iovlen32 = aiov32[i].iov_len;
6537c478bd9Sstevel@tonic-gate 			count32 += iovlen32;
6547c478bd9Sstevel@tonic-gate 			if (iovlen32 < 0 || count32 < 0)
6557c478bd9Sstevel@tonic-gate 				return (set_errno(EINVAL));
6567c478bd9Sstevel@tonic-gate 			aiov[i].iov_len = iovlen32;
6577c478bd9Sstevel@tonic-gate 			aiov[i].iov_base =
6587c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)aiov32[i].iov_base;
6597c478bd9Sstevel@tonic-gate 		}
6607c478bd9Sstevel@tonic-gate 	} else
6617c478bd9Sstevel@tonic-gate #endif
6627c478bd9Sstevel@tonic-gate 	if (copyin(iovp, aiov, iovcnt * sizeof (struct iovec)))
6637c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	count = 0;
6667c478bd9Sstevel@tonic-gate 	for (i = 0; i < iovcnt; i++) {
6677c478bd9Sstevel@tonic-gate 		ssize_t iovlen = aiov[i].iov_len;
6687c478bd9Sstevel@tonic-gate 		count += iovlen;
6697c478bd9Sstevel@tonic-gate 		if (iovlen < 0 || count < 0)
6707c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
6737c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
6747c478bd9Sstevel@tonic-gate 	if (((fflag = fp->f_flag) & FREAD) == 0) {
6757c478bd9Sstevel@tonic-gate 		error = EBADF;
6767c478bd9Sstevel@tonic-gate 		goto out;
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
6797c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG && count == 0) {
6807c478bd9Sstevel@tonic-gate 		goto out;
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	rwflag = 0;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	/*
6867c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
6877c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with ufs.
6887c478bd9Sstevel@tonic-gate 	 */
6897c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
6907c478bd9Sstevel@tonic-gate 		int svmand;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
6937c478bd9Sstevel@tonic-gate 		in_crit = 1;
6947c478bd9Sstevel@tonic-gate 		error = nbl_svmand(vp, fp->f_cred, &svmand);
6957c478bd9Sstevel@tonic-gate 		if (error != 0)
6967c478bd9Sstevel@tonic-gate 			goto out;
697da6c28aaSamw 		if (nbl_conflict(vp, NBL_READ, fp->f_offset, count, svmand,
698da6c28aaSamw 		    NULL)) {
6997c478bd9Sstevel@tonic-gate 			error = EACCES;
7007c478bd9Sstevel@tonic-gate 			goto out;
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
7057c478bd9Sstevel@tonic-gate 	fileoff = fp->f_offset;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	/*
7087c478bd9Sstevel@tonic-gate 	 * Behaviour is same as read. Please see comments in read.
7097c478bd9Sstevel@tonic-gate 	 */
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	if ((vp->v_type == VREG) && (fileoff >= OFFSET_MAX(fp))) {
7127c478bd9Sstevel@tonic-gate 		struct vattr va;
7137c478bd9Sstevel@tonic-gate 		va.va_mask = AT_SIZE;
714da6c28aaSamw 		if ((error = VOP_GETATTR(vp, &va, 0, fp->f_cred, NULL)))  {
7157c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, rwflag, NULL);
7167c478bd9Sstevel@tonic-gate 			goto out;
7177c478bd9Sstevel@tonic-gate 		}
7187c478bd9Sstevel@tonic-gate 		if (fileoff >= va.va_size) {
7197c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, rwflag, NULL);
7207c478bd9Sstevel@tonic-gate 			count = 0;
7217c478bd9Sstevel@tonic-gate 			goto out;
7227c478bd9Sstevel@tonic-gate 		} else {
7237c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, rwflag, NULL);
7247c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
7257c478bd9Sstevel@tonic-gate 			goto out;
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 	if ((vp->v_type == VREG) && (fileoff + count > OFFSET_MAX(fp))) {
7297c478bd9Sstevel@tonic-gate 		count = (ssize_t)(OFFSET_MAX(fp) - fileoff);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 	auio.uio_loffset = fileoff;
7327c478bd9Sstevel@tonic-gate 	auio.uio_iov = aiov;
7337c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = iovcnt;
7347c478bd9Sstevel@tonic-gate 	auio.uio_resid = bcount = count;
7357c478bd9Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
7367c478bd9Sstevel@tonic-gate 	auio.uio_llimit = MAXOFFSET_T;
7377c478bd9Sstevel@tonic-gate 	auio.uio_fmode = fflag;
73813506d1eSmaybee 	if (bcount <= copyout_max_cached)
7397c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_CACHED;
7407c478bd9Sstevel@tonic-gate 	else
7417c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_DEFAULT;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	/* If read sync is not asked for, filter sync flags */
7477c478bd9Sstevel@tonic-gate 	if ((ioflag & FRSYNC) == 0)
7487c478bd9Sstevel@tonic-gate 		ioflag &= ~(FSYNC|FDSYNC);
7497c478bd9Sstevel@tonic-gate 	error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
7507c478bd9Sstevel@tonic-gate 	count -= auio.uio_resid;
7517c478bd9Sstevel@tonic-gate 	CPU_STATS_ENTER_K();
7527c478bd9Sstevel@tonic-gate 	cp = CPU;
7537c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, sysread, 1);
7547c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, readch, (ulong_t)count);
7557c478bd9Sstevel@tonic-gate 	CPU_STATS_EXIT_K();
7567c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_ru.ioch += (ulong_t)count;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	if (vp->v_type == VFIFO)	/* Backward compatibility */
7597c478bd9Sstevel@tonic-gate 		fp->f_offset = count;
7607c478bd9Sstevel@tonic-gate 	else if (((fp->f_flag & FAPPEND) == 0) ||
7617c478bd9Sstevel@tonic-gate 	    (vp->v_type != VREG) || (bcount != 0))	/* POSIX */
7627c478bd9Sstevel@tonic-gate 		fp->f_offset = auio.uio_loffset;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if (error == EINTR && count != 0)
7677c478bd9Sstevel@tonic-gate 		error = 0;
7687c478bd9Sstevel@tonic-gate out:
7697c478bd9Sstevel@tonic-gate 	if (in_crit)
7707c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
7717c478bd9Sstevel@tonic-gate 	releasef(fdes);
7727c478bd9Sstevel@tonic-gate 	if (error)
7737c478bd9Sstevel@tonic-gate 		return (set_errno(error));
7747c478bd9Sstevel@tonic-gate 	return (count);
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate ssize_t
7787c478bd9Sstevel@tonic-gate writev(int fdes, struct iovec *iovp, int iovcnt)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	struct uio auio;
7817c478bd9Sstevel@tonic-gate 	struct iovec aiov[DEF_IOV_MAX];
7827c478bd9Sstevel@tonic-gate 	file_t *fp;
7837c478bd9Sstevel@tonic-gate 	register vnode_t *vp;
7847c478bd9Sstevel@tonic-gate 	struct cpu *cp;
7857c478bd9Sstevel@tonic-gate 	int fflag, ioflag, rwflag;
7867c478bd9Sstevel@tonic-gate 	ssize_t count, bcount;
7877c478bd9Sstevel@tonic-gate 	int error = 0;
7887c478bd9Sstevel@tonic-gate 	int i;
7897c478bd9Sstevel@tonic-gate 	u_offset_t fileoff;
7907c478bd9Sstevel@tonic-gate 	int in_crit = 0;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	if (iovcnt <= 0 || iovcnt > DEF_IOV_MAX)
7937c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
7967c478bd9Sstevel@tonic-gate 	/*
7977c478bd9Sstevel@tonic-gate 	 * 32-bit callers need to have their iovec expanded,
7987c478bd9Sstevel@tonic-gate 	 * while ensuring that they can't move more than 2Gbytes
7997c478bd9Sstevel@tonic-gate 	 * of data in a single call.
8007c478bd9Sstevel@tonic-gate 	 */
8017c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_ILP32) {
8027c478bd9Sstevel@tonic-gate 		struct iovec32 aiov32[DEF_IOV_MAX];
8037c478bd9Sstevel@tonic-gate 		ssize32_t count32;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 		if (copyin(iovp, aiov32, iovcnt * sizeof (struct iovec32)))
8067c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 		count32 = 0;
8097c478bd9Sstevel@tonic-gate 		for (i = 0; i < iovcnt; i++) {
8107c478bd9Sstevel@tonic-gate 			ssize32_t iovlen = aiov32[i].iov_len;
8117c478bd9Sstevel@tonic-gate 			count32 += iovlen;
8127c478bd9Sstevel@tonic-gate 			if (iovlen < 0 || count32 < 0)
8137c478bd9Sstevel@tonic-gate 				return (set_errno(EINVAL));
8147c478bd9Sstevel@tonic-gate 			aiov[i].iov_len = iovlen;
8157c478bd9Sstevel@tonic-gate 			aiov[i].iov_base =
8167c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)aiov32[i].iov_base;
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 	} else
8197c478bd9Sstevel@tonic-gate #endif
8207c478bd9Sstevel@tonic-gate 	if (copyin(iovp, aiov, iovcnt * sizeof (struct iovec)))
8217c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	count = 0;
8247c478bd9Sstevel@tonic-gate 	for (i = 0; i < iovcnt; i++) {
8257c478bd9Sstevel@tonic-gate 		ssize_t iovlen = aiov[i].iov_len;
8267c478bd9Sstevel@tonic-gate 		count += iovlen;
8277c478bd9Sstevel@tonic-gate 		if (iovlen < 0 || count < 0)
8287c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
8317c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
8327c478bd9Sstevel@tonic-gate 	if (((fflag = fp->f_flag) & FWRITE) == 0) {
8337c478bd9Sstevel@tonic-gate 		error = EBADF;
8347c478bd9Sstevel@tonic-gate 		goto out;
8357c478bd9Sstevel@tonic-gate 	}
8367c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
8377c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG && count == 0) {
8387c478bd9Sstevel@tonic-gate 		goto out;
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	rwflag = 1;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	/*
8447c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
8457c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with ufs.
8467c478bd9Sstevel@tonic-gate 	 */
8477c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
8487c478bd9Sstevel@tonic-gate 		int svmand;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
8517c478bd9Sstevel@tonic-gate 		in_crit = 1;
8527c478bd9Sstevel@tonic-gate 		error = nbl_svmand(vp, fp->f_cred, &svmand);
8537c478bd9Sstevel@tonic-gate 		if (error != 0)
8547c478bd9Sstevel@tonic-gate 			goto out;
855da6c28aaSamw 		if (nbl_conflict(vp, NBL_WRITE, fp->f_offset, count, svmand,
856da6c28aaSamw 		    NULL)) {
8577c478bd9Sstevel@tonic-gate 			error = EACCES;
8587c478bd9Sstevel@tonic-gate 			goto out;
8597c478bd9Sstevel@tonic-gate 		}
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	fileoff = fp->f_offset;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	/*
8677c478bd9Sstevel@tonic-gate 	 * Behaviour is same as write. Please see comments for write.
8687c478bd9Sstevel@tonic-gate 	 */
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG) {
8717c478bd9Sstevel@tonic-gate 		if (fileoff >= curproc->p_fsz_ctl) {
8727c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, rwflag, NULL);
8737c478bd9Sstevel@tonic-gate 			mutex_enter(&curproc->p_lock);
8747c478bd9Sstevel@tonic-gate 			(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
8757c478bd9Sstevel@tonic-gate 			    curproc->p_rctls, curproc, RCA_UNSAFE_SIGINFO);
8767c478bd9Sstevel@tonic-gate 			mutex_exit(&curproc->p_lock);
8777c478bd9Sstevel@tonic-gate 			error = EFBIG;
8787c478bd9Sstevel@tonic-gate 			goto out;
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 		if (fileoff >= OFFSET_MAX(fp)) {
8817c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, rwflag, NULL);
8827c478bd9Sstevel@tonic-gate 			error = EFBIG;
8837c478bd9Sstevel@tonic-gate 			goto out;
8847c478bd9Sstevel@tonic-gate 		}
8857c478bd9Sstevel@tonic-gate 		if (fileoff + count > OFFSET_MAX(fp))
8867c478bd9Sstevel@tonic-gate 			count = (ssize_t)(OFFSET_MAX(fp) - fileoff);
8877c478bd9Sstevel@tonic-gate 	}
8887c478bd9Sstevel@tonic-gate 	auio.uio_loffset = fileoff;
8897c478bd9Sstevel@tonic-gate 	auio.uio_iov = aiov;
8907c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = iovcnt;
8917c478bd9Sstevel@tonic-gate 	auio.uio_resid = bcount = count;
8927c478bd9Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
8937c478bd9Sstevel@tonic-gate 	auio.uio_llimit = curproc->p_fsz_ctl;
8947c478bd9Sstevel@tonic-gate 	auio.uio_fmode = fflag;
8957c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_DEFAULT;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	error = VOP_WRITE(vp, &auio, ioflag, fp->f_cred, NULL);
9007c478bd9Sstevel@tonic-gate 	count -= auio.uio_resid;
9017c478bd9Sstevel@tonic-gate 	CPU_STATS_ENTER_K();
9027c478bd9Sstevel@tonic-gate 	cp = CPU;
9037c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, syswrite, 1);
9047c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, writech, (ulong_t)count);
9057c478bd9Sstevel@tonic-gate 	CPU_STATS_EXIT_K();
9067c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_ru.ioch += (ulong_t)count;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	if (vp->v_type == VFIFO)	/* Backward compatibility */
9097c478bd9Sstevel@tonic-gate 		fp->f_offset = count;
9107c478bd9Sstevel@tonic-gate 	else if (((fp->f_flag & FAPPEND) == 0) ||
9117c478bd9Sstevel@tonic-gate 	    (vp->v_type != VREG) || (bcount != 0))	/* POSIX */
9127c478bd9Sstevel@tonic-gate 		fp->f_offset = auio.uio_loffset;
9137c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	if (error == EINTR && count != 0)
9167c478bd9Sstevel@tonic-gate 		error = 0;
9177c478bd9Sstevel@tonic-gate out:
9187c478bd9Sstevel@tonic-gate 	if (in_crit)
9197c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
9207c478bd9Sstevel@tonic-gate 	releasef(fdes);
9217c478bd9Sstevel@tonic-gate 	if (error)
9227c478bd9Sstevel@tonic-gate 		return (set_errno(error));
9237c478bd9Sstevel@tonic-gate 	return (count);
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate /*
9297c478bd9Sstevel@tonic-gate  * This syscall supplies 64-bit file offsets to 32-bit applications only.
9307c478bd9Sstevel@tonic-gate  */
9317c478bd9Sstevel@tonic-gate ssize32_t
9327c478bd9Sstevel@tonic-gate pread64(int fdes, void *cbuf, size32_t count, uint32_t offset_1,
9337c478bd9Sstevel@tonic-gate     uint32_t offset_2)
9347c478bd9Sstevel@tonic-gate {
9357c478bd9Sstevel@tonic-gate 	struct uio auio;
9367c478bd9Sstevel@tonic-gate 	struct iovec aiov;
9377c478bd9Sstevel@tonic-gate 	file_t *fp;
9387c478bd9Sstevel@tonic-gate 	register vnode_t *vp;
9397c478bd9Sstevel@tonic-gate 	struct cpu *cp;
9407c478bd9Sstevel@tonic-gate 	int fflag, ioflag, rwflag;
9417c478bd9Sstevel@tonic-gate 	ssize_t bcount;
9427c478bd9Sstevel@tonic-gate 	int error = 0;
9437c478bd9Sstevel@tonic-gate 	u_offset_t fileoff;
9447c478bd9Sstevel@tonic-gate 	int in_crit = 0;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
9477c478bd9Sstevel@tonic-gate 	fileoff = ((u_offset_t)offset_2 << 32) | (u_offset_t)offset_1;
9487c478bd9Sstevel@tonic-gate #else
9497c478bd9Sstevel@tonic-gate 	fileoff = ((u_offset_t)offset_1 << 32) | (u_offset_t)offset_2;
9507c478bd9Sstevel@tonic-gate #endif
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	if ((bcount = (ssize_t)count) < 0 || bcount > INT32_MAX)
9537c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
9567c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
9577c478bd9Sstevel@tonic-gate 	if (((fflag = fp->f_flag) & (FREAD)) == 0) {
9587c478bd9Sstevel@tonic-gate 		error = EBADF;
9597c478bd9Sstevel@tonic-gate 		goto out;
9607c478bd9Sstevel@tonic-gate 	}
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	rwflag = 0;
9637c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG) {
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		if (bcount == 0)
9687c478bd9Sstevel@tonic-gate 			goto out;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 		/*
9717c478bd9Sstevel@tonic-gate 		 * Same as pread. See comments in pread.
9727c478bd9Sstevel@tonic-gate 		 */
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 		if (fileoff > MAXOFFSET_T) {
9757c478bd9Sstevel@tonic-gate 			error = EINVAL;
9767c478bd9Sstevel@tonic-gate 			goto out;
9777c478bd9Sstevel@tonic-gate 		}
9787c478bd9Sstevel@tonic-gate 		if (fileoff + bcount > MAXOFFSET_T)
9797c478bd9Sstevel@tonic-gate 			bcount = (ssize_t)(MAXOFFSET_T - fileoff);
9807c478bd9Sstevel@tonic-gate 	} else if (vp->v_type == VFIFO) {
9817c478bd9Sstevel@tonic-gate 		error = ESPIPE;
9827c478bd9Sstevel@tonic-gate 		goto out;
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	/*
9867c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
9877c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with ufs.
9887c478bd9Sstevel@tonic-gate 	 */
9897c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
9907c478bd9Sstevel@tonic-gate 		int svmand;
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
9937c478bd9Sstevel@tonic-gate 		in_crit = 1;
9947c478bd9Sstevel@tonic-gate 		error = nbl_svmand(vp, fp->f_cred, &svmand);
9957c478bd9Sstevel@tonic-gate 		if (error != 0)
9967c478bd9Sstevel@tonic-gate 			goto out;
997da6c28aaSamw 		if (nbl_conflict(vp, NBL_READ, fileoff, bcount, svmand,
998da6c28aaSamw 		    NULL)) {
9997c478bd9Sstevel@tonic-gate 			error = EACCES;
10007c478bd9Sstevel@tonic-gate 			goto out;
10017c478bd9Sstevel@tonic-gate 		}
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	aiov.iov_base = cbuf;
10057c478bd9Sstevel@tonic-gate 	aiov.iov_len = bcount;
10067c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
10077c478bd9Sstevel@tonic-gate 	auio.uio_loffset = fileoff;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	/*
10107c478bd9Sstevel@tonic-gate 	 * Note: File size can never be greater than MAXOFFSET_T.
10117c478bd9Sstevel@tonic-gate 	 * If ever we start supporting 128 bit files the code
10127c478bd9Sstevel@tonic-gate 	 * similar to the one in pread at this place should be here.
10137c478bd9Sstevel@tonic-gate 	 * Here we avoid the unnecessary VOP_GETATTR() when we
10147c478bd9Sstevel@tonic-gate 	 * know that fileoff == MAXOFFSET_T implies that it is always
10157c478bd9Sstevel@tonic-gate 	 * greater than or equal to file size.
10167c478bd9Sstevel@tonic-gate 	 */
10177c478bd9Sstevel@tonic-gate 	auio.uio_iov = &aiov;
10187c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
10197c478bd9Sstevel@tonic-gate 	auio.uio_resid = bcount;
10207c478bd9Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
10217c478bd9Sstevel@tonic-gate 	auio.uio_llimit = MAXOFFSET_T;
10227c478bd9Sstevel@tonic-gate 	auio.uio_fmode = fflag;
10237c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	/* If read sync is not asked for, filter sync flags */
10287c478bd9Sstevel@tonic-gate 	if ((ioflag & FRSYNC) == 0)
10297c478bd9Sstevel@tonic-gate 		ioflag &= ~(FSYNC|FDSYNC);
10307c478bd9Sstevel@tonic-gate 	error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
10317c478bd9Sstevel@tonic-gate 	bcount -= auio.uio_resid;
10327c478bd9Sstevel@tonic-gate 	CPU_STATS_ENTER_K();
10337c478bd9Sstevel@tonic-gate 	cp = CPU;
10347c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, sysread, 1);
10357c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, readch, (ulong_t)bcount);
10367c478bd9Sstevel@tonic-gate 	CPU_STATS_EXIT_K();
10377c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_ru.ioch += (ulong_t)bcount;
10387c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	if (error == EINTR && bcount != 0)
10417c478bd9Sstevel@tonic-gate 		error = 0;
10427c478bd9Sstevel@tonic-gate out:
10437c478bd9Sstevel@tonic-gate 	if (in_crit)
10447c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
10457c478bd9Sstevel@tonic-gate 	releasef(fdes);
10467c478bd9Sstevel@tonic-gate 	if (error)
10477c478bd9Sstevel@tonic-gate 		return (set_errno(error));
10487c478bd9Sstevel@tonic-gate 	return (bcount);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate /*
10527c478bd9Sstevel@tonic-gate  * This syscall supplies 64-bit file offsets to 32-bit applications only.
10537c478bd9Sstevel@tonic-gate  */
10547c478bd9Sstevel@tonic-gate ssize32_t
10557c478bd9Sstevel@tonic-gate pwrite64(int fdes, void *cbuf, size32_t count, uint32_t offset_1,
10567c478bd9Sstevel@tonic-gate     uint32_t offset_2)
10577c478bd9Sstevel@tonic-gate {
10587c478bd9Sstevel@tonic-gate 	struct uio auio;
10597c478bd9Sstevel@tonic-gate 	struct iovec aiov;
10607c478bd9Sstevel@tonic-gate 	file_t *fp;
10617c478bd9Sstevel@tonic-gate 	register vnode_t *vp;
10627c478bd9Sstevel@tonic-gate 	struct cpu *cp;
10637c478bd9Sstevel@tonic-gate 	int fflag, ioflag, rwflag;
10647c478bd9Sstevel@tonic-gate 	ssize_t bcount;
10657c478bd9Sstevel@tonic-gate 	int error = 0;
10667c478bd9Sstevel@tonic-gate 	u_offset_t fileoff;
10677c478bd9Sstevel@tonic-gate 	int in_crit = 0;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
10707c478bd9Sstevel@tonic-gate 	fileoff = ((u_offset_t)offset_2 << 32) | (u_offset_t)offset_1;
10717c478bd9Sstevel@tonic-gate #else
10727c478bd9Sstevel@tonic-gate 	fileoff = ((u_offset_t)offset_1 << 32) | (u_offset_t)offset_2;
10737c478bd9Sstevel@tonic-gate #endif
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	if ((bcount = (ssize_t)count) < 0 || bcount > INT32_MAX)
10767c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
10777c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
10787c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
10797c478bd9Sstevel@tonic-gate 	if (((fflag = fp->f_flag) & (FWRITE)) == 0) {
10807c478bd9Sstevel@tonic-gate 		error = EBADF;
10817c478bd9Sstevel@tonic-gate 		goto out;
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	rwflag = 1;
10857c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG) {
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 		if (bcount == 0)
10907c478bd9Sstevel@tonic-gate 			goto out;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 		/*
10937c478bd9Sstevel@tonic-gate 		 * See comments in pwrite.
10947c478bd9Sstevel@tonic-gate 		 */
10957c478bd9Sstevel@tonic-gate 		if (fileoff > MAXOFFSET_T) {
10967c478bd9Sstevel@tonic-gate 			error = EINVAL;
10977c478bd9Sstevel@tonic-gate 			goto out;
10987c478bd9Sstevel@tonic-gate 		}
10997c478bd9Sstevel@tonic-gate 		if (fileoff >= curproc->p_fsz_ctl) {
11007c478bd9Sstevel@tonic-gate 			mutex_enter(&curproc->p_lock);
11017c478bd9Sstevel@tonic-gate 			(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
11027c478bd9Sstevel@tonic-gate 			    curproc->p_rctls, curproc, RCA_SAFE);
11037c478bd9Sstevel@tonic-gate 			mutex_exit(&curproc->p_lock);
11047c478bd9Sstevel@tonic-gate 			error = EFBIG;
11057c478bd9Sstevel@tonic-gate 			goto out;
11067c478bd9Sstevel@tonic-gate 		}
11077c478bd9Sstevel@tonic-gate 		if (fileoff == MAXOFFSET_T) {
11087c478bd9Sstevel@tonic-gate 			error = EFBIG;
11097c478bd9Sstevel@tonic-gate 			goto out;
11107c478bd9Sstevel@tonic-gate 		}
11117c478bd9Sstevel@tonic-gate 		if (fileoff + bcount > MAXOFFSET_T)
11127c478bd9Sstevel@tonic-gate 			bcount = (ssize_t)((u_offset_t)MAXOFFSET_T - fileoff);
11137c478bd9Sstevel@tonic-gate 	} else if (vp->v_type == VFIFO) {
11147c478bd9Sstevel@tonic-gate 		error = ESPIPE;
11157c478bd9Sstevel@tonic-gate 		goto out;
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	/*
11197c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
11207c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with ufs.
11217c478bd9Sstevel@tonic-gate 	 */
11227c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
11237c478bd9Sstevel@tonic-gate 		int svmand;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
11267c478bd9Sstevel@tonic-gate 		in_crit = 1;
11277c478bd9Sstevel@tonic-gate 		error = nbl_svmand(vp, fp->f_cred, &svmand);
11287c478bd9Sstevel@tonic-gate 		if (error != 0)
11297c478bd9Sstevel@tonic-gate 			goto out;
1130da6c28aaSamw 		if (nbl_conflict(vp, NBL_WRITE, fileoff, bcount, svmand,
1131da6c28aaSamw 		    NULL)) {
11327c478bd9Sstevel@tonic-gate 			error = EACCES;
11337c478bd9Sstevel@tonic-gate 			goto out;
11347c478bd9Sstevel@tonic-gate 		}
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	aiov.iov_base = cbuf;
11387c478bd9Sstevel@tonic-gate 	aiov.iov_len = bcount;
11397c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
11407c478bd9Sstevel@tonic-gate 	auio.uio_loffset = fileoff;
11417c478bd9Sstevel@tonic-gate 	auio.uio_iov = &aiov;
11427c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
11437c478bd9Sstevel@tonic-gate 	auio.uio_resid = bcount;
11447c478bd9Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
11457c478bd9Sstevel@tonic-gate 	auio.uio_llimit = curproc->p_fsz_ctl;
11467c478bd9Sstevel@tonic-gate 	auio.uio_fmode = fflag;
11477c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
11487c478bd9Sstevel@tonic-gate 
1149*4d86dd30Sraf 	/*
1150*4d86dd30Sraf 	 * The SUSv4 POSIX specification states:
1151*4d86dd30Sraf 	 *	The pwrite() function shall be equivalent to write(), except
1152*4d86dd30Sraf 	 *	that it writes into a given position and does not change
1153*4d86dd30Sraf 	 *	the file offset (regardless of whether O_APPEND is set).
1154*4d86dd30Sraf 	 * To make this be true, we omit the FAPPEND flag from ioflag.
1155*4d86dd30Sraf 	 */
1156*4d86dd30Sraf 	ioflag = auio.uio_fmode & (FSYNC|FDSYNC|FRSYNC);
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	error = VOP_WRITE(vp, &auio, ioflag, fp->f_cred, NULL);
11597c478bd9Sstevel@tonic-gate 	bcount -= auio.uio_resid;
11607c478bd9Sstevel@tonic-gate 	CPU_STATS_ENTER_K();
11617c478bd9Sstevel@tonic-gate 	cp = CPU;
11627c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, syswrite, 1);
11637c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(cp, sys, writech, (ulong_t)bcount);
11647c478bd9Sstevel@tonic-gate 	CPU_STATS_EXIT_K();
11657c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_ru.ioch += (ulong_t)bcount;
11667c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	if (error == EINTR && bcount != 0)
11697c478bd9Sstevel@tonic-gate 		error = 0;
11707c478bd9Sstevel@tonic-gate out:
11717c478bd9Sstevel@tonic-gate 	if (in_crit)
11727c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
11737c478bd9Sstevel@tonic-gate 	releasef(fdes);
11747c478bd9Sstevel@tonic-gate 	if (error)
11757c478bd9Sstevel@tonic-gate 		return (set_errno(error));
11767c478bd9Sstevel@tonic-gate 	return (bcount);
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL || _ILP32 */
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11827c478bd9Sstevel@tonic-gate /*
11837c478bd9Sstevel@tonic-gate  * Tail-call elimination of xxx32() down to xxx()
11847c478bd9Sstevel@tonic-gate  *
11857c478bd9Sstevel@tonic-gate  * A number of xxx32 system calls take a len (or count) argument and
11867c478bd9Sstevel@tonic-gate  * return a number in the range [0,len] or -1 on error.
11877c478bd9Sstevel@tonic-gate  * Given an ssize32_t input len, the downcall xxx() will return
11887c478bd9Sstevel@tonic-gate  * a 64-bit value that is -1 or in the range [0,len] which actually
11897c478bd9Sstevel@tonic-gate  * is a proper return value for the xxx32 call. So even if the xxx32
11907c478bd9Sstevel@tonic-gate  * calls can be considered as returning a ssize32_t, they are currently
11917c478bd9Sstevel@tonic-gate  * declared as returning a ssize_t as this enables tail-call elimination.
11927c478bd9Sstevel@tonic-gate  *
11937c478bd9Sstevel@tonic-gate  * The cast of len (or count) to ssize32_t is needed to ensure we pass
11947c478bd9Sstevel@tonic-gate  * down negative input values as such and let the downcall handle error
11957c478bd9Sstevel@tonic-gate  * reporting. Functions covered by this comments are:
11967c478bd9Sstevel@tonic-gate  *
11977c478bd9Sstevel@tonic-gate  * rw.c:           read32, write32, pread32, pwrite32, readv32, writev32.
11987c478bd9Sstevel@tonic-gate  * socksyscall.c:  recv32, recvfrom32, send32, sendto32.
11997c478bd9Sstevel@tonic-gate  * readlink.c:     readlink32.
12007c478bd9Sstevel@tonic-gate  */
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate ssize_t
12037c478bd9Sstevel@tonic-gate read32(int32_t fdes, caddr32_t cbuf, size32_t count)
12047c478bd9Sstevel@tonic-gate {
12057c478bd9Sstevel@tonic-gate 	return (read(fdes,
12067c478bd9Sstevel@tonic-gate 	    (void *)(uintptr_t)cbuf, (ssize32_t)count));
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate ssize_t
12107c478bd9Sstevel@tonic-gate write32(int32_t fdes, caddr32_t cbuf, size32_t count)
12117c478bd9Sstevel@tonic-gate {
12127c478bd9Sstevel@tonic-gate 	return (write(fdes,
12137c478bd9Sstevel@tonic-gate 	    (void *)(uintptr_t)cbuf, (ssize32_t)count));
12147c478bd9Sstevel@tonic-gate }
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate ssize_t
12177c478bd9Sstevel@tonic-gate pread32(int32_t fdes, caddr32_t cbuf, size32_t count, off32_t offset)
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate 	return (pread(fdes,
12207c478bd9Sstevel@tonic-gate 	    (void *)(uintptr_t)cbuf, (ssize32_t)count,
12217c478bd9Sstevel@tonic-gate 	    (off_t)(uint32_t)offset));
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate ssize_t
12257c478bd9Sstevel@tonic-gate pwrite32(int32_t fdes, caddr32_t cbuf, size32_t count, off32_t offset)
12267c478bd9Sstevel@tonic-gate {
12277c478bd9Sstevel@tonic-gate 	return (pwrite(fdes,
12287c478bd9Sstevel@tonic-gate 	    (void *)(uintptr_t)cbuf, (ssize32_t)count,
12297c478bd9Sstevel@tonic-gate 	    (off_t)(uint32_t)offset));
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate ssize_t
12337c478bd9Sstevel@tonic-gate readv32(int32_t fdes, caddr32_t iovp, int32_t iovcnt)
12347c478bd9Sstevel@tonic-gate {
12357c478bd9Sstevel@tonic-gate 	return (readv(fdes, (void *)(uintptr_t)iovp, iovcnt));
12367c478bd9Sstevel@tonic-gate }
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate ssize_t
12397c478bd9Sstevel@tonic-gate writev32(int32_t fdes, caddr32_t iovp, int32_t iovcnt)
12407c478bd9Sstevel@tonic-gate {
12417c478bd9Sstevel@tonic-gate 	return (writev(fdes, (void *)(uintptr_t)iovp, iovcnt));
12427c478bd9Sstevel@tonic-gate }
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
1245