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