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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
46 #include <sys/sysent.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/mountctl.h>
50 #include <sys/sysproto.h>
51 #include <sys/filedesc.h>
52 #include <sys/kernel.h>
53 #include <sys/fcntl.h>
55 #include <sys/linker.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
62 #include <sys/namei.h>
63 #include <sys/nlookup.h>
64 #include <sys/dirent.h>
65 #include <sys/extattr.h>
66 #include <sys/spinlock.h>
67 #include <sys/kern_syscall.h>
68 #include <sys/objcache.h>
69 #include <sys/sysctl.h>
72 #include <sys/file2.h>
73 #include <sys/spinlock2.h>
74 #include <sys/mplock2.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
80 #include <machine/limits.h>
81 #include <machine/stdarg.h>
83 #include <vfs/union/union.h>
85 static void mount_warning(struct mount *mp, const char *ctl, ...)
87 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
88 static int checkvp_chdir (struct vnode *vn, struct thread *td);
89 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
90 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
91 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
92 static int getutimes (const struct timeval *, struct timespec *);
93 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
94 static int setfmode (struct vnode *, int);
95 static int setfflags (struct vnode *, int);
96 static int setutimes (struct vnode *, struct vattr *,
97 const struct timespec *, int);
98 static int usermount = 0; /* if 1, non-root can mount fs. */
100 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
102 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
103 "Allow non-root users to mount filesystems");
106 * Virtual File System System Calls
110 * Mount a file system.
112 * mount_args(char *type, char *path, int flags, caddr_t data)
117 sys_mount(struct mount_args *uap)
119 struct thread *td = curthread;
122 struct mount *mp, *nullmp;
123 struct vfsconf *vfsp;
124 int error, flag = 0, flag2 = 0;
127 struct nlookupdata nd;
128 char fstypename[MFSNAMELEN];
136 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
140 * Do not allow NFS export by non-root users.
142 if (uap->flags & MNT_EXPORTED) {
143 error = priv_check(td, PRIV_ROOT);
148 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
150 if (priv_check(td, PRIV_ROOT))
151 uap->flags |= MNT_NOSUID | MNT_NODEV;
154 * Lookup the requested path and extract the nch and vnode.
156 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
158 if ((error = nlookup(&nd)) == 0) {
159 if (nd.nl_nch.ncp->nc_vp == NULL)
169 * If the target filesystem is resolved via a nullfs mount, then
170 * nd.nl_nch.mount will be pointing to the nullfs mount structure
171 * instead of the target file system. We need it in case we are
174 nullmp = nd.nl_nch.mount;
177 * Extract the locked+refd ncp and cleanup the nd structure
180 cache_zero(&nd.nl_nch);
183 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
184 (mp = cache_findmount(&nch)) != NULL) {
193 * now we have the locked ref'd nch and unreferenced vnode.
196 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
203 * Extract the file system type. We need to know this early, to take
204 * appropriate actions if we are dealing with a nullfs.
206 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
213 * Now we have an unlocked ref'd nch and a locked ref'd vp
215 if (uap->flags & MNT_UPDATE) {
216 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
223 if (strncmp(fstypename, "null", 5) == 0) {
231 flag2 = mp->mnt_kern_flag;
233 * We only allow the filesystem to be reloaded if it
234 * is currently mounted read-only.
236 if ((uap->flags & MNT_RELOAD) &&
237 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
240 error = EOPNOTSUPP; /* Needs translation */
244 * Only root, or the user that did the original mount is
245 * permitted to update it.
247 if (mp->mnt_stat.f_owner != cred->cr_uid &&
248 (error = priv_check(td, PRIV_ROOT))) {
253 if (vfs_busy(mp, LK_NOWAIT)) {
267 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
268 lwkt_gettoken(&mp->mnt_token);
274 * If the user is not root, ensure that they own the directory
275 * onto which we are attempting to mount.
277 if ((error = VOP_GETATTR(vp, &va)) ||
278 (va.va_uid != cred->cr_uid &&
279 (error = priv_check(td, PRIV_ROOT)))) {
284 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
289 if (vp->v_type != VDIR) {
295 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
301 vfsp = vfsconf_find_by_name(fstypename);
305 /* Only load modules for root (very important!) */
306 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
311 error = linker_load_file(fstypename, &lf);
312 if (error || lf == NULL) {
320 /* lookup again, see if the VFS was loaded */
321 vfsp = vfsconf_find_by_name(fstypename);
324 linker_file_unload(lf);
339 * Allocate and initialize the filesystem.
341 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
343 vfs_busy(mp, LK_NOWAIT);
344 mp->mnt_op = vfsp->vfc_vfsops;
346 vfsp->vfc_refcount++;
347 mp->mnt_stat.f_type = vfsp->vfc_typenum;
348 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
349 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
350 mp->mnt_stat.f_owner = cred->cr_uid;
351 lwkt_gettoken(&mp->mnt_token);
355 * (per-mount token acquired at this point)
357 * Set the mount level flags.
359 if (uap->flags & MNT_RDONLY)
360 mp->mnt_flag |= MNT_RDONLY;
361 else if (mp->mnt_flag & MNT_RDONLY)
362 mp->mnt_kern_flag |= MNTK_WANTRDWR;
363 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
364 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
365 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
366 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
367 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
368 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
369 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
370 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
372 * Mount the filesystem.
373 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
376 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
377 if (mp->mnt_flag & MNT_UPDATE) {
378 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
379 mp->mnt_flag &= ~MNT_RDONLY;
380 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
381 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
384 mp->mnt_kern_flag = flag2;
386 lwkt_reltoken(&mp->mnt_token);
392 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
395 * Put the new filesystem on the mount list after root. The mount
396 * point gets its own mnt_ncmountpt (unless the VFS already set one
397 * up) which represents the root of the mount. The lookup code
398 * detects the mount point going forward and checks the root of
399 * the mount going backwards.
401 * It is not necessary to invalidate or purge the vnode underneath
402 * because elements under the mount will be given their own glue
406 if (mp->mnt_ncmountpt.ncp == NULL) {
408 * allocate, then unlock, but leave the ref intact
410 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
411 cache_unlock(&mp->mnt_ncmountpt);
413 mp->mnt_ncmounton = nch; /* inherits ref */
414 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
415 cache_ismounting(mp);
417 mountlist_insert(mp, MNTINS_LAST);
419 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
420 error = vfs_allocate_syncvnode(mp);
421 lwkt_reltoken(&mp->mnt_token);
423 error = VFS_START(mp, 0);
426 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
427 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
428 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
429 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
430 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
431 mp->mnt_vfc->vfc_refcount--;
432 lwkt_reltoken(&mp->mnt_token);
443 * Scan all active processes to see if any of them have a current
444 * or root directory onto which the new filesystem has just been
445 * mounted. If so, replace them with the new mount point.
447 * Both old_nch and new_nch are ref'd on call but not locked.
448 * new_nch must be temporarily locked so it can be associated with the
449 * vnode representing the root of the mount point.
451 struct checkdirs_info {
452 struct nchandle old_nch;
453 struct nchandle new_nch;
454 struct vnode *old_vp;
455 struct vnode *new_vp;
458 static int checkdirs_callback(struct proc *p, void *data);
461 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
463 struct checkdirs_info info;
469 * If the old mount point's vnode has a usecount of 1, it is not
470 * being held as a descriptor anywhere.
472 olddp = old_nch->ncp->nc_vp;
473 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
477 * Force the root vnode of the new mount point to be resolved
478 * so we can update any matching processes.
481 if (VFS_ROOT(mp, &newdp))
482 panic("mount: lost mount");
484 cache_setunresolved(new_nch);
485 cache_setvp(new_nch, newdp);
486 cache_unlock(new_nch);
489 * Special handling of the root node
491 if (rootvnode == olddp) {
493 vfs_cache_setroot(newdp, cache_hold(new_nch));
497 * Pass newdp separately so the callback does not have to access
498 * it via new_nch->ncp->nc_vp.
500 info.old_nch = *old_nch;
501 info.new_nch = *new_nch;
503 allproc_scan(checkdirs_callback, &info);
508 * NOTE: callback is not MP safe because the scanned process's filedesc
509 * structure can be ripped out from under us, amoung other things.
512 checkdirs_callback(struct proc *p, void *data)
514 struct checkdirs_info *info = data;
515 struct filedesc *fdp;
516 struct nchandle ncdrop1;
517 struct nchandle ncdrop2;
518 struct vnode *vprele1;
519 struct vnode *vprele2;
521 if ((fdp = p->p_fd) != NULL) {
522 cache_zero(&ncdrop1);
523 cache_zero(&ncdrop2);
528 * MPUNSAFE - XXX fdp can be pulled out from under a
531 * A shared filedesc is ok, we don't have to copy it
532 * because we are making this change globally.
534 spin_lock(&fdp->fd_spin);
535 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
536 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
537 vprele1 = fdp->fd_cdir;
539 fdp->fd_cdir = info->new_vp;
540 ncdrop1 = fdp->fd_ncdir;
541 cache_copy(&info->new_nch, &fdp->fd_ncdir);
543 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
544 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
545 vprele2 = fdp->fd_rdir;
547 fdp->fd_rdir = info->new_vp;
548 ncdrop2 = fdp->fd_nrdir;
549 cache_copy(&info->new_nch, &fdp->fd_nrdir);
551 spin_unlock(&fdp->fd_spin);
553 cache_drop(&ncdrop1);
555 cache_drop(&ncdrop2);
565 * Unmount a file system.
567 * Note: unmount takes a path to the vnode mounted on as argument,
568 * not special file (as before).
570 * umount_args(char *path, int flags)
575 sys_unmount(struct unmount_args *uap)
577 struct thread *td = curthread;
578 struct proc *p __debugvar = td->td_proc;
579 struct mount *mp = NULL;
580 struct nlookupdata nd;
585 if (td->td_ucred->cr_prison != NULL) {
589 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
592 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
594 error = nlookup(&nd);
598 mp = nd.nl_nch.mount;
601 * Only root, or the user that did the original mount is
602 * permitted to unmount this filesystem.
604 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
605 (error = priv_check(td, PRIV_ROOT)))
609 * Don't allow unmounting the root file system.
611 if (mp->mnt_flag & MNT_ROOTFS) {
617 * Must be the root of the filesystem
619 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
627 error = dounmount(mp, uap->flags);
634 * Do the actual file system unmount.
637 dounmount_interlock(struct mount *mp)
639 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
641 mp->mnt_kern_flag |= MNTK_UNMOUNT;
646 unmount_allproc_cb(struct proc *p, void *arg)
650 if (p->p_textnch.ncp == NULL)
653 mp = (struct mount *)arg;
654 if (p->p_textnch.mount == mp)
655 cache_drop(&p->p_textnch);
661 dounmount(struct mount *mp, int flags)
663 struct namecache *ncp;
672 lwkt_gettoken(&mp->mnt_token);
674 * Exclusive access for unmounting purposes
676 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
680 * Allow filesystems to detect that a forced unmount is in progress.
682 if (flags & MNT_FORCE)
683 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
684 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
685 error = lockmgr(&mp->mnt_lock, lflags);
687 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
688 if (mp->mnt_kern_flag & MNTK_MWAIT) {
689 mp->mnt_kern_flag &= ~MNTK_MWAIT;
695 if (mp->mnt_flag & MNT_EXPUBLIC)
696 vfs_setpublicfs(NULL, NULL, NULL);
698 vfs_msync(mp, MNT_WAIT);
699 async_flag = mp->mnt_flag & MNT_ASYNC;
700 mp->mnt_flag &=~ MNT_ASYNC;
703 * If this filesystem isn't aliasing other filesystems,
704 * try to invalidate any remaining namecache entries and
705 * check the count afterwords.
707 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
708 cache_lock(&mp->mnt_ncmountpt);
709 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
710 cache_unlock(&mp->mnt_ncmountpt);
712 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
713 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
714 allproc_scan(&unmount_allproc_cb, mp);
717 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
718 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
720 if ((flags & MNT_FORCE) == 0) {
722 mount_warning(mp, "Cannot unmount: "
728 mount_warning(mp, "Forced unmount: "
739 * Decomission our special mnt_syncer vnode. This also stops
740 * the vnlru code. If we are unable to unmount we recommission
743 * Then sync the filesystem.
745 if ((vp = mp->mnt_syncer) != NULL) {
746 mp->mnt_syncer = NULL;
749 if ((mp->mnt_flag & MNT_RDONLY) == 0)
750 VFS_SYNC(mp, MNT_WAIT);
753 * nchandle records ref the mount structure. Expect a count of 1
754 * (our mount->mnt_ncmountpt).
756 * Scans can get temporary refs on a mountpoint (thought really
757 * heavy duty stuff like cache_findmount() do not).
759 for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
760 cache_unmounting(mp);
761 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
763 if (mp->mnt_refs != 1) {
764 if ((flags & MNT_FORCE) == 0) {
765 mount_warning(mp, "Cannot unmount: "
766 "%d mount refs still present",
770 mount_warning(mp, "Forced unmount: "
771 "%d mount refs still present",
778 * So far so good, sync the filesystem once more and
779 * call the VFS unmount code if the sync succeeds.
782 if (((mp->mnt_flag & MNT_RDONLY) ||
783 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
784 (flags & MNT_FORCE)) {
785 error = VFS_UNMOUNT(mp, flags);
790 * If an error occurred we can still recover, restoring the
791 * syncer vnode and misc flags.
794 if (mp->mnt_syncer == NULL)
795 vfs_allocate_syncvnode(mp);
796 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
797 mp->mnt_flag |= async_flag;
798 lockmgr(&mp->mnt_lock, LK_RELEASE);
799 if (mp->mnt_kern_flag & MNTK_MWAIT) {
800 mp->mnt_kern_flag &= ~MNTK_MWAIT;
806 * Clean up any journals still associated with the mount after
807 * filesystem activity has ceased.
809 journal_remove_all_journals(mp,
810 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
812 mountlist_remove(mp);
815 * Remove any installed vnode ops here so the individual VFSs don't
818 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
819 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
820 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
821 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
822 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
824 if (mp->mnt_ncmountpt.ncp != NULL) {
825 nch = mp->mnt_ncmountpt;
826 cache_zero(&mp->mnt_ncmountpt);
827 cache_clrmountpt(&nch);
830 if (mp->mnt_ncmounton.ncp != NULL) {
831 cache_unmounting(mp);
832 nch = mp->mnt_ncmounton;
833 cache_zero(&mp->mnt_ncmounton);
834 cache_clrmountpt(&nch);
838 mp->mnt_vfc->vfc_refcount--;
839 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
840 panic("unmount: dangling vnode");
841 lockmgr(&mp->mnt_lock, LK_RELEASE);
842 if (mp->mnt_kern_flag & MNTK_MWAIT) {
843 mp->mnt_kern_flag &= ~MNTK_MWAIT;
848 * If we reach here and freeok != 0 we must free the mount.
849 * If refs > 1 cycle and wait, just in case someone tried
850 * to busy the mount after we decided to do the unmount.
853 while (mp->mnt_refs > 1) {
854 cache_unmounting(mp);
856 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
858 lwkt_reltoken(&mp->mnt_token);
865 lwkt_reltoken(&mp->mnt_token);
871 mount_warning(struct mount *mp, const char *ctl, ...)
878 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
879 &ptr, &buf, 0) == 0) {
880 kprintf("unmount(%s): ", ptr);
885 kprintf("unmount(%p", mp);
886 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
887 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
896 * Shim cache_fullpath() to handle the case where a process is chrooted into
897 * a subdirectory of a mount. In this case if the root mount matches the
898 * process root directory's mount we have to specify the process's root
899 * directory instead of the mount point, because the mount point might
900 * be above the root directory.
904 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
906 struct nchandle *nch;
908 if (p && p->p_fd->fd_nrdir.mount == mp)
909 nch = &p->p_fd->fd_nrdir;
911 nch = &mp->mnt_ncmountpt;
912 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
916 * Sync each mounted filesystem.
920 static int syncprt = 0;
921 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
924 static int sync_callback(struct mount *mp, void *data);
927 sys_sync(struct sync_args *uap)
929 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
932 * print out buffer pool stat information on each sync() call.
942 sync_callback(struct mount *mp, void *data __unused)
946 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
947 asyncflag = mp->mnt_flag & MNT_ASYNC;
948 mp->mnt_flag &= ~MNT_ASYNC;
949 vfs_msync(mp, MNT_NOWAIT);
950 VFS_SYNC(mp, MNT_NOWAIT);
951 mp->mnt_flag |= asyncflag;
956 /* XXX PRISON: could be per prison flag */
957 static int prison_quotas;
959 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
963 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
965 * Change filesystem quotas.
970 sys_quotactl(struct quotactl_args *uap)
972 struct nlookupdata nd;
979 if (td->td_ucred->cr_prison && !prison_quotas) {
984 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
986 error = nlookup(&nd);
988 mp = nd.nl_nch.mount;
989 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
990 uap->arg, nd.nl_cred);
999 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1000 * void *buf, int buflen)
1002 * This function operates on a mount point and executes the specified
1003 * operation using the specified control data, and possibly returns data.
1005 * The actual number of bytes stored in the result buffer is returned, 0
1006 * if none, otherwise an error is returned.
1011 sys_mountctl(struct mountctl_args *uap)
1013 struct thread *td = curthread;
1014 struct proc *p = td->td_proc;
1022 * Sanity and permissions checks. We must be root.
1025 if (td->td_ucred->cr_prison != NULL)
1027 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1028 (error = priv_check(td, PRIV_ROOT)) != 0)
1032 * Argument length checks
1034 if (uap->ctllen < 0 || uap->ctllen > 1024)
1036 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1038 if (uap->path == NULL)
1042 * Allocate the necessary buffers and copyin data
1044 path = objcache_get(namei_oc, M_WAITOK);
1045 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1050 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1051 error = copyin(uap->ctl, ctl, uap->ctllen);
1056 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1059 * Validate the descriptor
1062 fp = holdfp(p->p_fd, uap->fd, -1);
1072 * Execute the internal kernel function and clean up.
1075 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1079 if (error == 0 && uap->sysmsg_result > 0)
1080 error = copyout(buf, uap->buf, uap->sysmsg_result);
1083 objcache_put(namei_oc, path);
1092 * Execute a mount control operation by resolving the path to a mount point
1093 * and calling vop_mountctl().
1095 * Use the mount point from the nch instead of the vnode so nullfs mounts
1096 * can properly spike the VOP.
1099 kern_mountctl(const char *path, int op, struct file *fp,
1100 const void *ctl, int ctllen,
1101 void *buf, int buflen, int *res)
1105 struct nlookupdata nd;
1110 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1112 error = nlookup(&nd);
1114 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1115 mp = nd.nl_nch.mount;
1122 * Must be the root of the filesystem
1124 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1128 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1135 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1137 struct thread *td = curthread;
1138 struct proc *p = td->td_proc;
1141 char *fullpath, *freepath;
1144 if ((error = nlookup(nd)) != 0)
1146 mp = nd->nl_nch.mount;
1148 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1151 error = mount_path(p, mp, &fullpath, &freepath);
1154 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1155 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1156 kfree(freepath, M_TEMP);
1158 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1159 bcopy(sp, buf, sizeof(*buf));
1160 /* Only root should have access to the fsid's. */
1161 if (priv_check(td, PRIV_ROOT))
1162 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1167 * statfs_args(char *path, struct statfs *buf)
1169 * Get filesystem statistics.
1172 sys_statfs(struct statfs_args *uap)
1174 struct nlookupdata nd;
1178 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1180 error = kern_statfs(&nd, &buf);
1183 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1188 kern_fstatfs(int fd, struct statfs *buf)
1190 struct thread *td = curthread;
1191 struct proc *p = td->td_proc;
1195 char *fullpath, *freepath;
1199 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1203 * Try to use mount info from any overlays rather than the
1204 * mount info for the underlying vnode, otherwise we will
1205 * fail when operating on null-mounted paths inside a chroot.
1207 if ((mp = fp->f_nchandle.mount) == NULL)
1208 mp = ((struct vnode *)fp->f_data)->v_mount;
1213 if (fp->f_cred == NULL) {
1218 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1221 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1223 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1224 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1225 kfree(freepath, M_TEMP);
1227 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1228 bcopy(sp, buf, sizeof(*buf));
1230 /* Only root should have access to the fsid's. */
1231 if (priv_check(td, PRIV_ROOT))
1232 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1240 * fstatfs_args(int fd, struct statfs *buf)
1242 * Get filesystem statistics.
1245 sys_fstatfs(struct fstatfs_args *uap)
1250 error = kern_fstatfs(uap->fd, &buf);
1253 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1258 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1264 if ((error = nlookup(nd)) != 0)
1266 mp = nd->nl_nch.mount;
1267 sp = &mp->mnt_vstat;
1268 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1272 if (mp->mnt_flag & MNT_RDONLY)
1273 sp->f_flag |= ST_RDONLY;
1274 if (mp->mnt_flag & MNT_NOSUID)
1275 sp->f_flag |= ST_NOSUID;
1276 bcopy(sp, buf, sizeof(*buf));
1281 * statfs_args(char *path, struct statfs *buf)
1283 * Get filesystem statistics.
1286 sys_statvfs(struct statvfs_args *uap)
1288 struct nlookupdata nd;
1292 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1294 error = kern_statvfs(&nd, &buf);
1297 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1302 kern_fstatvfs(int fd, struct statvfs *buf)
1304 struct thread *td = curthread;
1305 struct proc *p = td->td_proc;
1312 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1314 if ((mp = fp->f_nchandle.mount) == NULL)
1315 mp = ((struct vnode *)fp->f_data)->v_mount;
1320 if (fp->f_cred == NULL) {
1324 sp = &mp->mnt_vstat;
1325 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1329 if (mp->mnt_flag & MNT_RDONLY)
1330 sp->f_flag |= ST_RDONLY;
1331 if (mp->mnt_flag & MNT_NOSUID)
1332 sp->f_flag |= ST_NOSUID;
1334 bcopy(sp, buf, sizeof(*buf));
1342 * fstatfs_args(int fd, struct statfs *buf)
1344 * Get filesystem statistics.
1347 sys_fstatvfs(struct fstatvfs_args *uap)
1352 error = kern_fstatvfs(uap->fd, &buf);
1355 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1360 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1362 * Get statistics on all filesystems.
1365 struct getfsstat_info {
1366 struct statfs *sfsp;
1374 static int getfsstat_callback(struct mount *, void *);
1377 sys_getfsstat(struct getfsstat_args *uap)
1379 struct thread *td = curthread;
1380 struct getfsstat_info info;
1382 bzero(&info, sizeof(info));
1384 info.maxcount = uap->bufsize / sizeof(struct statfs);
1385 info.sfsp = uap->buf;
1387 info.flags = uap->flags;
1390 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1391 if (info.sfsp && info.count > info.maxcount)
1392 uap->sysmsg_result = info.maxcount;
1394 uap->sysmsg_result = info.count;
1395 return (info.error);
1399 getfsstat_callback(struct mount *mp, void *data)
1401 struct getfsstat_info *info = data;
1407 if (info->sfsp && info->count < info->maxcount) {
1408 if (info->td->td_proc &&
1409 !chroot_visible_mnt(mp, info->td->td_proc)) {
1415 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1416 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1417 * overrides MNT_WAIT.
1419 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1420 (info->flags & MNT_WAIT)) &&
1421 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1424 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1426 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1428 info->error = error;
1431 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1432 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1433 kfree(freepath, M_TEMP);
1435 error = copyout(sp, info->sfsp, sizeof(*sp));
1437 info->error = error;
1447 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1448 long bufsize, int flags)
1450 * Get statistics on all filesystems.
1453 struct getvfsstat_info {
1454 struct statfs *sfsp;
1455 struct statvfs *vsfsp;
1463 static int getvfsstat_callback(struct mount *, void *);
1466 sys_getvfsstat(struct getvfsstat_args *uap)
1468 struct thread *td = curthread;
1469 struct getvfsstat_info info;
1471 bzero(&info, sizeof(info));
1473 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1474 info.sfsp = uap->buf;
1475 info.vsfsp = uap->vbuf;
1477 info.flags = uap->flags;
1480 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1481 if (info.vsfsp && info.count > info.maxcount)
1482 uap->sysmsg_result = info.maxcount;
1484 uap->sysmsg_result = info.count;
1485 return (info.error);
1489 getvfsstat_callback(struct mount *mp, void *data)
1491 struct getvfsstat_info *info = data;
1493 struct statvfs *vsp;
1498 if (info->vsfsp && info->count < info->maxcount) {
1499 if (info->td->td_proc &&
1500 !chroot_visible_mnt(mp, info->td->td_proc)) {
1504 vsp = &mp->mnt_vstat;
1507 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1508 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1509 * overrides MNT_WAIT.
1511 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1512 (info->flags & MNT_WAIT)) &&
1513 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1516 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1518 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1519 (info->flags & MNT_WAIT)) &&
1520 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1524 if (mp->mnt_flag & MNT_RDONLY)
1525 vsp->f_flag |= ST_RDONLY;
1526 if (mp->mnt_flag & MNT_NOSUID)
1527 vsp->f_flag |= ST_NOSUID;
1529 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1531 info->error = error;
1534 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1535 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1536 kfree(freepath, M_TEMP);
1538 error = copyout(sp, info->sfsp, sizeof(*sp));
1540 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1542 info->error = error;
1554 * fchdir_args(int fd)
1556 * Change current working directory to a given file descriptor.
1559 sys_fchdir(struct fchdir_args *uap)
1561 struct thread *td = curthread;
1562 struct proc *p = td->td_proc;
1563 struct filedesc *fdp = p->p_fd;
1564 struct vnode *vp, *ovp;
1567 struct nchandle nch, onch, tnch;
1570 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1572 lwkt_gettoken(&p->p_token);
1573 vp = (struct vnode *)fp->f_data;
1575 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1576 if (fp->f_nchandle.ncp == NULL)
1579 error = checkvp_chdir(vp, td);
1584 cache_copy(&fp->f_nchandle, &nch);
1587 * If the ncp has become a mount point, traverse through
1591 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1592 (mp = cache_findmount(&nch)) != NULL
1594 error = nlookup_mp(mp, &tnch);
1596 cache_unlock(&tnch); /* leave ref intact */
1598 vp = tnch.ncp->nc_vp;
1599 error = vget(vp, LK_SHARED);
1600 KKASSERT(error == 0);
1604 cache_dropmount(mp);
1608 onch = fdp->fd_ncdir;
1609 vn_unlock(vp); /* leave ref intact */
1611 fdp->fd_ncdir = nch;
1620 lwkt_reltoken(&p->p_token);
1625 kern_chdir(struct nlookupdata *nd)
1627 struct thread *td = curthread;
1628 struct proc *p = td->td_proc;
1629 struct filedesc *fdp = p->p_fd;
1630 struct vnode *vp, *ovp;
1631 struct nchandle onch;
1634 if ((error = nlookup(nd)) != 0)
1636 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1638 if ((error = vget(vp, LK_SHARED)) != 0)
1641 lwkt_gettoken(&p->p_token);
1642 error = checkvp_chdir(vp, td);
1646 onch = fdp->fd_ncdir;
1647 cache_unlock(&nd->nl_nch); /* leave reference intact */
1648 fdp->fd_ncdir = nd->nl_nch;
1652 cache_zero(&nd->nl_nch);
1656 lwkt_reltoken(&p->p_token);
1661 * chdir_args(char *path)
1663 * Change current working directory (``.'').
1666 sys_chdir(struct chdir_args *uap)
1668 struct nlookupdata nd;
1671 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1673 error = kern_chdir(&nd);
1679 * Helper function for raised chroot(2) security function: Refuse if
1680 * any filedescriptors are open directories.
1683 chroot_refuse_vdir_fds(struct filedesc *fdp)
1690 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1691 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1693 vp = (struct vnode *)fp->f_data;
1694 if (vp->v_type != VDIR) {
1705 * This sysctl determines if we will allow a process to chroot(2) if it
1706 * has a directory open:
1707 * 0: disallowed for all processes.
1708 * 1: allowed for processes that were not already chroot(2)'ed.
1709 * 2: allowed for all processes.
1712 static int chroot_allow_open_directories = 1;
1714 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1715 &chroot_allow_open_directories, 0, "");
1718 * chroot to the specified namecache entry. We obtain the vp from the
1719 * namecache data. The passed ncp must be locked and referenced and will
1720 * remain locked and referenced on return.
1723 kern_chroot(struct nchandle *nch)
1725 struct thread *td = curthread;
1726 struct proc *p = td->td_proc;
1727 struct filedesc *fdp = p->p_fd;
1732 * Only privileged user can chroot
1734 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1739 * Disallow open directory descriptors (fchdir() breakouts).
1741 if (chroot_allow_open_directories == 0 ||
1742 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1743 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1746 if ((vp = nch->ncp->nc_vp) == NULL)
1749 if ((error = vget(vp, LK_SHARED)) != 0)
1753 * Check the validity of vp as a directory to change to and
1754 * associate it with rdir/jdir.
1756 error = checkvp_chdir(vp, td);
1757 vn_unlock(vp); /* leave reference intact */
1759 vrele(fdp->fd_rdir);
1760 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1761 cache_drop(&fdp->fd_nrdir);
1762 cache_copy(nch, &fdp->fd_nrdir);
1763 if (fdp->fd_jdir == NULL) {
1766 cache_copy(nch, &fdp->fd_njdir);
1775 * chroot_args(char *path)
1777 * Change notion of root (``/'') directory.
1780 sys_chroot(struct chroot_args *uap)
1782 struct thread *td __debugvar = curthread;
1783 struct nlookupdata nd;
1786 KKASSERT(td->td_proc);
1787 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1789 nd.nl_flags |= NLC_EXEC;
1790 error = nlookup(&nd);
1792 error = kern_chroot(&nd.nl_nch);
1799 sys_chroot_kernel(struct chroot_kernel_args *uap)
1801 struct thread *td = curthread;
1802 struct nlookupdata nd;
1803 struct nchandle *nch;
1807 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1811 error = nlookup(&nd);
1817 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1821 if ((vp = nch->ncp->nc_vp) == NULL) {
1826 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1829 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1831 vfs_cache_setroot(vp, cache_hold(nch));
1841 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1842 * determine whether it is legal to chdir to the vnode. The vnode's state
1843 * is not changed by this call.
1846 checkvp_chdir(struct vnode *vp, struct thread *td)
1850 if (vp->v_type != VDIR)
1853 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1858 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1860 struct thread *td = curthread;
1861 struct proc *p = td->td_proc;
1862 struct lwp *lp = td->td_lwp;
1863 struct filedesc *fdp = p->p_fd;
1868 int type, indx, error = 0;
1871 if ((oflags & O_ACCMODE) == O_ACCMODE)
1873 flags = FFLAGS(oflags);
1874 error = falloc(lp, &nfp, NULL);
1878 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1881 * XXX p_dupfd is a real mess. It allows a device to return a
1882 * file descriptor to be duplicated rather then doing the open
1888 * Call vn_open() to do the lookup and assign the vnode to the
1889 * file pointer. vn_open() does not change the ref count on fp
1890 * and the vnode, on success, will be inherited by the file pointer
1893 nd->nl_flags |= NLC_LOCKVP;
1894 error = vn_open(nd, fp, flags, cmode);
1898 * handle special fdopen() case. bleh. dupfdopen() is
1899 * responsible for dropping the old contents of ofiles[indx]
1902 * Note that fsetfd() will add a ref to fp which represents
1903 * the fd_files[] assignment. We must still drop our
1906 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1907 if (fdalloc(p, 0, &indx) == 0) {
1908 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1911 fdrop(fp); /* our ref */
1914 fsetfd(fdp, NULL, indx);
1917 fdrop(fp); /* our ref */
1918 if (error == ERESTART)
1924 * ref the vnode for ourselves so it can't be ripped out from under
1925 * is. XXX need an ND flag to request that the vnode be returned
1928 * Reserve a file descriptor but do not assign it until the open
1931 vp = (struct vnode *)fp->f_data;
1933 if ((error = fdalloc(p, 0, &indx)) != 0) {
1940 * If no error occurs the vp will have been assigned to the file
1945 if (flags & (O_EXLOCK | O_SHLOCK)) {
1946 lf.l_whence = SEEK_SET;
1949 if (flags & O_EXLOCK)
1950 lf.l_type = F_WRLCK;
1952 lf.l_type = F_RDLCK;
1953 if (flags & FNONBLOCK)
1958 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1960 * lock request failed. Clean up the reserved
1964 fsetfd(fdp, NULL, indx);
1968 fp->f_flag |= FHASLOCK;
1972 * Assert that all regular file vnodes were created with a object.
1974 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1975 ("open: regular file has no backing object after vn_open"));
1981 * release our private reference, leaving the one associated with the
1982 * descriptor table intact.
1984 fsetfd(fdp, fp, indx);
1987 if (oflags & O_CLOEXEC)
1988 error = fsetfdflags(fdp, *res, UF_EXCLOSE);
1993 * open_args(char *path, int flags, int mode)
1995 * Check permissions, allocate an open file structure,
1996 * and call the device open routine if any.
1999 sys_open(struct open_args *uap)
2001 struct nlookupdata nd;
2004 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2006 error = kern_open(&nd, uap->flags,
2007 uap->mode, &uap->sysmsg_result);
2014 * openat_args(int fd, char *path, int flags, int mode)
2017 sys_openat(struct openat_args *uap)
2019 struct nlookupdata nd;
2023 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2025 error = kern_open(&nd, uap->flags, uap->mode,
2026 &uap->sysmsg_result);
2028 nlookup_done_at(&nd, fp);
2033 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2035 struct thread *td = curthread;
2036 struct proc *p = td->td_proc;
2045 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2046 vattr.va_rmajor = rmajor;
2047 vattr.va_rminor = rminor;
2049 switch (mode & S_IFMT) {
2050 case S_IFMT: /* used by badsect to flag bad sectors */
2051 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2052 vattr.va_type = VBAD;
2055 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2056 vattr.va_type = VCHR;
2059 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2060 vattr.va_type = VBLK;
2063 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2066 case S_IFDIR: /* special directories support for HAMMER */
2067 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2068 vattr.va_type = VDIR;
2079 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2080 if ((error = nlookup(nd)) != 0)
2082 if (nd->nl_nch.ncp->nc_vp)
2084 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2088 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2089 nd->nl_cred, NAMEI_CREATE);
2092 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2093 &vp, nd->nl_cred, &vattr);
2101 * mknod_args(char *path, int mode, int dev)
2103 * Create a special file.
2106 sys_mknod(struct mknod_args *uap)
2108 struct nlookupdata nd;
2111 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2113 error = kern_mknod(&nd, uap->mode,
2114 umajor(uap->dev), uminor(uap->dev));
2121 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2123 * Create a special file. The path is relative to the directory associated
2127 sys_mknodat(struct mknodat_args *uap)
2129 struct nlookupdata nd;
2133 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2135 error = kern_mknod(&nd, uap->mode,
2136 umajor(uap->dev), uminor(uap->dev));
2138 nlookup_done_at(&nd, fp);
2143 kern_mkfifo(struct nlookupdata *nd, int mode)
2145 struct thread *td = curthread;
2146 struct proc *p = td->td_proc;
2153 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2154 if ((error = nlookup(nd)) != 0)
2156 if (nd->nl_nch.ncp->nc_vp)
2158 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2162 vattr.va_type = VFIFO;
2163 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2165 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2172 * mkfifo_args(char *path, int mode)
2174 * Create a named pipe.
2177 sys_mkfifo(struct mkfifo_args *uap)
2179 struct nlookupdata nd;
2182 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2184 error = kern_mkfifo(&nd, uap->mode);
2190 * mkfifoat_args(int fd, char *path, mode_t mode)
2192 * Create a named pipe. The path is relative to the directory associated
2196 sys_mkfifoat(struct mkfifoat_args *uap)
2198 struct nlookupdata nd;
2202 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2204 error = kern_mkfifo(&nd, uap->mode);
2205 nlookup_done_at(&nd, fp);
2209 static int hardlink_check_uid = 0;
2210 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2211 &hardlink_check_uid, 0,
2212 "Unprivileged processes cannot create hard links to files owned by other "
2214 static int hardlink_check_gid = 0;
2215 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2216 &hardlink_check_gid, 0,
2217 "Unprivileged processes cannot create hard links to files owned by other "
2221 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2227 * Shortcut if disabled
2229 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2233 * Privileged user can always hardlink
2235 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2239 * Otherwise only if the originating file is owned by the
2240 * same user or group. Note that any group is allowed if
2241 * the file is owned by the caller.
2243 error = VOP_GETATTR(vp, &va);
2247 if (hardlink_check_uid) {
2248 if (cred->cr_uid != va.va_uid)
2252 if (hardlink_check_gid) {
2253 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2261 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2263 struct thread *td = curthread;
2268 * Lookup the source and obtained a locked vnode.
2270 * You may only hardlink a file which you have write permission
2271 * on or which you own.
2273 * XXX relookup on vget failure / race ?
2276 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2277 if ((error = nlookup(nd)) != 0)
2279 vp = nd->nl_nch.ncp->nc_vp;
2280 KKASSERT(vp != NULL);
2281 if (vp->v_type == VDIR)
2282 return (EPERM); /* POSIX */
2283 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2285 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2289 * Unlock the source so we can lookup the target without deadlocking
2290 * (XXX vp is locked already, possible other deadlock?). The target
2293 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2294 nd->nl_flags &= ~NLC_NCPISLOCKED;
2295 cache_unlock(&nd->nl_nch);
2298 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2299 if ((error = nlookup(linknd)) != 0) {
2303 if (linknd->nl_nch.ncp->nc_vp) {
2307 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2313 * Finally run the new API VOP.
2315 error = can_hardlink(vp, td, td->td_ucred);
2317 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2318 vp, linknd->nl_cred);
2325 * link_args(char *path, char *link)
2327 * Make a hard file link.
2330 sys_link(struct link_args *uap)
2332 struct nlookupdata nd, linknd;
2335 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2337 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2339 error = kern_link(&nd, &linknd);
2340 nlookup_done(&linknd);
2347 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2349 * Make a hard file link. The path1 argument is relative to the directory
2350 * associated with fd1, and similarly the path2 argument is relative to
2351 * the directory associated with fd2.
2354 sys_linkat(struct linkat_args *uap)
2356 struct nlookupdata nd, linknd;
2357 struct file *fp1, *fp2;
2360 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2361 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2363 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2364 uap->path2, UIO_USERSPACE, 0);
2366 error = kern_link(&nd, &linknd);
2367 nlookup_done_at(&linknd, fp2);
2369 nlookup_done_at(&nd, fp1);
2374 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2382 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2383 if ((error = nlookup(nd)) != 0)
2385 if (nd->nl_nch.ncp->nc_vp)
2387 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2391 vattr.va_mode = mode;
2392 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2399 * symlink(char *path, char *link)
2401 * Make a symbolic link.
2404 sys_symlink(struct symlink_args *uap)
2406 struct thread *td = curthread;
2407 struct nlookupdata nd;
2412 path = objcache_get(namei_oc, M_WAITOK);
2413 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2415 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2417 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2418 error = kern_symlink(&nd, path, mode);
2422 objcache_put(namei_oc, path);
2427 * symlinkat_args(char *path1, int fd, char *path2)
2429 * Make a symbolic link. The path2 argument is relative to the directory
2430 * associated with fd.
2433 sys_symlinkat(struct symlinkat_args *uap)
2435 struct thread *td = curthread;
2436 struct nlookupdata nd;
2442 path1 = objcache_get(namei_oc, M_WAITOK);
2443 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2445 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2448 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2449 error = kern_symlink(&nd, path1, mode);
2451 nlookup_done_at(&nd, fp);
2453 objcache_put(namei_oc, path1);
2458 * undelete_args(char *path)
2460 * Delete a whiteout from the filesystem.
2463 sys_undelete(struct undelete_args *uap)
2465 struct nlookupdata nd;
2468 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2470 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2472 error = nlookup(&nd);
2474 error = ncp_writechk(&nd.nl_nch);
2476 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2484 kern_unlink(struct nlookupdata *nd)
2489 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2490 if ((error = nlookup(nd)) != 0)
2492 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2494 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2499 * unlink_args(char *path)
2501 * Delete a name from the filesystem.
2504 sys_unlink(struct unlink_args *uap)
2506 struct nlookupdata nd;
2509 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2511 error = kern_unlink(&nd);
2518 * unlinkat_args(int fd, char *path, int flags)
2520 * Delete the file or directory entry pointed to by fd/path.
2523 sys_unlinkat(struct unlinkat_args *uap)
2525 struct nlookupdata nd;
2529 if (uap->flags & ~AT_REMOVEDIR)
2532 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2534 if (uap->flags & AT_REMOVEDIR)
2535 error = kern_rmdir(&nd);
2537 error = kern_unlink(&nd);
2539 nlookup_done_at(&nd, fp);
2544 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2546 struct thread *td = curthread;
2547 struct proc *p = td->td_proc;
2554 fp = holdfp(p->p_fd, fd, -1);
2557 if (fp->f_type != DTYPE_VNODE) {
2561 vp = (struct vnode *)fp->f_data;
2565 spin_lock(&fp->f_spin);
2566 new_offset = fp->f_offset + offset;
2570 error = VOP_GETATTR(vp, &vattr);
2571 spin_lock(&fp->f_spin);
2572 new_offset = offset + vattr.va_size;
2575 new_offset = offset;
2577 spin_lock(&fp->f_spin);
2582 spin_lock(&fp->f_spin);
2587 * Validate the seek position. Negative offsets are not allowed
2588 * for regular files or directories.
2590 * Normally we would also not want to allow negative offsets for
2591 * character and block-special devices. However kvm addresses
2592 * on 64 bit architectures might appear to be negative and must
2596 if (new_offset < 0 &&
2597 (vp->v_type == VREG || vp->v_type == VDIR)) {
2600 fp->f_offset = new_offset;
2603 *res = fp->f_offset;
2604 spin_unlock(&fp->f_spin);
2611 * lseek_args(int fd, int pad, off_t offset, int whence)
2613 * Reposition read/write file offset.
2616 sys_lseek(struct lseek_args *uap)
2620 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2621 &uap->sysmsg_offset);
2627 * Check if current process can access given file. amode is a bitmask of *_OK
2628 * access bits. flags is a bitmask of AT_* flags.
2631 kern_access(struct nlookupdata *nd, int amode, int flags)
2636 if (flags & ~AT_EACCESS)
2638 if ((error = nlookup(nd)) != 0)
2641 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2645 /* Flags == 0 means only check for existence. */
2654 if ((mode & VWRITE) == 0 ||
2655 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2656 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2659 * If the file handle is stale we have to re-resolve the
2660 * entry. This is a hack at the moment.
2662 if (error == ESTALE) {
2664 cache_setunresolved(&nd->nl_nch);
2665 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2678 * access_args(char *path, int flags)
2680 * Check access permissions.
2683 sys_access(struct access_args *uap)
2685 struct nlookupdata nd;
2688 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2690 error = kern_access(&nd, uap->flags, 0);
2697 * eaccess_args(char *path, int flags)
2699 * Check access permissions.
2702 sys_eaccess(struct eaccess_args *uap)
2704 struct nlookupdata nd;
2707 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2709 error = kern_access(&nd, uap->flags, AT_EACCESS);
2716 * faccessat_args(int fd, char *path, int amode, int flags)
2718 * Check access permissions.
2721 sys_faccessat(struct faccessat_args *uap)
2723 struct nlookupdata nd;
2727 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2730 error = kern_access(&nd, uap->amode, uap->flags);
2731 nlookup_done_at(&nd, fp);
2737 kern_stat(struct nlookupdata *nd, struct stat *st)
2742 if ((error = nlookup(nd)) != 0)
2745 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2748 if ((error = vget(vp, LK_SHARED)) != 0)
2750 error = vn_stat(vp, st, nd->nl_cred);
2753 * If the file handle is stale we have to re-resolve the entry. This
2754 * is a hack at the moment.
2756 if (error == ESTALE) {
2758 cache_setunresolved(&nd->nl_nch);
2759 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2769 * stat_args(char *path, struct stat *ub)
2771 * Get file status; this version follows links.
2774 sys_stat(struct stat_args *uap)
2776 struct nlookupdata nd;
2780 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2782 error = kern_stat(&nd, &st);
2784 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2791 * lstat_args(char *path, struct stat *ub)
2793 * Get file status; this version does not follow links.
2796 sys_lstat(struct lstat_args *uap)
2798 struct nlookupdata nd;
2802 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2804 error = kern_stat(&nd, &st);
2806 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2813 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2815 * Get status of file pointed to by fd/path.
2818 sys_fstatat(struct fstatat_args *uap)
2820 struct nlookupdata nd;
2826 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2829 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2831 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2832 UIO_USERSPACE, flags);
2834 error = kern_stat(&nd, &st);
2836 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2838 nlookup_done_at(&nd, fp);
2843 * pathconf_Args(char *path, int name)
2845 * Get configurable pathname variables.
2848 sys_pathconf(struct pathconf_args *uap)
2850 struct nlookupdata nd;
2855 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2857 error = nlookup(&nd);
2859 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2862 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2870 * kern_readlink isn't properly split yet. There is a copyin burried
2871 * in VOP_READLINK().
2874 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2876 struct thread *td = curthread;
2882 if ((error = nlookup(nd)) != 0)
2884 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2887 if (vp->v_type != VLNK) {
2890 aiov.iov_base = buf;
2891 aiov.iov_len = count;
2892 auio.uio_iov = &aiov;
2893 auio.uio_iovcnt = 1;
2894 auio.uio_offset = 0;
2895 auio.uio_rw = UIO_READ;
2896 auio.uio_segflg = UIO_USERSPACE;
2898 auio.uio_resid = count;
2899 error = VOP_READLINK(vp, &auio, td->td_ucred);
2902 *res = count - auio.uio_resid;
2907 * readlink_args(char *path, char *buf, int count)
2909 * Return target name of a symbolic link.
2912 sys_readlink(struct readlink_args *uap)
2914 struct nlookupdata nd;
2917 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2919 error = kern_readlink(&nd, uap->buf, uap->count,
2920 &uap->sysmsg_result);
2927 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2929 * Return target name of a symbolic link. The path is relative to the
2930 * directory associated with fd.
2933 sys_readlinkat(struct readlinkat_args *uap)
2935 struct nlookupdata nd;
2939 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2941 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2942 &uap->sysmsg_result);
2944 nlookup_done_at(&nd, fp);
2949 setfflags(struct vnode *vp, int flags)
2951 struct thread *td = curthread;
2956 * Prevent non-root users from setting flags on devices. When
2957 * a device is reused, users can retain ownership of the device
2958 * if they are allowed to set flags and programs assume that
2959 * chown can't fail when done as root.
2961 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2962 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2966 * note: vget is required for any operation that might mod the vnode
2967 * so VINACTIVE is properly cleared.
2969 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2971 vattr.va_flags = flags;
2972 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2979 * chflags(char *path, int flags)
2981 * Change flags of a file given a path name.
2984 sys_chflags(struct chflags_args *uap)
2986 struct nlookupdata nd;
2991 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2993 error = nlookup(&nd);
2995 error = ncp_writechk(&nd.nl_nch);
2997 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3000 error = setfflags(vp, uap->flags);
3007 * lchflags(char *path, int flags)
3009 * Change flags of a file given a path name, but don't follow symlinks.
3012 sys_lchflags(struct lchflags_args *uap)
3014 struct nlookupdata nd;
3019 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3021 error = nlookup(&nd);
3023 error = ncp_writechk(&nd.nl_nch);
3025 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3028 error = setfflags(vp, uap->flags);
3035 * fchflags_args(int fd, int flags)
3037 * Change flags of a file given a file descriptor.
3040 sys_fchflags(struct fchflags_args *uap)
3042 struct thread *td = curthread;
3043 struct proc *p = td->td_proc;
3047 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3049 if (fp->f_nchandle.ncp)
3050 error = ncp_writechk(&fp->f_nchandle);
3052 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3058 setfmode(struct vnode *vp, int mode)
3060 struct thread *td = curthread;
3065 * note: vget is required for any operation that might mod the vnode
3066 * so VINACTIVE is properly cleared.
3068 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3070 vattr.va_mode = mode & ALLPERMS;
3071 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3078 kern_chmod(struct nlookupdata *nd, int mode)
3083 if ((error = nlookup(nd)) != 0)
3085 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3087 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3088 error = setfmode(vp, mode);
3094 * chmod_args(char *path, int mode)
3096 * Change mode of a file given path name.
3099 sys_chmod(struct chmod_args *uap)
3101 struct nlookupdata nd;
3104 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3106 error = kern_chmod(&nd, uap->mode);
3112 * lchmod_args(char *path, int mode)
3114 * Change mode of a file given path name (don't follow links.)
3117 sys_lchmod(struct lchmod_args *uap)
3119 struct nlookupdata nd;
3122 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3124 error = kern_chmod(&nd, uap->mode);
3130 * fchmod_args(int fd, int mode)
3132 * Change mode of a file given a file descriptor.
3135 sys_fchmod(struct fchmod_args *uap)
3137 struct thread *td = curthread;
3138 struct proc *p = td->td_proc;
3142 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3144 if (fp->f_nchandle.ncp)
3145 error = ncp_writechk(&fp->f_nchandle);
3147 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3153 * fchmodat_args(char *path, int mode)
3155 * Change mode of a file pointed to by fd/path.
3158 sys_fchmodat(struct fchmodat_args *uap)
3160 struct nlookupdata nd;
3165 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3167 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3169 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3170 UIO_USERSPACE, flags);
3172 error = kern_chmod(&nd, uap->mode);
3173 nlookup_done_at(&nd, fp);
3178 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3180 struct thread *td = curthread;
3188 * note: vget is required for any operation that might mod the vnode
3189 * so VINACTIVE is properly cleared.
3191 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3192 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3194 o_uid = vattr.va_uid;
3195 o_gid = vattr.va_gid;
3196 size = vattr.va_size;
3201 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3210 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3211 VFS_ACCOUNT(mp, uid, gid, size);
3218 kern_chown(struct nlookupdata *nd, int uid, int gid)
3223 if ((error = nlookup(nd)) != 0)
3225 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3227 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3228 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3234 * chown(char *path, int uid, int gid)
3236 * Set ownership given a path name.
3239 sys_chown(struct chown_args *uap)
3241 struct nlookupdata nd;
3244 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3246 error = kern_chown(&nd, uap->uid, uap->gid);
3252 * lchown_args(char *path, int uid, int gid)
3254 * Set ownership given a path name, do not cross symlinks.
3257 sys_lchown(struct lchown_args *uap)
3259 struct nlookupdata nd;
3262 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3264 error = kern_chown(&nd, uap->uid, uap->gid);
3270 * fchown_args(int fd, int uid, int gid)
3272 * Set ownership given a file descriptor.
3275 sys_fchown(struct fchown_args *uap)
3277 struct thread *td = curthread;
3278 struct proc *p = td->td_proc;
3282 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3284 if (fp->f_nchandle.ncp)
3285 error = ncp_writechk(&fp->f_nchandle);
3287 error = setfown(p->p_fd->fd_ncdir.mount,
3288 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3294 * fchownat(int fd, char *path, int uid, int gid, int flags)
3296 * Set ownership of file pointed to by fd/path.
3299 sys_fchownat(struct fchownat_args *uap)
3301 struct nlookupdata nd;
3306 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3308 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3310 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3311 UIO_USERSPACE, flags);
3313 error = kern_chown(&nd, uap->uid, uap->gid);
3314 nlookup_done_at(&nd, fp);
3320 getutimes(const struct timeval *tvp, struct timespec *tsp)
3322 struct timeval tv[2];
3326 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3329 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3330 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3336 setutimes(struct vnode *vp, struct vattr *vattr,
3337 const struct timespec *ts, int nullflag)
3339 struct thread *td = curthread;
3343 vattr->va_atime = ts[0];
3344 vattr->va_mtime = ts[1];
3346 vattr->va_vaflags |= VA_UTIMES_NULL;
3347 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3353 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3355 struct timespec ts[2];
3360 if ((error = getutimes(tptr, ts)) != 0)
3364 * NOTE: utimes() succeeds for the owner even if the file
3365 * is not user-writable.
3367 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3369 if ((error = nlookup(nd)) != 0)
3371 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3373 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3377 * note: vget is required for any operation that might mod the vnode
3378 * so VINACTIVE is properly cleared.
3380 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3381 error = vget(vp, LK_EXCLUSIVE);
3383 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3392 * utimes_args(char *path, struct timeval *tptr)
3394 * Set the access and modification times of a file.
3397 sys_utimes(struct utimes_args *uap)
3399 struct timeval tv[2];
3400 struct nlookupdata nd;
3404 error = copyin(uap->tptr, tv, sizeof(tv));
3408 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3410 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3416 * lutimes_args(char *path, struct timeval *tptr)
3418 * Set the access and modification times of a file.
3421 sys_lutimes(struct lutimes_args *uap)
3423 struct timeval tv[2];
3424 struct nlookupdata nd;
3428 error = copyin(uap->tptr, tv, sizeof(tv));
3432 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3434 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3440 * Set utimes on a file descriptor. The creds used to open the
3441 * file are used to determine whether the operation is allowed
3445 kern_futimes(int fd, struct timeval *tptr)
3447 struct thread *td = curthread;
3448 struct proc *p = td->td_proc;
3449 struct timespec ts[2];
3455 error = getutimes(tptr, ts);
3458 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3460 if (fp->f_nchandle.ncp)
3461 error = ncp_writechk(&fp->f_nchandle);
3464 error = vget(vp, LK_EXCLUSIVE);
3466 error = VOP_GETATTR(vp, &vattr);
3468 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3472 error = setutimes(vp, &vattr, ts,
3483 * futimes_args(int fd, struct timeval *tptr)
3485 * Set the access and modification times of a file.
3488 sys_futimes(struct futimes_args *uap)
3490 struct timeval tv[2];
3494 error = copyin(uap->tptr, tv, sizeof(tv));
3498 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3504 kern_truncate(struct nlookupdata *nd, off_t length)
3511 uint64_t old_size = 0;
3515 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3516 if ((error = nlookup(nd)) != 0)
3518 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3520 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3522 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3526 if (vp->v_type == VDIR) {
3530 if (vfs_quota_enabled) {
3531 error = VOP_GETATTR(vp, &vattr);
3532 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3535 old_size = vattr.va_size;
3538 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3540 vattr.va_size = length;
3541 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3542 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3550 * truncate(char *path, int pad, off_t length)
3552 * Truncate a file given its path name.
3555 sys_truncate(struct truncate_args *uap)
3557 struct nlookupdata nd;
3560 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3562 error = kern_truncate(&nd, uap->length);
3568 kern_ftruncate(int fd, off_t length)
3570 struct thread *td = curthread;
3571 struct proc *p = td->td_proc;
3578 uint64_t old_size = 0;
3583 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3585 if (fp->f_nchandle.ncp) {
3586 error = ncp_writechk(&fp->f_nchandle);
3590 if ((fp->f_flag & FWRITE) == 0) {
3594 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3598 vp = (struct vnode *)fp->f_data;
3599 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3600 if (vp->v_type == VDIR) {
3605 if (vfs_quota_enabled) {
3606 error = VOP_GETATTR(vp, &vattr);
3607 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3610 old_size = vattr.va_size;
3613 if ((error = vn_writechk(vp, NULL)) == 0) {
3615 vattr.va_size = length;
3616 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3618 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3627 * ftruncate_args(int fd, int pad, off_t length)
3629 * Truncate a file given a file descriptor.
3632 sys_ftruncate(struct ftruncate_args *uap)
3636 error = kern_ftruncate(uap->fd, uap->length);
3644 * Sync an open file.
3647 sys_fsync(struct fsync_args *uap)
3649 struct thread *td = curthread;
3650 struct proc *p = td->td_proc;
3656 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3658 vp = (struct vnode *)fp->f_data;
3659 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3660 if ((obj = vp->v_object) != NULL)
3661 vm_object_page_clean(obj, 0, 0, 0);
3662 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3663 if (error == 0 && vp->v_mount)
3664 error = buf_fsync(vp);
3672 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3674 struct nchandle fnchd;
3675 struct nchandle tnchd;
3676 struct namecache *ncp;
3683 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3684 if ((error = nlookup(fromnd)) != 0)
3686 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3688 fnchd.mount = fromnd->nl_nch.mount;
3692 * unlock the source nch so we can lookup the target nch without
3693 * deadlocking. The target may or may not exist so we do not check
3694 * for a target vp like kern_mkdir() and other creation functions do.
3696 * The source and target directories are ref'd and rechecked after
3697 * everything is relocked to determine if the source or target file
3700 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3701 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3702 cache_unlock(&fromnd->nl_nch);
3704 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3705 if ((error = nlookup(tond)) != 0) {
3709 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3713 tnchd.mount = tond->nl_nch.mount;
3717 * If the source and target are the same there is nothing to do
3719 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3726 * Mount points cannot be renamed or overwritten
3728 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3737 * Relock the source ncp. cache_relock() will deal with any
3738 * deadlocks against the already-locked tond and will also
3739 * make sure both are resolved.
3741 * NOTE AFTER RELOCKING: The source or target ncp may have become
3742 * invalid while they were unlocked, nc_vp and nc_mount could
3745 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3746 &tond->nl_nch, tond->nl_cred);
3747 fromnd->nl_flags |= NLC_NCPISLOCKED;
3750 * If either fromnd or tond are marked destroyed a ripout occured
3751 * out from under us and we must retry.
3753 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
3754 fromnd->nl_nch.ncp->nc_vp == NULL ||
3755 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
3756 kprintf("kern_rename: retry due to ripout on: "
3757 "\"%s\" -> \"%s\"\n",
3758 fromnd->nl_nch.ncp->nc_name,
3759 tond->nl_nch.ncp->nc_name);
3766 * make sure the parent directories linkages are the same
3768 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3769 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3776 * Both the source and target must be within the same filesystem and
3777 * in the same filesystem as their parent directories within the
3778 * namecache topology.
3780 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3783 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3784 mp != tond->nl_nch.mount) {
3791 * Make sure the mount point is writable
3793 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3800 * If the target exists and either the source or target is a directory,
3801 * then both must be directories.
3803 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3806 if (tond->nl_nch.ncp->nc_vp) {
3807 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3809 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3810 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3812 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3818 * You cannot rename a source into itself or a subdirectory of itself.
3819 * We check this by travsersing the target directory upwards looking
3820 * for a match against the source.
3825 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3826 if (fromnd->nl_nch.ncp == ncp) {
3837 * Even though the namespaces are different, they may still represent
3838 * hardlinks to the same file. The filesystem might have a hard time
3839 * with this so we issue a NREMOVE of the source instead of a NRENAME
3840 * when we detect the situation.
3843 fdvp = fromnd->nl_dvp;
3844 tdvp = tond->nl_dvp;
3845 if (fdvp == NULL || tdvp == NULL) {
3847 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3848 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3851 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3852 fdvp, tdvp, tond->nl_cred);
3859 * rename_args(char *from, char *to)
3861 * Rename files. Source and destination must either both be directories,
3862 * or both not be directories. If target is a directory, it must be empty.
3865 sys_rename(struct rename_args *uap)
3867 struct nlookupdata fromnd, tond;
3871 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3873 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3875 error = kern_rename(&fromnd, &tond);
3876 nlookup_done(&tond);
3878 nlookup_done(&fromnd);
3879 } while (error == EAGAIN);
3884 * renameat_args(int oldfd, char *old, int newfd, char *new)
3886 * Rename files using paths relative to the directories associated with
3887 * oldfd and newfd. Source and destination must either both be directories,
3888 * or both not be directories. If target is a directory, it must be empty.
3891 sys_renameat(struct renameat_args *uap)
3893 struct nlookupdata oldnd, newnd;
3894 struct file *oldfp, *newfp;
3898 error = nlookup_init_at(&oldnd, &oldfp,
3899 uap->oldfd, uap->old,
3902 error = nlookup_init_at(&newnd, &newfp,
3903 uap->newfd, uap->new,
3906 error = kern_rename(&oldnd, &newnd);
3907 nlookup_done_at(&newnd, newfp);
3909 nlookup_done_at(&oldnd, oldfp);
3910 } while (error == EAGAIN);
3915 kern_mkdir(struct nlookupdata *nd, int mode)
3917 struct thread *td = curthread;
3918 struct proc *p = td->td_proc;
3924 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3925 if ((error = nlookup(nd)) != 0)
3928 if (nd->nl_nch.ncp->nc_vp)
3930 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3933 vattr.va_type = VDIR;
3934 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3937 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3944 * mkdir_args(char *path, int mode)
3946 * Make a directory file.
3949 sys_mkdir(struct mkdir_args *uap)
3951 struct nlookupdata nd;
3954 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3956 error = kern_mkdir(&nd, uap->mode);
3962 * mkdirat_args(int fd, char *path, mode_t mode)
3964 * Make a directory file. The path is relative to the directory associated
3968 sys_mkdirat(struct mkdirat_args *uap)
3970 struct nlookupdata nd;
3974 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3976 error = kern_mkdir(&nd, uap->mode);
3977 nlookup_done_at(&nd, fp);
3982 kern_rmdir(struct nlookupdata *nd)
3987 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3988 if ((error = nlookup(nd)) != 0)
3992 * Do not allow directories representing mount points to be
3993 * deleted, even if empty. Check write perms on mount point
3994 * in case the vnode is aliased (aka nullfs).
3996 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3998 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4000 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4005 * rmdir_args(char *path)
4007 * Remove a directory file.
4010 sys_rmdir(struct rmdir_args *uap)
4012 struct nlookupdata nd;
4015 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4017 error = kern_rmdir(&nd);
4023 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4024 enum uio_seg direction)
4026 struct thread *td = curthread;
4027 struct proc *p = td->td_proc;
4035 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4037 if ((fp->f_flag & FREAD) == 0) {
4041 vp = (struct vnode *)fp->f_data;
4043 if (vp->v_type != VDIR) {
4047 aiov.iov_base = buf;
4048 aiov.iov_len = count;
4049 auio.uio_iov = &aiov;
4050 auio.uio_iovcnt = 1;
4051 auio.uio_rw = UIO_READ;
4052 auio.uio_segflg = direction;
4054 auio.uio_resid = count;
4055 loff = auio.uio_offset = fp->f_offset;
4056 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4057 fp->f_offset = auio.uio_offset;
4060 if (count == auio.uio_resid) {
4061 if (union_dircheckp) {
4062 error = union_dircheckp(td, &vp, fp);
4069 if ((vp->v_flag & VROOT) &&
4070 (vp->v_mount->mnt_flag & MNT_UNION)) {
4071 struct vnode *tvp = vp;
4072 vp = vp->v_mount->mnt_vnodecovered;
4083 * WARNING! *basep may not be wide enough to accomodate the
4084 * seek offset. XXX should we hack this to return the upper 32 bits
4085 * for offsets greater then 4G?
4088 *basep = (long)loff;
4090 *res = count - auio.uio_resid;
4097 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4099 * Read a block of directory entries in a file system independent format.
4102 sys_getdirentries(struct getdirentries_args *uap)
4107 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4108 &uap->sysmsg_result, UIO_USERSPACE);
4110 if (error == 0 && uap->basep)
4111 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4116 * getdents_args(int fd, char *buf, size_t count)
4119 sys_getdents(struct getdents_args *uap)
4123 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4124 &uap->sysmsg_result, UIO_USERSPACE);
4130 * Set the mode mask for creation of filesystem nodes.
4132 * umask(int newmask)
4135 sys_umask(struct umask_args *uap)
4137 struct thread *td = curthread;
4138 struct proc *p = td->td_proc;
4139 struct filedesc *fdp;
4142 uap->sysmsg_result = fdp->fd_cmask;
4143 fdp->fd_cmask = uap->newmask & ALLPERMS;
4148 * revoke(char *path)
4150 * Void all references to file by ripping underlying filesystem
4154 sys_revoke(struct revoke_args *uap)
4156 struct nlookupdata nd;
4163 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4165 error = nlookup(&nd);
4167 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4168 cred = crhold(nd.nl_cred);
4172 error = VOP_GETATTR(vp, &vattr);
4173 if (error == 0 && cred->cr_uid != vattr.va_uid)
4174 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4175 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4177 error = vrevoke(vp, cred);
4178 } else if (error == 0) {
4179 error = vrevoke(vp, cred);
4189 * getfh_args(char *fname, fhandle_t *fhp)
4191 * Get (NFS) file handle
4193 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4194 * mount. This allows nullfs mounts to be explicitly exported.
4196 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4198 * nullfs mounts of subdirectories are not safe. That is, it will
4199 * work, but you do not really have protection against access to
4200 * the related parent directories.
4203 sys_getfh(struct getfh_args *uap)
4205 struct thread *td = curthread;
4206 struct nlookupdata nd;
4213 * Must be super user
4215 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4219 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4221 error = nlookup(&nd);
4223 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4224 mp = nd.nl_nch.mount;
4227 bzero(&fh, sizeof(fh));
4228 fh.fh_fsid = mp->mnt_stat.f_fsid;
4229 error = VFS_VPTOFH(vp, &fh.fh_fid);
4232 error = copyout(&fh, uap->fhp, sizeof(fh));
4238 * fhopen_args(const struct fhandle *u_fhp, int flags)
4240 * syscall for the rpc.lockd to use to translate a NFS file handle into
4241 * an open descriptor.
4243 * warning: do not remove the priv_check() call or this becomes one giant
4247 sys_fhopen(struct fhopen_args *uap)
4249 struct thread *td = curthread;
4250 struct filedesc *fdp = td->td_proc->p_fd;
4255 struct vattr *vap = &vat;
4257 int fmode, mode, error = 0, type;
4263 * Must be super user
4265 error = priv_check(td, PRIV_ROOT);
4269 fmode = FFLAGS(uap->flags);
4272 * Why not allow a non-read/write open for our lockd?
4274 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4276 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4281 * Find the mount point
4283 mp = vfs_getvfs(&fhp.fh_fsid);
4288 /* now give me my vnode, it gets returned to me locked */
4289 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4293 * from now on we have to make sure not
4294 * to forget about the vnode
4295 * any error that causes an abort must vput(vp)
4296 * just set error = err and 'goto bad;'.
4302 if (vp->v_type == VLNK) {
4306 if (vp->v_type == VSOCK) {
4311 if (fmode & (FWRITE | O_TRUNC)) {
4312 if (vp->v_type == VDIR) {
4316 error = vn_writechk(vp, NULL);
4324 error = VOP_ACCESS(vp, mode, td->td_ucred);
4328 if (fmode & O_TRUNC) {
4329 vn_unlock(vp); /* XXX */
4330 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4333 error = VOP_SETATTR(vp, vap, td->td_ucred);
4339 * VOP_OPEN needs the file pointer so it can potentially override
4342 * WARNING! no f_nchandle will be associated when fhopen()ing a
4345 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4349 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4352 * setting f_ops this way prevents VOP_CLOSE from being
4353 * called or fdrop() releasing the vp from v_data. Since
4354 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4356 fp->f_ops = &badfileops;
4362 * The fp is given its own reference, we still have our ref and lock.
4364 * Assert that all regular files must be created with a VM object.
4366 if (vp->v_type == VREG && vp->v_object == NULL) {
4367 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4372 * The open was successful. Handle any locking requirements.
4374 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4375 lf.l_whence = SEEK_SET;
4378 if (fmode & O_EXLOCK)
4379 lf.l_type = F_WRLCK;
4381 lf.l_type = F_RDLCK;
4382 if (fmode & FNONBLOCK)
4387 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4389 * release our private reference.
4391 fsetfd(fdp, NULL, indx);
4396 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4397 fp->f_flag |= FHASLOCK;
4401 * Clean up. Associate the file pointer with the previously
4402 * reserved descriptor and return it.
4405 fsetfd(fdp, fp, indx);
4407 uap->sysmsg_result = indx;
4408 if (uap->flags & O_CLOEXEC)
4409 error = fsetfdflags(fdp, indx, UF_EXCLOSE);
4413 fsetfd(fdp, NULL, indx);
4422 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4425 sys_fhstat(struct fhstat_args *uap)
4427 struct thread *td = curthread;
4435 * Must be super user
4437 error = priv_check(td, PRIV_ROOT);
4441 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4445 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4448 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4449 error = vn_stat(vp, &sb, td->td_ucred);
4454 error = copyout(&sb, uap->sb, sizeof(sb));
4459 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4462 sys_fhstatfs(struct fhstatfs_args *uap)
4464 struct thread *td = curthread;
4465 struct proc *p = td->td_proc;
4470 char *fullpath, *freepath;
4475 * Must be super user
4477 if ((error = priv_check(td, PRIV_ROOT)))
4480 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4483 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4487 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4492 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4497 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4500 error = mount_path(p, mp, &fullpath, &freepath);
4503 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4504 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4505 kfree(freepath, M_TEMP);
4507 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4508 if (priv_check(td, PRIV_ROOT)) {
4509 bcopy(sp, &sb, sizeof(sb));
4510 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4513 error = copyout(sp, uap->buf, sizeof(*sp));
4519 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4522 sys_fhstatvfs(struct fhstatvfs_args *uap)
4524 struct thread *td = curthread;
4525 struct proc *p = td->td_proc;
4533 * Must be super user
4535 if ((error = priv_check(td, PRIV_ROOT)))
4538 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4541 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4545 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4550 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4553 sp = &mp->mnt_vstat;
4555 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4559 if (mp->mnt_flag & MNT_RDONLY)
4560 sp->f_flag |= ST_RDONLY;
4561 if (mp->mnt_flag & MNT_NOSUID)
4562 sp->f_flag |= ST_NOSUID;
4563 error = copyout(sp, uap->buf, sizeof(*sp));
4570 * Syscall to push extended attribute configuration information into the
4571 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4572 * a command (int cmd), and attribute name and misc data. For now, the
4573 * attribute name is left in userspace for consumption by the VFS_op.
4574 * It will probably be changed to be copied into sysspace by the
4575 * syscall in the future, once issues with various consumers of the
4576 * attribute code have raised their hands.
4578 * Currently this is used only by UFS Extended Attributes.
4581 sys_extattrctl(struct extattrctl_args *uap)
4583 struct nlookupdata nd;
4585 char attrname[EXTATTR_MAXNAMELEN];
4593 if (error == 0 && uap->filename) {
4594 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4597 error = nlookup(&nd);
4599 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4603 if (error == 0 && uap->attrname) {
4604 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4609 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4611 error = nlookup(&nd);
4613 error = ncp_writechk(&nd.nl_nch);
4615 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4617 uap->attrname, nd.nl_cred);
4626 * Syscall to get a named extended attribute on a file or directory.
4629 sys_extattr_set_file(struct extattr_set_file_args *uap)
4631 char attrname[EXTATTR_MAXNAMELEN];
4632 struct nlookupdata nd;
4638 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4644 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4646 error = nlookup(&nd);
4648 error = ncp_writechk(&nd.nl_nch);
4650 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4656 bzero(&auio, sizeof(auio));
4657 aiov.iov_base = uap->data;
4658 aiov.iov_len = uap->nbytes;
4659 auio.uio_iov = &aiov;
4660 auio.uio_iovcnt = 1;
4661 auio.uio_offset = 0;
4662 auio.uio_resid = uap->nbytes;
4663 auio.uio_rw = UIO_WRITE;
4664 auio.uio_td = curthread;
4666 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4675 * Syscall to get a named extended attribute on a file or directory.
4678 sys_extattr_get_file(struct extattr_get_file_args *uap)
4680 char attrname[EXTATTR_MAXNAMELEN];
4681 struct nlookupdata nd;
4687 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4693 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4695 error = nlookup(&nd);
4697 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4703 bzero(&auio, sizeof(auio));
4704 aiov.iov_base = uap->data;
4705 aiov.iov_len = uap->nbytes;
4706 auio.uio_iov = &aiov;
4707 auio.uio_iovcnt = 1;
4708 auio.uio_offset = 0;
4709 auio.uio_resid = uap->nbytes;
4710 auio.uio_rw = UIO_READ;
4711 auio.uio_td = curthread;
4713 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4715 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4723 * Syscall to delete a named extended attribute from a file or directory.
4724 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4727 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4729 char attrname[EXTATTR_MAXNAMELEN];
4730 struct nlookupdata nd;
4734 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4738 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4740 error = nlookup(&nd);
4742 error = ncp_writechk(&nd.nl_nch);
4744 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4746 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4747 attrname, NULL, nd.nl_cred);
4756 * Determine if the mount is visible to the process.
4759 chroot_visible_mnt(struct mount *mp, struct proc *p)
4761 struct nchandle nch;
4764 * Traverse from the mount point upwards. If we hit the process
4765 * root then the mount point is visible to the process.
4767 nch = mp->mnt_ncmountpt;
4769 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4770 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4773 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4774 nch = nch.mount->mnt_ncmounton;
4776 nch.ncp = nch.ncp->nc_parent;
4781 * If the mount point is not visible to the process, but the
4782 * process root is in a subdirectory of the mount, return
4785 if (p->p_fd->fd_nrdir.mount == mp)