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, ...);
86 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
87 static int checkvp_chdir (struct vnode *vn, struct thread *td);
88 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
89 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
90 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
91 static int getutimes (const struct timeval *, struct timespec *);
92 static int setfown (struct vnode *, uid_t, gid_t);
93 static int setfmode (struct vnode *, int);
94 static int setfflags (struct vnode *, int);
95 static int setutimes (struct vnode *, struct vattr *,
96 const struct timespec *, int);
97 static int usermount = 0; /* if 1, non-root can mount fs. */
99 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
101 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
104 * Virtual File System System Calls
108 * Mount a file system.
110 * mount_args(char *type, char *path, int flags, caddr_t data)
115 sys_mount(struct mount_args *uap)
117 struct thread *td = curthread;
120 struct mount *mp, *nullmp;
121 struct vfsconf *vfsp;
122 int error, flag = 0, flag2 = 0;
125 struct nlookupdata nd;
126 char fstypename[MFSNAMELEN];
135 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
139 * Do not allow NFS export by non-root users.
141 if (uap->flags & MNT_EXPORTED) {
142 error = priv_check(td, PRIV_ROOT);
147 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
149 if (priv_check(td, PRIV_ROOT))
150 uap->flags |= MNT_NOSUID | MNT_NODEV;
153 * Lookup the requested path and extract the nch and vnode.
155 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
157 if ((error = nlookup(&nd)) == 0) {
158 if (nd.nl_nch.ncp->nc_vp == NULL)
168 * If the target filesystem is resolved via a nullfs mount, then
169 * nd.nl_nch.mount will be pointing to the nullfs mount structure
170 * instead of the target file system. We need it in case we are
173 nullmp = nd.nl_nch.mount;
176 * Extract the locked+refd ncp and cleanup the nd structure
179 cache_zero(&nd.nl_nch);
182 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
189 * now we have the locked ref'd nch and unreferenced vnode.
192 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
199 * Extract the file system type. We need to know this early, to take
200 * appropriate actions if we are dealing with a nullfs.
202 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
209 * Now we have an unlocked ref'd nch and a locked ref'd vp
211 if (uap->flags & MNT_UPDATE) {
212 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
219 if (strncmp(fstypename, "null", 5) == 0) {
227 flag2 = mp->mnt_kern_flag;
229 * We only allow the filesystem to be reloaded if it
230 * is currently mounted read-only.
232 if ((uap->flags & MNT_RELOAD) &&
233 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
236 error = EOPNOTSUPP; /* Needs translation */
240 * Only root, or the user that did the original mount is
241 * permitted to update it.
243 if (mp->mnt_stat.f_owner != cred->cr_uid &&
244 (error = priv_check(td, PRIV_ROOT))) {
249 if (vfs_busy(mp, LK_NOWAIT)) {
255 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
262 vsetflags(vp, VMOUNT);
264 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
269 * If the user is not root, ensure that they own the directory
270 * onto which we are attempting to mount.
272 if ((error = VOP_GETATTR(vp, &va)) ||
273 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
278 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
283 if (vp->v_type != VDIR) {
289 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
295 vfsp = vfsconf_find_by_name(fstypename);
299 /* Only load modules for root (very important!) */
300 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
305 error = linker_load_file(fstypename, &lf);
306 if (error || lf == NULL) {
314 /* lookup again, see if the VFS was loaded */
315 vfsp = vfsconf_find_by_name(fstypename);
318 linker_file_unload(lf);
325 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
331 vsetflags(vp, VMOUNT);
334 * Allocate and initialize the filesystem.
336 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
338 vfs_busy(mp, LK_NOWAIT);
339 mp->mnt_op = vfsp->vfc_vfsops;
341 vfsp->vfc_refcount++;
342 mp->mnt_stat.f_type = vfsp->vfc_typenum;
343 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
344 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
345 mp->mnt_stat.f_owner = cred->cr_uid;
349 * Set the mount level flags.
351 if (uap->flags & MNT_RDONLY)
352 mp->mnt_flag |= MNT_RDONLY;
353 else if (mp->mnt_flag & MNT_RDONLY)
354 mp->mnt_kern_flag |= MNTK_WANTRDWR;
355 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
356 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
357 MNT_NOSYMFOLLOW | MNT_IGNORE |
358 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
359 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
360 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
361 MNT_NOSYMFOLLOW | MNT_IGNORE |
362 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
364 * Mount the filesystem.
365 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
368 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
369 if (mp->mnt_flag & MNT_UPDATE) {
370 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
371 mp->mnt_flag &= ~MNT_RDONLY;
372 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
373 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
376 mp->mnt_kern_flag = flag2;
379 vclrflags(vp, VMOUNT);
384 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
386 * Put the new filesystem on the mount list after root. The mount
387 * point gets its own mnt_ncmountpt (unless the VFS already set one
388 * up) which represents the root of the mount. The lookup code
389 * detects the mount point going forward and checks the root of
390 * the mount going backwards.
392 * It is not necessary to invalidate or purge the vnode underneath
393 * because elements under the mount will be given their own glue
397 if (mp->mnt_ncmountpt.ncp == NULL) {
399 * allocate, then unlock, but leave the ref intact
401 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
402 cache_unlock(&mp->mnt_ncmountpt);
404 mp->mnt_ncmounton = nch; /* inherits ref */
405 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
407 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
408 vclrflags(vp, VMOUNT);
409 mountlist_insert(mp, MNTINS_LAST);
411 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
412 error = vfs_allocate_syncvnode(mp);
414 error = VFS_START(mp, 0);
417 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
418 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
419 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
420 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
421 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
422 vclrflags(vp, VMOUNT);
423 mp->mnt_vfc->vfc_refcount--;
435 * Scan all active processes to see if any of them have a current
436 * or root directory onto which the new filesystem has just been
437 * mounted. If so, replace them with the new mount point.
439 * The passed ncp is ref'd and locked (from the mount code) and
440 * must be associated with the vnode representing the root of the
443 struct checkdirs_info {
444 struct nchandle old_nch;
445 struct nchandle new_nch;
446 struct vnode *old_vp;
447 struct vnode *new_vp;
450 static int checkdirs_callback(struct proc *p, void *data);
453 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
455 struct checkdirs_info info;
461 * If the old mount point's vnode has a usecount of 1, it is not
462 * being held as a descriptor anywhere.
464 olddp = old_nch->ncp->nc_vp;
465 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
469 * Force the root vnode of the new mount point to be resolved
470 * so we can update any matching processes.
473 if (VFS_ROOT(mp, &newdp))
474 panic("mount: lost mount");
475 cache_setunresolved(new_nch);
476 cache_setvp(new_nch, newdp);
479 * Special handling of the root node
481 if (rootvnode == olddp) {
483 vfs_cache_setroot(newdp, cache_hold(new_nch));
487 * Pass newdp separately so the callback does not have to access
488 * it via new_nch->ncp->nc_vp.
490 info.old_nch = *old_nch;
491 info.new_nch = *new_nch;
493 allproc_scan(checkdirs_callback, &info);
498 * NOTE: callback is not MP safe because the scanned process's filedesc
499 * structure can be ripped out from under us, amoung other things.
502 checkdirs_callback(struct proc *p, void *data)
504 struct checkdirs_info *info = data;
505 struct filedesc *fdp;
506 struct nchandle ncdrop1;
507 struct nchandle ncdrop2;
508 struct vnode *vprele1;
509 struct vnode *vprele2;
511 if ((fdp = p->p_fd) != NULL) {
512 cache_zero(&ncdrop1);
513 cache_zero(&ncdrop2);
518 * MPUNSAFE - XXX fdp can be pulled out from under a
521 * A shared filedesc is ok, we don't have to copy it
522 * because we are making this change globally.
524 spin_lock_wr(&fdp->fd_spin);
525 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
526 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
527 vprele1 = fdp->fd_cdir;
529 fdp->fd_cdir = info->new_vp;
530 ncdrop1 = fdp->fd_ncdir;
531 cache_copy(&info->new_nch, &fdp->fd_ncdir);
533 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
534 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
535 vprele2 = fdp->fd_rdir;
537 fdp->fd_rdir = info->new_vp;
538 ncdrop2 = fdp->fd_nrdir;
539 cache_copy(&info->new_nch, &fdp->fd_nrdir);
541 spin_unlock_wr(&fdp->fd_spin);
543 cache_drop(&ncdrop1);
545 cache_drop(&ncdrop2);
555 * Unmount a file system.
557 * Note: unmount takes a path to the vnode mounted on as argument,
558 * not special file (as before).
560 * umount_args(char *path, int flags)
565 sys_unmount(struct unmount_args *uap)
567 struct thread *td = curthread;
568 struct proc *p __debugvar = td->td_proc;
569 struct mount *mp = NULL;
570 struct nlookupdata nd;
575 if (td->td_ucred->cr_prison != NULL) {
579 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
582 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
584 error = nlookup(&nd);
588 mp = nd.nl_nch.mount;
591 * Only root, or the user that did the original mount is
592 * permitted to unmount this filesystem.
594 if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
595 (error = priv_check(td, PRIV_ROOT)))
599 * Don't allow unmounting the root file system.
601 if (mp->mnt_flag & MNT_ROOTFS) {
607 * Must be the root of the filesystem
609 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
617 error = dounmount(mp, uap->flags);
624 * Do the actual file system unmount.
627 dounmount_interlock(struct mount *mp)
629 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
631 mp->mnt_kern_flag |= MNTK_UNMOUNT;
636 unmount_allproc_cb(struct proc *p, void *arg)
640 if (p->p_textnch.ncp == NULL)
643 mp = (struct mount *)arg;
644 if (p->p_textnch.mount == mp)
645 cache_drop(&p->p_textnch);
651 dounmount(struct mount *mp, int flags)
653 struct namecache *ncp;
662 * Exclusive access for unmounting purposes
664 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
668 * Allow filesystems to detect that a forced unmount is in progress.
670 if (flags & MNT_FORCE)
671 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
672 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
673 error = lockmgr(&mp->mnt_lock, lflags);
675 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
676 if (mp->mnt_kern_flag & MNTK_MWAIT)
681 if (mp->mnt_flag & MNT_EXPUBLIC)
682 vfs_setpublicfs(NULL, NULL, NULL);
684 vfs_msync(mp, MNT_WAIT);
685 async_flag = mp->mnt_flag & MNT_ASYNC;
686 mp->mnt_flag &=~ MNT_ASYNC;
689 * If this filesystem isn't aliasing other filesystems,
690 * try to invalidate any remaining namecache entries and
691 * check the count afterwords.
693 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
694 cache_lock(&mp->mnt_ncmountpt);
695 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
696 cache_unlock(&mp->mnt_ncmountpt);
698 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
699 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
700 allproc_scan(&unmount_allproc_cb, mp);
703 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
704 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
706 if ((flags & MNT_FORCE) == 0) {
708 mount_warning(mp, "Cannot unmount: "
714 mount_warning(mp, "Forced unmount: "
725 * nchandle records ref the mount structure. Expect a count of 1
726 * (our mount->mnt_ncmountpt).
728 if (mp->mnt_refs != 1) {
729 if ((flags & MNT_FORCE) == 0) {
730 mount_warning(mp, "Cannot unmount: "
731 "%d process references still "
732 "present", mp->mnt_refs);
735 mount_warning(mp, "Forced unmount: "
736 "%d process references still "
737 "present", mp->mnt_refs);
743 * Decomission our special mnt_syncer vnode. This also stops
744 * the vnlru code. If we are unable to unmount we recommission
748 if ((vp = mp->mnt_syncer) != NULL) {
749 mp->mnt_syncer = NULL;
752 if (((mp->mnt_flag & MNT_RDONLY) ||
753 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
754 (flags & MNT_FORCE)) {
755 error = VFS_UNMOUNT(mp, flags);
759 if (mp->mnt_syncer == NULL)
760 vfs_allocate_syncvnode(mp);
761 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
762 mp->mnt_flag |= async_flag;
763 lockmgr(&mp->mnt_lock, LK_RELEASE);
764 if (mp->mnt_kern_flag & MNTK_MWAIT)
769 * Clean up any journals still associated with the mount after
770 * filesystem activity has ceased.
772 journal_remove_all_journals(mp,
773 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
775 mountlist_remove(mp);
778 * Remove any installed vnode ops here so the individual VFSs don't
781 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
782 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
783 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
784 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
785 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
787 if (mp->mnt_ncmountpt.ncp != NULL) {
788 nch = mp->mnt_ncmountpt;
789 cache_zero(&mp->mnt_ncmountpt);
790 cache_clrmountpt(&nch);
793 if (mp->mnt_ncmounton.ncp != NULL) {
794 nch = mp->mnt_ncmounton;
795 cache_zero(&mp->mnt_ncmounton);
796 cache_clrmountpt(&nch);
800 mp->mnt_vfc->vfc_refcount--;
801 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
802 panic("unmount: dangling vnode");
803 lockmgr(&mp->mnt_lock, LK_RELEASE);
804 if (mp->mnt_kern_flag & MNTK_MWAIT)
813 mount_warning(struct mount *mp, const char *ctl, ...)
820 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf, 0) == 0) {
821 kprintf("unmount(%s): ", ptr);
826 kprintf("unmount(%p", mp);
827 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
828 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
837 * Shim cache_fullpath() to handle the case where a process is chrooted into
838 * a subdirectory of a mount. In this case if the root mount matches the
839 * process root directory's mount we have to specify the process's root
840 * directory instead of the mount point, because the mount point might
841 * be above the root directory.
845 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
847 struct nchandle *nch;
849 if (p && p->p_fd->fd_nrdir.mount == mp)
850 nch = &p->p_fd->fd_nrdir;
852 nch = &mp->mnt_ncmountpt;
853 return(cache_fullpath(p, nch, rb, fb, 0));
857 * Sync each mounted filesystem.
861 static int syncprt = 0;
862 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
865 static int sync_callback(struct mount *mp, void *data);
871 sys_sync(struct sync_args *uap)
874 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
877 * 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);
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;
927 if (td->td_ucred->cr_prison && !prison_quotas) {
932 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
934 error = nlookup(&nd);
936 mp = nd.nl_nch.mount;
937 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
938 uap->arg, nd.nl_cred);
947 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
948 * void *buf, int buflen)
950 * This function operates on a mount point and executes the specified
951 * operation using the specified control data, and possibly returns data.
953 * The actual number of bytes stored in the result buffer is returned, 0
954 * if none, otherwise an error is returned.
959 sys_mountctl(struct mountctl_args *uap)
961 struct thread *td = curthread;
962 struct proc *p = td->td_proc;
970 * Sanity and permissions checks. We must be root.
973 if (td->td_ucred->cr_prison != NULL)
975 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
976 (error = priv_check(td, PRIV_ROOT)) != 0)
980 * Argument length checks
982 if (uap->ctllen < 0 || uap->ctllen > 1024)
984 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
986 if (uap->path == NULL)
990 * Allocate the necessary buffers and copyin data
992 path = objcache_get(namei_oc, M_WAITOK);
993 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
998 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
999 error = copyin(uap->ctl, ctl, uap->ctllen);
1004 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1007 * Validate the descriptor
1010 fp = holdfp(p->p_fd, uap->fd, -1);
1020 * Execute the internal kernel function and clean up.
1023 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
1027 if (error == 0 && uap->sysmsg_result > 0)
1028 error = copyout(buf, uap->buf, uap->sysmsg_result);
1031 objcache_put(namei_oc, path);
1040 * Execute a mount control operation by resolving the path to a mount point
1041 * and calling vop_mountctl().
1043 * Use the mount point from the nch instead of the vnode so nullfs mounts
1044 * can properly spike the VOP.
1047 kern_mountctl(const char *path, int op, struct file *fp,
1048 const void *ctl, int ctllen,
1049 void *buf, int buflen, int *res)
1053 struct nlookupdata nd;
1058 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1060 error = nlookup(&nd);
1062 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1063 mp = nd.nl_nch.mount;
1070 * Must be the root of the filesystem
1072 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1076 error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1083 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1085 struct thread *td = curthread;
1086 struct proc *p = td->td_proc;
1089 char *fullpath, *freepath;
1092 if ((error = nlookup(nd)) != 0)
1094 mp = nd->nl_nch.mount;
1096 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1099 error = mount_path(p, mp, &fullpath, &freepath);
1102 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1103 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1104 kfree(freepath, M_TEMP);
1106 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1107 bcopy(sp, buf, sizeof(*buf));
1108 /* Only root should have access to the fsid's. */
1109 if (priv_check(td, PRIV_ROOT))
1110 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1115 * statfs_args(char *path, struct statfs *buf)
1117 * Get filesystem statistics.
1122 sys_statfs(struct statfs_args *uap)
1124 struct nlookupdata nd;
1129 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1131 error = kern_statfs(&nd, &buf);
1134 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1143 kern_fstatfs(int fd, struct statfs *buf)
1145 struct thread *td = curthread;
1146 struct proc *p = td->td_proc;
1150 char *fullpath, *freepath;
1154 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1157 mp = ((struct vnode *)fp->f_data)->v_mount;
1162 if (fp->f_cred == NULL) {
1167 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1170 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1172 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1173 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1174 kfree(freepath, M_TEMP);
1176 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1177 bcopy(sp, buf, sizeof(*buf));
1179 /* Only root should have access to the fsid's. */
1180 if (priv_check(td, PRIV_ROOT))
1181 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1190 * fstatfs_args(int fd, struct statfs *buf)
1192 * Get filesystem statistics.
1197 sys_fstatfs(struct fstatfs_args *uap)
1202 error = kern_fstatfs(uap->fd, &buf);
1205 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1210 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1216 if ((error = nlookup(nd)) != 0)
1218 mp = nd->nl_nch.mount;
1219 sp = &mp->mnt_vstat;
1220 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1224 if (mp->mnt_flag & MNT_RDONLY)
1225 sp->f_flag |= ST_RDONLY;
1226 if (mp->mnt_flag & MNT_NOSUID)
1227 sp->f_flag |= ST_NOSUID;
1228 bcopy(sp, buf, sizeof(*buf));
1233 * statfs_args(char *path, struct statfs *buf)
1235 * Get filesystem statistics.
1240 sys_statvfs(struct statvfs_args *uap)
1242 struct nlookupdata nd;
1247 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1249 error = kern_statvfs(&nd, &buf);
1252 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1258 kern_fstatvfs(int fd, struct statvfs *buf)
1260 struct thread *td = curthread;
1261 struct proc *p = td->td_proc;
1268 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1270 mp = ((struct vnode *)fp->f_data)->v_mount;
1275 if (fp->f_cred == NULL) {
1279 sp = &mp->mnt_vstat;
1280 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1284 if (mp->mnt_flag & MNT_RDONLY)
1285 sp->f_flag |= ST_RDONLY;
1286 if (mp->mnt_flag & MNT_NOSUID)
1287 sp->f_flag |= ST_NOSUID;
1289 bcopy(sp, buf, sizeof(*buf));
1297 * fstatfs_args(int fd, struct statfs *buf)
1299 * Get filesystem statistics.
1304 sys_fstatvfs(struct fstatvfs_args *uap)
1310 error = kern_fstatvfs(uap->fd, &buf);
1314 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1319 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1321 * Get statistics on all filesystems.
1324 struct getfsstat_info {
1325 struct statfs *sfsp;
1333 static int getfsstat_callback(struct mount *, void *);
1339 sys_getfsstat(struct getfsstat_args *uap)
1341 struct thread *td = curthread;
1342 struct getfsstat_info info;
1344 bzero(&info, sizeof(info));
1346 info.maxcount = uap->bufsize / sizeof(struct statfs);
1347 info.sfsp = uap->buf;
1349 info.flags = uap->flags;
1353 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1355 if (info.sfsp && info.count > info.maxcount)
1356 uap->sysmsg_result = info.maxcount;
1358 uap->sysmsg_result = info.count;
1359 return (info.error);
1363 getfsstat_callback(struct mount *mp, void *data)
1365 struct getfsstat_info *info = data;
1371 if (info->sfsp && info->count < info->maxcount) {
1372 if (info->td->td_proc &&
1373 !chroot_visible_mnt(mp, info->td->td_proc)) {
1379 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1380 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1381 * overrides MNT_WAIT.
1383 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1384 (info->flags & MNT_WAIT)) &&
1385 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1388 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1390 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1392 info->error = error;
1395 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1396 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1397 kfree(freepath, M_TEMP);
1399 error = copyout(sp, info->sfsp, sizeof(*sp));
1401 info->error = error;
1411 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1412 long bufsize, int flags)
1414 * Get statistics on all filesystems.
1417 struct getvfsstat_info {
1418 struct statfs *sfsp;
1419 struct statvfs *vsfsp;
1427 static int getvfsstat_callback(struct mount *, void *);
1433 sys_getvfsstat(struct getvfsstat_args *uap)
1435 struct thread *td = curthread;
1436 struct getvfsstat_info info;
1438 bzero(&info, sizeof(info));
1440 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1441 info.sfsp = uap->buf;
1442 info.vsfsp = uap->vbuf;
1444 info.flags = uap->flags;
1448 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1449 if (info.vsfsp && info.count > info.maxcount)
1450 uap->sysmsg_result = info.maxcount;
1452 uap->sysmsg_result = info.count;
1454 return (info.error);
1458 getvfsstat_callback(struct mount *mp, void *data)
1460 struct getvfsstat_info *info = data;
1462 struct statvfs *vsp;
1467 if (info->vsfsp && info->count < info->maxcount) {
1468 if (info->td->td_proc &&
1469 !chroot_visible_mnt(mp, info->td->td_proc)) {
1473 vsp = &mp->mnt_vstat;
1476 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1477 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1478 * overrides MNT_WAIT.
1480 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1481 (info->flags & MNT_WAIT)) &&
1482 (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1485 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1487 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1488 (info->flags & MNT_WAIT)) &&
1489 (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1493 if (mp->mnt_flag & MNT_RDONLY)
1494 vsp->f_flag |= ST_RDONLY;
1495 if (mp->mnt_flag & MNT_NOSUID)
1496 vsp->f_flag |= ST_NOSUID;
1498 error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1500 info->error = error;
1503 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1504 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1505 kfree(freepath, M_TEMP);
1507 error = copyout(sp, info->sfsp, sizeof(*sp));
1509 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1511 info->error = error;
1523 * fchdir_args(int fd)
1525 * Change current working directory to a given file descriptor.
1530 sys_fchdir(struct fchdir_args *uap)
1532 struct thread *td = curthread;
1533 struct proc *p = td->td_proc;
1534 struct filedesc *fdp = p->p_fd;
1535 struct vnode *vp, *ovp;
1538 struct nchandle nch, onch, tnch;
1541 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1544 vp = (struct vnode *)fp->f_data;
1546 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1547 if (fp->f_nchandle.ncp == NULL)
1550 error = checkvp_chdir(vp, td);
1555 cache_copy(&fp->f_nchandle, &nch);
1558 * If the ncp has become a mount point, traverse through
1562 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1563 (mp = cache_findmount(&nch)) != NULL
1565 error = nlookup_mp(mp, &tnch);
1567 cache_unlock(&tnch); /* leave ref intact */
1569 vp = tnch.ncp->nc_vp;
1570 error = vget(vp, LK_SHARED);
1571 KKASSERT(error == 0);
1578 onch = fdp->fd_ncdir;
1579 vn_unlock(vp); /* leave ref intact */
1581 fdp->fd_ncdir = nch;
1595 kern_chdir(struct nlookupdata *nd)
1597 struct thread *td = curthread;
1598 struct proc *p = td->td_proc;
1599 struct filedesc *fdp = p->p_fd;
1600 struct vnode *vp, *ovp;
1601 struct nchandle onch;
1604 if ((error = nlookup(nd)) != 0)
1606 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1608 if ((error = vget(vp, LK_SHARED)) != 0)
1611 error = checkvp_chdir(vp, td);
1615 onch = fdp->fd_ncdir;
1616 cache_unlock(&nd->nl_nch); /* leave reference intact */
1617 fdp->fd_ncdir = nd->nl_nch;
1621 cache_zero(&nd->nl_nch);
1629 * chdir_args(char *path)
1631 * Change current working directory (``.'').
1636 sys_chdir(struct chdir_args *uap)
1638 struct nlookupdata nd;
1642 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1644 error = kern_chdir(&nd);
1651 * Helper function for raised chroot(2) security function: Refuse if
1652 * any filedescriptors are open directories.
1655 chroot_refuse_vdir_fds(struct filedesc *fdp)
1662 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1663 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1665 vp = (struct vnode *)fp->f_data;
1666 if (vp->v_type != VDIR) {
1677 * This sysctl determines if we will allow a process to chroot(2) if it
1678 * has a directory open:
1679 * 0: disallowed for all processes.
1680 * 1: allowed for processes that were not already chroot(2)'ed.
1681 * 2: allowed for all processes.
1684 static int chroot_allow_open_directories = 1;
1686 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1687 &chroot_allow_open_directories, 0, "");
1690 * chroot to the specified namecache entry. We obtain the vp from the
1691 * namecache data. The passed ncp must be locked and referenced and will
1692 * remain locked and referenced on return.
1695 kern_chroot(struct nchandle *nch)
1697 struct thread *td = curthread;
1698 struct proc *p = td->td_proc;
1699 struct filedesc *fdp = p->p_fd;
1704 * Only privileged user can chroot
1706 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1711 * Disallow open directory descriptors (fchdir() breakouts).
1713 if (chroot_allow_open_directories == 0 ||
1714 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1715 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1718 if ((vp = nch->ncp->nc_vp) == NULL)
1721 if ((error = vget(vp, LK_SHARED)) != 0)
1725 * Check the validity of vp as a directory to change to and
1726 * associate it with rdir/jdir.
1728 error = checkvp_chdir(vp, td);
1729 vn_unlock(vp); /* leave reference intact */
1731 vrele(fdp->fd_rdir);
1732 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1733 cache_drop(&fdp->fd_nrdir);
1734 cache_copy(nch, &fdp->fd_nrdir);
1735 if (fdp->fd_jdir == NULL) {
1738 cache_copy(nch, &fdp->fd_njdir);
1747 * chroot_args(char *path)
1749 * Change notion of root (``/'') directory.
1754 sys_chroot(struct chroot_args *uap)
1756 struct thread *td __debugvar = curthread;
1757 struct nlookupdata nd;
1760 KKASSERT(td->td_proc);
1762 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1764 nd.nl_flags |= NLC_EXEC;
1765 error = nlookup(&nd);
1767 error = kern_chroot(&nd.nl_nch);
1775 sys_chroot_kernel(struct chroot_kernel_args *uap)
1777 struct thread *td = curthread;
1778 struct nlookupdata nd;
1779 struct nchandle *nch;
1784 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1788 error = nlookup(&nd);
1794 error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1798 if ((vp = nch->ncp->nc_vp) == NULL) {
1803 if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
1806 kprintf("chroot_kernel: set new rootnch/rootvnode to %s\n", uap->path);
1807 vfs_cache_setroot(vp, cache_hold(nch));
1817 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1818 * determine whether it is legal to chdir to the vnode. The vnode's state
1819 * is not changed by this call.
1822 checkvp_chdir(struct vnode *vp, struct thread *td)
1826 if (vp->v_type != VDIR)
1829 error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
1837 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1839 struct thread *td = curthread;
1840 struct proc *p = td->td_proc;
1841 struct lwp *lp = td->td_lwp;
1842 struct filedesc *fdp = p->p_fd;
1847 int type, indx, error;
1850 if ((oflags & O_ACCMODE) == O_ACCMODE)
1852 flags = FFLAGS(oflags);
1853 error = falloc(lp, &nfp, NULL);
1857 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1860 * XXX p_dupfd is a real mess. It allows a device to return a
1861 * file descriptor to be duplicated rather then doing the open
1867 * Call vn_open() to do the lookup and assign the vnode to the
1868 * file pointer. vn_open() does not change the ref count on fp
1869 * and the vnode, on success, will be inherited by the file pointer
1872 nd->nl_flags |= NLC_LOCKVP;
1873 error = vn_open(nd, fp, flags, cmode);
1877 * handle special fdopen() case. bleh. dupfdopen() is
1878 * responsible for dropping the old contents of ofiles[indx]
1881 * Note that fsetfd() will add a ref to fp which represents
1882 * the fd_files[] assignment. We must still drop our
1885 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1886 if (fdalloc(p, 0, &indx) == 0) {
1887 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1890 fdrop(fp); /* our ref */
1893 fsetfd(fdp, NULL, indx);
1896 fdrop(fp); /* our ref */
1897 if (error == ERESTART)
1903 * ref the vnode for ourselves so it can't be ripped out from under
1904 * is. XXX need an ND flag to request that the vnode be returned
1907 * Reserve a file descriptor but do not assign it until the open
1910 vp = (struct vnode *)fp->f_data;
1912 if ((error = fdalloc(p, 0, &indx)) != 0) {
1919 * If no error occurs the vp will have been assigned to the file
1924 if (flags & (O_EXLOCK | O_SHLOCK)) {
1925 lf.l_whence = SEEK_SET;
1928 if (flags & O_EXLOCK)
1929 lf.l_type = F_WRLCK;
1931 lf.l_type = F_RDLCK;
1932 if (flags & FNONBLOCK)
1937 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1939 * lock request failed. Clean up the reserved
1943 fsetfd(fdp, NULL, indx);
1947 fp->f_flag |= FHASLOCK;
1951 * Assert that all regular file vnodes were created with a object.
1953 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1954 ("open: regular file has no backing object after vn_open"));
1960 * release our private reference, leaving the one associated with the
1961 * descriptor table intact.
1963 fsetfd(fdp, fp, indx);
1970 * open_args(char *path, int flags, int mode)
1972 * Check permissions, allocate an open file structure,
1973 * and call the device open routine if any.
1978 sys_open(struct open_args *uap)
1980 CACHE_MPLOCK_DECLARE;
1981 struct nlookupdata nd;
1985 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1987 error = kern_open(&nd, uap->flags,
1988 uap->mode, &uap->sysmsg_result);
1996 * openat_args(int fd, char *path, int flags, int mode)
2001 sys_openat(struct openat_args *uap)
2003 CACHE_MPLOCK_DECLARE;
2004 struct nlookupdata nd;
2009 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2011 error = kern_open(&nd, uap->flags, uap->mode,
2012 &uap->sysmsg_result);
2014 nlookup_done_at(&nd, fp);
2020 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2022 struct thread *td = curthread;
2023 struct proc *p = td->td_proc;
2032 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2033 vattr.va_rmajor = rmajor;
2034 vattr.va_rminor = rminor;
2036 switch (mode & S_IFMT) {
2037 case S_IFMT: /* used by badsect to flag bad sectors */
2038 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2039 vattr.va_type = VBAD;
2042 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2043 vattr.va_type = VCHR;
2046 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2047 vattr.va_type = VBLK;
2050 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2053 case S_IFDIR: /* special directories support for HAMMER */
2054 error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2055 vattr.va_type = VDIR;
2066 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2067 if ((error = nlookup(nd)) != 0)
2069 if (nd->nl_nch.ncp->nc_vp)
2071 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2075 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2076 nd->nl_cred, NAMEI_CREATE);
2079 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2080 &vp, nd->nl_cred, &vattr);
2088 * mknod_args(char *path, int mode, int dev)
2090 * Create a special file.
2095 sys_mknod(struct mknod_args *uap)
2097 struct nlookupdata nd;
2101 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2103 error = kern_mknod(&nd, uap->mode,
2104 umajor(uap->dev), uminor(uap->dev));
2112 kern_mkfifo(struct nlookupdata *nd, int mode)
2114 struct thread *td = curthread;
2115 struct proc *p = td->td_proc;
2122 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2123 if ((error = nlookup(nd)) != 0)
2125 if (nd->nl_nch.ncp->nc_vp)
2127 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2131 vattr.va_type = VFIFO;
2132 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2134 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2141 * mkfifo_args(char *path, int mode)
2143 * Create a named pipe.
2148 sys_mkfifo(struct mkfifo_args *uap)
2150 struct nlookupdata nd;
2154 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2156 error = kern_mkfifo(&nd, uap->mode);
2162 static int hardlink_check_uid = 0;
2163 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2164 &hardlink_check_uid, 0,
2165 "Unprivileged processes cannot create hard links to files owned by other "
2167 static int hardlink_check_gid = 0;
2168 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2169 &hardlink_check_gid, 0,
2170 "Unprivileged processes cannot create hard links to files owned by other "
2174 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2180 * Shortcut if disabled
2182 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2186 * Privileged user can always hardlink
2188 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2192 * Otherwise only if the originating file is owned by the
2193 * same user or group. Note that any group is allowed if
2194 * the file is owned by the caller.
2196 error = VOP_GETATTR(vp, &va);
2200 if (hardlink_check_uid) {
2201 if (cred->cr_uid != va.va_uid)
2205 if (hardlink_check_gid) {
2206 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2214 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2216 struct thread *td = curthread;
2221 * Lookup the source and obtained a locked vnode.
2223 * You may only hardlink a file which you have write permission
2224 * on or which you own.
2226 * XXX relookup on vget failure / race ?
2229 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2230 if ((error = nlookup(nd)) != 0)
2232 vp = nd->nl_nch.ncp->nc_vp;
2233 KKASSERT(vp != NULL);
2234 if (vp->v_type == VDIR)
2235 return (EPERM); /* POSIX */
2236 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2238 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2242 * Unlock the source so we can lookup the target without deadlocking
2243 * (XXX vp is locked already, possible other deadlock?). The target
2246 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2247 nd->nl_flags &= ~NLC_NCPISLOCKED;
2248 cache_unlock(&nd->nl_nch);
2251 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2252 if ((error = nlookup(linknd)) != 0) {
2256 if (linknd->nl_nch.ncp->nc_vp) {
2260 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2266 * Finally run the new API VOP.
2268 error = can_hardlink(vp, td, td->td_ucred);
2270 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2271 vp, linknd->nl_cred);
2278 * link_args(char *path, char *link)
2280 * Make a hard file link.
2285 sys_link(struct link_args *uap)
2287 struct nlookupdata nd, linknd;
2291 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2293 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2295 error = kern_link(&nd, &linknd);
2296 nlookup_done(&linknd);
2304 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2312 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2313 if ((error = nlookup(nd)) != 0)
2315 if (nd->nl_nch.ncp->nc_vp)
2317 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2321 vattr.va_mode = mode;
2322 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2329 * symlink(char *path, char *link)
2331 * Make a symbolic link.
2336 sys_symlink(struct symlink_args *uap)
2338 struct thread *td = curthread;
2339 struct nlookupdata nd;
2344 path = objcache_get(namei_oc, M_WAITOK);
2345 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2348 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2350 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2351 error = kern_symlink(&nd, path, mode);
2356 objcache_put(namei_oc, path);
2361 * undelete_args(char *path)
2363 * Delete a whiteout from the filesystem.
2368 sys_undelete(struct undelete_args *uap)
2370 struct nlookupdata nd;
2374 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2376 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2378 error = nlookup(&nd);
2380 error = ncp_writechk(&nd.nl_nch);
2382 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2391 kern_unlink(struct nlookupdata *nd)
2396 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2397 if ((error = nlookup(nd)) != 0)
2399 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2401 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2406 * unlink_args(char *path)
2408 * Delete a name from the filesystem.
2413 sys_unlink(struct unlink_args *uap)
2415 struct nlookupdata nd;
2419 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2421 error = kern_unlink(&nd);
2429 * unlinkat_args(int fd, char *path, int flags)
2431 * Delete the file or directory entry pointed to by fd/path.
2436 sys_unlinkat(struct unlinkat_args *uap)
2438 struct nlookupdata nd;
2442 if (uap->flags & ~AT_REMOVEDIR)
2446 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2448 if (uap->flags & AT_REMOVEDIR)
2449 error = kern_rmdir(&nd);
2451 error = kern_unlink(&nd);
2453 nlookup_done_at(&nd, fp);
2462 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2464 struct thread *td = curthread;
2465 struct proc *p = td->td_proc;
2472 fp = holdfp(p->p_fd, fd, -1);
2475 if (fp->f_type != DTYPE_VNODE) {
2479 vp = (struct vnode *)fp->f_data;
2483 spin_lock_wr(&fp->f_spin);
2484 new_offset = fp->f_offset + offset;
2489 error = VOP_GETATTR(vp, &vattr);
2491 spin_lock_wr(&fp->f_spin);
2492 new_offset = offset + vattr.va_size;
2495 new_offset = offset;
2497 spin_lock_wr(&fp->f_spin);
2502 spin_lock_wr(&fp->f_spin);
2507 * Validate the seek position. Negative offsets are not allowed
2508 * for regular files or directories.
2510 * Normally we would also not want to allow negative offsets for
2511 * character and block-special devices. However kvm addresses
2512 * on 64 bit architectures might appear to be negative and must
2516 if (new_offset < 0 &&
2517 (vp->v_type == VREG || vp->v_type == VDIR)) {
2520 fp->f_offset = new_offset;
2523 *res = fp->f_offset;
2524 spin_unlock_wr(&fp->f_spin);
2531 * lseek_args(int fd, int pad, off_t offset, int whence)
2533 * Reposition read/write file offset.
2538 sys_lseek(struct lseek_args *uap)
2542 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2543 &uap->sysmsg_offset);
2549 * Check if current process can access given file. amode is a bitmask of *_OK
2550 * access bits. flags is a bitmask of AT_* flags.
2553 kern_access(struct nlookupdata *nd, int amode, int flags)
2558 if (flags & ~AT_EACCESS)
2560 if ((error = nlookup(nd)) != 0)
2563 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2567 /* Flags == 0 means only check for existence. */
2576 if ((mode & VWRITE) == 0 ||
2577 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2578 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2581 * If the file handle is stale we have to re-resolve the
2582 * entry. This is a hack at the moment.
2584 if (error == ESTALE) {
2586 cache_setunresolved(&nd->nl_nch);
2587 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2600 * access_args(char *path, int flags)
2602 * Check access permissions.
2607 sys_access(struct access_args *uap)
2609 struct nlookupdata nd;
2613 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2615 error = kern_access(&nd, uap->flags, 0);
2623 * faccessat_args(int fd, char *path, int amode, int flags)
2625 * Check access permissions.
2630 sys_faccessat(struct faccessat_args *uap)
2632 struct nlookupdata nd;
2637 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2640 error = kern_access(&nd, uap->amode, uap->flags);
2641 nlookup_done_at(&nd, fp);
2651 kern_stat(struct nlookupdata *nd, struct stat *st)
2657 if ((error = nlookup(nd)) != 0)
2660 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2664 if ((error = vget(vp, LK_SHARED)) != 0)
2666 error = vn_stat(vp, st, nd->nl_cred);
2669 * If the file handle is stale we have to re-resolve the entry. This
2670 * is a hack at the moment.
2672 if (error == ESTALE) {
2674 cache_setunresolved(&nd->nl_nch);
2675 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2685 * stat_args(char *path, struct stat *ub)
2687 * Get file status; this version follows links.
2692 sys_stat(struct stat_args *uap)
2694 CACHE_MPLOCK_DECLARE;
2695 struct nlookupdata nd;
2700 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2702 error = kern_stat(&nd, &st);
2704 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2712 * lstat_args(char *path, struct stat *ub)
2714 * Get file status; this version does not follow links.
2719 sys_lstat(struct lstat_args *uap)
2721 CACHE_MPLOCK_DECLARE;
2722 struct nlookupdata nd;
2727 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2729 error = kern_stat(&nd, &st);
2731 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2739 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2741 * Get status of file pointed to by fd/path.
2746 sys_fstatat(struct fstatat_args *uap)
2748 CACHE_MPLOCK_DECLARE;
2749 struct nlookupdata nd;
2755 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2758 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2761 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2762 UIO_USERSPACE, flags);
2764 error = kern_stat(&nd, &st);
2766 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2768 nlookup_done_at(&nd, fp);
2774 * pathconf_Args(char *path, int name)
2776 * Get configurable pathname variables.
2781 sys_pathconf(struct pathconf_args *uap)
2783 struct nlookupdata nd;
2789 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2791 error = nlookup(&nd);
2793 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2796 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2805 * kern_readlink isn't properly split yet. There is a copyin burried
2806 * in VOP_READLINK().
2809 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2811 struct thread *td = curthread;
2817 if ((error = nlookup(nd)) != 0)
2819 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2822 if (vp->v_type != VLNK) {
2825 aiov.iov_base = buf;
2826 aiov.iov_len = count;
2827 auio.uio_iov = &aiov;
2828 auio.uio_iovcnt = 1;
2829 auio.uio_offset = 0;
2830 auio.uio_rw = UIO_READ;
2831 auio.uio_segflg = UIO_USERSPACE;
2833 auio.uio_resid = count;
2834 error = VOP_READLINK(vp, &auio, td->td_ucred);
2837 *res = count - auio.uio_resid;
2842 * readlink_args(char *path, char *buf, int count)
2844 * Return target name of a symbolic link.
2849 sys_readlink(struct readlink_args *uap)
2851 struct nlookupdata nd;
2855 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2857 error = kern_readlink(&nd, uap->buf, uap->count,
2858 &uap->sysmsg_result);
2866 setfflags(struct vnode *vp, int flags)
2868 struct thread *td = curthread;
2873 * Prevent non-root users from setting flags on devices. When
2874 * a device is reused, users can retain ownership of the device
2875 * if they are allowed to set flags and programs assume that
2876 * chown can't fail when done as root.
2878 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2879 ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2883 * note: vget is required for any operation that might mod the vnode
2884 * so VINACTIVE is properly cleared.
2886 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2888 vattr.va_flags = flags;
2889 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2896 * chflags(char *path, int flags)
2898 * Change flags of a file given a path name.
2903 sys_chflags(struct chflags_args *uap)
2905 struct nlookupdata nd;
2911 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2913 error = nlookup(&nd);
2915 error = ncp_writechk(&nd.nl_nch);
2917 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2920 error = setfflags(vp, uap->flags);
2928 * lchflags(char *path, int flags)
2930 * Change flags of a file given a path name, but don't follow symlinks.
2935 sys_lchflags(struct lchflags_args *uap)
2937 struct nlookupdata nd;
2943 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2945 error = nlookup(&nd);
2947 error = ncp_writechk(&nd.nl_nch);
2949 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2952 error = setfflags(vp, uap->flags);
2960 * fchflags_args(int fd, int flags)
2962 * Change flags of a file given a file descriptor.
2967 sys_fchflags(struct fchflags_args *uap)
2969 struct thread *td = curthread;
2970 struct proc *p = td->td_proc;
2974 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2977 if (fp->f_nchandle.ncp)
2978 error = ncp_writechk(&fp->f_nchandle);
2980 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2987 setfmode(struct vnode *vp, int mode)
2989 struct thread *td = curthread;
2994 * note: vget is required for any operation that might mod the vnode
2995 * so VINACTIVE is properly cleared.
2997 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2999 vattr.va_mode = mode & ALLPERMS;
3000 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3007 kern_chmod(struct nlookupdata *nd, int mode)
3012 if ((error = nlookup(nd)) != 0)
3014 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3016 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3017 error = setfmode(vp, mode);
3023 * chmod_args(char *path, int mode)
3025 * Change mode of a file given path name.
3030 sys_chmod(struct chmod_args *uap)
3032 struct nlookupdata nd;
3036 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3038 error = kern_chmod(&nd, uap->mode);
3045 * lchmod_args(char *path, int mode)
3047 * Change mode of a file given path name (don't follow links.)
3052 sys_lchmod(struct lchmod_args *uap)
3054 struct nlookupdata nd;
3058 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3060 error = kern_chmod(&nd, uap->mode);
3067 * fchmod_args(int fd, int mode)
3069 * Change mode of a file given a file descriptor.
3074 sys_fchmod(struct fchmod_args *uap)
3076 struct thread *td = curthread;
3077 struct proc *p = td->td_proc;
3081 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3084 if (fp->f_nchandle.ncp)
3085 error = ncp_writechk(&fp->f_nchandle);
3087 error = setfmode((struct vnode *)fp->f_data, uap->mode);
3094 * fchmodat_args(char *path, int mode)
3096 * Change mode of a file pointed to by fd/path.
3101 sys_fchmodat(struct fchmodat_args *uap)
3103 struct nlookupdata nd;
3108 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3110 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3113 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3114 UIO_USERSPACE, flags);
3116 error = kern_chmod(&nd, uap->mode);
3117 nlookup_done_at(&nd, fp);
3123 setfown(struct vnode *vp, uid_t uid, gid_t gid)
3125 struct thread *td = curthread;
3130 * note: vget is required for any operation that might mod the vnode
3131 * so VINACTIVE is properly cleared.
3133 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3137 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3144 kern_chown(struct nlookupdata *nd, int uid, int gid)
3149 if ((error = nlookup(nd)) != 0)
3151 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3153 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3154 error = setfown(vp, uid, gid);
3160 * chown(char *path, int uid, int gid)
3162 * Set ownership given a path name.
3167 sys_chown(struct chown_args *uap)
3169 struct nlookupdata nd;
3173 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3175 error = kern_chown(&nd, uap->uid, uap->gid);
3182 * lchown_args(char *path, int uid, int gid)
3184 * Set ownership given a path name, do not cross symlinks.
3189 sys_lchown(struct lchown_args *uap)
3191 struct nlookupdata nd;
3195 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3197 error = kern_chown(&nd, uap->uid, uap->gid);
3204 * fchown_args(int fd, int uid, int gid)
3206 * Set ownership given a file descriptor.
3211 sys_fchown(struct fchown_args *uap)
3213 struct thread *td = curthread;
3214 struct proc *p = td->td_proc;
3218 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3221 if (fp->f_nchandle.ncp)
3222 error = ncp_writechk(&fp->f_nchandle);
3224 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
3231 * fchownat(int fd, char *path, int uid, int gid, int flags)
3233 * Set ownership of file pointed to by fd/path.
3238 sys_fchownat(struct fchownat_args *uap)
3240 struct nlookupdata nd;
3245 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3247 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3250 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3251 UIO_USERSPACE, flags);
3253 error = kern_chown(&nd, uap->uid, uap->gid);
3254 nlookup_done_at(&nd, fp);
3261 getutimes(const struct timeval *tvp, struct timespec *tsp)
3263 struct timeval tv[2];
3267 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3270 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3271 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3277 setutimes(struct vnode *vp, struct vattr *vattr,
3278 const struct timespec *ts, int nullflag)
3280 struct thread *td = curthread;
3284 vattr->va_atime = ts[0];
3285 vattr->va_mtime = ts[1];
3287 vattr->va_vaflags |= VA_UTIMES_NULL;
3288 error = VOP_SETATTR(vp, vattr, td->td_ucred);
3294 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3296 struct timespec ts[2];
3301 if ((error = getutimes(tptr, ts)) != 0)
3305 * NOTE: utimes() succeeds for the owner even if the file
3306 * is not user-writable.
3308 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3310 if ((error = nlookup(nd)) != 0)
3312 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3314 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3318 * note: vget is required for any operation that might mod the vnode
3319 * so VINACTIVE is properly cleared.
3321 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3322 error = vget(vp, LK_EXCLUSIVE);
3324 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3333 * utimes_args(char *path, struct timeval *tptr)
3335 * Set the access and modification times of a file.
3340 sys_utimes(struct utimes_args *uap)
3342 struct timeval tv[2];
3343 struct nlookupdata nd;
3347 error = copyin(uap->tptr, tv, sizeof(tv));
3352 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3354 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3361 * lutimes_args(char *path, struct timeval *tptr)
3363 * 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));
3380 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3382 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3389 * Set utimes on a file descriptor. The creds used to open the
3390 * file are used to determine whether the operation is allowed
3394 kern_futimes(int fd, struct timeval *tptr)
3396 struct thread *td = curthread;
3397 struct proc *p = td->td_proc;
3398 struct timespec ts[2];
3404 error = getutimes(tptr, ts);
3407 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3409 if (fp->f_nchandle.ncp)
3410 error = ncp_writechk(&fp->f_nchandle);
3413 error = vget(vp, LK_EXCLUSIVE);
3415 error = VOP_GETATTR(vp, &vattr);
3417 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3421 error = setutimes(vp, &vattr, ts,
3432 * futimes_args(int fd, struct timeval *tptr)
3434 * Set the access and modification times of a file.
3439 sys_futimes(struct futimes_args *uap)
3441 struct timeval tv[2];
3445 error = copyin(uap->tptr, tv, sizeof(tv));
3450 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3457 kern_truncate(struct nlookupdata *nd, off_t length)
3465 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3466 if ((error = nlookup(nd)) != 0)
3468 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3470 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3472 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3476 if (vp->v_type == VDIR) {
3478 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3480 vattr.va_size = length;
3481 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3488 * truncate(char *path, int pad, off_t length)
3490 * Truncate a file given its path name.
3495 sys_truncate(struct truncate_args *uap)
3497 struct nlookupdata nd;
3501 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3503 error = kern_truncate(&nd, uap->length);
3510 kern_ftruncate(int fd, off_t length)
3512 struct thread *td = curthread;
3513 struct proc *p = td->td_proc;
3521 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3523 if (fp->f_nchandle.ncp) {
3524 error = ncp_writechk(&fp->f_nchandle);
3528 if ((fp->f_flag & FWRITE) == 0) {
3532 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3536 vp = (struct vnode *)fp->f_data;
3537 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3538 if (vp->v_type == VDIR) {
3540 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3542 vattr.va_size = length;
3543 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3552 * ftruncate_args(int fd, int pad, off_t length)
3554 * Truncate a file given a file descriptor.
3559 sys_ftruncate(struct ftruncate_args *uap)
3564 error = kern_ftruncate(uap->fd, uap->length);
3573 * Sync an open file.
3578 sys_fsync(struct fsync_args *uap)
3580 struct thread *td = curthread;
3581 struct proc *p = td->td_proc;
3587 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3590 vp = (struct vnode *)fp->f_data;
3591 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3592 if ((obj = vp->v_object) != NULL)
3593 vm_object_page_clean(obj, 0, 0, 0);
3594 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3595 if (error == 0 && vp->v_mount)
3596 error = buf_fsync(vp);
3605 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3607 struct nchandle fnchd;
3608 struct nchandle tnchd;
3609 struct namecache *ncp;
3616 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3617 if ((error = nlookup(fromnd)) != 0)
3619 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3621 fnchd.mount = fromnd->nl_nch.mount;
3625 * unlock the source nch so we can lookup the target nch without
3626 * deadlocking. The target may or may not exist so we do not check
3627 * for a target vp like kern_mkdir() and other creation functions do.
3629 * The source and target directories are ref'd and rechecked after
3630 * everything is relocked to determine if the source or target file
3633 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3634 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3635 cache_unlock(&fromnd->nl_nch);
3637 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3638 if ((error = nlookup(tond)) != 0) {
3642 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3646 tnchd.mount = tond->nl_nch.mount;
3650 * If the source and target are the same there is nothing to do
3652 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3659 * Mount points cannot be renamed or overwritten
3661 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3670 * Relock the source ncp. cache_relock() will deal with any
3671 * deadlocks against the already-locked tond and will also
3672 * make sure both are resolved.
3674 * NOTE AFTER RELOCKING: The source or target ncp may have become
3675 * invalid while they were unlocked, nc_vp and nc_mount could
3678 cache_relock(&fromnd->nl_nch, fromnd->nl_cred,
3679 &tond->nl_nch, tond->nl_cred);
3680 fromnd->nl_flags |= NLC_NCPISLOCKED;
3683 * make sure the parent directories linkages are the same
3685 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3686 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3693 * Both the source and target must be within the same filesystem and
3694 * in the same filesystem as their parent directories within the
3695 * namecache topology.
3697 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3700 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3701 mp != tond->nl_nch.mount) {
3708 * Make sure the mount point is writable
3710 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3717 * If the target exists and either the source or target is a directory,
3718 * then both must be directories.
3720 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3723 if (tond->nl_nch.ncp->nc_vp) {
3724 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3726 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3727 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3729 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3735 * You cannot rename a source into itself or a subdirectory of itself.
3736 * We check this by travsersing the target directory upwards looking
3737 * for a match against the source.
3742 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3743 if (fromnd->nl_nch.ncp == ncp) {
3754 * Even though the namespaces are different, they may still represent
3755 * hardlinks to the same file. The filesystem might have a hard time
3756 * with this so we issue a NREMOVE of the source instead of a NRENAME
3757 * when we detect the situation.
3760 fdvp = fromnd->nl_dvp;
3761 tdvp = tond->nl_dvp;
3762 if (fdvp == NULL || tdvp == NULL) {
3764 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3765 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3768 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3769 fdvp, tdvp, tond->nl_cred);
3776 * rename_args(char *from, char *to)
3778 * Rename files. Source and destination must either both be directories,
3779 * or both not be directories. If target is a directory, it must be empty.
3784 sys_rename(struct rename_args *uap)
3786 struct nlookupdata fromnd, tond;
3790 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3792 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3794 error = kern_rename(&fromnd, &tond);
3795 nlookup_done(&tond);
3797 nlookup_done(&fromnd);
3803 * renameat_args(int oldfd, char *old, int newfd, char *new)
3805 * Rename files using paths relative to the directories associated with
3806 * oldfd and newfd. Source and destination must either both be directories,
3807 * or both not be directories. If target is a directory, it must be empty.
3812 sys_renameat(struct renameat_args *uap)
3814 struct nlookupdata oldnd, newnd;
3815 struct file *oldfp, *newfp;
3819 error = nlookup_init_at(&oldnd, &oldfp, uap->oldfd, uap->old,
3822 error = nlookup_init_at(&newnd, &newfp, uap->newfd, uap->new,
3825 error = kern_rename(&oldnd, &newnd);
3826 nlookup_done_at(&newnd, newfp);
3828 nlookup_done_at(&oldnd, oldfp);
3834 kern_mkdir(struct nlookupdata *nd, int mode)
3836 struct thread *td = curthread;
3837 struct proc *p = td->td_proc;
3843 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3844 if ((error = nlookup(nd)) != 0)
3847 if (nd->nl_nch.ncp->nc_vp)
3849 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3852 vattr.va_type = VDIR;
3853 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3856 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
3863 * mkdir_args(char *path, int mode)
3865 * Make a directory file.
3870 sys_mkdir(struct mkdir_args *uap)
3872 struct nlookupdata nd;
3876 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3878 error = kern_mkdir(&nd, uap->mode);
3885 kern_rmdir(struct nlookupdata *nd)
3890 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3891 if ((error = nlookup(nd)) != 0)
3895 * Do not allow directories representing mount points to be
3896 * deleted, even if empty. Check write perms on mount point
3897 * in case the vnode is aliased (aka nullfs).
3899 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3901 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3903 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3908 * rmdir_args(char *path)
3910 * Remove a directory file.
3915 sys_rmdir(struct rmdir_args *uap)
3917 struct nlookupdata nd;
3921 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3923 error = kern_rmdir(&nd);
3930 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3931 enum uio_seg direction)
3933 struct thread *td = curthread;
3934 struct proc *p = td->td_proc;
3942 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3944 if ((fp->f_flag & FREAD) == 0) {
3948 vp = (struct vnode *)fp->f_data;
3950 if (vp->v_type != VDIR) {
3954 aiov.iov_base = buf;
3955 aiov.iov_len = count;
3956 auio.uio_iov = &aiov;
3957 auio.uio_iovcnt = 1;
3958 auio.uio_rw = UIO_READ;
3959 auio.uio_segflg = direction;
3961 auio.uio_resid = count;
3962 loff = auio.uio_offset = fp->f_offset;
3963 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3964 fp->f_offset = auio.uio_offset;
3967 if (count == auio.uio_resid) {
3968 if (union_dircheckp) {
3969 error = union_dircheckp(td, &vp, fp);
3976 if ((vp->v_flag & VROOT) &&
3977 (vp->v_mount->mnt_flag & MNT_UNION)) {
3978 struct vnode *tvp = vp;
3979 vp = vp->v_mount->mnt_vnodecovered;
3990 * WARNING! *basep may not be wide enough to accomodate the
3991 * seek offset. XXX should we hack this to return the upper 32 bits
3992 * for offsets greater then 4G?
3995 *basep = (long)loff;
3997 *res = count - auio.uio_resid;
4004 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4006 * Read a block of directory entries in a file system independent format.
4011 sys_getdirentries(struct getdirentries_args *uap)
4017 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4018 &uap->sysmsg_result, UIO_USERSPACE);
4021 if (error == 0 && uap->basep)
4022 error = copyout(&base, uap->basep, sizeof(*uap->basep));
4027 * getdents_args(int fd, char *buf, size_t count)
4032 sys_getdents(struct getdents_args *uap)
4037 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4038 &uap->sysmsg_result, UIO_USERSPACE);
4045 * Set the mode mask for creation of filesystem nodes.
4047 * umask(int newmask)
4052 sys_umask(struct umask_args *uap)
4054 struct thread *td = curthread;
4055 struct proc *p = td->td_proc;
4056 struct filedesc *fdp;
4059 uap->sysmsg_result = fdp->fd_cmask;
4060 fdp->fd_cmask = uap->newmask & ALLPERMS;
4065 * revoke(char *path)
4067 * Void all references to file by ripping underlying filesystem
4073 sys_revoke(struct revoke_args *uap)
4075 struct nlookupdata nd;
4083 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4085 error = nlookup(&nd);
4087 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4088 cred = crhold(nd.nl_cred);
4092 error = VOP_GETATTR(vp, &vattr);
4093 if (error == 0 && cred->cr_uid != vattr.va_uid)
4094 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4095 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4097 error = vrevoke(vp, cred);
4098 } else if (error == 0) {
4099 error = vrevoke(vp, cred);
4110 * getfh_args(char *fname, fhandle_t *fhp)
4112 * Get (NFS) file handle
4114 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4115 * mount. This allows nullfs mounts to be explicitly exported.
4117 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4119 * nullfs mounts of subdirectories are not safe. That is, it will
4120 * work, but you do not really have protection against access to
4121 * the related parent directories.
4126 sys_getfh(struct getfh_args *uap)
4128 struct thread *td = curthread;
4129 struct nlookupdata nd;
4136 * Must be super user
4138 if ((error = priv_check(td, PRIV_ROOT)) != 0)
4143 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4145 error = nlookup(&nd);
4147 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4148 mp = nd.nl_nch.mount;
4151 bzero(&fh, sizeof(fh));
4152 fh.fh_fsid = mp->mnt_stat.f_fsid;
4153 error = VFS_VPTOFH(vp, &fh.fh_fid);
4156 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
4174 sys_fhopen(struct fhopen_args *uap)
4176 struct thread *td = curthread;
4177 struct filedesc *fdp = td->td_proc->p_fd;
4182 struct vattr *vap = &vat;
4184 int fmode, mode, error, type;
4190 * Must be super user
4192 error = priv_check(td, PRIV_ROOT);
4196 fmode = FFLAGS(uap->flags);
4199 * Why not allow a non-read/write open for our lockd?
4201 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4203 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4208 * Find the mount point
4211 mp = vfs_getvfs(&fhp.fh_fsid);
4216 /* now give me my vnode, it gets returned to me locked */
4217 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4221 * from now on we have to make sure not
4222 * to forget about the vnode
4223 * any error that causes an abort must vput(vp)
4224 * just set error = err and 'goto bad;'.
4230 if (vp->v_type == VLNK) {
4234 if (vp->v_type == VSOCK) {
4239 if (fmode & (FWRITE | O_TRUNC)) {
4240 if (vp->v_type == VDIR) {
4244 error = vn_writechk(vp, NULL);
4252 error = VOP_ACCESS(vp, mode, td->td_ucred);
4256 if (fmode & O_TRUNC) {
4257 vn_unlock(vp); /* XXX */
4258 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
4261 error = VOP_SETATTR(vp, vap, td->td_ucred);
4267 * VOP_OPEN needs the file pointer so it can potentially override
4270 * WARNING! no f_nchandle will be associated when fhopen()ing a
4273 if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4277 error = VOP_OPEN(vp, fmode, td->td_ucred, fp);
4280 * setting f_ops this way prevents VOP_CLOSE from being
4281 * called or fdrop() releasing the vp from v_data. Since
4282 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4284 fp->f_ops = &badfileops;
4290 * The fp is given its own reference, we still have our ref and lock.
4292 * Assert that all regular files must be created with a VM object.
4294 if (vp->v_type == VREG && vp->v_object == NULL) {
4295 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
4300 * The open was successful. Handle any locking requirements.
4302 if (fmode & (O_EXLOCK | O_SHLOCK)) {
4303 lf.l_whence = SEEK_SET;
4306 if (fmode & O_EXLOCK)
4307 lf.l_type = F_WRLCK;
4309 lf.l_type = F_RDLCK;
4310 if (fmode & FNONBLOCK)
4315 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
4317 * release our private reference.
4319 fsetfd(fdp, NULL, indx);
4324 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4325 fp->f_flag |= FHASLOCK;
4329 * Clean up. Associate the file pointer with the previously
4330 * reserved descriptor and return it.
4334 fsetfd(fdp, fp, indx);
4336 uap->sysmsg_result = indx;
4340 fsetfd(fdp, NULL, indx);
4350 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4355 sys_fhstat(struct fhstat_args *uap)
4357 struct thread *td = curthread;
4365 * Must be super user
4367 error = priv_check(td, PRIV_ROOT);
4371 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4376 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4379 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4380 error = vn_stat(vp, &sb, td->td_ucred);
4386 error = copyout(&sb, uap->sb, sizeof(sb));
4391 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4396 sys_fhstatfs(struct fhstatfs_args *uap)
4398 struct thread *td = curthread;
4399 struct proc *p = td->td_proc;
4404 char *fullpath, *freepath;
4409 * Must be super user
4411 if ((error = priv_check(td, PRIV_ROOT)))
4414 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4419 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4423 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4428 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4433 if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4436 error = mount_path(p, mp, &fullpath, &freepath);
4439 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4440 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4441 kfree(freepath, M_TEMP);
4443 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4444 if (priv_check(td, PRIV_ROOT)) {
4445 bcopy(sp, &sb, sizeof(sb));
4446 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4449 error = copyout(sp, uap->buf, sizeof(*sp));
4456 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4461 sys_fhstatvfs(struct fhstatvfs_args *uap)
4463 struct thread *td = curthread;
4464 struct proc *p = td->td_proc;
4472 * Must be super user
4474 if ((error = priv_check(td, PRIV_ROOT)))
4477 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4482 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4486 if (p != NULL && !chroot_visible_mnt(mp, p)) {
4491 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4494 sp = &mp->mnt_vstat;
4496 if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
4500 if (mp->mnt_flag & MNT_RDONLY)
4501 sp->f_flag |= ST_RDONLY;
4502 if (mp->mnt_flag & MNT_NOSUID)
4503 sp->f_flag |= ST_NOSUID;
4504 error = copyout(sp, uap->buf, sizeof(*sp));
4512 * Syscall to push extended attribute configuration information into the
4513 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4514 * a command (int cmd), and attribute name and misc data. For now, the
4515 * attribute name is left in userspace for consumption by the VFS_op.
4516 * It will probably be changed to be copied into sysspace by the
4517 * syscall in the future, once issues with various consumers of the
4518 * attribute code have raised their hands.
4520 * Currently this is used only by UFS Extended Attributes.
4525 sys_extattrctl(struct extattrctl_args *uap)
4527 struct nlookupdata nd;
4529 char attrname[EXTATTR_MAXNAMELEN];
4539 if (error == 0 && uap->filename) {
4540 error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
4543 error = nlookup(&nd);
4545 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4549 if (error == 0 && uap->attrname) {
4550 error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
4555 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4557 error = nlookup(&nd);
4559 error = ncp_writechk(&nd.nl_nch);
4561 error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
4563 uap->attrname, nd.nl_cred);
4574 * Syscall to get a named extended attribute on a file or directory.
4579 sys_extattr_set_file(struct extattr_set_file_args *uap)
4581 char attrname[EXTATTR_MAXNAMELEN];
4582 struct nlookupdata nd;
4588 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4595 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4597 error = nlookup(&nd);
4599 error = ncp_writechk(&nd.nl_nch);
4601 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4608 bzero(&auio, sizeof(auio));
4609 aiov.iov_base = uap->data;
4610 aiov.iov_len = uap->nbytes;
4611 auio.uio_iov = &aiov;
4612 auio.uio_iovcnt = 1;
4613 auio.uio_offset = 0;
4614 auio.uio_resid = uap->nbytes;
4615 auio.uio_rw = UIO_WRITE;
4616 auio.uio_td = curthread;
4618 error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
4628 * Syscall to get a named extended attribute on a file or directory.
4633 sys_extattr_get_file(struct extattr_get_file_args *uap)
4635 char attrname[EXTATTR_MAXNAMELEN];
4636 struct nlookupdata nd;
4642 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4649 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4651 error = nlookup(&nd);
4653 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4660 bzero(&auio, sizeof(auio));
4661 aiov.iov_base = uap->data;
4662 aiov.iov_len = uap->nbytes;
4663 auio.uio_iov = &aiov;
4664 auio.uio_iovcnt = 1;
4665 auio.uio_offset = 0;
4666 auio.uio_resid = uap->nbytes;
4667 auio.uio_rw = UIO_READ;
4668 auio.uio_td = curthread;
4670 error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
4672 uap->sysmsg_result = uap->nbytes - auio.uio_resid;
4681 * Syscall to delete a named extended attribute from a file or directory.
4682 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4687 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4689 char attrname[EXTATTR_MAXNAMELEN];
4690 struct nlookupdata nd;
4694 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4699 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4701 error = nlookup(&nd);
4703 error = ncp_writechk(&nd.nl_nch);
4705 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4707 error = VOP_SETEXTATTR(vp, uap->attrnamespace,
4708 attrname, NULL, nd.nl_cred);
4718 * Determine if the mount is visible to the process.
4721 chroot_visible_mnt(struct mount *mp, struct proc *p)
4723 struct nchandle nch;
4726 * Traverse from the mount point upwards. If we hit the process
4727 * root then the mount point is visible to the process.
4729 nch = mp->mnt_ncmountpt;
4731 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4732 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4735 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4736 nch = nch.mount->mnt_ncmounton;
4738 nch.ncp = nch.ncp->nc_parent;
4743 * If the mount point is not visible to the process, but the
4744 * process root is in a subdirectory of the mount, return
4747 if (p->p_fd->fd_nrdir.mount == mp)