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