Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.356
      1  1.356        ad /*	$NetBSD: vfs_syscalls.c,v 1.356 2008/05/06 12:54:25 ad Exp $	*/
      2  1.345        ad 
      3  1.345        ad /*-
      4  1.345        ad  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.345        ad  * All rights reserved.
      6  1.345        ad  *
      7  1.345        ad  * Redistribution and use in source and binary forms, with or without
      8  1.345        ad  * modification, are permitted provided that the following conditions
      9  1.345        ad  * are met:
     10  1.345        ad  * 1. Redistributions of source code must retain the above copyright
     11  1.345        ad  *    notice, this list of conditions and the following disclaimer.
     12  1.345        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.345        ad  *    notice, this list of conditions and the following disclaimer in the
     14  1.345        ad  *    documentation and/or other materials provided with the distribution.
     15  1.345        ad  *
     16  1.345        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.345        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.345        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.345        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.345        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.345        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.345        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.345        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.345        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.345        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.345        ad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.345        ad  */
     28   1.31       cgd 
     29   1.31       cgd /*
     30   1.31       cgd  * Copyright (c) 1989, 1993
     31   1.31       cgd  *	The Regents of the University of California.  All rights reserved.
     32   1.31       cgd  * (c) UNIX System Laboratories, Inc.
     33   1.31       cgd  * All or some portions of this file are derived from material licensed
     34   1.31       cgd  * to the University of California by American Telephone and Telegraph
     35   1.31       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     36   1.31       cgd  * the permission of UNIX System Laboratories, Inc.
     37   1.31       cgd  *
     38   1.31       cgd  * Redistribution and use in source and binary forms, with or without
     39   1.31       cgd  * modification, are permitted provided that the following conditions
     40   1.31       cgd  * are met:
     41   1.31       cgd  * 1. Redistributions of source code must retain the above copyright
     42   1.31       cgd  *    notice, this list of conditions and the following disclaimer.
     43   1.31       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     44   1.31       cgd  *    notice, this list of conditions and the following disclaimer in the
     45   1.31       cgd  *    documentation and/or other materials provided with the distribution.
     46  1.191       agc  * 3. Neither the name of the University nor the names of its contributors
     47   1.31       cgd  *    may be used to endorse or promote products derived from this software
     48   1.31       cgd  *    without specific prior written permission.
     49   1.31       cgd  *
     50   1.31       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51   1.31       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52   1.31       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53   1.31       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54   1.31       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55   1.31       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56   1.31       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57   1.31       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58   1.31       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59   1.31       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60   1.31       cgd  * SUCH DAMAGE.
     61   1.31       cgd  *
     62  1.113      fvdl  *	@(#)vfs_syscalls.c	8.42 (Berkeley) 7/31/95
     63   1.31       cgd  */
     64  1.173     lukem 
     65  1.173     lukem #include <sys/cdefs.h>
     66  1.356        ad __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.356 2008/05/06 12:54:25 ad Exp $");
     67  1.111       mrg 
     68  1.121  jonathan #include "opt_compat_netbsd.h"
     69  1.127  christos #include "opt_compat_43.h"
     70  1.258      elad #include "opt_fileassoc.h"
     71  1.202   hannken #include "fss.h"
     72  1.261    dogcow #include "veriexec.h"
     73   1.31       cgd 
     74   1.31       cgd #include <sys/param.h>
     75   1.31       cgd #include <sys/systm.h>
     76   1.31       cgd #include <sys/namei.h>
     77   1.31       cgd #include <sys/filedesc.h>
     78   1.31       cgd #include <sys/kernel.h>
     79   1.31       cgd #include <sys/file.h>
     80   1.31       cgd #include <sys/stat.h>
     81   1.31       cgd #include <sys/vnode.h>
     82   1.31       cgd #include <sys/mount.h>
     83   1.31       cgd #include <sys/proc.h>
     84   1.31       cgd #include <sys/uio.h>
     85   1.31       cgd #include <sys/malloc.h>
     86  1.248      yamt #include <sys/kmem.h>
     87   1.31       cgd #include <sys/dirent.h>
     88  1.172    simonb #include <sys/sysctl.h>
     89   1.35       cgd #include <sys/syscallargs.h>
     90  1.306       dsl #include <sys/vfs_syscalls.h>
     91  1.192  drochner #include <sys/ktrace.h>
     92  1.251      elad #ifdef FILEASSOC
     93  1.251      elad #include <sys/fileassoc.h>
     94  1.251      elad #endif /* FILEASSOC */
     95  1.219     blymn #include <sys/verified_exec.h>
     96  1.242      elad #include <sys/kauth.h>
     97  1.346        ad #include <sys/atomic.h>
     98   1.35       cgd 
     99  1.148      fvdl #include <miscfs/genfs/genfs.h>
    100  1.148      fvdl #include <miscfs/syncfs/syncfs.h>
    101  1.342        ad #include <miscfs/specfs/specdev.h>
    102  1.181   thorpej 
    103  1.232      jmmv #ifdef COMPAT_30
    104  1.232      jmmv #include "opt_nfsserver.h"
    105  1.232      jmmv #include <nfs/rpcv2.h>
    106  1.267      yamt #endif
    107  1.232      jmmv #include <nfs/nfsproto.h>
    108  1.267      yamt #ifdef COMPAT_30
    109  1.232      jmmv #include <nfs/nfs.h>
    110  1.232      jmmv #include <nfs/nfs_var.h>
    111  1.232      jmmv #endif
    112  1.232      jmmv 
    113  1.202   hannken #if NFSS > 0
    114  1.202   hannken #include <dev/fssvar.h>
    115  1.202   hannken #endif
    116  1.202   hannken 
    117  1.181   thorpej MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
    118  1.110       mrg 
    119  1.234  christos static int change_dir(struct nameidata *, struct lwp *);
    120  1.234  christos static int change_flags(struct vnode *, u_long, struct lwp *);
    121  1.234  christos static int change_mode(struct vnode *, int, struct lwp *l);
    122  1.234  christos static int change_owner(struct vnode *, uid_t, gid_t, struct lwp *, int);
    123   1.63  christos 
    124  1.205  junyoung void checkdirs(struct vnode *);
    125   1.31       cgd 
    126  1.150      fvdl int dovfsusermount = 0;
    127  1.150      fvdl 
    128   1.31       cgd /*
    129   1.31       cgd  * Virtual File System System Calls
    130   1.31       cgd  */
    131   1.31       cgd 
    132   1.31       cgd /*
    133   1.31       cgd  * Mount a file system.
    134   1.31       cgd  */
    135   1.99   thorpej 
    136  1.167  jdolecek #if defined(COMPAT_09) || defined(COMPAT_43)
    137   1.99   thorpej /*
    138   1.99   thorpej  * This table is used to maintain compatibility with 4.3BSD
    139   1.99   thorpej  * and NetBSD 0.9 mount syscalls.  Note, the order is important!
    140  1.124   thorpej  *
    141  1.167  jdolecek  * Do not modify this table. It should only contain filesystems
    142  1.167  jdolecek  * supported by NetBSD 0.9 and 4.3BSD.
    143   1.99   thorpej  */
    144  1.167  jdolecek const char * const mountcompatnames[] = {
    145   1.99   thorpej 	NULL,		/* 0 = MOUNT_NONE */
    146  1.167  jdolecek 	MOUNT_FFS,	/* 1 = MOUNT_UFS */
    147   1.99   thorpej 	MOUNT_NFS,	/* 2 */
    148   1.99   thorpej 	MOUNT_MFS,	/* 3 */
    149   1.99   thorpej 	MOUNT_MSDOS,	/* 4 */
    150  1.167  jdolecek 	MOUNT_CD9660,	/* 5 = MOUNT_ISOFS */
    151  1.167  jdolecek 	MOUNT_FDESC,	/* 6 */
    152  1.167  jdolecek 	MOUNT_KERNFS,	/* 7 */
    153  1.167  jdolecek 	NULL,		/* 8 = MOUNT_DEVFS */
    154  1.167  jdolecek 	MOUNT_AFS,	/* 9 */
    155   1.99   thorpej };
    156  1.113      fvdl const int nmountcompatnames = sizeof(mountcompatnames) /
    157   1.99   thorpej     sizeof(mountcompatnames[0]);
    158  1.167  jdolecek #endif /* COMPAT_09 || COMPAT_43 */
    159   1.99   thorpej 
    160  1.285      elad static int
    161  1.283      elad mount_update(struct lwp *l, struct vnode *vp, const char *path, int flags,
    162  1.324     pooka     void *data, size_t *data_len)
    163   1.56   thorpej {
    164  1.113      fvdl 	struct mount *mp;
    165  1.283      elad 	int error = 0, saved_flags;
    166  1.283      elad 
    167  1.283      elad 	mp = vp->v_mount;
    168  1.283      elad 	saved_flags = mp->mnt_flag;
    169  1.283      elad 
    170  1.329        ad 	/* We can operate only on VV_ROOT nodes. */
    171  1.329        ad 	if ((vp->v_vflag & VV_ROOT) == 0) {
    172  1.329        ad 		error = EINVAL;
    173  1.329        ad 		goto out;
    174  1.329        ad 	}
    175   1.31       cgd 
    176  1.218      yamt 	/*
    177  1.283      elad 	 * We only allow the filesystem to be reloaded if it
    178  1.283      elad 	 * is currently mounted read-only.
    179  1.283      elad 	 */
    180  1.329        ad 	if (flags & MNT_RELOAD && !(mp->mnt_flag & MNT_RDONLY)) {
    181  1.329        ad 		error = EOPNOTSUPP;	/* Needs translation */
    182  1.329        ad 		goto out;
    183  1.329        ad 	}
    184  1.292      elad 
    185  1.292      elad 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    186  1.292      elad 	    KAUTH_REQ_SYSTEM_MOUNT_UPDATE, mp, KAUTH_ARG(flags), data);
    187  1.292      elad 	if (error)
    188  1.329        ad 		goto out;
    189  1.292      elad 
    190  1.353        ad 	if (vfs_busy(mp, RW_WRITER)) {
    191  1.329        ad 		error = EPERM;
    192  1.329        ad 		goto out;
    193  1.329        ad 	}
    194  1.283      elad 
    195  1.286      yamt 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    196  1.286      yamt 	mp->mnt_flag |= flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
    197  1.286      yamt 
    198  1.283      elad 	/*
    199  1.283      elad 	 * Set the mount level flags.
    200  1.283      elad 	 */
    201  1.283      elad 	if (flags & MNT_RDONLY)
    202  1.283      elad 		mp->mnt_flag |= MNT_RDONLY;
    203  1.283      elad 	else if (mp->mnt_flag & MNT_RDONLY)
    204  1.283      elad 		mp->mnt_iflag |= IMNT_WANTRDWR;
    205  1.283      elad 	mp->mnt_flag &=
    206  1.283      elad 	  ~(MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    207  1.283      elad 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    208  1.283      elad 	    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP);
    209  1.283      elad 	mp->mnt_flag |= flags &
    210  1.283      elad 	   (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    211  1.283      elad 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    212  1.283      elad 	    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
    213  1.283      elad 	    MNT_IGNORE);
    214  1.283      elad 
    215  1.332     pooka 	error = VFS_MOUNT(mp, path, data, data_len);
    216  1.283      elad 
    217  1.283      elad #if defined(COMPAT_30) && defined(NFSSERVER)
    218  1.320       dsl 	if (error && data != NULL) {
    219  1.283      elad 		int error2;
    220  1.283      elad 
    221  1.283      elad 		/* Update failed; let's try and see if it was an
    222  1.283      elad 		 * export request. */
    223  1.283      elad 		error2 = nfs_update_exports_30(mp, path, data, l);
    224  1.283      elad 
    225  1.283      elad 		/* Only update error code if the export request was
    226  1.283      elad 		 * understood but some problem occurred while
    227  1.283      elad 		 * processing it. */
    228  1.283      elad 		if (error2 != EJUSTRETURN)
    229  1.283      elad 			error = error2;
    230  1.283      elad 	}
    231  1.283      elad #endif
    232  1.283      elad 	if (mp->mnt_iflag & IMNT_WANTRDWR)
    233  1.283      elad 		mp->mnt_flag &= ~MNT_RDONLY;
    234  1.283      elad 	if (error)
    235  1.283      elad 		mp->mnt_flag = saved_flags;
    236  1.286      yamt 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    237  1.286      yamt 	mp->mnt_iflag &= ~IMNT_WANTRDWR;
    238  1.283      elad 	if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
    239  1.283      elad 		if (mp->mnt_syncer == NULL)
    240  1.283      elad 			error = vfs_allocate_syncvnode(mp);
    241  1.125       tls 	} else {
    242  1.283      elad 		if (mp->mnt_syncer != NULL)
    243  1.283      elad 			vfs_deallocate_syncvnode(mp);
    244  1.283      elad 	}
    245  1.354        ad 	vfs_unbusy(mp, false, NULL);
    246  1.283      elad 
    247  1.329        ad  out:
    248  1.283      elad 	return (error);
    249  1.283      elad }
    250  1.283      elad 
    251  1.285      elad static int
    252  1.320       dsl mount_get_vfsops(const char *fstype, struct vfsops **vfsops)
    253  1.283      elad {
    254  1.322  christos 	char fstypename[sizeof(((struct statvfs *)NULL)->f_fstypename)];
    255  1.283      elad 	int error;
    256  1.283      elad 
    257  1.320       dsl 	/* Copy file-system type from userspace.  */
    258  1.322  christos 	error = copyinstr(fstype, fstypename, sizeof(fstypename), NULL);
    259   1.63  christos 	if (error) {
    260   1.54       cgd #if defined(COMPAT_09) || defined(COMPAT_43)
    261   1.54       cgd 		/*
    262  1.227      jmmv 		 * Historically, filesystem types were identified by numbers.
    263   1.54       cgd 		 * If we get an integer for the filesystem type instead of a
    264   1.54       cgd 		 * string, we check to see if it matches one of the historic
    265   1.54       cgd 		 * filesystem types.
    266  1.205  junyoung 		 */
    267  1.283      elad 		u_long fsindex = (u_long)fstype;
    268   1.99   thorpej 		if (fsindex >= nmountcompatnames ||
    269  1.320       dsl 		    mountcompatnames[fsindex] == NULL)
    270  1.320       dsl 			return ENODEV;
    271  1.331     pooka 		strlcpy(fstypename, mountcompatnames[fsindex],
    272  1.331     pooka 		    sizeof(fstypename));
    273   1.31       cgd #else
    274  1.320       dsl 		return error;
    275   1.31       cgd #endif
    276   1.31       cgd 	}
    277  1.283      elad 
    278  1.285      elad #ifdef	COMPAT_10
    279  1.285      elad 	/* Accept `ufs' as an alias for `ffs'. */
    280  1.320       dsl 	if (strcmp(fstypename, "ufs") == 0)
    281  1.320       dsl 		fstypename[0] = 'f';
    282  1.285      elad #endif
    283  1.285      elad 
    284  1.320       dsl 	if ((*vfsops = vfs_getopsbyname(fstypename)) == NULL)
    285  1.320       dsl 		return ENODEV;
    286  1.320       dsl 	return 0;
    287  1.320       dsl }
    288  1.320       dsl 
    289  1.320       dsl static int
    290  1.320       dsl mount_domount(struct lwp *l, struct vnode **vpp, struct vfsops *vfsops,
    291  1.337        ad     const char *path, int flags, void *data, size_t *data_len, u_int recurse)
    292  1.320       dsl {
    293  1.320       dsl 	struct mount *mp = NULL;
    294  1.320       dsl 	struct vnode *vp = *vpp;
    295  1.320       dsl 	struct vattr va;
    296  1.320       dsl 	int error;
    297  1.320       dsl 
    298  1.320       dsl 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    299  1.320       dsl 	    KAUTH_REQ_SYSTEM_MOUNT_NEW, vp, KAUTH_ARG(flags), data);
    300  1.320       dsl 	if (error)
    301  1.320       dsl 		return error;
    302  1.320       dsl 
    303  1.320       dsl 	/* Can't make a non-dir a mount-point (from here anyway). */
    304  1.320       dsl 	if (vp->v_type != VDIR)
    305  1.320       dsl 		return ENOTDIR;
    306  1.320       dsl 
    307  1.320       dsl 	/*
    308  1.320       dsl 	 * If the user is not root, ensure that they own the directory
    309  1.320       dsl 	 * onto which we are attempting to mount.
    310  1.320       dsl 	 */
    311  1.332     pooka 	if ((error = VOP_GETATTR(vp, &va, l->l_cred)) != 0 ||
    312  1.320       dsl 	    (va.va_uid != kauth_cred_geteuid(l->l_cred) &&
    313  1.320       dsl 	    (error = kauth_authorize_generic(l->l_cred,
    314  1.320       dsl 	    KAUTH_GENERIC_ISSUSER, NULL)) != 0)) {
    315  1.320       dsl 		return error;
    316  1.283      elad 	}
    317  1.283      elad 
    318  1.320       dsl 	if (flags & MNT_EXPORTED)
    319  1.320       dsl 		return EINVAL;
    320  1.320       dsl 
    321  1.320       dsl 	if ((error = vinvalbuf(vp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
    322  1.320       dsl 		return error;
    323  1.320       dsl 
    324  1.283      elad 	/*
    325  1.283      elad 	 * Check if a file-system is not already mounted on this vnode.
    326  1.283      elad 	 */
    327  1.320       dsl 	if (vp->v_mountedhere != NULL)
    328  1.320       dsl 		return EBUSY;
    329  1.283      elad 
    330  1.345        ad 	mp = kmem_zalloc(sizeof(*mp), KM_SLEEP);
    331  1.345        ad 	if (mp == NULL)
    332  1.345        ad 		return ENOMEM;
    333  1.283      elad 
    334  1.320       dsl 	mp->mnt_op = vfsops;
    335  1.345        ad 	mp->mnt_refcnt = 1;
    336   1.31       cgd 
    337  1.296     pooka 	TAILQ_INIT(&mp->mnt_vnodelist);
    338  1.345        ad 	rw_init(&mp->mnt_lock);
    339  1.344  dholland  	mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
    340  1.353        ad 	(void)vfs_busy(mp, RW_WRITER);
    341  1.283      elad 
    342  1.129      fvdl 	mp->mnt_vnodecovered = vp;
    343  1.257        ad 	mp->mnt_stat.f_owner = kauth_cred_geteuid(l->l_cred);
    344  1.277   hannken 	mount_initspecific(mp);
    345  1.195   thorpej 
    346  1.195   thorpej 	/*
    347  1.195   thorpej 	 * The underlying file system may refuse the mount for
    348  1.198   thorpej 	 * various reasons.  Allow the user to force it to happen.
    349  1.286      yamt 	 *
    350  1.284      elad 	 * Set the mount level flags.
    351  1.284      elad 	 */
    352  1.286      yamt 	mp->mnt_flag = flags &
    353  1.286      yamt 	   (MNT_FORCE | MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    354  1.284      elad 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    355  1.284      elad 	    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
    356  1.287      yamt 	    MNT_IGNORE | MNT_RDONLY);
    357  1.284      elad 
    358  1.332     pooka 	error = VFS_MOUNT(mp, path, data, data_len);
    359  1.286      yamt 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    360  1.232      jmmv 
    361   1.31       cgd 	/*
    362   1.31       cgd 	 * Put the new filesystem on the mount list after root.
    363   1.31       cgd 	 */
    364   1.31       cgd 	cache_purge(vp);
    365  1.320       dsl 	if (error != 0) {
    366  1.283      elad 		vp->v_mountedhere = NULL;
    367  1.354        ad 		vfs_unbusy(mp, false, NULL);
    368  1.354        ad 		vfs_destroy(mp, false);
    369  1.320       dsl 		return error;
    370   1.31       cgd 	}
    371  1.283      elad 
    372  1.320       dsl 	mp->mnt_iflag &= ~IMNT_WANTRDWR;
    373  1.345        ad 	mutex_enter(&mountlist_lock);
    374  1.320       dsl 	vp->v_mountedhere = mp;
    375  1.320       dsl 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    376  1.354        ad 	mp->mnt_iflag |= IMNT_ONLIST;
    377  1.329        ad 	mutex_exit(&mountlist_lock);
    378  1.337        ad     	vn_restorerecurse(vp, recurse);
    379  1.320       dsl 	VOP_UNLOCK(vp, 0);
    380  1.320       dsl 	checkdirs(vp);
    381  1.320       dsl 	if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    382  1.320       dsl 		error = vfs_allocate_syncvnode(mp);
    383  1.347        ad 	/* Hold an additional reference to the mount across VFS_START(). */
    384  1.354        ad 	vfs_unbusy(mp, true, NULL);
    385  1.332     pooka 	(void) VFS_STATVFS(mp, &mp->mnt_stat);
    386  1.332     pooka 	error = VFS_START(mp, 0);
    387  1.345        ad 	if (error) {
    388  1.320       dsl 		vrele(vp);
    389  1.354        ad 		vfs_destroy(mp, false);
    390  1.345        ad 	}
    391  1.347        ad 	/* Drop reference held for VFS_START(). */
    392  1.354        ad 	vfs_destroy(mp, false);
    393  1.320       dsl 	*vpp = NULL;
    394  1.320       dsl 	return error;
    395  1.283      elad }
    396  1.283      elad 
    397  1.283      elad static int
    398  1.283      elad mount_getargs(struct lwp *l, struct vnode *vp, const char *path, int flags,
    399  1.324     pooka     void *data, size_t *data_len)
    400  1.283      elad {
    401  1.283      elad 	struct mount *mp;
    402  1.283      elad 	int error;
    403  1.283      elad 
    404  1.289      elad 	/* If MNT_GETARGS is specified, it should be the only flag. */
    405  1.320       dsl 	if (flags & ~MNT_GETARGS)
    406  1.320       dsl 		return EINVAL;
    407  1.289      elad 
    408  1.283      elad 	mp = vp->v_mount;
    409  1.283      elad 
    410  1.292      elad 	/* XXX: probably some notion of "can see" here if we want isolation. */
    411  1.292      elad 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    412  1.292      elad 	    KAUTH_REQ_SYSTEM_MOUNT_GET, mp, data, NULL);
    413  1.292      elad 	if (error)
    414  1.320       dsl 		return error;
    415  1.292      elad 
    416  1.329        ad 	if ((vp->v_vflag & VV_ROOT) == 0)
    417  1.320       dsl 		return EINVAL;
    418  1.283      elad 
    419  1.353        ad 	if (vfs_busy(mp, RW_WRITER))
    420  1.320       dsl 		return EPERM;
    421  1.283      elad 
    422  1.286      yamt 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    423  1.286      yamt 	mp->mnt_flag |= MNT_GETARGS;
    424  1.332     pooka 	error = VFS_MOUNT(mp, path, data, data_len);
    425  1.286      yamt 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    426  1.283      elad 
    427  1.354        ad 	vfs_unbusy(mp, false, NULL);
    428  1.283      elad 	return (error);
    429  1.283      elad }
    430  1.283      elad 
    431  1.321       dsl #ifdef COMPAT_40
    432  1.283      elad /* ARGSUSED */
    433  1.283      elad int
    434  1.335       dsl compat_40_sys_mount(struct lwp *l, const struct compat_40_sys_mount_args *uap, register_t *retval)
    435  1.283      elad {
    436  1.335       dsl 	/* {
    437  1.283      elad 		syscallarg(const char *) type;
    438  1.283      elad 		syscallarg(const char *) path;
    439  1.283      elad 		syscallarg(int) flags;
    440  1.283      elad 		syscallarg(void *) data;
    441  1.335       dsl 	} */
    442  1.320       dsl 	register_t dummy;
    443  1.320       dsl 
    444  1.320       dsl 	return do_sys_mount(l, NULL, SCARG(uap, type), SCARG(uap, path),
    445  1.320       dsl 	    SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE, 0, &dummy);
    446  1.320       dsl }
    447  1.321       dsl #endif
    448  1.321       dsl 
    449  1.321       dsl int
    450  1.335       dsl sys___mount50(struct lwp *l, const struct sys___mount50_args *uap, register_t *retval)
    451  1.321       dsl {
    452  1.335       dsl 	/* {
    453  1.321       dsl 		syscallarg(const char *) type;
    454  1.321       dsl 		syscallarg(const char *) path;
    455  1.321       dsl 		syscallarg(int) flags;
    456  1.321       dsl 		syscallarg(void *) data;
    457  1.321       dsl 		syscallarg(size_t) data_len;
    458  1.335       dsl 	} */
    459  1.321       dsl 
    460  1.321       dsl 	return do_sys_mount(l, NULL, SCARG(uap, type), SCARG(uap, path),
    461  1.321       dsl 	    SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE,
    462  1.321       dsl 	    SCARG(uap, data_len), retval);
    463  1.321       dsl }
    464  1.320       dsl 
    465  1.320       dsl int
    466  1.320       dsl do_sys_mount(struct lwp *l, struct vfsops *vfsops, const char *type,
    467  1.320       dsl     const char *path, int flags, void *data, enum uio_seg data_seg,
    468  1.320       dsl     size_t data_len, register_t *retval)
    469  1.320       dsl {
    470  1.283      elad 	struct vnode *vp;
    471  1.283      elad 	struct nameidata nd;
    472  1.320       dsl 	void *data_buf = data;
    473  1.337        ad 	u_int recurse;
    474  1.283      elad 	int error;
    475  1.283      elad 
    476  1.283      elad 	/*
    477  1.283      elad 	 * Get vnode to be covered
    478  1.283      elad 	 */
    479  1.334     pooka 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, path);
    480  1.283      elad 	if ((error = namei(&nd)) != 0)
    481  1.283      elad 		return (error);
    482  1.283      elad 	vp = nd.ni_vp;
    483  1.283      elad 
    484  1.283      elad 	/*
    485  1.283      elad 	 * A lookup in VFS_MOUNT might result in an attempt to
    486  1.283      elad 	 * lock this vnode again, so make the lock recursive.
    487  1.283      elad 	 */
    488  1.337        ad 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    489  1.337        ad 	recurse = vn_setrecurse(vp);
    490  1.320       dsl 
    491  1.320       dsl 	if (vfsops == NULL) {
    492  1.320       dsl 		if (flags & (MNT_GETARGS | MNT_UPDATE))
    493  1.320       dsl 			vfsops = vp->v_mount->mnt_op;
    494  1.320       dsl 		else {
    495  1.320       dsl 			/* 'type' is userspace */
    496  1.320       dsl 			error = mount_get_vfsops(type, &vfsops);
    497  1.320       dsl 			if (error != 0)
    498  1.320       dsl 				goto done;
    499  1.320       dsl 		}
    500  1.320       dsl 	}
    501  1.320       dsl 
    502  1.320       dsl 	if (data != NULL && data_seg == UIO_USERSPACE) {
    503  1.320       dsl 		if (data_len == 0) {
    504  1.320       dsl 			/* No length supplied, use default for filesystem */
    505  1.320       dsl 			data_len = vfsops->vfs_min_mount_data;
    506  1.320       dsl 			if (data_len > VFS_MAX_MOUNT_DATA) {
    507  1.320       dsl 				/* maybe a force loaded old LKM */
    508  1.320       dsl 				error = EINVAL;
    509  1.320       dsl 				goto done;
    510  1.320       dsl 			}
    511  1.320       dsl #ifdef COMPAT_30
    512  1.320       dsl 			/* Hopefully a longer buffer won't make copyin() fail */
    513  1.320       dsl 			if (flags & MNT_UPDATE
    514  1.320       dsl 			    && data_len < sizeof (struct mnt_export_args30))
    515  1.320       dsl 				data_len = sizeof (struct mnt_export_args30);
    516  1.320       dsl #endif
    517  1.320       dsl 		}
    518  1.320       dsl 		data_buf = malloc(data_len, M_TEMP, M_WAITOK);
    519  1.283      elad 
    520  1.320       dsl 		/* NFS needs the buffer even for mnt_getargs .... */
    521  1.320       dsl 		error = copyin(data, data_buf, data_len);
    522  1.320       dsl 		if (error != 0)
    523  1.320       dsl 			goto done;
    524  1.320       dsl 	}
    525  1.320       dsl 
    526  1.320       dsl 	if (flags & MNT_GETARGS) {
    527  1.320       dsl 		if (data_len == 0) {
    528  1.320       dsl 			error = EINVAL;
    529  1.320       dsl 			goto done;
    530  1.320       dsl 		}
    531  1.324     pooka 		error = mount_getargs(l, vp, path, flags, data_buf, &data_len);
    532  1.320       dsl 		if (error != 0)
    533  1.320       dsl 			goto done;
    534  1.320       dsl 		if (data_seg == UIO_USERSPACE)
    535  1.320       dsl 			error = copyout(data_buf, data, data_len);
    536  1.320       dsl 		*retval = data_len;
    537  1.320       dsl 	} else if (flags & MNT_UPDATE) {
    538  1.324     pooka 		error = mount_update(l, vp, path, flags, data_buf, &data_len);
    539  1.283      elad 	} else {
    540  1.283      elad 		/* Locking is handled internally in mount_domount(). */
    541  1.320       dsl 		error = mount_domount(l, &vp, vfsops, path, flags, data_buf,
    542  1.337        ad 		    &data_len, recurse);
    543  1.283      elad 	}
    544  1.283      elad 
    545  1.320       dsl     done:
    546  1.337        ad     	if (vp != NULL) {
    547  1.337        ad 	    	vn_restorerecurse(vp, recurse);
    548  1.337        ad 	    	vput(vp);
    549  1.337        ad 	}
    550  1.320       dsl 	if (data_buf != data)
    551  1.320       dsl 		free(data_buf, M_TEMP);
    552   1.31       cgd 	return (error);
    553   1.31       cgd }
    554   1.31       cgd 
    555   1.31       cgd /*
    556   1.43   mycroft  * Scan all active processes to see if any of them have a current
    557   1.43   mycroft  * or root directory onto which the new filesystem has just been
    558   1.43   mycroft  * mounted. If so, replace them with the new mount point.
    559   1.43   mycroft  */
    560   1.63  christos void
    561  1.221   thorpej checkdirs(struct vnode *olddp)
    562   1.43   mycroft {
    563  1.134   thorpej 	struct cwdinfo *cwdi;
    564  1.355        ad 	struct vnode *newdp, *rele1, *rele2;
    565   1.43   mycroft 	struct proc *p;
    566  1.355        ad 	bool retry;
    567   1.43   mycroft 
    568   1.43   mycroft 	if (olddp->v_usecount == 1)
    569   1.43   mycroft 		return;
    570  1.189   thorpej 	if (VFS_ROOT(olddp->v_mountedhere, &newdp))
    571   1.43   mycroft 		panic("mount: lost mount");
    572  1.355        ad 
    573  1.355        ad 	do {
    574  1.355        ad 		retry = false;
    575  1.355        ad 		mutex_enter(proc_lock);
    576  1.355        ad 		PROCLIST_FOREACH(p, &allproc) {
    577  1.355        ad 			if ((p->p_flag & PK_MARKER) != 0)
    578  1.355        ad 				continue;
    579  1.355        ad 			if ((cwdi = p->p_cwdi) == NULL)
    580  1.355        ad 				continue;
    581  1.355        ad 			/*
    582  1.355        ad 			 * Can't change to the old directory any more,
    583  1.355        ad 			 * so even if we see a stale value it's not a
    584  1.355        ad 			 * problem.
    585  1.355        ad 			 */
    586  1.355        ad 			if (cwdi->cwdi_cdir != olddp &&
    587  1.355        ad 			    cwdi->cwdi_rdir != olddp)
    588  1.355        ad 			    	continue;
    589  1.355        ad 			retry = true;
    590  1.355        ad 			rele1 = NULL;
    591  1.355        ad 			rele2 = NULL;
    592  1.355        ad 			atomic_inc_uint(&cwdi->cwdi_refcnt);
    593  1.355        ad 			mutex_exit(proc_lock);
    594  1.355        ad 			rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    595  1.355        ad 			if (cwdi->cwdi_cdir == olddp) {
    596  1.355        ad 				rele1 = cwdi->cwdi_cdir;
    597  1.355        ad 				VREF(newdp);
    598  1.355        ad 				cwdi->cwdi_cdir = newdp;
    599  1.355        ad 			}
    600  1.355        ad 			if (cwdi->cwdi_rdir == olddp) {
    601  1.355        ad 				rele2 = cwdi->cwdi_rdir;
    602  1.355        ad 				VREF(newdp);
    603  1.355        ad 				cwdi->cwdi_rdir = newdp;
    604  1.355        ad 			}
    605  1.355        ad 			rw_exit(&cwdi->cwdi_lock);
    606  1.355        ad 			cwdfree(cwdi);
    607  1.355        ad 			if (rele1 != NULL)
    608  1.355        ad 				vrele(rele1);
    609  1.355        ad 			if (rele2 != NULL)
    610  1.355        ad 				vrele(rele2);
    611  1.355        ad 			mutex_enter(proc_lock);
    612  1.355        ad 			break;
    613   1.43   mycroft 		}
    614  1.355        ad 		mutex_exit(proc_lock);
    615  1.355        ad 	} while (retry);
    616  1.355        ad 
    617   1.43   mycroft 	if (rootvnode == olddp) {
    618   1.43   mycroft 		vrele(rootvnode);
    619   1.43   mycroft 		VREF(newdp);
    620   1.43   mycroft 		rootvnode = newdp;
    621   1.43   mycroft 	}
    622   1.43   mycroft 	vput(newdp);
    623   1.43   mycroft }
    624   1.43   mycroft 
    625   1.43   mycroft /*
    626   1.31       cgd  * Unmount a file system.
    627   1.31       cgd  *
    628   1.31       cgd  * Note: unmount takes a path to the vnode mounted on as argument,
    629   1.31       cgd  * not special file (as before).
    630   1.31       cgd  */
    631   1.31       cgd /* ARGSUSED */
    632   1.63  christos int
    633  1.335       dsl sys_unmount(struct lwp *l, const struct sys_unmount_args *uap, register_t *retval)
    634   1.56   thorpej {
    635  1.335       dsl 	/* {
    636   1.74       cgd 		syscallarg(const char *) path;
    637   1.35       cgd 		syscallarg(int) flags;
    638  1.335       dsl 	} */
    639  1.155  augustss 	struct vnode *vp;
    640   1.31       cgd 	struct mount *mp;
    641   1.31       cgd 	int error;
    642   1.31       cgd 	struct nameidata nd;
    643   1.31       cgd 
    644  1.310       dsl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
    645  1.334     pooka 	    SCARG(uap, path));
    646   1.63  christos 	if ((error = namei(&nd)) != 0)
    647   1.31       cgd 		return (error);
    648   1.31       cgd 	vp = nd.ni_vp;
    649   1.43   mycroft 	mp = vp->v_mount;
    650  1.345        ad 	VOP_UNLOCK(vp, 0);
    651   1.31       cgd 
    652  1.295      elad 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    653  1.295      elad 	    KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT, mp, NULL, NULL);
    654  1.295      elad 	if (error) {
    655  1.345        ad 		vrele(vp);
    656   1.31       cgd 		return (error);
    657   1.31       cgd 	}
    658   1.31       cgd 
    659   1.31       cgd 	/*
    660   1.47   mycroft 	 * Don't allow unmounting the root file system.
    661   1.47   mycroft 	 */
    662   1.47   mycroft 	if (mp->mnt_flag & MNT_ROOTFS) {
    663  1.345        ad 		vrele(vp);
    664   1.47   mycroft 		return (EINVAL);
    665   1.47   mycroft 	}
    666   1.47   mycroft 
    667   1.47   mycroft 	/*
    668   1.31       cgd 	 * Must be the root of the filesystem
    669   1.31       cgd 	 */
    670  1.329        ad 	if ((vp->v_vflag & VV_ROOT) == 0) {
    671  1.345        ad 		vrele(vp);
    672   1.31       cgd 		return (EINVAL);
    673   1.31       cgd 	}
    674   1.78      fvdl 
    675  1.165   thorpej 	/*
    676  1.165   thorpej 	 * XXX Freeze syncer.  Must do this before locking the
    677  1.165   thorpej 	 * mount point.  See dounmount() for details.
    678  1.165   thorpej 	 */
    679  1.300        ad 	mutex_enter(&syncer_mutex);
    680  1.353        ad 	error = vfs_busy(mp, RW_WRITER);
    681  1.345        ad 	vrele(vp);
    682  1.345        ad 	if (error != 0) {
    683  1.300        ad 		mutex_exit(&syncer_mutex);
    684  1.345        ad 		return (error);
    685  1.165   thorpej 	}
    686   1.78      fvdl 
    687  1.234  christos 	return (dounmount(mp, SCARG(uap, flags), l));
    688   1.31       cgd }
    689   1.31       cgd 
    690   1.31       cgd /*
    691  1.353        ad  * Lock mount and keep additional reference across unmount.
    692  1.353        ad  */
    693  1.353        ad static void
    694  1.353        ad dounmount_lock(struct mount *mp)
    695  1.353        ad {
    696  1.353        ad 
    697  1.353        ad 	KASSERT(rw_write_held(&mp->mnt_lock));
    698  1.353        ad 	KASSERT(mp->mnt_unmounter == NULL);
    699  1.353        ad 
    700  1.353        ad 	mp->mnt_unmounter = curlwp;
    701  1.354        ad 	vfs_unbusy(mp, true, NULL);
    702  1.353        ad }
    703  1.353        ad 
    704  1.353        ad /*
    705  1.353        ad  * Unlock mount and drop additional reference.
    706  1.353        ad  */
    707  1.353        ad static void
    708  1.353        ad dounmount_unlock(struct mount *mp)
    709  1.353        ad {
    710  1.353        ad 
    711  1.354        ad 	KASSERT(mp->mnt_unmounter == curlwp);
    712  1.354        ad 
    713  1.353        ad 	mutex_enter(&mount_lock);
    714  1.353        ad 	mp->mnt_unmounter = NULL;
    715  1.353        ad 	cv_broadcast(&mount_cv);
    716  1.353        ad 	mutex_exit(&mount_lock);
    717  1.354        ad 	vfs_destroy(mp, false);
    718  1.353        ad }
    719  1.353        ad 
    720  1.353        ad /*
    721   1.78      fvdl  * Do the actual file system unmount. File system is assumed to have been
    722   1.78      fvdl  * marked busy by the caller.
    723   1.31       cgd  */
    724   1.63  christos int
    725  1.234  christos dounmount(struct mount *mp, int flags, struct lwp *l)
    726   1.31       cgd {
    727   1.31       cgd 	struct vnode *coveredvp;
    728   1.31       cgd 	int error;
    729  1.140  sommerfe 	int async;
    730  1.162      fvdl 	int used_syncer;
    731   1.31       cgd 
    732  1.345        ad 	KASSERT(rw_write_held(&mp->mnt_lock));
    733  1.345        ad 
    734  1.256      elad #if NVERIEXEC > 0
    735  1.279      elad 	error = veriexec_unmountchk(mp);
    736  1.279      elad 	if (error)
    737  1.279      elad 		return (error);
    738  1.256      elad #endif /* NVERIEXEC > 0 */
    739  1.251      elad 
    740  1.353        ad 	dounmount_lock(mp);
    741  1.162      fvdl 	used_syncer = (mp->mnt_syncer != NULL);
    742  1.162      fvdl 
    743  1.148      fvdl 	/*
    744  1.165   thorpej 	 * XXX Syncer must be frozen when we get here.  This should really
    745  1.165   thorpej 	 * be done on a per-mountpoint basis, but especially the softdep
    746  1.227      jmmv 	 * code possibly called from the syncer doesn't exactly work on a
    747  1.165   thorpej 	 * per-mountpoint basis, so the softdep code would become a maze
    748  1.165   thorpej 	 * of vfs_busy() calls.
    749  1.165   thorpej 	 *
    750  1.300        ad 	 * The caller of dounmount() must acquire syncer_mutex because
    751  1.300        ad 	 * the syncer itself acquires locks in syncer_mutex -> vfs_busy
    752  1.165   thorpej 	 * order, and we must preserve that order to avoid deadlock.
    753  1.165   thorpej 	 *
    754  1.165   thorpej 	 * So, if the file system did not use the syncer, now is
    755  1.300        ad 	 * the time to release the syncer_mutex.
    756  1.148      fvdl 	 */
    757  1.165   thorpej 	if (used_syncer == 0)
    758  1.300        ad 		mutex_exit(&syncer_mutex);
    759  1.148      fvdl 
    760  1.196       dbj 	mp->mnt_iflag |= IMNT_UNMOUNT;
    761  1.141  sommerfe 	async = mp->mnt_flag & MNT_ASYNC;
    762  1.151   mycroft 	mp->mnt_flag &= ~MNT_ASYNC;
    763   1.31       cgd 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    764  1.160   mycroft 	if (mp->mnt_syncer != NULL)
    765  1.160   mycroft 		vfs_deallocate_syncvnode(mp);
    766  1.209   hannken 	error = 0;
    767  1.209   hannken 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    768  1.202   hannken #if NFSS > 0
    769  1.209   hannken 		error = fss_umount_hook(mp, (flags & MNT_FORCE));
    770  1.202   hannken #endif
    771  1.209   hannken 		if (error == 0)
    772  1.332     pooka 			error = VFS_SYNC(mp, MNT_WAIT, l->l_cred);
    773  1.209   hannken 	}
    774  1.342        ad 	vfs_scrubvnlist(mp);
    775  1.209   hannken 	if (error == 0 || (flags & MNT_FORCE))
    776  1.332     pooka 		error = VFS_UNMOUNT(mp, flags);
    777   1.31       cgd 	if (error) {
    778  1.151   mycroft 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    779  1.148      fvdl 			(void) vfs_allocate_syncvnode(mp);
    780  1.196       dbj 		mp->mnt_iflag &= ~IMNT_UNMOUNT;
    781  1.140  sommerfe 		mp->mnt_flag |= async;
    782  1.162      fvdl 		if (used_syncer)
    783  1.300        ad 			mutex_exit(&syncer_mutex);
    784  1.353        ad 		dounmount_unlock(mp);
    785  1.142  sommerfe 		return (error);
    786  1.113      fvdl 	}
    787  1.338        ad 	vfs_scrubvnlist(mp);
    788  1.297   hannken 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP)
    789  1.113      fvdl 		coveredvp->v_mountedhere = NULL;
    790  1.273   reinoud 	if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL)
    791  1.113      fvdl 		panic("unmount: dangling vnode");
    792  1.196       dbj 	mp->mnt_iflag |= IMNT_GONE;
    793  1.162      fvdl 	if (used_syncer)
    794  1.300        ad 		mutex_exit(&syncer_mutex);
    795  1.231      jmmv 	vfs_hooks_unmount(mp);
    796  1.353        ad 	dounmount_unlock(mp);
    797  1.354        ad 	vfs_destroy(mp, false);
    798  1.345        ad 	if (coveredvp != NULLVP)
    799  1.345        ad 		vrele(coveredvp);
    800  1.113      fvdl 	return (0);
    801   1.31       cgd }
    802   1.31       cgd 
    803   1.31       cgd /*
    804   1.31       cgd  * Sync each mounted filesystem.
    805   1.31       cgd  */
    806   1.31       cgd #ifdef DEBUG
    807   1.31       cgd int syncprt = 0;
    808   1.31       cgd struct ctldebug debug0 = { "syncprt", &syncprt };
    809   1.31       cgd #endif
    810   1.31       cgd 
    811   1.31       cgd /* ARGSUSED */
    812   1.63  christos int
    813  1.335       dsl sys_sync(struct lwp *l, const void *v, register_t *retval)
    814   1.31       cgd {
    815  1.155  augustss 	struct mount *mp, *nmp;
    816   1.31       cgd 	int asyncflag;
    817  1.257        ad 
    818  1.257        ad 	if (l == NULL)
    819  1.257        ad 		l = &lwp0;
    820   1.31       cgd 
    821  1.329        ad 	mutex_enter(&mountlist_lock);
    822  1.353        ad 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    823  1.353        ad 	     mp = nmp) {
    824  1.356        ad 		if (vfs_trybusy(mp, RW_WRITER, &nmp)) {
    825  1.113      fvdl 			continue;
    826  1.113      fvdl 		}
    827  1.307   hannken 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    828   1.31       cgd 			asyncflag = mp->mnt_flag & MNT_ASYNC;
    829   1.31       cgd 			mp->mnt_flag &= ~MNT_ASYNC;
    830  1.332     pooka 			VFS_SYNC(mp, MNT_NOWAIT, l->l_cred);
    831   1.31       cgd 			if (asyncflag)
    832  1.113      fvdl 				 mp->mnt_flag |= MNT_ASYNC;
    833   1.31       cgd 		}
    834  1.329        ad 		mutex_enter(&mountlist_lock);
    835  1.354        ad 		vfs_unbusy(mp, false, &nmp);
    836  1.205  junyoung 
    837   1.31       cgd 	}
    838  1.329        ad 	mutex_exit(&mountlist_lock);
    839   1.31       cgd #ifdef DEBUG
    840   1.31       cgd 	if (syncprt)
    841   1.31       cgd 		vfs_bufstats();
    842   1.31       cgd #endif /* DEBUG */
    843   1.31       cgd 	return (0);
    844   1.31       cgd }
    845   1.31       cgd 
    846   1.31       cgd /*
    847   1.31       cgd  * Change filesystem quotas.
    848   1.31       cgd  */
    849   1.31       cgd /* ARGSUSED */
    850   1.63  christos int
    851  1.335       dsl sys_quotactl(struct lwp *l, const struct sys_quotactl_args *uap, register_t *retval)
    852   1.56   thorpej {
    853  1.335       dsl 	/* {
    854   1.74       cgd 		syscallarg(const char *) path;
    855   1.35       cgd 		syscallarg(int) cmd;
    856   1.35       cgd 		syscallarg(int) uid;
    857  1.272  christos 		syscallarg(void *) arg;
    858  1.335       dsl 	} */
    859  1.155  augustss 	struct mount *mp;
    860   1.31       cgd 	int error;
    861   1.31       cgd 	struct nameidata nd;
    862   1.31       cgd 
    863  1.326     pooka 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
    864  1.334     pooka 	    SCARG(uap, path));
    865   1.63  christos 	if ((error = namei(&nd)) != 0)
    866   1.31       cgd 		return (error);
    867  1.307   hannken 	mp = nd.ni_vp->v_mount;
    868  1.197   hannken 	error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
    869  1.332     pooka 	    SCARG(uap, arg));
    870  1.326     pooka 	vrele(nd.ni_vp);
    871  1.197   hannken 	return (error);
    872   1.31       cgd }
    873   1.31       cgd 
    874  1.206  christos int
    875  1.234  christos dostatvfs(struct mount *mp, struct statvfs *sp, struct lwp *l, int flags,
    876  1.185  christos     int root)
    877  1.185  christos {
    878  1.234  christos 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    879  1.185  christos 	int error = 0;
    880  1.185  christos 
    881  1.185  christos 	/*
    882  1.185  christos 	 * If MNT_NOWAIT or MNT_LAZY is specified, do not
    883  1.204       dbj 	 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY
    884  1.185  christos 	 * overrides MNT_NOWAIT.
    885  1.185  christos 	 */
    886  1.185  christos 	if (flags == MNT_NOWAIT	|| flags == MNT_LAZY ||
    887  1.185  christos 	    (flags != MNT_WAIT && flags != 0)) {
    888  1.185  christos 		memcpy(sp, &mp->mnt_stat, sizeof(*sp));
    889  1.185  christos 		goto done;
    890  1.185  christos 	}
    891  1.205  junyoung 
    892  1.211  jdolecek 	/* Get the filesystem stats now */
    893  1.211  jdolecek 	memset(sp, 0, sizeof(*sp));
    894  1.332     pooka 	if ((error = VFS_STATVFS(mp, sp)) != 0) {
    895  1.185  christos 		return error;
    896  1.185  christos 	}
    897  1.185  christos 
    898  1.185  christos 	if (cwdi->cwdi_rdir == NULL)
    899  1.185  christos 		(void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat));
    900  1.185  christos done:
    901  1.185  christos 	if (cwdi->cwdi_rdir != NULL) {
    902  1.185  christos 		size_t len;
    903  1.185  christos 		char *bp;
    904  1.236      yamt 		char *path = PNBUF_GET();
    905  1.185  christos 
    906  1.185  christos 		bp = path + MAXPATHLEN;
    907  1.185  christos 		*--bp = '\0';
    908  1.328        ad 		rw_enter(&cwdi->cwdi_lock, RW_READER);
    909  1.185  christos 		error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
    910  1.234  christos 		    MAXPATHLEN / 2, 0, l);
    911  1.328        ad 		rw_exit(&cwdi->cwdi_lock);
    912  1.185  christos 		if (error) {
    913  1.236      yamt 			PNBUF_PUT(path);
    914  1.185  christos 			return error;
    915  1.185  christos 		}
    916  1.185  christos 		len = strlen(bp);
    917  1.185  christos 		/*
    918  1.185  christos 		 * for mount points that are below our root, we can see
    919  1.185  christos 		 * them, so we fix up the pathname and return them. The
    920  1.185  christos 		 * rest we cannot see, so we don't allow viewing the
    921  1.185  christos 		 * data.
    922  1.185  christos 		 */
    923  1.185  christos 		if (strncmp(bp, sp->f_mntonname, len) == 0) {
    924  1.187    itojun 			strlcpy(sp->f_mntonname, &sp->f_mntonname[len],
    925  1.187    itojun 			    sizeof(sp->f_mntonname));
    926  1.185  christos 			if (sp->f_mntonname[0] == '\0')
    927  1.187    itojun 				(void)strlcpy(sp->f_mntonname, "/",
    928  1.187    itojun 				    sizeof(sp->f_mntonname));
    929  1.185  christos 		} else {
    930  1.185  christos 			if (root)
    931  1.187    itojun 				(void)strlcpy(sp->f_mntonname, "/",
    932  1.187    itojun 				    sizeof(sp->f_mntonname));
    933  1.185  christos 			else
    934  1.185  christos 				error = EPERM;
    935  1.185  christos 		}
    936  1.236      yamt 		PNBUF_PUT(path);
    937  1.185  christos 	}
    938  1.206  christos 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    939  1.185  christos 	return error;
    940  1.185  christos }
    941  1.185  christos 
    942   1.31       cgd /*
    943  1.311       dsl  * Get filesystem statistics by path.
    944   1.31       cgd  */
    945  1.311       dsl int
    946  1.311       dsl do_sys_pstatvfs(struct lwp *l, const char *path, int flags, struct statvfs *sb)
    947  1.311       dsl {
    948  1.311       dsl 	struct mount *mp;
    949  1.311       dsl 	int error;
    950  1.311       dsl 	struct nameidata nd;
    951  1.311       dsl 
    952  1.334     pooka 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, path);
    953  1.311       dsl 	if ((error = namei(&nd)) != 0)
    954  1.311       dsl 		return error;
    955  1.311       dsl 	mp = nd.ni_vp->v_mount;
    956  1.311       dsl 	error = dostatvfs(mp, sb, l, flags, 1);
    957  1.311       dsl 	vrele(nd.ni_vp);
    958  1.311       dsl 	return error;
    959  1.311       dsl }
    960  1.311       dsl 
    961   1.31       cgd /* ARGSUSED */
    962   1.63  christos int
    963  1.335       dsl sys_statvfs1(struct lwp *l, const struct sys_statvfs1_args *uap, register_t *retval)
    964   1.56   thorpej {
    965  1.335       dsl 	/* {
    966   1.74       cgd 		syscallarg(const char *) path;
    967  1.206  christos 		syscallarg(struct statvfs *) buf;
    968  1.206  christos 		syscallarg(int) flags;
    969  1.335       dsl 	} */
    970  1.241      yamt 	struct statvfs *sb;
    971   1.31       cgd 	int error;
    972   1.31       cgd 
    973  1.241      yamt 	sb = STATVFSBUF_GET();
    974  1.311       dsl 	error = do_sys_pstatvfs(l, SCARG(uap, path), SCARG(uap, flags), sb);
    975  1.311       dsl 	if (error == 0)
    976  1.241      yamt 		error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
    977  1.241      yamt 	STATVFSBUF_PUT(sb);
    978  1.241      yamt 	return error;
    979   1.31       cgd }
    980   1.31       cgd 
    981   1.31       cgd /*
    982  1.311       dsl  * Get filesystem statistics by fd.
    983   1.31       cgd  */
    984  1.311       dsl int
    985  1.311       dsl do_sys_fstatvfs(struct lwp *l, int fd, int flags, struct statvfs *sb)
    986  1.311       dsl {
    987  1.346        ad 	file_t *fp;
    988  1.311       dsl 	struct mount *mp;
    989  1.311       dsl 	int error;
    990  1.311       dsl 
    991  1.346        ad 	/* fd_getvnode() will use the descriptor for us */
    992  1.346        ad 	if ((error = fd_getvnode(fd, &fp)) != 0)
    993  1.311       dsl 		return (error);
    994  1.311       dsl 	mp = ((struct vnode *)fp->f_data)->v_mount;
    995  1.346        ad 	error = dostatvfs(mp, sb, curlwp, flags, 1);
    996  1.346        ad 	fd_putfile(fd);
    997  1.311       dsl 	return error;
    998  1.311       dsl }
    999  1.311       dsl 
   1000   1.31       cgd /* ARGSUSED */
   1001   1.63  christos int
   1002  1.335       dsl sys_fstatvfs1(struct lwp *l, const struct sys_fstatvfs1_args *uap, register_t *retval)
   1003   1.56   thorpej {
   1004  1.335       dsl 	/* {
   1005   1.35       cgd 		syscallarg(int) fd;
   1006  1.206  christos 		syscallarg(struct statvfs *) buf;
   1007  1.206  christos 		syscallarg(int) flags;
   1008  1.335       dsl 	} */
   1009  1.241      yamt 	struct statvfs *sb;
   1010   1.31       cgd 	int error;
   1011   1.31       cgd 
   1012  1.241      yamt 	sb = STATVFSBUF_GET();
   1013  1.311       dsl 	error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
   1014  1.316       dsl 	if (error == 0)
   1015  1.311       dsl 		error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
   1016  1.241      yamt 	STATVFSBUF_PUT(sb);
   1017  1.185  christos 	return error;
   1018   1.31       cgd }
   1019   1.31       cgd 
   1020  1.185  christos 
   1021   1.31       cgd /*
   1022   1.31       cgd  * Get statistics on all filesystems.
   1023   1.31       cgd  */
   1024   1.63  christos int
   1025  1.311       dsl do_sys_getvfsstat(struct lwp *l, void *sfsp, size_t bufsize, int flags,
   1026  1.311       dsl     int (*copyfn)(const void *, void *, size_t), size_t entry_sz,
   1027  1.311       dsl     register_t *retval)
   1028   1.56   thorpej {
   1029  1.185  christos 	int root = 0;
   1030  1.179   thorpej 	struct proc *p = l->l_proc;
   1031  1.155  augustss 	struct mount *mp, *nmp;
   1032  1.241      yamt 	struct statvfs *sb;
   1033  1.206  christos 	size_t count, maxcount;
   1034  1.206  christos 	int error = 0;
   1035   1.31       cgd 
   1036  1.241      yamt 	sb = STATVFSBUF_GET();
   1037  1.311       dsl 	maxcount = bufsize / entry_sz;
   1038  1.329        ad 	mutex_enter(&mountlist_lock);
   1039  1.113      fvdl 	count = 0;
   1040  1.176      matt 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
   1041  1.176      matt 	     mp = nmp) {
   1042  1.353        ad 		if (vfs_trybusy(mp, RW_READER, &nmp)) {
   1043  1.113      fvdl 			continue;
   1044  1.113      fvdl 		}
   1045  1.113      fvdl 		if (sfsp && count < maxcount) {
   1046  1.311       dsl 			error = dostatvfs(mp, sb, l, flags, 0);
   1047  1.185  christos 			if (error) {
   1048  1.329        ad 				mutex_enter(&mountlist_lock);
   1049  1.354        ad 				vfs_unbusy(mp, false, &nmp);
   1050   1.31       cgd 				continue;
   1051  1.113      fvdl 			}
   1052  1.311       dsl 			error = copyfn(sb, sfsp, entry_sz);
   1053  1.133   mycroft 			if (error) {
   1054  1.354        ad 				vfs_unbusy(mp, false, NULL);
   1055  1.241      yamt 				goto out;
   1056  1.133   mycroft 			}
   1057  1.311       dsl 			sfsp = (char *)sfsp + entry_sz;
   1058  1.241      yamt 			root |= strcmp(sb->f_mntonname, "/") == 0;
   1059   1.31       cgd 		}
   1060   1.31       cgd 		count++;
   1061  1.329        ad 		mutex_enter(&mountlist_lock);
   1062  1.354        ad 		vfs_unbusy(mp, false, &nmp);
   1063   1.31       cgd 	}
   1064  1.331     pooka 
   1065  1.329        ad 	mutex_exit(&mountlist_lock);
   1066  1.185  christos 	if (root == 0 && p->p_cwdi->cwdi_rdir) {
   1067  1.185  christos 		/*
   1068  1.185  christos 		 * fake a root entry
   1069  1.185  christos 		 */
   1070  1.331     pooka 		error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount,
   1071  1.331     pooka 		    sb, l, flags, 1);
   1072  1.311       dsl 		if (error != 0)
   1073  1.241      yamt 			goto out;
   1074  1.185  christos 		if (sfsp)
   1075  1.311       dsl 			error = copyfn(sb, sfsp, entry_sz);
   1076  1.185  christos 		count++;
   1077  1.185  christos 	}
   1078   1.31       cgd 	if (sfsp && count > maxcount)
   1079   1.31       cgd 		*retval = maxcount;
   1080   1.31       cgd 	else
   1081   1.31       cgd 		*retval = count;
   1082  1.241      yamt out:
   1083  1.241      yamt 	STATVFSBUF_PUT(sb);
   1084  1.185  christos 	return error;
   1085   1.31       cgd }
   1086   1.31       cgd 
   1087  1.311       dsl int
   1088  1.335       dsl sys_getvfsstat(struct lwp *l, const struct sys_getvfsstat_args *uap, register_t *retval)
   1089  1.311       dsl {
   1090  1.335       dsl 	/* {
   1091  1.311       dsl 		syscallarg(struct statvfs *) buf;
   1092  1.311       dsl 		syscallarg(size_t) bufsize;
   1093  1.311       dsl 		syscallarg(int) flags;
   1094  1.335       dsl 	} */
   1095  1.311       dsl 
   1096  1.311       dsl 	return do_sys_getvfsstat(l, SCARG(uap, buf), SCARG(uap, bufsize),
   1097  1.311       dsl 	    SCARG(uap, flags), copyout, sizeof (struct statvfs), retval);
   1098  1.311       dsl }
   1099  1.311       dsl 
   1100   1.31       cgd /*
   1101   1.31       cgd  * Change current working directory to a given file descriptor.
   1102   1.31       cgd  */
   1103   1.31       cgd /* ARGSUSED */
   1104   1.63  christos int
   1105  1.335       dsl sys_fchdir(struct lwp *l, const struct sys_fchdir_args *uap, register_t *retval)
   1106   1.56   thorpej {
   1107  1.335       dsl 	/* {
   1108   1.35       cgd 		syscallarg(int) fd;
   1109  1.335       dsl 	} */
   1110  1.179   thorpej 	struct proc *p = l->l_proc;
   1111  1.328        ad 	struct cwdinfo *cwdi;
   1112   1.43   mycroft 	struct vnode *vp, *tdp;
   1113   1.43   mycroft 	struct mount *mp;
   1114  1.346        ad 	file_t *fp;
   1115  1.346        ad 	int error, fd;
   1116   1.31       cgd 
   1117  1.346        ad 	/* fd_getvnode() will use the descriptor for us */
   1118  1.346        ad 	fd = SCARG(uap, fd);
   1119  1.346        ad 	if ((error = fd_getvnode(fd, &fp)) != 0)
   1120   1.31       cgd 		return (error);
   1121  1.346        ad 	vp = fp->f_data;
   1122  1.131  sommerfe 
   1123   1.43   mycroft 	VREF(vp);
   1124  1.113      fvdl 	vn_lock(vp,  LK_EXCLUSIVE | LK_RETRY);
   1125   1.31       cgd 	if (vp->v_type != VDIR)
   1126   1.31       cgd 		error = ENOTDIR;
   1127   1.31       cgd 	else
   1128  1.332     pooka 		error = VOP_ACCESS(vp, VEXEC, l->l_cred);
   1129  1.303     pooka 	if (error) {
   1130  1.303     pooka 		vput(vp);
   1131  1.303     pooka 		goto out;
   1132  1.303     pooka 	}
   1133  1.304     pooka 	while ((mp = vp->v_mountedhere) != NULL) {
   1134  1.356        ad 		error = vfs_trybusy(mp, RW_READER, NULL);
   1135  1.298       chs 		vput(vp);
   1136  1.356        ad 		if (error != 0) {
   1137  1.356        ad 			goto out;
   1138  1.189   thorpej 		error = VFS_ROOT(mp, &tdp);
   1139  1.354        ad 		vfs_unbusy(mp, false, NULL);
   1140  1.113      fvdl 		if (error)
   1141  1.303     pooka 			goto out;
   1142   1.43   mycroft 		vp = tdp;
   1143   1.43   mycroft 	}
   1144  1.113      fvdl 	VOP_UNLOCK(vp, 0);
   1145  1.131  sommerfe 
   1146  1.131  sommerfe 	/*
   1147  1.131  sommerfe 	 * Disallow changing to a directory not under the process's
   1148  1.131  sommerfe 	 * current root directory (if there is one).
   1149  1.131  sommerfe 	 */
   1150  1.328        ad 	cwdi = p->p_cwdi;
   1151  1.328        ad 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   1152  1.234  christos 	if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, l)) {
   1153  1.131  sommerfe 		vrele(vp);
   1154  1.135   thorpej 		error = EPERM;	/* operation not permitted */
   1155  1.328        ad 	} else {
   1156  1.328        ad 		vrele(cwdi->cwdi_cdir);
   1157  1.328        ad 		cwdi->cwdi_cdir = vp;
   1158  1.131  sommerfe 	}
   1159  1.328        ad 	rw_exit(&cwdi->cwdi_lock);
   1160  1.205  junyoung 
   1161  1.135   thorpej  out:
   1162  1.346        ad 	fd_putfile(fd);
   1163  1.135   thorpej 	return (error);
   1164   1.31       cgd }
   1165   1.31       cgd 
   1166   1.31       cgd /*
   1167  1.221   thorpej  * Change this process's notion of the root directory to a given file
   1168  1.221   thorpej  * descriptor.
   1169  1.131  sommerfe  */
   1170  1.131  sommerfe int
   1171  1.335       dsl sys_fchroot(struct lwp *l, const struct sys_fchroot_args *uap, register_t *retval)
   1172  1.131  sommerfe {
   1173  1.179   thorpej 	struct proc *p = l->l_proc;
   1174  1.328        ad 	struct cwdinfo *cwdi;
   1175  1.131  sommerfe 	struct vnode	*vp;
   1176  1.346        ad 	file_t	*fp;
   1177  1.346        ad 	int		 error, fd = SCARG(uap, fd);
   1178  1.131  sommerfe 
   1179  1.268      elad 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
   1180  1.268      elad  	    KAUTH_REQ_SYSTEM_CHROOT_FCHROOT, NULL, NULL, NULL)) != 0)
   1181  1.131  sommerfe 		return error;
   1182  1.346        ad 	/* fd_getvnode() will use the descriptor for us */
   1183  1.346        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   1184  1.131  sommerfe 		return error;
   1185  1.346        ad 	vp = fp->f_data;
   1186  1.131  sommerfe 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1187  1.131  sommerfe 	if (vp->v_type != VDIR)
   1188  1.131  sommerfe 		error = ENOTDIR;
   1189  1.131  sommerfe 	else
   1190  1.332     pooka 		error = VOP_ACCESS(vp, VEXEC, l->l_cred);
   1191  1.131  sommerfe 	VOP_UNLOCK(vp, 0);
   1192  1.131  sommerfe 	if (error)
   1193  1.135   thorpej 		goto out;
   1194  1.131  sommerfe 	VREF(vp);
   1195  1.131  sommerfe 
   1196  1.131  sommerfe 	/*
   1197  1.131  sommerfe 	 * Prevent escaping from chroot by putting the root under
   1198  1.131  sommerfe 	 * the working directory.  Silently chdir to / if we aren't
   1199  1.131  sommerfe 	 * already there.
   1200  1.131  sommerfe 	 */
   1201  1.328        ad 	cwdi = p->p_cwdi;
   1202  1.328        ad 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   1203  1.234  christos 	if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
   1204  1.131  sommerfe 		/*
   1205  1.131  sommerfe 		 * XXX would be more failsafe to change directory to a
   1206  1.131  sommerfe 		 * deadfs node here instead
   1207  1.131  sommerfe 		 */
   1208  1.134   thorpej 		vrele(cwdi->cwdi_cdir);
   1209  1.131  sommerfe 		VREF(vp);
   1210  1.134   thorpej 		cwdi->cwdi_cdir = vp;
   1211  1.131  sommerfe 	}
   1212  1.205  junyoung 
   1213  1.134   thorpej 	if (cwdi->cwdi_rdir != NULL)
   1214  1.134   thorpej 		vrele(cwdi->cwdi_rdir);
   1215  1.134   thorpej 	cwdi->cwdi_rdir = vp;
   1216  1.328        ad 	rw_exit(&cwdi->cwdi_lock);
   1217  1.328        ad 
   1218  1.135   thorpej  out:
   1219  1.346        ad 	fd_putfile(fd);
   1220  1.135   thorpej 	return (error);
   1221  1.131  sommerfe }
   1222  1.131  sommerfe 
   1223  1.131  sommerfe /*
   1224   1.31       cgd  * Change current working directory (``.'').
   1225   1.31       cgd  */
   1226   1.31       cgd /* ARGSUSED */
   1227   1.63  christos int
   1228  1.335       dsl sys_chdir(struct lwp *l, const struct sys_chdir_args *uap, register_t *retval)
   1229   1.56   thorpej {
   1230  1.335       dsl 	/* {
   1231   1.74       cgd 		syscallarg(const char *) path;
   1232  1.335       dsl 	} */
   1233  1.179   thorpej 	struct proc *p = l->l_proc;
   1234  1.328        ad 	struct cwdinfo *cwdi;
   1235   1.31       cgd 	int error;
   1236   1.31       cgd 	struct nameidata nd;
   1237   1.31       cgd 
   1238  1.310       dsl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
   1239  1.334     pooka 	    SCARG(uap, path));
   1240  1.234  christos 	if ((error = change_dir(&nd, l)) != 0)
   1241   1.31       cgd 		return (error);
   1242  1.328        ad 	cwdi = p->p_cwdi;
   1243  1.328        ad 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   1244  1.134   thorpej 	vrele(cwdi->cwdi_cdir);
   1245  1.134   thorpej 	cwdi->cwdi_cdir = nd.ni_vp;
   1246  1.328        ad 	rw_exit(&cwdi->cwdi_lock);
   1247   1.31       cgd 	return (0);
   1248   1.31       cgd }
   1249   1.31       cgd 
   1250   1.31       cgd /*
   1251   1.31       cgd  * Change notion of root (``/'') directory.
   1252   1.31       cgd  */
   1253   1.31       cgd /* ARGSUSED */
   1254   1.63  christos int
   1255  1.335       dsl sys_chroot(struct lwp *l, const struct sys_chroot_args *uap, register_t *retval)
   1256   1.56   thorpej {
   1257  1.335       dsl 	/* {
   1258   1.74       cgd 		syscallarg(const char *) path;
   1259  1.335       dsl 	} */
   1260  1.179   thorpej 	struct proc *p = l->l_proc;
   1261  1.328        ad 	struct cwdinfo *cwdi;
   1262  1.131  sommerfe 	struct vnode *vp;
   1263   1.31       cgd 	int error;
   1264   1.31       cgd 	struct nameidata nd;
   1265   1.31       cgd 
   1266  1.268      elad 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
   1267  1.268      elad 	    KAUTH_REQ_SYSTEM_CHROOT_CHROOT, NULL, NULL, NULL)) != 0)
   1268   1.31       cgd 		return (error);
   1269  1.310       dsl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
   1270  1.334     pooka 	    SCARG(uap, path));
   1271  1.234  christos 	if ((error = change_dir(&nd, l)) != 0)
   1272   1.31       cgd 		return (error);
   1273  1.328        ad 
   1274  1.328        ad 	cwdi = p->p_cwdi;
   1275  1.328        ad 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   1276  1.134   thorpej 	if (cwdi->cwdi_rdir != NULL)
   1277  1.134   thorpej 		vrele(cwdi->cwdi_rdir);
   1278  1.131  sommerfe 	vp = nd.ni_vp;
   1279  1.134   thorpej 	cwdi->cwdi_rdir = vp;
   1280  1.131  sommerfe 
   1281  1.131  sommerfe 	/*
   1282  1.131  sommerfe 	 * Prevent escaping from chroot by putting the root under
   1283  1.131  sommerfe 	 * the working directory.  Silently chdir to / if we aren't
   1284  1.131  sommerfe 	 * already there.
   1285  1.131  sommerfe 	 */
   1286  1.234  christos 	if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
   1287  1.131  sommerfe 		/*
   1288  1.131  sommerfe 		 * XXX would be more failsafe to change directory to a
   1289  1.131  sommerfe 		 * deadfs node here instead
   1290  1.131  sommerfe 		 */
   1291  1.134   thorpej 		vrele(cwdi->cwdi_cdir);
   1292  1.131  sommerfe 		VREF(vp);
   1293  1.134   thorpej 		cwdi->cwdi_cdir = vp;
   1294  1.131  sommerfe 	}
   1295  1.328        ad 	rw_exit(&cwdi->cwdi_lock);
   1296  1.205  junyoung 
   1297   1.31       cgd 	return (0);
   1298   1.31       cgd }
   1299   1.31       cgd 
   1300   1.31       cgd /*
   1301   1.31       cgd  * Common routine for chroot and chdir.
   1302   1.31       cgd  */
   1303   1.31       cgd static int
   1304  1.234  christos change_dir(struct nameidata *ndp, struct lwp *l)
   1305   1.31       cgd {
   1306   1.31       cgd 	struct vnode *vp;
   1307   1.31       cgd 	int error;
   1308   1.31       cgd 
   1309   1.63  christos 	if ((error = namei(ndp)) != 0)
   1310   1.31       cgd 		return (error);
   1311   1.31       cgd 	vp = ndp->ni_vp;
   1312   1.31       cgd 	if (vp->v_type != VDIR)
   1313   1.31       cgd 		error = ENOTDIR;
   1314   1.31       cgd 	else
   1315  1.332     pooka 		error = VOP_ACCESS(vp, VEXEC, l->l_cred);
   1316  1.205  junyoung 
   1317   1.31       cgd 	if (error)
   1318  1.113      fvdl 		vput(vp);
   1319  1.113      fvdl 	else
   1320  1.113      fvdl 		VOP_UNLOCK(vp, 0);
   1321   1.31       cgd 	return (error);
   1322   1.31       cgd }
   1323   1.31       cgd 
   1324   1.31       cgd /*
   1325   1.31       cgd  * Check permissions, allocate an open file structure,
   1326   1.31       cgd  * and call the device open routine if any.
   1327   1.31       cgd  */
   1328   1.63  christos int
   1329  1.335       dsl sys_open(struct lwp *l, const struct sys_open_args *uap, register_t *retval)
   1330   1.56   thorpej {
   1331  1.335       dsl 	/* {
   1332   1.74       cgd 		syscallarg(const char *) path;
   1333   1.35       cgd 		syscallarg(int) flags;
   1334   1.35       cgd 		syscallarg(int) mode;
   1335  1.335       dsl 	} */
   1336  1.179   thorpej 	struct proc *p = l->l_proc;
   1337  1.134   thorpej 	struct cwdinfo *cwdi = p->p_cwdi;
   1338  1.346        ad 	file_t *fp;
   1339  1.135   thorpej 	struct vnode *vp;
   1340   1.31       cgd 	int flags, cmode;
   1341   1.31       cgd 	int type, indx, error;
   1342   1.31       cgd 	struct flock lf;
   1343   1.31       cgd 	struct nameidata nd;
   1344   1.31       cgd 
   1345  1.104   mycroft 	flags = FFLAGS(SCARG(uap, flags));
   1346  1.104   mycroft 	if ((flags & (FREAD | FWRITE)) == 0)
   1347  1.104   mycroft 		return (EINVAL);
   1348  1.346        ad 	if ((error = fd_allocfile(&fp, &indx)) != 0)
   1349   1.31       cgd 		return (error);
   1350  1.328        ad 	/* We're going to read cwdi->cwdi_cmask unlocked here. */
   1351  1.134   thorpej 	cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
   1352  1.331     pooka 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
   1353  1.334     pooka 	    SCARG(uap, path));
   1354  1.194  jdolecek 	l->l_dupfd = -indx - 1;			/* XXX check for fdopen */
   1355   1.63  christos 	if ((error = vn_open(&nd, flags, cmode)) != 0) {
   1356  1.346        ad 		fd_abort(p, fp, indx);
   1357  1.213  christos 		if ((error == EDUPFD || error == EMOVEFD) &&
   1358  1.194  jdolecek 		    l->l_dupfd >= 0 &&			/* XXX from fdopen */
   1359   1.45   mycroft 		    (error =
   1360  1.346        ad 			fd_dupopen(l->l_dupfd, &indx, flags, error)) == 0) {
   1361   1.45   mycroft 			*retval = indx;
   1362   1.45   mycroft 			return (0);
   1363   1.45   mycroft 		}
   1364   1.45   mycroft 		if (error == ERESTART)
   1365   1.44   mycroft 			error = EINTR;
   1366   1.31       cgd 		return (error);
   1367   1.31       cgd 	}
   1368  1.331     pooka 
   1369  1.194  jdolecek 	l->l_dupfd = 0;
   1370   1.31       cgd 	vp = nd.ni_vp;
   1371   1.31       cgd 	fp->f_flag = flags & FMASK;
   1372   1.31       cgd 	fp->f_type = DTYPE_VNODE;
   1373   1.31       cgd 	fp->f_ops = &vnops;
   1374  1.184       dsl 	fp->f_data = vp;
   1375   1.31       cgd 	if (flags & (O_EXLOCK | O_SHLOCK)) {
   1376   1.31       cgd 		lf.l_whence = SEEK_SET;
   1377   1.31       cgd 		lf.l_start = 0;
   1378   1.31       cgd 		lf.l_len = 0;
   1379   1.31       cgd 		if (flags & O_EXLOCK)
   1380   1.31       cgd 			lf.l_type = F_WRLCK;
   1381   1.31       cgd 		else
   1382   1.31       cgd 			lf.l_type = F_RDLCK;
   1383   1.31       cgd 		type = F_FLOCK;
   1384   1.31       cgd 		if ((flags & FNONBLOCK) == 0)
   1385   1.31       cgd 			type |= F_WAIT;
   1386  1.113      fvdl 		VOP_UNLOCK(vp, 0);
   1387  1.184       dsl 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
   1388   1.63  christos 		if (error) {
   1389  1.346        ad 			(void) vn_close(vp, fp->f_flag, fp->f_cred);
   1390  1.346        ad 			fd_abort(p, fp, indx);
   1391   1.31       cgd 			return (error);
   1392   1.31       cgd 		}
   1393  1.113      fvdl 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1394  1.346        ad 		atomic_or_uint(&fp->f_flag, FHASLOCK);
   1395   1.31       cgd 	}
   1396  1.113      fvdl 	VOP_UNLOCK(vp, 0);
   1397   1.31       cgd 	*retval = indx;
   1398  1.346        ad 	fd_affix(p, fp, indx);
   1399   1.31       cgd 	return (0);
   1400  1.137  wrstuden }
   1401  1.137  wrstuden 
   1402  1.249      yamt static void
   1403  1.249      yamt vfs__fhfree(fhandle_t *fhp)
   1404  1.249      yamt {
   1405  1.249      yamt 	size_t fhsize;
   1406  1.249      yamt 
   1407  1.249      yamt 	if (fhp == NULL) {
   1408  1.249      yamt 		return;
   1409  1.249      yamt 	}
   1410  1.249      yamt 	fhsize = FHANDLE_SIZE(fhp);
   1411  1.249      yamt 	kmem_free(fhp, fhsize);
   1412  1.249      yamt }
   1413  1.249      yamt 
   1414  1.137  wrstuden /*
   1415  1.243      yamt  * vfs_composefh: compose a filehandle.
   1416  1.243      yamt  */
   1417  1.243      yamt 
   1418  1.243      yamt int
   1419  1.244    martin vfs_composefh(struct vnode *vp, fhandle_t *fhp, size_t *fh_size)
   1420  1.243      yamt {
   1421  1.243      yamt 	struct mount *mp;
   1422  1.250      yamt 	struct fid *fidp;
   1423  1.243      yamt 	int error;
   1424  1.250      yamt 	size_t needfhsize;
   1425  1.250      yamt 	size_t fidsize;
   1426  1.243      yamt 
   1427  1.243      yamt 	mp = vp->v_mount;
   1428  1.250      yamt 	fidp = NULL;
   1429  1.252    martin 	if (*fh_size < FHANDLE_SIZE_MIN) {
   1430  1.250      yamt 		fidsize = 0;
   1431  1.244    martin 	} else {
   1432  1.250      yamt 		fidsize = *fh_size - offsetof(fhandle_t, fh_fid);
   1433  1.250      yamt 		if (fhp != NULL) {
   1434  1.250      yamt 			memset(fhp, 0, *fh_size);
   1435  1.250      yamt 			fhp->fh_fsid = mp->mnt_stat.f_fsidx;
   1436  1.250      yamt 			fidp = &fhp->fh_fid;
   1437  1.250      yamt 		}
   1438  1.244    martin 	}
   1439  1.250      yamt 	error = VFS_VPTOFH(vp, fidp, &fidsize);
   1440  1.250      yamt 	needfhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
   1441  1.250      yamt 	if (error == 0 && *fh_size < needfhsize) {
   1442  1.250      yamt 		error = E2BIG;
   1443  1.250      yamt 	}
   1444  1.250      yamt 	*fh_size = needfhsize;
   1445  1.243      yamt 	return error;
   1446  1.243      yamt }
   1447  1.243      yamt 
   1448  1.249      yamt int
   1449  1.249      yamt vfs_composefh_alloc(struct vnode *vp, fhandle_t **fhpp)
   1450  1.249      yamt {
   1451  1.249      yamt 	struct mount *mp;
   1452  1.249      yamt 	fhandle_t *fhp;
   1453  1.249      yamt 	size_t fhsize;
   1454  1.249      yamt 	size_t fidsize;
   1455  1.249      yamt 	int error;
   1456  1.249      yamt 
   1457  1.249      yamt 	*fhpp = NULL;
   1458  1.249      yamt 	mp = vp->v_mount;
   1459  1.255  christos 	fidsize = 0;
   1460  1.249      yamt 	error = VFS_VPTOFH(vp, NULL, &fidsize);
   1461  1.249      yamt 	KASSERT(error != 0);
   1462  1.249      yamt 	if (error != E2BIG) {
   1463  1.249      yamt 		goto out;
   1464  1.249      yamt 	}
   1465  1.250      yamt 	fhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
   1466  1.249      yamt 	fhp = kmem_zalloc(fhsize, KM_SLEEP);
   1467  1.249      yamt 	if (fhp == NULL) {
   1468  1.249      yamt 		error = ENOMEM;
   1469  1.249      yamt 		goto out;
   1470  1.249      yamt 	}
   1471  1.249      yamt 	fhp->fh_fsid = mp->mnt_stat.f_fsidx;
   1472  1.249      yamt 	error = VFS_VPTOFH(vp, &fhp->fh_fid, &fidsize);
   1473  1.249      yamt 	if (error == 0) {
   1474  1.249      yamt 		KASSERT((FHANDLE_SIZE(fhp) == fhsize &&
   1475  1.249      yamt 		    FHANDLE_FILEID(fhp)->fid_len == fidsize));
   1476  1.249      yamt 		*fhpp = fhp;
   1477  1.249      yamt 	} else {
   1478  1.249      yamt 		kmem_free(fhp, fhsize);
   1479  1.249      yamt 	}
   1480  1.249      yamt out:
   1481  1.249      yamt 	return error;
   1482  1.249      yamt }
   1483  1.249      yamt 
   1484  1.249      yamt void
   1485  1.249      yamt vfs_composefh_free(fhandle_t *fhp)
   1486  1.249      yamt {
   1487  1.249      yamt 
   1488  1.249      yamt 	vfs__fhfree(fhp);
   1489  1.249      yamt }
   1490  1.249      yamt 
   1491  1.243      yamt /*
   1492  1.248      yamt  * vfs_fhtovp: lookup a vnode by a filehandle.
   1493  1.248      yamt  */
   1494  1.248      yamt 
   1495  1.248      yamt int
   1496  1.248      yamt vfs_fhtovp(fhandle_t *fhp, struct vnode **vpp)
   1497  1.248      yamt {
   1498  1.248      yamt 	struct mount *mp;
   1499  1.248      yamt 	int error;
   1500  1.248      yamt 
   1501  1.248      yamt 	*vpp = NULL;
   1502  1.248      yamt 	mp = vfs_getvfs(FHANDLE_FSID(fhp));
   1503  1.248      yamt 	if (mp == NULL) {
   1504  1.248      yamt 		error = ESTALE;
   1505  1.248      yamt 		goto out;
   1506  1.248      yamt 	}
   1507  1.248      yamt 	if (mp->mnt_op->vfs_fhtovp == NULL) {
   1508  1.248      yamt 		error = EOPNOTSUPP;
   1509  1.248      yamt 		goto out;
   1510  1.248      yamt 	}
   1511  1.248      yamt 	error = VFS_FHTOVP(mp, FHANDLE_FILEID(fhp), vpp);
   1512  1.248      yamt out:
   1513  1.248      yamt 	return error;
   1514  1.248      yamt }
   1515  1.248      yamt 
   1516  1.248      yamt /*
   1517  1.265      yamt  * vfs_copyinfh_alloc: allocate and copyin a filehandle, given
   1518  1.263    martin  * the needed size.
   1519  1.263    martin  */
   1520  1.263    martin 
   1521  1.263    martin int
   1522  1.265      yamt vfs_copyinfh_alloc(const void *ufhp, size_t fhsize, fhandle_t **fhpp)
   1523  1.263    martin {
   1524  1.263    martin 	fhandle_t *fhp;
   1525  1.263    martin 	int error;
   1526  1.263    martin 
   1527  1.263    martin 	*fhpp = NULL;
   1528  1.250      yamt 	if (fhsize > FHANDLE_SIZE_MAX) {
   1529  1.250      yamt 		return EINVAL;
   1530  1.250      yamt 	}
   1531  1.265      yamt 	if (fhsize < FHANDLE_SIZE_MIN) {
   1532  1.265      yamt 		return EINVAL;
   1533  1.265      yamt 	}
   1534  1.267      yamt again:
   1535  1.248      yamt 	fhp = kmem_alloc(fhsize, KM_SLEEP);
   1536  1.248      yamt 	if (fhp == NULL) {
   1537  1.248      yamt 		return ENOMEM;
   1538  1.248      yamt 	}
   1539  1.248      yamt 	error = copyin(ufhp, fhp, fhsize);
   1540  1.248      yamt 	if (error == 0) {
   1541  1.265      yamt 		/* XXX this check shouldn't be here */
   1542  1.265      yamt 		if (FHANDLE_SIZE(fhp) == fhsize) {
   1543  1.263    martin 			*fhpp = fhp;
   1544  1.263    martin 			return 0;
   1545  1.267      yamt 		} else if (fhsize == NFSX_V2FH && FHANDLE_SIZE(fhp) < fhsize) {
   1546  1.267      yamt 			/*
   1547  1.267      yamt 			 * a kludge for nfsv2 padded handles.
   1548  1.267      yamt 			 */
   1549  1.267      yamt 			size_t sz;
   1550  1.267      yamt 
   1551  1.267      yamt 			sz = FHANDLE_SIZE(fhp);
   1552  1.267      yamt 			kmem_free(fhp, fhsize);
   1553  1.267      yamt 			fhsize = sz;
   1554  1.267      yamt 			goto again;
   1555  1.264      yamt 		} else {
   1556  1.265      yamt 			/*
   1557  1.265      yamt 			 * userland told us wrong size.
   1558  1.265      yamt 			 */
   1559  1.263    martin 		    	error = EINVAL;
   1560  1.264      yamt 		}
   1561  1.248      yamt 	}
   1562  1.263    martin 	kmem_free(fhp, fhsize);
   1563  1.248      yamt 	return error;
   1564  1.248      yamt }
   1565  1.248      yamt 
   1566  1.248      yamt void
   1567  1.248      yamt vfs_copyinfh_free(fhandle_t *fhp)
   1568  1.248      yamt {
   1569  1.248      yamt 
   1570  1.249      yamt 	vfs__fhfree(fhp);
   1571  1.248      yamt }
   1572  1.248      yamt 
   1573  1.248      yamt /*
   1574  1.137  wrstuden  * Get file handle system call
   1575  1.137  wrstuden  */
   1576  1.137  wrstuden int
   1577  1.335       dsl sys___getfh30(struct lwp *l, const struct sys___getfh30_args *uap, register_t *retval)
   1578  1.137  wrstuden {
   1579  1.335       dsl 	/* {
   1580  1.137  wrstuden 		syscallarg(char *) fname;
   1581  1.137  wrstuden 		syscallarg(fhandle_t *) fhp;
   1582  1.244    martin 		syscallarg(size_t *) fh_size;
   1583  1.335       dsl 	} */
   1584  1.155  augustss 	struct vnode *vp;
   1585  1.244    martin 	fhandle_t *fh;
   1586  1.137  wrstuden 	int error;
   1587  1.137  wrstuden 	struct nameidata nd;
   1588  1.244    martin 	size_t sz;
   1589  1.249      yamt 	size_t usz;
   1590  1.137  wrstuden 
   1591  1.137  wrstuden 	/*
   1592  1.137  wrstuden 	 * Must be super user
   1593  1.137  wrstuden 	 */
   1594  1.268      elad 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
   1595  1.268      elad 	    0, NULL, NULL, NULL);
   1596  1.137  wrstuden 	if (error)
   1597  1.137  wrstuden 		return (error);
   1598  1.310       dsl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
   1599  1.334     pooka 	    SCARG(uap, fname));
   1600  1.137  wrstuden 	error = namei(&nd);
   1601  1.137  wrstuden 	if (error)
   1602  1.137  wrstuden 		return (error);
   1603  1.137  wrstuden 	vp = nd.ni_vp;
   1604  1.249      yamt 	error = vfs_composefh_alloc(vp, &fh);
   1605  1.247      yamt 	vput(vp);
   1606  1.249      yamt 	if (error != 0) {
   1607  1.249      yamt 		goto out;
   1608  1.249      yamt 	}
   1609  1.249      yamt 	error = copyin(SCARG(uap, fh_size), &usz, sizeof(size_t));
   1610  1.249      yamt 	if (error != 0) {
   1611  1.249      yamt 		goto out;
   1612  1.249      yamt 	}
   1613  1.249      yamt 	sz = FHANDLE_SIZE(fh);
   1614  1.249      yamt 	error = copyout(&sz, SCARG(uap, fh_size), sizeof(size_t));
   1615  1.249      yamt 	if (error != 0) {
   1616  1.249      yamt 		goto out;
   1617  1.244    martin 	}
   1618  1.249      yamt 	if (usz >= sz) {
   1619  1.249      yamt 		error = copyout(fh, SCARG(uap, fhp), sz);
   1620  1.249      yamt 	} else {
   1621  1.249      yamt 		error = E2BIG;
   1622  1.244    martin 	}
   1623  1.249      yamt out:
   1624  1.249      yamt 	vfs_composefh_free(fh);
   1625  1.137  wrstuden 	return (error);
   1626  1.137  wrstuden }
   1627  1.137  wrstuden 
   1628  1.137  wrstuden /*
   1629  1.137  wrstuden  * Open a file given a file handle.
   1630  1.137  wrstuden  *
   1631  1.137  wrstuden  * Check permissions, allocate an open file structure,
   1632  1.137  wrstuden  * and call the device open routine if any.
   1633  1.137  wrstuden  */
   1634  1.265      yamt 
   1635  1.137  wrstuden int
   1636  1.265      yamt dofhopen(struct lwp *l, const void *ufhp, size_t fhsize, int oflags,
   1637  1.265      yamt     register_t *retval)
   1638  1.137  wrstuden {
   1639  1.346        ad 	file_t *fp;
   1640  1.139  wrstuden 	struct vnode *vp = NULL;
   1641  1.257        ad 	kauth_cred_t cred = l->l_cred;
   1642  1.346        ad 	file_t *nfp;
   1643  1.137  wrstuden 	int type, indx, error=0;
   1644  1.137  wrstuden 	struct flock lf;
   1645  1.137  wrstuden 	struct vattr va;
   1646  1.248      yamt 	fhandle_t *fh;
   1647  1.265      yamt 	int flags;
   1648  1.346        ad 	proc_t *p;
   1649  1.346        ad 
   1650  1.346        ad 	p = curproc;
   1651  1.137  wrstuden 
   1652  1.137  wrstuden 	/*
   1653  1.137  wrstuden 	 * Must be super user
   1654  1.137  wrstuden 	 */
   1655  1.269      elad 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
   1656  1.270      elad 	    0, NULL, NULL, NULL)))
   1657  1.137  wrstuden 		return (error);
   1658  1.137  wrstuden 
   1659  1.265      yamt 	flags = FFLAGS(oflags);
   1660  1.137  wrstuden 	if ((flags & (FREAD | FWRITE)) == 0)
   1661  1.137  wrstuden 		return (EINVAL);
   1662  1.137  wrstuden 	if ((flags & O_CREAT))
   1663  1.137  wrstuden 		return (EINVAL);
   1664  1.346        ad 	if ((error = fd_allocfile(&nfp, &indx)) != 0)
   1665  1.137  wrstuden 		return (error);
   1666  1.137  wrstuden 	fp = nfp;
   1667  1.265      yamt 	error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
   1668  1.248      yamt 	if (error != 0) {
   1669  1.139  wrstuden 		goto bad;
   1670  1.139  wrstuden 	}
   1671  1.248      yamt 	error = vfs_fhtovp(fh, &vp);
   1672  1.248      yamt 	if (error != 0) {
   1673  1.139  wrstuden 		goto bad;
   1674  1.139  wrstuden 	}
   1675  1.137  wrstuden 
   1676  1.137  wrstuden 	/* Now do an effective vn_open */
   1677  1.137  wrstuden 
   1678  1.137  wrstuden 	if (vp->v_type == VSOCK) {
   1679  1.137  wrstuden 		error = EOPNOTSUPP;
   1680  1.137  wrstuden 		goto bad;
   1681  1.137  wrstuden 	}
   1682  1.333      yamt 	error = vn_openchk(vp, cred, flags);
   1683  1.333      yamt 	if (error != 0)
   1684  1.333      yamt 		goto bad;
   1685  1.137  wrstuden 	if (flags & O_TRUNC) {
   1686  1.137  wrstuden 		VOP_UNLOCK(vp, 0);			/* XXX */
   1687  1.137  wrstuden 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
   1688  1.137  wrstuden 		VATTR_NULL(&va);
   1689  1.137  wrstuden 		va.va_size = 0;
   1690  1.332     pooka 		error = VOP_SETATTR(vp, &va, cred);
   1691  1.197   hannken 		if (error)
   1692  1.137  wrstuden 			goto bad;
   1693  1.137  wrstuden 	}
   1694  1.332     pooka 	if ((error = VOP_OPEN(vp, flags, cred)) != 0)
   1695  1.137  wrstuden 		goto bad;
   1696  1.346        ad 	if (flags & FWRITE) {
   1697  1.346        ad 		mutex_enter(&vp->v_interlock);
   1698  1.137  wrstuden 		vp->v_writecount++;
   1699  1.346        ad 		mutex_exit(&vp->v_interlock);
   1700  1.346        ad 	}
   1701  1.137  wrstuden 
   1702  1.137  wrstuden 	/* done with modified vn_open, now finish what sys_open does. */
   1703  1.137  wrstuden 
   1704  1.137  wrstuden 	fp->f_flag = flags & FMASK;
   1705  1.137  wrstuden 	fp->f_type = DTYPE_VNODE;
   1706  1.137  wrstuden 	fp->f_ops = &vnops;
   1707  1.184       dsl 	fp->f_data = vp;
   1708  1.137  wrstuden 	if (flags & (O_EXLOCK | O_SHLOCK)) {
   1709  1.137  wrstuden 		lf.l_whence = SEEK_SET;
   1710  1.137  wrstuden 		lf.l_start = 0;
   1711  1.137  wrstuden 		lf.l_len = 0;
   1712  1.137  wrstuden 		if (flags & O_EXLOCK)
   1713  1.137  wrstuden 			lf.l_type = F_WRLCK;
   1714  1.137  wrstuden 		else
   1715  1.137  wrstuden 			lf.l_type = F_RDLCK;
   1716  1.137  wrstuden 		type = F_FLOCK;
   1717  1.137  wrstuden 		if ((flags & FNONBLOCK) == 0)
   1718  1.137  wrstuden 			type |= F_WAIT;
   1719  1.137  wrstuden 		VOP_UNLOCK(vp, 0);
   1720  1.184       dsl 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
   1721  1.137  wrstuden 		if (error) {
   1722  1.346        ad 			(void) vn_close(vp, fp->f_flag, fp->f_cred);
   1723  1.346        ad 			fd_abort(p, fp, indx);
   1724  1.137  wrstuden 			return (error);
   1725  1.137  wrstuden 		}
   1726  1.137  wrstuden 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1727  1.346        ad 		atomic_or_uint(&fp->f_flag, FHASLOCK);
   1728  1.137  wrstuden 	}
   1729  1.137  wrstuden 	VOP_UNLOCK(vp, 0);
   1730  1.137  wrstuden 	*retval = indx;
   1731  1.346        ad 	fd_affix(p, fp, indx);
   1732  1.248      yamt 	vfs_copyinfh_free(fh);
   1733  1.137  wrstuden 	return (0);
   1734  1.139  wrstuden 
   1735  1.137  wrstuden bad:
   1736  1.346        ad 	fd_abort(p, fp, indx);
   1737  1.139  wrstuden 	if (vp != NULL)
   1738  1.139  wrstuden 		vput(vp);
   1739  1.248      yamt 	vfs_copyinfh_free(fh);
   1740  1.137  wrstuden 	return (error);
   1741  1.137  wrstuden }
   1742  1.137  wrstuden 
   1743  1.137  wrstuden int
   1744  1.335       dsl sys___fhopen40(struct lwp *l, const struct sys___fhopen40_args *uap, register_t *retval)
   1745  1.137  wrstuden {
   1746  1.335       dsl 	/* {
   1747  1.263    martin 		syscallarg(const void *) fhp;
   1748  1.263    martin 		syscallarg(size_t) fh_size;
   1749  1.265      yamt 		syscallarg(int) flags;
   1750  1.335       dsl 	} */
   1751  1.265      yamt 
   1752  1.265      yamt 	return dofhopen(l, SCARG(uap, fhp), SCARG(uap, fh_size),
   1753  1.265      yamt 	    SCARG(uap, flags), retval);
   1754  1.265      yamt }
   1755  1.265      yamt 
   1756  1.306       dsl int
   1757  1.306       dsl do_fhstat(struct lwp *l, const void *ufhp, size_t fhsize, struct stat *sb)
   1758  1.306       dsl {
   1759  1.137  wrstuden 	int error;
   1760  1.248      yamt 	fhandle_t *fh;
   1761  1.137  wrstuden 	struct vnode *vp;
   1762  1.137  wrstuden 
   1763  1.137  wrstuden 	/*
   1764  1.137  wrstuden 	 * Must be super user
   1765  1.137  wrstuden 	 */
   1766  1.268      elad 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
   1767  1.270      elad 	    0, NULL, NULL, NULL)))
   1768  1.137  wrstuden 		return (error);
   1769  1.137  wrstuden 
   1770  1.265      yamt 	error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
   1771  1.306       dsl 	if (error != 0)
   1772  1.306       dsl 		return error;
   1773  1.306       dsl 
   1774  1.248      yamt 	error = vfs_fhtovp(fh, &vp);
   1775  1.306       dsl 	vfs_copyinfh_free(fh);
   1776  1.306       dsl 	if (error != 0)
   1777  1.306       dsl 		return error;
   1778  1.306       dsl 
   1779  1.346        ad 	error = vn_stat(vp, sb);
   1780  1.137  wrstuden 	vput(vp);
   1781  1.248      yamt 	return error;
   1782  1.137  wrstuden }
   1783  1.137  wrstuden 
   1784  1.265      yamt 
   1785  1.137  wrstuden /* ARGSUSED */
   1786  1.137  wrstuden int
   1787  1.335       dsl sys___fhstat40(struct lwp *l, const struct sys___fhstat40_args *uap, register_t *retval)
   1788  1.137  wrstuden {
   1789  1.335       dsl 	/* {
   1790  1.265      yamt 		syscallarg(const void *) fhp;
   1791  1.263    martin 		syscallarg(size_t) fh_size;
   1792  1.265      yamt 		syscallarg(struct stat *) sb;
   1793  1.335       dsl 	} */
   1794  1.306       dsl 	struct stat sb;
   1795  1.306       dsl 	int error;
   1796  1.265      yamt 
   1797  1.306       dsl 	error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb);
   1798  1.306       dsl 	if (error)
   1799  1.306       dsl 		return error;
   1800  1.306       dsl 	return copyout(&sb, SCARG(uap, sb), sizeof(sb));
   1801  1.265      yamt }
   1802  1.265      yamt 
   1803  1.306       dsl int
   1804  1.306       dsl do_fhstatvfs(struct lwp *l, const void *ufhp, size_t fhsize, struct statvfs *sb,
   1805  1.306       dsl     int flags)
   1806  1.306       dsl {
   1807  1.248      yamt 	fhandle_t *fh;
   1808  1.137  wrstuden 	struct mount *mp;
   1809  1.137  wrstuden 	struct vnode *vp;
   1810  1.137  wrstuden 	int error;
   1811  1.137  wrstuden 
   1812  1.137  wrstuden 	/*
   1813  1.137  wrstuden 	 * Must be super user
   1814  1.137  wrstuden 	 */
   1815  1.268      elad 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
   1816  1.270      elad 	    0, NULL, NULL, NULL)))
   1817  1.206  christos 		return error;
   1818  1.137  wrstuden 
   1819  1.265      yamt 	error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
   1820  1.306       dsl 	if (error != 0)
   1821  1.306       dsl 		return error;
   1822  1.306       dsl 
   1823  1.248      yamt 	error = vfs_fhtovp(fh, &vp);
   1824  1.306       dsl 	vfs_copyinfh_free(fh);
   1825  1.306       dsl 	if (error != 0)
   1826  1.306       dsl 		return error;
   1827  1.306       dsl 
   1828  1.137  wrstuden 	mp = vp->v_mount;
   1829  1.306       dsl 	error = dostatvfs(mp, sb, l, flags, 1);
   1830  1.137  wrstuden 	vput(vp);
   1831  1.241      yamt 	return error;
   1832   1.31       cgd }
   1833   1.31       cgd 
   1834  1.265      yamt /* ARGSUSED */
   1835  1.265      yamt int
   1836  1.335       dsl sys___fhstatvfs140(struct lwp *l, const struct sys___fhstatvfs140_args *uap, register_t *retval)
   1837  1.265      yamt {
   1838  1.335       dsl 	/* {
   1839  1.266      yamt 		syscallarg(const void *) fhp;
   1840  1.265      yamt 		syscallarg(size_t) fh_size;
   1841  1.265      yamt 		syscallarg(struct statvfs *) buf;
   1842  1.265      yamt 		syscallarg(int)	flags;
   1843  1.335       dsl 	} */
   1844  1.306       dsl 	struct statvfs *sb = STATVFSBUF_GET();
   1845  1.306       dsl 	int error;
   1846  1.265      yamt 
   1847  1.306       dsl 	error = do_fhstatvfs(l, SCARG(uap, fhp), SCARG(uap, fh_size), sb,
   1848  1.306       dsl 	    SCARG(uap, flags));
   1849  1.306       dsl 	if (error == 0)
   1850  1.306       dsl 		error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
   1851  1.306       dsl 	STATVFSBUF_PUT(sb);
   1852  1.306       dsl 	return error;
   1853  1.265      yamt }
   1854  1.265      yamt 
   1855   1.31       cgd /*
   1856   1.31       cgd  * Create a special file.
   1857   1.31       cgd  */
   1858   1.31       cgd /* ARGSUSED */
   1859   1.63  christos int
   1860  1.335       dsl sys_mknod(struct lwp *l, const struct sys_mknod_args *uap, register_t *retval)
   1861   1.56   thorpej {
   1862  1.335       dsl 	/* {
   1863   1.74       cgd 		syscallarg(const char *) path;
   1864   1.35       cgd 		syscallarg(int) mode;
   1865   1.35       cgd 		syscallarg(int) dev;
   1866  1.335       dsl 	} */
   1867  1.179   thorpej 	struct proc *p = l->l_proc;
   1868  1.155  augustss 	struct vnode *vp;
   1869   1.31       cgd 	struct vattr vattr;
   1870  1.301     pooka 	int error, optype;
   1871  1.315  christos 	struct nameidata nd;
   1872  1.314  christos 	char *path;
   1873  1.315  christos 	const char *cpath;
   1874  1.315  christos 	enum uio_seg seg = UIO_USERSPACE;
   1875   1.31       cgd 
   1876  1.268      elad 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MKNOD,
   1877  1.268      elad 	    0, NULL, NULL, NULL)) != 0)
   1878   1.31       cgd 		return (error);
   1879  1.301     pooka 
   1880  1.301     pooka 	optype = VOP_MKNOD_DESCOFFSET;
   1881  1.315  christos 
   1882  1.315  christos 	VERIEXEC_PATH_GET(SCARG(uap, path), seg, cpath, path);
   1883  1.334     pooka 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, seg, cpath);
   1884  1.315  christos 
   1885   1.63  christos 	if ((error = namei(&nd)) != 0)
   1886  1.314  christos 		goto out;
   1887   1.31       cgd 	vp = nd.ni_vp;
   1888   1.43   mycroft 	if (vp != NULL)
   1889   1.31       cgd 		error = EEXIST;
   1890   1.43   mycroft 	else {
   1891   1.43   mycroft 		VATTR_NULL(&vattr);
   1892  1.328        ad 		/* We will read cwdi->cwdi_cmask unlocked. */
   1893   1.94     enami 		vattr.va_mode =
   1894  1.134   thorpej 		    (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1895   1.43   mycroft 		vattr.va_rdev = SCARG(uap, dev);
   1896   1.43   mycroft 
   1897   1.43   mycroft 		switch (SCARG(uap, mode) & S_IFMT) {
   1898   1.43   mycroft 		case S_IFMT:	/* used by badsect to flag bad sectors */
   1899   1.43   mycroft 			vattr.va_type = VBAD;
   1900   1.43   mycroft 			break;
   1901   1.43   mycroft 		case S_IFCHR:
   1902   1.43   mycroft 			vattr.va_type = VCHR;
   1903   1.43   mycroft 			break;
   1904   1.43   mycroft 		case S_IFBLK:
   1905   1.43   mycroft 			vattr.va_type = VBLK;
   1906   1.43   mycroft 			break;
   1907   1.43   mycroft 		case S_IFWHT:
   1908  1.301     pooka 			optype = VOP_WHITEOUT_DESCOFFSET;
   1909  1.301     pooka 			break;
   1910  1.301     pooka 		case S_IFREG:
   1911  1.314  christos #if NVERIEXEC > 0
   1912  1.314  christos 			error = veriexec_openchk(l, nd.ni_vp, nd.ni_dirp,
   1913  1.314  christos 			    O_CREAT);
   1914  1.314  christos #endif /* NVERIEXEC > 0 */
   1915  1.301     pooka 			vattr.va_type = VREG;
   1916  1.302     pooka 			vattr.va_rdev = VNOVAL;
   1917  1.301     pooka 			optype = VOP_CREATE_DESCOFFSET;
   1918   1.43   mycroft 			break;
   1919   1.43   mycroft 		default:
   1920   1.43   mycroft 			error = EINVAL;
   1921   1.43   mycroft 			break;
   1922   1.43   mycroft 		}
   1923   1.31       cgd 	}
   1924   1.43   mycroft 	if (!error) {
   1925  1.301     pooka 		switch (optype) {
   1926  1.301     pooka 		case VOP_WHITEOUT_DESCOFFSET:
   1927   1.43   mycroft 			error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
   1928   1.43   mycroft 			if (error)
   1929   1.43   mycroft 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1930   1.43   mycroft 			vput(nd.ni_dvp);
   1931  1.301     pooka 			break;
   1932  1.301     pooka 
   1933  1.301     pooka 		case VOP_MKNOD_DESCOFFSET:
   1934   1.43   mycroft 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
   1935   1.43   mycroft 						&nd.ni_cnd, &vattr);
   1936  1.168     assar 			if (error == 0)
   1937  1.168     assar 				vput(nd.ni_vp);
   1938  1.301     pooka 			break;
   1939  1.301     pooka 
   1940  1.301     pooka 		case VOP_CREATE_DESCOFFSET:
   1941  1.301     pooka 			error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
   1942  1.301     pooka 						&nd.ni_cnd, &vattr);
   1943  1.301     pooka 			if (error == 0)
   1944  1.301     pooka 				vput(nd.ni_vp);
   1945  1.301     pooka 			break;
   1946   1.43   mycroft 		}
   1947   1.43   mycroft 	} else {
   1948   1.43   mycroft 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1949   1.43   mycroft 		if (nd.ni_dvp == vp)
   1950   1.43   mycroft 			vrele(nd.ni_dvp);
   1951   1.43   mycroft 		else
   1952   1.43   mycroft 			vput(nd.ni_dvp);
   1953   1.43   mycroft 		if (vp)
   1954   1.43   mycroft 			vrele(vp);
   1955   1.31       cgd 	}
   1956  1.314  christos out:
   1957  1.315  christos 	VERIEXEC_PATH_PUT(path);
   1958   1.31       cgd 	return (error);
   1959   1.31       cgd }
   1960   1.31       cgd 
   1961   1.31       cgd /*
   1962   1.31       cgd  * Create a named pipe.
   1963   1.31       cgd  */
   1964   1.31       cgd /* ARGSUSED */
   1965   1.63  christos int
   1966  1.335       dsl sys_mkfifo(struct lwp *l, const struct sys_mkfifo_args *uap, register_t *retval)
   1967   1.56   thorpej {
   1968  1.335       dsl 	/* {
   1969   1.74       cgd 		syscallarg(const char *) path;
   1970   1.35       cgd 		syscallarg(int) mode;
   1971  1.335       dsl 	} */
   1972  1.179   thorpej 	struct proc *p = l->l_proc;
   1973   1.31       cgd 	struct vattr vattr;
   1974   1.31       cgd 	int error;
   1975   1.31       cgd 	struct nameidata nd;
   1976   1.31       cgd 
   1977  1.334     pooka 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE,
   1978  1.334     pooka 	    SCARG(uap, path));
   1979   1.63  christos 	if ((error = namei(&nd)) != 0)
   1980   1.31       cgd 		return (error);
   1981   1.31       cgd 	if (nd.ni_vp != NULL) {
   1982   1.31       cgd 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1983   1.31       cgd 		if (nd.ni_dvp == nd.ni_vp)
   1984   1.31       cgd 			vrele(nd.ni_dvp);
   1985   1.31       cgd 		else
   1986   1.31       cgd 			vput(nd.ni_dvp);
   1987   1.31       cgd 		vrele(nd.ni_vp);
   1988   1.31       cgd 		return (EEXIST);
   1989   1.31       cgd 	}
   1990   1.31       cgd 	VATTR_NULL(&vattr);
   1991   1.31       cgd 	vattr.va_type = VFIFO;
   1992  1.328        ad 	/* We will read cwdi->cwdi_cmask unlocked. */
   1993  1.134   thorpej 	vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1994  1.168     assar 	error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   1995  1.168     assar 	if (error == 0)
   1996  1.168     assar 		vput(nd.ni_vp);
   1997  1.168     assar 	return (error);
   1998   1.31       cgd }
   1999   1.31       cgd 
   2000   1.31       cgd /*
   2001   1.31       cgd  * Make a hard file link.
   2002   1.31       cgd  */
   2003   1.31       cgd /* ARGSUSED */
   2004   1.63  christos int
   2005  1.335       dsl sys_link(struct lwp *l, const struct sys_link_args *uap, register_t *retval)
   2006   1.56   thorpej {
   2007  1.335       dsl 	/* {
   2008   1.74       cgd 		syscallarg(const char *) path;
   2009   1.74       cgd 		syscallarg(const char *) link;
   2010  1.335       dsl 	} */
   2011  1.155  augustss 	struct vnode *vp;
   2012   1.31       cgd 	struct nameidata nd;
   2013   1.31       cgd 	int error;
   2014   1.31       cgd 
   2015  1.331     pooka 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
   2016  1.334     pooka 	    SCARG(uap, path));
   2017   1.63  christos 	if ((error = namei(&nd)) != 0)
   2018   1.31       cgd 		return (error);
   2019   1.31       cgd 	vp = nd.ni_vp;
   2020  1.331     pooka 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE,
   2021  1.334     pooka 	    SCARG(uap, link));
   2022   1.66   mycroft 	if ((error = namei(&nd)) != 0)
   2023   1.66   mycroft 		goto out;
   2024   1.66   mycroft 	if (nd.ni_vp) {
   2025   1.66   mycroft 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2026   1.66   mycroft 		if (nd.ni_dvp == nd.ni_vp)
   2027   1.66   mycroft 			vrele(nd.ni_dvp);
   2028   1.66   mycroft 		else
   2029   1.66   mycroft 			vput(nd.ni_dvp);
   2030   1.66   mycroft 		vrele(nd.ni_vp);
   2031   1.66   mycroft 		error = EEXIST;
   2032   1.66   mycroft 		goto out;
   2033   1.31       cgd 	}
   2034   1.66   mycroft 	error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   2035   1.66   mycroft out:
   2036   1.31       cgd 	vrele(vp);
   2037   1.31       cgd 	return (error);
   2038   1.31       cgd }
   2039   1.31       cgd 
   2040   1.31       cgd /*
   2041   1.31       cgd  * Make a symbolic link.
   2042   1.31       cgd  */
   2043   1.31       cgd /* ARGSUSED */
   2044   1.63  christos int
   2045  1.335       dsl sys_symlink(struct lwp *l, const struct sys_symlink_args *uap, register_t *retval)
   2046   1.56   thorpej {
   2047  1.335       dsl 	/* {
   2048   1.74       cgd 		syscallarg(const char *) path;
   2049   1.74       cgd 		syscallarg(const char *) link;
   2050  1.335       dsl 	} */
   2051  1.179   thorpej 	struct proc *p = l->l_proc;
   2052   1.31       cgd 	struct vattr vattr;
   2053   1.31       cgd 	char *path;
   2054   1.31       cgd 	int error;
   2055   1.31       cgd 	struct nameidata nd;
   2056   1.31       cgd 
   2057  1.161   thorpej 	path = PNBUF_GET();
   2058   1.63  christos 	error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
   2059   1.63  christos 	if (error)
   2060   1.43   mycroft 		goto out;
   2061  1.331     pooka 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE,
   2062  1.334     pooka 	    SCARG(uap, link));
   2063   1.63  christos 	if ((error = namei(&nd)) != 0)
   2064   1.43   mycroft 		goto out;
   2065   1.31       cgd 	if (nd.ni_vp) {
   2066   1.31       cgd 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2067   1.31       cgd 		if (nd.ni_dvp == nd.ni_vp)
   2068   1.31       cgd 			vrele(nd.ni_dvp);
   2069   1.31       cgd 		else
   2070   1.31       cgd 			vput(nd.ni_dvp);
   2071   1.31       cgd 		vrele(nd.ni_vp);
   2072   1.31       cgd 		error = EEXIST;
   2073   1.43   mycroft 		goto out;
   2074   1.31       cgd 	}
   2075   1.31       cgd 	VATTR_NULL(&vattr);
   2076  1.230      jmmv 	vattr.va_type = VLNK;
   2077  1.328        ad 	/* We will read cwdi->cwdi_cmask unlocked. */
   2078  1.134   thorpej 	vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
   2079   1.31       cgd 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
   2080  1.168     assar 	if (error == 0)
   2081  1.168     assar 		vput(nd.ni_vp);
   2082   1.43   mycroft out:
   2083  1.161   thorpej 	PNBUF_PUT(path);
   2084   1.31       cgd 	return (error);
   2085   1.31       cgd }
   2086   1.31       cgd 
   2087   1.31       cgd /*
   2088   1.43   mycroft  * Delete a whiteout from the filesystem.
   2089   1.43   mycroft  */
   2090   1.43   mycroft /* ARGSUSED */
   2091   1.63  christos int
   2092  1.335       dsl sys_undelete(struct lwp *l, const struct sys_undelete_args *uap, register_t *retval)
   2093   1.56   thorpej {
   2094  1.335       dsl 	/* {
   2095   1.74       cgd 		syscallarg(const char *) path;
   2096  1.335       dsl 	} */
   2097   1.43   mycroft 	int error;
   2098   1.43   mycroft 	struct nameidata nd;
   2099   1.43   mycroft 
   2100  1.331     pooka 	NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | TRYEMULROOT,
   2101  1.334     pooka 	    UIO_USERSPACE, SCARG(uap, path));
   2102   1.43   mycroft 	error = namei(&nd);
   2103   1.43   mycroft 	if (error)
   2104   1.43   mycroft 		return (error);
   2105   1.43   mycroft 
   2106   1.43   mycroft 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
   2107   1.43   mycroft 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2108   1.43   mycroft 		if (nd.ni_dvp == nd.ni_vp)
   2109   1.43   mycroft 			vrele(nd.ni_dvp);
   2110   1.43   mycroft 		else
   2111   1.43   mycroft 			vput(nd.ni_dvp);
   2112   1.43   mycroft 		if (nd.ni_vp)
   2113   1.43   mycroft 			vrele(nd.ni_vp);
   2114   1.43   mycroft 		return (EEXIST);
   2115   1.43   mycroft 	}
   2116   1.63  christos 	if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
   2117   1.43   mycroft 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2118   1.43   mycroft 	vput(nd.ni_dvp);
   2119   1.43   mycroft 	return (error);
   2120   1.43   mycroft }
   2121   1.43   mycroft 
   2122   1.43   mycroft /*
   2123   1.31       cgd  * Delete a name from the filesystem.
   2124   1.31       cgd  */
   2125   1.31       cgd /* ARGSUSED */
   2126   1.63  christos int
   2127  1.335       dsl sys_unlink(struct lwp *l, const struct sys_unlink_args *uap, register_t *retval)
   2128   1.56   thorpej {
   2129  1.335       dsl 	/* {
   2130   1.74       cgd 		syscallarg(const char *) path;
   2131  1.335       dsl 	} */
   2132  1.336        ad 
   2133  1.336        ad 	return do_sys_unlink(SCARG(uap, path), UIO_USERSPACE);
   2134  1.336        ad }
   2135  1.336        ad 
   2136  1.336        ad int
   2137  1.336        ad do_sys_unlink(const char *arg, enum uio_seg seg)
   2138  1.336        ad {
   2139  1.155  augustss 	struct vnode *vp;
   2140   1.31       cgd 	int error;
   2141   1.31       cgd 	struct nameidata nd;
   2142  1.336        ad 	kauth_cred_t cred;
   2143  1.315  christos 	char *path;
   2144  1.315  christos 	const char *cpath;
   2145   1.31       cgd 
   2146  1.336        ad 	VERIEXEC_PATH_GET(arg, seg, cpath, path);
   2147  1.334     pooka 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, seg, cpath);
   2148  1.313      elad 
   2149   1.63  christos 	if ((error = namei(&nd)) != 0)
   2150  1.313      elad 		goto out;
   2151   1.31       cgd 	vp = nd.ni_vp;
   2152   1.31       cgd 
   2153   1.66   mycroft 	/*
   2154   1.66   mycroft 	 * The root of a mounted filesystem cannot be deleted.
   2155   1.66   mycroft 	 */
   2156  1.329        ad 	if (vp->v_vflag & VV_ROOT) {
   2157   1.43   mycroft 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2158   1.43   mycroft 		if (nd.ni_dvp == vp)
   2159   1.43   mycroft 			vrele(nd.ni_dvp);
   2160   1.43   mycroft 		else
   2161   1.43   mycroft 			vput(nd.ni_dvp);
   2162   1.66   mycroft 		vput(vp);
   2163   1.66   mycroft 		error = EBUSY;
   2164   1.66   mycroft 		goto out;
   2165   1.31       cgd 	}
   2166  1.219     blymn 
   2167  1.256      elad #if NVERIEXEC > 0
   2168  1.222      elad 	/* Handle remove requests for veriexec entries. */
   2169  1.336        ad 	if ((error = veriexec_removechk(curlwp, nd.ni_vp, nd.ni_dirp)) != 0) {
   2170  1.219     blymn 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2171  1.219     blymn 		if (nd.ni_dvp == vp)
   2172  1.219     blymn 			vrele(nd.ni_dvp);
   2173  1.219     blymn 		else
   2174  1.219     blymn 			vput(nd.ni_dvp);
   2175  1.219     blymn 		vput(vp);
   2176  1.219     blymn 		goto out;
   2177  1.219     blymn 	}
   2178  1.256      elad #endif /* NVERIEXEC > 0 */
   2179  1.256      elad 
   2180  1.336        ad 	cred = kauth_cred_get();
   2181  1.253      elad #ifdef FILEASSOC
   2182  1.278      elad 	(void)fileassoc_file_delete(vp);
   2183  1.253      elad #endif /* FILEASSOC */
   2184   1.66   mycroft 	error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   2185   1.66   mycroft out:
   2186  1.315  christos 	VERIEXEC_PATH_PUT(path);
   2187   1.31       cgd 	return (error);
   2188   1.31       cgd }
   2189   1.31       cgd 
   2190   1.31       cgd /*
   2191   1.31       cgd  * Reposition read/write file offset.
   2192   1.31       cgd  */
   2193   1.63  christos int
   2194  1.335       dsl sys_lseek(struct lwp *l, const struct sys_lseek_args *uap, register_t *retval)
   2195   1.56   thorpej {
   2196  1.335       dsl 	/* {
   2197   1.35       cgd 		syscallarg(int) fd;
   2198   1.35       cgd 		syscallarg(int) pad;
   2199   1.35       cgd 		syscallarg(off_t) offset;
   2200   1.35       cgd 		syscallarg(int) whence;
   2201  1.335       dsl 	} */
   2202  1.257        ad 	kauth_cred_t cred = l->l_cred;
   2203  1.346        ad 	file_t *fp;
   2204   1.84    kleink 	struct vnode *vp;
   2205   1.31       cgd 	struct vattr vattr;
   2206  1.155  augustss 	off_t newoff;
   2207  1.346        ad 	int error, fd;
   2208  1.346        ad 
   2209  1.346        ad 	fd = SCARG(uap, fd);
   2210   1.31       cgd 
   2211  1.346        ad 	if ((fp = fd_getfile(fd)) == NULL)
   2212   1.31       cgd 		return (EBADF);
   2213   1.84    kleink 
   2214  1.346        ad 	vp = fp->f_data;
   2215  1.135   thorpej 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   2216  1.135   thorpej 		error = ESPIPE;
   2217  1.135   thorpej 		goto out;
   2218  1.135   thorpej 	}
   2219   1.84    kleink 
   2220   1.35       cgd 	switch (SCARG(uap, whence)) {
   2221   1.92    kleink 	case SEEK_CUR:
   2222   1.84    kleink 		newoff = fp->f_offset + SCARG(uap, offset);
   2223   1.31       cgd 		break;
   2224   1.92    kleink 	case SEEK_END:
   2225  1.332     pooka 		error = VOP_GETATTR(vp, &vattr, cred);
   2226  1.328        ad 		if (error) {
   2227  1.135   thorpej 			goto out;
   2228  1.328        ad 		}
   2229   1.84    kleink 		newoff = SCARG(uap, offset) + vattr.va_size;
   2230   1.31       cgd 		break;
   2231   1.92    kleink 	case SEEK_SET:
   2232   1.84    kleink 		newoff = SCARG(uap, offset);
   2233   1.31       cgd 		break;
   2234   1.31       cgd 	default:
   2235  1.135   thorpej 		error = EINVAL;
   2236  1.135   thorpej 		goto out;
   2237   1.31       cgd 	}
   2238  1.328        ad 	if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) == 0) {
   2239  1.328        ad 		*(off_t *)retval = fp->f_offset = newoff;
   2240  1.328        ad 	}
   2241  1.135   thorpej  out:
   2242  1.346        ad  	fd_putfile(fd);
   2243  1.135   thorpej 	return (error);
   2244  1.120   thorpej }
   2245  1.120   thorpej 
   2246  1.120   thorpej /*
   2247  1.120   thorpej  * Positional read system call.
   2248  1.120   thorpej  */
   2249  1.120   thorpej int
   2250  1.335       dsl sys_pread(struct lwp *l, const struct sys_pread_args *uap, register_t *retval)
   2251  1.120   thorpej {
   2252  1.335       dsl 	/* {
   2253  1.120   thorpej 		syscallarg(int) fd;
   2254  1.120   thorpej 		syscallarg(void *) buf;
   2255  1.120   thorpej 		syscallarg(size_t) nbyte;
   2256  1.120   thorpej 		syscallarg(off_t) offset;
   2257  1.335       dsl 	} */
   2258  1.346        ad 	file_t *fp;
   2259  1.120   thorpej 	struct vnode *vp;
   2260  1.120   thorpej 	off_t offset;
   2261  1.120   thorpej 	int error, fd = SCARG(uap, fd);
   2262  1.120   thorpej 
   2263  1.346        ad 	if ((fp = fd_getfile(fd)) == NULL)
   2264  1.166   thorpej 		return (EBADF);
   2265  1.166   thorpej 
   2266  1.183        pk 	if ((fp->f_flag & FREAD) == 0) {
   2267  1.346        ad 		fd_putfile(fd);
   2268  1.120   thorpej 		return (EBADF);
   2269  1.183        pk 	}
   2270  1.120   thorpej 
   2271  1.346        ad 	vp = fp->f_data;
   2272  1.135   thorpej 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   2273  1.135   thorpej 		error = ESPIPE;
   2274  1.135   thorpej 		goto out;
   2275  1.135   thorpej 	}
   2276  1.120   thorpej 
   2277  1.120   thorpej 	offset = SCARG(uap, offset);
   2278  1.120   thorpej 
   2279  1.120   thorpej 	/*
   2280  1.120   thorpej 	 * XXX This works because no file systems actually
   2281  1.120   thorpej 	 * XXX take any action on the seek operation.
   2282  1.120   thorpej 	 */
   2283  1.120   thorpej 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   2284  1.135   thorpej 		goto out;
   2285  1.120   thorpej 
   2286  1.135   thorpej 	/* dofileread() will unuse the descriptor for us */
   2287  1.328        ad 	return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   2288  1.120   thorpej 	    &offset, 0, retval));
   2289  1.135   thorpej 
   2290  1.135   thorpej  out:
   2291  1.346        ad 	fd_putfile(fd);
   2292  1.135   thorpej 	return (error);
   2293  1.120   thorpej }
   2294  1.120   thorpej 
   2295  1.120   thorpej /*
   2296  1.120   thorpej  * Positional scatter read system call.
   2297  1.120   thorpej  */
   2298  1.120   thorpej int
   2299  1.335       dsl sys_preadv(struct lwp *l, const struct sys_preadv_args *uap, register_t *retval)
   2300  1.120   thorpej {
   2301  1.335       dsl 	/* {
   2302  1.120   thorpej 		syscallarg(int) fd;
   2303  1.120   thorpej 		syscallarg(const struct iovec *) iovp;
   2304  1.120   thorpej 		syscallarg(int) iovcnt;
   2305  1.120   thorpej 		syscallarg(off_t) offset;
   2306  1.335       dsl 	} */
   2307  1.335       dsl 	off_t offset = SCARG(uap, offset);
   2308  1.120   thorpej 
   2309  1.328        ad 	return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
   2310  1.335       dsl 	    SCARG(uap, iovcnt), &offset, 0, retval);
   2311  1.120   thorpej }
   2312  1.120   thorpej 
   2313  1.120   thorpej /*
   2314  1.120   thorpej  * Positional write system call.
   2315  1.120   thorpej  */
   2316  1.120   thorpej int
   2317  1.335       dsl sys_pwrite(struct lwp *l, const struct sys_pwrite_args *uap, register_t *retval)
   2318  1.120   thorpej {
   2319  1.335       dsl 	/* {
   2320  1.120   thorpej 		syscallarg(int) fd;
   2321  1.120   thorpej 		syscallarg(const void *) buf;
   2322  1.120   thorpej 		syscallarg(size_t) nbyte;
   2323  1.120   thorpej 		syscallarg(off_t) offset;
   2324  1.335       dsl 	} */
   2325  1.346        ad 	file_t *fp;
   2326  1.120   thorpej 	struct vnode *vp;
   2327  1.120   thorpej 	off_t offset;
   2328  1.120   thorpej 	int error, fd = SCARG(uap, fd);
   2329  1.120   thorpej 
   2330  1.346        ad 	if ((fp = fd_getfile(fd)) == NULL)
   2331  1.166   thorpej 		return (EBADF);
   2332  1.166   thorpej 
   2333  1.183        pk 	if ((fp->f_flag & FWRITE) == 0) {
   2334  1.346        ad 		fd_putfile(fd);
   2335  1.120   thorpej 		return (EBADF);
   2336  1.183        pk 	}
   2337  1.120   thorpej 
   2338  1.346        ad 	vp = fp->f_data;
   2339  1.135   thorpej 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   2340  1.135   thorpej 		error = ESPIPE;
   2341  1.135   thorpej 		goto out;
   2342  1.135   thorpej 	}
   2343  1.120   thorpej 
   2344  1.120   thorpej 	offset = SCARG(uap, offset);
   2345  1.120   thorpej 
   2346  1.120   thorpej 	/*
   2347  1.120   thorpej 	 * XXX This works because no file systems actually
   2348  1.120   thorpej 	 * XXX take any action on the seek operation.
   2349  1.120   thorpej 	 */
   2350  1.120   thorpej 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   2351  1.135   thorpej 		goto out;
   2352  1.120   thorpej 
   2353  1.135   thorpej 	/* dofilewrite() will unuse the descriptor for us */
   2354  1.328        ad 	return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   2355  1.120   thorpej 	    &offset, 0, retval));
   2356  1.135   thorpej 
   2357  1.135   thorpej  out:
   2358  1.346        ad 	fd_putfile(fd);
   2359  1.135   thorpej 	return (error);
   2360  1.120   thorpej }
   2361  1.120   thorpej 
   2362  1.120   thorpej /*
   2363  1.120   thorpej  * Positional gather write system call.
   2364  1.120   thorpej  */
   2365  1.120   thorpej int
   2366  1.335       dsl sys_pwritev(struct lwp *l, const struct sys_pwritev_args *uap, register_t *retval)
   2367  1.120   thorpej {
   2368  1.335       dsl 	/* {
   2369  1.120   thorpej 		syscallarg(int) fd;
   2370  1.120   thorpej 		syscallarg(const struct iovec *) iovp;
   2371  1.120   thorpej 		syscallarg(int) iovcnt;
   2372  1.120   thorpej 		syscallarg(off_t) offset;
   2373  1.335       dsl 	} */
   2374  1.335       dsl 	off_t offset = SCARG(uap, offset);
   2375  1.120   thorpej 
   2376  1.328        ad 	return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
   2377  1.335       dsl 	    SCARG(uap, iovcnt), &offset, 0, retval);
   2378   1.31       cgd }
   2379   1.31       cgd 
   2380   1.31       cgd /*
   2381   1.31       cgd  * Check access permissions.
   2382   1.31       cgd  */
   2383   1.63  christos int
   2384  1.335       dsl sys_access(struct lwp *l, const struct sys_access_args *uap, register_t *retval)
   2385   1.56   thorpej {
   2386  1.335       dsl 	/* {
   2387   1.74       cgd 		syscallarg(const char *) path;
   2388   1.35       cgd 		syscallarg(int) flags;
   2389  1.335       dsl 	} */
   2390  1.242      elad 	kauth_cred_t cred;
   2391  1.155  augustss 	struct vnode *vp;
   2392  1.169  christos 	int error, flags;
   2393   1.31       cgd 	struct nameidata nd;
   2394   1.31       cgd 
   2395  1.257        ad 	cred = kauth_cred_dup(l->l_cred);
   2396  1.257        ad 	kauth_cred_seteuid(cred, kauth_cred_getuid(l->l_cred));
   2397  1.257        ad 	kauth_cred_setegid(cred, kauth_cred_getgid(l->l_cred));
   2398  1.310       dsl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
   2399  1.334     pooka 	    SCARG(uap, path));
   2400  1.169  christos 	/* Override default credentials */
   2401  1.169  christos 	nd.ni_cnd.cn_cred = cred;
   2402   1.63  christos 	if ((error = namei(&nd)) != 0)
   2403  1.169  christos 		goto out;
   2404   1.31       cgd 	vp = nd.ni_vp;
   2405   1.31       cgd 
   2406   1.31       cgd 	/* Flags == 0 means only check for existence. */
   2407   1.35       cgd 	if (SCARG(uap, flags)) {
   2408   1.31       cgd 		flags = 0;
   2409   1.35       cgd 		if (SCARG(uap, flags) & R_OK)
   2410   1.31       cgd 			flags |= VREAD;
   2411   1.35       cgd 		if (SCARG(uap, flags) & W_OK)
   2412   1.31       cgd 			flags |= VWRITE;
   2413   1.89   mycroft 		if (SCARG(uap, flags) & X_OK)
   2414   1.89   mycroft 			flags |= VEXEC;
   2415  1.138        is 
   2416  1.332     pooka 		error = VOP_ACCESS(vp, flags, cred);
   2417  1.138        is 		if (!error && (flags & VWRITE))
   2418  1.138        is 			error = vn_writechk(vp);
   2419   1.31       cgd 	}
   2420   1.31       cgd 	vput(vp);
   2421  1.169  christos out:
   2422  1.242      elad 	kauth_cred_free(cred);
   2423   1.31       cgd 	return (error);
   2424   1.31       cgd }
   2425   1.31       cgd 
   2426   1.31       cgd /*
   2427  1.306       dsl  * Common code for all sys_stat functions, including compat versions.
   2428  1.306       dsl  */
   2429  1.306       dsl int
   2430  1.346        ad do_sys_stat(const char *path, unsigned int nd_flags, struct stat *sb)
   2431  1.306       dsl {
   2432  1.306       dsl 	int error;
   2433  1.306       dsl 	struct nameidata nd;
   2434  1.306       dsl 
   2435  1.331     pooka 	NDINIT(&nd, LOOKUP, nd_flags | LOCKLEAF | TRYEMULROOT,
   2436  1.334     pooka 	    UIO_USERSPACE, path);
   2437  1.306       dsl 	error = namei(&nd);
   2438  1.306       dsl 	if (error != 0)
   2439  1.306       dsl 		return error;
   2440  1.346        ad 	error = vn_stat(nd.ni_vp, sb);
   2441  1.306       dsl 	vput(nd.ni_vp);
   2442  1.306       dsl 	return error;
   2443  1.306       dsl }
   2444  1.306       dsl 
   2445  1.306       dsl /*
   2446   1.31       cgd  * Get file status; this version follows links.
   2447   1.31       cgd  */
   2448   1.31       cgd /* ARGSUSED */
   2449   1.63  christos int
   2450  1.335       dsl sys___stat30(struct lwp *l, const struct sys___stat30_args *uap, register_t *retval)
   2451   1.56   thorpej {
   2452  1.335       dsl 	/* {
   2453   1.74       cgd 		syscallarg(const char *) path;
   2454   1.35       cgd 		syscallarg(struct stat *) ub;
   2455  1.335       dsl 	} */
   2456   1.31       cgd 	struct stat sb;
   2457   1.31       cgd 	int error;
   2458   1.31       cgd 
   2459  1.346        ad 	error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
   2460   1.31       cgd 	if (error)
   2461  1.306       dsl 		return error;
   2462  1.306       dsl 	return copyout(&sb, SCARG(uap, ub), sizeof(sb));
   2463   1.31       cgd }
   2464   1.31       cgd 
   2465   1.31       cgd /*
   2466   1.31       cgd  * Get file status; this version does not follow links.
   2467   1.31       cgd  */
   2468   1.31       cgd /* ARGSUSED */
   2469   1.63  christos int
   2470  1.335       dsl sys___lstat30(struct lwp *l, const struct sys___lstat30_args *uap, register_t *retval)
   2471   1.56   thorpej {
   2472  1.335       dsl 	/* {
   2473   1.74       cgd 		syscallarg(const char *) path;
   2474   1.35       cgd 		syscallarg(struct stat *) ub;
   2475  1.335       dsl 	} */
   2476   1.65   mycroft 	struct stat sb;
   2477   1.31       cgd 	int error;
   2478   1.31       cgd 
   2479  1.346        ad 	error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
   2480   1.64       jtc 	if (error)
   2481  1.306       dsl 		return error;
   2482  1.306       dsl 	return copyout(&sb, SCARG(uap, ub), sizeof(sb));
   2483   1.31       cgd }
   2484   1.31       cgd 
   2485   1.31       cgd /*
   2486   1.31       cgd  * Get configurable pathname variables.
   2487   1.31       cgd  */
   2488   1.31       cgd /* ARGSUSED */
   2489   1.63  christos int
   2490  1.335       dsl sys_pathconf(struct lwp *l, const struct sys_pathconf_args *uap, register_t *retval)
   2491   1.56   thorpej {
   2492  1.335       dsl 	/* {
   2493   1.74       cgd 		syscallarg(const char *) path;
   2494   1.35       cgd 		syscallarg(int) name;
   2495  1.335       dsl 	} */
   2496   1.31       cgd 	int error;
   2497   1.31       cgd 	struct nameidata nd;
   2498   1.31       cgd 
   2499  1.310       dsl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
   2500  1.334     pooka 	    SCARG(uap, path));
   2501   1.63  christos 	if ((error = namei(&nd)) != 0)
   2502   1.31       cgd 		return (error);
   2503   1.35       cgd 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
   2504   1.31       cgd 	vput(nd.ni_vp);
   2505   1.31       cgd 	return (error);
   2506   1.31       cgd }
   2507   1.31       cgd 
   2508   1.31       cgd /*
   2509   1.31       cgd  * Return target name of a symbolic link.
   2510   1.31       cgd  */
   2511   1.31       cgd /* ARGSUSED */
   2512   1.63  christos int
   2513  1.335       dsl sys_readlink(struct lwp *l, const struct sys_readlink_args *uap, register_t *retval)
   2514   1.56   thorpej {
   2515  1.335       dsl 	/* {
   2516   1.74       cgd 		syscallarg(const char *) path;
   2517   1.35       cgd 		syscallarg(char *) buf;
   2518  1.115    kleink 		syscallarg(size_t) count;
   2519  1.335       dsl 	} */
   2520  1.155  augustss 	struct vnode *vp;
   2521   1.31       cgd 	struct iovec aiov;
   2522   1.31       cgd 	struct uio auio;
   2523   1.31       cgd 	int error;
   2524   1.31       cgd 	struct nameidata nd;
   2525   1.31       cgd 
   2526  1.310       dsl 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
   2527  1.334     pooka 	    SCARG(uap, path));
   2528   1.63  christos 	if ((error = namei(&nd)) != 0)
   2529   1.31       cgd 		return (error);
   2530   1.31       cgd 	vp = nd.ni_vp;
   2531   1.31       cgd 	if (vp->v_type != VLNK)
   2532   1.31       cgd 		error = EINVAL;
   2533  1.106     enami 	else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
   2534  1.332     pooka 	    (error = VOP_ACCESS(vp, VREAD, l->l_cred)) == 0) {
   2535   1.35       cgd 		aiov.iov_base = SCARG(uap, buf);
   2536   1.35       cgd 		aiov.iov_len = SCARG(uap, count);
   2537   1.31       cgd 		auio.uio_iov = &aiov;
   2538   1.31       cgd 		auio.uio_iovcnt = 1;
   2539   1.31       cgd 		auio.uio_offset = 0;
   2540   1.31       cgd 		auio.uio_rw = UIO_READ;
   2541  1.238      yamt 		KASSERT(l == curlwp);
   2542  1.238      yamt 		auio.uio_vmspace = l->l_proc->p_vmspace;
   2543   1.35       cgd 		auio.uio_resid = SCARG(uap, count);
   2544  1.257        ad 		error = VOP_READLINK(vp, &auio, l->l_cred);
   2545   1.31       cgd 	}
   2546   1.31       cgd 	vput(vp);
   2547   1.35       cgd 	*retval = SCARG(uap, count) - auio.uio_resid;
   2548   1.31       cgd 	return (error);
   2549   1.31       cgd }
   2550   1.31       cgd 
   2551   1.31       cgd /*
   2552   1.31       cgd  * Change flags of a file given a path name.
   2553   1.31       cgd  */
   2554   1.31       cgd /* ARGSUSED */
   2555   1.63  christos int
   2556  1.335       dsl sys_chflags(struct lwp *l, const struct sys_chflags_args *uap, register_t *retval)
   2557   1.56   thorpej {
   2558  1.335       dsl 	/* {
   2559   1.74       cgd 		syscallarg(const char *) path;
   2560   1.74       cgd 		syscallarg(u_long) flags;
   2561  1.335       dsl 	} */
   2562  1.155  augustss 	struct vnode *vp;
   2563   1.31       cgd 	int error;
   2564   1.31       cgd 	struct nameidata nd;
   2565   1.31       cgd 
   2566  1.331     pooka 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
   2567  1.334     pooka 	    SCARG(uap, path));
   2568   1.63  christos 	if ((error = namei(&nd)) != 0)
   2569   1.31       cgd 		return (error);
   2570   1.31       cgd 	vp = nd.ni_vp;
   2571  1.234  christos 	error = change_flags(vp, SCARG(uap, flags), l);
   2572   1.31       cgd 	vput(vp);
   2573   1.31       cgd 	return (error);
   2574   1.31       cgd }
   2575   1.31       cgd 
   2576   1.31       cgd /*
   2577   1.31       cgd  * Change flags of a file given a file descriptor.
   2578   1.31       cgd  */
   2579   1.31       cgd /* ARGSUSED */
   2580   1.63  christos int
   2581  1.335       dsl sys_fchflags(struct lwp *l, const struct sys_fchflags_args *uap, register_t *retval)
   2582   1.56   thorpej {
   2583  1.335       dsl 	/* {
   2584   1.35       cgd 		syscallarg(int) fd;
   2585   1.74       cgd 		syscallarg(u_long) flags;
   2586  1.335       dsl 	} */
   2587   1.31       cgd 	struct vnode *vp;
   2588  1.346        ad 	file_t *fp;
   2589   1.31       cgd 	int error;
   2590   1.31       cgd 
   2591  1.346        ad 	/* fd_getvnode() will use the descriptor for us */
   2592  1.346        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   2593   1.31       cgd 		return (error);
   2594  1.346        ad 	vp = fp->f_data;
   2595  1.234  christos 	error = change_flags(vp, SCARG(uap, flags), l);
   2596  1.113      fvdl 	VOP_UNLOCK(vp, 0);
   2597  1.346        ad 	fd_putfile(SCARG(uap, fd));
   2598  1.156       mrg 	return (error);
   2599  1.156       mrg }
   2600  1.156       mrg 
   2601  1.156       mrg /*
   2602  1.163     enami  * Change flags of a file given a path name; this version does
   2603  1.156       mrg  * not follow links.
   2604  1.156       mrg  */
   2605  1.156       mrg int
   2606  1.335       dsl sys_lchflags(struct lwp *l, const struct sys_lchflags_args *uap, register_t *retval)
   2607  1.156       mrg {
   2608  1.335       dsl 	/* {
   2609  1.156       mrg 		syscallarg(const char *) path;
   2610  1.156       mrg 		syscallarg(u_long) flags;
   2611  1.335       dsl 	} */
   2612  1.163     enami 	struct vnode *vp;
   2613  1.156       mrg 	int error;
   2614  1.156       mrg 	struct nameidata nd;
   2615  1.156       mrg 
   2616  1.331     pooka 	NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE,
   2617  1.334     pooka 	    SCARG(uap, path));
   2618  1.156       mrg 	if ((error = namei(&nd)) != 0)
   2619  1.156       mrg 		return (error);
   2620  1.156       mrg 	vp = nd.ni_vp;
   2621  1.234  christos 	error = change_flags(vp, SCARG(uap, flags), l);
   2622  1.163     enami 	vput(vp);
   2623  1.163     enami 	return (error);
   2624  1.163     enami }
   2625  1.163     enami 
   2626  1.163     enami /*
   2627  1.163     enami  * Common routine to change flags of a file.
   2628  1.163     enami  */
   2629  1.163     enami int
   2630  1.234  christos change_flags(struct vnode *vp, u_long flags, struct lwp *l)
   2631  1.163     enami {
   2632  1.163     enami 	struct vattr vattr;
   2633  1.163     enami 	int error;
   2634  1.163     enami 
   2635  1.156       mrg 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2636  1.163     enami 	/*
   2637  1.163     enami 	 * Non-superusers cannot change the flags on devices, even if they
   2638  1.163     enami 	 * own them.
   2639  1.163     enami 	 */
   2640  1.294      elad 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
   2641  1.332     pooka 		if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
   2642  1.156       mrg 			goto out;
   2643  1.156       mrg 		if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
   2644  1.156       mrg 			error = EINVAL;
   2645  1.156       mrg 			goto out;
   2646  1.156       mrg 		}
   2647  1.156       mrg 	}
   2648  1.156       mrg 	VATTR_NULL(&vattr);
   2649  1.163     enami 	vattr.va_flags = flags;
   2650  1.332     pooka 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
   2651  1.156       mrg out:
   2652   1.31       cgd 	return (error);
   2653   1.31       cgd }
   2654   1.31       cgd 
   2655   1.31       cgd /*
   2656   1.98     enami  * Change mode of a file given path name; this version follows links.
   2657   1.31       cgd  */
   2658   1.31       cgd /* ARGSUSED */
   2659   1.63  christos int
   2660  1.335       dsl sys_chmod(struct lwp *l, const struct sys_chmod_args *uap, register_t *retval)
   2661   1.56   thorpej {
   2662  1.335       dsl 	/* {
   2663   1.74       cgd 		syscallarg(const char *) path;
   2664   1.35       cgd 		syscallarg(int) mode;
   2665  1.335       dsl 	} */
   2666   1.31       cgd 	int error;
   2667   1.31       cgd 	struct nameidata nd;
   2668   1.31       cgd 
   2669  1.331     pooka 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
   2670  1.334     pooka 	    SCARG(uap, path));
   2671   1.63  christos 	if ((error = namei(&nd)) != 0)
   2672   1.31       cgd 		return (error);
   2673   1.97     enami 
   2674  1.234  christos 	error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
   2675   1.97     enami 
   2676   1.97     enami 	vrele(nd.ni_vp);
   2677   1.31       cgd 	return (error);
   2678   1.31       cgd }
   2679   1.31       cgd 
   2680   1.31       cgd /*
   2681   1.31       cgd  * Change mode of a file given a file descriptor.
   2682   1.31       cgd  */
   2683   1.31       cgd /* ARGSUSED */
   2684   1.63  christos int
   2685  1.335       dsl sys_fchmod(struct lwp *l, const struct sys_fchmod_args *uap, register_t *retval)
   2686   1.56   thorpej {
   2687  1.335       dsl 	/* {
   2688   1.35       cgd 		syscallarg(int) fd;
   2689   1.35       cgd 		syscallarg(int) mode;
   2690  1.335       dsl 	} */
   2691  1.346        ad 	file_t *fp;
   2692   1.31       cgd 	int error;
   2693   1.31       cgd 
   2694  1.346        ad 	/* fd_getvnode() will use the descriptor for us */
   2695  1.346        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   2696   1.31       cgd 		return (error);
   2697  1.346        ad 	error = change_mode(fp->f_data, SCARG(uap, mode), l);
   2698  1.346        ad 	fd_putfile(SCARG(uap, fd));
   2699  1.135   thorpej 	return (error);
   2700   1.97     enami }
   2701   1.97     enami 
   2702   1.97     enami /*
   2703   1.98     enami  * Change mode of a file given path name; this version does not follow links.
   2704   1.98     enami  */
   2705   1.98     enami /* ARGSUSED */
   2706   1.98     enami int
   2707  1.335       dsl sys_lchmod(struct lwp *l, const struct sys_lchmod_args *uap, register_t *retval)
   2708   1.98     enami {
   2709  1.335       dsl 	/* {
   2710   1.98     enami 		syscallarg(const char *) path;
   2711   1.98     enami 		syscallarg(int) mode;
   2712  1.335       dsl 	} */
   2713   1.98     enami 	int error;
   2714   1.98     enami 	struct nameidata nd;
   2715   1.98     enami 
   2716  1.331     pooka 	NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE,
   2717  1.334     pooka 	    SCARG(uap, path));
   2718   1.98     enami 	if ((error = namei(&nd)) != 0)
   2719   1.98     enami 		return (error);
   2720   1.98     enami 
   2721  1.234  christos 	error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
   2722   1.98     enami 
   2723   1.98     enami 	vrele(nd.ni_vp);
   2724   1.98     enami 	return (error);
   2725   1.98     enami }
   2726   1.98     enami 
   2727   1.98     enami /*
   2728   1.97     enami  * Common routine to set mode given a vnode.
   2729   1.97     enami  */
   2730   1.97     enami static int
   2731  1.234  christos change_mode(struct vnode *vp, int mode, struct lwp *l)
   2732   1.97     enami {
   2733   1.97     enami 	struct vattr vattr;
   2734   1.97     enami 	int error;
   2735   1.97     enami 
   2736  1.113      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2737  1.113      fvdl 	VATTR_NULL(&vattr);
   2738  1.113      fvdl 	vattr.va_mode = mode & ALLPERMS;
   2739  1.332     pooka 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
   2740  1.113      fvdl 	VOP_UNLOCK(vp, 0);
   2741   1.31       cgd 	return (error);
   2742   1.31       cgd }
   2743   1.31       cgd 
   2744   1.31       cgd /*
   2745   1.98     enami  * Set ownership given a path name; this version follows links.
   2746   1.31       cgd  */
   2747   1.31       cgd /* ARGSUSED */
   2748   1.63  christos int
   2749  1.335       dsl sys_chown(struct lwp *l, const struct sys_chown_args *uap, register_t *retval)
   2750   1.56   thorpej {
   2751  1.335       dsl 	/* {
   2752   1.74       cgd 		syscallarg(const char *) path;
   2753   1.74       cgd 		syscallarg(uid_t) uid;
   2754   1.74       cgd 		syscallarg(gid_t) gid;
   2755  1.335       dsl 	} */
   2756   1.31       cgd 	int error;
   2757   1.31       cgd 	struct nameidata nd;
   2758   1.31       cgd 
   2759  1.331     pooka 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
   2760  1.334     pooka 	    SCARG(uap, path));
   2761   1.63  christos 	if ((error = namei(&nd)) != 0)
   2762   1.31       cgd 		return (error);
   2763   1.86    kleink 
   2764  1.234  christos 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
   2765  1.112    kleink 
   2766  1.112    kleink 	vrele(nd.ni_vp);
   2767  1.112    kleink 	return (error);
   2768  1.112    kleink }
   2769  1.112    kleink 
   2770  1.112    kleink /*
   2771  1.112    kleink  * Set ownership given a path name; this version follows links.
   2772  1.112    kleink  * Provides POSIX semantics.
   2773  1.112    kleink  */
   2774  1.112    kleink /* ARGSUSED */
   2775  1.112    kleink int
   2776  1.335       dsl sys___posix_chown(struct lwp *l, const struct sys___posix_chown_args *uap, register_t *retval)
   2777  1.112    kleink {
   2778  1.335       dsl 	/* {
   2779  1.112    kleink 		syscallarg(const char *) path;
   2780  1.112    kleink 		syscallarg(uid_t) uid;
   2781  1.112    kleink 		syscallarg(gid_t) gid;
   2782  1.335       dsl 	} */
   2783  1.112    kleink 	int error;
   2784  1.112    kleink 	struct nameidata nd;
   2785  1.112    kleink 
   2786  1.331     pooka 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
   2787  1.334     pooka 	    SCARG(uap, path));
   2788  1.112    kleink 	if ((error = namei(&nd)) != 0)
   2789  1.112    kleink 		return (error);
   2790  1.112    kleink 
   2791  1.234  christos 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
   2792   1.86    kleink 
   2793   1.86    kleink 	vrele(nd.ni_vp);
   2794   1.31       cgd 	return (error);
   2795   1.31       cgd }
   2796   1.31       cgd 
   2797   1.31       cgd /*
   2798   1.31       cgd  * Set ownership given a file descriptor.
   2799   1.31       cgd  */
   2800   1.31       cgd /* ARGSUSED */
   2801   1.63  christos int
   2802  1.335       dsl sys_fchown(struct lwp *l, const struct sys_fchown_args *uap, register_t *retval)
   2803   1.56   thorpej {
   2804  1.335       dsl 	/* {
   2805   1.35       cgd 		syscallarg(int) fd;
   2806   1.74       cgd 		syscallarg(uid_t) uid;
   2807   1.74       cgd 		syscallarg(gid_t) gid;
   2808  1.335       dsl 	} */
   2809   1.71   mycroft 	int error;
   2810  1.346        ad 	file_t *fp;
   2811   1.31       cgd 
   2812  1.346        ad 	/* fd_getvnode() will use the descriptor for us */
   2813  1.346        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   2814   1.31       cgd 		return (error);
   2815  1.346        ad 	error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid),
   2816  1.346        ad 	    l, 0);
   2817  1.346        ad 	fd_putfile(SCARG(uap, fd));
   2818  1.135   thorpej 	return (error);
   2819  1.112    kleink }
   2820  1.112    kleink 
   2821  1.112    kleink /*
   2822  1.112    kleink  * Set ownership given a file descriptor, providing POSIX/XPG semantics.
   2823  1.112    kleink  */
   2824  1.112    kleink /* ARGSUSED */
   2825  1.112    kleink int
   2826  1.335       dsl sys___posix_fchown(struct lwp *l, const struct sys___posix_fchown_args *uap, register_t *retval)
   2827  1.112    kleink {
   2828  1.335       dsl 	/* {
   2829  1.112    kleink 		syscallarg(int) fd;
   2830  1.112    kleink 		syscallarg(uid_t) uid;
   2831  1.112    kleink 		syscallarg(gid_t) gid;
   2832  1.335       dsl 	} */
   2833  1.112    kleink 	int error;
   2834  1.346        ad 	file_t *fp;
   2835  1.112    kleink 
   2836  1.346        ad 	/* fd_getvnode() will use the descriptor for us */
   2837  1.346        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   2838  1.112    kleink 		return (error);
   2839  1.346        ad 	error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid),
   2840  1.346        ad 	    l, 1);
   2841  1.346        ad 	fd_putfile(SCARG(uap, fd));
   2842  1.135   thorpej 	return (error);
   2843   1.86    kleink }
   2844   1.86    kleink 
   2845   1.86    kleink /*
   2846   1.98     enami  * Set ownership given a path name; this version does not follow links.
   2847   1.98     enami  */
   2848   1.98     enami /* ARGSUSED */
   2849   1.98     enami int
   2850  1.335       dsl sys_lchown(struct lwp *l, const struct sys_lchown_args *uap, register_t *retval)
   2851   1.98     enami {
   2852  1.335       dsl 	/* {
   2853   1.98     enami 		syscallarg(const char *) path;
   2854   1.98     enami 		syscallarg(uid_t) uid;
   2855   1.98     enami 		syscallarg(gid_t) gid;
   2856  1.335       dsl 	} */
   2857   1.98     enami 	int error;
   2858   1.98     enami 	struct nameidata nd;
   2859   1.98     enami 
   2860  1.331     pooka 	NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE,
   2861  1.334     pooka 	    SCARG(uap, path));
   2862   1.98     enami 	if ((error = namei(&nd)) != 0)
   2863   1.98     enami 		return (error);
   2864   1.98     enami 
   2865  1.234  christos 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
   2866  1.112    kleink 
   2867  1.112    kleink 	vrele(nd.ni_vp);
   2868  1.112    kleink 	return (error);
   2869  1.112    kleink }
   2870  1.112    kleink 
   2871  1.112    kleink /*
   2872  1.112    kleink  * Set ownership given a path name; this version does not follow links.
   2873  1.112    kleink  * Provides POSIX/XPG semantics.
   2874  1.112    kleink  */
   2875  1.112    kleink /* ARGSUSED */
   2876  1.112    kleink int
   2877  1.335       dsl sys___posix_lchown(struct lwp *l, const struct sys___posix_lchown_args *uap, register_t *retval)
   2878  1.112    kleink {
   2879  1.335       dsl 	/* {
   2880  1.112    kleink 		syscallarg(const char *) path;
   2881  1.112    kleink 		syscallarg(uid_t) uid;
   2882  1.112    kleink 		syscallarg(gid_t) gid;
   2883  1.335       dsl 	} */
   2884  1.112    kleink 	int error;
   2885  1.112    kleink 	struct nameidata nd;
   2886  1.112    kleink 
   2887  1.331     pooka 	NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE,
   2888  1.334     pooka 	    SCARG(uap, path));
   2889  1.112    kleink 	if ((error = namei(&nd)) != 0)
   2890  1.112    kleink 		return (error);
   2891  1.112    kleink 
   2892  1.234  christos 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
   2893   1.98     enami 
   2894   1.98     enami 	vrele(nd.ni_vp);
   2895   1.98     enami 	return (error);
   2896   1.98     enami }
   2897   1.98     enami 
   2898   1.98     enami /*
   2899  1.205  junyoung  * Common routine to set ownership given a vnode.
   2900   1.86    kleink  */
   2901   1.86    kleink static int
   2902  1.234  christos change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l,
   2903  1.221   thorpej     int posix_semantics)
   2904   1.86    kleink {
   2905   1.86    kleink 	struct vattr vattr;
   2906  1.112    kleink 	mode_t newmode;
   2907   1.86    kleink 	int error;
   2908   1.86    kleink 
   2909  1.113      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2910  1.332     pooka 	if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
   2911   1.86    kleink 		goto out;
   2912   1.86    kleink 
   2913  1.175   thorpej #define CHANGED(x) ((int)(x) != -1)
   2914  1.112    kleink 	newmode = vattr.va_mode;
   2915  1.112    kleink 	if (posix_semantics) {
   2916  1.112    kleink 		/*
   2917  1.114    kleink 		 * POSIX/XPG semantics: if the caller is not the super-user,
   2918  1.114    kleink 		 * clear set-user-id and set-group-id bits.  Both POSIX and
   2919  1.114    kleink 		 * the XPG consider the behaviour for calls by the super-user
   2920  1.114    kleink 		 * implementation-defined; we leave the set-user-id and set-
   2921  1.114    kleink 		 * group-id settings intact in that case.
   2922  1.112    kleink 		 */
   2923  1.257        ad 		if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
   2924  1.242      elad 				      NULL) != 0)
   2925  1.112    kleink 			newmode &= ~(S_ISUID | S_ISGID);
   2926  1.112    kleink 	} else {
   2927  1.112    kleink 		/*
   2928  1.112    kleink 		 * NetBSD semantics: when changing owner and/or group,
   2929  1.112    kleink 		 * clear the respective bit(s).
   2930  1.112    kleink 		 */
   2931  1.112    kleink 		if (CHANGED(uid))
   2932  1.112    kleink 			newmode &= ~S_ISUID;
   2933  1.112    kleink 		if (CHANGED(gid))
   2934  1.112    kleink 			newmode &= ~S_ISGID;
   2935  1.112    kleink 	}
   2936  1.112    kleink 	/* Update va_mode iff altered. */
   2937  1.112    kleink 	if (vattr.va_mode == newmode)
   2938  1.112    kleink 		newmode = VNOVAL;
   2939  1.205  junyoung 
   2940   1.86    kleink 	VATTR_NULL(&vattr);
   2941  1.175   thorpej 	vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
   2942  1.175   thorpej 	vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
   2943   1.86    kleink 	vattr.va_mode = newmode;
   2944  1.332     pooka 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
   2945  1.112    kleink #undef CHANGED
   2946  1.205  junyoung 
   2947   1.86    kleink out:
   2948  1.113      fvdl 	VOP_UNLOCK(vp, 0);
   2949   1.31       cgd 	return (error);
   2950   1.31       cgd }
   2951   1.31       cgd 
   2952   1.31       cgd /*
   2953   1.98     enami  * Set the access and modification times given a path name; this
   2954   1.98     enami  * version follows links.
   2955   1.31       cgd  */
   2956   1.31       cgd /* ARGSUSED */
   2957   1.63  christos int
   2958  1.335       dsl sys_utimes(struct lwp *l, const struct sys_utimes_args *uap, register_t *retval)
   2959   1.56   thorpej {
   2960  1.335       dsl 	/* {
   2961   1.74       cgd 		syscallarg(const char *) path;
   2962   1.74       cgd 		syscallarg(const struct timeval *) tptr;
   2963  1.335       dsl 	} */
   2964   1.31       cgd 
   2965  1.312       dsl 	return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
   2966  1.346        ad 	    SCARG(uap, tptr), UIO_USERSPACE);
   2967   1.71   mycroft }
   2968   1.71   mycroft 
   2969   1.71   mycroft /*
   2970   1.71   mycroft  * Set the access and modification times given a file descriptor.
   2971   1.71   mycroft  */
   2972   1.71   mycroft /* ARGSUSED */
   2973   1.71   mycroft int
   2974  1.335       dsl sys_futimes(struct lwp *l, const struct sys_futimes_args *uap, register_t *retval)
   2975   1.71   mycroft {
   2976  1.335       dsl 	/* {
   2977   1.71   mycroft 		syscallarg(int) fd;
   2978   1.74       cgd 		syscallarg(const struct timeval *) tptr;
   2979  1.335       dsl 	} */
   2980   1.71   mycroft 	int error;
   2981  1.346        ad 	file_t *fp;
   2982   1.71   mycroft 
   2983  1.346        ad 	/* fd_getvnode() will use the descriptor for us */
   2984  1.346        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   2985   1.96     enami 		return (error);
   2986  1.346        ad 	error = do_sys_utimes(l, fp->f_data, NULL, 0, SCARG(uap, tptr),
   2987  1.346        ad 	    UIO_USERSPACE);
   2988  1.346        ad 	fd_putfile(SCARG(uap, fd));
   2989  1.135   thorpej 	return (error);
   2990   1.98     enami }
   2991   1.98     enami 
   2992   1.98     enami /*
   2993   1.98     enami  * Set the access and modification times given a path name; this
   2994   1.98     enami  * version does not follow links.
   2995   1.98     enami  */
   2996   1.98     enami int
   2997  1.335       dsl sys_lutimes(struct lwp *l, const struct sys_lutimes_args *uap, register_t *retval)
   2998   1.98     enami {
   2999  1.335       dsl 	/* {
   3000   1.98     enami 		syscallarg(const char *) path;
   3001   1.98     enami 		syscallarg(const struct timeval *) tptr;
   3002  1.335       dsl 	} */
   3003   1.98     enami 
   3004  1.312       dsl 	return do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
   3005  1.346        ad 	    SCARG(uap, tptr), UIO_USERSPACE);
   3006   1.97     enami }
   3007   1.97     enami 
   3008   1.97     enami /*
   3009   1.97     enami  * Common routine to set access and modification times given a vnode.
   3010   1.97     enami  */
   3011  1.312       dsl int
   3012  1.312       dsl do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path, int flag,
   3013  1.312       dsl     const struct timeval *tptr, enum uio_seg seg)
   3014   1.97     enami {
   3015   1.97     enami 	struct vattr vattr;
   3016  1.312       dsl 	struct nameidata nd;
   3017   1.97     enami 	int error;
   3018   1.97     enami 
   3019   1.71   mycroft 	VATTR_NULL(&vattr);
   3020   1.97     enami 	if (tptr == NULL) {
   3021  1.233      yamt 		nanotime(&vattr.va_atime);
   3022  1.233      yamt 		vattr.va_mtime = vattr.va_atime;
   3023   1.71   mycroft 		vattr.va_vaflags |= VA_UTIMES_NULL;
   3024   1.71   mycroft 	} else {
   3025  1.233      yamt 		struct timeval tv[2];
   3026  1.233      yamt 
   3027  1.312       dsl 		if (seg != UIO_SYSSPACE) {
   3028  1.312       dsl 			error = copyin(tptr, &tv, sizeof (tv));
   3029  1.312       dsl 			if (error != 0)
   3030  1.312       dsl 				return error;
   3031  1.312       dsl 			tptr = tv;
   3032  1.312       dsl 		}
   3033  1.312       dsl 		TIMEVAL_TO_TIMESPEC(tptr, &vattr.va_atime);
   3034  1.312       dsl 		TIMEVAL_TO_TIMESPEC(tptr + 1, &vattr.va_mtime);
   3035   1.71   mycroft 	}
   3036  1.312       dsl 
   3037  1.312       dsl 	if (vp == NULL) {
   3038  1.334     pooka 		NDINIT(&nd, LOOKUP, flag | TRYEMULROOT, UIO_USERSPACE, path);
   3039  1.312       dsl 		if ((error = namei(&nd)) != 0)
   3040  1.312       dsl 			return (error);
   3041  1.312       dsl 		vp = nd.ni_vp;
   3042  1.312       dsl 	} else
   3043  1.312       dsl 		nd.ni_vp = NULL;
   3044  1.312       dsl 
   3045  1.113      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3046  1.332     pooka 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
   3047  1.113      fvdl 	VOP_UNLOCK(vp, 0);
   3048  1.312       dsl 
   3049  1.312       dsl 	if (nd.ni_vp != NULL)
   3050  1.312       dsl 		vrele(nd.ni_vp);
   3051  1.312       dsl 
   3052   1.31       cgd 	return (error);
   3053   1.31       cgd }
   3054   1.31       cgd 
   3055   1.31       cgd /*
   3056   1.31       cgd  * Truncate a file given its path name.
   3057   1.31       cgd  */
   3058   1.31       cgd /* ARGSUSED */
   3059   1.63  christos int
   3060  1.335       dsl sys_truncate(struct lwp *l, const struct sys_truncate_args *uap, register_t *retval)
   3061   1.56   thorpej {
   3062  1.335       dsl 	/* {
   3063   1.74       cgd 		syscallarg(const char *) path;
   3064   1.35       cgd 		syscallarg(int) pad;
   3065   1.35       cgd 		syscallarg(off_t) length;
   3066  1.335       dsl 	} */
   3067  1.155  augustss 	struct vnode *vp;
   3068   1.31       cgd 	struct vattr vattr;
   3069   1.31       cgd 	int error;
   3070   1.31       cgd 	struct nameidata nd;
   3071   1.31       cgd 
   3072  1.331     pooka 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
   3073  1.334     pooka 	    SCARG(uap, path));
   3074   1.63  christos 	if ((error = namei(&nd)) != 0)
   3075   1.31       cgd 		return (error);
   3076   1.31       cgd 	vp = nd.ni_vp;
   3077  1.113      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3078   1.31       cgd 	if (vp->v_type == VDIR)
   3079   1.31       cgd 		error = EISDIR;
   3080   1.31       cgd 	else if ((error = vn_writechk(vp)) == 0 &&
   3081  1.332     pooka 	    (error = VOP_ACCESS(vp, VWRITE, l->l_cred)) == 0) {
   3082   1.31       cgd 		VATTR_NULL(&vattr);
   3083   1.35       cgd 		vattr.va_size = SCARG(uap, length);
   3084  1.332     pooka 		error = VOP_SETATTR(vp, &vattr, l->l_cred);
   3085   1.31       cgd 	}
   3086   1.31       cgd 	vput(vp);
   3087   1.31       cgd 	return (error);
   3088   1.31       cgd }
   3089   1.31       cgd 
   3090   1.31       cgd /*
   3091   1.31       cgd  * Truncate a file given a file descriptor.
   3092   1.31       cgd  */
   3093   1.31       cgd /* ARGSUSED */
   3094   1.63  christos int
   3095  1.335       dsl sys_ftruncate(struct lwp *l, const struct sys_ftruncate_args *uap, register_t *retval)
   3096   1.56   thorpej {
   3097  1.335       dsl 	/* {
   3098   1.35       cgd 		syscallarg(int) fd;
   3099   1.35       cgd 		syscallarg(int) pad;
   3100   1.35       cgd 		syscallarg(off_t) length;
   3101  1.335       dsl 	} */
   3102   1.31       cgd 	struct vattr vattr;
   3103   1.31       cgd 	struct vnode *vp;
   3104  1.346        ad 	file_t *fp;
   3105   1.31       cgd 	int error;
   3106   1.31       cgd 
   3107  1.346        ad 	/* fd_getvnode() will use the descriptor for us */
   3108  1.346        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3109   1.31       cgd 		return (error);
   3110  1.135   thorpej 	if ((fp->f_flag & FWRITE) == 0) {
   3111  1.135   thorpej 		error = EINVAL;
   3112  1.135   thorpej 		goto out;
   3113  1.135   thorpej 	}
   3114  1.346        ad 	vp = fp->f_data;
   3115  1.113      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3116   1.31       cgd 	if (vp->v_type == VDIR)
   3117   1.31       cgd 		error = EISDIR;
   3118   1.31       cgd 	else if ((error = vn_writechk(vp)) == 0) {
   3119   1.31       cgd 		VATTR_NULL(&vattr);
   3120   1.35       cgd 		vattr.va_size = SCARG(uap, length);
   3121  1.332     pooka 		error = VOP_SETATTR(vp, &vattr, fp->f_cred);
   3122   1.31       cgd 	}
   3123  1.113      fvdl 	VOP_UNLOCK(vp, 0);
   3124  1.135   thorpej  out:
   3125  1.346        ad 	fd_putfile(SCARG(uap, fd));
   3126   1.31       cgd 	return (error);
   3127   1.31       cgd }
   3128   1.31       cgd 
   3129   1.31       cgd /*
   3130   1.31       cgd  * Sync an open file.
   3131   1.31       cgd  */
   3132   1.31       cgd /* ARGSUSED */
   3133   1.63  christos int
   3134  1.335       dsl sys_fsync(struct lwp *l, const struct sys_fsync_args *uap, register_t *retval)
   3135   1.56   thorpej {
   3136  1.335       dsl 	/* {
   3137   1.35       cgd 		syscallarg(int) fd;
   3138  1.335       dsl 	} */
   3139  1.155  augustss 	struct vnode *vp;
   3140  1.346        ad 	file_t *fp;
   3141   1.31       cgd 	int error;
   3142   1.31       cgd 
   3143  1.346        ad 	/* fd_getvnode() will use the descriptor for us */
   3144  1.346        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3145   1.31       cgd 		return (error);
   3146  1.346        ad 	vp = fp->f_data;
   3147  1.113      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3148  1.332     pooka 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0);
   3149  1.327     pooka 	if (error == 0 && bioopsp != NULL &&
   3150  1.148      fvdl 	    vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
   3151  1.329        ad 		(*bioopsp->io_fsync)(vp, 0);
   3152  1.113      fvdl 	VOP_UNLOCK(vp, 0);
   3153  1.346        ad 	fd_putfile(SCARG(uap, fd));
   3154  1.201   thorpej 	return (error);
   3155  1.201   thorpej }
   3156  1.201   thorpej 
   3157  1.201   thorpej /*
   3158  1.201   thorpej  * Sync a range of file data.  API modeled after that found in AIX.
   3159  1.201   thorpej  *
   3160  1.201   thorpej  * FDATASYNC indicates that we need only save enough metadata to be able
   3161  1.201   thorpej  * to re-read the written data.  Note we duplicate AIX's requirement that
   3162  1.201   thorpej  * the file be open for writing.
   3163  1.201   thorpej  */
   3164  1.201   thorpej /* ARGSUSED */
   3165  1.201   thorpej int
   3166  1.335       dsl sys_fsync_range(struct lwp *l, const struct sys_fsync_range_args *uap, register_t *retval)
   3167  1.201   thorpej {
   3168  1.335       dsl 	/* {
   3169  1.201   thorpej 		syscallarg(int) fd;
   3170  1.201   thorpej 		syscallarg(int) flags;
   3171  1.201   thorpej 		syscallarg(off_t) start;
   3172  1.225      cube 		syscallarg(off_t) length;
   3173  1.335       dsl 	} */
   3174  1.201   thorpej 	struct vnode *vp;
   3175  1.346        ad 	file_t *fp;
   3176  1.201   thorpej 	int flags, nflags;
   3177  1.201   thorpej 	off_t s, e, len;
   3178  1.201   thorpej 	int error;
   3179  1.201   thorpej 
   3180  1.346        ad 	/* fd_getvnode() will use the descriptor for us */
   3181  1.346        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3182  1.201   thorpej 		return (error);
   3183  1.201   thorpej 
   3184  1.201   thorpej 	if ((fp->f_flag & FWRITE) == 0) {
   3185  1.293  wrstuden 		error = EBADF;
   3186  1.293  wrstuden 		goto out;
   3187  1.201   thorpej 	}
   3188  1.201   thorpej 
   3189  1.201   thorpej 	flags = SCARG(uap, flags);
   3190  1.201   thorpej 	if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
   3191  1.201   thorpej 	    ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
   3192  1.293  wrstuden 		error = EINVAL;
   3193  1.293  wrstuden 		goto out;
   3194  1.201   thorpej 	}
   3195  1.201   thorpej 	/* Now set up the flags for value(s) to pass to VOP_FSYNC() */
   3196  1.201   thorpej 	if (flags & FDATASYNC)
   3197  1.201   thorpej 		nflags = FSYNC_DATAONLY | FSYNC_WAIT;
   3198  1.201   thorpej 	else
   3199  1.201   thorpej 		nflags = FSYNC_WAIT;
   3200  1.216  wrstuden 	if (flags & FDISKSYNC)
   3201  1.216  wrstuden 		nflags |= FSYNC_CACHE;
   3202  1.201   thorpej 
   3203  1.201   thorpej 	len = SCARG(uap, length);
   3204  1.201   thorpej 	/* If length == 0, we do the whole file, and s = l = 0 will do that */
   3205  1.201   thorpej 	if (len) {
   3206  1.201   thorpej 		s = SCARG(uap, start);
   3207  1.201   thorpej 		e = s + len;
   3208  1.201   thorpej 		if (e < s) {
   3209  1.293  wrstuden 			error = EINVAL;
   3210  1.293  wrstuden 			goto out;
   3211  1.201   thorpej 		}
   3212  1.201   thorpej 	} else {
   3213  1.201   thorpej 		e = 0;
   3214  1.201   thorpej 		s = 0;
   3215  1.201   thorpej 	}
   3216  1.201   thorpej 
   3217  1.346        ad 	vp = fp->f_data;
   3218  1.201   thorpej 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3219  1.332     pooka 	error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e);
   3220  1.201   thorpej 
   3221  1.327     pooka 	if (error == 0 && bioopsp != NULL &&
   3222  1.201   thorpej 	    vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
   3223  1.329        ad 		(*bioopsp->io_fsync)(vp, nflags);
   3224  1.201   thorpej 
   3225  1.201   thorpej 	VOP_UNLOCK(vp, 0);
   3226  1.293  wrstuden out:
   3227  1.346        ad 	fd_putfile(SCARG(uap, fd));
   3228   1.31       cgd 	return (error);
   3229   1.31       cgd }
   3230   1.31       cgd 
   3231   1.31       cgd /*
   3232  1.117    kleink  * Sync the data of an open file.
   3233  1.117    kleink  */
   3234  1.117    kleink /* ARGSUSED */
   3235  1.117    kleink int
   3236  1.335       dsl sys_fdatasync(struct lwp *l, const struct sys_fdatasync_args *uap, register_t *retval)
   3237  1.117    kleink {
   3238  1.335       dsl 	/* {
   3239  1.117    kleink 		syscallarg(int) fd;
   3240  1.335       dsl 	} */
   3241  1.117    kleink 	struct vnode *vp;
   3242  1.346        ad 	file_t *fp;
   3243  1.117    kleink 	int error;
   3244  1.117    kleink 
   3245  1.346        ad 	/* fd_getvnode() will use the descriptor for us */
   3246  1.346        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3247  1.117    kleink 		return (error);
   3248  1.199    kleink 	if ((fp->f_flag & FWRITE) == 0) {
   3249  1.346        ad 		fd_putfile(SCARG(uap, fd));
   3250  1.199    kleink 		return (EBADF);
   3251  1.199    kleink 	}
   3252  1.346        ad 	vp = fp->f_data;
   3253  1.117    kleink 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3254  1.332     pooka 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0);
   3255  1.117    kleink 	VOP_UNLOCK(vp, 0);
   3256  1.346        ad 	fd_putfile(SCARG(uap, fd));
   3257  1.117    kleink 	return (error);
   3258  1.117    kleink }
   3259  1.117    kleink 
   3260  1.117    kleink /*
   3261   1.90    kleink  * Rename files, (standard) BSD semantics frontend.
   3262   1.31       cgd  */
   3263   1.31       cgd /* ARGSUSED */
   3264   1.63  christos int
   3265  1.335       dsl sys_rename(struct lwp *l, const struct sys_rename_args *uap, register_t *retval)
   3266   1.56   thorpej {
   3267  1.335       dsl 	/* {
   3268   1.74       cgd 		syscallarg(const char *) from;
   3269   1.74       cgd 		syscallarg(const char *) to;
   3270  1.335       dsl 	} */
   3271   1.90    kleink 
   3272  1.336        ad 	return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 0));
   3273   1.90    kleink }
   3274   1.90    kleink 
   3275   1.90    kleink /*
   3276   1.90    kleink  * Rename files, POSIX semantics frontend.
   3277   1.90    kleink  */
   3278   1.90    kleink /* ARGSUSED */
   3279   1.90    kleink int
   3280  1.335       dsl sys___posix_rename(struct lwp *l, const struct sys___posix_rename_args *uap, register_t *retval)
   3281   1.90    kleink {
   3282  1.335       dsl 	/* {
   3283   1.90    kleink 		syscallarg(const char *) from;
   3284   1.90    kleink 		syscallarg(const char *) to;
   3285  1.335       dsl 	} */
   3286   1.90    kleink 
   3287  1.336        ad 	return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 1));
   3288   1.90    kleink }
   3289   1.90    kleink 
   3290   1.90    kleink /*
   3291   1.90    kleink  * Rename files.  Source and destination must either both be directories,
   3292   1.90    kleink  * or both not be directories.  If target is a directory, it must be empty.
   3293   1.90    kleink  * If `from' and `to' refer to the same object, the value of the `retain'
   3294   1.90    kleink  * argument is used to determine whether `from' will be
   3295   1.90    kleink  *
   3296   1.90    kleink  * (retain == 0)	deleted unless `from' and `to' refer to the same
   3297   1.90    kleink  *			object in the file system's name space (BSD).
   3298   1.90    kleink  * (retain == 1)	always retained (POSIX).
   3299   1.90    kleink  */
   3300  1.336        ad int
   3301  1.336        ad do_sys_rename(const char *from, const char *to, enum uio_seg seg, int retain)
   3302   1.90    kleink {
   3303  1.155  augustss 	struct vnode *tvp, *fvp, *tdvp;
   3304   1.31       cgd 	struct nameidata fromnd, tond;
   3305  1.344  dholland 	struct mount *fs;
   3306  1.336        ad 	struct lwp *l = curlwp;
   3307  1.234  christos 	struct proc *p;
   3308  1.344  dholland 	uint32_t saveflag;
   3309   1.31       cgd 	int error;
   3310   1.31       cgd 
   3311  1.331     pooka 	NDINIT(&fromnd, DELETE, LOCKPARENT | SAVESTART | TRYEMULROOT,
   3312  1.336        ad 	    seg, from);
   3313   1.63  christos 	if ((error = namei(&fromnd)) != 0)
   3314   1.31       cgd 		return (error);
   3315  1.291     pooka 	if (fromnd.ni_dvp != fromnd.ni_vp)
   3316  1.291     pooka 		VOP_UNLOCK(fromnd.ni_dvp, 0);
   3317   1.31       cgd 	fvp = fromnd.ni_vp;
   3318  1.344  dholland 
   3319  1.344  dholland 	fs = fvp->v_mount;
   3320  1.344  dholland 	error = VFS_RENAMELOCK_ENTER(fs);
   3321  1.344  dholland 	if (error) {
   3322  1.344  dholland 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3323  1.344  dholland 		vrele(fromnd.ni_dvp);
   3324  1.344  dholland 		vrele(fvp);
   3325  1.344  dholland 		goto out1;
   3326  1.344  dholland 	}
   3327  1.344  dholland 
   3328  1.344  dholland 	/*
   3329  1.344  dholland 	 * close, partially, yet another race - ideally we should only
   3330  1.344  dholland 	 * go as far as getting fromnd.ni_dvp before getting the per-fs
   3331  1.344  dholland 	 * lock, and then continue to get fromnd.ni_vp, but we can't do
   3332  1.344  dholland 	 * that with namei as it stands.
   3333  1.344  dholland 	 *
   3334  1.344  dholland 	 * This still won't prevent rmdir from nuking fromnd.ni_vp
   3335  1.344  dholland 	 * under us. The real fix is to get the locks in the right
   3336  1.344  dholland 	 * order and do the lookups in the right places, but that's a
   3337  1.344  dholland 	 * major rototill.
   3338  1.344  dholland 	 *
   3339  1.344  dholland 	 * Preserve the SAVESTART in cn_flags, because who knows what
   3340  1.344  dholland 	 * might happen if we don't.
   3341  1.344  dholland 	 *
   3342  1.344  dholland 	 * Note: this logic (as well as this whole function) is cloned
   3343  1.344  dholland 	 * in nfs_serv.c. Proceed accordingly.
   3344  1.344  dholland 	 */
   3345  1.344  dholland 	vrele(fvp);
   3346  1.348  dholland 	if ((fromnd.ni_cnd.cn_namelen == 1 &&
   3347  1.348  dholland 	     fromnd.ni_cnd.cn_nameptr[0] == '.') ||
   3348  1.348  dholland 	    (fromnd.ni_cnd.cn_namelen == 2 &&
   3349  1.348  dholland 	     fromnd.ni_cnd.cn_nameptr[0] == '.' &&
   3350  1.348  dholland 	     fromnd.ni_cnd.cn_nameptr[1] == '.')) {
   3351  1.348  dholland 		error = EINVAL;
   3352  1.348  dholland 		VFS_RENAMELOCK_EXIT(fs);
   3353  1.348  dholland 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3354  1.348  dholland 		vrele(fromnd.ni_dvp);
   3355  1.348  dholland 		goto out1;
   3356  1.348  dholland 	}
   3357  1.344  dholland 	saveflag = fromnd.ni_cnd.cn_flags & SAVESTART;
   3358  1.344  dholland 	fromnd.ni_cnd.cn_flags &= ~SAVESTART;
   3359  1.344  dholland 	vn_lock(fromnd.ni_dvp, LK_EXCLUSIVE | LK_RETRY);
   3360  1.344  dholland 	error = relookup(fromnd.ni_dvp, &fromnd.ni_vp, &fromnd.ni_cnd);
   3361  1.344  dholland 	fromnd.ni_cnd.cn_flags |= saveflag;
   3362  1.344  dholland 	if (error) {
   3363  1.344  dholland 		VOP_UNLOCK(fromnd.ni_dvp, 0);
   3364  1.344  dholland 		VFS_RENAMELOCK_EXIT(fs);
   3365  1.344  dholland 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3366  1.344  dholland 		vrele(fromnd.ni_dvp);
   3367  1.344  dholland 		goto out1;
   3368  1.344  dholland 	}
   3369  1.344  dholland 	VOP_UNLOCK(fromnd.ni_vp, 0);
   3370  1.344  dholland 	if (fromnd.ni_dvp != fromnd.ni_vp)
   3371  1.344  dholland 		VOP_UNLOCK(fromnd.ni_dvp, 0);
   3372  1.344  dholland 	fvp = fromnd.ni_vp;
   3373  1.344  dholland 
   3374  1.331     pooka 	NDINIT(&tond, RENAME,
   3375  1.331     pooka 	    LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | TRYEMULROOT
   3376  1.331     pooka 	      | (fvp->v_type == VDIR ? CREATEDIR : 0),
   3377  1.336        ad 	    seg, to);
   3378   1.63  christos 	if ((error = namei(&tond)) != 0) {
   3379  1.344  dholland 		VFS_RENAMELOCK_EXIT(fs);
   3380   1.31       cgd 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3381   1.31       cgd 		vrele(fromnd.ni_dvp);
   3382   1.31       cgd 		vrele(fvp);
   3383   1.31       cgd 		goto out1;
   3384   1.31       cgd 	}
   3385   1.31       cgd 	tdvp = tond.ni_dvp;
   3386   1.31       cgd 	tvp = tond.ni_vp;
   3387   1.90    kleink 
   3388   1.31       cgd 	if (tvp != NULL) {
   3389   1.31       cgd 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   3390   1.31       cgd 			error = ENOTDIR;
   3391   1.31       cgd 			goto out;
   3392   1.31       cgd 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   3393   1.31       cgd 			error = EISDIR;
   3394   1.31       cgd 			goto out;
   3395   1.31       cgd 		}
   3396   1.31       cgd 	}
   3397   1.90    kleink 
   3398   1.31       cgd 	if (fvp == tdvp)
   3399   1.31       cgd 		error = EINVAL;
   3400   1.90    kleink 
   3401   1.82    kleink 	/*
   3402   1.90    kleink 	 * Source and destination refer to the same object.
   3403   1.82    kleink 	 */
   3404   1.90    kleink 	if (fvp == tvp) {
   3405   1.90    kleink 		if (retain)
   3406   1.90    kleink 			error = -1;
   3407   1.90    kleink 		else if (fromnd.ni_dvp == tdvp &&
   3408   1.90    kleink 		    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   3409  1.123     perry 		    !memcmp(fromnd.ni_cnd.cn_nameptr,
   3410   1.90    kleink 		          tond.ni_cnd.cn_nameptr,
   3411   1.90    kleink 		          fromnd.ni_cnd.cn_namelen))
   3412   1.82    kleink 		error = -1;
   3413   1.90    kleink 	}
   3414   1.90    kleink 
   3415  1.256      elad #if NVERIEXEC > 0
   3416  1.282      elad 	if (!error) {
   3417  1.313      elad 		char *f1, *f2;
   3418  1.313      elad 
   3419  1.313      elad 		f1 = malloc(fromnd.ni_cnd.cn_namelen + 1, M_TEMP, M_WAITOK);
   3420  1.313      elad 		strlcpy(f1, fromnd.ni_cnd.cn_nameptr, fromnd.ni_cnd.cn_namelen);
   3421  1.313      elad 
   3422  1.313      elad 		f2 = malloc(tond.ni_cnd.cn_namelen + 1, M_TEMP, M_WAITOK);
   3423  1.317       tnn 		strlcpy(f2, tond.ni_cnd.cn_nameptr, tond.ni_cnd.cn_namelen);
   3424  1.282      elad 
   3425  1.315  christos 		error = veriexec_renamechk(l, fvp, f1, tvp, f2);
   3426  1.282      elad 
   3427  1.313      elad 		free(f1, M_TEMP);
   3428  1.313      elad 		free(f2, M_TEMP);
   3429  1.282      elad 	}
   3430  1.256      elad #endif /* NVERIEXEC > 0 */
   3431  1.229      elad 
   3432   1.31       cgd out:
   3433  1.234  christos 	p = l->l_proc;
   3434   1.31       cgd 	if (!error) {
   3435   1.31       cgd 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   3436   1.31       cgd 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   3437  1.344  dholland 		VFS_RENAMELOCK_EXIT(fs);
   3438   1.31       cgd 	} else {
   3439   1.31       cgd 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   3440   1.31       cgd 		if (tdvp == tvp)
   3441   1.31       cgd 			vrele(tdvp);
   3442   1.31       cgd 		else
   3443   1.31       cgd 			vput(tdvp);
   3444   1.31       cgd 		if (tvp)
   3445   1.31       cgd 			vput(tvp);
   3446  1.344  dholland 		VFS_RENAMELOCK_EXIT(fs);
   3447   1.31       cgd 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3448   1.31       cgd 		vrele(fromnd.ni_dvp);
   3449   1.31       cgd 		vrele(fvp);
   3450   1.31       cgd 	}
   3451   1.31       cgd 	vrele(tond.ni_startdir);
   3452  1.161   thorpej 	PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
   3453   1.31       cgd out1:
   3454   1.31       cgd 	if (fromnd.ni_startdir)
   3455   1.31       cgd 		vrele(fromnd.ni_startdir);
   3456  1.161   thorpej 	PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
   3457   1.80    kleink 	return (error == -1 ? 0 : error);
   3458   1.31       cgd }
   3459   1.31       cgd 
   3460   1.31       cgd /*
   3461   1.31       cgd  * Make a directory file.
   3462   1.31       cgd  */
   3463   1.31       cgd /* ARGSUSED */
   3464   1.63  christos int
   3465  1.335       dsl sys_mkdir(struct lwp *l, const struct sys_mkdir_args *uap, register_t *retval)
   3466   1.56   thorpej {
   3467  1.335       dsl 	/* {
   3468   1.74       cgd 		syscallarg(const char *) path;
   3469   1.35       cgd 		syscallarg(int) mode;
   3470  1.335       dsl 	} */
   3471  1.179   thorpej 	struct proc *p = l->l_proc;
   3472  1.155  augustss 	struct vnode *vp;
   3473   1.31       cgd 	struct vattr vattr;
   3474   1.31       cgd 	int error;
   3475   1.31       cgd 	struct nameidata nd;
   3476   1.31       cgd 
   3477  1.310       dsl 	NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, UIO_USERSPACE,
   3478  1.334     pooka 	    SCARG(uap, path));
   3479   1.63  christos 	if ((error = namei(&nd)) != 0)
   3480   1.31       cgd 		return (error);
   3481   1.31       cgd 	vp = nd.ni_vp;
   3482   1.31       cgd 	if (vp != NULL) {
   3483   1.31       cgd 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3484   1.31       cgd 		if (nd.ni_dvp == vp)
   3485   1.31       cgd 			vrele(nd.ni_dvp);
   3486   1.31       cgd 		else
   3487   1.31       cgd 			vput(nd.ni_dvp);
   3488   1.31       cgd 		vrele(vp);
   3489   1.31       cgd 		return (EEXIST);
   3490   1.31       cgd 	}
   3491   1.31       cgd 	VATTR_NULL(&vattr);
   3492   1.31       cgd 	vattr.va_type = VDIR;
   3493  1.328        ad 	/* We will read cwdi->cwdi_cmask unlocked. */
   3494  1.134   thorpej 	vattr.va_mode =
   3495  1.134   thorpej 	    (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
   3496   1.31       cgd 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   3497   1.31       cgd 	if (!error)
   3498   1.31       cgd 		vput(nd.ni_vp);
   3499   1.31       cgd 	return (error);
   3500   1.31       cgd }
   3501   1.31       cgd 
   3502   1.31       cgd /*
   3503   1.31       cgd  * Remove a directory file.
   3504   1.31       cgd  */
   3505   1.31       cgd /* ARGSUSED */
   3506   1.63  christos int
   3507  1.335       dsl sys_rmdir(struct lwp *l, const struct sys_rmdir_args *uap, register_t *retval)
   3508   1.56   thorpej {
   3509  1.335       dsl 	/* {
   3510   1.74       cgd 		syscallarg(const char *) path;
   3511  1.335       dsl 	} */
   3512  1.155  augustss 	struct vnode *vp;
   3513   1.31       cgd 	int error;
   3514   1.31       cgd 	struct nameidata nd;
   3515   1.31       cgd 
   3516  1.310       dsl 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
   3517  1.334     pooka 	    SCARG(uap, path));
   3518   1.63  christos 	if ((error = namei(&nd)) != 0)
   3519   1.31       cgd 		return (error);
   3520   1.31       cgd 	vp = nd.ni_vp;
   3521   1.31       cgd 	if (vp->v_type != VDIR) {
   3522   1.31       cgd 		error = ENOTDIR;
   3523   1.31       cgd 		goto out;
   3524   1.31       cgd 	}
   3525   1.31       cgd 	/*
   3526   1.31       cgd 	 * No rmdir "." please.
   3527   1.31       cgd 	 */
   3528   1.31       cgd 	if (nd.ni_dvp == vp) {
   3529   1.31       cgd 		error = EINVAL;
   3530   1.31       cgd 		goto out;
   3531   1.31       cgd 	}
   3532   1.31       cgd 	/*
   3533   1.31       cgd 	 * The root of a mounted filesystem cannot be deleted.
   3534   1.31       cgd 	 */
   3535  1.350     joerg 	if ((vp->v_vflag & VV_ROOT) != 0 || vp->v_mountedhere != NULL) {
   3536   1.31       cgd 		error = EBUSY;
   3537  1.197   hannken 		goto out;
   3538  1.197   hannken 	}
   3539  1.197   hannken 	error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   3540  1.197   hannken 	return (error);
   3541  1.197   hannken 
   3542  1.197   hannken out:
   3543  1.197   hannken 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3544  1.197   hannken 	if (nd.ni_dvp == vp)
   3545  1.197   hannken 		vrele(nd.ni_dvp);
   3546  1.197   hannken 	else
   3547  1.197   hannken 		vput(nd.ni_dvp);
   3548  1.197   hannken 	vput(vp);
   3549   1.31       cgd 	return (error);
   3550   1.31       cgd }
   3551   1.31       cgd 
   3552   1.31       cgd /*
   3553   1.31       cgd  * Read a block of directory entries in a file system independent format.
   3554   1.31       cgd  */
   3555   1.63  christos int
   3556  1.335       dsl sys___getdents30(struct lwp *l, const struct sys___getdents30_args *uap, register_t *retval)
   3557   1.56   thorpej {
   3558  1.335       dsl 	/* {
   3559   1.35       cgd 		syscallarg(int) fd;
   3560   1.35       cgd 		syscallarg(char *) buf;
   3561  1.101      fvdl 		syscallarg(size_t) count;
   3562  1.335       dsl 	} */
   3563  1.346        ad 	file_t *fp;
   3564  1.101      fvdl 	int error, done;
   3565   1.31       cgd 
   3566  1.346        ad 	/* fd_getvnode() will use the descriptor for us */
   3567  1.346        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3568   1.31       cgd 		return (error);
   3569  1.135   thorpej 	if ((fp->f_flag & FREAD) == 0) {
   3570  1.135   thorpej 		error = EBADF;
   3571  1.135   thorpej 		goto out;
   3572  1.135   thorpej 	}
   3573  1.101      fvdl 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
   3574  1.234  christos 			SCARG(uap, count), &done, l, 0, 0);
   3575  1.325        ad 	ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, buf), done, error);
   3576  1.101      fvdl 	*retval = done;
   3577  1.135   thorpej  out:
   3578  1.346        ad 	fd_putfile(SCARG(uap, fd));
   3579   1.31       cgd 	return (error);
   3580   1.31       cgd }
   3581   1.31       cgd 
   3582   1.31       cgd /*
   3583   1.31       cgd  * Set the mode mask for creation of filesystem nodes.
   3584   1.31       cgd  */
   3585   1.56   thorpej int
   3586  1.335       dsl sys_umask(struct lwp *l, const struct sys_umask_args *uap, register_t *retval)
   3587   1.56   thorpej {
   3588  1.335       dsl 	/* {
   3589  1.103   mycroft 		syscallarg(mode_t) newmask;
   3590  1.335       dsl 	} */
   3591  1.179   thorpej 	struct proc *p = l->l_proc;
   3592  1.134   thorpej 	struct cwdinfo *cwdi;
   3593   1.31       cgd 
   3594  1.328        ad 	/*
   3595  1.328        ad 	 * cwdi->cwdi_cmask will be read unlocked elsewhere.  What's
   3596  1.328        ad 	 * important is that we serialize changes to the mask.  The
   3597  1.328        ad 	 * rw_exit() will issue a write memory barrier on our behalf,
   3598  1.328        ad 	 * and force the changes out to other CPUs (as it must use an
   3599  1.328        ad 	 * atomic operation, draining the local CPU's store buffers).
   3600  1.328        ad 	 */
   3601  1.134   thorpej 	cwdi = p->p_cwdi;
   3602  1.328        ad 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   3603  1.134   thorpej 	*retval = cwdi->cwdi_cmask;
   3604  1.134   thorpej 	cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
   3605  1.328        ad 	rw_exit(&cwdi->cwdi_lock);
   3606  1.328        ad 
   3607   1.31       cgd 	return (0);
   3608   1.31       cgd }
   3609   1.31       cgd 
   3610  1.340      elad int
   3611  1.340      elad dorevoke(struct vnode *vp, kauth_cred_t cred)
   3612  1.340      elad {
   3613  1.340      elad 	struct vattr vattr;
   3614  1.340      elad 	int error;
   3615  1.340      elad 
   3616  1.340      elad 	if ((error = VOP_GETATTR(vp, &vattr, cred)) != 0)
   3617  1.342        ad 		return error;
   3618  1.340      elad 	if (kauth_cred_geteuid(cred) != vattr.va_uid &&
   3619  1.340      elad 	    (error = kauth_authorize_generic(cred,
   3620  1.342        ad 	    KAUTH_GENERIC_ISSUSER, NULL)) == 0)
   3621  1.340      elad 		VOP_REVOKE(vp, REVOKEALL);
   3622  1.340      elad 	return (error);
   3623  1.340      elad }
   3624  1.340      elad 
   3625   1.31       cgd /*
   3626   1.31       cgd  * Void all references to file by ripping underlying filesystem
   3627   1.31       cgd  * away from vnode.
   3628   1.31       cgd  */
   3629   1.31       cgd /* ARGSUSED */
   3630   1.63  christos int
   3631  1.335       dsl sys_revoke(struct lwp *l, const struct sys_revoke_args *uap, register_t *retval)
   3632   1.56   thorpej {
   3633  1.335       dsl 	/* {
   3634   1.74       cgd 		syscallarg(const char *) path;
   3635  1.335       dsl 	} */
   3636  1.155  augustss 	struct vnode *vp;
   3637   1.31       cgd 	int error;
   3638   1.31       cgd 	struct nameidata nd;
   3639   1.31       cgd 
   3640  1.331     pooka 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
   3641  1.334     pooka 	    SCARG(uap, path));
   3642   1.63  christos 	if ((error = namei(&nd)) != 0)
   3643   1.31       cgd 		return (error);
   3644   1.31       cgd 	vp = nd.ni_vp;
   3645  1.340      elad 	error = dorevoke(vp, l->l_cred);
   3646   1.31       cgd 	vrele(vp);
   3647   1.31       cgd 	return (error);
   3648   1.31       cgd }
   3649   1.31       cgd 
   3650   1.31       cgd /*
   3651   1.31       cgd  * Convert a user file descriptor to a kernel file entry.
   3652   1.31       cgd  */
   3653   1.60   mycroft int
   3654  1.346        ad getvnode(int fd, file_t **fpp)
   3655   1.31       cgd {
   3656   1.60   mycroft 	struct vnode *vp;
   3657  1.346        ad 	file_t *fp;
   3658   1.31       cgd 
   3659  1.346        ad 	if ((fp = fd_getfile(fd)) == NULL)
   3660   1.31       cgd 		return (EBADF);
   3661  1.135   thorpej 
   3662  1.135   thorpej 	if (fp->f_type != DTYPE_VNODE) {
   3663  1.346        ad 		fd_putfile(fd);
   3664   1.31       cgd 		return (EINVAL);
   3665  1.135   thorpej 	}
   3666  1.135   thorpej 
   3667  1.346        ad 	vp = fp->f_data;
   3668  1.135   thorpej 	if (vp->v_type == VBAD) {
   3669  1.346        ad 		fd_putfile(fd);
   3670   1.60   mycroft 		return (EBADF);
   3671  1.135   thorpej 	}
   3672  1.135   thorpej 
   3673   1.31       cgd 	*fpp = fp;
   3674   1.31       cgd 	return (0);
   3675   1.31       cgd }
   3676