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 * 3. 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->mode);
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 mode)
422 struct mount *mp, *nmp;
423 struct statfs *sfsp, *sp, sb, *tofree;
424 size_t count, maxcount;
432 if (bufseg == UIO_SYSSPACE)
437 maxcount = bufsize / sizeof(struct statfs);
441 } else if (bufseg == UIO_USERSPACE) {
444 } else /* if (bufseg == UIO_SYSSPACE) */ {
446 mtx_lock(&mountlist_mtx);
447 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
450 mtx_unlock(&mountlist_mtx);
451 if (maxcount > count)
453 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
457 mtx_lock(&mountlist_mtx);
458 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
459 if (prison_canseemount(td->td_ucred, mp) != 0) {
460 nmp = TAILQ_NEXT(mp, mnt_list);
464 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
465 nmp = TAILQ_NEXT(mp, mnt_list);
469 if (mode == MNT_WAIT) {
470 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
472 * If vfs_busy() failed, and MBF_NOWAIT
473 * wasn't passed, then the mp is gone.
474 * Furthermore, because of MBF_MNTLSTLOCK,
475 * the mountlist_mtx was dropped. We have
476 * no other choice than to start over.
478 mtx_unlock(&mountlist_mtx);
479 free(tofree, M_TEMP);
483 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
484 nmp = TAILQ_NEXT(mp, mnt_list);
488 if (sfsp && count < maxcount) {
491 * Set these in case the underlying filesystem
494 sp->f_version = STATFS_VERSION;
495 sp->f_namemax = NAME_MAX;
496 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
498 * If MNT_NOWAIT is specified, do not refresh
501 if (mode != MNT_NOWAIT) {
502 error = VFS_STATFS(mp, sp);
504 mtx_lock(&mountlist_mtx);
505 nmp = TAILQ_NEXT(mp, mnt_list);
510 if (priv_check(td, PRIV_VFS_GENERATION)) {
511 bcopy(sp, &sb, sizeof(sb));
512 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
513 prison_enforce_statfs(td->td_ucred, mp, &sb);
516 if (bufseg == UIO_SYSSPACE)
517 bcopy(sp, sfsp, sizeof(*sp));
518 else /* if (bufseg == UIO_USERSPACE) */ {
519 error = copyout(sp, sfsp, sizeof(*sp));
528 mtx_lock(&mountlist_mtx);
529 nmp = TAILQ_NEXT(mp, mnt_list);
532 mtx_unlock(&mountlist_mtx);
533 if (sfsp && count > maxcount)
540 #ifdef COMPAT_FREEBSD4
542 * Get old format filesystem statistics.
544 static void cvtstatfs(struct statfs *, struct ostatfs *);
546 #ifndef _SYS_SYSPROTO_H_
547 struct freebsd4_statfs_args {
553 freebsd4_statfs(td, uap)
555 struct freebsd4_statfs_args /* {
564 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
567 cvtstatfs(&sf, &osb);
568 return (copyout(&osb, uap->buf, sizeof(osb)));
572 * Get filesystem statistics.
574 #ifndef _SYS_SYSPROTO_H_
575 struct freebsd4_fstatfs_args {
581 freebsd4_fstatfs(td, uap)
583 struct freebsd4_fstatfs_args /* {
592 error = kern_fstatfs(td, uap->fd, &sf);
595 cvtstatfs(&sf, &osb);
596 return (copyout(&osb, uap->buf, sizeof(osb)));
600 * Get statistics on all filesystems.
602 #ifndef _SYS_SYSPROTO_H_
603 struct freebsd4_getfsstat_args {
610 freebsd4_getfsstat(td, uap)
612 register struct freebsd4_getfsstat_args /* {
618 struct statfs *buf, *sp;
623 if (uap->bufsize < 0)
625 count = uap->bufsize / sizeof(struct ostatfs);
626 if (count > SIZE_MAX / sizeof(struct statfs))
628 size = count * sizeof(struct statfs);
629 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
631 td->td_retval[0] = count;
634 while (count != 0 && error == 0) {
636 error = copyout(&osb, uap->buf, sizeof(osb));
647 * Implement fstatfs() for (NFS) file handles.
649 #ifndef _SYS_SYSPROTO_H_
650 struct freebsd4_fhstatfs_args {
651 struct fhandle *u_fhp;
656 freebsd4_fhstatfs(td, uap)
658 struct freebsd4_fhstatfs_args /* {
659 struct fhandle *u_fhp;
668 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
671 error = kern_fhstatfs(td, fh, &sf);
674 cvtstatfs(&sf, &osb);
675 return (copyout(&osb, uap->buf, sizeof(osb)));
679 * Convert a new format statfs structure to an old format statfs structure.
687 statfs_scale_blocks(nsp, LONG_MAX);
688 bzero(osp, sizeof(*osp));
689 osp->f_bsize = nsp->f_bsize;
690 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
691 osp->f_blocks = nsp->f_blocks;
692 osp->f_bfree = nsp->f_bfree;
693 osp->f_bavail = nsp->f_bavail;
694 osp->f_files = MIN(nsp->f_files, LONG_MAX);
695 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
696 osp->f_owner = nsp->f_owner;
697 osp->f_type = nsp->f_type;
698 osp->f_flags = nsp->f_flags;
699 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
700 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
701 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
702 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
703 strlcpy(osp->f_fstypename, nsp->f_fstypename,
704 MIN(MFSNAMELEN, OMFSNAMELEN));
705 strlcpy(osp->f_mntonname, nsp->f_mntonname,
706 MIN(MNAMELEN, OMNAMELEN));
707 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
708 MIN(MNAMELEN, OMNAMELEN));
709 osp->f_fsid = nsp->f_fsid;
711 #endif /* COMPAT_FREEBSD4 */
714 * Change current working directory to a given file descriptor.
716 #ifndef _SYS_SYSPROTO_H_
724 struct fchdir_args /* {
728 struct vnode *vp, *tdp;
734 AUDIT_ARG_FD(uap->fd);
735 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
742 vn_lock(vp, LK_SHARED | LK_RETRY);
743 AUDIT_ARG_VNODE1(vp);
744 error = change_dir(vp, td);
745 while (!error && (mp = vp->v_mountedhere) != NULL) {
748 error = VFS_ROOT(mp, LK_SHARED, &tdp);
765 * Change current working directory (``.'').
767 #ifndef _SYS_SYSPROTO_H_
775 struct chdir_args /* {
780 return (kern_chdir(td, uap->path, UIO_USERSPACE));
784 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
789 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
791 if ((error = namei(&nd)) != 0)
793 if ((error = change_dir(nd.ni_vp, td)) != 0) {
795 NDFREE(&nd, NDF_ONLY_PNBUF);
798 VOP_UNLOCK(nd.ni_vp, 0);
799 NDFREE(&nd, NDF_ONLY_PNBUF);
800 pwd_chdir(td, nd.ni_vp);
805 * Change notion of root (``/'') directory.
807 #ifndef _SYS_SYSPROTO_H_
815 struct chroot_args /* {
822 error = priv_check(td, PRIV_VFS_CHROOT);
825 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
826 UIO_USERSPACE, uap->path, td);
830 error = change_dir(nd.ni_vp, td);
834 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
838 VOP_UNLOCK(nd.ni_vp, 0);
839 error = pwd_chroot(td, nd.ni_vp);
841 NDFREE(&nd, NDF_ONLY_PNBUF);
846 NDFREE(&nd, NDF_ONLY_PNBUF);
851 * Common routine for chroot and chdir. Callers must provide a locked vnode
863 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
864 if (vp->v_type != VDIR)
867 error = mac_vnode_check_chdir(td->td_ucred, vp);
871 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
875 flags_to_rights(int flags, cap_rights_t *rightsp)
878 if (flags & O_EXEC) {
879 cap_rights_set(rightsp, CAP_FEXECVE);
881 switch ((flags & O_ACCMODE)) {
883 cap_rights_set(rightsp, CAP_READ);
886 cap_rights_set(rightsp, CAP_READ);
889 cap_rights_set(rightsp, CAP_WRITE);
890 if (!(flags & (O_APPEND | O_TRUNC)))
891 cap_rights_set(rightsp, CAP_SEEK);
897 cap_rights_set(rightsp, CAP_CREATE);
900 cap_rights_set(rightsp, CAP_FTRUNCATE);
902 if (flags & (O_SYNC | O_FSYNC))
903 cap_rights_set(rightsp, CAP_FSYNC);
905 if (flags & (O_EXLOCK | O_SHLOCK))
906 cap_rights_set(rightsp, CAP_FLOCK);
910 * Check permissions, allocate an open file structure, and call the device
911 * open routine if any.
913 #ifndef _SYS_SYSPROTO_H_
923 register struct open_args /* {
930 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
931 uap->flags, uap->mode));
934 #ifndef _SYS_SYSPROTO_H_
943 sys_openat(struct thread *td, struct openat_args *uap)
946 AUDIT_ARG_FD(uap->fd);
947 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
952 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
955 struct proc *p = td->td_proc;
956 struct filedesc *fdp = p->p_fd;
961 int cmode, error, indx;
965 AUDIT_ARG_FFLAGS(flags);
966 AUDIT_ARG_MODE(mode);
967 cap_rights_init(&rights, CAP_LOOKUP);
968 flags_to_rights(flags, &rights);
970 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
973 if (flags & O_EXEC) {
974 if (flags & O_ACCMODE)
976 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
979 flags = FFLAGS(flags);
983 * Allocate a file structure. The descriptor to reference it
984 * is allocated and set by finstall() below.
986 error = falloc_noinstall(td, &fp);
990 * An extra reference on `fp' has been held for us by
991 * falloc_noinstall().
993 /* Set the flags early so the finit in devfs can pick them up. */
994 fp->f_flag = flags & FMASK;
995 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
996 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
998 td->td_dupfd = -1; /* XXX check for fdopen */
999 error = vn_open(&nd, &flags, cmode, fp);
1002 * If the vn_open replaced the method vector, something
1003 * wonderous happened deep below and we just pass it up
1004 * pretending we know what we do.
1006 if (error == ENXIO && fp->f_ops != &badfileops)
1010 * Handle special fdopen() case. bleh.
1012 * Don't do this for relative (capability) lookups; we don't
1013 * understand exactly what would happen, and we don't think
1014 * that it ever should.
1016 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
1017 (error == ENODEV || error == ENXIO) &&
1018 td->td_dupfd >= 0) {
1019 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1028 NDFREE(&nd, NDF_ONLY_PNBUF);
1032 * Store the vnode, for any f_type. Typically, the vnode use
1033 * count is decremented by direct call to vn_closefile() for
1034 * files that switched type in the cdevsw fdopen() method.
1038 * If the file wasn't claimed by devfs bind it to the normal
1039 * vnode operations here.
1041 if (fp->f_ops == &badfileops) {
1042 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1044 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1045 DTYPE_VNODE, vp, &vnops);
1049 if (flags & O_TRUNC) {
1050 error = fo_truncate(fp, 0, td->td_ucred, td);
1056 * If we haven't already installed the FD (for dupfdopen), do so now.
1059 struct filecaps *fcaps;
1062 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1063 fcaps = &nd.ni_filecaps;
1067 error = finstall(td, fp, &indx, flags, fcaps);
1068 /* On success finstall() consumes fcaps. */
1070 filecaps_free(&nd.ni_filecaps);
1074 filecaps_free(&nd.ni_filecaps);
1078 * Release our private reference, leaving the one associated with
1079 * the descriptor table intact.
1082 td->td_retval[0] = indx;
1085 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1094 #ifndef _SYS_SYSPROTO_H_
1095 struct ocreat_args {
1103 register struct ocreat_args /* {
1109 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1110 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1112 #endif /* COMPAT_43 */
1115 * Create a special file.
1117 #ifndef _SYS_SYSPROTO_H_
1127 register struct mknod_args /* {
1134 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1135 uap->mode, uap->dev));
1138 #ifndef _SYS_SYSPROTO_H_
1139 struct mknodat_args {
1147 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1150 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1155 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1161 struct nameidata nd;
1162 cap_rights_t rights;
1163 int error, whiteout = 0;
1165 AUDIT_ARG_MODE(mode);
1167 switch (mode & S_IFMT) {
1170 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1171 if (error == 0 && dev == VNOVAL)
1175 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1178 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1182 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1192 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1193 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1195 if ((error = namei(&nd)) != 0)
1199 NDFREE(&nd, NDF_ONLY_PNBUF);
1200 if (vp == nd.ni_dvp)
1208 vattr.va_mode = (mode & ALLPERMS) &
1209 ~td->td_proc->p_fd->fd_cmask;
1210 vattr.va_rdev = dev;
1213 switch (mode & S_IFMT) {
1214 case S_IFMT: /* used by badsect to flag bad sectors */
1215 vattr.va_type = VBAD;
1218 vattr.va_type = VCHR;
1221 vattr.va_type = VBLK;
1227 panic("kern_mknod: invalid mode");
1230 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1231 NDFREE(&nd, NDF_ONLY_PNBUF);
1233 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1238 if (error == 0 && !whiteout)
1239 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1240 &nd.ni_cnd, &vattr);
1244 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1246 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1247 &nd.ni_cnd, &vattr);
1252 NDFREE(&nd, NDF_ONLY_PNBUF);
1254 vn_finished_write(mp);
1259 * Create a named pipe.
1261 #ifndef _SYS_SYSPROTO_H_
1262 struct mkfifo_args {
1270 register struct mkfifo_args /* {
1276 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1280 #ifndef _SYS_SYSPROTO_H_
1281 struct mkfifoat_args {
1288 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1291 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1296 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1301 struct nameidata nd;
1302 cap_rights_t rights;
1305 AUDIT_ARG_MODE(mode);
1308 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1309 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1311 if ((error = namei(&nd)) != 0)
1313 if (nd.ni_vp != NULL) {
1314 NDFREE(&nd, NDF_ONLY_PNBUF);
1315 if (nd.ni_vp == nd.ni_dvp)
1322 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1323 NDFREE(&nd, NDF_ONLY_PNBUF);
1325 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1330 vattr.va_type = VFIFO;
1331 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1333 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1338 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1345 vn_finished_write(mp);
1346 NDFREE(&nd, NDF_ONLY_PNBUF);
1351 * Make a hard file link.
1353 #ifndef _SYS_SYSPROTO_H_
1362 register struct link_args /* {
1368 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1369 UIO_USERSPACE, FOLLOW));
1372 #ifndef _SYS_SYSPROTO_H_
1373 struct linkat_args {
1382 sys_linkat(struct thread *td, struct linkat_args *uap)
1387 if (flag & ~AT_SYMLINK_FOLLOW)
1390 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1391 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1394 int hardlink_check_uid = 0;
1395 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1396 &hardlink_check_uid, 0,
1397 "Unprivileged processes cannot create hard links to files owned by other "
1399 static int hardlink_check_gid = 0;
1400 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1401 &hardlink_check_gid, 0,
1402 "Unprivileged processes cannot create hard links to files owned by other "
1406 can_hardlink(struct vnode *vp, struct ucred *cred)
1411 if (!hardlink_check_uid && !hardlink_check_gid)
1414 error = VOP_GETATTR(vp, &va, cred);
1418 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1419 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1424 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1425 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1434 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1435 enum uio_seg segflg, int follow)
1439 struct nameidata nd;
1440 cap_rights_t rights;
1445 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1446 cap_rights_init(&rights, CAP_LINKAT_SOURCE), td);
1448 if ((error = namei(&nd)) != 0)
1450 NDFREE(&nd, NDF_ONLY_PNBUF);
1452 if (vp->v_type == VDIR) {
1454 return (EPERM); /* POSIX */
1456 NDINIT_ATRIGHTS(&nd, CREATE,
1457 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1458 cap_rights_init(&rights, CAP_LINKAT_TARGET), td);
1459 if ((error = namei(&nd)) == 0) {
1460 if (nd.ni_vp != NULL) {
1461 NDFREE(&nd, NDF_ONLY_PNBUF);
1462 if (nd.ni_dvp == nd.ni_vp)
1469 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1471 * Cross-device link. No need to recheck
1472 * vp->v_type, since it cannot change, except
1475 NDFREE(&nd, NDF_ONLY_PNBUF);
1479 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1480 error = can_hardlink(vp, td->td_ucred);
1483 error = mac_vnode_check_link(td->td_ucred,
1484 nd.ni_dvp, vp, &nd.ni_cnd);
1489 NDFREE(&nd, NDF_ONLY_PNBUF);
1492 error = vn_start_write(vp, &mp, V_NOWAIT);
1496 NDFREE(&nd, NDF_ONLY_PNBUF);
1497 error = vn_start_write(NULL, &mp,
1503 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1506 vn_finished_write(mp);
1507 NDFREE(&nd, NDF_ONLY_PNBUF);
1510 NDFREE(&nd, NDF_ONLY_PNBUF);
1520 * Make a symbolic link.
1522 #ifndef _SYS_SYSPROTO_H_
1523 struct symlink_args {
1529 sys_symlink(td, uap)
1531 register struct symlink_args /* {
1537 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1541 #ifndef _SYS_SYSPROTO_H_
1542 struct symlinkat_args {
1549 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1552 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1557 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1558 enum uio_seg segflg)
1563 struct nameidata nd;
1565 cap_rights_t rights;
1567 if (segflg == UIO_SYSSPACE) {
1570 syspath = uma_zalloc(namei_zone, M_WAITOK);
1571 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1574 AUDIT_ARG_TEXT(syspath);
1577 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1578 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1580 if ((error = namei(&nd)) != 0)
1583 NDFREE(&nd, NDF_ONLY_PNBUF);
1584 if (nd.ni_vp == nd.ni_dvp)
1592 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1593 NDFREE(&nd, NDF_ONLY_PNBUF);
1595 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1600 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1602 vattr.va_type = VLNK;
1603 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1608 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1614 NDFREE(&nd, NDF_ONLY_PNBUF);
1616 vn_finished_write(mp);
1618 if (segflg != UIO_SYSSPACE)
1619 uma_zfree(namei_zone, syspath);
1624 * Delete a whiteout from the filesystem.
1627 sys_undelete(td, uap)
1629 register struct undelete_args /* {
1634 struct nameidata nd;
1639 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1640 UIO_USERSPACE, uap->path, td);
1645 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1646 NDFREE(&nd, NDF_ONLY_PNBUF);
1647 if (nd.ni_vp == nd.ni_dvp)
1655 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1656 NDFREE(&nd, NDF_ONLY_PNBUF);
1658 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1662 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1663 NDFREE(&nd, NDF_ONLY_PNBUF);
1665 vn_finished_write(mp);
1670 * Delete a name from the filesystem.
1672 #ifndef _SYS_SYSPROTO_H_
1673 struct unlink_args {
1680 struct unlink_args /* {
1685 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1688 #ifndef _SYS_SYSPROTO_H_
1689 struct unlinkat_args {
1696 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1698 int flag = uap->flag;
1700 char *path = uap->path;
1702 if (flag & ~AT_REMOVEDIR)
1705 if (flag & AT_REMOVEDIR)
1706 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1708 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1712 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1717 struct nameidata nd;
1719 cap_rights_t rights;
1724 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1725 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1726 if ((error = namei(&nd)) != 0)
1727 return (error == EINVAL ? EPERM : error);
1729 if (vp->v_type == VDIR && oldinum == 0) {
1730 error = EPERM; /* POSIX */
1731 } else if (oldinum != 0 &&
1732 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1733 sb.st_ino != oldinum) {
1734 error = EIDRM; /* Identifier removed */
1737 * The root of a mounted filesystem cannot be deleted.
1739 * XXX: can this only be a VDIR case?
1741 if (vp->v_vflag & VV_ROOT)
1745 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1746 NDFREE(&nd, NDF_ONLY_PNBUF);
1748 if (vp == nd.ni_dvp)
1752 if ((error = vn_start_write(NULL, &mp,
1753 V_XSLEEP | PCATCH)) != 0)
1758 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1763 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1764 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1768 vn_finished_write(mp);
1770 NDFREE(&nd, NDF_ONLY_PNBUF);
1772 if (vp == nd.ni_dvp)
1780 * Reposition read/write file offset.
1782 #ifndef _SYS_SYSPROTO_H_
1793 register struct lseek_args /* {
1801 cap_rights_t rights;
1804 AUDIT_ARG_FD(uap->fd);
1805 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1808 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1809 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1814 #if defined(COMPAT_43)
1816 * Reposition read/write file offset.
1818 #ifndef _SYS_SYSPROTO_H_
1819 struct olseek_args {
1828 register struct olseek_args /* {
1834 struct lseek_args /* {
1842 nuap.offset = uap->offset;
1843 nuap.whence = uap->whence;
1844 return (sys_lseek(td, &nuap));
1846 #endif /* COMPAT_43 */
1848 #if defined(COMPAT_FREEBSD6)
1849 /* Version with the 'pad' argument */
1851 freebsd6_lseek(td, uap)
1853 register struct freebsd6_lseek_args *uap;
1855 struct lseek_args ouap;
1858 ouap.offset = uap->offset;
1859 ouap.whence = uap->whence;
1860 return (sys_lseek(td, &ouap));
1865 * Check access permissions using passed credentials.
1868 vn_access(vp, user_flags, cred, td)
1877 /* Flags == 0 means only check for existence. */
1878 if (user_flags == 0)
1882 if (user_flags & R_OK)
1884 if (user_flags & W_OK)
1886 if (user_flags & X_OK)
1889 error = mac_vnode_check_access(cred, vp, accmode);
1893 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1894 error = VOP_ACCESS(vp, accmode, cred, td);
1899 * Check access permissions using "real" credentials.
1901 #ifndef _SYS_SYSPROTO_H_
1902 struct access_args {
1910 register struct access_args /* {
1916 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1920 #ifndef _SYS_SYSPROTO_H_
1921 struct faccessat_args {
1929 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1932 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1937 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1938 int flag, int amode)
1940 struct ucred *cred, *usecred;
1942 struct nameidata nd;
1943 cap_rights_t rights;
1946 if (flag & ~AT_EACCESS)
1948 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1952 * Create and modify a temporary credential instead of one that
1953 * is potentially shared (if we need one).
1955 cred = td->td_ucred;
1956 if ((flag & AT_EACCESS) == 0 &&
1957 ((cred->cr_uid != cred->cr_ruid ||
1958 cred->cr_rgid != cred->cr_groups[0]))) {
1959 usecred = crdup(cred);
1960 usecred->cr_uid = cred->cr_ruid;
1961 usecred->cr_groups[0] = cred->cr_rgid;
1962 td->td_ucred = usecred;
1965 AUDIT_ARG_VALUE(amode);
1966 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1967 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
1969 if ((error = namei(&nd)) != 0)
1973 error = vn_access(vp, amode, usecred, td);
1974 NDFREE(&nd, NDF_ONLY_PNBUF);
1977 if (usecred != cred) {
1978 td->td_ucred = cred;
1985 * Check access permissions using "effective" credentials.
1987 #ifndef _SYS_SYSPROTO_H_
1988 struct eaccess_args {
1994 sys_eaccess(td, uap)
1996 register struct eaccess_args /* {
2002 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2003 AT_EACCESS, uap->amode));
2006 #if defined(COMPAT_43)
2008 * Get file status; this version follows links.
2010 #ifndef _SYS_SYSPROTO_H_
2019 register struct ostat_args /* {
2028 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2033 return (copyout(&osb, uap->ub, sizeof (osb)));
2037 * Get file status; this version does not follow links.
2039 #ifndef _SYS_SYSPROTO_H_
2040 struct olstat_args {
2048 register struct olstat_args /* {
2057 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2058 UIO_USERSPACE, &sb, NULL);
2062 return (copyout(&osb, uap->ub, sizeof (osb)));
2066 * Convert from an old to a new stat structure.
2074 bzero(ost, sizeof(*ost));
2075 ost->st_dev = st->st_dev;
2076 ost->st_ino = st->st_ino;
2077 ost->st_mode = st->st_mode;
2078 ost->st_nlink = st->st_nlink;
2079 ost->st_uid = st->st_uid;
2080 ost->st_gid = st->st_gid;
2081 ost->st_rdev = st->st_rdev;
2082 if (st->st_size < (quad_t)1 << 32)
2083 ost->st_size = st->st_size;
2086 ost->st_atim = st->st_atim;
2087 ost->st_mtim = st->st_mtim;
2088 ost->st_ctim = st->st_ctim;
2089 ost->st_blksize = st->st_blksize;
2090 ost->st_blocks = st->st_blocks;
2091 ost->st_flags = st->st_flags;
2092 ost->st_gen = st->st_gen;
2094 #endif /* COMPAT_43 */
2097 * Get file status; this version follows links.
2099 #ifndef _SYS_SYSPROTO_H_
2108 register struct stat_args /* {
2116 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2119 error = copyout(&sb, uap->ub, sizeof (sb));
2123 #ifndef _SYS_SYSPROTO_H_
2124 struct fstatat_args {
2132 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2137 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2138 UIO_USERSPACE, &sb, NULL);
2140 error = copyout(&sb, uap->buf, sizeof (sb));
2145 kern_statat(struct thread *td, int flag, int fd, char *path,
2146 enum uio_seg pathseg, struct stat *sbp,
2147 void (*hook)(struct vnode *vp, struct stat *sbp))
2149 struct nameidata nd;
2151 cap_rights_t rights;
2154 if (flag & ~AT_SYMLINK_NOFOLLOW)
2157 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2158 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2159 cap_rights_init(&rights, CAP_FSTAT), td);
2161 if ((error = namei(&nd)) != 0)
2163 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2165 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2166 if (S_ISREG(sb.st_mode))
2167 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2168 if (__predict_false(hook != NULL))
2169 hook(nd.ni_vp, &sb);
2171 NDFREE(&nd, NDF_ONLY_PNBUF);
2177 if (KTRPOINT(td, KTR_STRUCT))
2184 * Get file status; this version does not follow links.
2186 #ifndef _SYS_SYSPROTO_H_
2195 register struct lstat_args /* {
2203 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2204 UIO_USERSPACE, &sb, NULL);
2206 error = copyout(&sb, uap->ub, sizeof (sb));
2211 * Implementation of the NetBSD [l]stat() functions.
2219 bzero(nsb, sizeof *nsb);
2220 nsb->st_dev = sb->st_dev;
2221 nsb->st_ino = sb->st_ino;
2222 nsb->st_mode = sb->st_mode;
2223 nsb->st_nlink = sb->st_nlink;
2224 nsb->st_uid = sb->st_uid;
2225 nsb->st_gid = sb->st_gid;
2226 nsb->st_rdev = sb->st_rdev;
2227 nsb->st_atim = sb->st_atim;
2228 nsb->st_mtim = sb->st_mtim;
2229 nsb->st_ctim = sb->st_ctim;
2230 nsb->st_size = sb->st_size;
2231 nsb->st_blocks = sb->st_blocks;
2232 nsb->st_blksize = sb->st_blksize;
2233 nsb->st_flags = sb->st_flags;
2234 nsb->st_gen = sb->st_gen;
2235 nsb->st_birthtim = sb->st_birthtim;
2238 #ifndef _SYS_SYSPROTO_H_
2247 register struct nstat_args /* {
2256 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2260 cvtnstat(&sb, &nsb);
2261 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2265 * NetBSD lstat. Get file status; this version does not follow links.
2267 #ifndef _SYS_SYSPROTO_H_
2276 register struct nlstat_args /* {
2285 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2286 UIO_USERSPACE, &sb, NULL);
2289 cvtnstat(&sb, &nsb);
2290 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2294 * Get configurable pathname variables.
2296 #ifndef _SYS_SYSPROTO_H_
2297 struct pathconf_args {
2303 sys_pathconf(td, uap)
2305 register struct pathconf_args /* {
2311 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2314 #ifndef _SYS_SYSPROTO_H_
2315 struct lpathconf_args {
2321 sys_lpathconf(td, uap)
2323 register struct lpathconf_args /* {
2329 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2334 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2337 struct nameidata nd;
2340 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2342 if ((error = namei(&nd)) != 0)
2344 NDFREE(&nd, NDF_ONLY_PNBUF);
2346 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2352 * Return target name of a symbolic link.
2354 #ifndef _SYS_SYSPROTO_H_
2355 struct readlink_args {
2362 sys_readlink(td, uap)
2364 register struct readlink_args /* {
2371 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2372 uap->buf, UIO_USERSPACE, uap->count));
2374 #ifndef _SYS_SYSPROTO_H_
2375 struct readlinkat_args {
2383 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2386 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2387 uap->buf, UIO_USERSPACE, uap->bufsize));
2391 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2392 char *buf, enum uio_seg bufseg, size_t count)
2397 struct nameidata nd;
2400 if (count > IOSIZE_MAX)
2403 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2404 pathseg, path, fd, td);
2406 if ((error = namei(&nd)) != 0)
2408 NDFREE(&nd, NDF_ONLY_PNBUF);
2411 error = mac_vnode_check_readlink(td->td_ucred, vp);
2417 if (vp->v_type != VLNK)
2420 aiov.iov_base = buf;
2421 aiov.iov_len = count;
2422 auio.uio_iov = &aiov;
2423 auio.uio_iovcnt = 1;
2424 auio.uio_offset = 0;
2425 auio.uio_rw = UIO_READ;
2426 auio.uio_segflg = bufseg;
2428 auio.uio_resid = count;
2429 error = VOP_READLINK(vp, &auio, td->td_ucred);
2430 td->td_retval[0] = count - auio.uio_resid;
2437 * Common implementation code for chflags() and fchflags().
2440 setfflags(td, vp, flags)
2449 /* We can't support the value matching VNOVAL. */
2450 if (flags == VNOVAL)
2451 return (EOPNOTSUPP);
2454 * Prevent non-root users from setting flags on devices. When
2455 * a device is reused, users can retain ownership of the device
2456 * if they are allowed to set flags and programs assume that
2457 * chown can't fail when done as root.
2459 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2460 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2465 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2468 vattr.va_flags = flags;
2469 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2471 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2474 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2476 vn_finished_write(mp);
2481 * Change flags of a file given a path name.
2483 #ifndef _SYS_SYSPROTO_H_
2484 struct chflags_args {
2490 sys_chflags(td, uap)
2492 register struct chflags_args /* {
2498 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2502 #ifndef _SYS_SYSPROTO_H_
2503 struct chflagsat_args {
2511 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2514 const char *path = uap->path;
2515 u_long flags = uap->flags;
2516 int atflag = uap->atflag;
2518 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2521 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2525 * Same as chflags() but doesn't follow symlinks.
2528 sys_lchflags(td, uap)
2530 register struct lchflags_args /* {
2536 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2537 uap->flags, AT_SYMLINK_NOFOLLOW));
2541 kern_chflagsat(struct thread *td, int fd, const char *path,
2542 enum uio_seg pathseg, u_long flags, int atflag)
2544 struct nameidata nd;
2545 cap_rights_t rights;
2548 AUDIT_ARG_FFLAGS(flags);
2549 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2550 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2551 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2552 if ((error = namei(&nd)) != 0)
2554 NDFREE(&nd, NDF_ONLY_PNBUF);
2555 error = setfflags(td, nd.ni_vp, flags);
2561 * Change flags of a file given a file descriptor.
2563 #ifndef _SYS_SYSPROTO_H_
2564 struct fchflags_args {
2570 sys_fchflags(td, uap)
2572 register struct fchflags_args /* {
2578 cap_rights_t rights;
2581 AUDIT_ARG_FD(uap->fd);
2582 AUDIT_ARG_FFLAGS(uap->flags);
2583 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2588 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2589 AUDIT_ARG_VNODE1(fp->f_vnode);
2590 VOP_UNLOCK(fp->f_vnode, 0);
2592 error = setfflags(td, fp->f_vnode, uap->flags);
2598 * Common implementation code for chmod(), lchmod() and fchmod().
2601 setfmode(td, cred, vp, mode)
2611 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2613 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2615 vattr.va_mode = mode & ALLPERMS;
2617 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2620 error = VOP_SETATTR(vp, &vattr, cred);
2622 vn_finished_write(mp);
2627 * Change mode of a file given path name.
2629 #ifndef _SYS_SYSPROTO_H_
2638 register struct chmod_args /* {
2644 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2648 #ifndef _SYS_SYSPROTO_H_
2649 struct fchmodat_args {
2657 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2659 int flag = uap->flag;
2661 char *path = uap->path;
2662 mode_t mode = uap->mode;
2664 if (flag & ~AT_SYMLINK_NOFOLLOW)
2667 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2671 * Change mode of a file given path name (don't follow links.)
2673 #ifndef _SYS_SYSPROTO_H_
2674 struct lchmod_args {
2682 register struct lchmod_args /* {
2688 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2689 uap->mode, AT_SYMLINK_NOFOLLOW));
2693 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2694 mode_t mode, int flag)
2696 struct nameidata nd;
2697 cap_rights_t rights;
2700 AUDIT_ARG_MODE(mode);
2701 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2702 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2703 cap_rights_init(&rights, CAP_FCHMOD), td);
2704 if ((error = namei(&nd)) != 0)
2706 NDFREE(&nd, NDF_ONLY_PNBUF);
2707 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2713 * Change mode of a file given a file descriptor.
2715 #ifndef _SYS_SYSPROTO_H_
2716 struct fchmod_args {
2722 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2725 cap_rights_t rights;
2728 AUDIT_ARG_FD(uap->fd);
2729 AUDIT_ARG_MODE(uap->mode);
2731 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2734 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2740 * Common implementation for chown(), lchown(), and fchown()
2743 setfown(td, cred, vp, uid, gid)
2754 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2756 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2761 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2765 error = VOP_SETATTR(vp, &vattr, cred);
2767 vn_finished_write(mp);
2772 * Set ownership given a path name.
2774 #ifndef _SYS_SYSPROTO_H_
2784 register struct chown_args /* {
2791 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2795 #ifndef _SYS_SYSPROTO_H_
2796 struct fchownat_args {
2805 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2810 if (flag & ~AT_SYMLINK_NOFOLLOW)
2813 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2814 uap->gid, uap->flag));
2818 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2819 int uid, int gid, int flag)
2821 struct nameidata nd;
2822 cap_rights_t rights;
2825 AUDIT_ARG_OWNER(uid, gid);
2826 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2827 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2828 cap_rights_init(&rights, CAP_FCHOWN), td);
2830 if ((error = namei(&nd)) != 0)
2832 NDFREE(&nd, NDF_ONLY_PNBUF);
2833 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2839 * Set ownership given a path name, do not cross symlinks.
2841 #ifndef _SYS_SYSPROTO_H_
2842 struct lchown_args {
2851 register struct lchown_args /* {
2858 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2859 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2863 * Set ownership given a file descriptor.
2865 #ifndef _SYS_SYSPROTO_H_
2866 struct fchown_args {
2875 register struct fchown_args /* {
2882 cap_rights_t rights;
2885 AUDIT_ARG_FD(uap->fd);
2886 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2887 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2890 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2896 * Common implementation code for utimes(), lutimes(), and futimes().
2899 getutimes(usrtvp, tvpseg, tsp)
2900 const struct timeval *usrtvp;
2901 enum uio_seg tvpseg;
2902 struct timespec *tsp;
2904 struct timeval tv[2];
2905 const struct timeval *tvp;
2908 if (usrtvp == NULL) {
2909 vfs_timestamp(&tsp[0]);
2912 if (tvpseg == UIO_SYSSPACE) {
2915 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2920 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2921 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2923 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2924 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2930 * Common implementation code for futimens(), utimensat().
2932 #define UTIMENS_NULL 0x1
2933 #define UTIMENS_EXIT 0x2
2935 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2936 struct timespec *tsp, int *retflags)
2938 struct timespec tsnow;
2941 vfs_timestamp(&tsnow);
2943 if (usrtsp == NULL) {
2946 *retflags |= UTIMENS_NULL;
2949 if (tspseg == UIO_SYSSPACE) {
2952 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2954 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2955 *retflags |= UTIMENS_EXIT;
2956 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2957 *retflags |= UTIMENS_NULL;
2958 if (tsp[0].tv_nsec == UTIME_OMIT)
2959 tsp[0].tv_sec = VNOVAL;
2960 else if (tsp[0].tv_nsec == UTIME_NOW)
2962 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2964 if (tsp[1].tv_nsec == UTIME_OMIT)
2965 tsp[1].tv_sec = VNOVAL;
2966 else if (tsp[1].tv_nsec == UTIME_NOW)
2968 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2975 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2979 setutimes(td, vp, ts, numtimes, nullflag)
2982 const struct timespec *ts;
2988 int error, setbirthtime;
2990 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2992 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2994 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
2995 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2998 vattr.va_atime = ts[0];
2999 vattr.va_mtime = ts[1];
3001 vattr.va_birthtime = ts[1];
3003 vattr.va_birthtime = ts[2];
3005 vattr.va_vaflags |= VA_UTIMES_NULL;
3007 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3011 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3013 vn_finished_write(mp);
3018 * Set the access and modification times of a file.
3020 #ifndef _SYS_SYSPROTO_H_
3021 struct utimes_args {
3023 struct timeval *tptr;
3029 register struct utimes_args /* {
3031 struct timeval *tptr;
3035 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3036 uap->tptr, UIO_USERSPACE));
3039 #ifndef _SYS_SYSPROTO_H_
3040 struct futimesat_args {
3043 const struct timeval * times;
3047 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3050 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3051 uap->times, UIO_USERSPACE));
3055 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3056 struct timeval *tptr, enum uio_seg tptrseg)
3058 struct nameidata nd;
3059 struct timespec ts[2];
3060 cap_rights_t rights;
3063 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3065 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3066 cap_rights_init(&rights, CAP_FUTIMES), td);
3068 if ((error = namei(&nd)) != 0)
3070 NDFREE(&nd, NDF_ONLY_PNBUF);
3071 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3077 * Set the access and modification times of a file.
3079 #ifndef _SYS_SYSPROTO_H_
3080 struct lutimes_args {
3082 struct timeval *tptr;
3086 sys_lutimes(td, uap)
3088 register struct lutimes_args /* {
3090 struct timeval *tptr;
3094 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3099 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3100 struct timeval *tptr, enum uio_seg tptrseg)
3102 struct timespec ts[2];
3103 struct nameidata nd;
3106 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3108 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3109 if ((error = namei(&nd)) != 0)
3111 NDFREE(&nd, NDF_ONLY_PNBUF);
3112 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3118 * Set the access and modification times of a file.
3120 #ifndef _SYS_SYSPROTO_H_
3121 struct futimes_args {
3123 struct timeval *tptr;
3127 sys_futimes(td, uap)
3129 register struct futimes_args /* {
3131 struct timeval *tptr;
3135 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3139 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3140 enum uio_seg tptrseg)
3142 struct timespec ts[2];
3144 cap_rights_t rights;
3148 error = getutimes(tptr, tptrseg, ts);
3151 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3155 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3156 AUDIT_ARG_VNODE1(fp->f_vnode);
3157 VOP_UNLOCK(fp->f_vnode, 0);
3159 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3165 sys_futimens(struct thread *td, struct futimens_args *uap)
3168 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3172 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3173 enum uio_seg tptrseg)
3175 struct timespec ts[2];
3177 cap_rights_t rights;
3181 error = getutimens(tptr, tptrseg, ts, &flags);
3184 if (flags & UTIMENS_EXIT)
3186 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3190 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3191 AUDIT_ARG_VNODE1(fp->f_vnode);
3192 VOP_UNLOCK(fp->f_vnode, 0);
3194 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3200 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3203 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3204 uap->times, UIO_USERSPACE, uap->flag));
3208 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3209 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3211 struct nameidata nd;
3212 struct timespec ts[2];
3213 cap_rights_t rights;
3216 if (flag & ~AT_SYMLINK_NOFOLLOW)
3219 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3221 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3222 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3223 cap_rights_init(&rights, CAP_FUTIMES), td);
3224 if ((error = namei(&nd)) != 0)
3227 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3229 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3230 * "Search permission is denied by a component of the path prefix."
3232 NDFREE(&nd, NDF_ONLY_PNBUF);
3233 if ((flags & UTIMENS_EXIT) == 0)
3234 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3240 * Truncate a file given its path name.
3242 #ifndef _SYS_SYSPROTO_H_
3243 struct truncate_args {
3250 sys_truncate(td, uap)
3252 register struct truncate_args /* {
3259 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3263 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3269 struct nameidata nd;
3274 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3275 if ((error = namei(&nd)) != 0)
3278 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3279 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3280 vn_rangelock_unlock(vp, rl_cookie);
3284 NDFREE(&nd, NDF_ONLY_PNBUF);
3285 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3286 if (vp->v_type == VDIR)
3289 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3292 else if ((error = vn_writechk(vp)) == 0 &&
3293 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3295 vattr.va_size = length;
3296 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3299 vn_finished_write(mp);
3300 vn_rangelock_unlock(vp, rl_cookie);
3305 #if defined(COMPAT_43)
3307 * Truncate a file given its path name.
3309 #ifndef _SYS_SYSPROTO_H_
3310 struct otruncate_args {
3318 register struct otruncate_args /* {
3323 struct truncate_args /* {
3329 nuap.path = uap->path;
3330 nuap.length = uap->length;
3331 return (sys_truncate(td, &nuap));
3333 #endif /* COMPAT_43 */
3335 #if defined(COMPAT_FREEBSD6)
3336 /* Versions with the pad argument */
3338 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3340 struct truncate_args ouap;
3342 ouap.path = uap->path;
3343 ouap.length = uap->length;
3344 return (sys_truncate(td, &ouap));
3348 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3350 struct ftruncate_args ouap;
3353 ouap.length = uap->length;
3354 return (sys_ftruncate(td, &ouap));
3359 kern_fsync(struct thread *td, int fd, bool fullsync)
3364 cap_rights_t rights;
3365 int error, lock_flags;
3368 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3374 /* XXXKIB: compete outstanding aio writes */;
3376 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3379 if (MNT_SHARED_WRITES(mp) ||
3380 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3381 lock_flags = LK_SHARED;
3383 lock_flags = LK_EXCLUSIVE;
3385 vn_lock(vp, lock_flags | LK_RETRY);
3386 AUDIT_ARG_VNODE1(vp);
3387 if (vp->v_object != NULL) {
3388 VM_OBJECT_WLOCK(vp->v_object);
3389 vm_object_page_clean(vp->v_object, 0, 0, 0);
3390 VM_OBJECT_WUNLOCK(vp->v_object);
3392 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3394 vn_finished_write(mp);
3401 * Sync an open file.
3403 #ifndef _SYS_SYSPROTO_H_
3409 sys_fsync(struct thread *td, struct fsync_args *uap)
3412 return (kern_fsync(td, uap->fd, true));
3416 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3419 return (kern_fsync(td, uap->fd, false));
3423 * Rename files. Source and destination must either both be directories, or
3424 * both not be directories. If target is a directory, it must be empty.
3426 #ifndef _SYS_SYSPROTO_H_
3427 struct rename_args {
3435 register struct rename_args /* {
3441 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3442 uap->to, UIO_USERSPACE));
3445 #ifndef _SYS_SYSPROTO_H_
3446 struct renameat_args {
3454 sys_renameat(struct thread *td, struct renameat_args *uap)
3457 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3462 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3463 enum uio_seg pathseg)
3465 struct mount *mp = NULL;
3466 struct vnode *tvp, *fvp, *tdvp;
3467 struct nameidata fromnd, tond;
3468 cap_rights_t rights;
3474 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3475 AUDITVNODE1, pathseg, old, oldfd,
3476 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3478 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3479 pathseg, old, oldfd,
3480 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3483 if ((error = namei(&fromnd)) != 0)
3486 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3487 fromnd.ni_vp, &fromnd.ni_cnd);
3488 VOP_UNLOCK(fromnd.ni_dvp, 0);
3489 if (fromnd.ni_dvp != fromnd.ni_vp)
3490 VOP_UNLOCK(fromnd.ni_vp, 0);
3493 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3494 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3495 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3496 if (fromnd.ni_vp->v_type == VDIR)
3497 tond.ni_cnd.cn_flags |= WILLBEDIR;
3498 if ((error = namei(&tond)) != 0) {
3499 /* Translate error code for rename("dir1", "dir2/."). */
3500 if (error == EISDIR && fvp->v_type == VDIR)
3502 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3503 vrele(fromnd.ni_dvp);
3509 error = vn_start_write(fvp, &mp, V_NOWAIT);
3511 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3512 NDFREE(&tond, NDF_ONLY_PNBUF);
3519 vrele(fromnd.ni_dvp);
3521 vrele(tond.ni_startdir);
3522 if (fromnd.ni_startdir != NULL)
3523 vrele(fromnd.ni_startdir);
3524 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3530 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3533 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3538 if (newfd != AT_FDCWD) {
3540 * If the target already exists we require CAP_UNLINKAT
3543 error = cap_check(&tond.ni_filecaps.fc_rights,
3544 cap_rights_init(&rights, CAP_UNLINKAT));
3555 * If the source is the same as the destination (that is, if they
3556 * are links to the same vnode), then there is nothing to do.
3562 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3563 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3567 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3568 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3569 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3570 NDFREE(&tond, NDF_ONLY_PNBUF);
3572 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3573 NDFREE(&tond, NDF_ONLY_PNBUF);
3580 vrele(fromnd.ni_dvp);
3583 vrele(tond.ni_startdir);
3584 vn_finished_write(mp);
3586 if (fromnd.ni_startdir)
3587 vrele(fromnd.ni_startdir);
3594 * Make a directory file.
3596 #ifndef _SYS_SYSPROTO_H_
3605 register struct mkdir_args /* {
3611 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3615 #ifndef _SYS_SYSPROTO_H_
3616 struct mkdirat_args {
3623 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3626 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3630 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3636 struct nameidata nd;
3637 cap_rights_t rights;
3640 AUDIT_ARG_MODE(mode);
3643 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3644 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3646 nd.ni_cnd.cn_flags |= WILLBEDIR;
3647 if ((error = namei(&nd)) != 0)
3651 NDFREE(&nd, NDF_ONLY_PNBUF);
3653 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3654 * the strange behaviour of leaving the vnode unlocked
3655 * if the target is the same vnode as the parent.
3657 if (vp == nd.ni_dvp)
3664 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3665 NDFREE(&nd, NDF_ONLY_PNBUF);
3667 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3672 vattr.va_type = VDIR;
3673 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3675 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3680 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3684 NDFREE(&nd, NDF_ONLY_PNBUF);
3688 vn_finished_write(mp);
3693 * Remove a directory file.
3695 #ifndef _SYS_SYSPROTO_H_
3703 struct rmdir_args /* {
3708 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3712 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3716 struct nameidata nd;
3717 cap_rights_t rights;
3722 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3723 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3724 if ((error = namei(&nd)) != 0)
3727 if (vp->v_type != VDIR) {
3732 * No rmdir "." please.
3734 if (nd.ni_dvp == vp) {
3739 * The root of a mounted filesystem cannot be deleted.
3741 if (vp->v_vflag & VV_ROOT) {
3746 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3751 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3752 NDFREE(&nd, NDF_ONLY_PNBUF);
3754 if (nd.ni_dvp == vp)
3758 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3762 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3763 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3764 vn_finished_write(mp);
3766 NDFREE(&nd, NDF_ONLY_PNBUF);
3768 if (nd.ni_dvp == vp)
3777 * Read a block of directory entries in a filesystem independent format.
3779 #ifndef _SYS_SYSPROTO_H_
3780 struct ogetdirentries_args {
3788 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3793 error = kern_ogetdirentries(td, uap, &loff);
3795 error = copyout(&loff, uap->basep, sizeof(long));
3800 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3805 struct uio auio, kuio;
3806 struct iovec aiov, kiov;
3807 struct dirent *dp, *edp;
3808 cap_rights_t rights;
3810 int error, eofflag, readcnt;
3814 /* XXX arbitrary sanity limit on `count'. */
3815 if (uap->count > 64 * 1024)
3817 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp);
3820 if ((fp->f_flag & FREAD) == 0) {
3825 foffset = foffset_lock(fp, 0);
3827 if (vp->v_type != VDIR) {
3828 foffset_unlock(fp, foffset, 0);
3832 aiov.iov_base = uap->buf;
3833 aiov.iov_len = uap->count;
3834 auio.uio_iov = &aiov;
3835 auio.uio_iovcnt = 1;
3836 auio.uio_rw = UIO_READ;
3837 auio.uio_segflg = UIO_USERSPACE;
3839 auio.uio_resid = uap->count;
3840 vn_lock(vp, LK_SHARED | LK_RETRY);
3841 loff = auio.uio_offset = foffset;
3843 error = mac_vnode_check_readdir(td->td_ucred, vp);
3846 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3851 # if (BYTE_ORDER != LITTLE_ENDIAN)
3852 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3853 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3855 foffset = auio.uio_offset;
3860 kuio.uio_iov = &kiov;
3861 kuio.uio_segflg = UIO_SYSSPACE;
3862 kiov.iov_len = uap->count;
3863 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3864 kiov.iov_base = dirbuf;
3865 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3867 foffset = kuio.uio_offset;
3869 readcnt = uap->count - kuio.uio_resid;
3870 edp = (struct dirent *)&dirbuf[readcnt];
3871 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3872 # if (BYTE_ORDER == LITTLE_ENDIAN)
3874 * The expected low byte of
3875 * dp->d_namlen is our dp->d_type.
3876 * The high MBZ byte of dp->d_namlen
3877 * is our dp->d_namlen.
3879 dp->d_type = dp->d_namlen;
3883 * The dp->d_type is the high byte
3884 * of the expected dp->d_namlen,
3885 * so must be zero'ed.
3889 if (dp->d_reclen > 0) {
3890 dp = (struct dirent *)
3891 ((char *)dp + dp->d_reclen);
3898 error = uiomove(dirbuf, readcnt, &auio);
3900 free(dirbuf, M_TEMP);
3904 foffset_unlock(fp, foffset, 0);
3908 if (uap->count == auio.uio_resid &&
3909 (vp->v_vflag & VV_ROOT) &&
3910 (vp->v_mount->mnt_flag & MNT_UNION)) {
3911 struct vnode *tvp = vp;
3912 vp = vp->v_mount->mnt_vnodecovered;
3921 foffset_unlock(fp, foffset, 0);
3923 td->td_retval[0] = uap->count - auio.uio_resid;
3928 #endif /* COMPAT_43 */
3931 * Read a block of directory entries in a filesystem independent format.
3933 #ifndef _SYS_SYSPROTO_H_
3934 struct getdirentries_args {
3942 sys_getdirentries(td, uap)
3944 register struct getdirentries_args /* {
3954 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3955 NULL, UIO_USERSPACE);
3958 if (uap->basep != NULL)
3959 error = copyout(&base, uap->basep, sizeof(long));
3964 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3965 long *basep, ssize_t *residp, enum uio_seg bufseg)
3971 cap_rights_t rights;
3977 if (count > IOSIZE_MAX)
3979 auio.uio_resid = count;
3980 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
3983 if ((fp->f_flag & FREAD) == 0) {
3988 foffset = foffset_lock(fp, 0);
3990 if (vp->v_type != VDIR) {
3994 aiov.iov_base = buf;
3995 aiov.iov_len = count;
3996 auio.uio_iov = &aiov;
3997 auio.uio_iovcnt = 1;
3998 auio.uio_rw = UIO_READ;
3999 auio.uio_segflg = bufseg;
4001 vn_lock(vp, LK_SHARED | LK_RETRY);
4002 AUDIT_ARG_VNODE1(vp);
4003 loff = auio.uio_offset = foffset;
4005 error = mac_vnode_check_readdir(td->td_ucred, vp);
4008 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4010 foffset = auio.uio_offset;
4015 if (count == auio.uio_resid &&
4016 (vp->v_vflag & VV_ROOT) &&
4017 (vp->v_mount->mnt_flag & MNT_UNION)) {
4018 struct vnode *tvp = vp;
4020 vp = vp->v_mount->mnt_vnodecovered;
4031 *residp = auio.uio_resid;
4032 td->td_retval[0] = count - auio.uio_resid;
4034 foffset_unlock(fp, foffset, 0);
4039 #ifndef _SYS_SYSPROTO_H_
4040 struct getdents_args {
4047 sys_getdents(td, uap)
4049 register struct getdents_args /* {
4055 struct getdirentries_args ap;
4059 ap.count = uap->count;
4061 return (sys_getdirentries(td, &ap));
4065 * Set the mode mask for creation of filesystem nodes.
4067 #ifndef _SYS_SYSPROTO_H_
4075 struct umask_args /* {
4079 struct filedesc *fdp;
4081 fdp = td->td_proc->p_fd;
4082 FILEDESC_XLOCK(fdp);
4083 td->td_retval[0] = fdp->fd_cmask;
4084 fdp->fd_cmask = uap->newmask & ALLPERMS;
4085 FILEDESC_XUNLOCK(fdp);
4090 * Void all references to file by ripping underlying filesystem away from
4093 #ifndef _SYS_SYSPROTO_H_
4094 struct revoke_args {
4101 register struct revoke_args /* {
4107 struct nameidata nd;
4110 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4112 if ((error = namei(&nd)) != 0)
4115 NDFREE(&nd, NDF_ONLY_PNBUF);
4116 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4121 error = mac_vnode_check_revoke(td->td_ucred, vp);
4125 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4128 if (td->td_ucred->cr_uid != vattr.va_uid) {
4129 error = priv_check(td, PRIV_VFS_ADMIN);
4134 VOP_REVOKE(vp, REVOKEALL);
4141 * Convert a user file descriptor to a kernel file entry and check that, if it
4142 * is a capability, the correct rights are present. A reference on the file
4143 * entry is held upon returning.
4146 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4151 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4156 * The file could be not of the vnode type, or it may be not
4157 * yet fully initialized, in which case the f_vnode pointer
4158 * may be set, but f_ops is still badfileops. E.g.,
4159 * devfs_open() transiently create such situation to
4160 * facilitate csw d_fdopen().
4162 * Dupfdopen() handling in kern_openat() installs the
4163 * half-baked file into the process descriptor table, allowing
4164 * other thread to dereference it. Guard against the race by
4167 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4177 * Get an (NFS) file handle.
4179 #ifndef _SYS_SYSPROTO_H_
4180 struct lgetfh_args {
4188 register struct lgetfh_args *uap;
4190 struct nameidata nd;
4192 register struct vnode *vp;
4195 error = priv_check(td, PRIV_VFS_GETFH);
4198 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4203 NDFREE(&nd, NDF_ONLY_PNBUF);
4205 bzero(&fh, sizeof(fh));
4206 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4207 error = VOP_VPTOFH(vp, &fh.fh_fid);
4210 error = copyout(&fh, uap->fhp, sizeof (fh));
4214 #ifndef _SYS_SYSPROTO_H_
4223 register struct getfh_args *uap;
4225 struct nameidata nd;
4227 register struct vnode *vp;
4230 error = priv_check(td, PRIV_VFS_GETFH);
4233 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4238 NDFREE(&nd, NDF_ONLY_PNBUF);
4240 bzero(&fh, sizeof(fh));
4241 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4242 error = VOP_VPTOFH(vp, &fh.fh_fid);
4245 error = copyout(&fh, uap->fhp, sizeof (fh));
4250 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4253 * warning: do not remove the priv_check() call or this becomes one giant
4256 #ifndef _SYS_SYSPROTO_H_
4257 struct fhopen_args {
4258 const struct fhandle *u_fhp;
4265 struct fhopen_args /* {
4266 const struct fhandle *u_fhp;
4277 error = priv_check(td, PRIV_VFS_FHOPEN);
4281 fmode = FFLAGS(uap->flags);
4282 /* why not allow a non-read/write open for our lockd? */
4283 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4285 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4288 /* find the mount point */
4289 mp = vfs_busyfs(&fhp.fh_fsid);
4292 /* now give me my vnode, it gets returned to me locked */
4293 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4298 error = falloc_noinstall(td, &fp);
4304 * An extra reference on `fp' has been held for us by
4305 * falloc_noinstall().
4311 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4313 KASSERT(fp->f_ops == &badfileops,
4314 ("VOP_OPEN in fhopen() set f_ops"));
4315 KASSERT(td->td_dupfd < 0,
4316 ("fhopen() encountered fdopen()"));
4326 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4329 if ((fmode & O_TRUNC) != 0) {
4330 error = fo_truncate(fp, 0, td->td_ucred, td);
4335 error = finstall(td, fp, &indx, fmode, NULL);
4338 td->td_retval[0] = indx;
4343 * Stat an (NFS) file handle.
4345 #ifndef _SYS_SYSPROTO_H_
4346 struct fhstat_args {
4347 struct fhandle *u_fhp;
4354 register struct fhstat_args /* {
4355 struct fhandle *u_fhp;
4363 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4366 error = kern_fhstat(td, fh, &sb);
4368 error = copyout(&sb, uap->sb, sizeof(sb));
4373 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4379 error = priv_check(td, PRIV_VFS_FHSTAT);
4382 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4384 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4388 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4394 * Implement fstatfs() for (NFS) file handles.
4396 #ifndef _SYS_SYSPROTO_H_
4397 struct fhstatfs_args {
4398 struct fhandle *u_fhp;
4403 sys_fhstatfs(td, uap)
4405 struct fhstatfs_args /* {
4406 struct fhandle *u_fhp;
4414 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4417 error = kern_fhstatfs(td, fh, &sf);
4420 return (copyout(&sf, uap->buf, sizeof(sf)));
4424 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4431 error = priv_check(td, PRIV_VFS_FHSTATFS);
4434 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4436 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4442 error = prison_canseemount(td->td_ucred, mp);
4446 error = mac_mount_check_stat(td->td_ucred, mp);
4451 * Set these in case the underlying filesystem fails to do so.
4454 sp->f_version = STATFS_VERSION;
4455 sp->f_namemax = NAME_MAX;
4456 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4457 error = VFS_STATFS(mp, sp);
4466 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4471 cap_rights_t rights;
4472 off_t olen, ooffset;
4475 if (offset < 0 || len <= 0)
4477 /* Check for wrap. */
4478 if (offset > OFF_MAX - len)
4480 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4483 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4487 if ((fp->f_flag & FWRITE) == 0) {
4491 if (fp->f_type != DTYPE_VNODE) {
4496 if (vp->v_type != VREG) {
4501 /* Allocating blocks may take a long time, so iterate. */
4508 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4511 error = vn_lock(vp, LK_EXCLUSIVE);
4513 vn_finished_write(mp);
4517 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4520 error = VOP_ALLOCATE(vp, &offset, &len);
4522 vn_finished_write(mp);
4524 if (olen + ooffset != offset + len) {
4525 panic("offset + len changed from %jx/%jx to %jx/%jx",
4526 ooffset, olen, offset, len);
4528 if (error != 0 || len == 0)
4530 KASSERT(olen > len, ("Iteration did not make progress?"));
4539 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4543 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4544 return (kern_posix_error(td, error));
4548 * Unlike madvise(2), we do not make a best effort to remember every
4549 * possible caching hint. Instead, we remember the last setting with
4550 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4551 * region of any current setting.
4554 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4557 struct fadvise_info *fa, *new;
4560 cap_rights_t rights;
4564 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4567 case POSIX_FADV_SEQUENTIAL:
4568 case POSIX_FADV_RANDOM:
4569 case POSIX_FADV_NOREUSE:
4570 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4572 case POSIX_FADV_NORMAL:
4573 case POSIX_FADV_WILLNEED:
4574 case POSIX_FADV_DONTNEED:
4580 /* XXX: CAP_POSIX_FADVISE? */
4581 error = fget(td, fd, cap_rights_init(&rights), &fp);
4584 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4588 if (fp->f_type != DTYPE_VNODE) {
4593 if (vp->v_type != VREG) {
4600 end = offset + len - 1;
4602 case POSIX_FADV_SEQUENTIAL:
4603 case POSIX_FADV_RANDOM:
4604 case POSIX_FADV_NOREUSE:
4606 * Try to merge any existing non-standard region with
4607 * this new region if possible, otherwise create a new
4608 * non-standard region for this request.
4610 mtx_pool_lock(mtxpool_sleep, fp);
4612 if (fa != NULL && fa->fa_advice == advice &&
4613 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4614 (end != OFF_MAX && fa->fa_start == end + 1) ||
4615 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4616 if (offset < fa->fa_start)
4617 fa->fa_start = offset;
4618 if (end > fa->fa_end)
4621 new->fa_advice = advice;
4622 new->fa_start = offset;
4627 mtx_pool_unlock(mtxpool_sleep, fp);
4629 case POSIX_FADV_NORMAL:
4631 * If a the "normal" region overlaps with an existing
4632 * non-standard region, trim or remove the
4633 * non-standard region.
4635 mtx_pool_lock(mtxpool_sleep, fp);
4638 if (offset <= fa->fa_start && end >= fa->fa_end) {
4640 fp->f_advice = NULL;
4641 } else if (offset <= fa->fa_start &&
4642 end >= fa->fa_start)
4643 fa->fa_start = end + 1;
4644 else if (offset <= fa->fa_end && end >= fa->fa_end)
4645 fa->fa_end = offset - 1;
4646 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4648 * If the "normal" region is a middle
4649 * portion of the existing
4650 * non-standard region, just remove
4651 * the whole thing rather than picking
4652 * one side or the other to
4656 fp->f_advice = NULL;
4659 mtx_pool_unlock(mtxpool_sleep, fp);
4661 case POSIX_FADV_WILLNEED:
4662 case POSIX_FADV_DONTNEED:
4663 error = VOP_ADVISE(vp, offset, end, advice);
4669 free(new, M_FADVISE);
4674 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4678 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4680 return (kern_posix_error(td, error));