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