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"
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/sysent.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/mutex.h>
52 #include <sys/sysproto.h>
53 #include <sys/namei.h>
54 #include <sys/filedesc.h>
55 #include <sys/kernel.h>
56 #include <sys/fcntl.h>
58 #include <sys/limits.h>
59 #include <sys/linker.h>
62 #include <sys/unistd.h>
63 #include <sys/vnode.h>
65 #include <sys/dirent.h>
66 #include <sys/extattr.h>
68 #include <sys/syscallsubr.h>
69 #include <sys/sysctl.h>
71 #include <machine/stdarg.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_page.h>
78 static int chroot_refuse_vdir_fds(struct filedesc *fdp);
79 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
80 static int setfown(struct thread *td, struct vnode *, uid_t, gid_t);
81 static int setfmode(struct thread *td, struct vnode *, int);
82 static int setfflags(struct thread *td, struct vnode *, int);
83 static int setutimes(struct thread *td, struct vnode *,
84 const struct timespec *, int, int);
85 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
88 static int extattr_list_vp(struct vnode *vp, int attrnamespace, void *data,
89 size_t nbytes, struct thread *td);
91 int (*union_dircheckp)(struct thread *td, struct vnode **, struct file *);
94 * The module initialization routine for POSIX asynchronous I/O will
95 * set this to the version of AIO that it implements. (Zero means
96 * that it is not implemented.) This value is used here by pathconf()
97 * and in kern_descrip.c by fpathconf().
102 * Sync each mounted filesystem.
104 #ifndef _SYS_SYSPROTO_H_
111 static int syncprt = 0;
112 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
119 struct sync_args *uap;
121 struct mount *mp, *nmp;
125 mtx_lock(&mountlist_mtx);
126 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
127 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
128 nmp = TAILQ_NEXT(mp, mnt_list);
131 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
132 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
133 asyncflag = mp->mnt_flag & MNT_ASYNC;
134 mp->mnt_flag &= ~MNT_ASYNC;
135 vfs_msync(mp, MNT_NOWAIT);
136 VFS_SYNC(mp, MNT_NOWAIT, td);
137 mp->mnt_flag |= asyncflag;
138 vn_finished_write(mp);
140 mtx_lock(&mountlist_mtx);
141 nmp = TAILQ_NEXT(mp, mnt_list);
144 mtx_unlock(&mountlist_mtx);
147 * XXX don't call vfs_bufstats() yet because that routine
148 * was not imported in the Lite2 merge.
153 #endif /* DIAGNOSTIC */
159 /* XXX PRISON: could be per prison flag */
160 static int prison_quotas;
162 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
166 * Change filesystem quotas.
170 #ifndef _SYS_SYSPROTO_H_
171 struct quotactl_args {
181 register struct quotactl_args /* {
188 struct mount *mp, *vmp;
192 if (jailed(td->td_ucred) && !prison_quotas)
195 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, td);
196 if ((error = namei(&nd)) != 0) {
200 NDFREE(&nd, NDF_ONLY_PNBUF);
201 error = vn_start_write(nd.ni_vp, &vmp, V_WAIT | PCATCH);
202 mp = nd.ni_vp->v_mount;
208 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, td);
209 vn_finished_write(vmp);
215 * Get filesystem statistics.
217 #ifndef _SYS_SYSPROTO_H_
226 register struct statfs_args /* {
234 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
236 error = copyout(&sf, uap->buf, sizeof(sf));
241 kern_statfs(struct thread *td, char *path, enum uio_seg pathseg,
245 struct statfs *sp, sb;
250 NDINIT(&nd, LOOKUP, FOLLOW, pathseg, path, td);
256 mp = nd.ni_vp->v_mount;
258 NDFREE(&nd, NDF_ONLY_PNBUF);
261 error = mac_check_mount_stat(td->td_ucred, mp);
268 * Set these in case the underlying filesystem fails to do so.
270 sp->f_version = STATFS_VERSION;
271 sp->f_namemax = NAME_MAX;
272 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
273 error = VFS_STATFS(mp, sp, td);
279 bcopy(sp, &sb, sizeof(sb));
280 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
281 prison_enforce_statfs(td->td_ucred, mp, &sb);
290 * Get filesystem statistics.
292 #ifndef _SYS_SYSPROTO_H_
293 struct fstatfs_args {
301 register struct fstatfs_args /* {
309 error = kern_fstatfs(td, uap->fd, &sf);
311 error = copyout(&sf, uap->buf, sizeof(sf));
316 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
320 struct statfs *sp, sb;
323 error = getvnode(td->td_proc->p_fd, fd, &fp);
326 mp = fp->f_vnode->v_mount;
332 error = mac_check_mount_stat(td->td_ucred, mp);
340 * Set these in case the underlying filesystem fails to do so.
342 sp->f_version = STATFS_VERSION;
343 sp->f_namemax = NAME_MAX;
344 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
345 error = VFS_STATFS(mp, sp, td);
351 bcopy(sp, &sb, sizeof(sb));
352 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
353 prison_enforce_statfs(td->td_ucred, mp, &sb);
362 * Get statistics on all filesystems.
364 #ifndef _SYS_SYSPROTO_H_
365 struct getfsstat_args {
374 register struct getfsstat_args /* {
381 return (kern_getfsstat(td, &uap->buf, uap->bufsize, UIO_USERSPACE,
386 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
387 * The caller is responsible for freeing memory which will be allocated
391 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
392 enum uio_seg bufseg, int flags)
394 struct mount *mp, *nmp;
395 struct statfs *sfsp, *sp, sb;
396 size_t count, maxcount;
399 maxcount = bufsize / sizeof(struct statfs);
402 else if (bufseg == UIO_USERSPACE)
404 else /* if (bufseg == UIO_SYSSPACE) */ {
406 mtx_lock(&mountlist_mtx);
407 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
410 mtx_unlock(&mountlist_mtx);
411 if (maxcount > count)
413 sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_TEMP,
418 mtx_lock(&mountlist_mtx);
419 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
420 if (prison_canseemount(td->td_ucred, mp) != 0) {
421 nmp = TAILQ_NEXT(mp, mnt_list);
425 if (mac_check_mount_stat(td->td_ucred, mp) != 0) {
426 nmp = TAILQ_NEXT(mp, mnt_list);
430 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
431 nmp = TAILQ_NEXT(mp, mnt_list);
434 if (sfsp && count < maxcount) {
437 * Set these in case the underlying filesystem
440 sp->f_version = STATFS_VERSION;
441 sp->f_namemax = NAME_MAX;
442 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
444 * If MNT_NOWAIT or MNT_LAZY is specified, do not
445 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
446 * overrides MNT_WAIT.
448 if (((flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
449 (flags & MNT_WAIT)) &&
450 (error = VFS_STATFS(mp, sp, td))) {
451 mtx_lock(&mountlist_mtx);
452 nmp = TAILQ_NEXT(mp, mnt_list);
457 bcopy(sp, &sb, sizeof(sb));
458 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
459 prison_enforce_statfs(td->td_ucred, mp, &sb);
462 if (bufseg == UIO_SYSSPACE)
463 bcopy(sp, sfsp, sizeof(*sp));
464 else /* if (bufseg == UIO_USERSPACE) */ {
465 error = copyout(sp, sfsp, sizeof(*sp));
475 mtx_lock(&mountlist_mtx);
476 nmp = TAILQ_NEXT(mp, mnt_list);
479 mtx_unlock(&mountlist_mtx);
481 if (sfsp && count > maxcount)
482 td->td_retval[0] = maxcount;
484 td->td_retval[0] = count;
488 #ifdef COMPAT_FREEBSD4
490 * Get old format filesystem statistics.
492 static void cvtstatfs(struct statfs *, struct ostatfs *);
494 #ifndef _SYS_SYSPROTO_H_
495 struct freebsd4_statfs_args {
501 freebsd4_statfs(td, uap)
503 struct freebsd4_statfs_args /* {
512 error = kern_statfs(td, uap->path, UIO_USERSPACE, &sf);
515 cvtstatfs(&sf, &osb);
516 return (copyout(&osb, uap->buf, sizeof(osb)));
520 * Get filesystem statistics.
522 #ifndef _SYS_SYSPROTO_H_
523 struct freebsd4_fstatfs_args {
529 freebsd4_fstatfs(td, uap)
531 struct freebsd4_fstatfs_args /* {
540 error = kern_fstatfs(td, uap->fd, &sf);
543 cvtstatfs(&sf, &osb);
544 return (copyout(&osb, uap->buf, sizeof(osb)));
548 * Get statistics on all filesystems.
550 #ifndef _SYS_SYSPROTO_H_
551 struct freebsd4_getfsstat_args {
558 freebsd4_getfsstat(td, uap)
560 register struct freebsd4_getfsstat_args /* {
566 struct statfs *buf, *sp;
571 count = uap->bufsize / sizeof(struct ostatfs);
572 size = count * sizeof(struct statfs);
573 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
575 count = td->td_retval[0];
577 while (count > 0 && error == 0) {
579 error = copyout(&osb, uap->buf, sizeof(osb));
590 * Implement fstatfs() for (NFS) file handles.
592 #ifndef _SYS_SYSPROTO_H_
593 struct freebsd4_fhstatfs_args {
594 struct fhandle *u_fhp;
599 freebsd4_fhstatfs(td, uap)
601 struct freebsd4_fhstatfs_args /* {
602 struct fhandle *u_fhp;
611 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
614 error = kern_fhstatfs(td, fh, &sf);
617 cvtstatfs(&sf, &osb);
618 return (copyout(&osb, uap->buf, sizeof(osb)));
622 * Convert a new format statfs structure to an old format statfs structure.
630 bzero(osp, sizeof(*osp));
631 osp->f_bsize = MIN(nsp->f_bsize, LONG_MAX);
632 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
633 osp->f_blocks = MIN(nsp->f_blocks, LONG_MAX);
634 osp->f_bfree = MIN(nsp->f_bfree, LONG_MAX);
635 osp->f_bavail = MIN(nsp->f_bavail, LONG_MAX);
636 osp->f_files = MIN(nsp->f_files, LONG_MAX);
637 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
638 osp->f_owner = nsp->f_owner;
639 osp->f_type = nsp->f_type;
640 osp->f_flags = nsp->f_flags;
641 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
642 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
643 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
644 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
645 bcopy(nsp->f_fstypename, osp->f_fstypename,
646 MIN(MFSNAMELEN, OMNAMELEN));
647 bcopy(nsp->f_mntonname, osp->f_mntonname,
648 MIN(MFSNAMELEN, OMNAMELEN));
649 bcopy(nsp->f_mntfromname, osp->f_mntfromname,
650 MIN(MFSNAMELEN, OMNAMELEN));
651 osp->f_fsid = nsp->f_fsid;
653 #endif /* COMPAT_FREEBSD4 */
656 * Change current working directory to a given file descriptor.
658 #ifndef _SYS_SYSPROTO_H_
666 struct fchdir_args /* {
670 register struct filedesc *fdp = td->td_proc->p_fd;
671 struct vnode *vp, *tdp, *vpold;
677 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
682 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
683 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
684 if (vp->v_type != VDIR)
687 else if ((error = mac_check_vnode_chdir(td->td_ucred, vp)) != 0) {
691 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
692 while (!error && (mp = vp->v_mountedhere) != NULL) {
694 if (vfs_busy(mp, 0, 0, td))
696 tvfslocked = VFS_LOCK_GIANT(mp);
697 error = VFS_ROOT(mp, LK_EXCLUSIVE, &tdp, td);
700 VFS_UNLOCK_GIANT(tvfslocked);
704 VFS_UNLOCK_GIANT(vfslocked);
706 vfslocked = tvfslocked;
710 VFS_UNLOCK_GIANT(vfslocked);
713 VOP_UNLOCK(vp, 0, td);
714 FILEDESC_LOCK_FAST(fdp);
715 vpold = fdp->fd_cdir;
717 FILEDESC_UNLOCK_FAST(fdp);
719 VFS_UNLOCK_GIANT(vfslocked);
724 * Change current working directory (``.'').
726 #ifndef _SYS_SYSPROTO_H_
734 struct chdir_args /* {
739 return (kern_chdir(td, uap->path, UIO_USERSPACE));
743 kern_chdir(struct thread *td, char *path, enum uio_seg pathseg)
745 register struct filedesc *fdp = td->td_proc->p_fd;
751 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, pathseg, path, td);
752 if ((error = namei(&nd)) != 0)
754 vfslocked = NDHASGIANT(&nd);
755 if ((error = change_dir(nd.ni_vp, td)) != 0) {
757 VFS_UNLOCK_GIANT(vfslocked);
758 NDFREE(&nd, NDF_ONLY_PNBUF);
761 VOP_UNLOCK(nd.ni_vp, 0, td);
762 NDFREE(&nd, NDF_ONLY_PNBUF);
763 FILEDESC_LOCK_FAST(fdp);
765 fdp->fd_cdir = nd.ni_vp;
766 FILEDESC_UNLOCK_FAST(fdp);
768 VFS_UNLOCK_GIANT(vfslocked);
773 * Helper function for raised chroot(2) security function: Refuse if
774 * any filedescriptors are open directories.
777 chroot_refuse_vdir_fds(fdp)
778 struct filedesc *fdp;
784 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
785 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
786 fp = fget_locked(fdp, fd);
789 if (fp->f_type == DTYPE_VNODE) {
791 if (vp->v_type == VDIR)
799 * This sysctl determines if we will allow a process to chroot(2) if it
800 * has a directory open:
801 * 0: disallowed for all processes.
802 * 1: allowed for processes that were not already chroot(2)'ed.
803 * 2: allowed for all processes.
806 static int chroot_allow_open_directories = 1;
808 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
809 &chroot_allow_open_directories, 0, "");
812 * Change notion of root (``/'') directory.
814 #ifndef _SYS_SYSPROTO_H_
822 struct chroot_args /* {
830 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
833 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE,
834 UIO_USERSPACE, uap->path, td);
838 vfslocked = NDHASGIANT(&nd);
839 if ((error = change_dir(nd.ni_vp, td)) != 0)
842 if ((error = mac_check_vnode_chroot(td->td_ucred, nd.ni_vp)))
845 VOP_UNLOCK(nd.ni_vp, 0, td);
846 error = change_root(nd.ni_vp, td);
848 VFS_UNLOCK_GIANT(vfslocked);
849 NDFREE(&nd, NDF_ONLY_PNBUF);
853 VFS_UNLOCK_GIANT(vfslocked);
855 NDFREE(&nd, NDF_ONLY_PNBUF);
860 * Common routine for chroot and chdir. Callers must provide a locked vnode
870 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
871 if (vp->v_type != VDIR)
874 error = mac_check_vnode_chdir(td->td_ucred, vp);
878 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
883 * Common routine for kern_chroot() and jail_attach(). The caller is
884 * responsible for invoking suser() and mac_check_chroot() to authorize this
892 struct filedesc *fdp;
896 VFS_ASSERT_GIANT(vp->v_mount);
897 fdp = td->td_proc->p_fd;
899 if (chroot_allow_open_directories == 0 ||
900 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
901 error = chroot_refuse_vdir_fds(fdp);
903 FILEDESC_UNLOCK(fdp);
907 oldvp = fdp->fd_rdir;
914 FILEDESC_UNLOCK(fdp);
920 * Check permissions, allocate an open file structure,
921 * and call the device open routine if any.
925 #ifndef _SYS_SYSPROTO_H_
935 register struct open_args /* {
943 error = kern_open(td, uap->path, UIO_USERSPACE, uap->flags, uap->mode);
944 if (mtx_owned(&Giant))
945 printf("open: %s: %d\n", uap->path, error);
950 kern_open(struct thread *td, char *path, enum uio_seg pathseg, int flags,
953 struct proc *p = td->td_proc;
954 struct filedesc *fdp = p->p_fd;
961 int type, indx, error;
966 if ((flags & O_ACCMODE) == O_ACCMODE)
968 flags = FFLAGS(flags);
969 error = falloc(td, &nfp, &indx);
972 /* An extra reference on `nfp' has been held for us by falloc(). */
974 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
975 NDINIT(&nd, LOOKUP, FOLLOW, pathseg, path, td);
976 td->td_dupfd = -1; /* XXX check for fdopen */
977 error = vn_open(&nd, &flags, cmode, indx);
980 * If the vn_open replaced the method vector, something
981 * wonderous happened deep below and we just pass it up
982 * pretending we know what we do.
984 if (error == ENXIO && fp->f_ops != &badfileops) {
986 td->td_retval[0] = indx;
991 * release our own reference
996 * handle special fdopen() case. bleh. dupfdopen() is
997 * responsible for dropping the old contents of ofiles[indx]
1000 if ((error == ENODEV || error == ENXIO) &&
1001 td->td_dupfd >= 0 && /* XXX from fdopen */
1003 dupfdopen(td, fdp, indx, td->td_dupfd, flags, error)) == 0) {
1004 td->td_retval[0] = indx;
1008 * Clean up the descriptor, but only if another thread hadn't
1009 * replaced or closed it.
1011 fdclose(fdp, fp, indx, td);
1013 if (error == ERESTART)
1018 vfslocked = NDHASGIANT(&nd);
1019 NDFREE(&nd, NDF_ONLY_PNBUF);
1023 * There should be 2 references on the file, one from the descriptor
1024 * table, and one for us.
1026 * Handle the case where someone closed the file (via its file
1027 * descriptor) while we were blocked. The end result should look
1028 * like opening the file succeeded but it was immediately closed.
1029 * We call vn_close() manually because we haven't yet hooked up
1030 * the various 'struct file' fields.
1034 if (fp->f_count == 1) {
1036 KASSERT(fdp->fd_ofiles[indx] != fp,
1037 ("Open file descriptor lost all refs"));
1039 FILEDESC_UNLOCK(fdp);
1040 VOP_UNLOCK(vp, 0, td);
1041 vn_close(vp, flags & FMASK, fp->f_cred, td);
1042 VFS_UNLOCK_GIANT(vfslocked);
1044 td->td_retval[0] = indx;
1048 if (fp->f_data == NULL)
1050 fp->f_flag = flags & FMASK;
1051 if (fp->f_ops == &badfileops)
1054 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1056 FILEDESC_UNLOCK(fdp);
1058 VOP_UNLOCK(vp, 0, td);
1059 if (flags & (O_EXLOCK | O_SHLOCK)) {
1060 lf.l_whence = SEEK_SET;
1063 if (flags & O_EXLOCK)
1064 lf.l_type = F_WRLCK;
1066 lf.l_type = F_RDLCK;
1068 if ((flags & FNONBLOCK) == 0)
1070 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1073 fp->f_flag |= FHASLOCK;
1075 if (flags & O_TRUNC) {
1076 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
1078 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1081 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1083 error = mac_check_vnode_write(td->td_ucred, fp->f_cred, vp);
1086 error = VOP_SETATTR(vp, &vat, td->td_ucred, td);
1087 VOP_UNLOCK(vp, 0, td);
1088 vn_finished_write(mp);
1092 VFS_UNLOCK_GIANT(vfslocked);
1094 * Release our private reference, leaving the one associated with
1095 * the descriptor table intact.
1098 td->td_retval[0] = indx;
1101 VFS_UNLOCK_GIANT(vfslocked);
1102 fdclose(fdp, fp, indx, td);
1113 #ifndef _SYS_SYSPROTO_H_
1114 struct ocreat_args {
1122 register struct ocreat_args /* {
1128 return (kern_open(td, uap->path, UIO_USERSPACE,
1129 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1131 #endif /* COMPAT_43 */
1134 * Create a special file.
1136 #ifndef _SYS_SYSPROTO_H_
1146 register struct mknod_args /* {
1153 return (kern_mknod(td, uap->path, UIO_USERSPACE, uap->mode, uap->dev));
1157 kern_mknod(struct thread *td, char *path, enum uio_seg pathseg, int mode,
1165 struct nameidata nd;
1168 switch (mode & S_IFMT) {
1174 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
1181 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE, pathseg, path, td);
1182 if ((error = namei(&nd)) != 0)
1184 vfslocked = NDHASGIANT(&nd);
1187 NDFREE(&nd, NDF_ONLY_PNBUF);
1189 if (vp == nd.ni_dvp)
1193 VFS_UNLOCK_GIANT(vfslocked);
1197 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
1198 vattr.va_mode = (mode & ALLPERMS) &
1199 ~td->td_proc->p_fd->fd_cmask;
1200 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
1201 vattr.va_rdev = dev;
1204 switch (mode & S_IFMT) {
1205 case S_IFMT: /* used by badsect to flag bad sectors */
1206 vattr.va_type = VBAD;
1209 vattr.va_type = VCHR;
1212 vattr.va_type = VBLK;
1222 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1223 NDFREE(&nd, NDF_ONLY_PNBUF);
1225 VFS_UNLOCK_GIANT(vfslocked);
1226 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1231 if (error == 0 && !whiteout)
1232 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp,
1233 &nd.ni_cnd, &vattr);
1236 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1238 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1240 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1241 &nd.ni_cnd, &vattr);
1246 NDFREE(&nd, NDF_ONLY_PNBUF);
1248 vn_finished_write(mp);
1249 VFS_UNLOCK_GIANT(vfslocked);
1254 * Create a named pipe.
1256 #ifndef _SYS_SYSPROTO_H_
1257 struct mkfifo_args {
1265 register struct mkfifo_args /* {
1271 return (kern_mkfifo(td, uap->path, UIO_USERSPACE, uap->mode));
1275 kern_mkfifo(struct thread *td, char *path, enum uio_seg pathseg, int mode)
1280 struct nameidata nd;
1285 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE, pathseg, path, td);
1286 if ((error = namei(&nd)) != 0)
1288 vfslocked = NDHASGIANT(&nd);
1289 if (nd.ni_vp != NULL) {
1290 NDFREE(&nd, NDF_ONLY_PNBUF);
1292 if (nd.ni_vp == nd.ni_dvp)
1296 VFS_UNLOCK_GIANT(vfslocked);
1299 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1300 NDFREE(&nd, NDF_ONLY_PNBUF);
1302 VFS_UNLOCK_GIANT(vfslocked);
1303 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1308 vattr.va_type = VFIFO;
1309 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
1310 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask;
1311 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
1313 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1318 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1319 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1326 vn_finished_write(mp);
1327 VFS_UNLOCK_GIANT(vfslocked);
1328 NDFREE(&nd, NDF_ONLY_PNBUF);
1333 * Make a hard file link.
1335 #ifndef _SYS_SYSPROTO_H_
1344 register struct link_args /* {
1351 error = kern_link(td, uap->path, uap->link, UIO_USERSPACE);
1355 SYSCTL_DECL(_security_bsd);
1357 static int hardlink_check_uid = 0;
1358 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1359 &hardlink_check_uid, 0,
1360 "Unprivileged processes cannot create hard links to files owned by other "
1362 static int hardlink_check_gid = 0;
1363 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1364 &hardlink_check_gid, 0,
1365 "Unprivileged processes cannot create hard links to files owned by other "
1369 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1374 if (suser_cred(cred, SUSER_ALLOWJAIL) == 0)
1377 if (!hardlink_check_uid && !hardlink_check_gid)
1380 error = VOP_GETATTR(vp, &va, cred, td);
1384 if (hardlink_check_uid) {
1385 if (cred->cr_uid != va.va_uid)
1389 if (hardlink_check_gid) {
1390 if (!groupmember(va.va_gid, cred))
1398 kern_link(struct thread *td, char *path, char *link, enum uio_seg segflg)
1402 struct nameidata nd;
1408 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, segflg, path, td);
1409 if ((error = namei(&nd)) != 0)
1411 vfslocked = NDHASGIANT(&nd);
1412 NDFREE(&nd, NDF_ONLY_PNBUF);
1414 if (vp->v_type == VDIR) {
1416 VFS_UNLOCK_GIANT(vfslocked);
1417 return (EPERM); /* POSIX */
1419 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
1421 VFS_UNLOCK_GIANT(vfslocked);
1424 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE, segflg, link, td);
1425 if ((error = namei(&nd)) == 0) {
1426 lvfslocked = NDHASGIANT(&nd);
1427 if (nd.ni_vp != NULL) {
1429 if (nd.ni_dvp == nd.ni_vp)
1434 } else if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td))
1436 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1437 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
1438 error = can_hardlink(vp, td, td->td_ucred);
1441 error = mac_check_vnode_link(td->td_ucred,
1442 nd.ni_dvp, vp, &nd.ni_cnd);
1445 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1446 VOP_UNLOCK(vp, 0, td);
1449 NDFREE(&nd, NDF_ONLY_PNBUF);
1450 VFS_UNLOCK_GIANT(lvfslocked);
1453 vn_finished_write(mp);
1454 VFS_UNLOCK_GIANT(vfslocked);
1459 * Make a symbolic link.
1461 #ifndef _SYS_SYSPROTO_H_
1462 struct symlink_args {
1470 register struct symlink_args /* {
1476 return (kern_symlink(td, uap->path, uap->link, UIO_USERSPACE));
1480 kern_symlink(struct thread *td, char *path, char *link, enum uio_seg segflg)
1486 struct nameidata nd;
1489 if (segflg == UIO_SYSSPACE) {
1492 syspath = uma_zalloc(namei_zone, M_WAITOK);
1493 if ((error = copyinstr(path, syspath, MAXPATHLEN, NULL)) != 0)
1498 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE,
1500 if ((error = namei(&nd)) != 0)
1502 vfslocked = NDHASGIANT(&nd);
1504 NDFREE(&nd, NDF_ONLY_PNBUF);
1506 if (nd.ni_vp == nd.ni_dvp)
1510 VFS_UNLOCK_GIANT(vfslocked);
1514 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1515 NDFREE(&nd, NDF_ONLY_PNBUF);
1517 VFS_UNLOCK_GIANT(vfslocked);
1518 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1523 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
1524 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask;
1525 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
1527 vattr.va_type = VLNK;
1528 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1533 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1534 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1540 NDFREE(&nd, NDF_ONLY_PNBUF);
1542 vn_finished_write(mp);
1543 VFS_UNLOCK_GIANT(vfslocked);
1545 if (segflg != UIO_SYSSPACE)
1546 uma_zfree(namei_zone, syspath);
1551 * Delete a whiteout from the filesystem.
1556 register struct undelete_args /* {
1562 struct nameidata nd;
1567 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | MPSAFE, UIO_USERSPACE,
1572 vfslocked = NDHASGIANT(&nd);
1574 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1575 NDFREE(&nd, NDF_ONLY_PNBUF);
1578 if (nd.ni_vp == nd.ni_dvp)
1582 VFS_UNLOCK_GIANT(vfslocked);
1585 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1586 NDFREE(&nd, NDF_ONLY_PNBUF);
1588 VFS_UNLOCK_GIANT(vfslocked);
1589 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
1593 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1594 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1595 NDFREE(&nd, NDF_ONLY_PNBUF);
1597 vn_finished_write(mp);
1598 VFS_UNLOCK_GIANT(vfslocked);
1603 * Delete a name from the filesystem.
1605 #ifndef _SYS_SYSPROTO_H_
1606 struct unlink_args {
1613 struct unlink_args /* {
1619 error = kern_unlink(td, uap->path, UIO_USERSPACE);
1624 kern_unlink(struct thread *td, char *path, enum uio_seg pathseg)
1629 struct nameidata nd;
1634 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE, pathseg, path, td);
1635 if ((error = namei(&nd)) != 0)
1637 vfslocked = NDHASGIANT(&nd);
1639 if (vp->v_type == VDIR)
1640 error = EPERM; /* POSIX */
1643 * The root of a mounted filesystem cannot be deleted.
1645 * XXX: can this only be a VDIR case?
1647 if (vp->v_vflag & VV_ROOT)
1651 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1652 NDFREE(&nd, NDF_ONLY_PNBUF);
1653 if (vp == nd.ni_dvp)
1658 VFS_UNLOCK_GIANT(vfslocked);
1659 if ((error = vn_start_write(NULL, &mp,
1660 V_XSLEEP | PCATCH)) != 0)
1665 error = mac_check_vnode_delete(td->td_ucred, nd.ni_dvp, vp,
1670 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
1671 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1675 vn_finished_write(mp);
1677 NDFREE(&nd, NDF_ONLY_PNBUF);
1678 if (vp == nd.ni_dvp)
1683 VFS_UNLOCK_GIANT(vfslocked);
1688 * Reposition read/write file offset.
1690 #ifndef _SYS_SYSPROTO_H_
1701 register struct lseek_args /* {
1708 struct ucred *cred = td->td_ucred;
1716 if ((error = fget(td, uap->fd, &fp)) != 0)
1718 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE)) {
1723 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1724 noneg = (vp->v_type != VCHR);
1725 offset = uap->offset;
1726 switch (uap->whence) {
1729 (fp->f_offset < 0 ||
1730 (offset > 0 && fp->f_offset > OFF_MAX - offset))) {
1734 offset += fp->f_offset;
1737 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1738 error = VOP_GETATTR(vp, &vattr, cred, td);
1739 VOP_UNLOCK(vp, 0, td);
1743 (vattr.va_size > OFF_MAX ||
1744 (offset > 0 && vattr.va_size > OFF_MAX - offset))) {
1748 offset += vattr.va_size;
1755 if (error == 0 && noneg && offset < 0)
1759 fp->f_offset = offset;
1760 *(off_t *)(td->td_retval) = fp->f_offset;
1763 VFS_UNLOCK_GIANT(vfslocked);
1767 #if defined(COMPAT_43)
1769 * Reposition read/write file offset.
1771 #ifndef _SYS_SYSPROTO_H_
1772 struct olseek_args {
1781 register struct olseek_args /* {
1787 struct lseek_args /* {
1796 nuap.offset = uap->offset;
1797 nuap.whence = uap->whence;
1798 error = lseek(td, &nuap);
1801 #endif /* COMPAT_43 */
1804 * Check access permissions using passed credentials.
1807 vn_access(vp, user_flags, cred, td)
1815 /* Flags == 0 means only check for existence. */
1819 if (user_flags & R_OK)
1821 if (user_flags & W_OK)
1823 if (user_flags & X_OK)
1826 error = mac_check_vnode_access(cred, vp, flags);
1830 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1831 error = VOP_ACCESS(vp, flags, cred, td);
1837 * Check access permissions using "real" credentials.
1839 #ifndef _SYS_SYSPROTO_H_
1840 struct access_args {
1848 register struct access_args /* {
1854 return (kern_access(td, uap->path, UIO_USERSPACE, uap->flags));
1858 kern_access(struct thread *td, char *path, enum uio_seg pathseg, int flags)
1860 struct ucred *cred, *tmpcred;
1861 register struct vnode *vp;
1862 struct nameidata nd;
1867 * Create and modify a temporary credential instead of one that
1868 * is potentially shared. This could also mess up socket
1869 * buffer accounting which can run in an interrupt context.
1871 cred = td->td_ucred;
1872 tmpcred = crdup(cred);
1873 tmpcred->cr_uid = cred->cr_ruid;
1874 tmpcred->cr_groups[0] = cred->cr_rgid;
1875 td->td_ucred = tmpcred;
1876 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, pathseg, path, td);
1877 if ((error = namei(&nd)) != 0)
1879 vfslocked = NDHASGIANT(&nd);
1882 error = vn_access(vp, flags, tmpcred, td);
1883 NDFREE(&nd, NDF_ONLY_PNBUF);
1885 VFS_UNLOCK_GIANT(vfslocked);
1887 td->td_ucred = cred;
1893 * Check access permissions using "effective" credentials.
1895 #ifndef _SYS_SYSPROTO_H_
1896 struct eaccess_args {
1904 register struct eaccess_args /* {
1909 struct nameidata nd;
1914 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, UIO_USERSPACE,
1916 if ((error = namei(&nd)) != 0)
1919 vfslocked = NDHASGIANT(&nd);
1920 error = vn_access(vp, uap->flags, td->td_ucred, td);
1921 NDFREE(&nd, NDF_ONLY_PNBUF);
1923 VFS_UNLOCK_GIANT(vfslocked);
1927 #if defined(COMPAT_43)
1929 * Get file status; this version follows links.
1931 #ifndef _SYS_SYSPROTO_H_
1940 register struct ostat_args /* {
1949 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1953 error = copyout(&osb, uap->ub, sizeof (osb));
1958 * Get file status; this version does not follow links.
1960 #ifndef _SYS_SYSPROTO_H_
1961 struct olstat_args {
1969 register struct olstat_args /* {
1978 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1982 error = copyout(&osb, uap->ub, sizeof (osb));
1987 * Convert from an old to a new stat structure.
1995 ost->st_dev = st->st_dev;
1996 ost->st_ino = st->st_ino;
1997 ost->st_mode = st->st_mode;
1998 ost->st_nlink = st->st_nlink;
1999 ost->st_uid = st->st_uid;
2000 ost->st_gid = st->st_gid;
2001 ost->st_rdev = st->st_rdev;
2002 if (st->st_size < (quad_t)1 << 32)
2003 ost->st_size = st->st_size;
2006 ost->st_atime = st->st_atime;
2007 ost->st_mtime = st->st_mtime;
2008 ost->st_ctime = st->st_ctime;
2009 ost->st_blksize = st->st_blksize;
2010 ost->st_blocks = st->st_blocks;
2011 ost->st_flags = st->st_flags;
2012 ost->st_gen = st->st_gen;
2014 #endif /* COMPAT_43 */
2017 * Get file status; this version follows links.
2019 #ifndef _SYS_SYSPROTO_H_
2028 register struct stat_args /* {
2036 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2038 error = copyout(&sb, uap->ub, sizeof (sb));
2043 kern_stat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2045 struct nameidata nd;
2047 int error, vfslocked;
2049 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | MPSAFE,
2051 if ((error = namei(&nd)) != 0)
2053 vfslocked = NDHASGIANT(&nd);
2054 error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
2055 NDFREE(&nd, NDF_ONLY_PNBUF);
2057 VFS_UNLOCK_GIANT(vfslocked);
2065 * Get file status; this version does not follow links.
2067 #ifndef _SYS_SYSPROTO_H_
2076 register struct lstat_args /* {
2084 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2086 error = copyout(&sb, uap->ub, sizeof (sb));
2091 kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, struct stat *sbp)
2095 struct nameidata nd;
2096 int error, vfslocked;
2098 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED | MPSAFE,
2100 if ((error = namei(&nd)) != 0)
2102 vfslocked = NDHASGIANT(&nd);
2104 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
2105 NDFREE(&nd, NDF_ONLY_PNBUF);
2107 VFS_UNLOCK_GIANT(vfslocked);
2115 * Implementation of the NetBSD [l]stat() functions.
2122 bzero(nsb, sizeof *nsb);
2123 nsb->st_dev = sb->st_dev;
2124 nsb->st_ino = sb->st_ino;
2125 nsb->st_mode = sb->st_mode;
2126 nsb->st_nlink = sb->st_nlink;
2127 nsb->st_uid = sb->st_uid;
2128 nsb->st_gid = sb->st_gid;
2129 nsb->st_rdev = sb->st_rdev;
2130 nsb->st_atimespec = sb->st_atimespec;
2131 nsb->st_mtimespec = sb->st_mtimespec;
2132 nsb->st_ctimespec = sb->st_ctimespec;
2133 nsb->st_size = sb->st_size;
2134 nsb->st_blocks = sb->st_blocks;
2135 nsb->st_blksize = sb->st_blksize;
2136 nsb->st_flags = sb->st_flags;
2137 nsb->st_gen = sb->st_gen;
2138 nsb->st_birthtimespec = sb->st_birthtimespec;
2141 #ifndef _SYS_SYSPROTO_H_
2150 register struct nstat_args /* {
2159 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
2162 cvtnstat(&sb, &nsb);
2163 error = copyout(&nsb, uap->ub, sizeof (nsb));
2168 * NetBSD lstat. Get file status; this version does not follow links.
2170 #ifndef _SYS_SYSPROTO_H_
2179 register struct nlstat_args /* {
2188 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
2191 cvtnstat(&sb, &nsb);
2192 error = copyout(&nsb, uap->ub, sizeof (nsb));
2197 * Get configurable pathname variables.
2199 #ifndef _SYS_SYSPROTO_H_
2200 struct pathconf_args {
2208 register struct pathconf_args /* {
2214 return (kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name));
2218 kern_pathconf(struct thread *td, char *path, enum uio_seg pathseg, int name)
2220 struct nameidata nd;
2221 int error, vfslocked;
2223 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, pathseg, path, td);
2224 if ((error = namei(&nd)) != 0)
2226 vfslocked = NDHASGIANT(&nd);
2227 NDFREE(&nd, NDF_ONLY_PNBUF);
2229 /* If asynchronous I/O is available, it works for all files. */
2230 if (name == _PC_ASYNC_IO)
2231 td->td_retval[0] = async_io_version;
2233 error = VOP_PATHCONF(nd.ni_vp, name, td->td_retval);
2235 VFS_UNLOCK_GIANT(vfslocked);
2240 * Return target name of a symbolic link.
2242 #ifndef _SYS_SYSPROTO_H_
2243 struct readlink_args {
2252 register struct readlink_args /* {
2259 return (kern_readlink(td, uap->path, UIO_USERSPACE, uap->buf,
2260 UIO_USERSPACE, uap->count));
2264 kern_readlink(struct thread *td, char *path, enum uio_seg pathseg, char *buf,
2265 enum uio_seg bufseg, int count)
2267 register struct vnode *vp;
2271 struct nameidata nd;
2274 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE, pathseg, path, td);
2275 if ((error = namei(&nd)) != 0)
2277 NDFREE(&nd, NDF_ONLY_PNBUF);
2278 vfslocked = NDHASGIANT(&nd);
2281 error = mac_check_vnode_readlink(td->td_ucred, vp);
2284 VFS_UNLOCK_GIANT(vfslocked);
2288 if (vp->v_type != VLNK)
2291 aiov.iov_base = buf;
2292 aiov.iov_len = count;
2293 auio.uio_iov = &aiov;
2294 auio.uio_iovcnt = 1;
2295 auio.uio_offset = 0;
2296 auio.uio_rw = UIO_READ;
2297 auio.uio_segflg = bufseg;
2299 auio.uio_resid = count;
2300 error = VOP_READLINK(vp, &auio, td->td_ucred);
2303 VFS_UNLOCK_GIANT(vfslocked);
2304 td->td_retval[0] = count - auio.uio_resid;
2309 * Common implementation code for chflags() and fchflags().
2312 setfflags(td, vp, flags)
2322 * Prevent non-root users from setting flags on devices. When
2323 * a device is reused, users can retain ownership of the device
2324 * if they are allowed to set flags and programs assume that
2325 * chown can't fail when done as root.
2327 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2328 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
2333 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2335 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2336 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2338 vattr.va_flags = flags;
2340 error = mac_check_vnode_setflags(td->td_ucred, vp, vattr.va_flags);
2343 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2344 VOP_UNLOCK(vp, 0, td);
2345 vn_finished_write(mp);
2350 * Change flags of a file given a path name.
2352 #ifndef _SYS_SYSPROTO_H_
2353 struct chflags_args {
2361 register struct chflags_args /* {
2367 struct nameidata nd;
2370 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE, uap->path, td);
2371 if ((error = namei(&nd)) != 0)
2373 NDFREE(&nd, NDF_ONLY_PNBUF);
2374 vfslocked = NDHASGIANT(&nd);
2375 error = setfflags(td, nd.ni_vp, uap->flags);
2377 VFS_UNLOCK_GIANT(vfslocked);
2382 * Same as chflags() but doesn't follow symlinks.
2387 register struct lchflags_args /* {
2393 struct nameidata nd;
2396 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE, uap->path, td);
2397 if ((error = namei(&nd)) != 0)
2399 vfslocked = NDHASGIANT(&nd);
2400 NDFREE(&nd, NDF_ONLY_PNBUF);
2401 error = setfflags(td, nd.ni_vp, uap->flags);
2403 VFS_UNLOCK_GIANT(vfslocked);
2408 * Change flags of a file given a file descriptor.
2410 #ifndef _SYS_SYSPROTO_H_
2411 struct fchflags_args {
2419 register struct fchflags_args /* {
2428 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2430 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2431 error = setfflags(td, fp->f_vnode, uap->flags);
2432 VFS_UNLOCK_GIANT(vfslocked);
2438 * Common implementation code for chmod(), lchmod() and fchmod().
2441 setfmode(td, vp, mode)
2450 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2452 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2453 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2455 vattr.va_mode = mode & ALLPERMS;
2457 error = mac_check_vnode_setmode(td->td_ucred, vp, vattr.va_mode);
2460 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2461 VOP_UNLOCK(vp, 0, td);
2462 vn_finished_write(mp);
2467 * Change mode of a file given path name.
2469 #ifndef _SYS_SYSPROTO_H_
2478 register struct chmod_args /* {
2484 return (kern_chmod(td, uap->path, UIO_USERSPACE, uap->mode));
2488 kern_chmod(struct thread *td, char *path, enum uio_seg pathseg, int mode)
2491 struct nameidata nd;
2494 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2495 if ((error = namei(&nd)) != 0)
2497 vfslocked = NDHASGIANT(&nd);
2498 NDFREE(&nd, NDF_ONLY_PNBUF);
2499 error = setfmode(td, nd.ni_vp, mode);
2501 VFS_UNLOCK_GIANT(vfslocked);
2506 * Change mode of a file given path name (don't follow links.)
2508 #ifndef _SYS_SYSPROTO_H_
2509 struct lchmod_args {
2517 register struct lchmod_args /* {
2523 struct nameidata nd;
2526 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE, uap->path, td);
2527 if ((error = namei(&nd)) != 0)
2529 vfslocked = NDHASGIANT(&nd);
2530 NDFREE(&nd, NDF_ONLY_PNBUF);
2531 error = setfmode(td, nd.ni_vp, uap->mode);
2533 VFS_UNLOCK_GIANT(vfslocked);
2538 * Change mode of a file given a file descriptor.
2540 #ifndef _SYS_SYSPROTO_H_
2541 struct fchmod_args {
2549 register struct fchmod_args /* {
2558 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2560 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2561 error = setfmode(td, fp->f_vnode, uap->mode);
2562 VFS_UNLOCK_GIANT(vfslocked);
2568 * Common implementation for chown(), lchown(), and fchown()
2571 setfown(td, vp, uid, gid)
2581 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2583 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2584 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2589 error = mac_check_vnode_setowner(td->td_ucred, vp, vattr.va_uid,
2593 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2594 VOP_UNLOCK(vp, 0, td);
2595 vn_finished_write(mp);
2600 * Set ownership given a path name.
2602 #ifndef _SYS_SYSPROTO_H_
2612 register struct chown_args /* {
2619 return (kern_chown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2623 kern_chown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2627 struct nameidata nd;
2630 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2631 if ((error = namei(&nd)) != 0)
2633 vfslocked = NDHASGIANT(&nd);
2634 NDFREE(&nd, NDF_ONLY_PNBUF);
2635 error = setfown(td, nd.ni_vp, uid, gid);
2637 VFS_UNLOCK_GIANT(vfslocked);
2642 * Set ownership given a path name, do not cross symlinks.
2644 #ifndef _SYS_SYSPROTO_H_
2645 struct lchown_args {
2654 register struct lchown_args /* {
2661 return (kern_lchown(td, uap->path, UIO_USERSPACE, uap->uid, uap->gid));
2665 kern_lchown(struct thread *td, char *path, enum uio_seg pathseg, int uid,
2669 struct nameidata nd;
2672 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, pathseg, path, td);
2673 if ((error = namei(&nd)) != 0)
2675 vfslocked = NDHASGIANT(&nd);
2676 NDFREE(&nd, NDF_ONLY_PNBUF);
2677 error = setfown(td, nd.ni_vp, uid, gid);
2679 VFS_UNLOCK_GIANT(vfslocked);
2684 * Set ownership given a file descriptor.
2686 #ifndef _SYS_SYSPROTO_H_
2687 struct fchown_args {
2696 register struct fchown_args /* {
2706 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
2708 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2709 error = setfown(td, fp->f_vnode, uap->uid, uap->gid);
2710 VFS_UNLOCK_GIANT(vfslocked);
2716 * Normalize the tv_usec value of t within the allowed range.
2718 static struct timeval
2719 normalize_timeval(const struct timeval *t)
2723 if (t->tv_usec >= 0 && t->tv_usec < 1000000)
2725 n.tv_sec = t->tv_sec + t->tv_usec / 1000000;
2726 n.tv_usec = t->tv_usec % 1000000;
2727 if (n.tv_usec < 0) {
2729 n.tv_usec += 1000000;
2735 * Common implementation code for utimes(), lutimes(), and futimes().
2738 getutimes(usrtvp, tvpseg, tsp)
2739 const struct timeval *usrtvp;
2740 enum uio_seg tvpseg;
2741 struct timespec *tsp;
2743 struct timeval tv[2], tvn;
2744 const struct timeval *tvp;
2747 if (usrtvp == NULL) {
2749 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2752 if (tvpseg == UIO_SYSSPACE) {
2755 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2760 tvn = normalize_timeval(&tvp[0]);
2761 TIMEVAL_TO_TIMESPEC(&tvn, &tsp[0]);
2762 tvn = normalize_timeval(&tvp[1]);
2763 TIMEVAL_TO_TIMESPEC(&tvn, &tsp[1]);
2769 * Common implementation code for utimes(), lutimes(), and futimes().
2772 setutimes(td, vp, ts, numtimes, nullflag)
2775 const struct timespec *ts;
2779 int error, setbirthtime;
2783 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2785 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2786 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2788 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred, td) == 0 &&
2789 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2792 vattr.va_atime = ts[0];
2793 vattr.va_mtime = ts[1];
2795 vattr.va_birthtime = ts[1];
2797 vattr.va_birthtime = ts[2];
2799 vattr.va_vaflags |= VA_UTIMES_NULL;
2801 error = mac_check_vnode_setutimes(td->td_ucred, vp, vattr.va_atime,
2805 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2806 VOP_UNLOCK(vp, 0, td);
2807 vn_finished_write(mp);
2812 * Set the access and modification times of a file.
2814 #ifndef _SYS_SYSPROTO_H_
2815 struct utimes_args {
2817 struct timeval *tptr;
2823 register struct utimes_args /* {
2825 struct timeval *tptr;
2829 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2834 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
2835 struct timeval *tptr, enum uio_seg tptrseg)
2837 struct timespec ts[2];
2839 struct nameidata nd;
2842 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2844 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2845 if ((error = namei(&nd)) != 0)
2847 vfslocked = NDHASGIANT(&nd);
2848 NDFREE(&nd, NDF_ONLY_PNBUF);
2849 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2851 VFS_UNLOCK_GIANT(vfslocked);
2856 * Set the access and modification times of a file.
2858 #ifndef _SYS_SYSPROTO_H_
2859 struct lutimes_args {
2861 struct timeval *tptr;
2867 register struct lutimes_args /* {
2869 struct timeval *tptr;
2873 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2878 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
2879 struct timeval *tptr, enum uio_seg tptrseg)
2881 struct timespec ts[2];
2883 struct nameidata nd;
2886 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2888 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, pathseg, path, td);
2889 if ((error = namei(&nd)) != 0)
2891 vfslocked = NDHASGIANT(&nd);
2892 NDFREE(&nd, NDF_ONLY_PNBUF);
2893 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2895 VFS_UNLOCK_GIANT(vfslocked);
2900 * Set the access and modification times of a file.
2902 #ifndef _SYS_SYSPROTO_H_
2903 struct futimes_args {
2905 struct timeval *tptr;
2911 register struct futimes_args /* {
2913 struct timeval *tptr;
2917 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
2921 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
2922 enum uio_seg tptrseg)
2924 struct timespec ts[2];
2929 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2931 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
2933 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2934 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
2935 VFS_UNLOCK_GIANT(vfslocked);
2941 * Truncate a file given its path name.
2943 #ifndef _SYS_SYSPROTO_H_
2944 struct truncate_args {
2953 register struct truncate_args /* {
2960 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
2964 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
2970 struct nameidata nd;
2975 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2976 if ((error = namei(&nd)) != 0)
2978 vfslocked = NDHASGIANT(&nd);
2980 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
2982 VFS_UNLOCK_GIANT(vfslocked);
2985 NDFREE(&nd, NDF_ONLY_PNBUF);
2986 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2987 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2988 if (vp->v_type == VDIR)
2991 else if ((error = mac_check_vnode_write(td->td_ucred, NOCRED, vp))) {
2994 else if ((error = vn_writechk(vp)) == 0 &&
2995 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
2997 vattr.va_size = length;
2998 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
3001 vn_finished_write(mp);
3002 VFS_UNLOCK_GIANT(vfslocked);
3007 * Truncate a file given a file descriptor.
3009 #ifndef _SYS_SYSPROTO_H_
3010 struct ftruncate_args {
3019 register struct ftruncate_args /* {
3032 if (uap->length < 0)
3034 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3036 if ((fp->f_flag & FWRITE) == 0) {
3041 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3042 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3044 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3045 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3046 if (vp->v_type == VDIR)
3049 else if ((error = mac_check_vnode_write(td->td_ucred, fp->f_cred,
3053 else if ((error = vn_writechk(vp)) == 0) {
3055 vattr.va_size = uap->length;
3056 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
3058 VOP_UNLOCK(vp, 0, td);
3059 vn_finished_write(mp);
3061 VFS_UNLOCK_GIANT(vfslocked);
3066 #if defined(COMPAT_43)
3068 * Truncate a file given its path name.
3070 #ifndef _SYS_SYSPROTO_H_
3071 struct otruncate_args {
3079 register struct otruncate_args /* {
3084 struct truncate_args /* {
3090 nuap.path = uap->path;
3091 nuap.length = uap->length;
3092 return (truncate(td, &nuap));
3096 * Truncate a file given a file descriptor.
3098 #ifndef _SYS_SYSPROTO_H_
3099 struct oftruncate_args {
3107 register struct oftruncate_args /* {
3112 struct ftruncate_args /* {
3119 nuap.length = uap->length;
3120 return (ftruncate(td, &nuap));
3122 #endif /* COMPAT_43 */
3125 * Sync an open file.
3127 #ifndef _SYS_SYSPROTO_H_
3135 struct fsync_args /* {
3145 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3148 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3149 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3151 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3152 if (vp->v_object != NULL) {
3153 VM_OBJECT_LOCK(vp->v_object);
3154 vm_object_page_clean(vp->v_object, 0, 0, 0);
3155 VM_OBJECT_UNLOCK(vp->v_object);
3157 error = VOP_FSYNC(vp, MNT_WAIT, td);
3159 VOP_UNLOCK(vp, 0, td);
3160 vn_finished_write(mp);
3162 VFS_UNLOCK_GIANT(vfslocked);
3168 * Rename files. Source and destination must either both be directories,
3169 * or both not be directories. If target is a directory, it must be empty.
3171 #ifndef _SYS_SYSPROTO_H_
3172 struct rename_args {
3180 register struct rename_args /* {
3186 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3190 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3192 struct mount *mp = NULL;
3193 struct vnode *tvp, *fvp, *tdvp;
3194 struct nameidata fromnd, tond;
3201 NDINIT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE,
3204 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE,
3207 if ((error = namei(&fromnd)) != 0)
3209 fvfslocked = NDHASGIANT(&fromnd);
3212 error = mac_check_vnode_rename_from(td->td_ucred, fromnd.ni_dvp,
3213 fromnd.ni_vp, &fromnd.ni_cnd);
3214 VOP_UNLOCK(fromnd.ni_dvp, 0, td);
3215 VOP_UNLOCK(fromnd.ni_vp, 0, td);
3219 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3221 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3222 vrele(fromnd.ni_dvp);
3226 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3227 MPSAFE, pathseg, to, td);
3228 if (fromnd.ni_vp->v_type == VDIR)
3229 tond.ni_cnd.cn_flags |= WILLBEDIR;
3230 if ((error = namei(&tond)) != 0) {
3231 /* Translate error code for rename("dir1", "dir2/."). */
3232 if (error == EISDIR && fvp->v_type == VDIR)
3234 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3235 vrele(fromnd.ni_dvp);
3237 vn_finished_write(mp);
3240 tvfslocked = NDHASGIANT(&tond);
3244 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3247 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3255 * If the source is the same as the destination (that is, if they
3256 * are links to the same vnode), then there is nothing to do.
3262 error = mac_check_vnode_rename_to(td->td_ucred, tdvp,
3263 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3267 VOP_LEASE(tdvp, td, td->td_ucred, LEASE_WRITE);
3268 if (fromnd.ni_dvp != tdvp) {
3269 VOP_LEASE(fromnd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3272 VOP_LEASE(tvp, td, td->td_ucred, LEASE_WRITE);
3274 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3275 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3276 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3277 NDFREE(&tond, NDF_ONLY_PNBUF);
3279 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3280 NDFREE(&tond, NDF_ONLY_PNBUF);
3287 vrele(fromnd.ni_dvp);
3290 vrele(tond.ni_startdir);
3291 vn_finished_write(mp);
3293 if (fromnd.ni_startdir)
3294 vrele(fromnd.ni_startdir);
3295 VFS_UNLOCK_GIANT(fvfslocked);
3296 VFS_UNLOCK_GIANT(tvfslocked);
3303 * Make a directory file.
3305 #ifndef _SYS_SYSPROTO_H_
3314 register struct mkdir_args /* {
3320 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3324 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3330 struct nameidata nd;
3335 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE, segflg, path, td);
3336 nd.ni_cnd.cn_flags |= WILLBEDIR;
3337 if ((error = namei(&nd)) != 0)
3339 vfslocked = NDHASGIANT(&nd);
3342 NDFREE(&nd, NDF_ONLY_PNBUF);
3345 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3346 * the strange behaviour of leaving the vnode unlocked
3347 * if the target is the same vnode as the parent.
3349 if (vp == nd.ni_dvp)
3353 VFS_UNLOCK_GIANT(vfslocked);
3356 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3357 NDFREE(&nd, NDF_ONLY_PNBUF);
3359 VFS_UNLOCK_GIANT(vfslocked);
3360 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3365 vattr.va_type = VDIR;
3366 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
3367 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3368 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
3370 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3375 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3376 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3380 NDFREE(&nd, NDF_ONLY_PNBUF);
3384 vn_finished_write(mp);
3385 VFS_UNLOCK_GIANT(vfslocked);
3390 * Remove a directory file.
3392 #ifndef _SYS_SYSPROTO_H_
3400 struct rmdir_args /* {
3405 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3409 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3414 struct nameidata nd;
3419 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE, pathseg, path, td);
3420 if ((error = namei(&nd)) != 0)
3422 vfslocked = NDHASGIANT(&nd);
3424 if (vp->v_type != VDIR) {
3429 * No rmdir "." please.
3431 if (nd.ni_dvp == vp) {
3436 * The root of a mounted filesystem cannot be deleted.
3438 if (vp->v_vflag & VV_ROOT) {
3443 error = mac_check_vnode_delete(td->td_ucred, nd.ni_dvp, vp,
3448 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3449 NDFREE(&nd, NDF_ONLY_PNBUF);
3450 if (nd.ni_dvp == vp)
3455 VFS_UNLOCK_GIANT(vfslocked);
3456 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3460 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3461 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3462 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3463 vn_finished_write(mp);
3465 NDFREE(&nd, NDF_ONLY_PNBUF);
3466 if (nd.ni_dvp == vp)
3471 VFS_UNLOCK_GIANT(vfslocked);
3477 * Read a block of directory entries in a filesystem independent format.
3479 #ifndef _SYS_SYSPROTO_H_
3480 struct ogetdirentries_args {
3488 ogetdirentries(td, uap)
3490 register struct ogetdirentries_args /* {
3499 struct uio auio, kuio;
3500 struct iovec aiov, kiov;
3501 struct dirent *dp, *edp;
3503 int error, eofflag, readcnt;
3506 /* XXX arbitrary sanity limit on `count'. */
3507 if (uap->count > 64 * 1024)
3509 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3511 if ((fp->f_flag & FREAD) == 0) {
3517 if (vp->v_type != VDIR) {
3521 aiov.iov_base = uap->buf;
3522 aiov.iov_len = uap->count;
3523 auio.uio_iov = &aiov;
3524 auio.uio_iovcnt = 1;
3525 auio.uio_rw = UIO_READ;
3526 auio.uio_segflg = UIO_USERSPACE;
3528 auio.uio_resid = uap->count;
3529 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3530 loff = auio.uio_offset = fp->f_offset;
3532 error = mac_check_vnode_readdir(td->td_ucred, vp);
3534 VOP_UNLOCK(vp, 0, td);
3539 # if (BYTE_ORDER != LITTLE_ENDIAN)
3540 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3541 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3543 fp->f_offset = auio.uio_offset;
3548 kuio.uio_iov = &kiov;
3549 kuio.uio_segflg = UIO_SYSSPACE;
3550 kiov.iov_len = uap->count;
3551 MALLOC(dirbuf, caddr_t, uap->count, M_TEMP, M_WAITOK);
3552 kiov.iov_base = dirbuf;
3553 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3555 fp->f_offset = kuio.uio_offset;
3557 readcnt = uap->count - kuio.uio_resid;
3558 edp = (struct dirent *)&dirbuf[readcnt];
3559 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3560 # if (BYTE_ORDER == LITTLE_ENDIAN)
3562 * The expected low byte of
3563 * dp->d_namlen is our dp->d_type.
3564 * The high MBZ byte of dp->d_namlen
3565 * is our dp->d_namlen.
3567 dp->d_type = dp->d_namlen;
3571 * The dp->d_type is the high byte
3572 * of the expected dp->d_namlen,
3573 * so must be zero'ed.
3577 if (dp->d_reclen > 0) {
3578 dp = (struct dirent *)
3579 ((char *)dp + dp->d_reclen);
3586 error = uiomove(dirbuf, readcnt, &auio);
3588 FREE(dirbuf, M_TEMP);
3590 VOP_UNLOCK(vp, 0, td);
3595 if (uap->count == auio.uio_resid) {
3596 if (union_dircheckp) {
3597 error = union_dircheckp(td, &vp, fp);
3606 * XXX We could delay dropping the lock above but
3607 * union_dircheckp complicates things.
3609 vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, td);
3610 if ((vp->v_vflag & VV_ROOT) &&
3611 (vp->v_mount->mnt_flag & MNT_UNION)) {
3612 struct vnode *tvp = vp;
3613 vp = vp->v_mount->mnt_vnodecovered;
3621 VOP_UNLOCK(vp, 0, td);
3623 error = copyout(&loff, uap->basep, sizeof(long));
3625 td->td_retval[0] = uap->count - auio.uio_resid;
3628 #endif /* COMPAT_43 */
3631 * Read a block of directory entries in a filesystem independent format.
3633 #ifndef _SYS_SYSPROTO_H_
3634 struct getdirentries_args {
3642 getdirentries(td, uap)
3644 register struct getdirentries_args /* {
3659 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3661 if ((fp->f_flag & FREAD) == 0) {
3667 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3668 if (vp->v_type != VDIR) {
3672 aiov.iov_base = uap->buf;
3673 aiov.iov_len = uap->count;
3674 auio.uio_iov = &aiov;
3675 auio.uio_iovcnt = 1;
3676 auio.uio_rw = UIO_READ;
3677 auio.uio_segflg = UIO_USERSPACE;
3679 auio.uio_resid = uap->count;
3680 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
3681 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3682 loff = auio.uio_offset = fp->f_offset;
3684 error = mac_check_vnode_readdir(td->td_ucred, vp);
3687 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3689 fp->f_offset = auio.uio_offset;
3690 VOP_UNLOCK(vp, 0, td);
3693 if (uap->count == auio.uio_resid) {
3694 if (union_dircheckp) {
3695 error = union_dircheckp(td, &vp, fp);
3697 VFS_UNLOCK_GIANT(vfslocked);
3704 * XXX We could delay dropping the lock above but
3705 * union_dircheckp complicates things.
3707 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3708 if ((vp->v_vflag & VV_ROOT) &&
3709 (vp->v_mount->mnt_flag & MNT_UNION)) {
3710 struct vnode *tvp = vp;
3711 vp = vp->v_mount->mnt_vnodecovered;
3717 VFS_UNLOCK_GIANT(vfslocked);
3720 VOP_UNLOCK(vp, 0, td);
3722 if (uap->basep != NULL) {
3723 error = copyout(&loff, uap->basep, sizeof(long));
3725 td->td_retval[0] = uap->count - auio.uio_resid;
3727 VFS_UNLOCK_GIANT(vfslocked);
3731 #ifndef _SYS_SYSPROTO_H_
3732 struct getdents_args {
3741 register struct getdents_args /* {
3747 struct getdirentries_args ap;
3750 ap.count = uap->count;
3752 return (getdirentries(td, &ap));
3756 * Set the mode mask for creation of filesystem nodes.
3760 #ifndef _SYS_SYSPROTO_H_
3768 struct umask_args /* {
3772 register struct filedesc *fdp;
3774 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
3775 fdp = td->td_proc->p_fd;
3776 td->td_retval[0] = fdp->fd_cmask;
3777 fdp->fd_cmask = uap->newmask & ALLPERMS;
3778 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
3783 * Void all references to file by ripping underlying filesystem
3786 #ifndef _SYS_SYSPROTO_H_
3787 struct revoke_args {
3794 register struct revoke_args /* {
3801 struct nameidata nd;
3804 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, UIO_USERSPACE,
3806 if ((error = namei(&nd)) != 0)
3808 vfslocked = NDHASGIANT(&nd);
3810 NDFREE(&nd, NDF_ONLY_PNBUF);
3811 if (vp->v_type != VCHR) {
3816 error = mac_check_vnode_revoke(td->td_ucred, vp);
3820 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
3823 if (td->td_ucred->cr_uid != vattr.va_uid) {
3824 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
3829 VOP_REVOKE(vp, REVOKEALL);
3832 VFS_UNLOCK_GIANT(vfslocked);
3837 * Convert a user file descriptor to a kernel file entry.
3838 * A reference on the file entry is held upon returning.
3841 getvnode(fdp, fd, fpp)
3842 struct filedesc *fdp;
3854 if ((u_int)fd >= fdp->fd_nfiles ||
3855 (fp = fdp->fd_ofiles[fd]) == NULL)
3857 else if (fp->f_vnode == NULL) {
3864 FILEDESC_UNLOCK(fdp);
3871 * Get (NFS) file handle
3873 #ifndef _SYS_SYSPROTO_H_
3874 struct lgetfh_args {
3882 register struct lgetfh_args *uap;
3884 struct nameidata nd;
3886 register struct vnode *vp;
3893 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE,
3894 UIO_USERSPACE, uap->fname, td);
3898 vfslocked = NDHASGIANT(&nd);
3899 NDFREE(&nd, NDF_ONLY_PNBUF);
3901 bzero(&fh, sizeof(fh));
3902 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3903 error = VFS_VPTOFH(vp, &fh.fh_fid);
3905 VFS_UNLOCK_GIANT(vfslocked);
3908 error = copyout(&fh, uap->fhp, sizeof (fh));
3912 #ifndef _SYS_SYSPROTO_H_
3921 register struct getfh_args *uap;
3923 struct nameidata nd;
3925 register struct vnode *vp;
3932 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE,
3933 UIO_USERSPACE, uap->fname, td);
3937 vfslocked = NDHASGIANT(&nd);
3938 NDFREE(&nd, NDF_ONLY_PNBUF);
3940 bzero(&fh, sizeof(fh));
3941 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3942 error = VFS_VPTOFH(vp, &fh.fh_fid);
3944 VFS_UNLOCK_GIANT(vfslocked);
3947 error = copyout(&fh, uap->fhp, sizeof (fh));
3952 * syscall for the rpc.lockd to use to translate a NFS file handle into
3953 * an open descriptor.
3955 * warning: do not remove the suser() call or this becomes one giant
3960 #ifndef _SYS_SYSPROTO_H_
3961 struct fhopen_args {
3962 const struct fhandle *u_fhp;
3969 struct fhopen_args /* {
3970 const struct fhandle *u_fhp;
3974 struct proc *p = td->td_proc;
3979 struct vattr *vap = &vat;
3982 register struct filedesc *fdp = p->p_fd;
3983 int fmode, mode, error, type;
3990 fmode = FFLAGS(uap->flags);
3991 /* why not allow a non-read/write open for our lockd? */
3992 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3994 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3997 /* find the mount point */
3999 mp = vfs_getvfs(&fhp.fh_fsid);
4004 /* now give me my vnode, it gets returned to me locked */
4005 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
4009 * from now on we have to make sure not
4010 * to forget about the vnode
4011 * any error that causes an abort must vput(vp)
4012 * just set error = err and 'goto bad;'.
4018 if (vp->v_type == VLNK) {
4022 if (vp->v_type == VSOCK) {
4027 if (fmode & (FWRITE | O_TRUNC)) {
4028 if (vp->v_type == VDIR) {
4032 error = vn_writechk(vp);
4039 if (fmode & O_APPEND)
4042 error = mac_check_vnode_open(td->td_ucred, vp, mode);
4047 error = VOP_ACCESS(vp, mode, td->td_ucred, td);
4051 if (fmode & O_TRUNC) {
4052 VOP_UNLOCK(vp, 0, td); /* XXX */
4053 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4057 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4058 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
4061 * We don't yet have fp->f_cred, so use td->td_ucred, which
4064 error = mac_check_vnode_write(td->td_ucred, td->td_ucred, vp);
4069 error = VOP_SETATTR(vp, vap, td->td_ucred, td);
4073 vn_finished_write(mp);
4077 error = VOP_OPEN(vp, fmode, td->td_ucred, td, -1);
4085 * end of vn_open code
4088 if ((error = falloc(td, &nfp, &indx)) != 0) {
4093 /* An extra reference on `nfp' has been held for us by falloc(). */
4098 nfp->f_flag = fmode & FMASK;
4099 nfp->f_ops = &vnops;
4100 nfp->f_type = DTYPE_VNODE;
4101 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4102 lf.l_whence = SEEK_SET;
4105 if (fmode & O_EXLOCK)
4106 lf.l_type = F_WRLCK;
4108 lf.l_type = F_RDLCK;
4110 if ((fmode & FNONBLOCK) == 0)
4112 VOP_UNLOCK(vp, 0, td);
4113 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4116 * The lock request failed. Normally close the
4117 * descriptor but handle the case where someone might
4118 * have dup()d or close()d it when we weren't looking.
4120 fdclose(fdp, fp, indx, td);
4123 * release our private reference
4128 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4129 fp->f_flag |= FHASLOCK;
4132 VOP_UNLOCK(vp, 0, td);
4135 td->td_retval[0] = indx;
4146 * Stat an (NFS) file handle.
4150 #ifndef _SYS_SYSPROTO_H_
4151 struct fhstat_args {
4152 struct fhandle *u_fhp;
4159 register struct fhstat_args /* {
4160 struct fhandle *u_fhp;
4173 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4177 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4181 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp))) {
4185 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4190 error = copyout(&sb, uap->sb, sizeof(sb));
4195 * Implement fstatfs() for (NFS) file handles.
4199 #ifndef _SYS_SYSPROTO_H_
4200 struct fhstatfs_args {
4201 struct fhandle *u_fhp;
4208 struct fhstatfs_args /* {
4209 struct fhandle *u_fhp;
4217 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4220 error = kern_fhstatfs(td, fh, &sf);
4223 return (copyout(&sf, uap->buf, sizeof(sf)));
4227 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4238 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4242 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4250 error = prison_canseemount(td->td_ucred, mp);
4254 error = mac_check_mount_stat(td->td_ucred, mp);
4261 * Set these in case the underlying filesystem fails to do so.
4263 sp->f_version = STATFS_VERSION;
4264 sp->f_namemax = NAME_MAX;
4265 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4266 error = VFS_STATFS(mp, sp, td);
4275 * Syscall to push extended attribute configuration information into the
4276 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4277 * a command (int cmd), and attribute name and misc data. For now, the
4278 * attribute name is left in userspace for consumption by the VFS_op.
4279 * It will probably be changed to be copied into sysspace by the
4280 * syscall in the future, once issues with various consumers of the
4281 * attribute code have raised their hands.
4283 * Currently this is used only by UFS Extended Attributes.
4288 struct extattrctl_args /* {
4291 const char *filename;
4293 const char *attrname;
4296 struct vnode *filename_vp;
4297 struct nameidata nd;
4298 struct mount *mp, *mp_writable;
4299 char attrname[EXTATTR_MAXNAMELEN];
4300 int vfslocked, fnvfslocked, error;
4303 * uap->attrname is not always defined. We check again later when we
4304 * invoke the VFS call so as to pass in NULL there if needed.
4306 if (uap->attrname != NULL) {
4307 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4313 vfslocked = fnvfslocked = 0;
4315 * uap->filename is not always defined. If it is, grab a vnode lock,
4316 * which VFS_EXTATTRCTL() will later release.
4319 if (uap->filename != NULL) {
4320 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW | LOCKLEAF,
4321 UIO_USERSPACE, uap->filename, td);
4325 fnvfslocked = NDHASGIANT(&nd);
4326 filename_vp = nd.ni_vp;
4327 NDFREE(&nd, NDF_NO_VP_RELE | NDF_NO_VP_UNLOCK);
4330 /* uap->path is always defined. */
4331 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4334 if (filename_vp != NULL)
4338 vfslocked = NDHASGIANT(&nd);
4339 mp = nd.ni_vp->v_mount;
4340 error = vn_start_write(nd.ni_vp, &mp_writable, V_WAIT | PCATCH);
4343 if (filename_vp != NULL)
4348 error = VFS_EXTATTRCTL(mp, uap->cmd, filename_vp, uap->attrnamespace,
4349 uap->attrname != NULL ? attrname : NULL, td);
4351 vn_finished_write(mp_writable);
4353 * VFS_EXTATTRCTL will have unlocked, but not de-ref'd,
4354 * filename_vp, so vrele it if it is defined.
4356 if (filename_vp != NULL)
4359 VFS_UNLOCK_GIANT(fnvfslocked);
4360 VFS_UNLOCK_GIANT(vfslocked);
4365 * Set a named extended attribute on a file or directory
4367 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4368 * kernelspace string pointer "attrname", userspace buffer
4369 * pointer "data", buffer length "nbytes", thread "td".
4370 * Returns: 0 on success, an error number otherwise
4372 * References: vp must be a valid reference for the duration of the call
4375 extattr_set_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4376 void *data, size_t nbytes, struct thread *td)
4384 VFS_ASSERT_GIANT(vp->v_mount);
4385 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4388 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4389 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4391 aiov.iov_base = data;
4392 aiov.iov_len = nbytes;
4393 auio.uio_iov = &aiov;
4394 auio.uio_iovcnt = 1;
4395 auio.uio_offset = 0;
4396 if (nbytes > INT_MAX) {
4400 auio.uio_resid = nbytes;
4401 auio.uio_rw = UIO_WRITE;
4402 auio.uio_segflg = UIO_USERSPACE;
4407 error = mac_check_vnode_setextattr(td->td_ucred, vp, attrnamespace,
4413 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio,
4415 cnt -= auio.uio_resid;
4416 td->td_retval[0] = cnt;
4419 VOP_UNLOCK(vp, 0, td);
4420 vn_finished_write(mp);
4425 extattr_set_fd(td, uap)
4427 struct extattr_set_fd_args /* {
4430 const char *attrname;
4436 char attrname[EXTATTR_MAXNAMELEN];
4437 int vfslocked, error;
4439 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4443 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4447 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
4448 error = extattr_set_vp(fp->f_vnode, uap->attrnamespace,
4449 attrname, uap->data, uap->nbytes, td);
4451 VFS_UNLOCK_GIANT(vfslocked);
4457 extattr_set_file(td, uap)
4459 struct extattr_set_file_args /* {
4462 const char *attrname;
4467 struct nameidata nd;
4468 char attrname[EXTATTR_MAXNAMELEN];
4469 int vfslocked, error;
4471 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4475 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4479 NDFREE(&nd, NDF_ONLY_PNBUF);
4481 vfslocked = NDHASGIANT(&nd);
4482 error = extattr_set_vp(nd.ni_vp, uap->attrnamespace, attrname,
4483 uap->data, uap->nbytes, td);
4486 VFS_UNLOCK_GIANT(vfslocked);
4491 extattr_set_link(td, uap)
4493 struct extattr_set_link_args /* {
4496 const char *attrname;
4501 struct nameidata nd;
4502 char attrname[EXTATTR_MAXNAMELEN];
4503 int vfslocked, error;
4505 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4509 NDINIT(&nd, LOOKUP, MPSAFE | NOFOLLOW, UIO_USERSPACE, uap->path, td);
4513 NDFREE(&nd, NDF_ONLY_PNBUF);
4515 vfslocked = NDHASGIANT(&nd);
4516 error = extattr_set_vp(nd.ni_vp, uap->attrnamespace, attrname,
4517 uap->data, uap->nbytes, td);
4520 VFS_UNLOCK_GIANT(vfslocked);
4525 * Get a named extended attribute on a file or directory
4527 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4528 * kernelspace string pointer "attrname", userspace buffer
4529 * pointer "data", buffer length "nbytes", thread "td".
4530 * Returns: 0 on success, an error number otherwise
4532 * References: vp must be a valid reference for the duration of the call
4535 extattr_get_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4536 void *data, size_t nbytes, struct thread *td)
4538 struct uio auio, *auiop;
4541 size_t size, *sizep;
4544 VFS_ASSERT_GIANT(vp->v_mount);
4545 VOP_LEASE(vp, td, td->td_ucred, LEASE_READ);
4546 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4549 * Slightly unusual semantics: if the user provides a NULL data
4550 * pointer, they don't want to receive the data, just the
4551 * maximum read length.
4557 aiov.iov_base = data;
4558 aiov.iov_len = nbytes;
4559 auio.uio_iov = &aiov;
4560 auio.uio_iovcnt = 1;
4561 auio.uio_offset = 0;
4562 if (nbytes > INT_MAX) {
4566 auio.uio_resid = nbytes;
4567 auio.uio_rw = UIO_READ;
4568 auio.uio_segflg = UIO_USERSPACE;
4576 error = mac_check_vnode_getextattr(td->td_ucred, vp, attrnamespace,
4582 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, auiop, sizep,
4585 if (auiop != NULL) {
4586 cnt -= auio.uio_resid;
4587 td->td_retval[0] = cnt;
4589 td->td_retval[0] = size;
4592 VOP_UNLOCK(vp, 0, td);
4597 extattr_get_fd(td, uap)
4599 struct extattr_get_fd_args /* {
4602 const char *attrname;
4608 char attrname[EXTATTR_MAXNAMELEN];
4609 int vfslocked, error;
4611 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4615 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4619 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
4620 error = extattr_get_vp(fp->f_vnode, uap->attrnamespace,
4621 attrname, uap->data, uap->nbytes, td);
4624 VFS_UNLOCK_GIANT(vfslocked);
4629 extattr_get_file(td, uap)
4631 struct extattr_get_file_args /* {
4634 const char *attrname;
4639 struct nameidata nd;
4640 char attrname[EXTATTR_MAXNAMELEN];
4641 int vfslocked, error;
4643 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4647 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4651 NDFREE(&nd, NDF_ONLY_PNBUF);
4653 vfslocked = NDHASGIANT(&nd);
4654 error = extattr_get_vp(nd.ni_vp, uap->attrnamespace, attrname,
4655 uap->data, uap->nbytes, td);
4658 VFS_UNLOCK_GIANT(vfslocked);
4663 extattr_get_link(td, uap)
4665 struct extattr_get_link_args /* {
4668 const char *attrname;
4673 struct nameidata nd;
4674 char attrname[EXTATTR_MAXNAMELEN];
4675 int vfslocked, error;
4677 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4681 NDINIT(&nd, LOOKUP, MPSAFE | NOFOLLOW, UIO_USERSPACE, uap->path, td);
4685 NDFREE(&nd, NDF_ONLY_PNBUF);
4687 vfslocked = NDHASGIANT(&nd);
4688 error = extattr_get_vp(nd.ni_vp, uap->attrnamespace, attrname,
4689 uap->data, uap->nbytes, td);
4692 VFS_UNLOCK_GIANT(vfslocked);
4697 * extattr_delete_vp(): Delete a named extended attribute on a file or
4700 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4701 * kernelspace string pointer "attrname", proc "p"
4702 * Returns: 0 on success, an error number otherwise
4704 * References: vp must be a valid reference for the duration of the call
4707 extattr_delete_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4713 VFS_ASSERT_GIANT(vp->v_mount);
4714 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4717 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4718 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4721 error = mac_check_vnode_deleteextattr(td->td_ucred, vp, attrnamespace,
4727 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, td->td_ucred,
4729 if (error == EOPNOTSUPP)
4730 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
4735 VOP_UNLOCK(vp, 0, td);
4736 vn_finished_write(mp);
4741 extattr_delete_fd(td, uap)
4743 struct extattr_delete_fd_args /* {
4746 const char *attrname;
4750 char attrname[EXTATTR_MAXNAMELEN];
4751 int vfslocked, error;
4753 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4757 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4761 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
4762 error = extattr_delete_vp(fp->f_vnode, uap->attrnamespace,
4765 VFS_UNLOCK_GIANT(vfslocked);
4770 extattr_delete_file(td, uap)
4772 struct extattr_delete_file_args /* {
4775 const char *attrname;
4778 struct nameidata nd;
4779 char attrname[EXTATTR_MAXNAMELEN];
4780 int vfslocked, error;
4782 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4786 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4790 NDFREE(&nd, NDF_ONLY_PNBUF);
4792 vfslocked = NDHASGIANT(&nd);
4793 error = extattr_delete_vp(nd.ni_vp, uap->attrnamespace, attrname, td);
4795 VFS_UNLOCK_GIANT(vfslocked);
4800 extattr_delete_link(td, uap)
4802 struct extattr_delete_link_args /* {
4805 const char *attrname;
4808 struct nameidata nd;
4809 char attrname[EXTATTR_MAXNAMELEN];
4810 int vfslocked, error;
4812 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4816 NDINIT(&nd, LOOKUP, MPSAFE | NOFOLLOW, UIO_USERSPACE, uap->path, td);
4820 NDFREE(&nd, NDF_ONLY_PNBUF);
4822 vfslocked = NDHASGIANT(&nd);
4823 error = extattr_delete_vp(nd.ni_vp, uap->attrnamespace, attrname, td);
4825 VFS_UNLOCK_GIANT(vfslocked);
4830 * Retrieve a list of extended attributes on a file or directory.
4832 * Arguments: unlocked vnode "vp", attribute namespace 'attrnamespace",
4833 * userspace buffer pointer "data", buffer length "nbytes",
4835 * Returns: 0 on success, an error number otherwise
4837 * References: vp must be a valid reference for the duration of the call
4840 extattr_list_vp(struct vnode *vp, int attrnamespace, void *data,
4841 size_t nbytes, struct thread *td)
4843 struct uio auio, *auiop;
4844 size_t size, *sizep;
4849 VFS_ASSERT_GIANT(vp->v_mount);
4850 VOP_LEASE(vp, td, td->td_ucred, LEASE_READ);
4851 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4857 aiov.iov_base = data;
4858 aiov.iov_len = nbytes;
4859 auio.uio_iov = &aiov;
4860 auio.uio_iovcnt = 1;
4861 auio.uio_offset = 0;
4862 if (nbytes > INT_MAX) {
4866 auio.uio_resid = nbytes;
4867 auio.uio_rw = UIO_READ;
4868 auio.uio_segflg = UIO_USERSPACE;
4876 error = mac_check_vnode_listextattr(td->td_ucred, vp, attrnamespace);
4881 error = VOP_LISTEXTATTR(vp, attrnamespace, auiop, sizep,
4884 if (auiop != NULL) {
4885 cnt -= auio.uio_resid;
4886 td->td_retval[0] = cnt;
4888 td->td_retval[0] = size;
4891 VOP_UNLOCK(vp, 0, td);
4897 extattr_list_fd(td, uap)
4899 struct extattr_list_fd_args /* {
4907 int vfslocked, error;
4909 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4913 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
4914 error = extattr_list_vp(fp->f_vnode, uap->attrnamespace, uap->data,
4918 VFS_UNLOCK_GIANT(vfslocked);
4923 extattr_list_file(td, uap)
4925 struct extattr_list_file_args /* {
4932 struct nameidata nd;
4933 int vfslocked, error;
4935 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4939 NDFREE(&nd, NDF_ONLY_PNBUF);
4941 vfslocked = NDHASGIANT(&nd);
4942 error = extattr_list_vp(nd.ni_vp, uap->attrnamespace, uap->data,
4946 VFS_UNLOCK_GIANT(vfslocked);
4951 extattr_list_link(td, uap)
4953 struct extattr_list_link_args /* {
4960 struct nameidata nd;
4961 int vfslocked, error;
4963 NDINIT(&nd, LOOKUP, MPSAFE | NOFOLLOW, UIO_USERSPACE, uap->path, td);
4967 NDFREE(&nd, NDF_ONLY_PNBUF);
4969 vfslocked = NDHASGIANT(&nd);
4970 error = extattr_list_vp(nd.ni_vp, uap->attrnamespace, uap->data,
4974 VFS_UNLOCK_GIANT(vfslocked);