xref: /titanic_50/usr/src/uts/common/syscall/fcntl.c (revision a5f69788de7ac07553de47f7fec8c05a9a94c105)
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
5*a5f69788Scraigm  * Common Development and Distribution License (the "License").
6*a5f69788Scraigm  * 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  */
217c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
227c478bd9Sstevel@tonic-gate /*
23*a5f69788Scraigm  * Copyright 2006 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 /* ONC_PLUS EXTRACT END */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/param.h>
397c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/errno.h>
447c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
457c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
467c478bd9Sstevel@tonic-gate #include <sys/flock.h>
477c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
487c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
497c478bd9Sstevel@tonic-gate #include <sys/file.h>
507c478bd9Sstevel@tonic-gate #include <sys/mode.h>
517c478bd9Sstevel@tonic-gate #include <sys/proc.h>
527c478bd9Sstevel@tonic-gate #include <sys/filio.h>
537c478bd9Sstevel@tonic-gate #include <sys/share.h>
547c478bd9Sstevel@tonic-gate #include <sys/debug.h>
557c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
567c478bd9Sstevel@tonic-gate #include <sys/nbmlock.h>
577c478bd9Sstevel@tonic-gate 
58303bf60bSsdebnath #include <sys/cmn_err.h>
59303bf60bSsdebnath 
607c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
617c478bd9Sstevel@tonic-gate static int flock_check(vnode_t *, flock64_t *, offset_t, offset_t);
627c478bd9Sstevel@tonic-gate static int flock_get_start(vnode_t *, flock64_t *, offset_t, u_offset_t *);
637c478bd9Sstevel@tonic-gate static void fd_too_big(proc_t *);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * File control.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate int
697c478bd9Sstevel@tonic-gate fcntl(int fdes, int cmd, intptr_t arg)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	int iarg;
727c478bd9Sstevel@tonic-gate 	int error = 0;
737c478bd9Sstevel@tonic-gate 	int retval;
747c478bd9Sstevel@tonic-gate 	proc_t *p;
757c478bd9Sstevel@tonic-gate 	file_t *fp;
767c478bd9Sstevel@tonic-gate 	vnode_t *vp;
777c478bd9Sstevel@tonic-gate 	u_offset_t offset;
787c478bd9Sstevel@tonic-gate 	u_offset_t start;
797c478bd9Sstevel@tonic-gate 	struct vattr vattr;
807c478bd9Sstevel@tonic-gate 	int in_crit;
817c478bd9Sstevel@tonic-gate 	int flag;
827c478bd9Sstevel@tonic-gate 	struct flock sbf;
837c478bd9Sstevel@tonic-gate 	struct flock64 bf;
847c478bd9Sstevel@tonic-gate 	struct o_flock obf;
857c478bd9Sstevel@tonic-gate 	struct flock64_32 bf64_32;
867c478bd9Sstevel@tonic-gate 	struct fshare fsh;
877c478bd9Sstevel@tonic-gate 	struct shrlock shr;
887c478bd9Sstevel@tonic-gate 	struct shr_locowner shr_own;
897c478bd9Sstevel@tonic-gate 	offset_t maxoffset;
907c478bd9Sstevel@tonic-gate 	model_t datamodel;
91*a5f69788Scraigm 	int fdres;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #if defined(_ILP32) && !defined(lint) && defined(_SYSCALL32)
947c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct flock) == sizeof (struct flock32));
957c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct flock64) == sizeof (struct flock64_32));
967c478bd9Sstevel@tonic-gate #endif
977c478bd9Sstevel@tonic-gate #if defined(_LP64) && !defined(lint) && defined(_SYSCALL32)
987c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct flock) == sizeof (struct flock64_64));
997c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct flock64) == sizeof (struct flock64_64));
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/*
1037c478bd9Sstevel@tonic-gate 	 * First, for speed, deal with the subset of cases
1047c478bd9Sstevel@tonic-gate 	 * that do not require getf() / releasef().
1057c478bd9Sstevel@tonic-gate 	 */
1067c478bd9Sstevel@tonic-gate 	switch (cmd) {
1077c478bd9Sstevel@tonic-gate 	case F_GETFD:
1087c478bd9Sstevel@tonic-gate 		if ((error = f_getfd_error(fdes, &flag)) == 0)
1097c478bd9Sstevel@tonic-gate 			retval = flag;
1107c478bd9Sstevel@tonic-gate 		goto out;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	case F_SETFD:
1137c478bd9Sstevel@tonic-gate 		error = f_setfd_error(fdes, (int)arg);
1147c478bd9Sstevel@tonic-gate 		retval = 0;
1157c478bd9Sstevel@tonic-gate 		goto out;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	case F_GETFL:
1187c478bd9Sstevel@tonic-gate 		if ((error = f_getfl(fdes, &flag)) == 0)
1197c478bd9Sstevel@tonic-gate 			retval = (flag & (FMASK | FASYNC)) + FOPEN;
1207c478bd9Sstevel@tonic-gate 		goto out;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	case F_GETXFL:
1237c478bd9Sstevel@tonic-gate 		if ((error = f_getfl(fdes, &flag)) == 0)
1247c478bd9Sstevel@tonic-gate 			retval = flag + FOPEN;
1257c478bd9Sstevel@tonic-gate 		goto out;
126*a5f69788Scraigm 
127*a5f69788Scraigm 	case F_BADFD:
128*a5f69788Scraigm 		if ((error = f_badfd(fdes, &fdres, (int)arg)) == 0)
129*a5f69788Scraigm 			retval = fdres;
130*a5f69788Scraigm 		goto out;
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * Second, for speed, deal with the subset of cases that
1357c478bd9Sstevel@tonic-gate 	 * require getf() / releasef() but do not require copyin.
1367c478bd9Sstevel@tonic-gate 	 */
1377c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL) {
1387c478bd9Sstevel@tonic-gate 		error = EBADF;
1397c478bd9Sstevel@tonic-gate 		goto out;
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	iarg = (int)arg;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	switch (cmd) {
1447c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	case F_DUPFD:
1477c478bd9Sstevel@tonic-gate 		p = curproc;
1487c478bd9Sstevel@tonic-gate 		if ((uint_t)iarg >= p->p_fno_ctl) {
1497c478bd9Sstevel@tonic-gate 			if (iarg >= 0)
1507c478bd9Sstevel@tonic-gate 				fd_too_big(p);
1517c478bd9Sstevel@tonic-gate 			error = EINVAL;
1527c478bd9Sstevel@tonic-gate 		} else if ((retval = ufalloc_file(iarg, fp)) == -1) {
1537c478bd9Sstevel@tonic-gate 			error = EMFILE;
1547c478bd9Sstevel@tonic-gate 		} else {
1557c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->f_tlock);
1567c478bd9Sstevel@tonic-gate 			fp->f_count++;
1577c478bd9Sstevel@tonic-gate 			mutex_exit(&fp->f_tlock);
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 		goto done;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	case F_DUP2FD:
1627c478bd9Sstevel@tonic-gate 		p = curproc;
1637c478bd9Sstevel@tonic-gate 		if (fdes == iarg) {
1647c478bd9Sstevel@tonic-gate 			retval = iarg;
1657c478bd9Sstevel@tonic-gate 		} else if ((uint_t)iarg >= p->p_fno_ctl) {
1667c478bd9Sstevel@tonic-gate 			if (iarg >= 0)
1677c478bd9Sstevel@tonic-gate 				fd_too_big(p);
1687c478bd9Sstevel@tonic-gate 			error = EBADF;
1697c478bd9Sstevel@tonic-gate 		} else {
1707c478bd9Sstevel@tonic-gate 			/*
1717c478bd9Sstevel@tonic-gate 			 * We can't hold our getf(fdes) across the call to
1727c478bd9Sstevel@tonic-gate 			 * closeandsetf() because it creates a window for
1737c478bd9Sstevel@tonic-gate 			 * deadlock: if one thread is doing dup2(a, b) while
1747c478bd9Sstevel@tonic-gate 			 * another is doing dup2(b, a), each one will block
1757c478bd9Sstevel@tonic-gate 			 * waiting for the other to call releasef().  The
1767c478bd9Sstevel@tonic-gate 			 * solution is to increment the file reference count
1777c478bd9Sstevel@tonic-gate 			 * (which we have to do anyway), then releasef(fdes),
1787c478bd9Sstevel@tonic-gate 			 * then closeandsetf().  Incrementing f_count ensures
1797c478bd9Sstevel@tonic-gate 			 * that fp won't disappear after we call releasef().
180*a5f69788Scraigm 			 * When closeandsetf() fails, we try avoid calling
181*a5f69788Scraigm 			 * closef() because of all the side effects.
1827c478bd9Sstevel@tonic-gate 			 */
1837c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->f_tlock);
1847c478bd9Sstevel@tonic-gate 			fp->f_count++;
1857c478bd9Sstevel@tonic-gate 			mutex_exit(&fp->f_tlock);
1867c478bd9Sstevel@tonic-gate 			releasef(fdes);
187*a5f69788Scraigm 			if ((error = closeandsetf(iarg, fp)) == 0) {
1887c478bd9Sstevel@tonic-gate 				retval = iarg;
189*a5f69788Scraigm 			} else {
190*a5f69788Scraigm 				mutex_enter(&fp->f_tlock);
191*a5f69788Scraigm 				if (fp->f_count > 1) {
192*a5f69788Scraigm 					fp->f_count--;
193*a5f69788Scraigm 					mutex_exit(&fp->f_tlock);
194*a5f69788Scraigm 				} else {
195*a5f69788Scraigm 					mutex_exit(&fp->f_tlock);
196*a5f69788Scraigm 					(void) closef(fp);
197*a5f69788Scraigm 				}
198*a5f69788Scraigm 			}
1997c478bd9Sstevel@tonic-gate 			goto out;
2007c478bd9Sstevel@tonic-gate 		}
2017c478bd9Sstevel@tonic-gate 		goto done;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	case F_SETFL:
2047c478bd9Sstevel@tonic-gate 		vp = fp->f_vnode;
2057c478bd9Sstevel@tonic-gate 		flag = fp->f_flag;
2067c478bd9Sstevel@tonic-gate 		if ((iarg & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY))
2077c478bd9Sstevel@tonic-gate 			iarg &= ~FNDELAY;
2087c478bd9Sstevel@tonic-gate 		if ((error = VOP_SETFL(vp, flag, iarg, fp->f_cred)) == 0) {
2097c478bd9Sstevel@tonic-gate 			iarg &= FMASK;
2107c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->f_tlock);
2117c478bd9Sstevel@tonic-gate 			fp->f_flag &= ~FMASK | (FREAD|FWRITE);
2127c478bd9Sstevel@tonic-gate 			fp->f_flag |= (iarg - FOPEN) & ~(FREAD|FWRITE);
2137c478bd9Sstevel@tonic-gate 			mutex_exit(&fp->f_tlock);
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 		retval = 0;
2167c478bd9Sstevel@tonic-gate 		goto done;
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/*
2207c478bd9Sstevel@tonic-gate 	 * Finally, deal with the expensive cases.
2217c478bd9Sstevel@tonic-gate 	 */
2227c478bd9Sstevel@tonic-gate 	retval = 0;
2237c478bd9Sstevel@tonic-gate 	in_crit = 0;
2247c478bd9Sstevel@tonic-gate 	maxoffset = MAXOFF_T;
2257c478bd9Sstevel@tonic-gate 	datamodel = DATAMODEL_NATIVE;
2267c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
2277c478bd9Sstevel@tonic-gate 	if ((datamodel = get_udatamodel()) == DATAMODEL_ILP32)
2287c478bd9Sstevel@tonic-gate 		maxoffset = MAXOFF32_T;
2297c478bd9Sstevel@tonic-gate #endif
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
2327c478bd9Sstevel@tonic-gate 	flag = fp->f_flag;
2337c478bd9Sstevel@tonic-gate 	offset = fp->f_offset;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	switch (cmd) {
2367c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
2377c478bd9Sstevel@tonic-gate 	/*
2387c478bd9Sstevel@tonic-gate 	 * The file system and vnode layers understand and implement
2397c478bd9Sstevel@tonic-gate 	 * locking with flock64 structures. So here once we pass through
2407c478bd9Sstevel@tonic-gate 	 * the test for compatibility as defined by LFS API, (for F_SETLK,
2417c478bd9Sstevel@tonic-gate 	 * F_SETLKW, F_GETLK, F_GETLKW, F_FREESP) we transform
2427c478bd9Sstevel@tonic-gate 	 * the flock structure to a flock64 structure and send it to the
2437c478bd9Sstevel@tonic-gate 	 * lower layers. Similarly in case of GETLK the returned flock64
2447c478bd9Sstevel@tonic-gate 	 * structure is transformed to a flock structure if everything fits
2457c478bd9Sstevel@tonic-gate 	 * in nicely, otherwise we return EOVERFLOW.
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	case F_GETLK:
2497c478bd9Sstevel@tonic-gate 	case F_O_GETLK:
2507c478bd9Sstevel@tonic-gate 	case F_SETLK:
2517c478bd9Sstevel@tonic-gate 	case F_SETLKW:
2527c478bd9Sstevel@tonic-gate 	case F_SETLK_NBMAND:
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		/*
2557c478bd9Sstevel@tonic-gate 		 * Copy in input fields only.
2567c478bd9Sstevel@tonic-gate 		 */
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		if (cmd == F_O_GETLK) {
2597c478bd9Sstevel@tonic-gate 			if (datamodel != DATAMODEL_ILP32) {
2607c478bd9Sstevel@tonic-gate 				error = EINVAL;
2617c478bd9Sstevel@tonic-gate 				break;
2627c478bd9Sstevel@tonic-gate 			}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &obf, sizeof (obf))) {
2657c478bd9Sstevel@tonic-gate 				error = EFAULT;
2667c478bd9Sstevel@tonic-gate 				break;
2677c478bd9Sstevel@tonic-gate 			}
2687c478bd9Sstevel@tonic-gate 			bf.l_type = obf.l_type;
2697c478bd9Sstevel@tonic-gate 			bf.l_whence = obf.l_whence;
2707c478bd9Sstevel@tonic-gate 			bf.l_start = (off64_t)obf.l_start;
2717c478bd9Sstevel@tonic-gate 			bf.l_len = (off64_t)obf.l_len;
2727c478bd9Sstevel@tonic-gate 			bf.l_sysid = (int)obf.l_sysid;
2737c478bd9Sstevel@tonic-gate 			bf.l_pid = obf.l_pid;
2747c478bd9Sstevel@tonic-gate 		} else if (datamodel == DATAMODEL_NATIVE) {
2757c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &sbf, sizeof (sbf))) {
2767c478bd9Sstevel@tonic-gate 				error = EFAULT;
2777c478bd9Sstevel@tonic-gate 				break;
2787c478bd9Sstevel@tonic-gate 			}
2797c478bd9Sstevel@tonic-gate 			/*
2807c478bd9Sstevel@tonic-gate 			 * XXX	In an LP64 kernel with an LP64 application
2817c478bd9Sstevel@tonic-gate 			 *	there's no need to do a structure copy here
2827c478bd9Sstevel@tonic-gate 			 *	struct flock == struct flock64. However,
2837c478bd9Sstevel@tonic-gate 			 *	we did it this way to avoid more conditional
2847c478bd9Sstevel@tonic-gate 			 *	compilation.
2857c478bd9Sstevel@tonic-gate 			 */
2867c478bd9Sstevel@tonic-gate 			bf.l_type = sbf.l_type;
2877c478bd9Sstevel@tonic-gate 			bf.l_whence = sbf.l_whence;
2887c478bd9Sstevel@tonic-gate 			bf.l_start = (off64_t)sbf.l_start;
2897c478bd9Sstevel@tonic-gate 			bf.l_len = (off64_t)sbf.l_len;
2907c478bd9Sstevel@tonic-gate 			bf.l_sysid = sbf.l_sysid;
2917c478bd9Sstevel@tonic-gate 			bf.l_pid = sbf.l_pid;
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
2947c478bd9Sstevel@tonic-gate 		else {
2957c478bd9Sstevel@tonic-gate 			struct flock32 sbf32;
2967c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
2977c478bd9Sstevel@tonic-gate 				error = EFAULT;
2987c478bd9Sstevel@tonic-gate 				break;
2997c478bd9Sstevel@tonic-gate 			}
3007c478bd9Sstevel@tonic-gate 			bf.l_type = sbf32.l_type;
3017c478bd9Sstevel@tonic-gate 			bf.l_whence = sbf32.l_whence;
3027c478bd9Sstevel@tonic-gate 			bf.l_start = (off64_t)sbf32.l_start;
3037c478bd9Sstevel@tonic-gate 			bf.l_len = (off64_t)sbf32.l_len;
3047c478bd9Sstevel@tonic-gate 			bf.l_sysid = sbf32.l_sysid;
3057c478bd9Sstevel@tonic-gate 			bf.l_pid = sbf32.l_pid;
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 		/*
3107c478bd9Sstevel@tonic-gate 		 * 64-bit support: check for overflow for 32-bit lock ops
3117c478bd9Sstevel@tonic-gate 		 */
3127c478bd9Sstevel@tonic-gate 		if ((error = flock_check(vp, &bf, offset, maxoffset)) != 0)
3137c478bd9Sstevel@tonic-gate 			break;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 		/*
3167c478bd9Sstevel@tonic-gate 		 * Not all of the filesystems understand F_O_GETLK, and
3177c478bd9Sstevel@tonic-gate 		 * there's no need for them to know.  Map it to F_GETLK.
3187c478bd9Sstevel@tonic-gate 		 */
3197c478bd9Sstevel@tonic-gate 		if ((error = VOP_FRLOCK(vp, (cmd == F_O_GETLK) ? F_GETLK : cmd,
3207c478bd9Sstevel@tonic-gate 		    &bf, flag, offset, NULL, fp->f_cred)) != 0)
3217c478bd9Sstevel@tonic-gate 			break;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		/*
3247c478bd9Sstevel@tonic-gate 		 * If command is GETLK and no lock is found, only
3257c478bd9Sstevel@tonic-gate 		 * the type field is changed.
3267c478bd9Sstevel@tonic-gate 		 */
3277c478bd9Sstevel@tonic-gate 		if ((cmd == F_O_GETLK || cmd == F_GETLK) &&
3287c478bd9Sstevel@tonic-gate 		    bf.l_type == F_UNLCK) {
3297c478bd9Sstevel@tonic-gate 			/* l_type always first entry, always a short */
3307c478bd9Sstevel@tonic-gate 			if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
3317c478bd9Sstevel@tonic-gate 			    sizeof (bf.l_type)))
3327c478bd9Sstevel@tonic-gate 				error = EFAULT;
3337c478bd9Sstevel@tonic-gate 			break;
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		if (cmd == F_O_GETLK) {
3377c478bd9Sstevel@tonic-gate 			/*
3387c478bd9Sstevel@tonic-gate 			 * Return an SVR3 flock structure to the user.
3397c478bd9Sstevel@tonic-gate 			 */
3407c478bd9Sstevel@tonic-gate 			obf.l_type = (int16_t)bf.l_type;
3417c478bd9Sstevel@tonic-gate 			obf.l_whence = (int16_t)bf.l_whence;
3427c478bd9Sstevel@tonic-gate 			obf.l_start = (int32_t)bf.l_start;
3437c478bd9Sstevel@tonic-gate 			obf.l_len = (int32_t)bf.l_len;
3447c478bd9Sstevel@tonic-gate 			if (bf.l_sysid > SHRT_MAX || bf.l_pid > SHRT_MAX) {
3457c478bd9Sstevel@tonic-gate 				/*
3467c478bd9Sstevel@tonic-gate 				 * One or both values for the above fields
3477c478bd9Sstevel@tonic-gate 				 * is too large to store in an SVR3 flock
3487c478bd9Sstevel@tonic-gate 				 * structure.
3497c478bd9Sstevel@tonic-gate 				 */
3507c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
3517c478bd9Sstevel@tonic-gate 				break;
3527c478bd9Sstevel@tonic-gate 			}
3537c478bd9Sstevel@tonic-gate 			obf.l_sysid = (int16_t)bf.l_sysid;
3547c478bd9Sstevel@tonic-gate 			obf.l_pid = (int16_t)bf.l_pid;
3557c478bd9Sstevel@tonic-gate 			if (copyout(&obf, (void *)arg, sizeof (obf)))
3567c478bd9Sstevel@tonic-gate 				error = EFAULT;
3577c478bd9Sstevel@tonic-gate 		} else if (cmd == F_GETLK) {
3587c478bd9Sstevel@tonic-gate 			/*
3597c478bd9Sstevel@tonic-gate 			 * Copy out SVR4 flock.
3607c478bd9Sstevel@tonic-gate 			 */
3617c478bd9Sstevel@tonic-gate 			int i;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 			if (bf.l_start > maxoffset || bf.l_len > maxoffset) {
3647c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
3657c478bd9Sstevel@tonic-gate 				break;
3667c478bd9Sstevel@tonic-gate 			}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 			if (datamodel == DATAMODEL_NATIVE) {
3697c478bd9Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
3707c478bd9Sstevel@tonic-gate 					sbf.l_pad[i] = 0;
3717c478bd9Sstevel@tonic-gate 				/*
3727c478bd9Sstevel@tonic-gate 				 * XXX	In an LP64 kernel with an LP64
3737c478bd9Sstevel@tonic-gate 				 *	application there's no need to do a
3747c478bd9Sstevel@tonic-gate 				 *	structure copy here as currently
3757c478bd9Sstevel@tonic-gate 				 *	struct flock == struct flock64.
3767c478bd9Sstevel@tonic-gate 				 *	We did it this way to avoid more
3777c478bd9Sstevel@tonic-gate 				 *	conditional compilation.
3787c478bd9Sstevel@tonic-gate 				 */
3797c478bd9Sstevel@tonic-gate 				sbf.l_type = bf.l_type;
3807c478bd9Sstevel@tonic-gate 				sbf.l_whence = bf.l_whence;
3817c478bd9Sstevel@tonic-gate 				sbf.l_start = (off_t)bf.l_start;
3827c478bd9Sstevel@tonic-gate 				sbf.l_len = (off_t)bf.l_len;
3837c478bd9Sstevel@tonic-gate 				sbf.l_sysid = bf.l_sysid;
3847c478bd9Sstevel@tonic-gate 				sbf.l_pid = bf.l_pid;
3857c478bd9Sstevel@tonic-gate 				if (copyout(&sbf, (void *)arg, sizeof (sbf)))
3867c478bd9Sstevel@tonic-gate 					error = EFAULT;
3877c478bd9Sstevel@tonic-gate 			}
3887c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
3897c478bd9Sstevel@tonic-gate 			else {
3907c478bd9Sstevel@tonic-gate 				struct flock32 sbf32;
3917c478bd9Sstevel@tonic-gate 				if (bf.l_start > MAXOFF32_T ||
3927c478bd9Sstevel@tonic-gate 				    bf.l_len > MAXOFF32_T) {
3937c478bd9Sstevel@tonic-gate 					error = EOVERFLOW;
3947c478bd9Sstevel@tonic-gate 					break;
3957c478bd9Sstevel@tonic-gate 				}
3967c478bd9Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
3977c478bd9Sstevel@tonic-gate 					sbf32.l_pad[i] = 0;
3987c478bd9Sstevel@tonic-gate 				sbf32.l_type = (int16_t)bf.l_type;
3997c478bd9Sstevel@tonic-gate 				sbf32.l_whence = (int16_t)bf.l_whence;
4007c478bd9Sstevel@tonic-gate 				sbf32.l_start = (off32_t)bf.l_start;
4017c478bd9Sstevel@tonic-gate 				sbf32.l_len = (off32_t)bf.l_len;
4027c478bd9Sstevel@tonic-gate 				sbf32.l_sysid = (int32_t)bf.l_sysid;
4037c478bd9Sstevel@tonic-gate 				sbf32.l_pid = (pid32_t)bf.l_pid;
4047c478bd9Sstevel@tonic-gate 				if (copyout(&sbf32,
4057c478bd9Sstevel@tonic-gate 				    (void *)arg, sizeof (sbf32)))
4067c478bd9Sstevel@tonic-gate 					error = EFAULT;
4077c478bd9Sstevel@tonic-gate 			}
4087c478bd9Sstevel@tonic-gate #endif
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 		break;
4117c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	case F_CHKFL:
4147c478bd9Sstevel@tonic-gate 		/*
4157c478bd9Sstevel@tonic-gate 		 * This is for internal use only, to allow the vnode layer
4167c478bd9Sstevel@tonic-gate 		 * to validate a flags setting before applying it.  User
4177c478bd9Sstevel@tonic-gate 		 * programs can't issue it.
4187c478bd9Sstevel@tonic-gate 		 */
4197c478bd9Sstevel@tonic-gate 		error = EINVAL;
4207c478bd9Sstevel@tonic-gate 		break;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	case F_ALLOCSP:
4237c478bd9Sstevel@tonic-gate 	case F_FREESP:
424303bf60bSsdebnath 	case F_ALLOCSP64:
425303bf60bSsdebnath 	case F_FREESP64:
4267c478bd9Sstevel@tonic-gate 		if ((flag & FWRITE) == 0) {
4277c478bd9Sstevel@tonic-gate 			error = EBADF;
4287c478bd9Sstevel@tonic-gate 			break;
4297c478bd9Sstevel@tonic-gate 		}
430303bf60bSsdebnath 
4317c478bd9Sstevel@tonic-gate 		if (vp->v_type != VREG) {
4327c478bd9Sstevel@tonic-gate 			error = EINVAL;
4337c478bd9Sstevel@tonic-gate 			break;
4347c478bd9Sstevel@tonic-gate 		}
4357c478bd9Sstevel@tonic-gate 
436303bf60bSsdebnath 		if (datamodel != DATAMODEL_ILP32 &&
437303bf60bSsdebnath 		    (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
438303bf60bSsdebnath 			error = EINVAL;
439303bf60bSsdebnath 			break;
440303bf60bSsdebnath 		}
441303bf60bSsdebnath 
4427c478bd9Sstevel@tonic-gate #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
443303bf60bSsdebnath 		if (datamodel == DATAMODEL_ILP32 &&
444303bf60bSsdebnath 		    (cmd == F_ALLOCSP || cmd == F_FREESP)) {
4457c478bd9Sstevel@tonic-gate 			struct flock32 sbf32;
4467c478bd9Sstevel@tonic-gate 			/*
4477c478bd9Sstevel@tonic-gate 			 * For compatibility we overlay an SVR3 flock on an SVR4
4487c478bd9Sstevel@tonic-gate 			 * flock.  This works because the input field offsets
4497c478bd9Sstevel@tonic-gate 			 * in "struct flock" were preserved.
4507c478bd9Sstevel@tonic-gate 			 */
4517c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
4527c478bd9Sstevel@tonic-gate 				error = EFAULT;
4537c478bd9Sstevel@tonic-gate 				break;
4547c478bd9Sstevel@tonic-gate 			} else {
4557c478bd9Sstevel@tonic-gate 				bf.l_type = sbf32.l_type;
4567c478bd9Sstevel@tonic-gate 				bf.l_whence = sbf32.l_whence;
4577c478bd9Sstevel@tonic-gate 				bf.l_start = (off64_t)sbf32.l_start;
4587c478bd9Sstevel@tonic-gate 				bf.l_len = (off64_t)sbf32.l_len;
4597c478bd9Sstevel@tonic-gate 				bf.l_sysid = sbf32.l_sysid;
4607c478bd9Sstevel@tonic-gate 				bf.l_pid = sbf32.l_pid;
4617c478bd9Sstevel@tonic-gate 			}
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate #endif /* _ILP32 || _SYSCALL32_IMPL */
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate #if defined(_LP64)
466303bf60bSsdebnath 		if (datamodel == DATAMODEL_LP64 &&
467303bf60bSsdebnath 		    (cmd == F_ALLOCSP || cmd == F_FREESP)) {
4687c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &bf, sizeof (bf))) {
4697c478bd9Sstevel@tonic-gate 				error = EFAULT;
4707c478bd9Sstevel@tonic-gate 				break;
4717c478bd9Sstevel@tonic-gate 			}
4727c478bd9Sstevel@tonic-gate 		}
473303bf60bSsdebnath #endif /* defined(_LP64) */
4747c478bd9Sstevel@tonic-gate 
475303bf60bSsdebnath #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
476303bf60bSsdebnath 		if (datamodel == DATAMODEL_ILP32 &&
477303bf60bSsdebnath 		    (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
478303bf60bSsdebnath 			if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
479303bf60bSsdebnath 				error = EFAULT;
480303bf60bSsdebnath 				break;
481303bf60bSsdebnath 			} else {
482303bf60bSsdebnath 				/*
483303bf60bSsdebnath 				 * Note that the size of flock64 is different in
484303bf60bSsdebnath 				 * the ILP32 and LP64 models, due to the l_pad
485303bf60bSsdebnath 				 * field. We do not want to assume that the
486303bf60bSsdebnath 				 * flock64 structure is laid out the same in
487303bf60bSsdebnath 				 * ILP32 and LP64 environments, so we will
488303bf60bSsdebnath 				 * copy in the ILP32 version of flock64
489303bf60bSsdebnath 				 * explicitly and copy it to the native
490303bf60bSsdebnath 				 * flock64 structure.
491303bf60bSsdebnath 				 */
492303bf60bSsdebnath 				bf.l_type = (short)bf64_32.l_type;
493303bf60bSsdebnath 				bf.l_whence = (short)bf64_32.l_whence;
494303bf60bSsdebnath 				bf.l_start = bf64_32.l_start;
495303bf60bSsdebnath 				bf.l_len = bf64_32.l_len;
496303bf60bSsdebnath 				bf.l_sysid = (int)bf64_32.l_sysid;
497303bf60bSsdebnath 				bf.l_pid = (pid_t)bf64_32.l_pid;
498303bf60bSsdebnath 			}
499303bf60bSsdebnath 		}
500303bf60bSsdebnath #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
501303bf60bSsdebnath 
502303bf60bSsdebnath 		if (cmd == F_ALLOCSP || cmd == F_FREESP)
503303bf60bSsdebnath 			error = flock_check(vp, &bf, offset, maxoffset);
504303bf60bSsdebnath 		else if (cmd == F_ALLOCSP64 || cmd == F_FREESP64)
505303bf60bSsdebnath 			error = flock_check(vp, &bf, offset, MAXOFFSET_T);
506303bf60bSsdebnath 		if (error)
5077c478bd9Sstevel@tonic-gate 			break;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG && bf.l_len == 0 &&
5107c478bd9Sstevel@tonic-gate 		    bf.l_start > OFFSET_MAX(fp)) {
5117c478bd9Sstevel@tonic-gate 			error = EFBIG;
5127c478bd9Sstevel@tonic-gate 			break;
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 		/*
5167c478bd9Sstevel@tonic-gate 		 * Make sure that there are no conflicting non-blocking
5177c478bd9Sstevel@tonic-gate 		 * mandatory locks in the region being manipulated. If
5187c478bd9Sstevel@tonic-gate 		 * there are such locks then return EACCES.
5197c478bd9Sstevel@tonic-gate 		 */
5207c478bd9Sstevel@tonic-gate 		if ((error = flock_get_start(vp, &bf, offset, &start)) != 0)
5217c478bd9Sstevel@tonic-gate 			break;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		if (nbl_need_check(vp)) {
5247c478bd9Sstevel@tonic-gate 			u_offset_t	begin;
5257c478bd9Sstevel@tonic-gate 			ssize_t		length;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 			nbl_start_crit(vp, RW_READER);
5287c478bd9Sstevel@tonic-gate 			in_crit = 1;
5297c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_SIZE;
5307c478bd9Sstevel@tonic-gate 			if ((error = VOP_GETATTR(vp, &vattr, 0, CRED())) != 0)
5317c478bd9Sstevel@tonic-gate 				break;
5327c478bd9Sstevel@tonic-gate 			begin = start > vattr.va_size ? vattr.va_size : start;
5337c478bd9Sstevel@tonic-gate 			length = vattr.va_size > start ? vattr.va_size - start :
5347c478bd9Sstevel@tonic-gate 				start - vattr.va_size;
5357c478bd9Sstevel@tonic-gate 			if (nbl_conflict(vp, NBL_WRITE, begin, length, 0)) {
5367c478bd9Sstevel@tonic-gate 				error = EACCES;
5377c478bd9Sstevel@tonic-gate 				break;
5387c478bd9Sstevel@tonic-gate 			}
5397c478bd9Sstevel@tonic-gate 		}
540303bf60bSsdebnath 
541303bf60bSsdebnath 		if (cmd == F_ALLOCSP64)
542303bf60bSsdebnath 			cmd = F_ALLOCSP;
543303bf60bSsdebnath 		else if (cmd == F_FREESP64)
544303bf60bSsdebnath 			cmd = F_FREESP;
545303bf60bSsdebnath 
5467c478bd9Sstevel@tonic-gate 		error = VOP_SPACE(vp, cmd, &bf, flag, offset, fp->f_cred, NULL);
547303bf60bSsdebnath 
5487c478bd9Sstevel@tonic-gate 		break;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
5517c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
5527c478bd9Sstevel@tonic-gate 	case F_GETLK64:
5537c478bd9Sstevel@tonic-gate 	case F_SETLK64:
5547c478bd9Sstevel@tonic-gate 	case F_SETLKW64:
5557c478bd9Sstevel@tonic-gate 	case F_SETLK64_NBMAND:
5567c478bd9Sstevel@tonic-gate 		/*
5577c478bd9Sstevel@tonic-gate 		 * Large Files: Here we set cmd as *LK and send it to
5587c478bd9Sstevel@tonic-gate 		 * lower layers. *LK64 is only for the user land.
5597c478bd9Sstevel@tonic-gate 		 * Most of the comments described above for F_SETLK
5607c478bd9Sstevel@tonic-gate 		 * applies here too.
5617c478bd9Sstevel@tonic-gate 		 * Large File support is only needed for ILP32 apps!
5627c478bd9Sstevel@tonic-gate 		 */
5637c478bd9Sstevel@tonic-gate 		if (datamodel != DATAMODEL_ILP32) {
5647c478bd9Sstevel@tonic-gate 			error = EINVAL;
5657c478bd9Sstevel@tonic-gate 			break;
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 		if (cmd == F_GETLK64)
5697c478bd9Sstevel@tonic-gate 			cmd = F_GETLK;
5707c478bd9Sstevel@tonic-gate 		else if (cmd == F_SETLK64)
5717c478bd9Sstevel@tonic-gate 			cmd = F_SETLK;
5727c478bd9Sstevel@tonic-gate 		else if (cmd == F_SETLKW64)
5737c478bd9Sstevel@tonic-gate 			cmd = F_SETLKW;
5747c478bd9Sstevel@tonic-gate 		else if (cmd == F_SETLK64_NBMAND)
5757c478bd9Sstevel@tonic-gate 			cmd = F_SETLK_NBMAND;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		/*
5787c478bd9Sstevel@tonic-gate 		 * Note that the size of flock64 is different in the ILP32
5797c478bd9Sstevel@tonic-gate 		 * and LP64 models, due to the sucking l_pad field.
5807c478bd9Sstevel@tonic-gate 		 * We do not want to assume that the flock64 structure is
5817c478bd9Sstevel@tonic-gate 		 * laid out in the same in ILP32 and LP64 environments, so
5827c478bd9Sstevel@tonic-gate 		 * we will copy in the ILP32 version of flock64 explicitly
5837c478bd9Sstevel@tonic-gate 		 * and copy it to the native flock64 structure.
5847c478bd9Sstevel@tonic-gate 		 */
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
5877c478bd9Sstevel@tonic-gate 			error = EFAULT;
5887c478bd9Sstevel@tonic-gate 			break;
5897c478bd9Sstevel@tonic-gate 		}
590303bf60bSsdebnath 
5917c478bd9Sstevel@tonic-gate 		bf.l_type = (short)bf64_32.l_type;
5927c478bd9Sstevel@tonic-gate 		bf.l_whence = (short)bf64_32.l_whence;
5937c478bd9Sstevel@tonic-gate 		bf.l_start = bf64_32.l_start;
5947c478bd9Sstevel@tonic-gate 		bf.l_len = bf64_32.l_len;
5957c478bd9Sstevel@tonic-gate 		bf.l_sysid = (int)bf64_32.l_sysid;
5967c478bd9Sstevel@tonic-gate 		bf.l_pid = (pid_t)bf64_32.l_pid;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 		if ((error = flock_check(vp, &bf, offset, MAXOFFSET_T)) != 0)
5997c478bd9Sstevel@tonic-gate 			break;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		if ((error = VOP_FRLOCK(vp, cmd, &bf, flag, offset,
6027c478bd9Sstevel@tonic-gate 		    NULL, fp->f_cred)) != 0)
6037c478bd9Sstevel@tonic-gate 			break;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 		if ((cmd == F_GETLK) && bf.l_type == F_UNLCK) {
6067c478bd9Sstevel@tonic-gate 			if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
6077c478bd9Sstevel@tonic-gate 			    sizeof (bf.l_type)))
6087c478bd9Sstevel@tonic-gate 				error = EFAULT;
6097c478bd9Sstevel@tonic-gate 			break;
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		if (cmd == F_GETLK) {
6137c478bd9Sstevel@tonic-gate 			int i;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 			/*
6167c478bd9Sstevel@tonic-gate 			 * We do not want to assume that the flock64 structure
6177c478bd9Sstevel@tonic-gate 			 * is laid out in the same in ILP32 and LP64
6187c478bd9Sstevel@tonic-gate 			 * environments, so we will copy out the ILP32 version
6197c478bd9Sstevel@tonic-gate 			 * of flock64 explicitly after copying the native
6207c478bd9Sstevel@tonic-gate 			 * flock64 structure to it.
6217c478bd9Sstevel@tonic-gate 			 */
6227c478bd9Sstevel@tonic-gate 			for (i = 0; i < 4; i++)
6237c478bd9Sstevel@tonic-gate 				bf64_32.l_pad[i] = 0;
6247c478bd9Sstevel@tonic-gate 			bf64_32.l_type = (int16_t)bf.l_type;
6257c478bd9Sstevel@tonic-gate 			bf64_32.l_whence = (int16_t)bf.l_whence;
6267c478bd9Sstevel@tonic-gate 			bf64_32.l_start = bf.l_start;
6277c478bd9Sstevel@tonic-gate 			bf64_32.l_len = bf.l_len;
6287c478bd9Sstevel@tonic-gate 			bf64_32.l_sysid = (int32_t)bf.l_sysid;
6297c478bd9Sstevel@tonic-gate 			bf64_32.l_pid = (pid32_t)bf.l_pid;
6307c478bd9Sstevel@tonic-gate 			if (copyout(&bf64_32, (void *)arg, sizeof (bf64_32)))
6317c478bd9Sstevel@tonic-gate 				error = EFAULT;
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 		break;
6347c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
635303bf60bSsdebnath #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
6387c478bd9Sstevel@tonic-gate 	case F_SHARE:
6397c478bd9Sstevel@tonic-gate 	case F_SHARE_NBMAND:
6407c478bd9Sstevel@tonic-gate 	case F_UNSHARE:
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		/*
6437c478bd9Sstevel@tonic-gate 		 * Copy in input fields only.
6447c478bd9Sstevel@tonic-gate 		 */
6457c478bd9Sstevel@tonic-gate 		if (copyin((void *)arg, &fsh, sizeof (fsh))) {
6467c478bd9Sstevel@tonic-gate 			error = EFAULT;
6477c478bd9Sstevel@tonic-gate 			break;
6487c478bd9Sstevel@tonic-gate 		}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		/*
6517c478bd9Sstevel@tonic-gate 		 * Local share reservations always have this simple form
6527c478bd9Sstevel@tonic-gate 		 */
6537c478bd9Sstevel@tonic-gate 		shr.s_access = fsh.f_access;
6547c478bd9Sstevel@tonic-gate 		shr.s_deny = fsh.f_deny;
6557c478bd9Sstevel@tonic-gate 		shr.s_sysid = 0;
6567c478bd9Sstevel@tonic-gate 		shr.s_pid = ttoproc(curthread)->p_pid;
6577c478bd9Sstevel@tonic-gate 		shr_own.sl_pid = shr.s_pid;
6587c478bd9Sstevel@tonic-gate 		shr_own.sl_id = fsh.f_id;
6597c478bd9Sstevel@tonic-gate 		shr.s_own_len = sizeof (shr_own);
6607c478bd9Sstevel@tonic-gate 		shr.s_owner = (caddr_t)&shr_own;
6617c478bd9Sstevel@tonic-gate 		error = VOP_SHRLOCK(vp, cmd, &shr, flag, fp->f_cred);
6627c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
6637c478bd9Sstevel@tonic-gate 		break;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	default:
6667c478bd9Sstevel@tonic-gate 		error = EINVAL;
6677c478bd9Sstevel@tonic-gate 		break;
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	if (in_crit)
6717c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate done:
6747c478bd9Sstevel@tonic-gate 	releasef(fdes);
6757c478bd9Sstevel@tonic-gate out:
6767c478bd9Sstevel@tonic-gate 	if (error)
6777c478bd9Sstevel@tonic-gate 		return (set_errno(error));
6787c478bd9Sstevel@tonic-gate 	return (retval);
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate int
6827c478bd9Sstevel@tonic-gate dup(int fd)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	return (fcntl(fd, F_DUPFD, 0));
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
6887c478bd9Sstevel@tonic-gate int
6897c478bd9Sstevel@tonic-gate flock_check(vnode_t *vp, flock64_t *flp, offset_t offset, offset_t max)
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate 	struct vattr	vattr;
6927c478bd9Sstevel@tonic-gate 	int	error;
6937c478bd9Sstevel@tonic-gate 	u_offset_t start, end;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	/*
6967c478bd9Sstevel@tonic-gate 	 * Determine the starting point of the request
6977c478bd9Sstevel@tonic-gate 	 */
6987c478bd9Sstevel@tonic-gate 	switch (flp->l_whence) {
6997c478bd9Sstevel@tonic-gate 	case 0:		/* SEEK_SET */
7007c478bd9Sstevel@tonic-gate 		start = (u_offset_t)flp->l_start;
7017c478bd9Sstevel@tonic-gate 		if (start > max)
7027c478bd9Sstevel@tonic-gate 			return (EINVAL);
7037c478bd9Sstevel@tonic-gate 		break;
7047c478bd9Sstevel@tonic-gate 	case 1:		/* SEEK_CUR */
7057c478bd9Sstevel@tonic-gate 		if (flp->l_start > (max - offset))
7067c478bd9Sstevel@tonic-gate 			return (EOVERFLOW);
7077c478bd9Sstevel@tonic-gate 		start = (u_offset_t)(flp->l_start + offset);
7087c478bd9Sstevel@tonic-gate 		if (start > max)
7097c478bd9Sstevel@tonic-gate 			return (EINVAL);
7107c478bd9Sstevel@tonic-gate 		break;
7117c478bd9Sstevel@tonic-gate 	case 2:		/* SEEK_END */
7127c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_SIZE;
7137c478bd9Sstevel@tonic-gate 		if (error = VOP_GETATTR(vp, &vattr, 0, CRED()))
7147c478bd9Sstevel@tonic-gate 			return (error);
7157c478bd9Sstevel@tonic-gate 		if (flp->l_start > (max - (offset_t)vattr.va_size))
7167c478bd9Sstevel@tonic-gate 			return (EOVERFLOW);
7177c478bd9Sstevel@tonic-gate 		start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
7187c478bd9Sstevel@tonic-gate 		if (start > max)
7197c478bd9Sstevel@tonic-gate 			return (EINVAL);
7207c478bd9Sstevel@tonic-gate 		break;
7217c478bd9Sstevel@tonic-gate 	default:
7227c478bd9Sstevel@tonic-gate 		return (EINVAL);
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	/*
7267c478bd9Sstevel@tonic-gate 	 * Determine the range covered by the request.
7277c478bd9Sstevel@tonic-gate 	 */
7287c478bd9Sstevel@tonic-gate 	if (flp->l_len == 0)
7297c478bd9Sstevel@tonic-gate 		end = MAXEND;
7307c478bd9Sstevel@tonic-gate 	else if ((offset_t)flp->l_len > 0) {
7317c478bd9Sstevel@tonic-gate 		if (flp->l_len > (max - start + 1))
7327c478bd9Sstevel@tonic-gate 			return (EOVERFLOW);
7337c478bd9Sstevel@tonic-gate 		end = (u_offset_t)(start + (flp->l_len - 1));
7347c478bd9Sstevel@tonic-gate 		ASSERT(end <= max);
7357c478bd9Sstevel@tonic-gate 	} else {
7367c478bd9Sstevel@tonic-gate 		/*
7377c478bd9Sstevel@tonic-gate 		 * Negative length; why do we even allow this ?
7387c478bd9Sstevel@tonic-gate 		 * Because this allows easy specification of
7397c478bd9Sstevel@tonic-gate 		 * the last n bytes of the file.
7407c478bd9Sstevel@tonic-gate 		 */
7417c478bd9Sstevel@tonic-gate 		end = start;
7427c478bd9Sstevel@tonic-gate 		start += (u_offset_t)flp->l_len;
7437c478bd9Sstevel@tonic-gate 		(start)++;
7447c478bd9Sstevel@tonic-gate 		if (start > max)
7457c478bd9Sstevel@tonic-gate 			return (EINVAL);
7467c478bd9Sstevel@tonic-gate 		ASSERT(end <= max);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	ASSERT(start <= max);
7497c478bd9Sstevel@tonic-gate 	if (flp->l_type == F_UNLCK && flp->l_len > 0 &&
7507c478bd9Sstevel@tonic-gate 	    end == (offset_t)max) {
7517c478bd9Sstevel@tonic-gate 		flp->l_len = 0;
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 	if (start  > end)
7547c478bd9Sstevel@tonic-gate 		return (EINVAL);
7557c478bd9Sstevel@tonic-gate 	return (0);
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate static int
7597c478bd9Sstevel@tonic-gate flock_get_start(vnode_t *vp, flock64_t *flp, offset_t offset, u_offset_t *start)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	struct vattr	vattr;
7627c478bd9Sstevel@tonic-gate 	int	error;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	/*
7657c478bd9Sstevel@tonic-gate 	 * Determine the starting point of the request. Assume that it is
7667c478bd9Sstevel@tonic-gate 	 * a valid starting point.
7677c478bd9Sstevel@tonic-gate 	 */
7687c478bd9Sstevel@tonic-gate 	switch (flp->l_whence) {
7697c478bd9Sstevel@tonic-gate 	case 0:		/* SEEK_SET */
7707c478bd9Sstevel@tonic-gate 		*start = (u_offset_t)flp->l_start;
7717c478bd9Sstevel@tonic-gate 		break;
7727c478bd9Sstevel@tonic-gate 	case 1:		/* SEEK_CUR */
7737c478bd9Sstevel@tonic-gate 		*start = (u_offset_t)(flp->l_start + offset);
7747c478bd9Sstevel@tonic-gate 		break;
7757c478bd9Sstevel@tonic-gate 	case 2:		/* SEEK_END */
7767c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_SIZE;
7777c478bd9Sstevel@tonic-gate 		if (error = VOP_GETATTR(vp, &vattr, 0, CRED()))
7787c478bd9Sstevel@tonic-gate 			return (error);
7797c478bd9Sstevel@tonic-gate 		*start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
7807c478bd9Sstevel@tonic-gate 		break;
7817c478bd9Sstevel@tonic-gate 	default:
7827c478bd9Sstevel@tonic-gate 		return (EINVAL);
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	return (0);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate /*
7897c478bd9Sstevel@tonic-gate  * Take rctl action when the requested file descriptor is too big.
7907c478bd9Sstevel@tonic-gate  */
7917c478bd9Sstevel@tonic-gate static void
7927c478bd9Sstevel@tonic-gate fd_too_big(proc_t *p)
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
7957c478bd9Sstevel@tonic-gate 	(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
7967c478bd9Sstevel@tonic-gate 	    p->p_rctls, p, RCA_SAFE);
7977c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
800