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