fcntl.c revision c0e499e09ec049c4d1cbd00993c4a458787e220c
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* ONC_PLUS EXTRACT START */
/*
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
/* ONC_PLUS EXTRACT END */
#include <sys/isa_defs.h>
#include <sys/sysmacros.h>
/* ONC_PLUS EXTRACT START */
/* ONC_PLUS EXTRACT END */
/* ONC_PLUS EXTRACT START */
static void fd_too_big(proc_t *);
/*
* File control.
*/
int
{
int iarg;
int error = 0;
int retval;
proc_t *p;
int in_crit;
int flag;
struct flock64_32 bf64_32;
struct shr_locowner shr_own;
int fdres;
#endif
#endif
/*
* First, for speed, deal with the subset of cases
* that do not require getf() / releasef().
*/
switch (cmd) {
case F_GETFD:
goto out;
case F_SETFD:
retval = 0;
goto out;
case F_GETFL:
goto out;
case F_GETXFL:
goto out;
case F_BADFD:
goto out;
}
/*
* Second, for speed, deal with the subset of cases that
* require getf() / releasef() but do not require copyin.
*/
goto out;
}
switch (cmd) {
/* ONC_PLUS EXTRACT END */
case F_DUPFD:
p = curproc;
if (iarg >= 0)
fd_too_big(p);
goto done;
}
/*
* We need to increment the f_count reference counter
* before allocating a new file descriptor.
* Doing it other way round opens a window for race condition
* with closeandsetf() on the target file descriptor which can
* close the file still referenced by the original
* file descriptor.
*/
/*
* New file descriptor can't be allocated.
* Revert the reference count.
*/
}
goto done;
case F_DUP2FD:
p = curproc;
if (iarg >= 0)
fd_too_big(p);
} else {
/*
* We can't hold our getf(fdes) across the call to
* closeandsetf() because it creates a window for
* deadlock: if one thread is doing dup2(a, b) while
* another is doing dup2(b, a), each one will block
* waiting for the other to call releasef(). The
* solution is to increment the file reference count
* (which we have to do anyway), then releasef(fdes),
* then closeandsetf(). Incrementing f_count ensures
* that fp won't disappear after we call releasef().
* When closeandsetf() fails, we try avoid calling
* closef() because of all the side effects.
*/
} else {
} else {
}
}
goto out;
}
goto done;
case F_SETFL:
0) {
}
retval = 0;
goto done;
}
/*
* Finally, deal with the expensive cases.
*/
retval = 0;
in_crit = 0;
#if defined(_SYSCALL32_IMPL)
#endif
switch (cmd) {
/* ONC_PLUS EXTRACT START */
/*
* The file system and vnode layers understand and implement
* locking with flock64 structures. So here once we pass through
* the test for compatibility as defined by LFS API, (for F_SETLK,
* F_SETLKW, F_GETLK, F_GETLKW, F_FREESP) we transform
* the flock structure to a flock64 structure and send it to the
* lower layers. Similarly in case of GETLK the returned flock64
* structure is transformed to a flock structure if everything fits
* in nicely, otherwise we return EOVERFLOW.
*/
case F_GETLK:
case F_O_GETLK:
case F_SETLK:
case F_SETLKW:
case F_SETLK_NBMAND:
/*
* Copy in input fields only.
*/
if (datamodel != DATAMODEL_ILP32) {
break;
}
break;
}
} else if (datamodel == DATAMODEL_NATIVE) {
break;
}
/*
* XXX In an LP64 kernel with an LP64 application
* there's no need to do a structure copy here
* struct flock == struct flock64. However,
* we did it this way to avoid more conditional
* compilation.
*/
}
#if defined(_SYSCALL32_IMPL)
else {
break;
}
}
#endif /* _SYSCALL32_IMPL */
/*
* 64-bit support: check for overflow for 32-bit lock ops
*/
break;
/*
* Not all of the filesystems understand F_O_GETLK, and
* there's no need for them to know. Map it to F_GETLK.
*/
break;
/*
* If command is GETLK and no lock is found, only
* the type field is changed.
*/
/* l_type always first entry, always a short */
break;
}
/*
* Return an SVR3 flock structure to the user.
*/
/*
* One or both values for the above fields
* is too large to store in an SVR3 flock
* structure.
*/
break;
}
/*
* Copy out SVR4 flock.
*/
int i;
break;
}
if (datamodel == DATAMODEL_NATIVE) {
for (i = 0; i < 4; i++)
/*
* XXX In an LP64 kernel with an LP64
* application there's no need to do a
* structure copy here as currently
* struct flock == struct flock64.
* We did it this way to avoid more
* conditional compilation.
*/
}
#if defined(_SYSCALL32_IMPL)
else {
break;
}
for (i = 0; i < 4; i++)
}
#endif
}
break;
/* ONC_PLUS EXTRACT END */
case F_CHKFL:
/*
* This is for internal use only, to allow the vnode layer
* to validate a flags setting before applying it. User
* programs can't issue it.
*/
break;
case F_ALLOCSP:
case F_FREESP:
case F_ALLOCSP64:
case F_FREESP64:
/*
* Test for not-a-regular-file (and returning EINVAL)
* before testing for open-for-writing (and returning EBADF).
* This is relied upon by posix_fallocate() in libc.
*/
break;
}
break;
}
if (datamodel != DATAMODEL_ILP32 &&
break;
}
#if defined(_ILP32) || defined(_SYSCALL32_IMPL)
if (datamodel == DATAMODEL_ILP32 &&
/*
* For compatibility we overlay an SVR3 flock on an SVR4
* flock. This works because the input field offsets
* in "struct flock" were preserved.
*/
break;
} else {
}
}
#endif /* _ILP32 || _SYSCALL32_IMPL */
#if defined(_LP64)
if (datamodel == DATAMODEL_LP64 &&
break;
}
}
#endif /* defined(_LP64) */
#if !defined(_LP64) || defined(_SYSCALL32_IMPL)
if (datamodel == DATAMODEL_ILP32 &&
break;
} else {
/*
* Note that the size of flock64 is different in
* the ILP32 and LP64 models, due to the l_pad
* field. We do not want to assume that the
* flock64 structure is laid out the same in
* ILP32 and LP64 environments, so we will
* copy in the ILP32 version of flock64
* explicitly and copy it to the native
* flock64 structure.
*/
}
}
#endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
if (error)
break;
break;
}
/*
* Make sure that there are no conflicting non-blocking
* mandatory locks in the region being manipulated. If
* there are such locks then return EACCES.
*/
break;
if (nbl_need_check(vp)) {
in_crit = 1;
!= 0)
break;
NULL)) {
break;
}
}
if (cmd == F_ALLOCSP64)
else if (cmd == F_FREESP64)
break;
#if !defined(_LP64) || defined(_SYSCALL32_IMPL)
/* ONC_PLUS EXTRACT START */
case F_GETLK64:
case F_SETLK64:
case F_SETLKW64:
case F_SETLK64_NBMAND:
/*
* Large Files: Here we set cmd as *LK and send it to
* lower layers. *LK64 is only for the user land.
* Most of the comments described above for F_SETLK
* applies here too.
* Large File support is only needed for ILP32 apps!
*/
if (datamodel != DATAMODEL_ILP32) {
break;
}
else if (cmd == F_SETLKW64)
else if (cmd == F_SETLK64_NBMAND)
/*
* Note that the size of flock64 is different in the ILP32
* and LP64 models, due to the sucking l_pad field.
* We do not want to assume that the flock64 structure is
* laid out in the same in ILP32 and LP64 environments, so
* we will copy in the ILP32 version of flock64 explicitly
* and copy it to the native flock64 structure.
*/
break;
}
break;
break;
break;
}
int i;
/*
* We do not want to assume that the flock64 structure
* is laid out in the same in ILP32 and LP64
* environments, so we will copy out the ILP32 version
* of flock64 explicitly after copying the native
* flock64 structure to it.
*/
for (i = 0; i < 4; i++)
}
break;
/* ONC_PLUS EXTRACT END */
#endif /* !defined(_LP64) || defined(_SYSCALL32_IMPL) */
/* ONC_PLUS EXTRACT START */
case F_SHARE:
case F_SHARE_NBMAND:
case F_UNSHARE:
/*
* Copy in input fields only.
*/
break;
}
/*
* Local share reservations always have this simple form
*/
/* ONC_PLUS EXTRACT END */
break;
default:
break;
}
if (in_crit)
done:
out:
if (error)
return (retval);
}
/* ONC_PLUS EXTRACT START */
int
{
int error;
/*
* Determine the starting point of the request
*/
case 0: /* SEEK_SET */
return (EINVAL);
break;
case 1: /* SEEK_CUR */
return (EOVERFLOW);
return (EINVAL);
break;
case 2: /* SEEK_END */
return (error);
return (EOVERFLOW);
return (EINVAL);
break;
default:
return (EINVAL);
}
/*
* Determine the range covered by the request.
*/
return (EOVERFLOW);
} else {
/*
* Negative length; why do we even allow this ?
* Because this allows easy specification of
* the last n bytes of the file.
*/
(start)++;
return (EINVAL);
}
}
return (EINVAL);
return (0);
}
static int
{
int error;
/*
* Determine the starting point of the request. Assume that it is
* a valid starting point.
*/
case 0: /* SEEK_SET */
break;
case 1: /* SEEK_CUR */
break;
case 2: /* SEEK_END */
return (error);
break;
default:
return (EINVAL);
}
return (0);
}
/*
* Take rctl action when the requested file descriptor is too big.
*/
static void
fd_too_big(proc_t *p)
{
mutex_enter(&p->p_lock);
mutex_exit(&p->p_lock);
}
/* ONC_PLUS EXTRACT END */