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>
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>
76 #include <vm/vm_object.h>
77 #include <vm/vm_page.h>
79 #include <machine/limits.h>
80 #include <machine/stdarg.h>
82 #include <vfs/union/union.h>
84 static void mount_warning(struct mount *mp, const char *ctl, ...);
85 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
86 static int checkvp_chdir (struct vnode *vn, struct thread *td);
87 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
88 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
89 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
90 static int getutimes (const struct timeval *, struct timespec *);
91 static int setfown (struct vnode *, uid_t, gid_t);
92 static int setfmode (struct vnode *, int);
93 static int setfflags (struct vnode *, int);
94 static int setutimes (struct vnode *, const struct timespec *, int);
95 static int usermount = 0; /* if 1, non-root can mount fs. */
97 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
99 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
102 * Virtual File System System Calls
106 * Mount a file system.
109 * mount_args(char *type, char *path, int flags, caddr_t data)
113 sys_mount(struct mount_args *uap)
115 struct thread *td = curthread;
116 struct proc *p = td->td_proc;
120 struct vfsconf *vfsp;
121 int error, flag = 0, flag2 = 0;
124 struct nlookupdata nd;
125 char fstypename[MFSNAMELEN];
126 struct ucred *cred = p->p_ucred;
129 if (cred->cr_prison != NULL)
131 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
134 * Do not allow NFS export by non-root users.
136 if (uap->flags & MNT_EXPORTED) {
137 error = priv_check(td, PRIV_ROOT);
142 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
144 if (priv_check(td, PRIV_ROOT))
145 uap->flags |= MNT_NOSUID | MNT_NODEV;
148 * Lookup the requested path and extract the nch and vnode.
150 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
152 if ((error = nlookup(&nd)) == 0) {
153 if (nd.nl_nch.ncp->nc_vp == NULL)
163 * Extract the locked+refd ncp and cleanup the nd structure
166 cache_zero(&nd.nl_nch);
169 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
176 * now we have the locked ref'd nch and unreferenced vnode.
179 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
186 * Now we have an unlocked ref'd nch and a locked ref'd vp
188 if (uap->flags & MNT_UPDATE) {
189 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
196 flag2 = mp->mnt_kern_flag;
198 * We only allow the filesystem to be reloaded if it
199 * is currently mounted read-only.
201 if ((uap->flags & MNT_RELOAD) &&
202 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
205 return (EOPNOTSUPP); /* Needs translation */
208 * Only root, or the user that did the original mount is
209 * permitted to update it.
211 if (mp->mnt_stat.f_owner != cred->cr_uid &&
212 (error = priv_check(td, PRIV_ROOT))) {
217 if (vfs_busy(mp, LK_NOWAIT)) {
222 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
228 vp->v_flag |= VMOUNT;
230 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
235 * If the user is not root, ensure that they own the directory
236 * onto which we are attempting to mount.
238 if ((error = VOP_GETATTR(vp, &va)) ||
239 (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) {
244 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
249 if (vp->v_type != VDIR) {
254 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
259 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
264 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
265 if (!strcmp(vfsp->vfc_name, fstypename))
271 /* Only load modules for root (very important!) */
272 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
277 error = linker_load_file(fstypename, &lf);
278 if (error || lf == NULL) {
286 /* lookup again, see if the VFS was loaded */
287 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
288 if (!strcmp(vfsp->vfc_name, fstypename))
293 linker_file_unload(lf);
299 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
304 vp->v_flag |= VMOUNT;
307 * Allocate and initialize the filesystem.
309 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
310 TAILQ_INIT(&mp->mnt_nvnodelist);
311 TAILQ_INIT(&mp->mnt_reservedvnlist);
312 TAILQ_INIT(&mp->mnt_jlist);
313 mp->mnt_nvnodelistsize = 0;
314 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
315 vfs_busy(mp, LK_NOWAIT);
316 mp->mnt_op = vfsp->vfc_vfsops;
318 vfsp->vfc_refcount++;
319 mp->mnt_stat.f_type = vfsp->vfc_typenum;
320 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
321 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
322 mp->mnt_stat.f_owner = cred->cr_uid;
323 mp->mnt_iosize_max = DFLTPHYS;
327 * Set the mount level flags.
329 if (uap->flags & MNT_RDONLY)
330 mp->mnt_flag |= MNT_RDONLY;
331 else if (mp->mnt_flag & MNT_RDONLY)
332 mp->mnt_kern_flag |= MNTK_WANTRDWR;
333 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
334 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
335 MNT_NOSYMFOLLOW | MNT_IGNORE |
336 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
337 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
338 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
339 MNT_NOSYMFOLLOW | MNT_IGNORE |
340 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
342 * Mount the filesystem.
343 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
346 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
347 if (mp->mnt_flag & MNT_UPDATE) {
348 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
349 mp->mnt_flag &= ~MNT_RDONLY;
350 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
351 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
354 mp->mnt_kern_flag = flag2;
357 vp->v_flag &= ~VMOUNT;
362 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
364 * Put the new filesystem on the mount list after root. The mount
365 * point gets its own mnt_ncmountpt (unless the VFS already set one
366 * up) which represents the root of the mount. The lookup code
367 * detects the mount point going forward and checks the root of
368 * the mount going backwards.
370 * It is not necessary to invalidate or purge the vnode underneath
371 * because elements under the mount will be given their own glue
375 if (mp->mnt_ncmountpt.ncp == NULL) {
377 * allocate, then unlock, but leave the ref intact
379 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
380 cache_unlock(&mp->mnt_ncmountpt);
382 mp->mnt_ncmounton = nch; /* inherits ref */
383 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
385 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
386 vp->v_flag &= ~VMOUNT;
387 mountlist_insert(mp, MNTINS_LAST);
388 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
390 error = vfs_allocate_syncvnode(mp);
392 error = VFS_START(mp, 0);
395 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
396 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
397 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
398 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
399 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
400 vp->v_flag &= ~VMOUNT;
401 mp->mnt_vfc->vfc_refcount--;
411 * Scan all active processes to see if any of them have a current
412 * or root directory onto which the new filesystem has just been
413 * mounted. If so, replace them with the new mount point.
415 * The passed ncp is ref'd and locked (from the mount code) and
416 * must be associated with the vnode representing the root of the
419 struct checkdirs_info {
420 struct nchandle old_nch;
421 struct nchandle new_nch;
422 struct vnode *old_vp;
423 struct vnode *new_vp;
426 static int checkdirs_callback(struct proc *p, void *data);
429 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
431 struct checkdirs_info info;
437 * If the old mount point's vnode has a usecount of 1, it is not
438 * being held as a descriptor anywhere.
440 olddp = old_nch->ncp->nc_vp;
441 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
445 * Force the root vnode of the new mount point to be resolved
446 * so we can update any matching processes.
449 if (VFS_ROOT(mp, &newdp))
450 panic("mount: lost mount");
451 cache_setunresolved(new_nch);
452 cache_setvp(new_nch, newdp);
455 * Special handling of the root node
457 if (rootvnode == olddp) {
459 vfs_cache_setroot(newdp, cache_hold(new_nch));
463 * Pass newdp separately so the callback does not have to access
464 * it via new_nch->ncp->nc_vp.
466 info.old_nch = *old_nch;
467 info.new_nch = *new_nch;
469 allproc_scan(checkdirs_callback, &info);
474 * NOTE: callback is not MP safe because the scanned process's filedesc
475 * structure can be ripped out from under us, amoung other things.
478 checkdirs_callback(struct proc *p, void *data)
480 struct checkdirs_info *info = data;
481 struct filedesc *fdp;
482 struct nchandle ncdrop1;
483 struct nchandle ncdrop2;
484 struct vnode *vprele1;
485 struct vnode *vprele2;
487 if ((fdp = p->p_fd) != NULL) {
488 cache_zero(&ncdrop1);
489 cache_zero(&ncdrop2);
494 * MPUNSAFE - XXX fdp can be pulled out from under a
497 * A shared filedesc is ok, we don't have to copy it
498 * because we are making this change globally.
500 spin_lock_wr(&fdp->fd_spin);
501 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
502 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
503 vprele1 = fdp->fd_cdir;
505 fdp->fd_cdir = info->new_vp;
506 ncdrop1 = fdp->fd_ncdir;
507 cache_copy(&info->new_nch, &fdp->fd_ncdir);
509 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
510 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
511 vprele2 = fdp->fd_rdir;
513 fdp->fd_rdir = info->new_vp;
514 ncdrop2 = fdp->fd_nrdir;
515 cache_copy(&info->new_nch, &fdp->fd_nrdir);
517 spin_unlock_wr(&fdp->fd_spin);
519 cache_drop(&ncdrop1);
521 cache_drop(&ncdrop2);
531 * Unmount a file system.
533 * Note: unmount takes a path to the vnode mounted on as argument,
534 * not special file (as before).
537 * umount_args(char *path, int flags)
541 sys_unmount(struct unmount_args *uap)
543 struct thread *td = curthread;
544 struct proc *p = td->td_proc;
545 struct mount *mp = NULL;
547 struct nlookupdata nd;
550 if (p->p_ucred->cr_prison != NULL)
552 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
555 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
557 error = nlookup(&nd);
561 mp = nd.nl_nch.mount;
564 * Only root, or the user that did the original mount is
565 * permitted to unmount this filesystem.
567 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
568 (error = priv_check(td, PRIV_ROOT)))
572 * Don't allow unmounting the root file system.
574 if (mp->mnt_flag & MNT_ROOTFS) {
580 * Must be the root of the filesystem
582 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
591 return (dounmount(mp, uap->flags));
595 * Do the actual file system unmount.
598 dounmount_interlock(struct mount *mp)
600 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
602 mp->mnt_kern_flag |= MNTK_UNMOUNT;
607 dounmount(struct mount *mp, int flags)
609 struct namecache *ncp;
618 * Exclusive access for unmounting purposes
620 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
624 * Allow filesystems to detect that a forced unmount is in progress.
626 if (flags & MNT_FORCE)
627 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
628 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
629 error = lockmgr(&mp->mnt_lock, lflags);
631 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
632 if (mp->mnt_kern_flag & MNTK_MWAIT)
637 if (mp->mnt_flag & MNT_EXPUBLIC)
638 vfs_setpublicfs(NULL, NULL, NULL);
640 vfs_msync(mp, MNT_WAIT);
641 async_flag = mp->mnt_flag & MNT_ASYNC;
642 mp->mnt_flag &=~ MNT_ASYNC;
645 * If this filesystem isn't aliasing other filesystems,
646 * try to invalidate any remaining namecache entries and
647 * check the count afterwords.
649 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
650 cache_lock(&mp->mnt_ncmountpt);
651 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
652 cache_unlock(&mp->mnt_ncmountpt);
654 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
655 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
657 if ((flags & MNT_FORCE) == 0) {
659 mount_warning(mp, "Cannot unmount: "
665 mount_warning(mp, "Forced unmount: "
676 * nchandle records ref the mount structure. Expect a count of 1
677 * (our mount->mnt_ncmountpt).
679 if (mp->mnt_refs != 1) {
680 if ((flags & MNT_FORCE) == 0) {
681 mount_warning(mp, "Cannot unmount: "
682 "%d process references still "
683 "present", mp->mnt_refs);
686 mount_warning(mp, "Forced unmount: "
687 "%d process references still "
688 "present", mp->mnt_refs);
694 * Decomission our special mnt_syncer vnode. This also stops
695 * the vnlru code. If we are unable to unmount we recommission
699 if ((vp = mp->mnt_syncer) != NULL) {
700 mp->mnt_syncer = NULL;
703 if (((mp->mnt_flag & MNT_RDONLY) ||
704 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
705 (flags & MNT_FORCE)) {
706 error = VFS_UNMOUNT(mp, flags);
710 if (mp->mnt_syncer == NULL)
711 vfs_allocate_syncvnode(mp);
712 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
713 mp->mnt_flag |= async_flag;
714 lockmgr(&mp->mnt_lock, LK_RELEASE);
715 if (mp->mnt_kern_flag & MNTK_MWAIT)
720 * Clean up any journals still associated with the mount after
721 * filesystem activity has ceased.
723 journal_remove_all_journals(mp,
724 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
726 mountlist_remove(mp);
729 * Remove any installed vnode ops here so the individual VFSs don't
732 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
733 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
734 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
735 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
736 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
738 if (mp->mnt_ncmountpt.ncp != NULL) {
739 nch = mp->mnt_ncmountpt;
740 cache_zero(&mp->mnt_ncmountpt);
741 cache_clrmountpt(&nch);
744 if (mp->mnt_ncmounton.ncp != NULL) {
745 nch = mp->mnt_ncmounton;
746 cache_zero(&mp->mnt_ncmounton);
747 cache_clrmountpt(&nch);
751 mp->mnt_vfc->vfc_refcount--;
752 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
753 panic("unmount: dangling vnode");
754 lockmgr(&mp->mnt_lock, LK_RELEASE);
755 if (mp->mnt_kern_flag & MNTK_MWAIT)
764 mount_warning(struct mount *mp, const char *ctl, ...)
771 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
772 kprintf("unmount(%s): ", ptr);
777 kprintf("unmount(%p", mp);
778 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
779 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
788 * Shim cache_fullpath() to handle the case where a process is chrooted into
789 * a subdirectory of a mount. In this case if the root mount matches the
790 * process root directory's mount we have to specify the process's root
791 * directory instead of the mount point, because the mount point might
792 * be above the root directory.
796 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
798 struct nchandle *nch;
800 if (p && p->p_fd->fd_nrdir.mount == mp)
801 nch = &p->p_fd->fd_nrdir;
803 nch = &mp->mnt_ncmountpt;
804 return(cache_fullpath(p, nch, rb, fb));
808 * Sync each mounted filesystem.
812 static int syncprt = 0;
813 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
816 static int sync_callback(struct mount *mp, void *data);
820 sys_sync(struct sync_args *uap)
822 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
825 * print out buffer pool stat information on each sync() call.
835 sync_callback(struct mount *mp, void *data __unused)
839 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
840 asyncflag = mp->mnt_flag & MNT_ASYNC;
841 mp->mnt_flag &= ~MNT_ASYNC;
842 vfs_msync(mp, MNT_NOWAIT);
843 VFS_SYNC(mp, MNT_NOWAIT);
844 mp->mnt_flag |= asyncflag;
849 /* XXX PRISON: could be per prison flag */
850 static int prison_quotas;
852 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
856 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
858 * Change filesystem quotas.
862 sys_quotactl(struct quotactl_args *uap)
864 struct nlookupdata nd;
872 if (p->p_ucred->cr_prison && !prison_quotas)
875 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
877 error = nlookup(&nd);
879 mp = nd.nl_nch.mount;
880 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
881 uap->arg, nd.nl_cred);
888 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
889 * void *buf, int buflen)
891 * This function operates on a mount point and executes the specified
892 * operation using the specified control data, and possibly returns data.
894 * The actual number of bytes stored in the result buffer is returned, 0
895 * if none, otherwise an error is returned.
899 sys_mountctl(struct mountctl_args *uap)
901 struct thread *td = curthread;
902 struct proc *p = td->td_proc;
910 * Sanity and permissions checks. We must be root.
913 if (p->p_ucred->cr_prison != NULL)
915 if ((error = priv_check(td, PRIV_ROOT)) != 0)
919 * Argument length checks
921 if (uap->ctllen < 0 || uap->ctllen > 1024)
923 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
925 if (uap->path == NULL)
929 * Allocate the necessary buffers and copyin data
931 path = objcache_get(namei_oc, M_WAITOK);
932 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
937 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
938 error = copyin(uap->ctl, ctl, uap->ctllen);
943 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
946 * Validate the descriptor
949 fp = holdfp(p->p_fd, uap->fd, -1);
959 * Execute the internal kernel function and clean up.
961 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
964 if (error == 0 && uap->sysmsg_result > 0)
965 error = copyout(buf, uap->buf, uap->sysmsg_result);
968 objcache_put(namei_oc, path);
977 * Execute a mount control operation by resolving the path to a mount point
978 * and calling vop_mountctl().
980 * Use the mount point from the nch instead of the vnode so nullfs mounts
981 * can properly spike the VOP.
984 kern_mountctl(const char *path, int op, struct file *fp,
985 const void *ctl, int ctllen,
986 void *buf, int buflen, int *res)
990 struct nlookupdata nd;
995 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
997 error = nlookup(&nd);
999 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1000 mp = nd.nl_nch.mount;
1006 * Must be the root of the filesystem
1008 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1012 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1019 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1021 struct thread *td = curthread;
1022 struct proc *p = td->td_proc;
1025 char *fullpath, *freepath;
1028 if ((error = nlookup(nd)) != 0)
1030 mp = nd->nl_nch.mount;
1032 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1035 error = mount_path(p, mp, &fullpath, &freepath);
1038 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1039 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1040 kfree(freepath, M_TEMP);
1042 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1043 bcopy(sp, buf, sizeof(*buf));
1044 /* Only root should have access to the fsid's. */
1045 if (priv_check(td, PRIV_ROOT))
1046 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1051 * statfs_args(char *path, struct statfs *buf)
1053 * Get filesystem statistics.
1056 sys_statfs(struct statfs_args *uap)
1058 struct nlookupdata nd;
1062 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1064 error = kern_statfs(&nd, &buf);
1067 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1072 kern_fstatfs(int fd, struct statfs *buf)
1074 struct thread *td = curthread;
1075 struct proc *p = td->td_proc;
1079 char *fullpath, *freepath;
1083 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1085 mp = ((struct vnode *)fp->f_data)->v_mount;
1090 if (fp->f_cred == NULL) {
1095 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1098 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1100 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1101 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1102 kfree(freepath, M_TEMP);
1104 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1105 bcopy(sp, buf, sizeof(*buf));
1107 /* Only root should have access to the fsid's. */
1108 if (priv_check(td, PRIV_ROOT))
1109 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1117 * fstatfs_args(int fd, struct statfs *buf)
1119 * Get filesystem statistics.
1122 sys_fstatfs(struct fstatfs_args *uap)
1127 error = kern_fstatfs(uap->fd, &buf);
1130 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1135 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1141 if ((error = nlookup(nd)) != 0)
1143 mp = nd->nl_nch.mount;
1144 sp = &mp->mnt_vstat;
1145 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1149 if (mp->mnt_flag & MNT_RDONLY)
1150 sp->f_flag |= ST_RDONLY;
1151 if (mp->mnt_flag & MNT_NOSUID)
1152 sp->f_flag |= ST_NOSUID;
1153 bcopy(sp, buf, sizeof(*buf));
1158 * statfs_args(char *path, struct statfs *buf)
1160 * Get filesystem statistics.
1163 sys_statvfs(struct statvfs_args *uap)
1165 struct nlookupdata nd;
1169 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1171 error = kern_statvfs(&nd, &buf);
1174 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1179 kern_fstatvfs(int fd, struct statvfs *buf)
1181 struct thread *td = curthread;
1182 struct proc *p = td->td_proc;
1189 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1191 mp = ((struct vnode *)fp->f_data)->v_mount;
1196 if (fp->f_cred == NULL) {
1200 sp = &mp->mnt_vstat;
1201 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1205 if (mp->mnt_flag & MNT_RDONLY)
1206 sp->f_flag |= ST_RDONLY;
1207 if (mp->mnt_flag & MNT_NOSUID)
1208 sp->f_flag |= ST_NOSUID;
1210 bcopy(sp, buf, sizeof(*buf));
1218 * fstatfs_args(int fd, struct statfs *buf)
1220 * Get filesystem statistics.
1223 sys_fstatvfs(struct fstatvfs_args *uap)
1228 error = kern_fstatvfs(uap->fd, &buf);
1231 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1236 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1238 * Get statistics on all filesystems.
1241 struct getfsstat_info {
1242 struct statfs *sfsp;
1250 static int getfsstat_callback(struct mount *, void *);
1254 sys_getfsstat(struct getfsstat_args *uap)
1256 struct thread *td = curthread;
1257 struct proc *p = td->td_proc;
1258 struct getfsstat_info info;
1260 bzero(&info, sizeof(info));
1262 info.maxcount = uap->bufsize / sizeof(struct statfs);
1263 info.sfsp = uap->buf;
1265 info.flags = uap->flags;
1268 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1269 if (info.sfsp && info.count > info.maxcount)
1270 uap->sysmsg_result = info.maxcount;
1272 uap->sysmsg_result = info.count;
1273 return (info.error);
1277 getfsstat_callback(struct mount *mp, void *data)
1279 struct getfsstat_info *info = data;
1285 if (info->sfsp && info->count < info->maxcount) {
1286 if (info->p && !chroot_visible_mnt(mp, info->p))
1291 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1292 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1293 * overrides MNT_WAIT.
1295 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1296 (info->flags & MNT_WAIT)) &&
1297 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1300 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1302 error = mount_path(info->p, mp, &fullpath, &freepath);
1304 info->error = error;
1307 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1308 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1309 kfree(freepath, M_TEMP);
1311 error = copyout(sp, info->sfsp, sizeof(*sp));
1313 info->error = error;
1323 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1324 long bufsize, int flags)
1326 * Get statistics on all filesystems.
1329 struct getvfsstat_info {
1330 struct statfs *sfsp;
1331 struct statvfs *vsfsp;
1339 static int getvfsstat_callback(struct mount *, void *);
1343 sys_getvfsstat(struct getvfsstat_args *uap)
1345 struct thread *td = curthread;
1346 struct proc *p = td->td_proc;
1347 struct getvfsstat_info info;
1349 bzero(&info, sizeof(info));
1351 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1352 info.sfsp = uap->buf;
1353 info.vsfsp = uap->vbuf;
1355 info.flags = uap->flags;
1358 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1359 if (info.vsfsp && info.count > info.maxcount)
1360 uap->sysmsg_result = info.maxcount;
1362 uap->sysmsg_result = info.count;
1363 return (info.error);
1367 getvfsstat_callback(struct mount *mp, void *data)
1369 struct getvfsstat_info *info = data;
1371 struct statvfs *vsp;
1376 if (info->vsfsp && info->count < info->maxcount) {
1377 if (info->p && !chroot_visible_mnt(mp, info->p))
1380 vsp = &mp->mnt_vstat;
1383 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1384 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1385 * overrides MNT_WAIT.
1387 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1388 (info->flags & MNT_WAIT)) &&
1389 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1392 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1394 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1395 (info->flags & MNT_WAIT)) &&
1396 (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1400 if (mp->mnt_flag & MNT_RDONLY)
1401 vsp->f_flag |= ST_RDONLY;
1402 if (mp->mnt_flag & MNT_NOSUID)
1403 vsp->f_flag |= ST_NOSUID;
1405 error = mount_path(info->p, mp, &fullpath, &freepath);
1407 info->error = error;
1410 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1411 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1412 kfree(freepath, M_TEMP);
1414 error = copyout(sp, info->sfsp, sizeof(*sp));
1416 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1418 info->error = error;
1430 * fchdir_args(int fd)
1432 * Change current working directory to a given file descriptor.
1436 sys_fchdir(struct fchdir_args *uap)
1438 struct thread *td = curthread;
1439 struct proc *p = td->td_proc;
1440 struct filedesc *fdp = p->p_fd;
1441 struct vnode *vp, *ovp;
1444 struct nchandle nch, onch, tnch;
1447 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1449 vp = (struct vnode *)fp->f_data;
1451 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1452 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1455 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1461 cache_copy(&fp->f_nchandle, &nch);
1464 * If the ncp has become a mount point, traverse through
1468 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1469 (mp = cache_findmount(&nch)) != NULL
1471 error = nlookup_mp(mp, &tnch);
1473 cache_unlock(&tnch); /* leave ref intact */
1475 vp = tnch.ncp->nc_vp;
1476 error = vget(vp, LK_SHARED);
1477 KKASSERT(error == 0);
1484 onch = fdp->fd_ncdir;
1485 vn_unlock(vp); /* leave ref intact */
1487 fdp->fd_ncdir = nch;
1499 kern_chdir(struct nlookupdata *nd)
1501 struct thread *td = curthread;
1502 struct proc *p = td->td_proc;
1503 struct filedesc *fdp = p->p_fd;
1504 struct vnode *vp, *ovp;
1505 struct nchandle onch;
1508 if ((error = nlookup(nd)) != 0)
1510 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1512 if ((error = vget(vp, LK_SHARED)) != 0)
1515 error = checkvp_chdir(vp, td);
1519 onch = fdp->fd_ncdir;
1520 cache_unlock(&nd->nl_nch); /* leave reference intact */
1521 fdp->fd_ncdir = nd->nl_nch;
1525 cache_zero(&nd->nl_nch);
1533 * chdir_args(char *path)
1535 * Change current working directory (``.'').
1538 sys_chdir(struct chdir_args *uap)
1540 struct nlookupdata nd;
1543 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1545 error = kern_chdir(&nd);
1551 * Helper function for raised chroot(2) security function: Refuse if
1552 * any filedescriptors are open directories.
1555 chroot_refuse_vdir_fds(struct filedesc *fdp)
1562 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1563 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1565 vp = (struct vnode *)fp->f_data;
1566 if (vp->v_type != VDIR) {
1577 * This sysctl determines if we will allow a process to chroot(2) if it
1578 * has a directory open:
1579 * 0: disallowed for all processes.
1580 * 1: allowed for processes that were not already chroot(2)'ed.
1581 * 2: allowed for all processes.
1584 static int chroot_allow_open_directories = 1;
1586 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1587 &chroot_allow_open_directories, 0, "");
1590 * chroot to the specified namecache entry. We obtain the vp from the
1591 * namecache data. The passed ncp must be locked and referenced and will
1592 * remain locked and referenced on return.
1595 kern_chroot(struct nchandle *nch)
1597 struct thread *td = curthread;
1598 struct proc *p = td->td_proc;
1599 struct filedesc *fdp = p->p_fd;
1604 * Only root can chroot
1606 if ((error = priv_check_cred(p->p_ucred, PRIV_ROOT, PRISON_ROOT)) != 0)
1610 * Disallow open directory descriptors (fchdir() breakouts).
1612 if (chroot_allow_open_directories == 0 ||
1613 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1614 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1617 if ((vp = nch->ncp->nc_vp) == NULL)
1620 if ((error = vget(vp, LK_SHARED)) != 0)
1624 * Check the validity of vp as a directory to change to and
1625 * associate it with rdir/jdir.
1627 error = checkvp_chdir(vp, td);
1628 vn_unlock(vp); /* leave reference intact */
1630 vrele(fdp->fd_rdir);
1631 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1632 cache_drop(&fdp->fd_nrdir);
1633 cache_copy(nch, &fdp->fd_nrdir);
1634 if (fdp->fd_jdir == NULL) {
1637 cache_copy(nch, &fdp->fd_njdir);
1646 * chroot_args(char *path)
1648 * Change notion of root (``/'') directory.
1652 sys_chroot(struct chroot_args *uap)
1654 struct thread *td = curthread;
1655 struct nlookupdata nd;
1658 KKASSERT(td->td_proc);
1659 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1664 error = nlookup(&nd);
1666 error = kern_chroot(&nd.nl_nch);
1672 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1673 * determine whether it is legal to chdir to the vnode. The vnode's state
1674 * is not changed by this call.
1677 checkvp_chdir(struct vnode *vp, struct thread *td)
1681 if (vp->v_type != VDIR)
1684 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1689 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1691 struct thread *td = curthread;
1692 struct proc *p = td->td_proc;
1693 struct lwp *lp = td->td_lwp;
1694 struct filedesc *fdp = p->p_fd;
1699 int type, indx, error;
1702 if ((oflags & O_ACCMODE) == O_ACCMODE)
1704 flags = FFLAGS(oflags);
1705 error = falloc(p, &nfp, NULL);
1709 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1712 * XXX p_dupfd is a real mess. It allows a device to return a
1713 * file descriptor to be duplicated rather then doing the open
1719 * Call vn_open() to do the lookup and assign the vnode to the
1720 * file pointer. vn_open() does not change the ref count on fp
1721 * and the vnode, on success, will be inherited by the file pointer
1724 nd->nl_flags |= NLC_LOCKVP;
1725 error = vn_open(nd, fp, flags, cmode);
1729 * handle special fdopen() case. bleh. dupfdopen() is
1730 * responsible for dropping the old contents of ofiles[indx]
1733 * Note that fsetfd() will add a ref to fp which represents
1734 * the fd_files[] assignment. We must still drop our
1737 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1738 if (fdalloc(p, 0, &indx) == 0) {
1739 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1742 fdrop(fp); /* our ref */
1745 fsetfd(p, NULL, indx);
1748 fdrop(fp); /* our ref */
1749 if (error == ERESTART)
1755 * ref the vnode for ourselves so it can't be ripped out from under
1756 * is. XXX need an ND flag to request that the vnode be returned
1759 * Reserve a file descriptor but do not assign it until the open
1762 vp = (struct vnode *)fp->f_data;
1764 if ((error = fdalloc(p, 0, &indx)) != 0) {
1771 * If no error occurs the vp will have been assigned to the file
1776 if (flags & (O_EXLOCK | O_SHLOCK)) {
1777 lf.l_whence = SEEK_SET;
1780 if (flags & O_EXLOCK)
1781 lf.l_type = F_WRLCK;
1783 lf.l_type = F_RDLCK;
1784 if (flags & FNONBLOCK)
1789 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1791 * lock request failed. Clean up the reserved
1795 fsetfd(p, NULL, indx);
1799 fp->f_flag |= FHASLOCK;
1803 * Assert that all regular file vnodes were created with a object.
1805 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1806 ("open: regular file has no backing object after vn_open"));
1812 * release our private reference, leaving the one associated with the
1813 * descriptor table intact.
1815 fsetfd(p, fp, indx);
1822 * open_args(char *path, int flags, int mode)
1824 * Check permissions, allocate an open file structure,
1825 * and call the device open routine if any.
1828 sys_open(struct open_args *uap)
1830 struct nlookupdata nd;
1833 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1835 error = kern_open(&nd, uap->flags,
1836 uap->mode, &uap->sysmsg_result);
1843 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1845 struct thread *td = curthread;
1846 struct proc *p = td->td_proc;
1854 switch (mode & S_IFMT) {
1857 error = priv_check(td, PRIV_ROOT);
1860 error = priv_check_cred(p->p_ucred, PRIV_ROOT, PRISON_ROOT);
1867 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1868 if ((error = nlookup(nd)) != 0)
1870 if (nd->nl_nch.ncp->nc_vp)
1872 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1876 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1877 vattr.va_rmajor = rmajor;
1878 vattr.va_rminor = rminor;
1881 switch (mode & S_IFMT) {
1882 case S_IFMT: /* used by badsect to flag bad sectors */
1883 vattr.va_type = VBAD;
1886 vattr.va_type = VCHR;
1889 vattr.va_type = VBLK;
1895 /* special directories support for HAMMER */
1896 vattr.va_type = VDIR;
1904 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1905 nd->nl_cred, NAMEI_CREATE);
1908 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1909 &vp, nd->nl_cred, &vattr);
1918 * mknod_args(char *path, int mode, int dev)
1920 * Create a special file.
1923 sys_mknod(struct mknod_args *uap)
1925 struct nlookupdata nd;
1928 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1930 error = kern_mknod(&nd, uap->mode,
1931 umajor(uap->dev), uminor(uap->dev));
1938 kern_mkfifo(struct nlookupdata *nd, int mode)
1940 struct thread *td = curthread;
1941 struct proc *p = td->td_proc;
1948 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1949 if ((error = nlookup(nd)) != 0)
1951 if (nd->nl_nch.ncp->nc_vp)
1953 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1957 vattr.va_type = VFIFO;
1958 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1960 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
1967 * mkfifo_args(char *path, int mode)
1969 * Create a named pipe.
1972 sys_mkfifo(struct mkfifo_args *uap)
1974 struct nlookupdata nd;
1977 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1979 error = kern_mkfifo(&nd, uap->mode);
1984 static int hardlink_check_uid = 0;
1985 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1986 &hardlink_check_uid, 0,
1987 "Unprivileged processes cannot create hard links to files owned by other "
1989 static int hardlink_check_gid = 0;
1990 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1991 &hardlink_check_gid, 0,
1992 "Unprivileged processes cannot create hard links to files owned by other "
1996 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2002 * Shortcut if disabled
2004 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2008 * root cred can always hardlink
2010 if (priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT) == 0)
2014 * Otherwise only if the originating file is owned by the
2015 * same user or group. Note that any group is allowed if
2016 * the file is owned by the caller.
2018 error = VOP_GETATTR(vp, &va);
2022 if (hardlink_check_uid) {
2023 if (cred->cr_uid != va.va_uid)
2027 if (hardlink_check_gid) {
2028 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2036 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2038 struct thread *td = curthread;
2043 * Lookup the source and obtained a locked vnode.
2045 * XXX relookup on vget failure / race ?
2048 if ((error = nlookup(nd)) != 0)
2050 vp = nd->nl_nch.ncp->nc_vp;
2051 KKASSERT(vp != NULL);
2052 if (vp->v_type == VDIR)
2053 return (EPERM); /* POSIX */
2054 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2056 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2060 * Unlock the source so we can lookup the target without deadlocking
2061 * (XXX vp is locked already, possible other deadlock?). The target
2064 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2065 nd->nl_flags &= ~NLC_NCPISLOCKED;
2066 cache_unlock(&nd->nl_nch);
2068 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2069 if ((error = nlookup(linknd)) != 0) {
2073 if (linknd->nl_nch.ncp->nc_vp) {
2079 * Finally run the new API VOP.
2081 error = can_hardlink(vp, td, td->td_proc->p_ucred);
2083 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2084 vp, linknd->nl_cred);
2091 * link_args(char *path, char *link)
2093 * Make a hard file link.
2096 sys_link(struct link_args *uap)
2098 struct nlookupdata nd, linknd;
2101 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2103 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2105 error = kern_link(&nd, &linknd);
2106 nlookup_done(&linknd);
2113 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2121 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2122 if ((error = nlookup(nd)) != 0)
2124 if (nd->nl_nch.ncp->nc_vp)
2126 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2130 vattr.va_mode = mode;
2131 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2138 * symlink(char *path, char *link)
2140 * Make a symbolic link.
2143 sys_symlink(struct symlink_args *uap)
2145 struct thread *td = curthread;
2146 struct nlookupdata nd;
2151 path = objcache_get(namei_oc, M_WAITOK);
2152 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2154 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2156 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2157 error = kern_symlink(&nd, path, mode);
2161 objcache_put(namei_oc, path);
2166 * undelete_args(char *path)
2168 * Delete a whiteout from the filesystem.
2172 sys_undelete(struct undelete_args *uap)
2174 struct nlookupdata nd;
2177 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2179 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2181 error = nlookup(&nd);
2183 error = ncp_writechk(&nd.nl_nch);
2185 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2193 kern_unlink(struct nlookupdata *nd)
2198 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2199 if ((error = nlookup(nd)) != 0)
2201 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2203 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2208 * unlink_args(char *path)
2210 * Delete a name from the filesystem.
2213 sys_unlink(struct unlink_args *uap)
2215 struct nlookupdata nd;
2218 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2220 error = kern_unlink(&nd);
2226 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2228 struct thread *td = curthread;
2229 struct proc *p = td->td_proc;
2236 fp = holdfp(p->p_fd, fd, -1);
2239 if (fp->f_type != DTYPE_VNODE) {
2243 vp = (struct vnode *)fp->f_data;
2247 new_offset = fp->f_offset + offset;
2251 error = VOP_GETATTR(vp, &vattr);
2252 new_offset = offset + vattr.va_size;
2255 new_offset = offset;
2265 * Validate the seek position. Negative offsets are not allowed
2266 * for regular files, block specials, or directories.
2269 if (new_offset < 0 &&
2270 (vp->v_type == VREG || vp->v_type == VDIR ||
2271 vp->v_type == VCHR || vp->v_type == VBLK)) {
2274 fp->f_offset = new_offset;
2277 *res = fp->f_offset;
2284 * lseek_args(int fd, int pad, off_t offset, int whence)
2286 * Reposition read/write file offset.
2289 sys_lseek(struct lseek_args *uap)
2293 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2294 &uap->sysmsg_offset);
2300 kern_access(struct nlookupdata *nd, int aflags)
2305 if ((error = nlookup(nd)) != 0)
2308 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2312 /* Flags == 0 means only check for existence. */
2321 if ((flags & VWRITE) == 0 ||
2322 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2323 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2326 * If the file handle is stale we have to re-resolve the
2327 * entry. This is a hack at the moment.
2329 if (error == ESTALE) {
2331 cache_setunresolved(&nd->nl_nch);
2332 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2345 * access_args(char *path, int flags)
2347 * Check access permissions.
2350 sys_access(struct access_args *uap)
2352 struct nlookupdata nd;
2355 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2357 error = kern_access(&nd, uap->flags);
2363 kern_stat(struct nlookupdata *nd, struct stat *st)
2369 if ((error = nlookup(nd)) != 0)
2372 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2376 if ((error = vget(vp, LK_SHARED)) != 0)
2378 error = vn_stat(vp, st, nd->nl_cred);
2381 * If the file handle is stale we have to re-resolve the entry. This
2382 * is a hack at the moment.
2384 if (error == ESTALE) {
2386 cache_setunresolved(&nd->nl_nch);
2387 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2397 * stat_args(char *path, struct stat *ub)
2399 * Get file status; this version follows links.
2402 sys_stat(struct stat_args *uap)
2404 struct nlookupdata nd;
2408 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2410 error = kern_stat(&nd, &st);
2412 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2419 * lstat_args(char *path, struct stat *ub)
2421 * Get file status; this version does not follow links.
2424 sys_lstat(struct lstat_args *uap)
2426 struct nlookupdata nd;
2430 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2432 error = kern_stat(&nd, &st);
2434 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2441 * pathconf_Args(char *path, int name)
2443 * Get configurable pathname variables.
2447 sys_pathconf(struct pathconf_args *uap)
2449 struct nlookupdata nd;
2454 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2456 error = nlookup(&nd);
2458 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2461 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2469 * kern_readlink isn't properly split yet. There is a copyin burried
2470 * in VOP_READLINK().
2473 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2475 struct thread *td = curthread;
2476 struct proc *p = td->td_proc;
2482 if ((error = nlookup(nd)) != 0)
2484 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2487 if (vp->v_type != VLNK) {
2490 aiov.iov_base = buf;
2491 aiov.iov_len = count;
2492 auio.uio_iov = &aiov;
2493 auio.uio_iovcnt = 1;
2494 auio.uio_offset = 0;
2495 auio.uio_rw = UIO_READ;
2496 auio.uio_segflg = UIO_USERSPACE;
2498 auio.uio_resid = count;
2499 error = VOP_READLINK(vp, &auio, p->p_ucred);
2502 *res = count - auio.uio_resid;
2507 * readlink_args(char *path, char *buf, int count)
2509 * Return target name of a symbolic link.
2512 sys_readlink(struct readlink_args *uap)
2514 struct nlookupdata nd;
2517 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2519 error = kern_readlink(&nd, uap->buf, uap->count,
2520 &uap->sysmsg_result);
2527 setfflags(struct vnode *vp, int flags)
2529 struct thread *td = curthread;
2530 struct proc *p = td->td_proc;
2535 * Prevent non-root users from setting flags on devices. When
2536 * a device is reused, users can retain ownership of the device
2537 * if they are allowed to set flags and programs assume that
2538 * chown can't fail when done as root.
2540 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2541 ((error = priv_check_cred(p->p_ucred, PRIV_ROOT, PRISON_ROOT)) != 0))
2545 * note: vget is required for any operation that might mod the vnode
2546 * so VINACTIVE is properly cleared.
2548 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2550 vattr.va_flags = flags;
2551 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2558 * chflags(char *path, int flags)
2560 * Change flags of a file given a path name.
2564 sys_chflags(struct chflags_args *uap)
2566 struct nlookupdata nd;
2571 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2572 /* XXX Add NLC flag indicating modifying operation? */
2574 error = nlookup(&nd);
2576 error = ncp_writechk(&nd.nl_nch);
2578 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2581 error = setfflags(vp, uap->flags);
2588 * lchflags(char *path, int flags)
2590 * Change flags of a file given a path name, but don't follow symlinks.
2594 sys_lchflags(struct lchflags_args *uap)
2596 struct nlookupdata nd;
2601 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2602 /* XXX Add NLC flag indicating modifying operation? */
2604 error = nlookup(&nd);
2606 error = ncp_writechk(&nd.nl_nch);
2608 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2611 error = setfflags(vp, uap->flags);
2618 * fchflags_args(int fd, int flags)
2620 * Change flags of a file given a file descriptor.
2624 sys_fchflags(struct fchflags_args *uap)
2626 struct thread *td = curthread;
2627 struct proc *p = td->td_proc;
2631 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2633 if (fp->f_nchandle.ncp)
2634 error = ncp_writechk(&fp->f_nchandle);
2636 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2642 setfmode(struct vnode *vp, int mode)
2644 struct thread *td = curthread;
2645 struct proc *p = td->td_proc;
2650 * note: vget is required for any operation that might mod the vnode
2651 * so VINACTIVE is properly cleared.
2653 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2655 vattr.va_mode = mode & ALLPERMS;
2656 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2663 kern_chmod(struct nlookupdata *nd, int mode)
2668 /* XXX Add NLC flag indicating modifying operation? */
2669 if ((error = nlookup(nd)) != 0)
2671 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2673 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2674 error = setfmode(vp, mode);
2680 * chmod_args(char *path, int mode)
2682 * Change mode of a file given path name.
2686 sys_chmod(struct chmod_args *uap)
2688 struct nlookupdata nd;
2691 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2693 error = kern_chmod(&nd, uap->mode);
2699 * lchmod_args(char *path, int mode)
2701 * Change mode of a file given path name (don't follow links.)
2705 sys_lchmod(struct lchmod_args *uap)
2707 struct nlookupdata nd;
2710 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2712 error = kern_chmod(&nd, uap->mode);
2718 * fchmod_args(int fd, int mode)
2720 * Change mode of a file given a file descriptor.
2724 sys_fchmod(struct fchmod_args *uap)
2726 struct thread *td = curthread;
2727 struct proc *p = td->td_proc;
2731 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2733 if (fp->f_nchandle.ncp)
2734 error = ncp_writechk(&fp->f_nchandle);
2736 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2742 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2744 struct thread *td = curthread;
2745 struct proc *p = td->td_proc;
2750 * note: vget is required for any operation that might mod the vnode
2751 * so VINACTIVE is properly cleared.
2753 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2757 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2764 kern_chown(struct nlookupdata *nd, int uid, int gid)
2769 /* XXX Add NLC flag indicating modifying operation? */
2770 if ((error = nlookup(nd)) != 0)
2772 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2774 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2775 error = setfown(vp, uid, gid);
2781 * chown(char *path, int uid, int gid)
2783 * Set ownership given a path name.
2786 sys_chown(struct chown_args *uap)
2788 struct nlookupdata nd;
2791 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2793 error = kern_chown(&nd, uap->uid, uap->gid);
2799 * lchown_args(char *path, int uid, int gid)
2801 * Set ownership given a path name, do not cross symlinks.
2804 sys_lchown(struct lchown_args *uap)
2806 struct nlookupdata nd;
2809 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2811 error = kern_chown(&nd, uap->uid, uap->gid);
2817 * fchown_args(int fd, int uid, int gid)
2819 * Set ownership given a file descriptor.
2823 sys_fchown(struct fchown_args *uap)
2825 struct thread *td = curthread;
2826 struct proc *p = td->td_proc;
2830 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2832 if (fp->f_nchandle.ncp)
2833 error = ncp_writechk(&fp->f_nchandle);
2835 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2841 getutimes(const struct timeval *tvp, struct timespec *tsp)
2843 struct timeval tv[2];
2847 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2850 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2851 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2857 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2859 struct thread *td = curthread;
2860 struct proc *p = td->td_proc;
2865 * note: vget is required for any operation that might mod the vnode
2866 * so VINACTIVE is properly cleared.
2868 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2870 vattr.va_atime = ts[0];
2871 vattr.va_mtime = ts[1];
2873 vattr.va_vaflags |= VA_UTIMES_NULL;
2874 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2881 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2883 struct timespec ts[2];
2887 if ((error = getutimes(tptr, ts)) != 0)
2889 /* XXX Add NLC flag indicating modifying operation? */
2890 if ((error = nlookup(nd)) != 0)
2892 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2894 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2896 error = setutimes(vp, ts, tptr == NULL);
2902 * utimes_args(char *path, struct timeval *tptr)
2904 * Set the access and modification times of a file.
2907 sys_utimes(struct utimes_args *uap)
2909 struct timeval tv[2];
2910 struct nlookupdata nd;
2914 error = copyin(uap->tptr, tv, sizeof(tv));
2918 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2920 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2926 * lutimes_args(char *path, struct timeval *tptr)
2928 * Set the access and modification times of a file.
2931 sys_lutimes(struct lutimes_args *uap)
2933 struct timeval tv[2];
2934 struct nlookupdata nd;
2938 error = copyin(uap->tptr, tv, sizeof(tv));
2942 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2944 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2950 kern_futimes(int fd, struct timeval *tptr)
2952 struct thread *td = curthread;
2953 struct proc *p = td->td_proc;
2954 struct timespec ts[2];
2958 error = getutimes(tptr, ts);
2961 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2963 if (fp->f_nchandle.ncp)
2964 error = ncp_writechk(&fp->f_nchandle);
2966 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2972 * futimes_args(int fd, struct timeval *tptr)
2974 * Set the access and modification times of a file.
2977 sys_futimes(struct futimes_args *uap)
2979 struct timeval tv[2];
2983 error = copyin(uap->tptr, tv, sizeof(tv));
2988 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2994 kern_truncate(struct nlookupdata *nd, off_t length)
3002 /* XXX Add NLC flag indicating modifying operation? */
3003 if ((error = nlookup(nd)) != 0)
3005 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3007 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3009 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3013 if (vp->v_type == VDIR) {
3015 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
3016 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
3018 vattr.va_size = length;
3019 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3026 * truncate(char *path, int pad, off_t length)
3028 * Truncate a file given its path name.
3031 sys_truncate(struct truncate_args *uap)
3033 struct nlookupdata nd;
3036 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3038 error = kern_truncate(&nd, uap->length);
3044 kern_ftruncate(int fd, off_t length)
3046 struct thread *td = curthread;
3047 struct proc *p = td->td_proc;
3055 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3057 if (fp->f_nchandle.ncp) {
3058 error = ncp_writechk(&fp->f_nchandle);
3062 if ((fp->f_flag & FWRITE) == 0) {
3066 vp = (struct vnode *)fp->f_data;
3067 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3068 if (vp->v_type == VDIR) {
3070 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3072 vattr.va_size = length;
3073 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3082 * ftruncate_args(int fd, int pad, off_t length)
3084 * Truncate a file given a file descriptor.
3087 sys_ftruncate(struct ftruncate_args *uap)
3091 error = kern_ftruncate(uap->fd, uap->length);
3099 * Sync an open file.
3103 sys_fsync(struct fsync_args *uap)
3105 struct thread *td = curthread;
3106 struct proc *p = td->td_proc;
3112 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3114 vp = (struct vnode *)fp->f_data;
3115 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3116 if ((obj = vp->v_object) != NULL)
3117 vm_object_page_clean(obj, 0, 0, 0);
3118 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 && vp->v_mount)
3119 error = buf_fsync(vp);
3126 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3128 struct nchandle fnchd;
3129 struct nchandle tnchd;
3130 struct namecache *ncp;
3137 fromnd->nl_flags |= NLC_REFDVP;
3138 if ((error = nlookup(fromnd)) != 0)
3140 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3142 fnchd.mount = fromnd->nl_nch.mount;
3146 * unlock the source nch so we can lookup the target nch without
3147 * deadlocking. The target may or may not exist so we do not check
3148 * for a target vp like kern_mkdir() and other creation functions do.
3150 * The source and target directories are ref'd and rechecked after
3151 * everything is relocked to determine if the source or target file
3154 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3155 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3156 cache_unlock(&fromnd->nl_nch);
3158 tond->nl_flags |= NLC_CREATE | NLC_REFDVP;
3159 if ((error = nlookup(tond)) != 0) {
3163 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3167 tnchd.mount = tond->nl_nch.mount;
3171 * If the source and target are the same there is nothing to do
3173 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3180 * Mount points cannot be renamed or overwritten
3182 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3191 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3192 * may have become invalid while it was unlocked, nc_vp and nc_mount
3195 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3196 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3197 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3198 cache_lock(&fromnd->nl_nch);
3199 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3201 cache_unlock(&tond->nl_nch);
3202 cache_lock(&fromnd->nl_nch);
3203 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3204 cache_lock(&tond->nl_nch);
3205 cache_resolve(&tond->nl_nch, tond->nl_cred);
3207 fromnd->nl_flags |= NLC_NCPISLOCKED;
3210 * make sure the parent directories linkages are the same
3212 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3213 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3220 * Both the source and target must be within the same filesystem and
3221 * in the same filesystem as their parent directories within the
3222 * namecache topology.
3224 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3227 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3228 mp != tond->nl_nch.mount) {
3235 * Make sure the mount point is writable
3237 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3244 * If the target exists and either the source or target is a directory,
3245 * then both must be directories.
3247 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3250 if (tond->nl_nch.ncp->nc_vp) {
3251 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3253 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3254 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3256 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3262 * You cannot rename a source into itself or a subdirectory of itself.
3263 * We check this by travsersing the target directory upwards looking
3264 * for a match against the source.
3267 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3268 if (fromnd->nl_nch.ncp == ncp) {
3279 * Even though the namespaces are different, they may still represent
3280 * hardlinks to the same file. The filesystem might have a hard time
3281 * with this so we issue a NREMOVE of the source instead of a NRENAME
3282 * when we detect the situation.
3285 fdvp = fromnd->nl_dvp;
3286 tdvp = tond->nl_dvp;
3287 if (fdvp == NULL || tdvp == NULL) {
3289 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3290 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3293 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3294 fdvp, tdvp, tond->nl_cred);
3301 * rename_args(char *from, char *to)
3303 * Rename files. Source and destination must either both be directories,
3304 * or both not be directories. If target is a directory, it must be empty.
3307 sys_rename(struct rename_args *uap)
3309 struct nlookupdata fromnd, tond;
3312 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3314 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3316 error = kern_rename(&fromnd, &tond);
3317 nlookup_done(&tond);
3319 nlookup_done(&fromnd);
3324 kern_mkdir(struct nlookupdata *nd, int mode)
3326 struct thread *td = curthread;
3327 struct proc *p = td->td_proc;
3333 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3334 if ((error = nlookup(nd)) != 0)
3337 if (nd->nl_nch.ncp->nc_vp)
3339 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3342 vattr.va_type = VDIR;
3343 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3346 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3353 * mkdir_args(char *path, int mode)
3355 * Make a directory file.
3359 sys_mkdir(struct mkdir_args *uap)
3361 struct nlookupdata nd;
3364 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3366 error = kern_mkdir(&nd, uap->mode);
3372 kern_rmdir(struct nlookupdata *nd)
3377 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3378 if ((error = nlookup(nd)) != 0)
3382 * Do not allow directories representing mount points to be
3383 * deleted, even if empty. Check write perms on mount point
3384 * in case the vnode is aliased (aka nullfs).
3386 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3388 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3390 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3395 * rmdir_args(char *path)
3397 * Remove a directory file.
3401 sys_rmdir(struct rmdir_args *uap)
3403 struct nlookupdata nd;
3406 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3408 error = kern_rmdir(&nd);
3414 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3415 enum uio_seg direction)
3417 struct thread *td = curthread;
3418 struct proc *p = td->td_proc;
3426 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3428 if ((fp->f_flag & FREAD) == 0) {
3432 vp = (struct vnode *)fp->f_data;
3434 if (vp->v_type != VDIR) {
3438 aiov.iov_base = buf;
3439 aiov.iov_len = count;
3440 auio.uio_iov = &aiov;
3441 auio.uio_iovcnt = 1;
3442 auio.uio_rw = UIO_READ;
3443 auio.uio_segflg = direction;
3445 auio.uio_resid = count;
3446 loff = auio.uio_offset = fp->f_offset;
3447 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3448 fp->f_offset = auio.uio_offset;
3451 if (count == auio.uio_resid) {
3452 if (union_dircheckp) {
3453 error = union_dircheckp(td, &vp, fp);
3460 if ((vp->v_flag & VROOT) &&
3461 (vp->v_mount->mnt_flag & MNT_UNION)) {
3462 struct vnode *tvp = vp;
3463 vp = vp->v_mount->mnt_vnodecovered;
3474 * WARNING! *basep may not be wide enough to accomodate the
3475 * seek offset. XXX should we hack this to return the upper 32 bits
3476 * for offsets greater then 4G?
3479 *basep = (long)loff;
3481 *res = count - auio.uio_resid;
3488 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3490 * Read a block of directory entries in a file system independent format.
3493 sys_getdirentries(struct getdirentries_args *uap)
3498 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3499 &uap->sysmsg_result, UIO_USERSPACE);
3501 if (error == 0 && uap->basep)
3502 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3507 * getdents_args(int fd, char *buf, size_t count)
3510 sys_getdents(struct getdents_args *uap)
3514 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3515 &uap->sysmsg_result, UIO_USERSPACE);
3521 * umask(int newmask)
3523 * Set the mode mask for creation of filesystem nodes.
3528 sys_umask(struct umask_args *uap)
3530 struct thread *td = curthread;
3531 struct proc *p = td->td_proc;
3532 struct filedesc *fdp;
3535 uap->sysmsg_result = fdp->fd_cmask;
3536 fdp->fd_cmask = uap->newmask & ALLPERMS;
3541 * revoke(char *path)
3543 * Void all references to file by ripping underlying filesystem
3548 sys_revoke(struct revoke_args *uap)
3550 struct nlookupdata nd;
3557 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3559 error = nlookup(&nd);
3561 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3562 cred = crhold(nd.nl_cred);
3565 if (vp->v_type != VCHR && vp->v_type != VBLK)
3568 error = VOP_GETATTR(vp, &vattr);
3569 if (error == 0 && cred->cr_uid != vattr.va_uid)
3570 error = priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT);
3571 if (error == 0 && count_udev(vp->v_umajor, vp->v_uminor) > 0) {
3574 VOP_REVOKE(vp, REVOKEALL);
3585 * getfh_args(char *fname, fhandle_t *fhp)
3587 * Get (NFS) file handle
3589 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
3590 * mount. This allows nullfs mounts to be explicitly exported.
3592 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
3594 * nullfs mounts of subdirectories are not safe. That is, it will
3595 * work, but you do not really have protection against access to
3596 * the related parent directories.
3599 sys_getfh(struct getfh_args *uap)
3601 struct thread *td = curthread;
3602 struct nlookupdata nd;
3609 * Must be super user
3611 if ((error = priv_check(td, PRIV_ROOT)) != 0)
3615 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3617 error = nlookup(&nd);
3619 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3620 mp = nd.nl_nch.mount;
3623 bzero(&fh, sizeof(fh));
3624 fh.fh_fsid = mp->mnt_stat.f_fsid;
3625 error = VFS_VPTOFH(vp, &fh.fh_fid);
3628 error = copyout(&fh, uap->fhp, sizeof(fh));
3634 * fhopen_args(const struct fhandle *u_fhp, int flags)
3636 * syscall for the rpc.lockd to use to translate a NFS file handle into
3637 * an open descriptor.
3639 * warning: do not remove the priv_check() call or this becomes one giant
3643 sys_fhopen(struct fhopen_args *uap)
3645 struct thread *td = curthread;
3646 struct proc *p = td->td_proc;
3651 struct vattr *vap = &vat;
3653 int fmode, mode, error, type;
3659 * Must be super user
3661 error = priv_check(td, PRIV_ROOT);
3665 fmode = FFLAGS(uap->flags);
3666 /* why not allow a non-read/write open for our lockd? */
3667 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3669 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3672 /* find the mount point */
3673 mp = vfs_getvfs(&fhp.fh_fsid);
3676 /* now give me my vnode, it gets returned to me locked */
3677 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
3681 * from now on we have to make sure not
3682 * to forget about the vnode
3683 * any error that causes an abort must vput(vp)
3684 * just set error = err and 'goto bad;'.
3690 if (vp->v_type == VLNK) {
3694 if (vp->v_type == VSOCK) {
3699 if (fmode & (FWRITE | O_TRUNC)) {
3700 if (vp->v_type == VDIR) {
3704 error = vn_writechk(vp, NULL);
3712 error = VOP_ACCESS(vp, mode, p->p_ucred);
3716 if (fmode & O_TRUNC) {
3717 vn_unlock(vp); /* XXX */
3718 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3721 error = VOP_SETATTR(vp, vap, p->p_ucred);
3727 * VOP_OPEN needs the file pointer so it can potentially override
3730 * WARNING! no f_nchandle will be associated when fhopen()ing a
3733 if ((error = falloc(p, &nfp, &indx)) != 0)
3737 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3740 * setting f_ops this way prevents VOP_CLOSE from being
3741 * called or fdrop() releasing the vp from v_data. Since
3742 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3744 fp->f_ops = &badfileops;
3750 * The fp is given its own reference, we still have our ref and lock.
3752 * Assert that all regular files must be created with a VM object.
3754 if (vp->v_type == VREG && vp->v_object == NULL) {
3755 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3760 * The open was successful. Handle any locking requirements.
3762 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3763 lf.l_whence = SEEK_SET;
3766 if (fmode & O_EXLOCK)
3767 lf.l_type = F_WRLCK;
3769 lf.l_type = F_RDLCK;
3770 if (fmode & FNONBLOCK)
3775 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3777 * release our private reference.
3779 fsetfd(p, NULL, indx);
3784 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3785 fp->f_flag |= FHASLOCK;
3789 * Clean up. Associate the file pointer with the previously
3790 * reserved descriptor and return it.
3793 fsetfd(p, fp, indx);
3795 uap->sysmsg_result = indx;
3799 fsetfd(p, NULL, indx);
3807 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3810 sys_fhstat(struct fhstat_args *uap)
3812 struct thread *td = curthread;
3820 * Must be super user
3822 error = priv_check(td, PRIV_ROOT);
3826 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3830 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3832 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3834 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3838 error = copyout(&sb, uap->sb, sizeof(sb));
3843 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3846 sys_fhstatfs(struct fhstatfs_args *uap)
3848 struct thread *td = curthread;
3849 struct proc *p = td->td_proc;
3854 char *fullpath, *freepath;
3859 * Must be super user
3861 if ((error = priv_check(td, PRIV_ROOT)))
3864 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3867 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3870 if (p != NULL && !chroot_visible_mnt(mp, p))
3873 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3878 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3881 error = mount_path(p, mp, &fullpath, &freepath);
3884 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3885 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3886 kfree(freepath, M_TEMP);
3888 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3889 if (priv_check(td, PRIV_ROOT)) {
3890 bcopy(sp, &sb, sizeof(sb));
3891 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3894 return (copyout(sp, uap->buf, sizeof(*sp)));
3898 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
3901 sys_fhstatvfs(struct fhstatvfs_args *uap)
3903 struct thread *td = curthread;
3904 struct proc *p = td->td_proc;
3912 * Must be super user
3914 if ((error = priv_check(td, PRIV_ROOT)))
3917 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3920 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3923 if (p != NULL && !chroot_visible_mnt(mp, p))
3926 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3929 sp = &mp->mnt_vstat;
3931 if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
3935 if (mp->mnt_flag & MNT_RDONLY)
3936 sp->f_flag |= ST_RDONLY;
3937 if (mp->mnt_flag & MNT_NOSUID)
3938 sp->f_flag |= ST_NOSUID;
3940 return (copyout(sp, uap->buf, sizeof(*sp)));
3945 * Syscall to push extended attribute configuration information into the
3946 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3947 * a command (int cmd), and attribute name and misc data. For now, the
3948 * attribute name is left in userspace for consumption by the VFS_op.
3949 * It will probably be changed to be copied into sysspace by the
3950 * syscall in the future, once issues with various consumers of the
3951 * attribute code have raised their hands.
3953 * Currently this is used only by UFS Extended Attributes.
3956 sys_extattrctl(struct extattrctl_args *uap)
3958 struct nlookupdata nd;
3964 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3966 error = nlookup(&nd);
3968 mp = nd.nl_nch.mount;
3969 error = VFS_EXTATTRCTL(mp, uap->cmd,
3970 uap->attrname, uap->arg,
3978 * Syscall to set a named extended attribute on a file or directory.
3979 * Accepts attribute name, and a uio structure pointing to the data to set.
3980 * The uio is consumed in the style of writev(). The real work happens
3981 * in VOP_SETEXTATTR().
3984 sys_extattr_set_file(struct extattr_set_file_args *uap)
3986 char attrname[EXTATTR_MAXNAMELEN];
3987 struct iovec aiov[UIO_SMALLIOV];
3988 struct iovec *needfree;
3989 struct nlookupdata nd;
3998 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4003 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4005 error = nlookup(&nd);
4007 error = ncp_writechk(&nd.nl_nch);
4009 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4016 iovlen = uap->iovcnt * sizeof(struct iovec);
4017 if (uap->iovcnt > UIO_SMALLIOV) {
4018 if (uap->iovcnt > UIO_MAXIOV) {
4022 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4028 auio.uio_iovcnt = uap->iovcnt;
4029 auio.uio_rw = UIO_WRITE;
4030 auio.uio_segflg = UIO_USERSPACE;
4031 auio.uio_td = nd.nl_td;
4032 auio.uio_offset = 0;
4033 if ((error = copyin(uap->iovp, iov, iovlen)))
4036 for (i = 0; i < uap->iovcnt; i++) {
4037 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4041 auio.uio_resid += iov->iov_len;
4044 cnt = auio.uio_resid;
4045 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4046 cnt -= auio.uio_resid;
4047 uap->sysmsg_result = cnt;
4052 FREE(needfree, M_IOV);
4057 * Syscall to get a named extended attribute on a file or directory.
4058 * Accepts attribute name, and a uio structure pointing to a buffer for the
4059 * data. The uio is consumed in the style of readv(). The real work
4060 * happens in VOP_GETEXTATTR();
4063 sys_extattr_get_file(struct extattr_get_file_args *uap)
4065 char attrname[EXTATTR_MAXNAMELEN];
4066 struct iovec aiov[UIO_SMALLIOV];
4067 struct iovec *needfree;
4068 struct nlookupdata nd;
4077 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4082 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4084 error = nlookup(&nd);
4086 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4092 iovlen = uap->iovcnt * sizeof (struct iovec);
4094 if (uap->iovcnt > UIO_SMALLIOV) {
4095 if (uap->iovcnt > UIO_MAXIOV) {
4099 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4105 auio.uio_iovcnt = uap->iovcnt;
4106 auio.uio_rw = UIO_READ;
4107 auio.uio_segflg = UIO_USERSPACE;
4108 auio.uio_td = nd.nl_td;
4109 auio.uio_offset = 0;
4110 if ((error = copyin(uap->iovp, iov, iovlen)))
4113 for (i = 0; i < uap->iovcnt; i++) {
4114 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4118 auio.uio_resid += iov->iov_len;
4121 cnt = auio.uio_resid;
4122 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4123 cnt -= auio.uio_resid;
4124 uap->sysmsg_result = cnt;
4129 FREE(needfree, M_IOV);
4134 * Syscall to delete a named extended attribute from a file or directory.
4135 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4138 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4140 char attrname[EXTATTR_MAXNAMELEN];
4141 struct nlookupdata nd;
4145 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4150 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4152 error = nlookup(&nd);
4154 error = ncp_writechk(&nd.nl_nch);
4156 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4162 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4169 * Determine if the mount is visible to the process.
4172 chroot_visible_mnt(struct mount *mp, struct proc *p)
4174 struct nchandle nch;
4177 * Traverse from the mount point upwards. If we hit the process
4178 * root then the mount point is visible to the process.
4180 nch = mp->mnt_ncmountpt;
4182 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4183 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4186 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4187 nch = nch.mount->mnt_ncmounton;
4189 nch.ncp = nch.ncp->nc_parent;
4194 * If the mount point is not visible to the process, but the
4195 * process root is in a subdirectory of the mount, return
4198 if (p->p_fd->fd_nrdir.mount == mp)