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.114 2007/02/18 07:12:19 swildner 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>
69 #include <sys/file2.h>
70 #include <sys/spinlock2.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
76 #include <machine/limits.h>
77 #include <machine/stdarg.h>
79 #include <vfs/union/union.h>
81 static void mount_warning(struct mount *mp, const char *ctl, ...);
82 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
83 static int checkvp_chdir (struct vnode *vn, struct thread *td);
84 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
85 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
86 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
87 static int getutimes (const struct timeval *, struct timespec *);
88 static int setfown (struct vnode *, uid_t, gid_t);
89 static int setfmode (struct vnode *, int);
90 static int setfflags (struct vnode *, int);
91 static int setutimes (struct vnode *, const struct timespec *, int);
92 static int usermount = 0; /* if 1, non-root can mount fs. */
94 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
96 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
99 * Virtual File System System Calls
103 * Mount a file system.
106 * mount_args(char *type, char *path, int flags, caddr_t data)
110 sys_mount(struct mount_args *uap)
112 struct thread *td = curthread;
113 struct proc *p = td->td_proc;
117 struct vfsconf *vfsp;
118 int error, flag = 0, flag2 = 0;
121 struct nlookupdata nd;
122 char fstypename[MFSNAMELEN];
123 struct ucred *cred = p->p_ucred;
126 if (cred->cr_prison != NULL)
128 if (usermount == 0 && (error = suser(td)))
131 * Do not allow NFS export by non-root users.
133 if (uap->flags & MNT_EXPORTED) {
139 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
142 uap->flags |= MNT_NOSUID | MNT_NODEV;
145 * Lookup the requested path and extract the nch and vnode.
147 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
149 if ((error = nlookup(&nd)) == 0) {
150 if (nd.nl_nch.ncp->nc_vp == NULL)
160 * Extract the locked+refd ncp and cleanup the nd structure
163 cache_zero(&nd.nl_nch);
166 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
173 * now we have the locked ref'd nch and unreferenced vnode.
176 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
183 * Now we have an unlocked ref'd nch and a locked ref'd vp
185 if (uap->flags & MNT_UPDATE) {
186 if ((vp->v_flag & VROOT) == 0) {
193 flag2 = mp->mnt_kern_flag;
195 * We only allow the filesystem to be reloaded if it
196 * is currently mounted read-only.
198 if ((uap->flags & MNT_RELOAD) &&
199 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
202 return (EOPNOTSUPP); /* Needs translation */
205 * Only root, or the user that did the original mount is
206 * permitted to update it.
208 if (mp->mnt_stat.f_owner != cred->cr_uid &&
209 (error = suser(td))) {
214 if (vfs_busy(mp, LK_NOWAIT)) {
219 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
225 vp->v_flag |= VMOUNT;
227 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
232 * If the user is not root, ensure that they own the directory
233 * onto which we are attempting to mount.
235 if ((error = VOP_GETATTR(vp, &va)) ||
236 (va.va_uid != cred->cr_uid && (error = suser(td)))) {
241 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
246 if (vp->v_type != VDIR) {
251 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
256 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
257 if (!strcmp(vfsp->vfc_name, fstypename))
263 /* Only load modules for root (very important!) */
264 if ((error = suser(td)) != 0) {
269 error = linker_load_file(fstypename, &lf);
270 if (error || lf == NULL) {
278 /* lookup again, see if the VFS was loaded */
279 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
280 if (!strcmp(vfsp->vfc_name, fstypename))
285 linker_file_unload(lf);
291 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
296 vp->v_flag |= VMOUNT;
299 * Allocate and initialize the filesystem.
301 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
302 TAILQ_INIT(&mp->mnt_nvnodelist);
303 TAILQ_INIT(&mp->mnt_reservedvnlist);
304 TAILQ_INIT(&mp->mnt_jlist);
305 mp->mnt_nvnodelistsize = 0;
306 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
307 vfs_busy(mp, LK_NOWAIT);
308 mp->mnt_op = vfsp->vfc_vfsops;
310 vfsp->vfc_refcount++;
311 mp->mnt_stat.f_type = vfsp->vfc_typenum;
312 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
313 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
314 mp->mnt_stat.f_owner = cred->cr_uid;
315 mp->mnt_iosize_max = DFLTPHYS;
319 * Set the mount level flags.
321 if (uap->flags & MNT_RDONLY)
322 mp->mnt_flag |= MNT_RDONLY;
323 else if (mp->mnt_flag & MNT_RDONLY)
324 mp->mnt_kern_flag |= MNTK_WANTRDWR;
325 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
326 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
327 MNT_NOSYMFOLLOW | MNT_IGNORE |
328 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
329 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
330 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
331 MNT_NOSYMFOLLOW | MNT_IGNORE |
332 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
334 * Mount the filesystem.
335 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
338 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
339 if (mp->mnt_flag & MNT_UPDATE) {
340 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
341 mp->mnt_flag &= ~MNT_RDONLY;
342 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
343 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
346 mp->mnt_kern_flag = flag2;
349 vp->v_flag &= ~VMOUNT;
354 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
356 * Put the new filesystem on the mount list after root. The mount
357 * point gets its own mnt_ncmountpt (unless the VFS already set one
358 * up) which represents the root of the mount. The lookup code
359 * detects the mount point going forward and checks the root of
360 * the mount going backwards.
362 * It is not necessary to invalidate or purge the vnode underneath
363 * because elements under the mount will be given their own glue
367 if (mp->mnt_ncmountpt.ncp == NULL) {
369 * allocate, then unlock, but leave the ref intact
371 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
372 cache_unlock(&mp->mnt_ncmountpt);
374 mp->mnt_ncmounton = nch; /* inherits ref */
375 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
377 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
378 vp->v_flag &= ~VMOUNT;
379 mountlist_insert(mp, MNTINS_LAST);
380 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
382 error = vfs_allocate_syncvnode(mp);
384 error = VFS_START(mp, 0);
387 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
388 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
389 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
390 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
391 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
392 vp->v_flag &= ~VMOUNT;
393 mp->mnt_vfc->vfc_refcount--;
403 * Scan all active processes to see if any of them have a current
404 * or root directory onto which the new filesystem has just been
405 * mounted. If so, replace them with the new mount point.
407 * The passed ncp is ref'd and locked (from the mount code) and
408 * must be associated with the vnode representing the root of the
411 struct checkdirs_info {
412 struct nchandle old_nch;
413 struct nchandle new_nch;
414 struct vnode *old_vp;
415 struct vnode *new_vp;
418 static int checkdirs_callback(struct proc *p, void *data);
421 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
423 struct checkdirs_info info;
429 * If the old mount point's vnode has a usecount of 1, it is not
430 * being held as a descriptor anywhere.
432 olddp = old_nch->ncp->nc_vp;
433 if (olddp == NULL || olddp->v_usecount == 1)
437 * Force the root vnode of the new mount point to be resolved
438 * so we can update any matching processes.
441 if (VFS_ROOT(mp, &newdp))
442 panic("mount: lost mount");
443 cache_setunresolved(new_nch);
444 cache_setvp(new_nch, newdp);
447 * Special handling of the root node
449 if (rootvnode == olddp) {
451 vfs_cache_setroot(newdp, cache_hold(new_nch));
455 * Pass newdp separately so the callback does not have to access
456 * it via new_nch->ncp->nc_vp.
458 info.old_nch = *old_nch;
459 info.new_nch = *new_nch;
461 allproc_scan(checkdirs_callback, &info);
466 * NOTE: callback is not MP safe because the scanned process's filedesc
467 * structure can be ripped out from under us, amoung other things.
470 checkdirs_callback(struct proc *p, void *data)
472 struct checkdirs_info *info = data;
473 struct filedesc *fdp;
474 struct nchandle ncdrop1;
475 struct nchandle ncdrop2;
476 struct vnode *vprele1;
477 struct vnode *vprele2;
479 if ((fdp = p->p_fd) != NULL) {
480 cache_zero(&ncdrop1);
481 cache_zero(&ncdrop2);
486 * MPUNSAFE - XXX fdp can be pulled out from under a
489 * A shared filedesc is ok, we don't have to copy it
490 * because we are making this change globally.
492 spin_lock_wr(&fdp->fd_spin);
493 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
494 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
495 vprele1 = fdp->fd_cdir;
497 fdp->fd_cdir = info->new_vp;
498 ncdrop1 = fdp->fd_ncdir;
499 cache_copy(&info->new_nch, &fdp->fd_ncdir);
501 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
502 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
503 vprele2 = fdp->fd_rdir;
505 fdp->fd_rdir = info->new_vp;
506 ncdrop2 = fdp->fd_nrdir;
507 cache_copy(&info->new_nch, &fdp->fd_nrdir);
509 spin_unlock_wr(&fdp->fd_spin);
511 cache_drop(&ncdrop1);
513 cache_drop(&ncdrop2);
523 * Unmount a file system.
525 * Note: unmount takes a path to the vnode mounted on as argument,
526 * not special file (as before).
529 * umount_args(char *path, int flags)
533 sys_unmount(struct unmount_args *uap)
535 struct thread *td = curthread;
536 struct proc *p = td->td_proc;
537 struct mount *mp = NULL;
539 struct nlookupdata nd;
542 if (p->p_ucred->cr_prison != NULL)
544 if (usermount == 0 && (error = suser(td)))
547 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
549 error = nlookup(&nd);
553 mp = nd.nl_nch.mount;
556 * Only root, or the user that did the original mount is
557 * permitted to unmount this filesystem.
559 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
564 * Don't allow unmounting the root file system.
566 if (mp->mnt_flag & MNT_ROOTFS) {
572 * Must be the root of the filesystem
574 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
583 return (dounmount(mp, uap->flags));
587 * Do the actual file system unmount.
590 dounmount_interlock(struct mount *mp)
592 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
594 mp->mnt_kern_flag |= MNTK_UNMOUNT;
599 dounmount(struct mount *mp, int flags)
601 struct namecache *ncp;
609 * Exclusive access for unmounting purposes
611 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
615 * Allow filesystems to detect that a forced unmount is in progress.
617 if (flags & MNT_FORCE)
618 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
619 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
620 error = lockmgr(&mp->mnt_lock, lflags);
622 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
623 if (mp->mnt_kern_flag & MNTK_MWAIT)
628 if (mp->mnt_flag & MNT_EXPUBLIC)
629 vfs_setpublicfs(NULL, NULL, NULL);
631 vfs_msync(mp, MNT_WAIT);
632 async_flag = mp->mnt_flag & MNT_ASYNC;
633 mp->mnt_flag &=~ MNT_ASYNC;
636 * If this filesystem isn't aliasing other filesystems,
637 * try to invalidate any remaining namecache entries and
638 * check the count afterwords.
640 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
641 cache_lock(&mp->mnt_ncmountpt);
642 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
643 cache_unlock(&mp->mnt_ncmountpt);
645 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
646 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
648 if ((flags & MNT_FORCE) == 0) {
650 mount_warning(mp, "Cannot unmount: "
656 mount_warning(mp, "Forced unmount: "
667 * nchandle records ref the mount structure. Expect a count of 1
668 * (our mount->mnt_ncmountpt).
670 if (mp->mnt_refs != 1) {
671 if ((flags & MNT_FORCE) == 0) {
672 mount_warning(mp, "Cannot unmount: "
673 "%d process references still "
674 "present", mp->mnt_refs);
677 mount_warning(mp, "Forced unmount: "
678 "%d process references still "
679 "present", mp->mnt_refs);
685 if (mp->mnt_syncer != NULL)
686 vrele(mp->mnt_syncer);
687 if (((mp->mnt_flag & MNT_RDONLY) ||
688 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
689 (flags & MNT_FORCE)) {
690 error = VFS_UNMOUNT(mp, flags);
694 if (mp->mnt_syncer == NULL)
695 vfs_allocate_syncvnode(mp);
696 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
697 mp->mnt_flag |= async_flag;
698 lockmgr(&mp->mnt_lock, LK_RELEASE);
699 if (mp->mnt_kern_flag & MNTK_MWAIT)
704 * Clean up any journals still associated with the mount after
705 * filesystem activity has ceased.
707 journal_remove_all_journals(mp,
708 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
710 mountlist_remove(mp);
713 * Remove any installed vnode ops here so the individual VFSs don't
716 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
717 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
718 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
719 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
720 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
722 if (mp->mnt_ncmountpt.ncp != NULL) {
723 nch = mp->mnt_ncmountpt;
724 cache_zero(&mp->mnt_ncmountpt);
725 cache_clrmountpt(&nch);
728 if (mp->mnt_ncmounton.ncp != NULL) {
729 nch = mp->mnt_ncmounton;
730 cache_zero(&mp->mnt_ncmounton);
731 cache_clrmountpt(&nch);
735 mp->mnt_vfc->vfc_refcount--;
736 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
737 panic("unmount: dangling vnode");
738 lockmgr(&mp->mnt_lock, LK_RELEASE);
739 if (mp->mnt_kern_flag & MNTK_MWAIT)
748 mount_warning(struct mount *mp, const char *ctl, ...)
755 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
756 kprintf("unmount(%s): ", ptr);
761 kprintf("unmount(%p): ", mp);
769 * Shim cache_fullpath() to handle the case where a process is chrooted into
770 * a subdirectory of a mount. In this case if the root mount matches the
771 * process root directory's mount we have to specify the process's root
772 * directory instead of the mount point, because the mount point might
773 * be above the root directory.
777 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
779 struct nchandle *nch;
781 if (p && p->p_fd->fd_nrdir.mount == mp)
782 nch = &p->p_fd->fd_nrdir;
784 nch = &mp->mnt_ncmountpt;
785 return(cache_fullpath(p, nch, rb, fb));
789 * Sync each mounted filesystem.
793 static int syncprt = 0;
794 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
797 static int sync_callback(struct mount *mp, void *data);
801 sys_sync(struct sync_args *uap)
803 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
806 * print out buffer pool stat information on each sync() call.
816 sync_callback(struct mount *mp, void *data __unused)
820 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
821 asyncflag = mp->mnt_flag & MNT_ASYNC;
822 mp->mnt_flag &= ~MNT_ASYNC;
823 vfs_msync(mp, MNT_NOWAIT);
824 VFS_SYNC(mp, MNT_NOWAIT);
825 mp->mnt_flag |= asyncflag;
830 /* XXX PRISON: could be per prison flag */
831 static int prison_quotas;
833 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
837 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
839 * Change filesystem quotas.
843 sys_quotactl(struct quotactl_args *uap)
845 struct nlookupdata nd;
853 if (p->p_ucred->cr_prison && !prison_quotas)
856 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
858 error = nlookup(&nd);
860 mp = nd.nl_nch.mount;
861 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
862 uap->arg, nd.nl_cred);
869 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
870 * void *buf, int buflen)
872 * This function operates on a mount point and executes the specified
873 * operation using the specified control data, and possibly returns data.
875 * The actual number of bytes stored in the result buffer is returned, 0
876 * if none, otherwise an error is returned.
880 sys_mountctl(struct mountctl_args *uap)
882 struct thread *td = curthread;
883 struct proc *p = td->td_proc;
891 * Sanity and permissions checks. We must be root.
894 if (p->p_ucred->cr_prison != NULL)
896 if ((error = suser(td)) != 0)
900 * Argument length checks
902 if (uap->ctllen < 0 || uap->ctllen > 1024)
904 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
906 if (uap->path == NULL)
910 * Allocate the necessary buffers and copyin data
912 path = objcache_get(namei_oc, M_WAITOK);
913 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
918 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
919 error = copyin(uap->ctl, ctl, uap->ctllen);
924 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
927 * Validate the descriptor
929 fp = holdfp(p->p_fd, uap->fd, -1);
936 * Execute the internal kernel function and clean up.
938 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
941 if (error == 0 && uap->sysmsg_result > 0)
942 error = copyout(buf, uap->buf, uap->sysmsg_result);
945 objcache_put(namei_oc, path);
954 * Execute a mount control operation by resolving the path to a mount point
955 * and calling vop_mountctl().
958 kern_mountctl(const char *path, int op, struct file *fp,
959 const void *ctl, int ctllen,
960 void *buf, int buflen, int *res)
964 struct nlookupdata nd;
969 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
971 error = nlookup(&nd);
973 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
981 * Must be the root of the filesystem
983 if ((vp->v_flag & VROOT) == 0) {
987 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
994 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
996 struct thread *td = curthread;
997 struct proc *p = td->td_proc;
1000 char *fullpath, *freepath;
1003 if ((error = nlookup(nd)) != 0)
1005 mp = nd->nl_nch.mount;
1007 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1010 error = mount_path(p, mp, &fullpath, &freepath);
1013 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1014 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1015 kfree(freepath, M_TEMP);
1017 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1018 bcopy(sp, buf, sizeof(*buf));
1019 /* Only root should have access to the fsid's. */
1021 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1026 * statfs_args(char *path, struct statfs *buf)
1028 * Get filesystem statistics.
1031 sys_statfs(struct statfs_args *uap)
1033 struct nlookupdata nd;
1037 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1039 error = kern_statfs(&nd, &buf);
1042 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1047 kern_fstatfs(int fd, struct statfs *buf)
1049 struct thread *td = curthread;
1050 struct proc *p = td->td_proc;
1054 char *fullpath, *freepath;
1058 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1060 mp = ((struct vnode *)fp->f_data)->v_mount;
1065 if (fp->f_cred == NULL) {
1070 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1073 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1075 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1076 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1077 kfree(freepath, M_TEMP);
1079 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1080 bcopy(sp, buf, sizeof(*buf));
1082 /* Only root should have access to the fsid's. */
1084 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1092 * fstatfs_args(int fd, struct statfs *buf)
1094 * Get filesystem statistics.
1097 sys_fstatfs(struct fstatfs_args *uap)
1102 error = kern_fstatfs(uap->fd, &buf);
1105 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1110 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1112 * Get statistics on all filesystems.
1115 struct getfsstat_info {
1116 struct statfs *sfsp;
1124 static int getfsstat_callback(struct mount *, void *);
1128 sys_getfsstat(struct getfsstat_args *uap)
1130 struct thread *td = curthread;
1131 struct proc *p = td->td_proc;
1132 struct getfsstat_info info;
1134 bzero(&info, sizeof(info));
1136 info.maxcount = uap->bufsize / sizeof(struct statfs);
1137 info.sfsp = uap->buf;
1139 info.flags = uap->flags;
1142 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1143 if (info.sfsp && info.count > info.maxcount)
1144 uap->sysmsg_result = info.maxcount;
1146 uap->sysmsg_result = info.count;
1147 return (info.error);
1151 getfsstat_callback(struct mount *mp, void *data)
1153 struct getfsstat_info *info = data;
1159 if (info->sfsp && info->count < info->maxcount) {
1160 if (info->p && !chroot_visible_mnt(mp, info->p))
1165 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1166 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1167 * overrides MNT_WAIT.
1169 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1170 (info->flags & MNT_WAIT)) &&
1171 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1174 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1176 error = mount_path(info->p, mp, &fullpath, &freepath);
1178 info->error = error;
1181 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1182 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1183 kfree(freepath, M_TEMP);
1185 error = copyout(sp, info->sfsp, sizeof(*sp));
1187 info->error = error;
1197 * fchdir_args(int fd)
1199 * Change current working directory to a given file descriptor.
1203 sys_fchdir(struct fchdir_args *uap)
1205 struct thread *td = curthread;
1206 struct proc *p = td->td_proc;
1207 struct filedesc *fdp = p->p_fd;
1208 struct vnode *vp, *ovp;
1211 struct nchandle nch, onch, tnch;
1214 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1216 vp = (struct vnode *)fp->f_data;
1218 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1219 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1222 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1228 cache_copy(&fp->f_nchandle, &nch);
1231 * If the ncp has become a mount point, traverse through
1235 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1236 (mp = cache_findmount(&nch)) != NULL
1238 error = nlookup_mp(mp, &tnch);
1240 cache_unlock(&tnch); /* leave ref intact */
1242 vp = tnch.ncp->nc_vp;
1243 error = vget(vp, LK_SHARED);
1244 KKASSERT(error == 0);
1251 onch = fdp->fd_ncdir;
1252 vn_unlock(vp); /* leave ref intact */
1254 fdp->fd_ncdir = nch;
1266 kern_chdir(struct nlookupdata *nd)
1268 struct thread *td = curthread;
1269 struct proc *p = td->td_proc;
1270 struct filedesc *fdp = p->p_fd;
1271 struct vnode *vp, *ovp;
1272 struct nchandle onch;
1275 if ((error = nlookup(nd)) != 0)
1277 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1279 if ((error = vget(vp, LK_SHARED)) != 0)
1282 error = checkvp_chdir(vp, td);
1286 onch = fdp->fd_ncdir;
1287 cache_unlock(&nd->nl_nch); /* leave reference intact */
1288 fdp->fd_ncdir = nd->nl_nch;
1292 cache_zero(&nd->nl_nch);
1300 * chdir_args(char *path)
1302 * Change current working directory (``.'').
1305 sys_chdir(struct chdir_args *uap)
1307 struct nlookupdata nd;
1310 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1312 error = kern_chdir(&nd);
1318 * Helper function for raised chroot(2) security function: Refuse if
1319 * any filedescriptors are open directories.
1322 chroot_refuse_vdir_fds(struct filedesc *fdp)
1329 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1330 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1332 vp = (struct vnode *)fp->f_data;
1333 if (vp->v_type != VDIR) {
1344 * This sysctl determines if we will allow a process to chroot(2) if it
1345 * has a directory open:
1346 * 0: disallowed for all processes.
1347 * 1: allowed for processes that were not already chroot(2)'ed.
1348 * 2: allowed for all processes.
1351 static int chroot_allow_open_directories = 1;
1353 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1354 &chroot_allow_open_directories, 0, "");
1357 * chroot to the specified namecache entry. We obtain the vp from the
1358 * namecache data. The passed ncp must be locked and referenced and will
1359 * remain locked and referenced on return.
1362 kern_chroot(struct nchandle *nch)
1364 struct thread *td = curthread;
1365 struct proc *p = td->td_proc;
1366 struct filedesc *fdp = p->p_fd;
1371 * Only root can chroot
1373 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1377 * Disallow open directory descriptors (fchdir() breakouts).
1379 if (chroot_allow_open_directories == 0 ||
1380 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1381 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1384 if ((vp = nch->ncp->nc_vp) == NULL)
1387 if ((error = vget(vp, LK_SHARED)) != 0)
1391 * Check the validity of vp as a directory to change to and
1392 * associate it with rdir/jdir.
1394 error = checkvp_chdir(vp, td);
1395 vn_unlock(vp); /* leave reference intact */
1397 vrele(fdp->fd_rdir);
1398 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1399 cache_drop(&fdp->fd_nrdir);
1400 cache_copy(nch, &fdp->fd_nrdir);
1401 if (fdp->fd_jdir == NULL) {
1404 cache_copy(nch, &fdp->fd_njdir);
1413 * chroot_args(char *path)
1415 * Change notion of root (``/'') directory.
1419 sys_chroot(struct chroot_args *uap)
1421 struct thread *td = curthread;
1422 struct nlookupdata nd;
1425 KKASSERT(td->td_proc);
1426 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1431 error = nlookup(&nd);
1433 error = kern_chroot(&nd.nl_nch);
1439 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1440 * determine whether it is legal to chdir to the vnode. The vnode's state
1441 * is not changed by this call.
1444 checkvp_chdir(struct vnode *vp, struct thread *td)
1448 if (vp->v_type != VDIR)
1451 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1456 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1458 struct thread *td = curthread;
1459 struct proc *p = td->td_proc;
1460 struct lwp *lp = td->td_lwp;
1461 struct filedesc *fdp = p->p_fd;
1466 int type, indx, error;
1469 if ((oflags & O_ACCMODE) == O_ACCMODE)
1471 flags = FFLAGS(oflags);
1472 error = falloc(p, &nfp, NULL);
1476 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1479 * XXX p_dupfd is a real mess. It allows a device to return a
1480 * file descriptor to be duplicated rather then doing the open
1486 * Call vn_open() to do the lookup and assign the vnode to the
1487 * file pointer. vn_open() does not change the ref count on fp
1488 * and the vnode, on success, will be inherited by the file pointer
1491 nd->nl_flags |= NLC_LOCKVP;
1492 error = vn_open(nd, fp, flags, cmode);
1496 * handle special fdopen() case. bleh. dupfdopen() is
1497 * responsible for dropping the old contents of ofiles[indx]
1500 * Note that fsetfd() will add a ref to fp which represents
1501 * the fd_files[] assignment. We must still drop our
1504 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1505 if (fdalloc(p, 0, &indx) == 0) {
1506 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1509 fdrop(fp); /* our ref */
1512 fsetfd(p, NULL, indx);
1515 fdrop(fp); /* our ref */
1516 if (error == ERESTART)
1522 * ref the vnode for ourselves so it can't be ripped out from under
1523 * is. XXX need an ND flag to request that the vnode be returned
1526 * Reserve a file descriptor but do not assign it until the open
1529 vp = (struct vnode *)fp->f_data;
1531 if ((error = fdalloc(p, 0, &indx)) != 0) {
1538 * If no error occurs the vp will have been assigned to the file
1543 if (flags & (O_EXLOCK | O_SHLOCK)) {
1544 lf.l_whence = SEEK_SET;
1547 if (flags & O_EXLOCK)
1548 lf.l_type = F_WRLCK;
1550 lf.l_type = F_RDLCK;
1551 if (flags & FNONBLOCK)
1556 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1558 * lock request failed. Clean up the reserved
1562 fsetfd(p, NULL, indx);
1566 fp->f_flag |= FHASLOCK;
1570 * Assert that all regular file vnodes were created with a object.
1572 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1573 ("open: regular file has no backing object after vn_open"));
1579 * release our private reference, leaving the one associated with the
1580 * descriptor table intact.
1582 fsetfd(p, fp, indx);
1589 * open_args(char *path, int flags, int mode)
1591 * Check permissions, allocate an open file structure,
1592 * and call the device open routine if any.
1595 sys_open(struct open_args *uap)
1597 struct nlookupdata nd;
1600 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1602 error = kern_open(&nd, uap->flags,
1603 uap->mode, &uap->sysmsg_result);
1610 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1612 struct thread *td = curthread;
1613 struct proc *p = td->td_proc;
1621 switch (mode & S_IFMT) {
1627 error = suser_cred(p->p_ucred, PRISON_ROOT);
1634 nd->nl_flags |= NLC_CREATE;
1635 if ((error = nlookup(nd)) != 0)
1637 if (nd->nl_nch.ncp->nc_vp)
1639 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1643 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1644 vattr.va_rdev = dev;
1647 switch (mode & S_IFMT) {
1648 case S_IFMT: /* used by badsect to flag bad sectors */
1649 vattr.va_type = VBAD;
1652 vattr.va_type = VCHR;
1655 vattr.va_type = VBLK;
1666 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_cred, NAMEI_CREATE);
1669 error = VOP_NMKNOD(&nd->nl_nch, &vp, nd->nl_cred, &vattr);
1678 * mknod_args(char *path, int mode, int dev)
1680 * Create a special file.
1683 sys_mknod(struct mknod_args *uap)
1685 struct nlookupdata nd;
1688 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1690 error = kern_mknod(&nd, uap->mode, uap->dev);
1696 kern_mkfifo(struct nlookupdata *nd, int mode)
1698 struct thread *td = curthread;
1699 struct proc *p = td->td_proc;
1706 nd->nl_flags |= NLC_CREATE;
1707 if ((error = nlookup(nd)) != 0)
1709 if (nd->nl_nch.ncp->nc_vp)
1711 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1715 vattr.va_type = VFIFO;
1716 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1718 error = VOP_NMKNOD(&nd->nl_nch, &vp, nd->nl_cred, &vattr);
1725 * mkfifo_args(char *path, int mode)
1727 * Create a named pipe.
1730 sys_mkfifo(struct mkfifo_args *uap)
1732 struct nlookupdata nd;
1735 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1737 error = kern_mkfifo(&nd, uap->mode);
1742 static int hardlink_check_uid = 0;
1743 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1744 &hardlink_check_uid, 0,
1745 "Unprivileged processes cannot create hard links to files owned by other "
1747 static int hardlink_check_gid = 0;
1748 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1749 &hardlink_check_gid, 0,
1750 "Unprivileged processes cannot create hard links to files owned by other "
1754 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1760 * Shortcut if disabled
1762 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1766 * root cred can always hardlink
1768 if (suser_cred(cred, PRISON_ROOT) == 0)
1772 * Otherwise only if the originating file is owned by the
1773 * same user or group. Note that any group is allowed if
1774 * the file is owned by the caller.
1776 error = VOP_GETATTR(vp, &va);
1780 if (hardlink_check_uid) {
1781 if (cred->cr_uid != va.va_uid)
1785 if (hardlink_check_gid) {
1786 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1794 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1796 struct thread *td = curthread;
1801 * Lookup the source and obtained a locked vnode.
1803 * XXX relookup on vget failure / race ?
1806 if ((error = nlookup(nd)) != 0)
1808 vp = nd->nl_nch.ncp->nc_vp;
1809 KKASSERT(vp != NULL);
1810 if (vp->v_type == VDIR)
1811 return (EPERM); /* POSIX */
1812 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1814 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1818 * Unlock the source so we can lookup the target without deadlocking
1819 * (XXX vp is locked already, possible other deadlock?). The target
1822 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1823 nd->nl_flags &= ~NLC_NCPISLOCKED;
1824 cache_unlock(&nd->nl_nch);
1826 linknd->nl_flags |= NLC_CREATE;
1827 if ((error = nlookup(linknd)) != 0) {
1831 if (linknd->nl_nch.ncp->nc_vp) {
1837 * Finally run the new API VOP.
1839 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1841 error = VOP_NLINK(&linknd->nl_nch, vp, linknd->nl_cred);
1847 * link_args(char *path, char *link)
1849 * Make a hard file link.
1852 sys_link(struct link_args *uap)
1854 struct nlookupdata nd, linknd;
1857 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1859 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1861 error = kern_link(&nd, &linknd);
1862 nlookup_done(&linknd);
1869 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1876 nd->nl_flags |= NLC_CREATE;
1877 if ((error = nlookup(nd)) != 0)
1879 if (nd->nl_nch.ncp->nc_vp)
1881 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1884 vattr.va_mode = mode;
1885 error = VOP_NSYMLINK(&nd->nl_nch, &vp, nd->nl_cred, &vattr, path);
1892 * symlink(char *path, char *link)
1894 * Make a symbolic link.
1897 sys_symlink(struct symlink_args *uap)
1899 struct thread *td = curthread;
1900 struct nlookupdata nd;
1905 path = objcache_get(namei_oc, M_WAITOK);
1906 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1908 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1910 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1911 error = kern_symlink(&nd, path, mode);
1915 objcache_put(namei_oc, path);
1920 * undelete_args(char *path)
1922 * Delete a whiteout from the filesystem.
1926 sys_undelete(struct undelete_args *uap)
1928 struct nlookupdata nd;
1931 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1933 nd.nl_flags |= NLC_DELETE;
1935 error = nlookup(&nd);
1937 error = ncp_writechk(&nd.nl_nch);
1939 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_cred, NAMEI_DELETE);
1945 kern_unlink(struct nlookupdata *nd)
1950 nd->nl_flags |= NLC_DELETE;
1951 if ((error = nlookup(nd)) != 0)
1953 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1955 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_cred);
1960 * unlink_args(char *path)
1962 * Delete a name from the filesystem.
1965 sys_unlink(struct unlink_args *uap)
1967 struct nlookupdata nd;
1970 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1972 error = kern_unlink(&nd);
1978 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1980 struct thread *td = curthread;
1981 struct proc *p = td->td_proc;
1986 fp = holdfp(p->p_fd, fd, -1);
1989 if (fp->f_type != DTYPE_VNODE) {
1996 fp->f_offset += offset;
2000 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
2002 fp->f_offset = offset + vattr.va_size;
2005 fp->f_offset = offset;
2012 *res = fp->f_offset;
2019 * lseek_args(int fd, int pad, off_t offset, int whence)
2021 * Reposition read/write file offset.
2024 sys_lseek(struct lseek_args *uap)
2028 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2029 &uap->sysmsg_offset);
2035 kern_access(struct nlookupdata *nd, int aflags)
2040 if ((error = nlookup(nd)) != 0)
2043 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2047 /* Flags == 0 means only check for existence. */
2056 if ((flags & VWRITE) == 0 ||
2057 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2058 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2061 * If the file handle is stale we have to re-resolve the
2062 * entry. This is a hack at the moment.
2064 if (error == ESTALE) {
2065 cache_setunresolved(&nd->nl_nch);
2066 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2079 * access_args(char *path, int flags)
2081 * Check access permissions.
2084 sys_access(struct access_args *uap)
2086 struct nlookupdata nd;
2089 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2091 error = kern_access(&nd, uap->flags);
2097 kern_stat(struct nlookupdata *nd, struct stat *st)
2103 if ((error = nlookup(nd)) != 0)
2106 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2110 if ((error = vget(vp, LK_SHARED)) != 0)
2112 error = vn_stat(vp, st, nd->nl_cred);
2115 * If the file handle is stale we have to re-resolve the entry. This
2116 * is a hack at the moment.
2118 if (error == ESTALE) {
2119 cache_setunresolved(&nd->nl_nch);
2120 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2131 * stat_args(char *path, struct stat *ub)
2133 * Get file status; this version follows links.
2136 sys_stat(struct stat_args *uap)
2138 struct nlookupdata nd;
2142 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2144 error = kern_stat(&nd, &st);
2146 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2153 * lstat_args(char *path, struct stat *ub)
2155 * Get file status; this version does not follow links.
2158 sys_lstat(struct lstat_args *uap)
2160 struct nlookupdata nd;
2164 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2166 error = kern_stat(&nd, &st);
2168 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2175 * pathconf_Args(char *path, int name)
2177 * Get configurable pathname variables.
2181 sys_pathconf(struct pathconf_args *uap)
2183 struct nlookupdata nd;
2188 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2190 error = nlookup(&nd);
2192 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2195 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2203 * kern_readlink isn't properly split yet. There is a copyin burried
2204 * in VOP_READLINK().
2207 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2209 struct thread *td = curthread;
2210 struct proc *p = td->td_proc;
2216 if ((error = nlookup(nd)) != 0)
2218 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2221 if (vp->v_type != VLNK) {
2224 aiov.iov_base = buf;
2225 aiov.iov_len = count;
2226 auio.uio_iov = &aiov;
2227 auio.uio_iovcnt = 1;
2228 auio.uio_offset = 0;
2229 auio.uio_rw = UIO_READ;
2230 auio.uio_segflg = UIO_USERSPACE;
2232 auio.uio_resid = count;
2233 error = VOP_READLINK(vp, &auio, p->p_ucred);
2236 *res = count - auio.uio_resid;
2241 * readlink_args(char *path, char *buf, int count)
2243 * Return target name of a symbolic link.
2246 sys_readlink(struct readlink_args *uap)
2248 struct nlookupdata nd;
2251 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2253 error = kern_readlink(&nd, uap->buf, uap->count,
2254 &uap->sysmsg_result);
2261 setfflags(struct vnode *vp, int flags)
2263 struct thread *td = curthread;
2264 struct proc *p = td->td_proc;
2269 * Prevent non-root users from setting flags on devices. When
2270 * a device is reused, users can retain ownership of the device
2271 * if they are allowed to set flags and programs assume that
2272 * chown can't fail when done as root.
2274 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2275 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2279 * note: vget is required for any operation that might mod the vnode
2280 * so VINACTIVE is properly cleared.
2282 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2284 vattr.va_flags = flags;
2285 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2292 * chflags(char *path, int flags)
2294 * Change flags of a file given a path name.
2298 sys_chflags(struct chflags_args *uap)
2300 struct nlookupdata nd;
2305 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2306 /* XXX Add NLC flag indicating modifying operation? */
2308 error = nlookup(&nd);
2310 error = ncp_writechk(&nd.nl_nch);
2312 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2315 error = setfflags(vp, uap->flags);
2322 * fchflags_args(int fd, int flags)
2324 * Change flags of a file given a file descriptor.
2328 sys_fchflags(struct fchflags_args *uap)
2330 struct thread *td = curthread;
2331 struct proc *p = td->td_proc;
2335 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2337 if (fp->f_nchandle.ncp)
2338 error = ncp_writechk(&fp->f_nchandle);
2340 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2346 setfmode(struct vnode *vp, int mode)
2348 struct thread *td = curthread;
2349 struct proc *p = td->td_proc;
2354 * note: vget is required for any operation that might mod the vnode
2355 * so VINACTIVE is properly cleared.
2357 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2359 vattr.va_mode = mode & ALLPERMS;
2360 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2367 kern_chmod(struct nlookupdata *nd, int mode)
2372 /* XXX Add NLC flag indicating modifying operation? */
2373 if ((error = nlookup(nd)) != 0)
2375 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2377 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2378 error = setfmode(vp, mode);
2384 * chmod_args(char *path, int mode)
2386 * Change mode of a file given path name.
2390 sys_chmod(struct chmod_args *uap)
2392 struct nlookupdata nd;
2395 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2397 error = kern_chmod(&nd, uap->mode);
2403 * lchmod_args(char *path, int mode)
2405 * Change mode of a file given path name (don't follow links.)
2409 sys_lchmod(struct lchmod_args *uap)
2411 struct nlookupdata nd;
2414 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2416 error = kern_chmod(&nd, uap->mode);
2422 * fchmod_args(int fd, int mode)
2424 * Change mode of a file given a file descriptor.
2428 sys_fchmod(struct fchmod_args *uap)
2430 struct thread *td = curthread;
2431 struct proc *p = td->td_proc;
2435 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2437 if (fp->f_nchandle.ncp)
2438 error = ncp_writechk(&fp->f_nchandle);
2440 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2446 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2448 struct thread *td = curthread;
2449 struct proc *p = td->td_proc;
2454 * note: vget is required for any operation that might mod the vnode
2455 * so VINACTIVE is properly cleared.
2457 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2461 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2468 kern_chown(struct nlookupdata *nd, int uid, int gid)
2473 /* XXX Add NLC flag indicating modifying operation? */
2474 if ((error = nlookup(nd)) != 0)
2476 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2478 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2479 error = setfown(vp, uid, gid);
2485 * chown(char *path, int uid, int gid)
2487 * Set ownership given a path name.
2490 sys_chown(struct chown_args *uap)
2492 struct nlookupdata nd;
2495 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2497 error = kern_chown(&nd, uap->uid, uap->gid);
2503 * lchown_args(char *path, int uid, int gid)
2505 * Set ownership given a path name, do not cross symlinks.
2508 sys_lchown(struct lchown_args *uap)
2510 struct nlookupdata nd;
2513 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2515 error = kern_chown(&nd, uap->uid, uap->gid);
2521 * fchown_args(int fd, int uid, int gid)
2523 * Set ownership given a file descriptor.
2527 sys_fchown(struct fchown_args *uap)
2529 struct thread *td = curthread;
2530 struct proc *p = td->td_proc;
2534 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2536 if (fp->f_nchandle.ncp)
2537 error = ncp_writechk(&fp->f_nchandle);
2539 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2545 getutimes(const struct timeval *tvp, struct timespec *tsp)
2547 struct timeval tv[2];
2551 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2554 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2555 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2561 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2563 struct thread *td = curthread;
2564 struct proc *p = td->td_proc;
2569 * note: vget is required for any operation that might mod the vnode
2570 * so VINACTIVE is properly cleared.
2572 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2574 vattr.va_atime = ts[0];
2575 vattr.va_mtime = ts[1];
2577 vattr.va_vaflags |= VA_UTIMES_NULL;
2578 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2585 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2587 struct timespec ts[2];
2591 if ((error = getutimes(tptr, ts)) != 0)
2593 /* XXX Add NLC flag indicating modifying operation? */
2594 if ((error = nlookup(nd)) != 0)
2596 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2598 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2600 error = setutimes(vp, ts, tptr == NULL);
2606 * utimes_args(char *path, struct timeval *tptr)
2608 * Set the access and modification times of a file.
2611 sys_utimes(struct utimes_args *uap)
2613 struct timeval tv[2];
2614 struct nlookupdata nd;
2618 error = copyin(uap->tptr, tv, sizeof(tv));
2622 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2624 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2630 * lutimes_args(char *path, struct timeval *tptr)
2632 * Set the access and modification times of a file.
2635 sys_lutimes(struct lutimes_args *uap)
2637 struct timeval tv[2];
2638 struct nlookupdata nd;
2642 error = copyin(uap->tptr, tv, sizeof(tv));
2646 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2648 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2654 kern_futimes(int fd, struct timeval *tptr)
2656 struct thread *td = curthread;
2657 struct proc *p = td->td_proc;
2658 struct timespec ts[2];
2662 error = getutimes(tptr, ts);
2665 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2667 if (fp->f_nchandle.ncp)
2668 error = ncp_writechk(&fp->f_nchandle);
2670 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2676 * futimes_args(int fd, struct timeval *tptr)
2678 * Set the access and modification times of a file.
2681 sys_futimes(struct futimes_args *uap)
2683 struct timeval tv[2];
2687 error = copyin(uap->tptr, tv, sizeof(tv));
2692 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2698 kern_truncate(struct nlookupdata *nd, off_t length)
2706 /* XXX Add NLC flag indicating modifying operation? */
2707 if ((error = nlookup(nd)) != 0)
2709 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2711 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2713 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2717 if (vp->v_type == VDIR) {
2719 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
2720 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2722 vattr.va_size = length;
2723 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2730 * truncate(char *path, int pad, off_t length)
2732 * Truncate a file given its path name.
2735 sys_truncate(struct truncate_args *uap)
2737 struct nlookupdata nd;
2740 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2742 error = kern_truncate(&nd, uap->length);
2748 kern_ftruncate(int fd, off_t length)
2750 struct thread *td = curthread;
2751 struct proc *p = td->td_proc;
2759 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2761 if (fp->f_nchandle.ncp) {
2762 error = ncp_writechk(&fp->f_nchandle);
2766 if ((fp->f_flag & FWRITE) == 0) {
2770 vp = (struct vnode *)fp->f_data;
2771 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2772 if (vp->v_type == VDIR) {
2774 } else if ((error = vn_writechk(vp, NULL)) == 0) {
2776 vattr.va_size = length;
2777 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2786 * ftruncate_args(int fd, int pad, off_t length)
2788 * Truncate a file given a file descriptor.
2791 sys_ftruncate(struct ftruncate_args *uap)
2795 error = kern_ftruncate(uap->fd, uap->length);
2803 * Sync an open file.
2807 sys_fsync(struct fsync_args *uap)
2809 struct thread *td = curthread;
2810 struct proc *p = td->td_proc;
2816 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2818 vp = (struct vnode *)fp->f_data;
2819 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2820 if ((obj = vp->v_object) != NULL)
2821 vm_object_page_clean(obj, 0, 0, 0);
2822 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2823 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2825 error = (*bioops.io_fsync)(vp);
2833 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2835 struct nchandle fnchd;
2836 struct nchandle tnchd;
2837 struct namecache *ncp;
2842 if ((error = nlookup(fromnd)) != 0)
2844 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
2846 fnchd.mount = fromnd->nl_nch.mount;
2850 * unlock the source nch so we can lookup the target nch without
2851 * deadlocking. The target may or may not exist so we do not check
2852 * for a target vp like kern_mkdir() and other creation functions do.
2854 * The source and target directories are ref'd and rechecked after
2855 * everything is relocked to determine if the source or target file
2858 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2859 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2860 cache_unlock(&fromnd->nl_nch);
2862 tond->nl_flags |= NLC_CREATE;
2863 if ((error = nlookup(tond)) != 0) {
2867 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
2871 tnchd.mount = tond->nl_nch.mount;
2875 * If the source and target are the same there is nothing to do
2877 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
2884 * Mount points cannot be renamed or overwritten
2886 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
2895 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2896 * may have become invalid while it was unlocked, nc_vp and nc_mount
2899 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
2900 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2901 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
2902 cache_lock(&fromnd->nl_nch);
2903 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2905 cache_unlock(&tond->nl_nch);
2906 cache_lock(&fromnd->nl_nch);
2907 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2908 cache_lock(&tond->nl_nch);
2909 cache_resolve(&tond->nl_nch, tond->nl_cred);
2911 fromnd->nl_flags |= NLC_NCPISLOCKED;
2914 * make sure the parent directories linkages are the same
2916 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
2917 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
2924 * Both the source and target must be within the same filesystem and
2925 * in the same filesystem as their parent directories within the
2926 * namecache topology.
2928 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2931 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
2932 mp != tond->nl_nch.mount) {
2939 * Make sure the mount point is writable
2941 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
2948 * If the target exists and either the source or target is a directory,
2949 * then both must be directories.
2951 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
2954 if (tond->nl_nch.ncp->nc_vp) {
2955 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
2957 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
2958 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
2960 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
2966 * You cannot rename a source into itself or a subdirectory of itself.
2967 * We check this by travsersing the target directory upwards looking
2968 * for a match against the source.
2971 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
2972 if (fromnd->nl_nch.ncp == ncp) {
2983 * Even though the namespaces are different, they may still represent
2984 * hardlinks to the same file. The filesystem might have a hard time
2985 * with this so we issue a NREMOVE of the source instead of a NRENAME
2986 * when we detect the situation.
2989 if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
2990 error = VOP_NREMOVE(&fromnd->nl_nch, fromnd->nl_cred);
2992 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3000 * rename_args(char *from, char *to)
3002 * Rename files. Source and destination must either both be directories,
3003 * or both not be directories. If target is a directory, it must be empty.
3006 sys_rename(struct rename_args *uap)
3008 struct nlookupdata fromnd, tond;
3011 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3013 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3015 error = kern_rename(&fromnd, &tond);
3016 nlookup_done(&tond);
3018 nlookup_done(&fromnd);
3023 kern_mkdir(struct nlookupdata *nd, int mode)
3025 struct thread *td = curthread;
3026 struct proc *p = td->td_proc;
3032 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
3033 if ((error = nlookup(nd)) != 0)
3036 if (nd->nl_nch.ncp->nc_vp)
3038 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3042 vattr.va_type = VDIR;
3043 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3046 error = VOP_NMKDIR(&nd->nl_nch, &vp, p->p_ucred, &vattr);
3053 * mkdir_args(char *path, int mode)
3055 * Make a directory file.
3059 sys_mkdir(struct mkdir_args *uap)
3061 struct nlookupdata nd;
3064 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3066 error = kern_mkdir(&nd, uap->mode);
3072 kern_rmdir(struct nlookupdata *nd)
3077 nd->nl_flags |= NLC_DELETE;
3078 if ((error = nlookup(nd)) != 0)
3082 * Do not allow directories representing mount points to be
3083 * deleted, even if empty. Check write perms on mount point
3084 * in case the vnode is aliased (aka nullfs).
3086 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3088 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3091 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_cred);
3096 * rmdir_args(char *path)
3098 * Remove a directory file.
3102 sys_rmdir(struct rmdir_args *uap)
3104 struct nlookupdata nd;
3107 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3109 error = kern_rmdir(&nd);
3115 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3116 enum uio_seg direction)
3118 struct thread *td = curthread;
3119 struct proc *p = td->td_proc;
3127 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3129 if ((fp->f_flag & FREAD) == 0) {
3133 vp = (struct vnode *)fp->f_data;
3135 if (vp->v_type != VDIR) {
3139 aiov.iov_base = buf;
3140 aiov.iov_len = count;
3141 auio.uio_iov = &aiov;
3142 auio.uio_iovcnt = 1;
3143 auio.uio_rw = UIO_READ;
3144 auio.uio_segflg = direction;
3146 auio.uio_resid = count;
3147 loff = auio.uio_offset = fp->f_offset;
3148 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3149 fp->f_offset = auio.uio_offset;
3152 if (count == auio.uio_resid) {
3153 if (union_dircheckp) {
3154 error = union_dircheckp(td, &vp, fp);
3161 if ((vp->v_flag & VROOT) &&
3162 (vp->v_mount->mnt_flag & MNT_UNION)) {
3163 struct vnode *tvp = vp;
3164 vp = vp->v_mount->mnt_vnodecovered;
3176 *res = count - auio.uio_resid;
3183 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3185 * Read a block of directory entries in a file system independent format.
3188 sys_getdirentries(struct getdirentries_args *uap)
3193 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3194 &uap->sysmsg_result, UIO_USERSPACE);
3197 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3202 * getdents_args(int fd, char *buf, size_t count)
3205 sys_getdents(struct getdents_args *uap)
3209 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3210 &uap->sysmsg_result, UIO_USERSPACE);
3216 * umask(int newmask)
3218 * Set the mode mask for creation of filesystem nodes.
3223 sys_umask(struct umask_args *uap)
3225 struct thread *td = curthread;
3226 struct proc *p = td->td_proc;
3227 struct filedesc *fdp;
3230 uap->sysmsg_result = fdp->fd_cmask;
3231 fdp->fd_cmask = uap->newmask & ALLPERMS;
3236 * revoke(char *path)
3238 * Void all references to file by ripping underlying filesystem
3243 sys_revoke(struct revoke_args *uap)
3245 struct nlookupdata nd;
3252 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3254 error = nlookup(&nd);
3256 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3257 cred = crhold(nd.nl_cred);
3260 if (vp->v_type != VCHR && vp->v_type != VBLK)
3263 error = VOP_GETATTR(vp, &vattr);
3264 if (error == 0 && cred->cr_uid != vattr.va_uid)
3265 error = suser_cred(cred, PRISON_ROOT);
3266 if (error == 0 && count_udev(vp->v_udev) > 0) {
3269 VOP_REVOKE(vp, REVOKEALL);
3280 * getfh_args(char *fname, fhandle_t *fhp)
3282 * Get (NFS) file handle
3285 sys_getfh(struct getfh_args *uap)
3287 struct thread *td = curthread;
3288 struct nlookupdata nd;
3294 * Must be super user
3296 if ((error = suser(td)) != 0)
3300 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3302 error = nlookup(&nd);
3304 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3307 bzero(&fh, sizeof(fh));
3308 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3309 error = VFS_VPTOFH(vp, &fh.fh_fid);
3312 error = copyout(&fh, uap->fhp, sizeof(fh));
3318 * fhopen_args(const struct fhandle *u_fhp, int flags)
3320 * syscall for the rpc.lockd to use to translate a NFS file handle into
3321 * an open descriptor.
3323 * warning: do not remove the suser() call or this becomes one giant
3327 sys_fhopen(struct fhopen_args *uap)
3329 struct thread *td = curthread;
3330 struct proc *p = td->td_proc;
3335 struct vattr *vap = &vat;
3337 int fmode, mode, error, type;
3343 * Must be super user
3349 fmode = FFLAGS(uap->flags);
3350 /* why not allow a non-read/write open for our lockd? */
3351 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3353 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3356 /* find the mount point */
3357 mp = vfs_getvfs(&fhp.fh_fsid);
3360 /* now give me my vnode, it gets returned to me locked */
3361 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3365 * from now on we have to make sure not
3366 * to forget about the vnode
3367 * any error that causes an abort must vput(vp)
3368 * just set error = err and 'goto bad;'.
3374 if (vp->v_type == VLNK) {
3378 if (vp->v_type == VSOCK) {
3383 if (fmode & (FWRITE | O_TRUNC)) {
3384 if (vp->v_type == VDIR) {
3388 error = vn_writechk(vp, NULL);
3396 error = VOP_ACCESS(vp, mode, p->p_ucred);
3400 if (fmode & O_TRUNC) {
3401 vn_unlock(vp); /* XXX */
3402 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3405 error = VOP_SETATTR(vp, vap, p->p_ucred);
3411 * VOP_OPEN needs the file pointer so it can potentially override
3414 * WARNING! no f_nchandle will be associated when fhopen()ing a
3417 if ((error = falloc(p, &nfp, &indx)) != 0)
3421 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3424 * setting f_ops this way prevents VOP_CLOSE from being
3425 * called or fdrop() releasing the vp from v_data. Since
3426 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3428 fp->f_ops = &badfileops;
3434 * The fp is given its own reference, we still have our ref and lock.
3436 * Assert that all regular files must be created with a VM object.
3438 if (vp->v_type == VREG && vp->v_object == NULL) {
3439 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3444 * The open was successful. Handle any locking requirements.
3446 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3447 lf.l_whence = SEEK_SET;
3450 if (fmode & O_EXLOCK)
3451 lf.l_type = F_WRLCK;
3453 lf.l_type = F_RDLCK;
3454 if (fmode & FNONBLOCK)
3459 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3461 * release our private reference.
3463 fsetfd(p, NULL, indx);
3468 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3469 fp->f_flag |= FHASLOCK;
3473 * Clean up. Associate the file pointer with the previously
3474 * reserved descriptor and return it.
3477 fsetfd(p, fp, indx);
3479 uap->sysmsg_result = indx;
3483 fsetfd(p, NULL, indx);
3491 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3494 sys_fhstat(struct fhstat_args *uap)
3496 struct thread *td = curthread;
3504 * Must be super user
3510 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3514 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3516 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3518 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3522 error = copyout(&sb, uap->sb, sizeof(sb));
3527 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3530 sys_fhstatfs(struct fhstatfs_args *uap)
3532 struct thread *td = curthread;
3533 struct proc *p = td->td_proc;
3538 char *fullpath, *freepath;
3543 * Must be super user
3545 if ((error = suser(td)))
3548 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3551 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3554 if (p != NULL && !chroot_visible_mnt(mp, p))
3557 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3562 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3565 error = mount_path(p, mp, &fullpath, &freepath);
3568 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3569 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3570 kfree(freepath, M_TEMP);
3572 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3574 bcopy(sp, &sb, sizeof(sb));
3575 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3578 return (copyout(sp, uap->buf, sizeof(*sp)));
3582 * Syscall to push extended attribute configuration information into the
3583 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3584 * a command (int cmd), and attribute name and misc data. For now, the
3585 * attribute name is left in userspace for consumption by the VFS_op.
3586 * It will probably be changed to be copied into sysspace by the
3587 * syscall in the future, once issues with various consumers of the
3588 * attribute code have raised their hands.
3590 * Currently this is used only by UFS Extended Attributes.
3593 sys_extattrctl(struct extattrctl_args *uap)
3595 struct nlookupdata nd;
3601 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3603 error = nlookup(&nd);
3605 mp = nd.nl_nch.mount;
3606 error = VFS_EXTATTRCTL(mp, uap->cmd,
3607 uap->attrname, uap->arg,
3615 * Syscall to set a named extended attribute on a file or directory.
3616 * Accepts attribute name, and a uio structure pointing to the data to set.
3617 * The uio is consumed in the style of writev(). The real work happens
3618 * in VOP_SETEXTATTR().
3621 sys_extattr_set_file(struct extattr_set_file_args *uap)
3623 char attrname[EXTATTR_MAXNAMELEN];
3624 struct iovec aiov[UIO_SMALLIOV];
3625 struct iovec *needfree;
3626 struct nlookupdata nd;
3635 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3640 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3642 error = nlookup(&nd);
3644 error = ncp_writechk(&nd.nl_nch);
3646 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3653 iovlen = uap->iovcnt * sizeof(struct iovec);
3654 if (uap->iovcnt > UIO_SMALLIOV) {
3655 if (uap->iovcnt > UIO_MAXIOV) {
3659 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3665 auio.uio_iovcnt = uap->iovcnt;
3666 auio.uio_rw = UIO_WRITE;
3667 auio.uio_segflg = UIO_USERSPACE;
3668 auio.uio_td = nd.nl_td;
3669 auio.uio_offset = 0;
3670 if ((error = copyin(uap->iovp, iov, iovlen)))
3673 for (i = 0; i < uap->iovcnt; i++) {
3674 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3678 auio.uio_resid += iov->iov_len;
3681 cnt = auio.uio_resid;
3682 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3683 cnt -= auio.uio_resid;
3684 uap->sysmsg_result = cnt;
3689 FREE(needfree, M_IOV);
3694 * Syscall to get a named extended attribute on a file or directory.
3695 * Accepts attribute name, and a uio structure pointing to a buffer for the
3696 * data. The uio is consumed in the style of readv(). The real work
3697 * happens in VOP_GETEXTATTR();
3700 sys_extattr_get_file(struct extattr_get_file_args *uap)
3702 char attrname[EXTATTR_MAXNAMELEN];
3703 struct iovec aiov[UIO_SMALLIOV];
3704 struct iovec *needfree;
3705 struct nlookupdata nd;
3714 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3719 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3721 error = nlookup(&nd);
3723 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3729 iovlen = uap->iovcnt * sizeof (struct iovec);
3731 if (uap->iovcnt > UIO_SMALLIOV) {
3732 if (uap->iovcnt > UIO_MAXIOV) {
3736 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3742 auio.uio_iovcnt = uap->iovcnt;
3743 auio.uio_rw = UIO_READ;
3744 auio.uio_segflg = UIO_USERSPACE;
3745 auio.uio_td = nd.nl_td;
3746 auio.uio_offset = 0;
3747 if ((error = copyin(uap->iovp, iov, iovlen)))
3750 for (i = 0; i < uap->iovcnt; i++) {
3751 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3755 auio.uio_resid += iov->iov_len;
3758 cnt = auio.uio_resid;
3759 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3760 cnt -= auio.uio_resid;
3761 uap->sysmsg_result = cnt;
3766 FREE(needfree, M_IOV);
3771 * Syscall to delete a named extended attribute from a file or directory.
3772 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3775 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
3777 char attrname[EXTATTR_MAXNAMELEN];
3778 struct nlookupdata nd;
3782 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3787 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3789 error = nlookup(&nd);
3791 error = ncp_writechk(&nd.nl_nch);
3793 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3799 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3806 * Determine if the mount is visible to the process.
3809 chroot_visible_mnt(struct mount *mp, struct proc *p)
3811 struct nchandle nch;
3814 * Traverse from the mount point upwards. If we hit the process
3815 * root then the mount point is visible to the process.
3817 nch = mp->mnt_ncmountpt;
3819 if (nch.mount == p->p_fd->fd_nrdir.mount &&
3820 nch.ncp == p->p_fd->fd_nrdir.ncp) {
3823 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
3824 nch = nch.mount->mnt_ncmounton;
3826 nch.ncp = nch.ncp->nc_parent;
3831 * If the mount point is not visible to the process, but the
3832 * process root is in a subdirectory of the mount, return
3835 if (p->p_fd->fd_nrdir.mount == mp)