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