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_kdtrace.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/capability.h>
51 #include <sys/sysent.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/namei.h>
57 #include <sys/filedesc.h>
58 #include <sys/kernel.h>
59 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #include <sys/limits.h>
63 #include <sys/linker.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 SDT_PROVIDER_DEFINE(vfs);
90 SDT_PROBE_DEFINE(vfs, , stat, mode, mode);
91 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 0, "char *");
92 SDT_PROBE_ARGTYPE(vfs, , stat, mode, 1, "int");
93 SDT_PROBE_DEFINE(vfs, , stat, reg, reg);
94 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 0, "char *");
95 SDT_PROBE_ARGTYPE(vfs, , stat, reg, 1, "int");
97 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
98 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
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 {
179 sys_quotactl(td, uap)
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, CAP_FSTATFS, &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 {
440 sys_getfsstat(td, uap)
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, CAP_FCHDIR, &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);
994 static __inline cap_rights_t
995 flags_to_rights(int flags)
997 cap_rights_t rights = 0;
999 switch ((flags & O_ACCMODE)) {
1009 rights |= CAP_WRITE;
1013 rights |= CAP_FEXECVE;
1017 if (flags & O_CREAT)
1018 rights |= CAP_CREATE;
1020 if (flags & O_TRUNC)
1021 rights |= CAP_FTRUNCATE;
1023 if ((flags & O_EXLOCK) || (flags & O_SHLOCK))
1024 rights |= CAP_FLOCK;
1030 * Check permissions, allocate an open file structure, and call the device
1031 * open routine if any.
1033 #ifndef _SYS_SYSPROTO_H_
1043 register struct open_args /* {
1050 return (kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode));
1053 #ifndef _SYS_SYSPROTO_H_
1054 struct openat_args {
1062 sys_openat(struct thread *td, struct openat_args *uap)
1065 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1070 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
1074 return (kern_openat(td, AT_FDCWD, path, pathseg, flags, mode));
1078 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1079 int flags, int mode)
1081 struct proc *p = td->td_proc;
1082 struct filedesc *fdp = p->p_fd;
1087 int type, indx = -1, error, error_open;
1089 struct nameidata nd;
1091 cap_rights_t rights_needed = CAP_LOOKUP;
1093 AUDIT_ARG_FFLAGS(flags);
1094 AUDIT_ARG_MODE(mode);
1095 /* XXX: audit dirfd */
1096 rights_needed |= flags_to_rights(flags);
1098 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1101 if (flags & O_EXEC) {
1102 if (flags & O_ACCMODE)
1104 } else if ((flags & O_ACCMODE) == O_ACCMODE)
1107 flags = FFLAGS(flags);
1110 * allocate the file descriptor, but don't install a descriptor yet
1112 error = falloc_noinstall(td, &nfp);
1115 /* An extra reference on `nfp' has been held for us by falloc_noinstall(). */
1117 /* Set the flags early so the finit in devfs can pick them up. */
1118 fp->f_flag = flags & FMASK;
1119 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1120 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | MPSAFE, pathseg,
1121 path, fd, rights_needed, td);
1122 td->td_dupfd = -1; /* XXX check for fdopen */
1123 error = vn_open(&nd, &flags, cmode, fp);
1126 * If the vn_open replaced the method vector, something
1127 * wonderous happened deep below and we just pass it up
1128 * pretending we know what we do.
1130 if (error == ENXIO && fp->f_ops != &badfileops)
1134 * handle special fdopen() case. bleh. dupfdopen() is
1135 * responsible for dropping the old contents of ofiles[indx]
1138 * Don't do this for relative (capability) lookups; we don't
1139 * understand exactly what would happen, and we don't think
1140 * that it ever should.
1142 if ((nd.ni_strictrelative == 0) &&
1143 (error == ENODEV || error == ENXIO) &&
1144 (td->td_dupfd >= 0)) {
1145 /* XXX from fdopen */
1147 if ((error = finstall(td, fp, &indx, flags)) != 0)
1149 if ((error = dupfdopen(td, fdp, indx, td->td_dupfd,
1150 flags, error_open)) == 0)
1154 * Clean up the descriptor, but only if another thread hadn't
1155 * replaced or closed it.
1158 fdclose(fdp, fp, indx, td);
1161 if (error == ERESTART)
1166 vfslocked = NDHASGIANT(&nd);
1167 NDFREE(&nd, NDF_ONLY_PNBUF);
1171 * Store the vnode, for any f_type. Typically, the vnode use
1172 * count is decremented by direct call to vn_closefile() for
1173 * files that switched type in the cdevsw fdopen() method.
1177 * If the file wasn't claimed by devfs bind it to the normal
1178 * vnode operations here.
1180 if (fp->f_ops == &badfileops) {
1181 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo."));
1183 finit(fp, flags & FMASK, DTYPE_VNODE, vp, &vnops);
1187 if (fp->f_type == DTYPE_VNODE && (flags & (O_EXLOCK | O_SHLOCK)) != 0) {
1188 lf.l_whence = SEEK_SET;
1191 if (flags & O_EXLOCK)
1192 lf.l_type = F_WRLCK;
1194 lf.l_type = F_RDLCK;
1196 if ((flags & FNONBLOCK) == 0)
1198 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1201 atomic_set_int(&fp->f_flag, FHASLOCK);
1203 if (flags & O_TRUNC) {
1204 error = fo_truncate(fp, 0, td->td_ucred, td);
1208 VFS_UNLOCK_GIANT(vfslocked);
1211 * If we haven't already installed the FD (for dupfdopen), do so now.
1215 if (nd.ni_strictrelative == 1) {
1217 * We are doing a strict relative lookup; wrap the
1218 * result in a capability.
1220 if ((error = kern_capwrap(td, fp, nd.ni_baserights,
1225 if ((error = finstall(td, fp, &indx, flags)) != 0)
1231 * Release our private reference, leaving the one associated with
1232 * the descriptor table intact.
1235 td->td_retval[0] = indx;
1238 VFS_UNLOCK_GIANT(vfslocked);
1241 fdclose(fdp, fp, indx, td);
1243 td->td_retval[0] = -1;
1251 #ifndef _SYS_SYSPROTO_H_
1252 struct ocreat_args {
1260 register struct ocreat_args /* {
1266 return (kern_open(td, uap->path, UIO_USERSPACE,
1267 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1269 #endif /* COMPAT_43 */
1272 * Create a special file.
1274 #ifndef _SYS_SYSPROTO_H_
1284 register struct mknod_args /* {
1291 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1294 #ifndef _SYS_SYSPROTO_H_
1295 struct mknodat_args {
1303 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1306 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1311 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1315 return (kern_mknodat(td, AT_FDCWD, path, pathseg, mode, dev));
1319 kern_mknodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1327 struct nameidata nd;
1330 AUDIT_ARG_MODE(mode);
1332 switch (mode & S_IFMT) {
1335 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1338 error = priv_check(td, PRIV_VFS_MKNOD_BAD);
1341 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1345 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1355 NDINIT_ATRIGHTS(&nd, CREATE,
1356 LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1, pathseg, path, fd,
1358 if ((error = namei(&nd)) != 0)
1360 vfslocked = NDHASGIANT(&nd);
1363 NDFREE(&nd, NDF_ONLY_PNBUF);
1364 if (vp == nd.ni_dvp)
1369 VFS_UNLOCK_GIANT(vfslocked);
1373 vattr.va_mode = (mode & ALLPERMS) &
1374 ~td->td_proc->p_fd->fd_cmask;
1375 vattr.va_rdev = dev;
1378 switch (mode & S_IFMT) {
1379 case S_IFMT: /* used by badsect to flag bad sectors */
1380 vattr.va_type = VBAD;
1383 vattr.va_type = VCHR;
1386 vattr.va_type = VBLK;
1392 panic("kern_mknod: invalid mode");
1395 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1396 NDFREE(&nd, NDF_ONLY_PNBUF);
1398 VFS_UNLOCK_GIANT(vfslocked);
1399 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1404 if (error == 0 && !whiteout)
1405 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1406 &nd.ni_cnd, &vattr);
1410 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1412 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1413 &nd.ni_cnd, &vattr);
1418 NDFREE(&nd, NDF_ONLY_PNBUF);
1420 vn_finished_write(mp);
1421 VFS_UNLOCK_GIANT(vfslocked);
1426 * Create a named pipe.
1428 #ifndef _SYS_SYSPROTO_H_
1429 struct mkfifo_args {
1437 register struct mkfifo_args /* {
1443 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1446 #ifndef _SYS_SYSPROTO_H_
1447 struct mkfifoat_args {
1454 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1457 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1462 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1465 return (kern_mkfifoat(td, AT_FDCWD, path, pathseg, mode));
1469 kern_mkfifoat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1475 struct nameidata nd;
1478 AUDIT_ARG_MODE(mode);
1481 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1482 pathseg, path, fd, td);
1483 if ((error = namei(&nd)) != 0)
1485 vfslocked = NDHASGIANT(&nd);
1486 if (nd.ni_vp != NULL) {
1487 NDFREE(&nd, NDF_ONLY_PNBUF);
1488 if (nd.ni_vp == nd.ni_dvp)
1493 VFS_UNLOCK_GIANT(vfslocked);
1496 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1497 NDFREE(&nd, NDF_ONLY_PNBUF);
1499 VFS_UNLOCK_GIANT(vfslocked);
1500 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1505 vattr.va_type = VFIFO;
1506 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1508 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1513 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1520 vn_finished_write(mp);
1521 VFS_UNLOCK_GIANT(vfslocked);
1522 NDFREE(&nd, NDF_ONLY_PNBUF);
1527 * Make a hard file link.
1529 #ifndef _SYS_SYSPROTO_H_
1538 register struct link_args /* {
1544 return (kern_link(td, uap->path, uap->link, UIO_USERSPACE));
1547 #ifndef _SYS_SYSPROTO_H_
1548 struct linkat_args {
1557 sys_linkat(struct thread *td, struct linkat_args *uap)
1562 if (flag & ~AT_SYMLINK_FOLLOW)
1565 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1566 UIO_USERSPACE, (flag & AT_SYMLINK_FOLLOW) ? FOLLOW : NOFOLLOW));
1569 int hardlink_check_uid = 0;
1570 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1571 &hardlink_check_uid, 0,
1572 "Unprivileged processes cannot create hard links to files owned by other "
1574 static int hardlink_check_gid = 0;
1575 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1576 &hardlink_check_gid, 0,
1577 "Unprivileged processes cannot create hard links to files owned by other "
1581 can_hardlink(struct vnode *vp, struct ucred *cred)
1586 if (!hardlink_check_uid && !hardlink_check_gid)
1589 error = VOP_GETATTR(vp, &va, cred);
1593 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1594 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1599 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1600 error = priv_check_cred(cred, PRIV_VFS_LINK, 0);
1609 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1612 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, path,link, segflg, FOLLOW));
1616 kern_linkat(struct thread *td, int fd1, int fd2, char *path1, char *path2,
1617 enum uio_seg segflg, int follow)
1621 struct nameidata nd;
1627 NDINIT_AT(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, segflg, path1,
1630 if ((error = namei(&nd)) != 0)
1632 vfslocked = NDHASGIANT(&nd);
1633 NDFREE(&nd, NDF_ONLY_PNBUF);
1635 if (vp->v_type == VDIR) {
1637 VFS_UNLOCK_GIANT(vfslocked);
1638 return (EPERM); /* POSIX */
1640 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1642 VFS_UNLOCK_GIANT(vfslocked);
1645 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE2,
1646 segflg, path2, fd2, td);
1647 if ((error = namei(&nd)) == 0) {
1648 lvfslocked = NDHASGIANT(&nd);
1649 if (nd.ni_vp != NULL) {
1650 if (nd.ni_dvp == nd.ni_vp)
1656 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY))
1658 error = can_hardlink(vp, td->td_ucred);
1661 error = mac_vnode_check_link(td->td_ucred,
1662 nd.ni_dvp, vp, &nd.ni_cnd);
1665 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1669 NDFREE(&nd, NDF_ONLY_PNBUF);
1670 VFS_UNLOCK_GIANT(lvfslocked);
1673 vn_finished_write(mp);
1674 VFS_UNLOCK_GIANT(vfslocked);
1679 * Make a symbolic link.
1681 #ifndef _SYS_SYSPROTO_H_
1682 struct symlink_args {
1688 sys_symlink(td, uap)
1690 register struct symlink_args /* {
1696 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1699 #ifndef _SYS_SYSPROTO_H_
1700 struct symlinkat_args {
1707 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1710 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1715 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1718 return (kern_symlinkat(td, path, AT_FDCWD, link, segflg));
1722 kern_symlinkat(struct thread *td, char *path1, int fd, char *path2,
1723 enum uio_seg segflg)
1729 struct nameidata nd;
1732 if (segflg == UIO_SYSSPACE) {
1735 syspath = uma_zalloc(namei_zone, M_WAITOK);
1736 if ((error = copyinstr(path1, syspath, MAXPATHLEN, NULL)) != 0)
1739 AUDIT_ARG_TEXT(syspath);
1742 NDINIT_AT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE | AUDITVNODE1,
1743 segflg, path2, fd, td);
1744 if ((error = namei(&nd)) != 0)
1746 vfslocked = NDHASGIANT(&nd);
1748 NDFREE(&nd, NDF_ONLY_PNBUF);
1749 if (nd.ni_vp == nd.ni_dvp)
1754 VFS_UNLOCK_GIANT(vfslocked);
1758 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1759 NDFREE(&nd, NDF_ONLY_PNBUF);
1761 VFS_UNLOCK_GIANT(vfslocked);
1762 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1767 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1769 vattr.va_type = VLNK;
1770 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1775 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1781 NDFREE(&nd, NDF_ONLY_PNBUF);
1783 vn_finished_write(mp);
1784 VFS_UNLOCK_GIANT(vfslocked);
1786 if (segflg != UIO_SYSSPACE)
1787 uma_zfree(namei_zone, syspath);
1792 * Delete a whiteout from the filesystem.
1795 sys_undelete(td, uap)
1797 register struct undelete_args /* {
1803 struct nameidata nd;
1808 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE | AUDITVNODE1,
1809 UIO_USERSPACE, uap->path, td);
1813 vfslocked = NDHASGIANT(&nd);
1815 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1816 NDFREE(&nd, NDF_ONLY_PNBUF);
1817 if (nd.ni_vp == nd.ni_dvp)
1823 VFS_UNLOCK_GIANT(vfslocked);
1826 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1827 NDFREE(&nd, NDF_ONLY_PNBUF);
1829 VFS_UNLOCK_GIANT(vfslocked);
1830 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1834 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1835 NDFREE(&nd, NDF_ONLY_PNBUF);
1837 vn_finished_write(mp);
1838 VFS_UNLOCK_GIANT(vfslocked);
1843 * Delete a name from the filesystem.
1845 #ifndef _SYS_SYSPROTO_H_
1846 struct unlink_args {
1853 struct unlink_args /* {
1858 return (kern_unlink(td, uap->path, UIO_USERSPACE));
1861 #ifndef _SYS_SYSPROTO_H_
1862 struct unlinkat_args {
1869 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1871 int flag = uap->flag;
1873 char *path = uap->path;
1875 if (flag & ~AT_REMOVEDIR)
1878 if (flag & AT_REMOVEDIR)
1879 return (kern_rmdirat(td, fd, path, UIO_USERSPACE));
1881 return (kern_unlinkat(td, fd, path, UIO_USERSPACE, 0));
1885 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1888 return (kern_unlinkat(td, AT_FDCWD, path, pathseg, 0));
1892 kern_unlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
1898 struct nameidata nd;
1904 NDINIT_AT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE | AUDITVNODE1,
1905 pathseg, path, fd, td);
1906 if ((error = namei(&nd)) != 0)
1907 return (error == EINVAL ? EPERM : error);
1908 vfslocked = NDHASGIANT(&nd);
1910 if (vp->v_type == VDIR && oldinum == 0) {
1911 error = EPERM; /* POSIX */
1912 } else if (oldinum != 0 &&
1913 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) &&
1914 sb.st_ino != oldinum) {
1915 error = EIDRM; /* Identifier removed */
1918 * The root of a mounted filesystem cannot be deleted.
1920 * XXX: can this only be a VDIR case?
1922 if (vp->v_vflag & VV_ROOT)
1926 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1927 NDFREE(&nd, NDF_ONLY_PNBUF);
1929 if (vp == nd.ni_dvp)
1933 VFS_UNLOCK_GIANT(vfslocked);
1934 if ((error = vn_start_write(NULL, &mp,
1935 V_XSLEEP | PCATCH)) != 0)
1940 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1945 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1949 vn_finished_write(mp);
1951 NDFREE(&nd, NDF_ONLY_PNBUF);
1953 if (vp == nd.ni_dvp)
1957 VFS_UNLOCK_GIANT(vfslocked);
1962 * Reposition read/write file offset.
1964 #ifndef _SYS_SYSPROTO_H_
1975 register struct lseek_args /* {
1982 struct ucred *cred = td->td_ucred;
1990 AUDIT_ARG_FD(uap->fd);
1991 if ((error = fget(td, uap->fd, CAP_SEEK, &fp)) != 0)
1993 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1998 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1999 noneg = (vp->v_type != VCHR);
2000 offset = uap->offset;
2001 switch (uap->whence) {
2004 (fp->f_offset < 0 ||
2005 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
2009 offset += fp->f_offset;
2012 vn_lock(vp, LK_SHARED | LK_RETRY);
2013 error = VOP_GETATTR(vp, &vattr, cred);
2019 * If the file references a disk device, then fetch
2020 * the media size and use that to determine the ending
2023 if (vattr.va_size == 0 && vp->v_type == VCHR &&
2024 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0)
2025 vattr.va_size = size;
2027 (vattr.va_size > OFF_MAX ||
2028 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
2032 offset += vattr.va_size;
2037 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
2040 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
2045 if (error == 0 && noneg && offset < 0)
2049 fp->f_offset = offset;
2050 *(off_t *)(td->td_retval) = fp->f_offset;
2053 VFS_UNLOCK_GIANT(vfslocked);
2057 #if defined(COMPAT_43)
2059 * Reposition read/write file offset.
2061 #ifndef _SYS_SYSPROTO_H_
2062 struct olseek_args {
2071 register struct olseek_args /* {
2077 struct lseek_args /* {
2085 nuap.offset = uap->offset;
2086 nuap.whence = uap->whence;
2087 return (sys_lseek(td, &nuap));
2089 #endif /* COMPAT_43 */
2091 /* Version with the 'pad' argument */
2093 freebsd6_lseek(td, uap)
2095 register struct freebsd6_lseek_args *uap;
2097 struct lseek_args ouap;
2100 ouap.offset = uap->offset;
2101 ouap.whence = uap->whence;
2102 return (sys_lseek(td, &ouap));
2106 * Check access permissions using passed credentials.
2109 vn_access(vp, user_flags, cred, td)
2118 /* Flags == 0 means only check for existence. */
2122 if (user_flags & R_OK)
2124 if (user_flags & W_OK)
2126 if (user_flags & X_OK)
2129 error = mac_vnode_check_access(cred, vp, accmode);
2133 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2134 error = VOP_ACCESS(vp, accmode, cred, td);
2140 * Check access permissions using "real" credentials.
2142 #ifndef _SYS_SYSPROTO_H_
2143 struct access_args {
2151 register struct access_args /* {
2157 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
2160 #ifndef _SYS_SYSPROTO_H_
2161 struct faccessat_args {
2169 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2172 if (uap->flag & ~AT_EACCESS)
2174 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2179 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2182 return (kern_accessat(td, AT_FDCWD, path, pathseg, 0, mode));
2186 kern_accessat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2187 int flags, int mode)
2189 struct ucred *cred, *tmpcred;
2191 struct nameidata nd;
2196 * Create and modify a temporary credential instead of one that
2197 * is potentially shared.
2199 if (!(flags & AT_EACCESS)) {
2200 cred = td->td_ucred;
2201 tmpcred = crdup(cred);
2202 tmpcred->cr_uid = cred->cr_ruid;
2203 tmpcred->cr_groups[0] = cred->cr_rgid;
2204 td->td_ucred = tmpcred;
2206 cred = tmpcred = td->td_ucred;
2207 AUDIT_ARG_VALUE(mode);
2208 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2209 AUDITVNODE1, pathseg, path, fd, CAP_FSTAT, td);
2210 if ((error = namei(&nd)) != 0)
2212 vfslocked = NDHASGIANT(&nd);
2215 error = vn_access(vp, mode, tmpcred, td);
2216 NDFREE(&nd, NDF_ONLY_PNBUF);
2218 VFS_UNLOCK_GIANT(vfslocked);
2220 if (!(flags & AT_EACCESS)) {
2221 td->td_ucred = cred;
2228 * Check access permissions using "effective" credentials.
2230 #ifndef _SYS_SYSPROTO_H_
2231 struct eaccess_args {
2237 sys_eaccess(td, uap)
2239 register struct eaccess_args /* {
2245 return (kern_eaccess(td, uap->path, UIO_USERSPACE, uap->flags));
2249 kern_eaccess(struct thread *td, char *path, enum uio_seg pathseg, int flags)
2252 return (kern_accessat(td, AT_FDCWD, path, pathseg, AT_EACCESS, flags));
2255 #if defined(COMPAT_43)
2257 * Get file status; this version follows links.
2259 #ifndef _SYS_SYSPROTO_H_
2268 register struct ostat_args /* {
2277 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2281 error = copyout(&osb, uap->ub, sizeof (osb));
2286 * Get file status; this version does not follow links.
2288 #ifndef _SYS_SYSPROTO_H_
2289 struct olstat_args {
2297 register struct olstat_args /* {
2306 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2310 error = copyout(&osb, uap->ub, sizeof (osb));
2315 * Convert from an old to a new stat structure.
2323 ost->st_dev = st->st_dev;
2324 ost->st_ino = st->st_ino;
2325 ost->st_mode = st->st_mode;
2326 ost->st_nlink = st->st_nlink;
2327 ost->st_uid = st->st_uid;
2328 ost->st_gid = st->st_gid;
2329 ost->st_rdev = st->st_rdev;
2330 if (st->st_size < (quad_t)1 << 32)
2331 ost->st_size = st->st_size;
2334 ost->st_atim = st->st_atim;
2335 ost->st_mtim = st->st_mtim;
2336 ost->st_ctim = st->st_ctim;
2337 ost->st_blksize = st->st_blksize;
2338 ost->st_blocks = st->st_blocks;
2339 ost->st_flags = st->st_flags;
2340 ost->st_gen = st->st_gen;
2342 #endif /* COMPAT_43 */
2345 * Get file status; this version follows links.
2347 #ifndef _SYS_SYSPROTO_H_
2356 register struct stat_args /* {
2364 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2366 error = copyout(&sb, uap->ub, sizeof (sb));
2370 #ifndef _SYS_SYSPROTO_H_
2371 struct fstatat_args {
2379 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2384 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2385 UIO_USERSPACE, &sb);
2387 error = copyout(&sb, uap->buf, sizeof (sb));
2392 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2395 return (kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
2399 kern_statat(struct thread *td, int flag, int fd, char *path,
2400 enum uio_seg pathseg, struct stat *sbp)
2403 return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp, NULL));
2407 kern_statat_vnhook(struct thread *td, int flag, int fd, char *path,
2408 enum uio_seg pathseg, struct stat *sbp,
2409 void (*hook)(struct vnode *vp, struct stat *sbp))
2411 struct nameidata nd;
2413 int error, vfslocked;
2415 if (flag & ~AT_SYMLINK_NOFOLLOW)
2418 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW :
2419 FOLLOW) | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | MPSAFE, pathseg,
2420 path, fd, CAP_FSTAT, td);
2422 if ((error = namei(&nd)) != 0)
2424 vfslocked = NDHASGIANT(&nd);
2425 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2427 SDT_PROBE(vfs, , stat, mode, path, sb.st_mode, 0, 0, 0);
2428 if (S_ISREG(sb.st_mode))
2429 SDT_PROBE(vfs, , stat, reg, path, pathseg, 0, 0, 0);
2430 if (__predict_false(hook != NULL))
2431 hook(nd.ni_vp, &sb);
2433 NDFREE(&nd, NDF_ONLY_PNBUF);
2435 VFS_UNLOCK_GIANT(vfslocked);
2440 if (KTRPOINT(td, KTR_STRUCT))
2447 * Get file status; this version does not follow links.
2449 #ifndef _SYS_SYSPROTO_H_
2458 register struct lstat_args /* {
2466 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2468 error = copyout(&sb, uap->ub, sizeof (sb));
2473 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2476 return (kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, pathseg,
2481 * Implementation of the NetBSD [l]stat() functions.
2488 bzero(nsb, sizeof *nsb);
2489 nsb->st_dev = sb->st_dev;
2490 nsb->st_ino = sb->st_ino;
2491 nsb->st_mode = sb->st_mode;
2492 nsb->st_nlink = sb->st_nlink;
2493 nsb->st_uid = sb->st_uid;
2494 nsb->st_gid = sb->st_gid;
2495 nsb->st_rdev = sb->st_rdev;
2496 nsb->st_atim = sb->st_atim;
2497 nsb->st_mtim = sb->st_mtim;
2498 nsb->st_ctim = sb->st_ctim;
2499 nsb->st_size = sb->st_size;
2500 nsb->st_blocks = sb->st_blocks;
2501 nsb->st_blksize = sb->st_blksize;
2502 nsb->st_flags = sb->st_flags;
2503 nsb->st_gen = sb->st_gen;
2504 nsb->st_birthtim = sb->st_birthtim;
2507 #ifndef _SYS_SYSPROTO_H_
2516 register struct nstat_args /* {
2525 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2528 cvtnstat(&sb, &nsb);
2529 error = copyout(&nsb, uap->ub, sizeof (nsb));
2534 * NetBSD lstat. Get file status; this version does not follow links.
2536 #ifndef _SYS_SYSPROTO_H_
2545 register struct nlstat_args /* {
2554 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2557 cvtnstat(&sb, &nsb);
2558 error = copyout(&nsb, uap->ub, sizeof (nsb));
2563 * Get configurable pathname variables.
2565 #ifndef _SYS_SYSPROTO_H_
2566 struct pathconf_args {
2572 sys_pathconf(td, uap)
2574 register struct pathconf_args /* {
2580 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW));
2583 #ifndef _SYS_SYSPROTO_H_
2584 struct lpathconf_args {
2590 sys_lpathconf(td, uap)
2592 register struct lpathconf_args /* {
2598 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, NOFOLLOW));
2602 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name,
2605 struct nameidata nd;
2606 int error, vfslocked;
2608 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1 |
2609 flags, pathseg, path, td);
2610 if ((error = namei(&nd)) != 0)
2612 vfslocked = NDHASGIANT(&nd);
2613 NDFREE(&nd, NDF_ONLY_PNBUF);
2615 /* If asynchronous I/O is available, it works for all files. */
2616 if (name == _PC_ASYNC_IO)
2617 td->td_retval[0] = async_io_version;
2619 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2621 VFS_UNLOCK_GIANT(vfslocked);
2626 * Return target name of a symbolic link.
2628 #ifndef _SYS_SYSPROTO_H_
2629 struct readlink_args {
2636 sys_readlink(td, uap)
2638 register struct readlink_args /* {
2645 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2646 UIO_USERSPACE, uap->count));
2648 #ifndef _SYS_SYSPROTO_H_
2649 struct readlinkat_args {
2657 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2660 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2661 uap->buf, UIO_USERSPACE, uap->bufsize));
2665 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2666 enum uio_seg bufseg, size_t count)
2669 return (kern_readlinkat(td, AT_FDCWD, path, pathseg, buf, bufseg,
2674 kern_readlinkat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2675 char *buf, enum uio_seg bufseg, size_t count)
2681 struct nameidata nd;
2684 if (count > INT_MAX)
2687 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE |
2688 AUDITVNODE1, pathseg, path, fd, td);
2690 if ((error = namei(&nd)) != 0)
2692 NDFREE(&nd, NDF_ONLY_PNBUF);
2693 vfslocked = NDHASGIANT(&nd);
2696 error = mac_vnode_check_readlink(td->td_ucred, vp);
2699 VFS_UNLOCK_GIANT(vfslocked);
2703 if (vp->v_type != VLNK)
2706 aiov.iov_base = buf;
2707 aiov.iov_len = count;
2708 auio.uio_iov = &aiov;
2709 auio.uio_iovcnt = 1;
2710 auio.uio_offset = 0;
2711 auio.uio_rw = UIO_READ;
2712 auio.uio_segflg = bufseg;
2714 auio.uio_resid = count;
2715 error = VOP_READLINK(vp, &auio, td->td_ucred);
2718 VFS_UNLOCK_GIANT(vfslocked);
2719 td->td_retval[0] = count - auio.uio_resid;
2724 * Common implementation code for chflags() and fchflags().
2727 setfflags(td, vp, flags)
2737 * Prevent non-root users from setting flags on devices. When
2738 * a device is reused, users can retain ownership of the device
2739 * if they are allowed to set flags and programs assume that
2740 * chown can't fail when done as root.
2742 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2743 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2748 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2750 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2752 vattr.va_flags = flags;
2754 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2757 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2759 vn_finished_write(mp);
2764 * Change flags of a file given a path name.
2766 #ifndef _SYS_SYSPROTO_H_
2767 struct chflags_args {
2773 sys_chflags(td, uap)
2775 register struct chflags_args /* {
2781 struct nameidata nd;
2784 AUDIT_ARG_FFLAGS(uap->flags);
2785 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2787 if ((error = namei(&nd)) != 0)
2789 NDFREE(&nd, NDF_ONLY_PNBUF);
2790 vfslocked = NDHASGIANT(&nd);
2791 error = setfflags(td, nd.ni_vp, uap->flags);
2793 VFS_UNLOCK_GIANT(vfslocked);
2798 * Same as chflags() but doesn't follow symlinks.
2801 sys_lchflags(td, uap)
2803 register struct lchflags_args /* {
2809 struct nameidata nd;
2812 AUDIT_ARG_FFLAGS(uap->flags);
2813 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, UIO_USERSPACE,
2815 if ((error = namei(&nd)) != 0)
2817 vfslocked = NDHASGIANT(&nd);
2818 NDFREE(&nd, NDF_ONLY_PNBUF);
2819 error = setfflags(td, nd.ni_vp, uap->flags);
2821 VFS_UNLOCK_GIANT(vfslocked);
2826 * Change flags of a file given a file descriptor.
2828 #ifndef _SYS_SYSPROTO_H_
2829 struct fchflags_args {
2835 sys_fchflags(td, uap)
2837 register struct fchflags_args /* {
2846 AUDIT_ARG_FD(uap->fd);
2847 AUDIT_ARG_FFLAGS(uap->flags);
2848 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FCHFLAGS,
2851 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2853 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2854 AUDIT_ARG_VNODE1(fp->f_vnode);
2855 VOP_UNLOCK(fp->f_vnode, 0);
2857 error = setfflags(td, fp->f_vnode, uap->flags);
2858 VFS_UNLOCK_GIANT(vfslocked);
2864 * Common implementation code for chmod(), lchmod() and fchmod().
2867 setfmode(td, cred, vp, mode)
2877 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2879 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2881 vattr.va_mode = mode & ALLPERMS;
2883 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2886 error = VOP_SETATTR(vp, &vattr, cred);
2888 vn_finished_write(mp);
2893 * Change mode of a file given path name.
2895 #ifndef _SYS_SYSPROTO_H_
2904 register struct chmod_args /* {
2910 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2913 #ifndef _SYS_SYSPROTO_H_
2914 struct fchmodat_args {
2922 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2924 int flag = uap->flag;
2926 char *path = uap->path;
2927 mode_t mode = uap->mode;
2929 if (flag & ~AT_SYMLINK_NOFOLLOW)
2932 return (kern_fchmodat(td, fd, path, UIO_USERSPACE, mode, flag));
2936 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2939 return (kern_fchmodat(td, AT_FDCWD, path, pathseg, mode, 0));
2943 * Change mode of a file given path name (don't follow links.)
2945 #ifndef _SYS_SYSPROTO_H_
2946 struct lchmod_args {
2954 register struct lchmod_args /* {
2960 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2961 uap->mode, AT_SYMLINK_NOFOLLOW));
2966 kern_fchmodat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
2967 mode_t mode, int flag)
2970 struct nameidata nd;
2974 AUDIT_ARG_MODE(mode);
2975 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
2976 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg,
2977 path, fd, CAP_FCHMOD, td);
2978 if ((error = namei(&nd)) != 0)
2980 vfslocked = NDHASGIANT(&nd);
2981 NDFREE(&nd, NDF_ONLY_PNBUF);
2982 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2984 VFS_UNLOCK_GIANT(vfslocked);
2989 * Change mode of a file given a file descriptor.
2991 #ifndef _SYS_SYSPROTO_H_
2992 struct fchmod_args {
2998 sys_fchmod(struct thread *td, struct fchmod_args *uap)
3003 AUDIT_ARG_FD(uap->fd);
3004 AUDIT_ARG_MODE(uap->mode);
3006 error = fget(td, uap->fd, CAP_FCHMOD, &fp);
3009 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
3015 * Common implementation for chown(), lchown(), and fchown()
3018 setfown(td, cred, vp, uid, gid)
3029 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3031 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3036 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
3040 error = VOP_SETATTR(vp, &vattr, cred);
3042 vn_finished_write(mp);
3047 * Set ownership given a path name.
3049 #ifndef _SYS_SYSPROTO_H_
3059 register struct chown_args /* {
3066 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3069 #ifndef _SYS_SYSPROTO_H_
3070 struct fchownat_args {
3079 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3084 if (flag & ~AT_SYMLINK_NOFOLLOW)
3087 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3088 uap->gid, uap->flag));
3092 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3096 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid, 0));
3100 kern_fchownat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3101 int uid, int gid, int flag)
3103 struct nameidata nd;
3104 int error, vfslocked, follow;
3106 AUDIT_ARG_OWNER(uid, gid);
3107 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3108 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | MPSAFE | AUDITVNODE1, pathseg,
3109 path, fd, CAP_FCHOWN, td);
3111 if ((error = namei(&nd)) != 0)
3113 vfslocked = NDHASGIANT(&nd);
3114 NDFREE(&nd, NDF_ONLY_PNBUF);
3115 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3117 VFS_UNLOCK_GIANT(vfslocked);
3122 * Set ownership given a path name, do not cross symlinks.
3124 #ifndef _SYS_SYSPROTO_H_
3125 struct lchown_args {
3134 register struct lchown_args /* {
3141 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
3145 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
3149 return (kern_fchownat(td, AT_FDCWD, path, pathseg, uid, gid,
3150 AT_SYMLINK_NOFOLLOW));
3154 * Set ownership given a file descriptor.
3156 #ifndef _SYS_SYSPROTO_H_
3157 struct fchown_args {
3166 register struct fchown_args /* {
3175 AUDIT_ARG_FD(uap->fd);
3176 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3177 error = fget(td, uap->fd, CAP_FCHOWN, &fp);
3180 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3186 * Common implementation code for utimes(), lutimes(), and futimes().
3189 getutimes(usrtvp, tvpseg, tsp)
3190 const struct timeval *usrtvp;
3191 enum uio_seg tvpseg;
3192 struct timespec *tsp;
3194 struct timeval tv[2];
3195 const struct timeval *tvp;
3198 if (usrtvp == NULL) {
3199 vfs_timestamp(&tsp[0]);
3202 if (tvpseg == UIO_SYSSPACE) {
3205 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3210 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3211 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3213 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3214 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3220 * Common implementation code for utimes(), lutimes(), and futimes().
3223 setutimes(td, vp, ts, numtimes, nullflag)
3226 const struct timespec *ts;
3230 int error, setbirthtime;
3234 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3236 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3238 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) &&
3239 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3242 vattr.va_atime = ts[0];
3243 vattr.va_mtime = ts[1];
3245 vattr.va_birthtime = ts[1];
3247 vattr.va_birthtime = ts[2];
3249 vattr.va_vaflags |= VA_UTIMES_NULL;
3251 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3255 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3257 vn_finished_write(mp);
3262 * Set the access and modification times of a file.
3264 #ifndef _SYS_SYSPROTO_H_
3265 struct utimes_args {
3267 struct timeval *tptr;
3273 register struct utimes_args /* {
3275 struct timeval *tptr;
3279 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3283 #ifndef _SYS_SYSPROTO_H_
3284 struct futimesat_args {
3287 const struct timeval * times;
3291 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3294 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3295 uap->times, UIO_USERSPACE));
3299 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
3300 struct timeval *tptr, enum uio_seg tptrseg)
3303 return (kern_utimesat(td, AT_FDCWD, path, pathseg, tptr, tptrseg));
3307 kern_utimesat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
3308 struct timeval *tptr, enum uio_seg tptrseg)
3310 struct nameidata nd;
3311 struct timespec ts[2];
3312 int error, vfslocked;
3314 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3316 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg,
3317 path, fd, CAP_FUTIMES, td);
3319 if ((error = namei(&nd)) != 0)
3321 vfslocked = NDHASGIANT(&nd);
3322 NDFREE(&nd, NDF_ONLY_PNBUF);
3323 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3325 VFS_UNLOCK_GIANT(vfslocked);
3330 * Set the access and modification times of a file.
3332 #ifndef _SYS_SYSPROTO_H_
3333 struct lutimes_args {
3335 struct timeval *tptr;
3339 sys_lutimes(td, uap)
3341 register struct lutimes_args /* {
3343 struct timeval *tptr;
3347 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3352 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
3353 struct timeval *tptr, enum uio_seg tptrseg)
3355 struct timespec ts[2];
3357 struct nameidata nd;
3360 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3362 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3363 if ((error = namei(&nd)) != 0)
3365 vfslocked = NDHASGIANT(&nd);
3366 NDFREE(&nd, NDF_ONLY_PNBUF);
3367 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3369 VFS_UNLOCK_GIANT(vfslocked);
3374 * Set the access and modification times of a file.
3376 #ifndef _SYS_SYSPROTO_H_
3377 struct futimes_args {
3379 struct timeval *tptr;
3383 sys_futimes(td, uap)
3385 register struct futimes_args /* {
3387 struct timeval *tptr;
3391 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3395 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
3396 enum uio_seg tptrseg)
3398 struct timespec ts[2];
3404 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3406 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_FUTIMES, &fp))
3409 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
3411 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3412 AUDIT_ARG_VNODE1(fp->f_vnode);
3413 VOP_UNLOCK(fp->f_vnode, 0);
3415 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3416 VFS_UNLOCK_GIANT(vfslocked);
3422 * Truncate a file given its path name.
3424 #ifndef _SYS_SYSPROTO_H_
3425 struct truncate_args {
3432 sys_truncate(td, uap)
3434 register struct truncate_args /* {
3441 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3445 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
3451 struct nameidata nd;
3456 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE | AUDITVNODE1, pathseg, path, td);
3457 if ((error = namei(&nd)) != 0)
3459 vfslocked = NDHASGIANT(&nd);
3461 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
3463 VFS_UNLOCK_GIANT(vfslocked);
3466 NDFREE(&nd, NDF_ONLY_PNBUF);
3467 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3468 if (vp->v_type == VDIR)
3471 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) {
3474 else if ((error = vn_writechk(vp)) == 0 &&
3475 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
3477 vattr.va_size = length;
3478 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3481 vn_finished_write(mp);
3482 VFS_UNLOCK_GIANT(vfslocked);
3486 #if defined(COMPAT_43)
3488 * Truncate a file given its path name.
3490 #ifndef _SYS_SYSPROTO_H_
3491 struct otruncate_args {
3499 register struct otruncate_args /* {
3504 struct truncate_args /* {
3510 nuap.path = uap->path;
3511 nuap.length = uap->length;
3512 return (sys_truncate(td, &nuap));
3514 #endif /* COMPAT_43 */
3516 /* Versions with the pad argument */
3518 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3520 struct truncate_args ouap;
3522 ouap.path = uap->path;
3523 ouap.length = uap->length;
3524 return (sys_truncate(td, &ouap));
3528 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3530 struct ftruncate_args ouap;
3533 ouap.length = uap->length;
3534 return (sys_ftruncate(td, &ouap));
3538 * Sync an open file.
3540 #ifndef _SYS_SYSPROTO_H_
3548 struct fsync_args /* {
3556 int error, lock_flags;
3558 AUDIT_ARG_FD(uap->fd);
3559 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_FSYNC,
3563 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3564 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3566 if (MNT_SHARED_WRITES(mp) ||
3567 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
3568 lock_flags = LK_SHARED;
3570 lock_flags = LK_EXCLUSIVE;
3572 vn_lock(vp, lock_flags | LK_RETRY);
3573 AUDIT_ARG_VNODE1(vp);
3574 if (vp->v_object != NULL) {
3575 VM_OBJECT_LOCK(vp->v_object);
3576 vm_object_page_clean(vp->v_object, 0, 0, 0);
3577 VM_OBJECT_UNLOCK(vp->v_object);
3579 error = VOP_FSYNC(vp, MNT_WAIT, td);
3582 vn_finished_write(mp);
3584 VFS_UNLOCK_GIANT(vfslocked);
3590 * Rename files. Source and destination must either both be directories, or
3591 * both not be directories. If target is a directory, it must be empty.
3593 #ifndef _SYS_SYSPROTO_H_
3594 struct rename_args {
3602 register struct rename_args /* {
3608 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3611 #ifndef _SYS_SYSPROTO_H_
3612 struct renameat_args {
3620 sys_renameat(struct thread *td, struct renameat_args *uap)
3623 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3628 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3631 return (kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, pathseg));
3635 kern_renameat(struct thread *td, int oldfd, char *old, int newfd, char *new,
3636 enum uio_seg pathseg)
3638 struct mount *mp = NULL;
3639 struct vnode *tvp, *fvp, *tdvp;
3640 struct nameidata fromnd, tond;
3647 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART |
3648 MPSAFE | AUDITVNODE1, pathseg, old, oldfd, CAP_DELETE, td);
3650 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE |
3651 AUDITVNODE1, pathseg, old, oldfd, CAP_DELETE, td);
3654 if ((error = namei(&fromnd)) != 0)
3656 fvfslocked = NDHASGIANT(&fromnd);
3659 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp,
3660 fromnd.ni_vp, &fromnd.ni_cnd);
3661 VOP_UNLOCK(fromnd.ni_dvp, 0);
3662 if (fromnd.ni_dvp != fromnd.ni_vp)
3663 VOP_UNLOCK(fromnd.ni_vp, 0);
3667 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3669 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3670 vrele(fromnd.ni_dvp);
3674 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE |
3675 SAVESTART | MPSAFE | AUDITVNODE2, pathseg, new, newfd, CAP_CREATE,
3677 if (fromnd.ni_vp->v_type == VDIR)
3678 tond.ni_cnd.cn_flags |= WILLBEDIR;
3679 if ((error = namei(&tond)) != 0) {
3680 /* Translate error code for rename("dir1", "dir2/."). */
3681 if (error == EISDIR && fvp->v_type == VDIR)
3683 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3684 vrele(fromnd.ni_dvp);
3686 vn_finished_write(mp);
3689 tvfslocked = NDHASGIANT(&tond);
3693 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3696 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3706 * If the source is the same as the destination (that is, if they
3707 * are links to the same vnode), then there is nothing to do.
3713 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3714 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3718 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3719 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3720 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3721 NDFREE(&tond, NDF_ONLY_PNBUF);
3723 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3724 NDFREE(&tond, NDF_ONLY_PNBUF);
3731 vrele(fromnd.ni_dvp);
3734 vrele(tond.ni_startdir);
3735 vn_finished_write(mp);
3737 if (fromnd.ni_startdir)
3738 vrele(fromnd.ni_startdir);
3739 VFS_UNLOCK_GIANT(fvfslocked);
3740 VFS_UNLOCK_GIANT(tvfslocked);
3747 * Make a directory file.
3749 #ifndef _SYS_SYSPROTO_H_
3758 register struct mkdir_args /* {
3764 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3767 #ifndef _SYS_SYSPROTO_H_
3768 struct mkdirat_args {
3775 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3778 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3782 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3785 return (kern_mkdirat(td, AT_FDCWD, path, segflg, mode));
3789 kern_mkdirat(struct thread *td, int fd, char *path, enum uio_seg segflg,
3796 struct nameidata nd;
3799 AUDIT_ARG_MODE(mode);
3802 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE |
3803 AUDITVNODE1, segflg, path, fd, CAP_MKDIR, td);
3804 nd.ni_cnd.cn_flags |= WILLBEDIR;
3805 if ((error = namei(&nd)) != 0)
3807 vfslocked = NDHASGIANT(&nd);
3810 NDFREE(&nd, NDF_ONLY_PNBUF);
3812 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3813 * the strange behaviour of leaving the vnode unlocked
3814 * if the target is the same vnode as the parent.
3816 if (vp == nd.ni_dvp)
3821 VFS_UNLOCK_GIANT(vfslocked);
3824 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3825 NDFREE(&nd, NDF_ONLY_PNBUF);
3827 VFS_UNLOCK_GIANT(vfslocked);
3828 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3833 vattr.va_type = VDIR;
3834 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3836 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3841 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3845 NDFREE(&nd, NDF_ONLY_PNBUF);
3849 vn_finished_write(mp);
3850 VFS_UNLOCK_GIANT(vfslocked);
3855 * Remove a directory file.
3857 #ifndef _SYS_SYSPROTO_H_
3865 struct rmdir_args /* {
3870 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3874 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3877 return (kern_rmdirat(td, AT_FDCWD, path, pathseg));
3881 kern_rmdirat(struct thread *td, int fd, char *path, enum uio_seg pathseg)
3886 struct nameidata nd;
3891 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE |
3892 AUDITVNODE1, pathseg, path, fd, CAP_RMDIR, td);
3893 if ((error = namei(&nd)) != 0)
3895 vfslocked = NDHASGIANT(&nd);
3897 if (vp->v_type != VDIR) {
3902 * No rmdir "." please.
3904 if (nd.ni_dvp == vp) {
3909 * The root of a mounted filesystem cannot be deleted.
3911 if (vp->v_vflag & VV_ROOT) {
3916 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3921 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3922 NDFREE(&nd, NDF_ONLY_PNBUF);
3924 if (nd.ni_dvp == vp)
3928 VFS_UNLOCK_GIANT(vfslocked);
3929 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3933 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3934 vn_finished_write(mp);
3936 NDFREE(&nd, NDF_ONLY_PNBUF);
3938 if (nd.ni_dvp == vp)
3942 VFS_UNLOCK_GIANT(vfslocked);
3948 * Read a block of directory entries in a filesystem independent format.
3950 #ifndef _SYS_SYSPROTO_H_
3951 struct ogetdirentries_args {
3959 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
3964 error = kern_ogetdirentries(td, uap, &loff);
3966 error = copyout(&loff, uap->basep, sizeof(long));
3971 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
3976 struct uio auio, kuio;
3977 struct iovec aiov, kiov;
3978 struct dirent *dp, *edp;
3980 int error, eofflag, readcnt, vfslocked;
3983 /* XXX arbitrary sanity limit on `count'. */
3984 if (uap->count > 64 * 1024)
3986 if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_READ,
3989 if ((fp->f_flag & FREAD) == 0) {
3995 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3996 if (vp->v_type != VDIR) {
3997 VFS_UNLOCK_GIANT(vfslocked);
4001 aiov.iov_base = uap->buf;
4002 aiov.iov_len = uap->count;
4003 auio.uio_iov = &aiov;
4004 auio.uio_iovcnt = 1;
4005 auio.uio_rw = UIO_READ;
4006 auio.uio_segflg = UIO_USERSPACE;
4008 auio.uio_resid = uap->count;
4009 vn_lock(vp, LK_SHARED | LK_RETRY);
4010 loff = auio.uio_offset = fp->f_offset;
4012 error = mac_vnode_check_readdir(td->td_ucred, vp);
4015 VFS_UNLOCK_GIANT(vfslocked);
4020 # if (BYTE_ORDER != LITTLE_ENDIAN)
4021 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
4022 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
4024 fp->f_offset = auio.uio_offset;
4029 kuio.uio_iov = &kiov;
4030 kuio.uio_segflg = UIO_SYSSPACE;
4031 kiov.iov_len = uap->count;
4032 dirbuf = malloc(uap->count, M_TEMP, M_WAITOK);
4033 kiov.iov_base = dirbuf;
4034 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
4036 fp->f_offset = kuio.uio_offset;
4038 readcnt = uap->count - kuio.uio_resid;
4039 edp = (struct dirent *)&dirbuf[readcnt];
4040 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
4041 # if (BYTE_ORDER == LITTLE_ENDIAN)
4043 * The expected low byte of
4044 * dp->d_namlen is our dp->d_type.
4045 * The high MBZ byte of dp->d_namlen
4046 * is our dp->d_namlen.
4048 dp->d_type = dp->d_namlen;
4052 * The dp->d_type is the high byte
4053 * of the expected dp->d_namlen,
4054 * so must be zero'ed.
4058 if (dp->d_reclen > 0) {
4059 dp = (struct dirent *)
4060 ((char *)dp + dp->d_reclen);
4067 error = uiomove(dirbuf, readcnt, &auio);
4069 free(dirbuf, M_TEMP);
4073 VFS_UNLOCK_GIANT(vfslocked);
4077 if (uap->count == auio.uio_resid &&
4078 (vp->v_vflag & VV_ROOT) &&
4079 (vp->v_mount->mnt_flag & MNT_UNION)) {
4080 struct vnode *tvp = vp;
4081 vp = vp->v_mount->mnt_vnodecovered;
4087 VFS_UNLOCK_GIANT(vfslocked);
4091 VFS_UNLOCK_GIANT(vfslocked);
4093 td->td_retval[0] = uap->count - auio.uio_resid;
4098 #endif /* COMPAT_43 */
4101 * Read a block of directory entries in a filesystem independent format.
4103 #ifndef _SYS_SYSPROTO_H_
4104 struct getdirentries_args {
4112 sys_getdirentries(td, uap)
4114 register struct getdirentries_args /* {
4124 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
4127 if (uap->basep != NULL)
4128 error = copyout(&base, uap->basep, sizeof(long));
4133 kern_getdirentries(struct thread *td, int fd, char *buf, u_int count,
4145 if (count > INT_MAX)
4147 if ((error = getvnode(td->td_proc->p_fd, fd, CAP_READ | CAP_SEEK,
4150 if ((fp->f_flag & FREAD) == 0) {
4156 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4157 if (vp->v_type != VDIR) {
4158 VFS_UNLOCK_GIANT(vfslocked);
4162 aiov.iov_base = buf;
4163 aiov.iov_len = count;
4164 auio.uio_iov = &aiov;
4165 auio.uio_iovcnt = 1;
4166 auio.uio_rw = UIO_READ;
4167 auio.uio_segflg = UIO_USERSPACE;
4169 auio.uio_resid = count;
4170 vn_lock(vp, LK_SHARED | LK_RETRY);
4171 AUDIT_ARG_VNODE1(vp);
4172 loff = auio.uio_offset = fp->f_offset;
4174 error = mac_vnode_check_readdir(td->td_ucred, vp);
4177 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4179 fp->f_offset = auio.uio_offset;
4182 VFS_UNLOCK_GIANT(vfslocked);
4185 if (count == auio.uio_resid &&
4186 (vp->v_vflag & VV_ROOT) &&
4187 (vp->v_mount->mnt_flag & MNT_UNION)) {
4188 struct vnode *tvp = vp;
4189 vp = vp->v_mount->mnt_vnodecovered;
4195 VFS_UNLOCK_GIANT(vfslocked);
4199 VFS_UNLOCK_GIANT(vfslocked);
4201 td->td_retval[0] = count - auio.uio_resid;
4207 #ifndef _SYS_SYSPROTO_H_
4208 struct getdents_args {
4215 sys_getdents(td, uap)
4217 register struct getdents_args /* {
4223 struct getdirentries_args ap;
4226 ap.count = uap->count;
4228 return (sys_getdirentries(td, &ap));
4232 * Set the mode mask for creation of filesystem nodes.
4234 #ifndef _SYS_SYSPROTO_H_
4242 struct umask_args /* {
4246 register struct filedesc *fdp;
4248 FILEDESC_XLOCK(td->td_proc->p_fd);
4249 fdp = td->td_proc->p_fd;
4250 td->td_retval[0] = fdp->fd_cmask;
4251 fdp->fd_cmask = uap->newmask & ALLPERMS;
4252 FILEDESC_XUNLOCK(td->td_proc->p_fd);
4257 * Void all references to file by ripping underlying filesystem away from
4260 #ifndef _SYS_SYSPROTO_H_
4261 struct revoke_args {
4268 register struct revoke_args /* {
4275 struct nameidata nd;
4278 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4279 UIO_USERSPACE, uap->path, td);
4280 if ((error = namei(&nd)) != 0)
4282 vfslocked = NDHASGIANT(&nd);
4284 NDFREE(&nd, NDF_ONLY_PNBUF);
4285 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4290 error = mac_vnode_check_revoke(td->td_ucred, vp);
4294 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4297 if (td->td_ucred->cr_uid != vattr.va_uid) {
4298 error = priv_check(td, PRIV_VFS_ADMIN);
4303 VOP_REVOKE(vp, REVOKEALL);
4306 VFS_UNLOCK_GIANT(vfslocked);
4311 * Convert a user file descriptor to a kernel file entry and check that, if it
4312 * is a capability, the correct rights are present. A reference on the file
4313 * entry is held upon returning.
4316 getvnode(struct filedesc *fdp, int fd, cap_rights_t rights,
4321 struct file *fp_fromcap;
4327 if ((fdp == NULL) || (fp = fget_unlocked(fdp, fd)) == NULL)
4331 * If the file descriptor is for a capability, test rights and use the
4332 * file descriptor referenced by the capability.
4334 error = cap_funwrap(fp, rights, &fp_fromcap);
4336 fdrop(fp, curthread);
4339 if (fp != fp_fromcap) {
4341 fdrop(fp, curthread);
4344 #endif /* CAPABILITIES */
4347 * The file could be not of the vnode type, or it may be not
4348 * yet fully initialized, in which case the f_vnode pointer
4349 * may be set, but f_ops is still badfileops. E.g.,
4350 * devfs_open() transiently create such situation to
4351 * facilitate csw d_fdopen().
4353 * Dupfdopen() handling in kern_openat() installs the
4354 * half-baked file into the process descriptor table, allowing
4355 * other thread to dereference it. Guard against the race by
4358 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) {
4359 fdrop(fp, curthread);
4368 * Get an (NFS) file handle.
4370 #ifndef _SYS_SYSPROTO_H_
4371 struct lgetfh_args {
4379 register struct lgetfh_args *uap;
4381 struct nameidata nd;
4383 register struct vnode *vp;
4387 error = priv_check(td, PRIV_VFS_GETFH);
4390 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4391 UIO_USERSPACE, uap->fname, td);
4395 vfslocked = NDHASGIANT(&nd);
4396 NDFREE(&nd, NDF_ONLY_PNBUF);
4398 bzero(&fh, sizeof(fh));
4399 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4400 error = VOP_VPTOFH(vp, &fh.fh_fid);
4402 VFS_UNLOCK_GIANT(vfslocked);
4405 error = copyout(&fh, uap->fhp, sizeof (fh));
4409 #ifndef _SYS_SYSPROTO_H_
4418 register struct getfh_args *uap;
4420 struct nameidata nd;
4422 register struct vnode *vp;
4426 error = priv_check(td, PRIV_VFS_GETFH);
4429 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
4430 UIO_USERSPACE, uap->fname, td);
4434 vfslocked = NDHASGIANT(&nd);
4435 NDFREE(&nd, NDF_ONLY_PNBUF);
4437 bzero(&fh, sizeof(fh));
4438 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4439 error = VOP_VPTOFH(vp, &fh.fh_fid);
4441 VFS_UNLOCK_GIANT(vfslocked);
4444 error = copyout(&fh, uap->fhp, sizeof (fh));
4449 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4452 * warning: do not remove the priv_check() call or this becomes one giant
4455 #ifndef _SYS_SYSPROTO_H_
4456 struct fhopen_args {
4457 const struct fhandle *u_fhp;
4464 struct fhopen_args /* {
4465 const struct fhandle *u_fhp;
4469 struct proc *p = td->td_proc;
4474 struct vattr *vap = &vat;
4477 register struct filedesc *fdp = p->p_fd;
4478 int fmode, error, type;
4484 error = priv_check(td, PRIV_VFS_FHOPEN);
4487 fmode = FFLAGS(uap->flags);
4488 /* why not allow a non-read/write open for our lockd? */
4489 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4491 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4494 /* find the mount point */
4495 mp = vfs_busyfs(&fhp.fh_fsid);
4498 vfslocked = VFS_LOCK_GIANT(mp);
4499 /* now give me my vnode, it gets returned to me locked */
4500 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4505 * from now on we have to make sure not
4506 * to forget about the vnode
4507 * any error that causes an abort must vput(vp)
4508 * just set error = err and 'goto bad;'.
4514 if (vp->v_type == VLNK) {
4518 if (vp->v_type == VSOCK) {
4522 if (vp->v_type != VDIR && fmode & O_DIRECTORY) {
4527 if (fmode & (FWRITE | O_TRUNC)) {
4528 if (vp->v_type == VDIR) {
4532 error = vn_writechk(vp);
4539 if ((fmode & O_APPEND) && (fmode & FWRITE))
4542 error = mac_vnode_check_open(td->td_ucred, vp, accmode);
4547 error = VOP_ACCESS(vp, accmode, td->td_ucred, td);
4551 if (fmode & O_TRUNC) {
4553 VOP_UNLOCK(vp, 0); /* XXX */
4554 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4559 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4563 * We don't yet have fp->f_cred, so use td->td_ucred, which
4566 error = mac_vnode_check_write(td->td_ucred, td->td_ucred, vp);
4571 error = VOP_SETATTR(vp, vap, td->td_ucred);
4575 vn_finished_write(mp);
4579 error = VOP_OPEN(vp, fmode, td->td_ucred, td, NULL);
4587 * end of vn_open code
4590 if ((error = falloc(td, &nfp, &indx, fmode)) != 0) {
4595 /* An extra reference on `nfp' has been held for us by falloc(). */
4598 finit(nfp, fmode & FMASK, DTYPE_VNODE, vp, &vnops);
4599 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4600 lf.l_whence = SEEK_SET;
4603 if (fmode & O_EXLOCK)
4604 lf.l_type = F_WRLCK;
4606 lf.l_type = F_RDLCK;
4608 if ((fmode & FNONBLOCK) == 0)
4611 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4614 * The lock request failed. Normally close the
4615 * descriptor but handle the case where someone might
4616 * have dup()d or close()d it when we weren't looking.
4618 fdclose(fdp, fp, indx, td);
4621 * release our private reference
4626 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4627 atomic_set_int(&fp->f_flag, FHASLOCK);
4632 VFS_UNLOCK_GIANT(vfslocked);
4633 td->td_retval[0] = indx;
4639 VFS_UNLOCK_GIANT(vfslocked);
4644 * Stat an (NFS) file handle.
4646 #ifndef _SYS_SYSPROTO_H_
4647 struct fhstat_args {
4648 struct fhandle *u_fhp;
4655 register struct fhstat_args /* {
4656 struct fhandle *u_fhp;
4667 error = priv_check(td, PRIV_VFS_FHSTAT);
4670 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4673 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4675 vfslocked = VFS_LOCK_GIANT(mp);
4676 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4679 VFS_UNLOCK_GIANT(vfslocked);
4682 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4684 VFS_UNLOCK_GIANT(vfslocked);
4687 error = copyout(&sb, uap->sb, sizeof(sb));
4692 * Implement fstatfs() for (NFS) file handles.
4694 #ifndef _SYS_SYSPROTO_H_
4695 struct fhstatfs_args {
4696 struct fhandle *u_fhp;
4701 sys_fhstatfs(td, uap)
4703 struct fhstatfs_args /* {
4704 struct fhandle *u_fhp;
4712 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4715 error = kern_fhstatfs(td, fh, &sf);
4718 return (copyout(&sf, uap->buf, sizeof(sf)));
4722 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4730 error = priv_check(td, PRIV_VFS_FHSTATFS);
4733 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4735 vfslocked = VFS_LOCK_GIANT(mp);
4736 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4739 VFS_UNLOCK_GIANT(vfslocked);
4743 error = prison_canseemount(td->td_ucred, mp);
4747 error = mac_mount_check_stat(td->td_ucred, mp);
4752 * Set these in case the underlying filesystem fails to do so.
4755 sp->f_version = STATFS_VERSION;
4756 sp->f_namemax = NAME_MAX;
4757 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4758 error = VFS_STATFS(mp, sp);
4763 VFS_UNLOCK_GIANT(vfslocked);
4768 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
4773 off_t olen, ooffset;
4774 int error, vfslocked;
4778 error = fget(td, fd, CAP_WRITE, &fp);
4782 switch (fp->f_type) {
4793 if ((fp->f_flag & FWRITE) == 0) {
4798 if (vp->v_type != VREG) {
4802 if (offset < 0 || len <= 0) {
4806 /* Check for wrap. */
4807 if (offset > OFF_MAX - len) {
4812 /* Allocating blocks may take a long time, so iterate. */
4818 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
4820 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4822 VFS_UNLOCK_GIANT(vfslocked);
4825 error = vn_lock(vp, LK_EXCLUSIVE);
4827 vn_finished_write(mp);
4828 VFS_UNLOCK_GIANT(vfslocked);
4832 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp);
4835 error = VOP_ALLOCATE(vp, &offset, &len);
4837 vn_finished_write(mp);
4838 VFS_UNLOCK_GIANT(vfslocked);
4840 if (olen + ooffset != offset + len) {
4841 panic("offset + len changed from %jx/%jx to %jx/%jx",
4842 ooffset, olen, offset, len);
4844 if (error != 0 || len == 0)
4846 KASSERT(olen > len, ("Iteration did not make progress?"));
4856 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
4859 return (kern_posix_fallocate(td, uap->fd, uap->offset, uap->len));