2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_capsicum.h"
41 #include "opt_compat.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
48 #include <sys/capsicum.h>
50 #include <sys/sysent.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/sysproto.h>
55 #include <sys/namei.h>
56 #include <sys/filedesc.h>
57 #include <sys/kernel.h>
58 #include <sys/fcntl.h>
60 #include <sys/filio.h>
61 #include <sys/limits.h>
62 #include <sys/linker.h>
63 #include <sys/rwlock.h>
67 #include <sys/unistd.h>
68 #include <sys/vnode.h>
71 #include <sys/dirent.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysctl.h>
76 #include <sys/ktrace.h>
79 #include <machine/stdarg.h>
81 #include <security/audit/audit.h>
82 #include <security/mac/mac_framework.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
89 #include <ufs/ufs/quota.h>
91 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
93 SDT_PROVIDER_DEFINE(vfs);
94 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int");
95 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int");
97 static int kern_chflagsat(struct thread *td, int fd, const char *path,
98 enum uio_seg pathseg, u_long flags, int atflag);
99 static int setfflags(struct thread *td, struct vnode *, u_long);
100 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
101 static int getutimens(const struct timespec *, enum uio_seg,
102 struct timespec *, int *);
103 static int setutimes(struct thread *td, struct vnode *,
104 const struct timespec *, int, int);
105 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
109 * Sync each mounted filesystem.
111 #ifndef _SYS_SYSPROTO_H_
120 struct sync_args *uap;
122 struct mount *mp, *nmp;
125 mtx_lock(&mountlist_mtx);
126 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
127 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
128 nmp = TAILQ_NEXT(mp, mnt_list);
131 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
132 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
133 save = curthread_pflags_set(TDP_SYNCIO);
134 vfs_msync(mp, MNT_NOWAIT);
135 VFS_SYNC(mp, MNT_NOWAIT);
136 curthread_pflags_restore(save);
137 vn_finished_write(mp);
139 mtx_lock(&mountlist_mtx);
140 nmp = TAILQ_NEXT(mp, mnt_list);
143 mtx_unlock(&mountlist_mtx);
148 * Change filesystem quotas.
150 #ifndef _SYS_SYSPROTO_H_
151 struct quotactl_args {
159 sys_quotactl(td, uap)
161 register struct quotactl_args /* {
172 AUDIT_ARG_CMD(uap->cmd);
173 AUDIT_ARG_UID(uap->uid);
174 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
176 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
178 if ((error = namei(&nd)) != 0)
180 NDFREE(&nd, NDF_ONLY_PNBUF);
181 mp = nd.ni_vp->v_mount;
184 error = vfs_busy(mp, 0);
188 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
191 * Since quota on operation typically needs to open quota
192 * file, the Q_QUOTAON handler needs to unbusy the mount point
193 * before calling into namei. Otherwise, unmount might be
194 * started between two vfs_busy() invocations (first is our,
195 * second is from mount point cross-walk code in lookup()),
198 * Require that Q_QUOTAON handles the vfs_busy() reference on
199 * its own, always returning with ubusied mount point.
201 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
207 * Used by statfs conversion routines to scale the block size up if
208 * necessary so that all of the block counts are <= 'max_size'. Note
209 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
213 statfs_scale_blocks(struct statfs *sf, long max_size)
218 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
221 * Attempt to scale the block counts to give a more accurate
222 * overview to userland of the ratio of free space to used
223 * space. To do this, find the largest block count and compute
224 * a divisor that lets it fit into a signed integer <= max_size.
226 if (sf->f_bavail < 0)
227 count = -sf->f_bavail;
229 count = sf->f_bavail;
230 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
231 if (count <= max_size)
234 count >>= flsl(max_size);
241 sf->f_bsize <<= shift;
242 sf->f_blocks >>= shift;
243 sf->f_bfree >>= shift;
244 sf->f_bavail >>= shift;
248 * Get filesystem statistics.
250 #ifndef _SYS_SYSPROTO_H_
259 register struct statfs_args /* {
267 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
269 error = copyout(&sf, uap->buf, sizeof(sf));
274 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
278 struct statfs *sp, sb;
282 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
287 mp = nd.ni_vp->v_mount;
289 NDFREE(&nd, NDF_ONLY_PNBUF);
291 error = vfs_busy(mp, 0);
296 error = mac_mount_check_stat(td->td_ucred, mp);
301 * Set these in case the underlying filesystem fails to do so.
304 sp->f_version = STATFS_VERSION;
305 sp->f_namemax = NAME_MAX;
306 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
307 error = VFS_STATFS(mp, sp);
310 if (priv_check(td, PRIV_VFS_GENERATION)) {
311 bcopy(sp, &sb, sizeof(sb));
312 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
313 prison_enforce_statfs(td->td_ucred, mp, &sb);
323 * Get filesystem statistics.
325 #ifndef _SYS_SYSPROTO_H_
326 struct fstatfs_args {
334 register struct fstatfs_args /* {
342 error = kern_fstatfs(td, uap->fd, &sf);
344 error = copyout(&sf, uap->buf, sizeof(sf));
349 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
353 struct statfs *sp, sb;
359 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSTATFS), &fp);
363 vn_lock(vp, LK_SHARED | LK_RETRY);
365 AUDIT_ARG_VNODE1(vp);
376 error = vfs_busy(mp, 0);
381 error = mac_mount_check_stat(td->td_ucred, mp);
386 * Set these in case the underlying filesystem fails to do so.
389 sp->f_version = STATFS_VERSION;
390 sp->f_namemax = NAME_MAX;
391 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
392 error = VFS_STATFS(mp, sp);
395 if (priv_check(td, PRIV_VFS_GENERATION)) {
396 bcopy(sp, &sb, sizeof(sb));
397 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
398 prison_enforce_statfs(td->td_ucred, mp, &sb);
409 * Get statistics on all filesystems.
411 #ifndef _SYS_SYSPROTO_H_
412 struct getfsstat_args {
419 sys_getfsstat(td, uap)
421 register struct getfsstat_args /* {
430 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
432 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
433 UIO_USERSPACE, uap->flags);
435 td->td_retval[0] = count;
440 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
441 * The caller is responsible for freeing memory which will be allocated
445 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
446 size_t *countp, enum uio_seg bufseg, int flags)
448 struct mount *mp, *nmp;
449 struct statfs *sfsp, *sp, sb, *tofree;
450 size_t count, maxcount;
454 maxcount = bufsize / sizeof(struct statfs);
458 } else if (bufseg == UIO_USERSPACE) {
461 } else /* if (bufseg == UIO_SYSSPACE) */ {
463 mtx_lock(&mountlist_mtx);
464 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
467 mtx_unlock(&mountlist_mtx);
468 if (maxcount > count)
470 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
474 mtx_lock(&mountlist_mtx);
475 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
476 if (prison_canseemount(td->td_ucred, mp) != 0) {
477 nmp = TAILQ_NEXT(mp, mnt_list);
481 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
482 nmp = TAILQ_NEXT(mp, mnt_list);
486 if (flags == MNT_WAIT) {
487 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
489 * If vfs_busy() failed, and MBF_NOWAIT
490 * wasn't passed, then the mp is gone.
491 * Furthermore, because of MBF_MNTLSTLOCK,
492 * the mountlist_mtx was dropped. We have
493 * no other choice than to start over.
495 mtx_unlock(&mountlist_mtx);
496 free(tofree, M_TEMP);
500 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
501 nmp = TAILQ_NEXT(mp, mnt_list);
505 if (sfsp && count < maxcount) {
508 * Set these in case the underlying filesystem
511 sp->f_version = STATFS_VERSION;
512 sp->f_namemax = NAME_MAX;
513 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
515 * If MNT_NOWAIT or MNT_LAZY is specified, do not
516 * refresh the fsstat cache.
518 if (flags != MNT_LAZY && flags != MNT_NOWAIT) {
519 error = VFS_STATFS(mp, sp);
521 mtx_lock(&mountlist_mtx);
522 nmp = TAILQ_NEXT(mp, mnt_list);
527 if (priv_check(td, PRIV_VFS_GENERATION)) {
528 bcopy(sp, &sb, sizeof(sb));
529 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
530 prison_enforce_statfs(td->td_ucred, mp, &sb);
533 if (bufseg == UIO_SYSSPACE)
534 bcopy(sp, sfsp, sizeof(*sp));
535 else /* if (bufseg == UIO_USERSPACE) */ {
536 error = copyout(sp, sfsp, sizeof(*sp));
545 mtx_lock(&mountlist_mtx);
546 nmp = TAILQ_NEXT(mp, mnt_list);
549 mtx_unlock(&mountlist_mtx);
550 if (sfsp && count > maxcount)
557 #ifdef COMPAT_FREEBSD4
559 * Get old format filesystem statistics.
561 static void cvtstatfs(struct statfs *, struct ostatfs *);
563 #ifndef _SYS_SYSPROTO_H_
564 struct freebsd4_statfs_args {
570 freebsd4_statfs(td, uap)
572 struct freebsd4_statfs_args /* {
581 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
584 cvtstatfs(&sf, &osb);
585 return (copyout(&osb, uap->buf, sizeof(osb)));
589 * Get filesystem statistics.
591 #ifndef _SYS_SYSPROTO_H_
592 struct freebsd4_fstatfs_args {
598 freebsd4_fstatfs(td, uap)
600 struct freebsd4_fstatfs_args /* {
609 error = kern_fstatfs(td, uap->fd, &sf);
612 cvtstatfs(&sf, &osb);
613 return (copyout(&osb, uap->buf, sizeof(osb)));
617 * Get statistics on all filesystems.
619 #ifndef _SYS_SYSPROTO_H_
620 struct freebsd4_getfsstat_args {
627 freebsd4_getfsstat(td, uap)
629 register struct freebsd4_getfsstat_args /* {
635 struct statfs *buf, *sp;
640 if (uap->bufsize < 0)
642 count = uap->bufsize / sizeof(struct ostatfs);
643 if (count > SIZE_MAX / sizeof(struct statfs))
645 size = count * sizeof(struct statfs);
646 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
648 td->td_retval[0] = count;
651 while (count != 0 && error == 0) {
653 error = copyout(&osb, uap->buf, sizeof(osb));
664 * Implement fstatfs() for (NFS) file handles.
666 #ifndef _SYS_SYSPROTO_H_
667 struct freebsd4_fhstatfs_args {
668 struct fhandle *u_fhp;
673 freebsd4_fhstatfs(td, uap)
675 struct freebsd4_fhstatfs_args /* {
676 struct fhandle *u_fhp;
685 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
688 error = kern_fhstatfs(td, fh, &sf);
691 cvtstatfs(&sf, &osb);
692 return (copyout(&osb, uap->buf, sizeof(osb)));
696 * Convert a new format statfs structure to an old format statfs structure.
704 statfs_scale_blocks(nsp, LONG_MAX);
705 bzero(osp, sizeof(*osp));
706 osp->f_bsize = nsp->f_bsize;
707 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
708 osp->f_blocks = nsp->f_blocks;
709 osp->f_bfree = nsp->f_bfree;
710 osp->f_bavail = nsp->f_bavail;
711 osp->f_files = MIN(nsp->f_files, LONG_MAX);
712 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
713 osp->f_owner = nsp->f_owner;
714 osp->f_type = nsp->f_type;
715 osp->f_flags = nsp->f_flags;
716 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
717 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
718 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
719 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
720 strlcpy(osp->f_fstypename, nsp->f_fstypename,
721 MIN(MFSNAMELEN, OMFSNAMELEN));
722 strlcpy(osp->f_mntonname, nsp->f_mntonname,
723 MIN(MNAMELEN, OMNAMELEN));
724 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
725 MIN(MNAMELEN, OMNAMELEN));
726 osp->f_fsid = nsp->f_fsid;
728 #endif /* COMPAT_FREEBSD4 */
731 * Change current working directory to a given file descriptor.
733 #ifndef _SYS_SYSPROTO_H_
741 struct fchdir_args /* {
745 struct vnode *vp, *tdp;
751 AUDIT_ARG_FD(uap->fd);
752 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHDIR),
759 vn_lock(vp, LK_SHARED | LK_RETRY);
760 AUDIT_ARG_VNODE1(vp);
761 error = change_dir(vp, td);
762 while (!error && (mp = vp->v_mountedhere) != NULL) {
765 error = VFS_ROOT(mp, LK_SHARED, &tdp);
782 * Change current working directory (``.'').
784 #ifndef _SYS_SYSPROTO_H_
792 struct chdir_args /* {
797 return (kern_chdir(td, uap->path, UIO_USERSPACE));
801 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
806 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
808 if ((error = namei(&nd)) != 0)
810 if ((error = change_dir(nd.ni_vp, td)) != 0) {
812 NDFREE(&nd, NDF_ONLY_PNBUF);
815 VOP_UNLOCK(nd.ni_vp, 0);
816 NDFREE(&nd, NDF_ONLY_PNBUF);
817 pwd_chdir(td, nd.ni_vp);
822 * Change notion of root (``/'') directory.
824 #ifndef _SYS_SYSPROTO_H_
832 struct chroot_args /* {
839 error = priv_check(td, PRIV_VFS_CHROOT);
842 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
843 UIO_USERSPACE, uap->path, td);
847 error = change_dir(nd.ni_vp, td);
851 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
855 VOP_UNLOCK(nd.ni_vp, 0);
856 error = pwd_chroot(td, nd.ni_vp);
858 NDFREE(&nd, NDF_ONLY_PNBUF);
863 NDFREE(&nd, NDF_ONLY_PNBUF);
868 * Common routine for chroot and chdir. Callers must provide a locked vnode
880 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
881 if (vp->v_type != VDIR)
884 error = mac_vnode_check_chdir(td->td_ucred, vp);
888 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
892 flags_to_rights(int flags, cap_rights_t *rightsp)
895 if (flags & O_EXEC) {
896 cap_rights_set(rightsp, CAP_FEXECVE);
898 switch ((flags & O_ACCMODE)) {
900 cap_rights_set(rightsp, CAP_READ);
903 cap_rights_set(rightsp, CAP_READ);
906 cap_rights_set(rightsp, CAP_WRITE);
907 if (!(flags & (O_APPEND | O_TRUNC)))
908 cap_rights_set(rightsp, CAP_SEEK);
914 cap_rights_set(rightsp, CAP_CREATE);
917 cap_rights_set(rightsp, CAP_FTRUNCATE);
919 if (flags & (O_SYNC | O_FSYNC))
920 cap_rights_set(rightsp, CAP_FSYNC);
922 if (flags & (O_EXLOCK | O_SHLOCK))
923 cap_rights_set(rightsp, CAP_FLOCK);
927 * Check permissions, allocate an open file structure, and call the device
928 * open routine if any.
930 #ifndef _SYS_SYSPROTO_H_
940 register struct open_args /* {
947 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
948 uap->flags, uap->mode));
951 #ifndef _SYS_SYSPROTO_H_
960 sys_openat(struct thread *td, struct openat_args *uap)
963 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
968 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
971 struct proc *p = td->td_proc;
972 struct filedesc *fdp = p->p_fd;
977 int cmode, error, indx;
981 AUDIT_ARG_FFLAGS(flags);
982 AUDIT_ARG_MODE(mode);
983 /* XXX: audit dirfd */
984 cap_rights_init(&rights, CAP_LOOKUP);
985 flags_to_rights(flags, &rights);
987 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
990 if (flags & O_EXEC) {
991 if (flags & O_ACCMODE)
993 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
996 flags = FFLAGS(flags);
1000 * Allocate a file structure. The descriptor to reference it
1001 * is allocated and set by finstall() below.
1003 error = falloc_noinstall(td, &fp);
1007 * An extra reference on `fp' has been held for us by
1008 * falloc_noinstall().
1010 /* Set the flags early so the finit in devfs can pick them up. */
1011 fp->f_flag = flags & FMASK;
1012 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1013 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
1015 td->td_dupfd = -1; /* XXX check for fdopen */
1016 error = vn_open(&nd, &flags, cmode, fp);
1019 * If the vn_open replaced the method vector, something
1020 * wonderous happened deep below and we just pass it up
1021 * pretending we know what we do.
1023 if (error == ENXIO && fp->f_ops != &badfileops)
1027 * Handle special fdopen() case. bleh.
1029 * Don't do this for relative (capability) lookups; we don't
1030 * understand exactly what would happen, and we don't think
1031 * that it ever should.
1033 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
1034 (error == ENODEV || error == ENXIO) &&
1035 td->td_dupfd >= 0) {
1036 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1045 NDFREE(&nd, NDF_ONLY_PNBUF);
1049 * Store the vnode, for any f_type. Typically, the vnode use
1050 * count is decremented by direct call to vn_closefile() for
1051 * files that switched type in the cdevsw fdopen() method.
1055 * If the file wasn't claimed by devfs bind it to the normal
1056 * vnode operations here.
1058 if (fp->f_ops == &badfileops) {
1059 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1061 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK),
1062 DTYPE_VNODE, vp, &vnops);
1066 if (flags & O_TRUNC) {
1067 error = fo_truncate(fp, 0, td->td_ucred, td);
1073 * If we haven't already installed the FD (for dupfdopen), do so now.
1076 struct filecaps *fcaps;
1079 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0)
1080 fcaps = &nd.ni_filecaps;
1084 error = finstall(td, fp, &indx, flags, fcaps);
1085 /* On success finstall() consumes fcaps. */
1087 filecaps_free(&nd.ni_filecaps);
1091 filecaps_free(&nd.ni_filecaps);
1095 * Release our private reference, leaving the one associated with
1096 * the descriptor table intact.
1099 td->td_retval[0] = indx;
1102 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1111 #ifndef _SYS_SYSPROTO_H_
1112 struct ocreat_args {
1120 register struct ocreat_args /* {
1126 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1127 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1129 #endif /* COMPAT_43 */
1132 * Create a special file.
1134 #ifndef _SYS_SYSPROTO_H_
1144 register struct mknod_args /* {
1151 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1152 uap->mode, uap->dev));
1155 #ifndef _SYS_SYSPROTO_H_
1156 struct mknodat_args {
1164 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1167 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1172 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1178 struct nameidata nd;
1179 cap_rights_t rights;
1180 int error, whiteout = 0;
1182 AUDIT_ARG_MODE(mode);
1184 switch (mode & S_IFMT) {
1187 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1188 if (error == 0 && dev == VNOVAL)
1192 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1195 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1199 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1209 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1210 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKNODAT),
1212 if ((error = namei(&nd)) != 0)
1216 NDFREE(&nd, NDF_ONLY_PNBUF);
1217 if (vp == nd.ni_dvp)
1225 vattr.va_mode = (mode & ALLPERMS) &
1226 ~td->td_proc->p_fd->fd_cmask;
1227 vattr.va_rdev = dev;
1230 switch (mode & S_IFMT) {
1231 case S_IFMT: /* used by badsect to flag bad sectors */
1232 vattr.va_type = VBAD;
1235 vattr.va_type = VCHR;
1238 vattr.va_type = VBLK;
1244 panic("kern_mknod: invalid mode");
1247 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1248 NDFREE(&nd, NDF_ONLY_PNBUF);
1250 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1255 if (error == 0 && !whiteout)
1256 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1257 &nd.ni_cnd, &vattr);
1261 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1263 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1264 &nd.ni_cnd, &vattr);
1269 NDFREE(&nd, NDF_ONLY_PNBUF);
1271 vn_finished_write(mp);
1276 * Create a named pipe.
1278 #ifndef _SYS_SYSPROTO_H_
1279 struct mkfifo_args {
1287 register struct mkfifo_args /* {
1293 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1297 #ifndef _SYS_SYSPROTO_H_
1298 struct mkfifoat_args {
1305 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1308 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1313 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1318 struct nameidata nd;
1319 cap_rights_t rights;
1322 AUDIT_ARG_MODE(mode);
1325 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1326 NOCACHE, pathseg, path, fd, cap_rights_init(&rights, CAP_MKFIFOAT),
1328 if ((error = namei(&nd)) != 0)
1330 if (nd.ni_vp != NULL) {
1331 NDFREE(&nd, NDF_ONLY_PNBUF);
1332 if (nd.ni_vp == nd.ni_dvp)
1339 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1340 NDFREE(&nd, NDF_ONLY_PNBUF);
1342 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1347 vattr.va_type = VFIFO;
1348 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1350 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1355 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1362 vn_finished_write(mp);
1363 NDFREE(&nd, NDF_ONLY_PNBUF);
1368 * Make a hard file link.
1370 #ifndef _SYS_SYSPROTO_H_
1379 register struct link_args /* {
1385 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1386 UIO_USERSPACE, FOLLOW));
1389 #ifndef _SYS_SYSPROTO_H_
1390 struct linkat_args {
1399 sys_linkat(struct thread *td, struct linkat_args *uap)
1404 if (flag & ~AT_SYMLINK_FOLLOW)
1407 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1408 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1411 int hardlink_check_uid = 0;
1412 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1413 &hardlink_check_uid, 0,
1414 "Unprivileged processes cannot create hard links to files owned by other "
1416 static int hardlink_check_gid = 0;
1417 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1418 &hardlink_check_gid, 0,
1419 "Unprivileged processes cannot create hard links to files owned by other "
1423 can_hardlink(struct vnode *vp, struct ucred *cred)
1428 if (!hardlink_check_uid && !hardlink_check_gid)
1431 error = VOP_GETATTR(vp, &va, cred);
1435 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1436 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1441 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1442 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1451 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1452 enum uio_seg segflg, int follow)
1456 struct nameidata nd;
1457 cap_rights_t rights;
1462 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflg, path1, fd1,
1463 cap_rights_init(&rights, CAP_LINKAT_SOURCE), td);
1465 if ((error = namei(&nd)) != 0)
1467 NDFREE(&nd, NDF_ONLY_PNBUF);
1469 if (vp->v_type == VDIR) {
1471 return (EPERM); /* POSIX */
1473 NDINIT_ATRIGHTS(&nd, CREATE,
1474 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflg, path2, fd2,
1475 cap_rights_init(&rights, CAP_LINKAT_TARGET), td);
1476 if ((error = namei(&nd)) == 0) {
1477 if (nd.ni_vp != NULL) {
1478 NDFREE(&nd, NDF_ONLY_PNBUF);
1479 if (nd.ni_dvp == nd.ni_vp)
1486 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1488 * Cross-device link. No need to recheck
1489 * vp->v_type, since it cannot change, except
1492 NDFREE(&nd, NDF_ONLY_PNBUF);
1496 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) {
1497 error = can_hardlink(vp, td->td_ucred);
1500 error = mac_vnode_check_link(td->td_ucred,
1501 nd.ni_dvp, vp, &nd.ni_cnd);
1506 NDFREE(&nd, NDF_ONLY_PNBUF);
1509 error = vn_start_write(vp, &mp, V_NOWAIT);
1513 NDFREE(&nd, NDF_ONLY_PNBUF);
1514 error = vn_start_write(NULL, &mp,
1520 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1523 vn_finished_write(mp);
1524 NDFREE(&nd, NDF_ONLY_PNBUF);
1527 NDFREE(&nd, NDF_ONLY_PNBUF);
1537 * Make a symbolic link.
1539 #ifndef _SYS_SYSPROTO_H_
1540 struct symlink_args {
1546 sys_symlink(td, uap)
1548 register struct symlink_args /* {
1554 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1558 #ifndef _SYS_SYSPROTO_H_
1559 struct symlinkat_args {
1566 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1569 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1574 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1575 enum uio_seg segflg)
1580 struct nameidata nd;
1582 cap_rights_t rights;
1584 if (segflg == UIO_SYSSPACE) {
1587 syspath = uma_zalloc(namei_zone, M_WAITOK);
1588 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1591 AUDIT_ARG_TEXT(syspath);
1594 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
1595 NOCACHE, segflg, path2, fd, cap_rights_init(&rights, CAP_SYMLINKAT),
1597 if ((error = namei(&nd)) != 0)
1600 NDFREE(&nd, NDF_ONLY_PNBUF);
1601 if (nd.ni_vp == nd.ni_dvp)
1609 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1610 NDFREE(&nd, NDF_ONLY_PNBUF);
1612 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1617 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1619 vattr.va_type = VLNK;
1620 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1625 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1631 NDFREE(&nd, NDF_ONLY_PNBUF);
1633 vn_finished_write(mp);
1635 if (segflg != UIO_SYSSPACE)
1636 uma_zfree(namei_zone, syspath);
1641 * Delete a whiteout from the filesystem.
1644 sys_undelete(td, uap)
1646 register struct undelete_args /* {
1651 struct nameidata nd;
1656 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1657 UIO_USERSPACE, uap->path, td);
1662 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1663 NDFREE(&nd, NDF_ONLY_PNBUF);
1664 if (nd.ni_vp == nd.ni_dvp)
1672 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1673 NDFREE(&nd, NDF_ONLY_PNBUF);
1675 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1679 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1680 NDFREE(&nd, NDF_ONLY_PNBUF);
1682 vn_finished_write(mp);
1687 * Delete a name from the filesystem.
1689 #ifndef _SYS_SYSPROTO_H_
1690 struct unlink_args {
1697 struct unlink_args /* {
1702 return (kern_unlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 0));
1705 #ifndef _SYS_SYSPROTO_H_
1706 struct unlinkat_args {
1713 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1715 int flag = uap->flag;
1717 char *path = uap->path;
1719 if (flag & ~AT_REMOVEDIR)
1722 if (flag & AT_REMOVEDIR)
1723 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1725 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1729 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1734 struct nameidata nd;
1736 cap_rights_t rights;
1741 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
1742 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
1743 if ((error = namei(&nd)) != 0)
1744 return (error == EINVAL ? EPERM : error);
1746 if (vp->v_type == VDIR && oldinum == 0) {
1747 error = EPERM; /* POSIX */
1748 } else if (oldinum != 0 &&
1749 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1750 sb.st_ino != oldinum) {
1751 error = EIDRM; /* Identifier removed */
1754 * The root of a mounted filesystem cannot be deleted.
1756 * XXX: can this only be a VDIR case?
1758 if (vp->v_vflag & VV_ROOT)
1762 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1763 NDFREE(&nd, NDF_ONLY_PNBUF);
1765 if (vp == nd.ni_dvp)
1769 if ((error = vn_start_write(NULL, &mp,
1770 V_XSLEEP | PCATCH)) != 0)
1775 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1780 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1781 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1785 vn_finished_write(mp);
1787 NDFREE(&nd, NDF_ONLY_PNBUF);
1789 if (vp == nd.ni_dvp)
1797 * Reposition read/write file offset.
1799 #ifndef _SYS_SYSPROTO_H_
1810 register struct lseek_args /* {
1818 cap_rights_t rights;
1821 AUDIT_ARG_FD(uap->fd);
1822 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEEK), &fp);
1825 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1826 fo_seek(fp, uap->offset, uap->whence, td) : ESPIPE;
1831 #if defined(COMPAT_43)
1833 * Reposition read/write file offset.
1835 #ifndef _SYS_SYSPROTO_H_
1836 struct olseek_args {
1845 register struct olseek_args /* {
1851 struct lseek_args /* {
1859 nuap.offset = uap->offset;
1860 nuap.whence = uap->whence;
1861 return (sys_lseek(td, &nuap));
1863 #endif /* COMPAT_43 */
1865 #if defined(COMPAT_FREEBSD6)
1866 /* Version with the 'pad' argument */
1868 freebsd6_lseek(td, uap)
1870 register struct freebsd6_lseek_args *uap;
1872 struct lseek_args ouap;
1875 ouap.offset = uap->offset;
1876 ouap.whence = uap->whence;
1877 return (sys_lseek(td, &ouap));
1882 * Check access permissions using passed credentials.
1885 vn_access(vp, user_flags, cred, td)
1894 /* Flags == 0 means only check for existence. */
1895 if (user_flags == 0)
1899 if (user_flags & R_OK)
1901 if (user_flags & W_OK)
1903 if (user_flags & X_OK)
1906 error = mac_vnode_check_access(cred, vp, accmode);
1910 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1911 error = VOP_ACCESS(vp, accmode, cred, td);
1916 * Check access permissions using "real" credentials.
1918 #ifndef _SYS_SYSPROTO_H_
1919 struct access_args {
1927 register struct access_args /* {
1933 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1937 #ifndef _SYS_SYSPROTO_H_
1938 struct faccessat_args {
1946 sys_faccessat(struct thread *td, struct faccessat_args *uap)
1949 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1954 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1955 int flag, int amode)
1957 struct ucred *cred, *usecred;
1959 struct nameidata nd;
1960 cap_rights_t rights;
1963 if (flag & ~AT_EACCESS)
1965 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
1969 * Create and modify a temporary credential instead of one that
1970 * is potentially shared (if we need one).
1972 cred = td->td_ucred;
1973 if ((flag & AT_EACCESS) == 0 &&
1974 ((cred->cr_uid != cred->cr_ruid ||
1975 cred->cr_rgid != cred->cr_groups[0]))) {
1976 usecred = crdup(cred);
1977 usecred->cr_uid = cred->cr_ruid;
1978 usecred->cr_groups[0] = cred->cr_rgid;
1979 td->td_ucred = usecred;
1982 AUDIT_ARG_VALUE(amode);
1983 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
1984 AUDITVNODE1, pathseg, path, fd, cap_rights_init(&rights, CAP_FSTAT),
1986 if ((error = namei(&nd)) != 0)
1990 error = vn_access(vp, amode, usecred, td);
1991 NDFREE(&nd, NDF_ONLY_PNBUF);
1994 if (usecred != cred) {
1995 td->td_ucred = cred;
2002 * Check access permissions using "effective" credentials.
2004 #ifndef _SYS_SYSPROTO_H_
2005 struct eaccess_args {
2011 sys_eaccess(td, uap)
2013 register struct eaccess_args /* {
2019 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2020 AT_EACCESS, uap->amode));
2023 #if defined(COMPAT_43)
2025 * Get file status; this version follows links.
2027 #ifndef _SYS_SYSPROTO_H_
2036 register struct ostat_args /* {
2045 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2050 return (copyout(&osb, uap->ub, sizeof (osb)));
2054 * Get file status; this version does not follow links.
2056 #ifndef _SYS_SYSPROTO_H_
2057 struct olstat_args {
2065 register struct olstat_args /* {
2074 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2075 UIO_USERSPACE, &sb, NULL);
2079 return (copyout(&osb, uap->ub, sizeof (osb)));
2083 * Convert from an old to a new stat structure.
2091 bzero(ost, sizeof(*ost));
2092 ost->st_dev = st->st_dev;
2093 ost->st_ino = st->st_ino;
2094 ost->st_mode = st->st_mode;
2095 ost->st_nlink = st->st_nlink;
2096 ost->st_uid = st->st_uid;
2097 ost->st_gid = st->st_gid;
2098 ost->st_rdev = st->st_rdev;
2099 if (st->st_size < (quad_t)1 << 32)
2100 ost->st_size = st->st_size;
2103 ost->st_atim = st->st_atim;
2104 ost->st_mtim = st->st_mtim;
2105 ost->st_ctim = st->st_ctim;
2106 ost->st_blksize = st->st_blksize;
2107 ost->st_blocks = st->st_blocks;
2108 ost->st_flags = st->st_flags;
2109 ost->st_gen = st->st_gen;
2111 #endif /* COMPAT_43 */
2114 * Get file status; this version follows links.
2116 #ifndef _SYS_SYSPROTO_H_
2125 register struct stat_args /* {
2133 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2136 error = copyout(&sb, uap->ub, sizeof (sb));
2140 #ifndef _SYS_SYSPROTO_H_
2141 struct fstatat_args {
2149 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2154 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2155 UIO_USERSPACE, &sb, NULL);
2157 error = copyout(&sb, uap->buf, sizeof (sb));
2162 kern_statat(struct thread *td, int flag, int fd, char *path,
2163 enum uio_seg pathseg, struct stat *sbp,
2164 void (*hook)(struct vnode *vp, struct stat *sbp))
2166 struct nameidata nd;
2168 cap_rights_t rights;
2171 if (flag & ~AT_SYMLINK_NOFOLLOW)
2174 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2175 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd,
2176 cap_rights_init(&rights, CAP_FSTAT), td);
2178 if ((error = namei(&nd)) != 0)
2180 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2182 SDT_PROBE2(vfs, , stat, mode, path, sb.st_mode);
2183 if (S_ISREG(sb.st_mode))
2184 SDT_PROBE2(vfs, , stat, reg, path, pathseg);
2185 if (__predict_false(hook != NULL))
2186 hook(nd.ni_vp, &sb);
2188 NDFREE(&nd, NDF_ONLY_PNBUF);
2194 if (KTRPOINT(td, KTR_STRUCT))
2201 * Get file status; this version does not follow links.
2203 #ifndef _SYS_SYSPROTO_H_
2212 register struct lstat_args /* {
2220 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2221 UIO_USERSPACE, &sb, NULL);
2223 error = copyout(&sb, uap->ub, sizeof (sb));
2228 * Implementation of the NetBSD [l]stat() functions.
2236 bzero(nsb, sizeof *nsb);
2237 nsb->st_dev = sb->st_dev;
2238 nsb->st_ino = sb->st_ino;
2239 nsb->st_mode = sb->st_mode;
2240 nsb->st_nlink = sb->st_nlink;
2241 nsb->st_uid = sb->st_uid;
2242 nsb->st_gid = sb->st_gid;
2243 nsb->st_rdev = sb->st_rdev;
2244 nsb->st_atim = sb->st_atim;
2245 nsb->st_mtim = sb->st_mtim;
2246 nsb->st_ctim = sb->st_ctim;
2247 nsb->st_size = sb->st_size;
2248 nsb->st_blocks = sb->st_blocks;
2249 nsb->st_blksize = sb->st_blksize;
2250 nsb->st_flags = sb->st_flags;
2251 nsb->st_gen = sb->st_gen;
2252 nsb->st_birthtim = sb->st_birthtim;
2255 #ifndef _SYS_SYSPROTO_H_
2264 register struct nstat_args /* {
2273 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2277 cvtnstat(&sb, &nsb);
2278 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2282 * NetBSD lstat. Get file status; this version does not follow links.
2284 #ifndef _SYS_SYSPROTO_H_
2293 register struct nlstat_args /* {
2302 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2303 UIO_USERSPACE, &sb, NULL);
2306 cvtnstat(&sb, &nsb);
2307 return (copyout(&nsb, uap->ub, sizeof (nsb)));
2311 * Get configurable pathname variables.
2313 #ifndef _SYS_SYSPROTO_H_
2314 struct pathconf_args {
2320 sys_pathconf(td, uap)
2322 register struct pathconf_args /* {
2328 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2331 #ifndef _SYS_SYSPROTO_H_
2332 struct lpathconf_args {
2338 sys_lpathconf(td, uap)
2340 register struct lpathconf_args /* {
2346 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2351 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2354 struct nameidata nd;
2357 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2359 if ((error = namei(&nd)) != 0)
2361 NDFREE(&nd, NDF_ONLY_PNBUF);
2363 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2369 * Return target name of a symbolic link.
2371 #ifndef _SYS_SYSPROTO_H_
2372 struct readlink_args {
2379 sys_readlink(td, uap)
2381 register struct readlink_args /* {
2388 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2389 uap->buf, UIO_USERSPACE, uap->count));
2391 #ifndef _SYS_SYSPROTO_H_
2392 struct readlinkat_args {
2400 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2403 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2404 uap->buf, UIO_USERSPACE, uap->bufsize));
2408 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2409 char *buf, enum uio_seg bufseg, size_t count)
2414 struct nameidata nd;
2417 if (count > IOSIZE_MAX)
2420 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
2421 pathseg, path, fd, td);
2423 if ((error = namei(&nd)) != 0)
2425 NDFREE(&nd, NDF_ONLY_PNBUF);
2428 error = mac_vnode_check_readlink(td->td_ucred, vp);
2434 if (vp->v_type != VLNK)
2437 aiov.iov_base = buf;
2438 aiov.iov_len = count;
2439 auio.uio_iov = &aiov;
2440 auio.uio_iovcnt = 1;
2441 auio.uio_offset = 0;
2442 auio.uio_rw = UIO_READ;
2443 auio.uio_segflg = bufseg;
2445 auio.uio_resid = count;
2446 error = VOP_READLINK(vp, &auio, td->td_ucred);
2447 td->td_retval[0] = count - auio.uio_resid;
2454 * Common implementation code for chflags() and fchflags().
2457 setfflags(td, vp, flags)
2466 /* We can't support the value matching VNOVAL. */
2467 if (flags == VNOVAL)
2468 return (EOPNOTSUPP);
2471 * Prevent non-root users from setting flags on devices. When
2472 * a device is reused, users can retain ownership of the device
2473 * if they are allowed to set flags and programs assume that
2474 * chown can't fail when done as root.
2476 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2477 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2482 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2485 vattr.va_flags = flags;
2486 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2488 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2491 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2493 vn_finished_write(mp);
2498 * Change flags of a file given a path name.
2500 #ifndef _SYS_SYSPROTO_H_
2501 struct chflags_args {
2507 sys_chflags(td, uap)
2509 register struct chflags_args /* {
2515 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2519 #ifndef _SYS_SYSPROTO_H_
2520 struct chflagsat_args {
2528 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2531 const char *path = uap->path;
2532 u_long flags = uap->flags;
2533 int atflag = uap->atflag;
2535 if (atflag & ~AT_SYMLINK_NOFOLLOW)
2538 return (kern_chflagsat(td, fd, path, UIO_USERSPACE, flags, atflag));
2542 * Same as chflags() but doesn't follow symlinks.
2545 sys_lchflags(td, uap)
2547 register struct lchflags_args /* {
2553 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2554 uap->flags, AT_SYMLINK_NOFOLLOW));
2558 kern_chflagsat(struct thread *td, int fd, const char *path,
2559 enum uio_seg pathseg, u_long flags, int atflag)
2561 struct nameidata nd;
2562 cap_rights_t rights;
2565 AUDIT_ARG_FFLAGS(flags);
2566 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2567 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2568 cap_rights_init(&rights, CAP_FCHFLAGS), td);
2569 if ((error = namei(&nd)) != 0)
2571 NDFREE(&nd, NDF_ONLY_PNBUF);
2572 error = setfflags(td, nd.ni_vp, flags);
2578 * Change flags of a file given a file descriptor.
2580 #ifndef _SYS_SYSPROTO_H_
2581 struct fchflags_args {
2587 sys_fchflags(td, uap)
2589 register struct fchflags_args /* {
2595 cap_rights_t rights;
2598 AUDIT_ARG_FD(uap->fd);
2599 AUDIT_ARG_FFLAGS(uap->flags);
2600 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_FCHFLAGS),
2605 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2606 AUDIT_ARG_VNODE1(fp->f_vnode);
2607 VOP_UNLOCK(fp->f_vnode, 0);
2609 error = setfflags(td, fp->f_vnode, uap->flags);
2615 * Common implementation code for chmod(), lchmod() and fchmod().
2618 setfmode(td, cred, vp, mode)
2628 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2630 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2632 vattr.va_mode = mode & ALLPERMS;
2634 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2637 error = VOP_SETATTR(vp, &vattr, cred);
2639 vn_finished_write(mp);
2644 * Change mode of a file given path name.
2646 #ifndef _SYS_SYSPROTO_H_
2655 register struct chmod_args /* {
2661 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2665 #ifndef _SYS_SYSPROTO_H_
2666 struct fchmodat_args {
2674 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2676 int flag = uap->flag;
2678 char *path = uap->path;
2679 mode_t mode = uap->mode;
2681 if (flag & ~AT_SYMLINK_NOFOLLOW)
2684 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2688 * Change mode of a file given path name (don't follow links.)
2690 #ifndef _SYS_SYSPROTO_H_
2691 struct lchmod_args {
2699 register struct lchmod_args /* {
2705 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2706 uap->mode, AT_SYMLINK_NOFOLLOW));
2710 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2711 mode_t mode, int flag)
2713 struct nameidata nd;
2714 cap_rights_t rights;
2717 AUDIT_ARG_MODE(mode);
2718 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2719 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2720 cap_rights_init(&rights, CAP_FCHMOD), td);
2721 if ((error = namei(&nd)) != 0)
2723 NDFREE(&nd, NDF_ONLY_PNBUF);
2724 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2730 * Change mode of a file given a file descriptor.
2732 #ifndef _SYS_SYSPROTO_H_
2733 struct fchmod_args {
2739 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2742 cap_rights_t rights;
2745 AUDIT_ARG_FD(uap->fd);
2746 AUDIT_ARG_MODE(uap->mode);
2748 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHMOD), &fp);
2751 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2757 * Common implementation for chown(), lchown(), and fchown()
2760 setfown(td, cred, vp, uid, gid)
2771 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2773 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2778 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2782 error = VOP_SETATTR(vp, &vattr, cred);
2784 vn_finished_write(mp);
2789 * Set ownership given a path name.
2791 #ifndef _SYS_SYSPROTO_H_
2801 register struct chown_args /* {
2808 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
2812 #ifndef _SYS_SYSPROTO_H_
2813 struct fchownat_args {
2822 sys_fchownat(struct thread *td, struct fchownat_args *uap)
2827 if (flag & ~AT_SYMLINK_NOFOLLOW)
2830 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
2831 uap->gid, uap->flag));
2835 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2836 int uid, int gid, int flag)
2838 struct nameidata nd;
2839 cap_rights_t rights;
2842 AUDIT_ARG_OWNER(uid, gid);
2843 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2844 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd,
2845 cap_rights_init(&rights, CAP_FCHOWN), td);
2847 if ((error = namei(&nd)) != 0)
2849 NDFREE(&nd, NDF_ONLY_PNBUF);
2850 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
2856 * Set ownership given a path name, do not cross symlinks.
2858 #ifndef _SYS_SYSPROTO_H_
2859 struct lchown_args {
2868 register struct lchown_args /* {
2875 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2876 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
2880 * Set ownership given a file descriptor.
2882 #ifndef _SYS_SYSPROTO_H_
2883 struct fchown_args {
2892 register struct fchown_args /* {
2899 cap_rights_t rights;
2902 AUDIT_ARG_FD(uap->fd);
2903 AUDIT_ARG_OWNER(uap->uid, uap->gid);
2904 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FCHOWN), &fp);
2907 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
2913 * Common implementation code for utimes(), lutimes(), and futimes().
2916 getutimes(usrtvp, tvpseg, tsp)
2917 const struct timeval *usrtvp;
2918 enum uio_seg tvpseg;
2919 struct timespec *tsp;
2921 struct timeval tv[2];
2922 const struct timeval *tvp;
2925 if (usrtvp == NULL) {
2926 vfs_timestamp(&tsp[0]);
2929 if (tvpseg == UIO_SYSSPACE) {
2932 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2937 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2938 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2940 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2941 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2947 * Common implementation code for futimens(), utimensat().
2949 #define UTIMENS_NULL 0x1
2950 #define UTIMENS_EXIT 0x2
2952 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
2953 struct timespec *tsp, int *retflags)
2955 struct timespec tsnow;
2958 vfs_timestamp(&tsnow);
2960 if (usrtsp == NULL) {
2963 *retflags |= UTIMENS_NULL;
2966 if (tspseg == UIO_SYSSPACE) {
2969 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
2971 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
2972 *retflags |= UTIMENS_EXIT;
2973 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
2974 *retflags |= UTIMENS_NULL;
2975 if (tsp[0].tv_nsec == UTIME_OMIT)
2976 tsp[0].tv_sec = VNOVAL;
2977 else if (tsp[0].tv_nsec == UTIME_NOW)
2979 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
2981 if (tsp[1].tv_nsec == UTIME_OMIT)
2982 tsp[1].tv_sec = VNOVAL;
2983 else if (tsp[1].tv_nsec == UTIME_NOW)
2985 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
2992 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
2996 setutimes(td, vp, ts, numtimes, nullflag)
2999 const struct timespec *ts;
3005 int error, setbirthtime;
3007 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3009 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3011 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3012 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3015 vattr.va_atime = ts[0];
3016 vattr.va_mtime = ts[1];
3018 vattr.va_birthtime = ts[1];
3020 vattr.va_birthtime = ts[2];
3022 vattr.va_vaflags |= VA_UTIMES_NULL;
3024 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3028 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3030 vn_finished_write(mp);
3035 * Set the access and modification times of a file.
3037 #ifndef _SYS_SYSPROTO_H_
3038 struct utimes_args {
3040 struct timeval *tptr;
3046 register struct utimes_args /* {
3048 struct timeval *tptr;
3052 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3053 uap->tptr, UIO_USERSPACE));
3056 #ifndef _SYS_SYSPROTO_H_
3057 struct futimesat_args {
3060 const struct timeval * times;
3064 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3067 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3068 uap->times, UIO_USERSPACE));
3072 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3073 struct timeval *tptr, enum uio_seg tptrseg)
3075 struct nameidata nd;
3076 struct timespec ts[2];
3077 cap_rights_t rights;
3080 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3082 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3083 cap_rights_init(&rights, CAP_FUTIMES), td);
3085 if ((error = namei(&nd)) != 0)
3087 NDFREE(&nd, NDF_ONLY_PNBUF);
3088 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3094 * Set the access and modification times of a file.
3096 #ifndef _SYS_SYSPROTO_H_
3097 struct lutimes_args {
3099 struct timeval *tptr;
3103 sys_lutimes(td, uap)
3105 register struct lutimes_args /* {
3107 struct timeval *tptr;
3111 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3116 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3117 struct timeval *tptr, enum uio_seg tptrseg)
3119 struct timespec ts[2];
3120 struct nameidata nd;
3123 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3125 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td);
3126 if ((error = namei(&nd)) != 0)
3128 NDFREE(&nd, NDF_ONLY_PNBUF);
3129 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3135 * Set the access and modification times of a file.
3137 #ifndef _SYS_SYSPROTO_H_
3138 struct futimes_args {
3140 struct timeval *tptr;
3144 sys_futimes(td, uap)
3146 register struct futimes_args /* {
3148 struct timeval *tptr;
3152 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3156 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3157 enum uio_seg tptrseg)
3159 struct timespec ts[2];
3161 cap_rights_t rights;
3165 error = getutimes(tptr, tptrseg, ts);
3168 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3172 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3173 AUDIT_ARG_VNODE1(fp->f_vnode);
3174 VOP_UNLOCK(fp->f_vnode, 0);
3176 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3182 sys_futimens(struct thread *td, struct futimens_args *uap)
3185 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3189 kern_futimens(struct thread *td, int fd, struct timespec *tptr,
3190 enum uio_seg tptrseg)
3192 struct timespec ts[2];
3194 cap_rights_t rights;
3198 error = getutimens(tptr, tptrseg, ts, &flags);
3201 if (flags & UTIMENS_EXIT)
3203 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FUTIMES), &fp);
3207 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3208 AUDIT_ARG_VNODE1(fp->f_vnode);
3209 VOP_UNLOCK(fp->f_vnode, 0);
3211 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3217 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3220 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3221 uap->times, UIO_USERSPACE, uap->flag));
3225 kern_utimensat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3226 struct timespec *tptr, enum uio_seg tptrseg, int flag)
3228 struct nameidata nd;
3229 struct timespec ts[2];
3230 cap_rights_t rights;
3233 if (flag & ~AT_SYMLINK_NOFOLLOW)
3236 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3238 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
3239 FOLLOW) | AUDITVNODE1, pathseg, path, fd,
3240 cap_rights_init(&rights, CAP_FUTIMES), td);
3241 if ((error = namei(&nd)) != 0)
3244 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3246 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3247 * "Search permission is denied by a component of the path prefix."
3249 NDFREE(&nd, NDF_ONLY_PNBUF);
3250 if ((flags & UTIMENS_EXIT) == 0)
3251 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3257 * Truncate a file given its path name.
3259 #ifndef _SYS_SYSPROTO_H_
3260 struct truncate_args {
3267 sys_truncate(td, uap)
3269 register struct truncate_args /* {
3276 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3280 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3286 struct nameidata nd;
3291 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td);
3292 if ((error = namei(&nd)) != 0)
3295 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3296 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3297 vn_rangelock_unlock(vp, rl_cookie);
3301 NDFREE(&nd, NDF_ONLY_PNBUF);
3302 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3303 if (vp->v_type == VDIR)
3306 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3309 else if ((error = vn_writechk(vp)) == 0 &&
3310 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3312 vattr.va_size = length;
3313 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3316 vn_finished_write(mp);
3317 vn_rangelock_unlock(vp, rl_cookie);
3322 #if defined(COMPAT_43)
3324 * Truncate a file given its path name.
3326 #ifndef _SYS_SYSPROTO_H_
3327 struct otruncate_args {
3335 register struct otruncate_args /* {
3340 struct truncate_args /* {
3346 nuap.path = uap->path;
3347 nuap.length = uap->length;
3348 return (sys_truncate(td, &nuap));
3350 #endif /* COMPAT_43 */
3352 #if defined(COMPAT_FREEBSD6)
3353 /* Versions with the pad argument */
3355 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3357 struct truncate_args ouap;
3359 ouap.path = uap->path;
3360 ouap.length = uap->length;
3361 return (sys_truncate(td, &ouap));
3365 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3367 struct ftruncate_args ouap;
3370 ouap.length = uap->length;
3371 return (sys_ftruncate(td, &ouap));
3376 kern_fsync(struct thread *td, int fd, bool fullsync)
3381 cap_rights_t rights;
3382 int error, lock_flags;
3385 error = getvnode(td, fd, cap_rights_init(&rights, CAP_FSYNC), &fp);
3391 /* XXXKIB: compete outstanding aio writes */;
3393 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
3396 if (MNT_SHARED_WRITES(mp) ||
3397 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3398 lock_flags = LK_SHARED;
3400 lock_flags = LK_EXCLUSIVE;
3402 vn_lock(vp, lock_flags | LK_RETRY);
3403 AUDIT_ARG_VNODE1(vp);
3404 if (vp->v_object != NULL) {
3405 VM_OBJECT_WLOCK(vp->v_object);
3406 vm_object_page_clean(vp->v_object, 0, 0, 0);
3407 VM_OBJECT_WUNLOCK(vp->v_object);
3409 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3411 vn_finished_write(mp);
3418 * Sync an open file.
3420 #ifndef _SYS_SYSPROTO_H_
3426 sys_fsync(struct thread *td, struct fsync_args *uap)
3429 return (kern_fsync(td, uap->fd, true));
3433 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3436 return (kern_fsync(td, uap->fd, false));
3440 * Rename files. Source and destination must either both be directories, or
3441 * both not be directories. If target is a directory, it must be empty.
3443 #ifndef _SYS_SYSPROTO_H_
3444 struct rename_args {
3452 register struct rename_args /* {
3458 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3459 uap->to, UIO_USERSPACE));
3462 #ifndef _SYS_SYSPROTO_H_
3463 struct renameat_args {
3471 sys_renameat(struct thread *td, struct renameat_args *uap)
3474 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3479 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3480 enum uio_seg pathseg)
3482 struct mount *mp = NULL;
3483 struct vnode *tvp, *fvp, *tdvp;
3484 struct nameidata fromnd, tond;
3485 cap_rights_t rights;
3491 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3492 AUDITVNODE1, pathseg, old, oldfd,
3493 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3495 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1,
3496 pathseg, old, oldfd,
3497 cap_rights_init(&rights, CAP_RENAMEAT_SOURCE), td);
3500 if ((error = namei(&fromnd)) != 0)
3503 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3504 fromnd.ni_vp, &fromnd.ni_cnd);
3505 VOP_UNLOCK(fromnd.ni_dvp, 0);
3506 if (fromnd.ni_dvp != fromnd.ni_vp)
3507 VOP_UNLOCK(fromnd.ni_vp, 0);
3510 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3511 SAVESTART | AUDITVNODE2, pathseg, new, newfd,
3512 cap_rights_init(&rights, CAP_RENAMEAT_TARGET), td);
3513 if (fromnd.ni_vp->v_type == VDIR)
3514 tond.ni_cnd.cn_flags |= WILLBEDIR;
3515 if ((error = namei(&tond)) != 0) {
3516 /* Translate error code for rename("dir1", "dir2/."). */
3517 if (error == EISDIR && fvp->v_type == VDIR)
3519 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3520 vrele(fromnd.ni_dvp);
3526 error = vn_start_write(fvp, &mp, V_NOWAIT);
3528 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3529 NDFREE(&tond, NDF_ONLY_PNBUF);
3536 vrele(fromnd.ni_dvp);
3538 vrele(tond.ni_startdir);
3539 if (fromnd.ni_startdir != NULL)
3540 vrele(fromnd.ni_startdir);
3541 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
3547 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3550 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3555 if (newfd != AT_FDCWD) {
3557 * If the target already exists we require CAP_UNLINKAT
3560 error = cap_check(&tond.ni_filecaps.fc_rights,
3561 cap_rights_init(&rights, CAP_UNLINKAT));
3572 * If the source is the same as the destination (that is, if they
3573 * are links to the same vnode), then there is nothing to do.
3579 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3580 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3584 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3585 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3586 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3587 NDFREE(&tond, NDF_ONLY_PNBUF);
3589 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3590 NDFREE(&tond, NDF_ONLY_PNBUF);
3597 vrele(fromnd.ni_dvp);
3600 vrele(tond.ni_startdir);
3601 vn_finished_write(mp);
3603 if (fromnd.ni_startdir)
3604 vrele(fromnd.ni_startdir);
3611 * Make a directory file.
3613 #ifndef _SYS_SYSPROTO_H_
3622 register struct mkdir_args /* {
3628 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3632 #ifndef _SYS_SYSPROTO_H_
3633 struct mkdirat_args {
3640 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3643 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3647 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3653 struct nameidata nd;
3654 cap_rights_t rights;
3657 AUDIT_ARG_MODE(mode);
3660 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 |
3661 NOCACHE, segflg, path, fd, cap_rights_init(&rights, CAP_MKDIRAT),
3663 nd.ni_cnd.cn_flags |= WILLBEDIR;
3664 if ((error = namei(&nd)) != 0)
3668 NDFREE(&nd, NDF_ONLY_PNBUF);
3670 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3671 * the strange behaviour of leaving the vnode unlocked
3672 * if the target is the same vnode as the parent.
3674 if (vp == nd.ni_dvp)
3681 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3682 NDFREE(&nd, NDF_ONLY_PNBUF);
3684 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3689 vattr.va_type = VDIR;
3690 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3692 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3697 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3701 NDFREE(&nd, NDF_ONLY_PNBUF);
3705 vn_finished_write(mp);
3710 * Remove a directory file.
3712 #ifndef _SYS_SYSPROTO_H_
3720 struct rmdir_args /* {
3725 return (kern_rmdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE));
3729 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3733 struct nameidata nd;
3734 cap_rights_t rights;
3739 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3740 pathseg, path, fd, cap_rights_init(&rights, CAP_UNLINKAT), td);
3741 if ((error = namei(&nd)) != 0)
3744 if (vp->v_type != VDIR) {
3749 * No rmdir "." please.
3751 if (nd.ni_dvp == vp) {
3756 * The root of a mounted filesystem cannot be deleted.
3758 if (vp->v_vflag & VV_ROOT) {
3763 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3768 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3769 NDFREE(&nd, NDF_ONLY_PNBUF);
3771 if (nd.ni_dvp == vp)
3775 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3779 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3780 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3781 vn_finished_write(mp);
3783 NDFREE(&nd, NDF_ONLY_PNBUF);
3785 if (nd.ni_dvp == vp)
3794 * Read a block of directory entries in a filesystem independent format.
3796 #ifndef _SYS_SYSPROTO_H_
3797 struct ogetdirentries_args {
3805 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3810 error = kern_ogetdirentries(td, uap, &loff);
3812 error = copyout(&loff, uap->basep, sizeof(long));
3817 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3822 struct uio auio, kuio;
3823 struct iovec aiov, kiov;
3824 struct dirent *dp, *edp;
3825 cap_rights_t rights;
3827 int error, eofflag, readcnt;
3831 /* XXX arbitrary sanity limit on `count'. */
3832 if (uap->count > 64 * 1024)
3834 error = getvnode(td, uap->fd, cap_rights_init(&rights, CAP_READ), &fp);
3837 if ((fp->f_flag & FREAD) == 0) {
3842 foffset = foffset_lock(fp, 0);
3844 if (vp->v_type != VDIR) {
3845 foffset_unlock(fp, foffset, 0);
3849 aiov.iov_base = uap->buf;
3850 aiov.iov_len = uap->count;
3851 auio.uio_iov = &aiov;
3852 auio.uio_iovcnt = 1;
3853 auio.uio_rw = UIO_READ;
3854 auio.uio_segflg = UIO_USERSPACE;
3856 auio.uio_resid = uap->count;
3857 vn_lock(vp, LK_SHARED | LK_RETRY);
3858 loff = auio.uio_offset = foffset;
3860 error = mac_vnode_check_readdir(td->td_ucred, vp);
3863 foffset_unlock(fp, foffset, FOF_NOUPDATE);
3868 # if (BYTE_ORDER != LITTLE_ENDIAN)
3869 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3870 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3872 foffset = auio.uio_offset;
3877 kuio.uio_iov = &kiov;
3878 kuio.uio_segflg = UIO_SYSSPACE;
3879 kiov.iov_len = uap->count;
3880 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3881 kiov.iov_base = dirbuf;
3882 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3884 foffset = kuio.uio_offset;
3886 readcnt = uap->count - kuio.uio_resid;
3887 edp = (struct dirent *)&dirbuf[readcnt];
3888 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3889 # if (BYTE_ORDER == LITTLE_ENDIAN)
3891 * The expected low byte of
3892 * dp->d_namlen is our dp->d_type.
3893 * The high MBZ byte of dp->d_namlen
3894 * is our dp->d_namlen.
3896 dp->d_type = dp->d_namlen;
3900 * The dp->d_type is the high byte
3901 * of the expected dp->d_namlen,
3902 * so must be zero'ed.
3906 if (dp->d_reclen > 0) {
3907 dp = (struct dirent *)
3908 ((char *)dp + dp->d_reclen);
3915 error = uiomove(dirbuf, readcnt, &auio);
3917 free(dirbuf, M_TEMP);
3921 foffset_unlock(fp, foffset, 0);
3925 if (uap->count == auio.uio_resid &&
3926 (vp->v_vflag & VV_ROOT) &&
3927 (vp->v_mount->mnt_flag & MNT_UNION)) {
3928 struct vnode *tvp = vp;
3929 vp = vp->v_mount->mnt_vnodecovered;
3938 foffset_unlock(fp, foffset, 0);
3940 td->td_retval[0] = uap->count - auio.uio_resid;
3945 #endif /* COMPAT_43 */
3948 * Read a block of directory entries in a filesystem independent format.
3950 #ifndef _SYS_SYSPROTO_H_
3951 struct getdirentries_args {
3959 sys_getdirentries(td, uap)
3961 register struct getdirentries_args /* {
3971 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
3972 NULL, UIO_USERSPACE);
3975 if (uap->basep != NULL)
3976 error = copyout(&base, uap->basep, sizeof(long));
3981 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
3982 long *basep, ssize_t *residp, enum uio_seg bufseg)
3988 cap_rights_t rights;
3994 if (count > IOSIZE_MAX)
3996 auio.uio_resid = count;
3997 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
4000 if ((fp->f_flag & FREAD) == 0) {
4005 foffset = foffset_lock(fp, 0);
4007 if (vp->v_type != VDIR) {
4011 aiov.iov_base = buf;
4012 aiov.iov_len = count;
4013 auio.uio_iov = &aiov;
4014 auio.uio_iovcnt = 1;
4015 auio.uio_rw = UIO_READ;
4016 auio.uio_segflg = bufseg;
4018 vn_lock(vp, LK_SHARED | LK_RETRY);
4019 AUDIT_ARG_VNODE1(vp);
4020 loff = auio.uio_offset = foffset;
4022 error = mac_vnode_check_readdir(td->td_ucred, vp);
4025 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4027 foffset = auio.uio_offset;
4032 if (count == auio.uio_resid &&
4033 (vp->v_vflag & VV_ROOT) &&
4034 (vp->v_mount->mnt_flag & MNT_UNION)) {
4035 struct vnode *tvp = vp;
4037 vp = vp->v_mount->mnt_vnodecovered;
4048 *residp = auio.uio_resid;
4049 td->td_retval[0] = count - auio.uio_resid;
4051 foffset_unlock(fp, foffset, 0);
4056 #ifndef _SYS_SYSPROTO_H_
4057 struct getdents_args {
4064 sys_getdents(td, uap)
4066 register struct getdents_args /* {
4072 struct getdirentries_args ap;
4076 ap.count = uap->count;
4078 return (sys_getdirentries(td, &ap));
4082 * Set the mode mask for creation of filesystem nodes.
4084 #ifndef _SYS_SYSPROTO_H_
4092 struct umask_args /* {
4096 struct filedesc *fdp;
4098 fdp = td->td_proc->p_fd;
4099 FILEDESC_XLOCK(fdp);
4100 td->td_retval[0] = fdp->fd_cmask;
4101 fdp->fd_cmask = uap->newmask & ALLPERMS;
4102 FILEDESC_XUNLOCK(fdp);
4107 * Void all references to file by ripping underlying filesystem away from
4110 #ifndef _SYS_SYSPROTO_H_
4111 struct revoke_args {
4118 register struct revoke_args /* {
4124 struct nameidata nd;
4127 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4129 if ((error = namei(&nd)) != 0)
4132 NDFREE(&nd, NDF_ONLY_PNBUF);
4133 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4138 error = mac_vnode_check_revoke(td->td_ucred, vp);
4142 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4145 if (td->td_ucred->cr_uid != vattr.va_uid) {
4146 error = priv_check(td, PRIV_VFS_ADMIN);
4151 VOP_REVOKE(vp, REVOKEALL);
4158 * Convert a user file descriptor to a kernel file entry and check that, if it
4159 * is a capability, the correct rights are present. A reference on the file
4160 * entry is held upon returning.
4163 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4168 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp, NULL);
4173 * The file could be not of the vnode type, or it may be not
4174 * yet fully initialized, in which case the f_vnode pointer
4175 * may be set, but f_ops is still badfileops. E.g.,
4176 * devfs_open() transiently create such situation to
4177 * facilitate csw d_fdopen().
4179 * Dupfdopen() handling in kern_openat() installs the
4180 * half-baked file into the process descriptor table, allowing
4181 * other thread to dereference it. Guard against the race by
4184 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4194 * Get an (NFS) file handle.
4196 #ifndef _SYS_SYSPROTO_H_
4197 struct lgetfh_args {
4205 register struct lgetfh_args *uap;
4207 struct nameidata nd;
4209 register struct vnode *vp;
4212 error = priv_check(td, PRIV_VFS_GETFH);
4215 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4220 NDFREE(&nd, NDF_ONLY_PNBUF);
4222 bzero(&fh, sizeof(fh));
4223 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4224 error = VOP_VPTOFH(vp, &fh.fh_fid);
4227 error = copyout(&fh, uap->fhp, sizeof (fh));
4231 #ifndef _SYS_SYSPROTO_H_
4240 register struct getfh_args *uap;
4242 struct nameidata nd;
4244 register struct vnode *vp;
4247 error = priv_check(td, PRIV_VFS_GETFH);
4250 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4255 NDFREE(&nd, NDF_ONLY_PNBUF);
4257 bzero(&fh, sizeof(fh));
4258 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4259 error = VOP_VPTOFH(vp, &fh.fh_fid);
4262 error = copyout(&fh, uap->fhp, sizeof (fh));
4267 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4270 * warning: do not remove the priv_check() call or this becomes one giant
4273 #ifndef _SYS_SYSPROTO_H_
4274 struct fhopen_args {
4275 const struct fhandle *u_fhp;
4282 struct fhopen_args /* {
4283 const struct fhandle *u_fhp;
4294 error = priv_check(td, PRIV_VFS_FHOPEN);
4298 fmode = FFLAGS(uap->flags);
4299 /* why not allow a non-read/write open for our lockd? */
4300 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4302 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4305 /* find the mount point */
4306 mp = vfs_busyfs(&fhp.fh_fsid);
4309 /* now give me my vnode, it gets returned to me locked */
4310 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4315 error = falloc_noinstall(td, &fp);
4321 * An extra reference on `fp' has been held for us by
4322 * falloc_noinstall().
4328 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4330 KASSERT(fp->f_ops == &badfileops,
4331 ("VOP_OPEN in fhopen() set f_ops"));
4332 KASSERT(td->td_dupfd < 0,
4333 ("fhopen() encountered fdopen()"));
4343 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp,
4346 if ((fmode & O_TRUNC) != 0) {
4347 error = fo_truncate(fp, 0, td->td_ucred, td);
4352 error = finstall(td, fp, &indx, fmode, NULL);
4355 td->td_retval[0] = indx;
4360 * Stat an (NFS) file handle.
4362 #ifndef _SYS_SYSPROTO_H_
4363 struct fhstat_args {
4364 struct fhandle *u_fhp;
4371 register struct fhstat_args /* {
4372 struct fhandle *u_fhp;
4380 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4383 error = kern_fhstat(td, fh, &sb);
4385 error = copyout(&sb, uap->sb, sizeof(sb));
4390 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4396 error = priv_check(td, PRIV_VFS_FHSTAT);
4399 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4401 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4405 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td);
4411 * Implement fstatfs() for (NFS) file handles.
4413 #ifndef _SYS_SYSPROTO_H_
4414 struct fhstatfs_args {
4415 struct fhandle *u_fhp;
4420 sys_fhstatfs(td, uap)
4422 struct fhstatfs_args /* {
4423 struct fhandle *u_fhp;
4431 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4434 error = kern_fhstatfs(td, fh, &sf);
4437 return (copyout(&sf, uap->buf, sizeof(sf)));
4441 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4448 error = priv_check(td, PRIV_VFS_FHSTATFS);
4451 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4453 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4459 error = prison_canseemount(td->td_ucred, mp);
4463 error = mac_mount_check_stat(td->td_ucred, mp);
4468 * Set these in case the underlying filesystem fails to do so.
4471 sp->f_version = STATFS_VERSION;
4472 sp->f_namemax = NAME_MAX;
4473 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4474 error = VFS_STATFS(mp, sp);
4483 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4488 cap_rights_t rights;
4489 off_t olen, ooffset;
4492 if (offset < 0 || len <= 0)
4494 /* Check for wrap. */
4495 if (offset > OFF_MAX - len)
4497 error = fget(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
4500 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4504 if ((fp->f_flag & FWRITE) == 0) {
4508 if (fp->f_type != DTYPE_VNODE) {
4513 if (vp->v_type != VREG) {
4518 /* Allocating blocks may take a long time, so iterate. */
4525 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4528 error = vn_lock(vp, LK_EXCLUSIVE);
4530 vn_finished_write(mp);
4534 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4537 error = VOP_ALLOCATE(vp, &offset, &len);
4539 vn_finished_write(mp);
4541 if (olen + ooffset != offset + len) {
4542 panic("offset + len changed from %jx/%jx to %jx/%jx",
4543 ooffset, olen, offset, len);
4545 if (error != 0 || len == 0)
4547 KASSERT(olen > len, ("Iteration did not make progress?"));
4556 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4560 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
4561 return (kern_posix_error(td, error));
4565 * Unlike madvise(2), we do not make a best effort to remember every
4566 * possible caching hint. Instead, we remember the last setting with
4567 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4568 * region of any current setting.
4571 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4574 struct fadvise_info *fa, *new;
4577 cap_rights_t rights;
4581 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4584 case POSIX_FADV_SEQUENTIAL:
4585 case POSIX_FADV_RANDOM:
4586 case POSIX_FADV_NOREUSE:
4587 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4589 case POSIX_FADV_NORMAL:
4590 case POSIX_FADV_WILLNEED:
4591 case POSIX_FADV_DONTNEED:
4597 /* XXX: CAP_POSIX_FADVISE? */
4598 error = fget(td, fd, cap_rights_init(&rights), &fp);
4601 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4605 if (fp->f_type != DTYPE_VNODE) {
4610 if (vp->v_type != VREG) {
4617 end = offset + len - 1;
4619 case POSIX_FADV_SEQUENTIAL:
4620 case POSIX_FADV_RANDOM:
4621 case POSIX_FADV_NOREUSE:
4623 * Try to merge any existing non-standard region with
4624 * this new region if possible, otherwise create a new
4625 * non-standard region for this request.
4627 mtx_pool_lock(mtxpool_sleep, fp);
4629 if (fa != NULL && fa->fa_advice == advice &&
4630 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4631 (end != OFF_MAX && fa->fa_start == end + 1) ||
4632 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4633 if (offset < fa->fa_start)
4634 fa->fa_start = offset;
4635 if (end > fa->fa_end)
4638 new->fa_advice = advice;
4639 new->fa_start = offset;
4644 mtx_pool_unlock(mtxpool_sleep, fp);
4646 case POSIX_FADV_NORMAL:
4648 * If a the "normal" region overlaps with an existing
4649 * non-standard region, trim or remove the
4650 * non-standard region.
4652 mtx_pool_lock(mtxpool_sleep, fp);
4655 if (offset <= fa->fa_start && end >= fa->fa_end) {
4657 fp->f_advice = NULL;
4658 } else if (offset <= fa->fa_start &&
4659 end >= fa->fa_start)
4660 fa->fa_start = end + 1;
4661 else if (offset <= fa->fa_end && end >= fa->fa_end)
4662 fa->fa_end = offset - 1;
4663 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4665 * If the "normal" region is a middle
4666 * portion of the existing
4667 * non-standard region, just remove
4668 * the whole thing rather than picking
4669 * one side or the other to
4673 fp->f_advice = NULL;
4676 mtx_pool_unlock(mtxpool_sleep, fp);
4678 case POSIX_FADV_WILLNEED:
4679 case POSIX_FADV_DONTNEED:
4680 error = VOP_ADVISE(vp, offset, end, advice);
4686 free(new, M_FADVISE);
4691 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4695 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4697 return (kern_posix_error(td, error));