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, "");
105 * Virtual File System System Calls
109 * Mount a file system.
111 * mount_args(char *type, char *path, int flags, caddr_t data)
116 sys_mount(struct mount_args *uap)
118 struct thread *td = curthread;
121 struct mount *mp, *nullmp;
122 struct vfsconf *vfsp;
123 int error, flag = 0, flag2 = 0;
126 struct nlookupdata nd;
127 char fstypename[MFSNAMELEN];
136 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
140 * Do not allow NFS export by non-root users.
142 if (uap->flags & MNT_EXPORTED) {
143 error = priv_check(td, PRIV_ROOT);
148 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
150 if (priv_check(td, PRIV_ROOT))
151 uap->flags |= MNT_NOSUID | MNT_NODEV;
154 * Lookup the requested path and extract the nch and vnode.
156 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
158 if ((error = nlookup(&nd)) == 0) {
159 if (nd.nl_nch.ncp->nc_vp == NULL)
169 * If the target filesystem is resolved via a nullfs mount, then
170 * nd.nl_nch.mount will be pointing to the nullfs mount structure
171 * instead of the target file system. We need it in case we are
174 nullmp = nd.nl_nch.mount;
177 * Extract the locked+refd ncp and cleanup the nd structure
180 cache_zero(&nd.nl_nch);
183 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
190 * now we have the locked ref'd nch and unreferenced vnode.
193 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
200 * Extract the file system type. We need to know this early, to take
201 * appropriate actions if we are dealing with a nullfs.
203 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
210 * Now we have an unlocked ref'd nch and a locked ref'd vp
212 if (uap->flags & MNT_UPDATE) {
213 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
220 if (strncmp(fstypename, "null", 5) == 0) {
228 flag2 = mp->mnt_kern_flag;
230 * We only allow the filesystem to be reloaded if it
231 * is currently mounted read-only.
233 if ((uap->flags & MNT_RELOAD) &&
234 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
237 error = EOPNOTSUPP; /* Needs translation */
241 * Only root, or the user that did the original mount is
242 * permitted to update it.
244 if (mp->mnt_stat.f_owner != cred->cr_uid &&
245 (error = priv_check(td, PRIV_ROOT))) {
250 if (vfs_busy(mp, LK_NOWAIT)) {
256 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
263 vsetflags(vp, VMOUNT);
265 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
270 * If the user is not root, ensure that they own the directory
271 * onto which we are attempting to mount.
273 if ((error = VOP_GETATTR(vp, &va)) ||
274 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
279 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
284 if (vp->v_type != VDIR) {
290 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
296 vfsp = vfsconf_find_by_name(fstypename);
300 /* Only load modules for root (very important!) */
301 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
306 error = linker_load_file(fstypename, &lf);
307 if (error || lf == NULL) {
315 /* lookup again, see if the VFS was loaded */
316 vfsp = vfsconf_find_by_name(fstypename);
319 linker_file_unload(lf);
326 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
332 vsetflags(vp, VMOUNT);
335 * Allocate and initialize the filesystem.
337 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
339 vfs_busy(mp, LK_NOWAIT);
340 mp->mnt_op = vfsp->vfc_vfsops;
342 vfsp->vfc_refcount++;
343 mp->mnt_stat.f_type = vfsp->vfc_typenum;
344 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
345 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
346 mp->mnt_stat.f_owner = cred->cr_uid;
350 * Set the mount level flags.
352 if (uap->flags & MNT_RDONLY)
353 mp->mnt_flag |= MNT_RDONLY;
354 else if (mp->mnt_flag & MNT_RDONLY)
355 mp->mnt_kern_flag |= MNTK_WANTRDWR;
356 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
357 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
358 MNT_NOSYMFOLLOW | MNT_IGNORE |
359 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
360 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
361 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
362 MNT_NOSYMFOLLOW | MNT_IGNORE |
363 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
365 * Mount the filesystem.
366 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
369 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
370 if (mp->mnt_flag & MNT_UPDATE) {
371 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
372 mp->mnt_flag &= ~MNT_RDONLY;
373 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
374 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
377 mp->mnt_kern_flag = flag2;
380 vclrflags(vp, VMOUNT);
385 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
387 * Put the new filesystem on the mount list after root. The mount
388 * point gets its own mnt_ncmountpt (unless the VFS already set one
389 * up) which represents the root of the mount. The lookup code
390 * detects the mount point going forward and checks the root of
391 * the mount going backwards.
393 * It is not necessary to invalidate or purge the vnode underneath
394 * because elements under the mount will be given their own glue
398 if (mp->mnt_ncmountpt.ncp == NULL) {
400 * allocate, then unlock, but leave the ref intact
402 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
403 cache_unlock(&mp->mnt_ncmountpt);
405 mp->mnt_ncmounton = nch; /* inherits ref */
406 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
408 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
409 vclrflags(vp, VMOUNT);
410 mountlist_insert(mp, MNTINS_LAST);
412 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
413 error = vfs_allocate_syncvnode(mp);
415 error = VFS_START(mp, 0);
418 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
419 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
420 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
421 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
422 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
423 vclrflags(vp, VMOUNT);
424 mp->mnt_vfc->vfc_refcount--;
436 * Scan all active processes to see if any of them have a current
437 * or root directory onto which the new filesystem has just been
438 * mounted. If so, replace them with the new mount point.
440 * The passed ncp is ref'd and locked (from the mount code) and
441 * must be associated with the vnode representing the root of the
444 struct checkdirs_info {
445 struct nchandle old_nch;
446 struct nchandle new_nch;
447 struct vnode *old_vp;
448 struct vnode *new_vp;
451 static int checkdirs_callback(struct proc *p, void *data);
454 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
456 struct checkdirs_info info;
462 * If the old mount point's vnode has a usecount of 1, it is not
463 * being held as a descriptor anywhere.
465 olddp = old_nch->ncp->nc_vp;
466 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
470 * Force the root vnode of the new mount point to be resolved
471 * so we can update any matching processes.
474 if (VFS_ROOT(mp, &newdp))
475 panic("mount: lost mount");
476 cache_setunresolved(new_nch);
477 cache_setvp(new_nch, newdp);
480 * Special handling of the root node
482 if (rootvnode == olddp) {
484 vfs_cache_setroot(newdp, cache_hold(new_nch));
488 * Pass newdp separately so the callback does not have to access
489 * it via new_nch->ncp->nc_vp.
491 info.old_nch = *old_nch;
492 info.new_nch = *new_nch;
494 allproc_scan(checkdirs_callback, &info);
499 * NOTE: callback is not MP safe because the scanned process's filedesc
500 * structure can be ripped out from under us, amoung other things.
503 checkdirs_callback(struct proc *p, void *data)
505 struct checkdirs_info *info = data;
506 struct filedesc *fdp;
507 struct nchandle ncdrop1;
508 struct nchandle ncdrop2;
509 struct vnode *vprele1;
510 struct vnode *vprele2;
512 if ((fdp = p->p_fd) != NULL) {
513 cache_zero(&ncdrop1);
514 cache_zero(&ncdrop2);
519 * MPUNSAFE - XXX fdp can be pulled out from under a
522 * A shared filedesc is ok, we don't have to copy it
523 * because we are making this change globally.
525 spin_lock(&fdp->fd_spin);
526 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
527 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
528 vprele1 = fdp->fd_cdir;
530 fdp->fd_cdir = info->new_vp;
531 ncdrop1 = fdp->fd_ncdir;
532 cache_copy(&info->new_nch, &fdp->fd_ncdir);
534 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
535 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
536 vprele2 = fdp->fd_rdir;
538 fdp->fd_rdir = info->new_vp;
539 ncdrop2 = fdp->fd_nrdir;
540 cache_copy(&info->new_nch, &fdp->fd_nrdir);
542 spin_unlock(&fdp->fd_spin);
544 cache_drop(&ncdrop1);
546 cache_drop(&ncdrop2);
556 * Unmount a file system.
558 * Note: unmount takes a path to the vnode mounted on as argument,
559 * not special file (as before).
561 * umount_args(char *path, int flags)
566 sys_unmount(struct unmount_args *uap)
568 struct thread *td = curthread;
569 struct proc *p __debugvar = td->td_proc;
570 struct mount *mp = NULL;
571 struct nlookupdata nd;
576 if (td->td_ucred->cr_prison != NULL) {
580 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
583 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
585 error = nlookup(&nd);
589 mp = nd.nl_nch.mount;
592 * Only root, or the user that did the original mount is
593 * permitted to unmount this filesystem.
595 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
596 (error = priv_check(td, PRIV_ROOT)))
600 * Don't allow unmounting the root file system.
602 if (mp->mnt_flag & MNT_ROOTFS) {
608 * Must be the root of the filesystem
610 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
618 error = dounmount(mp, uap->flags);
625 * Do the actual file system unmount.
628 dounmount_interlock(struct mount *mp)
630 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
632 mp->mnt_kern_flag |= MNTK_UNMOUNT;
637 unmount_allproc_cb(struct proc *p, void *arg)
641 if (p->p_textnch.ncp == NULL)
644 mp = (struct mount *)arg;
645 if (p->p_textnch.mount == mp)
646 cache_drop(&p->p_textnch);
652 dounmount(struct mount *mp, int flags)
654 struct namecache *ncp;
663 * Exclusive access for unmounting purposes
665 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
669 * Allow filesystems to detect that a forced unmount is in progress.
671 if (flags & MNT_FORCE)
672 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
673 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
674 error = lockmgr(&mp->mnt_lock, lflags);
676 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
677 if (mp->mnt_kern_flag & MNTK_MWAIT)
682 if (mp->mnt_flag & MNT_EXPUBLIC)
683 vfs_setpublicfs(NULL, NULL, NULL);
685 vfs_msync(mp, MNT_WAIT);
686 async_flag = mp->mnt_flag & MNT_ASYNC;
687 mp->mnt_flag &=~ MNT_ASYNC;
690 * If this filesystem isn't aliasing other filesystems,
691 * try to invalidate any remaining namecache entries and
692 * check the count afterwords.
694 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
695 cache_lock(&mp->mnt_ncmountpt);
696 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
697 cache_unlock(&mp->mnt_ncmountpt);
699 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
700 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
701 allproc_scan(&unmount_allproc_cb, mp);
704 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
705 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
707 if ((flags & MNT_FORCE) == 0) {
709 mount_warning(mp, "Cannot unmount: "
715 mount_warning(mp, "Forced unmount: "
726 * nchandle records ref the mount structure. Expect a count of 1
727 * (our mount->mnt_ncmountpt).
729 if (mp->mnt_refs != 1) {
730 if ((flags & MNT_FORCE) == 0) {
731 mount_warning(mp, "Cannot unmount: "
732 "%d process references still "
733 "present", mp->mnt_refs);
736 mount_warning(mp, "Forced unmount: "
737 "%d process references still "
738 "present", mp->mnt_refs);
744 * Decomission our special mnt_syncer vnode. This also stops
745 * the vnlru code. If we are unable to unmount we recommission
749 if ((vp = mp->mnt_syncer) != NULL) {
750 mp->mnt_syncer = NULL;
753 if (((mp->mnt_flag & MNT_RDONLY) ||
754 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
755 (flags & MNT_FORCE)) {
756 error = VFS_UNMOUNT(mp, flags);
760 if (mp->mnt_syncer == NULL)
761 vfs_allocate_syncvnode(mp);
762 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
763 mp->mnt_flag |= async_flag;
764 lockmgr(&mp->mnt_lock, LK_RELEASE);
765 if (mp->mnt_kern_flag & MNTK_MWAIT)
770 * Clean up any journals still associated with the mount after
771 * filesystem activity has ceased.
773 journal_remove_all_journals(mp,
774 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
776 mountlist_remove(mp);
779 * Remove any installed vnode ops here so the individual VFSs don't
782 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
783 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
784 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
785 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
786 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
788 if (mp->mnt_ncmountpt.ncp != NULL) {
789 nch = mp->mnt_ncmountpt;
790 cache_zero(&mp->mnt_ncmountpt);
791 cache_clrmountpt(&nch);
794 if (mp->mnt_ncmounton.ncp != NULL) {
795 nch = mp->mnt_ncmounton;
796 cache_zero(&mp->mnt_ncmounton);
797 cache_clrmountpt(&nch);
801 mp->mnt_vfc->vfc_refcount--;
802 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
803 panic("unmount: dangling vnode");
804 lockmgr(&mp->mnt_lock, LK_RELEASE);
805 if (mp->mnt_kern_flag & MNTK_MWAIT)
814 mount_warning(struct mount *mp, const char *ctl, ...)
821 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf, 0) == 0) {
822 kprintf("unmount(%s): ", ptr);
827 kprintf("unmount(%p", mp);
828 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
829 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
838 * Shim cache_fullpath() to handle the case where a process is chrooted into
839 * a subdirectory of a mount. In this case if the root mount matches the
840 * process root directory's mount we have to specify the process's root
841 * directory instead of the mount point, because the mount point might
842 * be above the root directory.
846 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
848 struct nchandle *nch;
850 if (p && p->p_fd->fd_nrdir.mount == mp)
851 nch = &p->p_fd->fd_nrdir;
853 nch = &mp->mnt_ncmountpt;
854 return(cache_fullpath(p, nch, rb, fb, 0));
858 * Sync each mounted filesystem.
862 static int syncprt = 0;
863 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
866 static int sync_callback(struct mount *mp, void *data);
872 sys_sync(struct sync_args *uap)
875 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
878 * print out buffer pool stat information on each sync() call.
889 sync_callback(struct mount *mp, void *data __unused)
893 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
894 asyncflag = mp->mnt_flag & MNT_ASYNC;
895 mp->mnt_flag &= ~MNT_ASYNC;
896 vfs_msync(mp, MNT_NOWAIT);
897 VFS_SYNC(mp, MNT_NOWAIT);
898 mp->mnt_flag |= asyncflag;
903 /* XXX PRISON: could be per prison flag */
904 static int prison_quotas;
906 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
910 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
912 * Change filesystem quotas.
917 sys_quotactl(struct quotactl_args *uap)
919 struct nlookupdata nd;
928 if (td->td_ucred->cr_prison && !prison_quotas) {
933 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
935 error = nlookup(&nd);
937 mp = nd.nl_nch.mount;
938 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
939 uap->arg, nd.nl_cred);
948 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
949 * void *buf, int buflen)
951 * This function operates on a mount point and executes the specified
952 * operation using the specified control data, and possibly returns data.
954 * The actual number of bytes stored in the result buffer is returned, 0
955 * if none, otherwise an error is returned.
960 sys_mountctl(struct mountctl_args *uap)
962 struct thread *td = curthread;
963 struct proc *p = td->td_proc;
971 * Sanity and permissions checks. We must be root.
974 if (td->td_ucred->cr_prison != NULL)
976 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
977 (error = priv_check(td, PRIV_ROOT)) != 0)
981 * Argument length checks
983 if (uap->ctllen < 0 || uap->ctllen > 1024)
985 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
987 if (uap->path == NULL)
991 * Allocate the necessary buffers and copyin data
993 path = objcache_get(namei_oc, M_WAITOK);
994 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
999 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1000 error = copyin(uap->ctl, ctl, uap->ctllen);
1005 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1008 * Validate the descriptor
1011 fp = holdfp(p->p_fd, uap->fd, -1);
1021 * Execute the internal kernel function and clean up.
1024 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1028 if (error == 0 && uap->sysmsg_result > 0)
1029 error = copyout(buf, uap->buf, uap->sysmsg_result);
1032 objcache_put(namei_oc, path);
1041 * Execute a mount control operation by resolving the path to a mount point
1042 * and calling vop_mountctl().
1044 * Use the mount point from the nch instead of the vnode so nullfs mounts
1045 * can properly spike the VOP.
1048 kern_mountctl(const char *path, int op, struct file *fp,
1049 const void *ctl, int ctllen,
1050 void *buf, int buflen, int *res)
1054 struct nlookupdata nd;
1059 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1061 error = nlookup(&nd);
1063 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1064 mp = nd.nl_nch.mount;
1071 * Must be the root of the filesystem
1073 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1077 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1084 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1086 struct thread *td = curthread;
1087 struct proc *p = td->td_proc;
1090 char *fullpath, *freepath;
1093 if ((error = nlookup(nd)) != 0)
1095 mp = nd->nl_nch.mount;
1097 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1100 error = mount_path(p, mp, &fullpath, &freepath);
1103 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1104 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1105 kfree(freepath, M_TEMP);
1107 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1108 bcopy(sp, buf, sizeof(*buf));
1109 /* Only root should have access to the fsid's. */
1110 if (priv_check(td, PRIV_ROOT))
1111 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1116 * statfs_args(char *path, struct statfs *buf)
1118 * Get filesystem statistics.
1123 sys_statfs(struct statfs_args *uap)
1125 struct nlookupdata nd;
1130 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1132 error = kern_statfs(&nd, &buf);
1135 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1144 kern_fstatfs(int fd, struct statfs *buf)
1146 struct thread *td = curthread;
1147 struct proc *p = td->td_proc;
1151 char *fullpath, *freepath;
1155 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1158 mp = ((struct vnode *)fp->f_data)->v_mount;
1163 if (fp->f_cred == NULL) {
1168 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1171 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1173 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1174 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1175 kfree(freepath, M_TEMP);
1177 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1178 bcopy(sp, buf, sizeof(*buf));
1180 /* Only root should have access to the fsid's. */
1181 if (priv_check(td, PRIV_ROOT))
1182 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1191 * fstatfs_args(int fd, struct statfs *buf)
1193 * Get filesystem statistics.
1198 sys_fstatfs(struct fstatfs_args *uap)
1203 error = kern_fstatfs(uap->fd, &buf);
1206 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1211 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1217 if ((error = nlookup(nd)) != 0)
1219 mp = nd->nl_nch.mount;
1220 sp = &mp->mnt_vstat;
1221 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1225 if (mp->mnt_flag & MNT_RDONLY)
1226 sp->f_flag |= ST_RDONLY;
1227 if (mp->mnt_flag & MNT_NOSUID)
1228 sp->f_flag |= ST_NOSUID;
1229 bcopy(sp, buf, sizeof(*buf));
1234 * statfs_args(char *path, struct statfs *buf)
1236 * Get filesystem statistics.
1241 sys_statvfs(struct statvfs_args *uap)
1243 struct nlookupdata nd;
1248 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1250 error = kern_statvfs(&nd, &buf);
1253 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1259 kern_fstatvfs(int fd, struct statvfs *buf)
1261 struct thread *td = curthread;
1262 struct proc *p = td->td_proc;
1269 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1271 mp = ((struct vnode *)fp->f_data)->v_mount;
1276 if (fp->f_cred == NULL) {
1280 sp = &mp->mnt_vstat;
1281 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1285 if (mp->mnt_flag & MNT_RDONLY)
1286 sp->f_flag |= ST_RDONLY;
1287 if (mp->mnt_flag & MNT_NOSUID)
1288 sp->f_flag |= ST_NOSUID;
1290 bcopy(sp, buf, sizeof(*buf));
1298 * fstatfs_args(int fd, struct statfs *buf)
1300 * Get filesystem statistics.
1305 sys_fstatvfs(struct fstatvfs_args *uap)
1311 error = kern_fstatvfs(uap->fd, &buf);
1315 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1320 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1322 * Get statistics on all filesystems.
1325 struct getfsstat_info {
1326 struct statfs *sfsp;
1334 static int getfsstat_callback(struct mount *, void *);
1340 sys_getfsstat(struct getfsstat_args *uap)
1342 struct thread *td = curthread;
1343 struct getfsstat_info info;
1345 bzero(&info, sizeof(info));
1347 info.maxcount = uap->bufsize / sizeof(struct statfs);
1348 info.sfsp = uap->buf;
1350 info.flags = uap->flags;
1354 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1356 if (info.sfsp && info.count > info.maxcount)
1357 uap->sysmsg_result = info.maxcount;
1359 uap->sysmsg_result = info.count;
1360 return (info.error);
1364 getfsstat_callback(struct mount *mp, void *data)
1366 struct getfsstat_info *info = data;
1372 if (info->sfsp && info->count < info->maxcount) {
1373 if (info->td->td_proc &&
1374 !chroot_visible_mnt(mp, info->td->td_proc)) {
1380 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1381 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1382 * overrides MNT_WAIT.
1384 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1385 (info->flags & MNT_WAIT)) &&
1386 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1389 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1391 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1393 info->error = error;
1396 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1397 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1398 kfree(freepath, M_TEMP);
1400 error = copyout(sp, info->sfsp, sizeof(*sp));
1402 info->error = error;
1412 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1413 long bufsize, int flags)
1415 * Get statistics on all filesystems.
1418 struct getvfsstat_info {
1419 struct statfs *sfsp;
1420 struct statvfs *vsfsp;
1428 static int getvfsstat_callback(struct mount *, void *);
1434 sys_getvfsstat(struct getvfsstat_args *uap)
1436 struct thread *td = curthread;
1437 struct getvfsstat_info info;
1439 bzero(&info, sizeof(info));
1441 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1442 info.sfsp = uap->buf;
1443 info.vsfsp = uap->vbuf;
1445 info.flags = uap->flags;
1449 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1450 if (info.vsfsp && info.count > info.maxcount)
1451 uap->sysmsg_result = info.maxcount;
1453 uap->sysmsg_result = info.count;
1455 return (info.error);
1459 getvfsstat_callback(struct mount *mp, void *data)
1461 struct getvfsstat_info *info = data;
1463 struct statvfs *vsp;
1468 if (info->vsfsp && info->count < info->maxcount) {
1469 if (info->td->td_proc &&
1470 !chroot_visible_mnt(mp, info->td->td_proc)) {
1474 vsp = &mp->mnt_vstat;
1477 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1478 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1479 * overrides MNT_WAIT.
1481 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1482 (info->flags & MNT_WAIT)) &&
1483 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1486 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1488 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1489 (info->flags & MNT_WAIT)) &&
1490 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1494 if (mp->mnt_flag & MNT_RDONLY)
1495 vsp->f_flag |= ST_RDONLY;
1496 if (mp->mnt_flag & MNT_NOSUID)
1497 vsp->f_flag |= ST_NOSUID;
1499 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1501 info->error = error;
1504 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1505 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1506 kfree(freepath, M_TEMP);
1508 error = copyout(sp, info->sfsp, sizeof(*sp));
1510 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1512 info->error = error;
1524 * fchdir_args(int fd)
1526 * Change current working directory to a given file descriptor.
1531 sys_fchdir(struct fchdir_args *uap)
1533 struct thread *td = curthread;
1534 struct proc *p = td->td_proc;
1535 struct filedesc *fdp = p->p_fd;
1536 struct vnode *vp, *ovp;
1539 struct nchandle nch, onch, tnch;
1542 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1545 vp = (struct vnode *)fp->f_data;
1547 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1548 if (fp->f_nchandle.ncp == NULL)
1551 error = checkvp_chdir(vp, td);
1556 cache_copy(&fp->f_nchandle, &nch);
1559 * If the ncp has become a mount point, traverse through
1563 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1564 (mp = cache_findmount(&nch)) != NULL
1566 error = nlookup_mp(mp, &tnch);
1568 cache_unlock(&tnch); /* leave ref intact */
1570 vp = tnch.ncp->nc_vp;
1571 error = vget(vp, LK_SHARED);
1572 KKASSERT(error == 0);
1579 onch = fdp->fd_ncdir;
1580 vn_unlock(vp); /* leave ref intact */
1582 fdp->fd_ncdir = nch;
1596 kern_chdir(struct nlookupdata *nd)
1598 struct thread *td = curthread;
1599 struct proc *p = td->td_proc;
1600 struct filedesc *fdp = p->p_fd;
1601 struct vnode *vp, *ovp;
1602 struct nchandle onch;
1605 if ((error = nlookup(nd)) != 0)
1607 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1609 if ((error = vget(vp, LK_SHARED)) != 0)
1612 error = checkvp_chdir(vp, td);
1616 onch = fdp->fd_ncdir;
1617 cache_unlock(&nd->nl_nch); /* leave reference intact */
1618 fdp->fd_ncdir = nd->nl_nch;
1622 cache_zero(&nd->nl_nch);
1630 * chdir_args(char *path)
1632 * Change current working directory (``.'').
1637 sys_chdir(struct chdir_args *uap)
1639 struct nlookupdata nd;
1643 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1645 error = kern_chdir(&nd);
1652 * Helper function for raised chroot(2) security function: Refuse if
1653 * any filedescriptors are open directories.
1656 chroot_refuse_vdir_fds(struct filedesc *fdp)
1663 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1664 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1666 vp = (struct vnode *)fp->f_data;
1667 if (vp->v_type != VDIR) {
1678 * This sysctl determines if we will allow a process to chroot(2) if it
1679 * has a directory open:
1680 * 0: disallowed for all processes.
1681 * 1: allowed for processes that were not already chroot(2)'ed.
1682 * 2: allowed for all processes.
1685 static int chroot_allow_open_directories = 1;
1687 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1688 &chroot_allow_open_directories, 0, "");
1691 * chroot to the specified namecache entry. We obtain the vp from the
1692 * namecache data. The passed ncp must be locked and referenced and will
1693 * remain locked and referenced on return.
1696 kern_chroot(struct nchandle *nch)
1698 struct thread *td = curthread;
1699 struct proc *p = td->td_proc;
1700 struct filedesc *fdp = p->p_fd;
1705 * Only privileged user can chroot
1707 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1712 * Disallow open directory descriptors (fchdir() breakouts).
1714 if (chroot_allow_open_directories == 0 ||
1715 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1716 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1719 if ((vp = nch->ncp->nc_vp) == NULL)
1722 if ((error = vget(vp, LK_SHARED)) != 0)
1726 * Check the validity of vp as a directory to change to and
1727 * associate it with rdir/jdir.
1729 error = checkvp_chdir(vp, td);
1730 vn_unlock(vp); /* leave reference intact */
1732 vrele(fdp->fd_rdir);
1733 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1734 cache_drop(&fdp->fd_nrdir);
1735 cache_copy(nch, &fdp->fd_nrdir);
1736 if (fdp->fd_jdir == NULL) {
1739 cache_copy(nch, &fdp->fd_njdir);
1748 * chroot_args(char *path)
1750 * Change notion of root (``/'') directory.
1755 sys_chroot(struct chroot_args *uap)
1757 struct thread *td __debugvar = curthread;
1758 struct nlookupdata nd;
1761 KKASSERT(td->td_proc);
1763 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1765 nd.nl_flags |= NLC_EXEC;
1766 error = nlookup(&nd);
1768 error = kern_chroot(&nd.nl_nch);
1776 sys_chroot_kernel(struct chroot_kernel_args *uap)
1778 struct thread *td = curthread;
1779 struct nlookupdata nd;
1780 struct nchandle *nch;
1785 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1789 error = nlookup(&nd);
1795 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1799 if ((vp = nch->ncp->nc_vp) == NULL) {
1804 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1807 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1808 vfs_cache_setroot(vp, cache_hold(nch));
1818 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1819 * determine whether it is legal to chdir to the vnode. The vnode's state
1820 * is not changed by this call.
1823 checkvp_chdir(struct vnode *vp, struct thread *td)
1827 if (vp->v_type != VDIR)
1830 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1838 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1840 struct thread *td = curthread;
1841 struct proc *p = td->td_proc;
1842 struct lwp *lp = td->td_lwp;
1843 struct filedesc *fdp = p->p_fd;
1848 int type, indx, error;
1851 if ((oflags & O_ACCMODE) == O_ACCMODE)
1853 flags = FFLAGS(oflags);
1854 error = falloc(lp, &nfp, NULL);
1858 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1861 * XXX p_dupfd is a real mess. It allows a device to return a
1862 * file descriptor to be duplicated rather then doing the open
1868 * Call vn_open() to do the lookup and assign the vnode to the
1869 * file pointer. vn_open() does not change the ref count on fp
1870 * and the vnode, on success, will be inherited by the file pointer
1873 nd->nl_flags |= NLC_LOCKVP;
1874 error = vn_open(nd, fp, flags, cmode);
1878 * handle special fdopen() case. bleh. dupfdopen() is
1879 * responsible for dropping the old contents of ofiles[indx]
1882 * Note that fsetfd() will add a ref to fp which represents
1883 * the fd_files[] assignment. We must still drop our
1886 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1887 if (fdalloc(p, 0, &indx) == 0) {
1888 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1891 fdrop(fp); /* our ref */
1894 fsetfd(fdp, NULL, indx);
1897 fdrop(fp); /* our ref */
1898 if (error == ERESTART)
1904 * ref the vnode for ourselves so it can't be ripped out from under
1905 * is. XXX need an ND flag to request that the vnode be returned
1908 * Reserve a file descriptor but do not assign it until the open
1911 vp = (struct vnode *)fp->f_data;
1913 if ((error = fdalloc(p, 0, &indx)) != 0) {
1920 * If no error occurs the vp will have been assigned to the file
1925 if (flags & (O_EXLOCK | O_SHLOCK)) {
1926 lf.l_whence = SEEK_SET;
1929 if (flags & O_EXLOCK)
1930 lf.l_type = F_WRLCK;
1932 lf.l_type = F_RDLCK;
1933 if (flags & FNONBLOCK)
1938 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1940 * lock request failed. Clean up the reserved
1944 fsetfd(fdp, NULL, indx);
1948 fp->f_flag |= FHASLOCK;
1952 * Assert that all regular file vnodes were created with a object.
1954 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1955 ("open: regular file has no backing object after vn_open"));
1961 * release our private reference, leaving the one associated with the
1962 * descriptor table intact.
1964 fsetfd(fdp, fp, indx);
1971 * open_args(char *path, int flags, int mode)
1973 * Check permissions, allocate an open file structure,
1974 * and call the device open routine if any.
1979 sys_open(struct open_args *uap)
1981 CACHE_MPLOCK_DECLARE;
1982 struct nlookupdata nd;
1986 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1988 error = kern_open(&nd, uap->flags,
1989 uap->mode, &uap->sysmsg_result);
1997 * openat_args(int fd, char *path, int flags, int mode)
2002 sys_openat(struct openat_args *uap)
2004 CACHE_MPLOCK_DECLARE;
2005 struct nlookupdata nd;
2010 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2012 error = kern_open(&nd, uap->flags, uap->mode,
2013 &uap->sysmsg_result);
2015 nlookup_done_at(&nd, fp);
2021 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2023 struct thread *td = curthread;
2024 struct proc *p = td->td_proc;
2033 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2034 vattr.va_rmajor = rmajor;
2035 vattr.va_rminor = rminor;
2037 switch (mode & S_IFMT) {
2038 case S_IFMT: /* used by badsect to flag bad sectors */
2039 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2040 vattr.va_type = VBAD;
2043 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2044 vattr.va_type = VCHR;
2047 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2048 vattr.va_type = VBLK;
2051 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2054 case S_IFDIR: /* special directories support for HAMMER */
2055 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2056 vattr.va_type = VDIR;
2067 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2068 if ((error = nlookup(nd)) != 0)
2070 if (nd->nl_nch.ncp->nc_vp)
2072 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2076 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2077 nd->nl_cred, NAMEI_CREATE);
2080 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2081 &vp, nd->nl_cred, &vattr);
2089 * mknod_args(char *path, int mode, int dev)
2091 * Create a special file.
2096 sys_mknod(struct mknod_args *uap)
2098 struct nlookupdata nd;
2102 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2104 error = kern_mknod(&nd, uap->mode,
2105 umajor(uap->dev), uminor(uap->dev));
2113 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2115 * Create a special file. The path is relative to the directory associated
2121 sys_mknodat(struct mknodat_args *uap)
2123 struct nlookupdata nd;
2128 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2130 error = kern_mknod(&nd, uap->mode,
2131 umajor(uap->dev), uminor(uap->dev));
2133 nlookup_done_at(&nd, fp);
2139 kern_mkfifo(struct nlookupdata *nd, int mode)
2141 struct thread *td = curthread;
2142 struct proc *p = td->td_proc;
2149 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2150 if ((error = nlookup(nd)) != 0)
2152 if (nd->nl_nch.ncp->nc_vp)
2154 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2158 vattr.va_type = VFIFO;
2159 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2161 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2168 * mkfifo_args(char *path, int mode)
2170 * Create a named pipe.
2175 sys_mkfifo(struct mkfifo_args *uap)
2177 struct nlookupdata nd;
2181 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2183 error = kern_mkfifo(&nd, uap->mode);
2190 * mkfifoat_args(int fd, char *path, mode_t mode)
2192 * Create a named pipe. The path is relative to the directory associated
2198 sys_mkfifoat(struct mkfifoat_args *uap)
2200 struct nlookupdata nd;
2205 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2207 error = kern_mkfifo(&nd, uap->mode);
2208 nlookup_done_at(&nd, fp);
2213 static int hardlink_check_uid = 0;
2214 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2215 &hardlink_check_uid, 0,
2216 "Unprivileged processes cannot create hard links to files owned by other "
2218 static int hardlink_check_gid = 0;
2219 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2220 &hardlink_check_gid, 0,
2221 "Unprivileged processes cannot create hard links to files owned by other "
2225 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2231 * Shortcut if disabled
2233 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2237 * Privileged user can always hardlink
2239 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2243 * Otherwise only if the originating file is owned by the
2244 * same user or group. Note that any group is allowed if
2245 * the file is owned by the caller.
2247 error = VOP_GETATTR(vp, &va);
2251 if (hardlink_check_uid) {
2252 if (cred->cr_uid != va.va_uid)
2256 if (hardlink_check_gid) {
2257 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2265 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2267 struct thread *td = curthread;
2272 * Lookup the source and obtained a locked vnode.
2274 * You may only hardlink a file which you have write permission
2275 * on or which you own.
2277 * XXX relookup on vget failure / race ?
2280 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2281 if ((error = nlookup(nd)) != 0)
2283 vp = nd->nl_nch.ncp->nc_vp;
2284 KKASSERT(vp != NULL);
2285 if (vp->v_type == VDIR)
2286 return (EPERM); /* POSIX */
2287 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2289 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2293 * Unlock the source so we can lookup the target without deadlocking
2294 * (XXX vp is locked already, possible other deadlock?). The target
2297 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2298 nd->nl_flags &= ~NLC_NCPISLOCKED;
2299 cache_unlock(&nd->nl_nch);
2302 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2303 if ((error = nlookup(linknd)) != 0) {
2307 if (linknd->nl_nch.ncp->nc_vp) {
2311 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2317 * Finally run the new API VOP.
2319 error = can_hardlink(vp, td, td->td_ucred);
2321 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2322 vp, linknd->nl_cred);
2329 * link_args(char *path, char *link)
2331 * Make a hard file link.
2336 sys_link(struct link_args *uap)
2338 struct nlookupdata nd, linknd;
2342 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2344 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2346 error = kern_link(&nd, &linknd);
2347 nlookup_done(&linknd);
2355 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2363 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2364 if ((error = nlookup(nd)) != 0)
2366 if (nd->nl_nch.ncp->nc_vp)
2368 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2372 vattr.va_mode = mode;
2373 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2380 * symlink(char *path, char *link)
2382 * Make a symbolic link.
2387 sys_symlink(struct symlink_args *uap)
2389 struct thread *td = curthread;
2390 struct nlookupdata nd;
2395 path = objcache_get(namei_oc, M_WAITOK);
2396 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2399 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2401 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2402 error = kern_symlink(&nd, path, mode);
2407 objcache_put(namei_oc, path);
2412 * symlinkat_args(char *path1, int fd, char *path2)
2414 * Make a symbolic link. The path2 argument is relative to the directory
2415 * associated with fd.
2420 sys_symlinkat(struct symlinkat_args *uap)
2422 struct thread *td = curthread;
2423 struct nlookupdata nd;
2429 path1 = objcache_get(namei_oc, M_WAITOK);
2430 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2433 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2436 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2437 error = kern_symlink(&nd, path1, mode);
2439 nlookup_done_at(&nd, fp);
2442 objcache_put(namei_oc, path1);
2447 * undelete_args(char *path)
2449 * Delete a whiteout from the filesystem.
2454 sys_undelete(struct undelete_args *uap)
2456 struct nlookupdata nd;
2460 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2462 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2464 error = nlookup(&nd);
2466 error = ncp_writechk(&nd.nl_nch);
2468 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2477 kern_unlink(struct nlookupdata *nd)
2482 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2483 if ((error = nlookup(nd)) != 0)
2485 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2487 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2492 * unlink_args(char *path)
2494 * Delete a name from the filesystem.
2499 sys_unlink(struct unlink_args *uap)
2501 struct nlookupdata nd;
2505 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2507 error = kern_unlink(&nd);
2515 * unlinkat_args(int fd, char *path, int flags)
2517 * Delete the file or directory entry pointed to by fd/path.
2522 sys_unlinkat(struct unlinkat_args *uap)
2524 struct nlookupdata nd;
2528 if (uap->flags & ~AT_REMOVEDIR)
2532 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2534 if (uap->flags & AT_REMOVEDIR)
2535 error = kern_rmdir(&nd);
2537 error = kern_unlink(&nd);
2539 nlookup_done_at(&nd, fp);
2548 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2550 struct thread *td = curthread;
2551 struct proc *p = td->td_proc;
2558 fp = holdfp(p->p_fd, fd, -1);
2561 if (fp->f_type != DTYPE_VNODE) {
2565 vp = (struct vnode *)fp->f_data;
2569 spin_lock(&fp->f_spin);
2570 new_offset = fp->f_offset + offset;
2575 error = VOP_GETATTR(vp, &vattr);
2577 spin_lock(&fp->f_spin);
2578 new_offset = offset + vattr.va_size;
2581 new_offset = offset;
2583 spin_lock(&fp->f_spin);
2588 spin_lock(&fp->f_spin);
2593 * Validate the seek position. Negative offsets are not allowed
2594 * for regular files or directories.
2596 * Normally we would also not want to allow negative offsets for
2597 * character and block-special devices. However kvm addresses
2598 * on 64 bit architectures might appear to be negative and must
2602 if (new_offset < 0 &&
2603 (vp->v_type == VREG || vp->v_type == VDIR)) {
2606 fp->f_offset = new_offset;
2609 *res = fp->f_offset;
2610 spin_unlock(&fp->f_spin);
2617 * lseek_args(int fd, int pad, off_t offset, int whence)
2619 * Reposition read/write file offset.
2624 sys_lseek(struct lseek_args *uap)
2628 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2629 &uap->sysmsg_offset);
2635 * Check if current process can access given file. amode is a bitmask of *_OK
2636 * access bits. flags is a bitmask of AT_* flags.
2639 kern_access(struct nlookupdata *nd, int amode, int flags)
2644 if (flags & ~AT_EACCESS)
2646 if ((error = nlookup(nd)) != 0)
2649 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2653 /* Flags == 0 means only check for existence. */
2662 if ((mode & VWRITE) == 0 ||
2663 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2664 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2667 * If the file handle is stale we have to re-resolve the
2668 * entry. This is a hack at the moment.
2670 if (error == ESTALE) {
2672 cache_setunresolved(&nd->nl_nch);
2673 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2686 * access_args(char *path, int flags)
2688 * Check access permissions.
2693 sys_access(struct access_args *uap)
2695 struct nlookupdata nd;
2699 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2701 error = kern_access(&nd, uap->flags, 0);
2709 * faccessat_args(int fd, char *path, int amode, int flags)
2711 * Check access permissions.
2716 sys_faccessat(struct faccessat_args *uap)
2718 struct nlookupdata nd;
2723 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2726 error = kern_access(&nd, uap->amode, uap->flags);
2727 nlookup_done_at(&nd, fp);
2737 kern_stat(struct nlookupdata *nd, struct stat *st)
2743 if ((error = nlookup(nd)) != 0)
2746 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2750 if ((error = vget(vp, LK_SHARED)) != 0)
2752 error = vn_stat(vp, st, nd->nl_cred);
2755 * If the file handle is stale we have to re-resolve the entry. This
2756 * is a hack at the moment.
2758 if (error == ESTALE) {
2760 cache_setunresolved(&nd->nl_nch);
2761 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2771 * stat_args(char *path, struct stat *ub)
2773 * Get file status; this version follows links.
2778 sys_stat(struct stat_args *uap)
2780 CACHE_MPLOCK_DECLARE;
2781 struct nlookupdata nd;
2786 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2788 error = kern_stat(&nd, &st);
2790 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2798 * lstat_args(char *path, struct stat *ub)
2800 * Get file status; this version does not follow links.
2805 sys_lstat(struct lstat_args *uap)
2807 CACHE_MPLOCK_DECLARE;
2808 struct nlookupdata nd;
2813 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2815 error = kern_stat(&nd, &st);
2817 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2825 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2827 * Get status of file pointed to by fd/path.
2832 sys_fstatat(struct fstatat_args *uap)
2834 CACHE_MPLOCK_DECLARE;
2835 struct nlookupdata nd;
2841 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2844 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2847 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2848 UIO_USERSPACE, flags);
2850 error = kern_stat(&nd, &st);
2852 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2854 nlookup_done_at(&nd, fp);
2860 * pathconf_Args(char *path, int name)
2862 * Get configurable pathname variables.
2867 sys_pathconf(struct pathconf_args *uap)
2869 struct nlookupdata nd;
2875 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2877 error = nlookup(&nd);
2879 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2882 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2891 * kern_readlink isn't properly split yet. There is a copyin burried
2892 * in VOP_READLINK().
2895 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2897 struct thread *td = curthread;
2903 if ((error = nlookup(nd)) != 0)
2905 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2908 if (vp->v_type != VLNK) {
2911 aiov.iov_base = buf;
2912 aiov.iov_len = count;
2913 auio.uio_iov = &aiov;
2914 auio.uio_iovcnt = 1;
2915 auio.uio_offset = 0;
2916 auio.uio_rw = UIO_READ;
2917 auio.uio_segflg = UIO_USERSPACE;
2919 auio.uio_resid = count;
2920 error = VOP_READLINK(vp, &auio, td->td_ucred);
2923 *res = count - auio.uio_resid;
2928 * readlink_args(char *path, char *buf, int count)
2930 * Return target name of a symbolic link.
2935 sys_readlink(struct readlink_args *uap)
2937 struct nlookupdata nd;
2941 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2943 error = kern_readlink(&nd, uap->buf, uap->count,
2944 &uap->sysmsg_result);
2952 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2954 * Return target name of a symbolic link. The path is relative to the
2955 * directory associated with fd.
2960 sys_readlinkat(struct readlinkat_args *uap)
2962 struct nlookupdata nd;
2967 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2969 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2970 &uap->sysmsg_result);
2972 nlookup_done_at(&nd, fp);
2978 setfflags(struct vnode *vp, int flags)
2980 struct thread *td = curthread;
2985 * Prevent non-root users from setting flags on devices. When
2986 * a device is reused, users can retain ownership of the device
2987 * if they are allowed to set flags and programs assume that
2988 * chown can't fail when done as root.
2990 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2991 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2995 * note: vget is required for any operation that might mod the vnode
2996 * so VINACTIVE is properly cleared.
2998 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3000 vattr.va_flags = flags;
3001 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3008 * chflags(char *path, int flags)
3010 * Change flags of a file given a path name.
3015 sys_chflags(struct chflags_args *uap)
3017 struct nlookupdata nd;
3023 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3025 error = nlookup(&nd);
3027 error = ncp_writechk(&nd.nl_nch);
3029 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3032 error = setfflags(vp, uap->flags);
3040 * lchflags(char *path, int flags)
3042 * Change flags of a file given a path name, but don't follow symlinks.
3047 sys_lchflags(struct lchflags_args *uap)
3049 struct nlookupdata nd;
3055 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3057 error = nlookup(&nd);
3059 error = ncp_writechk(&nd.nl_nch);
3061 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3064 error = setfflags(vp, uap->flags);
3072 * fchflags_args(int fd, int flags)
3074 * Change flags of a file given a file descriptor.
3079 sys_fchflags(struct fchflags_args *uap)
3081 struct thread *td = curthread;
3082 struct proc *p = td->td_proc;
3086 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3089 if (fp->f_nchandle.ncp)
3090 error = ncp_writechk(&fp->f_nchandle);
3092 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3099 setfmode(struct vnode *vp, int mode)
3101 struct thread *td = curthread;
3106 * note: vget is required for any operation that might mod the vnode
3107 * so VINACTIVE is properly cleared.
3109 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3111 vattr.va_mode = mode & ALLPERMS;
3112 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3119 kern_chmod(struct nlookupdata *nd, int mode)
3124 if ((error = nlookup(nd)) != 0)
3126 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3128 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3129 error = setfmode(vp, mode);
3135 * chmod_args(char *path, int mode)
3137 * Change mode of a file given path name.
3142 sys_chmod(struct chmod_args *uap)
3144 struct nlookupdata nd;
3148 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3150 error = kern_chmod(&nd, uap->mode);
3157 * lchmod_args(char *path, int mode)
3159 * Change mode of a file given path name (don't follow links.)
3164 sys_lchmod(struct lchmod_args *uap)
3166 struct nlookupdata nd;
3170 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3172 error = kern_chmod(&nd, uap->mode);
3179 * fchmod_args(int fd, int mode)
3181 * Change mode of a file given a file descriptor.
3186 sys_fchmod(struct fchmod_args *uap)
3188 struct thread *td = curthread;
3189 struct proc *p = td->td_proc;
3193 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3196 if (fp->f_nchandle.ncp)
3197 error = ncp_writechk(&fp->f_nchandle);
3199 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3206 * fchmodat_args(char *path, int mode)
3208 * Change mode of a file pointed to by fd/path.
3213 sys_fchmodat(struct fchmodat_args *uap)
3215 struct nlookupdata nd;
3220 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3222 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3225 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3226 UIO_USERSPACE, flags);
3228 error = kern_chmod(&nd, uap->mode);
3229 nlookup_done_at(&nd, fp);
3235 setfown(struct vnode *vp, uid_t uid, gid_t gid)
3237 struct thread *td = curthread;
3242 * note: vget is required for any operation that might mod the vnode
3243 * so VINACTIVE is properly cleared.
3245 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3249 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3256 kern_chown(struct nlookupdata *nd, int uid, int gid)
3261 if ((error = nlookup(nd)) != 0)
3263 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3265 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3266 error = setfown(vp, uid, gid);
3272 * chown(char *path, int uid, int gid)
3274 * Set ownership given a path name.
3279 sys_chown(struct chown_args *uap)
3281 struct nlookupdata nd;
3285 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3287 error = kern_chown(&nd, uap->uid, uap->gid);
3294 * lchown_args(char *path, int uid, int gid)
3296 * Set ownership given a path name, do not cross symlinks.
3301 sys_lchown(struct lchown_args *uap)
3303 struct nlookupdata nd;
3307 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3309 error = kern_chown(&nd, uap->uid, uap->gid);
3316 * fchown_args(int fd, int uid, int gid)
3318 * Set ownership given a file descriptor.
3323 sys_fchown(struct fchown_args *uap)
3325 struct thread *td = curthread;
3326 struct proc *p = td->td_proc;
3330 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3333 if (fp->f_nchandle.ncp)
3334 error = ncp_writechk(&fp->f_nchandle);
3336 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
3343 * fchownat(int fd, char *path, int uid, int gid, int flags)
3345 * Set ownership of file pointed to by fd/path.
3350 sys_fchownat(struct fchownat_args *uap)
3352 struct nlookupdata nd;
3357 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3359 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3362 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3363 UIO_USERSPACE, flags);
3365 error = kern_chown(&nd, uap->uid, uap->gid);
3366 nlookup_done_at(&nd, fp);
3373 getutimes(const struct timeval *tvp, struct timespec *tsp)
3375 struct timeval tv[2];
3379 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3382 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3383 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3389 setutimes(struct vnode *vp, struct vattr *vattr,
3390 const struct timespec *ts, int nullflag)
3392 struct thread *td = curthread;
3396 vattr->va_atime = ts[0];
3397 vattr->va_mtime = ts[1];
3399 vattr->va_vaflags |= VA_UTIMES_NULL;
3400 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3406 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3408 struct timespec ts[2];
3413 if ((error = getutimes(tptr, ts)) != 0)
3417 * NOTE: utimes() succeeds for the owner even if the file
3418 * is not user-writable.
3420 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3422 if ((error = nlookup(nd)) != 0)
3424 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3426 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3430 * note: vget is required for any operation that might mod the vnode
3431 * so VINACTIVE is properly cleared.
3433 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3434 error = vget(vp, LK_EXCLUSIVE);
3436 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3445 * utimes_args(char *path, struct timeval *tptr)
3447 * Set the access and modification times of a file.
3452 sys_utimes(struct utimes_args *uap)
3454 struct timeval tv[2];
3455 struct nlookupdata nd;
3459 error = copyin(uap->tptr, tv, sizeof(tv));
3464 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3466 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3473 * lutimes_args(char *path, struct timeval *tptr)
3475 * Set the access and modification times of a file.
3480 sys_lutimes(struct lutimes_args *uap)
3482 struct timeval tv[2];
3483 struct nlookupdata nd;
3487 error = copyin(uap->tptr, tv, sizeof(tv));
3492 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3494 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3501 * Set utimes on a file descriptor. The creds used to open the
3502 * file are used to determine whether the operation is allowed
3506 kern_futimes(int fd, struct timeval *tptr)
3508 struct thread *td = curthread;
3509 struct proc *p = td->td_proc;
3510 struct timespec ts[2];
3516 error = getutimes(tptr, ts);
3519 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3521 if (fp->f_nchandle.ncp)
3522 error = ncp_writechk(&fp->f_nchandle);
3525 error = vget(vp, LK_EXCLUSIVE);
3527 error = VOP_GETATTR(vp, &vattr);
3529 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3533 error = setutimes(vp, &vattr, ts,
3544 * futimes_args(int fd, struct timeval *tptr)
3546 * Set the access and modification times of a file.
3551 sys_futimes(struct futimes_args *uap)
3553 struct timeval tv[2];
3557 error = copyin(uap->tptr, tv, sizeof(tv));
3562 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3569 kern_truncate(struct nlookupdata *nd, off_t length)
3577 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3578 if ((error = nlookup(nd)) != 0)
3580 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3582 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3584 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3588 if (vp->v_type == VDIR) {
3590 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3592 vattr.va_size = length;
3593 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3600 * truncate(char *path, int pad, off_t length)
3602 * Truncate a file given its path name.
3607 sys_truncate(struct truncate_args *uap)
3609 struct nlookupdata nd;
3613 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3615 error = kern_truncate(&nd, uap->length);
3622 kern_ftruncate(int fd, off_t length)
3624 struct thread *td = curthread;
3625 struct proc *p = td->td_proc;
3633 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3635 if (fp->f_nchandle.ncp) {
3636 error = ncp_writechk(&fp->f_nchandle);
3640 if ((fp->f_flag & FWRITE) == 0) {
3644 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3648 vp = (struct vnode *)fp->f_data;
3649 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3650 if (vp->v_type == VDIR) {
3652 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3654 vattr.va_size = length;
3655 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3664 * ftruncate_args(int fd, int pad, off_t length)
3666 * Truncate a file given a file descriptor.
3671 sys_ftruncate(struct ftruncate_args *uap)
3676 error = kern_ftruncate(uap->fd, uap->length);
3685 * Sync an open file.
3690 sys_fsync(struct fsync_args *uap)
3692 struct thread *td = curthread;
3693 struct proc *p = td->td_proc;
3699 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3702 vp = (struct vnode *)fp->f_data;
3703 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3704 if ((obj = vp->v_object) != NULL)
3705 vm_object_page_clean(obj, 0, 0, 0);
3706 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3707 if (error == 0 && vp->v_mount)
3708 error = buf_fsync(vp);
3717 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3719 struct nchandle fnchd;
3720 struct nchandle tnchd;
3721 struct namecache *ncp;
3728 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3729 if ((error = nlookup(fromnd)) != 0)
3731 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3733 fnchd.mount = fromnd->nl_nch.mount;
3737 * unlock the source nch so we can lookup the target nch without
3738 * deadlocking. The target may or may not exist so we do not check
3739 * for a target vp like kern_mkdir() and other creation functions do.
3741 * The source and target directories are ref'd and rechecked after
3742 * everything is relocked to determine if the source or target file
3745 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3746 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3747 cache_unlock(&fromnd->nl_nch);
3749 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3750 if ((error = nlookup(tond)) != 0) {
3754 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3758 tnchd.mount = tond->nl_nch.mount;
3762 * If the source and target are the same there is nothing to do
3764 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3771 * Mount points cannot be renamed or overwritten
3773 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3782 * Relock the source ncp. cache_relock() will deal with any
3783 * deadlocks against the already-locked tond and will also
3784 * make sure both are resolved.
3786 * NOTE AFTER RELOCKING: The source or target ncp may have become
3787 * invalid while they were unlocked, nc_vp and nc_mount could
3790 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3791 &tond->nl_nch, tond->nl_cred);
3792 fromnd->nl_flags |= NLC_NCPISLOCKED;
3795 * make sure the parent directories linkages are the same
3797 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3798 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3805 * Both the source and target must be within the same filesystem and
3806 * in the same filesystem as their parent directories within the
3807 * namecache topology.
3809 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3812 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3813 mp != tond->nl_nch.mount) {
3820 * Make sure the mount point is writable
3822 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3829 * If the target exists and either the source or target is a directory,
3830 * then both must be directories.
3832 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3835 if (tond->nl_nch.ncp->nc_vp) {
3836 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3838 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3839 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3841 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3847 * You cannot rename a source into itself or a subdirectory of itself.
3848 * We check this by travsersing the target directory upwards looking
3849 * for a match against the source.
3854 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3855 if (fromnd->nl_nch.ncp == ncp) {
3866 * Even though the namespaces are different, they may still represent
3867 * hardlinks to the same file. The filesystem might have a hard time
3868 * with this so we issue a NREMOVE of the source instead of a NRENAME
3869 * when we detect the situation.
3872 fdvp = fromnd->nl_dvp;
3873 tdvp = tond->nl_dvp;
3874 if (fdvp == NULL || tdvp == NULL) {
3876 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3877 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3880 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3881 fdvp, tdvp, tond->nl_cred);
3888 * rename_args(char *from, char *to)
3890 * Rename files. Source and destination must either both be directories,
3891 * or both not be directories. If target is a directory, it must be empty.
3896 sys_rename(struct rename_args *uap)
3898 struct nlookupdata fromnd, tond;
3902 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3904 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3906 error = kern_rename(&fromnd, &tond);
3907 nlookup_done(&tond);
3909 nlookup_done(&fromnd);
3915 * renameat_args(int oldfd, char *old, int newfd, char *new)
3917 * Rename files using paths relative to the directories associated with
3918 * oldfd and newfd. Source and destination must either both be directories,
3919 * or both not be directories. If target is a directory, it must be empty.
3924 sys_renameat(struct renameat_args *uap)
3926 struct nlookupdata oldnd, newnd;
3927 struct file *oldfp, *newfp;
3931 error = nlookup_init_at(&oldnd, &oldfp, uap->oldfd, uap->old,
3934 error = nlookup_init_at(&newnd, &newfp, uap->newfd, uap->new,
3937 error = kern_rename(&oldnd, &newnd);
3938 nlookup_done_at(&newnd, newfp);
3940 nlookup_done_at(&oldnd, oldfp);
3946 kern_mkdir(struct nlookupdata *nd, int mode)
3948 struct thread *td = curthread;
3949 struct proc *p = td->td_proc;
3955 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3956 if ((error = nlookup(nd)) != 0)
3959 if (nd->nl_nch.ncp->nc_vp)
3961 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3964 vattr.va_type = VDIR;
3965 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3968 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3975 * mkdir_args(char *path, int mode)
3977 * Make a directory file.
3982 sys_mkdir(struct mkdir_args *uap)
3984 struct nlookupdata nd;
3988 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3990 error = kern_mkdir(&nd, uap->mode);
3997 * mkdirat_args(int fd, char *path, mode_t mode)
3999 * Make a directory file. The path is relative to the directory associated
4005 sys_mkdirat(struct mkdirat_args *uap)
4007 struct nlookupdata nd;
4012 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4014 error = kern_mkdir(&nd, uap->mode);
4015 nlookup_done_at(&nd, fp);
4021 kern_rmdir(struct nlookupdata *nd)
4026 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4027 if ((error = nlookup(nd)) != 0)
4031 * Do not allow directories representing mount points to be
4032 * deleted, even if empty. Check write perms on mount point
4033 * in case the vnode is aliased (aka nullfs).
4035 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4037 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4039 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4044 * rmdir_args(char *path)
4046 * Remove a directory file.
4051 sys_rmdir(struct rmdir_args *uap)
4053 struct nlookupdata nd;
4057 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4059 error = kern_rmdir(&nd);
4066 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4067 enum uio_seg direction)
4069 struct thread *td = curthread;
4070 struct proc *p = td->td_proc;
4078 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4080 if ((fp->f_flag & FREAD) == 0) {
4084 vp = (struct vnode *)fp->f_data;
4086 if (vp->v_type != VDIR) {
4090 aiov.iov_base = buf;
4091 aiov.iov_len = count;
4092 auio.uio_iov = &aiov;
4093 auio.uio_iovcnt = 1;
4094 auio.uio_rw = UIO_READ;
4095 auio.uio_segflg = direction;
4097 auio.uio_resid = count;
4098 loff = auio.uio_offset = fp->f_offset;
4099 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4100 fp->f_offset = auio.uio_offset;
4103 if (count == auio.uio_resid) {
4104 if (union_dircheckp) {
4105 error = union_dircheckp(td, &vp, fp);
4112 if ((vp->v_flag & VROOT) &&
4113 (vp->v_mount->mnt_flag & MNT_UNION)) {
4114 struct vnode *tvp = vp;
4115 vp = vp->v_mount->mnt_vnodecovered;
4126 * WARNING! *basep may not be wide enough to accomodate the
4127 * seek offset. XXX should we hack this to return the upper 32 bits
4128 * for offsets greater then 4G?
4131 *basep = (long)loff;
4133 *res = count - auio.uio_resid;
4140 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4142 * Read a block of directory entries in a file system independent format.
4147 sys_getdirentries(struct getdirentries_args *uap)
4153 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4154 &uap->sysmsg_result, UIO_USERSPACE);
4157 if (error == 0 && uap->basep)
4158 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4163 * getdents_args(int fd, char *buf, size_t count)
4168 sys_getdents(struct getdents_args *uap)
4173 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4174 &uap->sysmsg_result, UIO_USERSPACE);
4181 * Set the mode mask for creation of filesystem nodes.
4183 * umask(int newmask)
4188 sys_umask(struct umask_args *uap)
4190 struct thread *td = curthread;
4191 struct proc *p = td->td_proc;
4192 struct filedesc *fdp;
4195 uap->sysmsg_result = fdp->fd_cmask;
4196 fdp->fd_cmask = uap->newmask & ALLPERMS;
4201 * revoke(char *path)
4203 * Void all references to file by ripping underlying filesystem
4209 sys_revoke(struct revoke_args *uap)
4211 struct nlookupdata nd;
4219 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4221 error = nlookup(&nd);
4223 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4224 cred = crhold(nd.nl_cred);
4228 error = VOP_GETATTR(vp, &vattr);
4229 if (error == 0 && cred->cr_uid != vattr.va_uid)
4230 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4231 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4233 error = vrevoke(vp, cred);
4234 } else if (error == 0) {
4235 error = vrevoke(vp, cred);
4246 * getfh_args(char *fname, fhandle_t *fhp)
4248 * Get (NFS) file handle
4250 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4251 * mount. This allows nullfs mounts to be explicitly exported.
4253 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4255 * nullfs mounts of subdirectories are not safe. That is, it will
4256 * work, but you do not really have protection against access to
4257 * the related parent directories.
4262 sys_getfh(struct getfh_args *uap)
4264 struct thread *td = curthread;
4265 struct nlookupdata nd;
4272 * Must be super user
4274 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4279 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4281 error = nlookup(&nd);
4283 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4284 mp = nd.nl_nch.mount;
4287 bzero(&fh, sizeof(fh));
4288 fh.fh_fsid = mp->mnt_stat.f_fsid;
4289 error = VFS_VPTOFH(vp, &fh.fh_fid);
4292 error = copyout(&fh, uap->fhp, sizeof(fh));
4299 * fhopen_args(const struct fhandle *u_fhp, int flags)
4301 * syscall for the rpc.lockd to use to translate a NFS file handle into
4302 * an open descriptor.
4304 * warning: do not remove the priv_check() call or this becomes one giant
4310 sys_fhopen(struct fhopen_args *uap)
4312 struct thread *td = curthread;
4313 struct filedesc *fdp = td->td_proc->p_fd;
4318 struct vattr *vap = &vat;
4320 int fmode, mode, error, type;
4326 * Must be super user
4328 error = priv_check(td, PRIV_ROOT);
4332 fmode = FFLAGS(uap->flags);
4335 * Why not allow a non-read/write open for our lockd?
4337 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4339 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4344 * Find the mount point
4347 mp = vfs_getvfs(&fhp.fh_fsid);
4352 /* now give me my vnode, it gets returned to me locked */
4353 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4357 * from now on we have to make sure not
4358 * to forget about the vnode
4359 * any error that causes an abort must vput(vp)
4360 * just set error = err and 'goto bad;'.
4366 if (vp->v_type == VLNK) {
4370 if (vp->v_type == VSOCK) {
4375 if (fmode & (FWRITE | O_TRUNC)) {
4376 if (vp->v_type == VDIR) {
4380 error = vn_writechk(vp, NULL);
4388 error = VOP_ACCESS(vp, mode, td->td_ucred);
4392 if (fmode & O_TRUNC) {
4393 vn_unlock(vp); /* XXX */
4394 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4397 error = VOP_SETATTR(vp, vap, td->td_ucred);
4403 * VOP_OPEN needs the file pointer so it can potentially override
4406 * WARNING! no f_nchandle will be associated when fhopen()ing a
4409 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4413 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4416 * setting f_ops this way prevents VOP_CLOSE from being
4417 * called or fdrop() releasing the vp from v_data. Since
4418 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4420 fp->f_ops = &badfileops;
4426 * The fp is given its own reference, we still have our ref and lock.
4428 * Assert that all regular files must be created with a VM object.
4430 if (vp->v_type == VREG && vp->v_object == NULL) {
4431 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4436 * The open was successful. Handle any locking requirements.
4438 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4439 lf.l_whence = SEEK_SET;
4442 if (fmode & O_EXLOCK)
4443 lf.l_type = F_WRLCK;
4445 lf.l_type = F_RDLCK;
4446 if (fmode & FNONBLOCK)
4451 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4453 * release our private reference.
4455 fsetfd(fdp, NULL, indx);
4460 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4461 fp->f_flag |= FHASLOCK;
4465 * Clean up. Associate the file pointer with the previously
4466 * reserved descriptor and return it.
4470 fsetfd(fdp, fp, indx);
4472 uap->sysmsg_result = indx;
4476 fsetfd(fdp, NULL, indx);
4486 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4491 sys_fhstat(struct fhstat_args *uap)
4493 struct thread *td = curthread;
4501 * Must be super user
4503 error = priv_check(td, PRIV_ROOT);
4507 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4512 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4515 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4516 error = vn_stat(vp, &sb, td->td_ucred);
4522 error = copyout(&sb, uap->sb, sizeof(sb));
4527 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4532 sys_fhstatfs(struct fhstatfs_args *uap)
4534 struct thread *td = curthread;
4535 struct proc *p = td->td_proc;
4540 char *fullpath, *freepath;
4545 * Must be super user
4547 if ((error = priv_check(td, PRIV_ROOT)))
4550 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4555 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4559 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4564 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4569 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4572 error = mount_path(p, mp, &fullpath, &freepath);
4575 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4576 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4577 kfree(freepath, M_TEMP);
4579 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4580 if (priv_check(td, PRIV_ROOT)) {
4581 bcopy(sp, &sb, sizeof(sb));
4582 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4585 error = copyout(sp, uap->buf, sizeof(*sp));
4592 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4597 sys_fhstatvfs(struct fhstatvfs_args *uap)
4599 struct thread *td = curthread;
4600 struct proc *p = td->td_proc;
4608 * Must be super user
4610 if ((error = priv_check(td, PRIV_ROOT)))
4613 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4618 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4622 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4627 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4630 sp = &mp->mnt_vstat;
4632 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4636 if (mp->mnt_flag & MNT_RDONLY)
4637 sp->f_flag |= ST_RDONLY;
4638 if (mp->mnt_flag & MNT_NOSUID)
4639 sp->f_flag |= ST_NOSUID;
4640 error = copyout(sp, uap->buf, sizeof(*sp));
4648 * Syscall to push extended attribute configuration information into the
4649 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4650 * a command (int cmd), and attribute name and misc data. For now, the
4651 * attribute name is left in userspace for consumption by the VFS_op.
4652 * It will probably be changed to be copied into sysspace by the
4653 * syscall in the future, once issues with various consumers of the
4654 * attribute code have raised their hands.
4656 * Currently this is used only by UFS Extended Attributes.
4661 sys_extattrctl(struct extattrctl_args *uap)
4663 struct nlookupdata nd;
4665 char attrname[EXTATTR_MAXNAMELEN];
4675 if (error == 0 && uap->filename) {
4676 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4679 error = nlookup(&nd);
4681 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4685 if (error == 0 && uap->attrname) {
4686 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4691 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4693 error = nlookup(&nd);
4695 error = ncp_writechk(&nd.nl_nch);
4697 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4699 uap->attrname, nd.nl_cred);
4710 * Syscall to get a named extended attribute on a file or directory.
4715 sys_extattr_set_file(struct extattr_set_file_args *uap)
4717 char attrname[EXTATTR_MAXNAMELEN];
4718 struct nlookupdata nd;
4724 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4731 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4733 error = nlookup(&nd);
4735 error = ncp_writechk(&nd.nl_nch);
4737 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4744 bzero(&auio, sizeof(auio));
4745 aiov.iov_base = uap->data;
4746 aiov.iov_len = uap->nbytes;
4747 auio.uio_iov = &aiov;
4748 auio.uio_iovcnt = 1;
4749 auio.uio_offset = 0;
4750 auio.uio_resid = uap->nbytes;
4751 auio.uio_rw = UIO_WRITE;
4752 auio.uio_td = curthread;
4754 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4764 * Syscall to get a named extended attribute on a file or directory.
4769 sys_extattr_get_file(struct extattr_get_file_args *uap)
4771 char attrname[EXTATTR_MAXNAMELEN];
4772 struct nlookupdata nd;
4778 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4785 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4787 error = nlookup(&nd);
4789 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4796 bzero(&auio, sizeof(auio));
4797 aiov.iov_base = uap->data;
4798 aiov.iov_len = uap->nbytes;
4799 auio.uio_iov = &aiov;
4800 auio.uio_iovcnt = 1;
4801 auio.uio_offset = 0;
4802 auio.uio_resid = uap->nbytes;
4803 auio.uio_rw = UIO_READ;
4804 auio.uio_td = curthread;
4806 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4808 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4817 * Syscall to delete a named extended attribute from a file or directory.
4818 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4823 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4825 char attrname[EXTATTR_MAXNAMELEN];
4826 struct nlookupdata nd;
4830 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4835 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4837 error = nlookup(&nd);
4839 error = ncp_writechk(&nd.nl_nch);
4841 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4843 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4844 attrname, NULL, nd.nl_cred);
4854 * Determine if the mount is visible to the process.
4857 chroot_visible_mnt(struct mount *mp, struct proc *p)
4859 struct nchandle nch;
4862 * Traverse from the mount point upwards. If we hit the process
4863 * root then the mount point is visible to the process.
4865 nch = mp->mnt_ncmountpt;
4867 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4868 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4871 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4872 nch = nch.mount->mnt_ncmounton;
4874 nch.ncp = nch.ncp->nc_parent;
4879 * If the mount point is not visible to the process, but the
4880 * process root is in a subdirectory of the mount, return
4883 if (p->p_fd->fd_nrdir.mount == mp)