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