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 struct nlookupdata nd;
1984 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1986 error = kern_open(&nd, uap->flags,
1987 uap->mode, &uap->sysmsg_result);
1994 * openat_args(int fd, char *path, int flags, int mode)
1999 sys_openat(struct openat_args *uap)
2001 struct nlookupdata nd;
2005 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2007 error = kern_open(&nd, uap->flags, uap->mode,
2008 &uap->sysmsg_result);
2010 nlookup_done_at(&nd, fp);
2015 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2017 struct thread *td = curthread;
2018 struct proc *p = td->td_proc;
2027 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2028 vattr.va_rmajor = rmajor;
2029 vattr.va_rminor = rminor;
2031 switch (mode & S_IFMT) {
2032 case S_IFMT: /* used by badsect to flag bad sectors */
2033 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2034 vattr.va_type = VBAD;
2037 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2038 vattr.va_type = VCHR;
2041 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2042 vattr.va_type = VBLK;
2045 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2048 case S_IFDIR: /* special directories support for HAMMER */
2049 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2050 vattr.va_type = VDIR;
2061 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2062 if ((error = nlookup(nd)) != 0)
2064 if (nd->nl_nch.ncp->nc_vp)
2066 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2070 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2071 nd->nl_cred, NAMEI_CREATE);
2074 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2075 &vp, nd->nl_cred, &vattr);
2083 * mknod_args(char *path, int mode, int dev)
2085 * Create a special file.
2090 sys_mknod(struct mknod_args *uap)
2092 struct nlookupdata nd;
2096 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2098 error = kern_mknod(&nd, uap->mode,
2099 umajor(uap->dev), uminor(uap->dev));
2107 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2109 * Create a special file. The path is relative to the directory associated
2115 sys_mknodat(struct mknodat_args *uap)
2117 struct nlookupdata nd;
2122 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2124 error = kern_mknod(&nd, uap->mode,
2125 umajor(uap->dev), uminor(uap->dev));
2127 nlookup_done_at(&nd, fp);
2133 kern_mkfifo(struct nlookupdata *nd, int mode)
2135 struct thread *td = curthread;
2136 struct proc *p = td->td_proc;
2143 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2144 if ((error = nlookup(nd)) != 0)
2146 if (nd->nl_nch.ncp->nc_vp)
2148 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2152 vattr.va_type = VFIFO;
2153 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2155 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2162 * mkfifo_args(char *path, int mode)
2164 * Create a named pipe.
2169 sys_mkfifo(struct mkfifo_args *uap)
2171 struct nlookupdata nd;
2175 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2177 error = kern_mkfifo(&nd, uap->mode);
2184 * mkfifoat_args(int fd, char *path, mode_t mode)
2186 * Create a named pipe. The path is relative to the directory associated
2192 sys_mkfifoat(struct mkfifoat_args *uap)
2194 struct nlookupdata nd;
2199 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2201 error = kern_mkfifo(&nd, uap->mode);
2202 nlookup_done_at(&nd, fp);
2207 static int hardlink_check_uid = 0;
2208 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2209 &hardlink_check_uid, 0,
2210 "Unprivileged processes cannot create hard links to files owned by other "
2212 static int hardlink_check_gid = 0;
2213 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2214 &hardlink_check_gid, 0,
2215 "Unprivileged processes cannot create hard links to files owned by other "
2219 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2225 * Shortcut if disabled
2227 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2231 * Privileged user can always hardlink
2233 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2237 * Otherwise only if the originating file is owned by the
2238 * same user or group. Note that any group is allowed if
2239 * the file is owned by the caller.
2241 error = VOP_GETATTR(vp, &va);
2245 if (hardlink_check_uid) {
2246 if (cred->cr_uid != va.va_uid)
2250 if (hardlink_check_gid) {
2251 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2259 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2261 struct thread *td = curthread;
2266 * Lookup the source and obtained a locked vnode.
2268 * You may only hardlink a file which you have write permission
2269 * on or which you own.
2271 * XXX relookup on vget failure / race ?
2274 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2275 if ((error = nlookup(nd)) != 0)
2277 vp = nd->nl_nch.ncp->nc_vp;
2278 KKASSERT(vp != NULL);
2279 if (vp->v_type == VDIR)
2280 return (EPERM); /* POSIX */
2281 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2283 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2287 * Unlock the source so we can lookup the target without deadlocking
2288 * (XXX vp is locked already, possible other deadlock?). The target
2291 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2292 nd->nl_flags &= ~NLC_NCPISLOCKED;
2293 cache_unlock(&nd->nl_nch);
2296 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2297 if ((error = nlookup(linknd)) != 0) {
2301 if (linknd->nl_nch.ncp->nc_vp) {
2305 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2311 * Finally run the new API VOP.
2313 error = can_hardlink(vp, td, td->td_ucred);
2315 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2316 vp, linknd->nl_cred);
2323 * link_args(char *path, char *link)
2325 * Make a hard file link.
2330 sys_link(struct link_args *uap)
2332 struct nlookupdata nd, linknd;
2336 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2338 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2340 error = kern_link(&nd, &linknd);
2341 nlookup_done(&linknd);
2349 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2357 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2358 if ((error = nlookup(nd)) != 0)
2360 if (nd->nl_nch.ncp->nc_vp)
2362 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2366 vattr.va_mode = mode;
2367 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2374 * symlink(char *path, char *link)
2376 * Make a symbolic link.
2381 sys_symlink(struct symlink_args *uap)
2383 struct thread *td = curthread;
2384 struct nlookupdata nd;
2389 path = objcache_get(namei_oc, M_WAITOK);
2390 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2393 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2395 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2396 error = kern_symlink(&nd, path, mode);
2401 objcache_put(namei_oc, path);
2406 * symlinkat_args(char *path1, int fd, char *path2)
2408 * Make a symbolic link. The path2 argument is relative to the directory
2409 * associated with fd.
2414 sys_symlinkat(struct symlinkat_args *uap)
2416 struct thread *td = curthread;
2417 struct nlookupdata nd;
2423 path1 = objcache_get(namei_oc, M_WAITOK);
2424 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2427 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2430 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2431 error = kern_symlink(&nd, path1, mode);
2433 nlookup_done_at(&nd, fp);
2436 objcache_put(namei_oc, path1);
2441 * undelete_args(char *path)
2443 * Delete a whiteout from the filesystem.
2448 sys_undelete(struct undelete_args *uap)
2450 struct nlookupdata nd;
2454 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2456 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2458 error = nlookup(&nd);
2460 error = ncp_writechk(&nd.nl_nch);
2462 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2471 kern_unlink(struct nlookupdata *nd)
2476 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2477 if ((error = nlookup(nd)) != 0)
2479 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2481 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2486 * unlink_args(char *path)
2488 * Delete a name from the filesystem.
2493 sys_unlink(struct unlink_args *uap)
2495 struct nlookupdata nd;
2499 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2501 error = kern_unlink(&nd);
2509 * unlinkat_args(int fd, char *path, int flags)
2511 * Delete the file or directory entry pointed to by fd/path.
2516 sys_unlinkat(struct unlinkat_args *uap)
2518 struct nlookupdata nd;
2522 if (uap->flags & ~AT_REMOVEDIR)
2526 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2528 if (uap->flags & AT_REMOVEDIR)
2529 error = kern_rmdir(&nd);
2531 error = kern_unlink(&nd);
2533 nlookup_done_at(&nd, fp);
2542 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2544 struct thread *td = curthread;
2545 struct proc *p = td->td_proc;
2552 fp = holdfp(p->p_fd, fd, -1);
2555 if (fp->f_type != DTYPE_VNODE) {
2559 vp = (struct vnode *)fp->f_data;
2563 spin_lock(&fp->f_spin);
2564 new_offset = fp->f_offset + offset;
2569 error = VOP_GETATTR(vp, &vattr);
2571 spin_lock(&fp->f_spin);
2572 new_offset = offset + vattr.va_size;
2575 new_offset = offset;
2577 spin_lock(&fp->f_spin);
2582 spin_lock(&fp->f_spin);
2587 * Validate the seek position. Negative offsets are not allowed
2588 * for regular files or directories.
2590 * Normally we would also not want to allow negative offsets for
2591 * character and block-special devices. However kvm addresses
2592 * on 64 bit architectures might appear to be negative and must
2596 if (new_offset < 0 &&
2597 (vp->v_type == VREG || vp->v_type == VDIR)) {
2600 fp->f_offset = new_offset;
2603 *res = fp->f_offset;
2604 spin_unlock(&fp->f_spin);
2611 * lseek_args(int fd, int pad, off_t offset, int whence)
2613 * Reposition read/write file offset.
2618 sys_lseek(struct lseek_args *uap)
2622 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2623 &uap->sysmsg_offset);
2629 * Check if current process can access given file. amode is a bitmask of *_OK
2630 * access bits. flags is a bitmask of AT_* flags.
2633 kern_access(struct nlookupdata *nd, int amode, int flags)
2638 if (flags & ~AT_EACCESS)
2640 if ((error = nlookup(nd)) != 0)
2643 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2647 /* Flags == 0 means only check for existence. */
2656 if ((mode & VWRITE) == 0 ||
2657 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2658 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2661 * If the file handle is stale we have to re-resolve the
2662 * entry. This is a hack at the moment.
2664 if (error == ESTALE) {
2666 cache_setunresolved(&nd->nl_nch);
2667 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2680 * access_args(char *path, int flags)
2682 * Check access permissions.
2687 sys_access(struct access_args *uap)
2689 struct nlookupdata nd;
2693 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2695 error = kern_access(&nd, uap->flags, 0);
2703 * faccessat_args(int fd, char *path, int amode, int flags)
2705 * Check access permissions.
2710 sys_faccessat(struct faccessat_args *uap)
2712 struct nlookupdata nd;
2717 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2720 error = kern_access(&nd, uap->amode, uap->flags);
2721 nlookup_done_at(&nd, fp);
2731 kern_stat(struct nlookupdata *nd, struct stat *st)
2737 if ((error = nlookup(nd)) != 0)
2740 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2744 if ((error = vget(vp, LK_SHARED)) != 0)
2746 error = vn_stat(vp, st, nd->nl_cred);
2749 * If the file handle is stale we have to re-resolve the entry. This
2750 * is a hack at the moment.
2752 if (error == ESTALE) {
2754 cache_setunresolved(&nd->nl_nch);
2755 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2765 * stat_args(char *path, struct stat *ub)
2767 * Get file status; this version follows links.
2772 sys_stat(struct stat_args *uap)
2774 struct nlookupdata nd;
2778 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2780 error = kern_stat(&nd, &st);
2782 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2789 * lstat_args(char *path, struct stat *ub)
2791 * Get file status; this version does not follow links.
2796 sys_lstat(struct lstat_args *uap)
2798 struct nlookupdata nd;
2802 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2804 error = kern_stat(&nd, &st);
2806 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2813 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2815 * Get status of file pointed to by fd/path.
2820 sys_fstatat(struct fstatat_args *uap)
2822 struct nlookupdata nd;
2828 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2831 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2833 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2834 UIO_USERSPACE, flags);
2836 error = kern_stat(&nd, &st);
2838 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2840 nlookup_done_at(&nd, fp);
2845 * pathconf_Args(char *path, int name)
2847 * Get configurable pathname variables.
2852 sys_pathconf(struct pathconf_args *uap)
2854 struct nlookupdata nd;
2860 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2862 error = nlookup(&nd);
2864 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2867 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2876 * kern_readlink isn't properly split yet. There is a copyin burried
2877 * in VOP_READLINK().
2880 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2882 struct thread *td = curthread;
2888 if ((error = nlookup(nd)) != 0)
2890 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2893 if (vp->v_type != VLNK) {
2896 aiov.iov_base = buf;
2897 aiov.iov_len = count;
2898 auio.uio_iov = &aiov;
2899 auio.uio_iovcnt = 1;
2900 auio.uio_offset = 0;
2901 auio.uio_rw = UIO_READ;
2902 auio.uio_segflg = UIO_USERSPACE;
2904 auio.uio_resid = count;
2905 error = VOP_READLINK(vp, &auio, td->td_ucred);
2908 *res = count - auio.uio_resid;
2913 * readlink_args(char *path, char *buf, int count)
2915 * Return target name of a symbolic link.
2920 sys_readlink(struct readlink_args *uap)
2922 struct nlookupdata nd;
2926 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2928 error = kern_readlink(&nd, uap->buf, uap->count,
2929 &uap->sysmsg_result);
2937 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2939 * Return target name of a symbolic link. The path is relative to the
2940 * directory associated with fd.
2945 sys_readlinkat(struct readlinkat_args *uap)
2947 struct nlookupdata nd;
2952 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2954 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2955 &uap->sysmsg_result);
2957 nlookup_done_at(&nd, fp);
2963 setfflags(struct vnode *vp, int flags)
2965 struct thread *td = curthread;
2970 * Prevent non-root users from setting flags on devices. When
2971 * a device is reused, users can retain ownership of the device
2972 * if they are allowed to set flags and programs assume that
2973 * chown can't fail when done as root.
2975 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2976 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2980 * note: vget is required for any operation that might mod the vnode
2981 * so VINACTIVE is properly cleared.
2983 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2985 vattr.va_flags = flags;
2986 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2993 * chflags(char *path, int flags)
2995 * Change flags of a file given a path name.
3000 sys_chflags(struct chflags_args *uap)
3002 struct nlookupdata nd;
3008 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3010 error = nlookup(&nd);
3012 error = ncp_writechk(&nd.nl_nch);
3014 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3017 error = setfflags(vp, uap->flags);
3025 * lchflags(char *path, int flags)
3027 * Change flags of a file given a path name, but don't follow symlinks.
3032 sys_lchflags(struct lchflags_args *uap)
3034 struct nlookupdata nd;
3040 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3042 error = nlookup(&nd);
3044 error = ncp_writechk(&nd.nl_nch);
3046 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3049 error = setfflags(vp, uap->flags);
3057 * fchflags_args(int fd, int flags)
3059 * Change flags of a file given a file descriptor.
3064 sys_fchflags(struct fchflags_args *uap)
3066 struct thread *td = curthread;
3067 struct proc *p = td->td_proc;
3071 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3074 if (fp->f_nchandle.ncp)
3075 error = ncp_writechk(&fp->f_nchandle);
3077 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3084 setfmode(struct vnode *vp, int mode)
3086 struct thread *td = curthread;
3091 * note: vget is required for any operation that might mod the vnode
3092 * so VINACTIVE is properly cleared.
3094 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3096 vattr.va_mode = mode & ALLPERMS;
3097 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3104 kern_chmod(struct nlookupdata *nd, int mode)
3109 if ((error = nlookup(nd)) != 0)
3111 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3113 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3114 error = setfmode(vp, mode);
3120 * chmod_args(char *path, int mode)
3122 * Change mode of a file given path name.
3127 sys_chmod(struct chmod_args *uap)
3129 struct nlookupdata nd;
3133 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3135 error = kern_chmod(&nd, uap->mode);
3142 * lchmod_args(char *path, int mode)
3144 * Change mode of a file given path name (don't follow links.)
3149 sys_lchmod(struct lchmod_args *uap)
3151 struct nlookupdata nd;
3155 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3157 error = kern_chmod(&nd, uap->mode);
3164 * fchmod_args(int fd, int mode)
3166 * Change mode of a file given a file descriptor.
3171 sys_fchmod(struct fchmod_args *uap)
3173 struct thread *td = curthread;
3174 struct proc *p = td->td_proc;
3178 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3181 if (fp->f_nchandle.ncp)
3182 error = ncp_writechk(&fp->f_nchandle);
3184 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3191 * fchmodat_args(char *path, int mode)
3193 * Change mode of a file pointed to by fd/path.
3198 sys_fchmodat(struct fchmodat_args *uap)
3200 struct nlookupdata nd;
3205 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3207 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3210 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3211 UIO_USERSPACE, flags);
3213 error = kern_chmod(&nd, uap->mode);
3214 nlookup_done_at(&nd, fp);
3220 setfown(struct vnode *vp, uid_t uid, gid_t gid)
3222 struct thread *td = curthread;
3227 * note: vget is required for any operation that might mod the vnode
3228 * so VINACTIVE is properly cleared.
3230 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3234 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3241 kern_chown(struct nlookupdata *nd, int uid, int gid)
3246 if ((error = nlookup(nd)) != 0)
3248 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3250 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3251 error = setfown(vp, uid, gid);
3257 * chown(char *path, int uid, int gid)
3259 * Set ownership given a path name.
3264 sys_chown(struct chown_args *uap)
3266 struct nlookupdata nd;
3270 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3272 error = kern_chown(&nd, uap->uid, uap->gid);
3279 * lchown_args(char *path, int uid, int gid)
3281 * Set ownership given a path name, do not cross symlinks.
3286 sys_lchown(struct lchown_args *uap)
3288 struct nlookupdata nd;
3292 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3294 error = kern_chown(&nd, uap->uid, uap->gid);
3301 * fchown_args(int fd, int uid, int gid)
3303 * Set ownership given a file descriptor.
3308 sys_fchown(struct fchown_args *uap)
3310 struct thread *td = curthread;
3311 struct proc *p = td->td_proc;
3315 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3318 if (fp->f_nchandle.ncp)
3319 error = ncp_writechk(&fp->f_nchandle);
3321 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
3328 * fchownat(int fd, char *path, int uid, int gid, int flags)
3330 * Set ownership of file pointed to by fd/path.
3335 sys_fchownat(struct fchownat_args *uap)
3337 struct nlookupdata nd;
3342 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3344 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3347 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3348 UIO_USERSPACE, flags);
3350 error = kern_chown(&nd, uap->uid, uap->gid);
3351 nlookup_done_at(&nd, fp);
3358 getutimes(const struct timeval *tvp, struct timespec *tsp)
3360 struct timeval tv[2];
3364 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3367 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3368 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3374 setutimes(struct vnode *vp, struct vattr *vattr,
3375 const struct timespec *ts, int nullflag)
3377 struct thread *td = curthread;
3381 vattr->va_atime = ts[0];
3382 vattr->va_mtime = ts[1];
3384 vattr->va_vaflags |= VA_UTIMES_NULL;
3385 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3391 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3393 struct timespec ts[2];
3398 if ((error = getutimes(tptr, ts)) != 0)
3402 * NOTE: utimes() succeeds for the owner even if the file
3403 * is not user-writable.
3405 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3407 if ((error = nlookup(nd)) != 0)
3409 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3411 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3415 * note: vget is required for any operation that might mod the vnode
3416 * so VINACTIVE is properly cleared.
3418 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3419 error = vget(vp, LK_EXCLUSIVE);
3421 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3430 * utimes_args(char *path, struct timeval *tptr)
3432 * Set the access and modification times of a file.
3437 sys_utimes(struct utimes_args *uap)
3439 struct timeval tv[2];
3440 struct nlookupdata nd;
3444 error = copyin(uap->tptr, tv, sizeof(tv));
3449 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3451 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3458 * lutimes_args(char *path, struct timeval *tptr)
3460 * Set the access and modification times of a file.
3465 sys_lutimes(struct lutimes_args *uap)
3467 struct timeval tv[2];
3468 struct nlookupdata nd;
3472 error = copyin(uap->tptr, tv, sizeof(tv));
3477 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3479 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3486 * Set utimes on a file descriptor. The creds used to open the
3487 * file are used to determine whether the operation is allowed
3491 kern_futimes(int fd, struct timeval *tptr)
3493 struct thread *td = curthread;
3494 struct proc *p = td->td_proc;
3495 struct timespec ts[2];
3501 error = getutimes(tptr, ts);
3504 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3506 if (fp->f_nchandle.ncp)
3507 error = ncp_writechk(&fp->f_nchandle);
3510 error = vget(vp, LK_EXCLUSIVE);
3512 error = VOP_GETATTR(vp, &vattr);
3514 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3518 error = setutimes(vp, &vattr, ts,
3529 * futimes_args(int fd, struct timeval *tptr)
3531 * Set the access and modification times of a file.
3536 sys_futimes(struct futimes_args *uap)
3538 struct timeval tv[2];
3542 error = copyin(uap->tptr, tv, sizeof(tv));
3547 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3554 kern_truncate(struct nlookupdata *nd, off_t length)
3562 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3563 if ((error = nlookup(nd)) != 0)
3565 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3567 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3569 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3573 if (vp->v_type == VDIR) {
3575 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3577 vattr.va_size = length;
3578 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3585 * truncate(char *path, int pad, off_t length)
3587 * Truncate a file given its path name.
3592 sys_truncate(struct truncate_args *uap)
3594 struct nlookupdata nd;
3598 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3600 error = kern_truncate(&nd, uap->length);
3607 kern_ftruncate(int fd, off_t length)
3609 struct thread *td = curthread;
3610 struct proc *p = td->td_proc;
3618 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3620 if (fp->f_nchandle.ncp) {
3621 error = ncp_writechk(&fp->f_nchandle);
3625 if ((fp->f_flag & FWRITE) == 0) {
3629 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3633 vp = (struct vnode *)fp->f_data;
3634 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3635 if (vp->v_type == VDIR) {
3637 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3639 vattr.va_size = length;
3640 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3649 * ftruncate_args(int fd, int pad, off_t length)
3651 * Truncate a file given a file descriptor.
3656 sys_ftruncate(struct ftruncate_args *uap)
3661 error = kern_ftruncate(uap->fd, uap->length);
3670 * Sync an open file.
3675 sys_fsync(struct fsync_args *uap)
3677 struct thread *td = curthread;
3678 struct proc *p = td->td_proc;
3684 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3687 vp = (struct vnode *)fp->f_data;
3688 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3689 if ((obj = vp->v_object) != NULL)
3690 vm_object_page_clean(obj, 0, 0, 0);
3691 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3692 if (error == 0 && vp->v_mount)
3693 error = buf_fsync(vp);
3702 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3704 struct nchandle fnchd;
3705 struct nchandle tnchd;
3706 struct namecache *ncp;
3713 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3714 if ((error = nlookup(fromnd)) != 0)
3716 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3718 fnchd.mount = fromnd->nl_nch.mount;
3722 * unlock the source nch so we can lookup the target nch without
3723 * deadlocking. The target may or may not exist so we do not check
3724 * for a target vp like kern_mkdir() and other creation functions do.
3726 * The source and target directories are ref'd and rechecked after
3727 * everything is relocked to determine if the source or target file
3730 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3731 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3732 cache_unlock(&fromnd->nl_nch);
3734 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3735 if ((error = nlookup(tond)) != 0) {
3739 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3743 tnchd.mount = tond->nl_nch.mount;
3747 * If the source and target are the same there is nothing to do
3749 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3756 * Mount points cannot be renamed or overwritten
3758 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3767 * Relock the source ncp. cache_relock() will deal with any
3768 * deadlocks against the already-locked tond and will also
3769 * make sure both are resolved.
3771 * NOTE AFTER RELOCKING: The source or target ncp may have become
3772 * invalid while they were unlocked, nc_vp and nc_mount could
3775 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3776 &tond->nl_nch, tond->nl_cred);
3777 fromnd->nl_flags |= NLC_NCPISLOCKED;
3780 * make sure the parent directories linkages are the same
3782 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3783 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3790 * Both the source and target must be within the same filesystem and
3791 * in the same filesystem as their parent directories within the
3792 * namecache topology.
3794 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3797 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3798 mp != tond->nl_nch.mount) {
3805 * Make sure the mount point is writable
3807 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3814 * If the target exists and either the source or target is a directory,
3815 * then both must be directories.
3817 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3820 if (tond->nl_nch.ncp->nc_vp) {
3821 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3823 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3824 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3826 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3832 * You cannot rename a source into itself or a subdirectory of itself.
3833 * We check this by travsersing the target directory upwards looking
3834 * for a match against the source.
3839 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3840 if (fromnd->nl_nch.ncp == ncp) {
3851 * Even though the namespaces are different, they may still represent
3852 * hardlinks to the same file. The filesystem might have a hard time
3853 * with this so we issue a NREMOVE of the source instead of a NRENAME
3854 * when we detect the situation.
3857 fdvp = fromnd->nl_dvp;
3858 tdvp = tond->nl_dvp;
3859 if (fdvp == NULL || tdvp == NULL) {
3861 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3862 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3865 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3866 fdvp, tdvp, tond->nl_cred);
3873 * rename_args(char *from, char *to)
3875 * Rename files. Source and destination must either both be directories,
3876 * or both not be directories. If target is a directory, it must be empty.
3881 sys_rename(struct rename_args *uap)
3883 struct nlookupdata fromnd, tond;
3887 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3889 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3891 error = kern_rename(&fromnd, &tond);
3892 nlookup_done(&tond);
3894 nlookup_done(&fromnd);
3900 * renameat_args(int oldfd, char *old, int newfd, char *new)
3902 * Rename files using paths relative to the directories associated with
3903 * oldfd and newfd. Source and destination must either both be directories,
3904 * or both not be directories. If target is a directory, it must be empty.
3909 sys_renameat(struct renameat_args *uap)
3911 struct nlookupdata oldnd, newnd;
3912 struct file *oldfp, *newfp;
3916 error = nlookup_init_at(&oldnd, &oldfp, uap->oldfd, uap->old,
3919 error = nlookup_init_at(&newnd, &newfp, uap->newfd, uap->new,
3922 error = kern_rename(&oldnd, &newnd);
3923 nlookup_done_at(&newnd, newfp);
3925 nlookup_done_at(&oldnd, oldfp);
3931 kern_mkdir(struct nlookupdata *nd, int mode)
3933 struct thread *td = curthread;
3934 struct proc *p = td->td_proc;
3940 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3941 if ((error = nlookup(nd)) != 0)
3944 if (nd->nl_nch.ncp->nc_vp)
3946 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3949 vattr.va_type = VDIR;
3950 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3953 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3960 * mkdir_args(char *path, int mode)
3962 * Make a directory file.
3967 sys_mkdir(struct mkdir_args *uap)
3969 struct nlookupdata nd;
3973 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3975 error = kern_mkdir(&nd, uap->mode);
3982 * mkdirat_args(int fd, char *path, mode_t mode)
3984 * Make a directory file. The path is relative to the directory associated
3990 sys_mkdirat(struct mkdirat_args *uap)
3992 struct nlookupdata nd;
3997 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3999 error = kern_mkdir(&nd, uap->mode);
4000 nlookup_done_at(&nd, fp);
4006 kern_rmdir(struct nlookupdata *nd)
4011 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4012 if ((error = nlookup(nd)) != 0)
4016 * Do not allow directories representing mount points to be
4017 * deleted, even if empty. Check write perms on mount point
4018 * in case the vnode is aliased (aka nullfs).
4020 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4022 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4024 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4029 * rmdir_args(char *path)
4031 * Remove a directory file.
4036 sys_rmdir(struct rmdir_args *uap)
4038 struct nlookupdata nd;
4042 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4044 error = kern_rmdir(&nd);
4051 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4052 enum uio_seg direction)
4054 struct thread *td = curthread;
4055 struct proc *p = td->td_proc;
4063 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4065 if ((fp->f_flag & FREAD) == 0) {
4069 vp = (struct vnode *)fp->f_data;
4071 if (vp->v_type != VDIR) {
4075 aiov.iov_base = buf;
4076 aiov.iov_len = count;
4077 auio.uio_iov = &aiov;
4078 auio.uio_iovcnt = 1;
4079 auio.uio_rw = UIO_READ;
4080 auio.uio_segflg = direction;
4082 auio.uio_resid = count;
4083 loff = auio.uio_offset = fp->f_offset;
4084 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4085 fp->f_offset = auio.uio_offset;
4088 if (count == auio.uio_resid) {
4089 if (union_dircheckp) {
4090 error = union_dircheckp(td, &vp, fp);
4097 if ((vp->v_flag & VROOT) &&
4098 (vp->v_mount->mnt_flag & MNT_UNION)) {
4099 struct vnode *tvp = vp;
4100 vp = vp->v_mount->mnt_vnodecovered;
4111 * WARNING! *basep may not be wide enough to accomodate the
4112 * seek offset. XXX should we hack this to return the upper 32 bits
4113 * for offsets greater then 4G?
4116 *basep = (long)loff;
4118 *res = count - auio.uio_resid;
4125 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4127 * Read a block of directory entries in a file system independent format.
4132 sys_getdirentries(struct getdirentries_args *uap)
4138 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4139 &uap->sysmsg_result, UIO_USERSPACE);
4142 if (error == 0 && uap->basep)
4143 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4148 * getdents_args(int fd, char *buf, size_t count)
4153 sys_getdents(struct getdents_args *uap)
4158 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4159 &uap->sysmsg_result, UIO_USERSPACE);
4166 * Set the mode mask for creation of filesystem nodes.
4168 * umask(int newmask)
4173 sys_umask(struct umask_args *uap)
4175 struct thread *td = curthread;
4176 struct proc *p = td->td_proc;
4177 struct filedesc *fdp;
4180 uap->sysmsg_result = fdp->fd_cmask;
4181 fdp->fd_cmask = uap->newmask & ALLPERMS;
4186 * revoke(char *path)
4188 * Void all references to file by ripping underlying filesystem
4194 sys_revoke(struct revoke_args *uap)
4196 struct nlookupdata nd;
4204 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4206 error = nlookup(&nd);
4208 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4209 cred = crhold(nd.nl_cred);
4213 error = VOP_GETATTR(vp, &vattr);
4214 if (error == 0 && cred->cr_uid != vattr.va_uid)
4215 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4216 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4218 error = vrevoke(vp, cred);
4219 } else if (error == 0) {
4220 error = vrevoke(vp, cred);
4231 * getfh_args(char *fname, fhandle_t *fhp)
4233 * Get (NFS) file handle
4235 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4236 * mount. This allows nullfs mounts to be explicitly exported.
4238 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4240 * nullfs mounts of subdirectories are not safe. That is, it will
4241 * work, but you do not really have protection against access to
4242 * the related parent directories.
4247 sys_getfh(struct getfh_args *uap)
4249 struct thread *td = curthread;
4250 struct nlookupdata nd;
4257 * Must be super user
4259 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4264 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4266 error = nlookup(&nd);
4268 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4269 mp = nd.nl_nch.mount;
4272 bzero(&fh, sizeof(fh));
4273 fh.fh_fsid = mp->mnt_stat.f_fsid;
4274 error = VFS_VPTOFH(vp, &fh.fh_fid);
4277 error = copyout(&fh, uap->fhp, sizeof(fh));
4284 * fhopen_args(const struct fhandle *u_fhp, int flags)
4286 * syscall for the rpc.lockd to use to translate a NFS file handle into
4287 * an open descriptor.
4289 * warning: do not remove the priv_check() call or this becomes one giant
4295 sys_fhopen(struct fhopen_args *uap)
4297 struct thread *td = curthread;
4298 struct filedesc *fdp = td->td_proc->p_fd;
4303 struct vattr *vap = &vat;
4305 int fmode, mode, error, type;
4311 * Must be super user
4313 error = priv_check(td, PRIV_ROOT);
4317 fmode = FFLAGS(uap->flags);
4320 * Why not allow a non-read/write open for our lockd?
4322 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4324 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4329 * Find the mount point
4332 mp = vfs_getvfs(&fhp.fh_fsid);
4337 /* now give me my vnode, it gets returned to me locked */
4338 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4342 * from now on we have to make sure not
4343 * to forget about the vnode
4344 * any error that causes an abort must vput(vp)
4345 * just set error = err and 'goto bad;'.
4351 if (vp->v_type == VLNK) {
4355 if (vp->v_type == VSOCK) {
4360 if (fmode & (FWRITE | O_TRUNC)) {
4361 if (vp->v_type == VDIR) {
4365 error = vn_writechk(vp, NULL);
4373 error = VOP_ACCESS(vp, mode, td->td_ucred);
4377 if (fmode & O_TRUNC) {
4378 vn_unlock(vp); /* XXX */
4379 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4382 error = VOP_SETATTR(vp, vap, td->td_ucred);
4388 * VOP_OPEN needs the file pointer so it can potentially override
4391 * WARNING! no f_nchandle will be associated when fhopen()ing a
4394 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4398 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4401 * setting f_ops this way prevents VOP_CLOSE from being
4402 * called or fdrop() releasing the vp from v_data. Since
4403 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4405 fp->f_ops = &badfileops;
4411 * The fp is given its own reference, we still have our ref and lock.
4413 * Assert that all regular files must be created with a VM object.
4415 if (vp->v_type == VREG && vp->v_object == NULL) {
4416 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4421 * The open was successful. Handle any locking requirements.
4423 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4424 lf.l_whence = SEEK_SET;
4427 if (fmode & O_EXLOCK)
4428 lf.l_type = F_WRLCK;
4430 lf.l_type = F_RDLCK;
4431 if (fmode & FNONBLOCK)
4436 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4438 * release our private reference.
4440 fsetfd(fdp, NULL, indx);
4445 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4446 fp->f_flag |= FHASLOCK;
4450 * Clean up. Associate the file pointer with the previously
4451 * reserved descriptor and return it.
4455 fsetfd(fdp, fp, indx);
4457 uap->sysmsg_result = indx;
4461 fsetfd(fdp, NULL, indx);
4471 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4476 sys_fhstat(struct fhstat_args *uap)
4478 struct thread *td = curthread;
4486 * Must be super user
4488 error = priv_check(td, PRIV_ROOT);
4492 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4497 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4500 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4501 error = vn_stat(vp, &sb, td->td_ucred);
4507 error = copyout(&sb, uap->sb, sizeof(sb));
4512 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4517 sys_fhstatfs(struct fhstatfs_args *uap)
4519 struct thread *td = curthread;
4520 struct proc *p = td->td_proc;
4525 char *fullpath, *freepath;
4530 * Must be super user
4532 if ((error = priv_check(td, PRIV_ROOT)))
4535 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4540 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4544 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4549 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4554 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4557 error = mount_path(p, mp, &fullpath, &freepath);
4560 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4561 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4562 kfree(freepath, M_TEMP);
4564 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4565 if (priv_check(td, PRIV_ROOT)) {
4566 bcopy(sp, &sb, sizeof(sb));
4567 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4570 error = copyout(sp, uap->buf, sizeof(*sp));
4577 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4582 sys_fhstatvfs(struct fhstatvfs_args *uap)
4584 struct thread *td = curthread;
4585 struct proc *p = td->td_proc;
4593 * Must be super user
4595 if ((error = priv_check(td, PRIV_ROOT)))
4598 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4603 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4607 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4612 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4615 sp = &mp->mnt_vstat;
4617 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4621 if (mp->mnt_flag & MNT_RDONLY)
4622 sp->f_flag |= ST_RDONLY;
4623 if (mp->mnt_flag & MNT_NOSUID)
4624 sp->f_flag |= ST_NOSUID;
4625 error = copyout(sp, uap->buf, sizeof(*sp));
4633 * Syscall to push extended attribute configuration information into the
4634 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4635 * a command (int cmd), and attribute name and misc data. For now, the
4636 * attribute name is left in userspace for consumption by the VFS_op.
4637 * It will probably be changed to be copied into sysspace by the
4638 * syscall in the future, once issues with various consumers of the
4639 * attribute code have raised their hands.
4641 * Currently this is used only by UFS Extended Attributes.
4646 sys_extattrctl(struct extattrctl_args *uap)
4648 struct nlookupdata nd;
4650 char attrname[EXTATTR_MAXNAMELEN];
4660 if (error == 0 && uap->filename) {
4661 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4664 error = nlookup(&nd);
4666 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4670 if (error == 0 && uap->attrname) {
4671 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4676 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4678 error = nlookup(&nd);
4680 error = ncp_writechk(&nd.nl_nch);
4682 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4684 uap->attrname, nd.nl_cred);
4695 * Syscall to get a named extended attribute on a file or directory.
4700 sys_extattr_set_file(struct extattr_set_file_args *uap)
4702 char attrname[EXTATTR_MAXNAMELEN];
4703 struct nlookupdata nd;
4709 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4716 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4718 error = nlookup(&nd);
4720 error = ncp_writechk(&nd.nl_nch);
4722 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4729 bzero(&auio, sizeof(auio));
4730 aiov.iov_base = uap->data;
4731 aiov.iov_len = uap->nbytes;
4732 auio.uio_iov = &aiov;
4733 auio.uio_iovcnt = 1;
4734 auio.uio_offset = 0;
4735 auio.uio_resid = uap->nbytes;
4736 auio.uio_rw = UIO_WRITE;
4737 auio.uio_td = curthread;
4739 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4749 * Syscall to get a named extended attribute on a file or directory.
4754 sys_extattr_get_file(struct extattr_get_file_args *uap)
4756 char attrname[EXTATTR_MAXNAMELEN];
4757 struct nlookupdata nd;
4763 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4770 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4772 error = nlookup(&nd);
4774 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4781 bzero(&auio, sizeof(auio));
4782 aiov.iov_base = uap->data;
4783 aiov.iov_len = uap->nbytes;
4784 auio.uio_iov = &aiov;
4785 auio.uio_iovcnt = 1;
4786 auio.uio_offset = 0;
4787 auio.uio_resid = uap->nbytes;
4788 auio.uio_rw = UIO_READ;
4789 auio.uio_td = curthread;
4791 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4793 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4802 * Syscall to delete a named extended attribute from a file or directory.
4803 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4808 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4810 char attrname[EXTATTR_MAXNAMELEN];
4811 struct nlookupdata nd;
4815 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4820 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4822 error = nlookup(&nd);
4824 error = ncp_writechk(&nd.nl_nch);
4826 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4828 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4829 attrname, NULL, nd.nl_cred);
4839 * Determine if the mount is visible to the process.
4842 chroot_visible_mnt(struct mount *mp, struct proc *p)
4844 struct nchandle nch;
4847 * Traverse from the mount point upwards. If we hit the process
4848 * root then the mount point is visible to the process.
4850 nch = mp->mnt_ncmountpt;
4852 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4853 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4856 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4857 nch = nch.mount->mnt_ncmounton;
4859 nch.ncp = nch.ncp->nc_parent;
4864 * If the mount point is not visible to the process, but the
4865 * process root is in a subdirectory of the mount, return
4868 if (p->p_fd->fd_nrdir.mount == mp)