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_compat.h"
41 #include "opt_kdtrace.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
49 #include <sys/sysent.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
53 #include <sys/sysproto.h>
54 #include <sys/namei.h>
55 #include <sys/filedesc.h>
56 #include <sys/kernel.h>
57 #include <sys/fcntl.h>
59 #include <sys/filio.h>
60 #include <sys/limits.h>
61 #include <sys/linker.h>
65 #include <sys/unistd.h>
66 #include <sys/vnode.h>
69 #include <sys/dirent.h>
71 #include <sys/syscallsubr.h>
72 #include <sys/sysctl.h>
74 #include <sys/ktrace.h>
77 #include <machine/stdarg.h>
79 #include <security/audit/audit.h>
80 #include <security/mac/mac_framework.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
87 SDT_PROVIDER_DEFINE(vfs);
88 SDT_PROBE_DEFINE(vfs, , stat, mode);
89 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 0, "char *");
90 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 1, "int");
91 SDT_PROBE_DEFINE(vfs, , stat, reg);
92 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 0, "char *");
93 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 1, "int");
95 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
96 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
97 static int setfown(struct thread *td, struct vnode *, uid_t, gid_t);
98 static int setfmode(struct thread *td, struct vnode *, int);
99 static int setfflags(struct thread *td, struct vnode *, int);
100 static int setutimes(struct thread *td, struct vnode *,
101 const struct timespec *, int, int);
102 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
106 * The module initialization routine for POSIX asynchronous I/O will
107 * set this to the version of AIO that it implements. (Zero means
108 * that it is not implemented.) This value is used here by pathconf()
109 * and in kern_descrip.c by fpathconf().
111 int async_io_version;
114 static int syncprt = 0;
115 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
119 * Sync each mounted filesystem.
121 #ifndef _SYS_SYSPROTO_H_
130 struct sync_args *uap;
132 struct mount *mp, *nmp;
135 mtx_lock(&mountlist_mtx);
136 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
137 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
138 nmp = TAILQ_NEXT(mp, mnt_list);
141 vfslocked = VFS_LOCK_GIANT(mp);
142 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
143 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
146 mp->mnt_kern_flag &= ~MNTK_ASYNC;
148 vfs_msync(mp, MNT_NOWAIT);
149 VFS_SYNC(mp, MNT_NOWAIT);
152 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
153 mp->mnt_noasync == 0)
154 mp->mnt_kern_flag |= MNTK_ASYNC;
156 vn_finished_write(mp);
158 VFS_UNLOCK_GIANT(vfslocked);
159 mtx_lock(&mountlist_mtx);
160 nmp = TAILQ_NEXT(mp, mnt_list);
163 mtx_unlock(&mountlist_mtx);
168 * Change filesystem quotas.
170 #ifndef _SYS_SYSPROTO_H_
171 struct quotactl_args {
181 register struct quotactl_args /* {
193 AUDIT_ARG_CMD(uap->cmd);
194 AUDIT_ARG_UID(uap->uid);
195 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
197 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
198 UIO_USERSPACE, uap->path, td);
199 if ((error = namei(&nd)) != 0)
201 vfslocked = NDHASGIANT(&nd);
202 NDFREE(&nd, NDF_ONLY_PNBUF);
203 mp = nd.ni_vp->v_mount;
206 error = vfs_busy(mp, 0);
209 VFS_UNLOCK_GIANT(vfslocked);
212 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
214 VFS_UNLOCK_GIANT(vfslocked);
219 * Used by statfs conversion routines to scale the block size up if
220 * necessary so that all of the block counts are <= 'max_size'. Note
221 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
225 statfs_scale_blocks(struct statfs *sf, long max_size)
230 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
233 * Attempt to scale the block counts to give a more accurate
234 * overview to userland of the ratio of free space to used
235 * space. To do this, find the largest block count and compute
236 * a divisor that lets it fit into a signed integer <= max_size.
238 if (sf->f_bavail < 0)
239 count = -sf->f_bavail;
241 count = sf->f_bavail;
242 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
243 if (count <= max_size)
246 count >>= flsl(max_size);
253 sf->f_bsize <<= shift;
254 sf->f_blocks >>= shift;
255 sf->f_bfree >>= shift;
256 sf->f_bavail >>= shift;
260 * Get filesystem statistics.
262 #ifndef _SYS_SYSPROTO_H_
271 register struct statfs_args /* {
279 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
281 error = copyout(&sf, uap->buf, sizeof(sf));
286 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
290 struct statfs *sp, sb;
295 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
296 AUDITVNODE1, pathseg, path, td);
300 vfslocked = NDHASGIANT(&nd);
301 mp = nd.ni_vp->v_mount;
303 NDFREE(&nd, NDF_ONLY_PNBUF);
305 error = vfs_busy(mp, 0);
308 VFS_UNLOCK_GIANT(vfslocked);
312 error = mac_mount_check_stat(td->td_ucred, mp);
317 * Set these in case the underlying filesystem fails to do so.
320 sp->f_version = STATFS_VERSION;
321 sp->f_namemax = NAME_MAX;
322 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
323 error = VFS_STATFS(mp, sp);
326 if (priv_check(td, PRIV_VFS_GENERATION)) {
327 bcopy(sp, &sb, sizeof(sb));
328 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
329 prison_enforce_statfs(td->td_ucred, mp, &sb);
335 VFS_UNLOCK_GIANT(vfslocked);
340 * Get filesystem statistics.
342 #ifndef _SYS_SYSPROTO_H_
343 struct fstatfs_args {
351 register struct fstatfs_args /* {
359 error = kern_fstatfs(td, uap->fd, &sf);
361 error = copyout(&sf, uap->buf, sizeof(sf));
366 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
370 struct statfs *sp, sb;
376 error = getvnode(td->td_proc->p_fd, fd, &fp);
380 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
381 vn_lock(vp, LK_SHARED | LK_RETRY);
383 AUDIT_ARG_VNODE1(vp);
394 error = vfs_busy(mp, 0);
397 VFS_UNLOCK_GIANT(vfslocked);
401 error = mac_mount_check_stat(td->td_ucred, mp);
406 * Set these in case the underlying filesystem fails to do so.
409 sp->f_version = STATFS_VERSION;
410 sp->f_namemax = NAME_MAX;
411 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
412 error = VFS_STATFS(mp, sp);
415 if (priv_check(td, PRIV_VFS_GENERATION)) {
416 bcopy(sp, &sb, sizeof(sb));
417 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
418 prison_enforce_statfs(td->td_ucred, mp, &sb);
425 VFS_UNLOCK_GIANT(vfslocked);
430 * Get statistics on all filesystems.
432 #ifndef _SYS_SYSPROTO_H_
433 struct getfsstat_args {
442 register struct getfsstat_args /* {
449 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
454 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
455 * The caller is responsible for freeing memory which will be allocated
459 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
460 enum uio_seg bufseg, int flags)
462 struct mount *mp, *nmp;
463 struct statfs *sfsp, *sp, sb;
464 size_t count, maxcount;
468 maxcount = bufsize / sizeof(struct statfs);
471 else if (bufseg == UIO_USERSPACE)
473 else /* if (bufseg == UIO_SYSSPACE) */ {
475 mtx_lock(&mountlist_mtx);
476 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
479 mtx_unlock(&mountlist_mtx);
480 if (maxcount > count)
482 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
486 mtx_lock(&mountlist_mtx);
487 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
488 if (prison_canseemount(td->td_ucred, mp) != 0) {
489 nmp = TAILQ_NEXT(mp, mnt_list);
493 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
494 nmp = TAILQ_NEXT(mp, mnt_list);
498 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
499 nmp = TAILQ_NEXT(mp, mnt_list);
502 vfslocked = VFS_LOCK_GIANT(mp);
503 if (sfsp && count < maxcount) {
506 * Set these in case the underlying filesystem
509 sp->f_version = STATFS_VERSION;
510 sp->f_namemax = NAME_MAX;
511 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
513 * If MNT_NOWAIT or MNT_LAZY is specified, do not
514 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
515 * overrides MNT_WAIT.
517 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
518 (flags & MNT_WAIT)) &&
519 (error = VFS_STATFS(mp, sp))) {
520 VFS_UNLOCK_GIANT(vfslocked);
521 mtx_lock(&mountlist_mtx);
522 nmp = TAILQ_NEXT(mp, mnt_list);
526 if (priv_check(td, PRIV_VFS_GENERATION)) {
527 bcopy(sp, &sb, sizeof(sb));
528 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
529 prison_enforce_statfs(td->td_ucred, mp, &sb);
532 if (bufseg == UIO_SYSSPACE)
533 bcopy(sp, sfsp, sizeof(*sp));
534 else /* if (bufseg == UIO_USERSPACE) */ {
535 error = copyout(sp, sfsp, sizeof(*sp));
538 VFS_UNLOCK_GIANT(vfslocked);
544 VFS_UNLOCK_GIANT(vfslocked);
546 mtx_lock(&mountlist_mtx);
547 nmp = TAILQ_NEXT(mp, mnt_list);
550 mtx_unlock(&mountlist_mtx);
551 if (sfsp && count > maxcount)
552 td->td_retval[0] = maxcount;
554 td->td_retval[0] = count;
558 #ifdef COMPAT_FREEBSD4
560 * Get old format filesystem statistics.
562 static void cvtstatfs(struct statfs *, struct ostatfs *);
564 #ifndef _SYS_SYSPROTO_H_
565 struct freebsd4_statfs_args {
571 freebsd4_statfs(td, uap)
573 struct freebsd4_statfs_args /* {
582 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
585 cvtstatfs(&sf, &osb);
586 return (copyout(&osb, uap->buf, sizeof(osb)));
590 * Get filesystem statistics.
592 #ifndef _SYS_SYSPROTO_H_
593 struct freebsd4_fstatfs_args {
599 freebsd4_fstatfs(td, uap)
601 struct freebsd4_fstatfs_args /* {
610 error = kern_fstatfs(td, uap->fd, &sf);
613 cvtstatfs(&sf, &osb);
614 return (copyout(&osb, uap->buf, sizeof(osb)));
618 * Get statistics on all filesystems.
620 #ifndef _SYS_SYSPROTO_H_
621 struct freebsd4_getfsstat_args {
628 freebsd4_getfsstat(td, uap)
630 register struct freebsd4_getfsstat_args /* {
636 struct statfs *buf, *sp;
641 count = uap->bufsize / sizeof(struct ostatfs);
642 size = count * sizeof(struct statfs);
643 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
645 count = td->td_retval[0];
647 while (count > 0 && error == 0) {
649 error = copyout(&osb, uap->buf, sizeof(osb));
660 * Implement fstatfs() for (NFS) file handles.
662 #ifndef _SYS_SYSPROTO_H_
663 struct freebsd4_fhstatfs_args {
664 struct fhandle *u_fhp;
669 freebsd4_fhstatfs(td, uap)
671 struct freebsd4_fhstatfs_args /* {
672 struct fhandle *u_fhp;
681 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
684 error = kern_fhstatfs(td, fh, &sf);
687 cvtstatfs(&sf, &osb);
688 return (copyout(&osb, uap->buf, sizeof(osb)));
692 * Convert a new format statfs structure to an old format statfs structure.
700 statfs_scale_blocks(nsp, LONG_MAX);
701 bzero(osp, sizeof(*osp));
702 osp->f_bsize = nsp->f_bsize;
703 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
704 osp->f_blocks = nsp->f_blocks;
705 osp->f_bfree = nsp->f_bfree;
706 osp->f_bavail = nsp->f_bavail;
707 osp->f_files = MIN(nsp->f_files, LONG_MAX);
708 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
709 osp->f_owner = nsp->f_owner;
710 osp->f_type = nsp->f_type;
711 osp->f_flags = nsp->f_flags;
712 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
713 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
714 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
715 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
716 strlcpy(osp->f_fstypename, nsp->f_fstypename,
717 MIN(MFSNAMELEN, OMFSNAMELEN));
718 strlcpy(osp->f_mntonname, nsp->f_mntonname,
719 MIN(MNAMELEN, OMNAMELEN));
720 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
721 MIN(MNAMELEN, OMNAMELEN));
722 osp->f_fsid = nsp->f_fsid;
724 #endif /* COMPAT_FREEBSD4 */
727 * Change current working directory to a given file descriptor.
729 #ifndef _SYS_SYSPROTO_H_
737 struct fchdir_args /* {
741 register struct filedesc *fdp = td->td_proc->p_fd;
742 struct vnode *vp, *tdp, *vpold;
748 AUDIT_ARG_FD(uap->fd);
749 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
754 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
755 vn_lock(vp, LK_SHARED | LK_RETRY);
756 AUDIT_ARG_VNODE1(vp);
757 error = change_dir(vp, td);
758 while (!error && (mp = vp->v_mountedhere) != NULL) {
762 tvfslocked = VFS_LOCK_GIANT(mp);
763 error = VFS_ROOT(mp, LK_SHARED, &tdp);
766 VFS_UNLOCK_GIANT(tvfslocked);
770 VFS_UNLOCK_GIANT(vfslocked);
772 vfslocked = tvfslocked;
776 VFS_UNLOCK_GIANT(vfslocked);
780 VFS_UNLOCK_GIANT(vfslocked);
782 vpold = fdp->fd_cdir;
784 FILEDESC_XUNLOCK(fdp);
785 vfslocked = VFS_LOCK_GIANT(vpold->v_mount);
787 VFS_UNLOCK_GIANT(vfslocked);
792 * Change current working directory (``.'').
794 #ifndef _SYS_SYSPROTO_H_
802 struct chdir_args /* {
807 return (kern_chdir(td, uap->path, UIO_USERSPACE));
811 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
813 register struct filedesc *fdp = td->td_proc->p_fd;
819 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
820 MPSAFE, pathseg, path, td);
821 if ((error = namei(&nd)) != 0)
823 vfslocked = NDHASGIANT(&nd);
824 if ((error = change_dir(nd.ni_vp, td)) != 0) {
826 VFS_UNLOCK_GIANT(vfslocked);
827 NDFREE(&nd, NDF_ONLY_PNBUF);
830 VOP_UNLOCK(nd.ni_vp, 0);
831 VFS_UNLOCK_GIANT(vfslocked);
832 NDFREE(&nd, NDF_ONLY_PNBUF);
835 fdp->fd_cdir = nd.ni_vp;
836 FILEDESC_XUNLOCK(fdp);
837 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
839 VFS_UNLOCK_GIANT(vfslocked);
844 * Helper function for raised chroot(2) security function: Refuse if
845 * any filedescriptors are open directories.
848 chroot_refuse_vdir_fds(fdp)
849 struct filedesc *fdp;
855 FILEDESC_LOCK_ASSERT(fdp);
857 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
858 fp = fget_locked(fdp, fd);
861 if (fp->f_type == DTYPE_VNODE) {
863 if (vp->v_type == VDIR)
871 * This sysctl determines if we will allow a process to chroot(2) if it
872 * has a directory open:
873 * 0: disallowed for all processes.
874 * 1: allowed for processes that were not already chroot(2)'ed.
875 * 2: allowed for all processes.
878 static int chroot_allow_open_directories = 1;
880 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
881 &chroot_allow_open_directories, 0, "");
884 * Change notion of root (``/'') directory.
886 #ifndef _SYS_SYSPROTO_H_
894 struct chroot_args /* {
902 error = priv_check(td, PRIV_VFS_CHROOT);
905 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
906 AUDITVNODE1, UIO_USERSPACE, uap->path, td);
910 vfslocked = NDHASGIANT(&nd);
911 if ((error = change_dir(nd.ni_vp, td)) != 0)
914 if ((error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp)))
917 VOP_UNLOCK(nd.ni_vp, 0);
918 error = change_root(nd.ni_vp, td);
920 VFS_UNLOCK_GIANT(vfslocked);
921 NDFREE(&nd, NDF_ONLY_PNBUF);
925 VFS_UNLOCK_GIANT(vfslocked);
927 NDFREE(&nd, NDF_ONLY_PNBUF);
932 * Common routine for chroot and chdir. Callers must provide a locked vnode
942 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
943 if (vp->v_type != VDIR)
946 error = mac_vnode_check_chdir(td->td_ucred, vp);
950 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
955 * Common routine for kern_chroot() and jail_attach(). The caller is
956 * responsible for invoking priv_check() and mac_vnode_check_chroot() to
957 * authorize this operation.
964 struct filedesc *fdp;
969 VFS_ASSERT_GIANT(vp->v_mount);
970 fdp = td->td_proc->p_fd;
972 if (chroot_allow_open_directories == 0 ||
973 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
974 error = chroot_refuse_vdir_fds(fdp);
976 FILEDESC_XUNLOCK(fdp);
980 oldvp = fdp->fd_rdir;
987 FILEDESC_XUNLOCK(fdp);
988 vfslocked = VFS_LOCK_GIANT(oldvp->v_mount);
990 VFS_UNLOCK_GIANT(vfslocked);
995 * Check permissions, allocate an open file structure, and call the device
996 * open routine if any.
998 #ifndef _SYS_SYSPROTO_H_
1008 register struct open_args /* {
1015 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1018 #ifndef _SYS_SYSPROTO_H_
1019 struct openat_args {
1027 openat(struct thread *td, struct openat_args *uap)
1030 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1035 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1039 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1043 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1044 int flags, int mode)
1046 struct proc *p = td->td_proc;
1047 struct filedesc *fdp = p->p_fd;
1054 int type, indx, error;
1056 struct nameidata nd;
1059 AUDIT_ARG_FFLAGS(flags);
1060 AUDIT_ARG_MODE(mode);
1061 /* XXX: audit dirfd */
1063 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR may
1066 if (flags & O_EXEC) {
1067 if (flags & O_ACCMODE)
1069 } else if ((flags & O_ACCMODE) == O_ACCMODE)
1072 flags = FFLAGS(flags);
1074 error = falloc(td, &nfp, &indx);
1077 /* An extra reference on `nfp' has been held for us by falloc(). */
1079 /* Set the flags early so the finit in devfs can pick them up. */
1080 fp->f_flag = flags & FMASK;
1081 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1082 NDINIT_AT(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | MPSAFE, pathseg, path, fd,
1084 td->td_dupfd = -1; /* XXX check for fdopen */
1085 error = vn_open(&nd, &flags, cmode, fp);
1088 * If the vn_open replaced the method vector, something
1089 * wonderous happened deep below and we just pass it up
1090 * pretending we know what we do.
1092 if (error == ENXIO && fp->f_ops != &badfileops) {
1094 td->td_retval[0] = indx;
1099 * handle special fdopen() case. bleh. dupfdopen() is
1100 * responsible for dropping the old contents of ofiles[indx]
1103 if ((error == ENODEV || error == ENXIO) &&
1104 td->td_dupfd >= 0 && /* XXX from fdopen */
1106 dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
1107 td->td_retval[0] = indx;
1112 * Clean up the descriptor, but only if another thread hadn't
1113 * replaced or closed it.
1115 fdclose(fdp, fp, indx, td);
1118 if (error == ERESTART)
1123 vfslocked = NDHASGIANT(&nd);
1124 NDFREE(&nd, NDF_ONLY_PNBUF);
1127 fp->f_vnode = vp; /* XXX Does devfs need this? */
1129 * If the file wasn't claimed by devfs bind it to the normal
1130 * vnode operations here.
1132 if (fp->f_ops == &badfileops) {
1133 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1135 finit(fp, flags & FMASK, DTYPE_VNODE, vp, &vnops);
1139 if (flags & (O_EXLOCK | O_SHLOCK)) {
1140 lf.l_whence = SEEK_SET;
1143 if (flags & O_EXLOCK)
1144 lf.l_type = F_WRLCK;
1146 lf.l_type = F_RDLCK;
1148 if ((flags & FNONBLOCK) == 0)
1150 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1153 atomic_set_int(&fp->f_flag, FHASLOCK);
1155 if (flags & O_TRUNC) {
1156 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
1160 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1162 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
1165 error = VOP_SETATTR(vp, &vat, td->td_ucred);
1167 vn_finished_write(mp);
1171 VFS_UNLOCK_GIANT(vfslocked);
1173 * Release our private reference, leaving the one associated with
1174 * the descriptor table intact.
1177 td->td_retval[0] = indx;
1180 VFS_UNLOCK_GIANT(vfslocked);
1181 fdclose(fdp, fp, indx, td);
1190 #ifndef _SYS_SYSPROTO_H_
1191 struct ocreat_args {
1199 register struct ocreat_args /* {
1205 return (kern_open(td, uap->path, UIO_USERSPACE,
1206 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1208 #endif /* COMPAT_43 */
1211 * Create a special file.
1213 #ifndef _SYS_SYSPROTO_H_
1223 register struct mknod_args /* {
1230 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1233 #ifndef _SYS_SYSPROTO_H_
1234 struct mknodat_args {
1242 mknodat(struct thread *td, struct mknodat_args *uap)
1245 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1250 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1254 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1258 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1266 struct nameidata nd;
1269 AUDIT_ARG_MODE(mode);
1271 switch (mode & S_IFMT) {
1274 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1277 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1280 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1284 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1294 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1295 pathseg, path, fd, td);
1296 if ((error = namei(&nd)) != 0)
1298 vfslocked = NDHASGIANT(&nd);
1301 NDFREE(&nd, NDF_ONLY_PNBUF);
1302 if (vp == nd.ni_dvp)
1307 VFS_UNLOCK_GIANT(vfslocked);
1311 vattr.va_mode = (mode & ALLPERMS) &
1312 ~td->td_proc->p_fd->fd_cmask;
1313 vattr.va_rdev = dev;
1316 switch (mode & S_IFMT) {
1317 case S_IFMT: /* used by badsect to flag bad sectors */
1318 vattr.va_type = VBAD;
1321 vattr.va_type = VCHR;
1324 vattr.va_type = VBLK;
1330 panic("kern_mknod: invalid mode");
1333 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1334 NDFREE(&nd, NDF_ONLY_PNBUF);
1336 VFS_UNLOCK_GIANT(vfslocked);
1337 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1342 if (error == 0 && !whiteout)
1343 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1344 &nd.ni_cnd, &vattr);
1348 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1350 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1351 &nd.ni_cnd, &vattr);
1356 NDFREE(&nd, NDF_ONLY_PNBUF);
1358 vn_finished_write(mp);
1359 VFS_UNLOCK_GIANT(vfslocked);
1364 * Create a named pipe.
1366 #ifndef _SYS_SYSPROTO_H_
1367 struct mkfifo_args {
1375 register struct mkfifo_args /* {
1381 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1384 #ifndef _SYS_SYSPROTO_H_
1385 struct mkfifoat_args {
1392 mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1395 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1400 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1403 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1407 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1413 struct nameidata nd;
1416 AUDIT_ARG_MODE(mode);
1419 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1420 pathseg, path, fd, td);
1421 if ((error = namei(&nd)) != 0)
1423 vfslocked = NDHASGIANT(&nd);
1424 if (nd.ni_vp != NULL) {
1425 NDFREE(&nd, NDF_ONLY_PNBUF);
1426 if (nd.ni_vp == nd.ni_dvp)
1431 VFS_UNLOCK_GIANT(vfslocked);
1434 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1435 NDFREE(&nd, NDF_ONLY_PNBUF);
1437 VFS_UNLOCK_GIANT(vfslocked);
1438 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1443 vattr.va_type = VFIFO;
1444 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1446 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1451 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1458 vn_finished_write(mp);
1459 VFS_UNLOCK_GIANT(vfslocked);
1460 NDFREE(&nd, NDF_ONLY_PNBUF);
1465 * Make a hard file link.
1467 #ifndef _SYS_SYSPROTO_H_
1476 register struct link_args /* {
1482 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1485 #ifndef _SYS_SYSPROTO_H_
1486 struct linkat_args {
1495 linkat(struct thread *td, struct linkat_args *uap)
1500 if (flag & ~AT_SYMLINK_FOLLOW)
1503 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1504 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1507 int hardlink_check_uid = 0;
1508 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1509 &hardlink_check_uid, 0,
1510 "Unprivileged processes cannot create hard links to files owned by other "
1512 static int hardlink_check_gid = 0;
1513 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1514 &hardlink_check_gid, 0,
1515 "Unprivileged processes cannot create hard links to files owned by other "
1519 can_hardlink(struct vnode *vp, struct ucred *cred)
1524 if (!hardlink_check_uid && !hardlink_check_gid)
1527 error = VOP_GETATTR(vp, &va, cred);
1531 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1532 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1537 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1538 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1547 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1550 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1554 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1555 enum uio_seg segflg, int follow)
1559 struct nameidata nd;
1565 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, segflg, path1,
1568 if ((error = namei(&nd)) != 0)
1570 vfslocked = NDHASGIANT(&nd);
1571 NDFREE(&nd, NDF_ONLY_PNBUF);
1573 if (vp->v_type == VDIR) {
1575 VFS_UNLOCK_GIANT(vfslocked);
1576 return (EPERM); /* POSIX */
1578 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1580 VFS_UNLOCK_GIANT(vfslocked);
1583 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1584 segflg, path2, fd2, td);
1585 if ((error = namei(&nd)) == 0) {
1586 lvfslocked = NDHASGIANT(&nd);
1587 if (nd.ni_vp != NULL) {
1588 if (nd.ni_dvp == nd.ni_vp)
1594 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1596 error = can_hardlink(vp, td->td_ucred);
1599 error = mac_vnode_check_link(td->td_ucred,
1600 nd.ni_dvp, vp, &nd.ni_cnd);
1603 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1607 NDFREE(&nd, NDF_ONLY_PNBUF);
1608 VFS_UNLOCK_GIANT(lvfslocked);
1611 vn_finished_write(mp);
1612 VFS_UNLOCK_GIANT(vfslocked);
1617 * Make a symbolic link.
1619 #ifndef _SYS_SYSPROTO_H_
1620 struct symlink_args {
1628 register struct symlink_args /* {
1634 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1637 #ifndef _SYS_SYSPROTO_H_
1638 struct symlinkat_args {
1645 symlinkat(struct thread *td, struct symlinkat_args *uap)
1648 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1653 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1656 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1660 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1661 enum uio_seg segflg)
1667 struct nameidata nd;
1670 if (segflg == UIO_SYSSPACE) {
1673 syspath = uma_zalloc(namei_zone, M_WAITOK);
1674 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1677 AUDIT_ARG_TEXT(syspath);
1680 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1681 segflg, path2, fd, td);
1682 if ((error = namei(&nd)) != 0)
1684 vfslocked = NDHASGIANT(&nd);
1686 NDFREE(&nd, NDF_ONLY_PNBUF);
1687 if (nd.ni_vp == nd.ni_dvp)
1692 VFS_UNLOCK_GIANT(vfslocked);
1696 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1697 NDFREE(&nd, NDF_ONLY_PNBUF);
1699 VFS_UNLOCK_GIANT(vfslocked);
1700 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1705 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1707 vattr.va_type = VLNK;
1708 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1713 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1719 NDFREE(&nd, NDF_ONLY_PNBUF);
1721 vn_finished_write(mp);
1722 VFS_UNLOCK_GIANT(vfslocked);
1724 if (segflg != UIO_SYSSPACE)
1725 uma_zfree(namei_zone, syspath);
1730 * Delete a whiteout from the filesystem.
1735 register struct undelete_args /* {
1741 struct nameidata nd;
1746 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1747 UIO_USERSPACE, uap->path, td);
1751 vfslocked = NDHASGIANT(&nd);
1753 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1754 NDFREE(&nd, NDF_ONLY_PNBUF);
1755 if (nd.ni_vp == nd.ni_dvp)
1761 VFS_UNLOCK_GIANT(vfslocked);
1764 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1765 NDFREE(&nd, NDF_ONLY_PNBUF);
1767 VFS_UNLOCK_GIANT(vfslocked);
1768 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1772 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1773 NDFREE(&nd, NDF_ONLY_PNBUF);
1775 vn_finished_write(mp);
1776 VFS_UNLOCK_GIANT(vfslocked);
1781 * Delete a name from the filesystem.
1783 #ifndef _SYS_SYSPROTO_H_
1784 struct unlink_args {
1791 struct unlink_args /* {
1796 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1799 #ifndef _SYS_SYSPROTO_H_
1800 struct unlinkat_args {
1807 unlinkat(struct thread *td, struct unlinkat_args *uap)
1809 int flag = uap->flag;
1811 char *path = uap->path;
1813 if (flag & ~AT_REMOVEDIR)
1816 if (flag & AT_REMOVEDIR)
1817 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1819 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1823 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1826 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1830 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1836 struct nameidata nd;
1842 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1843 pathseg, path, fd, td);
1844 if ((error = namei(&nd)) != 0)
1845 return (error == EINVAL ? EPERM : error);
1846 vfslocked = NDHASGIANT(&nd);
1848 if (vp->v_type == VDIR && oldinum == 0) {
1849 error = EPERM; /* POSIX */
1850 } else if (oldinum != 0 &&
1851 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1852 sb.st_ino != oldinum) {
1853 error = EIDRM; /* Identifier removed */
1856 * The root of a mounted filesystem cannot be deleted.
1858 * XXX: can this only be a VDIR case?
1860 if (vp->v_vflag & VV_ROOT)
1864 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1865 NDFREE(&nd, NDF_ONLY_PNBUF);
1867 if (vp == nd.ni_dvp)
1871 VFS_UNLOCK_GIANT(vfslocked);
1872 if ((error = vn_start_write(NULL, &mp,
1873 V_XSLEEP | PCATCH)) != 0)
1878 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1883 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1887 vn_finished_write(mp);
1889 NDFREE(&nd, NDF_ONLY_PNBUF);
1891 if (vp == nd.ni_dvp)
1895 VFS_UNLOCK_GIANT(vfslocked);
1900 * Reposition read/write file offset.
1902 #ifndef _SYS_SYSPROTO_H_
1913 register struct lseek_args /* {
1920 struct ucred *cred = td->td_ucred;
1928 AUDIT_ARG_FD(uap->fd);
1929 if ((error = fget(td, uap->fd, &fp)) != 0)
1931 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1936 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1937 noneg = (vp->v_type != VCHR);
1938 offset = uap->offset;
1939 switch (uap->whence) {
1942 (fp->f_offset < 0 ||
1943 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
1947 offset += fp->f_offset;
1950 vn_lock(vp, LK_SHARED | LK_RETRY);
1951 error = VOP_GETATTR(vp, &vattr, cred);
1957 * If the file references a disk device, then fetch
1958 * the media size and use that to determine the ending
1961 if (vattr.va_size == 0 && vp->v_type == VCHR &&
1962 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
1963 vattr.va_size = size;
1965 (vattr.va_size > OFF_MAX ||
1966 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1970 offset += vattr.va_size;
1975 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
1978 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
1983 if (error == 0 && noneg && offset < 0)
1987 fp->f_offset = offset;
1988 *(off_t *)(td->td_retval) = fp->f_offset;
1991 VFS_UNLOCK_GIANT(vfslocked);
1995 #if defined(COMPAT_43)
1997 * Reposition read/write file offset.
1999 #ifndef _SYS_SYSPROTO_H_
2000 struct olseek_args {
2009 register struct olseek_args /* {
2015 struct lseek_args /* {
2023 nuap.offset = uap->offset;
2024 nuap.whence = uap->whence;
2025 return (lseek(td, &nuap));
2027 #endif /* COMPAT_43 */
2029 /* Version with the 'pad' argument */
2031 freebsd6_lseek(td, uap)
2033 register struct freebsd6_lseek_args *uap;
2035 struct lseek_args ouap;
2038 ouap.offset = uap->offset;
2039 ouap.whence = uap->whence;
2040 return (lseek(td, &ouap));
2044 * Check access permissions using passed credentials.
2047 vn_access(vp, user_flags, cred, td)
2056 /* Flags == 0 means only check for existence. */
2060 if (user_flags & R_OK)
2062 if (user_flags & W_OK)
2064 if (user_flags & X_OK)
2067 error = mac_vnode_check_access(cred, vp, accmode);
2071 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2072 error = VOP_ACCESS(vp, accmode, cred, td);
2078 * Check access permissions using "real" credentials.
2080 #ifndef _SYS_SYSPROTO_H_
2081 struct access_args {
2089 register struct access_args /* {
2095 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
2098 #ifndef _SYS_SYSPROTO_H_
2099 struct faccessat_args {
2107 faccessat(struct thread *td, struct faccessat_args *uap)
2110 if (uap->flag & ~AT_EACCESS)
2112 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2117 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2120 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, mode));
2124 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2125 int flags, int mode)
2127 struct ucred *cred, *tmpcred;
2129 struct nameidata nd;
2134 * Create and modify a temporary credential instead of one that
2135 * is potentially shared.
2137 if (!(flags & AT_EACCESS)) {
2138 cred = td->td_ucred;
2139 tmpcred = crdup(cred);
2140 tmpcred->cr_uid = cred->cr_ruid;
2141 tmpcred->cr_groups[0] = cred->cr_rgid;
2142 td->td_ucred = tmpcred;
2144 cred = tmpcred = td->td_ucred;
2145 AUDIT_ARG_VALUE(mode);
2146 NDINIT_AT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2147 AUDITVNODE1, pathseg, path, fd, td);
2148 if ((error = namei(&nd)) != 0)
2150 vfslocked = NDHASGIANT(&nd);
2153 error = vn_access(vp, mode, tmpcred, td);
2154 NDFREE(&nd, NDF_ONLY_PNBUF);
2156 VFS_UNLOCK_GIANT(vfslocked);
2158 if (!(flags & AT_EACCESS)) {
2159 td->td_ucred = cred;
2166 * Check access permissions using "effective" credentials.
2168 #ifndef _SYS_SYSPROTO_H_
2169 struct eaccess_args {
2177 register struct eaccess_args /* {
2183 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
2187 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
2190 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, flags));
2193 #if defined(COMPAT_43)
2195 * Get file status; this version follows links.
2197 #ifndef _SYS_SYSPROTO_H_
2206 register struct ostat_args /* {
2215 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2219 error = copyout(&osb, uap->ub, sizeof (osb));
2224 * Get file status; this version does not follow links.
2226 #ifndef _SYS_SYSPROTO_H_
2227 struct olstat_args {
2235 register struct olstat_args /* {
2244 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2248 error = copyout(&osb, uap->ub, sizeof (osb));
2253 * Convert from an old to a new stat structure.
2261 ost->st_dev = st->st_dev;
2262 ost->st_ino = st->st_ino;
2263 ost->st_mode = st->st_mode;
2264 ost->st_nlink = st->st_nlink;
2265 ost->st_uid = st->st_uid;
2266 ost->st_gid = st->st_gid;
2267 ost->st_rdev = st->st_rdev;
2268 if (st->st_size < (quad_t)1 << 32)
2269 ost->st_size = st->st_size;
2272 ost->st_atime = st->st_atime;
2273 ost->st_mtime = st->st_mtime;
2274 ost->st_ctime = st->st_ctime;
2275 ost->st_blksize = st->st_blksize;
2276 ost->st_blocks = st->st_blocks;
2277 ost->st_flags = st->st_flags;
2278 ost->st_gen = st->st_gen;
2280 #endif /* COMPAT_43 */
2283 * Get file status; this version follows links.
2285 #ifndef _SYS_SYSPROTO_H_
2294 register struct stat_args /* {
2302 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2304 error = copyout(&sb, uap->ub, sizeof (sb));
2308 #ifndef _SYS_SYSPROTO_H_
2309 struct fstatat_args {
2317 fstatat(struct thread *td, struct fstatat_args *uap)
2322 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2323 UIO_USERSPACE, &sb);
2325 error = copyout(&sb, uap->buf, sizeof (sb));
2330 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2333 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2337 kern_statat(struct thread *td, int flag, int fd, char *path,
2338 enum uio_seg pathseg, struct stat *sbp)
2341 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2345 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2346 enum uio_seg pathseg, struct stat *sbp,
2347 void (*hook)(struct vnode *vp, struct stat *sbp))
2349 struct nameidata nd;
2351 int error, vfslocked;
2353 if (flag & ~AT_SYMLINK_NOFOLLOW)
2356 NDINIT_AT(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2357 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | MPSAFE, pathseg,
2360 if ((error = namei(&nd)) != 0)
2362 vfslocked = NDHASGIANT(&nd);
2363 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2365 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2366 if (S_ISREG(sb.st_mode))
2367 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2368 if (__predict_false(hook != NULL))
2369 hook(nd.ni_vp, &sb);
2371 NDFREE(&nd, NDF_ONLY_PNBUF);
2373 VFS_UNLOCK_GIANT(vfslocked);
2378 if (KTRPOINT(td, KTR_STRUCT))
2385 * Get file status; this version does not follow links.
2387 #ifndef _SYS_SYSPROTO_H_
2396 register struct lstat_args /* {
2404 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2406 error = copyout(&sb, uap->ub, sizeof (sb));
2411 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2414 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2419 * Implementation of the NetBSD [l]stat() functions.
2426 bzero(nsb, sizeof *nsb);
2427 nsb->st_dev = sb->st_dev;
2428 nsb->st_ino = sb->st_ino;
2429 nsb->st_mode = sb->st_mode;
2430 nsb->st_nlink = sb->st_nlink;
2431 nsb->st_uid = sb->st_uid;
2432 nsb->st_gid = sb->st_gid;
2433 nsb->st_rdev = sb->st_rdev;
2434 nsb->st_atimespec = sb->st_atimespec;
2435 nsb->st_mtimespec = sb->st_mtimespec;
2436 nsb->st_ctimespec = sb->st_ctimespec;
2437 nsb->st_size = sb->st_size;
2438 nsb->st_blocks = sb->st_blocks;
2439 nsb->st_blksize = sb->st_blksize;
2440 nsb->st_flags = sb->st_flags;
2441 nsb->st_gen = sb->st_gen;
2442 nsb->st_birthtimespec = sb->st_birthtimespec;
2445 #ifndef _SYS_SYSPROTO_H_
2454 register struct nstat_args /* {
2463 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2466 cvtnstat(&sb, &nsb);
2467 error = copyout(&nsb, uap->ub, sizeof (nsb));
2472 * NetBSD lstat. Get file status; this version does not follow links.
2474 #ifndef _SYS_SYSPROTO_H_
2483 register struct nlstat_args /* {
2492 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2495 cvtnstat(&sb, &nsb);
2496 error = copyout(&nsb, uap->ub, sizeof (nsb));
2501 * Get configurable pathname variables.
2503 #ifndef _SYS_SYSPROTO_H_
2504 struct pathconf_args {
2512 register struct pathconf_args /* {
2518 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2521 #ifndef _SYS_SYSPROTO_H_
2522 struct lpathconf_args {
2530 register struct lpathconf_args /* {
2536 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, NOFOLLOW));
2540 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2543 struct nameidata nd;
2544 int error, vfslocked;
2546 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1 |
2547 flags, pathseg, path, td);
2548 if ((error = namei(&nd)) != 0)
2550 vfslocked = NDHASGIANT(&nd);
2551 NDFREE(&nd, NDF_ONLY_PNBUF);
2553 /* If asynchronous I/O is available, it works for all files. */
2554 if (name == _PC_ASYNC_IO)
2555 td->td_retval[0] = async_io_version;
2557 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2559 VFS_UNLOCK_GIANT(vfslocked);
2564 * Return target name of a symbolic link.
2566 #ifndef _SYS_SYSPROTO_H_
2567 struct readlink_args {
2576 register struct readlink_args /* {
2583 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2584 UIO_USERSPACE, uap->count));
2586 #ifndef _SYS_SYSPROTO_H_
2587 struct readlinkat_args {
2595 readlinkat(struct thread *td, struct readlinkat_args *uap)
2598 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2599 uap->buf, UIO_USERSPACE, uap->bufsize));
2603 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2604 enum uio_seg bufseg, size_t count)
2607 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2612 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2613 char *buf, enum uio_seg bufseg, size_t count)
2619 struct nameidata nd;
2622 if (count > INT_MAX)
2625 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2626 AUDITVNODE1, pathseg, path, fd, td);
2628 if ((error = namei(&nd)) != 0)
2630 NDFREE(&nd, NDF_ONLY_PNBUF);
2631 vfslocked = NDHASGIANT(&nd);
2634 error = mac_vnode_check_readlink(td->td_ucred, vp);
2637 VFS_UNLOCK_GIANT(vfslocked);
2641 if (vp->v_type != VLNK)
2644 aiov.iov_base = buf;
2645 aiov.iov_len = count;
2646 auio.uio_iov = &aiov;
2647 auio.uio_iovcnt = 1;
2648 auio.uio_offset = 0;
2649 auio.uio_rw = UIO_READ;
2650 auio.uio_segflg = bufseg;
2652 auio.uio_resid = count;
2653 error = VOP_READLINK(vp, &auio, td->td_ucred);
2656 VFS_UNLOCK_GIANT(vfslocked);
2657 td->td_retval[0] = count - auio.uio_resid;
2662 * Common implementation code for chflags() and fchflags().
2665 setfflags(td, vp, flags)
2675 * Prevent non-root users from setting flags on devices. When
2676 * a device is reused, users can retain ownership of the device
2677 * if they are allowed to set flags and programs assume that
2678 * chown can't fail when done as root.
2680 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2681 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2686 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2688 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2690 vattr.va_flags = flags;
2692 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2695 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2697 vn_finished_write(mp);
2702 * Change flags of a file given a path name.
2704 #ifndef _SYS_SYSPROTO_H_
2705 struct chflags_args {
2713 register struct chflags_args /* {
2719 struct nameidata nd;
2722 AUDIT_ARG_FFLAGS(uap->flags);
2723 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2725 if ((error = namei(&nd)) != 0)
2727 NDFREE(&nd, NDF_ONLY_PNBUF);
2728 vfslocked = NDHASGIANT(&nd);
2729 error = setfflags(td, nd.ni_vp, uap->flags);
2731 VFS_UNLOCK_GIANT(vfslocked);
2736 * Same as chflags() but doesn't follow symlinks.
2741 register struct lchflags_args /* {
2747 struct nameidata nd;
2750 AUDIT_ARG_FFLAGS(uap->flags);
2751 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2753 if ((error = namei(&nd)) != 0)
2755 vfslocked = NDHASGIANT(&nd);
2756 NDFREE(&nd, NDF_ONLY_PNBUF);
2757 error = setfflags(td, nd.ni_vp, uap->flags);
2759 VFS_UNLOCK_GIANT(vfslocked);
2764 * Change flags of a file given a file descriptor.
2766 #ifndef _SYS_SYSPROTO_H_
2767 struct fchflags_args {
2775 register struct fchflags_args /* {
2784 AUDIT_ARG_FD(uap->fd);
2785 AUDIT_ARG_FFLAGS(uap->flags);
2786 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2788 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2790 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2791 AUDIT_ARG_VNODE1(fp->f_vnode);
2792 VOP_UNLOCK(fp->f_vnode, 0);
2794 error = setfflags(td, fp->f_vnode, uap->flags);
2795 VFS_UNLOCK_GIANT(vfslocked);
2801 * Common implementation code for chmod(), lchmod() and fchmod().
2804 setfmode(td, vp, mode)
2813 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2815 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2817 vattr.va_mode = mode & ALLPERMS;
2819 error = mac_vnode_check_setmode(td->td_ucred, vp, vattr.va_mode);
2822 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2824 vn_finished_write(mp);
2829 * Change mode of a file given path name.
2831 #ifndef _SYS_SYSPROTO_H_
2840 register struct chmod_args /* {
2846 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2849 #ifndef _SYS_SYSPROTO_H_
2850 struct fchmodat_args {
2858 fchmodat(struct thread *td, struct fchmodat_args *uap)
2860 int flag = uap->flag;
2862 char *path = uap->path;
2863 mode_t mode = uap->mode;
2865 if (flag & ~AT_SYMLINK_NOFOLLOW)
2868 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2872 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2875 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2879 * Change mode of a file given path name (don't follow links.)
2881 #ifndef _SYS_SYSPROTO_H_
2882 struct lchmod_args {
2890 register struct lchmod_args /* {
2896 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2897 uap->mode, AT_SYMLINK_NOFOLLOW));
2902 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2903 mode_t mode, int flag)
2906 struct nameidata nd;
2910 AUDIT_ARG_MODE(mode);
2911 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2912 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg, path,
2914 if ((error = namei(&nd)) != 0)
2916 vfslocked = NDHASGIANT(&nd);
2917 NDFREE(&nd, NDF_ONLY_PNBUF);
2918 error = setfmode(td, nd.ni_vp, mode);
2920 VFS_UNLOCK_GIANT(vfslocked);
2925 * Change mode of a file given a file descriptor.
2927 #ifndef _SYS_SYSPROTO_H_
2928 struct fchmod_args {
2936 register struct fchmod_args /* {
2945 AUDIT_ARG_FD(uap->fd);
2946 AUDIT_ARG_MODE(uap->mode);
2947 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2949 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2951 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2952 AUDIT_ARG_VNODE1(fp->f_vnode);
2953 VOP_UNLOCK(fp->f_vnode, 0);
2955 error = setfmode(td, fp->f_vnode, uap->mode);
2956 VFS_UNLOCK_GIANT(vfslocked);
2962 * Common implementation for chown(), lchown(), and fchown()
2965 setfown(td, vp, uid, gid)
2975 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2977 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2982 error = mac_vnode_check_setowner(td->td_ucred, vp, vattr.va_uid,
2986 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2988 vn_finished_write(mp);
2993 * Set ownership given a path name.
2995 #ifndef _SYS_SYSPROTO_H_
3005 register struct chown_args /* {
3012 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3015 #ifndef _SYS_SYSPROTO_H_
3016 struct fchownat_args {
3025 fchownat(struct thread *td, struct fchownat_args *uap)
3030 if (flag & ~AT_SYMLINK_NOFOLLOW)
3033 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3034 uap->gid, uap->flag));
3038 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3042 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3046 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3047 int uid, int gid, int flag)
3049 struct nameidata nd;
3050 int error, vfslocked, follow;
3052 AUDIT_ARG_OWNER(uid, gid);
3053 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3054 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg, path,
3057 if ((error = namei(&nd)) != 0)
3059 vfslocked = NDHASGIANT(&nd);
3060 NDFREE(&nd, NDF_ONLY_PNBUF);
3061 error = setfown(td, nd.ni_vp, uid, gid);
3063 VFS_UNLOCK_GIANT(vfslocked);
3068 * Set ownership given a path name, do not cross symlinks.
3070 #ifndef _SYS_SYSPROTO_H_
3071 struct lchown_args {
3080 register struct lchown_args /* {
3087 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3091 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3095 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3096 AT_SYMLINK_NOFOLLOW));
3100 * Set ownership given a file descriptor.
3102 #ifndef _SYS_SYSPROTO_H_
3103 struct fchown_args {
3112 register struct fchown_args /* {
3122 AUDIT_ARG_FD(uap->fd);
3123 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3124 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3126 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3128 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3129 AUDIT_ARG_VNODE1(fp->f_vnode);
3130 VOP_UNLOCK(fp->f_vnode, 0);
3132 error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
3133 VFS_UNLOCK_GIANT(vfslocked);
3139 * Common implementation code for utimes(), lutimes(), and futimes().
3142 getutimes(usrtvp, tvpseg, tsp)
3143 const struct timeval *usrtvp;
3144 enum uio_seg tvpseg;
3145 struct timespec *tsp;
3147 struct timeval tv[2];
3148 const struct timeval *tvp;
3151 if (usrtvp == NULL) {
3152 vfs_timestamp(&tsp[0]);
3155 if (tvpseg == UIO_SYSSPACE) {
3158 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3163 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3164 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3166 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3167 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3173 * Common implementation code for utimes(), lutimes(), and futimes().
3176 setutimes(td, vp, ts, numtimes, nullflag)
3179 const struct timespec *ts;
3183 int error, setbirthtime;
3187 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3189 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3191 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3192 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3195 vattr.va_atime = ts[0];
3196 vattr.va_mtime = ts[1];
3198 vattr.va_birthtime = ts[1];
3200 vattr.va_birthtime = ts[2];
3202 vattr.va_vaflags |= VA_UTIMES_NULL;
3204 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3208 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3210 vn_finished_write(mp);
3215 * Set the access and modification times of a file.
3217 #ifndef _SYS_SYSPROTO_H_
3218 struct utimes_args {
3220 struct timeval *tptr;
3226 register struct utimes_args /* {
3228 struct timeval *tptr;
3232 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3236 #ifndef _SYS_SYSPROTO_H_
3237 struct futimesat_args {
3240 const struct timeval * times;
3244 futimesat(struct thread *td, struct futimesat_args *uap)
3247 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3248 uap->times, UIO_USERSPACE));
3252 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3253 struct timeval *tptr, enum uio_seg tptrseg)
3256 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3260 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3261 struct timeval *tptr, enum uio_seg tptrseg)
3263 struct nameidata nd;
3264 struct timespec ts[2];
3265 int error, vfslocked;
3267 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3269 NDINIT_AT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path,
3272 if ((error = namei(&nd)) != 0)
3274 vfslocked = NDHASGIANT(&nd);
3275 NDFREE(&nd, NDF_ONLY_PNBUF);
3276 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3278 VFS_UNLOCK_GIANT(vfslocked);
3283 * Set the access and modification times of a file.
3285 #ifndef _SYS_SYSPROTO_H_
3286 struct lutimes_args {
3288 struct timeval *tptr;
3294 register struct lutimes_args /* {
3296 struct timeval *tptr;
3300 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3305 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3306 struct timeval *tptr, enum uio_seg tptrseg)
3308 struct timespec ts[2];
3310 struct nameidata nd;
3313 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3315 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3316 if ((error = namei(&nd)) != 0)
3318 vfslocked = NDHASGIANT(&nd);
3319 NDFREE(&nd, NDF_ONLY_PNBUF);
3320 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3322 VFS_UNLOCK_GIANT(vfslocked);
3327 * Set the access and modification times of a file.
3329 #ifndef _SYS_SYSPROTO_H_
3330 struct futimes_args {
3332 struct timeval *tptr;
3338 register struct futimes_args /* {
3340 struct timeval *tptr;
3344 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3348 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3349 enum uio_seg tptrseg)
3351 struct timespec ts[2];
3357 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3359 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
3361 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3363 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3364 AUDIT_ARG_VNODE1(fp->f_vnode);
3365 VOP_UNLOCK(fp->f_vnode, 0);
3367 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3368 VFS_UNLOCK_GIANT(vfslocked);
3374 * Truncate a file given its path name.
3376 #ifndef _SYS_SYSPROTO_H_
3377 struct truncate_args {
3386 register struct truncate_args /* {
3393 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3397 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3403 struct nameidata nd;
3408 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3409 if ((error = namei(&nd)) != 0)
3411 vfslocked = NDHASGIANT(&nd);
3413 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3415 VFS_UNLOCK_GIANT(vfslocked);
3418 NDFREE(&nd, NDF_ONLY_PNBUF);
3419 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3420 if (vp->v_type == VDIR)
3423 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3426 else if ((error = vn_writechk(vp)) == 0 &&
3427 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3429 vattr.va_size = length;
3430 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3433 vn_finished_write(mp);
3434 VFS_UNLOCK_GIANT(vfslocked);
3438 #if defined(COMPAT_43)
3440 * Truncate a file given its path name.
3442 #ifndef _SYS_SYSPROTO_H_
3443 struct otruncate_args {
3451 register struct otruncate_args /* {
3456 struct truncate_args /* {
3462 nuap.path = uap->path;
3463 nuap.length = uap->length;
3464 return (truncate(td, &nuap));
3466 #endif /* COMPAT_43 */
3468 /* Versions with the pad argument */
3470 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3472 struct truncate_args ouap;
3474 ouap.path = uap->path;
3475 ouap.length = uap->length;
3476 return (truncate(td, &ouap));
3480 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3482 struct ftruncate_args ouap;
3485 ouap.length = uap->length;
3486 return (ftruncate(td, &ouap));
3490 * Sync an open file.
3492 #ifndef _SYS_SYSPROTO_H_
3500 struct fsync_args /* {
3508 int error, lock_flags;
3510 AUDIT_ARG_FD(uap->fd);
3511 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3514 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3515 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3517 if (MNT_SHARED_WRITES(mp) ||
3518 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3519 lock_flags = LK_SHARED;
3521 lock_flags = LK_EXCLUSIVE;
3523 vn_lock(vp, lock_flags | LK_RETRY);
3524 AUDIT_ARG_VNODE1(vp);
3525 if (vp->v_object != NULL) {
3526 VM_OBJECT_LOCK(vp->v_object);
3527 vm_object_page_clean(vp->v_object, 0, 0, 0);
3528 VM_OBJECT_UNLOCK(vp->v_object);
3530 error = VOP_FSYNC(vp, MNT_WAIT, td);
3533 vn_finished_write(mp);
3535 VFS_UNLOCK_GIANT(vfslocked);
3541 * Rename files. Source and destination must either both be directories, or
3542 * both not be directories. If target is a directory, it must be empty.
3544 #ifndef _SYS_SYSPROTO_H_
3545 struct rename_args {
3553 register struct rename_args /* {
3559 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3562 #ifndef _SYS_SYSPROTO_H_
3563 struct renameat_args {
3571 renameat(struct thread *td, struct renameat_args *uap)
3574 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3579 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3582 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3586 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3587 enum uio_seg pathseg)
3589 struct mount *mp = NULL;
3590 struct vnode *tvp, *fvp, *tdvp;
3591 struct nameidata fromnd, tond;
3598 NDINIT_AT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE |
3599 AUDITVNODE1, pathseg, old, oldfd, td);
3601 NDINIT_AT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3602 AUDITVNODE1, pathseg, old, oldfd, td);
3605 if ((error = namei(&fromnd)) != 0)
3607 fvfslocked = NDHASGIANT(&fromnd);
3610 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3611 fromnd.ni_vp, &fromnd.ni_cnd);
3612 VOP_UNLOCK(fromnd.ni_dvp, 0);
3613 if (fromnd.ni_dvp != fromnd.ni_vp)
3614 VOP_UNLOCK(fromnd.ni_vp, 0);
3618 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3620 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3621 vrele(fromnd.ni_dvp);
3625 NDINIT_AT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3626 MPSAFE | AUDITVNODE2, pathseg, new, newfd, td);
3627 if (fromnd.ni_vp->v_type == VDIR)
3628 tond.ni_cnd.cn_flags |= WILLBEDIR;
3629 if ((error = namei(&tond)) != 0) {
3630 /* Translate error code for rename("dir1", "dir2/."). */
3631 if (error == EISDIR && fvp->v_type == VDIR)
3633 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3634 vrele(fromnd.ni_dvp);
3636 vn_finished_write(mp);
3639 tvfslocked = NDHASGIANT(&tond);
3643 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3646 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3656 * If the source is the same as the destination (that is, if they
3657 * are links to the same vnode), then there is nothing to do.
3663 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3664 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3668 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3669 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3670 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3671 NDFREE(&tond, NDF_ONLY_PNBUF);
3673 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3674 NDFREE(&tond, NDF_ONLY_PNBUF);
3681 vrele(fromnd.ni_dvp);
3684 vrele(tond.ni_startdir);
3685 vn_finished_write(mp);
3687 if (fromnd.ni_startdir)
3688 vrele(fromnd.ni_startdir);
3689 VFS_UNLOCK_GIANT(fvfslocked);
3690 VFS_UNLOCK_GIANT(tvfslocked);
3697 * Make a directory file.
3699 #ifndef _SYS_SYSPROTO_H_
3708 register struct mkdir_args /* {
3714 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3717 #ifndef _SYS_SYSPROTO_H_
3718 struct mkdirat_args {
3725 mkdirat(struct thread *td, struct mkdirat_args *uap)
3728 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3732 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3735 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3739 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3746 struct nameidata nd;
3749 AUDIT_ARG_MODE(mode);
3752 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
3753 segflg, path, fd, td);
3754 nd.ni_cnd.cn_flags |= WILLBEDIR;
3755 if ((error = namei(&nd)) != 0)
3757 vfslocked = NDHASGIANT(&nd);
3760 NDFREE(&nd, NDF_ONLY_PNBUF);
3762 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3763 * the strange behaviour of leaving the vnode unlocked
3764 * if the target is the same vnode as the parent.
3766 if (vp == nd.ni_dvp)
3771 VFS_UNLOCK_GIANT(vfslocked);
3774 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3775 NDFREE(&nd, NDF_ONLY_PNBUF);
3777 VFS_UNLOCK_GIANT(vfslocked);
3778 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3783 vattr.va_type = VDIR;
3784 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3786 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3791 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3795 NDFREE(&nd, NDF_ONLY_PNBUF);
3799 vn_finished_write(mp);
3800 VFS_UNLOCK_GIANT(vfslocked);
3805 * Remove a directory file.
3807 #ifndef _SYS_SYSPROTO_H_
3815 struct rmdir_args /* {
3820 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3824 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3827 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3831 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3836 struct nameidata nd;
3841 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
3842 pathseg, path, fd, td);
3843 if ((error = namei(&nd)) != 0)
3845 vfslocked = NDHASGIANT(&nd);
3847 if (vp->v_type != VDIR) {
3852 * No rmdir "." please.
3854 if (nd.ni_dvp == vp) {
3859 * The root of a mounted filesystem cannot be deleted.
3861 if (vp->v_vflag & VV_ROOT) {
3866 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3871 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3872 NDFREE(&nd, NDF_ONLY_PNBUF);
3874 if (nd.ni_dvp == vp)
3878 VFS_UNLOCK_GIANT(vfslocked);
3879 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3883 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3884 vn_finished_write(mp);
3886 NDFREE(&nd, NDF_ONLY_PNBUF);
3888 if (nd.ni_dvp == vp)
3892 VFS_UNLOCK_GIANT(vfslocked);
3898 * Read a block of directory entries in a filesystem independent format.
3900 #ifndef _SYS_SYSPROTO_H_
3901 struct ogetdirentries_args {
3909 ogetdirentries(td, uap)
3911 register struct ogetdirentries_args /* {
3920 struct uio auio, kuio;
3921 struct iovec aiov, kiov;
3922 struct dirent *dp, *edp;
3924 int error, eofflag, readcnt, vfslocked;
3927 /* XXX arbitrary sanity limit on `count'. */
3928 if (uap->count > 64 * 1024)
3930 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3932 if ((fp->f_flag & FREAD) == 0) {
3938 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3939 if (vp->v_type != VDIR) {
3940 VFS_UNLOCK_GIANT(vfslocked);
3944 aiov.iov_base = uap->buf;
3945 aiov.iov_len = uap->count;
3946 auio.uio_iov = &aiov;
3947 auio.uio_iovcnt = 1;
3948 auio.uio_rw = UIO_READ;
3949 auio.uio_segflg = UIO_USERSPACE;
3951 auio.uio_resid = uap->count;
3952 vn_lock(vp, LK_SHARED | LK_RETRY);
3953 loff = auio.uio_offset = fp->f_offset;
3955 error = mac_vnode_check_readdir(td->td_ucred, vp);
3958 VFS_UNLOCK_GIANT(vfslocked);
3963 # if (BYTE_ORDER != LITTLE_ENDIAN)
3964 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3965 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3967 fp->f_offset = auio.uio_offset;
3972 kuio.uio_iov = &kiov;
3973 kuio.uio_segflg = UIO_SYSSPACE;
3974 kiov.iov_len = uap->count;
3975 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
3976 kiov.iov_base = dirbuf;
3977 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3979 fp->f_offset = kuio.uio_offset;
3981 readcnt = uap->count - kuio.uio_resid;
3982 edp = (struct dirent *)&dirbuf[readcnt];
3983 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3984 # if (BYTE_ORDER == LITTLE_ENDIAN)
3986 * The expected low byte of
3987 * dp->d_namlen is our dp->d_type.
3988 * The high MBZ byte of dp->d_namlen
3989 * is our dp->d_namlen.
3991 dp->d_type = dp->d_namlen;
3995 * The dp->d_type is the high byte
3996 * of the expected dp->d_namlen,
3997 * so must be zero'ed.
4001 if (dp->d_reclen > 0) {
4002 dp = (struct dirent *)
4003 ((char *)dp + dp->d_reclen);
4010 error = uiomove(dirbuf, readcnt, &auio);
4012 free(dirbuf, M_TEMP);
4016 VFS_UNLOCK_GIANT(vfslocked);
4020 if (uap->count == auio.uio_resid &&
4021 (vp->v_vflag & VV_ROOT) &&
4022 (vp->v_mount->mnt_flag & MNT_UNION)) {
4023 struct vnode *tvp = vp;
4024 vp = vp->v_mount->mnt_vnodecovered;
4030 VFS_UNLOCK_GIANT(vfslocked);
4034 VFS_UNLOCK_GIANT(vfslocked);
4035 error = copyout(&loff, uap->basep, sizeof(long));
4037 td->td_retval[0] = uap->count - auio.uio_resid;
4040 #endif /* COMPAT_43 */
4043 * Read a block of directory entries in a filesystem independent format.
4045 #ifndef _SYS_SYSPROTO_H_
4046 struct getdirentries_args {
4054 getdirentries(td, uap)
4056 register struct getdirentries_args /* {
4066 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
4069 if (uap->basep != NULL)
4070 error = copyout(&base, uap->basep, sizeof(long));
4075 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4087 if (count > INT_MAX)
4089 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
4091 if ((fp->f_flag & FREAD) == 0) {
4097 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4098 if (vp->v_type != VDIR) {
4099 VFS_UNLOCK_GIANT(vfslocked);
4103 aiov.iov_base = buf;
4104 aiov.iov_len = count;
4105 auio.uio_iov = &aiov;
4106 auio.uio_iovcnt = 1;
4107 auio.uio_rw = UIO_READ;
4108 auio.uio_segflg = UIO_USERSPACE;
4110 auio.uio_resid = count;
4111 vn_lock(vp, LK_SHARED | LK_RETRY);
4112 AUDIT_ARG_VNODE1(vp);
4113 loff = auio.uio_offset = fp->f_offset;
4115 error = mac_vnode_check_readdir(td->td_ucred, vp);
4118 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4120 fp->f_offset = auio.uio_offset;
4123 VFS_UNLOCK_GIANT(vfslocked);
4126 if (count == auio.uio_resid &&
4127 (vp->v_vflag & VV_ROOT) &&
4128 (vp->v_mount->mnt_flag & MNT_UNION)) {
4129 struct vnode *tvp = vp;
4130 vp = vp->v_mount->mnt_vnodecovered;
4136 VFS_UNLOCK_GIANT(vfslocked);
4140 VFS_UNLOCK_GIANT(vfslocked);
4142 td->td_retval[0] = count - auio.uio_resid;
4148 #ifndef _SYS_SYSPROTO_H_
4149 struct getdents_args {
4158 register struct getdents_args /* {
4164 struct getdirentries_args ap;
4167 ap.count = uap->count;
4169 return (getdirentries(td, &ap));
4173 * Set the mode mask for creation of filesystem nodes.
4175 #ifndef _SYS_SYSPROTO_H_
4183 struct umask_args /* {
4187 register struct filedesc *fdp;
4189 FILEDESC_XLOCK(td->td_proc->p_fd);
4190 fdp = td->td_proc->p_fd;
4191 td->td_retval[0] = fdp->fd_cmask;
4192 fdp->fd_cmask = uap->newmask & ALLPERMS;
4193 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4198 * Void all references to file by ripping underlying filesystem away from
4201 #ifndef _SYS_SYSPROTO_H_
4202 struct revoke_args {
4209 register struct revoke_args /* {
4216 struct nameidata nd;
4219 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4220 UIO_USERSPACE, uap->path, td);
4221 if ((error = namei(&nd)) != 0)
4223 vfslocked = NDHASGIANT(&nd);
4225 NDFREE(&nd, NDF_ONLY_PNBUF);
4226 if (vp->v_type != VCHR) {
4231 error = mac_vnode_check_revoke(td->td_ucred, vp);
4235 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4238 if (td->td_ucred->cr_uid != vattr.va_uid) {
4239 error = priv_check(td, PRIV_VFS_ADMIN);
4244 VOP_REVOKE(vp, REVOKEALL);
4247 VFS_UNLOCK_GIANT(vfslocked);
4252 * Convert a user file descriptor to a kernel file entry.
4253 * A reference on the file entry is held upon returning.
4256 getvnode(fdp, fd, fpp)
4257 struct filedesc *fdp;
4266 if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL)
4268 else if (fp->f_vnode == NULL) {
4270 fdrop(fp, curthread);
4277 * Get an (NFS) file handle.
4279 #ifndef _SYS_SYSPROTO_H_
4280 struct lgetfh_args {
4288 register struct lgetfh_args *uap;
4290 struct nameidata nd;
4292 register struct vnode *vp;
4296 error = priv_check(td, PRIV_VFS_GETFH);
4299 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4300 UIO_USERSPACE, uap->fname, td);
4304 vfslocked = NDHASGIANT(&nd);
4305 NDFREE(&nd, NDF_ONLY_PNBUF);
4307 bzero(&fh, sizeof(fh));
4308 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4309 error = VOP_VPTOFH(vp, &fh.fh_fid);
4311 VFS_UNLOCK_GIANT(vfslocked);
4314 error = copyout(&fh, uap->fhp, sizeof (fh));
4318 #ifndef _SYS_SYSPROTO_H_
4327 register struct getfh_args *uap;
4329 struct nameidata nd;
4331 register struct vnode *vp;
4335 error = priv_check(td, PRIV_VFS_GETFH);
4338 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4339 UIO_USERSPACE, uap->fname, td);
4343 vfslocked = NDHASGIANT(&nd);
4344 NDFREE(&nd, NDF_ONLY_PNBUF);
4346 bzero(&fh, sizeof(fh));
4347 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4348 error = VOP_VPTOFH(vp, &fh.fh_fid);
4350 VFS_UNLOCK_GIANT(vfslocked);
4353 error = copyout(&fh, uap->fhp, sizeof (fh));
4358 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4361 * warning: do not remove the priv_check() call or this becomes one giant
4364 #ifndef _SYS_SYSPROTO_H_
4365 struct fhopen_args {
4366 const struct fhandle *u_fhp;
4373 struct fhopen_args /* {
4374 const struct fhandle *u_fhp;
4378 struct proc *p = td->td_proc;
4383 struct vattr *vap = &vat;
4386 register struct filedesc *fdp = p->p_fd;
4387 int fmode, error, type;
4393 error = priv_check(td, PRIV_VFS_FHOPEN);
4396 fmode = FFLAGS(uap->flags);
4397 /* why not allow a non-read/write open for our lockd? */
4398 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4400 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4403 /* find the mount point */
4404 mp = vfs_busyfs(&fhp.fh_fsid);
4407 vfslocked = VFS_LOCK_GIANT(mp);
4408 /* now give me my vnode, it gets returned to me locked */
4409 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4414 * from now on we have to make sure not
4415 * to forget about the vnode
4416 * any error that causes an abort must vput(vp)
4417 * just set error = err and 'goto bad;'.
4423 if (vp->v_type == VLNK) {
4427 if (vp->v_type == VSOCK) {
4432 if (fmode & (FWRITE | O_TRUNC)) {
4433 if (vp->v_type == VDIR) {
4437 error = vn_writechk(vp);
4444 if ((fmode & O_APPEND) && (fmode & FWRITE))
4447 error = mac_vnode_check_open(td->td_ucred, vp, accmode);
4452 error = VOP_ACCESS(vp, accmode, td->td_ucred, td);
4456 if (fmode & O_TRUNC) {
4458 VOP_UNLOCK(vp, 0); /* XXX */
4459 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4464 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4468 * We don't yet have fp->f_cred, so use td->td_ucred, which
4471 error = mac_vnode_check_write(td->td_ucred, td->td_ucred, vp);
4476 error = VOP_SETATTR(vp, vap, td->td_ucred);
4480 vn_finished_write(mp);
4484 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4492 * end of vn_open code
4495 if ((error = falloc(td, &nfp, &indx)) != 0) {
4500 /* An extra reference on `nfp' has been held for us by falloc(). */
4503 finit(nfp, fmode & FMASK, DTYPE_VNODE, vp, &vnops);
4504 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4505 lf.l_whence = SEEK_SET;
4508 if (fmode & O_EXLOCK)
4509 lf.l_type = F_WRLCK;
4511 lf.l_type = F_RDLCK;
4513 if ((fmode & FNONBLOCK) == 0)
4516 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4519 * The lock request failed. Normally close the
4520 * descriptor but handle the case where someone might
4521 * have dup()d or close()d it when we weren't looking.
4523 fdclose(fdp, fp, indx, td);
4526 * release our private reference
4531 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4532 atomic_set_int(&fp->f_flag, FHASLOCK);
4537 VFS_UNLOCK_GIANT(vfslocked);
4538 td->td_retval[0] = indx;
4544 VFS_UNLOCK_GIANT(vfslocked);
4549 * Stat an (NFS) file handle.
4551 #ifndef _SYS_SYSPROTO_H_
4552 struct fhstat_args {
4553 struct fhandle *u_fhp;
4560 register struct fhstat_args /* {
4561 struct fhandle *u_fhp;
4572 error = priv_check(td, PRIV_VFS_FHSTAT);
4575 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4578 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4580 vfslocked = VFS_LOCK_GIANT(mp);
4581 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4584 VFS_UNLOCK_GIANT(vfslocked);
4587 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4589 VFS_UNLOCK_GIANT(vfslocked);
4592 error = copyout(&sb, uap->sb, sizeof(sb));
4597 * Implement fstatfs() for (NFS) file handles.
4599 #ifndef _SYS_SYSPROTO_H_
4600 struct fhstatfs_args {
4601 struct fhandle *u_fhp;
4608 struct fhstatfs_args /* {
4609 struct fhandle *u_fhp;
4617 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4620 error = kern_fhstatfs(td, fh, &sf);
4623 return (copyout(&sf, uap->buf, sizeof(sf)));
4627 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4635 error = priv_check(td, PRIV_VFS_FHSTATFS);
4638 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4640 vfslocked = VFS_LOCK_GIANT(mp);
4641 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4644 VFS_UNLOCK_GIANT(vfslocked);
4648 error = prison_canseemount(td->td_ucred, mp);
4652 error = mac_mount_check_stat(td->td_ucred, mp);
4657 * Set these in case the underlying filesystem fails to do so.
4660 sp->f_version = STATFS_VERSION;
4661 sp->f_namemax = NAME_MAX;
4662 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4663 error = VFS_STATFS(mp, sp);
4668 VFS_UNLOCK_GIANT(vfslocked);