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>
70 #include <sys/mplock2.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
76 #include <machine/limits.h>
77 #include <machine/stdarg.h>
79 static void mount_warning(struct mount *mp, const char *ctl, ...)
81 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
82 static int checkvp_chdir (struct vnode *vn, struct thread *td);
83 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
84 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
85 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
86 static int getutimes (struct timeval *, struct timespec *);
87 static int getutimens (const struct timespec *, struct timespec *, int *);
88 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
89 static int setfmode (struct vnode *, int);
90 static int setfflags (struct vnode *, int);
91 static int setutimes (struct vnode *, struct vattr *,
92 const struct timespec *, int);
93 static int usermount = 0; /* if 1, non-root can mount fs. */
95 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
96 "Allow non-root users to mount filesystems");
99 * Virtual File System System Calls
103 * Mount a file system.
105 * mount_args(char *type, char *path, int flags, caddr_t data)
110 sys_mount(struct mount_args *uap)
112 struct thread *td = curthread;
115 struct mount *mp, *nullmp;
116 struct vfsconf *vfsp;
117 int error, flag = 0, flag2 = 0;
120 struct nlookupdata nd;
121 char fstypename[MFSNAMELEN];
129 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
133 * Do not allow NFS export by non-root users.
135 if (uap->flags & MNT_EXPORTED) {
136 error = priv_check(td, PRIV_ROOT);
141 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
143 if (priv_check(td, PRIV_ROOT))
144 uap->flags |= MNT_NOSUID | MNT_NODEV;
147 * Lookup the requested path and extract the nch and vnode.
149 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
151 if ((error = nlookup(&nd)) == 0) {
152 if (nd.nl_nch.ncp->nc_vp == NULL)
162 * If the target filesystem is resolved via a nullfs mount, then
163 * nd.nl_nch.mount will be pointing to the nullfs mount structure
164 * instead of the target file system. We need it in case we are
167 nullmp = nd.nl_nch.mount;
170 * Extract the locked+refd ncp and cleanup the nd structure
173 cache_zero(&nd.nl_nch);
176 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
177 (mp = cache_findmount(&nch)) != NULL) {
186 * now we have the locked ref'd nch and unreferenced vnode.
189 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
196 * Extract the file system type. We need to know this early, to take
197 * appropriate actions if we are dealing with a nullfs.
199 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
206 * Now we have an unlocked ref'd nch and a locked ref'd vp
208 if (uap->flags & MNT_UPDATE) {
209 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
216 if (strncmp(fstypename, "null", 5) == 0) {
224 flag2 = mp->mnt_kern_flag;
226 * We only allow the filesystem to be reloaded if it
227 * is currently mounted read-only.
229 if ((uap->flags & MNT_RELOAD) &&
230 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
233 error = EOPNOTSUPP; /* Needs translation */
237 * Only root, or the user that did the original mount is
238 * permitted to update it.
240 if (mp->mnt_stat.f_owner != cred->cr_uid &&
241 (error = priv_check(td, PRIV_ROOT))) {
246 if (vfs_busy(mp, LK_NOWAIT)) {
260 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
261 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 vfsp->vfc_refcount++;
340 mp->mnt_stat.f_type = vfsp->vfc_typenum;
341 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
342 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
343 mp->mnt_stat.f_owner = cred->cr_uid;
344 lwkt_gettoken(&mp->mnt_token);
348 * (per-mount token acquired at this point)
350 * Set the mount level flags.
352 if (uap->flags & MNT_RDONLY)
353 mp->mnt_flag |= MNT_RDONLY;
354 else if (mp->mnt_flag & MNT_RDONLY)
355 mp->mnt_kern_flag |= MNTK_WANTRDWR;
356 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
357 MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME |
358 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
359 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
361 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
362 MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE |
363 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
364 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
367 * Mount the filesystem.
368 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
371 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
372 if (mp->mnt_flag & MNT_UPDATE) {
373 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
374 mp->mnt_flag &= ~MNT_RDONLY;
375 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
376 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
379 mp->mnt_kern_flag = flag2;
381 lwkt_reltoken(&mp->mnt_token);
387 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
390 * Put the new filesystem on the mount list after root. The mount
391 * point gets its own mnt_ncmountpt (unless the VFS already set one
392 * up) which represents the root of the mount. The lookup code
393 * detects the mount point going forward and checks the root of
394 * the mount going backwards.
396 * It is not necessary to invalidate or purge the vnode underneath
397 * because elements under the mount will be given their own glue
401 if (mp->mnt_ncmountpt.ncp == NULL) {
403 * allocate, then unlock, but leave the ref intact
405 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
406 cache_unlock(&mp->mnt_ncmountpt);
409 mp->mnt_ncmounton = nch; /* inherits ref */
411 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
413 cache_ismounting(mp);
414 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
416 mountlist_insert(mp, MNTINS_LAST);
418 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
419 error = vfs_allocate_syncvnode(mp);
420 lwkt_reltoken(&mp->mnt_token);
422 error = VFS_START(mp, 0);
424 KNOTE(&fs_klist, VQ_MOUNT);
426 vn_syncer_thr_stop(mp);
427 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
428 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
429 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
430 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
431 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
432 mp->mnt_vfc->vfc_refcount--;
433 lwkt_reltoken(&mp->mnt_token);
444 * Scan all active processes to see if any of them have a current
445 * or root directory onto which the new filesystem has just been
446 * mounted. If so, replace them with the new mount point.
448 * Both old_nch and new_nch are ref'd on call but not locked.
449 * new_nch must be temporarily locked so it can be associated with the
450 * vnode representing the root of the mount point.
452 struct checkdirs_info {
453 struct nchandle old_nch;
454 struct nchandle new_nch;
455 struct vnode *old_vp;
456 struct vnode *new_vp;
459 static int checkdirs_callback(struct proc *p, void *data);
462 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
464 struct checkdirs_info info;
470 * If the old mount point's vnode has a usecount of 1, it is not
471 * being held as a descriptor anywhere.
473 olddp = old_nch->ncp->nc_vp;
474 if (olddp == NULL || VREFCNT(olddp) == 1)
478 * Force the root vnode of the new mount point to be resolved
479 * so we can update any matching processes.
482 if (VFS_ROOT(mp, &newdp))
483 panic("mount: lost mount");
486 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
487 cache_setunresolved(new_nch);
488 cache_setvp(new_nch, newdp);
489 cache_unlock(new_nch);
492 * Special handling of the root node
494 if (rootvnode == olddp) {
496 vfs_cache_setroot(newdp, cache_hold(new_nch));
500 * Pass newdp separately so the callback does not have to access
501 * it via new_nch->ncp->nc_vp.
503 info.old_nch = *old_nch;
504 info.new_nch = *new_nch;
506 allproc_scan(checkdirs_callback, &info);
511 * NOTE: callback is not MP safe because the scanned process's filedesc
512 * structure can be ripped out from under us, amoung other things.
515 checkdirs_callback(struct proc *p, void *data)
517 struct checkdirs_info *info = data;
518 struct filedesc *fdp;
519 struct nchandle ncdrop1;
520 struct nchandle ncdrop2;
521 struct vnode *vprele1;
522 struct vnode *vprele2;
524 if ((fdp = p->p_fd) != NULL) {
525 cache_zero(&ncdrop1);
526 cache_zero(&ncdrop2);
531 * MPUNSAFE - XXX fdp can be pulled out from under a
534 * A shared filedesc is ok, we don't have to copy it
535 * because we are making this change globally.
537 spin_lock(&fdp->fd_spin);
538 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
539 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
540 vprele1 = fdp->fd_cdir;
542 fdp->fd_cdir = info->new_vp;
543 ncdrop1 = fdp->fd_ncdir;
544 cache_copy(&info->new_nch, &fdp->fd_ncdir);
546 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
547 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
548 vprele2 = fdp->fd_rdir;
550 fdp->fd_rdir = info->new_vp;
551 ncdrop2 = fdp->fd_nrdir;
552 cache_copy(&info->new_nch, &fdp->fd_nrdir);
554 spin_unlock(&fdp->fd_spin);
556 cache_drop(&ncdrop1);
558 cache_drop(&ncdrop2);
568 * Unmount a file system.
570 * Note: unmount takes a path to the vnode mounted on as argument,
571 * not special file (as before).
573 * umount_args(char *path, int flags)
578 sys_unmount(struct unmount_args *uap)
580 struct thread *td = curthread;
581 struct proc *p __debugvar = td->td_proc;
582 struct mount *mp = NULL;
583 struct nlookupdata nd;
588 if (td->td_ucred->cr_prison != NULL) {
592 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
595 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
597 error = nlookup(&nd);
601 mp = nd.nl_nch.mount;
604 * Only root, or the user that did the original mount is
605 * permitted to unmount this filesystem.
607 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
608 (error = priv_check(td, PRIV_ROOT)))
612 * Don't allow unmounting the root file system.
614 if (mp->mnt_flag & MNT_ROOTFS) {
620 * Must be the root of the filesystem
622 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
630 error = dounmount(mp, uap->flags);
637 * Do the actual file system unmount.
640 dounmount_interlock(struct mount *mp)
642 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
644 mp->mnt_kern_flag |= MNTK_UNMOUNT;
649 unmount_allproc_cb(struct proc *p, void *arg)
653 if (p->p_textnch.ncp == NULL)
656 mp = (struct mount *)arg;
657 if (p->p_textnch.mount == mp)
658 cache_drop(&p->p_textnch);
664 dounmount(struct mount *mp, int flags)
666 struct namecache *ncp;
675 lwkt_gettoken(&mp->mnt_token);
677 * Exclusive access for unmounting purposes
679 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
683 * Allow filesystems to detect that a forced unmount is in progress.
685 if (flags & MNT_FORCE)
686 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
687 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
688 error = lockmgr(&mp->mnt_lock, lflags);
690 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
691 if (mp->mnt_kern_flag & MNTK_MWAIT) {
692 mp->mnt_kern_flag &= ~MNTK_MWAIT;
698 if (mp->mnt_flag & MNT_EXPUBLIC)
699 vfs_setpublicfs(NULL, NULL, NULL);
701 vfs_msync(mp, MNT_WAIT);
702 async_flag = mp->mnt_flag & MNT_ASYNC;
703 mp->mnt_flag &=~ MNT_ASYNC;
706 * If this filesystem isn't aliasing other filesystems,
707 * try to invalidate any remaining namecache entries and
708 * check the count afterwords.
710 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
711 cache_lock(&mp->mnt_ncmountpt);
712 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
713 cache_unlock(&mp->mnt_ncmountpt);
715 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
716 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
717 allproc_scan(&unmount_allproc_cb, mp);
720 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
721 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
723 if ((flags & MNT_FORCE) == 0) {
725 mount_warning(mp, "Cannot unmount: "
731 mount_warning(mp, "Forced unmount: "
742 * Decomission our special mnt_syncer vnode. This also stops
743 * the vnlru code. If we are unable to unmount we recommission
746 * Then sync the filesystem.
748 if ((vp = mp->mnt_syncer) != NULL) {
749 mp->mnt_syncer = NULL;
750 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
753 if ((mp->mnt_flag & MNT_RDONLY) == 0)
754 VFS_SYNC(mp, MNT_WAIT);
757 * nchandle records ref the mount structure. Expect a count of 1
758 * (our mount->mnt_ncmountpt).
760 * Scans can get temporary refs on a mountpoint (thought really
761 * heavy duty stuff like cache_findmount() do not).
763 for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
764 cache_unmounting(mp);
765 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
767 if (mp->mnt_refs != 1) {
768 if ((flags & MNT_FORCE) == 0) {
769 mount_warning(mp, "Cannot unmount: "
770 "%d mount refs still present",
774 mount_warning(mp, "Forced unmount: "
775 "%d mount refs still present",
782 * So far so good, sync the filesystem once more and
783 * call the VFS unmount code if the sync succeeds.
786 if (mp->mnt_flag & MNT_RDONLY) {
787 error = VFS_UNMOUNT(mp, flags);
789 error = VFS_SYNC(mp, MNT_WAIT);
791 (error == EOPNOTSUPP) || /* No sync */
792 (flags & MNT_FORCE)) {
793 error = VFS_UNMOUNT(mp, flags);
799 * If an error occurred we can still recover, restoring the
800 * syncer vnode and misc flags.
803 if (mp->mnt_syncer == NULL)
804 vfs_allocate_syncvnode(mp);
805 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
806 mp->mnt_flag |= async_flag;
807 lockmgr(&mp->mnt_lock, LK_RELEASE);
808 if (mp->mnt_kern_flag & MNTK_MWAIT) {
809 mp->mnt_kern_flag &= ~MNTK_MWAIT;
815 * Clean up any journals still associated with the mount after
816 * filesystem activity has ceased.
818 journal_remove_all_journals(mp,
819 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
821 mountlist_remove(mp);
824 * Remove any installed vnode ops here so the individual VFSs don't
827 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
828 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
829 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
830 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
831 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
833 if (mp->mnt_ncmountpt.ncp != NULL) {
834 nch = mp->mnt_ncmountpt;
835 cache_zero(&mp->mnt_ncmountpt);
836 cache_clrmountpt(&nch);
839 if (mp->mnt_ncmounton.ncp != NULL) {
840 cache_unmounting(mp);
841 nch = mp->mnt_ncmounton;
842 cache_zero(&mp->mnt_ncmounton);
843 cache_clrmountpt(&nch);
847 mp->mnt_vfc->vfc_refcount--;
848 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
849 panic("unmount: dangling vnode");
850 lockmgr(&mp->mnt_lock, LK_RELEASE);
851 if (mp->mnt_kern_flag & MNTK_MWAIT) {
852 mp->mnt_kern_flag &= ~MNTK_MWAIT;
857 * If we reach here and freeok != 0 we must free the mount.
858 * If refs > 1 cycle and wait, just in case someone tried
859 * to busy the mount after we decided to do the unmount.
862 while (mp->mnt_refs > 1) {
863 cache_unmounting(mp);
865 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
867 lwkt_reltoken(&mp->mnt_token);
872 KNOTE(&fs_klist, VQ_UNMOUNT);
875 lwkt_reltoken(&mp->mnt_token);
881 mount_warning(struct mount *mp, const char *ctl, ...)
888 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
889 &ptr, &buf, 0) == 0) {
890 kprintf("unmount(%s): ", ptr);
895 kprintf("unmount(%p", mp);
896 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
897 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
906 * Shim cache_fullpath() to handle the case where a process is chrooted into
907 * a subdirectory of a mount. In this case if the root mount matches the
908 * process root directory's mount we have to specify the process's root
909 * directory instead of the mount point, because the mount point might
910 * be above the root directory.
914 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
916 struct nchandle *nch;
918 if (p && p->p_fd->fd_nrdir.mount == mp)
919 nch = &p->p_fd->fd_nrdir;
921 nch = &mp->mnt_ncmountpt;
922 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
926 * Sync each mounted filesystem.
930 static int syncprt = 0;
931 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
934 static int sync_callback(struct mount *mp, void *data);
937 sys_sync(struct sync_args *uap)
939 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
945 sync_callback(struct mount *mp, void *data __unused)
949 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
950 asyncflag = mp->mnt_flag & MNT_ASYNC;
951 mp->mnt_flag &= ~MNT_ASYNC;
952 vfs_msync(mp, MNT_NOWAIT);
953 VFS_SYNC(mp, MNT_NOWAIT);
954 mp->mnt_flag |= asyncflag;
959 /* XXX PRISON: could be per prison flag */
960 static int prison_quotas;
962 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
966 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
968 * Change filesystem quotas.
973 sys_quotactl(struct quotactl_args *uap)
975 struct nlookupdata nd;
982 if (td->td_ucred->cr_prison && !prison_quotas) {
987 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
989 error = nlookup(&nd);
991 mp = nd.nl_nch.mount;
992 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
993 uap->arg, nd.nl_cred);
1002 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1003 * void *buf, int buflen)
1005 * This function operates on a mount point and executes the specified
1006 * operation using the specified control data, and possibly returns data.
1008 * The actual number of bytes stored in the result buffer is returned, 0
1009 * if none, otherwise an error is returned.
1014 sys_mountctl(struct mountctl_args *uap)
1016 struct thread *td = curthread;
1017 struct proc *p = td->td_proc;
1025 * Sanity and permissions checks. We must be root.
1028 if (td->td_ucred->cr_prison != NULL)
1030 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1031 (error = priv_check(td, PRIV_ROOT)) != 0)
1035 * Argument length checks
1037 if (uap->ctllen < 0 || uap->ctllen > 1024)
1039 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1041 if (uap->path == NULL)
1045 * Allocate the necessary buffers and copyin data
1047 path = objcache_get(namei_oc, M_WAITOK);
1048 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1053 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1054 error = copyin(uap->ctl, ctl, uap->ctllen);
1059 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1062 * Validate the descriptor
1065 fp = holdfp(p->p_fd, uap->fd, -1);
1075 * Execute the internal kernel function and clean up.
1078 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1082 if (error == 0 && uap->sysmsg_result > 0)
1083 error = copyout(buf, uap->buf, uap->sysmsg_result);
1086 objcache_put(namei_oc, path);
1095 * Execute a mount control operation by resolving the path to a mount point
1096 * and calling vop_mountctl().
1098 * Use the mount point from the nch instead of the vnode so nullfs mounts
1099 * can properly spike the VOP.
1102 kern_mountctl(const char *path, int op, struct file *fp,
1103 const void *ctl, int ctllen,
1104 void *buf, int buflen, int *res)
1107 struct nlookupdata nd;
1108 struct nchandle nch;
1114 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1117 error = nlookup(&nd);
1122 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1129 * Yes, all this is needed to use the nch.mount below, because
1130 * we must maintain a ref on the mount to avoid ripouts (e.g.
1131 * due to heavy mount/unmount use by synth or poudriere).
1134 cache_zero(&nd.nl_nch);
1142 * Must be the root of the filesystem
1144 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1149 if (mp == NULL || mp->mnt_kern_flag & MNTK_UNMOUNT) {
1150 kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1156 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1165 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1167 struct thread *td = curthread;
1168 struct proc *p = td->td_proc;
1171 char *fullpath, *freepath;
1174 if ((error = nlookup(nd)) != 0)
1176 mp = nd->nl_nch.mount;
1178 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1181 error = mount_path(p, mp, &fullpath, &freepath);
1184 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1185 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1186 kfree(freepath, M_TEMP);
1188 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1189 bcopy(sp, buf, sizeof(*buf));
1190 /* Only root should have access to the fsid's. */
1191 if (priv_check(td, PRIV_ROOT))
1192 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1197 * statfs_args(char *path, struct statfs *buf)
1199 * Get filesystem statistics.
1202 sys_statfs(struct statfs_args *uap)
1204 struct nlookupdata nd;
1208 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1210 error = kern_statfs(&nd, &buf);
1213 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1218 kern_fstatfs(int fd, struct statfs *buf)
1220 struct thread *td = curthread;
1221 struct proc *p = td->td_proc;
1225 char *fullpath, *freepath;
1229 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1233 * Try to use mount info from any overlays rather than the
1234 * mount info for the underlying vnode, otherwise we will
1235 * fail when operating on null-mounted paths inside a chroot.
1237 if ((mp = fp->f_nchandle.mount) == NULL)
1238 mp = ((struct vnode *)fp->f_data)->v_mount;
1243 if (fp->f_cred == NULL) {
1248 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1251 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1253 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1254 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1255 kfree(freepath, M_TEMP);
1257 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1258 bcopy(sp, buf, sizeof(*buf));
1260 /* Only root should have access to the fsid's. */
1261 if (priv_check(td, PRIV_ROOT))
1262 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1270 * fstatfs_args(int fd, struct statfs *buf)
1272 * Get filesystem statistics.
1275 sys_fstatfs(struct fstatfs_args *uap)
1280 error = kern_fstatfs(uap->fd, &buf);
1283 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1288 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1294 if ((error = nlookup(nd)) != 0)
1296 mp = nd->nl_nch.mount;
1297 sp = &mp->mnt_vstat;
1298 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1302 if (mp->mnt_flag & MNT_RDONLY)
1303 sp->f_flag |= ST_RDONLY;
1304 if (mp->mnt_flag & MNT_NOSUID)
1305 sp->f_flag |= ST_NOSUID;
1306 bcopy(sp, buf, sizeof(*buf));
1311 * statfs_args(char *path, struct statfs *buf)
1313 * Get filesystem statistics.
1316 sys_statvfs(struct statvfs_args *uap)
1318 struct nlookupdata nd;
1322 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1324 error = kern_statvfs(&nd, &buf);
1327 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1332 kern_fstatvfs(int fd, struct statvfs *buf)
1334 struct thread *td = curthread;
1335 struct proc *p = td->td_proc;
1342 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1344 if ((mp = fp->f_nchandle.mount) == NULL)
1345 mp = ((struct vnode *)fp->f_data)->v_mount;
1350 if (fp->f_cred == NULL) {
1354 sp = &mp->mnt_vstat;
1355 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1359 if (mp->mnt_flag & MNT_RDONLY)
1360 sp->f_flag |= ST_RDONLY;
1361 if (mp->mnt_flag & MNT_NOSUID)
1362 sp->f_flag |= ST_NOSUID;
1364 bcopy(sp, buf, sizeof(*buf));
1372 * fstatfs_args(int fd, struct statfs *buf)
1374 * Get filesystem statistics.
1377 sys_fstatvfs(struct fstatvfs_args *uap)
1382 error = kern_fstatvfs(uap->fd, &buf);
1385 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1390 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1392 * Get statistics on all filesystems.
1395 struct getfsstat_info {
1396 struct statfs *sfsp;
1404 static int getfsstat_callback(struct mount *, void *);
1407 sys_getfsstat(struct getfsstat_args *uap)
1409 struct thread *td = curthread;
1410 struct getfsstat_info info;
1412 bzero(&info, sizeof(info));
1414 info.maxcount = uap->bufsize / sizeof(struct statfs);
1415 info.sfsp = uap->buf;
1417 info.flags = uap->flags;
1420 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1421 if (info.sfsp && info.count > info.maxcount)
1422 uap->sysmsg_result = info.maxcount;
1424 uap->sysmsg_result = info.count;
1425 return (info.error);
1429 getfsstat_callback(struct mount *mp, void *data)
1431 struct getfsstat_info *info = data;
1437 if (info->sfsp && info->count < info->maxcount) {
1438 if (info->td->td_proc &&
1439 !chroot_visible_mnt(mp, info->td->td_proc)) {
1445 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1446 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1447 * overrides MNT_WAIT.
1449 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1450 (info->flags & MNT_WAIT)) &&
1451 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1454 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1456 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1458 info->error = error;
1461 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1462 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1463 kfree(freepath, M_TEMP);
1465 error = copyout(sp, info->sfsp, sizeof(*sp));
1467 info->error = error;
1477 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1478 long bufsize, int flags)
1480 * Get statistics on all filesystems.
1483 struct getvfsstat_info {
1484 struct statfs *sfsp;
1485 struct statvfs *vsfsp;
1493 static int getvfsstat_callback(struct mount *, void *);
1496 sys_getvfsstat(struct getvfsstat_args *uap)
1498 struct thread *td = curthread;
1499 struct getvfsstat_info info;
1501 bzero(&info, sizeof(info));
1503 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1504 info.sfsp = uap->buf;
1505 info.vsfsp = uap->vbuf;
1507 info.flags = uap->flags;
1510 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1511 if (info.vsfsp && info.count > info.maxcount)
1512 uap->sysmsg_result = info.maxcount;
1514 uap->sysmsg_result = info.count;
1515 return (info.error);
1519 getvfsstat_callback(struct mount *mp, void *data)
1521 struct getvfsstat_info *info = data;
1523 struct statvfs *vsp;
1528 if (info->vsfsp && info->count < info->maxcount) {
1529 if (info->td->td_proc &&
1530 !chroot_visible_mnt(mp, info->td->td_proc)) {
1534 vsp = &mp->mnt_vstat;
1537 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1538 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1539 * overrides MNT_WAIT.
1541 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1542 (info->flags & MNT_WAIT)) &&
1543 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1546 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1548 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1549 (info->flags & MNT_WAIT)) &&
1550 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1554 if (mp->mnt_flag & MNT_RDONLY)
1555 vsp->f_flag |= ST_RDONLY;
1556 if (mp->mnt_flag & MNT_NOSUID)
1557 vsp->f_flag |= ST_NOSUID;
1559 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1561 info->error = error;
1564 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1565 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1566 kfree(freepath, M_TEMP);
1568 error = copyout(sp, info->sfsp, sizeof(*sp));
1570 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1572 info->error = error;
1584 * fchdir_args(int fd)
1586 * Change current working directory to a given file descriptor.
1589 sys_fchdir(struct fchdir_args *uap)
1591 struct thread *td = curthread;
1592 struct proc *p = td->td_proc;
1593 struct filedesc *fdp = p->p_fd;
1594 struct vnode *vp, *ovp;
1597 struct nchandle nch, onch, tnch;
1600 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1602 lwkt_gettoken(&p->p_token);
1603 vp = (struct vnode *)fp->f_data;
1605 vn_lock(vp, LK_SHARED | LK_RETRY);
1606 if (fp->f_nchandle.ncp == NULL)
1609 error = checkvp_chdir(vp, td);
1614 cache_copy(&fp->f_nchandle, &nch);
1617 * If the ncp has become a mount point, traverse through
1621 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1622 (mp = cache_findmount(&nch)) != NULL
1624 error = nlookup_mp(mp, &tnch);
1626 cache_unlock(&tnch); /* leave ref intact */
1628 vp = tnch.ncp->nc_vp;
1629 error = vget(vp, LK_SHARED);
1630 KKASSERT(error == 0);
1634 cache_dropmount(mp);
1638 onch = fdp->fd_ncdir;
1639 vn_unlock(vp); /* leave ref intact */
1641 fdp->fd_ncdir = nch;
1650 lwkt_reltoken(&p->p_token);
1655 kern_chdir(struct nlookupdata *nd)
1657 struct thread *td = curthread;
1658 struct proc *p = td->td_proc;
1659 struct filedesc *fdp = p->p_fd;
1660 struct vnode *vp, *ovp;
1661 struct nchandle onch;
1664 nd->nl_flags |= NLC_SHAREDLOCK;
1665 if ((error = nlookup(nd)) != 0)
1667 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1669 if ((error = vget(vp, LK_SHARED)) != 0)
1672 lwkt_gettoken(&p->p_token);
1673 error = checkvp_chdir(vp, td);
1677 onch = fdp->fd_ncdir;
1678 cache_unlock(&nd->nl_nch); /* leave reference intact */
1679 fdp->fd_ncdir = nd->nl_nch;
1683 cache_zero(&nd->nl_nch);
1687 lwkt_reltoken(&p->p_token);
1692 * chdir_args(char *path)
1694 * Change current working directory (``.'').
1697 sys_chdir(struct chdir_args *uap)
1699 struct nlookupdata nd;
1702 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1704 error = kern_chdir(&nd);
1710 * Helper function for raised chroot(2) security function: Refuse if
1711 * any filedescriptors are open directories.
1714 chroot_refuse_vdir_fds(struct filedesc *fdp)
1721 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1722 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1724 vp = (struct vnode *)fp->f_data;
1725 if (vp->v_type != VDIR) {
1736 * This sysctl determines if we will allow a process to chroot(2) if it
1737 * has a directory open:
1738 * 0: disallowed for all processes.
1739 * 1: allowed for processes that were not already chroot(2)'ed.
1740 * 2: allowed for all processes.
1743 static int chroot_allow_open_directories = 1;
1745 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1746 &chroot_allow_open_directories, 0, "");
1749 * chroot to the specified namecache entry. We obtain the vp from the
1750 * namecache data. The passed ncp must be locked and referenced and will
1751 * remain locked and referenced on return.
1754 kern_chroot(struct nchandle *nch)
1756 struct thread *td = curthread;
1757 struct proc *p = td->td_proc;
1758 struct filedesc *fdp = p->p_fd;
1763 * Only privileged user can chroot
1765 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1770 * Disallow open directory descriptors (fchdir() breakouts).
1772 if (chroot_allow_open_directories == 0 ||
1773 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1774 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1777 if ((vp = nch->ncp->nc_vp) == NULL)
1780 if ((error = vget(vp, LK_SHARED)) != 0)
1784 * Check the validity of vp as a directory to change to and
1785 * associate it with rdir/jdir.
1787 error = checkvp_chdir(vp, td);
1788 vn_unlock(vp); /* leave reference intact */
1790 vrele(fdp->fd_rdir);
1791 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1792 cache_drop(&fdp->fd_nrdir);
1793 cache_copy(nch, &fdp->fd_nrdir);
1794 if (fdp->fd_jdir == NULL) {
1797 cache_copy(nch, &fdp->fd_njdir);
1806 * chroot_args(char *path)
1808 * Change notion of root (``/'') directory.
1811 sys_chroot(struct chroot_args *uap)
1813 struct thread *td __debugvar = curthread;
1814 struct nlookupdata nd;
1817 KKASSERT(td->td_proc);
1818 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1820 nd.nl_flags |= NLC_EXEC;
1821 error = nlookup(&nd);
1823 error = kern_chroot(&nd.nl_nch);
1830 sys_chroot_kernel(struct chroot_kernel_args *uap)
1832 struct thread *td = curthread;
1833 struct nlookupdata nd;
1834 struct nchandle *nch;
1838 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1842 error = nlookup(&nd);
1848 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1852 if ((vp = nch->ncp->nc_vp) == NULL) {
1857 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1860 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1862 vfs_cache_setroot(vp, cache_hold(nch));
1872 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1873 * determine whether it is legal to chdir to the vnode. The vnode's state
1874 * is not changed by this call.
1877 checkvp_chdir(struct vnode *vp, struct thread *td)
1881 if (vp->v_type != VDIR)
1884 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1889 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1891 struct thread *td = curthread;
1892 struct proc *p = td->td_proc;
1893 struct lwp *lp = td->td_lwp;
1894 struct filedesc *fdp = p->p_fd;
1899 int type, indx, error = 0;
1902 if ((oflags & O_ACCMODE) == O_ACCMODE)
1904 flags = FFLAGS(oflags);
1905 error = falloc(lp, &nfp, NULL);
1909 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1912 * XXX p_dupfd is a real mess. It allows a device to return a
1913 * file descriptor to be duplicated rather then doing the open
1919 * Call vn_open() to do the lookup and assign the vnode to the
1920 * file pointer. vn_open() does not change the ref count on fp
1921 * and the vnode, on success, will be inherited by the file pointer
1924 * Request a shared lock on the vnode if possible.
1926 nd->nl_flags |= NLC_LOCKVP;
1927 if ((flags & (O_CREAT|O_TRUNC)) == 0)
1928 nd->nl_flags |= NLC_SHAREDLOCK;
1930 error = vn_open(nd, fp, flags, cmode);
1935 * handle special fdopen() case. bleh. dupfdopen() is
1936 * responsible for dropping the old contents of ofiles[indx]
1939 * Note that fsetfd() will add a ref to fp which represents
1940 * the fd_files[] assignment. We must still drop our
1943 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1944 if (fdalloc(p, 0, &indx) == 0) {
1945 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1948 fdrop(fp); /* our ref */
1951 fsetfd(fdp, NULL, indx);
1954 fdrop(fp); /* our ref */
1955 if (error == ERESTART)
1961 * ref the vnode for ourselves so it can't be ripped out from under
1962 * is. XXX need an ND flag to request that the vnode be returned
1965 * Reserve a file descriptor but do not assign it until the open
1968 vp = (struct vnode *)fp->f_data;
1970 if ((error = fdalloc(p, 0, &indx)) != 0) {
1977 * If no error occurs the vp will have been assigned to the file
1982 if (flags & (O_EXLOCK | O_SHLOCK)) {
1983 lf.l_whence = SEEK_SET;
1986 if (flags & O_EXLOCK)
1987 lf.l_type = F_WRLCK;
1989 lf.l_type = F_RDLCK;
1990 if (flags & FNONBLOCK)
1995 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1997 * lock request failed. Clean up the reserved
2001 fsetfd(fdp, NULL, indx);
2005 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
2009 * Assert that all regular file vnodes were created with a object.
2011 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
2012 ("open: regular file has no backing object after vn_open"));
2018 * release our private reference, leaving the one associated with the
2019 * descriptor table intact.
2021 if (oflags & O_CLOEXEC)
2022 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
2023 fsetfd(fdp, fp, indx);
2030 * open_args(char *path, int flags, int mode)
2032 * Check permissions, allocate an open file structure,
2033 * and call the device open routine if any.
2036 sys_open(struct open_args *uap)
2038 struct nlookupdata nd;
2041 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2043 error = kern_open(&nd, uap->flags,
2044 uap->mode, &uap->sysmsg_result);
2051 * openat_args(int fd, char *path, int flags, int mode)
2054 sys_openat(struct openat_args *uap)
2056 struct nlookupdata nd;
2060 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2062 error = kern_open(&nd, uap->flags, uap->mode,
2063 &uap->sysmsg_result);
2065 nlookup_done_at(&nd, fp);
2070 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2072 struct thread *td = curthread;
2073 struct proc *p = td->td_proc;
2082 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2083 vattr.va_rmajor = rmajor;
2084 vattr.va_rminor = rminor;
2086 switch (mode & S_IFMT) {
2087 case S_IFMT: /* used by badsect to flag bad sectors */
2088 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2089 vattr.va_type = VBAD;
2092 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2093 vattr.va_type = VCHR;
2096 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2097 vattr.va_type = VBLK;
2100 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2103 case S_IFDIR: /* special directories support for HAMMER */
2104 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2105 vattr.va_type = VDIR;
2116 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2117 if ((error = nlookup(nd)) != 0)
2119 if (nd->nl_nch.ncp->nc_vp)
2121 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2125 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2126 nd->nl_cred, NAMEI_CREATE);
2129 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2130 &vp, nd->nl_cred, &vattr);
2138 * mknod_args(char *path, int mode, int dev)
2140 * Create a special file.
2143 sys_mknod(struct mknod_args *uap)
2145 struct nlookupdata nd;
2148 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2150 error = kern_mknod(&nd, uap->mode,
2151 umajor(uap->dev), uminor(uap->dev));
2158 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2160 * Create a special file. The path is relative to the directory associated
2164 sys_mknodat(struct mknodat_args *uap)
2166 struct nlookupdata nd;
2170 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2172 error = kern_mknod(&nd, uap->mode,
2173 umajor(uap->dev), uminor(uap->dev));
2175 nlookup_done_at(&nd, fp);
2180 kern_mkfifo(struct nlookupdata *nd, int mode)
2182 struct thread *td = curthread;
2183 struct proc *p = td->td_proc;
2190 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2191 if ((error = nlookup(nd)) != 0)
2193 if (nd->nl_nch.ncp->nc_vp)
2195 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2199 vattr.va_type = VFIFO;
2200 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2202 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2209 * mkfifo_args(char *path, int mode)
2211 * Create a named pipe.
2214 sys_mkfifo(struct mkfifo_args *uap)
2216 struct nlookupdata nd;
2219 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2221 error = kern_mkfifo(&nd, uap->mode);
2227 * mkfifoat_args(int fd, char *path, mode_t mode)
2229 * Create a named pipe. The path is relative to the directory associated
2233 sys_mkfifoat(struct mkfifoat_args *uap)
2235 struct nlookupdata nd;
2239 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2241 error = kern_mkfifo(&nd, uap->mode);
2242 nlookup_done_at(&nd, fp);
2246 static int hardlink_check_uid = 0;
2247 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2248 &hardlink_check_uid, 0,
2249 "Unprivileged processes cannot create hard links to files owned by other "
2251 static int hardlink_check_gid = 0;
2252 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2253 &hardlink_check_gid, 0,
2254 "Unprivileged processes cannot create hard links to files owned by other "
2258 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2264 * Shortcut if disabled
2266 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2270 * Privileged user can always hardlink
2272 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2276 * Otherwise only if the originating file is owned by the
2277 * same user or group. Note that any group is allowed if
2278 * the file is owned by the caller.
2280 error = VOP_GETATTR(vp, &va);
2284 if (hardlink_check_uid) {
2285 if (cred->cr_uid != va.va_uid)
2289 if (hardlink_check_gid) {
2290 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2298 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2300 struct thread *td = curthread;
2305 * Lookup the source and obtained a locked vnode.
2307 * You may only hardlink a file which you have write permission
2308 * on or which you own.
2310 * XXX relookup on vget failure / race ?
2313 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2314 if ((error = nlookup(nd)) != 0)
2316 vp = nd->nl_nch.ncp->nc_vp;
2317 KKASSERT(vp != NULL);
2318 if (vp->v_type == VDIR)
2319 return (EPERM); /* POSIX */
2320 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2322 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2326 * Unlock the source so we can lookup the target without deadlocking
2327 * (XXX vp is locked already, possible other deadlock?). The target
2330 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2331 nd->nl_flags &= ~NLC_NCPISLOCKED;
2332 cache_unlock(&nd->nl_nch);
2335 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2336 if ((error = nlookup(linknd)) != 0) {
2340 if (linknd->nl_nch.ncp->nc_vp) {
2344 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2351 * Finally run the new API VOP.
2353 error = can_hardlink(vp, td, td->td_ucred);
2355 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2356 vp, linknd->nl_cred);
2363 * link_args(char *path, char *link)
2365 * Make a hard file link.
2368 sys_link(struct link_args *uap)
2370 struct nlookupdata nd, linknd;
2373 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2375 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2377 error = kern_link(&nd, &linknd);
2378 nlookup_done(&linknd);
2385 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2387 * Make a hard file link. The path1 argument is relative to the directory
2388 * associated with fd1, and similarly the path2 argument is relative to
2389 * the directory associated with fd2.
2392 sys_linkat(struct linkat_args *uap)
2394 struct nlookupdata nd, linknd;
2395 struct file *fp1, *fp2;
2398 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2399 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2401 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2402 uap->path2, UIO_USERSPACE, 0);
2404 error = kern_link(&nd, &linknd);
2405 nlookup_done_at(&linknd, fp2);
2407 nlookup_done_at(&nd, fp1);
2412 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2420 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2421 if ((error = nlookup(nd)) != 0)
2423 if (nd->nl_nch.ncp->nc_vp)
2425 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2429 vattr.va_mode = mode;
2430 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2437 * symlink(char *path, char *link)
2439 * Make a symbolic link.
2442 sys_symlink(struct symlink_args *uap)
2444 struct thread *td = curthread;
2445 struct nlookupdata nd;
2450 path = objcache_get(namei_oc, M_WAITOK);
2451 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2453 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2455 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2456 error = kern_symlink(&nd, path, mode);
2460 objcache_put(namei_oc, path);
2465 * symlinkat_args(char *path1, int fd, char *path2)
2467 * Make a symbolic link. The path2 argument is relative to the directory
2468 * associated with fd.
2471 sys_symlinkat(struct symlinkat_args *uap)
2473 struct thread *td = curthread;
2474 struct nlookupdata nd;
2480 path1 = objcache_get(namei_oc, M_WAITOK);
2481 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2483 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2486 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2487 error = kern_symlink(&nd, path1, mode);
2489 nlookup_done_at(&nd, fp);
2491 objcache_put(namei_oc, path1);
2496 * undelete_args(char *path)
2498 * Delete a whiteout from the filesystem.
2501 sys_undelete(struct undelete_args *uap)
2503 struct nlookupdata nd;
2506 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2508 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2510 error = nlookup(&nd);
2512 error = ncp_writechk(&nd.nl_nch);
2514 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2522 kern_unlink(struct nlookupdata *nd)
2527 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2528 if ((error = nlookup(nd)) != 0)
2530 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2532 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2537 * unlink_args(char *path)
2539 * Delete a name from the filesystem.
2542 sys_unlink(struct unlink_args *uap)
2544 struct nlookupdata nd;
2547 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2549 error = kern_unlink(&nd);
2556 * unlinkat_args(int fd, char *path, int flags)
2558 * Delete the file or directory entry pointed to by fd/path.
2561 sys_unlinkat(struct unlinkat_args *uap)
2563 struct nlookupdata nd;
2567 if (uap->flags & ~AT_REMOVEDIR)
2570 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2572 if (uap->flags & AT_REMOVEDIR)
2573 error = kern_rmdir(&nd);
2575 error = kern_unlink(&nd);
2577 nlookup_done_at(&nd, fp);
2582 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2584 struct thread *td = curthread;
2585 struct proc *p = td->td_proc;
2592 fp = holdfp(p->p_fd, fd, -1);
2595 if (fp->f_type != DTYPE_VNODE) {
2599 vp = (struct vnode *)fp->f_data;
2603 spin_lock(&fp->f_spin);
2604 new_offset = fp->f_offset + offset;
2608 error = VOP_GETATTR(vp, &vattr);
2609 spin_lock(&fp->f_spin);
2610 new_offset = offset + vattr.va_size;
2613 new_offset = offset;
2615 spin_lock(&fp->f_spin);
2620 spin_lock(&fp->f_spin);
2625 * Validate the seek position. Negative offsets are not allowed
2626 * for regular files or directories.
2628 * Normally we would also not want to allow negative offsets for
2629 * character and block-special devices. However kvm addresses
2630 * on 64 bit architectures might appear to be negative and must
2634 if (new_offset < 0 &&
2635 (vp->v_type == VREG || vp->v_type == VDIR)) {
2638 fp->f_offset = new_offset;
2641 *res = fp->f_offset;
2642 spin_unlock(&fp->f_spin);
2649 * lseek_args(int fd, int pad, off_t offset, int whence)
2651 * Reposition read/write file offset.
2654 sys_lseek(struct lseek_args *uap)
2658 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2659 &uap->sysmsg_offset);
2665 * Check if current process can access given file. amode is a bitmask of *_OK
2666 * access bits. flags is a bitmask of AT_* flags.
2669 kern_access(struct nlookupdata *nd, int amode, int flags)
2674 if (flags & ~AT_EACCESS)
2676 nd->nl_flags |= NLC_SHAREDLOCK;
2677 if ((error = nlookup(nd)) != 0)
2680 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2684 /* Flags == 0 means only check for existence. */
2693 if ((mode & VWRITE) == 0 ||
2694 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2695 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2698 * If the file handle is stale we have to re-resolve the
2699 * entry with the ncp held exclusively. This is a hack
2702 if (error == ESTALE) {
2704 cache_unlock(&nd->nl_nch);
2705 cache_lock(&nd->nl_nch);
2706 cache_setunresolved(&nd->nl_nch);
2707 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2720 * access_args(char *path, int flags)
2722 * Check access permissions.
2725 sys_access(struct access_args *uap)
2727 struct nlookupdata nd;
2730 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2732 error = kern_access(&nd, uap->flags, 0);
2739 * eaccess_args(char *path, int flags)
2741 * Check access permissions.
2744 sys_eaccess(struct eaccess_args *uap)
2746 struct nlookupdata nd;
2749 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2751 error = kern_access(&nd, uap->flags, AT_EACCESS);
2758 * faccessat_args(int fd, char *path, int amode, int flags)
2760 * Check access permissions.
2763 sys_faccessat(struct faccessat_args *uap)
2765 struct nlookupdata nd;
2769 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2772 error = kern_access(&nd, uap->amode, uap->flags);
2773 nlookup_done_at(&nd, fp);
2778 kern_stat(struct nlookupdata *nd, struct stat *st)
2783 nd->nl_flags |= NLC_SHAREDLOCK;
2784 if ((error = nlookup(nd)) != 0)
2787 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2790 if ((error = vget(vp, LK_SHARED)) != 0)
2792 error = vn_stat(vp, st, nd->nl_cred);
2795 * If the file handle is stale we have to re-resolve the
2796 * entry with the ncp held exclusively. This is a hack
2799 if (error == ESTALE) {
2801 cache_unlock(&nd->nl_nch);
2802 cache_lock(&nd->nl_nch);
2803 cache_setunresolved(&nd->nl_nch);
2804 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2814 * stat_args(char *path, struct stat *ub)
2816 * Get file status; this version follows links.
2819 sys_stat(struct stat_args *uap)
2821 struct nlookupdata nd;
2825 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2827 error = kern_stat(&nd, &st);
2829 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2836 * lstat_args(char *path, struct stat *ub)
2838 * Get file status; this version does not follow links.
2841 sys_lstat(struct lstat_args *uap)
2843 struct nlookupdata nd;
2847 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2849 error = kern_stat(&nd, &st);
2851 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2858 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2860 * Get status of file pointed to by fd/path.
2863 sys_fstatat(struct fstatat_args *uap)
2865 struct nlookupdata nd;
2871 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2874 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2876 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2877 UIO_USERSPACE, flags);
2879 error = kern_stat(&nd, &st);
2881 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2883 nlookup_done_at(&nd, fp);
2888 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
2890 struct nlookupdata nd;
2895 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
2897 error = nlookup(&nd);
2899 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2902 error = VOP_PATHCONF(vp, name, sysmsg_regp);
2909 * pathconf_Args(char *path, int name)
2911 * Get configurable pathname variables.
2914 sys_pathconf(struct pathconf_args *uap)
2916 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
2921 * lpathconf_Args(char *path, int name)
2923 * Get configurable pathname variables, but don't follow symlinks.
2926 sys_lpathconf(struct lpathconf_args *uap)
2928 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
2933 * kern_readlink isn't properly split yet. There is a copyin burried
2934 * in VOP_READLINK().
2937 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2939 struct thread *td = curthread;
2945 nd->nl_flags |= NLC_SHAREDLOCK;
2946 if ((error = nlookup(nd)) != 0)
2948 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2951 if (vp->v_type != VLNK) {
2954 aiov.iov_base = buf;
2955 aiov.iov_len = count;
2956 auio.uio_iov = &aiov;
2957 auio.uio_iovcnt = 1;
2958 auio.uio_offset = 0;
2959 auio.uio_rw = UIO_READ;
2960 auio.uio_segflg = UIO_USERSPACE;
2962 auio.uio_resid = count;
2963 error = VOP_READLINK(vp, &auio, td->td_ucred);
2966 *res = count - auio.uio_resid;
2971 * readlink_args(char *path, char *buf, int count)
2973 * Return target name of a symbolic link.
2976 sys_readlink(struct readlink_args *uap)
2978 struct nlookupdata nd;
2981 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2983 error = kern_readlink(&nd, uap->buf, uap->count,
2984 &uap->sysmsg_result);
2991 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2993 * Return target name of a symbolic link. The path is relative to the
2994 * directory associated with fd.
2997 sys_readlinkat(struct readlinkat_args *uap)
2999 struct nlookupdata nd;
3003 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3005 error = kern_readlink(&nd, uap->buf, uap->bufsize,
3006 &uap->sysmsg_result);
3008 nlookup_done_at(&nd, fp);
3013 setfflags(struct vnode *vp, int flags)
3015 struct thread *td = curthread;
3020 * Prevent non-root users from setting flags on devices. When
3021 * a device is reused, users can retain ownership of the device
3022 * if they are allowed to set flags and programs assume that
3023 * chown can't fail when done as root.
3025 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
3026 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
3030 * note: vget is required for any operation that might mod the vnode
3031 * so VINACTIVE is properly cleared.
3033 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3035 vattr.va_flags = flags;
3036 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3043 * chflags(char *path, int flags)
3045 * Change flags of a file given a path name.
3048 sys_chflags(struct chflags_args *uap)
3050 struct nlookupdata nd;
3055 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3057 error = nlookup(&nd);
3059 error = ncp_writechk(&nd.nl_nch);
3061 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3064 error = setfflags(vp, uap->flags);
3071 * lchflags(char *path, int flags)
3073 * Change flags of a file given a path name, but don't follow symlinks.
3076 sys_lchflags(struct lchflags_args *uap)
3078 struct nlookupdata nd;
3083 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3085 error = nlookup(&nd);
3087 error = ncp_writechk(&nd.nl_nch);
3089 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3092 error = setfflags(vp, uap->flags);
3099 * fchflags_args(int fd, int flags)
3101 * Change flags of a file given a file descriptor.
3104 sys_fchflags(struct fchflags_args *uap)
3106 struct thread *td = curthread;
3107 struct proc *p = td->td_proc;
3111 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3113 if (fp->f_nchandle.ncp)
3114 error = ncp_writechk(&fp->f_nchandle);
3116 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3122 * chflagsat_args(int fd, const char *path, int flags, int atflags)
3123 * change flags given a pathname relative to a filedescriptor
3125 int sys_chflagsat(struct chflagsat_args *uap)
3127 struct nlookupdata nd;
3133 if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3136 lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3139 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, lookupflags);
3141 error = nlookup(&nd);
3143 error = ncp_writechk(&nd.nl_nch);
3145 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3146 nlookup_done_at(&nd, fp);
3148 error = setfflags(vp, uap->flags);
3156 setfmode(struct vnode *vp, int mode)
3158 struct thread *td = curthread;
3163 * note: vget is required for any operation that might mod the vnode
3164 * so VINACTIVE is properly cleared.
3166 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3168 vattr.va_mode = mode & ALLPERMS;
3169 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3176 kern_chmod(struct nlookupdata *nd, int mode)
3181 if ((error = nlookup(nd)) != 0)
3183 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3185 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3186 error = setfmode(vp, mode);
3192 * chmod_args(char *path, int mode)
3194 * Change mode of a file given path name.
3197 sys_chmod(struct chmod_args *uap)
3199 struct nlookupdata nd;
3202 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3204 error = kern_chmod(&nd, uap->mode);
3210 * lchmod_args(char *path, int mode)
3212 * Change mode of a file given path name (don't follow links.)
3215 sys_lchmod(struct lchmod_args *uap)
3217 struct nlookupdata nd;
3220 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3222 error = kern_chmod(&nd, uap->mode);
3228 * fchmod_args(int fd, int mode)
3230 * Change mode of a file given a file descriptor.
3233 sys_fchmod(struct fchmod_args *uap)
3235 struct thread *td = curthread;
3236 struct proc *p = td->td_proc;
3240 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3242 if (fp->f_nchandle.ncp)
3243 error = ncp_writechk(&fp->f_nchandle);
3245 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3251 * fchmodat_args(char *path, int mode)
3253 * Change mode of a file pointed to by fd/path.
3256 sys_fchmodat(struct fchmodat_args *uap)
3258 struct nlookupdata nd;
3263 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3265 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3267 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3268 UIO_USERSPACE, flags);
3270 error = kern_chmod(&nd, uap->mode);
3271 nlookup_done_at(&nd, fp);
3276 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3278 struct thread *td = curthread;
3286 * note: vget is required for any operation that might mod the vnode
3287 * so VINACTIVE is properly cleared.
3289 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3290 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3292 o_uid = vattr.va_uid;
3293 o_gid = vattr.va_gid;
3294 size = vattr.va_size;
3299 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3308 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3309 VFS_ACCOUNT(mp, uid, gid, size);
3316 kern_chown(struct nlookupdata *nd, int uid, int gid)
3321 if ((error = nlookup(nd)) != 0)
3323 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3325 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3326 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3332 * chown(char *path, int uid, int gid)
3334 * Set ownership given a path name.
3337 sys_chown(struct chown_args *uap)
3339 struct nlookupdata nd;
3342 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3344 error = kern_chown(&nd, uap->uid, uap->gid);
3350 * lchown_args(char *path, int uid, int gid)
3352 * Set ownership given a path name, do not cross symlinks.
3355 sys_lchown(struct lchown_args *uap)
3357 struct nlookupdata nd;
3360 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3362 error = kern_chown(&nd, uap->uid, uap->gid);
3368 * fchown_args(int fd, int uid, int gid)
3370 * Set ownership given a file descriptor.
3373 sys_fchown(struct fchown_args *uap)
3375 struct thread *td = curthread;
3376 struct proc *p = td->td_proc;
3380 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3382 if (fp->f_nchandle.ncp)
3383 error = ncp_writechk(&fp->f_nchandle);
3385 error = setfown(p->p_fd->fd_ncdir.mount,
3386 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3392 * fchownat(int fd, char *path, int uid, int gid, int flags)
3394 * Set ownership of file pointed to by fd/path.
3397 sys_fchownat(struct fchownat_args *uap)
3399 struct nlookupdata nd;
3404 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3406 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3408 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3409 UIO_USERSPACE, flags);
3411 error = kern_chown(&nd, uap->uid, uap->gid);
3412 nlookup_done_at(&nd, fp);
3418 getutimes(struct timeval *tvp, struct timespec *tsp)
3420 struct timeval tv[2];
3425 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3428 if ((error = itimerfix(tvp)) != 0)
3430 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3431 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3437 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3439 struct timespec tsnow;
3453 if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3455 if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3458 if (newts[0].tv_nsec == UTIME_OMIT)
3459 newts[0].tv_sec = VNOVAL;
3460 else if (newts[0].tv_nsec == UTIME_NOW)
3462 else if ((error = itimespecfix(&newts[0])) != 0)
3465 if (newts[1].tv_nsec == UTIME_OMIT)
3466 newts[1].tv_sec = VNOVAL;
3467 else if (newts[1].tv_nsec == UTIME_NOW)
3469 else if ((error = itimespecfix(&newts[1])) != 0)
3476 setutimes(struct vnode *vp, struct vattr *vattr,
3477 const struct timespec *ts, int nullflag)
3479 struct thread *td = curthread;
3483 vattr->va_atime = ts[0];
3484 vattr->va_mtime = ts[1];
3486 vattr->va_vaflags |= VA_UTIMES_NULL;
3487 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3493 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3495 struct timespec ts[2];
3499 if ((error = getutimes(tptr, ts)) != 0)
3502 error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3507 * utimes_args(char *path, struct timeval *tptr)
3509 * Set the access and modification times of a file.
3512 sys_utimes(struct utimes_args *uap)
3514 struct timeval tv[2];
3515 struct nlookupdata nd;
3519 error = copyin(uap->tptr, tv, sizeof(tv));
3523 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3525 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3531 * lutimes_args(char *path, struct timeval *tptr)
3533 * Set the access and modification times of a file.
3536 sys_lutimes(struct lutimes_args *uap)
3538 struct timeval tv[2];
3539 struct nlookupdata nd;
3543 error = copyin(uap->tptr, tv, sizeof(tv));
3547 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3549 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3555 * Set utimes on a file descriptor. The creds used to open the
3556 * file are used to determine whether the operation is allowed
3560 kern_futimens(int fd, struct timespec *ts)
3562 struct thread *td = curthread;
3563 struct proc *p = td->td_proc;
3564 struct timespec newts[2];
3571 error = getutimens(ts, newts, &nullflag);
3574 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3576 if (fp->f_nchandle.ncp)
3577 error = ncp_writechk(&fp->f_nchandle);
3580 error = vget(vp, LK_EXCLUSIVE);
3582 error = VOP_GETATTR(vp, &vattr);
3584 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3588 error = setutimes(vp, &vattr, newts, nullflag);
3598 * futimens_args(int fd, struct timespec *ts)
3600 * Set the access and modification times of a file.
3603 sys_futimens(struct futimens_args *uap)
3605 struct timespec ts[2];
3609 error = copyin(uap->ts, ts, sizeof(ts));
3613 error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3618 kern_futimes(int fd, struct timeval *tptr)
3620 struct timespec ts[2];
3624 if ((error = getutimes(tptr, ts)) != 0)
3627 error = kern_futimens(fd, tptr ? ts : NULL);
3632 * futimes_args(int fd, struct timeval *tptr)
3634 * Set the access and modification times of a file.
3637 sys_futimes(struct futimes_args *uap)
3639 struct timeval tv[2];
3643 error = copyin(uap->tptr, tv, sizeof(tv));
3647 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3652 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3654 struct timespec newts[2];
3660 if (flags & ~AT_SYMLINK_NOFOLLOW)
3663 error = getutimens(ts, newts, &nullflag);
3667 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3668 if ((error = nlookup(nd)) != 0)
3670 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3672 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3674 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3675 error = vget(vp, LK_EXCLUSIVE);
3677 error = setutimes(vp, &vattr, newts, nullflag);
3686 * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3688 * Set file access and modification times of a file.
3691 sys_utimensat(struct utimensat_args *uap)
3693 struct timespec ts[2];
3694 struct nlookupdata nd;
3700 error = copyin(uap->ts, ts, sizeof(ts));
3705 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3706 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3707 UIO_USERSPACE, flags);
3709 error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3710 nlookup_done_at(&nd, fp);
3715 kern_truncate(struct nlookupdata *nd, off_t length)
3722 uint64_t old_size = 0;
3726 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3727 if ((error = nlookup(nd)) != 0)
3729 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3731 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3733 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3738 if (vp->v_type == VDIR) {
3742 if (vfs_quota_enabled) {
3743 error = VOP_GETATTR(vp, &vattr);
3744 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3747 old_size = vattr.va_size;
3750 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3752 vattr.va_size = length;
3753 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3754 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3762 * truncate(char *path, int pad, off_t length)
3764 * Truncate a file given its path name.
3767 sys_truncate(struct truncate_args *uap)
3769 struct nlookupdata nd;
3772 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3774 error = kern_truncate(&nd, uap->length);
3780 kern_ftruncate(int fd, off_t length)
3782 struct thread *td = curthread;
3783 struct proc *p = td->td_proc;
3790 uint64_t old_size = 0;
3795 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3797 if (fp->f_nchandle.ncp) {
3798 error = ncp_writechk(&fp->f_nchandle);
3802 if ((fp->f_flag & FWRITE) == 0) {
3806 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3810 vp = (struct vnode *)fp->f_data;
3811 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3812 if (vp->v_type == VDIR) {
3818 if (vfs_quota_enabled) {
3819 error = VOP_GETATTR(vp, &vattr);
3820 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3823 old_size = vattr.va_size;
3826 if ((error = vn_writechk(vp, NULL)) == 0) {
3828 vattr.va_size = length;
3829 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3831 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3840 * ftruncate_args(int fd, int pad, off_t length)
3842 * Truncate a file given a file descriptor.
3845 sys_ftruncate(struct ftruncate_args *uap)
3849 error = kern_ftruncate(uap->fd, uap->length);
3857 * Sync an open file.
3860 sys_fsync(struct fsync_args *uap)
3862 struct thread *td = curthread;
3863 struct proc *p = td->td_proc;
3869 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3871 vp = (struct vnode *)fp->f_data;
3872 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3873 if ((obj = vp->v_object) != NULL) {
3874 if (vp->v_mount == NULL ||
3875 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3876 vm_object_page_clean(obj, 0, 0, 0);
3879 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3880 if (error == 0 && vp->v_mount)
3881 error = buf_fsync(vp);
3889 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3891 struct nchandle fnchd;
3892 struct nchandle tnchd;
3893 struct namecache *ncp;
3902 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3903 if ((error = nlookup(fromnd)) != 0)
3905 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3907 fnchd.mount = fromnd->nl_nch.mount;
3911 * unlock the source nch so we can lookup the target nch without
3912 * deadlocking. The target may or may not exist so we do not check
3913 * for a target vp like kern_mkdir() and other creation functions do.
3915 * The source and target directories are ref'd and rechecked after
3916 * everything is relocked to determine if the source or target file
3919 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3920 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3922 fncp_gen = fromnd->nl_nch.ncp->nc_generation;
3924 cache_unlock(&fromnd->nl_nch);
3926 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3927 if ((error = nlookup(tond)) != 0) {
3931 tncp_gen = tond->nl_nch.ncp->nc_generation;
3933 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3937 tnchd.mount = tond->nl_nch.mount;
3941 * If the source and target are the same there is nothing to do
3943 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3950 * Mount points cannot be renamed or overwritten
3952 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3961 * Relock the source ncp. cache_relock() will deal with any
3962 * deadlocks against the already-locked tond and will also
3963 * make sure both are resolved.
3965 * NOTE AFTER RELOCKING: The source or target ncp may have become
3966 * invalid while they were unlocked, nc_vp and nc_mount could
3969 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3970 &tond->nl_nch, tond->nl_cred);
3971 fromnd->nl_flags |= NLC_NCPISLOCKED;
3974 * If the namecache generation changed for either fromnd or tond,
3977 if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
3978 tond->nl_nch.ncp->nc_generation != tncp_gen) {
3979 kprintf("kern_rename: retry due to gen on: "
3980 "\"%s\" -> \"%s\"\n",
3981 fromnd->nl_nch.ncp->nc_name,
3982 tond->nl_nch.ncp->nc_name);
3989 * If either fromnd or tond are marked destroyed a ripout occured
3990 * out from under us and we must retry.
3992 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
3993 fromnd->nl_nch.ncp->nc_vp == NULL ||
3994 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
3995 kprintf("kern_rename: retry due to ripout on: "
3996 "\"%s\" -> \"%s\"\n",
3997 fromnd->nl_nch.ncp->nc_name,
3998 tond->nl_nch.ncp->nc_name);
4005 * Make sure the parent directories linkages are the same.
4006 * XXX shouldn't be needed any more w/ generation check above.
4008 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
4009 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
4016 * Both the source and target must be within the same filesystem and
4017 * in the same filesystem as their parent directories within the
4018 * namecache topology.
4020 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4023 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
4024 mp != tond->nl_nch.mount) {
4031 * Make sure the mount point is writable
4033 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4040 * If the target exists and either the source or target is a directory,
4041 * then both must be directories.
4043 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4046 if (tond->nl_nch.ncp->nc_vp) {
4047 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4049 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4050 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4052 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4058 * You cannot rename a source into itself or a subdirectory of itself.
4059 * We check this by travsersing the target directory upwards looking
4060 * for a match against the source.
4065 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4066 if (fromnd->nl_nch.ncp == ncp) {
4077 * Even though the namespaces are different, they may still represent
4078 * hardlinks to the same file. The filesystem might have a hard time
4079 * with this so we issue a NREMOVE of the source instead of a NRENAME
4080 * when we detect the situation.
4083 fdvp = fromnd->nl_dvp;
4084 tdvp = tond->nl_dvp;
4085 if (fdvp == NULL || tdvp == NULL) {
4087 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4088 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4091 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4092 fdvp, tdvp, tond->nl_cred);
4099 * rename_args(char *from, char *to)
4101 * Rename files. Source and destination must either both be directories,
4102 * or both not be directories. If target is a directory, it must be empty.
4105 sys_rename(struct rename_args *uap)
4107 struct nlookupdata fromnd, tond;
4111 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4113 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4115 error = kern_rename(&fromnd, &tond);
4116 nlookup_done(&tond);
4118 nlookup_done(&fromnd);
4119 } while (error == EAGAIN);
4124 * renameat_args(int oldfd, char *old, int newfd, char *new)
4126 * Rename files using paths relative to the directories associated with
4127 * oldfd and newfd. Source and destination must either both be directories,
4128 * or both not be directories. If target is a directory, it must be empty.
4131 sys_renameat(struct renameat_args *uap)
4133 struct nlookupdata oldnd, newnd;
4134 struct file *oldfp, *newfp;
4138 error = nlookup_init_at(&oldnd, &oldfp,
4139 uap->oldfd, uap->old,
4142 error = nlookup_init_at(&newnd, &newfp,
4143 uap->newfd, uap->new,
4146 error = kern_rename(&oldnd, &newnd);
4147 nlookup_done_at(&newnd, newfp);
4149 nlookup_done_at(&oldnd, oldfp);
4150 } while (error == EAGAIN);
4155 kern_mkdir(struct nlookupdata *nd, int mode)
4157 struct thread *td = curthread;
4158 struct proc *p = td->td_proc;
4164 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4165 if ((error = nlookup(nd)) != 0)
4168 if (nd->nl_nch.ncp->nc_vp)
4170 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4173 vattr.va_type = VDIR;
4174 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4177 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4184 * mkdir_args(char *path, int mode)
4186 * Make a directory file.
4189 sys_mkdir(struct mkdir_args *uap)
4191 struct nlookupdata nd;
4194 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4196 error = kern_mkdir(&nd, uap->mode);
4202 * mkdirat_args(int fd, char *path, mode_t mode)
4204 * Make a directory file. The path is relative to the directory associated
4208 sys_mkdirat(struct mkdirat_args *uap)
4210 struct nlookupdata nd;
4214 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4216 error = kern_mkdir(&nd, uap->mode);
4217 nlookup_done_at(&nd, fp);
4222 kern_rmdir(struct nlookupdata *nd)
4227 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4228 if ((error = nlookup(nd)) != 0)
4232 * Do not allow directories representing mount points to be
4233 * deleted, even if empty. Check write perms on mount point
4234 * in case the vnode is aliased (aka nullfs).
4236 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4238 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4240 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4245 * rmdir_args(char *path)
4247 * Remove a directory file.
4250 sys_rmdir(struct rmdir_args *uap)
4252 struct nlookupdata nd;
4255 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4257 error = kern_rmdir(&nd);
4263 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4264 enum uio_seg direction)
4266 struct thread *td = curthread;
4267 struct proc *p = td->td_proc;
4275 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4277 if ((fp->f_flag & FREAD) == 0) {
4281 vp = (struct vnode *)fp->f_data;
4282 if (vp->v_type != VDIR) {
4286 aiov.iov_base = buf;
4287 aiov.iov_len = count;
4288 auio.uio_iov = &aiov;
4289 auio.uio_iovcnt = 1;
4290 auio.uio_rw = UIO_READ;
4291 auio.uio_segflg = direction;
4293 auio.uio_resid = count;
4294 loff = auio.uio_offset = fp->f_offset;
4295 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4296 fp->f_offset = auio.uio_offset;
4301 * WARNING! *basep may not be wide enough to accomodate the
4302 * seek offset. XXX should we hack this to return the upper 32 bits
4303 * for offsets greater then 4G?
4306 *basep = (long)loff;
4308 *res = count - auio.uio_resid;
4315 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4317 * Read a block of directory entries in a file system independent format.
4320 sys_getdirentries(struct getdirentries_args *uap)
4325 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4326 &uap->sysmsg_result, UIO_USERSPACE);
4328 if (error == 0 && uap->basep)
4329 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4334 * getdents_args(int fd, char *buf, size_t count)
4337 sys_getdents(struct getdents_args *uap)
4341 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4342 &uap->sysmsg_result, UIO_USERSPACE);
4348 * Set the mode mask for creation of filesystem nodes.
4350 * umask(int newmask)
4353 sys_umask(struct umask_args *uap)
4355 struct thread *td = curthread;
4356 struct proc *p = td->td_proc;
4357 struct filedesc *fdp;
4360 uap->sysmsg_result = fdp->fd_cmask;
4361 fdp->fd_cmask = uap->newmask & ALLPERMS;
4366 * revoke(char *path)
4368 * Void all references to file by ripping underlying filesystem
4372 sys_revoke(struct revoke_args *uap)
4374 struct nlookupdata nd;
4381 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4383 error = nlookup(&nd);
4385 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4386 cred = crhold(nd.nl_cred);
4390 error = VOP_GETATTR(vp, &vattr);
4391 if (error == 0 && cred->cr_uid != vattr.va_uid)
4392 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4393 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4395 error = vrevoke(vp, cred);
4396 } else if (error == 0) {
4397 error = vrevoke(vp, cred);
4407 * getfh_args(char *fname, fhandle_t *fhp)
4409 * Get (NFS) file handle
4411 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4412 * mount. This allows nullfs mounts to be explicitly exported.
4414 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4416 * nullfs mounts of subdirectories are not safe. That is, it will
4417 * work, but you do not really have protection against access to
4418 * the related parent directories.
4421 sys_getfh(struct getfh_args *uap)
4423 struct thread *td = curthread;
4424 struct nlookupdata nd;
4431 * Must be super user
4433 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4437 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4439 error = nlookup(&nd);
4441 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4442 mp = nd.nl_nch.mount;
4445 bzero(&fh, sizeof(fh));
4446 fh.fh_fsid = mp->mnt_stat.f_fsid;
4447 error = VFS_VPTOFH(vp, &fh.fh_fid);
4450 error = copyout(&fh, uap->fhp, sizeof(fh));
4456 * fhopen_args(const struct fhandle *u_fhp, int flags)
4458 * syscall for the rpc.lockd to use to translate a NFS file handle into
4459 * an open descriptor.
4461 * warning: do not remove the priv_check() call or this becomes one giant
4465 sys_fhopen(struct fhopen_args *uap)
4467 struct thread *td = curthread;
4468 struct filedesc *fdp = td->td_proc->p_fd;
4473 struct vattr *vap = &vat;
4475 int fmode, mode, error = 0, type;
4481 * Must be super user
4483 error = priv_check(td, PRIV_ROOT);
4487 fmode = FFLAGS(uap->flags);
4490 * Why not allow a non-read/write open for our lockd?
4492 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4494 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4499 * Find the mount point
4501 mp = vfs_getvfs(&fhp.fh_fsid);
4506 /* now give me my vnode, it gets returned to me locked */
4507 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4511 * from now on we have to make sure not
4512 * to forget about the vnode
4513 * any error that causes an abort must vput(vp)
4514 * just set error = err and 'goto bad;'.
4520 if (vp->v_type == VLNK) {
4524 if (vp->v_type == VSOCK) {
4529 if (fmode & (FWRITE | O_TRUNC)) {
4530 if (vp->v_type == VDIR) {
4534 error = vn_writechk(vp, NULL);
4542 error = VOP_ACCESS(vp, mode, td->td_ucred);
4546 if (fmode & O_TRUNC) {
4547 vn_unlock(vp); /* XXX */
4548 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4551 error = VOP_SETATTR(vp, vap, td->td_ucred);
4557 * VOP_OPEN needs the file pointer so it can potentially override
4560 * WARNING! no f_nchandle will be associated when fhopen()ing a
4563 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4567 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4570 * setting f_ops this way prevents VOP_CLOSE from being
4571 * called or fdrop() releasing the vp from v_data. Since
4572 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4574 fp->f_ops = &badfileops;
4580 * The fp is given its own reference, we still have our ref and lock.
4582 * Assert that all regular files must be created with a VM object.
4584 if (vp->v_type == VREG && vp->v_object == NULL) {
4585 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4590 * The open was successful. Handle any locking requirements.
4592 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4593 lf.l_whence = SEEK_SET;
4596 if (fmode & O_EXLOCK)
4597 lf.l_type = F_WRLCK;
4599 lf.l_type = F_RDLCK;
4600 if (fmode & FNONBLOCK)
4605 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4607 * release our private reference.
4609 fsetfd(fdp, NULL, indx);
4614 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4615 atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
4619 * Clean up. Associate the file pointer with the previously
4620 * reserved descriptor and return it.
4623 if (uap->flags & O_CLOEXEC)
4624 fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4625 fsetfd(fdp, fp, indx);
4627 uap->sysmsg_result = indx;
4631 fsetfd(fdp, NULL, indx);
4640 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4643 sys_fhstat(struct fhstat_args *uap)
4645 struct thread *td = curthread;
4653 * Must be super user
4655 error = priv_check(td, PRIV_ROOT);
4659 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4663 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4666 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4667 error = vn_stat(vp, &sb, td->td_ucred);
4672 error = copyout(&sb, uap->sb, sizeof(sb));
4677 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4680 sys_fhstatfs(struct fhstatfs_args *uap)
4682 struct thread *td = curthread;
4683 struct proc *p = td->td_proc;
4688 char *fullpath, *freepath;
4693 * Must be super user
4695 if ((error = priv_check(td, PRIV_ROOT)))
4698 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4701 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4705 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4710 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4715 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4718 error = mount_path(p, mp, &fullpath, &freepath);
4721 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4722 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4723 kfree(freepath, M_TEMP);
4725 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4726 if (priv_check(td, PRIV_ROOT)) {
4727 bcopy(sp, &sb, sizeof(sb));
4728 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4731 error = copyout(sp, uap->buf, sizeof(*sp));
4737 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4740 sys_fhstatvfs(struct fhstatvfs_args *uap)
4742 struct thread *td = curthread;
4743 struct proc *p = td->td_proc;
4751 * Must be super user
4753 if ((error = priv_check(td, PRIV_ROOT)))
4756 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4759 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4763 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4768 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4771 sp = &mp->mnt_vstat;
4773 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4777 if (mp->mnt_flag & MNT_RDONLY)
4778 sp->f_flag |= ST_RDONLY;
4779 if (mp->mnt_flag & MNT_NOSUID)
4780 sp->f_flag |= ST_NOSUID;
4781 error = copyout(sp, uap->buf, sizeof(*sp));
4788 * Syscall to push extended attribute configuration information into the
4789 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4790 * a command (int cmd), and attribute name and misc data. For now, the
4791 * attribute name is left in userspace for consumption by the VFS_op.
4792 * It will probably be changed to be copied into sysspace by the
4793 * syscall in the future, once issues with various consumers of the
4794 * attribute code have raised their hands.
4796 * Currently this is used only by UFS Extended Attributes.
4799 sys_extattrctl(struct extattrctl_args *uap)
4801 struct nlookupdata nd;
4803 char attrname[EXTATTR_MAXNAMELEN];
4811 if (error == 0 && uap->filename) {
4812 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4815 error = nlookup(&nd);
4817 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4821 if (error == 0 && uap->attrname) {
4822 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4827 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4829 error = nlookup(&nd);
4831 error = ncp_writechk(&nd.nl_nch);
4833 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4835 uap->attrname, nd.nl_cred);
4844 * Syscall to get a named extended attribute on a file or directory.
4847 sys_extattr_set_file(struct extattr_set_file_args *uap)
4849 char attrname[EXTATTR_MAXNAMELEN];
4850 struct nlookupdata nd;
4856 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4862 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4864 error = nlookup(&nd);
4866 error = ncp_writechk(&nd.nl_nch);
4868 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4874 bzero(&auio, sizeof(auio));
4875 aiov.iov_base = uap->data;
4876 aiov.iov_len = uap->nbytes;
4877 auio.uio_iov = &aiov;
4878 auio.uio_iovcnt = 1;
4879 auio.uio_offset = 0;
4880 auio.uio_resid = uap->nbytes;
4881 auio.uio_rw = UIO_WRITE;
4882 auio.uio_td = curthread;
4884 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4893 * Syscall to get a named extended attribute on a file or directory.
4896 sys_extattr_get_file(struct extattr_get_file_args *uap)
4898 char attrname[EXTATTR_MAXNAMELEN];
4899 struct nlookupdata nd;
4905 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4911 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4913 error = nlookup(&nd);
4915 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
4921 bzero(&auio, sizeof(auio));
4922 aiov.iov_base = uap->data;
4923 aiov.iov_len = uap->nbytes;
4924 auio.uio_iov = &aiov;
4925 auio.uio_iovcnt = 1;
4926 auio.uio_offset = 0;
4927 auio.uio_resid = uap->nbytes;
4928 auio.uio_rw = UIO_READ;
4929 auio.uio_td = curthread;
4931 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4933 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4941 * Syscall to delete a named extended attribute from a file or directory.
4942 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4945 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4947 char attrname[EXTATTR_MAXNAMELEN];
4948 struct nlookupdata nd;
4952 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4956 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4958 error = nlookup(&nd);
4960 error = ncp_writechk(&nd.nl_nch);
4962 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4964 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4965 attrname, NULL, nd.nl_cred);
4974 * Determine if the mount is visible to the process.
4977 chroot_visible_mnt(struct mount *mp, struct proc *p)
4979 struct nchandle nch;
4982 * Traverse from the mount point upwards. If we hit the process
4983 * root then the mount point is visible to the process.
4985 nch = mp->mnt_ncmountpt;
4987 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4988 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4991 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4992 nch = nch.mount->mnt_ncmounton;
4994 nch.ncp = nch.ncp->nc_parent;
4999 * If the mount point is not visible to the process, but the
5000 * process root is in a subdirectory of the mount, return
5003 if (p->p_fd->fd_nrdir.mount == mp)