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.41 2004/10/02 03:18:26 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/sysent.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/sysproto.h>
51 #include <sys/filedesc.h>
52 #include <sys/kernel.h>
53 #include <sys/fcntl.h>
55 #include <sys/linker.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
60 #include <sys/namei.h>
61 #include <sys/nlookup.h>
62 #include <sys/dirent.h>
63 #include <sys/extattr.h>
64 #include <sys/kern_syscall.h>
66 #include <machine/limits.h>
67 #include <vfs/union/union.h>
68 #include <sys/sysctl.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_zone.h>
72 #include <vm/vm_page.h>
74 #include <sys/file2.h>
76 static int checkvp_chdir (struct vnode *vn, struct thread *td);
77 static void checkdirs (struct vnode *olddp, struct namecache *ncp);
78 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
79 static int getutimes (const struct timeval *, struct timespec *);
80 static int setfown (struct vnode *, uid_t, gid_t);
81 static int setfmode (struct vnode *, int);
82 static int setfflags (struct vnode *, int);
83 static int setutimes (struct vnode *, const struct timespec *, int);
84 static int usermount = 0; /* if 1, non-root can mount fs. */
86 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
88 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
91 * Virtual File System System Calls
95 * Mount a file system.
98 * mount_args(char *type, char *path, int flags, caddr_t data)
102 mount(struct mount_args *uap)
104 struct thread *td = curthread;
105 struct proc *p = td->td_proc;
107 struct namecache *ncp;
109 struct vfsconf *vfsp;
110 int error, flag = 0, flag2 = 0;
112 struct nlookupdata nd;
113 char fstypename[MFSNAMELEN];
116 struct nlcomponent nlc;
119 if (p->p_ucred->cr_prison != NULL)
121 if (usermount == 0 && (error = suser(td)))
124 * Do not allow NFS export by non-root users.
126 if (SCARG(uap, flags) & MNT_EXPORTED) {
132 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
135 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
138 * Lookup the requested path and extract the ncp and vnode.
140 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
142 if ((error = nlookup(&nd)) == 0) {
143 if (nd.nl_ncp->nc_vp == NULL)
153 * Extract the locked+refd ncp and cleanup the nd structure
160 * now we have the locked ref'd ncp and unreferenced vnode.
163 if ((error = vget(vp, NULL, LK_EXCLUSIVE, td)) != 0) {
170 * Now we have an unlocked ref'd ncp and a locked ref'd vp
172 if (SCARG(uap, flags) & MNT_UPDATE) {
173 if ((vp->v_flag & VROOT) == 0) {
180 flag2 = mp->mnt_kern_flag;
182 * We only allow the filesystem to be reloaded if it
183 * is currently mounted read-only.
185 if ((SCARG(uap, flags) & MNT_RELOAD) &&
186 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
189 return (EOPNOTSUPP); /* Needs translation */
192 * Only root, or the user that did the original mount is
193 * permitted to update it.
195 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
196 (error = suser(td))) {
201 if (vfs_busy(mp, LK_NOWAIT, NULL, td)) {
206 lwkt_gettoken(&vlock, vp->v_interlock);
207 if ((vp->v_flag & VMOUNT) != 0 ||
208 vp->v_mountedhere != NULL) {
210 lwkt_reltoken(&vlock);
215 vp->v_flag |= VMOUNT;
216 lwkt_reltoken(&vlock);
218 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
219 VOP_UNLOCK(vp, NULL, 0, td);
223 * If the user is not root, ensure that they own the directory
224 * onto which we are attempting to mount.
226 if ((error = VOP_GETATTR(vp, &va, td)) ||
227 (va.va_uid != p->p_ucred->cr_uid &&
228 (error = suser(td)))) {
233 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
238 if (vp->v_type != VDIR) {
243 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
248 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
249 if (!strcmp(vfsp->vfc_name, fstypename))
255 /* Only load modules for root (very important!) */
256 if ((error = suser(td)) != 0) {
261 error = linker_load_file(fstypename, &lf);
262 if (error || lf == NULL) {
270 /* lookup again, see if the VFS was loaded */
271 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
272 if (!strcmp(vfsp->vfc_name, fstypename))
277 linker_file_unload(lf);
283 lwkt_gettoken(&vlock, vp->v_interlock);
284 if ((vp->v_flag & VMOUNT) != 0 ||
285 vp->v_mountedhere != NULL) {
286 lwkt_reltoken(&vlock);
291 vp->v_flag |= VMOUNT;
292 lwkt_reltoken(&vlock);
295 * Allocate and initialize the filesystem.
297 mp = malloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
298 TAILQ_INIT(&mp->mnt_nvnodelist);
299 TAILQ_INIT(&mp->mnt_reservedvnlist);
300 mp->mnt_nvnodelistsize = 0;
301 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
302 vfs_busy(mp, LK_NOWAIT, NULL, td);
303 mp->mnt_op = vfsp->vfc_vfsops;
305 vfsp->vfc_refcount++;
306 mp->mnt_stat.f_type = vfsp->vfc_typenum;
307 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
308 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
309 mp->mnt_vnodecovered = vp;
310 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
311 mp->mnt_iosize_max = DFLTPHYS;
312 VOP_UNLOCK(vp, NULL, 0, td);
315 * Set the mount level flags.
317 if (SCARG(uap, flags) & MNT_RDONLY)
318 mp->mnt_flag |= MNT_RDONLY;
319 else if (mp->mnt_flag & MNT_RDONLY)
320 mp->mnt_kern_flag |= MNTK_WANTRDWR;
321 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
322 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
323 MNT_NOSYMFOLLOW | MNT_IGNORE |
324 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
325 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
326 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
327 MNT_NOSYMFOLLOW | MNT_IGNORE |
328 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
330 * Mount the filesystem.
331 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
332 * get. No freeing of cn_pnbuf.
334 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), td);
335 if (mp->mnt_flag & MNT_UPDATE) {
336 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
337 mp->mnt_flag &= ~MNT_RDONLY;
338 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
339 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
342 mp->mnt_kern_flag = flag2;
345 lwkt_gettoken(&vlock, vp->v_interlock);
346 vp->v_flag &= ~VMOUNT;
347 lwkt_reltoken(&vlock);
352 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
354 * Put the new filesystem on the mount list after root. The mount
355 * point gets its own mnt_ncp which is a special ncp linking the
356 * vnode-under to the root of the new mount. The lookup code
357 * detects the mount point going forward and detects the special
358 * mnt_ncp via NCP_MOUNTPT going backwards.
362 nlc.nlc_nameptr = "";
364 mp->mnt_ncp = cache_nlookup(ncp, &nlc);
365 mp->mnt_ncp->nc_flag |= NCF_MOUNTPT;
366 mp->mnt_ncp->nc_mount = mp;
368 /* XXX get the root of the fs and cache_setvp(mnt_ncp...) */
369 lwkt_gettoken(&vlock, vp->v_interlock);
370 vp->v_flag &= ~VMOUNT;
371 vp->v_mountedhere = mp;
372 lwkt_reltoken(&vlock);
373 lwkt_gettoken(&ilock, &mountlist_token);
374 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
375 lwkt_reltoken(&ilock);
376 checkdirs(vp, mp->mnt_ncp);
377 cache_unlock(mp->mnt_ncp); /* leave ref intact */
378 VOP_UNLOCK(vp, NULL, 0, td);
379 error = vfs_allocate_syncvnode(mp);
381 if ((error = VFS_START(mp, 0, td)) != 0)
384 vfs_rm_vnodeops(&mp->mnt_vn_ops);
385 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
386 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
387 lwkt_gettoken(&vlock, vp->v_interlock);
388 vp->v_flag &= ~VMOUNT;
389 lwkt_reltoken(&vlock);
390 mp->mnt_vfc->vfc_refcount--;
400 * Scan all active processes to see if any of them have a current
401 * or root directory onto which the new filesystem has just been
402 * mounted. If so, replace them with the new mount point.
404 * The passed ncp is ref'd and locked (from the mount code) and
405 * must be associated with the vnode representing the root of the
409 checkdirs(struct vnode *olddp, struct namecache *ncp)
411 struct filedesc *fdp;
416 if (olddp->v_usecount == 1)
418 mp = olddp->v_mountedhere;
419 if (VFS_ROOT(mp, &newdp))
420 panic("mount: lost mount");
421 cache_setvp(ncp, newdp);
423 if (rootvnode == olddp) {
425 vfs_cache_setroot(newdp, cache_hold(ncp));
428 FOREACH_PROC_IN_SYSTEM(p) {
430 if (fdp->fd_cdir == olddp) {
433 fdp->fd_cdir = newdp;
434 cache_drop(fdp->fd_ncdir);
435 fdp->fd_ncdir = cache_hold(ncp);
437 if (fdp->fd_rdir == olddp) {
440 fdp->fd_rdir = newdp;
441 cache_drop(fdp->fd_nrdir);
442 fdp->fd_nrdir = cache_hold(ncp);
449 * Unmount a file system.
451 * Note: unmount takes a path to the vnode mounted on as argument,
452 * not special file (as before).
455 * umount_args(char *path, int flags)
459 unmount(struct unmount_args *uap)
461 struct thread *td = curthread;
462 struct proc *p = td->td_proc;
469 if (p->p_ucred->cr_prison != NULL)
471 if (usermount == 0 && (error = suser(td)))
474 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
475 SCARG(uap, path), td);
476 if ((error = namei(&nd)) != 0)
479 NDFREE(&nd, NDF_ONLY_PNBUF);
483 * Only root, or the user that did the original mount is
484 * permitted to unmount this filesystem.
486 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
487 (error = suser(td))) {
493 * Don't allow unmounting the root file system.
495 if (mp->mnt_flag & MNT_ROOTFS) {
501 * Must be the root of the filesystem
503 if ((vp->v_flag & VROOT) == 0) {
508 return (dounmount(mp, SCARG(uap, flags), td));
512 * Do the actual file system unmount.
515 dounmount(struct mount *mp, int flags, struct thread *td)
517 struct vnode *coveredvp;
522 lwkt_gettoken(&ilock, &mountlist_token);
523 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
524 lwkt_reltoken(&ilock);
527 mp->mnt_kern_flag |= MNTK_UNMOUNT;
528 /* Allow filesystems to detect that a forced unmount is in progress. */
529 if (flags & MNT_FORCE)
530 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
531 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
532 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
534 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
535 if (mp->mnt_kern_flag & MNTK_MWAIT)
540 if (mp->mnt_flag & MNT_EXPUBLIC)
541 vfs_setpublicfs(NULL, NULL, NULL);
543 vfs_msync(mp, MNT_WAIT);
544 async_flag = mp->mnt_flag & MNT_ASYNC;
545 mp->mnt_flag &=~ MNT_ASYNC;
546 cache_purgevfs(mp); /* remove cache entries for this file sys */
547 if (mp->mnt_syncer != NULL)
548 vrele(mp->mnt_syncer);
549 if (((mp->mnt_flag & MNT_RDONLY) ||
550 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
552 error = VFS_UNMOUNT(mp, flags, td);
553 lwkt_gettokref(&ilock);
555 if (mp->mnt_syncer == NULL)
556 vfs_allocate_syncvnode(mp);
557 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
558 mp->mnt_flag |= async_flag;
559 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
561 if (mp->mnt_kern_flag & MNTK_MWAIT)
565 TAILQ_REMOVE(&mountlist, mp, mnt_list);
568 * Remove any installed vnode ops here so the individual VFSs don't
571 vfs_rm_vnodeops(&mp->mnt_vn_ops);
572 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
573 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
575 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
576 coveredvp->v_mountedhere = NULL;
578 cache_drop(mp->mnt_ncp);
581 mp->mnt_vfc->vfc_refcount--;
582 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
583 panic("unmount: dangling vnode");
584 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
585 if (mp->mnt_kern_flag & MNTK_MWAIT)
592 * Sync each mounted filesystem.
596 static int syncprt = 0;
597 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
602 sync(struct sync_args *uap)
604 struct thread *td = curthread;
605 struct mount *mp, *nmp;
609 lwkt_gettoken(&ilock, &mountlist_token);
610 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
611 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
612 nmp = TAILQ_NEXT(mp, mnt_list);
615 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
616 asyncflag = mp->mnt_flag & MNT_ASYNC;
617 mp->mnt_flag &= ~MNT_ASYNC;
618 vfs_msync(mp, MNT_NOWAIT);
619 VFS_SYNC(mp, MNT_NOWAIT, td);
620 mp->mnt_flag |= asyncflag;
622 lwkt_gettokref(&ilock);
623 nmp = TAILQ_NEXT(mp, mnt_list);
626 lwkt_reltoken(&ilock);
628 * print out buffer pool stat information on each sync() call.
637 /* XXX PRISON: could be per prison flag */
638 static int prison_quotas;
640 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
644 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
646 * Change filesystem quotas.
650 quotactl(struct quotactl_args *uap)
652 struct thread *td = curthread;
653 struct proc *p = td->td_proc;
659 if (p->p_ucred->cr_prison && !prison_quotas)
661 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
662 SCARG(uap, path), td);
663 if ((error = namei(&nd)) != 0)
665 mp = nd.ni_vp->v_mount;
666 NDFREE(&nd, NDF_ONLY_PNBUF);
668 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
669 SCARG(uap, arg), td));
673 kern_statfs(struct nameidata *nd, struct statfs *buf)
675 struct thread *td = curthread;
683 mp = nd->ni_vp->v_mount;
685 NDFREE(nd, NDF_ONLY_PNBUF);
687 error = VFS_STATFS(mp, sp, td);
690 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
691 bcopy(sp, buf, sizeof(*buf));
692 /* Only root should have access to the fsid's. */
694 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
699 * statfs_args(char *path, struct statfs *buf)
701 * Get filesystem statistics.
704 statfs(struct statfs_args *uap)
706 struct thread *td = curthread;
711 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
713 error = kern_statfs(&nd, &buf);
716 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
721 kern_fstatfs(int fd, struct statfs *buf)
723 struct thread *td = curthread;
724 struct proc *p = td->td_proc;
731 error = getvnode(p->p_fd, fd, &fp);
734 mp = ((struct vnode *)fp->f_data)->v_mount;
738 error = VFS_STATFS(mp, sp, td);
741 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
742 bcopy(sp, buf, sizeof(*buf));
743 /* Only root should have access to the fsid's. */
745 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
750 * fstatfs_args(int fd, struct statfs *buf)
752 * Get filesystem statistics.
755 fstatfs(struct fstatfs_args *uap)
760 error = kern_fstatfs(uap->fd, &buf);
763 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
768 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
770 * Get statistics on all filesystems.
774 getfsstat(struct getfsstat_args *uap)
776 struct thread *td = curthread;
777 struct mount *mp, *nmp;
781 long count, maxcount, error;
783 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
784 sfsp = (caddr_t)SCARG(uap, buf);
786 lwkt_gettoken(&ilock, &mountlist_token);
787 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
788 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
789 nmp = TAILQ_NEXT(mp, mnt_list);
792 if (sfsp && count < maxcount) {
795 * If MNT_NOWAIT or MNT_LAZY is specified, do not
796 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
797 * overrides MNT_WAIT.
799 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
800 (SCARG(uap, flags) & MNT_WAIT)) &&
801 (error = VFS_STATFS(mp, sp, td))) {
802 lwkt_gettokref(&ilock);
803 nmp = TAILQ_NEXT(mp, mnt_list);
807 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
808 error = copyout(sp, sfsp, sizeof(*sp));
816 lwkt_gettokref(&ilock);
817 nmp = TAILQ_NEXT(mp, mnt_list);
820 lwkt_reltoken(&ilock);
821 if (sfsp && count > maxcount)
822 uap->sysmsg_result = maxcount;
824 uap->sysmsg_result = count;
829 * fchdir_args(int fd)
831 * Change current working directory to a given file descriptor.
835 fchdir(struct fchdir_args *uap)
837 struct thread *td = curthread;
838 struct proc *p = td->td_proc;
839 struct filedesc *fdp = p->p_fd;
840 struct vnode *vp, *ovp;
843 struct namecache *ncp, *oncp;
844 struct namecache *nct;
847 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
849 vp = (struct vnode *)fp->f_data;
851 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
852 if (vp->v_type != VDIR || fp->f_ncp == NULL)
855 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
860 ncp = cache_hold(fp->f_ncp);
861 while (!error && (mp = vp->v_mountedhere) != NULL) {
862 error = nlookup_mp(mp, &nct);
864 cache_unlock(nct); /* leave ref intact */
867 error = vget(vp, NULL, LK_SHARED, td);
868 KKASSERT(error == 0);
875 oncp = fdp->fd_ncdir;
876 VOP_UNLOCK(vp, NULL, 0, td); /* leave ref intact */
889 kern_chdir(struct nlookupdata *nd)
891 struct thread *td = curthread;
892 struct proc *p = td->td_proc;
893 struct filedesc *fdp = p->p_fd;
894 struct vnode *vp, *ovp;
895 struct namecache *oncp;
898 if ((error = nlookup(nd)) != 0)
900 if ((vp = nd->nl_ncp->nc_vp) == NULL)
902 if ((error = vget(vp, NULL, LK_SHARED, td)) != 0)
905 error = checkvp_chdir(vp, td);
906 VOP_UNLOCK(vp, NULL, 0, td);
909 oncp = fdp->fd_ncdir;
910 cache_unlock(nd->nl_ncp); /* leave reference intact */
911 fdp->fd_ncdir = nd->nl_ncp;
923 * chdir_args(char *path)
925 * Change current working directory (``.'').
928 chdir(struct chdir_args *uap)
930 struct nlookupdata nd;
933 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
935 error = kern_chdir(&nd);
942 * Helper function for raised chroot(2) security function: Refuse if
943 * any filedescriptors are open directories.
946 chroot_refuse_vdir_fds(fdp)
947 struct filedesc *fdp;
954 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
955 error = getvnode(fdp, fd, &fp);
958 vp = (struct vnode *)fp->f_data;
959 if (vp->v_type != VDIR)
967 * This sysctl determines if we will allow a process to chroot(2) if it
968 * has a directory open:
969 * 0: disallowed for all processes.
970 * 1: allowed for processes that were not already chroot(2)'ed.
971 * 2: allowed for all processes.
974 static int chroot_allow_open_directories = 1;
976 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
977 &chroot_allow_open_directories, 0, "");
980 * chroot to the specified namecache entry. We obtain the vp from the
981 * namecache data. The passed ncp must be locked and referenced and will
982 * remain locked and referenced on return.
985 kern_chroot(struct namecache *ncp)
987 struct thread *td = curthread;
988 struct proc *p = td->td_proc;
989 struct filedesc *fdp = p->p_fd;
994 * Only root can chroot
996 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1000 * Disallow open directory descriptors (fchdir() breakouts).
1002 if (chroot_allow_open_directories == 0 ||
1003 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1004 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1007 if ((vp = ncp->nc_vp) == NULL)
1010 if ((error = vget(vp, NULL, LK_SHARED, td)) != 0)
1014 * Check the validity of vp as a directory to change to and
1015 * associate it with rdir/jdir.
1017 error = checkvp_chdir(vp, td);
1018 VOP_UNLOCK(vp, NULL, 0, td); /* leave reference intact */
1020 vrele(fdp->fd_rdir);
1021 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1022 cache_drop(fdp->fd_nrdir);
1023 fdp->fd_nrdir = cache_hold(ncp);
1024 if (fdp->fd_jdir == NULL) {
1027 fdp->fd_njdir = cache_hold(ncp);
1036 * chroot_args(char *path)
1038 * Change notion of root (``/'') directory.
1042 chroot(struct chroot_args *uap)
1044 struct thread *td = curthread;
1045 struct nlookupdata nd;
1048 KKASSERT(td->td_proc);
1049 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1051 error = kern_chroot(nd.nl_ncp);
1058 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1059 * determine whether it is legal to chdir to the vnode. The vnode's state
1060 * is not changed by this call.
1063 checkvp_chdir(struct vnode *vp, struct thread *td)
1067 if (vp->v_type != VDIR)
1070 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
1075 kern_open(struct nameidata *nd, int oflags, int mode, int *res)
1077 struct thread *td = curthread;
1078 struct proc *p = td->td_proc;
1079 struct filedesc *fdp = p->p_fd;
1084 int type, indx, error;
1087 struct nlookupdata ndx;
1089 if ((oflags & O_ACCMODE) == O_ACCMODE)
1091 flags = FFLAGS(oflags);
1092 error = falloc(p, &nfp, &indx);
1096 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1097 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
1099 * Bump the ref count to prevent another process from closing
1100 * the descriptor while we are blocked in vn_open()
1105 * XXX temporary hack so we can record the namecache pointer
1106 * associated with an open descriptor.
1108 ndxerror = nlookup_init(&ndx, nd->ni_dirp, nd->ni_segflg,
1109 ((nd->ni_cnd.cn_flags & CNP_FOLLOW) ? NLC_FOLLOW : 0));
1111 error = vn_open(nd, flags, cmode);
1114 * release our own reference
1119 * handle special fdopen() case. bleh. dupfdopen() is
1120 * responsible for dropping the old contents of ofiles[indx]
1123 if ((error == ENODEV || error == ENXIO) &&
1124 p->p_dupfd >= 0 && /* XXX from fdopen */
1126 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
1132 * Clean up the descriptor, but only if another thread hadn't
1133 * replaced or closed it.
1135 if (fdp->fd_ofiles[indx] == fp) {
1136 fdp->fd_ofiles[indx] = NULL;
1140 if (error == ERESTART)
1146 NDFREE(nd, NDF_ONLY_PNBUF);
1150 * There should be 2 references on the file, one from the descriptor
1151 * table, and one for us.
1153 * Handle the case where someone closed the file (via its file
1154 * descriptor) while we were blocked. The end result should look
1155 * like opening the file succeeded but it was immediately closed.
1157 if (fp->f_count == 1) {
1158 KASSERT(fdp->fd_ofiles[indx] != fp,
1159 ("Open file descriptor lost all refs"));
1160 VOP_UNLOCK(vp, NULL, 0, td);
1161 vn_close(vp, flags & FMASK, td);
1168 fp->f_data = (caddr_t)vp;
1169 fp->f_flag = flags & FMASK;
1171 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1172 if (flags & (O_EXLOCK | O_SHLOCK)) {
1173 lf.l_whence = SEEK_SET;
1176 if (flags & O_EXLOCK)
1177 lf.l_type = F_WRLCK;
1179 lf.l_type = F_RDLCK;
1181 if ((flags & FNONBLOCK) == 0)
1183 VOP_UNLOCK(vp, NULL, 0, td);
1184 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1186 * lock request failed. Normally close the descriptor
1187 * but handle the case where someone might have dup()d
1188 * it when we weren't looking. One reference is
1189 * owned by the descriptor array, the other by us.
1191 if (fdp->fd_ofiles[indx] == fp) {
1192 fdp->fd_ofiles[indx] = NULL;
1199 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1200 fp->f_flag |= FHASLOCK;
1202 /* assert that vn_open created a backing object if one is needed */
1203 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1204 ("open: vmio vnode has no backing object after vn_open"));
1205 VOP_UNLOCK(vp, NULL, 0, td);
1208 * If the vp is a directory locate the ncp to store with the file
1209 * descriptor. XXX temporary. We may eventually wish to do this
1210 * permanently as it would provide an invaluable diagnostic tool,
1211 * but first the entire open path needs to be converted from
1212 * namei to nlookup so we can avoid having to do a double-lookup.
1214 * The primary purpose of storing the ncp with the file pointer is
1215 * so it can be used in fchdir() and fchroot() syscalls, allowing
1216 * us to retain an unbroken namecache topology.
1218 if (ndxerror == 0 && vp->v_type == VDIR) {
1219 if ((ndxerror = nlookup(&ndx)) == 0) {
1220 fp->f_ncp = ndx.nl_ncp;
1222 cache_unlock(fp->f_ncp);
1228 * release our private reference, leaving the one associated with the
1229 * descriptor table intact.
1237 * open_args(char *path, int flags, int mode)
1239 * Check permissions, allocate an open file structure,
1240 * and call the device open routine if any.
1243 open(struct open_args *uap)
1245 struct thread *td = curthread;
1246 struct nameidata nd;
1249 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1251 error = kern_open(&nd, uap->flags, uap->mode, &uap->sysmsg_result);
1257 kern_mknod(struct nameidata *nd, int mode, int dev)
1259 struct thread *td = curthread;
1260 struct proc *p = td->td_proc;
1268 switch (mode & S_IFMT) {
1274 error = suser_cred(p->p_ucred, PRISON_ROOT);
1288 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1289 vattr.va_rdev = dev;
1292 switch (mode & S_IFMT) {
1293 case S_IFMT: /* used by badsect to flag bad sectors */
1294 vattr.va_type = VBAD;
1297 vattr.va_type = VCHR;
1300 vattr.va_type = VBLK;
1311 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1313 error = VOP_WHITEOUT(nd->ni_dvp, NCPNULL,
1314 &nd->ni_cnd, NAMEI_CREATE);
1316 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp,
1317 &nd->ni_cnd, &vattr);
1321 NDFREE(nd, NDF_ONLY_PNBUF);
1324 NDFREE(nd, NDF_ONLY_PNBUF);
1325 if (nd->ni_dvp == vp)
1332 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mknod");
1333 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mknod");
1338 * mknod_args(char *path, int mode, int dev)
1340 * Create a special file.
1343 mknod(struct mknod_args *uap)
1345 struct thread *td = curthread;
1346 struct nameidata nd;
1349 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1352 error = kern_mknod(&nd, uap->mode, uap->dev);
1358 kern_mkfifo(struct nameidata *nd, int mode)
1360 struct thread *td = curthread;
1361 struct proc *p = td->td_proc;
1369 if (nd->ni_vp != NULL) {
1370 NDFREE(nd, NDF_ONLY_PNBUF);
1371 if (nd->ni_dvp == nd->ni_vp)
1379 vattr.va_type = VFIFO;
1380 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1381 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1382 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd, &vattr);
1385 NDFREE(nd, NDF_ONLY_PNBUF);
1391 * mkfifo_args(char *path, int mode)
1393 * Create a named pipe.
1396 mkfifo(struct mkfifo_args *uap)
1398 struct thread *td = curthread;
1399 struct nameidata nd;
1402 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1405 error = kern_mkfifo(&nd, uap->mode);
1411 kern_link(struct nameidata *nd, struct nameidata *linknd)
1413 struct thread *td = curthread;
1414 struct proc *p = td->td_proc;
1422 NDFREE(nd, NDF_ONLY_PNBUF);
1424 if (vp->v_type == VDIR)
1425 error = EPERM; /* POSIX */
1427 error = namei(linknd);
1429 if (linknd->ni_vp != NULL) {
1431 vrele(linknd->ni_vp);
1434 VOP_LEASE(linknd->ni_dvp, td, p->p_ucred,
1436 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1437 error = VOP_LINK(linknd->ni_dvp, NCPNULL, vp,
1440 NDFREE(linknd, NDF_ONLY_PNBUF);
1441 if (linknd->ni_dvp == linknd->ni_vp)
1442 vrele(linknd->ni_dvp);
1444 vput(linknd->ni_dvp);
1445 ASSERT_VOP_UNLOCKED(linknd->ni_dvp, "link");
1446 ASSERT_VOP_UNLOCKED(linknd->ni_vp, "link");
1454 * link_args(char *path, char *link)
1456 * Make a hard file link.
1459 link(struct link_args *uap)
1461 struct thread *td = curthread;
1462 struct nameidata nd, linknd;
1465 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1467 NDINIT(&linknd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1468 UIO_USERSPACE, uap->link, td);
1470 error = kern_link(&nd, &linknd);
1476 kern_symlink(char *path, struct nameidata *nd)
1478 struct thread *td = curthread;
1479 struct proc *p = td->td_proc;
1488 NDFREE(nd, NDF_ONLY_PNBUF);
1489 if (nd->ni_dvp == nd->ni_vp)
1497 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1498 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1499 error = VOP_SYMLINK(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
1501 NDFREE(nd, NDF_ONLY_PNBUF);
1505 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "symlink");
1506 ASSERT_VOP_UNLOCKED(nd->ni_vp, "symlink");
1512 * symlink(char *path, char *link)
1514 * Make a symbolic link.
1517 symlink(struct symlink_args *uap)
1519 struct thread *td = curthread;
1520 struct nameidata nd;
1524 path = zalloc(namei_zone);
1525 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1527 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1528 UIO_USERSPACE, uap->link, td);
1529 error = kern_symlink(path, &nd);
1531 zfree(namei_zone, path);
1536 * undelete_args(char *path)
1538 * Delete a whiteout from the filesystem.
1542 undelete(struct undelete_args *uap)
1544 struct thread *td = curthread;
1545 struct proc *p = td->td_proc;
1547 struct nameidata nd;
1550 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
1551 SCARG(uap, path), td);
1556 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
1557 NDFREE(&nd, NDF_ONLY_PNBUF);
1558 if (nd.ni_dvp == nd.ni_vp)
1567 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1568 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
1569 NDFREE(&nd, NDF_ONLY_PNBUF);
1571 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1572 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1577 kern_unlink(struct nameidata *nd)
1579 struct thread *td = curthread;
1580 struct proc *p = td->td_proc;
1589 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1590 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1592 if (vp->v_type == VDIR)
1593 error = EPERM; /* POSIX */
1596 * The root of a mounted filesystem cannot be deleted.
1598 * XXX: can this only be a VDIR case?
1600 if (vp->v_flag & VROOT)
1605 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1606 error = VOP_REMOVE(nd->ni_dvp, NCPNULL, vp, &nd->ni_cnd);
1608 NDFREE(nd, NDF_ONLY_PNBUF);
1609 if (nd->ni_dvp == vp)
1615 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "unlink");
1616 ASSERT_VOP_UNLOCKED(nd->ni_vp, "unlink");
1621 * unlink_args(char *path)
1623 * Delete a name from the filesystem.
1626 unlink(struct unlink_args *uap)
1628 struct thread *td = curthread;
1629 struct nameidata nd;
1632 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1635 error = kern_unlink(&nd);
1641 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1643 struct thread *td = curthread;
1644 struct proc *p = td->td_proc;
1645 struct filedesc *fdp = p->p_fd;
1650 if (fd >= fdp->fd_nfiles ||
1651 (fp = fdp->fd_ofiles[fd]) == NULL)
1653 if (fp->f_type != DTYPE_VNODE)
1657 fp->f_offset += offset;
1660 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1663 fp->f_offset = offset + vattr.va_size;
1666 fp->f_offset = offset;
1671 *res = fp->f_offset;
1676 * lseek_args(int fd, int pad, off_t offset, int whence)
1678 * Reposition read/write file offset.
1681 lseek(struct lseek_args *uap)
1685 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1686 &uap->sysmsg_offset);
1692 kern_access(struct nameidata *nd, int aflags)
1694 struct thread *td = curthread;
1695 struct proc *p = td->td_proc;
1696 struct ucred *cred, *tmpcred;
1702 * Create and modify a temporary credential instead of one that
1703 * is potentially shared. This could also mess up socket
1704 * buffer accounting which can run in an interrupt context.
1706 tmpcred = crdup(cred);
1707 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1708 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1709 p->p_ucred = tmpcred;
1710 nd->ni_cnd.cn_cred = tmpcred;
1716 /* Flags == 0 means only check for existence. */
1725 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1726 error = VOP_ACCESS(vp, flags, tmpcred, td);
1728 NDFREE(nd, NDF_ONLY_PNBUF);
1737 * access_args(char *path, int flags)
1739 * Check access permissions.
1742 access(struct access_args *uap)
1744 struct thread *td = curthread;
1745 struct nameidata nd;
1748 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1749 UIO_USERSPACE, uap->path, td);
1751 error = kern_access(&nd, uap->flags);
1757 kern_stat(struct nlookupdata *nd, struct stat *st)
1763 if ((error = nlookup(nd)) != 0)
1765 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1769 if ((error = vget(vp, NULL, LK_SHARED, td)) != 0)
1771 error = vn_stat(vp, st, td);
1777 * stat_args(char *path, struct stat *ub)
1779 * Get file status; this version follows links.
1782 stat(struct stat_args *uap)
1784 struct nlookupdata nd;
1788 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1790 error = kern_stat(&nd, &st);
1792 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1799 * lstat_args(char *path, struct stat *ub)
1801 * Get file status; this version does not follow links.
1804 lstat(struct lstat_args *uap)
1806 struct nlookupdata nd;
1810 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1812 error = kern_stat(&nd, &st);
1814 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1825 nsb->st_dev = sb->st_dev;
1826 nsb->st_ino = sb->st_ino;
1827 nsb->st_mode = sb->st_mode;
1828 nsb->st_nlink = sb->st_nlink;
1829 nsb->st_uid = sb->st_uid;
1830 nsb->st_gid = sb->st_gid;
1831 nsb->st_rdev = sb->st_rdev;
1832 nsb->st_atimespec = sb->st_atimespec;
1833 nsb->st_mtimespec = sb->st_mtimespec;
1834 nsb->st_ctimespec = sb->st_ctimespec;
1835 nsb->st_size = sb->st_size;
1836 nsb->st_blocks = sb->st_blocks;
1837 nsb->st_blksize = sb->st_blksize;
1838 nsb->st_flags = sb->st_flags;
1839 nsb->st_gen = sb->st_gen;
1840 nsb->st_qspare[0] = sb->st_qspare[0];
1841 nsb->st_qspare[1] = sb->st_qspare[1];
1845 * nstat_args(char *path, struct nstat *ub)
1849 nstat(struct nstat_args *uap)
1851 struct thread *td = curthread;
1855 struct nameidata nd;
1857 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1858 UIO_USERSPACE, SCARG(uap, path), td);
1859 if ((error = namei(&nd)) != 0)
1861 NDFREE(&nd, NDF_ONLY_PNBUF);
1862 error = vn_stat(nd.ni_vp, &sb, td);
1866 cvtnstat(&sb, &nsb);
1867 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1872 * lstat_args(char *path, struct stat *ub)
1874 * Get file status; this version does not follow links.
1878 nlstat(struct nlstat_args *uap)
1880 struct thread *td = curthread;
1885 struct nameidata nd;
1887 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1888 UIO_USERSPACE, SCARG(uap, path), td);
1889 if ((error = namei(&nd)) != 0)
1892 NDFREE(&nd, NDF_ONLY_PNBUF);
1893 error = vn_stat(vp, &sb, td);
1897 cvtnstat(&sb, &nsb);
1898 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1903 * pathconf_Args(char *path, int name)
1905 * Get configurable pathname variables.
1909 pathconf(struct pathconf_args *uap)
1911 struct thread *td = curthread;
1913 struct nameidata nd;
1915 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1916 UIO_USERSPACE, SCARG(uap, path), td);
1917 if ((error = namei(&nd)) != 0)
1919 NDFREE(&nd, NDF_ONLY_PNBUF);
1920 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1927 * kern_readlink isn't properly split yet. There is a copyin burried
1928 * in VOP_READLINK().
1931 kern_readlink(struct nameidata *nd, char *buf, int count, int *res)
1933 struct thread *td = curthread;
1934 struct proc *p = td->td_proc;
1943 NDFREE(nd, NDF_ONLY_PNBUF);
1945 if (vp->v_type != VLNK)
1948 aiov.iov_base = buf;
1949 aiov.iov_len = count;
1950 auio.uio_iov = &aiov;
1951 auio.uio_iovcnt = 1;
1952 auio.uio_offset = 0;
1953 auio.uio_rw = UIO_READ;
1954 auio.uio_segflg = UIO_USERSPACE;
1956 auio.uio_resid = count;
1957 error = VOP_READLINK(vp, &auio, p->p_ucred);
1960 *res = count - auio.uio_resid;
1965 * readlink_args(char *path, char *buf, int count)
1967 * Return target name of a symbolic link.
1970 readlink(struct readlink_args *uap)
1972 struct thread *td = curthread;
1973 struct nameidata nd;
1976 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ, UIO_USERSPACE,
1979 error = kern_readlink(&nd, uap->buf, uap->count,
1980 &uap->sysmsg_result);
1986 setfflags(struct vnode *vp, int flags)
1988 struct thread *td = curthread;
1989 struct proc *p = td->td_proc;
1994 * Prevent non-root users from setting flags on devices. When
1995 * a device is reused, users can retain ownership of the device
1996 * if they are allowed to set flags and programs assume that
1997 * chown can't fail when done as root.
1999 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2000 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2003 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2004 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2006 vattr.va_flags = flags;
2007 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2008 VOP_UNLOCK(vp, NULL, 0, td);
2013 * chflags(char *path, int flags)
2015 * Change flags of a file given a path name.
2019 chflags(struct chflags_args *uap)
2021 struct thread *td = curthread;
2023 struct nameidata nd;
2025 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
2026 SCARG(uap, path), td);
2027 if ((error = namei(&nd)) != 0)
2029 NDFREE(&nd, NDF_ONLY_PNBUF);
2030 error = setfflags(nd.ni_vp, SCARG(uap, flags));
2036 * fchflags_args(int fd, int flags)
2038 * Change flags of a file given a file descriptor.
2042 fchflags(struct fchflags_args *uap)
2044 struct thread *td = curthread;
2045 struct proc *p = td->td_proc;
2049 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2051 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
2055 setfmode(struct vnode *vp, int mode)
2057 struct thread *td = curthread;
2058 struct proc *p = td->td_proc;
2062 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2063 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2065 vattr.va_mode = mode & ALLPERMS;
2066 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2067 VOP_UNLOCK(vp, NULL, 0, td);
2072 kern_chmod(struct nameidata *nd, int mode)
2079 NDFREE(nd, NDF_ONLY_PNBUF);
2080 error = setfmode(nd->ni_vp, mode);
2086 * chmod_args(char *path, int mode)
2088 * Change mode of a file given path name.
2092 chmod(struct chmod_args *uap)
2094 struct thread *td = curthread;
2095 struct nameidata nd;
2098 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2100 error = kern_chmod(&nd, uap->mode);
2106 * lchmod_args(char *path, int mode)
2108 * Change mode of a file given path name (don't follow links.)
2112 lchmod(struct lchmod_args *uap)
2114 struct thread *td = curthread;
2116 struct nameidata nd;
2118 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
2119 if ((error = namei(&nd)) != 0)
2121 NDFREE(&nd, NDF_ONLY_PNBUF);
2122 error = setfmode(nd.ni_vp, SCARG(uap, mode));
2128 * fchmod_args(int fd, int mode)
2130 * Change mode of a file given a file descriptor.
2134 fchmod(struct fchmod_args *uap)
2136 struct thread *td = curthread;
2137 struct proc *p = td->td_proc;
2141 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2143 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
2147 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2149 struct thread *td = curthread;
2150 struct proc *p = td->td_proc;
2154 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2155 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2159 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2160 VOP_UNLOCK(vp, NULL, 0, td);
2165 kern_chown(struct nameidata *nd, int uid, int gid)
2172 NDFREE(nd, NDF_ONLY_PNBUF);
2173 error = setfown(nd->ni_vp, uid, gid);
2179 * chown(char *path, int uid, int gid)
2181 * Set ownership given a path name.
2184 chown(struct chown_args *uap)
2186 struct thread *td = curthread;
2187 struct nameidata nd;
2190 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2192 error = kern_chown(&nd, uap->uid, uap->gid);
2198 * lchown_args(char *path, int uid, int gid)
2200 * Set ownership given a path name, do not cross symlinks.
2203 lchown(struct lchown_args *uap)
2205 struct thread *td = curthread;
2207 struct nameidata nd;
2209 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2211 error = kern_chown(&nd, uap->uid, uap->gid);
2217 * fchown_args(int fd, int uid, int gid)
2219 * Set ownership given a file descriptor.
2223 fchown(struct fchown_args *uap)
2225 struct thread *td = curthread;
2226 struct proc *p = td->td_proc;
2230 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2232 return setfown((struct vnode *)fp->f_data,
2233 SCARG(uap, uid), SCARG(uap, gid));
2237 getutimes(const struct timeval *tvp, struct timespec *tsp)
2239 struct timeval tv[2];
2243 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2246 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2247 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2253 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2255 struct thread *td = curthread;
2256 struct proc *p = td->td_proc;
2260 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2261 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2263 vattr.va_atime = ts[0];
2264 vattr.va_mtime = ts[1];
2266 vattr.va_vaflags |= VA_UTIMES_NULL;
2267 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2268 VOP_UNLOCK(vp, NULL, 0, td);
2273 kern_utimes(struct nameidata *nd, struct timeval *tptr)
2275 struct timespec ts[2];
2278 error = getutimes(tptr, ts);
2284 NDFREE(nd, NDF_ONLY_PNBUF);
2285 error = setutimes(nd->ni_vp, ts, tptr == NULL);
2291 * utimes_args(char *path, struct timeval *tptr)
2293 * Set the access and modification times of a file.
2296 utimes(struct utimes_args *uap)
2298 struct thread *td = curthread;
2299 struct timeval tv[2];
2300 struct nameidata nd;
2304 error = copyin(uap->tptr, tv, sizeof(tv));
2308 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2310 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2316 * lutimes_args(char *path, struct timeval *tptr)
2318 * Set the access and modification times of a file.
2321 lutimes(struct lutimes_args *uap)
2323 struct thread *td = curthread;
2324 struct timeval tv[2];
2325 struct nameidata nd;
2329 error = copyin(uap->tptr, tv, sizeof(tv));
2333 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2335 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2341 kern_futimes(int fd, struct timeval *tptr)
2343 struct thread *td = curthread;
2344 struct proc *p = td->td_proc;
2345 struct timespec ts[2];
2349 error = getutimes(tptr, ts);
2352 error = getvnode(p->p_fd, fd, &fp);
2355 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2360 * futimes_args(int fd, struct timeval *tptr)
2362 * Set the access and modification times of a file.
2365 futimes(struct futimes_args *uap)
2367 struct timeval tv[2];
2371 error = copyin(uap->tptr, tv, sizeof(tv));
2376 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2382 kern_truncate(struct nameidata* nd, off_t length)
2384 struct thread *td = curthread;
2385 struct proc *p = td->td_proc;
2392 if ((error = namei(nd)) != 0)
2395 NDFREE(nd, NDF_ONLY_PNBUF);
2396 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2397 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2398 if (vp->v_type == VDIR)
2400 else if ((error = vn_writechk(vp)) == 0 &&
2401 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2403 vattr.va_size = length;
2404 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2411 * truncate(char *path, int pad, off_t length)
2413 * Truncate a file given its path name.
2416 truncate(struct truncate_args *uap)
2418 struct thread *td = curthread;
2419 struct nameidata nd;
2422 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2424 error = kern_truncate(&nd, uap->length);
2430 kern_ftruncate(int fd, off_t length)
2432 struct thread *td = curthread;
2433 struct proc *p = td->td_proc;
2441 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2443 if ((fp->f_flag & FWRITE) == 0)
2445 vp = (struct vnode *)fp->f_data;
2446 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2447 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2448 if (vp->v_type == VDIR)
2450 else if ((error = vn_writechk(vp)) == 0) {
2452 vattr.va_size = length;
2453 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2455 VOP_UNLOCK(vp, NULL, 0, td);
2460 * ftruncate_args(int fd, int pad, off_t length)
2462 * Truncate a file given a file descriptor.
2465 ftruncate(struct ftruncate_args *uap)
2469 error = kern_ftruncate(uap->fd, uap->length);
2477 * Sync an open file.
2481 fsync(struct fsync_args *uap)
2483 struct thread *td = curthread;
2484 struct proc *p = td->td_proc;
2490 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2492 vp = (struct vnode *)fp->f_data;
2493 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2494 if (VOP_GETVOBJECT(vp, &obj) == 0)
2495 vm_object_page_clean(obj, 0, 0, 0);
2496 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2497 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2499 error = (*bioops.io_fsync)(vp);
2500 VOP_UNLOCK(vp, NULL, 0, td);
2505 kern_rename(struct nameidata *fromnd, struct nameidata *tond)
2507 struct thread *td = curthread;
2508 struct proc *p = td->td_proc;
2509 struct vnode *tvp, *fvp, *tdvp;
2513 error = namei(fromnd);
2516 fvp = fromnd->ni_vp;
2517 if (fromnd->ni_vp->v_type == VDIR)
2518 tond->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2519 error = namei(tond);
2521 /* Translate error code for rename("dir1", "dir2/."). */
2522 if (error == EISDIR && fvp->v_type == VDIR)
2524 NDFREE(fromnd, NDF_ONLY_PNBUF);
2525 vrele(fromnd->ni_dvp);
2529 tdvp = tond->ni_dvp;
2532 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2535 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2543 * If the source is the same as the destination (that is, if they
2544 * are links to the same vnode), then there is nothing to do.
2550 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2551 if (fromnd->ni_dvp != tdvp) {
2552 VOP_LEASE(fromnd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2555 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2557 error = VOP_RENAME(fromnd->ni_dvp, NCPNULL, fromnd->ni_vp,
2558 &fromnd->ni_cnd, tond->ni_dvp, NCPNULL, tond->ni_vp,
2560 NDFREE(fromnd, NDF_ONLY_PNBUF);
2561 NDFREE(tond, NDF_ONLY_PNBUF);
2563 NDFREE(fromnd, NDF_ONLY_PNBUF);
2564 NDFREE(tond, NDF_ONLY_PNBUF);
2571 vrele(fromnd->ni_dvp);
2574 vrele(tond->ni_startdir);
2575 ASSERT_VOP_UNLOCKED(fromnd->ni_dvp, "rename");
2576 ASSERT_VOP_UNLOCKED(fromnd->ni_vp, "rename");
2577 ASSERT_VOP_UNLOCKED(tond->ni_dvp, "rename");
2578 ASSERT_VOP_UNLOCKED(tond->ni_vp, "rename");
2580 if (fromnd->ni_startdir)
2581 vrele(fromnd->ni_startdir);
2588 * rename_args(char *from, char *to)
2590 * Rename files. Source and destination must either both be directories,
2591 * or both not be directories. If target is a directory, it must be empty.
2594 rename(struct rename_args *uap)
2596 struct thread *td = curthread;
2597 struct nameidata fromnd, tond;
2600 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2601 UIO_USERSPACE, uap->from, td);
2602 NDINIT(&tond, NAMEI_RENAME,
2603 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2604 CNP_SAVESTART | CNP_NOOBJ,
2605 UIO_USERSPACE, uap->to, td);
2607 error = kern_rename(&fromnd, &tond);
2613 kern_mkdir(struct nameidata *nd, int mode)
2615 struct thread *td = curthread;
2616 struct proc *p = td->td_proc;
2622 nd->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2628 NDFREE(nd, NDF_ONLY_PNBUF);
2629 if (nd->ni_dvp == vp)
2637 vattr.va_type = VDIR;
2638 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2639 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2640 error = VOP_MKDIR(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
2642 NDFREE(nd, NDF_ONLY_PNBUF);
2646 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mkdir");
2647 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mkdir");
2652 * mkdir_args(char *path, int mode)
2654 * Make a directory file.
2658 mkdir(struct mkdir_args *uap)
2660 struct thread *td = curthread;
2661 struct nameidata nd;
2664 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
2667 error = kern_mkdir(&nd, uap->mode);
2673 kern_rmdir(struct nameidata *nd)
2675 struct thread *td = curthread;
2676 struct proc *p = td->td_proc;
2685 if (vp->v_type != VDIR) {
2690 * No rmdir "." please.
2692 if (nd->ni_dvp == vp) {
2697 * The root of a mounted filesystem cannot be deleted.
2699 if (vp->v_flag & VROOT)
2702 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2703 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2704 error = VOP_RMDIR(nd->ni_dvp, NCPNULL, nd->ni_vp,
2708 NDFREE(nd, NDF_ONLY_PNBUF);
2709 if (nd->ni_dvp == vp)
2715 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "rmdir");
2716 ASSERT_VOP_UNLOCKED(nd->ni_vp, "rmdir");
2721 * rmdir_args(char *path)
2723 * Remove a directory file.
2727 rmdir(struct rmdir_args *uap)
2729 struct thread *td = curthread;
2730 struct nameidata nd;
2733 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF,
2734 UIO_USERSPACE, uap->path, td);
2736 error = kern_rmdir(&nd);
2742 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2744 struct thread *td = curthread;
2745 struct proc *p = td->td_proc;
2753 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2755 if ((fp->f_flag & FREAD) == 0)
2757 vp = (struct vnode *)fp->f_data;
2759 if (vp->v_type != VDIR)
2761 aiov.iov_base = buf;
2762 aiov.iov_len = count;
2763 auio.uio_iov = &aiov;
2764 auio.uio_iovcnt = 1;
2765 auio.uio_rw = UIO_READ;
2766 auio.uio_segflg = UIO_USERSPACE;
2768 auio.uio_resid = count;
2769 /* vn_lock(vp, NULL, LK_SHARED | LK_RETRY, td); */
2770 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2771 loff = auio.uio_offset = fp->f_offset;
2772 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2773 fp->f_offset = auio.uio_offset;
2774 VOP_UNLOCK(vp, NULL, 0, td);
2777 if (count == auio.uio_resid) {
2778 if (union_dircheckp) {
2779 error = union_dircheckp(td, &vp, fp);
2785 if ((vp->v_flag & VROOT) &&
2786 (vp->v_mount->mnt_flag & MNT_UNION)) {
2787 struct vnode *tvp = vp;
2788 vp = vp->v_mount->mnt_vnodecovered;
2790 fp->f_data = (caddr_t)vp;
2799 *res = count - auio.uio_resid;
2804 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2806 * Read a block of directory entries in a file system independent format.
2809 getdirentries(struct getdirentries_args *uap)
2814 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2815 &uap->sysmsg_result);
2818 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2823 * getdents_args(int fd, char *buf, size_t count)
2826 getdents(struct getdents_args *uap)
2830 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2831 &uap->sysmsg_result);
2837 * umask(int newmask)
2839 * Set the mode mask for creation of filesystem nodes.
2844 umask(struct umask_args *uap)
2846 struct thread *td = curthread;
2847 struct proc *p = td->td_proc;
2848 struct filedesc *fdp;
2851 uap->sysmsg_result = fdp->fd_cmask;
2852 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2857 * revoke(char *path)
2859 * Void all references to file by ripping underlying filesystem
2864 revoke(struct revoke_args *uap)
2866 struct thread *td = curthread;
2867 struct proc *p = td->td_proc;
2871 struct nameidata nd;
2873 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2874 if ((error = namei(&nd)) != 0)
2877 NDFREE(&nd, NDF_ONLY_PNBUF);
2878 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2882 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2884 if (p->p_ucred->cr_uid != vattr.va_uid &&
2885 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2887 if (count_udev(vp->v_udev) > 0)
2888 VOP_REVOKE(vp, REVOKEALL);
2895 * Convert a user file descriptor to a kernel file entry.
2898 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2902 if ((u_int)fd >= fdp->fd_nfiles ||
2903 (fp = fdp->fd_ofiles[fd]) == NULL)
2905 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2911 * getfh_args(char *fname, fhandle_t *fhp)
2913 * Get (NFS) file handle
2916 getfh(struct getfh_args *uap)
2918 struct thread *td = curthread;
2919 struct nameidata nd;
2925 * Must be super user
2930 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2934 NDFREE(&nd, NDF_ONLY_PNBUF);
2936 bzero(&fh, sizeof(fh));
2937 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2938 error = VFS_VPTOFH(vp, &fh.fh_fid);
2942 error = copyout(&fh, uap->fhp, sizeof (fh));
2947 * fhopen_args(const struct fhandle *u_fhp, int flags)
2949 * syscall for the rpc.lockd to use to translate a NFS file handle into
2950 * an open descriptor.
2952 * warning: do not remove the suser() call or this becomes one giant
2956 fhopen(struct fhopen_args *uap)
2958 struct thread *td = curthread;
2959 struct proc *p = td->td_proc;
2964 struct vattr *vap = &vat;
2967 struct filedesc *fdp = p->p_fd;
2968 int fmode, mode, error, type;
2973 * Must be super user
2979 fmode = FFLAGS(SCARG(uap, flags));
2980 /* why not allow a non-read/write open for our lockd? */
2981 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2983 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2986 /* find the mount point */
2987 mp = vfs_getvfs(&fhp.fh_fsid);
2990 /* now give me my vnode, it gets returned to me locked */
2991 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2995 * from now on we have to make sure not
2996 * to forget about the vnode
2997 * any error that causes an abort must vput(vp)
2998 * just set error = err and 'goto bad;'.
3004 if (vp->v_type == VLNK) {
3008 if (vp->v_type == VSOCK) {
3013 if (fmode & (FWRITE | O_TRUNC)) {
3014 if (vp->v_type == VDIR) {
3018 error = vn_writechk(vp);
3026 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
3030 if (fmode & O_TRUNC) {
3031 VOP_UNLOCK(vp, NULL, 0, td); /* XXX */
3032 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
3033 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
3036 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
3040 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
3044 * Make sure that a VM object is created for VMIO support.
3046 if (vn_canvmio(vp) == TRUE) {
3047 if ((error = vfs_object_create(vp, td)) != 0)
3054 * end of vn_open code
3057 if ((error = falloc(p, &nfp, &indx)) != 0) {
3065 * hold an extra reference to avoid having fp ripped out
3066 * from under us while we block in the lock op.
3069 nfp->f_data = (caddr_t)vp;
3070 nfp->f_flag = fmode & FMASK;
3071 nfp->f_ops = &vnops;
3072 nfp->f_type = DTYPE_VNODE;
3073 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3074 lf.l_whence = SEEK_SET;
3077 if (fmode & O_EXLOCK)
3078 lf.l_type = F_WRLCK;
3080 lf.l_type = F_RDLCK;
3082 if ((fmode & FNONBLOCK) == 0)
3084 VOP_UNLOCK(vp, NULL, 0, td);
3085 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3087 * lock request failed. Normally close the descriptor
3088 * but handle the case where someone might have dup()d
3089 * or close()d it when we weren't looking.
3091 if (fdp->fd_ofiles[indx] == fp) {
3092 fdp->fd_ofiles[indx] = NULL;
3097 * release our private reference.
3102 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
3103 fp->f_flag |= FHASLOCK;
3105 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
3106 vfs_object_create(vp, td);
3108 VOP_UNLOCK(vp, NULL, 0, td);
3110 uap->sysmsg_result = indx;
3119 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3122 fhstat(struct fhstat_args *uap)
3124 struct thread *td = curthread;
3132 * Must be super user
3138 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3142 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3144 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3146 error = vn_stat(vp, &sb, td);
3150 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3155 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3158 fhstatfs(struct fhstatfs_args *uap)
3160 struct thread *td = curthread;
3169 * Must be super user
3171 if ((error = suser(td)))
3174 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3177 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3179 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3184 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3186 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3188 bcopy(sp, &sb, sizeof(sb));
3189 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3192 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3196 * Syscall to push extended attribute configuration information into the
3197 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3198 * a command (int cmd), and attribute name and misc data. For now, the
3199 * attribute name is left in userspace for consumption by the VFS_op.
3200 * It will probably be changed to be copied into sysspace by the
3201 * syscall in the future, once issues with various consumers of the
3202 * attribute code have raised their hands.
3204 * Currently this is used only by UFS Extended Attributes.
3207 extattrctl(struct extattrctl_args *uap)
3209 struct thread *td = curthread;
3210 struct nameidata nd;
3214 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3215 if ((error = namei(&nd)) != 0)
3217 mp = nd.ni_vp->v_mount;
3219 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3220 SCARG(uap, arg), td));
3224 * Syscall to set a named extended attribute on a file or directory.
3225 * Accepts attribute name, and a uio structure pointing to the data to set.
3226 * The uio is consumed in the style of writev(). The real work happens
3227 * in VOP_SETEXTATTR().
3230 extattr_set_file(struct extattr_set_file_args *uap)
3232 struct thread *td = curthread;
3233 struct proc *p = td->td_proc;
3234 struct nameidata nd;
3236 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3237 char attrname[EXTATTR_MAXNAMELEN];
3241 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3244 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3245 SCARG(uap, path), td);
3246 if ((error = namei(&nd)) != 0)
3248 iovlen = uap->iovcnt * sizeof(struct iovec);
3249 if (uap->iovcnt > UIO_SMALLIOV) {
3250 if (uap->iovcnt > UIO_MAXIOV) {
3254 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3259 auio.uio_iovcnt = uap->iovcnt;
3260 auio.uio_rw = UIO_WRITE;
3261 auio.uio_segflg = UIO_USERSPACE;
3263 auio.uio_offset = 0;
3264 if ((error = copyin(uap->iovp, iov, iovlen)))
3267 for (i = 0; i < uap->iovcnt; i++) {
3268 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3272 auio.uio_resid += iov->iov_len;
3275 cnt = auio.uio_resid;
3276 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3277 cnt -= auio.uio_resid;
3278 uap->sysmsg_result = cnt;
3281 FREE(needfree, M_IOV);
3287 * Syscall to get a named extended attribute on a file or directory.
3288 * Accepts attribute name, and a uio structure pointing to a buffer for the
3289 * data. The uio is consumed in the style of readv(). The real work
3290 * happens in VOP_GETEXTATTR();
3293 extattr_get_file(struct extattr_get_file_args *uap)
3295 struct thread *td = curthread;
3296 struct proc *p = td->td_proc;
3297 struct nameidata nd;
3299 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3300 char attrname[EXTATTR_MAXNAMELEN];
3304 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3307 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3308 SCARG(uap, path), td);
3309 if ((error = namei(&nd)) != 0)
3311 iovlen = uap->iovcnt * sizeof (struct iovec);
3312 if (uap->iovcnt > UIO_SMALLIOV) {
3313 if (uap->iovcnt > UIO_MAXIOV) {
3317 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3324 auio.uio_iovcnt = uap->iovcnt;
3325 auio.uio_rw = UIO_READ;
3326 auio.uio_segflg = UIO_USERSPACE;
3328 auio.uio_offset = 0;
3329 if ((error = copyin(uap->iovp, iov, iovlen)))
3332 for (i = 0; i < uap->iovcnt; i++) {
3333 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3337 auio.uio_resid += iov->iov_len;
3340 cnt = auio.uio_resid;
3341 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3342 cnt -= auio.uio_resid;
3343 uap->sysmsg_result = cnt;
3346 FREE(needfree, M_IOV);
3352 * Syscall to delete a named extended attribute from a file or directory.
3353 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3356 extattr_delete_file(struct extattr_delete_file_args *uap)
3358 struct thread *td = curthread;
3359 struct proc *p = td->td_proc;
3360 struct nameidata nd;
3361 char attrname[EXTATTR_MAXNAMELEN];
3364 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3367 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3368 SCARG(uap, path), td);
3369 if ((error = namei(&nd)) != 0)
3371 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);
3377 * print out statistics from the current status of the buffer pool
3378 * this can be toggeled by the system control option debug.syncprt
3387 int counts[(MAXBSIZE / PAGE_SIZE) + 1];
3388 static char *bname[3] = { "LOCKED", "LRU", "AGE" };
3390 for (dp = bufqueues, i = 0; dp < &bufqueues[3]; dp++, i++) {
3392 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3395 TAILQ_FOREACH(bp, dp, b_freelist) {
3396 counts[bp->b_bufsize/PAGE_SIZE]++;
3400 printf("%s: total-%d", bname[i], count);
3401 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3403 printf(", %d-%d", j * PAGE_SIZE, counts[j]);