xref: /titanic_52/usr/src/uts/common/syscall/fcntl.c (revision 7a5aac98bc37534537d4896efd4efd30627d221e)
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
5a5f69788Scraigm  * Common Development and Distribution License (the "License").
6a5f69788Scraigm  * 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  */
21019c3c43Sraf 
227c478bd9Sstevel@tonic-gate /*
23c0e499e0SMilan Cermak  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
245dbfd19aSTheo Schlossnagle  * Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved.
25*7a5aac98SJerry Jelinek  * Copyright 2015, Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
337c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
365dbfd19aSTheo Schlossnagle 
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/sysmacros.h>
417c478bd9Sstevel@tonic-gate #include <sys/systm.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
447c478bd9Sstevel@tonic-gate #include <sys/flock.h>
457c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
467c478bd9Sstevel@tonic-gate #include <sys/file.h>
477c478bd9Sstevel@tonic-gate #include <sys/mode.h>
487c478bd9Sstevel@tonic-gate #include <sys/proc.h>
497c478bd9Sstevel@tonic-gate #include <sys/filio.h>
507c478bd9Sstevel@tonic-gate #include <sys/share.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 
55303bf60bSsdebnath #include <sys/cmn_err.h>
56303bf60bSsdebnath 
577c478bd9Sstevel@tonic-gate static int flock_check(vnode_t *, flock64_t *, offset_t, offset_t);
587c478bd9Sstevel@tonic-gate static int flock_get_start(vnode_t *, flock64_t *, offset_t, u_offset_t *);
597c478bd9Sstevel@tonic-gate static void fd_too_big(proc_t *);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * File control.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate int
657c478bd9Sstevel@tonic-gate fcntl(int fdes, int cmd, intptr_t arg)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	int iarg;
687c478bd9Sstevel@tonic-gate 	int error = 0;
697c478bd9Sstevel@tonic-gate 	int retval;
707c478bd9Sstevel@tonic-gate 	proc_t *p;
717c478bd9Sstevel@tonic-gate 	file_t *fp;
727c478bd9Sstevel@tonic-gate 	vnode_t *vp;
737c478bd9Sstevel@tonic-gate 	u_offset_t offset;
747c478bd9Sstevel@tonic-gate 	u_offset_t start;
757c478bd9Sstevel@tonic-gate 	struct vattr vattr;
767c478bd9Sstevel@tonic-gate 	int in_crit;
777c478bd9Sstevel@tonic-gate 	int flag;
787c478bd9Sstevel@tonic-gate 	struct flock sbf;
797c478bd9Sstevel@tonic-gate 	struct flock64 bf;
807c478bd9Sstevel@tonic-gate 	struct o_flock obf;
817c478bd9Sstevel@tonic-gate 	struct flock64_32 bf64_32;
827c478bd9Sstevel@tonic-gate 	struct fshare fsh;
837c478bd9Sstevel@tonic-gate 	struct shrlock shr;
847c478bd9Sstevel@tonic-gate 	struct shr_locowner shr_own;
857c478bd9Sstevel@tonic-gate 	offset_t maxoffset;
867c478bd9Sstevel@tonic-gate 	model_t datamodel;
87a5f69788Scraigm 	int fdres;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #if defined(_ILP32) && !defined(lint) && defined(_SYSCALL32)
907c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct flock) == sizeof (struct flock32));
917c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct flock64) == sizeof (struct flock64_32));
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate #if defined(_LP64) && !defined(lint) && defined(_SYSCALL32)
947c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct flock) == sizeof (struct flock64_64));
957c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct flock64) == sizeof (struct flock64_64));
967c478bd9Sstevel@tonic-gate #endif
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	/*
997c478bd9Sstevel@tonic-gate 	 * First, for speed, deal with the subset of cases
1007c478bd9Sstevel@tonic-gate 	 * that do not require getf() / releasef().
1017c478bd9Sstevel@tonic-gate 	 */
1027c478bd9Sstevel@tonic-gate 	switch (cmd) {
1037c478bd9Sstevel@tonic-gate 	case F_GETFD:
1047c478bd9Sstevel@tonic-gate 		if ((error = f_getfd_error(fdes, &flag)) == 0)
1057c478bd9Sstevel@tonic-gate 			retval = flag;
1067c478bd9Sstevel@tonic-gate 		goto out;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	case F_SETFD:
1097c478bd9Sstevel@tonic-gate 		error = f_setfd_error(fdes, (int)arg);
1107c478bd9Sstevel@tonic-gate 		retval = 0;
1117c478bd9Sstevel@tonic-gate 		goto out;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	case F_GETFL:
114794f0adbSRoger A. Faulkner 		if ((error = f_getfl(fdes, &flag)) == 0) {
115794f0adbSRoger A. Faulkner 			retval = (flag & (FMASK | FASYNC));
116794f0adbSRoger A. Faulkner 			if ((flag & (FSEARCH | FEXEC)) == 0)
117794f0adbSRoger A. Faulkner 				retval += FOPEN;
118794f0adbSRoger A. Faulkner 			else
119794f0adbSRoger A. Faulkner 				retval |= (flag & (FSEARCH | FEXEC));
120794f0adbSRoger A. Faulkner 		}
1217c478bd9Sstevel@tonic-gate 		goto out;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	case F_GETXFL:
124794f0adbSRoger A. Faulkner 		if ((error = f_getfl(fdes, &flag)) == 0) {
125794f0adbSRoger A. Faulkner 			retval = flag;
126794f0adbSRoger A. Faulkner 			if ((flag & (FSEARCH | FEXEC)) == 0)
127794f0adbSRoger A. Faulkner 				retval += FOPEN;
128794f0adbSRoger A. Faulkner 		}
1297c478bd9Sstevel@tonic-gate 		goto out;
130a5f69788Scraigm 
131a5f69788Scraigm 	case F_BADFD:
132a5f69788Scraigm 		if ((error = f_badfd(fdes, &fdres, (int)arg)) == 0)
133a5f69788Scraigm 			retval = fdres;
134a5f69788Scraigm 		goto out;
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	/*
1387c478bd9Sstevel@tonic-gate 	 * Second, for speed, deal with the subset of cases that
1397c478bd9Sstevel@tonic-gate 	 * require getf() / releasef() but do not require copyin.
1407c478bd9Sstevel@tonic-gate 	 */
1417c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL) {
1427c478bd9Sstevel@tonic-gate 		error = EBADF;
1437c478bd9Sstevel@tonic-gate 		goto out;
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 	iarg = (int)arg;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	switch (cmd) {
1487c478bd9Sstevel@tonic-gate 	case F_DUPFD:
1495dbfd19aSTheo Schlossnagle 	case F_DUPFD_CLOEXEC:
1507c478bd9Sstevel@tonic-gate 		p = curproc;
1517c478bd9Sstevel@tonic-gate 		if ((uint_t)iarg >= p->p_fno_ctl) {
1527c478bd9Sstevel@tonic-gate 			if (iarg >= 0)
1537c478bd9Sstevel@tonic-gate 				fd_too_big(p);
1547c478bd9Sstevel@tonic-gate 			error = EINVAL;
155c0e499e0SMilan Cermak 			goto done;
156c0e499e0SMilan Cermak 		}
157c0e499e0SMilan Cermak 		/*
158c0e499e0SMilan Cermak 		 * We need to increment the f_count reference counter
159c0e499e0SMilan Cermak 		 * before allocating a new file descriptor.
160c0e499e0SMilan Cermak 		 * Doing it other way round opens a window for race condition
161c0e499e0SMilan Cermak 		 * with closeandsetf() on the target file descriptor which can
162c0e499e0SMilan Cermak 		 * close the file still referenced by the original
163c0e499e0SMilan Cermak 		 * file descriptor.
164c0e499e0SMilan Cermak 		 */
1657c478bd9Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
1667c478bd9Sstevel@tonic-gate 		fp->f_count++;
1677c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
168c0e499e0SMilan Cermak 		if ((retval = ufalloc_file(iarg, fp)) == -1) {
169c0e499e0SMilan Cermak 			/*
170c0e499e0SMilan Cermak 			 * New file descriptor can't be allocated.
171c0e499e0SMilan Cermak 			 * Revert the reference count.
172c0e499e0SMilan Cermak 			 */
173c0e499e0SMilan Cermak 			mutex_enter(&fp->f_tlock);
174c0e499e0SMilan Cermak 			fp->f_count--;
175c0e499e0SMilan Cermak 			mutex_exit(&fp->f_tlock);
176c0e499e0SMilan Cermak 			error = EMFILE;
1775dbfd19aSTheo Schlossnagle 		} else {
1785dbfd19aSTheo Schlossnagle 			if (cmd == F_DUPFD_CLOEXEC) {
1795dbfd19aSTheo Schlossnagle 				f_setfd(retval, FD_CLOEXEC);
1805dbfd19aSTheo Schlossnagle 			}
1817c478bd9Sstevel@tonic-gate 		}
1827c478bd9Sstevel@tonic-gate 		goto done;
1837c478bd9Sstevel@tonic-gate 
1845dbfd19aSTheo Schlossnagle 	case F_DUP2FD_CLOEXEC:
1855dbfd19aSTheo Schlossnagle 		if (fdes == iarg) {
1865dbfd19aSTheo Schlossnagle 			error = EINVAL;
1875dbfd19aSTheo Schlossnagle 			goto done;
1885dbfd19aSTheo Schlossnagle 		}
1895dbfd19aSTheo Schlossnagle 
1905eaceb49STheo Schlossnagle 		/*FALLTHROUGH*/
1915dbfd19aSTheo Schlossnagle 
1927c478bd9Sstevel@tonic-gate 	case F_DUP2FD:
1937c478bd9Sstevel@tonic-gate 		p = curproc;
1947c478bd9Sstevel@tonic-gate 		if (fdes == iarg) {
1957c478bd9Sstevel@tonic-gate 			retval = iarg;
1967c478bd9Sstevel@tonic-gate 		} else if ((uint_t)iarg >= p->p_fno_ctl) {
1977c478bd9Sstevel@tonic-gate 			if (iarg >= 0)
1987c478bd9Sstevel@tonic-gate 				fd_too_big(p);
1997c478bd9Sstevel@tonic-gate 			error = EBADF;
2007c478bd9Sstevel@tonic-gate 		} else {
2017c478bd9Sstevel@tonic-gate 			/*
2027c478bd9Sstevel@tonic-gate 			 * We can't hold our getf(fdes) across the call to
2037c478bd9Sstevel@tonic-gate 			 * closeandsetf() because it creates a window for
2047c478bd9Sstevel@tonic-gate 			 * deadlock: if one thread is doing dup2(a, b) while
2057c478bd9Sstevel@tonic-gate 			 * another is doing dup2(b, a), each one will block
2067c478bd9Sstevel@tonic-gate 			 * waiting for the other to call releasef().  The
2077c478bd9Sstevel@tonic-gate 			 * solution is to increment the file reference count
2087c478bd9Sstevel@tonic-gate 			 * (which we have to do anyway), then releasef(fdes),
2097c478bd9Sstevel@tonic-gate 			 * then closeandsetf().  Incrementing f_count ensures
2107c478bd9Sstevel@tonic-gate 			 * that fp won't disappear after we call releasef().
211a5f69788Scraigm 			 * When closeandsetf() fails, we try avoid calling
212a5f69788Scraigm 			 * closef() because of all the side effects.
2137c478bd9Sstevel@tonic-gate 			 */
2147c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->f_tlock);
2157c478bd9Sstevel@tonic-gate 			fp->f_count++;
2167c478bd9Sstevel@tonic-gate 			mutex_exit(&fp->f_tlock);
2177c478bd9Sstevel@tonic-gate 			releasef(fdes);
218a5f69788Scraigm 			if ((error = closeandsetf(iarg, fp)) == 0) {
2195dbfd19aSTheo Schlossnagle 				if (cmd == F_DUP2FD_CLOEXEC) {
2205dbfd19aSTheo Schlossnagle 					f_setfd(iarg, FD_CLOEXEC);
2215dbfd19aSTheo Schlossnagle 				}
2227c478bd9Sstevel@tonic-gate 				retval = iarg;
223a5f69788Scraigm 			} else {
224a5f69788Scraigm 				mutex_enter(&fp->f_tlock);
225a5f69788Scraigm 				if (fp->f_count > 1) {
226a5f69788Scraigm 					fp->f_count--;
227a5f69788Scraigm 					mutex_exit(&fp->f_tlock);
228a5f69788Scraigm 				} else {
229a5f69788Scraigm 					mutex_exit(&fp->f_tlock);
230a5f69788Scraigm 					(void) closef(fp);
231a5f69788Scraigm 				}
232a5f69788Scraigm 			}
2337c478bd9Sstevel@tonic-gate 			goto out;
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 		goto done;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	case F_SETFL:
2387c478bd9Sstevel@tonic-gate 		vp = fp->f_vnode;
2397c478bd9Sstevel@tonic-gate 		flag = fp->f_flag;
2407c478bd9Sstevel@tonic-gate 		if ((iarg & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY))
2417c478bd9Sstevel@tonic-gate 			iarg &= ~FNDELAY;
242da6c28aaSamw 		if ((error = VOP_SETFL(vp, flag, iarg, fp->f_cred, NULL)) ==
243da6c28aaSamw 		    0) {
2447c478bd9Sstevel@tonic-gate 			iarg &= FMASK;
2457c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->f_tlock);
2467c478bd9Sstevel@tonic-gate 			fp->f_flag &= ~FMASK | (FREAD|FWRITE);
2477c478bd9Sstevel@tonic-gate 			fp->f_flag |= (iarg - FOPEN) & ~(FREAD|FWRITE);
2487c478bd9Sstevel@tonic-gate 			mutex_exit(&fp->f_tlock);
2497c478bd9Sstevel@tonic-gate 		}
2507c478bd9Sstevel@tonic-gate 		retval = 0;
2517c478bd9Sstevel@tonic-gate 		goto done;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * Finally, deal with the expensive cases.
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate 	retval = 0;
2587c478bd9Sstevel@tonic-gate 	in_crit = 0;
2597c478bd9Sstevel@tonic-gate 	maxoffset = MAXOFF_T;
2607c478bd9Sstevel@tonic-gate 	datamodel = DATAMODEL_NATIVE;
2617c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
2627c478bd9Sstevel@tonic-gate 	if ((datamodel = get_udatamodel()) == DATAMODEL_ILP32)
2637c478bd9Sstevel@tonic-gate 		maxoffset = MAXOFF32_T;
2647c478bd9Sstevel@tonic-gate #endif
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
2677c478bd9Sstevel@tonic-gate 	flag = fp->f_flag;
2687c478bd9Sstevel@tonic-gate 	offset = fp->f_offset;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	switch (cmd) {
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * The file system and vnode layers understand and implement
2737c478bd9Sstevel@tonic-gate 	 * locking with flock64 structures. So here once we pass through
2747c478bd9Sstevel@tonic-gate 	 * the test for compatibility as defined by LFS API, (for F_SETLK,
275*7a5aac98SJerry Jelinek 	 * F_SETLKW, F_GETLK, F_GETLKW, F_OFD_GETLK, F_OFD_SETLK, F_OFD_SETLKW,
276*7a5aac98SJerry Jelinek 	 * F_FREESP) we transform the flock structure to a flock64 structure
277*7a5aac98SJerry Jelinek 	 * and send it to the lower layers. Similarly in case of GETLK and
278*7a5aac98SJerry Jelinek 	 * OFD_GETLK the returned flock64 structure is transformed to a flock
279*7a5aac98SJerry Jelinek 	 * structure if everything fits in nicely, otherwise we return
280*7a5aac98SJerry Jelinek 	 * EOVERFLOW.
2817c478bd9Sstevel@tonic-gate 	 */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	case F_GETLK:
2847c478bd9Sstevel@tonic-gate 	case F_O_GETLK:
2857c478bd9Sstevel@tonic-gate 	case F_SETLK:
2867c478bd9Sstevel@tonic-gate 	case F_SETLKW:
2877c478bd9Sstevel@tonic-gate 	case F_SETLK_NBMAND:
288*7a5aac98SJerry Jelinek 	case F_OFD_GETLK:
289*7a5aac98SJerry Jelinek 	case F_OFD_SETLK:
290*7a5aac98SJerry Jelinek 	case F_OFD_SETLKW:
291*7a5aac98SJerry Jelinek 	case F_FLOCK:
292*7a5aac98SJerry Jelinek 	case F_FLOCKW:
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		/*
2957c478bd9Sstevel@tonic-gate 		 * Copy in input fields only.
2967c478bd9Sstevel@tonic-gate 		 */
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		if (cmd == F_O_GETLK) {
2997c478bd9Sstevel@tonic-gate 			if (datamodel != DATAMODEL_ILP32) {
3007c478bd9Sstevel@tonic-gate 				error = EINVAL;
3017c478bd9Sstevel@tonic-gate 				break;
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &obf, sizeof (obf))) {
3057c478bd9Sstevel@tonic-gate 				error = EFAULT;
3067c478bd9Sstevel@tonic-gate 				break;
3077c478bd9Sstevel@tonic-gate 			}
3087c478bd9Sstevel@tonic-gate 			bf.l_type = obf.l_type;
3097c478bd9Sstevel@tonic-gate 			bf.l_whence = obf.l_whence;
3107c478bd9Sstevel@tonic-gate 			bf.l_start = (off64_t)obf.l_start;
3117c478bd9Sstevel@tonic-gate 			bf.l_len = (off64_t)obf.l_len;
3127c478bd9Sstevel@tonic-gate 			bf.l_sysid = (int)obf.l_sysid;
3137c478bd9Sstevel@tonic-gate 			bf.l_pid = obf.l_pid;
3147c478bd9Sstevel@tonic-gate 		} else if (datamodel == DATAMODEL_NATIVE) {
3157c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &sbf, sizeof (sbf))) {
3167c478bd9Sstevel@tonic-gate 				error = EFAULT;
3177c478bd9Sstevel@tonic-gate 				break;
3187c478bd9Sstevel@tonic-gate 			}
3197c478bd9Sstevel@tonic-gate 			/*
3207c478bd9Sstevel@tonic-gate 			 * XXX	In an LP64 kernel with an LP64 application
3217c478bd9Sstevel@tonic-gate 			 *	there's no need to do a structure copy here
3227c478bd9Sstevel@tonic-gate 			 *	struct flock == struct flock64. However,
3237c478bd9Sstevel@tonic-gate 			 *	we did it this way to avoid more conditional
3247c478bd9Sstevel@tonic-gate 			 *	compilation.
3257c478bd9Sstevel@tonic-gate 			 */
3267c478bd9Sstevel@tonic-gate 			bf.l_type = sbf.l_type;
3277c478bd9Sstevel@tonic-gate 			bf.l_whence = sbf.l_whence;
3287c478bd9Sstevel@tonic-gate 			bf.l_start = (off64_t)sbf.l_start;
3297c478bd9Sstevel@tonic-gate 			bf.l_len = (off64_t)sbf.l_len;
3307c478bd9Sstevel@tonic-gate 			bf.l_sysid = sbf.l_sysid;
3317c478bd9Sstevel@tonic-gate 			bf.l_pid = sbf.l_pid;
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
3347c478bd9Sstevel@tonic-gate 		else {
3357c478bd9Sstevel@tonic-gate 			struct flock32 sbf32;
3367c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
3377c478bd9Sstevel@tonic-gate 				error = EFAULT;
3387c478bd9Sstevel@tonic-gate 				break;
3397c478bd9Sstevel@tonic-gate 			}
3407c478bd9Sstevel@tonic-gate 			bf.l_type = sbf32.l_type;
3417c478bd9Sstevel@tonic-gate 			bf.l_whence = sbf32.l_whence;
3427c478bd9Sstevel@tonic-gate 			bf.l_start = (off64_t)sbf32.l_start;
3437c478bd9Sstevel@tonic-gate 			bf.l_len = (off64_t)sbf32.l_len;
3447c478bd9Sstevel@tonic-gate 			bf.l_sysid = sbf32.l_sysid;
3457c478bd9Sstevel@tonic-gate 			bf.l_pid = sbf32.l_pid;
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		/*
3507c478bd9Sstevel@tonic-gate 		 * 64-bit support: check for overflow for 32-bit lock ops
3517c478bd9Sstevel@tonic-gate 		 */
3527c478bd9Sstevel@tonic-gate 		if ((error = flock_check(vp, &bf, offset, maxoffset)) != 0)
3537c478bd9Sstevel@tonic-gate 			break;
3547c478bd9Sstevel@tonic-gate 
355*7a5aac98SJerry Jelinek 		if (cmd == F_FLOCK || cmd == F_FLOCKW) {
356*7a5aac98SJerry Jelinek 			/* FLOCK* locking is always over the entire file. */
357*7a5aac98SJerry Jelinek 			if (bf.l_whence != 0 || bf.l_start != 0 ||
358*7a5aac98SJerry Jelinek 			    bf.l_len != 0) {
359*7a5aac98SJerry Jelinek 				error = EINVAL;
360*7a5aac98SJerry Jelinek 				break;
361*7a5aac98SJerry Jelinek 			}
362*7a5aac98SJerry Jelinek 			if (bf.l_type < F_RDLCK || bf.l_type > F_UNLCK) {
363*7a5aac98SJerry Jelinek 				error = EINVAL;
364*7a5aac98SJerry Jelinek 				break;
365*7a5aac98SJerry Jelinek 			}
366*7a5aac98SJerry Jelinek 		}
367*7a5aac98SJerry Jelinek 
368*7a5aac98SJerry Jelinek 		if (cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
369*7a5aac98SJerry Jelinek 			/*
370*7a5aac98SJerry Jelinek 			 * TBD OFD-style locking is currently limited to
371*7a5aac98SJerry Jelinek 			 * covering the entire file.
372*7a5aac98SJerry Jelinek 			 */
373*7a5aac98SJerry Jelinek 			if (bf.l_whence != 0 || bf.l_start != 0 ||
374*7a5aac98SJerry Jelinek 			    bf.l_len != 0) {
375*7a5aac98SJerry Jelinek 				error = EINVAL;
376*7a5aac98SJerry Jelinek 				break;
377*7a5aac98SJerry Jelinek 			}
378*7a5aac98SJerry Jelinek 		}
379*7a5aac98SJerry Jelinek 
3807c478bd9Sstevel@tonic-gate 		/*
3817c478bd9Sstevel@tonic-gate 		 * Not all of the filesystems understand F_O_GETLK, and
3827c478bd9Sstevel@tonic-gate 		 * there's no need for them to know.  Map it to F_GETLK.
383*7a5aac98SJerry Jelinek 		 *
384*7a5aac98SJerry Jelinek 		 * The *_frlock functions in the various file systems basically
385*7a5aac98SJerry Jelinek 		 * do some validation and then funnel everything through the
386*7a5aac98SJerry Jelinek 		 * fs_frlock function. For OFD-style locks fs_frlock will do
387*7a5aac98SJerry Jelinek 		 * nothing so that once control returns here we can call the
388*7a5aac98SJerry Jelinek 		 * ofdlock function with the correct fp. For OFD-style locks
389*7a5aac98SJerry Jelinek 		 * the unsupported remote file systems, such as NFS, detect and
390*7a5aac98SJerry Jelinek 		 * reject the OFD-style cmd argument.
3917c478bd9Sstevel@tonic-gate 		 */
3927c478bd9Sstevel@tonic-gate 		if ((error = VOP_FRLOCK(vp, (cmd == F_O_GETLK) ? F_GETLK : cmd,
393da6c28aaSamw 		    &bf, flag, offset, NULL, fp->f_cred, NULL)) != 0)
3947c478bd9Sstevel@tonic-gate 			break;
3957c478bd9Sstevel@tonic-gate 
396*7a5aac98SJerry Jelinek 		if (cmd == F_FLOCK || cmd == F_FLOCKW || cmd == F_OFD_GETLK ||
397*7a5aac98SJerry Jelinek 		    cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
398*7a5aac98SJerry Jelinek 			/*
399*7a5aac98SJerry Jelinek 			 * This is an OFD-style lock so we need to handle it
400*7a5aac98SJerry Jelinek 			 * here. Because OFD-style locks are associated with
401*7a5aac98SJerry Jelinek 			 * the file_t we didn't have enough info down the
402*7a5aac98SJerry Jelinek 			 * VOP_FRLOCK path immediately above.
403*7a5aac98SJerry Jelinek 			 */
404*7a5aac98SJerry Jelinek 			if ((error = ofdlock(fp, cmd, &bf, flag, offset)) != 0)
405*7a5aac98SJerry Jelinek 				break;
406*7a5aac98SJerry Jelinek 		}
407*7a5aac98SJerry Jelinek 
4087c478bd9Sstevel@tonic-gate 		/*
4097c478bd9Sstevel@tonic-gate 		 * If command is GETLK and no lock is found, only
4107c478bd9Sstevel@tonic-gate 		 * the type field is changed.
4117c478bd9Sstevel@tonic-gate 		 */
412*7a5aac98SJerry Jelinek 		if ((cmd == F_O_GETLK || cmd == F_GETLK ||
413*7a5aac98SJerry Jelinek 		    cmd == F_OFD_GETLK) && bf.l_type == F_UNLCK) {
4147c478bd9Sstevel@tonic-gate 			/* l_type always first entry, always a short */
4157c478bd9Sstevel@tonic-gate 			if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
4167c478bd9Sstevel@tonic-gate 			    sizeof (bf.l_type)))
4177c478bd9Sstevel@tonic-gate 				error = EFAULT;
4187c478bd9Sstevel@tonic-gate 			break;
4197c478bd9Sstevel@tonic-gate 		}
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		if (cmd == F_O_GETLK) {
4227c478bd9Sstevel@tonic-gate 			/*
4237c478bd9Sstevel@tonic-gate 			 * Return an SVR3 flock structure to the user.
4247c478bd9Sstevel@tonic-gate 			 */
4257c478bd9Sstevel@tonic-gate 			obf.l_type = (int16_t)bf.l_type;
4267c478bd9Sstevel@tonic-gate 			obf.l_whence = (int16_t)bf.l_whence;
4277c478bd9Sstevel@tonic-gate 			obf.l_start = (int32_t)bf.l_start;
4287c478bd9Sstevel@tonic-gate 			obf.l_len = (int32_t)bf.l_len;
4297c478bd9Sstevel@tonic-gate 			if (bf.l_sysid > SHRT_MAX || bf.l_pid > SHRT_MAX) {
4307c478bd9Sstevel@tonic-gate 				/*
4317c478bd9Sstevel@tonic-gate 				 * One or both values for the above fields
4327c478bd9Sstevel@tonic-gate 				 * is too large to store in an SVR3 flock
4337c478bd9Sstevel@tonic-gate 				 * structure.
4347c478bd9Sstevel@tonic-gate 				 */
4357c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
4367c478bd9Sstevel@tonic-gate 				break;
4377c478bd9Sstevel@tonic-gate 			}
4387c478bd9Sstevel@tonic-gate 			obf.l_sysid = (int16_t)bf.l_sysid;
4397c478bd9Sstevel@tonic-gate 			obf.l_pid = (int16_t)bf.l_pid;
4407c478bd9Sstevel@tonic-gate 			if (copyout(&obf, (void *)arg, sizeof (obf)))
4417c478bd9Sstevel@tonic-gate 				error = EFAULT;
442*7a5aac98SJerry Jelinek 		} else if (cmd == F_GETLK || cmd == F_OFD_GETLK) {
4437c478bd9Sstevel@tonic-gate 			/*
4447c478bd9Sstevel@tonic-gate 			 * Copy out SVR4 flock.
4457c478bd9Sstevel@tonic-gate 			 */
4467c478bd9Sstevel@tonic-gate 			int i;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 			if (bf.l_start > maxoffset || bf.l_len > maxoffset) {
4497c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
4507c478bd9Sstevel@tonic-gate 				break;
4517c478bd9Sstevel@tonic-gate 			}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 			if (datamodel == DATAMODEL_NATIVE) {
4547c478bd9Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
4557c478bd9Sstevel@tonic-gate 					sbf.l_pad[i] = 0;
4567c478bd9Sstevel@tonic-gate 				/*
4577c478bd9Sstevel@tonic-gate 				 * XXX	In an LP64 kernel with an LP64
4587c478bd9Sstevel@tonic-gate 				 *	application there's no need to do a
4597c478bd9Sstevel@tonic-gate 				 *	structure copy here as currently
4607c478bd9Sstevel@tonic-gate 				 *	struct flock == struct flock64.
4617c478bd9Sstevel@tonic-gate 				 *	We did it this way to avoid more
4627c478bd9Sstevel@tonic-gate 				 *	conditional compilation.
4637c478bd9Sstevel@tonic-gate 				 */
4647c478bd9Sstevel@tonic-gate 				sbf.l_type = bf.l_type;
4657c478bd9Sstevel@tonic-gate 				sbf.l_whence = bf.l_whence;
4667c478bd9Sstevel@tonic-gate 				sbf.l_start = (off_t)bf.l_start;
4677c478bd9Sstevel@tonic-gate 				sbf.l_len = (off_t)bf.l_len;
4687c478bd9Sstevel@tonic-gate 				sbf.l_sysid = bf.l_sysid;
4697c478bd9Sstevel@tonic-gate 				sbf.l_pid = bf.l_pid;
4707c478bd9Sstevel@tonic-gate 				if (copyout(&sbf, (void *)arg, sizeof (sbf)))
4717c478bd9Sstevel@tonic-gate 					error = EFAULT;
4727c478bd9Sstevel@tonic-gate 			}
4737c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
4747c478bd9Sstevel@tonic-gate 			else {
4757c478bd9Sstevel@tonic-gate 				struct flock32 sbf32;
4767c478bd9Sstevel@tonic-gate 				if (bf.l_start > MAXOFF32_T ||
4777c478bd9Sstevel@tonic-gate 				    bf.l_len > MAXOFF32_T) {
4787c478bd9Sstevel@tonic-gate 					error = EOVERFLOW;
4797c478bd9Sstevel@tonic-gate 					break;
4807c478bd9Sstevel@tonic-gate 				}
4817c478bd9Sstevel@tonic-gate 				for (i = 0; i < 4; i++)
4827c478bd9Sstevel@tonic-gate 					sbf32.l_pad[i] = 0;
4837c478bd9Sstevel@tonic-gate 				sbf32.l_type = (int16_t)bf.l_type;
4847c478bd9Sstevel@tonic-gate 				sbf32.l_whence = (int16_t)bf.l_whence;
4857c478bd9Sstevel@tonic-gate 				sbf32.l_start = (off32_t)bf.l_start;
4867c478bd9Sstevel@tonic-gate 				sbf32.l_len = (off32_t)bf.l_len;
4877c478bd9Sstevel@tonic-gate 				sbf32.l_sysid = (int32_t)bf.l_sysid;
4887c478bd9Sstevel@tonic-gate 				sbf32.l_pid = (pid32_t)bf.l_pid;
4897c478bd9Sstevel@tonic-gate 				if (copyout(&sbf32,
4907c478bd9Sstevel@tonic-gate 				    (void *)arg, sizeof (sbf32)))
4917c478bd9Sstevel@tonic-gate 					error = EFAULT;
4927c478bd9Sstevel@tonic-gate 			}
4937c478bd9Sstevel@tonic-gate #endif
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 		break;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	case F_CHKFL:
4987c478bd9Sstevel@tonic-gate 		/*
4997c478bd9Sstevel@tonic-gate 		 * This is for internal use only, to allow the vnode layer
5007c478bd9Sstevel@tonic-gate 		 * to validate a flags setting before applying it.  User
5017c478bd9Sstevel@tonic-gate 		 * programs can't issue it.
5027c478bd9Sstevel@tonic-gate 		 */
5037c478bd9Sstevel@tonic-gate 		error = EINVAL;
5047c478bd9Sstevel@tonic-gate 		break;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	case F_ALLOCSP:
5077c478bd9Sstevel@tonic-gate 	case F_FREESP:
508303bf60bSsdebnath 	case F_ALLOCSP64:
509303bf60bSsdebnath 	case F_FREESP64:
510019c3c43Sraf 		/*
511019c3c43Sraf 		 * Test for not-a-regular-file (and returning EINVAL)
512019c3c43Sraf 		 * before testing for open-for-writing (and returning EBADF).
513019c3c43Sraf 		 * This is relied upon by posix_fallocate() in libc.
514019c3c43Sraf 		 */
515019c3c43Sraf 		if (vp->v_type != VREG) {
516019c3c43Sraf 			error = EINVAL;
5177c478bd9Sstevel@tonic-gate 			break;
5187c478bd9Sstevel@tonic-gate 		}
519303bf60bSsdebnath 
520019c3c43Sraf 		if ((flag & FWRITE) == 0) {
521019c3c43Sraf 			error = EBADF;
5227c478bd9Sstevel@tonic-gate 			break;
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 
525303bf60bSsdebnath 		if (datamodel != DATAMODEL_ILP32 &&
526303bf60bSsdebnath 		    (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
527303bf60bSsdebnath 			error = EINVAL;
528303bf60bSsdebnath 			break;
529303bf60bSsdebnath 		}
530303bf60bSsdebnath 
5317c478bd9Sstevel@tonic-gate #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
532303bf60bSsdebnath 		if (datamodel == DATAMODEL_ILP32 &&
533303bf60bSsdebnath 		    (cmd == F_ALLOCSP || cmd == F_FREESP)) {
5347c478bd9Sstevel@tonic-gate 			struct flock32 sbf32;
5357c478bd9Sstevel@tonic-gate 			/*
5367c478bd9Sstevel@tonic-gate 			 * For compatibility we overlay an SVR3 flock on an SVR4
5377c478bd9Sstevel@tonic-gate 			 * flock.  This works because the input field offsets
5387c478bd9Sstevel@tonic-gate 			 * in "struct flock" were preserved.
5397c478bd9Sstevel@tonic-gate 			 */
5407c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
5417c478bd9Sstevel@tonic-gate 				error = EFAULT;
5427c478bd9Sstevel@tonic-gate 				break;
5437c478bd9Sstevel@tonic-gate 			} else {
5447c478bd9Sstevel@tonic-gate 				bf.l_type = sbf32.l_type;
5457c478bd9Sstevel@tonic-gate 				bf.l_whence = sbf32.l_whence;
5467c478bd9Sstevel@tonic-gate 				bf.l_start = (off64_t)sbf32.l_start;
5477c478bd9Sstevel@tonic-gate 				bf.l_len = (off64_t)sbf32.l_len;
5487c478bd9Sstevel@tonic-gate 				bf.l_sysid = sbf32.l_sysid;
5497c478bd9Sstevel@tonic-gate 				bf.l_pid = sbf32.l_pid;
5507c478bd9Sstevel@tonic-gate 			}
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate #endif /* _ILP32 || _SYSCALL32_IMPL */
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate #if defined(_LP64)
555303bf60bSsdebnath 		if (datamodel == DATAMODEL_LP64 &&
556303bf60bSsdebnath 		    (cmd == F_ALLOCSP || cmd == F_FREESP)) {
5577c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &bf, sizeof (bf))) {
5587c478bd9Sstevel@tonic-gate 				error = EFAULT;
5597c478bd9Sstevel@tonic-gate 				break;
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 		}
562303bf60bSsdebnath #endif /* defined(_LP64) */
5637c478bd9Sstevel@tonic-gate 
564303bf60bSsdebnath #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
565303bf60bSsdebnath 		if (datamodel == DATAMODEL_ILP32 &&
566303bf60bSsdebnath 		    (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
567303bf60bSsdebnath 			if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
568303bf60bSsdebnath 				error = EFAULT;
569303bf60bSsdebnath 				break;
570303bf60bSsdebnath 			} else {
571303bf60bSsdebnath 				/*
572303bf60bSsdebnath 				 * Note that the size of flock64 is different in
573303bf60bSsdebnath 				 * the ILP32 and LP64 models, due to the l_pad
574303bf60bSsdebnath 				 * field. We do not want to assume that the
575303bf60bSsdebnath 				 * flock64 structure is laid out the same in
576303bf60bSsdebnath 				 * ILP32 and LP64 environments, so we will
577303bf60bSsdebnath 				 * copy in the ILP32 version of flock64
578303bf60bSsdebnath 				 * explicitly and copy it to the native
579303bf60bSsdebnath 				 * flock64 structure.
580303bf60bSsdebnath 				 */
581303bf60bSsdebnath 				bf.l_type = (short)bf64_32.l_type;
582303bf60bSsdebnath 				bf.l_whence = (short)bf64_32.l_whence;
583303bf60bSsdebnath 				bf.l_start = bf64_32.l_start;
584303bf60bSsdebnath 				bf.l_len = bf64_32.l_len;
585303bf60bSsdebnath 				bf.l_sysid = (int)bf64_32.l_sysid;
586303bf60bSsdebnath 				bf.l_pid = (pid_t)bf64_32.l_pid;
587303bf60bSsdebnath 			}
588303bf60bSsdebnath 		}
589303bf60bSsdebnath #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
590303bf60bSsdebnath 
591303bf60bSsdebnath 		if (cmd == F_ALLOCSP || cmd == F_FREESP)
592303bf60bSsdebnath 			error = flock_check(vp, &bf, offset, maxoffset);
593303bf60bSsdebnath 		else if (cmd == F_ALLOCSP64 || cmd == F_FREESP64)
594303bf60bSsdebnath 			error = flock_check(vp, &bf, offset, MAXOFFSET_T);
595303bf60bSsdebnath 		if (error)
5967c478bd9Sstevel@tonic-gate 			break;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG && bf.l_len == 0 &&
5997c478bd9Sstevel@tonic-gate 		    bf.l_start > OFFSET_MAX(fp)) {
6007c478bd9Sstevel@tonic-gate 			error = EFBIG;
6017c478bd9Sstevel@tonic-gate 			break;
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		/*
6057c478bd9Sstevel@tonic-gate 		 * Make sure that there are no conflicting non-blocking
6067c478bd9Sstevel@tonic-gate 		 * mandatory locks in the region being manipulated. If
6077c478bd9Sstevel@tonic-gate 		 * there are such locks then return EACCES.
6087c478bd9Sstevel@tonic-gate 		 */
6097c478bd9Sstevel@tonic-gate 		if ((error = flock_get_start(vp, &bf, offset, &start)) != 0)
6107c478bd9Sstevel@tonic-gate 			break;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		if (nbl_need_check(vp)) {
6137c478bd9Sstevel@tonic-gate 			u_offset_t	begin;
6147c478bd9Sstevel@tonic-gate 			ssize_t		length;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 			nbl_start_crit(vp, RW_READER);
6177c478bd9Sstevel@tonic-gate 			in_crit = 1;
6187c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_SIZE;
619da6c28aaSamw 			if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
620da6c28aaSamw 			    != 0)
6217c478bd9Sstevel@tonic-gate 				break;
6227c478bd9Sstevel@tonic-gate 			begin = start > vattr.va_size ? vattr.va_size : start;
6237c478bd9Sstevel@tonic-gate 			length = vattr.va_size > start ? vattr.va_size - start :
6247c478bd9Sstevel@tonic-gate 			    start - vattr.va_size;
625da6c28aaSamw 			if (nbl_conflict(vp, NBL_WRITE, begin, length, 0,
626da6c28aaSamw 			    NULL)) {
6277c478bd9Sstevel@tonic-gate 				error = EACCES;
6287c478bd9Sstevel@tonic-gate 				break;
6297c478bd9Sstevel@tonic-gate 			}
6307c478bd9Sstevel@tonic-gate 		}
631303bf60bSsdebnath 
632303bf60bSsdebnath 		if (cmd == F_ALLOCSP64)
633303bf60bSsdebnath 			cmd = F_ALLOCSP;
634303bf60bSsdebnath 		else if (cmd == F_FREESP64)
635303bf60bSsdebnath 			cmd = F_FREESP;
636303bf60bSsdebnath 
6377c478bd9Sstevel@tonic-gate 		error = VOP_SPACE(vp, cmd, &bf, flag, offset, fp->f_cred, NULL);
638303bf60bSsdebnath 
6397c478bd9Sstevel@tonic-gate 		break;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
6427c478bd9Sstevel@tonic-gate 	case F_GETLK64:
6437c478bd9Sstevel@tonic-gate 	case F_SETLK64:
6447c478bd9Sstevel@tonic-gate 	case F_SETLKW64:
6457c478bd9Sstevel@tonic-gate 	case F_SETLK64_NBMAND:
646*7a5aac98SJerry Jelinek 	case F_OFD_GETLK64:
647*7a5aac98SJerry Jelinek 	case F_OFD_SETLK64:
648*7a5aac98SJerry Jelinek 	case F_OFD_SETLKW64:
649*7a5aac98SJerry Jelinek 	case F_FLOCK64:
650*7a5aac98SJerry Jelinek 	case F_FLOCKW64:
6517c478bd9Sstevel@tonic-gate 		/*
6527c478bd9Sstevel@tonic-gate 		 * Large Files: Here we set cmd as *LK and send it to
6537c478bd9Sstevel@tonic-gate 		 * lower layers. *LK64 is only for the user land.
6547c478bd9Sstevel@tonic-gate 		 * Most of the comments described above for F_SETLK
6557c478bd9Sstevel@tonic-gate 		 * applies here too.
6567c478bd9Sstevel@tonic-gate 		 * Large File support is only needed for ILP32 apps!
6577c478bd9Sstevel@tonic-gate 		 */
6587c478bd9Sstevel@tonic-gate 		if (datamodel != DATAMODEL_ILP32) {
6597c478bd9Sstevel@tonic-gate 			error = EINVAL;
6607c478bd9Sstevel@tonic-gate 			break;
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		if (cmd == F_GETLK64)
6647c478bd9Sstevel@tonic-gate 			cmd = F_GETLK;
6657c478bd9Sstevel@tonic-gate 		else if (cmd == F_SETLK64)
6667c478bd9Sstevel@tonic-gate 			cmd = F_SETLK;
6677c478bd9Sstevel@tonic-gate 		else if (cmd == F_SETLKW64)
6687c478bd9Sstevel@tonic-gate 			cmd = F_SETLKW;
6697c478bd9Sstevel@tonic-gate 		else if (cmd == F_SETLK64_NBMAND)
6707c478bd9Sstevel@tonic-gate 			cmd = F_SETLK_NBMAND;
671*7a5aac98SJerry Jelinek 		else if (cmd == F_OFD_GETLK64)
672*7a5aac98SJerry Jelinek 			cmd = F_OFD_GETLK;
673*7a5aac98SJerry Jelinek 		else if (cmd == F_OFD_SETLK64)
674*7a5aac98SJerry Jelinek 			cmd = F_OFD_SETLK;
675*7a5aac98SJerry Jelinek 		else if (cmd == F_OFD_SETLKW64)
676*7a5aac98SJerry Jelinek 			cmd = F_OFD_SETLKW;
677*7a5aac98SJerry Jelinek 		else if (cmd == F_FLOCK64)
678*7a5aac98SJerry Jelinek 			cmd = F_FLOCK;
679*7a5aac98SJerry Jelinek 		else if (cmd == F_FLOCKW64)
680*7a5aac98SJerry Jelinek 			cmd = F_FLOCKW;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		/*
6837c478bd9Sstevel@tonic-gate 		 * Note that the size of flock64 is different in the ILP32
6847c478bd9Sstevel@tonic-gate 		 * and LP64 models, due to the sucking l_pad field.
6857c478bd9Sstevel@tonic-gate 		 * We do not want to assume that the flock64 structure is
6867c478bd9Sstevel@tonic-gate 		 * laid out in the same in ILP32 and LP64 environments, so
6877c478bd9Sstevel@tonic-gate 		 * we will copy in the ILP32 version of flock64 explicitly
6887c478bd9Sstevel@tonic-gate 		 * and copy it to the native flock64 structure.
6897c478bd9Sstevel@tonic-gate 		 */
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
6927c478bd9Sstevel@tonic-gate 			error = EFAULT;
6937c478bd9Sstevel@tonic-gate 			break;
6947c478bd9Sstevel@tonic-gate 		}
695303bf60bSsdebnath 
6967c478bd9Sstevel@tonic-gate 		bf.l_type = (short)bf64_32.l_type;
6977c478bd9Sstevel@tonic-gate 		bf.l_whence = (short)bf64_32.l_whence;
6987c478bd9Sstevel@tonic-gate 		bf.l_start = bf64_32.l_start;
6997c478bd9Sstevel@tonic-gate 		bf.l_len = bf64_32.l_len;
7007c478bd9Sstevel@tonic-gate 		bf.l_sysid = (int)bf64_32.l_sysid;
7017c478bd9Sstevel@tonic-gate 		bf.l_pid = (pid_t)bf64_32.l_pid;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		if ((error = flock_check(vp, &bf, offset, MAXOFFSET_T)) != 0)
7047c478bd9Sstevel@tonic-gate 			break;
7057c478bd9Sstevel@tonic-gate 
706*7a5aac98SJerry Jelinek 		if (cmd == F_FLOCK || cmd == F_FLOCKW) {
707*7a5aac98SJerry Jelinek 			/* FLOCK* locking is always over the entire file. */
708*7a5aac98SJerry Jelinek 			if (bf.l_whence != 0 || bf.l_start != 0 ||
709*7a5aac98SJerry Jelinek 			    bf.l_len != 0) {
710*7a5aac98SJerry Jelinek 				error = EINVAL;
711*7a5aac98SJerry Jelinek 				break;
712*7a5aac98SJerry Jelinek 			}
713*7a5aac98SJerry Jelinek 			if (bf.l_type < F_RDLCK || bf.l_type > F_UNLCK) {
714*7a5aac98SJerry Jelinek 				error = EINVAL;
715*7a5aac98SJerry Jelinek 				break;
716*7a5aac98SJerry Jelinek 			}
717*7a5aac98SJerry Jelinek 		}
718*7a5aac98SJerry Jelinek 
719*7a5aac98SJerry Jelinek 		if (cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
720*7a5aac98SJerry Jelinek 			/*
721*7a5aac98SJerry Jelinek 			 * TBD OFD-style locking is currently limited to
722*7a5aac98SJerry Jelinek 			 * covering the entire file.
723*7a5aac98SJerry Jelinek 			 */
724*7a5aac98SJerry Jelinek 			if (bf.l_whence != 0 || bf.l_start != 0 ||
725*7a5aac98SJerry Jelinek 			    bf.l_len != 0) {
726*7a5aac98SJerry Jelinek 				error = EINVAL;
727*7a5aac98SJerry Jelinek 				break;
728*7a5aac98SJerry Jelinek 			}
729*7a5aac98SJerry Jelinek 		}
730*7a5aac98SJerry Jelinek 
731*7a5aac98SJerry Jelinek 		/*
732*7a5aac98SJerry Jelinek 		 * The *_frlock functions in the various file systems basically
733*7a5aac98SJerry Jelinek 		 * do some validation and then funnel everything through the
734*7a5aac98SJerry Jelinek 		 * fs_frlock function. For OFD-style locks fs_frlock will do
735*7a5aac98SJerry Jelinek 		 * nothing so that once control returns here we can call the
736*7a5aac98SJerry Jelinek 		 * ofdlock function with the correct fp. For OFD-style locks
737*7a5aac98SJerry Jelinek 		 * the unsupported remote file systems, such as NFS, detect and
738*7a5aac98SJerry Jelinek 		 * reject the OFD-style cmd argument.
739*7a5aac98SJerry Jelinek 		 */
7407c478bd9Sstevel@tonic-gate 		if ((error = VOP_FRLOCK(vp, cmd, &bf, flag, offset,
741da6c28aaSamw 		    NULL, fp->f_cred, NULL)) != 0)
7427c478bd9Sstevel@tonic-gate 			break;
7437c478bd9Sstevel@tonic-gate 
744*7a5aac98SJerry Jelinek 		if (cmd == F_FLOCK || cmd == F_FLOCKW || cmd == F_OFD_GETLK ||
745*7a5aac98SJerry Jelinek 		    cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
746*7a5aac98SJerry Jelinek 			/*
747*7a5aac98SJerry Jelinek 			 * This is an OFD-style lock so we need to handle it
748*7a5aac98SJerry Jelinek 			 * here. Because OFD-style locks are associated with
749*7a5aac98SJerry Jelinek 			 * the file_t we didn't have enough info down the
750*7a5aac98SJerry Jelinek 			 * VOP_FRLOCK path immediately above.
751*7a5aac98SJerry Jelinek 			 */
752*7a5aac98SJerry Jelinek 			if ((error = ofdlock(fp, cmd, &bf, flag, offset)) != 0)
753*7a5aac98SJerry Jelinek 				break;
754*7a5aac98SJerry Jelinek 		}
755*7a5aac98SJerry Jelinek 
756*7a5aac98SJerry Jelinek 		if ((cmd == F_GETLK || cmd == F_OFD_GETLK) &&
757*7a5aac98SJerry Jelinek 		    bf.l_type == F_UNLCK) {
7587c478bd9Sstevel@tonic-gate 			if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
7597c478bd9Sstevel@tonic-gate 			    sizeof (bf.l_type)))
7607c478bd9Sstevel@tonic-gate 				error = EFAULT;
7617c478bd9Sstevel@tonic-gate 			break;
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 
764*7a5aac98SJerry Jelinek 		if (cmd == F_GETLK || cmd == F_OFD_GETLK) {
7657c478bd9Sstevel@tonic-gate 			int i;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 			/*
7687c478bd9Sstevel@tonic-gate 			 * We do not want to assume that the flock64 structure
7697c478bd9Sstevel@tonic-gate 			 * is laid out in the same in ILP32 and LP64
7707c478bd9Sstevel@tonic-gate 			 * environments, so we will copy out the ILP32 version
7717c478bd9Sstevel@tonic-gate 			 * of flock64 explicitly after copying the native
7727c478bd9Sstevel@tonic-gate 			 * flock64 structure to it.
7737c478bd9Sstevel@tonic-gate 			 */
7747c478bd9Sstevel@tonic-gate 			for (i = 0; i < 4; i++)
7757c478bd9Sstevel@tonic-gate 				bf64_32.l_pad[i] = 0;
7767c478bd9Sstevel@tonic-gate 			bf64_32.l_type = (int16_t)bf.l_type;
7777c478bd9Sstevel@tonic-gate 			bf64_32.l_whence = (int16_t)bf.l_whence;
7787c478bd9Sstevel@tonic-gate 			bf64_32.l_start = bf.l_start;
7797c478bd9Sstevel@tonic-gate 			bf64_32.l_len = bf.l_len;
7807c478bd9Sstevel@tonic-gate 			bf64_32.l_sysid = (int32_t)bf.l_sysid;
7817c478bd9Sstevel@tonic-gate 			bf64_32.l_pid = (pid32_t)bf.l_pid;
7827c478bd9Sstevel@tonic-gate 			if (copyout(&bf64_32, (void *)arg, sizeof (bf64_32)))
7837c478bd9Sstevel@tonic-gate 				error = EFAULT;
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 		break;
786303bf60bSsdebnath #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	case F_SHARE:
7897c478bd9Sstevel@tonic-gate 	case F_SHARE_NBMAND:
7907c478bd9Sstevel@tonic-gate 	case F_UNSHARE:
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 		/*
7937c478bd9Sstevel@tonic-gate 		 * Copy in input fields only.
7947c478bd9Sstevel@tonic-gate 		 */
7957c478bd9Sstevel@tonic-gate 		if (copyin((void *)arg, &fsh, sizeof (fsh))) {
7967c478bd9Sstevel@tonic-gate 			error = EFAULT;
7977c478bd9Sstevel@tonic-gate 			break;
7987c478bd9Sstevel@tonic-gate 		}
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 		/*
8017c478bd9Sstevel@tonic-gate 		 * Local share reservations always have this simple form
8027c478bd9Sstevel@tonic-gate 		 */
8037c478bd9Sstevel@tonic-gate 		shr.s_access = fsh.f_access;
8047c478bd9Sstevel@tonic-gate 		shr.s_deny = fsh.f_deny;
8057c478bd9Sstevel@tonic-gate 		shr.s_sysid = 0;
8067c478bd9Sstevel@tonic-gate 		shr.s_pid = ttoproc(curthread)->p_pid;
8077c478bd9Sstevel@tonic-gate 		shr_own.sl_pid = shr.s_pid;
8087c478bd9Sstevel@tonic-gate 		shr_own.sl_id = fsh.f_id;
8097c478bd9Sstevel@tonic-gate 		shr.s_own_len = sizeof (shr_own);
8107c478bd9Sstevel@tonic-gate 		shr.s_owner = (caddr_t)&shr_own;
811da6c28aaSamw 		error = VOP_SHRLOCK(vp, cmd, &shr, flag, fp->f_cred, NULL);
8127c478bd9Sstevel@tonic-gate 		break;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	default:
8157c478bd9Sstevel@tonic-gate 		error = EINVAL;
8167c478bd9Sstevel@tonic-gate 		break;
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	if (in_crit)
8207c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate done:
8237c478bd9Sstevel@tonic-gate 	releasef(fdes);
8247c478bd9Sstevel@tonic-gate out:
8257c478bd9Sstevel@tonic-gate 	if (error)
8267c478bd9Sstevel@tonic-gate 		return (set_errno(error));
8277c478bd9Sstevel@tonic-gate 	return (retval);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate int
8317c478bd9Sstevel@tonic-gate flock_check(vnode_t *vp, flock64_t *flp, offset_t offset, offset_t max)
8327c478bd9Sstevel@tonic-gate {
8337c478bd9Sstevel@tonic-gate 	struct vattr	vattr;
8347c478bd9Sstevel@tonic-gate 	int	error;
8357c478bd9Sstevel@tonic-gate 	u_offset_t start, end;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	/*
8387c478bd9Sstevel@tonic-gate 	 * Determine the starting point of the request
8397c478bd9Sstevel@tonic-gate 	 */
8407c478bd9Sstevel@tonic-gate 	switch (flp->l_whence) {
8417c478bd9Sstevel@tonic-gate 	case 0:		/* SEEK_SET */
8427c478bd9Sstevel@tonic-gate 		start = (u_offset_t)flp->l_start;
8437c478bd9Sstevel@tonic-gate 		if (start > max)
8447c478bd9Sstevel@tonic-gate 			return (EINVAL);
8457c478bd9Sstevel@tonic-gate 		break;
8467c478bd9Sstevel@tonic-gate 	case 1:		/* SEEK_CUR */
8477c478bd9Sstevel@tonic-gate 		if (flp->l_start > (max - offset))
8487c478bd9Sstevel@tonic-gate 			return (EOVERFLOW);
8497c478bd9Sstevel@tonic-gate 		start = (u_offset_t)(flp->l_start + offset);
8507c478bd9Sstevel@tonic-gate 		if (start > max)
8517c478bd9Sstevel@tonic-gate 			return (EINVAL);
8527c478bd9Sstevel@tonic-gate 		break;
8537c478bd9Sstevel@tonic-gate 	case 2:		/* SEEK_END */
8547c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_SIZE;
855da6c28aaSamw 		if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
8567c478bd9Sstevel@tonic-gate 			return (error);
8577c478bd9Sstevel@tonic-gate 		if (flp->l_start > (max - (offset_t)vattr.va_size))
8587c478bd9Sstevel@tonic-gate 			return (EOVERFLOW);
8597c478bd9Sstevel@tonic-gate 		start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
8607c478bd9Sstevel@tonic-gate 		if (start > max)
8617c478bd9Sstevel@tonic-gate 			return (EINVAL);
8627c478bd9Sstevel@tonic-gate 		break;
8637c478bd9Sstevel@tonic-gate 	default:
8647c478bd9Sstevel@tonic-gate 		return (EINVAL);
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	/*
8687c478bd9Sstevel@tonic-gate 	 * Determine the range covered by the request.
8697c478bd9Sstevel@tonic-gate 	 */
8707c478bd9Sstevel@tonic-gate 	if (flp->l_len == 0)
8717c478bd9Sstevel@tonic-gate 		end = MAXEND;
8727c478bd9Sstevel@tonic-gate 	else if ((offset_t)flp->l_len > 0) {
8737c478bd9Sstevel@tonic-gate 		if (flp->l_len > (max - start + 1))
8747c478bd9Sstevel@tonic-gate 			return (EOVERFLOW);
8757c478bd9Sstevel@tonic-gate 		end = (u_offset_t)(start + (flp->l_len - 1));
8767c478bd9Sstevel@tonic-gate 		ASSERT(end <= max);
8777c478bd9Sstevel@tonic-gate 	} else {
8787c478bd9Sstevel@tonic-gate 		/*
8797c478bd9Sstevel@tonic-gate 		 * Negative length; why do we even allow this ?
8807c478bd9Sstevel@tonic-gate 		 * Because this allows easy specification of
8817c478bd9Sstevel@tonic-gate 		 * the last n bytes of the file.
8827c478bd9Sstevel@tonic-gate 		 */
8837c478bd9Sstevel@tonic-gate 		end = start;
8847c478bd9Sstevel@tonic-gate 		start += (u_offset_t)flp->l_len;
8857c478bd9Sstevel@tonic-gate 		(start)++;
8867c478bd9Sstevel@tonic-gate 		if (start > max)
8877c478bd9Sstevel@tonic-gate 			return (EINVAL);
8887c478bd9Sstevel@tonic-gate 		ASSERT(end <= max);
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 	ASSERT(start <= max);
8917c478bd9Sstevel@tonic-gate 	if (flp->l_type == F_UNLCK && flp->l_len > 0 &&
8927c478bd9Sstevel@tonic-gate 	    end == (offset_t)max) {
8937c478bd9Sstevel@tonic-gate 		flp->l_len = 0;
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 	if (start  > end)
8967c478bd9Sstevel@tonic-gate 		return (EINVAL);
8977c478bd9Sstevel@tonic-gate 	return (0);
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate static int
9017c478bd9Sstevel@tonic-gate flock_get_start(vnode_t *vp, flock64_t *flp, offset_t offset, u_offset_t *start)
9027c478bd9Sstevel@tonic-gate {
9037c478bd9Sstevel@tonic-gate 	struct vattr	vattr;
9047c478bd9Sstevel@tonic-gate 	int	error;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	/*
9077c478bd9Sstevel@tonic-gate 	 * Determine the starting point of the request. Assume that it is
9087c478bd9Sstevel@tonic-gate 	 * a valid starting point.
9097c478bd9Sstevel@tonic-gate 	 */
9107c478bd9Sstevel@tonic-gate 	switch (flp->l_whence) {
9117c478bd9Sstevel@tonic-gate 	case 0:		/* SEEK_SET */
9127c478bd9Sstevel@tonic-gate 		*start = (u_offset_t)flp->l_start;
9137c478bd9Sstevel@tonic-gate 		break;
9147c478bd9Sstevel@tonic-gate 	case 1:		/* SEEK_CUR */
9157c478bd9Sstevel@tonic-gate 		*start = (u_offset_t)(flp->l_start + offset);
9167c478bd9Sstevel@tonic-gate 		break;
9177c478bd9Sstevel@tonic-gate 	case 2:		/* SEEK_END */
9187c478bd9Sstevel@tonic-gate 		vattr.va_mask = AT_SIZE;
919da6c28aaSamw 		if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
9207c478bd9Sstevel@tonic-gate 			return (error);
9217c478bd9Sstevel@tonic-gate 		*start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
9227c478bd9Sstevel@tonic-gate 		break;
9237c478bd9Sstevel@tonic-gate 	default:
9247c478bd9Sstevel@tonic-gate 		return (EINVAL);
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	return (0);
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate /*
9317c478bd9Sstevel@tonic-gate  * Take rctl action when the requested file descriptor is too big.
9327c478bd9Sstevel@tonic-gate  */
9337c478bd9Sstevel@tonic-gate static void
9347c478bd9Sstevel@tonic-gate fd_too_big(proc_t *p)
9357c478bd9Sstevel@tonic-gate {
9367c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
9377c478bd9Sstevel@tonic-gate 	(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
9387c478bd9Sstevel@tonic-gate 	    p->p_rctls, p, RCA_SAFE);
9397c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
9407c478bd9Sstevel@tonic-gate }
941