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.45 2004/10/12 19:20:46 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];
115 struct nlcomponent nlc;
118 if (p->p_ucred->cr_prison != NULL)
120 if (usermount == 0 && (error = suser(td)))
123 * Do not allow NFS export by non-root users.
125 if (SCARG(uap, flags) & MNT_EXPORTED) {
131 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
134 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
137 * Lookup the requested path and extract the ncp and vnode.
139 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
141 if ((error = nlookup(&nd)) == 0) {
142 if (nd.nl_ncp->nc_vp == NULL)
152 * Extract the locked+refd ncp and cleanup the nd structure
159 * now we have the locked ref'd ncp and unreferenced vnode.
162 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
169 * Now we have an unlocked ref'd ncp and a locked ref'd vp
171 if (SCARG(uap, flags) & MNT_UPDATE) {
172 if ((vp->v_flag & VROOT) == 0) {
179 flag2 = mp->mnt_kern_flag;
181 * We only allow the filesystem to be reloaded if it
182 * is currently mounted read-only.
184 if ((SCARG(uap, flags) & MNT_RELOAD) &&
185 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
188 return (EOPNOTSUPP); /* Needs translation */
191 * Only root, or the user that did the original mount is
192 * permitted to update it.
194 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
195 (error = suser(td))) {
200 if (vfs_busy(mp, LK_NOWAIT, NULL, td)) {
205 if ((vp->v_flag & VMOUNT) != 0 ||
206 vp->v_mountedhere != NULL) {
212 vp->v_flag |= VMOUNT;
214 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
215 VOP_UNLOCK(vp, 0, td);
219 * If the user is not root, ensure that they own the directory
220 * onto which we are attempting to mount.
222 if ((error = VOP_GETATTR(vp, &va, td)) ||
223 (va.va_uid != p->p_ucred->cr_uid &&
224 (error = suser(td)))) {
229 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
234 if (vp->v_type != VDIR) {
239 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
244 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
245 if (!strcmp(vfsp->vfc_name, fstypename))
251 /* Only load modules for root (very important!) */
252 if ((error = suser(td)) != 0) {
257 error = linker_load_file(fstypename, &lf);
258 if (error || lf == NULL) {
266 /* lookup again, see if the VFS was loaded */
267 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
268 if (!strcmp(vfsp->vfc_name, fstypename))
273 linker_file_unload(lf);
279 if ((vp->v_flag & VMOUNT) != 0 ||
280 vp->v_mountedhere != NULL) {
285 vp->v_flag |= VMOUNT;
288 * Allocate and initialize the filesystem.
290 mp = malloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
291 TAILQ_INIT(&mp->mnt_nvnodelist);
292 TAILQ_INIT(&mp->mnt_reservedvnlist);
293 mp->mnt_nvnodelistsize = 0;
294 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
295 vfs_busy(mp, LK_NOWAIT, NULL, td);
296 mp->mnt_op = vfsp->vfc_vfsops;
298 vfsp->vfc_refcount++;
299 mp->mnt_stat.f_type = vfsp->vfc_typenum;
300 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
301 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
302 mp->mnt_vnodecovered = vp;
303 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
304 mp->mnt_iosize_max = DFLTPHYS;
305 VOP_UNLOCK(vp, 0, td);
308 * Set the mount level flags.
310 if (SCARG(uap, flags) & MNT_RDONLY)
311 mp->mnt_flag |= MNT_RDONLY;
312 else if (mp->mnt_flag & MNT_RDONLY)
313 mp->mnt_kern_flag |= MNTK_WANTRDWR;
314 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
315 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
316 MNT_NOSYMFOLLOW | MNT_IGNORE |
317 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
318 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
319 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
320 MNT_NOSYMFOLLOW | MNT_IGNORE |
321 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
323 * Mount the filesystem.
324 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
325 * get. No freeing of cn_pnbuf.
327 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), td);
328 if (mp->mnt_flag & MNT_UPDATE) {
329 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
330 mp->mnt_flag &= ~MNT_RDONLY;
331 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
332 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
335 mp->mnt_kern_flag = flag2;
338 vp->v_flag &= ~VMOUNT;
343 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
345 * Put the new filesystem on the mount list after root. The mount
346 * point gets its own mnt_ncp which is a special ncp linking the
347 * vnode-under to the root of the new mount. The lookup code
348 * detects the mount point going forward and detects the special
349 * mnt_ncp via NCP_MOUNTPT going backwards.
351 * It is not necessary to invalidate or purge the vnode underneath
352 * because elements under the mount will be given their own glue
356 nlc.nlc_nameptr = "";
358 mp->mnt_ncp = cache_nlookup(ncp, &nlc);
359 mp->mnt_ncp->nc_flag |= NCF_MOUNTPT;
360 mp->mnt_ncp->nc_mount = mp;
362 /* XXX get the root of the fs and cache_setvp(mnt_ncp...) */
363 vp->v_flag &= ~VMOUNT;
364 vp->v_mountedhere = mp;
365 lwkt_gettoken(&ilock, &mountlist_token);
366 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
367 lwkt_reltoken(&ilock);
368 checkdirs(vp, mp->mnt_ncp);
369 cache_unlock(mp->mnt_ncp); /* leave ref intact */
370 VOP_UNLOCK(vp, 0, td);
371 error = vfs_allocate_syncvnode(mp);
373 if ((error = VFS_START(mp, 0, td)) != 0)
376 vfs_rm_vnodeops(&mp->mnt_vn_ops);
377 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
378 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
379 vp->v_flag &= ~VMOUNT;
380 mp->mnt_vfc->vfc_refcount--;
390 * Scan all active processes to see if any of them have a current
391 * or root directory onto which the new filesystem has just been
392 * mounted. If so, replace them with the new mount point.
394 * The passed ncp is ref'd and locked (from the mount code) and
395 * must be associated with the vnode representing the root of the
399 checkdirs(struct vnode *olddp, struct namecache *ncp)
401 struct filedesc *fdp;
406 if (olddp->v_usecount == 1)
408 mp = olddp->v_mountedhere;
409 if (VFS_ROOT(mp, &newdp))
410 panic("mount: lost mount");
411 cache_setvp(ncp, newdp);
413 if (rootvnode == olddp) {
415 vfs_cache_setroot(newdp, cache_hold(ncp));
418 FOREACH_PROC_IN_SYSTEM(p) {
420 if (fdp->fd_cdir == olddp) {
423 fdp->fd_cdir = newdp;
424 cache_drop(fdp->fd_ncdir);
425 fdp->fd_ncdir = cache_hold(ncp);
427 if (fdp->fd_rdir == olddp) {
430 fdp->fd_rdir = newdp;
431 cache_drop(fdp->fd_nrdir);
432 fdp->fd_nrdir = cache_hold(ncp);
439 * Unmount a file system.
441 * Note: unmount takes a path to the vnode mounted on as argument,
442 * not special file (as before).
445 * umount_args(char *path, int flags)
449 unmount(struct unmount_args *uap)
451 struct thread *td = curthread;
452 struct proc *p = td->td_proc;
459 if (p->p_ucred->cr_prison != NULL)
461 if (usermount == 0 && (error = suser(td)))
464 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
465 SCARG(uap, path), td);
466 if ((error = namei(&nd)) != 0)
469 NDFREE(&nd, NDF_ONLY_PNBUF);
473 * Only root, or the user that did the original mount is
474 * permitted to unmount this filesystem.
476 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
477 (error = suser(td))) {
483 * Don't allow unmounting the root file system.
485 if (mp->mnt_flag & MNT_ROOTFS) {
491 * Must be the root of the filesystem
493 if ((vp->v_flag & VROOT) == 0) {
498 return (dounmount(mp, SCARG(uap, flags), td));
502 * Do the actual file system unmount.
505 dounmount(struct mount *mp, int flags, struct thread *td)
507 struct vnode *coveredvp;
512 lwkt_gettoken(&ilock, &mountlist_token);
513 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
514 lwkt_reltoken(&ilock);
517 mp->mnt_kern_flag |= MNTK_UNMOUNT;
518 /* Allow filesystems to detect that a forced unmount is in progress. */
519 if (flags & MNT_FORCE)
520 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
521 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
522 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
524 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
525 if (mp->mnt_kern_flag & MNTK_MWAIT)
530 if (mp->mnt_flag & MNT_EXPUBLIC)
531 vfs_setpublicfs(NULL, NULL, NULL);
533 vfs_msync(mp, MNT_WAIT);
534 async_flag = mp->mnt_flag & MNT_ASYNC;
535 mp->mnt_flag &=~ MNT_ASYNC;
536 cache_purgevfs(mp); /* remove cache entries for this file sys */
537 if (mp->mnt_syncer != NULL)
538 vrele(mp->mnt_syncer);
539 if (((mp->mnt_flag & MNT_RDONLY) ||
540 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
542 error = VFS_UNMOUNT(mp, flags, td);
543 lwkt_gettokref(&ilock);
545 if (mp->mnt_syncer == NULL)
546 vfs_allocate_syncvnode(mp);
547 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
548 mp->mnt_flag |= async_flag;
549 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
551 if (mp->mnt_kern_flag & MNTK_MWAIT)
555 TAILQ_REMOVE(&mountlist, mp, mnt_list);
558 * Remove any installed vnode ops here so the individual VFSs don't
561 vfs_rm_vnodeops(&mp->mnt_vn_ops);
562 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
563 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
565 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
566 coveredvp->v_mountedhere = NULL;
568 cache_drop(mp->mnt_ncp);
571 mp->mnt_vfc->vfc_refcount--;
572 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
573 panic("unmount: dangling vnode");
574 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
575 if (mp->mnt_kern_flag & MNTK_MWAIT)
582 * Sync each mounted filesystem.
586 static int syncprt = 0;
587 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
592 sync(struct sync_args *uap)
594 struct thread *td = curthread;
595 struct mount *mp, *nmp;
599 lwkt_gettoken(&ilock, &mountlist_token);
600 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
601 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
602 nmp = TAILQ_NEXT(mp, mnt_list);
605 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
606 asyncflag = mp->mnt_flag & MNT_ASYNC;
607 mp->mnt_flag &= ~MNT_ASYNC;
608 vfs_msync(mp, MNT_NOWAIT);
609 VFS_SYNC(mp, MNT_NOWAIT, td);
610 mp->mnt_flag |= asyncflag;
612 lwkt_gettokref(&ilock);
613 nmp = TAILQ_NEXT(mp, mnt_list);
616 lwkt_reltoken(&ilock);
618 * print out buffer pool stat information on each sync() call.
627 /* XXX PRISON: could be per prison flag */
628 static int prison_quotas;
630 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
634 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
636 * Change filesystem quotas.
640 quotactl(struct quotactl_args *uap)
642 struct thread *td = curthread;
643 struct proc *p = td->td_proc;
649 if (p->p_ucred->cr_prison && !prison_quotas)
651 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
652 SCARG(uap, path), td);
653 if ((error = namei(&nd)) != 0)
655 mp = nd.ni_vp->v_mount;
656 NDFREE(&nd, NDF_ONLY_PNBUF);
658 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
659 SCARG(uap, arg), td));
663 kern_statfs(struct nameidata *nd, struct statfs *buf)
665 struct thread *td = curthread;
673 mp = nd->ni_vp->v_mount;
675 NDFREE(nd, NDF_ONLY_PNBUF);
677 error = VFS_STATFS(mp, sp, td);
680 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
681 bcopy(sp, buf, sizeof(*buf));
682 /* Only root should have access to the fsid's. */
684 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
689 * statfs_args(char *path, struct statfs *buf)
691 * Get filesystem statistics.
694 statfs(struct statfs_args *uap)
696 struct thread *td = curthread;
701 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
703 error = kern_statfs(&nd, &buf);
706 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
711 kern_fstatfs(int fd, struct statfs *buf)
713 struct thread *td = curthread;
714 struct proc *p = td->td_proc;
721 error = getvnode(p->p_fd, fd, &fp);
724 mp = ((struct vnode *)fp->f_data)->v_mount;
728 error = VFS_STATFS(mp, sp, td);
731 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
732 bcopy(sp, buf, sizeof(*buf));
733 /* Only root should have access to the fsid's. */
735 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
740 * fstatfs_args(int fd, struct statfs *buf)
742 * Get filesystem statistics.
745 fstatfs(struct fstatfs_args *uap)
750 error = kern_fstatfs(uap->fd, &buf);
753 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
758 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
760 * Get statistics on all filesystems.
764 getfsstat(struct getfsstat_args *uap)
766 struct thread *td = curthread;
767 struct mount *mp, *nmp;
771 long count, maxcount, error;
773 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
774 sfsp = (caddr_t)SCARG(uap, buf);
776 lwkt_gettoken(&ilock, &mountlist_token);
777 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
778 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
779 nmp = TAILQ_NEXT(mp, mnt_list);
782 if (sfsp && count < maxcount) {
785 * If MNT_NOWAIT or MNT_LAZY is specified, do not
786 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
787 * overrides MNT_WAIT.
789 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
790 (SCARG(uap, flags) & MNT_WAIT)) &&
791 (error = VFS_STATFS(mp, sp, td))) {
792 lwkt_gettokref(&ilock);
793 nmp = TAILQ_NEXT(mp, mnt_list);
797 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
798 error = copyout(sp, sfsp, sizeof(*sp));
806 lwkt_gettokref(&ilock);
807 nmp = TAILQ_NEXT(mp, mnt_list);
810 lwkt_reltoken(&ilock);
811 if (sfsp && count > maxcount)
812 uap->sysmsg_result = maxcount;
814 uap->sysmsg_result = count;
819 * fchdir_args(int fd)
821 * Change current working directory to a given file descriptor.
825 fchdir(struct fchdir_args *uap)
827 struct thread *td = curthread;
828 struct proc *p = td->td_proc;
829 struct filedesc *fdp = p->p_fd;
830 struct vnode *vp, *ovp;
833 struct namecache *ncp, *oncp;
834 struct namecache *nct;
837 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
839 vp = (struct vnode *)fp->f_data;
841 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
842 if (vp->v_type != VDIR || fp->f_ncp == NULL)
845 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
850 ncp = cache_hold(fp->f_ncp);
851 while (!error && (mp = vp->v_mountedhere) != NULL) {
852 error = nlookup_mp(mp, &nct);
854 cache_unlock(nct); /* leave ref intact */
857 error = vget(vp, LK_SHARED, td);
858 KKASSERT(error == 0);
865 oncp = fdp->fd_ncdir;
866 VOP_UNLOCK(vp, 0, td); /* leave ref intact */
879 kern_chdir(struct nlookupdata *nd)
881 struct thread *td = curthread;
882 struct proc *p = td->td_proc;
883 struct filedesc *fdp = p->p_fd;
884 struct vnode *vp, *ovp;
885 struct namecache *oncp;
888 if ((error = nlookup(nd)) != 0)
890 if ((vp = nd->nl_ncp->nc_vp) == NULL)
892 if ((error = vget(vp, LK_SHARED, td)) != 0)
895 error = checkvp_chdir(vp, td);
896 VOP_UNLOCK(vp, 0, td);
899 oncp = fdp->fd_ncdir;
900 cache_unlock(nd->nl_ncp); /* leave reference intact */
901 fdp->fd_ncdir = nd->nl_ncp;
913 * chdir_args(char *path)
915 * Change current working directory (``.'').
918 chdir(struct chdir_args *uap)
920 struct nlookupdata nd;
923 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
925 error = kern_chdir(&nd);
932 * Helper function for raised chroot(2) security function: Refuse if
933 * any filedescriptors are open directories.
936 chroot_refuse_vdir_fds(fdp)
937 struct filedesc *fdp;
944 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
945 error = getvnode(fdp, fd, &fp);
948 vp = (struct vnode *)fp->f_data;
949 if (vp->v_type != VDIR)
957 * This sysctl determines if we will allow a process to chroot(2) if it
958 * has a directory open:
959 * 0: disallowed for all processes.
960 * 1: allowed for processes that were not already chroot(2)'ed.
961 * 2: allowed for all processes.
964 static int chroot_allow_open_directories = 1;
966 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
967 &chroot_allow_open_directories, 0, "");
970 * chroot to the specified namecache entry. We obtain the vp from the
971 * namecache data. The passed ncp must be locked and referenced and will
972 * remain locked and referenced on return.
975 kern_chroot(struct nlookupdata *nd)
977 struct thread *td = curthread;
978 struct proc *p = td->td_proc;
979 struct filedesc *fdp = p->p_fd;
980 struct namecache *ncp;
985 * Only root can chroot
987 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
990 if ((error = nlookup(nd)) != 0)
995 * Disallow open directory descriptors (fchdir() breakouts).
997 if (chroot_allow_open_directories == 0 ||
998 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
999 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1002 if ((vp = ncp->nc_vp) == NULL)
1005 if ((error = vget(vp, LK_SHARED, td)) != 0)
1009 * Check the validity of vp as a directory to change to and
1010 * associate it with rdir/jdir.
1012 error = checkvp_chdir(vp, td);
1013 VOP_UNLOCK(vp, 0, td); /* leave reference intact */
1015 vrele(fdp->fd_rdir);
1016 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1017 cache_drop(fdp->fd_nrdir);
1018 fdp->fd_nrdir = cache_hold(ncp);
1019 if (fdp->fd_jdir == NULL) {
1022 fdp->fd_njdir = cache_hold(ncp);
1031 * chroot_args(char *path)
1033 * Change notion of root (``/'') directory.
1037 chroot(struct chroot_args *uap)
1039 struct thread *td = curthread;
1040 struct nlookupdata nd;
1043 KKASSERT(td->td_proc);
1044 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1046 error = kern_chroot(&nd);
1053 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1054 * determine whether it is legal to chdir to the vnode. The vnode's state
1055 * is not changed by this call.
1058 checkvp_chdir(struct vnode *vp, struct thread *td)
1062 if (vp->v_type != VDIR)
1065 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
1070 kern_open(struct nameidata *nd, int oflags, int mode, int *res)
1072 struct thread *td = curthread;
1073 struct proc *p = td->td_proc;
1074 struct filedesc *fdp = p->p_fd;
1079 int type, indx, error;
1082 struct nlookupdata ndx;
1084 if ((oflags & O_ACCMODE) == O_ACCMODE)
1086 flags = FFLAGS(oflags);
1087 error = falloc(p, &nfp, &indx);
1091 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1092 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
1094 * Bump the ref count to prevent another process from closing
1095 * the descriptor while we are blocked in vn_open()
1100 * XXX temporary hack so we can record the namecache pointer
1101 * associated with an open descriptor.
1103 ndxerror = nlookup_init(&ndx, nd->ni_dirp, nd->ni_segflg,
1104 ((nd->ni_cnd.cn_flags & CNP_FOLLOW) ? NLC_FOLLOW : 0));
1106 error = vn_open(nd, flags, cmode);
1109 * release our own reference
1114 * handle special fdopen() case. bleh. dupfdopen() is
1115 * responsible for dropping the old contents of ofiles[indx]
1118 if ((error == ENODEV || error == ENXIO) &&
1119 p->p_dupfd >= 0 && /* XXX from fdopen */
1121 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
1127 * Clean up the descriptor, but only if another thread hadn't
1128 * replaced or closed it.
1130 if (fdp->fd_ofiles[indx] == fp) {
1131 fdp->fd_ofiles[indx] = NULL;
1135 if (error == ERESTART)
1141 NDFREE(nd, NDF_ONLY_PNBUF);
1145 * There should be 2 references on the file, one from the descriptor
1146 * table, and one for us.
1148 * Handle the case where someone closed the file (via its file
1149 * descriptor) while we were blocked. The end result should look
1150 * like opening the file succeeded but it was immediately closed.
1152 if (fp->f_count == 1) {
1153 KASSERT(fdp->fd_ofiles[indx] != fp,
1154 ("Open file descriptor lost all refs"));
1155 VOP_UNLOCK(vp, 0, td);
1156 vn_close(vp, flags & FMASK, td);
1163 fp->f_data = (caddr_t)vp;
1164 fp->f_flag = flags & FMASK;
1166 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1167 if (flags & (O_EXLOCK | O_SHLOCK)) {
1168 lf.l_whence = SEEK_SET;
1171 if (flags & O_EXLOCK)
1172 lf.l_type = F_WRLCK;
1174 lf.l_type = F_RDLCK;
1176 if ((flags & FNONBLOCK) == 0)
1178 VOP_UNLOCK(vp, 0, td);
1179 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1181 * lock request failed. Normally close the descriptor
1182 * but handle the case where someone might have dup()d
1183 * it when we weren't looking. One reference is
1184 * owned by the descriptor array, the other by us.
1186 if (fdp->fd_ofiles[indx] == fp) {
1187 fdp->fd_ofiles[indx] = NULL;
1194 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1195 fp->f_flag |= FHASLOCK;
1197 /* assert that vn_open created a backing object if one is needed */
1198 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1199 ("open: vmio vnode has no backing object after vn_open"));
1200 VOP_UNLOCK(vp, 0, td);
1203 * If the vp is a directory locate the ncp to store with the file
1204 * descriptor. XXX temporary. We may eventually wish to do this
1205 * permanently as it would provide an invaluable diagnostic tool,
1206 * but first the entire open path needs to be converted from
1207 * namei to nlookup so we can avoid having to do a double-lookup.
1209 * The primary purpose of storing the ncp with the file pointer is
1210 * so it can be used in fchdir() and fchroot() syscalls, allowing
1211 * us to retain an unbroken namecache topology.
1213 if (ndxerror == 0 && vp->v_type == VDIR) {
1214 if ((ndxerror = nlookup(&ndx)) == 0) {
1215 fp->f_ncp = ndx.nl_ncp;
1217 cache_unlock(fp->f_ncp);
1223 * release our private reference, leaving the one associated with the
1224 * descriptor table intact.
1232 * open_args(char *path, int flags, int mode)
1234 * Check permissions, allocate an open file structure,
1235 * and call the device open routine if any.
1238 open(struct open_args *uap)
1240 struct thread *td = curthread;
1241 struct nameidata nd;
1244 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1246 error = kern_open(&nd, uap->flags, uap->mode, &uap->sysmsg_result);
1252 kern_mknod(struct nameidata *nd, int mode, int dev)
1254 struct thread *td = curthread;
1255 struct proc *p = td->td_proc;
1263 switch (mode & S_IFMT) {
1269 error = suser_cred(p->p_ucred, PRISON_ROOT);
1283 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1284 vattr.va_rdev = dev;
1287 switch (mode & S_IFMT) {
1288 case S_IFMT: /* used by badsect to flag bad sectors */
1289 vattr.va_type = VBAD;
1292 vattr.va_type = VCHR;
1295 vattr.va_type = VBLK;
1306 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1308 error = VOP_WHITEOUT(nd->ni_dvp, NCPNULL,
1309 &nd->ni_cnd, NAMEI_CREATE);
1311 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp,
1312 &nd->ni_cnd, &vattr);
1316 NDFREE(nd, NDF_ONLY_PNBUF);
1319 NDFREE(nd, NDF_ONLY_PNBUF);
1320 if (nd->ni_dvp == vp)
1327 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mknod");
1328 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mknod");
1333 * mknod_args(char *path, int mode, int dev)
1335 * Create a special file.
1338 mknod(struct mknod_args *uap)
1340 struct thread *td = curthread;
1341 struct nameidata nd;
1344 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1347 error = kern_mknod(&nd, uap->mode, uap->dev);
1353 kern_mkfifo(struct nameidata *nd, int mode)
1355 struct thread *td = curthread;
1356 struct proc *p = td->td_proc;
1364 if (nd->ni_vp != NULL) {
1365 NDFREE(nd, NDF_ONLY_PNBUF);
1366 if (nd->ni_dvp == nd->ni_vp)
1374 vattr.va_type = VFIFO;
1375 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1376 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1377 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd, &vattr);
1380 NDFREE(nd, NDF_ONLY_PNBUF);
1386 * mkfifo_args(char *path, int mode)
1388 * Create a named pipe.
1391 mkfifo(struct mkfifo_args *uap)
1393 struct thread *td = curthread;
1394 struct nameidata nd;
1397 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1400 error = kern_mkfifo(&nd, uap->mode);
1406 kern_link(struct nameidata *nd, struct nameidata *linknd)
1408 struct thread *td = curthread;
1409 struct proc *p = td->td_proc;
1417 NDFREE(nd, NDF_ONLY_PNBUF);
1419 if (vp->v_type == VDIR)
1420 error = EPERM; /* POSIX */
1422 error = namei(linknd);
1424 if (linknd->ni_vp != NULL) {
1426 vrele(linknd->ni_vp);
1429 VOP_LEASE(linknd->ni_dvp, td, p->p_ucred,
1431 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1432 error = VOP_LINK(linknd->ni_dvp, NCPNULL, vp,
1435 NDFREE(linknd, NDF_ONLY_PNBUF);
1436 if (linknd->ni_dvp == linknd->ni_vp)
1437 vrele(linknd->ni_dvp);
1439 vput(linknd->ni_dvp);
1440 ASSERT_VOP_UNLOCKED(linknd->ni_dvp, "link");
1441 ASSERT_VOP_UNLOCKED(linknd->ni_vp, "link");
1449 * link_args(char *path, char *link)
1451 * Make a hard file link.
1454 link(struct link_args *uap)
1456 struct thread *td = curthread;
1457 struct nameidata nd, linknd;
1460 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1462 NDINIT(&linknd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1463 UIO_USERSPACE, uap->link, td);
1465 error = kern_link(&nd, &linknd);
1471 kern_symlink(char *path, struct nameidata *nd)
1473 struct thread *td = curthread;
1474 struct proc *p = td->td_proc;
1483 NDFREE(nd, NDF_ONLY_PNBUF);
1484 if (nd->ni_dvp == nd->ni_vp)
1492 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1493 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1494 error = VOP_SYMLINK(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
1496 NDFREE(nd, NDF_ONLY_PNBUF);
1500 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "symlink");
1501 ASSERT_VOP_UNLOCKED(nd->ni_vp, "symlink");
1507 * symlink(char *path, char *link)
1509 * Make a symbolic link.
1512 symlink(struct symlink_args *uap)
1514 struct thread *td = curthread;
1515 struct nameidata nd;
1519 path = zalloc(namei_zone);
1520 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1522 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1523 UIO_USERSPACE, uap->link, td);
1524 error = kern_symlink(path, &nd);
1526 zfree(namei_zone, path);
1531 * undelete_args(char *path)
1533 * Delete a whiteout from the filesystem.
1537 undelete(struct undelete_args *uap)
1539 struct thread *td = curthread;
1540 struct proc *p = td->td_proc;
1542 struct nameidata nd;
1545 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
1546 SCARG(uap, path), td);
1551 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
1552 NDFREE(&nd, NDF_ONLY_PNBUF);
1553 if (nd.ni_dvp == nd.ni_vp)
1562 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1563 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
1565 cache_purge(nd.ni_vp);
1566 NDFREE(&nd, NDF_ONLY_PNBUF);
1568 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1569 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1574 kern_unlink(struct nameidata *nd)
1576 struct thread *td = curthread;
1577 struct proc *p = td->td_proc;
1586 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1587 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1589 if (vp->v_type == VDIR)
1590 error = EPERM; /* POSIX */
1593 * The root of a mounted filesystem cannot be deleted.
1595 * XXX: can this only be a VDIR case?
1597 if (vp->v_flag & VROOT)
1602 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1603 error = VOP_REMOVE(nd->ni_dvp, NCPNULL, vp, &nd->ni_cnd);
1607 NDFREE(nd, NDF_ONLY_PNBUF);
1608 if (nd->ni_dvp == vp)
1614 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "unlink");
1615 ASSERT_VOP_UNLOCKED(nd->ni_vp, "unlink");
1620 * unlink_args(char *path)
1622 * Delete a name from the filesystem.
1625 unlink(struct unlink_args *uap)
1627 struct thread *td = curthread;
1628 struct nameidata nd;
1631 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1634 error = kern_unlink(&nd);
1640 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1642 struct thread *td = curthread;
1643 struct proc *p = td->td_proc;
1644 struct filedesc *fdp = p->p_fd;
1649 if (fd >= fdp->fd_nfiles ||
1650 (fp = fdp->fd_ofiles[fd]) == NULL)
1652 if (fp->f_type != DTYPE_VNODE)
1656 fp->f_offset += offset;
1659 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1662 fp->f_offset = offset + vattr.va_size;
1665 fp->f_offset = offset;
1670 *res = fp->f_offset;
1675 * lseek_args(int fd, int pad, off_t offset, int whence)
1677 * Reposition read/write file offset.
1680 lseek(struct lseek_args *uap)
1684 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1685 &uap->sysmsg_offset);
1691 kern_access(struct nameidata *nd, int aflags)
1693 struct thread *td = curthread;
1694 struct proc *p = td->td_proc;
1695 struct ucred *cred, *tmpcred;
1701 * Create and modify a temporary credential instead of one that
1702 * is potentially shared. This could also mess up socket
1703 * buffer accounting which can run in an interrupt context.
1705 tmpcred = crdup(cred);
1706 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1707 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1708 p->p_ucred = tmpcred;
1709 nd->ni_cnd.cn_cred = tmpcred;
1715 /* Flags == 0 means only check for existence. */
1724 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1725 error = VOP_ACCESS(vp, flags, tmpcred, td);
1727 NDFREE(nd, NDF_ONLY_PNBUF);
1736 * access_args(char *path, int flags)
1738 * Check access permissions.
1741 access(struct access_args *uap)
1743 struct thread *td = curthread;
1744 struct nameidata nd;
1747 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1748 UIO_USERSPACE, uap->path, td);
1750 error = kern_access(&nd, uap->flags);
1756 kern_stat(struct nlookupdata *nd, struct stat *st)
1762 if ((error = nlookup(nd)) != 0)
1764 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1768 if ((error = vget(vp, LK_SHARED, td)) != 0)
1770 error = vn_stat(vp, st, td);
1776 * stat_args(char *path, struct stat *ub)
1778 * Get file status; this version follows links.
1781 stat(struct stat_args *uap)
1783 struct nlookupdata nd;
1787 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1789 error = kern_stat(&nd, &st);
1791 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1798 * lstat_args(char *path, struct stat *ub)
1800 * Get file status; this version does not follow links.
1803 lstat(struct lstat_args *uap)
1805 struct nlookupdata nd;
1809 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1811 error = kern_stat(&nd, &st);
1813 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1824 nsb->st_dev = sb->st_dev;
1825 nsb->st_ino = sb->st_ino;
1826 nsb->st_mode = sb->st_mode;
1827 nsb->st_nlink = sb->st_nlink;
1828 nsb->st_uid = sb->st_uid;
1829 nsb->st_gid = sb->st_gid;
1830 nsb->st_rdev = sb->st_rdev;
1831 nsb->st_atimespec = sb->st_atimespec;
1832 nsb->st_mtimespec = sb->st_mtimespec;
1833 nsb->st_ctimespec = sb->st_ctimespec;
1834 nsb->st_size = sb->st_size;
1835 nsb->st_blocks = sb->st_blocks;
1836 nsb->st_blksize = sb->st_blksize;
1837 nsb->st_flags = sb->st_flags;
1838 nsb->st_gen = sb->st_gen;
1839 nsb->st_qspare[0] = sb->st_qspare[0];
1840 nsb->st_qspare[1] = sb->st_qspare[1];
1844 * nstat_args(char *path, struct nstat *ub)
1848 nstat(struct nstat_args *uap)
1850 struct thread *td = curthread;
1854 struct nameidata nd;
1856 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1857 UIO_USERSPACE, SCARG(uap, path), td);
1858 if ((error = namei(&nd)) != 0)
1860 NDFREE(&nd, NDF_ONLY_PNBUF);
1861 error = vn_stat(nd.ni_vp, &sb, td);
1865 cvtnstat(&sb, &nsb);
1866 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1871 * lstat_args(char *path, struct stat *ub)
1873 * Get file status; this version does not follow links.
1877 nlstat(struct nlstat_args *uap)
1879 struct thread *td = curthread;
1884 struct nameidata nd;
1886 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1887 UIO_USERSPACE, SCARG(uap, path), td);
1888 if ((error = namei(&nd)) != 0)
1891 NDFREE(&nd, NDF_ONLY_PNBUF);
1892 error = vn_stat(vp, &sb, td);
1896 cvtnstat(&sb, &nsb);
1897 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1902 * pathconf_Args(char *path, int name)
1904 * Get configurable pathname variables.
1908 pathconf(struct pathconf_args *uap)
1910 struct thread *td = curthread;
1912 struct nameidata nd;
1914 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1915 UIO_USERSPACE, SCARG(uap, path), td);
1916 if ((error = namei(&nd)) != 0)
1918 NDFREE(&nd, NDF_ONLY_PNBUF);
1919 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1926 * kern_readlink isn't properly split yet. There is a copyin burried
1927 * in VOP_READLINK().
1930 kern_readlink(struct nameidata *nd, char *buf, int count, int *res)
1932 struct thread *td = curthread;
1933 struct proc *p = td->td_proc;
1942 NDFREE(nd, NDF_ONLY_PNBUF);
1944 if (vp->v_type != VLNK)
1947 aiov.iov_base = buf;
1948 aiov.iov_len = count;
1949 auio.uio_iov = &aiov;
1950 auio.uio_iovcnt = 1;
1951 auio.uio_offset = 0;
1952 auio.uio_rw = UIO_READ;
1953 auio.uio_segflg = UIO_USERSPACE;
1955 auio.uio_resid = count;
1956 error = VOP_READLINK(vp, &auio, p->p_ucred);
1959 *res = count - auio.uio_resid;
1964 * readlink_args(char *path, char *buf, int count)
1966 * Return target name of a symbolic link.
1969 readlink(struct readlink_args *uap)
1971 struct thread *td = curthread;
1972 struct nameidata nd;
1975 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ, UIO_USERSPACE,
1978 error = kern_readlink(&nd, uap->buf, uap->count,
1979 &uap->sysmsg_result);
1985 setfflags(struct vnode *vp, int flags)
1987 struct thread *td = curthread;
1988 struct proc *p = td->td_proc;
1993 * Prevent non-root users from setting flags on devices. When
1994 * a device is reused, users can retain ownership of the device
1995 * if they are allowed to set flags and programs assume that
1996 * chown can't fail when done as root.
1998 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1999 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2002 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2003 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2005 vattr.va_flags = flags;
2006 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2007 VOP_UNLOCK(vp, 0, td);
2012 * chflags(char *path, int flags)
2014 * Change flags of a file given a path name.
2018 chflags(struct chflags_args *uap)
2020 struct thread *td = curthread;
2022 struct nameidata nd;
2024 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
2025 SCARG(uap, path), td);
2026 if ((error = namei(&nd)) != 0)
2028 NDFREE(&nd, NDF_ONLY_PNBUF);
2029 error = setfflags(nd.ni_vp, SCARG(uap, flags));
2035 * fchflags_args(int fd, int flags)
2037 * Change flags of a file given a file descriptor.
2041 fchflags(struct fchflags_args *uap)
2043 struct thread *td = curthread;
2044 struct proc *p = td->td_proc;
2048 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2050 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
2054 setfmode(struct vnode *vp, int mode)
2056 struct thread *td = curthread;
2057 struct proc *p = td->td_proc;
2061 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2062 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2064 vattr.va_mode = mode & ALLPERMS;
2065 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2066 VOP_UNLOCK(vp, 0, td);
2071 kern_chmod(struct nameidata *nd, int mode)
2078 NDFREE(nd, NDF_ONLY_PNBUF);
2079 error = setfmode(nd->ni_vp, mode);
2085 * chmod_args(char *path, int mode)
2087 * Change mode of a file given path name.
2091 chmod(struct chmod_args *uap)
2093 struct thread *td = curthread;
2094 struct nameidata nd;
2097 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2099 error = kern_chmod(&nd, uap->mode);
2105 * lchmod_args(char *path, int mode)
2107 * Change mode of a file given path name (don't follow links.)
2111 lchmod(struct lchmod_args *uap)
2113 struct thread *td = curthread;
2115 struct nameidata nd;
2117 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
2118 if ((error = namei(&nd)) != 0)
2120 NDFREE(&nd, NDF_ONLY_PNBUF);
2121 error = setfmode(nd.ni_vp, SCARG(uap, mode));
2127 * fchmod_args(int fd, int mode)
2129 * Change mode of a file given a file descriptor.
2133 fchmod(struct fchmod_args *uap)
2135 struct thread *td = curthread;
2136 struct proc *p = td->td_proc;
2140 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2142 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
2146 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2148 struct thread *td = curthread;
2149 struct proc *p = td->td_proc;
2153 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2154 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2158 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2159 VOP_UNLOCK(vp, 0, td);
2164 kern_chown(struct nameidata *nd, int uid, int gid)
2171 NDFREE(nd, NDF_ONLY_PNBUF);
2172 error = setfown(nd->ni_vp, uid, gid);
2178 * chown(char *path, int uid, int gid)
2180 * Set ownership given a path name.
2183 chown(struct chown_args *uap)
2185 struct thread *td = curthread;
2186 struct nameidata nd;
2189 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2191 error = kern_chown(&nd, uap->uid, uap->gid);
2197 * lchown_args(char *path, int uid, int gid)
2199 * Set ownership given a path name, do not cross symlinks.
2202 lchown(struct lchown_args *uap)
2204 struct thread *td = curthread;
2206 struct nameidata nd;
2208 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2210 error = kern_chown(&nd, uap->uid, uap->gid);
2216 * fchown_args(int fd, int uid, int gid)
2218 * Set ownership given a file descriptor.
2222 fchown(struct fchown_args *uap)
2224 struct thread *td = curthread;
2225 struct proc *p = td->td_proc;
2229 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2231 return setfown((struct vnode *)fp->f_data,
2232 SCARG(uap, uid), SCARG(uap, gid));
2236 getutimes(const struct timeval *tvp, struct timespec *tsp)
2238 struct timeval tv[2];
2242 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2245 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2246 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2252 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2254 struct thread *td = curthread;
2255 struct proc *p = td->td_proc;
2259 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2260 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2262 vattr.va_atime = ts[0];
2263 vattr.va_mtime = ts[1];
2265 vattr.va_vaflags |= VA_UTIMES_NULL;
2266 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2267 VOP_UNLOCK(vp, 0, td);
2272 kern_utimes(struct nameidata *nd, struct timeval *tptr)
2274 struct timespec ts[2];
2277 error = getutimes(tptr, ts);
2283 NDFREE(nd, NDF_ONLY_PNBUF);
2284 error = setutimes(nd->ni_vp, ts, tptr == NULL);
2290 * utimes_args(char *path, struct timeval *tptr)
2292 * Set the access and modification times of a file.
2295 utimes(struct utimes_args *uap)
2297 struct thread *td = curthread;
2298 struct timeval tv[2];
2299 struct nameidata nd;
2303 error = copyin(uap->tptr, tv, sizeof(tv));
2307 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2309 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2315 * lutimes_args(char *path, struct timeval *tptr)
2317 * Set the access and modification times of a file.
2320 lutimes(struct lutimes_args *uap)
2322 struct thread *td = curthread;
2323 struct timeval tv[2];
2324 struct nameidata nd;
2328 error = copyin(uap->tptr, tv, sizeof(tv));
2332 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2334 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2340 kern_futimes(int fd, struct timeval *tptr)
2342 struct thread *td = curthread;
2343 struct proc *p = td->td_proc;
2344 struct timespec ts[2];
2348 error = getutimes(tptr, ts);
2351 error = getvnode(p->p_fd, fd, &fp);
2354 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2359 * futimes_args(int fd, struct timeval *tptr)
2361 * Set the access and modification times of a file.
2364 futimes(struct futimes_args *uap)
2366 struct timeval tv[2];
2370 error = copyin(uap->tptr, tv, sizeof(tv));
2375 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2381 kern_truncate(struct nameidata* nd, off_t length)
2383 struct thread *td = curthread;
2384 struct proc *p = td->td_proc;
2391 if ((error = namei(nd)) != 0)
2394 NDFREE(nd, NDF_ONLY_PNBUF);
2395 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2396 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2397 if (vp->v_type == VDIR)
2399 else if ((error = vn_writechk(vp)) == 0 &&
2400 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2402 vattr.va_size = length;
2403 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2410 * truncate(char *path, int pad, off_t length)
2412 * Truncate a file given its path name.
2415 truncate(struct truncate_args *uap)
2417 struct thread *td = curthread;
2418 struct nameidata nd;
2421 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2423 error = kern_truncate(&nd, uap->length);
2429 kern_ftruncate(int fd, off_t length)
2431 struct thread *td = curthread;
2432 struct proc *p = td->td_proc;
2440 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2442 if ((fp->f_flag & FWRITE) == 0)
2444 vp = (struct vnode *)fp->f_data;
2445 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2446 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2447 if (vp->v_type == VDIR)
2449 else if ((error = vn_writechk(vp)) == 0) {
2451 vattr.va_size = length;
2452 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2454 VOP_UNLOCK(vp, 0, td);
2459 * ftruncate_args(int fd, int pad, off_t length)
2461 * Truncate a file given a file descriptor.
2464 ftruncate(struct ftruncate_args *uap)
2468 error = kern_ftruncate(uap->fd, uap->length);
2476 * Sync an open file.
2480 fsync(struct fsync_args *uap)
2482 struct thread *td = curthread;
2483 struct proc *p = td->td_proc;
2489 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2491 vp = (struct vnode *)fp->f_data;
2492 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2493 if (VOP_GETVOBJECT(vp, &obj) == 0)
2494 vm_object_page_clean(obj, 0, 0, 0);
2495 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2496 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2498 error = (*bioops.io_fsync)(vp);
2499 VOP_UNLOCK(vp, 0, td);
2504 kern_rename(struct nameidata *fromnd, struct nameidata *tond)
2506 struct thread *td = curthread;
2507 struct proc *p = td->td_proc;
2508 struct vnode *tvp, *fvp, *tdvp;
2512 error = namei(fromnd);
2515 fvp = fromnd->ni_vp;
2516 if (fromnd->ni_vp->v_type == VDIR)
2517 tond->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2518 error = namei(tond);
2520 /* Translate error code for rename("dir1", "dir2/."). */
2521 if (error == EISDIR && fvp->v_type == VDIR)
2523 NDFREE(fromnd, NDF_ONLY_PNBUF);
2524 vrele(fromnd->ni_dvp);
2528 tdvp = tond->ni_dvp;
2531 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2534 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2542 * If the source is the same as the destination (that is, if they
2543 * are links to the same vnode), then there is nothing to do.
2549 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2550 if (fromnd->ni_dvp != tdvp) {
2551 VOP_LEASE(fromnd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2554 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2556 error = VOP_RENAME(fromnd->ni_dvp, NCPNULL, fromnd->ni_vp,
2557 &fromnd->ni_cnd, tond->ni_dvp, NCPNULL, tond->ni_vp,
2560 cache_purge(fromnd->ni_vp);
2561 NDFREE(fromnd, NDF_ONLY_PNBUF);
2562 NDFREE(tond, NDF_ONLY_PNBUF);
2564 NDFREE(fromnd, NDF_ONLY_PNBUF);
2565 NDFREE(tond, NDF_ONLY_PNBUF);
2572 vrele(fromnd->ni_dvp);
2575 vrele(tond->ni_startdir);
2576 ASSERT_VOP_UNLOCKED(fromnd->ni_dvp, "rename");
2577 ASSERT_VOP_UNLOCKED(fromnd->ni_vp, "rename");
2578 ASSERT_VOP_UNLOCKED(tond->ni_dvp, "rename");
2579 ASSERT_VOP_UNLOCKED(tond->ni_vp, "rename");
2581 if (fromnd->ni_startdir)
2582 vrele(fromnd->ni_startdir);
2589 * rename_args(char *from, char *to)
2591 * Rename files. Source and destination must either both be directories,
2592 * or both not be directories. If target is a directory, it must be empty.
2595 rename(struct rename_args *uap)
2597 struct thread *td = curthread;
2598 struct nameidata fromnd, tond;
2601 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2602 UIO_USERSPACE, uap->from, td);
2603 NDINIT(&tond, NAMEI_RENAME,
2604 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2605 CNP_SAVESTART | CNP_NOOBJ,
2606 UIO_USERSPACE, uap->to, td);
2608 error = kern_rename(&fromnd, &tond);
2614 kern_mkdir(struct nameidata *nd, int mode)
2616 struct thread *td = curthread;
2617 struct proc *p = td->td_proc;
2623 nd->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2629 NDFREE(nd, NDF_ONLY_PNBUF);
2630 if (nd->ni_dvp == vp)
2638 vattr.va_type = VDIR;
2639 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2640 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2641 error = VOP_MKDIR(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
2643 NDFREE(nd, NDF_ONLY_PNBUF);
2647 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mkdir");
2648 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mkdir");
2653 * mkdir_args(char *path, int mode)
2655 * Make a directory file.
2659 mkdir(struct mkdir_args *uap)
2661 struct thread *td = curthread;
2662 struct nameidata nd;
2665 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
2668 error = kern_mkdir(&nd, uap->mode);
2674 kern_rmdir(struct nameidata *nd)
2676 struct thread *td = curthread;
2677 struct proc *p = td->td_proc;
2686 if (vp->v_type != VDIR) {
2691 * No rmdir "." please.
2693 if (nd->ni_dvp == vp) {
2698 * The root of a mounted filesystem cannot be deleted.
2700 if (vp->v_flag & VROOT)
2703 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2704 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2705 error = VOP_RMDIR(nd->ni_dvp, NCPNULL, nd->ni_vp,
2711 NDFREE(nd, NDF_ONLY_PNBUF);
2712 if (nd->ni_dvp == vp)
2718 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "rmdir");
2719 ASSERT_VOP_UNLOCKED(nd->ni_vp, "rmdir");
2724 * rmdir_args(char *path)
2726 * Remove a directory file.
2730 rmdir(struct rmdir_args *uap)
2732 struct thread *td = curthread;
2733 struct nameidata nd;
2736 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF,
2737 UIO_USERSPACE, uap->path, td);
2739 error = kern_rmdir(&nd);
2745 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2747 struct thread *td = curthread;
2748 struct proc *p = td->td_proc;
2756 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2758 if ((fp->f_flag & FREAD) == 0)
2760 vp = (struct vnode *)fp->f_data;
2762 if (vp->v_type != VDIR)
2764 aiov.iov_base = buf;
2765 aiov.iov_len = count;
2766 auio.uio_iov = &aiov;
2767 auio.uio_iovcnt = 1;
2768 auio.uio_rw = UIO_READ;
2769 auio.uio_segflg = UIO_USERSPACE;
2771 auio.uio_resid = count;
2772 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2773 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2774 loff = auio.uio_offset = fp->f_offset;
2775 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2776 fp->f_offset = auio.uio_offset;
2777 VOP_UNLOCK(vp, 0, td);
2780 if (count == auio.uio_resid) {
2781 if (union_dircheckp) {
2782 error = union_dircheckp(td, &vp, fp);
2788 if ((vp->v_flag & VROOT) &&
2789 (vp->v_mount->mnt_flag & MNT_UNION)) {
2790 struct vnode *tvp = vp;
2791 vp = vp->v_mount->mnt_vnodecovered;
2793 fp->f_data = (caddr_t)vp;
2802 *res = count - auio.uio_resid;
2807 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2809 * Read a block of directory entries in a file system independent format.
2812 getdirentries(struct getdirentries_args *uap)
2817 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2818 &uap->sysmsg_result);
2821 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2826 * getdents_args(int fd, char *buf, size_t count)
2829 getdents(struct getdents_args *uap)
2833 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2834 &uap->sysmsg_result);
2840 * umask(int newmask)
2842 * Set the mode mask for creation of filesystem nodes.
2847 umask(struct umask_args *uap)
2849 struct thread *td = curthread;
2850 struct proc *p = td->td_proc;
2851 struct filedesc *fdp;
2854 uap->sysmsg_result = fdp->fd_cmask;
2855 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2860 * revoke(char *path)
2862 * Void all references to file by ripping underlying filesystem
2867 revoke(struct revoke_args *uap)
2869 struct thread *td = curthread;
2870 struct proc *p = td->td_proc;
2874 struct nameidata nd;
2876 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2877 if ((error = namei(&nd)) != 0)
2880 NDFREE(&nd, NDF_ONLY_PNBUF);
2881 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2885 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2887 if (p->p_ucred->cr_uid != vattr.va_uid &&
2888 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2890 if (count_udev(vp->v_udev) > 0) {
2891 if ((error = vx_lock(vp)) == 0) {
2892 VOP_REVOKE(vp, REVOKEALL);
2902 * Convert a user file descriptor to a kernel file entry.
2905 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2909 if ((u_int)fd >= fdp->fd_nfiles ||
2910 (fp = fdp->fd_ofiles[fd]) == NULL)
2912 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2918 * getfh_args(char *fname, fhandle_t *fhp)
2920 * Get (NFS) file handle
2923 getfh(struct getfh_args *uap)
2925 struct thread *td = curthread;
2926 struct nameidata nd;
2932 * Must be super user
2937 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2941 NDFREE(&nd, NDF_ONLY_PNBUF);
2943 bzero(&fh, sizeof(fh));
2944 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2945 error = VFS_VPTOFH(vp, &fh.fh_fid);
2949 error = copyout(&fh, uap->fhp, sizeof (fh));
2954 * fhopen_args(const struct fhandle *u_fhp, int flags)
2956 * syscall for the rpc.lockd to use to translate a NFS file handle into
2957 * an open descriptor.
2959 * warning: do not remove the suser() call or this becomes one giant
2963 fhopen(struct fhopen_args *uap)
2965 struct thread *td = curthread;
2966 struct proc *p = td->td_proc;
2971 struct vattr *vap = &vat;
2974 struct filedesc *fdp = p->p_fd;
2975 int fmode, mode, error, type;
2980 * Must be super user
2986 fmode = FFLAGS(SCARG(uap, flags));
2987 /* why not allow a non-read/write open for our lockd? */
2988 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2990 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2993 /* find the mount point */
2994 mp = vfs_getvfs(&fhp.fh_fsid);
2997 /* now give me my vnode, it gets returned to me locked */
2998 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3002 * from now on we have to make sure not
3003 * to forget about the vnode
3004 * any error that causes an abort must vput(vp)
3005 * just set error = err and 'goto bad;'.
3011 if (vp->v_type == VLNK) {
3015 if (vp->v_type == VSOCK) {
3020 if (fmode & (FWRITE | O_TRUNC)) {
3021 if (vp->v_type == VDIR) {
3025 error = vn_writechk(vp);
3033 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
3037 if (fmode & O_TRUNC) {
3038 VOP_UNLOCK(vp, 0, td); /* XXX */
3039 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
3040 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
3043 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
3047 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
3051 * Make sure that a VM object is created for VMIO support.
3053 if (vn_canvmio(vp) == TRUE) {
3054 if ((error = vfs_object_create(vp, td)) != 0)
3061 * end of vn_open code
3064 if ((error = falloc(p, &nfp, &indx)) != 0) {
3072 * hold an extra reference to avoid having fp ripped out
3073 * from under us while we block in the lock op.
3076 nfp->f_data = (caddr_t)vp;
3077 nfp->f_flag = fmode & FMASK;
3078 nfp->f_ops = &vnops;
3079 nfp->f_type = DTYPE_VNODE;
3080 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3081 lf.l_whence = SEEK_SET;
3084 if (fmode & O_EXLOCK)
3085 lf.l_type = F_WRLCK;
3087 lf.l_type = F_RDLCK;
3089 if ((fmode & FNONBLOCK) == 0)
3091 VOP_UNLOCK(vp, 0, td);
3092 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3094 * lock request failed. Normally close the descriptor
3095 * but handle the case where someone might have dup()d
3096 * or close()d it when we weren't looking.
3098 if (fdp->fd_ofiles[indx] == fp) {
3099 fdp->fd_ofiles[indx] = NULL;
3104 * release our private reference.
3109 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3110 fp->f_flag |= FHASLOCK;
3112 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
3113 vfs_object_create(vp, td);
3115 VOP_UNLOCK(vp, 0, td);
3117 uap->sysmsg_result = indx;
3126 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3129 fhstat(struct fhstat_args *uap)
3131 struct thread *td = curthread;
3139 * Must be super user
3145 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3149 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3151 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3153 error = vn_stat(vp, &sb, td);
3157 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3162 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3165 fhstatfs(struct fhstatfs_args *uap)
3167 struct thread *td = curthread;
3176 * Must be super user
3178 if ((error = suser(td)))
3181 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3184 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3186 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3191 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3193 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3195 bcopy(sp, &sb, sizeof(sb));
3196 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3199 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3203 * Syscall to push extended attribute configuration information into the
3204 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3205 * a command (int cmd), and attribute name and misc data. For now, the
3206 * attribute name is left in userspace for consumption by the VFS_op.
3207 * It will probably be changed to be copied into sysspace by the
3208 * syscall in the future, once issues with various consumers of the
3209 * attribute code have raised their hands.
3211 * Currently this is used only by UFS Extended Attributes.
3214 extattrctl(struct extattrctl_args *uap)
3216 struct thread *td = curthread;
3217 struct nameidata nd;
3221 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3222 if ((error = namei(&nd)) != 0)
3224 mp = nd.ni_vp->v_mount;
3226 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3227 SCARG(uap, arg), td));
3231 * Syscall to set a named extended attribute on a file or directory.
3232 * Accepts attribute name, and a uio structure pointing to the data to set.
3233 * The uio is consumed in the style of writev(). The real work happens
3234 * in VOP_SETEXTATTR().
3237 extattr_set_file(struct extattr_set_file_args *uap)
3239 struct thread *td = curthread;
3240 struct proc *p = td->td_proc;
3241 struct nameidata nd;
3243 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3244 char attrname[EXTATTR_MAXNAMELEN];
3248 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3251 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3252 SCARG(uap, path), td);
3253 if ((error = namei(&nd)) != 0)
3255 iovlen = uap->iovcnt * sizeof(struct iovec);
3256 if (uap->iovcnt > UIO_SMALLIOV) {
3257 if (uap->iovcnt > UIO_MAXIOV) {
3261 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3266 auio.uio_iovcnt = uap->iovcnt;
3267 auio.uio_rw = UIO_WRITE;
3268 auio.uio_segflg = UIO_USERSPACE;
3270 auio.uio_offset = 0;
3271 if ((error = copyin(uap->iovp, iov, iovlen)))
3274 for (i = 0; i < uap->iovcnt; i++) {
3275 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3279 auio.uio_resid += iov->iov_len;
3282 cnt = auio.uio_resid;
3283 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3284 cnt -= auio.uio_resid;
3285 uap->sysmsg_result = cnt;
3288 FREE(needfree, M_IOV);
3294 * Syscall to get a named extended attribute on a file or directory.
3295 * Accepts attribute name, and a uio structure pointing to a buffer for the
3296 * data. The uio is consumed in the style of readv(). The real work
3297 * happens in VOP_GETEXTATTR();
3300 extattr_get_file(struct extattr_get_file_args *uap)
3302 struct thread *td = curthread;
3303 struct proc *p = td->td_proc;
3304 struct nameidata nd;
3306 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3307 char attrname[EXTATTR_MAXNAMELEN];
3311 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3314 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3315 SCARG(uap, path), td);
3316 if ((error = namei(&nd)) != 0)
3318 iovlen = uap->iovcnt * sizeof (struct iovec);
3319 if (uap->iovcnt > UIO_SMALLIOV) {
3320 if (uap->iovcnt > UIO_MAXIOV) {
3324 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3331 auio.uio_iovcnt = uap->iovcnt;
3332 auio.uio_rw = UIO_READ;
3333 auio.uio_segflg = UIO_USERSPACE;
3335 auio.uio_offset = 0;
3336 if ((error = copyin(uap->iovp, iov, iovlen)))
3339 for (i = 0; i < uap->iovcnt; i++) {
3340 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3344 auio.uio_resid += iov->iov_len;
3347 cnt = auio.uio_resid;
3348 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3349 cnt -= auio.uio_resid;
3350 uap->sysmsg_result = cnt;
3353 FREE(needfree, M_IOV);
3359 * Syscall to delete a named extended attribute from a file or directory.
3360 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3363 extattr_delete_file(struct extattr_delete_file_args *uap)
3365 struct thread *td = curthread;
3366 struct proc *p = td->td_proc;
3367 struct nameidata nd;
3368 char attrname[EXTATTR_MAXNAMELEN];
3371 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3374 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3375 SCARG(uap, path), td);
3376 if ((error = namei(&nd)) != 0)
3378 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);
3384 * print out statistics from the current status of the buffer pool
3385 * this can be toggeled by the system control option debug.syncprt
3394 int counts[(MAXBSIZE / PAGE_SIZE) + 1];
3395 static char *bname[3] = { "LOCKED", "LRU", "AGE" };
3397 for (dp = bufqueues, i = 0; dp < &bufqueues[3]; dp++, i++) {
3399 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3402 TAILQ_FOREACH(bp, dp, b_freelist) {
3403 counts[bp->b_bufsize/PAGE_SIZE]++;
3407 printf("%s: total-%d", bname[i], count);
3408 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3410 printf(", %d-%d", j * PAGE_SIZE, counts[j]);