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