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.
259c7f1ae0SJerry Jelinek * Copyright 2018, Joyent, Inc.
26*0250c53aSRobert Mustacchi * Copyright 2024 Oxide Computer Company
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
307c478bd9Sstevel@tonic-gate /* All Rights Reserved */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
347c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California.
357c478bd9Sstevel@tonic-gate */
367c478bd9Sstevel@tonic-gate
375dbfd19aSTheo Schlossnagle
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 #include <sys/flock.h>
467c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
477c478bd9Sstevel@tonic-gate #include <sys/file.h>
487c478bd9Sstevel@tonic-gate #include <sys/mode.h>
497c478bd9Sstevel@tonic-gate #include <sys/proc.h>
507c478bd9Sstevel@tonic-gate #include <sys/filio.h>
517c478bd9Sstevel@tonic-gate #include <sys/share.h>
527c478bd9Sstevel@tonic-gate #include <sys/debug.h>
537c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
547c478bd9Sstevel@tonic-gate #include <sys/nbmlock.h>
557c478bd9Sstevel@tonic-gate
56303bf60bSsdebnath #include <sys/cmn_err.h>
57303bf60bSsdebnath
587c478bd9Sstevel@tonic-gate static int flock_check(vnode_t *, flock64_t *, offset_t, offset_t);
597c478bd9Sstevel@tonic-gate static int flock_get_start(vnode_t *, flock64_t *, offset_t, u_offset_t *);
607c478bd9Sstevel@tonic-gate static void fd_too_big(proc_t *);
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate * File control.
647c478bd9Sstevel@tonic-gate */
657c478bd9Sstevel@tonic-gate int
fcntl(int fdes,int cmd,intptr_t arg,intptr_t arg1)66*0250c53aSRobert Mustacchi fcntl(int fdes, int cmd, intptr_t arg, intptr_t arg1)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate int iarg;
697c478bd9Sstevel@tonic-gate int error = 0;
707c478bd9Sstevel@tonic-gate int retval;
717c478bd9Sstevel@tonic-gate proc_t *p;
727c478bd9Sstevel@tonic-gate file_t *fp;
737c478bd9Sstevel@tonic-gate vnode_t *vp;
747c478bd9Sstevel@tonic-gate u_offset_t offset;
757c478bd9Sstevel@tonic-gate u_offset_t start;
767c478bd9Sstevel@tonic-gate struct vattr vattr;
777c478bd9Sstevel@tonic-gate int in_crit;
787c478bd9Sstevel@tonic-gate int flag;
797c478bd9Sstevel@tonic-gate struct flock sbf;
807c478bd9Sstevel@tonic-gate struct flock64 bf;
817c478bd9Sstevel@tonic-gate struct o_flock obf;
827c478bd9Sstevel@tonic-gate struct flock64_32 bf64_32;
837c478bd9Sstevel@tonic-gate struct fshare fsh;
847c478bd9Sstevel@tonic-gate struct shrlock shr;
857c478bd9Sstevel@tonic-gate struct shr_locowner shr_own;
867c478bd9Sstevel@tonic-gate offset_t maxoffset;
877c478bd9Sstevel@tonic-gate model_t datamodel;
88a5f69788Scraigm int fdres;
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate #if defined(_ILP32) && !defined(lint) && defined(_SYSCALL32)
917c478bd9Sstevel@tonic-gate ASSERT(sizeof (struct flock) == sizeof (struct flock32));
927c478bd9Sstevel@tonic-gate ASSERT(sizeof (struct flock64) == sizeof (struct flock64_32));
937c478bd9Sstevel@tonic-gate #endif
947c478bd9Sstevel@tonic-gate #if defined(_LP64) && !defined(lint) && defined(_SYSCALL32)
957c478bd9Sstevel@tonic-gate ASSERT(sizeof (struct flock) == sizeof (struct flock64_64));
967c478bd9Sstevel@tonic-gate ASSERT(sizeof (struct flock64) == sizeof (struct flock64_64));
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate /*
100*0250c53aSRobert Mustacchi * Most fcntl() calls take either 2 or 3 arguments. The introduction of
101*0250c53aSRobert Mustacchi * F_DUP3FD added a version that takes a 4th argument (referred to as
102*0250c53aSRobert Mustacchi * arg1). While fcntl() traditionally has had loose validation, we
103*0250c53aSRobert Mustacchi * strictly validate this new arg.
104*0250c53aSRobert Mustacchi */
105*0250c53aSRobert Mustacchi switch (cmd) {
106*0250c53aSRobert Mustacchi case F_DUP3FD:
107*0250c53aSRobert Mustacchi if ((arg1 & ~(FD_CLOEXEC | FD_CLOFORK)) != 0) {
108*0250c53aSRobert Mustacchi error = EINVAL;
109*0250c53aSRobert Mustacchi goto out;
110*0250c53aSRobert Mustacchi }
111*0250c53aSRobert Mustacchi break;
112*0250c53aSRobert Mustacchi default:
113*0250c53aSRobert Mustacchi if (arg1 != 0) {
114*0250c53aSRobert Mustacchi error = EINVAL;
115*0250c53aSRobert Mustacchi goto out;
116*0250c53aSRobert Mustacchi }
117*0250c53aSRobert Mustacchi break;
118*0250c53aSRobert Mustacchi }
119*0250c53aSRobert Mustacchi
120*0250c53aSRobert Mustacchi /*
1217c478bd9Sstevel@tonic-gate * First, for speed, deal with the subset of cases
1227c478bd9Sstevel@tonic-gate * that do not require getf() / releasef().
1237c478bd9Sstevel@tonic-gate */
1247c478bd9Sstevel@tonic-gate switch (cmd) {
1257c478bd9Sstevel@tonic-gate case F_GETFD:
1267c478bd9Sstevel@tonic-gate if ((error = f_getfd_error(fdes, &flag)) == 0)
1277c478bd9Sstevel@tonic-gate retval = flag;
1287c478bd9Sstevel@tonic-gate goto out;
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate case F_SETFD:
1317c478bd9Sstevel@tonic-gate error = f_setfd_error(fdes, (int)arg);
1327c478bd9Sstevel@tonic-gate retval = 0;
1337c478bd9Sstevel@tonic-gate goto out;
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate case F_GETFL:
136794f0adbSRoger A. Faulkner if ((error = f_getfl(fdes, &flag)) == 0) {
137794f0adbSRoger A. Faulkner retval = (flag & (FMASK | FASYNC));
138794f0adbSRoger A. Faulkner if ((flag & (FSEARCH | FEXEC)) == 0)
139794f0adbSRoger A. Faulkner retval += FOPEN;
140794f0adbSRoger A. Faulkner else
141794f0adbSRoger A. Faulkner retval |= (flag & (FSEARCH | FEXEC));
142794f0adbSRoger A. Faulkner }
1437c478bd9Sstevel@tonic-gate goto out;
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate case F_GETXFL:
146794f0adbSRoger A. Faulkner if ((error = f_getfl(fdes, &flag)) == 0) {
147794f0adbSRoger A. Faulkner retval = flag;
148794f0adbSRoger A. Faulkner if ((flag & (FSEARCH | FEXEC)) == 0)
149794f0adbSRoger A. Faulkner retval += FOPEN;
150794f0adbSRoger A. Faulkner }
1517c478bd9Sstevel@tonic-gate goto out;
152a5f69788Scraigm
153a5f69788Scraigm case F_BADFD:
154a5f69788Scraigm if ((error = f_badfd(fdes, &fdres, (int)arg)) == 0)
155a5f69788Scraigm retval = fdres;
156a5f69788Scraigm goto out;
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate * Second, for speed, deal with the subset of cases that
1617c478bd9Sstevel@tonic-gate * require getf() / releasef() but do not require copyin.
1627c478bd9Sstevel@tonic-gate */
1637c478bd9Sstevel@tonic-gate if ((fp = getf(fdes)) == NULL) {
1647c478bd9Sstevel@tonic-gate error = EBADF;
1657c478bd9Sstevel@tonic-gate goto out;
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate iarg = (int)arg;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate switch (cmd) {
1707c478bd9Sstevel@tonic-gate case F_DUPFD:
1715dbfd19aSTheo Schlossnagle case F_DUPFD_CLOEXEC:
172*0250c53aSRobert Mustacchi case F_DUPFD_CLOFORK:
1737c478bd9Sstevel@tonic-gate p = curproc;
1747c478bd9Sstevel@tonic-gate if ((uint_t)iarg >= p->p_fno_ctl) {
1757c478bd9Sstevel@tonic-gate if (iarg >= 0)
1767c478bd9Sstevel@tonic-gate fd_too_big(p);
1777c478bd9Sstevel@tonic-gate error = EINVAL;
178c0e499e0SMilan Cermak goto done;
179c0e499e0SMilan Cermak }
180c0e499e0SMilan Cermak /*
181c0e499e0SMilan Cermak * We need to increment the f_count reference counter
182c0e499e0SMilan Cermak * before allocating a new file descriptor.
183c0e499e0SMilan Cermak * Doing it other way round opens a window for race condition
184c0e499e0SMilan Cermak * with closeandsetf() on the target file descriptor which can
185c0e499e0SMilan Cermak * close the file still referenced by the original
186c0e499e0SMilan Cermak * file descriptor.
187c0e499e0SMilan Cermak */
1887c478bd9Sstevel@tonic-gate mutex_enter(&fp->f_tlock);
1897c478bd9Sstevel@tonic-gate fp->f_count++;
1907c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock);
191c0e499e0SMilan Cermak if ((retval = ufalloc_file(iarg, fp)) == -1) {
192c0e499e0SMilan Cermak /*
193c0e499e0SMilan Cermak * New file descriptor can't be allocated.
194c0e499e0SMilan Cermak * Revert the reference count.
195c0e499e0SMilan Cermak */
196c0e499e0SMilan Cermak mutex_enter(&fp->f_tlock);
197c0e499e0SMilan Cermak fp->f_count--;
198c0e499e0SMilan Cermak mutex_exit(&fp->f_tlock);
199c0e499e0SMilan Cermak error = EMFILE;
2005dbfd19aSTheo Schlossnagle } else {
2015dbfd19aSTheo Schlossnagle if (cmd == F_DUPFD_CLOEXEC) {
202*0250c53aSRobert Mustacchi f_setfd_or(retval, FD_CLOEXEC);
203*0250c53aSRobert Mustacchi }
204*0250c53aSRobert Mustacchi
205*0250c53aSRobert Mustacchi if (cmd == F_DUPFD_CLOFORK) {
206*0250c53aSRobert Mustacchi f_setfd_or(retval, FD_CLOFORK);
2075dbfd19aSTheo Schlossnagle }
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate goto done;
2107c478bd9Sstevel@tonic-gate
2115dbfd19aSTheo Schlossnagle case F_DUP2FD_CLOEXEC:
212*0250c53aSRobert Mustacchi case F_DUP2FD_CLOFORK:
2135dbfd19aSTheo Schlossnagle if (fdes == iarg) {
2145dbfd19aSTheo Schlossnagle error = EINVAL;
2155dbfd19aSTheo Schlossnagle goto done;
2165dbfd19aSTheo Schlossnagle }
2175dbfd19aSTheo Schlossnagle
2185eaceb49STheo Schlossnagle /*FALLTHROUGH*/
2195dbfd19aSTheo Schlossnagle
2207c478bd9Sstevel@tonic-gate case F_DUP2FD:
221*0250c53aSRobert Mustacchi case F_DUP3FD:
2227c478bd9Sstevel@tonic-gate p = curproc;
2237c478bd9Sstevel@tonic-gate if (fdes == iarg) {
2247c478bd9Sstevel@tonic-gate retval = iarg;
2257c478bd9Sstevel@tonic-gate } else if ((uint_t)iarg >= p->p_fno_ctl) {
2267c478bd9Sstevel@tonic-gate if (iarg >= 0)
2277c478bd9Sstevel@tonic-gate fd_too_big(p);
2287c478bd9Sstevel@tonic-gate error = EBADF;
2297c478bd9Sstevel@tonic-gate } else {
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate * We can't hold our getf(fdes) across the call to
2327c478bd9Sstevel@tonic-gate * closeandsetf() because it creates a window for
2337c478bd9Sstevel@tonic-gate * deadlock: if one thread is doing dup2(a, b) while
2347c478bd9Sstevel@tonic-gate * another is doing dup2(b, a), each one will block
2357c478bd9Sstevel@tonic-gate * waiting for the other to call releasef(). The
2367c478bd9Sstevel@tonic-gate * solution is to increment the file reference count
2377c478bd9Sstevel@tonic-gate * (which we have to do anyway), then releasef(fdes),
2387c478bd9Sstevel@tonic-gate * then closeandsetf(). Incrementing f_count ensures
2397c478bd9Sstevel@tonic-gate * that fp won't disappear after we call releasef().
240a5f69788Scraigm * When closeandsetf() fails, we try avoid calling
241a5f69788Scraigm * closef() because of all the side effects.
2427c478bd9Sstevel@tonic-gate */
2437c478bd9Sstevel@tonic-gate mutex_enter(&fp->f_tlock);
2447c478bd9Sstevel@tonic-gate fp->f_count++;
2457c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock);
2467c478bd9Sstevel@tonic-gate releasef(fdes);
247a5f69788Scraigm if ((error = closeandsetf(iarg, fp)) == 0) {
2485dbfd19aSTheo Schlossnagle if (cmd == F_DUP2FD_CLOEXEC) {
249*0250c53aSRobert Mustacchi f_setfd_or(iarg, FD_CLOEXEC);
250*0250c53aSRobert Mustacchi } else if (cmd == F_DUP2FD_CLOFORK) {
251*0250c53aSRobert Mustacchi f_setfd_or(iarg, FD_CLOFORK);
252*0250c53aSRobert Mustacchi } else if (cmd == F_DUP3FD) {
253*0250c53aSRobert Mustacchi f_setfd_or(iarg, (int)arg1);
2545dbfd19aSTheo Schlossnagle }
2557c478bd9Sstevel@tonic-gate retval = iarg;
256a5f69788Scraigm } else {
257a5f69788Scraigm mutex_enter(&fp->f_tlock);
258a5f69788Scraigm if (fp->f_count > 1) {
259a5f69788Scraigm fp->f_count--;
260a5f69788Scraigm mutex_exit(&fp->f_tlock);
261a5f69788Scraigm } else {
262a5f69788Scraigm mutex_exit(&fp->f_tlock);
263a5f69788Scraigm (void) closef(fp);
264a5f69788Scraigm }
265a5f69788Scraigm }
2667c478bd9Sstevel@tonic-gate goto out;
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate goto done;
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate case F_SETFL:
2717c478bd9Sstevel@tonic-gate vp = fp->f_vnode;
2727c478bd9Sstevel@tonic-gate flag = fp->f_flag;
2737c478bd9Sstevel@tonic-gate if ((iarg & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY))
2747c478bd9Sstevel@tonic-gate iarg &= ~FNDELAY;
275da6c28aaSamw if ((error = VOP_SETFL(vp, flag, iarg, fp->f_cred, NULL)) ==
276da6c28aaSamw 0) {
2777c478bd9Sstevel@tonic-gate iarg &= FMASK;
2787c478bd9Sstevel@tonic-gate mutex_enter(&fp->f_tlock);
2797c478bd9Sstevel@tonic-gate fp->f_flag &= ~FMASK | (FREAD|FWRITE);
2807c478bd9Sstevel@tonic-gate fp->f_flag |= (iarg - FOPEN) & ~(FREAD|FWRITE);
2817c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate retval = 0;
2847c478bd9Sstevel@tonic-gate goto done;
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate /*
2887c478bd9Sstevel@tonic-gate * Finally, deal with the expensive cases.
2897c478bd9Sstevel@tonic-gate */
2907c478bd9Sstevel@tonic-gate retval = 0;
2917c478bd9Sstevel@tonic-gate in_crit = 0;
2927c478bd9Sstevel@tonic-gate maxoffset = MAXOFF_T;
2937c478bd9Sstevel@tonic-gate datamodel = DATAMODEL_NATIVE;
2947c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
2957c478bd9Sstevel@tonic-gate if ((datamodel = get_udatamodel()) == DATAMODEL_ILP32)
2967c478bd9Sstevel@tonic-gate maxoffset = MAXOFF32_T;
2977c478bd9Sstevel@tonic-gate #endif
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate vp = fp->f_vnode;
3007c478bd9Sstevel@tonic-gate flag = fp->f_flag;
3017c478bd9Sstevel@tonic-gate offset = fp->f_offset;
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate switch (cmd) {
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate * The file system and vnode layers understand and implement
3067c478bd9Sstevel@tonic-gate * locking with flock64 structures. So here once we pass through
3077c478bd9Sstevel@tonic-gate * the test for compatibility as defined by LFS API, (for F_SETLK,
3087a5aac98SJerry Jelinek * F_SETLKW, F_GETLK, F_GETLKW, F_OFD_GETLK, F_OFD_SETLK, F_OFD_SETLKW,
3097a5aac98SJerry Jelinek * F_FREESP) we transform the flock structure to a flock64 structure
3107a5aac98SJerry Jelinek * and send it to the lower layers. Similarly in case of GETLK and
3117a5aac98SJerry Jelinek * OFD_GETLK the returned flock64 structure is transformed to a flock
3127a5aac98SJerry Jelinek * structure if everything fits in nicely, otherwise we return
3137a5aac98SJerry Jelinek * EOVERFLOW.
3147c478bd9Sstevel@tonic-gate */
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate case F_GETLK:
3177c478bd9Sstevel@tonic-gate case F_O_GETLK:
3187c478bd9Sstevel@tonic-gate case F_SETLK:
3197c478bd9Sstevel@tonic-gate case F_SETLKW:
3207c478bd9Sstevel@tonic-gate case F_SETLK_NBMAND:
3217a5aac98SJerry Jelinek case F_OFD_GETLK:
3227a5aac98SJerry Jelinek case F_OFD_SETLK:
3237a5aac98SJerry Jelinek case F_OFD_SETLKW:
3247a5aac98SJerry Jelinek case F_FLOCK:
3257a5aac98SJerry Jelinek case F_FLOCKW:
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate * Copy in input fields only.
3297c478bd9Sstevel@tonic-gate */
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate if (cmd == F_O_GETLK) {
3327c478bd9Sstevel@tonic-gate if (datamodel != DATAMODEL_ILP32) {
3337c478bd9Sstevel@tonic-gate error = EINVAL;
3347c478bd9Sstevel@tonic-gate break;
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate if (copyin((void *)arg, &obf, sizeof (obf))) {
3387c478bd9Sstevel@tonic-gate error = EFAULT;
3397c478bd9Sstevel@tonic-gate break;
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate bf.l_type = obf.l_type;
3427c478bd9Sstevel@tonic-gate bf.l_whence = obf.l_whence;
3437c478bd9Sstevel@tonic-gate bf.l_start = (off64_t)obf.l_start;
3447c478bd9Sstevel@tonic-gate bf.l_len = (off64_t)obf.l_len;
3457c478bd9Sstevel@tonic-gate bf.l_sysid = (int)obf.l_sysid;
3467c478bd9Sstevel@tonic-gate bf.l_pid = obf.l_pid;
3477c478bd9Sstevel@tonic-gate } else if (datamodel == DATAMODEL_NATIVE) {
3487c478bd9Sstevel@tonic-gate if (copyin((void *)arg, &sbf, sizeof (sbf))) {
3497c478bd9Sstevel@tonic-gate error = EFAULT;
3507c478bd9Sstevel@tonic-gate break;
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate * XXX In an LP64 kernel with an LP64 application
3547c478bd9Sstevel@tonic-gate * there's no need to do a structure copy here
3557c478bd9Sstevel@tonic-gate * struct flock == struct flock64. However,
3567c478bd9Sstevel@tonic-gate * we did it this way to avoid more conditional
3577c478bd9Sstevel@tonic-gate * compilation.
3587c478bd9Sstevel@tonic-gate */
3597c478bd9Sstevel@tonic-gate bf.l_type = sbf.l_type;
3607c478bd9Sstevel@tonic-gate bf.l_whence = sbf.l_whence;
3617c478bd9Sstevel@tonic-gate bf.l_start = (off64_t)sbf.l_start;
3627c478bd9Sstevel@tonic-gate bf.l_len = (off64_t)sbf.l_len;
3637c478bd9Sstevel@tonic-gate bf.l_sysid = sbf.l_sysid;
3647c478bd9Sstevel@tonic-gate bf.l_pid = sbf.l_pid;
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
3677c478bd9Sstevel@tonic-gate else {
3687c478bd9Sstevel@tonic-gate struct flock32 sbf32;
3697c478bd9Sstevel@tonic-gate if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
3707c478bd9Sstevel@tonic-gate error = EFAULT;
3717c478bd9Sstevel@tonic-gate break;
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate bf.l_type = sbf32.l_type;
3747c478bd9Sstevel@tonic-gate bf.l_whence = sbf32.l_whence;
3757c478bd9Sstevel@tonic-gate bf.l_start = (off64_t)sbf32.l_start;
3767c478bd9Sstevel@tonic-gate bf.l_len = (off64_t)sbf32.l_len;
3777c478bd9Sstevel@tonic-gate bf.l_sysid = sbf32.l_sysid;
3787c478bd9Sstevel@tonic-gate bf.l_pid = sbf32.l_pid;
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate * 64-bit support: check for overflow for 32-bit lock ops
3847c478bd9Sstevel@tonic-gate */
3857c478bd9Sstevel@tonic-gate if ((error = flock_check(vp, &bf, offset, maxoffset)) != 0)
3867c478bd9Sstevel@tonic-gate break;
3877c478bd9Sstevel@tonic-gate
3887a5aac98SJerry Jelinek if (cmd == F_FLOCK || cmd == F_FLOCKW) {
3897a5aac98SJerry Jelinek /* FLOCK* locking is always over the entire file. */
3907a5aac98SJerry Jelinek if (bf.l_whence != 0 || bf.l_start != 0 ||
3917a5aac98SJerry Jelinek bf.l_len != 0) {
3927a5aac98SJerry Jelinek error = EINVAL;
3937a5aac98SJerry Jelinek break;
3947a5aac98SJerry Jelinek }
3957a5aac98SJerry Jelinek if (bf.l_type < F_RDLCK || bf.l_type > F_UNLCK) {
3967a5aac98SJerry Jelinek error = EINVAL;
3977a5aac98SJerry Jelinek break;
3987a5aac98SJerry Jelinek }
3997a5aac98SJerry Jelinek }
4007a5aac98SJerry Jelinek
40190221f91SCody Peter Mello if (cmd == F_OFD_GETLK || cmd == F_OFD_SETLK ||
40290221f91SCody Peter Mello cmd == F_OFD_SETLKW) {
4037a5aac98SJerry Jelinek /*
4047a5aac98SJerry Jelinek * TBD OFD-style locking is currently limited to
4057a5aac98SJerry Jelinek * covering the entire file.
4067a5aac98SJerry Jelinek */
4077a5aac98SJerry Jelinek if (bf.l_whence != 0 || bf.l_start != 0 ||
4087a5aac98SJerry Jelinek bf.l_len != 0) {
4097a5aac98SJerry Jelinek error = EINVAL;
4107a5aac98SJerry Jelinek break;
4117a5aac98SJerry Jelinek }
4127a5aac98SJerry Jelinek }
4137a5aac98SJerry Jelinek
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate * Not all of the filesystems understand F_O_GETLK, and
4167c478bd9Sstevel@tonic-gate * there's no need for them to know. Map it to F_GETLK.
4177a5aac98SJerry Jelinek *
4187a5aac98SJerry Jelinek * The *_frlock functions in the various file systems basically
4197a5aac98SJerry Jelinek * do some validation and then funnel everything through the
4207a5aac98SJerry Jelinek * fs_frlock function. For OFD-style locks fs_frlock will do
4217a5aac98SJerry Jelinek * nothing so that once control returns here we can call the
4227a5aac98SJerry Jelinek * ofdlock function with the correct fp. For OFD-style locks
4237a5aac98SJerry Jelinek * the unsupported remote file systems, such as NFS, detect and
4247a5aac98SJerry Jelinek * reject the OFD-style cmd argument.
4257c478bd9Sstevel@tonic-gate */
4267c478bd9Sstevel@tonic-gate if ((error = VOP_FRLOCK(vp, (cmd == F_O_GETLK) ? F_GETLK : cmd,
427da6c28aaSamw &bf, flag, offset, NULL, fp->f_cred, NULL)) != 0)
4287c478bd9Sstevel@tonic-gate break;
4297c478bd9Sstevel@tonic-gate
4307a5aac98SJerry Jelinek if (cmd == F_FLOCK || cmd == F_FLOCKW || cmd == F_OFD_GETLK ||
4317a5aac98SJerry Jelinek cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
4327a5aac98SJerry Jelinek /*
4337a5aac98SJerry Jelinek * This is an OFD-style lock so we need to handle it
4347a5aac98SJerry Jelinek * here. Because OFD-style locks are associated with
4357a5aac98SJerry Jelinek * the file_t we didn't have enough info down the
4367a5aac98SJerry Jelinek * VOP_FRLOCK path immediately above.
4377a5aac98SJerry Jelinek */
4387a5aac98SJerry Jelinek if ((error = ofdlock(fp, cmd, &bf, flag, offset)) != 0)
4397a5aac98SJerry Jelinek break;
4407a5aac98SJerry Jelinek }
4417a5aac98SJerry Jelinek
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate * If command is GETLK and no lock is found, only
4447c478bd9Sstevel@tonic-gate * the type field is changed.
4457c478bd9Sstevel@tonic-gate */
4467a5aac98SJerry Jelinek if ((cmd == F_O_GETLK || cmd == F_GETLK ||
4477a5aac98SJerry Jelinek cmd == F_OFD_GETLK) && bf.l_type == F_UNLCK) {
4487c478bd9Sstevel@tonic-gate /* l_type always first entry, always a short */
4497c478bd9Sstevel@tonic-gate if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
4507c478bd9Sstevel@tonic-gate sizeof (bf.l_type)))
4517c478bd9Sstevel@tonic-gate error = EFAULT;
4527c478bd9Sstevel@tonic-gate break;
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate if (cmd == F_O_GETLK) {
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate * Return an SVR3 flock structure to the user.
4587c478bd9Sstevel@tonic-gate */
4597c478bd9Sstevel@tonic-gate obf.l_type = (int16_t)bf.l_type;
4607c478bd9Sstevel@tonic-gate obf.l_whence = (int16_t)bf.l_whence;
4617c478bd9Sstevel@tonic-gate obf.l_start = (int32_t)bf.l_start;
4627c478bd9Sstevel@tonic-gate obf.l_len = (int32_t)bf.l_len;
4637c478bd9Sstevel@tonic-gate if (bf.l_sysid > SHRT_MAX || bf.l_pid > SHRT_MAX) {
4647c478bd9Sstevel@tonic-gate /*
4657c478bd9Sstevel@tonic-gate * One or both values for the above fields
4667c478bd9Sstevel@tonic-gate * is too large to store in an SVR3 flock
4677c478bd9Sstevel@tonic-gate * structure.
4687c478bd9Sstevel@tonic-gate */
4697c478bd9Sstevel@tonic-gate error = EOVERFLOW;
4707c478bd9Sstevel@tonic-gate break;
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate obf.l_sysid = (int16_t)bf.l_sysid;
4737c478bd9Sstevel@tonic-gate obf.l_pid = (int16_t)bf.l_pid;
4747c478bd9Sstevel@tonic-gate if (copyout(&obf, (void *)arg, sizeof (obf)))
4757c478bd9Sstevel@tonic-gate error = EFAULT;
4767a5aac98SJerry Jelinek } else if (cmd == F_GETLK || cmd == F_OFD_GETLK) {
4777c478bd9Sstevel@tonic-gate /*
4787c478bd9Sstevel@tonic-gate * Copy out SVR4 flock.
4797c478bd9Sstevel@tonic-gate */
4807c478bd9Sstevel@tonic-gate int i;
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate if (bf.l_start > maxoffset || bf.l_len > maxoffset) {
4837c478bd9Sstevel@tonic-gate error = EOVERFLOW;
4847c478bd9Sstevel@tonic-gate break;
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate
4877c478bd9Sstevel@tonic-gate if (datamodel == DATAMODEL_NATIVE) {
4887c478bd9Sstevel@tonic-gate for (i = 0; i < 4; i++)
4897c478bd9Sstevel@tonic-gate sbf.l_pad[i] = 0;
4907c478bd9Sstevel@tonic-gate /*
4917c478bd9Sstevel@tonic-gate * XXX In an LP64 kernel with an LP64
4927c478bd9Sstevel@tonic-gate * application there's no need to do a
4937c478bd9Sstevel@tonic-gate * structure copy here as currently
4947c478bd9Sstevel@tonic-gate * struct flock == struct flock64.
4957c478bd9Sstevel@tonic-gate * We did it this way to avoid more
4967c478bd9Sstevel@tonic-gate * conditional compilation.
4977c478bd9Sstevel@tonic-gate */
4987c478bd9Sstevel@tonic-gate sbf.l_type = bf.l_type;
4997c478bd9Sstevel@tonic-gate sbf.l_whence = bf.l_whence;
5007c478bd9Sstevel@tonic-gate sbf.l_start = (off_t)bf.l_start;
5017c478bd9Sstevel@tonic-gate sbf.l_len = (off_t)bf.l_len;
5027c478bd9Sstevel@tonic-gate sbf.l_sysid = bf.l_sysid;
5037c478bd9Sstevel@tonic-gate sbf.l_pid = bf.l_pid;
5047c478bd9Sstevel@tonic-gate if (copyout(&sbf, (void *)arg, sizeof (sbf)))
5057c478bd9Sstevel@tonic-gate error = EFAULT;
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
5087c478bd9Sstevel@tonic-gate else {
5097c478bd9Sstevel@tonic-gate struct flock32 sbf32;
5107c478bd9Sstevel@tonic-gate if (bf.l_start > MAXOFF32_T ||
5117c478bd9Sstevel@tonic-gate bf.l_len > MAXOFF32_T) {
5127c478bd9Sstevel@tonic-gate error = EOVERFLOW;
5137c478bd9Sstevel@tonic-gate break;
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate for (i = 0; i < 4; i++)
5167c478bd9Sstevel@tonic-gate sbf32.l_pad[i] = 0;
5177c478bd9Sstevel@tonic-gate sbf32.l_type = (int16_t)bf.l_type;
5187c478bd9Sstevel@tonic-gate sbf32.l_whence = (int16_t)bf.l_whence;
5197c478bd9Sstevel@tonic-gate sbf32.l_start = (off32_t)bf.l_start;
5207c478bd9Sstevel@tonic-gate sbf32.l_len = (off32_t)bf.l_len;
5217c478bd9Sstevel@tonic-gate sbf32.l_sysid = (int32_t)bf.l_sysid;
5227c478bd9Sstevel@tonic-gate sbf32.l_pid = (pid32_t)bf.l_pid;
5237c478bd9Sstevel@tonic-gate if (copyout(&sbf32,
5247c478bd9Sstevel@tonic-gate (void *)arg, sizeof (sbf32)))
5257c478bd9Sstevel@tonic-gate error = EFAULT;
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate #endif
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate break;
5307c478bd9Sstevel@tonic-gate
5317c478bd9Sstevel@tonic-gate case F_CHKFL:
5327c478bd9Sstevel@tonic-gate /*
5337c478bd9Sstevel@tonic-gate * This is for internal use only, to allow the vnode layer
5347c478bd9Sstevel@tonic-gate * to validate a flags setting before applying it. User
5357c478bd9Sstevel@tonic-gate * programs can't issue it.
5367c478bd9Sstevel@tonic-gate */
5377c478bd9Sstevel@tonic-gate error = EINVAL;
5387c478bd9Sstevel@tonic-gate break;
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate case F_ALLOCSP:
5417c478bd9Sstevel@tonic-gate case F_FREESP:
542303bf60bSsdebnath case F_ALLOCSP64:
543303bf60bSsdebnath case F_FREESP64:
544019c3c43Sraf /*
545019c3c43Sraf * Test for not-a-regular-file (and returning EINVAL)
546019c3c43Sraf * before testing for open-for-writing (and returning EBADF).
547019c3c43Sraf * This is relied upon by posix_fallocate() in libc.
548019c3c43Sraf */
549019c3c43Sraf if (vp->v_type != VREG) {
550019c3c43Sraf error = EINVAL;
5517c478bd9Sstevel@tonic-gate break;
5527c478bd9Sstevel@tonic-gate }
553303bf60bSsdebnath
554019c3c43Sraf if ((flag & FWRITE) == 0) {
555019c3c43Sraf error = EBADF;
5567c478bd9Sstevel@tonic-gate break;
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate
559303bf60bSsdebnath if (datamodel != DATAMODEL_ILP32 &&
560303bf60bSsdebnath (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
561303bf60bSsdebnath error = EINVAL;
562303bf60bSsdebnath break;
563303bf60bSsdebnath }
564303bf60bSsdebnath
5657c478bd9Sstevel@tonic-gate #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
566303bf60bSsdebnath if (datamodel == DATAMODEL_ILP32 &&
567303bf60bSsdebnath (cmd == F_ALLOCSP || cmd == F_FREESP)) {
5687c478bd9Sstevel@tonic-gate struct flock32 sbf32;
5697c478bd9Sstevel@tonic-gate /*
5707c478bd9Sstevel@tonic-gate * For compatibility we overlay an SVR3 flock on an SVR4
5717c478bd9Sstevel@tonic-gate * flock. This works because the input field offsets
5727c478bd9Sstevel@tonic-gate * in "struct flock" were preserved.
5737c478bd9Sstevel@tonic-gate */
5747c478bd9Sstevel@tonic-gate if (copyin((void *)arg, &sbf32, sizeof (sbf32))) {
5757c478bd9Sstevel@tonic-gate error = EFAULT;
5767c478bd9Sstevel@tonic-gate break;
5777c478bd9Sstevel@tonic-gate } else {
5787c478bd9Sstevel@tonic-gate bf.l_type = sbf32.l_type;
5797c478bd9Sstevel@tonic-gate bf.l_whence = sbf32.l_whence;
5807c478bd9Sstevel@tonic-gate bf.l_start = (off64_t)sbf32.l_start;
5817c478bd9Sstevel@tonic-gate bf.l_len = (off64_t)sbf32.l_len;
5827c478bd9Sstevel@tonic-gate bf.l_sysid = sbf32.l_sysid;
5837c478bd9Sstevel@tonic-gate bf.l_pid = sbf32.l_pid;
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate #endif /* _ILP32 || _SYSCALL32_IMPL */
5877c478bd9Sstevel@tonic-gate
5887c478bd9Sstevel@tonic-gate #if defined(_LP64)
589303bf60bSsdebnath if (datamodel == DATAMODEL_LP64 &&
590303bf60bSsdebnath (cmd == F_ALLOCSP || cmd == F_FREESP)) {
5917c478bd9Sstevel@tonic-gate if (copyin((void *)arg, &bf, sizeof (bf))) {
5927c478bd9Sstevel@tonic-gate error = EFAULT;
5937c478bd9Sstevel@tonic-gate break;
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate }
596303bf60bSsdebnath #endif /* defined(_LP64) */
5977c478bd9Sstevel@tonic-gate
598303bf60bSsdebnath #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
599303bf60bSsdebnath if (datamodel == DATAMODEL_ILP32 &&
600303bf60bSsdebnath (cmd == F_ALLOCSP64 || cmd == F_FREESP64)) {
601303bf60bSsdebnath if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
602303bf60bSsdebnath error = EFAULT;
603303bf60bSsdebnath break;
604303bf60bSsdebnath } else {
605303bf60bSsdebnath /*
606303bf60bSsdebnath * Note that the size of flock64 is different in
607303bf60bSsdebnath * the ILP32 and LP64 models, due to the l_pad
608303bf60bSsdebnath * field. We do not want to assume that the
609303bf60bSsdebnath * flock64 structure is laid out the same in
610303bf60bSsdebnath * ILP32 and LP64 environments, so we will
611303bf60bSsdebnath * copy in the ILP32 version of flock64
612303bf60bSsdebnath * explicitly and copy it to the native
613303bf60bSsdebnath * flock64 structure.
614303bf60bSsdebnath */
615303bf60bSsdebnath bf.l_type = (short)bf64_32.l_type;
616303bf60bSsdebnath bf.l_whence = (short)bf64_32.l_whence;
617303bf60bSsdebnath bf.l_start = bf64_32.l_start;
618303bf60bSsdebnath bf.l_len = bf64_32.l_len;
619303bf60bSsdebnath bf.l_sysid = (int)bf64_32.l_sysid;
620303bf60bSsdebnath bf.l_pid = (pid_t)bf64_32.l_pid;
621303bf60bSsdebnath }
622303bf60bSsdebnath }
623303bf60bSsdebnath #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
624303bf60bSsdebnath
625303bf60bSsdebnath if (cmd == F_ALLOCSP || cmd == F_FREESP)
626303bf60bSsdebnath error = flock_check(vp, &bf, offset, maxoffset);
627303bf60bSsdebnath else if (cmd == F_ALLOCSP64 || cmd == F_FREESP64)
628303bf60bSsdebnath error = flock_check(vp, &bf, offset, MAXOFFSET_T);
629303bf60bSsdebnath if (error)
6307c478bd9Sstevel@tonic-gate break;
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate if (vp->v_type == VREG && bf.l_len == 0 &&
6337c478bd9Sstevel@tonic-gate bf.l_start > OFFSET_MAX(fp)) {
6347c478bd9Sstevel@tonic-gate error = EFBIG;
6357c478bd9Sstevel@tonic-gate break;
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate
6387c478bd9Sstevel@tonic-gate /*
6397c478bd9Sstevel@tonic-gate * Make sure that there are no conflicting non-blocking
6407c478bd9Sstevel@tonic-gate * mandatory locks in the region being manipulated. If
6417c478bd9Sstevel@tonic-gate * there are such locks then return EACCES.
6427c478bd9Sstevel@tonic-gate */
6437c478bd9Sstevel@tonic-gate if ((error = flock_get_start(vp, &bf, offset, &start)) != 0)
6447c478bd9Sstevel@tonic-gate break;
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate if (nbl_need_check(vp)) {
6477c478bd9Sstevel@tonic-gate u_offset_t begin;
6487c478bd9Sstevel@tonic-gate ssize_t length;
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate nbl_start_crit(vp, RW_READER);
6517c478bd9Sstevel@tonic-gate in_crit = 1;
6527c478bd9Sstevel@tonic-gate vattr.va_mask = AT_SIZE;
653da6c28aaSamw if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
654da6c28aaSamw != 0)
6557c478bd9Sstevel@tonic-gate break;
6567c478bd9Sstevel@tonic-gate begin = start > vattr.va_size ? vattr.va_size : start;
6577c478bd9Sstevel@tonic-gate length = vattr.va_size > start ? vattr.va_size - start :
6587c478bd9Sstevel@tonic-gate start - vattr.va_size;
659da6c28aaSamw if (nbl_conflict(vp, NBL_WRITE, begin, length, 0,
660da6c28aaSamw NULL)) {
6617c478bd9Sstevel@tonic-gate error = EACCES;
6627c478bd9Sstevel@tonic-gate break;
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate }
665303bf60bSsdebnath
666303bf60bSsdebnath if (cmd == F_ALLOCSP64)
667303bf60bSsdebnath cmd = F_ALLOCSP;
668303bf60bSsdebnath else if (cmd == F_FREESP64)
669303bf60bSsdebnath cmd = F_FREESP;
670303bf60bSsdebnath
6717c478bd9Sstevel@tonic-gate error = VOP_SPACE(vp, cmd, &bf, flag, offset, fp->f_cred, NULL);
672303bf60bSsdebnath
6737c478bd9Sstevel@tonic-gate break;
6747c478bd9Sstevel@tonic-gate
6757c478bd9Sstevel@tonic-gate #if !defined(_LP64) || defined(_SYSCALL32_IMPL)
6767c478bd9Sstevel@tonic-gate case F_GETLK64:
6777c478bd9Sstevel@tonic-gate case F_SETLK64:
6787c478bd9Sstevel@tonic-gate case F_SETLKW64:
6797c478bd9Sstevel@tonic-gate case F_SETLK64_NBMAND:
6807a5aac98SJerry Jelinek case F_OFD_GETLK64:
6817a5aac98SJerry Jelinek case F_OFD_SETLK64:
6827a5aac98SJerry Jelinek case F_OFD_SETLKW64:
6837a5aac98SJerry Jelinek case F_FLOCK64:
6847a5aac98SJerry Jelinek case F_FLOCKW64:
6857c478bd9Sstevel@tonic-gate /*
6867c478bd9Sstevel@tonic-gate * Large Files: Here we set cmd as *LK and send it to
6877c478bd9Sstevel@tonic-gate * lower layers. *LK64 is only for the user land.
6887c478bd9Sstevel@tonic-gate * Most of the comments described above for F_SETLK
6897c478bd9Sstevel@tonic-gate * applies here too.
6907c478bd9Sstevel@tonic-gate * Large File support is only needed for ILP32 apps!
6917c478bd9Sstevel@tonic-gate */
6927c478bd9Sstevel@tonic-gate if (datamodel != DATAMODEL_ILP32) {
6937c478bd9Sstevel@tonic-gate error = EINVAL;
6947c478bd9Sstevel@tonic-gate break;
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate
6977c478bd9Sstevel@tonic-gate if (cmd == F_GETLK64)
6987c478bd9Sstevel@tonic-gate cmd = F_GETLK;
6997c478bd9Sstevel@tonic-gate else if (cmd == F_SETLK64)
7007c478bd9Sstevel@tonic-gate cmd = F_SETLK;
7017c478bd9Sstevel@tonic-gate else if (cmd == F_SETLKW64)
7027c478bd9Sstevel@tonic-gate cmd = F_SETLKW;
7037c478bd9Sstevel@tonic-gate else if (cmd == F_SETLK64_NBMAND)
7047c478bd9Sstevel@tonic-gate cmd = F_SETLK_NBMAND;
7057a5aac98SJerry Jelinek else if (cmd == F_OFD_GETLK64)
7067a5aac98SJerry Jelinek cmd = F_OFD_GETLK;
7077a5aac98SJerry Jelinek else if (cmd == F_OFD_SETLK64)
7087a5aac98SJerry Jelinek cmd = F_OFD_SETLK;
7097a5aac98SJerry Jelinek else if (cmd == F_OFD_SETLKW64)
7107a5aac98SJerry Jelinek cmd = F_OFD_SETLKW;
7117a5aac98SJerry Jelinek else if (cmd == F_FLOCK64)
7127a5aac98SJerry Jelinek cmd = F_FLOCK;
7137a5aac98SJerry Jelinek else if (cmd == F_FLOCKW64)
7147a5aac98SJerry Jelinek cmd = F_FLOCKW;
7157c478bd9Sstevel@tonic-gate
7167c478bd9Sstevel@tonic-gate /*
7177c478bd9Sstevel@tonic-gate * Note that the size of flock64 is different in the ILP32
7187c478bd9Sstevel@tonic-gate * and LP64 models, due to the sucking l_pad field.
7197c478bd9Sstevel@tonic-gate * We do not want to assume that the flock64 structure is
7207c478bd9Sstevel@tonic-gate * laid out in the same in ILP32 and LP64 environments, so
7217c478bd9Sstevel@tonic-gate * we will copy in the ILP32 version of flock64 explicitly
7227c478bd9Sstevel@tonic-gate * and copy it to the native flock64 structure.
7237c478bd9Sstevel@tonic-gate */
7247c478bd9Sstevel@tonic-gate
7257c478bd9Sstevel@tonic-gate if (copyin((void *)arg, &bf64_32, sizeof (bf64_32))) {
7267c478bd9Sstevel@tonic-gate error = EFAULT;
7277c478bd9Sstevel@tonic-gate break;
7287c478bd9Sstevel@tonic-gate }
729303bf60bSsdebnath
7307c478bd9Sstevel@tonic-gate bf.l_type = (short)bf64_32.l_type;
7317c478bd9Sstevel@tonic-gate bf.l_whence = (short)bf64_32.l_whence;
7327c478bd9Sstevel@tonic-gate bf.l_start = bf64_32.l_start;
7337c478bd9Sstevel@tonic-gate bf.l_len = bf64_32.l_len;
7347c478bd9Sstevel@tonic-gate bf.l_sysid = (int)bf64_32.l_sysid;
7357c478bd9Sstevel@tonic-gate bf.l_pid = (pid_t)bf64_32.l_pid;
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate if ((error = flock_check(vp, &bf, offset, MAXOFFSET_T)) != 0)
7387c478bd9Sstevel@tonic-gate break;
7397c478bd9Sstevel@tonic-gate
7407a5aac98SJerry Jelinek if (cmd == F_FLOCK || cmd == F_FLOCKW) {
7417a5aac98SJerry Jelinek /* FLOCK* locking is always over the entire file. */
7427a5aac98SJerry Jelinek if (bf.l_whence != 0 || bf.l_start != 0 ||
7437a5aac98SJerry Jelinek bf.l_len != 0) {
7447a5aac98SJerry Jelinek error = EINVAL;
7457a5aac98SJerry Jelinek break;
7467a5aac98SJerry Jelinek }
7477a5aac98SJerry Jelinek if (bf.l_type < F_RDLCK || bf.l_type > F_UNLCK) {
7487a5aac98SJerry Jelinek error = EINVAL;
7497a5aac98SJerry Jelinek break;
7507a5aac98SJerry Jelinek }
7517a5aac98SJerry Jelinek }
7527a5aac98SJerry Jelinek
7539c7f1ae0SJerry Jelinek if (cmd == F_OFD_GETLK || cmd == F_OFD_SETLK ||
7549c7f1ae0SJerry Jelinek cmd == F_OFD_SETLKW) {
7557a5aac98SJerry Jelinek /*
7567a5aac98SJerry Jelinek * TBD OFD-style locking is currently limited to
7577a5aac98SJerry Jelinek * covering the entire file.
7587a5aac98SJerry Jelinek */
7597a5aac98SJerry Jelinek if (bf.l_whence != 0 || bf.l_start != 0 ||
7607a5aac98SJerry Jelinek bf.l_len != 0) {
7617a5aac98SJerry Jelinek error = EINVAL;
7627a5aac98SJerry Jelinek break;
7637a5aac98SJerry Jelinek }
7647a5aac98SJerry Jelinek }
7657a5aac98SJerry Jelinek
7667a5aac98SJerry Jelinek /*
7677a5aac98SJerry Jelinek * The *_frlock functions in the various file systems basically
7687a5aac98SJerry Jelinek * do some validation and then funnel everything through the
7697a5aac98SJerry Jelinek * fs_frlock function. For OFD-style locks fs_frlock will do
7707a5aac98SJerry Jelinek * nothing so that once control returns here we can call the
7717a5aac98SJerry Jelinek * ofdlock function with the correct fp. For OFD-style locks
7727a5aac98SJerry Jelinek * the unsupported remote file systems, such as NFS, detect and
7737a5aac98SJerry Jelinek * reject the OFD-style cmd argument.
7747a5aac98SJerry Jelinek */
7757c478bd9Sstevel@tonic-gate if ((error = VOP_FRLOCK(vp, cmd, &bf, flag, offset,
776da6c28aaSamw NULL, fp->f_cred, NULL)) != 0)
7777c478bd9Sstevel@tonic-gate break;
7787c478bd9Sstevel@tonic-gate
7797a5aac98SJerry Jelinek if (cmd == F_FLOCK || cmd == F_FLOCKW || cmd == F_OFD_GETLK ||
7807a5aac98SJerry Jelinek cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW) {
7817a5aac98SJerry Jelinek /*
7827a5aac98SJerry Jelinek * This is an OFD-style lock so we need to handle it
7837a5aac98SJerry Jelinek * here. Because OFD-style locks are associated with
7847a5aac98SJerry Jelinek * the file_t we didn't have enough info down the
7857a5aac98SJerry Jelinek * VOP_FRLOCK path immediately above.
7867a5aac98SJerry Jelinek */
7877a5aac98SJerry Jelinek if ((error = ofdlock(fp, cmd, &bf, flag, offset)) != 0)
7887a5aac98SJerry Jelinek break;
7897a5aac98SJerry Jelinek }
7907a5aac98SJerry Jelinek
7917a5aac98SJerry Jelinek if ((cmd == F_GETLK || cmd == F_OFD_GETLK) &&
7927a5aac98SJerry Jelinek bf.l_type == F_UNLCK) {
7937c478bd9Sstevel@tonic-gate if (copyout(&bf.l_type, &((struct flock *)arg)->l_type,
7947c478bd9Sstevel@tonic-gate sizeof (bf.l_type)))
7957c478bd9Sstevel@tonic-gate error = EFAULT;
7967c478bd9Sstevel@tonic-gate break;
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate
7997a5aac98SJerry Jelinek if (cmd == F_GETLK || cmd == F_OFD_GETLK) {
8007c478bd9Sstevel@tonic-gate int i;
8017c478bd9Sstevel@tonic-gate
8027c478bd9Sstevel@tonic-gate /*
8037c478bd9Sstevel@tonic-gate * We do not want to assume that the flock64 structure
8047c478bd9Sstevel@tonic-gate * is laid out in the same in ILP32 and LP64
8057c478bd9Sstevel@tonic-gate * environments, so we will copy out the ILP32 version
8067c478bd9Sstevel@tonic-gate * of flock64 explicitly after copying the native
8077c478bd9Sstevel@tonic-gate * flock64 structure to it.
8087c478bd9Sstevel@tonic-gate */
8097c478bd9Sstevel@tonic-gate for (i = 0; i < 4; i++)
8107c478bd9Sstevel@tonic-gate bf64_32.l_pad[i] = 0;
8117c478bd9Sstevel@tonic-gate bf64_32.l_type = (int16_t)bf.l_type;
8127c478bd9Sstevel@tonic-gate bf64_32.l_whence = (int16_t)bf.l_whence;
8137c478bd9Sstevel@tonic-gate bf64_32.l_start = bf.l_start;
8147c478bd9Sstevel@tonic-gate bf64_32.l_len = bf.l_len;
8157c478bd9Sstevel@tonic-gate bf64_32.l_sysid = (int32_t)bf.l_sysid;
8167c478bd9Sstevel@tonic-gate bf64_32.l_pid = (pid32_t)bf.l_pid;
8177c478bd9Sstevel@tonic-gate if (copyout(&bf64_32, (void *)arg, sizeof (bf64_32)))
8187c478bd9Sstevel@tonic-gate error = EFAULT;
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate break;
821303bf60bSsdebnath #endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
8227c478bd9Sstevel@tonic-gate
8237c478bd9Sstevel@tonic-gate case F_SHARE:
8247c478bd9Sstevel@tonic-gate case F_SHARE_NBMAND:
8257c478bd9Sstevel@tonic-gate case F_UNSHARE:
8267c478bd9Sstevel@tonic-gate
8277c478bd9Sstevel@tonic-gate /*
8287c478bd9Sstevel@tonic-gate * Copy in input fields only.
8297c478bd9Sstevel@tonic-gate */
8307c478bd9Sstevel@tonic-gate if (copyin((void *)arg, &fsh, sizeof (fsh))) {
8317c478bd9Sstevel@tonic-gate error = EFAULT;
8327c478bd9Sstevel@tonic-gate break;
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate
8357c478bd9Sstevel@tonic-gate /*
8367c478bd9Sstevel@tonic-gate * Local share reservations always have this simple form
8377c478bd9Sstevel@tonic-gate */
8387c478bd9Sstevel@tonic-gate shr.s_access = fsh.f_access;
8397c478bd9Sstevel@tonic-gate shr.s_deny = fsh.f_deny;
8407c478bd9Sstevel@tonic-gate shr.s_sysid = 0;
8417c478bd9Sstevel@tonic-gate shr.s_pid = ttoproc(curthread)->p_pid;
8427c478bd9Sstevel@tonic-gate shr_own.sl_pid = shr.s_pid;
8437c478bd9Sstevel@tonic-gate shr_own.sl_id = fsh.f_id;
8447c478bd9Sstevel@tonic-gate shr.s_own_len = sizeof (shr_own);
8457c478bd9Sstevel@tonic-gate shr.s_owner = (caddr_t)&shr_own;
846da6c28aaSamw error = VOP_SHRLOCK(vp, cmd, &shr, flag, fp->f_cred, NULL);
8477c478bd9Sstevel@tonic-gate break;
8487c478bd9Sstevel@tonic-gate
8497c478bd9Sstevel@tonic-gate default:
8507c478bd9Sstevel@tonic-gate error = EINVAL;
8517c478bd9Sstevel@tonic-gate break;
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate
8547c478bd9Sstevel@tonic-gate if (in_crit)
8557c478bd9Sstevel@tonic-gate nbl_end_crit(vp);
8567c478bd9Sstevel@tonic-gate
8577c478bd9Sstevel@tonic-gate done:
8587c478bd9Sstevel@tonic-gate releasef(fdes);
8597c478bd9Sstevel@tonic-gate out:
8607c478bd9Sstevel@tonic-gate if (error)
8617c478bd9Sstevel@tonic-gate return (set_errno(error));
8627c478bd9Sstevel@tonic-gate return (retval);
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate
8657c478bd9Sstevel@tonic-gate int
flock_check(vnode_t * vp,flock64_t * flp,offset_t offset,offset_t max)8667c478bd9Sstevel@tonic-gate flock_check(vnode_t *vp, flock64_t *flp, offset_t offset, offset_t max)
8677c478bd9Sstevel@tonic-gate {
8687c478bd9Sstevel@tonic-gate struct vattr vattr;
8697c478bd9Sstevel@tonic-gate int error;
8707c478bd9Sstevel@tonic-gate u_offset_t start, end;
8717c478bd9Sstevel@tonic-gate
8727c478bd9Sstevel@tonic-gate /*
8737c478bd9Sstevel@tonic-gate * Determine the starting point of the request
8747c478bd9Sstevel@tonic-gate */
8757c478bd9Sstevel@tonic-gate switch (flp->l_whence) {
8767c478bd9Sstevel@tonic-gate case 0: /* SEEK_SET */
8777c478bd9Sstevel@tonic-gate start = (u_offset_t)flp->l_start;
8787c478bd9Sstevel@tonic-gate if (start > max)
8797c478bd9Sstevel@tonic-gate return (EINVAL);
8807c478bd9Sstevel@tonic-gate break;
8817c478bd9Sstevel@tonic-gate case 1: /* SEEK_CUR */
8827c478bd9Sstevel@tonic-gate if (flp->l_start > (max - offset))
8837c478bd9Sstevel@tonic-gate return (EOVERFLOW);
8847c478bd9Sstevel@tonic-gate start = (u_offset_t)(flp->l_start + offset);
8857c478bd9Sstevel@tonic-gate if (start > max)
8867c478bd9Sstevel@tonic-gate return (EINVAL);
8877c478bd9Sstevel@tonic-gate break;
8887c478bd9Sstevel@tonic-gate case 2: /* SEEK_END */
8897c478bd9Sstevel@tonic-gate vattr.va_mask = AT_SIZE;
890da6c28aaSamw if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
8917c478bd9Sstevel@tonic-gate return (error);
8927c478bd9Sstevel@tonic-gate if (flp->l_start > (max - (offset_t)vattr.va_size))
8937c478bd9Sstevel@tonic-gate return (EOVERFLOW);
8947c478bd9Sstevel@tonic-gate start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
8957c478bd9Sstevel@tonic-gate if (start > max)
8967c478bd9Sstevel@tonic-gate return (EINVAL);
8977c478bd9Sstevel@tonic-gate break;
8987c478bd9Sstevel@tonic-gate default:
8997c478bd9Sstevel@tonic-gate return (EINVAL);
9007c478bd9Sstevel@tonic-gate }
9017c478bd9Sstevel@tonic-gate
9027c478bd9Sstevel@tonic-gate /*
9037c478bd9Sstevel@tonic-gate * Determine the range covered by the request.
9047c478bd9Sstevel@tonic-gate */
9057c478bd9Sstevel@tonic-gate if (flp->l_len == 0)
9067c478bd9Sstevel@tonic-gate end = MAXEND;
9077c478bd9Sstevel@tonic-gate else if ((offset_t)flp->l_len > 0) {
9087c478bd9Sstevel@tonic-gate if (flp->l_len > (max - start + 1))
9097c478bd9Sstevel@tonic-gate return (EOVERFLOW);
9107c478bd9Sstevel@tonic-gate end = (u_offset_t)(start + (flp->l_len - 1));
9117c478bd9Sstevel@tonic-gate ASSERT(end <= max);
9127c478bd9Sstevel@tonic-gate } else {
9137c478bd9Sstevel@tonic-gate /*
9147c478bd9Sstevel@tonic-gate * Negative length; why do we even allow this ?
9157c478bd9Sstevel@tonic-gate * Because this allows easy specification of
9167c478bd9Sstevel@tonic-gate * the last n bytes of the file.
9177c478bd9Sstevel@tonic-gate */
9187c478bd9Sstevel@tonic-gate end = start;
9197c478bd9Sstevel@tonic-gate start += (u_offset_t)flp->l_len;
9207c478bd9Sstevel@tonic-gate (start)++;
9217c478bd9Sstevel@tonic-gate if (start > max)
9227c478bd9Sstevel@tonic-gate return (EINVAL);
9237c478bd9Sstevel@tonic-gate ASSERT(end <= max);
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate ASSERT(start <= max);
9267c478bd9Sstevel@tonic-gate if (flp->l_type == F_UNLCK && flp->l_len > 0 &&
9277c478bd9Sstevel@tonic-gate end == (offset_t)max) {
9287c478bd9Sstevel@tonic-gate flp->l_len = 0;
9297c478bd9Sstevel@tonic-gate }
9307c478bd9Sstevel@tonic-gate if (start > end)
9317c478bd9Sstevel@tonic-gate return (EINVAL);
9327c478bd9Sstevel@tonic-gate return (0);
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate
9357c478bd9Sstevel@tonic-gate static int
flock_get_start(vnode_t * vp,flock64_t * flp,offset_t offset,u_offset_t * start)9367c478bd9Sstevel@tonic-gate flock_get_start(vnode_t *vp, flock64_t *flp, offset_t offset, u_offset_t *start)
9377c478bd9Sstevel@tonic-gate {
9387c478bd9Sstevel@tonic-gate struct vattr vattr;
9397c478bd9Sstevel@tonic-gate int error;
9407c478bd9Sstevel@tonic-gate
9417c478bd9Sstevel@tonic-gate /*
9427c478bd9Sstevel@tonic-gate * Determine the starting point of the request. Assume that it is
9437c478bd9Sstevel@tonic-gate * a valid starting point.
9447c478bd9Sstevel@tonic-gate */
9457c478bd9Sstevel@tonic-gate switch (flp->l_whence) {
9467c478bd9Sstevel@tonic-gate case 0: /* SEEK_SET */
9477c478bd9Sstevel@tonic-gate *start = (u_offset_t)flp->l_start;
9487c478bd9Sstevel@tonic-gate break;
9497c478bd9Sstevel@tonic-gate case 1: /* SEEK_CUR */
9507c478bd9Sstevel@tonic-gate *start = (u_offset_t)(flp->l_start + offset);
9517c478bd9Sstevel@tonic-gate break;
9527c478bd9Sstevel@tonic-gate case 2: /* SEEK_END */
9537c478bd9Sstevel@tonic-gate vattr.va_mask = AT_SIZE;
954da6c28aaSamw if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
9557c478bd9Sstevel@tonic-gate return (error);
9567c478bd9Sstevel@tonic-gate *start = (u_offset_t)(flp->l_start + (offset_t)vattr.va_size);
9577c478bd9Sstevel@tonic-gate break;
9587c478bd9Sstevel@tonic-gate default:
9597c478bd9Sstevel@tonic-gate return (EINVAL);
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate
9627c478bd9Sstevel@tonic-gate return (0);
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate
9657c478bd9Sstevel@tonic-gate /*
9667c478bd9Sstevel@tonic-gate * Take rctl action when the requested file descriptor is too big.
9677c478bd9Sstevel@tonic-gate */
9687c478bd9Sstevel@tonic-gate static void
fd_too_big(proc_t * p)9697c478bd9Sstevel@tonic-gate fd_too_big(proc_t *p)
9707c478bd9Sstevel@tonic-gate {
9717c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
9727c478bd9Sstevel@tonic-gate (void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
9737c478bd9Sstevel@tonic-gate p->p_rctls, p, RCA_SAFE);
9747c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
9757c478bd9Sstevel@tonic-gate }
976