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)
1636 return (error == EINVAL ? EPERM : error);
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 * Common implementation code for utimes(), lutimes(), and futimes().
2719 getutimes(usrtvp, tvpseg, tsp)
2720 const struct timeval *usrtvp;
2721 enum uio_seg tvpseg;
2722 struct timespec *tsp;
2724 struct timeval tv[2];
2725 const struct timeval *tvp;
2728 if (usrtvp == NULL) {
2730 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2733 if (tvpseg == UIO_SYSSPACE) {
2736 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
2741 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
2742 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
2744 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2745 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2751 * Common implementation code for utimes(), lutimes(), and futimes().
2754 setutimes(td, vp, ts, numtimes, nullflag)
2757 const struct timespec *ts;
2761 int error, setbirthtime;
2765 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
2767 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2768 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2770 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred, td) == 0 &&
2771 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
2774 vattr.va_atime = ts[0];
2775 vattr.va_mtime = ts[1];
2777 vattr.va_birthtime = ts[1];
2779 vattr.va_birthtime = ts[2];
2781 vattr.va_vaflags |= VA_UTIMES_NULL;
2783 error = mac_check_vnode_setutimes(td->td_ucred, vp, vattr.va_atime,
2787 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2788 VOP_UNLOCK(vp, 0, td);
2789 vn_finished_write(mp);
2794 * Set the access and modification times of a file.
2796 #ifndef _SYS_SYSPROTO_H_
2797 struct utimes_args {
2799 struct timeval *tptr;
2805 register struct utimes_args /* {
2807 struct timeval *tptr;
2811 return (kern_utimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2816 kern_utimes(struct thread *td, char *path, enum uio_seg pathseg,
2817 struct timeval *tptr, enum uio_seg tptrseg)
2819 struct timespec ts[2];
2821 struct nameidata nd;
2824 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2826 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2827 if ((error = namei(&nd)) != 0)
2829 vfslocked = NDHASGIANT(&nd);
2830 NDFREE(&nd, NDF_ONLY_PNBUF);
2831 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2833 VFS_UNLOCK_GIANT(vfslocked);
2838 * Set the access and modification times of a file.
2840 #ifndef _SYS_SYSPROTO_H_
2841 struct lutimes_args {
2843 struct timeval *tptr;
2849 register struct lutimes_args /* {
2851 struct timeval *tptr;
2855 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
2860 kern_lutimes(struct thread *td, char *path, enum uio_seg pathseg,
2861 struct timeval *tptr, enum uio_seg tptrseg)
2863 struct timespec ts[2];
2865 struct nameidata nd;
2868 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2870 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, pathseg, path, td);
2871 if ((error = namei(&nd)) != 0)
2873 vfslocked = NDHASGIANT(&nd);
2874 NDFREE(&nd, NDF_ONLY_PNBUF);
2875 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
2877 VFS_UNLOCK_GIANT(vfslocked);
2882 * Set the access and modification times of a file.
2884 #ifndef _SYS_SYSPROTO_H_
2885 struct futimes_args {
2887 struct timeval *tptr;
2893 register struct futimes_args /* {
2895 struct timeval *tptr;
2899 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
2903 kern_futimes(struct thread *td, int fd, struct timeval *tptr,
2904 enum uio_seg tptrseg)
2906 struct timespec ts[2];
2911 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
2913 if ((error = getvnode(td->td_proc->p_fd, fd, &fp)) != 0)
2915 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
2916 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
2917 VFS_UNLOCK_GIANT(vfslocked);
2923 * Truncate a file given its path name.
2925 #ifndef _SYS_SYSPROTO_H_
2926 struct truncate_args {
2935 register struct truncate_args /* {
2942 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
2946 kern_truncate(struct thread *td, char *path, enum uio_seg pathseg, off_t length)
2952 struct nameidata nd;
2957 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, pathseg, path, td);
2958 if ((error = namei(&nd)) != 0)
2960 vfslocked = NDHASGIANT(&nd);
2962 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) {
2964 VFS_UNLOCK_GIANT(vfslocked);
2967 NDFREE(&nd, NDF_ONLY_PNBUF);
2968 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
2969 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2970 if (vp->v_type == VDIR)
2973 else if ((error = mac_check_vnode_write(td->td_ucred, NOCRED, vp))) {
2976 else if ((error = vn_writechk(vp)) == 0 &&
2977 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) {
2979 vattr.va_size = length;
2980 error = VOP_SETATTR(vp, &vattr, td->td_ucred, td);
2983 vn_finished_write(mp);
2984 VFS_UNLOCK_GIANT(vfslocked);
2989 * Truncate a file given a file descriptor.
2991 #ifndef _SYS_SYSPROTO_H_
2992 struct ftruncate_args {
3001 register struct ftruncate_args /* {
3014 if (uap->length < 0)
3016 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3018 if ((fp->f_flag & FWRITE) == 0) {
3023 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3024 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3026 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3027 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3028 if (vp->v_type == VDIR)
3031 else if ((error = mac_check_vnode_write(td->td_ucred, fp->f_cred,
3035 else if ((error = vn_writechk(vp)) == 0) {
3037 vattr.va_size = uap->length;
3038 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
3040 VOP_UNLOCK(vp, 0, td);
3041 vn_finished_write(mp);
3043 VFS_UNLOCK_GIANT(vfslocked);
3048 #if defined(COMPAT_43)
3050 * Truncate a file given its path name.
3052 #ifndef _SYS_SYSPROTO_H_
3053 struct otruncate_args {
3061 register struct otruncate_args /* {
3066 struct truncate_args /* {
3072 nuap.path = uap->path;
3073 nuap.length = uap->length;
3074 return (truncate(td, &nuap));
3078 * Truncate a file given a file descriptor.
3080 #ifndef _SYS_SYSPROTO_H_
3081 struct oftruncate_args {
3089 register struct oftruncate_args /* {
3094 struct ftruncate_args /* {
3101 nuap.length = uap->length;
3102 return (ftruncate(td, &nuap));
3104 #endif /* COMPAT_43 */
3107 * Sync an open file.
3109 #ifndef _SYS_SYSPROTO_H_
3117 struct fsync_args /* {
3127 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3130 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3131 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
3133 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3134 if (vp->v_object != NULL) {
3135 VM_OBJECT_LOCK(vp->v_object);
3136 vm_object_page_clean(vp->v_object, 0, 0, 0);
3137 VM_OBJECT_UNLOCK(vp->v_object);
3139 error = VOP_FSYNC(vp, MNT_WAIT, td);
3141 VOP_UNLOCK(vp, 0, td);
3142 vn_finished_write(mp);
3144 VFS_UNLOCK_GIANT(vfslocked);
3150 * Rename files. Source and destination must either both be directories,
3151 * or both not be directories. If target is a directory, it must be empty.
3153 #ifndef _SYS_SYSPROTO_H_
3154 struct rename_args {
3162 register struct rename_args /* {
3168 return (kern_rename(td, uap->from, uap->to, UIO_USERSPACE));
3172 kern_rename(struct thread *td, char *from, char *to, enum uio_seg pathseg)
3174 struct mount *mp = NULL;
3175 struct vnode *tvp, *fvp, *tdvp;
3176 struct nameidata fromnd, tond;
3183 NDINIT(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | MPSAFE,
3186 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART | MPSAFE,
3189 if ((error = namei(&fromnd)) != 0)
3191 fvfslocked = NDHASGIANT(&fromnd);
3194 error = mac_check_vnode_rename_from(td->td_ucred, fromnd.ni_dvp,
3195 fromnd.ni_vp, &fromnd.ni_cnd);
3196 VOP_UNLOCK(fromnd.ni_dvp, 0, td);
3197 VOP_UNLOCK(fromnd.ni_vp, 0, td);
3201 error = vn_start_write(fvp, &mp, V_WAIT | PCATCH);
3203 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3204 vrele(fromnd.ni_dvp);
3208 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3209 MPSAFE, pathseg, to, td);
3210 if (fromnd.ni_vp->v_type == VDIR)
3211 tond.ni_cnd.cn_flags |= WILLBEDIR;
3212 if ((error = namei(&tond)) != 0) {
3213 /* Translate error code for rename("dir1", "dir2/."). */
3214 if (error == EISDIR && fvp->v_type == VDIR)
3216 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3217 vrele(fromnd.ni_dvp);
3219 vn_finished_write(mp);
3222 tvfslocked = NDHASGIANT(&tond);
3226 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3229 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3237 * If the source is the same as the destination (that is, if they
3238 * are links to the same vnode), then there is nothing to do.
3244 error = mac_check_vnode_rename_to(td->td_ucred, tdvp,
3245 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3249 VOP_LEASE(tdvp, td, td->td_ucred, LEASE_WRITE);
3250 if (fromnd.ni_dvp != tdvp) {
3251 VOP_LEASE(fromnd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3254 VOP_LEASE(tvp, td, td->td_ucred, LEASE_WRITE);
3256 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3257 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3258 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3259 NDFREE(&tond, NDF_ONLY_PNBUF);
3261 NDFREE(&fromnd, NDF_ONLY_PNBUF);
3262 NDFREE(&tond, NDF_ONLY_PNBUF);
3269 vrele(fromnd.ni_dvp);
3272 vrele(tond.ni_startdir);
3273 vn_finished_write(mp);
3275 if (fromnd.ni_startdir)
3276 vrele(fromnd.ni_startdir);
3277 VFS_UNLOCK_GIANT(fvfslocked);
3278 VFS_UNLOCK_GIANT(tvfslocked);
3285 * Make a directory file.
3287 #ifndef _SYS_SYSPROTO_H_
3296 register struct mkdir_args /* {
3302 return (kern_mkdir(td, uap->path, UIO_USERSPACE, uap->mode));
3306 kern_mkdir(struct thread *td, char *path, enum uio_seg segflg, int mode)
3312 struct nameidata nd;
3317 NDINIT(&nd, CREATE, LOCKPARENT | SAVENAME | MPSAFE, segflg, path, td);
3318 nd.ni_cnd.cn_flags |= WILLBEDIR;
3319 if ((error = namei(&nd)) != 0)
3321 vfslocked = NDHASGIANT(&nd);
3324 NDFREE(&nd, NDF_ONLY_PNBUF);
3327 * XXX namei called with LOCKPARENT but not LOCKLEAF has
3328 * the strange behaviour of leaving the vnode unlocked
3329 * if the target is the same vnode as the parent.
3331 if (vp == nd.ni_dvp)
3335 VFS_UNLOCK_GIANT(vfslocked);
3338 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3339 NDFREE(&nd, NDF_ONLY_PNBUF);
3341 VFS_UNLOCK_GIANT(vfslocked);
3342 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3347 vattr.va_type = VDIR;
3348 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
3349 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask;
3350 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
3352 error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3357 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3358 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3362 NDFREE(&nd, NDF_ONLY_PNBUF);
3366 vn_finished_write(mp);
3367 VFS_UNLOCK_GIANT(vfslocked);
3372 * Remove a directory file.
3374 #ifndef _SYS_SYSPROTO_H_
3382 struct rmdir_args /* {
3387 return (kern_rmdir(td, uap->path, UIO_USERSPACE));
3391 kern_rmdir(struct thread *td, char *path, enum uio_seg pathseg)
3396 struct nameidata nd;
3401 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | MPSAFE, pathseg, path, td);
3402 if ((error = namei(&nd)) != 0)
3404 vfslocked = NDHASGIANT(&nd);
3406 if (vp->v_type != VDIR) {
3411 * No rmdir "." please.
3413 if (nd.ni_dvp == vp) {
3418 * The root of a mounted filesystem cannot be deleted.
3420 if (vp->v_vflag & VV_ROOT) {
3425 error = mac_check_vnode_delete(td->td_ucred, nd.ni_dvp, vp,
3430 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3431 NDFREE(&nd, NDF_ONLY_PNBUF);
3432 if (nd.ni_dvp == vp)
3437 VFS_UNLOCK_GIANT(vfslocked);
3438 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3442 VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
3443 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
3444 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3445 vn_finished_write(mp);
3447 NDFREE(&nd, NDF_ONLY_PNBUF);
3448 if (nd.ni_dvp == vp)
3453 VFS_UNLOCK_GIANT(vfslocked);
3459 * Read a block of directory entries in a filesystem independent format.
3461 #ifndef _SYS_SYSPROTO_H_
3462 struct ogetdirentries_args {
3470 ogetdirentries(td, uap)
3472 register struct ogetdirentries_args /* {
3481 struct uio auio, kuio;
3482 struct iovec aiov, kiov;
3483 struct dirent *dp, *edp;
3485 int error, eofflag, readcnt;
3488 /* XXX arbitrary sanity limit on `count'. */
3489 if (uap->count > 64 * 1024)
3491 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3493 if ((fp->f_flag & FREAD) == 0) {
3499 if (vp->v_type != VDIR) {
3503 aiov.iov_base = uap->buf;
3504 aiov.iov_len = uap->count;
3505 auio.uio_iov = &aiov;
3506 auio.uio_iovcnt = 1;
3507 auio.uio_rw = UIO_READ;
3508 auio.uio_segflg = UIO_USERSPACE;
3510 auio.uio_resid = uap->count;
3511 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3512 loff = auio.uio_offset = fp->f_offset;
3514 error = mac_check_vnode_readdir(td->td_ucred, vp);
3516 VOP_UNLOCK(vp, 0, td);
3521 # if (BYTE_ORDER != LITTLE_ENDIAN)
3522 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
3523 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
3525 fp->f_offset = auio.uio_offset;
3530 kuio.uio_iov = &kiov;
3531 kuio.uio_segflg = UIO_SYSSPACE;
3532 kiov.iov_len = uap->count;
3533 MALLOC(dirbuf, caddr_t, uap->count, M_TEMP, M_WAITOK);
3534 kiov.iov_base = dirbuf;
3535 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
3537 fp->f_offset = kuio.uio_offset;
3539 readcnt = uap->count - kuio.uio_resid;
3540 edp = (struct dirent *)&dirbuf[readcnt];
3541 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
3542 # if (BYTE_ORDER == LITTLE_ENDIAN)
3544 * The expected low byte of
3545 * dp->d_namlen is our dp->d_type.
3546 * The high MBZ byte of dp->d_namlen
3547 * is our dp->d_namlen.
3549 dp->d_type = dp->d_namlen;
3553 * The dp->d_type is the high byte
3554 * of the expected dp->d_namlen,
3555 * so must be zero'ed.
3559 if (dp->d_reclen > 0) {
3560 dp = (struct dirent *)
3561 ((char *)dp + dp->d_reclen);
3568 error = uiomove(dirbuf, readcnt, &auio);
3570 FREE(dirbuf, M_TEMP);
3572 VOP_UNLOCK(vp, 0, td);
3577 if (uap->count == auio.uio_resid) {
3578 if (union_dircheckp) {
3579 error = union_dircheckp(td, &vp, fp);
3588 * XXX We could delay dropping the lock above but
3589 * union_dircheckp complicates things.
3591 vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, td);
3592 if ((vp->v_vflag & VV_ROOT) &&
3593 (vp->v_mount->mnt_flag & MNT_UNION)) {
3594 struct vnode *tvp = vp;
3595 vp = vp->v_mount->mnt_vnodecovered;
3603 VOP_UNLOCK(vp, 0, td);
3605 error = copyout(&loff, uap->basep, sizeof(long));
3607 td->td_retval[0] = uap->count - auio.uio_resid;
3610 #endif /* COMPAT_43 */
3613 * Read a block of directory entries in a filesystem independent format.
3615 #ifndef _SYS_SYSPROTO_H_
3616 struct getdirentries_args {
3624 getdirentries(td, uap)
3626 register struct getdirentries_args /* {
3641 if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
3643 if ((fp->f_flag & FREAD) == 0) {
3649 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3650 if (vp->v_type != VDIR) {
3654 aiov.iov_base = uap->buf;
3655 aiov.iov_len = uap->count;
3656 auio.uio_iov = &aiov;
3657 auio.uio_iovcnt = 1;
3658 auio.uio_rw = UIO_READ;
3659 auio.uio_segflg = UIO_USERSPACE;
3661 auio.uio_resid = uap->count;
3662 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
3663 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3664 loff = auio.uio_offset = fp->f_offset;
3666 error = mac_check_vnode_readdir(td->td_ucred, vp);
3669 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
3671 fp->f_offset = auio.uio_offset;
3672 VOP_UNLOCK(vp, 0, td);
3675 if (uap->count == auio.uio_resid) {
3676 if (union_dircheckp) {
3677 error = union_dircheckp(td, &vp, fp);
3679 VFS_UNLOCK_GIANT(vfslocked);
3686 * XXX We could delay dropping the lock above but
3687 * union_dircheckp complicates things.
3689 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3690 if ((vp->v_vflag & VV_ROOT) &&
3691 (vp->v_mount->mnt_flag & MNT_UNION)) {
3692 struct vnode *tvp = vp;
3693 vp = vp->v_mount->mnt_vnodecovered;
3699 VFS_UNLOCK_GIANT(vfslocked);
3702 VOP_UNLOCK(vp, 0, td);
3704 if (uap->basep != NULL) {
3705 error = copyout(&loff, uap->basep, sizeof(long));
3707 td->td_retval[0] = uap->count - auio.uio_resid;
3709 VFS_UNLOCK_GIANT(vfslocked);
3713 #ifndef _SYS_SYSPROTO_H_
3714 struct getdents_args {
3723 register struct getdents_args /* {
3729 struct getdirentries_args ap;
3732 ap.count = uap->count;
3734 return (getdirentries(td, &ap));
3738 * Set the mode mask for creation of filesystem nodes.
3742 #ifndef _SYS_SYSPROTO_H_
3750 struct umask_args /* {
3754 register struct filedesc *fdp;
3756 FILEDESC_LOCK_FAST(td->td_proc->p_fd);
3757 fdp = td->td_proc->p_fd;
3758 td->td_retval[0] = fdp->fd_cmask;
3759 fdp->fd_cmask = uap->newmask & ALLPERMS;
3760 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd);
3765 * Void all references to file by ripping underlying filesystem
3768 #ifndef _SYS_SYSPROTO_H_
3769 struct revoke_args {
3776 register struct revoke_args /* {
3783 struct nameidata nd;
3786 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, UIO_USERSPACE,
3788 if ((error = namei(&nd)) != 0)
3790 vfslocked = NDHASGIANT(&nd);
3792 NDFREE(&nd, NDF_ONLY_PNBUF);
3793 if (vp->v_type != VCHR) {
3798 error = mac_check_vnode_revoke(td->td_ucred, vp);
3802 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
3805 if (td->td_ucred->cr_uid != vattr.va_uid) {
3806 error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL);
3811 VOP_REVOKE(vp, REVOKEALL);
3814 VFS_UNLOCK_GIANT(vfslocked);
3819 * Convert a user file descriptor to a kernel file entry.
3820 * A reference on the file entry is held upon returning.
3823 getvnode(fdp, fd, fpp)
3824 struct filedesc *fdp;
3836 if ((u_int)fd >= fdp->fd_nfiles ||
3837 (fp = fdp->fd_ofiles[fd]) == NULL)
3839 else if (fp->f_vnode == NULL) {
3846 FILEDESC_UNLOCK(fdp);
3853 * Get (NFS) file handle
3855 #ifndef _SYS_SYSPROTO_H_
3856 struct lgetfh_args {
3864 register struct lgetfh_args *uap;
3866 struct nameidata nd;
3868 register struct vnode *vp;
3875 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE,
3876 UIO_USERSPACE, uap->fname, td);
3880 vfslocked = NDHASGIANT(&nd);
3881 NDFREE(&nd, NDF_ONLY_PNBUF);
3883 bzero(&fh, sizeof(fh));
3884 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3885 error = VFS_VPTOFH(vp, &fh.fh_fid);
3887 VFS_UNLOCK_GIANT(vfslocked);
3890 error = copyout(&fh, uap->fhp, sizeof (fh));
3894 #ifndef _SYS_SYSPROTO_H_
3903 register struct getfh_args *uap;
3905 struct nameidata nd;
3907 register struct vnode *vp;
3914 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE,
3915 UIO_USERSPACE, uap->fname, td);
3919 vfslocked = NDHASGIANT(&nd);
3920 NDFREE(&nd, NDF_ONLY_PNBUF);
3922 bzero(&fh, sizeof(fh));
3923 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3924 error = VFS_VPTOFH(vp, &fh.fh_fid);
3926 VFS_UNLOCK_GIANT(vfslocked);
3929 error = copyout(&fh, uap->fhp, sizeof (fh));
3934 * syscall for the rpc.lockd to use to translate a NFS file handle into
3935 * an open descriptor.
3937 * warning: do not remove the suser() call or this becomes one giant
3942 #ifndef _SYS_SYSPROTO_H_
3943 struct fhopen_args {
3944 const struct fhandle *u_fhp;
3951 struct fhopen_args /* {
3952 const struct fhandle *u_fhp;
3956 struct proc *p = td->td_proc;
3961 struct vattr *vap = &vat;
3964 register struct filedesc *fdp = p->p_fd;
3965 int fmode, mode, error, type;
3972 fmode = FFLAGS(uap->flags);
3973 /* why not allow a non-read/write open for our lockd? */
3974 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3976 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3979 /* find the mount point */
3981 mp = vfs_getvfs(&fhp.fh_fsid);
3986 /* now give me my vnode, it gets returned to me locked */
3987 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3991 * from now on we have to make sure not
3992 * to forget about the vnode
3993 * any error that causes an abort must vput(vp)
3994 * just set error = err and 'goto bad;'.
4000 if (vp->v_type == VLNK) {
4004 if (vp->v_type == VSOCK) {
4009 if (fmode & (FWRITE | O_TRUNC)) {
4010 if (vp->v_type == VDIR) {
4014 error = vn_writechk(vp);
4021 if (fmode & O_APPEND)
4024 error = mac_check_vnode_open(td->td_ucred, vp, mode);
4029 error = VOP_ACCESS(vp, mode, td->td_ucred, td);
4033 if (fmode & O_TRUNC) {
4034 VOP_UNLOCK(vp, 0, td); /* XXX */
4035 if ((error = vn_start_write(NULL, &mp, V_WAIT | PCATCH)) != 0) {
4039 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4040 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
4043 * We don't yet have fp->f_cred, so use td->td_ucred, which
4046 error = mac_check_vnode_write(td->td_ucred, td->td_ucred, vp);
4051 error = VOP_SETATTR(vp, vap, td->td_ucred, td);
4055 vn_finished_write(mp);
4059 error = VOP_OPEN(vp, fmode, td->td_ucred, td, -1);
4067 * end of vn_open code
4070 if ((error = falloc(td, &nfp, &indx)) != 0) {
4075 /* An extra reference on `nfp' has been held for us by falloc(). */
4080 nfp->f_flag = fmode & FMASK;
4081 nfp->f_ops = &vnops;
4082 nfp->f_type = DTYPE_VNODE;
4083 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4084 lf.l_whence = SEEK_SET;
4087 if (fmode & O_EXLOCK)
4088 lf.l_type = F_WRLCK;
4090 lf.l_type = F_RDLCK;
4092 if ((fmode & FNONBLOCK) == 0)
4094 VOP_UNLOCK(vp, 0, td);
4095 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
4098 * The lock request failed. Normally close the
4099 * descriptor but handle the case where someone might
4100 * have dup()d or close()d it when we weren't looking.
4102 fdclose(fdp, fp, indx, td);
4105 * release our private reference
4110 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4111 fp->f_flag |= FHASLOCK;
4114 VOP_UNLOCK(vp, 0, td);
4117 td->td_retval[0] = indx;
4128 * Stat an (NFS) file handle.
4132 #ifndef _SYS_SYSPROTO_H_
4133 struct fhstat_args {
4134 struct fhandle *u_fhp;
4141 register struct fhstat_args /* {
4142 struct fhandle *u_fhp;
4155 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4159 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4163 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp))) {
4167 error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td);
4172 error = copyout(&sb, uap->sb, sizeof(sb));
4177 * Implement fstatfs() for (NFS) file handles.
4181 #ifndef _SYS_SYSPROTO_H_
4182 struct fhstatfs_args {
4183 struct fhandle *u_fhp;
4190 struct fhstatfs_args /* {
4191 struct fhandle *u_fhp;
4199 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4202 error = kern_fhstatfs(td, fh, &sf);
4205 return (copyout(&sf, uap->buf, sizeof(sf)));
4209 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4220 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4224 error = VFS_FHTOVP(mp, &fh.fh_fid, &vp);
4232 error = prison_canseemount(td->td_ucred, mp);
4236 error = mac_check_mount_stat(td->td_ucred, mp);
4243 * Set these in case the underlying filesystem fails to do so.
4245 sp->f_version = STATFS_VERSION;
4246 sp->f_namemax = NAME_MAX;
4247 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4248 error = VFS_STATFS(mp, sp, td);
4257 * Syscall to push extended attribute configuration information into the
4258 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4259 * a command (int cmd), and attribute name and misc data. For now, the
4260 * attribute name is left in userspace for consumption by the VFS_op.
4261 * It will probably be changed to be copied into sysspace by the
4262 * syscall in the future, once issues with various consumers of the
4263 * attribute code have raised their hands.
4265 * Currently this is used only by UFS Extended Attributes.
4270 struct extattrctl_args /* {
4273 const char *filename;
4275 const char *attrname;
4278 struct vnode *filename_vp;
4279 struct nameidata nd;
4280 struct mount *mp, *mp_writable;
4281 char attrname[EXTATTR_MAXNAMELEN];
4282 int vfslocked, fnvfslocked, error;
4285 * uap->attrname is not always defined. We check again later when we
4286 * invoke the VFS call so as to pass in NULL there if needed.
4288 if (uap->attrname != NULL) {
4289 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4295 vfslocked = fnvfslocked = 0;
4297 * uap->filename is not always defined. If it is, grab a vnode lock,
4298 * which VFS_EXTATTRCTL() will later release.
4301 if (uap->filename != NULL) {
4302 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW | LOCKLEAF,
4303 UIO_USERSPACE, uap->filename, td);
4307 fnvfslocked = NDHASGIANT(&nd);
4308 filename_vp = nd.ni_vp;
4309 NDFREE(&nd, NDF_NO_VP_RELE | NDF_NO_VP_UNLOCK);
4312 /* uap->path is always defined. */
4313 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4316 if (filename_vp != NULL)
4320 vfslocked = NDHASGIANT(&nd);
4321 mp = nd.ni_vp->v_mount;
4322 error = vn_start_write(nd.ni_vp, &mp_writable, V_WAIT | PCATCH);
4325 if (filename_vp != NULL)
4330 error = VFS_EXTATTRCTL(mp, uap->cmd, filename_vp, uap->attrnamespace,
4331 uap->attrname != NULL ? attrname : NULL, td);
4333 vn_finished_write(mp_writable);
4335 * VFS_EXTATTRCTL will have unlocked, but not de-ref'd,
4336 * filename_vp, so vrele it if it is defined.
4338 if (filename_vp != NULL)
4341 VFS_UNLOCK_GIANT(fnvfslocked);
4342 VFS_UNLOCK_GIANT(vfslocked);
4347 * Set a named extended attribute on a file or directory
4349 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4350 * kernelspace string pointer "attrname", userspace buffer
4351 * pointer "data", buffer length "nbytes", thread "td".
4352 * Returns: 0 on success, an error number otherwise
4354 * References: vp must be a valid reference for the duration of the call
4357 extattr_set_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4358 void *data, size_t nbytes, struct thread *td)
4366 VFS_ASSERT_GIANT(vp->v_mount);
4367 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4370 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4371 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4373 aiov.iov_base = data;
4374 aiov.iov_len = nbytes;
4375 auio.uio_iov = &aiov;
4376 auio.uio_iovcnt = 1;
4377 auio.uio_offset = 0;
4378 if (nbytes > INT_MAX) {
4382 auio.uio_resid = nbytes;
4383 auio.uio_rw = UIO_WRITE;
4384 auio.uio_segflg = UIO_USERSPACE;
4389 error = mac_check_vnode_setextattr(td->td_ucred, vp, attrnamespace,
4395 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio,
4397 cnt -= auio.uio_resid;
4398 td->td_retval[0] = cnt;
4401 VOP_UNLOCK(vp, 0, td);
4402 vn_finished_write(mp);
4407 extattr_set_fd(td, uap)
4409 struct extattr_set_fd_args /* {
4412 const char *attrname;
4418 char attrname[EXTATTR_MAXNAMELEN];
4419 int vfslocked, error;
4421 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4425 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4429 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
4430 error = extattr_set_vp(fp->f_vnode, uap->attrnamespace,
4431 attrname, uap->data, uap->nbytes, td);
4433 VFS_UNLOCK_GIANT(vfslocked);
4439 extattr_set_file(td, uap)
4441 struct extattr_set_file_args /* {
4444 const char *attrname;
4449 struct nameidata nd;
4450 char attrname[EXTATTR_MAXNAMELEN];
4451 int vfslocked, error;
4453 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4457 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4461 NDFREE(&nd, NDF_ONLY_PNBUF);
4463 vfslocked = NDHASGIANT(&nd);
4464 error = extattr_set_vp(nd.ni_vp, uap->attrnamespace, attrname,
4465 uap->data, uap->nbytes, td);
4468 VFS_UNLOCK_GIANT(vfslocked);
4473 extattr_set_link(td, uap)
4475 struct extattr_set_link_args /* {
4478 const char *attrname;
4483 struct nameidata nd;
4484 char attrname[EXTATTR_MAXNAMELEN];
4485 int vfslocked, error;
4487 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4491 NDINIT(&nd, LOOKUP, MPSAFE | NOFOLLOW, UIO_USERSPACE, uap->path, td);
4495 NDFREE(&nd, NDF_ONLY_PNBUF);
4497 vfslocked = NDHASGIANT(&nd);
4498 error = extattr_set_vp(nd.ni_vp, uap->attrnamespace, attrname,
4499 uap->data, uap->nbytes, td);
4502 VFS_UNLOCK_GIANT(vfslocked);
4507 * Get a named extended attribute on a file or directory
4509 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4510 * kernelspace string pointer "attrname", userspace buffer
4511 * pointer "data", buffer length "nbytes", thread "td".
4512 * Returns: 0 on success, an error number otherwise
4514 * References: vp must be a valid reference for the duration of the call
4517 extattr_get_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4518 void *data, size_t nbytes, struct thread *td)
4520 struct uio auio, *auiop;
4523 size_t size, *sizep;
4526 VFS_ASSERT_GIANT(vp->v_mount);
4527 VOP_LEASE(vp, td, td->td_ucred, LEASE_READ);
4528 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4531 * Slightly unusual semantics: if the user provides a NULL data
4532 * pointer, they don't want to receive the data, just the
4533 * maximum read length.
4539 aiov.iov_base = data;
4540 aiov.iov_len = nbytes;
4541 auio.uio_iov = &aiov;
4542 auio.uio_iovcnt = 1;
4543 auio.uio_offset = 0;
4544 if (nbytes > INT_MAX) {
4548 auio.uio_resid = nbytes;
4549 auio.uio_rw = UIO_READ;
4550 auio.uio_segflg = UIO_USERSPACE;
4558 error = mac_check_vnode_getextattr(td->td_ucred, vp, attrnamespace,
4564 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, auiop, sizep,
4567 if (auiop != NULL) {
4568 cnt -= auio.uio_resid;
4569 td->td_retval[0] = cnt;
4571 td->td_retval[0] = size;
4574 VOP_UNLOCK(vp, 0, td);
4579 extattr_get_fd(td, uap)
4581 struct extattr_get_fd_args /* {
4584 const char *attrname;
4590 char attrname[EXTATTR_MAXNAMELEN];
4591 int vfslocked, error;
4593 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4597 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4601 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
4602 error = extattr_get_vp(fp->f_vnode, uap->attrnamespace,
4603 attrname, uap->data, uap->nbytes, td);
4606 VFS_UNLOCK_GIANT(vfslocked);
4611 extattr_get_file(td, uap)
4613 struct extattr_get_file_args /* {
4616 const char *attrname;
4621 struct nameidata nd;
4622 char attrname[EXTATTR_MAXNAMELEN];
4623 int vfslocked, error;
4625 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4629 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4633 NDFREE(&nd, NDF_ONLY_PNBUF);
4635 vfslocked = NDHASGIANT(&nd);
4636 error = extattr_get_vp(nd.ni_vp, uap->attrnamespace, attrname,
4637 uap->data, uap->nbytes, td);
4640 VFS_UNLOCK_GIANT(vfslocked);
4645 extattr_get_link(td, uap)
4647 struct extattr_get_link_args /* {
4650 const char *attrname;
4655 struct nameidata nd;
4656 char attrname[EXTATTR_MAXNAMELEN];
4657 int vfslocked, error;
4659 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4663 NDINIT(&nd, LOOKUP, MPSAFE | NOFOLLOW, UIO_USERSPACE, uap->path, td);
4667 NDFREE(&nd, NDF_ONLY_PNBUF);
4669 vfslocked = NDHASGIANT(&nd);
4670 error = extattr_get_vp(nd.ni_vp, uap->attrnamespace, attrname,
4671 uap->data, uap->nbytes, td);
4674 VFS_UNLOCK_GIANT(vfslocked);
4679 * extattr_delete_vp(): Delete a named extended attribute on a file or
4682 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
4683 * kernelspace string pointer "attrname", proc "p"
4684 * Returns: 0 on success, an error number otherwise
4686 * References: vp must be a valid reference for the duration of the call
4689 extattr_delete_vp(struct vnode *vp, int attrnamespace, const char *attrname,
4695 VFS_ASSERT_GIANT(vp->v_mount);
4696 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
4699 VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
4700 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4703 error = mac_check_vnode_deleteextattr(td->td_ucred, vp, attrnamespace,
4709 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, td->td_ucred,
4711 if (error == EOPNOTSUPP)
4712 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
4717 VOP_UNLOCK(vp, 0, td);
4718 vn_finished_write(mp);
4723 extattr_delete_fd(td, uap)
4725 struct extattr_delete_fd_args /* {
4728 const char *attrname;
4732 char attrname[EXTATTR_MAXNAMELEN];
4733 int vfslocked, error;
4735 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4739 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4743 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
4744 error = extattr_delete_vp(fp->f_vnode, uap->attrnamespace,
4747 VFS_UNLOCK_GIANT(vfslocked);
4752 extattr_delete_file(td, uap)
4754 struct extattr_delete_file_args /* {
4757 const char *attrname;
4760 struct nameidata nd;
4761 char attrname[EXTATTR_MAXNAMELEN];
4762 int vfslocked, error;
4764 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4768 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4772 NDFREE(&nd, NDF_ONLY_PNBUF);
4774 vfslocked = NDHASGIANT(&nd);
4775 error = extattr_delete_vp(nd.ni_vp, uap->attrnamespace, attrname, td);
4777 VFS_UNLOCK_GIANT(vfslocked);
4782 extattr_delete_link(td, uap)
4784 struct extattr_delete_link_args /* {
4787 const char *attrname;
4790 struct nameidata nd;
4791 char attrname[EXTATTR_MAXNAMELEN];
4792 int vfslocked, error;
4794 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN, NULL);
4798 NDINIT(&nd, LOOKUP, MPSAFE | NOFOLLOW, UIO_USERSPACE, uap->path, td);
4802 NDFREE(&nd, NDF_ONLY_PNBUF);
4804 vfslocked = NDHASGIANT(&nd);
4805 error = extattr_delete_vp(nd.ni_vp, uap->attrnamespace, attrname, td);
4807 VFS_UNLOCK_GIANT(vfslocked);
4812 * Retrieve a list of extended attributes on a file or directory.
4814 * Arguments: unlocked vnode "vp", attribute namespace 'attrnamespace",
4815 * userspace buffer pointer "data", buffer length "nbytes",
4817 * Returns: 0 on success, an error number otherwise
4819 * References: vp must be a valid reference for the duration of the call
4822 extattr_list_vp(struct vnode *vp, int attrnamespace, void *data,
4823 size_t nbytes, struct thread *td)
4825 struct uio auio, *auiop;
4826 size_t size, *sizep;
4831 VFS_ASSERT_GIANT(vp->v_mount);
4832 VOP_LEASE(vp, td, td->td_ucred, LEASE_READ);
4833 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4839 aiov.iov_base = data;
4840 aiov.iov_len = nbytes;
4841 auio.uio_iov = &aiov;
4842 auio.uio_iovcnt = 1;
4843 auio.uio_offset = 0;
4844 if (nbytes > INT_MAX) {
4848 auio.uio_resid = nbytes;
4849 auio.uio_rw = UIO_READ;
4850 auio.uio_segflg = UIO_USERSPACE;
4858 error = mac_check_vnode_listextattr(td->td_ucred, vp, attrnamespace);
4863 error = VOP_LISTEXTATTR(vp, attrnamespace, auiop, sizep,
4866 if (auiop != NULL) {
4867 cnt -= auio.uio_resid;
4868 td->td_retval[0] = cnt;
4870 td->td_retval[0] = size;
4873 VOP_UNLOCK(vp, 0, td);
4879 extattr_list_fd(td, uap)
4881 struct extattr_list_fd_args /* {
4889 int vfslocked, error;
4891 error = getvnode(td->td_proc->p_fd, uap->fd, &fp);
4895 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
4896 error = extattr_list_vp(fp->f_vnode, uap->attrnamespace, uap->data,
4900 VFS_UNLOCK_GIANT(vfslocked);
4905 extattr_list_file(td, uap)
4907 struct extattr_list_file_args /* {
4914 struct nameidata nd;
4915 int vfslocked, error;
4917 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_USERSPACE, uap->path, td);
4921 NDFREE(&nd, NDF_ONLY_PNBUF);
4923 vfslocked = NDHASGIANT(&nd);
4924 error = extattr_list_vp(nd.ni_vp, uap->attrnamespace, uap->data,
4928 VFS_UNLOCK_GIANT(vfslocked);
4933 extattr_list_link(td, uap)
4935 struct extattr_list_link_args /* {
4942 struct nameidata nd;
4943 int vfslocked, error;
4945 NDINIT(&nd, LOOKUP, MPSAFE | NOFOLLOW, UIO_USERSPACE, uap->path, td);
4949 NDFREE(&nd, NDF_ONLY_PNBUF);
4951 vfslocked = NDHASGIANT(&nd);
4952 error = extattr_list_vp(nd.ni_vp, uap->attrnamespace, uap->data,
4956 VFS_UNLOCK_GIANT(vfslocked);