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 (struct filedesc *fdp);
84 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
85 static int getutimes (struct timeval *, struct timespec *);
86 static int getutimens (const struct timespec *, struct timespec *, int *);
87 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
88 static int setfmode (struct vnode *, int);
89 static int setfflags (struct vnode *, int);
90 static int setutimes (struct vnode *, struct vattr *,
91 const struct timespec *, int);
92 static int usermount = 0; /* if 1, non-root can mount fs. */
94 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
95 "Allow non-root users to mount filesystems");
98 * Virtual File System System Calls
102 * Mount a file system.
104 * mount_args(char *type, char *path, int flags, caddr_t data)
109 sys_mount(struct mount_args *uap)
111 struct thread *td = curthread;
114 struct mount *mp, *nullmp;
115 struct vfsconf *vfsp;
116 int error, flag = 0, flag2 = 0;
119 struct nlookupdata nd;
120 char fstypename[MFSNAMELEN];
128 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
132 * Do not allow NFS export by non-root users.
134 if (uap->flags & MNT_EXPORTED) {
135 error = priv_check(td, PRIV_ROOT);
140 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
142 if (priv_check(td, PRIV_ROOT))
143 uap->flags |= MNT_NOSUID | MNT_NODEV;
146 * Lookup the requested path and extract the nch and vnode.
148 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
150 if ((error = nlookup(&nd)) == 0) {
151 if (nd.nl_nch.ncp->nc_vp == NULL)
161 * If the target filesystem is resolved via a nullfs mount, then
162 * nd.nl_nch.mount will be pointing to the nullfs mount structure
163 * instead of the target file system. We need it in case we are
166 nullmp = nd.nl_nch.mount;
169 * Extract the locked+refd ncp and cleanup the nd structure
172 cache_zero(&nd.nl_nch);
175 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
176 (mp = cache_findmount(&nch)) != NULL) {
185 * now we have the locked ref'd nch and unreferenced vnode.
188 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
195 * Extract the file system type. We need to know this early, to take
196 * appropriate actions if we are dealing with a nullfs.
198 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
205 * Now we have an unlocked ref'd nch and a locked ref'd vp
207 if (uap->flags & MNT_UPDATE) {
208 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
215 if (strncmp(fstypename, "null", 5) == 0) {
223 flag2 = mp->mnt_kern_flag;
225 * We only allow the filesystem to be reloaded if it
226 * is currently mounted read-only.
228 if ((uap->flags & MNT_RELOAD) &&
229 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
232 error = EOPNOTSUPP; /* Needs translation */
236 * Only root, or the user that did the original mount is
237 * permitted to update it.
239 if (mp->mnt_stat.f_owner != cred->cr_uid &&
240 (error = priv_check(td, PRIV_ROOT))) {
245 if (vfs_busy(mp, LK_NOWAIT)) {
259 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
260 lwkt_gettoken(&mp->mnt_token);
267 * If the user is not root, ensure that they own the directory
268 * onto which we are attempting to mount.
270 if ((error = VOP_GETATTR(vp, &va)) ||
271 (va.va_uid != cred->cr_uid &&
272 (error = priv_check(td, PRIV_ROOT)))) {
277 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
282 if (vp->v_type != VDIR) {
288 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
294 vfsp = vfsconf_find_by_name(fstypename);
298 /* Only load modules for root (very important!) */
299 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
304 error = linker_load_file(fstypename, &lf);
305 if (error || lf == NULL) {
313 /* lookup again, see if the VFS was loaded */
314 vfsp = vfsconf_find_by_name(fstypename);
317 linker_file_unload(lf);
332 * Allocate and initialize the filesystem.
334 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
336 vfs_busy(mp, LK_NOWAIT);
337 mp->mnt_op = vfsp->vfc_vfsops;
339 mp->mnt_pbuf_count = nswbuf_kva / NSWBUF_SPLIT;
340 vfsp->vfc_refcount++;
341 mp->mnt_stat.f_type = vfsp->vfc_typenum;
342 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
343 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
344 mp->mnt_stat.f_owner = cred->cr_uid;
345 lwkt_gettoken(&mp->mnt_token);
349 * (per-mount token acquired at this point)
351 * Set the mount level flags.
353 if (uap->flags & MNT_RDONLY)
354 mp->mnt_flag |= MNT_RDONLY;
355 else if (mp->mnt_flag & MNT_RDONLY)
356 mp->mnt_kern_flag |= MNTK_WANTRDWR;
357 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
358 MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME |
359 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
360 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
362 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
363 MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE |
364 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
365 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
369 * Pre-set the mount's ALL_MPSAFE flags if specified in the vfsconf.
370 * This way the initial VFS_MOUNT() call will also be MPSAFE.
372 if (vfsp->vfc_flags & VFCF_MPSAFE)
373 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
376 * Mount the filesystem.
377 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
380 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
381 if (mp->mnt_flag & MNT_UPDATE) {
382 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
383 mp->mnt_flag &= ~MNT_RDONLY;
384 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
385 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
388 mp->mnt_kern_flag = flag2;
390 lwkt_reltoken(&mp->mnt_token);
396 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
399 * Put the new filesystem on the mount list after root. The mount
400 * point gets its own mnt_ncmountpt (unless the VFS already set one
401 * up) which represents the root of the mount. The lookup code
402 * detects the mount point going forward and checks the root of
403 * the mount going backwards.
405 * It is not necessary to invalidate or purge the vnode underneath
406 * because elements under the mount will be given their own glue
410 if (mp->mnt_ncmountpt.ncp == NULL) {
412 * allocate, then unlock, but leave the ref intact
414 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
415 cache_unlock(&mp->mnt_ncmountpt);
418 mp->mnt_ncmounton = nch; /* inherits ref */
420 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
422 cache_ismounting(mp);
423 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
425 mountlist_insert(mp, MNTINS_LAST);
427 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
428 error = vfs_allocate_syncvnode(mp);
429 lwkt_reltoken(&mp->mnt_token);
431 error = VFS_START(mp, 0);
433 KNOTE(&fs_klist, VQ_MOUNT);
435 vn_syncer_thr_stop(mp);
436 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
437 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
438 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
439 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
440 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
441 mp->mnt_vfc->vfc_refcount--;
442 lwkt_reltoken(&mp->mnt_token);
453 * Scan all active processes to see if any of them have a current
454 * or root directory onto which the new filesystem has just been
455 * mounted. If so, replace them with the new mount point.
457 * Both old_nch and new_nch are ref'd on call but not locked.
458 * new_nch must be temporarily locked so it can be associated with the
459 * vnode representing the root of the mount point.
461 struct checkdirs_info {
462 struct nchandle old_nch;
463 struct nchandle new_nch;
464 struct vnode *old_vp;
465 struct vnode *new_vp;
468 static int checkdirs_callback(struct proc *p, void *data);
471 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
473 struct checkdirs_info info;
479 * If the old mount point's vnode has a usecount of 1, it is not
480 * being held as a descriptor anywhere.
482 olddp = old_nch->ncp->nc_vp;
483 if (olddp == NULL || VREFCNT(olddp) == 1)
487 * Force the root vnode of the new mount point to be resolved
488 * so we can update any matching processes.
491 if (VFS_ROOT(mp, &newdp))
492 panic("mount: lost mount");
495 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
496 cache_setunresolved(new_nch);
497 cache_setvp(new_nch, newdp);
498 cache_unlock(new_nch);
501 * Special handling of the root node
503 if (rootvnode == olddp) {
505 vfs_cache_setroot(newdp, cache_hold(new_nch));
509 * Pass newdp separately so the callback does not have to access
510 * it via new_nch->ncp->nc_vp.
512 info.old_nch = *old_nch;
513 info.new_nch = *new_nch;
515 allproc_scan(checkdirs_callback, &info);
520 * NOTE: callback is not MP safe because the scanned process's filedesc
521 * structure can be ripped out from under us, amoung other things.
524 checkdirs_callback(struct proc *p, void *data)
526 struct checkdirs_info *info = data;
527 struct filedesc *fdp;
528 struct nchandle ncdrop1;
529 struct nchandle ncdrop2;
530 struct vnode *vprele1;
531 struct vnode *vprele2;
533 if ((fdp = p->p_fd) != NULL) {
534 cache_zero(&ncdrop1);
535 cache_zero(&ncdrop2);
540 * MPUNSAFE - XXX fdp can be pulled out from under a
543 * A shared filedesc is ok, we don't have to copy it
544 * because we are making this change globally.
546 spin_lock(&fdp->fd_spin);
547 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
548 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
549 vprele1 = fdp->fd_cdir;
551 fdp->fd_cdir = info->new_vp;
552 ncdrop1 = fdp->fd_ncdir;
553 cache_copy(&info->new_nch, &fdp->fd_ncdir);
555 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
556 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
557 vprele2 = fdp->fd_rdir;
559 fdp->fd_rdir = info->new_vp;
560 ncdrop2 = fdp->fd_nrdir;
561 cache_copy(&info->new_nch, &fdp->fd_nrdir);
563 spin_unlock(&fdp->fd_spin);
565 cache_drop(&ncdrop1);
567 cache_drop(&ncdrop2);
577 * Unmount a file system.
579 * Note: unmount takes a path to the vnode mounted on as argument,
580 * not special file (as before).
582 * umount_args(char *path, int flags)
587 sys_unmount(struct unmount_args *uap)
589 struct thread *td = curthread;
590 struct proc *p __debugvar = td->td_proc;
591 struct mount *mp = NULL;
592 struct nlookupdata nd;
596 if (td->td_ucred->cr_prison != NULL) {
600 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
603 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
605 error = nlookup(&nd);
609 mp = nd.nl_nch.mount;
612 * Only root, or the user that did the original mount is
613 * permitted to unmount this filesystem.
615 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
616 (error = priv_check(td, PRIV_ROOT)))
620 * Don't allow unmounting the root file system.
622 if (mp->mnt_flag & MNT_ROOTFS) {
628 * Must be the root of the filesystem
630 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
638 error = dounmount(mp, uap->flags);
644 * Do the actual file system unmount.
647 dounmount_interlock(struct mount *mp)
649 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
651 mp->mnt_kern_flag |= MNTK_UNMOUNT;
656 unmount_allproc_cb(struct proc *p, void *arg)
660 if (p->p_textnch.ncp == NULL)
663 mp = (struct mount *)arg;
664 if (p->p_textnch.mount == mp)
665 cache_drop(&p->p_textnch);
671 dounmount(struct mount *mp, int flags)
673 struct namecache *ncp;
682 lwkt_gettoken(&mp->mnt_token);
684 * Exclusive access for unmounting purposes
686 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
690 * Allow filesystems to detect that a forced unmount is in progress.
692 if (flags & MNT_FORCE)
693 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
694 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
695 error = lockmgr(&mp->mnt_lock, lflags);
697 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
698 if (mp->mnt_kern_flag & MNTK_MWAIT) {
699 mp->mnt_kern_flag &= ~MNTK_MWAIT;
705 if (mp->mnt_flag & MNT_EXPUBLIC)
706 vfs_setpublicfs(NULL, NULL, NULL);
708 vfs_msync(mp, MNT_WAIT);
709 async_flag = mp->mnt_flag & MNT_ASYNC;
710 mp->mnt_flag &=~ MNT_ASYNC;
713 * If this filesystem isn't aliasing other filesystems,
714 * try to invalidate any remaining namecache entries and
715 * check the count afterwords.
717 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
718 cache_lock(&mp->mnt_ncmountpt);
719 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
720 cache_unlock(&mp->mnt_ncmountpt);
722 cache_clearmntcache();
723 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
724 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
725 allproc_scan(&unmount_allproc_cb, mp);
728 cache_clearmntcache();
729 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
730 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
732 if ((flags & MNT_FORCE) == 0) {
734 mount_warning(mp, "Cannot unmount: "
740 mount_warning(mp, "Forced unmount: "
751 * Decomission our special mnt_syncer vnode. This also stops
752 * the vnlru code. If we are unable to unmount we recommission
755 * Then sync the filesystem.
757 if ((vp = mp->mnt_syncer) != NULL) {
758 mp->mnt_syncer = NULL;
759 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
762 if ((mp->mnt_flag & MNT_RDONLY) == 0)
763 VFS_SYNC(mp, MNT_WAIT);
766 * nchandle records ref the mount structure. Expect a count of 1
767 * (our mount->mnt_ncmountpt).
769 * Scans can get temporary refs on a mountpoint (thought really
770 * heavy duty stuff like cache_findmount() do not).
772 if (mp->mnt_refs != 1)
773 cache_clearmntcache();
774 for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
775 cache_unmounting(mp);
776 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
777 cache_clearmntcache();
779 if (mp->mnt_refs != 1) {
780 if ((flags & MNT_FORCE) == 0) {
781 mount_warning(mp, "Cannot unmount: "
782 "%d mount refs still present",
786 mount_warning(mp, "Forced unmount: "
787 "%d mount refs still present",
794 * So far so good, sync the filesystem once more and
795 * call the VFS unmount code if the sync succeeds.
798 if (mp->mnt_flag & MNT_RDONLY) {
799 error = VFS_UNMOUNT(mp, flags);
801 error = VFS_SYNC(mp, MNT_WAIT);
803 (error == EOPNOTSUPP) || /* No sync */
804 (flags & MNT_FORCE)) {
805 error = VFS_UNMOUNT(mp, flags);
811 * If an error occurred we can still recover, restoring the
812 * syncer vnode and misc flags.
815 if (mp->mnt_syncer == NULL)
816 vfs_allocate_syncvnode(mp);
817 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
818 mp->mnt_flag |= async_flag;
819 lockmgr(&mp->mnt_lock, LK_RELEASE);
820 if (mp->mnt_kern_flag & MNTK_MWAIT) {
821 mp->mnt_kern_flag &= ~MNTK_MWAIT;
827 * Clean up any journals still associated with the mount after
828 * filesystem activity has ceased.
830 journal_remove_all_journals(mp,
831 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
833 mountlist_remove(mp);
836 * Remove any installed vnode ops here so the individual VFSs don't
839 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
840 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
841 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
842 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
843 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
845 if (mp->mnt_ncmountpt.ncp != NULL) {
846 nch = mp->mnt_ncmountpt;
847 cache_zero(&mp->mnt_ncmountpt);
848 cache_clrmountpt(&nch);
851 if (mp->mnt_ncmounton.ncp != NULL) {
852 cache_unmounting(mp);
853 nch = mp->mnt_ncmounton;
854 cache_zero(&mp->mnt_ncmounton);
855 cache_clrmountpt(&nch);
859 mp->mnt_vfc->vfc_refcount--;
860 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
861 panic("unmount: dangling vnode");
862 lockmgr(&mp->mnt_lock, LK_RELEASE);
863 if (mp->mnt_kern_flag & MNTK_MWAIT) {
864 mp->mnt_kern_flag &= ~MNTK_MWAIT;
869 * If we reach here and freeok != 0 we must free the mount.
870 * If refs > 1 cycle and wait, just in case someone tried
871 * to busy the mount after we decided to do the unmount.
874 if (mp->mnt_refs > 1)
875 cache_clearmntcache();
876 while (mp->mnt_refs > 1) {
877 cache_unmounting(mp);
879 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
880 cache_clearmntcache();
882 lwkt_reltoken(&mp->mnt_token);
887 KNOTE(&fs_klist, VQ_UNMOUNT);
890 lwkt_reltoken(&mp->mnt_token);
896 mount_warning(struct mount *mp, const char *ctl, ...)
903 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
904 &ptr, &buf, 0) == 0) {
905 kprintf("unmount(%s): ", ptr);
910 kprintf("unmount(%p", mp);
911 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
912 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
921 * Shim cache_fullpath() to handle the case where a process is chrooted into
922 * a subdirectory of a mount. In this case if the root mount matches the
923 * process root directory's mount we have to specify the process's root
924 * directory instead of the mount point, because the mount point might
925 * be above the root directory.
929 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
931 struct nchandle *nch;
933 if (p && p->p_fd->fd_nrdir.mount == mp)
934 nch = &p->p_fd->fd_nrdir;
936 nch = &mp->mnt_ncmountpt;
937 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
941 * Sync each mounted filesystem.
945 static int syncprt = 0;
946 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
949 static int sync_callback(struct mount *mp, void *data);
952 sys_sync(struct sync_args *uap)
954 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
960 sync_callback(struct mount *mp, void *data __unused)
964 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
965 asyncflag = mp->mnt_flag & MNT_ASYNC;
966 mp->mnt_flag &= ~MNT_ASYNC;
967 vfs_msync(mp, MNT_NOWAIT);
968 VFS_SYNC(mp, MNT_NOWAIT);
969 mp->mnt_flag |= asyncflag;
974 /* XXX PRISON: could be per prison flag */
975 static int prison_quotas;
977 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
981 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
983 * Change filesystem quotas.
988 sys_quotactl(struct quotactl_args *uap)
990 struct nlookupdata nd;
996 if (td->td_ucred->cr_prison && !prison_quotas) {
1001 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1003 error = nlookup(&nd);
1005 mp = nd.nl_nch.mount;
1006 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
1007 uap->arg, nd.nl_cred);
1015 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1016 * void *buf, int buflen)
1018 * This function operates on a mount point and executes the specified
1019 * operation using the specified control data, and possibly returns data.
1021 * The actual number of bytes stored in the result buffer is returned, 0
1022 * if none, otherwise an error is returned.
1027 sys_mountctl(struct mountctl_args *uap)
1029 struct thread *td = curthread;
1030 struct proc *p = td->td_proc;
1038 * Sanity and permissions checks. We must be root.
1041 if (td->td_ucred->cr_prison != NULL)
1043 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1044 (error = priv_check(td, PRIV_ROOT)) != 0)
1048 * Argument length checks
1050 if (uap->ctllen < 0 || uap->ctllen > 1024)
1052 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1054 if (uap->path == NULL)
1058 * Allocate the necessary buffers and copyin data
1060 path = objcache_get(namei_oc, M_WAITOK);
1061 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1066 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1067 error = copyin(uap->ctl, ctl, uap->ctllen);
1072 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1075 * Validate the descriptor
1078 fp = holdfp(p->p_fd, uap->fd, -1);
1088 * Execute the internal kernel function and clean up.
1090 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1093 if (error == 0 && uap->sysmsg_result > 0)
1094 error = copyout(buf, uap->buf, uap->sysmsg_result);
1097 objcache_put(namei_oc, path);
1106 * Execute a mount control operation by resolving the path to a mount point
1107 * and calling vop_mountctl().
1109 * Use the mount point from the nch instead of the vnode so nullfs mounts
1110 * can properly spike the VOP.
1113 kern_mountctl(const char *path, int op, struct file *fp,
1114 const void *ctl, int ctllen,
1115 void *buf, int buflen, int *res)
1118 struct nlookupdata nd;
1119 struct nchandle nch;
1125 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1128 error = nlookup(&nd);
1133 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1140 * Yes, all this is needed to use the nch.mount below, because
1141 * we must maintain a ref on the mount to avoid ripouts (e.g.
1142 * due to heavy mount/unmount use by synth or poudriere).
1145 cache_zero(&nd.nl_nch);
1153 * Must be the root of the filesystem
1155 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1160 if (mp == NULL || mp->mnt_kern_flag & MNTK_UNMOUNT) {
1161 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1167 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1176 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1178 struct thread *td = curthread;
1179 struct proc *p = td->td_proc;
1182 char *fullpath, *freepath;
1185 if ((error = nlookup(nd)) != 0)
1187 mp = nd->nl_nch.mount;
1189 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1192 error = mount_path(p, mp, &fullpath, &freepath);
1195 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1196 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1197 kfree(freepath, M_TEMP);
1199 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1200 bcopy(sp, buf, sizeof(*buf));
1201 /* Only root should have access to the fsid's. */
1202 if (priv_check(td, PRIV_ROOT))
1203 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1208 * statfs_args(char *path, struct statfs *buf)
1210 * Get filesystem statistics.
1213 sys_statfs(struct statfs_args *uap)
1215 struct nlookupdata nd;
1219 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1221 error = kern_statfs(&nd, &buf);
1224 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1229 kern_fstatfs(int fd, struct statfs *buf)
1231 struct thread *td = curthread;
1232 struct proc *p = td->td_proc;
1236 char *fullpath, *freepath;
1240 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1244 * Try to use mount info from any overlays rather than the
1245 * mount info for the underlying vnode, otherwise we will
1246 * fail when operating on null-mounted paths inside a chroot.
1248 if ((mp = fp->f_nchandle.mount) == NULL)
1249 mp = ((struct vnode *)fp->f_data)->v_mount;
1254 if (fp->f_cred == NULL) {
1259 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1262 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1264 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1265 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1266 kfree(freepath, M_TEMP);
1268 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1269 bcopy(sp, buf, sizeof(*buf));
1271 /* Only root should have access to the fsid's. */
1272 if (priv_check(td, PRIV_ROOT))
1273 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1281 * fstatfs_args(int fd, struct statfs *buf)
1283 * Get filesystem statistics.
1286 sys_fstatfs(struct fstatfs_args *uap)
1291 error = kern_fstatfs(uap->fd, &buf);
1294 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1299 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1305 if ((error = nlookup(nd)) != 0)
1307 mp = nd->nl_nch.mount;
1308 sp = &mp->mnt_vstat;
1309 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1313 if (mp->mnt_flag & MNT_RDONLY)
1314 sp->f_flag |= ST_RDONLY;
1315 if (mp->mnt_flag & MNT_NOSUID)
1316 sp->f_flag |= ST_NOSUID;
1317 bcopy(sp, buf, sizeof(*buf));
1322 * statfs_args(char *path, struct statfs *buf)
1324 * Get filesystem statistics.
1327 sys_statvfs(struct statvfs_args *uap)
1329 struct nlookupdata nd;
1333 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1335 error = kern_statvfs(&nd, &buf);
1338 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1343 kern_fstatvfs(int fd, struct statvfs *buf)
1345 struct thread *td = curthread;
1346 struct proc *p = td->td_proc;
1353 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1355 if ((mp = fp->f_nchandle.mount) == NULL)
1356 mp = ((struct vnode *)fp->f_data)->v_mount;
1361 if (fp->f_cred == NULL) {
1365 sp = &mp->mnt_vstat;
1366 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1370 if (mp->mnt_flag & MNT_RDONLY)
1371 sp->f_flag |= ST_RDONLY;
1372 if (mp->mnt_flag & MNT_NOSUID)
1373 sp->f_flag |= ST_NOSUID;
1375 bcopy(sp, buf, sizeof(*buf));
1383 * fstatfs_args(int fd, struct statfs *buf)
1385 * Get filesystem statistics.
1388 sys_fstatvfs(struct fstatvfs_args *uap)
1393 error = kern_fstatvfs(uap->fd, &buf);
1396 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1401 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1403 * Get statistics on all filesystems.
1406 struct getfsstat_info {
1407 struct statfs *sfsp;
1415 static int getfsstat_callback(struct mount *, void *);
1418 sys_getfsstat(struct getfsstat_args *uap)
1420 struct thread *td = curthread;
1421 struct getfsstat_info info;
1423 bzero(&info, sizeof(info));
1425 info.maxcount = uap->bufsize / sizeof(struct statfs);
1426 info.sfsp = uap->buf;
1428 info.flags = uap->flags;
1431 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1432 if (info.sfsp && info.count > info.maxcount)
1433 uap->sysmsg_result = info.maxcount;
1435 uap->sysmsg_result = info.count;
1436 return (info.error);
1440 getfsstat_callback(struct mount *mp, void *data)
1442 struct getfsstat_info *info = data;
1448 if (info->sfsp && info->count < info->maxcount) {
1449 if (info->td->td_proc &&
1450 !chroot_visible_mnt(mp, info->td->td_proc)) {
1456 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1457 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1458 * overrides MNT_WAIT.
1460 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1461 (info->flags & MNT_WAIT)) &&
1462 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1465 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1467 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1469 info->error = error;
1472 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1473 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1474 kfree(freepath, M_TEMP);
1476 error = copyout(sp, info->sfsp, sizeof(*sp));
1478 info->error = error;
1488 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1489 long bufsize, int flags)
1491 * Get statistics on all filesystems.
1494 struct getvfsstat_info {
1495 struct statfs *sfsp;
1496 struct statvfs *vsfsp;
1504 static int getvfsstat_callback(struct mount *, void *);
1507 sys_getvfsstat(struct getvfsstat_args *uap)
1509 struct thread *td = curthread;
1510 struct getvfsstat_info info;
1512 bzero(&info, sizeof(info));
1514 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1515 info.sfsp = uap->buf;
1516 info.vsfsp = uap->vbuf;
1518 info.flags = uap->flags;
1521 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1522 if (info.vsfsp && info.count > info.maxcount)
1523 uap->sysmsg_result = info.maxcount;
1525 uap->sysmsg_result = info.count;
1526 return (info.error);
1530 getvfsstat_callback(struct mount *mp, void *data)
1532 struct getvfsstat_info *info = data;
1534 struct statvfs *vsp;
1539 if (info->vsfsp && info->count < info->maxcount) {
1540 if (info->td->td_proc &&
1541 !chroot_visible_mnt(mp, info->td->td_proc)) {
1545 vsp = &mp->mnt_vstat;
1548 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1549 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1550 * overrides MNT_WAIT.
1552 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1553 (info->flags & MNT_WAIT)) &&
1554 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1557 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1559 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1560 (info->flags & MNT_WAIT)) &&
1561 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1565 if (mp->mnt_flag & MNT_RDONLY)
1566 vsp->f_flag |= ST_RDONLY;
1567 if (mp->mnt_flag & MNT_NOSUID)
1568 vsp->f_flag |= ST_NOSUID;
1570 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1572 info->error = error;
1575 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1576 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1577 kfree(freepath, M_TEMP);
1579 error = copyout(sp, info->sfsp, sizeof(*sp));
1581 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1583 info->error = error;
1595 * fchdir_args(int fd)
1597 * Change current working directory to a given file descriptor.
1600 sys_fchdir(struct fchdir_args *uap)
1602 struct thread *td = curthread;
1603 struct proc *p = td->td_proc;
1604 struct filedesc *fdp = p->p_fd;
1605 struct vnode *vp, *ovp;
1608 struct nchandle nch, onch, tnch;
1611 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1613 lwkt_gettoken(&p->p_token);
1614 vp = (struct vnode *)fp->f_data;
1616 vn_lock(vp, LK_SHARED | LK_RETRY);
1617 if (fp->f_nchandle.ncp == NULL)
1620 error = checkvp_chdir(vp, td);
1625 cache_copy(&fp->f_nchandle, &nch);
1628 * If the ncp has become a mount point, traverse through
1632 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1633 (mp = cache_findmount(&nch)) != NULL
1635 error = nlookup_mp(mp, &tnch);
1637 cache_unlock(&tnch); /* leave ref intact */
1639 vp = tnch.ncp->nc_vp;
1640 error = vget(vp, LK_SHARED);
1641 KKASSERT(error == 0);
1645 cache_dropmount(mp);
1648 spin_lock(&fdp->fd_spin);
1650 onch = fdp->fd_ncdir;
1652 fdp->fd_ncdir = nch;
1653 spin_unlock(&fdp->fd_spin);
1654 vn_unlock(vp); /* leave ref intact */
1663 lwkt_reltoken(&p->p_token);
1668 kern_chdir(struct nlookupdata *nd)
1670 struct thread *td = curthread;
1671 struct proc *p = td->td_proc;
1672 struct filedesc *fdp = p->p_fd;
1673 struct vnode *vp, *ovp;
1674 struct nchandle onch;
1677 nd->nl_flags |= NLC_SHAREDLOCK;
1678 if ((error = nlookup(nd)) != 0)
1680 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1682 if ((error = vget(vp, LK_SHARED)) != 0)
1685 lwkt_gettoken(&p->p_token);
1686 error = checkvp_chdir(vp, td);
1689 spin_lock(&fdp->fd_spin);
1691 onch = fdp->fd_ncdir;
1692 fdp->fd_ncdir = nd->nl_nch;
1694 spin_unlock(&fdp->fd_spin);
1695 cache_unlock(&nd->nl_nch); /* leave reference intact */
1698 cache_zero(&nd->nl_nch);
1702 lwkt_reltoken(&p->p_token);
1707 * chdir_args(char *path)
1709 * Change current working directory (``.'').
1712 sys_chdir(struct chdir_args *uap)
1714 struct nlookupdata nd;
1717 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1719 error = kern_chdir(&nd);
1725 * Helper function for raised chroot(2) security function: Refuse if
1726 * any filedescriptors are open directories.
1729 chroot_refuse_vdir_fds(struct filedesc *fdp)
1736 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1737 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1739 vp = (struct vnode *)fp->f_data;
1740 if (vp->v_type != VDIR) {
1751 * This sysctl determines if we will allow a process to chroot(2) if it
1752 * has a directory open:
1753 * 0: disallowed for all processes.
1754 * 1: allowed for processes that were not already chroot(2)'ed.
1755 * 2: allowed for all processes.
1758 static int chroot_allow_open_directories = 1;
1760 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1761 &chroot_allow_open_directories, 0, "");
1764 * chroot to the specified namecache entry. We obtain the vp from the
1765 * namecache data. The passed ncp must be locked and referenced and will
1766 * remain locked and referenced on return.
1769 kern_chroot(struct nchandle *nch)
1771 struct thread *td = curthread;
1772 struct proc *p = td->td_proc;
1773 struct filedesc *fdp = p->p_fd;
1778 * Only privileged user can chroot
1780 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1785 * Disallow open directory descriptors (fchdir() breakouts).
1787 if (chroot_allow_open_directories == 0 ||
1788 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1789 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1792 if ((vp = nch->ncp->nc_vp) == NULL)
1795 if ((error = vget(vp, LK_SHARED)) != 0)
1799 * Check the validity of vp as a directory to change to and
1800 * associate it with rdir/jdir.
1802 error = checkvp_chdir(vp, td);
1803 vn_unlock(vp); /* leave reference intact */
1805 vrele(fdp->fd_rdir);
1806 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1807 cache_drop(&fdp->fd_nrdir);
1808 cache_copy(nch, &fdp->fd_nrdir);
1809 if (fdp->fd_jdir == NULL) {
1812 cache_copy(nch, &fdp->fd_njdir);
1821 * chroot_args(char *path)
1823 * Change notion of root (``/'') directory.
1826 sys_chroot(struct chroot_args *uap)
1828 struct thread *td __debugvar = curthread;
1829 struct nlookupdata nd;
1832 KKASSERT(td->td_proc);
1833 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1835 nd.nl_flags |= NLC_EXEC;
1836 error = nlookup(&nd);
1838 error = kern_chroot(&nd.nl_nch);
1845 sys_chroot_kernel(struct chroot_kernel_args *uap)
1847 struct thread *td = curthread;
1848 struct nlookupdata nd;
1849 struct nchandle *nch;
1853 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1857 error = nlookup(&nd);
1863 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1867 if ((vp = nch->ncp->nc_vp) == NULL) {
1872 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1875 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1876 vfs_cache_setroot(vp, cache_hold(nch));
1885 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1886 * determine whether it is legal to chdir to the vnode. The vnode's state
1887 * is not changed by this call.
1890 checkvp_chdir(struct vnode *vp, struct thread *td)
1894 if (vp->v_type != VDIR)
1897 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1902 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1904 struct thread *td = curthread;
1905 struct proc *p = td->td_proc;
1906 struct lwp *lp = td->td_lwp;
1907 struct filedesc *fdp = p->p_fd;
1912 int type, indx, error = 0;
1915 if ((oflags & O_ACCMODE) == O_ACCMODE)
1917 flags = FFLAGS(oflags);
1918 error = falloc(lp, &nfp, NULL);
1922 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1925 * XXX p_dupfd is a real mess. It allows a device to return a
1926 * file descriptor to be duplicated rather then doing the open
1932 * Call vn_open() to do the lookup and assign the vnode to the
1933 * file pointer. vn_open() does not change the ref count on fp
1934 * and the vnode, on success, will be inherited by the file pointer
1937 * Request a shared lock on the vnode if possible.
1939 nd->nl_flags |= NLC_LOCKVP;
1940 if ((flags & (O_CREAT|O_TRUNC)) == 0)
1941 nd->nl_flags |= NLC_SHAREDLOCK;
1943 error = vn_open(nd, fp, flags, cmode);
1948 * handle special fdopen() case. bleh. dupfdopen() is
1949 * responsible for dropping the old contents of ofiles[indx]
1952 * Note that fsetfd() will add a ref to fp which represents
1953 * the fd_files[] assignment. We must still drop our
1956 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1957 if (fdalloc(p, 0, &indx) == 0) {
1958 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1961 fdrop(fp); /* our ref */
1964 fsetfd(fdp, NULL, indx);
1967 fdrop(fp); /* our ref */
1968 if (error == ERESTART)
1974 * ref the vnode for ourselves so it can't be ripped out from under
1975 * is. XXX need an ND flag to request that the vnode be returned
1978 * Reserve a file descriptor but do not assign it until the open
1981 vp = (struct vnode *)fp->f_data;
1983 if ((error = fdalloc(p, 0, &indx)) != 0) {
1990 * If no error occurs the vp will have been assigned to the file
1995 if (flags & (O_EXLOCK | O_SHLOCK)) {
1996 lf.l_whence = SEEK_SET;
1999 if (flags & O_EXLOCK)
2000 lf.l_type = F_WRLCK;
2002 lf.l_type = F_RDLCK;
2003 if (flags & FNONBLOCK)
2008 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2010 * lock request failed. Clean up the reserved
2014 fsetfd(fdp, NULL, indx);
2018 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
2022 * Assert that all regular file vnodes were created with a object.
2024 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
2025 ("open: regular file has no backing object after vn_open"));
2031 * release our private reference, leaving the one associated with the
2032 * descriptor table intact.
2034 if (oflags & O_CLOEXEC)
2035 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
2036 fsetfd(fdp, fp, indx);
2043 * open_args(char *path, int flags, int mode)
2045 * Check permissions, allocate an open file structure,
2046 * and call the device open routine if any.
2049 sys_open(struct open_args *uap)
2051 struct nlookupdata nd;
2054 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2056 error = kern_open(&nd, uap->flags,
2057 uap->mode, &uap->sysmsg_result);
2064 * openat_args(int fd, char *path, int flags, int mode)
2067 sys_openat(struct openat_args *uap)
2069 struct nlookupdata nd;
2073 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2075 error = kern_open(&nd, uap->flags, uap->mode,
2076 &uap->sysmsg_result);
2078 nlookup_done_at(&nd, fp);
2083 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2085 struct thread *td = curthread;
2086 struct proc *p = td->td_proc;
2095 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2096 vattr.va_rmajor = rmajor;
2097 vattr.va_rminor = rminor;
2099 switch (mode & S_IFMT) {
2100 case S_IFMT: /* used by badsect to flag bad sectors */
2101 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2102 vattr.va_type = VBAD;
2105 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2106 vattr.va_type = VCHR;
2109 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2110 vattr.va_type = VBLK;
2113 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2116 case S_IFDIR: /* special directories support for HAMMER */
2117 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2118 vattr.va_type = VDIR;
2129 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2130 if ((error = nlookup(nd)) != 0)
2132 if (nd->nl_nch.ncp->nc_vp)
2134 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2138 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2139 nd->nl_cred, NAMEI_CREATE);
2142 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2143 &vp, nd->nl_cred, &vattr);
2151 * mknod_args(char *path, int mode, int dev)
2153 * Create a special file.
2156 sys_mknod(struct mknod_args *uap)
2158 struct nlookupdata nd;
2161 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2163 error = kern_mknod(&nd, uap->mode,
2164 umajor(uap->dev), uminor(uap->dev));
2171 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2173 * Create a special file. The path is relative to the directory associated
2177 sys_mknodat(struct mknodat_args *uap)
2179 struct nlookupdata nd;
2183 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2185 error = kern_mknod(&nd, uap->mode,
2186 umajor(uap->dev), uminor(uap->dev));
2188 nlookup_done_at(&nd, fp);
2193 kern_mkfifo(struct nlookupdata *nd, int mode)
2195 struct thread *td = curthread;
2196 struct proc *p = td->td_proc;
2203 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2204 if ((error = nlookup(nd)) != 0)
2206 if (nd->nl_nch.ncp->nc_vp)
2208 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2212 vattr.va_type = VFIFO;
2213 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2215 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2222 * mkfifo_args(char *path, int mode)
2224 * Create a named pipe.
2227 sys_mkfifo(struct mkfifo_args *uap)
2229 struct nlookupdata nd;
2232 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2234 error = kern_mkfifo(&nd, uap->mode);
2240 * mkfifoat_args(int fd, char *path, mode_t mode)
2242 * Create a named pipe. The path is relative to the directory associated
2246 sys_mkfifoat(struct mkfifoat_args *uap)
2248 struct nlookupdata nd;
2252 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2254 error = kern_mkfifo(&nd, uap->mode);
2255 nlookup_done_at(&nd, fp);
2259 static int hardlink_check_uid = 0;
2260 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2261 &hardlink_check_uid, 0,
2262 "Unprivileged processes cannot create hard links to files owned by other "
2264 static int hardlink_check_gid = 0;
2265 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2266 &hardlink_check_gid, 0,
2267 "Unprivileged processes cannot create hard links to files owned by other "
2271 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2277 * Shortcut if disabled
2279 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2283 * Privileged user can always hardlink
2285 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2289 * Otherwise only if the originating file is owned by the
2290 * same user or group. Note that any group is allowed if
2291 * the file is owned by the caller.
2293 error = VOP_GETATTR(vp, &va);
2297 if (hardlink_check_uid) {
2298 if (cred->cr_uid != va.va_uid)
2302 if (hardlink_check_gid) {
2303 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2311 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2313 struct thread *td = curthread;
2318 * Lookup the source and obtained a locked vnode.
2320 * You may only hardlink a file which you have write permission
2321 * on or which you own.
2323 * XXX relookup on vget failure / race ?
2326 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2327 if ((error = nlookup(nd)) != 0)
2329 vp = nd->nl_nch.ncp->nc_vp;
2330 KKASSERT(vp != NULL);
2331 if (vp->v_type == VDIR)
2332 return (EPERM); /* POSIX */
2333 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2335 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2339 * Unlock the source so we can lookup the target without deadlocking
2340 * (XXX vp is locked already, possible other deadlock?). The target
2343 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2344 nd->nl_flags &= ~NLC_NCPISLOCKED;
2345 cache_unlock(&nd->nl_nch);
2348 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2349 if ((error = nlookup(linknd)) != 0) {
2353 if (linknd->nl_nch.ncp->nc_vp) {
2357 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2364 * Finally run the new API VOP.
2366 error = can_hardlink(vp, td, td->td_ucred);
2368 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2369 vp, linknd->nl_cred);
2376 * link_args(char *path, char *link)
2378 * Make a hard file link.
2381 sys_link(struct link_args *uap)
2383 struct nlookupdata nd, linknd;
2386 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2388 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2390 error = kern_link(&nd, &linknd);
2391 nlookup_done(&linknd);
2398 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2400 * Make a hard file link. The path1 argument is relative to the directory
2401 * associated with fd1, and similarly the path2 argument is relative to
2402 * the directory associated with fd2.
2405 sys_linkat(struct linkat_args *uap)
2407 struct nlookupdata nd, linknd;
2408 struct file *fp1, *fp2;
2411 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2412 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2414 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2415 uap->path2, UIO_USERSPACE, 0);
2417 error = kern_link(&nd, &linknd);
2418 nlookup_done_at(&linknd, fp2);
2420 nlookup_done_at(&nd, fp1);
2425 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2433 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2434 if ((error = nlookup(nd)) != 0)
2436 if (nd->nl_nch.ncp->nc_vp)
2438 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2442 vattr.va_mode = mode;
2443 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2450 * symlink(char *path, char *link)
2452 * Make a symbolic link.
2455 sys_symlink(struct symlink_args *uap)
2457 struct thread *td = curthread;
2458 struct nlookupdata nd;
2463 path = objcache_get(namei_oc, M_WAITOK);
2464 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2466 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2468 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2469 error = kern_symlink(&nd, path, mode);
2473 objcache_put(namei_oc, path);
2478 * symlinkat_args(char *path1, int fd, char *path2)
2480 * Make a symbolic link. The path2 argument is relative to the directory
2481 * associated with fd.
2484 sys_symlinkat(struct symlinkat_args *uap)
2486 struct thread *td = curthread;
2487 struct nlookupdata nd;
2493 path1 = objcache_get(namei_oc, M_WAITOK);
2494 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2496 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2499 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2500 error = kern_symlink(&nd, path1, mode);
2502 nlookup_done_at(&nd, fp);
2504 objcache_put(namei_oc, path1);
2509 * undelete_args(char *path)
2511 * Delete a whiteout from the filesystem.
2514 sys_undelete(struct undelete_args *uap)
2516 struct nlookupdata nd;
2519 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2521 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2523 error = nlookup(&nd);
2525 error = ncp_writechk(&nd.nl_nch);
2527 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2535 kern_unlink(struct nlookupdata *nd)
2540 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2541 if ((error = nlookup(nd)) != 0)
2543 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2545 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2550 * unlink_args(char *path)
2552 * Delete a name from the filesystem.
2555 sys_unlink(struct unlink_args *uap)
2557 struct nlookupdata nd;
2560 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2562 error = kern_unlink(&nd);
2569 * unlinkat_args(int fd, char *path, int flags)
2571 * Delete the file or directory entry pointed to by fd/path.
2574 sys_unlinkat(struct unlinkat_args *uap)
2576 struct nlookupdata nd;
2580 if (uap->flags & ~AT_REMOVEDIR)
2583 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2585 if (uap->flags & AT_REMOVEDIR)
2586 error = kern_rmdir(&nd);
2588 error = kern_unlink(&nd);
2590 nlookup_done_at(&nd, fp);
2595 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2597 struct thread *td = curthread;
2598 struct proc *p = td->td_proc;
2605 fp = holdfp(p->p_fd, fd, -1);
2608 if (fp->f_type != DTYPE_VNODE) {
2612 vp = (struct vnode *)fp->f_data;
2616 spin_lock(&fp->f_spin);
2617 new_offset = fp->f_offset + offset;
2621 error = VOP_GETATTR(vp, &vattr);
2622 spin_lock(&fp->f_spin);
2623 new_offset = offset + vattr.va_size;
2626 new_offset = offset;
2628 spin_lock(&fp->f_spin);
2633 spin_lock(&fp->f_spin);
2638 * Validate the seek position. Negative offsets are not allowed
2639 * for regular files or directories.
2641 * Normally we would also not want to allow negative offsets for
2642 * character and block-special devices. However kvm addresses
2643 * on 64 bit architectures might appear to be negative and must
2647 if (new_offset < 0 &&
2648 (vp->v_type == VREG || vp->v_type == VDIR)) {
2651 fp->f_offset = new_offset;
2654 *res = fp->f_offset;
2655 spin_unlock(&fp->f_spin);
2662 * lseek_args(int fd, int pad, off_t offset, int whence)
2664 * Reposition read/write file offset.
2667 sys_lseek(struct lseek_args *uap)
2671 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2672 &uap->sysmsg_offset);
2678 * Check if current process can access given file. amode is a bitmask of *_OK
2679 * access bits. flags is a bitmask of AT_* flags.
2682 kern_access(struct nlookupdata *nd, int amode, int flags)
2687 if (flags & ~AT_EACCESS)
2689 nd->nl_flags |= NLC_SHAREDLOCK;
2690 if ((error = nlookup(nd)) != 0)
2693 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2697 /* Flags == 0 means only check for existence. */
2706 if ((mode & VWRITE) == 0 ||
2707 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2708 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2711 * If the file handle is stale we have to re-resolve the
2712 * entry with the ncp held exclusively. This is a hack
2715 if (error == ESTALE) {
2717 cache_unlock(&nd->nl_nch);
2718 cache_lock(&nd->nl_nch);
2719 cache_setunresolved(&nd->nl_nch);
2720 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2733 * access_args(char *path, int flags)
2735 * Check access permissions.
2738 sys_access(struct access_args *uap)
2740 struct nlookupdata nd;
2743 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2745 error = kern_access(&nd, uap->flags, 0);
2752 * eaccess_args(char *path, int flags)
2754 * Check access permissions.
2757 sys_eaccess(struct eaccess_args *uap)
2759 struct nlookupdata nd;
2762 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2764 error = kern_access(&nd, uap->flags, AT_EACCESS);
2771 * faccessat_args(int fd, char *path, int amode, int flags)
2773 * Check access permissions.
2776 sys_faccessat(struct faccessat_args *uap)
2778 struct nlookupdata nd;
2782 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2785 error = kern_access(&nd, uap->amode, uap->flags);
2786 nlookup_done_at(&nd, fp);
2791 kern_stat(struct nlookupdata *nd, struct stat *st)
2796 nd->nl_flags |= NLC_SHAREDLOCK;
2797 if ((error = nlookup(nd)) != 0)
2800 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2803 if ((error = vget(vp, LK_SHARED)) != 0)
2805 error = vn_stat(vp, st, nd->nl_cred);
2808 * If the file handle is stale we have to re-resolve the
2809 * entry with the ncp held exclusively. This is a hack
2812 if (error == ESTALE) {
2814 cache_unlock(&nd->nl_nch);
2815 cache_lock(&nd->nl_nch);
2816 cache_setunresolved(&nd->nl_nch);
2817 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2827 * stat_args(char *path, struct stat *ub)
2829 * Get file status; this version follows links.
2832 sys_stat(struct stat_args *uap)
2834 struct nlookupdata nd;
2838 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2840 error = kern_stat(&nd, &st);
2842 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2849 * lstat_args(char *path, struct stat *ub)
2851 * Get file status; this version does not follow links.
2854 sys_lstat(struct lstat_args *uap)
2856 struct nlookupdata nd;
2860 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2862 error = kern_stat(&nd, &st);
2864 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2871 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2873 * Get status of file pointed to by fd/path.
2876 sys_fstatat(struct fstatat_args *uap)
2878 struct nlookupdata nd;
2884 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2887 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2889 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2890 UIO_USERSPACE, flags);
2892 error = kern_stat(&nd, &st);
2894 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2896 nlookup_done_at(&nd, fp);
2901 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
2903 struct nlookupdata nd;
2908 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
2910 error = nlookup(&nd);
2912 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2915 error = VOP_PATHCONF(vp, name, sysmsg_regp);
2922 * pathconf_Args(char *path, int name)
2924 * Get configurable pathname variables.
2927 sys_pathconf(struct pathconf_args *uap)
2929 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
2934 * lpathconf_Args(char *path, int name)
2936 * Get configurable pathname variables, but don't follow symlinks.
2939 sys_lpathconf(struct lpathconf_args *uap)
2941 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
2946 * kern_readlink isn't properly split yet. There is a copyin burried
2947 * in VOP_READLINK().
2950 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2952 struct thread *td = curthread;
2958 nd->nl_flags |= NLC_SHAREDLOCK;
2959 if ((error = nlookup(nd)) != 0)
2961 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2964 if (vp->v_type != VLNK) {
2967 aiov.iov_base = buf;
2968 aiov.iov_len = count;
2969 auio.uio_iov = &aiov;
2970 auio.uio_iovcnt = 1;
2971 auio.uio_offset = 0;
2972 auio.uio_rw = UIO_READ;
2973 auio.uio_segflg = UIO_USERSPACE;
2975 auio.uio_resid = count;
2976 error = VOP_READLINK(vp, &auio, td->td_ucred);
2979 *res = count - auio.uio_resid;
2984 * readlink_args(char *path, char *buf, int count)
2986 * Return target name of a symbolic link.
2989 sys_readlink(struct readlink_args *uap)
2991 struct nlookupdata nd;
2994 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2996 error = kern_readlink(&nd, uap->buf, uap->count,
2997 &uap->sysmsg_result);
3004 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3006 * Return target name of a symbolic link. The path is relative to the
3007 * directory associated with fd.
3010 sys_readlinkat(struct readlinkat_args *uap)
3012 struct nlookupdata nd;
3016 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3018 error = kern_readlink(&nd, uap->buf, uap->bufsize,
3019 &uap->sysmsg_result);
3021 nlookup_done_at(&nd, fp);
3026 setfflags(struct vnode *vp, int flags)
3028 struct thread *td = curthread;
3033 * Prevent non-root users from setting flags on devices. When
3034 * a device is reused, users can retain ownership of the device
3035 * if they are allowed to set flags and programs assume that
3036 * chown can't fail when done as root.
3038 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
3039 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
3043 * note: vget is required for any operation that might mod the vnode
3044 * so VINACTIVE is properly cleared.
3046 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3048 vattr.va_flags = flags;
3049 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3056 * chflags(char *path, int flags)
3058 * Change flags of a file given a path name.
3061 sys_chflags(struct chflags_args *uap)
3063 struct nlookupdata nd;
3068 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3070 error = nlookup(&nd);
3072 error = ncp_writechk(&nd.nl_nch);
3074 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3077 error = setfflags(vp, uap->flags);
3084 * lchflags(char *path, int flags)
3086 * Change flags of a file given a path name, but don't follow symlinks.
3089 sys_lchflags(struct lchflags_args *uap)
3091 struct nlookupdata nd;
3096 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3098 error = nlookup(&nd);
3100 error = ncp_writechk(&nd.nl_nch);
3102 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3105 error = setfflags(vp, uap->flags);
3112 * fchflags_args(int fd, int flags)
3114 * Change flags of a file given a file descriptor.
3117 sys_fchflags(struct fchflags_args *uap)
3119 struct thread *td = curthread;
3120 struct proc *p = td->td_proc;
3124 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3126 if (fp->f_nchandle.ncp)
3127 error = ncp_writechk(&fp->f_nchandle);
3129 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3135 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3136 * change flags given a pathname relative to a filedescriptor
3138 int sys_chflagsat(struct chflagsat_args *uap)
3140 struct nlookupdata nd;
3146 if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3149 lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3152 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags);
3154 error = nlookup(&nd);
3156 error = ncp_writechk(&nd.nl_nch);
3158 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3159 nlookup_done_at(&nd, fp);
3161 error = setfflags(vp, uap->flags);
3169 setfmode(struct vnode *vp, int mode)
3171 struct thread *td = curthread;
3176 * note: vget is required for any operation that might mod the vnode
3177 * so VINACTIVE is properly cleared.
3179 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3181 vattr.va_mode = mode & ALLPERMS;
3182 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3183 cache_inval_wxok(vp);
3190 kern_chmod(struct nlookupdata *nd, int mode)
3195 if ((error = nlookup(nd)) != 0)
3197 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3199 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3200 error = setfmode(vp, mode);
3206 * chmod_args(char *path, int mode)
3208 * Change mode of a file given path name.
3211 sys_chmod(struct chmod_args *uap)
3213 struct nlookupdata nd;
3216 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3218 error = kern_chmod(&nd, uap->mode);
3224 * lchmod_args(char *path, int mode)
3226 * Change mode of a file given path name (don't follow links.)
3229 sys_lchmod(struct lchmod_args *uap)
3231 struct nlookupdata nd;
3234 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3236 error = kern_chmod(&nd, uap->mode);
3242 * fchmod_args(int fd, int mode)
3244 * Change mode of a file given a file descriptor.
3247 sys_fchmod(struct fchmod_args *uap)
3249 struct thread *td = curthread;
3250 struct proc *p = td->td_proc;
3254 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3256 if (fp->f_nchandle.ncp)
3257 error = ncp_writechk(&fp->f_nchandle);
3259 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3265 * fchmodat_args(char *path, int mode)
3267 * Change mode of a file pointed to by fd/path.
3270 sys_fchmodat(struct fchmodat_args *uap)
3272 struct nlookupdata nd;
3277 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3279 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3281 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3282 UIO_USERSPACE, flags);
3284 error = kern_chmod(&nd, uap->mode);
3285 nlookup_done_at(&nd, fp);
3290 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3292 struct thread *td = curthread;
3300 * note: vget is required for any operation that might mod the vnode
3301 * so VINACTIVE is properly cleared.
3303 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3304 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3306 o_uid = vattr.va_uid;
3307 o_gid = vattr.va_gid;
3308 size = vattr.va_size;
3313 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3322 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3323 VFS_ACCOUNT(mp, uid, gid, size);
3330 kern_chown(struct nlookupdata *nd, int uid, int gid)
3335 if ((error = nlookup(nd)) != 0)
3337 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3339 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3340 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3346 * chown(char *path, int uid, int gid)
3348 * Set ownership given a path name.
3351 sys_chown(struct chown_args *uap)
3353 struct nlookupdata nd;
3356 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3358 error = kern_chown(&nd, uap->uid, uap->gid);
3364 * lchown_args(char *path, int uid, int gid)
3366 * Set ownership given a path name, do not cross symlinks.
3369 sys_lchown(struct lchown_args *uap)
3371 struct nlookupdata nd;
3374 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3376 error = kern_chown(&nd, uap->uid, uap->gid);
3382 * fchown_args(int fd, int uid, int gid)
3384 * Set ownership given a file descriptor.
3387 sys_fchown(struct fchown_args *uap)
3389 struct thread *td = curthread;
3390 struct proc *p = td->td_proc;
3394 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3396 if (fp->f_nchandle.ncp)
3397 error = ncp_writechk(&fp->f_nchandle);
3399 error = setfown(p->p_fd->fd_ncdir.mount,
3400 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3406 * fchownat(int fd, char *path, int uid, int gid, int flags)
3408 * Set ownership of file pointed to by fd/path.
3411 sys_fchownat(struct fchownat_args *uap)
3413 struct nlookupdata nd;
3418 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3420 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3422 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3423 UIO_USERSPACE, flags);
3425 error = kern_chown(&nd, uap->uid, uap->gid);
3426 nlookup_done_at(&nd, fp);
3432 getutimes(struct timeval *tvp, struct timespec *tsp)
3434 struct timeval tv[2];
3439 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3442 if ((error = itimerfix(tvp)) != 0)
3444 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3445 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3451 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3453 struct timespec tsnow;
3467 if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3469 if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3472 if (newts[0].tv_nsec == UTIME_OMIT)
3473 newts[0].tv_sec = VNOVAL;
3474 else if (newts[0].tv_nsec == UTIME_NOW)
3476 else if ((error = itimespecfix(&newts[0])) != 0)
3479 if (newts[1].tv_nsec == UTIME_OMIT)
3480 newts[1].tv_sec = VNOVAL;
3481 else if (newts[1].tv_nsec == UTIME_NOW)
3483 else if ((error = itimespecfix(&newts[1])) != 0)
3490 setutimes(struct vnode *vp, struct vattr *vattr,
3491 const struct timespec *ts, int nullflag)
3493 struct thread *td = curthread;
3497 vattr->va_atime = ts[0];
3498 vattr->va_mtime = ts[1];
3500 vattr->va_vaflags |= VA_UTIMES_NULL;
3501 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3507 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3509 struct timespec ts[2];
3513 if ((error = getutimes(tptr, ts)) != 0)
3516 error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3521 * utimes_args(char *path, struct timeval *tptr)
3523 * Set the access and modification times of a file.
3526 sys_utimes(struct utimes_args *uap)
3528 struct timeval tv[2];
3529 struct nlookupdata nd;
3533 error = copyin(uap->tptr, tv, sizeof(tv));
3537 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3539 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3545 * lutimes_args(char *path, struct timeval *tptr)
3547 * Set the access and modification times of a file.
3550 sys_lutimes(struct lutimes_args *uap)
3552 struct timeval tv[2];
3553 struct nlookupdata nd;
3557 error = copyin(uap->tptr, tv, sizeof(tv));
3561 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3563 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3569 * Set utimes on a file descriptor. The creds used to open the
3570 * file are used to determine whether the operation is allowed
3574 kern_futimens(int fd, struct timespec *ts)
3576 struct thread *td = curthread;
3577 struct proc *p = td->td_proc;
3578 struct timespec newts[2];
3585 error = getutimens(ts, newts, &nullflag);
3588 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3590 if (fp->f_nchandle.ncp)
3591 error = ncp_writechk(&fp->f_nchandle);
3594 error = vget(vp, LK_EXCLUSIVE);
3596 error = VOP_GETATTR(vp, &vattr);
3598 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3602 error = setutimes(vp, &vattr, newts, nullflag);
3612 * futimens_args(int fd, struct timespec *ts)
3614 * Set the access and modification times of a file.
3617 sys_futimens(struct futimens_args *uap)
3619 struct timespec ts[2];
3623 error = copyin(uap->ts, ts, sizeof(ts));
3627 error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3632 kern_futimes(int fd, struct timeval *tptr)
3634 struct timespec ts[2];
3638 if ((error = getutimes(tptr, ts)) != 0)
3641 error = kern_futimens(fd, tptr ? ts : NULL);
3646 * futimes_args(int fd, struct timeval *tptr)
3648 * Set the access and modification times of a file.
3651 sys_futimes(struct futimes_args *uap)
3653 struct timeval tv[2];
3657 error = copyin(uap->tptr, tv, sizeof(tv));
3661 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3666 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3668 struct timespec newts[2];
3674 if (flags & ~AT_SYMLINK_NOFOLLOW)
3677 error = getutimens(ts, newts, &nullflag);
3681 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3682 if ((error = nlookup(nd)) != 0)
3684 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3686 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3688 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3689 error = vget(vp, LK_EXCLUSIVE);
3691 error = setutimes(vp, &vattr, newts, nullflag);
3700 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3702 * Set file access and modification times of a file.
3705 sys_utimensat(struct utimensat_args *uap)
3707 struct timespec ts[2];
3708 struct nlookupdata nd;
3714 error = copyin(uap->ts, ts, sizeof(ts));
3719 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3720 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3721 UIO_USERSPACE, flags);
3723 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3724 nlookup_done_at(&nd, fp);
3729 kern_truncate(struct nlookupdata *nd, off_t length)
3736 uint64_t old_size = 0;
3740 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3741 if ((error = nlookup(nd)) != 0)
3743 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3745 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3747 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3752 if (vp->v_type == VDIR) {
3756 if (vfs_quota_enabled) {
3757 error = VOP_GETATTR(vp, &vattr);
3758 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3761 old_size = vattr.va_size;
3764 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3766 vattr.va_size = length;
3767 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3768 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3776 * truncate(char *path, int pad, off_t length)
3778 * Truncate a file given its path name.
3781 sys_truncate(struct truncate_args *uap)
3783 struct nlookupdata nd;
3786 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3788 error = kern_truncate(&nd, uap->length);
3794 kern_ftruncate(int fd, off_t length)
3796 struct thread *td = curthread;
3797 struct proc *p = td->td_proc;
3804 uint64_t old_size = 0;
3809 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3811 if (fp->f_nchandle.ncp) {
3812 error = ncp_writechk(&fp->f_nchandle);
3816 if ((fp->f_flag & FWRITE) == 0) {
3820 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3824 vp = (struct vnode *)fp->f_data;
3825 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3826 if (vp->v_type == VDIR) {
3832 if (vfs_quota_enabled) {
3833 error = VOP_GETATTR(vp, &vattr);
3834 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3837 old_size = vattr.va_size;
3840 if ((error = vn_writechk(vp, NULL)) == 0) {
3842 vattr.va_size = length;
3843 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3845 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3854 * ftruncate_args(int fd, int pad, off_t length)
3856 * Truncate a file given a file descriptor.
3859 sys_ftruncate(struct ftruncate_args *uap)
3863 error = kern_ftruncate(uap->fd, uap->length);
3871 * Sync an open file.
3874 sys_fsync(struct fsync_args *uap)
3876 struct thread *td = curthread;
3877 struct proc *p = td->td_proc;
3883 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3885 vp = (struct vnode *)fp->f_data;
3886 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3887 if ((obj = vp->v_object) != NULL) {
3888 if (vp->v_mount == NULL ||
3889 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3890 vm_object_page_clean(obj, 0, 0, 0);
3893 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3894 if (error == 0 && vp->v_mount)
3895 error = buf_fsync(vp);
3903 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3905 struct nchandle fnchd;
3906 struct nchandle tnchd;
3907 struct namecache *ncp;
3916 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3917 if ((error = nlookup(fromnd)) != 0)
3919 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3921 fnchd.mount = fromnd->nl_nch.mount;
3925 * unlock the source nch so we can lookup the target nch without
3926 * deadlocking. The target may or may not exist so we do not check
3927 * for a target vp like kern_mkdir() and other creation functions do.
3929 * The source and target directories are ref'd and rechecked after
3930 * everything is relocked to determine if the source or target file
3933 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3934 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3936 fncp_gen = fromnd->nl_nch.ncp->nc_generation;
3938 cache_unlock(&fromnd->nl_nch);
3940 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3941 if ((error = nlookup(tond)) != 0) {
3945 tncp_gen = tond->nl_nch.ncp->nc_generation;
3947 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3951 tnchd.mount = tond->nl_nch.mount;
3955 * If the source and target are the same there is nothing to do
3957 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3964 * Mount points cannot be renamed or overwritten
3966 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3975 * Relock the source ncp. cache_relock() will deal with any
3976 * deadlocks against the already-locked tond and will also
3977 * make sure both are resolved.
3979 * NOTE AFTER RELOCKING: The source or target ncp may have become
3980 * invalid while they were unlocked, nc_vp and nc_mount could
3983 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3984 &tond->nl_nch, tond->nl_cred);
3985 fromnd->nl_flags |= NLC_NCPISLOCKED;
3988 * If the namecache generation changed for either fromnd or tond,
3991 if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
3992 tond->nl_nch.ncp->nc_generation != tncp_gen) {
3993 kprintf("kern_rename: retry due to gen on: "
3994 "\"%s\" -> \"%s\"\n",
3995 fromnd->nl_nch.ncp->nc_name,
3996 tond->nl_nch.ncp->nc_name);
4003 * If either fromnd or tond are marked destroyed a ripout occured
4004 * out from under us and we must retry.
4006 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
4007 fromnd->nl_nch.ncp->nc_vp == NULL ||
4008 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
4009 kprintf("kern_rename: retry due to ripout on: "
4010 "\"%s\" -> \"%s\"\n",
4011 fromnd->nl_nch.ncp->nc_name,
4012 tond->nl_nch.ncp->nc_name);
4019 * Make sure the parent directories linkages are the same.
4020 * XXX shouldn't be needed any more w/ generation check above.
4022 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
4023 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
4030 * Both the source and target must be within the same filesystem and
4031 * in the same filesystem as their parent directories within the
4032 * namecache topology.
4034 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4037 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
4038 mp != tond->nl_nch.mount) {
4045 * Make sure the mount point is writable
4047 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4054 * If the target exists and either the source or target is a directory,
4055 * then both must be directories.
4057 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4060 if (tond->nl_nch.ncp->nc_vp) {
4061 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4063 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4064 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4066 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4072 * You cannot rename a source into itself or a subdirectory of itself.
4073 * We check this by travsersing the target directory upwards looking
4074 * for a match against the source.
4079 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4080 if (fromnd->nl_nch.ncp == ncp) {
4091 * Even though the namespaces are different, they may still represent
4092 * hardlinks to the same file. The filesystem might have a hard time
4093 * with this so we issue a NREMOVE of the source instead of a NRENAME
4094 * when we detect the situation.
4097 fdvp = fromnd->nl_dvp;
4098 tdvp = tond->nl_dvp;
4099 if (fdvp == NULL || tdvp == NULL) {
4101 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4102 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4105 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4106 fdvp, tdvp, tond->nl_cred);
4113 * rename_args(char *from, char *to)
4115 * Rename files. Source and destination must either both be directories,
4116 * or both not be directories. If target is a directory, it must be empty.
4119 sys_rename(struct rename_args *uap)
4121 struct nlookupdata fromnd, tond;
4125 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4127 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4129 error = kern_rename(&fromnd, &tond);
4130 nlookup_done(&tond);
4132 nlookup_done(&fromnd);
4133 } while (error == EAGAIN);
4138 * renameat_args(int oldfd, char *old, int newfd, char *new)
4140 * Rename files using paths relative to the directories associated with
4141 * oldfd and newfd. Source and destination must either both be directories,
4142 * or both not be directories. If target is a directory, it must be empty.
4145 sys_renameat(struct renameat_args *uap)
4147 struct nlookupdata oldnd, newnd;
4148 struct file *oldfp, *newfp;
4152 error = nlookup_init_at(&oldnd, &oldfp,
4153 uap->oldfd, uap->old,
4156 error = nlookup_init_at(&newnd, &newfp,
4157 uap->newfd, uap->new,
4160 error = kern_rename(&oldnd, &newnd);
4161 nlookup_done_at(&newnd, newfp);
4163 nlookup_done_at(&oldnd, oldfp);
4164 } while (error == EAGAIN);
4169 kern_mkdir(struct nlookupdata *nd, int mode)
4171 struct thread *td = curthread;
4172 struct proc *p = td->td_proc;
4178 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4179 if ((error = nlookup(nd)) != 0)
4182 if (nd->nl_nch.ncp->nc_vp)
4184 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4187 vattr.va_type = VDIR;
4188 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4191 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4198 * mkdir_args(char *path, int mode)
4200 * Make a directory file.
4203 sys_mkdir(struct mkdir_args *uap)
4205 struct nlookupdata nd;
4208 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4210 error = kern_mkdir(&nd, uap->mode);
4216 * mkdirat_args(int fd, char *path, mode_t mode)
4218 * Make a directory file. The path is relative to the directory associated
4222 sys_mkdirat(struct mkdirat_args *uap)
4224 struct nlookupdata nd;
4228 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4230 error = kern_mkdir(&nd, uap->mode);
4231 nlookup_done_at(&nd, fp);
4236 kern_rmdir(struct nlookupdata *nd)
4241 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4242 if ((error = nlookup(nd)) != 0)
4246 * Do not allow directories representing mount points to be
4247 * deleted, even if empty. Check write perms on mount point
4248 * in case the vnode is aliased (aka nullfs).
4250 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4252 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4254 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4259 * rmdir_args(char *path)
4261 * Remove a directory file.
4264 sys_rmdir(struct rmdir_args *uap)
4266 struct nlookupdata nd;
4269 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4271 error = kern_rmdir(&nd);
4277 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4278 enum uio_seg direction)
4280 struct thread *td = curthread;
4281 struct proc *p = td->td_proc;
4289 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4291 if ((fp->f_flag & FREAD) == 0) {
4295 vp = (struct vnode *)fp->f_data;
4296 if (vp->v_type != VDIR) {
4300 aiov.iov_base = buf;
4301 aiov.iov_len = count;
4302 auio.uio_iov = &aiov;
4303 auio.uio_iovcnt = 1;
4304 auio.uio_rw = UIO_READ;
4305 auio.uio_segflg = direction;
4307 auio.uio_resid = count;
4308 loff = auio.uio_offset = fp->f_offset;
4309 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4310 fp->f_offset = auio.uio_offset;
4315 * WARNING! *basep may not be wide enough to accomodate the
4316 * seek offset. XXX should we hack this to return the upper 32 bits
4317 * for offsets greater then 4G?
4320 *basep = (long)loff;
4322 *res = count - auio.uio_resid;
4329 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4331 * Read a block of directory entries in a file system independent format.
4334 sys_getdirentries(struct getdirentries_args *uap)
4339 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4340 &uap->sysmsg_result, UIO_USERSPACE);
4342 if (error == 0 && uap->basep)
4343 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4348 * getdents_args(int fd, char *buf, size_t count)
4351 sys_getdents(struct getdents_args *uap)
4355 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4356 &uap->sysmsg_result, UIO_USERSPACE);
4362 * Set the mode mask for creation of filesystem nodes.
4364 * umask(int newmask)
4367 sys_umask(struct umask_args *uap)
4369 struct thread *td = curthread;
4370 struct proc *p = td->td_proc;
4371 struct filedesc *fdp;
4374 uap->sysmsg_result = fdp->fd_cmask;
4375 fdp->fd_cmask = uap->newmask & ALLPERMS;
4380 * revoke(char *path)
4382 * Void all references to file by ripping underlying filesystem
4386 sys_revoke(struct revoke_args *uap)
4388 struct nlookupdata nd;
4395 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4397 error = nlookup(&nd);
4399 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4400 cred = crhold(nd.nl_cred);
4404 error = VOP_GETATTR(vp, &vattr);
4405 if (error == 0 && cred->cr_uid != vattr.va_uid)
4406 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4407 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4409 error = vrevoke(vp, cred);
4410 } else if (error == 0) {
4411 error = vrevoke(vp, cred);
4421 * getfh_args(char *fname, fhandle_t *fhp)
4423 * Get (NFS) file handle
4425 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4426 * mount. This allows nullfs mounts to be explicitly exported.
4428 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4430 * nullfs mounts of subdirectories are not safe. That is, it will
4431 * work, but you do not really have protection against access to
4432 * the related parent directories.
4435 sys_getfh(struct getfh_args *uap)
4437 struct thread *td = curthread;
4438 struct nlookupdata nd;
4445 * Must be super user
4447 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4451 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4453 error = nlookup(&nd);
4455 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4456 mp = nd.nl_nch.mount;
4459 bzero(&fh, sizeof(fh));
4460 fh.fh_fsid = mp->mnt_stat.f_fsid;
4461 error = VFS_VPTOFH(vp, &fh.fh_fid);
4464 error = copyout(&fh, uap->fhp, sizeof(fh));
4470 * fhopen_args(const struct fhandle *u_fhp, int flags)
4472 * syscall for the rpc.lockd to use to translate a NFS file handle into
4473 * an open descriptor.
4475 * warning: do not remove the priv_check() call or this becomes one giant
4479 sys_fhopen(struct fhopen_args *uap)
4481 struct thread *td = curthread;
4482 struct filedesc *fdp = td->td_proc->p_fd;
4487 struct vattr *vap = &vat;
4489 int fmode, mode, error = 0, type;
4495 * Must be super user
4497 error = priv_check(td, PRIV_ROOT);
4501 fmode = FFLAGS(uap->flags);
4504 * Why not allow a non-read/write open for our lockd?
4506 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4508 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4513 * Find the mount point
4515 mp = vfs_getvfs(&fhp.fh_fsid);
4520 /* now give me my vnode, it gets returned to me locked */
4521 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4525 * from now on we have to make sure not
4526 * to forget about the vnode
4527 * any error that causes an abort must vput(vp)
4528 * just set error = err and 'goto bad;'.
4534 if (vp->v_type == VLNK) {
4538 if (vp->v_type == VSOCK) {
4543 if (fmode & (FWRITE | O_TRUNC)) {
4544 if (vp->v_type == VDIR) {
4548 error = vn_writechk(vp, NULL);
4556 error = VOP_ACCESS(vp, mode, td->td_ucred);
4560 if (fmode & O_TRUNC) {
4561 vn_unlock(vp); /* XXX */
4562 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4565 error = VOP_SETATTR(vp, vap, td->td_ucred);
4571 * VOP_OPEN needs the file pointer so it can potentially override
4574 * WARNING! no f_nchandle will be associated when fhopen()ing a
4577 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4581 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4584 * setting f_ops this way prevents VOP_CLOSE from being
4585 * called or fdrop() releasing the vp from v_data. Since
4586 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4588 fp->f_ops = &badfileops;
4594 * The fp is given its own reference, we still have our ref and lock.
4596 * Assert that all regular files must be created with a VM object.
4598 if (vp->v_type == VREG && vp->v_object == NULL) {
4599 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4604 * The open was successful. Handle any locking requirements.
4606 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4607 lf.l_whence = SEEK_SET;
4610 if (fmode & O_EXLOCK)
4611 lf.l_type = F_WRLCK;
4613 lf.l_type = F_RDLCK;
4614 if (fmode & FNONBLOCK)
4619 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4621 * release our private reference.
4623 fsetfd(fdp, NULL, indx);
4628 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4629 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
4633 * Clean up. Associate the file pointer with the previously
4634 * reserved descriptor and return it.
4637 if (uap->flags & O_CLOEXEC)
4638 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4639 fsetfd(fdp, fp, indx);
4641 uap->sysmsg_result = indx;
4645 fsetfd(fdp, NULL, indx);
4654 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4657 sys_fhstat(struct fhstat_args *uap)
4659 struct thread *td = curthread;
4667 * Must be super user
4669 error = priv_check(td, PRIV_ROOT);
4673 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4677 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4680 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4681 error = vn_stat(vp, &sb, td->td_ucred);
4686 error = copyout(&sb, uap->sb, sizeof(sb));
4691 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4694 sys_fhstatfs(struct fhstatfs_args *uap)
4696 struct thread *td = curthread;
4697 struct proc *p = td->td_proc;
4702 char *fullpath, *freepath;
4707 * Must be super user
4709 if ((error = priv_check(td, PRIV_ROOT)))
4712 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4715 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4719 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4724 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4729 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4732 error = mount_path(p, mp, &fullpath, &freepath);
4735 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4736 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4737 kfree(freepath, M_TEMP);
4739 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4740 if (priv_check(td, PRIV_ROOT)) {
4741 bcopy(sp, &sb, sizeof(sb));
4742 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4745 error = copyout(sp, uap->buf, sizeof(*sp));
4751 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4754 sys_fhstatvfs(struct fhstatvfs_args *uap)
4756 struct thread *td = curthread;
4757 struct proc *p = td->td_proc;
4765 * Must be super user
4767 if ((error = priv_check(td, PRIV_ROOT)))
4770 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4773 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4777 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4782 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4785 sp = &mp->mnt_vstat;
4787 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4791 if (mp->mnt_flag & MNT_RDONLY)
4792 sp->f_flag |= ST_RDONLY;
4793 if (mp->mnt_flag & MNT_NOSUID)
4794 sp->f_flag |= ST_NOSUID;
4795 error = copyout(sp, uap->buf, sizeof(*sp));
4802 * Syscall to push extended attribute configuration information into the
4803 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4804 * a command (int cmd), and attribute name and misc data. For now, the
4805 * attribute name is left in userspace for consumption by the VFS_op.
4806 * It will probably be changed to be copied into sysspace by the
4807 * syscall in the future, once issues with various consumers of the
4808 * attribute code have raised their hands.
4810 * Currently this is used only by UFS Extended Attributes.
4813 sys_extattrctl(struct extattrctl_args *uap)
4815 struct nlookupdata nd;
4817 char attrname[EXTATTR_MAXNAMELEN];
4825 if (error == 0 && uap->filename) {
4826 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4829 error = nlookup(&nd);
4831 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4835 if (error == 0 && uap->attrname) {
4836 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4841 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4843 error = nlookup(&nd);
4845 error = ncp_writechk(&nd.nl_nch);
4847 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4849 uap->attrname, nd.nl_cred);
4858 * Syscall to get a named extended attribute on a file or directory.
4861 sys_extattr_set_file(struct extattr_set_file_args *uap)
4863 char attrname[EXTATTR_MAXNAMELEN];
4864 struct nlookupdata nd;
4870 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4876 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4878 error = nlookup(&nd);
4880 error = ncp_writechk(&nd.nl_nch);
4882 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4888 bzero(&auio, sizeof(auio));
4889 aiov.iov_base = uap->data;
4890 aiov.iov_len = uap->nbytes;
4891 auio.uio_iov = &aiov;
4892 auio.uio_iovcnt = 1;
4893 auio.uio_offset = 0;
4894 auio.uio_resid = uap->nbytes;
4895 auio.uio_rw = UIO_WRITE;
4896 auio.uio_td = curthread;
4898 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4907 * Syscall to get a named extended attribute on a file or directory.
4910 sys_extattr_get_file(struct extattr_get_file_args *uap)
4912 char attrname[EXTATTR_MAXNAMELEN];
4913 struct nlookupdata nd;
4919 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4925 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4927 error = nlookup(&nd);
4929 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
4935 bzero(&auio, sizeof(auio));
4936 aiov.iov_base = uap->data;
4937 aiov.iov_len = uap->nbytes;
4938 auio.uio_iov = &aiov;
4939 auio.uio_iovcnt = 1;
4940 auio.uio_offset = 0;
4941 auio.uio_resid = uap->nbytes;
4942 auio.uio_rw = UIO_READ;
4943 auio.uio_td = curthread;
4945 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4947 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4955 * Syscall to delete a named extended attribute from a file or directory.
4956 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4959 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4961 char attrname[EXTATTR_MAXNAMELEN];
4962 struct nlookupdata nd;
4966 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4970 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4972 error = nlookup(&nd);
4974 error = ncp_writechk(&nd.nl_nch);
4976 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4978 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4979 attrname, NULL, nd.nl_cred);
4988 * Determine if the mount is visible to the process.
4991 chroot_visible_mnt(struct mount *mp, struct proc *p)
4993 struct nchandle nch;
4996 * Traverse from the mount point upwards. If we hit the process
4997 * root then the mount point is visible to the process.
4999 nch = mp->mnt_ncmountpt;
5001 if (nch.mount == p->p_fd->fd_nrdir.mount &&
5002 nch.ncp == p->p_fd->fd_nrdir.ncp) {
5005 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
5006 nch = nch.mount->mnt_ncmounton;
5008 nch.ncp = nch.ncp->nc_parent;
5013 * If the mount point is not visible to the process, but the
5014 * process root is in a subdirectory of the mount, return
5017 if (p->p_fd->fd_nrdir.mount == mp)