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];
136 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
140 * Do not allow NFS export by non-root users.
142 if (uap->flags & MNT_EXPORTED) {
143 error = priv_check(td, PRIV_ROOT);
148 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
150 if (priv_check(td, PRIV_ROOT))
151 uap->flags |= MNT_NOSUID | MNT_NODEV;
154 * Lookup the requested path and extract the nch and vnode.
156 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
158 if ((error = nlookup(&nd)) == 0) {
159 if (nd.nl_nch.ncp->nc_vp == NULL)
169 * If the target filesystem is resolved via a nullfs mount, then
170 * nd.nl_nch.mount will be pointing to the nullfs mount structure
171 * instead of the target file system. We need it in case we are
174 nullmp = nd.nl_nch.mount;
177 * Extract the locked+refd ncp and cleanup the nd structure
180 cache_zero(&nd.nl_nch);
183 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
184 (mp = cache_findmount(&nch)) != NULL) {
193 * now we have the locked ref'd nch and unreferenced vnode.
196 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
203 * Extract the file system type. We need to know this early, to take
204 * appropriate actions if we are dealing with a nullfs.
206 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
213 * Now we have an unlocked ref'd nch and a locked ref'd vp
215 if (uap->flags & MNT_UPDATE) {
216 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
223 if (strncmp(fstypename, "null", 5) == 0) {
231 flag2 = mp->mnt_kern_flag;
233 * We only allow the filesystem to be reloaded if it
234 * is currently mounted read-only.
236 if ((uap->flags & MNT_RELOAD) &&
237 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
240 error = EOPNOTSUPP; /* Needs translation */
244 * Only root, or the user that did the original mount is
245 * permitted to update it.
247 if (mp->mnt_stat.f_owner != cred->cr_uid &&
248 (error = priv_check(td, PRIV_ROOT))) {
253 if (vfs_busy(mp, LK_NOWAIT)) {
267 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
268 lwkt_gettoken(&mp->mnt_token);
274 * If the user is not root, ensure that they own the directory
275 * onto which we are attempting to mount.
277 if ((error = VOP_GETATTR(vp, &va)) ||
278 (va.va_uid != cred->cr_uid &&
279 (error = priv_check(td, PRIV_ROOT)))) {
284 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
289 if (vp->v_type != VDIR) {
295 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
301 vfsp = vfsconf_find_by_name(fstypename);
305 /* Only load modules for root (very important!) */
306 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
311 error = linker_load_file(fstypename, &lf);
312 if (error || lf == NULL) {
320 /* lookup again, see if the VFS was loaded */
321 vfsp = vfsconf_find_by_name(fstypename);
324 linker_file_unload(lf);
339 * Allocate and initialize the filesystem.
341 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
343 vfs_busy(mp, LK_NOWAIT);
344 mp->mnt_op = vfsp->vfc_vfsops;
346 vfsp->vfc_refcount++;
347 mp->mnt_stat.f_type = vfsp->vfc_typenum;
348 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
349 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
350 mp->mnt_stat.f_owner = cred->cr_uid;
351 lwkt_gettoken(&mp->mnt_token);
355 * (per-mount token acquired at this point)
357 * Set the mount level flags.
359 if (uap->flags & MNT_RDONLY)
360 mp->mnt_flag |= MNT_RDONLY;
361 else if (mp->mnt_flag & MNT_RDONLY)
362 mp->mnt_kern_flag |= MNTK_WANTRDWR;
363 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
364 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
365 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
366 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
367 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
368 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
369 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
370 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
372 * Mount the filesystem.
373 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
376 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
377 if (mp->mnt_flag & MNT_UPDATE) {
378 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
379 mp->mnt_flag &= ~MNT_RDONLY;
380 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
381 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
384 mp->mnt_kern_flag = flag2;
386 lwkt_reltoken(&mp->mnt_token);
392 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
395 * Put the new filesystem on the mount list after root. The mount
396 * point gets its own mnt_ncmountpt (unless the VFS already set one
397 * up) which represents the root of the mount. The lookup code
398 * detects the mount point going forward and checks the root of
399 * the mount going backwards.
401 * It is not necessary to invalidate or purge the vnode underneath
402 * because elements under the mount will be given their own glue
406 if (mp->mnt_ncmountpt.ncp == NULL) {
408 * allocate, then unlock, but leave the ref intact
410 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
411 cache_unlock(&mp->mnt_ncmountpt);
413 mp->mnt_ncmounton = nch; /* inherits ref */
414 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
415 cache_ismounting(mp);
417 mountlist_insert(mp, MNTINS_LAST);
419 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
420 error = vfs_allocate_syncvnode(mp);
421 lwkt_reltoken(&mp->mnt_token);
423 error = VFS_START(mp, 0);
426 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
427 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
428 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
429 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
430 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
431 mp->mnt_vfc->vfc_refcount--;
432 lwkt_reltoken(&mp->mnt_token);
443 * Scan all active processes to see if any of them have a current
444 * or root directory onto which the new filesystem has just been
445 * mounted. If so, replace them with the new mount point.
447 * Both old_nch and new_nch are ref'd on call but not locked.
448 * new_nch must be temporarily locked so it can be associated with the
449 * vnode representing the root of the mount point.
451 struct checkdirs_info {
452 struct nchandle old_nch;
453 struct nchandle new_nch;
454 struct vnode *old_vp;
455 struct vnode *new_vp;
458 static int checkdirs_callback(struct proc *p, void *data);
461 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
463 struct checkdirs_info info;
469 * If the old mount point's vnode has a usecount of 1, it is not
470 * being held as a descriptor anywhere.
472 olddp = old_nch->ncp->nc_vp;
473 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
477 * Force the root vnode of the new mount point to be resolved
478 * so we can update any matching processes.
481 if (VFS_ROOT(mp, &newdp))
482 panic("mount: lost mount");
485 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
486 cache_setunresolved(new_nch);
487 cache_setvp(new_nch, newdp);
488 cache_unlock(new_nch);
491 * Special handling of the root node
493 if (rootvnode == olddp) {
495 vfs_cache_setroot(newdp, cache_hold(new_nch));
499 * Pass newdp separately so the callback does not have to access
500 * it via new_nch->ncp->nc_vp.
502 info.old_nch = *old_nch;
503 info.new_nch = *new_nch;
505 allproc_scan(checkdirs_callback, &info);
510 * NOTE: callback is not MP safe because the scanned process's filedesc
511 * structure can be ripped out from under us, amoung other things.
514 checkdirs_callback(struct proc *p, void *data)
516 struct checkdirs_info *info = data;
517 struct filedesc *fdp;
518 struct nchandle ncdrop1;
519 struct nchandle ncdrop2;
520 struct vnode *vprele1;
521 struct vnode *vprele2;
523 if ((fdp = p->p_fd) != NULL) {
524 cache_zero(&ncdrop1);
525 cache_zero(&ncdrop2);
530 * MPUNSAFE - XXX fdp can be pulled out from under a
533 * A shared filedesc is ok, we don't have to copy it
534 * because we are making this change globally.
536 spin_lock(&fdp->fd_spin);
537 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
538 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
539 vprele1 = fdp->fd_cdir;
541 fdp->fd_cdir = info->new_vp;
542 ncdrop1 = fdp->fd_ncdir;
543 cache_copy(&info->new_nch, &fdp->fd_ncdir);
545 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
546 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
547 vprele2 = fdp->fd_rdir;
549 fdp->fd_rdir = info->new_vp;
550 ncdrop2 = fdp->fd_nrdir;
551 cache_copy(&info->new_nch, &fdp->fd_nrdir);
553 spin_unlock(&fdp->fd_spin);
555 cache_drop(&ncdrop1);
557 cache_drop(&ncdrop2);
567 * Unmount a file system.
569 * Note: unmount takes a path to the vnode mounted on as argument,
570 * not special file (as before).
572 * umount_args(char *path, int flags)
577 sys_unmount(struct unmount_args *uap)
579 struct thread *td = curthread;
580 struct proc *p __debugvar = td->td_proc;
581 struct mount *mp = NULL;
582 struct nlookupdata nd;
587 if (td->td_ucred->cr_prison != NULL) {
591 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
594 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
596 error = nlookup(&nd);
600 mp = nd.nl_nch.mount;
603 * Only root, or the user that did the original mount is
604 * permitted to unmount this filesystem.
606 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
607 (error = priv_check(td, PRIV_ROOT)))
611 * Don't allow unmounting the root file system.
613 if (mp->mnt_flag & MNT_ROOTFS) {
619 * Must be the root of the filesystem
621 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
629 error = dounmount(mp, uap->flags);
636 * Do the actual file system unmount.
639 dounmount_interlock(struct mount *mp)
641 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
643 mp->mnt_kern_flag |= MNTK_UNMOUNT;
648 unmount_allproc_cb(struct proc *p, void *arg)
652 if (p->p_textnch.ncp == NULL)
655 mp = (struct mount *)arg;
656 if (p->p_textnch.mount == mp)
657 cache_drop(&p->p_textnch);
663 dounmount(struct mount *mp, int flags)
665 struct namecache *ncp;
674 lwkt_gettoken(&mp->mnt_token);
676 * Exclusive access for unmounting purposes
678 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
682 * Allow filesystems to detect that a forced unmount is in progress.
684 if (flags & MNT_FORCE)
685 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
686 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
687 error = lockmgr(&mp->mnt_lock, lflags);
689 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
690 if (mp->mnt_kern_flag & MNTK_MWAIT) {
691 mp->mnt_kern_flag &= ~MNTK_MWAIT;
697 if (mp->mnt_flag & MNT_EXPUBLIC)
698 vfs_setpublicfs(NULL, NULL, NULL);
700 vfs_msync(mp, MNT_WAIT);
701 async_flag = mp->mnt_flag & MNT_ASYNC;
702 mp->mnt_flag &=~ MNT_ASYNC;
705 * If this filesystem isn't aliasing other filesystems,
706 * try to invalidate any remaining namecache entries and
707 * check the count afterwords.
709 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
710 cache_lock(&mp->mnt_ncmountpt);
711 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
712 cache_unlock(&mp->mnt_ncmountpt);
714 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
715 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
716 allproc_scan(&unmount_allproc_cb, mp);
719 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
720 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
722 if ((flags & MNT_FORCE) == 0) {
724 mount_warning(mp, "Cannot unmount: "
730 mount_warning(mp, "Forced unmount: "
741 * Decomission our special mnt_syncer vnode. This also stops
742 * the vnlru code. If we are unable to unmount we recommission
745 * Then sync the filesystem.
747 if ((vp = mp->mnt_syncer) != NULL) {
748 mp->mnt_syncer = NULL;
751 if ((mp->mnt_flag & MNT_RDONLY) == 0)
752 VFS_SYNC(mp, MNT_WAIT);
755 * nchandle records ref the mount structure. Expect a count of 1
756 * (our mount->mnt_ncmountpt).
758 * Scans can get temporary refs on a mountpoint (thought really
759 * heavy duty stuff like cache_findmount() do not).
761 for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
762 cache_unmounting(mp);
763 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
765 if (mp->mnt_refs != 1) {
766 if ((flags & MNT_FORCE) == 0) {
767 mount_warning(mp, "Cannot unmount: "
768 "%d mount refs still present",
772 mount_warning(mp, "Forced unmount: "
773 "%d mount refs still present",
780 * So far so good, sync the filesystem once more and
781 * call the VFS unmount code if the sync succeeds.
784 if (((mp->mnt_flag & MNT_RDONLY) ||
785 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
786 (flags & MNT_FORCE)) {
787 error = VFS_UNMOUNT(mp, flags);
792 * If an error occurred we can still recover, restoring the
793 * syncer vnode and misc flags.
796 if (mp->mnt_syncer == NULL)
797 vfs_allocate_syncvnode(mp);
798 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
799 mp->mnt_flag |= async_flag;
800 lockmgr(&mp->mnt_lock, LK_RELEASE);
801 if (mp->mnt_kern_flag & MNTK_MWAIT) {
802 mp->mnt_kern_flag &= ~MNTK_MWAIT;
808 * Clean up any journals still associated with the mount after
809 * filesystem activity has ceased.
811 journal_remove_all_journals(mp,
812 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
814 mountlist_remove(mp);
817 * Remove any installed vnode ops here so the individual VFSs don't
820 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
821 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
822 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
823 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
824 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
826 if (mp->mnt_ncmountpt.ncp != NULL) {
827 nch = mp->mnt_ncmountpt;
828 cache_zero(&mp->mnt_ncmountpt);
829 cache_clrmountpt(&nch);
832 if (mp->mnt_ncmounton.ncp != NULL) {
833 cache_unmounting(mp);
834 nch = mp->mnt_ncmounton;
835 cache_zero(&mp->mnt_ncmounton);
836 cache_clrmountpt(&nch);
840 mp->mnt_vfc->vfc_refcount--;
841 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
842 panic("unmount: dangling vnode");
843 lockmgr(&mp->mnt_lock, LK_RELEASE);
844 if (mp->mnt_kern_flag & MNTK_MWAIT) {
845 mp->mnt_kern_flag &= ~MNTK_MWAIT;
850 * If we reach here and freeok != 0 we must free the mount.
851 * If refs > 1 cycle and wait, just in case someone tried
852 * to busy the mount after we decided to do the unmount.
855 while (mp->mnt_refs > 1) {
856 cache_unmounting(mp);
858 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
860 lwkt_reltoken(&mp->mnt_token);
867 lwkt_reltoken(&mp->mnt_token);
873 mount_warning(struct mount *mp, const char *ctl, ...)
880 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
881 &ptr, &buf, 0) == 0) {
882 kprintf("unmount(%s): ", ptr);
887 kprintf("unmount(%p", mp);
888 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
889 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
898 * Shim cache_fullpath() to handle the case where a process is chrooted into
899 * a subdirectory of a mount. In this case if the root mount matches the
900 * process root directory's mount we have to specify the process's root
901 * directory instead of the mount point, because the mount point might
902 * be above the root directory.
906 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
908 struct nchandle *nch;
910 if (p && p->p_fd->fd_nrdir.mount == mp)
911 nch = &p->p_fd->fd_nrdir;
913 nch = &mp->mnt_ncmountpt;
914 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
918 * Sync each mounted filesystem.
922 static int syncprt = 0;
923 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
926 static int sync_callback(struct mount *mp, void *data);
929 sys_sync(struct sync_args *uap)
931 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
934 * print out buffer pool stat information on each sync() call.
944 sync_callback(struct mount *mp, void *data __unused)
948 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
949 asyncflag = mp->mnt_flag & MNT_ASYNC;
950 mp->mnt_flag &= ~MNT_ASYNC;
951 vfs_msync(mp, MNT_NOWAIT);
952 VFS_SYNC(mp, MNT_NOWAIT);
953 mp->mnt_flag |= asyncflag;
958 /* XXX PRISON: could be per prison flag */
959 static int prison_quotas;
961 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
965 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
967 * Change filesystem quotas.
972 sys_quotactl(struct quotactl_args *uap)
974 struct nlookupdata nd;
981 if (td->td_ucred->cr_prison && !prison_quotas) {
986 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
988 error = nlookup(&nd);
990 mp = nd.nl_nch.mount;
991 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
992 uap->arg, nd.nl_cred);
1001 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1002 * void *buf, int buflen)
1004 * This function operates on a mount point and executes the specified
1005 * operation using the specified control data, and possibly returns data.
1007 * The actual number of bytes stored in the result buffer is returned, 0
1008 * if none, otherwise an error is returned.
1013 sys_mountctl(struct mountctl_args *uap)
1015 struct thread *td = curthread;
1016 struct proc *p = td->td_proc;
1024 * Sanity and permissions checks. We must be root.
1027 if (td->td_ucred->cr_prison != NULL)
1029 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1030 (error = priv_check(td, PRIV_ROOT)) != 0)
1034 * Argument length checks
1036 if (uap->ctllen < 0 || uap->ctllen > 1024)
1038 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1040 if (uap->path == NULL)
1044 * Allocate the necessary buffers and copyin data
1046 path = objcache_get(namei_oc, M_WAITOK);
1047 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1052 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1053 error = copyin(uap->ctl, ctl, uap->ctllen);
1058 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1061 * Validate the descriptor
1064 fp = holdfp(p->p_fd, uap->fd, -1);
1074 * Execute the internal kernel function and clean up.
1077 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1081 if (error == 0 && uap->sysmsg_result > 0)
1082 error = copyout(buf, uap->buf, uap->sysmsg_result);
1085 objcache_put(namei_oc, path);
1094 * Execute a mount control operation by resolving the path to a mount point
1095 * and calling vop_mountctl().
1097 * Use the mount point from the nch instead of the vnode so nullfs mounts
1098 * can properly spike the VOP.
1101 kern_mountctl(const char *path, int op, struct file *fp,
1102 const void *ctl, int ctllen,
1103 void *buf, int buflen, int *res)
1107 struct nlookupdata nd;
1112 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1114 error = nlookup(&nd);
1116 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1117 mp = nd.nl_nch.mount;
1124 * Must be the root of the filesystem
1126 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1130 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1137 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1139 struct thread *td = curthread;
1140 struct proc *p = td->td_proc;
1143 char *fullpath, *freepath;
1146 if ((error = nlookup(nd)) != 0)
1148 mp = nd->nl_nch.mount;
1150 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1153 error = mount_path(p, mp, &fullpath, &freepath);
1156 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1157 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1158 kfree(freepath, M_TEMP);
1160 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1161 bcopy(sp, buf, sizeof(*buf));
1162 /* Only root should have access to the fsid's. */
1163 if (priv_check(td, PRIV_ROOT))
1164 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1169 * statfs_args(char *path, struct statfs *buf)
1171 * Get filesystem statistics.
1174 sys_statfs(struct statfs_args *uap)
1176 struct nlookupdata nd;
1180 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1182 error = kern_statfs(&nd, &buf);
1185 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1190 kern_fstatfs(int fd, struct statfs *buf)
1192 struct thread *td = curthread;
1193 struct proc *p = td->td_proc;
1197 char *fullpath, *freepath;
1201 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1205 * Try to use mount info from any overlays rather than the
1206 * mount info for the underlying vnode, otherwise we will
1207 * fail when operating on null-mounted paths inside a chroot.
1209 if ((mp = fp->f_nchandle.mount) == NULL)
1210 mp = ((struct vnode *)fp->f_data)->v_mount;
1215 if (fp->f_cred == NULL) {
1220 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1223 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1225 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1226 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1227 kfree(freepath, M_TEMP);
1229 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1230 bcopy(sp, buf, sizeof(*buf));
1232 /* Only root should have access to the fsid's. */
1233 if (priv_check(td, PRIV_ROOT))
1234 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1242 * fstatfs_args(int fd, struct statfs *buf)
1244 * Get filesystem statistics.
1247 sys_fstatfs(struct fstatfs_args *uap)
1252 error = kern_fstatfs(uap->fd, &buf);
1255 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1260 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1266 if ((error = nlookup(nd)) != 0)
1268 mp = nd->nl_nch.mount;
1269 sp = &mp->mnt_vstat;
1270 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1274 if (mp->mnt_flag & MNT_RDONLY)
1275 sp->f_flag |= ST_RDONLY;
1276 if (mp->mnt_flag & MNT_NOSUID)
1277 sp->f_flag |= ST_NOSUID;
1278 bcopy(sp, buf, sizeof(*buf));
1283 * statfs_args(char *path, struct statfs *buf)
1285 * Get filesystem statistics.
1288 sys_statvfs(struct statvfs_args *uap)
1290 struct nlookupdata nd;
1294 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1296 error = kern_statvfs(&nd, &buf);
1299 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1304 kern_fstatvfs(int fd, struct statvfs *buf)
1306 struct thread *td = curthread;
1307 struct proc *p = td->td_proc;
1314 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1316 if ((mp = fp->f_nchandle.mount) == NULL)
1317 mp = ((struct vnode *)fp->f_data)->v_mount;
1322 if (fp->f_cred == NULL) {
1326 sp = &mp->mnt_vstat;
1327 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1331 if (mp->mnt_flag & MNT_RDONLY)
1332 sp->f_flag |= ST_RDONLY;
1333 if (mp->mnt_flag & MNT_NOSUID)
1334 sp->f_flag |= ST_NOSUID;
1336 bcopy(sp, buf, sizeof(*buf));
1344 * fstatfs_args(int fd, struct statfs *buf)
1346 * Get filesystem statistics.
1349 sys_fstatvfs(struct fstatvfs_args *uap)
1354 error = kern_fstatvfs(uap->fd, &buf);
1357 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1362 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1364 * Get statistics on all filesystems.
1367 struct getfsstat_info {
1368 struct statfs *sfsp;
1376 static int getfsstat_callback(struct mount *, void *);
1379 sys_getfsstat(struct getfsstat_args *uap)
1381 struct thread *td = curthread;
1382 struct getfsstat_info info;
1384 bzero(&info, sizeof(info));
1386 info.maxcount = uap->bufsize / sizeof(struct statfs);
1387 info.sfsp = uap->buf;
1389 info.flags = uap->flags;
1392 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1393 if (info.sfsp && info.count > info.maxcount)
1394 uap->sysmsg_result = info.maxcount;
1396 uap->sysmsg_result = info.count;
1397 return (info.error);
1401 getfsstat_callback(struct mount *mp, void *data)
1403 struct getfsstat_info *info = data;
1409 if (info->sfsp && info->count < info->maxcount) {
1410 if (info->td->td_proc &&
1411 !chroot_visible_mnt(mp, info->td->td_proc)) {
1417 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1418 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1419 * overrides MNT_WAIT.
1421 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1422 (info->flags & MNT_WAIT)) &&
1423 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1426 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1428 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1430 info->error = error;
1433 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1434 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1435 kfree(freepath, M_TEMP);
1437 error = copyout(sp, info->sfsp, sizeof(*sp));
1439 info->error = error;
1449 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1450 long bufsize, int flags)
1452 * Get statistics on all filesystems.
1455 struct getvfsstat_info {
1456 struct statfs *sfsp;
1457 struct statvfs *vsfsp;
1465 static int getvfsstat_callback(struct mount *, void *);
1468 sys_getvfsstat(struct getvfsstat_args *uap)
1470 struct thread *td = curthread;
1471 struct getvfsstat_info info;
1473 bzero(&info, sizeof(info));
1475 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1476 info.sfsp = uap->buf;
1477 info.vsfsp = uap->vbuf;
1479 info.flags = uap->flags;
1482 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1483 if (info.vsfsp && info.count > info.maxcount)
1484 uap->sysmsg_result = info.maxcount;
1486 uap->sysmsg_result = info.count;
1487 return (info.error);
1491 getvfsstat_callback(struct mount *mp, void *data)
1493 struct getvfsstat_info *info = data;
1495 struct statvfs *vsp;
1500 if (info->vsfsp && info->count < info->maxcount) {
1501 if (info->td->td_proc &&
1502 !chroot_visible_mnt(mp, info->td->td_proc)) {
1506 vsp = &mp->mnt_vstat;
1509 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1510 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1511 * overrides MNT_WAIT.
1513 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1514 (info->flags & MNT_WAIT)) &&
1515 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1518 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1520 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1521 (info->flags & MNT_WAIT)) &&
1522 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1526 if (mp->mnt_flag & MNT_RDONLY)
1527 vsp->f_flag |= ST_RDONLY;
1528 if (mp->mnt_flag & MNT_NOSUID)
1529 vsp->f_flag |= ST_NOSUID;
1531 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1533 info->error = error;
1536 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1537 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1538 kfree(freepath, M_TEMP);
1540 error = copyout(sp, info->sfsp, sizeof(*sp));
1542 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1544 info->error = error;
1556 * fchdir_args(int fd)
1558 * Change current working directory to a given file descriptor.
1561 sys_fchdir(struct fchdir_args *uap)
1563 struct thread *td = curthread;
1564 struct proc *p = td->td_proc;
1565 struct filedesc *fdp = p->p_fd;
1566 struct vnode *vp, *ovp;
1569 struct nchandle nch, onch, tnch;
1572 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1574 lwkt_gettoken(&p->p_token);
1575 vp = (struct vnode *)fp->f_data;
1577 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1578 if (fp->f_nchandle.ncp == NULL)
1581 error = checkvp_chdir(vp, td);
1586 cache_copy(&fp->f_nchandle, &nch);
1589 * If the ncp has become a mount point, traverse through
1593 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1594 (mp = cache_findmount(&nch)) != NULL
1596 error = nlookup_mp(mp, &tnch);
1598 cache_unlock(&tnch); /* leave ref intact */
1600 vp = tnch.ncp->nc_vp;
1601 error = vget(vp, LK_SHARED);
1602 KKASSERT(error == 0);
1606 cache_dropmount(mp);
1610 onch = fdp->fd_ncdir;
1611 vn_unlock(vp); /* leave ref intact */
1613 fdp->fd_ncdir = nch;
1622 lwkt_reltoken(&p->p_token);
1627 kern_chdir(struct nlookupdata *nd)
1629 struct thread *td = curthread;
1630 struct proc *p = td->td_proc;
1631 struct filedesc *fdp = p->p_fd;
1632 struct vnode *vp, *ovp;
1633 struct nchandle onch;
1636 if ((error = nlookup(nd)) != 0)
1638 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1640 if ((error = vget(vp, LK_SHARED)) != 0)
1643 lwkt_gettoken(&p->p_token);
1644 error = checkvp_chdir(vp, td);
1648 onch = fdp->fd_ncdir;
1649 cache_unlock(&nd->nl_nch); /* leave reference intact */
1650 fdp->fd_ncdir = nd->nl_nch;
1654 cache_zero(&nd->nl_nch);
1658 lwkt_reltoken(&p->p_token);
1663 * chdir_args(char *path)
1665 * Change current working directory (``.'').
1668 sys_chdir(struct chdir_args *uap)
1670 struct nlookupdata nd;
1673 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1675 error = kern_chdir(&nd);
1681 * Helper function for raised chroot(2) security function: Refuse if
1682 * any filedescriptors are open directories.
1685 chroot_refuse_vdir_fds(struct filedesc *fdp)
1692 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1693 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1695 vp = (struct vnode *)fp->f_data;
1696 if (vp->v_type != VDIR) {
1707 * This sysctl determines if we will allow a process to chroot(2) if it
1708 * has a directory open:
1709 * 0: disallowed for all processes.
1710 * 1: allowed for processes that were not already chroot(2)'ed.
1711 * 2: allowed for all processes.
1714 static int chroot_allow_open_directories = 1;
1716 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1717 &chroot_allow_open_directories, 0, "");
1720 * chroot to the specified namecache entry. We obtain the vp from the
1721 * namecache data. The passed ncp must be locked and referenced and will
1722 * remain locked and referenced on return.
1725 kern_chroot(struct nchandle *nch)
1727 struct thread *td = curthread;
1728 struct proc *p = td->td_proc;
1729 struct filedesc *fdp = p->p_fd;
1734 * Only privileged user can chroot
1736 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1741 * Disallow open directory descriptors (fchdir() breakouts).
1743 if (chroot_allow_open_directories == 0 ||
1744 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1745 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1748 if ((vp = nch->ncp->nc_vp) == NULL)
1751 if ((error = vget(vp, LK_SHARED)) != 0)
1755 * Check the validity of vp as a directory to change to and
1756 * associate it with rdir/jdir.
1758 error = checkvp_chdir(vp, td);
1759 vn_unlock(vp); /* leave reference intact */
1761 vrele(fdp->fd_rdir);
1762 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1763 cache_drop(&fdp->fd_nrdir);
1764 cache_copy(nch, &fdp->fd_nrdir);
1765 if (fdp->fd_jdir == NULL) {
1768 cache_copy(nch, &fdp->fd_njdir);
1777 * chroot_args(char *path)
1779 * Change notion of root (``/'') directory.
1782 sys_chroot(struct chroot_args *uap)
1784 struct thread *td __debugvar = curthread;
1785 struct nlookupdata nd;
1788 KKASSERT(td->td_proc);
1789 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1791 nd.nl_flags |= NLC_EXEC;
1792 error = nlookup(&nd);
1794 error = kern_chroot(&nd.nl_nch);
1801 sys_chroot_kernel(struct chroot_kernel_args *uap)
1803 struct thread *td = curthread;
1804 struct nlookupdata nd;
1805 struct nchandle *nch;
1809 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1813 error = nlookup(&nd);
1819 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1823 if ((vp = nch->ncp->nc_vp) == NULL) {
1828 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1831 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1833 vfs_cache_setroot(vp, cache_hold(nch));
1843 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1844 * determine whether it is legal to chdir to the vnode. The vnode's state
1845 * is not changed by this call.
1848 checkvp_chdir(struct vnode *vp, struct thread *td)
1852 if (vp->v_type != VDIR)
1855 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1860 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1862 struct thread *td = curthread;
1863 struct proc *p = td->td_proc;
1864 struct lwp *lp = td->td_lwp;
1865 struct filedesc *fdp = p->p_fd;
1870 int type, indx, error = 0;
1873 if ((oflags & O_ACCMODE) == O_ACCMODE)
1875 flags = FFLAGS(oflags);
1876 error = falloc(lp, &nfp, NULL);
1880 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1883 * XXX p_dupfd is a real mess. It allows a device to return a
1884 * file descriptor to be duplicated rather then doing the open
1890 * Call vn_open() to do the lookup and assign the vnode to the
1891 * file pointer. vn_open() does not change the ref count on fp
1892 * and the vnode, on success, will be inherited by the file pointer
1895 nd->nl_flags |= NLC_LOCKVP;
1896 error = vn_open(nd, fp, flags, cmode);
1900 * handle special fdopen() case. bleh. dupfdopen() is
1901 * responsible for dropping the old contents of ofiles[indx]
1904 * Note that fsetfd() will add a ref to fp which represents
1905 * the fd_files[] assignment. We must still drop our
1908 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1909 if (fdalloc(p, 0, &indx) == 0) {
1910 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1913 fdrop(fp); /* our ref */
1916 fsetfd(fdp, NULL, indx);
1919 fdrop(fp); /* our ref */
1920 if (error == ERESTART)
1926 * ref the vnode for ourselves so it can't be ripped out from under
1927 * is. XXX need an ND flag to request that the vnode be returned
1930 * Reserve a file descriptor but do not assign it until the open
1933 vp = (struct vnode *)fp->f_data;
1935 if ((error = fdalloc(p, 0, &indx)) != 0) {
1942 * If no error occurs the vp will have been assigned to the file
1947 if (flags & (O_EXLOCK | O_SHLOCK)) {
1948 lf.l_whence = SEEK_SET;
1951 if (flags & O_EXLOCK)
1952 lf.l_type = F_WRLCK;
1954 lf.l_type = F_RDLCK;
1955 if (flags & FNONBLOCK)
1960 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1962 * lock request failed. Clean up the reserved
1966 fsetfd(fdp, NULL, indx);
1970 fp->f_flag |= FHASLOCK;
1974 * Assert that all regular file vnodes were created with a object.
1976 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1977 ("open: regular file has no backing object after vn_open"));
1983 * release our private reference, leaving the one associated with the
1984 * descriptor table intact.
1986 fsetfd(fdp, fp, indx);
1989 if (oflags & O_CLOEXEC)
1990 error = fsetfdflags(fdp, *res, UF_EXCLOSE);
1995 * open_args(char *path, int flags, int mode)
1997 * Check permissions, allocate an open file structure,
1998 * and call the device open routine if any.
2001 sys_open(struct open_args *uap)
2003 struct nlookupdata nd;
2006 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2008 error = kern_open(&nd, uap->flags,
2009 uap->mode, &uap->sysmsg_result);
2016 * openat_args(int fd, char *path, int flags, int mode)
2019 sys_openat(struct openat_args *uap)
2021 struct nlookupdata nd;
2025 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2027 error = kern_open(&nd, uap->flags, uap->mode,
2028 &uap->sysmsg_result);
2030 nlookup_done_at(&nd, fp);
2035 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2037 struct thread *td = curthread;
2038 struct proc *p = td->td_proc;
2047 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2048 vattr.va_rmajor = rmajor;
2049 vattr.va_rminor = rminor;
2051 switch (mode & S_IFMT) {
2052 case S_IFMT: /* used by badsect to flag bad sectors */
2053 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2054 vattr.va_type = VBAD;
2057 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2058 vattr.va_type = VCHR;
2061 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2062 vattr.va_type = VBLK;
2065 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2068 case S_IFDIR: /* special directories support for HAMMER */
2069 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2070 vattr.va_type = VDIR;
2081 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2082 if ((error = nlookup(nd)) != 0)
2084 if (nd->nl_nch.ncp->nc_vp)
2086 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2090 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2091 nd->nl_cred, NAMEI_CREATE);
2094 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2095 &vp, nd->nl_cred, &vattr);
2103 * mknod_args(char *path, int mode, int dev)
2105 * Create a special file.
2108 sys_mknod(struct mknod_args *uap)
2110 struct nlookupdata nd;
2113 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2115 error = kern_mknod(&nd, uap->mode,
2116 umajor(uap->dev), uminor(uap->dev));
2123 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2125 * Create a special file. The path is relative to the directory associated
2129 sys_mknodat(struct mknodat_args *uap)
2131 struct nlookupdata nd;
2135 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2137 error = kern_mknod(&nd, uap->mode,
2138 umajor(uap->dev), uminor(uap->dev));
2140 nlookup_done_at(&nd, fp);
2145 kern_mkfifo(struct nlookupdata *nd, int mode)
2147 struct thread *td = curthread;
2148 struct proc *p = td->td_proc;
2155 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2156 if ((error = nlookup(nd)) != 0)
2158 if (nd->nl_nch.ncp->nc_vp)
2160 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2164 vattr.va_type = VFIFO;
2165 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2167 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2174 * mkfifo_args(char *path, int mode)
2176 * Create a named pipe.
2179 sys_mkfifo(struct mkfifo_args *uap)
2181 struct nlookupdata nd;
2184 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2186 error = kern_mkfifo(&nd, uap->mode);
2192 * mkfifoat_args(int fd, char *path, mode_t mode)
2194 * Create a named pipe. The path is relative to the directory associated
2198 sys_mkfifoat(struct mkfifoat_args *uap)
2200 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);
2211 static int hardlink_check_uid = 0;
2212 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2213 &hardlink_check_uid, 0,
2214 "Unprivileged processes cannot create hard links to files owned by other "
2216 static int hardlink_check_gid = 0;
2217 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2218 &hardlink_check_gid, 0,
2219 "Unprivileged processes cannot create hard links to files owned by other "
2223 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2229 * Shortcut if disabled
2231 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2235 * Privileged user can always hardlink
2237 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2241 * Otherwise only if the originating file is owned by the
2242 * same user or group. Note that any group is allowed if
2243 * the file is owned by the caller.
2245 error = VOP_GETATTR(vp, &va);
2249 if (hardlink_check_uid) {
2250 if (cred->cr_uid != va.va_uid)
2254 if (hardlink_check_gid) {
2255 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2263 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2265 struct thread *td = curthread;
2270 * Lookup the source and obtained a locked vnode.
2272 * You may only hardlink a file which you have write permission
2273 * on or which you own.
2275 * XXX relookup on vget failure / race ?
2278 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2279 if ((error = nlookup(nd)) != 0)
2281 vp = nd->nl_nch.ncp->nc_vp;
2282 KKASSERT(vp != NULL);
2283 if (vp->v_type == VDIR)
2284 return (EPERM); /* POSIX */
2285 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2287 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2291 * Unlock the source so we can lookup the target without deadlocking
2292 * (XXX vp is locked already, possible other deadlock?). The target
2295 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2296 nd->nl_flags &= ~NLC_NCPISLOCKED;
2297 cache_unlock(&nd->nl_nch);
2300 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2301 if ((error = nlookup(linknd)) != 0) {
2305 if (linknd->nl_nch.ncp->nc_vp) {
2309 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2315 * Finally run the new API VOP.
2317 error = can_hardlink(vp, td, td->td_ucred);
2319 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2320 vp, linknd->nl_cred);
2327 * link_args(char *path, char *link)
2329 * Make a hard file link.
2332 sys_link(struct link_args *uap)
2334 struct nlookupdata nd, linknd;
2337 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2339 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2341 error = kern_link(&nd, &linknd);
2342 nlookup_done(&linknd);
2349 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2351 * Make a hard file link. The path1 argument is relative to the directory
2352 * associated with fd1, and similarly the path2 argument is relative to
2353 * the directory associated with fd2.
2356 sys_linkat(struct linkat_args *uap)
2358 struct nlookupdata nd, linknd;
2359 struct file *fp1, *fp2;
2362 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2363 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2365 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2366 uap->path2, UIO_USERSPACE, 0);
2368 error = kern_link(&nd, &linknd);
2369 nlookup_done_at(&linknd, fp2);
2371 nlookup_done_at(&nd, fp1);
2376 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2384 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2385 if ((error = nlookup(nd)) != 0)
2387 if (nd->nl_nch.ncp->nc_vp)
2389 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2393 vattr.va_mode = mode;
2394 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2401 * symlink(char *path, char *link)
2403 * Make a symbolic link.
2406 sys_symlink(struct symlink_args *uap)
2408 struct thread *td = curthread;
2409 struct nlookupdata nd;
2414 path = objcache_get(namei_oc, M_WAITOK);
2415 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2417 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2419 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2420 error = kern_symlink(&nd, path, mode);
2424 objcache_put(namei_oc, path);
2429 * symlinkat_args(char *path1, int fd, char *path2)
2431 * Make a symbolic link. The path2 argument is relative to the directory
2432 * associated with fd.
2435 sys_symlinkat(struct symlinkat_args *uap)
2437 struct thread *td = curthread;
2438 struct nlookupdata nd;
2444 path1 = objcache_get(namei_oc, M_WAITOK);
2445 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2447 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2450 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2451 error = kern_symlink(&nd, path1, mode);
2453 nlookup_done_at(&nd, fp);
2455 objcache_put(namei_oc, path1);
2460 * undelete_args(char *path)
2462 * Delete a whiteout from the filesystem.
2465 sys_undelete(struct undelete_args *uap)
2467 struct nlookupdata nd;
2470 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2472 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2474 error = nlookup(&nd);
2476 error = ncp_writechk(&nd.nl_nch);
2478 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2486 kern_unlink(struct nlookupdata *nd)
2491 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2492 if ((error = nlookup(nd)) != 0)
2494 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2496 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2501 * unlink_args(char *path)
2503 * Delete a name from the filesystem.
2506 sys_unlink(struct unlink_args *uap)
2508 struct nlookupdata nd;
2511 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2513 error = kern_unlink(&nd);
2520 * unlinkat_args(int fd, char *path, int flags)
2522 * Delete the file or directory entry pointed to by fd/path.
2525 sys_unlinkat(struct unlinkat_args *uap)
2527 struct nlookupdata nd;
2531 if (uap->flags & ~AT_REMOVEDIR)
2534 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2536 if (uap->flags & AT_REMOVEDIR)
2537 error = kern_rmdir(&nd);
2539 error = kern_unlink(&nd);
2541 nlookup_done_at(&nd, fp);
2546 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2548 struct thread *td = curthread;
2549 struct proc *p = td->td_proc;
2556 fp = holdfp(p->p_fd, fd, -1);
2559 if (fp->f_type != DTYPE_VNODE) {
2563 vp = (struct vnode *)fp->f_data;
2567 spin_lock(&fp->f_spin);
2568 new_offset = fp->f_offset + offset;
2572 error = VOP_GETATTR(vp, &vattr);
2573 spin_lock(&fp->f_spin);
2574 new_offset = offset + vattr.va_size;
2577 new_offset = offset;
2579 spin_lock(&fp->f_spin);
2584 spin_lock(&fp->f_spin);
2589 * Validate the seek position. Negative offsets are not allowed
2590 * for regular files or directories.
2592 * Normally we would also not want to allow negative offsets for
2593 * character and block-special devices. However kvm addresses
2594 * on 64 bit architectures might appear to be negative and must
2598 if (new_offset < 0 &&
2599 (vp->v_type == VREG || vp->v_type == VDIR)) {
2602 fp->f_offset = new_offset;
2605 *res = fp->f_offset;
2606 spin_unlock(&fp->f_spin);
2613 * lseek_args(int fd, int pad, off_t offset, int whence)
2615 * Reposition read/write file offset.
2618 sys_lseek(struct lseek_args *uap)
2622 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2623 &uap->sysmsg_offset);
2629 * Check if current process can access given file. amode is a bitmask of *_OK
2630 * access bits. flags is a bitmask of AT_* flags.
2633 kern_access(struct nlookupdata *nd, int amode, int flags)
2638 if (flags & ~AT_EACCESS)
2640 if ((error = nlookup(nd)) != 0)
2643 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2647 /* Flags == 0 means only check for existence. */
2656 if ((mode & VWRITE) == 0 ||
2657 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2658 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2661 * If the file handle is stale we have to re-resolve the
2662 * entry. This is a hack at the moment.
2664 if (error == ESTALE) {
2666 cache_setunresolved(&nd->nl_nch);
2667 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2680 * access_args(char *path, int flags)
2682 * Check access permissions.
2685 sys_access(struct access_args *uap)
2687 struct nlookupdata nd;
2690 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2692 error = kern_access(&nd, uap->flags, 0);
2699 * eaccess_args(char *path, int flags)
2701 * Check access permissions.
2704 sys_eaccess(struct eaccess_args *uap)
2706 struct nlookupdata nd;
2709 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2711 error = kern_access(&nd, uap->flags, AT_EACCESS);
2718 * faccessat_args(int fd, char *path, int amode, int flags)
2720 * Check access permissions.
2723 sys_faccessat(struct faccessat_args *uap)
2725 struct nlookupdata nd;
2729 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2732 error = kern_access(&nd, uap->amode, uap->flags);
2733 nlookup_done_at(&nd, fp);
2739 kern_stat(struct nlookupdata *nd, struct stat *st)
2744 if ((error = nlookup(nd)) != 0)
2747 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2750 if ((error = vget(vp, LK_SHARED)) != 0)
2752 error = vn_stat(vp, st, nd->nl_cred);
2755 * If the file handle is stale we have to re-resolve the entry. This
2756 * is a hack at the moment.
2758 if (error == ESTALE) {
2760 cache_setunresolved(&nd->nl_nch);
2761 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2771 * stat_args(char *path, struct stat *ub)
2773 * Get file status; this version follows links.
2776 sys_stat(struct stat_args *uap)
2778 struct nlookupdata nd;
2782 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2784 error = kern_stat(&nd, &st);
2786 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2793 * lstat_args(char *path, struct stat *ub)
2795 * Get file status; this version does not follow links.
2798 sys_lstat(struct lstat_args *uap)
2800 struct nlookupdata nd;
2804 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2806 error = kern_stat(&nd, &st);
2808 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2815 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2817 * Get status of file pointed to by fd/path.
2820 sys_fstatat(struct fstatat_args *uap)
2822 struct nlookupdata nd;
2828 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2831 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2833 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2834 UIO_USERSPACE, flags);
2836 error = kern_stat(&nd, &st);
2838 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2840 nlookup_done_at(&nd, fp);
2845 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
2847 struct nlookupdata nd;
2852 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
2854 error = nlookup(&nd);
2856 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2859 error = VOP_PATHCONF(vp, name, sysmsg_regp);
2866 * pathconf_Args(char *path, int name)
2868 * Get configurable pathname variables.
2871 sys_pathconf(struct pathconf_args *uap)
2873 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
2878 * lpathconf_Args(char *path, int name)
2880 * Get configurable pathname variables, but don't follow symlinks.
2883 sys_lpathconf(struct lpathconf_args *uap)
2885 return (kern_pathconf(uap->path, uap->name, 0, &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.
2932 sys_readlink(struct readlink_args *uap)
2934 struct nlookupdata nd;
2937 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2939 error = kern_readlink(&nd, uap->buf, uap->count,
2940 &uap->sysmsg_result);
2947 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2949 * Return target name of a symbolic link. The path is relative to the
2950 * directory associated with fd.
2953 sys_readlinkat(struct readlinkat_args *uap)
2955 struct nlookupdata nd;
2959 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2961 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2962 &uap->sysmsg_result);
2964 nlookup_done_at(&nd, fp);
2969 setfflags(struct vnode *vp, int flags)
2971 struct thread *td = curthread;
2976 * Prevent non-root users from setting flags on devices. When
2977 * a device is reused, users can retain ownership of the device
2978 * if they are allowed to set flags and programs assume that
2979 * chown can't fail when done as root.
2981 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2982 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2986 * note: vget is required for any operation that might mod the vnode
2987 * so VINACTIVE is properly cleared.
2989 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2991 vattr.va_flags = flags;
2992 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2999 * chflags(char *path, int flags)
3001 * Change flags of a file given a path name.
3004 sys_chflags(struct chflags_args *uap)
3006 struct nlookupdata nd;
3011 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3013 error = nlookup(&nd);
3015 error = ncp_writechk(&nd.nl_nch);
3017 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3020 error = setfflags(vp, uap->flags);
3027 * lchflags(char *path, int flags)
3029 * Change flags of a file given a path name, but don't follow symlinks.
3032 sys_lchflags(struct lchflags_args *uap)
3034 struct nlookupdata nd;
3039 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3041 error = nlookup(&nd);
3043 error = ncp_writechk(&nd.nl_nch);
3045 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3048 error = setfflags(vp, uap->flags);
3055 * fchflags_args(int fd, int flags)
3057 * Change flags of a file given a file descriptor.
3060 sys_fchflags(struct fchflags_args *uap)
3062 struct thread *td = curthread;
3063 struct proc *p = td->td_proc;
3067 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3069 if (fp->f_nchandle.ncp)
3070 error = ncp_writechk(&fp->f_nchandle);
3072 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3078 setfmode(struct vnode *vp, int mode)
3080 struct thread *td = curthread;
3085 * note: vget is required for any operation that might mod the vnode
3086 * so VINACTIVE is properly cleared.
3088 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3090 vattr.va_mode = mode & ALLPERMS;
3091 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3098 kern_chmod(struct nlookupdata *nd, int mode)
3103 if ((error = nlookup(nd)) != 0)
3105 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3107 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3108 error = setfmode(vp, mode);
3114 * chmod_args(char *path, int mode)
3116 * Change mode of a file given path name.
3119 sys_chmod(struct chmod_args *uap)
3121 struct nlookupdata nd;
3124 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3126 error = kern_chmod(&nd, uap->mode);
3132 * lchmod_args(char *path, int mode)
3134 * Change mode of a file given path name (don't follow links.)
3137 sys_lchmod(struct lchmod_args *uap)
3139 struct nlookupdata nd;
3142 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3144 error = kern_chmod(&nd, uap->mode);
3150 * fchmod_args(int fd, int mode)
3152 * Change mode of a file given a file descriptor.
3155 sys_fchmod(struct fchmod_args *uap)
3157 struct thread *td = curthread;
3158 struct proc *p = td->td_proc;
3162 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3164 if (fp->f_nchandle.ncp)
3165 error = ncp_writechk(&fp->f_nchandle);
3167 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3173 * fchmodat_args(char *path, int mode)
3175 * Change mode of a file pointed to by fd/path.
3178 sys_fchmodat(struct fchmodat_args *uap)
3180 struct nlookupdata nd;
3185 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3187 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3189 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3190 UIO_USERSPACE, flags);
3192 error = kern_chmod(&nd, uap->mode);
3193 nlookup_done_at(&nd, fp);
3198 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3200 struct thread *td = curthread;
3208 * note: vget is required for any operation that might mod the vnode
3209 * so VINACTIVE is properly cleared.
3211 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3212 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3214 o_uid = vattr.va_uid;
3215 o_gid = vattr.va_gid;
3216 size = vattr.va_size;
3221 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3230 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3231 VFS_ACCOUNT(mp, uid, gid, size);
3238 kern_chown(struct nlookupdata *nd, int uid, int gid)
3243 if ((error = nlookup(nd)) != 0)
3245 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3247 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3248 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3254 * chown(char *path, int uid, int gid)
3256 * Set ownership given a path name.
3259 sys_chown(struct chown_args *uap)
3261 struct nlookupdata nd;
3264 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3266 error = kern_chown(&nd, uap->uid, uap->gid);
3272 * lchown_args(char *path, int uid, int gid)
3274 * Set ownership given a path name, do not cross symlinks.
3277 sys_lchown(struct lchown_args *uap)
3279 struct nlookupdata nd;
3282 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3284 error = kern_chown(&nd, uap->uid, uap->gid);
3290 * fchown_args(int fd, int uid, int gid)
3292 * Set ownership given a file descriptor.
3295 sys_fchown(struct fchown_args *uap)
3297 struct thread *td = curthread;
3298 struct proc *p = td->td_proc;
3302 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3304 if (fp->f_nchandle.ncp)
3305 error = ncp_writechk(&fp->f_nchandle);
3307 error = setfown(p->p_fd->fd_ncdir.mount,
3308 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3314 * fchownat(int fd, char *path, int uid, int gid, int flags)
3316 * Set ownership of file pointed to by fd/path.
3319 sys_fchownat(struct fchownat_args *uap)
3321 struct nlookupdata nd;
3326 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3328 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3330 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3331 UIO_USERSPACE, flags);
3333 error = kern_chown(&nd, uap->uid, uap->gid);
3334 nlookup_done_at(&nd, fp);
3340 getutimes(const struct timeval *tvp, struct timespec *tsp)
3342 struct timeval tv[2];
3346 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3349 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3350 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3356 setutimes(struct vnode *vp, struct vattr *vattr,
3357 const struct timespec *ts, int nullflag)
3359 struct thread *td = curthread;
3363 vattr->va_atime = ts[0];
3364 vattr->va_mtime = ts[1];
3366 vattr->va_vaflags |= VA_UTIMES_NULL;
3367 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3373 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3375 struct timespec ts[2];
3380 if ((error = getutimes(tptr, ts)) != 0)
3384 * NOTE: utimes() succeeds for the owner even if the file
3385 * is not user-writable.
3387 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3389 if ((error = nlookup(nd)) != 0)
3391 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3393 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3397 * note: vget is required for any operation that might mod the vnode
3398 * so VINACTIVE is properly cleared.
3400 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3401 error = vget(vp, LK_EXCLUSIVE);
3403 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3412 * utimes_args(char *path, struct timeval *tptr)
3414 * Set the access and modification times of a file.
3417 sys_utimes(struct utimes_args *uap)
3419 struct timeval tv[2];
3420 struct nlookupdata nd;
3424 error = copyin(uap->tptr, tv, sizeof(tv));
3428 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3430 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3436 * lutimes_args(char *path, struct timeval *tptr)
3438 * Set the access and modification times of a file.
3441 sys_lutimes(struct lutimes_args *uap)
3443 struct timeval tv[2];
3444 struct nlookupdata nd;
3448 error = copyin(uap->tptr, tv, sizeof(tv));
3452 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3454 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3460 * Set utimes on a file descriptor. The creds used to open the
3461 * file are used to determine whether the operation is allowed
3465 kern_futimes(int fd, struct timeval *tptr)
3467 struct thread *td = curthread;
3468 struct proc *p = td->td_proc;
3469 struct timespec ts[2];
3475 error = getutimes(tptr, ts);
3478 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3480 if (fp->f_nchandle.ncp)
3481 error = ncp_writechk(&fp->f_nchandle);
3484 error = vget(vp, LK_EXCLUSIVE);
3486 error = VOP_GETATTR(vp, &vattr);
3488 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3492 error = setutimes(vp, &vattr, ts,
3503 * futimes_args(int fd, struct timeval *tptr)
3505 * Set the access and modification times of a file.
3508 sys_futimes(struct futimes_args *uap)
3510 struct timeval tv[2];
3514 error = copyin(uap->tptr, tv, sizeof(tv));
3518 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3524 kern_truncate(struct nlookupdata *nd, off_t length)
3531 uint64_t old_size = 0;
3535 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3536 if ((error = nlookup(nd)) != 0)
3538 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3540 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3542 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3546 if (vp->v_type == VDIR) {
3550 if (vfs_quota_enabled) {
3551 error = VOP_GETATTR(vp, &vattr);
3552 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3555 old_size = vattr.va_size;
3558 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3560 vattr.va_size = length;
3561 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3562 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3570 * truncate(char *path, int pad, off_t length)
3572 * Truncate a file given its path name.
3575 sys_truncate(struct truncate_args *uap)
3577 struct nlookupdata nd;
3580 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3582 error = kern_truncate(&nd, uap->length);
3588 kern_ftruncate(int fd, off_t length)
3590 struct thread *td = curthread;
3591 struct proc *p = td->td_proc;
3598 uint64_t old_size = 0;
3603 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3605 if (fp->f_nchandle.ncp) {
3606 error = ncp_writechk(&fp->f_nchandle);
3610 if ((fp->f_flag & FWRITE) == 0) {
3614 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3618 vp = (struct vnode *)fp->f_data;
3619 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3620 if (vp->v_type == VDIR) {
3625 if (vfs_quota_enabled) {
3626 error = VOP_GETATTR(vp, &vattr);
3627 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3630 old_size = vattr.va_size;
3633 if ((error = vn_writechk(vp, NULL)) == 0) {
3635 vattr.va_size = length;
3636 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3638 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3647 * ftruncate_args(int fd, int pad, off_t length)
3649 * Truncate a file given a file descriptor.
3652 sys_ftruncate(struct ftruncate_args *uap)
3656 error = kern_ftruncate(uap->fd, uap->length);
3664 * Sync an open file.
3667 sys_fsync(struct fsync_args *uap)
3669 struct thread *td = curthread;
3670 struct proc *p = td->td_proc;
3676 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3678 vp = (struct vnode *)fp->f_data;
3679 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3680 if ((obj = vp->v_object) != NULL) {
3681 if (vp->v_mount == NULL ||
3682 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3683 vm_object_page_clean(obj, 0, 0, 0);
3686 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3687 if (error == 0 && vp->v_mount)
3688 error = buf_fsync(vp);
3696 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3698 struct nchandle fnchd;
3699 struct nchandle tnchd;
3700 struct namecache *ncp;
3707 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3708 if ((error = nlookup(fromnd)) != 0)
3710 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3712 fnchd.mount = fromnd->nl_nch.mount;
3716 * unlock the source nch so we can lookup the target nch without
3717 * deadlocking. The target may or may not exist so we do not check
3718 * for a target vp like kern_mkdir() and other creation functions do.
3720 * The source and target directories are ref'd and rechecked after
3721 * everything is relocked to determine if the source or target file
3724 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3725 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3726 cache_unlock(&fromnd->nl_nch);
3728 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3729 if ((error = nlookup(tond)) != 0) {
3733 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3737 tnchd.mount = tond->nl_nch.mount;
3741 * If the source and target are the same there is nothing to do
3743 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3750 * Mount points cannot be renamed or overwritten
3752 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3761 * Relock the source ncp. cache_relock() will deal with any
3762 * deadlocks against the already-locked tond and will also
3763 * make sure both are resolved.
3765 * NOTE AFTER RELOCKING: The source or target ncp may have become
3766 * invalid while they were unlocked, nc_vp and nc_mount could
3769 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3770 &tond->nl_nch, tond->nl_cred);
3771 fromnd->nl_flags |= NLC_NCPISLOCKED;
3774 * If either fromnd or tond are marked destroyed a ripout occured
3775 * out from under us and we must retry.
3777 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
3778 fromnd->nl_nch.ncp->nc_vp == NULL ||
3779 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
3780 kprintf("kern_rename: retry due to ripout on: "
3781 "\"%s\" -> \"%s\"\n",
3782 fromnd->nl_nch.ncp->nc_name,
3783 tond->nl_nch.ncp->nc_name);
3790 * make sure the parent directories linkages are the same
3792 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3793 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3800 * Both the source and target must be within the same filesystem and
3801 * in the same filesystem as their parent directories within the
3802 * namecache topology.
3804 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3807 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3808 mp != tond->nl_nch.mount) {
3815 * Make sure the mount point is writable
3817 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3824 * If the target exists and either the source or target is a directory,
3825 * then both must be directories.
3827 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3830 if (tond->nl_nch.ncp->nc_vp) {
3831 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3833 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3834 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3836 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3842 * You cannot rename a source into itself or a subdirectory of itself.
3843 * We check this by travsersing the target directory upwards looking
3844 * for a match against the source.
3849 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3850 if (fromnd->nl_nch.ncp == ncp) {
3861 * Even though the namespaces are different, they may still represent
3862 * hardlinks to the same file. The filesystem might have a hard time
3863 * with this so we issue a NREMOVE of the source instead of a NRENAME
3864 * when we detect the situation.
3867 fdvp = fromnd->nl_dvp;
3868 tdvp = tond->nl_dvp;
3869 if (fdvp == NULL || tdvp == NULL) {
3871 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3872 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3875 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3876 fdvp, tdvp, tond->nl_cred);
3883 * rename_args(char *from, char *to)
3885 * Rename files. Source and destination must either both be directories,
3886 * or both not be directories. If target is a directory, it must be empty.
3889 sys_rename(struct rename_args *uap)
3891 struct nlookupdata fromnd, tond;
3895 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3897 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3899 error = kern_rename(&fromnd, &tond);
3900 nlookup_done(&tond);
3902 nlookup_done(&fromnd);
3903 } while (error == EAGAIN);
3908 * renameat_args(int oldfd, char *old, int newfd, char *new)
3910 * Rename files using paths relative to the directories associated with
3911 * oldfd and newfd. Source and destination must either both be directories,
3912 * or both not be directories. If target is a directory, it must be empty.
3915 sys_renameat(struct renameat_args *uap)
3917 struct nlookupdata oldnd, newnd;
3918 struct file *oldfp, *newfp;
3922 error = nlookup_init_at(&oldnd, &oldfp,
3923 uap->oldfd, uap->old,
3926 error = nlookup_init_at(&newnd, &newfp,
3927 uap->newfd, uap->new,
3930 error = kern_rename(&oldnd, &newnd);
3931 nlookup_done_at(&newnd, newfp);
3933 nlookup_done_at(&oldnd, oldfp);
3934 } while (error == EAGAIN);
3939 kern_mkdir(struct nlookupdata *nd, int mode)
3941 struct thread *td = curthread;
3942 struct proc *p = td->td_proc;
3948 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3949 if ((error = nlookup(nd)) != 0)
3952 if (nd->nl_nch.ncp->nc_vp)
3954 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3957 vattr.va_type = VDIR;
3958 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3961 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3968 * mkdir_args(char *path, int mode)
3970 * Make a directory file.
3973 sys_mkdir(struct mkdir_args *uap)
3975 struct nlookupdata nd;
3978 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3980 error = kern_mkdir(&nd, uap->mode);
3986 * mkdirat_args(int fd, char *path, mode_t mode)
3988 * Make a directory file. The path is relative to the directory associated
3992 sys_mkdirat(struct mkdirat_args *uap)
3994 struct nlookupdata nd;
3998 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4000 error = kern_mkdir(&nd, uap->mode);
4001 nlookup_done_at(&nd, fp);
4006 kern_rmdir(struct nlookupdata *nd)
4011 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4012 if ((error = nlookup(nd)) != 0)
4016 * Do not allow directories representing mount points to be
4017 * deleted, even if empty. Check write perms on mount point
4018 * in case the vnode is aliased (aka nullfs).
4020 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4022 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4024 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4029 * rmdir_args(char *path)
4031 * Remove a directory file.
4034 sys_rmdir(struct rmdir_args *uap)
4036 struct nlookupdata nd;
4039 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4041 error = kern_rmdir(&nd);
4047 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4048 enum uio_seg direction)
4050 struct thread *td = curthread;
4051 struct proc *p = td->td_proc;
4059 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4061 if ((fp->f_flag & FREAD) == 0) {
4065 vp = (struct vnode *)fp->f_data;
4067 if (vp->v_type != VDIR) {
4071 aiov.iov_base = buf;
4072 aiov.iov_len = count;
4073 auio.uio_iov = &aiov;
4074 auio.uio_iovcnt = 1;
4075 auio.uio_rw = UIO_READ;
4076 auio.uio_segflg = direction;
4078 auio.uio_resid = count;
4079 loff = auio.uio_offset = fp->f_offset;
4080 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4081 fp->f_offset = auio.uio_offset;
4084 if (count == auio.uio_resid) {
4085 if (union_dircheckp) {
4086 error = union_dircheckp(td, &vp, fp);
4093 if ((vp->v_flag & VROOT) &&
4094 (vp->v_mount->mnt_flag & MNT_UNION)) {
4095 struct vnode *tvp = vp;
4096 vp = vp->v_mount->mnt_vnodecovered;
4107 * WARNING! *basep may not be wide enough to accomodate the
4108 * seek offset. XXX should we hack this to return the upper 32 bits
4109 * for offsets greater then 4G?
4112 *basep = (long)loff;
4114 *res = count - auio.uio_resid;
4121 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4123 * Read a block of directory entries in a file system independent format.
4126 sys_getdirentries(struct getdirentries_args *uap)
4131 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4132 &uap->sysmsg_result, UIO_USERSPACE);
4134 if (error == 0 && uap->basep)
4135 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4140 * getdents_args(int fd, char *buf, size_t count)
4143 sys_getdents(struct getdents_args *uap)
4147 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4148 &uap->sysmsg_result, UIO_USERSPACE);
4154 * Set the mode mask for creation of filesystem nodes.
4156 * umask(int newmask)
4159 sys_umask(struct umask_args *uap)
4161 struct thread *td = curthread;
4162 struct proc *p = td->td_proc;
4163 struct filedesc *fdp;
4166 uap->sysmsg_result = fdp->fd_cmask;
4167 fdp->fd_cmask = uap->newmask & ALLPERMS;
4172 * revoke(char *path)
4174 * Void all references to file by ripping underlying filesystem
4178 sys_revoke(struct revoke_args *uap)
4180 struct nlookupdata nd;
4187 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4189 error = nlookup(&nd);
4191 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4192 cred = crhold(nd.nl_cred);
4196 error = VOP_GETATTR(vp, &vattr);
4197 if (error == 0 && cred->cr_uid != vattr.va_uid)
4198 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4199 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4201 error = vrevoke(vp, cred);
4202 } else if (error == 0) {
4203 error = vrevoke(vp, cred);
4213 * getfh_args(char *fname, fhandle_t *fhp)
4215 * Get (NFS) file handle
4217 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4218 * mount. This allows nullfs mounts to be explicitly exported.
4220 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4222 * nullfs mounts of subdirectories are not safe. That is, it will
4223 * work, but you do not really have protection against access to
4224 * the related parent directories.
4227 sys_getfh(struct getfh_args *uap)
4229 struct thread *td = curthread;
4230 struct nlookupdata nd;
4237 * Must be super user
4239 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4243 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4245 error = nlookup(&nd);
4247 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4248 mp = nd.nl_nch.mount;
4251 bzero(&fh, sizeof(fh));
4252 fh.fh_fsid = mp->mnt_stat.f_fsid;
4253 error = VFS_VPTOFH(vp, &fh.fh_fid);
4256 error = copyout(&fh, uap->fhp, sizeof(fh));
4262 * fhopen_args(const struct fhandle *u_fhp, int flags)
4264 * syscall for the rpc.lockd to use to translate a NFS file handle into
4265 * an open descriptor.
4267 * warning: do not remove the priv_check() call or this becomes one giant
4271 sys_fhopen(struct fhopen_args *uap)
4273 struct thread *td = curthread;
4274 struct filedesc *fdp = td->td_proc->p_fd;
4279 struct vattr *vap = &vat;
4281 int fmode, mode, error = 0, type;
4287 * Must be super user
4289 error = priv_check(td, PRIV_ROOT);
4293 fmode = FFLAGS(uap->flags);
4296 * Why not allow a non-read/write open for our lockd?
4298 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4300 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4305 * Find the mount point
4307 mp = vfs_getvfs(&fhp.fh_fsid);
4312 /* now give me my vnode, it gets returned to me locked */
4313 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4317 * from now on we have to make sure not
4318 * to forget about the vnode
4319 * any error that causes an abort must vput(vp)
4320 * just set error = err and 'goto bad;'.
4326 if (vp->v_type == VLNK) {
4330 if (vp->v_type == VSOCK) {
4335 if (fmode & (FWRITE | O_TRUNC)) {
4336 if (vp->v_type == VDIR) {
4340 error = vn_writechk(vp, NULL);
4348 error = VOP_ACCESS(vp, mode, td->td_ucred);
4352 if (fmode & O_TRUNC) {
4353 vn_unlock(vp); /* XXX */
4354 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4357 error = VOP_SETATTR(vp, vap, td->td_ucred);
4363 * VOP_OPEN needs the file pointer so it can potentially override
4366 * WARNING! no f_nchandle will be associated when fhopen()ing a
4369 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4373 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4376 * setting f_ops this way prevents VOP_CLOSE from being
4377 * called or fdrop() releasing the vp from v_data. Since
4378 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4380 fp->f_ops = &badfileops;
4386 * The fp is given its own reference, we still have our ref and lock.
4388 * Assert that all regular files must be created with a VM object.
4390 if (vp->v_type == VREG && vp->v_object == NULL) {
4391 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4396 * The open was successful. Handle any locking requirements.
4398 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4399 lf.l_whence = SEEK_SET;
4402 if (fmode & O_EXLOCK)
4403 lf.l_type = F_WRLCK;
4405 lf.l_type = F_RDLCK;
4406 if (fmode & FNONBLOCK)
4411 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4413 * release our private reference.
4415 fsetfd(fdp, NULL, indx);
4420 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4421 fp->f_flag |= FHASLOCK;
4425 * Clean up. Associate the file pointer with the previously
4426 * reserved descriptor and return it.
4429 fsetfd(fdp, fp, indx);
4431 uap->sysmsg_result = indx;
4432 if (uap->flags & O_CLOEXEC)
4433 error = fsetfdflags(fdp, indx, UF_EXCLOSE);
4437 fsetfd(fdp, NULL, indx);
4446 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4449 sys_fhstat(struct fhstat_args *uap)
4451 struct thread *td = curthread;
4459 * Must be super user
4461 error = priv_check(td, PRIV_ROOT);
4465 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4469 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4472 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4473 error = vn_stat(vp, &sb, td->td_ucred);
4478 error = copyout(&sb, uap->sb, sizeof(sb));
4483 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4486 sys_fhstatfs(struct fhstatfs_args *uap)
4488 struct thread *td = curthread;
4489 struct proc *p = td->td_proc;
4494 char *fullpath, *freepath;
4499 * Must be super user
4501 if ((error = priv_check(td, PRIV_ROOT)))
4504 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4507 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4511 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4516 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4521 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4524 error = mount_path(p, mp, &fullpath, &freepath);
4527 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4528 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4529 kfree(freepath, M_TEMP);
4531 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4532 if (priv_check(td, PRIV_ROOT)) {
4533 bcopy(sp, &sb, sizeof(sb));
4534 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4537 error = copyout(sp, uap->buf, sizeof(*sp));
4543 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4546 sys_fhstatvfs(struct fhstatvfs_args *uap)
4548 struct thread *td = curthread;
4549 struct proc *p = td->td_proc;
4557 * Must be super user
4559 if ((error = priv_check(td, PRIV_ROOT)))
4562 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4565 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4569 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4574 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4577 sp = &mp->mnt_vstat;
4579 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4583 if (mp->mnt_flag & MNT_RDONLY)
4584 sp->f_flag |= ST_RDONLY;
4585 if (mp->mnt_flag & MNT_NOSUID)
4586 sp->f_flag |= ST_NOSUID;
4587 error = copyout(sp, uap->buf, sizeof(*sp));
4594 * Syscall to push extended attribute configuration information into the
4595 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4596 * a command (int cmd), and attribute name and misc data. For now, the
4597 * attribute name is left in userspace for consumption by the VFS_op.
4598 * It will probably be changed to be copied into sysspace by the
4599 * syscall in the future, once issues with various consumers of the
4600 * attribute code have raised their hands.
4602 * Currently this is used only by UFS Extended Attributes.
4605 sys_extattrctl(struct extattrctl_args *uap)
4607 struct nlookupdata nd;
4609 char attrname[EXTATTR_MAXNAMELEN];
4617 if (error == 0 && uap->filename) {
4618 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4621 error = nlookup(&nd);
4623 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4627 if (error == 0 && uap->attrname) {
4628 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4633 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4635 error = nlookup(&nd);
4637 error = ncp_writechk(&nd.nl_nch);
4639 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4641 uap->attrname, nd.nl_cred);
4650 * Syscall to get a named extended attribute on a file or directory.
4653 sys_extattr_set_file(struct extattr_set_file_args *uap)
4655 char attrname[EXTATTR_MAXNAMELEN];
4656 struct nlookupdata nd;
4662 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4668 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4670 error = nlookup(&nd);
4672 error = ncp_writechk(&nd.nl_nch);
4674 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4680 bzero(&auio, sizeof(auio));
4681 aiov.iov_base = uap->data;
4682 aiov.iov_len = uap->nbytes;
4683 auio.uio_iov = &aiov;
4684 auio.uio_iovcnt = 1;
4685 auio.uio_offset = 0;
4686 auio.uio_resid = uap->nbytes;
4687 auio.uio_rw = UIO_WRITE;
4688 auio.uio_td = curthread;
4690 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4699 * Syscall to get a named extended attribute on a file or directory.
4702 sys_extattr_get_file(struct extattr_get_file_args *uap)
4704 char attrname[EXTATTR_MAXNAMELEN];
4705 struct nlookupdata nd;
4711 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4717 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4719 error = nlookup(&nd);
4721 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4727 bzero(&auio, sizeof(auio));
4728 aiov.iov_base = uap->data;
4729 aiov.iov_len = uap->nbytes;
4730 auio.uio_iov = &aiov;
4731 auio.uio_iovcnt = 1;
4732 auio.uio_offset = 0;
4733 auio.uio_resid = uap->nbytes;
4734 auio.uio_rw = UIO_READ;
4735 auio.uio_td = curthread;
4737 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4739 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4747 * Syscall to delete a named extended attribute from a file or directory.
4748 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4751 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4753 char attrname[EXTATTR_MAXNAMELEN];
4754 struct nlookupdata nd;
4758 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4762 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4764 error = nlookup(&nd);
4766 error = ncp_writechk(&nd.nl_nch);
4768 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4770 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4771 attrname, NULL, nd.nl_cred);
4780 * Determine if the mount is visible to the process.
4783 chroot_visible_mnt(struct mount *mp, struct proc *p)
4785 struct nchandle nch;
4788 * Traverse from the mount point upwards. If we hit the process
4789 * root then the mount point is visible to the process.
4791 nch = mp->mnt_ncmountpt;
4793 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4794 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4797 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4798 nch = nch.mount->mnt_ncmounton;
4800 nch.ncp = nch.ncp->nc_parent;
4805 * If the mount point is not visible to the process, but the
4806 * process root is in a subdirectory of the mount, return
4809 if (p->p_fd->fd_nrdir.mount == mp)