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.111 2006/12/23 23:47:54 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(struct filedesc *fdp)
1288 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1289 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1291 vp = (struct vnode *)fp->f_data;
1292 if (vp->v_type != VDIR) {
1303 * This sysctl determines if we will allow a process to chroot(2) if it
1304 * has a directory open:
1305 * 0: disallowed for all processes.
1306 * 1: allowed for processes that were not already chroot(2)'ed.
1307 * 2: allowed for all processes.
1310 static int chroot_allow_open_directories = 1;
1312 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1313 &chroot_allow_open_directories, 0, "");
1316 * chroot to the specified namecache entry. We obtain the vp from the
1317 * namecache data. The passed ncp must be locked and referenced and will
1318 * remain locked and referenced on return.
1321 kern_chroot(struct nchandle *nch)
1323 struct thread *td = curthread;
1324 struct proc *p = td->td_proc;
1325 struct filedesc *fdp = p->p_fd;
1330 * Only root can chroot
1332 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1336 * Disallow open directory descriptors (fchdir() breakouts).
1338 if (chroot_allow_open_directories == 0 ||
1339 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1340 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1343 if ((vp = nch->ncp->nc_vp) == NULL)
1346 if ((error = vget(vp, LK_SHARED)) != 0)
1350 * Check the validity of vp as a directory to change to and
1351 * associate it with rdir/jdir.
1353 error = checkvp_chdir(vp, td);
1354 vn_unlock(vp); /* leave reference intact */
1356 vrele(fdp->fd_rdir);
1357 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1358 cache_drop(&fdp->fd_nrdir);
1359 cache_copy(nch, &fdp->fd_nrdir);
1360 if (fdp->fd_jdir == NULL) {
1363 cache_copy(nch, &fdp->fd_njdir);
1372 * chroot_args(char *path)
1374 * Change notion of root (``/'') directory.
1378 sys_chroot(struct chroot_args *uap)
1380 struct thread *td = curthread;
1381 struct nlookupdata nd;
1384 KKASSERT(td->td_proc);
1385 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1390 error = nlookup(&nd);
1392 error = kern_chroot(&nd.nl_nch);
1398 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1399 * determine whether it is legal to chdir to the vnode. The vnode's state
1400 * is not changed by this call.
1403 checkvp_chdir(struct vnode *vp, struct thread *td)
1407 if (vp->v_type != VDIR)
1410 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1415 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1417 struct thread *td = curthread;
1418 struct proc *p = td->td_proc;
1419 struct lwp *lp = td->td_lwp;
1420 struct filedesc *fdp = p->p_fd;
1425 int type, indx, error;
1428 if ((oflags & O_ACCMODE) == O_ACCMODE)
1430 flags = FFLAGS(oflags);
1431 error = falloc(p, &nfp, NULL);
1435 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1438 * XXX p_dupfd is a real mess. It allows a device to return a
1439 * file descriptor to be duplicated rather then doing the open
1445 * Call vn_open() to do the lookup and assign the vnode to the
1446 * file pointer. vn_open() does not change the ref count on fp
1447 * and the vnode, on success, will be inherited by the file pointer
1450 nd->nl_flags |= NLC_LOCKVP;
1451 error = vn_open(nd, fp, flags, cmode);
1455 * handle special fdopen() case. bleh. dupfdopen() is
1456 * responsible for dropping the old contents of ofiles[indx]
1459 * Note that fsetfd() will add a ref to fp which represents
1460 * the fd_files[] assignment. We must still drop our
1463 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1464 if (fdalloc(p, 0, &indx) == 0) {
1465 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1468 fdrop(fp); /* our ref */
1471 fsetfd(p, NULL, indx);
1474 fdrop(fp); /* our ref */
1475 if (error == ERESTART)
1481 * ref the vnode for ourselves so it can't be ripped out from under
1482 * is. XXX need an ND flag to request that the vnode be returned
1485 * Reserve a file descriptor but do not assign it until the open
1488 vp = (struct vnode *)fp->f_data;
1490 if ((error = fdalloc(p, 0, &indx)) != 0) {
1497 * If no error occurs the vp will have been assigned to the file
1502 if (flags & (O_EXLOCK | O_SHLOCK)) {
1503 lf.l_whence = SEEK_SET;
1506 if (flags & O_EXLOCK)
1507 lf.l_type = F_WRLCK;
1509 lf.l_type = F_RDLCK;
1510 if (flags & FNONBLOCK)
1515 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1517 * lock request failed. Clean up the reserved
1521 fsetfd(p, NULL, indx);
1525 fp->f_flag |= FHASLOCK;
1529 * Assert that all regular file vnodes were created with a object.
1531 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1532 ("open: regular file has no backing object after vn_open"));
1538 * release our private reference, leaving the one associated with the
1539 * descriptor table intact.
1541 fsetfd(p, fp, indx);
1548 * open_args(char *path, int flags, int mode)
1550 * Check permissions, allocate an open file structure,
1551 * and call the device open routine if any.
1554 sys_open(struct open_args *uap)
1556 struct nlookupdata nd;
1559 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1561 error = kern_open(&nd, uap->flags,
1562 uap->mode, &uap->sysmsg_result);
1569 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1571 struct thread *td = curthread;
1572 struct proc *p = td->td_proc;
1580 switch (mode & S_IFMT) {
1586 error = suser_cred(p->p_ucred, PRISON_ROOT);
1593 nd->nl_flags |= NLC_CREATE;
1594 if ((error = nlookup(nd)) != 0)
1596 if (nd->nl_nch.ncp->nc_vp)
1598 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1602 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1603 vattr.va_rdev = dev;
1606 switch (mode & S_IFMT) {
1607 case S_IFMT: /* used by badsect to flag bad sectors */
1608 vattr.va_type = VBAD;
1611 vattr.va_type = VCHR;
1614 vattr.va_type = VBLK;
1625 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_cred, NAMEI_CREATE);
1628 error = VOP_NMKNOD(&nd->nl_nch, &vp, nd->nl_cred, &vattr);
1637 * mknod_args(char *path, int mode, int dev)
1639 * Create a special file.
1642 sys_mknod(struct mknod_args *uap)
1644 struct nlookupdata nd;
1647 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1649 error = kern_mknod(&nd, uap->mode, uap->dev);
1655 kern_mkfifo(struct nlookupdata *nd, int mode)
1657 struct thread *td = curthread;
1658 struct proc *p = td->td_proc;
1665 nd->nl_flags |= NLC_CREATE;
1666 if ((error = nlookup(nd)) != 0)
1668 if (nd->nl_nch.ncp->nc_vp)
1670 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1674 vattr.va_type = VFIFO;
1675 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1677 error = VOP_NMKNOD(&nd->nl_nch, &vp, nd->nl_cred, &vattr);
1684 * mkfifo_args(char *path, int mode)
1686 * Create a named pipe.
1689 sys_mkfifo(struct mkfifo_args *uap)
1691 struct nlookupdata nd;
1694 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1696 error = kern_mkfifo(&nd, uap->mode);
1701 static int hardlink_check_uid = 0;
1702 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1703 &hardlink_check_uid, 0,
1704 "Unprivileged processes cannot create hard links to files owned by other "
1706 static int hardlink_check_gid = 0;
1707 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1708 &hardlink_check_gid, 0,
1709 "Unprivileged processes cannot create hard links to files owned by other "
1713 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1719 * Shortcut if disabled
1721 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1725 * root cred can always hardlink
1727 if (suser_cred(cred, PRISON_ROOT) == 0)
1731 * Otherwise only if the originating file is owned by the
1732 * same user or group. Note that any group is allowed if
1733 * the file is owned by the caller.
1735 error = VOP_GETATTR(vp, &va);
1739 if (hardlink_check_uid) {
1740 if (cred->cr_uid != va.va_uid)
1744 if (hardlink_check_gid) {
1745 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1753 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1755 struct thread *td = curthread;
1760 * Lookup the source and obtained a locked vnode.
1762 * XXX relookup on vget failure / race ?
1765 if ((error = nlookup(nd)) != 0)
1767 vp = nd->nl_nch.ncp->nc_vp;
1768 KKASSERT(vp != NULL);
1769 if (vp->v_type == VDIR)
1770 return (EPERM); /* POSIX */
1771 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1773 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1777 * Unlock the source so we can lookup the target without deadlocking
1778 * (XXX vp is locked already, possible other deadlock?). The target
1781 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1782 nd->nl_flags &= ~NLC_NCPISLOCKED;
1783 cache_unlock(&nd->nl_nch);
1785 linknd->nl_flags |= NLC_CREATE;
1786 if ((error = nlookup(linknd)) != 0) {
1790 if (linknd->nl_nch.ncp->nc_vp) {
1796 * Finally run the new API VOP.
1798 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1800 error = VOP_NLINK(&linknd->nl_nch, vp, linknd->nl_cred);
1806 * link_args(char *path, char *link)
1808 * Make a hard file link.
1811 sys_link(struct link_args *uap)
1813 struct nlookupdata nd, linknd;
1816 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1818 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1820 error = kern_link(&nd, &linknd);
1821 nlookup_done(&linknd);
1828 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1835 nd->nl_flags |= NLC_CREATE;
1836 if ((error = nlookup(nd)) != 0)
1838 if (nd->nl_nch.ncp->nc_vp)
1840 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1843 vattr.va_mode = mode;
1844 error = VOP_NSYMLINK(&nd->nl_nch, &vp, nd->nl_cred, &vattr, path);
1851 * symlink(char *path, char *link)
1853 * Make a symbolic link.
1856 sys_symlink(struct symlink_args *uap)
1858 struct thread *td = curthread;
1859 struct nlookupdata nd;
1864 path = objcache_get(namei_oc, M_WAITOK);
1865 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1867 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1869 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1870 error = kern_symlink(&nd, path, mode);
1874 objcache_put(namei_oc, path);
1879 * undelete_args(char *path)
1881 * Delete a whiteout from the filesystem.
1885 sys_undelete(struct undelete_args *uap)
1887 struct nlookupdata nd;
1890 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1892 nd.nl_flags |= NLC_DELETE;
1894 error = nlookup(&nd);
1896 error = ncp_writechk(&nd.nl_nch);
1898 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_cred, NAMEI_DELETE);
1904 kern_unlink(struct nlookupdata *nd)
1909 nd->nl_flags |= NLC_DELETE;
1910 if ((error = nlookup(nd)) != 0)
1912 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1914 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_cred);
1919 * unlink_args(char *path)
1921 * Delete a name from the filesystem.
1924 sys_unlink(struct unlink_args *uap)
1926 struct nlookupdata nd;
1929 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1931 error = kern_unlink(&nd);
1937 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1939 struct thread *td = curthread;
1940 struct proc *p = td->td_proc;
1945 fp = holdfp(p->p_fd, fd, -1);
1948 if (fp->f_type != DTYPE_VNODE) {
1955 fp->f_offset += offset;
1959 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
1961 fp->f_offset = offset + vattr.va_size;
1964 fp->f_offset = offset;
1971 *res = fp->f_offset;
1978 * lseek_args(int fd, int pad, off_t offset, int whence)
1980 * Reposition read/write file offset.
1983 sys_lseek(struct lseek_args *uap)
1987 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1988 &uap->sysmsg_offset);
1994 kern_access(struct nlookupdata *nd, int aflags)
1999 if ((error = nlookup(nd)) != 0)
2002 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2006 /* Flags == 0 means only check for existence. */
2015 if ((flags & VWRITE) == 0 ||
2016 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2017 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2020 * If the file handle is stale we have to re-resolve the
2021 * entry. This is a hack at the moment.
2023 if (error == ESTALE) {
2024 cache_setunresolved(&nd->nl_nch);
2025 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2038 * access_args(char *path, int flags)
2040 * Check access permissions.
2043 sys_access(struct access_args *uap)
2045 struct nlookupdata nd;
2048 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2050 error = kern_access(&nd, uap->flags);
2056 kern_stat(struct nlookupdata *nd, struct stat *st)
2062 if ((error = nlookup(nd)) != 0)
2065 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2069 if ((error = vget(vp, LK_SHARED)) != 0)
2071 error = vn_stat(vp, st, nd->nl_cred);
2074 * If the file handle is stale we have to re-resolve the entry. This
2075 * is a hack at the moment.
2077 if (error == ESTALE) {
2078 cache_setunresolved(&nd->nl_nch);
2079 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2090 * stat_args(char *path, struct stat *ub)
2092 * Get file status; this version follows links.
2095 sys_stat(struct stat_args *uap)
2097 struct nlookupdata nd;
2101 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2103 error = kern_stat(&nd, &st);
2105 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2112 * lstat_args(char *path, struct stat *ub)
2114 * Get file status; this version does not follow links.
2117 sys_lstat(struct lstat_args *uap)
2119 struct nlookupdata nd;
2123 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2125 error = kern_stat(&nd, &st);
2127 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2134 * pathconf_Args(char *path, int name)
2136 * Get configurable pathname variables.
2140 sys_pathconf(struct pathconf_args *uap)
2142 struct nlookupdata nd;
2147 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2149 error = nlookup(&nd);
2151 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2154 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2162 * kern_readlink isn't properly split yet. There is a copyin burried
2163 * in VOP_READLINK().
2166 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2168 struct thread *td = curthread;
2169 struct proc *p = td->td_proc;
2175 if ((error = nlookup(nd)) != 0)
2177 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2180 if (vp->v_type != VLNK) {
2183 aiov.iov_base = buf;
2184 aiov.iov_len = count;
2185 auio.uio_iov = &aiov;
2186 auio.uio_iovcnt = 1;
2187 auio.uio_offset = 0;
2188 auio.uio_rw = UIO_READ;
2189 auio.uio_segflg = UIO_USERSPACE;
2191 auio.uio_resid = count;
2192 error = VOP_READLINK(vp, &auio, p->p_ucred);
2195 *res = count - auio.uio_resid;
2200 * readlink_args(char *path, char *buf, int count)
2202 * Return target name of a symbolic link.
2205 sys_readlink(struct readlink_args *uap)
2207 struct nlookupdata nd;
2210 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2212 error = kern_readlink(&nd, uap->buf, uap->count,
2213 &uap->sysmsg_result);
2220 setfflags(struct vnode *vp, int flags)
2222 struct thread *td = curthread;
2223 struct proc *p = td->td_proc;
2228 * Prevent non-root users from setting flags on devices. When
2229 * a device is reused, users can retain ownership of the device
2230 * if they are allowed to set flags and programs assume that
2231 * chown can't fail when done as root.
2233 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2234 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2238 * note: vget is required for any operation that might mod the vnode
2239 * so VINACTIVE is properly cleared.
2241 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2243 vattr.va_flags = flags;
2244 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2251 * chflags(char *path, int flags)
2253 * Change flags of a file given a path name.
2257 sys_chflags(struct chflags_args *uap)
2259 struct nlookupdata nd;
2264 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2265 /* XXX Add NLC flag indicating modifying operation? */
2267 error = nlookup(&nd);
2269 error = ncp_writechk(&nd.nl_nch);
2271 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2274 error = setfflags(vp, uap->flags);
2281 * fchflags_args(int fd, int flags)
2283 * Change flags of a file given a file descriptor.
2287 sys_fchflags(struct fchflags_args *uap)
2289 struct thread *td = curthread;
2290 struct proc *p = td->td_proc;
2294 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2296 if (fp->f_nchandle.ncp)
2297 error = ncp_writechk(&fp->f_nchandle);
2299 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2305 setfmode(struct vnode *vp, int mode)
2307 struct thread *td = curthread;
2308 struct proc *p = td->td_proc;
2313 * note: vget is required for any operation that might mod the vnode
2314 * so VINACTIVE is properly cleared.
2316 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2318 vattr.va_mode = mode & ALLPERMS;
2319 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2326 kern_chmod(struct nlookupdata *nd, int mode)
2331 /* XXX Add NLC flag indicating modifying operation? */
2332 if ((error = nlookup(nd)) != 0)
2334 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2336 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2337 error = setfmode(vp, mode);
2343 * chmod_args(char *path, int mode)
2345 * Change mode of a file given path name.
2349 sys_chmod(struct chmod_args *uap)
2351 struct nlookupdata nd;
2354 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2356 error = kern_chmod(&nd, uap->mode);
2362 * lchmod_args(char *path, int mode)
2364 * Change mode of a file given path name (don't follow links.)
2368 sys_lchmod(struct lchmod_args *uap)
2370 struct nlookupdata nd;
2373 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2375 error = kern_chmod(&nd, uap->mode);
2381 * fchmod_args(int fd, int mode)
2383 * Change mode of a file given a file descriptor.
2387 sys_fchmod(struct fchmod_args *uap)
2389 struct thread *td = curthread;
2390 struct proc *p = td->td_proc;
2394 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2396 if (fp->f_nchandle.ncp)
2397 error = ncp_writechk(&fp->f_nchandle);
2399 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2405 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2407 struct thread *td = curthread;
2408 struct proc *p = td->td_proc;
2413 * note: vget is required for any operation that might mod the vnode
2414 * so VINACTIVE is properly cleared.
2416 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2420 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2427 kern_chown(struct nlookupdata *nd, int uid, int gid)
2432 /* XXX Add NLC flag indicating modifying operation? */
2433 if ((error = nlookup(nd)) != 0)
2435 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2437 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2438 error = setfown(vp, uid, gid);
2444 * chown(char *path, int uid, int gid)
2446 * Set ownership given a path name.
2449 sys_chown(struct chown_args *uap)
2451 struct nlookupdata nd;
2454 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2456 error = kern_chown(&nd, uap->uid, uap->gid);
2462 * lchown_args(char *path, int uid, int gid)
2464 * Set ownership given a path name, do not cross symlinks.
2467 sys_lchown(struct lchown_args *uap)
2469 struct nlookupdata nd;
2472 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2474 error = kern_chown(&nd, uap->uid, uap->gid);
2480 * fchown_args(int fd, int uid, int gid)
2482 * Set ownership given a file descriptor.
2486 sys_fchown(struct fchown_args *uap)
2488 struct thread *td = curthread;
2489 struct proc *p = td->td_proc;
2493 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2495 if (fp->f_nchandle.ncp)
2496 error = ncp_writechk(&fp->f_nchandle);
2498 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2504 getutimes(const struct timeval *tvp, struct timespec *tsp)
2506 struct timeval tv[2];
2510 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2513 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2514 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2520 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2522 struct thread *td = curthread;
2523 struct proc *p = td->td_proc;
2528 * note: vget is required for any operation that might mod the vnode
2529 * so VINACTIVE is properly cleared.
2531 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2533 vattr.va_atime = ts[0];
2534 vattr.va_mtime = ts[1];
2536 vattr.va_vaflags |= VA_UTIMES_NULL;
2537 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2544 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2546 struct timespec ts[2];
2550 if ((error = getutimes(tptr, ts)) != 0)
2552 /* XXX Add NLC flag indicating modifying operation? */
2553 if ((error = nlookup(nd)) != 0)
2555 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2557 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2559 error = setutimes(vp, ts, tptr == NULL);
2565 * utimes_args(char *path, struct timeval *tptr)
2567 * Set the access and modification times of a file.
2570 sys_utimes(struct utimes_args *uap)
2572 struct timeval tv[2];
2573 struct nlookupdata nd;
2577 error = copyin(uap->tptr, tv, sizeof(tv));
2581 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2583 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2589 * lutimes_args(char *path, struct timeval *tptr)
2591 * Set the access and modification times of a file.
2594 sys_lutimes(struct lutimes_args *uap)
2596 struct timeval tv[2];
2597 struct nlookupdata nd;
2601 error = copyin(uap->tptr, tv, sizeof(tv));
2605 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2607 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2613 kern_futimes(int fd, struct timeval *tptr)
2615 struct thread *td = curthread;
2616 struct proc *p = td->td_proc;
2617 struct timespec ts[2];
2621 error = getutimes(tptr, ts);
2624 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2626 if (fp->f_nchandle.ncp)
2627 error = ncp_writechk(&fp->f_nchandle);
2629 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2635 * futimes_args(int fd, struct timeval *tptr)
2637 * Set the access and modification times of a file.
2640 sys_futimes(struct futimes_args *uap)
2642 struct timeval tv[2];
2646 error = copyin(uap->tptr, tv, sizeof(tv));
2651 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2657 kern_truncate(struct nlookupdata *nd, off_t length)
2665 /* XXX Add NLC flag indicating modifying operation? */
2666 if ((error = nlookup(nd)) != 0)
2668 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2670 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2672 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2676 if (vp->v_type == VDIR) {
2678 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
2679 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2681 vattr.va_size = length;
2682 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2689 * truncate(char *path, int pad, off_t length)
2691 * Truncate a file given its path name.
2694 sys_truncate(struct truncate_args *uap)
2696 struct nlookupdata nd;
2699 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2701 error = kern_truncate(&nd, uap->length);
2707 kern_ftruncate(int fd, off_t length)
2709 struct thread *td = curthread;
2710 struct proc *p = td->td_proc;
2718 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2720 if (fp->f_nchandle.ncp) {
2721 error = ncp_writechk(&fp->f_nchandle);
2725 if ((fp->f_flag & FWRITE) == 0) {
2729 vp = (struct vnode *)fp->f_data;
2730 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2731 if (vp->v_type == VDIR) {
2733 } else if ((error = vn_writechk(vp, NULL)) == 0) {
2735 vattr.va_size = length;
2736 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2745 * ftruncate_args(int fd, int pad, off_t length)
2747 * Truncate a file given a file descriptor.
2750 sys_ftruncate(struct ftruncate_args *uap)
2754 error = kern_ftruncate(uap->fd, uap->length);
2762 * Sync an open file.
2766 sys_fsync(struct fsync_args *uap)
2768 struct thread *td = curthread;
2769 struct proc *p = td->td_proc;
2775 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2777 vp = (struct vnode *)fp->f_data;
2778 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2779 if ((obj = vp->v_object) != NULL)
2780 vm_object_page_clean(obj, 0, 0, 0);
2781 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2782 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2784 error = (*bioops.io_fsync)(vp);
2792 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2794 struct nchandle fnchd;
2795 struct nchandle tnchd;
2796 struct namecache *ncp;
2801 if ((error = nlookup(fromnd)) != 0)
2803 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
2805 fnchd.mount = fromnd->nl_nch.mount;
2809 * unlock the source nch so we can lookup the target nch without
2810 * deadlocking. The target may or may not exist so we do not check
2811 * for a target vp like kern_mkdir() and other creation functions do.
2813 * The source and target directories are ref'd and rechecked after
2814 * everything is relocked to determine if the source or target file
2817 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2818 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2819 cache_unlock(&fromnd->nl_nch);
2821 tond->nl_flags |= NLC_CREATE;
2822 if ((error = nlookup(tond)) != 0) {
2826 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
2830 tnchd.mount = tond->nl_nch.mount;
2834 * If the source and target are the same there is nothing to do
2836 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
2843 * Mount points cannot be renamed or overwritten
2845 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
2854 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2855 * may have become invalid while it was unlocked, nc_vp and nc_mount
2858 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
2859 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2860 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
2861 cache_lock(&fromnd->nl_nch);
2862 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2864 cache_unlock(&tond->nl_nch);
2865 cache_lock(&fromnd->nl_nch);
2866 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2867 cache_lock(&tond->nl_nch);
2868 cache_resolve(&tond->nl_nch, tond->nl_cred);
2870 fromnd->nl_flags |= NLC_NCPISLOCKED;
2873 * make sure the parent directories linkages are the same
2875 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
2876 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
2883 * Both the source and target must be within the same filesystem and
2884 * in the same filesystem as their parent directories within the
2885 * namecache topology.
2887 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2890 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
2891 mp != tond->nl_nch.mount) {
2898 * Make sure the mount point is writable
2900 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
2907 * If the target exists and either the source or target is a directory,
2908 * then both must be directories.
2910 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
2913 if (tond->nl_nch.ncp->nc_vp) {
2914 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
2916 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
2917 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
2919 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
2925 * You cannot rename a source into itself or a subdirectory of itself.
2926 * We check this by travsersing the target directory upwards looking
2927 * for a match against the source.
2930 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
2931 if (fromnd->nl_nch.ncp == ncp) {
2942 * Even though the namespaces are different, they may still represent
2943 * hardlinks to the same file. The filesystem might have a hard time
2944 * with this so we issue a NREMOVE of the source instead of a NRENAME
2945 * when we detect the situation.
2948 if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
2949 error = VOP_NREMOVE(&fromnd->nl_nch, fromnd->nl_cred);
2951 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
2959 * rename_args(char *from, char *to)
2961 * Rename files. Source and destination must either both be directories,
2962 * or both not be directories. If target is a directory, it must be empty.
2965 sys_rename(struct rename_args *uap)
2967 struct nlookupdata fromnd, tond;
2970 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2972 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2974 error = kern_rename(&fromnd, &tond);
2975 nlookup_done(&tond);
2977 nlookup_done(&fromnd);
2982 kern_mkdir(struct nlookupdata *nd, int mode)
2984 struct thread *td = curthread;
2985 struct proc *p = td->td_proc;
2991 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2992 if ((error = nlookup(nd)) != 0)
2995 if (nd->nl_nch.ncp->nc_vp)
2997 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3001 vattr.va_type = VDIR;
3002 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3005 error = VOP_NMKDIR(&nd->nl_nch, &vp, p->p_ucred, &vattr);
3012 * mkdir_args(char *path, int mode)
3014 * Make a directory file.
3018 sys_mkdir(struct mkdir_args *uap)
3020 struct nlookupdata nd;
3023 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3025 error = kern_mkdir(&nd, uap->mode);
3031 kern_rmdir(struct nlookupdata *nd)
3036 nd->nl_flags |= NLC_DELETE;
3037 if ((error = nlookup(nd)) != 0)
3041 * Do not allow directories representing mount points to be
3042 * deleted, even if empty. Check write perms on mount point
3043 * in case the vnode is aliased (aka nullfs).
3045 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3047 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3050 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_cred);
3055 * rmdir_args(char *path)
3057 * Remove a directory file.
3061 sys_rmdir(struct rmdir_args *uap)
3063 struct nlookupdata nd;
3066 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3068 error = kern_rmdir(&nd);
3074 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3075 enum uio_seg direction)
3077 struct thread *td = curthread;
3078 struct proc *p = td->td_proc;
3086 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3088 if ((fp->f_flag & FREAD) == 0) {
3092 vp = (struct vnode *)fp->f_data;
3094 if (vp->v_type != VDIR) {
3098 aiov.iov_base = buf;
3099 aiov.iov_len = count;
3100 auio.uio_iov = &aiov;
3101 auio.uio_iovcnt = 1;
3102 auio.uio_rw = UIO_READ;
3103 auio.uio_segflg = direction;
3105 auio.uio_resid = count;
3106 loff = auio.uio_offset = fp->f_offset;
3107 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3108 fp->f_offset = auio.uio_offset;
3111 if (count == auio.uio_resid) {
3112 if (union_dircheckp) {
3113 error = union_dircheckp(td, &vp, fp);
3120 if ((vp->v_flag & VROOT) &&
3121 (vp->v_mount->mnt_flag & MNT_UNION)) {
3122 struct vnode *tvp = vp;
3123 vp = vp->v_mount->mnt_vnodecovered;
3135 *res = count - auio.uio_resid;
3142 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3144 * Read a block of directory entries in a file system independent format.
3147 sys_getdirentries(struct getdirentries_args *uap)
3152 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3153 &uap->sysmsg_result, UIO_USERSPACE);
3156 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3161 * getdents_args(int fd, char *buf, size_t count)
3164 sys_getdents(struct getdents_args *uap)
3168 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3169 &uap->sysmsg_result, UIO_USERSPACE);
3175 * umask(int newmask)
3177 * Set the mode mask for creation of filesystem nodes.
3182 sys_umask(struct umask_args *uap)
3184 struct thread *td = curthread;
3185 struct proc *p = td->td_proc;
3186 struct filedesc *fdp;
3189 uap->sysmsg_result = fdp->fd_cmask;
3190 fdp->fd_cmask = uap->newmask & ALLPERMS;
3195 * revoke(char *path)
3197 * Void all references to file by ripping underlying filesystem
3202 sys_revoke(struct revoke_args *uap)
3204 struct nlookupdata nd;
3211 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3213 error = nlookup(&nd);
3215 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3216 cred = crhold(nd.nl_cred);
3219 if (vp->v_type != VCHR && vp->v_type != VBLK)
3222 error = VOP_GETATTR(vp, &vattr);
3223 if (error == 0 && cred->cr_uid != vattr.va_uid)
3224 error = suser_cred(cred, PRISON_ROOT);
3225 if (error == 0 && count_udev(vp->v_udev) > 0) {
3228 VOP_REVOKE(vp, REVOKEALL);
3239 * getfh_args(char *fname, fhandle_t *fhp)
3241 * Get (NFS) file handle
3244 sys_getfh(struct getfh_args *uap)
3246 struct thread *td = curthread;
3247 struct nlookupdata nd;
3253 * Must be super user
3255 if ((error = suser(td)) != 0)
3259 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3261 error = nlookup(&nd);
3263 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3266 bzero(&fh, sizeof(fh));
3267 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3268 error = VFS_VPTOFH(vp, &fh.fh_fid);
3271 error = copyout(&fh, uap->fhp, sizeof(fh));
3277 * fhopen_args(const struct fhandle *u_fhp, int flags)
3279 * syscall for the rpc.lockd to use to translate a NFS file handle into
3280 * an open descriptor.
3282 * warning: do not remove the suser() call or this becomes one giant
3286 sys_fhopen(struct fhopen_args *uap)
3288 struct thread *td = curthread;
3289 struct proc *p = td->td_proc;
3294 struct vattr *vap = &vat;
3296 int fmode, mode, error, type;
3302 * Must be super user
3308 fmode = FFLAGS(uap->flags);
3309 /* why not allow a non-read/write open for our lockd? */
3310 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3312 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3315 /* find the mount point */
3316 mp = vfs_getvfs(&fhp.fh_fsid);
3319 /* now give me my vnode, it gets returned to me locked */
3320 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3324 * from now on we have to make sure not
3325 * to forget about the vnode
3326 * any error that causes an abort must vput(vp)
3327 * just set error = err and 'goto bad;'.
3333 if (vp->v_type == VLNK) {
3337 if (vp->v_type == VSOCK) {
3342 if (fmode & (FWRITE | O_TRUNC)) {
3343 if (vp->v_type == VDIR) {
3347 error = vn_writechk(vp, NULL);
3355 error = VOP_ACCESS(vp, mode, p->p_ucred);
3359 if (fmode & O_TRUNC) {
3360 vn_unlock(vp); /* XXX */
3361 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3364 error = VOP_SETATTR(vp, vap, p->p_ucred);
3370 * VOP_OPEN needs the file pointer so it can potentially override
3373 * WARNING! no f_nchandle will be associated when fhopen()ing a
3376 if ((error = falloc(p, &nfp, &indx)) != 0)
3380 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3383 * setting f_ops this way prevents VOP_CLOSE from being
3384 * called or fdrop() releasing the vp from v_data. Since
3385 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3387 fp->f_ops = &badfileops;
3393 * The fp is given its own reference, we still have our ref and lock.
3395 * Assert that all regular files must be created with a VM object.
3397 if (vp->v_type == VREG && vp->v_object == NULL) {
3398 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3403 * The open was successful. Handle any locking requirements.
3405 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3406 lf.l_whence = SEEK_SET;
3409 if (fmode & O_EXLOCK)
3410 lf.l_type = F_WRLCK;
3412 lf.l_type = F_RDLCK;
3413 if (fmode & FNONBLOCK)
3418 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3420 * release our private reference.
3422 fsetfd(p, NULL, indx);
3427 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3428 fp->f_flag |= FHASLOCK;
3432 * Clean up. Associate the file pointer with the previously
3433 * reserved descriptor and return it.
3436 fsetfd(p, fp, indx);
3438 uap->sysmsg_result = indx;
3442 fsetfd(p, NULL, indx);
3450 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3453 sys_fhstat(struct fhstat_args *uap)
3455 struct thread *td = curthread;
3463 * Must be super user
3469 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3473 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3475 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3477 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3481 error = copyout(&sb, uap->sb, sizeof(sb));
3486 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3489 sys_fhstatfs(struct fhstatfs_args *uap)
3491 struct thread *td = curthread;
3492 struct proc *p = td->td_proc;
3497 char *fullpath, *freepath;
3502 * Must be super user
3504 if ((error = suser(td)))
3507 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3510 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3513 if (p != NULL && !chroot_visible_mnt(mp, p))
3516 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3521 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3524 error = cache_fullpath(p, &mp->mnt_ncmountpt, &fullpath, &freepath);
3527 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3528 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3529 kfree(freepath, M_TEMP);
3531 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3533 bcopy(sp, &sb, sizeof(sb));
3534 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3537 return (copyout(sp, uap->buf, sizeof(*sp)));
3541 * Syscall to push extended attribute configuration information into the
3542 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3543 * a command (int cmd), and attribute name and misc data. For now, the
3544 * attribute name is left in userspace for consumption by the VFS_op.
3545 * It will probably be changed to be copied into sysspace by the
3546 * syscall in the future, once issues with various consumers of the
3547 * attribute code have raised their hands.
3549 * Currently this is used only by UFS Extended Attributes.
3552 sys_extattrctl(struct extattrctl_args *uap)
3554 struct nlookupdata nd;
3560 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3562 error = nlookup(&nd);
3564 mp = nd.nl_nch.mount;
3565 error = VFS_EXTATTRCTL(mp, uap->cmd,
3566 uap->attrname, uap->arg,
3574 * Syscall to set a named extended attribute on a file or directory.
3575 * Accepts attribute name, and a uio structure pointing to the data to set.
3576 * The uio is consumed in the style of writev(). The real work happens
3577 * in VOP_SETEXTATTR().
3580 sys_extattr_set_file(struct extattr_set_file_args *uap)
3582 char attrname[EXTATTR_MAXNAMELEN];
3583 struct iovec aiov[UIO_SMALLIOV];
3584 struct iovec *needfree;
3585 struct nlookupdata nd;
3594 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3599 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3601 error = nlookup(&nd);
3603 error = ncp_writechk(&nd.nl_nch);
3605 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3612 iovlen = uap->iovcnt * sizeof(struct iovec);
3613 if (uap->iovcnt > UIO_SMALLIOV) {
3614 if (uap->iovcnt > UIO_MAXIOV) {
3618 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3624 auio.uio_iovcnt = uap->iovcnt;
3625 auio.uio_rw = UIO_WRITE;
3626 auio.uio_segflg = UIO_USERSPACE;
3627 auio.uio_td = nd.nl_td;
3628 auio.uio_offset = 0;
3629 if ((error = copyin(uap->iovp, iov, iovlen)))
3632 for (i = 0; i < uap->iovcnt; i++) {
3633 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3637 auio.uio_resid += iov->iov_len;
3640 cnt = auio.uio_resid;
3641 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3642 cnt -= auio.uio_resid;
3643 uap->sysmsg_result = cnt;
3648 FREE(needfree, M_IOV);
3653 * Syscall to get a named extended attribute on a file or directory.
3654 * Accepts attribute name, and a uio structure pointing to a buffer for the
3655 * data. The uio is consumed in the style of readv(). The real work
3656 * happens in VOP_GETEXTATTR();
3659 sys_extattr_get_file(struct extattr_get_file_args *uap)
3661 char attrname[EXTATTR_MAXNAMELEN];
3662 struct iovec aiov[UIO_SMALLIOV];
3663 struct iovec *needfree;
3664 struct nlookupdata nd;
3673 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3678 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3680 error = nlookup(&nd);
3682 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3688 iovlen = uap->iovcnt * sizeof (struct iovec);
3690 if (uap->iovcnt > UIO_SMALLIOV) {
3691 if (uap->iovcnt > UIO_MAXIOV) {
3695 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3701 auio.uio_iovcnt = uap->iovcnt;
3702 auio.uio_rw = UIO_READ;
3703 auio.uio_segflg = UIO_USERSPACE;
3704 auio.uio_td = nd.nl_td;
3705 auio.uio_offset = 0;
3706 if ((error = copyin(uap->iovp, iov, iovlen)))
3709 for (i = 0; i < uap->iovcnt; i++) {
3710 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3714 auio.uio_resid += iov->iov_len;
3717 cnt = auio.uio_resid;
3718 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3719 cnt -= auio.uio_resid;
3720 uap->sysmsg_result = cnt;
3725 FREE(needfree, M_IOV);
3730 * Syscall to delete a named extended attribute from a file or directory.
3731 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3734 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
3736 char attrname[EXTATTR_MAXNAMELEN];
3737 struct nlookupdata nd;
3741 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3746 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3748 error = nlookup(&nd);
3750 error = ncp_writechk(&nd.nl_nch);
3752 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3758 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3765 * Determine if the mount is visible to the process.
3768 chroot_visible_mnt(struct mount *mp, struct proc *p)
3770 struct nchandle nch;
3773 * Traverse from the mount point upwards. If we hit the process
3774 * root then the mount point is visible to the process.
3776 nch = mp->mnt_ncmountpt;
3778 if (nch.mount == p->p_fd->fd_nrdir.mount &&
3779 nch.ncp == p->p_fd->fd_nrdir.ncp) {
3782 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
3783 nch = nch.mount->mnt_ncmounton;
3785 nch.ncp = nch.ncp->nc_parent;