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];
137 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
141 * Do not allow NFS export by non-root users.
143 if (uap->flags & MNT_EXPORTED) {
144 error = priv_check(td, PRIV_ROOT);
149 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
151 if (priv_check(td, PRIV_ROOT))
152 uap->flags |= MNT_NOSUID | MNT_NODEV;
155 * Lookup the requested path and extract the nch and vnode.
157 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
159 if ((error = nlookup(&nd)) == 0) {
160 if (nd.nl_nch.ncp->nc_vp == NULL)
170 * If the target filesystem is resolved via a nullfs mount, then
171 * nd.nl_nch.mount will be pointing to the nullfs mount structure
172 * instead of the target file system. We need it in case we are
175 nullmp = nd.nl_nch.mount;
178 * Extract the locked+refd ncp and cleanup the nd structure
181 cache_zero(&nd.nl_nch);
184 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
185 (mp = cache_findmount(&nch)) != NULL) {
194 * now we have the locked ref'd nch and unreferenced vnode.
197 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
204 * Extract the file system type. We need to know this early, to take
205 * appropriate actions if we are dealing with a nullfs.
207 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
214 * Now we have an unlocked ref'd nch and a locked ref'd vp
216 if (uap->flags & MNT_UPDATE) {
217 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
224 if (strncmp(fstypename, "null", 5) == 0) {
232 flag2 = mp->mnt_kern_flag;
234 * We only allow the filesystem to be reloaded if it
235 * is currently mounted read-only.
237 if ((uap->flags & MNT_RELOAD) &&
238 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
241 error = EOPNOTSUPP; /* Needs translation */
245 * Only root, or the user that did the original mount is
246 * permitted to update it.
248 if (mp->mnt_stat.f_owner != cred->cr_uid &&
249 (error = priv_check(td, PRIV_ROOT))) {
254 if (vfs_busy(mp, LK_NOWAIT)) {
260 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
267 vsetflags(vp, VMOUNT);
269 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
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 && (error = priv_check(td, PRIV_ROOT)))) {
283 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
288 if (vp->v_type != VDIR) {
294 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
300 vfsp = vfsconf_find_by_name(fstypename);
304 /* Only load modules for root (very important!) */
305 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
310 error = linker_load_file(fstypename, &lf);
311 if (error || lf == NULL) {
319 /* lookup again, see if the VFS was loaded */
320 vfsp = vfsconf_find_by_name(fstypename);
323 linker_file_unload(lf);
330 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
336 vsetflags(vp, VMOUNT);
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;
354 * Set the mount level flags.
356 if (uap->flags & MNT_RDONLY)
357 mp->mnt_flag |= MNT_RDONLY;
358 else if (mp->mnt_flag & MNT_RDONLY)
359 mp->mnt_kern_flag |= MNTK_WANTRDWR;
360 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
361 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
362 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
363 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
364 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
365 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
366 MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
367 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
369 * Mount the filesystem.
370 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
373 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
374 if (mp->mnt_flag & MNT_UPDATE) {
375 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
376 mp->mnt_flag &= ~MNT_RDONLY;
377 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
378 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
381 mp->mnt_kern_flag = flag2;
384 vclrflags(vp, VMOUNT);
389 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
391 * Put the new filesystem on the mount list after root. The mount
392 * point gets its own mnt_ncmountpt (unless the VFS already set one
393 * up) which represents the root of the mount. The lookup code
394 * detects the mount point going forward and checks the root of
395 * the mount going backwards.
397 * It is not necessary to invalidate or purge the vnode underneath
398 * because elements under the mount will be given their own glue
402 if (mp->mnt_ncmountpt.ncp == NULL) {
404 * allocate, then unlock, but leave the ref intact
406 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
407 cache_unlock(&mp->mnt_ncmountpt);
409 mp->mnt_ncmounton = nch; /* inherits ref */
410 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
412 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
413 vclrflags(vp, VMOUNT);
414 mountlist_insert(mp, MNTINS_LAST);
416 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
417 error = vfs_allocate_syncvnode(mp);
419 error = VFS_START(mp, 0);
422 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
423 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
424 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
425 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
426 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
427 vclrflags(vp, VMOUNT);
428 mp->mnt_vfc->vfc_refcount--;
440 * Scan all active processes to see if any of them have a current
441 * or root directory onto which the new filesystem has just been
442 * mounted. If so, replace them with the new mount point.
444 * The passed ncp is ref'd and locked (from the mount code) and
445 * must be associated with the vnode representing the root of the
448 struct checkdirs_info {
449 struct nchandle old_nch;
450 struct nchandle new_nch;
451 struct vnode *old_vp;
452 struct vnode *new_vp;
455 static int checkdirs_callback(struct proc *p, void *data);
458 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
460 struct checkdirs_info info;
466 * If the old mount point's vnode has a usecount of 1, it is not
467 * being held as a descriptor anywhere.
469 olddp = old_nch->ncp->nc_vp;
470 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
474 * Force the root vnode of the new mount point to be resolved
475 * so we can update any matching processes.
478 if (VFS_ROOT(mp, &newdp))
479 panic("mount: lost mount");
480 cache_setunresolved(new_nch);
481 cache_setvp(new_nch, newdp);
484 * Special handling of the root node
486 if (rootvnode == olddp) {
488 vfs_cache_setroot(newdp, cache_hold(new_nch));
492 * Pass newdp separately so the callback does not have to access
493 * it via new_nch->ncp->nc_vp.
495 info.old_nch = *old_nch;
496 info.new_nch = *new_nch;
498 allproc_scan(checkdirs_callback, &info);
503 * NOTE: callback is not MP safe because the scanned process's filedesc
504 * structure can be ripped out from under us, amoung other things.
507 checkdirs_callback(struct proc *p, void *data)
509 struct checkdirs_info *info = data;
510 struct filedesc *fdp;
511 struct nchandle ncdrop1;
512 struct nchandle ncdrop2;
513 struct vnode *vprele1;
514 struct vnode *vprele2;
516 if ((fdp = p->p_fd) != NULL) {
517 cache_zero(&ncdrop1);
518 cache_zero(&ncdrop2);
523 * MPUNSAFE - XXX fdp can be pulled out from under a
526 * A shared filedesc is ok, we don't have to copy it
527 * because we are making this change globally.
529 spin_lock(&fdp->fd_spin);
530 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
531 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
532 vprele1 = fdp->fd_cdir;
534 fdp->fd_cdir = info->new_vp;
535 ncdrop1 = fdp->fd_ncdir;
536 cache_copy(&info->new_nch, &fdp->fd_ncdir);
538 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
539 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
540 vprele2 = fdp->fd_rdir;
542 fdp->fd_rdir = info->new_vp;
543 ncdrop2 = fdp->fd_nrdir;
544 cache_copy(&info->new_nch, &fdp->fd_nrdir);
546 spin_unlock(&fdp->fd_spin);
548 cache_drop(&ncdrop1);
550 cache_drop(&ncdrop2);
560 * Unmount a file system.
562 * Note: unmount takes a path to the vnode mounted on as argument,
563 * not special file (as before).
565 * umount_args(char *path, int flags)
570 sys_unmount(struct unmount_args *uap)
572 struct thread *td = curthread;
573 struct proc *p __debugvar = td->td_proc;
574 struct mount *mp = NULL;
575 struct nlookupdata nd;
580 if (td->td_ucred->cr_prison != NULL) {
584 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
587 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
589 error = nlookup(&nd);
593 mp = nd.nl_nch.mount;
596 * Only root, or the user that did the original mount is
597 * permitted to unmount this filesystem.
599 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
600 (error = priv_check(td, PRIV_ROOT)))
604 * Don't allow unmounting the root file system.
606 if (mp->mnt_flag & MNT_ROOTFS) {
612 * Must be the root of the filesystem
614 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
622 error = dounmount(mp, uap->flags);
629 * Do the actual file system unmount.
632 dounmount_interlock(struct mount *mp)
634 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
636 mp->mnt_kern_flag |= MNTK_UNMOUNT;
641 unmount_allproc_cb(struct proc *p, void *arg)
645 if (p->p_textnch.ncp == NULL)
648 mp = (struct mount *)arg;
649 if (p->p_textnch.mount == mp)
650 cache_drop(&p->p_textnch);
656 dounmount(struct mount *mp, int flags)
658 struct namecache *ncp;
667 * Exclusive access for unmounting purposes
669 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
673 * Allow filesystems to detect that a forced unmount is in progress.
675 if (flags & MNT_FORCE)
676 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
677 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
678 error = lockmgr(&mp->mnt_lock, lflags);
680 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
681 if (mp->mnt_kern_flag & MNTK_MWAIT)
686 if (mp->mnt_flag & MNT_EXPUBLIC)
687 vfs_setpublicfs(NULL, NULL, NULL);
689 vfs_msync(mp, MNT_WAIT);
690 async_flag = mp->mnt_flag & MNT_ASYNC;
691 mp->mnt_flag &=~ MNT_ASYNC;
694 * If this filesystem isn't aliasing other filesystems,
695 * try to invalidate any remaining namecache entries and
696 * check the count afterwords.
698 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
699 cache_lock(&mp->mnt_ncmountpt);
700 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
701 cache_unlock(&mp->mnt_ncmountpt);
703 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
704 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
705 allproc_scan(&unmount_allproc_cb, mp);
708 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
709 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
711 if ((flags & MNT_FORCE) == 0) {
713 mount_warning(mp, "Cannot unmount: "
719 mount_warning(mp, "Forced unmount: "
730 * nchandle records ref the mount structure. Expect a count of 1
731 * (our mount->mnt_ncmountpt).
733 if (mp->mnt_refs != 1) {
734 if ((flags & MNT_FORCE) == 0) {
735 mount_warning(mp, "Cannot unmount: "
736 "%d process references still "
737 "present", mp->mnt_refs);
740 mount_warning(mp, "Forced unmount: "
741 "%d process references still "
742 "present", mp->mnt_refs);
748 * Decomission our special mnt_syncer vnode. This also stops
749 * the vnlru code. If we are unable to unmount we recommission
753 if ((vp = mp->mnt_syncer) != NULL) {
754 mp->mnt_syncer = NULL;
757 if (((mp->mnt_flag & MNT_RDONLY) ||
758 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
759 (flags & MNT_FORCE)) {
760 error = VFS_UNMOUNT(mp, flags);
764 if (mp->mnt_syncer == NULL)
765 vfs_allocate_syncvnode(mp);
766 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
767 mp->mnt_flag |= async_flag;
768 lockmgr(&mp->mnt_lock, LK_RELEASE);
769 if (mp->mnt_kern_flag & MNTK_MWAIT)
774 * Clean up any journals still associated with the mount after
775 * filesystem activity has ceased.
777 journal_remove_all_journals(mp,
778 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
780 mountlist_remove(mp);
783 * Remove any installed vnode ops here so the individual VFSs don't
786 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
787 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
788 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
789 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
790 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
792 if (mp->mnt_ncmountpt.ncp != NULL) {
793 nch = mp->mnt_ncmountpt;
794 cache_zero(&mp->mnt_ncmountpt);
795 cache_clrmountpt(&nch);
798 if (mp->mnt_ncmounton.ncp != NULL) {
799 nch = mp->mnt_ncmounton;
800 cache_zero(&mp->mnt_ncmounton);
801 cache_clrmountpt(&nch);
805 mp->mnt_vfc->vfc_refcount--;
806 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
807 panic("unmount: dangling vnode");
808 lockmgr(&mp->mnt_lock, LK_RELEASE);
809 if (mp->mnt_kern_flag & MNTK_MWAIT)
818 mount_warning(struct mount *mp, const char *ctl, ...)
825 if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
826 &ptr, &buf, 0) == 0) {
827 kprintf("unmount(%s): ", ptr);
832 kprintf("unmount(%p", mp);
833 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
834 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
843 * Shim cache_fullpath() to handle the case where a process is chrooted into
844 * a subdirectory of a mount. In this case if the root mount matches the
845 * process root directory's mount we have to specify the process's root
846 * directory instead of the mount point, because the mount point might
847 * be above the root directory.
851 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
853 struct nchandle *nch;
855 if (p && p->p_fd->fd_nrdir.mount == mp)
856 nch = &p->p_fd->fd_nrdir;
858 nch = &mp->mnt_ncmountpt;
859 return(cache_fullpath(p, nch, NULL, rb, fb, 0));
863 * Sync each mounted filesystem.
867 static int syncprt = 0;
868 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
871 static int sync_callback(struct mount *mp, void *data);
874 sys_sync(struct sync_args *uap)
876 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
879 * print out buffer pool stat information on each sync() call.
889 sync_callback(struct mount *mp, void *data __unused)
893 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
894 asyncflag = mp->mnt_flag & MNT_ASYNC;
895 mp->mnt_flag &= ~MNT_ASYNC;
896 vfs_msync(mp, MNT_NOWAIT);
897 VFS_SYNC(mp, MNT_NOWAIT);
898 mp->mnt_flag |= asyncflag;
903 /* XXX PRISON: could be per prison flag */
904 static int prison_quotas;
906 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
910 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
912 * Change filesystem quotas.
917 sys_quotactl(struct quotactl_args *uap)
919 struct nlookupdata nd;
926 if (td->td_ucred->cr_prison && !prison_quotas) {
931 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
933 error = nlookup(&nd);
935 mp = nd.nl_nch.mount;
936 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
937 uap->arg, nd.nl_cred);
946 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
947 * void *buf, int buflen)
949 * This function operates on a mount point and executes the specified
950 * operation using the specified control data, and possibly returns data.
952 * The actual number of bytes stored in the result buffer is returned, 0
953 * if none, otherwise an error is returned.
958 sys_mountctl(struct mountctl_args *uap)
960 struct thread *td = curthread;
961 struct proc *p = td->td_proc;
969 * Sanity and permissions checks. We must be root.
972 if (td->td_ucred->cr_prison != NULL)
974 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
975 (error = priv_check(td, PRIV_ROOT)) != 0)
979 * Argument length checks
981 if (uap->ctllen < 0 || uap->ctllen > 1024)
983 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
985 if (uap->path == NULL)
989 * Allocate the necessary buffers and copyin data
991 path = objcache_get(namei_oc, M_WAITOK);
992 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
997 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
998 error = copyin(uap->ctl, ctl, uap->ctllen);
1003 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1006 * Validate the descriptor
1009 fp = holdfp(p->p_fd, uap->fd, -1);
1019 * Execute the internal kernel function and clean up.
1022 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1026 if (error == 0 && uap->sysmsg_result > 0)
1027 error = copyout(buf, uap->buf, uap->sysmsg_result);
1030 objcache_put(namei_oc, path);
1039 * Execute a mount control operation by resolving the path to a mount point
1040 * and calling vop_mountctl().
1042 * Use the mount point from the nch instead of the vnode so nullfs mounts
1043 * can properly spike the VOP.
1046 kern_mountctl(const char *path, int op, struct file *fp,
1047 const void *ctl, int ctllen,
1048 void *buf, int buflen, int *res)
1052 struct nlookupdata nd;
1057 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1059 error = nlookup(&nd);
1061 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1062 mp = nd.nl_nch.mount;
1069 * Must be the root of the filesystem
1071 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1075 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1082 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1084 struct thread *td = curthread;
1085 struct proc *p = td->td_proc;
1088 char *fullpath, *freepath;
1091 if ((error = nlookup(nd)) != 0)
1093 mp = nd->nl_nch.mount;
1095 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1098 error = mount_path(p, mp, &fullpath, &freepath);
1101 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1102 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1103 kfree(freepath, M_TEMP);
1105 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1106 bcopy(sp, buf, sizeof(*buf));
1107 /* Only root should have access to the fsid's. */
1108 if (priv_check(td, PRIV_ROOT))
1109 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1114 * statfs_args(char *path, struct statfs *buf)
1116 * Get filesystem statistics.
1119 sys_statfs(struct statfs_args *uap)
1121 struct nlookupdata nd;
1125 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1127 error = kern_statfs(&nd, &buf);
1130 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1135 kern_fstatfs(int fd, struct statfs *buf)
1137 struct thread *td = curthread;
1138 struct proc *p = td->td_proc;
1142 char *fullpath, *freepath;
1146 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1150 * Try to use mount info from any overlays rather than the
1151 * mount info for the underlying vnode, otherwise we will
1152 * fail when operating on null-mounted paths inside a chroot.
1154 if ((mp = fp->f_nchandle.mount) == NULL)
1155 mp = ((struct vnode *)fp->f_data)->v_mount;
1160 if (fp->f_cred == NULL) {
1165 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1168 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1170 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1171 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1172 kfree(freepath, M_TEMP);
1174 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1175 bcopy(sp, buf, sizeof(*buf));
1177 /* Only root should have access to the fsid's. */
1178 if (priv_check(td, PRIV_ROOT))
1179 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1187 * fstatfs_args(int fd, struct statfs *buf)
1189 * Get filesystem statistics.
1192 sys_fstatfs(struct fstatfs_args *uap)
1197 error = kern_fstatfs(uap->fd, &buf);
1200 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1205 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1211 if ((error = nlookup(nd)) != 0)
1213 mp = nd->nl_nch.mount;
1214 sp = &mp->mnt_vstat;
1215 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1219 if (mp->mnt_flag & MNT_RDONLY)
1220 sp->f_flag |= ST_RDONLY;
1221 if (mp->mnt_flag & MNT_NOSUID)
1222 sp->f_flag |= ST_NOSUID;
1223 bcopy(sp, buf, sizeof(*buf));
1228 * statfs_args(char *path, struct statfs *buf)
1230 * Get filesystem statistics.
1233 sys_statvfs(struct statvfs_args *uap)
1235 struct nlookupdata nd;
1239 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1241 error = kern_statvfs(&nd, &buf);
1244 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1249 kern_fstatvfs(int fd, struct statvfs *buf)
1251 struct thread *td = curthread;
1252 struct proc *p = td->td_proc;
1259 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1261 if ((mp = fp->f_nchandle.mount) == NULL)
1262 mp = ((struct vnode *)fp->f_data)->v_mount;
1267 if (fp->f_cred == NULL) {
1271 sp = &mp->mnt_vstat;
1272 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1276 if (mp->mnt_flag & MNT_RDONLY)
1277 sp->f_flag |= ST_RDONLY;
1278 if (mp->mnt_flag & MNT_NOSUID)
1279 sp->f_flag |= ST_NOSUID;
1281 bcopy(sp, buf, sizeof(*buf));
1289 * fstatfs_args(int fd, struct statfs *buf)
1291 * Get filesystem statistics.
1294 sys_fstatvfs(struct fstatvfs_args *uap)
1299 error = kern_fstatvfs(uap->fd, &buf);
1302 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1307 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1309 * Get statistics on all filesystems.
1312 struct getfsstat_info {
1313 struct statfs *sfsp;
1321 static int getfsstat_callback(struct mount *, void *);
1324 sys_getfsstat(struct getfsstat_args *uap)
1326 struct thread *td = curthread;
1327 struct getfsstat_info info;
1329 bzero(&info, sizeof(info));
1331 info.maxcount = uap->bufsize / sizeof(struct statfs);
1332 info.sfsp = uap->buf;
1334 info.flags = uap->flags;
1337 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1338 if (info.sfsp && info.count > info.maxcount)
1339 uap->sysmsg_result = info.maxcount;
1341 uap->sysmsg_result = info.count;
1342 return (info.error);
1346 getfsstat_callback(struct mount *mp, void *data)
1348 struct getfsstat_info *info = data;
1354 if (info->sfsp && info->count < info->maxcount) {
1355 if (info->td->td_proc &&
1356 !chroot_visible_mnt(mp, info->td->td_proc)) {
1362 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1363 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1364 * overrides MNT_WAIT.
1366 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1367 (info->flags & MNT_WAIT)) &&
1368 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1371 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1373 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1375 info->error = error;
1378 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1379 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1380 kfree(freepath, M_TEMP);
1382 error = copyout(sp, info->sfsp, sizeof(*sp));
1384 info->error = error;
1394 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1395 long bufsize, int flags)
1397 * Get statistics on all filesystems.
1400 struct getvfsstat_info {
1401 struct statfs *sfsp;
1402 struct statvfs *vsfsp;
1410 static int getvfsstat_callback(struct mount *, void *);
1413 sys_getvfsstat(struct getvfsstat_args *uap)
1415 struct thread *td = curthread;
1416 struct getvfsstat_info info;
1418 bzero(&info, sizeof(info));
1420 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1421 info.sfsp = uap->buf;
1422 info.vsfsp = uap->vbuf;
1424 info.flags = uap->flags;
1427 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1428 if (info.vsfsp && info.count > info.maxcount)
1429 uap->sysmsg_result = info.maxcount;
1431 uap->sysmsg_result = info.count;
1432 return (info.error);
1436 getvfsstat_callback(struct mount *mp, void *data)
1438 struct getvfsstat_info *info = data;
1440 struct statvfs *vsp;
1445 if (info->vsfsp && info->count < info->maxcount) {
1446 if (info->td->td_proc &&
1447 !chroot_visible_mnt(mp, info->td->td_proc)) {
1451 vsp = &mp->mnt_vstat;
1454 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1455 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1456 * overrides MNT_WAIT.
1458 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1459 (info->flags & MNT_WAIT)) &&
1460 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1463 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1465 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1466 (info->flags & MNT_WAIT)) &&
1467 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1471 if (mp->mnt_flag & MNT_RDONLY)
1472 vsp->f_flag |= ST_RDONLY;
1473 if (mp->mnt_flag & MNT_NOSUID)
1474 vsp->f_flag |= ST_NOSUID;
1476 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1478 info->error = error;
1481 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1482 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1483 kfree(freepath, M_TEMP);
1485 error = copyout(sp, info->sfsp, sizeof(*sp));
1487 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1489 info->error = error;
1501 * fchdir_args(int fd)
1503 * Change current working directory to a given file descriptor.
1506 sys_fchdir(struct fchdir_args *uap)
1508 struct thread *td = curthread;
1509 struct proc *p = td->td_proc;
1510 struct filedesc *fdp = p->p_fd;
1511 struct vnode *vp, *ovp;
1514 struct nchandle nch, onch, tnch;
1517 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1519 lwkt_gettoken(&p->p_token);
1520 vp = (struct vnode *)fp->f_data;
1522 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1523 if (fp->f_nchandle.ncp == NULL)
1526 error = checkvp_chdir(vp, td);
1531 cache_copy(&fp->f_nchandle, &nch);
1534 * If the ncp has become a mount point, traverse through
1538 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1539 (mp = cache_findmount(&nch)) != NULL
1541 error = nlookup_mp(mp, &tnch);
1543 cache_unlock(&tnch); /* leave ref intact */
1545 vp = tnch.ncp->nc_vp;
1546 error = vget(vp, LK_SHARED);
1547 KKASSERT(error == 0);
1551 cache_dropmount(mp);
1555 onch = fdp->fd_ncdir;
1556 vn_unlock(vp); /* leave ref intact */
1558 fdp->fd_ncdir = nch;
1567 lwkt_reltoken(&p->p_token);
1572 kern_chdir(struct nlookupdata *nd)
1574 struct thread *td = curthread;
1575 struct proc *p = td->td_proc;
1576 struct filedesc *fdp = p->p_fd;
1577 struct vnode *vp, *ovp;
1578 struct nchandle onch;
1581 if ((error = nlookup(nd)) != 0)
1583 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1585 if ((error = vget(vp, LK_SHARED)) != 0)
1588 lwkt_gettoken(&p->p_token);
1589 error = checkvp_chdir(vp, td);
1593 onch = fdp->fd_ncdir;
1594 cache_unlock(&nd->nl_nch); /* leave reference intact */
1595 fdp->fd_ncdir = nd->nl_nch;
1599 cache_zero(&nd->nl_nch);
1603 lwkt_reltoken(&p->p_token);
1608 * chdir_args(char *path)
1610 * Change current working directory (``.'').
1613 sys_chdir(struct chdir_args *uap)
1615 struct nlookupdata nd;
1618 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1620 error = kern_chdir(&nd);
1626 * Helper function for raised chroot(2) security function: Refuse if
1627 * any filedescriptors are open directories.
1630 chroot_refuse_vdir_fds(struct filedesc *fdp)
1637 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1638 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1640 vp = (struct vnode *)fp->f_data;
1641 if (vp->v_type != VDIR) {
1652 * This sysctl determines if we will allow a process to chroot(2) if it
1653 * has a directory open:
1654 * 0: disallowed for all processes.
1655 * 1: allowed for processes that were not already chroot(2)'ed.
1656 * 2: allowed for all processes.
1659 static int chroot_allow_open_directories = 1;
1661 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1662 &chroot_allow_open_directories, 0, "");
1665 * chroot to the specified namecache entry. We obtain the vp from the
1666 * namecache data. The passed ncp must be locked and referenced and will
1667 * remain locked and referenced on return.
1670 kern_chroot(struct nchandle *nch)
1672 struct thread *td = curthread;
1673 struct proc *p = td->td_proc;
1674 struct filedesc *fdp = p->p_fd;
1679 * Only privileged user can chroot
1681 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1686 * Disallow open directory descriptors (fchdir() breakouts).
1688 if (chroot_allow_open_directories == 0 ||
1689 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1690 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1693 if ((vp = nch->ncp->nc_vp) == NULL)
1696 if ((error = vget(vp, LK_SHARED)) != 0)
1700 * Check the validity of vp as a directory to change to and
1701 * associate it with rdir/jdir.
1703 error = checkvp_chdir(vp, td);
1704 vn_unlock(vp); /* leave reference intact */
1706 vrele(fdp->fd_rdir);
1707 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1708 cache_drop(&fdp->fd_nrdir);
1709 cache_copy(nch, &fdp->fd_nrdir);
1710 if (fdp->fd_jdir == NULL) {
1713 cache_copy(nch, &fdp->fd_njdir);
1722 * chroot_args(char *path)
1724 * Change notion of root (``/'') directory.
1727 sys_chroot(struct chroot_args *uap)
1729 struct thread *td __debugvar = curthread;
1730 struct nlookupdata nd;
1733 KKASSERT(td->td_proc);
1734 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1736 nd.nl_flags |= NLC_EXEC;
1737 error = nlookup(&nd);
1739 error = kern_chroot(&nd.nl_nch);
1746 sys_chroot_kernel(struct chroot_kernel_args *uap)
1748 struct thread *td = curthread;
1749 struct nlookupdata nd;
1750 struct nchandle *nch;
1754 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1758 error = nlookup(&nd);
1764 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1768 if ((vp = nch->ncp->nc_vp) == NULL) {
1773 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1776 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1778 vfs_cache_setroot(vp, cache_hold(nch));
1788 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1789 * determine whether it is legal to chdir to the vnode. The vnode's state
1790 * is not changed by this call.
1793 checkvp_chdir(struct vnode *vp, struct thread *td)
1797 if (vp->v_type != VDIR)
1800 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1805 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1807 struct thread *td = curthread;
1808 struct proc *p = td->td_proc;
1809 struct lwp *lp = td->td_lwp;
1810 struct filedesc *fdp = p->p_fd;
1815 int type, indx, error = 0;
1818 if ((oflags & O_ACCMODE) == O_ACCMODE)
1820 flags = FFLAGS(oflags);
1821 error = falloc(lp, &nfp, NULL);
1825 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1828 * XXX p_dupfd is a real mess. It allows a device to return a
1829 * file descriptor to be duplicated rather then doing the open
1835 * Call vn_open() to do the lookup and assign the vnode to the
1836 * file pointer. vn_open() does not change the ref count on fp
1837 * and the vnode, on success, will be inherited by the file pointer
1840 nd->nl_flags |= NLC_LOCKVP;
1841 error = vn_open(nd, fp, flags, cmode);
1845 * handle special fdopen() case. bleh. dupfdopen() is
1846 * responsible for dropping the old contents of ofiles[indx]
1849 * Note that fsetfd() will add a ref to fp which represents
1850 * the fd_files[] assignment. We must still drop our
1853 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1854 if (fdalloc(p, 0, &indx) == 0) {
1855 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1858 fdrop(fp); /* our ref */
1861 fsetfd(fdp, NULL, indx);
1864 fdrop(fp); /* our ref */
1865 if (error == ERESTART)
1871 * ref the vnode for ourselves so it can't be ripped out from under
1872 * is. XXX need an ND flag to request that the vnode be returned
1875 * Reserve a file descriptor but do not assign it until the open
1878 vp = (struct vnode *)fp->f_data;
1880 if ((error = fdalloc(p, 0, &indx)) != 0) {
1887 * If no error occurs the vp will have been assigned to the file
1892 if (flags & (O_EXLOCK | O_SHLOCK)) {
1893 lf.l_whence = SEEK_SET;
1896 if (flags & O_EXLOCK)
1897 lf.l_type = F_WRLCK;
1899 lf.l_type = F_RDLCK;
1900 if (flags & FNONBLOCK)
1905 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1907 * lock request failed. Clean up the reserved
1911 fsetfd(fdp, NULL, indx);
1915 fp->f_flag |= FHASLOCK;
1919 * Assert that all regular file vnodes were created with a object.
1921 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1922 ("open: regular file has no backing object after vn_open"));
1928 * release our private reference, leaving the one associated with the
1929 * descriptor table intact.
1931 fsetfd(fdp, fp, indx);
1934 if (oflags & O_CLOEXEC)
1935 error = fsetfdflags(fdp, *res, UF_EXCLOSE);
1940 * open_args(char *path, int flags, int mode)
1942 * Check permissions, allocate an open file structure,
1943 * and call the device open routine if any.
1946 sys_open(struct open_args *uap)
1948 struct nlookupdata nd;
1951 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1953 error = kern_open(&nd, uap->flags,
1954 uap->mode, &uap->sysmsg_result);
1961 * openat_args(int fd, char *path, int flags, int mode)
1964 sys_openat(struct openat_args *uap)
1966 struct nlookupdata nd;
1970 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
1972 error = kern_open(&nd, uap->flags, uap->mode,
1973 &uap->sysmsg_result);
1975 nlookup_done_at(&nd, fp);
1980 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1982 struct thread *td = curthread;
1983 struct proc *p = td->td_proc;
1992 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1993 vattr.va_rmajor = rmajor;
1994 vattr.va_rminor = rminor;
1996 switch (mode & S_IFMT) {
1997 case S_IFMT: /* used by badsect to flag bad sectors */
1998 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
1999 vattr.va_type = VBAD;
2002 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2003 vattr.va_type = VCHR;
2006 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2007 vattr.va_type = VBLK;
2010 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2013 case S_IFDIR: /* special directories support for HAMMER */
2014 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2015 vattr.va_type = VDIR;
2026 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2027 if ((error = nlookup(nd)) != 0)
2029 if (nd->nl_nch.ncp->nc_vp)
2031 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2035 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2036 nd->nl_cred, NAMEI_CREATE);
2039 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2040 &vp, nd->nl_cred, &vattr);
2048 * mknod_args(char *path, int mode, int dev)
2050 * Create a special file.
2053 sys_mknod(struct mknod_args *uap)
2055 struct nlookupdata nd;
2058 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2060 error = kern_mknod(&nd, uap->mode,
2061 umajor(uap->dev), uminor(uap->dev));
2068 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2070 * Create a special file. The path is relative to the directory associated
2074 sys_mknodat(struct mknodat_args *uap)
2076 struct nlookupdata nd;
2080 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2082 error = kern_mknod(&nd, uap->mode,
2083 umajor(uap->dev), uminor(uap->dev));
2085 nlookup_done_at(&nd, fp);
2090 kern_mkfifo(struct nlookupdata *nd, int mode)
2092 struct thread *td = curthread;
2093 struct proc *p = td->td_proc;
2100 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2101 if ((error = nlookup(nd)) != 0)
2103 if (nd->nl_nch.ncp->nc_vp)
2105 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2109 vattr.va_type = VFIFO;
2110 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2112 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2119 * mkfifo_args(char *path, int mode)
2121 * Create a named pipe.
2124 sys_mkfifo(struct mkfifo_args *uap)
2126 struct nlookupdata nd;
2129 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2131 error = kern_mkfifo(&nd, uap->mode);
2137 * mkfifoat_args(int fd, char *path, mode_t mode)
2139 * Create a named pipe. The path is relative to the directory associated
2143 sys_mkfifoat(struct mkfifoat_args *uap)
2145 struct nlookupdata nd;
2149 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2151 error = kern_mkfifo(&nd, uap->mode);
2152 nlookup_done_at(&nd, fp);
2156 static int hardlink_check_uid = 0;
2157 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2158 &hardlink_check_uid, 0,
2159 "Unprivileged processes cannot create hard links to files owned by other "
2161 static int hardlink_check_gid = 0;
2162 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2163 &hardlink_check_gid, 0,
2164 "Unprivileged processes cannot create hard links to files owned by other "
2168 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2174 * Shortcut if disabled
2176 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2180 * Privileged user can always hardlink
2182 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2186 * Otherwise only if the originating file is owned by the
2187 * same user or group. Note that any group is allowed if
2188 * the file is owned by the caller.
2190 error = VOP_GETATTR(vp, &va);
2194 if (hardlink_check_uid) {
2195 if (cred->cr_uid != va.va_uid)
2199 if (hardlink_check_gid) {
2200 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2208 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2210 struct thread *td = curthread;
2215 * Lookup the source and obtained a locked vnode.
2217 * You may only hardlink a file which you have write permission
2218 * on or which you own.
2220 * XXX relookup on vget failure / race ?
2223 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2224 if ((error = nlookup(nd)) != 0)
2226 vp = nd->nl_nch.ncp->nc_vp;
2227 KKASSERT(vp != NULL);
2228 if (vp->v_type == VDIR)
2229 return (EPERM); /* POSIX */
2230 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2232 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2236 * Unlock the source so we can lookup the target without deadlocking
2237 * (XXX vp is locked already, possible other deadlock?). The target
2240 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2241 nd->nl_flags &= ~NLC_NCPISLOCKED;
2242 cache_unlock(&nd->nl_nch);
2245 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2246 if ((error = nlookup(linknd)) != 0) {
2250 if (linknd->nl_nch.ncp->nc_vp) {
2254 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2260 * Finally run the new API VOP.
2262 error = can_hardlink(vp, td, td->td_ucred);
2264 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2265 vp, linknd->nl_cred);
2272 * link_args(char *path, char *link)
2274 * Make a hard file link.
2277 sys_link(struct link_args *uap)
2279 struct nlookupdata nd, linknd;
2282 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2284 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2286 error = kern_link(&nd, &linknd);
2287 nlookup_done(&linknd);
2294 * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2296 * Make a hard file link. The path1 argument is relative to the directory
2297 * associated with fd1, and similarly the path2 argument is relative to
2298 * the directory associated with fd2.
2301 sys_linkat(struct linkat_args *uap)
2303 struct nlookupdata nd, linknd;
2304 struct file *fp1, *fp2;
2307 error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2308 (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2310 error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2311 uap->path2, UIO_USERSPACE, 0);
2313 error = kern_link(&nd, &linknd);
2314 nlookup_done_at(&linknd, fp2);
2316 nlookup_done_at(&nd, fp1);
2321 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2329 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2330 if ((error = nlookup(nd)) != 0)
2332 if (nd->nl_nch.ncp->nc_vp)
2334 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2338 vattr.va_mode = mode;
2339 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2346 * symlink(char *path, char *link)
2348 * Make a symbolic link.
2351 sys_symlink(struct symlink_args *uap)
2353 struct thread *td = curthread;
2354 struct nlookupdata nd;
2359 path = objcache_get(namei_oc, M_WAITOK);
2360 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2362 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2364 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2365 error = kern_symlink(&nd, path, mode);
2369 objcache_put(namei_oc, path);
2374 * symlinkat_args(char *path1, int fd, char *path2)
2376 * Make a symbolic link. The path2 argument is relative to the directory
2377 * associated with fd.
2380 sys_symlinkat(struct symlinkat_args *uap)
2382 struct thread *td = curthread;
2383 struct nlookupdata nd;
2389 path1 = objcache_get(namei_oc, M_WAITOK);
2390 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2392 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2395 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2396 error = kern_symlink(&nd, path1, mode);
2398 nlookup_done_at(&nd, fp);
2400 objcache_put(namei_oc, path1);
2405 * undelete_args(char *path)
2407 * Delete a whiteout from the filesystem.
2410 sys_undelete(struct undelete_args *uap)
2412 struct nlookupdata nd;
2415 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2417 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2419 error = nlookup(&nd);
2421 error = ncp_writechk(&nd.nl_nch);
2423 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2431 kern_unlink(struct nlookupdata *nd)
2436 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2437 if ((error = nlookup(nd)) != 0)
2439 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2441 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2446 * unlink_args(char *path)
2448 * Delete a name from the filesystem.
2451 sys_unlink(struct unlink_args *uap)
2453 struct nlookupdata nd;
2456 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2458 error = kern_unlink(&nd);
2465 * unlinkat_args(int fd, char *path, int flags)
2467 * Delete the file or directory entry pointed to by fd/path.
2470 sys_unlinkat(struct unlinkat_args *uap)
2472 struct nlookupdata nd;
2476 if (uap->flags & ~AT_REMOVEDIR)
2479 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2481 if (uap->flags & AT_REMOVEDIR)
2482 error = kern_rmdir(&nd);
2484 error = kern_unlink(&nd);
2486 nlookup_done_at(&nd, fp);
2491 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2493 struct thread *td = curthread;
2494 struct proc *p = td->td_proc;
2501 fp = holdfp(p->p_fd, fd, -1);
2504 if (fp->f_type != DTYPE_VNODE) {
2508 vp = (struct vnode *)fp->f_data;
2512 spin_lock(&fp->f_spin);
2513 new_offset = fp->f_offset + offset;
2517 error = VOP_GETATTR(vp, &vattr);
2518 spin_lock(&fp->f_spin);
2519 new_offset = offset + vattr.va_size;
2522 new_offset = offset;
2524 spin_lock(&fp->f_spin);
2529 spin_lock(&fp->f_spin);
2534 * Validate the seek position. Negative offsets are not allowed
2535 * for regular files or directories.
2537 * Normally we would also not want to allow negative offsets for
2538 * character and block-special devices. However kvm addresses
2539 * on 64 bit architectures might appear to be negative and must
2543 if (new_offset < 0 &&
2544 (vp->v_type == VREG || vp->v_type == VDIR)) {
2547 fp->f_offset = new_offset;
2550 *res = fp->f_offset;
2551 spin_unlock(&fp->f_spin);
2558 * lseek_args(int fd, int pad, off_t offset, int whence)
2560 * Reposition read/write file offset.
2563 sys_lseek(struct lseek_args *uap)
2567 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2568 &uap->sysmsg_offset);
2574 * Check if current process can access given file. amode is a bitmask of *_OK
2575 * access bits. flags is a bitmask of AT_* flags.
2578 kern_access(struct nlookupdata *nd, int amode, int flags)
2583 if (flags & ~AT_EACCESS)
2585 if ((error = nlookup(nd)) != 0)
2588 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2592 /* Flags == 0 means only check for existence. */
2601 if ((mode & VWRITE) == 0 ||
2602 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2603 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2606 * If the file handle is stale we have to re-resolve the
2607 * entry. This is a hack at the moment.
2609 if (error == ESTALE) {
2611 cache_setunresolved(&nd->nl_nch);
2612 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2625 * access_args(char *path, int flags)
2627 * Check access permissions.
2630 sys_access(struct access_args *uap)
2632 struct nlookupdata nd;
2635 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2637 error = kern_access(&nd, uap->flags, 0);
2644 * eaccess_args(char *path, int flags)
2646 * Check access permissions.
2649 sys_eaccess(struct eaccess_args *uap)
2651 struct nlookupdata nd;
2654 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2656 error = kern_access(&nd, uap->flags, AT_EACCESS);
2663 * faccessat_args(int fd, char *path, int amode, int flags)
2665 * Check access permissions.
2668 sys_faccessat(struct faccessat_args *uap)
2670 struct nlookupdata nd;
2674 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2677 error = kern_access(&nd, uap->amode, uap->flags);
2678 nlookup_done_at(&nd, fp);
2684 kern_stat(struct nlookupdata *nd, struct stat *st)
2689 if ((error = nlookup(nd)) != 0)
2692 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2695 if ((error = vget(vp, LK_SHARED)) != 0)
2697 error = vn_stat(vp, st, nd->nl_cred);
2700 * If the file handle is stale we have to re-resolve the entry. This
2701 * is a hack at the moment.
2703 if (error == ESTALE) {
2705 cache_setunresolved(&nd->nl_nch);
2706 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2716 * stat_args(char *path, struct stat *ub)
2718 * Get file status; this version follows links.
2721 sys_stat(struct stat_args *uap)
2723 struct nlookupdata nd;
2727 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2729 error = kern_stat(&nd, &st);
2731 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2738 * lstat_args(char *path, struct stat *ub)
2740 * Get file status; this version does not follow links.
2743 sys_lstat(struct lstat_args *uap)
2745 struct nlookupdata nd;
2749 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2751 error = kern_stat(&nd, &st);
2753 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2760 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2762 * Get status of file pointed to by fd/path.
2765 sys_fstatat(struct fstatat_args *uap)
2767 struct nlookupdata nd;
2773 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2776 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2778 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2779 UIO_USERSPACE, flags);
2781 error = kern_stat(&nd, &st);
2783 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2785 nlookup_done_at(&nd, fp);
2790 * pathconf_Args(char *path, int name)
2792 * Get configurable pathname variables.
2795 sys_pathconf(struct pathconf_args *uap)
2797 struct nlookupdata nd;
2802 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2804 error = nlookup(&nd);
2806 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2809 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2817 * kern_readlink isn't properly split yet. There is a copyin burried
2818 * in VOP_READLINK().
2821 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2823 struct thread *td = curthread;
2829 if ((error = nlookup(nd)) != 0)
2831 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2834 if (vp->v_type != VLNK) {
2837 aiov.iov_base = buf;
2838 aiov.iov_len = count;
2839 auio.uio_iov = &aiov;
2840 auio.uio_iovcnt = 1;
2841 auio.uio_offset = 0;
2842 auio.uio_rw = UIO_READ;
2843 auio.uio_segflg = UIO_USERSPACE;
2845 auio.uio_resid = count;
2846 error = VOP_READLINK(vp, &auio, td->td_ucred);
2849 *res = count - auio.uio_resid;
2854 * readlink_args(char *path, char *buf, int count)
2856 * Return target name of a symbolic link.
2859 sys_readlink(struct readlink_args *uap)
2861 struct nlookupdata nd;
2864 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2866 error = kern_readlink(&nd, uap->buf, uap->count,
2867 &uap->sysmsg_result);
2874 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2876 * Return target name of a symbolic link. The path is relative to the
2877 * directory associated with fd.
2880 sys_readlinkat(struct readlinkat_args *uap)
2882 struct nlookupdata nd;
2886 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2888 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2889 &uap->sysmsg_result);
2891 nlookup_done_at(&nd, fp);
2896 setfflags(struct vnode *vp, int flags)
2898 struct thread *td = curthread;
2903 * Prevent non-root users from setting flags on devices. When
2904 * a device is reused, users can retain ownership of the device
2905 * if they are allowed to set flags and programs assume that
2906 * chown can't fail when done as root.
2908 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2909 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2913 * note: vget is required for any operation that might mod the vnode
2914 * so VINACTIVE is properly cleared.
2916 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2918 vattr.va_flags = flags;
2919 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2926 * chflags(char *path, int flags)
2928 * Change flags of a file given a path name.
2931 sys_chflags(struct chflags_args *uap)
2933 struct nlookupdata nd;
2938 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2940 error = nlookup(&nd);
2942 error = ncp_writechk(&nd.nl_nch);
2944 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2947 error = setfflags(vp, uap->flags);
2954 * lchflags(char *path, int flags)
2956 * Change flags of a file given a path name, but don't follow symlinks.
2959 sys_lchflags(struct lchflags_args *uap)
2961 struct nlookupdata nd;
2966 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2968 error = nlookup(&nd);
2970 error = ncp_writechk(&nd.nl_nch);
2972 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2975 error = setfflags(vp, uap->flags);
2982 * fchflags_args(int fd, int flags)
2984 * Change flags of a file given a file descriptor.
2987 sys_fchflags(struct fchflags_args *uap)
2989 struct thread *td = curthread;
2990 struct proc *p = td->td_proc;
2994 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2996 if (fp->f_nchandle.ncp)
2997 error = ncp_writechk(&fp->f_nchandle);
2999 error = setfflags((struct vnode *) fp->f_data, uap->flags);
3005 setfmode(struct vnode *vp, int mode)
3007 struct thread *td = curthread;
3012 * note: vget is required for any operation that might mod the vnode
3013 * so VINACTIVE is properly cleared.
3015 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3017 vattr.va_mode = mode & ALLPERMS;
3018 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3025 kern_chmod(struct nlookupdata *nd, int mode)
3030 if ((error = nlookup(nd)) != 0)
3032 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3034 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3035 error = setfmode(vp, mode);
3041 * chmod_args(char *path, int mode)
3043 * Change mode of a file given path name.
3046 sys_chmod(struct chmod_args *uap)
3048 struct nlookupdata nd;
3051 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3053 error = kern_chmod(&nd, uap->mode);
3059 * lchmod_args(char *path, int mode)
3061 * Change mode of a file given path name (don't follow links.)
3064 sys_lchmod(struct lchmod_args *uap)
3066 struct nlookupdata nd;
3069 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3071 error = kern_chmod(&nd, uap->mode);
3077 * fchmod_args(int fd, int mode)
3079 * Change mode of a file given a file descriptor.
3082 sys_fchmod(struct fchmod_args *uap)
3084 struct thread *td = curthread;
3085 struct proc *p = td->td_proc;
3089 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3091 if (fp->f_nchandle.ncp)
3092 error = ncp_writechk(&fp->f_nchandle);
3094 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3100 * fchmodat_args(char *path, int mode)
3102 * Change mode of a file pointed to by fd/path.
3105 sys_fchmodat(struct fchmodat_args *uap)
3107 struct nlookupdata nd;
3112 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3114 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3116 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3117 UIO_USERSPACE, flags);
3119 error = kern_chmod(&nd, uap->mode);
3120 nlookup_done_at(&nd, fp);
3125 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3127 struct thread *td = curthread;
3135 * note: vget is required for any operation that might mod the vnode
3136 * so VINACTIVE is properly cleared.
3138 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3139 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3141 o_uid = vattr.va_uid;
3142 o_gid = vattr.va_gid;
3143 size = vattr.va_size;
3148 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3157 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3158 VFS_ACCOUNT(mp, uid, gid, size);
3165 kern_chown(struct nlookupdata *nd, int uid, int gid)
3170 if ((error = nlookup(nd)) != 0)
3172 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3174 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3175 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3181 * chown(char *path, int uid, int gid)
3183 * Set ownership given a path name.
3186 sys_chown(struct chown_args *uap)
3188 struct nlookupdata nd;
3191 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3193 error = kern_chown(&nd, uap->uid, uap->gid);
3199 * lchown_args(char *path, int uid, int gid)
3201 * Set ownership given a path name, do not cross symlinks.
3204 sys_lchown(struct lchown_args *uap)
3206 struct nlookupdata nd;
3209 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3211 error = kern_chown(&nd, uap->uid, uap->gid);
3217 * fchown_args(int fd, int uid, int gid)
3219 * Set ownership given a file descriptor.
3222 sys_fchown(struct fchown_args *uap)
3224 struct thread *td = curthread;
3225 struct proc *p = td->td_proc;
3229 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3231 if (fp->f_nchandle.ncp)
3232 error = ncp_writechk(&fp->f_nchandle);
3234 error = setfown(p->p_fd->fd_ncdir.mount,
3235 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3241 * fchownat(int fd, char *path, int uid, int gid, int flags)
3243 * Set ownership of file pointed to by fd/path.
3246 sys_fchownat(struct fchownat_args *uap)
3248 struct nlookupdata nd;
3253 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3255 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3257 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3258 UIO_USERSPACE, flags);
3260 error = kern_chown(&nd, uap->uid, uap->gid);
3261 nlookup_done_at(&nd, fp);
3267 getutimes(const struct timeval *tvp, struct timespec *tsp)
3269 struct timeval tv[2];
3273 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3276 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3277 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3283 setutimes(struct vnode *vp, struct vattr *vattr,
3284 const struct timespec *ts, int nullflag)
3286 struct thread *td = curthread;
3290 vattr->va_atime = ts[0];
3291 vattr->va_mtime = ts[1];
3293 vattr->va_vaflags |= VA_UTIMES_NULL;
3294 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3300 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3302 struct timespec ts[2];
3307 if ((error = getutimes(tptr, ts)) != 0)
3311 * NOTE: utimes() succeeds for the owner even if the file
3312 * is not user-writable.
3314 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3316 if ((error = nlookup(nd)) != 0)
3318 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3320 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3324 * note: vget is required for any operation that might mod the vnode
3325 * so VINACTIVE is properly cleared.
3327 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3328 error = vget(vp, LK_EXCLUSIVE);
3330 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3339 * utimes_args(char *path, struct timeval *tptr)
3341 * Set the access and modification times of a file.
3344 sys_utimes(struct utimes_args *uap)
3346 struct timeval tv[2];
3347 struct nlookupdata nd;
3351 error = copyin(uap->tptr, tv, sizeof(tv));
3355 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3357 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3363 * lutimes_args(char *path, struct timeval *tptr)
3365 * Set the access and modification times of a file.
3368 sys_lutimes(struct lutimes_args *uap)
3370 struct timeval tv[2];
3371 struct nlookupdata nd;
3375 error = copyin(uap->tptr, tv, sizeof(tv));
3379 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3381 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3387 * Set utimes on a file descriptor. The creds used to open the
3388 * file are used to determine whether the operation is allowed
3392 kern_futimes(int fd, struct timeval *tptr)
3394 struct thread *td = curthread;
3395 struct proc *p = td->td_proc;
3396 struct timespec ts[2];
3402 error = getutimes(tptr, ts);
3405 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3407 if (fp->f_nchandle.ncp)
3408 error = ncp_writechk(&fp->f_nchandle);
3411 error = vget(vp, LK_EXCLUSIVE);
3413 error = VOP_GETATTR(vp, &vattr);
3415 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3419 error = setutimes(vp, &vattr, ts,
3430 * futimes_args(int fd, struct timeval *tptr)
3432 * Set the access and modification times of a file.
3435 sys_futimes(struct futimes_args *uap)
3437 struct timeval tv[2];
3441 error = copyin(uap->tptr, tv, sizeof(tv));
3445 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3451 kern_truncate(struct nlookupdata *nd, off_t length)
3458 uint64_t old_size = 0;
3462 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3463 if ((error = nlookup(nd)) != 0)
3465 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3467 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3469 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3473 if (vp->v_type == VDIR) {
3477 if (vfs_quota_enabled) {
3478 error = VOP_GETATTR(vp, &vattr);
3479 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3482 old_size = vattr.va_size;
3485 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3487 vattr.va_size = length;
3488 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3489 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3497 * truncate(char *path, int pad, off_t length)
3499 * Truncate a file given its path name.
3502 sys_truncate(struct truncate_args *uap)
3504 struct nlookupdata nd;
3507 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3509 error = kern_truncate(&nd, uap->length);
3515 kern_ftruncate(int fd, off_t length)
3517 struct thread *td = curthread;
3518 struct proc *p = td->td_proc;
3525 uint64_t old_size = 0;
3530 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3532 if (fp->f_nchandle.ncp) {
3533 error = ncp_writechk(&fp->f_nchandle);
3537 if ((fp->f_flag & FWRITE) == 0) {
3541 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3545 vp = (struct vnode *)fp->f_data;
3546 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3547 if (vp->v_type == VDIR) {
3552 if (vfs_quota_enabled) {
3553 error = VOP_GETATTR(vp, &vattr);
3554 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3557 old_size = vattr.va_size;
3560 if ((error = vn_writechk(vp, NULL)) == 0) {
3562 vattr.va_size = length;
3563 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3565 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3574 * ftruncate_args(int fd, int pad, off_t length)
3576 * Truncate a file given a file descriptor.
3579 sys_ftruncate(struct ftruncate_args *uap)
3583 error = kern_ftruncate(uap->fd, uap->length);
3591 * Sync an open file.
3594 sys_fsync(struct fsync_args *uap)
3596 struct thread *td = curthread;
3597 struct proc *p = td->td_proc;
3603 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3605 vp = (struct vnode *)fp->f_data;
3606 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3607 if ((obj = vp->v_object) != NULL)
3608 vm_object_page_clean(obj, 0, 0, 0);
3609 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3610 if (error == 0 && vp->v_mount)
3611 error = buf_fsync(vp);
3619 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3621 struct nchandle fnchd;
3622 struct nchandle tnchd;
3623 struct namecache *ncp;
3630 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3631 if ((error = nlookup(fromnd)) != 0)
3633 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3635 fnchd.mount = fromnd->nl_nch.mount;
3639 * unlock the source nch so we can lookup the target nch without
3640 * deadlocking. The target may or may not exist so we do not check
3641 * for a target vp like kern_mkdir() and other creation functions do.
3643 * The source and target directories are ref'd and rechecked after
3644 * everything is relocked to determine if the source or target file
3647 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3648 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3649 cache_unlock(&fromnd->nl_nch);
3651 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3652 if ((error = nlookup(tond)) != 0) {
3656 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3660 tnchd.mount = tond->nl_nch.mount;
3664 * If the source and target are the same there is nothing to do
3666 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3673 * Mount points cannot be renamed or overwritten
3675 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3684 * Relock the source ncp. cache_relock() will deal with any
3685 * deadlocks against the already-locked tond and will also
3686 * make sure both are resolved.
3688 * NOTE AFTER RELOCKING: The source or target ncp may have become
3689 * invalid while they were unlocked, nc_vp and nc_mount could
3692 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3693 &tond->nl_nch, tond->nl_cred);
3694 fromnd->nl_flags |= NLC_NCPISLOCKED;
3697 * make sure the parent directories linkages are the same
3699 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3700 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3707 * Both the source and target must be within the same filesystem and
3708 * in the same filesystem as their parent directories within the
3709 * namecache topology.
3711 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3714 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3715 mp != tond->nl_nch.mount) {
3722 * Make sure the mount point is writable
3724 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3731 * If the target exists and either the source or target is a directory,
3732 * then both must be directories.
3734 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3737 if (tond->nl_nch.ncp->nc_vp) {
3738 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3740 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3741 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3743 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3749 * You cannot rename a source into itself or a subdirectory of itself.
3750 * We check this by travsersing the target directory upwards looking
3751 * for a match against the source.
3756 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3757 if (fromnd->nl_nch.ncp == ncp) {
3768 * Even though the namespaces are different, they may still represent
3769 * hardlinks to the same file. The filesystem might have a hard time
3770 * with this so we issue a NREMOVE of the source instead of a NRENAME
3771 * when we detect the situation.
3774 fdvp = fromnd->nl_dvp;
3775 tdvp = tond->nl_dvp;
3776 if (fdvp == NULL || tdvp == NULL) {
3778 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3779 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3782 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3783 fdvp, tdvp, tond->nl_cred);
3790 * rename_args(char *from, char *to)
3792 * Rename files. Source and destination must either both be directories,
3793 * or both not be directories. If target is a directory, it must be empty.
3796 sys_rename(struct rename_args *uap)
3798 struct nlookupdata fromnd, tond;
3801 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3803 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3805 error = kern_rename(&fromnd, &tond);
3806 nlookup_done(&tond);
3808 nlookup_done(&fromnd);
3813 * renameat_args(int oldfd, char *old, int newfd, char *new)
3815 * Rename files using paths relative to the directories associated with
3816 * oldfd and newfd. Source and destination must either both be directories,
3817 * or both not be directories. If target is a directory, it must be empty.
3820 sys_renameat(struct renameat_args *uap)
3822 struct nlookupdata oldnd, newnd;
3823 struct file *oldfp, *newfp;
3826 error = nlookup_init_at(&oldnd, &oldfp, uap->oldfd, uap->old,
3829 error = nlookup_init_at(&newnd, &newfp, uap->newfd, uap->new,
3832 error = kern_rename(&oldnd, &newnd);
3833 nlookup_done_at(&newnd, newfp);
3835 nlookup_done_at(&oldnd, oldfp);
3840 kern_mkdir(struct nlookupdata *nd, int mode)
3842 struct thread *td = curthread;
3843 struct proc *p = td->td_proc;
3849 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3850 if ((error = nlookup(nd)) != 0)
3853 if (nd->nl_nch.ncp->nc_vp)
3855 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3858 vattr.va_type = VDIR;
3859 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3862 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3869 * mkdir_args(char *path, int mode)
3871 * Make a directory file.
3874 sys_mkdir(struct mkdir_args *uap)
3876 struct nlookupdata nd;
3879 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3881 error = kern_mkdir(&nd, uap->mode);
3887 * mkdirat_args(int fd, char *path, mode_t mode)
3889 * Make a directory file. The path is relative to the directory associated
3893 sys_mkdirat(struct mkdirat_args *uap)
3895 struct nlookupdata nd;
3899 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3901 error = kern_mkdir(&nd, uap->mode);
3902 nlookup_done_at(&nd, fp);
3907 kern_rmdir(struct nlookupdata *nd)
3912 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3913 if ((error = nlookup(nd)) != 0)
3917 * Do not allow directories representing mount points to be
3918 * deleted, even if empty. Check write perms on mount point
3919 * in case the vnode is aliased (aka nullfs).
3921 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3923 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3925 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3930 * rmdir_args(char *path)
3932 * Remove a directory file.
3935 sys_rmdir(struct rmdir_args *uap)
3937 struct nlookupdata nd;
3940 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3942 error = kern_rmdir(&nd);
3948 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3949 enum uio_seg direction)
3951 struct thread *td = curthread;
3952 struct proc *p = td->td_proc;
3960 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3962 if ((fp->f_flag & FREAD) == 0) {
3966 vp = (struct vnode *)fp->f_data;
3968 if (vp->v_type != VDIR) {
3972 aiov.iov_base = buf;
3973 aiov.iov_len = count;
3974 auio.uio_iov = &aiov;
3975 auio.uio_iovcnt = 1;
3976 auio.uio_rw = UIO_READ;
3977 auio.uio_segflg = direction;
3979 auio.uio_resid = count;
3980 loff = auio.uio_offset = fp->f_offset;
3981 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3982 fp->f_offset = auio.uio_offset;
3985 if (count == auio.uio_resid) {
3986 if (union_dircheckp) {
3987 error = union_dircheckp(td, &vp, fp);
3994 if ((vp->v_flag & VROOT) &&
3995 (vp->v_mount->mnt_flag & MNT_UNION)) {
3996 struct vnode *tvp = vp;
3997 vp = vp->v_mount->mnt_vnodecovered;
4008 * WARNING! *basep may not be wide enough to accomodate the
4009 * seek offset. XXX should we hack this to return the upper 32 bits
4010 * for offsets greater then 4G?
4013 *basep = (long)loff;
4015 *res = count - auio.uio_resid;
4022 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4024 * Read a block of directory entries in a file system independent format.
4027 sys_getdirentries(struct getdirentries_args *uap)
4032 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4033 &uap->sysmsg_result, UIO_USERSPACE);
4035 if (error == 0 && uap->basep)
4036 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4041 * getdents_args(int fd, char *buf, size_t count)
4044 sys_getdents(struct getdents_args *uap)
4048 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4049 &uap->sysmsg_result, UIO_USERSPACE);
4055 * Set the mode mask for creation of filesystem nodes.
4057 * umask(int newmask)
4060 sys_umask(struct umask_args *uap)
4062 struct thread *td = curthread;
4063 struct proc *p = td->td_proc;
4064 struct filedesc *fdp;
4067 uap->sysmsg_result = fdp->fd_cmask;
4068 fdp->fd_cmask = uap->newmask & ALLPERMS;
4073 * revoke(char *path)
4075 * Void all references to file by ripping underlying filesystem
4079 sys_revoke(struct revoke_args *uap)
4081 struct nlookupdata nd;
4088 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4090 error = nlookup(&nd);
4092 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4093 cred = crhold(nd.nl_cred);
4097 error = VOP_GETATTR(vp, &vattr);
4098 if (error == 0 && cred->cr_uid != vattr.va_uid)
4099 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4100 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4102 error = vrevoke(vp, cred);
4103 } else if (error == 0) {
4104 error = vrevoke(vp, cred);
4114 * getfh_args(char *fname, fhandle_t *fhp)
4116 * Get (NFS) file handle
4118 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4119 * mount. This allows nullfs mounts to be explicitly exported.
4121 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4123 * nullfs mounts of subdirectories are not safe. That is, it will
4124 * work, but you do not really have protection against access to
4125 * the related parent directories.
4128 sys_getfh(struct getfh_args *uap)
4130 struct thread *td = curthread;
4131 struct nlookupdata nd;
4138 * Must be super user
4140 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4144 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4146 error = nlookup(&nd);
4148 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4149 mp = nd.nl_nch.mount;
4152 bzero(&fh, sizeof(fh));
4153 fh.fh_fsid = mp->mnt_stat.f_fsid;
4154 error = VFS_VPTOFH(vp, &fh.fh_fid);
4157 error = copyout(&fh, uap->fhp, sizeof(fh));
4163 * fhopen_args(const struct fhandle *u_fhp, int flags)
4165 * syscall for the rpc.lockd to use to translate a NFS file handle into
4166 * an open descriptor.
4168 * warning: do not remove the priv_check() call or this becomes one giant
4172 sys_fhopen(struct fhopen_args *uap)
4174 struct thread *td = curthread;
4175 struct filedesc *fdp = td->td_proc->p_fd;
4180 struct vattr *vap = &vat;
4182 int fmode, mode, error = 0, type;
4188 * Must be super user
4190 error = priv_check(td, PRIV_ROOT);
4194 fmode = FFLAGS(uap->flags);
4197 * Why not allow a non-read/write open for our lockd?
4199 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4201 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4206 * Find the mount point
4208 mp = vfs_getvfs(&fhp.fh_fsid);
4213 /* now give me my vnode, it gets returned to me locked */
4214 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4218 * from now on we have to make sure not
4219 * to forget about the vnode
4220 * any error that causes an abort must vput(vp)
4221 * just set error = err and 'goto bad;'.
4227 if (vp->v_type == VLNK) {
4231 if (vp->v_type == VSOCK) {
4236 if (fmode & (FWRITE | O_TRUNC)) {
4237 if (vp->v_type == VDIR) {
4241 error = vn_writechk(vp, NULL);
4249 error = VOP_ACCESS(vp, mode, td->td_ucred);
4253 if (fmode & O_TRUNC) {
4254 vn_unlock(vp); /* XXX */
4255 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4258 error = VOP_SETATTR(vp, vap, td->td_ucred);
4264 * VOP_OPEN needs the file pointer so it can potentially override
4267 * WARNING! no f_nchandle will be associated when fhopen()ing a
4270 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4274 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4277 * setting f_ops this way prevents VOP_CLOSE from being
4278 * called or fdrop() releasing the vp from v_data. Since
4279 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4281 fp->f_ops = &badfileops;
4287 * The fp is given its own reference, we still have our ref and lock.
4289 * Assert that all regular files must be created with a VM object.
4291 if (vp->v_type == VREG && vp->v_object == NULL) {
4292 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4297 * The open was successful. Handle any locking requirements.
4299 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4300 lf.l_whence = SEEK_SET;
4303 if (fmode & O_EXLOCK)
4304 lf.l_type = F_WRLCK;
4306 lf.l_type = F_RDLCK;
4307 if (fmode & FNONBLOCK)
4312 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4314 * release our private reference.
4316 fsetfd(fdp, NULL, indx);
4321 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4322 fp->f_flag |= FHASLOCK;
4326 * Clean up. Associate the file pointer with the previously
4327 * reserved descriptor and return it.
4330 fsetfd(fdp, fp, indx);
4332 uap->sysmsg_result = indx;
4333 if (uap->flags & O_CLOEXEC)
4334 error = fsetfdflags(fdp, indx, UF_EXCLOSE);
4338 fsetfd(fdp, NULL, indx);
4347 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4350 sys_fhstat(struct fhstat_args *uap)
4352 struct thread *td = curthread;
4360 * Must be super user
4362 error = priv_check(td, PRIV_ROOT);
4366 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4370 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4373 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4374 error = vn_stat(vp, &sb, td->td_ucred);
4379 error = copyout(&sb, uap->sb, sizeof(sb));
4384 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4387 sys_fhstatfs(struct fhstatfs_args *uap)
4389 struct thread *td = curthread;
4390 struct proc *p = td->td_proc;
4395 char *fullpath, *freepath;
4400 * Must be super user
4402 if ((error = priv_check(td, PRIV_ROOT)))
4405 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4408 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4412 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4417 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4422 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4425 error = mount_path(p, mp, &fullpath, &freepath);
4428 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4429 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4430 kfree(freepath, M_TEMP);
4432 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4433 if (priv_check(td, PRIV_ROOT)) {
4434 bcopy(sp, &sb, sizeof(sb));
4435 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4438 error = copyout(sp, uap->buf, sizeof(*sp));
4444 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4447 sys_fhstatvfs(struct fhstatvfs_args *uap)
4449 struct thread *td = curthread;
4450 struct proc *p = td->td_proc;
4458 * Must be super user
4460 if ((error = priv_check(td, PRIV_ROOT)))
4463 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4466 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4470 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4475 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4478 sp = &mp->mnt_vstat;
4480 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4484 if (mp->mnt_flag & MNT_RDONLY)
4485 sp->f_flag |= ST_RDONLY;
4486 if (mp->mnt_flag & MNT_NOSUID)
4487 sp->f_flag |= ST_NOSUID;
4488 error = copyout(sp, uap->buf, sizeof(*sp));
4495 * Syscall to push extended attribute configuration information into the
4496 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4497 * a command (int cmd), and attribute name and misc data. For now, the
4498 * attribute name is left in userspace for consumption by the VFS_op.
4499 * It will probably be changed to be copied into sysspace by the
4500 * syscall in the future, once issues with various consumers of the
4501 * attribute code have raised their hands.
4503 * Currently this is used only by UFS Extended Attributes.
4506 sys_extattrctl(struct extattrctl_args *uap)
4508 struct nlookupdata nd;
4510 char attrname[EXTATTR_MAXNAMELEN];
4518 if (error == 0 && uap->filename) {
4519 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4522 error = nlookup(&nd);
4524 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4528 if (error == 0 && uap->attrname) {
4529 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4534 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4536 error = nlookup(&nd);
4538 error = ncp_writechk(&nd.nl_nch);
4540 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4542 uap->attrname, nd.nl_cred);
4551 * Syscall to get a named extended attribute on a file or directory.
4554 sys_extattr_set_file(struct extattr_set_file_args *uap)
4556 char attrname[EXTATTR_MAXNAMELEN];
4557 struct nlookupdata nd;
4563 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4569 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4571 error = nlookup(&nd);
4573 error = ncp_writechk(&nd.nl_nch);
4575 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4581 bzero(&auio, sizeof(auio));
4582 aiov.iov_base = uap->data;
4583 aiov.iov_len = uap->nbytes;
4584 auio.uio_iov = &aiov;
4585 auio.uio_iovcnt = 1;
4586 auio.uio_offset = 0;
4587 auio.uio_resid = uap->nbytes;
4588 auio.uio_rw = UIO_WRITE;
4589 auio.uio_td = curthread;
4591 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4600 * Syscall to get a named extended attribute on a file or directory.
4603 sys_extattr_get_file(struct extattr_get_file_args *uap)
4605 char attrname[EXTATTR_MAXNAMELEN];
4606 struct nlookupdata nd;
4612 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4618 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4620 error = nlookup(&nd);
4622 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4628 bzero(&auio, sizeof(auio));
4629 aiov.iov_base = uap->data;
4630 aiov.iov_len = uap->nbytes;
4631 auio.uio_iov = &aiov;
4632 auio.uio_iovcnt = 1;
4633 auio.uio_offset = 0;
4634 auio.uio_resid = uap->nbytes;
4635 auio.uio_rw = UIO_READ;
4636 auio.uio_td = curthread;
4638 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4640 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4648 * Syscall to delete a named extended attribute from a file or directory.
4649 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4652 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4654 char attrname[EXTATTR_MAXNAMELEN];
4655 struct nlookupdata nd;
4659 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4663 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4665 error = nlookup(&nd);
4667 error = ncp_writechk(&nd.nl_nch);
4669 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4671 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4672 attrname, NULL, nd.nl_cred);
4681 * Determine if the mount is visible to the process.
4684 chroot_visible_mnt(struct mount *mp, struct proc *p)
4686 struct nchandle nch;
4689 * Traverse from the mount point upwards. If we hit the process
4690 * root then the mount point is visible to the process.
4692 nch = mp->mnt_ncmountpt;
4694 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4695 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4698 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4699 nch = nch.mount->mnt_ncmounton;
4701 nch.ncp = nch.ncp->nc_parent;
4706 * If the mount point is not visible to the process, but the
4707 * process root is in a subdirectory of the mount, return
4710 if (p->p_fd->fd_nrdir.mount == mp)