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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.30 2004/03/16 17:53:53 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/sysent.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/sysproto.h>
50 #include <sys/filedesc.h>
51 #include <sys/kernel.h>
52 #include <sys/fcntl.h>
54 #include <sys/linker.h>
56 #include <sys/unistd.h>
57 #include <sys/vnode.h>
59 #include <sys/namei.h>
60 #include <sys/dirent.h>
61 #include <sys/extattr.h>
62 #include <sys/kern_syscall.h>
64 #include <machine/limits.h>
65 #include <vfs/union/union.h>
66 #include <sys/sysctl.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_zone.h>
70 #include <vm/vm_page.h>
72 #include <sys/file2.h>
74 static int checkvp_chdir (struct vnode *vn, struct thread *td);
75 static void checkdirs (struct vnode *olddp);
76 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
77 static int getutimes (const struct timeval *, struct timespec *);
78 static int setfown (struct vnode *, uid_t, gid_t);
79 static int setfmode (struct vnode *, int);
80 static int setfflags (struct vnode *, int);
81 static int setutimes (struct vnode *, const struct timespec *, int);
82 static int usermount = 0; /* if 1, non-root can mount fs. */
84 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
86 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
89 * Virtual File System System Calls
93 * Mount a file system.
96 * mount_args(char *type, char *path, int flags, caddr_t data)
100 mount(struct mount_args *uap)
102 struct thread *td = curthread;
103 struct proc *p = td->td_proc;
106 struct vfsconf *vfsp;
107 int error, flag = 0, flag2 = 0;
110 char fstypename[MFSNAMELEN];
114 if (usermount == 0 && (error = suser(td)))
117 * Do not allow NFS export by non-root users.
119 if (SCARG(uap, flags) & MNT_EXPORTED) {
125 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
128 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
130 * Get vnode to be covered
132 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
133 SCARG(uap, path), td);
134 if ((error = namei(&nd)) != 0)
136 NDFREE(&nd, NDF_ONLY_PNBUF);
138 if (SCARG(uap, flags) & MNT_UPDATE) {
139 if ((vp->v_flag & VROOT) == 0) {
145 flag2 = mp->mnt_kern_flag;
147 * We only allow the filesystem to be reloaded if it
148 * is currently mounted read-only.
150 if ((SCARG(uap, flags) & MNT_RELOAD) &&
151 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
153 return (EOPNOTSUPP); /* Needs translation */
156 * Only root, or the user that did the original mount is
157 * permitted to update it.
159 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
160 (error = suser(td))) {
164 if (vfs_busy(mp, LK_NOWAIT, NULL, td)) {
168 lwkt_gettoken(&vlock, vp->v_interlock);
169 if ((vp->v_flag & VMOUNT) != 0 ||
170 vp->v_mountedhere != NULL) {
171 lwkt_reltoken(&vlock);
176 vp->v_flag |= VMOUNT;
177 lwkt_reltoken(&vlock);
179 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
180 VOP_UNLOCK(vp, NULL, 0, td);
184 * If the user is not root, ensure that they own the directory
185 * onto which we are attempting to mount.
187 if ((error = VOP_GETATTR(vp, &va, td)) ||
188 (va.va_uid != p->p_ucred->cr_uid &&
189 (error = suser(td)))) {
193 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
197 if (vp->v_type != VDIR) {
201 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
205 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
206 if (!strcmp(vfsp->vfc_name, fstypename))
211 /* Only load modules for root (very important!) */
212 if ((error = suser(td)) != 0) {
216 error = linker_load_file(fstypename, &lf);
217 if (error || lf == NULL) {
224 /* lookup again, see if the VFS was loaded */
225 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
226 if (!strcmp(vfsp->vfc_name, fstypename))
230 linker_file_unload(lf);
235 lwkt_gettoken(&vlock, vp->v_interlock);
236 if ((vp->v_flag & VMOUNT) != 0 ||
237 vp->v_mountedhere != NULL) {
238 lwkt_reltoken(&vlock);
242 vp->v_flag |= VMOUNT;
243 lwkt_reltoken(&vlock);
246 * Allocate and initialize the filesystem.
248 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
249 bzero((char *)mp, (u_long)sizeof(struct mount));
250 TAILQ_INIT(&mp->mnt_nvnodelist);
251 TAILQ_INIT(&mp->mnt_reservedvnlist);
252 mp->mnt_nvnodelistsize = 0;
253 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
254 vfs_busy(mp, LK_NOWAIT, NULL, td);
255 mp->mnt_op = vfsp->vfc_vfsops;
257 vfsp->vfc_refcount++;
258 mp->mnt_stat.f_type = vfsp->vfc_typenum;
259 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
260 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
261 mp->mnt_vnodecovered = vp;
262 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
263 mp->mnt_iosize_max = DFLTPHYS;
264 VOP_UNLOCK(vp, NULL, 0, td);
267 * Set the mount level flags.
269 if (SCARG(uap, flags) & MNT_RDONLY)
270 mp->mnt_flag |= MNT_RDONLY;
271 else if (mp->mnt_flag & MNT_RDONLY)
272 mp->mnt_kern_flag |= MNTK_WANTRDWR;
273 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
274 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
275 MNT_NOSYMFOLLOW | MNT_IGNORE |
276 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
277 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
278 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
279 MNT_NOSYMFOLLOW | MNT_IGNORE |
280 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
282 * Mount the filesystem.
283 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
284 * get. No freeing of cn_pnbuf.
286 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
287 if (mp->mnt_flag & MNT_UPDATE) {
288 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
289 mp->mnt_flag &= ~MNT_RDONLY;
290 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
291 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
294 mp->mnt_kern_flag = flag2;
297 lwkt_gettoken(&vlock, vp->v_interlock);
298 vp->v_flag &= ~VMOUNT;
299 lwkt_reltoken(&vlock);
303 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
305 * Put the new filesystem on the mount list after root.
309 lwkt_gettoken(&vlock, vp->v_interlock);
310 vp->v_flag &= ~VMOUNT;
311 vp->v_mountedhere = mp;
312 lwkt_reltoken(&vlock);
313 lwkt_gettoken(&ilock, &mountlist_token);
314 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
315 lwkt_reltoken(&ilock);
317 VOP_UNLOCK(vp, NULL, 0, td);
318 error = vfs_allocate_syncvnode(mp);
320 if ((error = VFS_START(mp, 0, td)) != 0)
323 lwkt_gettoken(&vlock, vp->v_interlock);
324 vp->v_flag &= ~VMOUNT;
325 lwkt_reltoken(&vlock);
326 mp->mnt_vfc->vfc_refcount--;
328 free((caddr_t)mp, M_MOUNT);
335 * Scan all active processes to see if any of them have a current
336 * or root directory onto which the new filesystem has just been
337 * mounted. If so, replace them with the new mount point.
340 checkdirs(struct vnode *olddp)
342 struct filedesc *fdp;
346 if (olddp->v_usecount == 1)
348 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
349 panic("mount: lost mount");
350 FOREACH_PROC_IN_SYSTEM(p) {
352 if (fdp->fd_cdir == olddp) {
355 fdp->fd_cdir = newdp;
357 if (fdp->fd_rdir == olddp) {
360 fdp->fd_rdir = newdp;
363 if (rootvnode == olddp) {
367 vfs_cache_setroot(rootvnode);
373 * Unmount a file system.
375 * Note: unmount takes a path to the vnode mounted on as argument,
376 * not special file (as before).
379 * umount_args(char *path, int flags)
383 unmount(struct unmount_args *uap)
385 struct thread *td = curthread;
386 struct proc *p = td->td_proc;
393 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
394 SCARG(uap, path), td);
395 if ((error = namei(&nd)) != 0)
398 NDFREE(&nd, NDF_ONLY_PNBUF);
402 * Only root, or the user that did the original mount is
403 * permitted to unmount this filesystem.
405 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
406 (error = suser(td))) {
412 * Don't allow unmounting the root file system.
414 if (mp->mnt_flag & MNT_ROOTFS) {
420 * Must be the root of the filesystem
422 if ((vp->v_flag & VROOT) == 0) {
427 return (dounmount(mp, SCARG(uap, flags), td));
431 * Do the actual file system unmount.
434 dounmount(struct mount *mp, int flags, struct thread *td)
436 struct vnode *coveredvp;
441 lwkt_gettoken(&ilock, &mountlist_token);
442 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
443 lwkt_reltoken(&ilock);
446 mp->mnt_kern_flag |= MNTK_UNMOUNT;
447 /* Allow filesystems to detect that a forced unmount is in progress. */
448 if (flags & MNT_FORCE)
449 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
450 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
451 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
453 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
454 if (mp->mnt_kern_flag & MNTK_MWAIT)
459 if (mp->mnt_flag & MNT_EXPUBLIC)
460 vfs_setpublicfs(NULL, NULL, NULL);
462 vfs_msync(mp, MNT_WAIT);
463 async_flag = mp->mnt_flag & MNT_ASYNC;
464 mp->mnt_flag &=~ MNT_ASYNC;
465 cache_purgevfs(mp); /* remove cache entries for this file sys */
466 if (mp->mnt_syncer != NULL)
467 vrele(mp->mnt_syncer);
468 if (((mp->mnt_flag & MNT_RDONLY) ||
469 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
471 error = VFS_UNMOUNT(mp, flags, td);
472 lwkt_gettokref(&ilock);
474 if (mp->mnt_syncer == NULL)
475 vfs_allocate_syncvnode(mp);
476 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
477 mp->mnt_flag |= async_flag;
478 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
480 if (mp->mnt_kern_flag & MNTK_MWAIT)
484 TAILQ_REMOVE(&mountlist, mp, mnt_list);
485 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
486 coveredvp->v_mountedhere = NULL;
489 mp->mnt_vfc->vfc_refcount--;
490 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
491 panic("unmount: dangling vnode");
492 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
493 if (mp->mnt_kern_flag & MNTK_MWAIT)
495 free((caddr_t)mp, M_MOUNT);
500 * Sync each mounted filesystem.
504 static int syncprt = 0;
505 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
510 sync(struct sync_args *uap)
512 struct thread *td = curthread;
513 struct mount *mp, *nmp;
517 lwkt_gettoken(&ilock, &mountlist_token);
518 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
519 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
520 nmp = TAILQ_NEXT(mp, mnt_list);
523 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
524 asyncflag = mp->mnt_flag & MNT_ASYNC;
525 mp->mnt_flag &= ~MNT_ASYNC;
526 vfs_msync(mp, MNT_NOWAIT);
527 VFS_SYNC(mp, MNT_NOWAIT, td);
528 mp->mnt_flag |= asyncflag;
530 lwkt_gettokref(&ilock);
531 nmp = TAILQ_NEXT(mp, mnt_list);
534 lwkt_reltoken(&ilock);
537 * XXX don't call vfs_bufstats() yet because that routine
538 * was not imported in the Lite2 merge.
543 #endif /* DIAGNOSTIC */
548 /* XXX PRISON: could be per prison flag */
549 static int prison_quotas;
551 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
555 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
557 * Change filesystem quotas.
561 quotactl(struct quotactl_args *uap)
563 struct thread *td = curthread;
564 struct proc *p = td->td_proc;
570 if (p->p_ucred->cr_prison && !prison_quotas)
572 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
573 SCARG(uap, path), td);
574 if ((error = namei(&nd)) != 0)
576 mp = nd.ni_vp->v_mount;
577 NDFREE(&nd, NDF_ONLY_PNBUF);
579 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
580 SCARG(uap, arg), td));
584 kern_statfs(struct nameidata *nd, struct statfs *buf)
586 struct thread *td = curthread;
594 mp = nd->ni_vp->v_mount;
596 NDFREE(nd, NDF_ONLY_PNBUF);
598 error = VFS_STATFS(mp, sp, td);
601 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
602 bcopy(sp, buf, sizeof(*buf));
603 /* Only root should have access to the fsid's. */
605 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
610 * statfs_args(char *path, struct statfs *buf)
612 * Get filesystem statistics.
615 statfs(struct statfs_args *uap)
617 struct thread *td = curthread;
622 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
624 error = kern_statfs(&nd, &buf);
627 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
632 kern_fstatfs(int fd, struct statfs *buf)
634 struct thread *td = curthread;
635 struct proc *p = td->td_proc;
642 error = getvnode(p->p_fd, fd, &fp);
645 mp = ((struct vnode *)fp->f_data)->v_mount;
649 error = VFS_STATFS(mp, sp, td);
652 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
653 bcopy(sp, buf, sizeof(*buf));
654 /* Only root should have access to the fsid's. */
656 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
661 * fstatfs_args(int fd, struct statfs *buf)
663 * Get filesystem statistics.
666 fstatfs(struct fstatfs_args *uap)
671 error = kern_fstatfs(uap->fd, &buf);
674 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
679 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
681 * Get statistics on all filesystems.
685 getfsstat(struct getfsstat_args *uap)
687 struct thread *td = curthread;
688 struct mount *mp, *nmp;
692 long count, maxcount, error;
694 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
695 sfsp = (caddr_t)SCARG(uap, buf);
697 lwkt_gettoken(&ilock, &mountlist_token);
698 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
699 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
700 nmp = TAILQ_NEXT(mp, mnt_list);
703 if (sfsp && count < maxcount) {
706 * If MNT_NOWAIT or MNT_LAZY is specified, do not
707 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
708 * overrides MNT_WAIT.
710 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
711 (SCARG(uap, flags) & MNT_WAIT)) &&
712 (error = VFS_STATFS(mp, sp, td))) {
713 lwkt_gettokref(&ilock);
714 nmp = TAILQ_NEXT(mp, mnt_list);
718 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
719 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
727 lwkt_gettokref(&ilock);
728 nmp = TAILQ_NEXT(mp, mnt_list);
731 lwkt_reltoken(&ilock);
732 if (sfsp && count > maxcount)
733 uap->sysmsg_result = maxcount;
735 uap->sysmsg_result = count;
740 * fchdir_args(int fd)
742 * Change current working directory to a given file descriptor.
746 fchdir(struct fchdir_args *uap)
748 struct thread *td = curthread;
749 struct proc *p = td->td_proc;
750 struct filedesc *fdp = p->p_fd;
751 struct vnode *vp, *tdp;
756 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
758 vp = (struct vnode *)fp->f_data;
760 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
761 if (vp->v_type != VDIR)
764 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
765 while (!error && (mp = vp->v_mountedhere) != NULL) {
766 if (vfs_busy(mp, 0, NULL, td))
768 error = VFS_ROOT(mp, &tdp);
779 VOP_UNLOCK(vp, NULL, 0, td);
786 kern_chdir(struct nameidata *nd)
788 struct thread *td = curthread;
789 struct proc *p = td->td_proc;
790 struct filedesc *fdp = p->p_fd;
793 if ((error = namei(nd)) != 0)
795 if ((error = checkvp_chdir(nd->ni_vp, td)) == 0) {
797 fdp->fd_cdir = nd->ni_vp;
800 NDFREE(nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
805 * chdir_args(char *path)
807 * Change current working directory (``.'').
810 chdir(struct chdir_args *uap)
812 struct thread *td = curthread;
816 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
819 error = kern_chdir(&nd);
825 * Helper function for raised chroot(2) security function: Refuse if
826 * any filedescriptors are open directories.
829 chroot_refuse_vdir_fds(fdp)
830 struct filedesc *fdp;
837 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
838 error = getvnode(fdp, fd, &fp);
841 vp = (struct vnode *)fp->f_data;
842 if (vp->v_type != VDIR)
850 * This sysctl determines if we will allow a process to chroot(2) if it
851 * has a directory open:
852 * 0: disallowed for all processes.
853 * 1: allowed for processes that were not already chroot(2)'ed.
854 * 2: allowed for all processes.
857 static int chroot_allow_open_directories = 1;
859 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
860 &chroot_allow_open_directories, 0, "");
863 * Chroot to the specified vnode. vp must be locked and referenced on
864 * call, and will be left locked and referenced on return. This routine
865 * may acquire additional refs on the vnode when associating it with
866 * the process's root and/or jail dirs.
869 kern_chroot(struct vnode *vp)
871 struct thread *td = curthread;
872 struct proc *p = td->td_proc;
873 struct filedesc *fdp = p->p_fd;
877 * Only root can chroot
879 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
883 * Disallow open directory descriptors (fchdir() breakouts).
885 if (chroot_allow_open_directories == 0 ||
886 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
887 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
892 * Check the validity of vp as a directory to change to and
893 * associate it with rdir/jdir.
895 if ((error = checkvp_chdir(vp, td)) == 0) {
899 if (fdp->fd_jdir == NULL) {
908 * chroot_args(char *path)
910 * Change notion of root (``/'') directory.
914 chroot(struct chroot_args *uap)
916 struct thread *td = curthread;
920 KKASSERT(td->td_proc);
921 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
922 SCARG(uap, path), td);
923 if ((error = namei(&nd)) == 0) {
924 error = kern_chroot(nd.ni_vp);
925 NDFREE(&nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
931 * Common routine for chroot and chdir. Given a locked, referenced vnode,
932 * determine whether it is legal to chdir to the vnode. The vnode's state
933 * is not changed by this call.
936 checkvp_chdir(struct vnode *vp, struct thread *td)
940 if (vp->v_type != VDIR)
943 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
948 kern_open(struct nameidata *nd, int oflags, int mode, int *res)
950 struct thread *td = curthread;
951 struct proc *p = td->td_proc;
952 struct filedesc *fdp = p->p_fd;
957 int type, indx, error;
960 if ((oflags & O_ACCMODE) == O_ACCMODE)
962 flags = FFLAGS(oflags);
963 error = falloc(p, &nfp, &indx);
967 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
968 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
970 * Bump the ref count to prevent another process from closing
971 * the descriptor while we are blocked in vn_open()
974 error = vn_open(nd, flags, cmode);
977 * release our own reference
982 * handle special fdopen() case. bleh. dupfdopen() is
983 * responsible for dropping the old contents of ofiles[indx]
986 if ((error == ENODEV || error == ENXIO) &&
987 p->p_dupfd >= 0 && /* XXX from fdopen */
989 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
994 * Clean up the descriptor, but only if another thread hadn't
995 * replaced or closed it.
997 if (fdp->fd_ofiles[indx] == fp) {
998 fdp->fd_ofiles[indx] = NULL;
1002 if (error == ERESTART)
1007 NDFREE(nd, NDF_ONLY_PNBUF);
1011 * There should be 2 references on the file, one from the descriptor
1012 * table, and one for us.
1014 * Handle the case where someone closed the file (via its file
1015 * descriptor) while we were blocked. The end result should look
1016 * like opening the file succeeded but it was immediately closed.
1018 if (fp->f_count == 1) {
1019 KASSERT(fdp->fd_ofiles[indx] != fp,
1020 ("Open file descriptor lost all refs"));
1021 VOP_UNLOCK(vp, NULL, 0, td);
1022 vn_close(vp, flags & FMASK, td);
1028 fp->f_data = (caddr_t)vp;
1029 fp->f_flag = flags & FMASK;
1031 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1032 if (flags & (O_EXLOCK | O_SHLOCK)) {
1033 lf.l_whence = SEEK_SET;
1036 if (flags & O_EXLOCK)
1037 lf.l_type = F_WRLCK;
1039 lf.l_type = F_RDLCK;
1041 if ((flags & FNONBLOCK) == 0)
1043 VOP_UNLOCK(vp, NULL, 0, td);
1044 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1046 * lock request failed. Normally close the descriptor
1047 * but handle the case where someone might have dup()d
1048 * it when we weren't looking. One reference is
1049 * owned by the descriptor array, the other by us.
1051 if (fdp->fd_ofiles[indx] == fp) {
1052 fdp->fd_ofiles[indx] = NULL;
1058 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1059 fp->f_flag |= FHASLOCK;
1061 /* assert that vn_open created a backing object if one is needed */
1062 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1063 ("open: vmio vnode has no backing object after vn_open"));
1064 VOP_UNLOCK(vp, NULL, 0, td);
1067 * release our private reference, leaving the one associated with the
1068 * descriptor table intact.
1076 * open_args(char *path, int flags, int mode)
1078 * Check permissions, allocate an open file structure,
1079 * and call the device open routine if any.
1082 open(struct open_args *uap)
1084 struct thread *td = curthread;
1085 struct nameidata nd;
1088 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1090 error = kern_open(&nd, uap->flags, uap->mode, &uap->sysmsg_result);
1096 kern_mknod(struct nameidata *nd, int mode, int dev)
1098 struct thread *td = curthread;
1099 struct proc *p = td->td_proc;
1107 switch (mode & S_IFMT) {
1113 error = suser_cred(p->p_ucred, PRISON_ROOT);
1127 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1128 vattr.va_rdev = dev;
1131 switch (mode & S_IFMT) {
1132 case S_IFMT: /* used by badsect to flag bad sectors */
1133 vattr.va_type = VBAD;
1136 vattr.va_type = VCHR;
1139 vattr.va_type = VBLK;
1150 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1152 error = VOP_WHITEOUT(nd->ni_dvp, NCPNULL,
1153 &nd->ni_cnd, NAMEI_CREATE);
1155 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp,
1156 &nd->ni_cnd, &vattr);
1160 NDFREE(nd, NDF_ONLY_PNBUF);
1163 NDFREE(nd, NDF_ONLY_PNBUF);
1164 if (nd->ni_dvp == vp)
1171 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mknod");
1172 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mknod");
1177 * mknod_args(char *path, int mode, int dev)
1179 * Create a special file.
1182 mknod(struct mknod_args *uap)
1184 struct thread *td = curthread;
1185 struct nameidata nd;
1188 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1191 error = kern_mknod(&nd, uap->mode, uap->dev);
1197 kern_mkfifo(struct nameidata *nd, int mode)
1199 struct thread *td = curthread;
1200 struct proc *p = td->td_proc;
1208 if (nd->ni_vp != NULL) {
1209 NDFREE(nd, NDF_ONLY_PNBUF);
1210 if (nd->ni_dvp == nd->ni_vp)
1218 vattr.va_type = VFIFO;
1219 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1220 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1221 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd, &vattr);
1224 NDFREE(nd, NDF_ONLY_PNBUF);
1230 * mkfifo_args(char *path, int mode)
1232 * Create a named pipe.
1235 mkfifo(struct mkfifo_args *uap)
1237 struct thread *td = curthread;
1238 struct nameidata nd;
1241 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1244 error = kern_mkfifo(&nd, uap->mode);
1250 kern_link(struct nameidata *nd, struct nameidata *linknd)
1252 struct thread *td = curthread;
1253 struct proc *p = td->td_proc;
1261 NDFREE(nd, NDF_ONLY_PNBUF);
1263 if (vp->v_type == VDIR)
1264 error = EPERM; /* POSIX */
1266 error = namei(linknd);
1268 if (linknd->ni_vp != NULL) {
1270 vrele(linknd->ni_vp);
1273 VOP_LEASE(linknd->ni_dvp, td, p->p_ucred,
1275 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1276 error = VOP_LINK(linknd->ni_dvp, NCPNULL, vp,
1279 NDFREE(linknd, NDF_ONLY_PNBUF);
1280 if (linknd->ni_dvp == linknd->ni_vp)
1281 vrele(linknd->ni_dvp);
1283 vput(linknd->ni_dvp);
1284 ASSERT_VOP_UNLOCKED(linknd->ni_dvp, "link");
1285 ASSERT_VOP_UNLOCKED(linknd->ni_vp, "link");
1293 * link_args(char *path, char *link)
1295 * Make a hard file link.
1298 link(struct link_args *uap)
1300 struct thread *td = curthread;
1301 struct nameidata nd, linknd;
1304 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1306 NDINIT(&linknd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1307 UIO_USERSPACE, uap->link, td);
1309 error = kern_link(&nd, &linknd);
1315 kern_symlink(char *path, struct nameidata *nd)
1317 struct thread *td = curthread;
1318 struct proc *p = td->td_proc;
1327 NDFREE(nd, NDF_ONLY_PNBUF);
1328 if (nd->ni_dvp == nd->ni_vp)
1336 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1337 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1338 error = VOP_SYMLINK(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
1340 NDFREE(nd, NDF_ONLY_PNBUF);
1344 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "symlink");
1345 ASSERT_VOP_UNLOCKED(nd->ni_vp, "symlink");
1351 * symlink(char *path, char *link)
1353 * Make a symbolic link.
1356 symlink(struct symlink_args *uap)
1358 struct thread *td = curthread;
1359 struct nameidata nd;
1363 path = zalloc(namei_zone);
1364 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1366 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1367 UIO_USERSPACE, uap->link, td);
1368 error = kern_symlink(path, &nd);
1370 zfree(namei_zone, path);
1375 * undelete_args(char *path)
1377 * Delete a whiteout from the filesystem.
1381 undelete(struct undelete_args *uap)
1383 struct thread *td = curthread;
1384 struct proc *p = td->td_proc;
1386 struct nameidata nd;
1389 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
1390 SCARG(uap, path), td);
1395 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
1396 NDFREE(&nd, NDF_ONLY_PNBUF);
1397 if (nd.ni_dvp == nd.ni_vp)
1406 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1407 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
1408 NDFREE(&nd, NDF_ONLY_PNBUF);
1410 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1411 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1416 kern_unlink(struct nameidata *nd)
1418 struct thread *td = curthread;
1419 struct proc *p = td->td_proc;
1428 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1429 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1431 if (vp->v_type == VDIR)
1432 error = EPERM; /* POSIX */
1435 * The root of a mounted filesystem cannot be deleted.
1437 * XXX: can this only be a VDIR case?
1439 if (vp->v_flag & VROOT)
1444 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1445 error = VOP_REMOVE(nd->ni_dvp, NCPNULL, vp, &nd->ni_cnd);
1447 NDFREE(nd, NDF_ONLY_PNBUF);
1448 if (nd->ni_dvp == vp)
1454 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "unlink");
1455 ASSERT_VOP_UNLOCKED(nd->ni_vp, "unlink");
1460 * unlink_args(char *path)
1462 * Delete a name from the filesystem.
1465 unlink(struct unlink_args *uap)
1467 struct thread *td = curthread;
1468 struct nameidata nd;
1471 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1474 error = kern_unlink(&nd);
1480 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1482 struct thread *td = curthread;
1483 struct proc *p = td->td_proc;
1484 struct filedesc *fdp = p->p_fd;
1489 if (fd >= fdp->fd_nfiles ||
1490 (fp = fdp->fd_ofiles[fd]) == NULL)
1492 if (fp->f_type != DTYPE_VNODE)
1496 fp->f_offset += offset;
1499 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1502 fp->f_offset = offset + vattr.va_size;
1505 fp->f_offset = offset;
1510 *res = fp->f_offset;
1515 * lseek_args(int fd, int pad, off_t offset, int whence)
1517 * Reposition read/write file offset.
1520 lseek(struct lseek_args *uap)
1524 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1525 &uap->sysmsg_offset);
1531 kern_access(struct nameidata *nd, int aflags)
1533 struct thread *td = curthread;
1534 struct proc *p = td->td_proc;
1535 struct ucred *cred, *tmpcred;
1541 * Create and modify a temporary credential instead of one that
1542 * is potentially shared. This could also mess up socket
1543 * buffer accounting which can run in an interrupt context.
1545 tmpcred = crdup(cred);
1546 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1547 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1548 p->p_ucred = tmpcred;
1549 nd->ni_cnd.cn_cred = tmpcred;
1555 /* Flags == 0 means only check for existence. */
1564 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1565 error = VOP_ACCESS(vp, flags, tmpcred, td);
1567 NDFREE(nd, NDF_ONLY_PNBUF);
1576 * access_args(char *path, int flags)
1578 * Check access permissions.
1581 access(struct access_args *uap)
1583 struct thread *td = curthread;
1584 struct nameidata nd;
1587 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1588 UIO_USERSPACE, uap->path, td);
1590 error = kern_access(&nd, uap->flags);
1596 kern_stat(struct nameidata *nd, struct stat *st)
1598 struct thread *td = curthread;
1604 error = vn_stat(nd->ni_vp, st, td);
1605 NDFREE(nd, NDF_ONLY_PNBUF);
1611 * stat_args(char *path, struct stat *ub)
1613 * Get file status; this version follows links.
1616 stat(struct stat_args *uap)
1618 struct thread *td = curthread;
1619 struct nameidata nd;
1623 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1624 UIO_USERSPACE, uap->path, td);
1626 error = kern_stat(&nd, &st);
1629 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1634 * lstat_args(char *path, struct stat *ub)
1636 * Get file status; this version does not follow links.
1639 lstat(struct lstat_args *uap)
1641 struct thread *td = curthread;
1642 struct nameidata nd;
1646 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1647 UIO_USERSPACE, SCARG(uap, path), td);
1649 error = kern_stat(&nd, &st);
1652 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1661 nsb->st_dev = sb->st_dev;
1662 nsb->st_ino = sb->st_ino;
1663 nsb->st_mode = sb->st_mode;
1664 nsb->st_nlink = sb->st_nlink;
1665 nsb->st_uid = sb->st_uid;
1666 nsb->st_gid = sb->st_gid;
1667 nsb->st_rdev = sb->st_rdev;
1668 nsb->st_atimespec = sb->st_atimespec;
1669 nsb->st_mtimespec = sb->st_mtimespec;
1670 nsb->st_ctimespec = sb->st_ctimespec;
1671 nsb->st_size = sb->st_size;
1672 nsb->st_blocks = sb->st_blocks;
1673 nsb->st_blksize = sb->st_blksize;
1674 nsb->st_flags = sb->st_flags;
1675 nsb->st_gen = sb->st_gen;
1676 nsb->st_qspare[0] = sb->st_qspare[0];
1677 nsb->st_qspare[1] = sb->st_qspare[1];
1681 * nstat_args(char *path, struct nstat *ub)
1685 nstat(struct nstat_args *uap)
1687 struct thread *td = curthread;
1691 struct nameidata nd;
1693 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1694 UIO_USERSPACE, SCARG(uap, path), td);
1695 if ((error = namei(&nd)) != 0)
1697 NDFREE(&nd, NDF_ONLY_PNBUF);
1698 error = vn_stat(nd.ni_vp, &sb, td);
1702 cvtnstat(&sb, &nsb);
1703 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1708 * lstat_args(char *path, struct stat *ub)
1710 * Get file status; this version does not follow links.
1714 nlstat(struct nlstat_args *uap)
1716 struct thread *td = curthread;
1721 struct nameidata nd;
1723 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1724 UIO_USERSPACE, SCARG(uap, path), td);
1725 if ((error = namei(&nd)) != 0)
1728 NDFREE(&nd, NDF_ONLY_PNBUF);
1729 error = vn_stat(vp, &sb, td);
1733 cvtnstat(&sb, &nsb);
1734 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1739 * pathconf_Args(char *path, int name)
1741 * Get configurable pathname variables.
1745 pathconf(struct pathconf_args *uap)
1747 struct thread *td = curthread;
1749 struct nameidata nd;
1751 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1752 UIO_USERSPACE, SCARG(uap, path), td);
1753 if ((error = namei(&nd)) != 0)
1755 NDFREE(&nd, NDF_ONLY_PNBUF);
1756 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1763 * kern_readlink isn't properly split yet. There is a copyin burried
1764 * in VOP_READLINK().
1767 kern_readlink(struct nameidata *nd, char *buf, int count, int *res)
1769 struct thread *td = curthread;
1770 struct proc *p = td->td_proc;
1779 NDFREE(nd, NDF_ONLY_PNBUF);
1781 if (vp->v_type != VLNK)
1784 aiov.iov_base = buf;
1785 aiov.iov_len = count;
1786 auio.uio_iov = &aiov;
1787 auio.uio_iovcnt = 1;
1788 auio.uio_offset = 0;
1789 auio.uio_rw = UIO_READ;
1790 auio.uio_segflg = UIO_USERSPACE;
1792 auio.uio_resid = count;
1793 error = VOP_READLINK(vp, &auio, p->p_ucred);
1796 *res = count - auio.uio_resid;
1801 * readlink_args(char *path, char *buf, int count)
1803 * Return target name of a symbolic link.
1806 readlink(struct readlink_args *uap)
1808 struct thread *td = curthread;
1809 struct nameidata nd;
1812 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ, UIO_USERSPACE,
1815 error = kern_readlink(&nd, uap->buf, uap->count,
1816 &uap->sysmsg_result);
1822 setfflags(struct vnode *vp, int flags)
1824 struct thread *td = curthread;
1825 struct proc *p = td->td_proc;
1830 * Prevent non-root users from setting flags on devices. When
1831 * a device is reused, users can retain ownership of the device
1832 * if they are allowed to set flags and programs assume that
1833 * chown can't fail when done as root.
1835 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1836 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1839 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1840 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1842 vattr.va_flags = flags;
1843 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1844 VOP_UNLOCK(vp, NULL, 0, td);
1849 * chflags(char *path, int flags)
1851 * Change flags of a file given a path name.
1855 chflags(struct chflags_args *uap)
1857 struct thread *td = curthread;
1859 struct nameidata nd;
1861 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1862 SCARG(uap, path), td);
1863 if ((error = namei(&nd)) != 0)
1865 NDFREE(&nd, NDF_ONLY_PNBUF);
1866 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1872 * fchflags_args(int fd, int flags)
1874 * Change flags of a file given a file descriptor.
1878 fchflags(struct fchflags_args *uap)
1880 struct thread *td = curthread;
1881 struct proc *p = td->td_proc;
1885 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1887 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1891 setfmode(struct vnode *vp, int mode)
1893 struct thread *td = curthread;
1894 struct proc *p = td->td_proc;
1898 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1899 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1901 vattr.va_mode = mode & ALLPERMS;
1902 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1903 VOP_UNLOCK(vp, NULL, 0, td);
1908 kern_chmod(struct nameidata *nd, int mode)
1915 NDFREE(nd, NDF_ONLY_PNBUF);
1916 error = setfmode(nd->ni_vp, mode);
1922 * chmod_args(char *path, int mode)
1924 * Change mode of a file given path name.
1928 chmod(struct chmod_args *uap)
1930 struct thread *td = curthread;
1931 struct nameidata nd;
1934 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1936 error = kern_chmod(&nd, uap->mode);
1942 * lchmod_args(char *path, int mode)
1944 * Change mode of a file given path name (don't follow links.)
1948 lchmod(struct lchmod_args *uap)
1950 struct thread *td = curthread;
1952 struct nameidata nd;
1954 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
1955 if ((error = namei(&nd)) != 0)
1957 NDFREE(&nd, NDF_ONLY_PNBUF);
1958 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1964 * fchmod_args(int fd, int mode)
1966 * Change mode of a file given a file descriptor.
1970 fchmod(struct fchmod_args *uap)
1972 struct thread *td = curthread;
1973 struct proc *p = td->td_proc;
1977 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1979 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1983 setfown(struct vnode *vp, uid_t uid, gid_t gid)
1985 struct thread *td = curthread;
1986 struct proc *p = td->td_proc;
1990 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1991 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1995 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1996 VOP_UNLOCK(vp, NULL, 0, td);
2001 kern_chown(struct nameidata *nd, int uid, int gid)
2008 NDFREE(nd, NDF_ONLY_PNBUF);
2009 error = setfown(nd->ni_vp, uid, gid);
2015 * chown(char *path, int uid, int gid)
2017 * Set ownership given a path name.
2020 chown(struct chown_args *uap)
2022 struct thread *td = curthread;
2023 struct nameidata nd;
2026 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2028 error = kern_chown(&nd, uap->uid, uap->gid);
2034 * lchown_args(char *path, int uid, int gid)
2036 * Set ownership given a path name, do not cross symlinks.
2039 lchown(struct lchown_args *uap)
2041 struct thread *td = curthread;
2043 struct nameidata nd;
2045 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2047 error = kern_chown(&nd, uap->uid, uap->gid);
2053 * fchown_args(int fd, int uid, int gid)
2055 * Set ownership given a file descriptor.
2059 fchown(struct fchown_args *uap)
2061 struct thread *td = curthread;
2062 struct proc *p = td->td_proc;
2066 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2068 return setfown((struct vnode *)fp->f_data,
2069 SCARG(uap, uid), SCARG(uap, gid));
2073 getutimes(const struct timeval *tvp, struct timespec *tsp)
2075 struct timeval tv[2];
2079 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2082 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2083 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2089 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2091 struct thread *td = curthread;
2092 struct proc *p = td->td_proc;
2096 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2097 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2099 vattr.va_atime = ts[0];
2100 vattr.va_mtime = ts[1];
2102 vattr.va_vaflags |= VA_UTIMES_NULL;
2103 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2104 VOP_UNLOCK(vp, NULL, 0, td);
2109 kern_utimes(struct nameidata *nd, struct timeval *tptr)
2111 struct timespec ts[2];
2114 error = getutimes(tptr, ts);
2120 NDFREE(nd, NDF_ONLY_PNBUF);
2121 error = setutimes(nd->ni_vp, ts, tptr == NULL);
2127 * utimes_args(char *path, struct timeval *tptr)
2129 * Set the access and modification times of a file.
2132 utimes(struct utimes_args *uap)
2134 struct thread *td = curthread;
2135 struct timeval tv[2];
2136 struct nameidata nd;
2140 error = copyin(uap->tptr, tv, sizeof(tv));
2144 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2146 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2152 * lutimes_args(char *path, struct timeval *tptr)
2154 * Set the access and modification times of a file.
2157 lutimes(struct lutimes_args *uap)
2159 struct thread *td = curthread;
2160 struct timeval tv[2];
2161 struct nameidata nd;
2165 error = copyin(uap->tptr, tv, sizeof(tv));
2169 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2171 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2177 kern_futimes(int fd, struct timeval *tptr)
2179 struct thread *td = curthread;
2180 struct proc *p = td->td_proc;
2181 struct timespec ts[2];
2185 error = getutimes(tptr, ts);
2188 error = getvnode(p->p_fd, fd, &fp);
2191 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2196 * futimes_args(int fd, struct timeval *tptr)
2198 * Set the access and modification times of a file.
2201 futimes(struct futimes_args *uap)
2203 struct timeval tv[2];
2207 error = copyin(uap->tptr, tv, sizeof(tv));
2212 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2218 kern_truncate(struct nameidata* nd, off_t length)
2220 struct thread *td = curthread;
2221 struct proc *p = td->td_proc;
2228 if ((error = namei(nd)) != 0)
2231 NDFREE(nd, NDF_ONLY_PNBUF);
2232 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2233 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2234 if (vp->v_type == VDIR)
2236 else if ((error = vn_writechk(vp)) == 0 &&
2237 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2239 vattr.va_size = length;
2240 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2247 * truncate(char *path, int pad, off_t length)
2249 * Truncate a file given its path name.
2252 truncate(struct truncate_args *uap)
2254 struct thread *td = curthread;
2255 struct nameidata nd;
2258 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2260 error = kern_truncate(&nd, uap->length);
2266 kern_ftruncate(int fd, off_t length)
2268 struct thread *td = curthread;
2269 struct proc *p = td->td_proc;
2277 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2279 if ((fp->f_flag & FWRITE) == 0)
2281 vp = (struct vnode *)fp->f_data;
2282 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2283 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2284 if (vp->v_type == VDIR)
2286 else if ((error = vn_writechk(vp)) == 0) {
2288 vattr.va_size = length;
2289 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2291 VOP_UNLOCK(vp, NULL, 0, td);
2296 * ftruncate_args(int fd, int pad, off_t length)
2298 * Truncate a file given a file descriptor.
2301 ftruncate(struct ftruncate_args *uap)
2305 error = kern_ftruncate(uap->fd, uap->length);
2313 * Sync an open file.
2317 fsync(struct fsync_args *uap)
2319 struct thread *td = curthread;
2320 struct proc *p = td->td_proc;
2326 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2328 vp = (struct vnode *)fp->f_data;
2329 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2330 if (VOP_GETVOBJECT(vp, &obj) == 0)
2331 vm_object_page_clean(obj, 0, 0, 0);
2332 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2333 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2335 error = (*bioops.io_fsync)(vp);
2336 VOP_UNLOCK(vp, NULL, 0, td);
2341 kern_rename(struct nameidata *fromnd, struct nameidata *tond)
2343 struct thread *td = curthread;
2344 struct proc *p = td->td_proc;
2345 struct vnode *tvp, *fvp, *tdvp;
2349 error = namei(fromnd);
2352 fvp = fromnd->ni_vp;
2353 if (fromnd->ni_vp->v_type == VDIR)
2354 tond->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2355 error = namei(tond);
2357 /* Translate error code for rename("dir1", "dir2/."). */
2358 if (error == EISDIR && fvp->v_type == VDIR)
2360 NDFREE(fromnd, NDF_ONLY_PNBUF);
2361 vrele(fromnd->ni_dvp);
2365 tdvp = tond->ni_dvp;
2368 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2371 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2379 * If the source is the same as the destination (that is, if they
2380 * are links to the same vnode), then there is nothing to do.
2386 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2387 if (fromnd->ni_dvp != tdvp) {
2388 VOP_LEASE(fromnd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2391 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2393 error = VOP_RENAME(fromnd->ni_dvp, NCPNULL, fromnd->ni_vp,
2394 &fromnd->ni_cnd, tond->ni_dvp, NCPNULL, tond->ni_vp,
2396 NDFREE(fromnd, NDF_ONLY_PNBUF);
2397 NDFREE(tond, NDF_ONLY_PNBUF);
2399 NDFREE(fromnd, NDF_ONLY_PNBUF);
2400 NDFREE(tond, NDF_ONLY_PNBUF);
2407 vrele(fromnd->ni_dvp);
2410 vrele(tond->ni_startdir);
2411 ASSERT_VOP_UNLOCKED(fromnd->ni_dvp, "rename");
2412 ASSERT_VOP_UNLOCKED(fromnd->ni_vp, "rename");
2413 ASSERT_VOP_UNLOCKED(tond->ni_dvp, "rename");
2414 ASSERT_VOP_UNLOCKED(tond->ni_vp, "rename");
2416 if (fromnd->ni_startdir)
2417 vrele(fromnd->ni_startdir);
2424 * rename_args(char *from, char *to)
2426 * Rename files. Source and destination must either both be directories,
2427 * or both not be directories. If target is a directory, it must be empty.
2430 rename(struct rename_args *uap)
2432 struct thread *td = curthread;
2433 struct nameidata fromnd, tond;
2436 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2437 UIO_USERSPACE, uap->from, td);
2438 NDINIT(&tond, NAMEI_RENAME,
2439 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2440 CNP_SAVESTART | CNP_NOOBJ,
2441 UIO_USERSPACE, uap->to, td);
2443 error = kern_rename(&fromnd, &tond);
2449 kern_mkdir(struct nameidata *nd, int mode)
2451 struct thread *td = curthread;
2452 struct proc *p = td->td_proc;
2458 nd->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2464 NDFREE(nd, NDF_ONLY_PNBUF);
2465 if (nd->ni_dvp == vp)
2473 vattr.va_type = VDIR;
2474 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2475 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2476 error = VOP_MKDIR(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
2478 NDFREE(nd, NDF_ONLY_PNBUF);
2482 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mkdir");
2483 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mkdir");
2488 * mkdir_args(char *path, int mode)
2490 * Make a directory file.
2494 mkdir(struct mkdir_args *uap)
2496 struct thread *td = curthread;
2497 struct nameidata nd;
2500 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
2503 error = kern_mkdir(&nd, uap->mode);
2509 kern_rmdir(struct nameidata *nd)
2511 struct thread *td = curthread;
2512 struct proc *p = td->td_proc;
2521 if (vp->v_type != VDIR) {
2526 * No rmdir "." please.
2528 if (nd->ni_dvp == vp) {
2533 * The root of a mounted filesystem cannot be deleted.
2535 if (vp->v_flag & VROOT)
2538 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2539 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2540 error = VOP_RMDIR(nd->ni_dvp, NCPNULL, nd->ni_vp,
2544 NDFREE(nd, NDF_ONLY_PNBUF);
2545 if (nd->ni_dvp == vp)
2551 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "rmdir");
2552 ASSERT_VOP_UNLOCKED(nd->ni_vp, "rmdir");
2557 * rmdir_args(char *path)
2559 * Remove a directory file.
2563 rmdir(struct rmdir_args *uap)
2565 struct thread *td = curthread;
2566 struct nameidata nd;
2569 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF,
2570 UIO_USERSPACE, uap->path, td);
2572 error = kern_rmdir(&nd);
2578 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2580 struct thread *td = curthread;
2581 struct proc *p = td->td_proc;
2589 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2591 if ((fp->f_flag & FREAD) == 0)
2593 vp = (struct vnode *)fp->f_data;
2595 if (vp->v_type != VDIR)
2597 aiov.iov_base = buf;
2598 aiov.iov_len = count;
2599 auio.uio_iov = &aiov;
2600 auio.uio_iovcnt = 1;
2601 auio.uio_rw = UIO_READ;
2602 auio.uio_segflg = UIO_USERSPACE;
2604 auio.uio_resid = count;
2605 /* vn_lock(vp, NULL, LK_SHARED | LK_RETRY, td); */
2606 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2607 loff = auio.uio_offset = fp->f_offset;
2608 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2609 fp->f_offset = auio.uio_offset;
2610 VOP_UNLOCK(vp, NULL, 0, td);
2613 if (count == auio.uio_resid) {
2614 if (union_dircheckp) {
2615 error = union_dircheckp(td, &vp, fp);
2621 if ((vp->v_flag & VROOT) &&
2622 (vp->v_mount->mnt_flag & MNT_UNION)) {
2623 struct vnode *tvp = vp;
2624 vp = vp->v_mount->mnt_vnodecovered;
2626 fp->f_data = (caddr_t) vp;
2635 *res = count - auio.uio_resid;
2640 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2642 * Read a block of directory entries in a file system independent format.
2645 getdirentries(struct getdirentries_args *uap)
2650 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2651 &uap->sysmsg_result);
2654 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2659 * getdents_args(int fd, char *buf, size_t count)
2662 getdents(struct getdents_args *uap)
2666 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2667 &uap->sysmsg_result);
2673 * umask(int newmask)
2675 * Set the mode mask for creation of filesystem nodes.
2680 umask(struct umask_args *uap)
2682 struct thread *td = curthread;
2683 struct proc *p = td->td_proc;
2684 struct filedesc *fdp;
2687 uap->sysmsg_result = fdp->fd_cmask;
2688 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2693 * revoke(char *path)
2695 * Void all references to file by ripping underlying filesystem
2700 revoke(struct revoke_args *uap)
2702 struct thread *td = curthread;
2703 struct proc *p = td->td_proc;
2707 struct nameidata nd;
2709 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2710 if ((error = namei(&nd)) != 0)
2713 NDFREE(&nd, NDF_ONLY_PNBUF);
2714 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2718 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2720 if (p->p_ucred->cr_uid != vattr.va_uid &&
2721 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2724 VOP_REVOKE(vp, REVOKEALL);
2731 * Convert a user file descriptor to a kernel file entry.
2734 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2738 if ((u_int)fd >= fdp->fd_nfiles ||
2739 (fp = fdp->fd_ofiles[fd]) == NULL)
2741 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2747 * getfh_args(char *fname, fhandle_t *fhp)
2749 * Get (NFS) file handle
2752 getfh(struct getfh_args *uap)
2754 struct thread *td = curthread;
2755 struct nameidata nd;
2761 * Must be super user
2766 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2770 NDFREE(&nd, NDF_ONLY_PNBUF);
2772 bzero(&fh, sizeof(fh));
2773 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2774 error = VFS_VPTOFH(vp, &fh.fh_fid);
2778 error = copyout(&fh, uap->fhp, sizeof (fh));
2783 * fhopen_args(const struct fhandle *u_fhp, int flags)
2785 * syscall for the rpc.lockd to use to translate a NFS file handle into
2786 * an open descriptor.
2788 * warning: do not remove the suser() call or this becomes one giant
2792 fhopen(struct fhopen_args *uap)
2794 struct thread *td = curthread;
2795 struct proc *p = td->td_proc;
2800 struct vattr *vap = &vat;
2803 struct filedesc *fdp = p->p_fd;
2804 int fmode, mode, error, type;
2809 * Must be super user
2815 fmode = FFLAGS(SCARG(uap, flags));
2816 /* why not allow a non-read/write open for our lockd? */
2817 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2819 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2822 /* find the mount point */
2823 mp = vfs_getvfs(&fhp.fh_fsid);
2826 /* now give me my vnode, it gets returned to me locked */
2827 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2831 * from now on we have to make sure not
2832 * to forget about the vnode
2833 * any error that causes an abort must vput(vp)
2834 * just set error = err and 'goto bad;'.
2840 if (vp->v_type == VLNK) {
2844 if (vp->v_type == VSOCK) {
2849 if (fmode & (FWRITE | O_TRUNC)) {
2850 if (vp->v_type == VDIR) {
2854 error = vn_writechk(vp);
2862 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2866 if (fmode & O_TRUNC) {
2867 VOP_UNLOCK(vp, NULL, 0, td); /* XXX */
2868 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2869 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2872 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2876 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2880 * Make sure that a VM object is created for VMIO support.
2882 if (vn_canvmio(vp) == TRUE) {
2883 if ((error = vfs_object_create(vp, td)) != 0)
2890 * end of vn_open code
2893 if ((error = falloc(p, &nfp, &indx)) != 0) {
2901 * hold an extra reference to avoid having fp ripped out
2902 * from under us while we block in the lock op.
2905 nfp->f_data = (caddr_t)vp;
2906 nfp->f_flag = fmode & FMASK;
2907 nfp->f_ops = &vnops;
2908 nfp->f_type = DTYPE_VNODE;
2909 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2910 lf.l_whence = SEEK_SET;
2913 if (fmode & O_EXLOCK)
2914 lf.l_type = F_WRLCK;
2916 lf.l_type = F_RDLCK;
2918 if ((fmode & FNONBLOCK) == 0)
2920 VOP_UNLOCK(vp, NULL, 0, td);
2921 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2923 * lock request failed. Normally close the descriptor
2924 * but handle the case where someone might have dup()d
2925 * or close()d it when we weren't looking.
2927 if (fdp->fd_ofiles[indx] == fp) {
2928 fdp->fd_ofiles[indx] = NULL;
2933 * release our private reference.
2938 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2939 fp->f_flag |= FHASLOCK;
2941 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2942 vfs_object_create(vp, td);
2944 VOP_UNLOCK(vp, NULL, 0, td);
2946 uap->sysmsg_result = indx;
2955 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2958 fhstat(struct fhstat_args *uap)
2960 struct thread *td = curthread;
2968 * Must be super user
2974 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
2978 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2980 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
2982 error = vn_stat(vp, &sb, td);
2986 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
2991 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
2994 fhstatfs(struct fhstatfs_args *uap)
2996 struct thread *td = curthread;
3005 * Must be super user
3007 if ((error = suser(td)))
3010 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3013 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3015 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3020 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3022 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3024 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3025 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3028 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3032 * Syscall to push extended attribute configuration information into the
3033 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3034 * a command (int cmd), and attribute name and misc data. For now, the
3035 * attribute name is left in userspace for consumption by the VFS_op.
3036 * It will probably be changed to be copied into sysspace by the
3037 * syscall in the future, once issues with various consumers of the
3038 * attribute code have raised their hands.
3040 * Currently this is used only by UFS Extended Attributes.
3043 extattrctl(struct extattrctl_args *uap)
3045 struct thread *td = curthread;
3046 struct nameidata nd;
3050 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3051 if ((error = namei(&nd)) != 0)
3053 mp = nd.ni_vp->v_mount;
3055 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3056 SCARG(uap, arg), td));
3060 * Syscall to set a named extended attribute on a file or directory.
3061 * Accepts attribute name, and a uio structure pointing to the data to set.
3062 * The uio is consumed in the style of writev(). The real work happens
3063 * in VOP_SETEXTATTR().
3066 extattr_set_file(struct extattr_set_file_args *uap)
3068 struct thread *td = curthread;
3069 struct proc *p = td->td_proc;
3070 struct nameidata nd;
3072 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3073 char attrname[EXTATTR_MAXNAMELEN];
3077 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3080 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3081 SCARG(uap, path), td);
3082 if ((error = namei(&nd)) != 0)
3084 iovlen = uap->iovcnt * sizeof(struct iovec);
3085 if (uap->iovcnt > UIO_SMALLIOV) {
3086 if (uap->iovcnt > UIO_MAXIOV) {
3090 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3095 auio.uio_iovcnt = uap->iovcnt;
3096 auio.uio_rw = UIO_WRITE;
3097 auio.uio_segflg = UIO_USERSPACE;
3099 auio.uio_offset = 0;
3100 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3103 for (i = 0; i < uap->iovcnt; i++) {
3104 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3108 auio.uio_resid += iov->iov_len;
3111 cnt = auio.uio_resid;
3112 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3113 cnt -= auio.uio_resid;
3114 uap->sysmsg_result = cnt;
3117 FREE(needfree, M_IOV);
3123 * Syscall to get a named extended attribute on a file or directory.
3124 * Accepts attribute name, and a uio structure pointing to a buffer for the
3125 * data. The uio is consumed in the style of readv(). The real work
3126 * happens in VOP_GETEXTATTR();
3129 extattr_get_file(struct extattr_get_file_args *uap)
3131 struct thread *td = curthread;
3132 struct proc *p = td->td_proc;
3133 struct nameidata nd;
3135 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3136 char attrname[EXTATTR_MAXNAMELEN];
3140 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3143 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3144 SCARG(uap, path), td);
3145 if ((error = namei(&nd)) != 0)
3147 iovlen = uap->iovcnt * sizeof (struct iovec);
3148 if (uap->iovcnt > UIO_SMALLIOV) {
3149 if (uap->iovcnt > UIO_MAXIOV) {
3153 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3160 auio.uio_iovcnt = uap->iovcnt;
3161 auio.uio_rw = UIO_READ;
3162 auio.uio_segflg = UIO_USERSPACE;
3164 auio.uio_offset = 0;
3165 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3168 for (i = 0; i < uap->iovcnt; i++) {
3169 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3173 auio.uio_resid += iov->iov_len;
3176 cnt = auio.uio_resid;
3177 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3178 cnt -= auio.uio_resid;
3179 uap->sysmsg_result = cnt;
3182 FREE(needfree, M_IOV);
3188 * Syscall to delete a named extended attribute from a file or directory.
3189 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3192 extattr_delete_file(struct extattr_delete_file_args *uap)
3194 struct thread *td = curthread;
3195 struct proc *p = td->td_proc;
3196 struct nameidata nd;
3197 char attrname[EXTATTR_MAXNAMELEN];
3200 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3203 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3204 SCARG(uap, path), td);
3205 if ((error = namei(&nd)) != 0)
3207 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);