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);
92 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");
98 * Virtual File System System Calls
102 * Mount a file system.
104 * mount_args(char *type, char *path, int flags, caddr_t data)
109 sys_mount(struct mount_args *uap)
111 struct thread *td = curthread;
114 struct mount *mp, *nullmp;
115 struct vfsconf *vfsp;
116 int error, flag = 0, flag2 = 0;
119 struct nlookupdata nd;
120 char fstypename[MFSNAMELEN];
128 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
132 * Do not allow NFS export by non-root users.
134 if (uap->flags & MNT_EXPORTED) {
135 error = priv_check(td, PRIV_ROOT);
140 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
142 if (priv_check(td, PRIV_ROOT))
143 uap->flags |= MNT_NOSUID | MNT_NODEV;
146 * Lookup the requested path and extract the nch and vnode.
148 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
150 if ((error = nlookup(&nd)) == 0) {
151 if (nd.nl_nch.ncp->nc_vp == NULL)
161 * If the target filesystem is resolved via a nullfs mount, then
162 * nd.nl_nch.mount will be pointing to the nullfs mount structure
163 * instead of the target file system. We need it in case we are
166 nullmp = nd.nl_nch.mount;
169 * Extract the locked+refd ncp and cleanup the nd structure
172 cache_zero(&nd.nl_nch);
175 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
176 (mp = cache_findmount(&nch)) != NULL) {
185 * now we have the locked ref'd nch and unreferenced vnode.
188 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
195 * Extract the file system type. We need to know this early, to take
196 * appropriate actions if we are dealing with a nullfs.
198 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
205 * Now we have an unlocked ref'd nch and a locked ref'd vp
207 if (uap->flags & MNT_UPDATE) {
208 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
215 if (strncmp(fstypename, "null", 5) == 0) {
223 flag2 = mp->mnt_kern_flag;
225 * We only allow the filesystem to be reloaded if it
226 * is currently mounted read-only.
228 if ((uap->flags & MNT_RELOAD) &&
229 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
232 error = EOPNOTSUPP; /* Needs translation */
236 * Only root, or the user that did the original mount is
237 * permitted to update it.
239 if (mp->mnt_stat.f_owner != cred->cr_uid &&
240 (error = priv_check(td, PRIV_ROOT))) {
245 if (vfs_busy(mp, LK_NOWAIT)) {
259 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
260 lwkt_gettoken(&mp->mnt_token);
267 * If the user is not root, ensure that they own the directory
268 * onto which we are attempting to mount.
270 if ((error = VOP_GETATTR(vp, &va)) ||
271 (va.va_uid != cred->cr_uid &&
272 (error = priv_check(td, PRIV_ROOT)))) {
277 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
282 if (vp->v_type != VDIR) {
288 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
294 vfsp = vfsconf_find_by_name(fstypename);
298 /* Only load modules for root (very important!) */
299 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
304 error = linker_load_file(fstypename, &lf);
305 if (error || lf == NULL) {
313 /* lookup again, see if the VFS was loaded */
314 vfsp = vfsconf_find_by_name(fstypename);
317 linker_file_unload(lf);
332 * Allocate and initialize the filesystem.
334 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
336 vfs_busy(mp, LK_NOWAIT);
337 mp->mnt_op = vfsp->vfc_vfsops;
339 mp->mnt_pbuf_count = nswbuf_kva / NSWBUF_SPLIT;
340 vfsp->vfc_refcount++;
341 mp->mnt_stat.f_type = vfsp->vfc_typenum;
342 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
343 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
344 mp->mnt_stat.f_owner = cred->cr_uid;
345 lwkt_gettoken(&mp->mnt_token);
349 * (per-mount token acquired at this point)
351 * Set the mount level flags.
353 if (uap->flags & MNT_RDONLY)
354 mp->mnt_flag |= MNT_RDONLY;
355 else if (mp->mnt_flag & MNT_RDONLY)
356 mp->mnt_kern_flag |= MNTK_WANTRDWR;
357 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
358 MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME |
359 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
360 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
362 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
363 MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE |
364 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
365 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
369 * Pre-set the mount's ALL_MPSAFE flags if specified in the vfsconf.
370 * This way the initial VFS_MOUNT() call will also be MPSAFE.
372 if (vfsp->vfc_flags & VFCF_MPSAFE)
373 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
376 * Mount the filesystem.
377 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
380 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
381 if (mp->mnt_flag & MNT_UPDATE) {
382 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
383 mp->mnt_flag &= ~MNT_RDONLY;
384 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
385 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
388 mp->mnt_kern_flag = flag2;
390 lwkt_reltoken(&mp->mnt_token);
396 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
399 * Put the new filesystem on the mount list after root. The mount
400 * point gets its own mnt_ncmountpt (unless the VFS already set one
401 * up) which represents the root of the mount. The lookup code
402 * detects the mount point going forward and checks the root of
403 * the mount going backwards.
405 * It is not necessary to invalidate or purge the vnode underneath
406 * because elements under the mount will be given their own glue
410 if (mp->mnt_ncmountpt.ncp == NULL) {
412 * Allocate, then unlock, but leave the ref intact.
413 * This is the mnt_refs (1) that we will retain
414 * through to the unmount.
416 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
417 cache_unlock(&mp->mnt_ncmountpt);
420 mp->mnt_ncmounton = nch; /* inherits ref */
422 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
424 cache_ismounting(mp);
425 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
427 mountlist_insert(mp, MNTINS_LAST);
429 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
430 error = vfs_allocate_syncvnode(mp);
431 lwkt_reltoken(&mp->mnt_token);
433 error = VFS_START(mp, 0);
435 KNOTE(&fs_klist, VQ_MOUNT);
437 vn_syncer_thr_stop(mp);
438 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
439 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
440 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
441 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
442 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
443 mp->mnt_vfc->vfc_refcount--;
444 lwkt_reltoken(&mp->mnt_token);
455 * Scan all active processes to see if any of them have a current
456 * or root directory onto which the new filesystem has just been
457 * mounted. If so, replace them with the new mount point.
459 * Both old_nch and new_nch are ref'd on call but not locked.
460 * new_nch must be temporarily locked so it can be associated with the
461 * vnode representing the root of the mount point.
463 struct checkdirs_info {
464 struct nchandle old_nch;
465 struct nchandle new_nch;
466 struct vnode *old_vp;
467 struct vnode *new_vp;
470 static int checkdirs_callback(struct proc *p, void *data);
473 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
475 struct checkdirs_info info;
481 * If the old mount point's vnode has a usecount of 1, it is not
482 * being held as a descriptor anywhere.
484 olddp = old_nch->ncp->nc_vp;
485 if (olddp == NULL || VREFCNT(olddp) == 1)
489 * Force the root vnode of the new mount point to be resolved
490 * so we can update any matching processes.
493 if (VFS_ROOT(mp, &newdp))
494 panic("mount: lost mount");
497 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
498 cache_setunresolved(new_nch);
499 cache_setvp(new_nch, newdp);
500 cache_unlock(new_nch);
503 * Special handling of the root node
505 if (rootvnode == olddp) {
507 vfs_cache_setroot(newdp, cache_hold(new_nch));
511 * Pass newdp separately so the callback does not have to access
512 * it via new_nch->ncp->nc_vp.
514 info.old_nch = *old_nch;
515 info.new_nch = *new_nch;
517 allproc_scan(checkdirs_callback, &info, 0);
522 * NOTE: callback is not MP safe because the scanned process's filedesc
523 * structure can be ripped out from under us, amoung other things.
526 checkdirs_callback(struct proc *p, void *data)
528 struct checkdirs_info *info = data;
529 struct filedesc *fdp;
530 struct nchandle ncdrop1;
531 struct nchandle ncdrop2;
532 struct vnode *vprele1;
533 struct vnode *vprele2;
535 if ((fdp = p->p_fd) != NULL) {
536 cache_zero(&ncdrop1);
537 cache_zero(&ncdrop2);
542 * MPUNSAFE - XXX fdp can be pulled out from under a
545 * A shared filedesc is ok, we don't have to copy it
546 * because we are making this change globally.
548 spin_lock(&fdp->fd_spin);
549 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
550 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
551 vprele1 = fdp->fd_cdir;
553 fdp->fd_cdir = info->new_vp;
554 ncdrop1 = fdp->fd_ncdir;
555 cache_copy(&info->new_nch, &fdp->fd_ncdir);
557 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
558 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
559 vprele2 = fdp->fd_rdir;
561 fdp->fd_rdir = info->new_vp;
562 ncdrop2 = fdp->fd_nrdir;
563 cache_copy(&info->new_nch, &fdp->fd_nrdir);
565 spin_unlock(&fdp->fd_spin);
567 cache_drop(&ncdrop1);
569 cache_drop(&ncdrop2);
579 * Unmount a file system.
581 * Note: unmount takes a path to the vnode mounted on as argument,
582 * not special file (as before).
584 * umount_args(char *path, int flags)
589 sys_unmount(struct unmount_args *uap)
591 struct thread *td = curthread;
592 struct proc *p __debugvar = td->td_proc;
593 struct mount *mp = NULL;
594 struct nlookupdata nd;
598 if (td->td_ucred->cr_prison != NULL) {
602 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
605 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
607 error = nlookup(&nd);
611 mp = nd.nl_nch.mount;
614 * Only root, or the user that did the original mount is
615 * permitted to unmount this filesystem.
617 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
618 (error = priv_check(td, PRIV_ROOT)))
622 * Don't allow unmounting the root file system.
624 if (mp->mnt_flag & MNT_ROOTFS) {
630 * Must be the root of the filesystem
632 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
638 * If no error try to issue the unmount. We lose our cache
639 * ref when we call nlookup_done so we must hold the mount point
640 * to prevent use-after-free races.
646 error = dounmount(mp, uap->flags, 0);
656 * Do the actual file system unmount (interlocked against the mountlist
657 * token and mp->mnt_token).
660 dounmount_interlock(struct mount *mp)
662 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
664 mp->mnt_kern_flag |= MNTK_UNMOUNT;
669 unmount_allproc_cb(struct proc *p, void *arg)
673 if (p->p_textnch.ncp == NULL)
676 mp = (struct mount *)arg;
677 if (p->p_textnch.mount == mp)
678 cache_drop(&p->p_textnch);
684 * The guts of the unmount code. The mount owns one ref and one hold
685 * count. If we successfully interlock the unmount, those refs are ours.
686 * (The ref is from mnt_ncmountpt).
688 * When halting we shortcut certain mount types such as devfs by not actually
689 * issuing the VFS_SYNC() or VFS_UNMOUNT(). They are still disconnected
690 * from the mountlist so higher-level filesytems can unmount cleanly.
692 * The mount types that allow QUICKHALT are: devfs, tmpfs, procfs.
695 dounmount(struct mount *mp, int flags, int halting)
697 struct namecache *ncp;
707 lwkt_gettoken(&mp->mnt_token);
710 * When halting, certain mount points can essentially just
711 * be unhooked and otherwise ignored.
713 if (halting && (mp->mnt_kern_flag & MNTK_QUICKHALT)) {
722 * Exclusive access for unmounting purposes.
724 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
728 * We now 'own' the last mp->mnt_refs
730 * Allow filesystems to detect that a forced unmount is in progress.
732 if (flags & MNT_FORCE)
733 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
734 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
735 error = lockmgr(&mp->mnt_lock, lflags);
737 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
738 if (mp->mnt_kern_flag & MNTK_MWAIT) {
739 mp->mnt_kern_flag &= ~MNTK_MWAIT;
745 if (mp->mnt_flag & MNT_EXPUBLIC)
746 vfs_setpublicfs(NULL, NULL, NULL);
748 vfs_msync(mp, MNT_WAIT);
749 async_flag = mp->mnt_flag & MNT_ASYNC;
750 mp->mnt_flag &=~ MNT_ASYNC;
753 * If this filesystem isn't aliasing other filesystems,
754 * try to invalidate any remaining namecache entries and
755 * check the count afterwords.
757 * We own the last mnt_refs by owning mnt_ncmountpt.
759 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
760 cache_lock(&mp->mnt_ncmountpt);
761 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
762 cache_unlock(&mp->mnt_ncmountpt);
764 cache_clearmntcache();
765 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
766 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
767 allproc_scan(&unmount_allproc_cb, mp, 0);
770 cache_clearmntcache();
771 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
772 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
774 if ((flags & MNT_FORCE) == 0) {
776 mount_warning(mp, "Cannot unmount: "
782 mount_warning(mp, "Forced unmount: "
793 * Decomission our special mnt_syncer vnode. This also stops
794 * the vnlru code. If we are unable to unmount we recommission
797 * Then sync the filesystem.
799 if ((vp = mp->mnt_syncer) != NULL) {
800 mp->mnt_syncer = NULL;
801 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
805 if (quickhalt == 0) {
806 if ((mp->mnt_flag & MNT_RDONLY) == 0)
807 VFS_SYNC(mp, MNT_WAIT);
811 * nchandle records ref the mount structure. Expect a count of 1
812 * (our mount->mnt_ncmountpt).
814 * Scans can get temporary refs on a mountpoint (thought really
815 * heavy duty stuff like cache_findmount() do not).
817 if (mp->mnt_refs != 1)
818 cache_clearmntcache();
819 for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
820 cache_unmounting(mp);
821 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
822 cache_clearmntcache();
824 if (mp->mnt_refs != 1) {
825 if ((flags & MNT_FORCE) == 0) {
826 mount_warning(mp, "Cannot unmount: "
827 "%d mount refs still present",
831 mount_warning(mp, "Forced unmount: "
832 "%d mount refs still present",
839 * So far so good, sync the filesystem once more and
840 * call the VFS unmount code if the sync succeeds.
842 if (error == 0 && quickhalt == 0) {
843 if (mp->mnt_flag & MNT_RDONLY) {
844 error = VFS_UNMOUNT(mp, flags);
846 error = VFS_SYNC(mp, MNT_WAIT);
848 (error == EOPNOTSUPP) || /* No sync */
849 (flags & MNT_FORCE)) {
850 error = VFS_UNMOUNT(mp, flags);
856 * If an error occurred we can still recover, restoring the
857 * syncer vnode and misc flags.
860 if (mp->mnt_syncer == NULL)
861 vfs_allocate_syncvnode(mp);
862 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
863 mp->mnt_flag |= async_flag;
864 lockmgr(&mp->mnt_lock, LK_RELEASE);
865 if (mp->mnt_kern_flag & MNTK_MWAIT) {
866 mp->mnt_kern_flag &= ~MNTK_MWAIT;
872 * Clean up any journals still associated with the mount after
873 * filesystem activity has ceased.
875 journal_remove_all_journals(mp,
876 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
878 mountlist_remove(mp);
881 * Remove any installed vnode ops here so the individual VFSs don't
884 * mnt_refs should go to zero when we scrap mnt_ncmountpt.
886 * When quickhalting we have to keep these intact because the
887 * underlying vnodes have not been destroyed, and some might be
890 if (quickhalt == 0) {
891 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
892 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
893 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
894 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
895 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
898 if (mp->mnt_ncmountpt.ncp != NULL) {
899 nch = mp->mnt_ncmountpt;
900 cache_zero(&mp->mnt_ncmountpt);
901 cache_clrmountpt(&nch);
904 if (mp->mnt_ncmounton.ncp != NULL) {
905 cache_unmounting(mp);
906 nch = mp->mnt_ncmounton;
907 cache_zero(&mp->mnt_ncmounton);
908 cache_clrmountpt(&nch);
912 mp->mnt_vfc->vfc_refcount--;
915 * If not quickhalting the mount, we expect there to be no
918 if (quickhalt == 0 && !TAILQ_EMPTY(&mp->mnt_nvnodelist))
919 panic("unmount: dangling vnode");
924 lockmgr(&mp->mnt_lock, LK_RELEASE);
925 if (mp->mnt_kern_flag & MNTK_MWAIT) {
926 mp->mnt_kern_flag &= ~MNTK_MWAIT;
931 * If we reach here and freeok != 0 we must free the mount.
932 * mnt_refs should already have dropped to 0, so if it is not
933 * zero we must cycle the caches and wait.
935 * When we are satisfied that the mount has disconnected we can
936 * drop the hold on the mp that represented the mount (though the
937 * caller might actually have another, so the caller's drop may
938 * do the actual free).
941 if (mp->mnt_refs > 0)
942 cache_clearmntcache();
943 while (mp->mnt_refs > 0) {
944 cache_unmounting(mp);
946 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
947 cache_clearmntcache();
949 lwkt_reltoken(&mp->mnt_token);
953 cache_clearmntcache();
956 KNOTE(&fs_klist, VQ_UNMOUNT);
959 lwkt_reltoken(&mp->mnt_token);
965 mount_warning(struct mount *mp, const char *ctl, ...)
972 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
973 &ptr, &buf, 0) == 0) {
974 kprintf("unmount(%s): ", ptr);
979 kprintf("unmount(%p", mp);
980 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
981 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
990 * Shim cache_fullpath() to handle the case where a process is chrooted into
991 * a subdirectory of a mount. In this case if the root mount matches the
992 * process root directory's mount we have to specify the process's root
993 * directory instead of the mount point, because the mount point might
994 * be above the root directory.
998 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
1000 struct nchandle *nch;
1002 if (p && p->p_fd->fd_nrdir.mount == mp)
1003 nch = &p->p_fd->fd_nrdir;
1005 nch = &mp->mnt_ncmountpt;
1006 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
1010 * Sync each mounted filesystem.
1014 static int syncprt = 0;
1015 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
1018 static int sync_callback(struct mount *mp, void *data);
1021 sys_sync(struct sync_args *uap)
1023 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
1029 sync_callback(struct mount *mp, void *data __unused)
1033 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1034 lwkt_gettoken(&mp->mnt_token);
1035 asyncflag = mp->mnt_flag & MNT_ASYNC;
1036 mp->mnt_flag &= ~MNT_ASYNC;
1037 lwkt_reltoken(&mp->mnt_token);
1038 vfs_msync(mp, MNT_NOWAIT);
1039 VFS_SYNC(mp, MNT_NOWAIT);
1040 lwkt_gettoken(&mp->mnt_token);
1041 mp->mnt_flag |= asyncflag;
1042 lwkt_reltoken(&mp->mnt_token);
1047 /* XXX PRISON: could be per prison flag */
1048 static int prison_quotas;
1050 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
1054 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
1056 * Change filesystem quotas.
1061 sys_quotactl(struct quotactl_args *uap)
1063 struct nlookupdata nd;
1069 if (td->td_ucred->cr_prison && !prison_quotas) {
1074 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1076 error = nlookup(&nd);
1078 mp = nd.nl_nch.mount;
1079 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
1080 uap->arg, nd.nl_cred);
1088 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1089 * void *buf, int buflen)
1091 * This function operates on a mount point and executes the specified
1092 * operation using the specified control data, and possibly returns data.
1094 * The actual number of bytes stored in the result buffer is returned, 0
1095 * if none, otherwise an error is returned.
1100 sys_mountctl(struct mountctl_args *uap)
1102 struct thread *td = curthread;
1110 * Sanity and permissions checks. We must be root.
1112 if (td->td_ucred->cr_prison != NULL)
1114 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1115 (error = priv_check(td, PRIV_ROOT)) != 0)
1119 * Argument length checks
1121 if (uap->ctllen < 0 || uap->ctllen > 1024)
1123 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1125 if (uap->path == NULL)
1129 * Allocate the necessary buffers and copyin data
1131 path = objcache_get(namei_oc, M_WAITOK);
1132 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1137 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1138 error = copyin(uap->ctl, ctl, uap->ctllen);
1143 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1146 * Validate the descriptor
1149 fp = holdfp(td, uap->fd, -1);
1159 * Execute the internal kernel function and clean up.
1161 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen,
1162 buf, uap->buflen, &uap->sysmsg_result);
1164 dropfp(td, uap->fd, fp);
1165 if (error == 0 && uap->sysmsg_result > 0)
1166 error = copyout(buf, uap->buf, uap->sysmsg_result);
1169 objcache_put(namei_oc, path);
1178 * Execute a mount control operation by resolving the path to a mount point
1179 * and calling vop_mountctl().
1181 * Use the mount point from the nch instead of the vnode so nullfs mounts
1182 * can properly spike the VOP.
1185 kern_mountctl(const char *path, int op, struct file *fp,
1186 const void *ctl, int ctllen,
1187 void *buf, int buflen, int *res)
1190 struct nlookupdata nd;
1191 struct nchandle nch;
1197 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1200 error = nlookup(&nd);
1205 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1212 * Yes, all this is needed to use the nch.mount below, because
1213 * we must maintain a ref on the mount to avoid ripouts (e.g.
1214 * due to heavy mount/unmount use by synth or poudriere).
1217 cache_zero(&nd.nl_nch);
1225 * Must be the root of the filesystem
1227 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1232 if (mp == NULL || mp->mnt_kern_flag & MNTK_UNMOUNT) {
1233 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1239 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1248 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1250 struct thread *td = curthread;
1251 struct proc *p = td->td_proc;
1254 char *fullpath, *freepath;
1257 if ((error = nlookup(nd)) != 0)
1259 mp = nd->nl_nch.mount;
1261 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1264 error = mount_path(p, mp, &fullpath, &freepath);
1267 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1268 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1269 kfree(freepath, M_TEMP);
1271 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1272 bcopy(sp, buf, sizeof(*buf));
1273 /* Only root should have access to the fsid's. */
1274 if (priv_check(td, PRIV_ROOT))
1275 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1280 * statfs_args(char *path, struct statfs *buf)
1282 * Get filesystem statistics.
1285 sys_statfs(struct statfs_args *uap)
1287 struct nlookupdata nd;
1291 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1293 error = kern_statfs(&nd, &buf);
1296 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1301 kern_fstatfs(int fd, struct statfs *buf)
1303 struct thread *td = curthread;
1304 struct proc *p = td->td_proc;
1308 char *fullpath, *freepath;
1312 if ((error = holdvnode(td, fd, &fp)) != 0)
1316 * Try to use mount info from any overlays rather than the
1317 * mount info for the underlying vnode, otherwise we will
1318 * fail when operating on null-mounted paths inside a chroot.
1320 if ((mp = fp->f_nchandle.mount) == NULL)
1321 mp = ((struct vnode *)fp->f_data)->v_mount;
1326 if (fp->f_cred == NULL) {
1331 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1334 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1336 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1337 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1338 kfree(freepath, M_TEMP);
1340 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1341 bcopy(sp, buf, sizeof(*buf));
1343 /* Only root should have access to the fsid's. */
1344 if (priv_check(td, PRIV_ROOT))
1345 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1353 * fstatfs_args(int fd, struct statfs *buf)
1355 * Get filesystem statistics.
1358 sys_fstatfs(struct fstatfs_args *uap)
1363 error = kern_fstatfs(uap->fd, &buf);
1366 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1371 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1377 if ((error = nlookup(nd)) != 0)
1379 mp = nd->nl_nch.mount;
1380 sp = &mp->mnt_vstat;
1381 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1385 if (mp->mnt_flag & MNT_RDONLY)
1386 sp->f_flag |= ST_RDONLY;
1387 if (mp->mnt_flag & MNT_NOSUID)
1388 sp->f_flag |= ST_NOSUID;
1389 bcopy(sp, buf, sizeof(*buf));
1394 * statfs_args(char *path, struct statfs *buf)
1396 * Get filesystem statistics.
1399 sys_statvfs(struct statvfs_args *uap)
1401 struct nlookupdata nd;
1405 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1407 error = kern_statvfs(&nd, &buf);
1410 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1415 kern_fstatvfs(int fd, struct statvfs *buf)
1417 struct thread *td = curthread;
1423 if ((error = holdvnode(td, fd, &fp)) != 0)
1425 if ((mp = fp->f_nchandle.mount) == NULL)
1426 mp = ((struct vnode *)fp->f_data)->v_mount;
1431 if (fp->f_cred == NULL) {
1435 sp = &mp->mnt_vstat;
1436 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1440 if (mp->mnt_flag & MNT_RDONLY)
1441 sp->f_flag |= ST_RDONLY;
1442 if (mp->mnt_flag & MNT_NOSUID)
1443 sp->f_flag |= ST_NOSUID;
1445 bcopy(sp, buf, sizeof(*buf));
1453 * fstatfs_args(int fd, struct statfs *buf)
1455 * Get filesystem statistics.
1458 sys_fstatvfs(struct fstatvfs_args *uap)
1463 error = kern_fstatvfs(uap->fd, &buf);
1466 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1471 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1473 * Get statistics on all filesystems.
1476 struct getfsstat_info {
1477 struct statfs *sfsp;
1485 static int getfsstat_callback(struct mount *, void *);
1488 sys_getfsstat(struct getfsstat_args *uap)
1490 struct thread *td = curthread;
1491 struct getfsstat_info info;
1493 bzero(&info, sizeof(info));
1495 info.maxcount = uap->bufsize / sizeof(struct statfs);
1496 info.sfsp = uap->buf;
1498 info.flags = uap->flags;
1501 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1502 if (info.sfsp && info.count > info.maxcount)
1503 uap->sysmsg_result = info.maxcount;
1505 uap->sysmsg_result = info.count;
1506 return (info.error);
1510 getfsstat_callback(struct mount *mp, void *data)
1512 struct getfsstat_info *info = data;
1518 if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1521 if (info->sfsp && info->count < info->maxcount) {
1525 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1526 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1527 * overrides MNT_WAIT.
1529 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1530 (info->flags & MNT_WAIT)) &&
1531 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1534 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1536 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1538 info->error = error;
1541 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1542 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1543 kfree(freepath, M_TEMP);
1545 error = copyout(sp, info->sfsp, sizeof(*sp));
1547 info->error = error;
1557 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1558 long bufsize, int flags)
1560 * Get statistics on all filesystems.
1563 struct getvfsstat_info {
1564 struct statfs *sfsp;
1565 struct statvfs *vsfsp;
1573 static int getvfsstat_callback(struct mount *, void *);
1576 sys_getvfsstat(struct getvfsstat_args *uap)
1578 struct thread *td = curthread;
1579 struct getvfsstat_info info;
1581 bzero(&info, sizeof(info));
1583 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1584 info.sfsp = uap->buf;
1585 info.vsfsp = uap->vbuf;
1587 info.flags = uap->flags;
1590 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1591 if (info.vsfsp && info.count > info.maxcount)
1592 uap->sysmsg_result = info.maxcount;
1594 uap->sysmsg_result = info.count;
1595 return (info.error);
1599 getvfsstat_callback(struct mount *mp, void *data)
1601 struct getvfsstat_info *info = data;
1603 struct statvfs *vsp;
1608 if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1611 if (info->vsfsp && info->count < info->maxcount) {
1613 vsp = &mp->mnt_vstat;
1616 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1617 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1618 * overrides MNT_WAIT.
1620 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1621 (info->flags & MNT_WAIT)) &&
1622 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1625 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1627 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1628 (info->flags & MNT_WAIT)) &&
1629 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1633 if (mp->mnt_flag & MNT_RDONLY)
1634 vsp->f_flag |= ST_RDONLY;
1635 if (mp->mnt_flag & MNT_NOSUID)
1636 vsp->f_flag |= ST_NOSUID;
1638 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1640 info->error = error;
1643 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1644 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1645 kfree(freepath, M_TEMP);
1647 error = copyout(sp, info->sfsp, sizeof(*sp));
1649 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1651 info->error = error;
1663 * fchdir_args(int fd)
1665 * Change current working directory to a given file descriptor.
1668 sys_fchdir(struct fchdir_args *uap)
1670 struct thread *td = curthread;
1671 struct proc *p = td->td_proc;
1672 struct filedesc *fdp = p->p_fd;
1673 struct vnode *vp, *ovp;
1676 struct nchandle nch, onch, tnch;
1679 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
1681 lwkt_gettoken(&p->p_token);
1682 vp = (struct vnode *)fp->f_data;
1684 vn_lock(vp, LK_SHARED | LK_RETRY);
1685 if (fp->f_nchandle.ncp == NULL)
1688 error = checkvp_chdir(vp, td);
1693 cache_copy(&fp->f_nchandle, &nch);
1696 * If the ncp has become a mount point, traverse through
1700 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1701 (mp = cache_findmount(&nch)) != NULL
1703 error = nlookup_mp(mp, &tnch);
1705 cache_unlock(&tnch); /* leave ref intact */
1707 vp = tnch.ncp->nc_vp;
1708 error = vget(vp, LK_SHARED);
1709 KKASSERT(error == 0);
1713 cache_dropmount(mp);
1716 spin_lock(&fdp->fd_spin);
1718 onch = fdp->fd_ncdir;
1720 fdp->fd_ncdir = nch;
1721 spin_unlock(&fdp->fd_spin);
1722 vn_unlock(vp); /* leave ref intact */
1731 lwkt_reltoken(&p->p_token);
1736 kern_chdir(struct nlookupdata *nd)
1738 struct thread *td = curthread;
1739 struct proc *p = td->td_proc;
1740 struct filedesc *fdp = p->p_fd;
1741 struct vnode *vp, *ovp;
1742 struct nchandle onch;
1745 nd->nl_flags |= NLC_SHAREDLOCK;
1746 if ((error = nlookup(nd)) != 0)
1748 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1750 if ((error = vget(vp, LK_SHARED)) != 0)
1753 lwkt_gettoken(&p->p_token);
1754 error = checkvp_chdir(vp, td);
1757 spin_lock(&fdp->fd_spin);
1759 onch = fdp->fd_ncdir;
1760 fdp->fd_ncdir = nd->nl_nch;
1762 spin_unlock(&fdp->fd_spin);
1763 cache_unlock(&nd->nl_nch); /* leave reference intact */
1766 cache_zero(&nd->nl_nch);
1770 lwkt_reltoken(&p->p_token);
1775 * chdir_args(char *path)
1777 * Change current working directory (``.'').
1780 sys_chdir(struct chdir_args *uap)
1782 struct nlookupdata nd;
1785 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1787 error = kern_chdir(&nd);
1793 * Helper function for raised chroot(2) security function: Refuse if
1794 * any filedescriptors are open directories.
1797 chroot_refuse_vdir_fds(thread_t td, struct filedesc *fdp)
1804 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1805 if ((error = holdvnode(td, fd, &fp)) != 0)
1807 vp = (struct vnode *)fp->f_data;
1808 if (vp->v_type != VDIR) {
1819 * This sysctl determines if we will allow a process to chroot(2) if it
1820 * has a directory open:
1821 * 0: disallowed for all processes.
1822 * 1: allowed for processes that were not already chroot(2)'ed.
1823 * 2: allowed for all processes.
1826 static int chroot_allow_open_directories = 1;
1828 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1829 &chroot_allow_open_directories, 0, "");
1832 * chroot to the specified namecache entry. We obtain the vp from the
1833 * namecache data. The passed ncp must be locked and referenced and will
1834 * remain locked and referenced on return.
1837 kern_chroot(struct nchandle *nch)
1839 struct thread *td = curthread;
1840 struct proc *p = td->td_proc;
1841 struct filedesc *fdp = p->p_fd;
1846 * Only privileged user can chroot
1848 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1853 * Disallow open directory descriptors (fchdir() breakouts).
1855 if (chroot_allow_open_directories == 0 ||
1856 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1857 if ((error = chroot_refuse_vdir_fds(td, fdp)) != 0)
1860 if ((vp = nch->ncp->nc_vp) == NULL)
1863 if ((error = vget(vp, LK_SHARED)) != 0)
1867 * Check the validity of vp as a directory to change to and
1868 * associate it with rdir/jdir.
1870 error = checkvp_chdir(vp, td);
1871 vn_unlock(vp); /* leave reference intact */
1873 lwkt_gettoken(&p->p_token);
1874 vrele(fdp->fd_rdir);
1875 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1876 cache_drop(&fdp->fd_nrdir);
1877 cache_copy(nch, &fdp->fd_nrdir);
1878 if (fdp->fd_jdir == NULL) {
1881 cache_copy(nch, &fdp->fd_njdir);
1883 if ((p->p_flags & P_DIDCHROOT) == 0) {
1884 p->p_flags |= P_DIDCHROOT;
1885 if (p->p_depth <= 65535 - 32)
1888 lwkt_reltoken(&p->p_token);
1896 * chroot_args(char *path)
1898 * Change notion of root (``/'') directory.
1901 sys_chroot(struct chroot_args *uap)
1903 struct thread *td __debugvar = curthread;
1904 struct nlookupdata nd;
1907 KKASSERT(td->td_proc);
1908 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1910 nd.nl_flags |= NLC_EXEC;
1911 error = nlookup(&nd);
1913 error = kern_chroot(&nd.nl_nch);
1920 sys_chroot_kernel(struct chroot_kernel_args *uap)
1922 struct thread *td = curthread;
1923 struct nlookupdata nd;
1924 struct nchandle *nch;
1928 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1932 error = nlookup(&nd);
1938 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1942 if ((vp = nch->ncp->nc_vp) == NULL) {
1947 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1950 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1951 vfs_cache_setroot(vp, cache_hold(nch));
1960 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1961 * determine whether it is legal to chdir to the vnode. The vnode's state
1962 * is not changed by this call.
1965 checkvp_chdir(struct vnode *vp, struct thread *td)
1969 if (vp->v_type != VDIR)
1972 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1977 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1979 struct thread *td = curthread;
1980 struct proc *p = td->td_proc;
1981 struct lwp *lp = td->td_lwp;
1982 struct filedesc *fdp = p->p_fd;
1987 int type, indx, error = 0;
1990 if ((oflags & O_ACCMODE) == O_ACCMODE)
1992 flags = FFLAGS(oflags);
1993 error = falloc(lp, &nfp, NULL);
1997 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
2000 * XXX p_dupfd is a real mess. It allows a device to return a
2001 * file descriptor to be duplicated rather then doing the open
2007 * Call vn_open() to do the lookup and assign the vnode to the
2008 * file pointer. vn_open() does not change the ref count on fp
2009 * and the vnode, on success, will be inherited by the file pointer
2012 * Request a shared lock on the vnode if possible.
2014 * Executable binaries can race VTEXT against O_RDWR opens, so
2015 * use an exclusive lock for O_RDWR opens as well.
2017 * NOTE: We need a flag to separate terminal vnode locking from
2018 * parent locking. O_CREAT needs parent locking, but O_TRUNC
2019 * and O_RDWR only need to lock the terminal vnode exclusively.
2021 nd->nl_flags |= NLC_LOCKVP;
2022 if ((flags & (O_CREAT|O_TRUNC|O_RDWR)) == 0)
2023 nd->nl_flags |= NLC_SHAREDLOCK;
2025 error = vn_open(nd, fp, flags, cmode);
2030 * handle special fdopen() case. bleh. dupfdopen() is
2031 * responsible for dropping the old contents of ofiles[indx]
2034 * Note that fsetfd() will add a ref to fp which represents
2035 * the fd_files[] assignment. We must still drop our
2038 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
2039 if (fdalloc(p, 0, &indx) == 0) {
2040 error = dupfdopen(td, indx, lp->lwp_dupfd, flags, error);
2043 fdrop(fp); /* our ref */
2046 fsetfd(fdp, NULL, indx);
2049 fdrop(fp); /* our ref */
2050 if (error == ERESTART)
2056 * ref the vnode for ourselves so it can't be ripped out from under
2057 * is. XXX need an ND flag to request that the vnode be returned
2060 * Reserve a file descriptor but do not assign it until the open
2063 vp = (struct vnode *)fp->f_data;
2065 if ((error = fdalloc(p, 0, &indx)) != 0) {
2072 * If no error occurs the vp will have been assigned to the file
2077 if (flags & (O_EXLOCK | O_SHLOCK)) {
2078 lf.l_whence = SEEK_SET;
2081 if (flags & O_EXLOCK)
2082 lf.l_type = F_WRLCK;
2084 lf.l_type = F_RDLCK;
2085 if (flags & FNONBLOCK)
2090 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2092 * lock request failed. Clean up the reserved
2096 fsetfd(fdp, NULL, indx);
2100 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
2104 * Assert that all regular file vnodes were created with a object.
2106 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
2107 ("open: regular file has no backing object after vn_open"));
2113 * release our private reference, leaving the one associated with the
2114 * descriptor table intact.
2116 if (oflags & O_CLOEXEC)
2117 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
2118 fsetfd(fdp, fp, indx);
2126 * open_args(char *path, int flags, int mode)
2128 * Check permissions, allocate an open file structure,
2129 * and call the device open routine if any.
2132 sys_open(struct open_args *uap)
2134 struct nlookupdata nd;
2137 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2139 error = kern_open(&nd, uap->flags,
2140 uap->mode, &uap->sysmsg_result);
2147 * openat_args(int fd, char *path, int flags, int mode)
2150 sys_openat(struct openat_args *uap)
2152 struct nlookupdata nd;
2156 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2158 error = kern_open(&nd, uap->flags, uap->mode,
2159 &uap->sysmsg_result);
2161 nlookup_done_at(&nd, fp);
2166 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2168 struct thread *td = curthread;
2169 struct proc *p = td->td_proc;
2178 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2179 vattr.va_rmajor = rmajor;
2180 vattr.va_rminor = rminor;
2182 switch (mode & S_IFMT) {
2183 case S_IFMT: /* used by badsect to flag bad sectors */
2184 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2185 vattr.va_type = VBAD;
2188 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2189 vattr.va_type = VCHR;
2192 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2193 vattr.va_type = VBLK;
2196 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2199 case S_IFDIR: /* special directories support for HAMMER */
2200 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2201 vattr.va_type = VDIR;
2212 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2213 if ((error = nlookup(nd)) != 0)
2215 if (nd->nl_nch.ncp->nc_vp)
2217 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2221 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2222 nd->nl_cred, NAMEI_CREATE);
2225 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2226 &vp, nd->nl_cred, &vattr);
2234 * mknod_args(char *path, int mode, int dev)
2236 * Create a special file.
2239 sys_mknod(struct mknod_args *uap)
2241 struct nlookupdata nd;
2244 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2246 error = kern_mknod(&nd, uap->mode,
2247 umajor(uap->dev), uminor(uap->dev));
2254 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2256 * Create a special file. The path is relative to the directory associated
2260 sys_mknodat(struct mknodat_args *uap)
2262 struct nlookupdata nd;
2266 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2268 error = kern_mknod(&nd, uap->mode,
2269 umajor(uap->dev), uminor(uap->dev));
2271 nlookup_done_at(&nd, fp);
2276 kern_mkfifo(struct nlookupdata *nd, int mode)
2278 struct thread *td = curthread;
2279 struct proc *p = td->td_proc;
2286 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2287 if ((error = nlookup(nd)) != 0)
2289 if (nd->nl_nch.ncp->nc_vp)
2291 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2295 vattr.va_type = VFIFO;
2296 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2298 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2305 * mkfifo_args(char *path, int mode)
2307 * Create a named pipe.
2310 sys_mkfifo(struct mkfifo_args *uap)
2312 struct nlookupdata nd;
2315 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2317 error = kern_mkfifo(&nd, uap->mode);
2323 * mkfifoat_args(int fd, char *path, mode_t mode)
2325 * Create a named pipe. The path is relative to the directory associated
2329 sys_mkfifoat(struct mkfifoat_args *uap)
2331 struct nlookupdata nd;
2335 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2337 error = kern_mkfifo(&nd, uap->mode);
2338 nlookup_done_at(&nd, fp);
2342 static int hardlink_check_uid = 0;
2343 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2344 &hardlink_check_uid, 0,
2345 "Unprivileged processes cannot create hard links to files owned by other "
2347 static int hardlink_check_gid = 0;
2348 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2349 &hardlink_check_gid, 0,
2350 "Unprivileged processes cannot create hard links to files owned by other "
2354 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2360 * Shortcut if disabled
2362 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2366 * Privileged user can always hardlink
2368 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2372 * Otherwise only if the originating file is owned by the
2373 * same user or group. Note that any group is allowed if
2374 * the file is owned by the caller.
2376 error = VOP_GETATTR(vp, &va);
2380 if (hardlink_check_uid) {
2381 if (cred->cr_uid != va.va_uid)
2385 if (hardlink_check_gid) {
2386 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2394 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2396 struct thread *td = curthread;
2401 * Lookup the source and obtained a locked vnode.
2403 * You may only hardlink a file which you have write permission
2404 * on or which you own.
2406 * XXX relookup on vget failure / race ?
2409 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2410 if ((error = nlookup(nd)) != 0)
2412 vp = nd->nl_nch.ncp->nc_vp;
2413 KKASSERT(vp != NULL);
2414 if (vp->v_type == VDIR)
2415 return (EPERM); /* POSIX */
2416 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2418 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2422 * Unlock the source so we can lookup the target without deadlocking
2423 * (XXX vp is locked already, possible other deadlock?). The target
2426 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2427 nd->nl_flags &= ~NLC_NCPISLOCKED;
2428 cache_unlock(&nd->nl_nch);
2431 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2432 if ((error = nlookup(linknd)) != 0) {
2436 if (linknd->nl_nch.ncp->nc_vp) {
2440 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2447 * Finally run the new API VOP.
2449 error = can_hardlink(vp, td, td->td_ucred);
2451 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2452 vp, linknd->nl_cred);
2459 * link_args(char *path, char *link)
2461 * Make a hard file link.
2464 sys_link(struct link_args *uap)
2466 struct nlookupdata nd, linknd;
2469 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2471 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2473 error = kern_link(&nd, &linknd);
2474 nlookup_done(&linknd);
2481 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2483 * Make a hard file link. The path1 argument is relative to the directory
2484 * associated with fd1, and similarly the path2 argument is relative to
2485 * the directory associated with fd2.
2488 sys_linkat(struct linkat_args *uap)
2490 struct nlookupdata nd, linknd;
2491 struct file *fp1, *fp2;
2494 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2495 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2497 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2498 uap->path2, UIO_USERSPACE, 0);
2500 error = kern_link(&nd, &linknd);
2501 nlookup_done_at(&linknd, fp2);
2503 nlookup_done_at(&nd, fp1);
2508 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2516 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2517 if ((error = nlookup(nd)) != 0)
2519 if (nd->nl_nch.ncp->nc_vp)
2521 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2525 vattr.va_mode = mode;
2526 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2533 * symlink(char *path, char *link)
2535 * Make a symbolic link.
2538 sys_symlink(struct symlink_args *uap)
2540 struct thread *td = curthread;
2541 struct nlookupdata nd;
2546 path = objcache_get(namei_oc, M_WAITOK);
2547 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2549 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2551 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2552 error = kern_symlink(&nd, path, mode);
2556 objcache_put(namei_oc, path);
2561 * symlinkat_args(char *path1, int fd, char *path2)
2563 * Make a symbolic link. The path2 argument is relative to the directory
2564 * associated with fd.
2567 sys_symlinkat(struct symlinkat_args *uap)
2569 struct thread *td = curthread;
2570 struct nlookupdata nd;
2576 path1 = objcache_get(namei_oc, M_WAITOK);
2577 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2579 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2582 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2583 error = kern_symlink(&nd, path1, mode);
2585 nlookup_done_at(&nd, fp);
2587 objcache_put(namei_oc, path1);
2592 * undelete_args(char *path)
2594 * Delete a whiteout from the filesystem.
2597 sys_undelete(struct undelete_args *uap)
2599 struct nlookupdata nd;
2602 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2604 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2606 error = nlookup(&nd);
2608 error = ncp_writechk(&nd.nl_nch);
2610 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2618 kern_unlink(struct nlookupdata *nd)
2623 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2624 if ((error = nlookup(nd)) != 0)
2626 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2628 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2633 * unlink_args(char *path)
2635 * Delete a name from the filesystem.
2638 sys_unlink(struct unlink_args *uap)
2640 struct nlookupdata nd;
2643 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2645 error = kern_unlink(&nd);
2652 * unlinkat_args(int fd, char *path, int flags)
2654 * Delete the file or directory entry pointed to by fd/path.
2657 sys_unlinkat(struct unlinkat_args *uap)
2659 struct nlookupdata nd;
2663 if (uap->flags & ~AT_REMOVEDIR)
2666 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2668 if (uap->flags & AT_REMOVEDIR)
2669 error = kern_rmdir(&nd);
2671 error = kern_unlink(&nd);
2673 nlookup_done_at(&nd, fp);
2678 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2680 struct thread *td = curthread;
2687 fp = holdfp(td, fd, -1);
2690 if (fp->f_type != DTYPE_VNODE) {
2694 vp = (struct vnode *)fp->f_data;
2698 spin_lock(&fp->f_spin);
2699 new_offset = fp->f_offset + offset;
2703 error = VOP_GETATTR(vp, &vattr);
2704 spin_lock(&fp->f_spin);
2705 new_offset = offset + vattr.va_size;
2708 new_offset = offset;
2710 spin_lock(&fp->f_spin);
2715 spin_lock(&fp->f_spin);
2720 * Validate the seek position. Negative offsets are not allowed
2721 * for regular files or directories.
2723 * Normally we would also not want to allow negative offsets for
2724 * character and block-special devices. However kvm addresses
2725 * on 64 bit architectures might appear to be negative and must
2729 if (new_offset < 0 &&
2730 (vp->v_type == VREG || vp->v_type == VDIR)) {
2733 fp->f_offset = new_offset;
2736 *res = fp->f_offset;
2737 spin_unlock(&fp->f_spin);
2745 * lseek_args(int fd, int pad, off_t offset, int whence)
2747 * Reposition read/write file offset.
2750 sys_lseek(struct lseek_args *uap)
2754 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2755 &uap->sysmsg_offset);
2761 * Check if current process can access given file. amode is a bitmask of *_OK
2762 * access bits. flags is a bitmask of AT_* flags.
2765 kern_access(struct nlookupdata *nd, int amode, int flags)
2770 if (flags & ~AT_EACCESS)
2772 nd->nl_flags |= NLC_SHAREDLOCK;
2773 if ((error = nlookup(nd)) != 0)
2776 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2780 /* Flags == 0 means only check for existence. */
2789 if ((mode & VWRITE) == 0 ||
2790 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2791 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2794 * If the file handle is stale we have to re-resolve the
2795 * entry with the ncp held exclusively. This is a hack
2798 if (error == ESTALE) {
2800 cache_unlock(&nd->nl_nch);
2801 cache_lock(&nd->nl_nch);
2802 cache_setunresolved(&nd->nl_nch);
2803 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2816 * access_args(char *path, int flags)
2818 * Check access permissions.
2821 sys_access(struct access_args *uap)
2823 struct nlookupdata nd;
2826 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2828 error = kern_access(&nd, uap->flags, 0);
2835 * eaccess_args(char *path, int flags)
2837 * Check access permissions.
2840 sys_eaccess(struct eaccess_args *uap)
2842 struct nlookupdata nd;
2845 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2847 error = kern_access(&nd, uap->flags, AT_EACCESS);
2854 * faccessat_args(int fd, char *path, int amode, int flags)
2856 * Check access permissions.
2859 sys_faccessat(struct faccessat_args *uap)
2861 struct nlookupdata nd;
2865 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2868 error = kern_access(&nd, uap->amode, uap->flags);
2869 nlookup_done_at(&nd, fp);
2874 kern_stat(struct nlookupdata *nd, struct stat *st)
2879 nd->nl_flags |= NLC_SHAREDLOCK;
2880 if ((error = nlookup(nd)) != 0)
2883 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2886 if ((error = vget(vp, LK_SHARED)) != 0)
2888 error = vn_stat(vp, st, nd->nl_cred);
2891 * If the file handle is stale we have to re-resolve the
2892 * entry with the ncp held exclusively. This is a hack
2895 if (error == ESTALE) {
2897 cache_unlock(&nd->nl_nch);
2898 cache_lock(&nd->nl_nch);
2899 cache_setunresolved(&nd->nl_nch);
2900 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2910 * stat_args(char *path, struct stat *ub)
2912 * Get file status; this version follows links.
2915 sys_stat(struct stat_args *uap)
2917 struct nlookupdata nd;
2921 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2923 error = kern_stat(&nd, &st);
2925 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2932 * lstat_args(char *path, struct stat *ub)
2934 * Get file status; this version does not follow links.
2937 sys_lstat(struct lstat_args *uap)
2939 struct nlookupdata nd;
2943 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2945 error = kern_stat(&nd, &st);
2947 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2954 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2956 * Get status of file pointed to by fd/path.
2959 sys_fstatat(struct fstatat_args *uap)
2961 struct nlookupdata nd;
2967 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2970 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2972 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2973 UIO_USERSPACE, flags);
2975 error = kern_stat(&nd, &st);
2977 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2979 nlookup_done_at(&nd, fp);
2984 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
2986 struct nlookupdata nd;
2991 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
2993 error = nlookup(&nd);
2995 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2998 error = VOP_PATHCONF(vp, name, sysmsg_regp);
3005 * pathconf_Args(char *path, int name)
3007 * Get configurable pathname variables.
3010 sys_pathconf(struct pathconf_args *uap)
3012 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
3017 * lpathconf_Args(char *path, int name)
3019 * Get configurable pathname variables, but don't follow symlinks.
3022 sys_lpathconf(struct lpathconf_args *uap)
3024 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
3029 * kern_readlink isn't properly split yet. There is a copyin burried
3030 * in VOP_READLINK().
3033 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
3035 struct thread *td = curthread;
3041 nd->nl_flags |= NLC_SHAREDLOCK;
3042 if ((error = nlookup(nd)) != 0)
3044 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
3047 if (vp->v_type != VLNK) {
3050 aiov.iov_base = buf;
3051 aiov.iov_len = count;
3052 auio.uio_iov = &aiov;
3053 auio.uio_iovcnt = 1;
3054 auio.uio_offset = 0;
3055 auio.uio_rw = UIO_READ;
3056 auio.uio_segflg = UIO_USERSPACE;
3058 auio.uio_resid = count;
3059 error = VOP_READLINK(vp, &auio, td->td_ucred);
3062 *res = count - auio.uio_resid;
3067 * readlink_args(char *path, char *buf, int count)
3069 * Return target name of a symbolic link.
3072 sys_readlink(struct readlink_args *uap)
3074 struct nlookupdata nd;
3077 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3079 error = kern_readlink(&nd, uap->buf, uap->count,
3080 &uap->sysmsg_result);
3087 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3089 * Return target name of a symbolic link. The path is relative to the
3090 * directory associated with fd.
3093 sys_readlinkat(struct readlinkat_args *uap)
3095 struct nlookupdata nd;
3099 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3101 error = kern_readlink(&nd, uap->buf, uap->bufsize,
3102 &uap->sysmsg_result);
3104 nlookup_done_at(&nd, fp);
3109 setfflags(struct vnode *vp, int flags)
3111 struct thread *td = curthread;
3116 * Prevent non-root users from setting flags on devices. When
3117 * a device is reused, users can retain ownership of the device
3118 * if they are allowed to set flags and programs assume that
3119 * chown can't fail when done as root.
3121 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
3122 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
3126 * note: vget is required for any operation that might mod the vnode
3127 * so VINACTIVE is properly cleared.
3129 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3131 vattr.va_flags = flags;
3132 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3139 * chflags(char *path, int flags)
3141 * Change flags of a file given a path name.
3144 sys_chflags(struct chflags_args *uap)
3146 struct nlookupdata nd;
3151 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3153 error = nlookup(&nd);
3155 error = ncp_writechk(&nd.nl_nch);
3157 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3160 error = setfflags(vp, uap->flags);
3167 * lchflags(char *path, int flags)
3169 * Change flags of a file given a path name, but don't follow symlinks.
3172 sys_lchflags(struct lchflags_args *uap)
3174 struct nlookupdata nd;
3179 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3181 error = nlookup(&nd);
3183 error = ncp_writechk(&nd.nl_nch);
3185 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3188 error = setfflags(vp, uap->flags);
3195 * fchflags_args(int fd, int flags)
3197 * Change flags of a file given a file descriptor.
3200 sys_fchflags(struct fchflags_args *uap)
3202 struct thread *td = curthread;
3206 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3208 if (fp->f_nchandle.ncp)
3209 error = ncp_writechk(&fp->f_nchandle);
3211 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3217 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3218 * change flags given a pathname relative to a filedescriptor
3220 int sys_chflagsat(struct chflagsat_args *uap)
3222 struct nlookupdata nd;
3228 if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3231 lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3234 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags);
3236 error = nlookup(&nd);
3238 error = ncp_writechk(&nd.nl_nch);
3240 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3241 nlookup_done_at(&nd, fp);
3243 error = setfflags(vp, uap->flags);
3251 setfmode(struct vnode *vp, int mode)
3253 struct thread *td = curthread;
3258 * note: vget is required for any operation that might mod the vnode
3259 * so VINACTIVE is properly cleared.
3261 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3263 vattr.va_mode = mode & ALLPERMS;
3264 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3265 cache_inval_wxok(vp);
3272 kern_chmod(struct nlookupdata *nd, int mode)
3277 if ((error = nlookup(nd)) != 0)
3279 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3281 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3282 error = setfmode(vp, mode);
3288 * chmod_args(char *path, int mode)
3290 * Change mode of a file given path name.
3293 sys_chmod(struct chmod_args *uap)
3295 struct nlookupdata nd;
3298 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3300 error = kern_chmod(&nd, uap->mode);
3306 * lchmod_args(char *path, int mode)
3308 * Change mode of a file given path name (don't follow links.)
3311 sys_lchmod(struct lchmod_args *uap)
3313 struct nlookupdata nd;
3316 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3318 error = kern_chmod(&nd, uap->mode);
3324 * fchmod_args(int fd, int mode)
3326 * Change mode of a file given a file descriptor.
3329 sys_fchmod(struct fchmod_args *uap)
3331 struct thread *td = curthread;
3335 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3337 if (fp->f_nchandle.ncp)
3338 error = ncp_writechk(&fp->f_nchandle);
3340 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3346 * fchmodat_args(char *path, int mode)
3348 * Change mode of a file pointed to by fd/path.
3351 sys_fchmodat(struct fchmodat_args *uap)
3353 struct nlookupdata nd;
3358 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3360 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3362 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3363 UIO_USERSPACE, flags);
3365 error = kern_chmod(&nd, uap->mode);
3366 nlookup_done_at(&nd, fp);
3371 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3373 struct thread *td = curthread;
3381 * note: vget is required for any operation that might mod the vnode
3382 * so VINACTIVE is properly cleared.
3384 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3385 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3387 o_uid = vattr.va_uid;
3388 o_gid = vattr.va_gid;
3389 size = vattr.va_size;
3394 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3403 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3404 VFS_ACCOUNT(mp, uid, gid, size);
3411 kern_chown(struct nlookupdata *nd, int uid, int gid)
3416 if ((error = nlookup(nd)) != 0)
3418 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3420 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3421 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3427 * chown(char *path, int uid, int gid)
3429 * Set ownership given a path name.
3432 sys_chown(struct chown_args *uap)
3434 struct nlookupdata nd;
3437 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3439 error = kern_chown(&nd, uap->uid, uap->gid);
3445 * lchown_args(char *path, int uid, int gid)
3447 * Set ownership given a path name, do not cross symlinks.
3450 sys_lchown(struct lchown_args *uap)
3452 struct nlookupdata nd;
3455 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3457 error = kern_chown(&nd, uap->uid, uap->gid);
3463 * fchown_args(int fd, int uid, int gid)
3465 * Set ownership given a file descriptor.
3468 sys_fchown(struct fchown_args *uap)
3470 struct thread *td = curthread;
3471 struct proc *p = td->td_proc;
3475 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3477 if (fp->f_nchandle.ncp)
3478 error = ncp_writechk(&fp->f_nchandle);
3480 error = setfown(p->p_fd->fd_ncdir.mount,
3481 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3487 * fchownat(int fd, char *path, int uid, int gid, int flags)
3489 * Set ownership of file pointed to by fd/path.
3492 sys_fchownat(struct fchownat_args *uap)
3494 struct nlookupdata nd;
3499 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3501 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3503 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3504 UIO_USERSPACE, flags);
3506 error = kern_chown(&nd, uap->uid, uap->gid);
3507 nlookup_done_at(&nd, fp);
3513 getutimes(struct timeval *tvp, struct timespec *tsp)
3515 struct timeval tv[2];
3520 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3523 if ((error = itimerfix(tvp)) != 0)
3525 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3526 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3532 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3534 struct timespec tsnow;
3548 if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3550 if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3553 if (newts[0].tv_nsec == UTIME_OMIT)
3554 newts[0].tv_sec = VNOVAL;
3555 else if (newts[0].tv_nsec == UTIME_NOW)
3557 else if ((error = itimespecfix(&newts[0])) != 0)
3560 if (newts[1].tv_nsec == UTIME_OMIT)
3561 newts[1].tv_sec = VNOVAL;
3562 else if (newts[1].tv_nsec == UTIME_NOW)
3564 else if ((error = itimespecfix(&newts[1])) != 0)
3571 setutimes(struct vnode *vp, struct vattr *vattr,
3572 const struct timespec *ts, int nullflag)
3574 struct thread *td = curthread;
3578 vattr->va_atime = ts[0];
3579 vattr->va_mtime = ts[1];
3581 vattr->va_vaflags |= VA_UTIMES_NULL;
3582 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3588 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3590 struct timespec ts[2];
3594 if ((error = getutimes(tptr, ts)) != 0)
3597 error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3602 * utimes_args(char *path, struct timeval *tptr)
3604 * Set the access and modification times of a file.
3607 sys_utimes(struct utimes_args *uap)
3609 struct timeval tv[2];
3610 struct nlookupdata nd;
3614 error = copyin(uap->tptr, tv, sizeof(tv));
3618 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3620 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3626 * lutimes_args(char *path, struct timeval *tptr)
3628 * Set the access and modification times of a file.
3631 sys_lutimes(struct lutimes_args *uap)
3633 struct timeval tv[2];
3634 struct nlookupdata nd;
3638 error = copyin(uap->tptr, tv, sizeof(tv));
3642 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3644 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3650 * Set utimes on a file descriptor. The creds used to open the
3651 * file are used to determine whether the operation is allowed
3655 kern_futimens(int fd, struct timespec *ts)
3657 struct thread *td = curthread;
3658 struct timespec newts[2];
3665 error = getutimens(ts, newts, &nullflag);
3668 if ((error = holdvnode(td, fd, &fp)) != 0)
3670 if (fp->f_nchandle.ncp)
3671 error = ncp_writechk(&fp->f_nchandle);
3674 error = vget(vp, LK_EXCLUSIVE);
3676 error = VOP_GETATTR(vp, &vattr);
3678 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3682 error = setutimes(vp, &vattr, newts, nullflag);
3692 * futimens_args(int fd, struct timespec *ts)
3694 * Set the access and modification times of a file.
3697 sys_futimens(struct futimens_args *uap)
3699 struct timespec ts[2];
3703 error = copyin(uap->ts, ts, sizeof(ts));
3707 error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3712 kern_futimes(int fd, struct timeval *tptr)
3714 struct timespec ts[2];
3718 if ((error = getutimes(tptr, ts)) != 0)
3721 error = kern_futimens(fd, tptr ? ts : NULL);
3726 * futimes_args(int fd, struct timeval *tptr)
3728 * Set the access and modification times of a file.
3731 sys_futimes(struct futimes_args *uap)
3733 struct timeval tv[2];
3737 error = copyin(uap->tptr, tv, sizeof(tv));
3741 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3746 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3748 struct timespec newts[2];
3754 if (flags & ~AT_SYMLINK_NOFOLLOW)
3757 error = getutimens(ts, newts, &nullflag);
3761 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3762 if ((error = nlookup(nd)) != 0)
3764 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3766 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3768 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3769 error = vget(vp, LK_EXCLUSIVE);
3771 error = setutimes(vp, &vattr, newts, nullflag);
3780 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3782 * Set file access and modification times of a file.
3785 sys_utimensat(struct utimensat_args *uap)
3787 struct timespec ts[2];
3788 struct nlookupdata nd;
3794 error = copyin(uap->ts, ts, sizeof(ts));
3799 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3800 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3801 UIO_USERSPACE, flags);
3803 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3804 nlookup_done_at(&nd, fp);
3809 kern_truncate(struct nlookupdata *nd, off_t length)
3816 uint64_t old_size = 0;
3820 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3821 if ((error = nlookup(nd)) != 0)
3823 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3825 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3827 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3832 if (vp->v_type == VDIR) {
3836 if (vfs_quota_enabled) {
3837 error = VOP_GETATTR(vp, &vattr);
3838 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3841 old_size = vattr.va_size;
3844 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3846 vattr.va_size = length;
3847 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3848 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3856 * truncate(char *path, int pad, off_t length)
3858 * Truncate a file given its path name.
3861 sys_truncate(struct truncate_args *uap)
3863 struct nlookupdata nd;
3866 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3868 error = kern_truncate(&nd, uap->length);
3874 kern_ftruncate(int fd, off_t length)
3876 struct thread *td = curthread;
3883 uint64_t old_size = 0;
3888 if ((error = holdvnode(td, fd, &fp)) != 0)
3890 if (fp->f_nchandle.ncp) {
3891 error = ncp_writechk(&fp->f_nchandle);
3895 if ((fp->f_flag & FWRITE) == 0) {
3899 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3903 vp = (struct vnode *)fp->f_data;
3904 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3905 if (vp->v_type == VDIR) {
3911 if (vfs_quota_enabled) {
3912 error = VOP_GETATTR(vp, &vattr);
3913 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3916 old_size = vattr.va_size;
3919 if ((error = vn_writechk(vp, NULL)) == 0) {
3921 vattr.va_size = length;
3922 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3924 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3933 * ftruncate_args(int fd, int pad, off_t length)
3935 * Truncate a file given a file descriptor.
3938 sys_ftruncate(struct ftruncate_args *uap)
3942 error = kern_ftruncate(uap->fd, uap->length);
3950 * Sync an open file.
3953 sys_fsync(struct fsync_args *uap)
3955 struct thread *td = curthread;
3961 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3963 vp = (struct vnode *)fp->f_data;
3964 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3965 if ((obj = vp->v_object) != NULL) {
3966 if (vp->v_mount == NULL ||
3967 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3968 vm_object_page_clean(obj, 0, 0, 0);
3971 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3972 if (error == 0 && vp->v_mount)
3973 error = buf_fsync(vp);
3981 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3983 struct nchandle fnchd;
3984 struct nchandle tnchd;
3985 struct namecache *ncp;
3994 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3995 if ((error = nlookup(fromnd)) != 0)
3997 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3999 fnchd.mount = fromnd->nl_nch.mount;
4003 * unlock the source nch so we can lookup the target nch without
4004 * deadlocking. The target may or may not exist so we do not check
4005 * for a target vp like kern_mkdir() and other creation functions do.
4007 * The source and target directories are ref'd and rechecked after
4008 * everything is relocked to determine if the source or target file
4011 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
4012 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
4014 fncp_gen = fromnd->nl_nch.ncp->nc_generation;
4016 cache_unlock(&fromnd->nl_nch);
4018 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
4019 if ((error = nlookup(tond)) != 0) {
4023 tncp_gen = tond->nl_nch.ncp->nc_generation;
4025 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
4029 tnchd.mount = tond->nl_nch.mount;
4033 * If the source and target are the same there is nothing to do
4035 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
4042 * Mount points cannot be renamed or overwritten
4044 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
4053 * Relock the source ncp. cache_relock() will deal with any
4054 * deadlocks against the already-locked tond and will also
4055 * make sure both are resolved.
4057 * NOTE AFTER RELOCKING: The source or target ncp may have become
4058 * invalid while they were unlocked, nc_vp and nc_mount could
4061 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
4062 &tond->nl_nch, tond->nl_cred);
4063 fromnd->nl_flags |= NLC_NCPISLOCKED;
4066 * If the namecache generation changed for either fromnd or tond,
4069 if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
4070 tond->nl_nch.ncp->nc_generation != tncp_gen) {
4071 kprintf("kern_rename: retry due to gen on: "
4072 "\"%s\" -> \"%s\"\n",
4073 fromnd->nl_nch.ncp->nc_name,
4074 tond->nl_nch.ncp->nc_name);
4081 * If either fromnd or tond are marked destroyed a ripout occured
4082 * out from under us and we must retry.
4084 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
4085 fromnd->nl_nch.ncp->nc_vp == NULL ||
4086 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
4087 kprintf("kern_rename: retry due to ripout on: "
4088 "\"%s\" -> \"%s\"\n",
4089 fromnd->nl_nch.ncp->nc_name,
4090 tond->nl_nch.ncp->nc_name);
4097 * Make sure the parent directories linkages are the same.
4098 * XXX shouldn't be needed any more w/ generation check above.
4100 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
4101 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
4108 * Both the source and target must be within the same filesystem and
4109 * in the same filesystem as their parent directories within the
4110 * namecache topology.
4112 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4115 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
4116 mp != tond->nl_nch.mount) {
4123 * Make sure the mount point is writable
4125 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4132 * If the target exists and either the source or target is a directory,
4133 * then both must be directories.
4135 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4138 if (tond->nl_nch.ncp->nc_vp) {
4139 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4141 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4142 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4144 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4150 * You cannot rename a source into itself or a subdirectory of itself.
4151 * We check this by travsersing the target directory upwards looking
4152 * for a match against the source.
4157 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4158 if (fromnd->nl_nch.ncp == ncp) {
4169 * Even though the namespaces are different, they may still represent
4170 * hardlinks to the same file. The filesystem might have a hard time
4171 * with this so we issue a NREMOVE of the source instead of a NRENAME
4172 * when we detect the situation.
4175 fdvp = fromnd->nl_dvp;
4176 tdvp = tond->nl_dvp;
4177 if (fdvp == NULL || tdvp == NULL) {
4179 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4180 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4183 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4184 fdvp, tdvp, tond->nl_cred);
4191 * rename_args(char *from, char *to)
4193 * Rename files. Source and destination must either both be directories,
4194 * or both not be directories. If target is a directory, it must be empty.
4197 sys_rename(struct rename_args *uap)
4199 struct nlookupdata fromnd, tond;
4203 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4205 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4207 error = kern_rename(&fromnd, &tond);
4208 nlookup_done(&tond);
4210 nlookup_done(&fromnd);
4211 } while (error == EAGAIN);
4216 * renameat_args(int oldfd, char *old, int newfd, char *new)
4218 * Rename files using paths relative to the directories associated with
4219 * oldfd and newfd. Source and destination must either both be directories,
4220 * or both not be directories. If target is a directory, it must be empty.
4223 sys_renameat(struct renameat_args *uap)
4225 struct nlookupdata oldnd, newnd;
4226 struct file *oldfp, *newfp;
4230 error = nlookup_init_at(&oldnd, &oldfp,
4231 uap->oldfd, uap->old,
4234 error = nlookup_init_at(&newnd, &newfp,
4235 uap->newfd, uap->new,
4238 error = kern_rename(&oldnd, &newnd);
4239 nlookup_done_at(&newnd, newfp);
4241 nlookup_done_at(&oldnd, oldfp);
4242 } while (error == EAGAIN);
4247 kern_mkdir(struct nlookupdata *nd, int mode)
4249 struct thread *td = curthread;
4250 struct proc *p = td->td_proc;
4256 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4257 if ((error = nlookup(nd)) != 0)
4260 if (nd->nl_nch.ncp->nc_vp)
4262 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4265 vattr.va_type = VDIR;
4266 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4269 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4276 * mkdir_args(char *path, int mode)
4278 * Make a directory file.
4281 sys_mkdir(struct mkdir_args *uap)
4283 struct nlookupdata nd;
4286 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4288 error = kern_mkdir(&nd, uap->mode);
4294 * mkdirat_args(int fd, char *path, mode_t mode)
4296 * Make a directory file. The path is relative to the directory associated
4300 sys_mkdirat(struct mkdirat_args *uap)
4302 struct nlookupdata nd;
4306 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4308 error = kern_mkdir(&nd, uap->mode);
4309 nlookup_done_at(&nd, fp);
4314 kern_rmdir(struct nlookupdata *nd)
4319 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4320 if ((error = nlookup(nd)) != 0)
4324 * Do not allow directories representing mount points to be
4325 * deleted, even if empty. Check write perms on mount point
4326 * in case the vnode is aliased (aka nullfs).
4328 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4330 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4332 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4337 * rmdir_args(char *path)
4339 * Remove a directory file.
4342 sys_rmdir(struct rmdir_args *uap)
4344 struct nlookupdata nd;
4347 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4349 error = kern_rmdir(&nd);
4355 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4356 enum uio_seg direction)
4358 struct thread *td = curthread;
4366 if ((error = holdvnode(td, fd, &fp)) != 0)
4368 if ((fp->f_flag & FREAD) == 0) {
4372 vp = (struct vnode *)fp->f_data;
4373 if (vp->v_type != VDIR) {
4377 aiov.iov_base = buf;
4378 aiov.iov_len = count;
4379 auio.uio_iov = &aiov;
4380 auio.uio_iovcnt = 1;
4381 auio.uio_rw = UIO_READ;
4382 auio.uio_segflg = direction;
4384 auio.uio_resid = count;
4385 loff = auio.uio_offset = fp->f_offset;
4386 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4387 fp->f_offset = auio.uio_offset;
4392 * WARNING! *basep may not be wide enough to accomodate the
4393 * seek offset. XXX should we hack this to return the upper 32 bits
4394 * for offsets greater then 4G?
4397 *basep = (long)loff;
4399 *res = count - auio.uio_resid;
4406 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4408 * Read a block of directory entries in a file system independent format.
4411 sys_getdirentries(struct getdirentries_args *uap)
4416 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4417 &uap->sysmsg_result, UIO_USERSPACE);
4419 if (error == 0 && uap->basep)
4420 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4425 * getdents_args(int fd, char *buf, size_t count)
4428 sys_getdents(struct getdents_args *uap)
4432 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4433 &uap->sysmsg_result, UIO_USERSPACE);
4439 * Set the mode mask for creation of filesystem nodes.
4441 * umask(int newmask)
4444 sys_umask(struct umask_args *uap)
4446 struct thread *td = curthread;
4447 struct proc *p = td->td_proc;
4448 struct filedesc *fdp;
4451 uap->sysmsg_result = fdp->fd_cmask;
4452 fdp->fd_cmask = uap->newmask & ALLPERMS;
4457 * revoke(char *path)
4459 * Void all references to file by ripping underlying filesystem
4463 sys_revoke(struct revoke_args *uap)
4465 struct nlookupdata nd;
4472 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4474 error = nlookup(&nd);
4476 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4477 cred = crhold(nd.nl_cred);
4481 error = VOP_GETATTR(vp, &vattr);
4482 if (error == 0 && cred->cr_uid != vattr.va_uid)
4483 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4484 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4486 error = vrevoke(vp, cred);
4487 } else if (error == 0) {
4488 error = vrevoke(vp, cred);
4498 * getfh_args(char *fname, fhandle_t *fhp)
4500 * Get (NFS) file handle
4502 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4503 * mount. This allows nullfs mounts to be explicitly exported.
4505 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4507 * nullfs mounts of subdirectories are not safe. That is, it will
4508 * work, but you do not really have protection against access to
4509 * the related parent directories.
4512 sys_getfh(struct getfh_args *uap)
4514 struct thread *td = curthread;
4515 struct nlookupdata nd;
4522 * Must be super user
4524 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4528 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4530 error = nlookup(&nd);
4532 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4533 mp = nd.nl_nch.mount;
4536 bzero(&fh, sizeof(fh));
4537 fh.fh_fsid = mp->mnt_stat.f_fsid;
4538 error = VFS_VPTOFH(vp, &fh.fh_fid);
4541 error = copyout(&fh, uap->fhp, sizeof(fh));
4547 * fhopen_args(const struct fhandle *u_fhp, int flags)
4549 * syscall for the rpc.lockd to use to translate a NFS file handle into
4550 * an open descriptor.
4552 * warning: do not remove the priv_check() call or this becomes one giant
4556 sys_fhopen(struct fhopen_args *uap)
4558 struct thread *td = curthread;
4559 struct filedesc *fdp = td->td_proc->p_fd;
4564 struct vattr *vap = &vat;
4566 int fmode, mode, error = 0, type;
4572 * Must be super user
4574 error = priv_check(td, PRIV_ROOT);
4578 fmode = FFLAGS(uap->flags);
4581 * Why not allow a non-read/write open for our lockd?
4583 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4585 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4590 * Find the mount point
4592 mp = vfs_getvfs(&fhp.fh_fsid);
4597 /* now give me my vnode, it gets returned to me locked */
4598 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4602 * from now on we have to make sure not
4603 * to forget about the vnode
4604 * any error that causes an abort must vput(vp)
4605 * just set error = err and 'goto bad;'.
4611 if (vp->v_type == VLNK) {
4615 if (vp->v_type == VSOCK) {
4620 if (fmode & (FWRITE | O_TRUNC)) {
4621 if (vp->v_type == VDIR) {
4625 error = vn_writechk(vp, NULL);
4633 error = VOP_ACCESS(vp, mode, td->td_ucred);
4637 if (fmode & O_TRUNC) {
4638 vn_unlock(vp); /* XXX */
4639 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4642 error = VOP_SETATTR(vp, vap, td->td_ucred);
4648 * VOP_OPEN needs the file pointer so it can potentially override
4651 * WARNING! no f_nchandle will be associated when fhopen()ing a
4654 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4658 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4661 * setting f_ops this way prevents VOP_CLOSE from being
4662 * called or fdrop() releasing the vp from v_data. Since
4663 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4665 fp->f_ops = &badfileops;
4671 * The fp is given its own reference, we still have our ref and lock.
4673 * Assert that all regular files must be created with a VM object.
4675 if (vp->v_type == VREG && vp->v_object == NULL) {
4676 kprintf("fhopen: regular file did not "
4677 "have VM object: %p\n",
4683 * The open was successful. Handle any locking requirements.
4685 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4686 lf.l_whence = SEEK_SET;
4689 if (fmode & O_EXLOCK)
4690 lf.l_type = F_WRLCK;
4692 lf.l_type = F_RDLCK;
4693 if (fmode & FNONBLOCK)
4698 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK,
4701 * release our private reference.
4703 fsetfd(fdp, NULL, indx);
4708 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4709 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
4713 * Clean up. Associate the file pointer with the previously
4714 * reserved descriptor and return it.
4717 if (uap->flags & O_CLOEXEC)
4718 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4719 fsetfd(fdp, fp, indx);
4721 uap->sysmsg_result = indx;
4727 fsetfd(fdp, NULL, indx);
4738 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4741 sys_fhstat(struct fhstat_args *uap)
4743 struct thread *td = curthread;
4751 * Must be super user
4753 error = priv_check(td, PRIV_ROOT);
4757 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4761 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4764 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4765 error = vn_stat(vp, &sb, td->td_ucred);
4770 error = copyout(&sb, uap->sb, sizeof(sb));
4778 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4781 sys_fhstatfs(struct fhstatfs_args *uap)
4783 struct thread *td = curthread;
4784 struct proc *p = td->td_proc;
4789 char *fullpath, *freepath;
4794 * Must be super user
4796 if ((error = priv_check(td, PRIV_ROOT)))
4799 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4802 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4806 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4811 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4816 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4819 error = mount_path(p, mp, &fullpath, &freepath);
4822 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4823 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4824 kfree(freepath, M_TEMP);
4826 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4827 if (priv_check(td, PRIV_ROOT)) {
4828 bcopy(sp, &sb, sizeof(sb));
4829 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4832 error = copyout(sp, uap->buf, sizeof(*sp));
4841 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4844 sys_fhstatvfs(struct fhstatvfs_args *uap)
4846 struct thread *td = curthread;
4847 struct proc *p = td->td_proc;
4855 * Must be super user
4857 if ((error = priv_check(td, PRIV_ROOT)))
4860 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4863 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4867 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4872 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4875 sp = &mp->mnt_vstat;
4877 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4881 if (mp->mnt_flag & MNT_RDONLY)
4882 sp->f_flag |= ST_RDONLY;
4883 if (mp->mnt_flag & MNT_NOSUID)
4884 sp->f_flag |= ST_NOSUID;
4885 error = copyout(sp, uap->buf, sizeof(*sp));
4894 * Syscall to push extended attribute configuration information into the
4895 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4896 * a command (int cmd), and attribute name and misc data. For now, the
4897 * attribute name is left in userspace for consumption by the VFS_op.
4898 * It will probably be changed to be copied into sysspace by the
4899 * syscall in the future, once issues with various consumers of the
4900 * attribute code have raised their hands.
4902 * Currently this is used only by UFS Extended Attributes.
4905 sys_extattrctl(struct extattrctl_args *uap)
4907 struct nlookupdata nd;
4909 char attrname[EXTATTR_MAXNAMELEN];
4917 if (error == 0 && uap->filename) {
4918 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4921 error = nlookup(&nd);
4923 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4927 if (error == 0 && uap->attrname) {
4928 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4933 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4935 error = nlookup(&nd);
4937 error = ncp_writechk(&nd.nl_nch);
4939 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4941 uap->attrname, nd.nl_cred);
4950 * Syscall to get a named extended attribute on a file or directory.
4953 sys_extattr_set_file(struct extattr_set_file_args *uap)
4955 char attrname[EXTATTR_MAXNAMELEN];
4956 struct nlookupdata nd;
4962 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4968 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4970 error = nlookup(&nd);
4972 error = ncp_writechk(&nd.nl_nch);
4974 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4980 bzero(&auio, sizeof(auio));
4981 aiov.iov_base = uap->data;
4982 aiov.iov_len = uap->nbytes;
4983 auio.uio_iov = &aiov;
4984 auio.uio_iovcnt = 1;
4985 auio.uio_offset = 0;
4986 auio.uio_resid = uap->nbytes;
4987 auio.uio_rw = UIO_WRITE;
4988 auio.uio_td = curthread;
4990 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4999 * Syscall to get a named extended attribute on a file or directory.
5002 sys_extattr_get_file(struct extattr_get_file_args *uap)
5004 char attrname[EXTATTR_MAXNAMELEN];
5005 struct nlookupdata nd;
5011 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5017 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5019 error = nlookup(&nd);
5021 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
5027 bzero(&auio, sizeof(auio));
5028 aiov.iov_base = uap->data;
5029 aiov.iov_len = uap->nbytes;
5030 auio.uio_iov = &aiov;
5031 auio.uio_iovcnt = 1;
5032 auio.uio_offset = 0;
5033 auio.uio_resid = uap->nbytes;
5034 auio.uio_rw = UIO_READ;
5035 auio.uio_td = curthread;
5037 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
5039 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
5047 * Syscall to delete a named extended attribute from a file or directory.
5048 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
5051 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
5053 char attrname[EXTATTR_MAXNAMELEN];
5054 struct nlookupdata nd;
5058 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5062 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5064 error = nlookup(&nd);
5066 error = ncp_writechk(&nd.nl_nch);
5068 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5070 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
5071 attrname, NULL, nd.nl_cred);
5080 * Determine if the mount is visible to the process.
5083 chroot_visible_mnt(struct mount *mp, struct proc *p)
5085 struct nchandle nch;
5088 * Traverse from the mount point upwards. If we hit the process
5089 * root then the mount point is visible to the process.
5091 nch = mp->mnt_ncmountpt;
5093 if (nch.mount == p->p_fd->fd_nrdir.mount &&
5094 nch.ncp == p->p_fd->fd_nrdir.ncp) {
5097 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
5098 nch = nch.mount->mnt_ncmounton;
5100 nch.ncp = nch.ncp->nc_parent;
5105 * If the mount point is not visible to the process, but the
5106 * process root is in a subdirectory of the mount, return
5109 if (p->p_fd->fd_nrdir.mount == mp)