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.34 2004/05/19 22:52:58 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];
115 if (p->p_ucred->cr_prison != NULL)
117 if (usermount == 0 && (error = suser(td)))
120 * Do not allow NFS export by non-root users.
122 if (SCARG(uap, flags) & MNT_EXPORTED) {
128 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
131 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
133 * Get vnode to be covered
135 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
136 SCARG(uap, path), td);
137 if ((error = namei(&nd)) != 0)
139 NDFREE(&nd, NDF_ONLY_PNBUF);
141 if (SCARG(uap, flags) & MNT_UPDATE) {
142 if ((vp->v_flag & VROOT) == 0) {
148 flag2 = mp->mnt_kern_flag;
150 * We only allow the filesystem to be reloaded if it
151 * is currently mounted read-only.
153 if ((SCARG(uap, flags) & MNT_RELOAD) &&
154 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
156 return (EOPNOTSUPP); /* Needs translation */
159 * Only root, or the user that did the original mount is
160 * permitted to update it.
162 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
163 (error = suser(td))) {
167 if (vfs_busy(mp, LK_NOWAIT, NULL, td)) {
171 lwkt_gettoken(&vlock, vp->v_interlock);
172 if ((vp->v_flag & VMOUNT) != 0 ||
173 vp->v_mountedhere != NULL) {
174 lwkt_reltoken(&vlock);
179 vp->v_flag |= VMOUNT;
180 lwkt_reltoken(&vlock);
182 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
183 VOP_UNLOCK(vp, NULL, 0, td);
187 * If the user is not root, ensure that they own the directory
188 * onto which we are attempting to mount.
190 if ((error = VOP_GETATTR(vp, &va, td)) ||
191 (va.va_uid != p->p_ucred->cr_uid &&
192 (error = suser(td)))) {
196 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
200 if (vp->v_type != VDIR) {
204 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
208 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
209 if (!strcmp(vfsp->vfc_name, fstypename))
214 /* Only load modules for root (very important!) */
215 if ((error = suser(td)) != 0) {
219 error = linker_load_file(fstypename, &lf);
220 if (error || lf == NULL) {
227 /* lookup again, see if the VFS was loaded */
228 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
229 if (!strcmp(vfsp->vfc_name, fstypename))
233 linker_file_unload(lf);
238 lwkt_gettoken(&vlock, vp->v_interlock);
239 if ((vp->v_flag & VMOUNT) != 0 ||
240 vp->v_mountedhere != NULL) {
241 lwkt_reltoken(&vlock);
245 vp->v_flag |= VMOUNT;
246 lwkt_reltoken(&vlock);
249 * Allocate and initialize the filesystem.
251 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
252 bzero((char *)mp, (u_long)sizeof(struct mount));
253 TAILQ_INIT(&mp->mnt_nvnodelist);
254 TAILQ_INIT(&mp->mnt_reservedvnlist);
255 mp->mnt_nvnodelistsize = 0;
256 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
257 vfs_busy(mp, LK_NOWAIT, NULL, td);
258 mp->mnt_op = vfsp->vfc_vfsops;
260 vfsp->vfc_refcount++;
261 mp->mnt_stat.f_type = vfsp->vfc_typenum;
262 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
263 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
264 mp->mnt_vnodecovered = vp;
265 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
266 mp->mnt_iosize_max = DFLTPHYS;
267 VOP_UNLOCK(vp, NULL, 0, td);
270 * Set the mount level flags.
272 if (SCARG(uap, flags) & MNT_RDONLY)
273 mp->mnt_flag |= MNT_RDONLY;
274 else if (mp->mnt_flag & MNT_RDONLY)
275 mp->mnt_kern_flag |= MNTK_WANTRDWR;
276 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
277 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
278 MNT_NOSYMFOLLOW | MNT_IGNORE |
279 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
280 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
281 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
282 MNT_NOSYMFOLLOW | MNT_IGNORE |
283 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
285 * Mount the filesystem.
286 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
287 * get. No freeing of cn_pnbuf.
289 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
290 if (mp->mnt_flag & MNT_UPDATE) {
291 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
292 mp->mnt_flag &= ~MNT_RDONLY;
293 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
294 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
297 mp->mnt_kern_flag = flag2;
300 lwkt_gettoken(&vlock, vp->v_interlock);
301 vp->v_flag &= ~VMOUNT;
302 lwkt_reltoken(&vlock);
306 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
308 * Put the new filesystem on the mount list after root.
312 lwkt_gettoken(&vlock, vp->v_interlock);
313 vp->v_flag &= ~VMOUNT;
314 vp->v_mountedhere = mp;
315 lwkt_reltoken(&vlock);
316 lwkt_gettoken(&ilock, &mountlist_token);
317 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
318 lwkt_reltoken(&ilock);
320 VOP_UNLOCK(vp, NULL, 0, td);
321 error = vfs_allocate_syncvnode(mp);
323 if ((error = VFS_START(mp, 0, td)) != 0)
326 lwkt_gettoken(&vlock, vp->v_interlock);
327 vp->v_flag &= ~VMOUNT;
328 lwkt_reltoken(&vlock);
329 mp->mnt_vfc->vfc_refcount--;
331 free((caddr_t)mp, M_MOUNT);
338 * Scan all active processes to see if any of them have a current
339 * or root directory onto which the new filesystem has just been
340 * mounted. If so, replace them with the new mount point.
343 checkdirs(struct vnode *olddp)
345 struct filedesc *fdp;
349 if (olddp->v_usecount == 1)
351 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
352 panic("mount: lost mount");
353 FOREACH_PROC_IN_SYSTEM(p) {
355 if (fdp->fd_cdir == olddp) {
358 fdp->fd_cdir = newdp;
360 if (fdp->fd_rdir == olddp) {
363 fdp->fd_rdir = newdp;
366 if (rootvnode == olddp) {
370 vfs_cache_setroot(rootvnode);
376 * Unmount a file system.
378 * Note: unmount takes a path to the vnode mounted on as argument,
379 * not special file (as before).
382 * umount_args(char *path, int flags)
386 unmount(struct unmount_args *uap)
388 struct thread *td = curthread;
389 struct proc *p = td->td_proc;
396 if (p->p_ucred->cr_prison != NULL)
398 if (usermount == 0 && (error = suser(td)))
401 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
402 SCARG(uap, path), td);
403 if ((error = namei(&nd)) != 0)
406 NDFREE(&nd, NDF_ONLY_PNBUF);
410 * Only root, or the user that did the original mount is
411 * permitted to unmount this filesystem.
413 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
414 (error = suser(td))) {
420 * Don't allow unmounting the root file system.
422 if (mp->mnt_flag & MNT_ROOTFS) {
428 * Must be the root of the filesystem
430 if ((vp->v_flag & VROOT) == 0) {
435 return (dounmount(mp, SCARG(uap, flags), td));
439 * Do the actual file system unmount.
442 dounmount(struct mount *mp, int flags, struct thread *td)
444 struct vnode *coveredvp;
449 lwkt_gettoken(&ilock, &mountlist_token);
450 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
451 lwkt_reltoken(&ilock);
454 mp->mnt_kern_flag |= MNTK_UNMOUNT;
455 /* Allow filesystems to detect that a forced unmount is in progress. */
456 if (flags & MNT_FORCE)
457 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
458 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
459 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
461 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
462 if (mp->mnt_kern_flag & MNTK_MWAIT)
467 if (mp->mnt_flag & MNT_EXPUBLIC)
468 vfs_setpublicfs(NULL, NULL, NULL);
470 vfs_msync(mp, MNT_WAIT);
471 async_flag = mp->mnt_flag & MNT_ASYNC;
472 mp->mnt_flag &=~ MNT_ASYNC;
473 cache_purgevfs(mp); /* remove cache entries for this file sys */
474 if (mp->mnt_syncer != NULL)
475 vrele(mp->mnt_syncer);
476 if (((mp->mnt_flag & MNT_RDONLY) ||
477 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
479 error = VFS_UNMOUNT(mp, flags, td);
480 lwkt_gettokref(&ilock);
482 if (mp->mnt_syncer == NULL)
483 vfs_allocate_syncvnode(mp);
484 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
485 mp->mnt_flag |= async_flag;
486 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
488 if (mp->mnt_kern_flag & MNTK_MWAIT)
492 TAILQ_REMOVE(&mountlist, mp, mnt_list);
493 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
494 coveredvp->v_mountedhere = NULL;
497 mp->mnt_vfc->vfc_refcount--;
498 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
499 panic("unmount: dangling vnode");
500 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
501 if (mp->mnt_kern_flag & MNTK_MWAIT)
503 free((caddr_t)mp, M_MOUNT);
508 * Sync each mounted filesystem.
512 static int syncprt = 0;
513 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
518 sync(struct sync_args *uap)
520 struct thread *td = curthread;
521 struct mount *mp, *nmp;
525 lwkt_gettoken(&ilock, &mountlist_token);
526 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
527 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
528 nmp = TAILQ_NEXT(mp, mnt_list);
531 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
532 asyncflag = mp->mnt_flag & MNT_ASYNC;
533 mp->mnt_flag &= ~MNT_ASYNC;
534 vfs_msync(mp, MNT_NOWAIT);
535 VFS_SYNC(mp, MNT_NOWAIT, td);
536 mp->mnt_flag |= asyncflag;
538 lwkt_gettokref(&ilock);
539 nmp = TAILQ_NEXT(mp, mnt_list);
542 lwkt_reltoken(&ilock);
545 * XXX don't call vfs_bufstats() yet because that routine
546 * was not imported in the Lite2 merge.
551 #endif /* DIAGNOSTIC */
556 /* XXX PRISON: could be per prison flag */
557 static int prison_quotas;
559 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
563 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
565 * Change filesystem quotas.
569 quotactl(struct quotactl_args *uap)
571 struct thread *td = curthread;
572 struct proc *p = td->td_proc;
578 if (p->p_ucred->cr_prison && !prison_quotas)
580 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
581 SCARG(uap, path), td);
582 if ((error = namei(&nd)) != 0)
584 mp = nd.ni_vp->v_mount;
585 NDFREE(&nd, NDF_ONLY_PNBUF);
587 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
588 SCARG(uap, arg), td));
592 kern_statfs(struct nameidata *nd, struct statfs *buf)
594 struct thread *td = curthread;
602 mp = nd->ni_vp->v_mount;
604 NDFREE(nd, NDF_ONLY_PNBUF);
606 error = VFS_STATFS(mp, sp, td);
609 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
610 bcopy(sp, buf, sizeof(*buf));
611 /* Only root should have access to the fsid's. */
613 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
618 * statfs_args(char *path, struct statfs *buf)
620 * Get filesystem statistics.
623 statfs(struct statfs_args *uap)
625 struct thread *td = curthread;
630 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
632 error = kern_statfs(&nd, &buf);
635 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
640 kern_fstatfs(int fd, struct statfs *buf)
642 struct thread *td = curthread;
643 struct proc *p = td->td_proc;
650 error = getvnode(p->p_fd, fd, &fp);
653 mp = ((struct vnode *)fp->f_data)->v_mount;
657 error = VFS_STATFS(mp, sp, td);
660 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
661 bcopy(sp, buf, sizeof(*buf));
662 /* Only root should have access to the fsid's. */
664 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
669 * fstatfs_args(int fd, struct statfs *buf)
671 * Get filesystem statistics.
674 fstatfs(struct fstatfs_args *uap)
679 error = kern_fstatfs(uap->fd, &buf);
682 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
687 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
689 * Get statistics on all filesystems.
693 getfsstat(struct getfsstat_args *uap)
695 struct thread *td = curthread;
696 struct mount *mp, *nmp;
700 long count, maxcount, error;
702 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
703 sfsp = (caddr_t)SCARG(uap, buf);
705 lwkt_gettoken(&ilock, &mountlist_token);
706 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
707 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
708 nmp = TAILQ_NEXT(mp, mnt_list);
711 if (sfsp && count < maxcount) {
714 * If MNT_NOWAIT or MNT_LAZY is specified, do not
715 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
716 * overrides MNT_WAIT.
718 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
719 (SCARG(uap, flags) & MNT_WAIT)) &&
720 (error = VFS_STATFS(mp, sp, td))) {
721 lwkt_gettokref(&ilock);
722 nmp = TAILQ_NEXT(mp, mnt_list);
726 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
727 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
735 lwkt_gettokref(&ilock);
736 nmp = TAILQ_NEXT(mp, mnt_list);
739 lwkt_reltoken(&ilock);
740 if (sfsp && count > maxcount)
741 uap->sysmsg_result = maxcount;
743 uap->sysmsg_result = count;
748 * fchdir_args(int fd)
750 * Change current working directory to a given file descriptor.
754 fchdir(struct fchdir_args *uap)
756 struct thread *td = curthread;
757 struct proc *p = td->td_proc;
758 struct filedesc *fdp = p->p_fd;
759 struct vnode *vp, *tdp;
764 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
766 vp = (struct vnode *)fp->f_data;
768 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
769 if (vp->v_type != VDIR)
772 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
773 while (!error && (mp = vp->v_mountedhere) != NULL) {
774 if (vfs_busy(mp, 0, NULL, td))
776 error = VFS_ROOT(mp, &tdp);
787 VOP_UNLOCK(vp, NULL, 0, td);
794 kern_chdir(struct nameidata *nd)
796 struct thread *td = curthread;
797 struct proc *p = td->td_proc;
798 struct filedesc *fdp = p->p_fd;
801 if ((error = namei(nd)) != 0)
803 if ((error = checkvp_chdir(nd->ni_vp, td)) == 0) {
805 fdp->fd_cdir = nd->ni_vp;
808 NDFREE(nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
813 * chdir_args(char *path)
815 * Change current working directory (``.'').
818 chdir(struct chdir_args *uap)
820 struct thread *td = curthread;
824 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
827 error = kern_chdir(&nd);
833 * Helper function for raised chroot(2) security function: Refuse if
834 * any filedescriptors are open directories.
837 chroot_refuse_vdir_fds(fdp)
838 struct filedesc *fdp;
845 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
846 error = getvnode(fdp, fd, &fp);
849 vp = (struct vnode *)fp->f_data;
850 if (vp->v_type != VDIR)
858 * This sysctl determines if we will allow a process to chroot(2) if it
859 * has a directory open:
860 * 0: disallowed for all processes.
861 * 1: allowed for processes that were not already chroot(2)'ed.
862 * 2: allowed for all processes.
865 static int chroot_allow_open_directories = 1;
867 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
868 &chroot_allow_open_directories, 0, "");
871 * Chroot to the specified vnode. vp must be locked and referenced on
872 * call, and will be left locked and referenced on return. This routine
873 * may acquire additional refs on the vnode when associating it with
874 * the process's root and/or jail dirs.
877 kern_chroot(struct vnode *vp)
879 struct thread *td = curthread;
880 struct proc *p = td->td_proc;
881 struct filedesc *fdp = p->p_fd;
885 * Only root can chroot
887 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
891 * Disallow open directory descriptors (fchdir() breakouts).
893 if (chroot_allow_open_directories == 0 ||
894 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
895 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
900 * Check the validity of vp as a directory to change to and
901 * associate it with rdir/jdir.
903 if ((error = checkvp_chdir(vp, td)) == 0) {
907 if (fdp->fd_jdir == NULL) {
916 * chroot_args(char *path)
918 * Change notion of root (``/'') directory.
922 chroot(struct chroot_args *uap)
924 struct thread *td = curthread;
928 KKASSERT(td->td_proc);
929 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
930 SCARG(uap, path), td);
931 if ((error = namei(&nd)) == 0) {
932 error = kern_chroot(nd.ni_vp);
933 NDFREE(&nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
939 * Common routine for chroot and chdir. Given a locked, referenced vnode,
940 * determine whether it is legal to chdir to the vnode. The vnode's state
941 * is not changed by this call.
944 checkvp_chdir(struct vnode *vp, struct thread *td)
948 if (vp->v_type != VDIR)
951 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
956 kern_open(struct nameidata *nd, int oflags, int mode, int *res)
958 struct thread *td = curthread;
959 struct proc *p = td->td_proc;
960 struct filedesc *fdp = p->p_fd;
965 int type, indx, error;
968 if ((oflags & O_ACCMODE) == O_ACCMODE)
970 flags = FFLAGS(oflags);
971 error = falloc(p, &nfp, &indx);
975 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
976 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
978 * Bump the ref count to prevent another process from closing
979 * the descriptor while we are blocked in vn_open()
982 error = vn_open(nd, flags, cmode);
985 * release our own reference
990 * handle special fdopen() case. bleh. dupfdopen() is
991 * responsible for dropping the old contents of ofiles[indx]
994 if ((error == ENODEV || error == ENXIO) &&
995 p->p_dupfd >= 0 && /* XXX from fdopen */
997 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
1002 * Clean up the descriptor, but only if another thread hadn't
1003 * replaced or closed it.
1005 if (fdp->fd_ofiles[indx] == fp) {
1006 fdp->fd_ofiles[indx] = NULL;
1010 if (error == ERESTART)
1015 NDFREE(nd, NDF_ONLY_PNBUF);
1019 * There should be 2 references on the file, one from the descriptor
1020 * table, and one for us.
1022 * Handle the case where someone closed the file (via its file
1023 * descriptor) while we were blocked. The end result should look
1024 * like opening the file succeeded but it was immediately closed.
1026 if (fp->f_count == 1) {
1027 KASSERT(fdp->fd_ofiles[indx] != fp,
1028 ("Open file descriptor lost all refs"));
1029 VOP_UNLOCK(vp, NULL, 0, td);
1030 vn_close(vp, flags & FMASK, td);
1036 fp->f_data = (caddr_t)vp;
1037 fp->f_flag = flags & FMASK;
1039 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1040 if (flags & (O_EXLOCK | O_SHLOCK)) {
1041 lf.l_whence = SEEK_SET;
1044 if (flags & O_EXLOCK)
1045 lf.l_type = F_WRLCK;
1047 lf.l_type = F_RDLCK;
1049 if ((flags & FNONBLOCK) == 0)
1051 VOP_UNLOCK(vp, NULL, 0, td);
1052 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1054 * lock request failed. Normally close the descriptor
1055 * but handle the case where someone might have dup()d
1056 * it when we weren't looking. One reference is
1057 * owned by the descriptor array, the other by us.
1059 if (fdp->fd_ofiles[indx] == fp) {
1060 fdp->fd_ofiles[indx] = NULL;
1066 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1067 fp->f_flag |= FHASLOCK;
1069 /* assert that vn_open created a backing object if one is needed */
1070 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1071 ("open: vmio vnode has no backing object after vn_open"));
1072 VOP_UNLOCK(vp, NULL, 0, td);
1075 * release our private reference, leaving the one associated with the
1076 * descriptor table intact.
1084 * open_args(char *path, int flags, int mode)
1086 * Check permissions, allocate an open file structure,
1087 * and call the device open routine if any.
1090 open(struct open_args *uap)
1092 struct thread *td = curthread;
1093 struct nameidata nd;
1096 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1098 error = kern_open(&nd, uap->flags, uap->mode, &uap->sysmsg_result);
1104 kern_mknod(struct nameidata *nd, int mode, int dev)
1106 struct thread *td = curthread;
1107 struct proc *p = td->td_proc;
1115 switch (mode & S_IFMT) {
1121 error = suser_cred(p->p_ucred, PRISON_ROOT);
1135 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1136 vattr.va_rdev = dev;
1139 switch (mode & S_IFMT) {
1140 case S_IFMT: /* used by badsect to flag bad sectors */
1141 vattr.va_type = VBAD;
1144 vattr.va_type = VCHR;
1147 vattr.va_type = VBLK;
1158 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1160 error = VOP_WHITEOUT(nd->ni_dvp, NCPNULL,
1161 &nd->ni_cnd, NAMEI_CREATE);
1163 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp,
1164 &nd->ni_cnd, &vattr);
1168 NDFREE(nd, NDF_ONLY_PNBUF);
1171 NDFREE(nd, NDF_ONLY_PNBUF);
1172 if (nd->ni_dvp == vp)
1179 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mknod");
1180 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mknod");
1185 * mknod_args(char *path, int mode, int dev)
1187 * Create a special file.
1190 mknod(struct mknod_args *uap)
1192 struct thread *td = curthread;
1193 struct nameidata nd;
1196 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1199 error = kern_mknod(&nd, uap->mode, uap->dev);
1205 kern_mkfifo(struct nameidata *nd, int mode)
1207 struct thread *td = curthread;
1208 struct proc *p = td->td_proc;
1216 if (nd->ni_vp != NULL) {
1217 NDFREE(nd, NDF_ONLY_PNBUF);
1218 if (nd->ni_dvp == nd->ni_vp)
1226 vattr.va_type = VFIFO;
1227 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1228 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1229 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd, &vattr);
1232 NDFREE(nd, NDF_ONLY_PNBUF);
1238 * mkfifo_args(char *path, int mode)
1240 * Create a named pipe.
1243 mkfifo(struct mkfifo_args *uap)
1245 struct thread *td = curthread;
1246 struct nameidata nd;
1249 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1252 error = kern_mkfifo(&nd, uap->mode);
1258 kern_link(struct nameidata *nd, struct nameidata *linknd)
1260 struct thread *td = curthread;
1261 struct proc *p = td->td_proc;
1269 NDFREE(nd, NDF_ONLY_PNBUF);
1271 if (vp->v_type == VDIR)
1272 error = EPERM; /* POSIX */
1274 error = namei(linknd);
1276 if (linknd->ni_vp != NULL) {
1278 vrele(linknd->ni_vp);
1281 VOP_LEASE(linknd->ni_dvp, td, p->p_ucred,
1283 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1284 error = VOP_LINK(linknd->ni_dvp, NCPNULL, vp,
1287 NDFREE(linknd, NDF_ONLY_PNBUF);
1288 if (linknd->ni_dvp == linknd->ni_vp)
1289 vrele(linknd->ni_dvp);
1291 vput(linknd->ni_dvp);
1292 ASSERT_VOP_UNLOCKED(linknd->ni_dvp, "link");
1293 ASSERT_VOP_UNLOCKED(linknd->ni_vp, "link");
1301 * link_args(char *path, char *link)
1303 * Make a hard file link.
1306 link(struct link_args *uap)
1308 struct thread *td = curthread;
1309 struct nameidata nd, linknd;
1312 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1314 NDINIT(&linknd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1315 UIO_USERSPACE, uap->link, td);
1317 error = kern_link(&nd, &linknd);
1323 kern_symlink(char *path, struct nameidata *nd)
1325 struct thread *td = curthread;
1326 struct proc *p = td->td_proc;
1335 NDFREE(nd, NDF_ONLY_PNBUF);
1336 if (nd->ni_dvp == nd->ni_vp)
1344 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1345 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1346 error = VOP_SYMLINK(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
1348 NDFREE(nd, NDF_ONLY_PNBUF);
1352 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "symlink");
1353 ASSERT_VOP_UNLOCKED(nd->ni_vp, "symlink");
1359 * symlink(char *path, char *link)
1361 * Make a symbolic link.
1364 symlink(struct symlink_args *uap)
1366 struct thread *td = curthread;
1367 struct nameidata nd;
1371 path = zalloc(namei_zone);
1372 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1374 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1375 UIO_USERSPACE, uap->link, td);
1376 error = kern_symlink(path, &nd);
1378 zfree(namei_zone, path);
1383 * undelete_args(char *path)
1385 * Delete a whiteout from the filesystem.
1389 undelete(struct undelete_args *uap)
1391 struct thread *td = curthread;
1392 struct proc *p = td->td_proc;
1394 struct nameidata nd;
1397 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
1398 SCARG(uap, path), td);
1403 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
1404 NDFREE(&nd, NDF_ONLY_PNBUF);
1405 if (nd.ni_dvp == nd.ni_vp)
1414 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1415 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
1416 NDFREE(&nd, NDF_ONLY_PNBUF);
1418 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1419 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1424 kern_unlink(struct nameidata *nd)
1426 struct thread *td = curthread;
1427 struct proc *p = td->td_proc;
1436 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1437 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1439 if (vp->v_type == VDIR)
1440 error = EPERM; /* POSIX */
1443 * The root of a mounted filesystem cannot be deleted.
1445 * XXX: can this only be a VDIR case?
1447 if (vp->v_flag & VROOT)
1452 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1453 error = VOP_REMOVE(nd->ni_dvp, NCPNULL, vp, &nd->ni_cnd);
1455 NDFREE(nd, NDF_ONLY_PNBUF);
1456 if (nd->ni_dvp == vp)
1462 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "unlink");
1463 ASSERT_VOP_UNLOCKED(nd->ni_vp, "unlink");
1468 * unlink_args(char *path)
1470 * Delete a name from the filesystem.
1473 unlink(struct unlink_args *uap)
1475 struct thread *td = curthread;
1476 struct nameidata nd;
1479 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1482 error = kern_unlink(&nd);
1488 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1490 struct thread *td = curthread;
1491 struct proc *p = td->td_proc;
1492 struct filedesc *fdp = p->p_fd;
1497 if (fd >= fdp->fd_nfiles ||
1498 (fp = fdp->fd_ofiles[fd]) == NULL)
1500 if (fp->f_type != DTYPE_VNODE)
1504 fp->f_offset += offset;
1507 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1510 fp->f_offset = offset + vattr.va_size;
1513 fp->f_offset = offset;
1518 *res = fp->f_offset;
1523 * lseek_args(int fd, int pad, off_t offset, int whence)
1525 * Reposition read/write file offset.
1528 lseek(struct lseek_args *uap)
1532 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1533 &uap->sysmsg_offset);
1539 kern_access(struct nameidata *nd, int aflags)
1541 struct thread *td = curthread;
1542 struct proc *p = td->td_proc;
1543 struct ucred *cred, *tmpcred;
1549 * Create and modify a temporary credential instead of one that
1550 * is potentially shared. This could also mess up socket
1551 * buffer accounting which can run in an interrupt context.
1553 tmpcred = crdup(cred);
1554 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1555 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1556 p->p_ucred = tmpcred;
1557 nd->ni_cnd.cn_cred = tmpcred;
1563 /* Flags == 0 means only check for existence. */
1572 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1573 error = VOP_ACCESS(vp, flags, tmpcred, td);
1575 NDFREE(nd, NDF_ONLY_PNBUF);
1584 * access_args(char *path, int flags)
1586 * Check access permissions.
1589 access(struct access_args *uap)
1591 struct thread *td = curthread;
1592 struct nameidata nd;
1595 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1596 UIO_USERSPACE, uap->path, td);
1598 error = kern_access(&nd, uap->flags);
1604 kern_stat(struct nameidata *nd, struct stat *st)
1606 struct thread *td = curthread;
1612 error = vn_stat(nd->ni_vp, st, td);
1613 NDFREE(nd, NDF_ONLY_PNBUF);
1619 * stat_args(char *path, struct stat *ub)
1621 * Get file status; this version follows links.
1624 stat(struct stat_args *uap)
1626 struct thread *td = curthread;
1627 struct nameidata nd;
1631 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1632 UIO_USERSPACE, uap->path, td);
1634 error = kern_stat(&nd, &st);
1637 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1642 * lstat_args(char *path, struct stat *ub)
1644 * Get file status; this version does not follow links.
1647 lstat(struct lstat_args *uap)
1649 struct thread *td = curthread;
1650 struct nameidata nd;
1654 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1655 UIO_USERSPACE, SCARG(uap, path), td);
1657 error = kern_stat(&nd, &st);
1660 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1669 nsb->st_dev = sb->st_dev;
1670 nsb->st_ino = sb->st_ino;
1671 nsb->st_mode = sb->st_mode;
1672 nsb->st_nlink = sb->st_nlink;
1673 nsb->st_uid = sb->st_uid;
1674 nsb->st_gid = sb->st_gid;
1675 nsb->st_rdev = sb->st_rdev;
1676 nsb->st_atimespec = sb->st_atimespec;
1677 nsb->st_mtimespec = sb->st_mtimespec;
1678 nsb->st_ctimespec = sb->st_ctimespec;
1679 nsb->st_size = sb->st_size;
1680 nsb->st_blocks = sb->st_blocks;
1681 nsb->st_blksize = sb->st_blksize;
1682 nsb->st_flags = sb->st_flags;
1683 nsb->st_gen = sb->st_gen;
1684 nsb->st_qspare[0] = sb->st_qspare[0];
1685 nsb->st_qspare[1] = sb->st_qspare[1];
1689 * nstat_args(char *path, struct nstat *ub)
1693 nstat(struct nstat_args *uap)
1695 struct thread *td = curthread;
1699 struct nameidata nd;
1701 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1702 UIO_USERSPACE, SCARG(uap, path), td);
1703 if ((error = namei(&nd)) != 0)
1705 NDFREE(&nd, NDF_ONLY_PNBUF);
1706 error = vn_stat(nd.ni_vp, &sb, td);
1710 cvtnstat(&sb, &nsb);
1711 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1716 * lstat_args(char *path, struct stat *ub)
1718 * Get file status; this version does not follow links.
1722 nlstat(struct nlstat_args *uap)
1724 struct thread *td = curthread;
1729 struct nameidata nd;
1731 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1732 UIO_USERSPACE, SCARG(uap, path), td);
1733 if ((error = namei(&nd)) != 0)
1736 NDFREE(&nd, NDF_ONLY_PNBUF);
1737 error = vn_stat(vp, &sb, td);
1741 cvtnstat(&sb, &nsb);
1742 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1747 * pathconf_Args(char *path, int name)
1749 * Get configurable pathname variables.
1753 pathconf(struct pathconf_args *uap)
1755 struct thread *td = curthread;
1757 struct nameidata nd;
1759 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1760 UIO_USERSPACE, SCARG(uap, path), td);
1761 if ((error = namei(&nd)) != 0)
1763 NDFREE(&nd, NDF_ONLY_PNBUF);
1764 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1771 * kern_readlink isn't properly split yet. There is a copyin burried
1772 * in VOP_READLINK().
1775 kern_readlink(struct nameidata *nd, char *buf, int count, int *res)
1777 struct thread *td = curthread;
1778 struct proc *p = td->td_proc;
1787 NDFREE(nd, NDF_ONLY_PNBUF);
1789 if (vp->v_type != VLNK)
1792 aiov.iov_base = buf;
1793 aiov.iov_len = count;
1794 auio.uio_iov = &aiov;
1795 auio.uio_iovcnt = 1;
1796 auio.uio_offset = 0;
1797 auio.uio_rw = UIO_READ;
1798 auio.uio_segflg = UIO_USERSPACE;
1800 auio.uio_resid = count;
1801 error = VOP_READLINK(vp, &auio, p->p_ucred);
1804 *res = count - auio.uio_resid;
1809 * readlink_args(char *path, char *buf, int count)
1811 * Return target name of a symbolic link.
1814 readlink(struct readlink_args *uap)
1816 struct thread *td = curthread;
1817 struct nameidata nd;
1820 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ, UIO_USERSPACE,
1823 error = kern_readlink(&nd, uap->buf, uap->count,
1824 &uap->sysmsg_result);
1830 setfflags(struct vnode *vp, int flags)
1832 struct thread *td = curthread;
1833 struct proc *p = td->td_proc;
1838 * Prevent non-root users from setting flags on devices. When
1839 * a device is reused, users can retain ownership of the device
1840 * if they are allowed to set flags and programs assume that
1841 * chown can't fail when done as root.
1843 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1844 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1847 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1848 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1850 vattr.va_flags = flags;
1851 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1852 VOP_UNLOCK(vp, NULL, 0, td);
1857 * chflags(char *path, int flags)
1859 * Change flags of a file given a path name.
1863 chflags(struct chflags_args *uap)
1865 struct thread *td = curthread;
1867 struct nameidata nd;
1869 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1870 SCARG(uap, path), td);
1871 if ((error = namei(&nd)) != 0)
1873 NDFREE(&nd, NDF_ONLY_PNBUF);
1874 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1880 * fchflags_args(int fd, int flags)
1882 * Change flags of a file given a file descriptor.
1886 fchflags(struct fchflags_args *uap)
1888 struct thread *td = curthread;
1889 struct proc *p = td->td_proc;
1893 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1895 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1899 setfmode(struct vnode *vp, int mode)
1901 struct thread *td = curthread;
1902 struct proc *p = td->td_proc;
1906 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1907 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1909 vattr.va_mode = mode & ALLPERMS;
1910 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1911 VOP_UNLOCK(vp, NULL, 0, td);
1916 kern_chmod(struct nameidata *nd, int mode)
1923 NDFREE(nd, NDF_ONLY_PNBUF);
1924 error = setfmode(nd->ni_vp, mode);
1930 * chmod_args(char *path, int mode)
1932 * Change mode of a file given path name.
1936 chmod(struct chmod_args *uap)
1938 struct thread *td = curthread;
1939 struct nameidata nd;
1942 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1944 error = kern_chmod(&nd, uap->mode);
1950 * lchmod_args(char *path, int mode)
1952 * Change mode of a file given path name (don't follow links.)
1956 lchmod(struct lchmod_args *uap)
1958 struct thread *td = curthread;
1960 struct nameidata nd;
1962 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
1963 if ((error = namei(&nd)) != 0)
1965 NDFREE(&nd, NDF_ONLY_PNBUF);
1966 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1972 * fchmod_args(int fd, int mode)
1974 * Change mode of a file given a file descriptor.
1978 fchmod(struct fchmod_args *uap)
1980 struct thread *td = curthread;
1981 struct proc *p = td->td_proc;
1985 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1987 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1991 setfown(struct vnode *vp, uid_t uid, gid_t gid)
1993 struct thread *td = curthread;
1994 struct proc *p = td->td_proc;
1998 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1999 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2003 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2004 VOP_UNLOCK(vp, NULL, 0, td);
2009 kern_chown(struct nameidata *nd, int uid, int gid)
2016 NDFREE(nd, NDF_ONLY_PNBUF);
2017 error = setfown(nd->ni_vp, uid, gid);
2023 * chown(char *path, int uid, int gid)
2025 * Set ownership given a path name.
2028 chown(struct chown_args *uap)
2030 struct thread *td = curthread;
2031 struct nameidata nd;
2034 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2036 error = kern_chown(&nd, uap->uid, uap->gid);
2042 * lchown_args(char *path, int uid, int gid)
2044 * Set ownership given a path name, do not cross symlinks.
2047 lchown(struct lchown_args *uap)
2049 struct thread *td = curthread;
2051 struct nameidata nd;
2053 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2055 error = kern_chown(&nd, uap->uid, uap->gid);
2061 * fchown_args(int fd, int uid, int gid)
2063 * Set ownership given a file descriptor.
2067 fchown(struct fchown_args *uap)
2069 struct thread *td = curthread;
2070 struct proc *p = td->td_proc;
2074 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2076 return setfown((struct vnode *)fp->f_data,
2077 SCARG(uap, uid), SCARG(uap, gid));
2081 getutimes(const struct timeval *tvp, struct timespec *tsp)
2083 struct timeval tv[2];
2087 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2090 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2091 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2097 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2099 struct thread *td = curthread;
2100 struct proc *p = td->td_proc;
2104 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2105 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2107 vattr.va_atime = ts[0];
2108 vattr.va_mtime = ts[1];
2110 vattr.va_vaflags |= VA_UTIMES_NULL;
2111 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2112 VOP_UNLOCK(vp, NULL, 0, td);
2117 kern_utimes(struct nameidata *nd, struct timeval *tptr)
2119 struct timespec ts[2];
2122 error = getutimes(tptr, ts);
2128 NDFREE(nd, NDF_ONLY_PNBUF);
2129 error = setutimes(nd->ni_vp, ts, tptr == NULL);
2135 * utimes_args(char *path, struct timeval *tptr)
2137 * Set the access and modification times of a file.
2140 utimes(struct utimes_args *uap)
2142 struct thread *td = curthread;
2143 struct timeval tv[2];
2144 struct nameidata nd;
2148 error = copyin(uap->tptr, tv, sizeof(tv));
2152 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2154 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2160 * lutimes_args(char *path, struct timeval *tptr)
2162 * Set the access and modification times of a file.
2165 lutimes(struct lutimes_args *uap)
2167 struct thread *td = curthread;
2168 struct timeval tv[2];
2169 struct nameidata nd;
2173 error = copyin(uap->tptr, tv, sizeof(tv));
2177 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2179 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2185 kern_futimes(int fd, struct timeval *tptr)
2187 struct thread *td = curthread;
2188 struct proc *p = td->td_proc;
2189 struct timespec ts[2];
2193 error = getutimes(tptr, ts);
2196 error = getvnode(p->p_fd, fd, &fp);
2199 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2204 * futimes_args(int fd, struct timeval *tptr)
2206 * Set the access and modification times of a file.
2209 futimes(struct futimes_args *uap)
2211 struct timeval tv[2];
2215 error = copyin(uap->tptr, tv, sizeof(tv));
2220 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2226 kern_truncate(struct nameidata* nd, off_t length)
2228 struct thread *td = curthread;
2229 struct proc *p = td->td_proc;
2236 if ((error = namei(nd)) != 0)
2239 NDFREE(nd, NDF_ONLY_PNBUF);
2240 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2241 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2242 if (vp->v_type == VDIR)
2244 else if ((error = vn_writechk(vp)) == 0 &&
2245 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2247 vattr.va_size = length;
2248 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2255 * truncate(char *path, int pad, off_t length)
2257 * Truncate a file given its path name.
2260 truncate(struct truncate_args *uap)
2262 struct thread *td = curthread;
2263 struct nameidata nd;
2266 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2268 error = kern_truncate(&nd, uap->length);
2274 kern_ftruncate(int fd, off_t length)
2276 struct thread *td = curthread;
2277 struct proc *p = td->td_proc;
2285 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2287 if ((fp->f_flag & FWRITE) == 0)
2289 vp = (struct vnode *)fp->f_data;
2290 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2291 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2292 if (vp->v_type == VDIR)
2294 else if ((error = vn_writechk(vp)) == 0) {
2296 vattr.va_size = length;
2297 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2299 VOP_UNLOCK(vp, NULL, 0, td);
2304 * ftruncate_args(int fd, int pad, off_t length)
2306 * Truncate a file given a file descriptor.
2309 ftruncate(struct ftruncate_args *uap)
2313 error = kern_ftruncate(uap->fd, uap->length);
2321 * Sync an open file.
2325 fsync(struct fsync_args *uap)
2327 struct thread *td = curthread;
2328 struct proc *p = td->td_proc;
2334 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2336 vp = (struct vnode *)fp->f_data;
2337 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2338 if (VOP_GETVOBJECT(vp, &obj) == 0)
2339 vm_object_page_clean(obj, 0, 0, 0);
2340 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2341 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2343 error = (*bioops.io_fsync)(vp);
2344 VOP_UNLOCK(vp, NULL, 0, td);
2349 kern_rename(struct nameidata *fromnd, struct nameidata *tond)
2351 struct thread *td = curthread;
2352 struct proc *p = td->td_proc;
2353 struct vnode *tvp, *fvp, *tdvp;
2357 error = namei(fromnd);
2360 fvp = fromnd->ni_vp;
2361 if (fromnd->ni_vp->v_type == VDIR)
2362 tond->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2363 error = namei(tond);
2365 /* Translate error code for rename("dir1", "dir2/."). */
2366 if (error == EISDIR && fvp->v_type == VDIR)
2368 NDFREE(fromnd, NDF_ONLY_PNBUF);
2369 vrele(fromnd->ni_dvp);
2373 tdvp = tond->ni_dvp;
2376 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2379 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2387 * If the source is the same as the destination (that is, if they
2388 * are links to the same vnode), then there is nothing to do.
2394 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2395 if (fromnd->ni_dvp != tdvp) {
2396 VOP_LEASE(fromnd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2399 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2401 error = VOP_RENAME(fromnd->ni_dvp, NCPNULL, fromnd->ni_vp,
2402 &fromnd->ni_cnd, tond->ni_dvp, NCPNULL, tond->ni_vp,
2404 NDFREE(fromnd, NDF_ONLY_PNBUF);
2405 NDFREE(tond, NDF_ONLY_PNBUF);
2407 NDFREE(fromnd, NDF_ONLY_PNBUF);
2408 NDFREE(tond, NDF_ONLY_PNBUF);
2415 vrele(fromnd->ni_dvp);
2418 vrele(tond->ni_startdir);
2419 ASSERT_VOP_UNLOCKED(fromnd->ni_dvp, "rename");
2420 ASSERT_VOP_UNLOCKED(fromnd->ni_vp, "rename");
2421 ASSERT_VOP_UNLOCKED(tond->ni_dvp, "rename");
2422 ASSERT_VOP_UNLOCKED(tond->ni_vp, "rename");
2424 if (fromnd->ni_startdir)
2425 vrele(fromnd->ni_startdir);
2432 * rename_args(char *from, char *to)
2434 * Rename files. Source and destination must either both be directories,
2435 * or both not be directories. If target is a directory, it must be empty.
2438 rename(struct rename_args *uap)
2440 struct thread *td = curthread;
2441 struct nameidata fromnd, tond;
2444 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2445 UIO_USERSPACE, uap->from, td);
2446 NDINIT(&tond, NAMEI_RENAME,
2447 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2448 CNP_SAVESTART | CNP_NOOBJ,
2449 UIO_USERSPACE, uap->to, td);
2451 error = kern_rename(&fromnd, &tond);
2457 kern_mkdir(struct nameidata *nd, int mode)
2459 struct thread *td = curthread;
2460 struct proc *p = td->td_proc;
2466 nd->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2472 NDFREE(nd, NDF_ONLY_PNBUF);
2473 if (nd->ni_dvp == vp)
2481 vattr.va_type = VDIR;
2482 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2483 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2484 error = VOP_MKDIR(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
2486 NDFREE(nd, NDF_ONLY_PNBUF);
2490 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mkdir");
2491 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mkdir");
2496 * mkdir_args(char *path, int mode)
2498 * Make a directory file.
2502 mkdir(struct mkdir_args *uap)
2504 struct thread *td = curthread;
2505 struct nameidata nd;
2508 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
2511 error = kern_mkdir(&nd, uap->mode);
2517 kern_rmdir(struct nameidata *nd)
2519 struct thread *td = curthread;
2520 struct proc *p = td->td_proc;
2529 if (vp->v_type != VDIR) {
2534 * No rmdir "." please.
2536 if (nd->ni_dvp == vp) {
2541 * The root of a mounted filesystem cannot be deleted.
2543 if (vp->v_flag & VROOT)
2546 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2547 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2548 error = VOP_RMDIR(nd->ni_dvp, NCPNULL, nd->ni_vp,
2552 NDFREE(nd, NDF_ONLY_PNBUF);
2553 if (nd->ni_dvp == vp)
2559 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "rmdir");
2560 ASSERT_VOP_UNLOCKED(nd->ni_vp, "rmdir");
2565 * rmdir_args(char *path)
2567 * Remove a directory file.
2571 rmdir(struct rmdir_args *uap)
2573 struct thread *td = curthread;
2574 struct nameidata nd;
2577 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF,
2578 UIO_USERSPACE, uap->path, td);
2580 error = kern_rmdir(&nd);
2586 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2588 struct thread *td = curthread;
2589 struct proc *p = td->td_proc;
2597 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2599 if ((fp->f_flag & FREAD) == 0)
2601 vp = (struct vnode *)fp->f_data;
2603 if (vp->v_type != VDIR)
2605 aiov.iov_base = buf;
2606 aiov.iov_len = count;
2607 auio.uio_iov = &aiov;
2608 auio.uio_iovcnt = 1;
2609 auio.uio_rw = UIO_READ;
2610 auio.uio_segflg = UIO_USERSPACE;
2612 auio.uio_resid = count;
2613 /* vn_lock(vp, NULL, LK_SHARED | LK_RETRY, td); */
2614 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2615 loff = auio.uio_offset = fp->f_offset;
2616 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2617 fp->f_offset = auio.uio_offset;
2618 VOP_UNLOCK(vp, NULL, 0, td);
2621 if (count == auio.uio_resid) {
2622 if (union_dircheckp) {
2623 error = union_dircheckp(td, &vp, fp);
2629 if ((vp->v_flag & VROOT) &&
2630 (vp->v_mount->mnt_flag & MNT_UNION)) {
2631 struct vnode *tvp = vp;
2632 vp = vp->v_mount->mnt_vnodecovered;
2634 fp->f_data = (caddr_t) vp;
2643 *res = count - auio.uio_resid;
2648 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2650 * Read a block of directory entries in a file system independent format.
2653 getdirentries(struct getdirentries_args *uap)
2658 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2659 &uap->sysmsg_result);
2662 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2667 * getdents_args(int fd, char *buf, size_t count)
2670 getdents(struct getdents_args *uap)
2674 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2675 &uap->sysmsg_result);
2681 * umask(int newmask)
2683 * Set the mode mask for creation of filesystem nodes.
2688 umask(struct umask_args *uap)
2690 struct thread *td = curthread;
2691 struct proc *p = td->td_proc;
2692 struct filedesc *fdp;
2695 uap->sysmsg_result = fdp->fd_cmask;
2696 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2701 * revoke(char *path)
2703 * Void all references to file by ripping underlying filesystem
2708 revoke(struct revoke_args *uap)
2710 struct thread *td = curthread;
2711 struct proc *p = td->td_proc;
2715 struct nameidata nd;
2717 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2718 if ((error = namei(&nd)) != 0)
2721 NDFREE(&nd, NDF_ONLY_PNBUF);
2722 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2726 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2728 if (p->p_ucred->cr_uid != vattr.va_uid &&
2729 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2731 if (count_udev(vp->v_udev) > 0)
2732 VOP_REVOKE(vp, REVOKEALL);
2739 * Convert a user file descriptor to a kernel file entry.
2742 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2746 if ((u_int)fd >= fdp->fd_nfiles ||
2747 (fp = fdp->fd_ofiles[fd]) == NULL)
2749 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2755 * getfh_args(char *fname, fhandle_t *fhp)
2757 * Get (NFS) file handle
2760 getfh(struct getfh_args *uap)
2762 struct thread *td = curthread;
2763 struct nameidata nd;
2769 * Must be super user
2774 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2778 NDFREE(&nd, NDF_ONLY_PNBUF);
2780 bzero(&fh, sizeof(fh));
2781 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2782 error = VFS_VPTOFH(vp, &fh.fh_fid);
2786 error = copyout(&fh, uap->fhp, sizeof (fh));
2791 * fhopen_args(const struct fhandle *u_fhp, int flags)
2793 * syscall for the rpc.lockd to use to translate a NFS file handle into
2794 * an open descriptor.
2796 * warning: do not remove the suser() call or this becomes one giant
2800 fhopen(struct fhopen_args *uap)
2802 struct thread *td = curthread;
2803 struct proc *p = td->td_proc;
2808 struct vattr *vap = &vat;
2811 struct filedesc *fdp = p->p_fd;
2812 int fmode, mode, error, type;
2817 * Must be super user
2823 fmode = FFLAGS(SCARG(uap, flags));
2824 /* why not allow a non-read/write open for our lockd? */
2825 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2827 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2830 /* find the mount point */
2831 mp = vfs_getvfs(&fhp.fh_fsid);
2834 /* now give me my vnode, it gets returned to me locked */
2835 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2839 * from now on we have to make sure not
2840 * to forget about the vnode
2841 * any error that causes an abort must vput(vp)
2842 * just set error = err and 'goto bad;'.
2848 if (vp->v_type == VLNK) {
2852 if (vp->v_type == VSOCK) {
2857 if (fmode & (FWRITE | O_TRUNC)) {
2858 if (vp->v_type == VDIR) {
2862 error = vn_writechk(vp);
2870 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2874 if (fmode & O_TRUNC) {
2875 VOP_UNLOCK(vp, NULL, 0, td); /* XXX */
2876 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2877 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2880 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2884 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2888 * Make sure that a VM object is created for VMIO support.
2890 if (vn_canvmio(vp) == TRUE) {
2891 if ((error = vfs_object_create(vp, td)) != 0)
2898 * end of vn_open code
2901 if ((error = falloc(p, &nfp, &indx)) != 0) {
2909 * hold an extra reference to avoid having fp ripped out
2910 * from under us while we block in the lock op.
2913 nfp->f_data = (caddr_t)vp;
2914 nfp->f_flag = fmode & FMASK;
2915 nfp->f_ops = &vnops;
2916 nfp->f_type = DTYPE_VNODE;
2917 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2918 lf.l_whence = SEEK_SET;
2921 if (fmode & O_EXLOCK)
2922 lf.l_type = F_WRLCK;
2924 lf.l_type = F_RDLCK;
2926 if ((fmode & FNONBLOCK) == 0)
2928 VOP_UNLOCK(vp, NULL, 0, td);
2929 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2931 * lock request failed. Normally close the descriptor
2932 * but handle the case where someone might have dup()d
2933 * or close()d it when we weren't looking.
2935 if (fdp->fd_ofiles[indx] == fp) {
2936 fdp->fd_ofiles[indx] = NULL;
2941 * release our private reference.
2946 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2947 fp->f_flag |= FHASLOCK;
2949 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2950 vfs_object_create(vp, td);
2952 VOP_UNLOCK(vp, NULL, 0, td);
2954 uap->sysmsg_result = indx;
2963 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2966 fhstat(struct fhstat_args *uap)
2968 struct thread *td = curthread;
2976 * Must be super user
2982 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
2986 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2988 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
2990 error = vn_stat(vp, &sb, td);
2994 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
2999 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3002 fhstatfs(struct fhstatfs_args *uap)
3004 struct thread *td = curthread;
3013 * Must be super user
3015 if ((error = suser(td)))
3018 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3021 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3023 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3028 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3030 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3032 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3033 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3036 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3040 * Syscall to push extended attribute configuration information into the
3041 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3042 * a command (int cmd), and attribute name and misc data. For now, the
3043 * attribute name is left in userspace for consumption by the VFS_op.
3044 * It will probably be changed to be copied into sysspace by the
3045 * syscall in the future, once issues with various consumers of the
3046 * attribute code have raised their hands.
3048 * Currently this is used only by UFS Extended Attributes.
3051 extattrctl(struct extattrctl_args *uap)
3053 struct thread *td = curthread;
3054 struct nameidata nd;
3058 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3059 if ((error = namei(&nd)) != 0)
3061 mp = nd.ni_vp->v_mount;
3063 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3064 SCARG(uap, arg), td));
3068 * Syscall to set a named extended attribute on a file or directory.
3069 * Accepts attribute name, and a uio structure pointing to the data to set.
3070 * The uio is consumed in the style of writev(). The real work happens
3071 * in VOP_SETEXTATTR().
3074 extattr_set_file(struct extattr_set_file_args *uap)
3076 struct thread *td = curthread;
3077 struct proc *p = td->td_proc;
3078 struct nameidata nd;
3080 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3081 char attrname[EXTATTR_MAXNAMELEN];
3085 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3088 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3089 SCARG(uap, path), td);
3090 if ((error = namei(&nd)) != 0)
3092 iovlen = uap->iovcnt * sizeof(struct iovec);
3093 if (uap->iovcnt > UIO_SMALLIOV) {
3094 if (uap->iovcnt > UIO_MAXIOV) {
3098 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3103 auio.uio_iovcnt = uap->iovcnt;
3104 auio.uio_rw = UIO_WRITE;
3105 auio.uio_segflg = UIO_USERSPACE;
3107 auio.uio_offset = 0;
3108 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3111 for (i = 0; i < uap->iovcnt; i++) {
3112 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3116 auio.uio_resid += iov->iov_len;
3119 cnt = auio.uio_resid;
3120 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3121 cnt -= auio.uio_resid;
3122 uap->sysmsg_result = cnt;
3125 FREE(needfree, M_IOV);
3131 * Syscall to get a named extended attribute on a file or directory.
3132 * Accepts attribute name, and a uio structure pointing to a buffer for the
3133 * data. The uio is consumed in the style of readv(). The real work
3134 * happens in VOP_GETEXTATTR();
3137 extattr_get_file(struct extattr_get_file_args *uap)
3139 struct thread *td = curthread;
3140 struct proc *p = td->td_proc;
3141 struct nameidata nd;
3143 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3144 char attrname[EXTATTR_MAXNAMELEN];
3148 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3151 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3152 SCARG(uap, path), td);
3153 if ((error = namei(&nd)) != 0)
3155 iovlen = uap->iovcnt * sizeof (struct iovec);
3156 if (uap->iovcnt > UIO_SMALLIOV) {
3157 if (uap->iovcnt > UIO_MAXIOV) {
3161 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3168 auio.uio_iovcnt = uap->iovcnt;
3169 auio.uio_rw = UIO_READ;
3170 auio.uio_segflg = UIO_USERSPACE;
3172 auio.uio_offset = 0;
3173 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3176 for (i = 0; i < uap->iovcnt; i++) {
3177 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3181 auio.uio_resid += iov->iov_len;
3184 cnt = auio.uio_resid;
3185 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3186 cnt -= auio.uio_resid;
3187 uap->sysmsg_result = cnt;
3190 FREE(needfree, M_IOV);
3196 * Syscall to delete a named extended attribute from a file or directory.
3197 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3200 extattr_delete_file(struct extattr_delete_file_args *uap)
3202 struct thread *td = curthread;
3203 struct proc *p = td->td_proc;
3204 struct nameidata nd;
3205 char attrname[EXTATTR_MAXNAMELEN];
3208 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3211 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3212 SCARG(uap, path), td);
3213 if ((error = namei(&nd)) != 0)
3215 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);