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.118 2007/07/19 01:16:39 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>
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_sysref.refcnt == 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);
762 if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
763 kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
772 * Shim cache_fullpath() to handle the case where a process is chrooted into
773 * a subdirectory of a mount. In this case if the root mount matches the
774 * process root directory's mount we have to specify the process's root
775 * directory instead of the mount point, because the mount point might
776 * be above the root directory.
780 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
782 struct nchandle *nch;
784 if (p && p->p_fd->fd_nrdir.mount == mp)
785 nch = &p->p_fd->fd_nrdir;
787 nch = &mp->mnt_ncmountpt;
788 return(cache_fullpath(p, nch, rb, fb));
792 * Sync each mounted filesystem.
796 static int syncprt = 0;
797 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
800 static int sync_callback(struct mount *mp, void *data);
804 sys_sync(struct sync_args *uap)
806 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
809 * print out buffer pool stat information on each sync() call.
819 sync_callback(struct mount *mp, void *data __unused)
823 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
824 asyncflag = mp->mnt_flag & MNT_ASYNC;
825 mp->mnt_flag &= ~MNT_ASYNC;
826 vfs_msync(mp, MNT_NOWAIT);
827 VFS_SYNC(mp, MNT_NOWAIT);
828 mp->mnt_flag |= asyncflag;
833 /* XXX PRISON: could be per prison flag */
834 static int prison_quotas;
836 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
840 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
842 * Change filesystem quotas.
846 sys_quotactl(struct quotactl_args *uap)
848 struct nlookupdata nd;
856 if (p->p_ucred->cr_prison && !prison_quotas)
859 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
861 error = nlookup(&nd);
863 mp = nd.nl_nch.mount;
864 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
865 uap->arg, nd.nl_cred);
872 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
873 * void *buf, int buflen)
875 * This function operates on a mount point and executes the specified
876 * operation using the specified control data, and possibly returns data.
878 * The actual number of bytes stored in the result buffer is returned, 0
879 * if none, otherwise an error is returned.
883 sys_mountctl(struct mountctl_args *uap)
885 struct thread *td = curthread;
886 struct proc *p = td->td_proc;
894 * Sanity and permissions checks. We must be root.
897 if (p->p_ucred->cr_prison != NULL)
899 if ((error = suser(td)) != 0)
903 * Argument length checks
905 if (uap->ctllen < 0 || uap->ctllen > 1024)
907 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
909 if (uap->path == NULL)
913 * Allocate the necessary buffers and copyin data
915 path = objcache_get(namei_oc, M_WAITOK);
916 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
921 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
922 error = copyin(uap->ctl, ctl, uap->ctllen);
927 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
930 * Validate the descriptor
933 fp = holdfp(p->p_fd, uap->fd, -1);
943 * Execute the internal kernel function and clean up.
945 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
948 if (error == 0 && uap->sysmsg_result > 0)
949 error = copyout(buf, uap->buf, uap->sysmsg_result);
952 objcache_put(namei_oc, path);
961 * Execute a mount control operation by resolving the path to a mount point
962 * and calling vop_mountctl().
965 kern_mountctl(const char *path, int op, struct file *fp,
966 const void *ctl, int ctllen,
967 void *buf, int buflen, int *res)
971 struct nlookupdata nd;
976 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
978 error = nlookup(&nd);
980 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
988 * Must be the root of the filesystem
990 if ((vp->v_flag & VROOT) == 0) {
994 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
1001 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1003 struct thread *td = curthread;
1004 struct proc *p = td->td_proc;
1007 char *fullpath, *freepath;
1010 if ((error = nlookup(nd)) != 0)
1012 mp = nd->nl_nch.mount;
1014 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
1017 error = mount_path(p, mp, &fullpath, &freepath);
1020 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1021 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1022 kfree(freepath, M_TEMP);
1024 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1025 bcopy(sp, buf, sizeof(*buf));
1026 /* Only root should have access to the fsid's. */
1028 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1033 * statfs_args(char *path, struct statfs *buf)
1035 * Get filesystem statistics.
1038 sys_statfs(struct statfs_args *uap)
1040 struct nlookupdata nd;
1044 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1046 error = kern_statfs(&nd, &buf);
1049 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1054 kern_fstatfs(int fd, struct statfs *buf)
1056 struct thread *td = curthread;
1057 struct proc *p = td->td_proc;
1061 char *fullpath, *freepath;
1065 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1067 mp = ((struct vnode *)fp->f_data)->v_mount;
1072 if (fp->f_cred == NULL) {
1077 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1080 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1082 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1083 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1084 kfree(freepath, M_TEMP);
1086 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1087 bcopy(sp, buf, sizeof(*buf));
1089 /* Only root should have access to the fsid's. */
1091 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1099 * fstatfs_args(int fd, struct statfs *buf)
1101 * Get filesystem statistics.
1104 sys_fstatfs(struct fstatfs_args *uap)
1109 error = kern_fstatfs(uap->fd, &buf);
1112 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1117 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1119 * Get statistics on all filesystems.
1122 struct getfsstat_info {
1123 struct statfs *sfsp;
1131 static int getfsstat_callback(struct mount *, void *);
1135 sys_getfsstat(struct getfsstat_args *uap)
1137 struct thread *td = curthread;
1138 struct proc *p = td->td_proc;
1139 struct getfsstat_info info;
1141 bzero(&info, sizeof(info));
1143 info.maxcount = uap->bufsize / sizeof(struct statfs);
1144 info.sfsp = uap->buf;
1146 info.flags = uap->flags;
1149 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1150 if (info.sfsp && info.count > info.maxcount)
1151 uap->sysmsg_result = info.maxcount;
1153 uap->sysmsg_result = info.count;
1154 return (info.error);
1158 getfsstat_callback(struct mount *mp, void *data)
1160 struct getfsstat_info *info = data;
1166 if (info->sfsp && info->count < info->maxcount) {
1167 if (info->p && !chroot_visible_mnt(mp, info->p))
1172 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1173 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1174 * overrides MNT_WAIT.
1176 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1177 (info->flags & MNT_WAIT)) &&
1178 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1181 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1183 error = mount_path(info->p, mp, &fullpath, &freepath);
1185 info->error = error;
1188 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1189 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1190 kfree(freepath, M_TEMP);
1192 error = copyout(sp, info->sfsp, sizeof(*sp));
1194 info->error = error;
1204 * fchdir_args(int fd)
1206 * Change current working directory to a given file descriptor.
1210 sys_fchdir(struct fchdir_args *uap)
1212 struct thread *td = curthread;
1213 struct proc *p = td->td_proc;
1214 struct filedesc *fdp = p->p_fd;
1215 struct vnode *vp, *ovp;
1218 struct nchandle nch, onch, tnch;
1221 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1223 vp = (struct vnode *)fp->f_data;
1225 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1226 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1229 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1235 cache_copy(&fp->f_nchandle, &nch);
1238 * If the ncp has become a mount point, traverse through
1242 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1243 (mp = cache_findmount(&nch)) != NULL
1245 error = nlookup_mp(mp, &tnch);
1247 cache_unlock(&tnch); /* leave ref intact */
1249 vp = tnch.ncp->nc_vp;
1250 error = vget(vp, LK_SHARED);
1251 KKASSERT(error == 0);
1258 onch = fdp->fd_ncdir;
1259 vn_unlock(vp); /* leave ref intact */
1261 fdp->fd_ncdir = nch;
1273 kern_chdir(struct nlookupdata *nd)
1275 struct thread *td = curthread;
1276 struct proc *p = td->td_proc;
1277 struct filedesc *fdp = p->p_fd;
1278 struct vnode *vp, *ovp;
1279 struct nchandle onch;
1282 if ((error = nlookup(nd)) != 0)
1284 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1286 if ((error = vget(vp, LK_SHARED)) != 0)
1289 error = checkvp_chdir(vp, td);
1293 onch = fdp->fd_ncdir;
1294 cache_unlock(&nd->nl_nch); /* leave reference intact */
1295 fdp->fd_ncdir = nd->nl_nch;
1299 cache_zero(&nd->nl_nch);
1307 * chdir_args(char *path)
1309 * Change current working directory (``.'').
1312 sys_chdir(struct chdir_args *uap)
1314 struct nlookupdata nd;
1317 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1319 error = kern_chdir(&nd);
1325 * Helper function for raised chroot(2) security function: Refuse if
1326 * any filedescriptors are open directories.
1329 chroot_refuse_vdir_fds(struct filedesc *fdp)
1336 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1337 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1339 vp = (struct vnode *)fp->f_data;
1340 if (vp->v_type != VDIR) {
1351 * This sysctl determines if we will allow a process to chroot(2) if it
1352 * has a directory open:
1353 * 0: disallowed for all processes.
1354 * 1: allowed for processes that were not already chroot(2)'ed.
1355 * 2: allowed for all processes.
1358 static int chroot_allow_open_directories = 1;
1360 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1361 &chroot_allow_open_directories, 0, "");
1364 * chroot to the specified namecache entry. We obtain the vp from the
1365 * namecache data. The passed ncp must be locked and referenced and will
1366 * remain locked and referenced on return.
1369 kern_chroot(struct nchandle *nch)
1371 struct thread *td = curthread;
1372 struct proc *p = td->td_proc;
1373 struct filedesc *fdp = p->p_fd;
1378 * Only root can chroot
1380 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1384 * Disallow open directory descriptors (fchdir() breakouts).
1386 if (chroot_allow_open_directories == 0 ||
1387 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1388 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1391 if ((vp = nch->ncp->nc_vp) == NULL)
1394 if ((error = vget(vp, LK_SHARED)) != 0)
1398 * Check the validity of vp as a directory to change to and
1399 * associate it with rdir/jdir.
1401 error = checkvp_chdir(vp, td);
1402 vn_unlock(vp); /* leave reference intact */
1404 vrele(fdp->fd_rdir);
1405 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1406 cache_drop(&fdp->fd_nrdir);
1407 cache_copy(nch, &fdp->fd_nrdir);
1408 if (fdp->fd_jdir == NULL) {
1411 cache_copy(nch, &fdp->fd_njdir);
1420 * chroot_args(char *path)
1422 * Change notion of root (``/'') directory.
1426 sys_chroot(struct chroot_args *uap)
1428 struct thread *td = curthread;
1429 struct nlookupdata nd;
1432 KKASSERT(td->td_proc);
1433 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1438 error = nlookup(&nd);
1440 error = kern_chroot(&nd.nl_nch);
1446 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1447 * determine whether it is legal to chdir to the vnode. The vnode's state
1448 * is not changed by this call.
1451 checkvp_chdir(struct vnode *vp, struct thread *td)
1455 if (vp->v_type != VDIR)
1458 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1463 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1465 struct thread *td = curthread;
1466 struct proc *p = td->td_proc;
1467 struct lwp *lp = td->td_lwp;
1468 struct filedesc *fdp = p->p_fd;
1473 int type, indx, error;
1476 if ((oflags & O_ACCMODE) == O_ACCMODE)
1478 flags = FFLAGS(oflags);
1479 error = falloc(p, &nfp, NULL);
1483 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1486 * XXX p_dupfd is a real mess. It allows a device to return a
1487 * file descriptor to be duplicated rather then doing the open
1493 * Call vn_open() to do the lookup and assign the vnode to the
1494 * file pointer. vn_open() does not change the ref count on fp
1495 * and the vnode, on success, will be inherited by the file pointer
1498 nd->nl_flags |= NLC_LOCKVP;
1499 error = vn_open(nd, fp, flags, cmode);
1503 * handle special fdopen() case. bleh. dupfdopen() is
1504 * responsible for dropping the old contents of ofiles[indx]
1507 * Note that fsetfd() will add a ref to fp which represents
1508 * the fd_files[] assignment. We must still drop our
1511 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1512 if (fdalloc(p, 0, &indx) == 0) {
1513 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1516 fdrop(fp); /* our ref */
1519 fsetfd(p, NULL, indx);
1522 fdrop(fp); /* our ref */
1523 if (error == ERESTART)
1529 * ref the vnode for ourselves so it can't be ripped out from under
1530 * is. XXX need an ND flag to request that the vnode be returned
1533 * Reserve a file descriptor but do not assign it until the open
1536 vp = (struct vnode *)fp->f_data;
1538 if ((error = fdalloc(p, 0, &indx)) != 0) {
1545 * If no error occurs the vp will have been assigned to the file
1550 if (flags & (O_EXLOCK | O_SHLOCK)) {
1551 lf.l_whence = SEEK_SET;
1554 if (flags & O_EXLOCK)
1555 lf.l_type = F_WRLCK;
1557 lf.l_type = F_RDLCK;
1558 if (flags & FNONBLOCK)
1563 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1565 * lock request failed. Clean up the reserved
1569 fsetfd(p, NULL, indx);
1573 fp->f_flag |= FHASLOCK;
1577 * Assert that all regular file vnodes were created with a object.
1579 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1580 ("open: regular file has no backing object after vn_open"));
1586 * release our private reference, leaving the one associated with the
1587 * descriptor table intact.
1589 fsetfd(p, fp, indx);
1596 * open_args(char *path, int flags, int mode)
1598 * Check permissions, allocate an open file structure,
1599 * and call the device open routine if any.
1602 sys_open(struct open_args *uap)
1604 struct nlookupdata nd;
1607 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1609 error = kern_open(&nd, uap->flags,
1610 uap->mode, &uap->sysmsg_result);
1617 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
1619 struct thread *td = curthread;
1620 struct proc *p = td->td_proc;
1628 switch (mode & S_IFMT) {
1634 error = suser_cred(p->p_ucred, PRISON_ROOT);
1641 nd->nl_flags |= NLC_CREATE;
1642 if ((error = nlookup(nd)) != 0)
1644 if (nd->nl_nch.ncp->nc_vp)
1646 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1650 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1651 vattr.va_rmajor = rmajor;
1652 vattr.va_rminor = rminor;
1655 switch (mode & S_IFMT) {
1656 case S_IFMT: /* used by badsect to flag bad sectors */
1657 vattr.va_type = VBAD;
1660 vattr.va_type = VCHR;
1663 vattr.va_type = VBLK;
1674 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_cred, NAMEI_CREATE);
1677 error = VOP_NMKNOD(&nd->nl_nch, &vp, nd->nl_cred, &vattr);
1686 * mknod_args(char *path, int mode, int dev)
1688 * Create a special file.
1691 sys_mknod(struct mknod_args *uap)
1693 struct nlookupdata nd;
1696 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1698 error = kern_mknod(&nd, uap->mode,
1699 umajor(uap->dev), uminor(uap->dev));
1706 kern_mkfifo(struct nlookupdata *nd, int mode)
1708 struct thread *td = curthread;
1709 struct proc *p = td->td_proc;
1716 nd->nl_flags |= NLC_CREATE;
1717 if ((error = nlookup(nd)) != 0)
1719 if (nd->nl_nch.ncp->nc_vp)
1721 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1725 vattr.va_type = VFIFO;
1726 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1728 error = VOP_NMKNOD(&nd->nl_nch, &vp, nd->nl_cred, &vattr);
1735 * mkfifo_args(char *path, int mode)
1737 * Create a named pipe.
1740 sys_mkfifo(struct mkfifo_args *uap)
1742 struct nlookupdata nd;
1745 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1747 error = kern_mkfifo(&nd, uap->mode);
1752 static int hardlink_check_uid = 0;
1753 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1754 &hardlink_check_uid, 0,
1755 "Unprivileged processes cannot create hard links to files owned by other "
1757 static int hardlink_check_gid = 0;
1758 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1759 &hardlink_check_gid, 0,
1760 "Unprivileged processes cannot create hard links to files owned by other "
1764 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1770 * Shortcut if disabled
1772 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1776 * root cred can always hardlink
1778 if (suser_cred(cred, PRISON_ROOT) == 0)
1782 * Otherwise only if the originating file is owned by the
1783 * same user or group. Note that any group is allowed if
1784 * the file is owned by the caller.
1786 error = VOP_GETATTR(vp, &va);
1790 if (hardlink_check_uid) {
1791 if (cred->cr_uid != va.va_uid)
1795 if (hardlink_check_gid) {
1796 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1804 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1806 struct thread *td = curthread;
1811 * Lookup the source and obtained a locked vnode.
1813 * XXX relookup on vget failure / race ?
1816 if ((error = nlookup(nd)) != 0)
1818 vp = nd->nl_nch.ncp->nc_vp;
1819 KKASSERT(vp != NULL);
1820 if (vp->v_type == VDIR)
1821 return (EPERM); /* POSIX */
1822 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1824 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1828 * Unlock the source so we can lookup the target without deadlocking
1829 * (XXX vp is locked already, possible other deadlock?). The target
1832 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1833 nd->nl_flags &= ~NLC_NCPISLOCKED;
1834 cache_unlock(&nd->nl_nch);
1836 linknd->nl_flags |= NLC_CREATE;
1837 if ((error = nlookup(linknd)) != 0) {
1841 if (linknd->nl_nch.ncp->nc_vp) {
1847 * Finally run the new API VOP.
1849 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1851 error = VOP_NLINK(&linknd->nl_nch, vp, linknd->nl_cred);
1857 * link_args(char *path, char *link)
1859 * Make a hard file link.
1862 sys_link(struct link_args *uap)
1864 struct nlookupdata nd, linknd;
1867 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1869 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1871 error = kern_link(&nd, &linknd);
1872 nlookup_done(&linknd);
1879 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1886 nd->nl_flags |= NLC_CREATE;
1887 if ((error = nlookup(nd)) != 0)
1889 if (nd->nl_nch.ncp->nc_vp)
1891 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1894 vattr.va_mode = mode;
1895 error = VOP_NSYMLINK(&nd->nl_nch, &vp, nd->nl_cred, &vattr, path);
1902 * symlink(char *path, char *link)
1904 * Make a symbolic link.
1907 sys_symlink(struct symlink_args *uap)
1909 struct thread *td = curthread;
1910 struct nlookupdata nd;
1915 path = objcache_get(namei_oc, M_WAITOK);
1916 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1918 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1920 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1921 error = kern_symlink(&nd, path, mode);
1925 objcache_put(namei_oc, path);
1930 * undelete_args(char *path)
1932 * Delete a whiteout from the filesystem.
1936 sys_undelete(struct undelete_args *uap)
1938 struct nlookupdata nd;
1941 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1943 nd.nl_flags |= NLC_DELETE;
1945 error = nlookup(&nd);
1947 error = ncp_writechk(&nd.nl_nch);
1949 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_cred, NAMEI_DELETE);
1955 kern_unlink(struct nlookupdata *nd)
1960 nd->nl_flags |= NLC_DELETE;
1961 if ((error = nlookup(nd)) != 0)
1963 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1965 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_cred);
1970 * unlink_args(char *path)
1972 * Delete a name from the filesystem.
1975 sys_unlink(struct unlink_args *uap)
1977 struct nlookupdata nd;
1980 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1982 error = kern_unlink(&nd);
1988 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1990 struct thread *td = curthread;
1991 struct proc *p = td->td_proc;
1996 fp = holdfp(p->p_fd, fd, -1);
1999 if (fp->f_type != DTYPE_VNODE) {
2006 fp->f_offset += offset;
2010 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
2012 fp->f_offset = offset + vattr.va_size;
2015 fp->f_offset = offset;
2022 *res = fp->f_offset;
2029 * lseek_args(int fd, int pad, off_t offset, int whence)
2031 * Reposition read/write file offset.
2034 sys_lseek(struct lseek_args *uap)
2038 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2039 &uap->sysmsg_offset);
2045 kern_access(struct nlookupdata *nd, int aflags)
2050 if ((error = nlookup(nd)) != 0)
2053 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2057 /* Flags == 0 means only check for existence. */
2066 if ((flags & VWRITE) == 0 ||
2067 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2068 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2071 * If the file handle is stale we have to re-resolve the
2072 * entry. This is a hack at the moment.
2074 if (error == ESTALE) {
2075 cache_setunresolved(&nd->nl_nch);
2076 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2089 * access_args(char *path, int flags)
2091 * Check access permissions.
2094 sys_access(struct access_args *uap)
2096 struct nlookupdata nd;
2099 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2101 error = kern_access(&nd, uap->flags);
2107 kern_stat(struct nlookupdata *nd, struct stat *st)
2113 if ((error = nlookup(nd)) != 0)
2116 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2120 if ((error = vget(vp, LK_SHARED)) != 0)
2122 error = vn_stat(vp, st, nd->nl_cred);
2125 * If the file handle is stale we have to re-resolve the entry. This
2126 * is a hack at the moment.
2128 if (error == ESTALE) {
2129 cache_setunresolved(&nd->nl_nch);
2130 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2141 * stat_args(char *path, struct stat *ub)
2143 * Get file status; this version follows links.
2146 sys_stat(struct stat_args *uap)
2148 struct nlookupdata nd;
2152 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2154 error = kern_stat(&nd, &st);
2156 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2163 * lstat_args(char *path, struct stat *ub)
2165 * Get file status; this version does not follow links.
2168 sys_lstat(struct lstat_args *uap)
2170 struct nlookupdata nd;
2174 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2176 error = kern_stat(&nd, &st);
2178 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2185 * pathconf_Args(char *path, int name)
2187 * Get configurable pathname variables.
2191 sys_pathconf(struct pathconf_args *uap)
2193 struct nlookupdata nd;
2198 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2200 error = nlookup(&nd);
2202 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2205 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2213 * kern_readlink isn't properly split yet. There is a copyin burried
2214 * in VOP_READLINK().
2217 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2219 struct thread *td = curthread;
2220 struct proc *p = td->td_proc;
2226 if ((error = nlookup(nd)) != 0)
2228 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2231 if (vp->v_type != VLNK) {
2234 aiov.iov_base = buf;
2235 aiov.iov_len = count;
2236 auio.uio_iov = &aiov;
2237 auio.uio_iovcnt = 1;
2238 auio.uio_offset = 0;
2239 auio.uio_rw = UIO_READ;
2240 auio.uio_segflg = UIO_USERSPACE;
2242 auio.uio_resid = count;
2243 error = VOP_READLINK(vp, &auio, p->p_ucred);
2246 *res = count - auio.uio_resid;
2251 * readlink_args(char *path, char *buf, int count)
2253 * Return target name of a symbolic link.
2256 sys_readlink(struct readlink_args *uap)
2258 struct nlookupdata nd;
2261 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2263 error = kern_readlink(&nd, uap->buf, uap->count,
2264 &uap->sysmsg_result);
2271 setfflags(struct vnode *vp, int flags)
2273 struct thread *td = curthread;
2274 struct proc *p = td->td_proc;
2279 * Prevent non-root users from setting flags on devices. When
2280 * a device is reused, users can retain ownership of the device
2281 * if they are allowed to set flags and programs assume that
2282 * chown can't fail when done as root.
2284 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2285 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2289 * note: vget is required for any operation that might mod the vnode
2290 * so VINACTIVE is properly cleared.
2292 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2294 vattr.va_flags = flags;
2295 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2302 * chflags(char *path, int flags)
2304 * Change flags of a file given a path name.
2308 sys_chflags(struct chflags_args *uap)
2310 struct nlookupdata nd;
2315 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2316 /* XXX Add NLC flag indicating modifying operation? */
2318 error = nlookup(&nd);
2320 error = ncp_writechk(&nd.nl_nch);
2322 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2325 error = setfflags(vp, uap->flags);
2332 * fchflags_args(int fd, int flags)
2334 * Change flags of a file given a file descriptor.
2338 sys_fchflags(struct fchflags_args *uap)
2340 struct thread *td = curthread;
2341 struct proc *p = td->td_proc;
2345 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2347 if (fp->f_nchandle.ncp)
2348 error = ncp_writechk(&fp->f_nchandle);
2350 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2356 setfmode(struct vnode *vp, int mode)
2358 struct thread *td = curthread;
2359 struct proc *p = td->td_proc;
2364 * note: vget is required for any operation that might mod the vnode
2365 * so VINACTIVE is properly cleared.
2367 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2369 vattr.va_mode = mode & ALLPERMS;
2370 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2377 kern_chmod(struct nlookupdata *nd, int mode)
2382 /* XXX Add NLC flag indicating modifying operation? */
2383 if ((error = nlookup(nd)) != 0)
2385 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2387 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2388 error = setfmode(vp, mode);
2394 * chmod_args(char *path, int mode)
2396 * Change mode of a file given path name.
2400 sys_chmod(struct chmod_args *uap)
2402 struct nlookupdata nd;
2405 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2407 error = kern_chmod(&nd, uap->mode);
2413 * lchmod_args(char *path, int mode)
2415 * Change mode of a file given path name (don't follow links.)
2419 sys_lchmod(struct lchmod_args *uap)
2421 struct nlookupdata nd;
2424 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2426 error = kern_chmod(&nd, uap->mode);
2432 * fchmod_args(int fd, int mode)
2434 * Change mode of a file given a file descriptor.
2438 sys_fchmod(struct fchmod_args *uap)
2440 struct thread *td = curthread;
2441 struct proc *p = td->td_proc;
2445 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2447 if (fp->f_nchandle.ncp)
2448 error = ncp_writechk(&fp->f_nchandle);
2450 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2456 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2458 struct thread *td = curthread;
2459 struct proc *p = td->td_proc;
2464 * note: vget is required for any operation that might mod the vnode
2465 * so VINACTIVE is properly cleared.
2467 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2471 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2478 kern_chown(struct nlookupdata *nd, int uid, int gid)
2483 /* XXX Add NLC flag indicating modifying operation? */
2484 if ((error = nlookup(nd)) != 0)
2486 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2488 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2489 error = setfown(vp, uid, gid);
2495 * chown(char *path, int uid, int gid)
2497 * Set ownership given a path name.
2500 sys_chown(struct chown_args *uap)
2502 struct nlookupdata nd;
2505 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2507 error = kern_chown(&nd, uap->uid, uap->gid);
2513 * lchown_args(char *path, int uid, int gid)
2515 * Set ownership given a path name, do not cross symlinks.
2518 sys_lchown(struct lchown_args *uap)
2520 struct nlookupdata nd;
2523 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2525 error = kern_chown(&nd, uap->uid, uap->gid);
2531 * fchown_args(int fd, int uid, int gid)
2533 * Set ownership given a file descriptor.
2537 sys_fchown(struct fchown_args *uap)
2539 struct thread *td = curthread;
2540 struct proc *p = td->td_proc;
2544 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2546 if (fp->f_nchandle.ncp)
2547 error = ncp_writechk(&fp->f_nchandle);
2549 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2555 getutimes(const struct timeval *tvp, struct timespec *tsp)
2557 struct timeval tv[2];
2561 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2564 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2565 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2571 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2573 struct thread *td = curthread;
2574 struct proc *p = td->td_proc;
2579 * note: vget is required for any operation that might mod the vnode
2580 * so VINACTIVE is properly cleared.
2582 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2584 vattr.va_atime = ts[0];
2585 vattr.va_mtime = ts[1];
2587 vattr.va_vaflags |= VA_UTIMES_NULL;
2588 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2595 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2597 struct timespec ts[2];
2601 if ((error = getutimes(tptr, ts)) != 0)
2603 /* XXX Add NLC flag indicating modifying operation? */
2604 if ((error = nlookup(nd)) != 0)
2606 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2608 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2610 error = setutimes(vp, ts, tptr == NULL);
2616 * utimes_args(char *path, struct timeval *tptr)
2618 * Set the access and modification times of a file.
2621 sys_utimes(struct utimes_args *uap)
2623 struct timeval tv[2];
2624 struct nlookupdata nd;
2628 error = copyin(uap->tptr, tv, sizeof(tv));
2632 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2634 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2640 * lutimes_args(char *path, struct timeval *tptr)
2642 * Set the access and modification times of a file.
2645 sys_lutimes(struct lutimes_args *uap)
2647 struct timeval tv[2];
2648 struct nlookupdata nd;
2652 error = copyin(uap->tptr, tv, sizeof(tv));
2656 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2658 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2664 kern_futimes(int fd, struct timeval *tptr)
2666 struct thread *td = curthread;
2667 struct proc *p = td->td_proc;
2668 struct timespec ts[2];
2672 error = getutimes(tptr, ts);
2675 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2677 if (fp->f_nchandle.ncp)
2678 error = ncp_writechk(&fp->f_nchandle);
2680 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2686 * futimes_args(int fd, struct timeval *tptr)
2688 * Set the access and modification times of a file.
2691 sys_futimes(struct futimes_args *uap)
2693 struct timeval tv[2];
2697 error = copyin(uap->tptr, tv, sizeof(tv));
2702 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2708 kern_truncate(struct nlookupdata *nd, off_t length)
2716 /* XXX Add NLC flag indicating modifying operation? */
2717 if ((error = nlookup(nd)) != 0)
2719 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2721 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2723 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2727 if (vp->v_type == VDIR) {
2729 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
2730 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2732 vattr.va_size = length;
2733 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2740 * truncate(char *path, int pad, off_t length)
2742 * Truncate a file given its path name.
2745 sys_truncate(struct truncate_args *uap)
2747 struct nlookupdata nd;
2750 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2752 error = kern_truncate(&nd, uap->length);
2758 kern_ftruncate(int fd, off_t length)
2760 struct thread *td = curthread;
2761 struct proc *p = td->td_proc;
2769 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2771 if (fp->f_nchandle.ncp) {
2772 error = ncp_writechk(&fp->f_nchandle);
2776 if ((fp->f_flag & FWRITE) == 0) {
2780 vp = (struct vnode *)fp->f_data;
2781 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2782 if (vp->v_type == VDIR) {
2784 } else if ((error = vn_writechk(vp, NULL)) == 0) {
2786 vattr.va_size = length;
2787 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2796 * ftruncate_args(int fd, int pad, off_t length)
2798 * Truncate a file given a file descriptor.
2801 sys_ftruncate(struct ftruncate_args *uap)
2805 error = kern_ftruncate(uap->fd, uap->length);
2813 * Sync an open file.
2817 sys_fsync(struct fsync_args *uap)
2819 struct thread *td = curthread;
2820 struct proc *p = td->td_proc;
2826 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2828 vp = (struct vnode *)fp->f_data;
2829 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2830 if ((obj = vp->v_object) != NULL)
2831 vm_object_page_clean(obj, 0, 0, 0);
2832 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2833 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2835 error = (*bioops.io_fsync)(vp);
2843 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2845 struct nchandle fnchd;
2846 struct nchandle tnchd;
2847 struct namecache *ncp;
2852 if ((error = nlookup(fromnd)) != 0)
2854 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
2856 fnchd.mount = fromnd->nl_nch.mount;
2860 * unlock the source nch so we can lookup the target nch without
2861 * deadlocking. The target may or may not exist so we do not check
2862 * for a target vp like kern_mkdir() and other creation functions do.
2864 * The source and target directories are ref'd and rechecked after
2865 * everything is relocked to determine if the source or target file
2868 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2869 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2870 cache_unlock(&fromnd->nl_nch);
2872 tond->nl_flags |= NLC_CREATE;
2873 if ((error = nlookup(tond)) != 0) {
2877 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
2881 tnchd.mount = tond->nl_nch.mount;
2885 * If the source and target are the same there is nothing to do
2887 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
2894 * Mount points cannot be renamed or overwritten
2896 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
2905 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2906 * may have become invalid while it was unlocked, nc_vp and nc_mount
2909 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
2910 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2911 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
2912 cache_lock(&fromnd->nl_nch);
2913 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2915 cache_unlock(&tond->nl_nch);
2916 cache_lock(&fromnd->nl_nch);
2917 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2918 cache_lock(&tond->nl_nch);
2919 cache_resolve(&tond->nl_nch, tond->nl_cred);
2921 fromnd->nl_flags |= NLC_NCPISLOCKED;
2924 * make sure the parent directories linkages are the same
2926 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
2927 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
2934 * Both the source and target must be within the same filesystem and
2935 * in the same filesystem as their parent directories within the
2936 * namecache topology.
2938 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2941 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
2942 mp != tond->nl_nch.mount) {
2949 * Make sure the mount point is writable
2951 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
2958 * If the target exists and either the source or target is a directory,
2959 * then both must be directories.
2961 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
2964 if (tond->nl_nch.ncp->nc_vp) {
2965 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
2967 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
2968 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
2970 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
2976 * You cannot rename a source into itself or a subdirectory of itself.
2977 * We check this by travsersing the target directory upwards looking
2978 * for a match against the source.
2981 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
2982 if (fromnd->nl_nch.ncp == ncp) {
2993 * Even though the namespaces are different, they may still represent
2994 * hardlinks to the same file. The filesystem might have a hard time
2995 * with this so we issue a NREMOVE of the source instead of a NRENAME
2996 * when we detect the situation.
2999 if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3000 error = VOP_NREMOVE(&fromnd->nl_nch, fromnd->nl_cred);
3002 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3010 * rename_args(char *from, char *to)
3012 * Rename files. Source and destination must either both be directories,
3013 * or both not be directories. If target is a directory, it must be empty.
3016 sys_rename(struct rename_args *uap)
3018 struct nlookupdata fromnd, tond;
3021 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3023 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3025 error = kern_rename(&fromnd, &tond);
3026 nlookup_done(&tond);
3028 nlookup_done(&fromnd);
3033 kern_mkdir(struct nlookupdata *nd, int mode)
3035 struct thread *td = curthread;
3036 struct proc *p = td->td_proc;
3042 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
3043 if ((error = nlookup(nd)) != 0)
3046 if (nd->nl_nch.ncp->nc_vp)
3048 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3052 vattr.va_type = VDIR;
3053 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3056 error = VOP_NMKDIR(&nd->nl_nch, &vp, p->p_ucred, &vattr);
3063 * mkdir_args(char *path, int mode)
3065 * Make a directory file.
3069 sys_mkdir(struct mkdir_args *uap)
3071 struct nlookupdata nd;
3074 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3076 error = kern_mkdir(&nd, uap->mode);
3082 kern_rmdir(struct nlookupdata *nd)
3087 nd->nl_flags |= NLC_DELETE;
3088 if ((error = nlookup(nd)) != 0)
3092 * Do not allow directories representing mount points to be
3093 * deleted, even if empty. Check write perms on mount point
3094 * in case the vnode is aliased (aka nullfs).
3096 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3098 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3101 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_cred);
3106 * rmdir_args(char *path)
3108 * Remove a directory file.
3112 sys_rmdir(struct rmdir_args *uap)
3114 struct nlookupdata nd;
3117 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3119 error = kern_rmdir(&nd);
3125 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3126 enum uio_seg direction)
3128 struct thread *td = curthread;
3129 struct proc *p = td->td_proc;
3137 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3139 if ((fp->f_flag & FREAD) == 0) {
3143 vp = (struct vnode *)fp->f_data;
3145 if (vp->v_type != VDIR) {
3149 aiov.iov_base = buf;
3150 aiov.iov_len = count;
3151 auio.uio_iov = &aiov;
3152 auio.uio_iovcnt = 1;
3153 auio.uio_rw = UIO_READ;
3154 auio.uio_segflg = direction;
3156 auio.uio_resid = count;
3157 loff = auio.uio_offset = fp->f_offset;
3158 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3159 fp->f_offset = auio.uio_offset;
3162 if (count == auio.uio_resid) {
3163 if (union_dircheckp) {
3164 error = union_dircheckp(td, &vp, fp);
3171 if ((vp->v_flag & VROOT) &&
3172 (vp->v_mount->mnt_flag & MNT_UNION)) {
3173 struct vnode *tvp = vp;
3174 vp = vp->v_mount->mnt_vnodecovered;
3186 *res = count - auio.uio_resid;
3193 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3195 * Read a block of directory entries in a file system independent format.
3198 sys_getdirentries(struct getdirentries_args *uap)
3203 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3204 &uap->sysmsg_result, UIO_USERSPACE);
3207 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3212 * getdents_args(int fd, char *buf, size_t count)
3215 sys_getdents(struct getdents_args *uap)
3219 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3220 &uap->sysmsg_result, UIO_USERSPACE);
3226 * umask(int newmask)
3228 * Set the mode mask for creation of filesystem nodes.
3233 sys_umask(struct umask_args *uap)
3235 struct thread *td = curthread;
3236 struct proc *p = td->td_proc;
3237 struct filedesc *fdp;
3240 uap->sysmsg_result = fdp->fd_cmask;
3241 fdp->fd_cmask = uap->newmask & ALLPERMS;
3246 * revoke(char *path)
3248 * Void all references to file by ripping underlying filesystem
3253 sys_revoke(struct revoke_args *uap)
3255 struct nlookupdata nd;
3262 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3264 error = nlookup(&nd);
3266 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3267 cred = crhold(nd.nl_cred);
3270 if (vp->v_type != VCHR && vp->v_type != VBLK)
3273 error = VOP_GETATTR(vp, &vattr);
3274 if (error == 0 && cred->cr_uid != vattr.va_uid)
3275 error = suser_cred(cred, PRISON_ROOT);
3276 if (error == 0 && count_udev(vp->v_umajor, vp->v_uminor) > 0) {
3279 VOP_REVOKE(vp, REVOKEALL);
3290 * getfh_args(char *fname, fhandle_t *fhp)
3292 * Get (NFS) file handle
3295 sys_getfh(struct getfh_args *uap)
3297 struct thread *td = curthread;
3298 struct nlookupdata nd;
3304 * Must be super user
3306 if ((error = suser(td)) != 0)
3310 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3312 error = nlookup(&nd);
3314 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3317 bzero(&fh, sizeof(fh));
3318 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3319 error = VFS_VPTOFH(vp, &fh.fh_fid);
3322 error = copyout(&fh, uap->fhp, sizeof(fh));
3328 * fhopen_args(const struct fhandle *u_fhp, int flags)
3330 * syscall for the rpc.lockd to use to translate a NFS file handle into
3331 * an open descriptor.
3333 * warning: do not remove the suser() call or this becomes one giant
3337 sys_fhopen(struct fhopen_args *uap)
3339 struct thread *td = curthread;
3340 struct proc *p = td->td_proc;
3345 struct vattr *vap = &vat;
3347 int fmode, mode, error, type;
3353 * Must be super user
3359 fmode = FFLAGS(uap->flags);
3360 /* why not allow a non-read/write open for our lockd? */
3361 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3363 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3366 /* find the mount point */
3367 mp = vfs_getvfs(&fhp.fh_fsid);
3370 /* now give me my vnode, it gets returned to me locked */
3371 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3375 * from now on we have to make sure not
3376 * to forget about the vnode
3377 * any error that causes an abort must vput(vp)
3378 * just set error = err and 'goto bad;'.
3384 if (vp->v_type == VLNK) {
3388 if (vp->v_type == VSOCK) {
3393 if (fmode & (FWRITE | O_TRUNC)) {
3394 if (vp->v_type == VDIR) {
3398 error = vn_writechk(vp, NULL);
3406 error = VOP_ACCESS(vp, mode, p->p_ucred);
3410 if (fmode & O_TRUNC) {
3411 vn_unlock(vp); /* XXX */
3412 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3415 error = VOP_SETATTR(vp, vap, p->p_ucred);
3421 * VOP_OPEN needs the file pointer so it can potentially override
3424 * WARNING! no f_nchandle will be associated when fhopen()ing a
3427 if ((error = falloc(p, &nfp, &indx)) != 0)
3431 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3434 * setting f_ops this way prevents VOP_CLOSE from being
3435 * called or fdrop() releasing the vp from v_data. Since
3436 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3438 fp->f_ops = &badfileops;
3444 * The fp is given its own reference, we still have our ref and lock.
3446 * Assert that all regular files must be created with a VM object.
3448 if (vp->v_type == VREG && vp->v_object == NULL) {
3449 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3454 * The open was successful. Handle any locking requirements.
3456 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3457 lf.l_whence = SEEK_SET;
3460 if (fmode & O_EXLOCK)
3461 lf.l_type = F_WRLCK;
3463 lf.l_type = F_RDLCK;
3464 if (fmode & FNONBLOCK)
3469 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3471 * release our private reference.
3473 fsetfd(p, NULL, indx);
3478 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3479 fp->f_flag |= FHASLOCK;
3483 * Clean up. Associate the file pointer with the previously
3484 * reserved descriptor and return it.
3487 fsetfd(p, fp, indx);
3489 uap->sysmsg_result = indx;
3493 fsetfd(p, NULL, indx);
3501 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3504 sys_fhstat(struct fhstat_args *uap)
3506 struct thread *td = curthread;
3514 * Must be super user
3520 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3524 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3526 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3528 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3532 error = copyout(&sb, uap->sb, sizeof(sb));
3537 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3540 sys_fhstatfs(struct fhstatfs_args *uap)
3542 struct thread *td = curthread;
3543 struct proc *p = td->td_proc;
3548 char *fullpath, *freepath;
3553 * Must be super user
3555 if ((error = suser(td)))
3558 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3561 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3564 if (p != NULL && !chroot_visible_mnt(mp, p))
3567 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3572 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3575 error = mount_path(p, mp, &fullpath, &freepath);
3578 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3579 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3580 kfree(freepath, M_TEMP);
3582 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3584 bcopy(sp, &sb, sizeof(sb));
3585 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3588 return (copyout(sp, uap->buf, sizeof(*sp)));
3592 * Syscall to push extended attribute configuration information into the
3593 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3594 * a command (int cmd), and attribute name and misc data. For now, the
3595 * attribute name is left in userspace for consumption by the VFS_op.
3596 * It will probably be changed to be copied into sysspace by the
3597 * syscall in the future, once issues with various consumers of the
3598 * attribute code have raised their hands.
3600 * Currently this is used only by UFS Extended Attributes.
3603 sys_extattrctl(struct extattrctl_args *uap)
3605 struct nlookupdata nd;
3611 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3613 error = nlookup(&nd);
3615 mp = nd.nl_nch.mount;
3616 error = VFS_EXTATTRCTL(mp, uap->cmd,
3617 uap->attrname, uap->arg,
3625 * Syscall to set a named extended attribute on a file or directory.
3626 * Accepts attribute name, and a uio structure pointing to the data to set.
3627 * The uio is consumed in the style of writev(). The real work happens
3628 * in VOP_SETEXTATTR().
3631 sys_extattr_set_file(struct extattr_set_file_args *uap)
3633 char attrname[EXTATTR_MAXNAMELEN];
3634 struct iovec aiov[UIO_SMALLIOV];
3635 struct iovec *needfree;
3636 struct nlookupdata nd;
3645 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3650 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3652 error = nlookup(&nd);
3654 error = ncp_writechk(&nd.nl_nch);
3656 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3663 iovlen = uap->iovcnt * sizeof(struct iovec);
3664 if (uap->iovcnt > UIO_SMALLIOV) {
3665 if (uap->iovcnt > UIO_MAXIOV) {
3669 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3675 auio.uio_iovcnt = uap->iovcnt;
3676 auio.uio_rw = UIO_WRITE;
3677 auio.uio_segflg = UIO_USERSPACE;
3678 auio.uio_td = nd.nl_td;
3679 auio.uio_offset = 0;
3680 if ((error = copyin(uap->iovp, iov, iovlen)))
3683 for (i = 0; i < uap->iovcnt; i++) {
3684 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3688 auio.uio_resid += iov->iov_len;
3691 cnt = auio.uio_resid;
3692 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3693 cnt -= auio.uio_resid;
3694 uap->sysmsg_result = cnt;
3699 FREE(needfree, M_IOV);
3704 * Syscall to get a named extended attribute on a file or directory.
3705 * Accepts attribute name, and a uio structure pointing to a buffer for the
3706 * data. The uio is consumed in the style of readv(). The real work
3707 * happens in VOP_GETEXTATTR();
3710 sys_extattr_get_file(struct extattr_get_file_args *uap)
3712 char attrname[EXTATTR_MAXNAMELEN];
3713 struct iovec aiov[UIO_SMALLIOV];
3714 struct iovec *needfree;
3715 struct nlookupdata nd;
3724 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3729 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3731 error = nlookup(&nd);
3733 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3739 iovlen = uap->iovcnt * sizeof (struct iovec);
3741 if (uap->iovcnt > UIO_SMALLIOV) {
3742 if (uap->iovcnt > UIO_MAXIOV) {
3746 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3752 auio.uio_iovcnt = uap->iovcnt;
3753 auio.uio_rw = UIO_READ;
3754 auio.uio_segflg = UIO_USERSPACE;
3755 auio.uio_td = nd.nl_td;
3756 auio.uio_offset = 0;
3757 if ((error = copyin(uap->iovp, iov, iovlen)))
3760 for (i = 0; i < uap->iovcnt; i++) {
3761 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3765 auio.uio_resid += iov->iov_len;
3768 cnt = auio.uio_resid;
3769 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3770 cnt -= auio.uio_resid;
3771 uap->sysmsg_result = cnt;
3776 FREE(needfree, M_IOV);
3781 * Syscall to delete a named extended attribute from a file or directory.
3782 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3785 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
3787 char attrname[EXTATTR_MAXNAMELEN];
3788 struct nlookupdata nd;
3792 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3797 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3799 error = nlookup(&nd);
3801 error = ncp_writechk(&nd.nl_nch);
3803 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3809 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3816 * Determine if the mount is visible to the process.
3819 chroot_visible_mnt(struct mount *mp, struct proc *p)
3821 struct nchandle nch;
3824 * Traverse from the mount point upwards. If we hit the process
3825 * root then the mount point is visible to the process.
3827 nch = mp->mnt_ncmountpt;
3829 if (nch.mount == p->p_fd->fd_nrdir.mount &&
3830 nch.ncp == p->p_fd->fd_nrdir.ncp) {
3833 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
3834 nch = nch.mount->mnt_ncmounton;
3836 nch.ncp = nch.ncp->nc_parent;
3841 * If the mount point is not visible to the process, but the
3842 * process root is in a subdirectory of the mount, return
3845 if (p->p_fd->fd_nrdir.mount == mp)