2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
46 #include <sys/sysent.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/mountctl.h>
50 #include <sys/sysproto.h>
51 #include <sys/filedesc.h>
52 #include <sys/kernel.h>
53 #include <sys/fcntl.h>
55 #include <sys/linker.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
62 #include <sys/namei.h>
63 #include <sys/nlookup.h>
64 #include <sys/dirent.h>
65 #include <sys/extattr.h>
66 #include <sys/spinlock.h>
67 #include <sys/kern_syscall.h>
68 #include <sys/objcache.h>
69 #include <sys/sysctl.h>
72 #include <sys/file2.h>
73 #include <sys/spinlock2.h>
74 #include <sys/mplock2.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
80 #include <machine/limits.h>
81 #include <machine/stdarg.h>
83 #include <vfs/union/union.h>
85 static void mount_warning(struct mount *mp, const char *ctl, ...)
87 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
88 static int checkvp_chdir (struct vnode *vn, struct thread *td);
89 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
90 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
91 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
92 static int getutimes (const struct timeval *, struct timespec *);
93 static int setfown (struct vnode *, uid_t, gid_t);
94 static int setfmode (struct vnode *, int);
95 static int setfflags (struct vnode *, int);
96 static int setutimes (struct vnode *, struct vattr *,
97 const struct timespec *, int);
98 static int usermount = 0; /* if 1, non-root can mount fs. */
100 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
102 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
103 "Allow non-root users to mount filesystems");
106 * Virtual File System System Calls
110 * Mount a file system.
112 * mount_args(char *type, char *path, int flags, caddr_t data)
117 sys_mount(struct mount_args *uap)
119 struct thread *td = curthread;
122 struct mount *mp, *nullmp;
123 struct vfsconf *vfsp;
124 int error, flag = 0, flag2 = 0;
127 struct nlookupdata nd;
128 char fstypename[MFSNAMELEN];
137 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
141 * Do not allow NFS export by non-root users.
143 if (uap->flags & MNT_EXPORTED) {
144 error = priv_check(td, PRIV_ROOT);
149 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
151 if (priv_check(td, PRIV_ROOT))
152 uap->flags |= MNT_NOSUID | MNT_NODEV;
155 * Lookup the requested path and extract the nch and vnode.
157 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
159 if ((error = nlookup(&nd)) == 0) {
160 if (nd.nl_nch.ncp->nc_vp == NULL)
170 * If the target filesystem is resolved via a nullfs mount, then
171 * nd.nl_nch.mount will be pointing to the nullfs mount structure
172 * instead of the target file system. We need it in case we are
175 nullmp = nd.nl_nch.mount;
178 * Extract the locked+refd ncp and cleanup the nd structure
181 cache_zero(&nd.nl_nch);
184 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
191 * now we have the locked ref'd nch and unreferenced vnode.
194 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
201 * Extract the file system type. We need to know this early, to take
202 * appropriate actions if we are dealing with a nullfs.
204 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
211 * Now we have an unlocked ref'd nch and a locked ref'd vp
213 if (uap->flags & MNT_UPDATE) {
214 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
221 if (strncmp(fstypename, "null", 5) == 0) {
229 flag2 = mp->mnt_kern_flag;
231 * We only allow the filesystem to be reloaded if it
232 * is currently mounted read-only.
234 if ((uap->flags & MNT_RELOAD) &&
235 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
238 error = EOPNOTSUPP; /* Needs translation */
242 * Only root, or the user that did the original mount is
243 * permitted to update it.
245 if (mp->mnt_stat.f_owner != cred->cr_uid &&
246 (error = priv_check(td, PRIV_ROOT))) {
251 if (vfs_busy(mp, LK_NOWAIT)) {
257 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
264 vsetflags(vp, VMOUNT);
266 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
271 * If the user is not root, ensure that they own the directory
272 * onto which we are attempting to mount.
274 if ((error = VOP_GETATTR(vp, &va)) ||
275 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
280 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
285 if (vp->v_type != VDIR) {
291 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
297 vfsp = vfsconf_find_by_name(fstypename);
301 /* Only load modules for root (very important!) */
302 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
307 error = linker_load_file(fstypename, &lf);
308 if (error || lf == NULL) {
316 /* lookup again, see if the VFS was loaded */
317 vfsp = vfsconf_find_by_name(fstypename);
320 linker_file_unload(lf);
327 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
333 vsetflags(vp, VMOUNT);
336 * Allocate and initialize the filesystem.
338 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
340 vfs_busy(mp, LK_NOWAIT);
341 mp->mnt_op = vfsp->vfc_vfsops;
343 vfsp->vfc_refcount++;
344 mp->mnt_stat.f_type = vfsp->vfc_typenum;
345 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
346 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
347 mp->mnt_stat.f_owner = cred->cr_uid;
351 * Set the mount level flags.
353 if (uap->flags & MNT_RDONLY)
354 mp->mnt_flag |= MNT_RDONLY;
355 else if (mp->mnt_flag & MNT_RDONLY)
356 mp->mnt_kern_flag |= MNTK_WANTRDWR;
357 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
358 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
359 MNT_NOSYMFOLLOW | MNT_IGNORE |
360 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
361 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
362 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
363 MNT_NOSYMFOLLOW | MNT_IGNORE |
364 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
366 * Mount the filesystem.
367 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
370 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
371 if (mp->mnt_flag & MNT_UPDATE) {
372 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
373 mp->mnt_flag &= ~MNT_RDONLY;
374 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
375 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
378 mp->mnt_kern_flag = flag2;
381 vclrflags(vp, VMOUNT);
386 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
388 * Put the new filesystem on the mount list after root. The mount
389 * point gets its own mnt_ncmountpt (unless the VFS already set one
390 * up) which represents the root of the mount. The lookup code
391 * detects the mount point going forward and checks the root of
392 * the mount going backwards.
394 * It is not necessary to invalidate or purge the vnode underneath
395 * because elements under the mount will be given their own glue
399 if (mp->mnt_ncmountpt.ncp == NULL) {
401 * allocate, then unlock, but leave the ref intact
403 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
404 cache_unlock(&mp->mnt_ncmountpt);
406 mp->mnt_ncmounton = nch; /* inherits ref */
407 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
409 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
410 vclrflags(vp, VMOUNT);
411 mountlist_insert(mp, MNTINS_LAST);
413 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
414 error = vfs_allocate_syncvnode(mp);
416 error = VFS_START(mp, 0);
419 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
420 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
421 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
422 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
423 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
424 vclrflags(vp, VMOUNT);
425 mp->mnt_vfc->vfc_refcount--;
437 * Scan all active processes to see if any of them have a current
438 * or root directory onto which the new filesystem has just been
439 * mounted. If so, replace them with the new mount point.
441 * The passed ncp is ref'd and locked (from the mount code) and
442 * must be associated with the vnode representing the root of the
445 struct checkdirs_info {
446 struct nchandle old_nch;
447 struct nchandle new_nch;
448 struct vnode *old_vp;
449 struct vnode *new_vp;
452 static int checkdirs_callback(struct proc *p, void *data);
455 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
457 struct checkdirs_info info;
463 * If the old mount point's vnode has a usecount of 1, it is not
464 * being held as a descriptor anywhere.
466 olddp = old_nch->ncp->nc_vp;
467 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
471 * Force the root vnode of the new mount point to be resolved
472 * so we can update any matching processes.
475 if (VFS_ROOT(mp, &newdp))
476 panic("mount: lost mount");
477 cache_setunresolved(new_nch);
478 cache_setvp(new_nch, newdp);
481 * Special handling of the root node
483 if (rootvnode == olddp) {
485 vfs_cache_setroot(newdp, cache_hold(new_nch));
489 * Pass newdp separately so the callback does not have to access
490 * it via new_nch->ncp->nc_vp.
492 info.old_nch = *old_nch;
493 info.new_nch = *new_nch;
495 allproc_scan(checkdirs_callback, &info);
500 * NOTE: callback is not MP safe because the scanned process's filedesc
501 * structure can be ripped out from under us, amoung other things.
504 checkdirs_callback(struct proc *p, void *data)
506 struct checkdirs_info *info = data;
507 struct filedesc *fdp;
508 struct nchandle ncdrop1;
509 struct nchandle ncdrop2;
510 struct vnode *vprele1;
511 struct vnode *vprele2;
513 if ((fdp = p->p_fd) != NULL) {
514 cache_zero(&ncdrop1);
515 cache_zero(&ncdrop2);
520 * MPUNSAFE - XXX fdp can be pulled out from under a
523 * A shared filedesc is ok, we don't have to copy it
524 * because we are making this change globally.
526 spin_lock(&fdp->fd_spin);
527 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
528 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
529 vprele1 = fdp->fd_cdir;
531 fdp->fd_cdir = info->new_vp;
532 ncdrop1 = fdp->fd_ncdir;
533 cache_copy(&info->new_nch, &fdp->fd_ncdir);
535 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
536 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
537 vprele2 = fdp->fd_rdir;
539 fdp->fd_rdir = info->new_vp;
540 ncdrop2 = fdp->fd_nrdir;
541 cache_copy(&info->new_nch, &fdp->fd_nrdir);
543 spin_unlock(&fdp->fd_spin);
545 cache_drop(&ncdrop1);
547 cache_drop(&ncdrop2);
557 * Unmount a file system.
559 * Note: unmount takes a path to the vnode mounted on as argument,
560 * not special file (as before).
562 * umount_args(char *path, int flags)
567 sys_unmount(struct unmount_args *uap)
569 struct thread *td = curthread;
570 struct proc *p __debugvar = td->td_proc;
571 struct mount *mp = NULL;
572 struct nlookupdata nd;
577 if (td->td_ucred->cr_prison != NULL) {
581 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
584 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
586 error = nlookup(&nd);
590 mp = nd.nl_nch.mount;
593 * Only root, or the user that did the original mount is
594 * permitted to unmount this filesystem.
596 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
597 (error = priv_check(td, PRIV_ROOT)))
601 * Don't allow unmounting the root file system.
603 if (mp->mnt_flag & MNT_ROOTFS) {
609 * Must be the root of the filesystem
611 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
619 error = dounmount(mp, uap->flags);
626 * Do the actual file system unmount.
629 dounmount_interlock(struct mount *mp)
631 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
633 mp->mnt_kern_flag |= MNTK_UNMOUNT;
638 unmount_allproc_cb(struct proc *p, void *arg)
642 if (p->p_textnch.ncp == NULL)
645 mp = (struct mount *)arg;
646 if (p->p_textnch.mount == mp)
647 cache_drop(&p->p_textnch);
653 dounmount(struct mount *mp, int flags)
655 struct namecache *ncp;
664 * Exclusive access for unmounting purposes
666 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
670 * Allow filesystems to detect that a forced unmount is in progress.
672 if (flags & MNT_FORCE)
673 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
674 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
675 error = lockmgr(&mp->mnt_lock, lflags);
677 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
678 if (mp->mnt_kern_flag & MNTK_MWAIT)
683 if (mp->mnt_flag & MNT_EXPUBLIC)
684 vfs_setpublicfs(NULL, NULL, NULL);
686 vfs_msync(mp, MNT_WAIT);
687 async_flag = mp->mnt_flag & MNT_ASYNC;
688 mp->mnt_flag &=~ MNT_ASYNC;
691 * If this filesystem isn't aliasing other filesystems,
692 * try to invalidate any remaining namecache entries and
693 * check the count afterwords.
695 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
696 cache_lock(&mp->mnt_ncmountpt);
697 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
698 cache_unlock(&mp->mnt_ncmountpt);
700 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
701 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
702 allproc_scan(&unmount_allproc_cb, mp);
705 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
706 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
708 if ((flags & MNT_FORCE) == 0) {
710 mount_warning(mp, "Cannot unmount: "
716 mount_warning(mp, "Forced unmount: "
727 * nchandle records ref the mount structure. Expect a count of 1
728 * (our mount->mnt_ncmountpt).
730 if (mp->mnt_refs != 1) {
731 if ((flags & MNT_FORCE) == 0) {
732 mount_warning(mp, "Cannot unmount: "
733 "%d process references still "
734 "present", mp->mnt_refs);
737 mount_warning(mp, "Forced unmount: "
738 "%d process references still "
739 "present", mp->mnt_refs);
745 * Decomission our special mnt_syncer vnode. This also stops
746 * the vnlru code. If we are unable to unmount we recommission
750 if ((vp = mp->mnt_syncer) != NULL) {
751 mp->mnt_syncer = NULL;
754 if (((mp->mnt_flag & MNT_RDONLY) ||
755 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
756 (flags & MNT_FORCE)) {
757 error = VFS_UNMOUNT(mp, flags);
761 if (mp->mnt_syncer == NULL)
762 vfs_allocate_syncvnode(mp);
763 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
764 mp->mnt_flag |= async_flag;
765 lockmgr(&mp->mnt_lock, LK_RELEASE);
766 if (mp->mnt_kern_flag & MNTK_MWAIT)
771 * Clean up any journals still associated with the mount after
772 * filesystem activity has ceased.
774 journal_remove_all_journals(mp,
775 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
777 mountlist_remove(mp);
780 * Remove any installed vnode ops here so the individual VFSs don't
783 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
784 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
785 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
786 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
787 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
789 if (mp->mnt_ncmountpt.ncp != NULL) {
790 nch = mp->mnt_ncmountpt;
791 cache_zero(&mp->mnt_ncmountpt);
792 cache_clrmountpt(&nch);
795 if (mp->mnt_ncmounton.ncp != NULL) {
796 nch = mp->mnt_ncmounton;
797 cache_zero(&mp->mnt_ncmounton);
798 cache_clrmountpt(&nch);
802 mp->mnt_vfc->vfc_refcount--;
803 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
804 panic("unmount: dangling vnode");
805 lockmgr(&mp->mnt_lock, LK_RELEASE);
806 if (mp->mnt_kern_flag & MNTK_MWAIT)
815 mount_warning(struct mount *mp, const char *ctl, ...)
822 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf, 0) == 0) {
823 kprintf("unmount(%s): ", ptr);
828 kprintf("unmount(%p", mp);
829 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
830 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
839 * Shim cache_fullpath() to handle the case where a process is chrooted into
840 * a subdirectory of a mount. In this case if the root mount matches the
841 * process root directory's mount we have to specify the process's root
842 * directory instead of the mount point, because the mount point might
843 * be above the root directory.
847 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
849 struct nchandle *nch;
851 if (p && p->p_fd->fd_nrdir.mount == mp)
852 nch = &p->p_fd->fd_nrdir;
854 nch = &mp->mnt_ncmountpt;
855 return(cache_fullpath(p, nch, rb, fb, 0));
859 * Sync each mounted filesystem.
863 static int syncprt = 0;
864 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
867 static int sync_callback(struct mount *mp, void *data);
873 sys_sync(struct sync_args *uap)
876 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
879 * print out buffer pool stat information on each sync() call.
890 sync_callback(struct mount *mp, void *data __unused)
894 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
895 asyncflag = mp->mnt_flag & MNT_ASYNC;
896 mp->mnt_flag &= ~MNT_ASYNC;
897 vfs_msync(mp, MNT_NOWAIT);
898 VFS_SYNC(mp, MNT_NOWAIT | MNT_LAZY);
899 mp->mnt_flag |= asyncflag;
904 /* XXX PRISON: could be per prison flag */
905 static int prison_quotas;
907 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
911 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
913 * Change filesystem quotas.
918 sys_quotactl(struct quotactl_args *uap)
920 struct nlookupdata nd;
929 if (td->td_ucred->cr_prison && !prison_quotas) {
934 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
936 error = nlookup(&nd);
938 mp = nd.nl_nch.mount;
939 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
940 uap->arg, nd.nl_cred);
949 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
950 * void *buf, int buflen)
952 * This function operates on a mount point and executes the specified
953 * operation using the specified control data, and possibly returns data.
955 * The actual number of bytes stored in the result buffer is returned, 0
956 * if none, otherwise an error is returned.
961 sys_mountctl(struct mountctl_args *uap)
963 struct thread *td = curthread;
964 struct proc *p = td->td_proc;
972 * Sanity and permissions checks. We must be root.
975 if (td->td_ucred->cr_prison != NULL)
977 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
978 (error = priv_check(td, PRIV_ROOT)) != 0)
982 * Argument length checks
984 if (uap->ctllen < 0 || uap->ctllen > 1024)
986 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
988 if (uap->path == NULL)
992 * Allocate the necessary buffers and copyin data
994 path = objcache_get(namei_oc, M_WAITOK);
995 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1000 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1001 error = copyin(uap->ctl, ctl, uap->ctllen);
1006 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1009 * Validate the descriptor
1012 fp = holdfp(p->p_fd, uap->fd, -1);
1022 * Execute the internal kernel function and clean up.
1025 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1029 if (error == 0 && uap->sysmsg_result > 0)
1030 error = copyout(buf, uap->buf, uap->sysmsg_result);
1033 objcache_put(namei_oc, path);
1042 * Execute a mount control operation by resolving the path to a mount point
1043 * and calling vop_mountctl().
1045 * Use the mount point from the nch instead of the vnode so nullfs mounts
1046 * can properly spike the VOP.
1049 kern_mountctl(const char *path, int op, struct file *fp,
1050 const void *ctl, int ctllen,
1051 void *buf, int buflen, int *res)
1055 struct nlookupdata nd;
1060 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1062 error = nlookup(&nd);
1064 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1065 mp = nd.nl_nch.mount;
1072 * Must be the root of the filesystem
1074 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1078 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1085 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1087 struct thread *td = curthread;
1088 struct proc *p = td->td_proc;
1091 char *fullpath, *freepath;
1094 if ((error = nlookup(nd)) != 0)
1096 mp = nd->nl_nch.mount;
1098 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1101 error = mount_path(p, mp, &fullpath, &freepath);
1104 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1105 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1106 kfree(freepath, M_TEMP);
1108 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1109 bcopy(sp, buf, sizeof(*buf));
1110 /* Only root should have access to the fsid's. */
1111 if (priv_check(td, PRIV_ROOT))
1112 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1117 * statfs_args(char *path, struct statfs *buf)
1119 * Get filesystem statistics.
1124 sys_statfs(struct statfs_args *uap)
1126 struct nlookupdata nd;
1131 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1133 error = kern_statfs(&nd, &buf);
1136 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1145 kern_fstatfs(int fd, struct statfs *buf)
1147 struct thread *td = curthread;
1148 struct proc *p = td->td_proc;
1152 char *fullpath, *freepath;
1156 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1159 mp = ((struct vnode *)fp->f_data)->v_mount;
1164 if (fp->f_cred == NULL) {
1169 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1172 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1174 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1175 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1176 kfree(freepath, M_TEMP);
1178 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1179 bcopy(sp, buf, sizeof(*buf));
1181 /* Only root should have access to the fsid's. */
1182 if (priv_check(td, PRIV_ROOT))
1183 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1192 * fstatfs_args(int fd, struct statfs *buf)
1194 * Get filesystem statistics.
1199 sys_fstatfs(struct fstatfs_args *uap)
1204 error = kern_fstatfs(uap->fd, &buf);
1207 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1212 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1218 if ((error = nlookup(nd)) != 0)
1220 mp = nd->nl_nch.mount;
1221 sp = &mp->mnt_vstat;
1222 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1226 if (mp->mnt_flag & MNT_RDONLY)
1227 sp->f_flag |= ST_RDONLY;
1228 if (mp->mnt_flag & MNT_NOSUID)
1229 sp->f_flag |= ST_NOSUID;
1230 bcopy(sp, buf, sizeof(*buf));
1235 * statfs_args(char *path, struct statfs *buf)
1237 * Get filesystem statistics.
1242 sys_statvfs(struct statvfs_args *uap)
1244 struct nlookupdata nd;
1249 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1251 error = kern_statvfs(&nd, &buf);
1254 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1260 kern_fstatvfs(int fd, struct statvfs *buf)
1262 struct thread *td = curthread;
1263 struct proc *p = td->td_proc;
1270 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1272 mp = ((struct vnode *)fp->f_data)->v_mount;
1277 if (fp->f_cred == NULL) {
1281 sp = &mp->mnt_vstat;
1282 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1286 if (mp->mnt_flag & MNT_RDONLY)
1287 sp->f_flag |= ST_RDONLY;
1288 if (mp->mnt_flag & MNT_NOSUID)
1289 sp->f_flag |= ST_NOSUID;
1291 bcopy(sp, buf, sizeof(*buf));
1299 * fstatfs_args(int fd, struct statfs *buf)
1301 * Get filesystem statistics.
1306 sys_fstatvfs(struct fstatvfs_args *uap)
1312 error = kern_fstatvfs(uap->fd, &buf);
1316 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1321 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1323 * Get statistics on all filesystems.
1326 struct getfsstat_info {
1327 struct statfs *sfsp;
1335 static int getfsstat_callback(struct mount *, void *);
1341 sys_getfsstat(struct getfsstat_args *uap)
1343 struct thread *td = curthread;
1344 struct getfsstat_info info;
1346 bzero(&info, sizeof(info));
1348 info.maxcount = uap->bufsize / sizeof(struct statfs);
1349 info.sfsp = uap->buf;
1351 info.flags = uap->flags;
1355 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1357 if (info.sfsp && info.count > info.maxcount)
1358 uap->sysmsg_result = info.maxcount;
1360 uap->sysmsg_result = info.count;
1361 return (info.error);
1365 getfsstat_callback(struct mount *mp, void *data)
1367 struct getfsstat_info *info = data;
1373 if (info->sfsp && info->count < info->maxcount) {
1374 if (info->td->td_proc &&
1375 !chroot_visible_mnt(mp, info->td->td_proc)) {
1381 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1382 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1383 * overrides MNT_WAIT.
1385 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1386 (info->flags & MNT_WAIT)) &&
1387 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1390 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1392 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1394 info->error = error;
1397 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1398 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1399 kfree(freepath, M_TEMP);
1401 error = copyout(sp, info->sfsp, sizeof(*sp));
1403 info->error = error;
1413 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1414 long bufsize, int flags)
1416 * Get statistics on all filesystems.
1419 struct getvfsstat_info {
1420 struct statfs *sfsp;
1421 struct statvfs *vsfsp;
1429 static int getvfsstat_callback(struct mount *, void *);
1435 sys_getvfsstat(struct getvfsstat_args *uap)
1437 struct thread *td = curthread;
1438 struct getvfsstat_info info;
1440 bzero(&info, sizeof(info));
1442 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1443 info.sfsp = uap->buf;
1444 info.vsfsp = uap->vbuf;
1446 info.flags = uap->flags;
1450 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1451 if (info.vsfsp && info.count > info.maxcount)
1452 uap->sysmsg_result = info.maxcount;
1454 uap->sysmsg_result = info.count;
1456 return (info.error);
1460 getvfsstat_callback(struct mount *mp, void *data)
1462 struct getvfsstat_info *info = data;
1464 struct statvfs *vsp;
1469 if (info->vsfsp && info->count < info->maxcount) {
1470 if (info->td->td_proc &&
1471 !chroot_visible_mnt(mp, info->td->td_proc)) {
1475 vsp = &mp->mnt_vstat;
1478 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1479 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1480 * overrides MNT_WAIT.
1482 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1483 (info->flags & MNT_WAIT)) &&
1484 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1487 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1489 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1490 (info->flags & MNT_WAIT)) &&
1491 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1495 if (mp->mnt_flag & MNT_RDONLY)
1496 vsp->f_flag |= ST_RDONLY;
1497 if (mp->mnt_flag & MNT_NOSUID)
1498 vsp->f_flag |= ST_NOSUID;
1500 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1502 info->error = error;
1505 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1506 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1507 kfree(freepath, M_TEMP);
1509 error = copyout(sp, info->sfsp, sizeof(*sp));
1511 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1513 info->error = error;
1525 * fchdir_args(int fd)
1527 * Change current working directory to a given file descriptor.
1532 sys_fchdir(struct fchdir_args *uap)
1534 struct thread *td = curthread;
1535 struct proc *p = td->td_proc;
1536 struct filedesc *fdp = p->p_fd;
1537 struct vnode *vp, *ovp;
1540 struct nchandle nch, onch, tnch;
1543 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1546 vp = (struct vnode *)fp->f_data;
1548 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1549 if (fp->f_nchandle.ncp == NULL)
1552 error = checkvp_chdir(vp, td);
1557 cache_copy(&fp->f_nchandle, &nch);
1560 * If the ncp has become a mount point, traverse through
1564 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1565 (mp = cache_findmount(&nch)) != NULL
1567 error = nlookup_mp(mp, &tnch);
1569 cache_unlock(&tnch); /* leave ref intact */
1571 vp = tnch.ncp->nc_vp;
1572 error = vget(vp, LK_SHARED);
1573 KKASSERT(error == 0);
1580 onch = fdp->fd_ncdir;
1581 vn_unlock(vp); /* leave ref intact */
1583 fdp->fd_ncdir = nch;
1597 kern_chdir(struct nlookupdata *nd)
1599 struct thread *td = curthread;
1600 struct proc *p = td->td_proc;
1601 struct filedesc *fdp = p->p_fd;
1602 struct vnode *vp, *ovp;
1603 struct nchandle onch;
1606 if ((error = nlookup(nd)) != 0)
1608 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1610 if ((error = vget(vp, LK_SHARED)) != 0)
1613 error = checkvp_chdir(vp, td);
1617 onch = fdp->fd_ncdir;
1618 cache_unlock(&nd->nl_nch); /* leave reference intact */
1619 fdp->fd_ncdir = nd->nl_nch;
1623 cache_zero(&nd->nl_nch);
1631 * chdir_args(char *path)
1633 * Change current working directory (``.'').
1638 sys_chdir(struct chdir_args *uap)
1640 struct nlookupdata nd;
1644 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1646 error = kern_chdir(&nd);
1653 * Helper function for raised chroot(2) security function: Refuse if
1654 * any filedescriptors are open directories.
1657 chroot_refuse_vdir_fds(struct filedesc *fdp)
1664 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1665 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1667 vp = (struct vnode *)fp->f_data;
1668 if (vp->v_type != VDIR) {
1679 * This sysctl determines if we will allow a process to chroot(2) if it
1680 * has a directory open:
1681 * 0: disallowed for all processes.
1682 * 1: allowed for processes that were not already chroot(2)'ed.
1683 * 2: allowed for all processes.
1686 static int chroot_allow_open_directories = 1;
1688 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1689 &chroot_allow_open_directories, 0, "");
1692 * chroot to the specified namecache entry. We obtain the vp from the
1693 * namecache data. The passed ncp must be locked and referenced and will
1694 * remain locked and referenced on return.
1697 kern_chroot(struct nchandle *nch)
1699 struct thread *td = curthread;
1700 struct proc *p = td->td_proc;
1701 struct filedesc *fdp = p->p_fd;
1706 * Only privileged user can chroot
1708 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1713 * Disallow open directory descriptors (fchdir() breakouts).
1715 if (chroot_allow_open_directories == 0 ||
1716 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1717 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1720 if ((vp = nch->ncp->nc_vp) == NULL)
1723 if ((error = vget(vp, LK_SHARED)) != 0)
1727 * Check the validity of vp as a directory to change to and
1728 * associate it with rdir/jdir.
1730 error = checkvp_chdir(vp, td);
1731 vn_unlock(vp); /* leave reference intact */
1733 vrele(fdp->fd_rdir);
1734 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1735 cache_drop(&fdp->fd_nrdir);
1736 cache_copy(nch, &fdp->fd_nrdir);
1737 if (fdp->fd_jdir == NULL) {
1740 cache_copy(nch, &fdp->fd_njdir);
1749 * chroot_args(char *path)
1751 * Change notion of root (``/'') directory.
1756 sys_chroot(struct chroot_args *uap)
1758 struct thread *td __debugvar = curthread;
1759 struct nlookupdata nd;
1762 KKASSERT(td->td_proc);
1764 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1766 nd.nl_flags |= NLC_EXEC;
1767 error = nlookup(&nd);
1769 error = kern_chroot(&nd.nl_nch);
1777 sys_chroot_kernel(struct chroot_kernel_args *uap)
1779 struct thread *td = curthread;
1780 struct nlookupdata nd;
1781 struct nchandle *nch;
1786 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1790 error = nlookup(&nd);
1796 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1800 if ((vp = nch->ncp->nc_vp) == NULL) {
1805 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1808 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1809 vfs_cache_setroot(vp, cache_hold(nch));
1819 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1820 * determine whether it is legal to chdir to the vnode. The vnode's state
1821 * is not changed by this call.
1824 checkvp_chdir(struct vnode *vp, struct thread *td)
1828 if (vp->v_type != VDIR)
1831 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1839 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1841 struct thread *td = curthread;
1842 struct proc *p = td->td_proc;
1843 struct lwp *lp = td->td_lwp;
1844 struct filedesc *fdp = p->p_fd;
1849 int type, indx, error;
1852 if ((oflags & O_ACCMODE) == O_ACCMODE)
1854 flags = FFLAGS(oflags);
1855 error = falloc(lp, &nfp, NULL);
1859 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1862 * XXX p_dupfd is a real mess. It allows a device to return a
1863 * file descriptor to be duplicated rather then doing the open
1869 * Call vn_open() to do the lookup and assign the vnode to the
1870 * file pointer. vn_open() does not change the ref count on fp
1871 * and the vnode, on success, will be inherited by the file pointer
1874 nd->nl_flags |= NLC_LOCKVP;
1875 error = vn_open(nd, fp, flags, cmode);
1879 * handle special fdopen() case. bleh. dupfdopen() is
1880 * responsible for dropping the old contents of ofiles[indx]
1883 * Note that fsetfd() will add a ref to fp which represents
1884 * the fd_files[] assignment. We must still drop our
1887 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1888 if (fdalloc(p, 0, &indx) == 0) {
1889 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1892 fdrop(fp); /* our ref */
1895 fsetfd(fdp, NULL, indx);
1898 fdrop(fp); /* our ref */
1899 if (error == ERESTART)
1905 * ref the vnode for ourselves so it can't be ripped out from under
1906 * is. XXX need an ND flag to request that the vnode be returned
1909 * Reserve a file descriptor but do not assign it until the open
1912 vp = (struct vnode *)fp->f_data;
1914 if ((error = fdalloc(p, 0, &indx)) != 0) {
1921 * If no error occurs the vp will have been assigned to the file
1926 if (flags & (O_EXLOCK | O_SHLOCK)) {
1927 lf.l_whence = SEEK_SET;
1930 if (flags & O_EXLOCK)
1931 lf.l_type = F_WRLCK;
1933 lf.l_type = F_RDLCK;
1934 if (flags & FNONBLOCK)
1939 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1941 * lock request failed. Clean up the reserved
1945 fsetfd(fdp, NULL, indx);
1949 fp->f_flag |= FHASLOCK;
1953 * Assert that all regular file vnodes were created with a object.
1955 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1956 ("open: regular file has no backing object after vn_open"));
1962 * release our private reference, leaving the one associated with the
1963 * descriptor table intact.
1965 fsetfd(fdp, fp, indx);
1972 * open_args(char *path, int flags, int mode)
1974 * Check permissions, allocate an open file structure,
1975 * and call the device open routine if any.
1980 sys_open(struct open_args *uap)
1982 struct nlookupdata nd;
1985 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1987 error = kern_open(&nd, uap->flags,
1988 uap->mode, &uap->sysmsg_result);
1995 * openat_args(int fd, char *path, int flags, int mode)
2000 sys_openat(struct openat_args *uap)
2002 struct nlookupdata nd;
2006 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2008 error = kern_open(&nd, uap->flags, uap->mode,
2009 &uap->sysmsg_result);
2011 nlookup_done_at(&nd, fp);
2016 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2018 struct thread *td = curthread;
2019 struct proc *p = td->td_proc;
2028 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2029 vattr.va_rmajor = rmajor;
2030 vattr.va_rminor = rminor;
2032 switch (mode & S_IFMT) {
2033 case S_IFMT: /* used by badsect to flag bad sectors */
2034 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2035 vattr.va_type = VBAD;
2038 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2039 vattr.va_type = VCHR;
2042 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2043 vattr.va_type = VBLK;
2046 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2049 case S_IFDIR: /* special directories support for HAMMER */
2050 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2051 vattr.va_type = VDIR;
2062 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2063 if ((error = nlookup(nd)) != 0)
2065 if (nd->nl_nch.ncp->nc_vp)
2067 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2071 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2072 nd->nl_cred, NAMEI_CREATE);
2075 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2076 &vp, nd->nl_cred, &vattr);
2084 * mknod_args(char *path, int mode, int dev)
2086 * Create a special file.
2091 sys_mknod(struct mknod_args *uap)
2093 struct nlookupdata nd;
2097 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2099 error = kern_mknod(&nd, uap->mode,
2100 umajor(uap->dev), uminor(uap->dev));
2108 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2110 * Create a special file. The path is relative to the directory associated
2116 sys_mknodat(struct mknodat_args *uap)
2118 struct nlookupdata nd;
2123 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2125 error = kern_mknod(&nd, uap->mode,
2126 umajor(uap->dev), uminor(uap->dev));
2128 nlookup_done_at(&nd, fp);
2134 kern_mkfifo(struct nlookupdata *nd, int mode)
2136 struct thread *td = curthread;
2137 struct proc *p = td->td_proc;
2144 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2145 if ((error = nlookup(nd)) != 0)
2147 if (nd->nl_nch.ncp->nc_vp)
2149 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2153 vattr.va_type = VFIFO;
2154 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2156 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2163 * mkfifo_args(char *path, int mode)
2165 * Create a named pipe.
2170 sys_mkfifo(struct mkfifo_args *uap)
2172 struct nlookupdata nd;
2176 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2178 error = kern_mkfifo(&nd, uap->mode);
2185 * mkfifoat_args(int fd, char *path, mode_t mode)
2187 * Create a named pipe. The path is relative to the directory associated
2193 sys_mkfifoat(struct mkfifoat_args *uap)
2195 struct nlookupdata nd;
2200 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2202 error = kern_mkfifo(&nd, uap->mode);
2203 nlookup_done_at(&nd, fp);
2208 static int hardlink_check_uid = 0;
2209 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2210 &hardlink_check_uid, 0,
2211 "Unprivileged processes cannot create hard links to files owned by other "
2213 static int hardlink_check_gid = 0;
2214 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2215 &hardlink_check_gid, 0,
2216 "Unprivileged processes cannot create hard links to files owned by other "
2220 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2226 * Shortcut if disabled
2228 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2232 * Privileged user can always hardlink
2234 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2238 * Otherwise only if the originating file is owned by the
2239 * same user or group. Note that any group is allowed if
2240 * the file is owned by the caller.
2242 error = VOP_GETATTR(vp, &va);
2246 if (hardlink_check_uid) {
2247 if (cred->cr_uid != va.va_uid)
2251 if (hardlink_check_gid) {
2252 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2260 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2262 struct thread *td = curthread;
2267 * Lookup the source and obtained a locked vnode.
2269 * You may only hardlink a file which you have write permission
2270 * on or which you own.
2272 * XXX relookup on vget failure / race ?
2275 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2276 if ((error = nlookup(nd)) != 0)
2278 vp = nd->nl_nch.ncp->nc_vp;
2279 KKASSERT(vp != NULL);
2280 if (vp->v_type == VDIR)
2281 return (EPERM); /* POSIX */
2282 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2284 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2288 * Unlock the source so we can lookup the target without deadlocking
2289 * (XXX vp is locked already, possible other deadlock?). The target
2292 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2293 nd->nl_flags &= ~NLC_NCPISLOCKED;
2294 cache_unlock(&nd->nl_nch);
2297 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2298 if ((error = nlookup(linknd)) != 0) {
2302 if (linknd->nl_nch.ncp->nc_vp) {
2306 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2312 * Finally run the new API VOP.
2314 error = can_hardlink(vp, td, td->td_ucred);
2316 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2317 vp, linknd->nl_cred);
2324 * link_args(char *path, char *link)
2326 * Make a hard file link.
2331 sys_link(struct link_args *uap)
2333 struct nlookupdata nd, linknd;
2337 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2339 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2341 error = kern_link(&nd, &linknd);
2342 nlookup_done(&linknd);
2350 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2358 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2359 if ((error = nlookup(nd)) != 0)
2361 if (nd->nl_nch.ncp->nc_vp)
2363 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2367 vattr.va_mode = mode;
2368 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2375 * symlink(char *path, char *link)
2377 * Make a symbolic link.
2382 sys_symlink(struct symlink_args *uap)
2384 struct thread *td = curthread;
2385 struct nlookupdata nd;
2390 path = objcache_get(namei_oc, M_WAITOK);
2391 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2394 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2396 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2397 error = kern_symlink(&nd, path, mode);
2402 objcache_put(namei_oc, path);
2407 * symlinkat_args(char *path1, int fd, char *path2)
2409 * Make a symbolic link. The path2 argument is relative to the directory
2410 * associated with fd.
2415 sys_symlinkat(struct symlinkat_args *uap)
2417 struct thread *td = curthread;
2418 struct nlookupdata nd;
2424 path1 = objcache_get(namei_oc, M_WAITOK);
2425 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2428 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2431 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2432 error = kern_symlink(&nd, path1, mode);
2434 nlookup_done_at(&nd, fp);
2437 objcache_put(namei_oc, path1);
2442 * undelete_args(char *path)
2444 * Delete a whiteout from the filesystem.
2449 sys_undelete(struct undelete_args *uap)
2451 struct nlookupdata nd;
2455 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2457 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2459 error = nlookup(&nd);
2461 error = ncp_writechk(&nd.nl_nch);
2463 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2472 kern_unlink(struct nlookupdata *nd)
2477 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2478 if ((error = nlookup(nd)) != 0)
2480 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2482 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2487 * unlink_args(char *path)
2489 * Delete a name from the filesystem.
2494 sys_unlink(struct unlink_args *uap)
2496 struct nlookupdata nd;
2500 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2502 error = kern_unlink(&nd);
2510 * unlinkat_args(int fd, char *path, int flags)
2512 * Delete the file or directory entry pointed to by fd/path.
2517 sys_unlinkat(struct unlinkat_args *uap)
2519 struct nlookupdata nd;
2523 if (uap->flags & ~AT_REMOVEDIR)
2527 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2529 if (uap->flags & AT_REMOVEDIR)
2530 error = kern_rmdir(&nd);
2532 error = kern_unlink(&nd);
2534 nlookup_done_at(&nd, fp);
2543 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2545 struct thread *td = curthread;
2546 struct proc *p = td->td_proc;
2553 fp = holdfp(p->p_fd, fd, -1);
2556 if (fp->f_type != DTYPE_VNODE) {
2560 vp = (struct vnode *)fp->f_data;
2564 spin_lock(&fp->f_spin);
2565 new_offset = fp->f_offset + offset;
2570 error = VOP_GETATTR(vp, &vattr);
2572 spin_lock(&fp->f_spin);
2573 new_offset = offset + vattr.va_size;
2576 new_offset = offset;
2578 spin_lock(&fp->f_spin);
2583 spin_lock(&fp->f_spin);
2588 * Validate the seek position. Negative offsets are not allowed
2589 * for regular files or directories.
2591 * Normally we would also not want to allow negative offsets for
2592 * character and block-special devices. However kvm addresses
2593 * on 64 bit architectures might appear to be negative and must
2597 if (new_offset < 0 &&
2598 (vp->v_type == VREG || vp->v_type == VDIR)) {
2601 fp->f_offset = new_offset;
2604 *res = fp->f_offset;
2605 spin_unlock(&fp->f_spin);
2612 * lseek_args(int fd, int pad, off_t offset, int whence)
2614 * Reposition read/write file offset.
2619 sys_lseek(struct lseek_args *uap)
2623 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2624 &uap->sysmsg_offset);
2630 * Check if current process can access given file. amode is a bitmask of *_OK
2631 * access bits. flags is a bitmask of AT_* flags.
2634 kern_access(struct nlookupdata *nd, int amode, int flags)
2639 if (flags & ~AT_EACCESS)
2641 if ((error = nlookup(nd)) != 0)
2644 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2648 /* Flags == 0 means only check for existence. */
2657 if ((mode & VWRITE) == 0 ||
2658 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2659 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2662 * If the file handle is stale we have to re-resolve the
2663 * entry. This is a hack at the moment.
2665 if (error == ESTALE) {
2667 cache_setunresolved(&nd->nl_nch);
2668 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2681 * access_args(char *path, int flags)
2683 * Check access permissions.
2688 sys_access(struct access_args *uap)
2690 struct nlookupdata nd;
2694 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2696 error = kern_access(&nd, uap->flags, 0);
2704 * faccessat_args(int fd, char *path, int amode, int flags)
2706 * Check access permissions.
2711 sys_faccessat(struct faccessat_args *uap)
2713 struct nlookupdata nd;
2718 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2721 error = kern_access(&nd, uap->amode, uap->flags);
2722 nlookup_done_at(&nd, fp);
2732 kern_stat(struct nlookupdata *nd, struct stat *st)
2738 if ((error = nlookup(nd)) != 0)
2741 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2745 if ((error = vget(vp, LK_SHARED)) != 0)
2747 error = vn_stat(vp, st, nd->nl_cred);
2750 * If the file handle is stale we have to re-resolve the entry. This
2751 * is a hack at the moment.
2753 if (error == ESTALE) {
2755 cache_setunresolved(&nd->nl_nch);
2756 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2766 * stat_args(char *path, struct stat *ub)
2768 * Get file status; this version follows links.
2773 sys_stat(struct stat_args *uap)
2775 struct nlookupdata nd;
2779 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2781 error = kern_stat(&nd, &st);
2783 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2790 * lstat_args(char *path, struct stat *ub)
2792 * Get file status; this version does not follow links.
2797 sys_lstat(struct lstat_args *uap)
2799 struct nlookupdata nd;
2803 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2805 error = kern_stat(&nd, &st);
2807 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2814 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2816 * Get status of file pointed to by fd/path.
2821 sys_fstatat(struct fstatat_args *uap)
2823 struct nlookupdata nd;
2829 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2832 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2834 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2835 UIO_USERSPACE, flags);
2837 error = kern_stat(&nd, &st);
2839 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2841 nlookup_done_at(&nd, fp);
2846 * pathconf_Args(char *path, int name)
2848 * Get configurable pathname variables.
2853 sys_pathconf(struct pathconf_args *uap)
2855 struct nlookupdata nd;
2861 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2863 error = nlookup(&nd);
2865 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2868 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2877 * kern_readlink isn't properly split yet. There is a copyin burried
2878 * in VOP_READLINK().
2881 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2883 struct thread *td = curthread;
2889 if ((error = nlookup(nd)) != 0)
2891 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2894 if (vp->v_type != VLNK) {
2897 aiov.iov_base = buf;
2898 aiov.iov_len = count;
2899 auio.uio_iov = &aiov;
2900 auio.uio_iovcnt = 1;
2901 auio.uio_offset = 0;
2902 auio.uio_rw = UIO_READ;
2903 auio.uio_segflg = UIO_USERSPACE;
2905 auio.uio_resid = count;
2906 error = VOP_READLINK(vp, &auio, td->td_ucred);
2909 *res = count - auio.uio_resid;
2914 * readlink_args(char *path, char *buf, int count)
2916 * Return target name of a symbolic link.
2921 sys_readlink(struct readlink_args *uap)
2923 struct nlookupdata nd;
2927 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2929 error = kern_readlink(&nd, uap->buf, uap->count,
2930 &uap->sysmsg_result);
2938 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2940 * Return target name of a symbolic link. The path is relative to the
2941 * directory associated with fd.
2946 sys_readlinkat(struct readlinkat_args *uap)
2948 struct nlookupdata nd;
2953 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2955 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2956 &uap->sysmsg_result);
2958 nlookup_done_at(&nd, fp);
2964 setfflags(struct vnode *vp, int flags)
2966 struct thread *td = curthread;
2971 * Prevent non-root users from setting flags on devices. When
2972 * a device is reused, users can retain ownership of the device
2973 * if they are allowed to set flags and programs assume that
2974 * chown can't fail when done as root.
2976 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2977 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2981 * note: vget is required for any operation that might mod the vnode
2982 * so VINACTIVE is properly cleared.
2984 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2986 vattr.va_flags = flags;
2987 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2994 * chflags(char *path, int flags)
2996 * Change flags of a file given a path name.
3001 sys_chflags(struct chflags_args *uap)
3003 struct nlookupdata nd;
3009 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3011 error = nlookup(&nd);
3013 error = ncp_writechk(&nd.nl_nch);
3015 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3018 error = setfflags(vp, uap->flags);
3026 * lchflags(char *path, int flags)
3028 * Change flags of a file given a path name, but don't follow symlinks.
3033 sys_lchflags(struct lchflags_args *uap)
3035 struct nlookupdata nd;
3041 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3043 error = nlookup(&nd);
3045 error = ncp_writechk(&nd.nl_nch);
3047 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3050 error = setfflags(vp, uap->flags);
3058 * fchflags_args(int fd, int flags)
3060 * Change flags of a file given a file descriptor.
3065 sys_fchflags(struct fchflags_args *uap)
3067 struct thread *td = curthread;
3068 struct proc *p = td->td_proc;
3072 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3075 if (fp->f_nchandle.ncp)
3076 error = ncp_writechk(&fp->f_nchandle);
3078 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3085 setfmode(struct vnode *vp, int mode)
3087 struct thread *td = curthread;
3092 * note: vget is required for any operation that might mod the vnode
3093 * so VINACTIVE is properly cleared.
3095 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3097 vattr.va_mode = mode & ALLPERMS;
3098 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3105 kern_chmod(struct nlookupdata *nd, int mode)
3110 if ((error = nlookup(nd)) != 0)
3112 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3114 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3115 error = setfmode(vp, mode);
3121 * chmod_args(char *path, int mode)
3123 * Change mode of a file given path name.
3128 sys_chmod(struct chmod_args *uap)
3130 struct nlookupdata nd;
3134 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3136 error = kern_chmod(&nd, uap->mode);
3143 * lchmod_args(char *path, int mode)
3145 * Change mode of a file given path name (don't follow links.)
3150 sys_lchmod(struct lchmod_args *uap)
3152 struct nlookupdata nd;
3156 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3158 error = kern_chmod(&nd, uap->mode);
3165 * fchmod_args(int fd, int mode)
3167 * Change mode of a file given a file descriptor.
3172 sys_fchmod(struct fchmod_args *uap)
3174 struct thread *td = curthread;
3175 struct proc *p = td->td_proc;
3179 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3182 if (fp->f_nchandle.ncp)
3183 error = ncp_writechk(&fp->f_nchandle);
3185 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3192 * fchmodat_args(char *path, int mode)
3194 * Change mode of a file pointed to by fd/path.
3199 sys_fchmodat(struct fchmodat_args *uap)
3201 struct nlookupdata nd;
3206 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3208 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3211 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3212 UIO_USERSPACE, flags);
3214 error = kern_chmod(&nd, uap->mode);
3215 nlookup_done_at(&nd, fp);
3221 setfown(struct vnode *vp, uid_t uid, gid_t gid)
3223 struct thread *td = curthread;
3228 * note: vget is required for any operation that might mod the vnode
3229 * so VINACTIVE is properly cleared.
3231 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3235 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3242 kern_chown(struct nlookupdata *nd, int uid, int gid)
3247 if ((error = nlookup(nd)) != 0)
3249 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3251 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3252 error = setfown(vp, uid, gid);
3258 * chown(char *path, int uid, int gid)
3260 * Set ownership given a path name.
3265 sys_chown(struct chown_args *uap)
3267 struct nlookupdata nd;
3271 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3273 error = kern_chown(&nd, uap->uid, uap->gid);
3280 * lchown_args(char *path, int uid, int gid)
3282 * Set ownership given a path name, do not cross symlinks.
3287 sys_lchown(struct lchown_args *uap)
3289 struct nlookupdata nd;
3293 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3295 error = kern_chown(&nd, uap->uid, uap->gid);
3302 * fchown_args(int fd, int uid, int gid)
3304 * Set ownership given a file descriptor.
3309 sys_fchown(struct fchown_args *uap)
3311 struct thread *td = curthread;
3312 struct proc *p = td->td_proc;
3316 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3319 if (fp->f_nchandle.ncp)
3320 error = ncp_writechk(&fp->f_nchandle);
3322 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
3329 * fchownat(int fd, char *path, int uid, int gid, int flags)
3331 * Set ownership of file pointed to by fd/path.
3336 sys_fchownat(struct fchownat_args *uap)
3338 struct nlookupdata nd;
3343 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3345 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3348 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3349 UIO_USERSPACE, flags);
3351 error = kern_chown(&nd, uap->uid, uap->gid);
3352 nlookup_done_at(&nd, fp);
3359 getutimes(const struct timeval *tvp, struct timespec *tsp)
3361 struct timeval tv[2];
3365 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3368 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3369 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3375 setutimes(struct vnode *vp, struct vattr *vattr,
3376 const struct timespec *ts, int nullflag)
3378 struct thread *td = curthread;
3382 vattr->va_atime = ts[0];
3383 vattr->va_mtime = ts[1];
3385 vattr->va_vaflags |= VA_UTIMES_NULL;
3386 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3392 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3394 struct timespec ts[2];
3399 if ((error = getutimes(tptr, ts)) != 0)
3403 * NOTE: utimes() succeeds for the owner even if the file
3404 * is not user-writable.
3406 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3408 if ((error = nlookup(nd)) != 0)
3410 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3412 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3416 * note: vget is required for any operation that might mod the vnode
3417 * so VINACTIVE is properly cleared.
3419 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3420 error = vget(vp, LK_EXCLUSIVE);
3422 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3431 * utimes_args(char *path, struct timeval *tptr)
3433 * Set the access and modification times of a file.
3438 sys_utimes(struct utimes_args *uap)
3440 struct timeval tv[2];
3441 struct nlookupdata nd;
3445 error = copyin(uap->tptr, tv, sizeof(tv));
3450 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3452 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3459 * lutimes_args(char *path, struct timeval *tptr)
3461 * Set the access and modification times of a file.
3466 sys_lutimes(struct lutimes_args *uap)
3468 struct timeval tv[2];
3469 struct nlookupdata nd;
3473 error = copyin(uap->tptr, tv, sizeof(tv));
3478 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3480 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3487 * Set utimes on a file descriptor. The creds used to open the
3488 * file are used to determine whether the operation is allowed
3492 kern_futimes(int fd, struct timeval *tptr)
3494 struct thread *td = curthread;
3495 struct proc *p = td->td_proc;
3496 struct timespec ts[2];
3502 error = getutimes(tptr, ts);
3505 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3507 if (fp->f_nchandle.ncp)
3508 error = ncp_writechk(&fp->f_nchandle);
3511 error = vget(vp, LK_EXCLUSIVE);
3513 error = VOP_GETATTR(vp, &vattr);
3515 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3519 error = setutimes(vp, &vattr, ts,
3530 * futimes_args(int fd, struct timeval *tptr)
3532 * Set the access and modification times of a file.
3537 sys_futimes(struct futimes_args *uap)
3539 struct timeval tv[2];
3543 error = copyin(uap->tptr, tv, sizeof(tv));
3548 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3555 kern_truncate(struct nlookupdata *nd, off_t length)
3563 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3564 if ((error = nlookup(nd)) != 0)
3566 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3568 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3570 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3574 if (vp->v_type == VDIR) {
3576 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3578 vattr.va_size = length;
3579 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3586 * truncate(char *path, int pad, off_t length)
3588 * Truncate a file given its path name.
3593 sys_truncate(struct truncate_args *uap)
3595 struct nlookupdata nd;
3599 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3601 error = kern_truncate(&nd, uap->length);
3608 kern_ftruncate(int fd, off_t length)
3610 struct thread *td = curthread;
3611 struct proc *p = td->td_proc;
3619 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3621 if (fp->f_nchandle.ncp) {
3622 error = ncp_writechk(&fp->f_nchandle);
3626 if ((fp->f_flag & FWRITE) == 0) {
3630 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3634 vp = (struct vnode *)fp->f_data;
3635 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3636 if (vp->v_type == VDIR) {
3638 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3640 vattr.va_size = length;
3641 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3650 * ftruncate_args(int fd, int pad, off_t length)
3652 * Truncate a file given a file descriptor.
3657 sys_ftruncate(struct ftruncate_args *uap)
3662 error = kern_ftruncate(uap->fd, uap->length);
3671 * Sync an open file.
3676 sys_fsync(struct fsync_args *uap)
3678 struct thread *td = curthread;
3679 struct proc *p = td->td_proc;
3685 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3688 vp = (struct vnode *)fp->f_data;
3689 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3690 if ((obj = vp->v_object) != NULL)
3691 vm_object_page_clean(obj, 0, 0, 0);
3692 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3693 if (error == 0 && vp->v_mount)
3694 error = buf_fsync(vp);
3703 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3705 struct nchandle fnchd;
3706 struct nchandle tnchd;
3707 struct namecache *ncp;
3714 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3715 if ((error = nlookup(fromnd)) != 0)
3717 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3719 fnchd.mount = fromnd->nl_nch.mount;
3723 * unlock the source nch so we can lookup the target nch without
3724 * deadlocking. The target may or may not exist so we do not check
3725 * for a target vp like kern_mkdir() and other creation functions do.
3727 * The source and target directories are ref'd and rechecked after
3728 * everything is relocked to determine if the source or target file
3731 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3732 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3733 cache_unlock(&fromnd->nl_nch);
3735 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3736 if ((error = nlookup(tond)) != 0) {
3740 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3744 tnchd.mount = tond->nl_nch.mount;
3748 * If the source and target are the same there is nothing to do
3750 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3757 * Mount points cannot be renamed or overwritten
3759 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3768 * Relock the source ncp. cache_relock() will deal with any
3769 * deadlocks against the already-locked tond and will also
3770 * make sure both are resolved.
3772 * NOTE AFTER RELOCKING: The source or target ncp may have become
3773 * invalid while they were unlocked, nc_vp and nc_mount could
3776 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3777 &tond->nl_nch, tond->nl_cred);
3778 fromnd->nl_flags |= NLC_NCPISLOCKED;
3781 * make sure the parent directories linkages are the same
3783 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3784 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3791 * Both the source and target must be within the same filesystem and
3792 * in the same filesystem as their parent directories within the
3793 * namecache topology.
3795 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3798 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3799 mp != tond->nl_nch.mount) {
3806 * Make sure the mount point is writable
3808 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3815 * If the target exists and either the source or target is a directory,
3816 * then both must be directories.
3818 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3821 if (tond->nl_nch.ncp->nc_vp) {
3822 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3824 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3825 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3827 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3833 * You cannot rename a source into itself or a subdirectory of itself.
3834 * We check this by travsersing the target directory upwards looking
3835 * for a match against the source.
3840 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3841 if (fromnd->nl_nch.ncp == ncp) {
3852 * Even though the namespaces are different, they may still represent
3853 * hardlinks to the same file. The filesystem might have a hard time
3854 * with this so we issue a NREMOVE of the source instead of a NRENAME
3855 * when we detect the situation.
3858 fdvp = fromnd->nl_dvp;
3859 tdvp = tond->nl_dvp;
3860 if (fdvp == NULL || tdvp == NULL) {
3862 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3863 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3866 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3867 fdvp, tdvp, tond->nl_cred);
3874 * rename_args(char *from, char *to)
3876 * Rename files. Source and destination must either both be directories,
3877 * or both not be directories. If target is a directory, it must be empty.
3882 sys_rename(struct rename_args *uap)
3884 struct nlookupdata fromnd, tond;
3888 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3890 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3892 error = kern_rename(&fromnd, &tond);
3893 nlookup_done(&tond);
3895 nlookup_done(&fromnd);
3901 * renameat_args(int oldfd, char *old, int newfd, char *new)
3903 * Rename files using paths relative to the directories associated with
3904 * oldfd and newfd. Source and destination must either both be directories,
3905 * or both not be directories. If target is a directory, it must be empty.
3910 sys_renameat(struct renameat_args *uap)
3912 struct nlookupdata oldnd, newnd;
3913 struct file *oldfp, *newfp;
3917 error = nlookup_init_at(&oldnd, &oldfp, uap->oldfd, uap->old,
3920 error = nlookup_init_at(&newnd, &newfp, uap->newfd, uap->new,
3923 error = kern_rename(&oldnd, &newnd);
3924 nlookup_done_at(&newnd, newfp);
3926 nlookup_done_at(&oldnd, oldfp);
3932 kern_mkdir(struct nlookupdata *nd, int mode)
3934 struct thread *td = curthread;
3935 struct proc *p = td->td_proc;
3941 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3942 if ((error = nlookup(nd)) != 0)
3945 if (nd->nl_nch.ncp->nc_vp)
3947 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3950 vattr.va_type = VDIR;
3951 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3954 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3961 * mkdir_args(char *path, int mode)
3963 * Make a directory file.
3968 sys_mkdir(struct mkdir_args *uap)
3970 struct nlookupdata nd;
3974 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3976 error = kern_mkdir(&nd, uap->mode);
3983 * mkdirat_args(int fd, char *path, mode_t mode)
3985 * Make a directory file. The path is relative to the directory associated
3991 sys_mkdirat(struct mkdirat_args *uap)
3993 struct nlookupdata nd;
3998 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4000 error = kern_mkdir(&nd, uap->mode);
4001 nlookup_done_at(&nd, fp);
4007 kern_rmdir(struct nlookupdata *nd)
4012 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4013 if ((error = nlookup(nd)) != 0)
4017 * Do not allow directories representing mount points to be
4018 * deleted, even if empty. Check write perms on mount point
4019 * in case the vnode is aliased (aka nullfs).
4021 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4023 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4025 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4030 * rmdir_args(char *path)
4032 * Remove a directory file.
4037 sys_rmdir(struct rmdir_args *uap)
4039 struct nlookupdata nd;
4043 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4045 error = kern_rmdir(&nd);
4052 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4053 enum uio_seg direction)
4055 struct thread *td = curthread;
4056 struct proc *p = td->td_proc;
4064 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4066 if ((fp->f_flag & FREAD) == 0) {
4070 vp = (struct vnode *)fp->f_data;
4072 if (vp->v_type != VDIR) {
4076 aiov.iov_base = buf;
4077 aiov.iov_len = count;
4078 auio.uio_iov = &aiov;
4079 auio.uio_iovcnt = 1;
4080 auio.uio_rw = UIO_READ;
4081 auio.uio_segflg = direction;
4083 auio.uio_resid = count;
4084 loff = auio.uio_offset = fp->f_offset;
4085 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4086 fp->f_offset = auio.uio_offset;
4089 if (count == auio.uio_resid) {
4090 if (union_dircheckp) {
4091 error = union_dircheckp(td, &vp, fp);
4098 if ((vp->v_flag & VROOT) &&
4099 (vp->v_mount->mnt_flag & MNT_UNION)) {
4100 struct vnode *tvp = vp;
4101 vp = vp->v_mount->mnt_vnodecovered;
4112 * WARNING! *basep may not be wide enough to accomodate the
4113 * seek offset. XXX should we hack this to return the upper 32 bits
4114 * for offsets greater then 4G?
4117 *basep = (long)loff;
4119 *res = count - auio.uio_resid;
4126 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4128 * Read a block of directory entries in a file system independent format.
4133 sys_getdirentries(struct getdirentries_args *uap)
4139 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4140 &uap->sysmsg_result, UIO_USERSPACE);
4143 if (error == 0 && uap->basep)
4144 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4149 * getdents_args(int fd, char *buf, size_t count)
4154 sys_getdents(struct getdents_args *uap)
4159 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4160 &uap->sysmsg_result, UIO_USERSPACE);
4167 * Set the mode mask for creation of filesystem nodes.
4169 * umask(int newmask)
4174 sys_umask(struct umask_args *uap)
4176 struct thread *td = curthread;
4177 struct proc *p = td->td_proc;
4178 struct filedesc *fdp;
4181 uap->sysmsg_result = fdp->fd_cmask;
4182 fdp->fd_cmask = uap->newmask & ALLPERMS;
4187 * revoke(char *path)
4189 * Void all references to file by ripping underlying filesystem
4195 sys_revoke(struct revoke_args *uap)
4197 struct nlookupdata nd;
4205 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4207 error = nlookup(&nd);
4209 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4210 cred = crhold(nd.nl_cred);
4214 error = VOP_GETATTR(vp, &vattr);
4215 if (error == 0 && cred->cr_uid != vattr.va_uid)
4216 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4217 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4219 error = vrevoke(vp, cred);
4220 } else if (error == 0) {
4221 error = vrevoke(vp, cred);
4232 * getfh_args(char *fname, fhandle_t *fhp)
4234 * Get (NFS) file handle
4236 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4237 * mount. This allows nullfs mounts to be explicitly exported.
4239 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4241 * nullfs mounts of subdirectories are not safe. That is, it will
4242 * work, but you do not really have protection against access to
4243 * the related parent directories.
4248 sys_getfh(struct getfh_args *uap)
4250 struct thread *td = curthread;
4251 struct nlookupdata nd;
4258 * Must be super user
4260 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4265 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4267 error = nlookup(&nd);
4269 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4270 mp = nd.nl_nch.mount;
4273 bzero(&fh, sizeof(fh));
4274 fh.fh_fsid = mp->mnt_stat.f_fsid;
4275 error = VFS_VPTOFH(vp, &fh.fh_fid);
4278 error = copyout(&fh, uap->fhp, sizeof(fh));
4285 * fhopen_args(const struct fhandle *u_fhp, int flags)
4287 * syscall for the rpc.lockd to use to translate a NFS file handle into
4288 * an open descriptor.
4290 * warning: do not remove the priv_check() call or this becomes one giant
4296 sys_fhopen(struct fhopen_args *uap)
4298 struct thread *td = curthread;
4299 struct filedesc *fdp = td->td_proc->p_fd;
4304 struct vattr *vap = &vat;
4306 int fmode, mode, error, type;
4312 * Must be super user
4314 error = priv_check(td, PRIV_ROOT);
4318 fmode = FFLAGS(uap->flags);
4321 * Why not allow a non-read/write open for our lockd?
4323 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4325 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4330 * Find the mount point
4333 mp = vfs_getvfs(&fhp.fh_fsid);
4338 /* now give me my vnode, it gets returned to me locked */
4339 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4343 * from now on we have to make sure not
4344 * to forget about the vnode
4345 * any error that causes an abort must vput(vp)
4346 * just set error = err and 'goto bad;'.
4352 if (vp->v_type == VLNK) {
4356 if (vp->v_type == VSOCK) {
4361 if (fmode & (FWRITE | O_TRUNC)) {
4362 if (vp->v_type == VDIR) {
4366 error = vn_writechk(vp, NULL);
4374 error = VOP_ACCESS(vp, mode, td->td_ucred);
4378 if (fmode & O_TRUNC) {
4379 vn_unlock(vp); /* XXX */
4380 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4383 error = VOP_SETATTR(vp, vap, td->td_ucred);
4389 * VOP_OPEN needs the file pointer so it can potentially override
4392 * WARNING! no f_nchandle will be associated when fhopen()ing a
4395 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4399 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4402 * setting f_ops this way prevents VOP_CLOSE from being
4403 * called or fdrop() releasing the vp from v_data. Since
4404 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4406 fp->f_ops = &badfileops;
4412 * The fp is given its own reference, we still have our ref and lock.
4414 * Assert that all regular files must be created with a VM object.
4416 if (vp->v_type == VREG && vp->v_object == NULL) {
4417 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4422 * The open was successful. Handle any locking requirements.
4424 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4425 lf.l_whence = SEEK_SET;
4428 if (fmode & O_EXLOCK)
4429 lf.l_type = F_WRLCK;
4431 lf.l_type = F_RDLCK;
4432 if (fmode & FNONBLOCK)
4437 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4439 * release our private reference.
4441 fsetfd(fdp, NULL, indx);
4446 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4447 fp->f_flag |= FHASLOCK;
4451 * Clean up. Associate the file pointer with the previously
4452 * reserved descriptor and return it.
4456 fsetfd(fdp, fp, indx);
4458 uap->sysmsg_result = indx;
4462 fsetfd(fdp, NULL, indx);
4472 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4477 sys_fhstat(struct fhstat_args *uap)
4479 struct thread *td = curthread;
4487 * Must be super user
4489 error = priv_check(td, PRIV_ROOT);
4493 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4498 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4501 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4502 error = vn_stat(vp, &sb, td->td_ucred);
4508 error = copyout(&sb, uap->sb, sizeof(sb));
4513 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4518 sys_fhstatfs(struct fhstatfs_args *uap)
4520 struct thread *td = curthread;
4521 struct proc *p = td->td_proc;
4526 char *fullpath, *freepath;
4531 * Must be super user
4533 if ((error = priv_check(td, PRIV_ROOT)))
4536 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4541 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4545 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4550 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4555 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4558 error = mount_path(p, mp, &fullpath, &freepath);
4561 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4562 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4563 kfree(freepath, M_TEMP);
4565 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4566 if (priv_check(td, PRIV_ROOT)) {
4567 bcopy(sp, &sb, sizeof(sb));
4568 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4571 error = copyout(sp, uap->buf, sizeof(*sp));
4578 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4583 sys_fhstatvfs(struct fhstatvfs_args *uap)
4585 struct thread *td = curthread;
4586 struct proc *p = td->td_proc;
4594 * Must be super user
4596 if ((error = priv_check(td, PRIV_ROOT)))
4599 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4604 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4608 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4613 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4616 sp = &mp->mnt_vstat;
4618 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4622 if (mp->mnt_flag & MNT_RDONLY)
4623 sp->f_flag |= ST_RDONLY;
4624 if (mp->mnt_flag & MNT_NOSUID)
4625 sp->f_flag |= ST_NOSUID;
4626 error = copyout(sp, uap->buf, sizeof(*sp));
4634 * Syscall to push extended attribute configuration information into the
4635 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4636 * a command (int cmd), and attribute name and misc data. For now, the
4637 * attribute name is left in userspace for consumption by the VFS_op.
4638 * It will probably be changed to be copied into sysspace by the
4639 * syscall in the future, once issues with various consumers of the
4640 * attribute code have raised their hands.
4642 * Currently this is used only by UFS Extended Attributes.
4647 sys_extattrctl(struct extattrctl_args *uap)
4649 struct nlookupdata nd;
4651 char attrname[EXTATTR_MAXNAMELEN];
4661 if (error == 0 && uap->filename) {
4662 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4665 error = nlookup(&nd);
4667 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4671 if (error == 0 && uap->attrname) {
4672 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4677 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4679 error = nlookup(&nd);
4681 error = ncp_writechk(&nd.nl_nch);
4683 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4685 uap->attrname, nd.nl_cred);
4696 * Syscall to get a named extended attribute on a file or directory.
4701 sys_extattr_set_file(struct extattr_set_file_args *uap)
4703 char attrname[EXTATTR_MAXNAMELEN];
4704 struct nlookupdata nd;
4710 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4717 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4719 error = nlookup(&nd);
4721 error = ncp_writechk(&nd.nl_nch);
4723 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4730 bzero(&auio, sizeof(auio));
4731 aiov.iov_base = uap->data;
4732 aiov.iov_len = uap->nbytes;
4733 auio.uio_iov = &aiov;
4734 auio.uio_iovcnt = 1;
4735 auio.uio_offset = 0;
4736 auio.uio_resid = uap->nbytes;
4737 auio.uio_rw = UIO_WRITE;
4738 auio.uio_td = curthread;
4740 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4750 * Syscall to get a named extended attribute on a file or directory.
4755 sys_extattr_get_file(struct extattr_get_file_args *uap)
4757 char attrname[EXTATTR_MAXNAMELEN];
4758 struct nlookupdata nd;
4764 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4771 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4773 error = nlookup(&nd);
4775 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4782 bzero(&auio, sizeof(auio));
4783 aiov.iov_base = uap->data;
4784 aiov.iov_len = uap->nbytes;
4785 auio.uio_iov = &aiov;
4786 auio.uio_iovcnt = 1;
4787 auio.uio_offset = 0;
4788 auio.uio_resid = uap->nbytes;
4789 auio.uio_rw = UIO_READ;
4790 auio.uio_td = curthread;
4792 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4794 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4803 * Syscall to delete a named extended attribute from a file or directory.
4804 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4809 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4811 char attrname[EXTATTR_MAXNAMELEN];
4812 struct nlookupdata nd;
4816 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4821 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4823 error = nlookup(&nd);
4825 error = ncp_writechk(&nd.nl_nch);
4827 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4829 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4830 attrname, NULL, nd.nl_cred);
4840 * Determine if the mount is visible to the process.
4843 chroot_visible_mnt(struct mount *mp, struct proc *p)
4845 struct nchandle nch;
4848 * Traverse from the mount point upwards. If we hit the process
4849 * root then the mount point is visible to the process.
4851 nch = mp->mnt_ncmountpt;
4853 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4854 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4857 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4858 nch = nch.mount->mnt_ncmounton;
4860 nch.ncp = nch.ncp->nc_parent;
4865 * If the mount point is not visible to the process, but the
4866 * process root is in a subdirectory of the mount, return
4869 if (p->p_fd->fd_nrdir.mount == mp)