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 $
40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.135 2008/11/11 00:55:49 pavalos Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/sysent.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mountctl.h>
51 #include <sys/sysproto.h>
52 #include <sys/filedesc.h>
53 #include <sys/kernel.h>
54 #include <sys/fcntl.h>
56 #include <sys/linker.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
63 #include <sys/namei.h>
64 #include <sys/nlookup.h>
65 #include <sys/dirent.h>
66 #include <sys/extattr.h>
67 #include <sys/spinlock.h>
68 #include <sys/kern_syscall.h>
69 #include <sys/objcache.h>
70 #include <sys/sysctl.h>
73 #include <sys/file2.h>
74 #include <sys/spinlock2.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.
111 * mount_args(char *type, char *path, int flags, caddr_t data)
115 sys_mount(struct mount_args *uap)
117 struct thread *td = curthread;
118 struct proc *p = td->td_proc;
121 struct mount *mp, *nullmp;
122 struct vfsconf *vfsp;
123 int error, flag = 0, flag2 = 0;
126 struct nlookupdata nd;
127 char fstypename[MFSNAMELEN];
128 struct ucred *cred = p->p_ucred;
133 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
136 * Do not allow NFS export by non-root users.
138 if (uap->flags & MNT_EXPORTED) {
139 error = priv_check(td, PRIV_ROOT);
144 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
146 if (priv_check(td, PRIV_ROOT))
147 uap->flags |= MNT_NOSUID | MNT_NODEV;
150 * Lookup the requested path and extract the nch and vnode.
152 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
154 if ((error = nlookup(&nd)) == 0) {
155 if (nd.nl_nch.ncp->nc_vp == NULL)
165 * If the target filesystem is resolved via a nullfs mount, then
166 * nd.nl_nch.mount will be pointing to the nullfs mount structure
167 * instead of the target file system. We need it in case we are
170 nullmp = nd.nl_nch.mount;
173 * Extract the locked+refd ncp and cleanup the nd structure
176 cache_zero(&nd.nl_nch);
179 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
186 * now we have the locked ref'd nch and unreferenced vnode.
189 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
196 * Extract the file system type. We need to know this early, to take
197 * appropriate actions if we are dealing with a nullfs.
199 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
206 * Now we have an unlocked ref'd nch and a locked ref'd vp
208 if (uap->flags & MNT_UPDATE) {
209 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
215 if (strncmp(fstypename, "null", 5) == 0) {
223 flag2 = mp->mnt_kern_flag;
225 * We only allow the filesystem to be reloaded if it
226 * is currently mounted read-only.
228 if ((uap->flags & MNT_RELOAD) &&
229 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
232 return (EOPNOTSUPP); /* Needs translation */
235 * Only root, or the user that did the original mount is
236 * permitted to update it.
238 if (mp->mnt_stat.f_owner != cred->cr_uid &&
239 (error = priv_check(td, PRIV_ROOT))) {
244 if (vfs_busy(mp, LK_NOWAIT)) {
249 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
255 vp->v_flag |= VMOUNT;
257 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
262 * If the user is not root, ensure that they own the directory
263 * onto which we are attempting to mount.
265 if ((error = VOP_GETATTR(vp, &va)) ||
266 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
271 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
276 if (vp->v_type != VDIR) {
281 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
286 vfsp = vfsconf_find_by_name(fstypename);
290 /* Only load modules for root (very important!) */
291 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
296 error = linker_load_file(fstypename, &lf);
297 if (error || lf == NULL) {
305 /* lookup again, see if the VFS was loaded */
306 vfsp = vfsconf_find_by_name(fstypename);
309 linker_file_unload(lf);
315 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
320 vp->v_flag |= VMOUNT;
323 * Allocate and initialize the filesystem.
325 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
326 TAILQ_INIT(&mp->mnt_nvnodelist);
327 TAILQ_INIT(&mp->mnt_reservedvnlist);
328 TAILQ_INIT(&mp->mnt_jlist);
329 mp->mnt_nvnodelistsize = 0;
330 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
331 vfs_busy(mp, LK_NOWAIT);
332 mp->mnt_op = vfsp->vfc_vfsops;
334 vfsp->vfc_refcount++;
335 mp->mnt_stat.f_type = vfsp->vfc_typenum;
336 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
337 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
338 mp->mnt_stat.f_owner = cred->cr_uid;
339 mp->mnt_iosize_max = DFLTPHYS;
343 * Set the mount level flags.
345 if (uap->flags & MNT_RDONLY)
346 mp->mnt_flag |= MNT_RDONLY;
347 else if (mp->mnt_flag & MNT_RDONLY)
348 mp->mnt_kern_flag |= MNTK_WANTRDWR;
349 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
350 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
351 MNT_NOSYMFOLLOW | MNT_IGNORE |
352 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
353 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
354 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
355 MNT_NOSYMFOLLOW | MNT_IGNORE |
356 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
358 * Mount the filesystem.
359 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
362 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
363 if (mp->mnt_flag & MNT_UPDATE) {
364 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
365 mp->mnt_flag &= ~MNT_RDONLY;
366 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
367 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
370 mp->mnt_kern_flag = flag2;
373 vp->v_flag &= ~VMOUNT;
378 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
380 * Put the new filesystem on the mount list after root. The mount
381 * point gets its own mnt_ncmountpt (unless the VFS already set one
382 * up) which represents the root of the mount. The lookup code
383 * detects the mount point going forward and checks the root of
384 * the mount going backwards.
386 * It is not necessary to invalidate or purge the vnode underneath
387 * because elements under the mount will be given their own glue
391 if (mp->mnt_ncmountpt.ncp == NULL) {
393 * allocate, then unlock, but leave the ref intact
395 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
396 cache_unlock(&mp->mnt_ncmountpt);
398 mp->mnt_ncmounton = nch; /* inherits ref */
399 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
401 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
402 vp->v_flag &= ~VMOUNT;
403 mountlist_insert(mp, MNTINS_LAST);
405 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
406 error = vfs_allocate_syncvnode(mp);
408 error = VFS_START(mp, 0);
411 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
412 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
413 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
414 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
415 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
416 vp->v_flag &= ~VMOUNT;
417 mp->mnt_vfc->vfc_refcount--;
427 * Scan all active processes to see if any of them have a current
428 * or root directory onto which the new filesystem has just been
429 * mounted. If so, replace them with the new mount point.
431 * The passed ncp is ref'd and locked (from the mount code) and
432 * must be associated with the vnode representing the root of the
435 struct checkdirs_info {
436 struct nchandle old_nch;
437 struct nchandle new_nch;
438 struct vnode *old_vp;
439 struct vnode *new_vp;
442 static int checkdirs_callback(struct proc *p, void *data);
445 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
447 struct checkdirs_info info;
453 * If the old mount point's vnode has a usecount of 1, it is not
454 * being held as a descriptor anywhere.
456 olddp = old_nch->ncp->nc_vp;
457 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
461 * Force the root vnode of the new mount point to be resolved
462 * so we can update any matching processes.
465 if (VFS_ROOT(mp, &newdp))
466 panic("mount: lost mount");
467 cache_setunresolved(new_nch);
468 cache_setvp(new_nch, newdp);
471 * Special handling of the root node
473 if (rootvnode == olddp) {
475 vfs_cache_setroot(newdp, cache_hold(new_nch));
479 * Pass newdp separately so the callback does not have to access
480 * it via new_nch->ncp->nc_vp.
482 info.old_nch = *old_nch;
483 info.new_nch = *new_nch;
485 allproc_scan(checkdirs_callback, &info);
490 * NOTE: callback is not MP safe because the scanned process's filedesc
491 * structure can be ripped out from under us, amoung other things.
494 checkdirs_callback(struct proc *p, void *data)
496 struct checkdirs_info *info = data;
497 struct filedesc *fdp;
498 struct nchandle ncdrop1;
499 struct nchandle ncdrop2;
500 struct vnode *vprele1;
501 struct vnode *vprele2;
503 if ((fdp = p->p_fd) != NULL) {
504 cache_zero(&ncdrop1);
505 cache_zero(&ncdrop2);
510 * MPUNSAFE - XXX fdp can be pulled out from under a
513 * A shared filedesc is ok, we don't have to copy it
514 * because we are making this change globally.
516 spin_lock_wr(&fdp->fd_spin);
517 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
518 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
519 vprele1 = fdp->fd_cdir;
521 fdp->fd_cdir = info->new_vp;
522 ncdrop1 = fdp->fd_ncdir;
523 cache_copy(&info->new_nch, &fdp->fd_ncdir);
525 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
526 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
527 vprele2 = fdp->fd_rdir;
529 fdp->fd_rdir = info->new_vp;
530 ncdrop2 = fdp->fd_nrdir;
531 cache_copy(&info->new_nch, &fdp->fd_nrdir);
533 spin_unlock_wr(&fdp->fd_spin);
535 cache_drop(&ncdrop1);
537 cache_drop(&ncdrop2);
547 * Unmount a file system.
549 * Note: unmount takes a path to the vnode mounted on as argument,
550 * not special file (as before).
553 * umount_args(char *path, int flags)
557 sys_unmount(struct unmount_args *uap)
559 struct thread *td = curthread;
560 struct proc *p = td->td_proc;
561 struct mount *mp = NULL;
563 struct nlookupdata nd;
566 if (p->p_ucred->cr_prison != NULL)
568 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
571 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
573 error = nlookup(&nd);
577 mp = nd.nl_nch.mount;
580 * Only root, or the user that did the original mount is
581 * permitted to unmount this filesystem.
583 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
584 (error = priv_check(td, PRIV_ROOT)))
588 * Don't allow unmounting the root file system.
590 if (mp->mnt_flag & MNT_ROOTFS) {
596 * Must be the root of the filesystem
598 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
607 return (dounmount(mp, uap->flags));
611 * Do the actual file system unmount.
614 dounmount_interlock(struct mount *mp)
616 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
618 mp->mnt_kern_flag |= MNTK_UNMOUNT;
623 dounmount(struct mount *mp, int flags)
625 struct namecache *ncp;
634 * Exclusive access for unmounting purposes
636 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
640 * Allow filesystems to detect that a forced unmount is in progress.
642 if (flags & MNT_FORCE)
643 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
644 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
645 error = lockmgr(&mp->mnt_lock, lflags);
647 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
648 if (mp->mnt_kern_flag & MNTK_MWAIT)
653 if (mp->mnt_flag & MNT_EXPUBLIC)
654 vfs_setpublicfs(NULL, NULL, NULL);
656 vfs_msync(mp, MNT_WAIT);
657 async_flag = mp->mnt_flag & MNT_ASYNC;
658 mp->mnt_flag &=~ MNT_ASYNC;
661 * If this filesystem isn't aliasing other filesystems,
662 * try to invalidate any remaining namecache entries and
663 * check the count afterwords.
665 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
666 cache_lock(&mp->mnt_ncmountpt);
667 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
668 cache_unlock(&mp->mnt_ncmountpt);
670 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
671 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
673 if ((flags & MNT_FORCE) == 0) {
675 mount_warning(mp, "Cannot unmount: "
681 mount_warning(mp, "Forced unmount: "
692 * nchandle records ref the mount structure. Expect a count of 1
693 * (our mount->mnt_ncmountpt).
695 if (mp->mnt_refs != 1) {
696 if ((flags & MNT_FORCE) == 0) {
697 mount_warning(mp, "Cannot unmount: "
698 "%d process references still "
699 "present", mp->mnt_refs);
702 mount_warning(mp, "Forced unmount: "
703 "%d process references still "
704 "present", mp->mnt_refs);
710 * Decomission our special mnt_syncer vnode. This also stops
711 * the vnlru code. If we are unable to unmount we recommission
715 if ((vp = mp->mnt_syncer) != NULL) {
716 mp->mnt_syncer = NULL;
719 if (((mp->mnt_flag & MNT_RDONLY) ||
720 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
721 (flags & MNT_FORCE)) {
722 error = VFS_UNMOUNT(mp, flags);
726 if (mp->mnt_syncer == NULL)
727 vfs_allocate_syncvnode(mp);
728 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
729 mp->mnt_flag |= async_flag;
730 lockmgr(&mp->mnt_lock, LK_RELEASE);
731 if (mp->mnt_kern_flag & MNTK_MWAIT)
736 * Clean up any journals still associated with the mount after
737 * filesystem activity has ceased.
739 journal_remove_all_journals(mp,
740 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
742 mountlist_remove(mp);
745 * Remove any installed vnode ops here so the individual VFSs don't
748 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
749 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
750 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
751 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
752 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
754 if (mp->mnt_ncmountpt.ncp != NULL) {
755 nch = mp->mnt_ncmountpt;
756 cache_zero(&mp->mnt_ncmountpt);
757 cache_clrmountpt(&nch);
760 if (mp->mnt_ncmounton.ncp != NULL) {
761 nch = mp->mnt_ncmounton;
762 cache_zero(&mp->mnt_ncmounton);
763 cache_clrmountpt(&nch);
767 mp->mnt_vfc->vfc_refcount--;
768 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
769 panic("unmount: dangling vnode");
770 lockmgr(&mp->mnt_lock, LK_RELEASE);
771 if (mp->mnt_kern_flag & MNTK_MWAIT)
780 mount_warning(struct mount *mp, const char *ctl, ...)
787 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
788 kprintf("unmount(%s): ", ptr);
793 kprintf("unmount(%p", mp);
794 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
795 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
804 * Shim cache_fullpath() to handle the case where a process is chrooted into
805 * a subdirectory of a mount. In this case if the root mount matches the
806 * process root directory's mount we have to specify the process's root
807 * directory instead of the mount point, because the mount point might
808 * be above the root directory.
812 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
814 struct nchandle *nch;
816 if (p && p->p_fd->fd_nrdir.mount == mp)
817 nch = &p->p_fd->fd_nrdir;
819 nch = &mp->mnt_ncmountpt;
820 return(cache_fullpath(p, nch, rb, fb));
824 * Sync each mounted filesystem.
828 static int syncprt = 0;
829 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
832 static int sync_callback(struct mount *mp, void *data);
836 sys_sync(struct sync_args *uap)
838 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
841 * print out buffer pool stat information on each sync() call.
851 sync_callback(struct mount *mp, void *data __unused)
855 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
856 asyncflag = mp->mnt_flag & MNT_ASYNC;
857 mp->mnt_flag &= ~MNT_ASYNC;
858 vfs_msync(mp, MNT_NOWAIT);
859 VFS_SYNC(mp, MNT_NOWAIT);
860 mp->mnt_flag |= asyncflag;
865 /* XXX PRISON: could be per prison flag */
866 static int prison_quotas;
868 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
872 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
874 * Change filesystem quotas.
878 sys_quotactl(struct quotactl_args *uap)
880 struct nlookupdata nd;
888 if (p->p_ucred->cr_prison && !prison_quotas)
891 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
893 error = nlookup(&nd);
895 mp = nd.nl_nch.mount;
896 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
897 uap->arg, nd.nl_cred);
904 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
905 * void *buf, int buflen)
907 * This function operates on a mount point and executes the specified
908 * operation using the specified control data, and possibly returns data.
910 * The actual number of bytes stored in the result buffer is returned, 0
911 * if none, otherwise an error is returned.
915 sys_mountctl(struct mountctl_args *uap)
917 struct thread *td = curthread;
918 struct proc *p = td->td_proc;
926 * Sanity and permissions checks. We must be root.
929 if (p->p_ucred->cr_prison != NULL)
931 if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
932 (error = priv_check(td, PRIV_ROOT)) != 0)
936 * Argument length checks
938 if (uap->ctllen < 0 || uap->ctllen > 1024)
940 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
942 if (uap->path == NULL)
946 * Allocate the necessary buffers and copyin data
948 path = objcache_get(namei_oc, M_WAITOK);
949 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
954 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
955 error = copyin(uap->ctl, ctl, uap->ctllen);
960 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
963 * Validate the descriptor
966 fp = holdfp(p->p_fd, uap->fd, -1);
976 * Execute the internal kernel function and clean up.
978 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
981 if (error == 0 && uap->sysmsg_result > 0)
982 error = copyout(buf, uap->buf, uap->sysmsg_result);
985 objcache_put(namei_oc, path);
994 * Execute a mount control operation by resolving the path to a mount point
995 * and calling vop_mountctl().
997 * Use the mount point from the nch instead of the vnode so nullfs mounts
998 * can properly spike the VOP.
1001 kern_mountctl(const char *path, int op, struct file *fp,
1002 const void *ctl, int ctllen,
1003 void *buf, int buflen, int *res)
1007 struct nlookupdata nd;
1012 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1014 error = nlookup(&nd);
1016 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1017 mp = nd.nl_nch.mount;
1024 * Must be the root of the filesystem
1026 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1030 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1037 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1039 struct thread *td = curthread;
1040 struct proc *p = td->td_proc;
1043 char *fullpath, *freepath;
1046 if ((error = nlookup(nd)) != 0)
1048 mp = nd->nl_nch.mount;
1050 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1053 error = mount_path(p, mp, &fullpath, &freepath);
1056 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1057 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1058 kfree(freepath, M_TEMP);
1060 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1061 bcopy(sp, buf, sizeof(*buf));
1062 /* Only root should have access to the fsid's. */
1063 if (priv_check(td, PRIV_ROOT))
1064 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1069 * statfs_args(char *path, struct statfs *buf)
1071 * Get filesystem statistics.
1074 sys_statfs(struct statfs_args *uap)
1076 struct nlookupdata nd;
1080 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1082 error = kern_statfs(&nd, &buf);
1085 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1090 kern_fstatfs(int fd, struct statfs *buf)
1092 struct thread *td = curthread;
1093 struct proc *p = td->td_proc;
1097 char *fullpath, *freepath;
1101 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1103 mp = ((struct vnode *)fp->f_data)->v_mount;
1108 if (fp->f_cred == NULL) {
1113 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1116 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1118 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1119 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1120 kfree(freepath, M_TEMP);
1122 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1123 bcopy(sp, buf, sizeof(*buf));
1125 /* Only root should have access to the fsid's. */
1126 if (priv_check(td, PRIV_ROOT))
1127 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1135 * fstatfs_args(int fd, struct statfs *buf)
1137 * Get filesystem statistics.
1140 sys_fstatfs(struct fstatfs_args *uap)
1145 error = kern_fstatfs(uap->fd, &buf);
1148 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1153 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1159 if ((error = nlookup(nd)) != 0)
1161 mp = nd->nl_nch.mount;
1162 sp = &mp->mnt_vstat;
1163 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1167 if (mp->mnt_flag & MNT_RDONLY)
1168 sp->f_flag |= ST_RDONLY;
1169 if (mp->mnt_flag & MNT_NOSUID)
1170 sp->f_flag |= ST_NOSUID;
1171 bcopy(sp, buf, sizeof(*buf));
1176 * statfs_args(char *path, struct statfs *buf)
1178 * Get filesystem statistics.
1181 sys_statvfs(struct statvfs_args *uap)
1183 struct nlookupdata nd;
1187 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1189 error = kern_statvfs(&nd, &buf);
1192 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1197 kern_fstatvfs(int fd, struct statvfs *buf)
1199 struct thread *td = curthread;
1200 struct proc *p = td->td_proc;
1207 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1209 mp = ((struct vnode *)fp->f_data)->v_mount;
1214 if (fp->f_cred == NULL) {
1218 sp = &mp->mnt_vstat;
1219 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1223 if (mp->mnt_flag & MNT_RDONLY)
1224 sp->f_flag |= ST_RDONLY;
1225 if (mp->mnt_flag & MNT_NOSUID)
1226 sp->f_flag |= ST_NOSUID;
1228 bcopy(sp, buf, sizeof(*buf));
1236 * fstatfs_args(int fd, struct statfs *buf)
1238 * Get filesystem statistics.
1241 sys_fstatvfs(struct fstatvfs_args *uap)
1246 error = kern_fstatvfs(uap->fd, &buf);
1249 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1254 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1256 * Get statistics on all filesystems.
1259 struct getfsstat_info {
1260 struct statfs *sfsp;
1268 static int getfsstat_callback(struct mount *, void *);
1272 sys_getfsstat(struct getfsstat_args *uap)
1274 struct thread *td = curthread;
1275 struct proc *p = td->td_proc;
1276 struct getfsstat_info info;
1278 bzero(&info, sizeof(info));
1280 info.maxcount = uap->bufsize / sizeof(struct statfs);
1281 info.sfsp = uap->buf;
1283 info.flags = uap->flags;
1286 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1287 if (info.sfsp && info.count > info.maxcount)
1288 uap->sysmsg_result = info.maxcount;
1290 uap->sysmsg_result = info.count;
1291 return (info.error);
1295 getfsstat_callback(struct mount *mp, void *data)
1297 struct getfsstat_info *info = data;
1303 if (info->sfsp && info->count < info->maxcount) {
1304 if (info->p && !chroot_visible_mnt(mp, info->p))
1309 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1310 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1311 * overrides MNT_WAIT.
1313 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1314 (info->flags & MNT_WAIT)) &&
1315 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1318 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1320 error = mount_path(info->p, mp, &fullpath, &freepath);
1322 info->error = error;
1325 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1326 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1327 kfree(freepath, M_TEMP);
1329 error = copyout(sp, info->sfsp, sizeof(*sp));
1331 info->error = error;
1341 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1342 long bufsize, int flags)
1344 * Get statistics on all filesystems.
1347 struct getvfsstat_info {
1348 struct statfs *sfsp;
1349 struct statvfs *vsfsp;
1357 static int getvfsstat_callback(struct mount *, void *);
1361 sys_getvfsstat(struct getvfsstat_args *uap)
1363 struct thread *td = curthread;
1364 struct proc *p = td->td_proc;
1365 struct getvfsstat_info info;
1367 bzero(&info, sizeof(info));
1369 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1370 info.sfsp = uap->buf;
1371 info.vsfsp = uap->vbuf;
1373 info.flags = uap->flags;
1376 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1377 if (info.vsfsp && info.count > info.maxcount)
1378 uap->sysmsg_result = info.maxcount;
1380 uap->sysmsg_result = info.count;
1381 return (info.error);
1385 getvfsstat_callback(struct mount *mp, void *data)
1387 struct getvfsstat_info *info = data;
1389 struct statvfs *vsp;
1394 if (info->vsfsp && info->count < info->maxcount) {
1395 if (info->p && !chroot_visible_mnt(mp, info->p))
1398 vsp = &mp->mnt_vstat;
1401 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1402 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1403 * overrides MNT_WAIT.
1405 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1406 (info->flags & MNT_WAIT)) &&
1407 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1410 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1412 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1413 (info->flags & MNT_WAIT)) &&
1414 (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1418 if (mp->mnt_flag & MNT_RDONLY)
1419 vsp->f_flag |= ST_RDONLY;
1420 if (mp->mnt_flag & MNT_NOSUID)
1421 vsp->f_flag |= ST_NOSUID;
1423 error = mount_path(info->p, mp, &fullpath, &freepath);
1425 info->error = error;
1428 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1429 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1430 kfree(freepath, M_TEMP);
1432 error = copyout(sp, info->sfsp, sizeof(*sp));
1434 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1436 info->error = error;
1448 * fchdir_args(int fd)
1450 * Change current working directory to a given file descriptor.
1454 sys_fchdir(struct fchdir_args *uap)
1456 struct thread *td = curthread;
1457 struct proc *p = td->td_proc;
1458 struct filedesc *fdp = p->p_fd;
1459 struct vnode *vp, *ovp;
1462 struct nchandle nch, onch, tnch;
1465 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1467 vp = (struct vnode *)fp->f_data;
1469 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1470 if (fp->f_nchandle.ncp == NULL)
1473 error = checkvp_chdir(vp, td);
1479 cache_copy(&fp->f_nchandle, &nch);
1482 * If the ncp has become a mount point, traverse through
1486 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1487 (mp = cache_findmount(&nch)) != NULL
1489 error = nlookup_mp(mp, &tnch);
1491 cache_unlock(&tnch); /* leave ref intact */
1493 vp = tnch.ncp->nc_vp;
1494 error = vget(vp, LK_SHARED);
1495 KKASSERT(error == 0);
1502 onch = fdp->fd_ncdir;
1503 vn_unlock(vp); /* leave ref intact */
1505 fdp->fd_ncdir = nch;
1517 kern_chdir(struct nlookupdata *nd)
1519 struct thread *td = curthread;
1520 struct proc *p = td->td_proc;
1521 struct filedesc *fdp = p->p_fd;
1522 struct vnode *vp, *ovp;
1523 struct nchandle onch;
1526 if ((error = nlookup(nd)) != 0)
1528 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1530 if ((error = vget(vp, LK_SHARED)) != 0)
1533 error = checkvp_chdir(vp, td);
1537 onch = fdp->fd_ncdir;
1538 cache_unlock(&nd->nl_nch); /* leave reference intact */
1539 fdp->fd_ncdir = nd->nl_nch;
1543 cache_zero(&nd->nl_nch);
1551 * chdir_args(char *path)
1553 * Change current working directory (``.'').
1556 sys_chdir(struct chdir_args *uap)
1558 struct nlookupdata nd;
1561 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1563 error = kern_chdir(&nd);
1569 * Helper function for raised chroot(2) security function: Refuse if
1570 * any filedescriptors are open directories.
1573 chroot_refuse_vdir_fds(struct filedesc *fdp)
1580 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1581 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1583 vp = (struct vnode *)fp->f_data;
1584 if (vp->v_type != VDIR) {
1595 * This sysctl determines if we will allow a process to chroot(2) if it
1596 * has a directory open:
1597 * 0: disallowed for all processes.
1598 * 1: allowed for processes that were not already chroot(2)'ed.
1599 * 2: allowed for all processes.
1602 static int chroot_allow_open_directories = 1;
1604 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1605 &chroot_allow_open_directories, 0, "");
1608 * chroot to the specified namecache entry. We obtain the vp from the
1609 * namecache data. The passed ncp must be locked and referenced and will
1610 * remain locked and referenced on return.
1613 kern_chroot(struct nchandle *nch)
1615 struct thread *td = curthread;
1616 struct proc *p = td->td_proc;
1617 struct filedesc *fdp = p->p_fd;
1622 * Only privileged user can chroot
1624 error = priv_check_cred(p->p_ucred, PRIV_VFS_CHROOT, 0);
1629 * Disallow open directory descriptors (fchdir() breakouts).
1631 if (chroot_allow_open_directories == 0 ||
1632 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1633 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1636 if ((vp = nch->ncp->nc_vp) == NULL)
1639 if ((error = vget(vp, LK_SHARED)) != 0)
1643 * Check the validity of vp as a directory to change to and
1644 * associate it with rdir/jdir.
1646 error = checkvp_chdir(vp, td);
1647 vn_unlock(vp); /* leave reference intact */
1649 vrele(fdp->fd_rdir);
1650 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1651 cache_drop(&fdp->fd_nrdir);
1652 cache_copy(nch, &fdp->fd_nrdir);
1653 if (fdp->fd_jdir == NULL) {
1656 cache_copy(nch, &fdp->fd_njdir);
1665 * chroot_args(char *path)
1667 * Change notion of root (``/'') directory.
1671 sys_chroot(struct chroot_args *uap)
1673 struct thread *td = curthread;
1674 struct nlookupdata nd;
1677 KKASSERT(td->td_proc);
1678 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1683 nd.nl_flags |= NLC_EXEC;
1684 error = nlookup(&nd);
1686 error = kern_chroot(&nd.nl_nch);
1692 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1693 * determine whether it is legal to chdir to the vnode. The vnode's state
1694 * is not changed by this call.
1697 checkvp_chdir(struct vnode *vp, struct thread *td)
1701 if (vp->v_type != VDIR)
1704 error = VOP_EACCESS(vp, VEXEC, td->td_proc->p_ucred);
1709 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1711 struct thread *td = curthread;
1712 struct proc *p = td->td_proc;
1713 struct lwp *lp = td->td_lwp;
1714 struct filedesc *fdp = p->p_fd;
1719 int type, indx, error;
1722 if ((oflags & O_ACCMODE) == O_ACCMODE)
1724 flags = FFLAGS(oflags);
1725 error = falloc(p, &nfp, NULL);
1729 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
1732 * XXX p_dupfd is a real mess. It allows a device to return a
1733 * file descriptor to be duplicated rather then doing the open
1739 * Call vn_open() to do the lookup and assign the vnode to the
1740 * file pointer. vn_open() does not change the ref count on fp
1741 * and the vnode, on success, will be inherited by the file pointer
1744 nd->nl_flags |= NLC_LOCKVP;
1745 error = vn_open(nd, fp, flags, cmode);
1749 * handle special fdopen() case. bleh. dupfdopen() is
1750 * responsible for dropping the old contents of ofiles[indx]
1753 * Note that fsetfd() will add a ref to fp which represents
1754 * the fd_files[] assignment. We must still drop our
1757 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1758 if (fdalloc(p, 0, &indx) == 0) {
1759 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1762 fdrop(fp); /* our ref */
1765 fsetfd(p, NULL, indx);
1768 fdrop(fp); /* our ref */
1769 if (error == ERESTART)
1775 * ref the vnode for ourselves so it can't be ripped out from under
1776 * is. XXX need an ND flag to request that the vnode be returned
1779 * Reserve a file descriptor but do not assign it until the open
1782 vp = (struct vnode *)fp->f_data;
1784 if ((error = fdalloc(p, 0, &indx)) != 0) {
1791 * If no error occurs the vp will have been assigned to the file
1796 if (flags & (O_EXLOCK | O_SHLOCK)) {
1797 lf.l_whence = SEEK_SET;
1800 if (flags & O_EXLOCK)
1801 lf.l_type = F_WRLCK;
1803 lf.l_type = F_RDLCK;
1804 if (flags & FNONBLOCK)
1809 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1811 * lock request failed. Clean up the reserved
1815 fsetfd(p, NULL, indx);
1819 fp->f_flag |= FHASLOCK;
1823 * Assert that all regular file vnodes were created with a object.
1825 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1826 ("open: regular file has no backing object after vn_open"));
1832 * release our private reference, leaving the one associated with the
1833 * descriptor table intact.
1835 fsetfd(p, fp, indx);
1842 * open_args(char *path, int flags, int mode)
1844 * Check permissions, allocate an open file structure,
1845 * and call the device open routine if any.
1848 sys_open(struct open_args *uap)
1850 struct nlookupdata nd;
1853 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1855 error = kern_open(&nd, uap->flags,
1856 uap->mode, &uap->sysmsg_result);
1863 * openat_args(int fd, char *path, int flags, int mode)
1866 sys_openat(struct openat_args *uap)
1868 struct nlookupdata nd;
1872 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
1874 error = kern_open(&nd, uap->flags, uap->mode,
1875 &uap->sysmsg_result);
1877 nlookup_done_at(&nd, fp);
1882 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1884 struct thread *td = curthread;
1885 struct proc *p = td->td_proc;
1894 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1895 vattr.va_rmajor = rmajor;
1896 vattr.va_rminor = rminor;
1898 switch (mode & S_IFMT) {
1899 case S_IFMT: /* used by badsect to flag bad sectors */
1900 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_BAD, 0);
1901 vattr.va_type = VBAD;
1904 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1905 vattr.va_type = VCHR;
1908 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1909 vattr.va_type = VBLK;
1912 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_WHT, 0);
1915 case S_IFDIR: /* special directories support for HAMMER */
1916 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_DIR, 0);
1917 vattr.va_type = VDIR;
1928 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1929 if ((error = nlookup(nd)) != 0)
1931 if (nd->nl_nch.ncp->nc_vp)
1933 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1937 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1938 nd->nl_cred, NAMEI_CREATE);
1941 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1942 &vp, nd->nl_cred, &vattr);
1950 * mknod_args(char *path, int mode, int dev)
1952 * Create a special file.
1955 sys_mknod(struct mknod_args *uap)
1957 struct nlookupdata nd;
1960 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1962 error = kern_mknod(&nd, uap->mode,
1963 umajor(uap->dev), uminor(uap->dev));
1970 kern_mkfifo(struct nlookupdata *nd, int mode)
1972 struct thread *td = curthread;
1973 struct proc *p = td->td_proc;
1980 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1981 if ((error = nlookup(nd)) != 0)
1983 if (nd->nl_nch.ncp->nc_vp)
1985 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1989 vattr.va_type = VFIFO;
1990 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1992 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
1999 * mkfifo_args(char *path, int mode)
2001 * Create a named pipe.
2004 sys_mkfifo(struct mkfifo_args *uap)
2006 struct nlookupdata nd;
2009 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2011 error = kern_mkfifo(&nd, uap->mode);
2016 static int hardlink_check_uid = 0;
2017 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2018 &hardlink_check_uid, 0,
2019 "Unprivileged processes cannot create hard links to files owned by other "
2021 static int hardlink_check_gid = 0;
2022 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2023 &hardlink_check_gid, 0,
2024 "Unprivileged processes cannot create hard links to files owned by other "
2028 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2034 * Shortcut if disabled
2036 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2040 * Privileged user can always hardlink
2042 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2046 * Otherwise only if the originating file is owned by the
2047 * same user or group. Note that any group is allowed if
2048 * the file is owned by the caller.
2050 error = VOP_GETATTR(vp, &va);
2054 if (hardlink_check_uid) {
2055 if (cred->cr_uid != va.va_uid)
2059 if (hardlink_check_gid) {
2060 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2068 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2070 struct thread *td = curthread;
2075 * Lookup the source and obtained a locked vnode.
2077 * You may only hardlink a file which you have write permission
2078 * on or which you own.
2080 * XXX relookup on vget failure / race ?
2083 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2084 if ((error = nlookup(nd)) != 0)
2086 vp = nd->nl_nch.ncp->nc_vp;
2087 KKASSERT(vp != NULL);
2088 if (vp->v_type == VDIR)
2089 return (EPERM); /* POSIX */
2090 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2092 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2096 * Unlock the source so we can lookup the target without deadlocking
2097 * (XXX vp is locked already, possible other deadlock?). The target
2100 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2101 nd->nl_flags &= ~NLC_NCPISLOCKED;
2102 cache_unlock(&nd->nl_nch);
2104 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2105 if ((error = nlookup(linknd)) != 0) {
2109 if (linknd->nl_nch.ncp->nc_vp) {
2115 * Finally run the new API VOP.
2117 error = can_hardlink(vp, td, td->td_proc->p_ucred);
2119 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2120 vp, linknd->nl_cred);
2127 * link_args(char *path, char *link)
2129 * Make a hard file link.
2132 sys_link(struct link_args *uap)
2134 struct nlookupdata nd, linknd;
2137 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2139 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2141 error = kern_link(&nd, &linknd);
2142 nlookup_done(&linknd);
2149 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2157 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2158 if ((error = nlookup(nd)) != 0)
2160 if (nd->nl_nch.ncp->nc_vp)
2162 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2166 vattr.va_mode = mode;
2167 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2174 * symlink(char *path, char *link)
2176 * Make a symbolic link.
2179 sys_symlink(struct symlink_args *uap)
2181 struct thread *td = curthread;
2182 struct nlookupdata nd;
2187 path = objcache_get(namei_oc, M_WAITOK);
2188 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2190 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2192 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2193 error = kern_symlink(&nd, path, mode);
2197 objcache_put(namei_oc, path);
2202 * undelete_args(char *path)
2204 * Delete a whiteout from the filesystem.
2208 sys_undelete(struct undelete_args *uap)
2210 struct nlookupdata nd;
2213 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2215 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2217 error = nlookup(&nd);
2219 error = ncp_writechk(&nd.nl_nch);
2221 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2229 kern_unlink(struct nlookupdata *nd)
2234 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2235 if ((error = nlookup(nd)) != 0)
2237 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2239 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2244 * unlink_args(char *path)
2246 * Delete a name from the filesystem.
2249 sys_unlink(struct unlink_args *uap)
2251 struct nlookupdata nd;
2254 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2256 error = kern_unlink(&nd);
2263 * unlinkat_args(int fd, char *path, int flags)
2265 * Delete the file or directory entry pointed to by fd/path.
2268 sys_unlinkat(struct unlinkat_args *uap)
2270 struct nlookupdata nd;
2274 if (uap->flags & ~AT_REMOVEDIR)
2277 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2279 if (uap->flags & AT_REMOVEDIR)
2280 error = kern_rmdir(&nd);
2282 error = kern_unlink(&nd);
2284 nlookup_done_at(&nd, fp);
2289 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2291 struct thread *td = curthread;
2292 struct proc *p = td->td_proc;
2299 fp = holdfp(p->p_fd, fd, -1);
2302 if (fp->f_type != DTYPE_VNODE) {
2306 vp = (struct vnode *)fp->f_data;
2310 new_offset = fp->f_offset + offset;
2314 error = VOP_GETATTR(vp, &vattr);
2315 new_offset = offset + vattr.va_size;
2318 new_offset = offset;
2328 * Validate the seek position. Negative offsets are not allowed
2329 * for regular files or directories.
2331 * Normally we would also not want to allow negative offsets for
2332 * character and block-special devices. However kvm addresses
2333 * on 64 bit architectures might appear to be negative and must
2337 if (new_offset < 0 &&
2338 (vp->v_type == VREG || vp->v_type == VDIR)) {
2341 fp->f_offset = new_offset;
2344 *res = fp->f_offset;
2351 * lseek_args(int fd, int pad, off_t offset, int whence)
2353 * Reposition read/write file offset.
2356 sys_lseek(struct lseek_args *uap)
2360 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2361 &uap->sysmsg_offset);
2367 * Check if current process can access given file. amode is a bitmask of *_OK
2368 * access bits. flags is a bitmask of AT_* flags.
2371 kern_access(struct nlookupdata *nd, int amode, int flags)
2376 if (flags & ~AT_EACCESS)
2378 if ((error = nlookup(nd)) != 0)
2381 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2385 /* Flags == 0 means only check for existence. */
2394 if ((mode & VWRITE) == 0 ||
2395 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2396 error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2399 * If the file handle is stale we have to re-resolve the
2400 * entry. This is a hack at the moment.
2402 if (error == ESTALE) {
2404 cache_setunresolved(&nd->nl_nch);
2405 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2418 * access_args(char *path, int flags)
2420 * Check access permissions.
2423 sys_access(struct access_args *uap)
2425 struct nlookupdata nd;
2428 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2430 error = kern_access(&nd, uap->flags, 0);
2437 * faccessat_args(int fd, char *path, int amode, int flags)
2439 * Check access permissions.
2442 sys_faccessat(struct faccessat_args *uap)
2444 struct nlookupdata nd;
2448 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2451 error = kern_access(&nd, uap->amode, uap->flags);
2452 nlookup_done_at(&nd, fp);
2458 kern_stat(struct nlookupdata *nd, struct stat *st)
2464 if ((error = nlookup(nd)) != 0)
2467 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2471 if ((error = vget(vp, LK_SHARED)) != 0)
2473 error = vn_stat(vp, st, nd->nl_cred);
2476 * If the file handle is stale we have to re-resolve the entry. This
2477 * is a hack at the moment.
2479 if (error == ESTALE) {
2481 cache_setunresolved(&nd->nl_nch);
2482 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2492 * stat_args(char *path, struct stat *ub)
2494 * Get file status; this version follows links.
2497 sys_stat(struct stat_args *uap)
2499 struct nlookupdata nd;
2503 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2505 error = kern_stat(&nd, &st);
2507 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2514 * lstat_args(char *path, struct stat *ub)
2516 * Get file status; this version does not follow links.
2519 sys_lstat(struct lstat_args *uap)
2521 struct nlookupdata nd;
2525 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2527 error = kern_stat(&nd, &st);
2529 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2536 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2538 * Get status of file pointed to by fd/path.
2541 sys_fstatat(struct fstatat_args *uap)
2543 struct nlookupdata nd;
2549 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2552 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2554 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2555 UIO_USERSPACE, flags);
2557 error = kern_stat(&nd, &st);
2559 error = copyout(&st, uap->sb, sizeof(*uap->sb));
2561 nlookup_done_at(&nd, fp);
2566 * pathconf_Args(char *path, int name)
2568 * Get configurable pathname variables.
2572 sys_pathconf(struct pathconf_args *uap)
2574 struct nlookupdata nd;
2579 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2581 error = nlookup(&nd);
2583 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2586 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
2594 * kern_readlink isn't properly split yet. There is a copyin burried
2595 * in VOP_READLINK().
2598 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2600 struct thread *td = curthread;
2601 struct proc *p = td->td_proc;
2607 if ((error = nlookup(nd)) != 0)
2609 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2612 if (vp->v_type != VLNK) {
2615 aiov.iov_base = buf;
2616 aiov.iov_len = count;
2617 auio.uio_iov = &aiov;
2618 auio.uio_iovcnt = 1;
2619 auio.uio_offset = 0;
2620 auio.uio_rw = UIO_READ;
2621 auio.uio_segflg = UIO_USERSPACE;
2623 auio.uio_resid = count;
2624 error = VOP_READLINK(vp, &auio, p->p_ucred);
2627 *res = count - auio.uio_resid;
2632 * readlink_args(char *path, char *buf, int count)
2634 * Return target name of a symbolic link.
2637 sys_readlink(struct readlink_args *uap)
2639 struct nlookupdata nd;
2642 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2644 error = kern_readlink(&nd, uap->buf, uap->count,
2645 &uap->sysmsg_result);
2652 setfflags(struct vnode *vp, int flags)
2654 struct thread *td = curthread;
2655 struct proc *p = td->td_proc;
2660 * Prevent non-root users from setting flags on devices. When
2661 * a device is reused, users can retain ownership of the device
2662 * if they are allowed to set flags and programs assume that
2663 * chown can't fail when done as root.
2665 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2666 ((error = priv_check_cred(p->p_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
2670 * note: vget is required for any operation that might mod the vnode
2671 * so VINACTIVE is properly cleared.
2673 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2675 vattr.va_flags = flags;
2676 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2683 * chflags(char *path, int flags)
2685 * Change flags of a file given a path name.
2689 sys_chflags(struct chflags_args *uap)
2691 struct nlookupdata nd;
2696 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2698 error = nlookup(&nd);
2700 error = ncp_writechk(&nd.nl_nch);
2702 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2705 error = setfflags(vp, uap->flags);
2712 * lchflags(char *path, int flags)
2714 * Change flags of a file given a path name, but don't follow symlinks.
2718 sys_lchflags(struct lchflags_args *uap)
2720 struct nlookupdata nd;
2725 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2727 error = nlookup(&nd);
2729 error = ncp_writechk(&nd.nl_nch);
2731 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2734 error = setfflags(vp, uap->flags);
2741 * fchflags_args(int fd, int flags)
2743 * Change flags of a file given a file descriptor.
2747 sys_fchflags(struct fchflags_args *uap)
2749 struct thread *td = curthread;
2750 struct proc *p = td->td_proc;
2754 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2756 if (fp->f_nchandle.ncp)
2757 error = ncp_writechk(&fp->f_nchandle);
2759 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2765 setfmode(struct vnode *vp, int mode)
2767 struct thread *td = curthread;
2768 struct proc *p = td->td_proc;
2773 * note: vget is required for any operation that might mod the vnode
2774 * so VINACTIVE is properly cleared.
2776 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2778 vattr.va_mode = mode & ALLPERMS;
2779 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2786 kern_chmod(struct nlookupdata *nd, int mode)
2791 if ((error = nlookup(nd)) != 0)
2793 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2795 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2796 error = setfmode(vp, mode);
2802 * chmod_args(char *path, int mode)
2804 * Change mode of a file given path name.
2808 sys_chmod(struct chmod_args *uap)
2810 struct nlookupdata nd;
2813 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2815 error = kern_chmod(&nd, uap->mode);
2821 * lchmod_args(char *path, int mode)
2823 * Change mode of a file given path name (don't follow links.)
2827 sys_lchmod(struct lchmod_args *uap)
2829 struct nlookupdata nd;
2832 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2834 error = kern_chmod(&nd, uap->mode);
2840 * fchmod_args(int fd, int mode)
2842 * Change mode of a file given a file descriptor.
2846 sys_fchmod(struct fchmod_args *uap)
2848 struct thread *td = curthread;
2849 struct proc *p = td->td_proc;
2853 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2855 if (fp->f_nchandle.ncp)
2856 error = ncp_writechk(&fp->f_nchandle);
2858 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2864 * fchmodat_args(char *path, int mode)
2866 * Change mode of a file pointed to by fd/path.
2869 sys_fchmodat(struct fchmodat_args *uap)
2871 struct nlookupdata nd;
2876 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
2878 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
2880 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
2881 UIO_USERSPACE, flags);
2883 error = kern_chmod(&nd, uap->mode);
2884 nlookup_done_at(&nd, fp);
2889 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2891 struct thread *td = curthread;
2892 struct proc *p = td->td_proc;
2897 * note: vget is required for any operation that might mod the vnode
2898 * so VINACTIVE is properly cleared.
2900 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2904 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2911 kern_chown(struct nlookupdata *nd, int uid, int gid)
2916 if ((error = nlookup(nd)) != 0)
2918 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2920 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2921 error = setfown(vp, uid, gid);
2927 * chown(char *path, int uid, int gid)
2929 * Set ownership given a path name.
2932 sys_chown(struct chown_args *uap)
2934 struct nlookupdata nd;
2937 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2939 error = kern_chown(&nd, uap->uid, uap->gid);
2945 * lchown_args(char *path, int uid, int gid)
2947 * Set ownership given a path name, do not cross symlinks.
2950 sys_lchown(struct lchown_args *uap)
2952 struct nlookupdata nd;
2955 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2957 error = kern_chown(&nd, uap->uid, uap->gid);
2963 * fchown_args(int fd, int uid, int gid)
2965 * Set ownership given a file descriptor.
2969 sys_fchown(struct fchown_args *uap)
2971 struct thread *td = curthread;
2972 struct proc *p = td->td_proc;
2976 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2978 if (fp->f_nchandle.ncp)
2979 error = ncp_writechk(&fp->f_nchandle);
2981 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2987 * fchownat(int fd, char *path, int uid, int gid, int flags)
2989 * Set ownership of file pointed to by fd/path.
2992 sys_fchownat(struct fchownat_args *uap)
2994 struct nlookupdata nd;
2999 if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3001 flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3003 error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3004 UIO_USERSPACE, flags);
3006 error = kern_chown(&nd, uap->uid, uap->gid);
3007 nlookup_done_at(&nd, fp);
3013 getutimes(const struct timeval *tvp, struct timespec *tsp)
3015 struct timeval tv[2];
3019 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3022 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3023 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3029 setutimes(struct vnode *vp, struct vattr *vattr,
3030 const struct timespec *ts, int nullflag)
3032 struct thread *td = curthread;
3033 struct proc *p = td->td_proc;
3037 vattr->va_atime = ts[0];
3038 vattr->va_mtime = ts[1];
3040 vattr->va_vaflags |= VA_UTIMES_NULL;
3041 error = VOP_SETATTR(vp, vattr, p->p_ucred);
3047 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3049 struct timespec ts[2];
3054 if ((error = getutimes(tptr, ts)) != 0)
3058 * NOTE: utimes() succeeds for the owner even if the file
3059 * is not user-writable.
3061 nd->nl_flags |= NLC_OWN | NLC_WRITE;
3063 if ((error = nlookup(nd)) != 0)
3065 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3067 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3071 * note: vget is required for any operation that might mod the vnode
3072 * so VINACTIVE is properly cleared.
3074 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3075 error = vget(vp, LK_EXCLUSIVE);
3077 error = setutimes(vp, &vattr, ts, (tptr == NULL));
3086 * utimes_args(char *path, struct timeval *tptr)
3088 * Set the access and modification times of a file.
3091 sys_utimes(struct utimes_args *uap)
3093 struct timeval tv[2];
3094 struct nlookupdata nd;
3098 error = copyin(uap->tptr, tv, sizeof(tv));
3102 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3104 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3110 * lutimes_args(char *path, struct timeval *tptr)
3112 * Set the access and modification times of a file.
3115 sys_lutimes(struct lutimes_args *uap)
3117 struct timeval tv[2];
3118 struct nlookupdata nd;
3122 error = copyin(uap->tptr, tv, sizeof(tv));
3126 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3128 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3134 * Set utimes on a file descriptor. The creds used to open the
3135 * file are used to determine whether the operation is allowed
3139 kern_futimes(int fd, struct timeval *tptr)
3141 struct thread *td = curthread;
3142 struct proc *p = td->td_proc;
3143 struct timespec ts[2];
3149 error = getutimes(tptr, ts);
3152 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3154 if (fp->f_nchandle.ncp)
3155 error = ncp_writechk(&fp->f_nchandle);
3158 error = vget(vp, LK_EXCLUSIVE);
3160 error = VOP_GETATTR(vp, &vattr);
3162 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
3166 error = setutimes(vp, &vattr, ts,
3177 * futimes_args(int fd, struct timeval *tptr)
3179 * Set the access and modification times of a file.
3182 sys_futimes(struct futimes_args *uap)
3184 struct timeval tv[2];
3188 error = copyin(uap->tptr, tv, sizeof(tv));
3193 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3199 kern_truncate(struct nlookupdata *nd, off_t length)
3207 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3208 if ((error = nlookup(nd)) != 0)
3210 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3212 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3214 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3218 if (vp->v_type == VDIR) {
3220 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
3222 vattr.va_size = length;
3223 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3230 * truncate(char *path, int pad, off_t length)
3232 * Truncate a file given its path name.
3235 sys_truncate(struct truncate_args *uap)
3237 struct nlookupdata nd;
3240 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3242 error = kern_truncate(&nd, uap->length);
3248 kern_ftruncate(int fd, off_t length)
3250 struct thread *td = curthread;
3251 struct proc *p = td->td_proc;
3259 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3261 if (fp->f_nchandle.ncp) {
3262 error = ncp_writechk(&fp->f_nchandle);
3266 if ((fp->f_flag & FWRITE) == 0) {
3270 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3274 vp = (struct vnode *)fp->f_data;
3275 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3276 if (vp->v_type == VDIR) {
3278 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3280 vattr.va_size = length;
3281 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3290 * ftruncate_args(int fd, int pad, off_t length)
3292 * Truncate a file given a file descriptor.
3295 sys_ftruncate(struct ftruncate_args *uap)
3299 error = kern_ftruncate(uap->fd, uap->length);
3307 * Sync an open file.
3311 sys_fsync(struct fsync_args *uap)
3313 struct thread *td = curthread;
3314 struct proc *p = td->td_proc;
3320 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3322 vp = (struct vnode *)fp->f_data;
3323 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3324 if ((obj = vp->v_object) != NULL)
3325 vm_object_page_clean(obj, 0, 0, 0);
3326 error = VOP_FSYNC(vp, MNT_WAIT, VOP_FSYNC_SYSCALL);
3327 if (error == 0 && vp->v_mount)
3328 error = buf_fsync(vp);
3335 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3337 struct nchandle fnchd;
3338 struct nchandle tnchd;
3339 struct namecache *ncp;
3346 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
3347 if ((error = nlookup(fromnd)) != 0)
3349 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3351 fnchd.mount = fromnd->nl_nch.mount;
3355 * unlock the source nch so we can lookup the target nch without
3356 * deadlocking. The target may or may not exist so we do not check
3357 * for a target vp like kern_mkdir() and other creation functions do.
3359 * The source and target directories are ref'd and rechecked after
3360 * everything is relocked to determine if the source or target file
3363 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3364 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3365 cache_unlock(&fromnd->nl_nch);
3367 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
3368 if ((error = nlookup(tond)) != 0) {
3372 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3376 tnchd.mount = tond->nl_nch.mount;
3380 * If the source and target are the same there is nothing to do
3382 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3389 * Mount points cannot be renamed or overwritten
3391 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3400 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3401 * may have become invalid while it was unlocked, nc_vp and nc_mount
3404 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3405 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3406 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3407 cache_lock(&fromnd->nl_nch);
3408 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3410 cache_unlock(&tond->nl_nch);
3411 cache_lock(&fromnd->nl_nch);
3412 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3413 cache_lock(&tond->nl_nch);
3414 cache_resolve(&tond->nl_nch, tond->nl_cred);
3416 fromnd->nl_flags |= NLC_NCPISLOCKED;
3419 * make sure the parent directories linkages are the same
3421 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3422 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3429 * Both the source and target must be within the same filesystem and
3430 * in the same filesystem as their parent directories within the
3431 * namecache topology.
3433 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3436 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3437 mp != tond->nl_nch.mount) {
3444 * Make sure the mount point is writable
3446 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3453 * If the target exists and either the source or target is a directory,
3454 * then both must be directories.
3456 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3459 if (tond->nl_nch.ncp->nc_vp) {
3460 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3462 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3463 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3465 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3471 * You cannot rename a source into itself or a subdirectory of itself.
3472 * We check this by travsersing the target directory upwards looking
3473 * for a match against the source.
3476 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3477 if (fromnd->nl_nch.ncp == ncp) {
3488 * Even though the namespaces are different, they may still represent
3489 * hardlinks to the same file. The filesystem might have a hard time
3490 * with this so we issue a NREMOVE of the source instead of a NRENAME
3491 * when we detect the situation.
3494 fdvp = fromnd->nl_dvp;
3495 tdvp = tond->nl_dvp;
3496 if (fdvp == NULL || tdvp == NULL) {
3498 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3499 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3502 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3503 fdvp, tdvp, tond->nl_cred);
3510 * rename_args(char *from, char *to)
3512 * Rename files. Source and destination must either both be directories,
3513 * or both not be directories. If target is a directory, it must be empty.
3516 sys_rename(struct rename_args *uap)
3518 struct nlookupdata fromnd, tond;
3521 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3523 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3525 error = kern_rename(&fromnd, &tond);
3526 nlookup_done(&tond);
3528 nlookup_done(&fromnd);
3533 kern_mkdir(struct nlookupdata *nd, int mode)
3535 struct thread *td = curthread;
3536 struct proc *p = td->td_proc;
3542 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3543 if ((error = nlookup(nd)) != 0)
3546 if (nd->nl_nch.ncp->nc_vp)
3548 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3551 vattr.va_type = VDIR;
3552 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3555 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3562 * mkdir_args(char *path, int mode)
3564 * Make a directory file.
3568 sys_mkdir(struct mkdir_args *uap)
3570 struct nlookupdata nd;
3573 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3575 error = kern_mkdir(&nd, uap->mode);
3581 kern_rmdir(struct nlookupdata *nd)
3586 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3587 if ((error = nlookup(nd)) != 0)
3591 * Do not allow directories representing mount points to be
3592 * deleted, even if empty. Check write perms on mount point
3593 * in case the vnode is aliased (aka nullfs).
3595 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3597 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3599 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3604 * rmdir_args(char *path)
3606 * Remove a directory file.
3610 sys_rmdir(struct rmdir_args *uap)
3612 struct nlookupdata nd;
3615 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3617 error = kern_rmdir(&nd);
3623 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3624 enum uio_seg direction)
3626 struct thread *td = curthread;
3627 struct proc *p = td->td_proc;
3635 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3637 if ((fp->f_flag & FREAD) == 0) {
3641 vp = (struct vnode *)fp->f_data;
3643 if (vp->v_type != VDIR) {
3647 aiov.iov_base = buf;
3648 aiov.iov_len = count;
3649 auio.uio_iov = &aiov;
3650 auio.uio_iovcnt = 1;
3651 auio.uio_rw = UIO_READ;
3652 auio.uio_segflg = direction;
3654 auio.uio_resid = count;
3655 loff = auio.uio_offset = fp->f_offset;
3656 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3657 fp->f_offset = auio.uio_offset;
3660 if (count == auio.uio_resid) {
3661 if (union_dircheckp) {
3662 error = union_dircheckp(td, &vp, fp);
3669 if ((vp->v_flag & VROOT) &&
3670 (vp->v_mount->mnt_flag & MNT_UNION)) {
3671 struct vnode *tvp = vp;
3672 vp = vp->v_mount->mnt_vnodecovered;
3683 * WARNING! *basep may not be wide enough to accomodate the
3684 * seek offset. XXX should we hack this to return the upper 32 bits
3685 * for offsets greater then 4G?
3688 *basep = (long)loff;
3690 *res = count - auio.uio_resid;
3697 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3699 * Read a block of directory entries in a file system independent format.
3702 sys_getdirentries(struct getdirentries_args *uap)
3707 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3708 &uap->sysmsg_result, UIO_USERSPACE);
3710 if (error == 0 && uap->basep)
3711 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3716 * getdents_args(int fd, char *buf, size_t count)
3719 sys_getdents(struct getdents_args *uap)
3723 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3724 &uap->sysmsg_result, UIO_USERSPACE);
3730 * umask(int newmask)
3732 * Set the mode mask for creation of filesystem nodes.
3737 sys_umask(struct umask_args *uap)
3739 struct thread *td = curthread;
3740 struct proc *p = td->td_proc;
3741 struct filedesc *fdp;
3744 uap->sysmsg_result = fdp->fd_cmask;
3745 fdp->fd_cmask = uap->newmask & ALLPERMS;
3750 * revoke(char *path)
3752 * Void all references to file by ripping underlying filesystem
3757 sys_revoke(struct revoke_args *uap)
3759 struct nlookupdata nd;
3766 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3768 error = nlookup(&nd);
3770 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3771 cred = crhold(nd.nl_cred);
3775 error = VOP_GETATTR(vp, &vattr);
3776 if (error == 0 && cred->cr_uid != vattr.va_uid)
3777 error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
3778 if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
3780 error = vrevoke(vp, cred);
3781 } else if (error == 0) {
3782 error = vrevoke(vp, cred);
3792 * getfh_args(char *fname, fhandle_t *fhp)
3794 * Get (NFS) file handle
3796 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
3797 * mount. This allows nullfs mounts to be explicitly exported.
3799 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
3801 * nullfs mounts of subdirectories are not safe. That is, it will
3802 * work, but you do not really have protection against access to
3803 * the related parent directories.
3806 sys_getfh(struct getfh_args *uap)
3808 struct thread *td = curthread;
3809 struct nlookupdata nd;
3816 * Must be super user
3818 if ((error = priv_check(td, PRIV_ROOT)) != 0)
3822 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3824 error = nlookup(&nd);
3826 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3827 mp = nd.nl_nch.mount;
3830 bzero(&fh, sizeof(fh));
3831 fh.fh_fsid = mp->mnt_stat.f_fsid;
3832 error = VFS_VPTOFH(vp, &fh.fh_fid);
3835 error = copyout(&fh, uap->fhp, sizeof(fh));
3841 * fhopen_args(const struct fhandle *u_fhp, int flags)
3843 * syscall for the rpc.lockd to use to translate a NFS file handle into
3844 * an open descriptor.
3846 * warning: do not remove the priv_check() call or this becomes one giant
3850 sys_fhopen(struct fhopen_args *uap)
3852 struct thread *td = curthread;
3853 struct proc *p = td->td_proc;
3858 struct vattr *vap = &vat;
3860 int fmode, mode, error, type;
3866 * Must be super user
3868 error = priv_check(td, PRIV_ROOT);
3872 fmode = FFLAGS(uap->flags);
3873 /* why not allow a non-read/write open for our lockd? */
3874 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3876 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3879 /* find the mount point */
3880 mp = vfs_getvfs(&fhp.fh_fsid);
3883 /* now give me my vnode, it gets returned to me locked */
3884 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
3888 * from now on we have to make sure not
3889 * to forget about the vnode
3890 * any error that causes an abort must vput(vp)
3891 * just set error = err and 'goto bad;'.
3897 if (vp->v_type == VLNK) {
3901 if (vp->v_type == VSOCK) {
3906 if (fmode & (FWRITE | O_TRUNC)) {
3907 if (vp->v_type == VDIR) {
3911 error = vn_writechk(vp, NULL);
3919 error = VOP_ACCESS(vp, mode, p->p_ucred);
3923 if (fmode & O_TRUNC) {
3924 vn_unlock(vp); /* XXX */
3925 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3928 error = VOP_SETATTR(vp, vap, p->p_ucred);
3934 * VOP_OPEN needs the file pointer so it can potentially override
3937 * WARNING! no f_nchandle will be associated when fhopen()ing a
3940 if ((error = falloc(p, &nfp, &indx)) != 0)
3944 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3947 * setting f_ops this way prevents VOP_CLOSE from being
3948 * called or fdrop() releasing the vp from v_data. Since
3949 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3951 fp->f_ops = &badfileops;
3957 * The fp is given its own reference, we still have our ref and lock.
3959 * Assert that all regular files must be created with a VM object.
3961 if (vp->v_type == VREG && vp->v_object == NULL) {
3962 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3967 * The open was successful. Handle any locking requirements.
3969 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3970 lf.l_whence = SEEK_SET;
3973 if (fmode & O_EXLOCK)
3974 lf.l_type = F_WRLCK;
3976 lf.l_type = F_RDLCK;
3977 if (fmode & FNONBLOCK)
3982 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3984 * release our private reference.
3986 fsetfd(p, NULL, indx);
3991 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3992 fp->f_flag |= FHASLOCK;
3996 * Clean up. Associate the file pointer with the previously
3997 * reserved descriptor and return it.
4000 fsetfd(p, fp, indx);
4002 uap->sysmsg_result = indx;
4006 fsetfd(p, NULL, indx);
4014 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4017 sys_fhstat(struct fhstat_args *uap)
4019 struct thread *td = curthread;
4027 * Must be super user
4029 error = priv_check(td, PRIV_ROOT);
4033 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4037 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4039 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4041 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
4045 error = copyout(&sb, uap->sb, sizeof(sb));
4050 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4053 sys_fhstatfs(struct fhstatfs_args *uap)
4055 struct thread *td = curthread;
4056 struct proc *p = td->td_proc;
4061 char *fullpath, *freepath;
4066 * Must be super user
4068 if ((error = priv_check(td, PRIV_ROOT)))
4071 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4074 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4077 if (p != NULL && !chroot_visible_mnt(mp, p))
4080 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4085 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
4088 error = mount_path(p, mp, &fullpath, &freepath);
4091 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4092 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4093 kfree(freepath, M_TEMP);
4095 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4096 if (priv_check(td, PRIV_ROOT)) {
4097 bcopy(sp, &sb, sizeof(sb));
4098 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4101 return (copyout(sp, uap->buf, sizeof(*sp)));
4105 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4108 sys_fhstatvfs(struct fhstatvfs_args *uap)
4110 struct thread *td = curthread;
4111 struct proc *p = td->td_proc;
4119 * Must be super user
4121 if ((error = priv_check(td, PRIV_ROOT)))
4124 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4127 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4130 if (p != NULL && !chroot_visible_mnt(mp, p))
4133 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
4136 sp = &mp->mnt_vstat;
4138 if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
4142 if (mp->mnt_flag & MNT_RDONLY)
4143 sp->f_flag |= ST_RDONLY;
4144 if (mp->mnt_flag & MNT_NOSUID)
4145 sp->f_flag |= ST_NOSUID;
4147 return (copyout(sp, uap->buf, sizeof(*sp)));
4152 * Syscall to push extended attribute configuration information into the
4153 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4154 * a command (int cmd), and attribute name and misc data. For now, the
4155 * attribute name is left in userspace for consumption by the VFS_op.
4156 * It will probably be changed to be copied into sysspace by the
4157 * syscall in the future, once issues with various consumers of the
4158 * attribute code have raised their hands.
4160 * Currently this is used only by UFS Extended Attributes.
4163 sys_extattrctl(struct extattrctl_args *uap)
4165 struct nlookupdata nd;
4171 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4173 error = nlookup(&nd);
4175 mp = nd.nl_nch.mount;
4176 error = VFS_EXTATTRCTL(mp, uap->cmd,
4177 uap->attrname, uap->arg,
4185 * Syscall to set a named extended attribute on a file or directory.
4186 * Accepts attribute name, and a uio structure pointing to the data to set.
4187 * The uio is consumed in the style of writev(). The real work happens
4188 * in VOP_SETEXTATTR().
4191 sys_extattr_set_file(struct extattr_set_file_args *uap)
4193 char attrname[EXTATTR_MAXNAMELEN];
4194 struct iovec aiov[UIO_SMALLIOV];
4195 struct iovec *needfree;
4196 struct nlookupdata nd;
4205 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4210 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4212 error = nlookup(&nd);
4214 error = ncp_writechk(&nd.nl_nch);
4216 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4223 iovlen = uap->iovcnt * sizeof(struct iovec);
4224 if (uap->iovcnt > UIO_SMALLIOV) {
4225 if (uap->iovcnt > UIO_MAXIOV) {
4229 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4235 auio.uio_iovcnt = uap->iovcnt;
4236 auio.uio_rw = UIO_WRITE;
4237 auio.uio_segflg = UIO_USERSPACE;
4238 auio.uio_td = nd.nl_td;
4239 auio.uio_offset = 0;
4240 if ((error = copyin(uap->iovp, iov, iovlen)))
4243 for (i = 0; i < uap->iovcnt; i++) {
4244 if (iov->iov_len > LONG_MAX - auio.uio_resid) {
4248 auio.uio_resid += iov->iov_len;
4251 cnt = auio.uio_resid;
4252 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4253 cnt -= auio.uio_resid;
4254 uap->sysmsg_result = cnt;
4259 FREE(needfree, M_IOV);
4264 * Syscall to get a named extended attribute on a file or directory.
4265 * Accepts attribute name, and a uio structure pointing to a buffer for the
4266 * data. The uio is consumed in the style of readv(). The real work
4267 * happens in VOP_GETEXTATTR();
4270 sys_extattr_get_file(struct extattr_get_file_args *uap)
4272 char attrname[EXTATTR_MAXNAMELEN];
4273 struct iovec aiov[UIO_SMALLIOV];
4274 struct iovec *needfree;
4275 struct nlookupdata nd;
4284 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4289 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4291 error = nlookup(&nd);
4293 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4299 iovlen = uap->iovcnt * sizeof (struct iovec);
4301 if (uap->iovcnt > UIO_SMALLIOV) {
4302 if (uap->iovcnt > UIO_MAXIOV) {
4306 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4312 auio.uio_iovcnt = uap->iovcnt;
4313 auio.uio_rw = UIO_READ;
4314 auio.uio_segflg = UIO_USERSPACE;
4315 auio.uio_td = nd.nl_td;
4316 auio.uio_offset = 0;
4317 if ((error = copyin(uap->iovp, iov, iovlen)))
4320 for (i = 0; i < uap->iovcnt; i++) {
4321 if (iov->iov_len > LONG_MAX - auio.uio_resid) {
4325 auio.uio_resid += iov->iov_len;
4328 cnt = auio.uio_resid;
4329 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4330 cnt -= auio.uio_resid;
4331 uap->sysmsg_result = cnt;
4336 FREE(needfree, M_IOV);
4341 * Syscall to delete a named extended attribute from a file or directory.
4342 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4345 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4347 char attrname[EXTATTR_MAXNAMELEN];
4348 struct nlookupdata nd;
4352 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4357 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4359 error = nlookup(&nd);
4361 error = ncp_writechk(&nd.nl_nch);
4363 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4369 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4376 * Determine if the mount is visible to the process.
4379 chroot_visible_mnt(struct mount *mp, struct proc *p)
4381 struct nchandle nch;
4384 * Traverse from the mount point upwards. If we hit the process
4385 * root then the mount point is visible to the process.
4387 nch = mp->mnt_ncmountpt;
4389 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4390 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4393 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4394 nch = nch.mount->mnt_ncmounton;
4396 nch.ncp = nch.ncp->nc_parent;
4401 * If the mount point is not visible to the process, but the
4402 * process root is in a subdirectory of the mount, return
4405 if (p->p_fd->fd_nrdir.mount == mp)