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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_capsicum.h"
41 #include "opt_compat.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
48 #include <sys/capsicum.h>
50 #include <sys/sysent.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/sysproto.h>
55 #include <sys/namei.h>
56 #include <sys/filedesc.h>
57 #include <sys/kernel.h>
58 #include <sys/fcntl.h>
60 #include <sys/filio.h>
61 #include <sys/limits.h>
62 #include <sys/linker.h>
63 #include <sys/rwlock.h>
67 #include <sys/unistd.h>
68 #include <sys/vnode.h>
71 #include <sys/dirent.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysctl.h>
76 #include <sys/ktrace.h>
79 #include <machine/stdarg.h>
81 #include <security/audit/audit.h>
82 #include <security/mac/mac_framework.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
89 #include <ufs/ufs/quota.h>
91 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
93 SDT_PROVIDER_DEFINE(vfs);
94 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
95 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
97 static int kern_chflagsat(struct thread *td, int fd, const char *path,
98 enum uio_seg pathseg, u_long flags, int atflag);
99 static int setfflags(struct thread *td, struct vnode *, u_long);
100 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
101 static int getutimens(const struct timespec *, enum uio_seg,
102 struct timespec *, int *);
103 static int setutimes(struct thread *td, struct vnode *,
104 const struct timespec *, int, int);
105 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
109 * Sync each mounted filesystem.
111 #ifndef _SYS_SYSPROTO_H_
120 struct sync_args *uap;
122 struct mount *mp, *nmp;
125 mtx_lock(&mountlist_mtx);
126 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
127 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
128 nmp = TAILQ_NEXT(mp, mnt_list);
131 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
132 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
133 save = curthread_pflags_set(TDP_SYNCIO);
134 vfs_msync(mp, MNT_NOWAIT);
135 VFS_SYNC(mp, MNT_NOWAIT);
136 curthread_pflags_restore(save);
137 vn_finished_write(mp);
139 mtx_lock(&mountlist_mtx);
140 nmp = TAILQ_NEXT(mp, mnt_list);
143 mtx_unlock(&mountlist_mtx);
148 * Change filesystem quotas.
150 #ifndef _SYS_SYSPROTO_H_
151 struct quotactl_args {
159 sys_quotactl(td, uap)
161 register struct quotactl_args /* {
172 AUDIT_ARG_CMD(uap->cmd);
173 AUDIT_ARG_UID(uap->uid);
174 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
176 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
178 if ((error = namei(&nd)) != 0)
180 NDFREE(&nd, NDF_ONLY_PNBUF);
181 mp = nd.ni_vp->v_mount;
184 error = vfs_busy(mp, 0);
188 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
191 * Since quota on operation typically needs to open quota
192 * file, the Q_QUOTAON handler needs to unbusy the mount point
193 * before calling into namei. Otherwise, unmount might be
194 * started between two vfs_busy() invocations (first is our,
195 * second is from mount point cross-walk code in lookup()),
198 * Require that Q_QUOTAON handles the vfs_busy() reference on
199 * its own, always returning with ubusied mount point.
201 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
207 * Used by statfs conversion routines to scale the block size up if
208 * necessary so that all of the block counts are <= 'max_size'. Note
209 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
213 statfs_scale_blocks(struct statfs *sf, long max_size)
218 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
221 * Attempt to scale the block counts to give a more accurate
222 * overview to userland of the ratio of free space to used
223 * space. To do this, find the largest block count and compute
224 * a divisor that lets it fit into a signed integer <= max_size.
226 if (sf->f_bavail < 0)
227 count = -sf->f_bavail;
229 count = sf->f_bavail;
230 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
231 if (count <= max_size)
234 count >>= flsl(max_size);
241 sf->f_bsize <<= shift;
242 sf->f_blocks >>= shift;
243 sf->f_bfree >>= shift;
244 sf->f_bavail >>= shift;
248 * Get filesystem statistics.
250 #ifndef _SYS_SYSPROTO_H_
259 register struct statfs_args /* {
267 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
269 error = copyout(&sf, uap->buf, sizeof(sf));
274 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
278 struct statfs *sp, sb;
282 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
287 mp = nd.ni_vp->v_mount;
289 NDFREE(&nd, NDF_ONLY_PNBUF);
291 error = vfs_busy(mp, 0);
296 error = mac_mount_check_stat(td->td_ucred, mp);
301 * Set these in case the underlying filesystem fails to do so.
304 sp->f_version = STATFS_VERSION;
305 sp->f_namemax = NAME_MAX;
306 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
307 error = VFS_STATFS(mp, sp);
310 if (priv_check(td, PRIV_VFS_GENERATION)) {
311 bcopy(sp, &sb, sizeof(sb));
312 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
313 prison_enforce_statfs(td->td_ucred, mp, &sb);
323 * Get filesystem statistics.
325 #ifndef _SYS_SYSPROTO_H_
326 struct fstatfs_args {
334 register struct fstatfs_args /* {
342 error = kern_fstatfs(td, uap->fd, &sf);
344 error = copyout(&sf, uap->buf, sizeof(sf));
349 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
353 struct statfs *sp, sb;
359 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSTATFS), &fp);
363 vn_lock(vp, LK_SHARED | LK_RETRY);
365 AUDIT_ARG_VNODE1(vp);
376 error = vfs_busy(mp, 0);
381 error = mac_mount_check_stat(td->td_ucred, mp);
386 * Set these in case the underlying filesystem fails to do so.
389 sp->f_version = STATFS_VERSION;
390 sp->f_namemax = NAME_MAX;
391 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
392 error = VFS_STATFS(mp, sp);
395 if (priv_check(td, PRIV_VFS_GENERATION)) {
396 bcopy(sp, &sb, sizeof(sb));
397 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
398 prison_enforce_statfs(td->td_ucred, mp, &sb);
409 * Get statistics on all filesystems.
411 #ifndef _SYS_SYSPROTO_H_
412 struct getfsstat_args {
419 sys_getfsstat(td, uap)
421 register struct getfsstat_args /* {
430 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
432 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
433 UIO_USERSPACE, uap->flags);
435 td->td_retval[0] = count;
440 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
441 * The caller is responsible for freeing memory which will be allocated
445 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
446 size_t *countp, enum uio_seg bufseg, int flags)
448 struct mount *mp, *nmp;
449 struct statfs *sfsp, *sp, sb;
450 size_t count, maxcount;
453 maxcount = bufsize / sizeof(struct statfs);
456 else if (bufseg == UIO_USERSPACE)
458 else /* if (bufseg == UIO_SYSSPACE) */ {
460 mtx_lock(&mountlist_mtx);
461 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
464 mtx_unlock(&mountlist_mtx);
465 if (maxcount > count)
467 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
471 mtx_lock(&mountlist_mtx);
472 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
473 if (prison_canseemount(td->td_ucred, mp) != 0) {
474 nmp = TAILQ_NEXT(mp, mnt_list);
478 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
479 nmp = TAILQ_NEXT(mp, mnt_list);
483 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
484 nmp = TAILQ_NEXT(mp, mnt_list);
487 if (sfsp && count < maxcount) {
490 * Set these in case the underlying filesystem
493 sp->f_version = STATFS_VERSION;
494 sp->f_namemax = NAME_MAX;
495 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
497 * If MNT_NOWAIT or MNT_LAZY is specified, do not
498 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
499 * overrides MNT_WAIT.
501 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
502 (flags & MNT_WAIT)) &&
503 (error = VFS_STATFS(mp, sp))) {
504 mtx_lock(&mountlist_mtx);
505 nmp = TAILQ_NEXT(mp, mnt_list);
509 if (priv_check(td, PRIV_VFS_GENERATION)) {
510 bcopy(sp, &sb, sizeof(sb));
511 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
512 prison_enforce_statfs(td->td_ucred, mp, &sb);
515 if (bufseg == UIO_SYSSPACE)
516 bcopy(sp, sfsp, sizeof(*sp));
517 else /* if (bufseg == UIO_USERSPACE) */ {
518 error = copyout(sp, sfsp, sizeof(*sp));
527 mtx_lock(&mountlist_mtx);
528 nmp = TAILQ_NEXT(mp, mnt_list);
531 mtx_unlock(&mountlist_mtx);
532 if (sfsp && count > maxcount)
539 #ifdef COMPAT_FREEBSD4
541 * Get old format filesystem statistics.
543 static void cvtstatfs(struct statfs *, struct ostatfs *);
545 #ifndef _SYS_SYSPROTO_H_
546 struct freebsd4_statfs_args {
552 freebsd4_statfs(td, uap)
554 struct freebsd4_statfs_args /* {
563 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
566 cvtstatfs(&sf, &osb);
567 return (copyout(&osb, uap->buf, sizeof(osb)));
571 * Get filesystem statistics.
573 #ifndef _SYS_SYSPROTO_H_
574 struct freebsd4_fstatfs_args {
580 freebsd4_fstatfs(td, uap)
582 struct freebsd4_fstatfs_args /* {
591 error = kern_fstatfs(td, uap->fd, &sf);
594 cvtstatfs(&sf, &osb);
595 return (copyout(&osb, uap->buf, sizeof(osb)));
599 * Get statistics on all filesystems.
601 #ifndef _SYS_SYSPROTO_H_
602 struct freebsd4_getfsstat_args {
609 freebsd4_getfsstat(td, uap)
611 register struct freebsd4_getfsstat_args /* {
617 struct statfs *buf, *sp;
622 if (uap->bufsize < 0)
624 count = uap->bufsize / sizeof(struct ostatfs);
625 if (count > SIZE_MAX / sizeof(struct statfs))
627 size = count * sizeof(struct statfs);
628 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
630 td->td_retval[0] = count;
633 while (count != 0 && error == 0) {
635 error = copyout(&osb, uap->buf, sizeof(osb));
646 * Implement fstatfs() for (NFS) file handles.
648 #ifndef _SYS_SYSPROTO_H_
649 struct freebsd4_fhstatfs_args {
650 struct fhandle *u_fhp;
655 freebsd4_fhstatfs(td, uap)
657 struct freebsd4_fhstatfs_args /* {
658 struct fhandle *u_fhp;
667 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
670 error = kern_fhstatfs(td, fh, &sf);
673 cvtstatfs(&sf, &osb);
674 return (copyout(&osb, uap->buf, sizeof(osb)));
678 * Convert a new format statfs structure to an old format statfs structure.
686 statfs_scale_blocks(nsp, LONG_MAX);
687 bzero(osp, sizeof(*osp));
688 osp->f_bsize = nsp->f_bsize;
689 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
690 osp->f_blocks = nsp->f_blocks;
691 osp->f_bfree = nsp->f_bfree;
692 osp->f_bavail = nsp->f_bavail;
693 osp->f_files = MIN(nsp->f_files, LONG_MAX);
694 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
695 osp->f_owner = nsp->f_owner;
696 osp->f_type = nsp->f_type;
697 osp->f_flags = nsp->f_flags;
698 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
699 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
700 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
701 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
702 strlcpy(osp->f_fstypename, nsp->f_fstypename,
703 MIN(MFSNAMELEN, OMFSNAMELEN));
704 strlcpy(osp->f_mntonname, nsp->f_mntonname,
705 MIN(MNAMELEN, OMNAMELEN));
706 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
707 MIN(MNAMELEN, OMNAMELEN));
708 osp->f_fsid = nsp->f_fsid;
710 #endif /* COMPAT_FREEBSD4 */
713 * Change current working directory to a given file descriptor.
715 #ifndef _SYS_SYSPROTO_H_
723 struct fchdir_args /* {
727 struct vnode *vp, *tdp;
733 AUDIT_ARG_FD(uap->fd);
734 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
741 vn_lock(vp, LK_SHARED | LK_RETRY);
742 AUDIT_ARG_VNODE1(vp);
743 error = change_dir(vp, td);
744 while (!error && (mp = vp->v_mountedhere) != NULL) {
747 error = VFS_ROOT(mp, LK_SHARED, &tdp);
764 * Change current working directory (``.'').
766 #ifndef _SYS_SYSPROTO_H_
774 struct chdir_args /* {
779 return (kern_chdir(td, uap->path, UIO_USERSPACE));
783 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
788 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
790 if ((error = namei(&nd)) != 0)
792 if ((error = change_dir(nd.ni_vp, td)) != 0) {
794 NDFREE(&nd, NDF_ONLY_PNBUF);
797 VOP_UNLOCK(nd.ni_vp, 0);
798 NDFREE(&nd, NDF_ONLY_PNBUF);
799 pwd_chdir(td, nd.ni_vp);
804 * Change notion of root (``/'') directory.
806 #ifndef _SYS_SYSPROTO_H_
814 struct chroot_args /* {
821 error = priv_check(td, PRIV_VFS_CHROOT);
824 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
825 UIO_USERSPACE, uap->path, td);
829 error = change_dir(nd.ni_vp, td);
833 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
837 VOP_UNLOCK(nd.ni_vp, 0);
838 error = pwd_chroot(td, nd.ni_vp);
840 NDFREE(&nd, NDF_ONLY_PNBUF);
845 NDFREE(&nd, NDF_ONLY_PNBUF);
850 * Common routine for chroot and chdir. Callers must provide a locked vnode
862 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
863 if (vp->v_type != VDIR)
866 error = mac_vnode_check_chdir(td->td_ucred, vp);
870 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
874 flags_to_rights(int flags, cap_rights_t *rightsp)
877 if (flags & O_EXEC) {
878 cap_rights_set(rightsp, CAP_FEXECVE);
880 switch ((flags & O_ACCMODE)) {
882 cap_rights_set(rightsp, CAP_READ);
885 cap_rights_set(rightsp, CAP_READ);
888 cap_rights_set(rightsp, CAP_WRITE);
889 if (!(flags & (O_APPEND | O_TRUNC)))
890 cap_rights_set(rightsp, CAP_SEEK);
896 cap_rights_set(rightsp, CAP_CREATE);
899 cap_rights_set(rightsp, CAP_FTRUNCATE);
901 if (flags & (O_SYNC | O_FSYNC))
902 cap_rights_set(rightsp, CAP_FSYNC);
904 if (flags & (O_EXLOCK | O_SHLOCK))
905 cap_rights_set(rightsp, CAP_FLOCK);
909 * Check permissions, allocate an open file structure, and call the device
910 * open routine if any.
912 #ifndef _SYS_SYSPROTO_H_
922 register struct open_args /* {
929 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
930 uap->flags, uap->mode));
933 #ifndef _SYS_SYSPROTO_H_
942 sys_openat(struct thread *td, struct openat_args *uap)
945 AUDIT_ARG_FD(uap->fd);
946 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
951 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
954 struct proc *p = td->td_proc;
955 struct filedesc *fdp = p->p_fd;
960 int cmode, error, indx;
964 AUDIT_ARG_FFLAGS(flags);
965 AUDIT_ARG_MODE(mode);
966 cap_rights_init(&rights, CAP_LOOKUP);
967 flags_to_rights(flags, &rights);
969 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
972 if (flags & O_EXEC) {
973 if (flags & O_ACCMODE)
975 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
978 flags = FFLAGS(flags);
982 * Allocate a file structure. The descriptor to reference it
983 * is allocated and set by finstall() below.
985 error = falloc_noinstall(td, &fp);
989 * An extra reference on `fp' has been held for us by
990 * falloc_noinstall().
992 /* Set the flags early so the finit in devfs can pick them up. */
993 fp->f_flag = flags & FMASK;
994 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
995 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
997 td->td_dupfd = -1; /* XXX check for fdopen */
998 error = vn_open(&nd, &flags, cmode, fp);
1001 * If the vn_open replaced the method vector, something
1002 * wonderous happened deep below and we just pass it up
1003 * pretending we know what we do.
1005 if (error == ENXIO && fp->f_ops != &badfileops)
1009 * Handle special fdopen() case. bleh.
1011 * Don't do this for relative (capability) lookups; we don't
1012 * understand exactly what would happen, and we don't think
1013 * that it ever should.
1015 if (nd.ni_strictrelative == 0 &&
1016 (error == ENODEV || error == ENXIO) &&
1017 td->td_dupfd >= 0) {
1018 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1027 NDFREE(&nd, NDF_ONLY_PNBUF);
1031 * Store the vnode, for any f_type. Typically, the vnode use
1032 * count is decremented by direct call to vn_closefile() for
1033 * files that switched type in the cdevsw fdopen() method.
1037 * If the file wasn't claimed by devfs bind it to the normal
1038 * vnode operations here.
1040 if (fp->f_ops == &badfileops) {
1041 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1043 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1044 DTYPE_VNODE, vp, &vnops);
1048 if (flags & O_TRUNC) {
1049 error = fo_truncate(fp, 0, td->td_ucred, td);
1055 * If we haven't already installed the FD (for dupfdopen), do so now.
1058 struct filecaps *fcaps;
1061 if (nd.ni_strictrelative == 1)
1062 fcaps = &nd.ni_filecaps;
1066 error = finstall(td, fp, &indx, flags, fcaps);
1067 /* On success finstall() consumes fcaps. */
1069 filecaps_free(&nd.ni_filecaps);
1073 filecaps_free(&nd.ni_filecaps);
1077 * Release our private reference, leaving the one associated with
1078 * the descriptor table intact.
1081 td->td_retval[0] = indx;
1084 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1093 #ifndef _SYS_SYSPROTO_H_
1094 struct ocreat_args {
1102 register struct ocreat_args /* {
1108 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1109 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1111 #endif /* COMPAT_43 */
1114 * Create a special file.
1116 #ifndef _SYS_SYSPROTO_H_
1126 register struct mknod_args /* {
1133 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1134 uap->mode, uap->dev));
1137 #ifndef _SYS_SYSPROTO_H_
1138 struct mknodat_args {
1146 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1149 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1154 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1160 struct nameidata nd;
1161 cap_rights_t rights;
1162 int error, whiteout = 0;
1164 AUDIT_ARG_MODE(mode);
1166 switch (mode & S_IFMT) {
1169 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1170 if (error == 0 && dev == VNOVAL)
1174 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1177 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1181 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1191 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1192 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1194 if ((error = namei(&nd)) != 0)
1198 NDFREE(&nd, NDF_ONLY_PNBUF);
1199 if (vp == nd.ni_dvp)
1207 vattr.va_mode = (mode & ALLPERMS) &
1208 ~td->td_proc->p_fd->fd_cmask;
1209 vattr.va_rdev = dev;
1212 switch (mode & S_IFMT) {
1213 case S_IFMT: /* used by badsect to flag bad sectors */
1214 vattr.va_type = VBAD;
1217 vattr.va_type = VCHR;
1220 vattr.va_type = VBLK;
1226 panic("kern_mknod: invalid mode");
1229 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1230 NDFREE(&nd, NDF_ONLY_PNBUF);
1232 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1237 if (error == 0 && !whiteout)
1238 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1239 &nd.ni_cnd, &vattr);
1243 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1245 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1246 &nd.ni_cnd, &vattr);
1251 NDFREE(&nd, NDF_ONLY_PNBUF);
1253 vn_finished_write(mp);
1258 * Create a named pipe.
1260 #ifndef _SYS_SYSPROTO_H_
1261 struct mkfifo_args {
1269 register struct mkfifo_args /* {
1275 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1279 #ifndef _SYS_SYSPROTO_H_
1280 struct mkfifoat_args {
1287 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1290 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1295 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1300 struct nameidata nd;
1301 cap_rights_t rights;
1304 AUDIT_ARG_MODE(mode);
1307 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1308 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1310 if ((error = namei(&nd)) != 0)
1312 if (nd.ni_vp != NULL) {
1313 NDFREE(&nd, NDF_ONLY_PNBUF);
1314 if (nd.ni_vp == nd.ni_dvp)
1321 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1322 NDFREE(&nd, NDF_ONLY_PNBUF);
1324 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1329 vattr.va_type = VFIFO;
1330 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1332 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1337 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1344 vn_finished_write(mp);
1345 NDFREE(&nd, NDF_ONLY_PNBUF);
1350 * Make a hard file link.
1352 #ifndef _SYS_SYSPROTO_H_
1361 register struct link_args /* {
1367 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1368 UIO_USERSPACE, FOLLOW));
1371 #ifndef _SYS_SYSPROTO_H_
1372 struct linkat_args {
1381 sys_linkat(struct thread *td, struct linkat_args *uap)
1386 if (flag & ~AT_SYMLINK_FOLLOW)
1389 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1390 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1393 int hardlink_check_uid = 0;
1394 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1395 &hardlink_check_uid, 0,
1396 "Unprivileged processes cannot create hard links to files owned by other "
1398 static int hardlink_check_gid = 0;
1399 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1400 &hardlink_check_gid, 0,
1401 "Unprivileged processes cannot create hard links to files owned by other "
1405 can_hardlink(struct vnode *vp, struct ucred *cred)
1410 if (!hardlink_check_uid && !hardlink_check_gid)
1413 error = VOP_GETATTR(vp, &va, cred);
1417 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1418 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1423 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1424 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1433 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1434 enum uio_seg segflg, int follow)
1438 struct nameidata nd;
1439 cap_rights_t rights;
1444 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1445 cap_rights_init(&rights, CAP_LINKAT_SOURCE), td);
1447 if ((error = namei(&nd)) != 0)
1449 NDFREE(&nd, NDF_ONLY_PNBUF);
1451 if (vp->v_type == VDIR) {
1453 return (EPERM); /* POSIX */
1455 NDINIT_ATRIGHTS(&nd, CREATE,
1456 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1457 cap_rights_init(&rights, CAP_LINKAT_TARGET), td);
1458 if ((error = namei(&nd)) == 0) {
1459 if (nd.ni_vp != NULL) {
1460 NDFREE(&nd, NDF_ONLY_PNBUF);
1461 if (nd.ni_dvp == nd.ni_vp)
1468 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1470 * Cross-device link. No need to recheck
1471 * vp->v_type, since it cannot change, except
1474 NDFREE(&nd, NDF_ONLY_PNBUF);
1478 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1479 error = can_hardlink(vp, td->td_ucred);
1482 error = mac_vnode_check_link(td->td_ucred,
1483 nd.ni_dvp, vp, &nd.ni_cnd);
1488 NDFREE(&nd, NDF_ONLY_PNBUF);
1491 error = vn_start_write(vp, &mp, V_NOWAIT);
1495 NDFREE(&nd, NDF_ONLY_PNBUF);
1496 error = vn_start_write(NULL, &mp,
1502 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1505 vn_finished_write(mp);
1506 NDFREE(&nd, NDF_ONLY_PNBUF);
1509 NDFREE(&nd, NDF_ONLY_PNBUF);
1519 * Make a symbolic link.
1521 #ifndef _SYS_SYSPROTO_H_
1522 struct symlink_args {
1528 sys_symlink(td, uap)
1530 register struct symlink_args /* {
1536 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1540 #ifndef _SYS_SYSPROTO_H_
1541 struct symlinkat_args {
1548 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1551 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1556 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1557 enum uio_seg segflg)
1562 struct nameidata nd;
1564 cap_rights_t rights;
1566 if (segflg == UIO_SYSSPACE) {
1569 syspath = uma_zalloc(namei_zone, M_WAITOK);
1570 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1573 AUDIT_ARG_TEXT(syspath);
1576 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1577 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1579 if ((error = namei(&nd)) != 0)
1582 NDFREE(&nd, NDF_ONLY_PNBUF);
1583 if (nd.ni_vp == nd.ni_dvp)
1591 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1592 NDFREE(&nd, NDF_ONLY_PNBUF);
1594 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1599 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1601 vattr.va_type = VLNK;
1602 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1607 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1613 NDFREE(&nd, NDF_ONLY_PNBUF);
1615 vn_finished_write(mp);
1617 if (segflg != UIO_SYSSPACE)
1618 uma_zfree(namei_zone, syspath);
1623 * Delete a whiteout from the filesystem.
1626 sys_undelete(td, uap)
1628 register struct undelete_args /* {
1633 struct nameidata nd;
1638 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1639 UIO_USERSPACE, uap->path, td);
1644 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1645 NDFREE(&nd, NDF_ONLY_PNBUF);
1646 if (nd.ni_vp == nd.ni_dvp)
1654 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1655 NDFREE(&nd, NDF_ONLY_PNBUF);
1657 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1661 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1662 NDFREE(&nd, NDF_ONLY_PNBUF);
1664 vn_finished_write(mp);
1669 * Delete a name from the filesystem.
1671 #ifndef _SYS_SYSPROTO_H_
1672 struct unlink_args {
1679 struct unlink_args /* {
1684 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1687 #ifndef _SYS_SYSPROTO_H_
1688 struct unlinkat_args {
1695 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1697 int flag = uap->flag;
1699 char *path = uap->path;
1701 if (flag & ~AT_REMOVEDIR)
1704 if (flag & AT_REMOVEDIR)
1705 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1707 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1711 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1716 struct nameidata nd;
1718 cap_rights_t rights;
1723 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1724 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1725 if ((error = namei(&nd)) != 0)
1726 return (error == EINVAL ? EPERM : error);
1728 if (vp->v_type == VDIR && oldinum == 0) {
1729 error = EPERM; /* POSIX */
1730 } else if (oldinum != 0 &&
1731 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1732 sb.st_ino != oldinum) {
1733 error = EIDRM; /* Identifier removed */
1736 * The root of a mounted filesystem cannot be deleted.
1738 * XXX: can this only be a VDIR case?
1740 if (vp->v_vflag & VV_ROOT)
1744 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1745 NDFREE(&nd, NDF_ONLY_PNBUF);
1747 if (vp == nd.ni_dvp)
1751 if ((error = vn_start_write(NULL, &mp,
1752 V_XSLEEP | PCATCH)) != 0)
1757 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1762 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1763 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1767 vn_finished_write(mp);
1769 NDFREE(&nd, NDF_ONLY_PNBUF);
1771 if (vp == nd.ni_dvp)
1779 * Reposition read/write file offset.
1781 #ifndef _SYS_SYSPROTO_H_
1792 register struct lseek_args /* {
1800 cap_rights_t rights;
1803 AUDIT_ARG_FD(uap->fd);
1804 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1807 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1808 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1813 #if defined(COMPAT_43)
1815 * Reposition read/write file offset.
1817 #ifndef _SYS_SYSPROTO_H_
1818 struct olseek_args {
1827 register struct olseek_args /* {
1833 struct lseek_args /* {
1841 nuap.offset = uap->offset;
1842 nuap.whence = uap->whence;
1843 return (sys_lseek(td, &nuap));
1845 #endif /* COMPAT_43 */
1847 #if defined(COMPAT_FREEBSD6)
1848 /* Version with the 'pad' argument */
1850 freebsd6_lseek(td, uap)
1852 register struct freebsd6_lseek_args *uap;
1854 struct lseek_args ouap;
1857 ouap.offset = uap->offset;
1858 ouap.whence = uap->whence;
1859 return (sys_lseek(td, &ouap));
1864 * Check access permissions using passed credentials.
1867 vn_access(vp, user_flags, cred, td)
1876 /* Flags == 0 means only check for existence. */
1877 if (user_flags == 0)
1881 if (user_flags & R_OK)
1883 if (user_flags & W_OK)
1885 if (user_flags & X_OK)
1888 error = mac_vnode_check_access(cred, vp, accmode);
1892 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1893 error = VOP_ACCESS(vp, accmode, cred, td);
1898 * Check access permissions using "real" credentials.
1900 #ifndef _SYS_SYSPROTO_H_
1901 struct access_args {
1909 register struct access_args /* {
1915 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1919 #ifndef _SYS_SYSPROTO_H_
1920 struct faccessat_args {
1928 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1931 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1936 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1937 int flag, int amode)
1939 struct ucred *cred, *usecred;
1941 struct nameidata nd;
1942 cap_rights_t rights;
1945 if (flag & ~AT_EACCESS)
1947 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1951 * Create and modify a temporary credential instead of one that
1952 * is potentially shared (if we need one).
1954 cred = td->td_ucred;
1955 if ((flag & AT_EACCESS) == 0 &&
1956 ((cred->cr_uid != cred->cr_ruid ||
1957 cred->cr_rgid != cred->cr_groups[0]))) {
1958 usecred = crdup(cred);
1959 usecred->cr_uid = cred->cr_ruid;
1960 usecred->cr_groups[0] = cred->cr_rgid;
1961 td->td_ucred = usecred;
1964 AUDIT_ARG_VALUE(amode);
1965 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1966 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
1968 if ((error = namei(&nd)) != 0)
1972 error = vn_access(vp, amode, usecred, td);
1973 NDFREE(&nd, NDF_ONLY_PNBUF);
1976 if (usecred != cred) {
1977 td->td_ucred = cred;
1984 * Check access permissions using "effective" credentials.
1986 #ifndef _SYS_SYSPROTO_H_
1987 struct eaccess_args {
1993 sys_eaccess(td, uap)
1995 register struct eaccess_args /* {
2001 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2002 AT_EACCESS, uap->amode));
2005 #if defined(COMPAT_43)
2007 * Get file status; this version follows links.
2009 #ifndef _SYS_SYSPROTO_H_
2018 register struct ostat_args /* {
2027 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2032 return (copyout(&osb, uap->ub, sizeof (osb)));
2036 * Get file status; this version does not follow links.
2038 #ifndef _SYS_SYSPROTO_H_
2039 struct olstat_args {
2047 register struct olstat_args /* {
2056 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2057 UIO_USERSPACE, &sb, NULL);
2061 return (copyout(&osb, uap->ub, sizeof (osb)));
2065 * Convert from an old to a new stat structure.
2073 bzero(ost, sizeof(*ost));
2074 ost->st_dev = st->st_dev;
2075 ost->st_ino = st->st_ino;
2076 ost->st_mode = st->st_mode;
2077 ost->st_nlink = st->st_nlink;
2078 ost->st_uid = st->st_uid;
2079 ost->st_gid = st->st_gid;
2080 ost->st_rdev = st->st_rdev;
2081 if (st->st_size < (quad_t)1 << 32)
2082 ost->st_size = st->st_size;
2085 ost->st_atim = st->st_atim;
2086 ost->st_mtim = st->st_mtim;
2087 ost->st_ctim = st->st_ctim;
2088 ost->st_blksize = st->st_blksize;
2089 ost->st_blocks = st->st_blocks;
2090 ost->st_flags = st->st_flags;
2091 ost->st_gen = st->st_gen;
2093 #endif /* COMPAT_43 */
2096 * Get file status; this version follows links.
2098 #ifndef _SYS_SYSPROTO_H_
2107 register struct stat_args /* {
2115 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2118 error = copyout(&sb, uap->ub, sizeof (sb));
2122 #ifndef _SYS_SYSPROTO_H_
2123 struct fstatat_args {
2131 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2136 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2137 UIO_USERSPACE, &sb, NULL);
2139 error = copyout(&sb, uap->buf, sizeof (sb));
2144 kern_statat(struct thread *td, int flag, int fd, char *path,
2145 enum uio_seg pathseg, struct stat *sbp,
2146 void (*hook)(struct vnode *vp, struct stat *sbp))
2148 struct nameidata nd;
2150 cap_rights_t rights;
2153 if (flag & ~AT_SYMLINK_NOFOLLOW)
2156 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2157 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2158 cap_rights_init(&rights, CAP_FSTAT), td);
2160 if ((error = namei(&nd)) != 0)
2162 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2164 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2165 if (S_ISREG(sb.st_mode))
2166 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2167 if (__predict_false(hook != NULL))
2168 hook(nd.ni_vp, &sb);
2170 NDFREE(&nd, NDF_ONLY_PNBUF);
2176 if (KTRPOINT(td, KTR_STRUCT))
2183 * Get file status; this version does not follow links.
2185 #ifndef _SYS_SYSPROTO_H_
2194 register struct lstat_args /* {
2202 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2203 UIO_USERSPACE, &sb, NULL);
2205 error = copyout(&sb, uap->ub, sizeof (sb));
2210 * Implementation of the NetBSD [l]stat() functions.
2218 bzero(nsb, sizeof *nsb);
2219 nsb->st_dev = sb->st_dev;
2220 nsb->st_ino = sb->st_ino;
2221 nsb->st_mode = sb->st_mode;
2222 nsb->st_nlink = sb->st_nlink;
2223 nsb->st_uid = sb->st_uid;
2224 nsb->st_gid = sb->st_gid;
2225 nsb->st_rdev = sb->st_rdev;
2226 nsb->st_atim = sb->st_atim;
2227 nsb->st_mtim = sb->st_mtim;
2228 nsb->st_ctim = sb->st_ctim;
2229 nsb->st_size = sb->st_size;
2230 nsb->st_blocks = sb->st_blocks;
2231 nsb->st_blksize = sb->st_blksize;
2232 nsb->st_flags = sb->st_flags;
2233 nsb->st_gen = sb->st_gen;
2234 nsb->st_birthtim = sb->st_birthtim;
2237 #ifndef _SYS_SYSPROTO_H_
2246 register struct nstat_args /* {
2255 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2259 cvtnstat(&sb, &nsb);
2260 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2264 * NetBSD lstat. Get file status; this version does not follow links.
2266 #ifndef _SYS_SYSPROTO_H_
2275 register struct nlstat_args /* {
2284 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2285 UIO_USERSPACE, &sb, NULL);
2288 cvtnstat(&sb, &nsb);
2289 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2293 * Get configurable pathname variables.
2295 #ifndef _SYS_SYSPROTO_H_
2296 struct pathconf_args {
2302 sys_pathconf(td, uap)
2304 register struct pathconf_args /* {
2310 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2313 #ifndef _SYS_SYSPROTO_H_
2314 struct lpathconf_args {
2320 sys_lpathconf(td, uap)
2322 register struct lpathconf_args /* {
2328 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2333 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2336 struct nameidata nd;
2339 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2341 if ((error = namei(&nd)) != 0)
2343 NDFREE(&nd, NDF_ONLY_PNBUF);
2345 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2351 * Return target name of a symbolic link.
2353 #ifndef _SYS_SYSPROTO_H_
2354 struct readlink_args {
2361 sys_readlink(td, uap)
2363 register struct readlink_args /* {
2370 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2371 uap->buf, UIO_USERSPACE, uap->count));
2373 #ifndef _SYS_SYSPROTO_H_
2374 struct readlinkat_args {
2382 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2385 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2386 uap->buf, UIO_USERSPACE, uap->bufsize));
2390 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2391 char *buf, enum uio_seg bufseg, size_t count)
2396 struct nameidata nd;
2399 if (count > IOSIZE_MAX)
2402 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2403 pathseg, path, fd, td);
2405 if ((error = namei(&nd)) != 0)
2407 NDFREE(&nd, NDF_ONLY_PNBUF);
2410 error = mac_vnode_check_readlink(td->td_ucred, vp);
2416 if (vp->v_type != VLNK)
2419 aiov.iov_base = buf;
2420 aiov.iov_len = count;
2421 auio.uio_iov = &aiov;
2422 auio.uio_iovcnt = 1;
2423 auio.uio_offset = 0;
2424 auio.uio_rw = UIO_READ;
2425 auio.uio_segflg = bufseg;
2427 auio.uio_resid = count;
2428 error = VOP_READLINK(vp, &auio, td->td_ucred);
2429 td->td_retval[0] = count - auio.uio_resid;
2436 * Common implementation code for chflags() and fchflags().
2439 setfflags(td, vp, flags)
2448 /* We can't support the value matching VNOVAL. */
2449 if (flags == VNOVAL)
2450 return (EOPNOTSUPP);
2453 * Prevent non-root users from setting flags on devices. When
2454 * a device is reused, users can retain ownership of the device
2455 * if they are allowed to set flags and programs assume that
2456 * chown can't fail when done as root.
2458 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2459 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2464 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2467 vattr.va_flags = flags;
2468 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2470 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2473 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2475 vn_finished_write(mp);
2480 * Change flags of a file given a path name.
2482 #ifndef _SYS_SYSPROTO_H_
2483 struct chflags_args {
2489 sys_chflags(td, uap)
2491 register struct chflags_args /* {
2497 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2501 #ifndef _SYS_SYSPROTO_H_
2502 struct chflagsat_args {
2510 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2513 const char *path = uap->path;
2514 u_long flags = uap->flags;
2515 int atflag = uap->atflag;
2517 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2520 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2524 * Same as chflags() but doesn't follow symlinks.
2527 sys_lchflags(td, uap)
2529 register struct lchflags_args /* {
2535 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2536 uap->flags, AT_SYMLINK_NOFOLLOW));
2540 kern_chflagsat(struct thread *td, int fd, const char *path,
2541 enum uio_seg pathseg, u_long flags, int atflag)
2543 struct nameidata nd;
2544 cap_rights_t rights;
2547 AUDIT_ARG_FFLAGS(flags);
2548 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2549 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2550 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2551 if ((error = namei(&nd)) != 0)
2553 NDFREE(&nd, NDF_ONLY_PNBUF);
2554 error = setfflags(td, nd.ni_vp, flags);
2560 * Change flags of a file given a file descriptor.
2562 #ifndef _SYS_SYSPROTO_H_
2563 struct fchflags_args {
2569 sys_fchflags(td, uap)
2571 register struct fchflags_args /* {
2577 cap_rights_t rights;
2580 AUDIT_ARG_FD(uap->fd);
2581 AUDIT_ARG_FFLAGS(uap->flags);
2582 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2587 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2588 AUDIT_ARG_VNODE1(fp->f_vnode);
2589 VOP_UNLOCK(fp->f_vnode, 0);
2591 error = setfflags(td, fp->f_vnode, uap->flags);
2597 * Common implementation code for chmod(), lchmod() and fchmod().
2600 setfmode(td, cred, vp, mode)
2610 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2612 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2614 vattr.va_mode = mode & ALLPERMS;
2616 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2619 error = VOP_SETATTR(vp, &vattr, cred);
2621 vn_finished_write(mp);
2626 * Change mode of a file given path name.
2628 #ifndef _SYS_SYSPROTO_H_
2637 register struct chmod_args /* {
2643 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2647 #ifndef _SYS_SYSPROTO_H_
2648 struct fchmodat_args {
2656 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2658 int flag = uap->flag;
2660 char *path = uap->path;
2661 mode_t mode = uap->mode;
2663 if (flag & ~AT_SYMLINK_NOFOLLOW)
2666 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2670 * Change mode of a file given path name (don't follow links.)
2672 #ifndef _SYS_SYSPROTO_H_
2673 struct lchmod_args {
2681 register struct lchmod_args /* {
2687 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2688 uap->mode, AT_SYMLINK_NOFOLLOW));
2692 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2693 mode_t mode, int flag)
2695 struct nameidata nd;
2696 cap_rights_t rights;
2699 AUDIT_ARG_MODE(mode);
2700 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2701 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2702 cap_rights_init(&rights, CAP_FCHMOD), td);
2703 if ((error = namei(&nd)) != 0)
2705 NDFREE(&nd, NDF_ONLY_PNBUF);
2706 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2712 * Change mode of a file given a file descriptor.
2714 #ifndef _SYS_SYSPROTO_H_
2715 struct fchmod_args {
2721 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2724 cap_rights_t rights;
2727 AUDIT_ARG_FD(uap->fd);
2728 AUDIT_ARG_MODE(uap->mode);
2730 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2733 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2739 * Common implementation for chown(), lchown(), and fchown()
2742 setfown(td, cred, vp, uid, gid)
2753 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2755 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2760 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2764 error = VOP_SETATTR(vp, &vattr, cred);
2766 vn_finished_write(mp);
2771 * Set ownership given a path name.
2773 #ifndef _SYS_SYSPROTO_H_
2783 register struct chown_args /* {
2790 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2794 #ifndef _SYS_SYSPROTO_H_
2795 struct fchownat_args {
2804 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2809 if (flag & ~AT_SYMLINK_NOFOLLOW)
2812 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2813 uap->gid, uap->flag));
2817 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2818 int uid, int gid, int flag)
2820 struct nameidata nd;
2821 cap_rights_t rights;
2824 AUDIT_ARG_OWNER(uid, gid);
2825 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2826 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2827 cap_rights_init(&rights, CAP_FCHOWN), td);
2829 if ((error = namei(&nd)) != 0)
2831 NDFREE(&nd, NDF_ONLY_PNBUF);
2832 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2838 * Set ownership given a path name, do not cross symlinks.
2840 #ifndef _SYS_SYSPROTO_H_
2841 struct lchown_args {
2850 register struct lchown_args /* {
2857 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2858 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2862 * Set ownership given a file descriptor.
2864 #ifndef _SYS_SYSPROTO_H_
2865 struct fchown_args {
2874 register struct fchown_args /* {
2881 cap_rights_t rights;
2884 AUDIT_ARG_FD(uap->fd);
2885 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2886 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2889 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2895 * Common implementation code for utimes(), lutimes(), and futimes().
2898 getutimes(usrtvp, tvpseg, tsp)
2899 const struct timeval *usrtvp;
2900 enum uio_seg tvpseg;
2901 struct timespec *tsp;
2903 struct timeval tv[2];
2904 const struct timeval *tvp;
2907 if (usrtvp == NULL) {
2908 vfs_timestamp(&tsp[0]);
2911 if (tvpseg == UIO_SYSSPACE) {
2914 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2919 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2920 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2922 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2923 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2929 * Common implementation code for futimens(), utimensat().
2931 #define UTIMENS_NULL 0x1
2932 #define UTIMENS_EXIT 0x2
2934 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2935 struct timespec *tsp, int *retflags)
2937 struct timespec tsnow;
2940 vfs_timestamp(&tsnow);
2942 if (usrtsp == NULL) {
2945 *retflags |= UTIMENS_NULL;
2948 if (tspseg == UIO_SYSSPACE) {
2951 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2953 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2954 *retflags |= UTIMENS_EXIT;
2955 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2956 *retflags |= UTIMENS_NULL;
2957 if (tsp[0].tv_nsec == UTIME_OMIT)
2958 tsp[0].tv_sec = VNOVAL;
2959 else if (tsp[0].tv_nsec == UTIME_NOW)
2961 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2963 if (tsp[1].tv_nsec == UTIME_OMIT)
2964 tsp[1].tv_sec = VNOVAL;
2965 else if (tsp[1].tv_nsec == UTIME_NOW)
2967 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2974 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2978 setutimes(td, vp, ts, numtimes, nullflag)
2981 const struct timespec *ts;
2987 int error, setbirthtime;
2989 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2991 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2993 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
2994 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2997 vattr.va_atime = ts[0];
2998 vattr.va_mtime = ts[1];
3000 vattr.va_birthtime = ts[1];
3002 vattr.va_birthtime = ts[2];
3004 vattr.va_vaflags |= VA_UTIMES_NULL;
3006 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3010 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3012 vn_finished_write(mp);
3017 * Set the access and modification times of a file.
3019 #ifndef _SYS_SYSPROTO_H_
3020 struct utimes_args {
3022 struct timeval *tptr;
3028 register struct utimes_args /* {
3030 struct timeval *tptr;
3034 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3035 uap->tptr, UIO_USERSPACE));
3038 #ifndef _SYS_SYSPROTO_H_
3039 struct futimesat_args {
3042 const struct timeval * times;
3046 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3049 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3050 uap->times, UIO_USERSPACE));
3054 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3055 struct timeval *tptr, enum uio_seg tptrseg)
3057 struct nameidata nd;
3058 struct timespec ts[2];
3059 cap_rights_t rights;
3062 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3064 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3065 cap_rights_init(&rights, CAP_FUTIMES), td);
3067 if ((error = namei(&nd)) != 0)
3069 NDFREE(&nd, NDF_ONLY_PNBUF);
3070 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3076 * Set the access and modification times of a file.
3078 #ifndef _SYS_SYSPROTO_H_
3079 struct lutimes_args {
3081 struct timeval *tptr;
3085 sys_lutimes(td, uap)
3087 register struct lutimes_args /* {
3089 struct timeval *tptr;
3093 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3098 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3099 struct timeval *tptr, enum uio_seg tptrseg)
3101 struct timespec ts[2];
3102 struct nameidata nd;
3105 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3107 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3108 if ((error = namei(&nd)) != 0)
3110 NDFREE(&nd, NDF_ONLY_PNBUF);
3111 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3117 * Set the access and modification times of a file.
3119 #ifndef _SYS_SYSPROTO_H_
3120 struct futimes_args {
3122 struct timeval *tptr;
3126 sys_futimes(td, uap)
3128 register struct futimes_args /* {
3130 struct timeval *tptr;
3134 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3138 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3139 enum uio_seg tptrseg)
3141 struct timespec ts[2];
3143 cap_rights_t rights;
3147 error = getutimes(tptr, tptrseg, ts);
3150 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3154 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3155 AUDIT_ARG_VNODE1(fp->f_vnode);
3156 VOP_UNLOCK(fp->f_vnode, 0);
3158 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3164 sys_futimens(struct thread *td, struct futimens_args *uap)
3167 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3171 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3172 enum uio_seg tptrseg)
3174 struct timespec ts[2];
3176 cap_rights_t rights;
3180 error = getutimens(tptr, tptrseg, ts, &flags);
3183 if (flags & UTIMENS_EXIT)
3185 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3189 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3190 AUDIT_ARG_VNODE1(fp->f_vnode);
3191 VOP_UNLOCK(fp->f_vnode, 0);
3193 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3199 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3202 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3203 uap->times, UIO_USERSPACE, uap->flag));
3207 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3208 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3210 struct nameidata nd;
3211 struct timespec ts[2];
3212 cap_rights_t rights;
3215 if (flag & ~AT_SYMLINK_NOFOLLOW)
3218 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3220 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3221 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3222 cap_rights_init(&rights, CAP_FUTIMES), td);
3223 if ((error = namei(&nd)) != 0)
3226 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3228 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3229 * "Search permission is denied by a component of the path prefix."
3231 NDFREE(&nd, NDF_ONLY_PNBUF);
3232 if ((flags & UTIMENS_EXIT) == 0)
3233 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3239 * Truncate a file given its path name.
3241 #ifndef _SYS_SYSPROTO_H_
3242 struct truncate_args {
3249 sys_truncate(td, uap)
3251 register struct truncate_args /* {
3258 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3262 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3268 struct nameidata nd;
3273 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3274 if ((error = namei(&nd)) != 0)
3277 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3278 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3279 vn_rangelock_unlock(vp, rl_cookie);
3283 NDFREE(&nd, NDF_ONLY_PNBUF);
3284 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3285 if (vp->v_type == VDIR)
3288 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3291 else if ((error = vn_writechk(vp)) == 0 &&
3292 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3294 vattr.va_size = length;
3295 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3298 vn_finished_write(mp);
3299 vn_rangelock_unlock(vp, rl_cookie);
3304 #if defined(COMPAT_43)
3306 * Truncate a file given its path name.
3308 #ifndef _SYS_SYSPROTO_H_
3309 struct otruncate_args {
3317 register struct otruncate_args /* {
3322 struct truncate_args /* {
3328 nuap.path = uap->path;
3329 nuap.length = uap->length;
3330 return (sys_truncate(td, &nuap));
3332 #endif /* COMPAT_43 */
3334 #if defined(COMPAT_FREEBSD6)
3335 /* Versions with the pad argument */
3337 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3339 struct truncate_args ouap;
3341 ouap.path = uap->path;
3342 ouap.length = uap->length;
3343 return (sys_truncate(td, &ouap));
3347 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3349 struct ftruncate_args ouap;
3352 ouap.length = uap->length;
3353 return (sys_ftruncate(td, &ouap));
3358 * Sync an open file.
3360 #ifndef _SYS_SYSPROTO_H_
3368 struct fsync_args /* {
3375 cap_rights_t rights;
3376 int error, lock_flags;
3378 AUDIT_ARG_FD(uap->fd);
3379 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3383 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3386 if (MNT_SHARED_WRITES(mp) ||
3387 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3388 lock_flags = LK_SHARED;
3390 lock_flags = LK_EXCLUSIVE;
3392 vn_lock(vp, lock_flags | LK_RETRY);
3393 AUDIT_ARG_VNODE1(vp);
3394 if (vp->v_object != NULL) {
3395 VM_OBJECT_WLOCK(vp->v_object);
3396 vm_object_page_clean(vp->v_object, 0, 0, 0);
3397 VM_OBJECT_WUNLOCK(vp->v_object);
3399 error = VOP_FSYNC(vp, MNT_WAIT, td);
3402 vn_finished_write(mp);
3409 * Rename files. Source and destination must either both be directories, or
3410 * both not be directories. If target is a directory, it must be empty.
3412 #ifndef _SYS_SYSPROTO_H_
3413 struct rename_args {
3421 register struct rename_args /* {
3427 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3428 uap->to, UIO_USERSPACE));
3431 #ifndef _SYS_SYSPROTO_H_
3432 struct renameat_args {
3440 sys_renameat(struct thread *td, struct renameat_args *uap)
3443 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3448 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3449 enum uio_seg pathseg)
3451 struct mount *mp = NULL;
3452 struct vnode *tvp, *fvp, *tdvp;
3453 struct nameidata fromnd, tond;
3454 cap_rights_t rights;
3460 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3461 AUDITVNODE1, pathseg, old, oldfd,
3462 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3464 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3465 pathseg, old, oldfd,
3466 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3469 if ((error = namei(&fromnd)) != 0)
3472 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3473 fromnd.ni_vp, &fromnd.ni_cnd);
3474 VOP_UNLOCK(fromnd.ni_dvp, 0);
3475 if (fromnd.ni_dvp != fromnd.ni_vp)
3476 VOP_UNLOCK(fromnd.ni_vp, 0);
3479 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3480 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3481 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3482 if (fromnd.ni_vp->v_type == VDIR)
3483 tond.ni_cnd.cn_flags |= WILLBEDIR;
3484 if ((error = namei(&tond)) != 0) {
3485 /* Translate error code for rename("dir1", "dir2/."). */
3486 if (error == EISDIR && fvp->v_type == VDIR)
3488 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3489 vrele(fromnd.ni_dvp);
3495 error = vn_start_write(fvp, &mp, V_NOWAIT);
3497 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3498 NDFREE(&tond, NDF_ONLY_PNBUF);
3505 vrele(fromnd.ni_dvp);
3507 vrele(tond.ni_startdir);
3508 if (fromnd.ni_startdir != NULL)
3509 vrele(fromnd.ni_startdir);
3510 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3516 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3519 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3524 if (newfd != AT_FDCWD) {
3526 * If the target already exists we require CAP_UNLINKAT
3529 error = cap_check(&tond.ni_filecaps.fc_rights,
3530 cap_rights_init(&rights, CAP_UNLINKAT));
3541 * If the source is the same as the destination (that is, if they
3542 * are links to the same vnode), then there is nothing to do.
3548 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3549 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3553 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3554 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3555 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3556 NDFREE(&tond, NDF_ONLY_PNBUF);
3558 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3559 NDFREE(&tond, NDF_ONLY_PNBUF);
3566 vrele(fromnd.ni_dvp);
3569 vrele(tond.ni_startdir);
3570 vn_finished_write(mp);
3572 if (fromnd.ni_startdir)
3573 vrele(fromnd.ni_startdir);
3580 * Make a directory file.
3582 #ifndef _SYS_SYSPROTO_H_
3591 register struct mkdir_args /* {
3597 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3601 #ifndef _SYS_SYSPROTO_H_
3602 struct mkdirat_args {
3609 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3612 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3616 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3622 struct nameidata nd;
3623 cap_rights_t rights;
3626 AUDIT_ARG_MODE(mode);
3629 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3630 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3632 nd.ni_cnd.cn_flags |= WILLBEDIR;
3633 if ((error = namei(&nd)) != 0)
3637 NDFREE(&nd, NDF_ONLY_PNBUF);
3639 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3640 * the strange behaviour of leaving the vnode unlocked
3641 * if the target is the same vnode as the parent.
3643 if (vp == nd.ni_dvp)
3650 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3651 NDFREE(&nd, NDF_ONLY_PNBUF);
3653 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3658 vattr.va_type = VDIR;
3659 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3661 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3666 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3670 NDFREE(&nd, NDF_ONLY_PNBUF);
3674 vn_finished_write(mp);
3679 * Remove a directory file.
3681 #ifndef _SYS_SYSPROTO_H_
3689 struct rmdir_args /* {
3694 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3698 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3702 struct nameidata nd;
3703 cap_rights_t rights;
3708 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3709 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3710 if ((error = namei(&nd)) != 0)
3713 if (vp->v_type != VDIR) {
3718 * No rmdir "." please.
3720 if (nd.ni_dvp == vp) {
3725 * The root of a mounted filesystem cannot be deleted.
3727 if (vp->v_vflag & VV_ROOT) {
3732 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3737 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3738 NDFREE(&nd, NDF_ONLY_PNBUF);
3740 if (nd.ni_dvp == vp)
3744 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3748 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3749 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3750 vn_finished_write(mp);
3752 NDFREE(&nd, NDF_ONLY_PNBUF);
3754 if (nd.ni_dvp == vp)
3763 * Read a block of directory entries in a filesystem independent format.
3765 #ifndef _SYS_SYSPROTO_H_
3766 struct ogetdirentries_args {
3774 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3779 error = kern_ogetdirentries(td, uap, &loff);
3781 error = copyout(&loff, uap->basep, sizeof(long));
3786 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3791 struct uio auio, kuio;
3792 struct iovec aiov, kiov;
3793 struct dirent *dp, *edp;
3794 cap_rights_t rights;
3796 int error, eofflag, readcnt;
3800 /* XXX arbitrary sanity limit on `count'. */
3801 if (uap->count > 64 * 1024)
3803 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp);
3806 if ((fp->f_flag & FREAD) == 0) {
3811 foffset = foffset_lock(fp, 0);
3813 if (vp->v_type != VDIR) {
3814 foffset_unlock(fp, foffset, 0);
3818 aiov.iov_base = uap->buf;
3819 aiov.iov_len = uap->count;
3820 auio.uio_iov = &aiov;
3821 auio.uio_iovcnt = 1;
3822 auio.uio_rw = UIO_READ;
3823 auio.uio_segflg = UIO_USERSPACE;
3825 auio.uio_resid = uap->count;
3826 vn_lock(vp, LK_SHARED | LK_RETRY);
3827 loff = auio.uio_offset = foffset;
3829 error = mac_vnode_check_readdir(td->td_ucred, vp);
3832 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3837 # if (BYTE_ORDER != LITTLE_ENDIAN)
3838 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3839 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3841 foffset = auio.uio_offset;
3846 kuio.uio_iov = &kiov;
3847 kuio.uio_segflg = UIO_SYSSPACE;
3848 kiov.iov_len = uap->count;
3849 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3850 kiov.iov_base = dirbuf;
3851 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3853 foffset = kuio.uio_offset;
3855 readcnt = uap->count - kuio.uio_resid;
3856 edp = (struct dirent *)&dirbuf[readcnt];
3857 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3858 # if (BYTE_ORDER == LITTLE_ENDIAN)
3860 * The expected low byte of
3861 * dp->d_namlen is our dp->d_type.
3862 * The high MBZ byte of dp->d_namlen
3863 * is our dp->d_namlen.
3865 dp->d_type = dp->d_namlen;
3869 * The dp->d_type is the high byte
3870 * of the expected dp->d_namlen,
3871 * so must be zero'ed.
3875 if (dp->d_reclen > 0) {
3876 dp = (struct dirent *)
3877 ((char *)dp + dp->d_reclen);
3884 error = uiomove(dirbuf, readcnt, &auio);
3886 free(dirbuf, M_TEMP);
3890 foffset_unlock(fp, foffset, 0);
3894 if (uap->count == auio.uio_resid &&
3895 (vp->v_vflag & VV_ROOT) &&
3896 (vp->v_mount->mnt_flag & MNT_UNION)) {
3897 struct vnode *tvp = vp;
3898 vp = vp->v_mount->mnt_vnodecovered;
3907 foffset_unlock(fp, foffset, 0);
3909 td->td_retval[0] = uap->count - auio.uio_resid;
3914 #endif /* COMPAT_43 */
3917 * Read a block of directory entries in a filesystem independent format.
3919 #ifndef _SYS_SYSPROTO_H_
3920 struct getdirentries_args {
3928 sys_getdirentries(td, uap)
3930 register struct getdirentries_args /* {
3940 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3941 NULL, UIO_USERSPACE);
3944 if (uap->basep != NULL)
3945 error = copyout(&base, uap->basep, sizeof(long));
3950 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3951 long *basep, ssize_t *residp, enum uio_seg bufseg)
3957 cap_rights_t rights;
3963 if (count > IOSIZE_MAX)
3965 auio.uio_resid = count;
3966 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3969 if ((fp->f_flag & FREAD) == 0) {
3974 foffset = foffset_lock(fp, 0);
3976 if (vp->v_type != VDIR) {
3980 aiov.iov_base = buf;
3981 aiov.iov_len = count;
3982 auio.uio_iov = &aiov;
3983 auio.uio_iovcnt = 1;
3984 auio.uio_rw = UIO_READ;
3985 auio.uio_segflg = bufseg;
3987 vn_lock(vp, LK_SHARED | LK_RETRY);
3988 AUDIT_ARG_VNODE1(vp);
3989 loff = auio.uio_offset = foffset;
3991 error = mac_vnode_check_readdir(td->td_ucred, vp);
3994 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3996 foffset = auio.uio_offset;
4001 if (count == auio.uio_resid &&
4002 (vp->v_vflag & VV_ROOT) &&
4003 (vp->v_mount->mnt_flag & MNT_UNION)) {
4004 struct vnode *tvp = vp;
4006 vp = vp->v_mount->mnt_vnodecovered;
4017 *residp = auio.uio_resid;
4018 td->td_retval[0] = count - auio.uio_resid;
4020 foffset_unlock(fp, foffset, 0);
4025 #ifndef _SYS_SYSPROTO_H_
4026 struct getdents_args {
4033 sys_getdents(td, uap)
4035 register struct getdents_args /* {
4041 struct getdirentries_args ap;
4045 ap.count = uap->count;
4047 return (sys_getdirentries(td, &ap));
4051 * Set the mode mask for creation of filesystem nodes.
4053 #ifndef _SYS_SYSPROTO_H_
4061 struct umask_args /* {
4065 struct filedesc *fdp;
4067 fdp = td->td_proc->p_fd;
4068 FILEDESC_XLOCK(fdp);
4069 td->td_retval[0] = fdp->fd_cmask;
4070 fdp->fd_cmask = uap->newmask & ALLPERMS;
4071 FILEDESC_XUNLOCK(fdp);
4076 * Void all references to file by ripping underlying filesystem away from
4079 #ifndef _SYS_SYSPROTO_H_
4080 struct revoke_args {
4087 register struct revoke_args /* {
4093 struct nameidata nd;
4096 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4098 if ((error = namei(&nd)) != 0)
4101 NDFREE(&nd, NDF_ONLY_PNBUF);
4102 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4107 error = mac_vnode_check_revoke(td->td_ucred, vp);
4111 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4114 if (td->td_ucred->cr_uid != vattr.va_uid) {
4115 error = priv_check(td, PRIV_VFS_ADMIN);
4120 VOP_REVOKE(vp, REVOKEALL);
4127 * Convert a user file descriptor to a kernel file entry and check that, if it
4128 * is a capability, the correct rights are present. A reference on the file
4129 * entry is held upon returning.
4132 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4137 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4142 * The file could be not of the vnode type, or it may be not
4143 * yet fully initialized, in which case the f_vnode pointer
4144 * may be set, but f_ops is still badfileops. E.g.,
4145 * devfs_open() transiently create such situation to
4146 * facilitate csw d_fdopen().
4148 * Dupfdopen() handling in kern_openat() installs the
4149 * half-baked file into the process descriptor table, allowing
4150 * other thread to dereference it. Guard against the race by
4153 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4163 * Get an (NFS) file handle.
4165 #ifndef _SYS_SYSPROTO_H_
4166 struct lgetfh_args {
4174 register struct lgetfh_args *uap;
4176 struct nameidata nd;
4178 register struct vnode *vp;
4181 error = priv_check(td, PRIV_VFS_GETFH);
4184 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4189 NDFREE(&nd, NDF_ONLY_PNBUF);
4191 bzero(&fh, sizeof(fh));
4192 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4193 error = VOP_VPTOFH(vp, &fh.fh_fid);
4196 error = copyout(&fh, uap->fhp, sizeof (fh));
4200 #ifndef _SYS_SYSPROTO_H_
4209 register struct getfh_args *uap;
4211 struct nameidata nd;
4213 register struct vnode *vp;
4216 error = priv_check(td, PRIV_VFS_GETFH);
4219 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4224 NDFREE(&nd, NDF_ONLY_PNBUF);
4226 bzero(&fh, sizeof(fh));
4227 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4228 error = VOP_VPTOFH(vp, &fh.fh_fid);
4231 error = copyout(&fh, uap->fhp, sizeof (fh));
4236 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4239 * warning: do not remove the priv_check() call or this becomes one giant
4242 #ifndef _SYS_SYSPROTO_H_
4243 struct fhopen_args {
4244 const struct fhandle *u_fhp;
4251 struct fhopen_args /* {
4252 const struct fhandle *u_fhp;
4263 error = priv_check(td, PRIV_VFS_FHOPEN);
4267 fmode = FFLAGS(uap->flags);
4268 /* why not allow a non-read/write open for our lockd? */
4269 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4271 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4274 /* find the mount point */
4275 mp = vfs_busyfs(&fhp.fh_fsid);
4278 /* now give me my vnode, it gets returned to me locked */
4279 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4284 error = falloc_noinstall(td, &fp);
4290 * An extra reference on `fp' has been held for us by
4291 * falloc_noinstall().
4297 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4299 KASSERT(fp->f_ops == &badfileops,
4300 ("VOP_OPEN in fhopen() set f_ops"));
4301 KASSERT(td->td_dupfd < 0,
4302 ("fhopen() encountered fdopen()"));
4312 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4315 if ((fmode & O_TRUNC) != 0) {
4316 error = fo_truncate(fp, 0, td->td_ucred, td);
4321 error = finstall(td, fp, &indx, fmode, NULL);
4324 td->td_retval[0] = indx;
4329 * Stat an (NFS) file handle.
4331 #ifndef _SYS_SYSPROTO_H_
4332 struct fhstat_args {
4333 struct fhandle *u_fhp;
4340 register struct fhstat_args /* {
4341 struct fhandle *u_fhp;
4349 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4352 error = kern_fhstat(td, fh, &sb);
4354 error = copyout(&sb, uap->sb, sizeof(sb));
4359 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4365 error = priv_check(td, PRIV_VFS_FHSTAT);
4368 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4370 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4374 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4380 * Implement fstatfs() for (NFS) file handles.
4382 #ifndef _SYS_SYSPROTO_H_
4383 struct fhstatfs_args {
4384 struct fhandle *u_fhp;
4389 sys_fhstatfs(td, uap)
4391 struct fhstatfs_args /* {
4392 struct fhandle *u_fhp;
4400 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4403 error = kern_fhstatfs(td, fh, &sf);
4406 return (copyout(&sf, uap->buf, sizeof(sf)));
4410 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4417 error = priv_check(td, PRIV_VFS_FHSTATFS);
4420 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4422 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4428 error = prison_canseemount(td->td_ucred, mp);
4432 error = mac_mount_check_stat(td->td_ucred, mp);
4437 * Set these in case the underlying filesystem fails to do so.
4440 sp->f_version = STATFS_VERSION;
4441 sp->f_namemax = NAME_MAX;
4442 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4443 error = VFS_STATFS(mp, sp);
4452 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4457 cap_rights_t rights;
4458 off_t olen, ooffset;
4461 if (offset < 0 || len <= 0)
4463 /* Check for wrap. */
4464 if (offset > OFF_MAX - len)
4466 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4469 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4473 if ((fp->f_flag & FWRITE) == 0) {
4477 if (fp->f_type != DTYPE_VNODE) {
4482 if (vp->v_type != VREG) {
4487 /* Allocating blocks may take a long time, so iterate. */
4494 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4497 error = vn_lock(vp, LK_EXCLUSIVE);
4499 vn_finished_write(mp);
4503 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4506 error = VOP_ALLOCATE(vp, &offset, &len);
4508 vn_finished_write(mp);
4510 if (olen + ooffset != offset + len) {
4511 panic("offset + len changed from %jx/%jx to %jx/%jx",
4512 ooffset, olen, offset, len);
4514 if (error != 0 || len == 0)
4516 KASSERT(olen > len, ("Iteration did not make progress?"));
4525 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4529 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4530 return (kern_posix_error(td, error));
4534 * Unlike madvise(2), we do not make a best effort to remember every
4535 * possible caching hint. Instead, we remember the last setting with
4536 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4537 * region of any current setting.
4540 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4543 struct fadvise_info *fa, *new;
4546 cap_rights_t rights;
4550 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4553 case POSIX_FADV_SEQUENTIAL:
4554 case POSIX_FADV_RANDOM:
4555 case POSIX_FADV_NOREUSE:
4556 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4558 case POSIX_FADV_NORMAL:
4559 case POSIX_FADV_WILLNEED:
4560 case POSIX_FADV_DONTNEED:
4566 /* XXX: CAP_POSIX_FADVISE? */
4567 error = fget(td, fd, cap_rights_init(&rights), &fp);
4570 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4574 if (fp->f_type != DTYPE_VNODE) {
4579 if (vp->v_type != VREG) {
4586 end = offset + len - 1;
4588 case POSIX_FADV_SEQUENTIAL:
4589 case POSIX_FADV_RANDOM:
4590 case POSIX_FADV_NOREUSE:
4592 * Try to merge any existing non-standard region with
4593 * this new region if possible, otherwise create a new
4594 * non-standard region for this request.
4596 mtx_pool_lock(mtxpool_sleep, fp);
4598 if (fa != NULL && fa->fa_advice == advice &&
4599 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4600 (end != OFF_MAX && fa->fa_start == end + 1) ||
4601 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4602 if (offset < fa->fa_start)
4603 fa->fa_start = offset;
4604 if (end > fa->fa_end)
4607 new->fa_advice = advice;
4608 new->fa_start = offset;
4613 mtx_pool_unlock(mtxpool_sleep, fp);
4615 case POSIX_FADV_NORMAL:
4617 * If a the "normal" region overlaps with an existing
4618 * non-standard region, trim or remove the
4619 * non-standard region.
4621 mtx_pool_lock(mtxpool_sleep, fp);
4624 if (offset <= fa->fa_start && end >= fa->fa_end) {
4626 fp->f_advice = NULL;
4627 } else if (offset <= fa->fa_start &&
4628 end >= fa->fa_start)
4629 fa->fa_start = end + 1;
4630 else if (offset <= fa->fa_end && end >= fa->fa_end)
4631 fa->fa_end = offset - 1;
4632 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4634 * If the "normal" region is a middle
4635 * portion of the existing
4636 * non-standard region, just remove
4637 * the whole thing rather than picking
4638 * one side or the other to
4642 fp->f_advice = NULL;
4645 mtx_pool_unlock(mtxpool_sleep, fp);
4647 case POSIX_FADV_WILLNEED:
4648 case POSIX_FADV_DONTNEED:
4649 error = VOP_ADVISE(vp, offset, end, advice);
4655 free(new, M_FADVISE);
4660 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4664 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4666 return (kern_posix_error(td, error));