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_AT(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1, td);
1446 if ((error = namei(&nd)) != 0)
1448 NDFREE(&nd, NDF_ONLY_PNBUF);
1450 if (vp->v_type == VDIR) {
1452 return (EPERM); /* POSIX */
1454 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE2 |
1455 NOCACHE, segflg, path2, fd2, cap_rights_init(&rights, CAP_LINKAT),
1457 if ((error = namei(&nd)) == 0) {
1458 if (nd.ni_vp != NULL) {
1459 NDFREE(&nd, NDF_ONLY_PNBUF);
1460 if (nd.ni_dvp == nd.ni_vp)
1467 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1469 * Cross-device link. No need to recheck
1470 * vp->v_type, since it cannot change, except
1473 NDFREE(&nd, NDF_ONLY_PNBUF);
1477 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1478 error = can_hardlink(vp, td->td_ucred);
1481 error = mac_vnode_check_link(td->td_ucred,
1482 nd.ni_dvp, vp, &nd.ni_cnd);
1487 NDFREE(&nd, NDF_ONLY_PNBUF);
1490 error = vn_start_write(vp, &mp, V_NOWAIT);
1494 NDFREE(&nd, NDF_ONLY_PNBUF);
1495 error = vn_start_write(NULL, &mp,
1501 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1504 vn_finished_write(mp);
1505 NDFREE(&nd, NDF_ONLY_PNBUF);
1508 NDFREE(&nd, NDF_ONLY_PNBUF);
1518 * Make a symbolic link.
1520 #ifndef _SYS_SYSPROTO_H_
1521 struct symlink_args {
1527 sys_symlink(td, uap)
1529 register struct symlink_args /* {
1535 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1539 #ifndef _SYS_SYSPROTO_H_
1540 struct symlinkat_args {
1547 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1550 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1555 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1556 enum uio_seg segflg)
1561 struct nameidata nd;
1563 cap_rights_t rights;
1565 if (segflg == UIO_SYSSPACE) {
1568 syspath = uma_zalloc(namei_zone, M_WAITOK);
1569 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1572 AUDIT_ARG_TEXT(syspath);
1575 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1576 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1578 if ((error = namei(&nd)) != 0)
1581 NDFREE(&nd, NDF_ONLY_PNBUF);
1582 if (nd.ni_vp == nd.ni_dvp)
1590 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1591 NDFREE(&nd, NDF_ONLY_PNBUF);
1593 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1598 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1600 vattr.va_type = VLNK;
1601 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1606 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1612 NDFREE(&nd, NDF_ONLY_PNBUF);
1614 vn_finished_write(mp);
1616 if (segflg != UIO_SYSSPACE)
1617 uma_zfree(namei_zone, syspath);
1622 * Delete a whiteout from the filesystem.
1625 sys_undelete(td, uap)
1627 register struct undelete_args /* {
1632 struct nameidata nd;
1637 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1638 UIO_USERSPACE, uap->path, td);
1643 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1644 NDFREE(&nd, NDF_ONLY_PNBUF);
1645 if (nd.ni_vp == nd.ni_dvp)
1653 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1654 NDFREE(&nd, NDF_ONLY_PNBUF);
1656 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1660 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1661 NDFREE(&nd, NDF_ONLY_PNBUF);
1663 vn_finished_write(mp);
1668 * Delete a name from the filesystem.
1670 #ifndef _SYS_SYSPROTO_H_
1671 struct unlink_args {
1678 struct unlink_args /* {
1683 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1686 #ifndef _SYS_SYSPROTO_H_
1687 struct unlinkat_args {
1694 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1696 int flag = uap->flag;
1698 char *path = uap->path;
1700 if (flag & ~AT_REMOVEDIR)
1703 if (flag & AT_REMOVEDIR)
1704 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1706 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1710 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1715 struct nameidata nd;
1717 cap_rights_t rights;
1722 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1723 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1724 if ((error = namei(&nd)) != 0)
1725 return (error == EINVAL ? EPERM : error);
1727 if (vp->v_type == VDIR && oldinum == 0) {
1728 error = EPERM; /* POSIX */
1729 } else if (oldinum != 0 &&
1730 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1731 sb.st_ino != oldinum) {
1732 error = EIDRM; /* Identifier removed */
1735 * The root of a mounted filesystem cannot be deleted.
1737 * XXX: can this only be a VDIR case?
1739 if (vp->v_vflag & VV_ROOT)
1743 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1744 NDFREE(&nd, NDF_ONLY_PNBUF);
1746 if (vp == nd.ni_dvp)
1750 if ((error = vn_start_write(NULL, &mp,
1751 V_XSLEEP | PCATCH)) != 0)
1756 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1761 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1762 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1766 vn_finished_write(mp);
1768 NDFREE(&nd, NDF_ONLY_PNBUF);
1770 if (vp == nd.ni_dvp)
1778 * Reposition read/write file offset.
1780 #ifndef _SYS_SYSPROTO_H_
1791 register struct lseek_args /* {
1799 cap_rights_t rights;
1802 AUDIT_ARG_FD(uap->fd);
1803 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1806 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1807 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1812 #if defined(COMPAT_43)
1814 * Reposition read/write file offset.
1816 #ifndef _SYS_SYSPROTO_H_
1817 struct olseek_args {
1826 register struct olseek_args /* {
1832 struct lseek_args /* {
1840 nuap.offset = uap->offset;
1841 nuap.whence = uap->whence;
1842 return (sys_lseek(td, &nuap));
1844 #endif /* COMPAT_43 */
1846 #if defined(COMPAT_FREEBSD6)
1847 /* Version with the 'pad' argument */
1849 freebsd6_lseek(td, uap)
1851 register struct freebsd6_lseek_args *uap;
1853 struct lseek_args ouap;
1856 ouap.offset = uap->offset;
1857 ouap.whence = uap->whence;
1858 return (sys_lseek(td, &ouap));
1863 * Check access permissions using passed credentials.
1866 vn_access(vp, user_flags, cred, td)
1875 /* Flags == 0 means only check for existence. */
1876 if (user_flags == 0)
1880 if (user_flags & R_OK)
1882 if (user_flags & W_OK)
1884 if (user_flags & X_OK)
1887 error = mac_vnode_check_access(cred, vp, accmode);
1891 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1892 error = VOP_ACCESS(vp, accmode, cred, td);
1897 * Check access permissions using "real" credentials.
1899 #ifndef _SYS_SYSPROTO_H_
1900 struct access_args {
1908 register struct access_args /* {
1914 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1918 #ifndef _SYS_SYSPROTO_H_
1919 struct faccessat_args {
1927 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1930 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1935 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1936 int flag, int amode)
1938 struct ucred *cred, *usecred;
1940 struct nameidata nd;
1941 cap_rights_t rights;
1944 if (flag & ~AT_EACCESS)
1946 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1950 * Create and modify a temporary credential instead of one that
1951 * is potentially shared (if we need one).
1953 cred = td->td_ucred;
1954 if ((flag & AT_EACCESS) == 0 &&
1955 ((cred->cr_uid != cred->cr_ruid ||
1956 cred->cr_rgid != cred->cr_groups[0]))) {
1957 usecred = crdup(cred);
1958 usecred->cr_uid = cred->cr_ruid;
1959 usecred->cr_groups[0] = cred->cr_rgid;
1960 td->td_ucred = usecred;
1963 AUDIT_ARG_VALUE(amode);
1964 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1965 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
1967 if ((error = namei(&nd)) != 0)
1971 error = vn_access(vp, amode, usecred, td);
1972 NDFREE(&nd, NDF_ONLY_PNBUF);
1975 if (usecred != cred) {
1976 td->td_ucred = cred;
1983 * Check access permissions using "effective" credentials.
1985 #ifndef _SYS_SYSPROTO_H_
1986 struct eaccess_args {
1992 sys_eaccess(td, uap)
1994 register struct eaccess_args /* {
2000 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2001 AT_EACCESS, uap->amode));
2004 #if defined(COMPAT_43)
2006 * Get file status; this version follows links.
2008 #ifndef _SYS_SYSPROTO_H_
2017 register struct ostat_args /* {
2026 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2031 return (copyout(&osb, uap->ub, sizeof (osb)));
2035 * Get file status; this version does not follow links.
2037 #ifndef _SYS_SYSPROTO_H_
2038 struct olstat_args {
2046 register struct olstat_args /* {
2055 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2056 UIO_USERSPACE, &sb, NULL);
2060 return (copyout(&osb, uap->ub, sizeof (osb)));
2064 * Convert from an old to a new stat structure.
2072 ost->st_dev = st->st_dev;
2073 ost->st_ino = st->st_ino;
2074 ost->st_mode = st->st_mode;
2075 ost->st_nlink = st->st_nlink;
2076 ost->st_uid = st->st_uid;
2077 ost->st_gid = st->st_gid;
2078 ost->st_rdev = st->st_rdev;
2079 if (st->st_size < (quad_t)1 << 32)
2080 ost->st_size = st->st_size;
2083 ost->st_atim = st->st_atim;
2084 ost->st_mtim = st->st_mtim;
2085 ost->st_ctim = st->st_ctim;
2086 ost->st_blksize = st->st_blksize;
2087 ost->st_blocks = st->st_blocks;
2088 ost->st_flags = st->st_flags;
2089 ost->st_gen = st->st_gen;
2091 #endif /* COMPAT_43 */
2094 * Get file status; this version follows links.
2096 #ifndef _SYS_SYSPROTO_H_
2105 register struct stat_args /* {
2113 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2116 error = copyout(&sb, uap->ub, sizeof (sb));
2120 #ifndef _SYS_SYSPROTO_H_
2121 struct fstatat_args {
2129 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2134 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2135 UIO_USERSPACE, &sb, NULL);
2137 error = copyout(&sb, uap->buf, sizeof (sb));
2142 kern_statat(struct thread *td, int flag, int fd, char *path,
2143 enum uio_seg pathseg, struct stat *sbp,
2144 void (*hook)(struct vnode *vp, struct stat *sbp))
2146 struct nameidata nd;
2148 cap_rights_t rights;
2151 if (flag & ~AT_SYMLINK_NOFOLLOW)
2154 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2155 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2156 cap_rights_init(&rights, CAP_FSTAT), td);
2158 if ((error = namei(&nd)) != 0)
2160 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2162 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2163 if (S_ISREG(sb.st_mode))
2164 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2165 if (__predict_false(hook != NULL))
2166 hook(nd.ni_vp, &sb);
2168 NDFREE(&nd, NDF_ONLY_PNBUF);
2174 if (KTRPOINT(td, KTR_STRUCT))
2181 * Get file status; this version does not follow links.
2183 #ifndef _SYS_SYSPROTO_H_
2192 register struct lstat_args /* {
2200 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2201 UIO_USERSPACE, &sb, NULL);
2203 error = copyout(&sb, uap->ub, sizeof (sb));
2208 * Implementation of the NetBSD [l]stat() functions.
2216 bzero(nsb, sizeof *nsb);
2217 nsb->st_dev = sb->st_dev;
2218 nsb->st_ino = sb->st_ino;
2219 nsb->st_mode = sb->st_mode;
2220 nsb->st_nlink = sb->st_nlink;
2221 nsb->st_uid = sb->st_uid;
2222 nsb->st_gid = sb->st_gid;
2223 nsb->st_rdev = sb->st_rdev;
2224 nsb->st_atim = sb->st_atim;
2225 nsb->st_mtim = sb->st_mtim;
2226 nsb->st_ctim = sb->st_ctim;
2227 nsb->st_size = sb->st_size;
2228 nsb->st_blocks = sb->st_blocks;
2229 nsb->st_blksize = sb->st_blksize;
2230 nsb->st_flags = sb->st_flags;
2231 nsb->st_gen = sb->st_gen;
2232 nsb->st_birthtim = sb->st_birthtim;
2235 #ifndef _SYS_SYSPROTO_H_
2244 register struct nstat_args /* {
2253 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2257 cvtnstat(&sb, &nsb);
2258 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2262 * NetBSD lstat. Get file status; this version does not follow links.
2264 #ifndef _SYS_SYSPROTO_H_
2273 register struct nlstat_args /* {
2282 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2283 UIO_USERSPACE, &sb, NULL);
2286 cvtnstat(&sb, &nsb);
2287 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2291 * Get configurable pathname variables.
2293 #ifndef _SYS_SYSPROTO_H_
2294 struct pathconf_args {
2300 sys_pathconf(td, uap)
2302 register struct pathconf_args /* {
2308 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2311 #ifndef _SYS_SYSPROTO_H_
2312 struct lpathconf_args {
2318 sys_lpathconf(td, uap)
2320 register struct lpathconf_args /* {
2326 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2331 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2334 struct nameidata nd;
2337 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2339 if ((error = namei(&nd)) != 0)
2341 NDFREE(&nd, NDF_ONLY_PNBUF);
2343 /* If asynchronous I/O is available, it works for all files. */
2344 if (name == _PC_ASYNC_IO)
2345 td->td_retval[0] = async_io_version;
2347 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2353 * Return target name of a symbolic link.
2355 #ifndef _SYS_SYSPROTO_H_
2356 struct readlink_args {
2363 sys_readlink(td, uap)
2365 register struct readlink_args /* {
2372 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2373 uap->buf, UIO_USERSPACE, uap->count));
2375 #ifndef _SYS_SYSPROTO_H_
2376 struct readlinkat_args {
2384 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2387 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2388 uap->buf, UIO_USERSPACE, uap->bufsize));
2392 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2393 char *buf, enum uio_seg bufseg, size_t count)
2398 struct nameidata nd;
2401 if (count > IOSIZE_MAX)
2404 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2405 pathseg, path, fd, td);
2407 if ((error = namei(&nd)) != 0)
2409 NDFREE(&nd, NDF_ONLY_PNBUF);
2412 error = mac_vnode_check_readlink(td->td_ucred, vp);
2418 if (vp->v_type != VLNK)
2421 aiov.iov_base = buf;
2422 aiov.iov_len = count;
2423 auio.uio_iov = &aiov;
2424 auio.uio_iovcnt = 1;
2425 auio.uio_offset = 0;
2426 auio.uio_rw = UIO_READ;
2427 auio.uio_segflg = bufseg;
2429 auio.uio_resid = count;
2430 error = VOP_READLINK(vp, &auio, td->td_ucred);
2431 td->td_retval[0] = count - auio.uio_resid;
2438 * Common implementation code for chflags() and fchflags().
2441 setfflags(td, vp, flags)
2450 /* We can't support the value matching VNOVAL. */
2451 if (flags == VNOVAL)
2452 return (EOPNOTSUPP);
2455 * Prevent non-root users from setting flags on devices. When
2456 * a device is reused, users can retain ownership of the device
2457 * if they are allowed to set flags and programs assume that
2458 * chown can't fail when done as root.
2460 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2461 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2466 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2469 vattr.va_flags = flags;
2470 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2472 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2475 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2477 vn_finished_write(mp);
2482 * Change flags of a file given a path name.
2484 #ifndef _SYS_SYSPROTO_H_
2485 struct chflags_args {
2491 sys_chflags(td, uap)
2493 register struct chflags_args /* {
2499 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2503 #ifndef _SYS_SYSPROTO_H_
2504 struct chflagsat_args {
2512 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2515 const char *path = uap->path;
2516 u_long flags = uap->flags;
2517 int atflag = uap->atflag;
2519 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2522 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2526 * Same as chflags() but doesn't follow symlinks.
2529 sys_lchflags(td, uap)
2531 register struct lchflags_args /* {
2537 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2538 uap->flags, AT_SYMLINK_NOFOLLOW));
2542 kern_chflagsat(struct thread *td, int fd, const char *path,
2543 enum uio_seg pathseg, u_long flags, int atflag)
2545 struct nameidata nd;
2546 cap_rights_t rights;
2549 AUDIT_ARG_FFLAGS(flags);
2550 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2551 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2552 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2553 if ((error = namei(&nd)) != 0)
2555 NDFREE(&nd, NDF_ONLY_PNBUF);
2556 error = setfflags(td, nd.ni_vp, flags);
2562 * Change flags of a file given a file descriptor.
2564 #ifndef _SYS_SYSPROTO_H_
2565 struct fchflags_args {
2571 sys_fchflags(td, uap)
2573 register struct fchflags_args /* {
2579 cap_rights_t rights;
2582 AUDIT_ARG_FD(uap->fd);
2583 AUDIT_ARG_FFLAGS(uap->flags);
2584 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2589 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2590 AUDIT_ARG_VNODE1(fp->f_vnode);
2591 VOP_UNLOCK(fp->f_vnode, 0);
2593 error = setfflags(td, fp->f_vnode, uap->flags);
2599 * Common implementation code for chmod(), lchmod() and fchmod().
2602 setfmode(td, cred, vp, mode)
2612 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2614 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2616 vattr.va_mode = mode & ALLPERMS;
2618 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2621 error = VOP_SETATTR(vp, &vattr, cred);
2623 vn_finished_write(mp);
2628 * Change mode of a file given path name.
2630 #ifndef _SYS_SYSPROTO_H_
2639 register struct chmod_args /* {
2645 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2649 #ifndef _SYS_SYSPROTO_H_
2650 struct fchmodat_args {
2658 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2660 int flag = uap->flag;
2662 char *path = uap->path;
2663 mode_t mode = uap->mode;
2665 if (flag & ~AT_SYMLINK_NOFOLLOW)
2668 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2672 * Change mode of a file given path name (don't follow links.)
2674 #ifndef _SYS_SYSPROTO_H_
2675 struct lchmod_args {
2683 register struct lchmod_args /* {
2689 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2690 uap->mode, AT_SYMLINK_NOFOLLOW));
2694 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2695 mode_t mode, int flag)
2697 struct nameidata nd;
2698 cap_rights_t rights;
2701 AUDIT_ARG_MODE(mode);
2702 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2703 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2704 cap_rights_init(&rights, CAP_FCHMOD), td);
2705 if ((error = namei(&nd)) != 0)
2707 NDFREE(&nd, NDF_ONLY_PNBUF);
2708 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2714 * Change mode of a file given a file descriptor.
2716 #ifndef _SYS_SYSPROTO_H_
2717 struct fchmod_args {
2723 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2726 cap_rights_t rights;
2729 AUDIT_ARG_FD(uap->fd);
2730 AUDIT_ARG_MODE(uap->mode);
2732 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2735 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2741 * Common implementation for chown(), lchown(), and fchown()
2744 setfown(td, cred, vp, uid, gid)
2755 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2757 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2762 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2766 error = VOP_SETATTR(vp, &vattr, cred);
2768 vn_finished_write(mp);
2773 * Set ownership given a path name.
2775 #ifndef _SYS_SYSPROTO_H_
2785 register struct chown_args /* {
2792 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2796 #ifndef _SYS_SYSPROTO_H_
2797 struct fchownat_args {
2806 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2811 if (flag & ~AT_SYMLINK_NOFOLLOW)
2814 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2815 uap->gid, uap->flag));
2819 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2820 int uid, int gid, int flag)
2822 struct nameidata nd;
2823 cap_rights_t rights;
2826 AUDIT_ARG_OWNER(uid, gid);
2827 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2828 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2829 cap_rights_init(&rights, CAP_FCHOWN), td);
2831 if ((error = namei(&nd)) != 0)
2833 NDFREE(&nd, NDF_ONLY_PNBUF);
2834 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2840 * Set ownership given a path name, do not cross symlinks.
2842 #ifndef _SYS_SYSPROTO_H_
2843 struct lchown_args {
2852 register struct lchown_args /* {
2859 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2860 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2864 * Set ownership given a file descriptor.
2866 #ifndef _SYS_SYSPROTO_H_
2867 struct fchown_args {
2876 register struct fchown_args /* {
2883 cap_rights_t rights;
2886 AUDIT_ARG_FD(uap->fd);
2887 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2888 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2891 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2897 * Common implementation code for utimes(), lutimes(), and futimes().
2900 getutimes(usrtvp, tvpseg, tsp)
2901 const struct timeval *usrtvp;
2902 enum uio_seg tvpseg;
2903 struct timespec *tsp;
2905 struct timeval tv[2];
2906 const struct timeval *tvp;
2909 if (usrtvp == NULL) {
2910 vfs_timestamp(&tsp[0]);
2913 if (tvpseg == UIO_SYSSPACE) {
2916 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2921 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2922 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2924 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2925 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2931 * Common implementation code for futimens(), utimensat().
2933 #define UTIMENS_NULL 0x1
2934 #define UTIMENS_EXIT 0x2
2936 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2937 struct timespec *tsp, int *retflags)
2939 struct timespec tsnow;
2942 vfs_timestamp(&tsnow);
2944 if (usrtsp == NULL) {
2947 *retflags |= UTIMENS_NULL;
2950 if (tspseg == UIO_SYSSPACE) {
2953 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2955 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2956 *retflags |= UTIMENS_EXIT;
2957 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2958 *retflags |= UTIMENS_NULL;
2959 if (tsp[0].tv_nsec == UTIME_OMIT)
2960 tsp[0].tv_sec = VNOVAL;
2961 else if (tsp[0].tv_nsec == UTIME_NOW)
2963 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2965 if (tsp[1].tv_nsec == UTIME_OMIT)
2966 tsp[1].tv_sec = VNOVAL;
2967 else if (tsp[1].tv_nsec == UTIME_NOW)
2969 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2976 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2980 setutimes(td, vp, ts, numtimes, nullflag)
2983 const struct timespec *ts;
2989 int error, setbirthtime;
2991 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2993 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2995 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
2996 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2999 vattr.va_atime = ts[0];
3000 vattr.va_mtime = ts[1];
3002 vattr.va_birthtime = ts[1];
3004 vattr.va_birthtime = ts[2];
3006 vattr.va_vaflags |= VA_UTIMES_NULL;
3008 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3012 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3014 vn_finished_write(mp);
3019 * Set the access and modification times of a file.
3021 #ifndef _SYS_SYSPROTO_H_
3022 struct utimes_args {
3024 struct timeval *tptr;
3030 register struct utimes_args /* {
3032 struct timeval *tptr;
3036 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3037 uap->tptr, UIO_USERSPACE));
3040 #ifndef _SYS_SYSPROTO_H_
3041 struct futimesat_args {
3044 const struct timeval * times;
3048 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3051 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3052 uap->times, UIO_USERSPACE));
3056 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3057 struct timeval *tptr, enum uio_seg tptrseg)
3059 struct nameidata nd;
3060 struct timespec ts[2];
3061 cap_rights_t rights;
3064 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3066 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3067 cap_rights_init(&rights, CAP_FUTIMES), td);
3069 if ((error = namei(&nd)) != 0)
3071 NDFREE(&nd, NDF_ONLY_PNBUF);
3072 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3078 * Set the access and modification times of a file.
3080 #ifndef _SYS_SYSPROTO_H_
3081 struct lutimes_args {
3083 struct timeval *tptr;
3087 sys_lutimes(td, uap)
3089 register struct lutimes_args /* {
3091 struct timeval *tptr;
3095 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3100 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3101 struct timeval *tptr, enum uio_seg tptrseg)
3103 struct timespec ts[2];
3104 struct nameidata nd;
3107 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3109 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3110 if ((error = namei(&nd)) != 0)
3112 NDFREE(&nd, NDF_ONLY_PNBUF);
3113 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3119 * Set the access and modification times of a file.
3121 #ifndef _SYS_SYSPROTO_H_
3122 struct futimes_args {
3124 struct timeval *tptr;
3128 sys_futimes(td, uap)
3130 register struct futimes_args /* {
3132 struct timeval *tptr;
3136 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3140 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3141 enum uio_seg tptrseg)
3143 struct timespec ts[2];
3145 cap_rights_t rights;
3149 error = getutimes(tptr, tptrseg, ts);
3152 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3156 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3157 AUDIT_ARG_VNODE1(fp->f_vnode);
3158 VOP_UNLOCK(fp->f_vnode, 0);
3160 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3166 sys_futimens(struct thread *td, struct futimens_args *uap)
3169 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3173 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3174 enum uio_seg tptrseg)
3176 struct timespec ts[2];
3178 cap_rights_t rights;
3182 error = getutimens(tptr, tptrseg, ts, &flags);
3185 if (flags & UTIMENS_EXIT)
3187 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3191 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3192 AUDIT_ARG_VNODE1(fp->f_vnode);
3193 VOP_UNLOCK(fp->f_vnode, 0);
3195 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3201 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3204 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3205 uap->times, UIO_USERSPACE, uap->flag));
3209 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3210 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3212 struct nameidata nd;
3213 struct timespec ts[2];
3214 cap_rights_t rights;
3217 if (flag & ~AT_SYMLINK_NOFOLLOW)
3220 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3222 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3223 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3224 cap_rights_init(&rights, CAP_FUTIMES), td);
3225 if ((error = namei(&nd)) != 0)
3228 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3230 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3231 * "Search permission is denied by a component of the path prefix."
3233 NDFREE(&nd, NDF_ONLY_PNBUF);
3234 if ((flags & UTIMENS_EXIT) == 0)
3235 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3241 * Truncate a file given its path name.
3243 #ifndef _SYS_SYSPROTO_H_
3244 struct truncate_args {
3251 sys_truncate(td, uap)
3253 register struct truncate_args /* {
3260 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3264 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3270 struct nameidata nd;
3275 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3276 if ((error = namei(&nd)) != 0)
3279 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3280 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3281 vn_rangelock_unlock(vp, rl_cookie);
3285 NDFREE(&nd, NDF_ONLY_PNBUF);
3286 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3287 if (vp->v_type == VDIR)
3290 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3293 else if ((error = vn_writechk(vp)) == 0 &&
3294 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3296 vattr.va_size = length;
3297 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3300 vn_finished_write(mp);
3301 vn_rangelock_unlock(vp, rl_cookie);
3306 #if defined(COMPAT_43)
3308 * Truncate a file given its path name.
3310 #ifndef _SYS_SYSPROTO_H_
3311 struct otruncate_args {
3319 register struct otruncate_args /* {
3324 struct truncate_args /* {
3330 nuap.path = uap->path;
3331 nuap.length = uap->length;
3332 return (sys_truncate(td, &nuap));
3334 #endif /* COMPAT_43 */
3336 #if defined(COMPAT_FREEBSD6)
3337 /* Versions with the pad argument */
3339 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3341 struct truncate_args ouap;
3343 ouap.path = uap->path;
3344 ouap.length = uap->length;
3345 return (sys_truncate(td, &ouap));
3349 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3351 struct ftruncate_args ouap;
3354 ouap.length = uap->length;
3355 return (sys_ftruncate(td, &ouap));
3360 * Sync an open file.
3362 #ifndef _SYS_SYSPROTO_H_
3370 struct fsync_args /* {
3377 cap_rights_t rights;
3378 int error, lock_flags;
3380 AUDIT_ARG_FD(uap->fd);
3381 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3385 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3388 if (MNT_SHARED_WRITES(mp) ||
3389 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3390 lock_flags = LK_SHARED;
3392 lock_flags = LK_EXCLUSIVE;
3394 vn_lock(vp, lock_flags | LK_RETRY);
3395 AUDIT_ARG_VNODE1(vp);
3396 if (vp->v_object != NULL) {
3397 VM_OBJECT_WLOCK(vp->v_object);
3398 vm_object_page_clean(vp->v_object, 0, 0, 0);
3399 VM_OBJECT_WUNLOCK(vp->v_object);
3401 error = VOP_FSYNC(vp, MNT_WAIT, td);
3404 vn_finished_write(mp);
3411 * Rename files. Source and destination must either both be directories, or
3412 * both not be directories. If target is a directory, it must be empty.
3414 #ifndef _SYS_SYSPROTO_H_
3415 struct rename_args {
3423 register struct rename_args /* {
3429 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3430 uap->to, UIO_USERSPACE));
3433 #ifndef _SYS_SYSPROTO_H_
3434 struct renameat_args {
3442 sys_renameat(struct thread *td, struct renameat_args *uap)
3445 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3450 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3451 enum uio_seg pathseg)
3453 struct mount *mp = NULL;
3454 struct vnode *tvp, *fvp, *tdvp;
3455 struct nameidata fromnd, tond;
3456 cap_rights_t rights;
3462 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3463 AUDITVNODE1, pathseg, old, oldfd,
3464 cap_rights_init(&rights, CAP_RENAMEAT), td);
3466 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3467 pathseg, old, oldfd, cap_rights_init(&rights, CAP_RENAMEAT), td);
3470 if ((error = namei(&fromnd)) != 0)
3473 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3474 fromnd.ni_vp, &fromnd.ni_cnd);
3475 VOP_UNLOCK(fromnd.ni_dvp, 0);
3476 if (fromnd.ni_dvp != fromnd.ni_vp)
3477 VOP_UNLOCK(fromnd.ni_vp, 0);
3480 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3481 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3482 cap_rights_init(&rights, CAP_LINKAT), td);
3483 if (fromnd.ni_vp->v_type == VDIR)
3484 tond.ni_cnd.cn_flags |= WILLBEDIR;
3485 if ((error = namei(&tond)) != 0) {
3486 /* Translate error code for rename("dir1", "dir2/."). */
3487 if (error == EISDIR && fvp->v_type == VDIR)
3489 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3490 vrele(fromnd.ni_dvp);
3496 error = vn_start_write(fvp, &mp, V_NOWAIT);
3498 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3499 NDFREE(&tond, NDF_ONLY_PNBUF);
3506 vrele(fromnd.ni_dvp);
3508 vrele(tond.ni_startdir);
3509 if (fromnd.ni_startdir != NULL)
3510 vrele(fromnd.ni_startdir);
3511 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3517 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3520 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3525 if (newfd != AT_FDCWD) {
3527 * If the target already exists we require CAP_UNLINKAT
3530 error = cap_check(&tond.ni_filecaps.fc_rights,
3531 cap_rights_init(&rights, CAP_UNLINKAT));
3542 * If the source is the same as the destination (that is, if they
3543 * are links to the same vnode), then there is nothing to do.
3549 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3550 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3554 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3555 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3556 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3557 NDFREE(&tond, NDF_ONLY_PNBUF);
3559 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3560 NDFREE(&tond, NDF_ONLY_PNBUF);
3567 vrele(fromnd.ni_dvp);
3570 vrele(tond.ni_startdir);
3571 vn_finished_write(mp);
3573 if (fromnd.ni_startdir)
3574 vrele(fromnd.ni_startdir);
3581 * Make a directory file.
3583 #ifndef _SYS_SYSPROTO_H_
3592 register struct mkdir_args /* {
3598 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3602 #ifndef _SYS_SYSPROTO_H_
3603 struct mkdirat_args {
3610 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3613 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3617 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3623 struct nameidata nd;
3624 cap_rights_t rights;
3627 AUDIT_ARG_MODE(mode);
3630 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3631 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3633 nd.ni_cnd.cn_flags |= WILLBEDIR;
3634 if ((error = namei(&nd)) != 0)
3638 NDFREE(&nd, NDF_ONLY_PNBUF);
3640 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3641 * the strange behaviour of leaving the vnode unlocked
3642 * if the target is the same vnode as the parent.
3644 if (vp == nd.ni_dvp)
3651 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3652 NDFREE(&nd, NDF_ONLY_PNBUF);
3654 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3659 vattr.va_type = VDIR;
3660 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3662 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3667 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3671 NDFREE(&nd, NDF_ONLY_PNBUF);
3675 vn_finished_write(mp);
3680 * Remove a directory file.
3682 #ifndef _SYS_SYSPROTO_H_
3690 struct rmdir_args /* {
3695 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3699 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3703 struct nameidata nd;
3704 cap_rights_t rights;
3709 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3710 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3711 if ((error = namei(&nd)) != 0)
3714 if (vp->v_type != VDIR) {
3719 * No rmdir "." please.
3721 if (nd.ni_dvp == vp) {
3726 * The root of a mounted filesystem cannot be deleted.
3728 if (vp->v_vflag & VV_ROOT) {
3733 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3738 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3739 NDFREE(&nd, NDF_ONLY_PNBUF);
3741 if (nd.ni_dvp == vp)
3745 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3749 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3750 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3751 vn_finished_write(mp);
3753 NDFREE(&nd, NDF_ONLY_PNBUF);
3755 if (nd.ni_dvp == vp)
3764 * Read a block of directory entries in a filesystem independent format.
3766 #ifndef _SYS_SYSPROTO_H_
3767 struct ogetdirentries_args {
3775 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3780 error = kern_ogetdirentries(td, uap, &loff);
3782 error = copyout(&loff, uap->basep, sizeof(long));
3787 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3792 struct uio auio, kuio;
3793 struct iovec aiov, kiov;
3794 struct dirent *dp, *edp;
3795 cap_rights_t rights;
3797 int error, eofflag, readcnt;
3801 /* XXX arbitrary sanity limit on `count'. */
3802 if (uap->count > 64 * 1024)
3804 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp);
3807 if ((fp->f_flag & FREAD) == 0) {
3812 foffset = foffset_lock(fp, 0);
3814 if (vp->v_type != VDIR) {
3815 foffset_unlock(fp, foffset, 0);
3819 aiov.iov_base = uap->buf;
3820 aiov.iov_len = uap->count;
3821 auio.uio_iov = &aiov;
3822 auio.uio_iovcnt = 1;
3823 auio.uio_rw = UIO_READ;
3824 auio.uio_segflg = UIO_USERSPACE;
3826 auio.uio_resid = uap->count;
3827 vn_lock(vp, LK_SHARED | LK_RETRY);
3828 loff = auio.uio_offset = foffset;
3830 error = mac_vnode_check_readdir(td->td_ucred, vp);
3833 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3838 # if (BYTE_ORDER != LITTLE_ENDIAN)
3839 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3840 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3842 foffset = auio.uio_offset;
3847 kuio.uio_iov = &kiov;
3848 kuio.uio_segflg = UIO_SYSSPACE;
3849 kiov.iov_len = uap->count;
3850 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3851 kiov.iov_base = dirbuf;
3852 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3854 foffset = kuio.uio_offset;
3856 readcnt = uap->count - kuio.uio_resid;
3857 edp = (struct dirent *)&dirbuf[readcnt];
3858 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3859 # if (BYTE_ORDER == LITTLE_ENDIAN)
3861 * The expected low byte of
3862 * dp->d_namlen is our dp->d_type.
3863 * The high MBZ byte of dp->d_namlen
3864 * is our dp->d_namlen.
3866 dp->d_type = dp->d_namlen;
3870 * The dp->d_type is the high byte
3871 * of the expected dp->d_namlen,
3872 * so must be zero'ed.
3876 if (dp->d_reclen > 0) {
3877 dp = (struct dirent *)
3878 ((char *)dp + dp->d_reclen);
3885 error = uiomove(dirbuf, readcnt, &auio);
3887 free(dirbuf, M_TEMP);
3891 foffset_unlock(fp, foffset, 0);
3895 if (uap->count == auio.uio_resid &&
3896 (vp->v_vflag & VV_ROOT) &&
3897 (vp->v_mount->mnt_flag & MNT_UNION)) {
3898 struct vnode *tvp = vp;
3899 vp = vp->v_mount->mnt_vnodecovered;
3908 foffset_unlock(fp, foffset, 0);
3910 td->td_retval[0] = uap->count - auio.uio_resid;
3915 #endif /* COMPAT_43 */
3918 * Read a block of directory entries in a filesystem independent format.
3920 #ifndef _SYS_SYSPROTO_H_
3921 struct getdirentries_args {
3929 sys_getdirentries(td, uap)
3931 register struct getdirentries_args /* {
3941 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3942 NULL, UIO_USERSPACE);
3945 if (uap->basep != NULL)
3946 error = copyout(&base, uap->basep, sizeof(long));
3951 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3952 long *basep, ssize_t *residp, enum uio_seg bufseg)
3958 cap_rights_t rights;
3964 if (count > IOSIZE_MAX)
3966 auio.uio_resid = count;
3967 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3970 if ((fp->f_flag & FREAD) == 0) {
3975 foffset = foffset_lock(fp, 0);
3977 if (vp->v_type != VDIR) {
3981 aiov.iov_base = buf;
3982 aiov.iov_len = count;
3983 auio.uio_iov = &aiov;
3984 auio.uio_iovcnt = 1;
3985 auio.uio_rw = UIO_READ;
3986 auio.uio_segflg = bufseg;
3988 vn_lock(vp, LK_SHARED | LK_RETRY);
3989 AUDIT_ARG_VNODE1(vp);
3990 loff = auio.uio_offset = foffset;
3992 error = mac_vnode_check_readdir(td->td_ucred, vp);
3995 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3997 foffset = auio.uio_offset;
4002 if (count == auio.uio_resid &&
4003 (vp->v_vflag & VV_ROOT) &&
4004 (vp->v_mount->mnt_flag & MNT_UNION)) {
4005 struct vnode *tvp = vp;
4007 vp = vp->v_mount->mnt_vnodecovered;
4018 *residp = auio.uio_resid;
4019 td->td_retval[0] = count - auio.uio_resid;
4021 foffset_unlock(fp, foffset, 0);
4026 #ifndef _SYS_SYSPROTO_H_
4027 struct getdents_args {
4034 sys_getdents(td, uap)
4036 register struct getdents_args /* {
4042 struct getdirentries_args ap;
4046 ap.count = uap->count;
4048 return (sys_getdirentries(td, &ap));
4052 * Set the mode mask for creation of filesystem nodes.
4054 #ifndef _SYS_SYSPROTO_H_
4062 struct umask_args /* {
4066 struct filedesc *fdp;
4068 fdp = td->td_proc->p_fd;
4069 FILEDESC_XLOCK(fdp);
4070 td->td_retval[0] = fdp->fd_cmask;
4071 fdp->fd_cmask = uap->newmask & ALLPERMS;
4072 FILEDESC_XUNLOCK(fdp);
4077 * Void all references to file by ripping underlying filesystem away from
4080 #ifndef _SYS_SYSPROTO_H_
4081 struct revoke_args {
4088 register struct revoke_args /* {
4094 struct nameidata nd;
4097 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4099 if ((error = namei(&nd)) != 0)
4102 NDFREE(&nd, NDF_ONLY_PNBUF);
4103 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4108 error = mac_vnode_check_revoke(td->td_ucred, vp);
4112 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4115 if (td->td_ucred->cr_uid != vattr.va_uid) {
4116 error = priv_check(td, PRIV_VFS_ADMIN);
4121 VOP_REVOKE(vp, REVOKEALL);
4128 * Convert a user file descriptor to a kernel file entry and check that, if it
4129 * is a capability, the correct rights are present. A reference on the file
4130 * entry is held upon returning.
4133 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4138 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4143 * The file could be not of the vnode type, or it may be not
4144 * yet fully initialized, in which case the f_vnode pointer
4145 * may be set, but f_ops is still badfileops. E.g.,
4146 * devfs_open() transiently create such situation to
4147 * facilitate csw d_fdopen().
4149 * Dupfdopen() handling in kern_openat() installs the
4150 * half-baked file into the process descriptor table, allowing
4151 * other thread to dereference it. Guard against the race by
4154 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4164 * Get an (NFS) file handle.
4166 #ifndef _SYS_SYSPROTO_H_
4167 struct lgetfh_args {
4175 register struct lgetfh_args *uap;
4177 struct nameidata nd;
4179 register struct vnode *vp;
4182 error = priv_check(td, PRIV_VFS_GETFH);
4185 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4190 NDFREE(&nd, NDF_ONLY_PNBUF);
4192 bzero(&fh, sizeof(fh));
4193 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4194 error = VOP_VPTOFH(vp, &fh.fh_fid);
4197 error = copyout(&fh, uap->fhp, sizeof (fh));
4201 #ifndef _SYS_SYSPROTO_H_
4210 register struct getfh_args *uap;
4212 struct nameidata nd;
4214 register struct vnode *vp;
4217 error = priv_check(td, PRIV_VFS_GETFH);
4220 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4225 NDFREE(&nd, NDF_ONLY_PNBUF);
4227 bzero(&fh, sizeof(fh));
4228 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4229 error = VOP_VPTOFH(vp, &fh.fh_fid);
4232 error = copyout(&fh, uap->fhp, sizeof (fh));
4237 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4240 * warning: do not remove the priv_check() call or this becomes one giant
4243 #ifndef _SYS_SYSPROTO_H_
4244 struct fhopen_args {
4245 const struct fhandle *u_fhp;
4252 struct fhopen_args /* {
4253 const struct fhandle *u_fhp;
4264 error = priv_check(td, PRIV_VFS_FHOPEN);
4268 fmode = FFLAGS(uap->flags);
4269 /* why not allow a non-read/write open for our lockd? */
4270 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4272 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4275 /* find the mount point */
4276 mp = vfs_busyfs(&fhp.fh_fsid);
4279 /* now give me my vnode, it gets returned to me locked */
4280 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4285 error = falloc_noinstall(td, &fp);
4291 * An extra reference on `fp' has been held for us by
4292 * falloc_noinstall().
4298 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4300 KASSERT(fp->f_ops == &badfileops,
4301 ("VOP_OPEN in fhopen() set f_ops"));
4302 KASSERT(td->td_dupfd < 0,
4303 ("fhopen() encountered fdopen()"));
4313 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4316 if ((fmode & O_TRUNC) != 0) {
4317 error = fo_truncate(fp, 0, td->td_ucred, td);
4322 error = finstall(td, fp, &indx, fmode, NULL);
4325 td->td_retval[0] = indx;
4330 * Stat an (NFS) file handle.
4332 #ifndef _SYS_SYSPROTO_H_
4333 struct fhstat_args {
4334 struct fhandle *u_fhp;
4341 register struct fhstat_args /* {
4342 struct fhandle *u_fhp;
4350 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4353 error = kern_fhstat(td, fh, &sb);
4355 error = copyout(&sb, uap->sb, sizeof(sb));
4360 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4366 error = priv_check(td, PRIV_VFS_FHSTAT);
4369 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4371 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4375 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4381 * Implement fstatfs() for (NFS) file handles.
4383 #ifndef _SYS_SYSPROTO_H_
4384 struct fhstatfs_args {
4385 struct fhandle *u_fhp;
4390 sys_fhstatfs(td, uap)
4392 struct fhstatfs_args /* {
4393 struct fhandle *u_fhp;
4401 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4404 error = kern_fhstatfs(td, fh, &sf);
4407 return (copyout(&sf, uap->buf, sizeof(sf)));
4411 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4418 error = priv_check(td, PRIV_VFS_FHSTATFS);
4421 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4423 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4429 error = prison_canseemount(td->td_ucred, mp);
4433 error = mac_mount_check_stat(td->td_ucred, mp);
4438 * Set these in case the underlying filesystem fails to do so.
4441 sp->f_version = STATFS_VERSION;
4442 sp->f_namemax = NAME_MAX;
4443 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4444 error = VFS_STATFS(mp, sp);
4453 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4458 cap_rights_t rights;
4459 off_t olen, ooffset;
4462 if (offset < 0 || len <= 0)
4464 /* Check for wrap. */
4465 if (offset > OFF_MAX - len)
4467 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4470 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4474 if ((fp->f_flag & FWRITE) == 0) {
4478 if (fp->f_type != DTYPE_VNODE) {
4483 if (vp->v_type != VREG) {
4488 /* Allocating blocks may take a long time, so iterate. */
4495 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4498 error = vn_lock(vp, LK_EXCLUSIVE);
4500 vn_finished_write(mp);
4504 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4507 error = VOP_ALLOCATE(vp, &offset, &len);
4509 vn_finished_write(mp);
4511 if (olen + ooffset != offset + len) {
4512 panic("offset + len changed from %jx/%jx to %jx/%jx",
4513 ooffset, olen, offset, len);
4515 if (error != 0 || len == 0)
4517 KASSERT(olen > len, ("Iteration did not make progress?"));
4526 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4529 td->td_retval[0] = kern_posix_fallocate(td, uap->fd, uap->offset,
4535 * Unlike madvise(2), we do not make a best effort to remember every
4536 * possible caching hint. Instead, we remember the last setting with
4537 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4538 * region of any current setting.
4541 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4544 struct fadvise_info *fa, *new;
4547 cap_rights_t rights;
4551 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4554 case POSIX_FADV_SEQUENTIAL:
4555 case POSIX_FADV_RANDOM:
4556 case POSIX_FADV_NOREUSE:
4557 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4559 case POSIX_FADV_NORMAL:
4560 case POSIX_FADV_WILLNEED:
4561 case POSIX_FADV_DONTNEED:
4567 /* XXX: CAP_POSIX_FADVISE? */
4568 error = fget(td, fd, cap_rights_init(&rights), &fp);
4571 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4575 if (fp->f_type != DTYPE_VNODE) {
4580 if (vp->v_type != VREG) {
4587 end = offset + len - 1;
4589 case POSIX_FADV_SEQUENTIAL:
4590 case POSIX_FADV_RANDOM:
4591 case POSIX_FADV_NOREUSE:
4593 * Try to merge any existing non-standard region with
4594 * this new region if possible, otherwise create a new
4595 * non-standard region for this request.
4597 mtx_pool_lock(mtxpool_sleep, fp);
4599 if (fa != NULL && fa->fa_advice == advice &&
4600 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4601 (end != OFF_MAX && fa->fa_start == end + 1) ||
4602 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4603 if (offset < fa->fa_start)
4604 fa->fa_start = offset;
4605 if (end > fa->fa_end)
4608 new->fa_advice = advice;
4609 new->fa_start = offset;
4611 new->fa_prevstart = 0;
4612 new->fa_prevend = 0;
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);