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, &ptr, &buf, 0) == 0) {
826 kprintf("unmount(%s): ", ptr);
831 kprintf("unmount(%p", mp);
832 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
833 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
842 * Shim cache_fullpath() to handle the case where a process is chrooted into
843 * a subdirectory of a mount. In this case if the root mount matches the
844 * process root directory's mount we have to specify the process's root
845 * directory instead of the mount point, because the mount point might
846 * be above the root directory.
850 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
852 struct nchandle *nch;
854 if (p && p->p_fd->fd_nrdir.mount == mp)
855 nch = &p->p_fd->fd_nrdir;
857 nch = &mp->mnt_ncmountpt;
858 return(cache_fullpath(p, nch, rb, fb, 0));
862 * Sync each mounted filesystem.
866 static int syncprt = 0;
867 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
870 static int sync_callback(struct mount *mp, void *data);
873 sys_sync(struct sync_args *uap)
875 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
878 * print out buffer pool stat information on each sync() call.
888 sync_callback(struct mount *mp, void *data __unused)
892 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
893 asyncflag = mp->mnt_flag & MNT_ASYNC;
894 mp->mnt_flag &= ~MNT_ASYNC;
895 vfs_msync(mp, MNT_NOWAIT);
896 VFS_SYNC(mp, MNT_NOWAIT | MNT_LAZY);
897 mp->mnt_flag |= asyncflag;
902 /* XXX PRISON: could be per prison flag */
903 static int prison_quotas;
905 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
909 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
911 * Change filesystem quotas.
916 sys_quotactl(struct quotactl_args *uap)
918 struct nlookupdata nd;
925 if (td->td_ucred->cr_prison && !prison_quotas) {
930 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
932 error = nlookup(&nd);
934 mp = nd.nl_nch.mount;
935 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
936 uap->arg, nd.nl_cred);
945 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
946 * void *buf, int buflen)
948 * This function operates on a mount point and executes the specified
949 * operation using the specified control data, and possibly returns data.
951 * The actual number of bytes stored in the result buffer is returned, 0
952 * if none, otherwise an error is returned.
957 sys_mountctl(struct mountctl_args *uap)
959 struct thread *td = curthread;
960 struct proc *p = td->td_proc;
968 * Sanity and permissions checks. We must be root.
971 if (td->td_ucred->cr_prison != NULL)
973 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
974 (error = priv_check(td, PRIV_ROOT)) != 0)
978 * Argument length checks
980 if (uap->ctllen < 0 || uap->ctllen > 1024)
982 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
984 if (uap->path == NULL)
988 * Allocate the necessary buffers and copyin data
990 path = objcache_get(namei_oc, M_WAITOK);
991 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
996 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
997 error = copyin(uap->ctl, ctl, uap->ctllen);
1002 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1005 * Validate the descriptor
1008 fp = holdfp(p->p_fd, uap->fd, -1);
1018 * Execute the internal kernel function and clean up.
1021 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1025 if (error == 0 && uap->sysmsg_result > 0)
1026 error = copyout(buf, uap->buf, uap->sysmsg_result);
1029 objcache_put(namei_oc, path);
1038 * Execute a mount control operation by resolving the path to a mount point
1039 * and calling vop_mountctl().
1041 * Use the mount point from the nch instead of the vnode so nullfs mounts
1042 * can properly spike the VOP.
1045 kern_mountctl(const char *path, int op, struct file *fp,
1046 const void *ctl, int ctllen,
1047 void *buf, int buflen, int *res)
1051 struct nlookupdata nd;
1056 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1058 error = nlookup(&nd);
1060 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1061 mp = nd.nl_nch.mount;
1068 * Must be the root of the filesystem
1070 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1074 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1081 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1083 struct thread *td = curthread;
1084 struct proc *p = td->td_proc;
1087 char *fullpath, *freepath;
1090 if ((error = nlookup(nd)) != 0)
1092 mp = nd->nl_nch.mount;
1094 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1097 error = mount_path(p, mp, &fullpath, &freepath);
1100 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1101 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1102 kfree(freepath, M_TEMP);
1104 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1105 bcopy(sp, buf, sizeof(*buf));
1106 /* Only root should have access to the fsid's. */
1107 if (priv_check(td, PRIV_ROOT))
1108 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1113 * statfs_args(char *path, struct statfs *buf)
1115 * Get filesystem statistics.
1118 sys_statfs(struct statfs_args *uap)
1120 struct nlookupdata nd;
1124 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1126 error = kern_statfs(&nd, &buf);
1129 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1134 kern_fstatfs(int fd, struct statfs *buf)
1136 struct thread *td = curthread;
1137 struct proc *p = td->td_proc;
1141 char *fullpath, *freepath;
1145 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1149 * Try to use mount info from any overlays rather than the
1150 * mount info for the underlying vnode, otherwise we will
1151 * fail when operating on null-mounted paths inside a chroot.
1153 if ((mp = fp->f_nchandle.mount) == NULL)
1154 mp = ((struct vnode *)fp->f_data)->v_mount;
1159 if (fp->f_cred == NULL) {
1164 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1167 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1169 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1170 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1171 kfree(freepath, M_TEMP);
1173 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1174 bcopy(sp, buf, sizeof(*buf));
1176 /* Only root should have access to the fsid's. */
1177 if (priv_check(td, PRIV_ROOT))
1178 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1186 * fstatfs_args(int fd, struct statfs *buf)
1188 * Get filesystem statistics.
1191 sys_fstatfs(struct fstatfs_args *uap)
1196 error = kern_fstatfs(uap->fd, &buf);
1199 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1204 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1210 if ((error = nlookup(nd)) != 0)
1212 mp = nd->nl_nch.mount;
1213 sp = &mp->mnt_vstat;
1214 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1218 if (mp->mnt_flag & MNT_RDONLY)
1219 sp->f_flag |= ST_RDONLY;
1220 if (mp->mnt_flag & MNT_NOSUID)
1221 sp->f_flag |= ST_NOSUID;
1222 bcopy(sp, buf, sizeof(*buf));
1227 * statfs_args(char *path, struct statfs *buf)
1229 * Get filesystem statistics.
1232 sys_statvfs(struct statvfs_args *uap)
1234 struct nlookupdata nd;
1238 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1240 error = kern_statvfs(&nd, &buf);
1243 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1248 kern_fstatvfs(int fd, struct statvfs *buf)
1250 struct thread *td = curthread;
1251 struct proc *p = td->td_proc;
1258 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1260 if ((mp = fp->f_nchandle.mount) == NULL)
1261 mp = ((struct vnode *)fp->f_data)->v_mount;
1266 if (fp->f_cred == NULL) {
1270 sp = &mp->mnt_vstat;
1271 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1275 if (mp->mnt_flag & MNT_RDONLY)
1276 sp->f_flag |= ST_RDONLY;
1277 if (mp->mnt_flag & MNT_NOSUID)
1278 sp->f_flag |= ST_NOSUID;
1280 bcopy(sp, buf, sizeof(*buf));
1288 * fstatfs_args(int fd, struct statfs *buf)
1290 * Get filesystem statistics.
1293 sys_fstatvfs(struct fstatvfs_args *uap)
1298 error = kern_fstatvfs(uap->fd, &buf);
1301 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1306 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1308 * Get statistics on all filesystems.
1311 struct getfsstat_info {
1312 struct statfs *sfsp;
1320 static int getfsstat_callback(struct mount *, void *);
1323 sys_getfsstat(struct getfsstat_args *uap)
1325 struct thread *td = curthread;
1326 struct getfsstat_info info;
1328 bzero(&info, sizeof(info));
1330 info.maxcount = uap->bufsize / sizeof(struct statfs);
1331 info.sfsp = uap->buf;
1333 info.flags = uap->flags;
1336 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1337 if (info.sfsp && info.count > info.maxcount)
1338 uap->sysmsg_result = info.maxcount;
1340 uap->sysmsg_result = info.count;
1341 return (info.error);
1345 getfsstat_callback(struct mount *mp, void *data)
1347 struct getfsstat_info *info = data;
1353 if (info->sfsp && info->count < info->maxcount) {
1354 if (info->td->td_proc &&
1355 !chroot_visible_mnt(mp, info->td->td_proc)) {
1361 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1362 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1363 * overrides MNT_WAIT.
1365 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1366 (info->flags & MNT_WAIT)) &&
1367 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1370 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1372 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1374 info->error = error;
1377 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1378 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1379 kfree(freepath, M_TEMP);
1381 error = copyout(sp, info->sfsp, sizeof(*sp));
1383 info->error = error;
1393 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1394 long bufsize, int flags)
1396 * Get statistics on all filesystems.
1399 struct getvfsstat_info {
1400 struct statfs *sfsp;
1401 struct statvfs *vsfsp;
1409 static int getvfsstat_callback(struct mount *, void *);
1412 sys_getvfsstat(struct getvfsstat_args *uap)
1414 struct thread *td = curthread;
1415 struct getvfsstat_info info;
1417 bzero(&info, sizeof(info));
1419 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1420 info.sfsp = uap->buf;
1421 info.vsfsp = uap->vbuf;
1423 info.flags = uap->flags;
1426 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1427 if (info.vsfsp && info.count > info.maxcount)
1428 uap->sysmsg_result = info.maxcount;
1430 uap->sysmsg_result = info.count;
1431 return (info.error);
1435 getvfsstat_callback(struct mount *mp, void *data)
1437 struct getvfsstat_info *info = data;
1439 struct statvfs *vsp;
1444 if (info->vsfsp && info->count < info->maxcount) {
1445 if (info->td->td_proc &&
1446 !chroot_visible_mnt(mp, info->td->td_proc)) {
1450 vsp = &mp->mnt_vstat;
1453 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1454 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1455 * overrides MNT_WAIT.
1457 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1458 (info->flags & MNT_WAIT)) &&
1459 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1462 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1464 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1465 (info->flags & MNT_WAIT)) &&
1466 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1470 if (mp->mnt_flag & MNT_RDONLY)
1471 vsp->f_flag |= ST_RDONLY;
1472 if (mp->mnt_flag & MNT_NOSUID)
1473 vsp->f_flag |= ST_NOSUID;
1475 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1477 info->error = error;
1480 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1481 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1482 kfree(freepath, M_TEMP);
1484 error = copyout(sp, info->sfsp, sizeof(*sp));
1486 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1488 info->error = error;
1500 * fchdir_args(int fd)
1502 * Change current working directory to a given file descriptor.
1505 sys_fchdir(struct fchdir_args *uap)
1507 struct thread *td = curthread;
1508 struct proc *p = td->td_proc;
1509 struct filedesc *fdp = p->p_fd;
1510 struct vnode *vp, *ovp;
1513 struct nchandle nch, onch, tnch;
1516 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1518 lwkt_gettoken(&p->p_token);
1519 vp = (struct vnode *)fp->f_data;
1521 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1522 if (fp->f_nchandle.ncp == NULL)
1525 error = checkvp_chdir(vp, td);
1530 cache_copy(&fp->f_nchandle, &nch);
1533 * If the ncp has become a mount point, traverse through
1537 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1538 (mp = cache_findmount(&nch)) != NULL
1540 error = nlookup_mp(mp, &tnch);
1542 cache_unlock(&tnch); /* leave ref intact */
1544 vp = tnch.ncp->nc_vp;
1545 error = vget(vp, LK_SHARED);
1546 KKASSERT(error == 0);
1550 cache_dropmount(mp);
1554 onch = fdp->fd_ncdir;
1555 vn_unlock(vp); /* leave ref intact */
1557 fdp->fd_ncdir = nch;
1566 lwkt_reltoken(&p->p_token);
1571 kern_chdir(struct nlookupdata *nd)
1573 struct thread *td = curthread;
1574 struct proc *p = td->td_proc;
1575 struct filedesc *fdp = p->p_fd;
1576 struct vnode *vp, *ovp;
1577 struct nchandle onch;
1580 if ((error = nlookup(nd)) != 0)
1582 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1584 if ((error = vget(vp, LK_SHARED)) != 0)
1587 lwkt_gettoken(&p->p_token);
1588 error = checkvp_chdir(vp, td);
1592 onch = fdp->fd_ncdir;
1593 cache_unlock(&nd->nl_nch); /* leave reference intact */
1594 fdp->fd_ncdir = nd->nl_nch;
1598 cache_zero(&nd->nl_nch);
1602 lwkt_reltoken(&p->p_token);
1607 * chdir_args(char *path)
1609 * Change current working directory (``.'').
1612 sys_chdir(struct chdir_args *uap)
1614 struct nlookupdata nd;
1617 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1619 error = kern_chdir(&nd);
1625 * Helper function for raised chroot(2) security function: Refuse if
1626 * any filedescriptors are open directories.
1629 chroot_refuse_vdir_fds(struct filedesc *fdp)
1636 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1637 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1639 vp = (struct vnode *)fp->f_data;
1640 if (vp->v_type != VDIR) {
1651 * This sysctl determines if we will allow a process to chroot(2) if it
1652 * has a directory open:
1653 * 0: disallowed for all processes.
1654 * 1: allowed for processes that were not already chroot(2)'ed.
1655 * 2: allowed for all processes.
1658 static int chroot_allow_open_directories = 1;
1660 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1661 &chroot_allow_open_directories, 0, "");
1664 * chroot to the specified namecache entry. We obtain the vp from the
1665 * namecache data. The passed ncp must be locked and referenced and will
1666 * remain locked and referenced on return.
1669 kern_chroot(struct nchandle *nch)
1671 struct thread *td = curthread;
1672 struct proc *p = td->td_proc;
1673 struct filedesc *fdp = p->p_fd;
1678 * Only privileged user can chroot
1680 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1685 * Disallow open directory descriptors (fchdir() breakouts).
1687 if (chroot_allow_open_directories == 0 ||
1688 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1689 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1692 if ((vp = nch->ncp->nc_vp) == NULL)
1695 if ((error = vget(vp, LK_SHARED)) != 0)
1699 * Check the validity of vp as a directory to change to and
1700 * associate it with rdir/jdir.
1702 error = checkvp_chdir(vp, td);
1703 vn_unlock(vp); /* leave reference intact */
1705 vrele(fdp->fd_rdir);
1706 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1707 cache_drop(&fdp->fd_nrdir);
1708 cache_copy(nch, &fdp->fd_nrdir);
1709 if (fdp->fd_jdir == NULL) {
1712 cache_copy(nch, &fdp->fd_njdir);
1721 * chroot_args(char *path)
1723 * Change notion of root (``/'') directory.
1726 sys_chroot(struct chroot_args *uap)
1728 struct thread *td __debugvar = curthread;
1729 struct nlookupdata nd;
1732 KKASSERT(td->td_proc);
1733 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1735 nd.nl_flags |= NLC_EXEC;
1736 error = nlookup(&nd);
1738 error = kern_chroot(&nd.nl_nch);
1745 sys_chroot_kernel(struct chroot_kernel_args *uap)
1747 struct thread *td = curthread;
1748 struct nlookupdata nd;
1749 struct nchandle *nch;
1753 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1757 error = nlookup(&nd);
1763 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1767 if ((vp = nch->ncp->nc_vp) == NULL) {
1772 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1775 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1777 vfs_cache_setroot(vp, cache_hold(nch));
1787 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1788 * determine whether it is legal to chdir to the vnode. The vnode's state
1789 * is not changed by this call.
1792 checkvp_chdir(struct vnode *vp, struct thread *td)
1796 if (vp->v_type != VDIR)
1799 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1804 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1806 struct thread *td = curthread;
1807 struct proc *p = td->td_proc;
1808 struct lwp *lp = td->td_lwp;
1809 struct filedesc *fdp = p->p_fd;
1814 int type, indx, error;
1817 if ((oflags & O_ACCMODE) == O_ACCMODE)
1819 flags = FFLAGS(oflags);
1820 error = falloc(lp, &nfp, NULL);
1824 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1827 * XXX p_dupfd is a real mess. It allows a device to return a
1828 * file descriptor to be duplicated rather then doing the open
1834 * Call vn_open() to do the lookup and assign the vnode to the
1835 * file pointer. vn_open() does not change the ref count on fp
1836 * and the vnode, on success, will be inherited by the file pointer
1839 nd->nl_flags |= NLC_LOCKVP;
1840 error = vn_open(nd, fp, flags, cmode);
1844 * handle special fdopen() case. bleh. dupfdopen() is
1845 * responsible for dropping the old contents of ofiles[indx]
1848 * Note that fsetfd() will add a ref to fp which represents
1849 * the fd_files[] assignment. We must still drop our
1852 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1853 if (fdalloc(p, 0, &indx) == 0) {
1854 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1857 fdrop(fp); /* our ref */
1860 fsetfd(fdp, NULL, indx);
1863 fdrop(fp); /* our ref */
1864 if (error == ERESTART)
1870 * ref the vnode for ourselves so it can't be ripped out from under
1871 * is. XXX need an ND flag to request that the vnode be returned
1874 * Reserve a file descriptor but do not assign it until the open
1877 vp = (struct vnode *)fp->f_data;
1879 if ((error = fdalloc(p, 0, &indx)) != 0) {
1886 * If no error occurs the vp will have been assigned to the file
1891 if (flags & (O_EXLOCK | O_SHLOCK)) {
1892 lf.l_whence = SEEK_SET;
1895 if (flags & O_EXLOCK)
1896 lf.l_type = F_WRLCK;
1898 lf.l_type = F_RDLCK;
1899 if (flags & FNONBLOCK)
1904 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1906 * lock request failed. Clean up the reserved
1910 fsetfd(fdp, NULL, indx);
1914 fp->f_flag |= FHASLOCK;
1918 * Assert that all regular file vnodes were created with a object.
1920 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1921 ("open: regular file has no backing object after vn_open"));
1927 * release our private reference, leaving the one associated with the
1928 * descriptor table intact.
1930 fsetfd(fdp, fp, indx);
1937 * open_args(char *path, int flags, int mode)
1939 * Check permissions, allocate an open file structure,
1940 * and call the device open routine if any.
1943 sys_open(struct open_args *uap)
1945 struct nlookupdata nd;
1948 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1950 error = kern_open(&nd, uap->flags,
1951 uap->mode, &uap->sysmsg_result);
1958 * openat_args(int fd, char *path, int flags, int mode)
1961 sys_openat(struct openat_args *uap)
1963 struct nlookupdata nd;
1967 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
1969 error = kern_open(&nd, uap->flags, uap->mode,
1970 &uap->sysmsg_result);
1972 nlookup_done_at(&nd, fp);
1977 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1979 struct thread *td = curthread;
1980 struct proc *p = td->td_proc;
1989 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1990 vattr.va_rmajor = rmajor;
1991 vattr.va_rminor = rminor;
1993 switch (mode & S_IFMT) {
1994 case S_IFMT: /* used by badsect to flag bad sectors */
1995 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
1996 vattr.va_type = VBAD;
1999 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2000 vattr.va_type = VCHR;
2003 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2004 vattr.va_type = VBLK;
2007 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2010 case S_IFDIR: /* special directories support for HAMMER */
2011 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2012 vattr.va_type = VDIR;
2023 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2024 if ((error = nlookup(nd)) != 0)
2026 if (nd->nl_nch.ncp->nc_vp)
2028 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2032 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2033 nd->nl_cred, NAMEI_CREATE);
2036 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2037 &vp, nd->nl_cred, &vattr);
2045 * mknod_args(char *path, int mode, int dev)
2047 * Create a special file.
2050 sys_mknod(struct mknod_args *uap)
2052 struct nlookupdata nd;
2055 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2057 error = kern_mknod(&nd, uap->mode,
2058 umajor(uap->dev), uminor(uap->dev));
2065 * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2067 * Create a special file. The path is relative to the directory associated
2071 sys_mknodat(struct mknodat_args *uap)
2073 struct nlookupdata nd;
2077 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2079 error = kern_mknod(&nd, uap->mode,
2080 umajor(uap->dev), uminor(uap->dev));
2082 nlookup_done_at(&nd, fp);
2087 kern_mkfifo(struct nlookupdata *nd, int mode)
2089 struct thread *td = curthread;
2090 struct proc *p = td->td_proc;
2097 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2098 if ((error = nlookup(nd)) != 0)
2100 if (nd->nl_nch.ncp->nc_vp)
2102 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2106 vattr.va_type = VFIFO;
2107 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2109 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2116 * mkfifo_args(char *path, int mode)
2118 * Create a named pipe.
2121 sys_mkfifo(struct mkfifo_args *uap)
2123 struct nlookupdata nd;
2126 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2128 error = kern_mkfifo(&nd, uap->mode);
2134 * mkfifoat_args(int fd, char *path, mode_t mode)
2136 * Create a named pipe. The path is relative to the directory associated
2140 sys_mkfifoat(struct mkfifoat_args *uap)
2142 struct nlookupdata nd;
2146 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2148 error = kern_mkfifo(&nd, uap->mode);
2149 nlookup_done_at(&nd, fp);
2153 static int hardlink_check_uid = 0;
2154 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2155 &hardlink_check_uid, 0,
2156 "Unprivileged processes cannot create hard links to files owned by other "
2158 static int hardlink_check_gid = 0;
2159 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2160 &hardlink_check_gid, 0,
2161 "Unprivileged processes cannot create hard links to files owned by other "
2165 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2171 * Shortcut if disabled
2173 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2177 * Privileged user can always hardlink
2179 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2183 * Otherwise only if the originating file is owned by the
2184 * same user or group. Note that any group is allowed if
2185 * the file is owned by the caller.
2187 error = VOP_GETATTR(vp, &va);
2191 if (hardlink_check_uid) {
2192 if (cred->cr_uid != va.va_uid)
2196 if (hardlink_check_gid) {
2197 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2205 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2207 struct thread *td = curthread;
2212 * Lookup the source and obtained a locked vnode.
2214 * You may only hardlink a file which you have write permission
2215 * on or which you own.
2217 * XXX relookup on vget failure / race ?
2220 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2221 if ((error = nlookup(nd)) != 0)
2223 vp = nd->nl_nch.ncp->nc_vp;
2224 KKASSERT(vp != NULL);
2225 if (vp->v_type == VDIR)
2226 return (EPERM); /* POSIX */
2227 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2229 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2233 * Unlock the source so we can lookup the target without deadlocking
2234 * (XXX vp is locked already, possible other deadlock?). The target
2237 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2238 nd->nl_flags &= ~NLC_NCPISLOCKED;
2239 cache_unlock(&nd->nl_nch);
2242 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2243 if ((error = nlookup(linknd)) != 0) {
2247 if (linknd->nl_nch.ncp->nc_vp) {
2251 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2257 * Finally run the new API VOP.
2259 error = can_hardlink(vp, td, td->td_ucred);
2261 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2262 vp, linknd->nl_cred);
2269 * link_args(char *path, char *link)
2271 * Make a hard file link.
2274 sys_link(struct link_args *uap)
2276 struct nlookupdata nd, linknd;
2279 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2281 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2283 error = kern_link(&nd, &linknd);
2284 nlookup_done(&linknd);
2291 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2299 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2300 if ((error = nlookup(nd)) != 0)
2302 if (nd->nl_nch.ncp->nc_vp)
2304 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2308 vattr.va_mode = mode;
2309 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2316 * symlink(char *path, char *link)
2318 * Make a symbolic link.
2321 sys_symlink(struct symlink_args *uap)
2323 struct thread *td = curthread;
2324 struct nlookupdata nd;
2329 path = objcache_get(namei_oc, M_WAITOK);
2330 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2332 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2334 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2335 error = kern_symlink(&nd, path, mode);
2339 objcache_put(namei_oc, path);
2344 * symlinkat_args(char *path1, int fd, char *path2)
2346 * Make a symbolic link. The path2 argument is relative to the directory
2347 * associated with fd.
2350 sys_symlinkat(struct symlinkat_args *uap)
2352 struct thread *td = curthread;
2353 struct nlookupdata nd;
2359 path1 = objcache_get(namei_oc, M_WAITOK);
2360 error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2362 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2365 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2366 error = kern_symlink(&nd, path1, mode);
2368 nlookup_done_at(&nd, fp);
2370 objcache_put(namei_oc, path1);
2375 * undelete_args(char *path)
2377 * Delete a whiteout from the filesystem.
2380 sys_undelete(struct undelete_args *uap)
2382 struct nlookupdata nd;
2385 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2387 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2389 error = nlookup(&nd);
2391 error = ncp_writechk(&nd.nl_nch);
2393 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2401 kern_unlink(struct nlookupdata *nd)
2406 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2407 if ((error = nlookup(nd)) != 0)
2409 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2411 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2416 * unlink_args(char *path)
2418 * Delete a name from the filesystem.
2421 sys_unlink(struct unlink_args *uap)
2423 struct nlookupdata nd;
2426 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2428 error = kern_unlink(&nd);
2435 * unlinkat_args(int fd, char *path, int flags)
2437 * Delete the file or directory entry pointed to by fd/path.
2440 sys_unlinkat(struct unlinkat_args *uap)
2442 struct nlookupdata nd;
2446 if (uap->flags & ~AT_REMOVEDIR)
2449 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2451 if (uap->flags & AT_REMOVEDIR)
2452 error = kern_rmdir(&nd);
2454 error = kern_unlink(&nd);
2456 nlookup_done_at(&nd, fp);
2461 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2463 struct thread *td = curthread;
2464 struct proc *p = td->td_proc;
2471 fp = holdfp(p->p_fd, fd, -1);
2474 if (fp->f_type != DTYPE_VNODE) {
2478 vp = (struct vnode *)fp->f_data;
2482 spin_lock(&fp->f_spin);
2483 new_offset = fp->f_offset + offset;
2487 error = VOP_GETATTR(vp, &vattr);
2488 spin_lock(&fp->f_spin);
2489 new_offset = offset + vattr.va_size;
2492 new_offset = offset;
2494 spin_lock(&fp->f_spin);
2499 spin_lock(&fp->f_spin);
2504 * Validate the seek position. Negative offsets are not allowed
2505 * for regular files or directories.
2507 * Normally we would also not want to allow negative offsets for
2508 * character and block-special devices. However kvm addresses
2509 * on 64 bit architectures might appear to be negative and must
2513 if (new_offset < 0 &&
2514 (vp->v_type == VREG || vp->v_type == VDIR)) {
2517 fp->f_offset = new_offset;
2520 *res = fp->f_offset;
2521 spin_unlock(&fp->f_spin);
2528 * lseek_args(int fd, int pad, off_t offset, int whence)
2530 * Reposition read/write file offset.
2533 sys_lseek(struct lseek_args *uap)
2537 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2538 &uap->sysmsg_offset);
2544 * Check if current process can access given file. amode is a bitmask of *_OK
2545 * access bits. flags is a bitmask of AT_* flags.
2548 kern_access(struct nlookupdata *nd, int amode, int flags)
2553 if (flags & ~AT_EACCESS)
2555 if ((error = nlookup(nd)) != 0)
2558 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2562 /* Flags == 0 means only check for existence. */
2571 if ((mode & VWRITE) == 0 ||
2572 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2573 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2576 * If the file handle is stale we have to re-resolve the
2577 * entry. This is a hack at the moment.
2579 if (error == ESTALE) {
2581 cache_setunresolved(&nd->nl_nch);
2582 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2595 * access_args(char *path, int flags)
2597 * Check access permissions.
2600 sys_access(struct access_args *uap)
2602 struct nlookupdata nd;
2605 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2607 error = kern_access(&nd, uap->flags, 0);
2614 * faccessat_args(int fd, char *path, int amode, int flags)
2616 * Check access permissions.
2619 sys_faccessat(struct faccessat_args *uap)
2621 struct nlookupdata nd;
2625 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2628 error = kern_access(&nd, uap->amode, uap->flags);
2629 nlookup_done_at(&nd, fp);
2635 kern_stat(struct nlookupdata *nd, struct stat *st)
2640 if ((error = nlookup(nd)) != 0)
2643 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2646 if ((error = vget(vp, LK_SHARED)) != 0)
2648 error = vn_stat(vp, st, nd->nl_cred);
2651 * If the file handle is stale we have to re-resolve the entry. This
2652 * is a hack at the moment.
2654 if (error == ESTALE) {
2656 cache_setunresolved(&nd->nl_nch);
2657 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2667 * stat_args(char *path, struct stat *ub)
2669 * Get file status; this version follows links.
2672 sys_stat(struct stat_args *uap)
2674 struct nlookupdata nd;
2678 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2680 error = kern_stat(&nd, &st);
2682 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2689 * lstat_args(char *path, struct stat *ub)
2691 * Get file status; this version does not follow links.
2694 sys_lstat(struct lstat_args *uap)
2696 struct nlookupdata nd;
2700 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2702 error = kern_stat(&nd, &st);
2704 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2711 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2713 * Get status of file pointed to by fd/path.
2716 sys_fstatat(struct fstatat_args *uap)
2718 struct nlookupdata nd;
2724 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2727 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2729 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2730 UIO_USERSPACE, flags);
2732 error = kern_stat(&nd, &st);
2734 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2736 nlookup_done_at(&nd, fp);
2741 * pathconf_Args(char *path, int name)
2743 * Get configurable pathname variables.
2746 sys_pathconf(struct pathconf_args *uap)
2748 struct nlookupdata nd;
2753 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2755 error = nlookup(&nd);
2757 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2760 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2768 * kern_readlink isn't properly split yet. There is a copyin burried
2769 * in VOP_READLINK().
2772 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2774 struct thread *td = curthread;
2780 if ((error = nlookup(nd)) != 0)
2782 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2785 if (vp->v_type != VLNK) {
2788 aiov.iov_base = buf;
2789 aiov.iov_len = count;
2790 auio.uio_iov = &aiov;
2791 auio.uio_iovcnt = 1;
2792 auio.uio_offset = 0;
2793 auio.uio_rw = UIO_READ;
2794 auio.uio_segflg = UIO_USERSPACE;
2796 auio.uio_resid = count;
2797 error = VOP_READLINK(vp, &auio, td->td_ucred);
2800 *res = count - auio.uio_resid;
2805 * readlink_args(char *path, char *buf, int count)
2807 * Return target name of a symbolic link.
2810 sys_readlink(struct readlink_args *uap)
2812 struct nlookupdata nd;
2815 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2817 error = kern_readlink(&nd, uap->buf, uap->count,
2818 &uap->sysmsg_result);
2825 * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
2827 * Return target name of a symbolic link. The path is relative to the
2828 * directory associated with fd.
2831 sys_readlinkat(struct readlinkat_args *uap)
2833 struct nlookupdata nd;
2837 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2839 error = kern_readlink(&nd, uap->buf, uap->bufsize,
2840 &uap->sysmsg_result);
2842 nlookup_done_at(&nd, fp);
2847 setfflags(struct vnode *vp, int flags)
2849 struct thread *td = curthread;
2854 * Prevent non-root users from setting flags on devices. When
2855 * a device is reused, users can retain ownership of the device
2856 * if they are allowed to set flags and programs assume that
2857 * chown can't fail when done as root.
2859 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2860 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2864 * note: vget is required for any operation that might mod the vnode
2865 * so VINACTIVE is properly cleared.
2867 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2869 vattr.va_flags = flags;
2870 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2877 * chflags(char *path, int flags)
2879 * Change flags of a file given a path name.
2882 sys_chflags(struct chflags_args *uap)
2884 struct nlookupdata nd;
2889 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2891 error = nlookup(&nd);
2893 error = ncp_writechk(&nd.nl_nch);
2895 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2898 error = setfflags(vp, uap->flags);
2905 * lchflags(char *path, int flags)
2907 * Change flags of a file given a path name, but don't follow symlinks.
2910 sys_lchflags(struct lchflags_args *uap)
2912 struct nlookupdata nd;
2917 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2919 error = nlookup(&nd);
2921 error = ncp_writechk(&nd.nl_nch);
2923 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2926 error = setfflags(vp, uap->flags);
2933 * fchflags_args(int fd, int flags)
2935 * Change flags of a file given a file descriptor.
2938 sys_fchflags(struct fchflags_args *uap)
2940 struct thread *td = curthread;
2941 struct proc *p = td->td_proc;
2945 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2947 if (fp->f_nchandle.ncp)
2948 error = ncp_writechk(&fp->f_nchandle);
2950 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2956 setfmode(struct vnode *vp, int mode)
2958 struct thread *td = curthread;
2963 * note: vget is required for any operation that might mod the vnode
2964 * so VINACTIVE is properly cleared.
2966 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2968 vattr.va_mode = mode & ALLPERMS;
2969 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2976 kern_chmod(struct nlookupdata *nd, int mode)
2981 if ((error = nlookup(nd)) != 0)
2983 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2985 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2986 error = setfmode(vp, mode);
2992 * chmod_args(char *path, int mode)
2994 * Change mode of a file given path name.
2997 sys_chmod(struct chmod_args *uap)
2999 struct nlookupdata nd;
3002 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3004 error = kern_chmod(&nd, uap->mode);
3010 * lchmod_args(char *path, int mode)
3012 * Change mode of a file given path name (don't follow links.)
3015 sys_lchmod(struct lchmod_args *uap)
3017 struct nlookupdata nd;
3020 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3022 error = kern_chmod(&nd, uap->mode);
3028 * fchmod_args(int fd, int mode)
3030 * Change mode of a file given a file descriptor.
3033 sys_fchmod(struct fchmod_args *uap)
3035 struct thread *td = curthread;
3036 struct proc *p = td->td_proc;
3040 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3042 if (fp->f_nchandle.ncp)
3043 error = ncp_writechk(&fp->f_nchandle);
3045 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3051 * fchmodat_args(char *path, int mode)
3053 * Change mode of a file pointed to by fd/path.
3056 sys_fchmodat(struct fchmodat_args *uap)
3058 struct nlookupdata nd;
3063 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3065 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3067 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3068 UIO_USERSPACE, flags);
3070 error = kern_chmod(&nd, uap->mode);
3071 nlookup_done_at(&nd, fp);
3076 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3078 struct thread *td = curthread;
3086 * note: vget is required for any operation that might mod the vnode
3087 * so VINACTIVE is properly cleared.
3089 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3090 if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3092 o_uid = vattr.va_uid;
3093 o_gid = vattr.va_gid;
3094 size = vattr.va_size;
3099 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3108 VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3109 VFS_ACCOUNT(mp, uid, gid, size);
3116 kern_chown(struct nlookupdata *nd, int uid, int gid)
3121 if ((error = nlookup(nd)) != 0)
3123 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3125 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3126 error = setfown(nd->nl_nch.mount, vp, uid, gid);
3132 * chown(char *path, int uid, int gid)
3134 * Set ownership given a path name.
3137 sys_chown(struct chown_args *uap)
3139 struct nlookupdata nd;
3142 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3144 error = kern_chown(&nd, uap->uid, uap->gid);
3150 * lchown_args(char *path, int uid, int gid)
3152 * Set ownership given a path name, do not cross symlinks.
3155 sys_lchown(struct lchown_args *uap)
3157 struct nlookupdata nd;
3160 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3162 error = kern_chown(&nd, uap->uid, uap->gid);
3168 * fchown_args(int fd, int uid, int gid)
3170 * Set ownership given a file descriptor.
3173 sys_fchown(struct fchown_args *uap)
3175 struct thread *td = curthread;
3176 struct proc *p = td->td_proc;
3180 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3182 if (fp->f_nchandle.ncp)
3183 error = ncp_writechk(&fp->f_nchandle);
3185 error = setfown(p->p_fd->fd_ncdir.mount,
3186 (struct vnode *)fp->f_data, uap->uid, uap->gid);
3192 * fchownat(int fd, char *path, int uid, int gid, int flags)
3194 * Set ownership of file pointed to by fd/path.
3197 sys_fchownat(struct fchownat_args *uap)
3199 struct nlookupdata nd;
3204 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3206 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3208 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3209 UIO_USERSPACE, flags);
3211 error = kern_chown(&nd, uap->uid, uap->gid);
3212 nlookup_done_at(&nd, fp);
3218 getutimes(const struct timeval *tvp, struct timespec *tsp)
3220 struct timeval tv[2];
3224 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3227 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3228 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3234 setutimes(struct vnode *vp, struct vattr *vattr,
3235 const struct timespec *ts, int nullflag)
3237 struct thread *td = curthread;
3241 vattr->va_atime = ts[0];
3242 vattr->va_mtime = ts[1];
3244 vattr->va_vaflags |= VA_UTIMES_NULL;
3245 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3251 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3253 struct timespec ts[2];
3258 if ((error = getutimes(tptr, ts)) != 0)
3262 * NOTE: utimes() succeeds for the owner even if the file
3263 * is not user-writable.
3265 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3267 if ((error = nlookup(nd)) != 0)
3269 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3271 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3275 * note: vget is required for any operation that might mod the vnode
3276 * so VINACTIVE is properly cleared.
3278 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3279 error = vget(vp, LK_EXCLUSIVE);
3281 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3290 * utimes_args(char *path, struct timeval *tptr)
3292 * Set the access and modification times of a file.
3295 sys_utimes(struct utimes_args *uap)
3297 struct timeval tv[2];
3298 struct nlookupdata nd;
3302 error = copyin(uap->tptr, tv, sizeof(tv));
3306 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3308 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3314 * lutimes_args(char *path, struct timeval *tptr)
3316 * Set the access and modification times of a file.
3319 sys_lutimes(struct lutimes_args *uap)
3321 struct timeval tv[2];
3322 struct nlookupdata nd;
3326 error = copyin(uap->tptr, tv, sizeof(tv));
3330 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3332 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3338 * Set utimes on a file descriptor. The creds used to open the
3339 * file are used to determine whether the operation is allowed
3343 kern_futimes(int fd, struct timeval *tptr)
3345 struct thread *td = curthread;
3346 struct proc *p = td->td_proc;
3347 struct timespec ts[2];
3353 error = getutimes(tptr, ts);
3356 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3358 if (fp->f_nchandle.ncp)
3359 error = ncp_writechk(&fp->f_nchandle);
3362 error = vget(vp, LK_EXCLUSIVE);
3364 error = VOP_GETATTR(vp, &vattr);
3366 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3370 error = setutimes(vp, &vattr, ts,
3381 * futimes_args(int fd, struct timeval *tptr)
3383 * Set the access and modification times of a file.
3386 sys_futimes(struct futimes_args *uap)
3388 struct timeval tv[2];
3392 error = copyin(uap->tptr, tv, sizeof(tv));
3396 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3402 kern_truncate(struct nlookupdata *nd, off_t length)
3409 uint64_t old_size = 0;
3413 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3414 if ((error = nlookup(nd)) != 0)
3416 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3418 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3420 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3424 if (vp->v_type == VDIR) {
3428 if (vfs_accounting_enabled) {
3429 error = VOP_GETATTR(vp, &vattr);
3430 KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3433 old_size = vattr.va_size;
3436 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3438 vattr.va_size = length;
3439 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3440 VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3448 * truncate(char *path, int pad, off_t length)
3450 * Truncate a file given its path name.
3453 sys_truncate(struct truncate_args *uap)
3455 struct nlookupdata nd;
3458 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3460 error = kern_truncate(&nd, uap->length);
3466 kern_ftruncate(int fd, off_t length)
3468 struct thread *td = curthread;
3469 struct proc *p = td->td_proc;
3476 uint64_t old_size = 0;
3481 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3483 if (fp->f_nchandle.ncp) {
3484 error = ncp_writechk(&fp->f_nchandle);
3488 if ((fp->f_flag & FWRITE) == 0) {
3492 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3496 vp = (struct vnode *)fp->f_data;
3497 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3498 if (vp->v_type == VDIR) {
3503 if (vfs_accounting_enabled) {
3504 error = VOP_GETATTR(vp, &vattr);
3505 KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
3508 old_size = vattr.va_size;
3511 if ((error = vn_writechk(vp, NULL)) == 0) {
3513 vattr.va_size = length;
3514 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3516 VFS_ACCOUNT(mp, uid, gid, length - old_size);
3525 * ftruncate_args(int fd, int pad, off_t length)
3527 * Truncate a file given a file descriptor.
3530 sys_ftruncate(struct ftruncate_args *uap)
3534 error = kern_ftruncate(uap->fd, uap->length);
3542 * Sync an open file.
3545 sys_fsync(struct fsync_args *uap)
3547 struct thread *td = curthread;
3548 struct proc *p = td->td_proc;
3554 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3556 vp = (struct vnode *)fp->f_data;
3557 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3558 if ((obj = vp->v_object) != NULL)
3559 vm_object_page_clean(obj, 0, 0, 0);
3560 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3561 if (error == 0 && vp->v_mount)
3562 error = buf_fsync(vp);
3570 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3572 struct nchandle fnchd;
3573 struct nchandle tnchd;
3574 struct namecache *ncp;
3581 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3582 if ((error = nlookup(fromnd)) != 0)
3584 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3586 fnchd.mount = fromnd->nl_nch.mount;
3590 * unlock the source nch so we can lookup the target nch without
3591 * deadlocking. The target may or may not exist so we do not check
3592 * for a target vp like kern_mkdir() and other creation functions do.
3594 * The source and target directories are ref'd and rechecked after
3595 * everything is relocked to determine if the source or target file
3598 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3599 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3600 cache_unlock(&fromnd->nl_nch);
3602 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3603 if ((error = nlookup(tond)) != 0) {
3607 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3611 tnchd.mount = tond->nl_nch.mount;
3615 * If the source and target are the same there is nothing to do
3617 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3624 * Mount points cannot be renamed or overwritten
3626 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3635 * Relock the source ncp. cache_relock() will deal with any
3636 * deadlocks against the already-locked tond and will also
3637 * make sure both are resolved.
3639 * NOTE AFTER RELOCKING: The source or target ncp may have become
3640 * invalid while they were unlocked, nc_vp and nc_mount could
3643 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3644 &tond->nl_nch, tond->nl_cred);
3645 fromnd->nl_flags |= NLC_NCPISLOCKED;
3648 * make sure the parent directories linkages are the same
3650 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3651 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3658 * Both the source and target must be within the same filesystem and
3659 * in the same filesystem as their parent directories within the
3660 * namecache topology.
3662 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3665 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3666 mp != tond->nl_nch.mount) {
3673 * Make sure the mount point is writable
3675 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3682 * If the target exists and either the source or target is a directory,
3683 * then both must be directories.
3685 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3688 if (tond->nl_nch.ncp->nc_vp) {
3689 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3691 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3692 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3694 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3700 * You cannot rename a source into itself or a subdirectory of itself.
3701 * We check this by travsersing the target directory upwards looking
3702 * for a match against the source.
3707 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3708 if (fromnd->nl_nch.ncp == ncp) {
3719 * Even though the namespaces are different, they may still represent
3720 * hardlinks to the same file. The filesystem might have a hard time
3721 * with this so we issue a NREMOVE of the source instead of a NRENAME
3722 * when we detect the situation.
3725 fdvp = fromnd->nl_dvp;
3726 tdvp = tond->nl_dvp;
3727 if (fdvp == NULL || tdvp == NULL) {
3729 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3730 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3733 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3734 fdvp, tdvp, tond->nl_cred);
3741 * rename_args(char *from, char *to)
3743 * Rename files. Source and destination must either both be directories,
3744 * or both not be directories. If target is a directory, it must be empty.
3747 sys_rename(struct rename_args *uap)
3749 struct nlookupdata fromnd, tond;
3752 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3754 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3756 error = kern_rename(&fromnd, &tond);
3757 nlookup_done(&tond);
3759 nlookup_done(&fromnd);
3764 * renameat_args(int oldfd, char *old, int newfd, char *new)
3766 * Rename files using paths relative to the directories associated with
3767 * oldfd and newfd. Source and destination must either both be directories,
3768 * or both not be directories. If target is a directory, it must be empty.
3771 sys_renameat(struct renameat_args *uap)
3773 struct nlookupdata oldnd, newnd;
3774 struct file *oldfp, *newfp;
3777 error = nlookup_init_at(&oldnd, &oldfp, uap->oldfd, uap->old,
3780 error = nlookup_init_at(&newnd, &newfp, uap->newfd, uap->new,
3783 error = kern_rename(&oldnd, &newnd);
3784 nlookup_done_at(&newnd, newfp);
3786 nlookup_done_at(&oldnd, oldfp);
3791 kern_mkdir(struct nlookupdata *nd, int mode)
3793 struct thread *td = curthread;
3794 struct proc *p = td->td_proc;
3800 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3801 if ((error = nlookup(nd)) != 0)
3804 if (nd->nl_nch.ncp->nc_vp)
3806 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3809 vattr.va_type = VDIR;
3810 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3813 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3820 * mkdir_args(char *path, int mode)
3822 * Make a directory file.
3825 sys_mkdir(struct mkdir_args *uap)
3827 struct nlookupdata nd;
3830 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3832 error = kern_mkdir(&nd, uap->mode);
3838 * mkdirat_args(int fd, char *path, mode_t mode)
3840 * Make a directory file. The path is relative to the directory associated
3844 sys_mkdirat(struct mkdirat_args *uap)
3846 struct nlookupdata nd;
3850 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3852 error = kern_mkdir(&nd, uap->mode);
3853 nlookup_done_at(&nd, fp);
3858 kern_rmdir(struct nlookupdata *nd)
3863 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3864 if ((error = nlookup(nd)) != 0)
3868 * Do not allow directories representing mount points to be
3869 * deleted, even if empty. Check write perms on mount point
3870 * in case the vnode is aliased (aka nullfs).
3872 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3874 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3876 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3881 * rmdir_args(char *path)
3883 * Remove a directory file.
3886 sys_rmdir(struct rmdir_args *uap)
3888 struct nlookupdata nd;
3891 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3893 error = kern_rmdir(&nd);
3899 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3900 enum uio_seg direction)
3902 struct thread *td = curthread;
3903 struct proc *p = td->td_proc;
3911 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3913 if ((fp->f_flag & FREAD) == 0) {
3917 vp = (struct vnode *)fp->f_data;
3919 if (vp->v_type != VDIR) {
3923 aiov.iov_base = buf;
3924 aiov.iov_len = count;
3925 auio.uio_iov = &aiov;
3926 auio.uio_iovcnt = 1;
3927 auio.uio_rw = UIO_READ;
3928 auio.uio_segflg = direction;
3930 auio.uio_resid = count;
3931 loff = auio.uio_offset = fp->f_offset;
3932 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3933 fp->f_offset = auio.uio_offset;
3936 if (count == auio.uio_resid) {
3937 if (union_dircheckp) {
3938 error = union_dircheckp(td, &vp, fp);
3945 if ((vp->v_flag & VROOT) &&
3946 (vp->v_mount->mnt_flag & MNT_UNION)) {
3947 struct vnode *tvp = vp;
3948 vp = vp->v_mount->mnt_vnodecovered;
3959 * WARNING! *basep may not be wide enough to accomodate the
3960 * seek offset. XXX should we hack this to return the upper 32 bits
3961 * for offsets greater then 4G?
3964 *basep = (long)loff;
3966 *res = count - auio.uio_resid;
3973 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3975 * Read a block of directory entries in a file system independent format.
3978 sys_getdirentries(struct getdirentries_args *uap)
3983 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3984 &uap->sysmsg_result, UIO_USERSPACE);
3986 if (error == 0 && uap->basep)
3987 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3992 * getdents_args(int fd, char *buf, size_t count)
3995 sys_getdents(struct getdents_args *uap)
3999 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4000 &uap->sysmsg_result, UIO_USERSPACE);
4006 * Set the mode mask for creation of filesystem nodes.
4008 * umask(int newmask)
4011 sys_umask(struct umask_args *uap)
4013 struct thread *td = curthread;
4014 struct proc *p = td->td_proc;
4015 struct filedesc *fdp;
4018 uap->sysmsg_result = fdp->fd_cmask;
4019 fdp->fd_cmask = uap->newmask & ALLPERMS;
4024 * revoke(char *path)
4026 * Void all references to file by ripping underlying filesystem
4030 sys_revoke(struct revoke_args *uap)
4032 struct nlookupdata nd;
4039 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4041 error = nlookup(&nd);
4043 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4044 cred = crhold(nd.nl_cred);
4048 error = VOP_GETATTR(vp, &vattr);
4049 if (error == 0 && cred->cr_uid != vattr.va_uid)
4050 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4051 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4053 error = vrevoke(vp, cred);
4054 } else if (error == 0) {
4055 error = vrevoke(vp, cred);
4065 * getfh_args(char *fname, fhandle_t *fhp)
4067 * Get (NFS) file handle
4069 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4070 * mount. This allows nullfs mounts to be explicitly exported.
4072 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4074 * nullfs mounts of subdirectories are not safe. That is, it will
4075 * work, but you do not really have protection against access to
4076 * the related parent directories.
4079 sys_getfh(struct getfh_args *uap)
4081 struct thread *td = curthread;
4082 struct nlookupdata nd;
4089 * Must be super user
4091 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4095 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4097 error = nlookup(&nd);
4099 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4100 mp = nd.nl_nch.mount;
4103 bzero(&fh, sizeof(fh));
4104 fh.fh_fsid = mp->mnt_stat.f_fsid;
4105 error = VFS_VPTOFH(vp, &fh.fh_fid);
4108 error = copyout(&fh, uap->fhp, sizeof(fh));
4114 * fhopen_args(const struct fhandle *u_fhp, int flags)
4116 * syscall for the rpc.lockd to use to translate a NFS file handle into
4117 * an open descriptor.
4119 * warning: do not remove the priv_check() call or this becomes one giant
4123 sys_fhopen(struct fhopen_args *uap)
4125 struct thread *td = curthread;
4126 struct filedesc *fdp = td->td_proc->p_fd;
4131 struct vattr *vap = &vat;
4133 int fmode, mode, error, type;
4139 * Must be super user
4141 error = priv_check(td, PRIV_ROOT);
4145 fmode = FFLAGS(uap->flags);
4148 * Why not allow a non-read/write open for our lockd?
4150 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4152 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4157 * Find the mount point
4159 mp = vfs_getvfs(&fhp.fh_fsid);
4164 /* now give me my vnode, it gets returned to me locked */
4165 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4169 * from now on we have to make sure not
4170 * to forget about the vnode
4171 * any error that causes an abort must vput(vp)
4172 * just set error = err and 'goto bad;'.
4178 if (vp->v_type == VLNK) {
4182 if (vp->v_type == VSOCK) {
4187 if (fmode & (FWRITE | O_TRUNC)) {
4188 if (vp->v_type == VDIR) {
4192 error = vn_writechk(vp, NULL);
4200 error = VOP_ACCESS(vp, mode, td->td_ucred);
4204 if (fmode & O_TRUNC) {
4205 vn_unlock(vp); /* XXX */
4206 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4209 error = VOP_SETATTR(vp, vap, td->td_ucred);
4215 * VOP_OPEN needs the file pointer so it can potentially override
4218 * WARNING! no f_nchandle will be associated when fhopen()ing a
4221 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4225 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4228 * setting f_ops this way prevents VOP_CLOSE from being
4229 * called or fdrop() releasing the vp from v_data. Since
4230 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4232 fp->f_ops = &badfileops;
4238 * The fp is given its own reference, we still have our ref and lock.
4240 * Assert that all regular files must be created with a VM object.
4242 if (vp->v_type == VREG && vp->v_object == NULL) {
4243 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4248 * The open was successful. Handle any locking requirements.
4250 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4251 lf.l_whence = SEEK_SET;
4254 if (fmode & O_EXLOCK)
4255 lf.l_type = F_WRLCK;
4257 lf.l_type = F_RDLCK;
4258 if (fmode & FNONBLOCK)
4263 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4265 * release our private reference.
4267 fsetfd(fdp, NULL, indx);
4272 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4273 fp->f_flag |= FHASLOCK;
4277 * Clean up. Associate the file pointer with the previously
4278 * reserved descriptor and return it.
4281 fsetfd(fdp, fp, indx);
4283 uap->sysmsg_result = indx;
4287 fsetfd(fdp, NULL, indx);
4296 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4299 sys_fhstat(struct fhstat_args *uap)
4301 struct thread *td = curthread;
4309 * Must be super user
4311 error = priv_check(td, PRIV_ROOT);
4315 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4319 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4322 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4323 error = vn_stat(vp, &sb, td->td_ucred);
4328 error = copyout(&sb, uap->sb, sizeof(sb));
4333 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4336 sys_fhstatfs(struct fhstatfs_args *uap)
4338 struct thread *td = curthread;
4339 struct proc *p = td->td_proc;
4344 char *fullpath, *freepath;
4349 * Must be super user
4351 if ((error = priv_check(td, PRIV_ROOT)))
4354 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4357 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4361 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4366 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4371 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4374 error = mount_path(p, mp, &fullpath, &freepath);
4377 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4378 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4379 kfree(freepath, M_TEMP);
4381 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4382 if (priv_check(td, PRIV_ROOT)) {
4383 bcopy(sp, &sb, sizeof(sb));
4384 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4387 error = copyout(sp, uap->buf, sizeof(*sp));
4393 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4396 sys_fhstatvfs(struct fhstatvfs_args *uap)
4398 struct thread *td = curthread;
4399 struct proc *p = td->td_proc;
4407 * Must be super user
4409 if ((error = priv_check(td, PRIV_ROOT)))
4412 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4415 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4419 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4424 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4427 sp = &mp->mnt_vstat;
4429 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4433 if (mp->mnt_flag & MNT_RDONLY)
4434 sp->f_flag |= ST_RDONLY;
4435 if (mp->mnt_flag & MNT_NOSUID)
4436 sp->f_flag |= ST_NOSUID;
4437 error = copyout(sp, uap->buf, sizeof(*sp));
4444 * Syscall to push extended attribute configuration information into the
4445 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4446 * a command (int cmd), and attribute name and misc data. For now, the
4447 * attribute name is left in userspace for consumption by the VFS_op.
4448 * It will probably be changed to be copied into sysspace by the
4449 * syscall in the future, once issues with various consumers of the
4450 * attribute code have raised their hands.
4452 * Currently this is used only by UFS Extended Attributes.
4455 sys_extattrctl(struct extattrctl_args *uap)
4457 struct nlookupdata nd;
4459 char attrname[EXTATTR_MAXNAMELEN];
4467 if (error == 0 && uap->filename) {
4468 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4471 error = nlookup(&nd);
4473 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4477 if (error == 0 && uap->attrname) {
4478 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4483 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4485 error = nlookup(&nd);
4487 error = ncp_writechk(&nd.nl_nch);
4489 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4491 uap->attrname, nd.nl_cred);
4500 * Syscall to get a named extended attribute on a file or directory.
4503 sys_extattr_set_file(struct extattr_set_file_args *uap)
4505 char attrname[EXTATTR_MAXNAMELEN];
4506 struct nlookupdata nd;
4512 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4518 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4520 error = nlookup(&nd);
4522 error = ncp_writechk(&nd.nl_nch);
4524 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4530 bzero(&auio, sizeof(auio));
4531 aiov.iov_base = uap->data;
4532 aiov.iov_len = uap->nbytes;
4533 auio.uio_iov = &aiov;
4534 auio.uio_iovcnt = 1;
4535 auio.uio_offset = 0;
4536 auio.uio_resid = uap->nbytes;
4537 auio.uio_rw = UIO_WRITE;
4538 auio.uio_td = curthread;
4540 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4549 * Syscall to get a named extended attribute on a file or directory.
4552 sys_extattr_get_file(struct extattr_get_file_args *uap)
4554 char attrname[EXTATTR_MAXNAMELEN];
4555 struct nlookupdata nd;
4561 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4567 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4569 error = nlookup(&nd);
4571 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4577 bzero(&auio, sizeof(auio));
4578 aiov.iov_base = uap->data;
4579 aiov.iov_len = uap->nbytes;
4580 auio.uio_iov = &aiov;
4581 auio.uio_iovcnt = 1;
4582 auio.uio_offset = 0;
4583 auio.uio_resid = uap->nbytes;
4584 auio.uio_rw = UIO_READ;
4585 auio.uio_td = curthread;
4587 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4589 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4597 * Syscall to delete a named extended attribute from a file or directory.
4598 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4601 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4603 char attrname[EXTATTR_MAXNAMELEN];
4604 struct nlookupdata nd;
4608 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4612 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4614 error = nlookup(&nd);
4616 error = ncp_writechk(&nd.nl_nch);
4618 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4620 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4621 attrname, NULL, nd.nl_cred);
4630 * Determine if the mount is visible to the process.
4633 chroot_visible_mnt(struct mount *mp, struct proc *p)
4635 struct nchandle nch;
4638 * Traverse from the mount point upwards. If we hit the process
4639 * root then the mount point is visible to the process.
4641 nch = mp->mnt_ncmountpt;
4643 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4644 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4647 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4648 nch = nch.mount->mnt_ncmounton;
4650 nch.ncp = nch.ncp->nc_parent;
4655 * If the mount point is not visible to the process, but the
4656 * process root is in a subdirectory of the mount, return
4659 if (p->p_fd->fd_nrdir.mount == mp)