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 $
42 #include <sys/param.h>
43 #include <sys/systm.h>
46 #include <sys/sysent.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/mountctl.h>
50 #include <sys/sysproto.h>
51 #include <sys/filedesc.h>
52 #include <sys/kernel.h>
53 #include <sys/fcntl.h>
55 #include <sys/linker.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
62 #include <sys/namei.h>
63 #include <sys/nlookup.h>
64 #include <sys/dirent.h>
65 #include <sys/extattr.h>
66 #include <sys/spinlock.h>
67 #include <sys/kern_syscall.h>
68 #include <sys/objcache.h>
69 #include <sys/sysctl.h>
72 #include <sys/file2.h>
73 #include <sys/spinlock2.h>
74 #include <sys/mplock2.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
80 #include <machine/limits.h>
81 #include <machine/stdarg.h>
83 #include <vfs/union/union.h>
85 static void mount_warning(struct mount *mp, const char *ctl, ...)
87 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
88 static int checkvp_chdir (struct vnode *vn, struct thread *td);
89 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
90 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
91 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
92 static int getutimes (const struct timeval *, struct timespec *);
93 static int setfown (struct vnode *, uid_t, gid_t);
94 static int setfmode (struct vnode *, int);
95 static int setfflags (struct vnode *, int);
96 static int setutimes (struct vnode *, struct vattr *,
97 const struct timespec *, int);
98 static int usermount = 0; /* if 1, non-root can mount fs. */
100 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
102 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
103 "Allow non-root users to mount filesystems");
106 * Virtual File System System Calls
110 * Mount a file system.
112 * mount_args(char *type, char *path, int flags, caddr_t data)
117 sys_mount(struct mount_args *uap)
119 struct thread *td = curthread;
122 struct mount *mp, *nullmp;
123 struct vfsconf *vfsp;
124 int error, flag = 0, flag2 = 0;
127 struct nlookupdata nd;
128 char fstypename[MFSNAMELEN];
137 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
141 * Do not allow NFS export by non-root users.
143 if (uap->flags & MNT_EXPORTED) {
144 error = priv_check(td, PRIV_ROOT);
149 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
151 if (priv_check(td, PRIV_ROOT))
152 uap->flags |= MNT_NOSUID | MNT_NODEV;
155 * Lookup the requested path and extract the nch and vnode.
157 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
159 if ((error = nlookup(&nd)) == 0) {
160 if (nd.nl_nch.ncp->nc_vp == NULL)
170 * If the target filesystem is resolved via a nullfs mount, then
171 * nd.nl_nch.mount will be pointing to the nullfs mount structure
172 * instead of the target file system. We need it in case we are
175 nullmp = nd.nl_nch.mount;
178 * Extract the locked+refd ncp and cleanup the nd structure
181 cache_zero(&nd.nl_nch);
184 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
191 * now we have the locked ref'd nch and unreferenced vnode.
194 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
201 * Extract the file system type. We need to know this early, to take
202 * appropriate actions if we are dealing with a nullfs.
204 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
211 * Now we have an unlocked ref'd nch and a locked ref'd vp
213 if (uap->flags & MNT_UPDATE) {
214 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
221 if (strncmp(fstypename, "null", 5) == 0) {
229 flag2 = mp->mnt_kern_flag;
231 * We only allow the filesystem to be reloaded if it
232 * is currently mounted read-only.
234 if ((uap->flags & MNT_RELOAD) &&
235 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
238 error = EOPNOTSUPP; /* Needs translation */
242 * Only root, or the user that did the original mount is
243 * permitted to update it.
245 if (mp->mnt_stat.f_owner != cred->cr_uid &&
246 (error = priv_check(td, PRIV_ROOT))) {
251 if (vfs_busy(mp, LK_NOWAIT)) {
257 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
264 vsetflags(vp, VMOUNT);
266 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
271 * If the user is not root, ensure that they own the directory
272 * onto which we are attempting to mount.
274 if ((error = VOP_GETATTR(vp, &va)) ||
275 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
280 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
285 if (vp->v_type != VDIR) {
291 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
297 vfsp = vfsconf_find_by_name(fstypename);
301 /* Only load modules for root (very important!) */
302 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
307 error = linker_load_file(fstypename, &lf);
308 if (error || lf == NULL) {
316 /* lookup again, see if the VFS was loaded */
317 vfsp = vfsconf_find_by_name(fstypename);
320 linker_file_unload(lf);
327 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
333 vsetflags(vp, VMOUNT);
336 * Allocate and initialize the filesystem.
338 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
340 vfs_busy(mp, LK_NOWAIT);
341 mp->mnt_op = vfsp->vfc_vfsops;
343 vfsp->vfc_refcount++;
344 mp->mnt_stat.f_type = vfsp->vfc_typenum;
345 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
346 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
347 mp->mnt_stat.f_owner = cred->cr_uid;
351 * Set the mount level flags.
353 if (uap->flags & MNT_RDONLY)
354 mp->mnt_flag |= MNT_RDONLY;
355 else if (mp->mnt_flag & MNT_RDONLY)
356 mp->mnt_kern_flag |= MNTK_WANTRDWR;
357 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
358 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
359 MNT_NOSYMFOLLOW | MNT_IGNORE |
360 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
361 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
362 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
363 MNT_NOSYMFOLLOW | MNT_IGNORE |
364 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
366 * Mount the filesystem.
367 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
370 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
371 if (mp->mnt_flag & MNT_UPDATE) {
372 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
373 mp->mnt_flag &= ~MNT_RDONLY;
374 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
375 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
378 mp->mnt_kern_flag = flag2;
381 vclrflags(vp, VMOUNT);
386 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
388 * Put the new filesystem on the mount list after root. The mount
389 * point gets its own mnt_ncmountpt (unless the VFS already set one
390 * up) which represents the root of the mount. The lookup code
391 * detects the mount point going forward and checks the root of
392 * the mount going backwards.
394 * It is not necessary to invalidate or purge the vnode underneath
395 * because elements under the mount will be given their own glue
399 if (mp->mnt_ncmountpt.ncp == NULL) {
401 * allocate, then unlock, but leave the ref intact
403 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
404 cache_unlock(&mp->mnt_ncmountpt);
406 mp->mnt_ncmounton = nch; /* inherits ref */
407 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
409 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
410 vclrflags(vp, VMOUNT);
411 mountlist_insert(mp, MNTINS_LAST);
413 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
414 error = vfs_allocate_syncvnode(mp);
416 error = VFS_START(mp, 0);
419 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
420 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
421 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
422 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
423 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
424 vclrflags(vp, VMOUNT);
425 mp->mnt_vfc->vfc_refcount--;
437 * Scan all active processes to see if any of them have a current
438 * or root directory onto which the new filesystem has just been
439 * mounted. If so, replace them with the new mount point.
441 * The passed ncp is ref'd and locked (from the mount code) and
442 * must be associated with the vnode representing the root of the
445 struct checkdirs_info {
446 struct nchandle old_nch;
447 struct nchandle new_nch;
448 struct vnode *old_vp;
449 struct vnode *new_vp;
452 static int checkdirs_callback(struct proc *p, void *data);
455 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
457 struct checkdirs_info info;
463 * If the old mount point's vnode has a usecount of 1, it is not
464 * being held as a descriptor anywhere.
466 olddp = old_nch->ncp->nc_vp;
467 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
471 * Force the root vnode of the new mount point to be resolved
472 * so we can update any matching processes.
475 if (VFS_ROOT(mp, &newdp))
476 panic("mount: lost mount");
477 cache_setunresolved(new_nch);
478 cache_setvp(new_nch, newdp);
481 * Special handling of the root node
483 if (rootvnode == olddp) {
485 vfs_cache_setroot(newdp, cache_hold(new_nch));
489 * Pass newdp separately so the callback does not have to access
490 * it via new_nch->ncp->nc_vp.
492 info.old_nch = *old_nch;
493 info.new_nch = *new_nch;
495 allproc_scan(checkdirs_callback, &info);
500 * NOTE: callback is not MP safe because the scanned process's filedesc
501 * structure can be ripped out from under us, amoung other things.
504 checkdirs_callback(struct proc *p, void *data)
506 struct checkdirs_info *info = data;
507 struct filedesc *fdp;
508 struct nchandle ncdrop1;
509 struct nchandle ncdrop2;
510 struct vnode *vprele1;
511 struct vnode *vprele2;
513 if ((fdp = p->p_fd) != NULL) {
514 cache_zero(&ncdrop1);
515 cache_zero(&ncdrop2);
520 * MPUNSAFE - XXX fdp can be pulled out from under a
523 * A shared filedesc is ok, we don't have to copy it
524 * because we are making this change globally.
526 spin_lock(&fdp->fd_spin);
527 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
528 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
529 vprele1 = fdp->fd_cdir;
531 fdp->fd_cdir = info->new_vp;
532 ncdrop1 = fdp->fd_ncdir;
533 cache_copy(&info->new_nch, &fdp->fd_ncdir);
535 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
536 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
537 vprele2 = fdp->fd_rdir;
539 fdp->fd_rdir = info->new_vp;
540 ncdrop2 = fdp->fd_nrdir;
541 cache_copy(&info->new_nch, &fdp->fd_nrdir);
543 spin_unlock(&fdp->fd_spin);
545 cache_drop(&ncdrop1);
547 cache_drop(&ncdrop2);
557 * Unmount a file system.
559 * Note: unmount takes a path to the vnode mounted on as argument,
560 * not special file (as before).
562 * umount_args(char *path, int flags)
567 sys_unmount(struct unmount_args *uap)
569 struct thread *td = curthread;
570 struct proc *p __debugvar = td->td_proc;
571 struct mount *mp = NULL;
572 struct nlookupdata nd;
577 if (td->td_ucred->cr_prison != NULL) {
581 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
584 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
586 error = nlookup(&nd);
590 mp = nd.nl_nch.mount;
593 * Only root, or the user that did the original mount is
594 * permitted to unmount this filesystem.
596 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
597 (error = priv_check(td, PRIV_ROOT)))
601 * Don't allow unmounting the root file system.
603 if (mp->mnt_flag & MNT_ROOTFS) {
609 * Must be the root of the filesystem
611 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
619 error = dounmount(mp, uap->flags);
626 * Do the actual file system unmount.
629 dounmount_interlock(struct mount *mp)
631 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
633 mp->mnt_kern_flag |= MNTK_UNMOUNT;
638 unmount_allproc_cb(struct proc *p, void *arg)
642 if (p->p_textnch.ncp == NULL)
645 mp = (struct mount *)arg;
646 if (p->p_textnch.mount == mp)
647 cache_drop(&p->p_textnch);
653 dounmount(struct mount *mp, int flags)
655 struct namecache *ncp;
664 * Exclusive access for unmounting purposes
666 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
670 * Allow filesystems to detect that a forced unmount is in progress.
672 if (flags & MNT_FORCE)
673 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
674 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
675 error = lockmgr(&mp->mnt_lock, lflags);
677 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
678 if (mp->mnt_kern_flag & MNTK_MWAIT)
683 if (mp->mnt_flag & MNT_EXPUBLIC)
684 vfs_setpublicfs(NULL, NULL, NULL);
686 vfs_msync(mp, MNT_WAIT);
687 async_flag = mp->mnt_flag & MNT_ASYNC;
688 mp->mnt_flag &=~ MNT_ASYNC;
691 * If this filesystem isn't aliasing other filesystems,
692 * try to invalidate any remaining namecache entries and
693 * check the count afterwords.
695 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
696 cache_lock(&mp->mnt_ncmountpt);
697 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
698 cache_unlock(&mp->mnt_ncmountpt);
700 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
701 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
702 allproc_scan(&unmount_allproc_cb, mp);
705 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
706 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
708 if ((flags & MNT_FORCE) == 0) {
710 mount_warning(mp, "Cannot unmount: "
716 mount_warning(mp, "Forced unmount: "
727 * nchandle records ref the mount structure. Expect a count of 1
728 * (our mount->mnt_ncmountpt).
730 if (mp->mnt_refs != 1) {
731 if ((flags & MNT_FORCE) == 0) {
732 mount_warning(mp, "Cannot unmount: "
733 "%d process references still "
734 "present", mp->mnt_refs);
737 mount_warning(mp, "Forced unmount: "
738 "%d process references still "
739 "present", mp->mnt_refs);
745 * Decomission our special mnt_syncer vnode. This also stops
746 * the vnlru code. If we are unable to unmount we recommission
750 if ((vp = mp->mnt_syncer) != NULL) {
751 mp->mnt_syncer = NULL;
754 if (((mp->mnt_flag & MNT_RDONLY) ||
755 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
756 (flags & MNT_FORCE)) {
757 error = VFS_UNMOUNT(mp, flags);
761 if (mp->mnt_syncer == NULL)
762 vfs_allocate_syncvnode(mp);
763 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
764 mp->mnt_flag |= async_flag;
765 lockmgr(&mp->mnt_lock, LK_RELEASE);
766 if (mp->mnt_kern_flag & MNTK_MWAIT)
771 * Clean up any journals still associated with the mount after
772 * filesystem activity has ceased.
774 journal_remove_all_journals(mp,
775 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
777 mountlist_remove(mp);
780 * Remove any installed vnode ops here so the individual VFSs don't
783 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
784 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
785 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
786 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
787 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
789 if (mp->mnt_ncmountpt.ncp != NULL) {
790 nch = mp->mnt_ncmountpt;
791 cache_zero(&mp->mnt_ncmountpt);
792 cache_clrmountpt(&nch);
795 if (mp->mnt_ncmounton.ncp != NULL) {
796 nch = mp->mnt_ncmounton;
797 cache_zero(&mp->mnt_ncmounton);
798 cache_clrmountpt(&nch);
802 mp->mnt_vfc->vfc_refcount--;
803 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
804 panic("unmount: dangling vnode");
805 lockmgr(&mp->mnt_lock, LK_RELEASE);
806 if (mp->mnt_kern_flag & MNTK_MWAIT)
815 mount_warning(struct mount *mp, const char *ctl, ...)
822 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf, 0) == 0) {
823 kprintf("unmount(%s): ", ptr);
828 kprintf("unmount(%p", mp);
829 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
830 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
839 * Shim cache_fullpath() to handle the case where a process is chrooted into
840 * a subdirectory of a mount. In this case if the root mount matches the
841 * process root directory's mount we have to specify the process's root
842 * directory instead of the mount point, because the mount point might
843 * be above the root directory.
847 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
849 struct nchandle *nch;
851 if (p && p->p_fd->fd_nrdir.mount == mp)
852 nch = &p->p_fd->fd_nrdir;
854 nch = &mp->mnt_ncmountpt;
855 return(cache_fullpath(p, nch, rb, fb, 0));
859 * Sync each mounted filesystem.
863 static int syncprt = 0;
864 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
867 static int sync_callback(struct mount *mp, void *data);
870 sys_sync(struct sync_args *uap)
872 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
875 * print out buffer pool stat information on each sync() call.
885 sync_callback(struct mount *mp, void *data __unused)
889 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
890 asyncflag = mp->mnt_flag & MNT_ASYNC;
891 mp->mnt_flag &= ~MNT_ASYNC;
892 vfs_msync(mp, MNT_NOWAIT);
893 VFS_SYNC(mp, MNT_NOWAIT | MNT_LAZY);
894 mp->mnt_flag |= asyncflag;
899 /* XXX PRISON: could be per prison flag */
900 static int prison_quotas;
902 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
906 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
908 * Change filesystem quotas.
913 sys_quotactl(struct quotactl_args *uap)
915 struct nlookupdata nd;
924 if (td->td_ucred->cr_prison && !prison_quotas) {
929 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
931 error = nlookup(&nd);
933 mp = nd.nl_nch.mount;
934 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
935 uap->arg, nd.nl_cred);
944 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
945 * void *buf, int buflen)
947 * This function operates on a mount point and executes the specified
948 * operation using the specified control data, and possibly returns data.
950 * The actual number of bytes stored in the result buffer is returned, 0
951 * if none, otherwise an error is returned.
956 sys_mountctl(struct mountctl_args *uap)
958 struct thread *td = curthread;
959 struct proc *p = td->td_proc;
967 * Sanity and permissions checks. We must be root.
970 if (td->td_ucred->cr_prison != NULL)
972 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
973 (error = priv_check(td, PRIV_ROOT)) != 0)
977 * Argument length checks
979 if (uap->ctllen < 0 || uap->ctllen > 1024)
981 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
983 if (uap->path == NULL)
987 * Allocate the necessary buffers and copyin data
989 path = objcache_get(namei_oc, M_WAITOK);
990 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
995 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
996 error = copyin(uap->ctl, ctl, uap->ctllen);
1001 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1004 * Validate the descriptor
1007 fp = holdfp(p->p_fd, uap->fd, -1);
1017 * Execute the internal kernel function and clean up.
1020 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1024 if (error == 0 && uap->sysmsg_result > 0)
1025 error = copyout(buf, uap->buf, uap->sysmsg_result);
1028 objcache_put(namei_oc, path);
1037 * Execute a mount control operation by resolving the path to a mount point
1038 * and calling vop_mountctl().
1040 * Use the mount point from the nch instead of the vnode so nullfs mounts
1041 * can properly spike the VOP.
1044 kern_mountctl(const char *path, int op, struct file *fp,
1045 const void *ctl, int ctllen,
1046 void *buf, int buflen, int *res)
1050 struct nlookupdata nd;
1055 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1057 error = nlookup(&nd);
1059 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1060 mp = nd.nl_nch.mount;
1067 * Must be the root of the filesystem
1069 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1073 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1080 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1082 struct thread *td = curthread;
1083 struct proc *p = td->td_proc;
1086 char *fullpath, *freepath;
1089 if ((error = nlookup(nd)) != 0)
1091 mp = nd->nl_nch.mount;
1093 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1096 error = mount_path(p, mp, &fullpath, &freepath);
1099 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1100 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1101 kfree(freepath, M_TEMP);
1103 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1104 bcopy(sp, buf, sizeof(*buf));
1105 /* Only root should have access to the fsid's. */
1106 if (priv_check(td, PRIV_ROOT))
1107 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1112 * statfs_args(char *path, struct statfs *buf)
1114 * Get filesystem statistics.
1117 sys_statfs(struct statfs_args *uap)
1119 struct nlookupdata nd;
1123 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1125 error = kern_statfs(&nd, &buf);
1128 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1133 kern_fstatfs(int fd, struct statfs *buf)
1135 struct thread *td = curthread;
1136 struct proc *p = td->td_proc;
1140 char *fullpath, *freepath;
1144 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1146 mp = ((struct vnode *)fp->f_data)->v_mount;
1151 if (fp->f_cred == NULL) {
1156 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1159 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1161 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1162 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1163 kfree(freepath, M_TEMP);
1165 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1166 bcopy(sp, buf, sizeof(*buf));
1168 /* Only root should have access to the fsid's. */
1169 if (priv_check(td, PRIV_ROOT))
1170 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1178 * fstatfs_args(int fd, struct statfs *buf)
1180 * Get filesystem statistics.
1183 sys_fstatfs(struct fstatfs_args *uap)
1188 error = kern_fstatfs(uap->fd, &buf);
1191 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1196 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1202 if ((error = nlookup(nd)) != 0)
1204 mp = nd->nl_nch.mount;
1205 sp = &mp->mnt_vstat;
1206 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1210 if (mp->mnt_flag & MNT_RDONLY)
1211 sp->f_flag |= ST_RDONLY;
1212 if (mp->mnt_flag & MNT_NOSUID)
1213 sp->f_flag |= ST_NOSUID;
1214 bcopy(sp, buf, sizeof(*buf));
1219 * statfs_args(char *path, struct statfs *buf)
1221 * Get filesystem statistics.
1224 sys_statvfs(struct statvfs_args *uap)
1226 struct nlookupdata nd;
1230 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1232 error = kern_statvfs(&nd, &buf);
1235 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1240 kern_fstatvfs(int fd, struct statvfs *buf)
1242 struct thread *td = curthread;
1243 struct proc *p = td->td_proc;
1250 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1252 mp = ((struct vnode *)fp->f_data)->v_mount;
1257 if (fp->f_cred == NULL) {
1261 sp = &mp->mnt_vstat;
1262 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1266 if (mp->mnt_flag & MNT_RDONLY)
1267 sp->f_flag |= ST_RDONLY;
1268 if (mp->mnt_flag & MNT_NOSUID)
1269 sp->f_flag |= ST_NOSUID;
1271 bcopy(sp, buf, sizeof(*buf));
1279 * fstatfs_args(int fd, struct statfs *buf)
1281 * Get filesystem statistics.
1284 sys_fstatvfs(struct fstatvfs_args *uap)
1289 error = kern_fstatvfs(uap->fd, &buf);
1292 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1297 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1299 * Get statistics on all filesystems.
1302 struct getfsstat_info {
1303 struct statfs *sfsp;
1311 static int getfsstat_callback(struct mount *, void *);
1314 sys_getfsstat(struct getfsstat_args *uap)
1316 struct thread *td = curthread;
1317 struct getfsstat_info info;
1319 bzero(&info, sizeof(info));
1321 info.maxcount = uap->bufsize / sizeof(struct statfs);
1322 info.sfsp = uap->buf;
1324 info.flags = uap->flags;
1327 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1328 if (info.sfsp && info.count > info.maxcount)
1329 uap->sysmsg_result = info.maxcount;
1331 uap->sysmsg_result = info.count;
1332 return (info.error);
1336 getfsstat_callback(struct mount *mp, void *data)
1338 struct getfsstat_info *info = data;
1344 if (info->sfsp && info->count < info->maxcount) {
1345 if (info->td->td_proc &&
1346 !chroot_visible_mnt(mp, info->td->td_proc)) {
1352 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1353 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1354 * overrides MNT_WAIT.
1356 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1357 (info->flags & MNT_WAIT)) &&
1358 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1361 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1363 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1365 info->error = error;
1368 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1369 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1370 kfree(freepath, M_TEMP);
1372 error = copyout(sp, info->sfsp, sizeof(*sp));
1374 info->error = error;
1384 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1385 long bufsize, int flags)
1387 * Get statistics on all filesystems.
1390 struct getvfsstat_info {
1391 struct statfs *sfsp;
1392 struct statvfs *vsfsp;
1400 static int getvfsstat_callback(struct mount *, void *);
1403 sys_getvfsstat(struct getvfsstat_args *uap)
1405 struct thread *td = curthread;
1406 struct getvfsstat_info info;
1408 bzero(&info, sizeof(info));
1410 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1411 info.sfsp = uap->buf;
1412 info.vsfsp = uap->vbuf;
1414 info.flags = uap->flags;
1417 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1418 if (info.vsfsp && info.count > info.maxcount)
1419 uap->sysmsg_result = info.maxcount;
1421 uap->sysmsg_result = info.count;
1422 return (info.error);
1426 getvfsstat_callback(struct mount *mp, void *data)
1428 struct getvfsstat_info *info = data;
1430 struct statvfs *vsp;
1435 if (info->vsfsp && info->count < info->maxcount) {
1436 if (info->td->td_proc &&
1437 !chroot_visible_mnt(mp, info->td->td_proc)) {
1441 vsp = &mp->mnt_vstat;
1444 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1445 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1446 * overrides MNT_WAIT.
1448 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1449 (info->flags & MNT_WAIT)) &&
1450 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1453 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1455 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1456 (info->flags & MNT_WAIT)) &&
1457 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1461 if (mp->mnt_flag & MNT_RDONLY)
1462 vsp->f_flag |= ST_RDONLY;
1463 if (mp->mnt_flag & MNT_NOSUID)
1464 vsp->f_flag |= ST_NOSUID;
1466 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1468 info->error = error;
1471 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1472 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1473 kfree(freepath, M_TEMP);
1475 error = copyout(sp, info->sfsp, sizeof(*sp));
1477 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1479 info->error = error;
1491 * fchdir_args(int fd)
1493 * Change current working directory to a given file descriptor.
1496 sys_fchdir(struct fchdir_args *uap)
1498 struct thread *td = curthread;
1499 struct proc *p = td->td_proc;
1500 struct filedesc *fdp = p->p_fd;
1501 struct vnode *vp, *ovp;
1504 struct nchandle nch, onch, tnch;
1507 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1509 lwkt_gettoken(&p->p_token);
1510 vp = (struct vnode *)fp->f_data;
1512 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1513 if (fp->f_nchandle.ncp == NULL)
1516 error = checkvp_chdir(vp, td);
1521 cache_copy(&fp->f_nchandle, &nch);
1524 * If the ncp has become a mount point, traverse through
1528 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1529 (mp = cache_findmount(&nch)) != NULL
1531 error = nlookup_mp(mp, &tnch);
1533 cache_unlock(&tnch); /* leave ref intact */
1535 vp = tnch.ncp->nc_vp;
1536 error = vget(vp, LK_SHARED);
1537 KKASSERT(error == 0);
1544 onch = fdp->fd_ncdir;
1545 vn_unlock(vp); /* leave ref intact */
1547 fdp->fd_ncdir = nch;
1556 lwkt_reltoken(&p->p_token);
1561 kern_chdir(struct nlookupdata *nd)
1563 struct thread *td = curthread;
1564 struct proc *p = td->td_proc;
1565 struct filedesc *fdp = p->p_fd;
1566 struct vnode *vp, *ovp;
1567 struct nchandle onch;
1570 if ((error = nlookup(nd)) != 0)
1572 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1574 if ((error = vget(vp, LK_SHARED)) != 0)
1577 lwkt_gettoken(&p->p_token);
1578 error = checkvp_chdir(vp, td);
1582 onch = fdp->fd_ncdir;
1583 cache_unlock(&nd->nl_nch); /* leave reference intact */
1584 fdp->fd_ncdir = nd->nl_nch;
1588 cache_zero(&nd->nl_nch);
1592 lwkt_reltoken(&p->p_token);
1597 * chdir_args(char *path)
1599 * Change current working directory (``.'').
1602 sys_chdir(struct chdir_args *uap)
1604 struct nlookupdata nd;
1607 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1609 error = kern_chdir(&nd);
1615 * Helper function for raised chroot(2) security function: Refuse if
1616 * any filedescriptors are open directories.
1619 chroot_refuse_vdir_fds(struct filedesc *fdp)
1626 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1627 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1629 vp = (struct vnode *)fp->f_data;
1630 if (vp->v_type != VDIR) {
1641 * This sysctl determines if we will allow a process to chroot(2) if it
1642 * has a directory open:
1643 * 0: disallowed for all processes.
1644 * 1: allowed for processes that were not already chroot(2)'ed.
1645 * 2: allowed for all processes.
1648 static int chroot_allow_open_directories = 1;
1650 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1651 &chroot_allow_open_directories, 0, "");
1654 * chroot to the specified namecache entry. We obtain the vp from the
1655 * namecache data. The passed ncp must be locked and referenced and will
1656 * remain locked and referenced on return.
1659 kern_chroot(struct nchandle *nch)
1661 struct thread *td = curthread;
1662 struct proc *p = td->td_proc;
1663 struct filedesc *fdp = p->p_fd;
1668 * Only privileged user can chroot
1670 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1675 * Disallow open directory descriptors (fchdir() breakouts).
1677 if (chroot_allow_open_directories == 0 ||
1678 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1679 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1682 if ((vp = nch->ncp->nc_vp) == NULL)
1685 if ((error = vget(vp, LK_SHARED)) != 0)
1689 * Check the validity of vp as a directory to change to and
1690 * associate it with rdir/jdir.
1692 error = checkvp_chdir(vp, td);
1693 vn_unlock(vp); /* leave reference intact */
1695 vrele(fdp->fd_rdir);
1696 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1697 cache_drop(&fdp->fd_nrdir);
1698 cache_copy(nch, &fdp->fd_nrdir);
1699 if (fdp->fd_jdir == NULL) {
1702 cache_copy(nch, &fdp->fd_njdir);
1711 * chroot_args(char *path)
1713 * Change notion of root (``/'') directory.
1716 sys_chroot(struct chroot_args *uap)
1718 struct thread *td __debugvar = curthread;
1719 struct nlookupdata nd;
1722 KKASSERT(td->td_proc);
1723 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1725 nd.nl_flags |= NLC_EXEC;
1726 error = nlookup(&nd);
1728 error = kern_chroot(&nd.nl_nch);
1735 sys_chroot_kernel(struct chroot_kernel_args *uap)
1737 struct thread *td = curthread;
1738 struct nlookupdata nd;
1739 struct nchandle *nch;
1743 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1747 error = nlookup(&nd);
1753 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1757 if ((vp = nch->ncp->nc_vp) == NULL) {
1762 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1765 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1767 vfs_cache_setroot(vp, cache_hold(nch));
1777 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1778 * determine whether it is legal to chdir to the vnode. The vnode's state
1779 * is not changed by this call.
1782 checkvp_chdir(struct vnode *vp, struct thread *td)
1786 if (vp->v_type != VDIR)
1789 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1794 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1796 struct thread *td = curthread;
1797 struct proc *p = td->td_proc;
1798 struct lwp *lp = td->td_lwp;
1799 struct filedesc *fdp = p->p_fd;
1804 int type, indx, error;
1807 if ((oflags & O_ACCMODE) == O_ACCMODE)
1809 flags = FFLAGS(oflags);
1810 error = falloc(lp, &nfp, NULL);
1814 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1817 * XXX p_dupfd is a real mess. It allows a device to return a
1818 * file descriptor to be duplicated rather then doing the open
1824 * Call vn_open() to do the lookup and assign the vnode to the
1825 * file pointer. vn_open() does not change the ref count on fp
1826 * and the vnode, on success, will be inherited by the file pointer
1829 nd->nl_flags |= NLC_LOCKVP;
1830 error = vn_open(nd, fp, flags, cmode);
1834 * handle special fdopen() case. bleh. dupfdopen() is
1835 * responsible for dropping the old contents of ofiles[indx]
1838 * Note that fsetfd() will add a ref to fp which represents
1839 * the fd_files[] assignment. We must still drop our
1842 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1843 if (fdalloc(p, 0, &indx) == 0) {
1844 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1847 fdrop(fp); /* our ref */
1850 fsetfd(fdp, NULL, indx);
1853 fdrop(fp); /* our ref */
1854 if (error == ERESTART)
1860 * ref the vnode for ourselves so it can't be ripped out from under
1861 * is. XXX need an ND flag to request that the vnode be returned
1864 * Reserve a file descriptor but do not assign it until the open
1867 vp = (struct vnode *)fp->f_data;
1869 if ((error = fdalloc(p, 0, &indx)) != 0) {
1876 * If no error occurs the vp will have been assigned to the file
1881 if (flags & (O_EXLOCK | O_SHLOCK)) {
1882 lf.l_whence = SEEK_SET;
1885 if (flags & O_EXLOCK)
1886 lf.l_type = F_WRLCK;
1888 lf.l_type = F_RDLCK;
1889 if (flags & FNONBLOCK)
1894 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1896 * lock request failed. Clean up the reserved
1900 fsetfd(fdp, NULL, indx);
1904 fp->f_flag |= FHASLOCK;
1908 * Assert that all regular file vnodes were created with a object.
1910 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1911 ("open: regular file has no backing object after vn_open"));
1917 * release our private reference, leaving the one associated with the
1918 * descriptor table intact.
1920 fsetfd(fdp, fp, indx);
1927 * open_args(char *path, int flags, int mode)
1929 * Check permissions, allocate an open file structure,
1930 * and call the device open routine if any.
1933 sys_open(struct open_args *uap)
1935 struct nlookupdata nd;
1938 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1940 error = kern_open(&nd, uap->flags,
1941 uap->mode, &uap->sysmsg_result);
1948 * openat_args(int fd, char *path, int flags, int mode)
1951 sys_openat(struct openat_args *uap)
1953 struct nlookupdata nd;
1957 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
1959 error = kern_open(&nd, uap->flags, uap->mode,
1960 &uap->sysmsg_result);
1962 nlookup_done_at(&nd, fp);
1967 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1969 struct thread *td = curthread;
1970 struct proc *p = td->td_proc;
1979 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1980 vattr.va_rmajor = rmajor;
1981 vattr.va_rminor = rminor;
1983 switch (mode & S_IFMT) {
1984 case S_IFMT: /* used by badsect to flag bad sectors */
1985 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
1986 vattr.va_type = VBAD;
1989 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1990 vattr.va_type = VCHR;
1993 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1994 vattr.va_type = VBLK;
1997 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2000 case S_IFDIR: /* special directories support for HAMMER */
2001 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2002 vattr.va_type = VDIR;
2013 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2014 if ((error = nlookup(nd)) != 0)
2016 if (nd->nl_nch.ncp->nc_vp)
2018 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2022 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2023 nd->nl_cred, NAMEI_CREATE);
2026 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2027 &vp, nd->nl_cred, &vattr);
2035 * mknod_args(char *path, int mode, int dev)
2037 * Create a special file.
2040 sys_mknod(struct mknod_args *uap)
2042 struct nlookupdata nd;
2045 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2047 error = kern_mknod(&nd, uap->mode,
2048 umajor(uap->dev), uminor(uap->dev));
2055 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2057 * Create a special file. The path is relative to the directory associated
2061 sys_mknodat(struct mknodat_args *uap)
2063 struct nlookupdata nd;
2067 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2069 error = kern_mknod(&nd, uap->mode,
2070 umajor(uap->dev), uminor(uap->dev));
2072 nlookup_done_at(&nd, fp);
2077 kern_mkfifo(struct nlookupdata *nd, int mode)
2079 struct thread *td = curthread;
2080 struct proc *p = td->td_proc;
2087 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2088 if ((error = nlookup(nd)) != 0)
2090 if (nd->nl_nch.ncp->nc_vp)
2092 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2096 vattr.va_type = VFIFO;
2097 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2099 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2106 * mkfifo_args(char *path, int mode)
2108 * Create a named pipe.
2111 sys_mkfifo(struct mkfifo_args *uap)
2113 struct nlookupdata nd;
2116 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2118 error = kern_mkfifo(&nd, uap->mode);
2124 * mkfifoat_args(int fd, char *path, mode_t mode)
2126 * Create a named pipe. The path is relative to the directory associated
2130 sys_mkfifoat(struct mkfifoat_args *uap)
2132 struct nlookupdata nd;
2136 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2138 error = kern_mkfifo(&nd, uap->mode);
2139 nlookup_done_at(&nd, fp);
2143 static int hardlink_check_uid = 0;
2144 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2145 &hardlink_check_uid, 0,
2146 "Unprivileged processes cannot create hard links to files owned by other "
2148 static int hardlink_check_gid = 0;
2149 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2150 &hardlink_check_gid, 0,
2151 "Unprivileged processes cannot create hard links to files owned by other "
2155 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2161 * Shortcut if disabled
2163 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2167 * Privileged user can always hardlink
2169 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2173 * Otherwise only if the originating file is owned by the
2174 * same user or group. Note that any group is allowed if
2175 * the file is owned by the caller.
2177 error = VOP_GETATTR(vp, &va);
2181 if (hardlink_check_uid) {
2182 if (cred->cr_uid != va.va_uid)
2186 if (hardlink_check_gid) {
2187 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2195 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2197 struct thread *td = curthread;
2202 * Lookup the source and obtained a locked vnode.
2204 * You may only hardlink a file which you have write permission
2205 * on or which you own.
2207 * XXX relookup on vget failure / race ?
2210 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2211 if ((error = nlookup(nd)) != 0)
2213 vp = nd->nl_nch.ncp->nc_vp;
2214 KKASSERT(vp != NULL);
2215 if (vp->v_type == VDIR)
2216 return (EPERM); /* POSIX */
2217 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2219 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2223 * Unlock the source so we can lookup the target without deadlocking
2224 * (XXX vp is locked already, possible other deadlock?). The target
2227 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2228 nd->nl_flags &= ~NLC_NCPISLOCKED;
2229 cache_unlock(&nd->nl_nch);
2232 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2233 if ((error = nlookup(linknd)) != 0) {
2237 if (linknd->nl_nch.ncp->nc_vp) {
2241 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2247 * Finally run the new API VOP.
2249 error = can_hardlink(vp, td, td->td_ucred);
2251 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2252 vp, linknd->nl_cred);
2259 * link_args(char *path, char *link)
2261 * Make a hard file link.
2264 sys_link(struct link_args *uap)
2266 struct nlookupdata nd, linknd;
2269 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2271 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2273 error = kern_link(&nd, &linknd);
2274 nlookup_done(&linknd);
2281 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2289 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2290 if ((error = nlookup(nd)) != 0)
2292 if (nd->nl_nch.ncp->nc_vp)
2294 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2298 vattr.va_mode = mode;
2299 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2306 * symlink(char *path, char *link)
2308 * Make a symbolic link.
2311 sys_symlink(struct symlink_args *uap)
2313 struct thread *td = curthread;
2314 struct nlookupdata nd;
2319 path = objcache_get(namei_oc, M_WAITOK);
2320 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2322 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2324 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2325 error = kern_symlink(&nd, path, mode);
2329 objcache_put(namei_oc, path);
2334 * symlinkat_args(char *path1, int fd, char *path2)
2336 * Make a symbolic link. The path2 argument is relative to the directory
2337 * associated with fd.
2340 sys_symlinkat(struct symlinkat_args *uap)
2342 struct thread *td = curthread;
2343 struct nlookupdata nd;
2349 path1 = objcache_get(namei_oc, M_WAITOK);
2350 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2352 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2355 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2356 error = kern_symlink(&nd, path1, mode);
2358 nlookup_done_at(&nd, fp);
2360 objcache_put(namei_oc, path1);
2365 * undelete_args(char *path)
2367 * Delete a whiteout from the filesystem.
2370 sys_undelete(struct undelete_args *uap)
2372 struct nlookupdata nd;
2375 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2377 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2379 error = nlookup(&nd);
2381 error = ncp_writechk(&nd.nl_nch);
2383 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2391 kern_unlink(struct nlookupdata *nd)
2396 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2397 if ((error = nlookup(nd)) != 0)
2399 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2401 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2406 * unlink_args(char *path)
2408 * Delete a name from the filesystem.
2411 sys_unlink(struct unlink_args *uap)
2413 struct nlookupdata nd;
2416 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2418 error = kern_unlink(&nd);
2425 * unlinkat_args(int fd, char *path, int flags)
2427 * Delete the file or directory entry pointed to by fd/path.
2430 sys_unlinkat(struct unlinkat_args *uap)
2432 struct nlookupdata nd;
2436 if (uap->flags & ~AT_REMOVEDIR)
2439 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2441 if (uap->flags & AT_REMOVEDIR)
2442 error = kern_rmdir(&nd);
2444 error = kern_unlink(&nd);
2446 nlookup_done_at(&nd, fp);
2451 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2453 struct thread *td = curthread;
2454 struct proc *p = td->td_proc;
2461 fp = holdfp(p->p_fd, fd, -1);
2464 if (fp->f_type != DTYPE_VNODE) {
2468 vp = (struct vnode *)fp->f_data;
2472 spin_lock(&fp->f_spin);
2473 new_offset = fp->f_offset + offset;
2477 error = VOP_GETATTR(vp, &vattr);
2478 spin_lock(&fp->f_spin);
2479 new_offset = offset + vattr.va_size;
2482 new_offset = offset;
2484 spin_lock(&fp->f_spin);
2489 spin_lock(&fp->f_spin);
2494 * Validate the seek position. Negative offsets are not allowed
2495 * for regular files or directories.
2497 * Normally we would also not want to allow negative offsets for
2498 * character and block-special devices. However kvm addresses
2499 * on 64 bit architectures might appear to be negative and must
2503 if (new_offset < 0 &&
2504 (vp->v_type == VREG || vp->v_type == VDIR)) {
2507 fp->f_offset = new_offset;
2510 *res = fp->f_offset;
2511 spin_unlock(&fp->f_spin);
2518 * lseek_args(int fd, int pad, off_t offset, int whence)
2520 * Reposition read/write file offset.
2523 sys_lseek(struct lseek_args *uap)
2527 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2528 &uap->sysmsg_offset);
2534 * Check if current process can access given file. amode is a bitmask of *_OK
2535 * access bits. flags is a bitmask of AT_* flags.
2538 kern_access(struct nlookupdata *nd, int amode, int flags)
2543 if (flags & ~AT_EACCESS)
2545 if ((error = nlookup(nd)) != 0)
2548 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2552 /* Flags == 0 means only check for existence. */
2561 if ((mode & VWRITE) == 0 ||
2562 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2563 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2566 * If the file handle is stale we have to re-resolve the
2567 * entry. This is a hack at the moment.
2569 if (error == ESTALE) {
2571 cache_setunresolved(&nd->nl_nch);
2572 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2585 * access_args(char *path, int flags)
2587 * Check access permissions.
2590 sys_access(struct access_args *uap)
2592 struct nlookupdata nd;
2595 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2597 error = kern_access(&nd, uap->flags, 0);
2604 * faccessat_args(int fd, char *path, int amode, int flags)
2606 * Check access permissions.
2609 sys_faccessat(struct faccessat_args *uap)
2611 struct nlookupdata nd;
2615 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2618 error = kern_access(&nd, uap->amode, uap->flags);
2619 nlookup_done_at(&nd, fp);
2625 kern_stat(struct nlookupdata *nd, struct stat *st)
2631 if ((error = nlookup(nd)) != 0)
2634 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2638 if ((error = vget(vp, LK_SHARED)) != 0)
2640 error = vn_stat(vp, st, nd->nl_cred);
2643 * If the file handle is stale we have to re-resolve the entry. This
2644 * is a hack at the moment.
2646 if (error == ESTALE) {
2648 cache_setunresolved(&nd->nl_nch);
2649 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2659 * stat_args(char *path, struct stat *ub)
2661 * Get file status; this version follows links.
2664 sys_stat(struct stat_args *uap)
2666 struct nlookupdata nd;
2670 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2672 error = kern_stat(&nd, &st);
2674 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2681 * lstat_args(char *path, struct stat *ub)
2683 * Get file status; this version does not follow links.
2686 sys_lstat(struct lstat_args *uap)
2688 struct nlookupdata nd;
2692 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2694 error = kern_stat(&nd, &st);
2696 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2703 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2705 * Get status of file pointed to by fd/path.
2708 sys_fstatat(struct fstatat_args *uap)
2710 struct nlookupdata nd;
2716 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2719 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2721 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2722 UIO_USERSPACE, flags);
2724 error = kern_stat(&nd, &st);
2726 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2728 nlookup_done_at(&nd, fp);
2733 * pathconf_Args(char *path, int name)
2735 * Get configurable pathname variables.
2738 sys_pathconf(struct pathconf_args *uap)
2740 struct nlookupdata nd;
2745 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2747 error = nlookup(&nd);
2749 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2752 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2760 * kern_readlink isn't properly split yet. There is a copyin burried
2761 * in VOP_READLINK().
2764 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2766 struct thread *td = curthread;
2772 if ((error = nlookup(nd)) != 0)
2774 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2777 if (vp->v_type != VLNK) {
2780 aiov.iov_base = buf;
2781 aiov.iov_len = count;
2782 auio.uio_iov = &aiov;
2783 auio.uio_iovcnt = 1;
2784 auio.uio_offset = 0;
2785 auio.uio_rw = UIO_READ;
2786 auio.uio_segflg = UIO_USERSPACE;
2788 auio.uio_resid = count;
2789 error = VOP_READLINK(vp, &auio, td->td_ucred);
2792 *res = count - auio.uio_resid;
2797 * readlink_args(char *path, char *buf, int count)
2799 * Return target name of a symbolic link.
2802 sys_readlink(struct readlink_args *uap)
2804 struct nlookupdata nd;
2807 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2809 error = kern_readlink(&nd, uap->buf, uap->count,
2810 &uap->sysmsg_result);
2817 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2819 * Return target name of a symbolic link. The path is relative to the
2820 * directory associated with fd.
2823 sys_readlinkat(struct readlinkat_args *uap)
2825 struct nlookupdata nd;
2829 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2831 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2832 &uap->sysmsg_result);
2834 nlookup_done_at(&nd, fp);
2839 setfflags(struct vnode *vp, int flags)
2841 struct thread *td = curthread;
2846 * Prevent non-root users from setting flags on devices. When
2847 * a device is reused, users can retain ownership of the device
2848 * if they are allowed to set flags and programs assume that
2849 * chown can't fail when done as root.
2851 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2852 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2856 * note: vget is required for any operation that might mod the vnode
2857 * so VINACTIVE is properly cleared.
2859 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2861 vattr.va_flags = flags;
2862 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2869 * chflags(char *path, int flags)
2871 * Change flags of a file given a path name.
2874 sys_chflags(struct chflags_args *uap)
2876 struct nlookupdata nd;
2881 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2883 error = nlookup(&nd);
2885 error = ncp_writechk(&nd.nl_nch);
2887 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2890 error = setfflags(vp, uap->flags);
2897 * lchflags(char *path, int flags)
2899 * Change flags of a file given a path name, but don't follow symlinks.
2902 sys_lchflags(struct lchflags_args *uap)
2904 struct nlookupdata nd;
2909 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2911 error = nlookup(&nd);
2913 error = ncp_writechk(&nd.nl_nch);
2915 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2918 error = setfflags(vp, uap->flags);
2925 * fchflags_args(int fd, int flags)
2927 * Change flags of a file given a file descriptor.
2930 sys_fchflags(struct fchflags_args *uap)
2932 struct thread *td = curthread;
2933 struct proc *p = td->td_proc;
2937 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2939 if (fp->f_nchandle.ncp)
2940 error = ncp_writechk(&fp->f_nchandle);
2942 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2948 setfmode(struct vnode *vp, int mode)
2950 struct thread *td = curthread;
2955 * note: vget is required for any operation that might mod the vnode
2956 * so VINACTIVE is properly cleared.
2958 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2960 vattr.va_mode = mode & ALLPERMS;
2961 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2968 kern_chmod(struct nlookupdata *nd, int mode)
2973 if ((error = nlookup(nd)) != 0)
2975 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2977 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2978 error = setfmode(vp, mode);
2984 * chmod_args(char *path, int mode)
2986 * Change mode of a file given path name.
2989 sys_chmod(struct chmod_args *uap)
2991 struct nlookupdata nd;
2994 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2996 error = kern_chmod(&nd, uap->mode);
3002 * lchmod_args(char *path, int mode)
3004 * Change mode of a file given path name (don't follow links.)
3007 sys_lchmod(struct lchmod_args *uap)
3009 struct nlookupdata nd;
3012 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3014 error = kern_chmod(&nd, uap->mode);
3020 * fchmod_args(int fd, int mode)
3022 * Change mode of a file given a file descriptor.
3025 sys_fchmod(struct fchmod_args *uap)
3027 struct thread *td = curthread;
3028 struct proc *p = td->td_proc;
3032 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3034 if (fp->f_nchandle.ncp)
3035 error = ncp_writechk(&fp->f_nchandle);
3037 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3043 * fchmodat_args(char *path, int mode)
3045 * Change mode of a file pointed to by fd/path.
3048 sys_fchmodat(struct fchmodat_args *uap)
3050 struct nlookupdata nd;
3055 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3057 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3059 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3060 UIO_USERSPACE, flags);
3062 error = kern_chmod(&nd, uap->mode);
3063 nlookup_done_at(&nd, fp);
3068 setfown(struct vnode *vp, uid_t uid, gid_t gid)
3070 struct thread *td = curthread;
3075 * note: vget is required for any operation that might mod the vnode
3076 * so VINACTIVE is properly cleared.
3078 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3082 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3089 kern_chown(struct nlookupdata *nd, int uid, int gid)
3094 if ((error = nlookup(nd)) != 0)
3096 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3098 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3099 error = setfown(vp, uid, gid);
3105 * chown(char *path, int uid, int gid)
3107 * Set ownership given a path name.
3110 sys_chown(struct chown_args *uap)
3112 struct nlookupdata nd;
3115 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3117 error = kern_chown(&nd, uap->uid, uap->gid);
3123 * lchown_args(char *path, int uid, int gid)
3125 * Set ownership given a path name, do not cross symlinks.
3128 sys_lchown(struct lchown_args *uap)
3130 struct nlookupdata nd;
3133 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3135 error = kern_chown(&nd, uap->uid, uap->gid);
3141 * fchown_args(int fd, int uid, int gid)
3143 * Set ownership given a file descriptor.
3146 sys_fchown(struct fchown_args *uap)
3148 struct thread *td = curthread;
3149 struct proc *p = td->td_proc;
3153 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3155 if (fp->f_nchandle.ncp)
3156 error = ncp_writechk(&fp->f_nchandle);
3158 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
3164 * fchownat(int fd, char *path, int uid, int gid, int flags)
3166 * Set ownership of file pointed to by fd/path.
3169 sys_fchownat(struct fchownat_args *uap)
3171 struct nlookupdata nd;
3176 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3178 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3180 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3181 UIO_USERSPACE, flags);
3183 error = kern_chown(&nd, uap->uid, uap->gid);
3184 nlookup_done_at(&nd, fp);
3190 getutimes(const struct timeval *tvp, struct timespec *tsp)
3192 struct timeval tv[2];
3196 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3199 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3200 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3206 setutimes(struct vnode *vp, struct vattr *vattr,
3207 const struct timespec *ts, int nullflag)
3209 struct thread *td = curthread;
3213 vattr->va_atime = ts[0];
3214 vattr->va_mtime = ts[1];
3216 vattr->va_vaflags |= VA_UTIMES_NULL;
3217 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3223 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3225 struct timespec ts[2];
3230 if ((error = getutimes(tptr, ts)) != 0)
3234 * NOTE: utimes() succeeds for the owner even if the file
3235 * is not user-writable.
3237 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3239 if ((error = nlookup(nd)) != 0)
3241 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3243 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3247 * note: vget is required for any operation that might mod the vnode
3248 * so VINACTIVE is properly cleared.
3250 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3251 error = vget(vp, LK_EXCLUSIVE);
3253 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3262 * utimes_args(char *path, struct timeval *tptr)
3264 * Set the access and modification times of a file.
3267 sys_utimes(struct utimes_args *uap)
3269 struct timeval tv[2];
3270 struct nlookupdata nd;
3274 error = copyin(uap->tptr, tv, sizeof(tv));
3278 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3280 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3286 * lutimes_args(char *path, struct timeval *tptr)
3288 * Set the access and modification times of a file.
3291 sys_lutimes(struct lutimes_args *uap)
3293 struct timeval tv[2];
3294 struct nlookupdata nd;
3298 error = copyin(uap->tptr, tv, sizeof(tv));
3302 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3304 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3310 * Set utimes on a file descriptor. The creds used to open the
3311 * file are used to determine whether the operation is allowed
3315 kern_futimes(int fd, struct timeval *tptr)
3317 struct thread *td = curthread;
3318 struct proc *p = td->td_proc;
3319 struct timespec ts[2];
3325 error = getutimes(tptr, ts);
3328 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3330 if (fp->f_nchandle.ncp)
3331 error = ncp_writechk(&fp->f_nchandle);
3334 error = vget(vp, LK_EXCLUSIVE);
3336 error = VOP_GETATTR(vp, &vattr);
3338 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3342 error = setutimes(vp, &vattr, ts,
3353 * futimes_args(int fd, struct timeval *tptr)
3355 * Set the access and modification times of a file.
3358 sys_futimes(struct futimes_args *uap)
3360 struct timeval tv[2];
3364 error = copyin(uap->tptr, tv, sizeof(tv));
3368 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3374 kern_truncate(struct nlookupdata *nd, off_t length)
3382 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3383 if ((error = nlookup(nd)) != 0)
3385 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3387 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3389 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3393 if (vp->v_type == VDIR) {
3395 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3397 vattr.va_size = length;
3398 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3405 * truncate(char *path, int pad, off_t length)
3407 * Truncate a file given its path name.
3410 sys_truncate(struct truncate_args *uap)
3412 struct nlookupdata nd;
3415 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3417 error = kern_truncate(&nd, uap->length);
3423 kern_ftruncate(int fd, off_t length)
3425 struct thread *td = curthread;
3426 struct proc *p = td->td_proc;
3434 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3436 if (fp->f_nchandle.ncp) {
3437 error = ncp_writechk(&fp->f_nchandle);
3441 if ((fp->f_flag & FWRITE) == 0) {
3445 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3449 vp = (struct vnode *)fp->f_data;
3450 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3451 if (vp->v_type == VDIR) {
3453 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3455 vattr.va_size = length;
3456 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3465 * ftruncate_args(int fd, int pad, off_t length)
3467 * Truncate a file given a file descriptor.
3470 sys_ftruncate(struct ftruncate_args *uap)
3474 error = kern_ftruncate(uap->fd, uap->length);
3482 * Sync an open file.
3485 sys_fsync(struct fsync_args *uap)
3487 struct thread *td = curthread;
3488 struct proc *p = td->td_proc;
3494 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3496 vp = (struct vnode *)fp->f_data;
3497 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3498 if ((obj = vp->v_object) != NULL)
3499 vm_object_page_clean(obj, 0, 0, 0);
3500 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3501 if (error == 0 && vp->v_mount)
3502 error = buf_fsync(vp);
3510 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3512 struct nchandle fnchd;
3513 struct nchandle tnchd;
3514 struct namecache *ncp;
3521 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3522 if ((error = nlookup(fromnd)) != 0)
3524 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3526 fnchd.mount = fromnd->nl_nch.mount;
3530 * unlock the source nch so we can lookup the target nch without
3531 * deadlocking. The target may or may not exist so we do not check
3532 * for a target vp like kern_mkdir() and other creation functions do.
3534 * The source and target directories are ref'd and rechecked after
3535 * everything is relocked to determine if the source or target file
3538 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3539 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3540 cache_unlock(&fromnd->nl_nch);
3542 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3543 if ((error = nlookup(tond)) != 0) {
3547 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3551 tnchd.mount = tond->nl_nch.mount;
3555 * If the source and target are the same there is nothing to do
3557 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3564 * Mount points cannot be renamed or overwritten
3566 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3575 * Relock the source ncp. cache_relock() will deal with any
3576 * deadlocks against the already-locked tond and will also
3577 * make sure both are resolved.
3579 * NOTE AFTER RELOCKING: The source or target ncp may have become
3580 * invalid while they were unlocked, nc_vp and nc_mount could
3583 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3584 &tond->nl_nch, tond->nl_cred);
3585 fromnd->nl_flags |= NLC_NCPISLOCKED;
3588 * make sure the parent directories linkages are the same
3590 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3591 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3598 * Both the source and target must be within the same filesystem and
3599 * in the same filesystem as their parent directories within the
3600 * namecache topology.
3602 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3605 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3606 mp != tond->nl_nch.mount) {
3613 * Make sure the mount point is writable
3615 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3622 * If the target exists and either the source or target is a directory,
3623 * then both must be directories.
3625 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3628 if (tond->nl_nch.ncp->nc_vp) {
3629 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3631 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3632 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3634 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3640 * You cannot rename a source into itself or a subdirectory of itself.
3641 * We check this by travsersing the target directory upwards looking
3642 * for a match against the source.
3647 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3648 if (fromnd->nl_nch.ncp == ncp) {
3659 * Even though the namespaces are different, they may still represent
3660 * hardlinks to the same file. The filesystem might have a hard time
3661 * with this so we issue a NREMOVE of the source instead of a NRENAME
3662 * when we detect the situation.
3665 fdvp = fromnd->nl_dvp;
3666 tdvp = tond->nl_dvp;
3667 if (fdvp == NULL || tdvp == NULL) {
3669 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3670 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3673 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3674 fdvp, tdvp, tond->nl_cred);
3681 * rename_args(char *from, char *to)
3683 * Rename files. Source and destination must either both be directories,
3684 * or both not be directories. If target is a directory, it must be empty.
3687 sys_rename(struct rename_args *uap)
3689 struct nlookupdata fromnd, tond;
3692 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3694 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3696 error = kern_rename(&fromnd, &tond);
3697 nlookup_done(&tond);
3699 nlookup_done(&fromnd);
3704 * renameat_args(int oldfd, char *old, int newfd, char *new)
3706 * Rename files using paths relative to the directories associated with
3707 * oldfd and newfd. Source and destination must either both be directories,
3708 * or both not be directories. If target is a directory, it must be empty.
3711 sys_renameat(struct renameat_args *uap)
3713 struct nlookupdata oldnd, newnd;
3714 struct file *oldfp, *newfp;
3717 error = nlookup_init_at(&oldnd, &oldfp, uap->oldfd, uap->old,
3720 error = nlookup_init_at(&newnd, &newfp, uap->newfd, uap->new,
3723 error = kern_rename(&oldnd, &newnd);
3724 nlookup_done_at(&newnd, newfp);
3726 nlookup_done_at(&oldnd, oldfp);
3731 kern_mkdir(struct nlookupdata *nd, int mode)
3733 struct thread *td = curthread;
3734 struct proc *p = td->td_proc;
3740 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3741 if ((error = nlookup(nd)) != 0)
3744 if (nd->nl_nch.ncp->nc_vp)
3746 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3749 vattr.va_type = VDIR;
3750 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3753 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3760 * mkdir_args(char *path, int mode)
3762 * Make a directory file.
3765 sys_mkdir(struct mkdir_args *uap)
3767 struct nlookupdata nd;
3770 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3772 error = kern_mkdir(&nd, uap->mode);
3778 * mkdirat_args(int fd, char *path, mode_t mode)
3780 * Make a directory file. The path is relative to the directory associated
3784 sys_mkdirat(struct mkdirat_args *uap)
3786 struct nlookupdata nd;
3790 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3792 error = kern_mkdir(&nd, uap->mode);
3793 nlookup_done_at(&nd, fp);
3798 kern_rmdir(struct nlookupdata *nd)
3803 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3804 if ((error = nlookup(nd)) != 0)
3808 * Do not allow directories representing mount points to be
3809 * deleted, even if empty. Check write perms on mount point
3810 * in case the vnode is aliased (aka nullfs).
3812 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3814 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3816 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3821 * rmdir_args(char *path)
3823 * Remove a directory file.
3826 sys_rmdir(struct rmdir_args *uap)
3828 struct nlookupdata nd;
3831 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3833 error = kern_rmdir(&nd);
3839 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3840 enum uio_seg direction)
3842 struct thread *td = curthread;
3843 struct proc *p = td->td_proc;
3851 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3853 if ((fp->f_flag & FREAD) == 0) {
3857 vp = (struct vnode *)fp->f_data;
3859 if (vp->v_type != VDIR) {
3863 aiov.iov_base = buf;
3864 aiov.iov_len = count;
3865 auio.uio_iov = &aiov;
3866 auio.uio_iovcnt = 1;
3867 auio.uio_rw = UIO_READ;
3868 auio.uio_segflg = direction;
3870 auio.uio_resid = count;
3871 loff = auio.uio_offset = fp->f_offset;
3872 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3873 fp->f_offset = auio.uio_offset;
3876 if (count == auio.uio_resid) {
3877 if (union_dircheckp) {
3878 error = union_dircheckp(td, &vp, fp);
3885 if ((vp->v_flag & VROOT) &&
3886 (vp->v_mount->mnt_flag & MNT_UNION)) {
3887 struct vnode *tvp = vp;
3888 vp = vp->v_mount->mnt_vnodecovered;
3899 * WARNING! *basep may not be wide enough to accomodate the
3900 * seek offset. XXX should we hack this to return the upper 32 bits
3901 * for offsets greater then 4G?
3904 *basep = (long)loff;
3906 *res = count - auio.uio_resid;
3913 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3915 * Read a block of directory entries in a file system independent format.
3918 sys_getdirentries(struct getdirentries_args *uap)
3923 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3924 &uap->sysmsg_result, UIO_USERSPACE);
3926 if (error == 0 && uap->basep)
3927 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3932 * getdents_args(int fd, char *buf, size_t count)
3935 sys_getdents(struct getdents_args *uap)
3939 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3940 &uap->sysmsg_result, UIO_USERSPACE);
3946 * Set the mode mask for creation of filesystem nodes.
3948 * umask(int newmask)
3951 sys_umask(struct umask_args *uap)
3953 struct thread *td = curthread;
3954 struct proc *p = td->td_proc;
3955 struct filedesc *fdp;
3958 uap->sysmsg_result = fdp->fd_cmask;
3959 fdp->fd_cmask = uap->newmask & ALLPERMS;
3964 * revoke(char *path)
3966 * Void all references to file by ripping underlying filesystem
3970 sys_revoke(struct revoke_args *uap)
3972 struct nlookupdata nd;
3979 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3981 error = nlookup(&nd);
3983 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3984 cred = crhold(nd.nl_cred);
3988 error = VOP_GETATTR(vp, &vattr);
3989 if (error == 0 && cred->cr_uid != vattr.va_uid)
3990 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
3991 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
3993 error = vrevoke(vp, cred);
3994 } else if (error == 0) {
3995 error = vrevoke(vp, cred);
4005 * getfh_args(char *fname, fhandle_t *fhp)
4007 * Get (NFS) file handle
4009 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4010 * mount. This allows nullfs mounts to be explicitly exported.
4012 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4014 * nullfs mounts of subdirectories are not safe. That is, it will
4015 * work, but you do not really have protection against access to
4016 * the related parent directories.
4019 sys_getfh(struct getfh_args *uap)
4021 struct thread *td = curthread;
4022 struct nlookupdata nd;
4029 * Must be super user
4031 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4035 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4037 error = nlookup(&nd);
4039 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4040 mp = nd.nl_nch.mount;
4043 bzero(&fh, sizeof(fh));
4044 fh.fh_fsid = mp->mnt_stat.f_fsid;
4045 error = VFS_VPTOFH(vp, &fh.fh_fid);
4048 error = copyout(&fh, uap->fhp, sizeof(fh));
4054 * fhopen_args(const struct fhandle *u_fhp, int flags)
4056 * syscall for the rpc.lockd to use to translate a NFS file handle into
4057 * an open descriptor.
4059 * warning: do not remove the priv_check() call or this becomes one giant
4063 sys_fhopen(struct fhopen_args *uap)
4065 struct thread *td = curthread;
4066 struct filedesc *fdp = td->td_proc->p_fd;
4071 struct vattr *vap = &vat;
4073 int fmode, mode, error, type;
4079 * Must be super user
4081 error = priv_check(td, PRIV_ROOT);
4085 fmode = FFLAGS(uap->flags);
4088 * Why not allow a non-read/write open for our lockd?
4090 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4092 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4097 * Find the mount point
4099 mp = vfs_getvfs(&fhp.fh_fsid);
4104 /* now give me my vnode, it gets returned to me locked */
4105 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4109 * from now on we have to make sure not
4110 * to forget about the vnode
4111 * any error that causes an abort must vput(vp)
4112 * just set error = err and 'goto bad;'.
4118 if (vp->v_type == VLNK) {
4122 if (vp->v_type == VSOCK) {
4127 if (fmode & (FWRITE | O_TRUNC)) {
4128 if (vp->v_type == VDIR) {
4132 error = vn_writechk(vp, NULL);
4140 error = VOP_ACCESS(vp, mode, td->td_ucred);
4144 if (fmode & O_TRUNC) {
4145 vn_unlock(vp); /* XXX */
4146 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4149 error = VOP_SETATTR(vp, vap, td->td_ucred);
4155 * VOP_OPEN needs the file pointer so it can potentially override
4158 * WARNING! no f_nchandle will be associated when fhopen()ing a
4161 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4165 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4168 * setting f_ops this way prevents VOP_CLOSE from being
4169 * called or fdrop() releasing the vp from v_data. Since
4170 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4172 fp->f_ops = &badfileops;
4178 * The fp is given its own reference, we still have our ref and lock.
4180 * Assert that all regular files must be created with a VM object.
4182 if (vp->v_type == VREG && vp->v_object == NULL) {
4183 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4188 * The open was successful. Handle any locking requirements.
4190 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4191 lf.l_whence = SEEK_SET;
4194 if (fmode & O_EXLOCK)
4195 lf.l_type = F_WRLCK;
4197 lf.l_type = F_RDLCK;
4198 if (fmode & FNONBLOCK)
4203 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4205 * release our private reference.
4207 fsetfd(fdp, NULL, indx);
4212 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4213 fp->f_flag |= FHASLOCK;
4217 * Clean up. Associate the file pointer with the previously
4218 * reserved descriptor and return it.
4221 fsetfd(fdp, fp, indx);
4223 uap->sysmsg_result = indx;
4227 fsetfd(fdp, NULL, indx);
4236 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4239 sys_fhstat(struct fhstat_args *uap)
4241 struct thread *td = curthread;
4249 * Must be super user
4251 error = priv_check(td, PRIV_ROOT);
4255 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4259 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4262 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4263 error = vn_stat(vp, &sb, td->td_ucred);
4268 error = copyout(&sb, uap->sb, sizeof(sb));
4273 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4276 sys_fhstatfs(struct fhstatfs_args *uap)
4278 struct thread *td = curthread;
4279 struct proc *p = td->td_proc;
4284 char *fullpath, *freepath;
4289 * Must be super user
4291 if ((error = priv_check(td, PRIV_ROOT)))
4294 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4297 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4301 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4306 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4311 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4314 error = mount_path(p, mp, &fullpath, &freepath);
4317 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4318 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4319 kfree(freepath, M_TEMP);
4321 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4322 if (priv_check(td, PRIV_ROOT)) {
4323 bcopy(sp, &sb, sizeof(sb));
4324 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4327 error = copyout(sp, uap->buf, sizeof(*sp));
4333 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4336 sys_fhstatvfs(struct fhstatvfs_args *uap)
4338 struct thread *td = curthread;
4339 struct proc *p = td->td_proc;
4347 * Must be super user
4349 if ((error = priv_check(td, PRIV_ROOT)))
4352 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4355 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4359 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4364 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4367 sp = &mp->mnt_vstat;
4369 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4373 if (mp->mnt_flag & MNT_RDONLY)
4374 sp->f_flag |= ST_RDONLY;
4375 if (mp->mnt_flag & MNT_NOSUID)
4376 sp->f_flag |= ST_NOSUID;
4377 error = copyout(sp, uap->buf, sizeof(*sp));
4384 * Syscall to push extended attribute configuration information into the
4385 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4386 * a command (int cmd), and attribute name and misc data. For now, the
4387 * attribute name is left in userspace for consumption by the VFS_op.
4388 * It will probably be changed to be copied into sysspace by the
4389 * syscall in the future, once issues with various consumers of the
4390 * attribute code have raised their hands.
4392 * Currently this is used only by UFS Extended Attributes.
4395 sys_extattrctl(struct extattrctl_args *uap)
4397 struct nlookupdata nd;
4399 char attrname[EXTATTR_MAXNAMELEN];
4407 if (error == 0 && uap->filename) {
4408 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4411 error = nlookup(&nd);
4413 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4417 if (error == 0 && uap->attrname) {
4418 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4423 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4425 error = nlookup(&nd);
4427 error = ncp_writechk(&nd.nl_nch);
4429 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4431 uap->attrname, nd.nl_cred);
4440 * Syscall to get a named extended attribute on a file or directory.
4443 sys_extattr_set_file(struct extattr_set_file_args *uap)
4445 char attrname[EXTATTR_MAXNAMELEN];
4446 struct nlookupdata nd;
4452 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4458 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4460 error = nlookup(&nd);
4462 error = ncp_writechk(&nd.nl_nch);
4464 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4470 bzero(&auio, sizeof(auio));
4471 aiov.iov_base = uap->data;
4472 aiov.iov_len = uap->nbytes;
4473 auio.uio_iov = &aiov;
4474 auio.uio_iovcnt = 1;
4475 auio.uio_offset = 0;
4476 auio.uio_resid = uap->nbytes;
4477 auio.uio_rw = UIO_WRITE;
4478 auio.uio_td = curthread;
4480 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4489 * Syscall to get a named extended attribute on a file or directory.
4492 sys_extattr_get_file(struct extattr_get_file_args *uap)
4494 char attrname[EXTATTR_MAXNAMELEN];
4495 struct nlookupdata nd;
4501 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4507 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4509 error = nlookup(&nd);
4511 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4517 bzero(&auio, sizeof(auio));
4518 aiov.iov_base = uap->data;
4519 aiov.iov_len = uap->nbytes;
4520 auio.uio_iov = &aiov;
4521 auio.uio_iovcnt = 1;
4522 auio.uio_offset = 0;
4523 auio.uio_resid = uap->nbytes;
4524 auio.uio_rw = UIO_READ;
4525 auio.uio_td = curthread;
4527 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4529 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4537 * Syscall to delete a named extended attribute from a file or directory.
4538 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4541 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4543 char attrname[EXTATTR_MAXNAMELEN];
4544 struct nlookupdata nd;
4548 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4552 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4554 error = nlookup(&nd);
4556 error = ncp_writechk(&nd.nl_nch);
4558 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4560 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4561 attrname, NULL, nd.nl_cred);
4570 * Determine if the mount is visible to the process.
4573 chroot_visible_mnt(struct mount *mp, struct proc *p)
4575 struct nchandle nch;
4578 * Traverse from the mount point upwards. If we hit the process
4579 * root then the mount point is visible to the process.
4581 nch = mp->mnt_ncmountpt;
4583 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4584 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4587 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4588 nch = nch.mount->mnt_ncmounton;
4590 nch.ncp = nch.ncp->nc_parent;
4595 * If the mount point is not visible to the process, but the
4596 * process root is in a subdirectory of the mount, return
4599 if (p->p_fd->fd_nrdir.mount == mp)