xref: /freebsd/sys/kern/vfs_syscalls.c (revision 92f340d137ba5d6db7610ba1dae35842e2c9c8ea)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 #include "opt_capsicum.h"
39 #include "opt_ktrace.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #ifdef COMPAT_FREEBSD11
44 #include <sys/abi_compat.h>
45 #endif
46 #include <sys/bio.h>
47 #include <sys/buf.h>
48 #include <sys/capsicum.h>
49 #include <sys/disk.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
53 #include <sys/sysproto.h>
54 #include <sys/namei.h>
55 #include <sys/filedesc.h>
56 #include <sys/kernel.h>
57 #include <sys/fcntl.h>
58 #include <sys/file.h>
59 #include <sys/filio.h>
60 #include <sys/limits.h>
61 #include <sys/linker.h>
62 #include <sys/rwlock.h>
63 #include <sys/sdt.h>
64 #include <sys/stat.h>
65 #include <sys/sx.h>
66 #include <sys/unistd.h>
67 #include <sys/vnode.h>
68 #include <sys/priv.h>
69 #include <sys/proc.h>
70 #include <sys/dirent.h>
71 #include <sys/jail.h>
72 #include <sys/syscallsubr.h>
73 #include <sys/sysctl.h>
74 #ifdef KTRACE
75 #include <sys/ktrace.h>
76 #endif
77 
78 #include <machine/stdarg.h>
79 
80 #include <security/audit/audit.h>
81 #include <security/mac/mac_framework.h>
82 
83 #include <vm/vm.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
86 #include <vm/vnode_pager.h>
87 #include <vm/uma.h>
88 
89 #include <fs/devfs/devfs.h>
90 
91 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
92 
93 static int kern_chflagsat(struct thread *td, int fd, const char *path,
94     enum uio_seg pathseg, u_long flags, int atflag);
95 static int setfflags(struct thread *td, struct vnode *, u_long);
96 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
97 static int getutimens(const struct timespec *, enum uio_seg,
98     struct timespec *, int *);
99 static int setutimes(struct thread *td, struct vnode *,
100     const struct timespec *, int, int);
101 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
102     struct thread *td);
103 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
104     enum uio_seg pathseg, fhandle_t *fhp);
105 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
106     size_t count, struct thread *td);
107 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
108     const char *path, enum uio_seg segflag);
109 
110 uint64_t
111 at2cnpflags(u_int at_flags, u_int mask)
112 {
113 	uint64_t res;
114 
115 	MPASS((at_flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)) !=
116 	    (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW));
117 
118 	res = 0;
119 	at_flags &= mask;
120 	if ((at_flags & AT_RESOLVE_BENEATH) != 0)
121 		res |= RBENEATH;
122 	if ((at_flags & AT_SYMLINK_FOLLOW) != 0)
123 		res |= FOLLOW;
124 	/* NOFOLLOW is pseudo flag */
125 	if ((mask & AT_SYMLINK_NOFOLLOW) != 0) {
126 		res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
127 		    FOLLOW;
128 	}
129 	if ((mask & AT_EMPTY_PATH) != 0 && (at_flags & AT_EMPTY_PATH) != 0)
130 		res |= EMPTYPATH;
131 	return (res);
132 }
133 
134 int
135 kern_sync(struct thread *td)
136 {
137 	struct mount *mp, *nmp;
138 	int save;
139 
140 	mtx_lock(&mountlist_mtx);
141 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
142 		if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
143 			nmp = TAILQ_NEXT(mp, mnt_list);
144 			continue;
145 		}
146 		if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
147 		    vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
148 			save = curthread_pflags_set(TDP_SYNCIO);
149 			vfs_periodic(mp, MNT_NOWAIT);
150 			VFS_SYNC(mp, MNT_NOWAIT);
151 			curthread_pflags_restore(save);
152 			vn_finished_write(mp);
153 		}
154 		mtx_lock(&mountlist_mtx);
155 		nmp = TAILQ_NEXT(mp, mnt_list);
156 		vfs_unbusy(mp);
157 	}
158 	mtx_unlock(&mountlist_mtx);
159 	return (0);
160 }
161 
162 /*
163  * Sync each mounted filesystem.
164  */
165 #ifndef _SYS_SYSPROTO_H_
166 struct sync_args {
167 	int     dummy;
168 };
169 #endif
170 /* ARGSUSED */
171 int
172 sys_sync(struct thread *td, struct sync_args *uap)
173 {
174 
175 	return (kern_sync(td));
176 }
177 
178 /*
179  * Change filesystem quotas.
180  */
181 #ifndef _SYS_SYSPROTO_H_
182 struct quotactl_args {
183 	char *path;
184 	int cmd;
185 	int uid;
186 	caddr_t arg;
187 };
188 #endif
189 int
190 sys_quotactl(struct thread *td, struct quotactl_args *uap)
191 {
192 	struct mount *mp;
193 	struct nameidata nd;
194 	int error;
195 	bool mp_busy;
196 
197 	AUDIT_ARG_CMD(uap->cmd);
198 	AUDIT_ARG_UID(uap->uid);
199 	if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
200 		return (EPERM);
201 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
202 	    uap->path);
203 	if ((error = namei(&nd)) != 0)
204 		return (error);
205 	NDFREE_PNBUF(&nd);
206 	mp = nd.ni_vp->v_mount;
207 	vfs_ref(mp);
208 	vput(nd.ni_vp);
209 	error = vfs_busy(mp, 0);
210 	if (error != 0) {
211 		vfs_rel(mp);
212 		return (error);
213 	}
214 	mp_busy = true;
215 	error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, &mp_busy);
216 
217 	/*
218 	 * Since quota on/off operations typically need to open quota
219 	 * files, the implementation may need to unbusy the mount point
220 	 * before calling into namei.  Otherwise, unmount might be
221 	 * started between two vfs_busy() invocations (first is ours,
222 	 * second is from mount point cross-walk code in lookup()),
223 	 * causing deadlock.
224 	 *
225 	 * Avoid unbusying mp if the implementation indicates it has
226 	 * already done so.
227 	 */
228 	if (mp_busy)
229 		vfs_unbusy(mp);
230 	vfs_rel(mp);
231 	return (error);
232 }
233 
234 /*
235  * Used by statfs conversion routines to scale the block size up if
236  * necessary so that all of the block counts are <= 'max_size'.  Note
237  * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
238  * value of 'n'.
239  */
240 void
241 statfs_scale_blocks(struct statfs *sf, long max_size)
242 {
243 	uint64_t count;
244 	int shift;
245 
246 	KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
247 
248 	/*
249 	 * Attempt to scale the block counts to give a more accurate
250 	 * overview to userland of the ratio of free space to used
251 	 * space.  To do this, find the largest block count and compute
252 	 * a divisor that lets it fit into a signed integer <= max_size.
253 	 */
254 	if (sf->f_bavail < 0)
255 		count = -sf->f_bavail;
256 	else
257 		count = sf->f_bavail;
258 	count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
259 	if (count <= max_size)
260 		return;
261 
262 	count >>= flsl(max_size);
263 	shift = 0;
264 	while (count > 0) {
265 		shift++;
266 		count >>=1;
267 	}
268 
269 	sf->f_bsize <<= shift;
270 	sf->f_blocks >>= shift;
271 	sf->f_bfree >>= shift;
272 	sf->f_bavail >>= shift;
273 }
274 
275 static int
276 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
277 {
278 	int error;
279 
280 	if (mp == NULL)
281 		return (EBADF);
282 	error = vfs_busy(mp, 0);
283 	vfs_rel(mp);
284 	if (error != 0)
285 		return (error);
286 #ifdef MAC
287 	error = mac_mount_check_stat(td->td_ucred, mp);
288 	if (error != 0)
289 		goto out;
290 #endif
291 	error = VFS_STATFS(mp, buf);
292 	if (error != 0)
293 		goto out;
294 	if (priv_check_cred_vfs_generation(td->td_ucred)) {
295 		buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
296 		prison_enforce_statfs(td->td_ucred, mp, buf);
297 	}
298 out:
299 	vfs_unbusy(mp);
300 	return (error);
301 }
302 
303 /*
304  * Get filesystem statistics.
305  */
306 #ifndef _SYS_SYSPROTO_H_
307 struct statfs_args {
308 	char *path;
309 	struct statfs *buf;
310 };
311 #endif
312 int
313 sys_statfs(struct thread *td, struct statfs_args *uap)
314 {
315 	struct statfs *sfp;
316 	int error;
317 
318 	sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
319 	error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
320 	if (error == 0)
321 		error = copyout(sfp, uap->buf, sizeof(struct statfs));
322 	free(sfp, M_STATFS);
323 	return (error);
324 }
325 
326 int
327 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
328     struct statfs *buf)
329 {
330 	struct mount *mp;
331 	struct nameidata nd;
332 	int error;
333 
334 	NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path);
335 	error = namei(&nd);
336 	if (error != 0)
337 		return (error);
338 	NDFREE_PNBUF(&nd);
339 	mp = vfs_ref_from_vp(nd.ni_vp);
340 	vrele(nd.ni_vp);
341 	return (kern_do_statfs(td, mp, buf));
342 }
343 
344 /*
345  * Get filesystem statistics.
346  */
347 #ifndef _SYS_SYSPROTO_H_
348 struct fstatfs_args {
349 	int fd;
350 	struct statfs *buf;
351 };
352 #endif
353 int
354 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
355 {
356 	struct statfs *sfp;
357 	int error;
358 
359 	sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
360 	error = kern_fstatfs(td, uap->fd, sfp);
361 	if (error == 0)
362 		error = copyout(sfp, uap->buf, sizeof(struct statfs));
363 	free(sfp, M_STATFS);
364 	return (error);
365 }
366 
367 int
368 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
369 {
370 	struct file *fp;
371 	struct mount *mp;
372 	struct vnode *vp;
373 	int error;
374 
375 	AUDIT_ARG_FD(fd);
376 	error = getvnode_path(td, fd, &cap_fstatfs_rights, &fp);
377 	if (error != 0)
378 		return (error);
379 	vp = fp->f_vnode;
380 #ifdef AUDIT
381 	if (AUDITING_TD(td)) {
382 		vn_lock(vp, LK_SHARED | LK_RETRY);
383 		AUDIT_ARG_VNODE1(vp);
384 		VOP_UNLOCK(vp);
385 	}
386 #endif
387 	mp = vfs_ref_from_vp(vp);
388 	fdrop(fp, td);
389 	return (kern_do_statfs(td, mp, buf));
390 }
391 
392 /*
393  * Get statistics on all filesystems.
394  */
395 #ifndef _SYS_SYSPROTO_H_
396 struct getfsstat_args {
397 	struct statfs *buf;
398 	long bufsize;
399 	int mode;
400 };
401 #endif
402 int
403 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
404 {
405 	size_t count;
406 	int error;
407 
408 	if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
409 		return (EINVAL);
410 	error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
411 	    UIO_USERSPACE, uap->mode);
412 	if (error == 0)
413 		td->td_retval[0] = count;
414 	return (error);
415 }
416 
417 /*
418  * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
419  *	The caller is responsible for freeing memory which will be allocated
420  *	in '*buf'.
421  */
422 int
423 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
424     size_t *countp, enum uio_seg bufseg, int mode)
425 {
426 	struct mount *mp, *nmp;
427 	struct statfs *sfsp, *sp, *sptmp, *tofree;
428 	size_t count, maxcount;
429 	int error;
430 
431 	switch (mode) {
432 	case MNT_WAIT:
433 	case MNT_NOWAIT:
434 		break;
435 	default:
436 		if (bufseg == UIO_SYSSPACE)
437 			*buf = NULL;
438 		return (EINVAL);
439 	}
440 restart:
441 	maxcount = bufsize / sizeof(struct statfs);
442 	if (bufsize == 0) {
443 		sfsp = NULL;
444 		tofree = NULL;
445 	} else if (bufseg == UIO_USERSPACE) {
446 		sfsp = *buf;
447 		tofree = NULL;
448 	} else /* if (bufseg == UIO_SYSSPACE) */ {
449 		count = 0;
450 		mtx_lock(&mountlist_mtx);
451 		TAILQ_FOREACH(mp, &mountlist, mnt_list) {
452 			count++;
453 		}
454 		mtx_unlock(&mountlist_mtx);
455 		if (maxcount > count)
456 			maxcount = count;
457 		tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
458 		    M_STATFS, M_WAITOK);
459 	}
460 
461 	count = 0;
462 
463 	/*
464 	 * If there is no target buffer they only want the count.
465 	 *
466 	 * This could be TAILQ_FOREACH but it is open-coded to match the original
467 	 * code below.
468 	 */
469 	if (sfsp == NULL) {
470 		mtx_lock(&mountlist_mtx);
471 		for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
472 			if (prison_canseemount(td->td_ucred, mp) != 0) {
473 				nmp = TAILQ_NEXT(mp, mnt_list);
474 				continue;
475 			}
476 #ifdef MAC
477 			if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
478 				nmp = TAILQ_NEXT(mp, mnt_list);
479 				continue;
480 			}
481 #endif
482 			count++;
483 			nmp = TAILQ_NEXT(mp, mnt_list);
484 		}
485 		mtx_unlock(&mountlist_mtx);
486 		*countp = count;
487 		return (0);
488 	}
489 
490 	/*
491 	 * They want the entire thing.
492 	 *
493 	 * Short-circuit the corner case of no room for anything, avoids
494 	 * relocking below.
495 	 */
496 	if (maxcount < 1) {
497 		goto out;
498 	}
499 
500 	mtx_lock(&mountlist_mtx);
501 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
502 		if (prison_canseemount(td->td_ucred, mp) != 0) {
503 			nmp = TAILQ_NEXT(mp, mnt_list);
504 			continue;
505 		}
506 #ifdef MAC
507 		if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
508 			nmp = TAILQ_NEXT(mp, mnt_list);
509 			continue;
510 		}
511 #endif
512 		if (mode == MNT_WAIT) {
513 			if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
514 				/*
515 				 * If vfs_busy() failed, and MBF_NOWAIT
516 				 * wasn't passed, then the mp is gone.
517 				 * Furthermore, because of MBF_MNTLSTLOCK,
518 				 * the mountlist_mtx was dropped.  We have
519 				 * no other choice than to start over.
520 				 */
521 				mtx_unlock(&mountlist_mtx);
522 				free(tofree, M_STATFS);
523 				goto restart;
524 			}
525 		} else {
526 			if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
527 				nmp = TAILQ_NEXT(mp, mnt_list);
528 				continue;
529 			}
530 		}
531 		sp = &mp->mnt_stat;
532 		/*
533 		 * If MNT_NOWAIT is specified, do not refresh
534 		 * the fsstat cache.
535 		 */
536 		if (mode != MNT_NOWAIT) {
537 			error = VFS_STATFS(mp, sp);
538 			if (error != 0) {
539 				mtx_lock(&mountlist_mtx);
540 				nmp = TAILQ_NEXT(mp, mnt_list);
541 				vfs_unbusy(mp);
542 				continue;
543 			}
544 		}
545 		if (priv_check_cred_vfs_generation(td->td_ucred)) {
546 			sptmp = malloc(sizeof(struct statfs), M_STATFS,
547 			    M_WAITOK);
548 			*sptmp = *sp;
549 			sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
550 			prison_enforce_statfs(td->td_ucred, mp, sptmp);
551 			sp = sptmp;
552 		} else
553 			sptmp = NULL;
554 		if (bufseg == UIO_SYSSPACE) {
555 			bcopy(sp, sfsp, sizeof(*sp));
556 			free(sptmp, M_STATFS);
557 		} else /* if (bufseg == UIO_USERSPACE) */ {
558 			error = copyout(sp, sfsp, sizeof(*sp));
559 			free(sptmp, M_STATFS);
560 			if (error != 0) {
561 				vfs_unbusy(mp);
562 				return (error);
563 			}
564 		}
565 		sfsp++;
566 		count++;
567 
568 		if (count == maxcount) {
569 			vfs_unbusy(mp);
570 			goto out;
571 		}
572 
573 		mtx_lock(&mountlist_mtx);
574 		nmp = TAILQ_NEXT(mp, mnt_list);
575 		vfs_unbusy(mp);
576 	}
577 	mtx_unlock(&mountlist_mtx);
578 out:
579 	*countp = count;
580 	return (0);
581 }
582 
583 #ifdef COMPAT_FREEBSD4
584 /*
585  * Get old format filesystem statistics.
586  */
587 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
588 
589 #ifndef _SYS_SYSPROTO_H_
590 struct freebsd4_statfs_args {
591 	char *path;
592 	struct ostatfs *buf;
593 };
594 #endif
595 int
596 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
597 {
598 	struct ostatfs osb;
599 	struct statfs *sfp;
600 	int error;
601 
602 	sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
603 	error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
604 	if (error == 0) {
605 		freebsd4_cvtstatfs(sfp, &osb);
606 		error = copyout(&osb, uap->buf, sizeof(osb));
607 	}
608 	free(sfp, M_STATFS);
609 	return (error);
610 }
611 
612 /*
613  * Get filesystem statistics.
614  */
615 #ifndef _SYS_SYSPROTO_H_
616 struct freebsd4_fstatfs_args {
617 	int fd;
618 	struct ostatfs *buf;
619 };
620 #endif
621 int
622 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
623 {
624 	struct ostatfs osb;
625 	struct statfs *sfp;
626 	int error;
627 
628 	sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
629 	error = kern_fstatfs(td, uap->fd, sfp);
630 	if (error == 0) {
631 		freebsd4_cvtstatfs(sfp, &osb);
632 		error = copyout(&osb, uap->buf, sizeof(osb));
633 	}
634 	free(sfp, M_STATFS);
635 	return (error);
636 }
637 
638 /*
639  * Get statistics on all filesystems.
640  */
641 #ifndef _SYS_SYSPROTO_H_
642 struct freebsd4_getfsstat_args {
643 	struct ostatfs *buf;
644 	long bufsize;
645 	int mode;
646 };
647 #endif
648 int
649 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
650 {
651 	struct statfs *buf, *sp;
652 	struct ostatfs osb;
653 	size_t count, size;
654 	int error;
655 
656 	if (uap->bufsize < 0)
657 		return (EINVAL);
658 	count = uap->bufsize / sizeof(struct ostatfs);
659 	if (count > SIZE_MAX / sizeof(struct statfs))
660 		return (EINVAL);
661 	size = count * sizeof(struct statfs);
662 	error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
663 	    uap->mode);
664 	if (error == 0)
665 		td->td_retval[0] = count;
666 	if (size != 0) {
667 		sp = buf;
668 		while (count != 0 && error == 0) {
669 			freebsd4_cvtstatfs(sp, &osb);
670 			error = copyout(&osb, uap->buf, sizeof(osb));
671 			sp++;
672 			uap->buf++;
673 			count--;
674 		}
675 		free(buf, M_STATFS);
676 	}
677 	return (error);
678 }
679 
680 /*
681  * Implement fstatfs() for (NFS) file handles.
682  */
683 #ifndef _SYS_SYSPROTO_H_
684 struct freebsd4_fhstatfs_args {
685 	struct fhandle *u_fhp;
686 	struct ostatfs *buf;
687 };
688 #endif
689 int
690 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
691 {
692 	struct ostatfs osb;
693 	struct statfs *sfp;
694 	fhandle_t fh;
695 	int error;
696 
697 	error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
698 	if (error != 0)
699 		return (error);
700 	sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
701 	error = kern_fhstatfs(td, fh, sfp);
702 	if (error == 0) {
703 		freebsd4_cvtstatfs(sfp, &osb);
704 		error = copyout(&osb, uap->buf, sizeof(osb));
705 	}
706 	free(sfp, M_STATFS);
707 	return (error);
708 }
709 
710 /*
711  * Convert a new format statfs structure to an old format statfs structure.
712  */
713 static void
714 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
715 {
716 
717 	statfs_scale_blocks(nsp, LONG_MAX);
718 	bzero(osp, sizeof(*osp));
719 	osp->f_bsize = nsp->f_bsize;
720 	osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
721 	osp->f_blocks = nsp->f_blocks;
722 	osp->f_bfree = nsp->f_bfree;
723 	osp->f_bavail = nsp->f_bavail;
724 	osp->f_files = MIN(nsp->f_files, LONG_MAX);
725 	osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
726 	osp->f_owner = nsp->f_owner;
727 	osp->f_type = nsp->f_type;
728 	osp->f_flags = nsp->f_flags;
729 	osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
730 	osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
731 	osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
732 	osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
733 	strlcpy(osp->f_fstypename, nsp->f_fstypename,
734 	    MIN(MFSNAMELEN, OMFSNAMELEN));
735 	strlcpy(osp->f_mntonname, nsp->f_mntonname,
736 	    MIN(MNAMELEN, OMNAMELEN));
737 	strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
738 	    MIN(MNAMELEN, OMNAMELEN));
739 	osp->f_fsid = nsp->f_fsid;
740 }
741 #endif /* COMPAT_FREEBSD4 */
742 
743 #if defined(COMPAT_FREEBSD11)
744 /*
745  * Get old format filesystem statistics.
746  */
747 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
748 
749 int
750 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
751 {
752 	struct freebsd11_statfs osb;
753 	struct statfs *sfp;
754 	int error;
755 
756 	sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
757 	error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
758 	if (error == 0) {
759 		freebsd11_cvtstatfs(sfp, &osb);
760 		error = copyout(&osb, uap->buf, sizeof(osb));
761 	}
762 	free(sfp, M_STATFS);
763 	return (error);
764 }
765 
766 /*
767  * Get filesystem statistics.
768  */
769 int
770 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
771 {
772 	struct freebsd11_statfs osb;
773 	struct statfs *sfp;
774 	int error;
775 
776 	sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
777 	error = kern_fstatfs(td, uap->fd, sfp);
778 	if (error == 0) {
779 		freebsd11_cvtstatfs(sfp, &osb);
780 		error = copyout(&osb, uap->buf, sizeof(osb));
781 	}
782 	free(sfp, M_STATFS);
783 	return (error);
784 }
785 
786 /*
787  * Get statistics on all filesystems.
788  */
789 int
790 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
791 {
792 	return (kern_freebsd11_getfsstat(td, uap->buf, uap->bufsize, uap->mode));
793 }
794 
795 int
796 kern_freebsd11_getfsstat(struct thread *td, struct freebsd11_statfs * ubuf,
797     long bufsize, int mode)
798 {
799 	struct freebsd11_statfs osb;
800 	struct statfs *buf, *sp;
801 	size_t count, size;
802 	int error;
803 
804 	if (bufsize < 0)
805 		return (EINVAL);
806 
807 	count = bufsize / sizeof(struct ostatfs);
808 	size = count * sizeof(struct statfs);
809 	error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, mode);
810 	if (error == 0)
811 		td->td_retval[0] = count;
812 	if (size > 0) {
813 		sp = buf;
814 		while (count > 0 && error == 0) {
815 			freebsd11_cvtstatfs(sp, &osb);
816 			error = copyout(&osb, ubuf, sizeof(osb));
817 			sp++;
818 			ubuf++;
819 			count--;
820 		}
821 		free(buf, M_STATFS);
822 	}
823 	return (error);
824 }
825 
826 /*
827  * Implement fstatfs() for (NFS) file handles.
828  */
829 int
830 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
831 {
832 	struct freebsd11_statfs osb;
833 	struct statfs *sfp;
834 	fhandle_t fh;
835 	int error;
836 
837 	error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
838 	if (error)
839 		return (error);
840 	sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
841 	error = kern_fhstatfs(td, fh, sfp);
842 	if (error == 0) {
843 		freebsd11_cvtstatfs(sfp, &osb);
844 		error = copyout(&osb, uap->buf, sizeof(osb));
845 	}
846 	free(sfp, M_STATFS);
847 	return (error);
848 }
849 
850 /*
851  * Convert a new format statfs structure to an old format statfs structure.
852  */
853 static void
854 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
855 {
856 
857 	bzero(osp, sizeof(*osp));
858 	osp->f_version = FREEBSD11_STATFS_VERSION;
859 	osp->f_type = nsp->f_type;
860 	osp->f_flags = nsp->f_flags;
861 	osp->f_bsize = nsp->f_bsize;
862 	osp->f_iosize = nsp->f_iosize;
863 	osp->f_blocks = nsp->f_blocks;
864 	osp->f_bfree = nsp->f_bfree;
865 	osp->f_bavail = nsp->f_bavail;
866 	osp->f_files = nsp->f_files;
867 	osp->f_ffree = nsp->f_ffree;
868 	osp->f_syncwrites = nsp->f_syncwrites;
869 	osp->f_asyncwrites = nsp->f_asyncwrites;
870 	osp->f_syncreads = nsp->f_syncreads;
871 	osp->f_asyncreads = nsp->f_asyncreads;
872 	osp->f_namemax = nsp->f_namemax;
873 	osp->f_owner = nsp->f_owner;
874 	osp->f_fsid = nsp->f_fsid;
875 	strlcpy(osp->f_fstypename, nsp->f_fstypename,
876 	    MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
877 	strlcpy(osp->f_mntonname, nsp->f_mntonname,
878 	    MIN(MNAMELEN, sizeof(osp->f_mntonname)));
879 	strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
880 	    MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
881 }
882 #endif /* COMPAT_FREEBSD11 */
883 
884 /*
885  * Change current working directory to a given file descriptor.
886  */
887 #ifndef _SYS_SYSPROTO_H_
888 struct fchdir_args {
889 	int	fd;
890 };
891 #endif
892 int
893 sys_fchdir(struct thread *td, struct fchdir_args *uap)
894 {
895 	struct vnode *vp, *tdp;
896 	struct mount *mp;
897 	struct file *fp;
898 	int error;
899 
900 	AUDIT_ARG_FD(uap->fd);
901 	error = getvnode_path(td, uap->fd, &cap_fchdir_rights,
902 	    &fp);
903 	if (error != 0)
904 		return (error);
905 	vp = fp->f_vnode;
906 	vrefact(vp);
907 	fdrop(fp, td);
908 	vn_lock(vp, LK_SHARED | LK_RETRY);
909 	AUDIT_ARG_VNODE1(vp);
910 	error = change_dir(vp, td);
911 	while (!error && (mp = vp->v_mountedhere) != NULL) {
912 		if (vfs_busy(mp, 0))
913 			continue;
914 		error = VFS_ROOT(mp, LK_SHARED, &tdp);
915 		vfs_unbusy(mp);
916 		if (error != 0)
917 			break;
918 		vput(vp);
919 		vp = tdp;
920 	}
921 	if (error != 0) {
922 		vput(vp);
923 		return (error);
924 	}
925 	VOP_UNLOCK(vp);
926 	pwd_chdir(td, vp);
927 	return (0);
928 }
929 
930 /*
931  * Change current working directory (``.'').
932  */
933 #ifndef _SYS_SYSPROTO_H_
934 struct chdir_args {
935 	char	*path;
936 };
937 #endif
938 int
939 sys_chdir(struct thread *td, struct chdir_args *uap)
940 {
941 
942 	return (kern_chdir(td, uap->path, UIO_USERSPACE));
943 }
944 
945 int
946 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
947 {
948 	struct nameidata nd;
949 	int error;
950 
951 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
952 	    pathseg, path);
953 	if ((error = namei(&nd)) != 0)
954 		return (error);
955 	if ((error = change_dir(nd.ni_vp, td)) != 0) {
956 		vput(nd.ni_vp);
957 		NDFREE_PNBUF(&nd);
958 		return (error);
959 	}
960 	VOP_UNLOCK(nd.ni_vp);
961 	NDFREE_PNBUF(&nd);
962 	pwd_chdir(td, nd.ni_vp);
963 	return (0);
964 }
965 
966 static int unprivileged_chroot = 0;
967 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_chroot, CTLFLAG_RW,
968     &unprivileged_chroot, 0,
969     "Unprivileged processes can use chroot(2)");
970 
971 /*
972  * Takes locked vnode, unlocks it before returning.
973  */
974 static int
975 kern_chroot(struct thread *td, struct vnode *vp)
976 {
977 	struct proc *p;
978 	int error;
979 
980 	error = priv_check(td, PRIV_VFS_CHROOT);
981 	if (error != 0) {
982 		p = td->td_proc;
983 		PROC_LOCK(p);
984 		if (unprivileged_chroot == 0 ||
985 		    (p->p_flag2 & P2_NO_NEW_PRIVS) == 0) {
986 			PROC_UNLOCK(p);
987 			goto e_vunlock;
988 		}
989 		PROC_UNLOCK(p);
990 	}
991 
992 	error = change_dir(vp, td);
993 	if (error != 0)
994 		goto e_vunlock;
995 #ifdef MAC
996 	error = mac_vnode_check_chroot(td->td_ucred, vp);
997 	if (error != 0)
998 		goto e_vunlock;
999 #endif
1000 	VOP_UNLOCK(vp);
1001 	error = pwd_chroot(td, vp);
1002 	vrele(vp);
1003 	return (error);
1004 e_vunlock:
1005 	vput(vp);
1006 	return (error);
1007 }
1008 
1009 /*
1010  * Change notion of root (``/'') directory.
1011  */
1012 #ifndef _SYS_SYSPROTO_H_
1013 struct chroot_args {
1014 	char	*path;
1015 };
1016 #endif
1017 int
1018 sys_chroot(struct thread *td, struct chroot_args *uap)
1019 {
1020 	struct nameidata nd;
1021 	int error;
1022 
1023 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
1024 	    UIO_USERSPACE, uap->path);
1025 	error = namei(&nd);
1026 	if (error != 0)
1027 		return (error);
1028 	NDFREE_PNBUF(&nd);
1029 	error = kern_chroot(td, nd.ni_vp);
1030 	return (error);
1031 }
1032 
1033 /*
1034  * Change notion of root directory to a given file descriptor.
1035  */
1036 #ifndef _SYS_SYSPROTO_H_
1037 struct fchroot_args {
1038 	int	fd;
1039 };
1040 #endif
1041 int
1042 sys_fchroot(struct thread *td, struct fchroot_args *uap)
1043 {
1044 	struct vnode *vp;
1045 	struct file *fp;
1046 	int error;
1047 
1048 	error = getvnode_path(td, uap->fd, &cap_fchroot_rights, &fp);
1049 	if (error != 0)
1050 		return (error);
1051 	vp = fp->f_vnode;
1052 	vrefact(vp);
1053 	fdrop(fp, td);
1054 	vn_lock(vp, LK_SHARED | LK_RETRY);
1055 	error = kern_chroot(td, vp);
1056 	return (error);
1057 }
1058 
1059 /*
1060  * Common routine for chroot and chdir.  Callers must provide a locked vnode
1061  * instance.
1062  */
1063 int
1064 change_dir(struct vnode *vp, struct thread *td)
1065 {
1066 #ifdef MAC
1067 	int error;
1068 #endif
1069 
1070 	ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1071 	if (vp->v_type != VDIR)
1072 		return (ENOTDIR);
1073 #ifdef MAC
1074 	error = mac_vnode_check_chdir(td->td_ucred, vp);
1075 	if (error != 0)
1076 		return (error);
1077 #endif
1078 	return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1079 }
1080 
1081 static __inline void
1082 flags_to_rights(int flags, cap_rights_t *rightsp)
1083 {
1084 	if (flags & O_EXEC) {
1085 		cap_rights_set_one(rightsp, CAP_FEXECVE);
1086 		if (flags & O_PATH)
1087 			return;
1088 	} else {
1089 		switch ((flags & O_ACCMODE)) {
1090 		case O_RDONLY:
1091 			cap_rights_set_one(rightsp, CAP_READ);
1092 			break;
1093 		case O_RDWR:
1094 			cap_rights_set_one(rightsp, CAP_READ);
1095 			/* FALLTHROUGH */
1096 		case O_WRONLY:
1097 			cap_rights_set_one(rightsp, CAP_WRITE);
1098 			if (!(flags & (O_APPEND | O_TRUNC)))
1099 				cap_rights_set_one(rightsp, CAP_SEEK);
1100 			break;
1101 		}
1102 	}
1103 
1104 	if (flags & O_CREAT)
1105 		cap_rights_set_one(rightsp, CAP_CREATE);
1106 
1107 	if (flags & O_TRUNC)
1108 		cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1109 
1110 	if (flags & (O_SYNC | O_FSYNC))
1111 		cap_rights_set_one(rightsp, CAP_FSYNC);
1112 
1113 	if (flags & (O_EXLOCK | O_SHLOCK))
1114 		cap_rights_set_one(rightsp, CAP_FLOCK);
1115 }
1116 
1117 /*
1118  * Check permissions, allocate an open file structure, and call the device
1119  * open routine if any.
1120  */
1121 #ifndef _SYS_SYSPROTO_H_
1122 struct open_args {
1123 	char	*path;
1124 	int	flags;
1125 	int	mode;
1126 };
1127 #endif
1128 int
1129 sys_open(struct thread *td, struct open_args *uap)
1130 {
1131 
1132 	return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1133 	    uap->flags, uap->mode));
1134 }
1135 
1136 #ifndef _SYS_SYSPROTO_H_
1137 struct openat_args {
1138 	int	fd;
1139 	char	*path;
1140 	int	flag;
1141 	int	mode;
1142 };
1143 #endif
1144 int
1145 sys_openat(struct thread *td, struct openat_args *uap)
1146 {
1147 
1148 	AUDIT_ARG_FD(uap->fd);
1149 	return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1150 	    uap->mode));
1151 }
1152 
1153 /*
1154  * Validate open(2) flags and convert access mode flags (O_RDONLY etc.) to their
1155  * in-kernel representations (FREAD etc.).
1156  */
1157 static int
1158 openflags(int *flagsp)
1159 {
1160 	int flags;
1161 
1162 	/*
1163 	 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1164 	 * may be specified.  On the other hand, for O_PATH any mode
1165 	 * except O_EXEC is ignored.
1166 	 */
1167 	flags = *flagsp;
1168 	if ((flags & O_PATH) != 0) {
1169 		flags &= ~O_ACCMODE;
1170 	} else if ((flags & O_EXEC) != 0) {
1171 		if ((flags & O_ACCMODE) != 0)
1172 			return (EINVAL);
1173 	} else if ((flags & O_ACCMODE) == O_ACCMODE) {
1174 		return (EINVAL);
1175 	} else {
1176 		flags = FFLAGS(flags);
1177 	}
1178 	*flagsp = flags;
1179 	return (0);
1180 }
1181 
1182 static void
1183 finit_open(struct file *fp, struct vnode *vp, int flags)
1184 {
1185 	/*
1186 	 * Store the vnode, for any f_type. Typically, the vnode use count is
1187 	 * decremented by a direct call to vnops.fo_close() for files that
1188 	 * switched type.
1189 	 */
1190 	fp->f_vnode = vp;
1191 
1192 	/*
1193 	 * If the file wasn't claimed by devfs or fifofs, bind it to the normal
1194 	 * vnode operations here.
1195 	 */
1196 	if (fp->f_ops == &badfileops) {
1197 		KASSERT(vp->v_type != VFIFO || (flags & O_PATH) != 0,
1198 		    ("Unexpected fifo fp %p vp %p", fp, vp));
1199 		if ((flags & O_PATH) != 0) {
1200 			finit(fp, (flags & FMASK) | (fp->f_flag & FKQALLOWED),
1201 			    DTYPE_VNODE, NULL, &path_fileops);
1202 		} else {
1203 			finit_vnode(fp, flags, NULL, &vnops);
1204 		}
1205 	}
1206 }
1207 
1208 /*
1209  * If fpp != NULL, opened file is not installed into the file
1210  * descriptor table, instead it is returned in *fpp.  This is
1211  * incompatible with fdopen(), in which case we return EINVAL.
1212  */
1213 static int
1214 openatfp(struct thread *td, int dirfd, const char *path,
1215     enum uio_seg pathseg, int flags, int mode, struct file **fpp)
1216 {
1217 	struct proc *p;
1218 	struct filedesc *fdp;
1219 	struct pwddesc *pdp;
1220 	struct file *fp;
1221 	struct vnode *vp;
1222 	struct filecaps *fcaps;
1223 	struct nameidata nd;
1224 	cap_rights_t rights;
1225 	int cmode, error, indx;
1226 
1227 	indx = -1;
1228 	p = td->td_proc;
1229 	fdp = p->p_fd;
1230 	pdp = p->p_pd;
1231 
1232 	AUDIT_ARG_FFLAGS(flags);
1233 	AUDIT_ARG_MODE(mode);
1234 	cap_rights_init_one(&rights, CAP_LOOKUP);
1235 	flags_to_rights(flags, &rights);
1236 
1237 	error = openflags(&flags);
1238 	if (error != 0)
1239 		return (error);
1240 
1241 	/*
1242 	 * Allocate a file structure. The descriptor to reference it
1243 	 * is allocated and used by finstall_refed() below.
1244 	 */
1245 	error = falloc_noinstall(td, &fp);
1246 	if (error != 0)
1247 		return (error);
1248 	/* Set the flags early so the finit in devfs can pick them up. */
1249 	fp->f_flag = flags & FMASK;
1250 	cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT;
1251 	NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | WANTIOCTLCAPS,
1252 	    pathseg, path, dirfd, &rights);
1253 	td->td_dupfd = -1;		/* XXX check for fdopen */
1254 	error = vn_open_cred(&nd, &flags, cmode, VN_OPEN_WANTIOCTLCAPS,
1255 	    td->td_ucred, fp);
1256 	if (error != 0) {
1257 		/*
1258 		 * If the vn_open replaced the method vector, something
1259 		 * wonderous happened deep below and we just pass it up
1260 		 * pretending we know what we do.
1261 		 */
1262 		if (error == ENXIO && fp->f_ops != &badfileops) {
1263 			MPASS((flags & O_PATH) == 0);
1264 			goto success;
1265 		}
1266 
1267 		/*
1268 		 * Handle special fdopen() case. bleh.
1269 		 *
1270 		 * Don't do this for relative (capability) lookups; we don't
1271 		 * understand exactly what would happen, and we don't think
1272 		 * that it ever should.
1273 		 */
1274 		if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1275 		    (error == ENODEV || error == ENXIO) &&
1276 		    td->td_dupfd >= 0) {
1277 			MPASS(fpp == NULL);
1278 			error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1279 			    &indx);
1280 			if (error == 0)
1281 				goto success;
1282 		}
1283 
1284 		goto bad;
1285 	}
1286 	td->td_dupfd = 0;
1287 	NDFREE_PNBUF(&nd);
1288 	vp = nd.ni_vp;
1289 
1290 	finit_open(fp, vp, flags);
1291 	VOP_UNLOCK(vp);
1292 	if (flags & O_TRUNC) {
1293 		error = fo_truncate(fp, 0, td->td_ucred, td);
1294 		if (error != 0)
1295 			goto bad;
1296 	}
1297 success:
1298 	if (fpp != NULL) {
1299 		MPASS(error == 0);
1300 		NDFREE_IOCTLCAPS(&nd);
1301 		*fpp = fp;
1302 		return (0);
1303 	}
1304 
1305 	/*
1306 	 * If we haven't already installed the FD (for dupfdopen), do so now.
1307 	 */
1308 	if (indx == -1) {
1309 #ifdef CAPABILITIES
1310 		if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1311 			fcaps = &nd.ni_filecaps;
1312 		else
1313 #endif
1314 			fcaps = NULL;
1315 		error = finstall_refed(td, fp, &indx, flags, fcaps);
1316 		/* On success finstall_refed() consumes fcaps. */
1317 		if (error != 0) {
1318 			goto bad;
1319 		}
1320 	} else {
1321 		NDFREE_IOCTLCAPS(&nd);
1322 		falloc_abort(td, fp);
1323 	}
1324 
1325 	td->td_retval[0] = indx;
1326 	return (0);
1327 bad:
1328 	KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1329 	NDFREE_IOCTLCAPS(&nd);
1330 	falloc_abort(td, fp);
1331 	return (error);
1332 }
1333 
1334 int
1335 kern_openat(struct thread *td, int dirfd, const char *path,
1336     enum uio_seg pathseg, int flags, int mode)
1337 {
1338 	return (openatfp(td, dirfd, path, pathseg, flags, mode, NULL));
1339 }
1340 
1341 int
1342 kern_openatfp(struct thread *td, int dirfd, const char *path,
1343     enum uio_seg pathseg, int flags, int mode, struct file **fpp)
1344 {
1345 	int error, old_dupfd;
1346 
1347 	old_dupfd = td->td_dupfd;
1348 	td->td_dupfd = -1;
1349 	error = openatfp(td, dirfd, path, pathseg, flags, mode, fpp);
1350 	td->td_dupfd = old_dupfd;
1351 	return (error);
1352 }
1353 
1354 #ifdef COMPAT_43
1355 /*
1356  * Create a file.
1357  */
1358 #ifndef _SYS_SYSPROTO_H_
1359 struct ocreat_args {
1360 	char	*path;
1361 	int	mode;
1362 };
1363 #endif
1364 int
1365 ocreat(struct thread *td, struct ocreat_args *uap)
1366 {
1367 
1368 	return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1369 	    O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1370 }
1371 #endif /* COMPAT_43 */
1372 
1373 /*
1374  * Create a special file.
1375  */
1376 #ifndef _SYS_SYSPROTO_H_
1377 struct mknodat_args {
1378 	int	fd;
1379 	char	*path;
1380 	mode_t	mode;
1381 	dev_t	dev;
1382 };
1383 #endif
1384 int
1385 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1386 {
1387 
1388 	return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1389 	    uap->dev));
1390 }
1391 
1392 #if defined(COMPAT_FREEBSD11)
1393 int
1394 freebsd11_mknod(struct thread *td,
1395     struct freebsd11_mknod_args *uap)
1396 {
1397 
1398 	return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1399 	    uap->mode, uap->dev));
1400 }
1401 
1402 int
1403 freebsd11_mknodat(struct thread *td,
1404     struct freebsd11_mknodat_args *uap)
1405 {
1406 
1407 	return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1408 	    uap->dev));
1409 }
1410 #endif /* COMPAT_FREEBSD11 */
1411 
1412 int
1413 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1414     int mode, dev_t dev)
1415 {
1416 	struct vnode *vp;
1417 	struct mount *mp;
1418 	struct vattr vattr;
1419 	struct nameidata nd;
1420 	int error, whiteout = 0;
1421 
1422 	AUDIT_ARG_MODE(mode);
1423 	AUDIT_ARG_DEV(dev);
1424 	switch (mode & S_IFMT) {
1425 	case S_IFCHR:
1426 	case S_IFBLK:
1427 		error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1428 		if (error == 0 && dev == VNOVAL)
1429 			error = EINVAL;
1430 		break;
1431 	case S_IFWHT:
1432 		error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1433 		break;
1434 	case S_IFIFO:
1435 		if (dev == 0)
1436 			return (kern_mkfifoat(td, fd, path, pathseg, mode));
1437 		/* FALLTHROUGH */
1438 	default:
1439 		error = EINVAL;
1440 		break;
1441 	}
1442 	if (error != 0)
1443 		return (error);
1444 	NDPREINIT(&nd);
1445 restart:
1446 	bwillwrite();
1447 	NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE,
1448 	    pathseg, path, fd, &cap_mknodat_rights);
1449 	if ((error = namei(&nd)) != 0)
1450 		return (error);
1451 	vp = nd.ni_vp;
1452 	if (vp != NULL) {
1453 		NDFREE_PNBUF(&nd);
1454 		if (vp == nd.ni_dvp)
1455 			vrele(nd.ni_dvp);
1456 		else
1457 			vput(nd.ni_dvp);
1458 		vrele(vp);
1459 		return (EEXIST);
1460 	} else if ((vn_irflag_read(nd.ni_dvp) & VIRF_NAMEDDIR) != 0) {
1461 		NDFREE_PNBUF(&nd);
1462 		vput(nd.ni_dvp);
1463 		return (EINVAL);
1464 	} else {
1465 		VATTR_NULL(&vattr);
1466 		vattr.va_mode = (mode & ALLPERMS) &
1467 		    ~td->td_proc->p_pd->pd_cmask;
1468 		vattr.va_rdev = dev;
1469 		whiteout = 0;
1470 
1471 		switch (mode & S_IFMT) {
1472 		case S_IFCHR:
1473 			vattr.va_type = VCHR;
1474 			break;
1475 		case S_IFBLK:
1476 			vattr.va_type = VBLK;
1477 			break;
1478 		case S_IFWHT:
1479 			whiteout = 1;
1480 			break;
1481 		default:
1482 			panic("kern_mknod: invalid mode");
1483 		}
1484 	}
1485 	if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1486 		NDFREE_PNBUF(&nd);
1487 		vput(nd.ni_dvp);
1488 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1489 			return (error);
1490 		goto restart;
1491 	}
1492 #ifdef MAC
1493 	if (error == 0 && !whiteout)
1494 		error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1495 		    &nd.ni_cnd, &vattr);
1496 #endif
1497 	if (error == 0) {
1498 		if (whiteout)
1499 			error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1500 		else {
1501 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1502 						&nd.ni_cnd, &vattr);
1503 		}
1504 	}
1505 	VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL,
1506 	    true);
1507 	vn_finished_write(mp);
1508 	NDFREE_PNBUF(&nd);
1509 	if (error == ERELOOKUP)
1510 		goto restart;
1511 	return (error);
1512 }
1513 
1514 /*
1515  * Create a named pipe.
1516  */
1517 #ifndef _SYS_SYSPROTO_H_
1518 struct mkfifo_args {
1519 	char	*path;
1520 	int	mode;
1521 };
1522 #endif
1523 int
1524 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1525 {
1526 
1527 	return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1528 	    uap->mode));
1529 }
1530 
1531 #ifndef _SYS_SYSPROTO_H_
1532 struct mkfifoat_args {
1533 	int	fd;
1534 	char	*path;
1535 	mode_t	mode;
1536 };
1537 #endif
1538 int
1539 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1540 {
1541 
1542 	return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1543 	    uap->mode));
1544 }
1545 
1546 int
1547 kern_mkfifoat(struct thread *td, int fd, const char *path,
1548     enum uio_seg pathseg, int mode)
1549 {
1550 	struct mount *mp;
1551 	struct vattr vattr;
1552 	struct nameidata nd;
1553 	int error;
1554 
1555 	AUDIT_ARG_MODE(mode);
1556 	NDPREINIT(&nd);
1557 restart:
1558 	bwillwrite();
1559 	NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE,
1560 	    pathseg, path, fd, &cap_mkfifoat_rights);
1561 	if ((error = namei(&nd)) != 0)
1562 		return (error);
1563 	if (nd.ni_vp != NULL) {
1564 		NDFREE_PNBUF(&nd);
1565 		if (nd.ni_vp == nd.ni_dvp)
1566 			vrele(nd.ni_dvp);
1567 		else
1568 			vput(nd.ni_dvp);
1569 		vrele(nd.ni_vp);
1570 		return (EEXIST);
1571 	}
1572 	if ((vn_irflag_read(nd.ni_dvp) & VIRF_NAMEDDIR) != 0) {
1573 		NDFREE_PNBUF(&nd);
1574 		vput(nd.ni_dvp);
1575 		return (EINVAL);
1576 	}
1577 	if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1578 		NDFREE_PNBUF(&nd);
1579 		vput(nd.ni_dvp);
1580 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1581 			return (error);
1582 		goto restart;
1583 	}
1584 	VATTR_NULL(&vattr);
1585 	vattr.va_type = VFIFO;
1586 	vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1587 #ifdef MAC
1588 	error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1589 	    &vattr);
1590 	if (error != 0)
1591 		goto out;
1592 #endif
1593 	error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1594 #ifdef MAC
1595 out:
1596 #endif
1597 	VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1598 	vn_finished_write(mp);
1599 	NDFREE_PNBUF(&nd);
1600 	if (error == ERELOOKUP)
1601 		goto restart;
1602 	return (error);
1603 }
1604 
1605 /*
1606  * Make a hard file link.
1607  */
1608 #ifndef _SYS_SYSPROTO_H_
1609 struct link_args {
1610 	char	*path;
1611 	char	*link;
1612 };
1613 #endif
1614 int
1615 sys_link(struct thread *td, struct link_args *uap)
1616 {
1617 
1618 	return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1619 	    UIO_USERSPACE, AT_SYMLINK_FOLLOW));
1620 }
1621 
1622 #ifndef _SYS_SYSPROTO_H_
1623 struct linkat_args {
1624 	int	fd1;
1625 	char	*path1;
1626 	int	fd2;
1627 	char	*path2;
1628 	int	flag;
1629 };
1630 #endif
1631 int
1632 sys_linkat(struct thread *td, struct linkat_args *uap)
1633 {
1634 
1635 	return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1636 	    UIO_USERSPACE, uap->flag));
1637 }
1638 
1639 int hardlink_check_uid = 0;
1640 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1641     &hardlink_check_uid, 0,
1642     "Unprivileged processes cannot create hard links to files owned by other "
1643     "users");
1644 static int hardlink_check_gid = 0;
1645 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1646     &hardlink_check_gid, 0,
1647     "Unprivileged processes cannot create hard links to files owned by other "
1648     "groups");
1649 
1650 static int
1651 can_hardlink(struct vnode *vp, struct ucred *cred)
1652 {
1653 	struct vattr va;
1654 	int error;
1655 
1656 	if (!hardlink_check_uid && !hardlink_check_gid)
1657 		return (0);
1658 
1659 	error = VOP_GETATTR(vp, &va, cred);
1660 	if (error != 0)
1661 		return (error);
1662 
1663 	if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1664 		error = priv_check_cred(cred, PRIV_VFS_LINK);
1665 		if (error != 0)
1666 			return (error);
1667 	}
1668 
1669 	if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1670 		error = priv_check_cred(cred, PRIV_VFS_LINK);
1671 		if (error != 0)
1672 			return (error);
1673 	}
1674 
1675 	return (0);
1676 }
1677 
1678 int
1679 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1680     const char *path2, enum uio_seg segflag, int flag)
1681 {
1682 	struct nameidata nd;
1683 	int error;
1684 
1685 	if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH |
1686 	    AT_EMPTY_PATH)) != 0)
1687 		return (EINVAL);
1688 
1689 	NDPREINIT(&nd);
1690 	do {
1691 		bwillwrite();
1692 		NDINIT_ATRIGHTS(&nd, LOOKUP, AUDITVNODE1 | at2cnpflags(flag,
1693 		    AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH | AT_EMPTY_PATH),
1694 		    segflag, path1, fd1, &cap_linkat_source_rights);
1695 		if ((error = namei(&nd)) != 0)
1696 			return (error);
1697 		NDFREE_PNBUF(&nd);
1698 		if ((nd.ni_resflags & NIRES_EMPTYPATH) != 0) {
1699 			error = priv_check(td, PRIV_VFS_FHOPEN);
1700 			if (error != 0) {
1701 				vrele(nd.ni_vp);
1702 				return (error);
1703 			}
1704 		}
1705 		error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1706 	} while (error ==  EAGAIN || error == ERELOOKUP);
1707 	return (error);
1708 }
1709 
1710 static int
1711 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1712     enum uio_seg segflag)
1713 {
1714 	struct nameidata nd;
1715 	struct mount *mp;
1716 	int error;
1717 
1718 	if (vp->v_type == VDIR) {
1719 		vrele(vp);
1720 		return (EPERM);		/* POSIX */
1721 	}
1722 	if ((vn_irflag_read(vp) & (VIRF_NAMEDDIR | VIRF_NAMEDATTR)) != 0) {
1723 		vrele(vp);
1724 		return (EINVAL);
1725 	}
1726 	NDINIT_ATRIGHTS(&nd, CREATE,
1727 	    LOCKPARENT | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1728 	    &cap_linkat_target_rights);
1729 	if ((error = namei(&nd)) == 0) {
1730 		if (nd.ni_vp != NULL) {
1731 			NDFREE_PNBUF(&nd);
1732 			if (nd.ni_dvp == nd.ni_vp)
1733 				vrele(nd.ni_dvp);
1734 			else
1735 				vput(nd.ni_dvp);
1736 			vrele(nd.ni_vp);
1737 			vrele(vp);
1738 			return (EEXIST);
1739 		} else if (nd.ni_dvp->v_mount != vp->v_mount) {
1740 			/*
1741 			 * Cross-device link.  No need to recheck
1742 			 * vp->v_type, since it cannot change, except
1743 			 * to VBAD.
1744 			 */
1745 			NDFREE_PNBUF(&nd);
1746 			vput(nd.ni_dvp);
1747 			vrele(vp);
1748 			return (EXDEV);
1749 		} else if (vn_lock(vp, LK_EXCLUSIVE) == 0) {
1750 			error = can_hardlink(vp, td->td_ucred);
1751 #ifdef MAC
1752 			if (error == 0)
1753 				error = mac_vnode_check_link(td->td_ucred,
1754 				    nd.ni_dvp, vp, &nd.ni_cnd);
1755 #endif
1756 			if (error != 0) {
1757 				vput(vp);
1758 				vput(nd.ni_dvp);
1759 				NDFREE_PNBUF(&nd);
1760 				return (error);
1761 			}
1762 			error = vn_start_write(vp, &mp, V_NOWAIT);
1763 			if (error != 0) {
1764 				vput(vp);
1765 				vput(nd.ni_dvp);
1766 				NDFREE_PNBUF(&nd);
1767 				error = vn_start_write(NULL, &mp,
1768 				    V_XSLEEP | V_PCATCH);
1769 				if (error != 0)
1770 					return (error);
1771 				return (EAGAIN);
1772 			}
1773 			error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1774 			VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
1775 			vn_finished_write(mp);
1776 			NDFREE_PNBUF(&nd);
1777 			vp = NULL;
1778 		} else {
1779 			vput(nd.ni_dvp);
1780 			NDFREE_PNBUF(&nd);
1781 			vrele(vp);
1782 			return (EAGAIN);
1783 		}
1784 	}
1785 	if (vp != NULL)
1786 		vrele(vp);
1787 	return (error);
1788 }
1789 
1790 /*
1791  * Make a symbolic link.
1792  */
1793 #ifndef _SYS_SYSPROTO_H_
1794 struct symlink_args {
1795 	char	*path;
1796 	char	*link;
1797 };
1798 #endif
1799 int
1800 sys_symlink(struct thread *td, struct symlink_args *uap)
1801 {
1802 
1803 	return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1804 	    UIO_USERSPACE));
1805 }
1806 
1807 #ifndef _SYS_SYSPROTO_H_
1808 struct symlinkat_args {
1809 	char	*path;
1810 	int	fd;
1811 	char	*path2;
1812 };
1813 #endif
1814 int
1815 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1816 {
1817 
1818 	return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1819 	    UIO_USERSPACE));
1820 }
1821 
1822 int
1823 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1824     enum uio_seg segflg)
1825 {
1826 	struct mount *mp;
1827 	struct vattr vattr;
1828 	const char *syspath;
1829 	char *tmppath;
1830 	struct nameidata nd;
1831 	int error;
1832 
1833 	if (segflg == UIO_SYSSPACE) {
1834 		syspath = path1;
1835 	} else {
1836 		tmppath = uma_zalloc(namei_zone, M_WAITOK);
1837 		if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1838 			goto out;
1839 		syspath = tmppath;
1840 	}
1841 	AUDIT_ARG_TEXT(syspath);
1842 	NDPREINIT(&nd);
1843 restart:
1844 	bwillwrite();
1845 	NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE, segflg,
1846 	    path2, fd, &cap_symlinkat_rights);
1847 	if ((error = namei(&nd)) != 0)
1848 		goto out;
1849 	if (nd.ni_vp) {
1850 		NDFREE_PNBUF(&nd);
1851 		if (nd.ni_vp == nd.ni_dvp)
1852 			vrele(nd.ni_dvp);
1853 		else
1854 			vput(nd.ni_dvp);
1855 		vrele(nd.ni_vp);
1856 		nd.ni_vp = NULL;
1857 		error = EEXIST;
1858 		goto out;
1859 	}
1860 	if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1861 		NDFREE_PNBUF(&nd);
1862 		vput(nd.ni_dvp);
1863 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1864 			goto out;
1865 		goto restart;
1866 	}
1867 	if ((vn_irflag_read(nd.ni_dvp) & VIRF_NAMEDDIR) != 0) {
1868 		error = EINVAL;
1869 		goto out;
1870 	}
1871 	VATTR_NULL(&vattr);
1872 	vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1873 #ifdef MAC
1874 	vattr.va_type = VLNK;
1875 	error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1876 	    &vattr);
1877 	if (error != 0)
1878 		goto out2;
1879 #endif
1880 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1881 #ifdef MAC
1882 out2:
1883 #endif
1884 	VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1885 	vn_finished_write(mp);
1886 	NDFREE_PNBUF(&nd);
1887 	if (error == ERELOOKUP)
1888 		goto restart;
1889 out:
1890 	if (segflg != UIO_SYSSPACE)
1891 		uma_zfree(namei_zone, tmppath);
1892 	return (error);
1893 }
1894 
1895 /*
1896  * Delete a whiteout from the filesystem.
1897  */
1898 #ifndef _SYS_SYSPROTO_H_
1899 struct undelete_args {
1900 	char *path;
1901 };
1902 #endif
1903 int
1904 sys_undelete(struct thread *td, struct undelete_args *uap)
1905 {
1906 	struct mount *mp;
1907 	struct nameidata nd;
1908 	int error;
1909 
1910 	NDPREINIT(&nd);
1911 restart:
1912 	bwillwrite();
1913 	NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1914 	    UIO_USERSPACE, uap->path);
1915 	error = namei(&nd);
1916 	if (error != 0)
1917 		return (error);
1918 
1919 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1920 		NDFREE_PNBUF(&nd);
1921 		if (nd.ni_vp == nd.ni_dvp)
1922 			vrele(nd.ni_dvp);
1923 		else
1924 			vput(nd.ni_dvp);
1925 		if (nd.ni_vp)
1926 			vrele(nd.ni_vp);
1927 		return (EEXIST);
1928 	}
1929 	if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1930 		NDFREE_PNBUF(&nd);
1931 		vput(nd.ni_dvp);
1932 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1933 			return (error);
1934 		goto restart;
1935 	}
1936 	error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1937 	NDFREE_PNBUF(&nd);
1938 	vput(nd.ni_dvp);
1939 	vn_finished_write(mp);
1940 	if (error == ERELOOKUP)
1941 		goto restart;
1942 	return (error);
1943 }
1944 
1945 /*
1946  * Delete a name from the filesystem.
1947  */
1948 #ifndef _SYS_SYSPROTO_H_
1949 struct unlink_args {
1950 	char	*path;
1951 };
1952 #endif
1953 int
1954 sys_unlink(struct thread *td, struct unlink_args *uap)
1955 {
1956 
1957 	return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1958 	    0, 0));
1959 }
1960 
1961 static int
1962 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1963     int flag, enum uio_seg pathseg, ino_t oldinum)
1964 {
1965 
1966 	if ((flag & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0)
1967 		return (EINVAL);
1968 
1969 	if ((flag & AT_REMOVEDIR) != 0)
1970 		return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1971 
1972 	return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1973 }
1974 
1975 #ifndef _SYS_SYSPROTO_H_
1976 struct unlinkat_args {
1977 	int	fd;
1978 	char	*path;
1979 	int	flag;
1980 };
1981 #endif
1982 int
1983 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1984 {
1985 
1986 	return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1987 	    UIO_USERSPACE, 0));
1988 }
1989 
1990 #ifndef _SYS_SYSPROTO_H_
1991 struct funlinkat_args {
1992 	int		dfd;
1993 	const char	*path;
1994 	int		fd;
1995 	int		flag;
1996 };
1997 #endif
1998 int
1999 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
2000 {
2001 
2002 	return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
2003 	    UIO_USERSPACE, 0));
2004 }
2005 
2006 int
2007 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
2008     enum uio_seg pathseg, int flag, ino_t oldinum)
2009 {
2010 	struct mount *mp;
2011 	struct file *fp;
2012 	struct vnode *vp;
2013 	struct nameidata nd;
2014 	struct stat sb;
2015 	int error;
2016 
2017 	fp = NULL;
2018 	if (fd != FD_NONE) {
2019 		error = getvnode_path(td, fd, &cap_no_rights, &fp);
2020 		if (error != 0)
2021 			return (error);
2022 	}
2023 
2024 	NDPREINIT(&nd);
2025 restart:
2026 	bwillwrite();
2027 	NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
2028 	    at2cnpflags(flag, AT_RESOLVE_BENEATH),
2029 	    pathseg, path, dfd, &cap_unlinkat_rights);
2030 	if ((error = namei(&nd)) != 0) {
2031 		if (error == EINVAL)
2032 			error = EPERM;
2033 		goto fdout;
2034 	}
2035 	vp = nd.ni_vp;
2036 	if (vp->v_type == VDIR && oldinum == 0) {
2037 		error = EPERM;		/* POSIX */
2038 	} else if (oldinum != 0 &&
2039 	    ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED)) == 0) &&
2040 	    sb.st_ino != oldinum) {
2041 		error = EIDRM;	/* Identifier removed */
2042 	} else if (fp != NULL && fp->f_vnode != vp) {
2043 		if (VN_IS_DOOMED(fp->f_vnode))
2044 			error = EBADF;
2045 		else
2046 			error = EDEADLK;
2047 	} else {
2048 		/*
2049 		 * The root of a mounted filesystem cannot be deleted.
2050 		 *
2051 		 * XXX: can this only be a VDIR case?
2052 		 */
2053 		if (vp->v_vflag & VV_ROOT)
2054 			error = EBUSY;
2055 	}
2056 	if (error == 0) {
2057 		if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
2058 			NDFREE_PNBUF(&nd);
2059 			vput(nd.ni_dvp);
2060 			if (vp == nd.ni_dvp)
2061 				vrele(vp);
2062 			else
2063 				vput(vp);
2064 			if ((error = vn_start_write(NULL, &mp,
2065 			    V_XSLEEP | V_PCATCH)) != 0) {
2066 				goto fdout;
2067 			}
2068 			goto restart;
2069 		}
2070 #ifdef MAC
2071 		error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
2072 		    &nd.ni_cnd);
2073 		if (error != 0)
2074 			goto out;
2075 #endif
2076 		error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
2077 #ifdef MAC
2078 out:
2079 #endif
2080 		vn_finished_write(mp);
2081 	}
2082 	NDFREE_PNBUF(&nd);
2083 	vput(nd.ni_dvp);
2084 	if (vp == nd.ni_dvp)
2085 		vrele(vp);
2086 	else
2087 		vput(vp);
2088 	if (error == ERELOOKUP)
2089 		goto restart;
2090 fdout:
2091 	if (fp != NULL)
2092 		fdrop(fp, td);
2093 	return (error);
2094 }
2095 
2096 /*
2097  * Reposition read/write file offset.
2098  */
2099 #ifndef _SYS_SYSPROTO_H_
2100 struct lseek_args {
2101 	int	fd;
2102 	int	pad;
2103 	off_t	offset;
2104 	int	whence;
2105 };
2106 #endif
2107 int
2108 sys_lseek(struct thread *td, struct lseek_args *uap)
2109 {
2110 
2111 	return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2112 }
2113 
2114 int
2115 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
2116 {
2117 	struct file *fp;
2118 	int error;
2119 
2120 	AUDIT_ARG_FD(fd);
2121 	error = fget(td, fd, &cap_seek_rights, &fp);
2122 	if (error != 0)
2123 		return (error);
2124 	error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2125 	    fo_seek(fp, offset, whence, td) : ESPIPE;
2126 	fdrop(fp, td);
2127 	return (error);
2128 }
2129 
2130 #if defined(COMPAT_43)
2131 /*
2132  * Reposition read/write file offset.
2133  */
2134 #ifndef _SYS_SYSPROTO_H_
2135 struct olseek_args {
2136 	int	fd;
2137 	long	offset;
2138 	int	whence;
2139 };
2140 #endif
2141 int
2142 olseek(struct thread *td, struct olseek_args *uap)
2143 {
2144 
2145 	return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2146 }
2147 #endif /* COMPAT_43 */
2148 
2149 #if defined(COMPAT_FREEBSD6)
2150 /* Version with the 'pad' argument */
2151 int
2152 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2153 {
2154 
2155 	return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2156 }
2157 #endif
2158 
2159 /*
2160  * Check access permissions using passed credentials.
2161  */
2162 static int
2163 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2164      struct thread *td)
2165 {
2166 	accmode_t accmode;
2167 	int error;
2168 
2169 	/* Flags == 0 means only check for existence. */
2170 	if (user_flags == 0)
2171 		return (0);
2172 
2173 	accmode = 0;
2174 	if (user_flags & R_OK)
2175 		accmode |= VREAD;
2176 	if (user_flags & W_OK)
2177 		accmode |= VWRITE;
2178 	if (user_flags & X_OK)
2179 		accmode |= VEXEC;
2180 #ifdef MAC
2181 	error = mac_vnode_check_access(cred, vp, accmode);
2182 	if (error != 0)
2183 		return (error);
2184 #endif
2185 	if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2186 		error = VOP_ACCESS(vp, accmode, cred, td);
2187 	return (error);
2188 }
2189 
2190 /*
2191  * Check access permissions using "real" credentials.
2192  */
2193 #ifndef _SYS_SYSPROTO_H_
2194 struct access_args {
2195 	char	*path;
2196 	int	amode;
2197 };
2198 #endif
2199 int
2200 sys_access(struct thread *td, struct access_args *uap)
2201 {
2202 
2203 	return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2204 	    0, uap->amode));
2205 }
2206 
2207 #ifndef _SYS_SYSPROTO_H_
2208 struct faccessat_args {
2209 	int	dirfd;
2210 	char	*path;
2211 	int	amode;
2212 	int	flag;
2213 }
2214 #endif
2215 int
2216 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2217 {
2218 
2219 	return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2220 	    uap->amode));
2221 }
2222 
2223 int
2224 kern_accessat(struct thread *td, int fd, const char *path,
2225     enum uio_seg pathseg, int flag, int amode)
2226 {
2227 	struct ucred *cred, *usecred;
2228 	struct vnode *vp;
2229 	struct nameidata nd;
2230 	int error;
2231 
2232 	if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH | AT_EMPTY_PATH |
2233 	    AT_SYMLINK_NOFOLLOW)) != 0)
2234 		return (EINVAL);
2235 	if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2236 		return (EINVAL);
2237 
2238 	/*
2239 	 * Create and modify a temporary credential instead of one that
2240 	 * is potentially shared (if we need one).
2241 	 */
2242 	cred = td->td_ucred;
2243 	if ((flag & AT_EACCESS) == 0 &&
2244 	    ((cred->cr_uid != cred->cr_ruid ||
2245 	    cred->cr_rgid != cred->cr_groups[0]))) {
2246 		usecred = crdup(cred);
2247 		usecred->cr_uid = cred->cr_ruid;
2248 		usecred->cr_groups[0] = cred->cr_rgid;
2249 		td->td_ucred = usecred;
2250 	} else
2251 		usecred = cred;
2252 	AUDIT_ARG_VALUE(amode);
2253 	NDINIT_ATRIGHTS(&nd, LOOKUP, LOCKSHARED | LOCKLEAF |
2254 	    AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH | AT_SYMLINK_NOFOLLOW |
2255 	    AT_EMPTY_PATH), pathseg, path, fd, &cap_fstat_rights);
2256 	if ((error = namei(&nd)) != 0)
2257 		goto out;
2258 	vp = nd.ni_vp;
2259 
2260 	error = vn_access(vp, amode, usecred, td);
2261 	NDFREE_PNBUF(&nd);
2262 	vput(vp);
2263 out:
2264 	if (usecred != cred) {
2265 		td->td_ucred = cred;
2266 		crfree(usecred);
2267 	}
2268 	return (error);
2269 }
2270 
2271 /*
2272  * Check access permissions using "effective" credentials.
2273  */
2274 #ifndef _SYS_SYSPROTO_H_
2275 struct eaccess_args {
2276 	char	*path;
2277 	int	amode;
2278 };
2279 #endif
2280 int
2281 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2282 {
2283 
2284 	return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2285 	    AT_EACCESS, uap->amode));
2286 }
2287 
2288 #if defined(COMPAT_43)
2289 /*
2290  * Get file status; this version follows links.
2291  */
2292 #ifndef _SYS_SYSPROTO_H_
2293 struct ostat_args {
2294 	char	*path;
2295 	struct ostat *ub;
2296 };
2297 #endif
2298 int
2299 ostat(struct thread *td, struct ostat_args *uap)
2300 {
2301 	struct stat sb;
2302 	struct ostat osb;
2303 	int error;
2304 
2305 	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2306 	if (error != 0)
2307 		return (error);
2308 	cvtstat(&sb, &osb);
2309 	return (copyout(&osb, uap->ub, sizeof (osb)));
2310 }
2311 
2312 /*
2313  * Get file status; this version does not follow links.
2314  */
2315 #ifndef _SYS_SYSPROTO_H_
2316 struct olstat_args {
2317 	char	*path;
2318 	struct ostat *ub;
2319 };
2320 #endif
2321 int
2322 olstat(struct thread *td, struct olstat_args *uap)
2323 {
2324 	struct stat sb;
2325 	struct ostat osb;
2326 	int error;
2327 
2328 	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2329 	    UIO_USERSPACE, &sb);
2330 	if (error != 0)
2331 		return (error);
2332 	cvtstat(&sb, &osb);
2333 	return (copyout(&osb, uap->ub, sizeof (osb)));
2334 }
2335 
2336 /*
2337  * Convert from an old to a new stat structure.
2338  * XXX: many values are blindly truncated.
2339  */
2340 void
2341 cvtstat(struct stat *st, struct ostat *ost)
2342 {
2343 
2344 	bzero(ost, sizeof(*ost));
2345 	ost->st_dev = st->st_dev;
2346 	ost->st_ino = st->st_ino;
2347 	ost->st_mode = st->st_mode;
2348 	ost->st_nlink = st->st_nlink;
2349 	ost->st_uid = st->st_uid;
2350 	ost->st_gid = st->st_gid;
2351 	ost->st_rdev = st->st_rdev;
2352 	ost->st_size = MIN(st->st_size, INT32_MAX);
2353 	ost->st_atim = st->st_atim;
2354 	ost->st_mtim = st->st_mtim;
2355 	ost->st_ctim = st->st_ctim;
2356 	ost->st_blksize = st->st_blksize;
2357 	ost->st_blocks = st->st_blocks;
2358 	ost->st_flags = st->st_flags;
2359 	ost->st_gen = st->st_gen;
2360 }
2361 #endif /* COMPAT_43 */
2362 
2363 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2364 int ino64_trunc_error;
2365 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2366     &ino64_trunc_error, 0,
2367     "Error on truncation of device, file or inode number, or link count");
2368 
2369 int
2370 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2371 {
2372 
2373 	ost->st_dev = st->st_dev;
2374 	if (ost->st_dev != st->st_dev) {
2375 		switch (ino64_trunc_error) {
2376 		default:
2377 			/*
2378 			 * Since dev_t is almost raw, don't clamp to the
2379 			 * maximum for case 2, but ignore the error.
2380 			 */
2381 			break;
2382 		case 1:
2383 			return (EOVERFLOW);
2384 		}
2385 	}
2386 	ost->st_ino = st->st_ino;
2387 	if (ost->st_ino != st->st_ino) {
2388 		switch (ino64_trunc_error) {
2389 		default:
2390 		case 0:
2391 			break;
2392 		case 1:
2393 			return (EOVERFLOW);
2394 		case 2:
2395 			ost->st_ino = UINT32_MAX;
2396 			break;
2397 		}
2398 	}
2399 	ost->st_mode = st->st_mode;
2400 	ost->st_nlink = st->st_nlink;
2401 	if (ost->st_nlink != st->st_nlink) {
2402 		switch (ino64_trunc_error) {
2403 		default:
2404 		case 0:
2405 			break;
2406 		case 1:
2407 			return (EOVERFLOW);
2408 		case 2:
2409 			ost->st_nlink = UINT16_MAX;
2410 			break;
2411 		}
2412 	}
2413 	ost->st_uid = st->st_uid;
2414 	ost->st_gid = st->st_gid;
2415 	ost->st_rdev = st->st_rdev;
2416 	if (ost->st_rdev != st->st_rdev) {
2417 		switch (ino64_trunc_error) {
2418 		default:
2419 			break;
2420 		case 1:
2421 			return (EOVERFLOW);
2422 		}
2423 	}
2424 	ost->st_atim = st->st_atim;
2425 	ost->st_mtim = st->st_mtim;
2426 	ost->st_ctim = st->st_ctim;
2427 	ost->st_size = st->st_size;
2428 	ost->st_blocks = st->st_blocks;
2429 	ost->st_blksize = st->st_blksize;
2430 	ost->st_flags = st->st_flags;
2431 	ost->st_gen = st->st_gen;
2432 	ost->st_lspare = 0;
2433 	ost->st_birthtim = st->st_birthtim;
2434 	bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2435 	    sizeof(*ost) - offsetof(struct freebsd11_stat,
2436 	    st_birthtim) - sizeof(ost->st_birthtim));
2437 	return (0);
2438 }
2439 
2440 int
2441 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2442 {
2443 	struct stat sb;
2444 	struct freebsd11_stat osb;
2445 	int error;
2446 
2447 	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2448 	if (error != 0)
2449 		return (error);
2450 	error = freebsd11_cvtstat(&sb, &osb);
2451 	if (error == 0)
2452 		error = copyout(&osb, uap->ub, sizeof(osb));
2453 	return (error);
2454 }
2455 
2456 int
2457 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2458 {
2459 	struct stat sb;
2460 	struct freebsd11_stat osb;
2461 	int error;
2462 
2463 	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2464 	    UIO_USERSPACE, &sb);
2465 	if (error != 0)
2466 		return (error);
2467 	error = freebsd11_cvtstat(&sb, &osb);
2468 	if (error == 0)
2469 		error = copyout(&osb, uap->ub, sizeof(osb));
2470 	return (error);
2471 }
2472 
2473 int
2474 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2475 {
2476 	struct fhandle fh;
2477 	struct stat sb;
2478 	struct freebsd11_stat osb;
2479 	int error;
2480 
2481 	error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2482 	if (error != 0)
2483 		return (error);
2484 	error = kern_fhstat(td, fh, &sb);
2485 	if (error != 0)
2486 		return (error);
2487 	error = freebsd11_cvtstat(&sb, &osb);
2488 	if (error == 0)
2489 		error = copyout(&osb, uap->sb, sizeof(osb));
2490 	return (error);
2491 }
2492 
2493 int
2494 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2495 {
2496 	struct stat sb;
2497 	struct freebsd11_stat osb;
2498 	int error;
2499 
2500 	error = kern_statat(td, uap->flag, uap->fd, uap->path,
2501 	    UIO_USERSPACE, &sb);
2502 	if (error != 0)
2503 		return (error);
2504 	error = freebsd11_cvtstat(&sb, &osb);
2505 	if (error == 0)
2506 		error = copyout(&osb, uap->buf, sizeof(osb));
2507 	return (error);
2508 }
2509 #endif	/* COMPAT_FREEBSD11 */
2510 
2511 /*
2512  * Get file status
2513  */
2514 #ifndef _SYS_SYSPROTO_H_
2515 struct fstatat_args {
2516 	int	fd;
2517 	char	*path;
2518 	struct stat	*buf;
2519 	int	flag;
2520 }
2521 #endif
2522 int
2523 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2524 {
2525 	struct stat sb;
2526 	int error;
2527 
2528 	error = kern_statat(td, uap->flag, uap->fd, uap->path,
2529 	    UIO_USERSPACE, &sb);
2530 	if (error == 0)
2531 		error = copyout(&sb, uap->buf, sizeof (sb));
2532 	return (error);
2533 }
2534 
2535 int
2536 kern_statat(struct thread *td, int flag, int fd, const char *path,
2537     enum uio_seg pathseg, struct stat *sbp)
2538 {
2539 	struct nameidata nd;
2540 	int error;
2541 
2542 	if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2543 	    AT_EMPTY_PATH)) != 0)
2544 		return (EINVAL);
2545 
2546 	NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
2547 	    AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
2548 	    AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights);
2549 
2550 	if ((error = namei(&nd)) != 0) {
2551 		if (error == ENOTDIR &&
2552 		    (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
2553 			error = kern_fstat(td, fd, sbp);
2554 		return (error);
2555 	}
2556 	error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED);
2557 	NDFREE_PNBUF(&nd);
2558 	vput(nd.ni_vp);
2559 #ifdef __STAT_TIME_T_EXT
2560 	sbp->st_atim_ext = 0;
2561 	sbp->st_mtim_ext = 0;
2562 	sbp->st_ctim_ext = 0;
2563 	sbp->st_btim_ext = 0;
2564 #endif
2565 #ifdef KTRACE
2566 	if (KTRPOINT(td, KTR_STRUCT))
2567 		ktrstat_error(sbp, error);
2568 #endif
2569 	return (error);
2570 }
2571 
2572 #if defined(COMPAT_FREEBSD11)
2573 /*
2574  * Implementation of the NetBSD [l]stat() functions.
2575  */
2576 int
2577 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2578 {
2579 	struct freebsd11_stat sb11;
2580 	int error;
2581 
2582 	error = freebsd11_cvtstat(sb, &sb11);
2583 	if (error != 0)
2584 		return (error);
2585 
2586 	bzero(nsb, sizeof(*nsb));
2587 	CP(sb11, *nsb, st_dev);
2588 	CP(sb11, *nsb, st_ino);
2589 	CP(sb11, *nsb, st_mode);
2590 	CP(sb11, *nsb, st_nlink);
2591 	CP(sb11, *nsb, st_uid);
2592 	CP(sb11, *nsb, st_gid);
2593 	CP(sb11, *nsb, st_rdev);
2594 	CP(sb11, *nsb, st_atim);
2595 	CP(sb11, *nsb, st_mtim);
2596 	CP(sb11, *nsb, st_ctim);
2597 	CP(sb11, *nsb, st_size);
2598 	CP(sb11, *nsb, st_blocks);
2599 	CP(sb11, *nsb, st_blksize);
2600 	CP(sb11, *nsb, st_flags);
2601 	CP(sb11, *nsb, st_gen);
2602 	CP(sb11, *nsb, st_birthtim);
2603 	return (0);
2604 }
2605 
2606 #ifndef _SYS_SYSPROTO_H_
2607 struct freebsd11_nstat_args {
2608 	char	*path;
2609 	struct nstat *ub;
2610 };
2611 #endif
2612 int
2613 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2614 {
2615 	struct stat sb;
2616 	struct nstat nsb;
2617 	int error;
2618 
2619 	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2620 	if (error != 0)
2621 		return (error);
2622 	error = freebsd11_cvtnstat(&sb, &nsb);
2623 	if (error == 0)
2624 		error = copyout(&nsb, uap->ub, sizeof (nsb));
2625 	return (error);
2626 }
2627 
2628 /*
2629  * NetBSD lstat.  Get file status; this version does not follow links.
2630  */
2631 #ifndef _SYS_SYSPROTO_H_
2632 struct freebsd11_nlstat_args {
2633 	char	*path;
2634 	struct nstat *ub;
2635 };
2636 #endif
2637 int
2638 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2639 {
2640 	struct stat sb;
2641 	struct nstat nsb;
2642 	int error;
2643 
2644 	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2645 	    UIO_USERSPACE, &sb);
2646 	if (error != 0)
2647 		return (error);
2648 	error = freebsd11_cvtnstat(&sb, &nsb);
2649 	if (error == 0)
2650 		error = copyout(&nsb, uap->ub, sizeof (nsb));
2651 	return (error);
2652 }
2653 #endif /* COMPAT_FREEBSD11 */
2654 
2655 /*
2656  * Get configurable pathname variables.
2657  */
2658 #ifndef _SYS_SYSPROTO_H_
2659 struct pathconf_args {
2660 	char	*path;
2661 	int	name;
2662 };
2663 #endif
2664 int
2665 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2666 {
2667 	long value;
2668 	int error;
2669 
2670 	error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2671 	    &value);
2672 	if (error == 0)
2673 		td->td_retval[0] = value;
2674 	return (error);
2675 }
2676 
2677 #ifndef _SYS_SYSPROTO_H_
2678 struct lpathconf_args {
2679 	char	*path;
2680 	int	name;
2681 };
2682 #endif
2683 int
2684 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2685 {
2686 	long value;
2687 	int error;
2688 
2689 	error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2690 	    NOFOLLOW, &value);
2691 	if (error == 0)
2692 		td->td_retval[0] = value;
2693 	return (error);
2694 }
2695 
2696 int
2697 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2698     int name, u_long flags, long *valuep)
2699 {
2700 	struct nameidata nd;
2701 	int error;
2702 
2703 	NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2704 	       pathseg, path);
2705 	if ((error = namei(&nd)) != 0)
2706 		return (error);
2707 	NDFREE_PNBUF(&nd);
2708 
2709 	error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2710 	vput(nd.ni_vp);
2711 	return (error);
2712 }
2713 
2714 /*
2715  * Return target name of a symbolic link.
2716  */
2717 #ifndef _SYS_SYSPROTO_H_
2718 struct readlink_args {
2719 	char	*path;
2720 	char	*buf;
2721 	size_t	count;
2722 };
2723 #endif
2724 int
2725 sys_readlink(struct thread *td, struct readlink_args *uap)
2726 {
2727 
2728 	return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2729 	    uap->buf, UIO_USERSPACE, uap->count));
2730 }
2731 #ifndef _SYS_SYSPROTO_H_
2732 struct readlinkat_args {
2733 	int	fd;
2734 	char	*path;
2735 	char	*buf;
2736 	size_t	bufsize;
2737 };
2738 #endif
2739 int
2740 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2741 {
2742 
2743 	return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2744 	    uap->buf, UIO_USERSPACE, uap->bufsize));
2745 }
2746 
2747 int
2748 kern_readlinkat(struct thread *td, int fd, const char *path,
2749     enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2750 {
2751 	struct vnode *vp;
2752 	struct nameidata nd;
2753 	int error;
2754 
2755 	if (count > IOSIZE_MAX)
2756 		return (EINVAL);
2757 
2758 	NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
2759 	    EMPTYPATH, pathseg, path, fd);
2760 
2761 	if ((error = namei(&nd)) != 0)
2762 		return (error);
2763 	NDFREE_PNBUF(&nd);
2764 	vp = nd.ni_vp;
2765 
2766 	error = kern_readlink_vp(vp, buf, bufseg, count, td);
2767 	vput(vp);
2768 
2769 	return (error);
2770 }
2771 
2772 /*
2773  * Helper function to readlink from a vnode
2774  */
2775 static int
2776 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2777     struct thread *td)
2778 {
2779 	struct iovec aiov;
2780 	struct uio auio;
2781 	int error;
2782 
2783 	ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2784 #ifdef MAC
2785 	error = mac_vnode_check_readlink(td->td_ucred, vp);
2786 	if (error != 0)
2787 		return (error);
2788 #endif
2789 	if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2790 		return (EINVAL);
2791 
2792 	aiov.iov_base = buf;
2793 	aiov.iov_len = count;
2794 	auio.uio_iov = &aiov;
2795 	auio.uio_iovcnt = 1;
2796 	auio.uio_offset = 0;
2797 	auio.uio_rw = UIO_READ;
2798 	auio.uio_segflg = bufseg;
2799 	auio.uio_td = td;
2800 	auio.uio_resid = count;
2801 	error = VOP_READLINK(vp, &auio, td->td_ucred);
2802 	td->td_retval[0] = count - auio.uio_resid;
2803 	return (error);
2804 }
2805 
2806 /*
2807  * Common implementation code for chflags() and fchflags().
2808  */
2809 static int
2810 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2811 {
2812 	struct mount *mp;
2813 	struct vattr vattr;
2814 	int error;
2815 
2816 	/* We can't support the value matching VNOVAL. */
2817 	if (flags == VNOVAL)
2818 		return (EOPNOTSUPP);
2819 
2820 	/*
2821 	 * Prevent non-root users from setting flags on devices.  When
2822 	 * a device is reused, users can retain ownership of the device
2823 	 * if they are allowed to set flags and programs assume that
2824 	 * chown can't fail when done as root.
2825 	 */
2826 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
2827 		error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2828 		if (error != 0)
2829 			return (error);
2830 	}
2831 
2832 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2833 		return (error);
2834 	VATTR_NULL(&vattr);
2835 	vattr.va_flags = flags;
2836 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2837 #ifdef MAC
2838 	error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2839 	if (error == 0)
2840 #endif
2841 		error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2842 	VOP_UNLOCK(vp);
2843 	vn_finished_write(mp);
2844 	return (error);
2845 }
2846 
2847 /*
2848  * Change flags of a file given a path name.
2849  */
2850 #ifndef _SYS_SYSPROTO_H_
2851 struct chflags_args {
2852 	const char *path;
2853 	u_long	flags;
2854 };
2855 #endif
2856 int
2857 sys_chflags(struct thread *td, struct chflags_args *uap)
2858 {
2859 
2860 	return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2861 	    uap->flags, 0));
2862 }
2863 
2864 #ifndef _SYS_SYSPROTO_H_
2865 struct chflagsat_args {
2866 	int	fd;
2867 	const char *path;
2868 	u_long	flags;
2869 	int	atflag;
2870 }
2871 #endif
2872 int
2873 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2874 {
2875 
2876 	return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2877 	    uap->flags, uap->atflag));
2878 }
2879 
2880 /*
2881  * Same as chflags() but doesn't follow symlinks.
2882  */
2883 #ifndef _SYS_SYSPROTO_H_
2884 struct lchflags_args {
2885 	const char *path;
2886 	u_long flags;
2887 };
2888 #endif
2889 int
2890 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2891 {
2892 
2893 	return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2894 	    uap->flags, AT_SYMLINK_NOFOLLOW));
2895 }
2896 
2897 static int
2898 kern_chflagsat(struct thread *td, int fd, const char *path,
2899     enum uio_seg pathseg, u_long flags, int atflag)
2900 {
2901 	struct nameidata nd;
2902 	int error;
2903 
2904 	if ((atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2905 	    AT_EMPTY_PATH)) != 0)
2906 		return (EINVAL);
2907 
2908 	AUDIT_ARG_FFLAGS(flags);
2909 	NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2910 	    AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2911 	    fd, &cap_fchflags_rights);
2912 	if ((error = namei(&nd)) != 0)
2913 		return (error);
2914 	NDFREE_PNBUF(&nd);
2915 	error = setfflags(td, nd.ni_vp, flags);
2916 	vrele(nd.ni_vp);
2917 	return (error);
2918 }
2919 
2920 /*
2921  * Change flags of a file given a file descriptor.
2922  */
2923 #ifndef _SYS_SYSPROTO_H_
2924 struct fchflags_args {
2925 	int	fd;
2926 	u_long	flags;
2927 };
2928 #endif
2929 int
2930 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2931 {
2932 	struct file *fp;
2933 	int error;
2934 
2935 	AUDIT_ARG_FD(uap->fd);
2936 	AUDIT_ARG_FFLAGS(uap->flags);
2937 	error = getvnode(td, uap->fd, &cap_fchflags_rights,
2938 	    &fp);
2939 	if (error != 0)
2940 		return (error);
2941 #ifdef AUDIT
2942 	if (AUDITING_TD(td)) {
2943 		vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2944 		AUDIT_ARG_VNODE1(fp->f_vnode);
2945 		VOP_UNLOCK(fp->f_vnode);
2946 	}
2947 #endif
2948 	error = setfflags(td, fp->f_vnode, uap->flags);
2949 	fdrop(fp, td);
2950 	return (error);
2951 }
2952 
2953 /*
2954  * Common implementation code for chmod(), lchmod() and fchmod().
2955  */
2956 int
2957 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2958 {
2959 	struct mount *mp;
2960 	struct vattr vattr;
2961 	int error;
2962 
2963 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2964 		return (error);
2965 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2966 	VATTR_NULL(&vattr);
2967 	vattr.va_mode = mode & ALLPERMS;
2968 #ifdef MAC
2969 	error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2970 	if (error == 0)
2971 #endif
2972 		error = VOP_SETATTR(vp, &vattr, cred);
2973 	VOP_UNLOCK(vp);
2974 	vn_finished_write(mp);
2975 	return (error);
2976 }
2977 
2978 /*
2979  * Change mode of a file given path name.
2980  */
2981 #ifndef _SYS_SYSPROTO_H_
2982 struct chmod_args {
2983 	char	*path;
2984 	int	mode;
2985 };
2986 #endif
2987 int
2988 sys_chmod(struct thread *td, struct chmod_args *uap)
2989 {
2990 
2991 	return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2992 	    uap->mode, 0));
2993 }
2994 
2995 #ifndef _SYS_SYSPROTO_H_
2996 struct fchmodat_args {
2997 	int	dirfd;
2998 	char	*path;
2999 	mode_t	mode;
3000 	int	flag;
3001 }
3002 #endif
3003 int
3004 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
3005 {
3006 
3007 	return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
3008 	    uap->mode, uap->flag));
3009 }
3010 
3011 /*
3012  * Change mode of a file given path name (don't follow links.)
3013  */
3014 #ifndef _SYS_SYSPROTO_H_
3015 struct lchmod_args {
3016 	char	*path;
3017 	int	mode;
3018 };
3019 #endif
3020 int
3021 sys_lchmod(struct thread *td, struct lchmod_args *uap)
3022 {
3023 
3024 	return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3025 	    uap->mode, AT_SYMLINK_NOFOLLOW));
3026 }
3027 
3028 int
3029 kern_fchmodat(struct thread *td, int fd, const char *path,
3030     enum uio_seg pathseg, mode_t mode, int flag)
3031 {
3032 	struct nameidata nd;
3033 	int error;
3034 
3035 	if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3036 	    AT_EMPTY_PATH)) != 0)
3037 		return (EINVAL);
3038 
3039 	AUDIT_ARG_MODE(mode);
3040 	NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3041 	    AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
3042 	    fd, &cap_fchmod_rights);
3043 	if ((error = namei(&nd)) != 0)
3044 		return (error);
3045 	NDFREE_PNBUF(&nd);
3046 	error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
3047 	vrele(nd.ni_vp);
3048 	return (error);
3049 }
3050 
3051 /*
3052  * Change mode of a file given a file descriptor.
3053  */
3054 #ifndef _SYS_SYSPROTO_H_
3055 struct fchmod_args {
3056 	int	fd;
3057 	int	mode;
3058 };
3059 #endif
3060 int
3061 sys_fchmod(struct thread *td, struct fchmod_args *uap)
3062 {
3063 	struct file *fp;
3064 	int error;
3065 
3066 	AUDIT_ARG_FD(uap->fd);
3067 	AUDIT_ARG_MODE(uap->mode);
3068 
3069 	error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
3070 	if (error != 0)
3071 		return (error);
3072 	error = fo_chmod(fp, uap->mode, td->td_ucred, td);
3073 	fdrop(fp, td);
3074 	return (error);
3075 }
3076 
3077 /*
3078  * Common implementation for chown(), lchown(), and fchown()
3079  */
3080 int
3081 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
3082     gid_t gid)
3083 {
3084 	struct mount *mp;
3085 	struct vattr vattr;
3086 	int error;
3087 
3088 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
3089 		return (error);
3090 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3091 	VATTR_NULL(&vattr);
3092 	vattr.va_uid = uid;
3093 	vattr.va_gid = gid;
3094 #ifdef MAC
3095 	error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
3096 	    vattr.va_gid);
3097 	if (error == 0)
3098 #endif
3099 		error = VOP_SETATTR(vp, &vattr, cred);
3100 	VOP_UNLOCK(vp);
3101 	vn_finished_write(mp);
3102 	return (error);
3103 }
3104 
3105 /*
3106  * Set ownership given a path name.
3107  */
3108 #ifndef _SYS_SYSPROTO_H_
3109 struct chown_args {
3110 	char	*path;
3111 	int	uid;
3112 	int	gid;
3113 };
3114 #endif
3115 int
3116 sys_chown(struct thread *td, struct chown_args *uap)
3117 {
3118 
3119 	return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
3120 	    uap->gid, 0));
3121 }
3122 
3123 #ifndef _SYS_SYSPROTO_H_
3124 struct fchownat_args {
3125 	int fd;
3126 	const char * path;
3127 	uid_t uid;
3128 	gid_t gid;
3129 	int flag;
3130 };
3131 #endif
3132 int
3133 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3134 {
3135 
3136 	return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3137 	    uap->gid, uap->flag));
3138 }
3139 
3140 int
3141 kern_fchownat(struct thread *td, int fd, const char *path,
3142     enum uio_seg pathseg, int uid, int gid, int flag)
3143 {
3144 	struct nameidata nd;
3145 	int error;
3146 
3147 	if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3148 	    AT_EMPTY_PATH)) != 0)
3149 		return (EINVAL);
3150 
3151 	AUDIT_ARG_OWNER(uid, gid);
3152 	NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3153 	    AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
3154 	    fd, &cap_fchown_rights);
3155 
3156 	if ((error = namei(&nd)) != 0)
3157 		return (error);
3158 	NDFREE_PNBUF(&nd);
3159 	error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3160 	vrele(nd.ni_vp);
3161 	return (error);
3162 }
3163 
3164 /*
3165  * Set ownership given a path name, do not cross symlinks.
3166  */
3167 #ifndef _SYS_SYSPROTO_H_
3168 struct lchown_args {
3169 	char	*path;
3170 	int	uid;
3171 	int	gid;
3172 };
3173 #endif
3174 int
3175 sys_lchown(struct thread *td, struct lchown_args *uap)
3176 {
3177 
3178 	return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3179 	    uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3180 }
3181 
3182 /*
3183  * Set ownership given a file descriptor.
3184  */
3185 #ifndef _SYS_SYSPROTO_H_
3186 struct fchown_args {
3187 	int	fd;
3188 	int	uid;
3189 	int	gid;
3190 };
3191 #endif
3192 int
3193 sys_fchown(struct thread *td, struct fchown_args *uap)
3194 {
3195 	struct file *fp;
3196 	int error;
3197 
3198 	AUDIT_ARG_FD(uap->fd);
3199 	AUDIT_ARG_OWNER(uap->uid, uap->gid);
3200 	error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3201 	if (error != 0)
3202 		return (error);
3203 	error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3204 	fdrop(fp, td);
3205 	return (error);
3206 }
3207 
3208 /*
3209  * Common implementation code for utimes(), lutimes(), and futimes().
3210  */
3211 static int
3212 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3213     struct timespec *tsp)
3214 {
3215 	struct timeval tv[2];
3216 	const struct timeval *tvp;
3217 	int error;
3218 
3219 	if (usrtvp == NULL) {
3220 		vfs_timestamp(&tsp[0]);
3221 		tsp[1] = tsp[0];
3222 	} else {
3223 		if (tvpseg == UIO_SYSSPACE) {
3224 			tvp = usrtvp;
3225 		} else {
3226 			if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3227 				return (error);
3228 			tvp = tv;
3229 		}
3230 
3231 		if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3232 		    tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3233 			return (EINVAL);
3234 		TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3235 		TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3236 	}
3237 	return (0);
3238 }
3239 
3240 /*
3241  * Common implementation code for futimens(), utimensat().
3242  */
3243 #define	UTIMENS_NULL	0x1
3244 #define	UTIMENS_EXIT	0x2
3245 static int
3246 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3247     struct timespec *tsp, int *retflags)
3248 {
3249 	struct timespec tsnow;
3250 	int error;
3251 
3252 	vfs_timestamp(&tsnow);
3253 	*retflags = 0;
3254 	if (usrtsp == NULL) {
3255 		tsp[0] = tsnow;
3256 		tsp[1] = tsnow;
3257 		*retflags |= UTIMENS_NULL;
3258 		return (0);
3259 	}
3260 	if (tspseg == UIO_SYSSPACE) {
3261 		tsp[0] = usrtsp[0];
3262 		tsp[1] = usrtsp[1];
3263 	} else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3264 		return (error);
3265 	if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3266 		*retflags |= UTIMENS_EXIT;
3267 	if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3268 		*retflags |= UTIMENS_NULL;
3269 	if (tsp[0].tv_nsec == UTIME_OMIT)
3270 		tsp[0].tv_sec = VNOVAL;
3271 	else if (tsp[0].tv_nsec == UTIME_NOW)
3272 		tsp[0] = tsnow;
3273 	else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3274 		return (EINVAL);
3275 	if (tsp[1].tv_nsec == UTIME_OMIT)
3276 		tsp[1].tv_sec = VNOVAL;
3277 	else if (tsp[1].tv_nsec == UTIME_NOW)
3278 		tsp[1] = tsnow;
3279 	else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3280 		return (EINVAL);
3281 
3282 	return (0);
3283 }
3284 
3285 /*
3286  * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3287  * and utimensat().
3288  */
3289 static int
3290 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3291     int numtimes, int nullflag)
3292 {
3293 	struct mount *mp;
3294 	struct vattr vattr;
3295 	int error;
3296 	bool setbirthtime;
3297 
3298 	setbirthtime = false;
3299 	vattr.va_birthtime.tv_sec = VNOVAL;
3300 	vattr.va_birthtime.tv_nsec = 0;
3301 
3302 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
3303 		return (error);
3304 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3305 	if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred) == 0 &&
3306 	    timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3307 		setbirthtime = true;
3308 	VATTR_NULL(&vattr);
3309 	vattr.va_atime = ts[0];
3310 	vattr.va_mtime = ts[1];
3311 	if (setbirthtime)
3312 		vattr.va_birthtime = ts[1];
3313 	if (numtimes > 2)
3314 		vattr.va_birthtime = ts[2];
3315 	if (nullflag)
3316 		vattr.va_vaflags |= VA_UTIMES_NULL;
3317 #ifdef MAC
3318 	error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3319 	    vattr.va_mtime);
3320 #endif
3321 	if (error == 0)
3322 		error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3323 	VOP_UNLOCK(vp);
3324 	vn_finished_write(mp);
3325 	return (error);
3326 }
3327 
3328 /*
3329  * Set the access and modification times of a file.
3330  */
3331 #ifndef _SYS_SYSPROTO_H_
3332 struct utimes_args {
3333 	char	*path;
3334 	struct	timeval *tptr;
3335 };
3336 #endif
3337 int
3338 sys_utimes(struct thread *td, struct utimes_args *uap)
3339 {
3340 
3341 	return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3342 	    uap->tptr, UIO_USERSPACE));
3343 }
3344 
3345 #ifndef _SYS_SYSPROTO_H_
3346 struct futimesat_args {
3347 	int fd;
3348 	const char * path;
3349 	const struct timeval * times;
3350 };
3351 #endif
3352 int
3353 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3354 {
3355 
3356 	return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3357 	    uap->times, UIO_USERSPACE));
3358 }
3359 
3360 int
3361 kern_utimesat(struct thread *td, int fd, const char *path,
3362     enum uio_seg pathseg, const struct timeval *tptr, enum uio_seg tptrseg)
3363 {
3364 	struct nameidata nd;
3365 	struct timespec ts[2];
3366 	int error;
3367 
3368 	if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3369 		return (error);
3370 	NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3371 	    &cap_futimes_rights);
3372 
3373 	if ((error = namei(&nd)) != 0)
3374 		return (error);
3375 	NDFREE_PNBUF(&nd);
3376 	error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3377 	vrele(nd.ni_vp);
3378 	return (error);
3379 }
3380 
3381 /*
3382  * Set the access and modification times of a file.
3383  */
3384 #ifndef _SYS_SYSPROTO_H_
3385 struct lutimes_args {
3386 	char	*path;
3387 	struct	timeval *tptr;
3388 };
3389 #endif
3390 int
3391 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3392 {
3393 
3394 	return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3395 	    UIO_USERSPACE));
3396 }
3397 
3398 int
3399 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3400     const struct timeval *tptr, enum uio_seg tptrseg)
3401 {
3402 	struct timespec ts[2];
3403 	struct nameidata nd;
3404 	int error;
3405 
3406 	if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3407 		return (error);
3408 	NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path);
3409 	if ((error = namei(&nd)) != 0)
3410 		return (error);
3411 	NDFREE_PNBUF(&nd);
3412 	error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3413 	vrele(nd.ni_vp);
3414 	return (error);
3415 }
3416 
3417 /*
3418  * Set the access and modification times of a file.
3419  */
3420 #ifndef _SYS_SYSPROTO_H_
3421 struct futimes_args {
3422 	int	fd;
3423 	struct	timeval *tptr;
3424 };
3425 #endif
3426 int
3427 sys_futimes(struct thread *td, struct futimes_args *uap)
3428 {
3429 
3430 	return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3431 }
3432 
3433 int
3434 kern_futimes(struct thread *td, int fd, const struct timeval *tptr,
3435     enum uio_seg tptrseg)
3436 {
3437 	struct timespec ts[2];
3438 	struct file *fp;
3439 	int error;
3440 
3441 	AUDIT_ARG_FD(fd);
3442 	error = getutimes(tptr, tptrseg, ts);
3443 	if (error != 0)
3444 		return (error);
3445 	error = getvnode(td, fd, &cap_futimes_rights, &fp);
3446 	if (error != 0)
3447 		return (error);
3448 #ifdef AUDIT
3449 	if (AUDITING_TD(td)) {
3450 		vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3451 		AUDIT_ARG_VNODE1(fp->f_vnode);
3452 		VOP_UNLOCK(fp->f_vnode);
3453 	}
3454 #endif
3455 	error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3456 	fdrop(fp, td);
3457 	return (error);
3458 }
3459 
3460 int
3461 sys_futimens(struct thread *td, struct futimens_args *uap)
3462 {
3463 
3464 	return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3465 }
3466 
3467 int
3468 kern_futimens(struct thread *td, int fd, const struct timespec *tptr,
3469     enum uio_seg tptrseg)
3470 {
3471 	struct timespec ts[2];
3472 	struct file *fp;
3473 	int error, flags;
3474 
3475 	AUDIT_ARG_FD(fd);
3476 	error = getutimens(tptr, tptrseg, ts, &flags);
3477 	if (error != 0)
3478 		return (error);
3479 	if (flags & UTIMENS_EXIT)
3480 		return (0);
3481 	error = getvnode(td, fd, &cap_futimes_rights, &fp);
3482 	if (error != 0)
3483 		return (error);
3484 #ifdef AUDIT
3485 	if (AUDITING_TD(td)) {
3486 		vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3487 		AUDIT_ARG_VNODE1(fp->f_vnode);
3488 		VOP_UNLOCK(fp->f_vnode);
3489 	}
3490 #endif
3491 	error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3492 	fdrop(fp, td);
3493 	return (error);
3494 }
3495 
3496 int
3497 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3498 {
3499 
3500 	return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3501 	    uap->times, UIO_USERSPACE, uap->flag));
3502 }
3503 
3504 int
3505 kern_utimensat(struct thread *td, int fd, const char *path,
3506     enum uio_seg pathseg, const struct timespec *tptr, enum uio_seg tptrseg,
3507     int flag)
3508 {
3509 	struct nameidata nd;
3510 	struct timespec ts[2];
3511 	int error, flags;
3512 
3513 	if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3514 	    AT_EMPTY_PATH)) != 0)
3515 		return (EINVAL);
3516 
3517 	if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3518 		return (error);
3519 	NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3520 	    AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1,
3521 	    pathseg, path, fd, &cap_futimes_rights);
3522 	if ((error = namei(&nd)) != 0)
3523 		return (error);
3524 	/*
3525 	 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3526 	 * POSIX states:
3527 	 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3528 	 * "Search permission is denied by a component of the path prefix."
3529 	 */
3530 	NDFREE_PNBUF(&nd);
3531 	if ((flags & UTIMENS_EXIT) == 0)
3532 		error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3533 	vrele(nd.ni_vp);
3534 	return (error);
3535 }
3536 
3537 /*
3538  * Truncate a file given its path name.
3539  */
3540 #ifndef _SYS_SYSPROTO_H_
3541 struct truncate_args {
3542 	char	*path;
3543 	int	pad;
3544 	off_t	length;
3545 };
3546 #endif
3547 int
3548 sys_truncate(struct thread *td, struct truncate_args *uap)
3549 {
3550 
3551 	return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3552 }
3553 
3554 int
3555 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3556     off_t length)
3557 {
3558 	struct mount *mp;
3559 	struct vnode *vp;
3560 	void *rl_cookie;
3561 	struct nameidata nd;
3562 	int error;
3563 
3564 	if (length < 0)
3565 		return (EINVAL);
3566 	NDPREINIT(&nd);
3567 retry:
3568 	NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path);
3569 	if ((error = namei(&nd)) != 0)
3570 		return (error);
3571 	vp = nd.ni_vp;
3572 	NDFREE_PNBUF(&nd);
3573 	rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3574 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
3575 		vn_rangelock_unlock(vp, rl_cookie);
3576 		vrele(vp);
3577 		return (error);
3578 	}
3579 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3580 	if (vp->v_type == VDIR) {
3581 		error = EISDIR;
3582 		goto out;
3583 	}
3584 #ifdef MAC
3585 	error = mac_vnode_check_write(td->td_ucred, NOCRED, vp);
3586 	if (error != 0)
3587 		goto out;
3588 #endif
3589 	error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
3590 	if (error != 0)
3591 		goto out;
3592 
3593 	error = vn_truncate_locked(vp, length, false, td->td_ucred);
3594 out:
3595 	VOP_UNLOCK(vp);
3596 	vn_finished_write(mp);
3597 	vn_rangelock_unlock(vp, rl_cookie);
3598 	vrele(vp);
3599 	if (error == ERELOOKUP)
3600 		goto retry;
3601 	return (error);
3602 }
3603 
3604 #if defined(COMPAT_43)
3605 /*
3606  * Truncate a file given its path name.
3607  */
3608 #ifndef _SYS_SYSPROTO_H_
3609 struct otruncate_args {
3610 	char	*path;
3611 	long	length;
3612 };
3613 #endif
3614 int
3615 otruncate(struct thread *td, struct otruncate_args *uap)
3616 {
3617 
3618 	return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3619 }
3620 #endif /* COMPAT_43 */
3621 
3622 #if defined(COMPAT_FREEBSD6)
3623 /* Versions with the pad argument */
3624 int
3625 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3626 {
3627 
3628 	return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3629 }
3630 
3631 int
3632 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3633 {
3634 
3635 	return (kern_ftruncate(td, uap->fd, uap->length));
3636 }
3637 #endif
3638 
3639 int
3640 kern_fsync(struct thread *td, int fd, bool fullsync)
3641 {
3642 	struct vnode *vp;
3643 	struct mount *mp;
3644 	struct file *fp;
3645 	int error;
3646 
3647 	AUDIT_ARG_FD(fd);
3648 	error = getvnode(td, fd, &cap_fsync_rights, &fp);
3649 	if (error != 0)
3650 		return (error);
3651 	vp = fp->f_vnode;
3652 #if 0
3653 	if (!fullsync)
3654 		/* XXXKIB: compete outstanding aio writes */;
3655 #endif
3656 retry:
3657 	error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
3658 	if (error != 0)
3659 		goto drop;
3660 	vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY);
3661 	AUDIT_ARG_VNODE1(vp);
3662 	vnode_pager_clean_async(vp);
3663 	error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3664 	VOP_UNLOCK(vp);
3665 	vn_finished_write(mp);
3666 	if (error == ERELOOKUP)
3667 		goto retry;
3668 drop:
3669 	fdrop(fp, td);
3670 	return (error);
3671 }
3672 
3673 /*
3674  * Sync an open file.
3675  */
3676 #ifndef _SYS_SYSPROTO_H_
3677 struct fsync_args {
3678 	int	fd;
3679 };
3680 #endif
3681 int
3682 sys_fsync(struct thread *td, struct fsync_args *uap)
3683 {
3684 
3685 	return (kern_fsync(td, uap->fd, true));
3686 }
3687 
3688 int
3689 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3690 {
3691 
3692 	return (kern_fsync(td, uap->fd, false));
3693 }
3694 
3695 /*
3696  * Rename files.  Source and destination must either both be directories, or
3697  * both not be directories.  If target is a directory, it must be empty.
3698  */
3699 #ifndef _SYS_SYSPROTO_H_
3700 struct rename_args {
3701 	char	*from;
3702 	char	*to;
3703 };
3704 #endif
3705 int
3706 sys_rename(struct thread *td, struct rename_args *uap)
3707 {
3708 
3709 	return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3710 	    uap->to, UIO_USERSPACE));
3711 }
3712 
3713 #ifndef _SYS_SYSPROTO_H_
3714 struct renameat_args {
3715 	int	oldfd;
3716 	char	*old;
3717 	int	newfd;
3718 	char	*new;
3719 };
3720 #endif
3721 int
3722 sys_renameat(struct thread *td, struct renameat_args *uap)
3723 {
3724 
3725 	return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3726 	    UIO_USERSPACE));
3727 }
3728 
3729 #ifdef MAC
3730 static int
3731 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3732     const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3733 {
3734 	int error;
3735 
3736 	NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3737 	    pathseg, old, oldfd, &cap_renameat_source_rights);
3738 	if ((error = namei(fromnd)) != 0)
3739 		return (error);
3740 	error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3741 	    fromnd->ni_vp, &fromnd->ni_cnd);
3742 	VOP_UNLOCK(fromnd->ni_dvp);
3743 	if (fromnd->ni_dvp != fromnd->ni_vp)
3744 		VOP_UNLOCK(fromnd->ni_vp);
3745 	if (error != 0) {
3746 		NDFREE_PNBUF(fromnd);
3747 		vrele(fromnd->ni_dvp);
3748 		vrele(fromnd->ni_vp);
3749 	}
3750 	return (error);
3751 }
3752 #endif
3753 
3754 int
3755 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3756     const char *new, enum uio_seg pathseg)
3757 {
3758 	struct mount *mp = NULL;
3759 	struct vnode *tvp, *fvp, *tdvp;
3760 	struct nameidata fromnd, tond;
3761 	uint64_t tondflags;
3762 	int error;
3763 	short irflag;
3764 
3765 again:
3766 	bwillwrite();
3767 #ifdef MAC
3768 	if (mac_vnode_check_rename_from_enabled()) {
3769 		error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3770 		    &fromnd);
3771 		if (error != 0)
3772 			return (error);
3773 	} else {
3774 #endif
3775 	NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | AUDITVNODE1,
3776 	    pathseg, old, oldfd, &cap_renameat_source_rights);
3777 	if ((error = namei(&fromnd)) != 0)
3778 		return (error);
3779 #ifdef MAC
3780 	}
3781 #endif
3782 	fvp = fromnd.ni_vp;
3783 	tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | AUDITVNODE2;
3784 	if (fromnd.ni_vp->v_type == VDIR)
3785 		tondflags |= WILLBEDIR;
3786 	NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3787 	    &cap_renameat_target_rights);
3788 	if ((error = namei(&tond)) != 0) {
3789 		/* Translate error code for rename("dir1", "dir2/."). */
3790 		if (error == EISDIR && fvp->v_type == VDIR)
3791 			error = EINVAL;
3792 		NDFREE_PNBUF(&fromnd);
3793 		vrele(fromnd.ni_dvp);
3794 		vrele(fvp);
3795 		goto out1;
3796 	}
3797 	tdvp = tond.ni_dvp;
3798 	tvp = tond.ni_vp;
3799 	error = vn_start_write(fvp, &mp, V_NOWAIT);
3800 	if (error != 0) {
3801 		NDFREE_PNBUF(&fromnd);
3802 		NDFREE_PNBUF(&tond);
3803 		if (tvp != NULL)
3804 			vput(tvp);
3805 		if (tdvp == tvp)
3806 			vrele(tdvp);
3807 		else
3808 			vput(tdvp);
3809 		vrele(fromnd.ni_dvp);
3810 		vrele(fvp);
3811 		error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH);
3812 		if (error != 0)
3813 			return (error);
3814 		goto again;
3815 	}
3816 	irflag = vn_irflag_read(fvp);
3817 	if (((irflag & VIRF_NAMEDATTR) != 0 && tdvp != fromnd.ni_dvp) ||
3818 	    (irflag & VIRF_NAMEDDIR) != 0) {
3819 		error = EINVAL;
3820 		goto out;
3821 	}
3822 	if (tvp != NULL) {
3823 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3824 			error = ENOTDIR;
3825 			goto out;
3826 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3827 			error = EISDIR;
3828 			goto out;
3829 		}
3830 #ifdef CAPABILITIES
3831 		if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3832 			/*
3833 			 * If the target already exists we require CAP_UNLINKAT
3834 			 * from 'newfd', when newfd was used for the lookup.
3835 			 */
3836 			error = cap_check(&tond.ni_filecaps.fc_rights,
3837 			    &cap_unlinkat_rights);
3838 			if (error != 0)
3839 				goto out;
3840 		}
3841 #endif
3842 	}
3843 	if (fvp == tdvp) {
3844 		error = EINVAL;
3845 		goto out;
3846 	}
3847 	/*
3848 	 * If the source is the same as the destination (that is, if they
3849 	 * are links to the same vnode), then there is nothing to do.
3850 	 */
3851 	if (fvp == tvp)
3852 		error = ERESTART;
3853 #ifdef MAC
3854 	else
3855 		error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3856 		    tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3857 #endif
3858 out:
3859 	if (error == 0) {
3860 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3861 		    tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3862 		NDFREE_PNBUF(&fromnd);
3863 		NDFREE_PNBUF(&tond);
3864 	} else {
3865 		NDFREE_PNBUF(&fromnd);
3866 		NDFREE_PNBUF(&tond);
3867 		if (tvp != NULL)
3868 			vput(tvp);
3869 		if (tdvp == tvp)
3870 			vrele(tdvp);
3871 		else
3872 			vput(tdvp);
3873 		vrele(fromnd.ni_dvp);
3874 		vrele(fvp);
3875 	}
3876 	vn_finished_write(mp);
3877 out1:
3878 	if (error == ERESTART)
3879 		return (0);
3880 	if (error == ERELOOKUP)
3881 		goto again;
3882 	return (error);
3883 }
3884 
3885 /*
3886  * Make a directory file.
3887  */
3888 #ifndef _SYS_SYSPROTO_H_
3889 struct mkdir_args {
3890 	char	*path;
3891 	int	mode;
3892 };
3893 #endif
3894 int
3895 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3896 {
3897 
3898 	return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3899 	    uap->mode));
3900 }
3901 
3902 #ifndef _SYS_SYSPROTO_H_
3903 struct mkdirat_args {
3904 	int	fd;
3905 	char	*path;
3906 	mode_t	mode;
3907 };
3908 #endif
3909 int
3910 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3911 {
3912 
3913 	return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3914 }
3915 
3916 int
3917 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3918     int mode)
3919 {
3920 	struct mount *mp;
3921 	struct vattr vattr;
3922 	struct nameidata nd;
3923 	int error;
3924 
3925 	AUDIT_ARG_MODE(mode);
3926 	NDPREINIT(&nd);
3927 restart:
3928 	bwillwrite();
3929 	NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 |
3930 	    NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3931 	    segflg, path, fd, &cap_mkdirat_rights);
3932 	if ((error = namei(&nd)) != 0)
3933 		return (error);
3934 	if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3935 		NDFREE_PNBUF(&nd);
3936 		vput(nd.ni_dvp);
3937 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
3938 			return (error);
3939 		goto restart;
3940 	}
3941 	if ((vn_irflag_read(nd.ni_dvp) & VIRF_NAMEDDIR) != 0) {
3942 		error = EINVAL;
3943 		goto out;
3944 	}
3945 	VATTR_NULL(&vattr);
3946 	vattr.va_type = VDIR;
3947 	vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3948 #ifdef MAC
3949 	error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3950 	    &vattr);
3951 	if (error != 0)
3952 		goto out;
3953 #endif
3954 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3955 out:
3956 	NDFREE_PNBUF(&nd);
3957 	VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
3958 	vn_finished_write(mp);
3959 	if (error == ERELOOKUP)
3960 		goto restart;
3961 	return (error);
3962 }
3963 
3964 /*
3965  * Remove a directory file.
3966  */
3967 #ifndef _SYS_SYSPROTO_H_
3968 struct rmdir_args {
3969 	char	*path;
3970 };
3971 #endif
3972 int
3973 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3974 {
3975 
3976 	return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3977 	    0));
3978 }
3979 
3980 int
3981 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3982     enum uio_seg pathseg, int flag)
3983 {
3984 	struct mount *mp;
3985 	struct vnode *vp;
3986 	struct file *fp;
3987 	struct nameidata nd;
3988 	cap_rights_t rights;
3989 	int error;
3990 
3991 	fp = NULL;
3992 	if (fd != FD_NONE) {
3993 		error = getvnode(td, fd, cap_rights_init_one(&rights,
3994 		    CAP_LOOKUP), &fp);
3995 		if (error != 0)
3996 			return (error);
3997 	}
3998 
3999 	NDPREINIT(&nd);
4000 restart:
4001 	bwillwrite();
4002 	NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
4003 	    at2cnpflags(flag, AT_RESOLVE_BENEATH),
4004 	    pathseg, path, dfd, &cap_unlinkat_rights);
4005 	if ((error = namei(&nd)) != 0)
4006 		goto fdout;
4007 	vp = nd.ni_vp;
4008 	if (vp->v_type != VDIR) {
4009 		error = ENOTDIR;
4010 		goto out;
4011 	}
4012 	/*
4013 	 * No rmdir "." please.
4014 	 */
4015 	if (nd.ni_dvp == vp) {
4016 		error = EINVAL;
4017 		goto out;
4018 	}
4019 	/*
4020 	 * The root of a mounted filesystem cannot be deleted.
4021 	 */
4022 	if (vp->v_vflag & VV_ROOT) {
4023 		error = EBUSY;
4024 		goto out;
4025 	}
4026 
4027 	if (fp != NULL && fp->f_vnode != vp) {
4028 		if (VN_IS_DOOMED(fp->f_vnode))
4029 			error = EBADF;
4030 		else
4031 			error = EDEADLK;
4032 		goto out;
4033 	}
4034 
4035 #ifdef MAC
4036 	error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
4037 	    &nd.ni_cnd);
4038 	if (error != 0)
4039 		goto out;
4040 #endif
4041 	if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
4042 		NDFREE_PNBUF(&nd);
4043 		vput(vp);
4044 		if (nd.ni_dvp == vp)
4045 			vrele(nd.ni_dvp);
4046 		else
4047 			vput(nd.ni_dvp);
4048 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
4049 			goto fdout;
4050 		goto restart;
4051 	}
4052 	error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
4053 	vn_finished_write(mp);
4054 out:
4055 	NDFREE_PNBUF(&nd);
4056 	vput(vp);
4057 	if (nd.ni_dvp == vp)
4058 		vrele(nd.ni_dvp);
4059 	else
4060 		vput(nd.ni_dvp);
4061 	if (error == ERELOOKUP)
4062 		goto restart;
4063 fdout:
4064 	if (fp != NULL)
4065 		fdrop(fp, td);
4066 	return (error);
4067 }
4068 
4069 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
4070 int
4071 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
4072     long *basep, void (*func)(struct freebsd11_dirent *))
4073 {
4074 	struct freebsd11_dirent dstdp;
4075 	struct dirent *dp, *edp;
4076 	char *dirbuf;
4077 	off_t base;
4078 	ssize_t resid, ucount;
4079 	int error;
4080 
4081 	/* XXX arbitrary sanity limit on `count'. */
4082 	count = min(count, 64 * 1024);
4083 
4084 	dirbuf = malloc(count, M_TEMP, M_WAITOK);
4085 
4086 	error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
4087 	    UIO_SYSSPACE);
4088 	if (error != 0)
4089 		goto done;
4090 	if (basep != NULL)
4091 		*basep = base;
4092 
4093 	ucount = 0;
4094 	for (dp = (struct dirent *)dirbuf,
4095 	    edp = (struct dirent *)&dirbuf[count - resid];
4096 	    ucount < count && dp < edp; ) {
4097 		if (dp->d_reclen == 0)
4098 			break;
4099 		MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
4100 		if (dp->d_namlen >= sizeof(dstdp.d_name))
4101 			continue;
4102 		dstdp.d_type = dp->d_type;
4103 		dstdp.d_namlen = dp->d_namlen;
4104 		dstdp.d_fileno = dp->d_fileno;		/* truncate */
4105 		if (dstdp.d_fileno != dp->d_fileno) {
4106 			switch (ino64_trunc_error) {
4107 			default:
4108 			case 0:
4109 				break;
4110 			case 1:
4111 				error = EOVERFLOW;
4112 				goto done;
4113 			case 2:
4114 				dstdp.d_fileno = UINT32_MAX;
4115 				break;
4116 			}
4117 		}
4118 		dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
4119 		    ((dp->d_namlen + 1 + 3) &~ 3);
4120 		bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
4121 		bzero(dstdp.d_name + dstdp.d_namlen,
4122 		    dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
4123 		    dstdp.d_namlen);
4124 		MPASS(dstdp.d_reclen <= dp->d_reclen);
4125 		MPASS(ucount + dstdp.d_reclen <= count);
4126 		if (func != NULL)
4127 			func(&dstdp);
4128 		error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
4129 		if (error != 0)
4130 			break;
4131 		dp = (struct dirent *)((char *)dp + dp->d_reclen);
4132 		ucount += dstdp.d_reclen;
4133 	}
4134 
4135 done:
4136 	free(dirbuf, M_TEMP);
4137 	if (error == 0)
4138 		td->td_retval[0] = ucount;
4139 	return (error);
4140 }
4141 #endif /* COMPAT */
4142 
4143 #ifdef COMPAT_43
4144 static void
4145 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4146 {
4147 #if (BYTE_ORDER == LITTLE_ENDIAN)
4148 	/*
4149 	 * The expected low byte of dp->d_namlen is our dp->d_type.
4150 	 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4151 	 */
4152 	dp->d_type = dp->d_namlen;
4153 	dp->d_namlen = 0;
4154 #else
4155 	/*
4156 	 * The dp->d_type is the high byte of the expected dp->d_namlen,
4157 	 * so must be zero'ed.
4158 	 */
4159 	dp->d_type = 0;
4160 #endif
4161 }
4162 
4163 /*
4164  * Read a block of directory entries in a filesystem independent format.
4165  */
4166 #ifndef _SYS_SYSPROTO_H_
4167 struct ogetdirentries_args {
4168 	int	fd;
4169 	char	*buf;
4170 	u_int	count;
4171 	long	*basep;
4172 };
4173 #endif
4174 int
4175 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4176 {
4177 	long loff;
4178 	int error;
4179 
4180 	error = kern_ogetdirentries(td, uap, &loff);
4181 	if (error == 0)
4182 		error = copyout(&loff, uap->basep, sizeof(long));
4183 	return (error);
4184 }
4185 
4186 int
4187 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4188     long *ploff)
4189 {
4190 	long base;
4191 	int error;
4192 
4193 	/* XXX arbitrary sanity limit on `count'. */
4194 	if (uap->count > 64 * 1024)
4195 		return (EINVAL);
4196 
4197 	error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4198 	    &base, ogetdirentries_cvt);
4199 
4200 	if (error == 0 && uap->basep != NULL)
4201 		error = copyout(&base, uap->basep, sizeof(long));
4202 
4203 	return (error);
4204 }
4205 #endif /* COMPAT_43 */
4206 
4207 #if defined(COMPAT_FREEBSD11)
4208 #ifndef _SYS_SYSPROTO_H_
4209 struct freebsd11_getdirentries_args {
4210 	int	fd;
4211 	char	*buf;
4212 	u_int	count;
4213 	long	*basep;
4214 };
4215 #endif
4216 int
4217 freebsd11_getdirentries(struct thread *td,
4218     struct freebsd11_getdirentries_args *uap)
4219 {
4220 	long base;
4221 	int error;
4222 
4223 	error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4224 	    &base, NULL);
4225 
4226 	if (error == 0 && uap->basep != NULL)
4227 		error = copyout(&base, uap->basep, sizeof(long));
4228 	return (error);
4229 }
4230 
4231 int
4232 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4233 {
4234 	struct freebsd11_getdirentries_args ap;
4235 
4236 	ap.fd = uap->fd;
4237 	ap.buf = uap->buf;
4238 	ap.count = uap->count;
4239 	ap.basep = NULL;
4240 	return (freebsd11_getdirentries(td, &ap));
4241 }
4242 #endif /* COMPAT_FREEBSD11 */
4243 
4244 /*
4245  * Read a block of directory entries in a filesystem independent format.
4246  */
4247 int
4248 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4249 {
4250 	off_t base;
4251 	int error;
4252 
4253 	error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4254 	    NULL, UIO_USERSPACE);
4255 	if (error != 0)
4256 		return (error);
4257 	if (uap->basep != NULL)
4258 		error = copyout(&base, uap->basep, sizeof(off_t));
4259 	return (error);
4260 }
4261 
4262 int
4263 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4264     off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4265 {
4266 	struct vnode *vp;
4267 	struct file *fp;
4268 	struct uio auio;
4269 	struct iovec aiov;
4270 	off_t loff;
4271 	int error, eofflag;
4272 	off_t foffset;
4273 
4274 	AUDIT_ARG_FD(fd);
4275 	if (count > IOSIZE_MAX)
4276 		return (EINVAL);
4277 	auio.uio_resid = count;
4278 	error = getvnode(td, fd, &cap_read_rights, &fp);
4279 	if (error != 0)
4280 		return (error);
4281 	if ((fp->f_flag & FREAD) == 0) {
4282 		fdrop(fp, td);
4283 		return (EBADF);
4284 	}
4285 	vp = fp->f_vnode;
4286 	foffset = foffset_lock(fp, 0);
4287 unionread:
4288 	if (vp->v_type != VDIR) {
4289 		error = EINVAL;
4290 		goto fail;
4291 	}
4292 	if (__predict_false((vp->v_vflag & VV_UNLINKED) != 0)) {
4293 		error = ENOENT;
4294 		goto fail;
4295 	}
4296 	aiov.iov_base = buf;
4297 	aiov.iov_len = count;
4298 	auio.uio_iov = &aiov;
4299 	auio.uio_iovcnt = 1;
4300 	auio.uio_rw = UIO_READ;
4301 	auio.uio_segflg = bufseg;
4302 	auio.uio_td = td;
4303 	vn_lock(vp, LK_SHARED | LK_RETRY);
4304 	AUDIT_ARG_VNODE1(vp);
4305 	loff = auio.uio_offset = foffset;
4306 #ifdef MAC
4307 	error = mac_vnode_check_readdir(td->td_ucred, vp);
4308 	if (error == 0)
4309 #endif
4310 		error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4311 		    NULL);
4312 	foffset = auio.uio_offset;
4313 	if (error != 0) {
4314 		VOP_UNLOCK(vp);
4315 		goto fail;
4316 	}
4317 	if (count == auio.uio_resid &&
4318 	    (vp->v_vflag & VV_ROOT) &&
4319 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
4320 		struct vnode *tvp = vp;
4321 
4322 		vp = vp->v_mount->mnt_vnodecovered;
4323 		VREF(vp);
4324 		fp->f_vnode = vp;
4325 		foffset = 0;
4326 		vput(tvp);
4327 		goto unionread;
4328 	}
4329 	VOP_UNLOCK(vp);
4330 	*basep = loff;
4331 	if (residp != NULL)
4332 		*residp = auio.uio_resid;
4333 	td->td_retval[0] = count - auio.uio_resid;
4334 fail:
4335 	foffset_unlock(fp, foffset, 0);
4336 	fdrop(fp, td);
4337 	return (error);
4338 }
4339 
4340 /*
4341  * Set the mode mask for creation of filesystem nodes.
4342  */
4343 #ifndef _SYS_SYSPROTO_H_
4344 struct umask_args {
4345 	int	newmask;
4346 };
4347 #endif
4348 int
4349 sys_umask(struct thread *td, struct umask_args *uap)
4350 {
4351 	struct pwddesc *pdp;
4352 
4353 	pdp = td->td_proc->p_pd;
4354 	PWDDESC_XLOCK(pdp);
4355 	td->td_retval[0] = pdp->pd_cmask;
4356 	pdp->pd_cmask = uap->newmask & ALLPERMS;
4357 	PWDDESC_XUNLOCK(pdp);
4358 	return (0);
4359 }
4360 
4361 /*
4362  * Void all references to file by ripping underlying filesystem away from
4363  * vnode.
4364  */
4365 #ifndef _SYS_SYSPROTO_H_
4366 struct revoke_args {
4367 	char	*path;
4368 };
4369 #endif
4370 int
4371 sys_revoke(struct thread *td, struct revoke_args *uap)
4372 {
4373 	struct vnode *vp;
4374 	struct vattr vattr;
4375 	struct nameidata nd;
4376 	int error;
4377 
4378 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4379 	    uap->path);
4380 	if ((error = namei(&nd)) != 0)
4381 		return (error);
4382 	vp = nd.ni_vp;
4383 	NDFREE_PNBUF(&nd);
4384 	if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4385 		error = EINVAL;
4386 		goto out;
4387 	}
4388 #ifdef MAC
4389 	error = mac_vnode_check_revoke(td->td_ucred, vp);
4390 	if (error != 0)
4391 		goto out;
4392 #endif
4393 	error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4394 	if (error != 0)
4395 		goto out;
4396 	if (td->td_ucred->cr_uid != vattr.va_uid) {
4397 		error = priv_check(td, PRIV_VFS_ADMIN);
4398 		if (error != 0)
4399 			goto out;
4400 	}
4401 	if (devfs_usecount(vp) > 0)
4402 		VOP_REVOKE(vp, REVOKEALL);
4403 out:
4404 	vput(vp);
4405 	return (error);
4406 }
4407 
4408 /*
4409  * This variant of getvnode() allows O_PATH files.  Caller should
4410  * ensure that returned file and vnode are only used for compatible
4411  * semantics.
4412  */
4413 int
4414 getvnode_path(struct thread *td, int fd, cap_rights_t *rightsp,
4415     struct file **fpp)
4416 {
4417 	struct file *fp;
4418 	int error;
4419 
4420 	error = fget_unlocked(td, fd, rightsp, &fp);
4421 	if (error != 0)
4422 		return (error);
4423 
4424 	/*
4425 	 * The file could be not of the vnode type, or it may be not
4426 	 * yet fully initialized, in which case the f_vnode pointer
4427 	 * may be set, but f_ops is still badfileops.  E.g.,
4428 	 * devfs_open() transiently create such situation to
4429 	 * facilitate csw d_fdopen().
4430 	 *
4431 	 * Dupfdopen() handling in kern_openat() installs the
4432 	 * half-baked file into the process descriptor table, allowing
4433 	 * other thread to dereference it. Guard against the race by
4434 	 * checking f_ops.
4435 	 */
4436 	if (__predict_false(fp->f_vnode == NULL || fp->f_ops == &badfileops)) {
4437 		fdrop(fp, td);
4438 		*fpp = NULL;
4439 		return (EINVAL);
4440 	}
4441 
4442 	*fpp = fp;
4443 	return (0);
4444 }
4445 
4446 /*
4447  * Convert a user file descriptor to a kernel file entry and check
4448  * that, if it is a capability, the correct rights are present.
4449  * A reference on the file entry is held upon returning.
4450  */
4451 int
4452 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4453 {
4454 	int error;
4455 
4456 	error = getvnode_path(td, fd, rightsp, fpp);
4457 	if (__predict_false(error != 0))
4458 		return (error);
4459 
4460 	/*
4461 	 * Filter out O_PATH file descriptors, most getvnode() callers
4462 	 * do not call fo_ methods.
4463 	 */
4464 	if (__predict_false((*fpp)->f_ops == &path_fileops)) {
4465 		fdrop(*fpp, td);
4466 		*fpp = NULL;
4467 		error = EBADF;
4468 	}
4469 
4470 	return (error);
4471 }
4472 
4473 /*
4474  * Get an (NFS) file handle.
4475  */
4476 #ifndef _SYS_SYSPROTO_H_
4477 struct lgetfh_args {
4478 	char *fname;
4479 	fhandle_t *fhp;
4480 };
4481 #endif
4482 int
4483 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4484 {
4485 
4486 	return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4487 	    UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4488 }
4489 
4490 #ifndef _SYS_SYSPROTO_H_
4491 struct getfh_args {
4492 	char *fname;
4493 	fhandle_t *fhp;
4494 };
4495 #endif
4496 int
4497 sys_getfh(struct thread *td, struct getfh_args *uap)
4498 {
4499 
4500 	return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4501 	    uap->fhp, UIO_USERSPACE));
4502 }
4503 
4504 /*
4505  * syscall for the rpc.lockd to use to translate an open descriptor into
4506  * a NFS file handle.
4507  *
4508  * warning: do not remove the priv_check() call or this becomes one giant
4509  * security hole.
4510  */
4511 #ifndef _SYS_SYSPROTO_H_
4512 struct getfhat_args {
4513 	int fd;
4514 	char *path;
4515 	fhandle_t *fhp;
4516 	int flags;
4517 };
4518 #endif
4519 int
4520 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4521 {
4522 
4523 	return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4524 	    uap->fhp, UIO_USERSPACE));
4525 }
4526 
4527 int
4528 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4529     enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4530 {
4531 	struct nameidata nd;
4532 	fhandle_t fh;
4533 	struct vnode *vp;
4534 	int error;
4535 
4536 	if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
4537 		return (EINVAL);
4538 	error = priv_check(td, PRIV_VFS_GETFH);
4539 	if (error != 0)
4540 		return (error);
4541 	NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4542 	    AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path,
4543 	    fd);
4544 	error = namei(&nd);
4545 	if (error != 0)
4546 		return (error);
4547 	NDFREE_PNBUF(&nd);
4548 	vp = nd.ni_vp;
4549 	bzero(&fh, sizeof(fh));
4550 	fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4551 	error = VOP_VPTOFH(vp, &fh.fh_fid);
4552 	vput(vp);
4553 	if (error == 0) {
4554 		if (fhseg == UIO_USERSPACE)
4555 			error = copyout(&fh, fhp, sizeof (fh));
4556 		else
4557 			memcpy(fhp, &fh, sizeof(fh));
4558 	}
4559 	return (error);
4560 }
4561 
4562 #ifndef _SYS_SYSPROTO_H_
4563 struct fhlink_args {
4564 	fhandle_t *fhp;
4565 	const char *to;
4566 };
4567 #endif
4568 int
4569 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4570 {
4571 
4572 	return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4573 }
4574 
4575 #ifndef _SYS_SYSPROTO_H_
4576 struct fhlinkat_args {
4577 	fhandle_t *fhp;
4578 	int tofd;
4579 	const char *to;
4580 };
4581 #endif
4582 int
4583 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4584 {
4585 
4586 	return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4587 }
4588 
4589 static int
4590 kern_fhlinkat(struct thread *td, int fd, const char *path,
4591     enum uio_seg pathseg, fhandle_t *fhp)
4592 {
4593 	fhandle_t fh;
4594 	struct mount *mp;
4595 	struct vnode *vp;
4596 	int error;
4597 
4598 	error = priv_check(td, PRIV_VFS_GETFH);
4599 	if (error != 0)
4600 		return (error);
4601 	error = copyin(fhp, &fh, sizeof(fh));
4602 	if (error != 0)
4603 		return (error);
4604 	do {
4605 		bwillwrite();
4606 		if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4607 			return (ESTALE);
4608 		error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4609 		vfs_unbusy(mp);
4610 		if (error != 0)
4611 			return (error);
4612 		VOP_UNLOCK(vp);
4613 		error = kern_linkat_vp(td, vp, fd, path, pathseg);
4614 	} while (error == EAGAIN || error == ERELOOKUP);
4615 	return (error);
4616 }
4617 
4618 #ifndef _SYS_SYSPROTO_H_
4619 struct fhreadlink_args {
4620 	fhandle_t *fhp;
4621 	char *buf;
4622 	size_t bufsize;
4623 };
4624 #endif
4625 int
4626 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4627 {
4628 	fhandle_t fh;
4629 	struct mount *mp;
4630 	struct vnode *vp;
4631 	int error;
4632 
4633 	error = priv_check(td, PRIV_VFS_GETFH);
4634 	if (error != 0)
4635 		return (error);
4636 	if (uap->bufsize > IOSIZE_MAX)
4637 		return (EINVAL);
4638 	error = copyin(uap->fhp, &fh, sizeof(fh));
4639 	if (error != 0)
4640 		return (error);
4641 	if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4642 		return (ESTALE);
4643 	error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4644 	vfs_unbusy(mp);
4645 	if (error != 0)
4646 		return (error);
4647 	error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4648 	vput(vp);
4649 	return (error);
4650 }
4651 
4652 /*
4653  * syscall for the rpc.lockd to use to translate a NFS file handle into an
4654  * open descriptor.
4655  *
4656  * warning: do not remove the priv_check() call or this becomes one giant
4657  * security hole.
4658  */
4659 #ifndef _SYS_SYSPROTO_H_
4660 struct fhopen_args {
4661 	const struct fhandle *u_fhp;
4662 	int flags;
4663 };
4664 #endif
4665 int
4666 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4667 {
4668 	return (kern_fhopen(td, uap->u_fhp, uap->flags));
4669 }
4670 
4671 int
4672 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4673 {
4674 	struct mount *mp;
4675 	struct vnode *vp;
4676 	struct fhandle fhp;
4677 	struct file *fp;
4678 	int error, indx;
4679 	bool named_attr;
4680 
4681 	error = priv_check(td, PRIV_VFS_FHOPEN);
4682 	if (error != 0)
4683 		return (error);
4684 
4685 	indx = -1;
4686 	if ((flags & O_CREAT) != 0)
4687 		return (EINVAL);
4688 	error = openflags(&flags);
4689 	if (error != 0)
4690 		return (error);
4691 	error = copyin(u_fhp, &fhp, sizeof(fhp));
4692 	if (error != 0)
4693 		return (error);
4694 	/* find the mount point */
4695 	mp = vfs_busyfs(&fhp.fh_fsid);
4696 	if (mp == NULL)
4697 		return (ESTALE);
4698 	/* now give me my vnode, it gets returned to me locked */
4699 	error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4700 	vfs_unbusy(mp);
4701 	if (error != 0)
4702 		return (error);
4703 
4704 	/*
4705 	 * Check to see if the file handle refers to a named attribute
4706 	 * directory or attribute.  If it does, the O_NAMEDATTR flag
4707 	 * must have been specified.
4708 	 */
4709 	named_attr = (vn_irflag_read(vp) &
4710 	    (VIRF_NAMEDDIR | VIRF_NAMEDATTR)) != 0;
4711 	if ((named_attr && (flags & O_NAMEDATTR) == 0) ||
4712 	    (!named_attr && (flags & O_NAMEDATTR) != 0)) {
4713 		vput(vp);
4714 		return (ENOATTR);
4715 	}
4716 
4717 	error = falloc_noinstall(td, &fp);
4718 	if (error != 0) {
4719 		vput(vp);
4720 		return (error);
4721 	}
4722 	/* Set the flags early so the finit in devfs can pick them up. */
4723 	fp->f_flag = flags & FMASK;
4724 
4725 #ifdef INVARIANTS
4726 	td->td_dupfd = -1;
4727 #endif
4728 	error = vn_open_vnode(vp, flags, td->td_ucred, td, fp);
4729 	if (error != 0) {
4730 		KASSERT(fp->f_ops == &badfileops,
4731 		    ("VOP_OPEN in fhopen() set f_ops"));
4732 		KASSERT(td->td_dupfd < 0,
4733 		    ("fhopen() encountered fdopen()"));
4734 
4735 		vput(vp);
4736 		goto bad;
4737 	}
4738 #ifdef INVARIANTS
4739 	td->td_dupfd = 0;
4740 #endif
4741 	finit_open(fp, vp, flags);
4742 	VOP_UNLOCK(vp);
4743 	if ((flags & O_TRUNC) != 0) {
4744 		error = fo_truncate(fp, 0, td->td_ucred, td);
4745 		if (error != 0)
4746 			goto bad;
4747 	}
4748 
4749 	error = finstall(td, fp, &indx, flags, NULL);
4750 bad:
4751 	fdrop(fp, td);
4752 	td->td_retval[0] = indx;
4753 	return (error);
4754 }
4755 
4756 /*
4757  * Stat an (NFS) file handle.
4758  */
4759 #ifndef _SYS_SYSPROTO_H_
4760 struct fhstat_args {
4761 	struct fhandle *u_fhp;
4762 	struct stat *sb;
4763 };
4764 #endif
4765 int
4766 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4767 {
4768 	struct stat sb;
4769 	struct fhandle fh;
4770 	int error;
4771 
4772 	error = copyin(uap->u_fhp, &fh, sizeof(fh));
4773 	if (error != 0)
4774 		return (error);
4775 	error = kern_fhstat(td, fh, &sb);
4776 	if (error == 0)
4777 		error = copyout(&sb, uap->sb, sizeof(sb));
4778 	return (error);
4779 }
4780 
4781 int
4782 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4783 {
4784 	struct mount *mp;
4785 	struct vnode *vp;
4786 	int error;
4787 
4788 	error = priv_check(td, PRIV_VFS_FHSTAT);
4789 	if (error != 0)
4790 		return (error);
4791 	if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4792 		return (ESTALE);
4793 	error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4794 	vfs_unbusy(mp);
4795 	if (error != 0)
4796 		return (error);
4797 	error = VOP_STAT(vp, sb, td->td_ucred, NOCRED);
4798 	vput(vp);
4799 	return (error);
4800 }
4801 
4802 /*
4803  * Implement fstatfs() for (NFS) file handles.
4804  */
4805 #ifndef _SYS_SYSPROTO_H_
4806 struct fhstatfs_args {
4807 	struct fhandle *u_fhp;
4808 	struct statfs *buf;
4809 };
4810 #endif
4811 int
4812 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4813 {
4814 	struct statfs *sfp;
4815 	fhandle_t fh;
4816 	int error;
4817 
4818 	error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4819 	if (error != 0)
4820 		return (error);
4821 	sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4822 	error = kern_fhstatfs(td, fh, sfp);
4823 	if (error == 0)
4824 		error = copyout(sfp, uap->buf, sizeof(*sfp));
4825 	free(sfp, M_STATFS);
4826 	return (error);
4827 }
4828 
4829 int
4830 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4831 {
4832 	struct mount *mp;
4833 	struct vnode *vp;
4834 	int error;
4835 
4836 	error = priv_check(td, PRIV_VFS_FHSTATFS);
4837 	if (error != 0)
4838 		return (error);
4839 	if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4840 		return (ESTALE);
4841 	error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4842 	if (error != 0) {
4843 		vfs_unbusy(mp);
4844 		return (error);
4845 	}
4846 	vput(vp);
4847 	error = prison_canseemount(td->td_ucred, mp);
4848 	if (error != 0)
4849 		goto out;
4850 #ifdef MAC
4851 	error = mac_mount_check_stat(td->td_ucred, mp);
4852 	if (error != 0)
4853 		goto out;
4854 #endif
4855 	error = VFS_STATFS(mp, buf);
4856 out:
4857 	vfs_unbusy(mp);
4858 	return (error);
4859 }
4860 
4861 /*
4862  * Unlike madvise(2), we do not make a best effort to remember every
4863  * possible caching hint.  Instead, we remember the last setting with
4864  * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4865  * region of any current setting.
4866  */
4867 int
4868 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4869     int advice)
4870 {
4871 	struct fadvise_info *fa, *new;
4872 	struct file *fp;
4873 	struct vnode *vp;
4874 	off_t end;
4875 	int error;
4876 
4877 	if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4878 		return (EINVAL);
4879 	AUDIT_ARG_VALUE(advice);
4880 	switch (advice) {
4881 	case POSIX_FADV_SEQUENTIAL:
4882 	case POSIX_FADV_RANDOM:
4883 	case POSIX_FADV_NOREUSE:
4884 		new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4885 		break;
4886 	case POSIX_FADV_NORMAL:
4887 	case POSIX_FADV_WILLNEED:
4888 	case POSIX_FADV_DONTNEED:
4889 		new = NULL;
4890 		break;
4891 	default:
4892 		return (EINVAL);
4893 	}
4894 	/* XXX: CAP_POSIX_FADVISE? */
4895 	AUDIT_ARG_FD(fd);
4896 	error = fget(td, fd, &cap_no_rights, &fp);
4897 	if (error != 0)
4898 		goto out;
4899 	AUDIT_ARG_FILE(td->td_proc, fp);
4900 	if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4901 		error = ESPIPE;
4902 		goto out;
4903 	}
4904 	if (fp->f_type != DTYPE_VNODE) {
4905 		error = ENODEV;
4906 		goto out;
4907 	}
4908 	vp = fp->f_vnode;
4909 	if (vp->v_type != VREG) {
4910 		error = ENODEV;
4911 		goto out;
4912 	}
4913 	if (len == 0)
4914 		end = OFF_MAX;
4915 	else
4916 		end = offset + len - 1;
4917 	switch (advice) {
4918 	case POSIX_FADV_SEQUENTIAL:
4919 	case POSIX_FADV_RANDOM:
4920 	case POSIX_FADV_NOREUSE:
4921 		/*
4922 		 * Try to merge any existing non-standard region with
4923 		 * this new region if possible, otherwise create a new
4924 		 * non-standard region for this request.
4925 		 */
4926 		mtx_pool_lock(mtxpool_sleep, fp);
4927 		fa = fp->f_advice;
4928 		if (fa != NULL && fa->fa_advice == advice &&
4929 		    ((fa->fa_start <= end && fa->fa_end >= offset) ||
4930 		    (end != OFF_MAX && fa->fa_start == end + 1) ||
4931 		    (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4932 			if (offset < fa->fa_start)
4933 				fa->fa_start = offset;
4934 			if (end > fa->fa_end)
4935 				fa->fa_end = end;
4936 		} else {
4937 			new->fa_advice = advice;
4938 			new->fa_start = offset;
4939 			new->fa_end = end;
4940 			fp->f_advice = new;
4941 			new = fa;
4942 		}
4943 		mtx_pool_unlock(mtxpool_sleep, fp);
4944 		break;
4945 	case POSIX_FADV_NORMAL:
4946 		/*
4947 		 * If a the "normal" region overlaps with an existing
4948 		 * non-standard region, trim or remove the
4949 		 * non-standard region.
4950 		 */
4951 		mtx_pool_lock(mtxpool_sleep, fp);
4952 		fa = fp->f_advice;
4953 		if (fa != NULL) {
4954 			if (offset <= fa->fa_start && end >= fa->fa_end) {
4955 				new = fa;
4956 				fp->f_advice = NULL;
4957 			} else if (offset <= fa->fa_start &&
4958 			    end >= fa->fa_start)
4959 				fa->fa_start = end + 1;
4960 			else if (offset <= fa->fa_end && end >= fa->fa_end)
4961 				fa->fa_end = offset - 1;
4962 			else if (offset >= fa->fa_start && end <= fa->fa_end) {
4963 				/*
4964 				 * If the "normal" region is a middle
4965 				 * portion of the existing
4966 				 * non-standard region, just remove
4967 				 * the whole thing rather than picking
4968 				 * one side or the other to
4969 				 * preserve.
4970 				 */
4971 				new = fa;
4972 				fp->f_advice = NULL;
4973 			}
4974 		}
4975 		mtx_pool_unlock(mtxpool_sleep, fp);
4976 		break;
4977 	case POSIX_FADV_WILLNEED:
4978 	case POSIX_FADV_DONTNEED:
4979 		error = VOP_ADVISE(vp, offset, end, advice);
4980 		break;
4981 	}
4982 out:
4983 	if (fp != NULL)
4984 		fdrop(fp, td);
4985 	free(new, M_FADVISE);
4986 	return (error);
4987 }
4988 
4989 int
4990 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4991 {
4992 	int error;
4993 
4994 	error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4995 	    uap->advice);
4996 	return (kern_posix_error(td, error));
4997 }
4998 
4999 int
5000 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
5001     off_t *outoffp, size_t len, unsigned int flags)
5002 {
5003 	struct file *infp, *infp1, *outfp, *outfp1;
5004 	struct vnode *invp, *outvp;
5005 	int error;
5006 	size_t retlen;
5007 	void *rl_rcookie, *rl_wcookie;
5008 	off_t inoff, outoff, savinoff, savoutoff;
5009 	bool foffsets_locked;
5010 
5011 	infp = outfp = NULL;
5012 	rl_rcookie = rl_wcookie = NULL;
5013 	foffsets_locked = false;
5014 	error = 0;
5015 	retlen = 0;
5016 
5017 	if (flags != 0) {
5018 		error = EINVAL;
5019 		goto out;
5020 	}
5021 	if (len > SSIZE_MAX)
5022 		/*
5023 		 * Although the len argument is size_t, the return argument
5024 		 * is ssize_t (which is signed).  Therefore a size that won't
5025 		 * fit in ssize_t can't be returned.
5026 		 */
5027 		len = SSIZE_MAX;
5028 
5029 	/* Get the file structures for the file descriptors. */
5030 	error = fget_read(td, infd,
5031 	    inoffp != NULL ? &cap_pread_rights : &cap_read_rights, &infp);
5032 	if (error != 0)
5033 		goto out;
5034 	if (infp->f_ops == &badfileops) {
5035 		error = EBADF;
5036 		goto out;
5037 	}
5038 	if (infp->f_vnode == NULL) {
5039 		error = EINVAL;
5040 		goto out;
5041 	}
5042 	error = fget_write(td, outfd,
5043 	    outoffp != NULL ? &cap_pwrite_rights : &cap_write_rights, &outfp);
5044 	if (error != 0)
5045 		goto out;
5046 	if (outfp->f_ops == &badfileops) {
5047 		error = EBADF;
5048 		goto out;
5049 	}
5050 	if (outfp->f_vnode == NULL) {
5051 		error = EINVAL;
5052 		goto out;
5053 	}
5054 
5055 	/*
5056 	 * Figure out which file offsets we're reading from and writing to.
5057 	 * If the offsets come from the file descriptions, we need to lock them,
5058 	 * and locking both offsets requires a loop to avoid deadlocks.
5059 	 */
5060 	infp1 = outfp1 = NULL;
5061 	if (inoffp != NULL)
5062 		inoff = *inoffp;
5063 	else
5064 		infp1 = infp;
5065 	if (outoffp != NULL)
5066 		outoff = *outoffp;
5067 	else
5068 		outfp1 = outfp;
5069 	if (infp1 != NULL || outfp1 != NULL) {
5070 		if (infp1 == outfp1) {
5071 			/*
5072 			 * Overlapping ranges are not allowed.  A more thorough
5073 			 * check appears below, but we must not lock the same
5074 			 * offset twice.
5075 			 */
5076 			error = EINVAL;
5077 			goto out;
5078 		}
5079 		foffset_lock_pair(infp1, &inoff, outfp1, &outoff, 0);
5080 		foffsets_locked = true;
5081 	}
5082 	savinoff = inoff;
5083 	savoutoff = outoff;
5084 
5085 	invp = infp->f_vnode;
5086 	outvp = outfp->f_vnode;
5087 	/* Sanity check the f_flag bits. */
5088 	if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
5089 	    (infp->f_flag & FREAD) == 0) {
5090 		error = EBADF;
5091 		goto out;
5092 	}
5093 
5094 	/* If len == 0, just return 0. */
5095 	if (len == 0)
5096 		goto out;
5097 
5098 	/*
5099 	 * Make sure that the ranges we check and lock below are valid.  Note
5100 	 * that len is clamped to SSIZE_MAX above.
5101 	 */
5102 	if (inoff < 0 || outoff < 0) {
5103 		error = EINVAL;
5104 		goto out;
5105 	}
5106 
5107 	/*
5108 	 * If infp and outfp refer to the same file, the byte ranges cannot
5109 	 * overlap.
5110 	 */
5111 	if (invp == outvp) {
5112 		if ((inoff <= outoff && inoff + len > outoff) ||
5113 		    (inoff > outoff && outoff + len > inoff)) {
5114 			error = EINVAL;
5115 			goto out;
5116 		}
5117 		rangelock_may_recurse(&invp->v_rl);
5118 	}
5119 
5120 	/* Range lock the byte ranges for both invp and outvp. */
5121 	for (;;) {
5122 		rl_wcookie = vn_rangelock_wlock(outvp, outoff, outoff + len);
5123 		rl_rcookie = vn_rangelock_tryrlock(invp, inoff, inoff + len);
5124 		if (rl_rcookie != NULL)
5125 			break;
5126 		vn_rangelock_unlock(outvp, rl_wcookie);
5127 		rl_rcookie = vn_rangelock_rlock(invp, inoff, inoff + len);
5128 		vn_rangelock_unlock(invp, rl_rcookie);
5129 	}
5130 
5131 	retlen = len;
5132 	error = vn_copy_file_range(invp, &inoff, outvp, &outoff, &retlen,
5133 	    flags, infp->f_cred, outfp->f_cred, td);
5134 out:
5135 	if (rl_rcookie != NULL)
5136 		vn_rangelock_unlock(invp, rl_rcookie);
5137 	if (rl_wcookie != NULL)
5138 		vn_rangelock_unlock(outvp, rl_wcookie);
5139 	if (foffsets_locked) {
5140 		if (error == EINTR || error == ERESTART) {
5141 			inoff = savinoff;
5142 			outoff = savoutoff;
5143 		}
5144 		if (inoffp == NULL)
5145 			foffset_unlock(infp, inoff, 0);
5146 		else
5147 			*inoffp = inoff;
5148 		if (outoffp == NULL)
5149 			foffset_unlock(outfp, outoff, 0);
5150 		else
5151 			*outoffp = outoff;
5152 	}
5153 	if (outfp != NULL)
5154 		fdrop(outfp, td);
5155 	if (infp != NULL)
5156 		fdrop(infp, td);
5157 	td->td_retval[0] = retlen;
5158 	return (error);
5159 }
5160 
5161 int
5162 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
5163 {
5164 	off_t inoff, outoff, *inoffp, *outoffp;
5165 	int error;
5166 
5167 	inoffp = outoffp = NULL;
5168 	if (uap->inoffp != NULL) {
5169 		error = copyin(uap->inoffp, &inoff, sizeof(off_t));
5170 		if (error != 0)
5171 			return (error);
5172 		inoffp = &inoff;
5173 	}
5174 	if (uap->outoffp != NULL) {
5175 		error = copyin(uap->outoffp, &outoff, sizeof(off_t));
5176 		if (error != 0)
5177 			return (error);
5178 		outoffp = &outoff;
5179 	}
5180 	error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
5181 	    outoffp, uap->len, uap->flags);
5182 	if (error == 0 && uap->inoffp != NULL)
5183 		error = copyout(inoffp, uap->inoffp, sizeof(off_t));
5184 	if (error == 0 && uap->outoffp != NULL)
5185 		error = copyout(outoffp, uap->outoffp, sizeof(off_t));
5186 	return (error);
5187 }
5188