/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Andrew Doran.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1982, 1986, 1989, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95
*/
/*
* Common code for vnode open operations.
* Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
*
* at_dvp is the directory for openat(), if any.
* pb is the path.
* nmode is additional namei flags, restricted to TRYEMULROOT and NOCHROOT.
* fmode is the open flags, converted from O_* to F*
* cmode is the creation file permissions.
*
* XXX shouldn't cmode be mode_t?
*
* On success produces either a locked vnode in *ret_vp, or NULL in
* *ret_vp and a file descriptor number in *ret_fd.
*
* The caller may pass NULL for ret_fd (and ret_domove), in which case
* EOPNOTSUPP will be produced in the cases that would otherwise return
* a file descriptor.
*
* Note that callers that want no-follow behavior should pass
* O_NOFOLLOW in fmode. Neither FOLLOW nor NOFOLLOW in nmode is
* honored.
*/
int
vn_open(struct vnode *at_dvp, struct pathbuf *pb,
int nmode, int fmode, int cmode,
struct vnode **ret_vp, bool *ret_domove, int *ret_fd)
{
struct nameidata nd;
struct vnode *vp = NULL;
struct lwp *l = curlwp;
kauth_cred_t cred = l->l_cred;
struct vattr va;
int error;
const char *pathstring;
/*
* When this "interface" was exposed to do_open() it used
* to initialize l_dupfd to -newfd-1 (thus passing in the
* new file handle number to use)... but nothing in the
* kernel uses that value. So just send 0.
*/
l->l_dupfd = 0;
error = namei(&nd);
if (error)
goto out;
vp = nd.ni_vp;
#if NVERIEXEC > 0
error = veriexec_openchk(l, nd.ni_vp, pathstring, fmode);
if (error) {
/* We have to release the locks ourselves */
/*
* 20210604 dholland passing NONEXCLHACK means we can
* get ni_dvp == NULL back if ni_vp exists, and we should
* treat that like the non-O_CREAT case.
*/
if ((fmode & O_CREAT) != 0 && nd.ni_dvp != NULL) {
if (vp == NULL) {
vput(nd.ni_dvp);
} else {
VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
if (nd.ni_dvp == nd.ni_vp)
vrele(nd.ni_dvp);
else
vput(nd.ni_dvp);
nd.ni_dvp = NULL;
vput(vp);
vp = NULL;
}
} else {
vput(vp);
vp = NULL;
}
goto out;
}
#endif /* NVERIEXEC > 0 */
/*
* 20210604 dholland ditto
*/
if ((fmode & O_CREAT) != 0 && nd.ni_dvp != NULL) {
if (nd.ni_vp == NULL) {
vattr_null(&va);
va.va_type = VREG;
va.va_mode = cmode;
if (fmode & O_EXCL)
va.va_vaflags |= VA_EXCLUSIVE;
error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
&nd.ni_cnd, &va);
if (error) {
vput(nd.ni_dvp);
goto out;
}
fmode &= ~O_TRUNC;
vp = nd.ni_vp;
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
vput(nd.ni_dvp);
} else {
VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
if (nd.ni_dvp == nd.ni_vp)
vrele(nd.ni_dvp);
else
vput(nd.ni_dvp);
nd.ni_dvp = NULL;
vp = nd.ni_vp;
if (fmode & O_EXCL) {
error = SET_ERROR(EEXIST);
goto bad;
}
fmode &= ~O_CREAT;
}
} else if ((fmode & O_CREAT) != 0) {
/*
* 20210606 dholland passing NONEXCLHACK means this
* case exists; it is the same as the following one
* but also needs to do things in the second (exists)
* half of the following block. (Besides handle
* ni_dvp, anyway.)
*/
vp = nd.ni_vp;
KASSERT((fmode & O_EXCL) == 0);
fmode &= ~O_CREAT;
} else {
vp = nd.ni_vp;
}
if (vp->v_type == VSOCK) {
error = SET_ERROR(EOPNOTSUPP);
goto bad;
}
if (nd.ni_vp->v_type == VLNK) {
error = SET_ERROR(EFTYPE);
goto bad;
}
if ((fmode & O_CREAT) == 0) {
error = vn_openchk(vp, cred, fmode);
if (error != 0)
goto bad;
}
if (fmode & O_TRUNC) {
vattr_null(&va);
va.va_size = 0;
error = VOP_SETATTR(vp, &va, cred);
if (error != 0)
goto bad;
}
if ((error = VOP_OPEN(vp, fmode, cred)) != 0)
goto bad;
if (fmode & FWRITE) {
mutex_enter(vp->v_interlock);
vp->v_writecount++;
mutex_exit(vp->v_interlock);
}
/*
* Vnode close call
*
* Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
*/
int
vn_close(struct vnode *vp, int flags, kauth_cred_t cred)
{
int error;
/*
* Package up an I/O request on a vnode into a uio and do it.
*/
int
vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
struct lwp *l)
{
struct uio auio;
struct iovec aiov;
int error;
if ((error = enforce_rlimit_fsize(vp, uio, ioflag)) != 0)
goto out;
error = VOP_WRITE(vp, uio, ioflag, cred);
if (flags & FOF_UPDATE_OFFSET) {
if (ioflag & IO_APPEND) {
/*
* SUSv3 describes behaviour for count = 0 as
* following: "Before any action ... is taken,
* and if nbyte is zero and the file is a
* regular file, the write() function ... in
* the absence of errors ... shall return zero
* and have no other results."
*/
if (count)
*offset = uio->uio_offset;
} else
*offset += count - uio->uio_resid;
}
out:
VOP_UNLOCK(vp);
return error;
}
/*
* File table vnode stat routine.
*/
static int
vn_statfile(file_t *fp, struct stat *sb)
{
struct vnode *vp = fp->f_vnode;
int error;
case VREG:
case VDIR:
if (com == FIONREAD) {
vn_lock(vp, LK_SHARED | LK_RETRY);
error = VOP_GETATTR(vp, &vattr, kauth_cred_get());
if (error == 0) {
if (vp->v_type == VDIR)
mutex_enter(&fp->f_lock);
*(int *)data = vattr.va_size - fp->f_offset;
if (vp->v_type == VDIR)
mutex_exit(&fp->f_lock);
}
VOP_UNLOCK(vp);
if (error)
return error;
return 0;
}
if ((com == FIONWRITE) || (com == FIONSPACE)) {
/*
* Files don't have send queues, so there never
* are any bytes in them, nor is there any
* open space in them.
*/
*(int *)data = 0;
return 0;
}
if (com == FIOGETBMAP) {
daddr_t *block;
/*
* Old programs may not select a specific sharing type, so
* default to an appropriate one.
*
* XXX: how does MAP_ANON fit in the picture?
*/
if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) {
#if defined(DEBUG)
struct proc *p = l->l_proc;
printf("WARNING: defaulted mmap() share type to "
"%s (pid %d command %s)\n",
vp->v_type == VCHR ? "MAP_SHARED" : "MAP_PRIVATE",
p->p_pid,
p->p_comm);
#endif
if (vp->v_type == VCHR)
flags |= MAP_SHARED; /* for a device */
else
flags |= MAP_PRIVATE; /* for a file */
}
/*
* MAP_PRIVATE device mappings don't make sense (and aren't
* supported anyway). However, some programs rely on this,
* so just change it to MAP_SHARED.
*/
if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) {
flags = (flags & ~MAP_PRIVATE) | MAP_SHARED;
}
/* check write access, shared case first */
if (flags & MAP_SHARED) {
/*
* if the file is writable, only add PROT_WRITE to
* maxprot if the file is not immutable, append-only.
* otherwise, if we have asked for PROT_WRITE, return
* EPERM.
*/
if (fp->f_flag & FWRITE) {
vn_lock(vp, LK_SHARED | LK_RETRY);
error = VOP_GETATTR(vp, &va, l->l_cred);
VOP_UNLOCK(vp);
if (error) {
return error;
}
if ((va.va_flags &
(SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0)
maxprot |= VM_PROT_WRITE;
else if (prot & PROT_WRITE) {
return SET_ERROR(EPERM);
}
} else if (prot & PROT_WRITE) {
return SET_ERROR(EACCES);
}
} else {
/* MAP_PRIVATE mappings can always write to */
maxprot |= VM_PROT_WRITE;
}
/*
* Don't allow mmap for EXEC if the file system
* is mounted NOEXEC.
*/
if ((prot & PROT_EXEC) != 0 &&
(vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) {
return SET_ERROR(EACCES);
}
if (vp->v_type != VCHR) {
error = VOP_MMAP(vp, prot, curlwp->l_cred);
if (error) {
return error;
}
vref(vp);
uobj = &vp->v_uobj;
/*
* If the vnode is being mapped with PROT_EXEC,
* then mark it as text.
*/
if (prot & PROT_EXEC) {
vn_markexec(vp);
}
} else {
int i = maxprot;
/*
* XXX Some devices don't like to be mapped with
* XXX PROT_EXEC or PROT_WRITE, but we don't really
* XXX have a better way of handling this, right now
*/
do {
uobj = udv_attach(vp->v_rdev,
(flags & MAP_SHARED) ? i : (i & ~VM_PROT_WRITE),
off, size);
i--;
} while ((uobj == NULL) && (i > 0));
if (uobj == NULL) {
return SET_ERROR(EINVAL);
}
*advicep = UVM_ADV_RANDOM;
}
/*
* Set vnode flags to indicate the new kinds of mapping.
* We take the vnode lock in exclusive mode here to serialize
* with direct I/O.
*
* Safe to check for these flag values without a lock, as
* long as a reference to the vnode is held.
*/
needwritemap = (vp->v_iflag & VI_WRMAP) == 0 &&
(flags & MAP_SHARED) != 0 &&
(maxprot & VM_PROT_WRITE) != 0;
if ((vp->v_vflag & VV_MAPPED) == 0 || needwritemap) {
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
vp->v_vflag |= VV_MAPPED;
if (needwritemap) {
rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
mutex_enter(vp->v_interlock);
vp->v_iflag |= VI_WRMAP;
mutex_exit(vp->v_interlock);
rw_exit(vp->v_uobj.vmobjlock);
}
VOP_UNLOCK(vp);
}
#if NVERIEXEC > 0
/*
* Check if the file can be executed indirectly.
*
* XXX: This gives false warnings about "Incorrect access type"
* XXX: if the mapping is not executable. Harmless, but will be
* XXX: fixed as part of other changes.
*/
if (veriexec_verify(l, vp, "(mmap)", VERIEXEC_INDIRECT,
NULL)) {
/*
* Don't allow executable mappings if we can't
* indirectly execute the file.
*/
if (prot & VM_PROT_EXECUTE) {
return SET_ERROR(EPERM);
}
/*
* Strip the executable bit from 'maxprot' to make sure
* it can't be made executable later.
*/
maxprot &= ~VM_PROT_EXECUTE;
}
#endif /* NVERIEXEC > 0 */
switch (advice) {
case POSIX_FADV_WILLNEED:
case POSIX_FADV_DONTNEED:
if (vp->v_type != VREG && vp->v_type != VBLK)
return 0;
break;
}
switch (advice) {
case POSIX_FADV_NORMAL:
case POSIX_FADV_RANDOM:
case POSIX_FADV_SEQUENTIAL:
/*
* We ignore offset and size. Must lock the file to
* do this, as f_advice is sub-word sized.
*/
mutex_enter(&fp->f_lock);
fp->f_advice = (u_char)advice;
mutex_exit(&fp->f_lock);
error = 0;
break;
case POSIX_FADV_WILLNEED:
error = uvm_readahead(&vp->v_uobj, offset, endoffset - offset);
break;
case POSIX_FADV_DONTNEED:
/*
* Align the region to page boundaries as VOP_PUTPAGES expects
* by shrinking it. We shrink instead of expand because we
* do not want to deactivate cache outside of the requested
* region. It means that if the specified region is smaller
* than PAGE_SIZE, we do nothing.
*/
if (offset <= trunc_page(OFF_MAX) &&
round_page(offset) < trunc_page(endoffset)) {
rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
error = VOP_PUTPAGES(vp,
round_page(offset), trunc_page(endoffset),
PGO_DEACTIVATE | PGO_CLEANIT);
} else {
error = 0;
}
break;
case POSIX_FADV_NOREUSE:
/* Not implemented yet. */
error = 0;
break;
default:
error = SET_ERROR(EINVAL);
break;
}
return error;
}
static int
vn_truncate(file_t *fp, off_t length)
{
struct vattr vattr;
struct vnode *vp;
int error = 0;
/*
* Simplified in-kernel wrapper calls for extended attribute access.
* Both calls pass in a NULL credential, authorizing a "kernel" access.
* Set IO_NODELOCKED in ioflg if the vnode is already locked.
*/
int
vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
const char *attrname, size_t *buflen, void *bf, struct lwp *l)
{
struct uio auio;
struct iovec aiov;
int error;
/*
* Lookup the provided name in the filesystem. If the file exists,
* is a valid block device, and isn't being used by anyone else,
* set *vpp to the file's vnode.
*/
int
vn_bdev_openpath(struct pathbuf *pb, struct vnode **vpp, struct lwp *l)
{
struct vnode *vp;
dev_t dev;
enum vtype vt;
int error;
static long
vn_knote_to_interest(const struct knote *kn)
{
switch (kn->kn_filter) {
case EVFILT_READ:
/*
* Writing to the file or changing its attributes can
* set the file size, which impacts the readability
* filter.
*
* (No need to set NOTE_EXTEND here; it's only ever
* send with other hints; see vnode_if.c.)
*/
return NOTE_WRITE | NOTE_ATTRIB;
/*
* In the case of layered / stacked file systems, knotes
* should only ever be associated with the base vnode.
*/
KASSERT(kn->kn_hook == vp);
KASSERT(vp->v_klist == &VNODE_TO_VIMPL(vp)->vi_klist);
/*
* We maintain a bitmask of the kevents that there is interest in,
* to minimize the impact of having watchers. It's silly to have
* to traverse vn_klist every time a read or write happens simply
* because there is someone interested in knowing when the file
* is deleted, for example.
*/
/* See above. */
KASSERT(kn->kn_hook == vp);
KASSERT(vp->v_klist == &VNODE_TO_VIMPL(vp)->vi_klist);
/*
* We special case removing the head of the list, because:
*
* 1. It's extremely likely that we're detaching the only
* knote.
*
* 2. We're already traversing the whole list, so we don't
* want to use the generic SLIST_REMOVE() which would
* traverse it *again*.
*/