Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.239
      1  1.239  riastrad /*	$NetBSD: vfs_vnops.c,v 1.239 2023/04/22 13:52:46 riastradh Exp $	*/
      2  1.164        ad 
      3  1.164        ad /*-
      4  1.164        ad  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  1.164        ad  * All rights reserved.
      6  1.164        ad  *
      7  1.164        ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.164        ad  * by Andrew Doran.
      9  1.164        ad  *
     10  1.164        ad  * Redistribution and use in source and binary forms, with or without
     11  1.164        ad  * modification, are permitted provided that the following conditions
     12  1.164        ad  * are met:
     13  1.164        ad  * 1. Redistributions of source code must retain the above copyright
     14  1.164        ad  *    notice, this list of conditions and the following disclaimer.
     15  1.164        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.164        ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.164        ad  *    documentation and/or other materials provided with the distribution.
     18  1.164        ad  *
     19  1.164        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.164        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.164        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.164        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.164        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.164        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.164        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.164        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.164        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.164        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.164        ad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.164        ad  */
     31   1.12       cgd 
     32   1.10       cgd /*
     33   1.11   mycroft  * Copyright (c) 1982, 1986, 1989, 1993
     34   1.11   mycroft  *	The Regents of the University of California.  All rights reserved.
     35   1.10       cgd  * (c) UNIX System Laboratories, Inc.
     36   1.10       cgd  * All or some portions of this file are derived from material licensed
     37   1.10       cgd  * to the University of California by American Telephone and Telegraph
     38   1.10       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     39   1.10       cgd  * the permission of UNIX System Laboratories, Inc.
     40   1.10       cgd  *
     41   1.10       cgd  * Redistribution and use in source and binary forms, with or without
     42   1.10       cgd  * modification, are permitted provided that the following conditions
     43   1.10       cgd  * are met:
     44   1.10       cgd  * 1. Redistributions of source code must retain the above copyright
     45   1.10       cgd  *    notice, this list of conditions and the following disclaimer.
     46   1.10       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     47   1.10       cgd  *    notice, this list of conditions and the following disclaimer in the
     48   1.10       cgd  *    documentation and/or other materials provided with the distribution.
     49   1.73       agc  * 3. Neither the name of the University nor the names of its contributors
     50   1.10       cgd  *    may be used to endorse or promote products derived from this software
     51   1.10       cgd  *    without specific prior written permission.
     52   1.10       cgd  *
     53   1.10       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54   1.10       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55   1.10       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56   1.10       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57   1.10       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58   1.10       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59   1.10       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60   1.10       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61   1.10       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62   1.10       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63   1.10       cgd  * SUCH DAMAGE.
     64   1.10       cgd  *
     65   1.28      fvdl  *	@(#)vfs_vnops.c	8.14 (Berkeley) 6/15/95
     66   1.10       cgd  */
     67   1.52     lukem 
     68   1.52     lukem #include <sys/cdefs.h>
     69  1.239  riastrad __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.239 2023/04/22 13:52:46 riastradh Exp $");
     70   1.26       mrg 
     71  1.122    dogcow #include "veriexec.h"
     72   1.10       cgd 
     73   1.10       cgd #include <sys/param.h>
     74   1.10       cgd #include <sys/systm.h>
     75   1.10       cgd #include <sys/kernel.h>
     76   1.10       cgd #include <sys/file.h>
     77   1.10       cgd #include <sys/stat.h>
     78   1.10       cgd #include <sys/buf.h>
     79   1.10       cgd #include <sys/proc.h>
     80   1.10       cgd #include <sys/mount.h>
     81   1.10       cgd #include <sys/namei.h>
     82  1.234   thorpej #include <sys/vnode_impl.h>
     83   1.10       cgd #include <sys/ioctl.h>
     84   1.10       cgd #include <sys/tty.h>
     85   1.21   mycroft #include <sys/poll.h>
     86  1.111      elad #include <sys/kauth.h>
     87  1.128      elad #include <sys/syslog.h>
     88  1.141   hannken #include <sys/fstrans.h>
     89  1.145        ad #include <sys/atomic.h>
     90  1.151     pooka #include <sys/filedesc.h>
     91  1.159    simonb #include <sys/wapbl.h>
     92  1.192       chs #include <sys/mman.h>
     93   1.11   mycroft 
     94   1.77   hannken #include <miscfs/specfs/specdev.h>
     95  1.170     pooka #include <miscfs/fifofs/fifo.h>
     96   1.77   hannken 
     97   1.25       mrg #include <uvm/uvm_extern.h>
     98  1.100      yamt #include <uvm/uvm_readahead.h>
     99  1.192       chs #include <uvm/uvm_device.h>
    100   1.25       mrg 
    101   1.28      fvdl #ifdef UNION
    102   1.65  jdolecek #include <fs/union/union.h>
    103   1.28      fvdl #endif
    104   1.64  jdolecek 
    105  1.192       chs #ifndef COMPAT_ZERODEV
    106  1.192       chs #define COMPAT_ZERODEV(dev)	(0)
    107  1.192       chs #endif
    108  1.192       chs 
    109  1.231  riastrad int (*vn_union_readdir_hook)(struct vnode **, struct file *, struct lwp *);
    110   1.66  jdolecek 
    111   1.58     blymn #include <sys/verified_exec.h>
    112   1.28      fvdl 
    113  1.155        ad static int vn_read(file_t *fp, off_t *offset, struct uio *uio,
    114  1.231  riastrad     kauth_cred_t cred, int flags);
    115  1.155        ad static int vn_write(file_t *fp, off_t *offset, struct uio *uio,
    116  1.231  riastrad     kauth_cred_t cred, int flags);
    117  1.155        ad static int vn_closefile(file_t *fp);
    118  1.155        ad static int vn_poll(file_t *fp, int events);
    119  1.155        ad static int vn_fcntl(file_t *fp, u_int com, void *data);
    120  1.155        ad static int vn_statfile(file_t *fp, struct stat *sb);
    121  1.155        ad static int vn_ioctl(file_t *fp, u_long com, void *data);
    122  1.192       chs static int vn_mmap(struct file *, off_t *, size_t, int, int *, int *,
    123  1.231  riastrad     struct uvm_object **, int *);
    124  1.222  riastrad static int vn_seek(struct file *, off_t, int, off_t *, int);
    125  1.239  riastrad static int vn_advlock(struct file *, void *, int, struct flock *, int);
    126   1.48  jdolecek 
    127   1.83  christos const struct fileops vnops = {
    128  1.197  christos 	.fo_name = "vn",
    129  1.164        ad 	.fo_read = vn_read,
    130  1.164        ad 	.fo_write = vn_write,
    131  1.164        ad 	.fo_ioctl = vn_ioctl,
    132  1.164        ad 	.fo_fcntl = vn_fcntl,
    133  1.164        ad 	.fo_poll = vn_poll,
    134  1.164        ad 	.fo_stat = vn_statfile,
    135  1.164        ad 	.fo_close = vn_closefile,
    136  1.164        ad 	.fo_kqfilter = vn_kqfilter,
    137  1.168       dsl 	.fo_restart = fnullop_restart,
    138  1.192       chs 	.fo_mmap = vn_mmap,
    139  1.222  riastrad 	.fo_seek = vn_seek,
    140  1.239  riastrad 	.fo_advlock = vn_advlock,
    141   1.48  jdolecek };
    142   1.10       cgd 
    143   1.10       cgd /*
    144   1.10       cgd  * Common code for vnode open operations.
    145   1.10       cgd  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
    146  1.216  dholland  *
    147  1.216  dholland  * at_dvp is the directory for openat(), if any.
    148  1.216  dholland  * pb is the path.
    149  1.216  dholland  * nmode is additional namei flags, restricted to TRYEMULROOT and NOCHROOT.
    150  1.216  dholland  * fmode is the open flags, converted from O_* to F*
    151  1.216  dholland  * cmode is the creation file permissions.
    152  1.216  dholland  *
    153  1.216  dholland  * XXX shouldn't cmode be mode_t?
    154  1.216  dholland  *
    155  1.236  riastrad  * On success produces either a locked vnode in *ret_vp, or NULL in
    156  1.236  riastrad  * *ret_vp and a file descriptor number in *ret_fd.
    157  1.216  dholland  *
    158  1.216  dholland  * The caller may pass NULL for ret_fd (and ret_domove), in which case
    159  1.216  dholland  * EOPNOTSUPP will be produced in the cases that would otherwise return
    160  1.216  dholland  * a file descriptor.
    161  1.216  dholland  *
    162  1.221  dholland  * Note that callers that want no-follow behavior should pass
    163  1.221  dholland  * O_NOFOLLOW in fmode. Neither FOLLOW nor NOFOLLOW in nmode is
    164  1.221  dholland  * honored.
    165   1.10       cgd  */
    166   1.20  christos int
    167  1.216  dholland vn_open(struct vnode *at_dvp, struct pathbuf *pb,
    168  1.216  dholland 	int nmode, int fmode, int cmode,
    169  1.216  dholland 	struct vnode **ret_vp, bool *ret_domove, int *ret_fd)
    170   1.10       cgd {
    171  1.216  dholland 	struct nameidata nd;
    172  1.220    martin 	struct vnode *vp = NULL;
    173  1.148     pooka 	struct lwp *l = curlwp;
    174  1.117        ad 	kauth_cred_t cred = l->l_cred;
    175   1.19   mycroft 	struct vattr va;
    176   1.10       cgd 	int error;
    177  1.180  dholland 	const char *pathstring;
    178   1.75   hannken 
    179  1.216  dholland 	KASSERT((nmode & (TRYEMULROOT | NOCHROOT)) == nmode);
    180  1.216  dholland 
    181  1.218  dholland 	KASSERT(ret_vp != NULL);
    182  1.218  dholland 	KASSERT((ret_domove == NULL) == (ret_fd == NULL));
    183  1.218  dholland 
    184  1.178       chs 	if ((fmode & (O_CREAT | O_DIRECTORY)) == (O_CREAT | O_DIRECTORY))
    185  1.178       chs 		return EINVAL;
    186  1.178       chs 
    187  1.221  dholland 	NDINIT(&nd, LOOKUP, nmode, pb);
    188  1.216  dholland 	if (at_dvp != NULL)
    189  1.216  dholland 		NDAT(&nd, at_dvp);
    190  1.216  dholland 
    191  1.216  dholland 	nd.ni_cnd.cn_flags &= TRYEMULROOT | NOCHROOT;
    192  1.138       dsl 
    193   1.10       cgd 	if (fmode & O_CREAT) {
    194  1.216  dholland 		nd.ni_cnd.cn_nameiop = CREATE;
    195  1.216  dholland 		nd.ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF;
    196   1.38    bouyer 		if ((fmode & O_EXCL) == 0 &&
    197   1.59  christos 		    ((fmode & O_NOFOLLOW) == 0))
    198  1.216  dholland 			nd.ni_cnd.cn_flags |= FOLLOW;
    199  1.215  dholland 		if ((fmode & O_EXCL) == 0)
    200  1.216  dholland 			nd.ni_cnd.cn_flags |= NONEXCLHACK;
    201  1.130      elad 	} else {
    202  1.216  dholland 		nd.ni_cnd.cn_nameiop = LOOKUP;
    203  1.216  dholland 		nd.ni_cnd.cn_flags |= LOCKLEAF;
    204  1.130      elad 		if ((fmode & O_NOFOLLOW) == 0)
    205  1.216  dholland 			nd.ni_cnd.cn_flags |= FOLLOW;
    206  1.130      elad 	}
    207  1.139  christos 
    208  1.216  dholland 	pathstring = pathbuf_stringcopy_get(nd.ni_pathbuf);
    209  1.180  dholland 	if (pathstring == NULL) {
    210  1.180  dholland 		return ENOMEM;
    211  1.180  dholland 	}
    212  1.139  christos 
    213  1.216  dholland 	/*
    214  1.216  dholland 	 * When this "interface" was exposed to do_open() it used
    215  1.216  dholland 	 * to initialize l_dupfd to -newfd-1 (thus passing in the
    216  1.216  dholland 	 * new file handle number to use)... but nothing in the
    217  1.216  dholland 	 * kernel uses that value. So just send 0.
    218  1.216  dholland 	 */
    219  1.216  dholland 	l->l_dupfd = 0;
    220  1.216  dholland 
    221  1.216  dholland 	error = namei(&nd);
    222  1.130      elad 	if (error)
    223  1.139  christos 		goto out;
    224   1.91      elad 
    225  1.216  dholland 	vp = nd.ni_vp;
    226   1.91      elad 
    227  1.130      elad #if NVERIEXEC > 0
    228  1.216  dholland 	error = veriexec_openchk(l, nd.ni_vp, pathstring, fmode);
    229  1.190      maxv 	if (error) {
    230  1.190      maxv 		/* We have to release the locks ourselves */
    231  1.215  dholland 		/*
    232  1.215  dholland 		 * 20210604 dholland passing NONEXCLHACK means we can
    233  1.215  dholland 		 * get ni_dvp == NULL back if ni_vp exists, and we should
    234  1.215  dholland 		 * treat that like the non-O_CREAT case.
    235  1.215  dholland 		 */
    236  1.216  dholland 		if ((fmode & O_CREAT) != 0 && nd.ni_dvp != NULL) {
    237  1.190      maxv 			if (vp == NULL) {
    238  1.216  dholland 				vput(nd.ni_dvp);
    239  1.190      maxv 			} else {
    240  1.216  dholland 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    241  1.216  dholland 				if (nd.ni_dvp == nd.ni_vp)
    242  1.216  dholland 					vrele(nd.ni_dvp);
    243  1.190      maxv 				else
    244  1.216  dholland 					vput(nd.ni_dvp);
    245  1.216  dholland 				nd.ni_dvp = NULL;
    246  1.190      maxv 				vput(vp);
    247  1.237  riastrad 				vp = NULL;
    248  1.190      maxv 			}
    249  1.190      maxv 		} else {
    250  1.190      maxv 			vput(vp);
    251  1.237  riastrad 			vp = NULL;
    252  1.190      maxv 		}
    253  1.190      maxv 		goto out;
    254  1.190      maxv 	}
    255  1.115      elad #endif /* NVERIEXEC > 0 */
    256   1.91      elad 
    257  1.215  dholland 	/*
    258  1.215  dholland 	 * 20210604 dholland ditto
    259  1.215  dholland 	 */
    260  1.216  dholland 	if ((fmode & O_CREAT) != 0 && nd.ni_dvp != NULL) {
    261  1.216  dholland 		if (nd.ni_vp == NULL) {
    262  1.169     pooka 			vattr_null(&va);
    263   1.19   mycroft 			va.va_type = VREG;
    264   1.19   mycroft 			va.va_mode = cmode;
    265   1.28      fvdl 			if (fmode & O_EXCL)
    266   1.28      fvdl 				 va.va_vaflags |= VA_EXCLUSIVE;
    267  1.216  dholland 			error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
    268  1.216  dholland 					   &nd.ni_cnd, &va);
    269  1.194   hannken 			if (error) {
    270  1.216  dholland 				vput(nd.ni_dvp);
    271  1.139  christos 				goto out;
    272  1.194   hannken 			}
    273   1.10       cgd 			fmode &= ~O_TRUNC;
    274  1.216  dholland 			vp = nd.ni_vp;
    275  1.188   hannken 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    276  1.216  dholland 			vput(nd.ni_dvp);
    277   1.10       cgd 		} else {
    278  1.216  dholland 			VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    279  1.216  dholland 			if (nd.ni_dvp == nd.ni_vp)
    280  1.216  dholland 				vrele(nd.ni_dvp);
    281   1.10       cgd 			else
    282  1.216  dholland 				vput(nd.ni_dvp);
    283  1.216  dholland 			nd.ni_dvp = NULL;
    284  1.216  dholland 			vp = nd.ni_vp;
    285   1.10       cgd 			if (fmode & O_EXCL) {
    286   1.10       cgd 				error = EEXIST;
    287   1.38    bouyer 				goto bad;
    288   1.38    bouyer 			}
    289   1.10       cgd 			fmode &= ~O_CREAT;
    290   1.10       cgd 		}
    291  1.215  dholland 	} else if ((fmode & O_CREAT) != 0) {
    292  1.215  dholland 		/*
    293  1.215  dholland 		 * 20210606 dholland passing NONEXCLHACK means this
    294  1.215  dholland 		 * case exists; it is the same as the following one
    295  1.215  dholland 		 * but also needs to do things in the second (exists)
    296  1.215  dholland 		 * half of the following block. (Besides handle
    297  1.215  dholland 		 * ni_dvp, anyway.)
    298  1.215  dholland 		 */
    299  1.216  dholland 		vp = nd.ni_vp;
    300  1.215  dholland 		KASSERT((fmode & O_EXCL) == 0);
    301  1.215  dholland 		fmode &= ~O_CREAT;
    302   1.10       cgd 	} else {
    303  1.216  dholland 		vp = nd.ni_vp;
    304   1.10       cgd 	}
    305   1.10       cgd 	if (vp->v_type == VSOCK) {
    306   1.10       cgd 		error = EOPNOTSUPP;
    307   1.74        cb 		goto bad;
    308   1.74        cb 	}
    309  1.216  dholland 	if (nd.ni_vp->v_type == VLNK) {
    310   1.74        cb 		error = EFTYPE;
    311   1.10       cgd 		goto bad;
    312   1.10       cgd 	}
    313   1.58     blymn 
    314   1.10       cgd 	if ((fmode & O_CREAT) == 0) {
    315  1.146      yamt 		error = vn_openchk(vp, cred, fmode);
    316  1.146      yamt 		if (error != 0)
    317  1.146      yamt 			goto bad;
    318   1.10       cgd 	}
    319   1.87     blymn 
    320   1.10       cgd 	if (fmode & O_TRUNC) {
    321  1.169     pooka 		vattr_null(&va);
    322   1.19   mycroft 		va.va_size = 0;
    323  1.144     pooka 		error = VOP_SETATTR(vp, &va, cred);
    324   1.75   hannken 		if (error != 0)
    325   1.10       cgd 			goto bad;
    326   1.10       cgd 	}
    327  1.144     pooka 	if ((error = VOP_OPEN(vp, fmode, cred)) != 0)
    328   1.10       cgd 		goto bad;
    329  1.143        ad 	if (fmode & FWRITE) {
    330  1.181     rmind 		mutex_enter(vp->v_interlock);
    331   1.10       cgd 		vp->v_writecount++;
    332  1.181     rmind 		mutex_exit(vp->v_interlock);
    333  1.143        ad 	}
    334   1.45       chs 
    335   1.10       cgd bad:
    336  1.237  riastrad 	if (error) {
    337  1.127      elad 		vput(vp);
    338  1.237  riastrad 		vp = NULL;
    339  1.237  riastrad 	}
    340  1.139  christos out:
    341  1.216  dholland 	pathbuf_stringcopy_put(nd.ni_pathbuf, pathstring);
    342  1.216  dholland 
    343  1.217  christos 	switch (error) {
    344  1.217  christos 	case EDUPFD:
    345  1.217  christos 	case EMOVEFD:
    346  1.217  christos 		/* if the caller isn't prepared to handle fds, fail for them */
    347  1.218  dholland 		if (ret_fd == NULL) {
    348  1.218  dholland 			error = EOPNOTSUPP;
    349  1.218  dholland 			break;
    350  1.218  dholland 		}
    351  1.216  dholland 		*ret_vp = NULL;
    352  1.217  christos 		*ret_domove = error == EMOVEFD;
    353  1.216  dholland 		*ret_fd = l->l_dupfd;
    354  1.219  christos 		error = 0;
    355  1.217  christos 		break;
    356  1.217  christos 	case 0:
    357  1.237  riastrad 		KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    358  1.216  dholland 		*ret_vp = vp;
    359  1.217  christos 		break;
    360  1.216  dholland 	}
    361  1.216  dholland 	l->l_dupfd = 0;
    362  1.216  dholland 	return error;
    363   1.10       cgd }
    364   1.10       cgd 
    365   1.10       cgd /*
    366   1.10       cgd  * Check for write permissions on the specified vnode.
    367   1.28      fvdl  * Prototype text segments cannot be written.
    368   1.10       cgd  */
    369   1.20  christos int
    370   1.89   thorpej vn_writechk(struct vnode *vp)
    371   1.10       cgd {
    372   1.10       cgd 
    373   1.10       cgd 	/*
    374   1.45       chs 	 * If the vnode is in use as a process's text,
    375   1.45       chs 	 * we can't allow writing.
    376   1.10       cgd 	 */
    377  1.143        ad 	if (vp->v_iflag & VI_TEXT)
    378  1.231  riastrad 		return ETXTBSY;
    379  1.231  riastrad 	return 0;
    380   1.42       chs }
    381   1.42       chs 
    382  1.146      yamt int
    383  1.146      yamt vn_openchk(struct vnode *vp, kauth_cred_t cred, int fflags)
    384  1.146      yamt {
    385  1.146      yamt 	int permbits = 0;
    386  1.146      yamt 	int error;
    387  1.146      yamt 
    388  1.200   hannken 	if (vp->v_type == VNON || vp->v_type == VBAD)
    389  1.200   hannken 		return ENXIO;
    390  1.200   hannken 
    391  1.178       chs 	if ((fflags & O_DIRECTORY) != 0 && vp->v_type != VDIR)
    392  1.178       chs 		return ENOTDIR;
    393  1.178       chs 
    394  1.196  christos 	if ((fflags & O_REGULAR) != 0 && vp->v_type != VREG)
    395  1.196  christos 		return EFTYPE;
    396  1.196  christos 
    397  1.146      yamt 	if ((fflags & FREAD) != 0) {
    398  1.146      yamt 		permbits = VREAD;
    399  1.146      yamt 	}
    400  1.201  christos 	if ((fflags & FEXEC) != 0) {
    401  1.201  christos 		permbits |= VEXEC;
    402  1.201  christos 	}
    403  1.146      yamt 	if ((fflags & (FWRITE | O_TRUNC)) != 0) {
    404  1.146      yamt 		permbits |= VWRITE;
    405  1.146      yamt 		if (vp->v_type == VDIR) {
    406  1.146      yamt 			error = EISDIR;
    407  1.146      yamt 			goto bad;
    408  1.146      yamt 		}
    409  1.146      yamt 		error = vn_writechk(vp);
    410  1.146      yamt 		if (error != 0)
    411  1.146      yamt 			goto bad;
    412  1.146      yamt 	}
    413  1.146      yamt 	error = VOP_ACCESS(vp, permbits, cred);
    414  1.146      yamt bad:
    415  1.146      yamt 	return error;
    416  1.146      yamt }
    417  1.146      yamt 
    418   1.42       chs /*
    419   1.51   thorpej  * Mark a vnode as having executable mappings.
    420   1.42       chs  */
    421   1.42       chs void
    422   1.89   thorpej vn_markexec(struct vnode *vp)
    423   1.42       chs {
    424  1.143        ad 
    425  1.158        ad 	if ((vp->v_iflag & VI_EXECMAP) != 0) {
    426  1.158        ad 		/* Safe unlocked, as long as caller holds a reference. */
    427  1.158        ad 		return;
    428  1.158        ad 	}
    429  1.143        ad 
    430  1.207        ad 	rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
    431  1.181     rmind 	mutex_enter(vp->v_interlock);
    432  1.143        ad 	if ((vp->v_iflag & VI_EXECMAP) == 0) {
    433  1.204        ad 		cpu_count(CPU_COUNT_EXECPAGES, vp->v_uobj.uo_npages);
    434  1.158        ad 		vp->v_iflag |= VI_EXECMAP;
    435   1.46       chs 	}
    436  1.181     rmind 	mutex_exit(vp->v_interlock);
    437  1.207        ad 	rw_exit(vp->v_uobj.vmobjlock);
    438   1.55       chs }
    439   1.55       chs 
    440   1.55       chs /*
    441   1.55       chs  * Mark a vnode as being the text of a process.
    442   1.55       chs  * Fail if the vnode is currently writable.
    443   1.55       chs  */
    444   1.55       chs int
    445   1.89   thorpej vn_marktext(struct vnode *vp)
    446   1.55       chs {
    447   1.55       chs 
    448  1.157        ad 	if ((vp->v_iflag & (VI_TEXT|VI_EXECMAP)) == (VI_TEXT|VI_EXECMAP)) {
    449  1.157        ad 		/* Safe unlocked, as long as caller holds a reference. */
    450  1.231  riastrad 		return 0;
    451  1.157        ad 	}
    452  1.157        ad 
    453  1.207        ad 	rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
    454  1.181     rmind 	mutex_enter(vp->v_interlock);
    455   1.55       chs 	if (vp->v_writecount != 0) {
    456  1.143        ad 		KASSERT((vp->v_iflag & VI_TEXT) == 0);
    457  1.181     rmind 		mutex_exit(vp->v_interlock);
    458  1.207        ad 		rw_exit(vp->v_uobj.vmobjlock);
    459  1.231  riastrad 		return ETXTBSY;
    460   1.55       chs 	}
    461  1.158        ad 	if ((vp->v_iflag & VI_EXECMAP) == 0) {
    462  1.204        ad 		cpu_count(CPU_COUNT_EXECPAGES, vp->v_uobj.uo_npages);
    463  1.158        ad 	}
    464  1.158        ad 	vp->v_iflag |= (VI_TEXT | VI_EXECMAP);
    465  1.181     rmind 	mutex_exit(vp->v_interlock);
    466  1.207        ad 	rw_exit(vp->v_uobj.vmobjlock);
    467  1.231  riastrad 	return 0;
    468   1.10       cgd }
    469   1.10       cgd 
    470   1.10       cgd /*
    471   1.10       cgd  * Vnode close call
    472   1.32  wrstuden  *
    473   1.32  wrstuden  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
    474   1.10       cgd  */
    475   1.20  christos int
    476  1.155        ad vn_close(struct vnode *vp, int flags, kauth_cred_t cred)
    477   1.10       cgd {
    478   1.10       cgd 	int error;
    479   1.10       cgd 
    480  1.195   hannken 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    481  1.158        ad 	if (flags & FWRITE) {
    482  1.181     rmind 		mutex_enter(vp->v_interlock);
    483  1.182      yamt 		KASSERT(vp->v_writecount > 0);
    484   1.10       cgd 		vp->v_writecount--;
    485  1.181     rmind 		mutex_exit(vp->v_interlock);
    486  1.158        ad 	}
    487  1.144     pooka 	error = VOP_CLOSE(vp, flags, cred);
    488   1.32  wrstuden 	vput(vp);
    489  1.231  riastrad 	return error;
    490   1.10       cgd }
    491   1.10       cgd 
    492  1.171     pooka static int
    493  1.171     pooka enforce_rlimit_fsize(struct vnode *vp, struct uio *uio, int ioflag)
    494  1.171     pooka {
    495  1.171     pooka 	struct lwp *l = curlwp;
    496  1.171     pooka 	off_t testoff;
    497  1.171     pooka 
    498  1.171     pooka 	if (uio->uio_rw != UIO_WRITE || vp->v_type != VREG)
    499  1.171     pooka 		return 0;
    500  1.171     pooka 
    501  1.171     pooka 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    502  1.171     pooka 	if (ioflag & IO_APPEND)
    503  1.171     pooka 		testoff = vp->v_size;
    504  1.171     pooka 	else
    505  1.171     pooka 		testoff = uio->uio_offset;
    506  1.171     pooka 
    507  1.171     pooka 	if (testoff + uio->uio_resid >
    508  1.171     pooka 	    l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    509  1.212        ad 		mutex_enter(&proc_lock);
    510  1.171     pooka 		psignal(l->l_proc, SIGXFSZ);
    511  1.212        ad 		mutex_exit(&proc_lock);
    512  1.171     pooka 		return EFBIG;
    513  1.171     pooka 	}
    514  1.171     pooka 
    515  1.171     pooka 	return 0;
    516  1.171     pooka }
    517  1.171     pooka 
    518   1.10       cgd /*
    519   1.10       cgd  * Package up an I/O request on a vnode into a uio and do it.
    520   1.10       cgd  */
    521   1.20  christos int
    522  1.134  christos vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
    523  1.111      elad     enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
    524  1.101  christos     struct lwp *l)
    525   1.10       cgd {
    526   1.10       cgd 	struct uio auio;
    527   1.10       cgd 	struct iovec aiov;
    528   1.10       cgd 	int error;
    529   1.10       cgd 
    530   1.50       chs 	if ((ioflg & IO_NODELOCKED) == 0) {
    531   1.50       chs 		if (rw == UIO_READ) {
    532   1.50       chs 			vn_lock(vp, LK_SHARED | LK_RETRY);
    533   1.75   hannken 		} else /* UIO_WRITE */ {
    534   1.50       chs 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    535   1.50       chs 		}
    536   1.50       chs 	}
    537   1.10       cgd 	auio.uio_iov = &aiov;
    538   1.10       cgd 	auio.uio_iovcnt = 1;
    539   1.10       cgd 	aiov.iov_base = base;
    540   1.10       cgd 	aiov.iov_len = len;
    541   1.10       cgd 	auio.uio_resid = len;
    542   1.10       cgd 	auio.uio_offset = offset;
    543   1.10       cgd 	auio.uio_rw = rw;
    544  1.106      yamt 	if (segflg == UIO_SYSSPACE) {
    545  1.106      yamt 		UIO_SETUP_SYSSPACE(&auio);
    546  1.106      yamt 	} else {
    547  1.106      yamt 		auio.uio_vmspace = l->l_proc->p_vmspace;
    548  1.106      yamt 	}
    549  1.171     pooka 
    550  1.171     pooka 	if ((error = enforce_rlimit_fsize(vp, &auio, ioflg)) != 0)
    551  1.171     pooka 		goto out;
    552  1.171     pooka 
    553   1.11   mycroft 	if (rw == UIO_READ) {
    554   1.10       cgd 		error = VOP_READ(vp, &auio, ioflg, cred);
    555   1.11   mycroft 	} else {
    556   1.10       cgd 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    557   1.11   mycroft 	}
    558  1.171     pooka 
    559   1.10       cgd 	if (aresid)
    560   1.10       cgd 		*aresid = auio.uio_resid;
    561   1.10       cgd 	else
    562   1.10       cgd 		if (auio.uio_resid && error == 0)
    563   1.10       cgd 			error = EIO;
    564  1.171     pooka 
    565  1.171     pooka  out:
    566   1.75   hannken 	if ((ioflg & IO_NODELOCKED) == 0) {
    567  1.174   hannken 		VOP_UNLOCK(vp);
    568   1.75   hannken 	}
    569  1.231  riastrad 	return error;
    570   1.23      fvdl }
    571   1.23      fvdl 
    572   1.23      fvdl int
    573  1.155        ad vn_readdir(file_t *fp, char *bf, int segflg, u_int count, int *done,
    574  1.101  christos     struct lwp *l, off_t **cookies, int *ncookies)
    575   1.23      fvdl {
    576  1.191      matt 	struct vnode *vp = fp->f_vnode;
    577   1.23      fvdl 	struct iovec aiov;
    578   1.23      fvdl 	struct uio auio;
    579   1.23      fvdl 	int error, eofflag;
    580   1.23      fvdl 
    581  1.112    simonb 	/* Limit the size on any kernel buffers used by VOP_READDIR */
    582  1.198  riastrad 	count = uimin(MAXBSIZE, count);
    583  1.112    simonb 
    584   1.23      fvdl unionread:
    585   1.23      fvdl 	if (vp->v_type != VDIR)
    586  1.231  riastrad 		return EINVAL;
    587   1.88  christos 	aiov.iov_base = bf;
    588   1.23      fvdl 	aiov.iov_len = count;
    589   1.23      fvdl 	auio.uio_iov = &aiov;
    590   1.23      fvdl 	auio.uio_iovcnt = 1;
    591   1.23      fvdl 	auio.uio_rw = UIO_READ;
    592  1.106      yamt 	if (segflg == UIO_SYSSPACE) {
    593  1.106      yamt 		UIO_SETUP_SYSSPACE(&auio);
    594  1.106      yamt 	} else {
    595  1.106      yamt 		KASSERT(l == curlwp);
    596  1.106      yamt 		auio.uio_vmspace = l->l_proc->p_vmspace;
    597  1.106      yamt 	}
    598   1.23      fvdl 	auio.uio_resid = count;
    599   1.50       chs 	vn_lock(vp, LK_SHARED | LK_RETRY);
    600  1.238  riastrad 	mutex_enter(&fp->f_lock);
    601   1.23      fvdl 	auio.uio_offset = fp->f_offset;
    602  1.238  riastrad 	mutex_exit(&fp->f_lock);
    603   1.28      fvdl 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
    604  1.238  riastrad 	    ncookies);
    605  1.166      yamt 	mutex_enter(&fp->f_lock);
    606   1.23      fvdl 	fp->f_offset = auio.uio_offset;
    607  1.166      yamt 	mutex_exit(&fp->f_lock);
    608  1.174   hannken 	VOP_UNLOCK(vp);
    609   1.23      fvdl 	if (error)
    610  1.232  riastrad 		return error;
    611   1.23      fvdl 
    612   1.66  jdolecek 	if (count == auio.uio_resid && vn_union_readdir_hook) {
    613   1.66  jdolecek 		struct vnode *ovp = vp;
    614   1.23      fvdl 
    615  1.101  christos 		error = (*vn_union_readdir_hook)(&vp, fp, l);
    616   1.66  jdolecek 		if (error)
    617  1.231  riastrad 			return error;
    618   1.66  jdolecek 		if (vp != ovp)
    619   1.23      fvdl 			goto unionread;
    620   1.23      fvdl 	}
    621   1.23      fvdl 
    622  1.143        ad 	if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) &&
    623   1.23      fvdl 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    624   1.23      fvdl 		struct vnode *tvp = vp;
    625   1.23      fvdl 		vp = vp->v_mount->mnt_vnodecovered;
    626  1.169     pooka 		vref(vp);
    627  1.166      yamt 		mutex_enter(&fp->f_lock);
    628  1.191      matt 		fp->f_vnode = vp;
    629   1.23      fvdl 		fp->f_offset = 0;
    630  1.166      yamt 		mutex_exit(&fp->f_lock);
    631   1.23      fvdl 		vrele(tvp);
    632   1.23      fvdl 		goto unionread;
    633   1.23      fvdl 	}
    634   1.23      fvdl 	*done = count - auio.uio_resid;
    635   1.23      fvdl 	return error;
    636   1.10       cgd }
    637   1.10       cgd 
    638   1.10       cgd /*
    639   1.10       cgd  * File table vnode read routine.
    640   1.10       cgd  */
    641   1.56  gmcgarry static int
    642  1.155        ad vn_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    643   1.89   thorpej     int flags)
    644   1.10       cgd {
    645  1.191      matt 	struct vnode *vp = fp->f_vnode;
    646  1.185  dholland 	int error, ioflag, fflag;
    647  1.185  dholland 	size_t count;
    648   1.10       cgd 
    649  1.100      yamt 	ioflag = IO_ADV_ENCODE(fp->f_advice);
    650  1.155        ad 	fflag = fp->f_flag;
    651  1.155        ad 	if (fflag & FNONBLOCK)
    652   1.31    kleink 		ioflag |= IO_NDELAY;
    653  1.155        ad 	if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    654   1.31    kleink 		ioflag |= IO_SYNC;
    655  1.155        ad 	if (fflag & FALTIO)
    656   1.37  wrstuden 		ioflag |= IO_ALTSEMANTICS;
    657  1.155        ad 	if (fflag & FDIRECT)
    658  1.125       chs 		ioflag |= IO_DIRECT;
    659  1.235  riastrad 	if (offset == &fp->f_offset && (flags & FOF_UPDATE_OFFSET) != 0)
    660  1.235  riastrad 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    661  1.235  riastrad 	else
    662  1.235  riastrad 		vn_lock(vp, LK_SHARED | LK_RETRY);
    663  1.238  riastrad 	if (__predict_false(vp->v_type == VDIR) &&
    664  1.238  riastrad 	    offset == &fp->f_offset && (flags & FOF_UPDATE_OFFSET) == 0)
    665  1.238  riastrad 		mutex_enter(&fp->f_lock);
    666   1.29   thorpej 	uio->uio_offset = *offset;
    667  1.238  riastrad 	if (__predict_false(vp->v_type == VDIR) &&
    668  1.238  riastrad 	    offset == &fp->f_offset && (flags & FOF_UPDATE_OFFSET) == 0)
    669  1.238  riastrad 		mutex_enter(&fp->f_lock);
    670   1.10       cgd 	count = uio->uio_resid;
    671   1.31    kleink 	error = VOP_READ(vp, uio, ioflag, cred);
    672   1.29   thorpej 	if (flags & FOF_UPDATE_OFFSET)
    673   1.29   thorpej 		*offset += count - uio->uio_resid;
    674  1.174   hannken 	VOP_UNLOCK(vp);
    675  1.231  riastrad 	return error;
    676   1.10       cgd }
    677   1.10       cgd 
    678   1.10       cgd /*
    679   1.10       cgd  * File table vnode write routine.
    680   1.10       cgd  */
    681   1.56  gmcgarry static int
    682  1.155        ad vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    683   1.89   thorpej     int flags)
    684   1.10       cgd {
    685  1.191      matt 	struct vnode *vp = fp->f_vnode;
    686  1.185  dholland 	int error, ioflag, fflag;
    687  1.185  dholland 	size_t count;
    688   1.10       cgd 
    689  1.151     pooka 	ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT;
    690  1.155        ad 	fflag = fp->f_flag;
    691  1.155        ad 	if (vp->v_type == VREG && (fflag & O_APPEND))
    692   1.10       cgd 		ioflag |= IO_APPEND;
    693  1.155        ad 	if (fflag & FNONBLOCK)
    694   1.10       cgd 		ioflag |= IO_NDELAY;
    695  1.155        ad 	if (fflag & FFSYNC ||
    696   1.24   thorpej 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    697   1.24   thorpej 		ioflag |= IO_SYNC;
    698  1.155        ad 	else if (fflag & FDSYNC)
    699   1.31    kleink 		ioflag |= IO_DSYNC;
    700  1.155        ad 	if (fflag & FALTIO)
    701   1.37  wrstuden 		ioflag |= IO_ALTSEMANTICS;
    702  1.155        ad 	if (fflag & FDIRECT)
    703  1.125       chs 		ioflag |= IO_DIRECT;
    704   1.28      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    705   1.29   thorpej 	uio->uio_offset = *offset;
    706   1.10       cgd 	count = uio->uio_resid;
    707  1.171     pooka 
    708  1.171     pooka 	if ((error = enforce_rlimit_fsize(vp, uio, ioflag)) != 0)
    709  1.171     pooka 		goto out;
    710  1.171     pooka 
    711   1.10       cgd 	error = VOP_WRITE(vp, uio, ioflag, cred);
    712  1.171     pooka 
    713   1.29   thorpej 	if (flags & FOF_UPDATE_OFFSET) {
    714  1.160  christos 		if (ioflag & IO_APPEND) {
    715  1.160  christos 			/*
    716  1.160  christos 			 * SUSv3 describes behaviour for count = 0 as following:
    717  1.160  christos 			 * "Before any action ... is taken, and if nbyte is zero
    718  1.160  christos 			 * and the file is a regular file, the write() function
    719  1.160  christos 			 * ... in the absence of errors ... shall return zero
    720  1.160  christos 			 * and have no other results."
    721  1.160  christos 			 */
    722  1.160  christos 			if (count)
    723  1.160  christos 				*offset = uio->uio_offset;
    724  1.160  christos 		} else
    725   1.29   thorpej 			*offset += count - uio->uio_resid;
    726   1.29   thorpej 	}
    727  1.171     pooka 
    728  1.171     pooka  out:
    729  1.174   hannken 	VOP_UNLOCK(vp);
    730  1.231  riastrad 	return error;
    731   1.10       cgd }
    732   1.10       cgd 
    733   1.10       cgd /*
    734   1.10       cgd  * File table vnode stat routine.
    735   1.10       cgd  */
    736   1.48  jdolecek static int
    737  1.155        ad vn_statfile(file_t *fp, struct stat *sb)
    738   1.48  jdolecek {
    739  1.191      matt 	struct vnode *vp = fp->f_vnode;
    740  1.165  christos 	int error;
    741   1.48  jdolecek 
    742  1.165  christos 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    743  1.165  christos 	error = vn_stat(vp, sb);
    744  1.174   hannken 	VOP_UNLOCK(vp);
    745  1.165  christos 	return error;
    746   1.48  jdolecek }
    747   1.48  jdolecek 
    748   1.20  christos int
    749  1.155        ad vn_stat(struct vnode *vp, struct stat *sb)
    750   1.10       cgd {
    751   1.19   mycroft 	struct vattr va;
    752   1.10       cgd 	int error;
    753   1.35  wrstuden 	mode_t mode;
    754   1.10       cgd 
    755  1.175     pooka 	memset(&va, 0, sizeof(va));
    756  1.155        ad 	error = VOP_GETATTR(vp, &va, kauth_cred_get());
    757   1.10       cgd 	if (error)
    758  1.231  riastrad 		return error;
    759   1.10       cgd 	/*
    760   1.10       cgd 	 * Copy from vattr table
    761   1.10       cgd 	 */
    762  1.179     pooka 	memset(sb, 0, sizeof(*sb));
    763   1.19   mycroft 	sb->st_dev = va.va_fsid;
    764   1.19   mycroft 	sb->st_ino = va.va_fileid;
    765   1.19   mycroft 	mode = va.va_mode;
    766   1.10       cgd 	switch (vp->v_type) {
    767   1.10       cgd 	case VREG:
    768   1.10       cgd 		mode |= S_IFREG;
    769   1.10       cgd 		break;
    770   1.10       cgd 	case VDIR:
    771   1.10       cgd 		mode |= S_IFDIR;
    772   1.10       cgd 		break;
    773   1.10       cgd 	case VBLK:
    774   1.10       cgd 		mode |= S_IFBLK;
    775   1.10       cgd 		break;
    776   1.10       cgd 	case VCHR:
    777   1.10       cgd 		mode |= S_IFCHR;
    778   1.10       cgd 		break;
    779   1.10       cgd 	case VLNK:
    780   1.10       cgd 		mode |= S_IFLNK;
    781   1.10       cgd 		break;
    782   1.10       cgd 	case VSOCK:
    783   1.10       cgd 		mode |= S_IFSOCK;
    784   1.10       cgd 		break;
    785   1.10       cgd 	case VFIFO:
    786   1.10       cgd 		mode |= S_IFIFO;
    787   1.10       cgd 		break;
    788   1.10       cgd 	default:
    789  1.231  riastrad 		return EBADF;
    790  1.193   msaitoh 	}
    791   1.10       cgd 	sb->st_mode = mode;
    792   1.19   mycroft 	sb->st_nlink = va.va_nlink;
    793   1.19   mycroft 	sb->st_uid = va.va_uid;
    794   1.19   mycroft 	sb->st_gid = va.va_gid;
    795   1.19   mycroft 	sb->st_rdev = va.va_rdev;
    796   1.19   mycroft 	sb->st_size = va.va_size;
    797   1.19   mycroft 	sb->st_atimespec = va.va_atime;
    798   1.19   mycroft 	sb->st_mtimespec = va.va_mtime;
    799   1.19   mycroft 	sb->st_ctimespec = va.va_ctime;
    800   1.69      fvdl 	sb->st_birthtimespec = va.va_birthtime;
    801   1.19   mycroft 	sb->st_blksize = va.va_blocksize;
    802   1.19   mycroft 	sb->st_flags = va.va_flags;
    803   1.22   mycroft 	sb->st_gen = 0;
    804   1.19   mycroft 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    805  1.231  riastrad 	return 0;
    806   1.37  wrstuden }
    807   1.37  wrstuden 
    808   1.37  wrstuden /*
    809   1.37  wrstuden  * File table vnode fcntl routine.
    810   1.37  wrstuden  */
    811   1.56  gmcgarry static int
    812  1.155        ad vn_fcntl(file_t *fp, u_int com, void *data)
    813   1.37  wrstuden {
    814  1.191      matt 	struct vnode *vp = fp->f_vnode;
    815   1.37  wrstuden 	int error;
    816   1.37  wrstuden 
    817  1.155        ad 	error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get());
    818  1.231  riastrad 	return error;
    819   1.10       cgd }
    820   1.10       cgd 
    821   1.10       cgd /*
    822   1.10       cgd  * File table vnode ioctl routine.
    823   1.10       cgd  */
    824   1.56  gmcgarry static int
    825  1.155        ad vn_ioctl(file_t *fp, u_long com, void *data)
    826   1.10       cgd {
    827  1.191      matt 	struct vnode *vp = fp->f_vnode, *ovp;
    828   1.10       cgd 	struct vattr vattr;
    829   1.10       cgd 	int error;
    830   1.10       cgd 
    831   1.10       cgd 	switch (vp->v_type) {
    832   1.10       cgd 
    833   1.10       cgd 	case VREG:
    834   1.10       cgd 	case VDIR:
    835   1.10       cgd 		if (com == FIONREAD) {
    836  1.183   hannken 			vn_lock(vp, LK_SHARED | LK_RETRY);
    837  1.183   hannken 			error = VOP_GETATTR(vp, &vattr, kauth_cred_get());
    838  1.238  riastrad 			if (error == 0) {
    839  1.238  riastrad 				if (vp->v_type == VDIR)
    840  1.238  riastrad 					mutex_enter(&fp->f_lock);
    841  1.223  riastrad 				*(int *)data = vattr.va_size - fp->f_offset;
    842  1.238  riastrad 				if (vp->v_type == VDIR)
    843  1.238  riastrad 					mutex_exit(&fp->f_lock);
    844  1.238  riastrad 			}
    845  1.183   hannken 			VOP_UNLOCK(vp);
    846   1.20  christos 			if (error)
    847  1.231  riastrad 				return error;
    848  1.231  riastrad 			return 0;
    849   1.60    atatat 		}
    850   1.82  christos 		if ((com == FIONWRITE) || (com == FIONSPACE)) {
    851   1.81  wrstuden 			/*
    852   1.81  wrstuden 			 * Files don't have send queues, so there never
    853   1.81  wrstuden 			 * are any bytes in them, nor is there any
    854   1.81  wrstuden 			 * open space in them.
    855   1.81  wrstuden 			 */
    856   1.81  wrstuden 			*(int *)data = 0;
    857  1.231  riastrad 			return 0;
    858   1.81  wrstuden 		}
    859   1.60    atatat 		if (com == FIOGETBMAP) {
    860   1.60    atatat 			daddr_t *block;
    861   1.60    atatat 
    862   1.62    atatat 			if (*(daddr_t *)data < 0)
    863  1.231  riastrad 				return EINVAL;
    864   1.60    atatat 			block = (daddr_t *)data;
    865  1.214       chs 			vn_lock(vp, LK_SHARED | LK_RETRY);
    866  1.214       chs 			error = VOP_BMAP(vp, *block, NULL, block, NULL);
    867  1.214       chs 			VOP_UNLOCK(vp);
    868  1.214       chs 			return error;
    869   1.61      fvdl 		}
    870   1.61      fvdl 		if (com == OFIOGETBMAP) {
    871   1.61      fvdl 			daddr_t ibn, obn;
    872   1.61      fvdl 
    873   1.62    atatat 			if (*(int32_t *)data < 0)
    874  1.231  riastrad 				return EINVAL;
    875   1.61      fvdl 			ibn = (daddr_t)*(int32_t *)data;
    876  1.214       chs 			vn_lock(vp, LK_SHARED | LK_RETRY);
    877   1.61      fvdl 			error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
    878  1.214       chs 			VOP_UNLOCK(vp);
    879   1.61      fvdl 			*(int32_t *)data = (int32_t)obn;
    880   1.61      fvdl 			return error;
    881   1.10       cgd 		}
    882   1.10       cgd 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    883  1.231  riastrad 			return 0;			/* XXX */
    884  1.199       mrg 		/* FALLTHROUGH */
    885   1.10       cgd 	case VFIFO:
    886   1.10       cgd 	case VCHR:
    887   1.10       cgd 	case VBLK:
    888  1.155        ad 		error = VOP_IOCTL(vp, com, data, fp->f_flag,
    889  1.155        ad 		    kauth_cred_get());
    890   1.10       cgd 		if (error == 0 && com == TIOCSCTTY) {
    891  1.169     pooka 			vref(vp);
    892  1.212        ad 			mutex_enter(&proc_lock);
    893  1.155        ad 			ovp = curproc->p_session->s_ttyvp;
    894  1.155        ad 			curproc->p_session->s_ttyvp = vp;
    895  1.212        ad 			mutex_exit(&proc_lock);
    896  1.132        ad 			if (ovp != NULL)
    897  1.132        ad 				vrele(ovp);
    898   1.10       cgd 		}
    899  1.231  riastrad 		return error;
    900   1.63  perseant 
    901   1.63  perseant 	default:
    902  1.231  riastrad 		return EPASSTHROUGH;
    903   1.10       cgd 	}
    904   1.10       cgd }
    905   1.10       cgd 
    906   1.10       cgd /*
    907   1.21   mycroft  * File table vnode poll routine.
    908   1.10       cgd  */
    909   1.56  gmcgarry static int
    910  1.155        ad vn_poll(file_t *fp, int events)
    911   1.10       cgd {
    912   1.10       cgd 
    913  1.231  riastrad 	return VOP_POLL(fp->f_vnode, events);
    914   1.57  jdolecek }
    915   1.57  jdolecek 
    916   1.57  jdolecek /*
    917   1.57  jdolecek  * File table vnode kqfilter routine.
    918   1.57  jdolecek  */
    919   1.57  jdolecek int
    920  1.155        ad vn_kqfilter(file_t *fp, struct knote *kn)
    921   1.57  jdolecek {
    922   1.57  jdolecek 
    923  1.231  riastrad 	return VOP_KQFILTER(fp->f_vnode, kn);
    924   1.28      fvdl }
    925   1.28      fvdl 
    926  1.192       chs static int
    927  1.192       chs vn_mmap(struct file *fp, off_t *offp, size_t size, int prot, int *flagsp,
    928  1.231  riastrad     int *advicep, struct uvm_object **uobjp, int *maxprotp)
    929  1.192       chs {
    930  1.192       chs 	struct uvm_object *uobj;
    931  1.192       chs 	struct vnode *vp;
    932  1.192       chs 	struct vattr va;
    933  1.192       chs 	struct lwp *l;
    934  1.192       chs 	vm_prot_t maxprot;
    935  1.192       chs 	off_t off;
    936  1.192       chs 	int error, flags;
    937  1.192       chs 	bool needwritemap;
    938  1.192       chs 
    939  1.192       chs 	l = curlwp;
    940  1.192       chs 
    941  1.192       chs 	off = *offp;
    942  1.192       chs 	flags = *flagsp;
    943  1.192       chs 	maxprot = VM_PROT_EXECUTE;
    944  1.192       chs 
    945  1.229  riastrad 	KASSERT(size > 0);
    946  1.229  riastrad 
    947  1.192       chs 	vp = fp->f_vnode;
    948  1.192       chs 	if (vp->v_type != VREG && vp->v_type != VCHR &&
    949  1.192       chs 	    vp->v_type != VBLK) {
    950  1.192       chs 		/* only REG/CHR/BLK support mmap */
    951  1.192       chs 		return ENODEV;
    952  1.192       chs 	}
    953  1.192       chs 	if (vp->v_type != VCHR && off < 0) {
    954  1.192       chs 		return EINVAL;
    955  1.192       chs 	}
    956  1.233  riastrad #if SIZE_MAX > UINT32_MAX	/* XXX -Wtype-limits */
    957  1.233  riastrad 	if (vp->v_type != VCHR && size > __type_max(off_t)) {
    958  1.233  riastrad 		return EOVERFLOW;
    959  1.233  riastrad 	}
    960  1.233  riastrad #endif
    961  1.233  riastrad 	if (vp->v_type != VCHR && off > __type_max(off_t) - size) {
    962  1.192       chs 		/* no offset wrapping */
    963  1.192       chs 		return EOVERFLOW;
    964  1.192       chs 	}
    965  1.192       chs 
    966  1.192       chs 	/* special case: catch SunOS style /dev/zero */
    967  1.192       chs 	if (vp->v_type == VCHR &&
    968  1.192       chs 	    (vp->v_rdev == zerodev || COMPAT_ZERODEV(vp->v_rdev))) {
    969  1.192       chs 		*uobjp = NULL;
    970  1.192       chs 		*maxprotp = VM_PROT_ALL;
    971  1.192       chs 		return 0;
    972  1.192       chs 	}
    973  1.192       chs 
    974  1.192       chs 	/*
    975  1.192       chs 	 * Old programs may not select a specific sharing type, so
    976  1.192       chs 	 * default to an appropriate one.
    977  1.192       chs 	 *
    978  1.192       chs 	 * XXX: how does MAP_ANON fit in the picture?
    979  1.192       chs 	 */
    980  1.192       chs 	if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) {
    981  1.192       chs #if defined(DEBUG)
    982  1.192       chs 		struct proc *p = l->l_proc;
    983  1.192       chs 		printf("WARNING: defaulted mmap() share type to "
    984  1.192       chs 		       "%s (pid %d command %s)\n", vp->v_type == VCHR ?
    985  1.192       chs 		       "MAP_SHARED" : "MAP_PRIVATE", p->p_pid,
    986  1.192       chs 		       p->p_comm);
    987  1.192       chs #endif
    988  1.192       chs 		if (vp->v_type == VCHR)
    989  1.192       chs 			flags |= MAP_SHARED;	/* for a device */
    990  1.192       chs 		else
    991  1.192       chs 			flags |= MAP_PRIVATE;	/* for a file */
    992  1.192       chs 	}
    993  1.192       chs 
    994  1.192       chs 	/*
    995  1.192       chs 	 * MAP_PRIVATE device mappings don't make sense (and aren't
    996  1.192       chs 	 * supported anyway).  However, some programs rely on this,
    997  1.192       chs 	 * so just change it to MAP_SHARED.
    998  1.192       chs 	 */
    999  1.192       chs 	if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) {
   1000  1.192       chs 		flags = (flags & ~MAP_PRIVATE) | MAP_SHARED;
   1001  1.192       chs 	}
   1002  1.192       chs 
   1003  1.192       chs 	/*
   1004  1.192       chs 	 * now check protection
   1005  1.192       chs 	 */
   1006  1.192       chs 
   1007  1.192       chs 	/* check read access */
   1008  1.192       chs 	if (fp->f_flag & FREAD)
   1009  1.192       chs 		maxprot |= VM_PROT_READ;
   1010  1.192       chs 	else if (prot & PROT_READ) {
   1011  1.192       chs 		return EACCES;
   1012  1.192       chs 	}
   1013  1.192       chs 
   1014  1.192       chs 	/* check write access, shared case first */
   1015  1.192       chs 	if (flags & MAP_SHARED) {
   1016  1.192       chs 		/*
   1017  1.192       chs 		 * if the file is writable, only add PROT_WRITE to
   1018  1.192       chs 		 * maxprot if the file is not immutable, append-only.
   1019  1.192       chs 		 * otherwise, if we have asked for PROT_WRITE, return
   1020  1.192       chs 		 * EPERM.
   1021  1.192       chs 		 */
   1022  1.192       chs 		if (fp->f_flag & FWRITE) {
   1023  1.192       chs 			vn_lock(vp, LK_SHARED | LK_RETRY);
   1024  1.192       chs 			error = VOP_GETATTR(vp, &va, l->l_cred);
   1025  1.192       chs 			VOP_UNLOCK(vp);
   1026  1.192       chs 			if (error) {
   1027  1.192       chs 				return error;
   1028  1.192       chs 			}
   1029  1.192       chs 			if ((va.va_flags &
   1030  1.192       chs 			     (SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0)
   1031  1.192       chs 				maxprot |= VM_PROT_WRITE;
   1032  1.192       chs 			else if (prot & PROT_WRITE) {
   1033  1.192       chs 				return EPERM;
   1034  1.192       chs 			}
   1035  1.192       chs 		} else if (prot & PROT_WRITE) {
   1036  1.192       chs 			return EACCES;
   1037  1.192       chs 		}
   1038  1.192       chs 	} else {
   1039  1.192       chs 		/* MAP_PRIVATE mappings can always write to */
   1040  1.192       chs 		maxprot |= VM_PROT_WRITE;
   1041  1.192       chs 	}
   1042  1.192       chs 
   1043  1.192       chs 	/*
   1044  1.192       chs 	 * Don't allow mmap for EXEC if the file system
   1045  1.192       chs 	 * is mounted NOEXEC.
   1046  1.192       chs 	 */
   1047  1.192       chs 	if ((prot & PROT_EXEC) != 0 &&
   1048  1.192       chs 	    (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) {
   1049  1.192       chs 		return EACCES;
   1050  1.192       chs 	}
   1051  1.192       chs 
   1052  1.192       chs 	if (vp->v_type != VCHR) {
   1053  1.192       chs 		error = VOP_MMAP(vp, prot, curlwp->l_cred);
   1054  1.192       chs 		if (error) {
   1055  1.192       chs 			return error;
   1056  1.192       chs 		}
   1057  1.192       chs 		vref(vp);
   1058  1.192       chs 		uobj = &vp->v_uobj;
   1059  1.192       chs 
   1060  1.192       chs 		/*
   1061  1.192       chs 		 * If the vnode is being mapped with PROT_EXEC,
   1062  1.192       chs 		 * then mark it as text.
   1063  1.192       chs 		 */
   1064  1.192       chs 		if (prot & PROT_EXEC) {
   1065  1.192       chs 			vn_markexec(vp);
   1066  1.192       chs 		}
   1067  1.192       chs 	} else {
   1068  1.192       chs 		int i = maxprot;
   1069  1.192       chs 
   1070  1.192       chs 		/*
   1071  1.192       chs 		 * XXX Some devices don't like to be mapped with
   1072  1.192       chs 		 * XXX PROT_EXEC or PROT_WRITE, but we don't really
   1073  1.192       chs 		 * XXX have a better way of handling this, right now
   1074  1.192       chs 		 */
   1075  1.192       chs 		do {
   1076  1.192       chs 			uobj = udv_attach(vp->v_rdev,
   1077  1.192       chs 					  (flags & MAP_SHARED) ? i :
   1078  1.192       chs 					  (i & ~VM_PROT_WRITE), off, size);
   1079  1.192       chs 			i--;
   1080  1.192       chs 		} while ((uobj == NULL) && (i > 0));
   1081  1.192       chs 		if (uobj == NULL) {
   1082  1.192       chs 			return EINVAL;
   1083  1.192       chs 		}
   1084  1.192       chs 		*advicep = UVM_ADV_RANDOM;
   1085  1.192       chs 	}
   1086  1.192       chs 
   1087  1.192       chs 	/*
   1088  1.192       chs 	 * Set vnode flags to indicate the new kinds of mapping.
   1089  1.192       chs 	 * We take the vnode lock in exclusive mode here to serialize
   1090  1.192       chs 	 * with direct I/O.
   1091  1.192       chs 	 *
   1092  1.192       chs 	 * Safe to check for these flag values without a lock, as
   1093  1.192       chs 	 * long as a reference to the vnode is held.
   1094  1.192       chs 	 */
   1095  1.192       chs 	needwritemap = (vp->v_iflag & VI_WRMAP) == 0 &&
   1096  1.192       chs 		(flags & MAP_SHARED) != 0 &&
   1097  1.192       chs 		(maxprot & VM_PROT_WRITE) != 0;
   1098  1.192       chs 	if ((vp->v_vflag & VV_MAPPED) == 0 || needwritemap) {
   1099  1.192       chs 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1100  1.192       chs 		vp->v_vflag |= VV_MAPPED;
   1101  1.192       chs 		if (needwritemap) {
   1102  1.207        ad 			rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
   1103  1.192       chs 			mutex_enter(vp->v_interlock);
   1104  1.192       chs 			vp->v_iflag |= VI_WRMAP;
   1105  1.192       chs 			mutex_exit(vp->v_interlock);
   1106  1.207        ad 			rw_exit(vp->v_uobj.vmobjlock);
   1107  1.192       chs 		}
   1108  1.192       chs 		VOP_UNLOCK(vp);
   1109  1.192       chs 	}
   1110  1.192       chs 
   1111  1.192       chs #if NVERIEXEC > 0
   1112  1.192       chs 
   1113  1.192       chs 	/*
   1114  1.192       chs 	 * Check if the file can be executed indirectly.
   1115  1.192       chs 	 *
   1116  1.192       chs 	 * XXX: This gives false warnings about "Incorrect access type"
   1117  1.192       chs 	 * XXX: if the mapping is not executable. Harmless, but will be
   1118  1.192       chs 	 * XXX: fixed as part of other changes.
   1119  1.192       chs 	 */
   1120  1.192       chs 	if (veriexec_verify(l, vp, "(mmap)", VERIEXEC_INDIRECT,
   1121  1.192       chs 			    NULL)) {
   1122  1.192       chs 
   1123  1.192       chs 		/*
   1124  1.192       chs 		 * Don't allow executable mappings if we can't
   1125  1.192       chs 		 * indirectly execute the file.
   1126  1.192       chs 		 */
   1127  1.192       chs 		if (prot & VM_PROT_EXECUTE) {
   1128  1.192       chs 			return EPERM;
   1129  1.192       chs 		}
   1130  1.192       chs 
   1131  1.192       chs 		/*
   1132  1.192       chs 		 * Strip the executable bit from 'maxprot' to make sure
   1133  1.192       chs 		 * it can't be made executable later.
   1134  1.192       chs 		 */
   1135  1.192       chs 		maxprot &= ~VM_PROT_EXECUTE;
   1136  1.192       chs 	}
   1137  1.192       chs #endif /* NVERIEXEC > 0 */
   1138  1.192       chs 
   1139  1.192       chs 	*uobjp = uobj;
   1140  1.192       chs 	*maxprotp = maxprot;
   1141  1.192       chs 	*flagsp = flags;
   1142  1.192       chs 
   1143  1.192       chs 	return 0;
   1144  1.192       chs }
   1145  1.192       chs 
   1146  1.222  riastrad static int
   1147  1.222  riastrad vn_seek(struct file *fp, off_t delta, int whence, off_t *newoffp,
   1148  1.222  riastrad     int flags)
   1149  1.222  riastrad {
   1150  1.225  riastrad 	const off_t OFF_MIN = __type_min(off_t);
   1151  1.225  riastrad 	const off_t OFF_MAX = __type_max(off_t);
   1152  1.222  riastrad 	kauth_cred_t cred = fp->f_cred;
   1153  1.222  riastrad 	off_t oldoff, newoff;
   1154  1.222  riastrad 	struct vnode *vp = fp->f_vnode;
   1155  1.222  riastrad 	struct vattr vattr;
   1156  1.222  riastrad 	int error;
   1157  1.222  riastrad 
   1158  1.222  riastrad 	if (vp->v_type == VFIFO)
   1159  1.222  riastrad 		return ESPIPE;
   1160  1.222  riastrad 
   1161  1.235  riastrad 	if (flags & FOF_UPDATE_OFFSET)
   1162  1.235  riastrad 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1163  1.235  riastrad 	else
   1164  1.235  riastrad 		vn_lock(vp, LK_SHARED | LK_RETRY);
   1165  1.222  riastrad 
   1166  1.222  riastrad 	/* Compute the old and new offsets.  */
   1167  1.238  riastrad 	if (vp->v_type == VDIR && (flags & FOF_UPDATE_OFFSET) == 0)
   1168  1.238  riastrad 		mutex_enter(&fp->f_lock);
   1169  1.222  riastrad 	oldoff = fp->f_offset;
   1170  1.238  riastrad 	if (vp->v_type == VDIR && (flags & FOF_UPDATE_OFFSET) == 0)
   1171  1.238  riastrad 		mutex_exit(&fp->f_lock);
   1172  1.222  riastrad 	switch (whence) {
   1173  1.222  riastrad 	case SEEK_CUR:
   1174  1.225  riastrad 		if (delta > 0) {
   1175  1.225  riastrad 			if (oldoff > 0 && delta > OFF_MAX - oldoff) {
   1176  1.225  riastrad 				newoff = OFF_MAX;
   1177  1.225  riastrad 				break;
   1178  1.225  riastrad 			}
   1179  1.225  riastrad 		} else {
   1180  1.225  riastrad 			if (oldoff < 0 && delta < OFF_MIN - oldoff) {
   1181  1.225  riastrad 				newoff = OFF_MIN;
   1182  1.225  riastrad 				break;
   1183  1.225  riastrad 			}
   1184  1.225  riastrad 		}
   1185  1.225  riastrad 		newoff = oldoff + delta;
   1186  1.222  riastrad 		break;
   1187  1.222  riastrad 	case SEEK_END:
   1188  1.222  riastrad 		error = VOP_GETATTR(vp, &vattr, cred);
   1189  1.222  riastrad 		if (error)
   1190  1.222  riastrad 			goto out;
   1191  1.225  riastrad 		if (vattr.va_size > OFF_MAX ||
   1192  1.225  riastrad 		    delta > OFF_MAX - (off_t)vattr.va_size) {
   1193  1.225  riastrad 			newoff = OFF_MAX;
   1194  1.225  riastrad 			break;
   1195  1.225  riastrad 		}
   1196  1.225  riastrad 		newoff = delta + vattr.va_size;
   1197  1.222  riastrad 		break;
   1198  1.222  riastrad 	case SEEK_SET:
   1199  1.222  riastrad 		newoff = delta;
   1200  1.222  riastrad 		break;
   1201  1.222  riastrad 	default:
   1202  1.222  riastrad 		error = EINVAL;
   1203  1.222  riastrad 		goto out;
   1204  1.222  riastrad 	}
   1205  1.222  riastrad 
   1206  1.222  riastrad 	/* Pass the proposed change to the file system to audit.  */
   1207  1.222  riastrad 	error = VOP_SEEK(vp, oldoff, newoff, cred);
   1208  1.222  riastrad 	if (error)
   1209  1.222  riastrad 		goto out;
   1210  1.222  riastrad 
   1211  1.222  riastrad 	/* Success!  */
   1212  1.222  riastrad 	if (newoffp)
   1213  1.222  riastrad 		*newoffp = newoff;
   1214  1.222  riastrad 	if (flags & FOF_UPDATE_OFFSET)
   1215  1.222  riastrad 		fp->f_offset = newoff;
   1216  1.222  riastrad 	error = 0;
   1217  1.192       chs 
   1218  1.222  riastrad out:	VOP_UNLOCK(vp);
   1219  1.222  riastrad 	return error;
   1220  1.222  riastrad }
   1221  1.192       chs 
   1222  1.239  riastrad static int
   1223  1.239  riastrad vn_advlock(struct file *fp, void *id, int op, struct flock *fl,
   1224  1.239  riastrad     int flags)
   1225  1.239  riastrad {
   1226  1.239  riastrad 	struct vnode *const vp = fp->f_vnode;
   1227  1.239  riastrad 
   1228  1.239  riastrad 	if (fl->l_whence == SEEK_CUR) {
   1229  1.239  riastrad 		vn_lock(vp, LK_SHARED | LK_RETRY);
   1230  1.239  riastrad 		fl->l_start += fp->f_offset;
   1231  1.239  riastrad 		VOP_UNLOCK(vp);
   1232  1.239  riastrad 	}
   1233  1.239  riastrad 
   1234  1.239  riastrad 	return VOP_ADVLOCK(vp, id, op, fl, flags);
   1235  1.239  riastrad }
   1236  1.239  riastrad 
   1237   1.28      fvdl /*
   1238   1.28      fvdl  * Check that the vnode is still valid, and if so
   1239   1.28      fvdl  * acquire requested lock.
   1240   1.28      fvdl  */
   1241   1.28      fvdl int
   1242   1.89   thorpej vn_lock(struct vnode *vp, int flags)
   1243   1.28      fvdl {
   1244  1.205        ad 	struct lwp *l;
   1245   1.28      fvdl 	int error;
   1246   1.34  sommerfe 
   1247  1.227   hannken 	KASSERT(vrefcnt(vp) > 0);
   1248  1.203        ad 	KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY|
   1249  1.203        ad 	    LK_UPGRADE|LK_DOWNGRADE)) == 0);
   1250  1.203        ad 	KASSERT((flags & LK_NOWAIT) != 0 || !mutex_owned(vp->v_interlock));
   1251   1.80      yamt 
   1252  1.159    simonb #ifdef DIAGNOSTIC
   1253  1.159    simonb 	if (wapbl_vphaswapbl(vp))
   1254  1.159    simonb 		WAPBL_JUNLOCK_ASSERT(wapbl_vptomp(vp));
   1255  1.159    simonb #endif
   1256  1.159    simonb 
   1257  1.205        ad 	/* Get a more useful report for lockstat. */
   1258  1.205        ad 	l = curlwp;
   1259  1.205        ad 	KASSERT(l->l_rwcallsite == 0);
   1260  1.205        ad 	l->l_rwcallsite = (uintptr_t)__builtin_return_address(0);
   1261  1.205        ad 
   1262  1.189   hannken 	error = VOP_LOCK(vp, flags);
   1263  1.189   hannken 
   1264  1.205        ad 	l->l_rwcallsite = 0;
   1265  1.205        ad 
   1266  1.227   hannken 	switch (flags & (LK_RETRY | LK_NOWAIT)) {
   1267  1.227   hannken 	case 0:
   1268  1.227   hannken 		KASSERT(error == 0 || error == ENOENT);
   1269  1.227   hannken 		break;
   1270  1.227   hannken 	case LK_RETRY:
   1271  1.227   hannken 		KASSERT(error == 0);
   1272  1.227   hannken 		break;
   1273  1.227   hannken 	case LK_NOWAIT:
   1274  1.227   hannken 		KASSERT(error == 0 || error == EBUSY || error == ENOENT);
   1275  1.227   hannken 		break;
   1276  1.227   hannken 	case LK_RETRY | LK_NOWAIT:
   1277  1.227   hannken 		KASSERT(error == 0 || error == EBUSY);
   1278  1.227   hannken 		break;
   1279  1.227   hannken 	}
   1280  1.189   hannken 
   1281  1.189   hannken 	return error;
   1282   1.10       cgd }
   1283   1.10       cgd 
   1284   1.10       cgd /*
   1285   1.10       cgd  * File table vnode close routine.
   1286   1.10       cgd  */
   1287   1.56  gmcgarry static int
   1288  1.155        ad vn_closefile(file_t *fp)
   1289   1.10       cgd {
   1290   1.10       cgd 
   1291  1.191      matt 	return vn_close(fp->f_vnode, fp->f_flag, fp->f_cred);
   1292   1.39      fvdl }
   1293   1.39      fvdl 
   1294   1.39      fvdl /*
   1295   1.85   thorpej  * Simplified in-kernel wrapper calls for extended attribute access.
   1296   1.85   thorpej  * Both calls pass in a NULL credential, authorizing a "kernel" access.
   1297   1.85   thorpej  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
   1298   1.85   thorpej  */
   1299   1.85   thorpej int
   1300   1.85   thorpej vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
   1301  1.101  christos     const char *attrname, size_t *buflen, void *bf, struct lwp *l)
   1302   1.85   thorpej {
   1303   1.85   thorpej 	struct uio auio;
   1304   1.85   thorpej 	struct iovec aiov;
   1305   1.85   thorpej 	int error;
   1306   1.85   thorpej 
   1307   1.85   thorpej 	aiov.iov_len = *buflen;
   1308   1.88  christos 	aiov.iov_base = bf;
   1309   1.85   thorpej 
   1310   1.85   thorpej 	auio.uio_iov = &aiov;
   1311   1.85   thorpej 	auio.uio_iovcnt = 1;
   1312   1.85   thorpej 	auio.uio_rw = UIO_READ;
   1313   1.85   thorpej 	auio.uio_offset = 0;
   1314   1.85   thorpej 	auio.uio_resid = *buflen;
   1315  1.106      yamt 	UIO_SETUP_SYSSPACE(&auio);
   1316   1.85   thorpej 
   1317   1.85   thorpej 	if ((ioflg & IO_NODELOCKED) == 0)
   1318   1.85   thorpej 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1319   1.86     perry 
   1320  1.208  christos 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL,
   1321  1.208  christos 	    NOCRED);
   1322   1.86     perry 
   1323   1.85   thorpej 	if ((ioflg & IO_NODELOCKED) == 0)
   1324  1.174   hannken 		VOP_UNLOCK(vp);
   1325   1.86     perry 
   1326   1.85   thorpej 	if (error == 0)
   1327   1.85   thorpej 		*buflen = *buflen - auio.uio_resid;
   1328   1.86     perry 
   1329  1.231  riastrad 	return error;
   1330   1.85   thorpej }
   1331   1.85   thorpej 
   1332   1.85   thorpej /*
   1333   1.85   thorpej  * XXX Failure mode if partially written?
   1334   1.85   thorpej  */
   1335   1.85   thorpej int
   1336   1.85   thorpej vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
   1337  1.101  christos     const char *attrname, size_t buflen, const void *bf, struct lwp *l)
   1338   1.85   thorpej {
   1339   1.85   thorpej 	struct uio auio;
   1340   1.85   thorpej 	struct iovec aiov;
   1341   1.85   thorpej 	int error;
   1342   1.85   thorpej 
   1343   1.85   thorpej 	aiov.iov_len = buflen;
   1344   1.88  christos 	aiov.iov_base = __UNCONST(bf);		/* XXXUNCONST kills const */
   1345   1.85   thorpej 
   1346   1.85   thorpej 	auio.uio_iov = &aiov;
   1347   1.85   thorpej 	auio.uio_iovcnt = 1;
   1348   1.85   thorpej 	auio.uio_rw = UIO_WRITE;
   1349   1.85   thorpej 	auio.uio_offset = 0;
   1350   1.85   thorpej 	auio.uio_resid = buflen;
   1351  1.106      yamt 	UIO_SETUP_SYSSPACE(&auio);
   1352   1.85   thorpej 
   1353   1.85   thorpej 	if ((ioflg & IO_NODELOCKED) == 0) {
   1354   1.85   thorpej 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1355   1.85   thorpej 	}
   1356   1.85   thorpej 
   1357  1.208  christos 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NOCRED);
   1358   1.85   thorpej 
   1359   1.85   thorpej 	if ((ioflg & IO_NODELOCKED) == 0) {
   1360  1.174   hannken 		VOP_UNLOCK(vp);
   1361   1.85   thorpej 	}
   1362   1.85   thorpej 
   1363  1.231  riastrad 	return error;
   1364   1.85   thorpej }
   1365   1.85   thorpej 
   1366   1.85   thorpej int
   1367   1.85   thorpej vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
   1368  1.101  christos     const char *attrname, struct lwp *l)
   1369   1.85   thorpej {
   1370   1.85   thorpej 	int error;
   1371   1.85   thorpej 
   1372   1.85   thorpej 	if ((ioflg & IO_NODELOCKED) == 0) {
   1373   1.85   thorpej 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1374   1.85   thorpej 	}
   1375   1.85   thorpej 
   1376  1.210  christos 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NOCRED);
   1377   1.85   thorpej 	if (error == EOPNOTSUPP)
   1378  1.208  christos 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
   1379  1.208  christos 		    NOCRED);
   1380   1.86     perry 
   1381   1.85   thorpej 	if ((ioflg & IO_NODELOCKED) == 0) {
   1382  1.174   hannken 		VOP_UNLOCK(vp);
   1383   1.85   thorpej 	}
   1384   1.85   thorpej 
   1385  1.231  riastrad 	return error;
   1386   1.85   thorpej }
   1387   1.95      yamt 
   1388  1.170     pooka int
   1389  1.170     pooka vn_fifo_bypass(void *v)
   1390  1.170     pooka {
   1391  1.170     pooka 	struct vop_generic_args *ap = v;
   1392  1.170     pooka 
   1393  1.170     pooka 	return VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, v);
   1394  1.170     pooka }
   1395  1.202   mlelstv 
   1396  1.202   mlelstv /*
   1397  1.202   mlelstv  * Open block device by device number
   1398  1.202   mlelstv  */
   1399  1.202   mlelstv int
   1400  1.202   mlelstv vn_bdev_open(dev_t dev, struct vnode **vpp, struct lwp *l)
   1401  1.202   mlelstv {
   1402  1.202   mlelstv 	int     error;
   1403  1.202   mlelstv 
   1404  1.202   mlelstv 	if ((error = bdevvp(dev, vpp)) != 0)
   1405  1.202   mlelstv 		return error;
   1406  1.202   mlelstv 
   1407  1.226   hannken 	vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
   1408  1.202   mlelstv 	if ((error = VOP_OPEN(*vpp, FREAD | FWRITE, l->l_cred)) != 0) {
   1409  1.226   hannken 		vput(*vpp);
   1410  1.202   mlelstv 		return error;
   1411  1.202   mlelstv 	}
   1412  1.202   mlelstv 	mutex_enter((*vpp)->v_interlock);
   1413  1.202   mlelstv 	(*vpp)->v_writecount++;
   1414  1.202   mlelstv 	mutex_exit((*vpp)->v_interlock);
   1415  1.226   hannken 	VOP_UNLOCK(*vpp);
   1416  1.202   mlelstv 
   1417  1.202   mlelstv 	return 0;
   1418  1.202   mlelstv }
   1419  1.202   mlelstv 
   1420  1.202   mlelstv /*
   1421  1.202   mlelstv  * Lookup the provided name in the filesystem.  If the file exists,
   1422  1.202   mlelstv  * is a valid block device, and isn't being used by anyone else,
   1423  1.202   mlelstv  * set *vpp to the file's vnode.
   1424  1.202   mlelstv  */
   1425  1.202   mlelstv int
   1426  1.202   mlelstv vn_bdev_openpath(struct pathbuf *pb, struct vnode **vpp, struct lwp *l)
   1427  1.202   mlelstv {
   1428  1.202   mlelstv 	struct vnode *vp;
   1429  1.202   mlelstv 	dev_t dev;
   1430  1.202   mlelstv 	enum vtype vt;
   1431  1.202   mlelstv 	int     error;
   1432  1.202   mlelstv 
   1433  1.216  dholland 	error = vn_open(NULL, pb, 0, FREAD | FWRITE, 0, &vp, NULL, NULL);
   1434  1.216  dholland 	if (error != 0)
   1435  1.202   mlelstv 		return error;
   1436  1.202   mlelstv 
   1437  1.202   mlelstv 	dev = vp->v_rdev;
   1438  1.202   mlelstv 	vt = vp->v_type;
   1439  1.202   mlelstv 
   1440  1.202   mlelstv 	VOP_UNLOCK(vp);
   1441  1.202   mlelstv 	(void) vn_close(vp, FREAD | FWRITE, l->l_cred);
   1442  1.202   mlelstv 
   1443  1.202   mlelstv 	if (vt != VBLK)
   1444  1.202   mlelstv 		return ENOTBLK;
   1445  1.202   mlelstv 
   1446  1.202   mlelstv 	return vn_bdev_open(dev, vpp, l);
   1447  1.202   mlelstv }
   1448  1.224   thorpej 
   1449  1.224   thorpej static long
   1450  1.224   thorpej vn_knote_to_interest(const struct knote *kn)
   1451  1.224   thorpej {
   1452  1.224   thorpej 	switch (kn->kn_filter) {
   1453  1.224   thorpej 	case EVFILT_READ:
   1454  1.224   thorpej 		/*
   1455  1.224   thorpej 		 * Writing to the file or changing its attributes can
   1456  1.224   thorpej 		 * set the file size, which impacts the readability
   1457  1.224   thorpej 		 * filter.
   1458  1.224   thorpej 		 *
   1459  1.224   thorpej 		 * (No need to set NOTE_EXTEND here; it's only ever
   1460  1.224   thorpej 		 * send with other hints; see vnode_if.c.)
   1461  1.224   thorpej 		 */
   1462  1.224   thorpej 		return NOTE_WRITE | NOTE_ATTRIB;
   1463  1.224   thorpej 
   1464  1.224   thorpej 	case EVFILT_VNODE:
   1465  1.224   thorpej 		return kn->kn_sfflags;
   1466  1.224   thorpej 
   1467  1.224   thorpej 	case EVFILT_WRITE:
   1468  1.224   thorpej 	default:
   1469  1.224   thorpej 		return 0;
   1470  1.224   thorpej 	}
   1471  1.224   thorpej }
   1472  1.224   thorpej 
   1473  1.224   thorpej void
   1474  1.224   thorpej vn_knote_attach(struct vnode *vp, struct knote *kn)
   1475  1.224   thorpej {
   1476  1.234   thorpej 	struct vnode_klist *vk = vp->v_klist;
   1477  1.224   thorpej 	long interest = 0;
   1478  1.224   thorpej 
   1479  1.224   thorpej 	/*
   1480  1.234   thorpej 	 * In the case of layered / stacked file systems, knotes
   1481  1.234   thorpej 	 * should only ever be associated with the base vnode.
   1482  1.234   thorpej 	 */
   1483  1.234   thorpej 	KASSERT(kn->kn_hook == vp);
   1484  1.234   thorpej 	KASSERT(vp->v_klist == &VNODE_TO_VIMPL(vp)->vi_klist);
   1485  1.234   thorpej 
   1486  1.234   thorpej 	/*
   1487  1.224   thorpej 	 * We maintain a bitmask of the kevents that there is interest in,
   1488  1.224   thorpej 	 * to minimize the impact of having watchers.  It's silly to have
   1489  1.224   thorpej 	 * to traverse vn_klist every time a read or write happens simply
   1490  1.224   thorpej 	 * because there is someone interested in knowing when the file
   1491  1.224   thorpej 	 * is deleted, for example.
   1492  1.224   thorpej 	 */
   1493  1.224   thorpej 
   1494  1.224   thorpej 	mutex_enter(vp->v_interlock);
   1495  1.234   thorpej 	SLIST_INSERT_HEAD(&vk->vk_klist, kn, kn_selnext);
   1496  1.234   thorpej 	SLIST_FOREACH(kn, &vk->vk_klist, kn_selnext) {
   1497  1.224   thorpej 		interest |= vn_knote_to_interest(kn);
   1498  1.224   thorpej 	}
   1499  1.234   thorpej 	vk->vk_interest = interest;
   1500  1.224   thorpej 	mutex_exit(vp->v_interlock);
   1501  1.224   thorpej }
   1502  1.224   thorpej 
   1503  1.224   thorpej void
   1504  1.224   thorpej vn_knote_detach(struct vnode *vp, struct knote *kn)
   1505  1.224   thorpej {
   1506  1.234   thorpej 	struct vnode_klist *vk = vp->v_klist;
   1507  1.234   thorpej 	long interest = 0;
   1508  1.234   thorpej 
   1509  1.234   thorpej 	/* See above. */
   1510  1.234   thorpej 	KASSERT(kn->kn_hook == vp);
   1511  1.234   thorpej 	KASSERT(vp->v_klist == &VNODE_TO_VIMPL(vp)->vi_klist);
   1512  1.224   thorpej 
   1513  1.224   thorpej 	/*
   1514  1.228    andvar 	 * We special case removing the head of the list, because:
   1515  1.224   thorpej 	 *
   1516  1.224   thorpej 	 * 1. It's extremely likely that we're detaching the only
   1517  1.224   thorpej 	 *    knote.
   1518  1.224   thorpej 	 *
   1519  1.224   thorpej 	 * 2. We're already traversing the whole list, so we don't
   1520  1.224   thorpej 	 *    want to use the generic SLIST_REMOVE() which would
   1521  1.224   thorpej 	 *    traverse it *again*.
   1522  1.224   thorpej 	 */
   1523  1.224   thorpej 
   1524  1.224   thorpej 	mutex_enter(vp->v_interlock);
   1525  1.234   thorpej 	if (__predict_true(kn == SLIST_FIRST(&vk->vk_klist))) {
   1526  1.234   thorpej 		SLIST_REMOVE_HEAD(&vk->vk_klist, kn_selnext);
   1527  1.234   thorpej 		SLIST_FOREACH(kn, &vk->vk_klist, kn_selnext) {
   1528  1.224   thorpej 			interest |= vn_knote_to_interest(kn);
   1529  1.224   thorpej 		}
   1530  1.234   thorpej 		vk->vk_interest = interest;
   1531  1.224   thorpej 	} else {
   1532  1.224   thorpej 		struct knote *thiskn, *nextkn, *prevkn = NULL;
   1533  1.224   thorpej 
   1534  1.234   thorpej 		SLIST_FOREACH_SAFE(thiskn, &vk->vk_klist, kn_selnext, nextkn) {
   1535  1.224   thorpej 			if (thiskn == kn) {
   1536  1.224   thorpej 				KASSERT(kn != NULL);
   1537  1.224   thorpej 				KASSERT(prevkn != NULL);
   1538  1.224   thorpej 				SLIST_REMOVE_AFTER(prevkn, kn_selnext);
   1539  1.224   thorpej 				kn = NULL;
   1540  1.224   thorpej 			} else {
   1541  1.224   thorpej 				interest |= vn_knote_to_interest(thiskn);
   1542  1.224   thorpej 				prevkn = thiskn;
   1543  1.224   thorpej 			}
   1544  1.224   thorpej 		}
   1545  1.234   thorpej 		vk->vk_interest = interest;
   1546  1.224   thorpej 	}
   1547  1.224   thorpej 	mutex_exit(vp->v_interlock);
   1548  1.224   thorpej }
   1549