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, ...);
86 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
87 static int checkvp_chdir (struct vnode *vn, struct thread *td);
88 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
89 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
90 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
91 static int getutimes (const struct timeval *, struct timespec *);
92 static int setfown (struct vnode *, uid_t, gid_t);
93 static int setfmode (struct vnode *, int);
94 static int setfflags (struct vnode *, int);
95 static int setutimes (struct vnode *, struct vattr *,
96 const struct timespec *, int);
97 static int usermount = 0; /* if 1, non-root can mount fs. */
99 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
101 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
104 * Virtual File System System Calls
108 * Mount a file system.
110 * mount_args(char *type, char *path, int flags, caddr_t data)
115 sys_mount(struct mount_args *uap)
117 struct thread *td = curthread;
120 struct mount *mp, *nullmp;
121 struct vfsconf *vfsp;
122 int error, flag = 0, flag2 = 0;
125 struct nlookupdata nd;
126 char fstypename[MFSNAMELEN];
135 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
139 * Do not allow NFS export by non-root users.
141 if (uap->flags & MNT_EXPORTED) {
142 error = priv_check(td, PRIV_ROOT);
147 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
149 if (priv_check(td, PRIV_ROOT))
150 uap->flags |= MNT_NOSUID | MNT_NODEV;
153 * Lookup the requested path and extract the nch and vnode.
155 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
157 if ((error = nlookup(&nd)) == 0) {
158 if (nd.nl_nch.ncp->nc_vp == NULL)
168 * If the target filesystem is resolved via a nullfs mount, then
169 * nd.nl_nch.mount will be pointing to the nullfs mount structure
170 * instead of the target file system. We need it in case we are
173 nullmp = nd.nl_nch.mount;
176 * Extract the locked+refd ncp and cleanup the nd structure
179 cache_zero(&nd.nl_nch);
182 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
189 * now we have the locked ref'd nch and unreferenced vnode.
192 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
199 * Extract the file system type. We need to know this early, to take
200 * appropriate actions if we are dealing with a nullfs.
202 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
209 * Now we have an unlocked ref'd nch and a locked ref'd vp
211 if (uap->flags & MNT_UPDATE) {
212 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
219 if (strncmp(fstypename, "null", 5) == 0) {
227 flag2 = mp->mnt_kern_flag;
229 * We only allow the filesystem to be reloaded if it
230 * is currently mounted read-only.
232 if ((uap->flags & MNT_RELOAD) &&
233 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
236 error = EOPNOTSUPP; /* Needs translation */
240 * Only root, or the user that did the original mount is
241 * permitted to update it.
243 if (mp->mnt_stat.f_owner != cred->cr_uid &&
244 (error = priv_check(td, PRIV_ROOT))) {
249 if (vfs_busy(mp, LK_NOWAIT)) {
255 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
262 vsetflags(vp, VMOUNT);
264 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
269 * If the user is not root, ensure that they own the directory
270 * onto which we are attempting to mount.
272 if ((error = VOP_GETATTR(vp, &va)) ||
273 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
278 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
283 if (vp->v_type != VDIR) {
289 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
295 vfsp = vfsconf_find_by_name(fstypename);
299 /* Only load modules for root (very important!) */
300 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
305 error = linker_load_file(fstypename, &lf);
306 if (error || lf == NULL) {
314 /* lookup again, see if the VFS was loaded */
315 vfsp = vfsconf_find_by_name(fstypename);
318 linker_file_unload(lf);
325 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
331 vsetflags(vp, VMOUNT);
334 * Allocate and initialize the filesystem.
336 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
338 vfs_busy(mp, LK_NOWAIT);
339 mp->mnt_op = vfsp->vfc_vfsops;
341 vfsp->vfc_refcount++;
342 mp->mnt_stat.f_type = vfsp->vfc_typenum;
343 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
344 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
345 mp->mnt_stat.f_owner = cred->cr_uid;
349 * Set the mount level flags.
351 if (uap->flags & MNT_RDONLY)
352 mp->mnt_flag |= MNT_RDONLY;
353 else if (mp->mnt_flag & MNT_RDONLY)
354 mp->mnt_kern_flag |= MNTK_WANTRDWR;
355 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
356 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
357 MNT_NOSYMFOLLOW | MNT_IGNORE |
358 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
359 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
360 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
361 MNT_NOSYMFOLLOW | MNT_IGNORE |
362 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
364 * Mount the filesystem.
365 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
368 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
369 if (mp->mnt_flag & MNT_UPDATE) {
370 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
371 mp->mnt_flag &= ~MNT_RDONLY;
372 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
373 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
376 mp->mnt_kern_flag = flag2;
379 vclrflags(vp, VMOUNT);
384 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
386 * Put the new filesystem on the mount list after root. The mount
387 * point gets its own mnt_ncmountpt (unless the VFS already set one
388 * up) which represents the root of the mount. The lookup code
389 * detects the mount point going forward and checks the root of
390 * the mount going backwards.
392 * It is not necessary to invalidate or purge the vnode underneath
393 * because elements under the mount will be given their own glue
397 if (mp->mnt_ncmountpt.ncp == NULL) {
399 * allocate, then unlock, but leave the ref intact
401 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
402 cache_unlock(&mp->mnt_ncmountpt);
404 mp->mnt_ncmounton = nch; /* inherits ref */
405 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
407 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
408 vclrflags(vp, VMOUNT);
409 mountlist_insert(mp, MNTINS_LAST);
411 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
412 error = vfs_allocate_syncvnode(mp);
414 error = VFS_START(mp, 0);
417 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
418 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
419 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
420 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
421 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
422 vclrflags(vp, VMOUNT);
423 mp->mnt_vfc->vfc_refcount--;
435 * Scan all active processes to see if any of them have a current
436 * or root directory onto which the new filesystem has just been
437 * mounted. If so, replace them with the new mount point.
439 * The passed ncp is ref'd and locked (from the mount code) and
440 * must be associated with the vnode representing the root of the
443 struct checkdirs_info {
444 struct nchandle old_nch;
445 struct nchandle new_nch;
446 struct vnode *old_vp;
447 struct vnode *new_vp;
450 static int checkdirs_callback(struct proc *p, void *data);
453 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
455 struct checkdirs_info info;
461 * If the old mount point's vnode has a usecount of 1, it is not
462 * being held as a descriptor anywhere.
464 olddp = old_nch->ncp->nc_vp;
465 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
469 * Force the root vnode of the new mount point to be resolved
470 * so we can update any matching processes.
473 if (VFS_ROOT(mp, &newdp))
474 panic("mount: lost mount");
475 cache_setunresolved(new_nch);
476 cache_setvp(new_nch, newdp);
479 * Special handling of the root node
481 if (rootvnode == olddp) {
483 vfs_cache_setroot(newdp, cache_hold(new_nch));
487 * Pass newdp separately so the callback does not have to access
488 * it via new_nch->ncp->nc_vp.
490 info.old_nch = *old_nch;
491 info.new_nch = *new_nch;
493 allproc_scan(checkdirs_callback, &info);
498 * NOTE: callback is not MP safe because the scanned process's filedesc
499 * structure can be ripped out from under us, amoung other things.
502 checkdirs_callback(struct proc *p, void *data)
504 struct checkdirs_info *info = data;
505 struct filedesc *fdp;
506 struct nchandle ncdrop1;
507 struct nchandle ncdrop2;
508 struct vnode *vprele1;
509 struct vnode *vprele2;
511 if ((fdp = p->p_fd) != NULL) {
512 cache_zero(&ncdrop1);
513 cache_zero(&ncdrop2);
518 * MPUNSAFE - XXX fdp can be pulled out from under a
521 * A shared filedesc is ok, we don't have to copy it
522 * because we are making this change globally.
524 spin_lock(&fdp->fd_spin);
525 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
526 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
527 vprele1 = fdp->fd_cdir;
529 fdp->fd_cdir = info->new_vp;
530 ncdrop1 = fdp->fd_ncdir;
531 cache_copy(&info->new_nch, &fdp->fd_ncdir);
533 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
534 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
535 vprele2 = fdp->fd_rdir;
537 fdp->fd_rdir = info->new_vp;
538 ncdrop2 = fdp->fd_nrdir;
539 cache_copy(&info->new_nch, &fdp->fd_nrdir);
541 spin_unlock(&fdp->fd_spin);
543 cache_drop(&ncdrop1);
545 cache_drop(&ncdrop2);
555 * Unmount a file system.
557 * Note: unmount takes a path to the vnode mounted on as argument,
558 * not special file (as before).
560 * umount_args(char *path, int flags)
565 sys_unmount(struct unmount_args *uap)
567 struct thread *td = curthread;
568 struct proc *p __debugvar = td->td_proc;
569 struct mount *mp = NULL;
570 struct nlookupdata nd;
575 if (td->td_ucred->cr_prison != NULL) {
579 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
582 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
584 error = nlookup(&nd);
588 mp = nd.nl_nch.mount;
591 * Only root, or the user that did the original mount is
592 * permitted to unmount this filesystem.
594 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
595 (error = priv_check(td, PRIV_ROOT)))
599 * Don't allow unmounting the root file system.
601 if (mp->mnt_flag & MNT_ROOTFS) {
607 * Must be the root of the filesystem
609 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
617 error = dounmount(mp, uap->flags);
624 * Do the actual file system unmount.
627 dounmount_interlock(struct mount *mp)
629 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
631 mp->mnt_kern_flag |= MNTK_UNMOUNT;
636 unmount_allproc_cb(struct proc *p, void *arg)
640 if (p->p_textnch.ncp == NULL)
643 mp = (struct mount *)arg;
644 if (p->p_textnch.mount == mp)
645 cache_drop(&p->p_textnch);
651 dounmount(struct mount *mp, int flags)
653 struct namecache *ncp;
662 * Exclusive access for unmounting purposes
664 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
668 * Allow filesystems to detect that a forced unmount is in progress.
670 if (flags & MNT_FORCE)
671 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
672 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
673 error = lockmgr(&mp->mnt_lock, lflags);
675 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
676 if (mp->mnt_kern_flag & MNTK_MWAIT)
681 if (mp->mnt_flag & MNT_EXPUBLIC)
682 vfs_setpublicfs(NULL, NULL, NULL);
684 vfs_msync(mp, MNT_WAIT);
685 async_flag = mp->mnt_flag & MNT_ASYNC;
686 mp->mnt_flag &=~ MNT_ASYNC;
689 * If this filesystem isn't aliasing other filesystems,
690 * try to invalidate any remaining namecache entries and
691 * check the count afterwords.
693 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
694 cache_lock(&mp->mnt_ncmountpt);
695 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
696 cache_unlock(&mp->mnt_ncmountpt);
698 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
699 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
700 allproc_scan(&unmount_allproc_cb, mp);
703 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
704 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
706 if ((flags & MNT_FORCE) == 0) {
708 mount_warning(mp, "Cannot unmount: "
714 mount_warning(mp, "Forced unmount: "
725 * nchandle records ref the mount structure. Expect a count of 1
726 * (our mount->mnt_ncmountpt).
728 if (mp->mnt_refs != 1) {
729 if ((flags & MNT_FORCE) == 0) {
730 mount_warning(mp, "Cannot unmount: "
731 "%d process references still "
732 "present", mp->mnt_refs);
735 mount_warning(mp, "Forced unmount: "
736 "%d process references still "
737 "present", mp->mnt_refs);
743 * Decomission our special mnt_syncer vnode. This also stops
744 * the vnlru code. If we are unable to unmount we recommission
748 if ((vp = mp->mnt_syncer) != NULL) {
749 mp->mnt_syncer = NULL;
752 if (((mp->mnt_flag & MNT_RDONLY) ||
753 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
754 (flags & MNT_FORCE)) {
755 error = VFS_UNMOUNT(mp, flags);
759 if (mp->mnt_syncer == NULL)
760 vfs_allocate_syncvnode(mp);
761 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
762 mp->mnt_flag |= async_flag;
763 lockmgr(&mp->mnt_lock, LK_RELEASE);
764 if (mp->mnt_kern_flag & MNTK_MWAIT)
769 * Clean up any journals still associated with the mount after
770 * filesystem activity has ceased.
772 journal_remove_all_journals(mp,
773 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
775 mountlist_remove(mp);
778 * Remove any installed vnode ops here so the individual VFSs don't
781 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
782 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
783 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
784 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
785 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
787 if (mp->mnt_ncmountpt.ncp != NULL) {
788 nch = mp->mnt_ncmountpt;
789 cache_zero(&mp->mnt_ncmountpt);
790 cache_clrmountpt(&nch);
793 if (mp->mnt_ncmounton.ncp != NULL) {
794 nch = mp->mnt_ncmounton;
795 cache_zero(&mp->mnt_ncmounton);
796 cache_clrmountpt(&nch);
800 mp->mnt_vfc->vfc_refcount--;
801 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
802 panic("unmount: dangling vnode");
803 lockmgr(&mp->mnt_lock, LK_RELEASE);
804 if (mp->mnt_kern_flag & MNTK_MWAIT)
813 mount_warning(struct mount *mp, const char *ctl, ...)
820 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf, 0) == 0) {
821 kprintf("unmount(%s): ", ptr);
826 kprintf("unmount(%p", mp);
827 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
828 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
837 * Shim cache_fullpath() to handle the case where a process is chrooted into
838 * a subdirectory of a mount. In this case if the root mount matches the
839 * process root directory's mount we have to specify the process's root
840 * directory instead of the mount point, because the mount point might
841 * be above the root directory.
845 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
847 struct nchandle *nch;
849 if (p && p->p_fd->fd_nrdir.mount == mp)
850 nch = &p->p_fd->fd_nrdir;
852 nch = &mp->mnt_ncmountpt;
853 return(cache_fullpath(p, nch, rb, fb, 0));
857 * Sync each mounted filesystem.
861 static int syncprt = 0;
862 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
865 static int sync_callback(struct mount *mp, void *data);
871 sys_sync(struct sync_args *uap)
874 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
877 * print out buffer pool stat information on each sync() call.
888 sync_callback(struct mount *mp, void *data __unused)
892 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
893 asyncflag = mp->mnt_flag & MNT_ASYNC;
894 mp->mnt_flag &= ~MNT_ASYNC;
895 vfs_msync(mp, MNT_NOWAIT);
896 VFS_SYNC(mp, MNT_NOWAIT);
897 mp->mnt_flag |= asyncflag;
902 /* XXX PRISON: could be per prison flag */
903 static int prison_quotas;
905 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
909 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
911 * Change filesystem quotas.
916 sys_quotactl(struct quotactl_args *uap)
918 struct nlookupdata nd;
927 if (td->td_ucred->cr_prison && !prison_quotas) {
932 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
934 error = nlookup(&nd);
936 mp = nd.nl_nch.mount;
937 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
938 uap->arg, nd.nl_cred);
947 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
948 * void *buf, int buflen)
950 * This function operates on a mount point and executes the specified
951 * operation using the specified control data, and possibly returns data.
953 * The actual number of bytes stored in the result buffer is returned, 0
954 * if none, otherwise an error is returned.
959 sys_mountctl(struct mountctl_args *uap)
961 struct thread *td = curthread;
962 struct proc *p = td->td_proc;
970 * Sanity and permissions checks. We must be root.
973 if (td->td_ucred->cr_prison != NULL)
975 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
976 (error = priv_check(td, PRIV_ROOT)) != 0)
980 * Argument length checks
982 if (uap->ctllen < 0 || uap->ctllen > 1024)
984 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
986 if (uap->path == NULL)
990 * Allocate the necessary buffers and copyin data
992 path = objcache_get(namei_oc, M_WAITOK);
993 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
998 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
999 error = copyin(uap->ctl, ctl, uap->ctllen);
1004 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1007 * Validate the descriptor
1010 fp = holdfp(p->p_fd, uap->fd, -1);
1020 * Execute the internal kernel function and clean up.
1023 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1027 if (error == 0 && uap->sysmsg_result > 0)
1028 error = copyout(buf, uap->buf, uap->sysmsg_result);
1031 objcache_put(namei_oc, path);
1040 * Execute a mount control operation by resolving the path to a mount point
1041 * and calling vop_mountctl().
1043 * Use the mount point from the nch instead of the vnode so nullfs mounts
1044 * can properly spike the VOP.
1047 kern_mountctl(const char *path, int op, struct file *fp,
1048 const void *ctl, int ctllen,
1049 void *buf, int buflen, int *res)
1053 struct nlookupdata nd;
1058 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1060 error = nlookup(&nd);
1062 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1063 mp = nd.nl_nch.mount;
1070 * Must be the root of the filesystem
1072 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1076 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1083 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1085 struct thread *td = curthread;
1086 struct proc *p = td->td_proc;
1089 char *fullpath, *freepath;
1092 if ((error = nlookup(nd)) != 0)
1094 mp = nd->nl_nch.mount;
1096 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1099 error = mount_path(p, mp, &fullpath, &freepath);
1102 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1103 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1104 kfree(freepath, M_TEMP);
1106 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1107 bcopy(sp, buf, sizeof(*buf));
1108 /* Only root should have access to the fsid's. */
1109 if (priv_check(td, PRIV_ROOT))
1110 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1115 * statfs_args(char *path, struct statfs *buf)
1117 * Get filesystem statistics.
1122 sys_statfs(struct statfs_args *uap)
1124 struct nlookupdata nd;
1129 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1131 error = kern_statfs(&nd, &buf);
1134 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1143 kern_fstatfs(int fd, struct statfs *buf)
1145 struct thread *td = curthread;
1146 struct proc *p = td->td_proc;
1150 char *fullpath, *freepath;
1154 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1157 mp = ((struct vnode *)fp->f_data)->v_mount;
1162 if (fp->f_cred == NULL) {
1167 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1170 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1172 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1173 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1174 kfree(freepath, M_TEMP);
1176 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1177 bcopy(sp, buf, sizeof(*buf));
1179 /* Only root should have access to the fsid's. */
1180 if (priv_check(td, PRIV_ROOT))
1181 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1190 * fstatfs_args(int fd, struct statfs *buf)
1192 * Get filesystem statistics.
1197 sys_fstatfs(struct fstatfs_args *uap)
1202 error = kern_fstatfs(uap->fd, &buf);
1205 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1210 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1216 if ((error = nlookup(nd)) != 0)
1218 mp = nd->nl_nch.mount;
1219 sp = &mp->mnt_vstat;
1220 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1224 if (mp->mnt_flag & MNT_RDONLY)
1225 sp->f_flag |= ST_RDONLY;
1226 if (mp->mnt_flag & MNT_NOSUID)
1227 sp->f_flag |= ST_NOSUID;
1228 bcopy(sp, buf, sizeof(*buf));
1233 * statfs_args(char *path, struct statfs *buf)
1235 * Get filesystem statistics.
1240 sys_statvfs(struct statvfs_args *uap)
1242 struct nlookupdata nd;
1247 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1249 error = kern_statvfs(&nd, &buf);
1252 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1258 kern_fstatvfs(int fd, struct statvfs *buf)
1260 struct thread *td = curthread;
1261 struct proc *p = td->td_proc;
1268 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1270 mp = ((struct vnode *)fp->f_data)->v_mount;
1275 if (fp->f_cred == NULL) {
1279 sp = &mp->mnt_vstat;
1280 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1284 if (mp->mnt_flag & MNT_RDONLY)
1285 sp->f_flag |= ST_RDONLY;
1286 if (mp->mnt_flag & MNT_NOSUID)
1287 sp->f_flag |= ST_NOSUID;
1289 bcopy(sp, buf, sizeof(*buf));
1297 * fstatfs_args(int fd, struct statfs *buf)
1299 * Get filesystem statistics.
1304 sys_fstatvfs(struct fstatvfs_args *uap)
1310 error = kern_fstatvfs(uap->fd, &buf);
1314 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1319 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1321 * Get statistics on all filesystems.
1324 struct getfsstat_info {
1325 struct statfs *sfsp;
1333 static int getfsstat_callback(struct mount *, void *);
1339 sys_getfsstat(struct getfsstat_args *uap)
1341 struct thread *td = curthread;
1342 struct getfsstat_info info;
1344 bzero(&info, sizeof(info));
1346 info.maxcount = uap->bufsize / sizeof(struct statfs);
1347 info.sfsp = uap->buf;
1349 info.flags = uap->flags;
1353 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1355 if (info.sfsp && info.count > info.maxcount)
1356 uap->sysmsg_result = info.maxcount;
1358 uap->sysmsg_result = info.count;
1359 return (info.error);
1363 getfsstat_callback(struct mount *mp, void *data)
1365 struct getfsstat_info *info = data;
1371 if (info->sfsp && info->count < info->maxcount) {
1372 if (info->td->td_proc &&
1373 !chroot_visible_mnt(mp, info->td->td_proc)) {
1379 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1380 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1381 * overrides MNT_WAIT.
1383 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1384 (info->flags & MNT_WAIT)) &&
1385 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1388 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1390 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1392 info->error = error;
1395 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1396 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1397 kfree(freepath, M_TEMP);
1399 error = copyout(sp, info->sfsp, sizeof(*sp));
1401 info->error = error;
1411 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1412 long bufsize, int flags)
1414 * Get statistics on all filesystems.
1417 struct getvfsstat_info {
1418 struct statfs *sfsp;
1419 struct statvfs *vsfsp;
1427 static int getvfsstat_callback(struct mount *, void *);
1433 sys_getvfsstat(struct getvfsstat_args *uap)
1435 struct thread *td = curthread;
1436 struct getvfsstat_info info;
1438 bzero(&info, sizeof(info));
1440 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1441 info.sfsp = uap->buf;
1442 info.vsfsp = uap->vbuf;
1444 info.flags = uap->flags;
1448 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1449 if (info.vsfsp && info.count > info.maxcount)
1450 uap->sysmsg_result = info.maxcount;
1452 uap->sysmsg_result = info.count;
1454 return (info.error);
1458 getvfsstat_callback(struct mount *mp, void *data)
1460 struct getvfsstat_info *info = data;
1462 struct statvfs *vsp;
1467 if (info->vsfsp && info->count < info->maxcount) {
1468 if (info->td->td_proc &&
1469 !chroot_visible_mnt(mp, info->td->td_proc)) {
1473 vsp = &mp->mnt_vstat;
1476 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1477 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1478 * overrides MNT_WAIT.
1480 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1481 (info->flags & MNT_WAIT)) &&
1482 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1485 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1487 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1488 (info->flags & MNT_WAIT)) &&
1489 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1493 if (mp->mnt_flag & MNT_RDONLY)
1494 vsp->f_flag |= ST_RDONLY;
1495 if (mp->mnt_flag & MNT_NOSUID)
1496 vsp->f_flag |= ST_NOSUID;
1498 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1500 info->error = error;
1503 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1504 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1505 kfree(freepath, M_TEMP);
1507 error = copyout(sp, info->sfsp, sizeof(*sp));
1509 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1511 info->error = error;
1523 * fchdir_args(int fd)
1525 * Change current working directory to a given file descriptor.
1530 sys_fchdir(struct fchdir_args *uap)
1532 struct thread *td = curthread;
1533 struct proc *p = td->td_proc;
1534 struct filedesc *fdp = p->p_fd;
1535 struct vnode *vp, *ovp;
1538 struct nchandle nch, onch, tnch;
1541 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1544 vp = (struct vnode *)fp->f_data;
1546 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1547 if (fp->f_nchandle.ncp == NULL)
1550 error = checkvp_chdir(vp, td);
1555 cache_copy(&fp->f_nchandle, &nch);
1558 * If the ncp has become a mount point, traverse through
1562 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1563 (mp = cache_findmount(&nch)) != NULL
1565 error = nlookup_mp(mp, &tnch);
1567 cache_unlock(&tnch); /* leave ref intact */
1569 vp = tnch.ncp->nc_vp;
1570 error = vget(vp, LK_SHARED);
1571 KKASSERT(error == 0);
1578 onch = fdp->fd_ncdir;
1579 vn_unlock(vp); /* leave ref intact */
1581 fdp->fd_ncdir = nch;
1595 kern_chdir(struct nlookupdata *nd)
1597 struct thread *td = curthread;
1598 struct proc *p = td->td_proc;
1599 struct filedesc *fdp = p->p_fd;
1600 struct vnode *vp, *ovp;
1601 struct nchandle onch;
1604 if ((error = nlookup(nd)) != 0)
1606 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1608 if ((error = vget(vp, LK_SHARED)) != 0)
1611 error = checkvp_chdir(vp, td);
1615 onch = fdp->fd_ncdir;
1616 cache_unlock(&nd->nl_nch); /* leave reference intact */
1617 fdp->fd_ncdir = nd->nl_nch;
1621 cache_zero(&nd->nl_nch);
1629 * chdir_args(char *path)
1631 * Change current working directory (``.'').
1636 sys_chdir(struct chdir_args *uap)
1638 struct nlookupdata nd;
1642 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1644 error = kern_chdir(&nd);
1651 * Helper function for raised chroot(2) security function: Refuse if
1652 * any filedescriptors are open directories.
1655 chroot_refuse_vdir_fds(struct filedesc *fdp)
1662 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1663 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1665 vp = (struct vnode *)fp->f_data;
1666 if (vp->v_type != VDIR) {
1677 * This sysctl determines if we will allow a process to chroot(2) if it
1678 * has a directory open:
1679 * 0: disallowed for all processes.
1680 * 1: allowed for processes that were not already chroot(2)'ed.
1681 * 2: allowed for all processes.
1684 static int chroot_allow_open_directories = 1;
1686 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1687 &chroot_allow_open_directories, 0, "");
1690 * chroot to the specified namecache entry. We obtain the vp from the
1691 * namecache data. The passed ncp must be locked and referenced and will
1692 * remain locked and referenced on return.
1695 kern_chroot(struct nchandle *nch)
1697 struct thread *td = curthread;
1698 struct proc *p = td->td_proc;
1699 struct filedesc *fdp = p->p_fd;
1704 * Only privileged user can chroot
1706 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1711 * Disallow open directory descriptors (fchdir() breakouts).
1713 if (chroot_allow_open_directories == 0 ||
1714 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1715 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1718 if ((vp = nch->ncp->nc_vp) == NULL)
1721 if ((error = vget(vp, LK_SHARED)) != 0)
1725 * Check the validity of vp as a directory to change to and
1726 * associate it with rdir/jdir.
1728 error = checkvp_chdir(vp, td);
1729 vn_unlock(vp); /* leave reference intact */
1731 vrele(fdp->fd_rdir);
1732 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1733 cache_drop(&fdp->fd_nrdir);
1734 cache_copy(nch, &fdp->fd_nrdir);
1735 if (fdp->fd_jdir == NULL) {
1738 cache_copy(nch, &fdp->fd_njdir);
1747 * chroot_args(char *path)
1749 * Change notion of root (``/'') directory.
1754 sys_chroot(struct chroot_args *uap)
1756 struct thread *td __debugvar = curthread;
1757 struct nlookupdata nd;
1760 KKASSERT(td->td_proc);
1762 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1764 nd.nl_flags |= NLC_EXEC;
1765 error = nlookup(&nd);
1767 error = kern_chroot(&nd.nl_nch);
1775 sys_chroot_kernel(struct chroot_kernel_args *uap)
1777 struct thread *td = curthread;
1778 struct nlookupdata nd;
1779 struct nchandle *nch;
1784 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1788 error = nlookup(&nd);
1794 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1798 if ((vp = nch->ncp->nc_vp) == NULL) {
1803 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1806 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1807 vfs_cache_setroot(vp, cache_hold(nch));
1817 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1818 * determine whether it is legal to chdir to the vnode. The vnode's state
1819 * is not changed by this call.
1822 checkvp_chdir(struct vnode *vp, struct thread *td)
1826 if (vp->v_type != VDIR)
1829 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1837 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1839 struct thread *td = curthread;
1840 struct proc *p = td->td_proc;
1841 struct lwp *lp = td->td_lwp;
1842 struct filedesc *fdp = p->p_fd;
1847 int type, indx, error;
1850 if ((oflags & O_ACCMODE) == O_ACCMODE)
1852 flags = FFLAGS(oflags);
1853 error = falloc(lp, &nfp, NULL);
1857 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1860 * XXX p_dupfd is a real mess. It allows a device to return a
1861 * file descriptor to be duplicated rather then doing the open
1867 * Call vn_open() to do the lookup and assign the vnode to the
1868 * file pointer. vn_open() does not change the ref count on fp
1869 * and the vnode, on success, will be inherited by the file pointer
1872 nd->nl_flags |= NLC_LOCKVP;
1873 error = vn_open(nd, fp, flags, cmode);
1877 * handle special fdopen() case. bleh. dupfdopen() is
1878 * responsible for dropping the old contents of ofiles[indx]
1881 * Note that fsetfd() will add a ref to fp which represents
1882 * the fd_files[] assignment. We must still drop our
1885 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1886 if (fdalloc(p, 0, &indx) == 0) {
1887 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1890 fdrop(fp); /* our ref */
1893 fsetfd(fdp, NULL, indx);
1896 fdrop(fp); /* our ref */
1897 if (error == ERESTART)
1903 * ref the vnode for ourselves so it can't be ripped out from under
1904 * is. XXX need an ND flag to request that the vnode be returned
1907 * Reserve a file descriptor but do not assign it until the open
1910 vp = (struct vnode *)fp->f_data;
1912 if ((error = fdalloc(p, 0, &indx)) != 0) {
1919 * If no error occurs the vp will have been assigned to the file
1924 if (flags & (O_EXLOCK | O_SHLOCK)) {
1925 lf.l_whence = SEEK_SET;
1928 if (flags & O_EXLOCK)
1929 lf.l_type = F_WRLCK;
1931 lf.l_type = F_RDLCK;
1932 if (flags & FNONBLOCK)
1937 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1939 * lock request failed. Clean up the reserved
1943 fsetfd(fdp, NULL, indx);
1947 fp->f_flag |= FHASLOCK;
1951 * Assert that all regular file vnodes were created with a object.
1953 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1954 ("open: regular file has no backing object after vn_open"));
1960 * release our private reference, leaving the one associated with the
1961 * descriptor table intact.
1963 fsetfd(fdp, fp, indx);
1970 * open_args(char *path, int flags, int mode)
1972 * Check permissions, allocate an open file structure,
1973 * and call the device open routine if any.
1978 sys_open(struct open_args *uap)
1980 CACHE_MPLOCK_DECLARE;
1981 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);
1996 * openat_args(int fd, char *path, int flags, int mode)
2001 sys_openat(struct openat_args *uap)
2003 CACHE_MPLOCK_DECLARE;
2004 struct nlookupdata nd;
2009 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2011 error = kern_open(&nd, uap->flags, uap->mode,
2012 &uap->sysmsg_result);
2014 nlookup_done_at(&nd, fp);
2020 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2022 struct thread *td = curthread;
2023 struct proc *p = td->td_proc;
2032 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2033 vattr.va_rmajor = rmajor;
2034 vattr.va_rminor = rminor;
2036 switch (mode & S_IFMT) {
2037 case S_IFMT: /* used by badsect to flag bad sectors */
2038 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2039 vattr.va_type = VBAD;
2042 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2043 vattr.va_type = VCHR;
2046 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2047 vattr.va_type = VBLK;
2050 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2053 case S_IFDIR: /* special directories support for HAMMER */
2054 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2055 vattr.va_type = VDIR;
2066 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2067 if ((error = nlookup(nd)) != 0)
2069 if (nd->nl_nch.ncp->nc_vp)
2071 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2075 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2076 nd->nl_cred, NAMEI_CREATE);
2079 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2080 &vp, nd->nl_cred, &vattr);
2088 * mknod_args(char *path, int mode, int dev)
2090 * Create a special file.
2095 sys_mknod(struct mknod_args *uap)
2097 struct nlookupdata nd;
2101 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2103 error = kern_mknod(&nd, uap->mode,
2104 umajor(uap->dev), uminor(uap->dev));
2112 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2114 * Create a special file. The path is relative to the directory associated
2120 sys_mknodat(struct mknodat_args *uap)
2122 struct nlookupdata nd;
2127 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2129 error = kern_mknod(&nd, uap->mode,
2130 umajor(uap->dev), uminor(uap->dev));
2132 nlookup_done_at(&nd, fp);
2138 kern_mkfifo(struct nlookupdata *nd, int mode)
2140 struct thread *td = curthread;
2141 struct proc *p = td->td_proc;
2148 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2149 if ((error = nlookup(nd)) != 0)
2151 if (nd->nl_nch.ncp->nc_vp)
2153 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2157 vattr.va_type = VFIFO;
2158 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2160 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2167 * mkfifo_args(char *path, int mode)
2169 * Create a named pipe.
2174 sys_mkfifo(struct mkfifo_args *uap)
2176 struct nlookupdata nd;
2180 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2182 error = kern_mkfifo(&nd, uap->mode);
2189 * mkfifoat_args(int fd, char *path, mode_t mode)
2191 * Create a named pipe. The path is relative to the directory associated
2197 sys_mkfifoat(struct mkfifoat_args *uap)
2199 struct nlookupdata nd;
2204 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2206 error = kern_mkfifo(&nd, uap->mode);
2207 nlookup_done_at(&nd, fp);
2212 static int hardlink_check_uid = 0;
2213 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2214 &hardlink_check_uid, 0,
2215 "Unprivileged processes cannot create hard links to files owned by other "
2217 static int hardlink_check_gid = 0;
2218 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2219 &hardlink_check_gid, 0,
2220 "Unprivileged processes cannot create hard links to files owned by other "
2224 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2230 * Shortcut if disabled
2232 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2236 * Privileged user can always hardlink
2238 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2242 * Otherwise only if the originating file is owned by the
2243 * same user or group. Note that any group is allowed if
2244 * the file is owned by the caller.
2246 error = VOP_GETATTR(vp, &va);
2250 if (hardlink_check_uid) {
2251 if (cred->cr_uid != va.va_uid)
2255 if (hardlink_check_gid) {
2256 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2264 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2266 struct thread *td = curthread;
2271 * Lookup the source and obtained a locked vnode.
2273 * You may only hardlink a file which you have write permission
2274 * on or which you own.
2276 * XXX relookup on vget failure / race ?
2279 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2280 if ((error = nlookup(nd)) != 0)
2282 vp = nd->nl_nch.ncp->nc_vp;
2283 KKASSERT(vp != NULL);
2284 if (vp->v_type == VDIR)
2285 return (EPERM); /* POSIX */
2286 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2288 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2292 * Unlock the source so we can lookup the target without deadlocking
2293 * (XXX vp is locked already, possible other deadlock?). The target
2296 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2297 nd->nl_flags &= ~NLC_NCPISLOCKED;
2298 cache_unlock(&nd->nl_nch);
2301 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2302 if ((error = nlookup(linknd)) != 0) {
2306 if (linknd->nl_nch.ncp->nc_vp) {
2310 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2316 * Finally run the new API VOP.
2318 error = can_hardlink(vp, td, td->td_ucred);
2320 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2321 vp, linknd->nl_cred);
2328 * link_args(char *path, char *link)
2330 * Make a hard file link.
2335 sys_link(struct link_args *uap)
2337 struct nlookupdata nd, linknd;
2341 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2343 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2345 error = kern_link(&nd, &linknd);
2346 nlookup_done(&linknd);
2354 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2362 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2363 if ((error = nlookup(nd)) != 0)
2365 if (nd->nl_nch.ncp->nc_vp)
2367 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2371 vattr.va_mode = mode;
2372 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2379 * symlink(char *path, char *link)
2381 * Make a symbolic link.
2386 sys_symlink(struct symlink_args *uap)
2388 struct thread *td = curthread;
2389 struct nlookupdata nd;
2394 path = objcache_get(namei_oc, M_WAITOK);
2395 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2398 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2400 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2401 error = kern_symlink(&nd, path, mode);
2406 objcache_put(namei_oc, path);
2411 * symlinkat_args(char *path1, int fd, char *path2)
2413 * Make a symbolic link. The path2 argument is relative to the directory
2414 * associated with fd.
2419 sys_symlinkat(struct symlinkat_args *uap)
2421 struct thread *td = curthread;
2422 struct nlookupdata nd;
2428 path1 = objcache_get(namei_oc, M_WAITOK);
2429 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2432 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2435 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2436 error = kern_symlink(&nd, path1, mode);
2438 nlookup_done_at(&nd, fp);
2441 objcache_put(namei_oc, path1);
2446 * undelete_args(char *path)
2448 * Delete a whiteout from the filesystem.
2453 sys_undelete(struct undelete_args *uap)
2455 struct nlookupdata nd;
2459 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2461 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2463 error = nlookup(&nd);
2465 error = ncp_writechk(&nd.nl_nch);
2467 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2476 kern_unlink(struct nlookupdata *nd)
2481 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2482 if ((error = nlookup(nd)) != 0)
2484 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2486 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2491 * unlink_args(char *path)
2493 * Delete a name from the filesystem.
2498 sys_unlink(struct unlink_args *uap)
2500 struct nlookupdata nd;
2504 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2506 error = kern_unlink(&nd);
2514 * unlinkat_args(int fd, char *path, int flags)
2516 * Delete the file or directory entry pointed to by fd/path.
2521 sys_unlinkat(struct unlinkat_args *uap)
2523 struct nlookupdata nd;
2527 if (uap->flags & ~AT_REMOVEDIR)
2531 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2533 if (uap->flags & AT_REMOVEDIR)
2534 error = kern_rmdir(&nd);
2536 error = kern_unlink(&nd);
2538 nlookup_done_at(&nd, fp);
2547 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2549 struct thread *td = curthread;
2550 struct proc *p = td->td_proc;
2557 fp = holdfp(p->p_fd, fd, -1);
2560 if (fp->f_type != DTYPE_VNODE) {
2564 vp = (struct vnode *)fp->f_data;
2568 spin_lock(&fp->f_spin);
2569 new_offset = fp->f_offset + offset;
2574 error = VOP_GETATTR(vp, &vattr);
2576 spin_lock(&fp->f_spin);
2577 new_offset = offset + vattr.va_size;
2580 new_offset = offset;
2582 spin_lock(&fp->f_spin);
2587 spin_lock(&fp->f_spin);
2592 * Validate the seek position. Negative offsets are not allowed
2593 * for regular files or directories.
2595 * Normally we would also not want to allow negative offsets for
2596 * character and block-special devices. However kvm addresses
2597 * on 64 bit architectures might appear to be negative and must
2601 if (new_offset < 0 &&
2602 (vp->v_type == VREG || vp->v_type == VDIR)) {
2605 fp->f_offset = new_offset;
2608 *res = fp->f_offset;
2609 spin_unlock(&fp->f_spin);
2616 * lseek_args(int fd, int pad, off_t offset, int whence)
2618 * Reposition read/write file offset.
2623 sys_lseek(struct lseek_args *uap)
2627 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2628 &uap->sysmsg_offset);
2634 * Check if current process can access given file. amode is a bitmask of *_OK
2635 * access bits. flags is a bitmask of AT_* flags.
2638 kern_access(struct nlookupdata *nd, int amode, int flags)
2643 if (flags & ~AT_EACCESS)
2645 if ((error = nlookup(nd)) != 0)
2648 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2652 /* Flags == 0 means only check for existence. */
2661 if ((mode & VWRITE) == 0 ||
2662 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2663 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2666 * If the file handle is stale we have to re-resolve the
2667 * entry. This is a hack at the moment.
2669 if (error == ESTALE) {
2671 cache_setunresolved(&nd->nl_nch);
2672 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2685 * access_args(char *path, int flags)
2687 * Check access permissions.
2692 sys_access(struct access_args *uap)
2694 struct nlookupdata nd;
2698 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2700 error = kern_access(&nd, uap->flags, 0);
2708 * faccessat_args(int fd, char *path, int amode, int flags)
2710 * Check access permissions.
2715 sys_faccessat(struct faccessat_args *uap)
2717 struct nlookupdata nd;
2722 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2725 error = kern_access(&nd, uap->amode, uap->flags);
2726 nlookup_done_at(&nd, fp);
2736 kern_stat(struct nlookupdata *nd, struct stat *st)
2742 if ((error = nlookup(nd)) != 0)
2745 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2749 if ((error = vget(vp, LK_SHARED)) != 0)
2751 error = vn_stat(vp, st, nd->nl_cred);
2754 * If the file handle is stale we have to re-resolve the entry. This
2755 * is a hack at the moment.
2757 if (error == ESTALE) {
2759 cache_setunresolved(&nd->nl_nch);
2760 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2770 * stat_args(char *path, struct stat *ub)
2772 * Get file status; this version follows links.
2777 sys_stat(struct stat_args *uap)
2779 CACHE_MPLOCK_DECLARE;
2780 struct nlookupdata nd;
2785 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2787 error = kern_stat(&nd, &st);
2789 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2797 * lstat_args(char *path, struct stat *ub)
2799 * Get file status; this version does not follow links.
2804 sys_lstat(struct lstat_args *uap)
2806 CACHE_MPLOCK_DECLARE;
2807 struct nlookupdata nd;
2812 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2814 error = kern_stat(&nd, &st);
2816 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2824 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2826 * Get status of file pointed to by fd/path.
2831 sys_fstatat(struct fstatat_args *uap)
2833 CACHE_MPLOCK_DECLARE;
2834 struct nlookupdata nd;
2840 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2843 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2846 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2847 UIO_USERSPACE, flags);
2849 error = kern_stat(&nd, &st);
2851 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2853 nlookup_done_at(&nd, fp);
2859 * pathconf_Args(char *path, int name)
2861 * Get configurable pathname variables.
2866 sys_pathconf(struct pathconf_args *uap)
2868 struct nlookupdata nd;
2874 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2876 error = nlookup(&nd);
2878 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2881 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2890 * kern_readlink isn't properly split yet. There is a copyin burried
2891 * in VOP_READLINK().
2894 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2896 struct thread *td = curthread;
2902 if ((error = nlookup(nd)) != 0)
2904 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2907 if (vp->v_type != VLNK) {
2910 aiov.iov_base = buf;
2911 aiov.iov_len = count;
2912 auio.uio_iov = &aiov;
2913 auio.uio_iovcnt = 1;
2914 auio.uio_offset = 0;
2915 auio.uio_rw = UIO_READ;
2916 auio.uio_segflg = UIO_USERSPACE;
2918 auio.uio_resid = count;
2919 error = VOP_READLINK(vp, &auio, td->td_ucred);
2922 *res = count - auio.uio_resid;
2927 * readlink_args(char *path, char *buf, int count)
2929 * Return target name of a symbolic link.
2934 sys_readlink(struct readlink_args *uap)
2936 struct nlookupdata nd;
2940 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2942 error = kern_readlink(&nd, uap->buf, uap->count,
2943 &uap->sysmsg_result);
2951 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2953 * Return target name of a symbolic link. The path is relative to the
2954 * directory associated with fd.
2959 sys_readlinkat(struct readlinkat_args *uap)
2961 struct nlookupdata nd;
2966 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2968 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2969 &uap->sysmsg_result);
2971 nlookup_done_at(&nd, fp);
2977 setfflags(struct vnode *vp, int flags)
2979 struct thread *td = curthread;
2984 * Prevent non-root users from setting flags on devices. When
2985 * a device is reused, users can retain ownership of the device
2986 * if they are allowed to set flags and programs assume that
2987 * chown can't fail when done as root.
2989 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2990 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2994 * note: vget is required for any operation that might mod the vnode
2995 * so VINACTIVE is properly cleared.
2997 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2999 vattr.va_flags = flags;
3000 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3007 * chflags(char *path, int flags)
3009 * Change flags of a file given a path name.
3014 sys_chflags(struct chflags_args *uap)
3016 struct nlookupdata nd;
3022 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3024 error = nlookup(&nd);
3026 error = ncp_writechk(&nd.nl_nch);
3028 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3031 error = setfflags(vp, uap->flags);
3039 * lchflags(char *path, int flags)
3041 * Change flags of a file given a path name, but don't follow symlinks.
3046 sys_lchflags(struct lchflags_args *uap)
3048 struct nlookupdata nd;
3054 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3056 error = nlookup(&nd);
3058 error = ncp_writechk(&nd.nl_nch);
3060 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3063 error = setfflags(vp, uap->flags);
3071 * fchflags_args(int fd, int flags)
3073 * Change flags of a file given a file descriptor.
3078 sys_fchflags(struct fchflags_args *uap)
3080 struct thread *td = curthread;
3081 struct proc *p = td->td_proc;
3085 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3088 if (fp->f_nchandle.ncp)
3089 error = ncp_writechk(&fp->f_nchandle);
3091 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3098 setfmode(struct vnode *vp, int mode)
3100 struct thread *td = curthread;
3105 * note: vget is required for any operation that might mod the vnode
3106 * so VINACTIVE is properly cleared.
3108 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3110 vattr.va_mode = mode & ALLPERMS;
3111 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3118 kern_chmod(struct nlookupdata *nd, int mode)
3123 if ((error = nlookup(nd)) != 0)
3125 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3127 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3128 error = setfmode(vp, mode);
3134 * chmod_args(char *path, int mode)
3136 * Change mode of a file given path name.
3141 sys_chmod(struct chmod_args *uap)
3143 struct nlookupdata nd;
3147 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3149 error = kern_chmod(&nd, uap->mode);
3156 * lchmod_args(char *path, int mode)
3158 * Change mode of a file given path name (don't follow links.)
3163 sys_lchmod(struct lchmod_args *uap)
3165 struct nlookupdata nd;
3169 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3171 error = kern_chmod(&nd, uap->mode);
3178 * fchmod_args(int fd, int mode)
3180 * Change mode of a file given a file descriptor.
3185 sys_fchmod(struct fchmod_args *uap)
3187 struct thread *td = curthread;
3188 struct proc *p = td->td_proc;
3192 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3195 if (fp->f_nchandle.ncp)
3196 error = ncp_writechk(&fp->f_nchandle);
3198 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3205 * fchmodat_args(char *path, int mode)
3207 * Change mode of a file pointed to by fd/path.
3212 sys_fchmodat(struct fchmodat_args *uap)
3214 struct nlookupdata nd;
3219 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3221 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3224 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3225 UIO_USERSPACE, flags);
3227 error = kern_chmod(&nd, uap->mode);
3228 nlookup_done_at(&nd, fp);
3234 setfown(struct vnode *vp, uid_t uid, gid_t gid)
3236 struct thread *td = curthread;
3241 * note: vget is required for any operation that might mod the vnode
3242 * so VINACTIVE is properly cleared.
3244 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3248 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3255 kern_chown(struct nlookupdata *nd, int uid, int gid)
3260 if ((error = nlookup(nd)) != 0)
3262 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3264 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3265 error = setfown(vp, uid, gid);
3271 * chown(char *path, int uid, int gid)
3273 * Set ownership given a path name.
3278 sys_chown(struct chown_args *uap)
3280 struct nlookupdata nd;
3284 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3286 error = kern_chown(&nd, uap->uid, uap->gid);
3293 * lchown_args(char *path, int uid, int gid)
3295 * Set ownership given a path name, do not cross symlinks.
3300 sys_lchown(struct lchown_args *uap)
3302 struct nlookupdata nd;
3306 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3308 error = kern_chown(&nd, uap->uid, uap->gid);
3315 * fchown_args(int fd, int uid, int gid)
3317 * Set ownership given a file descriptor.
3322 sys_fchown(struct fchown_args *uap)
3324 struct thread *td = curthread;
3325 struct proc *p = td->td_proc;
3329 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3332 if (fp->f_nchandle.ncp)
3333 error = ncp_writechk(&fp->f_nchandle);
3335 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
3342 * fchownat(int fd, char *path, int uid, int gid, int flags)
3344 * Set ownership of file pointed to by fd/path.
3349 sys_fchownat(struct fchownat_args *uap)
3351 struct nlookupdata nd;
3356 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3358 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3361 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3362 UIO_USERSPACE, flags);
3364 error = kern_chown(&nd, uap->uid, uap->gid);
3365 nlookup_done_at(&nd, fp);
3372 getutimes(const struct timeval *tvp, struct timespec *tsp)
3374 struct timeval tv[2];
3378 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3381 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3382 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3388 setutimes(struct vnode *vp, struct vattr *vattr,
3389 const struct timespec *ts, int nullflag)
3391 struct thread *td = curthread;
3395 vattr->va_atime = ts[0];
3396 vattr->va_mtime = ts[1];
3398 vattr->va_vaflags |= VA_UTIMES_NULL;
3399 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3405 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3407 struct timespec ts[2];
3412 if ((error = getutimes(tptr, ts)) != 0)
3416 * NOTE: utimes() succeeds for the owner even if the file
3417 * is not user-writable.
3419 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3421 if ((error = nlookup(nd)) != 0)
3423 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3425 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3429 * note: vget is required for any operation that might mod the vnode
3430 * so VINACTIVE is properly cleared.
3432 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3433 error = vget(vp, LK_EXCLUSIVE);
3435 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3444 * utimes_args(char *path, struct timeval *tptr)
3446 * Set the access and modification times of a file.
3451 sys_utimes(struct utimes_args *uap)
3453 struct timeval tv[2];
3454 struct nlookupdata nd;
3458 error = copyin(uap->tptr, tv, sizeof(tv));
3463 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3465 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3472 * lutimes_args(char *path, struct timeval *tptr)
3474 * Set the access and modification times of a file.
3479 sys_lutimes(struct lutimes_args *uap)
3481 struct timeval tv[2];
3482 struct nlookupdata nd;
3486 error = copyin(uap->tptr, tv, sizeof(tv));
3491 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3493 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3500 * Set utimes on a file descriptor. The creds used to open the
3501 * file are used to determine whether the operation is allowed
3505 kern_futimes(int fd, struct timeval *tptr)
3507 struct thread *td = curthread;
3508 struct proc *p = td->td_proc;
3509 struct timespec ts[2];
3515 error = getutimes(tptr, ts);
3518 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3520 if (fp->f_nchandle.ncp)
3521 error = ncp_writechk(&fp->f_nchandle);
3524 error = vget(vp, LK_EXCLUSIVE);
3526 error = VOP_GETATTR(vp, &vattr);
3528 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3532 error = setutimes(vp, &vattr, ts,
3543 * futimes_args(int fd, struct timeval *tptr)
3545 * Set the access and modification times of a file.
3550 sys_futimes(struct futimes_args *uap)
3552 struct timeval tv[2];
3556 error = copyin(uap->tptr, tv, sizeof(tv));
3561 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3568 kern_truncate(struct nlookupdata *nd, off_t length)
3576 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3577 if ((error = nlookup(nd)) != 0)
3579 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3581 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3583 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3587 if (vp->v_type == VDIR) {
3589 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3591 vattr.va_size = length;
3592 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3599 * truncate(char *path, int pad, off_t length)
3601 * Truncate a file given its path name.
3606 sys_truncate(struct truncate_args *uap)
3608 struct nlookupdata nd;
3612 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3614 error = kern_truncate(&nd, uap->length);
3621 kern_ftruncate(int fd, off_t length)
3623 struct thread *td = curthread;
3624 struct proc *p = td->td_proc;
3632 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3634 if (fp->f_nchandle.ncp) {
3635 error = ncp_writechk(&fp->f_nchandle);
3639 if ((fp->f_flag & FWRITE) == 0) {
3643 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3647 vp = (struct vnode *)fp->f_data;
3648 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3649 if (vp->v_type == VDIR) {
3651 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3653 vattr.va_size = length;
3654 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3663 * ftruncate_args(int fd, int pad, off_t length)
3665 * Truncate a file given a file descriptor.
3670 sys_ftruncate(struct ftruncate_args *uap)
3675 error = kern_ftruncate(uap->fd, uap->length);
3684 * Sync an open file.
3689 sys_fsync(struct fsync_args *uap)
3691 struct thread *td = curthread;
3692 struct proc *p = td->td_proc;
3698 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3701 vp = (struct vnode *)fp->f_data;
3702 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3703 if ((obj = vp->v_object) != NULL)
3704 vm_object_page_clean(obj, 0, 0, 0);
3705 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3706 if (error == 0 && vp->v_mount)
3707 error = buf_fsync(vp);
3716 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3718 struct nchandle fnchd;
3719 struct nchandle tnchd;
3720 struct namecache *ncp;
3727 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3728 if ((error = nlookup(fromnd)) != 0)
3730 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3732 fnchd.mount = fromnd->nl_nch.mount;
3736 * unlock the source nch so we can lookup the target nch without
3737 * deadlocking. The target may or may not exist so we do not check
3738 * for a target vp like kern_mkdir() and other creation functions do.
3740 * The source and target directories are ref'd and rechecked after
3741 * everything is relocked to determine if the source or target file
3744 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3745 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3746 cache_unlock(&fromnd->nl_nch);
3748 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3749 if ((error = nlookup(tond)) != 0) {
3753 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3757 tnchd.mount = tond->nl_nch.mount;
3761 * If the source and target are the same there is nothing to do
3763 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3770 * Mount points cannot be renamed or overwritten
3772 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3781 * Relock the source ncp. cache_relock() will deal with any
3782 * deadlocks against the already-locked tond and will also
3783 * make sure both are resolved.
3785 * NOTE AFTER RELOCKING: The source or target ncp may have become
3786 * invalid while they were unlocked, nc_vp and nc_mount could
3789 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3790 &tond->nl_nch, tond->nl_cred);
3791 fromnd->nl_flags |= NLC_NCPISLOCKED;
3794 * make sure the parent directories linkages are the same
3796 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3797 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3804 * Both the source and target must be within the same filesystem and
3805 * in the same filesystem as their parent directories within the
3806 * namecache topology.
3808 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3811 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3812 mp != tond->nl_nch.mount) {
3819 * Make sure the mount point is writable
3821 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3828 * If the target exists and either the source or target is a directory,
3829 * then both must be directories.
3831 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3834 if (tond->nl_nch.ncp->nc_vp) {
3835 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3837 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3838 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3840 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3846 * You cannot rename a source into itself or a subdirectory of itself.
3847 * We check this by travsersing the target directory upwards looking
3848 * for a match against the source.
3853 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3854 if (fromnd->nl_nch.ncp == ncp) {
3865 * Even though the namespaces are different, they may still represent
3866 * hardlinks to the same file. The filesystem might have a hard time
3867 * with this so we issue a NREMOVE of the source instead of a NRENAME
3868 * when we detect the situation.
3871 fdvp = fromnd->nl_dvp;
3872 tdvp = tond->nl_dvp;
3873 if (fdvp == NULL || tdvp == NULL) {
3875 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3876 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3879 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3880 fdvp, tdvp, tond->nl_cred);
3887 * rename_args(char *from, char *to)
3889 * Rename files. Source and destination must either both be directories,
3890 * or both not be directories. If target is a directory, it must be empty.
3895 sys_rename(struct rename_args *uap)
3897 struct nlookupdata fromnd, tond;
3901 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3903 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3905 error = kern_rename(&fromnd, &tond);
3906 nlookup_done(&tond);
3908 nlookup_done(&fromnd);
3914 * renameat_args(int oldfd, char *old, int newfd, char *new)
3916 * Rename files using paths relative to the directories associated with
3917 * oldfd and newfd. Source and destination must either both be directories,
3918 * or both not be directories. If target is a directory, it must be empty.
3923 sys_renameat(struct renameat_args *uap)
3925 struct nlookupdata oldnd, newnd;
3926 struct file *oldfp, *newfp;
3930 error = nlookup_init_at(&oldnd, &oldfp, uap->oldfd, uap->old,
3933 error = nlookup_init_at(&newnd, &newfp, uap->newfd, uap->new,
3936 error = kern_rename(&oldnd, &newnd);
3937 nlookup_done_at(&newnd, newfp);
3939 nlookup_done_at(&oldnd, oldfp);
3945 kern_mkdir(struct nlookupdata *nd, int mode)
3947 struct thread *td = curthread;
3948 struct proc *p = td->td_proc;
3954 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3955 if ((error = nlookup(nd)) != 0)
3958 if (nd->nl_nch.ncp->nc_vp)
3960 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3963 vattr.va_type = VDIR;
3964 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3967 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3974 * mkdir_args(char *path, int mode)
3976 * Make a directory file.
3981 sys_mkdir(struct mkdir_args *uap)
3983 struct nlookupdata nd;
3987 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3989 error = kern_mkdir(&nd, uap->mode);
3996 * mkdirat_args(int fd, char *path, mode_t mode)
3998 * Make a directory file. The path is relative to the directory associated
4004 sys_mkdirat(struct mkdirat_args *uap)
4006 struct nlookupdata nd;
4011 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4013 error = kern_mkdir(&nd, uap->mode);
4014 nlookup_done_at(&nd, fp);
4020 kern_rmdir(struct nlookupdata *nd)
4025 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4026 if ((error = nlookup(nd)) != 0)
4030 * Do not allow directories representing mount points to be
4031 * deleted, even if empty. Check write perms on mount point
4032 * in case the vnode is aliased (aka nullfs).
4034 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4036 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4038 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4043 * rmdir_args(char *path)
4045 * Remove a directory file.
4050 sys_rmdir(struct rmdir_args *uap)
4052 struct nlookupdata nd;
4056 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4058 error = kern_rmdir(&nd);
4065 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4066 enum uio_seg direction)
4068 struct thread *td = curthread;
4069 struct proc *p = td->td_proc;
4077 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4079 if ((fp->f_flag & FREAD) == 0) {
4083 vp = (struct vnode *)fp->f_data;
4085 if (vp->v_type != VDIR) {
4089 aiov.iov_base = buf;
4090 aiov.iov_len = count;
4091 auio.uio_iov = &aiov;
4092 auio.uio_iovcnt = 1;
4093 auio.uio_rw = UIO_READ;
4094 auio.uio_segflg = direction;
4096 auio.uio_resid = count;
4097 loff = auio.uio_offset = fp->f_offset;
4098 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4099 fp->f_offset = auio.uio_offset;
4102 if (count == auio.uio_resid) {
4103 if (union_dircheckp) {
4104 error = union_dircheckp(td, &vp, fp);
4111 if ((vp->v_flag & VROOT) &&
4112 (vp->v_mount->mnt_flag & MNT_UNION)) {
4113 struct vnode *tvp = vp;
4114 vp = vp->v_mount->mnt_vnodecovered;
4125 * WARNING! *basep may not be wide enough to accomodate the
4126 * seek offset. XXX should we hack this to return the upper 32 bits
4127 * for offsets greater then 4G?
4130 *basep = (long)loff;
4132 *res = count - auio.uio_resid;
4139 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4141 * Read a block of directory entries in a file system independent format.
4146 sys_getdirentries(struct getdirentries_args *uap)
4152 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4153 &uap->sysmsg_result, UIO_USERSPACE);
4156 if (error == 0 && uap->basep)
4157 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4162 * getdents_args(int fd, char *buf, size_t count)
4167 sys_getdents(struct getdents_args *uap)
4172 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4173 &uap->sysmsg_result, UIO_USERSPACE);
4180 * Set the mode mask for creation of filesystem nodes.
4182 * umask(int newmask)
4187 sys_umask(struct umask_args *uap)
4189 struct thread *td = curthread;
4190 struct proc *p = td->td_proc;
4191 struct filedesc *fdp;
4194 uap->sysmsg_result = fdp->fd_cmask;
4195 fdp->fd_cmask = uap->newmask & ALLPERMS;
4200 * revoke(char *path)
4202 * Void all references to file by ripping underlying filesystem
4208 sys_revoke(struct revoke_args *uap)
4210 struct nlookupdata nd;
4218 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4220 error = nlookup(&nd);
4222 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4223 cred = crhold(nd.nl_cred);
4227 error = VOP_GETATTR(vp, &vattr);
4228 if (error == 0 && cred->cr_uid != vattr.va_uid)
4229 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4230 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4232 error = vrevoke(vp, cred);
4233 } else if (error == 0) {
4234 error = vrevoke(vp, cred);
4245 * getfh_args(char *fname, fhandle_t *fhp)
4247 * Get (NFS) file handle
4249 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4250 * mount. This allows nullfs mounts to be explicitly exported.
4252 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4254 * nullfs mounts of subdirectories are not safe. That is, it will
4255 * work, but you do not really have protection against access to
4256 * the related parent directories.
4261 sys_getfh(struct getfh_args *uap)
4263 struct thread *td = curthread;
4264 struct nlookupdata nd;
4271 * Must be super user
4273 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4278 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4280 error = nlookup(&nd);
4282 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4283 mp = nd.nl_nch.mount;
4286 bzero(&fh, sizeof(fh));
4287 fh.fh_fsid = mp->mnt_stat.f_fsid;
4288 error = VFS_VPTOFH(vp, &fh.fh_fid);
4291 error = copyout(&fh, uap->fhp, sizeof(fh));
4298 * fhopen_args(const struct fhandle *u_fhp, int flags)
4300 * syscall for the rpc.lockd to use to translate a NFS file handle into
4301 * an open descriptor.
4303 * warning: do not remove the priv_check() call or this becomes one giant
4309 sys_fhopen(struct fhopen_args *uap)
4311 struct thread *td = curthread;
4312 struct filedesc *fdp = td->td_proc->p_fd;
4317 struct vattr *vap = &vat;
4319 int fmode, mode, error, type;
4325 * Must be super user
4327 error = priv_check(td, PRIV_ROOT);
4331 fmode = FFLAGS(uap->flags);
4334 * Why not allow a non-read/write open for our lockd?
4336 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4338 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4343 * Find the mount point
4346 mp = vfs_getvfs(&fhp.fh_fsid);
4351 /* now give me my vnode, it gets returned to me locked */
4352 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4356 * from now on we have to make sure not
4357 * to forget about the vnode
4358 * any error that causes an abort must vput(vp)
4359 * just set error = err and 'goto bad;'.
4365 if (vp->v_type == VLNK) {
4369 if (vp->v_type == VSOCK) {
4374 if (fmode & (FWRITE | O_TRUNC)) {
4375 if (vp->v_type == VDIR) {
4379 error = vn_writechk(vp, NULL);
4387 error = VOP_ACCESS(vp, mode, td->td_ucred);
4391 if (fmode & O_TRUNC) {
4392 vn_unlock(vp); /* XXX */
4393 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4396 error = VOP_SETATTR(vp, vap, td->td_ucred);
4402 * VOP_OPEN needs the file pointer so it can potentially override
4405 * WARNING! no f_nchandle will be associated when fhopen()ing a
4408 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4412 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4415 * setting f_ops this way prevents VOP_CLOSE from being
4416 * called or fdrop() releasing the vp from v_data. Since
4417 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4419 fp->f_ops = &badfileops;
4425 * The fp is given its own reference, we still have our ref and lock.
4427 * Assert that all regular files must be created with a VM object.
4429 if (vp->v_type == VREG && vp->v_object == NULL) {
4430 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4435 * The open was successful. Handle any locking requirements.
4437 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4438 lf.l_whence = SEEK_SET;
4441 if (fmode & O_EXLOCK)
4442 lf.l_type = F_WRLCK;
4444 lf.l_type = F_RDLCK;
4445 if (fmode & FNONBLOCK)
4450 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4452 * release our private reference.
4454 fsetfd(fdp, NULL, indx);
4459 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4460 fp->f_flag |= FHASLOCK;
4464 * Clean up. Associate the file pointer with the previously
4465 * reserved descriptor and return it.
4469 fsetfd(fdp, fp, indx);
4471 uap->sysmsg_result = indx;
4475 fsetfd(fdp, NULL, indx);
4485 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4490 sys_fhstat(struct fhstat_args *uap)
4492 struct thread *td = curthread;
4500 * Must be super user
4502 error = priv_check(td, PRIV_ROOT);
4506 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4511 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4514 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4515 error = vn_stat(vp, &sb, td->td_ucred);
4521 error = copyout(&sb, uap->sb, sizeof(sb));
4526 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4531 sys_fhstatfs(struct fhstatfs_args *uap)
4533 struct thread *td = curthread;
4534 struct proc *p = td->td_proc;
4539 char *fullpath, *freepath;
4544 * Must be super user
4546 if ((error = priv_check(td, PRIV_ROOT)))
4549 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4554 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4558 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4563 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4568 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4571 error = mount_path(p, mp, &fullpath, &freepath);
4574 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4575 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4576 kfree(freepath, M_TEMP);
4578 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4579 if (priv_check(td, PRIV_ROOT)) {
4580 bcopy(sp, &sb, sizeof(sb));
4581 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4584 error = copyout(sp, uap->buf, sizeof(*sp));
4591 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4596 sys_fhstatvfs(struct fhstatvfs_args *uap)
4598 struct thread *td = curthread;
4599 struct proc *p = td->td_proc;
4607 * Must be super user
4609 if ((error = priv_check(td, PRIV_ROOT)))
4612 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4617 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4621 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4626 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4629 sp = &mp->mnt_vstat;
4631 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4635 if (mp->mnt_flag & MNT_RDONLY)
4636 sp->f_flag |= ST_RDONLY;
4637 if (mp->mnt_flag & MNT_NOSUID)
4638 sp->f_flag |= ST_NOSUID;
4639 error = copyout(sp, uap->buf, sizeof(*sp));
4647 * Syscall to push extended attribute configuration information into the
4648 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4649 * a command (int cmd), and attribute name and misc data. For now, the
4650 * attribute name is left in userspace for consumption by the VFS_op.
4651 * It will probably be changed to be copied into sysspace by the
4652 * syscall in the future, once issues with various consumers of the
4653 * attribute code have raised their hands.
4655 * Currently this is used only by UFS Extended Attributes.
4660 sys_extattrctl(struct extattrctl_args *uap)
4662 struct nlookupdata nd;
4664 char attrname[EXTATTR_MAXNAMELEN];
4674 if (error == 0 && uap->filename) {
4675 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4678 error = nlookup(&nd);
4680 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4684 if (error == 0 && uap->attrname) {
4685 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4690 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4692 error = nlookup(&nd);
4694 error = ncp_writechk(&nd.nl_nch);
4696 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4698 uap->attrname, nd.nl_cred);
4709 * Syscall to get a named extended attribute on a file or directory.
4714 sys_extattr_set_file(struct extattr_set_file_args *uap)
4716 char attrname[EXTATTR_MAXNAMELEN];
4717 struct nlookupdata nd;
4723 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4730 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4732 error = nlookup(&nd);
4734 error = ncp_writechk(&nd.nl_nch);
4736 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4743 bzero(&auio, sizeof(auio));
4744 aiov.iov_base = uap->data;
4745 aiov.iov_len = uap->nbytes;
4746 auio.uio_iov = &aiov;
4747 auio.uio_iovcnt = 1;
4748 auio.uio_offset = 0;
4749 auio.uio_resid = uap->nbytes;
4750 auio.uio_rw = UIO_WRITE;
4751 auio.uio_td = curthread;
4753 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4763 * Syscall to get a named extended attribute on a file or directory.
4768 sys_extattr_get_file(struct extattr_get_file_args *uap)
4770 char attrname[EXTATTR_MAXNAMELEN];
4771 struct nlookupdata nd;
4777 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4784 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4786 error = nlookup(&nd);
4788 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4795 bzero(&auio, sizeof(auio));
4796 aiov.iov_base = uap->data;
4797 aiov.iov_len = uap->nbytes;
4798 auio.uio_iov = &aiov;
4799 auio.uio_iovcnt = 1;
4800 auio.uio_offset = 0;
4801 auio.uio_resid = uap->nbytes;
4802 auio.uio_rw = UIO_READ;
4803 auio.uio_td = curthread;
4805 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4807 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4816 * Syscall to delete a named extended attribute from a file or directory.
4817 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4822 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4824 char attrname[EXTATTR_MAXNAMELEN];
4825 struct nlookupdata nd;
4829 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4834 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4836 error = nlookup(&nd);
4838 error = ncp_writechk(&nd.nl_nch);
4840 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4842 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4843 attrname, NULL, nd.nl_cred);
4853 * Determine if the mount is visible to the process.
4856 chroot_visible_mnt(struct mount *mp, struct proc *p)
4858 struct nchandle nch;
4861 * Traverse from the mount point upwards. If we hit the process
4862 * root then the mount point is visible to the process.
4864 nch = mp->mnt_ncmountpt;
4866 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4867 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4870 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4871 nch = nch.mount->mnt_ncmounton;
4873 nch.ncp = nch.ncp->nc_parent;
4878 * If the mount point is not visible to the process, but the
4879 * process root is in a subdirectory of the mount, return
4882 if (p->p_fd->fd_nrdir.mount == mp)