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, NLC_FOLLOW);
610 error = nlookup(&nd);
614 mp = nd.nl_nch.mount;
617 * Only root, or the user that did the original mount is
618 * permitted to unmount this filesystem.
620 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
621 (error = priv_check(td, PRIV_ROOT)))
625 * Don't allow unmounting the root file system.
627 if (mp->mnt_flag & MNT_ROOTFS) {
633 * Must be the root of the filesystem
635 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
641 * If no error try to issue the unmount. We lose our cache
642 * ref when we call nlookup_done so we must hold the mount point
643 * to prevent use-after-free races.
649 error = dounmount(mp, uap->flags, 0);
659 * Do the actual file system unmount (interlocked against the mountlist
660 * token and mp->mnt_token).
663 dounmount_interlock(struct mount *mp)
665 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
667 mp->mnt_kern_flag |= MNTK_UNMOUNT;
672 unmount_allproc_cb(struct proc *p, void *arg)
676 if (p->p_textnch.ncp == NULL)
679 mp = (struct mount *)arg;
680 if (p->p_textnch.mount == mp)
681 cache_drop(&p->p_textnch);
687 * The guts of the unmount code. The mount owns one ref and one hold
688 * count. If we successfully interlock the unmount, those refs are ours.
689 * (The ref is from mnt_ncmountpt).
691 * When halting we shortcut certain mount types such as devfs by not actually
692 * issuing the VFS_SYNC() or VFS_UNMOUNT(). They are still disconnected
693 * from the mountlist so higher-level filesytems can unmount cleanly.
695 * The mount types that allow QUICKHALT are: devfs, tmpfs, procfs.
698 dounmount(struct mount *mp, int flags, int halting)
700 struct namecache *ncp;
711 lwkt_gettoken(&mp->mnt_token);
714 * When halting, certain mount points can essentially just
715 * be unhooked and otherwise ignored.
717 if (halting && (mp->mnt_kern_flag & MNTK_QUICKHALT)) {
726 * Exclusive access for unmounting purposes.
728 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
732 * We now 'own' the last mp->mnt_refs
734 * Allow filesystems to detect that a forced unmount is in progress.
736 if (flags & MNT_FORCE)
737 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
738 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
739 error = lockmgr(&mp->mnt_lock, lflags);
741 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
742 if (mp->mnt_kern_flag & MNTK_MWAIT) {
743 mp->mnt_kern_flag &= ~MNTK_MWAIT;
749 if (mp->mnt_flag & MNT_EXPUBLIC)
750 vfs_setpublicfs(NULL, NULL, NULL);
752 vfs_msync(mp, MNT_WAIT);
753 async_flag = mp->mnt_flag & MNT_ASYNC;
754 mp->mnt_flag &=~ MNT_ASYNC;
757 * Decomission our special mnt_syncer vnode. This also stops
758 * the vnlru code. If we are unable to unmount we recommission
761 * Then sync the filesystem.
763 if ((vp = mp->mnt_syncer) != NULL) {
764 mp->mnt_syncer = NULL;
765 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
771 * Sync normally-mounted filesystem.
773 if (quickhalt == 0) {
774 if ((mp->mnt_flag & MNT_RDONLY) == 0)
775 VFS_SYNC(mp, MNT_WAIT);
779 * nchandle records ref the mount structure. Expect a count of 1
780 * (our mount->mnt_ncmountpt).
782 * Scans can get temporary refs on a mountpoint (thought really
783 * heavy duty stuff like cache_findmount() do not).
785 for (retry = 0; (retry < 10 || debug_unmount); ++retry) {
787 * Invalidate the namecache topology under the mount.
788 * nullfs mounts alias a real mount's namecache topology
789 * and it should not be invalidated in that case.
791 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
792 cache_lock(&mp->mnt_ncmountpt);
793 cache_inval(&mp->mnt_ncmountpt,
794 CINV_DESTROY|CINV_CHILDREN);
795 cache_unlock(&mp->mnt_ncmountpt);
801 cache_unmounting(mp);
802 if (mp->mnt_refs != 1)
803 cache_clearmntcache();
806 * Break out if we are good. Don't count ncp refs if the
809 ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ?
810 NULL : mp->mnt_ncmountpt.ncp;
811 if (mp->mnt_refs == 1 &&
812 (ncp == NULL || (ncp->nc_refs == 1 &&
813 TAILQ_FIRST(&ncp->nc_list) == NULL))) {
818 * If forcing the unmount, clean out any p->p_textnch
819 * nchandles that match this mount.
821 if (flags & MNT_FORCE)
822 allproc_scan(&unmount_allproc_cb, mp, 0);
827 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
828 if ((retry & 15) == 15) {
830 "(%p) debug - retry %d, "
831 "%d namecache refs, %d mount refs",
833 (ncp ? ncp->nc_refs - 1 : 0),
839 ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ?
840 NULL : mp->mnt_ncmountpt.ncp;
841 if (mp->mnt_refs != 1 ||
842 (ncp != NULL && (ncp->nc_refs != 1 ||
843 TAILQ_FIRST(&ncp->nc_list)))) {
845 "(%p): %d namecache refs, %d mount refs "
848 (ncp ? ncp->nc_refs - 1 : 0),
850 if (flags & MNT_FORCE) {
852 mount_warning(mp, "forcing unmount\n");
859 * So far so good, sync the filesystem once more and
860 * call the VFS unmount code if the sync succeeds.
862 if (error == 0 && quickhalt == 0) {
863 if (mp->mnt_flag & MNT_RDONLY) {
864 error = VFS_UNMOUNT(mp, flags);
866 error = VFS_SYNC(mp, MNT_WAIT);
868 (error == EOPNOTSUPP) || /* No sync */
869 (flags & MNT_FORCE)) {
870 error = VFS_UNMOUNT(mp, flags);
875 "(%p) unmount: vfs refused to unmount, "
882 * If an error occurred we can still recover, restoring the
883 * syncer vnode and misc flags.
886 if (mp->mnt_syncer == NULL && hadsyncer)
887 vfs_allocate_syncvnode(mp);
888 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
889 mp->mnt_flag |= async_flag;
890 lockmgr(&mp->mnt_lock, LK_RELEASE);
891 if (mp->mnt_kern_flag & MNTK_MWAIT) {
892 mp->mnt_kern_flag &= ~MNTK_MWAIT;
898 * Clean up any journals still associated with the mount after
899 * filesystem activity has ceased.
901 journal_remove_all_journals(mp,
902 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
904 mountlist_remove(mp);
907 * Remove any installed vnode ops here so the individual VFSs don't
910 * mnt_refs should go to zero when we scrap mnt_ncmountpt.
912 * When quickhalting we have to keep these intact because the
913 * underlying vnodes have not been destroyed, and some might be
916 if (quickhalt == 0) {
917 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
918 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
919 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
920 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
921 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
924 if (mp->mnt_ncmountpt.ncp != NULL) {
925 nch = mp->mnt_ncmountpt;
926 cache_zero(&mp->mnt_ncmountpt);
927 cache_clrmountpt(&nch);
930 if (mp->mnt_ncmounton.ncp != NULL) {
931 cache_unmounting(mp);
932 nch = mp->mnt_ncmounton;
933 cache_zero(&mp->mnt_ncmounton);
934 cache_clrmountpt(&nch);
938 mp->mnt_vfc->vfc_refcount--;
941 * If not quickhalting the mount, we expect there to be no
944 if (quickhalt == 0 && !TAILQ_EMPTY(&mp->mnt_nvnodelist))
945 panic("unmount: dangling vnode");
950 lockmgr(&mp->mnt_lock, LK_RELEASE);
951 if (mp->mnt_kern_flag & MNTK_MWAIT) {
952 mp->mnt_kern_flag &= ~MNTK_MWAIT;
957 * If we reach here and freeok != 0 we must free the mount.
958 * mnt_refs should already have dropped to 0, so if it is not
959 * zero we must cycle the caches and wait.
961 * When we are satisfied that the mount has disconnected we can
962 * drop the hold on the mp that represented the mount (though the
963 * caller might actually have another, so the caller's drop may
964 * do the actual free).
967 if (mp->mnt_refs > 0)
968 cache_clearmntcache();
969 while (mp->mnt_refs > 0) {
970 cache_unmounting(mp);
972 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
973 cache_clearmntcache();
975 lwkt_reltoken(&mp->mnt_token);
979 cache_clearmntcache();
982 KNOTE(&fs_klist, VQ_UNMOUNT);
985 lwkt_reltoken(&mp->mnt_token);
991 mount_warning(struct mount *mp, const char *ctl, ...)
998 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
999 &ptr, &buf, 0) == 0) {
1000 kprintf("unmount(%s): ", ptr);
1005 kprintf("unmount(%p", mp);
1006 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
1007 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
1016 * Shim cache_fullpath() to handle the case where a process is chrooted into
1017 * a subdirectory of a mount. In this case if the root mount matches the
1018 * process root directory's mount we have to specify the process's root
1019 * directory instead of the mount point, because the mount point might
1020 * be above the root directory.
1024 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
1026 struct nchandle *nch;
1028 if (p && p->p_fd->fd_nrdir.mount == mp)
1029 nch = &p->p_fd->fd_nrdir;
1031 nch = &mp->mnt_ncmountpt;
1032 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
1036 * Sync each mounted filesystem.
1040 static int syncprt = 0;
1041 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
1044 static int sync_callback(struct mount *mp, void *data);
1047 sys_sync(struct sync_args *uap)
1049 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
1055 sync_callback(struct mount *mp, void *data __unused)
1059 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1060 lwkt_gettoken(&mp->mnt_token);
1061 asyncflag = mp->mnt_flag & MNT_ASYNC;
1062 mp->mnt_flag &= ~MNT_ASYNC;
1063 lwkt_reltoken(&mp->mnt_token);
1064 vfs_msync(mp, MNT_NOWAIT);
1065 VFS_SYNC(mp, MNT_NOWAIT);
1066 lwkt_gettoken(&mp->mnt_token);
1067 mp->mnt_flag |= asyncflag;
1068 lwkt_reltoken(&mp->mnt_token);
1073 /* XXX PRISON: could be per prison flag */
1074 static int prison_quotas;
1076 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
1080 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
1082 * Change filesystem quotas.
1087 sys_quotactl(struct quotactl_args *uap)
1089 struct nlookupdata nd;
1095 if (td->td_ucred->cr_prison && !prison_quotas) {
1100 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1102 error = nlookup(&nd);
1104 mp = nd.nl_nch.mount;
1105 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
1106 uap->arg, nd.nl_cred);
1114 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1115 * void *buf, int buflen)
1117 * This function operates on a mount point and executes the specified
1118 * operation using the specified control data, and possibly returns data.
1120 * The actual number of bytes stored in the result buffer is returned, 0
1121 * if none, otherwise an error is returned.
1126 sys_mountctl(struct mountctl_args *uap)
1128 struct thread *td = curthread;
1136 * Sanity and permissions checks. We must be root.
1138 if (td->td_ucred->cr_prison != NULL)
1140 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1141 (error = priv_check(td, PRIV_ROOT)) != 0)
1145 * Argument length checks
1147 if (uap->ctllen < 0 || uap->ctllen > 1024)
1149 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1151 if (uap->path == NULL)
1155 * Allocate the necessary buffers and copyin data
1157 path = objcache_get(namei_oc, M_WAITOK);
1158 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1163 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1164 error = copyin(uap->ctl, ctl, uap->ctllen);
1169 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1172 * Validate the descriptor
1175 fp = holdfp(td, uap->fd, -1);
1185 * Execute the internal kernel function and clean up.
1187 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen,
1188 buf, uap->buflen, &uap->sysmsg_result);
1190 dropfp(td, uap->fd, fp);
1191 if (error == 0 && uap->sysmsg_result > 0)
1192 error = copyout(buf, uap->buf, uap->sysmsg_result);
1195 objcache_put(namei_oc, path);
1204 * Execute a mount control operation by resolving the path to a mount point
1205 * and calling vop_mountctl().
1207 * Use the mount point from the nch instead of the vnode so nullfs mounts
1208 * can properly spike the VOP.
1211 kern_mountctl(const char *path, int op, struct file *fp,
1212 const void *ctl, int ctllen,
1213 void *buf, int buflen, int *res)
1216 struct nlookupdata nd;
1217 struct nchandle nch;
1223 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1226 error = nlookup(&nd);
1231 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1238 * Yes, all this is needed to use the nch.mount below, because
1239 * we must maintain a ref on the mount to avoid ripouts (e.g.
1240 * due to heavy mount/unmount use by synth or poudriere).
1243 cache_zero(&nd.nl_nch);
1251 * Must be the root of the filesystem
1253 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1258 if (mp == NULL || mp->mnt_kern_flag & MNTK_UNMOUNT) {
1259 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1265 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1274 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1276 struct thread *td = curthread;
1277 struct proc *p = td->td_proc;
1280 char *fullpath, *freepath;
1283 if ((error = nlookup(nd)) != 0)
1285 mp = nd->nl_nch.mount;
1287 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1290 error = mount_path(p, mp, &fullpath, &freepath);
1293 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1294 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1295 kfree(freepath, M_TEMP);
1297 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1298 bcopy(sp, buf, sizeof(*buf));
1299 /* Only root should have access to the fsid's. */
1300 if (priv_check(td, PRIV_ROOT))
1301 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1306 * statfs_args(char *path, struct statfs *buf)
1308 * Get filesystem statistics.
1311 sys_statfs(struct statfs_args *uap)
1313 struct nlookupdata nd;
1317 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1319 error = kern_statfs(&nd, &buf);
1322 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1327 kern_fstatfs(int fd, struct statfs *buf)
1329 struct thread *td = curthread;
1330 struct proc *p = td->td_proc;
1334 char *fullpath, *freepath;
1338 if ((error = holdvnode(td, fd, &fp)) != 0)
1342 * Try to use mount info from any overlays rather than the
1343 * mount info for the underlying vnode, otherwise we will
1344 * fail when operating on null-mounted paths inside a chroot.
1346 if ((mp = fp->f_nchandle.mount) == NULL)
1347 mp = ((struct vnode *)fp->f_data)->v_mount;
1352 if (fp->f_cred == NULL) {
1357 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1360 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1362 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1363 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1364 kfree(freepath, M_TEMP);
1366 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1367 bcopy(sp, buf, sizeof(*buf));
1369 /* Only root should have access to the fsid's. */
1370 if (priv_check(td, PRIV_ROOT))
1371 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1379 * fstatfs_args(int fd, struct statfs *buf)
1381 * Get filesystem statistics.
1384 sys_fstatfs(struct fstatfs_args *uap)
1389 error = kern_fstatfs(uap->fd, &buf);
1392 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1397 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1403 if ((error = nlookup(nd)) != 0)
1405 mp = nd->nl_nch.mount;
1406 sp = &mp->mnt_vstat;
1407 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1411 if (mp->mnt_flag & MNT_RDONLY)
1412 sp->f_flag |= ST_RDONLY;
1413 if (mp->mnt_flag & MNT_NOSUID)
1414 sp->f_flag |= ST_NOSUID;
1415 bcopy(sp, buf, sizeof(*buf));
1420 * statfs_args(char *path, struct statfs *buf)
1422 * Get filesystem statistics.
1425 sys_statvfs(struct statvfs_args *uap)
1427 struct nlookupdata nd;
1431 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1433 error = kern_statvfs(&nd, &buf);
1436 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1441 kern_fstatvfs(int fd, struct statvfs *buf)
1443 struct thread *td = curthread;
1449 if ((error = holdvnode(td, fd, &fp)) != 0)
1451 if ((mp = fp->f_nchandle.mount) == NULL)
1452 mp = ((struct vnode *)fp->f_data)->v_mount;
1457 if (fp->f_cred == NULL) {
1461 sp = &mp->mnt_vstat;
1462 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1466 if (mp->mnt_flag & MNT_RDONLY)
1467 sp->f_flag |= ST_RDONLY;
1468 if (mp->mnt_flag & MNT_NOSUID)
1469 sp->f_flag |= ST_NOSUID;
1471 bcopy(sp, buf, sizeof(*buf));
1479 * fstatfs_args(int fd, struct statfs *buf)
1481 * Get filesystem statistics.
1484 sys_fstatvfs(struct fstatvfs_args *uap)
1489 error = kern_fstatvfs(uap->fd, &buf);
1492 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1497 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1499 * Get statistics on all filesystems.
1502 struct getfsstat_info {
1503 struct statfs *sfsp;
1511 static int getfsstat_callback(struct mount *, void *);
1514 sys_getfsstat(struct getfsstat_args *uap)
1516 struct thread *td = curthread;
1517 struct getfsstat_info info;
1519 bzero(&info, sizeof(info));
1521 info.maxcount = uap->bufsize / sizeof(struct statfs);
1522 info.sfsp = uap->buf;
1524 info.flags = uap->flags;
1527 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1528 if (info.sfsp && info.count > info.maxcount)
1529 uap->sysmsg_result = info.maxcount;
1531 uap->sysmsg_result = info.count;
1532 return (info.error);
1536 getfsstat_callback(struct mount *mp, void *data)
1538 struct getfsstat_info *info = data;
1544 if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1547 if (info->sfsp && info->count < info->maxcount) {
1551 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1552 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1553 * overrides MNT_WAIT.
1555 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1556 (info->flags & MNT_WAIT)) &&
1557 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1560 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1562 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1564 info->error = error;
1567 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1568 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1569 kfree(freepath, M_TEMP);
1571 error = copyout(sp, info->sfsp, sizeof(*sp));
1573 info->error = error;
1583 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1584 long bufsize, int flags)
1586 * Get statistics on all filesystems.
1589 struct getvfsstat_info {
1590 struct statfs *sfsp;
1591 struct statvfs *vsfsp;
1599 static int getvfsstat_callback(struct mount *, void *);
1602 sys_getvfsstat(struct getvfsstat_args *uap)
1604 struct thread *td = curthread;
1605 struct getvfsstat_info info;
1607 bzero(&info, sizeof(info));
1609 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1610 info.sfsp = uap->buf;
1611 info.vsfsp = uap->vbuf;
1613 info.flags = uap->flags;
1616 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1617 if (info.vsfsp && info.count > info.maxcount)
1618 uap->sysmsg_result = info.maxcount;
1620 uap->sysmsg_result = info.count;
1621 return (info.error);
1625 getvfsstat_callback(struct mount *mp, void *data)
1627 struct getvfsstat_info *info = data;
1629 struct statvfs *vsp;
1634 if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1637 if (info->vsfsp && info->count < info->maxcount) {
1639 vsp = &mp->mnt_vstat;
1642 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1643 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1644 * overrides MNT_WAIT.
1646 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1647 (info->flags & MNT_WAIT)) &&
1648 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1651 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1653 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1654 (info->flags & MNT_WAIT)) &&
1655 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1659 if (mp->mnt_flag & MNT_RDONLY)
1660 vsp->f_flag |= ST_RDONLY;
1661 if (mp->mnt_flag & MNT_NOSUID)
1662 vsp->f_flag |= ST_NOSUID;
1664 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1666 info->error = error;
1669 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1670 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1671 kfree(freepath, M_TEMP);
1673 error = copyout(sp, info->sfsp, sizeof(*sp));
1675 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1677 info->error = error;
1689 * fchdir_args(int fd)
1691 * Change current working directory to a given file descriptor.
1694 sys_fchdir(struct fchdir_args *uap)
1696 struct thread *td = curthread;
1697 struct proc *p = td->td_proc;
1698 struct filedesc *fdp = p->p_fd;
1699 struct vnode *vp, *ovp;
1702 struct nchandle nch, onch, tnch;
1705 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
1707 lwkt_gettoken(&p->p_token);
1708 vp = (struct vnode *)fp->f_data;
1710 vn_lock(vp, LK_SHARED | LK_RETRY);
1711 if (fp->f_nchandle.ncp == NULL)
1714 error = checkvp_chdir(vp, td);
1719 cache_copy(&fp->f_nchandle, &nch);
1722 * If the ncp has become a mount point, traverse through
1726 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1727 (mp = cache_findmount(&nch)) != NULL
1729 error = nlookup_mp(mp, &tnch);
1731 cache_unlock(&tnch); /* leave ref intact */
1733 vp = tnch.ncp->nc_vp;
1734 error = vget(vp, LK_SHARED);
1735 KKASSERT(error == 0);
1739 cache_dropmount(mp);
1742 spin_lock(&fdp->fd_spin);
1744 onch = fdp->fd_ncdir;
1746 fdp->fd_ncdir = nch;
1747 spin_unlock(&fdp->fd_spin);
1748 vn_unlock(vp); /* leave ref intact */
1757 lwkt_reltoken(&p->p_token);
1762 kern_chdir(struct nlookupdata *nd)
1764 struct thread *td = curthread;
1765 struct proc *p = td->td_proc;
1766 struct filedesc *fdp = p->p_fd;
1767 struct vnode *vp, *ovp;
1768 struct nchandle onch;
1771 nd->nl_flags |= NLC_SHAREDLOCK;
1772 if ((error = nlookup(nd)) != 0)
1774 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1776 if ((error = vget(vp, LK_SHARED)) != 0)
1779 lwkt_gettoken(&p->p_token);
1780 error = checkvp_chdir(vp, td);
1783 spin_lock(&fdp->fd_spin);
1785 onch = fdp->fd_ncdir;
1786 fdp->fd_ncdir = nd->nl_nch;
1788 spin_unlock(&fdp->fd_spin);
1789 cache_unlock(&nd->nl_nch); /* leave reference intact */
1792 cache_zero(&nd->nl_nch);
1796 lwkt_reltoken(&p->p_token);
1801 * chdir_args(char *path)
1803 * Change current working directory (``.'').
1806 sys_chdir(struct chdir_args *uap)
1808 struct nlookupdata nd;
1811 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1813 error = kern_chdir(&nd);
1819 * Helper function for raised chroot(2) security function: Refuse if
1820 * any filedescriptors are open directories.
1823 chroot_refuse_vdir_fds(thread_t td, struct filedesc *fdp)
1830 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1831 if ((error = holdvnode(td, fd, &fp)) != 0)
1833 vp = (struct vnode *)fp->f_data;
1834 if (vp->v_type != VDIR) {
1845 * This sysctl determines if we will allow a process to chroot(2) if it
1846 * has a directory open:
1847 * 0: disallowed for all processes.
1848 * 1: allowed for processes that were not already chroot(2)'ed.
1849 * 2: allowed for all processes.
1852 static int chroot_allow_open_directories = 1;
1854 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1855 &chroot_allow_open_directories, 0, "");
1858 * chroot to the specified namecache entry. We obtain the vp from the
1859 * namecache data. The passed ncp must be locked and referenced and will
1860 * remain locked and referenced on return.
1863 kern_chroot(struct nchandle *nch)
1865 struct thread *td = curthread;
1866 struct proc *p = td->td_proc;
1867 struct filedesc *fdp = p->p_fd;
1872 * Only privileged user can chroot
1874 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1879 * Disallow open directory descriptors (fchdir() breakouts).
1881 if (chroot_allow_open_directories == 0 ||
1882 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1883 if ((error = chroot_refuse_vdir_fds(td, fdp)) != 0)
1886 if ((vp = nch->ncp->nc_vp) == NULL)
1889 if ((error = vget(vp, LK_SHARED)) != 0)
1893 * Check the validity of vp as a directory to change to and
1894 * associate it with rdir/jdir.
1896 error = checkvp_chdir(vp, td);
1897 vn_unlock(vp); /* leave reference intact */
1899 lwkt_gettoken(&p->p_token);
1900 vrele(fdp->fd_rdir);
1901 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1902 cache_drop(&fdp->fd_nrdir);
1903 cache_copy(nch, &fdp->fd_nrdir);
1904 if (fdp->fd_jdir == NULL) {
1907 cache_copy(nch, &fdp->fd_njdir);
1909 if ((p->p_flags & P_DIDCHROOT) == 0) {
1910 p->p_flags |= P_DIDCHROOT;
1911 if (p->p_depth <= 65535 - 32)
1914 lwkt_reltoken(&p->p_token);
1922 * chroot_args(char *path)
1924 * Change notion of root (``/'') directory.
1927 sys_chroot(struct chroot_args *uap)
1929 struct thread *td __debugvar = curthread;
1930 struct nlookupdata nd;
1933 KKASSERT(td->td_proc);
1934 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1936 nd.nl_flags |= NLC_EXEC;
1937 error = nlookup(&nd);
1939 error = kern_chroot(&nd.nl_nch);
1946 sys_chroot_kernel(struct chroot_kernel_args *uap)
1948 struct thread *td = curthread;
1949 struct nlookupdata nd;
1950 struct nchandle *nch;
1954 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1958 error = nlookup(&nd);
1964 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1968 if ((vp = nch->ncp->nc_vp) == NULL) {
1973 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1976 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1977 vfs_cache_setroot(vp, cache_hold(nch));
1986 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1987 * determine whether it is legal to chdir to the vnode. The vnode's state
1988 * is not changed by this call.
1991 checkvp_chdir(struct vnode *vp, struct thread *td)
1995 if (vp->v_type != VDIR)
1998 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
2003 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
2005 struct thread *td = curthread;
2006 struct proc *p = td->td_proc;
2007 struct lwp *lp = td->td_lwp;
2008 struct filedesc *fdp = p->p_fd;
2013 int type, indx, error = 0;
2016 if ((oflags & O_ACCMODE) == O_ACCMODE)
2018 flags = FFLAGS(oflags);
2019 error = falloc(lp, &nfp, NULL);
2023 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
2026 * XXX p_dupfd is a real mess. It allows a device to return a
2027 * file descriptor to be duplicated rather then doing the open
2033 * Call vn_open() to do the lookup and assign the vnode to the
2034 * file pointer. vn_open() does not change the ref count on fp
2035 * and the vnode, on success, will be inherited by the file pointer
2038 * Request a shared lock on the vnode if possible.
2040 * Executable binaries can race VTEXT against O_RDWR opens, so
2041 * use an exclusive lock for O_RDWR opens as well.
2043 * NOTE: We need a flag to separate terminal vnode locking from
2044 * parent locking. O_CREAT needs parent locking, but O_TRUNC
2045 * and O_RDWR only need to lock the terminal vnode exclusively.
2047 nd->nl_flags |= NLC_LOCKVP;
2048 if ((flags & (O_CREAT|O_TRUNC|O_RDWR)) == 0)
2049 nd->nl_flags |= NLC_SHAREDLOCK;
2051 error = vn_open(nd, fp, flags, cmode);
2056 * handle special fdopen() case. bleh. dupfdopen() is
2057 * responsible for dropping the old contents of ofiles[indx]
2060 * Note that fsetfd() will add a ref to fp which represents
2061 * the fd_files[] assignment. We must still drop our
2064 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
2065 if (fdalloc(p, 0, &indx) == 0) {
2066 error = dupfdopen(td, indx, lp->lwp_dupfd, flags, error);
2069 fdrop(fp); /* our ref */
2072 fsetfd(fdp, NULL, indx);
2075 fdrop(fp); /* our ref */
2076 if (error == ERESTART)
2082 * ref the vnode for ourselves so it can't be ripped out from under
2083 * is. XXX need an ND flag to request that the vnode be returned
2086 * Reserve a file descriptor but do not assign it until the open
2089 vp = (struct vnode *)fp->f_data;
2091 if ((error = fdalloc(p, 0, &indx)) != 0) {
2098 * If no error occurs the vp will have been assigned to the file
2103 if (flags & (O_EXLOCK | O_SHLOCK)) {
2104 lf.l_whence = SEEK_SET;
2107 if (flags & O_EXLOCK)
2108 lf.l_type = F_WRLCK;
2110 lf.l_type = F_RDLCK;
2111 if (flags & FNONBLOCK)
2116 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2118 * lock request failed. Clean up the reserved
2122 fsetfd(fdp, NULL, indx);
2126 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
2130 * Assert that all regular file vnodes were created with a object.
2132 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
2133 ("open: regular file has no backing object after vn_open"));
2139 * release our private reference, leaving the one associated with the
2140 * descriptor table intact.
2142 if (oflags & O_CLOEXEC)
2143 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
2144 fsetfd(fdp, fp, indx);
2152 * open_args(char *path, int flags, int mode)
2154 * Check permissions, allocate an open file structure,
2155 * and call the device open routine if any.
2158 sys_open(struct open_args *uap)
2160 struct nlookupdata nd;
2163 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2165 error = kern_open(&nd, uap->flags,
2166 uap->mode, &uap->sysmsg_result);
2173 * openat_args(int fd, char *path, int flags, int mode)
2176 sys_openat(struct openat_args *uap)
2178 struct nlookupdata nd;
2182 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2184 error = kern_open(&nd, uap->flags, uap->mode,
2185 &uap->sysmsg_result);
2187 nlookup_done_at(&nd, fp);
2192 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2194 struct thread *td = curthread;
2195 struct proc *p = td->td_proc;
2204 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2205 vattr.va_rmajor = rmajor;
2206 vattr.va_rminor = rminor;
2208 switch (mode & S_IFMT) {
2209 case S_IFMT: /* used by badsect to flag bad sectors */
2210 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2211 vattr.va_type = VBAD;
2214 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2215 vattr.va_type = VCHR;
2218 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2219 vattr.va_type = VBLK;
2222 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2225 case S_IFDIR: /* special directories support for HAMMER */
2226 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2227 vattr.va_type = VDIR;
2238 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2239 if ((error = nlookup(nd)) != 0)
2241 if (nd->nl_nch.ncp->nc_vp)
2243 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2247 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2248 nd->nl_cred, NAMEI_CREATE);
2251 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2252 &vp, nd->nl_cred, &vattr);
2260 * mknod_args(char *path, int mode, int dev)
2262 * Create a special file.
2265 sys_mknod(struct mknod_args *uap)
2267 struct nlookupdata nd;
2270 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2272 error = kern_mknod(&nd, uap->mode,
2273 umajor(uap->dev), uminor(uap->dev));
2280 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2282 * Create a special file. The path is relative to the directory associated
2286 sys_mknodat(struct mknodat_args *uap)
2288 struct nlookupdata nd;
2292 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2294 error = kern_mknod(&nd, uap->mode,
2295 umajor(uap->dev), uminor(uap->dev));
2297 nlookup_done_at(&nd, fp);
2302 kern_mkfifo(struct nlookupdata *nd, int mode)
2304 struct thread *td = curthread;
2305 struct proc *p = td->td_proc;
2312 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2313 if ((error = nlookup(nd)) != 0)
2315 if (nd->nl_nch.ncp->nc_vp)
2317 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2321 vattr.va_type = VFIFO;
2322 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2324 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2331 * mkfifo_args(char *path, int mode)
2333 * Create a named pipe.
2336 sys_mkfifo(struct mkfifo_args *uap)
2338 struct nlookupdata nd;
2341 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2343 error = kern_mkfifo(&nd, uap->mode);
2349 * mkfifoat_args(int fd, char *path, mode_t mode)
2351 * Create a named pipe. The path is relative to the directory associated
2355 sys_mkfifoat(struct mkfifoat_args *uap)
2357 struct nlookupdata nd;
2361 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2363 error = kern_mkfifo(&nd, uap->mode);
2364 nlookup_done_at(&nd, fp);
2368 static int hardlink_check_uid = 0;
2369 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2370 &hardlink_check_uid, 0,
2371 "Unprivileged processes cannot create hard links to files owned by other "
2373 static int hardlink_check_gid = 0;
2374 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2375 &hardlink_check_gid, 0,
2376 "Unprivileged processes cannot create hard links to files owned by other "
2380 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2386 * Shortcut if disabled
2388 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2392 * Privileged user can always hardlink
2394 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2398 * Otherwise only if the originating file is owned by the
2399 * same user or group. Note that any group is allowed if
2400 * the file is owned by the caller.
2402 error = VOP_GETATTR(vp, &va);
2406 if (hardlink_check_uid) {
2407 if (cred->cr_uid != va.va_uid)
2411 if (hardlink_check_gid) {
2412 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2420 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2422 struct thread *td = curthread;
2427 * Lookup the source and obtained a locked vnode.
2429 * You may only hardlink a file which you have write permission
2430 * on or which you own.
2432 * XXX relookup on vget failure / race ?
2435 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2436 if ((error = nlookup(nd)) != 0)
2438 vp = nd->nl_nch.ncp->nc_vp;
2439 KKASSERT(vp != NULL);
2440 if (vp->v_type == VDIR)
2441 return (EPERM); /* POSIX */
2442 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2444 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2448 * Unlock the source so we can lookup the target without deadlocking
2449 * (XXX vp is locked already, possible other deadlock?). The target
2452 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2453 nd->nl_flags &= ~NLC_NCPISLOCKED;
2454 cache_unlock(&nd->nl_nch);
2457 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2458 if ((error = nlookup(linknd)) != 0) {
2462 if (linknd->nl_nch.ncp->nc_vp) {
2466 VFS_MODIFYING(vp->v_mount);
2467 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2474 * Finally run the new API VOP.
2476 error = can_hardlink(vp, td, td->td_ucred);
2478 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2479 vp, linknd->nl_cred);
2486 * link_args(char *path, char *link)
2488 * Make a hard file link.
2491 sys_link(struct link_args *uap)
2493 struct nlookupdata nd, linknd;
2496 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2498 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2500 error = kern_link(&nd, &linknd);
2501 nlookup_done(&linknd);
2508 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2510 * Make a hard file link. The path1 argument is relative to the directory
2511 * associated with fd1, and similarly the path2 argument is relative to
2512 * the directory associated with fd2.
2515 sys_linkat(struct linkat_args *uap)
2517 struct nlookupdata nd, linknd;
2518 struct file *fp1, *fp2;
2521 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2522 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2524 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2525 uap->path2, UIO_USERSPACE, 0);
2527 error = kern_link(&nd, &linknd);
2528 nlookup_done_at(&linknd, fp2);
2530 nlookup_done_at(&nd, fp1);
2535 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2543 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2544 if ((error = nlookup(nd)) != 0)
2546 if (nd->nl_nch.ncp->nc_vp)
2548 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2552 vattr.va_mode = mode;
2553 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2560 * symlink(char *path, char *link)
2562 * Make a symbolic link.
2565 sys_symlink(struct symlink_args *uap)
2567 struct thread *td = curthread;
2568 struct nlookupdata nd;
2573 path = objcache_get(namei_oc, M_WAITOK);
2574 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2576 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2578 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2579 error = kern_symlink(&nd, path, mode);
2583 objcache_put(namei_oc, path);
2588 * symlinkat_args(char *path1, int fd, char *path2)
2590 * Make a symbolic link. The path2 argument is relative to the directory
2591 * associated with fd.
2594 sys_symlinkat(struct symlinkat_args *uap)
2596 struct thread *td = curthread;
2597 struct nlookupdata nd;
2603 path1 = objcache_get(namei_oc, M_WAITOK);
2604 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2606 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2609 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2610 error = kern_symlink(&nd, path1, mode);
2612 nlookup_done_at(&nd, fp);
2614 objcache_put(namei_oc, path1);
2619 * undelete_args(char *path)
2621 * Delete a whiteout from the filesystem.
2624 sys_undelete(struct undelete_args *uap)
2626 struct nlookupdata nd;
2629 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2631 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2633 error = nlookup(&nd);
2635 error = ncp_writechk(&nd.nl_nch);
2637 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2645 kern_unlink(struct nlookupdata *nd)
2650 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2651 if ((error = nlookup(nd)) != 0)
2653 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2655 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2660 * unlink_args(char *path)
2662 * Delete a name from the filesystem.
2665 sys_unlink(struct unlink_args *uap)
2667 struct nlookupdata nd;
2670 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2672 error = kern_unlink(&nd);
2679 * unlinkat_args(int fd, char *path, int flags)
2681 * Delete the file or directory entry pointed to by fd/path.
2684 sys_unlinkat(struct unlinkat_args *uap)
2686 struct nlookupdata nd;
2690 if (uap->flags & ~AT_REMOVEDIR)
2693 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2695 if (uap->flags & AT_REMOVEDIR)
2696 error = kern_rmdir(&nd);
2698 error = kern_unlink(&nd);
2700 nlookup_done_at(&nd, fp);
2705 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2707 struct thread *td = curthread;
2714 fp = holdfp(td, fd, -1);
2717 if (fp->f_type != DTYPE_VNODE) {
2721 vp = (struct vnode *)fp->f_data;
2725 spin_lock(&fp->f_spin);
2726 new_offset = fp->f_offset + offset;
2730 error = VOP_GETATTR(vp, &vattr);
2731 spin_lock(&fp->f_spin);
2732 new_offset = offset + vattr.va_size;
2735 new_offset = offset;
2737 spin_lock(&fp->f_spin);
2742 spin_lock(&fp->f_spin);
2747 * Validate the seek position. Negative offsets are not allowed
2748 * for regular files or directories.
2750 * Normally we would also not want to allow negative offsets for
2751 * character and block-special devices. However kvm addresses
2752 * on 64 bit architectures might appear to be negative and must
2756 if (new_offset < 0 &&
2757 (vp->v_type == VREG || vp->v_type == VDIR)) {
2760 fp->f_offset = new_offset;
2763 *res = fp->f_offset;
2764 spin_unlock(&fp->f_spin);
2772 * lseek_args(int fd, int pad, off_t offset, int whence)
2774 * Reposition read/write file offset.
2777 sys_lseek(struct lseek_args *uap)
2781 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2782 &uap->sysmsg_offset);
2788 * Check if current process can access given file. amode is a bitmask of *_OK
2789 * access bits. flags is a bitmask of AT_* flags.
2792 kern_access(struct nlookupdata *nd, int amode, int flags)
2797 if (flags & ~AT_EACCESS)
2799 nd->nl_flags |= NLC_SHAREDLOCK;
2800 if ((error = nlookup(nd)) != 0)
2803 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2807 /* Flags == 0 means only check for existence. */
2816 if ((mode & VWRITE) == 0 ||
2817 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2818 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2821 * If the file handle is stale we have to re-resolve the
2822 * entry with the ncp held exclusively. This is a hack
2825 if (error == ESTALE) {
2827 cache_unlock(&nd->nl_nch);
2828 cache_lock(&nd->nl_nch);
2829 cache_setunresolved(&nd->nl_nch);
2830 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2843 * access_args(char *path, int flags)
2845 * Check access permissions.
2848 sys_access(struct access_args *uap)
2850 struct nlookupdata nd;
2853 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2855 error = kern_access(&nd, uap->flags, 0);
2862 * eaccess_args(char *path, int flags)
2864 * Check access permissions.
2867 sys_eaccess(struct eaccess_args *uap)
2869 struct nlookupdata nd;
2872 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2874 error = kern_access(&nd, uap->flags, AT_EACCESS);
2881 * faccessat_args(int fd, char *path, int amode, int flags)
2883 * Check access permissions.
2886 sys_faccessat(struct faccessat_args *uap)
2888 struct nlookupdata nd;
2892 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2895 error = kern_access(&nd, uap->amode, uap->flags);
2896 nlookup_done_at(&nd, fp);
2901 kern_stat(struct nlookupdata *nd, struct stat *st)
2906 nd->nl_flags |= NLC_SHAREDLOCK;
2907 if ((error = nlookup(nd)) != 0)
2910 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2913 if ((error = vget(vp, LK_SHARED)) != 0)
2915 error = vn_stat(vp, st, nd->nl_cred);
2918 * If the file handle is stale we have to re-resolve the
2919 * entry with the ncp held exclusively. This is a hack
2922 if (error == ESTALE) {
2924 cache_unlock(&nd->nl_nch);
2925 cache_lock(&nd->nl_nch);
2926 cache_setunresolved(&nd->nl_nch);
2927 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2937 * stat_args(char *path, struct stat *ub)
2939 * Get file status; this version follows links.
2942 sys_stat(struct stat_args *uap)
2944 struct nlookupdata nd;
2948 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2950 error = kern_stat(&nd, &st);
2952 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2959 * lstat_args(char *path, struct stat *ub)
2961 * Get file status; this version does not follow links.
2964 sys_lstat(struct lstat_args *uap)
2966 struct nlookupdata nd;
2970 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2972 error = kern_stat(&nd, &st);
2974 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2981 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2983 * Get status of file pointed to by fd/path.
2986 sys_fstatat(struct fstatat_args *uap)
2988 struct nlookupdata nd;
2994 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2997 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2999 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3000 UIO_USERSPACE, flags);
3002 error = kern_stat(&nd, &st);
3004 error = copyout(&st, uap->sb, sizeof(*uap->sb));
3006 nlookup_done_at(&nd, fp);
3011 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
3013 struct nlookupdata nd;
3018 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
3020 error = nlookup(&nd);
3022 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3025 error = VOP_PATHCONF(vp, name, sysmsg_regp);
3032 * pathconf_Args(char *path, int name)
3034 * Get configurable pathname variables.
3037 sys_pathconf(struct pathconf_args *uap)
3039 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
3044 * lpathconf_Args(char *path, int name)
3046 * Get configurable pathname variables, but don't follow symlinks.
3049 sys_lpathconf(struct lpathconf_args *uap)
3051 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
3056 * kern_readlink isn't properly split yet. There is a copyin burried
3057 * in VOP_READLINK().
3060 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
3062 struct thread *td = curthread;
3068 nd->nl_flags |= NLC_SHAREDLOCK;
3069 if ((error = nlookup(nd)) != 0)
3071 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
3074 if (vp->v_type != VLNK) {
3077 aiov.iov_base = buf;
3078 aiov.iov_len = count;
3079 auio.uio_iov = &aiov;
3080 auio.uio_iovcnt = 1;
3081 auio.uio_offset = 0;
3082 auio.uio_rw = UIO_READ;
3083 auio.uio_segflg = UIO_USERSPACE;
3085 auio.uio_resid = count;
3086 error = VOP_READLINK(vp, &auio, td->td_ucred);
3089 *res = count - auio.uio_resid;
3094 * readlink_args(char *path, char *buf, int count)
3096 * Return target name of a symbolic link.
3099 sys_readlink(struct readlink_args *uap)
3101 struct nlookupdata nd;
3104 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3106 error = kern_readlink(&nd, uap->buf, uap->count,
3107 &uap->sysmsg_result);
3114 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3116 * Return target name of a symbolic link. The path is relative to the
3117 * directory associated with fd.
3120 sys_readlinkat(struct readlinkat_args *uap)
3122 struct nlookupdata nd;
3126 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3128 error = kern_readlink(&nd, uap->buf, uap->bufsize,
3129 &uap->sysmsg_result);
3131 nlookup_done_at(&nd, fp);
3136 setfflags(struct vnode *vp, int flags)
3138 struct thread *td = curthread;
3143 * Prevent non-root users from setting flags on devices. When
3144 * a device is reused, users can retain ownership of the device
3145 * if they are allowed to set flags and programs assume that
3146 * chown can't fail when done as root.
3148 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
3149 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
3153 * note: vget is required for any operation that might mod the vnode
3154 * so VINACTIVE is properly cleared.
3156 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3158 vattr.va_flags = flags;
3159 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3166 * chflags(char *path, int flags)
3168 * Change flags of a file given a path name.
3171 sys_chflags(struct chflags_args *uap)
3173 struct nlookupdata nd;
3178 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3180 error = nlookup(&nd);
3182 error = ncp_writechk(&nd.nl_nch);
3184 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3187 error = setfflags(vp, uap->flags);
3194 * lchflags(char *path, int flags)
3196 * Change flags of a file given a path name, but don't follow symlinks.
3199 sys_lchflags(struct lchflags_args *uap)
3201 struct nlookupdata nd;
3206 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3208 error = nlookup(&nd);
3210 error = ncp_writechk(&nd.nl_nch);
3212 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3215 error = setfflags(vp, uap->flags);
3222 * fchflags_args(int fd, int flags)
3224 * Change flags of a file given a file descriptor.
3227 sys_fchflags(struct fchflags_args *uap)
3229 struct thread *td = curthread;
3233 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3235 if (fp->f_nchandle.ncp)
3236 error = ncp_writechk(&fp->f_nchandle);
3238 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3244 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3245 * change flags given a pathname relative to a filedescriptor
3247 int sys_chflagsat(struct chflagsat_args *uap)
3249 struct nlookupdata nd;
3255 if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3258 lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3261 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags);
3263 error = nlookup(&nd);
3265 error = ncp_writechk(&nd.nl_nch);
3267 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3268 nlookup_done_at(&nd, fp);
3270 error = setfflags(vp, uap->flags);
3278 setfmode(struct vnode *vp, int mode)
3280 struct thread *td = curthread;
3285 * note: vget is required for any operation that might mod the vnode
3286 * so VINACTIVE is properly cleared.
3288 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3290 vattr.va_mode = mode & ALLPERMS;
3291 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3292 cache_inval_wxok(vp);
3299 kern_chmod(struct nlookupdata *nd, int mode)
3304 if ((error = nlookup(nd)) != 0)
3306 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3308 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3309 error = setfmode(vp, mode);
3315 * chmod_args(char *path, int mode)
3317 * Change mode of a file given path name.
3320 sys_chmod(struct chmod_args *uap)
3322 struct nlookupdata nd;
3325 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3327 error = kern_chmod(&nd, uap->mode);
3333 * lchmod_args(char *path, int mode)
3335 * Change mode of a file given path name (don't follow links.)
3338 sys_lchmod(struct lchmod_args *uap)
3340 struct nlookupdata nd;
3343 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3345 error = kern_chmod(&nd, uap->mode);
3351 * fchmod_args(int fd, int mode)
3353 * Change mode of a file given a file descriptor.
3356 sys_fchmod(struct fchmod_args *uap)
3358 struct thread *td = curthread;
3362 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3364 if (fp->f_nchandle.ncp)
3365 error = ncp_writechk(&fp->f_nchandle);
3367 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3373 * fchmodat_args(char *path, int mode)
3375 * Change mode of a file pointed to by fd/path.
3378 sys_fchmodat(struct fchmodat_args *uap)
3380 struct nlookupdata nd;
3385 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3387 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3389 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3390 UIO_USERSPACE, flags);
3392 error = kern_chmod(&nd, uap->mode);
3393 nlookup_done_at(&nd, fp);
3398 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3400 struct thread *td = curthread;
3408 * note: vget is required for any operation that might mod the vnode
3409 * so VINACTIVE is properly cleared.
3411 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3412 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3414 o_uid = vattr.va_uid;
3415 o_gid = vattr.va_gid;
3416 size = vattr.va_size;
3421 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3430 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3431 VFS_ACCOUNT(mp, uid, gid, size);
3438 kern_chown(struct nlookupdata *nd, int uid, int gid)
3443 if ((error = nlookup(nd)) != 0)
3445 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3447 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3448 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3454 * chown(char *path, int uid, int gid)
3456 * Set ownership given a path name.
3459 sys_chown(struct chown_args *uap)
3461 struct nlookupdata nd;
3464 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3466 error = kern_chown(&nd, uap->uid, uap->gid);
3472 * lchown_args(char *path, int uid, int gid)
3474 * Set ownership given a path name, do not cross symlinks.
3477 sys_lchown(struct lchown_args *uap)
3479 struct nlookupdata nd;
3482 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3484 error = kern_chown(&nd, uap->uid, uap->gid);
3490 * fchown_args(int fd, int uid, int gid)
3492 * Set ownership given a file descriptor.
3495 sys_fchown(struct fchown_args *uap)
3497 struct thread *td = curthread;
3498 struct proc *p = td->td_proc;
3502 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3504 if (fp->f_nchandle.ncp)
3505 error = ncp_writechk(&fp->f_nchandle);
3507 error = setfown(p->p_fd->fd_ncdir.mount,
3508 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3514 * fchownat(int fd, char *path, int uid, int gid, int flags)
3516 * Set ownership of file pointed to by fd/path.
3519 sys_fchownat(struct fchownat_args *uap)
3521 struct nlookupdata nd;
3526 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3528 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3530 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3531 UIO_USERSPACE, flags);
3533 error = kern_chown(&nd, uap->uid, uap->gid);
3534 nlookup_done_at(&nd, fp);
3540 getutimes(struct timeval *tvp, struct timespec *tsp)
3542 struct timeval tv[2];
3547 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3550 if ((error = itimerfix(tvp)) != 0)
3552 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3553 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3559 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3561 struct timespec tsnow;
3575 if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3577 if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3580 if (newts[0].tv_nsec == UTIME_OMIT)
3581 newts[0].tv_sec = VNOVAL;
3582 else if (newts[0].tv_nsec == UTIME_NOW)
3584 else if ((error = itimespecfix(&newts[0])) != 0)
3587 if (newts[1].tv_nsec == UTIME_OMIT)
3588 newts[1].tv_sec = VNOVAL;
3589 else if (newts[1].tv_nsec == UTIME_NOW)
3591 else if ((error = itimespecfix(&newts[1])) != 0)
3598 setutimes(struct vnode *vp, struct vattr *vattr,
3599 const struct timespec *ts, int nullflag)
3601 struct thread *td = curthread;
3605 vattr->va_atime = ts[0];
3606 vattr->va_mtime = ts[1];
3608 vattr->va_vaflags |= VA_UTIMES_NULL;
3609 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3615 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3617 struct timespec ts[2];
3621 if ((error = getutimes(tptr, ts)) != 0)
3624 error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3629 * utimes_args(char *path, struct timeval *tptr)
3631 * Set the access and modification times of a file.
3634 sys_utimes(struct utimes_args *uap)
3636 struct timeval tv[2];
3637 struct nlookupdata nd;
3641 error = copyin(uap->tptr, tv, sizeof(tv));
3645 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3647 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3653 * lutimes_args(char *path, struct timeval *tptr)
3655 * Set the access and modification times of a file.
3658 sys_lutimes(struct lutimes_args *uap)
3660 struct timeval tv[2];
3661 struct nlookupdata nd;
3665 error = copyin(uap->tptr, tv, sizeof(tv));
3669 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3671 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3677 * Set utimes on a file descriptor. The creds used to open the
3678 * file are used to determine whether the operation is allowed
3682 kern_futimens(int fd, struct timespec *ts)
3684 struct thread *td = curthread;
3685 struct timespec newts[2];
3692 error = getutimens(ts, newts, &nullflag);
3695 if ((error = holdvnode(td, fd, &fp)) != 0)
3697 if (fp->f_nchandle.ncp)
3698 error = ncp_writechk(&fp->f_nchandle);
3701 error = vget(vp, LK_EXCLUSIVE);
3703 error = VOP_GETATTR(vp, &vattr);
3705 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3709 error = setutimes(vp, &vattr, newts, nullflag);
3719 * futimens_args(int fd, struct timespec *ts)
3721 * Set the access and modification times of a file.
3724 sys_futimens(struct futimens_args *uap)
3726 struct timespec ts[2];
3730 error = copyin(uap->ts, ts, sizeof(ts));
3734 error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3739 kern_futimes(int fd, struct timeval *tptr)
3741 struct timespec ts[2];
3745 if ((error = getutimes(tptr, ts)) != 0)
3748 error = kern_futimens(fd, tptr ? ts : NULL);
3753 * futimes_args(int fd, struct timeval *tptr)
3755 * Set the access and modification times of a file.
3758 sys_futimes(struct futimes_args *uap)
3760 struct timeval tv[2];
3764 error = copyin(uap->tptr, tv, sizeof(tv));
3768 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3773 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3775 struct timespec newts[2];
3781 if (flags & ~AT_SYMLINK_NOFOLLOW)
3784 error = getutimens(ts, newts, &nullflag);
3788 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3789 if ((error = nlookup(nd)) != 0)
3791 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3793 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3795 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3796 error = vget(vp, LK_EXCLUSIVE);
3798 error = setutimes(vp, &vattr, newts, nullflag);
3807 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3809 * Set file access and modification times of a file.
3812 sys_utimensat(struct utimensat_args *uap)
3814 struct timespec ts[2];
3815 struct nlookupdata nd;
3821 error = copyin(uap->ts, ts, sizeof(ts));
3826 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3827 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3828 UIO_USERSPACE, flags);
3830 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3831 nlookup_done_at(&nd, fp);
3836 kern_truncate(struct nlookupdata *nd, off_t length)
3843 uint64_t old_size = 0;
3847 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3848 if ((error = nlookup(nd)) != 0)
3850 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3852 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3854 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3859 if (vp->v_type == VDIR) {
3863 if (vfs_quota_enabled) {
3864 error = VOP_GETATTR(vp, &vattr);
3865 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3868 old_size = vattr.va_size;
3871 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3873 vattr.va_size = length;
3874 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3875 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3883 * truncate(char *path, int pad, off_t length)
3885 * Truncate a file given its path name.
3888 sys_truncate(struct truncate_args *uap)
3890 struct nlookupdata nd;
3893 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3895 error = kern_truncate(&nd, uap->length);
3901 kern_ftruncate(int fd, off_t length)
3903 struct thread *td = curthread;
3910 uint64_t old_size = 0;
3915 if ((error = holdvnode(td, fd, &fp)) != 0)
3917 if (fp->f_nchandle.ncp) {
3918 error = ncp_writechk(&fp->f_nchandle);
3922 if ((fp->f_flag & FWRITE) == 0) {
3926 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3930 vp = (struct vnode *)fp->f_data;
3931 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3932 if (vp->v_type == VDIR) {
3938 if (vfs_quota_enabled) {
3939 error = VOP_GETATTR(vp, &vattr);
3940 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3943 old_size = vattr.va_size;
3946 if ((error = vn_writechk(vp, NULL)) == 0) {
3948 vattr.va_size = length;
3949 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3951 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3960 * ftruncate_args(int fd, int pad, off_t length)
3962 * Truncate a file given a file descriptor.
3965 sys_ftruncate(struct ftruncate_args *uap)
3969 error = kern_ftruncate(uap->fd, uap->length);
3977 * Sync an open file.
3980 sys_fsync(struct fsync_args *uap)
3982 struct thread *td = curthread;
3988 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3990 vp = (struct vnode *)fp->f_data;
3991 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3992 if ((obj = vp->v_object) != NULL) {
3993 if (vp->v_mount == NULL ||
3994 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3995 vm_object_page_clean(obj, 0, 0, 0);
3998 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3999 if (error == 0 && vp->v_mount)
4000 error = buf_fsync(vp);
4008 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
4010 struct nchandle fnchd;
4011 struct nchandle tnchd;
4012 struct namecache *ncp;
4021 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
4022 if ((error = nlookup(fromnd)) != 0)
4024 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
4026 fnchd.mount = fromnd->nl_nch.mount;
4030 * unlock the source nch so we can lookup the target nch without
4031 * deadlocking. The target may or may not exist so we do not check
4032 * for a target vp like kern_mkdir() and other creation functions do.
4034 * The source and target directories are ref'd and rechecked after
4035 * everything is relocked to determine if the source or target file
4038 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
4039 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
4041 fncp_gen = fromnd->nl_nch.ncp->nc_generation;
4043 cache_unlock(&fromnd->nl_nch);
4045 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
4046 if ((error = nlookup(tond)) != 0) {
4050 tncp_gen = tond->nl_nch.ncp->nc_generation;
4052 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
4056 tnchd.mount = tond->nl_nch.mount;
4060 * If the source and target are the same there is nothing to do
4062 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
4069 * Mount points cannot be renamed or overwritten
4071 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
4080 * Relock the source ncp. cache_relock() will deal with any
4081 * deadlocks against the already-locked tond and will also
4082 * make sure both are resolved.
4084 * NOTE AFTER RELOCKING: The source or target ncp may have become
4085 * invalid while they were unlocked, nc_vp and nc_mount could
4088 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
4089 &tond->nl_nch, tond->nl_cred);
4090 fromnd->nl_flags |= NLC_NCPISLOCKED;
4093 * If the namecache generation changed for either fromnd or tond,
4096 if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
4097 tond->nl_nch.ncp->nc_generation != tncp_gen) {
4098 kprintf("kern_rename: retry due to gen on: "
4099 "\"%s\" -> \"%s\"\n",
4100 fromnd->nl_nch.ncp->nc_name,
4101 tond->nl_nch.ncp->nc_name);
4108 * If either fromnd or tond are marked destroyed a ripout occured
4109 * out from under us and we must retry.
4111 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
4112 fromnd->nl_nch.ncp->nc_vp == NULL ||
4113 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
4114 kprintf("kern_rename: retry due to ripout on: "
4115 "\"%s\" -> \"%s\"\n",
4116 fromnd->nl_nch.ncp->nc_name,
4117 tond->nl_nch.ncp->nc_name);
4124 * Make sure the parent directories linkages are the same.
4125 * XXX shouldn't be needed any more w/ generation check above.
4127 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
4128 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
4135 * Both the source and target must be within the same filesystem and
4136 * in the same filesystem as their parent directories within the
4137 * namecache topology.
4139 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4142 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
4143 mp != tond->nl_nch.mount) {
4150 * Make sure the mount point is writable
4152 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4159 * If the target exists and either the source or target is a directory,
4160 * then both must be directories.
4162 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4165 if (tond->nl_nch.ncp->nc_vp) {
4166 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4168 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4169 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4171 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4177 * You cannot rename a source into itself or a subdirectory of itself.
4178 * We check this by travsersing the target directory upwards looking
4179 * for a match against the source.
4184 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4185 if (fromnd->nl_nch.ncp == ncp) {
4196 * Even though the namespaces are different, they may still represent
4197 * hardlinks to the same file. The filesystem might have a hard time
4198 * with this so we issue a NREMOVE of the source instead of a NRENAME
4199 * when we detect the situation.
4202 fdvp = fromnd->nl_dvp;
4203 tdvp = tond->nl_dvp;
4204 if (fdvp == NULL || tdvp == NULL) {
4206 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4207 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4210 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4211 fdvp, tdvp, tond->nl_cred);
4218 * rename_args(char *from, char *to)
4220 * Rename files. Source and destination must either both be directories,
4221 * or both not be directories. If target is a directory, it must be empty.
4224 sys_rename(struct rename_args *uap)
4226 struct nlookupdata fromnd, tond;
4230 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4232 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4234 error = kern_rename(&fromnd, &tond);
4235 nlookup_done(&tond);
4237 nlookup_done(&fromnd);
4238 } while (error == EAGAIN);
4243 * renameat_args(int oldfd, char *old, int newfd, char *new)
4245 * Rename files using paths relative to the directories associated with
4246 * oldfd and newfd. Source and destination must either both be directories,
4247 * or both not be directories. If target is a directory, it must be empty.
4250 sys_renameat(struct renameat_args *uap)
4252 struct nlookupdata oldnd, newnd;
4253 struct file *oldfp, *newfp;
4257 error = nlookup_init_at(&oldnd, &oldfp,
4258 uap->oldfd, uap->old,
4261 error = nlookup_init_at(&newnd, &newfp,
4262 uap->newfd, uap->new,
4265 error = kern_rename(&oldnd, &newnd);
4266 nlookup_done_at(&newnd, newfp);
4268 nlookup_done_at(&oldnd, oldfp);
4269 } while (error == EAGAIN);
4274 kern_mkdir(struct nlookupdata *nd, int mode)
4276 struct thread *td = curthread;
4277 struct proc *p = td->td_proc;
4283 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4284 if ((error = nlookup(nd)) != 0)
4287 if (nd->nl_nch.ncp->nc_vp)
4289 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4292 vattr.va_type = VDIR;
4293 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4296 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4303 * mkdir_args(char *path, int mode)
4305 * Make a directory file.
4308 sys_mkdir(struct mkdir_args *uap)
4310 struct nlookupdata nd;
4313 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4315 error = kern_mkdir(&nd, uap->mode);
4321 * mkdirat_args(int fd, char *path, mode_t mode)
4323 * Make a directory file. The path is relative to the directory associated
4327 sys_mkdirat(struct mkdirat_args *uap)
4329 struct nlookupdata nd;
4333 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4335 error = kern_mkdir(&nd, uap->mode);
4336 nlookup_done_at(&nd, fp);
4341 kern_rmdir(struct nlookupdata *nd)
4346 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4347 if ((error = nlookup(nd)) != 0)
4351 * Do not allow directories representing mount points to be
4352 * deleted, even if empty. Check write perms on mount point
4353 * in case the vnode is aliased (aka nullfs).
4355 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4357 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4359 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4364 * rmdir_args(char *path)
4366 * Remove a directory file.
4369 sys_rmdir(struct rmdir_args *uap)
4371 struct nlookupdata nd;
4374 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4376 error = kern_rmdir(&nd);
4382 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4383 enum uio_seg direction)
4385 struct thread *td = curthread;
4393 if ((error = holdvnode(td, fd, &fp)) != 0)
4395 if ((fp->f_flag & FREAD) == 0) {
4399 vp = (struct vnode *)fp->f_data;
4400 if (vp->v_type != VDIR) {
4404 aiov.iov_base = buf;
4405 aiov.iov_len = count;
4406 auio.uio_iov = &aiov;
4407 auio.uio_iovcnt = 1;
4408 auio.uio_rw = UIO_READ;
4409 auio.uio_segflg = direction;
4411 auio.uio_resid = count;
4412 loff = auio.uio_offset = fp->f_offset;
4413 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4414 fp->f_offset = auio.uio_offset;
4419 * WARNING! *basep may not be wide enough to accomodate the
4420 * seek offset. XXX should we hack this to return the upper 32 bits
4421 * for offsets greater then 4G?
4424 *basep = (long)loff;
4426 *res = count - auio.uio_resid;
4433 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4435 * Read a block of directory entries in a file system independent format.
4438 sys_getdirentries(struct getdirentries_args *uap)
4443 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4444 &uap->sysmsg_result, UIO_USERSPACE);
4446 if (error == 0 && uap->basep)
4447 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4452 * getdents_args(int fd, char *buf, size_t count)
4455 sys_getdents(struct getdents_args *uap)
4459 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4460 &uap->sysmsg_result, UIO_USERSPACE);
4466 * Set the mode mask for creation of filesystem nodes.
4468 * umask(int newmask)
4471 sys_umask(struct umask_args *uap)
4473 struct thread *td = curthread;
4474 struct proc *p = td->td_proc;
4475 struct filedesc *fdp;
4478 uap->sysmsg_result = fdp->fd_cmask;
4479 fdp->fd_cmask = uap->newmask & ALLPERMS;
4484 * revoke(char *path)
4486 * Void all references to file by ripping underlying filesystem
4490 sys_revoke(struct revoke_args *uap)
4492 struct nlookupdata nd;
4499 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4501 error = nlookup(&nd);
4503 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4504 cred = crhold(nd.nl_cred);
4508 error = VOP_GETATTR(vp, &vattr);
4509 if (error == 0 && cred->cr_uid != vattr.va_uid)
4510 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4511 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4513 error = vrevoke(vp, cred);
4514 } else if (error == 0) {
4515 error = vrevoke(vp, cred);
4525 * getfh_args(char *fname, fhandle_t *fhp)
4527 * Get (NFS) file handle
4529 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4530 * mount. This allows nullfs mounts to be explicitly exported.
4532 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4534 * nullfs mounts of subdirectories are not safe. That is, it will
4535 * work, but you do not really have protection against access to
4536 * the related parent directories.
4539 sys_getfh(struct getfh_args *uap)
4541 struct thread *td = curthread;
4542 struct nlookupdata nd;
4549 * Must be super user
4551 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4555 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4557 error = nlookup(&nd);
4559 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4560 mp = nd.nl_nch.mount;
4563 bzero(&fh, sizeof(fh));
4564 fh.fh_fsid = mp->mnt_stat.f_fsid;
4565 error = VFS_VPTOFH(vp, &fh.fh_fid);
4568 error = copyout(&fh, uap->fhp, sizeof(fh));
4574 * fhopen_args(const struct fhandle *u_fhp, int flags)
4576 * syscall for the rpc.lockd to use to translate a NFS file handle into
4577 * an open descriptor.
4579 * warning: do not remove the priv_check() call or this becomes one giant
4583 sys_fhopen(struct fhopen_args *uap)
4585 struct thread *td = curthread;
4586 struct filedesc *fdp = td->td_proc->p_fd;
4591 struct vattr *vap = &vat;
4593 int fmode, mode, error = 0, type;
4599 * Must be super user
4601 error = priv_check(td, PRIV_ROOT);
4605 fmode = FFLAGS(uap->flags);
4608 * Why not allow a non-read/write open for our lockd?
4610 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4612 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4617 * Find the mount point
4619 mp = vfs_getvfs(&fhp.fh_fsid);
4624 /* now give me my vnode, it gets returned to me locked */
4625 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4629 * from now on we have to make sure not
4630 * to forget about the vnode
4631 * any error that causes an abort must vput(vp)
4632 * just set error = err and 'goto bad;'.
4638 if (vp->v_type == VLNK) {
4642 if (vp->v_type == VSOCK) {
4647 if (fmode & (FWRITE | O_TRUNC)) {
4648 if (vp->v_type == VDIR) {
4652 error = vn_writechk(vp, NULL);
4660 error = VOP_ACCESS(vp, mode, td->td_ucred);
4664 if (fmode & O_TRUNC) {
4665 vn_unlock(vp); /* XXX */
4666 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4669 error = VOP_SETATTR(vp, vap, td->td_ucred);
4675 * VOP_OPEN needs the file pointer so it can potentially override
4678 * WARNING! no f_nchandle will be associated when fhopen()ing a
4681 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4685 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4688 * setting f_ops this way prevents VOP_CLOSE from being
4689 * called or fdrop() releasing the vp from v_data. Since
4690 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4692 fp->f_ops = &badfileops;
4698 * The fp is given its own reference, we still have our ref and lock.
4700 * Assert that all regular files must be created with a VM object.
4702 if (vp->v_type == VREG && vp->v_object == NULL) {
4703 kprintf("fhopen: regular file did not "
4704 "have VM object: %p\n",
4710 * The open was successful. Handle any locking requirements.
4712 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4713 lf.l_whence = SEEK_SET;
4716 if (fmode & O_EXLOCK)
4717 lf.l_type = F_WRLCK;
4719 lf.l_type = F_RDLCK;
4720 if (fmode & FNONBLOCK)
4725 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK,
4728 * release our private reference.
4730 fsetfd(fdp, NULL, indx);
4735 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4736 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
4740 * Clean up. Associate the file pointer with the previously
4741 * reserved descriptor and return it.
4744 if (uap->flags & O_CLOEXEC)
4745 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4746 fsetfd(fdp, fp, indx);
4748 uap->sysmsg_result = indx;
4754 fsetfd(fdp, NULL, indx);
4765 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4768 sys_fhstat(struct fhstat_args *uap)
4770 struct thread *td = curthread;
4778 * Must be super user
4780 error = priv_check(td, PRIV_ROOT);
4784 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4788 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4791 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4792 error = vn_stat(vp, &sb, td->td_ucred);
4797 error = copyout(&sb, uap->sb, sizeof(sb));
4805 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4808 sys_fhstatfs(struct fhstatfs_args *uap)
4810 struct thread *td = curthread;
4811 struct proc *p = td->td_proc;
4816 char *fullpath, *freepath;
4821 * Must be super user
4823 if ((error = priv_check(td, PRIV_ROOT)))
4826 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4829 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4833 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4838 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4843 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4846 error = mount_path(p, mp, &fullpath, &freepath);
4849 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4850 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4851 kfree(freepath, M_TEMP);
4853 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4854 if (priv_check(td, PRIV_ROOT)) {
4855 bcopy(sp, &sb, sizeof(sb));
4856 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4859 error = copyout(sp, uap->buf, sizeof(*sp));
4868 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4871 sys_fhstatvfs(struct fhstatvfs_args *uap)
4873 struct thread *td = curthread;
4874 struct proc *p = td->td_proc;
4882 * Must be super user
4884 if ((error = priv_check(td, PRIV_ROOT)))
4887 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4890 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4894 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4899 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4902 sp = &mp->mnt_vstat;
4904 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4908 if (mp->mnt_flag & MNT_RDONLY)
4909 sp->f_flag |= ST_RDONLY;
4910 if (mp->mnt_flag & MNT_NOSUID)
4911 sp->f_flag |= ST_NOSUID;
4912 error = copyout(sp, uap->buf, sizeof(*sp));
4921 * Syscall to push extended attribute configuration information into the
4922 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4923 * a command (int cmd), and attribute name and misc data. For now, the
4924 * attribute name is left in userspace for consumption by the VFS_op.
4925 * It will probably be changed to be copied into sysspace by the
4926 * syscall in the future, once issues with various consumers of the
4927 * attribute code have raised their hands.
4929 * Currently this is used only by UFS Extended Attributes.
4932 sys_extattrctl(struct extattrctl_args *uap)
4934 struct nlookupdata nd;
4936 char attrname[EXTATTR_MAXNAMELEN];
4944 if (error == 0 && uap->filename) {
4945 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4948 error = nlookup(&nd);
4950 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4954 if (error == 0 && uap->attrname) {
4955 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4960 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4962 error = nlookup(&nd);
4964 error = ncp_writechk(&nd.nl_nch);
4966 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4968 uap->attrname, nd.nl_cred);
4977 * Syscall to get a named extended attribute on a file or directory.
4980 sys_extattr_set_file(struct extattr_set_file_args *uap)
4982 char attrname[EXTATTR_MAXNAMELEN];
4983 struct nlookupdata nd;
4989 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4995 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4997 error = nlookup(&nd);
4999 error = ncp_writechk(&nd.nl_nch);
5001 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5007 bzero(&auio, sizeof(auio));
5008 aiov.iov_base = uap->data;
5009 aiov.iov_len = uap->nbytes;
5010 auio.uio_iov = &aiov;
5011 auio.uio_iovcnt = 1;
5012 auio.uio_offset = 0;
5013 auio.uio_resid = uap->nbytes;
5014 auio.uio_rw = UIO_WRITE;
5015 auio.uio_td = curthread;
5017 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
5026 * Syscall to get a named extended attribute on a file or directory.
5029 sys_extattr_get_file(struct extattr_get_file_args *uap)
5031 char attrname[EXTATTR_MAXNAMELEN];
5032 struct nlookupdata nd;
5038 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5044 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5046 error = nlookup(&nd);
5048 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
5054 bzero(&auio, sizeof(auio));
5055 aiov.iov_base = uap->data;
5056 aiov.iov_len = uap->nbytes;
5057 auio.uio_iov = &aiov;
5058 auio.uio_iovcnt = 1;
5059 auio.uio_offset = 0;
5060 auio.uio_resid = uap->nbytes;
5061 auio.uio_rw = UIO_READ;
5062 auio.uio_td = curthread;
5064 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
5066 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
5074 * Syscall to delete a named extended attribute from a file or directory.
5075 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
5078 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
5080 char attrname[EXTATTR_MAXNAMELEN];
5081 struct nlookupdata nd;
5085 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5089 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5091 error = nlookup(&nd);
5093 error = ncp_writechk(&nd.nl_nch);
5095 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5097 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
5098 attrname, NULL, nd.nl_cred);
5107 * Determine if the mount is visible to the process.
5110 chroot_visible_mnt(struct mount *mp, struct proc *p)
5112 struct nchandle nch;
5115 * Traverse from the mount point upwards. If we hit the process
5116 * root then the mount point is visible to the process.
5118 nch = mp->mnt_ncmountpt;
5120 if (nch.mount == p->p_fd->fd_nrdir.mount &&
5121 nch.ncp == p->p_fd->fd_nrdir.ncp) {
5124 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
5125 nch = nch.mount->mnt_ncmounton;
5127 nch.ncp = nch.ncp->nc_parent;
5132 * If the mount point is not visible to the process, but the
5133 * process root is in a subdirectory of the mount, return
5136 if (p->p_fd->fd_nrdir.mount == mp)