2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
35 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/sysent.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/mountctl.h>
46 #include <sys/sysproto.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
49 #include <sys/fcntl.h>
51 #include <sys/linker.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
58 #include <sys/namei.h>
59 #include <sys/nlookup.h>
60 #include <sys/dirent.h>
61 #include <sys/extattr.h>
62 #include <sys/spinlock.h>
63 #include <sys/kern_syscall.h>
64 #include <sys/objcache.h>
65 #include <sys/sysctl.h>
68 #include <sys/file2.h>
69 #include <sys/spinlock2.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
75 #include <machine/limits.h>
76 #include <machine/stdarg.h>
78 static void mount_warning(struct mount *mp, const char *ctl, ...)
80 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
81 static int checkvp_chdir (struct vnode *vn, struct thread *td);
82 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
83 static int chroot_refuse_vdir_fds (thread_t td, struct filedesc *fdp);
84 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
85 static int getutimes (struct timeval *, struct timespec *);
86 static int getutimens (const struct timespec *, struct timespec *, int *);
87 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
88 static int setfmode (struct vnode *, int);
89 static int setfflags (struct vnode *, int);
90 static int setutimes (struct vnode *, struct vattr *,
91 const struct timespec *, int);
93 static int usermount = 0; /* if 1, non-root can mount fs. */
94 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
95 "Allow non-root users to mount filesystems");
97 static int debug_unmount = 0; /* if 1 loop until unmount success */
98 SYSCTL_INT(_vfs, OID_AUTO, debug_unmount, CTLFLAG_RW, &debug_unmount, 0,
99 "Stall failed unmounts in loop");
101 * Virtual File System System Calls
105 * Mount a file system.
107 * mount_args(char *type, char *path, int flags, caddr_t data)
112 sys_mount(struct mount_args *uap)
114 struct thread *td = curthread;
117 struct mount *mp, *nullmp;
118 struct vfsconf *vfsp;
119 int error, flag = 0, flag2 = 0;
122 struct nlookupdata nd;
123 char fstypename[MFSNAMELEN];
131 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
135 * Do not allow NFS export by non-root users.
137 if (uap->flags & MNT_EXPORTED) {
138 error = priv_check(td, PRIV_ROOT);
143 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
145 if (priv_check(td, PRIV_ROOT))
146 uap->flags |= MNT_NOSUID | MNT_NODEV;
149 * Lookup the requested path and extract the nch and vnode.
151 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
153 if ((error = nlookup(&nd)) == 0) {
154 if (nd.nl_nch.ncp->nc_vp == NULL)
164 * If the target filesystem is resolved via a nullfs mount, then
165 * nd.nl_nch.mount will be pointing to the nullfs mount structure
166 * instead of the target file system. We need it in case we are
169 nullmp = nd.nl_nch.mount;
172 * Extract the locked+refd ncp and cleanup the nd structure
175 cache_zero(&nd.nl_nch);
178 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
179 (mp = cache_findmount(&nch)) != NULL) {
188 * now we have the locked ref'd nch and unreferenced vnode.
191 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
198 * Extract the file system type. We need to know this early, to take
199 * appropriate actions if we are dealing with a nullfs.
201 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
208 * Now we have an unlocked ref'd nch and a locked ref'd vp
210 if (uap->flags & MNT_UPDATE) {
211 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
218 if (strncmp(fstypename, "null", 5) == 0) {
226 flag2 = mp->mnt_kern_flag;
228 * We only allow the filesystem to be reloaded if it
229 * is currently mounted read-only.
231 if ((uap->flags & MNT_RELOAD) &&
232 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
235 error = EOPNOTSUPP; /* Needs translation */
239 * Only root, or the user that did the original mount is
240 * permitted to update it.
242 if (mp->mnt_stat.f_owner != cred->cr_uid &&
243 (error = priv_check(td, PRIV_ROOT))) {
248 if (vfs_busy(mp, LK_NOWAIT)) {
262 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
263 lwkt_gettoken(&mp->mnt_token);
270 * If the user is not root, ensure that they own the directory
271 * onto which we are attempting to mount.
273 if ((error = VOP_GETATTR(vp, &va)) ||
274 (va.va_uid != cred->cr_uid &&
275 (error = priv_check(td, PRIV_ROOT)))) {
280 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
285 if (vp->v_type != VDIR) {
291 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
297 vfsp = vfsconf_find_by_name(fstypename);
301 /* Only load modules for root (very important!) */
302 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
307 error = linker_load_file(fstypename, &lf);
308 if (error || lf == NULL) {
316 /* lookup again, see if the VFS was loaded */
317 vfsp = vfsconf_find_by_name(fstypename);
320 linker_file_unload(lf);
335 * Allocate and initialize the filesystem.
337 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
339 vfs_busy(mp, LK_NOWAIT);
340 mp->mnt_op = vfsp->vfc_vfsops;
342 mp->mnt_pbuf_count = nswbuf_kva / NSWBUF_SPLIT;
343 vfsp->vfc_refcount++;
344 mp->mnt_stat.f_type = vfsp->vfc_typenum;
345 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
346 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
347 mp->mnt_stat.f_owner = cred->cr_uid;
348 lwkt_gettoken(&mp->mnt_token);
352 * (per-mount token acquired at this point)
354 * Set the mount level flags.
356 if (uap->flags & MNT_RDONLY)
357 mp->mnt_flag |= MNT_RDONLY;
358 else if (mp->mnt_flag & MNT_RDONLY)
359 mp->mnt_kern_flag |= MNTK_WANTRDWR;
360 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
361 MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME |
362 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
363 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
365 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
366 MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE |
367 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
368 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
372 * Pre-set the mount's ALL_MPSAFE flags if specified in the vfsconf.
373 * This way the initial VFS_MOUNT() call will also be MPSAFE.
375 if (vfsp->vfc_flags & VFCF_MPSAFE)
376 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
379 * Mount the filesystem.
380 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
383 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 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1999 vfs_cache_setroot(vp, cache_hold(nch));
2008 * Common routine for chroot and chdir. Given a locked, referenced vnode,
2009 * determine whether it is legal to chdir to the vnode. The vnode's state
2010 * is not changed by this call.
2013 checkvp_chdir(struct vnode *vp, struct thread *td)
2017 if (vp->v_type != VDIR)
2020 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
2025 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
2027 struct thread *td = curthread;
2028 struct proc *p = td->td_proc;
2029 struct lwp *lp = td->td_lwp;
2030 struct filedesc *fdp = p->p_fd;
2035 int type, indx, error = 0;
2038 if ((oflags & O_ACCMODE) == O_ACCMODE)
2040 flags = FFLAGS(oflags);
2041 error = falloc(lp, &nfp, NULL);
2045 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
2048 * XXX p_dupfd is a real mess. It allows a device to return a
2049 * file descriptor to be duplicated rather then doing the open
2055 * Call vn_open() to do the lookup and assign the vnode to the
2056 * file pointer. vn_open() does not change the ref count on fp
2057 * and the vnode, on success, will be inherited by the file pointer
2060 * Request a shared lock on the vnode if possible.
2062 * Executable binaries can race VTEXT against O_RDWR opens, so
2063 * use an exclusive lock for O_RDWR opens as well.
2065 * NOTE: We need a flag to separate terminal vnode locking from
2066 * parent locking. O_CREAT needs parent locking, but O_TRUNC
2067 * and O_RDWR only need to lock the terminal vnode exclusively.
2069 nd->nl_flags |= NLC_LOCKVP;
2070 if ((flags & (O_CREAT|O_TRUNC|O_RDWR)) == 0)
2071 nd->nl_flags |= NLC_SHAREDLOCK;
2073 error = vn_open(nd, fp, flags, cmode);
2078 * handle special fdopen() case. bleh. dupfdopen() is
2079 * responsible for dropping the old contents of ofiles[indx]
2082 * Note that fsetfd() will add a ref to fp which represents
2083 * the fd_files[] assignment. We must still drop our
2086 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
2087 if (fdalloc(p, 0, &indx) == 0) {
2088 error = dupfdopen(td, indx, lp->lwp_dupfd, flags, error);
2091 fdrop(fp); /* our ref */
2094 fsetfd(fdp, NULL, indx);
2097 fdrop(fp); /* our ref */
2098 if (error == ERESTART)
2104 * ref the vnode for ourselves so it can't be ripped out from under
2105 * is. XXX need an ND flag to request that the vnode be returned
2108 * Reserve a file descriptor but do not assign it until the open
2111 vp = (struct vnode *)fp->f_data;
2113 if ((error = fdalloc(p, 0, &indx)) != 0) {
2120 * If no error occurs the vp will have been assigned to the file
2125 if (flags & (O_EXLOCK | O_SHLOCK)) {
2126 lf.l_whence = SEEK_SET;
2129 if (flags & O_EXLOCK)
2130 lf.l_type = F_WRLCK;
2132 lf.l_type = F_RDLCK;
2133 if (flags & FNONBLOCK)
2138 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2140 * lock request failed. Clean up the reserved
2144 fsetfd(fdp, NULL, indx);
2148 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
2152 * Assert that all regular file vnodes were created with a object.
2154 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
2155 ("open: regular file has no backing object after vn_open"));
2161 * release our private reference, leaving the one associated with the
2162 * descriptor table intact.
2164 if (oflags & O_CLOEXEC)
2165 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
2166 fsetfd(fdp, fp, indx);
2174 * open_args(char *path, int flags, int mode)
2176 * Check permissions, allocate an open file structure,
2177 * and call the device open routine if any.
2180 sys_open(struct open_args *uap)
2182 struct nlookupdata nd;
2185 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2187 error = kern_open(&nd, uap->flags,
2188 uap->mode, &uap->sysmsg_result);
2195 * openat_args(int fd, char *path, int flags, int mode)
2198 sys_openat(struct openat_args *uap)
2200 struct nlookupdata nd;
2204 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2206 error = kern_open(&nd, uap->flags, uap->mode,
2207 &uap->sysmsg_result);
2209 nlookup_done_at(&nd, fp);
2214 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2216 struct thread *td = curthread;
2217 struct proc *p = td->td_proc;
2226 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2227 vattr.va_rmajor = rmajor;
2228 vattr.va_rminor = rminor;
2230 switch (mode & S_IFMT) {
2231 case S_IFMT: /* used by badsect to flag bad sectors */
2232 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2233 vattr.va_type = VBAD;
2236 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2237 vattr.va_type = VCHR;
2240 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2241 vattr.va_type = VBLK;
2244 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2247 case S_IFDIR: /* special directories support for HAMMER */
2248 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2249 vattr.va_type = VDIR;
2260 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2261 if ((error = nlookup(nd)) != 0)
2263 if (nd->nl_nch.ncp->nc_vp)
2265 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2269 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2270 nd->nl_cred, NAMEI_CREATE);
2273 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2274 &vp, nd->nl_cred, &vattr);
2282 * mknod_args(char *path, int mode, int dev)
2284 * Create a special file.
2287 sys_mknod(struct mknod_args *uap)
2289 struct nlookupdata nd;
2292 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2294 error = kern_mknod(&nd, uap->mode,
2295 umajor(uap->dev), uminor(uap->dev));
2302 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2304 * Create a special file. The path is relative to the directory associated
2308 sys_mknodat(struct mknodat_args *uap)
2310 struct nlookupdata nd;
2314 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2316 error = kern_mknod(&nd, uap->mode,
2317 umajor(uap->dev), uminor(uap->dev));
2319 nlookup_done_at(&nd, fp);
2324 kern_mkfifo(struct nlookupdata *nd, int mode)
2326 struct thread *td = curthread;
2327 struct proc *p = td->td_proc;
2334 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2335 if ((error = nlookup(nd)) != 0)
2337 if (nd->nl_nch.ncp->nc_vp)
2339 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2343 vattr.va_type = VFIFO;
2344 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2346 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2353 * mkfifo_args(char *path, int mode)
2355 * Create a named pipe.
2358 sys_mkfifo(struct mkfifo_args *uap)
2360 struct nlookupdata nd;
2363 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2365 error = kern_mkfifo(&nd, uap->mode);
2371 * mkfifoat_args(int fd, char *path, mode_t mode)
2373 * Create a named pipe. The path is relative to the directory associated
2377 sys_mkfifoat(struct mkfifoat_args *uap)
2379 struct nlookupdata nd;
2383 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2385 error = kern_mkfifo(&nd, uap->mode);
2386 nlookup_done_at(&nd, fp);
2390 static int hardlink_check_uid = 0;
2391 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2392 &hardlink_check_uid, 0,
2393 "Unprivileged processes cannot create hard links to files owned by other "
2395 static int hardlink_check_gid = 0;
2396 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2397 &hardlink_check_gid, 0,
2398 "Unprivileged processes cannot create hard links to files owned by other "
2402 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2408 * Shortcut if disabled
2410 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2414 * Privileged user can always hardlink
2416 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2420 * Otherwise only if the originating file is owned by the
2421 * same user or group. Note that any group is allowed if
2422 * the file is owned by the caller.
2424 error = VOP_GETATTR(vp, &va);
2428 if (hardlink_check_uid) {
2429 if (cred->cr_uid != va.va_uid)
2433 if (hardlink_check_gid) {
2434 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2442 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2444 struct thread *td = curthread;
2449 * Lookup the source and obtained a locked vnode.
2451 * You may only hardlink a file which you have write permission
2452 * on or which you own.
2454 * XXX relookup on vget failure / race ?
2457 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2458 if ((error = nlookup(nd)) != 0)
2460 vp = nd->nl_nch.ncp->nc_vp;
2461 KKASSERT(vp != NULL);
2462 if (vp->v_type == VDIR)
2463 return (EPERM); /* POSIX */
2464 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2466 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2470 * Unlock the source so we can lookup the target without deadlocking
2471 * (XXX vp is locked already, possible other deadlock?). The target
2474 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2475 nd->nl_flags &= ~NLC_NCPISLOCKED;
2476 cache_unlock(&nd->nl_nch);
2479 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2480 if ((error = nlookup(linknd)) != 0) {
2484 if (linknd->nl_nch.ncp->nc_vp) {
2488 VFS_MODIFYING(vp->v_mount);
2489 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2496 * Finally run the new API VOP.
2498 error = can_hardlink(vp, td, td->td_ucred);
2500 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2501 vp, linknd->nl_cred);
2508 * link_args(char *path, char *link)
2510 * Make a hard file link.
2513 sys_link(struct link_args *uap)
2515 struct nlookupdata nd, linknd;
2518 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2520 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2522 error = kern_link(&nd, &linknd);
2523 nlookup_done(&linknd);
2530 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2532 * Make a hard file link. The path1 argument is relative to the directory
2533 * associated with fd1, and similarly the path2 argument is relative to
2534 * the directory associated with fd2.
2537 sys_linkat(struct linkat_args *uap)
2539 struct nlookupdata nd, linknd;
2540 struct file *fp1, *fp2;
2543 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2544 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2546 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2547 uap->path2, UIO_USERSPACE, 0);
2549 error = kern_link(&nd, &linknd);
2550 nlookup_done_at(&linknd, fp2);
2552 nlookup_done_at(&nd, fp1);
2557 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2565 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2566 if ((error = nlookup(nd)) != 0)
2568 if (nd->nl_nch.ncp->nc_vp)
2570 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2574 vattr.va_mode = mode;
2575 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2582 * symlink(char *path, char *link)
2584 * Make a symbolic link.
2587 sys_symlink(struct symlink_args *uap)
2589 struct thread *td = curthread;
2590 struct nlookupdata nd;
2595 path = objcache_get(namei_oc, M_WAITOK);
2596 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2598 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2600 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2601 error = kern_symlink(&nd, path, mode);
2605 objcache_put(namei_oc, path);
2610 * symlinkat_args(char *path1, int fd, char *path2)
2612 * Make a symbolic link. The path2 argument is relative to the directory
2613 * associated with fd.
2616 sys_symlinkat(struct symlinkat_args *uap)
2618 struct thread *td = curthread;
2619 struct nlookupdata nd;
2625 path1 = objcache_get(namei_oc, M_WAITOK);
2626 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2628 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2631 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2632 error = kern_symlink(&nd, path1, mode);
2634 nlookup_done_at(&nd, fp);
2636 objcache_put(namei_oc, path1);
2641 * undelete_args(char *path)
2643 * Delete a whiteout from the filesystem.
2646 sys_undelete(struct undelete_args *uap)
2648 struct nlookupdata nd;
2651 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2653 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2655 error = nlookup(&nd);
2657 error = ncp_writechk(&nd.nl_nch);
2659 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2667 kern_unlink(struct nlookupdata *nd)
2672 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2673 if ((error = nlookup(nd)) != 0)
2675 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2677 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2682 * unlink_args(char *path)
2684 * Delete a name from the filesystem.
2687 sys_unlink(struct unlink_args *uap)
2689 struct nlookupdata nd;
2692 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2694 error = kern_unlink(&nd);
2701 * unlinkat_args(int fd, char *path, int flags)
2703 * Delete the file or directory entry pointed to by fd/path.
2706 sys_unlinkat(struct unlinkat_args *uap)
2708 struct nlookupdata nd;
2712 if (uap->flags & ~AT_REMOVEDIR)
2715 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2717 if (uap->flags & AT_REMOVEDIR)
2718 error = kern_rmdir(&nd);
2720 error = kern_unlink(&nd);
2722 nlookup_done_at(&nd, fp);
2727 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2729 struct thread *td = curthread;
2736 fp = holdfp(td, fd, -1);
2739 if (fp->f_type != DTYPE_VNODE) {
2743 vp = (struct vnode *)fp->f_data;
2747 spin_lock(&fp->f_spin);
2748 new_offset = fp->f_offset + offset;
2752 error = VOP_GETATTR_FP(vp, &vattr, fp);
2753 spin_lock(&fp->f_spin);
2754 new_offset = offset + vattr.va_size;
2757 new_offset = offset;
2759 spin_lock(&fp->f_spin);
2764 spin_lock(&fp->f_spin);
2769 * Validate the seek position. Negative offsets are not allowed
2770 * for regular files or directories.
2772 * Normally we would also not want to allow negative offsets for
2773 * character and block-special devices. However kvm addresses
2774 * on 64 bit architectures might appear to be negative and must
2778 if (new_offset < 0 &&
2779 (vp->v_type == VREG || vp->v_type == VDIR)) {
2782 fp->f_offset = new_offset;
2785 *res = fp->f_offset;
2786 spin_unlock(&fp->f_spin);
2794 * lseek_args(int fd, int pad, off_t offset, int whence)
2796 * Reposition read/write file offset.
2799 sys_lseek(struct lseek_args *uap)
2803 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2804 &uap->sysmsg_offset);
2810 * Check if current process can access given file. amode is a bitmask of *_OK
2811 * access bits. flags is a bitmask of AT_* flags.
2814 kern_access(struct nlookupdata *nd, int amode, int flags)
2819 if (flags & ~AT_EACCESS)
2821 nd->nl_flags |= NLC_SHAREDLOCK;
2822 if ((error = nlookup(nd)) != 0)
2825 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2829 /* Flags == 0 means only check for existence. */
2838 if ((mode & VWRITE) == 0 ||
2839 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2840 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2843 * If the file handle is stale we have to re-resolve the
2844 * entry with the ncp held exclusively. This is a hack
2847 if (error == ESTALE) {
2849 cache_unlock(&nd->nl_nch);
2850 cache_lock(&nd->nl_nch);
2851 cache_setunresolved(&nd->nl_nch);
2852 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2865 * access_args(char *path, int flags)
2867 * Check access permissions.
2870 sys_access(struct access_args *uap)
2872 struct nlookupdata nd;
2875 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2877 error = kern_access(&nd, uap->flags, 0);
2884 * eaccess_args(char *path, int flags)
2886 * Check access permissions.
2889 sys_eaccess(struct eaccess_args *uap)
2891 struct nlookupdata nd;
2894 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2896 error = kern_access(&nd, uap->flags, AT_EACCESS);
2903 * faccessat_args(int fd, char *path, int amode, int flags)
2905 * Check access permissions.
2908 sys_faccessat(struct faccessat_args *uap)
2910 struct nlookupdata nd;
2914 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2917 error = kern_access(&nd, uap->amode, uap->flags);
2918 nlookup_done_at(&nd, fp);
2923 kern_stat(struct nlookupdata *nd, struct stat *st)
2928 nd->nl_flags |= NLC_SHAREDLOCK;
2929 if ((error = nlookup(nd)) != 0)
2932 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2935 if ((error = vget(vp, LK_SHARED)) != 0)
2937 error = vn_stat(vp, st, nd->nl_cred);
2940 * If the file handle is stale we have to re-resolve the
2941 * entry with the ncp held exclusively. This is a hack
2944 if (error == ESTALE) {
2946 cache_unlock(&nd->nl_nch);
2947 cache_lock(&nd->nl_nch);
2948 cache_setunresolved(&nd->nl_nch);
2949 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2959 * stat_args(char *path, struct stat *ub)
2961 * Get file status; this version follows links.
2964 sys_stat(struct stat_args *uap)
2966 struct nlookupdata nd;
2970 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2972 error = kern_stat(&nd, &st);
2974 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2981 * lstat_args(char *path, struct stat *ub)
2983 * Get file status; this version does not follow links.
2986 sys_lstat(struct lstat_args *uap)
2988 struct nlookupdata nd;
2992 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2994 error = kern_stat(&nd, &st);
2996 error = copyout(&st, uap->ub, sizeof(*uap->ub));
3003 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
3005 * Get status of file pointed to by fd/path.
3008 sys_fstatat(struct fstatat_args *uap)
3010 struct nlookupdata nd;
3016 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3019 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3021 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3022 UIO_USERSPACE, flags);
3024 error = kern_stat(&nd, &st);
3026 error = copyout(&st, uap->sb, sizeof(*uap->sb));
3028 nlookup_done_at(&nd, fp);
3033 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
3035 struct nlookupdata nd;
3040 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
3042 error = nlookup(&nd);
3044 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3047 error = VOP_PATHCONF(vp, name, sysmsg_regp);
3054 * pathconf_Args(char *path, int name)
3056 * Get configurable pathname variables.
3059 sys_pathconf(struct pathconf_args *uap)
3061 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
3066 * lpathconf_Args(char *path, int name)
3068 * Get configurable pathname variables, but don't follow symlinks.
3071 sys_lpathconf(struct lpathconf_args *uap)
3073 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
3078 * kern_readlink isn't properly split yet. There is a copyin burried
3079 * in VOP_READLINK().
3082 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
3084 struct thread *td = curthread;
3090 nd->nl_flags |= NLC_SHAREDLOCK;
3091 if ((error = nlookup(nd)) != 0)
3093 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
3096 if (vp->v_type != VLNK) {
3099 aiov.iov_base = buf;
3100 aiov.iov_len = count;
3101 auio.uio_iov = &aiov;
3102 auio.uio_iovcnt = 1;
3103 auio.uio_offset = 0;
3104 auio.uio_rw = UIO_READ;
3105 auio.uio_segflg = UIO_USERSPACE;
3107 auio.uio_resid = count;
3108 error = VOP_READLINK(vp, &auio, td->td_ucred);
3111 *res = count - auio.uio_resid;
3116 * readlink_args(char *path, char *buf, int count)
3118 * Return target name of a symbolic link.
3121 sys_readlink(struct readlink_args *uap)
3123 struct nlookupdata nd;
3126 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3128 error = kern_readlink(&nd, uap->buf, uap->count,
3129 &uap->sysmsg_result);
3136 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3138 * Return target name of a symbolic link. The path is relative to the
3139 * directory associated with fd.
3142 sys_readlinkat(struct readlinkat_args *uap)
3144 struct nlookupdata nd;
3148 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3150 error = kern_readlink(&nd, uap->buf, uap->bufsize,
3151 &uap->sysmsg_result);
3153 nlookup_done_at(&nd, fp);
3158 setfflags(struct vnode *vp, int flags)
3160 struct thread *td = curthread;
3165 * Prevent non-root users from setting flags on devices. When
3166 * a device is reused, users can retain ownership of the device
3167 * if they are allowed to set flags and programs assume that
3168 * chown can't fail when done as root.
3170 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
3171 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
3175 * note: vget is required for any operation that might mod the vnode
3176 * so VINACTIVE is properly cleared.
3178 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3180 vattr.va_flags = flags;
3181 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3188 * chflags(char *path, int flags)
3190 * Change flags of a file given a path name.
3193 sys_chflags(struct chflags_args *uap)
3195 struct nlookupdata nd;
3200 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3202 error = nlookup(&nd);
3204 error = ncp_writechk(&nd.nl_nch);
3206 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3209 error = setfflags(vp, uap->flags);
3216 * lchflags(char *path, int flags)
3218 * Change flags of a file given a path name, but don't follow symlinks.
3221 sys_lchflags(struct lchflags_args *uap)
3223 struct nlookupdata nd;
3228 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3230 error = nlookup(&nd);
3232 error = ncp_writechk(&nd.nl_nch);
3234 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3237 error = setfflags(vp, uap->flags);
3244 * fchflags_args(int fd, int flags)
3246 * Change flags of a file given a file descriptor.
3249 sys_fchflags(struct fchflags_args *uap)
3251 struct thread *td = curthread;
3255 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3257 if (fp->f_nchandle.ncp)
3258 error = ncp_writechk(&fp->f_nchandle);
3260 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3266 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3267 * change flags given a pathname relative to a filedescriptor
3269 int sys_chflagsat(struct chflagsat_args *uap)
3271 struct nlookupdata nd;
3277 if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3280 lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3283 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags);
3285 error = nlookup(&nd);
3287 error = ncp_writechk(&nd.nl_nch);
3289 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3290 nlookup_done_at(&nd, fp);
3292 error = setfflags(vp, uap->flags);
3300 setfmode(struct vnode *vp, int mode)
3302 struct thread *td = curthread;
3307 * note: vget is required for any operation that might mod the vnode
3308 * so VINACTIVE is properly cleared.
3310 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3312 vattr.va_mode = mode & ALLPERMS;
3313 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3314 cache_inval_wxok(vp);
3321 kern_chmod(struct nlookupdata *nd, int mode)
3326 if ((error = nlookup(nd)) != 0)
3328 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3330 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3331 error = setfmode(vp, mode);
3337 * chmod_args(char *path, int mode)
3339 * Change mode of a file given path name.
3342 sys_chmod(struct chmod_args *uap)
3344 struct nlookupdata nd;
3347 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3349 error = kern_chmod(&nd, uap->mode);
3355 * lchmod_args(char *path, int mode)
3357 * Change mode of a file given path name (don't follow links.)
3360 sys_lchmod(struct lchmod_args *uap)
3362 struct nlookupdata nd;
3365 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3367 error = kern_chmod(&nd, uap->mode);
3373 * fchmod_args(int fd, int mode)
3375 * Change mode of a file given a file descriptor.
3378 sys_fchmod(struct fchmod_args *uap)
3380 struct thread *td = curthread;
3384 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3386 if (fp->f_nchandle.ncp)
3387 error = ncp_writechk(&fp->f_nchandle);
3389 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3395 * fchmodat_args(char *path, int mode)
3397 * Change mode of a file pointed to by fd/path.
3400 sys_fchmodat(struct fchmodat_args *uap)
3402 struct nlookupdata nd;
3407 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3409 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3411 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3412 UIO_USERSPACE, flags);
3414 error = kern_chmod(&nd, uap->mode);
3415 nlookup_done_at(&nd, fp);
3420 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3422 struct thread *td = curthread;
3430 * note: vget is required for any operation that might mod the vnode
3431 * so VINACTIVE is properly cleared.
3433 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3434 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3436 o_uid = vattr.va_uid;
3437 o_gid = vattr.va_gid;
3438 size = vattr.va_size;
3443 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3452 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3453 VFS_ACCOUNT(mp, uid, gid, size);
3460 kern_chown(struct nlookupdata *nd, int uid, int gid)
3465 if ((error = nlookup(nd)) != 0)
3467 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3469 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3470 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3476 * chown(char *path, int uid, int gid)
3478 * Set ownership given a path name.
3481 sys_chown(struct chown_args *uap)
3483 struct nlookupdata nd;
3486 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3488 error = kern_chown(&nd, uap->uid, uap->gid);
3494 * lchown_args(char *path, int uid, int gid)
3496 * Set ownership given a path name, do not cross symlinks.
3499 sys_lchown(struct lchown_args *uap)
3501 struct nlookupdata nd;
3504 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3506 error = kern_chown(&nd, uap->uid, uap->gid);
3512 * fchown_args(int fd, int uid, int gid)
3514 * Set ownership given a file descriptor.
3517 sys_fchown(struct fchown_args *uap)
3519 struct thread *td = curthread;
3520 struct proc *p = td->td_proc;
3524 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3526 if (fp->f_nchandle.ncp)
3527 error = ncp_writechk(&fp->f_nchandle);
3529 error = setfown(p->p_fd->fd_ncdir.mount,
3530 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3536 * fchownat(int fd, char *path, int uid, int gid, int flags)
3538 * Set ownership of file pointed to by fd/path.
3541 sys_fchownat(struct fchownat_args *uap)
3543 struct nlookupdata nd;
3548 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3550 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3552 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3553 UIO_USERSPACE, flags);
3555 error = kern_chown(&nd, uap->uid, uap->gid);
3556 nlookup_done_at(&nd, fp);
3562 getutimes(struct timeval *tvp, struct timespec *tsp)
3564 struct timeval tv[2];
3569 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3572 if ((error = itimerfix(tvp)) != 0)
3574 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3575 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3581 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3583 struct timespec tsnow;
3597 if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3599 if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3602 if (newts[0].tv_nsec == UTIME_OMIT)
3603 newts[0].tv_sec = VNOVAL;
3604 else if (newts[0].tv_nsec == UTIME_NOW)
3606 else if ((error = itimespecfix(&newts[0])) != 0)
3609 if (newts[1].tv_nsec == UTIME_OMIT)
3610 newts[1].tv_sec = VNOVAL;
3611 else if (newts[1].tv_nsec == UTIME_NOW)
3613 else if ((error = itimespecfix(&newts[1])) != 0)
3620 setutimes(struct vnode *vp, struct vattr *vattr,
3621 const struct timespec *ts, int nullflag)
3623 struct thread *td = curthread;
3627 vattr->va_atime = ts[0];
3628 vattr->va_mtime = ts[1];
3630 vattr->va_vaflags |= VA_UTIMES_NULL;
3631 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3637 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3639 struct timespec ts[2];
3643 if ((error = getutimes(tptr, ts)) != 0)
3646 error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3651 * utimes_args(char *path, struct timeval *tptr)
3653 * Set the access and modification times of a file.
3656 sys_utimes(struct utimes_args *uap)
3658 struct timeval tv[2];
3659 struct nlookupdata nd;
3663 error = copyin(uap->tptr, tv, sizeof(tv));
3667 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3669 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3675 * lutimes_args(char *path, struct timeval *tptr)
3677 * Set the access and modification times of a file.
3680 sys_lutimes(struct lutimes_args *uap)
3682 struct timeval tv[2];
3683 struct nlookupdata nd;
3687 error = copyin(uap->tptr, tv, sizeof(tv));
3691 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3693 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3699 * Set utimes on a file descriptor. The creds used to open the
3700 * file are used to determine whether the operation is allowed
3704 kern_futimens(int fd, struct timespec *ts)
3706 struct thread *td = curthread;
3707 struct timespec newts[2];
3714 error = getutimens(ts, newts, &nullflag);
3717 if ((error = holdvnode(td, fd, &fp)) != 0)
3719 if (fp->f_nchandle.ncp)
3720 error = ncp_writechk(&fp->f_nchandle);
3723 error = vget(vp, LK_EXCLUSIVE);
3725 error = VOP_GETATTR_FP(vp, &vattr, fp);
3727 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3731 error = setutimes(vp, &vattr, newts, nullflag);
3741 * futimens_args(int fd, struct timespec *ts)
3743 * Set the access and modification times of a file.
3746 sys_futimens(struct futimens_args *uap)
3748 struct timespec ts[2];
3752 error = copyin(uap->ts, ts, sizeof(ts));
3756 error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3761 kern_futimes(int fd, struct timeval *tptr)
3763 struct timespec ts[2];
3767 if ((error = getutimes(tptr, ts)) != 0)
3770 error = kern_futimens(fd, tptr ? ts : NULL);
3775 * futimes_args(int fd, struct timeval *tptr)
3777 * Set the access and modification times of a file.
3780 sys_futimes(struct futimes_args *uap)
3782 struct timeval tv[2];
3786 error = copyin(uap->tptr, tv, sizeof(tv));
3790 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3795 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3797 struct timespec newts[2];
3803 if (flags & ~AT_SYMLINK_NOFOLLOW)
3806 error = getutimens(ts, newts, &nullflag);
3810 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3811 if ((error = nlookup(nd)) != 0)
3813 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3815 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3817 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3818 error = vget(vp, LK_EXCLUSIVE);
3820 error = setutimes(vp, &vattr, newts, nullflag);
3829 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3831 * Set file access and modification times of a file.
3834 sys_utimensat(struct utimensat_args *uap)
3836 struct timespec ts[2];
3837 struct nlookupdata nd;
3843 error = copyin(uap->ts, ts, sizeof(ts));
3848 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3849 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3850 UIO_USERSPACE, flags);
3852 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3853 nlookup_done_at(&nd, fp);
3858 kern_truncate(struct nlookupdata *nd, off_t length)
3865 uint64_t old_size = 0;
3869 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3870 if ((error = nlookup(nd)) != 0)
3872 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3874 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3876 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3881 if (vp->v_type == VDIR) {
3885 if (vfs_quota_enabled) {
3886 error = VOP_GETATTR(vp, &vattr);
3887 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3890 old_size = vattr.va_size;
3893 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3895 vattr.va_size = length;
3896 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3897 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3905 * truncate(char *path, int pad, off_t length)
3907 * Truncate a file given its path name.
3910 sys_truncate(struct truncate_args *uap)
3912 struct nlookupdata nd;
3915 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3917 error = kern_truncate(&nd, uap->length);
3923 kern_ftruncate(int fd, off_t length)
3925 struct thread *td = curthread;
3932 uint64_t old_size = 0;
3937 if ((error = holdvnode(td, fd, &fp)) != 0)
3939 if (fp->f_nchandle.ncp) {
3940 error = ncp_writechk(&fp->f_nchandle);
3944 if ((fp->f_flag & FWRITE) == 0) {
3948 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3952 vp = (struct vnode *)fp->f_data;
3953 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3954 if (vp->v_type == VDIR) {
3960 if (vfs_quota_enabled) {
3961 error = VOP_GETATTR_FP(vp, &vattr, fp);
3962 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3965 old_size = vattr.va_size;
3968 if ((error = vn_writechk(vp, NULL)) == 0) {
3970 vattr.va_size = length;
3971 error = VOP_SETATTR_FP(vp, &vattr, fp->f_cred, fp);
3973 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3982 * ftruncate_args(int fd, int pad, off_t length)
3984 * Truncate a file given a file descriptor.
3987 sys_ftruncate(struct ftruncate_args *uap)
3991 error = kern_ftruncate(uap->fd, uap->length);
3999 * Sync an open file.
4002 sys_fsync(struct fsync_args *uap)
4004 struct thread *td = curthread;
4010 if ((error = holdvnode(td, uap->fd, &fp)) != 0)
4012 vp = (struct vnode *)fp->f_data;
4013 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4014 if ((obj = vp->v_object) != NULL) {
4015 if (vp->v_mount == NULL ||
4016 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
4017 vm_object_page_clean(obj, 0, 0, 0);
4020 error = VOP_FSYNC_FP(vp, MNT_WAIT, VOP_FSYNC_SYSCALL, fp);
4021 if (error == 0 && vp->v_mount)
4022 error = buf_fsync(vp);
4030 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
4032 struct nchandle fnchd;
4033 struct nchandle tnchd;
4034 struct namecache *ncp;
4043 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
4044 if ((error = nlookup(fromnd)) != 0)
4046 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
4048 fnchd.mount = fromnd->nl_nch.mount;
4052 * unlock the source nch so we can lookup the target nch without
4053 * deadlocking. The target may or may not exist so we do not check
4054 * for a target vp like kern_mkdir() and other creation functions do.
4056 * The source and target directories are ref'd and rechecked after
4057 * everything is relocked to determine if the source or target file
4060 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
4061 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
4063 fncp_gen = fromnd->nl_nch.ncp->nc_generation;
4065 cache_unlock(&fromnd->nl_nch);
4067 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
4068 if ((error = nlookup(tond)) != 0) {
4072 tncp_gen = tond->nl_nch.ncp->nc_generation;
4074 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
4078 tnchd.mount = tond->nl_nch.mount;
4082 * If the source and target are the same there is nothing to do
4084 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
4091 * Mount points cannot be renamed or overwritten
4093 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
4102 * Relock the source ncp. cache_relock() will deal with any
4103 * deadlocks against the already-locked tond and will also
4104 * make sure both are resolved.
4106 * NOTE AFTER RELOCKING: The source or target ncp may have become
4107 * invalid while they were unlocked, nc_vp and nc_mount could
4110 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
4111 &tond->nl_nch, tond->nl_cred);
4112 fromnd->nl_flags |= NLC_NCPISLOCKED;
4115 * If the namecache generation changed for either fromnd or tond,
4118 if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
4119 tond->nl_nch.ncp->nc_generation != tncp_gen) {
4120 kprintf("kern_rename: retry due to gen on: "
4121 "\"%s\" -> \"%s\"\n",
4122 fromnd->nl_nch.ncp->nc_name,
4123 tond->nl_nch.ncp->nc_name);
4130 * If either fromnd or tond are marked destroyed a ripout occured
4131 * out from under us and we must retry.
4133 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
4134 fromnd->nl_nch.ncp->nc_vp == NULL ||
4135 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
4136 kprintf("kern_rename: retry due to ripout on: "
4137 "\"%s\" -> \"%s\"\n",
4138 fromnd->nl_nch.ncp->nc_name,
4139 tond->nl_nch.ncp->nc_name);
4146 * Make sure the parent directories linkages are the same.
4147 * XXX shouldn't be needed any more w/ generation check above.
4149 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
4150 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
4157 * Both the source and target must be within the same filesystem and
4158 * in the same filesystem as their parent directories within the
4159 * namecache topology.
4161 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4164 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
4165 mp != tond->nl_nch.mount) {
4172 * Make sure the mount point is writable
4174 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4181 * If the target exists and either the source or target is a directory,
4182 * then both must be directories.
4184 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4187 if (tond->nl_nch.ncp->nc_vp) {
4188 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4190 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4191 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4193 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4199 * You cannot rename a source into itself or a subdirectory of itself.
4200 * We check this by travsersing the target directory upwards looking
4201 * for a match against the source.
4206 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4207 if (fromnd->nl_nch.ncp == ncp) {
4218 * Even though the namespaces are different, they may still represent
4219 * hardlinks to the same file. The filesystem might have a hard time
4220 * with this so we issue a NREMOVE of the source instead of a NRENAME
4221 * when we detect the situation.
4224 fdvp = fromnd->nl_dvp;
4225 tdvp = tond->nl_dvp;
4226 if (fdvp == NULL || tdvp == NULL) {
4228 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4229 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4232 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4233 fdvp, tdvp, tond->nl_cred);
4240 * rename_args(char *from, char *to)
4242 * Rename files. Source and destination must either both be directories,
4243 * or both not be directories. If target is a directory, it must be empty.
4246 sys_rename(struct rename_args *uap)
4248 struct nlookupdata fromnd, tond;
4252 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4254 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4256 error = kern_rename(&fromnd, &tond);
4257 nlookup_done(&tond);
4259 nlookup_done(&fromnd);
4260 } while (error == EAGAIN);
4265 * renameat_args(int oldfd, char *old, int newfd, char *new)
4267 * Rename files using paths relative to the directories associated with
4268 * oldfd and newfd. Source and destination must either both be directories,
4269 * or both not be directories. If target is a directory, it must be empty.
4272 sys_renameat(struct renameat_args *uap)
4274 struct nlookupdata oldnd, newnd;
4275 struct file *oldfp, *newfp;
4279 error = nlookup_init_at(&oldnd, &oldfp,
4280 uap->oldfd, uap->old,
4283 error = nlookup_init_at(&newnd, &newfp,
4284 uap->newfd, uap->new,
4287 error = kern_rename(&oldnd, &newnd);
4288 nlookup_done_at(&newnd, newfp);
4290 nlookup_done_at(&oldnd, oldfp);
4291 } while (error == EAGAIN);
4296 kern_mkdir(struct nlookupdata *nd, int mode)
4298 struct thread *td = curthread;
4299 struct proc *p = td->td_proc;
4305 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4306 if ((error = nlookup(nd)) != 0)
4309 if (nd->nl_nch.ncp->nc_vp)
4311 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4314 vattr.va_type = VDIR;
4315 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4318 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4325 * mkdir_args(char *path, int mode)
4327 * Make a directory file.
4330 sys_mkdir(struct mkdir_args *uap)
4332 struct nlookupdata nd;
4335 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4337 error = kern_mkdir(&nd, uap->mode);
4343 * mkdirat_args(int fd, char *path, mode_t mode)
4345 * Make a directory file. The path is relative to the directory associated
4349 sys_mkdirat(struct mkdirat_args *uap)
4351 struct nlookupdata nd;
4355 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4357 error = kern_mkdir(&nd, uap->mode);
4358 nlookup_done_at(&nd, fp);
4363 kern_rmdir(struct nlookupdata *nd)
4368 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4369 if ((error = nlookup(nd)) != 0)
4373 * Do not allow directories representing mount points to be
4374 * deleted, even if empty. Check write perms on mount point
4375 * in case the vnode is aliased (aka nullfs).
4377 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4379 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4381 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4386 * rmdir_args(char *path)
4388 * Remove a directory file.
4391 sys_rmdir(struct rmdir_args *uap)
4393 struct nlookupdata nd;
4396 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4398 error = kern_rmdir(&nd);
4404 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4405 enum uio_seg direction)
4407 struct thread *td = curthread;
4415 if ((error = holdvnode(td, fd, &fp)) != 0)
4417 if ((fp->f_flag & FREAD) == 0) {
4421 vp = (struct vnode *)fp->f_data;
4422 if (vp->v_type != VDIR) {
4426 aiov.iov_base = buf;
4427 aiov.iov_len = count;
4428 auio.uio_iov = &aiov;
4429 auio.uio_iovcnt = 1;
4430 auio.uio_rw = UIO_READ;
4431 auio.uio_segflg = direction;
4433 auio.uio_resid = count;
4434 loff = auio.uio_offset = fp->f_offset;
4435 error = VOP_READDIR_FP(vp, &auio, fp->f_cred, &eofflag, NULL, NULL, fp);
4436 fp->f_offset = auio.uio_offset;
4441 * WARNING! *basep may not be wide enough to accomodate the
4442 * seek offset. XXX should we hack this to return the upper 32 bits
4443 * for offsets greater then 4G?
4446 *basep = (long)loff;
4448 *res = count - auio.uio_resid;
4455 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4457 * Read a block of directory entries in a file system independent format.
4460 sys_getdirentries(struct getdirentries_args *uap)
4465 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4466 &uap->sysmsg_result, UIO_USERSPACE);
4468 if (error == 0 && uap->basep)
4469 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4474 * getdents_args(int fd, char *buf, size_t count)
4477 sys_getdents(struct getdents_args *uap)
4481 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4482 &uap->sysmsg_result, UIO_USERSPACE);
4488 * Set the mode mask for creation of filesystem nodes.
4490 * umask(int newmask)
4493 sys_umask(struct umask_args *uap)
4495 struct thread *td = curthread;
4496 struct proc *p = td->td_proc;
4497 struct filedesc *fdp;
4500 uap->sysmsg_result = fdp->fd_cmask;
4501 fdp->fd_cmask = uap->newmask & ALLPERMS;
4506 * revoke(char *path)
4508 * Void all references to file by ripping underlying filesystem
4512 sys_revoke(struct revoke_args *uap)
4514 struct nlookupdata nd;
4521 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4523 error = nlookup(&nd);
4525 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4526 cred = crhold(nd.nl_cred);
4530 error = VOP_GETATTR(vp, &vattr);
4531 if (error == 0 && cred->cr_uid != vattr.va_uid)
4532 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4533 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4535 error = vrevoke(vp, cred);
4536 } else if (error == 0) {
4537 error = vrevoke(vp, cred);
4547 * getfh_args(char *fname, fhandle_t *fhp)
4549 * Get (NFS) file handle
4551 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4552 * mount. This allows nullfs mounts to be explicitly exported.
4554 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4556 * nullfs mounts of subdirectories are not safe. That is, it will
4557 * work, but you do not really have protection against access to
4558 * the related parent directories.
4561 sys_getfh(struct getfh_args *uap)
4563 struct thread *td = curthread;
4564 struct nlookupdata nd;
4571 * Must be super user
4573 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4577 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4579 error = nlookup(&nd);
4581 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4582 mp = nd.nl_nch.mount;
4585 bzero(&fh, sizeof(fh));
4586 fh.fh_fsid = mp->mnt_stat.f_fsid;
4587 error = VFS_VPTOFH(vp, &fh.fh_fid);
4590 error = copyout(&fh, uap->fhp, sizeof(fh));
4596 * fhopen_args(const struct fhandle *u_fhp, int flags)
4598 * syscall for the rpc.lockd to use to translate a NFS file handle into
4599 * an open descriptor.
4601 * warning: do not remove the priv_check() call or this becomes one giant
4605 sys_fhopen(struct fhopen_args *uap)
4607 struct thread *td = curthread;
4608 struct filedesc *fdp = td->td_proc->p_fd;
4613 struct vattr *vap = &vat;
4615 int fmode, mode, error = 0, type;
4621 * Must be super user
4623 error = priv_check(td, PRIV_ROOT);
4627 fmode = FFLAGS(uap->flags);
4630 * Why not allow a non-read/write open for our lockd?
4632 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4634 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4639 * Find the mount point
4641 mp = vfs_getvfs(&fhp.fh_fsid);
4646 /* now give me my vnode, it gets returned to me locked */
4647 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4651 * from now on we have to make sure not
4652 * to forget about the vnode
4653 * any error that causes an abort must vput(vp)
4654 * just set error = err and 'goto bad;'.
4660 if (vp->v_type == VLNK) {
4664 if (vp->v_type == VSOCK) {
4669 if (fmode & (FWRITE | O_TRUNC)) {
4670 if (vp->v_type == VDIR) {
4674 error = vn_writechk(vp, NULL);
4682 error = VOP_ACCESS(vp, mode, td->td_ucred);
4686 if (fmode & O_TRUNC) {
4687 vn_unlock(vp); /* XXX */
4688 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4691 error = VOP_SETATTR(vp, vap, td->td_ucred);
4697 * VOP_OPEN needs the file pointer so it can potentially override
4700 * WARNING! no f_nchandle will be associated when fhopen()ing a
4703 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4707 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4710 * setting f_ops this way prevents VOP_CLOSE from being
4711 * called or fdrop() releasing the vp from v_data. Since
4712 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4714 fp->f_ops = &badfileops;
4720 * The fp is given its own reference, we still have our ref and lock.
4722 * Assert that all regular files must be created with a VM object.
4724 if (vp->v_type == VREG && vp->v_object == NULL) {
4725 kprintf("fhopen: regular file did not "
4726 "have VM object: %p\n",
4732 * The open was successful. Handle any locking requirements.
4734 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4735 lf.l_whence = SEEK_SET;
4738 if (fmode & O_EXLOCK)
4739 lf.l_type = F_WRLCK;
4741 lf.l_type = F_RDLCK;
4742 if (fmode & FNONBLOCK)
4747 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK,
4750 * release our private reference.
4752 fsetfd(fdp, NULL, indx);
4757 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4758 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
4762 * Clean up. Associate the file pointer with the previously
4763 * reserved descriptor and return it.
4766 if (uap->flags & O_CLOEXEC)
4767 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4768 fsetfd(fdp, fp, indx);
4770 uap->sysmsg_result = indx;
4776 fsetfd(fdp, NULL, indx);
4787 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4790 sys_fhstat(struct fhstat_args *uap)
4792 struct thread *td = curthread;
4800 * Must be super user
4802 error = priv_check(td, PRIV_ROOT);
4806 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4810 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4813 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4814 error = vn_stat(vp, &sb, td->td_ucred);
4819 error = copyout(&sb, uap->sb, sizeof(sb));
4827 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4830 sys_fhstatfs(struct fhstatfs_args *uap)
4832 struct thread *td = curthread;
4833 struct proc *p = td->td_proc;
4838 char *fullpath, *freepath;
4843 * Must be super user
4845 if ((error = priv_check(td, PRIV_ROOT)))
4848 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4851 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4855 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4860 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4865 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4868 error = mount_path(p, mp, &fullpath, &freepath);
4871 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4872 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4873 kfree(freepath, M_TEMP);
4875 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4876 if (priv_check(td, PRIV_ROOT)) {
4877 bcopy(sp, &sb, sizeof(sb));
4878 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4881 error = copyout(sp, uap->buf, sizeof(*sp));
4890 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4893 sys_fhstatvfs(struct fhstatvfs_args *uap)
4895 struct thread *td = curthread;
4896 struct proc *p = td->td_proc;
4904 * Must be super user
4906 if ((error = priv_check(td, PRIV_ROOT)))
4909 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4912 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4916 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4921 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4924 sp = &mp->mnt_vstat;
4926 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4930 if (mp->mnt_flag & MNT_RDONLY)
4931 sp->f_flag |= ST_RDONLY;
4932 if (mp->mnt_flag & MNT_NOSUID)
4933 sp->f_flag |= ST_NOSUID;
4934 error = copyout(sp, uap->buf, sizeof(*sp));
4943 * Syscall to push extended attribute configuration information into the
4944 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4945 * a command (int cmd), and attribute name and misc data. For now, the
4946 * attribute name is left in userspace for consumption by the VFS_op.
4947 * It will probably be changed to be copied into sysspace by the
4948 * syscall in the future, once issues with various consumers of the
4949 * attribute code have raised their hands.
4951 * Currently this is used only by UFS Extended Attributes.
4954 sys_extattrctl(struct extattrctl_args *uap)
4956 struct nlookupdata nd;
4958 char attrname[EXTATTR_MAXNAMELEN];
4966 if (error == 0 && uap->filename) {
4967 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4970 error = nlookup(&nd);
4972 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4976 if (error == 0 && uap->attrname) {
4977 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4982 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4984 error = nlookup(&nd);
4986 error = ncp_writechk(&nd.nl_nch);
4988 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4990 uap->attrname, nd.nl_cred);
4999 * Syscall to get a named extended attribute on a file or directory.
5002 sys_extattr_set_file(struct extattr_set_file_args *uap)
5004 char attrname[EXTATTR_MAXNAMELEN];
5005 struct nlookupdata nd;
5011 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5017 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5019 error = nlookup(&nd);
5021 error = ncp_writechk(&nd.nl_nch);
5023 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5029 bzero(&auio, sizeof(auio));
5030 aiov.iov_base = uap->data;
5031 aiov.iov_len = uap->nbytes;
5032 auio.uio_iov = &aiov;
5033 auio.uio_iovcnt = 1;
5034 auio.uio_offset = 0;
5035 auio.uio_resid = uap->nbytes;
5036 auio.uio_rw = UIO_WRITE;
5037 auio.uio_td = curthread;
5039 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
5048 * Syscall to get a named extended attribute on a file or directory.
5051 sys_extattr_get_file(struct extattr_get_file_args *uap)
5053 char attrname[EXTATTR_MAXNAMELEN];
5054 struct nlookupdata nd;
5060 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5066 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5068 error = nlookup(&nd);
5070 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
5076 bzero(&auio, sizeof(auio));
5077 aiov.iov_base = uap->data;
5078 aiov.iov_len = uap->nbytes;
5079 auio.uio_iov = &aiov;
5080 auio.uio_iovcnt = 1;
5081 auio.uio_offset = 0;
5082 auio.uio_resid = uap->nbytes;
5083 auio.uio_rw = UIO_READ;
5084 auio.uio_td = curthread;
5086 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
5088 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
5096 * Syscall to delete a named extended attribute from a file or directory.
5097 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
5100 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
5102 char attrname[EXTATTR_MAXNAMELEN];
5103 struct nlookupdata nd;
5107 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5111 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5113 error = nlookup(&nd);
5115 error = ncp_writechk(&nd.nl_nch);
5117 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5119 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
5120 attrname, NULL, nd.nl_cred);
5129 * Determine if the mount is visible to the process.
5132 chroot_visible_mnt(struct mount *mp, struct proc *p)
5134 struct nchandle nch;
5137 * Traverse from the mount point upwards. If we hit the process
5138 * root then the mount point is visible to the process.
5140 nch = mp->mnt_ncmountpt;
5142 if (nch.mount == p->p_fd->fd_nrdir.mount &&
5143 nch.ncp == p->p_fd->fd_nrdir.ncp) {
5146 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
5147 nch = nch.mount->mnt_ncmounton;
5149 nch.ncp = nch.ncp->nc_parent;
5154 * If the mount point is not visible to the process, but the
5155 * process root is in a subdirectory of the mount, return
5158 if (p->p_fd->fd_nrdir.mount == mp)