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 mount *, 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 | MNT_TRIM |
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 | MNT_TRIM |
364 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
366 * Mount the filesystem.
367 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
370 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
371 if (mp->mnt_flag & MNT_UPDATE) {
372 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
373 mp->mnt_flag &= ~MNT_RDONLY;
374 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
375 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
378 mp->mnt_kern_flag = flag2;
381 vclrflags(vp, VMOUNT);
386 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
388 * Put the new filesystem on the mount list after root. The mount
389 * point gets its own mnt_ncmountpt (unless the VFS already set one
390 * up) which represents the root of the mount. The lookup code
391 * detects the mount point going forward and checks the root of
392 * the mount going backwards.
394 * It is not necessary to invalidate or purge the vnode underneath
395 * because elements under the mount will be given their own glue
399 if (mp->mnt_ncmountpt.ncp == NULL) {
401 * allocate, then unlock, but leave the ref intact
403 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
404 cache_unlock(&mp->mnt_ncmountpt);
406 mp->mnt_ncmounton = nch; /* inherits ref */
407 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
409 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
410 vclrflags(vp, VMOUNT);
411 mountlist_insert(mp, MNTINS_LAST);
413 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
414 error = vfs_allocate_syncvnode(mp);
416 error = VFS_START(mp, 0);
419 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
420 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
421 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
422 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
423 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
424 vclrflags(vp, VMOUNT);
425 mp->mnt_vfc->vfc_refcount--;
437 * Scan all active processes to see if any of them have a current
438 * or root directory onto which the new filesystem has just been
439 * mounted. If so, replace them with the new mount point.
441 * The passed ncp is ref'd and locked (from the mount code) and
442 * must be associated with the vnode representing the root of the
445 struct checkdirs_info {
446 struct nchandle old_nch;
447 struct nchandle new_nch;
448 struct vnode *old_vp;
449 struct vnode *new_vp;
452 static int checkdirs_callback(struct proc *p, void *data);
455 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
457 struct checkdirs_info info;
463 * If the old mount point's vnode has a usecount of 1, it is not
464 * being held as a descriptor anywhere.
466 olddp = old_nch->ncp->nc_vp;
467 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
471 * Force the root vnode of the new mount point to be resolved
472 * so we can update any matching processes.
475 if (VFS_ROOT(mp, &newdp))
476 panic("mount: lost mount");
477 cache_setunresolved(new_nch);
478 cache_setvp(new_nch, newdp);
481 * Special handling of the root node
483 if (rootvnode == olddp) {
485 vfs_cache_setroot(newdp, cache_hold(new_nch));
489 * Pass newdp separately so the callback does not have to access
490 * it via new_nch->ncp->nc_vp.
492 info.old_nch = *old_nch;
493 info.new_nch = *new_nch;
495 allproc_scan(checkdirs_callback, &info);
500 * NOTE: callback is not MP safe because the scanned process's filedesc
501 * structure can be ripped out from under us, amoung other things.
504 checkdirs_callback(struct proc *p, void *data)
506 struct checkdirs_info *info = data;
507 struct filedesc *fdp;
508 struct nchandle ncdrop1;
509 struct nchandle ncdrop2;
510 struct vnode *vprele1;
511 struct vnode *vprele2;
513 if ((fdp = p->p_fd) != NULL) {
514 cache_zero(&ncdrop1);
515 cache_zero(&ncdrop2);
520 * MPUNSAFE - XXX fdp can be pulled out from under a
523 * A shared filedesc is ok, we don't have to copy it
524 * because we are making this change globally.
526 spin_lock(&fdp->fd_spin);
527 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
528 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
529 vprele1 = fdp->fd_cdir;
531 fdp->fd_cdir = info->new_vp;
532 ncdrop1 = fdp->fd_ncdir;
533 cache_copy(&info->new_nch, &fdp->fd_ncdir);
535 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
536 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
537 vprele2 = fdp->fd_rdir;
539 fdp->fd_rdir = info->new_vp;
540 ncdrop2 = fdp->fd_nrdir;
541 cache_copy(&info->new_nch, &fdp->fd_nrdir);
543 spin_unlock(&fdp->fd_spin);
545 cache_drop(&ncdrop1);
547 cache_drop(&ncdrop2);
557 * Unmount a file system.
559 * Note: unmount takes a path to the vnode mounted on as argument,
560 * not special file (as before).
562 * umount_args(char *path, int flags)
567 sys_unmount(struct unmount_args *uap)
569 struct thread *td = curthread;
570 struct proc *p __debugvar = td->td_proc;
571 struct mount *mp = NULL;
572 struct nlookupdata nd;
577 if (td->td_ucred->cr_prison != NULL) {
581 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
584 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
586 error = nlookup(&nd);
590 mp = nd.nl_nch.mount;
593 * Only root, or the user that did the original mount is
594 * permitted to unmount this filesystem.
596 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
597 (error = priv_check(td, PRIV_ROOT)))
601 * Don't allow unmounting the root file system.
603 if (mp->mnt_flag & MNT_ROOTFS) {
609 * Must be the root of the filesystem
611 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
619 error = dounmount(mp, uap->flags);
626 * Do the actual file system unmount.
629 dounmount_interlock(struct mount *mp)
631 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
633 mp->mnt_kern_flag |= MNTK_UNMOUNT;
638 unmount_allproc_cb(struct proc *p, void *arg)
642 if (p->p_textnch.ncp == NULL)
645 mp = (struct mount *)arg;
646 if (p->p_textnch.mount == mp)
647 cache_drop(&p->p_textnch);
653 dounmount(struct mount *mp, int flags)
655 struct namecache *ncp;
664 * Exclusive access for unmounting purposes
666 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
670 * Allow filesystems to detect that a forced unmount is in progress.
672 if (flags & MNT_FORCE)
673 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
674 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
675 error = lockmgr(&mp->mnt_lock, lflags);
677 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
678 if (mp->mnt_kern_flag & MNTK_MWAIT)
683 if (mp->mnt_flag & MNT_EXPUBLIC)
684 vfs_setpublicfs(NULL, NULL, NULL);
686 vfs_msync(mp, MNT_WAIT);
687 async_flag = mp->mnt_flag & MNT_ASYNC;
688 mp->mnt_flag &=~ MNT_ASYNC;
691 * If this filesystem isn't aliasing other filesystems,
692 * try to invalidate any remaining namecache entries and
693 * check the count afterwords.
695 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
696 cache_lock(&mp->mnt_ncmountpt);
697 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
698 cache_unlock(&mp->mnt_ncmountpt);
700 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
701 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
702 allproc_scan(&unmount_allproc_cb, mp);
705 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
706 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
708 if ((flags & MNT_FORCE) == 0) {
710 mount_warning(mp, "Cannot unmount: "
716 mount_warning(mp, "Forced unmount: "
727 * nchandle records ref the mount structure. Expect a count of 1
728 * (our mount->mnt_ncmountpt).
730 if (mp->mnt_refs != 1) {
731 if ((flags & MNT_FORCE) == 0) {
732 mount_warning(mp, "Cannot unmount: "
733 "%d process references still "
734 "present", mp->mnt_refs);
737 mount_warning(mp, "Forced unmount: "
738 "%d process references still "
739 "present", mp->mnt_refs);
745 * Decomission our special mnt_syncer vnode. This also stops
746 * the vnlru code. If we are unable to unmount we recommission
750 if ((vp = mp->mnt_syncer) != NULL) {
751 mp->mnt_syncer = NULL;
754 if (((mp->mnt_flag & MNT_RDONLY) ||
755 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
756 (flags & MNT_FORCE)) {
757 error = VFS_UNMOUNT(mp, flags);
761 if (mp->mnt_syncer == NULL)
762 vfs_allocate_syncvnode(mp);
763 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
764 mp->mnt_flag |= async_flag;
765 lockmgr(&mp->mnt_lock, LK_RELEASE);
766 if (mp->mnt_kern_flag & MNTK_MWAIT)
771 * Clean up any journals still associated with the mount after
772 * filesystem activity has ceased.
774 journal_remove_all_journals(mp,
775 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
777 mountlist_remove(mp);
780 * Remove any installed vnode ops here so the individual VFSs don't
783 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
784 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
785 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
786 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
787 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
789 if (mp->mnt_ncmountpt.ncp != NULL) {
790 nch = mp->mnt_ncmountpt;
791 cache_zero(&mp->mnt_ncmountpt);
792 cache_clrmountpt(&nch);
795 if (mp->mnt_ncmounton.ncp != NULL) {
796 nch = mp->mnt_ncmounton;
797 cache_zero(&mp->mnt_ncmounton);
798 cache_clrmountpt(&nch);
802 mp->mnt_vfc->vfc_refcount--;
803 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
804 panic("unmount: dangling vnode");
805 lockmgr(&mp->mnt_lock, LK_RELEASE);
806 if (mp->mnt_kern_flag & MNTK_MWAIT)
815 mount_warning(struct mount *mp, const char *ctl, ...)
822 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf, 0) == 0) {
823 kprintf("unmount(%s): ", ptr);
828 kprintf("unmount(%p", mp);
829 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
830 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
839 * Shim cache_fullpath() to handle the case where a process is chrooted into
840 * a subdirectory of a mount. In this case if the root mount matches the
841 * process root directory's mount we have to specify the process's root
842 * directory instead of the mount point, because the mount point might
843 * be above the root directory.
847 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
849 struct nchandle *nch;
851 if (p && p->p_fd->fd_nrdir.mount == mp)
852 nch = &p->p_fd->fd_nrdir;
854 nch = &mp->mnt_ncmountpt;
855 return(cache_fullpath(p, nch, rb, fb, 0));
859 * Sync each mounted filesystem.
863 static int syncprt = 0;
864 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
867 static int sync_callback(struct mount *mp, void *data);
870 sys_sync(struct sync_args *uap)
872 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
875 * print out buffer pool stat information on each sync() call.
885 sync_callback(struct mount *mp, void *data __unused)
889 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
890 asyncflag = mp->mnt_flag & MNT_ASYNC;
891 mp->mnt_flag &= ~MNT_ASYNC;
892 vfs_msync(mp, MNT_NOWAIT);
893 VFS_SYNC(mp, MNT_NOWAIT | MNT_LAZY);
894 mp->mnt_flag |= asyncflag;
899 /* XXX PRISON: could be per prison flag */
900 static int prison_quotas;
902 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
906 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
908 * Change filesystem quotas.
913 sys_quotactl(struct quotactl_args *uap)
915 struct nlookupdata nd;
922 if (td->td_ucred->cr_prison && !prison_quotas) {
927 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
929 error = nlookup(&nd);
931 mp = nd.nl_nch.mount;
932 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
933 uap->arg, nd.nl_cred);
942 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
943 * void *buf, int buflen)
945 * This function operates on a mount point and executes the specified
946 * operation using the specified control data, and possibly returns data.
948 * The actual number of bytes stored in the result buffer is returned, 0
949 * if none, otherwise an error is returned.
954 sys_mountctl(struct mountctl_args *uap)
956 struct thread *td = curthread;
957 struct proc *p = td->td_proc;
965 * Sanity and permissions checks. We must be root.
968 if (td->td_ucred->cr_prison != NULL)
970 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
971 (error = priv_check(td, PRIV_ROOT)) != 0)
975 * Argument length checks
977 if (uap->ctllen < 0 || uap->ctllen > 1024)
979 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
981 if (uap->path == NULL)
985 * Allocate the necessary buffers and copyin data
987 path = objcache_get(namei_oc, M_WAITOK);
988 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
993 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
994 error = copyin(uap->ctl, ctl, uap->ctllen);
999 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1002 * Validate the descriptor
1005 fp = holdfp(p->p_fd, uap->fd, -1);
1015 * Execute the internal kernel function and clean up.
1018 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1022 if (error == 0 && uap->sysmsg_result > 0)
1023 error = copyout(buf, uap->buf, uap->sysmsg_result);
1026 objcache_put(namei_oc, path);
1035 * Execute a mount control operation by resolving the path to a mount point
1036 * and calling vop_mountctl().
1038 * Use the mount point from the nch instead of the vnode so nullfs mounts
1039 * can properly spike the VOP.
1042 kern_mountctl(const char *path, int op, struct file *fp,
1043 const void *ctl, int ctllen,
1044 void *buf, int buflen, int *res)
1048 struct nlookupdata nd;
1053 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1055 error = nlookup(&nd);
1057 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1058 mp = nd.nl_nch.mount;
1065 * Must be the root of the filesystem
1067 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1071 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1078 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1080 struct thread *td = curthread;
1081 struct proc *p = td->td_proc;
1084 char *fullpath, *freepath;
1087 if ((error = nlookup(nd)) != 0)
1089 mp = nd->nl_nch.mount;
1091 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1094 error = mount_path(p, mp, &fullpath, &freepath);
1097 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1098 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1099 kfree(freepath, M_TEMP);
1101 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1102 bcopy(sp, buf, sizeof(*buf));
1103 /* Only root should have access to the fsid's. */
1104 if (priv_check(td, PRIV_ROOT))
1105 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1110 * statfs_args(char *path, struct statfs *buf)
1112 * Get filesystem statistics.
1115 sys_statfs(struct statfs_args *uap)
1117 struct nlookupdata nd;
1121 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1123 error = kern_statfs(&nd, &buf);
1126 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1131 kern_fstatfs(int fd, struct statfs *buf)
1133 struct thread *td = curthread;
1134 struct proc *p = td->td_proc;
1138 char *fullpath, *freepath;
1142 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1146 * Try to use mount info from any overlays rather than the
1147 * mount info for the underlying vnode, otherwise we will
1148 * fail when operating on null-mounted paths inside a chroot.
1150 if ((mp = fp->f_nchandle.mount) == NULL)
1151 mp = ((struct vnode *)fp->f_data)->v_mount;
1156 if (fp->f_cred == NULL) {
1161 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1164 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1166 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1167 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1168 kfree(freepath, M_TEMP);
1170 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1171 bcopy(sp, buf, sizeof(*buf));
1173 /* Only root should have access to the fsid's. */
1174 if (priv_check(td, PRIV_ROOT))
1175 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1183 * fstatfs_args(int fd, struct statfs *buf)
1185 * Get filesystem statistics.
1188 sys_fstatfs(struct fstatfs_args *uap)
1193 error = kern_fstatfs(uap->fd, &buf);
1196 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1201 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1207 if ((error = nlookup(nd)) != 0)
1209 mp = nd->nl_nch.mount;
1210 sp = &mp->mnt_vstat;
1211 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1215 if (mp->mnt_flag & MNT_RDONLY)
1216 sp->f_flag |= ST_RDONLY;
1217 if (mp->mnt_flag & MNT_NOSUID)
1218 sp->f_flag |= ST_NOSUID;
1219 bcopy(sp, buf, sizeof(*buf));
1224 * statfs_args(char *path, struct statfs *buf)
1226 * Get filesystem statistics.
1229 sys_statvfs(struct statvfs_args *uap)
1231 struct nlookupdata nd;
1235 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1237 error = kern_statvfs(&nd, &buf);
1240 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1245 kern_fstatvfs(int fd, struct statvfs *buf)
1247 struct thread *td = curthread;
1248 struct proc *p = td->td_proc;
1255 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1257 if ((mp = fp->f_nchandle.mount) == NULL)
1258 mp = ((struct vnode *)fp->f_data)->v_mount;
1263 if (fp->f_cred == NULL) {
1267 sp = &mp->mnt_vstat;
1268 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1272 if (mp->mnt_flag & MNT_RDONLY)
1273 sp->f_flag |= ST_RDONLY;
1274 if (mp->mnt_flag & MNT_NOSUID)
1275 sp->f_flag |= ST_NOSUID;
1277 bcopy(sp, buf, sizeof(*buf));
1285 * fstatfs_args(int fd, struct statfs *buf)
1287 * Get filesystem statistics.
1290 sys_fstatvfs(struct fstatvfs_args *uap)
1295 error = kern_fstatvfs(uap->fd, &buf);
1298 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1303 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1305 * Get statistics on all filesystems.
1308 struct getfsstat_info {
1309 struct statfs *sfsp;
1317 static int getfsstat_callback(struct mount *, void *);
1320 sys_getfsstat(struct getfsstat_args *uap)
1322 struct thread *td = curthread;
1323 struct getfsstat_info info;
1325 bzero(&info, sizeof(info));
1327 info.maxcount = uap->bufsize / sizeof(struct statfs);
1328 info.sfsp = uap->buf;
1330 info.flags = uap->flags;
1333 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1334 if (info.sfsp && info.count > info.maxcount)
1335 uap->sysmsg_result = info.maxcount;
1337 uap->sysmsg_result = info.count;
1338 return (info.error);
1342 getfsstat_callback(struct mount *mp, void *data)
1344 struct getfsstat_info *info = data;
1350 if (info->sfsp && info->count < info->maxcount) {
1351 if (info->td->td_proc &&
1352 !chroot_visible_mnt(mp, info->td->td_proc)) {
1358 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1359 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1360 * overrides MNT_WAIT.
1362 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1363 (info->flags & MNT_WAIT)) &&
1364 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1367 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1369 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1371 info->error = error;
1374 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1375 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1376 kfree(freepath, M_TEMP);
1378 error = copyout(sp, info->sfsp, sizeof(*sp));
1380 info->error = error;
1390 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1391 long bufsize, int flags)
1393 * Get statistics on all filesystems.
1396 struct getvfsstat_info {
1397 struct statfs *sfsp;
1398 struct statvfs *vsfsp;
1406 static int getvfsstat_callback(struct mount *, void *);
1409 sys_getvfsstat(struct getvfsstat_args *uap)
1411 struct thread *td = curthread;
1412 struct getvfsstat_info info;
1414 bzero(&info, sizeof(info));
1416 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1417 info.sfsp = uap->buf;
1418 info.vsfsp = uap->vbuf;
1420 info.flags = uap->flags;
1423 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1424 if (info.vsfsp && info.count > info.maxcount)
1425 uap->sysmsg_result = info.maxcount;
1427 uap->sysmsg_result = info.count;
1428 return (info.error);
1432 getvfsstat_callback(struct mount *mp, void *data)
1434 struct getvfsstat_info *info = data;
1436 struct statvfs *vsp;
1441 if (info->vsfsp && info->count < info->maxcount) {
1442 if (info->td->td_proc &&
1443 !chroot_visible_mnt(mp, info->td->td_proc)) {
1447 vsp = &mp->mnt_vstat;
1450 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1451 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1452 * overrides MNT_WAIT.
1454 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1455 (info->flags & MNT_WAIT)) &&
1456 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1459 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1461 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1462 (info->flags & MNT_WAIT)) &&
1463 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1467 if (mp->mnt_flag & MNT_RDONLY)
1468 vsp->f_flag |= ST_RDONLY;
1469 if (mp->mnt_flag & MNT_NOSUID)
1470 vsp->f_flag |= ST_NOSUID;
1472 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1474 info->error = error;
1477 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1478 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1479 kfree(freepath, M_TEMP);
1481 error = copyout(sp, info->sfsp, sizeof(*sp));
1483 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1485 info->error = error;
1497 * fchdir_args(int fd)
1499 * Change current working directory to a given file descriptor.
1502 sys_fchdir(struct fchdir_args *uap)
1504 struct thread *td = curthread;
1505 struct proc *p = td->td_proc;
1506 struct filedesc *fdp = p->p_fd;
1507 struct vnode *vp, *ovp;
1510 struct nchandle nch, onch, tnch;
1513 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1515 lwkt_gettoken(&p->p_token);
1516 vp = (struct vnode *)fp->f_data;
1518 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1519 if (fp->f_nchandle.ncp == NULL)
1522 error = checkvp_chdir(vp, td);
1527 cache_copy(&fp->f_nchandle, &nch);
1530 * If the ncp has become a mount point, traverse through
1534 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1535 (mp = cache_findmount(&nch)) != NULL
1537 error = nlookup_mp(mp, &tnch);
1539 cache_unlock(&tnch); /* leave ref intact */
1541 vp = tnch.ncp->nc_vp;
1542 error = vget(vp, LK_SHARED);
1543 KKASSERT(error == 0);
1550 onch = fdp->fd_ncdir;
1551 vn_unlock(vp); /* leave ref intact */
1553 fdp->fd_ncdir = nch;
1562 lwkt_reltoken(&p->p_token);
1567 kern_chdir(struct nlookupdata *nd)
1569 struct thread *td = curthread;
1570 struct proc *p = td->td_proc;
1571 struct filedesc *fdp = p->p_fd;
1572 struct vnode *vp, *ovp;
1573 struct nchandle onch;
1576 if ((error = nlookup(nd)) != 0)
1578 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1580 if ((error = vget(vp, LK_SHARED)) != 0)
1583 lwkt_gettoken(&p->p_token);
1584 error = checkvp_chdir(vp, td);
1588 onch = fdp->fd_ncdir;
1589 cache_unlock(&nd->nl_nch); /* leave reference intact */
1590 fdp->fd_ncdir = nd->nl_nch;
1594 cache_zero(&nd->nl_nch);
1598 lwkt_reltoken(&p->p_token);
1603 * chdir_args(char *path)
1605 * Change current working directory (``.'').
1608 sys_chdir(struct chdir_args *uap)
1610 struct nlookupdata nd;
1613 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1615 error = kern_chdir(&nd);
1621 * Helper function for raised chroot(2) security function: Refuse if
1622 * any filedescriptors are open directories.
1625 chroot_refuse_vdir_fds(struct filedesc *fdp)
1632 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1633 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1635 vp = (struct vnode *)fp->f_data;
1636 if (vp->v_type != VDIR) {
1647 * This sysctl determines if we will allow a process to chroot(2) if it
1648 * has a directory open:
1649 * 0: disallowed for all processes.
1650 * 1: allowed for processes that were not already chroot(2)'ed.
1651 * 2: allowed for all processes.
1654 static int chroot_allow_open_directories = 1;
1656 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1657 &chroot_allow_open_directories, 0, "");
1660 * chroot to the specified namecache entry. We obtain the vp from the
1661 * namecache data. The passed ncp must be locked and referenced and will
1662 * remain locked and referenced on return.
1665 kern_chroot(struct nchandle *nch)
1667 struct thread *td = curthread;
1668 struct proc *p = td->td_proc;
1669 struct filedesc *fdp = p->p_fd;
1674 * Only privileged user can chroot
1676 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1681 * Disallow open directory descriptors (fchdir() breakouts).
1683 if (chroot_allow_open_directories == 0 ||
1684 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1685 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1688 if ((vp = nch->ncp->nc_vp) == NULL)
1691 if ((error = vget(vp, LK_SHARED)) != 0)
1695 * Check the validity of vp as a directory to change to and
1696 * associate it with rdir/jdir.
1698 error = checkvp_chdir(vp, td);
1699 vn_unlock(vp); /* leave reference intact */
1701 vrele(fdp->fd_rdir);
1702 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1703 cache_drop(&fdp->fd_nrdir);
1704 cache_copy(nch, &fdp->fd_nrdir);
1705 if (fdp->fd_jdir == NULL) {
1708 cache_copy(nch, &fdp->fd_njdir);
1717 * chroot_args(char *path)
1719 * Change notion of root (``/'') directory.
1722 sys_chroot(struct chroot_args *uap)
1724 struct thread *td __debugvar = curthread;
1725 struct nlookupdata nd;
1728 KKASSERT(td->td_proc);
1729 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1731 nd.nl_flags |= NLC_EXEC;
1732 error = nlookup(&nd);
1734 error = kern_chroot(&nd.nl_nch);
1741 sys_chroot_kernel(struct chroot_kernel_args *uap)
1743 struct thread *td = curthread;
1744 struct nlookupdata nd;
1745 struct nchandle *nch;
1749 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1753 error = nlookup(&nd);
1759 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1763 if ((vp = nch->ncp->nc_vp) == NULL) {
1768 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1771 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1773 vfs_cache_setroot(vp, cache_hold(nch));
1783 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1784 * determine whether it is legal to chdir to the vnode. The vnode's state
1785 * is not changed by this call.
1788 checkvp_chdir(struct vnode *vp, struct thread *td)
1792 if (vp->v_type != VDIR)
1795 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1800 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1802 struct thread *td = curthread;
1803 struct proc *p = td->td_proc;
1804 struct lwp *lp = td->td_lwp;
1805 struct filedesc *fdp = p->p_fd;
1810 int type, indx, error;
1813 if ((oflags & O_ACCMODE) == O_ACCMODE)
1815 flags = FFLAGS(oflags);
1816 error = falloc(lp, &nfp, NULL);
1820 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1823 * XXX p_dupfd is a real mess. It allows a device to return a
1824 * file descriptor to be duplicated rather then doing the open
1830 * Call vn_open() to do the lookup and assign the vnode to the
1831 * file pointer. vn_open() does not change the ref count on fp
1832 * and the vnode, on success, will be inherited by the file pointer
1835 nd->nl_flags |= NLC_LOCKVP;
1836 error = vn_open(nd, fp, flags, cmode);
1840 * handle special fdopen() case. bleh. dupfdopen() is
1841 * responsible for dropping the old contents of ofiles[indx]
1844 * Note that fsetfd() will add a ref to fp which represents
1845 * the fd_files[] assignment. We must still drop our
1848 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1849 if (fdalloc(p, 0, &indx) == 0) {
1850 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1853 fdrop(fp); /* our ref */
1856 fsetfd(fdp, NULL, indx);
1859 fdrop(fp); /* our ref */
1860 if (error == ERESTART)
1866 * ref the vnode for ourselves so it can't be ripped out from under
1867 * is. XXX need an ND flag to request that the vnode be returned
1870 * Reserve a file descriptor but do not assign it until the open
1873 vp = (struct vnode *)fp->f_data;
1875 if ((error = fdalloc(p, 0, &indx)) != 0) {
1882 * If no error occurs the vp will have been assigned to the file
1887 if (flags & (O_EXLOCK | O_SHLOCK)) {
1888 lf.l_whence = SEEK_SET;
1891 if (flags & O_EXLOCK)
1892 lf.l_type = F_WRLCK;
1894 lf.l_type = F_RDLCK;
1895 if (flags & FNONBLOCK)
1900 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1902 * lock request failed. Clean up the reserved
1906 fsetfd(fdp, NULL, indx);
1910 fp->f_flag |= FHASLOCK;
1914 * Assert that all regular file vnodes were created with a object.
1916 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1917 ("open: regular file has no backing object after vn_open"));
1923 * release our private reference, leaving the one associated with the
1924 * descriptor table intact.
1926 fsetfd(fdp, fp, indx);
1933 * open_args(char *path, int flags, int mode)
1935 * Check permissions, allocate an open file structure,
1936 * and call the device open routine if any.
1939 sys_open(struct open_args *uap)
1941 struct nlookupdata nd;
1944 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1946 error = kern_open(&nd, uap->flags,
1947 uap->mode, &uap->sysmsg_result);
1954 * openat_args(int fd, char *path, int flags, int mode)
1957 sys_openat(struct openat_args *uap)
1959 struct nlookupdata nd;
1963 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
1965 error = kern_open(&nd, uap->flags, uap->mode,
1966 &uap->sysmsg_result);
1968 nlookup_done_at(&nd, fp);
1973 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1975 struct thread *td = curthread;
1976 struct proc *p = td->td_proc;
1985 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1986 vattr.va_rmajor = rmajor;
1987 vattr.va_rminor = rminor;
1989 switch (mode & S_IFMT) {
1990 case S_IFMT: /* used by badsect to flag bad sectors */
1991 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
1992 vattr.va_type = VBAD;
1995 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1996 vattr.va_type = VCHR;
1999 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2000 vattr.va_type = VBLK;
2003 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2006 case S_IFDIR: /* special directories support for HAMMER */
2007 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2008 vattr.va_type = VDIR;
2019 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2020 if ((error = nlookup(nd)) != 0)
2022 if (nd->nl_nch.ncp->nc_vp)
2024 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2028 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2029 nd->nl_cred, NAMEI_CREATE);
2032 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2033 &vp, nd->nl_cred, &vattr);
2041 * mknod_args(char *path, int mode, int dev)
2043 * Create a special file.
2046 sys_mknod(struct mknod_args *uap)
2048 struct nlookupdata nd;
2051 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2053 error = kern_mknod(&nd, uap->mode,
2054 umajor(uap->dev), uminor(uap->dev));
2061 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2063 * Create a special file. The path is relative to the directory associated
2067 sys_mknodat(struct mknodat_args *uap)
2069 struct nlookupdata nd;
2073 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2075 error = kern_mknod(&nd, uap->mode,
2076 umajor(uap->dev), uminor(uap->dev));
2078 nlookup_done_at(&nd, fp);
2083 kern_mkfifo(struct nlookupdata *nd, int mode)
2085 struct thread *td = curthread;
2086 struct proc *p = td->td_proc;
2093 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2094 if ((error = nlookup(nd)) != 0)
2096 if (nd->nl_nch.ncp->nc_vp)
2098 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2102 vattr.va_type = VFIFO;
2103 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2105 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2112 * mkfifo_args(char *path, int mode)
2114 * Create a named pipe.
2117 sys_mkfifo(struct mkfifo_args *uap)
2119 struct nlookupdata nd;
2122 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2124 error = kern_mkfifo(&nd, uap->mode);
2130 * mkfifoat_args(int fd, char *path, mode_t mode)
2132 * Create a named pipe. The path is relative to the directory associated
2136 sys_mkfifoat(struct mkfifoat_args *uap)
2138 struct nlookupdata nd;
2142 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2144 error = kern_mkfifo(&nd, uap->mode);
2145 nlookup_done_at(&nd, fp);
2149 static int hardlink_check_uid = 0;
2150 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2151 &hardlink_check_uid, 0,
2152 "Unprivileged processes cannot create hard links to files owned by other "
2154 static int hardlink_check_gid = 0;
2155 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2156 &hardlink_check_gid, 0,
2157 "Unprivileged processes cannot create hard links to files owned by other "
2161 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2167 * Shortcut if disabled
2169 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2173 * Privileged user can always hardlink
2175 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2179 * Otherwise only if the originating file is owned by the
2180 * same user or group. Note that any group is allowed if
2181 * the file is owned by the caller.
2183 error = VOP_GETATTR(vp, &va);
2187 if (hardlink_check_uid) {
2188 if (cred->cr_uid != va.va_uid)
2192 if (hardlink_check_gid) {
2193 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2201 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2203 struct thread *td = curthread;
2208 * Lookup the source and obtained a locked vnode.
2210 * You may only hardlink a file which you have write permission
2211 * on or which you own.
2213 * XXX relookup on vget failure / race ?
2216 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2217 if ((error = nlookup(nd)) != 0)
2219 vp = nd->nl_nch.ncp->nc_vp;
2220 KKASSERT(vp != NULL);
2221 if (vp->v_type == VDIR)
2222 return (EPERM); /* POSIX */
2223 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2225 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2229 * Unlock the source so we can lookup the target without deadlocking
2230 * (XXX vp is locked already, possible other deadlock?). The target
2233 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2234 nd->nl_flags &= ~NLC_NCPISLOCKED;
2235 cache_unlock(&nd->nl_nch);
2238 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2239 if ((error = nlookup(linknd)) != 0) {
2243 if (linknd->nl_nch.ncp->nc_vp) {
2247 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2253 * Finally run the new API VOP.
2255 error = can_hardlink(vp, td, td->td_ucred);
2257 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2258 vp, linknd->nl_cred);
2265 * link_args(char *path, char *link)
2267 * Make a hard file link.
2270 sys_link(struct link_args *uap)
2272 struct nlookupdata nd, linknd;
2275 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2277 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2279 error = kern_link(&nd, &linknd);
2280 nlookup_done(&linknd);
2287 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2295 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2296 if ((error = nlookup(nd)) != 0)
2298 if (nd->nl_nch.ncp->nc_vp)
2300 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2304 vattr.va_mode = mode;
2305 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2312 * symlink(char *path, char *link)
2314 * Make a symbolic link.
2317 sys_symlink(struct symlink_args *uap)
2319 struct thread *td = curthread;
2320 struct nlookupdata nd;
2325 path = objcache_get(namei_oc, M_WAITOK);
2326 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2328 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2330 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2331 error = kern_symlink(&nd, path, mode);
2335 objcache_put(namei_oc, path);
2340 * symlinkat_args(char *path1, int fd, char *path2)
2342 * Make a symbolic link. The path2 argument is relative to the directory
2343 * associated with fd.
2346 sys_symlinkat(struct symlinkat_args *uap)
2348 struct thread *td = curthread;
2349 struct nlookupdata nd;
2355 path1 = objcache_get(namei_oc, M_WAITOK);
2356 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2358 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2361 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2362 error = kern_symlink(&nd, path1, mode);
2364 nlookup_done_at(&nd, fp);
2366 objcache_put(namei_oc, path1);
2371 * undelete_args(char *path)
2373 * Delete a whiteout from the filesystem.
2376 sys_undelete(struct undelete_args *uap)
2378 struct nlookupdata nd;
2381 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2383 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2385 error = nlookup(&nd);
2387 error = ncp_writechk(&nd.nl_nch);
2389 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2397 kern_unlink(struct nlookupdata *nd)
2402 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2403 if ((error = nlookup(nd)) != 0)
2405 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2407 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2412 * unlink_args(char *path)
2414 * Delete a name from the filesystem.
2417 sys_unlink(struct unlink_args *uap)
2419 struct nlookupdata nd;
2422 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2424 error = kern_unlink(&nd);
2431 * unlinkat_args(int fd, char *path, int flags)
2433 * Delete the file or directory entry pointed to by fd/path.
2436 sys_unlinkat(struct unlinkat_args *uap)
2438 struct nlookupdata nd;
2442 if (uap->flags & ~AT_REMOVEDIR)
2445 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2447 if (uap->flags & AT_REMOVEDIR)
2448 error = kern_rmdir(&nd);
2450 error = kern_unlink(&nd);
2452 nlookup_done_at(&nd, fp);
2457 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2459 struct thread *td = curthread;
2460 struct proc *p = td->td_proc;
2467 fp = holdfp(p->p_fd, fd, -1);
2470 if (fp->f_type != DTYPE_VNODE) {
2474 vp = (struct vnode *)fp->f_data;
2478 spin_lock(&fp->f_spin);
2479 new_offset = fp->f_offset + offset;
2483 error = VOP_GETATTR(vp, &vattr);
2484 spin_lock(&fp->f_spin);
2485 new_offset = offset + vattr.va_size;
2488 new_offset = offset;
2490 spin_lock(&fp->f_spin);
2495 spin_lock(&fp->f_spin);
2500 * Validate the seek position. Negative offsets are not allowed
2501 * for regular files or directories.
2503 * Normally we would also not want to allow negative offsets for
2504 * character and block-special devices. However kvm addresses
2505 * on 64 bit architectures might appear to be negative and must
2509 if (new_offset < 0 &&
2510 (vp->v_type == VREG || vp->v_type == VDIR)) {
2513 fp->f_offset = new_offset;
2516 *res = fp->f_offset;
2517 spin_unlock(&fp->f_spin);
2524 * lseek_args(int fd, int pad, off_t offset, int whence)
2526 * Reposition read/write file offset.
2529 sys_lseek(struct lseek_args *uap)
2533 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2534 &uap->sysmsg_offset);
2540 * Check if current process can access given file. amode is a bitmask of *_OK
2541 * access bits. flags is a bitmask of AT_* flags.
2544 kern_access(struct nlookupdata *nd, int amode, int flags)
2549 if (flags & ~AT_EACCESS)
2551 if ((error = nlookup(nd)) != 0)
2554 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2558 /* Flags == 0 means only check for existence. */
2567 if ((mode & VWRITE) == 0 ||
2568 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2569 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2572 * If the file handle is stale we have to re-resolve the
2573 * entry. This is a hack at the moment.
2575 if (error == ESTALE) {
2577 cache_setunresolved(&nd->nl_nch);
2578 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2591 * access_args(char *path, int flags)
2593 * Check access permissions.
2596 sys_access(struct access_args *uap)
2598 struct nlookupdata nd;
2601 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2603 error = kern_access(&nd, uap->flags, 0);
2610 * faccessat_args(int fd, char *path, int amode, int flags)
2612 * Check access permissions.
2615 sys_faccessat(struct faccessat_args *uap)
2617 struct nlookupdata nd;
2621 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2624 error = kern_access(&nd, uap->amode, uap->flags);
2625 nlookup_done_at(&nd, fp);
2631 kern_stat(struct nlookupdata *nd, struct stat *st)
2636 if ((error = nlookup(nd)) != 0)
2639 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2642 if ((error = vget(vp, LK_SHARED)) != 0)
2644 error = vn_stat(vp, st, nd->nl_cred);
2647 * If the file handle is stale we have to re-resolve the entry. This
2648 * is a hack at the moment.
2650 if (error == ESTALE) {
2652 cache_setunresolved(&nd->nl_nch);
2653 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2663 * stat_args(char *path, struct stat *ub)
2665 * Get file status; this version follows links.
2668 sys_stat(struct stat_args *uap)
2670 struct nlookupdata nd;
2674 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2676 error = kern_stat(&nd, &st);
2678 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2685 * lstat_args(char *path, struct stat *ub)
2687 * Get file status; this version does not follow links.
2690 sys_lstat(struct lstat_args *uap)
2692 struct nlookupdata nd;
2696 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2698 error = kern_stat(&nd, &st);
2700 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2707 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2709 * Get status of file pointed to by fd/path.
2712 sys_fstatat(struct fstatat_args *uap)
2714 struct nlookupdata nd;
2720 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2723 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2725 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2726 UIO_USERSPACE, flags);
2728 error = kern_stat(&nd, &st);
2730 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2732 nlookup_done_at(&nd, fp);
2737 * pathconf_Args(char *path, int name)
2739 * Get configurable pathname variables.
2742 sys_pathconf(struct pathconf_args *uap)
2744 struct nlookupdata nd;
2749 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2751 error = nlookup(&nd);
2753 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2756 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2764 * kern_readlink isn't properly split yet. There is a copyin burried
2765 * in VOP_READLINK().
2768 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2770 struct thread *td = curthread;
2776 if ((error = nlookup(nd)) != 0)
2778 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2781 if (vp->v_type != VLNK) {
2784 aiov.iov_base = buf;
2785 aiov.iov_len = count;
2786 auio.uio_iov = &aiov;
2787 auio.uio_iovcnt = 1;
2788 auio.uio_offset = 0;
2789 auio.uio_rw = UIO_READ;
2790 auio.uio_segflg = UIO_USERSPACE;
2792 auio.uio_resid = count;
2793 error = VOP_READLINK(vp, &auio, td->td_ucred);
2796 *res = count - auio.uio_resid;
2801 * readlink_args(char *path, char *buf, int count)
2803 * Return target name of a symbolic link.
2806 sys_readlink(struct readlink_args *uap)
2808 struct nlookupdata nd;
2811 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2813 error = kern_readlink(&nd, uap->buf, uap->count,
2814 &uap->sysmsg_result);
2821 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2823 * Return target name of a symbolic link. The path is relative to the
2824 * directory associated with fd.
2827 sys_readlinkat(struct readlinkat_args *uap)
2829 struct nlookupdata nd;
2833 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2835 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2836 &uap->sysmsg_result);
2838 nlookup_done_at(&nd, fp);
2843 setfflags(struct vnode *vp, int flags)
2845 struct thread *td = curthread;
2850 * Prevent non-root users from setting flags on devices. When
2851 * a device is reused, users can retain ownership of the device
2852 * if they are allowed to set flags and programs assume that
2853 * chown can't fail when done as root.
2855 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2856 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2860 * note: vget is required for any operation that might mod the vnode
2861 * so VINACTIVE is properly cleared.
2863 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2865 vattr.va_flags = flags;
2866 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2873 * chflags(char *path, int flags)
2875 * Change flags of a file given a path name.
2878 sys_chflags(struct chflags_args *uap)
2880 struct nlookupdata nd;
2885 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2887 error = nlookup(&nd);
2889 error = ncp_writechk(&nd.nl_nch);
2891 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2894 error = setfflags(vp, uap->flags);
2901 * lchflags(char *path, int flags)
2903 * Change flags of a file given a path name, but don't follow symlinks.
2906 sys_lchflags(struct lchflags_args *uap)
2908 struct nlookupdata nd;
2913 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2915 error = nlookup(&nd);
2917 error = ncp_writechk(&nd.nl_nch);
2919 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2922 error = setfflags(vp, uap->flags);
2929 * fchflags_args(int fd, int flags)
2931 * Change flags of a file given a file descriptor.
2934 sys_fchflags(struct fchflags_args *uap)
2936 struct thread *td = curthread;
2937 struct proc *p = td->td_proc;
2941 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2943 if (fp->f_nchandle.ncp)
2944 error = ncp_writechk(&fp->f_nchandle);
2946 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2952 setfmode(struct vnode *vp, int mode)
2954 struct thread *td = curthread;
2959 * note: vget is required for any operation that might mod the vnode
2960 * so VINACTIVE is properly cleared.
2962 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2964 vattr.va_mode = mode & ALLPERMS;
2965 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2972 kern_chmod(struct nlookupdata *nd, int mode)
2977 if ((error = nlookup(nd)) != 0)
2979 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2981 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2982 error = setfmode(vp, mode);
2988 * chmod_args(char *path, int mode)
2990 * Change mode of a file given path name.
2993 sys_chmod(struct chmod_args *uap)
2995 struct nlookupdata nd;
2998 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3000 error = kern_chmod(&nd, uap->mode);
3006 * lchmod_args(char *path, int mode)
3008 * Change mode of a file given path name (don't follow links.)
3011 sys_lchmod(struct lchmod_args *uap)
3013 struct nlookupdata nd;
3016 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3018 error = kern_chmod(&nd, uap->mode);
3024 * fchmod_args(int fd, int mode)
3026 * Change mode of a file given a file descriptor.
3029 sys_fchmod(struct fchmod_args *uap)
3031 struct thread *td = curthread;
3032 struct proc *p = td->td_proc;
3036 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3038 if (fp->f_nchandle.ncp)
3039 error = ncp_writechk(&fp->f_nchandle);
3041 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3047 * fchmodat_args(char *path, int mode)
3049 * Change mode of a file pointed to by fd/path.
3052 sys_fchmodat(struct fchmodat_args *uap)
3054 struct nlookupdata nd;
3059 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3061 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3063 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3064 UIO_USERSPACE, flags);
3066 error = kern_chmod(&nd, uap->mode);
3067 nlookup_done_at(&nd, fp);
3072 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3074 struct thread *td = curthread;
3082 * note: vget is required for any operation that might mod the vnode
3083 * so VINACTIVE is properly cleared.
3085 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3086 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3088 o_uid = vattr.va_uid;
3089 o_gid = vattr.va_gid;
3090 size = vattr.va_size;
3095 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3104 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3105 VFS_ACCOUNT(mp, uid, gid, size);
3112 kern_chown(struct nlookupdata *nd, int uid, int gid)
3117 if ((error = nlookup(nd)) != 0)
3119 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3121 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3122 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3128 * chown(char *path, int uid, int gid)
3130 * Set ownership given a path name.
3133 sys_chown(struct chown_args *uap)
3135 struct nlookupdata nd;
3138 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3140 error = kern_chown(&nd, uap->uid, uap->gid);
3146 * lchown_args(char *path, int uid, int gid)
3148 * Set ownership given a path name, do not cross symlinks.
3151 sys_lchown(struct lchown_args *uap)
3153 struct nlookupdata nd;
3156 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3158 error = kern_chown(&nd, uap->uid, uap->gid);
3164 * fchown_args(int fd, int uid, int gid)
3166 * Set ownership given a file descriptor.
3169 sys_fchown(struct fchown_args *uap)
3171 struct thread *td = curthread;
3172 struct proc *p = td->td_proc;
3176 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3178 if (fp->f_nchandle.ncp)
3179 error = ncp_writechk(&fp->f_nchandle);
3181 error = setfown(p->p_fd->fd_ncdir.mount,
3182 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3188 * fchownat(int fd, char *path, int uid, int gid, int flags)
3190 * Set ownership of file pointed to by fd/path.
3193 sys_fchownat(struct fchownat_args *uap)
3195 struct nlookupdata nd;
3200 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3202 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3204 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3205 UIO_USERSPACE, flags);
3207 error = kern_chown(&nd, uap->uid, uap->gid);
3208 nlookup_done_at(&nd, fp);
3214 getutimes(const struct timeval *tvp, struct timespec *tsp)
3216 struct timeval tv[2];
3220 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3223 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3224 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3230 setutimes(struct vnode *vp, struct vattr *vattr,
3231 const struct timespec *ts, int nullflag)
3233 struct thread *td = curthread;
3237 vattr->va_atime = ts[0];
3238 vattr->va_mtime = ts[1];
3240 vattr->va_vaflags |= VA_UTIMES_NULL;
3241 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3247 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3249 struct timespec ts[2];
3254 if ((error = getutimes(tptr, ts)) != 0)
3258 * NOTE: utimes() succeeds for the owner even if the file
3259 * is not user-writable.
3261 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3263 if ((error = nlookup(nd)) != 0)
3265 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3267 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3271 * note: vget is required for any operation that might mod the vnode
3272 * so VINACTIVE is properly cleared.
3274 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3275 error = vget(vp, LK_EXCLUSIVE);
3277 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3286 * utimes_args(char *path, struct timeval *tptr)
3288 * Set the access and modification times of a file.
3291 sys_utimes(struct utimes_args *uap)
3293 struct timeval tv[2];
3294 struct nlookupdata nd;
3298 error = copyin(uap->tptr, tv, sizeof(tv));
3302 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3304 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3310 * lutimes_args(char *path, struct timeval *tptr)
3312 * Set the access and modification times of a file.
3315 sys_lutimes(struct lutimes_args *uap)
3317 struct timeval tv[2];
3318 struct nlookupdata nd;
3322 error = copyin(uap->tptr, tv, sizeof(tv));
3326 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3328 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3334 * Set utimes on a file descriptor. The creds used to open the
3335 * file are used to determine whether the operation is allowed
3339 kern_futimes(int fd, struct timeval *tptr)
3341 struct thread *td = curthread;
3342 struct proc *p = td->td_proc;
3343 struct timespec ts[2];
3349 error = getutimes(tptr, ts);
3352 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3354 if (fp->f_nchandle.ncp)
3355 error = ncp_writechk(&fp->f_nchandle);
3358 error = vget(vp, LK_EXCLUSIVE);
3360 error = VOP_GETATTR(vp, &vattr);
3362 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3366 error = setutimes(vp, &vattr, ts,
3377 * futimes_args(int fd, struct timeval *tptr)
3379 * Set the access and modification times of a file.
3382 sys_futimes(struct futimes_args *uap)
3384 struct timeval tv[2];
3388 error = copyin(uap->tptr, tv, sizeof(tv));
3392 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3398 kern_truncate(struct nlookupdata *nd, off_t length)
3409 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3410 if ((error = nlookup(nd)) != 0)
3412 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3414 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3416 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3420 if (vp->v_type == VDIR) {
3424 error = VOP_GETATTR(vp, &vattr);
3425 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3428 old_size = vattr.va_size;
3430 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3432 vattr.va_size = length;
3433 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3434 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3442 * truncate(char *path, int pad, off_t length)
3444 * Truncate a file given its path name.
3447 sys_truncate(struct truncate_args *uap)
3449 struct nlookupdata nd;
3452 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3454 error = kern_truncate(&nd, uap->length);
3460 kern_ftruncate(int fd, off_t length)
3462 struct thread *td = curthread;
3463 struct proc *p = td->td_proc;
3474 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3476 if (fp->f_nchandle.ncp) {
3477 error = ncp_writechk(&fp->f_nchandle);
3481 if ((fp->f_flag & FWRITE) == 0) {
3485 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3489 vp = (struct vnode *)fp->f_data;
3490 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3491 if (vp->v_type == VDIR) {
3496 error = VOP_GETATTR(vp, &vattr);
3497 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3500 old_size = vattr.va_size;
3502 if ((error = vn_writechk(vp, NULL)) == 0) {
3504 vattr.va_size = length;
3505 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3506 VFS_ACCOUNT(p->p_fd->fd_ncdir.mount, uid, gid, length - old_size);
3515 * ftruncate_args(int fd, int pad, off_t length)
3517 * Truncate a file given a file descriptor.
3520 sys_ftruncate(struct ftruncate_args *uap)
3524 error = kern_ftruncate(uap->fd, uap->length);
3532 * Sync an open file.
3535 sys_fsync(struct fsync_args *uap)
3537 struct thread *td = curthread;
3538 struct proc *p = td->td_proc;
3544 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3546 vp = (struct vnode *)fp->f_data;
3547 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3548 if ((obj = vp->v_object) != NULL)
3549 vm_object_page_clean(obj, 0, 0, 0);
3550 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3551 if (error == 0 && vp->v_mount)
3552 error = buf_fsync(vp);
3560 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3562 struct nchandle fnchd;
3563 struct nchandle tnchd;
3564 struct namecache *ncp;
3571 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3572 if ((error = nlookup(fromnd)) != 0)
3574 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3576 fnchd.mount = fromnd->nl_nch.mount;
3580 * unlock the source nch so we can lookup the target nch without
3581 * deadlocking. The target may or may not exist so we do not check
3582 * for a target vp like kern_mkdir() and other creation functions do.
3584 * The source and target directories are ref'd and rechecked after
3585 * everything is relocked to determine if the source or target file
3588 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3589 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3590 cache_unlock(&fromnd->nl_nch);
3592 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3593 if ((error = nlookup(tond)) != 0) {
3597 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3601 tnchd.mount = tond->nl_nch.mount;
3605 * If the source and target are the same there is nothing to do
3607 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3614 * Mount points cannot be renamed or overwritten
3616 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3625 * Relock the source ncp. cache_relock() will deal with any
3626 * deadlocks against the already-locked tond and will also
3627 * make sure both are resolved.
3629 * NOTE AFTER RELOCKING: The source or target ncp may have become
3630 * invalid while they were unlocked, nc_vp and nc_mount could
3633 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3634 &tond->nl_nch, tond->nl_cred);
3635 fromnd->nl_flags |= NLC_NCPISLOCKED;
3638 * make sure the parent directories linkages are the same
3640 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3641 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3648 * Both the source and target must be within the same filesystem and
3649 * in the same filesystem as their parent directories within the
3650 * namecache topology.
3652 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3655 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3656 mp != tond->nl_nch.mount) {
3663 * Make sure the mount point is writable
3665 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3672 * If the target exists and either the source or target is a directory,
3673 * then both must be directories.
3675 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3678 if (tond->nl_nch.ncp->nc_vp) {
3679 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3681 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3682 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3684 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3690 * You cannot rename a source into itself or a subdirectory of itself.
3691 * We check this by travsersing the target directory upwards looking
3692 * for a match against the source.
3697 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3698 if (fromnd->nl_nch.ncp == ncp) {
3709 * Even though the namespaces are different, they may still represent
3710 * hardlinks to the same file. The filesystem might have a hard time
3711 * with this so we issue a NREMOVE of the source instead of a NRENAME
3712 * when we detect the situation.
3715 fdvp = fromnd->nl_dvp;
3716 tdvp = tond->nl_dvp;
3717 if (fdvp == NULL || tdvp == NULL) {
3719 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3720 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3723 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3724 fdvp, tdvp, tond->nl_cred);
3731 * rename_args(char *from, char *to)
3733 * Rename files. Source and destination must either both be directories,
3734 * or both not be directories. If target is a directory, it must be empty.
3737 sys_rename(struct rename_args *uap)
3739 struct nlookupdata fromnd, tond;
3742 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3744 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3746 error = kern_rename(&fromnd, &tond);
3747 nlookup_done(&tond);
3749 nlookup_done(&fromnd);
3754 * renameat_args(int oldfd, char *old, int newfd, char *new)
3756 * Rename files using paths relative to the directories associated with
3757 * oldfd and newfd. Source and destination must either both be directories,
3758 * or both not be directories. If target is a directory, it must be empty.
3761 sys_renameat(struct renameat_args *uap)
3763 struct nlookupdata oldnd, newnd;
3764 struct file *oldfp, *newfp;
3767 error = nlookup_init_at(&oldnd, &oldfp, uap->oldfd, uap->old,
3770 error = nlookup_init_at(&newnd, &newfp, uap->newfd, uap->new,
3773 error = kern_rename(&oldnd, &newnd);
3774 nlookup_done_at(&newnd, newfp);
3776 nlookup_done_at(&oldnd, oldfp);
3781 kern_mkdir(struct nlookupdata *nd, int mode)
3783 struct thread *td = curthread;
3784 struct proc *p = td->td_proc;
3790 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3791 if ((error = nlookup(nd)) != 0)
3794 if (nd->nl_nch.ncp->nc_vp)
3796 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3799 vattr.va_type = VDIR;
3800 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3803 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3810 * mkdir_args(char *path, int mode)
3812 * Make a directory file.
3815 sys_mkdir(struct mkdir_args *uap)
3817 struct nlookupdata nd;
3820 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3822 error = kern_mkdir(&nd, uap->mode);
3828 * mkdirat_args(int fd, char *path, mode_t mode)
3830 * Make a directory file. The path is relative to the directory associated
3834 sys_mkdirat(struct mkdirat_args *uap)
3836 struct nlookupdata nd;
3840 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3842 error = kern_mkdir(&nd, uap->mode);
3843 nlookup_done_at(&nd, fp);
3848 kern_rmdir(struct nlookupdata *nd)
3853 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3854 if ((error = nlookup(nd)) != 0)
3858 * Do not allow directories representing mount points to be
3859 * deleted, even if empty. Check write perms on mount point
3860 * in case the vnode is aliased (aka nullfs).
3862 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3864 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3866 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3871 * rmdir_args(char *path)
3873 * Remove a directory file.
3876 sys_rmdir(struct rmdir_args *uap)
3878 struct nlookupdata nd;
3881 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3883 error = kern_rmdir(&nd);
3889 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3890 enum uio_seg direction)
3892 struct thread *td = curthread;
3893 struct proc *p = td->td_proc;
3901 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3903 if ((fp->f_flag & FREAD) == 0) {
3907 vp = (struct vnode *)fp->f_data;
3909 if (vp->v_type != VDIR) {
3913 aiov.iov_base = buf;
3914 aiov.iov_len = count;
3915 auio.uio_iov = &aiov;
3916 auio.uio_iovcnt = 1;
3917 auio.uio_rw = UIO_READ;
3918 auio.uio_segflg = direction;
3920 auio.uio_resid = count;
3921 loff = auio.uio_offset = fp->f_offset;
3922 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3923 fp->f_offset = auio.uio_offset;
3926 if (count == auio.uio_resid) {
3927 if (union_dircheckp) {
3928 error = union_dircheckp(td, &vp, fp);
3935 if ((vp->v_flag & VROOT) &&
3936 (vp->v_mount->mnt_flag & MNT_UNION)) {
3937 struct vnode *tvp = vp;
3938 vp = vp->v_mount->mnt_vnodecovered;
3949 * WARNING! *basep may not be wide enough to accomodate the
3950 * seek offset. XXX should we hack this to return the upper 32 bits
3951 * for offsets greater then 4G?
3954 *basep = (long)loff;
3956 *res = count - auio.uio_resid;
3963 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3965 * Read a block of directory entries in a file system independent format.
3968 sys_getdirentries(struct getdirentries_args *uap)
3973 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3974 &uap->sysmsg_result, UIO_USERSPACE);
3976 if (error == 0 && uap->basep)
3977 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3982 * getdents_args(int fd, char *buf, size_t count)
3985 sys_getdents(struct getdents_args *uap)
3989 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3990 &uap->sysmsg_result, UIO_USERSPACE);
3996 * Set the mode mask for creation of filesystem nodes.
3998 * umask(int newmask)
4001 sys_umask(struct umask_args *uap)
4003 struct thread *td = curthread;
4004 struct proc *p = td->td_proc;
4005 struct filedesc *fdp;
4008 uap->sysmsg_result = fdp->fd_cmask;
4009 fdp->fd_cmask = uap->newmask & ALLPERMS;
4014 * revoke(char *path)
4016 * Void all references to file by ripping underlying filesystem
4020 sys_revoke(struct revoke_args *uap)
4022 struct nlookupdata nd;
4029 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4031 error = nlookup(&nd);
4033 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4034 cred = crhold(nd.nl_cred);
4038 error = VOP_GETATTR(vp, &vattr);
4039 if (error == 0 && cred->cr_uid != vattr.va_uid)
4040 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4041 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4043 error = vrevoke(vp, cred);
4044 } else if (error == 0) {
4045 error = vrevoke(vp, cred);
4055 * getfh_args(char *fname, fhandle_t *fhp)
4057 * Get (NFS) file handle
4059 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4060 * mount. This allows nullfs mounts to be explicitly exported.
4062 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4064 * nullfs mounts of subdirectories are not safe. That is, it will
4065 * work, but you do not really have protection against access to
4066 * the related parent directories.
4069 sys_getfh(struct getfh_args *uap)
4071 struct thread *td = curthread;
4072 struct nlookupdata nd;
4079 * Must be super user
4081 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4085 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4087 error = nlookup(&nd);
4089 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4090 mp = nd.nl_nch.mount;
4093 bzero(&fh, sizeof(fh));
4094 fh.fh_fsid = mp->mnt_stat.f_fsid;
4095 error = VFS_VPTOFH(vp, &fh.fh_fid);
4098 error = copyout(&fh, uap->fhp, sizeof(fh));
4104 * fhopen_args(const struct fhandle *u_fhp, int flags)
4106 * syscall for the rpc.lockd to use to translate a NFS file handle into
4107 * an open descriptor.
4109 * warning: do not remove the priv_check() call or this becomes one giant
4113 sys_fhopen(struct fhopen_args *uap)
4115 struct thread *td = curthread;
4116 struct filedesc *fdp = td->td_proc->p_fd;
4121 struct vattr *vap = &vat;
4123 int fmode, mode, error, type;
4129 * Must be super user
4131 error = priv_check(td, PRIV_ROOT);
4135 fmode = FFLAGS(uap->flags);
4138 * Why not allow a non-read/write open for our lockd?
4140 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4142 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4147 * Find the mount point
4149 mp = vfs_getvfs(&fhp.fh_fsid);
4154 /* now give me my vnode, it gets returned to me locked */
4155 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4159 * from now on we have to make sure not
4160 * to forget about the vnode
4161 * any error that causes an abort must vput(vp)
4162 * just set error = err and 'goto bad;'.
4168 if (vp->v_type == VLNK) {
4172 if (vp->v_type == VSOCK) {
4177 if (fmode & (FWRITE | O_TRUNC)) {
4178 if (vp->v_type == VDIR) {
4182 error = vn_writechk(vp, NULL);
4190 error = VOP_ACCESS(vp, mode, td->td_ucred);
4194 if (fmode & O_TRUNC) {
4195 vn_unlock(vp); /* XXX */
4196 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4199 error = VOP_SETATTR(vp, vap, td->td_ucred);
4205 * VOP_OPEN needs the file pointer so it can potentially override
4208 * WARNING! no f_nchandle will be associated when fhopen()ing a
4211 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4215 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4218 * setting f_ops this way prevents VOP_CLOSE from being
4219 * called or fdrop() releasing the vp from v_data. Since
4220 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4222 fp->f_ops = &badfileops;
4228 * The fp is given its own reference, we still have our ref and lock.
4230 * Assert that all regular files must be created with a VM object.
4232 if (vp->v_type == VREG && vp->v_object == NULL) {
4233 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4238 * The open was successful. Handle any locking requirements.
4240 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4241 lf.l_whence = SEEK_SET;
4244 if (fmode & O_EXLOCK)
4245 lf.l_type = F_WRLCK;
4247 lf.l_type = F_RDLCK;
4248 if (fmode & FNONBLOCK)
4253 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4255 * release our private reference.
4257 fsetfd(fdp, NULL, indx);
4262 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4263 fp->f_flag |= FHASLOCK;
4267 * Clean up. Associate the file pointer with the previously
4268 * reserved descriptor and return it.
4271 fsetfd(fdp, fp, indx);
4273 uap->sysmsg_result = indx;
4277 fsetfd(fdp, NULL, indx);
4286 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4289 sys_fhstat(struct fhstat_args *uap)
4291 struct thread *td = curthread;
4299 * Must be super user
4301 error = priv_check(td, PRIV_ROOT);
4305 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4309 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4312 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4313 error = vn_stat(vp, &sb, td->td_ucred);
4318 error = copyout(&sb, uap->sb, sizeof(sb));
4323 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4326 sys_fhstatfs(struct fhstatfs_args *uap)
4328 struct thread *td = curthread;
4329 struct proc *p = td->td_proc;
4334 char *fullpath, *freepath;
4339 * Must be super user
4341 if ((error = priv_check(td, PRIV_ROOT)))
4344 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4347 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4351 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4356 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4361 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4364 error = mount_path(p, mp, &fullpath, &freepath);
4367 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4368 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4369 kfree(freepath, M_TEMP);
4371 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4372 if (priv_check(td, PRIV_ROOT)) {
4373 bcopy(sp, &sb, sizeof(sb));
4374 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4377 error = copyout(sp, uap->buf, sizeof(*sp));
4383 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4386 sys_fhstatvfs(struct fhstatvfs_args *uap)
4388 struct thread *td = curthread;
4389 struct proc *p = td->td_proc;
4397 * Must be super user
4399 if ((error = priv_check(td, PRIV_ROOT)))
4402 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4405 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4409 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4414 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4417 sp = &mp->mnt_vstat;
4419 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4423 if (mp->mnt_flag & MNT_RDONLY)
4424 sp->f_flag |= ST_RDONLY;
4425 if (mp->mnt_flag & MNT_NOSUID)
4426 sp->f_flag |= ST_NOSUID;
4427 error = copyout(sp, uap->buf, sizeof(*sp));
4434 * Syscall to push extended attribute configuration information into the
4435 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4436 * a command (int cmd), and attribute name and misc data. For now, the
4437 * attribute name is left in userspace for consumption by the VFS_op.
4438 * It will probably be changed to be copied into sysspace by the
4439 * syscall in the future, once issues with various consumers of the
4440 * attribute code have raised their hands.
4442 * Currently this is used only by UFS Extended Attributes.
4445 sys_extattrctl(struct extattrctl_args *uap)
4447 struct nlookupdata nd;
4449 char attrname[EXTATTR_MAXNAMELEN];
4457 if (error == 0 && uap->filename) {
4458 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4461 error = nlookup(&nd);
4463 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4467 if (error == 0 && uap->attrname) {
4468 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4473 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4475 error = nlookup(&nd);
4477 error = ncp_writechk(&nd.nl_nch);
4479 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4481 uap->attrname, nd.nl_cred);
4490 * Syscall to get a named extended attribute on a file or directory.
4493 sys_extattr_set_file(struct extattr_set_file_args *uap)
4495 char attrname[EXTATTR_MAXNAMELEN];
4496 struct nlookupdata nd;
4502 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4508 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4510 error = nlookup(&nd);
4512 error = ncp_writechk(&nd.nl_nch);
4514 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4520 bzero(&auio, sizeof(auio));
4521 aiov.iov_base = uap->data;
4522 aiov.iov_len = uap->nbytes;
4523 auio.uio_iov = &aiov;
4524 auio.uio_iovcnt = 1;
4525 auio.uio_offset = 0;
4526 auio.uio_resid = uap->nbytes;
4527 auio.uio_rw = UIO_WRITE;
4528 auio.uio_td = curthread;
4530 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4539 * Syscall to get a named extended attribute on a file or directory.
4542 sys_extattr_get_file(struct extattr_get_file_args *uap)
4544 char attrname[EXTATTR_MAXNAMELEN];
4545 struct nlookupdata nd;
4551 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4557 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4559 error = nlookup(&nd);
4561 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4567 bzero(&auio, sizeof(auio));
4568 aiov.iov_base = uap->data;
4569 aiov.iov_len = uap->nbytes;
4570 auio.uio_iov = &aiov;
4571 auio.uio_iovcnt = 1;
4572 auio.uio_offset = 0;
4573 auio.uio_resid = uap->nbytes;
4574 auio.uio_rw = UIO_READ;
4575 auio.uio_td = curthread;
4577 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4579 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4587 * Syscall to delete a named extended attribute from a file or directory.
4588 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4591 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4593 char attrname[EXTATTR_MAXNAMELEN];
4594 struct nlookupdata nd;
4598 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4602 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4604 error = nlookup(&nd);
4606 error = ncp_writechk(&nd.nl_nch);
4608 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4610 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4611 attrname, NULL, nd.nl_cred);
4620 * Determine if the mount is visible to the process.
4623 chroot_visible_mnt(struct mount *mp, struct proc *p)
4625 struct nchandle nch;
4628 * Traverse from the mount point upwards. If we hit the process
4629 * root then the mount point is visible to the process.
4631 nch = mp->mnt_ncmountpt;
4633 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4634 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4637 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4638 nch = nch.mount->mnt_ncmounton;
4640 nch.ncp = nch.ncp->nc_parent;
4645 * If the mount point is not visible to the process, but the
4646 * process root is in a subdirectory of the mount, return
4649 if (p->p_fd->fd_nrdir.mount == mp)