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