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