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 *, u_long);
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 if (mp->mnt_flag & MNT_UPDATE) {
384 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
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 mp->mnt_ncmounton = nch;
400 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
401 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
404 * Put the new filesystem on the mount list after root. The mount
405 * point gets its own mnt_ncmountpt (unless the VFS already set one
406 * up) which represents the root of the mount. The lookup code
407 * detects the mount point going forward and checks the root of
408 * the mount going backwards.
410 * It is not necessary to invalidate or purge the vnode underneath
411 * because elements under the mount will be given their own glue
415 if (mp->mnt_ncmountpt.ncp == NULL) {
417 * Allocate, then unlock, but leave the ref intact.
418 * This is the mnt_refs (1) that we will retain
419 * through to the unmount.
421 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
422 cache_unlock(&mp->mnt_ncmountpt);
426 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
428 cache_ismounting(mp);
429 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
431 mountlist_insert(mp, MNTINS_LAST);
433 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
434 error = vfs_allocate_syncvnode(mp);
435 lwkt_reltoken(&mp->mnt_token);
437 error = VFS_START(mp, 0);
439 KNOTE(&fs_klist, VQ_MOUNT);
441 bzero(&mp->mnt_ncmounton, sizeof(mp->mnt_ncmounton));
442 vn_syncer_thr_stop(mp);
443 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
444 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
445 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
446 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
447 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
448 mp->mnt_vfc->vfc_refcount--;
449 lwkt_reltoken(&mp->mnt_token);
460 * Scan all active processes to see if any of them have a current
461 * or root directory onto which the new filesystem has just been
462 * mounted. If so, replace them with the new mount point.
464 * Both old_nch and new_nch are ref'd on call but not locked.
465 * new_nch must be temporarily locked so it can be associated with the
466 * vnode representing the root of the mount point.
468 struct checkdirs_info {
469 struct nchandle old_nch;
470 struct nchandle new_nch;
471 struct vnode *old_vp;
472 struct vnode *new_vp;
475 static int checkdirs_callback(struct proc *p, void *data);
478 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
480 struct checkdirs_info info;
486 * If the old mount point's vnode has a usecount of 1, it is not
487 * being held as a descriptor anywhere.
489 olddp = old_nch->ncp->nc_vp;
490 if (olddp == NULL || VREFCNT(olddp) == 1)
494 * Force the root vnode of the new mount point to be resolved
495 * so we can update any matching processes.
498 if (VFS_ROOT(mp, &newdp))
499 panic("mount: lost mount");
502 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
503 cache_setunresolved(new_nch);
504 cache_setvp(new_nch, newdp);
505 cache_unlock(new_nch);
508 * Special handling of the root node
510 if (rootvnode == olddp) {
512 vfs_cache_setroot(newdp, cache_hold(new_nch));
516 * Pass newdp separately so the callback does not have to access
517 * it via new_nch->ncp->nc_vp.
519 info.old_nch = *old_nch;
520 info.new_nch = *new_nch;
522 allproc_scan(checkdirs_callback, &info, 0);
527 * NOTE: callback is not MP safe because the scanned process's filedesc
528 * structure can be ripped out from under us, amoung other things.
531 checkdirs_callback(struct proc *p, void *data)
533 struct checkdirs_info *info = data;
534 struct filedesc *fdp;
535 struct nchandle ncdrop1;
536 struct nchandle ncdrop2;
537 struct vnode *vprele1;
538 struct vnode *vprele2;
540 if ((fdp = p->p_fd) != NULL) {
541 cache_zero(&ncdrop1);
542 cache_zero(&ncdrop2);
547 * MPUNSAFE - XXX fdp can be pulled out from under a
550 * A shared filedesc is ok, we don't have to copy it
551 * because we are making this change globally.
553 spin_lock(&fdp->fd_spin);
554 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
555 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
556 vprele1 = fdp->fd_cdir;
558 fdp->fd_cdir = info->new_vp;
559 ncdrop1 = fdp->fd_ncdir;
560 cache_copy(&info->new_nch, &fdp->fd_ncdir);
562 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
563 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
564 vprele2 = fdp->fd_rdir;
566 fdp->fd_rdir = info->new_vp;
567 ncdrop2 = fdp->fd_nrdir;
568 cache_copy(&info->new_nch, &fdp->fd_nrdir);
570 spin_unlock(&fdp->fd_spin);
572 cache_drop(&ncdrop1);
574 cache_drop(&ncdrop2);
584 * Unmount a file system.
586 * Note: unmount takes a path to the vnode mounted on as argument,
587 * not special file (as before).
589 * umount_args(char *path, int flags)
594 sys_unmount(struct unmount_args *uap)
596 struct thread *td = curthread;
597 struct proc *p __debugvar = td->td_proc;
598 struct mount *mp = NULL;
599 struct nlookupdata nd;
603 if (td->td_ucred->cr_prison != NULL) {
607 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
610 error = nlookup_init(&nd, uap->path, UIO_USERSPACE,
611 NLC_FOLLOW | NLC_IGNBADDIR);
613 error = nlookup(&nd);
617 mp = nd.nl_nch.mount;
620 * Only root, or the user that did the original mount is
621 * permitted to unmount this filesystem.
623 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
624 (error = priv_check(td, PRIV_ROOT)))
628 * Don't allow unmounting the root file system.
630 if (mp->mnt_flag & MNT_ROOTFS) {
636 * Must be the root of the filesystem
638 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
644 * If no error try to issue the unmount. We lose our cache
645 * ref when we call nlookup_done so we must hold the mount point
646 * to prevent use-after-free races.
652 error = dounmount(mp, uap->flags, 0);
662 * Do the actual file system unmount (interlocked against the mountlist
663 * token and mp->mnt_token).
666 dounmount_interlock(struct mount *mp)
668 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
670 mp->mnt_kern_flag |= MNTK_UNMOUNT;
675 unmount_allproc_cb(struct proc *p, void *arg)
679 if (p->p_textnch.ncp == NULL)
682 mp = (struct mount *)arg;
683 if (p->p_textnch.mount == mp)
684 cache_drop(&p->p_textnch);
690 * The guts of the unmount code. The mount owns one ref and one hold
691 * count. If we successfully interlock the unmount, those refs are ours.
692 * (The ref is from mnt_ncmountpt).
694 * When halting we shortcut certain mount types such as devfs by not actually
695 * issuing the VFS_SYNC() or VFS_UNMOUNT(). They are still disconnected
696 * from the mountlist so higher-level filesytems can unmount cleanly.
698 * The mount types that allow QUICKHALT are: devfs, tmpfs, procfs.
701 dounmount(struct mount *mp, int flags, int halting)
703 struct namecache *ncp;
714 lwkt_gettoken(&mp->mnt_token);
717 * When halting, certain mount points can essentially just
718 * be unhooked and otherwise ignored.
720 if (halting && (mp->mnt_kern_flag & MNTK_QUICKHALT)) {
729 * Exclusive access for unmounting purposes.
731 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
735 * We now 'own' the last mp->mnt_refs
737 * Allow filesystems to detect that a forced unmount is in progress.
739 if (flags & MNT_FORCE)
740 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
741 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
742 error = lockmgr(&mp->mnt_lock, lflags);
744 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
745 if (mp->mnt_kern_flag & MNTK_MWAIT) {
746 mp->mnt_kern_flag &= ~MNTK_MWAIT;
752 if (mp->mnt_flag & MNT_EXPUBLIC)
753 vfs_setpublicfs(NULL, NULL, NULL);
755 vfs_msync(mp, MNT_WAIT);
756 async_flag = mp->mnt_flag & MNT_ASYNC;
757 mp->mnt_flag &=~ MNT_ASYNC;
760 * Decomission our special mnt_syncer vnode. This also stops
761 * the vnlru code. If we are unable to unmount we recommission
764 * Then sync the filesystem.
766 if ((vp = mp->mnt_syncer) != NULL) {
767 mp->mnt_syncer = NULL;
768 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
774 * Sync normally-mounted filesystem.
776 if (quickhalt == 0) {
777 if ((mp->mnt_flag & MNT_RDONLY) == 0)
778 VFS_SYNC(mp, MNT_WAIT);
782 * nchandle records ref the mount structure. Expect a count of 1
783 * (our mount->mnt_ncmountpt).
785 * Scans can get temporary refs on a mountpoint (thought really
786 * heavy duty stuff like cache_findmount() do not).
788 for (retry = 0; (retry < 10 || debug_unmount); ++retry) {
790 * Invalidate the namecache topology under the mount.
791 * nullfs mounts alias a real mount's namecache topology
792 * and it should not be invalidated in that case.
794 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
795 cache_lock(&mp->mnt_ncmountpt);
796 cache_inval(&mp->mnt_ncmountpt,
797 CINV_DESTROY|CINV_CHILDREN);
798 cache_unlock(&mp->mnt_ncmountpt);
804 cache_unmounting(mp);
805 if (mp->mnt_refs != 1)
806 cache_clearmntcache();
809 * Break out if we are good. Don't count ncp refs if the
812 ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ?
813 NULL : mp->mnt_ncmountpt.ncp;
814 if (mp->mnt_refs == 1 &&
815 (ncp == NULL || (ncp->nc_refs == 1 &&
816 TAILQ_FIRST(&ncp->nc_list) == NULL))) {
821 * If forcing the unmount, clean out any p->p_textnch
822 * nchandles that match this mount.
824 if (flags & MNT_FORCE)
825 allproc_scan(&unmount_allproc_cb, mp, 0);
830 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
831 if ((retry & 15) == 15) {
833 "(%p) debug - retry %d, "
834 "%d namecache refs, %d mount refs",
836 (ncp ? ncp->nc_refs - 1 : 0),
842 ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ?
843 NULL : mp->mnt_ncmountpt.ncp;
844 if (mp->mnt_refs != 1 ||
845 (ncp != NULL && (ncp->nc_refs != 1 ||
846 TAILQ_FIRST(&ncp->nc_list)))) {
848 "(%p): %d namecache refs, %d mount refs "
851 (ncp ? ncp->nc_refs - 1 : 0),
853 if (flags & MNT_FORCE) {
855 mount_warning(mp, "forcing unmount\n");
862 * So far so good, sync the filesystem once more and
863 * call the VFS unmount code if the sync succeeds.
865 if (error == 0 && quickhalt == 0) {
866 if (mp->mnt_flag & MNT_RDONLY) {
867 error = VFS_UNMOUNT(mp, flags);
869 error = VFS_SYNC(mp, MNT_WAIT);
870 if (error == 0 || /* no error */
871 error == EOPNOTSUPP || /* no sync avail */
872 (flags & MNT_FORCE)) { /* force anyway */
873 error = VFS_UNMOUNT(mp, flags);
878 "(%p) unmount: vfs refused to unmount, "
885 * If an error occurred we can still recover, restoring the
886 * syncer vnode and misc flags.
889 if (mp->mnt_syncer == NULL && hadsyncer)
890 vfs_allocate_syncvnode(mp);
891 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
892 mp->mnt_flag |= async_flag;
893 lockmgr(&mp->mnt_lock, LK_RELEASE);
894 if (mp->mnt_kern_flag & MNTK_MWAIT) {
895 mp->mnt_kern_flag &= ~MNTK_MWAIT;
901 * Clean up any journals still associated with the mount after
902 * filesystem activity has ceased.
904 journal_remove_all_journals(mp,
905 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
907 mountlist_remove(mp);
910 * Remove any installed vnode ops here so the individual VFSs don't
913 * mnt_refs should go to zero when we scrap mnt_ncmountpt.
915 * When quickhalting we have to keep these intact because the
916 * underlying vnodes have not been destroyed, and some might be
919 if (quickhalt == 0) {
920 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
921 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
922 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
923 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
924 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
927 if (mp->mnt_ncmountpt.ncp != NULL) {
928 nch = mp->mnt_ncmountpt;
929 cache_zero(&mp->mnt_ncmountpt);
930 cache_clrmountpt(&nch);
933 if (mp->mnt_ncmounton.ncp != NULL) {
934 cache_unmounting(mp);
935 nch = mp->mnt_ncmounton;
936 cache_zero(&mp->mnt_ncmounton);
937 cache_clrmountpt(&nch);
941 mp->mnt_vfc->vfc_refcount--;
944 * If not quickhalting the mount, we expect there to be no
947 if (quickhalt == 0 && !TAILQ_EMPTY(&mp->mnt_nvnodelist))
948 panic("unmount: dangling vnode");
953 lockmgr(&mp->mnt_lock, LK_RELEASE);
954 if (mp->mnt_kern_flag & MNTK_MWAIT) {
955 mp->mnt_kern_flag &= ~MNTK_MWAIT;
960 * If we reach here and freeok != 0 we must free the mount.
961 * mnt_refs should already have dropped to 0, so if it is not
962 * zero we must cycle the caches and wait.
964 * When we are satisfied that the mount has disconnected we can
965 * drop the hold on the mp that represented the mount (though the
966 * caller might actually have another, so the caller's drop may
967 * do the actual free).
970 if (mp->mnt_refs > 0)
971 cache_clearmntcache();
972 while (mp->mnt_refs > 0) {
973 cache_unmounting(mp);
975 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
976 cache_clearmntcache();
978 lwkt_reltoken(&mp->mnt_token);
982 cache_clearmntcache();
985 KNOTE(&fs_klist, VQ_UNMOUNT);
988 lwkt_reltoken(&mp->mnt_token);
994 mount_warning(struct mount *mp, const char *ctl, ...)
1000 __va_start(va, ctl);
1001 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
1002 &ptr, &buf, 0) == 0) {
1003 kprintf("unmount(%s): ", ptr);
1008 kprintf("unmount(%p", mp);
1009 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
1010 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
1019 * Shim cache_fullpath() to handle the case where a process is chrooted into
1020 * a subdirectory of a mount. In this case if the root mount matches the
1021 * process root directory's mount we have to specify the process's root
1022 * directory instead of the mount point, because the mount point might
1023 * be above the root directory.
1027 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
1029 struct nchandle *nch;
1031 if (p && p->p_fd->fd_nrdir.mount == mp)
1032 nch = &p->p_fd->fd_nrdir;
1034 nch = &mp->mnt_ncmountpt;
1035 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
1039 * Sync each mounted filesystem.
1043 static int syncprt = 0;
1044 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
1047 static int sync_callback(struct mount *mp, void *data);
1050 sys_sync(struct sync_args *uap)
1052 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
1058 sync_callback(struct mount *mp, void *data __unused)
1062 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1063 lwkt_gettoken(&mp->mnt_token);
1064 asyncflag = mp->mnt_flag & MNT_ASYNC;
1065 mp->mnt_flag &= ~MNT_ASYNC;
1066 lwkt_reltoken(&mp->mnt_token);
1067 vfs_msync(mp, MNT_NOWAIT);
1068 VFS_SYNC(mp, MNT_NOWAIT);
1069 lwkt_gettoken(&mp->mnt_token);
1070 mp->mnt_flag |= asyncflag;
1071 lwkt_reltoken(&mp->mnt_token);
1076 /* XXX PRISON: could be per prison flag */
1077 static int prison_quotas;
1079 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
1083 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
1085 * Change filesystem quotas.
1090 sys_quotactl(struct quotactl_args *uap)
1092 struct nlookupdata nd;
1098 if (td->td_ucred->cr_prison && !prison_quotas) {
1103 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1105 error = nlookup(&nd);
1107 mp = nd.nl_nch.mount;
1108 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
1109 uap->arg, nd.nl_cred);
1117 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1118 * void *buf, int buflen)
1120 * This function operates on a mount point and executes the specified
1121 * operation using the specified control data, and possibly returns data.
1123 * The actual number of bytes stored in the result buffer is returned, 0
1124 * if none, otherwise an error is returned.
1129 sys_mountctl(struct mountctl_args *uap)
1131 struct thread *td = curthread;
1139 * Sanity and permissions checks. We must be root.
1141 if (td->td_ucred->cr_prison != NULL)
1143 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1144 (error = priv_check(td, PRIV_ROOT)) != 0)
1148 * Argument length checks
1150 if (uap->ctllen < 0 || uap->ctllen > 1024)
1152 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1154 if (uap->path == NULL)
1158 * Allocate the necessary buffers and copyin data
1160 path = objcache_get(namei_oc, M_WAITOK);
1161 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1166 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1167 error = copyin(uap->ctl, ctl, uap->ctllen);
1172 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1175 * Validate the descriptor
1178 fp = holdfp(td, uap->fd, -1);
1188 * Execute the internal kernel function and clean up.
1190 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen,
1191 buf, uap->buflen, &uap->sysmsg_result);
1193 dropfp(td, uap->fd, fp);
1194 if (error == 0 && uap->sysmsg_result > 0)
1195 error = copyout(buf, uap->buf, uap->sysmsg_result);
1198 objcache_put(namei_oc, path);
1207 * Execute a mount control operation by resolving the path to a mount point
1208 * and calling vop_mountctl().
1210 * Use the mount point from the nch instead of the vnode so nullfs mounts
1211 * can properly spike the VOP.
1214 kern_mountctl(const char *path, int op, struct file *fp,
1215 const void *ctl, int ctllen,
1216 void *buf, int buflen, int *res)
1219 struct nlookupdata nd;
1220 struct nchandle nch;
1226 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1229 error = nlookup(&nd);
1234 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1241 * Yes, all this is needed to use the nch.mount below, because
1242 * we must maintain a ref on the mount to avoid ripouts (e.g.
1243 * due to heavy mount/unmount use by synth or poudriere).
1246 cache_zero(&nd.nl_nch);
1254 * Must be the root of the filesystem
1256 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1261 if (mp == NULL || mp->mnt_kern_flag & MNTK_UNMOUNT) {
1262 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1268 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1277 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1279 struct thread *td = curthread;
1280 struct proc *p = td->td_proc;
1283 char *fullpath, *freepath;
1286 if ((error = nlookup(nd)) != 0)
1288 mp = nd->nl_nch.mount;
1292 * Ignore refresh error, user should have visibility.
1293 * This can happen if a NFS mount goes bad (e.g. server
1294 * revokes perms or goes down).
1296 error = VFS_STATFS(mp, sp, nd->nl_cred);
1299 error = mount_path(p, mp, &fullpath, &freepath);
1302 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1303 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1304 kfree(freepath, M_TEMP);
1306 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1307 bcopy(sp, buf, sizeof(*buf));
1308 /* Only root should have access to the fsid's. */
1309 if (priv_check(td, PRIV_ROOT))
1310 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1315 * statfs_args(char *path, struct statfs *buf)
1317 * Get filesystem statistics.
1320 sys_statfs(struct statfs_args *uap)
1322 struct nlookupdata nd;
1326 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1328 error = kern_statfs(&nd, &buf);
1331 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1336 kern_fstatfs(int fd, struct statfs *buf)
1338 struct thread *td = curthread;
1339 struct proc *p = td->td_proc;
1343 char *fullpath, *freepath;
1347 if ((error = holdvnode(td, fd, &fp)) != 0)
1351 * Try to use mount info from any overlays rather than the
1352 * mount info for the underlying vnode, otherwise we will
1353 * fail when operating on null-mounted paths inside a chroot.
1355 if ((mp = fp->f_nchandle.mount) == NULL)
1356 mp = ((struct vnode *)fp->f_data)->v_mount;
1361 if (fp->f_cred == NULL) {
1367 * Ignore refresh error, user should have visibility.
1368 * This can happen if a NFS mount goes bad (e.g. server
1369 * revokes perms or goes down).
1372 error = VFS_STATFS(mp, sp, fp->f_cred);
1374 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1376 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1377 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1378 kfree(freepath, M_TEMP);
1380 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1381 bcopy(sp, buf, sizeof(*buf));
1383 /* Only root should have access to the fsid's. */
1384 if (priv_check(td, PRIV_ROOT))
1385 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1393 * fstatfs_args(int fd, struct statfs *buf)
1395 * Get filesystem statistics.
1398 sys_fstatfs(struct fstatfs_args *uap)
1403 error = kern_fstatfs(uap->fd, &buf);
1406 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1411 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1417 if ((error = nlookup(nd)) != 0)
1419 mp = nd->nl_nch.mount;
1420 sp = &mp->mnt_vstat;
1421 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1425 if (mp->mnt_flag & MNT_RDONLY)
1426 sp->f_flag |= ST_RDONLY;
1427 if (mp->mnt_flag & MNT_NOSUID)
1428 sp->f_flag |= ST_NOSUID;
1429 bcopy(sp, buf, sizeof(*buf));
1434 * statfs_args(char *path, struct statfs *buf)
1436 * Get filesystem statistics.
1439 sys_statvfs(struct statvfs_args *uap)
1441 struct nlookupdata nd;
1445 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1447 error = kern_statvfs(&nd, &buf);
1450 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1455 kern_fstatvfs(int fd, struct statvfs *buf)
1457 struct thread *td = curthread;
1463 if ((error = holdvnode(td, fd, &fp)) != 0)
1465 if ((mp = fp->f_nchandle.mount) == NULL)
1466 mp = ((struct vnode *)fp->f_data)->v_mount;
1471 if (fp->f_cred == NULL) {
1475 sp = &mp->mnt_vstat;
1476 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1480 if (mp->mnt_flag & MNT_RDONLY)
1481 sp->f_flag |= ST_RDONLY;
1482 if (mp->mnt_flag & MNT_NOSUID)
1483 sp->f_flag |= ST_NOSUID;
1485 bcopy(sp, buf, sizeof(*buf));
1493 * fstatfs_args(int fd, struct statfs *buf)
1495 * Get filesystem statistics.
1498 sys_fstatvfs(struct fstatvfs_args *uap)
1503 error = kern_fstatvfs(uap->fd, &buf);
1506 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1511 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1513 * Get statistics on all filesystems.
1516 struct getfsstat_info {
1517 struct statfs *sfsp;
1525 static int getfsstat_callback(struct mount *, void *);
1528 sys_getfsstat(struct getfsstat_args *uap)
1530 struct thread *td = curthread;
1531 struct getfsstat_info info;
1533 bzero(&info, sizeof(info));
1535 info.maxcount = uap->bufsize / sizeof(struct statfs);
1536 info.sfsp = uap->buf;
1538 info.flags = uap->flags;
1541 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1542 if (info.sfsp && info.count > info.maxcount)
1543 uap->sysmsg_result = info.maxcount;
1545 uap->sysmsg_result = info.count;
1546 return (info.error);
1550 getfsstat_callback(struct mount *mp, void *data)
1552 struct getfsstat_info *info = data;
1558 if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1561 if (info->sfsp && info->count < info->maxcount) {
1565 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1566 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1567 * overrides MNT_WAIT.
1569 * Ignore refresh error, user should have visibility.
1570 * This can happen if a NFS mount goes bad (e.g. server
1571 * revokes perms or goes down).
1573 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1574 (info->flags & MNT_WAIT)) &&
1575 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1578 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1580 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1582 info->error = error;
1585 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1586 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1587 kfree(freepath, M_TEMP);
1589 error = copyout(sp, info->sfsp, sizeof(*sp));
1591 info->error = error;
1601 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1602 long bufsize, int flags)
1604 * Get statistics on all filesystems.
1607 struct getvfsstat_info {
1608 struct statfs *sfsp;
1609 struct statvfs *vsfsp;
1617 static int getvfsstat_callback(struct mount *, void *);
1620 sys_getvfsstat(struct getvfsstat_args *uap)
1622 struct thread *td = curthread;
1623 struct getvfsstat_info info;
1625 bzero(&info, sizeof(info));
1627 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1628 info.sfsp = uap->buf;
1629 info.vsfsp = uap->vbuf;
1631 info.flags = uap->flags;
1634 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1635 if (info.vsfsp && info.count > info.maxcount)
1636 uap->sysmsg_result = info.maxcount;
1638 uap->sysmsg_result = info.count;
1639 return (info.error);
1643 getvfsstat_callback(struct mount *mp, void *data)
1645 struct getvfsstat_info *info = data;
1647 struct statvfs *vsp;
1652 if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1655 if (info->vsfsp && info->count < info->maxcount) {
1657 vsp = &mp->mnt_vstat;
1660 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1661 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1662 * overrides MNT_WAIT.
1664 * Ignore refresh error, user should have visibility.
1665 * This can happen if a NFS mount goes bad (e.g. server
1666 * revokes perms or goes down).
1668 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1669 (info->flags & MNT_WAIT)) &&
1670 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1673 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1675 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1676 (info->flags & MNT_WAIT)) &&
1677 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1681 if (mp->mnt_flag & MNT_RDONLY)
1682 vsp->f_flag |= ST_RDONLY;
1683 if (mp->mnt_flag & MNT_NOSUID)
1684 vsp->f_flag |= ST_NOSUID;
1686 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1688 info->error = error;
1691 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1692 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1693 kfree(freepath, M_TEMP);
1695 error = copyout(sp, info->sfsp, sizeof(*sp));
1697 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1699 info->error = error;
1711 * fchdir_args(int fd)
1713 * Change current working directory to a given file descriptor.
1716 sys_fchdir(struct fchdir_args *uap)
1718 struct thread *td = curthread;
1719 struct proc *p = td->td_proc;
1720 struct filedesc *fdp = p->p_fd;
1721 struct vnode *vp, *ovp;
1724 struct nchandle nch, onch, tnch;
1727 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
1729 lwkt_gettoken(&p->p_token);
1730 vp = (struct vnode *)fp->f_data;
1732 vn_lock(vp, LK_SHARED | LK_RETRY);
1733 if (fp->f_nchandle.ncp == NULL)
1736 error = checkvp_chdir(vp, td);
1741 cache_copy(&fp->f_nchandle, &nch);
1744 * If the ncp has become a mount point, traverse through
1748 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1749 (mp = cache_findmount(&nch)) != NULL
1751 error = nlookup_mp(mp, &tnch);
1753 cache_unlock(&tnch); /* leave ref intact */
1755 vp = tnch.ncp->nc_vp;
1756 error = vget(vp, LK_SHARED);
1757 KKASSERT(error == 0);
1761 cache_dropmount(mp);
1764 spin_lock(&fdp->fd_spin);
1766 onch = fdp->fd_ncdir;
1768 fdp->fd_ncdir = nch;
1769 spin_unlock(&fdp->fd_spin);
1770 vn_unlock(vp); /* leave ref intact */
1779 lwkt_reltoken(&p->p_token);
1784 kern_chdir(struct nlookupdata *nd)
1786 struct thread *td = curthread;
1787 struct proc *p = td->td_proc;
1788 struct filedesc *fdp = p->p_fd;
1789 struct vnode *vp, *ovp;
1790 struct nchandle onch;
1793 nd->nl_flags |= NLC_SHAREDLOCK;
1794 if ((error = nlookup(nd)) != 0)
1796 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1798 if ((error = vget(vp, LK_SHARED)) != 0)
1801 lwkt_gettoken(&p->p_token);
1802 error = checkvp_chdir(vp, td);
1805 spin_lock(&fdp->fd_spin);
1807 onch = fdp->fd_ncdir;
1808 fdp->fd_ncdir = nd->nl_nch;
1810 spin_unlock(&fdp->fd_spin);
1811 cache_unlock(&nd->nl_nch); /* leave reference intact */
1814 cache_zero(&nd->nl_nch);
1818 lwkt_reltoken(&p->p_token);
1823 * chdir_args(char *path)
1825 * Change current working directory (``.'').
1828 sys_chdir(struct chdir_args *uap)
1830 struct nlookupdata nd;
1833 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1835 error = kern_chdir(&nd);
1841 * Helper function for raised chroot(2) security function: Refuse if
1842 * any filedescriptors are open directories.
1845 chroot_refuse_vdir_fds(thread_t td, struct filedesc *fdp)
1852 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1853 if ((error = holdvnode(td, fd, &fp)) != 0)
1855 vp = (struct vnode *)fp->f_data;
1856 if (vp->v_type != VDIR) {
1867 * This sysctl determines if we will allow a process to chroot(2) if it
1868 * has a directory open:
1869 * 0: disallowed for all processes.
1870 * 1: allowed for processes that were not already chroot(2)'ed.
1871 * 2: allowed for all processes.
1874 static int chroot_allow_open_directories = 1;
1876 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1877 &chroot_allow_open_directories, 0, "");
1880 * chroot to the specified namecache entry. We obtain the vp from the
1881 * namecache data. The passed ncp must be locked and referenced and will
1882 * remain locked and referenced on return.
1885 kern_chroot(struct nchandle *nch)
1887 struct thread *td = curthread;
1888 struct proc *p = td->td_proc;
1889 struct filedesc *fdp = p->p_fd;
1894 * Only privileged user can chroot
1896 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1901 * Disallow open directory descriptors (fchdir() breakouts).
1903 if (chroot_allow_open_directories == 0 ||
1904 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1905 if ((error = chroot_refuse_vdir_fds(td, fdp)) != 0)
1908 if ((vp = nch->ncp->nc_vp) == NULL)
1911 if ((error = vget(vp, LK_SHARED)) != 0)
1915 * Check the validity of vp as a directory to change to and
1916 * associate it with rdir/jdir.
1918 error = checkvp_chdir(vp, td);
1919 vn_unlock(vp); /* leave reference intact */
1921 lwkt_gettoken(&p->p_token);
1922 vrele(fdp->fd_rdir);
1923 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1924 cache_drop(&fdp->fd_nrdir);
1925 cache_copy(nch, &fdp->fd_nrdir);
1926 if (fdp->fd_jdir == NULL) {
1929 cache_copy(nch, &fdp->fd_njdir);
1931 if ((p->p_flags & P_DIDCHROOT) == 0) {
1932 p->p_flags |= P_DIDCHROOT;
1933 if (p->p_depth <= 65535 - 32)
1936 lwkt_reltoken(&p->p_token);
1944 * chroot_args(char *path)
1946 * Change notion of root (``/'') directory.
1949 sys_chroot(struct chroot_args *uap)
1951 struct thread *td __debugvar = curthread;
1952 struct nlookupdata nd;
1955 KKASSERT(td->td_proc);
1956 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1958 nd.nl_flags |= NLC_EXEC;
1959 error = nlookup(&nd);
1961 error = kern_chroot(&nd.nl_nch);
1968 sys_chroot_kernel(struct chroot_kernel_args *uap)
1970 struct thread *td = curthread;
1971 struct nlookupdata nd;
1972 struct nchandle *nch;
1976 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1980 error = nlookup(&nd);
1986 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1990 if ((vp = nch->ncp->nc_vp) == NULL) {
1995 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1998 vfs_cache_setroot(vp, cache_hold(nch));
2007 * Common routine for chroot and chdir. Given a locked, referenced vnode,
2008 * determine whether it is legal to chdir to the vnode. The vnode's state
2009 * is not changed by this call.
2012 checkvp_chdir(struct vnode *vp, struct thread *td)
2016 if (vp->v_type != VDIR)
2019 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
2024 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
2026 struct thread *td = curthread;
2027 struct proc *p = td->td_proc;
2028 struct lwp *lp = td->td_lwp;
2029 struct filedesc *fdp = p->p_fd;
2034 int type, indx, error = 0;
2037 if ((oflags & O_ACCMODE) == O_ACCMODE)
2039 flags = FFLAGS(oflags);
2040 error = falloc(lp, &nfp, NULL);
2044 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
2047 * XXX p_dupfd is a real mess. It allows a device to return a
2048 * file descriptor to be duplicated rather then doing the open
2054 * Call vn_open() to do the lookup and assign the vnode to the
2055 * file pointer. vn_open() does not change the ref count on fp
2056 * and the vnode, on success, will be inherited by the file pointer
2059 * Request a shared lock on the vnode if possible.
2061 * Executable binaries can race VTEXT against O_RDWR opens, so
2062 * use an exclusive lock for O_RDWR opens as well.
2064 * NOTE: We need a flag to separate terminal vnode locking from
2065 * parent locking. O_CREAT needs parent locking, but O_TRUNC
2066 * and O_RDWR only need to lock the terminal vnode exclusively.
2068 nd->nl_flags |= NLC_LOCKVP;
2069 if ((flags & (O_CREAT|O_TRUNC|O_RDWR)) == 0)
2070 nd->nl_flags |= NLC_SHAREDLOCK;
2072 error = vn_open(nd, fp, flags, cmode);
2077 * handle special fdopen() case. bleh. dupfdopen() is
2078 * responsible for dropping the old contents of ofiles[indx]
2081 * Note that fsetfd() will add a ref to fp which represents
2082 * the fd_files[] assignment. We must still drop our
2085 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
2086 if (fdalloc(p, 0, &indx) == 0) {
2087 error = dupfdopen(td, indx, lp->lwp_dupfd, flags, error);
2090 fdrop(fp); /* our ref */
2093 fsetfd(fdp, NULL, indx);
2096 fdrop(fp); /* our ref */
2097 if (error == ERESTART)
2103 * ref the vnode for ourselves so it can't be ripped out from under
2104 * is. XXX need an ND flag to request that the vnode be returned
2107 * Reserve a file descriptor but do not assign it until the open
2110 vp = (struct vnode *)fp->f_data;
2112 if ((error = fdalloc(p, 0, &indx)) != 0) {
2119 * If no error occurs the vp will have been assigned to the file
2124 if (flags & (O_EXLOCK | O_SHLOCK)) {
2125 lf.l_whence = SEEK_SET;
2128 if (flags & O_EXLOCK)
2129 lf.l_type = F_WRLCK;
2131 lf.l_type = F_RDLCK;
2132 if (flags & FNONBLOCK)
2137 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2139 * lock request failed. Clean up the reserved
2143 fsetfd(fdp, NULL, indx);
2147 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
2151 * Assert that all regular file vnodes were created with a object.
2153 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
2154 ("open: regular file has no backing object after vn_open"));
2160 * release our private reference, leaving the one associated with the
2161 * descriptor table intact.
2163 if (oflags & O_CLOEXEC)
2164 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
2165 fsetfd(fdp, fp, indx);
2173 * open_args(char *path, int flags, int mode)
2175 * Check permissions, allocate an open file structure,
2176 * and call the device open routine if any.
2179 sys_open(struct open_args *uap)
2181 struct nlookupdata nd;
2184 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2186 error = kern_open(&nd, uap->flags,
2187 uap->mode, &uap->sysmsg_result);
2194 * openat_args(int fd, char *path, int flags, int mode)
2197 sys_openat(struct openat_args *uap)
2199 struct nlookupdata nd;
2203 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2205 error = kern_open(&nd, uap->flags, uap->mode,
2206 &uap->sysmsg_result);
2208 nlookup_done_at(&nd, fp);
2213 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2215 struct thread *td = curthread;
2216 struct proc *p = td->td_proc;
2225 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2226 vattr.va_rmajor = rmajor;
2227 vattr.va_rminor = rminor;
2229 switch (mode & S_IFMT) {
2230 case S_IFMT: /* used by badsect to flag bad sectors */
2231 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2232 vattr.va_type = VBAD;
2235 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2236 vattr.va_type = VCHR;
2239 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2240 vattr.va_type = VBLK;
2243 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2246 case S_IFDIR: /* special directories support for HAMMER */
2247 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2248 vattr.va_type = VDIR;
2259 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2260 if ((error = nlookup(nd)) != 0)
2262 if (nd->nl_nch.ncp->nc_vp)
2264 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2268 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2269 nd->nl_cred, NAMEI_CREATE);
2272 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2273 &vp, nd->nl_cred, &vattr);
2281 * mknod_args(char *path, int mode, int dev)
2283 * Create a special file.
2286 sys_mknod(struct mknod_args *uap)
2288 struct nlookupdata nd;
2291 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2293 error = kern_mknod(&nd, uap->mode,
2294 umajor(uap->dev), uminor(uap->dev));
2301 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2303 * Create a special file. The path is relative to the directory associated
2307 sys_mknodat(struct mknodat_args *uap)
2309 struct nlookupdata nd;
2313 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2315 error = kern_mknod(&nd, uap->mode,
2316 umajor(uap->dev), uminor(uap->dev));
2318 nlookup_done_at(&nd, fp);
2323 kern_mkfifo(struct nlookupdata *nd, int mode)
2325 struct thread *td = curthread;
2326 struct proc *p = td->td_proc;
2333 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2334 if ((error = nlookup(nd)) != 0)
2336 if (nd->nl_nch.ncp->nc_vp)
2338 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2342 vattr.va_type = VFIFO;
2343 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2345 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2352 * mkfifo_args(char *path, int mode)
2354 * Create a named pipe.
2357 sys_mkfifo(struct mkfifo_args *uap)
2359 struct nlookupdata nd;
2362 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2364 error = kern_mkfifo(&nd, uap->mode);
2370 * mkfifoat_args(int fd, char *path, mode_t mode)
2372 * Create a named pipe. The path is relative to the directory associated
2376 sys_mkfifoat(struct mkfifoat_args *uap)
2378 struct nlookupdata nd;
2382 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2384 error = kern_mkfifo(&nd, uap->mode);
2385 nlookup_done_at(&nd, fp);
2389 static int hardlink_check_uid = 0;
2390 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2391 &hardlink_check_uid, 0,
2392 "Unprivileged processes cannot create hard links to files owned by other "
2394 static int hardlink_check_gid = 0;
2395 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2396 &hardlink_check_gid, 0,
2397 "Unprivileged processes cannot create hard links to files owned by other "
2401 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2407 * Shortcut if disabled
2409 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2413 * Privileged user can always hardlink
2415 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2419 * Otherwise only if the originating file is owned by the
2420 * same user or group. Note that any group is allowed if
2421 * the file is owned by the caller.
2423 error = VOP_GETATTR(vp, &va);
2427 if (hardlink_check_uid) {
2428 if (cred->cr_uid != va.va_uid)
2432 if (hardlink_check_gid) {
2433 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2441 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2443 struct thread *td = curthread;
2448 * Lookup the source and obtained a locked vnode.
2450 * You may only hardlink a file which you have write permission
2451 * on or which you own.
2453 * XXX relookup on vget failure / race ?
2456 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2457 if ((error = nlookup(nd)) != 0)
2459 vp = nd->nl_nch.ncp->nc_vp;
2460 KKASSERT(vp != NULL);
2461 if (vp->v_type == VDIR)
2462 return (EPERM); /* POSIX */
2463 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2465 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2469 * Unlock the source so we can lookup the target without deadlocking
2470 * (XXX vp is locked already, possible other deadlock?). The target
2473 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2474 nd->nl_flags &= ~NLC_NCPISLOCKED;
2475 cache_unlock(&nd->nl_nch);
2478 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2479 if ((error = nlookup(linknd)) != 0) {
2483 if (linknd->nl_nch.ncp->nc_vp) {
2487 VFS_MODIFYING(vp->v_mount);
2488 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2495 * Finally run the new API VOP.
2497 error = can_hardlink(vp, td, td->td_ucred);
2499 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2500 vp, linknd->nl_cred);
2507 * link_args(char *path, char *link)
2509 * Make a hard file link.
2512 sys_link(struct link_args *uap)
2514 struct nlookupdata nd, linknd;
2517 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2519 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2521 error = kern_link(&nd, &linknd);
2522 nlookup_done(&linknd);
2529 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2531 * Make a hard file link. The path1 argument is relative to the directory
2532 * associated with fd1, and similarly the path2 argument is relative to
2533 * the directory associated with fd2.
2536 sys_linkat(struct linkat_args *uap)
2538 struct nlookupdata nd, linknd;
2539 struct file *fp1, *fp2;
2542 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2543 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2545 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2546 uap->path2, UIO_USERSPACE, 0);
2548 error = kern_link(&nd, &linknd);
2549 nlookup_done_at(&linknd, fp2);
2551 nlookup_done_at(&nd, fp1);
2556 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2564 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2565 if ((error = nlookup(nd)) != 0)
2567 if (nd->nl_nch.ncp->nc_vp)
2569 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2573 vattr.va_mode = mode;
2574 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2581 * symlink(char *path, char *link)
2583 * Make a symbolic link.
2586 sys_symlink(struct symlink_args *uap)
2588 struct thread *td = curthread;
2589 struct nlookupdata nd;
2594 path = objcache_get(namei_oc, M_WAITOK);
2595 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2597 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2599 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2600 error = kern_symlink(&nd, path, mode);
2604 objcache_put(namei_oc, path);
2609 * symlinkat_args(char *path1, int fd, char *path2)
2611 * Make a symbolic link. The path2 argument is relative to the directory
2612 * associated with fd.
2615 sys_symlinkat(struct symlinkat_args *uap)
2617 struct thread *td = curthread;
2618 struct nlookupdata nd;
2624 path1 = objcache_get(namei_oc, M_WAITOK);
2625 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2627 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2630 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2631 error = kern_symlink(&nd, path1, mode);
2633 nlookup_done_at(&nd, fp);
2635 objcache_put(namei_oc, path1);
2640 * undelete_args(char *path)
2642 * Delete a whiteout from the filesystem.
2645 sys_undelete(struct undelete_args *uap)
2647 struct nlookupdata nd;
2650 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2652 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2654 error = nlookup(&nd);
2656 error = ncp_writechk(&nd.nl_nch);
2658 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2666 kern_unlink(struct nlookupdata *nd)
2671 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2672 if ((error = nlookup(nd)) != 0)
2674 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2676 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2681 * unlink_args(char *path)
2683 * Delete a name from the filesystem.
2686 sys_unlink(struct unlink_args *uap)
2688 struct nlookupdata nd;
2691 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2693 error = kern_unlink(&nd);
2700 * unlinkat_args(int fd, char *path, int flags)
2702 * Delete the file or directory entry pointed to by fd/path.
2705 sys_unlinkat(struct unlinkat_args *uap)
2707 struct nlookupdata nd;
2711 if (uap->flags & ~AT_REMOVEDIR)
2714 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2716 if (uap->flags & AT_REMOVEDIR)
2717 error = kern_rmdir(&nd);
2719 error = kern_unlink(&nd);
2721 nlookup_done_at(&nd, fp);
2726 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2728 struct thread *td = curthread;
2735 fp = holdfp(td, fd, -1);
2738 if (fp->f_type != DTYPE_VNODE) {
2742 vp = (struct vnode *)fp->f_data;
2746 spin_lock(&fp->f_spin);
2747 new_offset = fp->f_offset + offset;
2751 error = VOP_GETATTR_FP(vp, &vattr, fp);
2752 spin_lock(&fp->f_spin);
2753 new_offset = offset + vattr.va_size;
2756 new_offset = offset;
2758 spin_lock(&fp->f_spin);
2763 spin_lock(&fp->f_spin);
2768 * Validate the seek position. Negative offsets are not allowed
2769 * for regular files or directories.
2771 * Normally we would also not want to allow negative offsets for
2772 * character and block-special devices. However kvm addresses
2773 * on 64 bit architectures might appear to be negative and must
2777 if (new_offset < 0 &&
2778 (vp->v_type == VREG || vp->v_type == VDIR)) {
2781 fp->f_offset = new_offset;
2784 *res = fp->f_offset;
2785 spin_unlock(&fp->f_spin);
2793 * lseek_args(int fd, int pad, off_t offset, int whence)
2795 * Reposition read/write file offset.
2798 sys_lseek(struct lseek_args *uap)
2802 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2803 &uap->sysmsg_offset);
2809 * Check if current process can access given file. amode is a bitmask of *_OK
2810 * access bits. flags is a bitmask of AT_* flags.
2813 kern_access(struct nlookupdata *nd, int amode, int flags)
2818 if (flags & ~AT_EACCESS)
2820 nd->nl_flags |= NLC_SHAREDLOCK;
2821 if ((error = nlookup(nd)) != 0)
2824 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2828 /* Flags == 0 means only check for existence. */
2837 if ((mode & VWRITE) == 0 ||
2838 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2839 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2842 * If the file handle is stale we have to re-resolve the
2843 * entry with the ncp held exclusively. This is a hack
2846 if (error == ESTALE) {
2848 cache_unlock(&nd->nl_nch);
2849 cache_lock(&nd->nl_nch);
2850 cache_setunresolved(&nd->nl_nch);
2851 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2864 * access_args(char *path, int flags)
2866 * Check access permissions.
2869 sys_access(struct access_args *uap)
2871 struct nlookupdata nd;
2874 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2876 error = kern_access(&nd, uap->flags, 0);
2883 * eaccess_args(char *path, int flags)
2885 * Check access permissions.
2888 sys_eaccess(struct eaccess_args *uap)
2890 struct nlookupdata nd;
2893 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2895 error = kern_access(&nd, uap->flags, AT_EACCESS);
2902 * faccessat_args(int fd, char *path, int amode, int flags)
2904 * Check access permissions.
2907 sys_faccessat(struct faccessat_args *uap)
2909 struct nlookupdata nd;
2913 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2916 error = kern_access(&nd, uap->amode, uap->flags);
2917 nlookup_done_at(&nd, fp);
2922 kern_stat(struct nlookupdata *nd, struct stat *st)
2927 nd->nl_flags |= NLC_SHAREDLOCK;
2928 if ((error = nlookup(nd)) != 0)
2931 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2934 if ((error = vget(vp, LK_SHARED)) != 0)
2936 error = vn_stat(vp, st, nd->nl_cred);
2939 * If the file handle is stale we have to re-resolve the
2940 * entry with the ncp held exclusively. This is a hack
2943 if (error == ESTALE) {
2945 cache_unlock(&nd->nl_nch);
2946 cache_lock(&nd->nl_nch);
2947 cache_setunresolved(&nd->nl_nch);
2948 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2958 * stat_args(char *path, struct stat *ub)
2960 * Get file status; this version follows links.
2963 sys_stat(struct stat_args *uap)
2965 struct nlookupdata nd;
2969 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2971 error = kern_stat(&nd, &st);
2973 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2980 * lstat_args(char *path, struct stat *ub)
2982 * Get file status; this version does not follow links.
2985 sys_lstat(struct lstat_args *uap)
2987 struct nlookupdata nd;
2991 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2993 error = kern_stat(&nd, &st);
2995 error = copyout(&st, uap->ub, sizeof(*uap->ub));
3002 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
3004 * Get status of file pointed to by fd/path.
3007 sys_fstatat(struct fstatat_args *uap)
3009 struct nlookupdata nd;
3015 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3018 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3020 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3021 UIO_USERSPACE, flags);
3023 error = kern_stat(&nd, &st);
3025 error = copyout(&st, uap->sb, sizeof(*uap->sb));
3027 nlookup_done_at(&nd, fp);
3032 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
3034 struct nlookupdata nd;
3039 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
3041 error = nlookup(&nd);
3043 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3046 error = VOP_PATHCONF(vp, name, sysmsg_regp);
3053 * pathconf_Args(char *path, int name)
3055 * Get configurable pathname variables.
3058 sys_pathconf(struct pathconf_args *uap)
3060 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
3065 * lpathconf_Args(char *path, int name)
3067 * Get configurable pathname variables, but don't follow symlinks.
3070 sys_lpathconf(struct lpathconf_args *uap)
3072 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
3077 * kern_readlink isn't properly split yet. There is a copyin burried
3078 * in VOP_READLINK().
3081 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
3083 struct thread *td = curthread;
3089 nd->nl_flags |= NLC_SHAREDLOCK;
3090 if ((error = nlookup(nd)) != 0)
3092 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
3095 if (vp->v_type != VLNK) {
3098 aiov.iov_base = buf;
3099 aiov.iov_len = count;
3100 auio.uio_iov = &aiov;
3101 auio.uio_iovcnt = 1;
3102 auio.uio_offset = 0;
3103 auio.uio_rw = UIO_READ;
3104 auio.uio_segflg = UIO_USERSPACE;
3106 auio.uio_resid = count;
3107 error = VOP_READLINK(vp, &auio, td->td_ucred);
3110 *res = count - auio.uio_resid;
3115 * readlink_args(char *path, char *buf, int count)
3117 * Return target name of a symbolic link.
3120 sys_readlink(struct readlink_args *uap)
3122 struct nlookupdata nd;
3125 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3127 error = kern_readlink(&nd, uap->buf, uap->count,
3128 &uap->sysmsg_result);
3135 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3137 * Return target name of a symbolic link. The path is relative to the
3138 * directory associated with fd.
3141 sys_readlinkat(struct readlinkat_args *uap)
3143 struct nlookupdata nd;
3147 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3149 error = kern_readlink(&nd, uap->buf, uap->bufsize,
3150 &uap->sysmsg_result);
3152 nlookup_done_at(&nd, fp);
3157 setfflags(struct vnode *vp, u_long flags)
3159 struct thread *td = curthread;
3164 * Prevent non-root users from setting flags on devices. When
3165 * a device is reused, users can retain ownership of the device
3166 * if they are allowed to set flags and programs assume that
3167 * chown can't fail when done as root.
3169 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
3170 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
3174 * note: vget is required for any operation that might mod the vnode
3175 * so VINACTIVE is properly cleared.
3177 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3179 vattr.va_flags = flags;
3180 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3187 * chflags(const char *path, u_long flags)
3189 * Change flags of a file given a path name.
3192 sys_chflags(struct chflags_args *uap)
3194 struct nlookupdata nd;
3199 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3201 error = nlookup(&nd);
3203 error = ncp_writechk(&nd.nl_nch);
3205 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3208 error = setfflags(vp, uap->flags);
3215 * lchflags(const char *path, u_long flags)
3217 * Change flags of a file given a path name, but don't follow symlinks.
3220 sys_lchflags(struct lchflags_args *uap)
3222 struct nlookupdata nd;
3227 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3229 error = nlookup(&nd);
3231 error = ncp_writechk(&nd.nl_nch);
3233 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3236 error = setfflags(vp, uap->flags);
3243 * fchflags_args(int fd, u_flags flags)
3245 * Change flags of a file given a file descriptor.
3248 sys_fchflags(struct fchflags_args *uap)
3250 struct thread *td = curthread;
3254 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3256 if (fp->f_nchandle.ncp)
3257 error = ncp_writechk(&fp->f_nchandle);
3259 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3265 * chflagsat_args(int fd, const char *path, u_long flags, int atflags)
3266 * change flags given a pathname relative to a filedescriptor
3268 int sys_chflagsat(struct chflagsat_args *uap)
3270 struct nlookupdata nd;
3276 if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3279 lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3282 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags);
3284 error = nlookup(&nd);
3286 error = ncp_writechk(&nd.nl_nch);
3288 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3289 nlookup_done_at(&nd, fp);
3291 error = setfflags(vp, uap->flags);
3299 setfmode(struct vnode *vp, int mode)
3301 struct thread *td = curthread;
3306 * note: vget is required for any operation that might mod the vnode
3307 * so VINACTIVE is properly cleared.
3309 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3311 vattr.va_mode = mode & ALLPERMS;
3312 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3313 cache_inval_wxok(vp);
3320 kern_chmod(struct nlookupdata *nd, int mode)
3325 if ((error = nlookup(nd)) != 0)
3327 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3329 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3330 error = setfmode(vp, mode);
3336 * chmod_args(char *path, int mode)
3338 * Change mode of a file given path name.
3341 sys_chmod(struct chmod_args *uap)
3343 struct nlookupdata nd;
3346 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3348 error = kern_chmod(&nd, uap->mode);
3354 * lchmod_args(char *path, int mode)
3356 * Change mode of a file given path name (don't follow links.)
3359 sys_lchmod(struct lchmod_args *uap)
3361 struct nlookupdata nd;
3364 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3366 error = kern_chmod(&nd, uap->mode);
3372 * fchmod_args(int fd, int mode)
3374 * Change mode of a file given a file descriptor.
3377 sys_fchmod(struct fchmod_args *uap)
3379 struct thread *td = curthread;
3383 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3385 if (fp->f_nchandle.ncp)
3386 error = ncp_writechk(&fp->f_nchandle);
3388 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3394 * fchmodat_args(char *path, int mode)
3396 * Change mode of a file pointed to by fd/path.
3399 sys_fchmodat(struct fchmodat_args *uap)
3401 struct nlookupdata nd;
3406 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3408 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3410 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3411 UIO_USERSPACE, flags);
3413 error = kern_chmod(&nd, uap->mode);
3414 nlookup_done_at(&nd, fp);
3419 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3421 struct thread *td = curthread;
3429 * note: vget is required for any operation that might mod the vnode
3430 * so VINACTIVE is properly cleared.
3432 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3433 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3435 o_uid = vattr.va_uid;
3436 o_gid = vattr.va_gid;
3437 size = vattr.va_size;
3442 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3451 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3452 VFS_ACCOUNT(mp, uid, gid, size);
3459 kern_chown(struct nlookupdata *nd, int uid, int gid)
3464 if ((error = nlookup(nd)) != 0)
3466 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3468 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3469 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3475 * chown(char *path, int uid, int gid)
3477 * Set ownership given a path name.
3480 sys_chown(struct chown_args *uap)
3482 struct nlookupdata nd;
3485 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3487 error = kern_chown(&nd, uap->uid, uap->gid);
3493 * lchown_args(char *path, int uid, int gid)
3495 * Set ownership given a path name, do not cross symlinks.
3498 sys_lchown(struct lchown_args *uap)
3500 struct nlookupdata nd;
3503 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3505 error = kern_chown(&nd, uap->uid, uap->gid);
3511 * fchown_args(int fd, int uid, int gid)
3513 * Set ownership given a file descriptor.
3516 sys_fchown(struct fchown_args *uap)
3518 struct thread *td = curthread;
3519 struct proc *p = td->td_proc;
3523 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3525 if (fp->f_nchandle.ncp)
3526 error = ncp_writechk(&fp->f_nchandle);
3528 error = setfown(p->p_fd->fd_ncdir.mount,
3529 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3535 * fchownat(int fd, char *path, int uid, int gid, int flags)
3537 * Set ownership of file pointed to by fd/path.
3540 sys_fchownat(struct fchownat_args *uap)
3542 struct nlookupdata nd;
3547 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3549 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3551 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3552 UIO_USERSPACE, flags);
3554 error = kern_chown(&nd, uap->uid, uap->gid);
3555 nlookup_done_at(&nd, fp);
3561 getutimes(struct timeval *tvp, struct timespec *tsp)
3563 struct timeval tv[2];
3568 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3571 if ((error = itimerfix(tvp)) != 0)
3573 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3574 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3580 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3582 struct timespec tsnow;
3596 if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3598 if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3601 if (newts[0].tv_nsec == UTIME_OMIT)
3602 newts[0].tv_sec = VNOVAL;
3603 else if (newts[0].tv_nsec == UTIME_NOW)
3605 else if ((error = itimespecfix(&newts[0])) != 0)
3608 if (newts[1].tv_nsec == UTIME_OMIT)
3609 newts[1].tv_sec = VNOVAL;
3610 else if (newts[1].tv_nsec == UTIME_NOW)
3612 else if ((error = itimespecfix(&newts[1])) != 0)
3619 setutimes(struct vnode *vp, struct vattr *vattr,
3620 const struct timespec *ts, int nullflag)
3622 struct thread *td = curthread;
3626 vattr->va_atime = ts[0];
3627 vattr->va_mtime = ts[1];
3629 vattr->va_vaflags |= VA_UTIMES_NULL;
3630 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3636 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3638 struct timespec ts[2];
3642 if ((error = getutimes(tptr, ts)) != 0)
3645 error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3650 * utimes_args(char *path, struct timeval *tptr)
3652 * Set the access and modification times of a file.
3655 sys_utimes(struct utimes_args *uap)
3657 struct timeval tv[2];
3658 struct nlookupdata nd;
3662 error = copyin(uap->tptr, tv, sizeof(tv));
3666 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3668 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3674 * lutimes_args(char *path, struct timeval *tptr)
3676 * Set the access and modification times of a file.
3679 sys_lutimes(struct lutimes_args *uap)
3681 struct timeval tv[2];
3682 struct nlookupdata nd;
3686 error = copyin(uap->tptr, tv, sizeof(tv));
3690 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3692 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3698 * Set utimes on a file descriptor. The creds used to open the
3699 * file are used to determine whether the operation is allowed
3703 kern_futimens(int fd, struct timespec *ts)
3705 struct thread *td = curthread;
3706 struct timespec newts[2];
3713 error = getutimens(ts, newts, &nullflag);
3716 if ((error = holdvnode(td, fd, &fp)) != 0)
3718 if (fp->f_nchandle.ncp)
3719 error = ncp_writechk(&fp->f_nchandle);
3722 error = vget(vp, LK_EXCLUSIVE);
3724 error = VOP_GETATTR_FP(vp, &vattr, fp);
3726 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3730 error = setutimes(vp, &vattr, newts, nullflag);
3740 * futimens_args(int fd, struct timespec *ts)
3742 * Set the access and modification times of a file.
3745 sys_futimens(struct futimens_args *uap)
3747 struct timespec ts[2];
3751 error = copyin(uap->ts, ts, sizeof(ts));
3755 error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3760 kern_futimes(int fd, struct timeval *tptr)
3762 struct timespec ts[2];
3766 if ((error = getutimes(tptr, ts)) != 0)
3769 error = kern_futimens(fd, tptr ? ts : NULL);
3774 * futimes_args(int fd, struct timeval *tptr)
3776 * Set the access and modification times of a file.
3779 sys_futimes(struct futimes_args *uap)
3781 struct timeval tv[2];
3785 error = copyin(uap->tptr, tv, sizeof(tv));
3789 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3794 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3796 struct timespec newts[2];
3802 if (flags & ~AT_SYMLINK_NOFOLLOW)
3805 error = getutimens(ts, newts, &nullflag);
3809 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3810 if ((error = nlookup(nd)) != 0)
3812 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3814 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3816 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3817 error = vget(vp, LK_EXCLUSIVE);
3819 error = setutimes(vp, &vattr, newts, nullflag);
3828 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3830 * Set file access and modification times of a file.
3833 sys_utimensat(struct utimensat_args *uap)
3835 struct timespec ts[2];
3836 struct nlookupdata nd;
3842 error = copyin(uap->ts, ts, sizeof(ts));
3847 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3848 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3849 UIO_USERSPACE, flags);
3851 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3852 nlookup_done_at(&nd, fp);
3857 kern_truncate(struct nlookupdata *nd, off_t length)
3864 uint64_t old_size = 0;
3868 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3869 if ((error = nlookup(nd)) != 0)
3871 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3873 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3875 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3880 if (vp->v_type == VDIR) {
3884 if (vfs_quota_enabled) {
3885 error = VOP_GETATTR(vp, &vattr);
3886 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3889 old_size = vattr.va_size;
3892 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3894 vattr.va_size = length;
3895 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3896 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3904 * truncate(char *path, int pad, off_t length)
3906 * Truncate a file given its path name.
3909 sys_truncate(struct truncate_args *uap)
3911 struct nlookupdata nd;
3914 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3916 error = kern_truncate(&nd, uap->length);
3922 kern_ftruncate(int fd, off_t length)
3924 struct thread *td = curthread;
3931 uint64_t old_size = 0;
3936 if ((error = holdvnode(td, fd, &fp)) != 0)
3938 if (fp->f_nchandle.ncp) {
3939 error = ncp_writechk(&fp->f_nchandle);
3943 if ((fp->f_flag & FWRITE) == 0) {
3947 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3951 vp = (struct vnode *)fp->f_data;
3952 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3953 if (vp->v_type == VDIR) {
3959 if (vfs_quota_enabled) {
3960 error = VOP_GETATTR_FP(vp, &vattr, fp);
3961 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3964 old_size = vattr.va_size;
3967 if ((error = vn_writechk(vp, NULL)) == 0) {
3969 vattr.va_size = length;
3970 error = VOP_SETATTR_FP(vp, &vattr, fp->f_cred, fp);
3972 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3981 * ftruncate_args(int fd, int pad, off_t length)
3983 * Truncate a file given a file descriptor.
3986 sys_ftruncate(struct ftruncate_args *uap)
3990 error = kern_ftruncate(uap->fd, uap->length);
3998 * Sync an open file.
4001 sys_fsync(struct fsync_args *uap)
4003 struct thread *td = curthread;
4009 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
4011 vp = (struct vnode *)fp->f_data;
4012 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4013 if ((obj = vp->v_object) != NULL) {
4014 if (vp->v_mount == NULL ||
4015 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
4016 vm_object_page_clean(obj, 0, 0, 0);
4019 error = VOP_FSYNC_FP(vp, MNT_WAIT, VOP_FSYNC_SYSCALL, fp);
4020 if (error == 0 && vp->v_mount)
4021 error = buf_fsync(vp);
4029 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
4031 struct nchandle fnchd;
4032 struct nchandle tnchd;
4033 struct namecache *ncp;
4042 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
4043 if ((error = nlookup(fromnd)) != 0)
4045 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
4047 fnchd.mount = fromnd->nl_nch.mount;
4051 * unlock the source nch so we can lookup the target nch without
4052 * deadlocking. The target may or may not exist so we do not check
4053 * for a target vp like kern_mkdir() and other creation functions do.
4055 * The source and target directories are ref'd and rechecked after
4056 * everything is relocked to determine if the source or target file
4059 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
4060 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
4062 fncp_gen = fromnd->nl_nch.ncp->nc_generation;
4064 cache_unlock(&fromnd->nl_nch);
4066 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
4067 if ((error = nlookup(tond)) != 0) {
4071 tncp_gen = tond->nl_nch.ncp->nc_generation;
4073 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
4077 tnchd.mount = tond->nl_nch.mount;
4081 * If the source and target are the same there is nothing to do
4083 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
4090 * Mount points cannot be renamed or overwritten
4092 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
4101 * Relock the source ncp. cache_relock() will deal with any
4102 * deadlocks against the already-locked tond and will also
4103 * make sure both are resolved.
4105 * NOTE AFTER RELOCKING: The source or target ncp may have become
4106 * invalid while they were unlocked, nc_vp and nc_mount could
4109 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
4110 &tond->nl_nch, tond->nl_cred);
4111 fromnd->nl_flags |= NLC_NCPISLOCKED;
4114 * If the namecache generation changed for either fromnd or tond,
4117 if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
4118 tond->nl_nch.ncp->nc_generation != tncp_gen) {
4119 kprintf("kern_rename: retry due to gen on: "
4120 "\"%s\" -> \"%s\"\n",
4121 fromnd->nl_nch.ncp->nc_name,
4122 tond->nl_nch.ncp->nc_name);
4129 * If either fromnd or tond are marked destroyed a ripout occured
4130 * out from under us and we must retry.
4132 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
4133 fromnd->nl_nch.ncp->nc_vp == NULL ||
4134 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
4135 kprintf("kern_rename: retry due to ripout on: "
4136 "\"%s\" -> \"%s\"\n",
4137 fromnd->nl_nch.ncp->nc_name,
4138 tond->nl_nch.ncp->nc_name);
4145 * Make sure the parent directories linkages are the same.
4146 * XXX shouldn't be needed any more w/ generation check above.
4148 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
4149 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
4156 * Both the source and target must be within the same filesystem and
4157 * in the same filesystem as their parent directories within the
4158 * namecache topology.
4160 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4163 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
4164 mp != tond->nl_nch.mount) {
4171 * Make sure the mount point is writable
4173 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4180 * If the target exists and either the source or target is a directory,
4181 * then both must be directories.
4183 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4186 if (tond->nl_nch.ncp->nc_vp) {
4187 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4189 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4190 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4192 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4198 * You cannot rename a source into itself or a subdirectory of itself.
4199 * We check this by travsersing the target directory upwards looking
4200 * for a match against the source.
4205 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4206 if (fromnd->nl_nch.ncp == ncp) {
4217 * Even though the namespaces are different, they may still represent
4218 * hardlinks to the same file. The filesystem might have a hard time
4219 * with this so we issue a NREMOVE of the source instead of a NRENAME
4220 * when we detect the situation.
4223 fdvp = fromnd->nl_dvp;
4224 tdvp = tond->nl_dvp;
4225 if (fdvp == NULL || tdvp == NULL) {
4227 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4228 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4231 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4232 fdvp, tdvp, tond->nl_cred);
4239 * rename_args(char *from, char *to)
4241 * Rename files. Source and destination must either both be directories,
4242 * or both not be directories. If target is a directory, it must be empty.
4245 sys_rename(struct rename_args *uap)
4247 struct nlookupdata fromnd, tond;
4251 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4253 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4255 error = kern_rename(&fromnd, &tond);
4256 nlookup_done(&tond);
4258 nlookup_done(&fromnd);
4259 } while (error == EAGAIN);
4264 * renameat_args(int oldfd, char *old, int newfd, char *new)
4266 * Rename files using paths relative to the directories associated with
4267 * oldfd and newfd. Source and destination must either both be directories,
4268 * or both not be directories. If target is a directory, it must be empty.
4271 sys_renameat(struct renameat_args *uap)
4273 struct nlookupdata oldnd, newnd;
4274 struct file *oldfp, *newfp;
4278 error = nlookup_init_at(&oldnd, &oldfp,
4279 uap->oldfd, uap->old,
4282 error = nlookup_init_at(&newnd, &newfp,
4283 uap->newfd, uap->new,
4286 error = kern_rename(&oldnd, &newnd);
4287 nlookup_done_at(&newnd, newfp);
4289 nlookup_done_at(&oldnd, oldfp);
4290 } while (error == EAGAIN);
4295 kern_mkdir(struct nlookupdata *nd, int mode)
4297 struct thread *td = curthread;
4298 struct proc *p = td->td_proc;
4304 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4305 if ((error = nlookup(nd)) != 0)
4308 if (nd->nl_nch.ncp->nc_vp)
4310 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4313 vattr.va_type = VDIR;
4314 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4317 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4324 * mkdir_args(char *path, int mode)
4326 * Make a directory file.
4329 sys_mkdir(struct mkdir_args *uap)
4331 struct nlookupdata nd;
4334 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4336 error = kern_mkdir(&nd, uap->mode);
4342 * mkdirat_args(int fd, char *path, mode_t mode)
4344 * Make a directory file. The path is relative to the directory associated
4348 sys_mkdirat(struct mkdirat_args *uap)
4350 struct nlookupdata nd;
4354 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4356 error = kern_mkdir(&nd, uap->mode);
4357 nlookup_done_at(&nd, fp);
4362 kern_rmdir(struct nlookupdata *nd)
4367 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4368 if ((error = nlookup(nd)) != 0)
4372 * Do not allow directories representing mount points to be
4373 * deleted, even if empty. Check write perms on mount point
4374 * in case the vnode is aliased (aka nullfs).
4376 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4378 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4380 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4385 * rmdir_args(char *path)
4387 * Remove a directory file.
4390 sys_rmdir(struct rmdir_args *uap)
4392 struct nlookupdata nd;
4395 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4397 error = kern_rmdir(&nd);
4403 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4404 enum uio_seg direction)
4406 struct thread *td = curthread;
4414 if ((error = holdvnode(td, fd, &fp)) != 0)
4416 if ((fp->f_flag & FREAD) == 0) {
4420 vp = (struct vnode *)fp->f_data;
4421 if (vp->v_type != VDIR) {
4425 aiov.iov_base = buf;
4426 aiov.iov_len = count;
4427 auio.uio_iov = &aiov;
4428 auio.uio_iovcnt = 1;
4429 auio.uio_rw = UIO_READ;
4430 auio.uio_segflg = direction;
4432 auio.uio_resid = count;
4433 loff = auio.uio_offset = fp->f_offset;
4434 error = VOP_READDIR_FP(vp, &auio, fp->f_cred, &eofflag, NULL, NULL, fp);
4435 fp->f_offset = auio.uio_offset;
4440 * WARNING! *basep may not be wide enough to accomodate the
4441 * seek offset. XXX should we hack this to return the upper 32 bits
4442 * for offsets greater then 4G?
4445 *basep = (long)loff;
4447 *res = count - auio.uio_resid;
4454 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4456 * Read a block of directory entries in a file system independent format.
4459 sys_getdirentries(struct getdirentries_args *uap)
4464 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4465 &uap->sysmsg_result, UIO_USERSPACE);
4467 if (error == 0 && uap->basep)
4468 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4473 * getdents_args(int fd, char *buf, size_t count)
4476 sys_getdents(struct getdents_args *uap)
4480 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4481 &uap->sysmsg_result, UIO_USERSPACE);
4487 * Set the mode mask for creation of filesystem nodes.
4489 * umask(int newmask)
4492 sys_umask(struct umask_args *uap)
4494 struct thread *td = curthread;
4495 struct proc *p = td->td_proc;
4496 struct filedesc *fdp;
4499 uap->sysmsg_result = fdp->fd_cmask;
4500 fdp->fd_cmask = uap->newmask & ALLPERMS;
4505 * revoke(char *path)
4507 * Void all references to file by ripping underlying filesystem
4511 sys_revoke(struct revoke_args *uap)
4513 struct nlookupdata nd;
4520 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4522 error = nlookup(&nd);
4524 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4525 cred = crhold(nd.nl_cred);
4529 error = VOP_GETATTR(vp, &vattr);
4530 if (error == 0 && cred->cr_uid != vattr.va_uid)
4531 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4532 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4534 error = vrevoke(vp, cred);
4535 } else if (error == 0) {
4536 error = vrevoke(vp, cred);
4546 * getfh_args(char *fname, fhandle_t *fhp)
4548 * Get (NFS) file handle
4550 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4551 * mount. This allows nullfs mounts to be explicitly exported.
4553 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4555 * nullfs mounts of subdirectories are not safe. That is, it will
4556 * work, but you do not really have protection against access to
4557 * the related parent directories.
4560 sys_getfh(struct getfh_args *uap)
4562 struct thread *td = curthread;
4563 struct nlookupdata nd;
4570 * Must be super user
4572 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4576 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4578 error = nlookup(&nd);
4580 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4581 mp = nd.nl_nch.mount;
4584 bzero(&fh, sizeof(fh));
4585 fh.fh_fsid = mp->mnt_stat.f_fsid;
4586 error = VFS_VPTOFH(vp, &fh.fh_fid);
4589 error = copyout(&fh, uap->fhp, sizeof(fh));
4595 * fhopen_args(const struct fhandle *u_fhp, int flags)
4597 * syscall for the rpc.lockd to use to translate a NFS file handle into
4598 * an open descriptor.
4600 * warning: do not remove the priv_check() call or this becomes one giant
4604 sys_fhopen(struct fhopen_args *uap)
4606 struct thread *td = curthread;
4607 struct filedesc *fdp = td->td_proc->p_fd;
4612 struct vattr *vap = &vat;
4614 int fmode, mode, error = 0, type;
4620 * Must be super user
4622 error = priv_check(td, PRIV_ROOT);
4626 fmode = FFLAGS(uap->flags);
4629 * Why not allow a non-read/write open for our lockd?
4631 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4633 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4638 * Find the mount point
4640 mp = vfs_getvfs(&fhp.fh_fsid);
4645 /* now give me my vnode, it gets returned to me locked */
4646 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4650 * from now on we have to make sure not
4651 * to forget about the vnode
4652 * any error that causes an abort must vput(vp)
4653 * just set error = err and 'goto bad;'.
4659 if (vp->v_type == VLNK) {
4663 if (vp->v_type == VSOCK) {
4668 if (fmode & (FWRITE | O_TRUNC)) {
4669 if (vp->v_type == VDIR) {
4673 error = vn_writechk(vp, NULL);
4681 error = VOP_ACCESS(vp, mode, td->td_ucred);
4685 if (fmode & O_TRUNC) {
4686 vn_unlock(vp); /* XXX */
4687 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4690 error = VOP_SETATTR(vp, vap, td->td_ucred);
4696 * VOP_OPEN needs the file pointer so it can potentially override
4699 * WARNING! no f_nchandle will be associated when fhopen()ing a
4702 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4706 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4709 * setting f_ops this way prevents VOP_CLOSE from being
4710 * called or fdrop() releasing the vp from v_data. Since
4711 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4713 fp->f_ops = &badfileops;
4719 * The fp is given its own reference, we still have our ref and lock.
4721 * Assert that all regular files must be created with a VM object.
4723 if (vp->v_type == VREG && vp->v_object == NULL) {
4724 kprintf("fhopen: regular file did not "
4725 "have VM object: %p\n",
4731 * The open was successful. Handle any locking requirements.
4733 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4734 lf.l_whence = SEEK_SET;
4737 if (fmode & O_EXLOCK)
4738 lf.l_type = F_WRLCK;
4740 lf.l_type = F_RDLCK;
4741 if (fmode & FNONBLOCK)
4746 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK,
4749 * release our private reference.
4751 fsetfd(fdp, NULL, indx);
4756 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4757 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
4761 * Clean up. Associate the file pointer with the previously
4762 * reserved descriptor and return it.
4765 if (uap->flags & O_CLOEXEC)
4766 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4767 fsetfd(fdp, fp, indx);
4769 uap->sysmsg_result = indx;
4775 fsetfd(fdp, NULL, indx);
4786 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4789 sys_fhstat(struct fhstat_args *uap)
4791 struct thread *td = curthread;
4799 * Must be super user
4801 error = priv_check(td, PRIV_ROOT);
4805 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4809 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4812 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4813 error = vn_stat(vp, &sb, td->td_ucred);
4818 error = copyout(&sb, uap->sb, sizeof(sb));
4826 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4829 sys_fhstatfs(struct fhstatfs_args *uap)
4831 struct thread *td = curthread;
4832 struct proc *p = td->td_proc;
4837 char *fullpath, *freepath;
4842 * Must be super user
4844 if ((error = priv_check(td, PRIV_ROOT)))
4847 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4850 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4854 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4859 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4864 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4867 error = mount_path(p, mp, &fullpath, &freepath);
4870 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4871 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4872 kfree(freepath, M_TEMP);
4874 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4875 if (priv_check(td, PRIV_ROOT)) {
4876 bcopy(sp, &sb, sizeof(sb));
4877 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4880 error = copyout(sp, uap->buf, sizeof(*sp));
4889 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4892 sys_fhstatvfs(struct fhstatvfs_args *uap)
4894 struct thread *td = curthread;
4895 struct proc *p = td->td_proc;
4903 * Must be super user
4905 if ((error = priv_check(td, PRIV_ROOT)))
4908 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4911 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4915 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4920 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4923 sp = &mp->mnt_vstat;
4925 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4929 if (mp->mnt_flag & MNT_RDONLY)
4930 sp->f_flag |= ST_RDONLY;
4931 if (mp->mnt_flag & MNT_NOSUID)
4932 sp->f_flag |= ST_NOSUID;
4933 error = copyout(sp, uap->buf, sizeof(*sp));
4942 * Syscall to push extended attribute configuration information into the
4943 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4944 * a command (int cmd), and attribute name and misc data. For now, the
4945 * attribute name is left in userspace for consumption by the VFS_op.
4946 * It will probably be changed to be copied into sysspace by the
4947 * syscall in the future, once issues with various consumers of the
4948 * attribute code have raised their hands.
4950 * Currently this is used only by UFS Extended Attributes.
4953 sys_extattrctl(struct extattrctl_args *uap)
4955 struct nlookupdata nd;
4957 char attrname[EXTATTR_MAXNAMELEN];
4965 if (error == 0 && uap->filename) {
4966 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4969 error = nlookup(&nd);
4971 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4975 if (error == 0 && uap->attrname) {
4976 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4981 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4983 error = nlookup(&nd);
4985 error = ncp_writechk(&nd.nl_nch);
4987 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4989 uap->attrname, nd.nl_cred);
4998 * Syscall to get a named extended attribute on a file or directory.
5001 sys_extattr_set_file(struct extattr_set_file_args *uap)
5003 char attrname[EXTATTR_MAXNAMELEN];
5004 struct nlookupdata nd;
5010 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5016 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5018 error = nlookup(&nd);
5020 error = ncp_writechk(&nd.nl_nch);
5022 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5028 bzero(&auio, sizeof(auio));
5029 aiov.iov_base = uap->data;
5030 aiov.iov_len = uap->nbytes;
5031 auio.uio_iov = &aiov;
5032 auio.uio_iovcnt = 1;
5033 auio.uio_offset = 0;
5034 auio.uio_resid = uap->nbytes;
5035 auio.uio_rw = UIO_WRITE;
5036 auio.uio_td = curthread;
5038 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
5047 * Syscall to get a named extended attribute on a file or directory.
5050 sys_extattr_get_file(struct extattr_get_file_args *uap)
5052 char attrname[EXTATTR_MAXNAMELEN];
5053 struct nlookupdata nd;
5059 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5065 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5067 error = nlookup(&nd);
5069 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
5075 bzero(&auio, sizeof(auio));
5076 aiov.iov_base = uap->data;
5077 aiov.iov_len = uap->nbytes;
5078 auio.uio_iov = &aiov;
5079 auio.uio_iovcnt = 1;
5080 auio.uio_offset = 0;
5081 auio.uio_resid = uap->nbytes;
5082 auio.uio_rw = UIO_READ;
5083 auio.uio_td = curthread;
5085 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
5087 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
5095 * Syscall to delete a named extended attribute from a file or directory.
5096 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
5099 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
5101 char attrname[EXTATTR_MAXNAMELEN];
5102 struct nlookupdata nd;
5106 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5110 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5112 error = nlookup(&nd);
5114 error = ncp_writechk(&nd.nl_nch);
5116 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5118 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
5119 attrname, NULL, nd.nl_cred);
5128 * Determine if the mount is visible to the process.
5131 chroot_visible_mnt(struct mount *mp, struct proc *p)
5133 struct nchandle nch;
5136 * Traverse from the mount point upwards. If we hit the process
5137 * root then the mount point is visible to the process.
5139 nch = mp->mnt_ncmountpt;
5141 if (nch.mount == p->p_fd->fd_nrdir.mount &&
5142 nch.ncp == p->p_fd->fd_nrdir.ncp) {
5145 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
5146 nch = nch.mount->mnt_ncmounton;
5148 nch.ncp = nch.ncp->nc_parent;
5153 * If the mount point is not visible to the process, but the
5154 * process root is in a subdirectory of the mount, return
5157 if (p->p_fd->fd_nrdir.mount == mp)