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;
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 vfsp = vfsconf_find_by_name(fstypename);
268 /* Only load modules for root (very important!) */
269 if ((error = priv_check(td, PRIV_ROOT)) != 0) {
274 error = linker_load_file(fstypename, &lf);
275 if (error || lf == NULL) {
283 /* lookup again, see if the VFS was loaded */
284 vfsp = vfsconf_find_by_name(fstypename);
287 linker_file_unload(lf);
293 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
298 vp->v_flag |= VMOUNT;
301 * Allocate and initialize the filesystem.
303 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
304 TAILQ_INIT(&mp->mnt_nvnodelist);
305 TAILQ_INIT(&mp->mnt_reservedvnlist);
306 TAILQ_INIT(&mp->mnt_jlist);
307 mp->mnt_nvnodelistsize = 0;
308 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
309 vfs_busy(mp, LK_NOWAIT);
310 mp->mnt_op = vfsp->vfc_vfsops;
312 vfsp->vfc_refcount++;
313 mp->mnt_stat.f_type = vfsp->vfc_typenum;
314 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
315 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
316 mp->mnt_stat.f_owner = cred->cr_uid;
317 mp->mnt_iosize_max = DFLTPHYS;
321 * Set the mount level flags.
323 if (uap->flags & MNT_RDONLY)
324 mp->mnt_flag |= MNT_RDONLY;
325 else if (mp->mnt_flag & MNT_RDONLY)
326 mp->mnt_kern_flag |= MNTK_WANTRDWR;
327 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
328 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
329 MNT_NOSYMFOLLOW | MNT_IGNORE |
330 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
331 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
332 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
333 MNT_NOSYMFOLLOW | MNT_IGNORE |
334 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
336 * Mount the filesystem.
337 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
340 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
341 if (mp->mnt_flag & MNT_UPDATE) {
342 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
343 mp->mnt_flag &= ~MNT_RDONLY;
344 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
345 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
348 mp->mnt_kern_flag = flag2;
351 vp->v_flag &= ~VMOUNT;
356 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
358 * Put the new filesystem on the mount list after root. The mount
359 * point gets its own mnt_ncmountpt (unless the VFS already set one
360 * up) which represents the root of the mount. The lookup code
361 * detects the mount point going forward and checks the root of
362 * the mount going backwards.
364 * It is not necessary to invalidate or purge the vnode underneath
365 * because elements under the mount will be given their own glue
369 if (mp->mnt_ncmountpt.ncp == NULL) {
371 * allocate, then unlock, but leave the ref intact
373 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
374 cache_unlock(&mp->mnt_ncmountpt);
376 mp->mnt_ncmounton = nch; /* inherits ref */
377 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
379 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
380 vp->v_flag &= ~VMOUNT;
381 mountlist_insert(mp, MNTINS_LAST);
382 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
384 error = vfs_allocate_syncvnode(mp);
386 error = VFS_START(mp, 0);
389 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
390 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
391 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
392 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
393 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
394 vp->v_flag &= ~VMOUNT;
395 mp->mnt_vfc->vfc_refcount--;
405 * Scan all active processes to see if any of them have a current
406 * or root directory onto which the new filesystem has just been
407 * mounted. If so, replace them with the new mount point.
409 * The passed ncp is ref'd and locked (from the mount code) and
410 * must be associated with the vnode representing the root of the
413 struct checkdirs_info {
414 struct nchandle old_nch;
415 struct nchandle new_nch;
416 struct vnode *old_vp;
417 struct vnode *new_vp;
420 static int checkdirs_callback(struct proc *p, void *data);
423 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
425 struct checkdirs_info info;
431 * If the old mount point's vnode has a usecount of 1, it is not
432 * being held as a descriptor anywhere.
434 olddp = old_nch->ncp->nc_vp;
435 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
439 * Force the root vnode of the new mount point to be resolved
440 * so we can update any matching processes.
443 if (VFS_ROOT(mp, &newdp))
444 panic("mount: lost mount");
445 cache_setunresolved(new_nch);
446 cache_setvp(new_nch, newdp);
449 * Special handling of the root node
451 if (rootvnode == olddp) {
453 vfs_cache_setroot(newdp, cache_hold(new_nch));
457 * Pass newdp separately so the callback does not have to access
458 * it via new_nch->ncp->nc_vp.
460 info.old_nch = *old_nch;
461 info.new_nch = *new_nch;
463 allproc_scan(checkdirs_callback, &info);
468 * NOTE: callback is not MP safe because the scanned process's filedesc
469 * structure can be ripped out from under us, amoung other things.
472 checkdirs_callback(struct proc *p, void *data)
474 struct checkdirs_info *info = data;
475 struct filedesc *fdp;
476 struct nchandle ncdrop1;
477 struct nchandle ncdrop2;
478 struct vnode *vprele1;
479 struct vnode *vprele2;
481 if ((fdp = p->p_fd) != NULL) {
482 cache_zero(&ncdrop1);
483 cache_zero(&ncdrop2);
488 * MPUNSAFE - XXX fdp can be pulled out from under a
491 * A shared filedesc is ok, we don't have to copy it
492 * because we are making this change globally.
494 spin_lock_wr(&fdp->fd_spin);
495 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
496 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
497 vprele1 = fdp->fd_cdir;
499 fdp->fd_cdir = info->new_vp;
500 ncdrop1 = fdp->fd_ncdir;
501 cache_copy(&info->new_nch, &fdp->fd_ncdir);
503 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
504 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
505 vprele2 = fdp->fd_rdir;
507 fdp->fd_rdir = info->new_vp;
508 ncdrop2 = fdp->fd_nrdir;
509 cache_copy(&info->new_nch, &fdp->fd_nrdir);
511 spin_unlock_wr(&fdp->fd_spin);
513 cache_drop(&ncdrop1);
515 cache_drop(&ncdrop2);
525 * Unmount a file system.
527 * Note: unmount takes a path to the vnode mounted on as argument,
528 * not special file (as before).
531 * umount_args(char *path, int flags)
535 sys_unmount(struct unmount_args *uap)
537 struct thread *td = curthread;
538 struct proc *p = td->td_proc;
539 struct mount *mp = NULL;
541 struct nlookupdata nd;
544 if (p->p_ucred->cr_prison != NULL)
546 if (usermount == 0 && (error = priv_check(td, PRIV_ROOT)))
549 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
551 error = nlookup(&nd);
555 mp = nd.nl_nch.mount;
558 * Only root, or the user that did the original mount is
559 * permitted to unmount this filesystem.
561 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
562 (error = priv_check(td, PRIV_ROOT)))
566 * Don't allow unmounting the root file system.
568 if (mp->mnt_flag & MNT_ROOTFS) {
574 * Must be the root of the filesystem
576 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
585 return (dounmount(mp, uap->flags));
589 * Do the actual file system unmount.
592 dounmount_interlock(struct mount *mp)
594 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
596 mp->mnt_kern_flag |= MNTK_UNMOUNT;
601 dounmount(struct mount *mp, int flags)
603 struct namecache *ncp;
612 * Exclusive access for unmounting purposes
614 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
618 * Allow filesystems to detect that a forced unmount is in progress.
620 if (flags & MNT_FORCE)
621 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
622 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
623 error = lockmgr(&mp->mnt_lock, lflags);
625 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
626 if (mp->mnt_kern_flag & MNTK_MWAIT)
631 if (mp->mnt_flag & MNT_EXPUBLIC)
632 vfs_setpublicfs(NULL, NULL, NULL);
634 vfs_msync(mp, MNT_WAIT);
635 async_flag = mp->mnt_flag & MNT_ASYNC;
636 mp->mnt_flag &=~ MNT_ASYNC;
639 * If this filesystem isn't aliasing other filesystems,
640 * try to invalidate any remaining namecache entries and
641 * check the count afterwords.
643 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
644 cache_lock(&mp->mnt_ncmountpt);
645 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
646 cache_unlock(&mp->mnt_ncmountpt);
648 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
649 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
651 if ((flags & MNT_FORCE) == 0) {
653 mount_warning(mp, "Cannot unmount: "
659 mount_warning(mp, "Forced unmount: "
670 * nchandle records ref the mount structure. Expect a count of 1
671 * (our mount->mnt_ncmountpt).
673 if (mp->mnt_refs != 1) {
674 if ((flags & MNT_FORCE) == 0) {
675 mount_warning(mp, "Cannot unmount: "
676 "%d process references still "
677 "present", mp->mnt_refs);
680 mount_warning(mp, "Forced unmount: "
681 "%d process references still "
682 "present", mp->mnt_refs);
688 * Decomission our special mnt_syncer vnode. This also stops
689 * the vnlru code. If we are unable to unmount we recommission
693 if ((vp = mp->mnt_syncer) != NULL) {
694 mp->mnt_syncer = NULL;
697 if (((mp->mnt_flag & MNT_RDONLY) ||
698 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
699 (flags & MNT_FORCE)) {
700 error = VFS_UNMOUNT(mp, flags);
704 if (mp->mnt_syncer == NULL)
705 vfs_allocate_syncvnode(mp);
706 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
707 mp->mnt_flag |= async_flag;
708 lockmgr(&mp->mnt_lock, LK_RELEASE);
709 if (mp->mnt_kern_flag & MNTK_MWAIT)
714 * Clean up any journals still associated with the mount after
715 * filesystem activity has ceased.
717 journal_remove_all_journals(mp,
718 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
720 mountlist_remove(mp);
723 * Remove any installed vnode ops here so the individual VFSs don't
726 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
727 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
728 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
729 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
730 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
732 if (mp->mnt_ncmountpt.ncp != NULL) {
733 nch = mp->mnt_ncmountpt;
734 cache_zero(&mp->mnt_ncmountpt);
735 cache_clrmountpt(&nch);
738 if (mp->mnt_ncmounton.ncp != NULL) {
739 nch = mp->mnt_ncmounton;
740 cache_zero(&mp->mnt_ncmounton);
741 cache_clrmountpt(&nch);
745 mp->mnt_vfc->vfc_refcount--;
746 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
747 panic("unmount: dangling vnode");
748 lockmgr(&mp->mnt_lock, LK_RELEASE);
749 if (mp->mnt_kern_flag & MNTK_MWAIT)
758 mount_warning(struct mount *mp, const char *ctl, ...)
765 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
766 kprintf("unmount(%s): ", ptr);
771 kprintf("unmount(%p", mp);
772 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
773 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
782 * Shim cache_fullpath() to handle the case where a process is chrooted into
783 * a subdirectory of a mount. In this case if the root mount matches the
784 * process root directory's mount we have to specify the process's root
785 * directory instead of the mount point, because the mount point might
786 * be above the root directory.
790 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
792 struct nchandle *nch;
794 if (p && p->p_fd->fd_nrdir.mount == mp)
795 nch = &p->p_fd->fd_nrdir;
797 nch = &mp->mnt_ncmountpt;
798 return(cache_fullpath(p, nch, rb, fb));
802 * Sync each mounted filesystem.
806 static int syncprt = 0;
807 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
810 static int sync_callback(struct mount *mp, void *data);
814 sys_sync(struct sync_args *uap)
816 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
819 * print out buffer pool stat information on each sync() call.
829 sync_callback(struct mount *mp, void *data __unused)
833 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
834 asyncflag = mp->mnt_flag & MNT_ASYNC;
835 mp->mnt_flag &= ~MNT_ASYNC;
836 vfs_msync(mp, MNT_NOWAIT);
837 VFS_SYNC(mp, MNT_NOWAIT);
838 mp->mnt_flag |= asyncflag;
843 /* XXX PRISON: could be per prison flag */
844 static int prison_quotas;
846 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
850 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
852 * Change filesystem quotas.
856 sys_quotactl(struct quotactl_args *uap)
858 struct nlookupdata nd;
866 if (p->p_ucred->cr_prison && !prison_quotas)
869 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
871 error = nlookup(&nd);
873 mp = nd.nl_nch.mount;
874 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
875 uap->arg, nd.nl_cred);
882 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
883 * void *buf, int buflen)
885 * This function operates on a mount point and executes the specified
886 * operation using the specified control data, and possibly returns data.
888 * The actual number of bytes stored in the result buffer is returned, 0
889 * if none, otherwise an error is returned.
893 sys_mountctl(struct mountctl_args *uap)
895 struct thread *td = curthread;
896 struct proc *p = td->td_proc;
904 * Sanity and permissions checks. We must be root.
907 if (p->p_ucred->cr_prison != NULL)
909 if ((error = priv_check(td, PRIV_ROOT)) != 0)
913 * Argument length checks
915 if (uap->ctllen < 0 || uap->ctllen > 1024)
917 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
919 if (uap->path == NULL)
923 * Allocate the necessary buffers and copyin data
925 path = objcache_get(namei_oc, M_WAITOK);
926 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
931 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
932 error = copyin(uap->ctl, ctl, uap->ctllen);
937 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
940 * Validate the descriptor
943 fp = holdfp(p->p_fd, uap->fd, -1);
953 * Execute the internal kernel function and clean up.
955 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
958 if (error == 0 && uap->sysmsg_result > 0)
959 error = copyout(buf, uap->buf, uap->sysmsg_result);
962 objcache_put(namei_oc, path);
971 * Execute a mount control operation by resolving the path to a mount point
972 * and calling vop_mountctl().
974 * Use the mount point from the nch instead of the vnode so nullfs mounts
975 * can properly spike the VOP.
978 kern_mountctl(const char *path, int op, struct file *fp,
979 const void *ctl, int ctllen,
980 void *buf, int buflen, int *res)
984 struct nlookupdata nd;
989 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
991 error = nlookup(&nd);
993 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
994 mp = nd.nl_nch.mount;
1000 * Must be the root of the filesystem
1002 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1006 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1013 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1015 struct thread *td = curthread;
1016 struct proc *p = td->td_proc;
1019 char *fullpath, *freepath;
1022 if ((error = nlookup(nd)) != 0)
1024 mp = nd->nl_nch.mount;
1026 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1029 error = mount_path(p, mp, &fullpath, &freepath);
1032 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1033 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1034 kfree(freepath, M_TEMP);
1036 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1037 bcopy(sp, buf, sizeof(*buf));
1038 /* Only root should have access to the fsid's. */
1039 if (priv_check(td, PRIV_ROOT))
1040 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1045 * statfs_args(char *path, struct statfs *buf)
1047 * Get filesystem statistics.
1050 sys_statfs(struct statfs_args *uap)
1052 struct nlookupdata nd;
1056 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1058 error = kern_statfs(&nd, &buf);
1061 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1066 kern_fstatfs(int fd, struct statfs *buf)
1068 struct thread *td = curthread;
1069 struct proc *p = td->td_proc;
1073 char *fullpath, *freepath;
1077 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1079 mp = ((struct vnode *)fp->f_data)->v_mount;
1084 if (fp->f_cred == NULL) {
1089 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1092 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1094 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1095 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1096 kfree(freepath, M_TEMP);
1098 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1099 bcopy(sp, buf, sizeof(*buf));
1101 /* Only root should have access to the fsid's. */
1102 if (priv_check(td, PRIV_ROOT))
1103 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1111 * fstatfs_args(int fd, struct statfs *buf)
1113 * Get filesystem statistics.
1116 sys_fstatfs(struct fstatfs_args *uap)
1121 error = kern_fstatfs(uap->fd, &buf);
1124 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1129 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1135 if ((error = nlookup(nd)) != 0)
1137 mp = nd->nl_nch.mount;
1138 sp = &mp->mnt_vstat;
1139 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1143 if (mp->mnt_flag & MNT_RDONLY)
1144 sp->f_flag |= ST_RDONLY;
1145 if (mp->mnt_flag & MNT_NOSUID)
1146 sp->f_flag |= ST_NOSUID;
1147 bcopy(sp, buf, sizeof(*buf));
1152 * statfs_args(char *path, struct statfs *buf)
1154 * Get filesystem statistics.
1157 sys_statvfs(struct statvfs_args *uap)
1159 struct nlookupdata nd;
1163 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1165 error = kern_statvfs(&nd, &buf);
1168 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1173 kern_fstatvfs(int fd, struct statvfs *buf)
1175 struct thread *td = curthread;
1176 struct proc *p = td->td_proc;
1183 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1185 mp = ((struct vnode *)fp->f_data)->v_mount;
1190 if (fp->f_cred == NULL) {
1194 sp = &mp->mnt_vstat;
1195 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1199 if (mp->mnt_flag & MNT_RDONLY)
1200 sp->f_flag |= ST_RDONLY;
1201 if (mp->mnt_flag & MNT_NOSUID)
1202 sp->f_flag |= ST_NOSUID;
1204 bcopy(sp, buf, sizeof(*buf));
1212 * fstatfs_args(int fd, struct statfs *buf)
1214 * Get filesystem statistics.
1217 sys_fstatvfs(struct fstatvfs_args *uap)
1222 error = kern_fstatvfs(uap->fd, &buf);
1225 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1230 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1232 * Get statistics on all filesystems.
1235 struct getfsstat_info {
1236 struct statfs *sfsp;
1244 static int getfsstat_callback(struct mount *, void *);
1248 sys_getfsstat(struct getfsstat_args *uap)
1250 struct thread *td = curthread;
1251 struct proc *p = td->td_proc;
1252 struct getfsstat_info info;
1254 bzero(&info, sizeof(info));
1256 info.maxcount = uap->bufsize / sizeof(struct statfs);
1257 info.sfsp = uap->buf;
1259 info.flags = uap->flags;
1262 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1263 if (info.sfsp && info.count > info.maxcount)
1264 uap->sysmsg_result = info.maxcount;
1266 uap->sysmsg_result = info.count;
1267 return (info.error);
1271 getfsstat_callback(struct mount *mp, void *data)
1273 struct getfsstat_info *info = data;
1279 if (info->sfsp && info->count < info->maxcount) {
1280 if (info->p && !chroot_visible_mnt(mp, info->p))
1285 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1286 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1287 * overrides MNT_WAIT.
1289 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1290 (info->flags & MNT_WAIT)) &&
1291 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1294 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1296 error = mount_path(info->p, mp, &fullpath, &freepath);
1298 info->error = error;
1301 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1302 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1303 kfree(freepath, M_TEMP);
1305 error = copyout(sp, info->sfsp, sizeof(*sp));
1307 info->error = error;
1317 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1318 long bufsize, int flags)
1320 * Get statistics on all filesystems.
1323 struct getvfsstat_info {
1324 struct statfs *sfsp;
1325 struct statvfs *vsfsp;
1333 static int getvfsstat_callback(struct mount *, void *);
1337 sys_getvfsstat(struct getvfsstat_args *uap)
1339 struct thread *td = curthread;
1340 struct proc *p = td->td_proc;
1341 struct getvfsstat_info info;
1343 bzero(&info, sizeof(info));
1345 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1346 info.sfsp = uap->buf;
1347 info.vsfsp = uap->vbuf;
1349 info.flags = uap->flags;
1352 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1353 if (info.vsfsp && info.count > info.maxcount)
1354 uap->sysmsg_result = info.maxcount;
1356 uap->sysmsg_result = info.count;
1357 return (info.error);
1361 getvfsstat_callback(struct mount *mp, void *data)
1363 struct getvfsstat_info *info = data;
1365 struct statvfs *vsp;
1370 if (info->vsfsp && info->count < info->maxcount) {
1371 if (info->p && !chroot_visible_mnt(mp, info->p))
1374 vsp = &mp->mnt_vstat;
1377 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1378 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1379 * overrides MNT_WAIT.
1381 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1382 (info->flags & MNT_WAIT)) &&
1383 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1386 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1388 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1389 (info->flags & MNT_WAIT)) &&
1390 (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1394 if (mp->mnt_flag & MNT_RDONLY)
1395 vsp->f_flag |= ST_RDONLY;
1396 if (mp->mnt_flag & MNT_NOSUID)
1397 vsp->f_flag |= ST_NOSUID;
1399 error = mount_path(info->p, mp, &fullpath, &freepath);
1401 info->error = error;
1404 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1405 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1406 kfree(freepath, M_TEMP);
1408 error = copyout(sp, info->sfsp, sizeof(*sp));
1410 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1412 info->error = error;
1424 * fchdir_args(int fd)
1426 * Change current working directory to a given file descriptor.
1430 sys_fchdir(struct fchdir_args *uap)
1432 struct thread *td = curthread;
1433 struct proc *p = td->td_proc;
1434 struct filedesc *fdp = p->p_fd;
1435 struct vnode *vp, *ovp;
1438 struct nchandle nch, onch, tnch;
1441 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1443 vp = (struct vnode *)fp->f_data;
1445 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1446 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1449 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1455 cache_copy(&fp->f_nchandle, &nch);
1458 * If the ncp has become a mount point, traverse through
1462 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1463 (mp = cache_findmount(&nch)) != NULL
1465 error = nlookup_mp(mp, &tnch);
1467 cache_unlock(&tnch); /* leave ref intact */
1469 vp = tnch.ncp->nc_vp;
1470 error = vget(vp, LK_SHARED);
1471 KKASSERT(error == 0);
1478 onch = fdp->fd_ncdir;
1479 vn_unlock(vp); /* leave ref intact */
1481 fdp->fd_ncdir = nch;
1493 kern_chdir(struct nlookupdata *nd)
1495 struct thread *td = curthread;
1496 struct proc *p = td->td_proc;
1497 struct filedesc *fdp = p->p_fd;
1498 struct vnode *vp, *ovp;
1499 struct nchandle onch;
1502 if ((error = nlookup(nd)) != 0)
1504 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1506 if ((error = vget(vp, LK_SHARED)) != 0)
1509 error = checkvp_chdir(vp, td);
1513 onch = fdp->fd_ncdir;
1514 cache_unlock(&nd->nl_nch); /* leave reference intact */
1515 fdp->fd_ncdir = nd->nl_nch;
1519 cache_zero(&nd->nl_nch);
1527 * chdir_args(char *path)
1529 * Change current working directory (``.'').
1532 sys_chdir(struct chdir_args *uap)
1534 struct nlookupdata nd;
1537 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1539 error = kern_chdir(&nd);
1545 * Helper function for raised chroot(2) security function: Refuse if
1546 * any filedescriptors are open directories.
1549 chroot_refuse_vdir_fds(struct filedesc *fdp)
1556 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1557 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1559 vp = (struct vnode *)fp->f_data;
1560 if (vp->v_type != VDIR) {
1571 * This sysctl determines if we will allow a process to chroot(2) if it
1572 * has a directory open:
1573 * 0: disallowed for all processes.
1574 * 1: allowed for processes that were not already chroot(2)'ed.
1575 * 2: allowed for all processes.
1578 static int chroot_allow_open_directories = 1;
1580 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1581 &chroot_allow_open_directories, 0, "");
1584 * chroot to the specified namecache entry. We obtain the vp from the
1585 * namecache data. The passed ncp must be locked and referenced and will
1586 * remain locked and referenced on return.
1589 kern_chroot(struct nchandle *nch)
1591 struct thread *td = curthread;
1592 struct proc *p = td->td_proc;
1593 struct filedesc *fdp = p->p_fd;
1598 * Only root can chroot
1600 if ((error = priv_check_cred(p->p_ucred, PRIV_ROOT, PRISON_ROOT)) != 0)
1604 * Disallow open directory descriptors (fchdir() breakouts).
1606 if (chroot_allow_open_directories == 0 ||
1607 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1608 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1611 if ((vp = nch->ncp->nc_vp) == NULL)
1614 if ((error = vget(vp, LK_SHARED)) != 0)
1618 * Check the validity of vp as a directory to change to and
1619 * associate it with rdir/jdir.
1621 error = checkvp_chdir(vp, td);
1622 vn_unlock(vp); /* leave reference intact */
1624 vrele(fdp->fd_rdir);
1625 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1626 cache_drop(&fdp->fd_nrdir);
1627 cache_copy(nch, &fdp->fd_nrdir);
1628 if (fdp->fd_jdir == NULL) {
1631 cache_copy(nch, &fdp->fd_njdir);
1640 * chroot_args(char *path)
1642 * Change notion of root (``/'') directory.
1646 sys_chroot(struct chroot_args *uap)
1648 struct thread *td = curthread;
1649 struct nlookupdata nd;
1652 KKASSERT(td->td_proc);
1653 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1658 error = nlookup(&nd);
1660 error = kern_chroot(&nd.nl_nch);
1666 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1667 * determine whether it is legal to chdir to the vnode. The vnode's state
1668 * is not changed by this call.
1671 checkvp_chdir(struct vnode *vp, struct thread *td)
1675 if (vp->v_type != VDIR)
1678 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1683 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1685 struct thread *td = curthread;
1686 struct proc *p = td->td_proc;
1687 struct lwp *lp = td->td_lwp;
1688 struct filedesc *fdp = p->p_fd;
1693 int type, indx, error;
1696 if ((oflags & O_ACCMODE) == O_ACCMODE)
1698 flags = FFLAGS(oflags);
1699 error = falloc(p, &nfp, NULL);
1703 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1706 * XXX p_dupfd is a real mess. It allows a device to return a
1707 * file descriptor to be duplicated rather then doing the open
1713 * Call vn_open() to do the lookup and assign the vnode to the
1714 * file pointer. vn_open() does not change the ref count on fp
1715 * and the vnode, on success, will be inherited by the file pointer
1718 nd->nl_flags |= NLC_LOCKVP;
1719 error = vn_open(nd, fp, flags, cmode);
1723 * handle special fdopen() case. bleh. dupfdopen() is
1724 * responsible for dropping the old contents of ofiles[indx]
1727 * Note that fsetfd() will add a ref to fp which represents
1728 * the fd_files[] assignment. We must still drop our
1731 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1732 if (fdalloc(p, 0, &indx) == 0) {
1733 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1736 fdrop(fp); /* our ref */
1739 fsetfd(p, NULL, indx);
1742 fdrop(fp); /* our ref */
1743 if (error == ERESTART)
1749 * ref the vnode for ourselves so it can't be ripped out from under
1750 * is. XXX need an ND flag to request that the vnode be returned
1753 * Reserve a file descriptor but do not assign it until the open
1756 vp = (struct vnode *)fp->f_data;
1758 if ((error = fdalloc(p, 0, &indx)) != 0) {
1765 * If no error occurs the vp will have been assigned to the file
1770 if (flags & (O_EXLOCK | O_SHLOCK)) {
1771 lf.l_whence = SEEK_SET;
1774 if (flags & O_EXLOCK)
1775 lf.l_type = F_WRLCK;
1777 lf.l_type = F_RDLCK;
1778 if (flags & FNONBLOCK)
1783 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1785 * lock request failed. Clean up the reserved
1789 fsetfd(p, NULL, indx);
1793 fp->f_flag |= FHASLOCK;
1797 * Assert that all regular file vnodes were created with a object.
1799 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1800 ("open: regular file has no backing object after vn_open"));
1806 * release our private reference, leaving the one associated with the
1807 * descriptor table intact.
1809 fsetfd(p, fp, indx);
1816 * open_args(char *path, int flags, int mode)
1818 * Check permissions, allocate an open file structure,
1819 * and call the device open routine if any.
1822 sys_open(struct open_args *uap)
1824 struct nlookupdata nd;
1827 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1829 error = kern_open(&nd, uap->flags,
1830 uap->mode, &uap->sysmsg_result);
1837 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1839 struct thread *td = curthread;
1840 struct proc *p = td->td_proc;
1848 switch (mode & S_IFMT) {
1851 error = priv_check(td, PRIV_ROOT);
1854 error = priv_check_cred(p->p_ucred, PRIV_ROOT, PRISON_ROOT);
1861 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1862 if ((error = nlookup(nd)) != 0)
1864 if (nd->nl_nch.ncp->nc_vp)
1866 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1870 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1871 vattr.va_rmajor = rmajor;
1872 vattr.va_rminor = rminor;
1875 switch (mode & S_IFMT) {
1876 case S_IFMT: /* used by badsect to flag bad sectors */
1877 vattr.va_type = VBAD;
1880 vattr.va_type = VCHR;
1883 vattr.va_type = VBLK;
1889 /* special directories support for HAMMER */
1890 vattr.va_type = VDIR;
1898 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1899 nd->nl_cred, NAMEI_CREATE);
1902 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1903 &vp, nd->nl_cred, &vattr);
1912 * mknod_args(char *path, int mode, int dev)
1914 * Create a special file.
1917 sys_mknod(struct mknod_args *uap)
1919 struct nlookupdata nd;
1922 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1924 error = kern_mknod(&nd, uap->mode,
1925 umajor(uap->dev), uminor(uap->dev));
1932 kern_mkfifo(struct nlookupdata *nd, int mode)
1934 struct thread *td = curthread;
1935 struct proc *p = td->td_proc;
1942 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1943 if ((error = nlookup(nd)) != 0)
1945 if (nd->nl_nch.ncp->nc_vp)
1947 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1951 vattr.va_type = VFIFO;
1952 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1954 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
1961 * mkfifo_args(char *path, int mode)
1963 * Create a named pipe.
1966 sys_mkfifo(struct mkfifo_args *uap)
1968 struct nlookupdata nd;
1971 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1973 error = kern_mkfifo(&nd, uap->mode);
1978 static int hardlink_check_uid = 0;
1979 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1980 &hardlink_check_uid, 0,
1981 "Unprivileged processes cannot create hard links to files owned by other "
1983 static int hardlink_check_gid = 0;
1984 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1985 &hardlink_check_gid, 0,
1986 "Unprivileged processes cannot create hard links to files owned by other "
1990 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1996 * Shortcut if disabled
1998 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2002 * root cred can always hardlink
2004 if (priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT) == 0)
2008 * Otherwise only if the originating file is owned by the
2009 * same user or group. Note that any group is allowed if
2010 * the file is owned by the caller.
2012 error = VOP_GETATTR(vp, &va);
2016 if (hardlink_check_uid) {
2017 if (cred->cr_uid != va.va_uid)
2021 if (hardlink_check_gid) {
2022 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2030 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2032 struct thread *td = curthread;
2037 * Lookup the source and obtained a locked vnode.
2039 * XXX relookup on vget failure / race ?
2042 if ((error = nlookup(nd)) != 0)
2044 vp = nd->nl_nch.ncp->nc_vp;
2045 KKASSERT(vp != NULL);
2046 if (vp->v_type == VDIR)
2047 return (EPERM); /* POSIX */
2048 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2050 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2054 * Unlock the source so we can lookup the target without deadlocking
2055 * (XXX vp is locked already, possible other deadlock?). The target
2058 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2059 nd->nl_flags &= ~NLC_NCPISLOCKED;
2060 cache_unlock(&nd->nl_nch);
2062 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2063 if ((error = nlookup(linknd)) != 0) {
2067 if (linknd->nl_nch.ncp->nc_vp) {
2073 * Finally run the new API VOP.
2075 error = can_hardlink(vp, td, td->td_proc->p_ucred);
2077 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2078 vp, linknd->nl_cred);
2085 * link_args(char *path, char *link)
2087 * Make a hard file link.
2090 sys_link(struct link_args *uap)
2092 struct nlookupdata nd, linknd;
2095 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2097 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2099 error = kern_link(&nd, &linknd);
2100 nlookup_done(&linknd);
2107 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2115 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2116 if ((error = nlookup(nd)) != 0)
2118 if (nd->nl_nch.ncp->nc_vp)
2120 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2124 vattr.va_mode = mode;
2125 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2132 * symlink(char *path, char *link)
2134 * Make a symbolic link.
2137 sys_symlink(struct symlink_args *uap)
2139 struct thread *td = curthread;
2140 struct nlookupdata nd;
2145 path = objcache_get(namei_oc, M_WAITOK);
2146 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2148 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2150 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2151 error = kern_symlink(&nd, path, mode);
2155 objcache_put(namei_oc, path);
2160 * undelete_args(char *path)
2162 * Delete a whiteout from the filesystem.
2166 sys_undelete(struct undelete_args *uap)
2168 struct nlookupdata nd;
2171 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2173 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2175 error = nlookup(&nd);
2177 error = ncp_writechk(&nd.nl_nch);
2179 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2187 kern_unlink(struct nlookupdata *nd)
2192 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2193 if ((error = nlookup(nd)) != 0)
2195 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2197 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2202 * unlink_args(char *path)
2204 * Delete a name from the filesystem.
2207 sys_unlink(struct unlink_args *uap)
2209 struct nlookupdata nd;
2212 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2214 error = kern_unlink(&nd);
2220 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2222 struct thread *td = curthread;
2223 struct proc *p = td->td_proc;
2230 fp = holdfp(p->p_fd, fd, -1);
2233 if (fp->f_type != DTYPE_VNODE) {
2237 vp = (struct vnode *)fp->f_data;
2241 new_offset = fp->f_offset + offset;
2245 error = VOP_GETATTR(vp, &vattr);
2246 new_offset = offset + vattr.va_size;
2249 new_offset = offset;
2259 * Validate the seek position. Negative offsets are not allowed
2260 * for regular files, block specials, or directories.
2263 if (new_offset < 0 &&
2264 (vp->v_type == VREG || vp->v_type == VDIR ||
2265 vp->v_type == VCHR || vp->v_type == VBLK)) {
2268 fp->f_offset = new_offset;
2271 *res = fp->f_offset;
2278 * lseek_args(int fd, int pad, off_t offset, int whence)
2280 * Reposition read/write file offset.
2283 sys_lseek(struct lseek_args *uap)
2287 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2288 &uap->sysmsg_offset);
2294 kern_access(struct nlookupdata *nd, int aflags)
2299 if ((error = nlookup(nd)) != 0)
2302 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2306 /* Flags == 0 means only check for existence. */
2315 if ((flags & VWRITE) == 0 ||
2316 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2317 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2320 * If the file handle is stale we have to re-resolve the
2321 * entry. This is a hack at the moment.
2323 if (error == ESTALE) {
2325 cache_setunresolved(&nd->nl_nch);
2326 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2339 * access_args(char *path, int flags)
2341 * Check access permissions.
2344 sys_access(struct access_args *uap)
2346 struct nlookupdata nd;
2349 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2351 error = kern_access(&nd, uap->flags);
2357 kern_stat(struct nlookupdata *nd, struct stat *st)
2363 if ((error = nlookup(nd)) != 0)
2366 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2370 if ((error = vget(vp, LK_SHARED)) != 0)
2372 error = vn_stat(vp, st, nd->nl_cred);
2375 * If the file handle is stale we have to re-resolve the entry. This
2376 * is a hack at the moment.
2378 if (error == ESTALE) {
2380 cache_setunresolved(&nd->nl_nch);
2381 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2391 * stat_args(char *path, struct stat *ub)
2393 * Get file status; this version follows links.
2396 sys_stat(struct stat_args *uap)
2398 struct nlookupdata nd;
2402 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2404 error = kern_stat(&nd, &st);
2406 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2413 * lstat_args(char *path, struct stat *ub)
2415 * Get file status; this version does not follow links.
2418 sys_lstat(struct lstat_args *uap)
2420 struct nlookupdata nd;
2424 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2426 error = kern_stat(&nd, &st);
2428 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2435 * pathconf_Args(char *path, int name)
2437 * Get configurable pathname variables.
2441 sys_pathconf(struct pathconf_args *uap)
2443 struct nlookupdata nd;
2448 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2450 error = nlookup(&nd);
2452 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2455 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2463 * kern_readlink isn't properly split yet. There is a copyin burried
2464 * in VOP_READLINK().
2467 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2469 struct thread *td = curthread;
2470 struct proc *p = td->td_proc;
2476 if ((error = nlookup(nd)) != 0)
2478 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2481 if (vp->v_type != VLNK) {
2484 aiov.iov_base = buf;
2485 aiov.iov_len = count;
2486 auio.uio_iov = &aiov;
2487 auio.uio_iovcnt = 1;
2488 auio.uio_offset = 0;
2489 auio.uio_rw = UIO_READ;
2490 auio.uio_segflg = UIO_USERSPACE;
2492 auio.uio_resid = count;
2493 error = VOP_READLINK(vp, &auio, p->p_ucred);
2496 *res = count - auio.uio_resid;
2501 * readlink_args(char *path, char *buf, int count)
2503 * Return target name of a symbolic link.
2506 sys_readlink(struct readlink_args *uap)
2508 struct nlookupdata nd;
2511 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2513 error = kern_readlink(&nd, uap->buf, uap->count,
2514 &uap->sysmsg_result);
2521 setfflags(struct vnode *vp, int flags)
2523 struct thread *td = curthread;
2524 struct proc *p = td->td_proc;
2529 * Prevent non-root users from setting flags on devices. When
2530 * a device is reused, users can retain ownership of the device
2531 * if they are allowed to set flags and programs assume that
2532 * chown can't fail when done as root.
2534 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2535 ((error = priv_check_cred(p->p_ucred, PRIV_ROOT, PRISON_ROOT)) != 0))
2539 * note: vget is required for any operation that might mod the vnode
2540 * so VINACTIVE is properly cleared.
2542 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2544 vattr.va_flags = flags;
2545 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2552 * chflags(char *path, int flags)
2554 * Change flags of a file given a path name.
2558 sys_chflags(struct chflags_args *uap)
2560 struct nlookupdata nd;
2565 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2566 /* XXX Add NLC flag indicating modifying operation? */
2568 error = nlookup(&nd);
2570 error = ncp_writechk(&nd.nl_nch);
2572 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2575 error = setfflags(vp, uap->flags);
2582 * lchflags(char *path, int flags)
2584 * Change flags of a file given a path name, but don't follow symlinks.
2588 sys_lchflags(struct lchflags_args *uap)
2590 struct nlookupdata nd;
2595 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2596 /* XXX Add NLC flag indicating modifying operation? */
2598 error = nlookup(&nd);
2600 error = ncp_writechk(&nd.nl_nch);
2602 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2605 error = setfflags(vp, uap->flags);
2612 * fchflags_args(int fd, int flags)
2614 * Change flags of a file given a file descriptor.
2618 sys_fchflags(struct fchflags_args *uap)
2620 struct thread *td = curthread;
2621 struct proc *p = td->td_proc;
2625 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2627 if (fp->f_nchandle.ncp)
2628 error = ncp_writechk(&fp->f_nchandle);
2630 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2636 setfmode(struct vnode *vp, int mode)
2638 struct thread *td = curthread;
2639 struct proc *p = td->td_proc;
2644 * note: vget is required for any operation that might mod the vnode
2645 * so VINACTIVE is properly cleared.
2647 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2649 vattr.va_mode = mode & ALLPERMS;
2650 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2657 kern_chmod(struct nlookupdata *nd, int mode)
2662 /* XXX Add NLC flag indicating modifying operation? */
2663 if ((error = nlookup(nd)) != 0)
2665 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2667 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2668 error = setfmode(vp, mode);
2674 * chmod_args(char *path, int mode)
2676 * Change mode of a file given path name.
2680 sys_chmod(struct chmod_args *uap)
2682 struct nlookupdata nd;
2685 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2687 error = kern_chmod(&nd, uap->mode);
2693 * lchmod_args(char *path, int mode)
2695 * Change mode of a file given path name (don't follow links.)
2699 sys_lchmod(struct lchmod_args *uap)
2701 struct nlookupdata nd;
2704 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2706 error = kern_chmod(&nd, uap->mode);
2712 * fchmod_args(int fd, int mode)
2714 * Change mode of a file given a file descriptor.
2718 sys_fchmod(struct fchmod_args *uap)
2720 struct thread *td = curthread;
2721 struct proc *p = td->td_proc;
2725 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2727 if (fp->f_nchandle.ncp)
2728 error = ncp_writechk(&fp->f_nchandle);
2730 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2736 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2738 struct thread *td = curthread;
2739 struct proc *p = td->td_proc;
2744 * note: vget is required for any operation that might mod the vnode
2745 * so VINACTIVE is properly cleared.
2747 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2751 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2758 kern_chown(struct nlookupdata *nd, int uid, int gid)
2763 /* XXX Add NLC flag indicating modifying operation? */
2764 if ((error = nlookup(nd)) != 0)
2766 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2768 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2769 error = setfown(vp, uid, gid);
2775 * chown(char *path, int uid, int gid)
2777 * Set ownership given a path name.
2780 sys_chown(struct chown_args *uap)
2782 struct nlookupdata nd;
2785 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2787 error = kern_chown(&nd, uap->uid, uap->gid);
2793 * lchown_args(char *path, int uid, int gid)
2795 * Set ownership given a path name, do not cross symlinks.
2798 sys_lchown(struct lchown_args *uap)
2800 struct nlookupdata nd;
2803 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2805 error = kern_chown(&nd, uap->uid, uap->gid);
2811 * fchown_args(int fd, int uid, int gid)
2813 * Set ownership given a file descriptor.
2817 sys_fchown(struct fchown_args *uap)
2819 struct thread *td = curthread;
2820 struct proc *p = td->td_proc;
2824 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2826 if (fp->f_nchandle.ncp)
2827 error = ncp_writechk(&fp->f_nchandle);
2829 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2835 getutimes(const struct timeval *tvp, struct timespec *tsp)
2837 struct timeval tv[2];
2841 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2844 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2845 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2851 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2853 struct thread *td = curthread;
2854 struct proc *p = td->td_proc;
2859 * note: vget is required for any operation that might mod the vnode
2860 * so VINACTIVE is properly cleared.
2862 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2864 vattr.va_atime = ts[0];
2865 vattr.va_mtime = ts[1];
2867 vattr.va_vaflags |= VA_UTIMES_NULL;
2868 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2875 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2877 struct timespec ts[2];
2881 if ((error = getutimes(tptr, ts)) != 0)
2883 /* XXX Add NLC flag indicating modifying operation? */
2884 if ((error = nlookup(nd)) != 0)
2886 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2888 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2890 error = setutimes(vp, ts, tptr == NULL);
2896 * utimes_args(char *path, struct timeval *tptr)
2898 * Set the access and modification times of a file.
2901 sys_utimes(struct utimes_args *uap)
2903 struct timeval tv[2];
2904 struct nlookupdata nd;
2908 error = copyin(uap->tptr, tv, sizeof(tv));
2912 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2914 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2920 * lutimes_args(char *path, struct timeval *tptr)
2922 * Set the access and modification times of a file.
2925 sys_lutimes(struct lutimes_args *uap)
2927 struct timeval tv[2];
2928 struct nlookupdata nd;
2932 error = copyin(uap->tptr, tv, sizeof(tv));
2936 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2938 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2944 kern_futimes(int fd, struct timeval *tptr)
2946 struct thread *td = curthread;
2947 struct proc *p = td->td_proc;
2948 struct timespec ts[2];
2952 error = getutimes(tptr, ts);
2955 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2957 if (fp->f_nchandle.ncp)
2958 error = ncp_writechk(&fp->f_nchandle);
2960 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2966 * futimes_args(int fd, struct timeval *tptr)
2968 * Set the access and modification times of a file.
2971 sys_futimes(struct futimes_args *uap)
2973 struct timeval tv[2];
2977 error = copyin(uap->tptr, tv, sizeof(tv));
2982 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2988 kern_truncate(struct nlookupdata *nd, off_t length)
2996 /* XXX Add NLC flag indicating modifying operation? */
2997 if ((error = nlookup(nd)) != 0)
2999 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3001 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3003 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
3007 if (vp->v_type == VDIR) {
3009 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
3010 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
3012 vattr.va_size = length;
3013 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3020 * truncate(char *path, int pad, off_t length)
3022 * Truncate a file given its path name.
3025 sys_truncate(struct truncate_args *uap)
3027 struct nlookupdata nd;
3030 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3032 error = kern_truncate(&nd, uap->length);
3038 kern_ftruncate(int fd, off_t length)
3040 struct thread *td = curthread;
3041 struct proc *p = td->td_proc;
3049 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3051 if (fp->f_nchandle.ncp) {
3052 error = ncp_writechk(&fp->f_nchandle);
3056 if ((fp->f_flag & FWRITE) == 0) {
3060 vp = (struct vnode *)fp->f_data;
3061 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3062 if (vp->v_type == VDIR) {
3064 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3066 vattr.va_size = length;
3067 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3076 * ftruncate_args(int fd, int pad, off_t length)
3078 * Truncate a file given a file descriptor.
3081 sys_ftruncate(struct ftruncate_args *uap)
3085 error = kern_ftruncate(uap->fd, uap->length);
3093 * Sync an open file.
3097 sys_fsync(struct fsync_args *uap)
3099 struct thread *td = curthread;
3100 struct proc *p = td->td_proc;
3106 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3108 vp = (struct vnode *)fp->f_data;
3109 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3110 if ((obj = vp->v_object) != NULL)
3111 vm_object_page_clean(obj, 0, 0, 0);
3112 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 && vp->v_mount)
3113 error = buf_fsync(vp);
3120 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3122 struct nchandle fnchd;
3123 struct nchandle tnchd;
3124 struct namecache *ncp;
3131 fromnd->nl_flags |= NLC_REFDVP;
3132 if ((error = nlookup(fromnd)) != 0)
3134 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3136 fnchd.mount = fromnd->nl_nch.mount;
3140 * unlock the source nch so we can lookup the target nch without
3141 * deadlocking. The target may or may not exist so we do not check
3142 * for a target vp like kern_mkdir() and other creation functions do.
3144 * The source and target directories are ref'd and rechecked after
3145 * everything is relocked to determine if the source or target file
3148 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3149 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3150 cache_unlock(&fromnd->nl_nch);
3152 tond->nl_flags |= NLC_CREATE | NLC_REFDVP;
3153 if ((error = nlookup(tond)) != 0) {
3157 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3161 tnchd.mount = tond->nl_nch.mount;
3165 * If the source and target are the same there is nothing to do
3167 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3174 * Mount points cannot be renamed or overwritten
3176 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3185 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3186 * may have become invalid while it was unlocked, nc_vp and nc_mount
3189 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3190 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3191 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3192 cache_lock(&fromnd->nl_nch);
3193 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3195 cache_unlock(&tond->nl_nch);
3196 cache_lock(&fromnd->nl_nch);
3197 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3198 cache_lock(&tond->nl_nch);
3199 cache_resolve(&tond->nl_nch, tond->nl_cred);
3201 fromnd->nl_flags |= NLC_NCPISLOCKED;
3204 * make sure the parent directories linkages are the same
3206 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3207 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3214 * Both the source and target must be within the same filesystem and
3215 * in the same filesystem as their parent directories within the
3216 * namecache topology.
3218 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3221 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3222 mp != tond->nl_nch.mount) {
3229 * Make sure the mount point is writable
3231 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3238 * If the target exists and either the source or target is a directory,
3239 * then both must be directories.
3241 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3244 if (tond->nl_nch.ncp->nc_vp) {
3245 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3247 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3248 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3250 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3256 * You cannot rename a source into itself or a subdirectory of itself.
3257 * We check this by travsersing the target directory upwards looking
3258 * for a match against the source.
3261 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3262 if (fromnd->nl_nch.ncp == ncp) {
3273 * Even though the namespaces are different, they may still represent
3274 * hardlinks to the same file. The filesystem might have a hard time
3275 * with this so we issue a NREMOVE of the source instead of a NRENAME
3276 * when we detect the situation.
3279 fdvp = fromnd->nl_dvp;
3280 tdvp = tond->nl_dvp;
3281 if (fdvp == NULL || tdvp == NULL) {
3283 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3284 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3287 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3288 fdvp, tdvp, tond->nl_cred);
3295 * rename_args(char *from, char *to)
3297 * Rename files. Source and destination must either both be directories,
3298 * or both not be directories. If target is a directory, it must be empty.
3301 sys_rename(struct rename_args *uap)
3303 struct nlookupdata fromnd, tond;
3306 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3308 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3310 error = kern_rename(&fromnd, &tond);
3311 nlookup_done(&tond);
3313 nlookup_done(&fromnd);
3318 kern_mkdir(struct nlookupdata *nd, int mode)
3320 struct thread *td = curthread;
3321 struct proc *p = td->td_proc;
3327 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3328 if ((error = nlookup(nd)) != 0)
3331 if (nd->nl_nch.ncp->nc_vp)
3333 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3336 vattr.va_type = VDIR;
3337 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3340 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3347 * mkdir_args(char *path, int mode)
3349 * Make a directory file.
3353 sys_mkdir(struct mkdir_args *uap)
3355 struct nlookupdata nd;
3358 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3360 error = kern_mkdir(&nd, uap->mode);
3366 kern_rmdir(struct nlookupdata *nd)
3371 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3372 if ((error = nlookup(nd)) != 0)
3376 * Do not allow directories representing mount points to be
3377 * deleted, even if empty. Check write perms on mount point
3378 * in case the vnode is aliased (aka nullfs).
3380 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3382 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3384 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3389 * rmdir_args(char *path)
3391 * Remove a directory file.
3395 sys_rmdir(struct rmdir_args *uap)
3397 struct nlookupdata nd;
3400 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3402 error = kern_rmdir(&nd);
3408 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3409 enum uio_seg direction)
3411 struct thread *td = curthread;
3412 struct proc *p = td->td_proc;
3420 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3422 if ((fp->f_flag & FREAD) == 0) {
3426 vp = (struct vnode *)fp->f_data;
3428 if (vp->v_type != VDIR) {
3432 aiov.iov_base = buf;
3433 aiov.iov_len = count;
3434 auio.uio_iov = &aiov;
3435 auio.uio_iovcnt = 1;
3436 auio.uio_rw = UIO_READ;
3437 auio.uio_segflg = direction;
3439 auio.uio_resid = count;
3440 loff = auio.uio_offset = fp->f_offset;
3441 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3442 fp->f_offset = auio.uio_offset;
3445 if (count == auio.uio_resid) {
3446 if (union_dircheckp) {
3447 error = union_dircheckp(td, &vp, fp);
3454 if ((vp->v_flag & VROOT) &&
3455 (vp->v_mount->mnt_flag & MNT_UNION)) {
3456 struct vnode *tvp = vp;
3457 vp = vp->v_mount->mnt_vnodecovered;
3468 * WARNING! *basep may not be wide enough to accomodate the
3469 * seek offset. XXX should we hack this to return the upper 32 bits
3470 * for offsets greater then 4G?
3473 *basep = (long)loff;
3475 *res = count - auio.uio_resid;
3482 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3484 * Read a block of directory entries in a file system independent format.
3487 sys_getdirentries(struct getdirentries_args *uap)
3492 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3493 &uap->sysmsg_result, UIO_USERSPACE);
3495 if (error == 0 && uap->basep)
3496 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3501 * getdents_args(int fd, char *buf, size_t count)
3504 sys_getdents(struct getdents_args *uap)
3508 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3509 &uap->sysmsg_result, UIO_USERSPACE);
3515 * umask(int newmask)
3517 * Set the mode mask for creation of filesystem nodes.
3522 sys_umask(struct umask_args *uap)
3524 struct thread *td = curthread;
3525 struct proc *p = td->td_proc;
3526 struct filedesc *fdp;
3529 uap->sysmsg_result = fdp->fd_cmask;
3530 fdp->fd_cmask = uap->newmask & ALLPERMS;
3535 * revoke(char *path)
3537 * Void all references to file by ripping underlying filesystem
3542 sys_revoke(struct revoke_args *uap)
3544 struct nlookupdata nd;
3551 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3553 error = nlookup(&nd);
3555 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3556 cred = crhold(nd.nl_cred);
3559 if (vp->v_type != VCHR && vp->v_type != VBLK)
3562 error = VOP_GETATTR(vp, &vattr);
3563 if (error == 0 && cred->cr_uid != vattr.va_uid)
3564 error = priv_check_cred(cred, PRIV_ROOT, PRISON_ROOT);
3565 if (error == 0 && count_udev(vp->v_umajor, vp->v_uminor) > 0) {
3568 VOP_REVOKE(vp, REVOKEALL);
3579 * getfh_args(char *fname, fhandle_t *fhp)
3581 * Get (NFS) file handle
3583 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
3584 * mount. This allows nullfs mounts to be explicitly exported.
3586 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
3588 * nullfs mounts of subdirectories are not safe. That is, it will
3589 * work, but you do not really have protection against access to
3590 * the related parent directories.
3593 sys_getfh(struct getfh_args *uap)
3595 struct thread *td = curthread;
3596 struct nlookupdata nd;
3603 * Must be super user
3605 if ((error = priv_check(td, PRIV_ROOT)) != 0)
3609 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3611 error = nlookup(&nd);
3613 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3614 mp = nd.nl_nch.mount;
3617 bzero(&fh, sizeof(fh));
3618 fh.fh_fsid = mp->mnt_stat.f_fsid;
3619 error = VFS_VPTOFH(vp, &fh.fh_fid);
3622 error = copyout(&fh, uap->fhp, sizeof(fh));
3628 * fhopen_args(const struct fhandle *u_fhp, int flags)
3630 * syscall for the rpc.lockd to use to translate a NFS file handle into
3631 * an open descriptor.
3633 * warning: do not remove the priv_check() call or this becomes one giant
3637 sys_fhopen(struct fhopen_args *uap)
3639 struct thread *td = curthread;
3640 struct proc *p = td->td_proc;
3645 struct vattr *vap = &vat;
3647 int fmode, mode, error, type;
3653 * Must be super user
3655 error = priv_check(td, PRIV_ROOT);
3659 fmode = FFLAGS(uap->flags);
3660 /* why not allow a non-read/write open for our lockd? */
3661 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3663 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3666 /* find the mount point */
3667 mp = vfs_getvfs(&fhp.fh_fsid);
3670 /* now give me my vnode, it gets returned to me locked */
3671 error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
3675 * from now on we have to make sure not
3676 * to forget about the vnode
3677 * any error that causes an abort must vput(vp)
3678 * just set error = err and 'goto bad;'.
3684 if (vp->v_type == VLNK) {
3688 if (vp->v_type == VSOCK) {
3693 if (fmode & (FWRITE | O_TRUNC)) {
3694 if (vp->v_type == VDIR) {
3698 error = vn_writechk(vp, NULL);
3706 error = VOP_ACCESS(vp, mode, p->p_ucred);
3710 if (fmode & O_TRUNC) {
3711 vn_unlock(vp); /* XXX */
3712 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3715 error = VOP_SETATTR(vp, vap, p->p_ucred);
3721 * VOP_OPEN needs the file pointer so it can potentially override
3724 * WARNING! no f_nchandle will be associated when fhopen()ing a
3727 if ((error = falloc(p, &nfp, &indx)) != 0)
3731 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3734 * setting f_ops this way prevents VOP_CLOSE from being
3735 * called or fdrop() releasing the vp from v_data. Since
3736 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3738 fp->f_ops = &badfileops;
3744 * The fp is given its own reference, we still have our ref and lock.
3746 * Assert that all regular files must be created with a VM object.
3748 if (vp->v_type == VREG && vp->v_object == NULL) {
3749 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3754 * The open was successful. Handle any locking requirements.
3756 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3757 lf.l_whence = SEEK_SET;
3760 if (fmode & O_EXLOCK)
3761 lf.l_type = F_WRLCK;
3763 lf.l_type = F_RDLCK;
3764 if (fmode & FNONBLOCK)
3769 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3771 * release our private reference.
3773 fsetfd(p, NULL, indx);
3778 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3779 fp->f_flag |= FHASLOCK;
3783 * Clean up. Associate the file pointer with the previously
3784 * reserved descriptor and return it.
3787 fsetfd(p, fp, indx);
3789 uap->sysmsg_result = indx;
3793 fsetfd(p, NULL, indx);
3801 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3804 sys_fhstat(struct fhstat_args *uap)
3806 struct thread *td = curthread;
3814 * Must be super user
3816 error = priv_check(td, PRIV_ROOT);
3820 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3824 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3826 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3828 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3832 error = copyout(&sb, uap->sb, sizeof(sb));
3837 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3840 sys_fhstatfs(struct fhstatfs_args *uap)
3842 struct thread *td = curthread;
3843 struct proc *p = td->td_proc;
3848 char *fullpath, *freepath;
3853 * Must be super user
3855 if ((error = priv_check(td, PRIV_ROOT)))
3858 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3861 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3864 if (p != NULL && !chroot_visible_mnt(mp, p))
3867 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3872 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3875 error = mount_path(p, mp, &fullpath, &freepath);
3878 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3879 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3880 kfree(freepath, M_TEMP);
3882 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3883 if (priv_check(td, PRIV_ROOT)) {
3884 bcopy(sp, &sb, sizeof(sb));
3885 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3888 return (copyout(sp, uap->buf, sizeof(*sp)));
3892 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
3895 sys_fhstatvfs(struct fhstatvfs_args *uap)
3897 struct thread *td = curthread;
3898 struct proc *p = td->td_proc;
3906 * Must be super user
3908 if ((error = priv_check(td, PRIV_ROOT)))
3911 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3914 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3917 if (p != NULL && !chroot_visible_mnt(mp, p))
3920 if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
3923 sp = &mp->mnt_vstat;
3925 if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
3929 if (mp->mnt_flag & MNT_RDONLY)
3930 sp->f_flag |= ST_RDONLY;
3931 if (mp->mnt_flag & MNT_NOSUID)
3932 sp->f_flag |= ST_NOSUID;
3934 return (copyout(sp, uap->buf, sizeof(*sp)));
3939 * Syscall to push extended attribute configuration information into the
3940 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3941 * a command (int cmd), and attribute name and misc data. For now, the
3942 * attribute name is left in userspace for consumption by the VFS_op.
3943 * It will probably be changed to be copied into sysspace by the
3944 * syscall in the future, once issues with various consumers of the
3945 * attribute code have raised their hands.
3947 * Currently this is used only by UFS Extended Attributes.
3950 sys_extattrctl(struct extattrctl_args *uap)
3952 struct nlookupdata nd;
3958 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3960 error = nlookup(&nd);
3962 mp = nd.nl_nch.mount;
3963 error = VFS_EXTATTRCTL(mp, uap->cmd,
3964 uap->attrname, uap->arg,
3972 * Syscall to set a named extended attribute on a file or directory.
3973 * Accepts attribute name, and a uio structure pointing to the data to set.
3974 * The uio is consumed in the style of writev(). The real work happens
3975 * in VOP_SETEXTATTR().
3978 sys_extattr_set_file(struct extattr_set_file_args *uap)
3980 char attrname[EXTATTR_MAXNAMELEN];
3981 struct iovec aiov[UIO_SMALLIOV];
3982 struct iovec *needfree;
3983 struct nlookupdata nd;
3992 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3997 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3999 error = nlookup(&nd);
4001 error = ncp_writechk(&nd.nl_nch);
4003 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4010 iovlen = uap->iovcnt * sizeof(struct iovec);
4011 if (uap->iovcnt > UIO_SMALLIOV) {
4012 if (uap->iovcnt > UIO_MAXIOV) {
4016 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4022 auio.uio_iovcnt = uap->iovcnt;
4023 auio.uio_rw = UIO_WRITE;
4024 auio.uio_segflg = UIO_USERSPACE;
4025 auio.uio_td = nd.nl_td;
4026 auio.uio_offset = 0;
4027 if ((error = copyin(uap->iovp, iov, iovlen)))
4030 for (i = 0; i < uap->iovcnt; i++) {
4031 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4035 auio.uio_resid += iov->iov_len;
4038 cnt = auio.uio_resid;
4039 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4040 cnt -= auio.uio_resid;
4041 uap->sysmsg_result = cnt;
4046 FREE(needfree, M_IOV);
4051 * Syscall to get a named extended attribute on a file or directory.
4052 * Accepts attribute name, and a uio structure pointing to a buffer for the
4053 * data. The uio is consumed in the style of readv(). The real work
4054 * happens in VOP_GETEXTATTR();
4057 sys_extattr_get_file(struct extattr_get_file_args *uap)
4059 char attrname[EXTATTR_MAXNAMELEN];
4060 struct iovec aiov[UIO_SMALLIOV];
4061 struct iovec *needfree;
4062 struct nlookupdata nd;
4071 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4076 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4078 error = nlookup(&nd);
4080 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4086 iovlen = uap->iovcnt * sizeof (struct iovec);
4088 if (uap->iovcnt > UIO_SMALLIOV) {
4089 if (uap->iovcnt > UIO_MAXIOV) {
4093 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4099 auio.uio_iovcnt = uap->iovcnt;
4100 auio.uio_rw = UIO_READ;
4101 auio.uio_segflg = UIO_USERSPACE;
4102 auio.uio_td = nd.nl_td;
4103 auio.uio_offset = 0;
4104 if ((error = copyin(uap->iovp, iov, iovlen)))
4107 for (i = 0; i < uap->iovcnt; i++) {
4108 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4112 auio.uio_resid += iov->iov_len;
4115 cnt = auio.uio_resid;
4116 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4117 cnt -= auio.uio_resid;
4118 uap->sysmsg_result = cnt;
4123 FREE(needfree, M_IOV);
4128 * Syscall to delete a named extended attribute from a file or directory.
4129 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4132 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4134 char attrname[EXTATTR_MAXNAMELEN];
4135 struct nlookupdata nd;
4139 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4144 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4146 error = nlookup(&nd);
4148 error = ncp_writechk(&nd.nl_nch);
4150 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4156 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4163 * Determine if the mount is visible to the process.
4166 chroot_visible_mnt(struct mount *mp, struct proc *p)
4168 struct nchandle nch;
4171 * Traverse from the mount point upwards. If we hit the process
4172 * root then the mount point is visible to the process.
4174 nch = mp->mnt_ncmountpt;
4176 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4177 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4180 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4181 nch = nch.mount->mnt_ncmounton;
4183 nch.ncp = nch.ncp->nc_parent;
4188 * If the mount point is not visible to the process, but the
4189 * process root is in a subdirectory of the mount, return
4192 if (p->p_fd->fd_nrdir.mount == mp)