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)) {
259 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
266 vsetflags(vp, VMOUNT);
268 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
269 lwkt_gettoken(&mp->mnt_token);
275 * If the user is not root, ensure that they own the directory
276 * onto which we are attempting to mount.
278 if ((error = VOP_GETATTR(vp, &va)) ||
279 (va.va_uid != cred->cr_uid &&
280 (error = priv_check(td, PRIV_ROOT)))) {
285 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
290 if (vp->v_type != VDIR) {
296 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
302 vfsp = vfsconf_find_by_name(fstypename);
306 /* Only load modules for root (very important!) */
307 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
312 error = linker_load_file(fstypename, &lf);
313 if (error || lf == NULL) {
321 /* lookup again, see if the VFS was loaded */
322 vfsp = vfsconf_find_by_name(fstypename);
325 linker_file_unload(lf);
332 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
338 vsetflags(vp, VMOUNT);
341 * Allocate and initialize the filesystem.
343 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
345 vfs_busy(mp, LK_NOWAIT);
346 mp->mnt_op = vfsp->vfc_vfsops;
348 vfsp->vfc_refcount++;
349 mp->mnt_stat.f_type = vfsp->vfc_typenum;
350 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
351 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
352 mp->mnt_stat.f_owner = cred->cr_uid;
353 lwkt_gettoken(&mp->mnt_token);
357 * (per-mount token acquired at this point)
359 * Set the mount level flags.
361 if (uap->flags & MNT_RDONLY)
362 mp->mnt_flag |= MNT_RDONLY;
363 else if (mp->mnt_flag & MNT_RDONLY)
364 mp->mnt_kern_flag |= MNTK_WANTRDWR;
365 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
366 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
367 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
368 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
369 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
370 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
371 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
372 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
374 * Mount the filesystem.
375 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
378 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
379 if (mp->mnt_flag & MNT_UPDATE) {
380 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
381 mp->mnt_flag &= ~MNT_RDONLY;
382 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
383 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
386 mp->mnt_kern_flag = flag2;
388 lwkt_reltoken(&mp->mnt_token);
390 vclrflags(vp, VMOUNT);
395 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
398 * Put the new filesystem on the mount list after root. The mount
399 * point gets its own mnt_ncmountpt (unless the VFS already set one
400 * up) which represents the root of the mount. The lookup code
401 * detects the mount point going forward and checks the root of
402 * the mount going backwards.
404 * It is not necessary to invalidate or purge the vnode underneath
405 * because elements under the mount will be given their own glue
409 if (mp->mnt_ncmountpt.ncp == NULL) {
411 * allocate, then unlock, but leave the ref intact
413 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
414 cache_unlock(&mp->mnt_ncmountpt);
416 mp->mnt_ncmounton = nch; /* inherits ref */
417 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
418 cache_ismounting(mp);
420 vclrflags(vp, VMOUNT);
421 mountlist_insert(mp, MNTINS_LAST);
423 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
424 error = vfs_allocate_syncvnode(mp);
425 lwkt_reltoken(&mp->mnt_token);
427 error = VFS_START(mp, 0);
430 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
431 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
432 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
433 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
434 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
435 vclrflags(vp, VMOUNT);
436 mp->mnt_vfc->vfc_refcount--;
437 lwkt_reltoken(&mp->mnt_token);
448 * Scan all active processes to see if any of them have a current
449 * or root directory onto which the new filesystem has just been
450 * mounted. If so, replace them with the new mount point.
452 * Both old_nch and new_nch are ref'd on call but not locked.
453 * new_nch must be temporarily locked so it can be associated with the
454 * vnode representing the root of the mount point.
456 struct checkdirs_info {
457 struct nchandle old_nch;
458 struct nchandle new_nch;
459 struct vnode *old_vp;
460 struct vnode *new_vp;
463 static int checkdirs_callback(struct proc *p, void *data);
466 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
468 struct checkdirs_info info;
474 * If the old mount point's vnode has a usecount of 1, it is not
475 * being held as a descriptor anywhere.
477 olddp = old_nch->ncp->nc_vp;
478 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
482 * Force the root vnode of the new mount point to be resolved
483 * so we can update any matching processes.
486 if (VFS_ROOT(mp, &newdp))
487 panic("mount: lost mount");
489 cache_setunresolved(new_nch);
490 cache_setvp(new_nch, newdp);
491 cache_unlock(new_nch);
494 * Special handling of the root node
496 if (rootvnode == olddp) {
498 vfs_cache_setroot(newdp, cache_hold(new_nch));
502 * Pass newdp separately so the callback does not have to access
503 * it via new_nch->ncp->nc_vp.
505 info.old_nch = *old_nch;
506 info.new_nch = *new_nch;
508 allproc_scan(checkdirs_callback, &info);
513 * NOTE: callback is not MP safe because the scanned process's filedesc
514 * structure can be ripped out from under us, amoung other things.
517 checkdirs_callback(struct proc *p, void *data)
519 struct checkdirs_info *info = data;
520 struct filedesc *fdp;
521 struct nchandle ncdrop1;
522 struct nchandle ncdrop2;
523 struct vnode *vprele1;
524 struct vnode *vprele2;
526 if ((fdp = p->p_fd) != NULL) {
527 cache_zero(&ncdrop1);
528 cache_zero(&ncdrop2);
533 * MPUNSAFE - XXX fdp can be pulled out from under a
536 * A shared filedesc is ok, we don't have to copy it
537 * because we are making this change globally.
539 spin_lock(&fdp->fd_spin);
540 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
541 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
542 vprele1 = fdp->fd_cdir;
544 fdp->fd_cdir = info->new_vp;
545 ncdrop1 = fdp->fd_ncdir;
546 cache_copy(&info->new_nch, &fdp->fd_ncdir);
548 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
549 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
550 vprele2 = fdp->fd_rdir;
552 fdp->fd_rdir = info->new_vp;
553 ncdrop2 = fdp->fd_nrdir;
554 cache_copy(&info->new_nch, &fdp->fd_nrdir);
556 spin_unlock(&fdp->fd_spin);
558 cache_drop(&ncdrop1);
560 cache_drop(&ncdrop2);
570 * Unmount a file system.
572 * Note: unmount takes a path to the vnode mounted on as argument,
573 * not special file (as before).
575 * umount_args(char *path, int flags)
580 sys_unmount(struct unmount_args *uap)
582 struct thread *td = curthread;
583 struct proc *p __debugvar = td->td_proc;
584 struct mount *mp = NULL;
585 struct nlookupdata nd;
590 if (td->td_ucred->cr_prison != NULL) {
594 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
597 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
599 error = nlookup(&nd);
603 mp = nd.nl_nch.mount;
606 * Only root, or the user that did the original mount is
607 * permitted to unmount this filesystem.
609 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
610 (error = priv_check(td, PRIV_ROOT)))
614 * Don't allow unmounting the root file system.
616 if (mp->mnt_flag & MNT_ROOTFS) {
622 * Must be the root of the filesystem
624 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
632 error = dounmount(mp, uap->flags);
639 * Do the actual file system unmount.
642 dounmount_interlock(struct mount *mp)
644 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
646 mp->mnt_kern_flag |= MNTK_UNMOUNT;
651 unmount_allproc_cb(struct proc *p, void *arg)
655 if (p->p_textnch.ncp == NULL)
658 mp = (struct mount *)arg;
659 if (p->p_textnch.mount == mp)
660 cache_drop(&p->p_textnch);
666 dounmount(struct mount *mp, int flags)
668 struct namecache *ncp;
677 lwkt_gettoken(&mp->mnt_token);
679 * Exclusive access for unmounting purposes
681 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
685 * Allow filesystems to detect that a forced unmount is in progress.
687 if (flags & MNT_FORCE)
688 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
689 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
690 error = lockmgr(&mp->mnt_lock, lflags);
692 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
693 if (mp->mnt_kern_flag & MNTK_MWAIT) {
694 mp->mnt_kern_flag &= ~MNTK_MWAIT;
700 if (mp->mnt_flag & MNT_EXPUBLIC)
701 vfs_setpublicfs(NULL, NULL, NULL);
703 vfs_msync(mp, MNT_WAIT);
704 async_flag = mp->mnt_flag & MNT_ASYNC;
705 mp->mnt_flag &=~ MNT_ASYNC;
708 * If this filesystem isn't aliasing other filesystems,
709 * try to invalidate any remaining namecache entries and
710 * check the count afterwords.
712 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
713 cache_lock(&mp->mnt_ncmountpt);
714 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
715 cache_unlock(&mp->mnt_ncmountpt);
717 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
718 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
719 allproc_scan(&unmount_allproc_cb, mp);
722 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
723 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
725 if ((flags & MNT_FORCE) == 0) {
727 mount_warning(mp, "Cannot unmount: "
733 mount_warning(mp, "Forced unmount: "
744 * Decomission our special mnt_syncer vnode. This also stops
745 * the vnlru code. If we are unable to unmount we recommission
748 * Then sync the filesystem.
750 if ((vp = mp->mnt_syncer) != NULL) {
751 mp->mnt_syncer = NULL;
754 if ((mp->mnt_flag & MNT_RDONLY) == 0)
755 VFS_SYNC(mp, MNT_WAIT);
758 * nchandle records ref the mount structure. Expect a count of 1
759 * (our mount->mnt_ncmountpt).
761 * Scans can get temporary refs on a mountpoint (thought really
762 * heavy duty stuff like cache_findmount() do not).
764 for (retry = 0; retry < 10 && mp->mnt_refs != 1; ++retry) {
765 cache_unmounting(mp);
766 tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
768 if (mp->mnt_refs != 1) {
769 if ((flags & MNT_FORCE) == 0) {
770 mount_warning(mp, "Cannot unmount: "
771 "%d mount refs still present",
775 mount_warning(mp, "Forced unmount: "
776 "%d mount refs still present",
783 * So far so good, sync the filesystem once more and
784 * call the VFS unmount code if the sync succeeds.
787 if (((mp->mnt_flag & MNT_RDONLY) ||
788 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
789 (flags & MNT_FORCE)) {
790 error = VFS_UNMOUNT(mp, flags);
795 * If an error occurred we can still recover, restoring the
796 * syncer vnode and misc flags.
799 if (mp->mnt_syncer == NULL)
800 vfs_allocate_syncvnode(mp);
801 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
802 mp->mnt_flag |= async_flag;
803 lockmgr(&mp->mnt_lock, LK_RELEASE);
804 if (mp->mnt_kern_flag & MNTK_MWAIT) {
805 mp->mnt_kern_flag &= ~MNTK_MWAIT;
811 * Clean up any journals still associated with the mount after
812 * filesystem activity has ceased.
814 journal_remove_all_journals(mp,
815 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
817 mountlist_remove(mp);
820 * Remove any installed vnode ops here so the individual VFSs don't
823 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
824 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
825 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
826 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
827 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
829 if (mp->mnt_ncmountpt.ncp != NULL) {
830 nch = mp->mnt_ncmountpt;
831 cache_zero(&mp->mnt_ncmountpt);
832 cache_clrmountpt(&nch);
835 if (mp->mnt_ncmounton.ncp != NULL) {
836 cache_unmounting(mp);
837 nch = mp->mnt_ncmounton;
838 cache_zero(&mp->mnt_ncmounton);
839 cache_clrmountpt(&nch);
843 mp->mnt_vfc->vfc_refcount--;
844 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
845 panic("unmount: dangling vnode");
846 lockmgr(&mp->mnt_lock, LK_RELEASE);
847 if (mp->mnt_kern_flag & MNTK_MWAIT) {
848 mp->mnt_kern_flag &= ~MNTK_MWAIT;
853 * If we reach here and freeok != 0 we must free the mount.
854 * If refs > 1 cycle and wait, just in case someone tried
855 * to busy the mount after we decided to do the unmount.
858 while (mp->mnt_refs > 1) {
859 cache_unmounting(mp);
861 tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
863 lwkt_reltoken(&mp->mnt_token);
870 lwkt_reltoken(&mp->mnt_token);
876 mount_warning(struct mount *mp, const char *ctl, ...)
883 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
884 &ptr, &buf, 0) == 0) {
885 kprintf("unmount(%s): ", ptr);
890 kprintf("unmount(%p", mp);
891 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
892 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
901 * Shim cache_fullpath() to handle the case where a process is chrooted into
902 * a subdirectory of a mount. In this case if the root mount matches the
903 * process root directory's mount we have to specify the process's root
904 * directory instead of the mount point, because the mount point might
905 * be above the root directory.
909 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
911 struct nchandle *nch;
913 if (p && p->p_fd->fd_nrdir.mount == mp)
914 nch = &p->p_fd->fd_nrdir;
916 nch = &mp->mnt_ncmountpt;
917 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
921 * Sync each mounted filesystem.
925 static int syncprt = 0;
926 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
929 static int sync_callback(struct mount *mp, void *data);
932 sys_sync(struct sync_args *uap)
934 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
937 * print out buffer pool stat information on each sync() call.
947 sync_callback(struct mount *mp, void *data __unused)
951 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
952 asyncflag = mp->mnt_flag & MNT_ASYNC;
953 mp->mnt_flag &= ~MNT_ASYNC;
954 vfs_msync(mp, MNT_NOWAIT);
955 VFS_SYNC(mp, MNT_NOWAIT);
956 mp->mnt_flag |= asyncflag;
961 /* XXX PRISON: could be per prison flag */
962 static int prison_quotas;
964 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
968 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
970 * Change filesystem quotas.
975 sys_quotactl(struct quotactl_args *uap)
977 struct nlookupdata nd;
984 if (td->td_ucred->cr_prison && !prison_quotas) {
989 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
991 error = nlookup(&nd);
993 mp = nd.nl_nch.mount;
994 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
995 uap->arg, nd.nl_cred);
1004 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1005 * void *buf, int buflen)
1007 * This function operates on a mount point and executes the specified
1008 * operation using the specified control data, and possibly returns data.
1010 * The actual number of bytes stored in the result buffer is returned, 0
1011 * if none, otherwise an error is returned.
1016 sys_mountctl(struct mountctl_args *uap)
1018 struct thread *td = curthread;
1019 struct proc *p = td->td_proc;
1027 * Sanity and permissions checks. We must be root.
1030 if (td->td_ucred->cr_prison != NULL)
1032 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1033 (error = priv_check(td, PRIV_ROOT)) != 0)
1037 * Argument length checks
1039 if (uap->ctllen < 0 || uap->ctllen > 1024)
1041 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1043 if (uap->path == NULL)
1047 * Allocate the necessary buffers and copyin data
1049 path = objcache_get(namei_oc, M_WAITOK);
1050 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1055 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1056 error = copyin(uap->ctl, ctl, uap->ctllen);
1061 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1064 * Validate the descriptor
1067 fp = holdfp(p->p_fd, uap->fd, -1);
1077 * Execute the internal kernel function and clean up.
1080 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1084 if (error == 0 && uap->sysmsg_result > 0)
1085 error = copyout(buf, uap->buf, uap->sysmsg_result);
1088 objcache_put(namei_oc, path);
1097 * Execute a mount control operation by resolving the path to a mount point
1098 * and calling vop_mountctl().
1100 * Use the mount point from the nch instead of the vnode so nullfs mounts
1101 * can properly spike the VOP.
1104 kern_mountctl(const char *path, int op, struct file *fp,
1105 const void *ctl, int ctllen,
1106 void *buf, int buflen, int *res)
1110 struct nlookupdata nd;
1115 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1117 error = nlookup(&nd);
1119 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1120 mp = nd.nl_nch.mount;
1127 * Must be the root of the filesystem
1129 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1133 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1140 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1142 struct thread *td = curthread;
1143 struct proc *p = td->td_proc;
1146 char *fullpath, *freepath;
1149 if ((error = nlookup(nd)) != 0)
1151 mp = nd->nl_nch.mount;
1153 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1156 error = mount_path(p, mp, &fullpath, &freepath);
1159 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1160 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1161 kfree(freepath, M_TEMP);
1163 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1164 bcopy(sp, buf, sizeof(*buf));
1165 /* Only root should have access to the fsid's. */
1166 if (priv_check(td, PRIV_ROOT))
1167 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1172 * statfs_args(char *path, struct statfs *buf)
1174 * Get filesystem statistics.
1177 sys_statfs(struct statfs_args *uap)
1179 struct nlookupdata nd;
1183 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1185 error = kern_statfs(&nd, &buf);
1188 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1193 kern_fstatfs(int fd, struct statfs *buf)
1195 struct thread *td = curthread;
1196 struct proc *p = td->td_proc;
1200 char *fullpath, *freepath;
1204 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1208 * Try to use mount info from any overlays rather than the
1209 * mount info for the underlying vnode, otherwise we will
1210 * fail when operating on null-mounted paths inside a chroot.
1212 if ((mp = fp->f_nchandle.mount) == NULL)
1213 mp = ((struct vnode *)fp->f_data)->v_mount;
1218 if (fp->f_cred == NULL) {
1223 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1226 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1228 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1229 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1230 kfree(freepath, M_TEMP);
1232 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1233 bcopy(sp, buf, sizeof(*buf));
1235 /* Only root should have access to the fsid's. */
1236 if (priv_check(td, PRIV_ROOT))
1237 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1245 * fstatfs_args(int fd, struct statfs *buf)
1247 * Get filesystem statistics.
1250 sys_fstatfs(struct fstatfs_args *uap)
1255 error = kern_fstatfs(uap->fd, &buf);
1258 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1263 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1269 if ((error = nlookup(nd)) != 0)
1271 mp = nd->nl_nch.mount;
1272 sp = &mp->mnt_vstat;
1273 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1277 if (mp->mnt_flag & MNT_RDONLY)
1278 sp->f_flag |= ST_RDONLY;
1279 if (mp->mnt_flag & MNT_NOSUID)
1280 sp->f_flag |= ST_NOSUID;
1281 bcopy(sp, buf, sizeof(*buf));
1286 * statfs_args(char *path, struct statfs *buf)
1288 * Get filesystem statistics.
1291 sys_statvfs(struct statvfs_args *uap)
1293 struct nlookupdata nd;
1297 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1299 error = kern_statvfs(&nd, &buf);
1302 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1307 kern_fstatvfs(int fd, struct statvfs *buf)
1309 struct thread *td = curthread;
1310 struct proc *p = td->td_proc;
1317 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1319 if ((mp = fp->f_nchandle.mount) == NULL)
1320 mp = ((struct vnode *)fp->f_data)->v_mount;
1325 if (fp->f_cred == NULL) {
1329 sp = &mp->mnt_vstat;
1330 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1334 if (mp->mnt_flag & MNT_RDONLY)
1335 sp->f_flag |= ST_RDONLY;
1336 if (mp->mnt_flag & MNT_NOSUID)
1337 sp->f_flag |= ST_NOSUID;
1339 bcopy(sp, buf, sizeof(*buf));
1347 * fstatfs_args(int fd, struct statfs *buf)
1349 * Get filesystem statistics.
1352 sys_fstatvfs(struct fstatvfs_args *uap)
1357 error = kern_fstatvfs(uap->fd, &buf);
1360 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1365 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1367 * Get statistics on all filesystems.
1370 struct getfsstat_info {
1371 struct statfs *sfsp;
1379 static int getfsstat_callback(struct mount *, void *);
1382 sys_getfsstat(struct getfsstat_args *uap)
1384 struct thread *td = curthread;
1385 struct getfsstat_info info;
1387 bzero(&info, sizeof(info));
1389 info.maxcount = uap->bufsize / sizeof(struct statfs);
1390 info.sfsp = uap->buf;
1392 info.flags = uap->flags;
1395 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1396 if (info.sfsp && info.count > info.maxcount)
1397 uap->sysmsg_result = info.maxcount;
1399 uap->sysmsg_result = info.count;
1400 return (info.error);
1404 getfsstat_callback(struct mount *mp, void *data)
1406 struct getfsstat_info *info = data;
1412 if (info->sfsp && info->count < info->maxcount) {
1413 if (info->td->td_proc &&
1414 !chroot_visible_mnt(mp, info->td->td_proc)) {
1420 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1421 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1422 * overrides MNT_WAIT.
1424 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1425 (info->flags & MNT_WAIT)) &&
1426 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1429 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1431 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1433 info->error = error;
1436 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1437 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1438 kfree(freepath, M_TEMP);
1440 error = copyout(sp, info->sfsp, sizeof(*sp));
1442 info->error = error;
1452 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1453 long bufsize, int flags)
1455 * Get statistics on all filesystems.
1458 struct getvfsstat_info {
1459 struct statfs *sfsp;
1460 struct statvfs *vsfsp;
1468 static int getvfsstat_callback(struct mount *, void *);
1471 sys_getvfsstat(struct getvfsstat_args *uap)
1473 struct thread *td = curthread;
1474 struct getvfsstat_info info;
1476 bzero(&info, sizeof(info));
1478 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1479 info.sfsp = uap->buf;
1480 info.vsfsp = uap->vbuf;
1482 info.flags = uap->flags;
1485 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1486 if (info.vsfsp && info.count > info.maxcount)
1487 uap->sysmsg_result = info.maxcount;
1489 uap->sysmsg_result = info.count;
1490 return (info.error);
1494 getvfsstat_callback(struct mount *mp, void *data)
1496 struct getvfsstat_info *info = data;
1498 struct statvfs *vsp;
1503 if (info->vsfsp && info->count < info->maxcount) {
1504 if (info->td->td_proc &&
1505 !chroot_visible_mnt(mp, info->td->td_proc)) {
1509 vsp = &mp->mnt_vstat;
1512 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1513 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1514 * overrides MNT_WAIT.
1516 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1517 (info->flags & MNT_WAIT)) &&
1518 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1521 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1523 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1524 (info->flags & MNT_WAIT)) &&
1525 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1529 if (mp->mnt_flag & MNT_RDONLY)
1530 vsp->f_flag |= ST_RDONLY;
1531 if (mp->mnt_flag & MNT_NOSUID)
1532 vsp->f_flag |= ST_NOSUID;
1534 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1536 info->error = error;
1539 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1540 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1541 kfree(freepath, M_TEMP);
1543 error = copyout(sp, info->sfsp, sizeof(*sp));
1545 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1547 info->error = error;
1559 * fchdir_args(int fd)
1561 * Change current working directory to a given file descriptor.
1564 sys_fchdir(struct fchdir_args *uap)
1566 struct thread *td = curthread;
1567 struct proc *p = td->td_proc;
1568 struct filedesc *fdp = p->p_fd;
1569 struct vnode *vp, *ovp;
1572 struct nchandle nch, onch, tnch;
1575 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1577 lwkt_gettoken(&p->p_token);
1578 vp = (struct vnode *)fp->f_data;
1580 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1581 if (fp->f_nchandle.ncp == NULL)
1584 error = checkvp_chdir(vp, td);
1589 cache_copy(&fp->f_nchandle, &nch);
1592 * If the ncp has become a mount point, traverse through
1596 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1597 (mp = cache_findmount(&nch)) != NULL
1599 error = nlookup_mp(mp, &tnch);
1601 cache_unlock(&tnch); /* leave ref intact */
1603 vp = tnch.ncp->nc_vp;
1604 error = vget(vp, LK_SHARED);
1605 KKASSERT(error == 0);
1609 cache_dropmount(mp);
1613 onch = fdp->fd_ncdir;
1614 vn_unlock(vp); /* leave ref intact */
1616 fdp->fd_ncdir = nch;
1625 lwkt_reltoken(&p->p_token);
1630 kern_chdir(struct nlookupdata *nd)
1632 struct thread *td = curthread;
1633 struct proc *p = td->td_proc;
1634 struct filedesc *fdp = p->p_fd;
1635 struct vnode *vp, *ovp;
1636 struct nchandle onch;
1639 if ((error = nlookup(nd)) != 0)
1641 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1643 if ((error = vget(vp, LK_SHARED)) != 0)
1646 lwkt_gettoken(&p->p_token);
1647 error = checkvp_chdir(vp, td);
1651 onch = fdp->fd_ncdir;
1652 cache_unlock(&nd->nl_nch); /* leave reference intact */
1653 fdp->fd_ncdir = nd->nl_nch;
1657 cache_zero(&nd->nl_nch);
1661 lwkt_reltoken(&p->p_token);
1666 * chdir_args(char *path)
1668 * Change current working directory (``.'').
1671 sys_chdir(struct chdir_args *uap)
1673 struct nlookupdata nd;
1676 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1678 error = kern_chdir(&nd);
1684 * Helper function for raised chroot(2) security function: Refuse if
1685 * any filedescriptors are open directories.
1688 chroot_refuse_vdir_fds(struct filedesc *fdp)
1695 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1696 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1698 vp = (struct vnode *)fp->f_data;
1699 if (vp->v_type != VDIR) {
1710 * This sysctl determines if we will allow a process to chroot(2) if it
1711 * has a directory open:
1712 * 0: disallowed for all processes.
1713 * 1: allowed for processes that were not already chroot(2)'ed.
1714 * 2: allowed for all processes.
1717 static int chroot_allow_open_directories = 1;
1719 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1720 &chroot_allow_open_directories, 0, "");
1723 * chroot to the specified namecache entry. We obtain the vp from the
1724 * namecache data. The passed ncp must be locked and referenced and will
1725 * remain locked and referenced on return.
1728 kern_chroot(struct nchandle *nch)
1730 struct thread *td = curthread;
1731 struct proc *p = td->td_proc;
1732 struct filedesc *fdp = p->p_fd;
1737 * Only privileged user can chroot
1739 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1744 * Disallow open directory descriptors (fchdir() breakouts).
1746 if (chroot_allow_open_directories == 0 ||
1747 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1748 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1751 if ((vp = nch->ncp->nc_vp) == NULL)
1754 if ((error = vget(vp, LK_SHARED)) != 0)
1758 * Check the validity of vp as a directory to change to and
1759 * associate it with rdir/jdir.
1761 error = checkvp_chdir(vp, td);
1762 vn_unlock(vp); /* leave reference intact */
1764 vrele(fdp->fd_rdir);
1765 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1766 cache_drop(&fdp->fd_nrdir);
1767 cache_copy(nch, &fdp->fd_nrdir);
1768 if (fdp->fd_jdir == NULL) {
1771 cache_copy(nch, &fdp->fd_njdir);
1780 * chroot_args(char *path)
1782 * Change notion of root (``/'') directory.
1785 sys_chroot(struct chroot_args *uap)
1787 struct thread *td __debugvar = curthread;
1788 struct nlookupdata nd;
1791 KKASSERT(td->td_proc);
1792 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1794 nd.nl_flags |= NLC_EXEC;
1795 error = nlookup(&nd);
1797 error = kern_chroot(&nd.nl_nch);
1804 sys_chroot_kernel(struct chroot_kernel_args *uap)
1806 struct thread *td = curthread;
1807 struct nlookupdata nd;
1808 struct nchandle *nch;
1812 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1816 error = nlookup(&nd);
1822 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1826 if ((vp = nch->ncp->nc_vp) == NULL) {
1831 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1834 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1836 vfs_cache_setroot(vp, cache_hold(nch));
1846 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1847 * determine whether it is legal to chdir to the vnode. The vnode's state
1848 * is not changed by this call.
1851 checkvp_chdir(struct vnode *vp, struct thread *td)
1855 if (vp->v_type != VDIR)
1858 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1863 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1865 struct thread *td = curthread;
1866 struct proc *p = td->td_proc;
1867 struct lwp *lp = td->td_lwp;
1868 struct filedesc *fdp = p->p_fd;
1873 int type, indx, error = 0;
1876 if ((oflags & O_ACCMODE) == O_ACCMODE)
1878 flags = FFLAGS(oflags);
1879 error = falloc(lp, &nfp, NULL);
1883 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1886 * XXX p_dupfd is a real mess. It allows a device to return a
1887 * file descriptor to be duplicated rather then doing the open
1893 * Call vn_open() to do the lookup and assign the vnode to the
1894 * file pointer. vn_open() does not change the ref count on fp
1895 * and the vnode, on success, will be inherited by the file pointer
1898 nd->nl_flags |= NLC_LOCKVP;
1899 error = vn_open(nd, fp, flags, cmode);
1903 * handle special fdopen() case. bleh. dupfdopen() is
1904 * responsible for dropping the old contents of ofiles[indx]
1907 * Note that fsetfd() will add a ref to fp which represents
1908 * the fd_files[] assignment. We must still drop our
1911 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1912 if (fdalloc(p, 0, &indx) == 0) {
1913 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1916 fdrop(fp); /* our ref */
1919 fsetfd(fdp, NULL, indx);
1922 fdrop(fp); /* our ref */
1923 if (error == ERESTART)
1929 * ref the vnode for ourselves so it can't be ripped out from under
1930 * is. XXX need an ND flag to request that the vnode be returned
1933 * Reserve a file descriptor but do not assign it until the open
1936 vp = (struct vnode *)fp->f_data;
1938 if ((error = fdalloc(p, 0, &indx)) != 0) {
1945 * If no error occurs the vp will have been assigned to the file
1950 if (flags & (O_EXLOCK | O_SHLOCK)) {
1951 lf.l_whence = SEEK_SET;
1954 if (flags & O_EXLOCK)
1955 lf.l_type = F_WRLCK;
1957 lf.l_type = F_RDLCK;
1958 if (flags & FNONBLOCK)
1963 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1965 * lock request failed. Clean up the reserved
1969 fsetfd(fdp, NULL, indx);
1973 fp->f_flag |= FHASLOCK;
1977 * Assert that all regular file vnodes were created with a object.
1979 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1980 ("open: regular file has no backing object after vn_open"));
1986 * release our private reference, leaving the one associated with the
1987 * descriptor table intact.
1989 fsetfd(fdp, fp, indx);
1992 if (oflags & O_CLOEXEC)
1993 error = fsetfdflags(fdp, *res, UF_EXCLOSE);
1998 * open_args(char *path, int flags, int mode)
2000 * Check permissions, allocate an open file structure,
2001 * and call the device open routine if any.
2004 sys_open(struct open_args *uap)
2006 struct nlookupdata nd;
2009 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2011 error = kern_open(&nd, uap->flags,
2012 uap->mode, &uap->sysmsg_result);
2019 * openat_args(int fd, char *path, int flags, int mode)
2022 sys_openat(struct openat_args *uap)
2024 struct nlookupdata nd;
2028 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2030 error = kern_open(&nd, uap->flags, uap->mode,
2031 &uap->sysmsg_result);
2033 nlookup_done_at(&nd, fp);
2038 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2040 struct thread *td = curthread;
2041 struct proc *p = td->td_proc;
2050 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2051 vattr.va_rmajor = rmajor;
2052 vattr.va_rminor = rminor;
2054 switch (mode & S_IFMT) {
2055 case S_IFMT: /* used by badsect to flag bad sectors */
2056 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2057 vattr.va_type = VBAD;
2060 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2061 vattr.va_type = VCHR;
2064 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2065 vattr.va_type = VBLK;
2068 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2071 case S_IFDIR: /* special directories support for HAMMER */
2072 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2073 vattr.va_type = VDIR;
2084 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2085 if ((error = nlookup(nd)) != 0)
2087 if (nd->nl_nch.ncp->nc_vp)
2089 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2093 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2094 nd->nl_cred, NAMEI_CREATE);
2097 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2098 &vp, nd->nl_cred, &vattr);
2106 * mknod_args(char *path, int mode, int dev)
2108 * Create a special file.
2111 sys_mknod(struct mknod_args *uap)
2113 struct nlookupdata nd;
2116 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2118 error = kern_mknod(&nd, uap->mode,
2119 umajor(uap->dev), uminor(uap->dev));
2126 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2128 * Create a special file. The path is relative to the directory associated
2132 sys_mknodat(struct mknodat_args *uap)
2134 struct nlookupdata nd;
2138 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2140 error = kern_mknod(&nd, uap->mode,
2141 umajor(uap->dev), uminor(uap->dev));
2143 nlookup_done_at(&nd, fp);
2148 kern_mkfifo(struct nlookupdata *nd, int mode)
2150 struct thread *td = curthread;
2151 struct proc *p = td->td_proc;
2158 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2159 if ((error = nlookup(nd)) != 0)
2161 if (nd->nl_nch.ncp->nc_vp)
2163 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2167 vattr.va_type = VFIFO;
2168 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2170 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2177 * mkfifo_args(char *path, int mode)
2179 * Create a named pipe.
2182 sys_mkfifo(struct mkfifo_args *uap)
2184 struct nlookupdata nd;
2187 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2189 error = kern_mkfifo(&nd, uap->mode);
2195 * mkfifoat_args(int fd, char *path, mode_t mode)
2197 * Create a named pipe. The path is relative to the directory associated
2201 sys_mkfifoat(struct mkfifoat_args *uap)
2203 struct nlookupdata nd;
2207 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2209 error = kern_mkfifo(&nd, uap->mode);
2210 nlookup_done_at(&nd, fp);
2214 static int hardlink_check_uid = 0;
2215 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2216 &hardlink_check_uid, 0,
2217 "Unprivileged processes cannot create hard links to files owned by other "
2219 static int hardlink_check_gid = 0;
2220 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2221 &hardlink_check_gid, 0,
2222 "Unprivileged processes cannot create hard links to files owned by other "
2226 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2232 * Shortcut if disabled
2234 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2238 * Privileged user can always hardlink
2240 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2244 * Otherwise only if the originating file is owned by the
2245 * same user or group. Note that any group is allowed if
2246 * the file is owned by the caller.
2248 error = VOP_GETATTR(vp, &va);
2252 if (hardlink_check_uid) {
2253 if (cred->cr_uid != va.va_uid)
2257 if (hardlink_check_gid) {
2258 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2266 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2268 struct thread *td = curthread;
2273 * Lookup the source and obtained a locked vnode.
2275 * You may only hardlink a file which you have write permission
2276 * on or which you own.
2278 * XXX relookup on vget failure / race ?
2281 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2282 if ((error = nlookup(nd)) != 0)
2284 vp = nd->nl_nch.ncp->nc_vp;
2285 KKASSERT(vp != NULL);
2286 if (vp->v_type == VDIR)
2287 return (EPERM); /* POSIX */
2288 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2290 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2294 * Unlock the source so we can lookup the target without deadlocking
2295 * (XXX vp is locked already, possible other deadlock?). The target
2298 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2299 nd->nl_flags &= ~NLC_NCPISLOCKED;
2300 cache_unlock(&nd->nl_nch);
2303 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2304 if ((error = nlookup(linknd)) != 0) {
2308 if (linknd->nl_nch.ncp->nc_vp) {
2312 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2318 * Finally run the new API VOP.
2320 error = can_hardlink(vp, td, td->td_ucred);
2322 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2323 vp, linknd->nl_cred);
2330 * link_args(char *path, char *link)
2332 * Make a hard file link.
2335 sys_link(struct link_args *uap)
2337 struct nlookupdata nd, linknd;
2340 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2342 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2344 error = kern_link(&nd, &linknd);
2345 nlookup_done(&linknd);
2352 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2354 * Make a hard file link. The path1 argument is relative to the directory
2355 * associated with fd1, and similarly the path2 argument is relative to
2356 * the directory associated with fd2.
2359 sys_linkat(struct linkat_args *uap)
2361 struct nlookupdata nd, linknd;
2362 struct file *fp1, *fp2;
2365 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2366 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2368 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2369 uap->path2, UIO_USERSPACE, 0);
2371 error = kern_link(&nd, &linknd);
2372 nlookup_done_at(&linknd, fp2);
2374 nlookup_done_at(&nd, fp1);
2379 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2387 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2388 if ((error = nlookup(nd)) != 0)
2390 if (nd->nl_nch.ncp->nc_vp)
2392 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2396 vattr.va_mode = mode;
2397 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2404 * symlink(char *path, char *link)
2406 * Make a symbolic link.
2409 sys_symlink(struct symlink_args *uap)
2411 struct thread *td = curthread;
2412 struct nlookupdata nd;
2417 path = objcache_get(namei_oc, M_WAITOK);
2418 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2420 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2422 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2423 error = kern_symlink(&nd, path, mode);
2427 objcache_put(namei_oc, path);
2432 * symlinkat_args(char *path1, int fd, char *path2)
2434 * Make a symbolic link. The path2 argument is relative to the directory
2435 * associated with fd.
2438 sys_symlinkat(struct symlinkat_args *uap)
2440 struct thread *td = curthread;
2441 struct nlookupdata nd;
2447 path1 = objcache_get(namei_oc, M_WAITOK);
2448 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2450 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2453 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2454 error = kern_symlink(&nd, path1, mode);
2456 nlookup_done_at(&nd, fp);
2458 objcache_put(namei_oc, path1);
2463 * undelete_args(char *path)
2465 * Delete a whiteout from the filesystem.
2468 sys_undelete(struct undelete_args *uap)
2470 struct nlookupdata nd;
2473 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2475 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2477 error = nlookup(&nd);
2479 error = ncp_writechk(&nd.nl_nch);
2481 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2489 kern_unlink(struct nlookupdata *nd)
2494 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2495 if ((error = nlookup(nd)) != 0)
2497 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2499 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2504 * unlink_args(char *path)
2506 * Delete a name from the filesystem.
2509 sys_unlink(struct unlink_args *uap)
2511 struct nlookupdata nd;
2514 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2516 error = kern_unlink(&nd);
2523 * unlinkat_args(int fd, char *path, int flags)
2525 * Delete the file or directory entry pointed to by fd/path.
2528 sys_unlinkat(struct unlinkat_args *uap)
2530 struct nlookupdata nd;
2534 if (uap->flags & ~AT_REMOVEDIR)
2537 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2539 if (uap->flags & AT_REMOVEDIR)
2540 error = kern_rmdir(&nd);
2542 error = kern_unlink(&nd);
2544 nlookup_done_at(&nd, fp);
2549 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2551 struct thread *td = curthread;
2552 struct proc *p = td->td_proc;
2559 fp = holdfp(p->p_fd, fd, -1);
2562 if (fp->f_type != DTYPE_VNODE) {
2566 vp = (struct vnode *)fp->f_data;
2570 spin_lock(&fp->f_spin);
2571 new_offset = fp->f_offset + offset;
2575 error = VOP_GETATTR(vp, &vattr);
2576 spin_lock(&fp->f_spin);
2577 new_offset = offset + vattr.va_size;
2580 new_offset = offset;
2582 spin_lock(&fp->f_spin);
2587 spin_lock(&fp->f_spin);
2592 * Validate the seek position. Negative offsets are not allowed
2593 * for regular files or directories.
2595 * Normally we would also not want to allow negative offsets for
2596 * character and block-special devices. However kvm addresses
2597 * on 64 bit architectures might appear to be negative and must
2601 if (new_offset < 0 &&
2602 (vp->v_type == VREG || vp->v_type == VDIR)) {
2605 fp->f_offset = new_offset;
2608 *res = fp->f_offset;
2609 spin_unlock(&fp->f_spin);
2616 * lseek_args(int fd, int pad, off_t offset, int whence)
2618 * Reposition read/write file offset.
2621 sys_lseek(struct lseek_args *uap)
2625 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2626 &uap->sysmsg_offset);
2632 * Check if current process can access given file. amode is a bitmask of *_OK
2633 * access bits. flags is a bitmask of AT_* flags.
2636 kern_access(struct nlookupdata *nd, int amode, int flags)
2641 if (flags & ~AT_EACCESS)
2643 if ((error = nlookup(nd)) != 0)
2646 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2650 /* Flags == 0 means only check for existence. */
2659 if ((mode & VWRITE) == 0 ||
2660 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2661 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2664 * If the file handle is stale we have to re-resolve the
2665 * entry. This is a hack at the moment.
2667 if (error == ESTALE) {
2669 cache_setunresolved(&nd->nl_nch);
2670 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2683 * access_args(char *path, int flags)
2685 * Check access permissions.
2688 sys_access(struct access_args *uap)
2690 struct nlookupdata nd;
2693 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2695 error = kern_access(&nd, uap->flags, 0);
2702 * eaccess_args(char *path, int flags)
2704 * Check access permissions.
2707 sys_eaccess(struct eaccess_args *uap)
2709 struct nlookupdata nd;
2712 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2714 error = kern_access(&nd, uap->flags, AT_EACCESS);
2721 * faccessat_args(int fd, char *path, int amode, int flags)
2723 * Check access permissions.
2726 sys_faccessat(struct faccessat_args *uap)
2728 struct nlookupdata nd;
2732 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2735 error = kern_access(&nd, uap->amode, uap->flags);
2736 nlookup_done_at(&nd, fp);
2742 kern_stat(struct nlookupdata *nd, struct stat *st)
2747 if ((error = nlookup(nd)) != 0)
2750 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2753 if ((error = vget(vp, LK_SHARED)) != 0)
2755 error = vn_stat(vp, st, nd->nl_cred);
2758 * If the file handle is stale we have to re-resolve the entry. This
2759 * is a hack at the moment.
2761 if (error == ESTALE) {
2763 cache_setunresolved(&nd->nl_nch);
2764 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2774 * stat_args(char *path, struct stat *ub)
2776 * Get file status; this version follows links.
2779 sys_stat(struct stat_args *uap)
2781 struct nlookupdata nd;
2785 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2787 error = kern_stat(&nd, &st);
2789 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2796 * lstat_args(char *path, struct stat *ub)
2798 * Get file status; this version does not follow links.
2801 sys_lstat(struct lstat_args *uap)
2803 struct nlookupdata nd;
2807 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2809 error = kern_stat(&nd, &st);
2811 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2818 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2820 * Get status of file pointed to by fd/path.
2823 sys_fstatat(struct fstatat_args *uap)
2825 struct nlookupdata nd;
2831 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2834 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2836 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2837 UIO_USERSPACE, flags);
2839 error = kern_stat(&nd, &st);
2841 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2843 nlookup_done_at(&nd, fp);
2848 * pathconf_Args(char *path, int name)
2850 * Get configurable pathname variables.
2853 sys_pathconf(struct pathconf_args *uap)
2855 struct nlookupdata nd;
2860 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2862 error = nlookup(&nd);
2864 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2867 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2875 * kern_readlink isn't properly split yet. There is a copyin burried
2876 * in VOP_READLINK().
2879 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2881 struct thread *td = curthread;
2887 if ((error = nlookup(nd)) != 0)
2889 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2892 if (vp->v_type != VLNK) {
2895 aiov.iov_base = buf;
2896 aiov.iov_len = count;
2897 auio.uio_iov = &aiov;
2898 auio.uio_iovcnt = 1;
2899 auio.uio_offset = 0;
2900 auio.uio_rw = UIO_READ;
2901 auio.uio_segflg = UIO_USERSPACE;
2903 auio.uio_resid = count;
2904 error = VOP_READLINK(vp, &auio, td->td_ucred);
2907 *res = count - auio.uio_resid;
2912 * readlink_args(char *path, char *buf, int count)
2914 * Return target name of a symbolic link.
2917 sys_readlink(struct readlink_args *uap)
2919 struct nlookupdata nd;
2922 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2924 error = kern_readlink(&nd, uap->buf, uap->count,
2925 &uap->sysmsg_result);
2932 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2934 * Return target name of a symbolic link. The path is relative to the
2935 * directory associated with fd.
2938 sys_readlinkat(struct readlinkat_args *uap)
2940 struct nlookupdata nd;
2944 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2946 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2947 &uap->sysmsg_result);
2949 nlookup_done_at(&nd, fp);
2954 setfflags(struct vnode *vp, int flags)
2956 struct thread *td = curthread;
2961 * Prevent non-root users from setting flags on devices. When
2962 * a device is reused, users can retain ownership of the device
2963 * if they are allowed to set flags and programs assume that
2964 * chown can't fail when done as root.
2966 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2967 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2971 * note: vget is required for any operation that might mod the vnode
2972 * so VINACTIVE is properly cleared.
2974 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2976 vattr.va_flags = flags;
2977 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2984 * chflags(char *path, int flags)
2986 * Change flags of a file given a path name.
2989 sys_chflags(struct chflags_args *uap)
2991 struct nlookupdata nd;
2996 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2998 error = nlookup(&nd);
3000 error = ncp_writechk(&nd.nl_nch);
3002 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3005 error = setfflags(vp, uap->flags);
3012 * lchflags(char *path, int flags)
3014 * Change flags of a file given a path name, but don't follow symlinks.
3017 sys_lchflags(struct lchflags_args *uap)
3019 struct nlookupdata nd;
3024 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3026 error = nlookup(&nd);
3028 error = ncp_writechk(&nd.nl_nch);
3030 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3033 error = setfflags(vp, uap->flags);
3040 * fchflags_args(int fd, int flags)
3042 * Change flags of a file given a file descriptor.
3045 sys_fchflags(struct fchflags_args *uap)
3047 struct thread *td = curthread;
3048 struct proc *p = td->td_proc;
3052 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3054 if (fp->f_nchandle.ncp)
3055 error = ncp_writechk(&fp->f_nchandle);
3057 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3063 setfmode(struct vnode *vp, int mode)
3065 struct thread *td = curthread;
3070 * note: vget is required for any operation that might mod the vnode
3071 * so VINACTIVE is properly cleared.
3073 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3075 vattr.va_mode = mode & ALLPERMS;
3076 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3083 kern_chmod(struct nlookupdata *nd, int mode)
3088 if ((error = nlookup(nd)) != 0)
3090 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3092 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3093 error = setfmode(vp, mode);
3099 * chmod_args(char *path, int mode)
3101 * Change mode of a file given path name.
3104 sys_chmod(struct chmod_args *uap)
3106 struct nlookupdata nd;
3109 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3111 error = kern_chmod(&nd, uap->mode);
3117 * lchmod_args(char *path, int mode)
3119 * Change mode of a file given path name (don't follow links.)
3122 sys_lchmod(struct lchmod_args *uap)
3124 struct nlookupdata nd;
3127 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3129 error = kern_chmod(&nd, uap->mode);
3135 * fchmod_args(int fd, int mode)
3137 * Change mode of a file given a file descriptor.
3140 sys_fchmod(struct fchmod_args *uap)
3142 struct thread *td = curthread;
3143 struct proc *p = td->td_proc;
3147 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3149 if (fp->f_nchandle.ncp)
3150 error = ncp_writechk(&fp->f_nchandle);
3152 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3158 * fchmodat_args(char *path, int mode)
3160 * Change mode of a file pointed to by fd/path.
3163 sys_fchmodat(struct fchmodat_args *uap)
3165 struct nlookupdata nd;
3170 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3172 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3174 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3175 UIO_USERSPACE, flags);
3177 error = kern_chmod(&nd, uap->mode);
3178 nlookup_done_at(&nd, fp);
3183 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3185 struct thread *td = curthread;
3193 * note: vget is required for any operation that might mod the vnode
3194 * so VINACTIVE is properly cleared.
3196 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3197 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3199 o_uid = vattr.va_uid;
3200 o_gid = vattr.va_gid;
3201 size = vattr.va_size;
3206 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3215 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3216 VFS_ACCOUNT(mp, uid, gid, size);
3223 kern_chown(struct nlookupdata *nd, int uid, int gid)
3228 if ((error = nlookup(nd)) != 0)
3230 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3232 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3233 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3239 * chown(char *path, int uid, int gid)
3241 * Set ownership given a path name.
3244 sys_chown(struct chown_args *uap)
3246 struct nlookupdata nd;
3249 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3251 error = kern_chown(&nd, uap->uid, uap->gid);
3257 * lchown_args(char *path, int uid, int gid)
3259 * Set ownership given a path name, do not cross symlinks.
3262 sys_lchown(struct lchown_args *uap)
3264 struct nlookupdata nd;
3267 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3269 error = kern_chown(&nd, uap->uid, uap->gid);
3275 * fchown_args(int fd, int uid, int gid)
3277 * Set ownership given a file descriptor.
3280 sys_fchown(struct fchown_args *uap)
3282 struct thread *td = curthread;
3283 struct proc *p = td->td_proc;
3287 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3289 if (fp->f_nchandle.ncp)
3290 error = ncp_writechk(&fp->f_nchandle);
3292 error = setfown(p->p_fd->fd_ncdir.mount,
3293 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3299 * fchownat(int fd, char *path, int uid, int gid, int flags)
3301 * Set ownership of file pointed to by fd/path.
3304 sys_fchownat(struct fchownat_args *uap)
3306 struct nlookupdata nd;
3311 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3313 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3315 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3316 UIO_USERSPACE, flags);
3318 error = kern_chown(&nd, uap->uid, uap->gid);
3319 nlookup_done_at(&nd, fp);
3325 getutimes(const struct timeval *tvp, struct timespec *tsp)
3327 struct timeval tv[2];
3331 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3334 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3335 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3341 setutimes(struct vnode *vp, struct vattr *vattr,
3342 const struct timespec *ts, int nullflag)
3344 struct thread *td = curthread;
3348 vattr->va_atime = ts[0];
3349 vattr->va_mtime = ts[1];
3351 vattr->va_vaflags |= VA_UTIMES_NULL;
3352 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3358 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3360 struct timespec ts[2];
3365 if ((error = getutimes(tptr, ts)) != 0)
3369 * NOTE: utimes() succeeds for the owner even if the file
3370 * is not user-writable.
3372 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3374 if ((error = nlookup(nd)) != 0)
3376 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3378 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3382 * note: vget is required for any operation that might mod the vnode
3383 * so VINACTIVE is properly cleared.
3385 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3386 error = vget(vp, LK_EXCLUSIVE);
3388 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3397 * utimes_args(char *path, struct timeval *tptr)
3399 * Set the access and modification times of a file.
3402 sys_utimes(struct utimes_args *uap)
3404 struct timeval tv[2];
3405 struct nlookupdata nd;
3409 error = copyin(uap->tptr, tv, sizeof(tv));
3413 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3415 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3421 * lutimes_args(char *path, struct timeval *tptr)
3423 * Set the access and modification times of a file.
3426 sys_lutimes(struct lutimes_args *uap)
3428 struct timeval tv[2];
3429 struct nlookupdata nd;
3433 error = copyin(uap->tptr, tv, sizeof(tv));
3437 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3439 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3445 * Set utimes on a file descriptor. The creds used to open the
3446 * file are used to determine whether the operation is allowed
3450 kern_futimes(int fd, struct timeval *tptr)
3452 struct thread *td = curthread;
3453 struct proc *p = td->td_proc;
3454 struct timespec ts[2];
3460 error = getutimes(tptr, ts);
3463 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3465 if (fp->f_nchandle.ncp)
3466 error = ncp_writechk(&fp->f_nchandle);
3469 error = vget(vp, LK_EXCLUSIVE);
3471 error = VOP_GETATTR(vp, &vattr);
3473 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3477 error = setutimes(vp, &vattr, ts,
3488 * futimes_args(int fd, struct timeval *tptr)
3490 * Set the access and modification times of a file.
3493 sys_futimes(struct futimes_args *uap)
3495 struct timeval tv[2];
3499 error = copyin(uap->tptr, tv, sizeof(tv));
3503 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3509 kern_truncate(struct nlookupdata *nd, off_t length)
3516 uint64_t old_size = 0;
3520 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3521 if ((error = nlookup(nd)) != 0)
3523 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3525 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3527 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3531 if (vp->v_type == VDIR) {
3535 if (vfs_quota_enabled) {
3536 error = VOP_GETATTR(vp, &vattr);
3537 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3540 old_size = vattr.va_size;
3543 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3545 vattr.va_size = length;
3546 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3547 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3555 * truncate(char *path, int pad, off_t length)
3557 * Truncate a file given its path name.
3560 sys_truncate(struct truncate_args *uap)
3562 struct nlookupdata nd;
3565 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3567 error = kern_truncate(&nd, uap->length);
3573 kern_ftruncate(int fd, off_t length)
3575 struct thread *td = curthread;
3576 struct proc *p = td->td_proc;
3583 uint64_t old_size = 0;
3588 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3590 if (fp->f_nchandle.ncp) {
3591 error = ncp_writechk(&fp->f_nchandle);
3595 if ((fp->f_flag & FWRITE) == 0) {
3599 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3603 vp = (struct vnode *)fp->f_data;
3604 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3605 if (vp->v_type == VDIR) {
3610 if (vfs_quota_enabled) {
3611 error = VOP_GETATTR(vp, &vattr);
3612 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3615 old_size = vattr.va_size;
3618 if ((error = vn_writechk(vp, NULL)) == 0) {
3620 vattr.va_size = length;
3621 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3623 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3632 * ftruncate_args(int fd, int pad, off_t length)
3634 * Truncate a file given a file descriptor.
3637 sys_ftruncate(struct ftruncate_args *uap)
3641 error = kern_ftruncate(uap->fd, uap->length);
3649 * Sync an open file.
3652 sys_fsync(struct fsync_args *uap)
3654 struct thread *td = curthread;
3655 struct proc *p = td->td_proc;
3661 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3663 vp = (struct vnode *)fp->f_data;
3664 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3665 if ((obj = vp->v_object) != NULL)
3666 vm_object_page_clean(obj, 0, 0, 0);
3667 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3668 if (error == 0 && vp->v_mount)
3669 error = buf_fsync(vp);
3677 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3679 struct nchandle fnchd;
3680 struct nchandle tnchd;
3681 struct namecache *ncp;
3688 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3689 if ((error = nlookup(fromnd)) != 0)
3691 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3693 fnchd.mount = fromnd->nl_nch.mount;
3697 * unlock the source nch so we can lookup the target nch without
3698 * deadlocking. The target may or may not exist so we do not check
3699 * for a target vp like kern_mkdir() and other creation functions do.
3701 * The source and target directories are ref'd and rechecked after
3702 * everything is relocked to determine if the source or target file
3705 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3706 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3707 cache_unlock(&fromnd->nl_nch);
3709 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3710 if ((error = nlookup(tond)) != 0) {
3714 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3718 tnchd.mount = tond->nl_nch.mount;
3722 * If the source and target are the same there is nothing to do
3724 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3731 * Mount points cannot be renamed or overwritten
3733 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3742 * Relock the source ncp. cache_relock() will deal with any
3743 * deadlocks against the already-locked tond and will also
3744 * make sure both are resolved.
3746 * NOTE AFTER RELOCKING: The source or target ncp may have become
3747 * invalid while they were unlocked, nc_vp and nc_mount could
3750 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3751 &tond->nl_nch, tond->nl_cred);
3752 fromnd->nl_flags |= NLC_NCPISLOCKED;
3755 * If either fromnd or tond are marked destroyed a ripout occured
3756 * out from under us and we must retry.
3758 if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
3759 fromnd->nl_nch.ncp->nc_vp == NULL ||
3760 (tond->nl_nch.ncp->nc_flag & NCF_DESTROYED)) {
3761 kprintf("kern_rename: retry due to ripout on: "
3762 "\"%s\" -> \"%s\"\n",
3763 fromnd->nl_nch.ncp->nc_name,
3764 tond->nl_nch.ncp->nc_name);
3771 * make sure the parent directories linkages are the same
3773 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3774 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3781 * Both the source and target must be within the same filesystem and
3782 * in the same filesystem as their parent directories within the
3783 * namecache topology.
3785 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3788 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3789 mp != tond->nl_nch.mount) {
3796 * Make sure the mount point is writable
3798 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3805 * If the target exists and either the source or target is a directory,
3806 * then both must be directories.
3808 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3811 if (tond->nl_nch.ncp->nc_vp) {
3812 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3814 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3815 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3817 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3823 * You cannot rename a source into itself or a subdirectory of itself.
3824 * We check this by travsersing the target directory upwards looking
3825 * for a match against the source.
3830 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3831 if (fromnd->nl_nch.ncp == ncp) {
3842 * Even though the namespaces are different, they may still represent
3843 * hardlinks to the same file. The filesystem might have a hard time
3844 * with this so we issue a NREMOVE of the source instead of a NRENAME
3845 * when we detect the situation.
3848 fdvp = fromnd->nl_dvp;
3849 tdvp = tond->nl_dvp;
3850 if (fdvp == NULL || tdvp == NULL) {
3852 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3853 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3856 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3857 fdvp, tdvp, tond->nl_cred);
3864 * rename_args(char *from, char *to)
3866 * Rename files. Source and destination must either both be directories,
3867 * or both not be directories. If target is a directory, it must be empty.
3870 sys_rename(struct rename_args *uap)
3872 struct nlookupdata fromnd, tond;
3876 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3878 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3880 error = kern_rename(&fromnd, &tond);
3881 nlookup_done(&tond);
3883 nlookup_done(&fromnd);
3884 } while (error == EAGAIN);
3889 * renameat_args(int oldfd, char *old, int newfd, char *new)
3891 * Rename files using paths relative to the directories associated with
3892 * oldfd and newfd. Source and destination must either both be directories,
3893 * or both not be directories. If target is a directory, it must be empty.
3896 sys_renameat(struct renameat_args *uap)
3898 struct nlookupdata oldnd, newnd;
3899 struct file *oldfp, *newfp;
3903 error = nlookup_init_at(&oldnd, &oldfp,
3904 uap->oldfd, uap->old,
3907 error = nlookup_init_at(&newnd, &newfp,
3908 uap->newfd, uap->new,
3911 error = kern_rename(&oldnd, &newnd);
3912 nlookup_done_at(&newnd, newfp);
3914 nlookup_done_at(&oldnd, oldfp);
3915 } while (error == EAGAIN);
3920 kern_mkdir(struct nlookupdata *nd, int mode)
3922 struct thread *td = curthread;
3923 struct proc *p = td->td_proc;
3929 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3930 if ((error = nlookup(nd)) != 0)
3933 if (nd->nl_nch.ncp->nc_vp)
3935 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3938 vattr.va_type = VDIR;
3939 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3942 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3949 * mkdir_args(char *path, int mode)
3951 * Make a directory file.
3954 sys_mkdir(struct mkdir_args *uap)
3956 struct nlookupdata nd;
3959 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3961 error = kern_mkdir(&nd, uap->mode);
3967 * mkdirat_args(int fd, char *path, mode_t mode)
3969 * Make a directory file. The path is relative to the directory associated
3973 sys_mkdirat(struct mkdirat_args *uap)
3975 struct nlookupdata nd;
3979 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3981 error = kern_mkdir(&nd, uap->mode);
3982 nlookup_done_at(&nd, fp);
3987 kern_rmdir(struct nlookupdata *nd)
3992 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3993 if ((error = nlookup(nd)) != 0)
3997 * Do not allow directories representing mount points to be
3998 * deleted, even if empty. Check write perms on mount point
3999 * in case the vnode is aliased (aka nullfs).
4001 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4003 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4005 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4010 * rmdir_args(char *path)
4012 * Remove a directory file.
4015 sys_rmdir(struct rmdir_args *uap)
4017 struct nlookupdata nd;
4020 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4022 error = kern_rmdir(&nd);
4028 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4029 enum uio_seg direction)
4031 struct thread *td = curthread;
4032 struct proc *p = td->td_proc;
4040 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
4042 if ((fp->f_flag & FREAD) == 0) {
4046 vp = (struct vnode *)fp->f_data;
4048 if (vp->v_type != VDIR) {
4052 aiov.iov_base = buf;
4053 aiov.iov_len = count;
4054 auio.uio_iov = &aiov;
4055 auio.uio_iovcnt = 1;
4056 auio.uio_rw = UIO_READ;
4057 auio.uio_segflg = direction;
4059 auio.uio_resid = count;
4060 loff = auio.uio_offset = fp->f_offset;
4061 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
4062 fp->f_offset = auio.uio_offset;
4065 if (count == auio.uio_resid) {
4066 if (union_dircheckp) {
4067 error = union_dircheckp(td, &vp, fp);
4074 if ((vp->v_flag & VROOT) &&
4075 (vp->v_mount->mnt_flag & MNT_UNION)) {
4076 struct vnode *tvp = vp;
4077 vp = vp->v_mount->mnt_vnodecovered;
4088 * WARNING! *basep may not be wide enough to accomodate the
4089 * seek offset. XXX should we hack this to return the upper 32 bits
4090 * for offsets greater then 4G?
4093 *basep = (long)loff;
4095 *res = count - auio.uio_resid;
4102 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4104 * Read a block of directory entries in a file system independent format.
4107 sys_getdirentries(struct getdirentries_args *uap)
4112 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4113 &uap->sysmsg_result, UIO_USERSPACE);
4115 if (error == 0 && uap->basep)
4116 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4121 * getdents_args(int fd, char *buf, size_t count)
4124 sys_getdents(struct getdents_args *uap)
4128 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4129 &uap->sysmsg_result, UIO_USERSPACE);
4135 * Set the mode mask for creation of filesystem nodes.
4137 * umask(int newmask)
4140 sys_umask(struct umask_args *uap)
4142 struct thread *td = curthread;
4143 struct proc *p = td->td_proc;
4144 struct filedesc *fdp;
4147 uap->sysmsg_result = fdp->fd_cmask;
4148 fdp->fd_cmask = uap->newmask & ALLPERMS;
4153 * revoke(char *path)
4155 * Void all references to file by ripping underlying filesystem
4159 sys_revoke(struct revoke_args *uap)
4161 struct nlookupdata nd;
4168 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4170 error = nlookup(&nd);
4172 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4173 cred = crhold(nd.nl_cred);
4177 error = VOP_GETATTR(vp, &vattr);
4178 if (error == 0 && cred->cr_uid != vattr.va_uid)
4179 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4180 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4182 error = vrevoke(vp, cred);
4183 } else if (error == 0) {
4184 error = vrevoke(vp, cred);
4194 * getfh_args(char *fname, fhandle_t *fhp)
4196 * Get (NFS) file handle
4198 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4199 * mount. This allows nullfs mounts to be explicitly exported.
4201 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4203 * nullfs mounts of subdirectories are not safe. That is, it will
4204 * work, but you do not really have protection against access to
4205 * the related parent directories.
4208 sys_getfh(struct getfh_args *uap)
4210 struct thread *td = curthread;
4211 struct nlookupdata nd;
4218 * Must be super user
4220 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4224 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4226 error = nlookup(&nd);
4228 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4229 mp = nd.nl_nch.mount;
4232 bzero(&fh, sizeof(fh));
4233 fh.fh_fsid = mp->mnt_stat.f_fsid;
4234 error = VFS_VPTOFH(vp, &fh.fh_fid);
4237 error = copyout(&fh, uap->fhp, sizeof(fh));
4243 * fhopen_args(const struct fhandle *u_fhp, int flags)
4245 * syscall for the rpc.lockd to use to translate a NFS file handle into
4246 * an open descriptor.
4248 * warning: do not remove the priv_check() call or this becomes one giant
4252 sys_fhopen(struct fhopen_args *uap)
4254 struct thread *td = curthread;
4255 struct filedesc *fdp = td->td_proc->p_fd;
4260 struct vattr *vap = &vat;
4262 int fmode, mode, error = 0, type;
4268 * Must be super user
4270 error = priv_check(td, PRIV_ROOT);
4274 fmode = FFLAGS(uap->flags);
4277 * Why not allow a non-read/write open for our lockd?
4279 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4281 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4286 * Find the mount point
4288 mp = vfs_getvfs(&fhp.fh_fsid);
4293 /* now give me my vnode, it gets returned to me locked */
4294 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4298 * from now on we have to make sure not
4299 * to forget about the vnode
4300 * any error that causes an abort must vput(vp)
4301 * just set error = err and 'goto bad;'.
4307 if (vp->v_type == VLNK) {
4311 if (vp->v_type == VSOCK) {
4316 if (fmode & (FWRITE | O_TRUNC)) {
4317 if (vp->v_type == VDIR) {
4321 error = vn_writechk(vp, NULL);
4329 error = VOP_ACCESS(vp, mode, td->td_ucred);
4333 if (fmode & O_TRUNC) {
4334 vn_unlock(vp); /* XXX */
4335 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4338 error = VOP_SETATTR(vp, vap, td->td_ucred);
4344 * VOP_OPEN needs the file pointer so it can potentially override
4347 * WARNING! no f_nchandle will be associated when fhopen()ing a
4350 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4354 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4357 * setting f_ops this way prevents VOP_CLOSE from being
4358 * called or fdrop() releasing the vp from v_data. Since
4359 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4361 fp->f_ops = &badfileops;
4367 * The fp is given its own reference, we still have our ref and lock.
4369 * Assert that all regular files must be created with a VM object.
4371 if (vp->v_type == VREG && vp->v_object == NULL) {
4372 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4377 * The open was successful. Handle any locking requirements.
4379 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4380 lf.l_whence = SEEK_SET;
4383 if (fmode & O_EXLOCK)
4384 lf.l_type = F_WRLCK;
4386 lf.l_type = F_RDLCK;
4387 if (fmode & FNONBLOCK)
4392 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4394 * release our private reference.
4396 fsetfd(fdp, NULL, indx);
4401 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4402 fp->f_flag |= FHASLOCK;
4406 * Clean up. Associate the file pointer with the previously
4407 * reserved descriptor and return it.
4410 fsetfd(fdp, fp, indx);
4412 uap->sysmsg_result = indx;
4413 if (uap->flags & O_CLOEXEC)
4414 error = fsetfdflags(fdp, indx, UF_EXCLOSE);
4418 fsetfd(fdp, NULL, indx);
4427 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4430 sys_fhstat(struct fhstat_args *uap)
4432 struct thread *td = curthread;
4440 * Must be super user
4442 error = priv_check(td, PRIV_ROOT);
4446 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4450 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4453 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4454 error = vn_stat(vp, &sb, td->td_ucred);
4459 error = copyout(&sb, uap->sb, sizeof(sb));
4464 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4467 sys_fhstatfs(struct fhstatfs_args *uap)
4469 struct thread *td = curthread;
4470 struct proc *p = td->td_proc;
4475 char *fullpath, *freepath;
4480 * Must be super user
4482 if ((error = priv_check(td, PRIV_ROOT)))
4485 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4488 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4492 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4497 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4502 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4505 error = mount_path(p, mp, &fullpath, &freepath);
4508 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4509 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4510 kfree(freepath, M_TEMP);
4512 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4513 if (priv_check(td, PRIV_ROOT)) {
4514 bcopy(sp, &sb, sizeof(sb));
4515 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4518 error = copyout(sp, uap->buf, sizeof(*sp));
4524 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4527 sys_fhstatvfs(struct fhstatvfs_args *uap)
4529 struct thread *td = curthread;
4530 struct proc *p = td->td_proc;
4538 * Must be super user
4540 if ((error = priv_check(td, PRIV_ROOT)))
4543 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4546 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4550 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4555 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4558 sp = &mp->mnt_vstat;
4560 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4564 if (mp->mnt_flag & MNT_RDONLY)
4565 sp->f_flag |= ST_RDONLY;
4566 if (mp->mnt_flag & MNT_NOSUID)
4567 sp->f_flag |= ST_NOSUID;
4568 error = copyout(sp, uap->buf, sizeof(*sp));
4575 * Syscall to push extended attribute configuration information into the
4576 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4577 * a command (int cmd), and attribute name and misc data. For now, the
4578 * attribute name is left in userspace for consumption by the VFS_op.
4579 * It will probably be changed to be copied into sysspace by the
4580 * syscall in the future, once issues with various consumers of the
4581 * attribute code have raised their hands.
4583 * Currently this is used only by UFS Extended Attributes.
4586 sys_extattrctl(struct extattrctl_args *uap)
4588 struct nlookupdata nd;
4590 char attrname[EXTATTR_MAXNAMELEN];
4598 if (error == 0 && uap->filename) {
4599 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4602 error = nlookup(&nd);
4604 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4608 if (error == 0 && uap->attrname) {
4609 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4614 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4616 error = nlookup(&nd);
4618 error = ncp_writechk(&nd.nl_nch);
4620 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4622 uap->attrname, nd.nl_cred);
4631 * Syscall to get a named extended attribute on a file or directory.
4634 sys_extattr_set_file(struct extattr_set_file_args *uap)
4636 char attrname[EXTATTR_MAXNAMELEN];
4637 struct nlookupdata nd;
4643 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4649 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4651 error = nlookup(&nd);
4653 error = ncp_writechk(&nd.nl_nch);
4655 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4661 bzero(&auio, sizeof(auio));
4662 aiov.iov_base = uap->data;
4663 aiov.iov_len = uap->nbytes;
4664 auio.uio_iov = &aiov;
4665 auio.uio_iovcnt = 1;
4666 auio.uio_offset = 0;
4667 auio.uio_resid = uap->nbytes;
4668 auio.uio_rw = UIO_WRITE;
4669 auio.uio_td = curthread;
4671 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4680 * Syscall to get a named extended attribute on a file or directory.
4683 sys_extattr_get_file(struct extattr_get_file_args *uap)
4685 char attrname[EXTATTR_MAXNAMELEN];
4686 struct nlookupdata nd;
4692 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4698 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4700 error = nlookup(&nd);
4702 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4708 bzero(&auio, sizeof(auio));
4709 aiov.iov_base = uap->data;
4710 aiov.iov_len = uap->nbytes;
4711 auio.uio_iov = &aiov;
4712 auio.uio_iovcnt = 1;
4713 auio.uio_offset = 0;
4714 auio.uio_resid = uap->nbytes;
4715 auio.uio_rw = UIO_READ;
4716 auio.uio_td = curthread;
4718 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4720 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4728 * Syscall to delete a named extended attribute from a file or directory.
4729 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4732 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4734 char attrname[EXTATTR_MAXNAMELEN];
4735 struct nlookupdata nd;
4739 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4743 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4745 error = nlookup(&nd);
4747 error = ncp_writechk(&nd.nl_nch);
4749 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4751 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4752 attrname, NULL, nd.nl_cred);
4761 * Determine if the mount is visible to the process.
4764 chroot_visible_mnt(struct mount *mp, struct proc *p)
4766 struct nchandle nch;
4769 * Traverse from the mount point upwards. If we hit the process
4770 * root then the mount point is visible to the process.
4772 nch = mp->mnt_ncmountpt;
4774 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4775 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4778 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4779 nch = nch.mount->mnt_ncmounton;
4781 nch.ncp = nch.ncp->nc_parent;
4786 * If the mount point is not visible to the process, but the
4787 * process root is in a subdirectory of the mount, return
4790 if (p->p_fd->fd_nrdir.mount == mp)