xref: /netbsd-src/sys/kern/vfs_syscalls.c (revision 4fee23f98c45552038ad6b5bd05124a41302fb01)
1 /*	$NetBSD: vfs_syscalls.c,v 1.430 2011/06/17 14:23:51 manu Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1989, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  * (c) UNIX System Laboratories, Inc.
36  * All or some portions of this file are derived from material licensed
37  * to the University of California by American Telephone and Telegraph
38  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39  * the permission of UNIX System Laboratories, Inc.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)vfs_syscalls.c	8.42 (Berkeley) 7/31/95
66  */
67 
68 /*
69  * Virtual File System System Calls
70  */
71 
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.430 2011/06/17 14:23:51 manu Exp $");
74 
75 #ifdef _KERNEL_OPT
76 #include "opt_fileassoc.h"
77 #include "veriexec.h"
78 #endif
79 
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/namei.h>
83 #include <sys/filedesc.h>
84 #include <sys/kernel.h>
85 #include <sys/file.h>
86 #include <sys/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 (!error) {
1687 		switch (optype) {
1688 		case VOP_WHITEOUT_DESCOFFSET:
1689 			error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1690 			if (error)
1691 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1692 			vput(nd.ni_dvp);
1693 			break;
1694 
1695 		case VOP_MKNOD_DESCOFFSET:
1696 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1697 						&nd.ni_cnd, &vattr);
1698 			if (error == 0)
1699 				vput(nd.ni_vp);
1700 			break;
1701 
1702 		case VOP_CREATE_DESCOFFSET:
1703 			error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
1704 						&nd.ni_cnd, &vattr);
1705 			if (error == 0)
1706 				vput(nd.ni_vp);
1707 			break;
1708 		}
1709 	} else {
1710 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1711 		if (nd.ni_dvp == vp)
1712 			vrele(nd.ni_dvp);
1713 		else
1714 			vput(nd.ni_dvp);
1715 		if (vp)
1716 			vrele(vp);
1717 	}
1718 out:
1719 	pathbuf_stringcopy_put(pb, pathstring);
1720 	pathbuf_destroy(pb);
1721 	return (error);
1722 }
1723 
1724 /*
1725  * Create a named pipe.
1726  */
1727 /* ARGSUSED */
1728 int
1729 sys_mkfifo(struct lwp *l, const struct sys_mkfifo_args *uap, register_t *retval)
1730 {
1731 	/* {
1732 		syscallarg(const char *) path;
1733 		syscallarg(int) mode;
1734 	} */
1735 	struct proc *p = l->l_proc;
1736 	struct vattr vattr;
1737 	int error;
1738 	struct pathbuf *pb;
1739 	struct nameidata nd;
1740 
1741 	error = pathbuf_copyin(SCARG(uap, path), &pb);
1742 	if (error) {
1743 		return error;
1744 	}
1745 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, pb);
1746 	if ((error = namei(&nd)) != 0) {
1747 		pathbuf_destroy(pb);
1748 		return error;
1749 	}
1750 	if (nd.ni_vp != NULL) {
1751 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1752 		if (nd.ni_dvp == nd.ni_vp)
1753 			vrele(nd.ni_dvp);
1754 		else
1755 			vput(nd.ni_dvp);
1756 		vrele(nd.ni_vp);
1757 		pathbuf_destroy(pb);
1758 		return (EEXIST);
1759 	}
1760 	vattr_null(&vattr);
1761 	vattr.va_type = VFIFO;
1762 	/* We will read cwdi->cwdi_cmask unlocked. */
1763 	vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1764 	error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1765 	if (error == 0)
1766 		vput(nd.ni_vp);
1767 	pathbuf_destroy(pb);
1768 	return (error);
1769 }
1770 
1771 /*
1772  * Make a hard file link.
1773  */
1774 /* ARGSUSED */
1775 int
1776 sys_link(struct lwp *l, const struct sys_link_args *uap, register_t *retval)
1777 {
1778 	/* {
1779 		syscallarg(const char *) path;
1780 		syscallarg(const char *) link;
1781 	} */
1782 	struct vnode *vp;
1783 	struct pathbuf *linkpb;
1784 	struct nameidata nd;
1785 	int error;
1786 
1787 	error = namei_simple_user(SCARG(uap, path),
1788 				NSM_FOLLOW_TRYEMULROOT, &vp);
1789 	if (error != 0)
1790 		return (error);
1791 	error = pathbuf_copyin(SCARG(uap, link), &linkpb);
1792 	if (error) {
1793 		goto out1;
1794 	}
1795 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
1796 	if ((error = namei(&nd)) != 0)
1797 		goto out2;
1798 	if (nd.ni_vp) {
1799 		error = EEXIST;
1800 		goto abortop;
1801 	}
1802 	/* Prevent hard links on directories. */
1803 	if (vp->v_type == VDIR) {
1804 		error = EPERM;
1805 		goto abortop;
1806 	}
1807 	/* Prevent cross-mount operation. */
1808 	if (nd.ni_dvp->v_mount != vp->v_mount) {
1809 		error = EXDEV;
1810 		goto abortop;
1811 	}
1812 	error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1813 out2:
1814 	pathbuf_destroy(linkpb);
1815 out1:
1816 	vrele(vp);
1817 	return (error);
1818 abortop:
1819 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1820 	if (nd.ni_dvp == nd.ni_vp)
1821 		vrele(nd.ni_dvp);
1822 	else
1823 		vput(nd.ni_dvp);
1824 	if (nd.ni_vp != NULL)
1825 		vrele(nd.ni_vp);
1826 	goto out2;
1827 }
1828 
1829 int
1830 do_sys_symlink(const char *patharg, const char *link, enum uio_seg seg)
1831 {
1832 	struct proc *p = curproc;
1833 	struct vattr vattr;
1834 	char *path;
1835 	int error;
1836 	struct pathbuf *linkpb;
1837 	struct nameidata nd;
1838 
1839 	path = PNBUF_GET();
1840 	if (seg == UIO_USERSPACE) {
1841 		if ((error = copyinstr(patharg, path, MAXPATHLEN, NULL)) != 0)
1842 			goto out1;
1843 		if ((error = pathbuf_copyin(link, &linkpb)) != 0)
1844 			goto out1;
1845 	} else {
1846 		KASSERT(strlen(patharg) < MAXPATHLEN);
1847 		strcpy(path, patharg);
1848 		linkpb = pathbuf_create(link);
1849 		if (linkpb == NULL) {
1850 			error = ENOMEM;
1851 			goto out1;
1852 		}
1853 	}
1854 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
1855 	if ((error = namei(&nd)) != 0)
1856 		goto out2;
1857 	if (nd.ni_vp) {
1858 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1859 		if (nd.ni_dvp == nd.ni_vp)
1860 			vrele(nd.ni_dvp);
1861 		else
1862 			vput(nd.ni_dvp);
1863 		vrele(nd.ni_vp);
1864 		error = EEXIST;
1865 		goto out2;
1866 	}
1867 	vattr_null(&vattr);
1868 	vattr.va_type = VLNK;
1869 	/* We will read cwdi->cwdi_cmask unlocked. */
1870 	vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
1871 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1872 	if (error == 0)
1873 		vput(nd.ni_vp);
1874 out2:
1875 	pathbuf_destroy(linkpb);
1876 out1:
1877 	PNBUF_PUT(path);
1878 	return (error);
1879 }
1880 
1881 /*
1882  * Make a symbolic link.
1883  */
1884 /* ARGSUSED */
1885 int
1886 sys_symlink(struct lwp *l, const struct sys_symlink_args *uap, register_t *retval)
1887 {
1888 	/* {
1889 		syscallarg(const char *) path;
1890 		syscallarg(const char *) link;
1891 	} */
1892 
1893 	return do_sys_symlink(SCARG(uap, path), SCARG(uap, link),
1894 	    UIO_USERSPACE);
1895 }
1896 
1897 /*
1898  * Delete a whiteout from the filesystem.
1899  */
1900 /* ARGSUSED */
1901 int
1902 sys_undelete(struct lwp *l, const struct sys_undelete_args *uap, register_t *retval)
1903 {
1904 	/* {
1905 		syscallarg(const char *) path;
1906 	} */
1907 	int error;
1908 	struct pathbuf *pb;
1909 	struct nameidata nd;
1910 
1911 	error = pathbuf_copyin(SCARG(uap, path), &pb);
1912 	if (error) {
1913 		return error;
1914 	}
1915 
1916 	NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | TRYEMULROOT, pb);
1917 	error = namei(&nd);
1918 	if (error) {
1919 		pathbuf_destroy(pb);
1920 		return (error);
1921 	}
1922 
1923 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1924 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1925 		if (nd.ni_dvp == nd.ni_vp)
1926 			vrele(nd.ni_dvp);
1927 		else
1928 			vput(nd.ni_dvp);
1929 		if (nd.ni_vp)
1930 			vrele(nd.ni_vp);
1931 		pathbuf_destroy(pb);
1932 		return (EEXIST);
1933 	}
1934 	if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1935 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1936 	vput(nd.ni_dvp);
1937 	pathbuf_destroy(pb);
1938 	return (error);
1939 }
1940 
1941 /*
1942  * Delete a name from the filesystem.
1943  */
1944 /* ARGSUSED */
1945 int
1946 sys_unlink(struct lwp *l, const struct sys_unlink_args *uap, register_t *retval)
1947 {
1948 	/* {
1949 		syscallarg(const char *) path;
1950 	} */
1951 
1952 	return do_sys_unlink(SCARG(uap, path), UIO_USERSPACE);
1953 }
1954 
1955 int
1956 do_sys_unlink(const char *arg, enum uio_seg seg)
1957 {
1958 	struct vnode *vp;
1959 	int error;
1960 	struct pathbuf *pb;
1961 	struct nameidata nd;
1962 	const char *pathstring;
1963 
1964 	error = pathbuf_maybe_copyin(arg, seg, &pb);
1965 	if (error) {
1966 		return error;
1967 	}
1968 	pathstring = pathbuf_stringcopy_get(pb);
1969 	if (pathstring == NULL) {
1970 		pathbuf_destroy(pb);
1971 		return ENOMEM;
1972 	}
1973 
1974 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, pb);
1975 	if ((error = namei(&nd)) != 0)
1976 		goto out;
1977 	vp = nd.ni_vp;
1978 
1979 	/*
1980 	 * The root of a mounted filesystem cannot be deleted.
1981 	 */
1982 	if (vp->v_vflag & VV_ROOT) {
1983 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1984 		if (nd.ni_dvp == vp)
1985 			vrele(nd.ni_dvp);
1986 		else
1987 			vput(nd.ni_dvp);
1988 		vput(vp);
1989 		error = EBUSY;
1990 		goto out;
1991 	}
1992 
1993 #if NVERIEXEC > 0
1994 	/* Handle remove requests for veriexec entries. */
1995 	if ((error = veriexec_removechk(curlwp, nd.ni_vp, pathstring)) != 0) {
1996 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1997 		if (nd.ni_dvp == vp)
1998 			vrele(nd.ni_dvp);
1999 		else
2000 			vput(nd.ni_dvp);
2001 		vput(vp);
2002 		goto out;
2003 	}
2004 #endif /* NVERIEXEC > 0 */
2005 
2006 #ifdef FILEASSOC
2007 	(void)fileassoc_file_delete(vp);
2008 #endif /* FILEASSOC */
2009 	error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2010 out:
2011 	pathbuf_stringcopy_put(pb, pathstring);
2012 	pathbuf_destroy(pb);
2013 	return (error);
2014 }
2015 
2016 /*
2017  * Reposition read/write file offset.
2018  */
2019 int
2020 sys_lseek(struct lwp *l, const struct sys_lseek_args *uap, register_t *retval)
2021 {
2022 	/* {
2023 		syscallarg(int) fd;
2024 		syscallarg(int) pad;
2025 		syscallarg(off_t) offset;
2026 		syscallarg(int) whence;
2027 	} */
2028 	kauth_cred_t cred = l->l_cred;
2029 	file_t *fp;
2030 	struct vnode *vp;
2031 	struct vattr vattr;
2032 	off_t newoff;
2033 	int error, fd;
2034 
2035 	fd = SCARG(uap, fd);
2036 
2037 	if ((fp = fd_getfile(fd)) == NULL)
2038 		return (EBADF);
2039 
2040 	vp = fp->f_data;
2041 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2042 		error = ESPIPE;
2043 		goto out;
2044 	}
2045 
2046 	switch (SCARG(uap, whence)) {
2047 	case SEEK_CUR:
2048 		newoff = fp->f_offset + SCARG(uap, offset);
2049 		break;
2050 	case SEEK_END:
2051 		error = VOP_GETATTR(vp, &vattr, cred);
2052 		if (error) {
2053 			goto out;
2054 		}
2055 		newoff = SCARG(uap, offset) + vattr.va_size;
2056 		break;
2057 	case SEEK_SET:
2058 		newoff = SCARG(uap, offset);
2059 		break;
2060 	default:
2061 		error = EINVAL;
2062 		goto out;
2063 	}
2064 	if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) == 0) {
2065 		*(off_t *)retval = fp->f_offset = newoff;
2066 	}
2067  out:
2068  	fd_putfile(fd);
2069 	return (error);
2070 }
2071 
2072 /*
2073  * Positional read system call.
2074  */
2075 int
2076 sys_pread(struct lwp *l, const struct sys_pread_args *uap, register_t *retval)
2077 {
2078 	/* {
2079 		syscallarg(int) fd;
2080 		syscallarg(void *) buf;
2081 		syscallarg(size_t) nbyte;
2082 		syscallarg(off_t) offset;
2083 	} */
2084 	file_t *fp;
2085 	struct vnode *vp;
2086 	off_t offset;
2087 	int error, fd = SCARG(uap, fd);
2088 
2089 	if ((fp = fd_getfile(fd)) == NULL)
2090 		return (EBADF);
2091 
2092 	if ((fp->f_flag & FREAD) == 0) {
2093 		fd_putfile(fd);
2094 		return (EBADF);
2095 	}
2096 
2097 	vp = fp->f_data;
2098 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2099 		error = ESPIPE;
2100 		goto out;
2101 	}
2102 
2103 	offset = SCARG(uap, offset);
2104 
2105 	/*
2106 	 * XXX This works because no file systems actually
2107 	 * XXX take any action on the seek operation.
2108 	 */
2109 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2110 		goto out;
2111 
2112 	/* dofileread() will unuse the descriptor for us */
2113 	return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2114 	    &offset, 0, retval));
2115 
2116  out:
2117 	fd_putfile(fd);
2118 	return (error);
2119 }
2120 
2121 /*
2122  * Positional scatter read system call.
2123  */
2124 int
2125 sys_preadv(struct lwp *l, const struct sys_preadv_args *uap, register_t *retval)
2126 {
2127 	/* {
2128 		syscallarg(int) fd;
2129 		syscallarg(const struct iovec *) iovp;
2130 		syscallarg(int) iovcnt;
2131 		syscallarg(off_t) offset;
2132 	} */
2133 	off_t offset = SCARG(uap, offset);
2134 
2135 	return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
2136 	    SCARG(uap, iovcnt), &offset, 0, retval);
2137 }
2138 
2139 /*
2140  * Positional write system call.
2141  */
2142 int
2143 sys_pwrite(struct lwp *l, const struct sys_pwrite_args *uap, register_t *retval)
2144 {
2145 	/* {
2146 		syscallarg(int) fd;
2147 		syscallarg(const void *) buf;
2148 		syscallarg(size_t) nbyte;
2149 		syscallarg(off_t) offset;
2150 	} */
2151 	file_t *fp;
2152 	struct vnode *vp;
2153 	off_t offset;
2154 	int error, fd = SCARG(uap, fd);
2155 
2156 	if ((fp = fd_getfile(fd)) == NULL)
2157 		return (EBADF);
2158 
2159 	if ((fp->f_flag & FWRITE) == 0) {
2160 		fd_putfile(fd);
2161 		return (EBADF);
2162 	}
2163 
2164 	vp = fp->f_data;
2165 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2166 		error = ESPIPE;
2167 		goto out;
2168 	}
2169 
2170 	offset = SCARG(uap, offset);
2171 
2172 	/*
2173 	 * XXX This works because no file systems actually
2174 	 * XXX take any action on the seek operation.
2175 	 */
2176 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2177 		goto out;
2178 
2179 	/* dofilewrite() will unuse the descriptor for us */
2180 	return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2181 	    &offset, 0, retval));
2182 
2183  out:
2184 	fd_putfile(fd);
2185 	return (error);
2186 }
2187 
2188 /*
2189  * Positional gather write system call.
2190  */
2191 int
2192 sys_pwritev(struct lwp *l, const struct sys_pwritev_args *uap, register_t *retval)
2193 {
2194 	/* {
2195 		syscallarg(int) fd;
2196 		syscallarg(const struct iovec *) iovp;
2197 		syscallarg(int) iovcnt;
2198 		syscallarg(off_t) offset;
2199 	} */
2200 	off_t offset = SCARG(uap, offset);
2201 
2202 	return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
2203 	    SCARG(uap, iovcnt), &offset, 0, retval);
2204 }
2205 
2206 /*
2207  * Check access permissions.
2208  */
2209 int
2210 sys_access(struct lwp *l, const struct sys_access_args *uap, register_t *retval)
2211 {
2212 	/* {
2213 		syscallarg(const char *) path;
2214 		syscallarg(int) flags;
2215 	} */
2216 	kauth_cred_t cred;
2217 	struct vnode *vp;
2218 	int error, flags;
2219 	struct pathbuf *pb;
2220 	struct nameidata nd;
2221 
2222 	CTASSERT(F_OK == 0);
2223 	if ((SCARG(uap, flags) & ~(R_OK | W_OK | X_OK)) != 0) {
2224 		/* nonsense flags */
2225 		return EINVAL;
2226 	}
2227 
2228 	error = pathbuf_copyin(SCARG(uap, path), &pb);
2229 	if (error) {
2230 		return error;
2231 	}
2232 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
2233 
2234 	/* Override default credentials */
2235 	cred = kauth_cred_dup(l->l_cred);
2236 	kauth_cred_seteuid(cred, kauth_cred_getuid(l->l_cred));
2237 	kauth_cred_setegid(cred, kauth_cred_getgid(l->l_cred));
2238 	nd.ni_cnd.cn_cred = cred;
2239 
2240 	if ((error = namei(&nd)) != 0) {
2241 		pathbuf_destroy(pb);
2242 		goto out;
2243 	}
2244 	vp = nd.ni_vp;
2245 	pathbuf_destroy(pb);
2246 
2247 	/* Flags == 0 means only check for existence. */
2248 	if (SCARG(uap, flags)) {
2249 		flags = 0;
2250 		if (SCARG(uap, flags) & R_OK)
2251 			flags |= VREAD;
2252 		if (SCARG(uap, flags) & W_OK)
2253 			flags |= VWRITE;
2254 		if (SCARG(uap, flags) & X_OK)
2255 			flags |= VEXEC;
2256 
2257 		error = VOP_ACCESS(vp, flags, cred);
2258 		if (!error && (flags & VWRITE))
2259 			error = vn_writechk(vp);
2260 	}
2261 	vput(vp);
2262 out:
2263 	kauth_cred_free(cred);
2264 	return (error);
2265 }
2266 
2267 /*
2268  * Common code for all sys_stat functions, including compat versions.
2269  */
2270 int
2271 do_sys_stat(const char *userpath, unsigned int nd_flags, struct stat *sb)
2272 {
2273 	int error;
2274 	struct pathbuf *pb;
2275 	struct nameidata nd;
2276 
2277 	error = pathbuf_copyin(userpath, &pb);
2278 	if (error) {
2279 		return error;
2280 	}
2281 	NDINIT(&nd, LOOKUP, nd_flags | LOCKLEAF | TRYEMULROOT, pb);
2282 	error = namei(&nd);
2283 	if (error != 0) {
2284 		pathbuf_destroy(pb);
2285 		return error;
2286 	}
2287 	error = vn_stat(nd.ni_vp, sb);
2288 	vput(nd.ni_vp);
2289 	pathbuf_destroy(pb);
2290 	return error;
2291 }
2292 
2293 /*
2294  * Get file status; this version follows links.
2295  */
2296 /* ARGSUSED */
2297 int
2298 sys___stat50(struct lwp *l, const struct sys___stat50_args *uap, register_t *retval)
2299 {
2300 	/* {
2301 		syscallarg(const char *) path;
2302 		syscallarg(struct stat *) ub;
2303 	} */
2304 	struct stat sb;
2305 	int error;
2306 
2307 	error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
2308 	if (error)
2309 		return error;
2310 	return copyout(&sb, SCARG(uap, ub), sizeof(sb));
2311 }
2312 
2313 /*
2314  * Get file status; this version does not follow links.
2315  */
2316 /* ARGSUSED */
2317 int
2318 sys___lstat50(struct lwp *l, const struct sys___lstat50_args *uap, register_t *retval)
2319 {
2320 	/* {
2321 		syscallarg(const char *) path;
2322 		syscallarg(struct stat *) ub;
2323 	} */
2324 	struct stat sb;
2325 	int error;
2326 
2327 	error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
2328 	if (error)
2329 		return error;
2330 	return copyout(&sb, SCARG(uap, ub), sizeof(sb));
2331 }
2332 
2333 /*
2334  * Get configurable pathname variables.
2335  */
2336 /* ARGSUSED */
2337 int
2338 sys_pathconf(struct lwp *l, const struct sys_pathconf_args *uap, register_t *retval)
2339 {
2340 	/* {
2341 		syscallarg(const char *) path;
2342 		syscallarg(int) name;
2343 	} */
2344 	int error;
2345 	struct pathbuf *pb;
2346 	struct nameidata nd;
2347 
2348 	error = pathbuf_copyin(SCARG(uap, path), &pb);
2349 	if (error) {
2350 		return error;
2351 	}
2352 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
2353 	if ((error = namei(&nd)) != 0) {
2354 		pathbuf_destroy(pb);
2355 		return (error);
2356 	}
2357 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
2358 	vput(nd.ni_vp);
2359 	pathbuf_destroy(pb);
2360 	return (error);
2361 }
2362 
2363 /*
2364  * Return target name of a symbolic link.
2365  */
2366 /* ARGSUSED */
2367 int
2368 sys_readlink(struct lwp *l, const struct sys_readlink_args *uap, register_t *retval)
2369 {
2370 	/* {
2371 		syscallarg(const char *) path;
2372 		syscallarg(char *) buf;
2373 		syscallarg(size_t) count;
2374 	} */
2375 	struct vnode *vp;
2376 	struct iovec aiov;
2377 	struct uio auio;
2378 	int error;
2379 	struct pathbuf *pb;
2380 	struct nameidata nd;
2381 
2382 	error = pathbuf_copyin(SCARG(uap, path), &pb);
2383 	if (error) {
2384 		return error;
2385 	}
2386 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | TRYEMULROOT, pb);
2387 	if ((error = namei(&nd)) != 0) {
2388 		pathbuf_destroy(pb);
2389 		return error;
2390 	}
2391 	vp = nd.ni_vp;
2392 	pathbuf_destroy(pb);
2393 	if (vp->v_type != VLNK)
2394 		error = EINVAL;
2395 	else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
2396 	    (error = VOP_ACCESS(vp, VREAD, l->l_cred)) == 0) {
2397 		aiov.iov_base = SCARG(uap, buf);
2398 		aiov.iov_len = SCARG(uap, count);
2399 		auio.uio_iov = &aiov;
2400 		auio.uio_iovcnt = 1;
2401 		auio.uio_offset = 0;
2402 		auio.uio_rw = UIO_READ;
2403 		KASSERT(l == curlwp);
2404 		auio.uio_vmspace = l->l_proc->p_vmspace;
2405 		auio.uio_resid = SCARG(uap, count);
2406 		error = VOP_READLINK(vp, &auio, l->l_cred);
2407 	}
2408 	vput(vp);
2409 	*retval = SCARG(uap, count) - auio.uio_resid;
2410 	return (error);
2411 }
2412 
2413 /*
2414  * Change flags of a file given a path name.
2415  */
2416 /* ARGSUSED */
2417 int
2418 sys_chflags(struct lwp *l, const struct sys_chflags_args *uap, register_t *retval)
2419 {
2420 	/* {
2421 		syscallarg(const char *) path;
2422 		syscallarg(u_long) flags;
2423 	} */
2424 	struct vnode *vp;
2425 	int error;
2426 
2427 	error = namei_simple_user(SCARG(uap, path),
2428 				NSM_FOLLOW_TRYEMULROOT, &vp);
2429 	if (error != 0)
2430 		return (error);
2431 	error = change_flags(vp, SCARG(uap, flags), l);
2432 	vput(vp);
2433 	return (error);
2434 }
2435 
2436 /*
2437  * Change flags of a file given a file descriptor.
2438  */
2439 /* ARGSUSED */
2440 int
2441 sys_fchflags(struct lwp *l, const struct sys_fchflags_args *uap, register_t *retval)
2442 {
2443 	/* {
2444 		syscallarg(int) fd;
2445 		syscallarg(u_long) flags;
2446 	} */
2447 	struct vnode *vp;
2448 	file_t *fp;
2449 	int error;
2450 
2451 	/* fd_getvnode() will use the descriptor for us */
2452 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2453 		return (error);
2454 	vp = fp->f_data;
2455 	error = change_flags(vp, SCARG(uap, flags), l);
2456 	VOP_UNLOCK(vp);
2457 	fd_putfile(SCARG(uap, fd));
2458 	return (error);
2459 }
2460 
2461 /*
2462  * Change flags of a file given a path name; this version does
2463  * not follow links.
2464  */
2465 int
2466 sys_lchflags(struct lwp *l, const struct sys_lchflags_args *uap, register_t *retval)
2467 {
2468 	/* {
2469 		syscallarg(const char *) path;
2470 		syscallarg(u_long) flags;
2471 	} */
2472 	struct vnode *vp;
2473 	int error;
2474 
2475 	error = namei_simple_user(SCARG(uap, path),
2476 				NSM_NOFOLLOW_TRYEMULROOT, &vp);
2477 	if (error != 0)
2478 		return (error);
2479 	error = change_flags(vp, SCARG(uap, flags), l);
2480 	vput(vp);
2481 	return (error);
2482 }
2483 
2484 /*
2485  * Common routine to change flags of a file.
2486  */
2487 int
2488 change_flags(struct vnode *vp, u_long flags, struct lwp *l)
2489 {
2490 	struct vattr vattr;
2491 	int error;
2492 
2493 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2494 	/*
2495 	 * Non-superusers cannot change the flags on devices, even if they
2496 	 * own them.
2497 	 */
2498 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
2499 		if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
2500 			goto out;
2501 		if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2502 			error = EINVAL;
2503 			goto out;
2504 		}
2505 	}
2506 	vattr_null(&vattr);
2507 	vattr.va_flags = flags;
2508 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
2509 out:
2510 	return (error);
2511 }
2512 
2513 /*
2514  * Change mode of a file given path name; this version follows links.
2515  */
2516 /* ARGSUSED */
2517 int
2518 sys_chmod(struct lwp *l, const struct sys_chmod_args *uap, register_t *retval)
2519 {
2520 	/* {
2521 		syscallarg(const char *) path;
2522 		syscallarg(int) mode;
2523 	} */
2524 	int error;
2525 	struct vnode *vp;
2526 
2527 	error = namei_simple_user(SCARG(uap, path),
2528 				NSM_FOLLOW_TRYEMULROOT, &vp);
2529 	if (error != 0)
2530 		return (error);
2531 
2532 	error = change_mode(vp, SCARG(uap, mode), l);
2533 
2534 	vrele(vp);
2535 	return (error);
2536 }
2537 
2538 /*
2539  * Change mode of a file given a file descriptor.
2540  */
2541 /* ARGSUSED */
2542 int
2543 sys_fchmod(struct lwp *l, const struct sys_fchmod_args *uap, register_t *retval)
2544 {
2545 	/* {
2546 		syscallarg(int) fd;
2547 		syscallarg(int) mode;
2548 	} */
2549 	file_t *fp;
2550 	int error;
2551 
2552 	/* fd_getvnode() will use the descriptor for us */
2553 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2554 		return (error);
2555 	error = change_mode(fp->f_data, SCARG(uap, mode), l);
2556 	fd_putfile(SCARG(uap, fd));
2557 	return (error);
2558 }
2559 
2560 /*
2561  * Change mode of a file given path name; this version does not follow links.
2562  */
2563 /* ARGSUSED */
2564 int
2565 sys_lchmod(struct lwp *l, const struct sys_lchmod_args *uap, register_t *retval)
2566 {
2567 	/* {
2568 		syscallarg(const char *) path;
2569 		syscallarg(int) mode;
2570 	} */
2571 	int error;
2572 	struct vnode *vp;
2573 
2574 	error = namei_simple_user(SCARG(uap, path),
2575 				NSM_NOFOLLOW_TRYEMULROOT, &vp);
2576 	if (error != 0)
2577 		return (error);
2578 
2579 	error = change_mode(vp, SCARG(uap, mode), l);
2580 
2581 	vrele(vp);
2582 	return (error);
2583 }
2584 
2585 /*
2586  * Common routine to set mode given a vnode.
2587  */
2588 static int
2589 change_mode(struct vnode *vp, int mode, struct lwp *l)
2590 {
2591 	struct vattr vattr;
2592 	int error;
2593 
2594 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2595 	vattr_null(&vattr);
2596 	vattr.va_mode = mode & ALLPERMS;
2597 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
2598 	VOP_UNLOCK(vp);
2599 	return (error);
2600 }
2601 
2602 /*
2603  * Set ownership given a path name; this version follows links.
2604  */
2605 /* ARGSUSED */
2606 int
2607 sys_chown(struct lwp *l, const struct sys_chown_args *uap, register_t *retval)
2608 {
2609 	/* {
2610 		syscallarg(const char *) path;
2611 		syscallarg(uid_t) uid;
2612 		syscallarg(gid_t) gid;
2613 	} */
2614 	int error;
2615 	struct vnode *vp;
2616 
2617 	error = namei_simple_user(SCARG(uap, path),
2618 				NSM_FOLLOW_TRYEMULROOT, &vp);
2619 	if (error != 0)
2620 		return (error);
2621 
2622 	error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2623 
2624 	vrele(vp);
2625 	return (error);
2626 }
2627 
2628 /*
2629  * Set ownership given a path name; this version follows links.
2630  * Provides POSIX semantics.
2631  */
2632 /* ARGSUSED */
2633 int
2634 sys___posix_chown(struct lwp *l, const struct sys___posix_chown_args *uap, register_t *retval)
2635 {
2636 	/* {
2637 		syscallarg(const char *) path;
2638 		syscallarg(uid_t) uid;
2639 		syscallarg(gid_t) gid;
2640 	} */
2641 	int error;
2642 	struct vnode *vp;
2643 
2644 	error = namei_simple_user(SCARG(uap, path),
2645 				NSM_FOLLOW_TRYEMULROOT, &vp);
2646 	if (error != 0)
2647 		return (error);
2648 
2649 	error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2650 
2651 	vrele(vp);
2652 	return (error);
2653 }
2654 
2655 /*
2656  * Set ownership given a file descriptor.
2657  */
2658 /* ARGSUSED */
2659 int
2660 sys_fchown(struct lwp *l, const struct sys_fchown_args *uap, register_t *retval)
2661 {
2662 	/* {
2663 		syscallarg(int) fd;
2664 		syscallarg(uid_t) uid;
2665 		syscallarg(gid_t) gid;
2666 	} */
2667 	int error;
2668 	file_t *fp;
2669 
2670 	/* fd_getvnode() will use the descriptor for us */
2671 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2672 		return (error);
2673 	error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid),
2674 	    l, 0);
2675 	fd_putfile(SCARG(uap, fd));
2676 	return (error);
2677 }
2678 
2679 /*
2680  * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2681  */
2682 /* ARGSUSED */
2683 int
2684 sys___posix_fchown(struct lwp *l, const struct sys___posix_fchown_args *uap, register_t *retval)
2685 {
2686 	/* {
2687 		syscallarg(int) fd;
2688 		syscallarg(uid_t) uid;
2689 		syscallarg(gid_t) gid;
2690 	} */
2691 	int error;
2692 	file_t *fp;
2693 
2694 	/* fd_getvnode() will use the descriptor for us */
2695 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2696 		return (error);
2697 	error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid),
2698 	    l, 1);
2699 	fd_putfile(SCARG(uap, fd));
2700 	return (error);
2701 }
2702 
2703 /*
2704  * Set ownership given a path name; this version does not follow links.
2705  */
2706 /* ARGSUSED */
2707 int
2708 sys_lchown(struct lwp *l, const struct sys_lchown_args *uap, register_t *retval)
2709 {
2710 	/* {
2711 		syscallarg(const char *) path;
2712 		syscallarg(uid_t) uid;
2713 		syscallarg(gid_t) gid;
2714 	} */
2715 	int error;
2716 	struct vnode *vp;
2717 
2718 	error = namei_simple_user(SCARG(uap, path),
2719 				NSM_NOFOLLOW_TRYEMULROOT, &vp);
2720 	if (error != 0)
2721 		return (error);
2722 
2723 	error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2724 
2725 	vrele(vp);
2726 	return (error);
2727 }
2728 
2729 /*
2730  * Set ownership given a path name; this version does not follow links.
2731  * Provides POSIX/XPG semantics.
2732  */
2733 /* ARGSUSED */
2734 int
2735 sys___posix_lchown(struct lwp *l, const struct sys___posix_lchown_args *uap, register_t *retval)
2736 {
2737 	/* {
2738 		syscallarg(const char *) path;
2739 		syscallarg(uid_t) uid;
2740 		syscallarg(gid_t) gid;
2741 	} */
2742 	int error;
2743 	struct vnode *vp;
2744 
2745 	error = namei_simple_user(SCARG(uap, path),
2746 				NSM_NOFOLLOW_TRYEMULROOT, &vp);
2747 	if (error != 0)
2748 		return (error);
2749 
2750 	error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2751 
2752 	vrele(vp);
2753 	return (error);
2754 }
2755 
2756 /*
2757  * Common routine to set ownership given a vnode.
2758  */
2759 static int
2760 change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l,
2761     int posix_semantics)
2762 {
2763 	struct vattr vattr;
2764 	mode_t newmode;
2765 	int error;
2766 
2767 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2768 	if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
2769 		goto out;
2770 
2771 #define CHANGED(x) ((int)(x) != -1)
2772 	newmode = vattr.va_mode;
2773 	if (posix_semantics) {
2774 		/*
2775 		 * POSIX/XPG semantics: if the caller is not the super-user,
2776 		 * clear set-user-id and set-group-id bits.  Both POSIX and
2777 		 * the XPG consider the behaviour for calls by the super-user
2778 		 * implementation-defined; we leave the set-user-id and set-
2779 		 * group-id settings intact in that case.
2780 		 */
2781 		if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
2782 				      NULL) != 0)
2783 			newmode &= ~(S_ISUID | S_ISGID);
2784 	} else {
2785 		/*
2786 		 * NetBSD semantics: when changing owner and/or group,
2787 		 * clear the respective bit(s).
2788 		 */
2789 		if (CHANGED(uid))
2790 			newmode &= ~S_ISUID;
2791 		if (CHANGED(gid))
2792 			newmode &= ~S_ISGID;
2793 	}
2794 	/* Update va_mode iff altered. */
2795 	if (vattr.va_mode == newmode)
2796 		newmode = VNOVAL;
2797 
2798 	vattr_null(&vattr);
2799 	vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
2800 	vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
2801 	vattr.va_mode = newmode;
2802 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
2803 #undef CHANGED
2804 
2805 out:
2806 	VOP_UNLOCK(vp);
2807 	return (error);
2808 }
2809 
2810 /*
2811  * Set the access and modification times given a path name; this
2812  * version follows links.
2813  */
2814 /* ARGSUSED */
2815 int
2816 sys___utimes50(struct lwp *l, const struct sys___utimes50_args *uap,
2817     register_t *retval)
2818 {
2819 	/* {
2820 		syscallarg(const char *) path;
2821 		syscallarg(const struct timeval *) tptr;
2822 	} */
2823 
2824 	return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
2825 	    SCARG(uap, tptr), UIO_USERSPACE);
2826 }
2827 
2828 /*
2829  * Set the access and modification times given a file descriptor.
2830  */
2831 /* ARGSUSED */
2832 int
2833 sys___futimes50(struct lwp *l, const struct sys___futimes50_args *uap,
2834     register_t *retval)
2835 {
2836 	/* {
2837 		syscallarg(int) fd;
2838 		syscallarg(const struct timeval *) tptr;
2839 	} */
2840 	int error;
2841 	file_t *fp;
2842 
2843 	/* fd_getvnode() will use the descriptor for us */
2844 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2845 		return (error);
2846 	error = do_sys_utimes(l, fp->f_data, NULL, 0, SCARG(uap, tptr),
2847 	    UIO_USERSPACE);
2848 	fd_putfile(SCARG(uap, fd));
2849 	return (error);
2850 }
2851 
2852 /*
2853  * Set the access and modification times given a path name; this
2854  * version does not follow links.
2855  */
2856 int
2857 sys___lutimes50(struct lwp *l, const struct sys___lutimes50_args *uap,
2858     register_t *retval)
2859 {
2860 	/* {
2861 		syscallarg(const char *) path;
2862 		syscallarg(const struct timeval *) tptr;
2863 	} */
2864 
2865 	return do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
2866 	    SCARG(uap, tptr), UIO_USERSPACE);
2867 }
2868 
2869 /*
2870  * Common routine to set access and modification times given a vnode.
2871  */
2872 int
2873 do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path, int flag,
2874     const struct timeval *tptr, enum uio_seg seg)
2875 {
2876 	struct vattr vattr;
2877 	int error, dorele = 0;
2878 	namei_simple_flags_t sflags;
2879 
2880 	bool vanull, setbirthtime;
2881 	struct timespec ts[2];
2882 
2883 	/*
2884 	 * I have checked all callers and they pass either FOLLOW,
2885 	 * NOFOLLOW, or 0 (when they don't pass a path), and NOFOLLOW
2886 	 * is 0. More to the point, they don't pass anything else.
2887 	 * Let's keep it that way at least until the namei interfaces
2888 	 * are fully sanitized.
2889 	 */
2890 	KASSERT(flag == NOFOLLOW || flag == FOLLOW);
2891 	sflags = (flag == FOLLOW) ?
2892 		NSM_FOLLOW_TRYEMULROOT : NSM_NOFOLLOW_TRYEMULROOT;
2893 
2894 	if (tptr == NULL) {
2895 		vanull = true;
2896 		nanotime(&ts[0]);
2897 		ts[1] = ts[0];
2898 	} else {
2899 		struct timeval tv[2];
2900 
2901 		vanull = false;
2902 		if (seg != UIO_SYSSPACE) {
2903 			error = copyin(tptr, tv, sizeof (tv));
2904 			if (error != 0)
2905 				return error;
2906 			tptr = tv;
2907 		}
2908 		TIMEVAL_TO_TIMESPEC(&tptr[0], &ts[0]);
2909 		TIMEVAL_TO_TIMESPEC(&tptr[1], &ts[1]);
2910 	}
2911 
2912 	if (vp == NULL) {
2913 		/* note: SEG describes TPTR, not PATH; PATH is always user */
2914 		error = namei_simple_user(path, sflags, &vp);
2915 		if (error != 0)
2916 			return error;
2917 		dorele = 1;
2918 	}
2919 
2920 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2921 	setbirthtime = (VOP_GETATTR(vp, &vattr, l->l_cred) == 0 &&
2922 	    timespeccmp(&ts[1], &vattr.va_birthtime, <));
2923 	vattr_null(&vattr);
2924 	vattr.va_atime = ts[0];
2925 	vattr.va_mtime = ts[1];
2926 	if (setbirthtime)
2927 		vattr.va_birthtime = ts[1];
2928 	if (vanull)
2929 		vattr.va_vaflags |= VA_UTIMES_NULL;
2930 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
2931 	VOP_UNLOCK(vp);
2932 
2933 	if (dorele != 0)
2934 		vrele(vp);
2935 
2936 	return error;
2937 }
2938 
2939 /*
2940  * Truncate a file given its path name.
2941  */
2942 /* ARGSUSED */
2943 int
2944 sys_truncate(struct lwp *l, const struct sys_truncate_args *uap, register_t *retval)
2945 {
2946 	/* {
2947 		syscallarg(const char *) path;
2948 		syscallarg(int) pad;
2949 		syscallarg(off_t) length;
2950 	} */
2951 	struct vnode *vp;
2952 	struct vattr vattr;
2953 	int error;
2954 
2955 	error = namei_simple_user(SCARG(uap, path),
2956 				NSM_FOLLOW_TRYEMULROOT, &vp);
2957 	if (error != 0)
2958 		return (error);
2959 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2960 	if (vp->v_type == VDIR)
2961 		error = EISDIR;
2962 	else if ((error = vn_writechk(vp)) == 0 &&
2963 	    (error = VOP_ACCESS(vp, VWRITE, l->l_cred)) == 0) {
2964 		vattr_null(&vattr);
2965 		vattr.va_size = SCARG(uap, length);
2966 		error = VOP_SETATTR(vp, &vattr, l->l_cred);
2967 	}
2968 	vput(vp);
2969 	return (error);
2970 }
2971 
2972 /*
2973  * Truncate a file given a file descriptor.
2974  */
2975 /* ARGSUSED */
2976 int
2977 sys_ftruncate(struct lwp *l, const struct sys_ftruncate_args *uap, register_t *retval)
2978 {
2979 	/* {
2980 		syscallarg(int) fd;
2981 		syscallarg(int) pad;
2982 		syscallarg(off_t) length;
2983 	} */
2984 	struct vattr vattr;
2985 	struct vnode *vp;
2986 	file_t *fp;
2987 	int error;
2988 
2989 	/* fd_getvnode() will use the descriptor for us */
2990 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2991 		return (error);
2992 	if ((fp->f_flag & FWRITE) == 0) {
2993 		error = EINVAL;
2994 		goto out;
2995 	}
2996 	vp = fp->f_data;
2997 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2998 	if (vp->v_type == VDIR)
2999 		error = EISDIR;
3000 	else if ((error = vn_writechk(vp)) == 0) {
3001 		vattr_null(&vattr);
3002 		vattr.va_size = SCARG(uap, length);
3003 		error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3004 	}
3005 	VOP_UNLOCK(vp);
3006  out:
3007 	fd_putfile(SCARG(uap, fd));
3008 	return (error);
3009 }
3010 
3011 /*
3012  * Sync an open file.
3013  */
3014 /* ARGSUSED */
3015 int
3016 sys_fsync(struct lwp *l, const struct sys_fsync_args *uap, register_t *retval)
3017 {
3018 	/* {
3019 		syscallarg(int) fd;
3020 	} */
3021 	struct vnode *vp;
3022 	file_t *fp;
3023 	int error;
3024 
3025 	/* fd_getvnode() will use the descriptor for us */
3026 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3027 		return (error);
3028 	vp = fp->f_data;
3029 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3030 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0);
3031 	VOP_UNLOCK(vp);
3032 	fd_putfile(SCARG(uap, fd));
3033 	return (error);
3034 }
3035 
3036 /*
3037  * Sync a range of file data.  API modeled after that found in AIX.
3038  *
3039  * FDATASYNC indicates that we need only save enough metadata to be able
3040  * to re-read the written data.  Note we duplicate AIX's requirement that
3041  * the file be open for writing.
3042  */
3043 /* ARGSUSED */
3044 int
3045 sys_fsync_range(struct lwp *l, const struct sys_fsync_range_args *uap, register_t *retval)
3046 {
3047 	/* {
3048 		syscallarg(int) fd;
3049 		syscallarg(int) flags;
3050 		syscallarg(off_t) start;
3051 		syscallarg(off_t) length;
3052 	} */
3053 	struct vnode *vp;
3054 	file_t *fp;
3055 	int flags, nflags;
3056 	off_t s, e, len;
3057 	int error;
3058 
3059 	/* fd_getvnode() will use the descriptor for us */
3060 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3061 		return (error);
3062 
3063 	if ((fp->f_flag & FWRITE) == 0) {
3064 		error = EBADF;
3065 		goto out;
3066 	}
3067 
3068 	flags = SCARG(uap, flags);
3069 	if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
3070 	    ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
3071 		error = EINVAL;
3072 		goto out;
3073 	}
3074 	/* Now set up the flags for value(s) to pass to VOP_FSYNC() */
3075 	if (flags & FDATASYNC)
3076 		nflags = FSYNC_DATAONLY | FSYNC_WAIT;
3077 	else
3078 		nflags = FSYNC_WAIT;
3079 	if (flags & FDISKSYNC)
3080 		nflags |= FSYNC_CACHE;
3081 
3082 	len = SCARG(uap, length);
3083 	/* If length == 0, we do the whole file, and s = e = 0 will do that */
3084 	if (len) {
3085 		s = SCARG(uap, start);
3086 		e = s + len;
3087 		if (e < s) {
3088 			error = EINVAL;
3089 			goto out;
3090 		}
3091 	} else {
3092 		e = 0;
3093 		s = 0;
3094 	}
3095 
3096 	vp = fp->f_data;
3097 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3098 	error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e);
3099 	VOP_UNLOCK(vp);
3100 out:
3101 	fd_putfile(SCARG(uap, fd));
3102 	return (error);
3103 }
3104 
3105 /*
3106  * Sync the data of an open file.
3107  */
3108 /* ARGSUSED */
3109 int
3110 sys_fdatasync(struct lwp *l, const struct sys_fdatasync_args *uap, register_t *retval)
3111 {
3112 	/* {
3113 		syscallarg(int) fd;
3114 	} */
3115 	struct vnode *vp;
3116 	file_t *fp;
3117 	int error;
3118 
3119 	/* fd_getvnode() will use the descriptor for us */
3120 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3121 		return (error);
3122 	if ((fp->f_flag & FWRITE) == 0) {
3123 		fd_putfile(SCARG(uap, fd));
3124 		return (EBADF);
3125 	}
3126 	vp = fp->f_data;
3127 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3128 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0);
3129 	VOP_UNLOCK(vp);
3130 	fd_putfile(SCARG(uap, fd));
3131 	return (error);
3132 }
3133 
3134 /*
3135  * Rename files, (standard) BSD semantics frontend.
3136  */
3137 /* ARGSUSED */
3138 int
3139 sys_rename(struct lwp *l, const struct sys_rename_args *uap, register_t *retval)
3140 {
3141 	/* {
3142 		syscallarg(const char *) from;
3143 		syscallarg(const char *) to;
3144 	} */
3145 
3146 	return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 0));
3147 }
3148 
3149 /*
3150  * Rename files, POSIX semantics frontend.
3151  */
3152 /* ARGSUSED */
3153 int
3154 sys___posix_rename(struct lwp *l, const struct sys___posix_rename_args *uap, register_t *retval)
3155 {
3156 	/* {
3157 		syscallarg(const char *) from;
3158 		syscallarg(const char *) to;
3159 	} */
3160 
3161 	return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 1));
3162 }
3163 
3164 /*
3165  * Rename files.  Source and destination must either both be directories,
3166  * or both not be directories.  If target is a directory, it must be empty.
3167  * If `from' and `to' refer to the same object, the value of the `retain'
3168  * argument is used to determine whether `from' will be
3169  *
3170  * (retain == 0)	deleted unless `from' and `to' refer to the same
3171  *			object in the file system's name space (BSD).
3172  * (retain == 1)	always retained (POSIX).
3173  */
3174 int
3175 do_sys_rename(const char *from, const char *to, enum uio_seg seg, int retain)
3176 {
3177 	struct vnode *tvp, *fvp, *tdvp;
3178 	struct pathbuf *frompb, *topb;
3179 	struct nameidata fromnd, tond;
3180 	struct mount *fs;
3181 	int error;
3182 
3183 	error = pathbuf_maybe_copyin(from, seg, &frompb);
3184 	if (error) {
3185 		return error;
3186 	}
3187 	error = pathbuf_maybe_copyin(to, seg, &topb);
3188 	if (error) {
3189 		pathbuf_destroy(frompb);
3190 		return error;
3191 	}
3192 
3193 	NDINIT(&fromnd, DELETE, LOCKPARENT | TRYEMULROOT | INRENAME,
3194 	    frompb);
3195 	if ((error = namei(&fromnd)) != 0) {
3196 		pathbuf_destroy(frompb);
3197 		pathbuf_destroy(topb);
3198 		return (error);
3199 	}
3200 	if (fromnd.ni_dvp != fromnd.ni_vp)
3201 		VOP_UNLOCK(fromnd.ni_dvp);
3202 	fvp = fromnd.ni_vp;
3203 
3204 	fs = fvp->v_mount;
3205 	error = VFS_RENAMELOCK_ENTER(fs);
3206 	if (error) {
3207 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3208 		vrele(fromnd.ni_dvp);
3209 		vrele(fvp);
3210 		goto out1;
3211 	}
3212 
3213 	/*
3214 	 * close, partially, yet another race - ideally we should only
3215 	 * go as far as getting fromnd.ni_dvp before getting the per-fs
3216 	 * lock, and then continue to get fromnd.ni_vp, but we can't do
3217 	 * that with namei as it stands.
3218 	 *
3219 	 * This still won't prevent rmdir from nuking fromnd.ni_vp
3220 	 * under us. The real fix is to get the locks in the right
3221 	 * order and do the lookups in the right places, but that's a
3222 	 * major rototill.
3223 	 *
3224 	 * Note: this logic (as well as this whole function) is cloned
3225 	 * in nfs_serv.c. Proceed accordingly.
3226 	 */
3227 	vrele(fvp);
3228 	if ((fromnd.ni_cnd.cn_namelen == 1 &&
3229 	     fromnd.ni_cnd.cn_nameptr[0] == '.') ||
3230 	    (fromnd.ni_cnd.cn_namelen == 2 &&
3231 	     fromnd.ni_cnd.cn_nameptr[0] == '.' &&
3232 	     fromnd.ni_cnd.cn_nameptr[1] == '.')) {
3233 		error = EINVAL;
3234 		VFS_RENAMELOCK_EXIT(fs);
3235 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3236 		vrele(fromnd.ni_dvp);
3237 		goto out1;
3238 	}
3239 	vn_lock(fromnd.ni_dvp, LK_EXCLUSIVE | LK_RETRY);
3240 	error = relookup(fromnd.ni_dvp, &fromnd.ni_vp, &fromnd.ni_cnd, 0);
3241 	if (error) {
3242 		VOP_UNLOCK(fromnd.ni_dvp);
3243 		VFS_RENAMELOCK_EXIT(fs);
3244 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3245 		vrele(fromnd.ni_dvp);
3246 		goto out1;
3247 	}
3248 	VOP_UNLOCK(fromnd.ni_vp);
3249 	if (fromnd.ni_dvp != fromnd.ni_vp)
3250 		VOP_UNLOCK(fromnd.ni_dvp);
3251 	fvp = fromnd.ni_vp;
3252 
3253 	NDINIT(&tond, RENAME,
3254 	    LOCKPARENT | LOCKLEAF | NOCACHE | TRYEMULROOT
3255 	      | INRENAME | (fvp->v_type == VDIR ? CREATEDIR : 0),
3256 	    topb);
3257 	if ((error = namei(&tond)) != 0) {
3258 		VFS_RENAMELOCK_EXIT(fs);
3259 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3260 		vrele(fromnd.ni_dvp);
3261 		vrele(fvp);
3262 		goto out1;
3263 	}
3264 	tdvp = tond.ni_dvp;
3265 	tvp = tond.ni_vp;
3266 
3267 	if (tvp != NULL) {
3268 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3269 			error = ENOTDIR;
3270 			goto out;
3271 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3272 			error = EISDIR;
3273 			goto out;
3274 		}
3275 	}
3276 
3277 	if (fvp == tdvp)
3278 		error = EINVAL;
3279 
3280 	/*
3281 	 * Source and destination refer to the same object.
3282 	 */
3283 	if (fvp == tvp) {
3284 		if (retain)
3285 			error = -1;
3286 		else if (fromnd.ni_dvp == tdvp &&
3287 		    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
3288 		    !memcmp(fromnd.ni_cnd.cn_nameptr,
3289 		          tond.ni_cnd.cn_nameptr,
3290 		          fromnd.ni_cnd.cn_namelen))
3291 		error = -1;
3292 	}
3293 	/*
3294 	 * Prevent cross-mount operation.
3295 	 */
3296 	if (error == 0) {
3297 		if (tond.ni_dvp->v_mount != fromnd.ni_dvp->v_mount) {
3298 			error = EXDEV;
3299 		}
3300 	}
3301 #if NVERIEXEC > 0
3302 	if (!error) {
3303 		char *f1, *f2;
3304 		size_t f1_len;
3305 		size_t f2_len;
3306 
3307 		f1_len = fromnd.ni_cnd.cn_namelen + 1;
3308 		f1 = kmem_alloc(f1_len, KM_SLEEP);
3309 		strlcpy(f1, fromnd.ni_cnd.cn_nameptr, f1_len);
3310 
3311 		f2_len = tond.ni_cnd.cn_namelen + 1;
3312 		f2 = kmem_alloc(f2_len, KM_SLEEP);
3313 		strlcpy(f2, tond.ni_cnd.cn_nameptr, f2_len);
3314 
3315 		error = veriexec_renamechk(curlwp, fvp, f1, tvp, f2);
3316 
3317 		kmem_free(f1, f1_len);
3318 		kmem_free(f2, f2_len);
3319 	}
3320 #endif /* NVERIEXEC > 0 */
3321 
3322 out:
3323 	if (!error) {
3324 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3325 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3326 		VFS_RENAMELOCK_EXIT(fs);
3327 	} else {
3328 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
3329 		if (tdvp == tvp)
3330 			vrele(tdvp);
3331 		else
3332 			vput(tdvp);
3333 		if (tvp)
3334 			vput(tvp);
3335 		VFS_RENAMELOCK_EXIT(fs);
3336 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3337 		vrele(fromnd.ni_dvp);
3338 		vrele(fvp);
3339 	}
3340 out1:
3341 	pathbuf_destroy(frompb);
3342 	pathbuf_destroy(topb);
3343 	return (error == -1 ? 0 : error);
3344 }
3345 
3346 /*
3347  * Make a directory file.
3348  */
3349 /* ARGSUSED */
3350 int
3351 sys_mkdir(struct lwp *l, const struct sys_mkdir_args *uap, register_t *retval)
3352 {
3353 	/* {
3354 		syscallarg(const char *) path;
3355 		syscallarg(int) mode;
3356 	} */
3357 
3358 	return do_sys_mkdir(SCARG(uap, path), SCARG(uap, mode), UIO_USERSPACE);
3359 }
3360 
3361 int
3362 do_sys_mkdir(const char *path, mode_t mode, enum uio_seg seg)
3363 {
3364 	struct proc *p = curlwp->l_proc;
3365 	struct vnode *vp;
3366 	struct vattr vattr;
3367 	int error;
3368 	struct pathbuf *pb;
3369 	struct nameidata nd;
3370 
3371 	/* XXX bollocks, should pass in a pathbuf */
3372 	error = pathbuf_maybe_copyin(path, seg, &pb);
3373 	if (error) {
3374 		return error;
3375 	}
3376 
3377 	NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, pb);
3378 	if ((error = namei(&nd)) != 0) {
3379 		pathbuf_destroy(pb);
3380 		return (error);
3381 	}
3382 	vp = nd.ni_vp;
3383 	if (vp != NULL) {
3384 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3385 		if (nd.ni_dvp == vp)
3386 			vrele(nd.ni_dvp);
3387 		else
3388 			vput(nd.ni_dvp);
3389 		vrele(vp);
3390 		pathbuf_destroy(pb);
3391 		return (EEXIST);
3392 	}
3393 	vattr_null(&vattr);
3394 	vattr.va_type = VDIR;
3395 	/* We will read cwdi->cwdi_cmask unlocked. */
3396 	vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
3397 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3398 	if (!error)
3399 		vput(nd.ni_vp);
3400 	pathbuf_destroy(pb);
3401 	return (error);
3402 }
3403 
3404 /*
3405  * Remove a directory file.
3406  */
3407 /* ARGSUSED */
3408 int
3409 sys_rmdir(struct lwp *l, const struct sys_rmdir_args *uap, register_t *retval)
3410 {
3411 	/* {
3412 		syscallarg(const char *) path;
3413 	} */
3414 	struct vnode *vp;
3415 	int error;
3416 	struct pathbuf *pb;
3417 	struct nameidata nd;
3418 
3419 	error = pathbuf_copyin(SCARG(uap, path), &pb);
3420 	if (error) {
3421 		return error;
3422 	}
3423 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, pb);
3424 	if ((error = namei(&nd)) != 0) {
3425 		pathbuf_destroy(pb);
3426 		return error;
3427 	}
3428 	vp = nd.ni_vp;
3429 	if (vp->v_type != VDIR) {
3430 		error = ENOTDIR;
3431 		goto out;
3432 	}
3433 	/*
3434 	 * No rmdir "." please.
3435 	 */
3436 	if (nd.ni_dvp == vp) {
3437 		error = EINVAL;
3438 		goto out;
3439 	}
3440 	/*
3441 	 * The root of a mounted filesystem cannot be deleted.
3442 	 */
3443 	if ((vp->v_vflag & VV_ROOT) != 0 || vp->v_mountedhere != NULL) {
3444 		error = EBUSY;
3445 		goto out;
3446 	}
3447 	error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3448 	pathbuf_destroy(pb);
3449 	return (error);
3450 
3451 out:
3452 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3453 	if (nd.ni_dvp == vp)
3454 		vrele(nd.ni_dvp);
3455 	else
3456 		vput(nd.ni_dvp);
3457 	vput(vp);
3458 	pathbuf_destroy(pb);
3459 	return (error);
3460 }
3461 
3462 /*
3463  * Read a block of directory entries in a file system independent format.
3464  */
3465 int
3466 sys___getdents30(struct lwp *l, const struct sys___getdents30_args *uap, register_t *retval)
3467 {
3468 	/* {
3469 		syscallarg(int) fd;
3470 		syscallarg(char *) buf;
3471 		syscallarg(size_t) count;
3472 	} */
3473 	file_t *fp;
3474 	int error, done;
3475 
3476 	/* fd_getvnode() will use the descriptor for us */
3477 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3478 		return (error);
3479 	if ((fp->f_flag & FREAD) == 0) {
3480 		error = EBADF;
3481 		goto out;
3482 	}
3483 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
3484 			SCARG(uap, count), &done, l, 0, 0);
3485 	ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, buf), done, error);
3486 	*retval = done;
3487  out:
3488 	fd_putfile(SCARG(uap, fd));
3489 	return (error);
3490 }
3491 
3492 /*
3493  * Set the mode mask for creation of filesystem nodes.
3494  */
3495 int
3496 sys_umask(struct lwp *l, const struct sys_umask_args *uap, register_t *retval)
3497 {
3498 	/* {
3499 		syscallarg(mode_t) newmask;
3500 	} */
3501 	struct proc *p = l->l_proc;
3502 	struct cwdinfo *cwdi;
3503 
3504 	/*
3505 	 * cwdi->cwdi_cmask will be read unlocked elsewhere.  What's
3506 	 * important is that we serialize changes to the mask.  The
3507 	 * rw_exit() will issue a write memory barrier on our behalf,
3508 	 * and force the changes out to other CPUs (as it must use an
3509 	 * atomic operation, draining the local CPU's store buffers).
3510 	 */
3511 	cwdi = p->p_cwdi;
3512 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
3513 	*retval = cwdi->cwdi_cmask;
3514 	cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
3515 	rw_exit(&cwdi->cwdi_lock);
3516 
3517 	return (0);
3518 }
3519 
3520 int
3521 dorevoke(struct vnode *vp, kauth_cred_t cred)
3522 {
3523 	struct vattr vattr;
3524 	int error;
3525 
3526 	if ((error = VOP_GETATTR(vp, &vattr, cred)) != 0)
3527 		return error;
3528 	if (kauth_cred_geteuid(cred) == vattr.va_uid ||
3529 	    (error = kauth_authorize_generic(cred,
3530 	    KAUTH_GENERIC_ISSUSER, NULL)) == 0)
3531 		VOP_REVOKE(vp, REVOKEALL);
3532 	return (error);
3533 }
3534 
3535 /*
3536  * Void all references to file by ripping underlying filesystem
3537  * away from vnode.
3538  */
3539 /* ARGSUSED */
3540 int
3541 sys_revoke(struct lwp *l, const struct sys_revoke_args *uap, register_t *retval)
3542 {
3543 	/* {
3544 		syscallarg(const char *) path;
3545 	} */
3546 	struct vnode *vp;
3547 	int error;
3548 
3549 	error = namei_simple_user(SCARG(uap, path),
3550 				NSM_FOLLOW_TRYEMULROOT, &vp);
3551 	if (error != 0)
3552 		return (error);
3553 	error = dorevoke(vp, l->l_cred);
3554 	vrele(vp);
3555 	return (error);
3556 }
3557