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