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