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.110 2006/12/23 00:35:04 swildner 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/mountctl.h>
51 #include <sys/sysproto.h>
52 #include <sys/filedesc.h>
53 #include <sys/kernel.h>
54 #include <sys/fcntl.h>
56 #include <sys/linker.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
61 #include <sys/namei.h>
62 #include <sys/nlookup.h>
63 #include <sys/dirent.h>
64 #include <sys/extattr.h>
65 #include <sys/spinlock.h>
66 #include <sys/kern_syscall.h>
67 #include <sys/objcache.h>
68 #include <sys/sysctl.h>
69 #include <sys/file2.h>
70 #include <sys/spinlock2.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
76 #include <machine/limits.h>
77 #include <machine/stdarg.h>
79 #include <vfs/union/union.h>
81 static void mount_warning(struct mount *mp, const char *ctl, ...);
82 static int checkvp_chdir (struct vnode *vn, struct thread *td);
83 static void checkdirs (struct vnode *olddp, struct nchandle *nch);
84 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
85 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
86 static int getutimes (const struct timeval *, struct timespec *);
87 static int setfown (struct vnode *, uid_t, gid_t);
88 static int setfmode (struct vnode *, int);
89 static int setfflags (struct vnode *, int);
90 static int setutimes (struct vnode *, const struct timespec *, int);
91 static int usermount = 0; /* if 1, non-root can mount fs. */
93 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
95 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
98 * Virtual File System System Calls
102 * Mount a file system.
105 * mount_args(char *type, char *path, int flags, caddr_t data)
109 sys_mount(struct mount_args *uap)
111 struct thread *td = curthread;
112 struct proc *p = td->td_proc;
116 struct vfsconf *vfsp;
117 int error, flag = 0, flag2 = 0;
120 struct nlookupdata nd;
121 char fstypename[MFSNAMELEN];
122 struct ucred *cred = p->p_ucred;
125 if (cred->cr_prison != NULL)
127 if (usermount == 0 && (error = suser(td)))
130 * Do not allow NFS export by non-root users.
132 if (uap->flags & MNT_EXPORTED) {
138 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
141 uap->flags |= MNT_NOSUID | MNT_NODEV;
144 * Lookup the requested path and extract the nch and vnode.
146 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
148 if ((error = nlookup(&nd)) == 0) {
149 if (nd.nl_nch.ncp->nc_vp == NULL)
159 * Extract the locked+refd ncp and cleanup the nd structure
162 cache_zero(&nd.nl_nch);
165 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
172 * now we have the locked ref'd nch and unreferenced vnode.
175 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
182 * Now we have an unlocked ref'd nch and a locked ref'd vp
184 if (uap->flags & MNT_UPDATE) {
185 if ((vp->v_flag & VROOT) == 0) {
192 flag2 = mp->mnt_kern_flag;
194 * We only allow the filesystem to be reloaded if it
195 * is currently mounted read-only.
197 if ((uap->flags & MNT_RELOAD) &&
198 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
201 return (EOPNOTSUPP); /* Needs translation */
204 * Only root, or the user that did the original mount is
205 * permitted to update it.
207 if (mp->mnt_stat.f_owner != cred->cr_uid &&
208 (error = suser(td))) {
213 if (vfs_busy(mp, LK_NOWAIT)) {
218 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
224 vp->v_flag |= VMOUNT;
226 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
231 * If the user is not root, ensure that they own the directory
232 * onto which we are attempting to mount.
234 if ((error = VOP_GETATTR(vp, &va)) ||
235 (va.va_uid != cred->cr_uid && (error = suser(td)))) {
240 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
245 if (vp->v_type != VDIR) {
250 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
255 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
256 if (!strcmp(vfsp->vfc_name, fstypename))
262 /* Only load modules for root (very important!) */
263 if ((error = suser(td)) != 0) {
268 error = linker_load_file(fstypename, &lf);
269 if (error || lf == NULL) {
277 /* lookup again, see if the VFS was loaded */
278 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
279 if (!strcmp(vfsp->vfc_name, fstypename))
284 linker_file_unload(lf);
290 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
295 vp->v_flag |= VMOUNT;
298 * Allocate and initialize the filesystem.
300 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
301 TAILQ_INIT(&mp->mnt_nvnodelist);
302 TAILQ_INIT(&mp->mnt_reservedvnlist);
303 TAILQ_INIT(&mp->mnt_jlist);
304 mp->mnt_nvnodelistsize = 0;
305 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
306 vfs_busy(mp, LK_NOWAIT);
307 mp->mnt_op = vfsp->vfc_vfsops;
309 vfsp->vfc_refcount++;
310 mp->mnt_stat.f_type = vfsp->vfc_typenum;
311 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
312 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
313 mp->mnt_stat.f_owner = cred->cr_uid;
314 mp->mnt_iosize_max = DFLTPHYS;
318 * Set the mount level flags.
320 if (uap->flags & MNT_RDONLY)
321 mp->mnt_flag |= MNT_RDONLY;
322 else if (mp->mnt_flag & MNT_RDONLY)
323 mp->mnt_kern_flag |= MNTK_WANTRDWR;
324 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
325 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
326 MNT_NOSYMFOLLOW | MNT_IGNORE |
327 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
328 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
329 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
330 MNT_NOSYMFOLLOW | MNT_IGNORE |
331 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
333 * Mount the filesystem.
334 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
337 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
338 if (mp->mnt_flag & MNT_UPDATE) {
339 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
340 mp->mnt_flag &= ~MNT_RDONLY;
341 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
342 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
345 mp->mnt_kern_flag = flag2;
348 vp->v_flag &= ~VMOUNT;
353 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
355 * Put the new filesystem on the mount list after root. The mount
356 * point gets its own mnt_ncmountpt (unless the VFS already set one
357 * up) which represents the root of the mount. The lookup code
358 * detects the mount point going forward and checks the root of
359 * the mount going backwards.
361 * It is not necessary to invalidate or purge the vnode underneath
362 * because elements under the mount will be given their own glue
366 if (mp->mnt_ncmountpt.ncp == NULL) {
368 * allocate, then unlock, but leave the ref intact
370 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
371 cache_unlock(&mp->mnt_ncmountpt);
373 mp->mnt_ncmounton = nch; /* inherits ref */
374 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
376 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
377 vp->v_flag &= ~VMOUNT;
378 mountlist_insert(mp, MNTINS_LAST);
379 checkdirs(vp, &mp->mnt_ncmounton);
381 error = vfs_allocate_syncvnode(mp);
383 error = VFS_START(mp, 0);
386 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
387 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
388 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
389 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
390 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
391 vp->v_flag &= ~VMOUNT;
392 mp->mnt_vfc->vfc_refcount--;
402 * Scan all active processes to see if any of them have a current
403 * or root directory onto which the new filesystem has just been
404 * mounted. If so, replace them with the new mount point.
406 * The passed ncp is ref'd and locked (from the mount code) and
407 * must be associated with the vnode representing the root of the
410 struct checkdirs_info {
416 static int checkdirs_callback(struct proc *p, void *data);
419 checkdirs(struct vnode *olddp, struct nchandle *nch)
421 struct checkdirs_info info;
425 if (olddp->v_usecount == 1)
428 if (VFS_ROOT(mp, &newdp))
429 panic("mount: lost mount");
430 cache_setunresolved(nch);
431 cache_setvp(nch, newdp);
433 if (rootvnode == olddp) {
435 vfs_cache_setroot(newdp, cache_hold(nch));
441 allproc_scan(checkdirs_callback, &info);
446 * NOTE: callback is not MP safe because the scanned process's filedesc
447 * structure can be ripped out from under us, amoung other things.
450 checkdirs_callback(struct proc *p, void *data)
452 struct checkdirs_info *info = data;
453 struct filedesc *fdp;
454 struct nchandle ncdrop1;
455 struct nchandle ncdrop2;
456 struct vnode *vprele1;
457 struct vnode *vprele2;
459 if ((fdp = p->p_fd) != NULL) {
460 cache_zero(&ncdrop1);
461 cache_zero(&ncdrop2);
466 * MPUNSAFE - XXX fdp can be pulled out from under a
469 * A shared filedesc is ok, we don't have to copy it
470 * because we are making this change globally.
472 spin_lock_wr(&fdp->fd_spin);
473 if (fdp->fd_cdir == info->olddp) {
474 vprele1 = fdp->fd_cdir;
476 fdp->fd_cdir = info->newdp;
477 ncdrop1 = fdp->fd_ncdir;
478 cache_copy(&info->nch, &fdp->fd_ncdir);
480 if (fdp->fd_rdir == info->olddp) {
481 vprele2 = fdp->fd_rdir;
483 fdp->fd_rdir = info->newdp;
484 ncdrop2 = fdp->fd_nrdir;
485 cache_copy(&info->nch, &fdp->fd_nrdir);
487 spin_unlock_wr(&fdp->fd_spin);
489 cache_drop(&ncdrop1);
491 cache_drop(&ncdrop2);
501 * Unmount a file system.
503 * Note: unmount takes a path to the vnode mounted on as argument,
504 * not special file (as before).
507 * umount_args(char *path, int flags)
511 sys_unmount(struct unmount_args *uap)
513 struct thread *td = curthread;
514 struct proc *p = td->td_proc;
515 struct mount *mp = NULL;
517 struct nlookupdata nd;
520 if (p->p_ucred->cr_prison != NULL)
522 if (usermount == 0 && (error = suser(td)))
525 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
527 error = nlookup(&nd);
531 mp = nd.nl_nch.mount;
534 * Only root, or the user that did the original mount is
535 * permitted to unmount this filesystem.
537 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
542 * Don't allow unmounting the root file system.
544 if (mp->mnt_flag & MNT_ROOTFS) {
550 * Must be the root of the filesystem
552 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
561 return (dounmount(mp, uap->flags));
565 * Do the actual file system unmount.
568 dounmount_interlock(struct mount *mp)
570 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
572 mp->mnt_kern_flag |= MNTK_UNMOUNT;
577 dounmount(struct mount *mp, int flags)
579 struct namecache *ncp;
587 * Exclusive access for unmounting purposes
589 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
593 * Allow filesystems to detect that a forced unmount is in progress.
595 if (flags & MNT_FORCE)
596 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
597 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
598 error = lockmgr(&mp->mnt_lock, lflags);
600 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
601 if (mp->mnt_kern_flag & MNTK_MWAIT)
606 if (mp->mnt_flag & MNT_EXPUBLIC)
607 vfs_setpublicfs(NULL, NULL, NULL);
609 vfs_msync(mp, MNT_WAIT);
610 async_flag = mp->mnt_flag & MNT_ASYNC;
611 mp->mnt_flag &=~ MNT_ASYNC;
614 * If this filesystem isn't aliasing other filesystems,
615 * try to invalidate any remaining namecache entries and
616 * check the count afterwords.
618 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
619 cache_lock(&mp->mnt_ncmountpt);
620 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
621 cache_unlock(&mp->mnt_ncmountpt);
623 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
624 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
626 if ((flags & MNT_FORCE) == 0) {
628 mount_warning(mp, "Cannot unmount: "
634 mount_warning(mp, "Forced unmount: "
645 * nchandle records ref the mount structure. Expect a count of 1
646 * (our mount->mnt_ncmountpt).
648 if (mp->mnt_refs != 1) {
649 if ((flags & MNT_FORCE) == 0) {
650 mount_warning(mp, "Cannot unmount: "
651 "%d process references still "
652 "present", mp->mnt_refs);
655 mount_warning(mp, "Forced unmount: "
656 "%d process references still "
657 "present", mp->mnt_refs);
663 if (mp->mnt_syncer != NULL)
664 vrele(mp->mnt_syncer);
665 if (((mp->mnt_flag & MNT_RDONLY) ||
666 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
667 (flags & MNT_FORCE)) {
668 error = VFS_UNMOUNT(mp, flags);
672 if (mp->mnt_syncer == NULL)
673 vfs_allocate_syncvnode(mp);
674 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
675 mp->mnt_flag |= async_flag;
676 lockmgr(&mp->mnt_lock, LK_RELEASE);
677 if (mp->mnt_kern_flag & MNTK_MWAIT)
682 * Clean up any journals still associated with the mount after
683 * filesystem activity has ceased.
685 journal_remove_all_journals(mp,
686 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
688 mountlist_remove(mp);
691 * Remove any installed vnode ops here so the individual VFSs don't
694 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
695 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
696 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
697 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
698 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
700 if (mp->mnt_ncmountpt.ncp != NULL) {
701 nch = mp->mnt_ncmountpt;
702 cache_zero(&mp->mnt_ncmountpt);
703 cache_clrmountpt(&nch);
706 if (mp->mnt_ncmounton.ncp != NULL) {
707 nch = mp->mnt_ncmounton;
708 cache_zero(&mp->mnt_ncmounton);
709 cache_clrmountpt(&nch);
713 mp->mnt_vfc->vfc_refcount--;
714 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
715 panic("unmount: dangling vnode");
716 lockmgr(&mp->mnt_lock, LK_RELEASE);
717 if (mp->mnt_kern_flag & MNTK_MWAIT)
726 mount_warning(struct mount *mp, const char *ctl, ...)
733 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
734 kprintf("unmount(%s): ", ptr);
739 kprintf("unmount(%p): ", mp);
747 * Sync each mounted filesystem.
751 static int syncprt = 0;
752 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
755 static int sync_callback(struct mount *mp, void *data);
759 sys_sync(struct sync_args *uap)
761 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
764 * print out buffer pool stat information on each sync() call.
774 sync_callback(struct mount *mp, void *data __unused)
778 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
779 asyncflag = mp->mnt_flag & MNT_ASYNC;
780 mp->mnt_flag &= ~MNT_ASYNC;
781 vfs_msync(mp, MNT_NOWAIT);
782 VFS_SYNC(mp, MNT_NOWAIT);
783 mp->mnt_flag |= asyncflag;
788 /* XXX PRISON: could be per prison flag */
789 static int prison_quotas;
791 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
795 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
797 * Change filesystem quotas.
801 sys_quotactl(struct quotactl_args *uap)
803 struct nlookupdata nd;
811 if (p->p_ucred->cr_prison && !prison_quotas)
814 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
816 error = nlookup(&nd);
818 mp = nd.nl_nch.mount;
819 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
820 uap->arg, nd.nl_cred);
827 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
828 * void *buf, int buflen)
830 * This function operates on a mount point and executes the specified
831 * operation using the specified control data, and possibly returns data.
833 * The actual number of bytes stored in the result buffer is returned, 0
834 * if none, otherwise an error is returned.
838 sys_mountctl(struct mountctl_args *uap)
840 struct thread *td = curthread;
841 struct proc *p = td->td_proc;
849 * Sanity and permissions checks. We must be root.
852 if (p->p_ucred->cr_prison != NULL)
854 if ((error = suser(td)) != 0)
858 * Argument length checks
860 if (uap->ctllen < 0 || uap->ctllen > 1024)
862 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
864 if (uap->path == NULL)
868 * Allocate the necessary buffers and copyin data
870 path = objcache_get(namei_oc, M_WAITOK);
871 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
876 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
877 error = copyin(uap->ctl, ctl, uap->ctllen);
882 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
885 * Validate the descriptor
887 fp = holdfp(p->p_fd, uap->fd, -1);
894 * Execute the internal kernel function and clean up.
896 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
899 if (error == 0 && uap->sysmsg_result > 0)
900 error = copyout(buf, uap->buf, uap->sysmsg_result);
903 objcache_put(namei_oc, path);
912 * Execute a mount control operation by resolving the path to a mount point
913 * and calling vop_mountctl().
916 kern_mountctl(const char *path, int op, struct file *fp,
917 const void *ctl, int ctllen,
918 void *buf, int buflen, int *res)
922 struct nlookupdata nd;
927 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
929 error = nlookup(&nd);
931 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
939 * Must be the root of the filesystem
941 if ((vp->v_flag & VROOT) == 0) {
945 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
952 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
954 struct thread *td = curthread;
955 struct proc *p = td->td_proc;
958 char *fullpath, *freepath;
961 if ((error = nlookup(nd)) != 0)
963 mp = nd->nl_nch.mount;
965 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
968 error = cache_fullpath(p, &mp->mnt_ncmountpt, &fullpath, &freepath);
971 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
972 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
973 kfree(freepath, M_TEMP);
975 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
976 bcopy(sp, buf, sizeof(*buf));
977 /* Only root should have access to the fsid's. */
979 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
984 * statfs_args(char *path, struct statfs *buf)
986 * Get filesystem statistics.
989 sys_statfs(struct statfs_args *uap)
991 struct nlookupdata nd;
995 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
997 error = kern_statfs(&nd, &buf);
1000 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1005 kern_fstatfs(int fd, struct statfs *buf)
1007 struct thread *td = curthread;
1008 struct proc *p = td->td_proc;
1012 char *fullpath, *freepath;
1016 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1018 mp = ((struct vnode *)fp->f_data)->v_mount;
1023 if (fp->f_cred == NULL) {
1028 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1031 if ((error = cache_fullpath(p, &mp->mnt_ncmountpt, &fullpath, &freepath)) != 0)
1033 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1034 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1035 kfree(freepath, M_TEMP);
1037 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1038 bcopy(sp, buf, sizeof(*buf));
1040 /* Only root should have access to the fsid's. */
1042 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1050 * fstatfs_args(int fd, struct statfs *buf)
1052 * Get filesystem statistics.
1055 sys_fstatfs(struct fstatfs_args *uap)
1060 error = kern_fstatfs(uap->fd, &buf);
1063 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1068 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1070 * Get statistics on all filesystems.
1073 struct getfsstat_info {
1074 struct statfs *sfsp;
1082 static int getfsstat_callback(struct mount *, void *);
1086 sys_getfsstat(struct getfsstat_args *uap)
1088 struct thread *td = curthread;
1089 struct proc *p = td->td_proc;
1090 struct getfsstat_info info;
1092 bzero(&info, sizeof(info));
1094 info.maxcount = uap->bufsize / sizeof(struct statfs);
1095 info.sfsp = uap->buf;
1097 info.flags = uap->flags;
1100 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1101 if (info.sfsp && info.count > info.maxcount)
1102 uap->sysmsg_result = info.maxcount;
1104 uap->sysmsg_result = info.count;
1105 return (info.error);
1109 getfsstat_callback(struct mount *mp, void *data)
1111 struct getfsstat_info *info = data;
1117 if (info->sfsp && info->count < info->maxcount) {
1118 if (info->p && !chroot_visible_mnt(mp, info->p))
1123 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1124 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1125 * overrides MNT_WAIT.
1127 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1128 (info->flags & MNT_WAIT)) &&
1129 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1132 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1134 error = cache_fullpath(info->p, &mp->mnt_ncmountpt,
1135 &fullpath, &freepath);
1137 info->error = error;
1140 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1141 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1142 kfree(freepath, M_TEMP);
1144 error = copyout(sp, info->sfsp, sizeof(*sp));
1146 info->error = error;
1156 * fchdir_args(int fd)
1158 * Change current working directory to a given file descriptor.
1162 sys_fchdir(struct fchdir_args *uap)
1164 struct thread *td = curthread;
1165 struct proc *p = td->td_proc;
1166 struct filedesc *fdp = p->p_fd;
1167 struct vnode *vp, *ovp;
1170 struct nchandle nch, onch, tnch;
1173 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1175 vp = (struct vnode *)fp->f_data;
1177 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1178 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1181 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1187 cache_copy(&fp->f_nchandle, &nch);
1190 * If the ncp has become a mount point, traverse through
1194 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1195 (mp = cache_findmount(&nch)) != NULL
1197 error = nlookup_mp(mp, &tnch);
1199 cache_unlock(&tnch); /* leave ref intact */
1201 vp = tnch.ncp->nc_vp;
1202 error = vget(vp, LK_SHARED);
1203 KKASSERT(error == 0);
1210 onch = fdp->fd_ncdir;
1211 vn_unlock(vp); /* leave ref intact */
1213 fdp->fd_ncdir = nch;
1225 kern_chdir(struct nlookupdata *nd)
1227 struct thread *td = curthread;
1228 struct proc *p = td->td_proc;
1229 struct filedesc *fdp = p->p_fd;
1230 struct vnode *vp, *ovp;
1231 struct nchandle onch;
1234 if ((error = nlookup(nd)) != 0)
1236 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1238 if ((error = vget(vp, LK_SHARED)) != 0)
1241 error = checkvp_chdir(vp, td);
1245 onch = fdp->fd_ncdir;
1246 cache_unlock(&nd->nl_nch); /* leave reference intact */
1247 fdp->fd_ncdir = nd->nl_nch;
1251 cache_zero(&nd->nl_nch);
1259 * chdir_args(char *path)
1261 * Change current working directory (``.'').
1264 sys_chdir(struct chdir_args *uap)
1266 struct nlookupdata nd;
1269 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1271 error = kern_chdir(&nd);
1277 * Helper function for raised chroot(2) security function: Refuse if
1278 * any filedescriptors are open directories.
1281 chroot_refuse_vdir_fds(fdp)
1282 struct filedesc *fdp;
1289 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1290 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1292 vp = (struct vnode *)fp->f_data;
1293 if (vp->v_type != VDIR) {
1304 * This sysctl determines if we will allow a process to chroot(2) if it
1305 * has a directory open:
1306 * 0: disallowed for all processes.
1307 * 1: allowed for processes that were not already chroot(2)'ed.
1308 * 2: allowed for all processes.
1311 static int chroot_allow_open_directories = 1;
1313 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1314 &chroot_allow_open_directories, 0, "");
1317 * chroot to the specified namecache entry. We obtain the vp from the
1318 * namecache data. The passed ncp must be locked and referenced and will
1319 * remain locked and referenced on return.
1322 kern_chroot(struct nchandle *nch)
1324 struct thread *td = curthread;
1325 struct proc *p = td->td_proc;
1326 struct filedesc *fdp = p->p_fd;
1331 * Only root can chroot
1333 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1337 * Disallow open directory descriptors (fchdir() breakouts).
1339 if (chroot_allow_open_directories == 0 ||
1340 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1341 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1344 if ((vp = nch->ncp->nc_vp) == NULL)
1347 if ((error = vget(vp, LK_SHARED)) != 0)
1351 * Check the validity of vp as a directory to change to and
1352 * associate it with rdir/jdir.
1354 error = checkvp_chdir(vp, td);
1355 vn_unlock(vp); /* leave reference intact */
1357 vrele(fdp->fd_rdir);
1358 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1359 cache_drop(&fdp->fd_nrdir);
1360 cache_copy(nch, &fdp->fd_nrdir);
1361 if (fdp->fd_jdir == NULL) {
1364 cache_copy(nch, &fdp->fd_njdir);
1373 * chroot_args(char *path)
1375 * Change notion of root (``/'') directory.
1379 sys_chroot(struct chroot_args *uap)
1381 struct thread *td = curthread;
1382 struct nlookupdata nd;
1385 KKASSERT(td->td_proc);
1386 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1391 error = nlookup(&nd);
1393 error = kern_chroot(&nd.nl_nch);
1399 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1400 * determine whether it is legal to chdir to the vnode. The vnode's state
1401 * is not changed by this call.
1404 checkvp_chdir(struct vnode *vp, struct thread *td)
1408 if (vp->v_type != VDIR)
1411 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1416 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1418 struct thread *td = curthread;
1419 struct proc *p = td->td_proc;
1420 struct lwp *lp = td->td_lwp;
1421 struct filedesc *fdp = p->p_fd;
1426 int type, indx, error;
1429 if ((oflags & O_ACCMODE) == O_ACCMODE)
1431 flags = FFLAGS(oflags);
1432 error = falloc(p, &nfp, NULL);
1436 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1439 * XXX p_dupfd is a real mess. It allows a device to return a
1440 * file descriptor to be duplicated rather then doing the open
1446 * Call vn_open() to do the lookup and assign the vnode to the
1447 * file pointer. vn_open() does not change the ref count on fp
1448 * and the vnode, on success, will be inherited by the file pointer
1451 nd->nl_flags |= NLC_LOCKVP;
1452 error = vn_open(nd, fp, flags, cmode);
1456 * handle special fdopen() case. bleh. dupfdopen() is
1457 * responsible for dropping the old contents of ofiles[indx]
1460 * Note that fsetfd() will add a ref to fp which represents
1461 * the fd_files[] assignment. We must still drop our
1464 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1465 if (fdalloc(p, 0, &indx) == 0) {
1466 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1469 fdrop(fp); /* our ref */
1472 fsetfd(p, NULL, indx);
1475 fdrop(fp); /* our ref */
1476 if (error == ERESTART)
1482 * ref the vnode for ourselves so it can't be ripped out from under
1483 * is. XXX need an ND flag to request that the vnode be returned
1486 * Reserve a file descriptor but do not assign it until the open
1489 vp = (struct vnode *)fp->f_data;
1491 if ((error = fdalloc(p, 0, &indx)) != 0) {
1498 * If no error occurs the vp will have been assigned to the file
1503 if (flags & (O_EXLOCK | O_SHLOCK)) {
1504 lf.l_whence = SEEK_SET;
1507 if (flags & O_EXLOCK)
1508 lf.l_type = F_WRLCK;
1510 lf.l_type = F_RDLCK;
1511 if (flags & FNONBLOCK)
1516 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1518 * lock request failed. Clean up the reserved
1522 fsetfd(p, NULL, indx);
1526 fp->f_flag |= FHASLOCK;
1530 * Assert that all regular file vnodes were created with a object.
1532 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1533 ("open: regular file has no backing object after vn_open"));
1539 * release our private reference, leaving the one associated with the
1540 * descriptor table intact.
1542 fsetfd(p, fp, indx);
1549 * open_args(char *path, int flags, int mode)
1551 * Check permissions, allocate an open file structure,
1552 * and call the device open routine if any.
1555 sys_open(struct open_args *uap)
1557 struct nlookupdata nd;
1560 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1562 error = kern_open(&nd, uap->flags,
1563 uap->mode, &uap->sysmsg_result);
1570 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1572 struct thread *td = curthread;
1573 struct proc *p = td->td_proc;
1581 switch (mode & S_IFMT) {
1587 error = suser_cred(p->p_ucred, PRISON_ROOT);
1594 nd->nl_flags |= NLC_CREATE;
1595 if ((error = nlookup(nd)) != 0)
1597 if (nd->nl_nch.ncp->nc_vp)
1599 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1603 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1604 vattr.va_rdev = dev;
1607 switch (mode & S_IFMT) {
1608 case S_IFMT: /* used by badsect to flag bad sectors */
1609 vattr.va_type = VBAD;
1612 vattr.va_type = VCHR;
1615 vattr.va_type = VBLK;
1626 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_cred, NAMEI_CREATE);
1629 error = VOP_NMKNOD(&nd->nl_nch, &vp, nd->nl_cred, &vattr);
1638 * mknod_args(char *path, int mode, int dev)
1640 * Create a special file.
1643 sys_mknod(struct mknod_args *uap)
1645 struct nlookupdata nd;
1648 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1650 error = kern_mknod(&nd, uap->mode, uap->dev);
1656 kern_mkfifo(struct nlookupdata *nd, int mode)
1658 struct thread *td = curthread;
1659 struct proc *p = td->td_proc;
1666 nd->nl_flags |= NLC_CREATE;
1667 if ((error = nlookup(nd)) != 0)
1669 if (nd->nl_nch.ncp->nc_vp)
1671 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1675 vattr.va_type = VFIFO;
1676 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1678 error = VOP_NMKNOD(&nd->nl_nch, &vp, nd->nl_cred, &vattr);
1685 * mkfifo_args(char *path, int mode)
1687 * Create a named pipe.
1690 sys_mkfifo(struct mkfifo_args *uap)
1692 struct nlookupdata nd;
1695 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1697 error = kern_mkfifo(&nd, uap->mode);
1702 static int hardlink_check_uid = 0;
1703 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1704 &hardlink_check_uid, 0,
1705 "Unprivileged processes cannot create hard links to files owned by other "
1707 static int hardlink_check_gid = 0;
1708 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1709 &hardlink_check_gid, 0,
1710 "Unprivileged processes cannot create hard links to files owned by other "
1714 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1720 * Shortcut if disabled
1722 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1726 * root cred can always hardlink
1728 if (suser_cred(cred, PRISON_ROOT) == 0)
1732 * Otherwise only if the originating file is owned by the
1733 * same user or group. Note that any group is allowed if
1734 * the file is owned by the caller.
1736 error = VOP_GETATTR(vp, &va);
1740 if (hardlink_check_uid) {
1741 if (cred->cr_uid != va.va_uid)
1745 if (hardlink_check_gid) {
1746 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1754 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1756 struct thread *td = curthread;
1761 * Lookup the source and obtained a locked vnode.
1763 * XXX relookup on vget failure / race ?
1766 if ((error = nlookup(nd)) != 0)
1768 vp = nd->nl_nch.ncp->nc_vp;
1769 KKASSERT(vp != NULL);
1770 if (vp->v_type == VDIR)
1771 return (EPERM); /* POSIX */
1772 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1774 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1778 * Unlock the source so we can lookup the target without deadlocking
1779 * (XXX vp is locked already, possible other deadlock?). The target
1782 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1783 nd->nl_flags &= ~NLC_NCPISLOCKED;
1784 cache_unlock(&nd->nl_nch);
1786 linknd->nl_flags |= NLC_CREATE;
1787 if ((error = nlookup(linknd)) != 0) {
1791 if (linknd->nl_nch.ncp->nc_vp) {
1797 * Finally run the new API VOP.
1799 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1801 error = VOP_NLINK(&linknd->nl_nch, vp, linknd->nl_cred);
1807 * link_args(char *path, char *link)
1809 * Make a hard file link.
1812 sys_link(struct link_args *uap)
1814 struct nlookupdata nd, linknd;
1817 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1819 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1821 error = kern_link(&nd, &linknd);
1822 nlookup_done(&linknd);
1829 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1836 nd->nl_flags |= NLC_CREATE;
1837 if ((error = nlookup(nd)) != 0)
1839 if (nd->nl_nch.ncp->nc_vp)
1841 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1844 vattr.va_mode = mode;
1845 error = VOP_NSYMLINK(&nd->nl_nch, &vp, nd->nl_cred, &vattr, path);
1852 * symlink(char *path, char *link)
1854 * Make a symbolic link.
1857 sys_symlink(struct symlink_args *uap)
1859 struct thread *td = curthread;
1860 struct nlookupdata nd;
1865 path = objcache_get(namei_oc, M_WAITOK);
1866 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1868 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1870 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1871 error = kern_symlink(&nd, path, mode);
1875 objcache_put(namei_oc, path);
1880 * undelete_args(char *path)
1882 * Delete a whiteout from the filesystem.
1886 sys_undelete(struct undelete_args *uap)
1888 struct nlookupdata nd;
1891 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1893 nd.nl_flags |= NLC_DELETE;
1895 error = nlookup(&nd);
1897 error = ncp_writechk(&nd.nl_nch);
1899 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_cred, NAMEI_DELETE);
1905 kern_unlink(struct nlookupdata *nd)
1910 nd->nl_flags |= NLC_DELETE;
1911 if ((error = nlookup(nd)) != 0)
1913 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1915 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_cred);
1920 * unlink_args(char *path)
1922 * Delete a name from the filesystem.
1925 sys_unlink(struct unlink_args *uap)
1927 struct nlookupdata nd;
1930 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1932 error = kern_unlink(&nd);
1938 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1940 struct thread *td = curthread;
1941 struct proc *p = td->td_proc;
1946 fp = holdfp(p->p_fd, fd, -1);
1949 if (fp->f_type != DTYPE_VNODE) {
1956 fp->f_offset += offset;
1960 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
1962 fp->f_offset = offset + vattr.va_size;
1965 fp->f_offset = offset;
1972 *res = fp->f_offset;
1979 * lseek_args(int fd, int pad, off_t offset, int whence)
1981 * Reposition read/write file offset.
1984 sys_lseek(struct lseek_args *uap)
1988 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1989 &uap->sysmsg_offset);
1995 kern_access(struct nlookupdata *nd, int aflags)
2000 if ((error = nlookup(nd)) != 0)
2003 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2007 /* Flags == 0 means only check for existence. */
2016 if ((flags & VWRITE) == 0 ||
2017 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2018 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2021 * If the file handle is stale we have to re-resolve the
2022 * entry. This is a hack at the moment.
2024 if (error == ESTALE) {
2025 cache_setunresolved(&nd->nl_nch);
2026 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2039 * access_args(char *path, int flags)
2041 * Check access permissions.
2044 sys_access(struct access_args *uap)
2046 struct nlookupdata nd;
2049 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2051 error = kern_access(&nd, uap->flags);
2057 kern_stat(struct nlookupdata *nd, struct stat *st)
2063 if ((error = nlookup(nd)) != 0)
2066 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2070 if ((error = vget(vp, LK_SHARED)) != 0)
2072 error = vn_stat(vp, st, nd->nl_cred);
2075 * If the file handle is stale we have to re-resolve the entry. This
2076 * is a hack at the moment.
2078 if (error == ESTALE) {
2079 cache_setunresolved(&nd->nl_nch);
2080 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2091 * stat_args(char *path, struct stat *ub)
2093 * Get file status; this version follows links.
2096 sys_stat(struct stat_args *uap)
2098 struct nlookupdata nd;
2102 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2104 error = kern_stat(&nd, &st);
2106 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2113 * lstat_args(char *path, struct stat *ub)
2115 * Get file status; this version does not follow links.
2118 sys_lstat(struct lstat_args *uap)
2120 struct nlookupdata nd;
2124 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2126 error = kern_stat(&nd, &st);
2128 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2135 * pathconf_Args(char *path, int name)
2137 * Get configurable pathname variables.
2141 sys_pathconf(struct pathconf_args *uap)
2143 struct nlookupdata nd;
2148 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2150 error = nlookup(&nd);
2152 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2155 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2163 * kern_readlink isn't properly split yet. There is a copyin burried
2164 * in VOP_READLINK().
2167 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2169 struct thread *td = curthread;
2170 struct proc *p = td->td_proc;
2176 if ((error = nlookup(nd)) != 0)
2178 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2181 if (vp->v_type != VLNK) {
2184 aiov.iov_base = buf;
2185 aiov.iov_len = count;
2186 auio.uio_iov = &aiov;
2187 auio.uio_iovcnt = 1;
2188 auio.uio_offset = 0;
2189 auio.uio_rw = UIO_READ;
2190 auio.uio_segflg = UIO_USERSPACE;
2192 auio.uio_resid = count;
2193 error = VOP_READLINK(vp, &auio, p->p_ucred);
2196 *res = count - auio.uio_resid;
2201 * readlink_args(char *path, char *buf, int count)
2203 * Return target name of a symbolic link.
2206 sys_readlink(struct readlink_args *uap)
2208 struct nlookupdata nd;
2211 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2213 error = kern_readlink(&nd, uap->buf, uap->count,
2214 &uap->sysmsg_result);
2221 setfflags(struct vnode *vp, int flags)
2223 struct thread *td = curthread;
2224 struct proc *p = td->td_proc;
2229 * Prevent non-root users from setting flags on devices. When
2230 * a device is reused, users can retain ownership of the device
2231 * if they are allowed to set flags and programs assume that
2232 * chown can't fail when done as root.
2234 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2235 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2239 * note: vget is required for any operation that might mod the vnode
2240 * so VINACTIVE is properly cleared.
2242 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2244 vattr.va_flags = flags;
2245 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2252 * chflags(char *path, int flags)
2254 * Change flags of a file given a path name.
2258 sys_chflags(struct chflags_args *uap)
2260 struct nlookupdata nd;
2265 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2266 /* XXX Add NLC flag indicating modifying operation? */
2268 error = nlookup(&nd);
2270 error = ncp_writechk(&nd.nl_nch);
2272 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2275 error = setfflags(vp, uap->flags);
2282 * fchflags_args(int fd, int flags)
2284 * Change flags of a file given a file descriptor.
2288 sys_fchflags(struct fchflags_args *uap)
2290 struct thread *td = curthread;
2291 struct proc *p = td->td_proc;
2295 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2297 if (fp->f_nchandle.ncp)
2298 error = ncp_writechk(&fp->f_nchandle);
2300 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2306 setfmode(struct vnode *vp, int mode)
2308 struct thread *td = curthread;
2309 struct proc *p = td->td_proc;
2314 * note: vget is required for any operation that might mod the vnode
2315 * so VINACTIVE is properly cleared.
2317 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2319 vattr.va_mode = mode & ALLPERMS;
2320 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2327 kern_chmod(struct nlookupdata *nd, int mode)
2332 /* XXX Add NLC flag indicating modifying operation? */
2333 if ((error = nlookup(nd)) != 0)
2335 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2337 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2338 error = setfmode(vp, mode);
2344 * chmod_args(char *path, int mode)
2346 * Change mode of a file given path name.
2350 sys_chmod(struct chmod_args *uap)
2352 struct nlookupdata nd;
2355 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2357 error = kern_chmod(&nd, uap->mode);
2363 * lchmod_args(char *path, int mode)
2365 * Change mode of a file given path name (don't follow links.)
2369 sys_lchmod(struct lchmod_args *uap)
2371 struct nlookupdata nd;
2374 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2376 error = kern_chmod(&nd, uap->mode);
2382 * fchmod_args(int fd, int mode)
2384 * Change mode of a file given a file descriptor.
2388 sys_fchmod(struct fchmod_args *uap)
2390 struct thread *td = curthread;
2391 struct proc *p = td->td_proc;
2395 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2397 if (fp->f_nchandle.ncp)
2398 error = ncp_writechk(&fp->f_nchandle);
2400 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2406 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2408 struct thread *td = curthread;
2409 struct proc *p = td->td_proc;
2414 * note: vget is required for any operation that might mod the vnode
2415 * so VINACTIVE is properly cleared.
2417 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2421 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2428 kern_chown(struct nlookupdata *nd, int uid, int gid)
2433 /* XXX Add NLC flag indicating modifying operation? */
2434 if ((error = nlookup(nd)) != 0)
2436 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2438 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2439 error = setfown(vp, uid, gid);
2445 * chown(char *path, int uid, int gid)
2447 * Set ownership given a path name.
2450 sys_chown(struct chown_args *uap)
2452 struct nlookupdata nd;
2455 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2457 error = kern_chown(&nd, uap->uid, uap->gid);
2463 * lchown_args(char *path, int uid, int gid)
2465 * Set ownership given a path name, do not cross symlinks.
2468 sys_lchown(struct lchown_args *uap)
2470 struct nlookupdata nd;
2473 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2475 error = kern_chown(&nd, uap->uid, uap->gid);
2481 * fchown_args(int fd, int uid, int gid)
2483 * Set ownership given a file descriptor.
2487 sys_fchown(struct fchown_args *uap)
2489 struct thread *td = curthread;
2490 struct proc *p = td->td_proc;
2494 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2496 if (fp->f_nchandle.ncp)
2497 error = ncp_writechk(&fp->f_nchandle);
2499 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2505 getutimes(const struct timeval *tvp, struct timespec *tsp)
2507 struct timeval tv[2];
2511 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2514 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2515 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2521 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2523 struct thread *td = curthread;
2524 struct proc *p = td->td_proc;
2529 * note: vget is required for any operation that might mod the vnode
2530 * so VINACTIVE is properly cleared.
2532 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2534 vattr.va_atime = ts[0];
2535 vattr.va_mtime = ts[1];
2537 vattr.va_vaflags |= VA_UTIMES_NULL;
2538 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2545 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2547 struct timespec ts[2];
2551 if ((error = getutimes(tptr, ts)) != 0)
2553 /* XXX Add NLC flag indicating modifying operation? */
2554 if ((error = nlookup(nd)) != 0)
2556 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2558 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2560 error = setutimes(vp, ts, tptr == NULL);
2566 * utimes_args(char *path, struct timeval *tptr)
2568 * Set the access and modification times of a file.
2571 sys_utimes(struct utimes_args *uap)
2573 struct timeval tv[2];
2574 struct nlookupdata nd;
2578 error = copyin(uap->tptr, tv, sizeof(tv));
2582 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2584 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2590 * lutimes_args(char *path, struct timeval *tptr)
2592 * Set the access and modification times of a file.
2595 sys_lutimes(struct lutimes_args *uap)
2597 struct timeval tv[2];
2598 struct nlookupdata nd;
2602 error = copyin(uap->tptr, tv, sizeof(tv));
2606 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2608 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2614 kern_futimes(int fd, struct timeval *tptr)
2616 struct thread *td = curthread;
2617 struct proc *p = td->td_proc;
2618 struct timespec ts[2];
2622 error = getutimes(tptr, ts);
2625 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2627 if (fp->f_nchandle.ncp)
2628 error = ncp_writechk(&fp->f_nchandle);
2630 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2636 * futimes_args(int fd, struct timeval *tptr)
2638 * Set the access and modification times of a file.
2641 sys_futimes(struct futimes_args *uap)
2643 struct timeval tv[2];
2647 error = copyin(uap->tptr, tv, sizeof(tv));
2652 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2658 kern_truncate(struct nlookupdata *nd, off_t length)
2666 /* XXX Add NLC flag indicating modifying operation? */
2667 if ((error = nlookup(nd)) != 0)
2669 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2671 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2673 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2677 if (vp->v_type == VDIR) {
2679 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
2680 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2682 vattr.va_size = length;
2683 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2690 * truncate(char *path, int pad, off_t length)
2692 * Truncate a file given its path name.
2695 sys_truncate(struct truncate_args *uap)
2697 struct nlookupdata nd;
2700 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2702 error = kern_truncate(&nd, uap->length);
2708 kern_ftruncate(int fd, off_t length)
2710 struct thread *td = curthread;
2711 struct proc *p = td->td_proc;
2719 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2721 if (fp->f_nchandle.ncp) {
2722 error = ncp_writechk(&fp->f_nchandle);
2726 if ((fp->f_flag & FWRITE) == 0) {
2730 vp = (struct vnode *)fp->f_data;
2731 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2732 if (vp->v_type == VDIR) {
2734 } else if ((error = vn_writechk(vp, NULL)) == 0) {
2736 vattr.va_size = length;
2737 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2746 * ftruncate_args(int fd, int pad, off_t length)
2748 * Truncate a file given a file descriptor.
2751 sys_ftruncate(struct ftruncate_args *uap)
2755 error = kern_ftruncate(uap->fd, uap->length);
2763 * Sync an open file.
2767 sys_fsync(struct fsync_args *uap)
2769 struct thread *td = curthread;
2770 struct proc *p = td->td_proc;
2776 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2778 vp = (struct vnode *)fp->f_data;
2779 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2780 if ((obj = vp->v_object) != NULL)
2781 vm_object_page_clean(obj, 0, 0, 0);
2782 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2783 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2785 error = (*bioops.io_fsync)(vp);
2793 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2795 struct nchandle fnchd;
2796 struct nchandle tnchd;
2797 struct namecache *ncp;
2802 if ((error = nlookup(fromnd)) != 0)
2804 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
2806 fnchd.mount = fromnd->nl_nch.mount;
2810 * unlock the source nch so we can lookup the target nch without
2811 * deadlocking. The target may or may not exist so we do not check
2812 * for a target vp like kern_mkdir() and other creation functions do.
2814 * The source and target directories are ref'd and rechecked after
2815 * everything is relocked to determine if the source or target file
2818 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2819 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2820 cache_unlock(&fromnd->nl_nch);
2822 tond->nl_flags |= NLC_CREATE;
2823 if ((error = nlookup(tond)) != 0) {
2827 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
2831 tnchd.mount = tond->nl_nch.mount;
2835 * If the source and target are the same there is nothing to do
2837 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
2844 * Mount points cannot be renamed or overwritten
2846 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
2855 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2856 * may have become invalid while it was unlocked, nc_vp and nc_mount
2859 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
2860 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2861 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
2862 cache_lock(&fromnd->nl_nch);
2863 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2865 cache_unlock(&tond->nl_nch);
2866 cache_lock(&fromnd->nl_nch);
2867 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2868 cache_lock(&tond->nl_nch);
2869 cache_resolve(&tond->nl_nch, tond->nl_cred);
2871 fromnd->nl_flags |= NLC_NCPISLOCKED;
2874 * make sure the parent directories linkages are the same
2876 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
2877 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
2884 * Both the source and target must be within the same filesystem and
2885 * in the same filesystem as their parent directories within the
2886 * namecache topology.
2888 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2891 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
2892 mp != tond->nl_nch.mount) {
2899 * Make sure the mount point is writable
2901 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
2908 * If the target exists and either the source or target is a directory,
2909 * then both must be directories.
2911 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
2914 if (tond->nl_nch.ncp->nc_vp) {
2915 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
2917 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
2918 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
2920 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
2926 * You cannot rename a source into itself or a subdirectory of itself.
2927 * We check this by travsersing the target directory upwards looking
2928 * for a match against the source.
2931 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
2932 if (fromnd->nl_nch.ncp == ncp) {
2943 * Even though the namespaces are different, they may still represent
2944 * hardlinks to the same file. The filesystem might have a hard time
2945 * with this so we issue a NREMOVE of the source instead of a NRENAME
2946 * when we detect the situation.
2949 if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
2950 error = VOP_NREMOVE(&fromnd->nl_nch, fromnd->nl_cred);
2952 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
2960 * rename_args(char *from, char *to)
2962 * Rename files. Source and destination must either both be directories,
2963 * or both not be directories. If target is a directory, it must be empty.
2966 sys_rename(struct rename_args *uap)
2968 struct nlookupdata fromnd, tond;
2971 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2973 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2975 error = kern_rename(&fromnd, &tond);
2976 nlookup_done(&tond);
2978 nlookup_done(&fromnd);
2983 kern_mkdir(struct nlookupdata *nd, int mode)
2985 struct thread *td = curthread;
2986 struct proc *p = td->td_proc;
2992 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2993 if ((error = nlookup(nd)) != 0)
2996 if (nd->nl_nch.ncp->nc_vp)
2998 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3002 vattr.va_type = VDIR;
3003 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3006 error = VOP_NMKDIR(&nd->nl_nch, &vp, p->p_ucred, &vattr);
3013 * mkdir_args(char *path, int mode)
3015 * Make a directory file.
3019 sys_mkdir(struct mkdir_args *uap)
3021 struct nlookupdata nd;
3024 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3026 error = kern_mkdir(&nd, uap->mode);
3032 kern_rmdir(struct nlookupdata *nd)
3037 nd->nl_flags |= NLC_DELETE;
3038 if ((error = nlookup(nd)) != 0)
3042 * Do not allow directories representing mount points to be
3043 * deleted, even if empty. Check write perms on mount point
3044 * in case the vnode is aliased (aka nullfs).
3046 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3048 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3051 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_cred);
3056 * rmdir_args(char *path)
3058 * Remove a directory file.
3062 sys_rmdir(struct rmdir_args *uap)
3064 struct nlookupdata nd;
3067 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3069 error = kern_rmdir(&nd);
3075 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3076 enum uio_seg direction)
3078 struct thread *td = curthread;
3079 struct proc *p = td->td_proc;
3087 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3089 if ((fp->f_flag & FREAD) == 0) {
3093 vp = (struct vnode *)fp->f_data;
3095 if (vp->v_type != VDIR) {
3099 aiov.iov_base = buf;
3100 aiov.iov_len = count;
3101 auio.uio_iov = &aiov;
3102 auio.uio_iovcnt = 1;
3103 auio.uio_rw = UIO_READ;
3104 auio.uio_segflg = direction;
3106 auio.uio_resid = count;
3107 loff = auio.uio_offset = fp->f_offset;
3108 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3109 fp->f_offset = auio.uio_offset;
3112 if (count == auio.uio_resid) {
3113 if (union_dircheckp) {
3114 error = union_dircheckp(td, &vp, fp);
3121 if ((vp->v_flag & VROOT) &&
3122 (vp->v_mount->mnt_flag & MNT_UNION)) {
3123 struct vnode *tvp = vp;
3124 vp = vp->v_mount->mnt_vnodecovered;
3136 *res = count - auio.uio_resid;
3143 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3145 * Read a block of directory entries in a file system independent format.
3148 sys_getdirentries(struct getdirentries_args *uap)
3153 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3154 &uap->sysmsg_result, UIO_USERSPACE);
3157 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3162 * getdents_args(int fd, char *buf, size_t count)
3165 sys_getdents(struct getdents_args *uap)
3169 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3170 &uap->sysmsg_result, UIO_USERSPACE);
3176 * umask(int newmask)
3178 * Set the mode mask for creation of filesystem nodes.
3183 sys_umask(struct umask_args *uap)
3185 struct thread *td = curthread;
3186 struct proc *p = td->td_proc;
3187 struct filedesc *fdp;
3190 uap->sysmsg_result = fdp->fd_cmask;
3191 fdp->fd_cmask = uap->newmask & ALLPERMS;
3196 * revoke(char *path)
3198 * Void all references to file by ripping underlying filesystem
3203 sys_revoke(struct revoke_args *uap)
3205 struct nlookupdata nd;
3212 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3214 error = nlookup(&nd);
3216 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3217 cred = crhold(nd.nl_cred);
3220 if (vp->v_type != VCHR && vp->v_type != VBLK)
3223 error = VOP_GETATTR(vp, &vattr);
3224 if (error == 0 && cred->cr_uid != vattr.va_uid)
3225 error = suser_cred(cred, PRISON_ROOT);
3226 if (error == 0 && count_udev(vp->v_udev) > 0) {
3229 VOP_REVOKE(vp, REVOKEALL);
3240 * getfh_args(char *fname, fhandle_t *fhp)
3242 * Get (NFS) file handle
3245 sys_getfh(struct getfh_args *uap)
3247 struct thread *td = curthread;
3248 struct nlookupdata nd;
3254 * Must be super user
3256 if ((error = suser(td)) != 0)
3260 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3262 error = nlookup(&nd);
3264 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3267 bzero(&fh, sizeof(fh));
3268 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3269 error = VFS_VPTOFH(vp, &fh.fh_fid);
3272 error = copyout(&fh, uap->fhp, sizeof(fh));
3278 * fhopen_args(const struct fhandle *u_fhp, int flags)
3280 * syscall for the rpc.lockd to use to translate a NFS file handle into
3281 * an open descriptor.
3283 * warning: do not remove the suser() call or this becomes one giant
3287 sys_fhopen(struct fhopen_args *uap)
3289 struct thread *td = curthread;
3290 struct proc *p = td->td_proc;
3295 struct vattr *vap = &vat;
3297 int fmode, mode, error, type;
3303 * Must be super user
3309 fmode = FFLAGS(uap->flags);
3310 /* why not allow a non-read/write open for our lockd? */
3311 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3313 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3316 /* find the mount point */
3317 mp = vfs_getvfs(&fhp.fh_fsid);
3320 /* now give me my vnode, it gets returned to me locked */
3321 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3325 * from now on we have to make sure not
3326 * to forget about the vnode
3327 * any error that causes an abort must vput(vp)
3328 * just set error = err and 'goto bad;'.
3334 if (vp->v_type == VLNK) {
3338 if (vp->v_type == VSOCK) {
3343 if (fmode & (FWRITE | O_TRUNC)) {
3344 if (vp->v_type == VDIR) {
3348 error = vn_writechk(vp, NULL);
3356 error = VOP_ACCESS(vp, mode, p->p_ucred);
3360 if (fmode & O_TRUNC) {
3361 vn_unlock(vp); /* XXX */
3362 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3365 error = VOP_SETATTR(vp, vap, p->p_ucred);
3371 * VOP_OPEN needs the file pointer so it can potentially override
3374 * WARNING! no f_nchandle will be associated when fhopen()ing a
3377 if ((error = falloc(p, &nfp, &indx)) != 0)
3381 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3384 * setting f_ops this way prevents VOP_CLOSE from being
3385 * called or fdrop() releasing the vp from v_data. Since
3386 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3388 fp->f_ops = &badfileops;
3394 * The fp is given its own reference, we still have our ref and lock.
3396 * Assert that all regular files must be created with a VM object.
3398 if (vp->v_type == VREG && vp->v_object == NULL) {
3399 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3404 * The open was successful. Handle any locking requirements.
3406 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3407 lf.l_whence = SEEK_SET;
3410 if (fmode & O_EXLOCK)
3411 lf.l_type = F_WRLCK;
3413 lf.l_type = F_RDLCK;
3414 if (fmode & FNONBLOCK)
3419 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3421 * release our private reference.
3423 fsetfd(p, NULL, indx);
3428 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3429 fp->f_flag |= FHASLOCK;
3433 * Clean up. Associate the file pointer with the previously
3434 * reserved descriptor and return it.
3437 fsetfd(p, fp, indx);
3439 uap->sysmsg_result = indx;
3443 fsetfd(p, NULL, indx);
3451 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3454 sys_fhstat(struct fhstat_args *uap)
3456 struct thread *td = curthread;
3464 * Must be super user
3470 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3474 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3476 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3478 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3482 error = copyout(&sb, uap->sb, sizeof(sb));
3487 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3490 sys_fhstatfs(struct fhstatfs_args *uap)
3492 struct thread *td = curthread;
3493 struct proc *p = td->td_proc;
3498 char *fullpath, *freepath;
3503 * Must be super user
3505 if ((error = suser(td)))
3508 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3511 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3514 if (p != NULL && !chroot_visible_mnt(mp, p))
3517 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3522 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3525 error = cache_fullpath(p, &mp->mnt_ncmountpt, &fullpath, &freepath);
3528 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3529 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3530 kfree(freepath, M_TEMP);
3532 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3534 bcopy(sp, &sb, sizeof(sb));
3535 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3538 return (copyout(sp, uap->buf, sizeof(*sp)));
3542 * Syscall to push extended attribute configuration information into the
3543 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3544 * a command (int cmd), and attribute name and misc data. For now, the
3545 * attribute name is left in userspace for consumption by the VFS_op.
3546 * It will probably be changed to be copied into sysspace by the
3547 * syscall in the future, once issues with various consumers of the
3548 * attribute code have raised their hands.
3550 * Currently this is used only by UFS Extended Attributes.
3553 sys_extattrctl(struct extattrctl_args *uap)
3555 struct nlookupdata nd;
3561 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3563 error = nlookup(&nd);
3565 mp = nd.nl_nch.mount;
3566 error = VFS_EXTATTRCTL(mp, uap->cmd,
3567 uap->attrname, uap->arg,
3575 * Syscall to set a named extended attribute on a file or directory.
3576 * Accepts attribute name, and a uio structure pointing to the data to set.
3577 * The uio is consumed in the style of writev(). The real work happens
3578 * in VOP_SETEXTATTR().
3581 sys_extattr_set_file(struct extattr_set_file_args *uap)
3583 char attrname[EXTATTR_MAXNAMELEN];
3584 struct iovec aiov[UIO_SMALLIOV];
3585 struct iovec *needfree;
3586 struct nlookupdata nd;
3595 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3600 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3602 error = nlookup(&nd);
3604 error = ncp_writechk(&nd.nl_nch);
3606 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3613 iovlen = uap->iovcnt * sizeof(struct iovec);
3614 if (uap->iovcnt > UIO_SMALLIOV) {
3615 if (uap->iovcnt > UIO_MAXIOV) {
3619 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3625 auio.uio_iovcnt = uap->iovcnt;
3626 auio.uio_rw = UIO_WRITE;
3627 auio.uio_segflg = UIO_USERSPACE;
3628 auio.uio_td = nd.nl_td;
3629 auio.uio_offset = 0;
3630 if ((error = copyin(uap->iovp, iov, iovlen)))
3633 for (i = 0; i < uap->iovcnt; i++) {
3634 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3638 auio.uio_resid += iov->iov_len;
3641 cnt = auio.uio_resid;
3642 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3643 cnt -= auio.uio_resid;
3644 uap->sysmsg_result = cnt;
3649 FREE(needfree, M_IOV);
3654 * Syscall to get a named extended attribute on a file or directory.
3655 * Accepts attribute name, and a uio structure pointing to a buffer for the
3656 * data. The uio is consumed in the style of readv(). The real work
3657 * happens in VOP_GETEXTATTR();
3660 sys_extattr_get_file(struct extattr_get_file_args *uap)
3662 char attrname[EXTATTR_MAXNAMELEN];
3663 struct iovec aiov[UIO_SMALLIOV];
3664 struct iovec *needfree;
3665 struct nlookupdata nd;
3674 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3679 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3681 error = nlookup(&nd);
3683 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3689 iovlen = uap->iovcnt * sizeof (struct iovec);
3691 if (uap->iovcnt > UIO_SMALLIOV) {
3692 if (uap->iovcnt > UIO_MAXIOV) {
3696 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3702 auio.uio_iovcnt = uap->iovcnt;
3703 auio.uio_rw = UIO_READ;
3704 auio.uio_segflg = UIO_USERSPACE;
3705 auio.uio_td = nd.nl_td;
3706 auio.uio_offset = 0;
3707 if ((error = copyin(uap->iovp, iov, iovlen)))
3710 for (i = 0; i < uap->iovcnt; i++) {
3711 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3715 auio.uio_resid += iov->iov_len;
3718 cnt = auio.uio_resid;
3719 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3720 cnt -= auio.uio_resid;
3721 uap->sysmsg_result = cnt;
3726 FREE(needfree, M_IOV);
3731 * Syscall to delete a named extended attribute from a file or directory.
3732 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3735 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
3737 char attrname[EXTATTR_MAXNAMELEN];
3738 struct nlookupdata nd;
3742 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3747 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3749 error = nlookup(&nd);
3751 error = ncp_writechk(&nd.nl_nch);
3753 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3759 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3766 * Determine if the mount is visible to the process.
3769 chroot_visible_mnt(struct mount *mp, struct proc *p)
3771 struct nchandle nch;
3774 * Traverse from the mount point upwards. If we hit the process
3775 * root then the mount point is visible to the process.
3777 nch = mp->mnt_ncmountpt;
3779 if (nch.mount == p->p_fd->fd_nrdir.mount &&
3780 nch.ncp == p->p_fd->fd_nrdir.ncp) {
3783 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
3784 nch = nch.mount->mnt_ncmounton;
3786 nch.ncp = nch.ncp->nc_parent;