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>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
75 #include <machine/limits.h>
76 #include <machine/stdarg.h>
78 static void mount_warning(struct mount *mp, const char *ctl, ...)
80 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
81 static int checkvp_chdir (struct vnode *vn, struct thread *td);
82 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
83 static int chroot_refuse_vdir_fds (thread_t td, struct filedesc *fdp);
84 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
85 static int getutimes (struct timeval *, struct timespec *);
86 static int getutimens (const struct timespec *, struct timespec *, int *);
87 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
88 static int setfmode (struct vnode *, int);
89 static int setfflags (struct vnode *, int);
90 static int setutimes (struct vnode *, struct vattr *,
91 const struct timespec *, int);
93 static int usermount = 0; /* if 1, non-root can mount fs. */
94 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
95 "Allow non-root users to mount filesystems");
97 static int debug_unmount = 0; /* if 1 loop until unmount success */
98 SYSCTL_INT(_vfs, OID_AUTO, debug_unmount, CTLFLAG_RW, &debug_unmount, 0,
99 "Stall failed unmounts in loop");
101 * Virtual File System System Calls
105 * Mount a file system.
107 * mount_args(char *type, char *path, int flags, caddr_t data)
112 sys_mount(struct mount_args *uap)
114 struct thread *td = curthread;
117 struct mount *mp, *nullmp;
118 struct vfsconf *vfsp;
119 int error, flag = 0, flag2 = 0;
122 struct nlookupdata nd;
123 char fstypename[MFSNAMELEN];
131 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
135 * Do not allow NFS export by non-root users.
137 if (uap->flags & MNT_EXPORTED) {
138 error = priv_check(td, PRIV_ROOT);
143 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
145 if (priv_check(td, PRIV_ROOT))
146 uap->flags |= MNT_NOSUID | MNT_NODEV;
149 * Lookup the requested path and extract the nch and vnode.
151 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
153 if ((error = nlookup(&nd)) == 0) {
154 if (nd.nl_nch.ncp->nc_vp == NULL)
164 * If the target filesystem is resolved via a nullfs mount, then
165 * nd.nl_nch.mount will be pointing to the nullfs mount structure
166 * instead of the target file system. We need it in case we are
169 nullmp = nd.nl_nch.mount;
172 * Extract the locked+refd ncp and cleanup the nd structure
175 cache_zero(&nd.nl_nch);
178 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
179 (mp = cache_findmount(&nch)) != NULL) {
188 * now we have the locked ref'd nch and unreferenced vnode.
191 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
198 * Extract the file system type. We need to know this early, to take
199 * appropriate actions if we are dealing with a nullfs.
201 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
208 * Now we have an unlocked ref'd nch and a locked ref'd vp
210 if (uap->flags & MNT_UPDATE) {
211 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
218 if (strncmp(fstypename, "null", 5) == 0) {
226 flag2 = mp->mnt_kern_flag;
228 * We only allow the filesystem to be reloaded if it
229 * is currently mounted read-only.
231 if ((uap->flags & MNT_RELOAD) &&
232 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
235 error = EOPNOTSUPP; /* Needs translation */
239 * Only root, or the user that did the original mount is
240 * permitted to update it.
242 if (mp->mnt_stat.f_owner != cred->cr_uid &&
243 (error = priv_check(td, PRIV_ROOT))) {
248 if (vfs_busy(mp, LK_NOWAIT)) {
262 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
263 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 mp->mnt_pbuf_count = nswbuf_kva / NSWBUF_SPLIT;
343 vfsp->vfc_refcount++;
344 mp->mnt_stat.f_type = vfsp->vfc_typenum;
345 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
346 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
347 mp->mnt_stat.f_owner = cred->cr_uid;
348 lwkt_gettoken(&mp->mnt_token);
352 * (per-mount token acquired at this point)
354 * Set the mount level flags.
356 if (uap->flags & MNT_RDONLY)
357 mp->mnt_flag |= MNT_RDONLY;
358 else if (mp->mnt_flag & MNT_RDONLY)
359 mp->mnt_kern_flag |= MNTK_WANTRDWR;
360 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
361 MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME |
362 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
363 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
365 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
366 MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE |
367 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
368 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
372 * Pre-set the mount's ALL_MPSAFE flags if specified in the vfsconf.
373 * This way the initial VFS_MOUNT() call will also be MPSAFE.
375 if (vfsp->vfc_flags & VFCF_MPSAFE)
376 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
379 * Mount the filesystem.
380 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
383 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
384 if (mp->mnt_flag & MNT_UPDATE) {
385 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
386 mp->mnt_flag &= ~MNT_RDONLY;
387 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
388 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
391 mp->mnt_kern_flag = flag2;
393 lwkt_reltoken(&mp->mnt_token);
399 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
402 * Put the new filesystem on the mount list after root. The mount
403 * point gets its own mnt_ncmountpt (unless the VFS already set one
404 * up) which represents the root of the mount. The lookup code
405 * detects the mount point going forward and checks the root of
406 * the mount going backwards.
408 * It is not necessary to invalidate or purge the vnode underneath
409 * because elements under the mount will be given their own glue
413 if (mp->mnt_ncmountpt.ncp == NULL) {
415 * Allocate, then unlock, but leave the ref intact.
416 * This is the mnt_refs (1) that we will retain
417 * through to the unmount.
419 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
420 cache_unlock(&mp->mnt_ncmountpt);
423 mp->mnt_ncmounton = nch; /* inherits ref */
425 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
427 cache_ismounting(mp);
428 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
430 mountlist_insert(mp, MNTINS_LAST);
432 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
433 error = vfs_allocate_syncvnode(mp);
434 lwkt_reltoken(&mp->mnt_token);
436 error = VFS_START(mp, 0);
438 KNOTE(&fs_klist, VQ_MOUNT);
440 vn_syncer_thr_stop(mp);
441 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
442 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
443 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
444 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
445 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
446 mp->mnt_vfc->vfc_refcount--;
447 lwkt_reltoken(&mp->mnt_token);
458 * Scan all active processes to see if any of them have a current
459 * or root directory onto which the new filesystem has just been
460 * mounted. If so, replace them with the new mount point.
462 * Both old_nch and new_nch are ref'd on call but not locked.
463 * new_nch must be temporarily locked so it can be associated with the
464 * vnode representing the root of the mount point.
466 struct checkdirs_info {
467 struct nchandle old_nch;
468 struct nchandle new_nch;
469 struct vnode *old_vp;
470 struct vnode *new_vp;
473 static int checkdirs_callback(struct proc *p, void *data);
476 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
478 struct checkdirs_info info;
484 * If the old mount point's vnode has a usecount of 1, it is not
485 * being held as a descriptor anywhere.
487 olddp = old_nch->ncp->nc_vp;
488 if (olddp == NULL || VREFCNT(olddp) == 1)
492 * Force the root vnode of the new mount point to be resolved
493 * so we can update any matching processes.
496 if (VFS_ROOT(mp, &newdp))
497 panic("mount: lost mount");
500 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
501 cache_setunresolved(new_nch);
502 cache_setvp(new_nch, newdp);
503 cache_unlock(new_nch);
506 * Special handling of the root node
508 if (rootvnode == olddp) {
510 vfs_cache_setroot(newdp, cache_hold(new_nch));
514 * Pass newdp separately so the callback does not have to access
515 * it via new_nch->ncp->nc_vp.
517 info.old_nch = *old_nch;
518 info.new_nch = *new_nch;
520 allproc_scan(checkdirs_callback, &info, 0);
525 * NOTE: callback is not MP safe because the scanned process's filedesc
526 * structure can be ripped out from under us, amoung other things.
529 checkdirs_callback(struct proc *p, void *data)
531 struct checkdirs_info *info = data;
532 struct filedesc *fdp;
533 struct nchandle ncdrop1;
534 struct nchandle ncdrop2;
535 struct vnode *vprele1;
536 struct vnode *vprele2;
538 if ((fdp = p->p_fd) != NULL) {
539 cache_zero(&ncdrop1);
540 cache_zero(&ncdrop2);
545 * MPUNSAFE - XXX fdp can be pulled out from under a
548 * A shared filedesc is ok, we don't have to copy it
549 * because we are making this change globally.
551 spin_lock(&fdp->fd_spin);
552 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
553 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
554 vprele1 = fdp->fd_cdir;
556 fdp->fd_cdir = info->new_vp;
557 ncdrop1 = fdp->fd_ncdir;
558 cache_copy(&info->new_nch, &fdp->fd_ncdir);
560 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
561 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
562 vprele2 = fdp->fd_rdir;
564 fdp->fd_rdir = info->new_vp;
565 ncdrop2 = fdp->fd_nrdir;
566 cache_copy(&info->new_nch, &fdp->fd_nrdir);
568 spin_unlock(&fdp->fd_spin);
570 cache_drop(&ncdrop1);
572 cache_drop(&ncdrop2);
582 * Unmount a file system.
584 * Note: unmount takes a path to the vnode mounted on as argument,
585 * not special file (as before).
587 * umount_args(char *path, int flags)
592 sys_unmount(struct unmount_args *uap)
594 struct thread *td = curthread;
595 struct proc *p __debugvar = td->td_proc;
596 struct mount *mp = NULL;
597 struct nlookupdata nd;
601 if (td->td_ucred->cr_prison != NULL) {
605 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
608 error = nlookup_init(&nd, uap->path, UIO_USERSPACE,
609 NLC_FOLLOW | NLC_IGNBADDIR);
611 error = nlookup(&nd);
615 mp = nd.nl_nch.mount;
618 * Only root, or the user that did the original mount is
619 * permitted to unmount this filesystem.
621 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
622 (error = priv_check(td, PRIV_ROOT)))
626 * Don't allow unmounting the root file system.
628 if (mp->mnt_flag & MNT_ROOTFS) {
634 * Must be the root of the filesystem
636 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
642 * If no error try to issue the unmount. We lose our cache
643 * ref when we call nlookup_done so we must hold the mount point
644 * to prevent use-after-free races.
650 error = dounmount(mp, uap->flags, 0);
660 * Do the actual file system unmount (interlocked against the mountlist
661 * token and mp->mnt_token).
664 dounmount_interlock(struct mount *mp)
666 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
668 mp->mnt_kern_flag |= MNTK_UNMOUNT;
673 unmount_allproc_cb(struct proc *p, void *arg)
677 if (p->p_textnch.ncp == NULL)
680 mp = (struct mount *)arg;
681 if (p->p_textnch.mount == mp)
682 cache_drop(&p->p_textnch);
688 * The guts of the unmount code. The mount owns one ref and one hold
689 * count. If we successfully interlock the unmount, those refs are ours.
690 * (The ref is from mnt_ncmountpt).
692 * When halting we shortcut certain mount types such as devfs by not actually
693 * issuing the VFS_SYNC() or VFS_UNMOUNT(). They are still disconnected
694 * from the mountlist so higher-level filesytems can unmount cleanly.
696 * The mount types that allow QUICKHALT are: devfs, tmpfs, procfs.
699 dounmount(struct mount *mp, int flags, int halting)
701 struct namecache *ncp;
712 lwkt_gettoken(&mp->mnt_token);
715 * When halting, certain mount points can essentially just
716 * be unhooked and otherwise ignored.
718 if (halting && (mp->mnt_kern_flag & MNTK_QUICKHALT)) {
727 * Exclusive access for unmounting purposes.
729 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
733 * We now 'own' the last mp->mnt_refs
735 * Allow filesystems to detect that a forced unmount is in progress.
737 if (flags & MNT_FORCE)
738 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
739 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
740 error = lockmgr(&mp->mnt_lock, lflags);
742 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
743 if (mp->mnt_kern_flag & MNTK_MWAIT) {
744 mp->mnt_kern_flag &= ~MNTK_MWAIT;
750 if (mp->mnt_flag & MNT_EXPUBLIC)
751 vfs_setpublicfs(NULL, NULL, NULL);
753 vfs_msync(mp, MNT_WAIT);
754 async_flag = mp->mnt_flag & MNT_ASYNC;
755 mp->mnt_flag &=~ MNT_ASYNC;
758 * Decomission our special mnt_syncer vnode. This also stops
759 * the vnlru code. If we are unable to unmount we recommission
762 * Then sync the filesystem.
764 if ((vp = mp->mnt_syncer) != NULL) {
765 mp->mnt_syncer = NULL;
766 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
772 * Sync normally-mounted filesystem.
774 if (quickhalt == 0) {
775 if ((mp->mnt_flag & MNT_RDONLY) == 0)
776 VFS_SYNC(mp, MNT_WAIT);
780 * nchandle records ref the mount structure. Expect a count of 1
781 * (our mount->mnt_ncmountpt).
783 * Scans can get temporary refs on a mountpoint (thought really
784 * heavy duty stuff like cache_findmount() do not).
786 for (retry = 0; (retry < 10 || debug_unmount); ++retry) {
788 * Invalidate the namecache topology under the mount.
789 * nullfs mounts alias a real mount's namecache topology
790 * and it should not be invalidated in that case.
792 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
793 cache_lock(&mp->mnt_ncmountpt);
794 cache_inval(&mp->mnt_ncmountpt,
795 CINV_DESTROY|CINV_CHILDREN);
796 cache_unlock(&mp->mnt_ncmountpt);
802 cache_unmounting(mp);
803 if (mp->mnt_refs != 1)
804 cache_clearmntcache();
807 * Break out if we are good. Don't count ncp refs if the
810 ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ?
811 NULL : mp->mnt_ncmountpt.ncp;
812 if (mp->mnt_refs == 1 &&
813 (ncp == NULL || (ncp->nc_refs == 1 &&
814 TAILQ_FIRST(&ncp->nc_list) == NULL))) {
819 * If forcing the unmount, clean out any p->p_textnch
820 * nchandles that match this mount.
822 if (flags & MNT_FORCE)
823 allproc_scan(&unmount_allproc_cb, mp, 0);
828 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
829 if ((retry & 15) == 15) {
831 "(%p) debug - retry %d, "
832 "%d namecache refs, %d mount refs",
834 (ncp ? ncp->nc_refs - 1 : 0),
840 ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ?
841 NULL : mp->mnt_ncmountpt.ncp;
842 if (mp->mnt_refs != 1 ||
843 (ncp != NULL && (ncp->nc_refs != 1 ||
844 TAILQ_FIRST(&ncp->nc_list)))) {
846 "(%p): %d namecache refs, %d mount refs "
849 (ncp ? ncp->nc_refs - 1 : 0),
851 if (flags & MNT_FORCE) {
853 mount_warning(mp, "forcing unmount\n");
860 * So far so good, sync the filesystem once more and
861 * call the VFS unmount code if the sync succeeds.
863 if (error == 0 && quickhalt == 0) {
864 if (mp->mnt_flag & MNT_RDONLY) {
865 error = VFS_UNMOUNT(mp, flags);
867 error = VFS_SYNC(mp, MNT_WAIT);
868 if (error == 0 || /* no error */
869 error == EOPNOTSUPP || /* no sync avail */
870 (flags & MNT_FORCE)) { /* force anyway */
871 error = VFS_UNMOUNT(mp, flags);
876 "(%p) unmount: vfs refused to unmount, "
883 * If an error occurred we can still recover, restoring the
884 * syncer vnode and misc flags.
887 if (mp->mnt_syncer == NULL && hadsyncer)
888 vfs_allocate_syncvnode(mp);
889 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
890 mp->mnt_flag |= async_flag;
891 lockmgr(&mp->mnt_lock, LK_RELEASE);
892 if (mp->mnt_kern_flag & MNTK_MWAIT) {
893 mp->mnt_kern_flag &= ~MNTK_MWAIT;
899 * Clean up any journals still associated with the mount after
900 * filesystem activity has ceased.
902 journal_remove_all_journals(mp,
903 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
905 mountlist_remove(mp);
908 * Remove any installed vnode ops here so the individual VFSs don't
911 * mnt_refs should go to zero when we scrap mnt_ncmountpt.
913 * When quickhalting we have to keep these intact because the
914 * underlying vnodes have not been destroyed, and some might be
917 if (quickhalt == 0) {
918 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
919 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
920 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
921 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
922 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
925 if (mp->mnt_ncmountpt.ncp != NULL) {
926 nch = mp->mnt_ncmountpt;
927 cache_zero(&mp->mnt_ncmountpt);
928 cache_clrmountpt(&nch);
931 if (mp->mnt_ncmounton.ncp != NULL) {
932 cache_unmounting(mp);
933 nch = mp->mnt_ncmounton;
934 cache_zero(&mp->mnt_ncmounton);
935 cache_clrmountpt(&nch);
939 mp->mnt_vfc->vfc_refcount--;
942 * If not quickhalting the mount, we expect there to be no
945 if (quickhalt == 0 && !TAILQ_EMPTY(&mp->mnt_nvnodelist))
946 panic("unmount: dangling vnode");
951 lockmgr(&mp->mnt_lock, LK_RELEASE);
952 if (mp->mnt_kern_flag & MNTK_MWAIT) {
953 mp->mnt_kern_flag &= ~MNTK_MWAIT;
958 * If we reach here and freeok != 0 we must free the mount.
959 * mnt_refs should already have dropped to 0, so if it is not
960 * zero we must cycle the caches and wait.
962 * When we are satisfied that the mount has disconnected we can
963 * drop the hold on the mp that represented the mount (though the
964 * caller might actually have another, so the caller's drop may
965 * do the actual free).
968 if (mp->mnt_refs > 0)
969 cache_clearmntcache();
970 while (mp->mnt_refs > 0) {
971 cache_unmounting(mp);
973 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
974 cache_clearmntcache();
976 lwkt_reltoken(&mp->mnt_token);
980 cache_clearmntcache();
983 KNOTE(&fs_klist, VQ_UNMOUNT);
986 lwkt_reltoken(&mp->mnt_token);
992 mount_warning(struct mount *mp, const char *ctl, ...)
999 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
1000 &ptr, &buf, 0) == 0) {
1001 kprintf("unmount(%s): ", ptr);
1006 kprintf("unmount(%p", mp);
1007 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
1008 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
1017 * Shim cache_fullpath() to handle the case where a process is chrooted into
1018 * a subdirectory of a mount. In this case if the root mount matches the
1019 * process root directory's mount we have to specify the process's root
1020 * directory instead of the mount point, because the mount point might
1021 * be above the root directory.
1025 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
1027 struct nchandle *nch;
1029 if (p && p->p_fd->fd_nrdir.mount == mp)
1030 nch = &p->p_fd->fd_nrdir;
1032 nch = &mp->mnt_ncmountpt;
1033 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
1037 * Sync each mounted filesystem.
1041 static int syncprt = 0;
1042 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
1045 static int sync_callback(struct mount *mp, void *data);
1048 sys_sync(struct sync_args *uap)
1050 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
1056 sync_callback(struct mount *mp, void *data __unused)
1060 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1061 lwkt_gettoken(&mp->mnt_token);
1062 asyncflag = mp->mnt_flag & MNT_ASYNC;
1063 mp->mnt_flag &= ~MNT_ASYNC;
1064 lwkt_reltoken(&mp->mnt_token);
1065 vfs_msync(mp, MNT_NOWAIT);
1066 VFS_SYNC(mp, MNT_NOWAIT);
1067 lwkt_gettoken(&mp->mnt_token);
1068 mp->mnt_flag |= asyncflag;
1069 lwkt_reltoken(&mp->mnt_token);
1074 /* XXX PRISON: could be per prison flag */
1075 static int prison_quotas;
1077 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
1081 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
1083 * Change filesystem quotas.
1088 sys_quotactl(struct quotactl_args *uap)
1090 struct nlookupdata nd;
1096 if (td->td_ucred->cr_prison && !prison_quotas) {
1101 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1103 error = nlookup(&nd);
1105 mp = nd.nl_nch.mount;
1106 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
1107 uap->arg, nd.nl_cred);
1115 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1116 * void *buf, int buflen)
1118 * This function operates on a mount point and executes the specified
1119 * operation using the specified control data, and possibly returns data.
1121 * The actual number of bytes stored in the result buffer is returned, 0
1122 * if none, otherwise an error is returned.
1127 sys_mountctl(struct mountctl_args *uap)
1129 struct thread *td = curthread;
1137 * Sanity and permissions checks. We must be root.
1139 if (td->td_ucred->cr_prison != NULL)
1141 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1142 (error = priv_check(td, PRIV_ROOT)) != 0)
1146 * Argument length checks
1148 if (uap->ctllen < 0 || uap->ctllen > 1024)
1150 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1152 if (uap->path == NULL)
1156 * Allocate the necessary buffers and copyin data
1158 path = objcache_get(namei_oc, M_WAITOK);
1159 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1164 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1165 error = copyin(uap->ctl, ctl, uap->ctllen);
1170 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1173 * Validate the descriptor
1176 fp = holdfp(td, uap->fd, -1);
1186 * Execute the internal kernel function and clean up.
1188 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen,
1189 buf, uap->buflen, &uap->sysmsg_result);
1191 dropfp(td, uap->fd, fp);
1192 if (error == 0 && uap->sysmsg_result > 0)
1193 error = copyout(buf, uap->buf, uap->sysmsg_result);
1196 objcache_put(namei_oc, path);
1205 * Execute a mount control operation by resolving the path to a mount point
1206 * and calling vop_mountctl().
1208 * Use the mount point from the nch instead of the vnode so nullfs mounts
1209 * can properly spike the VOP.
1212 kern_mountctl(const char *path, int op, struct file *fp,
1213 const void *ctl, int ctllen,
1214 void *buf, int buflen, int *res)
1217 struct nlookupdata nd;
1218 struct nchandle nch;
1224 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1227 error = nlookup(&nd);
1232 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1239 * Yes, all this is needed to use the nch.mount below, because
1240 * we must maintain a ref on the mount to avoid ripouts (e.g.
1241 * due to heavy mount/unmount use by synth or poudriere).
1244 cache_zero(&nd.nl_nch);
1252 * Must be the root of the filesystem
1254 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1259 if (mp == NULL || mp->mnt_kern_flag & MNTK_UNMOUNT) {
1260 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1266 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1275 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1277 struct thread *td = curthread;
1278 struct proc *p = td->td_proc;
1281 char *fullpath, *freepath;
1284 if ((error = nlookup(nd)) != 0)
1286 mp = nd->nl_nch.mount;
1290 * Ignore refresh error, user should have visibility.
1291 * This can happen if a NFS mount goes bad (e.g. server
1292 * revokes perms or goes down).
1294 error = VFS_STATFS(mp, sp, nd->nl_cred);
1297 error = mount_path(p, mp, &fullpath, &freepath);
1300 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1301 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1302 kfree(freepath, M_TEMP);
1304 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1305 bcopy(sp, buf, sizeof(*buf));
1306 /* Only root should have access to the fsid's. */
1307 if (priv_check(td, PRIV_ROOT))
1308 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1313 * statfs_args(char *path, struct statfs *buf)
1315 * Get filesystem statistics.
1318 sys_statfs(struct statfs_args *uap)
1320 struct nlookupdata nd;
1324 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1326 error = kern_statfs(&nd, &buf);
1329 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1334 kern_fstatfs(int fd, struct statfs *buf)
1336 struct thread *td = curthread;
1337 struct proc *p = td->td_proc;
1341 char *fullpath, *freepath;
1345 if ((error = holdvnode(td, fd, &fp)) != 0)
1349 * Try to use mount info from any overlays rather than the
1350 * mount info for the underlying vnode, otherwise we will
1351 * fail when operating on null-mounted paths inside a chroot.
1353 if ((mp = fp->f_nchandle.mount) == NULL)
1354 mp = ((struct vnode *)fp->f_data)->v_mount;
1359 if (fp->f_cred == NULL) {
1365 * Ignore refresh error, user should have visibility.
1366 * This can happen if a NFS mount goes bad (e.g. server
1367 * revokes perms or goes down).
1370 error = VFS_STATFS(mp, sp, fp->f_cred);
1372 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1374 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1375 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1376 kfree(freepath, M_TEMP);
1378 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1379 bcopy(sp, buf, sizeof(*buf));
1381 /* Only root should have access to the fsid's. */
1382 if (priv_check(td, PRIV_ROOT))
1383 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1391 * fstatfs_args(int fd, struct statfs *buf)
1393 * Get filesystem statistics.
1396 sys_fstatfs(struct fstatfs_args *uap)
1401 error = kern_fstatfs(uap->fd, &buf);
1404 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1409 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1415 if ((error = nlookup(nd)) != 0)
1417 mp = nd->nl_nch.mount;
1418 sp = &mp->mnt_vstat;
1419 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1423 if (mp->mnt_flag & MNT_RDONLY)
1424 sp->f_flag |= ST_RDONLY;
1425 if (mp->mnt_flag & MNT_NOSUID)
1426 sp->f_flag |= ST_NOSUID;
1427 bcopy(sp, buf, sizeof(*buf));
1432 * statfs_args(char *path, struct statfs *buf)
1434 * Get filesystem statistics.
1437 sys_statvfs(struct statvfs_args *uap)
1439 struct nlookupdata nd;
1443 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1445 error = kern_statvfs(&nd, &buf);
1448 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1453 kern_fstatvfs(int fd, struct statvfs *buf)
1455 struct thread *td = curthread;
1461 if ((error = holdvnode(td, fd, &fp)) != 0)
1463 if ((mp = fp->f_nchandle.mount) == NULL)
1464 mp = ((struct vnode *)fp->f_data)->v_mount;
1469 if (fp->f_cred == NULL) {
1473 sp = &mp->mnt_vstat;
1474 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1478 if (mp->mnt_flag & MNT_RDONLY)
1479 sp->f_flag |= ST_RDONLY;
1480 if (mp->mnt_flag & MNT_NOSUID)
1481 sp->f_flag |= ST_NOSUID;
1483 bcopy(sp, buf, sizeof(*buf));
1491 * fstatfs_args(int fd, struct statfs *buf)
1493 * Get filesystem statistics.
1496 sys_fstatvfs(struct fstatvfs_args *uap)
1501 error = kern_fstatvfs(uap->fd, &buf);
1504 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1509 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1511 * Get statistics on all filesystems.
1514 struct getfsstat_info {
1515 struct statfs *sfsp;
1523 static int getfsstat_callback(struct mount *, void *);
1526 sys_getfsstat(struct getfsstat_args *uap)
1528 struct thread *td = curthread;
1529 struct getfsstat_info info;
1531 bzero(&info, sizeof(info));
1533 info.maxcount = uap->bufsize / sizeof(struct statfs);
1534 info.sfsp = uap->buf;
1536 info.flags = uap->flags;
1539 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1540 if (info.sfsp && info.count > info.maxcount)
1541 uap->sysmsg_result = info.maxcount;
1543 uap->sysmsg_result = info.count;
1544 return (info.error);
1548 getfsstat_callback(struct mount *mp, void *data)
1550 struct getfsstat_info *info = data;
1556 if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1559 if (info->sfsp && info->count < info->maxcount) {
1563 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1564 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1565 * overrides MNT_WAIT.
1567 * Ignore refresh error, user should have visibility.
1568 * This can happen if a NFS mount goes bad (e.g. server
1569 * revokes perms or goes down).
1571 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1572 (info->flags & MNT_WAIT)) &&
1573 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1576 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1578 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1580 info->error = error;
1583 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1584 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1585 kfree(freepath, M_TEMP);
1587 error = copyout(sp, info->sfsp, sizeof(*sp));
1589 info->error = error;
1599 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1600 long bufsize, int flags)
1602 * Get statistics on all filesystems.
1605 struct getvfsstat_info {
1606 struct statfs *sfsp;
1607 struct statvfs *vsfsp;
1615 static int getvfsstat_callback(struct mount *, void *);
1618 sys_getvfsstat(struct getvfsstat_args *uap)
1620 struct thread *td = curthread;
1621 struct getvfsstat_info info;
1623 bzero(&info, sizeof(info));
1625 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1626 info.sfsp = uap->buf;
1627 info.vsfsp = uap->vbuf;
1629 info.flags = uap->flags;
1632 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1633 if (info.vsfsp && info.count > info.maxcount)
1634 uap->sysmsg_result = info.maxcount;
1636 uap->sysmsg_result = info.count;
1637 return (info.error);
1641 getvfsstat_callback(struct mount *mp, void *data)
1643 struct getvfsstat_info *info = data;
1645 struct statvfs *vsp;
1650 if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1653 if (info->vsfsp && info->count < info->maxcount) {
1655 vsp = &mp->mnt_vstat;
1658 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1659 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1660 * overrides MNT_WAIT.
1662 * Ignore refresh error, user should have visibility.
1663 * This can happen if a NFS mount goes bad (e.g. server
1664 * revokes perms or goes down).
1666 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1667 (info->flags & MNT_WAIT)) &&
1668 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1671 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1673 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1674 (info->flags & MNT_WAIT)) &&
1675 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1679 if (mp->mnt_flag & MNT_RDONLY)
1680 vsp->f_flag |= ST_RDONLY;
1681 if (mp->mnt_flag & MNT_NOSUID)
1682 vsp->f_flag |= ST_NOSUID;
1684 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1686 info->error = error;
1689 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1690 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1691 kfree(freepath, M_TEMP);
1693 error = copyout(sp, info->sfsp, sizeof(*sp));
1695 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1697 info->error = error;
1709 * fchdir_args(int fd)
1711 * Change current working directory to a given file descriptor.
1714 sys_fchdir(struct fchdir_args *uap)
1716 struct thread *td = curthread;
1717 struct proc *p = td->td_proc;
1718 struct filedesc *fdp = p->p_fd;
1719 struct vnode *vp, *ovp;
1722 struct nchandle nch, onch, tnch;
1725 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
1727 lwkt_gettoken(&p->p_token);
1728 vp = (struct vnode *)fp->f_data;
1730 vn_lock(vp, LK_SHARED | LK_RETRY);
1731 if (fp->f_nchandle.ncp == NULL)
1734 error = checkvp_chdir(vp, td);
1739 cache_copy(&fp->f_nchandle, &nch);
1742 * If the ncp has become a mount point, traverse through
1746 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1747 (mp = cache_findmount(&nch)) != NULL
1749 error = nlookup_mp(mp, &tnch);
1751 cache_unlock(&tnch); /* leave ref intact */
1753 vp = tnch.ncp->nc_vp;
1754 error = vget(vp, LK_SHARED);
1755 KKASSERT(error == 0);
1759 cache_dropmount(mp);
1762 spin_lock(&fdp->fd_spin);
1764 onch = fdp->fd_ncdir;
1766 fdp->fd_ncdir = nch;
1767 spin_unlock(&fdp->fd_spin);
1768 vn_unlock(vp); /* leave ref intact */
1777 lwkt_reltoken(&p->p_token);
1782 kern_chdir(struct nlookupdata *nd)
1784 struct thread *td = curthread;
1785 struct proc *p = td->td_proc;
1786 struct filedesc *fdp = p->p_fd;
1787 struct vnode *vp, *ovp;
1788 struct nchandle onch;
1791 nd->nl_flags |= NLC_SHAREDLOCK;
1792 if ((error = nlookup(nd)) != 0)
1794 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1796 if ((error = vget(vp, LK_SHARED)) != 0)
1799 lwkt_gettoken(&p->p_token);
1800 error = checkvp_chdir(vp, td);
1803 spin_lock(&fdp->fd_spin);
1805 onch = fdp->fd_ncdir;
1806 fdp->fd_ncdir = nd->nl_nch;
1808 spin_unlock(&fdp->fd_spin);
1809 cache_unlock(&nd->nl_nch); /* leave reference intact */
1812 cache_zero(&nd->nl_nch);
1816 lwkt_reltoken(&p->p_token);
1821 * chdir_args(char *path)
1823 * Change current working directory (``.'').
1826 sys_chdir(struct chdir_args *uap)
1828 struct nlookupdata nd;
1831 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1833 error = kern_chdir(&nd);
1839 * Helper function for raised chroot(2) security function: Refuse if
1840 * any filedescriptors are open directories.
1843 chroot_refuse_vdir_fds(thread_t td, struct filedesc *fdp)
1850 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1851 if ((error = holdvnode(td, fd, &fp)) != 0)
1853 vp = (struct vnode *)fp->f_data;
1854 if (vp->v_type != VDIR) {
1865 * This sysctl determines if we will allow a process to chroot(2) if it
1866 * has a directory open:
1867 * 0: disallowed for all processes.
1868 * 1: allowed for processes that were not already chroot(2)'ed.
1869 * 2: allowed for all processes.
1872 static int chroot_allow_open_directories = 1;
1874 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1875 &chroot_allow_open_directories, 0, "");
1878 * chroot to the specified namecache entry. We obtain the vp from the
1879 * namecache data. The passed ncp must be locked and referenced and will
1880 * remain locked and referenced on return.
1883 kern_chroot(struct nchandle *nch)
1885 struct thread *td = curthread;
1886 struct proc *p = td->td_proc;
1887 struct filedesc *fdp = p->p_fd;
1892 * Only privileged user can chroot
1894 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1899 * Disallow open directory descriptors (fchdir() breakouts).
1901 if (chroot_allow_open_directories == 0 ||
1902 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1903 if ((error = chroot_refuse_vdir_fds(td, fdp)) != 0)
1906 if ((vp = nch->ncp->nc_vp) == NULL)
1909 if ((error = vget(vp, LK_SHARED)) != 0)
1913 * Check the validity of vp as a directory to change to and
1914 * associate it with rdir/jdir.
1916 error = checkvp_chdir(vp, td);
1917 vn_unlock(vp); /* leave reference intact */
1919 lwkt_gettoken(&p->p_token);
1920 vrele(fdp->fd_rdir);
1921 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1922 cache_drop(&fdp->fd_nrdir);
1923 cache_copy(nch, &fdp->fd_nrdir);
1924 if (fdp->fd_jdir == NULL) {
1927 cache_copy(nch, &fdp->fd_njdir);
1929 if ((p->p_flags & P_DIDCHROOT) == 0) {
1930 p->p_flags |= P_DIDCHROOT;
1931 if (p->p_depth <= 65535 - 32)
1934 lwkt_reltoken(&p->p_token);
1942 * chroot_args(char *path)
1944 * Change notion of root (``/'') directory.
1947 sys_chroot(struct chroot_args *uap)
1949 struct thread *td __debugvar = curthread;
1950 struct nlookupdata nd;
1953 KKASSERT(td->td_proc);
1954 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1956 nd.nl_flags |= NLC_EXEC;
1957 error = nlookup(&nd);
1959 error = kern_chroot(&nd.nl_nch);
1966 sys_chroot_kernel(struct chroot_kernel_args *uap)
1968 struct thread *td = curthread;
1969 struct nlookupdata nd;
1970 struct nchandle *nch;
1974 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1978 error = nlookup(&nd);
1984 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1988 if ((vp = nch->ncp->nc_vp) == NULL) {
1993 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1996 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1997 vfs_cache_setroot(vp, cache_hold(nch));
2006 * Common routine for chroot and chdir. Given a locked, referenced vnode,
2007 * determine whether it is legal to chdir to the vnode. The vnode's state
2008 * is not changed by this call.
2011 checkvp_chdir(struct vnode *vp, struct thread *td)
2015 if (vp->v_type != VDIR)
2018 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
2023 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
2025 struct thread *td = curthread;
2026 struct proc *p = td->td_proc;
2027 struct lwp *lp = td->td_lwp;
2028 struct filedesc *fdp = p->p_fd;
2033 int type, indx, error = 0;
2036 if ((oflags & O_ACCMODE) == O_ACCMODE)
2038 flags = FFLAGS(oflags);
2039 error = falloc(lp, &nfp, NULL);
2043 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
2046 * XXX p_dupfd is a real mess. It allows a device to return a
2047 * file descriptor to be duplicated rather then doing the open
2053 * Call vn_open() to do the lookup and assign the vnode to the
2054 * file pointer. vn_open() does not change the ref count on fp
2055 * and the vnode, on success, will be inherited by the file pointer
2058 * Request a shared lock on the vnode if possible.
2060 * Executable binaries can race VTEXT against O_RDWR opens, so
2061 * use an exclusive lock for O_RDWR opens as well.
2063 * NOTE: We need a flag to separate terminal vnode locking from
2064 * parent locking. O_CREAT needs parent locking, but O_TRUNC
2065 * and O_RDWR only need to lock the terminal vnode exclusively.
2067 nd->nl_flags |= NLC_LOCKVP;
2068 if ((flags & (O_CREAT|O_TRUNC|O_RDWR)) == 0)
2069 nd->nl_flags |= NLC_SHAREDLOCK;
2071 error = vn_open(nd, fp, flags, cmode);
2076 * handle special fdopen() case. bleh. dupfdopen() is
2077 * responsible for dropping the old contents of ofiles[indx]
2080 * Note that fsetfd() will add a ref to fp which represents
2081 * the fd_files[] assignment. We must still drop our
2084 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
2085 if (fdalloc(p, 0, &indx) == 0) {
2086 error = dupfdopen(td, indx, lp->lwp_dupfd, flags, error);
2089 fdrop(fp); /* our ref */
2092 fsetfd(fdp, NULL, indx);
2095 fdrop(fp); /* our ref */
2096 if (error == ERESTART)
2102 * ref the vnode for ourselves so it can't be ripped out from under
2103 * is. XXX need an ND flag to request that the vnode be returned
2106 * Reserve a file descriptor but do not assign it until the open
2109 vp = (struct vnode *)fp->f_data;
2111 if ((error = fdalloc(p, 0, &indx)) != 0) {
2118 * If no error occurs the vp will have been assigned to the file
2123 if (flags & (O_EXLOCK | O_SHLOCK)) {
2124 lf.l_whence = SEEK_SET;
2127 if (flags & O_EXLOCK)
2128 lf.l_type = F_WRLCK;
2130 lf.l_type = F_RDLCK;
2131 if (flags & FNONBLOCK)
2136 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2138 * lock request failed. Clean up the reserved
2142 fsetfd(fdp, NULL, indx);
2146 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
2150 * Assert that all regular file vnodes were created with a object.
2152 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
2153 ("open: regular file has no backing object after vn_open"));
2159 * release our private reference, leaving the one associated with the
2160 * descriptor table intact.
2162 if (oflags & O_CLOEXEC)
2163 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
2164 fsetfd(fdp, fp, indx);
2172 * open_args(char *path, int flags, int mode)
2174 * Check permissions, allocate an open file structure,
2175 * and call the device open routine if any.
2178 sys_open(struct open_args *uap)
2180 struct nlookupdata nd;
2183 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2185 error = kern_open(&nd, uap->flags,
2186 uap->mode, &uap->sysmsg_result);
2193 * openat_args(int fd, char *path, int flags, int mode)
2196 sys_openat(struct openat_args *uap)
2198 struct nlookupdata nd;
2202 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2204 error = kern_open(&nd, uap->flags, uap->mode,
2205 &uap->sysmsg_result);
2207 nlookup_done_at(&nd, fp);
2212 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2214 struct thread *td = curthread;
2215 struct proc *p = td->td_proc;
2224 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2225 vattr.va_rmajor = rmajor;
2226 vattr.va_rminor = rminor;
2228 switch (mode & S_IFMT) {
2229 case S_IFMT: /* used by badsect to flag bad sectors */
2230 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2231 vattr.va_type = VBAD;
2234 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2235 vattr.va_type = VCHR;
2238 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2239 vattr.va_type = VBLK;
2242 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2245 case S_IFDIR: /* special directories support for HAMMER */
2246 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2247 vattr.va_type = VDIR;
2258 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2259 if ((error = nlookup(nd)) != 0)
2261 if (nd->nl_nch.ncp->nc_vp)
2263 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2267 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2268 nd->nl_cred, NAMEI_CREATE);
2271 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2272 &vp, nd->nl_cred, &vattr);
2280 * mknod_args(char *path, int mode, int dev)
2282 * Create a special file.
2285 sys_mknod(struct mknod_args *uap)
2287 struct nlookupdata nd;
2290 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2292 error = kern_mknod(&nd, uap->mode,
2293 umajor(uap->dev), uminor(uap->dev));
2300 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2302 * Create a special file. The path is relative to the directory associated
2306 sys_mknodat(struct mknodat_args *uap)
2308 struct nlookupdata nd;
2312 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2314 error = kern_mknod(&nd, uap->mode,
2315 umajor(uap->dev), uminor(uap->dev));
2317 nlookup_done_at(&nd, fp);
2322 kern_mkfifo(struct nlookupdata *nd, int mode)
2324 struct thread *td = curthread;
2325 struct proc *p = td->td_proc;
2332 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2333 if ((error = nlookup(nd)) != 0)
2335 if (nd->nl_nch.ncp->nc_vp)
2337 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2341 vattr.va_type = VFIFO;
2342 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2344 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2351 * mkfifo_args(char *path, int mode)
2353 * Create a named pipe.
2356 sys_mkfifo(struct mkfifo_args *uap)
2358 struct nlookupdata nd;
2361 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2363 error = kern_mkfifo(&nd, uap->mode);
2369 * mkfifoat_args(int fd, char *path, mode_t mode)
2371 * Create a named pipe. The path is relative to the directory associated
2375 sys_mkfifoat(struct mkfifoat_args *uap)
2377 struct nlookupdata nd;
2381 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2383 error = kern_mkfifo(&nd, uap->mode);
2384 nlookup_done_at(&nd, fp);
2388 static int hardlink_check_uid = 0;
2389 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2390 &hardlink_check_uid, 0,
2391 "Unprivileged processes cannot create hard links to files owned by other "
2393 static int hardlink_check_gid = 0;
2394 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2395 &hardlink_check_gid, 0,
2396 "Unprivileged processes cannot create hard links to files owned by other "
2400 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2406 * Shortcut if disabled
2408 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2412 * Privileged user can always hardlink
2414 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2418 * Otherwise only if the originating file is owned by the
2419 * same user or group. Note that any group is allowed if
2420 * the file is owned by the caller.
2422 error = VOP_GETATTR(vp, &va);
2426 if (hardlink_check_uid) {
2427 if (cred->cr_uid != va.va_uid)
2431 if (hardlink_check_gid) {
2432 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2440 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2442 struct thread *td = curthread;
2447 * Lookup the source and obtained a locked vnode.
2449 * You may only hardlink a file which you have write permission
2450 * on or which you own.
2452 * XXX relookup on vget failure / race ?
2455 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2456 if ((error = nlookup(nd)) != 0)
2458 vp = nd->nl_nch.ncp->nc_vp;
2459 KKASSERT(vp != NULL);
2460 if (vp->v_type == VDIR)
2461 return (EPERM); /* POSIX */
2462 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2464 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2468 * Unlock the source so we can lookup the target without deadlocking
2469 * (XXX vp is locked already, possible other deadlock?). The target
2472 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2473 nd->nl_flags &= ~NLC_NCPISLOCKED;
2474 cache_unlock(&nd->nl_nch);
2477 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2478 if ((error = nlookup(linknd)) != 0) {
2482 if (linknd->nl_nch.ncp->nc_vp) {
2486 VFS_MODIFYING(vp->v_mount);
2487 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2494 * Finally run the new API VOP.
2496 error = can_hardlink(vp, td, td->td_ucred);
2498 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2499 vp, linknd->nl_cred);
2506 * link_args(char *path, char *link)
2508 * Make a hard file link.
2511 sys_link(struct link_args *uap)
2513 struct nlookupdata nd, linknd;
2516 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2518 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2520 error = kern_link(&nd, &linknd);
2521 nlookup_done(&linknd);
2528 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2530 * Make a hard file link. The path1 argument is relative to the directory
2531 * associated with fd1, and similarly the path2 argument is relative to
2532 * the directory associated with fd2.
2535 sys_linkat(struct linkat_args *uap)
2537 struct nlookupdata nd, linknd;
2538 struct file *fp1, *fp2;
2541 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2542 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2544 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2545 uap->path2, UIO_USERSPACE, 0);
2547 error = kern_link(&nd, &linknd);
2548 nlookup_done_at(&linknd, fp2);
2550 nlookup_done_at(&nd, fp1);
2555 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2563 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2564 if ((error = nlookup(nd)) != 0)
2566 if (nd->nl_nch.ncp->nc_vp)
2568 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2572 vattr.va_mode = mode;
2573 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2580 * symlink(char *path, char *link)
2582 * Make a symbolic link.
2585 sys_symlink(struct symlink_args *uap)
2587 struct thread *td = curthread;
2588 struct nlookupdata nd;
2593 path = objcache_get(namei_oc, M_WAITOK);
2594 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2596 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2598 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2599 error = kern_symlink(&nd, path, mode);
2603 objcache_put(namei_oc, path);
2608 * symlinkat_args(char *path1, int fd, char *path2)
2610 * Make a symbolic link. The path2 argument is relative to the directory
2611 * associated with fd.
2614 sys_symlinkat(struct symlinkat_args *uap)
2616 struct thread *td = curthread;
2617 struct nlookupdata nd;
2623 path1 = objcache_get(namei_oc, M_WAITOK);
2624 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2626 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2629 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2630 error = kern_symlink(&nd, path1, mode);
2632 nlookup_done_at(&nd, fp);
2634 objcache_put(namei_oc, path1);
2639 * undelete_args(char *path)
2641 * Delete a whiteout from the filesystem.
2644 sys_undelete(struct undelete_args *uap)
2646 struct nlookupdata nd;
2649 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2651 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2653 error = nlookup(&nd);
2655 error = ncp_writechk(&nd.nl_nch);
2657 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2665 kern_unlink(struct nlookupdata *nd)
2670 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2671 if ((error = nlookup(nd)) != 0)
2673 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2675 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2680 * unlink_args(char *path)
2682 * Delete a name from the filesystem.
2685 sys_unlink(struct unlink_args *uap)
2687 struct nlookupdata nd;
2690 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2692 error = kern_unlink(&nd);
2699 * unlinkat_args(int fd, char *path, int flags)
2701 * Delete the file or directory entry pointed to by fd/path.
2704 sys_unlinkat(struct unlinkat_args *uap)
2706 struct nlookupdata nd;
2710 if (uap->flags & ~AT_REMOVEDIR)
2713 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2715 if (uap->flags & AT_REMOVEDIR)
2716 error = kern_rmdir(&nd);
2718 error = kern_unlink(&nd);
2720 nlookup_done_at(&nd, fp);
2725 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2727 struct thread *td = curthread;
2734 fp = holdfp(td, fd, -1);
2737 if (fp->f_type != DTYPE_VNODE) {
2741 vp = (struct vnode *)fp->f_data;
2745 spin_lock(&fp->f_spin);
2746 new_offset = fp->f_offset + offset;
2750 error = VOP_GETATTR(vp, &vattr);
2751 spin_lock(&fp->f_spin);
2752 new_offset = offset + vattr.va_size;
2755 new_offset = offset;
2757 spin_lock(&fp->f_spin);
2762 spin_lock(&fp->f_spin);
2767 * Validate the seek position. Negative offsets are not allowed
2768 * for regular files or directories.
2770 * Normally we would also not want to allow negative offsets for
2771 * character and block-special devices. However kvm addresses
2772 * on 64 bit architectures might appear to be negative and must
2776 if (new_offset < 0 &&
2777 (vp->v_type == VREG || vp->v_type == VDIR)) {
2780 fp->f_offset = new_offset;
2783 *res = fp->f_offset;
2784 spin_unlock(&fp->f_spin);
2792 * lseek_args(int fd, int pad, off_t offset, int whence)
2794 * Reposition read/write file offset.
2797 sys_lseek(struct lseek_args *uap)
2801 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2802 &uap->sysmsg_offset);
2808 * Check if current process can access given file. amode is a bitmask of *_OK
2809 * access bits. flags is a bitmask of AT_* flags.
2812 kern_access(struct nlookupdata *nd, int amode, int flags)
2817 if (flags & ~AT_EACCESS)
2819 nd->nl_flags |= NLC_SHAREDLOCK;
2820 if ((error = nlookup(nd)) != 0)
2823 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2827 /* Flags == 0 means only check for existence. */
2836 if ((mode & VWRITE) == 0 ||
2837 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2838 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2841 * If the file handle is stale we have to re-resolve the
2842 * entry with the ncp held exclusively. This is a hack
2845 if (error == ESTALE) {
2847 cache_unlock(&nd->nl_nch);
2848 cache_lock(&nd->nl_nch);
2849 cache_setunresolved(&nd->nl_nch);
2850 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2863 * access_args(char *path, int flags)
2865 * Check access permissions.
2868 sys_access(struct access_args *uap)
2870 struct nlookupdata nd;
2873 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2875 error = kern_access(&nd, uap->flags, 0);
2882 * eaccess_args(char *path, int flags)
2884 * Check access permissions.
2887 sys_eaccess(struct eaccess_args *uap)
2889 struct nlookupdata nd;
2892 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2894 error = kern_access(&nd, uap->flags, AT_EACCESS);
2901 * faccessat_args(int fd, char *path, int amode, int flags)
2903 * Check access permissions.
2906 sys_faccessat(struct faccessat_args *uap)
2908 struct nlookupdata nd;
2912 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2915 error = kern_access(&nd, uap->amode, uap->flags);
2916 nlookup_done_at(&nd, fp);
2921 kern_stat(struct nlookupdata *nd, struct stat *st)
2926 nd->nl_flags |= NLC_SHAREDLOCK;
2927 if ((error = nlookup(nd)) != 0)
2930 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2933 if ((error = vget(vp, LK_SHARED)) != 0)
2935 error = vn_stat(vp, st, nd->nl_cred);
2938 * If the file handle is stale we have to re-resolve the
2939 * entry with the ncp held exclusively. This is a hack
2942 if (error == ESTALE) {
2944 cache_unlock(&nd->nl_nch);
2945 cache_lock(&nd->nl_nch);
2946 cache_setunresolved(&nd->nl_nch);
2947 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2957 * stat_args(char *path, struct stat *ub)
2959 * Get file status; this version follows links.
2962 sys_stat(struct stat_args *uap)
2964 struct nlookupdata nd;
2968 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2970 error = kern_stat(&nd, &st);
2972 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2979 * lstat_args(char *path, struct stat *ub)
2981 * Get file status; this version does not follow links.
2984 sys_lstat(struct lstat_args *uap)
2986 struct nlookupdata nd;
2990 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2992 error = kern_stat(&nd, &st);
2994 error = copyout(&st, uap->ub, sizeof(*uap->ub));
3001 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
3003 * Get status of file pointed to by fd/path.
3006 sys_fstatat(struct fstatat_args *uap)
3008 struct nlookupdata nd;
3014 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3017 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3019 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3020 UIO_USERSPACE, flags);
3022 error = kern_stat(&nd, &st);
3024 error = copyout(&st, uap->sb, sizeof(*uap->sb));
3026 nlookup_done_at(&nd, fp);
3031 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
3033 struct nlookupdata nd;
3038 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
3040 error = nlookup(&nd);
3042 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3045 error = VOP_PATHCONF(vp, name, sysmsg_regp);
3052 * pathconf_Args(char *path, int name)
3054 * Get configurable pathname variables.
3057 sys_pathconf(struct pathconf_args *uap)
3059 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
3064 * lpathconf_Args(char *path, int name)
3066 * Get configurable pathname variables, but don't follow symlinks.
3069 sys_lpathconf(struct lpathconf_args *uap)
3071 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
3076 * kern_readlink isn't properly split yet. There is a copyin burried
3077 * in VOP_READLINK().
3080 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
3082 struct thread *td = curthread;
3088 nd->nl_flags |= NLC_SHAREDLOCK;
3089 if ((error = nlookup(nd)) != 0)
3091 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
3094 if (vp->v_type != VLNK) {
3097 aiov.iov_base = buf;
3098 aiov.iov_len = count;
3099 auio.uio_iov = &aiov;
3100 auio.uio_iovcnt = 1;
3101 auio.uio_offset = 0;
3102 auio.uio_rw = UIO_READ;
3103 auio.uio_segflg = UIO_USERSPACE;
3105 auio.uio_resid = count;
3106 error = VOP_READLINK(vp, &auio, td->td_ucred);
3109 *res = count - auio.uio_resid;
3114 * readlink_args(char *path, char *buf, int count)
3116 * Return target name of a symbolic link.
3119 sys_readlink(struct readlink_args *uap)
3121 struct nlookupdata nd;
3124 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3126 error = kern_readlink(&nd, uap->buf, uap->count,
3127 &uap->sysmsg_result);
3134 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3136 * Return target name of a symbolic link. The path is relative to the
3137 * directory associated with fd.
3140 sys_readlinkat(struct readlinkat_args *uap)
3142 struct nlookupdata nd;
3146 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3148 error = kern_readlink(&nd, uap->buf, uap->bufsize,
3149 &uap->sysmsg_result);
3151 nlookup_done_at(&nd, fp);
3156 setfflags(struct vnode *vp, int flags)
3158 struct thread *td = curthread;
3163 * Prevent non-root users from setting flags on devices. When
3164 * a device is reused, users can retain ownership of the device
3165 * if they are allowed to set flags and programs assume that
3166 * chown can't fail when done as root.
3168 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
3169 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
3173 * note: vget is required for any operation that might mod the vnode
3174 * so VINACTIVE is properly cleared.
3176 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3178 vattr.va_flags = flags;
3179 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3186 * chflags(char *path, int flags)
3188 * Change flags of a file given a path name.
3191 sys_chflags(struct chflags_args *uap)
3193 struct nlookupdata nd;
3198 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3200 error = nlookup(&nd);
3202 error = ncp_writechk(&nd.nl_nch);
3204 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3207 error = setfflags(vp, uap->flags);
3214 * lchflags(char *path, int flags)
3216 * Change flags of a file given a path name, but don't follow symlinks.
3219 sys_lchflags(struct lchflags_args *uap)
3221 struct nlookupdata nd;
3226 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3228 error = nlookup(&nd);
3230 error = ncp_writechk(&nd.nl_nch);
3232 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3235 error = setfflags(vp, uap->flags);
3242 * fchflags_args(int fd, int flags)
3244 * Change flags of a file given a file descriptor.
3247 sys_fchflags(struct fchflags_args *uap)
3249 struct thread *td = curthread;
3253 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3255 if (fp->f_nchandle.ncp)
3256 error = ncp_writechk(&fp->f_nchandle);
3258 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3264 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3265 * change flags given a pathname relative to a filedescriptor
3267 int sys_chflagsat(struct chflagsat_args *uap)
3269 struct nlookupdata nd;
3275 if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3278 lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3281 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags);
3283 error = nlookup(&nd);
3285 error = ncp_writechk(&nd.nl_nch);
3287 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3288 nlookup_done_at(&nd, fp);
3290 error = setfflags(vp, uap->flags);
3298 setfmode(struct vnode *vp, int mode)
3300 struct thread *td = curthread;
3305 * note: vget is required for any operation that might mod the vnode
3306 * so VINACTIVE is properly cleared.
3308 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3310 vattr.va_mode = mode & ALLPERMS;
3311 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3312 cache_inval_wxok(vp);
3319 kern_chmod(struct nlookupdata *nd, int mode)
3324 if ((error = nlookup(nd)) != 0)
3326 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3328 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3329 error = setfmode(vp, mode);
3335 * chmod_args(char *path, int mode)
3337 * Change mode of a file given path name.
3340 sys_chmod(struct chmod_args *uap)
3342 struct nlookupdata nd;
3345 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3347 error = kern_chmod(&nd, uap->mode);
3353 * lchmod_args(char *path, int mode)
3355 * Change mode of a file given path name (don't follow links.)
3358 sys_lchmod(struct lchmod_args *uap)
3360 struct nlookupdata nd;
3363 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3365 error = kern_chmod(&nd, uap->mode);
3371 * fchmod_args(int fd, int mode)
3373 * Change mode of a file given a file descriptor.
3376 sys_fchmod(struct fchmod_args *uap)
3378 struct thread *td = curthread;
3382 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3384 if (fp->f_nchandle.ncp)
3385 error = ncp_writechk(&fp->f_nchandle);
3387 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3393 * fchmodat_args(char *path, int mode)
3395 * Change mode of a file pointed to by fd/path.
3398 sys_fchmodat(struct fchmodat_args *uap)
3400 struct nlookupdata nd;
3405 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3407 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3409 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3410 UIO_USERSPACE, flags);
3412 error = kern_chmod(&nd, uap->mode);
3413 nlookup_done_at(&nd, fp);
3418 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3420 struct thread *td = curthread;
3428 * note: vget is required for any operation that might mod the vnode
3429 * so VINACTIVE is properly cleared.
3431 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3432 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3434 o_uid = vattr.va_uid;
3435 o_gid = vattr.va_gid;
3436 size = vattr.va_size;
3441 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3450 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3451 VFS_ACCOUNT(mp, uid, gid, size);
3458 kern_chown(struct nlookupdata *nd, int uid, int gid)
3463 if ((error = nlookup(nd)) != 0)
3465 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3467 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3468 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3474 * chown(char *path, int uid, int gid)
3476 * Set ownership given a path name.
3479 sys_chown(struct chown_args *uap)
3481 struct nlookupdata nd;
3484 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3486 error = kern_chown(&nd, uap->uid, uap->gid);
3492 * lchown_args(char *path, int uid, int gid)
3494 * Set ownership given a path name, do not cross symlinks.
3497 sys_lchown(struct lchown_args *uap)
3499 struct nlookupdata nd;
3502 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3504 error = kern_chown(&nd, uap->uid, uap->gid);
3510 * fchown_args(int fd, int uid, int gid)
3512 * Set ownership given a file descriptor.
3515 sys_fchown(struct fchown_args *uap)
3517 struct thread *td = curthread;
3518 struct proc *p = td->td_proc;
3522 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3524 if (fp->f_nchandle.ncp)
3525 error = ncp_writechk(&fp->f_nchandle);
3527 error = setfown(p->p_fd->fd_ncdir.mount,
3528 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3534 * fchownat(int fd, char *path, int uid, int gid, int flags)
3536 * Set ownership of file pointed to by fd/path.
3539 sys_fchownat(struct fchownat_args *uap)
3541 struct nlookupdata nd;
3546 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3548 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3550 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3551 UIO_USERSPACE, flags);
3553 error = kern_chown(&nd, uap->uid, uap->gid);
3554 nlookup_done_at(&nd, fp);
3560 getutimes(struct timeval *tvp, struct timespec *tsp)
3562 struct timeval tv[2];
3567 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3570 if ((error = itimerfix(tvp)) != 0)
3572 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3573 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3579 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3581 struct timespec tsnow;
3595 if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3597 if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3600 if (newts[0].tv_nsec == UTIME_OMIT)
3601 newts[0].tv_sec = VNOVAL;
3602 else if (newts[0].tv_nsec == UTIME_NOW)
3604 else if ((error = itimespecfix(&newts[0])) != 0)
3607 if (newts[1].tv_nsec == UTIME_OMIT)
3608 newts[1].tv_sec = VNOVAL;
3609 else if (newts[1].tv_nsec == UTIME_NOW)
3611 else if ((error = itimespecfix(&newts[1])) != 0)
3618 setutimes(struct vnode *vp, struct vattr *vattr,
3619 const struct timespec *ts, int nullflag)
3621 struct thread *td = curthread;
3625 vattr->va_atime = ts[0];
3626 vattr->va_mtime = ts[1];
3628 vattr->va_vaflags |= VA_UTIMES_NULL;
3629 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3635 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3637 struct timespec ts[2];
3641 if ((error = getutimes(tptr, ts)) != 0)
3644 error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3649 * utimes_args(char *path, struct timeval *tptr)
3651 * Set the access and modification times of a file.
3654 sys_utimes(struct utimes_args *uap)
3656 struct timeval tv[2];
3657 struct nlookupdata nd;
3661 error = copyin(uap->tptr, tv, sizeof(tv));
3665 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3667 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3673 * lutimes_args(char *path, struct timeval *tptr)
3675 * Set the access and modification times of a file.
3678 sys_lutimes(struct lutimes_args *uap)
3680 struct timeval tv[2];
3681 struct nlookupdata nd;
3685 error = copyin(uap->tptr, tv, sizeof(tv));
3689 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3691 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3697 * Set utimes on a file descriptor. The creds used to open the
3698 * file are used to determine whether the operation is allowed
3702 kern_futimens(int fd, struct timespec *ts)
3704 struct thread *td = curthread;
3705 struct timespec newts[2];
3712 error = getutimens(ts, newts, &nullflag);
3715 if ((error = holdvnode(td, fd, &fp)) != 0)
3717 if (fp->f_nchandle.ncp)
3718 error = ncp_writechk(&fp->f_nchandle);
3721 error = vget(vp, LK_EXCLUSIVE);
3723 error = VOP_GETATTR(vp, &vattr);
3725 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3729 error = setutimes(vp, &vattr, newts, nullflag);
3739 * futimens_args(int fd, struct timespec *ts)
3741 * Set the access and modification times of a file.
3744 sys_futimens(struct futimens_args *uap)
3746 struct timespec ts[2];
3750 error = copyin(uap->ts, ts, sizeof(ts));
3754 error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3759 kern_futimes(int fd, struct timeval *tptr)
3761 struct timespec ts[2];
3765 if ((error = getutimes(tptr, ts)) != 0)
3768 error = kern_futimens(fd, tptr ? ts : NULL);
3773 * futimes_args(int fd, struct timeval *tptr)
3775 * Set the access and modification times of a file.
3778 sys_futimes(struct futimes_args *uap)
3780 struct timeval tv[2];
3784 error = copyin(uap->tptr, tv, sizeof(tv));
3788 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3793 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3795 struct timespec newts[2];
3801 if (flags & ~AT_SYMLINK_NOFOLLOW)
3804 error = getutimens(ts, newts, &nullflag);
3808 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3809 if ((error = nlookup(nd)) != 0)
3811 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3813 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3815 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3816 error = vget(vp, LK_EXCLUSIVE);
3818 error = setutimes(vp, &vattr, newts, nullflag);
3827 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3829 * Set file access and modification times of a file.
3832 sys_utimensat(struct utimensat_args *uap)
3834 struct timespec ts[2];
3835 struct nlookupdata nd;
3841 error = copyin(uap->ts, ts, sizeof(ts));
3846 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3847 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3848 UIO_USERSPACE, flags);
3850 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3851 nlookup_done_at(&nd, fp);
3856 kern_truncate(struct nlookupdata *nd, off_t length)
3863 uint64_t old_size = 0;
3867 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3868 if ((error = nlookup(nd)) != 0)
3870 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3872 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3874 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3879 if (vp->v_type == VDIR) {
3883 if (vfs_quota_enabled) {
3884 error = VOP_GETATTR(vp, &vattr);
3885 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3888 old_size = vattr.va_size;
3891 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3893 vattr.va_size = length;
3894 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3895 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3903 * truncate(char *path, int pad, off_t length)
3905 * Truncate a file given its path name.
3908 sys_truncate(struct truncate_args *uap)
3910 struct nlookupdata nd;
3913 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3915 error = kern_truncate(&nd, uap->length);
3921 kern_ftruncate(int fd, off_t length)
3923 struct thread *td = curthread;
3930 uint64_t old_size = 0;
3935 if ((error = holdvnode(td, fd, &fp)) != 0)
3937 if (fp->f_nchandle.ncp) {
3938 error = ncp_writechk(&fp->f_nchandle);
3942 if ((fp->f_flag & FWRITE) == 0) {
3946 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3950 vp = (struct vnode *)fp->f_data;
3951 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3952 if (vp->v_type == VDIR) {
3958 if (vfs_quota_enabled) {
3959 error = VOP_GETATTR(vp, &vattr);
3960 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3963 old_size = vattr.va_size;
3966 if ((error = vn_writechk(vp, NULL)) == 0) {
3968 vattr.va_size = length;
3969 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3971 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3980 * ftruncate_args(int fd, int pad, off_t length)
3982 * Truncate a file given a file descriptor.
3985 sys_ftruncate(struct ftruncate_args *uap)
3989 error = kern_ftruncate(uap->fd, uap->length);
3997 * Sync an open file.
4000 sys_fsync(struct fsync_args *uap)
4002 struct thread *td = curthread;
4008 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
4010 vp = (struct vnode *)fp->f_data;
4011 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4012 if ((obj = vp->v_object) != NULL) {
4013 if (vp->v_mount == NULL ||
4014 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
4015 vm_object_page_clean(obj, 0, 0, 0);
4018 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
4019 if (error == 0 && vp->v_mount)
4020 error = buf_fsync(vp);
4028 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
4030 struct nchandle fnchd;
4031 struct nchandle tnchd;
4032 struct namecache *ncp;
4041 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
4042 if ((error = nlookup(fromnd)) != 0)
4044 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
4046 fnchd.mount = fromnd->nl_nch.mount;
4050 * unlock the source nch so we can lookup the target nch without
4051 * deadlocking. The target may or may not exist so we do not check
4052 * for a target vp like kern_mkdir() and other creation functions do.
4054 * The source and target directories are ref'd and rechecked after
4055 * everything is relocked to determine if the source or target file
4058 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
4059 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
4061 fncp_gen = fromnd->nl_nch.ncp->nc_generation;
4063 cache_unlock(&fromnd->nl_nch);
4065 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
4066 if ((error = nlookup(tond)) != 0) {
4070 tncp_gen = tond->nl_nch.ncp->nc_generation;
4072 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
4076 tnchd.mount = tond->nl_nch.mount;
4080 * If the source and target are the same there is nothing to do
4082 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
4089 * Mount points cannot be renamed or overwritten
4091 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
4100 * Relock the source ncp. cache_relock() will deal with any
4101 * deadlocks against the already-locked tond and will also
4102 * make sure both are resolved.
4104 * NOTE AFTER RELOCKING: The source or target ncp may have become
4105 * invalid while they were unlocked, nc_vp and nc_mount could
4108 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
4109 &tond->nl_nch, tond->nl_cred);
4110 fromnd->nl_flags |= NLC_NCPISLOCKED;
4113 * If the namecache generation changed for either fromnd or tond,
4116 if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
4117 tond->nl_nch.ncp->nc_generation != tncp_gen) {
4118 kprintf("kern_rename: retry due to gen on: "
4119 "\"%s\" -> \"%s\"\n",
4120 fromnd->nl_nch.ncp->nc_name,
4121 tond->nl_nch.ncp->nc_name);
4128 * If either fromnd or tond are marked destroyed a ripout occured
4129 * out from under us and we must retry.
4131 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
4132 fromnd->nl_nch.ncp->nc_vp == NULL ||
4133 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
4134 kprintf("kern_rename: retry due to ripout on: "
4135 "\"%s\" -> \"%s\"\n",
4136 fromnd->nl_nch.ncp->nc_name,
4137 tond->nl_nch.ncp->nc_name);
4144 * Make sure the parent directories linkages are the same.
4145 * XXX shouldn't be needed any more w/ generation check above.
4147 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
4148 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
4155 * Both the source and target must be within the same filesystem and
4156 * in the same filesystem as their parent directories within the
4157 * namecache topology.
4159 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4162 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
4163 mp != tond->nl_nch.mount) {
4170 * Make sure the mount point is writable
4172 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4179 * If the target exists and either the source or target is a directory,
4180 * then both must be directories.
4182 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4185 if (tond->nl_nch.ncp->nc_vp) {
4186 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4188 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4189 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4191 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4197 * You cannot rename a source into itself or a subdirectory of itself.
4198 * We check this by travsersing the target directory upwards looking
4199 * for a match against the source.
4204 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4205 if (fromnd->nl_nch.ncp == ncp) {
4216 * Even though the namespaces are different, they may still represent
4217 * hardlinks to the same file. The filesystem might have a hard time
4218 * with this so we issue a NREMOVE of the source instead of a NRENAME
4219 * when we detect the situation.
4222 fdvp = fromnd->nl_dvp;
4223 tdvp = tond->nl_dvp;
4224 if (fdvp == NULL || tdvp == NULL) {
4226 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4227 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4230 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4231 fdvp, tdvp, tond->nl_cred);
4238 * rename_args(char *from, char *to)
4240 * Rename files. Source and destination must either both be directories,
4241 * or both not be directories. If target is a directory, it must be empty.
4244 sys_rename(struct rename_args *uap)
4246 struct nlookupdata fromnd, tond;
4250 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4252 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4254 error = kern_rename(&fromnd, &tond);
4255 nlookup_done(&tond);
4257 nlookup_done(&fromnd);
4258 } while (error == EAGAIN);
4263 * renameat_args(int oldfd, char *old, int newfd, char *new)
4265 * Rename files using paths relative to the directories associated with
4266 * oldfd and newfd. Source and destination must either both be directories,
4267 * or both not be directories. If target is a directory, it must be empty.
4270 sys_renameat(struct renameat_args *uap)
4272 struct nlookupdata oldnd, newnd;
4273 struct file *oldfp, *newfp;
4277 error = nlookup_init_at(&oldnd, &oldfp,
4278 uap->oldfd, uap->old,
4281 error = nlookup_init_at(&newnd, &newfp,
4282 uap->newfd, uap->new,
4285 error = kern_rename(&oldnd, &newnd);
4286 nlookup_done_at(&newnd, newfp);
4288 nlookup_done_at(&oldnd, oldfp);
4289 } while (error == EAGAIN);
4294 kern_mkdir(struct nlookupdata *nd, int mode)
4296 struct thread *td = curthread;
4297 struct proc *p = td->td_proc;
4303 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4304 if ((error = nlookup(nd)) != 0)
4307 if (nd->nl_nch.ncp->nc_vp)
4309 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4312 vattr.va_type = VDIR;
4313 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4316 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4323 * mkdir_args(char *path, int mode)
4325 * Make a directory file.
4328 sys_mkdir(struct mkdir_args *uap)
4330 struct nlookupdata nd;
4333 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4335 error = kern_mkdir(&nd, uap->mode);
4341 * mkdirat_args(int fd, char *path, mode_t mode)
4343 * Make a directory file. The path is relative to the directory associated
4347 sys_mkdirat(struct mkdirat_args *uap)
4349 struct nlookupdata nd;
4353 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4355 error = kern_mkdir(&nd, uap->mode);
4356 nlookup_done_at(&nd, fp);
4361 kern_rmdir(struct nlookupdata *nd)
4366 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4367 if ((error = nlookup(nd)) != 0)
4371 * Do not allow directories representing mount points to be
4372 * deleted, even if empty. Check write perms on mount point
4373 * in case the vnode is aliased (aka nullfs).
4375 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4377 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4379 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4384 * rmdir_args(char *path)
4386 * Remove a directory file.
4389 sys_rmdir(struct rmdir_args *uap)
4391 struct nlookupdata nd;
4394 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4396 error = kern_rmdir(&nd);
4402 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4403 enum uio_seg direction)
4405 struct thread *td = curthread;
4413 if ((error = holdvnode(td, fd, &fp)) != 0)
4415 if ((fp->f_flag & FREAD) == 0) {
4419 vp = (struct vnode *)fp->f_data;
4420 if (vp->v_type != VDIR) {
4424 aiov.iov_base = buf;
4425 aiov.iov_len = count;
4426 auio.uio_iov = &aiov;
4427 auio.uio_iovcnt = 1;
4428 auio.uio_rw = UIO_READ;
4429 auio.uio_segflg = direction;
4431 auio.uio_resid = count;
4432 loff = auio.uio_offset = fp->f_offset;
4433 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4434 fp->f_offset = auio.uio_offset;
4439 * WARNING! *basep may not be wide enough to accomodate the
4440 * seek offset. XXX should we hack this to return the upper 32 bits
4441 * for offsets greater then 4G?
4444 *basep = (long)loff;
4446 *res = count - auio.uio_resid;
4453 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4455 * Read a block of directory entries in a file system independent format.
4458 sys_getdirentries(struct getdirentries_args *uap)
4463 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4464 &uap->sysmsg_result, UIO_USERSPACE);
4466 if (error == 0 && uap->basep)
4467 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4472 * getdents_args(int fd, char *buf, size_t count)
4475 sys_getdents(struct getdents_args *uap)
4479 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4480 &uap->sysmsg_result, UIO_USERSPACE);
4486 * Set the mode mask for creation of filesystem nodes.
4488 * umask(int newmask)
4491 sys_umask(struct umask_args *uap)
4493 struct thread *td = curthread;
4494 struct proc *p = td->td_proc;
4495 struct filedesc *fdp;
4498 uap->sysmsg_result = fdp->fd_cmask;
4499 fdp->fd_cmask = uap->newmask & ALLPERMS;
4504 * revoke(char *path)
4506 * Void all references to file by ripping underlying filesystem
4510 sys_revoke(struct revoke_args *uap)
4512 struct nlookupdata nd;
4519 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4521 error = nlookup(&nd);
4523 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4524 cred = crhold(nd.nl_cred);
4528 error = VOP_GETATTR(vp, &vattr);
4529 if (error == 0 && cred->cr_uid != vattr.va_uid)
4530 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4531 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4533 error = vrevoke(vp, cred);
4534 } else if (error == 0) {
4535 error = vrevoke(vp, cred);
4545 * getfh_args(char *fname, fhandle_t *fhp)
4547 * Get (NFS) file handle
4549 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4550 * mount. This allows nullfs mounts to be explicitly exported.
4552 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4554 * nullfs mounts of subdirectories are not safe. That is, it will
4555 * work, but you do not really have protection against access to
4556 * the related parent directories.
4559 sys_getfh(struct getfh_args *uap)
4561 struct thread *td = curthread;
4562 struct nlookupdata nd;
4569 * Must be super user
4571 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4575 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4577 error = nlookup(&nd);
4579 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4580 mp = nd.nl_nch.mount;
4583 bzero(&fh, sizeof(fh));
4584 fh.fh_fsid = mp->mnt_stat.f_fsid;
4585 error = VFS_VPTOFH(vp, &fh.fh_fid);
4588 error = copyout(&fh, uap->fhp, sizeof(fh));
4594 * fhopen_args(const struct fhandle *u_fhp, int flags)
4596 * syscall for the rpc.lockd to use to translate a NFS file handle into
4597 * an open descriptor.
4599 * warning: do not remove the priv_check() call or this becomes one giant
4603 sys_fhopen(struct fhopen_args *uap)
4605 struct thread *td = curthread;
4606 struct filedesc *fdp = td->td_proc->p_fd;
4611 struct vattr *vap = &vat;
4613 int fmode, mode, error = 0, type;
4619 * Must be super user
4621 error = priv_check(td, PRIV_ROOT);
4625 fmode = FFLAGS(uap->flags);
4628 * Why not allow a non-read/write open for our lockd?
4630 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4632 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4637 * Find the mount point
4639 mp = vfs_getvfs(&fhp.fh_fsid);
4644 /* now give me my vnode, it gets returned to me locked */
4645 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4649 * from now on we have to make sure not
4650 * to forget about the vnode
4651 * any error that causes an abort must vput(vp)
4652 * just set error = err and 'goto bad;'.
4658 if (vp->v_type == VLNK) {
4662 if (vp->v_type == VSOCK) {
4667 if (fmode & (FWRITE | O_TRUNC)) {
4668 if (vp->v_type == VDIR) {
4672 error = vn_writechk(vp, NULL);
4680 error = VOP_ACCESS(vp, mode, td->td_ucred);
4684 if (fmode & O_TRUNC) {
4685 vn_unlock(vp); /* XXX */
4686 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4689 error = VOP_SETATTR(vp, vap, td->td_ucred);
4695 * VOP_OPEN needs the file pointer so it can potentially override
4698 * WARNING! no f_nchandle will be associated when fhopen()ing a
4701 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4705 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4708 * setting f_ops this way prevents VOP_CLOSE from being
4709 * called or fdrop() releasing the vp from v_data. Since
4710 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4712 fp->f_ops = &badfileops;
4718 * The fp is given its own reference, we still have our ref and lock.
4720 * Assert that all regular files must be created with a VM object.
4722 if (vp->v_type == VREG && vp->v_object == NULL) {
4723 kprintf("fhopen: regular file did not "
4724 "have VM object: %p\n",
4730 * The open was successful. Handle any locking requirements.
4732 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4733 lf.l_whence = SEEK_SET;
4736 if (fmode & O_EXLOCK)
4737 lf.l_type = F_WRLCK;
4739 lf.l_type = F_RDLCK;
4740 if (fmode & FNONBLOCK)
4745 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK,
4748 * release our private reference.
4750 fsetfd(fdp, NULL, indx);
4755 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4756 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
4760 * Clean up. Associate the file pointer with the previously
4761 * reserved descriptor and return it.
4764 if (uap->flags & O_CLOEXEC)
4765 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4766 fsetfd(fdp, fp, indx);
4768 uap->sysmsg_result = indx;
4774 fsetfd(fdp, NULL, indx);
4785 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4788 sys_fhstat(struct fhstat_args *uap)
4790 struct thread *td = curthread;
4798 * Must be super user
4800 error = priv_check(td, PRIV_ROOT);
4804 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4808 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4811 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4812 error = vn_stat(vp, &sb, td->td_ucred);
4817 error = copyout(&sb, uap->sb, sizeof(sb));
4825 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4828 sys_fhstatfs(struct fhstatfs_args *uap)
4830 struct thread *td = curthread;
4831 struct proc *p = td->td_proc;
4836 char *fullpath, *freepath;
4841 * Must be super user
4843 if ((error = priv_check(td, PRIV_ROOT)))
4846 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4849 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4853 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4858 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4863 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4866 error = mount_path(p, mp, &fullpath, &freepath);
4869 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4870 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4871 kfree(freepath, M_TEMP);
4873 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4874 if (priv_check(td, PRIV_ROOT)) {
4875 bcopy(sp, &sb, sizeof(sb));
4876 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4879 error = copyout(sp, uap->buf, sizeof(*sp));
4888 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4891 sys_fhstatvfs(struct fhstatvfs_args *uap)
4893 struct thread *td = curthread;
4894 struct proc *p = td->td_proc;
4902 * Must be super user
4904 if ((error = priv_check(td, PRIV_ROOT)))
4907 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4910 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4914 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4919 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4922 sp = &mp->mnt_vstat;
4924 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4928 if (mp->mnt_flag & MNT_RDONLY)
4929 sp->f_flag |= ST_RDONLY;
4930 if (mp->mnt_flag & MNT_NOSUID)
4931 sp->f_flag |= ST_NOSUID;
4932 error = copyout(sp, uap->buf, sizeof(*sp));
4941 * Syscall to push extended attribute configuration information into the
4942 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4943 * a command (int cmd), and attribute name and misc data. For now, the
4944 * attribute name is left in userspace for consumption by the VFS_op.
4945 * It will probably be changed to be copied into sysspace by the
4946 * syscall in the future, once issues with various consumers of the
4947 * attribute code have raised their hands.
4949 * Currently this is used only by UFS Extended Attributes.
4952 sys_extattrctl(struct extattrctl_args *uap)
4954 struct nlookupdata nd;
4956 char attrname[EXTATTR_MAXNAMELEN];
4964 if (error == 0 && uap->filename) {
4965 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4968 error = nlookup(&nd);
4970 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4974 if (error == 0 && uap->attrname) {
4975 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4980 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4982 error = nlookup(&nd);
4984 error = ncp_writechk(&nd.nl_nch);
4986 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4988 uap->attrname, nd.nl_cred);
4997 * Syscall to get a named extended attribute on a file or directory.
5000 sys_extattr_set_file(struct extattr_set_file_args *uap)
5002 char attrname[EXTATTR_MAXNAMELEN];
5003 struct nlookupdata nd;
5009 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5015 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5017 error = nlookup(&nd);
5019 error = ncp_writechk(&nd.nl_nch);
5021 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5027 bzero(&auio, sizeof(auio));
5028 aiov.iov_base = uap->data;
5029 aiov.iov_len = uap->nbytes;
5030 auio.uio_iov = &aiov;
5031 auio.uio_iovcnt = 1;
5032 auio.uio_offset = 0;
5033 auio.uio_resid = uap->nbytes;
5034 auio.uio_rw = UIO_WRITE;
5035 auio.uio_td = curthread;
5037 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
5046 * Syscall to get a named extended attribute on a file or directory.
5049 sys_extattr_get_file(struct extattr_get_file_args *uap)
5051 char attrname[EXTATTR_MAXNAMELEN];
5052 struct nlookupdata nd;
5058 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5064 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5066 error = nlookup(&nd);
5068 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
5074 bzero(&auio, sizeof(auio));
5075 aiov.iov_base = uap->data;
5076 aiov.iov_len = uap->nbytes;
5077 auio.uio_iov = &aiov;
5078 auio.uio_iovcnt = 1;
5079 auio.uio_offset = 0;
5080 auio.uio_resid = uap->nbytes;
5081 auio.uio_rw = UIO_READ;
5082 auio.uio_td = curthread;
5084 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
5086 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
5094 * Syscall to delete a named extended attribute from a file or directory.
5095 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
5098 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
5100 char attrname[EXTATTR_MAXNAMELEN];
5101 struct nlookupdata nd;
5105 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5109 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5111 error = nlookup(&nd);
5113 error = ncp_writechk(&nd.nl_nch);
5115 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5117 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
5118 attrname, NULL, nd.nl_cred);
5127 * Determine if the mount is visible to the process.
5130 chroot_visible_mnt(struct mount *mp, struct proc *p)
5132 struct nchandle nch;
5135 * Traverse from the mount point upwards. If we hit the process
5136 * root then the mount point is visible to the process.
5138 nch = mp->mnt_ncmountpt;
5140 if (nch.mount == p->p_fd->fd_nrdir.mount &&
5141 nch.ncp == p->p_fd->fd_nrdir.ncp) {
5144 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
5145 nch = nch.mount->mnt_ncmounton;
5147 nch.ncp = nch.ncp->nc_parent;
5152 * If the mount point is not visible to the process, but the
5153 * process root is in a subdirectory of the mount, return
5156 if (p->p_fd->fd_nrdir.mount == mp)