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 * 4. 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 #include <vfs/union/union.h>
81 static void mount_warning(struct mount *mp, const char *ctl, ...)
83 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
84 static int checkvp_chdir (struct vnode *vn, struct thread *td);
85 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
86 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
87 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
88 static int getutimes (const struct timeval *, struct timespec *);
89 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
90 static int setfmode (struct vnode *, int);
91 static int setfflags (struct vnode *, int);
92 static int setutimes (struct vnode *, struct vattr *,
93 const struct timespec *, int);
94 static int usermount = 0; /* if 1, non-root can mount fs. */
96 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
98 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
99 "Allow non-root users to mount filesystems");
102 * Virtual File System System Calls
106 * Mount a file system.
108 * mount_args(char *type, char *path, int flags, caddr_t data)
113 sys_mount(struct mount_args *uap)
115 struct thread *td = curthread;
118 struct mount *mp, *nullmp;
119 struct vfsconf *vfsp;
120 int error, flag = 0, flag2 = 0;
123 struct nlookupdata nd;
124 char fstypename[MFSNAMELEN];
132 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
136 * Do not allow NFS export by non-root users.
138 if (uap->flags & MNT_EXPORTED) {
139 error = priv_check(td, PRIV_ROOT);
144 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
146 if (priv_check(td, PRIV_ROOT))
147 uap->flags |= MNT_NOSUID | MNT_NODEV;
150 * Lookup the requested path and extract the nch and vnode.
152 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
154 if ((error = nlookup(&nd)) == 0) {
155 if (nd.nl_nch.ncp->nc_vp == NULL)
165 * If the target filesystem is resolved via a nullfs mount, then
166 * nd.nl_nch.mount will be pointing to the nullfs mount structure
167 * instead of the target file system. We need it in case we are
170 nullmp = nd.nl_nch.mount;
173 * Extract the locked+refd ncp and cleanup the nd structure
176 cache_zero(&nd.nl_nch);
179 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
180 (mp = cache_findmount(&nch)) != NULL) {
189 * now we have the locked ref'd nch and unreferenced vnode.
192 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
199 * Extract the file system type. We need to know this early, to take
200 * appropriate actions if we are dealing with a nullfs.
202 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
209 * Now we have an unlocked ref'd nch and a locked ref'd vp
211 if (uap->flags & MNT_UPDATE) {
212 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
219 if (strncmp(fstypename, "null", 5) == 0) {
227 flag2 = mp->mnt_kern_flag;
229 * We only allow the filesystem to be reloaded if it
230 * is currently mounted read-only.
232 if ((uap->flags & MNT_RELOAD) &&
233 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
236 error = EOPNOTSUPP; /* Needs translation */
240 * Only root, or the user that did the original mount is
241 * permitted to update it.
243 if (mp->mnt_stat.f_owner != cred->cr_uid &&
244 (error = priv_check(td, PRIV_ROOT))) {
249 if (vfs_busy(mp, LK_NOWAIT)) {
263 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
264 lwkt_gettoken(&mp->mnt_token);
270 * If the user is not root, ensure that they own the directory
271 * onto which we are attempting to mount.
273 if ((error = VOP_GETATTR(vp, &va)) ||
274 (va.va_uid != cred->cr_uid &&
275 (error = priv_check(td, PRIV_ROOT)))) {
280 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
285 if (vp->v_type != VDIR) {
291 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
297 vfsp = vfsconf_find_by_name(fstypename);
301 /* Only load modules for root (very important!) */
302 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
307 error = linker_load_file(fstypename, &lf);
308 if (error || lf == NULL) {
316 /* lookup again, see if the VFS was loaded */
317 vfsp = vfsconf_find_by_name(fstypename);
320 linker_file_unload(lf);
335 * Allocate and initialize the filesystem.
337 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
339 vfs_busy(mp, LK_NOWAIT);
340 mp->mnt_op = vfsp->vfc_vfsops;
342 vfsp->vfc_refcount++;
343 mp->mnt_stat.f_type = vfsp->vfc_typenum;
344 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
345 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
346 mp->mnt_stat.f_owner = cred->cr_uid;
347 lwkt_gettoken(&mp->mnt_token);
351 * (per-mount token acquired at this point)
353 * Set the mount level flags.
355 if (uap->flags & MNT_RDONLY)
356 mp->mnt_flag |= MNT_RDONLY;
357 else if (mp->mnt_flag & MNT_RDONLY)
358 mp->mnt_kern_flag |= MNTK_WANTRDWR;
359 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
360 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
361 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
362 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
363 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
364 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
365 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
366 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
368 * Mount the filesystem.
369 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
372 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
373 if (mp->mnt_flag & MNT_UPDATE) {
374 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
375 mp->mnt_flag &= ~MNT_RDONLY;
376 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
377 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
380 mp->mnt_kern_flag = flag2;
382 lwkt_reltoken(&mp->mnt_token);
388 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
391 * Put the new filesystem on the mount list after root. The mount
392 * point gets its own mnt_ncmountpt (unless the VFS already set one
393 * up) which represents the root of the mount. The lookup code
394 * detects the mount point going forward and checks the root of
395 * the mount going backwards.
397 * It is not necessary to invalidate or purge the vnode underneath
398 * because elements under the mount will be given their own glue
402 if (mp->mnt_ncmountpt.ncp == NULL) {
404 * allocate, then unlock, but leave the ref intact
406 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
407 cache_unlock(&mp->mnt_ncmountpt);
409 mp->mnt_ncmounton = nch; /* inherits ref */
410 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
411 cache_ismounting(mp);
413 mountlist_insert(mp, MNTINS_LAST);
415 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
416 error = vfs_allocate_syncvnode(mp);
417 lwkt_reltoken(&mp->mnt_token);
419 error = VFS_START(mp, 0);
422 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
423 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
424 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
425 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
426 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
427 mp->mnt_vfc->vfc_refcount--;
428 lwkt_reltoken(&mp->mnt_token);
439 * Scan all active processes to see if any of them have a current
440 * or root directory onto which the new filesystem has just been
441 * mounted. If so, replace them with the new mount point.
443 * Both old_nch and new_nch are ref'd on call but not locked.
444 * new_nch must be temporarily locked so it can be associated with the
445 * vnode representing the root of the mount point.
447 struct checkdirs_info {
448 struct nchandle old_nch;
449 struct nchandle new_nch;
450 struct vnode *old_vp;
451 struct vnode *new_vp;
454 static int checkdirs_callback(struct proc *p, void *data);
457 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
459 struct checkdirs_info info;
465 * If the old mount point's vnode has a usecount of 1, it is not
466 * being held as a descriptor anywhere.
468 olddp = old_nch->ncp->nc_vp;
469 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
473 * Force the root vnode of the new mount point to be resolved
474 * so we can update any matching processes.
477 if (VFS_ROOT(mp, &newdp))
478 panic("mount: lost mount");
481 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
482 cache_setunresolved(new_nch);
483 cache_setvp(new_nch, newdp);
484 cache_unlock(new_nch);
487 * Special handling of the root node
489 if (rootvnode == olddp) {
491 vfs_cache_setroot(newdp, cache_hold(new_nch));
495 * Pass newdp separately so the callback does not have to access
496 * it via new_nch->ncp->nc_vp.
498 info.old_nch = *old_nch;
499 info.new_nch = *new_nch;
501 allproc_scan(checkdirs_callback, &info);
506 * NOTE: callback is not MP safe because the scanned process's filedesc
507 * structure can be ripped out from under us, amoung other things.
510 checkdirs_callback(struct proc *p, void *data)
512 struct checkdirs_info *info = data;
513 struct filedesc *fdp;
514 struct nchandle ncdrop1;
515 struct nchandle ncdrop2;
516 struct vnode *vprele1;
517 struct vnode *vprele2;
519 if ((fdp = p->p_fd) != NULL) {
520 cache_zero(&ncdrop1);
521 cache_zero(&ncdrop2);
526 * MPUNSAFE - XXX fdp can be pulled out from under a
529 * A shared filedesc is ok, we don't have to copy it
530 * because we are making this change globally.
532 spin_lock(&fdp->fd_spin);
533 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
534 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
535 vprele1 = fdp->fd_cdir;
537 fdp->fd_cdir = info->new_vp;
538 ncdrop1 = fdp->fd_ncdir;
539 cache_copy(&info->new_nch, &fdp->fd_ncdir);
541 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
542 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
543 vprele2 = fdp->fd_rdir;
545 fdp->fd_rdir = info->new_vp;
546 ncdrop2 = fdp->fd_nrdir;
547 cache_copy(&info->new_nch, &fdp->fd_nrdir);
549 spin_unlock(&fdp->fd_spin);
551 cache_drop(&ncdrop1);
553 cache_drop(&ncdrop2);
563 * Unmount a file system.
565 * Note: unmount takes a path to the vnode mounted on as argument,
566 * not special file (as before).
568 * umount_args(char *path, int flags)
573 sys_unmount(struct unmount_args *uap)
575 struct thread *td = curthread;
576 struct proc *p __debugvar = td->td_proc;
577 struct mount *mp = NULL;
578 struct nlookupdata nd;
583 if (td->td_ucred->cr_prison != NULL) {
587 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
590 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
592 error = nlookup(&nd);
596 mp = nd.nl_nch.mount;
599 * Only root, or the user that did the original mount is
600 * permitted to unmount this filesystem.
602 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
603 (error = priv_check(td, PRIV_ROOT)))
607 * Don't allow unmounting the root file system.
609 if (mp->mnt_flag & MNT_ROOTFS) {
615 * Must be the root of the filesystem
617 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
625 error = dounmount(mp, uap->flags);
632 * Do the actual file system unmount.
635 dounmount_interlock(struct mount *mp)
637 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
639 mp->mnt_kern_flag |= MNTK_UNMOUNT;
644 unmount_allproc_cb(struct proc *p, void *arg)
648 if (p->p_textnch.ncp == NULL)
651 mp = (struct mount *)arg;
652 if (p->p_textnch.mount == mp)
653 cache_drop(&p->p_textnch);
659 dounmount(struct mount *mp, int flags)
661 struct namecache *ncp;
670 lwkt_gettoken(&mp->mnt_token);
672 * Exclusive access for unmounting purposes
674 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
678 * Allow filesystems to detect that a forced unmount is in progress.
680 if (flags & MNT_FORCE)
681 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
682 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
683 error = lockmgr(&mp->mnt_lock, lflags);
685 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
686 if (mp->mnt_kern_flag & MNTK_MWAIT) {
687 mp->mnt_kern_flag &= ~MNTK_MWAIT;
693 if (mp->mnt_flag & MNT_EXPUBLIC)
694 vfs_setpublicfs(NULL, NULL, NULL);
696 vfs_msync(mp, MNT_WAIT);
697 async_flag = mp->mnt_flag & MNT_ASYNC;
698 mp->mnt_flag &=~ MNT_ASYNC;
701 * If this filesystem isn't aliasing other filesystems,
702 * try to invalidate any remaining namecache entries and
703 * check the count afterwords.
705 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
706 cache_lock(&mp->mnt_ncmountpt);
707 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
708 cache_unlock(&mp->mnt_ncmountpt);
710 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
711 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
712 allproc_scan(&unmount_allproc_cb, mp);
715 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
716 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
718 if ((flags & MNT_FORCE) == 0) {
720 mount_warning(mp, "Cannot unmount: "
726 mount_warning(mp, "Forced unmount: "
737 * Decomission our special mnt_syncer vnode. This also stops
738 * the vnlru code. If we are unable to unmount we recommission
741 * Then sync the filesystem.
743 if ((vp = mp->mnt_syncer) != NULL) {
744 mp->mnt_syncer = NULL;
747 if ((mp->mnt_flag & MNT_RDONLY) == 0)
748 VFS_SYNC(mp, MNT_WAIT);
751 * nchandle records ref the mount structure. Expect a count of 1
752 * (our mount->mnt_ncmountpt).
754 * Scans can get temporary refs on a mountpoint (thought really
755 * heavy duty stuff like cache_findmount() do not).
757 for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
758 cache_unmounting(mp);
759 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
761 if (mp->mnt_refs != 1) {
762 if ((flags & MNT_FORCE) == 0) {
763 mount_warning(mp, "Cannot unmount: "
764 "%d mount refs still present",
768 mount_warning(mp, "Forced unmount: "
769 "%d mount refs still present",
776 * So far so good, sync the filesystem once more and
777 * call the VFS unmount code if the sync succeeds.
780 if (((mp->mnt_flag & MNT_RDONLY) ||
781 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
782 (flags & MNT_FORCE)) {
783 error = VFS_UNMOUNT(mp, flags);
788 * If an error occurred we can still recover, restoring the
789 * syncer vnode and misc flags.
792 if (mp->mnt_syncer == NULL)
793 vfs_allocate_syncvnode(mp);
794 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
795 mp->mnt_flag |= async_flag;
796 lockmgr(&mp->mnt_lock, LK_RELEASE);
797 if (mp->mnt_kern_flag & MNTK_MWAIT) {
798 mp->mnt_kern_flag &= ~MNTK_MWAIT;
804 * Clean up any journals still associated with the mount after
805 * filesystem activity has ceased.
807 journal_remove_all_journals(mp,
808 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
810 mountlist_remove(mp);
813 * Remove any installed vnode ops here so the individual VFSs don't
816 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
817 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
818 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
819 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
820 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
822 if (mp->mnt_ncmountpt.ncp != NULL) {
823 nch = mp->mnt_ncmountpt;
824 cache_zero(&mp->mnt_ncmountpt);
825 cache_clrmountpt(&nch);
828 if (mp->mnt_ncmounton.ncp != NULL) {
829 cache_unmounting(mp);
830 nch = mp->mnt_ncmounton;
831 cache_zero(&mp->mnt_ncmounton);
832 cache_clrmountpt(&nch);
836 mp->mnt_vfc->vfc_refcount--;
837 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
838 panic("unmount: dangling vnode");
839 lockmgr(&mp->mnt_lock, LK_RELEASE);
840 if (mp->mnt_kern_flag & MNTK_MWAIT) {
841 mp->mnt_kern_flag &= ~MNTK_MWAIT;
846 * If we reach here and freeok != 0 we must free the mount.
847 * If refs > 1 cycle and wait, just in case someone tried
848 * to busy the mount after we decided to do the unmount.
851 while (mp->mnt_refs > 1) {
852 cache_unmounting(mp);
854 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
856 lwkt_reltoken(&mp->mnt_token);
863 lwkt_reltoken(&mp->mnt_token);
869 mount_warning(struct mount *mp, const char *ctl, ...)
876 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
877 &ptr, &buf, 0) == 0) {
878 kprintf("unmount(%s): ", ptr);
883 kprintf("unmount(%p", mp);
884 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
885 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
894 * Shim cache_fullpath() to handle the case where a process is chrooted into
895 * a subdirectory of a mount. In this case if the root mount matches the
896 * process root directory's mount we have to specify the process's root
897 * directory instead of the mount point, because the mount point might
898 * be above the root directory.
902 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
904 struct nchandle *nch;
906 if (p && p->p_fd->fd_nrdir.mount == mp)
907 nch = &p->p_fd->fd_nrdir;
909 nch = &mp->mnt_ncmountpt;
910 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
914 * Sync each mounted filesystem.
918 static int syncprt = 0;
919 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
922 static int sync_callback(struct mount *mp, void *data);
925 sys_sync(struct sync_args *uap)
927 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
930 * print out buffer pool stat information on each sync() call.
940 sync_callback(struct mount *mp, void *data __unused)
944 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
945 asyncflag = mp->mnt_flag & MNT_ASYNC;
946 mp->mnt_flag &= ~MNT_ASYNC;
947 vfs_msync(mp, MNT_NOWAIT);
948 VFS_SYNC(mp, MNT_NOWAIT);
949 mp->mnt_flag |= asyncflag;
954 /* XXX PRISON: could be per prison flag */
955 static int prison_quotas;
957 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
961 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
963 * Change filesystem quotas.
968 sys_quotactl(struct quotactl_args *uap)
970 struct nlookupdata nd;
977 if (td->td_ucred->cr_prison && !prison_quotas) {
982 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
984 error = nlookup(&nd);
986 mp = nd.nl_nch.mount;
987 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
988 uap->arg, nd.nl_cred);
997 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
998 * void *buf, int buflen)
1000 * This function operates on a mount point and executes the specified
1001 * operation using the specified control data, and possibly returns data.
1003 * The actual number of bytes stored in the result buffer is returned, 0
1004 * if none, otherwise an error is returned.
1009 sys_mountctl(struct mountctl_args *uap)
1011 struct thread *td = curthread;
1012 struct proc *p = td->td_proc;
1020 * Sanity and permissions checks. We must be root.
1023 if (td->td_ucred->cr_prison != NULL)
1025 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1026 (error = priv_check(td, PRIV_ROOT)) != 0)
1030 * Argument length checks
1032 if (uap->ctllen < 0 || uap->ctllen > 1024)
1034 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1036 if (uap->path == NULL)
1040 * Allocate the necessary buffers and copyin data
1042 path = objcache_get(namei_oc, M_WAITOK);
1043 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1048 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1049 error = copyin(uap->ctl, ctl, uap->ctllen);
1054 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1057 * Validate the descriptor
1060 fp = holdfp(p->p_fd, uap->fd, -1);
1070 * Execute the internal kernel function and clean up.
1073 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1077 if (error == 0 && uap->sysmsg_result > 0)
1078 error = copyout(buf, uap->buf, uap->sysmsg_result);
1081 objcache_put(namei_oc, path);
1090 * Execute a mount control operation by resolving the path to a mount point
1091 * and calling vop_mountctl().
1093 * Use the mount point from the nch instead of the vnode so nullfs mounts
1094 * can properly spike the VOP.
1097 kern_mountctl(const char *path, int op, struct file *fp,
1098 const void *ctl, int ctllen,
1099 void *buf, int buflen, int *res)
1103 struct nlookupdata nd;
1108 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1110 error = nlookup(&nd);
1112 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1113 mp = nd.nl_nch.mount;
1120 * Must be the root of the filesystem
1122 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1126 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1133 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1135 struct thread *td = curthread;
1136 struct proc *p = td->td_proc;
1139 char *fullpath, *freepath;
1142 if ((error = nlookup(nd)) != 0)
1144 mp = nd->nl_nch.mount;
1146 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1149 error = mount_path(p, mp, &fullpath, &freepath);
1152 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1153 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1154 kfree(freepath, M_TEMP);
1156 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1157 bcopy(sp, buf, sizeof(*buf));
1158 /* Only root should have access to the fsid's. */
1159 if (priv_check(td, PRIV_ROOT))
1160 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1165 * statfs_args(char *path, struct statfs *buf)
1167 * Get filesystem statistics.
1170 sys_statfs(struct statfs_args *uap)
1172 struct nlookupdata nd;
1176 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1178 error = kern_statfs(&nd, &buf);
1181 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1186 kern_fstatfs(int fd, struct statfs *buf)
1188 struct thread *td = curthread;
1189 struct proc *p = td->td_proc;
1193 char *fullpath, *freepath;
1197 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1201 * Try to use mount info from any overlays rather than the
1202 * mount info for the underlying vnode, otherwise we will
1203 * fail when operating on null-mounted paths inside a chroot.
1205 if ((mp = fp->f_nchandle.mount) == NULL)
1206 mp = ((struct vnode *)fp->f_data)->v_mount;
1211 if (fp->f_cred == NULL) {
1216 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1219 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1221 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1222 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1223 kfree(freepath, M_TEMP);
1225 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1226 bcopy(sp, buf, sizeof(*buf));
1228 /* Only root should have access to the fsid's. */
1229 if (priv_check(td, PRIV_ROOT))
1230 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1238 * fstatfs_args(int fd, struct statfs *buf)
1240 * Get filesystem statistics.
1243 sys_fstatfs(struct fstatfs_args *uap)
1248 error = kern_fstatfs(uap->fd, &buf);
1251 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1256 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1262 if ((error = nlookup(nd)) != 0)
1264 mp = nd->nl_nch.mount;
1265 sp = &mp->mnt_vstat;
1266 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1270 if (mp->mnt_flag & MNT_RDONLY)
1271 sp->f_flag |= ST_RDONLY;
1272 if (mp->mnt_flag & MNT_NOSUID)
1273 sp->f_flag |= ST_NOSUID;
1274 bcopy(sp, buf, sizeof(*buf));
1279 * statfs_args(char *path, struct statfs *buf)
1281 * Get filesystem statistics.
1284 sys_statvfs(struct statvfs_args *uap)
1286 struct nlookupdata nd;
1290 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1292 error = kern_statvfs(&nd, &buf);
1295 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1300 kern_fstatvfs(int fd, struct statvfs *buf)
1302 struct thread *td = curthread;
1303 struct proc *p = td->td_proc;
1310 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1312 if ((mp = fp->f_nchandle.mount) == NULL)
1313 mp = ((struct vnode *)fp->f_data)->v_mount;
1318 if (fp->f_cred == NULL) {
1322 sp = &mp->mnt_vstat;
1323 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1327 if (mp->mnt_flag & MNT_RDONLY)
1328 sp->f_flag |= ST_RDONLY;
1329 if (mp->mnt_flag & MNT_NOSUID)
1330 sp->f_flag |= ST_NOSUID;
1332 bcopy(sp, buf, sizeof(*buf));
1340 * fstatfs_args(int fd, struct statfs *buf)
1342 * Get filesystem statistics.
1345 sys_fstatvfs(struct fstatvfs_args *uap)
1350 error = kern_fstatvfs(uap->fd, &buf);
1353 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1358 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1360 * Get statistics on all filesystems.
1363 struct getfsstat_info {
1364 struct statfs *sfsp;
1372 static int getfsstat_callback(struct mount *, void *);
1375 sys_getfsstat(struct getfsstat_args *uap)
1377 struct thread *td = curthread;
1378 struct getfsstat_info info;
1380 bzero(&info, sizeof(info));
1382 info.maxcount = uap->bufsize / sizeof(struct statfs);
1383 info.sfsp = uap->buf;
1385 info.flags = uap->flags;
1388 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1389 if (info.sfsp && info.count > info.maxcount)
1390 uap->sysmsg_result = info.maxcount;
1392 uap->sysmsg_result = info.count;
1393 return (info.error);
1397 getfsstat_callback(struct mount *mp, void *data)
1399 struct getfsstat_info *info = data;
1405 if (info->sfsp && info->count < info->maxcount) {
1406 if (info->td->td_proc &&
1407 !chroot_visible_mnt(mp, info->td->td_proc)) {
1413 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1414 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1415 * overrides MNT_WAIT.
1417 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1418 (info->flags & MNT_WAIT)) &&
1419 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1422 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1424 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1426 info->error = error;
1429 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1430 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1431 kfree(freepath, M_TEMP);
1433 error = copyout(sp, info->sfsp, sizeof(*sp));
1435 info->error = error;
1445 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1446 long bufsize, int flags)
1448 * Get statistics on all filesystems.
1451 struct getvfsstat_info {
1452 struct statfs *sfsp;
1453 struct statvfs *vsfsp;
1461 static int getvfsstat_callback(struct mount *, void *);
1464 sys_getvfsstat(struct getvfsstat_args *uap)
1466 struct thread *td = curthread;
1467 struct getvfsstat_info info;
1469 bzero(&info, sizeof(info));
1471 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1472 info.sfsp = uap->buf;
1473 info.vsfsp = uap->vbuf;
1475 info.flags = uap->flags;
1478 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1479 if (info.vsfsp && info.count > info.maxcount)
1480 uap->sysmsg_result = info.maxcount;
1482 uap->sysmsg_result = info.count;
1483 return (info.error);
1487 getvfsstat_callback(struct mount *mp, void *data)
1489 struct getvfsstat_info *info = data;
1491 struct statvfs *vsp;
1496 if (info->vsfsp && info->count < info->maxcount) {
1497 if (info->td->td_proc &&
1498 !chroot_visible_mnt(mp, info->td->td_proc)) {
1502 vsp = &mp->mnt_vstat;
1505 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1506 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1507 * overrides MNT_WAIT.
1509 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1510 (info->flags & MNT_WAIT)) &&
1511 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1514 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1516 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1517 (info->flags & MNT_WAIT)) &&
1518 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1522 if (mp->mnt_flag & MNT_RDONLY)
1523 vsp->f_flag |= ST_RDONLY;
1524 if (mp->mnt_flag & MNT_NOSUID)
1525 vsp->f_flag |= ST_NOSUID;
1527 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1529 info->error = error;
1532 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1533 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1534 kfree(freepath, M_TEMP);
1536 error = copyout(sp, info->sfsp, sizeof(*sp));
1538 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1540 info->error = error;
1552 * fchdir_args(int fd)
1554 * Change current working directory to a given file descriptor.
1557 sys_fchdir(struct fchdir_args *uap)
1559 struct thread *td = curthread;
1560 struct proc *p = td->td_proc;
1561 struct filedesc *fdp = p->p_fd;
1562 struct vnode *vp, *ovp;
1565 struct nchandle nch, onch, tnch;
1568 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1570 lwkt_gettoken(&p->p_token);
1571 vp = (struct vnode *)fp->f_data;
1573 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1574 if (fp->f_nchandle.ncp == NULL)
1577 error = checkvp_chdir(vp, td);
1582 cache_copy(&fp->f_nchandle, &nch);
1585 * If the ncp has become a mount point, traverse through
1589 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1590 (mp = cache_findmount(&nch)) != NULL
1592 error = nlookup_mp(mp, &tnch);
1594 cache_unlock(&tnch); /* leave ref intact */
1596 vp = tnch.ncp->nc_vp;
1597 error = vget(vp, LK_SHARED);
1598 KKASSERT(error == 0);
1602 cache_dropmount(mp);
1606 onch = fdp->fd_ncdir;
1607 vn_unlock(vp); /* leave ref intact */
1609 fdp->fd_ncdir = nch;
1618 lwkt_reltoken(&p->p_token);
1623 kern_chdir(struct nlookupdata *nd)
1625 struct thread *td = curthread;
1626 struct proc *p = td->td_proc;
1627 struct filedesc *fdp = p->p_fd;
1628 struct vnode *vp, *ovp;
1629 struct nchandle onch;
1632 if ((error = nlookup(nd)) != 0)
1634 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1636 if ((error = vget(vp, LK_SHARED)) != 0)
1639 lwkt_gettoken(&p->p_token);
1640 error = checkvp_chdir(vp, td);
1644 onch = fdp->fd_ncdir;
1645 cache_unlock(&nd->nl_nch); /* leave reference intact */
1646 fdp->fd_ncdir = nd->nl_nch;
1650 cache_zero(&nd->nl_nch);
1654 lwkt_reltoken(&p->p_token);
1659 * chdir_args(char *path)
1661 * Change current working directory (``.'').
1664 sys_chdir(struct chdir_args *uap)
1666 struct nlookupdata nd;
1669 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1671 error = kern_chdir(&nd);
1677 * Helper function for raised chroot(2) security function: Refuse if
1678 * any filedescriptors are open directories.
1681 chroot_refuse_vdir_fds(struct filedesc *fdp)
1688 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1689 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1691 vp = (struct vnode *)fp->f_data;
1692 if (vp->v_type != VDIR) {
1703 * This sysctl determines if we will allow a process to chroot(2) if it
1704 * has a directory open:
1705 * 0: disallowed for all processes.
1706 * 1: allowed for processes that were not already chroot(2)'ed.
1707 * 2: allowed for all processes.
1710 static int chroot_allow_open_directories = 1;
1712 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1713 &chroot_allow_open_directories, 0, "");
1716 * chroot to the specified namecache entry. We obtain the vp from the
1717 * namecache data. The passed ncp must be locked and referenced and will
1718 * remain locked and referenced on return.
1721 kern_chroot(struct nchandle *nch)
1723 struct thread *td = curthread;
1724 struct proc *p = td->td_proc;
1725 struct filedesc *fdp = p->p_fd;
1730 * Only privileged user can chroot
1732 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1737 * Disallow open directory descriptors (fchdir() breakouts).
1739 if (chroot_allow_open_directories == 0 ||
1740 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1741 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1744 if ((vp = nch->ncp->nc_vp) == NULL)
1747 if ((error = vget(vp, LK_SHARED)) != 0)
1751 * Check the validity of vp as a directory to change to and
1752 * associate it with rdir/jdir.
1754 error = checkvp_chdir(vp, td);
1755 vn_unlock(vp); /* leave reference intact */
1757 vrele(fdp->fd_rdir);
1758 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1759 cache_drop(&fdp->fd_nrdir);
1760 cache_copy(nch, &fdp->fd_nrdir);
1761 if (fdp->fd_jdir == NULL) {
1764 cache_copy(nch, &fdp->fd_njdir);
1773 * chroot_args(char *path)
1775 * Change notion of root (``/'') directory.
1778 sys_chroot(struct chroot_args *uap)
1780 struct thread *td __debugvar = curthread;
1781 struct nlookupdata nd;
1784 KKASSERT(td->td_proc);
1785 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1787 nd.nl_flags |= NLC_EXEC;
1788 error = nlookup(&nd);
1790 error = kern_chroot(&nd.nl_nch);
1797 sys_chroot_kernel(struct chroot_kernel_args *uap)
1799 struct thread *td = curthread;
1800 struct nlookupdata nd;
1801 struct nchandle *nch;
1805 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1809 error = nlookup(&nd);
1815 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1819 if ((vp = nch->ncp->nc_vp) == NULL) {
1824 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1827 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1829 vfs_cache_setroot(vp, cache_hold(nch));
1839 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1840 * determine whether it is legal to chdir to the vnode. The vnode's state
1841 * is not changed by this call.
1844 checkvp_chdir(struct vnode *vp, struct thread *td)
1848 if (vp->v_type != VDIR)
1851 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1856 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1858 struct thread *td = curthread;
1859 struct proc *p = td->td_proc;
1860 struct lwp *lp = td->td_lwp;
1861 struct filedesc *fdp = p->p_fd;
1866 int type, indx, error = 0;
1869 if ((oflags & O_ACCMODE) == O_ACCMODE)
1871 flags = FFLAGS(oflags);
1872 error = falloc(lp, &nfp, NULL);
1876 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1879 * XXX p_dupfd is a real mess. It allows a device to return a
1880 * file descriptor to be duplicated rather then doing the open
1886 * Call vn_open() to do the lookup and assign the vnode to the
1887 * file pointer. vn_open() does not change the ref count on fp
1888 * and the vnode, on success, will be inherited by the file pointer
1891 nd->nl_flags |= NLC_LOCKVP;
1892 error = vn_open(nd, fp, flags, cmode);
1896 * handle special fdopen() case. bleh. dupfdopen() is
1897 * responsible for dropping the old contents of ofiles[indx]
1900 * Note that fsetfd() will add a ref to fp which represents
1901 * the fd_files[] assignment. We must still drop our
1904 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1905 if (fdalloc(p, 0, &indx) == 0) {
1906 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1909 fdrop(fp); /* our ref */
1912 fsetfd(fdp, NULL, indx);
1915 fdrop(fp); /* our ref */
1916 if (error == ERESTART)
1922 * ref the vnode for ourselves so it can't be ripped out from under
1923 * is. XXX need an ND flag to request that the vnode be returned
1926 * Reserve a file descriptor but do not assign it until the open
1929 vp = (struct vnode *)fp->f_data;
1931 if ((error = fdalloc(p, 0, &indx)) != 0) {
1938 * If no error occurs the vp will have been assigned to the file
1943 if (flags & (O_EXLOCK | O_SHLOCK)) {
1944 lf.l_whence = SEEK_SET;
1947 if (flags & O_EXLOCK)
1948 lf.l_type = F_WRLCK;
1950 lf.l_type = F_RDLCK;
1951 if (flags & FNONBLOCK)
1956 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1958 * lock request failed. Clean up the reserved
1962 fsetfd(fdp, NULL, indx);
1966 fp->f_flag |= FHASLOCK;
1970 * Assert that all regular file vnodes were created with a object.
1972 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1973 ("open: regular file has no backing object after vn_open"));
1979 * release our private reference, leaving the one associated with the
1980 * descriptor table intact.
1982 fsetfd(fdp, fp, indx);
1985 if (oflags & O_CLOEXEC)
1986 error = fsetfdflags(fdp, *res, UF_EXCLOSE);
1991 * open_args(char *path, int flags, int mode)
1993 * Check permissions, allocate an open file structure,
1994 * and call the device open routine if any.
1997 sys_open(struct open_args *uap)
1999 struct nlookupdata nd;
2002 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2004 error = kern_open(&nd, uap->flags,
2005 uap->mode, &uap->sysmsg_result);
2012 * openat_args(int fd, char *path, int flags, int mode)
2015 sys_openat(struct openat_args *uap)
2017 struct nlookupdata nd;
2021 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2023 error = kern_open(&nd, uap->flags, uap->mode,
2024 &uap->sysmsg_result);
2026 nlookup_done_at(&nd, fp);
2031 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2033 struct thread *td = curthread;
2034 struct proc *p = td->td_proc;
2043 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2044 vattr.va_rmajor = rmajor;
2045 vattr.va_rminor = rminor;
2047 switch (mode & S_IFMT) {
2048 case S_IFMT: /* used by badsect to flag bad sectors */
2049 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2050 vattr.va_type = VBAD;
2053 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2054 vattr.va_type = VCHR;
2057 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2058 vattr.va_type = VBLK;
2061 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2064 case S_IFDIR: /* special directories support for HAMMER */
2065 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2066 vattr.va_type = VDIR;
2077 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2078 if ((error = nlookup(nd)) != 0)
2080 if (nd->nl_nch.ncp->nc_vp)
2082 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2086 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2087 nd->nl_cred, NAMEI_CREATE);
2090 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2091 &vp, nd->nl_cred, &vattr);
2099 * mknod_args(char *path, int mode, int dev)
2101 * Create a special file.
2104 sys_mknod(struct mknod_args *uap)
2106 struct nlookupdata nd;
2109 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2111 error = kern_mknod(&nd, uap->mode,
2112 umajor(uap->dev), uminor(uap->dev));
2119 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2121 * Create a special file. The path is relative to the directory associated
2125 sys_mknodat(struct mknodat_args *uap)
2127 struct nlookupdata nd;
2131 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2133 error = kern_mknod(&nd, uap->mode,
2134 umajor(uap->dev), uminor(uap->dev));
2136 nlookup_done_at(&nd, fp);
2141 kern_mkfifo(struct nlookupdata *nd, int mode)
2143 struct thread *td = curthread;
2144 struct proc *p = td->td_proc;
2151 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2152 if ((error = nlookup(nd)) != 0)
2154 if (nd->nl_nch.ncp->nc_vp)
2156 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2160 vattr.va_type = VFIFO;
2161 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2163 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2170 * mkfifo_args(char *path, int mode)
2172 * Create a named pipe.
2175 sys_mkfifo(struct mkfifo_args *uap)
2177 struct nlookupdata nd;
2180 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2182 error = kern_mkfifo(&nd, uap->mode);
2188 * mkfifoat_args(int fd, char *path, mode_t mode)
2190 * Create a named pipe. The path is relative to the directory associated
2194 sys_mkfifoat(struct mkfifoat_args *uap)
2196 struct nlookupdata nd;
2200 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2202 error = kern_mkfifo(&nd, uap->mode);
2203 nlookup_done_at(&nd, fp);
2207 static int hardlink_check_uid = 0;
2208 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2209 &hardlink_check_uid, 0,
2210 "Unprivileged processes cannot create hard links to files owned by other "
2212 static int hardlink_check_gid = 0;
2213 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2214 &hardlink_check_gid, 0,
2215 "Unprivileged processes cannot create hard links to files owned by other "
2219 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2225 * Shortcut if disabled
2227 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2231 * Privileged user can always hardlink
2233 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2237 * Otherwise only if the originating file is owned by the
2238 * same user or group. Note that any group is allowed if
2239 * the file is owned by the caller.
2241 error = VOP_GETATTR(vp, &va);
2245 if (hardlink_check_uid) {
2246 if (cred->cr_uid != va.va_uid)
2250 if (hardlink_check_gid) {
2251 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2259 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2261 struct thread *td = curthread;
2266 * Lookup the source and obtained a locked vnode.
2268 * You may only hardlink a file which you have write permission
2269 * on or which you own.
2271 * XXX relookup on vget failure / race ?
2274 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2275 if ((error = nlookup(nd)) != 0)
2277 vp = nd->nl_nch.ncp->nc_vp;
2278 KKASSERT(vp != NULL);
2279 if (vp->v_type == VDIR)
2280 return (EPERM); /* POSIX */
2281 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2283 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2287 * Unlock the source so we can lookup the target without deadlocking
2288 * (XXX vp is locked already, possible other deadlock?). The target
2291 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2292 nd->nl_flags &= ~NLC_NCPISLOCKED;
2293 cache_unlock(&nd->nl_nch);
2296 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2297 if ((error = nlookup(linknd)) != 0) {
2301 if (linknd->nl_nch.ncp->nc_vp) {
2305 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2311 * Finally run the new API VOP.
2313 error = can_hardlink(vp, td, td->td_ucred);
2315 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2316 vp, linknd->nl_cred);
2323 * link_args(char *path, char *link)
2325 * Make a hard file link.
2328 sys_link(struct link_args *uap)
2330 struct nlookupdata nd, linknd;
2333 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2335 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2337 error = kern_link(&nd, &linknd);
2338 nlookup_done(&linknd);
2345 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2347 * Make a hard file link. The path1 argument is relative to the directory
2348 * associated with fd1, and similarly the path2 argument is relative to
2349 * the directory associated with fd2.
2352 sys_linkat(struct linkat_args *uap)
2354 struct nlookupdata nd, linknd;
2355 struct file *fp1, *fp2;
2358 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2359 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2361 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2362 uap->path2, UIO_USERSPACE, 0);
2364 error = kern_link(&nd, &linknd);
2365 nlookup_done_at(&linknd, fp2);
2367 nlookup_done_at(&nd, fp1);
2372 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2380 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2381 if ((error = nlookup(nd)) != 0)
2383 if (nd->nl_nch.ncp->nc_vp)
2385 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2389 vattr.va_mode = mode;
2390 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2397 * symlink(char *path, char *link)
2399 * Make a symbolic link.
2402 sys_symlink(struct symlink_args *uap)
2404 struct thread *td = curthread;
2405 struct nlookupdata nd;
2410 path = objcache_get(namei_oc, M_WAITOK);
2411 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2413 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2415 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2416 error = kern_symlink(&nd, path, mode);
2420 objcache_put(namei_oc, path);
2425 * symlinkat_args(char *path1, int fd, char *path2)
2427 * Make a symbolic link. The path2 argument is relative to the directory
2428 * associated with fd.
2431 sys_symlinkat(struct symlinkat_args *uap)
2433 struct thread *td = curthread;
2434 struct nlookupdata nd;
2440 path1 = objcache_get(namei_oc, M_WAITOK);
2441 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2443 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2446 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2447 error = kern_symlink(&nd, path1, mode);
2449 nlookup_done_at(&nd, fp);
2451 objcache_put(namei_oc, path1);
2456 * undelete_args(char *path)
2458 * Delete a whiteout from the filesystem.
2461 sys_undelete(struct undelete_args *uap)
2463 struct nlookupdata nd;
2466 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2468 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2470 error = nlookup(&nd);
2472 error = ncp_writechk(&nd.nl_nch);
2474 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2482 kern_unlink(struct nlookupdata *nd)
2487 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2488 if ((error = nlookup(nd)) != 0)
2490 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2492 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2497 * unlink_args(char *path)
2499 * Delete a name from the filesystem.
2502 sys_unlink(struct unlink_args *uap)
2504 struct nlookupdata nd;
2507 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2509 error = kern_unlink(&nd);
2516 * unlinkat_args(int fd, char *path, int flags)
2518 * Delete the file or directory entry pointed to by fd/path.
2521 sys_unlinkat(struct unlinkat_args *uap)
2523 struct nlookupdata nd;
2527 if (uap->flags & ~AT_REMOVEDIR)
2530 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2532 if (uap->flags & AT_REMOVEDIR)
2533 error = kern_rmdir(&nd);
2535 error = kern_unlink(&nd);
2537 nlookup_done_at(&nd, fp);
2542 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2544 struct thread *td = curthread;
2545 struct proc *p = td->td_proc;
2552 fp = holdfp(p->p_fd, fd, -1);
2555 if (fp->f_type != DTYPE_VNODE) {
2559 vp = (struct vnode *)fp->f_data;
2563 spin_lock(&fp->f_spin);
2564 new_offset = fp->f_offset + offset;
2568 error = VOP_GETATTR(vp, &vattr);
2569 spin_lock(&fp->f_spin);
2570 new_offset = offset + vattr.va_size;
2573 new_offset = offset;
2575 spin_lock(&fp->f_spin);
2580 spin_lock(&fp->f_spin);
2585 * Validate the seek position. Negative offsets are not allowed
2586 * for regular files or directories.
2588 * Normally we would also not want to allow negative offsets for
2589 * character and block-special devices. However kvm addresses
2590 * on 64 bit architectures might appear to be negative and must
2594 if (new_offset < 0 &&
2595 (vp->v_type == VREG || vp->v_type == VDIR)) {
2598 fp->f_offset = new_offset;
2601 *res = fp->f_offset;
2602 spin_unlock(&fp->f_spin);
2609 * lseek_args(int fd, int pad, off_t offset, int whence)
2611 * Reposition read/write file offset.
2614 sys_lseek(struct lseek_args *uap)
2618 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2619 &uap->sysmsg_offset);
2625 * Check if current process can access given file. amode is a bitmask of *_OK
2626 * access bits. flags is a bitmask of AT_* flags.
2629 kern_access(struct nlookupdata *nd, int amode, int flags)
2634 if (flags & ~AT_EACCESS)
2636 if ((error = nlookup(nd)) != 0)
2639 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2643 /* Flags == 0 means only check for existence. */
2652 if ((mode & VWRITE) == 0 ||
2653 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2654 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2657 * If the file handle is stale we have to re-resolve the
2658 * entry. This is a hack at the moment.
2660 if (error == ESTALE) {
2662 cache_setunresolved(&nd->nl_nch);
2663 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2676 * access_args(char *path, int flags)
2678 * Check access permissions.
2681 sys_access(struct access_args *uap)
2683 struct nlookupdata nd;
2686 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2688 error = kern_access(&nd, uap->flags, 0);
2695 * eaccess_args(char *path, int flags)
2697 * Check access permissions.
2700 sys_eaccess(struct eaccess_args *uap)
2702 struct nlookupdata nd;
2705 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2707 error = kern_access(&nd, uap->flags, AT_EACCESS);
2714 * faccessat_args(int fd, char *path, int amode, int flags)
2716 * Check access permissions.
2719 sys_faccessat(struct faccessat_args *uap)
2721 struct nlookupdata nd;
2725 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2728 error = kern_access(&nd, uap->amode, uap->flags);
2729 nlookup_done_at(&nd, fp);
2735 kern_stat(struct nlookupdata *nd, struct stat *st)
2740 if ((error = nlookup(nd)) != 0)
2743 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2746 if ((error = vget(vp, LK_SHARED)) != 0)
2748 error = vn_stat(vp, st, nd->nl_cred);
2751 * If the file handle is stale we have to re-resolve the entry. This
2752 * is a hack at the moment.
2754 if (error == ESTALE) {
2756 cache_setunresolved(&nd->nl_nch);
2757 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2767 * stat_args(char *path, struct stat *ub)
2769 * Get file status; this version follows links.
2772 sys_stat(struct stat_args *uap)
2774 struct nlookupdata nd;
2778 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2780 error = kern_stat(&nd, &st);
2782 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2789 * lstat_args(char *path, struct stat *ub)
2791 * Get file status; this version does not follow links.
2794 sys_lstat(struct lstat_args *uap)
2796 struct nlookupdata nd;
2800 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2802 error = kern_stat(&nd, &st);
2804 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2811 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2813 * Get status of file pointed to by fd/path.
2816 sys_fstatat(struct fstatat_args *uap)
2818 struct nlookupdata nd;
2824 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2827 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2829 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2830 UIO_USERSPACE, flags);
2832 error = kern_stat(&nd, &st);
2834 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2836 nlookup_done_at(&nd, fp);
2841 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
2843 struct nlookupdata nd;
2848 error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
2850 error = nlookup(&nd);
2852 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2855 error = VOP_PATHCONF(vp, name, sysmsg_regp);
2862 * pathconf_Args(char *path, int name)
2864 * Get configurable pathname variables.
2867 sys_pathconf(struct pathconf_args *uap)
2869 return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
2874 * lpathconf_Args(char *path, int name)
2876 * Get configurable pathname variables, but don't follow symlinks.
2879 sys_lpathconf(struct lpathconf_args *uap)
2881 return (kern_pathconf(uap->path, uap->name, 0, &uap->sysmsg_reg));
2886 * kern_readlink isn't properly split yet. There is a copyin burried
2887 * in VOP_READLINK().
2890 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2892 struct thread *td = curthread;
2898 if ((error = nlookup(nd)) != 0)
2900 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2903 if (vp->v_type != VLNK) {
2906 aiov.iov_base = buf;
2907 aiov.iov_len = count;
2908 auio.uio_iov = &aiov;
2909 auio.uio_iovcnt = 1;
2910 auio.uio_offset = 0;
2911 auio.uio_rw = UIO_READ;
2912 auio.uio_segflg = UIO_USERSPACE;
2914 auio.uio_resid = count;
2915 error = VOP_READLINK(vp, &auio, td->td_ucred);
2918 *res = count - auio.uio_resid;
2923 * readlink_args(char *path, char *buf, int count)
2925 * Return target name of a symbolic link.
2928 sys_readlink(struct readlink_args *uap)
2930 struct nlookupdata nd;
2933 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2935 error = kern_readlink(&nd, uap->buf, uap->count,
2936 &uap->sysmsg_result);
2943 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2945 * Return target name of a symbolic link. The path is relative to the
2946 * directory associated with fd.
2949 sys_readlinkat(struct readlinkat_args *uap)
2951 struct nlookupdata nd;
2955 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2957 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2958 &uap->sysmsg_result);
2960 nlookup_done_at(&nd, fp);
2965 setfflags(struct vnode *vp, int flags)
2967 struct thread *td = curthread;
2972 * Prevent non-root users from setting flags on devices. When
2973 * a device is reused, users can retain ownership of the device
2974 * if they are allowed to set flags and programs assume that
2975 * chown can't fail when done as root.
2977 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2978 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2982 * note: vget is required for any operation that might mod the vnode
2983 * so VINACTIVE is properly cleared.
2985 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2987 vattr.va_flags = flags;
2988 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2995 * chflags(char *path, int flags)
2997 * Change flags of a file given a path name.
3000 sys_chflags(struct chflags_args *uap)
3002 struct nlookupdata nd;
3007 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3009 error = nlookup(&nd);
3011 error = ncp_writechk(&nd.nl_nch);
3013 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3016 error = setfflags(vp, uap->flags);
3023 * lchflags(char *path, int flags)
3025 * Change flags of a file given a path name, but don't follow symlinks.
3028 sys_lchflags(struct lchflags_args *uap)
3030 struct nlookupdata nd;
3035 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3037 error = nlookup(&nd);
3039 error = ncp_writechk(&nd.nl_nch);
3041 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3044 error = setfflags(vp, uap->flags);
3051 * fchflags_args(int fd, int flags)
3053 * Change flags of a file given a file descriptor.
3056 sys_fchflags(struct fchflags_args *uap)
3058 struct thread *td = curthread;
3059 struct proc *p = td->td_proc;
3063 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3065 if (fp->f_nchandle.ncp)
3066 error = ncp_writechk(&fp->f_nchandle);
3068 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3074 setfmode(struct vnode *vp, int mode)
3076 struct thread *td = curthread;
3081 * note: vget is required for any operation that might mod the vnode
3082 * so VINACTIVE is properly cleared.
3084 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3086 vattr.va_mode = mode & ALLPERMS;
3087 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3094 kern_chmod(struct nlookupdata *nd, int mode)
3099 if ((error = nlookup(nd)) != 0)
3101 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3103 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3104 error = setfmode(vp, mode);
3110 * chmod_args(char *path, int mode)
3112 * Change mode of a file given path name.
3115 sys_chmod(struct chmod_args *uap)
3117 struct nlookupdata nd;
3120 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3122 error = kern_chmod(&nd, uap->mode);
3128 * lchmod_args(char *path, int mode)
3130 * Change mode of a file given path name (don't follow links.)
3133 sys_lchmod(struct lchmod_args *uap)
3135 struct nlookupdata nd;
3138 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3140 error = kern_chmod(&nd, uap->mode);
3146 * fchmod_args(int fd, int mode)
3148 * Change mode of a file given a file descriptor.
3151 sys_fchmod(struct fchmod_args *uap)
3153 struct thread *td = curthread;
3154 struct proc *p = td->td_proc;
3158 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3160 if (fp->f_nchandle.ncp)
3161 error = ncp_writechk(&fp->f_nchandle);
3163 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3169 * fchmodat_args(char *path, int mode)
3171 * Change mode of a file pointed to by fd/path.
3174 sys_fchmodat(struct fchmodat_args *uap)
3176 struct nlookupdata nd;
3181 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3183 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3185 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3186 UIO_USERSPACE, flags);
3188 error = kern_chmod(&nd, uap->mode);
3189 nlookup_done_at(&nd, fp);
3194 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3196 struct thread *td = curthread;
3204 * note: vget is required for any operation that might mod the vnode
3205 * so VINACTIVE is properly cleared.
3207 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3208 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3210 o_uid = vattr.va_uid;
3211 o_gid = vattr.va_gid;
3212 size = vattr.va_size;
3217 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3226 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3227 VFS_ACCOUNT(mp, uid, gid, size);
3234 kern_chown(struct nlookupdata *nd, int uid, int gid)
3239 if ((error = nlookup(nd)) != 0)
3241 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3243 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3244 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3250 * chown(char *path, int uid, int gid)
3252 * Set ownership given a path name.
3255 sys_chown(struct chown_args *uap)
3257 struct nlookupdata nd;
3260 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3262 error = kern_chown(&nd, uap->uid, uap->gid);
3268 * lchown_args(char *path, int uid, int gid)
3270 * Set ownership given a path name, do not cross symlinks.
3273 sys_lchown(struct lchown_args *uap)
3275 struct nlookupdata nd;
3278 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3280 error = kern_chown(&nd, uap->uid, uap->gid);
3286 * fchown_args(int fd, int uid, int gid)
3288 * Set ownership given a file descriptor.
3291 sys_fchown(struct fchown_args *uap)
3293 struct thread *td = curthread;
3294 struct proc *p = td->td_proc;
3298 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3300 if (fp->f_nchandle.ncp)
3301 error = ncp_writechk(&fp->f_nchandle);
3303 error = setfown(p->p_fd->fd_ncdir.mount,
3304 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3310 * fchownat(int fd, char *path, int uid, int gid, int flags)
3312 * Set ownership of file pointed to by fd/path.
3315 sys_fchownat(struct fchownat_args *uap)
3317 struct nlookupdata nd;
3322 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3324 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3326 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3327 UIO_USERSPACE, flags);
3329 error = kern_chown(&nd, uap->uid, uap->gid);
3330 nlookup_done_at(&nd, fp);
3336 getutimes(const struct timeval *tvp, struct timespec *tsp)
3338 struct timeval tv[2];
3342 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3345 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3346 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3352 setutimes(struct vnode *vp, struct vattr *vattr,
3353 const struct timespec *ts, int nullflag)
3355 struct thread *td = curthread;
3359 vattr->va_atime = ts[0];
3360 vattr->va_mtime = ts[1];
3362 vattr->va_vaflags |= VA_UTIMES_NULL;
3363 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3369 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3371 struct timespec ts[2];
3376 if ((error = getutimes(tptr, ts)) != 0)
3380 * NOTE: utimes() succeeds for the owner even if the file
3381 * is not user-writable.
3383 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3385 if ((error = nlookup(nd)) != 0)
3387 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3389 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3393 * note: vget is required for any operation that might mod the vnode
3394 * so VINACTIVE is properly cleared.
3396 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3397 error = vget(vp, LK_EXCLUSIVE);
3399 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3408 * utimes_args(char *path, struct timeval *tptr)
3410 * Set the access and modification times of a file.
3413 sys_utimes(struct utimes_args *uap)
3415 struct timeval tv[2];
3416 struct nlookupdata nd;
3420 error = copyin(uap->tptr, tv, sizeof(tv));
3424 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3426 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3432 * lutimes_args(char *path, struct timeval *tptr)
3434 * Set the access and modification times of a file.
3437 sys_lutimes(struct lutimes_args *uap)
3439 struct timeval tv[2];
3440 struct nlookupdata nd;
3444 error = copyin(uap->tptr, tv, sizeof(tv));
3448 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3450 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3456 * Set utimes on a file descriptor. The creds used to open the
3457 * file are used to determine whether the operation is allowed
3461 kern_futimes(int fd, struct timeval *tptr)
3463 struct thread *td = curthread;
3464 struct proc *p = td->td_proc;
3465 struct timespec ts[2];
3471 error = getutimes(tptr, ts);
3474 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3476 if (fp->f_nchandle.ncp)
3477 error = ncp_writechk(&fp->f_nchandle);
3480 error = vget(vp, LK_EXCLUSIVE);
3482 error = VOP_GETATTR(vp, &vattr);
3484 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3488 error = setutimes(vp, &vattr, ts,
3499 * futimes_args(int fd, struct timeval *tptr)
3501 * Set the access and modification times of a file.
3504 sys_futimes(struct futimes_args *uap)
3506 struct timeval tv[2];
3510 error = copyin(uap->tptr, tv, sizeof(tv));
3514 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3520 kern_truncate(struct nlookupdata *nd, off_t length)
3527 uint64_t old_size = 0;
3531 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3532 if ((error = nlookup(nd)) != 0)
3534 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3536 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3538 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3542 if (vp->v_type == VDIR) {
3546 if (vfs_quota_enabled) {
3547 error = VOP_GETATTR(vp, &vattr);
3548 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3551 old_size = vattr.va_size;
3554 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3556 vattr.va_size = length;
3557 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3558 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3566 * truncate(char *path, int pad, off_t length)
3568 * Truncate a file given its path name.
3571 sys_truncate(struct truncate_args *uap)
3573 struct nlookupdata nd;
3576 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3578 error = kern_truncate(&nd, uap->length);
3584 kern_ftruncate(int fd, off_t length)
3586 struct thread *td = curthread;
3587 struct proc *p = td->td_proc;
3594 uint64_t old_size = 0;
3599 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3601 if (fp->f_nchandle.ncp) {
3602 error = ncp_writechk(&fp->f_nchandle);
3606 if ((fp->f_flag & FWRITE) == 0) {
3610 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3614 vp = (struct vnode *)fp->f_data;
3615 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3616 if (vp->v_type == VDIR) {
3621 if (vfs_quota_enabled) {
3622 error = VOP_GETATTR(vp, &vattr);
3623 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3626 old_size = vattr.va_size;
3629 if ((error = vn_writechk(vp, NULL)) == 0) {
3631 vattr.va_size = length;
3632 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3634 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3643 * ftruncate_args(int fd, int pad, off_t length)
3645 * Truncate a file given a file descriptor.
3648 sys_ftruncate(struct ftruncate_args *uap)
3652 error = kern_ftruncate(uap->fd, uap->length);
3660 * Sync an open file.
3663 sys_fsync(struct fsync_args *uap)
3665 struct thread *td = curthread;
3666 struct proc *p = td->td_proc;
3672 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3674 vp = (struct vnode *)fp->f_data;
3675 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3676 if ((obj = vp->v_object) != NULL) {
3677 if (vp->v_mount == NULL ||
3678 (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
3679 vm_object_page_clean(obj, 0, 0, 0);
3682 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3683 if (error == 0 && vp->v_mount)
3684 error = buf_fsync(vp);
3692 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3694 struct nchandle fnchd;
3695 struct nchandle tnchd;
3696 struct namecache *ncp;
3703 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3704 if ((error = nlookup(fromnd)) != 0)
3706 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3708 fnchd.mount = fromnd->nl_nch.mount;
3712 * unlock the source nch so we can lookup the target nch without
3713 * deadlocking. The target may or may not exist so we do not check
3714 * for a target vp like kern_mkdir() and other creation functions do.
3716 * The source and target directories are ref'd and rechecked after
3717 * everything is relocked to determine if the source or target file
3720 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3721 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3722 cache_unlock(&fromnd->nl_nch);
3724 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3725 if ((error = nlookup(tond)) != 0) {
3729 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3733 tnchd.mount = tond->nl_nch.mount;
3737 * If the source and target are the same there is nothing to do
3739 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3746 * Mount points cannot be renamed or overwritten
3748 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3757 * Relock the source ncp. cache_relock() will deal with any
3758 * deadlocks against the already-locked tond and will also
3759 * make sure both are resolved.
3761 * NOTE AFTER RELOCKING: The source or target ncp may have become
3762 * invalid while they were unlocked, nc_vp and nc_mount could
3765 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3766 &tond->nl_nch, tond->nl_cred);
3767 fromnd->nl_flags |= NLC_NCPISLOCKED;
3770 * If either fromnd or tond are marked destroyed a ripout occured
3771 * out from under us and we must retry.
3773 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
3774 fromnd->nl_nch.ncp->nc_vp == NULL ||
3775 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
3776 kprintf("kern_rename: retry due to ripout on: "
3777 "\"%s\" -> \"%s\"\n",
3778 fromnd->nl_nch.ncp->nc_name,
3779 tond->nl_nch.ncp->nc_name);
3786 * make sure the parent directories linkages are the same
3788 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3789 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3796 * Both the source and target must be within the same filesystem and
3797 * in the same filesystem as their parent directories within the
3798 * namecache topology.
3800 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3803 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3804 mp != tond->nl_nch.mount) {
3811 * Make sure the mount point is writable
3813 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3820 * If the target exists and either the source or target is a directory,
3821 * then both must be directories.
3823 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3826 if (tond->nl_nch.ncp->nc_vp) {
3827 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3829 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3830 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3832 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3838 * You cannot rename a source into itself or a subdirectory of itself.
3839 * We check this by travsersing the target directory upwards looking
3840 * for a match against the source.
3845 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3846 if (fromnd->nl_nch.ncp == ncp) {
3857 * Even though the namespaces are different, they may still represent
3858 * hardlinks to the same file. The filesystem might have a hard time
3859 * with this so we issue a NREMOVE of the source instead of a NRENAME
3860 * when we detect the situation.
3863 fdvp = fromnd->nl_dvp;
3864 tdvp = tond->nl_dvp;
3865 if (fdvp == NULL || tdvp == NULL) {
3867 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3868 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3871 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3872 fdvp, tdvp, tond->nl_cred);
3879 * rename_args(char *from, char *to)
3881 * Rename files. Source and destination must either both be directories,
3882 * or both not be directories. If target is a directory, it must be empty.
3885 sys_rename(struct rename_args *uap)
3887 struct nlookupdata fromnd, tond;
3891 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3893 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3895 error = kern_rename(&fromnd, &tond);
3896 nlookup_done(&tond);
3898 nlookup_done(&fromnd);
3899 } while (error == EAGAIN);
3904 * renameat_args(int oldfd, char *old, int newfd, char *new)
3906 * Rename files using paths relative to the directories associated with
3907 * oldfd and newfd. Source and destination must either both be directories,
3908 * or both not be directories. If target is a directory, it must be empty.
3911 sys_renameat(struct renameat_args *uap)
3913 struct nlookupdata oldnd, newnd;
3914 struct file *oldfp, *newfp;
3918 error = nlookup_init_at(&oldnd, &oldfp,
3919 uap->oldfd, uap->old,
3922 error = nlookup_init_at(&newnd, &newfp,
3923 uap->newfd, uap->new,
3926 error = kern_rename(&oldnd, &newnd);
3927 nlookup_done_at(&newnd, newfp);
3929 nlookup_done_at(&oldnd, oldfp);
3930 } while (error == EAGAIN);
3935 kern_mkdir(struct nlookupdata *nd, int mode)
3937 struct thread *td = curthread;
3938 struct proc *p = td->td_proc;
3944 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3945 if ((error = nlookup(nd)) != 0)
3948 if (nd->nl_nch.ncp->nc_vp)
3950 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3953 vattr.va_type = VDIR;
3954 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3957 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3964 * mkdir_args(char *path, int mode)
3966 * Make a directory file.
3969 sys_mkdir(struct mkdir_args *uap)
3971 struct nlookupdata nd;
3974 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3976 error = kern_mkdir(&nd, uap->mode);
3982 * mkdirat_args(int fd, char *path, mode_t mode)
3984 * Make a directory file. The path is relative to the directory associated
3988 sys_mkdirat(struct mkdirat_args *uap)
3990 struct nlookupdata nd;
3994 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3996 error = kern_mkdir(&nd, uap->mode);
3997 nlookup_done_at(&nd, fp);
4002 kern_rmdir(struct nlookupdata *nd)
4007 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4008 if ((error = nlookup(nd)) != 0)
4012 * Do not allow directories representing mount points to be
4013 * deleted, even if empty. Check write perms on mount point
4014 * in case the vnode is aliased (aka nullfs).
4016 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4018 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4020 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4025 * rmdir_args(char *path)
4027 * Remove a directory file.
4030 sys_rmdir(struct rmdir_args *uap)
4032 struct nlookupdata nd;
4035 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4037 error = kern_rmdir(&nd);
4043 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4044 enum uio_seg direction)
4046 struct thread *td = curthread;
4047 struct proc *p = td->td_proc;
4055 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4057 if ((fp->f_flag & FREAD) == 0) {
4061 vp = (struct vnode *)fp->f_data;
4063 if (vp->v_type != VDIR) {
4067 aiov.iov_base = buf;
4068 aiov.iov_len = count;
4069 auio.uio_iov = &aiov;
4070 auio.uio_iovcnt = 1;
4071 auio.uio_rw = UIO_READ;
4072 auio.uio_segflg = direction;
4074 auio.uio_resid = count;
4075 loff = auio.uio_offset = fp->f_offset;
4076 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4077 fp->f_offset = auio.uio_offset;
4080 if (count == auio.uio_resid) {
4081 if (union_dircheckp) {
4082 error = union_dircheckp(td, &vp, fp);
4089 if ((vp->v_flag & VROOT) &&
4090 (vp->v_mount->mnt_flag & MNT_UNION)) {
4091 struct vnode *tvp = vp;
4092 vp = vp->v_mount->mnt_vnodecovered;
4103 * WARNING! *basep may not be wide enough to accomodate the
4104 * seek offset. XXX should we hack this to return the upper 32 bits
4105 * for offsets greater then 4G?
4108 *basep = (long)loff;
4110 *res = count - auio.uio_resid;
4117 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4119 * Read a block of directory entries in a file system independent format.
4122 sys_getdirentries(struct getdirentries_args *uap)
4127 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4128 &uap->sysmsg_result, UIO_USERSPACE);
4130 if (error == 0 && uap->basep)
4131 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4136 * getdents_args(int fd, char *buf, size_t count)
4139 sys_getdents(struct getdents_args *uap)
4143 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4144 &uap->sysmsg_result, UIO_USERSPACE);
4150 * Set the mode mask for creation of filesystem nodes.
4152 * umask(int newmask)
4155 sys_umask(struct umask_args *uap)
4157 struct thread *td = curthread;
4158 struct proc *p = td->td_proc;
4159 struct filedesc *fdp;
4162 uap->sysmsg_result = fdp->fd_cmask;
4163 fdp->fd_cmask = uap->newmask & ALLPERMS;
4168 * revoke(char *path)
4170 * Void all references to file by ripping underlying filesystem
4174 sys_revoke(struct revoke_args *uap)
4176 struct nlookupdata nd;
4183 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4185 error = nlookup(&nd);
4187 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4188 cred = crhold(nd.nl_cred);
4192 error = VOP_GETATTR(vp, &vattr);
4193 if (error == 0 && cred->cr_uid != vattr.va_uid)
4194 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4195 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4197 error = vrevoke(vp, cred);
4198 } else if (error == 0) {
4199 error = vrevoke(vp, cred);
4209 * getfh_args(char *fname, fhandle_t *fhp)
4211 * Get (NFS) file handle
4213 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4214 * mount. This allows nullfs mounts to be explicitly exported.
4216 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4218 * nullfs mounts of subdirectories are not safe. That is, it will
4219 * work, but you do not really have protection against access to
4220 * the related parent directories.
4223 sys_getfh(struct getfh_args *uap)
4225 struct thread *td = curthread;
4226 struct nlookupdata nd;
4233 * Must be super user
4235 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4239 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4241 error = nlookup(&nd);
4243 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4244 mp = nd.nl_nch.mount;
4247 bzero(&fh, sizeof(fh));
4248 fh.fh_fsid = mp->mnt_stat.f_fsid;
4249 error = VFS_VPTOFH(vp, &fh.fh_fid);
4252 error = copyout(&fh, uap->fhp, sizeof(fh));
4258 * fhopen_args(const struct fhandle *u_fhp, int flags)
4260 * syscall for the rpc.lockd to use to translate a NFS file handle into
4261 * an open descriptor.
4263 * warning: do not remove the priv_check() call or this becomes one giant
4267 sys_fhopen(struct fhopen_args *uap)
4269 struct thread *td = curthread;
4270 struct filedesc *fdp = td->td_proc->p_fd;
4275 struct vattr *vap = &vat;
4277 int fmode, mode, error = 0, type;
4283 * Must be super user
4285 error = priv_check(td, PRIV_ROOT);
4289 fmode = FFLAGS(uap->flags);
4292 * Why not allow a non-read/write open for our lockd?
4294 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4296 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4301 * Find the mount point
4303 mp = vfs_getvfs(&fhp.fh_fsid);
4308 /* now give me my vnode, it gets returned to me locked */
4309 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4313 * from now on we have to make sure not
4314 * to forget about the vnode
4315 * any error that causes an abort must vput(vp)
4316 * just set error = err and 'goto bad;'.
4322 if (vp->v_type == VLNK) {
4326 if (vp->v_type == VSOCK) {
4331 if (fmode & (FWRITE | O_TRUNC)) {
4332 if (vp->v_type == VDIR) {
4336 error = vn_writechk(vp, NULL);
4344 error = VOP_ACCESS(vp, mode, td->td_ucred);
4348 if (fmode & O_TRUNC) {
4349 vn_unlock(vp); /* XXX */
4350 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4353 error = VOP_SETATTR(vp, vap, td->td_ucred);
4359 * VOP_OPEN needs the file pointer so it can potentially override
4362 * WARNING! no f_nchandle will be associated when fhopen()ing a
4365 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4369 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4372 * setting f_ops this way prevents VOP_CLOSE from being
4373 * called or fdrop() releasing the vp from v_data. Since
4374 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4376 fp->f_ops = &badfileops;
4382 * The fp is given its own reference, we still have our ref and lock.
4384 * Assert that all regular files must be created with a VM object.
4386 if (vp->v_type == VREG && vp->v_object == NULL) {
4387 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4392 * The open was successful. Handle any locking requirements.
4394 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4395 lf.l_whence = SEEK_SET;
4398 if (fmode & O_EXLOCK)
4399 lf.l_type = F_WRLCK;
4401 lf.l_type = F_RDLCK;
4402 if (fmode & FNONBLOCK)
4407 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4409 * release our private reference.
4411 fsetfd(fdp, NULL, indx);
4416 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4417 fp->f_flag |= FHASLOCK;
4421 * Clean up. Associate the file pointer with the previously
4422 * reserved descriptor and return it.
4425 fsetfd(fdp, fp, indx);
4427 uap->sysmsg_result = indx;
4428 if (uap->flags & O_CLOEXEC)
4429 error = fsetfdflags(fdp, indx, UF_EXCLOSE);
4433 fsetfd(fdp, NULL, indx);
4442 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4445 sys_fhstat(struct fhstat_args *uap)
4447 struct thread *td = curthread;
4455 * Must be super user
4457 error = priv_check(td, PRIV_ROOT);
4461 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4465 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4468 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4469 error = vn_stat(vp, &sb, td->td_ucred);
4474 error = copyout(&sb, uap->sb, sizeof(sb));
4479 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4482 sys_fhstatfs(struct fhstatfs_args *uap)
4484 struct thread *td = curthread;
4485 struct proc *p = td->td_proc;
4490 char *fullpath, *freepath;
4495 * Must be super user
4497 if ((error = priv_check(td, PRIV_ROOT)))
4500 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4503 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4507 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4512 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4517 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4520 error = mount_path(p, mp, &fullpath, &freepath);
4523 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4524 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4525 kfree(freepath, M_TEMP);
4527 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4528 if (priv_check(td, PRIV_ROOT)) {
4529 bcopy(sp, &sb, sizeof(sb));
4530 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4533 error = copyout(sp, uap->buf, sizeof(*sp));
4539 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4542 sys_fhstatvfs(struct fhstatvfs_args *uap)
4544 struct thread *td = curthread;
4545 struct proc *p = td->td_proc;
4553 * Must be super user
4555 if ((error = priv_check(td, PRIV_ROOT)))
4558 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4561 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4565 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4570 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4573 sp = &mp->mnt_vstat;
4575 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4579 if (mp->mnt_flag & MNT_RDONLY)
4580 sp->f_flag |= ST_RDONLY;
4581 if (mp->mnt_flag & MNT_NOSUID)
4582 sp->f_flag |= ST_NOSUID;
4583 error = copyout(sp, uap->buf, sizeof(*sp));
4590 * Syscall to push extended attribute configuration information into the
4591 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4592 * a command (int cmd), and attribute name and misc data. For now, the
4593 * attribute name is left in userspace for consumption by the VFS_op.
4594 * It will probably be changed to be copied into sysspace by the
4595 * syscall in the future, once issues with various consumers of the
4596 * attribute code have raised their hands.
4598 * Currently this is used only by UFS Extended Attributes.
4601 sys_extattrctl(struct extattrctl_args *uap)
4603 struct nlookupdata nd;
4605 char attrname[EXTATTR_MAXNAMELEN];
4613 if (error == 0 && uap->filename) {
4614 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4617 error = nlookup(&nd);
4619 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4623 if (error == 0 && uap->attrname) {
4624 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4629 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4631 error = nlookup(&nd);
4633 error = ncp_writechk(&nd.nl_nch);
4635 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4637 uap->attrname, nd.nl_cred);
4646 * Syscall to get a named extended attribute on a file or directory.
4649 sys_extattr_set_file(struct extattr_set_file_args *uap)
4651 char attrname[EXTATTR_MAXNAMELEN];
4652 struct nlookupdata nd;
4658 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4664 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4666 error = nlookup(&nd);
4668 error = ncp_writechk(&nd.nl_nch);
4670 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4676 bzero(&auio, sizeof(auio));
4677 aiov.iov_base = uap->data;
4678 aiov.iov_len = uap->nbytes;
4679 auio.uio_iov = &aiov;
4680 auio.uio_iovcnt = 1;
4681 auio.uio_offset = 0;
4682 auio.uio_resid = uap->nbytes;
4683 auio.uio_rw = UIO_WRITE;
4684 auio.uio_td = curthread;
4686 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4695 * Syscall to get a named extended attribute on a file or directory.
4698 sys_extattr_get_file(struct extattr_get_file_args *uap)
4700 char attrname[EXTATTR_MAXNAMELEN];
4701 struct nlookupdata nd;
4707 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4713 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4715 error = nlookup(&nd);
4717 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4723 bzero(&auio, sizeof(auio));
4724 aiov.iov_base = uap->data;
4725 aiov.iov_len = uap->nbytes;
4726 auio.uio_iov = &aiov;
4727 auio.uio_iovcnt = 1;
4728 auio.uio_offset = 0;
4729 auio.uio_resid = uap->nbytes;
4730 auio.uio_rw = UIO_READ;
4731 auio.uio_td = curthread;
4733 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4735 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4743 * Syscall to delete a named extended attribute from a file or directory.
4744 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4747 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4749 char attrname[EXTATTR_MAXNAMELEN];
4750 struct nlookupdata nd;
4754 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4758 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4760 error = nlookup(&nd);
4762 error = ncp_writechk(&nd.nl_nch);
4764 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4766 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4767 attrname, NULL, nd.nl_cred);
4776 * Determine if the mount is visible to the process.
4779 chroot_visible_mnt(struct mount *mp, struct proc *p)
4781 struct nchandle nch;
4784 * Traverse from the mount point upwards. If we hit the process
4785 * root then the mount point is visible to the process.
4787 nch = mp->mnt_ncmountpt;
4789 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4790 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4793 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4794 nch = nch.mount->mnt_ncmounton;
4796 nch.ncp = nch.ncp->nc_parent;
4801 * If the mount point is not visible to the process, but the
4802 * process root is in a subdirectory of the mount, return
4805 if (p->p_fd->fd_nrdir.mount == mp)