2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_capsicum.h"
41 #include "opt_compat.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
48 #include <sys/capsicum.h>
50 #include <sys/sysent.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/sysproto.h>
55 #include <sys/namei.h>
56 #include <sys/filedesc.h>
57 #include <sys/kernel.h>
58 #include <sys/fcntl.h>
60 #include <sys/filio.h>
61 #include <sys/limits.h>
62 #include <sys/linker.h>
63 #include <sys/rwlock.h>
67 #include <sys/unistd.h>
68 #include <sys/vnode.h>
71 #include <sys/dirent.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysctl.h>
76 #include <sys/ktrace.h>
79 #include <machine/stdarg.h>
81 #include <security/audit/audit.h>
82 #include <security/mac/mac_framework.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
89 #include <ufs/ufs/quota.h>
91 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
93 SDT_PROVIDER_DEFINE(vfs);
94 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
95 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
97 static int kern_chflagsat(struct thread *td, int fd, const char *path,
98 enum uio_seg pathseg, u_long flags, int atflag);
99 static int setfflags(struct thread *td, struct vnode *, u_long);
100 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
101 static int getutimens(const struct timespec *, enum uio_seg,
102 struct timespec *, int *);
103 static int setutimes(struct thread *td, struct vnode *,
104 const struct timespec *, int, int);
105 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
109 * Sync each mounted filesystem.
111 #ifndef _SYS_SYSPROTO_H_
120 struct sync_args *uap;
122 struct mount *mp, *nmp;
125 mtx_lock(&mountlist_mtx);
126 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
127 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
128 nmp = TAILQ_NEXT(mp, mnt_list);
131 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
132 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
133 save = curthread_pflags_set(TDP_SYNCIO);
134 vfs_msync(mp, MNT_NOWAIT);
135 VFS_SYNC(mp, MNT_NOWAIT);
136 curthread_pflags_restore(save);
137 vn_finished_write(mp);
139 mtx_lock(&mountlist_mtx);
140 nmp = TAILQ_NEXT(mp, mnt_list);
143 mtx_unlock(&mountlist_mtx);
148 * Change filesystem quotas.
150 #ifndef _SYS_SYSPROTO_H_
151 struct quotactl_args {
159 sys_quotactl(td, uap)
161 register struct quotactl_args /* {
172 AUDIT_ARG_CMD(uap->cmd);
173 AUDIT_ARG_UID(uap->uid);
174 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
176 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
178 if ((error = namei(&nd)) != 0)
180 NDFREE(&nd, NDF_ONLY_PNBUF);
181 mp = nd.ni_vp->v_mount;
184 error = vfs_busy(mp, 0);
188 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
191 * Since quota on operation typically needs to open quota
192 * file, the Q_QUOTAON handler needs to unbusy the mount point
193 * before calling into namei. Otherwise, unmount might be
194 * started between two vfs_busy() invocations (first is our,
195 * second is from mount point cross-walk code in lookup()),
198 * Require that Q_QUOTAON handles the vfs_busy() reference on
199 * its own, always returning with ubusied mount point.
201 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
207 * Used by statfs conversion routines to scale the block size up if
208 * necessary so that all of the block counts are <= 'max_size'. Note
209 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
213 statfs_scale_blocks(struct statfs *sf, long max_size)
218 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
221 * Attempt to scale the block counts to give a more accurate
222 * overview to userland of the ratio of free space to used
223 * space. To do this, find the largest block count and compute
224 * a divisor that lets it fit into a signed integer <= max_size.
226 if (sf->f_bavail < 0)
227 count = -sf->f_bavail;
229 count = sf->f_bavail;
230 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
231 if (count <= max_size)
234 count >>= flsl(max_size);
241 sf->f_bsize <<= shift;
242 sf->f_blocks >>= shift;
243 sf->f_bfree >>= shift;
244 sf->f_bavail >>= shift;
248 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
255 error = vfs_busy(mp, 0);
260 error = mac_mount_check_stat(td->td_ucred, mp);
265 * Set these in case the underlying filesystem fails to do so.
268 sp->f_version = STATFS_VERSION;
269 sp->f_namemax = NAME_MAX;
270 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
271 error = VFS_STATFS(mp, sp);
275 if (priv_check(td, PRIV_VFS_GENERATION)) {
276 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
277 prison_enforce_statfs(td->td_ucred, mp, buf);
285 * Get filesystem statistics.
287 #ifndef _SYS_SYSPROTO_H_
296 register struct statfs_args /* {
304 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
306 error = copyout(&sf, uap->buf, sizeof(sf));
311 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
318 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
323 mp = nd.ni_vp->v_mount;
325 NDFREE(&nd, NDF_ONLY_PNBUF);
327 return (kern_do_statfs(td, mp, buf));
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)
366 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSTATFS), &fp);
370 vn_lock(vp, LK_SHARED | LK_RETRY);
372 AUDIT_ARG_VNODE1(vp);
379 return (kern_do_statfs(td, mp, buf));
383 * Get statistics on all filesystems.
385 #ifndef _SYS_SYSPROTO_H_
386 struct getfsstat_args {
393 sys_getfsstat(td, uap)
395 register struct getfsstat_args /* {
404 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
406 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
407 UIO_USERSPACE, uap->flags);
409 td->td_retval[0] = count;
414 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
415 * The caller is responsible for freeing memory which will be allocated
419 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
420 size_t *countp, enum uio_seg bufseg, int flags)
422 struct mount *mp, *nmp;
423 struct statfs *sfsp, *sp, sb, *tofree;
424 size_t count, maxcount;
428 maxcount = bufsize / sizeof(struct statfs);
432 } else if (bufseg == UIO_USERSPACE) {
435 } else /* if (bufseg == UIO_SYSSPACE) */ {
437 mtx_lock(&mountlist_mtx);
438 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
441 mtx_unlock(&mountlist_mtx);
442 if (maxcount > count)
444 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
448 mtx_lock(&mountlist_mtx);
449 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
450 if (prison_canseemount(td->td_ucred, mp) != 0) {
451 nmp = TAILQ_NEXT(mp, mnt_list);
455 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
456 nmp = TAILQ_NEXT(mp, mnt_list);
460 if (flags == MNT_WAIT) {
461 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
463 * If vfs_busy() failed, and MBF_NOWAIT
464 * wasn't passed, then the mp is gone.
465 * Furthermore, because of MBF_MNTLSTLOCK,
466 * the mountlist_mtx was dropped. We have
467 * no other choice than to start over.
469 mtx_unlock(&mountlist_mtx);
470 free(tofree, M_TEMP);
474 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
475 nmp = TAILQ_NEXT(mp, mnt_list);
479 if (sfsp && count < maxcount) {
482 * Set these in case the underlying filesystem
485 sp->f_version = STATFS_VERSION;
486 sp->f_namemax = NAME_MAX;
487 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
489 * If MNT_NOWAIT or MNT_LAZY is specified, do not
490 * refresh the fsstat cache.
492 if (flags != MNT_LAZY && flags != MNT_NOWAIT) {
493 error = VFS_STATFS(mp, sp);
495 mtx_lock(&mountlist_mtx);
496 nmp = TAILQ_NEXT(mp, mnt_list);
501 if (priv_check(td, PRIV_VFS_GENERATION)) {
502 bcopy(sp, &sb, sizeof(sb));
503 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
504 prison_enforce_statfs(td->td_ucred, mp, &sb);
507 if (bufseg == UIO_SYSSPACE)
508 bcopy(sp, sfsp, sizeof(*sp));
509 else /* if (bufseg == UIO_USERSPACE) */ {
510 error = copyout(sp, sfsp, sizeof(*sp));
519 mtx_lock(&mountlist_mtx);
520 nmp = TAILQ_NEXT(mp, mnt_list);
523 mtx_unlock(&mountlist_mtx);
524 if (sfsp && count > maxcount)
531 #ifdef COMPAT_FREEBSD4
533 * Get old format filesystem statistics.
535 static void cvtstatfs(struct statfs *, struct ostatfs *);
537 #ifndef _SYS_SYSPROTO_H_
538 struct freebsd4_statfs_args {
544 freebsd4_statfs(td, uap)
546 struct freebsd4_statfs_args /* {
555 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
558 cvtstatfs(&sf, &osb);
559 return (copyout(&osb, uap->buf, sizeof(osb)));
563 * Get filesystem statistics.
565 #ifndef _SYS_SYSPROTO_H_
566 struct freebsd4_fstatfs_args {
572 freebsd4_fstatfs(td, uap)
574 struct freebsd4_fstatfs_args /* {
583 error = kern_fstatfs(td, uap->fd, &sf);
586 cvtstatfs(&sf, &osb);
587 return (copyout(&osb, uap->buf, sizeof(osb)));
591 * Get statistics on all filesystems.
593 #ifndef _SYS_SYSPROTO_H_
594 struct freebsd4_getfsstat_args {
601 freebsd4_getfsstat(td, uap)
603 register struct freebsd4_getfsstat_args /* {
609 struct statfs *buf, *sp;
614 if (uap->bufsize < 0)
616 count = uap->bufsize / sizeof(struct ostatfs);
617 if (count > SIZE_MAX / sizeof(struct statfs))
619 size = count * sizeof(struct statfs);
620 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
622 td->td_retval[0] = count;
625 while (count != 0 && error == 0) {
627 error = copyout(&osb, uap->buf, sizeof(osb));
638 * Implement fstatfs() for (NFS) file handles.
640 #ifndef _SYS_SYSPROTO_H_
641 struct freebsd4_fhstatfs_args {
642 struct fhandle *u_fhp;
647 freebsd4_fhstatfs(td, uap)
649 struct freebsd4_fhstatfs_args /* {
650 struct fhandle *u_fhp;
659 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
662 error = kern_fhstatfs(td, fh, &sf);
665 cvtstatfs(&sf, &osb);
666 return (copyout(&osb, uap->buf, sizeof(osb)));
670 * Convert a new format statfs structure to an old format statfs structure.
678 statfs_scale_blocks(nsp, LONG_MAX);
679 bzero(osp, sizeof(*osp));
680 osp->f_bsize = nsp->f_bsize;
681 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
682 osp->f_blocks = nsp->f_blocks;
683 osp->f_bfree = nsp->f_bfree;
684 osp->f_bavail = nsp->f_bavail;
685 osp->f_files = MIN(nsp->f_files, LONG_MAX);
686 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
687 osp->f_owner = nsp->f_owner;
688 osp->f_type = nsp->f_type;
689 osp->f_flags = nsp->f_flags;
690 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
691 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
692 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
693 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
694 strlcpy(osp->f_fstypename, nsp->f_fstypename,
695 MIN(MFSNAMELEN, OMFSNAMELEN));
696 strlcpy(osp->f_mntonname, nsp->f_mntonname,
697 MIN(MNAMELEN, OMNAMELEN));
698 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
699 MIN(MNAMELEN, OMNAMELEN));
700 osp->f_fsid = nsp->f_fsid;
702 #endif /* COMPAT_FREEBSD4 */
705 * Change current working directory to a given file descriptor.
707 #ifndef _SYS_SYSPROTO_H_
715 struct fchdir_args /* {
719 struct vnode *vp, *tdp;
725 AUDIT_ARG_FD(uap->fd);
726 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
733 vn_lock(vp, LK_SHARED | LK_RETRY);
734 AUDIT_ARG_VNODE1(vp);
735 error = change_dir(vp, td);
736 while (!error && (mp = vp->v_mountedhere) != NULL) {
739 error = VFS_ROOT(mp, LK_SHARED, &tdp);
756 * Change current working directory (``.'').
758 #ifndef _SYS_SYSPROTO_H_
766 struct chdir_args /* {
771 return (kern_chdir(td, uap->path, UIO_USERSPACE));
775 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
780 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
782 if ((error = namei(&nd)) != 0)
784 if ((error = change_dir(nd.ni_vp, td)) != 0) {
786 NDFREE(&nd, NDF_ONLY_PNBUF);
789 VOP_UNLOCK(nd.ni_vp, 0);
790 NDFREE(&nd, NDF_ONLY_PNBUF);
791 pwd_chdir(td, nd.ni_vp);
796 * Change notion of root (``/'') directory.
798 #ifndef _SYS_SYSPROTO_H_
806 struct chroot_args /* {
813 error = priv_check(td, PRIV_VFS_CHROOT);
816 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
817 UIO_USERSPACE, uap->path, td);
821 error = change_dir(nd.ni_vp, td);
825 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
829 VOP_UNLOCK(nd.ni_vp, 0);
830 error = pwd_chroot(td, nd.ni_vp);
832 NDFREE(&nd, NDF_ONLY_PNBUF);
837 NDFREE(&nd, NDF_ONLY_PNBUF);
842 * Common routine for chroot and chdir. Callers must provide a locked vnode
854 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
855 if (vp->v_type != VDIR)
858 error = mac_vnode_check_chdir(td->td_ucred, vp);
862 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
866 flags_to_rights(int flags, cap_rights_t *rightsp)
869 if (flags & O_EXEC) {
870 cap_rights_set(rightsp, CAP_FEXECVE);
872 switch ((flags & O_ACCMODE)) {
874 cap_rights_set(rightsp, CAP_READ);
877 cap_rights_set(rightsp, CAP_READ);
880 cap_rights_set(rightsp, CAP_WRITE);
881 if (!(flags & (O_APPEND | O_TRUNC)))
882 cap_rights_set(rightsp, CAP_SEEK);
888 cap_rights_set(rightsp, CAP_CREATE);
891 cap_rights_set(rightsp, CAP_FTRUNCATE);
893 if (flags & (O_SYNC | O_FSYNC))
894 cap_rights_set(rightsp, CAP_FSYNC);
896 if (flags & (O_EXLOCK | O_SHLOCK))
897 cap_rights_set(rightsp, CAP_FLOCK);
901 * Check permissions, allocate an open file structure, and call the device
902 * open routine if any.
904 #ifndef _SYS_SYSPROTO_H_
914 register struct open_args /* {
921 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
922 uap->flags, uap->mode));
925 #ifndef _SYS_SYSPROTO_H_
934 sys_openat(struct thread *td, struct openat_args *uap)
937 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
942 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
945 struct proc *p = td->td_proc;
946 struct filedesc *fdp = p->p_fd;
951 int cmode, error, indx;
955 AUDIT_ARG_FFLAGS(flags);
956 AUDIT_ARG_MODE(mode);
957 /* XXX: audit dirfd */
958 cap_rights_init(&rights, CAP_LOOKUP);
959 flags_to_rights(flags, &rights);
961 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
964 if (flags & O_EXEC) {
965 if (flags & O_ACCMODE)
967 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
970 flags = FFLAGS(flags);
974 * Allocate a file structure. The descriptor to reference it
975 * is allocated and set by finstall() below.
977 error = falloc_noinstall(td, &fp);
981 * An extra reference on `fp' has been held for us by
982 * falloc_noinstall().
984 /* Set the flags early so the finit in devfs can pick them up. */
985 fp->f_flag = flags & FMASK;
986 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
987 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
989 td->td_dupfd = -1; /* XXX check for fdopen */
990 error = vn_open(&nd, &flags, cmode, fp);
993 * If the vn_open replaced the method vector, something
994 * wonderous happened deep below and we just pass it up
995 * pretending we know what we do.
997 if (error == ENXIO && fp->f_ops != &badfileops)
1001 * Handle special fdopen() case. bleh.
1003 * Don't do this for relative (capability) lookups; we don't
1004 * understand exactly what would happen, and we don't think
1005 * that it ever should.
1007 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
1008 (error == ENODEV || error == ENXIO) &&
1009 td->td_dupfd >= 0) {
1010 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1019 NDFREE(&nd, NDF_ONLY_PNBUF);
1023 * Store the vnode, for any f_type. Typically, the vnode use
1024 * count is decremented by direct call to vn_closefile() for
1025 * files that switched type in the cdevsw fdopen() method.
1029 * If the file wasn't claimed by devfs bind it to the normal
1030 * vnode operations here.
1032 if (fp->f_ops == &badfileops) {
1033 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1035 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1036 DTYPE_VNODE, vp, &vnops);
1040 if (flags & O_TRUNC) {
1041 error = fo_truncate(fp, 0, td->td_ucred, td);
1047 * If we haven't already installed the FD (for dupfdopen), do so now.
1050 struct filecaps *fcaps;
1053 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1054 fcaps = &nd.ni_filecaps;
1058 error = finstall(td, fp, &indx, flags, fcaps);
1059 /* On success finstall() consumes fcaps. */
1061 filecaps_free(&nd.ni_filecaps);
1065 filecaps_free(&nd.ni_filecaps);
1069 * Release our private reference, leaving the one associated with
1070 * the descriptor table intact.
1073 td->td_retval[0] = indx;
1076 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1085 #ifndef _SYS_SYSPROTO_H_
1086 struct ocreat_args {
1094 register struct ocreat_args /* {
1100 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1101 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1103 #endif /* COMPAT_43 */
1106 * Create a special file.
1108 #ifndef _SYS_SYSPROTO_H_
1118 register struct mknod_args /* {
1125 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1126 uap->mode, uap->dev));
1129 #ifndef _SYS_SYSPROTO_H_
1130 struct mknodat_args {
1138 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1141 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1146 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1152 struct nameidata nd;
1153 cap_rights_t rights;
1154 int error, whiteout = 0;
1156 AUDIT_ARG_MODE(mode);
1158 switch (mode & S_IFMT) {
1161 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1162 if (error == 0 && dev == VNOVAL)
1166 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1169 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1173 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1183 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1184 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1186 if ((error = namei(&nd)) != 0)
1190 NDFREE(&nd, NDF_ONLY_PNBUF);
1191 if (vp == nd.ni_dvp)
1199 vattr.va_mode = (mode & ALLPERMS) &
1200 ~td->td_proc->p_fd->fd_cmask;
1201 vattr.va_rdev = dev;
1204 switch (mode & S_IFMT) {
1205 case S_IFMT: /* used by badsect to flag bad sectors */
1206 vattr.va_type = VBAD;
1209 vattr.va_type = VCHR;
1212 vattr.va_type = VBLK;
1218 panic("kern_mknod: invalid mode");
1221 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1222 NDFREE(&nd, NDF_ONLY_PNBUF);
1224 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1229 if (error == 0 && !whiteout)
1230 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1231 &nd.ni_cnd, &vattr);
1235 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1237 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1238 &nd.ni_cnd, &vattr);
1243 NDFREE(&nd, NDF_ONLY_PNBUF);
1245 vn_finished_write(mp);
1250 * Create a named pipe.
1252 #ifndef _SYS_SYSPROTO_H_
1253 struct mkfifo_args {
1261 register struct mkfifo_args /* {
1267 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1271 #ifndef _SYS_SYSPROTO_H_
1272 struct mkfifoat_args {
1279 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1282 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1287 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1292 struct nameidata nd;
1293 cap_rights_t rights;
1296 AUDIT_ARG_MODE(mode);
1299 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1300 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1302 if ((error = namei(&nd)) != 0)
1304 if (nd.ni_vp != NULL) {
1305 NDFREE(&nd, NDF_ONLY_PNBUF);
1306 if (nd.ni_vp == nd.ni_dvp)
1313 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1314 NDFREE(&nd, NDF_ONLY_PNBUF);
1316 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1321 vattr.va_type = VFIFO;
1322 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1324 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1329 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1336 vn_finished_write(mp);
1337 NDFREE(&nd, NDF_ONLY_PNBUF);
1342 * Make a hard file link.
1344 #ifndef _SYS_SYSPROTO_H_
1353 register struct link_args /* {
1359 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1360 UIO_USERSPACE, FOLLOW));
1363 #ifndef _SYS_SYSPROTO_H_
1364 struct linkat_args {
1373 sys_linkat(struct thread *td, struct linkat_args *uap)
1378 if (flag & ~AT_SYMLINK_FOLLOW)
1381 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1382 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1385 int hardlink_check_uid = 0;
1386 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1387 &hardlink_check_uid, 0,
1388 "Unprivileged processes cannot create hard links to files owned by other "
1390 static int hardlink_check_gid = 0;
1391 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1392 &hardlink_check_gid, 0,
1393 "Unprivileged processes cannot create hard links to files owned by other "
1397 can_hardlink(struct vnode *vp, struct ucred *cred)
1402 if (!hardlink_check_uid && !hardlink_check_gid)
1405 error = VOP_GETATTR(vp, &va, cred);
1409 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1410 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1415 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1416 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1425 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1426 enum uio_seg segflg, int follow)
1430 struct nameidata nd;
1431 cap_rights_t rights;
1436 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1437 cap_rights_init(&rights, CAP_LINKAT_SOURCE), td);
1439 if ((error = namei(&nd)) != 0)
1441 NDFREE(&nd, NDF_ONLY_PNBUF);
1443 if (vp->v_type == VDIR) {
1445 return (EPERM); /* POSIX */
1447 NDINIT_ATRIGHTS(&nd, CREATE,
1448 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1449 cap_rights_init(&rights, CAP_LINKAT_TARGET), td);
1450 if ((error = namei(&nd)) == 0) {
1451 if (nd.ni_vp != NULL) {
1452 NDFREE(&nd, NDF_ONLY_PNBUF);
1453 if (nd.ni_dvp == nd.ni_vp)
1460 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1462 * Cross-device link. No need to recheck
1463 * vp->v_type, since it cannot change, except
1466 NDFREE(&nd, NDF_ONLY_PNBUF);
1470 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1471 error = can_hardlink(vp, td->td_ucred);
1474 error = mac_vnode_check_link(td->td_ucred,
1475 nd.ni_dvp, vp, &nd.ni_cnd);
1480 NDFREE(&nd, NDF_ONLY_PNBUF);
1483 error = vn_start_write(vp, &mp, V_NOWAIT);
1487 NDFREE(&nd, NDF_ONLY_PNBUF);
1488 error = vn_start_write(NULL, &mp,
1494 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1497 vn_finished_write(mp);
1498 NDFREE(&nd, NDF_ONLY_PNBUF);
1501 NDFREE(&nd, NDF_ONLY_PNBUF);
1511 * Make a symbolic link.
1513 #ifndef _SYS_SYSPROTO_H_
1514 struct symlink_args {
1520 sys_symlink(td, uap)
1522 register struct symlink_args /* {
1528 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1532 #ifndef _SYS_SYSPROTO_H_
1533 struct symlinkat_args {
1540 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1543 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1548 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1549 enum uio_seg segflg)
1554 struct nameidata nd;
1556 cap_rights_t rights;
1558 if (segflg == UIO_SYSSPACE) {
1561 syspath = uma_zalloc(namei_zone, M_WAITOK);
1562 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1565 AUDIT_ARG_TEXT(syspath);
1568 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1569 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1571 if ((error = namei(&nd)) != 0)
1574 NDFREE(&nd, NDF_ONLY_PNBUF);
1575 if (nd.ni_vp == nd.ni_dvp)
1583 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1584 NDFREE(&nd, NDF_ONLY_PNBUF);
1586 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1591 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1593 vattr.va_type = VLNK;
1594 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1599 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1605 NDFREE(&nd, NDF_ONLY_PNBUF);
1607 vn_finished_write(mp);
1609 if (segflg != UIO_SYSSPACE)
1610 uma_zfree(namei_zone, syspath);
1615 * Delete a whiteout from the filesystem.
1618 sys_undelete(td, uap)
1620 register struct undelete_args /* {
1625 struct nameidata nd;
1630 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1631 UIO_USERSPACE, uap->path, td);
1636 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1637 NDFREE(&nd, NDF_ONLY_PNBUF);
1638 if (nd.ni_vp == nd.ni_dvp)
1646 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1647 NDFREE(&nd, NDF_ONLY_PNBUF);
1649 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1653 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1654 NDFREE(&nd, NDF_ONLY_PNBUF);
1656 vn_finished_write(mp);
1661 * Delete a name from the filesystem.
1663 #ifndef _SYS_SYSPROTO_H_
1664 struct unlink_args {
1671 struct unlink_args /* {
1676 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1679 #ifndef _SYS_SYSPROTO_H_
1680 struct unlinkat_args {
1687 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1689 int flag = uap->flag;
1691 char *path = uap->path;
1693 if (flag & ~AT_REMOVEDIR)
1696 if (flag & AT_REMOVEDIR)
1697 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1699 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1703 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1708 struct nameidata nd;
1710 cap_rights_t rights;
1715 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1716 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1717 if ((error = namei(&nd)) != 0)
1718 return (error == EINVAL ? EPERM : error);
1720 if (vp->v_type == VDIR && oldinum == 0) {
1721 error = EPERM; /* POSIX */
1722 } else if (oldinum != 0 &&
1723 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1724 sb.st_ino != oldinum) {
1725 error = EIDRM; /* Identifier removed */
1728 * The root of a mounted filesystem cannot be deleted.
1730 * XXX: can this only be a VDIR case?
1732 if (vp->v_vflag & VV_ROOT)
1736 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1737 NDFREE(&nd, NDF_ONLY_PNBUF);
1739 if (vp == nd.ni_dvp)
1743 if ((error = vn_start_write(NULL, &mp,
1744 V_XSLEEP | PCATCH)) != 0)
1749 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1754 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1755 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1759 vn_finished_write(mp);
1761 NDFREE(&nd, NDF_ONLY_PNBUF);
1763 if (vp == nd.ni_dvp)
1771 * Reposition read/write file offset.
1773 #ifndef _SYS_SYSPROTO_H_
1784 register struct lseek_args /* {
1792 cap_rights_t rights;
1795 AUDIT_ARG_FD(uap->fd);
1796 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1799 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1800 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1805 #if defined(COMPAT_43)
1807 * Reposition read/write file offset.
1809 #ifndef _SYS_SYSPROTO_H_
1810 struct olseek_args {
1819 register struct olseek_args /* {
1825 struct lseek_args /* {
1833 nuap.offset = uap->offset;
1834 nuap.whence = uap->whence;
1835 return (sys_lseek(td, &nuap));
1837 #endif /* COMPAT_43 */
1839 #if defined(COMPAT_FREEBSD6)
1840 /* Version with the 'pad' argument */
1842 freebsd6_lseek(td, uap)
1844 register struct freebsd6_lseek_args *uap;
1846 struct lseek_args ouap;
1849 ouap.offset = uap->offset;
1850 ouap.whence = uap->whence;
1851 return (sys_lseek(td, &ouap));
1856 * Check access permissions using passed credentials.
1859 vn_access(vp, user_flags, cred, td)
1868 /* Flags == 0 means only check for existence. */
1869 if (user_flags == 0)
1873 if (user_flags & R_OK)
1875 if (user_flags & W_OK)
1877 if (user_flags & X_OK)
1880 error = mac_vnode_check_access(cred, vp, accmode);
1884 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1885 error = VOP_ACCESS(vp, accmode, cred, td);
1890 * Check access permissions using "real" credentials.
1892 #ifndef _SYS_SYSPROTO_H_
1893 struct access_args {
1901 register struct access_args /* {
1907 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1911 #ifndef _SYS_SYSPROTO_H_
1912 struct faccessat_args {
1920 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1923 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1928 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1929 int flag, int amode)
1931 struct ucred *cred, *usecred;
1933 struct nameidata nd;
1934 cap_rights_t rights;
1937 if (flag & ~AT_EACCESS)
1939 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1943 * Create and modify a temporary credential instead of one that
1944 * is potentially shared (if we need one).
1946 cred = td->td_ucred;
1947 if ((flag & AT_EACCESS) == 0 &&
1948 ((cred->cr_uid != cred->cr_ruid ||
1949 cred->cr_rgid != cred->cr_groups[0]))) {
1950 usecred = crdup(cred);
1951 usecred->cr_uid = cred->cr_ruid;
1952 usecred->cr_groups[0] = cred->cr_rgid;
1953 td->td_ucred = usecred;
1956 AUDIT_ARG_VALUE(amode);
1957 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1958 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
1960 if ((error = namei(&nd)) != 0)
1964 error = vn_access(vp, amode, usecred, td);
1965 NDFREE(&nd, NDF_ONLY_PNBUF);
1968 if (usecred != cred) {
1969 td->td_ucred = cred;
1976 * Check access permissions using "effective" credentials.
1978 #ifndef _SYS_SYSPROTO_H_
1979 struct eaccess_args {
1985 sys_eaccess(td, uap)
1987 register struct eaccess_args /* {
1993 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1994 AT_EACCESS, uap->amode));
1997 #if defined(COMPAT_43)
1999 * Get file status; this version follows links.
2001 #ifndef _SYS_SYSPROTO_H_
2010 register struct ostat_args /* {
2019 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2024 return (copyout(&osb, uap->ub, sizeof (osb)));
2028 * Get file status; this version does not follow links.
2030 #ifndef _SYS_SYSPROTO_H_
2031 struct olstat_args {
2039 register struct olstat_args /* {
2048 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2049 UIO_USERSPACE, &sb, NULL);
2053 return (copyout(&osb, uap->ub, sizeof (osb)));
2057 * Convert from an old to a new stat structure.
2065 bzero(ost, sizeof(*ost));
2066 ost->st_dev = st->st_dev;
2067 ost->st_ino = st->st_ino;
2068 ost->st_mode = st->st_mode;
2069 ost->st_nlink = st->st_nlink;
2070 ost->st_uid = st->st_uid;
2071 ost->st_gid = st->st_gid;
2072 ost->st_rdev = st->st_rdev;
2073 if (st->st_size < (quad_t)1 << 32)
2074 ost->st_size = st->st_size;
2077 ost->st_atim = st->st_atim;
2078 ost->st_mtim = st->st_mtim;
2079 ost->st_ctim = st->st_ctim;
2080 ost->st_blksize = st->st_blksize;
2081 ost->st_blocks = st->st_blocks;
2082 ost->st_flags = st->st_flags;
2083 ost->st_gen = st->st_gen;
2085 #endif /* COMPAT_43 */
2088 * Get file status; this version follows links.
2090 #ifndef _SYS_SYSPROTO_H_
2099 register struct stat_args /* {
2107 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2110 error = copyout(&sb, uap->ub, sizeof (sb));
2114 #ifndef _SYS_SYSPROTO_H_
2115 struct fstatat_args {
2123 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2128 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2129 UIO_USERSPACE, &sb, NULL);
2131 error = copyout(&sb, uap->buf, sizeof (sb));
2136 kern_statat(struct thread *td, int flag, int fd, char *path,
2137 enum uio_seg pathseg, struct stat *sbp,
2138 void (*hook)(struct vnode *vp, struct stat *sbp))
2140 struct nameidata nd;
2142 cap_rights_t rights;
2145 if (flag & ~AT_SYMLINK_NOFOLLOW)
2148 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2149 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2150 cap_rights_init(&rights, CAP_FSTAT), td);
2152 if ((error = namei(&nd)) != 0)
2154 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2156 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2157 if (S_ISREG(sb.st_mode))
2158 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2159 if (__predict_false(hook != NULL))
2160 hook(nd.ni_vp, &sb);
2162 NDFREE(&nd, NDF_ONLY_PNBUF);
2168 if (KTRPOINT(td, KTR_STRUCT))
2175 * Get file status; this version does not follow links.
2177 #ifndef _SYS_SYSPROTO_H_
2186 register struct lstat_args /* {
2194 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2195 UIO_USERSPACE, &sb, NULL);
2197 error = copyout(&sb, uap->ub, sizeof (sb));
2202 * Implementation of the NetBSD [l]stat() functions.
2210 bzero(nsb, sizeof *nsb);
2211 nsb->st_dev = sb->st_dev;
2212 nsb->st_ino = sb->st_ino;
2213 nsb->st_mode = sb->st_mode;
2214 nsb->st_nlink = sb->st_nlink;
2215 nsb->st_uid = sb->st_uid;
2216 nsb->st_gid = sb->st_gid;
2217 nsb->st_rdev = sb->st_rdev;
2218 nsb->st_atim = sb->st_atim;
2219 nsb->st_mtim = sb->st_mtim;
2220 nsb->st_ctim = sb->st_ctim;
2221 nsb->st_size = sb->st_size;
2222 nsb->st_blocks = sb->st_blocks;
2223 nsb->st_blksize = sb->st_blksize;
2224 nsb->st_flags = sb->st_flags;
2225 nsb->st_gen = sb->st_gen;
2226 nsb->st_birthtim = sb->st_birthtim;
2229 #ifndef _SYS_SYSPROTO_H_
2238 register struct nstat_args /* {
2247 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2251 cvtnstat(&sb, &nsb);
2252 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2256 * NetBSD lstat. Get file status; this version does not follow links.
2258 #ifndef _SYS_SYSPROTO_H_
2267 register struct nlstat_args /* {
2276 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2277 UIO_USERSPACE, &sb, NULL);
2280 cvtnstat(&sb, &nsb);
2281 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2285 * Get configurable pathname variables.
2287 #ifndef _SYS_SYSPROTO_H_
2288 struct pathconf_args {
2294 sys_pathconf(td, uap)
2296 register struct pathconf_args /* {
2302 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2305 #ifndef _SYS_SYSPROTO_H_
2306 struct lpathconf_args {
2312 sys_lpathconf(td, uap)
2314 register struct lpathconf_args /* {
2320 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2325 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2328 struct nameidata nd;
2331 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2333 if ((error = namei(&nd)) != 0)
2335 NDFREE(&nd, NDF_ONLY_PNBUF);
2337 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2343 * Return target name of a symbolic link.
2345 #ifndef _SYS_SYSPROTO_H_
2346 struct readlink_args {
2353 sys_readlink(td, uap)
2355 register struct readlink_args /* {
2362 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2363 uap->buf, UIO_USERSPACE, uap->count));
2365 #ifndef _SYS_SYSPROTO_H_
2366 struct readlinkat_args {
2374 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2377 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2378 uap->buf, UIO_USERSPACE, uap->bufsize));
2382 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2383 char *buf, enum uio_seg bufseg, size_t count)
2388 struct nameidata nd;
2391 if (count > IOSIZE_MAX)
2394 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2395 pathseg, path, fd, td);
2397 if ((error = namei(&nd)) != 0)
2399 NDFREE(&nd, NDF_ONLY_PNBUF);
2402 error = mac_vnode_check_readlink(td->td_ucred, vp);
2408 if (vp->v_type != VLNK)
2411 aiov.iov_base = buf;
2412 aiov.iov_len = count;
2413 auio.uio_iov = &aiov;
2414 auio.uio_iovcnt = 1;
2415 auio.uio_offset = 0;
2416 auio.uio_rw = UIO_READ;
2417 auio.uio_segflg = bufseg;
2419 auio.uio_resid = count;
2420 error = VOP_READLINK(vp, &auio, td->td_ucred);
2421 td->td_retval[0] = count - auio.uio_resid;
2428 * Common implementation code for chflags() and fchflags().
2431 setfflags(td, vp, flags)
2440 /* We can't support the value matching VNOVAL. */
2441 if (flags == VNOVAL)
2442 return (EOPNOTSUPP);
2445 * Prevent non-root users from setting flags on devices. When
2446 * a device is reused, users can retain ownership of the device
2447 * if they are allowed to set flags and programs assume that
2448 * chown can't fail when done as root.
2450 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2451 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2456 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2459 vattr.va_flags = flags;
2460 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2462 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2465 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2467 vn_finished_write(mp);
2472 * Change flags of a file given a path name.
2474 #ifndef _SYS_SYSPROTO_H_
2475 struct chflags_args {
2481 sys_chflags(td, uap)
2483 register struct chflags_args /* {
2489 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2493 #ifndef _SYS_SYSPROTO_H_
2494 struct chflagsat_args {
2502 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2505 const char *path = uap->path;
2506 u_long flags = uap->flags;
2507 int atflag = uap->atflag;
2509 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2512 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2516 * Same as chflags() but doesn't follow symlinks.
2519 sys_lchflags(td, uap)
2521 register struct lchflags_args /* {
2527 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2528 uap->flags, AT_SYMLINK_NOFOLLOW));
2532 kern_chflagsat(struct thread *td, int fd, const char *path,
2533 enum uio_seg pathseg, u_long flags, int atflag)
2535 struct nameidata nd;
2536 cap_rights_t rights;
2539 AUDIT_ARG_FFLAGS(flags);
2540 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2541 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2542 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2543 if ((error = namei(&nd)) != 0)
2545 NDFREE(&nd, NDF_ONLY_PNBUF);
2546 error = setfflags(td, nd.ni_vp, flags);
2552 * Change flags of a file given a file descriptor.
2554 #ifndef _SYS_SYSPROTO_H_
2555 struct fchflags_args {
2561 sys_fchflags(td, uap)
2563 register struct fchflags_args /* {
2569 cap_rights_t rights;
2572 AUDIT_ARG_FD(uap->fd);
2573 AUDIT_ARG_FFLAGS(uap->flags);
2574 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2579 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2580 AUDIT_ARG_VNODE1(fp->f_vnode);
2581 VOP_UNLOCK(fp->f_vnode, 0);
2583 error = setfflags(td, fp->f_vnode, uap->flags);
2589 * Common implementation code for chmod(), lchmod() and fchmod().
2592 setfmode(td, cred, vp, mode)
2602 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2604 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2606 vattr.va_mode = mode & ALLPERMS;
2608 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2611 error = VOP_SETATTR(vp, &vattr, cred);
2613 vn_finished_write(mp);
2618 * Change mode of a file given path name.
2620 #ifndef _SYS_SYSPROTO_H_
2629 register struct chmod_args /* {
2635 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2639 #ifndef _SYS_SYSPROTO_H_
2640 struct fchmodat_args {
2648 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2650 int flag = uap->flag;
2652 char *path = uap->path;
2653 mode_t mode = uap->mode;
2655 if (flag & ~AT_SYMLINK_NOFOLLOW)
2658 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2662 * Change mode of a file given path name (don't follow links.)
2664 #ifndef _SYS_SYSPROTO_H_
2665 struct lchmod_args {
2673 register struct lchmod_args /* {
2679 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2680 uap->mode, AT_SYMLINK_NOFOLLOW));
2684 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2685 mode_t mode, int flag)
2687 struct nameidata nd;
2688 cap_rights_t rights;
2691 AUDIT_ARG_MODE(mode);
2692 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2693 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2694 cap_rights_init(&rights, CAP_FCHMOD), td);
2695 if ((error = namei(&nd)) != 0)
2697 NDFREE(&nd, NDF_ONLY_PNBUF);
2698 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2704 * Change mode of a file given a file descriptor.
2706 #ifndef _SYS_SYSPROTO_H_
2707 struct fchmod_args {
2713 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2716 cap_rights_t rights;
2719 AUDIT_ARG_FD(uap->fd);
2720 AUDIT_ARG_MODE(uap->mode);
2722 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2725 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2731 * Common implementation for chown(), lchown(), and fchown()
2734 setfown(td, cred, vp, uid, gid)
2745 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2747 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2752 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2756 error = VOP_SETATTR(vp, &vattr, cred);
2758 vn_finished_write(mp);
2763 * Set ownership given a path name.
2765 #ifndef _SYS_SYSPROTO_H_
2775 register struct chown_args /* {
2782 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2786 #ifndef _SYS_SYSPROTO_H_
2787 struct fchownat_args {
2796 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2801 if (flag & ~AT_SYMLINK_NOFOLLOW)
2804 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2805 uap->gid, uap->flag));
2809 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2810 int uid, int gid, int flag)
2812 struct nameidata nd;
2813 cap_rights_t rights;
2816 AUDIT_ARG_OWNER(uid, gid);
2817 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2818 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2819 cap_rights_init(&rights, CAP_FCHOWN), td);
2821 if ((error = namei(&nd)) != 0)
2823 NDFREE(&nd, NDF_ONLY_PNBUF);
2824 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2830 * Set ownership given a path name, do not cross symlinks.
2832 #ifndef _SYS_SYSPROTO_H_
2833 struct lchown_args {
2842 register struct lchown_args /* {
2849 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2850 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2854 * Set ownership given a file descriptor.
2856 #ifndef _SYS_SYSPROTO_H_
2857 struct fchown_args {
2866 register struct fchown_args /* {
2873 cap_rights_t rights;
2876 AUDIT_ARG_FD(uap->fd);
2877 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2878 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2881 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2887 * Common implementation code for utimes(), lutimes(), and futimes().
2890 getutimes(usrtvp, tvpseg, tsp)
2891 const struct timeval *usrtvp;
2892 enum uio_seg tvpseg;
2893 struct timespec *tsp;
2895 struct timeval tv[2];
2896 const struct timeval *tvp;
2899 if (usrtvp == NULL) {
2900 vfs_timestamp(&tsp[0]);
2903 if (tvpseg == UIO_SYSSPACE) {
2906 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2911 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2912 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2914 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2915 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2921 * Common implementation code for futimens(), utimensat().
2923 #define UTIMENS_NULL 0x1
2924 #define UTIMENS_EXIT 0x2
2926 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2927 struct timespec *tsp, int *retflags)
2929 struct timespec tsnow;
2932 vfs_timestamp(&tsnow);
2934 if (usrtsp == NULL) {
2937 *retflags |= UTIMENS_NULL;
2940 if (tspseg == UIO_SYSSPACE) {
2943 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2945 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2946 *retflags |= UTIMENS_EXIT;
2947 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2948 *retflags |= UTIMENS_NULL;
2949 if (tsp[0].tv_nsec == UTIME_OMIT)
2950 tsp[0].tv_sec = VNOVAL;
2951 else if (tsp[0].tv_nsec == UTIME_NOW)
2953 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2955 if (tsp[1].tv_nsec == UTIME_OMIT)
2956 tsp[1].tv_sec = VNOVAL;
2957 else if (tsp[1].tv_nsec == UTIME_NOW)
2959 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2966 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2970 setutimes(td, vp, ts, numtimes, nullflag)
2973 const struct timespec *ts;
2979 int error, setbirthtime;
2981 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2983 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2985 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
2986 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2989 vattr.va_atime = ts[0];
2990 vattr.va_mtime = ts[1];
2992 vattr.va_birthtime = ts[1];
2994 vattr.va_birthtime = ts[2];
2996 vattr.va_vaflags |= VA_UTIMES_NULL;
2998 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3002 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3004 vn_finished_write(mp);
3009 * Set the access and modification times of a file.
3011 #ifndef _SYS_SYSPROTO_H_
3012 struct utimes_args {
3014 struct timeval *tptr;
3020 register struct utimes_args /* {
3022 struct timeval *tptr;
3026 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3027 uap->tptr, UIO_USERSPACE));
3030 #ifndef _SYS_SYSPROTO_H_
3031 struct futimesat_args {
3034 const struct timeval * times;
3038 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3041 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3042 uap->times, UIO_USERSPACE));
3046 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3047 struct timeval *tptr, enum uio_seg tptrseg)
3049 struct nameidata nd;
3050 struct timespec ts[2];
3051 cap_rights_t rights;
3054 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3056 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3057 cap_rights_init(&rights, CAP_FUTIMES), td);
3059 if ((error = namei(&nd)) != 0)
3061 NDFREE(&nd, NDF_ONLY_PNBUF);
3062 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3068 * Set the access and modification times of a file.
3070 #ifndef _SYS_SYSPROTO_H_
3071 struct lutimes_args {
3073 struct timeval *tptr;
3077 sys_lutimes(td, uap)
3079 register struct lutimes_args /* {
3081 struct timeval *tptr;
3085 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3090 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3091 struct timeval *tptr, enum uio_seg tptrseg)
3093 struct timespec ts[2];
3094 struct nameidata nd;
3097 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3099 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3100 if ((error = namei(&nd)) != 0)
3102 NDFREE(&nd, NDF_ONLY_PNBUF);
3103 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3109 * Set the access and modification times of a file.
3111 #ifndef _SYS_SYSPROTO_H_
3112 struct futimes_args {
3114 struct timeval *tptr;
3118 sys_futimes(td, uap)
3120 register struct futimes_args /* {
3122 struct timeval *tptr;
3126 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3130 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3131 enum uio_seg tptrseg)
3133 struct timespec ts[2];
3135 cap_rights_t rights;
3139 error = getutimes(tptr, tptrseg, ts);
3142 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3146 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3147 AUDIT_ARG_VNODE1(fp->f_vnode);
3148 VOP_UNLOCK(fp->f_vnode, 0);
3150 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3156 sys_futimens(struct thread *td, struct futimens_args *uap)
3159 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3163 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3164 enum uio_seg tptrseg)
3166 struct timespec ts[2];
3168 cap_rights_t rights;
3172 error = getutimens(tptr, tptrseg, ts, &flags);
3175 if (flags & UTIMENS_EXIT)
3177 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3181 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3182 AUDIT_ARG_VNODE1(fp->f_vnode);
3183 VOP_UNLOCK(fp->f_vnode, 0);
3185 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3191 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3194 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3195 uap->times, UIO_USERSPACE, uap->flag));
3199 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3200 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3202 struct nameidata nd;
3203 struct timespec ts[2];
3204 cap_rights_t rights;
3207 if (flag & ~AT_SYMLINK_NOFOLLOW)
3210 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3212 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3213 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3214 cap_rights_init(&rights, CAP_FUTIMES), td);
3215 if ((error = namei(&nd)) != 0)
3218 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3220 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3221 * "Search permission is denied by a component of the path prefix."
3223 NDFREE(&nd, NDF_ONLY_PNBUF);
3224 if ((flags & UTIMENS_EXIT) == 0)
3225 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3231 * Truncate a file given its path name.
3233 #ifndef _SYS_SYSPROTO_H_
3234 struct truncate_args {
3241 sys_truncate(td, uap)
3243 register struct truncate_args /* {
3250 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3254 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3260 struct nameidata nd;
3265 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3266 if ((error = namei(&nd)) != 0)
3269 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3270 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3271 vn_rangelock_unlock(vp, rl_cookie);
3275 NDFREE(&nd, NDF_ONLY_PNBUF);
3276 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3277 if (vp->v_type == VDIR)
3280 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3283 else if ((error = vn_writechk(vp)) == 0 &&
3284 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3286 vattr.va_size = length;
3287 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3290 vn_finished_write(mp);
3291 vn_rangelock_unlock(vp, rl_cookie);
3296 #if defined(COMPAT_43)
3298 * Truncate a file given its path name.
3300 #ifndef _SYS_SYSPROTO_H_
3301 struct otruncate_args {
3309 register struct otruncate_args /* {
3314 struct truncate_args /* {
3320 nuap.path = uap->path;
3321 nuap.length = uap->length;
3322 return (sys_truncate(td, &nuap));
3324 #endif /* COMPAT_43 */
3326 #if defined(COMPAT_FREEBSD6)
3327 /* Versions with the pad argument */
3329 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3331 struct truncate_args ouap;
3333 ouap.path = uap->path;
3334 ouap.length = uap->length;
3335 return (sys_truncate(td, &ouap));
3339 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3341 struct ftruncate_args ouap;
3344 ouap.length = uap->length;
3345 return (sys_ftruncate(td, &ouap));
3350 kern_fsync(struct thread *td, int fd, bool fullsync)
3355 cap_rights_t rights;
3356 int error, lock_flags;
3359 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3365 /* XXXKIB: compete outstanding aio writes */;
3367 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3370 if (MNT_SHARED_WRITES(mp) ||
3371 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3372 lock_flags = LK_SHARED;
3374 lock_flags = LK_EXCLUSIVE;
3376 vn_lock(vp, lock_flags | LK_RETRY);
3377 AUDIT_ARG_VNODE1(vp);
3378 if (vp->v_object != NULL) {
3379 VM_OBJECT_WLOCK(vp->v_object);
3380 vm_object_page_clean(vp->v_object, 0, 0, 0);
3381 VM_OBJECT_WUNLOCK(vp->v_object);
3383 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3385 vn_finished_write(mp);
3392 * Sync an open file.
3394 #ifndef _SYS_SYSPROTO_H_
3400 sys_fsync(struct thread *td, struct fsync_args *uap)
3403 return (kern_fsync(td, uap->fd, true));
3407 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3410 return (kern_fsync(td, uap->fd, false));
3414 * Rename files. Source and destination must either both be directories, or
3415 * both not be directories. If target is a directory, it must be empty.
3417 #ifndef _SYS_SYSPROTO_H_
3418 struct rename_args {
3426 register struct rename_args /* {
3432 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3433 uap->to, UIO_USERSPACE));
3436 #ifndef _SYS_SYSPROTO_H_
3437 struct renameat_args {
3445 sys_renameat(struct thread *td, struct renameat_args *uap)
3448 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3453 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3454 enum uio_seg pathseg)
3456 struct mount *mp = NULL;
3457 struct vnode *tvp, *fvp, *tdvp;
3458 struct nameidata fromnd, tond;
3459 cap_rights_t rights;
3465 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3466 AUDITVNODE1, pathseg, old, oldfd,
3467 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3469 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3470 pathseg, old, oldfd,
3471 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3474 if ((error = namei(&fromnd)) != 0)
3477 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3478 fromnd.ni_vp, &fromnd.ni_cnd);
3479 VOP_UNLOCK(fromnd.ni_dvp, 0);
3480 if (fromnd.ni_dvp != fromnd.ni_vp)
3481 VOP_UNLOCK(fromnd.ni_vp, 0);
3484 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3485 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3486 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3487 if (fromnd.ni_vp->v_type == VDIR)
3488 tond.ni_cnd.cn_flags |= WILLBEDIR;
3489 if ((error = namei(&tond)) != 0) {
3490 /* Translate error code for rename("dir1", "dir2/."). */
3491 if (error == EISDIR && fvp->v_type == VDIR)
3493 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3494 vrele(fromnd.ni_dvp);
3500 error = vn_start_write(fvp, &mp, V_NOWAIT);
3502 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3503 NDFREE(&tond, NDF_ONLY_PNBUF);
3510 vrele(fromnd.ni_dvp);
3512 vrele(tond.ni_startdir);
3513 if (fromnd.ni_startdir != NULL)
3514 vrele(fromnd.ni_startdir);
3515 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3521 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3524 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3529 if (newfd != AT_FDCWD) {
3531 * If the target already exists we require CAP_UNLINKAT
3534 error = cap_check(&tond.ni_filecaps.fc_rights,
3535 cap_rights_init(&rights, CAP_UNLINKAT));
3546 * If the source is the same as the destination (that is, if they
3547 * are links to the same vnode), then there is nothing to do.
3553 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3554 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3558 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3559 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3560 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3561 NDFREE(&tond, NDF_ONLY_PNBUF);
3563 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3564 NDFREE(&tond, NDF_ONLY_PNBUF);
3571 vrele(fromnd.ni_dvp);
3574 vrele(tond.ni_startdir);
3575 vn_finished_write(mp);
3577 if (fromnd.ni_startdir)
3578 vrele(fromnd.ni_startdir);
3585 * Make a directory file.
3587 #ifndef _SYS_SYSPROTO_H_
3596 register struct mkdir_args /* {
3602 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3606 #ifndef _SYS_SYSPROTO_H_
3607 struct mkdirat_args {
3614 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3617 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3621 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3627 struct nameidata nd;
3628 cap_rights_t rights;
3631 AUDIT_ARG_MODE(mode);
3634 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3635 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3637 nd.ni_cnd.cn_flags |= WILLBEDIR;
3638 if ((error = namei(&nd)) != 0)
3642 NDFREE(&nd, NDF_ONLY_PNBUF);
3644 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3645 * the strange behaviour of leaving the vnode unlocked
3646 * if the target is the same vnode as the parent.
3648 if (vp == nd.ni_dvp)
3655 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3656 NDFREE(&nd, NDF_ONLY_PNBUF);
3658 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3663 vattr.va_type = VDIR;
3664 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3666 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3671 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3675 NDFREE(&nd, NDF_ONLY_PNBUF);
3679 vn_finished_write(mp);
3684 * Remove a directory file.
3686 #ifndef _SYS_SYSPROTO_H_
3694 struct rmdir_args /* {
3699 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3703 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3707 struct nameidata nd;
3708 cap_rights_t rights;
3713 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3714 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3715 if ((error = namei(&nd)) != 0)
3718 if (vp->v_type != VDIR) {
3723 * No rmdir "." please.
3725 if (nd.ni_dvp == vp) {
3730 * The root of a mounted filesystem cannot be deleted.
3732 if (vp->v_vflag & VV_ROOT) {
3737 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3742 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3743 NDFREE(&nd, NDF_ONLY_PNBUF);
3745 if (nd.ni_dvp == vp)
3749 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3753 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3754 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3755 vn_finished_write(mp);
3757 NDFREE(&nd, NDF_ONLY_PNBUF);
3759 if (nd.ni_dvp == vp)
3768 * Read a block of directory entries in a filesystem independent format.
3770 #ifndef _SYS_SYSPROTO_H_
3771 struct ogetdirentries_args {
3779 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3784 error = kern_ogetdirentries(td, uap, &loff);
3786 error = copyout(&loff, uap->basep, sizeof(long));
3791 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3796 struct uio auio, kuio;
3797 struct iovec aiov, kiov;
3798 struct dirent *dp, *edp;
3799 cap_rights_t rights;
3801 int error, eofflag, readcnt;
3805 /* XXX arbitrary sanity limit on `count'. */
3806 if (uap->count > 64 * 1024)
3808 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp);
3811 if ((fp->f_flag & FREAD) == 0) {
3816 foffset = foffset_lock(fp, 0);
3818 if (vp->v_type != VDIR) {
3819 foffset_unlock(fp, foffset, 0);
3823 aiov.iov_base = uap->buf;
3824 aiov.iov_len = uap->count;
3825 auio.uio_iov = &aiov;
3826 auio.uio_iovcnt = 1;
3827 auio.uio_rw = UIO_READ;
3828 auio.uio_segflg = UIO_USERSPACE;
3830 auio.uio_resid = uap->count;
3831 vn_lock(vp, LK_SHARED | LK_RETRY);
3832 loff = auio.uio_offset = foffset;
3834 error = mac_vnode_check_readdir(td->td_ucred, vp);
3837 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3842 # if (BYTE_ORDER != LITTLE_ENDIAN)
3843 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3844 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3846 foffset = auio.uio_offset;
3851 kuio.uio_iov = &kiov;
3852 kuio.uio_segflg = UIO_SYSSPACE;
3853 kiov.iov_len = uap->count;
3854 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3855 kiov.iov_base = dirbuf;
3856 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3858 foffset = kuio.uio_offset;
3860 readcnt = uap->count - kuio.uio_resid;
3861 edp = (struct dirent *)&dirbuf[readcnt];
3862 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3863 # if (BYTE_ORDER == LITTLE_ENDIAN)
3865 * The expected low byte of
3866 * dp->d_namlen is our dp->d_type.
3867 * The high MBZ byte of dp->d_namlen
3868 * is our dp->d_namlen.
3870 dp->d_type = dp->d_namlen;
3874 * The dp->d_type is the high byte
3875 * of the expected dp->d_namlen,
3876 * so must be zero'ed.
3880 if (dp->d_reclen > 0) {
3881 dp = (struct dirent *)
3882 ((char *)dp + dp->d_reclen);
3889 error = uiomove(dirbuf, readcnt, &auio);
3891 free(dirbuf, M_TEMP);
3895 foffset_unlock(fp, foffset, 0);
3899 if (uap->count == auio.uio_resid &&
3900 (vp->v_vflag & VV_ROOT) &&
3901 (vp->v_mount->mnt_flag & MNT_UNION)) {
3902 struct vnode *tvp = vp;
3903 vp = vp->v_mount->mnt_vnodecovered;
3912 foffset_unlock(fp, foffset, 0);
3914 td->td_retval[0] = uap->count - auio.uio_resid;
3919 #endif /* COMPAT_43 */
3922 * Read a block of directory entries in a filesystem independent format.
3924 #ifndef _SYS_SYSPROTO_H_
3925 struct getdirentries_args {
3933 sys_getdirentries(td, uap)
3935 register struct getdirentries_args /* {
3945 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3946 NULL, UIO_USERSPACE);
3949 if (uap->basep != NULL)
3950 error = copyout(&base, uap->basep, sizeof(long));
3955 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3956 long *basep, ssize_t *residp, enum uio_seg bufseg)
3962 cap_rights_t rights;
3968 if (count > IOSIZE_MAX)
3970 auio.uio_resid = count;
3971 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3974 if ((fp->f_flag & FREAD) == 0) {
3979 foffset = foffset_lock(fp, 0);
3981 if (vp->v_type != VDIR) {
3985 aiov.iov_base = buf;
3986 aiov.iov_len = count;
3987 auio.uio_iov = &aiov;
3988 auio.uio_iovcnt = 1;
3989 auio.uio_rw = UIO_READ;
3990 auio.uio_segflg = bufseg;
3992 vn_lock(vp, LK_SHARED | LK_RETRY);
3993 AUDIT_ARG_VNODE1(vp);
3994 loff = auio.uio_offset = foffset;
3996 error = mac_vnode_check_readdir(td->td_ucred, vp);
3999 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4001 foffset = auio.uio_offset;
4006 if (count == auio.uio_resid &&
4007 (vp->v_vflag & VV_ROOT) &&
4008 (vp->v_mount->mnt_flag & MNT_UNION)) {
4009 struct vnode *tvp = vp;
4011 vp = vp->v_mount->mnt_vnodecovered;
4022 *residp = auio.uio_resid;
4023 td->td_retval[0] = count - auio.uio_resid;
4025 foffset_unlock(fp, foffset, 0);
4030 #ifndef _SYS_SYSPROTO_H_
4031 struct getdents_args {
4038 sys_getdents(td, uap)
4040 register struct getdents_args /* {
4046 struct getdirentries_args ap;
4050 ap.count = uap->count;
4052 return (sys_getdirentries(td, &ap));
4056 * Set the mode mask for creation of filesystem nodes.
4058 #ifndef _SYS_SYSPROTO_H_
4066 struct umask_args /* {
4070 struct filedesc *fdp;
4072 fdp = td->td_proc->p_fd;
4073 FILEDESC_XLOCK(fdp);
4074 td->td_retval[0] = fdp->fd_cmask;
4075 fdp->fd_cmask = uap->newmask & ALLPERMS;
4076 FILEDESC_XUNLOCK(fdp);
4081 * Void all references to file by ripping underlying filesystem away from
4084 #ifndef _SYS_SYSPROTO_H_
4085 struct revoke_args {
4092 register struct revoke_args /* {
4098 struct nameidata nd;
4101 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4103 if ((error = namei(&nd)) != 0)
4106 NDFREE(&nd, NDF_ONLY_PNBUF);
4107 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4112 error = mac_vnode_check_revoke(td->td_ucred, vp);
4116 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4119 if (td->td_ucred->cr_uid != vattr.va_uid) {
4120 error = priv_check(td, PRIV_VFS_ADMIN);
4125 VOP_REVOKE(vp, REVOKEALL);
4132 * Convert a user file descriptor to a kernel file entry and check that, if it
4133 * is a capability, the correct rights are present. A reference on the file
4134 * entry is held upon returning.
4137 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4142 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4147 * The file could be not of the vnode type, or it may be not
4148 * yet fully initialized, in which case the f_vnode pointer
4149 * may be set, but f_ops is still badfileops. E.g.,
4150 * devfs_open() transiently create such situation to
4151 * facilitate csw d_fdopen().
4153 * Dupfdopen() handling in kern_openat() installs the
4154 * half-baked file into the process descriptor table, allowing
4155 * other thread to dereference it. Guard against the race by
4158 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4168 * Get an (NFS) file handle.
4170 #ifndef _SYS_SYSPROTO_H_
4171 struct lgetfh_args {
4179 register struct lgetfh_args *uap;
4181 struct nameidata nd;
4183 register struct vnode *vp;
4186 error = priv_check(td, PRIV_VFS_GETFH);
4189 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4194 NDFREE(&nd, NDF_ONLY_PNBUF);
4196 bzero(&fh, sizeof(fh));
4197 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4198 error = VOP_VPTOFH(vp, &fh.fh_fid);
4201 error = copyout(&fh, uap->fhp, sizeof (fh));
4205 #ifndef _SYS_SYSPROTO_H_
4214 register struct getfh_args *uap;
4216 struct nameidata nd;
4218 register struct vnode *vp;
4221 error = priv_check(td, PRIV_VFS_GETFH);
4224 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4229 NDFREE(&nd, NDF_ONLY_PNBUF);
4231 bzero(&fh, sizeof(fh));
4232 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4233 error = VOP_VPTOFH(vp, &fh.fh_fid);
4236 error = copyout(&fh, uap->fhp, sizeof (fh));
4241 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4244 * warning: do not remove the priv_check() call or this becomes one giant
4247 #ifndef _SYS_SYSPROTO_H_
4248 struct fhopen_args {
4249 const struct fhandle *u_fhp;
4256 struct fhopen_args /* {
4257 const struct fhandle *u_fhp;
4268 error = priv_check(td, PRIV_VFS_FHOPEN);
4272 fmode = FFLAGS(uap->flags);
4273 /* why not allow a non-read/write open for our lockd? */
4274 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4276 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4279 /* find the mount point */
4280 mp = vfs_busyfs(&fhp.fh_fsid);
4283 /* now give me my vnode, it gets returned to me locked */
4284 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4289 error = falloc_noinstall(td, &fp);
4295 * An extra reference on `fp' has been held for us by
4296 * falloc_noinstall().
4302 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4304 KASSERT(fp->f_ops == &badfileops,
4305 ("VOP_OPEN in fhopen() set f_ops"));
4306 KASSERT(td->td_dupfd < 0,
4307 ("fhopen() encountered fdopen()"));
4317 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4320 if ((fmode & O_TRUNC) != 0) {
4321 error = fo_truncate(fp, 0, td->td_ucred, td);
4326 error = finstall(td, fp, &indx, fmode, NULL);
4329 td->td_retval[0] = indx;
4334 * Stat an (NFS) file handle.
4336 #ifndef _SYS_SYSPROTO_H_
4337 struct fhstat_args {
4338 struct fhandle *u_fhp;
4345 register struct fhstat_args /* {
4346 struct fhandle *u_fhp;
4354 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4357 error = kern_fhstat(td, fh, &sb);
4359 error = copyout(&sb, uap->sb, sizeof(sb));
4364 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4370 error = priv_check(td, PRIV_VFS_FHSTAT);
4373 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4375 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4379 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4385 * Implement fstatfs() for (NFS) file handles.
4387 #ifndef _SYS_SYSPROTO_H_
4388 struct fhstatfs_args {
4389 struct fhandle *u_fhp;
4394 sys_fhstatfs(td, uap)
4396 struct fhstatfs_args /* {
4397 struct fhandle *u_fhp;
4405 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4408 error = kern_fhstatfs(td, fh, &sf);
4411 return (copyout(&sf, uap->buf, sizeof(sf)));
4415 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4422 error = priv_check(td, PRIV_VFS_FHSTATFS);
4425 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4427 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4433 error = prison_canseemount(td->td_ucred, mp);
4437 error = mac_mount_check_stat(td->td_ucred, mp);
4442 * Set these in case the underlying filesystem fails to do so.
4445 sp->f_version = STATFS_VERSION;
4446 sp->f_namemax = NAME_MAX;
4447 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4448 error = VFS_STATFS(mp, sp);
4457 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4462 cap_rights_t rights;
4463 off_t olen, ooffset;
4466 if (offset < 0 || len <= 0)
4468 /* Check for wrap. */
4469 if (offset > OFF_MAX - len)
4471 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4474 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4478 if ((fp->f_flag & FWRITE) == 0) {
4482 if (fp->f_type != DTYPE_VNODE) {
4487 if (vp->v_type != VREG) {
4492 /* Allocating blocks may take a long time, so iterate. */
4499 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4502 error = vn_lock(vp, LK_EXCLUSIVE);
4504 vn_finished_write(mp);
4508 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4511 error = VOP_ALLOCATE(vp, &offset, &len);
4513 vn_finished_write(mp);
4515 if (olen + ooffset != offset + len) {
4516 panic("offset + len changed from %jx/%jx to %jx/%jx",
4517 ooffset, olen, offset, len);
4519 if (error != 0 || len == 0)
4521 KASSERT(olen > len, ("Iteration did not make progress?"));
4530 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4534 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4535 return (kern_posix_error(td, error));
4539 * Unlike madvise(2), we do not make a best effort to remember every
4540 * possible caching hint. Instead, we remember the last setting with
4541 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4542 * region of any current setting.
4545 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4548 struct fadvise_info *fa, *new;
4551 cap_rights_t rights;
4555 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4558 case POSIX_FADV_SEQUENTIAL:
4559 case POSIX_FADV_RANDOM:
4560 case POSIX_FADV_NOREUSE:
4561 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4563 case POSIX_FADV_NORMAL:
4564 case POSIX_FADV_WILLNEED:
4565 case POSIX_FADV_DONTNEED:
4571 /* XXX: CAP_POSIX_FADVISE? */
4572 error = fget(td, fd, cap_rights_init(&rights), &fp);
4575 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4579 if (fp->f_type != DTYPE_VNODE) {
4584 if (vp->v_type != VREG) {
4591 end = offset + len - 1;
4593 case POSIX_FADV_SEQUENTIAL:
4594 case POSIX_FADV_RANDOM:
4595 case POSIX_FADV_NOREUSE:
4597 * Try to merge any existing non-standard region with
4598 * this new region if possible, otherwise create a new
4599 * non-standard region for this request.
4601 mtx_pool_lock(mtxpool_sleep, fp);
4603 if (fa != NULL && fa->fa_advice == advice &&
4604 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4605 (end != OFF_MAX && fa->fa_start == end + 1) ||
4606 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4607 if (offset < fa->fa_start)
4608 fa->fa_start = offset;
4609 if (end > fa->fa_end)
4612 new->fa_advice = advice;
4613 new->fa_start = offset;
4618 mtx_pool_unlock(mtxpool_sleep, fp);
4620 case POSIX_FADV_NORMAL:
4622 * If a the "normal" region overlaps with an existing
4623 * non-standard region, trim or remove the
4624 * non-standard region.
4626 mtx_pool_lock(mtxpool_sleep, fp);
4629 if (offset <= fa->fa_start && end >= fa->fa_end) {
4631 fp->f_advice = NULL;
4632 } else if (offset <= fa->fa_start &&
4633 end >= fa->fa_start)
4634 fa->fa_start = end + 1;
4635 else if (offset <= fa->fa_end && end >= fa->fa_end)
4636 fa->fa_end = offset - 1;
4637 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4639 * If the "normal" region is a middle
4640 * portion of the existing
4641 * non-standard region, just remove
4642 * the whole thing rather than picking
4643 * one side or the other to
4647 fp->f_advice = NULL;
4650 mtx_pool_unlock(mtxpool_sleep, fp);
4652 case POSIX_FADV_WILLNEED:
4653 case POSIX_FADV_DONTNEED:
4654 error = VOP_ADVISE(vp, offset, end, advice);
4660 free(new, M_FADVISE);
4665 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4669 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4671 return (kern_posix_error(td, error));