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