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.131 2008/06/03 16:16:40 dillon 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>
61 #include <sys/namei.h>
62 #include <sys/nlookup.h>
63 #include <sys/dirent.h>
64 #include <sys/extattr.h>
65 #include <sys/spinlock.h>
66 #include <sys/kern_syscall.h>
67 #include <sys/objcache.h>
68 #include <sys/sysctl.h>
71 #include <sys/file2.h>
72 #include <sys/spinlock2.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
78 #include <machine/limits.h>
79 #include <machine/stdarg.h>
81 #include <vfs/union/union.h>
83 static void mount_warning(struct mount *mp, const char *ctl, ...);
84 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
85 static int checkvp_chdir (struct vnode *vn, struct thread *td);
86 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
87 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
88 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
89 static int getutimes (const struct timeval *, struct timespec *);
90 static int setfown (struct vnode *, uid_t, gid_t);
91 static int setfmode (struct vnode *, int);
92 static int setfflags (struct vnode *, int);
93 static int setutimes (struct vnode *, const struct timespec *, int);
94 static int usermount = 0; /* if 1, non-root can mount fs. */
96 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
98 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
101 * Virtual File System System Calls
105 * Mount a file system.
108 * mount_args(char *type, char *path, int flags, caddr_t data)
112 sys_mount(struct mount_args *uap)
114 struct thread *td = curthread;
115 struct proc *p = td->td_proc;
119 struct vfsconf *vfsp;
120 int error, flag = 0, flag2 = 0;
123 struct nlookupdata nd;
124 char fstypename[MFSNAMELEN];
125 struct ucred *cred = p->p_ucred;
128 if (cred->cr_prison != NULL)
130 if (usermount == 0 && (error = suser(td)))
133 * Do not allow NFS export by non-root users.
135 if (uap->flags & MNT_EXPORTED) {
141 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
144 uap->flags |= MNT_NOSUID | MNT_NODEV;
147 * Lookup the requested path and extract the nch and vnode.
149 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
151 if ((error = nlookup(&nd)) == 0) {
152 if (nd.nl_nch.ncp->nc_vp == NULL)
162 * Extract the locked+refd ncp and cleanup the nd structure
165 cache_zero(&nd.nl_nch);
168 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
175 * now we have the locked ref'd nch and unreferenced vnode.
178 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
185 * Now we have an unlocked ref'd nch and a locked ref'd vp
187 if (uap->flags & MNT_UPDATE) {
188 if ((vp->v_flag & VROOT) == 0) {
195 flag2 = mp->mnt_kern_flag;
197 * We only allow the filesystem to be reloaded if it
198 * is currently mounted read-only.
200 if ((uap->flags & MNT_RELOAD) &&
201 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
204 return (EOPNOTSUPP); /* Needs translation */
207 * Only root, or the user that did the original mount is
208 * permitted to update it.
210 if (mp->mnt_stat.f_owner != cred->cr_uid &&
211 (error = suser(td))) {
216 if (vfs_busy(mp, LK_NOWAIT)) {
221 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
227 vp->v_flag |= VMOUNT;
229 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
234 * If the user is not root, ensure that they own the directory
235 * onto which we are attempting to mount.
237 if ((error = VOP_GETATTR(vp, &va)) ||
238 (va.va_uid != cred->cr_uid && (error = suser(td)))) {
243 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
248 if (vp->v_type != VDIR) {
253 if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
258 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
263 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
264 if (!strcmp(vfsp->vfc_name, fstypename))
270 /* Only load modules for root (very important!) */
271 if ((error = suser(td)) != 0) {
276 error = linker_load_file(fstypename, &lf);
277 if (error || lf == NULL) {
285 /* lookup again, see if the VFS was loaded */
286 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
287 if (!strcmp(vfsp->vfc_name, fstypename))
292 linker_file_unload(lf);
298 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
303 vp->v_flag |= VMOUNT;
306 * Allocate and initialize the filesystem.
308 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
309 TAILQ_INIT(&mp->mnt_nvnodelist);
310 TAILQ_INIT(&mp->mnt_reservedvnlist);
311 TAILQ_INIT(&mp->mnt_jlist);
312 mp->mnt_nvnodelistsize = 0;
313 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
314 vfs_busy(mp, LK_NOWAIT);
315 mp->mnt_op = vfsp->vfc_vfsops;
317 vfsp->vfc_refcount++;
318 mp->mnt_stat.f_type = vfsp->vfc_typenum;
319 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
320 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
321 mp->mnt_stat.f_owner = cred->cr_uid;
322 mp->mnt_iosize_max = DFLTPHYS;
326 * Set the mount level flags.
328 if (uap->flags & MNT_RDONLY)
329 mp->mnt_flag |= MNT_RDONLY;
330 else if (mp->mnt_flag & MNT_RDONLY)
331 mp->mnt_kern_flag |= MNTK_WANTRDWR;
332 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
333 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
334 MNT_NOSYMFOLLOW | MNT_IGNORE |
335 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
336 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
337 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
338 MNT_NOSYMFOLLOW | MNT_IGNORE |
339 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
341 * Mount the filesystem.
342 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
345 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
346 if (mp->mnt_flag & MNT_UPDATE) {
347 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
348 mp->mnt_flag &= ~MNT_RDONLY;
349 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
350 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
353 mp->mnt_kern_flag = flag2;
356 vp->v_flag &= ~VMOUNT;
361 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
363 * Put the new filesystem on the mount list after root. The mount
364 * point gets its own mnt_ncmountpt (unless the VFS already set one
365 * up) which represents the root of the mount. The lookup code
366 * detects the mount point going forward and checks the root of
367 * the mount going backwards.
369 * It is not necessary to invalidate or purge the vnode underneath
370 * because elements under the mount will be given their own glue
374 if (mp->mnt_ncmountpt.ncp == NULL) {
376 * allocate, then unlock, but leave the ref intact
378 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
379 cache_unlock(&mp->mnt_ncmountpt);
381 mp->mnt_ncmounton = nch; /* inherits ref */
382 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
384 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
385 vp->v_flag &= ~VMOUNT;
386 mountlist_insert(mp, MNTINS_LAST);
387 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
389 error = vfs_allocate_syncvnode(mp);
391 error = VFS_START(mp, 0);
394 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
395 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
396 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
397 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
398 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
399 vp->v_flag &= ~VMOUNT;
400 mp->mnt_vfc->vfc_refcount--;
410 * Scan all active processes to see if any of them have a current
411 * or root directory onto which the new filesystem has just been
412 * mounted. If so, replace them with the new mount point.
414 * The passed ncp is ref'd and locked (from the mount code) and
415 * must be associated with the vnode representing the root of the
418 struct checkdirs_info {
419 struct nchandle old_nch;
420 struct nchandle new_nch;
421 struct vnode *old_vp;
422 struct vnode *new_vp;
425 static int checkdirs_callback(struct proc *p, void *data);
428 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
430 struct checkdirs_info info;
436 * If the old mount point's vnode has a usecount of 1, it is not
437 * being held as a descriptor anywhere.
439 olddp = old_nch->ncp->nc_vp;
440 if (olddp == NULL || olddp->v_sysref.refcnt == 1)
444 * Force the root vnode of the new mount point to be resolved
445 * so we can update any matching processes.
448 if (VFS_ROOT(mp, &newdp))
449 panic("mount: lost mount");
450 cache_setunresolved(new_nch);
451 cache_setvp(new_nch, newdp);
454 * Special handling of the root node
456 if (rootvnode == olddp) {
458 vfs_cache_setroot(newdp, cache_hold(new_nch));
462 * Pass newdp separately so the callback does not have to access
463 * it via new_nch->ncp->nc_vp.
465 info.old_nch = *old_nch;
466 info.new_nch = *new_nch;
468 allproc_scan(checkdirs_callback, &info);
473 * NOTE: callback is not MP safe because the scanned process's filedesc
474 * structure can be ripped out from under us, amoung other things.
477 checkdirs_callback(struct proc *p, void *data)
479 struct checkdirs_info *info = data;
480 struct filedesc *fdp;
481 struct nchandle ncdrop1;
482 struct nchandle ncdrop2;
483 struct vnode *vprele1;
484 struct vnode *vprele2;
486 if ((fdp = p->p_fd) != NULL) {
487 cache_zero(&ncdrop1);
488 cache_zero(&ncdrop2);
493 * MPUNSAFE - XXX fdp can be pulled out from under a
496 * A shared filedesc is ok, we don't have to copy it
497 * because we are making this change globally.
499 spin_lock_wr(&fdp->fd_spin);
500 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
501 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
502 vprele1 = fdp->fd_cdir;
504 fdp->fd_cdir = info->new_vp;
505 ncdrop1 = fdp->fd_ncdir;
506 cache_copy(&info->new_nch, &fdp->fd_ncdir);
508 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
509 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
510 vprele2 = fdp->fd_rdir;
512 fdp->fd_rdir = info->new_vp;
513 ncdrop2 = fdp->fd_nrdir;
514 cache_copy(&info->new_nch, &fdp->fd_nrdir);
516 spin_unlock_wr(&fdp->fd_spin);
518 cache_drop(&ncdrop1);
520 cache_drop(&ncdrop2);
530 * Unmount a file system.
532 * Note: unmount takes a path to the vnode mounted on as argument,
533 * not special file (as before).
536 * umount_args(char *path, int flags)
540 sys_unmount(struct unmount_args *uap)
542 struct thread *td = curthread;
543 struct proc *p = td->td_proc;
544 struct mount *mp = NULL;
546 struct nlookupdata nd;
549 if (p->p_ucred->cr_prison != NULL)
551 if (usermount == 0 && (error = suser(td)))
554 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
556 error = nlookup(&nd);
560 mp = nd.nl_nch.mount;
563 * Only root, or the user that did the original mount is
564 * permitted to unmount this filesystem.
566 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
571 * Don't allow unmounting the root file system.
573 if (mp->mnt_flag & MNT_ROOTFS) {
579 * Must be the root of the filesystem
581 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
590 return (dounmount(mp, uap->flags));
594 * Do the actual file system unmount.
597 dounmount_interlock(struct mount *mp)
599 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
601 mp->mnt_kern_flag |= MNTK_UNMOUNT;
606 dounmount(struct mount *mp, int flags)
608 struct namecache *ncp;
617 * Exclusive access for unmounting purposes
619 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
623 * Allow filesystems to detect that a forced unmount is in progress.
625 if (flags & MNT_FORCE)
626 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
627 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
628 error = lockmgr(&mp->mnt_lock, lflags);
630 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
631 if (mp->mnt_kern_flag & MNTK_MWAIT)
636 if (mp->mnt_flag & MNT_EXPUBLIC)
637 vfs_setpublicfs(NULL, NULL, NULL);
639 vfs_msync(mp, MNT_WAIT);
640 async_flag = mp->mnt_flag & MNT_ASYNC;
641 mp->mnt_flag &=~ MNT_ASYNC;
644 * If this filesystem isn't aliasing other filesystems,
645 * try to invalidate any remaining namecache entries and
646 * check the count afterwords.
648 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
649 cache_lock(&mp->mnt_ncmountpt);
650 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
651 cache_unlock(&mp->mnt_ncmountpt);
653 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
654 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
656 if ((flags & MNT_FORCE) == 0) {
658 mount_warning(mp, "Cannot unmount: "
664 mount_warning(mp, "Forced unmount: "
675 * nchandle records ref the mount structure. Expect a count of 1
676 * (our mount->mnt_ncmountpt).
678 if (mp->mnt_refs != 1) {
679 if ((flags & MNT_FORCE) == 0) {
680 mount_warning(mp, "Cannot unmount: "
681 "%d process references still "
682 "present", mp->mnt_refs);
685 mount_warning(mp, "Forced unmount: "
686 "%d process references still "
687 "present", mp->mnt_refs);
693 * Decomission our special mnt_syncer vnode. This also stops
694 * the vnlru code. If we are unable to unmount we recommission
698 if ((vp = mp->mnt_syncer) != NULL) {
699 mp->mnt_syncer = NULL;
702 if (((mp->mnt_flag & MNT_RDONLY) ||
703 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
704 (flags & MNT_FORCE)) {
705 error = VFS_UNMOUNT(mp, flags);
709 if (mp->mnt_syncer == NULL)
710 vfs_allocate_syncvnode(mp);
711 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
712 mp->mnt_flag |= async_flag;
713 lockmgr(&mp->mnt_lock, LK_RELEASE);
714 if (mp->mnt_kern_flag & MNTK_MWAIT)
719 * Clean up any journals still associated with the mount after
720 * filesystem activity has ceased.
722 journal_remove_all_journals(mp,
723 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
725 mountlist_remove(mp);
728 * Remove any installed vnode ops here so the individual VFSs don't
731 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
732 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
733 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
734 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
735 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
737 if (mp->mnt_ncmountpt.ncp != NULL) {
738 nch = mp->mnt_ncmountpt;
739 cache_zero(&mp->mnt_ncmountpt);
740 cache_clrmountpt(&nch);
743 if (mp->mnt_ncmounton.ncp != NULL) {
744 nch = mp->mnt_ncmounton;
745 cache_zero(&mp->mnt_ncmounton);
746 cache_clrmountpt(&nch);
750 mp->mnt_vfc->vfc_refcount--;
751 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
752 panic("unmount: dangling vnode");
753 lockmgr(&mp->mnt_lock, LK_RELEASE);
754 if (mp->mnt_kern_flag & MNTK_MWAIT)
763 mount_warning(struct mount *mp, const char *ctl, ...)
770 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
771 kprintf("unmount(%s): ", ptr);
776 kprintf("unmount(%p", mp);
777 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
778 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
787 * Shim cache_fullpath() to handle the case where a process is chrooted into
788 * a subdirectory of a mount. In this case if the root mount matches the
789 * process root directory's mount we have to specify the process's root
790 * directory instead of the mount point, because the mount point might
791 * be above the root directory.
795 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
797 struct nchandle *nch;
799 if (p && p->p_fd->fd_nrdir.mount == mp)
800 nch = &p->p_fd->fd_nrdir;
802 nch = &mp->mnt_ncmountpt;
803 return(cache_fullpath(p, nch, rb, fb));
807 * Sync each mounted filesystem.
811 static int syncprt = 0;
812 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
815 static int sync_callback(struct mount *mp, void *data);
819 sys_sync(struct sync_args *uap)
821 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
824 * print out buffer pool stat information on each sync() call.
834 sync_callback(struct mount *mp, void *data __unused)
838 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
839 asyncflag = mp->mnt_flag & MNT_ASYNC;
840 mp->mnt_flag &= ~MNT_ASYNC;
841 vfs_msync(mp, MNT_NOWAIT);
842 VFS_SYNC(mp, MNT_NOWAIT);
843 mp->mnt_flag |= asyncflag;
848 /* XXX PRISON: could be per prison flag */
849 static int prison_quotas;
851 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
855 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
857 * Change filesystem quotas.
861 sys_quotactl(struct quotactl_args *uap)
863 struct nlookupdata nd;
871 if (p->p_ucred->cr_prison && !prison_quotas)
874 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
876 error = nlookup(&nd);
878 mp = nd.nl_nch.mount;
879 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
880 uap->arg, nd.nl_cred);
887 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
888 * void *buf, int buflen)
890 * This function operates on a mount point and executes the specified
891 * operation using the specified control data, and possibly returns data.
893 * The actual number of bytes stored in the result buffer is returned, 0
894 * if none, otherwise an error is returned.
898 sys_mountctl(struct mountctl_args *uap)
900 struct thread *td = curthread;
901 struct proc *p = td->td_proc;
909 * Sanity and permissions checks. We must be root.
912 if (p->p_ucred->cr_prison != NULL)
914 if ((error = suser(td)) != 0)
918 * Argument length checks
920 if (uap->ctllen < 0 || uap->ctllen > 1024)
922 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
924 if (uap->path == NULL)
928 * Allocate the necessary buffers and copyin data
930 path = objcache_get(namei_oc, M_WAITOK);
931 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
936 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
937 error = copyin(uap->ctl, ctl, uap->ctllen);
942 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
945 * Validate the descriptor
948 fp = holdfp(p->p_fd, uap->fd, -1);
958 * Execute the internal kernel function and clean up.
960 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
963 if (error == 0 && uap->sysmsg_result > 0)
964 error = copyout(buf, uap->buf, uap->sysmsg_result);
967 objcache_put(namei_oc, path);
976 * Execute a mount control operation by resolving the path to a mount point
977 * and calling vop_mountctl().
980 kern_mountctl(const char *path, int op, struct file *fp,
981 const void *ctl, int ctllen,
982 void *buf, int buflen, int *res)
986 struct nlookupdata nd;
991 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
993 error = nlookup(&nd);
995 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1003 * Must be the root of the filesystem
1005 if ((vp->v_flag & VROOT) == 0) {
1009 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1016 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1018 struct thread *td = curthread;
1019 struct proc *p = td->td_proc;
1022 char *fullpath, *freepath;
1025 if ((error = nlookup(nd)) != 0)
1027 mp = nd->nl_nch.mount;
1029 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1032 error = mount_path(p, mp, &fullpath, &freepath);
1035 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1036 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1037 kfree(freepath, M_TEMP);
1039 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1040 bcopy(sp, buf, sizeof(*buf));
1041 /* Only root should have access to the fsid's. */
1043 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1048 * statfs_args(char *path, struct statfs *buf)
1050 * Get filesystem statistics.
1053 sys_statfs(struct statfs_args *uap)
1055 struct nlookupdata nd;
1059 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1061 error = kern_statfs(&nd, &buf);
1064 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1069 kern_fstatfs(int fd, struct statfs *buf)
1071 struct thread *td = curthread;
1072 struct proc *p = td->td_proc;
1076 char *fullpath, *freepath;
1080 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1082 mp = ((struct vnode *)fp->f_data)->v_mount;
1087 if (fp->f_cred == NULL) {
1092 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1095 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1097 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1098 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1099 kfree(freepath, M_TEMP);
1101 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1102 bcopy(sp, buf, sizeof(*buf));
1104 /* Only root should have access to the fsid's. */
1106 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1114 * fstatfs_args(int fd, struct statfs *buf)
1116 * Get filesystem statistics.
1119 sys_fstatfs(struct fstatfs_args *uap)
1124 error = kern_fstatfs(uap->fd, &buf);
1127 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1132 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1138 if ((error = nlookup(nd)) != 0)
1140 mp = nd->nl_nch.mount;
1141 sp = &mp->mnt_vstat;
1142 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1146 if (mp->mnt_flag & MNT_RDONLY)
1147 sp->f_flag |= ST_RDONLY;
1148 if (mp->mnt_flag & MNT_NOSUID)
1149 sp->f_flag |= ST_NOSUID;
1150 bcopy(sp, buf, sizeof(*buf));
1155 * statfs_args(char *path, struct statfs *buf)
1157 * Get filesystem statistics.
1160 sys_statvfs(struct statvfs_args *uap)
1162 struct nlookupdata nd;
1166 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1168 error = kern_statvfs(&nd, &buf);
1171 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1176 kern_fstatvfs(int fd, struct statvfs *buf)
1178 struct thread *td = curthread;
1179 struct proc *p = td->td_proc;
1186 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1188 mp = ((struct vnode *)fp->f_data)->v_mount;
1193 if (fp->f_cred == NULL) {
1197 sp = &mp->mnt_vstat;
1198 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1202 if (mp->mnt_flag & MNT_RDONLY)
1203 sp->f_flag |= ST_RDONLY;
1204 if (mp->mnt_flag & MNT_NOSUID)
1205 sp->f_flag |= ST_NOSUID;
1207 bcopy(sp, buf, sizeof(*buf));
1215 * fstatfs_args(int fd, struct statfs *buf)
1217 * Get filesystem statistics.
1220 sys_fstatvfs(struct fstatvfs_args *uap)
1225 error = kern_fstatvfs(uap->fd, &buf);
1228 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1233 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1235 * Get statistics on all filesystems.
1238 struct getfsstat_info {
1239 struct statfs *sfsp;
1247 static int getfsstat_callback(struct mount *, void *);
1251 sys_getfsstat(struct getfsstat_args *uap)
1253 struct thread *td = curthread;
1254 struct proc *p = td->td_proc;
1255 struct getfsstat_info info;
1257 bzero(&info, sizeof(info));
1259 info.maxcount = uap->bufsize / sizeof(struct statfs);
1260 info.sfsp = uap->buf;
1262 info.flags = uap->flags;
1265 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1266 if (info.sfsp && info.count > info.maxcount)
1267 uap->sysmsg_result = info.maxcount;
1269 uap->sysmsg_result = info.count;
1270 return (info.error);
1274 getfsstat_callback(struct mount *mp, void *data)
1276 struct getfsstat_info *info = data;
1282 if (info->sfsp && info->count < info->maxcount) {
1283 if (info->p && !chroot_visible_mnt(mp, info->p))
1288 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1289 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1290 * overrides MNT_WAIT.
1292 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1293 (info->flags & MNT_WAIT)) &&
1294 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1297 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1299 error = mount_path(info->p, mp, &fullpath, &freepath);
1301 info->error = error;
1304 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1305 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1306 kfree(freepath, M_TEMP);
1308 error = copyout(sp, info->sfsp, sizeof(*sp));
1310 info->error = error;
1320 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1321 long bufsize, int flags)
1323 * Get statistics on all filesystems.
1326 struct getvfsstat_info {
1327 struct statfs *sfsp;
1328 struct statvfs *vsfsp;
1336 static int getvfsstat_callback(struct mount *, void *);
1340 sys_getvfsstat(struct getvfsstat_args *uap)
1342 struct thread *td = curthread;
1343 struct proc *p = td->td_proc;
1344 struct getvfsstat_info info;
1346 bzero(&info, sizeof(info));
1348 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1349 info.sfsp = uap->buf;
1350 info.vsfsp = uap->vbuf;
1352 info.flags = uap->flags;
1355 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1356 if (info.vsfsp && info.count > info.maxcount)
1357 uap->sysmsg_result = info.maxcount;
1359 uap->sysmsg_result = info.count;
1360 return (info.error);
1364 getvfsstat_callback(struct mount *mp, void *data)
1366 struct getvfsstat_info *info = data;
1368 struct statvfs *vsp;
1373 if (info->vsfsp && info->count < info->maxcount) {
1374 if (info->p && !chroot_visible_mnt(mp, info->p))
1377 vsp = &mp->mnt_vstat;
1380 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1381 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1382 * overrides MNT_WAIT.
1384 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1385 (info->flags & MNT_WAIT)) &&
1386 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1389 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1391 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1392 (info->flags & MNT_WAIT)) &&
1393 (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1397 if (mp->mnt_flag & MNT_RDONLY)
1398 vsp->f_flag |= ST_RDONLY;
1399 if (mp->mnt_flag & MNT_NOSUID)
1400 vsp->f_flag |= ST_NOSUID;
1402 error = mount_path(info->p, mp, &fullpath, &freepath);
1404 info->error = error;
1407 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1408 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1409 kfree(freepath, M_TEMP);
1411 error = copyout(sp, info->sfsp, sizeof(*sp));
1413 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1415 info->error = error;
1427 * fchdir_args(int fd)
1429 * Change current working directory to a given file descriptor.
1433 sys_fchdir(struct fchdir_args *uap)
1435 struct thread *td = curthread;
1436 struct proc *p = td->td_proc;
1437 struct filedesc *fdp = p->p_fd;
1438 struct vnode *vp, *ovp;
1441 struct nchandle nch, onch, tnch;
1444 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1446 vp = (struct vnode *)fp->f_data;
1448 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1449 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1452 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1458 cache_copy(&fp->f_nchandle, &nch);
1461 * If the ncp has become a mount point, traverse through
1465 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1466 (mp = cache_findmount(&nch)) != NULL
1468 error = nlookup_mp(mp, &tnch);
1470 cache_unlock(&tnch); /* leave ref intact */
1472 vp = tnch.ncp->nc_vp;
1473 error = vget(vp, LK_SHARED);
1474 KKASSERT(error == 0);
1481 onch = fdp->fd_ncdir;
1482 vn_unlock(vp); /* leave ref intact */
1484 fdp->fd_ncdir = nch;
1496 kern_chdir(struct nlookupdata *nd)
1498 struct thread *td = curthread;
1499 struct proc *p = td->td_proc;
1500 struct filedesc *fdp = p->p_fd;
1501 struct vnode *vp, *ovp;
1502 struct nchandle onch;
1505 if ((error = nlookup(nd)) != 0)
1507 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1509 if ((error = vget(vp, LK_SHARED)) != 0)
1512 error = checkvp_chdir(vp, td);
1516 onch = fdp->fd_ncdir;
1517 cache_unlock(&nd->nl_nch); /* leave reference intact */
1518 fdp->fd_ncdir = nd->nl_nch;
1522 cache_zero(&nd->nl_nch);
1530 * chdir_args(char *path)
1532 * Change current working directory (``.'').
1535 sys_chdir(struct chdir_args *uap)
1537 struct nlookupdata nd;
1540 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1542 error = kern_chdir(&nd);
1548 * Helper function for raised chroot(2) security function: Refuse if
1549 * any filedescriptors are open directories.
1552 chroot_refuse_vdir_fds(struct filedesc *fdp)
1559 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1560 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1562 vp = (struct vnode *)fp->f_data;
1563 if (vp->v_type != VDIR) {
1574 * This sysctl determines if we will allow a process to chroot(2) if it
1575 * has a directory open:
1576 * 0: disallowed for all processes.
1577 * 1: allowed for processes that were not already chroot(2)'ed.
1578 * 2: allowed for all processes.
1581 static int chroot_allow_open_directories = 1;
1583 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1584 &chroot_allow_open_directories, 0, "");
1587 * chroot to the specified namecache entry. We obtain the vp from the
1588 * namecache data. The passed ncp must be locked and referenced and will
1589 * remain locked and referenced on return.
1592 kern_chroot(struct nchandle *nch)
1594 struct thread *td = curthread;
1595 struct proc *p = td->td_proc;
1596 struct filedesc *fdp = p->p_fd;
1601 * Only root can chroot
1603 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1607 * Disallow open directory descriptors (fchdir() breakouts).
1609 if (chroot_allow_open_directories == 0 ||
1610 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1611 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1614 if ((vp = nch->ncp->nc_vp) == NULL)
1617 if ((error = vget(vp, LK_SHARED)) != 0)
1621 * Check the validity of vp as a directory to change to and
1622 * associate it with rdir/jdir.
1624 error = checkvp_chdir(vp, td);
1625 vn_unlock(vp); /* leave reference intact */
1627 vrele(fdp->fd_rdir);
1628 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1629 cache_drop(&fdp->fd_nrdir);
1630 cache_copy(nch, &fdp->fd_nrdir);
1631 if (fdp->fd_jdir == NULL) {
1634 cache_copy(nch, &fdp->fd_njdir);
1643 * chroot_args(char *path)
1645 * Change notion of root (``/'') directory.
1649 sys_chroot(struct chroot_args *uap)
1651 struct thread *td = curthread;
1652 struct nlookupdata nd;
1655 KKASSERT(td->td_proc);
1656 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1661 error = nlookup(&nd);
1663 error = kern_chroot(&nd.nl_nch);
1669 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1670 * determine whether it is legal to chdir to the vnode. The vnode's state
1671 * is not changed by this call.
1674 checkvp_chdir(struct vnode *vp, struct thread *td)
1678 if (vp->v_type != VDIR)
1681 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1686 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1688 struct thread *td = curthread;
1689 struct proc *p = td->td_proc;
1690 struct lwp *lp = td->td_lwp;
1691 struct filedesc *fdp = p->p_fd;
1696 int type, indx, error;
1699 if ((oflags & O_ACCMODE) == O_ACCMODE)
1701 flags = FFLAGS(oflags);
1702 error = falloc(p, &nfp, NULL);
1706 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1709 * XXX p_dupfd is a real mess. It allows a device to return a
1710 * file descriptor to be duplicated rather then doing the open
1716 * Call vn_open() to do the lookup and assign the vnode to the
1717 * file pointer. vn_open() does not change the ref count on fp
1718 * and the vnode, on success, will be inherited by the file pointer
1721 nd->nl_flags |= NLC_LOCKVP;
1722 error = vn_open(nd, fp, flags, cmode);
1726 * handle special fdopen() case. bleh. dupfdopen() is
1727 * responsible for dropping the old contents of ofiles[indx]
1730 * Note that fsetfd() will add a ref to fp which represents
1731 * the fd_files[] assignment. We must still drop our
1734 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1735 if (fdalloc(p, 0, &indx) == 0) {
1736 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1739 fdrop(fp); /* our ref */
1742 fsetfd(p, NULL, indx);
1745 fdrop(fp); /* our ref */
1746 if (error == ERESTART)
1752 * ref the vnode for ourselves so it can't be ripped out from under
1753 * is. XXX need an ND flag to request that the vnode be returned
1756 * Reserve a file descriptor but do not assign it until the open
1759 vp = (struct vnode *)fp->f_data;
1761 if ((error = fdalloc(p, 0, &indx)) != 0) {
1768 * If no error occurs the vp will have been assigned to the file
1773 if (flags & (O_EXLOCK | O_SHLOCK)) {
1774 lf.l_whence = SEEK_SET;
1777 if (flags & O_EXLOCK)
1778 lf.l_type = F_WRLCK;
1780 lf.l_type = F_RDLCK;
1781 if (flags & FNONBLOCK)
1786 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1788 * lock request failed. Clean up the reserved
1792 fsetfd(p, NULL, indx);
1796 fp->f_flag |= FHASLOCK;
1800 * Assert that all regular file vnodes were created with a object.
1802 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1803 ("open: regular file has no backing object after vn_open"));
1809 * release our private reference, leaving the one associated with the
1810 * descriptor table intact.
1812 fsetfd(p, fp, indx);
1819 * open_args(char *path, int flags, int mode)
1821 * Check permissions, allocate an open file structure,
1822 * and call the device open routine if any.
1825 sys_open(struct open_args *uap)
1827 struct nlookupdata nd;
1830 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1832 error = kern_open(&nd, uap->flags,
1833 uap->mode, &uap->sysmsg_result);
1840 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1842 struct thread *td = curthread;
1843 struct proc *p = td->td_proc;
1851 switch (mode & S_IFMT) {
1857 error = suser_cred(p->p_ucred, PRISON_ROOT);
1864 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1865 if ((error = nlookup(nd)) != 0)
1867 if (nd->nl_nch.ncp->nc_vp)
1869 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1873 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1874 vattr.va_rmajor = rmajor;
1875 vattr.va_rminor = rminor;
1878 switch (mode & S_IFMT) {
1879 case S_IFMT: /* used by badsect to flag bad sectors */
1880 vattr.va_type = VBAD;
1883 vattr.va_type = VCHR;
1886 vattr.va_type = VBLK;
1897 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1898 nd->nl_cred, NAMEI_CREATE);
1901 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1902 &vp, nd->nl_cred, &vattr);
1911 * mknod_args(char *path, int mode, int dev)
1913 * Create a special file.
1916 sys_mknod(struct mknod_args *uap)
1918 struct nlookupdata nd;
1921 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1923 error = kern_mknod(&nd, uap->mode,
1924 umajor(uap->dev), uminor(uap->dev));
1931 kern_mkfifo(struct nlookupdata *nd, int mode)
1933 struct thread *td = curthread;
1934 struct proc *p = td->td_proc;
1941 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1942 if ((error = nlookup(nd)) != 0)
1944 if (nd->nl_nch.ncp->nc_vp)
1946 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1950 vattr.va_type = VFIFO;
1951 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1953 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
1960 * mkfifo_args(char *path, int mode)
1962 * Create a named pipe.
1965 sys_mkfifo(struct mkfifo_args *uap)
1967 struct nlookupdata nd;
1970 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1972 error = kern_mkfifo(&nd, uap->mode);
1977 static int hardlink_check_uid = 0;
1978 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1979 &hardlink_check_uid, 0,
1980 "Unprivileged processes cannot create hard links to files owned by other "
1982 static int hardlink_check_gid = 0;
1983 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1984 &hardlink_check_gid, 0,
1985 "Unprivileged processes cannot create hard links to files owned by other "
1989 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1995 * Shortcut if disabled
1997 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2001 * root cred can always hardlink
2003 if (suser_cred(cred, PRISON_ROOT) == 0)
2007 * Otherwise only if the originating file is owned by the
2008 * same user or group. Note that any group is allowed if
2009 * the file is owned by the caller.
2011 error = VOP_GETATTR(vp, &va);
2015 if (hardlink_check_uid) {
2016 if (cred->cr_uid != va.va_uid)
2020 if (hardlink_check_gid) {
2021 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2029 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2031 struct thread *td = curthread;
2036 * Lookup the source and obtained a locked vnode.
2038 * XXX relookup on vget failure / race ?
2041 if ((error = nlookup(nd)) != 0)
2043 vp = nd->nl_nch.ncp->nc_vp;
2044 KKASSERT(vp != NULL);
2045 if (vp->v_type == VDIR)
2046 return (EPERM); /* POSIX */
2047 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2049 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2053 * Unlock the source so we can lookup the target without deadlocking
2054 * (XXX vp is locked already, possible other deadlock?). The target
2057 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2058 nd->nl_flags &= ~NLC_NCPISLOCKED;
2059 cache_unlock(&nd->nl_nch);
2061 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2062 if ((error = nlookup(linknd)) != 0) {
2066 if (linknd->nl_nch.ncp->nc_vp) {
2072 * Finally run the new API VOP.
2074 error = can_hardlink(vp, td, td->td_proc->p_ucred);
2076 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2077 vp, linknd->nl_cred);
2084 * link_args(char *path, char *link)
2086 * Make a hard file link.
2089 sys_link(struct link_args *uap)
2091 struct nlookupdata nd, linknd;
2094 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2096 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2098 error = kern_link(&nd, &linknd);
2099 nlookup_done(&linknd);
2106 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2114 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2115 if ((error = nlookup(nd)) != 0)
2117 if (nd->nl_nch.ncp->nc_vp)
2119 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2123 vattr.va_mode = mode;
2124 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2131 * symlink(char *path, char *link)
2133 * Make a symbolic link.
2136 sys_symlink(struct symlink_args *uap)
2138 struct thread *td = curthread;
2139 struct nlookupdata nd;
2144 path = objcache_get(namei_oc, M_WAITOK);
2145 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2147 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2149 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2150 error = kern_symlink(&nd, path, mode);
2154 objcache_put(namei_oc, path);
2159 * undelete_args(char *path)
2161 * Delete a whiteout from the filesystem.
2165 sys_undelete(struct undelete_args *uap)
2167 struct nlookupdata nd;
2170 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2172 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2174 error = nlookup(&nd);
2176 error = ncp_writechk(&nd.nl_nch);
2178 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2186 kern_unlink(struct nlookupdata *nd)
2191 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2192 if ((error = nlookup(nd)) != 0)
2194 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2196 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2201 * unlink_args(char *path)
2203 * Delete a name from the filesystem.
2206 sys_unlink(struct unlink_args *uap)
2208 struct nlookupdata nd;
2211 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2213 error = kern_unlink(&nd);
2219 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2221 struct thread *td = curthread;
2222 struct proc *p = td->td_proc;
2229 fp = holdfp(p->p_fd, fd, -1);
2232 if (fp->f_type != DTYPE_VNODE) {
2236 vp = (struct vnode *)fp->f_data;
2240 new_offset = fp->f_offset + offset;
2244 error = VOP_GETATTR(vp, &vattr);
2245 new_offset = offset + vattr.va_size;
2248 new_offset = offset;
2258 * Validate the seek position. Negative offsets are not allowed
2259 * for regular files, block specials, or directories.
2262 if (new_offset < 0 &&
2263 (vp->v_type == VREG || vp->v_type == VDIR ||
2264 vp->v_type == VCHR || vp->v_type == VBLK)) {
2267 fp->f_offset = new_offset;
2270 *res = fp->f_offset;
2277 * lseek_args(int fd, int pad, off_t offset, int whence)
2279 * Reposition read/write file offset.
2282 sys_lseek(struct lseek_args *uap)
2286 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2287 &uap->sysmsg_offset);
2293 kern_access(struct nlookupdata *nd, int aflags)
2298 if ((error = nlookup(nd)) != 0)
2301 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2305 /* Flags == 0 means only check for existence. */
2314 if ((flags & VWRITE) == 0 ||
2315 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2316 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2319 * If the file handle is stale we have to re-resolve the
2320 * entry. This is a hack at the moment.
2322 if (error == ESTALE) {
2324 cache_setunresolved(&nd->nl_nch);
2325 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2338 * access_args(char *path, int flags)
2340 * Check access permissions.
2343 sys_access(struct access_args *uap)
2345 struct nlookupdata nd;
2348 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2350 error = kern_access(&nd, uap->flags);
2356 kern_stat(struct nlookupdata *nd, struct stat *st)
2362 if ((error = nlookup(nd)) != 0)
2365 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2369 if ((error = vget(vp, LK_SHARED)) != 0)
2371 error = vn_stat(vp, st, nd->nl_cred);
2374 * If the file handle is stale we have to re-resolve the entry. This
2375 * is a hack at the moment.
2377 if (error == ESTALE) {
2379 cache_setunresolved(&nd->nl_nch);
2380 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2390 * stat_args(char *path, struct stat *ub)
2392 * Get file status; this version follows links.
2395 sys_stat(struct stat_args *uap)
2397 struct nlookupdata nd;
2401 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2403 error = kern_stat(&nd, &st);
2405 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2412 * lstat_args(char *path, struct stat *ub)
2414 * Get file status; this version does not follow links.
2417 sys_lstat(struct lstat_args *uap)
2419 struct nlookupdata nd;
2423 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2425 error = kern_stat(&nd, &st);
2427 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2434 * pathconf_Args(char *path, int name)
2436 * Get configurable pathname variables.
2440 sys_pathconf(struct pathconf_args *uap)
2442 struct nlookupdata nd;
2447 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2449 error = nlookup(&nd);
2451 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2454 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2462 * kern_readlink isn't properly split yet. There is a copyin burried
2463 * in VOP_READLINK().
2466 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2468 struct thread *td = curthread;
2469 struct proc *p = td->td_proc;
2475 if ((error = nlookup(nd)) != 0)
2477 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2480 if (vp->v_type != VLNK) {
2483 aiov.iov_base = buf;
2484 aiov.iov_len = count;
2485 auio.uio_iov = &aiov;
2486 auio.uio_iovcnt = 1;
2487 auio.uio_offset = 0;
2488 auio.uio_rw = UIO_READ;
2489 auio.uio_segflg = UIO_USERSPACE;
2491 auio.uio_resid = count;
2492 error = VOP_READLINK(vp, &auio, p->p_ucred);
2495 *res = count - auio.uio_resid;
2500 * readlink_args(char *path, char *buf, int count)
2502 * Return target name of a symbolic link.
2505 sys_readlink(struct readlink_args *uap)
2507 struct nlookupdata nd;
2510 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2512 error = kern_readlink(&nd, uap->buf, uap->count,
2513 &uap->sysmsg_result);
2520 setfflags(struct vnode *vp, int flags)
2522 struct thread *td = curthread;
2523 struct proc *p = td->td_proc;
2528 * Prevent non-root users from setting flags on devices. When
2529 * a device is reused, users can retain ownership of the device
2530 * if they are allowed to set flags and programs assume that
2531 * chown can't fail when done as root.
2533 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2534 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2538 * note: vget is required for any operation that might mod the vnode
2539 * so VINACTIVE is properly cleared.
2541 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2543 vattr.va_flags = flags;
2544 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2551 * chflags(char *path, int flags)
2553 * Change flags of a file given a path name.
2557 sys_chflags(struct chflags_args *uap)
2559 struct nlookupdata nd;
2564 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2565 /* XXX Add NLC flag indicating modifying operation? */
2567 error = nlookup(&nd);
2569 error = ncp_writechk(&nd.nl_nch);
2571 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2574 error = setfflags(vp, uap->flags);
2581 * fchflags_args(int fd, int flags)
2583 * Change flags of a file given a file descriptor.
2587 sys_fchflags(struct fchflags_args *uap)
2589 struct thread *td = curthread;
2590 struct proc *p = td->td_proc;
2594 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2596 if (fp->f_nchandle.ncp)
2597 error = ncp_writechk(&fp->f_nchandle);
2599 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2605 setfmode(struct vnode *vp, int mode)
2607 struct thread *td = curthread;
2608 struct proc *p = td->td_proc;
2613 * note: vget is required for any operation that might mod the vnode
2614 * so VINACTIVE is properly cleared.
2616 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2618 vattr.va_mode = mode & ALLPERMS;
2619 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2626 kern_chmod(struct nlookupdata *nd, int mode)
2631 /* XXX Add NLC flag indicating modifying operation? */
2632 if ((error = nlookup(nd)) != 0)
2634 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2636 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2637 error = setfmode(vp, mode);
2643 * chmod_args(char *path, int mode)
2645 * Change mode of a file given path name.
2649 sys_chmod(struct chmod_args *uap)
2651 struct nlookupdata nd;
2654 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2656 error = kern_chmod(&nd, uap->mode);
2662 * lchmod_args(char *path, int mode)
2664 * Change mode of a file given path name (don't follow links.)
2668 sys_lchmod(struct lchmod_args *uap)
2670 struct nlookupdata nd;
2673 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2675 error = kern_chmod(&nd, uap->mode);
2681 * fchmod_args(int fd, int mode)
2683 * Change mode of a file given a file descriptor.
2687 sys_fchmod(struct fchmod_args *uap)
2689 struct thread *td = curthread;
2690 struct proc *p = td->td_proc;
2694 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2696 if (fp->f_nchandle.ncp)
2697 error = ncp_writechk(&fp->f_nchandle);
2699 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2705 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2707 struct thread *td = curthread;
2708 struct proc *p = td->td_proc;
2713 * note: vget is required for any operation that might mod the vnode
2714 * so VINACTIVE is properly cleared.
2716 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2720 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2727 kern_chown(struct nlookupdata *nd, int uid, int gid)
2732 /* XXX Add NLC flag indicating modifying operation? */
2733 if ((error = nlookup(nd)) != 0)
2735 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2737 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2738 error = setfown(vp, uid, gid);
2744 * chown(char *path, int uid, int gid)
2746 * Set ownership given a path name.
2749 sys_chown(struct chown_args *uap)
2751 struct nlookupdata nd;
2754 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2756 error = kern_chown(&nd, uap->uid, uap->gid);
2762 * lchown_args(char *path, int uid, int gid)
2764 * Set ownership given a path name, do not cross symlinks.
2767 sys_lchown(struct lchown_args *uap)
2769 struct nlookupdata nd;
2772 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2774 error = kern_chown(&nd, uap->uid, uap->gid);
2780 * fchown_args(int fd, int uid, int gid)
2782 * Set ownership given a file descriptor.
2786 sys_fchown(struct fchown_args *uap)
2788 struct thread *td = curthread;
2789 struct proc *p = td->td_proc;
2793 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2795 if (fp->f_nchandle.ncp)
2796 error = ncp_writechk(&fp->f_nchandle);
2798 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2804 getutimes(const struct timeval *tvp, struct timespec *tsp)
2806 struct timeval tv[2];
2810 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2813 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2814 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2820 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2822 struct thread *td = curthread;
2823 struct proc *p = td->td_proc;
2828 * note: vget is required for any operation that might mod the vnode
2829 * so VINACTIVE is properly cleared.
2831 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2833 vattr.va_atime = ts[0];
2834 vattr.va_mtime = ts[1];
2836 vattr.va_vaflags |= VA_UTIMES_NULL;
2837 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2844 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2846 struct timespec ts[2];
2850 if ((error = getutimes(tptr, ts)) != 0)
2852 /* XXX Add NLC flag indicating modifying operation? */
2853 if ((error = nlookup(nd)) != 0)
2855 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2857 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2859 error = setutimes(vp, ts, tptr == NULL);
2865 * utimes_args(char *path, struct timeval *tptr)
2867 * Set the access and modification times of a file.
2870 sys_utimes(struct utimes_args *uap)
2872 struct timeval tv[2];
2873 struct nlookupdata nd;
2877 error = copyin(uap->tptr, tv, sizeof(tv));
2881 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2883 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2889 * lutimes_args(char *path, struct timeval *tptr)
2891 * Set the access and modification times of a file.
2894 sys_lutimes(struct lutimes_args *uap)
2896 struct timeval tv[2];
2897 struct nlookupdata nd;
2901 error = copyin(uap->tptr, tv, sizeof(tv));
2905 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2907 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2913 kern_futimes(int fd, struct timeval *tptr)
2915 struct thread *td = curthread;
2916 struct proc *p = td->td_proc;
2917 struct timespec ts[2];
2921 error = getutimes(tptr, ts);
2924 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2926 if (fp->f_nchandle.ncp)
2927 error = ncp_writechk(&fp->f_nchandle);
2929 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2935 * futimes_args(int fd, struct timeval *tptr)
2937 * Set the access and modification times of a file.
2940 sys_futimes(struct futimes_args *uap)
2942 struct timeval tv[2];
2946 error = copyin(uap->tptr, tv, sizeof(tv));
2951 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2957 kern_truncate(struct nlookupdata *nd, off_t length)
2965 /* XXX Add NLC flag indicating modifying operation? */
2966 if ((error = nlookup(nd)) != 0)
2968 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2970 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2972 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2976 if (vp->v_type == VDIR) {
2978 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
2979 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2981 vattr.va_size = length;
2982 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2989 * truncate(char *path, int pad, off_t length)
2991 * Truncate a file given its path name.
2994 sys_truncate(struct truncate_args *uap)
2996 struct nlookupdata nd;
2999 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3001 error = kern_truncate(&nd, uap->length);
3007 kern_ftruncate(int fd, off_t length)
3009 struct thread *td = curthread;
3010 struct proc *p = td->td_proc;
3018 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3020 if (fp->f_nchandle.ncp) {
3021 error = ncp_writechk(&fp->f_nchandle);
3025 if ((fp->f_flag & FWRITE) == 0) {
3029 vp = (struct vnode *)fp->f_data;
3030 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3031 if (vp->v_type == VDIR) {
3033 } else if ((error = vn_writechk(vp, NULL)) == 0) {
3035 vattr.va_size = length;
3036 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3045 * ftruncate_args(int fd, int pad, off_t length)
3047 * Truncate a file given a file descriptor.
3050 sys_ftruncate(struct ftruncate_args *uap)
3054 error = kern_ftruncate(uap->fd, uap->length);
3062 * Sync an open file.
3066 sys_fsync(struct fsync_args *uap)
3068 struct thread *td = curthread;
3069 struct proc *p = td->td_proc;
3075 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
3077 vp = (struct vnode *)fp->f_data;
3078 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3079 if ((obj = vp->v_object) != NULL)
3080 vm_object_page_clean(obj, 0, 0, 0);
3081 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 && vp->v_mount)
3082 error = buf_fsync(vp);
3089 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
3091 struct nchandle fnchd;
3092 struct nchandle tnchd;
3093 struct namecache *ncp;
3100 fromnd->nl_flags |= NLC_REFDVP;
3101 if ((error = nlookup(fromnd)) != 0)
3103 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
3105 fnchd.mount = fromnd->nl_nch.mount;
3109 * unlock the source nch so we can lookup the target nch without
3110 * deadlocking. The target may or may not exist so we do not check
3111 * for a target vp like kern_mkdir() and other creation functions do.
3113 * The source and target directories are ref'd and rechecked after
3114 * everything is relocked to determine if the source or target file
3117 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3118 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
3119 cache_unlock(&fromnd->nl_nch);
3121 tond->nl_flags |= NLC_CREATE | NLC_REFDVP;
3122 if ((error = nlookup(tond)) != 0) {
3126 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3130 tnchd.mount = tond->nl_nch.mount;
3134 * If the source and target are the same there is nothing to do
3136 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3143 * Mount points cannot be renamed or overwritten
3145 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3154 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3155 * may have become invalid while it was unlocked, nc_vp and nc_mount
3158 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3159 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3160 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3161 cache_lock(&fromnd->nl_nch);
3162 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3164 cache_unlock(&tond->nl_nch);
3165 cache_lock(&fromnd->nl_nch);
3166 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3167 cache_lock(&tond->nl_nch);
3168 cache_resolve(&tond->nl_nch, tond->nl_cred);
3170 fromnd->nl_flags |= NLC_NCPISLOCKED;
3173 * make sure the parent directories linkages are the same
3175 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3176 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3183 * Both the source and target must be within the same filesystem and
3184 * in the same filesystem as their parent directories within the
3185 * namecache topology.
3187 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3190 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3191 mp != tond->nl_nch.mount) {
3198 * Make sure the mount point is writable
3200 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3207 * If the target exists and either the source or target is a directory,
3208 * then both must be directories.
3210 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3213 if (tond->nl_nch.ncp->nc_vp) {
3214 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3216 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3217 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3219 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3225 * You cannot rename a source into itself or a subdirectory of itself.
3226 * We check this by travsersing the target directory upwards looking
3227 * for a match against the source.
3230 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3231 if (fromnd->nl_nch.ncp == ncp) {
3242 * Even though the namespaces are different, they may still represent
3243 * hardlinks to the same file. The filesystem might have a hard time
3244 * with this so we issue a NREMOVE of the source instead of a NRENAME
3245 * when we detect the situation.
3248 fdvp = fromnd->nl_dvp;
3249 tdvp = tond->nl_dvp;
3250 if (fdvp == NULL || tdvp == NULL) {
3252 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3253 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3256 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3257 fdvp, tdvp, tond->nl_cred);
3264 * rename_args(char *from, char *to)
3266 * Rename files. Source and destination must either both be directories,
3267 * or both not be directories. If target is a directory, it must be empty.
3270 sys_rename(struct rename_args *uap)
3272 struct nlookupdata fromnd, tond;
3275 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3277 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3279 error = kern_rename(&fromnd, &tond);
3280 nlookup_done(&tond);
3282 nlookup_done(&fromnd);
3287 kern_mkdir(struct nlookupdata *nd, int mode)
3289 struct thread *td = curthread;
3290 struct proc *p = td->td_proc;
3296 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
3297 if ((error = nlookup(nd)) != 0)
3300 if (nd->nl_nch.ncp->nc_vp)
3302 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3305 vattr.va_type = VDIR;
3306 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3309 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
3316 * mkdir_args(char *path, int mode)
3318 * Make a directory file.
3322 sys_mkdir(struct mkdir_args *uap)
3324 struct nlookupdata nd;
3327 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3329 error = kern_mkdir(&nd, uap->mode);
3335 kern_rmdir(struct nlookupdata *nd)
3340 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
3341 if ((error = nlookup(nd)) != 0)
3345 * Do not allow directories representing mount points to be
3346 * deleted, even if empty. Check write perms on mount point
3347 * in case the vnode is aliased (aka nullfs).
3349 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3351 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3353 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
3358 * rmdir_args(char *path)
3360 * Remove a directory file.
3364 sys_rmdir(struct rmdir_args *uap)
3366 struct nlookupdata nd;
3369 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3371 error = kern_rmdir(&nd);
3377 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3378 enum uio_seg direction)
3380 struct thread *td = curthread;
3381 struct proc *p = td->td_proc;
3389 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3391 if ((fp->f_flag & FREAD) == 0) {
3395 vp = (struct vnode *)fp->f_data;
3397 if (vp->v_type != VDIR) {
3401 aiov.iov_base = buf;
3402 aiov.iov_len = count;
3403 auio.uio_iov = &aiov;
3404 auio.uio_iovcnt = 1;
3405 auio.uio_rw = UIO_READ;
3406 auio.uio_segflg = direction;
3408 auio.uio_resid = count;
3409 loff = auio.uio_offset = fp->f_offset;
3410 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3411 fp->f_offset = auio.uio_offset;
3414 if (count == auio.uio_resid) {
3415 if (union_dircheckp) {
3416 error = union_dircheckp(td, &vp, fp);
3423 if ((vp->v_flag & VROOT) &&
3424 (vp->v_mount->mnt_flag & MNT_UNION)) {
3425 struct vnode *tvp = vp;
3426 vp = vp->v_mount->mnt_vnodecovered;
3437 * WARNING! *basep may not be wide enough to accomodate the
3438 * seek offset. XXX should we hack this to return the upper 32 bits
3439 * for offsets greater then 4G?
3442 *basep = (long)loff;
3444 *res = count - auio.uio_resid;
3451 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3453 * Read a block of directory entries in a file system independent format.
3456 sys_getdirentries(struct getdirentries_args *uap)
3461 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3462 &uap->sysmsg_result, UIO_USERSPACE);
3464 if (error == 0 && uap->basep)
3465 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3470 * getdents_args(int fd, char *buf, size_t count)
3473 sys_getdents(struct getdents_args *uap)
3477 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3478 &uap->sysmsg_result, UIO_USERSPACE);
3484 * umask(int newmask)
3486 * Set the mode mask for creation of filesystem nodes.
3491 sys_umask(struct umask_args *uap)
3493 struct thread *td = curthread;
3494 struct proc *p = td->td_proc;
3495 struct filedesc *fdp;
3498 uap->sysmsg_result = fdp->fd_cmask;
3499 fdp->fd_cmask = uap->newmask & ALLPERMS;
3504 * revoke(char *path)
3506 * Void all references to file by ripping underlying filesystem
3511 sys_revoke(struct revoke_args *uap)
3513 struct nlookupdata nd;
3520 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3522 error = nlookup(&nd);
3524 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3525 cred = crhold(nd.nl_cred);
3528 if (vp->v_type != VCHR && vp->v_type != VBLK)
3531 error = VOP_GETATTR(vp, &vattr);
3532 if (error == 0 && cred->cr_uid != vattr.va_uid)
3533 error = suser_cred(cred, PRISON_ROOT);
3534 if (error == 0 && count_udev(vp->v_umajor, vp->v_uminor) > 0) {
3537 VOP_REVOKE(vp, REVOKEALL);
3548 * getfh_args(char *fname, fhandle_t *fhp)
3550 * Get (NFS) file handle
3553 sys_getfh(struct getfh_args *uap)
3555 struct thread *td = curthread;
3556 struct nlookupdata nd;
3562 * Must be super user
3564 if ((error = suser(td)) != 0)
3568 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3570 error = nlookup(&nd);
3572 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3575 bzero(&fh, sizeof(fh));
3576 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3577 error = VFS_VPTOFH(vp, &fh.fh_fid);
3580 error = copyout(&fh, uap->fhp, sizeof(fh));
3586 * fhopen_args(const struct fhandle *u_fhp, int flags)
3588 * syscall for the rpc.lockd to use to translate a NFS file handle into
3589 * an open descriptor.
3591 * warning: do not remove the suser() call or this becomes one giant
3595 sys_fhopen(struct fhopen_args *uap)
3597 struct thread *td = curthread;
3598 struct proc *p = td->td_proc;
3603 struct vattr *vap = &vat;
3605 int fmode, mode, error, type;
3611 * Must be super user
3617 fmode = FFLAGS(uap->flags);
3618 /* why not allow a non-read/write open for our lockd? */
3619 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3621 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3624 /* find the mount point */
3625 mp = vfs_getvfs(&fhp.fh_fsid);
3628 /* now give me my vnode, it gets returned to me locked */
3629 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3633 * from now on we have to make sure not
3634 * to forget about the vnode
3635 * any error that causes an abort must vput(vp)
3636 * just set error = err and 'goto bad;'.
3642 if (vp->v_type == VLNK) {
3646 if (vp->v_type == VSOCK) {
3651 if (fmode & (FWRITE | O_TRUNC)) {
3652 if (vp->v_type == VDIR) {
3656 error = vn_writechk(vp, NULL);
3664 error = VOP_ACCESS(vp, mode, p->p_ucred);
3668 if (fmode & O_TRUNC) {
3669 vn_unlock(vp); /* XXX */
3670 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3673 error = VOP_SETATTR(vp, vap, p->p_ucred);
3679 * VOP_OPEN needs the file pointer so it can potentially override
3682 * WARNING! no f_nchandle will be associated when fhopen()ing a
3685 if ((error = falloc(p, &nfp, &indx)) != 0)
3689 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3692 * setting f_ops this way prevents VOP_CLOSE from being
3693 * called or fdrop() releasing the vp from v_data. Since
3694 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3696 fp->f_ops = &badfileops;
3702 * The fp is given its own reference, we still have our ref and lock.
3704 * Assert that all regular files must be created with a VM object.
3706 if (vp->v_type == VREG && vp->v_object == NULL) {
3707 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3712 * The open was successful. Handle any locking requirements.
3714 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3715 lf.l_whence = SEEK_SET;
3718 if (fmode & O_EXLOCK)
3719 lf.l_type = F_WRLCK;
3721 lf.l_type = F_RDLCK;
3722 if (fmode & FNONBLOCK)
3727 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3729 * release our private reference.
3731 fsetfd(p, NULL, indx);
3736 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3737 fp->f_flag |= FHASLOCK;
3741 * Clean up. Associate the file pointer with the previously
3742 * reserved descriptor and return it.
3745 fsetfd(p, fp, indx);
3747 uap->sysmsg_result = indx;
3751 fsetfd(p, NULL, indx);
3759 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3762 sys_fhstat(struct fhstat_args *uap)
3764 struct thread *td = curthread;
3772 * Must be super user
3778 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3782 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3784 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3786 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3790 error = copyout(&sb, uap->sb, sizeof(sb));
3795 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3798 sys_fhstatfs(struct fhstatfs_args *uap)
3800 struct thread *td = curthread;
3801 struct proc *p = td->td_proc;
3806 char *fullpath, *freepath;
3811 * Must be super user
3813 if ((error = suser(td)))
3816 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3819 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3822 if (p != NULL && !chroot_visible_mnt(mp, p))
3825 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3830 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3833 error = mount_path(p, mp, &fullpath, &freepath);
3836 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3837 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3838 kfree(freepath, M_TEMP);
3840 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3842 bcopy(sp, &sb, sizeof(sb));
3843 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3846 return (copyout(sp, uap->buf, sizeof(*sp)));
3850 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
3853 sys_fhstatvfs(struct fhstatvfs_args *uap)
3855 struct thread *td = curthread;
3856 struct proc *p = td->td_proc;
3864 * Must be super user
3866 if ((error = suser(td)))
3869 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3872 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3875 if (p != NULL && !chroot_visible_mnt(mp, p))
3878 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3881 sp = &mp->mnt_vstat;
3883 if ((error = VFS_STATVFS(mp, sp, p->p_ucred)) != 0)
3887 if (mp->mnt_flag & MNT_RDONLY)
3888 sp->f_flag |= ST_RDONLY;
3889 if (mp->mnt_flag & MNT_NOSUID)
3890 sp->f_flag |= ST_NOSUID;
3892 return (copyout(sp, uap->buf, sizeof(*sp)));
3897 * Syscall to push extended attribute configuration information into the
3898 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3899 * a command (int cmd), and attribute name and misc data. For now, the
3900 * attribute name is left in userspace for consumption by the VFS_op.
3901 * It will probably be changed to be copied into sysspace by the
3902 * syscall in the future, once issues with various consumers of the
3903 * attribute code have raised their hands.
3905 * Currently this is used only by UFS Extended Attributes.
3908 sys_extattrctl(struct extattrctl_args *uap)
3910 struct nlookupdata nd;
3916 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3918 error = nlookup(&nd);
3920 mp = nd.nl_nch.mount;
3921 error = VFS_EXTATTRCTL(mp, uap->cmd,
3922 uap->attrname, uap->arg,
3930 * Syscall to set a named extended attribute on a file or directory.
3931 * Accepts attribute name, and a uio structure pointing to the data to set.
3932 * The uio is consumed in the style of writev(). The real work happens
3933 * in VOP_SETEXTATTR().
3936 sys_extattr_set_file(struct extattr_set_file_args *uap)
3938 char attrname[EXTATTR_MAXNAMELEN];
3939 struct iovec aiov[UIO_SMALLIOV];
3940 struct iovec *needfree;
3941 struct nlookupdata nd;
3950 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3955 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3957 error = nlookup(&nd);
3959 error = ncp_writechk(&nd.nl_nch);
3961 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3968 iovlen = uap->iovcnt * sizeof(struct iovec);
3969 if (uap->iovcnt > UIO_SMALLIOV) {
3970 if (uap->iovcnt > UIO_MAXIOV) {
3974 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3980 auio.uio_iovcnt = uap->iovcnt;
3981 auio.uio_rw = UIO_WRITE;
3982 auio.uio_segflg = UIO_USERSPACE;
3983 auio.uio_td = nd.nl_td;
3984 auio.uio_offset = 0;
3985 if ((error = copyin(uap->iovp, iov, iovlen)))
3988 for (i = 0; i < uap->iovcnt; i++) {
3989 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3993 auio.uio_resid += iov->iov_len;
3996 cnt = auio.uio_resid;
3997 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3998 cnt -= auio.uio_resid;
3999 uap->sysmsg_result = cnt;
4004 FREE(needfree, M_IOV);
4009 * Syscall to get a named extended attribute on a file or directory.
4010 * Accepts attribute name, and a uio structure pointing to a buffer for the
4011 * data. The uio is consumed in the style of readv(). The real work
4012 * happens in VOP_GETEXTATTR();
4015 sys_extattr_get_file(struct extattr_get_file_args *uap)
4017 char attrname[EXTATTR_MAXNAMELEN];
4018 struct iovec aiov[UIO_SMALLIOV];
4019 struct iovec *needfree;
4020 struct nlookupdata nd;
4029 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4034 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4036 error = nlookup(&nd);
4038 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4044 iovlen = uap->iovcnt * sizeof (struct iovec);
4046 if (uap->iovcnt > UIO_SMALLIOV) {
4047 if (uap->iovcnt > UIO_MAXIOV) {
4051 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
4057 auio.uio_iovcnt = uap->iovcnt;
4058 auio.uio_rw = UIO_READ;
4059 auio.uio_segflg = UIO_USERSPACE;
4060 auio.uio_td = nd.nl_td;
4061 auio.uio_offset = 0;
4062 if ((error = copyin(uap->iovp, iov, iovlen)))
4065 for (i = 0; i < uap->iovcnt; i++) {
4066 if (iov->iov_len > INT_MAX - auio.uio_resid) {
4070 auio.uio_resid += iov->iov_len;
4073 cnt = auio.uio_resid;
4074 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
4075 cnt -= auio.uio_resid;
4076 uap->sysmsg_result = cnt;
4081 FREE(needfree, M_IOV);
4086 * Syscall to delete a named extended attribute from a file or directory.
4087 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4090 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
4092 char attrname[EXTATTR_MAXNAMELEN];
4093 struct nlookupdata nd;
4097 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
4102 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4104 error = nlookup(&nd);
4106 error = ncp_writechk(&nd.nl_nch);
4108 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4114 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
4121 * Determine if the mount is visible to the process.
4124 chroot_visible_mnt(struct mount *mp, struct proc *p)
4126 struct nchandle nch;
4129 * Traverse from the mount point upwards. If we hit the process
4130 * root then the mount point is visible to the process.
4132 nch = mp->mnt_ncmountpt;
4134 if (nch.mount == p->p_fd->fd_nrdir.mount &&
4135 nch.ncp == p->p_fd->fd_nrdir.ncp) {
4138 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
4139 nch = nch.mount->mnt_ncmounton;
4141 nch.ncp = nch.ncp->nc_parent;
4146 * If the mount point is not visible to the process, but the
4147 * process root is in a subdirectory of the mount, return
4150 if (p->p_fd->fd_nrdir.mount == mp)