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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
35 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/sysent.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/mountctl.h>
46 #include <sys/sysproto.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
49 #include <sys/fcntl.h>
51 #include <sys/linker.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
58 #include <sys/namei.h>
59 #include <sys/nlookup.h>
60 #include <sys/dirent.h>
61 #include <sys/extattr.h>
62 #include <sys/spinlock.h>
63 #include <sys/kern_syscall.h>
64 #include <sys/objcache.h>
65 #include <sys/sysctl.h>
68 #include <sys/file2.h>
69 #include <sys/spinlock2.h>
70 #include <sys/mplock2.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
76 #include <machine/limits.h>
77 #include <machine/stdarg.h>
79 #include <vfs/union/union.h>
81 static void mount_warning(struct mount *mp, const char *ctl, ...)
83 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
84 static int checkvp_chdir (struct vnode *vn, struct thread *td);
85 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
86 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
87 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
88 static int getutimes (const struct timeval *, struct timespec *);
89 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
90 static int setfmode (struct vnode *, int);
91 static int setfflags (struct vnode *, int);
92 static int setutimes (struct vnode *, struct vattr *,
93 const struct timespec *, int);
94 static int usermount = 0; /* if 1, non-root can mount fs. */
96 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
98 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
99 "Allow non-root users to mount filesystems");
102 * Virtual File System System Calls
106 * Mount a file system.
108 * mount_args(char *type, char *path, int flags, caddr_t data)
113 sys_mount(struct mount_args *uap)
115 struct thread *td = curthread;
118 struct mount *mp, *nullmp;
119 struct vfsconf *vfsp;
120 int error, flag = 0, flag2 = 0;
123 struct nlookupdata nd;
124 char fstypename[MFSNAMELEN];
132 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
136 * Do not allow NFS export by non-root users.
138 if (uap->flags & MNT_EXPORTED) {
139 error = priv_check(td, PRIV_ROOT);
144 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
146 if (priv_check(td, PRIV_ROOT))
147 uap->flags |= MNT_NOSUID | MNT_NODEV;
150 * Lookup the requested path and extract the nch and vnode.
152 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
154 if ((error = nlookup(&nd)) == 0) {
155 if (nd.nl_nch.ncp->nc_vp == NULL)
165 * If the target filesystem is resolved via a nullfs mount, then
166 * nd.nl_nch.mount will be pointing to the nullfs mount structure
167 * instead of the target file system. We need it in case we are
170 nullmp = nd.nl_nch.mount;
173 * Extract the locked+refd ncp and cleanup the nd structure
176 cache_zero(&nd.nl_nch);
179 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
180 (mp = cache_findmount(&nch)) != NULL) {
189 * now we have the locked ref'd nch and unreferenced vnode.
192 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
199 * Extract the file system type. We need to know this early, to take
200 * appropriate actions if we are dealing with a nullfs.
202 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
209 * Now we have an unlocked ref'd nch and a locked ref'd vp
211 if (uap->flags & MNT_UPDATE) {
212 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
219 if (strncmp(fstypename, "null", 5) == 0) {
227 flag2 = mp->mnt_kern_flag;
229 * We only allow the filesystem to be reloaded if it
230 * is currently mounted read-only.
232 if ((uap->flags & MNT_RELOAD) &&
233 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
236 error = EOPNOTSUPP; /* Needs translation */
240 * Only root, or the user that did the original mount is
241 * permitted to update it.
243 if (mp->mnt_stat.f_owner != cred->cr_uid &&
244 (error = priv_check(td, PRIV_ROOT))) {
249 if (vfs_busy(mp, LK_NOWAIT)) {
263 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
264 lwkt_gettoken(&mp->mnt_token);
270 * If the user is not root, ensure that they own the directory
271 * onto which we are attempting to mount.
273 if ((error = VOP_GETATTR(vp, &va)) ||
274 (va.va_uid != cred->cr_uid &&
275 (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);
335 * Allocate and initialize the filesystem.
337 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
339 vfs_busy(mp, LK_NOWAIT);
340 mp->mnt_op = vfsp->vfc_vfsops;
342 vfsp->vfc_refcount++;
343 mp->mnt_stat.f_type = vfsp->vfc_typenum;
344 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
345 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
346 mp->mnt_stat.f_owner = cred->cr_uid;
347 lwkt_gettoken(&mp->mnt_token);
351 * (per-mount token acquired at this point)
353 * Set the mount level flags.
355 if (uap->flags & MNT_RDONLY)
356 mp->mnt_flag |= MNT_RDONLY;
357 else if (mp->mnt_flag & MNT_RDONLY)
358 mp->mnt_kern_flag |= MNTK_WANTRDWR;
359 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
360 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
361 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
362 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
363 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
364 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
365 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
366 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
368 * Mount the filesystem.
369 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
372 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
373 if (mp->mnt_flag & MNT_UPDATE) {
374 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
375 mp->mnt_flag &= ~MNT_RDONLY;
376 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
377 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
380 mp->mnt_kern_flag = flag2;
382 lwkt_reltoken(&mp->mnt_token);
388 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
391 * Put the new filesystem on the mount list after root. The mount
392 * point gets its own mnt_ncmountpt (unless the VFS already set one
393 * up) which represents the root of the mount. The lookup code
394 * detects the mount point going forward and checks the root of
395 * the mount going backwards.
397 * It is not necessary to invalidate or purge the vnode underneath
398 * because elements under the mount will be given their own glue
402 if (mp->mnt_ncmountpt.ncp == NULL) {
404 * allocate, then unlock, but leave the ref intact
406 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
407 cache_unlock(&mp->mnt_ncmountpt);
409 mp->mnt_ncmounton = nch; /* inherits ref */
410 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
411 cache_ismounting(mp);
413 mountlist_insert(mp, MNTINS_LAST);
415 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
416 error = vfs_allocate_syncvnode(mp);
417 lwkt_reltoken(&mp->mnt_token);
419 error = VFS_START(mp, 0);
422 vn_syncer_thr_stop(mp);
423 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
424 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
425 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
426 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
427 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
428 mp->mnt_vfc->vfc_refcount--;
429 lwkt_reltoken(&mp->mnt_token);
440 * Scan all active processes to see if any of them have a current
441 * or root directory onto which the new filesystem has just been
442 * mounted. If so, replace them with the new mount point.
444 * Both old_nch and new_nch are ref'd on call but not locked.
445 * new_nch must be temporarily locked so it can be associated with the
446 * vnode representing the root of the mount point.
448 struct checkdirs_info {
449 struct nchandle old_nch;
450 struct nchandle new_nch;
451 struct vnode *old_vp;
452 struct vnode *new_vp;
455 static int checkdirs_callback(struct proc *p, void *data);
458 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
460 struct checkdirs_info info;
466 * If the old mount point's vnode has a usecount of 1, it is not
467 * being held as a descriptor anywhere.
469 olddp = old_nch->ncp->nc_vp;
470 if (olddp == NULL || VREFCNT(olddp) == 1)
474 * Force the root vnode of the new mount point to be resolved
475 * so we can update any matching processes.
478 if (VFS_ROOT(mp, &newdp))
479 panic("mount: lost mount");
482 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
483 cache_setunresolved(new_nch);
484 cache_setvp(new_nch, newdp);
485 cache_unlock(new_nch);
488 * Special handling of the root node
490 if (rootvnode == olddp) {
492 vfs_cache_setroot(newdp, cache_hold(new_nch));
496 * Pass newdp separately so the callback does not have to access
497 * it via new_nch->ncp->nc_vp.
499 info.old_nch = *old_nch;
500 info.new_nch = *new_nch;
502 allproc_scan(checkdirs_callback, &info);
507 * NOTE: callback is not MP safe because the scanned process's filedesc
508 * structure can be ripped out from under us, amoung other things.
511 checkdirs_callback(struct proc *p, void *data)
513 struct checkdirs_info *info = data;
514 struct filedesc *fdp;
515 struct nchandle ncdrop1;
516 struct nchandle ncdrop2;
517 struct vnode *vprele1;
518 struct vnode *vprele2;
520 if ((fdp = p->p_fd) != NULL) {
521 cache_zero(&ncdrop1);
522 cache_zero(&ncdrop2);
527 * MPUNSAFE - XXX fdp can be pulled out from under a
530 * A shared filedesc is ok, we don't have to copy it
531 * because we are making this change globally.
533 spin_lock(&fdp->fd_spin);
534 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
535 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
536 vprele1 = fdp->fd_cdir;
538 fdp->fd_cdir = info->new_vp;
539 ncdrop1 = fdp->fd_ncdir;
540 cache_copy(&info->new_nch, &fdp->fd_ncdir);
542 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
543 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
544 vprele2 = fdp->fd_rdir;
546 fdp->fd_rdir = info->new_vp;
547 ncdrop2 = fdp->fd_nrdir;
548 cache_copy(&info->new_nch, &fdp->fd_nrdir);
550 spin_unlock(&fdp->fd_spin);
552 cache_drop(&ncdrop1);
554 cache_drop(&ncdrop2);
564 * Unmount a file system.
566 * Note: unmount takes a path to the vnode mounted on as argument,
567 * not special file (as before).
569 * umount_args(char *path, int flags)
574 sys_unmount(struct unmount_args *uap)
576 struct thread *td = curthread;
577 struct proc *p __debugvar = td->td_proc;
578 struct mount *mp = NULL;
579 struct nlookupdata nd;
584 if (td->td_ucred->cr_prison != NULL) {
588 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
591 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
593 error = nlookup(&nd);
597 mp = nd.nl_nch.mount;
600 * Only root, or the user that did the original mount is
601 * permitted to unmount this filesystem.
603 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
604 (error = priv_check(td, PRIV_ROOT)))
608 * Don't allow unmounting the root file system.
610 if (mp->mnt_flag & MNT_ROOTFS) {
616 * Must be the root of the filesystem
618 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
626 error = dounmount(mp, uap->flags);
633 * Do the actual file system unmount.
636 dounmount_interlock(struct mount *mp)
638 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
640 mp->mnt_kern_flag |= MNTK_UNMOUNT;
645 unmount_allproc_cb(struct proc *p, void *arg)
649 if (p->p_textnch.ncp == NULL)
652 mp = (struct mount *)arg;
653 if (p->p_textnch.mount == mp)
654 cache_drop(&p->p_textnch);
660 dounmount(struct mount *mp, int flags)
662 struct namecache *ncp;
671 lwkt_gettoken(&mp->mnt_token);
673 * Exclusive access for unmounting purposes
675 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
679 * Allow filesystems to detect that a forced unmount is in progress.
681 if (flags & MNT_FORCE)
682 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
683 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
684 error = lockmgr(&mp->mnt_lock, lflags);
686 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
687 if (mp->mnt_kern_flag & MNTK_MWAIT) {
688 mp->mnt_kern_flag &= ~MNTK_MWAIT;
694 if (mp->mnt_flag & MNT_EXPUBLIC)
695 vfs_setpublicfs(NULL, NULL, NULL);
697 vfs_msync(mp, MNT_WAIT);
698 async_flag = mp->mnt_flag & MNT_ASYNC;
699 mp->mnt_flag &=~ MNT_ASYNC;
702 * If this filesystem isn't aliasing other filesystems,
703 * try to invalidate any remaining namecache entries and
704 * check the count afterwords.
706 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
707 cache_lock(&mp->mnt_ncmountpt);
708 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
709 cache_unlock(&mp->mnt_ncmountpt);
711 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
712 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
713 allproc_scan(&unmount_allproc_cb, mp);
716 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
717 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
719 if ((flags & MNT_FORCE) == 0) {
721 mount_warning(mp, "Cannot unmount: "
727 mount_warning(mp, "Forced unmount: "
738 * Decomission our special mnt_syncer vnode. This also stops
739 * the vnlru code. If we are unable to unmount we recommission
742 * Then sync the filesystem.
744 if ((vp = mp->mnt_syncer) != NULL) {
745 mp->mnt_syncer = NULL;
746 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
749 if ((mp->mnt_flag & MNT_RDONLY) == 0)
750 VFS_SYNC(mp, MNT_WAIT);
753 * nchandle records ref the mount structure. Expect a count of 1
754 * (our mount->mnt_ncmountpt).
756 * Scans can get temporary refs on a mountpoint (thought really
757 * heavy duty stuff like cache_findmount() do not).
759 for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
760 cache_unmounting(mp);
761 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
763 if (mp->mnt_refs != 1) {
764 if ((flags & MNT_FORCE) == 0) {
765 mount_warning(mp, "Cannot unmount: "
766 "%d mount refs still present",
770 mount_warning(mp, "Forced unmount: "
771 "%d mount refs still present",
778 * So far so good, sync the filesystem once more and
779 * call the VFS unmount code if the sync succeeds.
782 if (((mp->mnt_flag & MNT_RDONLY) ||
783 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
784 (flags & MNT_FORCE)) {
785 error = VFS_UNMOUNT(mp, flags);
790 * If an error occurred we can still recover, restoring the
791 * syncer vnode and misc flags.
794 if (mp->mnt_syncer == NULL)
795 vfs_allocate_syncvnode(mp);
796 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
797 mp->mnt_flag |= async_flag;
798 lockmgr(&mp->mnt_lock, LK_RELEASE);
799 if (mp->mnt_kern_flag & MNTK_MWAIT) {
800 mp->mnt_kern_flag &= ~MNTK_MWAIT;
806 * Clean up any journals still associated with the mount after
807 * filesystem activity has ceased.
809 journal_remove_all_journals(mp,
810 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
812 mountlist_remove(mp);
815 * Remove any installed vnode ops here so the individual VFSs don't
818 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
819 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
820 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
821 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
822 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
824 if (mp->mnt_ncmountpt.ncp != NULL) {
825 nch = mp->mnt_ncmountpt;
826 cache_zero(&mp->mnt_ncmountpt);
827 cache_clrmountpt(&nch);
830 if (mp->mnt_ncmounton.ncp != NULL) {
831 cache_unmounting(mp);
832 nch = mp->mnt_ncmounton;
833 cache_zero(&mp->mnt_ncmounton);
834 cache_clrmountpt(&nch);
838 mp->mnt_vfc->vfc_refcount--;
839 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
840 panic("unmount: dangling vnode");
841 lockmgr(&mp->mnt_lock, LK_RELEASE);
842 if (mp->mnt_kern_flag & MNTK_MWAIT) {
843 mp->mnt_kern_flag &= ~MNTK_MWAIT;
848 * If we reach here and freeok != 0 we must free the mount.
849 * If refs > 1 cycle and wait, just in case someone tried
850 * to busy the mount after we decided to do the unmount.
853 while (mp->mnt_refs > 1) {
854 cache_unmounting(mp);
856 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
858 lwkt_reltoken(&mp->mnt_token);
865 lwkt_reltoken(&mp->mnt_token);
871 mount_warning(struct mount *mp, const char *ctl, ...)
878 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
879 &ptr, &buf, 0) == 0) {
880 kprintf("unmount(%s): ", ptr);
885 kprintf("unmount(%p", mp);
886 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
887 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
896 * Shim cache_fullpath() to handle the case where a process is chrooted into
897 * a subdirectory of a mount. In this case if the root mount matches the
898 * process root directory's mount we have to specify the process's root
899 * directory instead of the mount point, because the mount point might
900 * be above the root directory.
904 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
906 struct nchandle *nch;
908 if (p && p->p_fd->fd_nrdir.mount == mp)
909 nch = &p->p_fd->fd_nrdir;
911 nch = &mp->mnt_ncmountpt;
912 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
916 * Sync each mounted filesystem.
920 static int syncprt = 0;
921 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
924 static int sync_callback(struct mount *mp, void *data);
927 sys_sync(struct sync_args *uap)
929 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
935 sync_callback(struct mount *mp, void *data __unused)
939 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
940 asyncflag = mp->mnt_flag & MNT_ASYNC;
941 mp->mnt_flag &= ~MNT_ASYNC;
942 vfs_msync(mp, MNT_NOWAIT);
943 VFS_SYNC(mp, MNT_NOWAIT);
944 mp->mnt_flag |= asyncflag;
949 /* XXX PRISON: could be per prison flag */
950 static int prison_quotas;
952 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
956 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
958 * Change filesystem quotas.
963 sys_quotactl(struct quotactl_args *uap)
965 struct nlookupdata nd;
972 if (td->td_ucred->cr_prison && !prison_quotas) {
977 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
979 error = nlookup(&nd);
981 mp = nd.nl_nch.mount;
982 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
983 uap->arg, nd.nl_cred);
992 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
993 * void *buf, int buflen)
995 * This function operates on a mount point and executes the specified
996 * operation using the specified control data, and possibly returns data.
998 * The actual number of bytes stored in the result buffer is returned, 0
999 * if none, otherwise an error is returned.
1004 sys_mountctl(struct mountctl_args *uap)
1006 struct thread *td = curthread;
1007 struct proc *p = td->td_proc;
1015 * Sanity and permissions checks. We must be root.
1018 if (td->td_ucred->cr_prison != NULL)
1020 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1021 (error = priv_check(td, PRIV_ROOT)) != 0)
1025 * Argument length checks
1027 if (uap->ctllen < 0 || uap->ctllen > 1024)
1029 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1031 if (uap->path == NULL)
1035 * Allocate the necessary buffers and copyin data
1037 path = objcache_get(namei_oc, M_WAITOK);
1038 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1043 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1044 error = copyin(uap->ctl, ctl, uap->ctllen);
1049 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1052 * Validate the descriptor
1055 fp = holdfp(p->p_fd, uap->fd, -1);
1065 * Execute the internal kernel function and clean up.
1068 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1072 if (error == 0 && uap->sysmsg_result > 0)
1073 error = copyout(buf, uap->buf, uap->sysmsg_result);
1076 objcache_put(namei_oc, path);
1085 * Execute a mount control operation by resolving the path to a mount point
1086 * and calling vop_mountctl().
1088 * Use the mount point from the nch instead of the vnode so nullfs mounts
1089 * can properly spike the VOP.
1092 kern_mountctl(const char *path, int op, struct file *fp,
1093 const void *ctl, int ctllen,
1094 void *buf, int buflen, int *res)
1098 struct nlookupdata nd;
1103 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1105 error = nlookup(&nd);
1107 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1108 mp = nd.nl_nch.mount;
1115 * Must be the root of the filesystem
1117 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1121 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1128 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1130 struct thread *td = curthread;
1131 struct proc *p = td->td_proc;
1134 char *fullpath, *freepath;
1137 if ((error = nlookup(nd)) != 0)
1139 mp = nd->nl_nch.mount;
1141 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1144 error = mount_path(p, mp, &fullpath, &freepath);
1147 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1148 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1149 kfree(freepath, M_TEMP);
1151 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1152 bcopy(sp, buf, sizeof(*buf));
1153 /* Only root should have access to the fsid's. */
1154 if (priv_check(td, PRIV_ROOT))
1155 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1160 * statfs_args(char *path, struct statfs *buf)
1162 * Get filesystem statistics.
1165 sys_statfs(struct statfs_args *uap)
1167 struct nlookupdata nd;
1171 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1173 error = kern_statfs(&nd, &buf);
1176 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1181 kern_fstatfs(int fd, struct statfs *buf)
1183 struct thread *td = curthread;
1184 struct proc *p = td->td_proc;
1188 char *fullpath, *freepath;
1192 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1196 * Try to use mount info from any overlays rather than the
1197 * mount info for the underlying vnode, otherwise we will
1198 * fail when operating on null-mounted paths inside a chroot.
1200 if ((mp = fp->f_nchandle.mount) == NULL)
1201 mp = ((struct vnode *)fp->f_data)->v_mount;
1206 if (fp->f_cred == NULL) {
1211 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1214 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1216 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1217 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1218 kfree(freepath, M_TEMP);
1220 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1221 bcopy(sp, buf, sizeof(*buf));
1223 /* Only root should have access to the fsid's. */
1224 if (priv_check(td, PRIV_ROOT))
1225 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1233 * fstatfs_args(int fd, struct statfs *buf)
1235 * Get filesystem statistics.
1238 sys_fstatfs(struct fstatfs_args *uap)
1243 error = kern_fstatfs(uap->fd, &buf);
1246 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1251 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1257 if ((error = nlookup(nd)) != 0)
1259 mp = nd->nl_nch.mount;
1260 sp = &mp->mnt_vstat;
1261 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1265 if (mp->mnt_flag & MNT_RDONLY)
1266 sp->f_flag |= ST_RDONLY;
1267 if (mp->mnt_flag & MNT_NOSUID)
1268 sp->f_flag |= ST_NOSUID;
1269 bcopy(sp, buf, sizeof(*buf));
1274 * statfs_args(char *path, struct statfs *buf)
1276 * Get filesystem statistics.
1279 sys_statvfs(struct statvfs_args *uap)
1281 struct nlookupdata nd;
1285 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1287 error = kern_statvfs(&nd, &buf);
1290 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1295 kern_fstatvfs(int fd, struct statvfs *buf)
1297 struct thread *td = curthread;
1298 struct proc *p = td->td_proc;
1305 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1307 if ((mp = fp->f_nchandle.mount) == NULL)
1308 mp = ((struct vnode *)fp->f_data)->v_mount;
1313 if (fp->f_cred == NULL) {
1317 sp = &mp->mnt_vstat;
1318 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1322 if (mp->mnt_flag & MNT_RDONLY)
1323 sp->f_flag |= ST_RDONLY;
1324 if (mp->mnt_flag & MNT_NOSUID)
1325 sp->f_flag |= ST_NOSUID;
1327 bcopy(sp, buf, sizeof(*buf));
1335 * fstatfs_args(int fd, struct statfs *buf)
1337 * Get filesystem statistics.
1340 sys_fstatvfs(struct fstatvfs_args *uap)
1345 error = kern_fstatvfs(uap->fd, &buf);
1348 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1353 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1355 * Get statistics on all filesystems.
1358 struct getfsstat_info {
1359 struct statfs *sfsp;
1367 static int getfsstat_callback(struct mount *, void *);
1370 sys_getfsstat(struct getfsstat_args *uap)
1372 struct thread *td = curthread;
1373 struct getfsstat_info info;
1375 bzero(&info, sizeof(info));
1377 info.maxcount = uap->bufsize / sizeof(struct statfs);
1378 info.sfsp = uap->buf;
1380 info.flags = uap->flags;
1383 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1384 if (info.sfsp && info.count > info.maxcount)
1385 uap->sysmsg_result = info.maxcount;
1387 uap->sysmsg_result = info.count;
1388 return (info.error);
1392 getfsstat_callback(struct mount *mp, void *data)
1394 struct getfsstat_info *info = data;
1400 if (info->sfsp && info->count < info->maxcount) {
1401 if (info->td->td_proc &&
1402 !chroot_visible_mnt(mp, info->td->td_proc)) {
1408 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1409 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1410 * overrides MNT_WAIT.
1412 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1413 (info->flags & MNT_WAIT)) &&
1414 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1417 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1419 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1421 info->error = error;
1424 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1425 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1426 kfree(freepath, M_TEMP);
1428 error = copyout(sp, info->sfsp, sizeof(*sp));
1430 info->error = error;
1440 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1441 long bufsize, int flags)
1443 * Get statistics on all filesystems.
1446 struct getvfsstat_info {
1447 struct statfs *sfsp;
1448 struct statvfs *vsfsp;
1456 static int getvfsstat_callback(struct mount *, void *);
1459 sys_getvfsstat(struct getvfsstat_args *uap)
1461 struct thread *td = curthread;
1462 struct getvfsstat_info info;
1464 bzero(&info, sizeof(info));
1466 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1467 info.sfsp = uap->buf;
1468 info.vsfsp = uap->vbuf;
1470 info.flags = uap->flags;
1473 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1474 if (info.vsfsp && info.count > info.maxcount)
1475 uap->sysmsg_result = info.maxcount;
1477 uap->sysmsg_result = info.count;
1478 return (info.error);
1482 getvfsstat_callback(struct mount *mp, void *data)
1484 struct getvfsstat_info *info = data;
1486 struct statvfs *vsp;
1491 if (info->vsfsp && info->count < info->maxcount) {
1492 if (info->td->td_proc &&
1493 !chroot_visible_mnt(mp, info->td->td_proc)) {
1497 vsp = &mp->mnt_vstat;
1500 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1501 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1502 * overrides MNT_WAIT.
1504 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1505 (info->flags & MNT_WAIT)) &&
1506 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1509 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1511 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1512 (info->flags & MNT_WAIT)) &&
1513 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1517 if (mp->mnt_flag & MNT_RDONLY)
1518 vsp->f_flag |= ST_RDONLY;
1519 if (mp->mnt_flag & MNT_NOSUID)
1520 vsp->f_flag |= ST_NOSUID;
1522 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1524 info->error = error;
1527 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1528 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1529 kfree(freepath, M_TEMP);
1531 error = copyout(sp, info->sfsp, sizeof(*sp));
1533 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1535 info->error = error;
1547 * fchdir_args(int fd)
1549 * Change current working directory to a given file descriptor.
1552 sys_fchdir(struct fchdir_args *uap)
1554 struct thread *td = curthread;
1555 struct proc *p = td->td_proc;
1556 struct filedesc *fdp = p->p_fd;
1557 struct vnode *vp, *ovp;
1560 struct nchandle nch, onch, tnch;
1563 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1565 lwkt_gettoken(&p->p_token);
1566 vp = (struct vnode *)fp->f_data;
1568 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1569 if (fp->f_nchandle.ncp == NULL)
1572 error = checkvp_chdir(vp, td);
1577 cache_copy(&fp->f_nchandle, &nch);
1580 * If the ncp has become a mount point, traverse through
1584 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1585 (mp = cache_findmount(&nch)) != NULL
1587 error = nlookup_mp(mp, &tnch);
1589 cache_unlock(&tnch); /* leave ref intact */
1591 vp = tnch.ncp->nc_vp;
1592 error = vget(vp, LK_SHARED);
1593 KKASSERT(error == 0);
1597 cache_dropmount(mp);
1601 onch = fdp->fd_ncdir;
1602 vn_unlock(vp); /* leave ref intact */
1604 fdp->fd_ncdir = nch;
1613 lwkt_reltoken(&p->p_token);
1618 kern_chdir(struct nlookupdata *nd)
1620 struct thread *td = curthread;
1621 struct proc *p = td->td_proc;
1622 struct filedesc *fdp = p->p_fd;
1623 struct vnode *vp, *ovp;
1624 struct nchandle onch;
1627 if ((error = nlookup(nd)) != 0)
1629 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1631 if ((error = vget(vp, LK_SHARED)) != 0)
1634 lwkt_gettoken(&p->p_token);
1635 error = checkvp_chdir(vp, td);
1639 onch = fdp->fd_ncdir;
1640 cache_unlock(&nd->nl_nch); /* leave reference intact */
1641 fdp->fd_ncdir = nd->nl_nch;
1645 cache_zero(&nd->nl_nch);
1649 lwkt_reltoken(&p->p_token);
1654 * chdir_args(char *path)
1656 * Change current working directory (``.'').
1659 sys_chdir(struct chdir_args *uap)
1661 struct nlookupdata nd;
1664 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1666 error = kern_chdir(&nd);
1672 * Helper function for raised chroot(2) security function: Refuse if
1673 * any filedescriptors are open directories.
1676 chroot_refuse_vdir_fds(struct filedesc *fdp)
1683 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1684 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1686 vp = (struct vnode *)fp->f_data;
1687 if (vp->v_type != VDIR) {
1698 * This sysctl determines if we will allow a process to chroot(2) if it
1699 * has a directory open:
1700 * 0: disallowed for all processes.
1701 * 1: allowed for processes that were not already chroot(2)'ed.
1702 * 2: allowed for all processes.
1705 static int chroot_allow_open_directories = 1;
1707 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1708 &chroot_allow_open_directories, 0, "");
1711 * chroot to the specified namecache entry. We obtain the vp from the
1712 * namecache data. The passed ncp must be locked and referenced and will
1713 * remain locked and referenced on return.
1716 kern_chroot(struct nchandle *nch)
1718 struct thread *td = curthread;
1719 struct proc *p = td->td_proc;
1720 struct filedesc *fdp = p->p_fd;
1725 * Only privileged user can chroot
1727 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1732 * Disallow open directory descriptors (fchdir() breakouts).
1734 if (chroot_allow_open_directories == 0 ||
1735 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1736 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1739 if ((vp = nch->ncp->nc_vp) == NULL)
1742 if ((error = vget(vp, LK_SHARED)) != 0)
1746 * Check the validity of vp as a directory to change to and
1747 * associate it with rdir/jdir.
1749 error = checkvp_chdir(vp, td);
1750 vn_unlock(vp); /* leave reference intact */
1752 vrele(fdp->fd_rdir);
1753 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1754 cache_drop(&fdp->fd_nrdir);
1755 cache_copy(nch, &fdp->fd_nrdir);
1756 if (fdp->fd_jdir == NULL) {
1759 cache_copy(nch, &fdp->fd_njdir);
1768 * chroot_args(char *path)
1770 * Change notion of root (``/'') directory.
1773 sys_chroot(struct chroot_args *uap)
1775 struct thread *td __debugvar = curthread;
1776 struct nlookupdata nd;
1779 KKASSERT(td->td_proc);
1780 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1782 nd.nl_flags |= NLC_EXEC;
1783 error = nlookup(&nd);
1785 error = kern_chroot(&nd.nl_nch);
1792 sys_chroot_kernel(struct chroot_kernel_args *uap)
1794 struct thread *td = curthread;
1795 struct nlookupdata nd;
1796 struct nchandle *nch;
1800 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1804 error = nlookup(&nd);
1810 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1814 if ((vp = nch->ncp->nc_vp) == NULL) {
1819 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1822 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1824 vfs_cache_setroot(vp, cache_hold(nch));
1834 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1835 * determine whether it is legal to chdir to the vnode. The vnode's state
1836 * is not changed by this call.
1839 checkvp_chdir(struct vnode *vp, struct thread *td)
1843 if (vp->v_type != VDIR)
1846 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1851 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1853 struct thread *td = curthread;
1854 struct proc *p = td->td_proc;
1855 struct lwp *lp = td->td_lwp;
1856 struct filedesc *fdp = p->p_fd;
1861 int type, indx, error = 0;
1864 if ((oflags & O_ACCMODE) == O_ACCMODE)
1866 flags = FFLAGS(oflags);
1867 error = falloc(lp, &nfp, NULL);
1871 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1874 * XXX p_dupfd is a real mess. It allows a device to return a
1875 * file descriptor to be duplicated rather then doing the open
1881 * Call vn_open() to do the lookup and assign the vnode to the
1882 * file pointer. vn_open() does not change the ref count on fp
1883 * and the vnode, on success, will be inherited by the file pointer
1886 nd->nl_flags |= NLC_LOCKVP;
1887 error = vn_open(nd, fp, flags, cmode);
1891 * handle special fdopen() case. bleh. dupfdopen() is
1892 * responsible for dropping the old contents of ofiles[indx]
1895 * Note that fsetfd() will add a ref to fp which represents
1896 * the fd_files[] assignment. We must still drop our
1899 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1900 if (fdalloc(p, 0, &indx) == 0) {
1901 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1904 fdrop(fp); /* our ref */
1907 fsetfd(fdp, NULL, indx);
1910 fdrop(fp); /* our ref */
1911 if (error == ERESTART)
1917 * ref the vnode for ourselves so it can't be ripped out from under
1918 * is. XXX need an ND flag to request that the vnode be returned
1921 * Reserve a file descriptor but do not assign it until the open
1924 vp = (struct vnode *)fp->f_data;
1926 if ((error = fdalloc(p, 0, &indx)) != 0) {
1933 * If no error occurs the vp will have been assigned to the file
1938 if (flags & (O_EXLOCK | O_SHLOCK)) {
1939 lf.l_whence = SEEK_SET;
1942 if (flags & O_EXLOCK)
1943 lf.l_type = F_WRLCK;
1945 lf.l_type = F_RDLCK;
1946 if (flags & FNONBLOCK)
1951 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1953 * lock request failed. Clean up the reserved
1957 fsetfd(fdp, NULL, indx);
1961 fp->f_flag |= FHASLOCK;
1965 * Assert that all regular file vnodes were created with a object.
1967 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1968 ("open: regular file has no backing object after vn_open"));
1974 * release our private reference, leaving the one associated with the
1975 * descriptor table intact.
1977 fsetfd(fdp, fp, indx);
1980 if (oflags & O_CLOEXEC)
1981 error = fsetfdflags(fdp, *res, UF_EXCLOSE);
1986 * open_args(char *path, int flags, int mode)
1988 * Check permissions, allocate an open file structure,
1989 * and call the device open routine if any.
1992 sys_open(struct open_args *uap)
1994 struct nlookupdata nd;
1997 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1999 error = kern_open(&nd, uap->flags,
2000 uap->mode, &uap->sysmsg_result);
2007 * openat_args(int fd, char *path, int flags, int mode)
2010 sys_openat(struct openat_args *uap)
2012 struct nlookupdata nd;
2016 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2018 error = kern_open(&nd, uap->flags, uap->mode,
2019 &uap->sysmsg_result);
2021 nlookup_done_at(&nd, fp);
2026 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2028 struct thread *td = curthread;
2029 struct proc *p = td->td_proc;
2038 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2039 vattr.va_rmajor = rmajor;
2040 vattr.va_rminor = rminor;
2042 switch (mode & S_IFMT) {
2043 case S_IFMT: /* used by badsect to flag bad sectors */
2044 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2045 vattr.va_type = VBAD;
2048 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2049 vattr.va_type = VCHR;
2052 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2053 vattr.va_type = VBLK;
2056 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2059 case S_IFDIR: /* special directories support for HAMMER */
2060 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2061 vattr.va_type = VDIR;
2072 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2073 if ((error = nlookup(nd)) != 0)
2075 if (nd->nl_nch.ncp->nc_vp)
2077 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2081 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2082 nd->nl_cred, NAMEI_CREATE);
2085 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2086 &vp, nd->nl_cred, &vattr);
2094 * mknod_args(char *path, int mode, int dev)
2096 * Create a special file.
2099 sys_mknod(struct mknod_args *uap)
2101 struct nlookupdata nd;
2104 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2106 error = kern_mknod(&nd, uap->mode,
2107 umajor(uap->dev), uminor(uap->dev));
2114 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2116 * Create a special file. The path is relative to the directory associated
2120 sys_mknodat(struct mknodat_args *uap)
2122 struct nlookupdata nd;
2126 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2128 error = kern_mknod(&nd, uap->mode,
2129 umajor(uap->dev), uminor(uap->dev));
2131 nlookup_done_at(&nd, fp);
2136 kern_mkfifo(struct nlookupdata *nd, int mode)
2138 struct thread *td = curthread;
2139 struct proc *p = td->td_proc;
2146 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2147 if ((error = nlookup(nd)) != 0)
2149 if (nd->nl_nch.ncp->nc_vp)
2151 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2155 vattr.va_type = VFIFO;
2156 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2158 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2165 * mkfifo_args(char *path, int mode)
2167 * Create a named pipe.
2170 sys_mkfifo(struct mkfifo_args *uap)
2172 struct nlookupdata nd;
2175 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2177 error = kern_mkfifo(&nd, uap->mode);
2183 * mkfifoat_args(int fd, char *path, mode_t mode)
2185 * Create a named pipe. The path is relative to the directory associated
2189 sys_mkfifoat(struct mkfifoat_args *uap)
2191 struct nlookupdata nd;
2195 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2197 error = kern_mkfifo(&nd, uap->mode);
2198 nlookup_done_at(&nd, fp);
2202 static int hardlink_check_uid = 0;
2203 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2204 &hardlink_check_uid, 0,
2205 "Unprivileged processes cannot create hard links to files owned by other "
2207 static int hardlink_check_gid = 0;
2208 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2209 &hardlink_check_gid, 0,
2210 "Unprivileged processes cannot create hard links to files owned by other "
2214 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2220 * Shortcut if disabled
2222 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2226 * Privileged user can always hardlink
2228 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2232 * Otherwise only if the originating file is owned by the
2233 * same user or group. Note that any group is allowed if
2234 * the file is owned by the caller.
2236 error = VOP_GETATTR(vp, &va);
2240 if (hardlink_check_uid) {
2241 if (cred->cr_uid != va.va_uid)
2245 if (hardlink_check_gid) {
2246 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2254 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2256 struct thread *td = curthread;
2261 * Lookup the source and obtained a locked vnode.
2263 * You may only hardlink a file which you have write permission
2264 * on or which you own.
2266 * XXX relookup on vget failure / race ?
2269 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2270 if ((error = nlookup(nd)) != 0)
2272 vp = nd->nl_nch.ncp->nc_vp;
2273 KKASSERT(vp != NULL);
2274 if (vp->v_type == VDIR)
2275 return (EPERM); /* POSIX */
2276 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2278 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2282 * Unlock the source so we can lookup the target without deadlocking
2283 * (XXX vp is locked already, possible other deadlock?). The target
2286 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2287 nd->nl_flags &= ~NLC_NCPISLOCKED;
2288 cache_unlock(&nd->nl_nch);
2291 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2292 if ((error = nlookup(linknd)) != 0) {
2296 if (linknd->nl_nch.ncp->nc_vp) {
2300 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2306 * Finally run the new API VOP.
2308 error = can_hardlink(vp, td, td->td_ucred);
2310 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2311 vp, linknd->nl_cred);
2318 * link_args(char *path, char *link)
2320 * Make a hard file link.
2323 sys_link(struct link_args *uap)
2325 struct nlookupdata nd, linknd;
2328 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2330 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2332 error = kern_link(&nd, &linknd);
2333 nlookup_done(&linknd);
2340 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2342 * Make a hard file link. The path1 argument is relative to the directory
2343 * associated with fd1, and similarly the path2 argument is relative to
2344 * the directory associated with fd2.
2347 sys_linkat(struct linkat_args *uap)
2349 struct nlookupdata nd, linknd;
2350 struct file *fp1, *fp2;
2353 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2354 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2356 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2357 uap->path2, UIO_USERSPACE, 0);
2359 error = kern_link(&nd, &linknd);
2360 nlookup_done_at(&linknd, fp2);
2362 nlookup_done_at(&nd, fp1);
2367 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2375 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2376 if ((error = nlookup(nd)) != 0)
2378 if (nd->nl_nch.ncp->nc_vp)
2380 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2384 vattr.va_mode = mode;
2385 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2392 * symlink(char *path, char *link)
2394 * Make a symbolic link.
2397 sys_symlink(struct symlink_args *uap)
2399 struct thread *td = curthread;
2400 struct nlookupdata nd;
2405 path = objcache_get(namei_oc, M_WAITOK);
2406 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2408 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2410 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2411 error = kern_symlink(&nd, path, mode);
2415 objcache_put(namei_oc, path);
2420 * symlinkat_args(char *path1, int fd, char *path2)
2422 * Make a symbolic link. The path2 argument is relative to the directory
2423 * associated with fd.
2426 sys_symlinkat(struct symlinkat_args *uap)
2428 struct thread *td = curthread;
2429 struct nlookupdata nd;
2435 path1 = objcache_get(namei_oc, M_WAITOK);
2436 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2438 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2441 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2442 error = kern_symlink(&nd, path1, mode);
2444 nlookup_done_at(&nd, fp);
2446 objcache_put(namei_oc, path1);
2451 * undelete_args(char *path)
2453 * Delete a whiteout from the filesystem.
2456 sys_undelete(struct undelete_args *uap)
2458 struct nlookupdata nd;
2461 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2463 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2465 error = nlookup(&nd);
2467 error = ncp_writechk(&nd.nl_nch);
2469 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2477 kern_unlink(struct nlookupdata *nd)
2482 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2483 if ((error = nlookup(nd)) != 0)
2485 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2487 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2492 * unlink_args(char *path)
2494 * Delete a name from the filesystem.
2497 sys_unlink(struct unlink_args *uap)
2499 struct nlookupdata nd;
2502 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2504 error = kern_unlink(&nd);
2511 * unlinkat_args(int fd, char *path, int flags)
2513 * Delete the file or directory entry pointed to by fd/path.
2516 sys_unlinkat(struct unlinkat_args *uap)
2518 struct nlookupdata nd;
2522 if (uap->flags & ~AT_REMOVEDIR)
2525 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2527 if (uap->flags & AT_REMOVEDIR)
2528 error = kern_rmdir(&nd);
2530 error = kern_unlink(&nd);
2532 nlookup_done_at(&nd, fp);
2537 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2539 struct thread *td = curthread;
2540 struct proc *p = td->td_proc;
2547 fp = holdfp(p->p_fd, fd, -1);
2550 if (fp->f_type != DTYPE_VNODE) {
2554 vp = (struct vnode *)fp->f_data;
2558 spin_lock(&fp->f_spin);
2559 new_offset = fp->f_offset + offset;
2563 error = VOP_GETATTR(vp, &vattr);
2564 spin_lock(&fp->f_spin);
2565 new_offset = offset + vattr.va_size;
2568 new_offset = offset;
2570 spin_lock(&fp->f_spin);
2575 spin_lock(&fp->f_spin);
2580 * Validate the seek position. Negative offsets are not allowed
2581 * for regular files or directories.
2583 * Normally we would also not want to allow negative offsets for
2584 * character and block-special devices. However kvm addresses
2585 * on 64 bit architectures might appear to be negative and must
2589 if (new_offset < 0 &&
2590 (vp->v_type == VREG || vp->v_type == VDIR)) {
2593 fp->f_offset = new_offset;
2596 *res = fp->f_offset;
2597 spin_unlock(&fp->f_spin);
2604 * lseek_args(int fd, int pad, off_t offset, int whence)
2606 * Reposition read/write file offset.
2609 sys_lseek(struct lseek_args *uap)
2613 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2614 &uap->sysmsg_offset);
2620 * Check if current process can access given file. amode is a bitmask of *_OK
2621 * access bits. flags is a bitmask of AT_* flags.
2624 kern_access(struct nlookupdata *nd, int amode, int flags)
2629 if (flags & ~AT_EACCESS)
2631 if ((error = nlookup(nd)) != 0)
2634 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2638 /* Flags == 0 means only check for existence. */
2647 if ((mode & VWRITE) == 0 ||
2648 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2649 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2652 * If the file handle is stale we have to re-resolve the
2653 * entry. This is a hack at the moment.
2655 if (error == ESTALE) {
2657 cache_setunresolved(&nd->nl_nch);
2658 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2671 * access_args(char *path, int flags)
2673 * Check access permissions.
2676 sys_access(struct access_args *uap)
2678 struct nlookupdata nd;
2681 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2683 error = kern_access(&nd, uap->flags, 0);
2690 * eaccess_args(char *path, int flags)
2692 * Check access permissions.
2695 sys_eaccess(struct eaccess_args *uap)
2697 struct nlookupdata nd;
2700 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2702 error = kern_access(&nd, uap->flags, AT_EACCESS);
2709 * faccessat_args(int fd, char *path, int amode, int flags)
2711 * Check access permissions.
2714 sys_faccessat(struct faccessat_args *uap)
2716 struct nlookupdata nd;
2720 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2723 error = kern_access(&nd, uap->amode, uap->flags);
2724 nlookup_done_at(&nd, fp);
2730 kern_stat(struct nlookupdata *nd, struct stat *st)
2735 if ((error = nlookup(nd)) != 0)
2738 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2741 if ((error = vget(vp, LK_SHARED)) != 0)
2743 error = vn_stat(vp, st, nd->nl_cred);
2746 * If the file handle is stale we have to re-resolve the entry. This
2747 * is a hack at the moment.
2749 if (error == ESTALE) {
2751 cache_setunresolved(&nd->nl_nch);
2752 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2762 * stat_args(char *path, struct stat *ub)
2764 * Get file status; this version follows links.
2767 sys_stat(struct stat_args *uap)
2769 struct nlookupdata nd;
2773 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2775 error = kern_stat(&nd, &st);
2777 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2784 * lstat_args(char *path, struct stat *ub)
2786 * Get file status; this version does not follow links.
2789 sys_lstat(struct lstat_args *uap)
2791 struct nlookupdata nd;
2795 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2797 error = kern_stat(&nd, &st);
2799 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2806 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2808 * Get status of file pointed to by fd/path.
2811 sys_fstatat(struct fstatat_args *uap)
2813 struct nlookupdata nd;
2819 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2822 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2824 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2825 UIO_USERSPACE, flags);
2827 error = kern_stat(&nd, &st);
2829 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2831 nlookup_done_at(&nd, fp);
2836 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
2838 struct nlookupdata nd;
2843 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
2845 error = nlookup(&nd);
2847 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2850 error = VOP_PATHCONF(vp, name, sysmsg_regp);
2857 * pathconf_Args(char *path, int name)
2859 * Get configurable pathname variables.
2862 sys_pathconf(struct pathconf_args *uap)
2864 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
2869 * lpathconf_Args(char *path, int name)
2871 * Get configurable pathname variables, but don't follow symlinks.
2874 sys_lpathconf(struct lpathconf_args *uap)
2876 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
2881 * kern_readlink isn't properly split yet. There is a copyin burried
2882 * in VOP_READLINK().
2885 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2887 struct thread *td = curthread;
2893 if ((error = nlookup(nd)) != 0)
2895 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2898 if (vp->v_type != VLNK) {
2901 aiov.iov_base = buf;
2902 aiov.iov_len = count;
2903 auio.uio_iov = &aiov;
2904 auio.uio_iovcnt = 1;
2905 auio.uio_offset = 0;
2906 auio.uio_rw = UIO_READ;
2907 auio.uio_segflg = UIO_USERSPACE;
2909 auio.uio_resid = count;
2910 error = VOP_READLINK(vp, &auio, td->td_ucred);
2913 *res = count - auio.uio_resid;
2918 * readlink_args(char *path, char *buf, int count)
2920 * Return target name of a symbolic link.
2923 sys_readlink(struct readlink_args *uap)
2925 struct nlookupdata nd;
2928 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2930 error = kern_readlink(&nd, uap->buf, uap->count,
2931 &uap->sysmsg_result);
2938 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2940 * Return target name of a symbolic link. The path is relative to the
2941 * directory associated with fd.
2944 sys_readlinkat(struct readlinkat_args *uap)
2946 struct nlookupdata nd;
2950 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2952 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2953 &uap->sysmsg_result);
2955 nlookup_done_at(&nd, fp);
2960 setfflags(struct vnode *vp, int flags)
2962 struct thread *td = curthread;
2967 * Prevent non-root users from setting flags on devices. When
2968 * a device is reused, users can retain ownership of the device
2969 * if they are allowed to set flags and programs assume that
2970 * chown can't fail when done as root.
2972 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2973 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2977 * note: vget is required for any operation that might mod the vnode
2978 * so VINACTIVE is properly cleared.
2980 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2982 vattr.va_flags = flags;
2983 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2990 * chflags(char *path, int flags)
2992 * Change flags of a file given a path name.
2995 sys_chflags(struct chflags_args *uap)
2997 struct nlookupdata nd;
3002 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3004 error = nlookup(&nd);
3006 error = ncp_writechk(&nd.nl_nch);
3008 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3011 error = setfflags(vp, uap->flags);
3018 * lchflags(char *path, int flags)
3020 * Change flags of a file given a path name, but don't follow symlinks.
3023 sys_lchflags(struct lchflags_args *uap)
3025 struct nlookupdata nd;
3030 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3032 error = nlookup(&nd);
3034 error = ncp_writechk(&nd.nl_nch);
3036 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3039 error = setfflags(vp, uap->flags);
3046 * fchflags_args(int fd, int flags)
3048 * Change flags of a file given a file descriptor.
3051 sys_fchflags(struct fchflags_args *uap)
3053 struct thread *td = curthread;
3054 struct proc *p = td->td_proc;
3058 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3060 if (fp->f_nchandle.ncp)
3061 error = ncp_writechk(&fp->f_nchandle);
3063 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3069 setfmode(struct vnode *vp, int mode)
3071 struct thread *td = curthread;
3076 * note: vget is required for any operation that might mod the vnode
3077 * so VINACTIVE is properly cleared.
3079 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3081 vattr.va_mode = mode & ALLPERMS;
3082 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3089 kern_chmod(struct nlookupdata *nd, int mode)
3094 if ((error = nlookup(nd)) != 0)
3096 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3098 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3099 error = setfmode(vp, mode);
3105 * chmod_args(char *path, int mode)
3107 * Change mode of a file given path name.
3110 sys_chmod(struct chmod_args *uap)
3112 struct nlookupdata nd;
3115 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3117 error = kern_chmod(&nd, uap->mode);
3123 * lchmod_args(char *path, int mode)
3125 * Change mode of a file given path name (don't follow links.)
3128 sys_lchmod(struct lchmod_args *uap)
3130 struct nlookupdata nd;
3133 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3135 error = kern_chmod(&nd, uap->mode);
3141 * fchmod_args(int fd, int mode)
3143 * Change mode of a file given a file descriptor.
3146 sys_fchmod(struct fchmod_args *uap)
3148 struct thread *td = curthread;
3149 struct proc *p = td->td_proc;
3153 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3155 if (fp->f_nchandle.ncp)
3156 error = ncp_writechk(&fp->f_nchandle);
3158 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3164 * fchmodat_args(char *path, int mode)
3166 * Change mode of a file pointed to by fd/path.
3169 sys_fchmodat(struct fchmodat_args *uap)
3171 struct nlookupdata nd;
3176 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3178 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3180 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3181 UIO_USERSPACE, flags);
3183 error = kern_chmod(&nd, uap->mode);
3184 nlookup_done_at(&nd, fp);
3189 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3191 struct thread *td = curthread;
3199 * note: vget is required for any operation that might mod the vnode
3200 * so VINACTIVE is properly cleared.
3202 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3203 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3205 o_uid = vattr.va_uid;
3206 o_gid = vattr.va_gid;
3207 size = vattr.va_size;
3212 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3221 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3222 VFS_ACCOUNT(mp, uid, gid, size);
3229 kern_chown(struct nlookupdata *nd, int uid, int gid)
3234 if ((error = nlookup(nd)) != 0)
3236 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3238 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3239 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3245 * chown(char *path, int uid, int gid)
3247 * Set ownership given a path name.
3250 sys_chown(struct chown_args *uap)
3252 struct nlookupdata nd;
3255 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3257 error = kern_chown(&nd, uap->uid, uap->gid);
3263 * lchown_args(char *path, int uid, int gid)
3265 * Set ownership given a path name, do not cross symlinks.
3268 sys_lchown(struct lchown_args *uap)
3270 struct nlookupdata nd;
3273 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3275 error = kern_chown(&nd, uap->uid, uap->gid);
3281 * fchown_args(int fd, int uid, int gid)
3283 * Set ownership given a file descriptor.
3286 sys_fchown(struct fchown_args *uap)
3288 struct thread *td = curthread;
3289 struct proc *p = td->td_proc;
3293 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3295 if (fp->f_nchandle.ncp)
3296 error = ncp_writechk(&fp->f_nchandle);
3298 error = setfown(p->p_fd->fd_ncdir.mount,
3299 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3305 * fchownat(int fd, char *path, int uid, int gid, int flags)
3307 * Set ownership of file pointed to by fd/path.
3310 sys_fchownat(struct fchownat_args *uap)
3312 struct nlookupdata nd;
3317 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3319 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3321 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3322 UIO_USERSPACE, flags);
3324 error = kern_chown(&nd, uap->uid, uap->gid);
3325 nlookup_done_at(&nd, fp);
3331 getutimes(const struct timeval *tvp, struct timespec *tsp)
3333 struct timeval tv[2];
3337 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3340 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3341 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3347 setutimes(struct vnode *vp, struct vattr *vattr,
3348 const struct timespec *ts, int nullflag)
3350 struct thread *td = curthread;
3354 vattr->va_atime = ts[0];
3355 vattr->va_mtime = ts[1];
3357 vattr->va_vaflags |= VA_UTIMES_NULL;
3358 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3364 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3366 struct timespec ts[2];
3371 if ((error = getutimes(tptr, ts)) != 0)
3375 * NOTE: utimes() succeeds for the owner even if the file
3376 * is not user-writable.
3378 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3380 if ((error = nlookup(nd)) != 0)
3382 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3384 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3388 * note: vget is required for any operation that might mod the vnode
3389 * so VINACTIVE is properly cleared.
3391 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3392 error = vget(vp, LK_EXCLUSIVE);
3394 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3403 * utimes_args(char *path, struct timeval *tptr)
3405 * Set the access and modification times of a file.
3408 sys_utimes(struct utimes_args *uap)
3410 struct timeval tv[2];
3411 struct nlookupdata nd;
3415 error = copyin(uap->tptr, tv, sizeof(tv));
3419 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3421 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3427 * lutimes_args(char *path, struct timeval *tptr)
3429 * Set the access and modification times of a file.
3432 sys_lutimes(struct lutimes_args *uap)
3434 struct timeval tv[2];
3435 struct nlookupdata nd;
3439 error = copyin(uap->tptr, tv, sizeof(tv));
3443 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3445 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3451 * Set utimes on a file descriptor. The creds used to open the
3452 * file are used to determine whether the operation is allowed
3456 kern_futimes(int fd, struct timeval *tptr)
3458 struct thread *td = curthread;
3459 struct proc *p = td->td_proc;
3460 struct timespec ts[2];
3466 error = getutimes(tptr, ts);
3469 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3471 if (fp->f_nchandle.ncp)
3472 error = ncp_writechk(&fp->f_nchandle);
3475 error = vget(vp, LK_EXCLUSIVE);
3477 error = VOP_GETATTR(vp, &vattr);
3479 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3483 error = setutimes(vp, &vattr, ts,
3494 * futimes_args(int fd, struct timeval *tptr)
3496 * Set the access and modification times of a file.
3499 sys_futimes(struct futimes_args *uap)
3501 struct timeval tv[2];
3505 error = copyin(uap->tptr, tv, sizeof(tv));
3509 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3515 kern_truncate(struct nlookupdata *nd, off_t length)
3522 uint64_t old_size = 0;
3526 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3527 if ((error = nlookup(nd)) != 0)
3529 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3531 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3533 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3537 if (vp->v_type == VDIR) {
3541 if (vfs_quota_enabled) {
3542 error = VOP_GETATTR(vp, &vattr);
3543 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3546 old_size = vattr.va_size;
3549 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3551 vattr.va_size = length;
3552 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3553 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3561 * truncate(char *path, int pad, off_t length)
3563 * Truncate a file given its path name.
3566 sys_truncate(struct truncate_args *uap)
3568 struct nlookupdata nd;
3571 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3573 error = kern_truncate(&nd, uap->length);
3579 kern_ftruncate(int fd, off_t length)
3581 struct thread *td = curthread;
3582 struct proc *p = td->td_proc;
3589 uint64_t old_size = 0;
3594 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3596 if (fp->f_nchandle.ncp) {
3597 error = ncp_writechk(&fp->f_nchandle);
3601 if ((fp->f_flag & FWRITE) == 0) {
3605 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3609 vp = (struct vnode *)fp->f_data;
3610 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3611 if (vp->v_type == VDIR) {
3616 if (vfs_quota_enabled) {
3617 error = VOP_GETATTR(vp, &vattr);
3618 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3621 old_size = vattr.va_size;
3624 if ((error = vn_writechk(vp, NULL)) == 0) {
3626 vattr.va_size = length;
3627 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3629 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3638 * ftruncate_args(int fd, int pad, off_t length)
3640 * Truncate a file given a file descriptor.
3643 sys_ftruncate(struct ftruncate_args *uap)
3647 error = kern_ftruncate(uap->fd, uap->length);
3655 * Sync an open file.
3658 sys_fsync(struct fsync_args *uap)
3660 struct thread *td = curthread;
3661 struct proc *p = td->td_proc;
3667 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3669 vp = (struct vnode *)fp->f_data;
3670 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3671 if ((obj = vp->v_object) != NULL) {
3672 if (vp->v_mount == NULL ||
3673 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3674 vm_object_page_clean(obj, 0, 0, 0);
3677 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3678 if (error == 0 && vp->v_mount)
3679 error = buf_fsync(vp);
3687 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3689 struct nchandle fnchd;
3690 struct nchandle tnchd;
3691 struct namecache *ncp;
3698 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3699 if ((error = nlookup(fromnd)) != 0)
3701 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3703 fnchd.mount = fromnd->nl_nch.mount;
3707 * unlock the source nch so we can lookup the target nch without
3708 * deadlocking. The target may or may not exist so we do not check
3709 * for a target vp like kern_mkdir() and other creation functions do.
3711 * The source and target directories are ref'd and rechecked after
3712 * everything is relocked to determine if the source or target file
3715 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3716 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3717 cache_unlock(&fromnd->nl_nch);
3719 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3720 if ((error = nlookup(tond)) != 0) {
3724 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3728 tnchd.mount = tond->nl_nch.mount;
3732 * If the source and target are the same there is nothing to do
3734 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3741 * Mount points cannot be renamed or overwritten
3743 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3752 * Relock the source ncp. cache_relock() will deal with any
3753 * deadlocks against the already-locked tond and will also
3754 * make sure both are resolved.
3756 * NOTE AFTER RELOCKING: The source or target ncp may have become
3757 * invalid while they were unlocked, nc_vp and nc_mount could
3760 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3761 &tond->nl_nch, tond->nl_cred);
3762 fromnd->nl_flags |= NLC_NCPISLOCKED;
3765 * If either fromnd or tond are marked destroyed a ripout occured
3766 * out from under us and we must retry.
3768 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
3769 fromnd->nl_nch.ncp->nc_vp == NULL ||
3770 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
3771 kprintf("kern_rename: retry due to ripout on: "
3772 "\"%s\" -> \"%s\"\n",
3773 fromnd->nl_nch.ncp->nc_name,
3774 tond->nl_nch.ncp->nc_name);
3781 * make sure the parent directories linkages are the same
3783 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3784 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3791 * Both the source and target must be within the same filesystem and
3792 * in the same filesystem as their parent directories within the
3793 * namecache topology.
3795 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3798 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3799 mp != tond->nl_nch.mount) {
3806 * Make sure the mount point is writable
3808 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3815 * If the target exists and either the source or target is a directory,
3816 * then both must be directories.
3818 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3821 if (tond->nl_nch.ncp->nc_vp) {
3822 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3824 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3825 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3827 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3833 * You cannot rename a source into itself or a subdirectory of itself.
3834 * We check this by travsersing the target directory upwards looking
3835 * for a match against the source.
3840 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3841 if (fromnd->nl_nch.ncp == ncp) {
3852 * Even though the namespaces are different, they may still represent
3853 * hardlinks to the same file. The filesystem might have a hard time
3854 * with this so we issue a NREMOVE of the source instead of a NRENAME
3855 * when we detect the situation.
3858 fdvp = fromnd->nl_dvp;
3859 tdvp = tond->nl_dvp;
3860 if (fdvp == NULL || tdvp == NULL) {
3862 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3863 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3866 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3867 fdvp, tdvp, tond->nl_cred);
3874 * rename_args(char *from, char *to)
3876 * Rename files. Source and destination must either both be directories,
3877 * or both not be directories. If target is a directory, it must be empty.
3880 sys_rename(struct rename_args *uap)
3882 struct nlookupdata fromnd, tond;
3886 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3888 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3890 error = kern_rename(&fromnd, &tond);
3891 nlookup_done(&tond);
3893 nlookup_done(&fromnd);
3894 } while (error == EAGAIN);
3899 * renameat_args(int oldfd, char *old, int newfd, char *new)
3901 * Rename files using paths relative to the directories associated with
3902 * oldfd and newfd. Source and destination must either both be directories,
3903 * or both not be directories. If target is a directory, it must be empty.
3906 sys_renameat(struct renameat_args *uap)
3908 struct nlookupdata oldnd, newnd;
3909 struct file *oldfp, *newfp;
3913 error = nlookup_init_at(&oldnd, &oldfp,
3914 uap->oldfd, uap->old,
3917 error = nlookup_init_at(&newnd, &newfp,
3918 uap->newfd, uap->new,
3921 error = kern_rename(&oldnd, &newnd);
3922 nlookup_done_at(&newnd, newfp);
3924 nlookup_done_at(&oldnd, oldfp);
3925 } while (error == EAGAIN);
3930 kern_mkdir(struct nlookupdata *nd, int mode)
3932 struct thread *td = curthread;
3933 struct proc *p = td->td_proc;
3939 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3940 if ((error = nlookup(nd)) != 0)
3943 if (nd->nl_nch.ncp->nc_vp)
3945 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3948 vattr.va_type = VDIR;
3949 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3952 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3959 * mkdir_args(char *path, int mode)
3961 * Make a directory file.
3964 sys_mkdir(struct mkdir_args *uap)
3966 struct nlookupdata nd;
3969 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3971 error = kern_mkdir(&nd, uap->mode);
3977 * mkdirat_args(int fd, char *path, mode_t mode)
3979 * Make a directory file. The path is relative to the directory associated
3983 sys_mkdirat(struct mkdirat_args *uap)
3985 struct nlookupdata nd;
3989 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3991 error = kern_mkdir(&nd, uap->mode);
3992 nlookup_done_at(&nd, fp);
3997 kern_rmdir(struct nlookupdata *nd)
4002 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4003 if ((error = nlookup(nd)) != 0)
4007 * Do not allow directories representing mount points to be
4008 * deleted, even if empty. Check write perms on mount point
4009 * in case the vnode is aliased (aka nullfs).
4011 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4013 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4015 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4020 * rmdir_args(char *path)
4022 * Remove a directory file.
4025 sys_rmdir(struct rmdir_args *uap)
4027 struct nlookupdata nd;
4030 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4032 error = kern_rmdir(&nd);
4038 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4039 enum uio_seg direction)
4041 struct thread *td = curthread;
4042 struct proc *p = td->td_proc;
4050 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4052 if ((fp->f_flag & FREAD) == 0) {
4056 vp = (struct vnode *)fp->f_data;
4058 if (vp->v_type != VDIR) {
4062 aiov.iov_base = buf;
4063 aiov.iov_len = count;
4064 auio.uio_iov = &aiov;
4065 auio.uio_iovcnt = 1;
4066 auio.uio_rw = UIO_READ;
4067 auio.uio_segflg = direction;
4069 auio.uio_resid = count;
4070 loff = auio.uio_offset = fp->f_offset;
4071 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4072 fp->f_offset = auio.uio_offset;
4075 if (count == auio.uio_resid) {
4076 if (union_dircheckp) {
4077 error = union_dircheckp(td, &vp, fp);
4084 if ((vp->v_flag & VROOT) &&
4085 (vp->v_mount->mnt_flag & MNT_UNION)) {
4086 struct vnode *tvp = vp;
4087 vp = vp->v_mount->mnt_vnodecovered;
4098 * WARNING! *basep may not be wide enough to accomodate the
4099 * seek offset. XXX should we hack this to return the upper 32 bits
4100 * for offsets greater then 4G?
4103 *basep = (long)loff;
4105 *res = count - auio.uio_resid;
4112 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4114 * Read a block of directory entries in a file system independent format.
4117 sys_getdirentries(struct getdirentries_args *uap)
4122 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4123 &uap->sysmsg_result, UIO_USERSPACE);
4125 if (error == 0 && uap->basep)
4126 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4131 * getdents_args(int fd, char *buf, size_t count)
4134 sys_getdents(struct getdents_args *uap)
4138 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4139 &uap->sysmsg_result, UIO_USERSPACE);
4145 * Set the mode mask for creation of filesystem nodes.
4147 * umask(int newmask)
4150 sys_umask(struct umask_args *uap)
4152 struct thread *td = curthread;
4153 struct proc *p = td->td_proc;
4154 struct filedesc *fdp;
4157 uap->sysmsg_result = fdp->fd_cmask;
4158 fdp->fd_cmask = uap->newmask & ALLPERMS;
4163 * revoke(char *path)
4165 * Void all references to file by ripping underlying filesystem
4169 sys_revoke(struct revoke_args *uap)
4171 struct nlookupdata nd;
4178 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4180 error = nlookup(&nd);
4182 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4183 cred = crhold(nd.nl_cred);
4187 error = VOP_GETATTR(vp, &vattr);
4188 if (error == 0 && cred->cr_uid != vattr.va_uid)
4189 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4190 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4192 error = vrevoke(vp, cred);
4193 } else if (error == 0) {
4194 error = vrevoke(vp, cred);
4204 * getfh_args(char *fname, fhandle_t *fhp)
4206 * Get (NFS) file handle
4208 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4209 * mount. This allows nullfs mounts to be explicitly exported.
4211 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4213 * nullfs mounts of subdirectories are not safe. That is, it will
4214 * work, but you do not really have protection against access to
4215 * the related parent directories.
4218 sys_getfh(struct getfh_args *uap)
4220 struct thread *td = curthread;
4221 struct nlookupdata nd;
4228 * Must be super user
4230 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4234 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4236 error = nlookup(&nd);
4238 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4239 mp = nd.nl_nch.mount;
4242 bzero(&fh, sizeof(fh));
4243 fh.fh_fsid = mp->mnt_stat.f_fsid;
4244 error = VFS_VPTOFH(vp, &fh.fh_fid);
4247 error = copyout(&fh, uap->fhp, sizeof(fh));
4253 * fhopen_args(const struct fhandle *u_fhp, int flags)
4255 * syscall for the rpc.lockd to use to translate a NFS file handle into
4256 * an open descriptor.
4258 * warning: do not remove the priv_check() call or this becomes one giant
4262 sys_fhopen(struct fhopen_args *uap)
4264 struct thread *td = curthread;
4265 struct filedesc *fdp = td->td_proc->p_fd;
4270 struct vattr *vap = &vat;
4272 int fmode, mode, error = 0, type;
4278 * Must be super user
4280 error = priv_check(td, PRIV_ROOT);
4284 fmode = FFLAGS(uap->flags);
4287 * Why not allow a non-read/write open for our lockd?
4289 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4291 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4296 * Find the mount point
4298 mp = vfs_getvfs(&fhp.fh_fsid);
4303 /* now give me my vnode, it gets returned to me locked */
4304 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4308 * from now on we have to make sure not
4309 * to forget about the vnode
4310 * any error that causes an abort must vput(vp)
4311 * just set error = err and 'goto bad;'.
4317 if (vp->v_type == VLNK) {
4321 if (vp->v_type == VSOCK) {
4326 if (fmode & (FWRITE | O_TRUNC)) {
4327 if (vp->v_type == VDIR) {
4331 error = vn_writechk(vp, NULL);
4339 error = VOP_ACCESS(vp, mode, td->td_ucred);
4343 if (fmode & O_TRUNC) {
4344 vn_unlock(vp); /* XXX */
4345 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4348 error = VOP_SETATTR(vp, vap, td->td_ucred);
4354 * VOP_OPEN needs the file pointer so it can potentially override
4357 * WARNING! no f_nchandle will be associated when fhopen()ing a
4360 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4364 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4367 * setting f_ops this way prevents VOP_CLOSE from being
4368 * called or fdrop() releasing the vp from v_data. Since
4369 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4371 fp->f_ops = &badfileops;
4377 * The fp is given its own reference, we still have our ref and lock.
4379 * Assert that all regular files must be created with a VM object.
4381 if (vp->v_type == VREG && vp->v_object == NULL) {
4382 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4387 * The open was successful. Handle any locking requirements.
4389 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4390 lf.l_whence = SEEK_SET;
4393 if (fmode & O_EXLOCK)
4394 lf.l_type = F_WRLCK;
4396 lf.l_type = F_RDLCK;
4397 if (fmode & FNONBLOCK)
4402 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4404 * release our private reference.
4406 fsetfd(fdp, NULL, indx);
4411 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4412 fp->f_flag |= FHASLOCK;
4416 * Clean up. Associate the file pointer with the previously
4417 * reserved descriptor and return it.
4420 fsetfd(fdp, fp, indx);
4422 uap->sysmsg_result = indx;
4423 if (uap->flags & O_CLOEXEC)
4424 error = fsetfdflags(fdp, indx, UF_EXCLOSE);
4428 fsetfd(fdp, NULL, indx);
4437 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4440 sys_fhstat(struct fhstat_args *uap)
4442 struct thread *td = curthread;
4450 * Must be super user
4452 error = priv_check(td, PRIV_ROOT);
4456 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4460 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4463 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4464 error = vn_stat(vp, &sb, td->td_ucred);
4469 error = copyout(&sb, uap->sb, sizeof(sb));
4474 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4477 sys_fhstatfs(struct fhstatfs_args *uap)
4479 struct thread *td = curthread;
4480 struct proc *p = td->td_proc;
4485 char *fullpath, *freepath;
4490 * Must be super user
4492 if ((error = priv_check(td, PRIV_ROOT)))
4495 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4498 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4502 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4507 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4512 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4515 error = mount_path(p, mp, &fullpath, &freepath);
4518 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4519 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4520 kfree(freepath, M_TEMP);
4522 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4523 if (priv_check(td, PRIV_ROOT)) {
4524 bcopy(sp, &sb, sizeof(sb));
4525 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4528 error = copyout(sp, uap->buf, sizeof(*sp));
4534 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4537 sys_fhstatvfs(struct fhstatvfs_args *uap)
4539 struct thread *td = curthread;
4540 struct proc *p = td->td_proc;
4548 * Must be super user
4550 if ((error = priv_check(td, PRIV_ROOT)))
4553 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4556 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4560 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4565 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4568 sp = &mp->mnt_vstat;
4570 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4574 if (mp->mnt_flag & MNT_RDONLY)
4575 sp->f_flag |= ST_RDONLY;
4576 if (mp->mnt_flag & MNT_NOSUID)
4577 sp->f_flag |= ST_NOSUID;
4578 error = copyout(sp, uap->buf, sizeof(*sp));
4585 * Syscall to push extended attribute configuration information into the
4586 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4587 * a command (int cmd), and attribute name and misc data. For now, the
4588 * attribute name is left in userspace for consumption by the VFS_op.
4589 * It will probably be changed to be copied into sysspace by the
4590 * syscall in the future, once issues with various consumers of the
4591 * attribute code have raised their hands.
4593 * Currently this is used only by UFS Extended Attributes.
4596 sys_extattrctl(struct extattrctl_args *uap)
4598 struct nlookupdata nd;
4600 char attrname[EXTATTR_MAXNAMELEN];
4608 if (error == 0 && uap->filename) {
4609 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4612 error = nlookup(&nd);
4614 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4618 if (error == 0 && uap->attrname) {
4619 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4624 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4626 error = nlookup(&nd);
4628 error = ncp_writechk(&nd.nl_nch);
4630 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4632 uap->attrname, nd.nl_cred);
4641 * Syscall to get a named extended attribute on a file or directory.
4644 sys_extattr_set_file(struct extattr_set_file_args *uap)
4646 char attrname[EXTATTR_MAXNAMELEN];
4647 struct nlookupdata nd;
4653 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4659 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4661 error = nlookup(&nd);
4663 error = ncp_writechk(&nd.nl_nch);
4665 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4671 bzero(&auio, sizeof(auio));
4672 aiov.iov_base = uap->data;
4673 aiov.iov_len = uap->nbytes;
4674 auio.uio_iov = &aiov;
4675 auio.uio_iovcnt = 1;
4676 auio.uio_offset = 0;
4677 auio.uio_resid = uap->nbytes;
4678 auio.uio_rw = UIO_WRITE;
4679 auio.uio_td = curthread;
4681 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4690 * Syscall to get a named extended attribute on a file or directory.
4693 sys_extattr_get_file(struct extattr_get_file_args *uap)
4695 char attrname[EXTATTR_MAXNAMELEN];
4696 struct nlookupdata nd;
4702 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4708 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4710 error = nlookup(&nd);
4712 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4718 bzero(&auio, sizeof(auio));
4719 aiov.iov_base = uap->data;
4720 aiov.iov_len = uap->nbytes;
4721 auio.uio_iov = &aiov;
4722 auio.uio_iovcnt = 1;
4723 auio.uio_offset = 0;
4724 auio.uio_resid = uap->nbytes;
4725 auio.uio_rw = UIO_READ;
4726 auio.uio_td = curthread;
4728 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4730 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4738 * Syscall to delete a named extended attribute from a file or directory.
4739 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4742 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4744 char attrname[EXTATTR_MAXNAMELEN];
4745 struct nlookupdata nd;
4749 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4753 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4755 error = nlookup(&nd);
4757 error = ncp_writechk(&nd.nl_nch);
4759 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4761 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4762 attrname, NULL, nd.nl_cred);
4771 * Determine if the mount is visible to the process.
4774 chroot_visible_mnt(struct mount *mp, struct proc *p)
4776 struct nchandle nch;
4779 * Traverse from the mount point upwards. If we hit the process
4780 * root then the mount point is visible to the process.
4782 nch = mp->mnt_ncmountpt;
4784 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4785 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4788 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4789 nch = nch.mount->mnt_ncmounton;
4791 nch.ncp = nch.ncp->nc_parent;
4796 * If the mount point is not visible to the process, but the
4797 * process root is in a subdirectory of the mount, return
4800 if (p->p_fd->fd_nrdir.mount == mp)