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 * The module initialization routine for POSIX asynchronous I/O will
110 * set this to the version of AIO that it implements. (Zero means
111 * that it is not implemented.) This value is used here by pathconf()
112 * and in kern_descrip.c by fpathconf().
114 int async_io_version;
117 * Sync each mounted filesystem.
119 #ifndef _SYS_SYSPROTO_H_
128 struct sync_args *uap;
130 struct mount *mp, *nmp;
133 mtx_lock(&mountlist_mtx);
134 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
135 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
136 nmp = TAILQ_NEXT(mp, mnt_list);
139 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
140 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
141 save = curthread_pflags_set(TDP_SYNCIO);
142 vfs_msync(mp, MNT_NOWAIT);
143 VFS_SYNC(mp, MNT_NOWAIT);
144 curthread_pflags_restore(save);
145 vn_finished_write(mp);
147 mtx_lock(&mountlist_mtx);
148 nmp = TAILQ_NEXT(mp, mnt_list);
151 mtx_unlock(&mountlist_mtx);
156 * Change filesystem quotas.
158 #ifndef _SYS_SYSPROTO_H_
159 struct quotactl_args {
167 sys_quotactl(td, uap)
169 register struct quotactl_args /* {
180 AUDIT_ARG_CMD(uap->cmd);
181 AUDIT_ARG_UID(uap->uid);
182 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
184 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
186 if ((error = namei(&nd)) != 0)
188 NDFREE(&nd, NDF_ONLY_PNBUF);
189 mp = nd.ni_vp->v_mount;
192 error = vfs_busy(mp, 0);
196 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
199 * Since quota on operation typically needs to open quota
200 * file, the Q_QUOTAON handler needs to unbusy the mount point
201 * before calling into namei. Otherwise, unmount might be
202 * started between two vfs_busy() invocations (first is our,
203 * second is from mount point cross-walk code in lookup()),
206 * Require that Q_QUOTAON handles the vfs_busy() reference on
207 * its own, always returning with ubusied mount point.
209 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
215 * Used by statfs conversion routines to scale the block size up if
216 * necessary so that all of the block counts are <= 'max_size'. Note
217 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
221 statfs_scale_blocks(struct statfs *sf, long max_size)
226 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
229 * Attempt to scale the block counts to give a more accurate
230 * overview to userland of the ratio of free space to used
231 * space. To do this, find the largest block count and compute
232 * a divisor that lets it fit into a signed integer <= max_size.
234 if (sf->f_bavail < 0)
235 count = -sf->f_bavail;
237 count = sf->f_bavail;
238 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
239 if (count <= max_size)
242 count >>= flsl(max_size);
249 sf->f_bsize <<= shift;
250 sf->f_blocks >>= shift;
251 sf->f_bfree >>= shift;
252 sf->f_bavail >>= shift;
256 * Get filesystem statistics.
258 #ifndef _SYS_SYSPROTO_H_
267 register struct statfs_args /* {
275 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
277 error = copyout(&sf, uap->buf, sizeof(sf));
282 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
286 struct statfs *sp, sb;
290 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
295 mp = nd.ni_vp->v_mount;
297 NDFREE(&nd, NDF_ONLY_PNBUF);
299 error = vfs_busy(mp, 0);
304 error = mac_mount_check_stat(td->td_ucred, mp);
309 * Set these in case the underlying filesystem fails to do so.
312 sp->f_version = STATFS_VERSION;
313 sp->f_namemax = NAME_MAX;
314 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
315 error = VFS_STATFS(mp, sp);
318 if (priv_check(td, PRIV_VFS_GENERATION)) {
319 bcopy(sp, &sb, sizeof(sb));
320 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
321 prison_enforce_statfs(td->td_ucred, mp, &sb);
331 * Get filesystem statistics.
333 #ifndef _SYS_SYSPROTO_H_
334 struct fstatfs_args {
342 register struct fstatfs_args /* {
350 error = kern_fstatfs(td, uap->fd, &sf);
352 error = copyout(&sf, uap->buf, sizeof(sf));
357 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
361 struct statfs *sp, sb;
367 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSTATFS), &fp);
371 vn_lock(vp, LK_SHARED | LK_RETRY);
373 AUDIT_ARG_VNODE1(vp);
384 error = vfs_busy(mp, 0);
389 error = mac_mount_check_stat(td->td_ucred, mp);
394 * Set these in case the underlying filesystem fails to do so.
397 sp->f_version = STATFS_VERSION;
398 sp->f_namemax = NAME_MAX;
399 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
400 error = VFS_STATFS(mp, sp);
403 if (priv_check(td, PRIV_VFS_GENERATION)) {
404 bcopy(sp, &sb, sizeof(sb));
405 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
406 prison_enforce_statfs(td->td_ucred, mp, &sb);
417 * Get statistics on all filesystems.
419 #ifndef _SYS_SYSPROTO_H_
420 struct getfsstat_args {
427 sys_getfsstat(td, uap)
429 register struct getfsstat_args /* {
438 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
439 UIO_USERSPACE, uap->flags);
441 td->td_retval[0] = count;
446 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
447 * The caller is responsible for freeing memory which will be allocated
451 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
452 size_t *countp, enum uio_seg bufseg, int flags)
454 struct mount *mp, *nmp;
455 struct statfs *sfsp, *sp, sb;
456 size_t count, maxcount;
459 maxcount = bufsize / sizeof(struct statfs);
462 else if (bufseg == UIO_USERSPACE)
464 else /* if (bufseg == UIO_SYSSPACE) */ {
466 mtx_lock(&mountlist_mtx);
467 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
470 mtx_unlock(&mountlist_mtx);
471 if (maxcount > count)
473 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
477 mtx_lock(&mountlist_mtx);
478 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
479 if (prison_canseemount(td->td_ucred, mp) != 0) {
480 nmp = TAILQ_NEXT(mp, mnt_list);
484 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
485 nmp = TAILQ_NEXT(mp, mnt_list);
489 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
490 nmp = TAILQ_NEXT(mp, mnt_list);
493 if (sfsp && count < maxcount) {
496 * Set these in case the underlying filesystem
499 sp->f_version = STATFS_VERSION;
500 sp->f_namemax = NAME_MAX;
501 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
503 * If MNT_NOWAIT or MNT_LAZY is specified, do not
504 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
505 * overrides MNT_WAIT.
507 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
508 (flags & MNT_WAIT)) &&
509 (error = VFS_STATFS(mp, sp))) {
510 mtx_lock(&mountlist_mtx);
511 nmp = TAILQ_NEXT(mp, mnt_list);
515 if (priv_check(td, PRIV_VFS_GENERATION)) {
516 bcopy(sp, &sb, sizeof(sb));
517 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
518 prison_enforce_statfs(td->td_ucred, mp, &sb);
521 if (bufseg == UIO_SYSSPACE)
522 bcopy(sp, sfsp, sizeof(*sp));
523 else /* if (bufseg == UIO_USERSPACE) */ {
524 error = copyout(sp, sfsp, sizeof(*sp));
533 mtx_lock(&mountlist_mtx);
534 nmp = TAILQ_NEXT(mp, mnt_list);
537 mtx_unlock(&mountlist_mtx);
538 if (sfsp && count > maxcount)
545 #ifdef COMPAT_FREEBSD4
547 * Get old format filesystem statistics.
549 static void cvtstatfs(struct statfs *, struct ostatfs *);
551 #ifndef _SYS_SYSPROTO_H_
552 struct freebsd4_statfs_args {
558 freebsd4_statfs(td, uap)
560 struct freebsd4_statfs_args /* {
569 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
572 cvtstatfs(&sf, &osb);
573 return (copyout(&osb, uap->buf, sizeof(osb)));
577 * Get filesystem statistics.
579 #ifndef _SYS_SYSPROTO_H_
580 struct freebsd4_fstatfs_args {
586 freebsd4_fstatfs(td, uap)
588 struct freebsd4_fstatfs_args /* {
597 error = kern_fstatfs(td, uap->fd, &sf);
600 cvtstatfs(&sf, &osb);
601 return (copyout(&osb, uap->buf, sizeof(osb)));
605 * Get statistics on all filesystems.
607 #ifndef _SYS_SYSPROTO_H_
608 struct freebsd4_getfsstat_args {
615 freebsd4_getfsstat(td, uap)
617 register struct freebsd4_getfsstat_args /* {
623 struct statfs *buf, *sp;
628 count = uap->bufsize / sizeof(struct ostatfs);
629 size = count * sizeof(struct statfs);
630 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
634 while (count > 0 && error == 0) {
636 error = copyout(&osb, uap->buf, sizeof(osb));
644 td->td_retval[0] = count;
649 * Implement fstatfs() for (NFS) file handles.
651 #ifndef _SYS_SYSPROTO_H_
652 struct freebsd4_fhstatfs_args {
653 struct fhandle *u_fhp;
658 freebsd4_fhstatfs(td, uap)
660 struct freebsd4_fhstatfs_args /* {
661 struct fhandle *u_fhp;
670 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
673 error = kern_fhstatfs(td, fh, &sf);
676 cvtstatfs(&sf, &osb);
677 return (copyout(&osb, uap->buf, sizeof(osb)));
681 * Convert a new format statfs structure to an old format statfs structure.
689 statfs_scale_blocks(nsp, LONG_MAX);
690 bzero(osp, sizeof(*osp));
691 osp->f_bsize = nsp->f_bsize;
692 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
693 osp->f_blocks = nsp->f_blocks;
694 osp->f_bfree = nsp->f_bfree;
695 osp->f_bavail = nsp->f_bavail;
696 osp->f_files = MIN(nsp->f_files, LONG_MAX);
697 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
698 osp->f_owner = nsp->f_owner;
699 osp->f_type = nsp->f_type;
700 osp->f_flags = nsp->f_flags;
701 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
702 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
703 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
704 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
705 strlcpy(osp->f_fstypename, nsp->f_fstypename,
706 MIN(MFSNAMELEN, OMFSNAMELEN));
707 strlcpy(osp->f_mntonname, nsp->f_mntonname,
708 MIN(MNAMELEN, OMNAMELEN));
709 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
710 MIN(MNAMELEN, OMNAMELEN));
711 osp->f_fsid = nsp->f_fsid;
713 #endif /* COMPAT_FREEBSD4 */
716 * Change current working directory to a given file descriptor.
718 #ifndef _SYS_SYSPROTO_H_
726 struct fchdir_args /* {
730 struct vnode *vp, *tdp;
736 AUDIT_ARG_FD(uap->fd);
737 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
744 vn_lock(vp, LK_SHARED | LK_RETRY);
745 AUDIT_ARG_VNODE1(vp);
746 error = change_dir(vp, td);
747 while (!error && (mp = vp->v_mountedhere) != NULL) {
750 error = VFS_ROOT(mp, LK_SHARED, &tdp);
767 * Change current working directory (``.'').
769 #ifndef _SYS_SYSPROTO_H_
777 struct chdir_args /* {
782 return (kern_chdir(td, uap->path, UIO_USERSPACE));
786 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
791 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
793 if ((error = namei(&nd)) != 0)
795 if ((error = change_dir(nd.ni_vp, td)) != 0) {
797 NDFREE(&nd, NDF_ONLY_PNBUF);
800 VOP_UNLOCK(nd.ni_vp, 0);
801 NDFREE(&nd, NDF_ONLY_PNBUF);
802 pwd_chdir(td, nd.ni_vp);
807 * Change notion of root (``/'') directory.
809 #ifndef _SYS_SYSPROTO_H_
817 struct chroot_args /* {
824 error = priv_check(td, PRIV_VFS_CHROOT);
827 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
828 UIO_USERSPACE, uap->path, td);
832 error = change_dir(nd.ni_vp, td);
836 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
840 VOP_UNLOCK(nd.ni_vp, 0);
841 error = pwd_chroot(td, nd.ni_vp);
843 NDFREE(&nd, NDF_ONLY_PNBUF);
848 NDFREE(&nd, NDF_ONLY_PNBUF);
853 * Common routine for chroot and chdir. Callers must provide a locked vnode
865 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
866 if (vp->v_type != VDIR)
869 error = mac_vnode_check_chdir(td->td_ucred, vp);
873 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
877 flags_to_rights(int flags, cap_rights_t *rightsp)
880 if (flags & O_EXEC) {
881 cap_rights_set(rightsp, CAP_FEXECVE);
883 switch ((flags & O_ACCMODE)) {
885 cap_rights_set(rightsp, CAP_READ);
888 cap_rights_set(rightsp, CAP_READ);
891 cap_rights_set(rightsp, CAP_WRITE);
892 if (!(flags & (O_APPEND | O_TRUNC)))
893 cap_rights_set(rightsp, CAP_SEEK);
899 cap_rights_set(rightsp, CAP_CREATE);
902 cap_rights_set(rightsp, CAP_FTRUNCATE);
904 if (flags & (O_SYNC | O_FSYNC))
905 cap_rights_set(rightsp, CAP_FSYNC);
907 if (flags & (O_EXLOCK | O_SHLOCK))
908 cap_rights_set(rightsp, CAP_FLOCK);
912 * Check permissions, allocate an open file structure, and call the device
913 * open routine if any.
915 #ifndef _SYS_SYSPROTO_H_
925 register struct open_args /* {
932 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
933 uap->flags, uap->mode));
936 #ifndef _SYS_SYSPROTO_H_
945 sys_openat(struct thread *td, struct openat_args *uap)
948 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
953 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
956 struct proc *p = td->td_proc;
957 struct filedesc *fdp = p->p_fd;
962 int cmode, error, indx;
966 AUDIT_ARG_FFLAGS(flags);
967 AUDIT_ARG_MODE(mode);
968 /* XXX: audit dirfd */
969 cap_rights_init(&rights, CAP_LOOKUP);
970 flags_to_rights(flags, &rights);
972 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
975 if (flags & O_EXEC) {
976 if (flags & O_ACCMODE)
978 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
981 flags = FFLAGS(flags);
985 * Allocate the file descriptor, but don't install a descriptor yet.
987 error = falloc_noinstall(td, &fp);
991 * An extra reference on `fp' has been held for us by
992 * falloc_noinstall().
994 /* Set the flags early so the finit in devfs can pick them up. */
995 fp->f_flag = flags & FMASK;
996 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
997 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
999 td->td_dupfd = -1; /* XXX check for fdopen */
1000 error = vn_open(&nd, &flags, cmode, fp);
1003 * If the vn_open replaced the method vector, something
1004 * wonderous happened deep below and we just pass it up
1005 * pretending we know what we do.
1007 if (error == ENXIO && fp->f_ops != &badfileops)
1011 * Handle special fdopen() case. bleh.
1013 * Don't do this for relative (capability) lookups; we don't
1014 * understand exactly what would happen, and we don't think
1015 * that it ever should.
1017 if (nd.ni_strictrelative == 0 &&
1018 (error == ENODEV || error == ENXIO) &&
1019 td->td_dupfd >= 0) {
1020 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1029 NDFREE(&nd, NDF_ONLY_PNBUF);
1033 * Store the vnode, for any f_type. Typically, the vnode use
1034 * count is decremented by direct call to vn_closefile() for
1035 * files that switched type in the cdevsw fdopen() method.
1039 * If the file wasn't claimed by devfs bind it to the normal
1040 * vnode operations here.
1042 if (fp->f_ops == &badfileops) {
1043 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1045 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1046 DTYPE_VNODE, vp, &vnops);
1050 if (flags & O_TRUNC) {
1051 error = fo_truncate(fp, 0, td->td_ucred, td);
1057 * If we haven't already installed the FD (for dupfdopen), do so now.
1060 struct filecaps *fcaps;
1063 if (nd.ni_strictrelative == 1)
1064 fcaps = &nd.ni_filecaps;
1068 error = finstall(td, fp, &indx, flags, fcaps);
1069 /* On success finstall() consumes fcaps. */
1071 filecaps_free(&nd.ni_filecaps);
1075 filecaps_free(&nd.ni_filecaps);
1079 * Release our private reference, leaving the one associated with
1080 * the descriptor table intact.
1083 td->td_retval[0] = indx;
1086 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1095 #ifndef _SYS_SYSPROTO_H_
1096 struct ocreat_args {
1104 register struct ocreat_args /* {
1110 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1111 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1113 #endif /* COMPAT_43 */
1116 * Create a special file.
1118 #ifndef _SYS_SYSPROTO_H_
1128 register struct mknod_args /* {
1135 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1136 uap->mode, uap->dev));
1139 #ifndef _SYS_SYSPROTO_H_
1140 struct mknodat_args {
1148 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1151 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1156 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1162 struct nameidata nd;
1163 cap_rights_t rights;
1164 int error, whiteout = 0;
1166 AUDIT_ARG_MODE(mode);
1168 switch (mode & S_IFMT) {
1171 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
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 ost->st_dev = st->st_dev;
2074 ost->st_ino = st->st_ino;
2075 ost->st_mode = st->st_mode;
2076 ost->st_nlink = st->st_nlink;
2077 ost->st_uid = st->st_uid;
2078 ost->st_gid = st->st_gid;
2079 ost->st_rdev = st->st_rdev;
2080 if (st->st_size < (quad_t)1 << 32)
2081 ost->st_size = st->st_size;
2084 ost->st_atim = st->st_atim;
2085 ost->st_mtim = st->st_mtim;
2086 ost->st_ctim = st->st_ctim;
2087 ost->st_blksize = st->st_blksize;
2088 ost->st_blocks = st->st_blocks;
2089 ost->st_flags = st->st_flags;
2090 ost->st_gen = st->st_gen;
2092 #endif /* COMPAT_43 */
2095 * Get file status; this version follows links.
2097 #ifndef _SYS_SYSPROTO_H_
2106 register struct stat_args /* {
2114 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2117 error = copyout(&sb, uap->ub, sizeof (sb));
2121 #ifndef _SYS_SYSPROTO_H_
2122 struct fstatat_args {
2130 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2135 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2136 UIO_USERSPACE, &sb, NULL);
2138 error = copyout(&sb, uap->buf, sizeof (sb));
2143 kern_statat(struct thread *td, int flag, int fd, char *path,
2144 enum uio_seg pathseg, struct stat *sbp,
2145 void (*hook)(struct vnode *vp, struct stat *sbp))
2147 struct nameidata nd;
2149 cap_rights_t rights;
2152 if (flag & ~AT_SYMLINK_NOFOLLOW)
2155 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2156 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2157 cap_rights_init(&rights, CAP_FSTAT), td);
2159 if ((error = namei(&nd)) != 0)
2161 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2163 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2164 if (S_ISREG(sb.st_mode))
2165 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2166 if (__predict_false(hook != NULL))
2167 hook(nd.ni_vp, &sb);
2169 NDFREE(&nd, NDF_ONLY_PNBUF);
2175 if (KTRPOINT(td, KTR_STRUCT))
2182 * Get file status; this version does not follow links.
2184 #ifndef _SYS_SYSPROTO_H_
2193 register struct lstat_args /* {
2201 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2202 UIO_USERSPACE, &sb, NULL);
2204 error = copyout(&sb, uap->ub, sizeof (sb));
2209 * Implementation of the NetBSD [l]stat() functions.
2217 bzero(nsb, sizeof *nsb);
2218 nsb->st_dev = sb->st_dev;
2219 nsb->st_ino = sb->st_ino;
2220 nsb->st_mode = sb->st_mode;
2221 nsb->st_nlink = sb->st_nlink;
2222 nsb->st_uid = sb->st_uid;
2223 nsb->st_gid = sb->st_gid;
2224 nsb->st_rdev = sb->st_rdev;
2225 nsb->st_atim = sb->st_atim;
2226 nsb->st_mtim = sb->st_mtim;
2227 nsb->st_ctim = sb->st_ctim;
2228 nsb->st_size = sb->st_size;
2229 nsb->st_blocks = sb->st_blocks;
2230 nsb->st_blksize = sb->st_blksize;
2231 nsb->st_flags = sb->st_flags;
2232 nsb->st_gen = sb->st_gen;
2233 nsb->st_birthtim = sb->st_birthtim;
2236 #ifndef _SYS_SYSPROTO_H_
2245 register struct nstat_args /* {
2254 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2258 cvtnstat(&sb, &nsb);
2259 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2263 * NetBSD lstat. Get file status; this version does not follow links.
2265 #ifndef _SYS_SYSPROTO_H_
2274 register struct nlstat_args /* {
2283 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2284 UIO_USERSPACE, &sb, NULL);
2287 cvtnstat(&sb, &nsb);
2288 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2292 * Get configurable pathname variables.
2294 #ifndef _SYS_SYSPROTO_H_
2295 struct pathconf_args {
2301 sys_pathconf(td, uap)
2303 register struct pathconf_args /* {
2309 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2312 #ifndef _SYS_SYSPROTO_H_
2313 struct lpathconf_args {
2319 sys_lpathconf(td, uap)
2321 register struct lpathconf_args /* {
2327 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2332 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2335 struct nameidata nd;
2338 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2340 if ((error = namei(&nd)) != 0)
2342 NDFREE(&nd, NDF_ONLY_PNBUF);
2344 /* If asynchronous I/O is available, it works for all files. */
2345 if (name == _PC_ASYNC_IO)
2346 td->td_retval[0] = async_io_version;
2348 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2354 * Return target name of a symbolic link.
2356 #ifndef _SYS_SYSPROTO_H_
2357 struct readlink_args {
2364 sys_readlink(td, uap)
2366 register struct readlink_args /* {
2373 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2374 uap->buf, UIO_USERSPACE, uap->count));
2376 #ifndef _SYS_SYSPROTO_H_
2377 struct readlinkat_args {
2385 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2388 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2389 uap->buf, UIO_USERSPACE, uap->bufsize));
2393 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2394 char *buf, enum uio_seg bufseg, size_t count)
2399 struct nameidata nd;
2402 if (count > IOSIZE_MAX)
2405 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2406 pathseg, path, fd, td);
2408 if ((error = namei(&nd)) != 0)
2410 NDFREE(&nd, NDF_ONLY_PNBUF);
2413 error = mac_vnode_check_readlink(td->td_ucred, vp);
2419 if (vp->v_type != VLNK)
2422 aiov.iov_base = buf;
2423 aiov.iov_len = count;
2424 auio.uio_iov = &aiov;
2425 auio.uio_iovcnt = 1;
2426 auio.uio_offset = 0;
2427 auio.uio_rw = UIO_READ;
2428 auio.uio_segflg = bufseg;
2430 auio.uio_resid = count;
2431 error = VOP_READLINK(vp, &auio, td->td_ucred);
2432 td->td_retval[0] = count - auio.uio_resid;
2439 * Common implementation code for chflags() and fchflags().
2442 setfflags(td, vp, flags)
2451 /* We can't support the value matching VNOVAL. */
2452 if (flags == VNOVAL)
2453 return (EOPNOTSUPP);
2456 * Prevent non-root users from setting flags on devices. When
2457 * a device is reused, users can retain ownership of the device
2458 * if they are allowed to set flags and programs assume that
2459 * chown can't fail when done as root.
2461 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2462 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2467 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2470 vattr.va_flags = flags;
2471 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2473 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2476 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2478 vn_finished_write(mp);
2483 * Change flags of a file given a path name.
2485 #ifndef _SYS_SYSPROTO_H_
2486 struct chflags_args {
2492 sys_chflags(td, uap)
2494 register struct chflags_args /* {
2500 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2504 #ifndef _SYS_SYSPROTO_H_
2505 struct chflagsat_args {
2513 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2516 const char *path = uap->path;
2517 u_long flags = uap->flags;
2518 int atflag = uap->atflag;
2520 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2523 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2527 * Same as chflags() but doesn't follow symlinks.
2530 sys_lchflags(td, uap)
2532 register struct lchflags_args /* {
2538 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2539 uap->flags, AT_SYMLINK_NOFOLLOW));
2543 kern_chflagsat(struct thread *td, int fd, const char *path,
2544 enum uio_seg pathseg, u_long flags, int atflag)
2546 struct nameidata nd;
2547 cap_rights_t rights;
2550 AUDIT_ARG_FFLAGS(flags);
2551 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2552 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2553 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2554 if ((error = namei(&nd)) != 0)
2556 NDFREE(&nd, NDF_ONLY_PNBUF);
2557 error = setfflags(td, nd.ni_vp, flags);
2563 * Change flags of a file given a file descriptor.
2565 #ifndef _SYS_SYSPROTO_H_
2566 struct fchflags_args {
2572 sys_fchflags(td, uap)
2574 register struct fchflags_args /* {
2580 cap_rights_t rights;
2583 AUDIT_ARG_FD(uap->fd);
2584 AUDIT_ARG_FFLAGS(uap->flags);
2585 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2590 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2591 AUDIT_ARG_VNODE1(fp->f_vnode);
2592 VOP_UNLOCK(fp->f_vnode, 0);
2594 error = setfflags(td, fp->f_vnode, uap->flags);
2600 * Common implementation code for chmod(), lchmod() and fchmod().
2603 setfmode(td, cred, vp, mode)
2613 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2615 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2617 vattr.va_mode = mode & ALLPERMS;
2619 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2622 error = VOP_SETATTR(vp, &vattr, cred);
2624 vn_finished_write(mp);
2629 * Change mode of a file given path name.
2631 #ifndef _SYS_SYSPROTO_H_
2640 register struct chmod_args /* {
2646 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2650 #ifndef _SYS_SYSPROTO_H_
2651 struct fchmodat_args {
2659 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2661 int flag = uap->flag;
2663 char *path = uap->path;
2664 mode_t mode = uap->mode;
2666 if (flag & ~AT_SYMLINK_NOFOLLOW)
2669 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2673 * Change mode of a file given path name (don't follow links.)
2675 #ifndef _SYS_SYSPROTO_H_
2676 struct lchmod_args {
2684 register struct lchmod_args /* {
2690 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2691 uap->mode, AT_SYMLINK_NOFOLLOW));
2695 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2696 mode_t mode, int flag)
2698 struct nameidata nd;
2699 cap_rights_t rights;
2702 AUDIT_ARG_MODE(mode);
2703 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2704 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2705 cap_rights_init(&rights, CAP_FCHMOD), td);
2706 if ((error = namei(&nd)) != 0)
2708 NDFREE(&nd, NDF_ONLY_PNBUF);
2709 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2715 * Change mode of a file given a file descriptor.
2717 #ifndef _SYS_SYSPROTO_H_
2718 struct fchmod_args {
2724 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2727 cap_rights_t rights;
2730 AUDIT_ARG_FD(uap->fd);
2731 AUDIT_ARG_MODE(uap->mode);
2733 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2736 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2742 * Common implementation for chown(), lchown(), and fchown()
2745 setfown(td, cred, vp, uid, gid)
2756 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2758 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2763 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2767 error = VOP_SETATTR(vp, &vattr, cred);
2769 vn_finished_write(mp);
2774 * Set ownership given a path name.
2776 #ifndef _SYS_SYSPROTO_H_
2786 register struct chown_args /* {
2793 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2797 #ifndef _SYS_SYSPROTO_H_
2798 struct fchownat_args {
2807 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2812 if (flag & ~AT_SYMLINK_NOFOLLOW)
2815 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2816 uap->gid, uap->flag));
2820 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2821 int uid, int gid, int flag)
2823 struct nameidata nd;
2824 cap_rights_t rights;
2827 AUDIT_ARG_OWNER(uid, gid);
2828 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2829 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2830 cap_rights_init(&rights, CAP_FCHOWN), td);
2832 if ((error = namei(&nd)) != 0)
2834 NDFREE(&nd, NDF_ONLY_PNBUF);
2835 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2841 * Set ownership given a path name, do not cross symlinks.
2843 #ifndef _SYS_SYSPROTO_H_
2844 struct lchown_args {
2853 register struct lchown_args /* {
2860 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2861 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2865 * Set ownership given a file descriptor.
2867 #ifndef _SYS_SYSPROTO_H_
2868 struct fchown_args {
2877 register struct fchown_args /* {
2884 cap_rights_t rights;
2887 AUDIT_ARG_FD(uap->fd);
2888 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2889 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2892 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2898 * Common implementation code for utimes(), lutimes(), and futimes().
2901 getutimes(usrtvp, tvpseg, tsp)
2902 const struct timeval *usrtvp;
2903 enum uio_seg tvpseg;
2904 struct timespec *tsp;
2906 struct timeval tv[2];
2907 const struct timeval *tvp;
2910 if (usrtvp == NULL) {
2911 vfs_timestamp(&tsp[0]);
2914 if (tvpseg == UIO_SYSSPACE) {
2917 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2922 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2923 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2925 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2926 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2932 * Common implementation code for futimens(), utimensat().
2934 #define UTIMENS_NULL 0x1
2935 #define UTIMENS_EXIT 0x2
2937 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2938 struct timespec *tsp, int *retflags)
2940 struct timespec tsnow;
2943 vfs_timestamp(&tsnow);
2945 if (usrtsp == NULL) {
2948 *retflags |= UTIMENS_NULL;
2951 if (tspseg == UIO_SYSSPACE) {
2954 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2956 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2957 *retflags |= UTIMENS_EXIT;
2958 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2959 *retflags |= UTIMENS_NULL;
2960 if (tsp[0].tv_nsec == UTIME_OMIT)
2961 tsp[0].tv_sec = VNOVAL;
2962 else if (tsp[0].tv_nsec == UTIME_NOW)
2964 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2966 if (tsp[1].tv_nsec == UTIME_OMIT)
2967 tsp[1].tv_sec = VNOVAL;
2968 else if (tsp[1].tv_nsec == UTIME_NOW)
2970 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2977 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2981 setutimes(td, vp, ts, numtimes, nullflag)
2984 const struct timespec *ts;
2990 int error, setbirthtime;
2992 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2994 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2996 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
2997 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3000 vattr.va_atime = ts[0];
3001 vattr.va_mtime = ts[1];
3003 vattr.va_birthtime = ts[1];
3005 vattr.va_birthtime = ts[2];
3007 vattr.va_vaflags |= VA_UTIMES_NULL;
3009 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3013 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3015 vn_finished_write(mp);
3020 * Set the access and modification times of a file.
3022 #ifndef _SYS_SYSPROTO_H_
3023 struct utimes_args {
3025 struct timeval *tptr;
3031 register struct utimes_args /* {
3033 struct timeval *tptr;
3037 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3038 uap->tptr, UIO_USERSPACE));
3041 #ifndef _SYS_SYSPROTO_H_
3042 struct futimesat_args {
3045 const struct timeval * times;
3049 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3052 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3053 uap->times, UIO_USERSPACE));
3057 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3058 struct timeval *tptr, enum uio_seg tptrseg)
3060 struct nameidata nd;
3061 struct timespec ts[2];
3062 cap_rights_t rights;
3065 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3067 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3068 cap_rights_init(&rights, CAP_FUTIMES), td);
3070 if ((error = namei(&nd)) != 0)
3072 NDFREE(&nd, NDF_ONLY_PNBUF);
3073 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3079 * Set the access and modification times of a file.
3081 #ifndef _SYS_SYSPROTO_H_
3082 struct lutimes_args {
3084 struct timeval *tptr;
3088 sys_lutimes(td, uap)
3090 register struct lutimes_args /* {
3092 struct timeval *tptr;
3096 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3101 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3102 struct timeval *tptr, enum uio_seg tptrseg)
3104 struct timespec ts[2];
3105 struct nameidata nd;
3108 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3110 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3111 if ((error = namei(&nd)) != 0)
3113 NDFREE(&nd, NDF_ONLY_PNBUF);
3114 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3120 * Set the access and modification times of a file.
3122 #ifndef _SYS_SYSPROTO_H_
3123 struct futimes_args {
3125 struct timeval *tptr;
3129 sys_futimes(td, uap)
3131 register struct futimes_args /* {
3133 struct timeval *tptr;
3137 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3141 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3142 enum uio_seg tptrseg)
3144 struct timespec ts[2];
3146 cap_rights_t rights;
3150 error = getutimes(tptr, tptrseg, ts);
3153 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3157 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3158 AUDIT_ARG_VNODE1(fp->f_vnode);
3159 VOP_UNLOCK(fp->f_vnode, 0);
3161 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3167 sys_futimens(struct thread *td, struct futimens_args *uap)
3170 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3174 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3175 enum uio_seg tptrseg)
3177 struct timespec ts[2];
3179 cap_rights_t rights;
3183 error = getutimens(tptr, tptrseg, ts, &flags);
3186 if (flags & UTIMENS_EXIT)
3188 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3192 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3193 AUDIT_ARG_VNODE1(fp->f_vnode);
3194 VOP_UNLOCK(fp->f_vnode, 0);
3196 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3202 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3205 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3206 uap->times, UIO_USERSPACE, uap->flag));
3210 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3211 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3213 struct nameidata nd;
3214 struct timespec ts[2];
3215 cap_rights_t rights;
3218 if (flag & ~AT_SYMLINK_NOFOLLOW)
3221 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3223 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3224 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3225 cap_rights_init(&rights, CAP_FUTIMES), td);
3226 if ((error = namei(&nd)) != 0)
3229 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3231 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3232 * "Search permission is denied by a component of the path prefix."
3234 NDFREE(&nd, NDF_ONLY_PNBUF);
3235 if ((flags & UTIMENS_EXIT) == 0)
3236 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3242 * Truncate a file given its path name.
3244 #ifndef _SYS_SYSPROTO_H_
3245 struct truncate_args {
3252 sys_truncate(td, uap)
3254 register struct truncate_args /* {
3261 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3265 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3271 struct nameidata nd;
3276 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3277 if ((error = namei(&nd)) != 0)
3280 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3281 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3282 vn_rangelock_unlock(vp, rl_cookie);
3286 NDFREE(&nd, NDF_ONLY_PNBUF);
3287 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3288 if (vp->v_type == VDIR)
3291 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3294 else if ((error = vn_writechk(vp)) == 0 &&
3295 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3297 vattr.va_size = length;
3298 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3301 vn_finished_write(mp);
3302 vn_rangelock_unlock(vp, rl_cookie);
3307 #if defined(COMPAT_43)
3309 * Truncate a file given its path name.
3311 #ifndef _SYS_SYSPROTO_H_
3312 struct otruncate_args {
3320 register struct otruncate_args /* {
3325 struct truncate_args /* {
3331 nuap.path = uap->path;
3332 nuap.length = uap->length;
3333 return (sys_truncate(td, &nuap));
3335 #endif /* COMPAT_43 */
3337 #if defined(COMPAT_FREEBSD6)
3338 /* Versions with the pad argument */
3340 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3342 struct truncate_args ouap;
3344 ouap.path = uap->path;
3345 ouap.length = uap->length;
3346 return (sys_truncate(td, &ouap));
3350 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3352 struct ftruncate_args ouap;
3355 ouap.length = uap->length;
3356 return (sys_ftruncate(td, &ouap));
3361 * Sync an open file.
3363 #ifndef _SYS_SYSPROTO_H_
3371 struct fsync_args /* {
3378 cap_rights_t rights;
3379 int error, lock_flags;
3381 AUDIT_ARG_FD(uap->fd);
3382 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3386 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3389 if (MNT_SHARED_WRITES(mp) ||
3390 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3391 lock_flags = LK_SHARED;
3393 lock_flags = LK_EXCLUSIVE;
3395 vn_lock(vp, lock_flags | LK_RETRY);
3396 AUDIT_ARG_VNODE1(vp);
3397 if (vp->v_object != NULL) {
3398 VM_OBJECT_WLOCK(vp->v_object);
3399 vm_object_page_clean(vp->v_object, 0, 0, 0);
3400 VM_OBJECT_WUNLOCK(vp->v_object);
3402 error = VOP_FSYNC(vp, MNT_WAIT, td);
3405 vn_finished_write(mp);
3412 * Rename files. Source and destination must either both be directories, or
3413 * both not be directories. If target is a directory, it must be empty.
3415 #ifndef _SYS_SYSPROTO_H_
3416 struct rename_args {
3424 register struct rename_args /* {
3430 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3431 uap->to, UIO_USERSPACE));
3434 #ifndef _SYS_SYSPROTO_H_
3435 struct renameat_args {
3443 sys_renameat(struct thread *td, struct renameat_args *uap)
3446 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3451 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3452 enum uio_seg pathseg)
3454 struct mount *mp = NULL;
3455 struct vnode *tvp, *fvp, *tdvp;
3456 struct nameidata fromnd, tond;
3457 cap_rights_t rights;
3463 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3464 AUDITVNODE1, pathseg, old, oldfd,
3465 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3467 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3468 pathseg, old, oldfd,
3469 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3472 if ((error = namei(&fromnd)) != 0)
3475 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3476 fromnd.ni_vp, &fromnd.ni_cnd);
3477 VOP_UNLOCK(fromnd.ni_dvp, 0);
3478 if (fromnd.ni_dvp != fromnd.ni_vp)
3479 VOP_UNLOCK(fromnd.ni_vp, 0);
3482 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3483 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3484 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3485 if (fromnd.ni_vp->v_type == VDIR)
3486 tond.ni_cnd.cn_flags |= WILLBEDIR;
3487 if ((error = namei(&tond)) != 0) {
3488 /* Translate error code for rename("dir1", "dir2/."). */
3489 if (error == EISDIR && fvp->v_type == VDIR)
3491 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3492 vrele(fromnd.ni_dvp);
3498 error = vn_start_write(fvp, &mp, V_NOWAIT);
3500 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3501 NDFREE(&tond, NDF_ONLY_PNBUF);
3508 vrele(fromnd.ni_dvp);
3510 vrele(tond.ni_startdir);
3511 if (fromnd.ni_startdir != NULL)
3512 vrele(fromnd.ni_startdir);
3513 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3519 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3522 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3527 if (newfd != AT_FDCWD) {
3529 * If the target already exists we require CAP_UNLINKAT
3532 error = cap_check(&tond.ni_filecaps.fc_rights,
3533 cap_rights_init(&rights, CAP_UNLINKAT));
3544 * If the source is the same as the destination (that is, if they
3545 * are links to the same vnode), then there is nothing to do.
3551 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3552 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3556 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3557 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3558 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3559 NDFREE(&tond, NDF_ONLY_PNBUF);
3561 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3562 NDFREE(&tond, NDF_ONLY_PNBUF);
3569 vrele(fromnd.ni_dvp);
3572 vrele(tond.ni_startdir);
3573 vn_finished_write(mp);
3575 if (fromnd.ni_startdir)
3576 vrele(fromnd.ni_startdir);
3583 * Make a directory file.
3585 #ifndef _SYS_SYSPROTO_H_
3594 register struct mkdir_args /* {
3600 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3604 #ifndef _SYS_SYSPROTO_H_
3605 struct mkdirat_args {
3612 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3615 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3619 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3625 struct nameidata nd;
3626 cap_rights_t rights;
3629 AUDIT_ARG_MODE(mode);
3632 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3633 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3635 nd.ni_cnd.cn_flags |= WILLBEDIR;
3636 if ((error = namei(&nd)) != 0)
3640 NDFREE(&nd, NDF_ONLY_PNBUF);
3642 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3643 * the strange behaviour of leaving the vnode unlocked
3644 * if the target is the same vnode as the parent.
3646 if (vp == nd.ni_dvp)
3653 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3654 NDFREE(&nd, NDF_ONLY_PNBUF);
3656 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3661 vattr.va_type = VDIR;
3662 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3664 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3669 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3673 NDFREE(&nd, NDF_ONLY_PNBUF);
3677 vn_finished_write(mp);
3682 * Remove a directory file.
3684 #ifndef _SYS_SYSPROTO_H_
3692 struct rmdir_args /* {
3697 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3701 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3705 struct nameidata nd;
3706 cap_rights_t rights;
3711 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3712 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3713 if ((error = namei(&nd)) != 0)
3716 if (vp->v_type != VDIR) {
3721 * No rmdir "." please.
3723 if (nd.ni_dvp == vp) {
3728 * The root of a mounted filesystem cannot be deleted.
3730 if (vp->v_vflag & VV_ROOT) {
3735 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3740 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3741 NDFREE(&nd, NDF_ONLY_PNBUF);
3743 if (nd.ni_dvp == vp)
3747 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3751 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3752 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3753 vn_finished_write(mp);
3755 NDFREE(&nd, NDF_ONLY_PNBUF);
3757 if (nd.ni_dvp == vp)
3766 * Read a block of directory entries in a filesystem independent format.
3768 #ifndef _SYS_SYSPROTO_H_
3769 struct ogetdirentries_args {
3777 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3782 error = kern_ogetdirentries(td, uap, &loff);
3784 error = copyout(&loff, uap->basep, sizeof(long));
3789 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3794 struct uio auio, kuio;
3795 struct iovec aiov, kiov;
3796 struct dirent *dp, *edp;
3797 cap_rights_t rights;
3799 int error, eofflag, readcnt;
3803 /* XXX arbitrary sanity limit on `count'. */
3804 if (uap->count > 64 * 1024)
3806 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp);
3809 if ((fp->f_flag & FREAD) == 0) {
3814 foffset = foffset_lock(fp, 0);
3816 if (vp->v_type != VDIR) {
3817 foffset_unlock(fp, foffset, 0);
3821 aiov.iov_base = uap->buf;
3822 aiov.iov_len = uap->count;
3823 auio.uio_iov = &aiov;
3824 auio.uio_iovcnt = 1;
3825 auio.uio_rw = UIO_READ;
3826 auio.uio_segflg = UIO_USERSPACE;
3828 auio.uio_resid = uap->count;
3829 vn_lock(vp, LK_SHARED | LK_RETRY);
3830 loff = auio.uio_offset = foffset;
3832 error = mac_vnode_check_readdir(td->td_ucred, vp);
3835 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3840 # if (BYTE_ORDER != LITTLE_ENDIAN)
3841 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3842 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3844 foffset = auio.uio_offset;
3849 kuio.uio_iov = &kiov;
3850 kuio.uio_segflg = UIO_SYSSPACE;
3851 kiov.iov_len = uap->count;
3852 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3853 kiov.iov_base = dirbuf;
3854 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3856 foffset = kuio.uio_offset;
3858 readcnt = uap->count - kuio.uio_resid;
3859 edp = (struct dirent *)&dirbuf[readcnt];
3860 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3861 # if (BYTE_ORDER == LITTLE_ENDIAN)
3863 * The expected low byte of
3864 * dp->d_namlen is our dp->d_type.
3865 * The high MBZ byte of dp->d_namlen
3866 * is our dp->d_namlen.
3868 dp->d_type = dp->d_namlen;
3872 * The dp->d_type is the high byte
3873 * of the expected dp->d_namlen,
3874 * so must be zero'ed.
3878 if (dp->d_reclen > 0) {
3879 dp = (struct dirent *)
3880 ((char *)dp + dp->d_reclen);
3887 error = uiomove(dirbuf, readcnt, &auio);
3889 free(dirbuf, M_TEMP);
3893 foffset_unlock(fp, foffset, 0);
3897 if (uap->count == auio.uio_resid &&
3898 (vp->v_vflag & VV_ROOT) &&
3899 (vp->v_mount->mnt_flag & MNT_UNION)) {
3900 struct vnode *tvp = vp;
3901 vp = vp->v_mount->mnt_vnodecovered;
3910 foffset_unlock(fp, foffset, 0);
3912 td->td_retval[0] = uap->count - auio.uio_resid;
3917 #endif /* COMPAT_43 */
3920 * Read a block of directory entries in a filesystem independent format.
3922 #ifndef _SYS_SYSPROTO_H_
3923 struct getdirentries_args {
3931 sys_getdirentries(td, uap)
3933 register struct getdirentries_args /* {
3943 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3944 NULL, UIO_USERSPACE);
3947 if (uap->basep != NULL)
3948 error = copyout(&base, uap->basep, sizeof(long));
3953 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3954 long *basep, ssize_t *residp, enum uio_seg bufseg)
3960 cap_rights_t rights;
3966 if (count > IOSIZE_MAX)
3968 auio.uio_resid = count;
3969 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3972 if ((fp->f_flag & FREAD) == 0) {
3977 foffset = foffset_lock(fp, 0);
3979 if (vp->v_type != VDIR) {
3983 aiov.iov_base = buf;
3984 aiov.iov_len = count;
3985 auio.uio_iov = &aiov;
3986 auio.uio_iovcnt = 1;
3987 auio.uio_rw = UIO_READ;
3988 auio.uio_segflg = bufseg;
3990 vn_lock(vp, LK_SHARED | LK_RETRY);
3991 AUDIT_ARG_VNODE1(vp);
3992 loff = auio.uio_offset = foffset;
3994 error = mac_vnode_check_readdir(td->td_ucred, vp);
3997 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3999 foffset = auio.uio_offset;
4004 if (count == auio.uio_resid &&
4005 (vp->v_vflag & VV_ROOT) &&
4006 (vp->v_mount->mnt_flag & MNT_UNION)) {
4007 struct vnode *tvp = vp;
4009 vp = vp->v_mount->mnt_vnodecovered;
4020 *residp = auio.uio_resid;
4021 td->td_retval[0] = count - auio.uio_resid;
4023 foffset_unlock(fp, foffset, 0);
4028 #ifndef _SYS_SYSPROTO_H_
4029 struct getdents_args {
4036 sys_getdents(td, uap)
4038 register struct getdents_args /* {
4044 struct getdirentries_args ap;
4048 ap.count = uap->count;
4050 return (sys_getdirentries(td, &ap));
4054 * Set the mode mask for creation of filesystem nodes.
4056 #ifndef _SYS_SYSPROTO_H_
4064 struct umask_args /* {
4068 struct filedesc *fdp;
4070 fdp = td->td_proc->p_fd;
4071 FILEDESC_XLOCK(fdp);
4072 td->td_retval[0] = fdp->fd_cmask;
4073 fdp->fd_cmask = uap->newmask & ALLPERMS;
4074 FILEDESC_XUNLOCK(fdp);
4079 * Void all references to file by ripping underlying filesystem away from
4082 #ifndef _SYS_SYSPROTO_H_
4083 struct revoke_args {
4090 register struct revoke_args /* {
4096 struct nameidata nd;
4099 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4101 if ((error = namei(&nd)) != 0)
4104 NDFREE(&nd, NDF_ONLY_PNBUF);
4105 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4110 error = mac_vnode_check_revoke(td->td_ucred, vp);
4114 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4117 if (td->td_ucred->cr_uid != vattr.va_uid) {
4118 error = priv_check(td, PRIV_VFS_ADMIN);
4123 VOP_REVOKE(vp, REVOKEALL);
4130 * Convert a user file descriptor to a kernel file entry and check that, if it
4131 * is a capability, the correct rights are present. A reference on the file
4132 * entry is held upon returning.
4135 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4140 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4145 * The file could be not of the vnode type, or it may be not
4146 * yet fully initialized, in which case the f_vnode pointer
4147 * may be set, but f_ops is still badfileops. E.g.,
4148 * devfs_open() transiently create such situation to
4149 * facilitate csw d_fdopen().
4151 * Dupfdopen() handling in kern_openat() installs the
4152 * half-baked file into the process descriptor table, allowing
4153 * other thread to dereference it. Guard against the race by
4156 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4166 * Get an (NFS) file handle.
4168 #ifndef _SYS_SYSPROTO_H_
4169 struct lgetfh_args {
4177 register struct lgetfh_args *uap;
4179 struct nameidata nd;
4181 register struct vnode *vp;
4184 error = priv_check(td, PRIV_VFS_GETFH);
4187 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4192 NDFREE(&nd, NDF_ONLY_PNBUF);
4194 bzero(&fh, sizeof(fh));
4195 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4196 error = VOP_VPTOFH(vp, &fh.fh_fid);
4199 error = copyout(&fh, uap->fhp, sizeof (fh));
4203 #ifndef _SYS_SYSPROTO_H_
4212 register struct getfh_args *uap;
4214 struct nameidata nd;
4216 register struct vnode *vp;
4219 error = priv_check(td, PRIV_VFS_GETFH);
4222 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4227 NDFREE(&nd, NDF_ONLY_PNBUF);
4229 bzero(&fh, sizeof(fh));
4230 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4231 error = VOP_VPTOFH(vp, &fh.fh_fid);
4234 error = copyout(&fh, uap->fhp, sizeof (fh));
4239 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4242 * warning: do not remove the priv_check() call or this becomes one giant
4245 #ifndef _SYS_SYSPROTO_H_
4246 struct fhopen_args {
4247 const struct fhandle *u_fhp;
4254 struct fhopen_args /* {
4255 const struct fhandle *u_fhp;
4266 error = priv_check(td, PRIV_VFS_FHOPEN);
4270 fmode = FFLAGS(uap->flags);
4271 /* why not allow a non-read/write open for our lockd? */
4272 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4274 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4277 /* find the mount point */
4278 mp = vfs_busyfs(&fhp.fh_fsid);
4281 /* now give me my vnode, it gets returned to me locked */
4282 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4287 error = falloc_noinstall(td, &fp);
4293 * An extra reference on `fp' has been held for us by
4294 * falloc_noinstall().
4300 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4302 KASSERT(fp->f_ops == &badfileops,
4303 ("VOP_OPEN in fhopen() set f_ops"));
4304 KASSERT(td->td_dupfd < 0,
4305 ("fhopen() encountered fdopen()"));
4315 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4318 if ((fmode & O_TRUNC) != 0) {
4319 error = fo_truncate(fp, 0, td->td_ucred, td);
4324 error = finstall(td, fp, &indx, fmode, NULL);
4327 td->td_retval[0] = indx;
4332 * Stat an (NFS) file handle.
4334 #ifndef _SYS_SYSPROTO_H_
4335 struct fhstat_args {
4336 struct fhandle *u_fhp;
4343 register struct fhstat_args /* {
4344 struct fhandle *u_fhp;
4352 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4355 error = kern_fhstat(td, fh, &sb);
4357 error = copyout(&sb, uap->sb, sizeof(sb));
4362 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4368 error = priv_check(td, PRIV_VFS_FHSTAT);
4371 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4373 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4377 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4383 * Implement fstatfs() for (NFS) file handles.
4385 #ifndef _SYS_SYSPROTO_H_
4386 struct fhstatfs_args {
4387 struct fhandle *u_fhp;
4392 sys_fhstatfs(td, uap)
4394 struct fhstatfs_args /* {
4395 struct fhandle *u_fhp;
4403 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4406 error = kern_fhstatfs(td, fh, &sf);
4409 return (copyout(&sf, uap->buf, sizeof(sf)));
4413 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4420 error = priv_check(td, PRIV_VFS_FHSTATFS);
4423 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4425 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4431 error = prison_canseemount(td->td_ucred, mp);
4435 error = mac_mount_check_stat(td->td_ucred, mp);
4440 * Set these in case the underlying filesystem fails to do so.
4443 sp->f_version = STATFS_VERSION;
4444 sp->f_namemax = NAME_MAX;
4445 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4446 error = VFS_STATFS(mp, sp);
4455 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4460 cap_rights_t rights;
4461 off_t olen, ooffset;
4464 if (offset < 0 || len <= 0)
4466 /* Check for wrap. */
4467 if (offset > OFF_MAX - len)
4469 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4472 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4476 if ((fp->f_flag & FWRITE) == 0) {
4480 if (fp->f_type != DTYPE_VNODE) {
4485 if (vp->v_type != VREG) {
4490 /* Allocating blocks may take a long time, so iterate. */
4497 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4500 error = vn_lock(vp, LK_EXCLUSIVE);
4502 vn_finished_write(mp);
4506 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4509 error = VOP_ALLOCATE(vp, &offset, &len);
4511 vn_finished_write(mp);
4513 if (olen + ooffset != offset + len) {
4514 panic("offset + len changed from %jx/%jx to %jx/%jx",
4515 ooffset, olen, offset, len);
4517 if (error != 0 || len == 0)
4519 KASSERT(olen > len, ("Iteration did not make progress?"));
4528 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4531 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4537 * Unlike madvise(2), we do not make a best effort to remember every
4538 * possible caching hint. Instead, we remember the last setting with
4539 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4540 * region of any current setting.
4543 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4546 struct fadvise_info *fa, *new;
4549 cap_rights_t rights;
4553 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4556 case POSIX_FADV_SEQUENTIAL:
4557 case POSIX_FADV_RANDOM:
4558 case POSIX_FADV_NOREUSE:
4559 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4561 case POSIX_FADV_NORMAL:
4562 case POSIX_FADV_WILLNEED:
4563 case POSIX_FADV_DONTNEED:
4569 /* XXX: CAP_POSIX_FADVISE? */
4570 error = fget(td, fd, cap_rights_init(&rights), &fp);
4573 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4577 if (fp->f_type != DTYPE_VNODE) {
4582 if (vp->v_type != VREG) {
4589 end = offset + len - 1;
4591 case POSIX_FADV_SEQUENTIAL:
4592 case POSIX_FADV_RANDOM:
4593 case POSIX_FADV_NOREUSE:
4595 * Try to merge any existing non-standard region with
4596 * this new region if possible, otherwise create a new
4597 * non-standard region for this request.
4599 mtx_pool_lock(mtxpool_sleep, fp);
4601 if (fa != NULL && fa->fa_advice == advice &&
4602 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4603 (end != OFF_MAX && fa->fa_start == end + 1) ||
4604 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4605 if (offset < fa->fa_start)
4606 fa->fa_start = offset;
4607 if (end > fa->fa_end)
4610 new->fa_advice = advice;
4611 new->fa_start = offset;
4616 mtx_pool_unlock(mtxpool_sleep, fp);
4618 case POSIX_FADV_NORMAL:
4620 * If a the "normal" region overlaps with an existing
4621 * non-standard region, trim or remove the
4622 * non-standard region.
4624 mtx_pool_lock(mtxpool_sleep, fp);
4627 if (offset <= fa->fa_start && end >= fa->fa_end) {
4629 fp->f_advice = NULL;
4630 } else if (offset <= fa->fa_start &&
4631 end >= fa->fa_start)
4632 fa->fa_start = end + 1;
4633 else if (offset <= fa->fa_end && end >= fa->fa_end)
4634 fa->fa_end = offset - 1;
4635 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4637 * If the "normal" region is a middle
4638 * portion of the existing
4639 * non-standard region, just remove
4640 * the whole thing rather than picking
4641 * one side or the other to
4645 fp->f_advice = NULL;
4648 mtx_pool_unlock(mtxpool_sleep, fp);
4650 case POSIX_FADV_WILLNEED:
4651 case POSIX_FADV_DONTNEED:
4652 error = VOP_ADVISE(vp, offset, end, advice);
4658 free(new, M_FADVISE);
4663 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4666 td->td_retval[0] = kern_posix_fadvise(td, uap->fd, uap->offset,
4667 uap->len, uap->advice);