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.119 2007/08/13 17:43:55 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;
1629 switch (mode & S_IFMT) {
1635 error = suser_cred(p->p_ucred, PRISON_ROOT);
1642 nd->nl_flags |= NLC_CREATE;
1643 if ((error = nlookup(nd)) != 0)
1645 if (nd->nl_nch.ncp->nc_vp)
1647 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1649 if ((dvp = nd->nl_nch.ncp->nc_parent->nc_vp) == NULL)
1651 /* vhold(dvp); - DVP can't go away */
1654 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1655 vattr.va_rmajor = rmajor;
1656 vattr.va_rminor = rminor;
1659 switch (mode & S_IFMT) {
1660 case S_IFMT: /* used by badsect to flag bad sectors */
1661 vattr.va_type = VBAD;
1664 vattr.va_type = VCHR;
1667 vattr.va_type = VBLK;
1678 error = VOP_NWHITEOUT(&nd->nl_nch, dvp, nd->nl_cred, NAMEI_CREATE);
1681 error = VOP_NMKNOD(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr);
1691 * mknod_args(char *path, int mode, int dev)
1693 * Create a special file.
1696 sys_mknod(struct mknod_args *uap)
1698 struct nlookupdata nd;
1701 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1703 error = kern_mknod(&nd, uap->mode,
1704 umajor(uap->dev), uminor(uap->dev));
1711 kern_mkfifo(struct nlookupdata *nd, int mode)
1713 struct thread *td = curthread;
1714 struct proc *p = td->td_proc;
1722 nd->nl_flags |= NLC_CREATE;
1723 if ((error = nlookup(nd)) != 0)
1725 if (nd->nl_nch.ncp->nc_vp)
1727 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1729 if ((dvp = nd->nl_nch.ncp->nc_parent->nc_vp) == NULL)
1731 /* vhold(dvp); - DVP can't go away */
1734 vattr.va_type = VFIFO;
1735 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1737 error = VOP_NMKNOD(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr);
1745 * mkfifo_args(char *path, int mode)
1747 * Create a named pipe.
1750 sys_mkfifo(struct mkfifo_args *uap)
1752 struct nlookupdata nd;
1755 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1757 error = kern_mkfifo(&nd, uap->mode);
1762 static int hardlink_check_uid = 0;
1763 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1764 &hardlink_check_uid, 0,
1765 "Unprivileged processes cannot create hard links to files owned by other "
1767 static int hardlink_check_gid = 0;
1768 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1769 &hardlink_check_gid, 0,
1770 "Unprivileged processes cannot create hard links to files owned by other "
1774 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1780 * Shortcut if disabled
1782 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1786 * root cred can always hardlink
1788 if (suser_cred(cred, PRISON_ROOT) == 0)
1792 * Otherwise only if the originating file is owned by the
1793 * same user or group. Note that any group is allowed if
1794 * the file is owned by the caller.
1796 error = VOP_GETATTR(vp, &va);
1800 if (hardlink_check_uid) {
1801 if (cred->cr_uid != va.va_uid)
1805 if (hardlink_check_gid) {
1806 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1814 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1816 struct thread *td = curthread;
1822 * Lookup the source and obtained a locked vnode.
1824 * XXX relookup on vget failure / race ?
1827 if ((error = nlookup(nd)) != 0)
1829 vp = nd->nl_nch.ncp->nc_vp;
1830 KKASSERT(vp != NULL);
1831 if (vp->v_type == VDIR)
1832 return (EPERM); /* POSIX */
1833 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1835 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1839 * Unlock the source so we can lookup the target without deadlocking
1840 * (XXX vp is locked already, possible other deadlock?). The target
1843 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1844 nd->nl_flags &= ~NLC_NCPISLOCKED;
1845 cache_unlock(&nd->nl_nch);
1847 linknd->nl_flags |= NLC_CREATE;
1848 if ((error = nlookup(linknd)) != 0) {
1852 if (linknd->nl_nch.ncp->nc_vp) {
1856 if ((dvp = linknd->nl_nch.ncp->nc_parent->nc_vp) == NULL) {
1860 /* vhold(dvp); - dvp can't go away */
1863 * Finally run the new API VOP.
1865 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1867 error = VOP_NLINK(&linknd->nl_nch, dvp, vp, linknd->nl_cred);
1874 * link_args(char *path, char *link)
1876 * Make a hard file link.
1879 sys_link(struct link_args *uap)
1881 struct nlookupdata nd, linknd;
1884 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1886 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1888 error = kern_link(&nd, &linknd);
1889 nlookup_done(&linknd);
1896 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1904 nd->nl_flags |= NLC_CREATE;
1905 if ((error = nlookup(nd)) != 0)
1907 if (nd->nl_nch.ncp->nc_vp)
1909 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1911 if ((dvp = nd->nl_nch.ncp->nc_parent->nc_vp) == NULL)
1913 /* vhold(dvp); - dvp can't go away */
1915 vattr.va_mode = mode;
1916 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
1924 * symlink(char *path, char *link)
1926 * Make a symbolic link.
1929 sys_symlink(struct symlink_args *uap)
1931 struct thread *td = curthread;
1932 struct nlookupdata nd;
1937 path = objcache_get(namei_oc, M_WAITOK);
1938 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1940 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1942 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1943 error = kern_symlink(&nd, path, mode);
1947 objcache_put(namei_oc, path);
1952 * undelete_args(char *path)
1954 * Delete a whiteout from the filesystem.
1958 sys_undelete(struct undelete_args *uap)
1960 struct nlookupdata nd;
1964 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1966 nd.nl_flags |= NLC_DELETE;
1968 error = nlookup(&nd);
1970 error = ncp_writechk(&nd.nl_nch);
1973 if ((dvp = nd.nl_nch.ncp->nc_parent->nc_vp) == NULL)
1977 /* vhold(dvp); - dvp can't go away */
1978 error = VOP_NWHITEOUT(&nd.nl_nch, dvp, nd.nl_cred, NAMEI_DELETE);
1986 kern_unlink(struct nlookupdata *nd)
1992 nd->nl_flags |= NLC_DELETE;
1993 if ((error = nlookup(nd)) != 0)
1995 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1997 if ((dvp = nd->nl_nch.ncp->nc_parent->nc_vp) == NULL)
1999 /* vhold(dvp); - dvp can't go away */
2000 error = VOP_NREMOVE(&nd->nl_nch, dvp, nd->nl_cred);
2006 * unlink_args(char *path)
2008 * Delete a name from the filesystem.
2011 sys_unlink(struct unlink_args *uap)
2013 struct nlookupdata nd;
2016 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2018 error = kern_unlink(&nd);
2024 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2026 struct thread *td = curthread;
2027 struct proc *p = td->td_proc;
2032 fp = holdfp(p->p_fd, fd, -1);
2035 if (fp->f_type != DTYPE_VNODE) {
2042 fp->f_offset += offset;
2046 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
2048 fp->f_offset = offset + vattr.va_size;
2051 fp->f_offset = offset;
2058 *res = fp->f_offset;
2065 * lseek_args(int fd, int pad, off_t offset, int whence)
2067 * Reposition read/write file offset.
2070 sys_lseek(struct lseek_args *uap)
2074 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2075 &uap->sysmsg_offset);
2081 kern_access(struct nlookupdata *nd, int aflags)
2086 if ((error = nlookup(nd)) != 0)
2089 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2093 /* Flags == 0 means only check for existence. */
2102 if ((flags & VWRITE) == 0 ||
2103 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2104 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2107 * If the file handle is stale we have to re-resolve the
2108 * entry. This is a hack at the moment.
2110 if (error == ESTALE) {
2111 cache_setunresolved(&nd->nl_nch);
2112 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2125 * access_args(char *path, int flags)
2127 * Check access permissions.
2130 sys_access(struct access_args *uap)
2132 struct nlookupdata nd;
2135 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2137 error = kern_access(&nd, uap->flags);
2143 kern_stat(struct nlookupdata *nd, struct stat *st)
2149 if ((error = nlookup(nd)) != 0)
2152 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2156 if ((error = vget(vp, LK_SHARED)) != 0)
2158 error = vn_stat(vp, st, nd->nl_cred);
2161 * If the file handle is stale we have to re-resolve the entry. This
2162 * is a hack at the moment.
2164 if (error == ESTALE) {
2165 cache_setunresolved(&nd->nl_nch);
2166 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2177 * stat_args(char *path, struct stat *ub)
2179 * Get file status; this version follows links.
2182 sys_stat(struct stat_args *uap)
2184 struct nlookupdata nd;
2188 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2190 error = kern_stat(&nd, &st);
2192 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2199 * lstat_args(char *path, struct stat *ub)
2201 * Get file status; this version does not follow links.
2204 sys_lstat(struct lstat_args *uap)
2206 struct nlookupdata nd;
2210 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2212 error = kern_stat(&nd, &st);
2214 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2221 * pathconf_Args(char *path, int name)
2223 * Get configurable pathname variables.
2227 sys_pathconf(struct pathconf_args *uap)
2229 struct nlookupdata nd;
2234 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2236 error = nlookup(&nd);
2238 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2241 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2249 * kern_readlink isn't properly split yet. There is a copyin burried
2250 * in VOP_READLINK().
2253 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2255 struct thread *td = curthread;
2256 struct proc *p = td->td_proc;
2262 if ((error = nlookup(nd)) != 0)
2264 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2267 if (vp->v_type != VLNK) {
2270 aiov.iov_base = buf;
2271 aiov.iov_len = count;
2272 auio.uio_iov = &aiov;
2273 auio.uio_iovcnt = 1;
2274 auio.uio_offset = 0;
2275 auio.uio_rw = UIO_READ;
2276 auio.uio_segflg = UIO_USERSPACE;
2278 auio.uio_resid = count;
2279 error = VOP_READLINK(vp, &auio, p->p_ucred);
2282 *res = count - auio.uio_resid;
2287 * readlink_args(char *path, char *buf, int count)
2289 * Return target name of a symbolic link.
2292 sys_readlink(struct readlink_args *uap)
2294 struct nlookupdata nd;
2297 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2299 error = kern_readlink(&nd, uap->buf, uap->count,
2300 &uap->sysmsg_result);
2307 setfflags(struct vnode *vp, int flags)
2309 struct thread *td = curthread;
2310 struct proc *p = td->td_proc;
2315 * Prevent non-root users from setting flags on devices. When
2316 * a device is reused, users can retain ownership of the device
2317 * if they are allowed to set flags and programs assume that
2318 * chown can't fail when done as root.
2320 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2321 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2325 * note: vget is required for any operation that might mod the vnode
2326 * so VINACTIVE is properly cleared.
2328 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2330 vattr.va_flags = flags;
2331 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2338 * chflags(char *path, int flags)
2340 * Change flags of a file given a path name.
2344 sys_chflags(struct chflags_args *uap)
2346 struct nlookupdata nd;
2351 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2352 /* XXX Add NLC flag indicating modifying operation? */
2354 error = nlookup(&nd);
2356 error = ncp_writechk(&nd.nl_nch);
2358 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2361 error = setfflags(vp, uap->flags);
2368 * fchflags_args(int fd, int flags)
2370 * Change flags of a file given a file descriptor.
2374 sys_fchflags(struct fchflags_args *uap)
2376 struct thread *td = curthread;
2377 struct proc *p = td->td_proc;
2381 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2383 if (fp->f_nchandle.ncp)
2384 error = ncp_writechk(&fp->f_nchandle);
2386 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2392 setfmode(struct vnode *vp, int mode)
2394 struct thread *td = curthread;
2395 struct proc *p = td->td_proc;
2400 * note: vget is required for any operation that might mod the vnode
2401 * so VINACTIVE is properly cleared.
2403 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2405 vattr.va_mode = mode & ALLPERMS;
2406 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2413 kern_chmod(struct nlookupdata *nd, int mode)
2418 /* XXX Add NLC flag indicating modifying operation? */
2419 if ((error = nlookup(nd)) != 0)
2421 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2423 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2424 error = setfmode(vp, mode);
2430 * chmod_args(char *path, int mode)
2432 * Change mode of a file given path name.
2436 sys_chmod(struct chmod_args *uap)
2438 struct nlookupdata nd;
2441 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2443 error = kern_chmod(&nd, uap->mode);
2449 * lchmod_args(char *path, int mode)
2451 * Change mode of a file given path name (don't follow links.)
2455 sys_lchmod(struct lchmod_args *uap)
2457 struct nlookupdata nd;
2460 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2462 error = kern_chmod(&nd, uap->mode);
2468 * fchmod_args(int fd, int mode)
2470 * Change mode of a file given a file descriptor.
2474 sys_fchmod(struct fchmod_args *uap)
2476 struct thread *td = curthread;
2477 struct proc *p = td->td_proc;
2481 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2483 if (fp->f_nchandle.ncp)
2484 error = ncp_writechk(&fp->f_nchandle);
2486 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2492 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2494 struct thread *td = curthread;
2495 struct proc *p = td->td_proc;
2500 * note: vget is required for any operation that might mod the vnode
2501 * so VINACTIVE is properly cleared.
2503 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2507 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2514 kern_chown(struct nlookupdata *nd, int uid, int gid)
2519 /* XXX Add NLC flag indicating modifying operation? */
2520 if ((error = nlookup(nd)) != 0)
2522 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2524 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2525 error = setfown(vp, uid, gid);
2531 * chown(char *path, int uid, int gid)
2533 * Set ownership given a path name.
2536 sys_chown(struct chown_args *uap)
2538 struct nlookupdata nd;
2541 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2543 error = kern_chown(&nd, uap->uid, uap->gid);
2549 * lchown_args(char *path, int uid, int gid)
2551 * Set ownership given a path name, do not cross symlinks.
2554 sys_lchown(struct lchown_args *uap)
2556 struct nlookupdata nd;
2559 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2561 error = kern_chown(&nd, uap->uid, uap->gid);
2567 * fchown_args(int fd, int uid, int gid)
2569 * Set ownership given a file descriptor.
2573 sys_fchown(struct fchown_args *uap)
2575 struct thread *td = curthread;
2576 struct proc *p = td->td_proc;
2580 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2582 if (fp->f_nchandle.ncp)
2583 error = ncp_writechk(&fp->f_nchandle);
2585 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2591 getutimes(const struct timeval *tvp, struct timespec *tsp)
2593 struct timeval tv[2];
2597 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2600 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2601 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2607 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2609 struct thread *td = curthread;
2610 struct proc *p = td->td_proc;
2615 * note: vget is required for any operation that might mod the vnode
2616 * so VINACTIVE is properly cleared.
2618 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2620 vattr.va_atime = ts[0];
2621 vattr.va_mtime = ts[1];
2623 vattr.va_vaflags |= VA_UTIMES_NULL;
2624 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2631 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2633 struct timespec ts[2];
2637 if ((error = getutimes(tptr, ts)) != 0)
2639 /* XXX Add NLC flag indicating modifying operation? */
2640 if ((error = nlookup(nd)) != 0)
2642 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2644 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2646 error = setutimes(vp, ts, tptr == NULL);
2652 * utimes_args(char *path, struct timeval *tptr)
2654 * Set the access and modification times of a file.
2657 sys_utimes(struct utimes_args *uap)
2659 struct timeval tv[2];
2660 struct nlookupdata nd;
2664 error = copyin(uap->tptr, tv, sizeof(tv));
2668 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2670 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2676 * lutimes_args(char *path, struct timeval *tptr)
2678 * Set the access and modification times of a file.
2681 sys_lutimes(struct lutimes_args *uap)
2683 struct timeval tv[2];
2684 struct nlookupdata nd;
2688 error = copyin(uap->tptr, tv, sizeof(tv));
2692 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2694 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2700 kern_futimes(int fd, struct timeval *tptr)
2702 struct thread *td = curthread;
2703 struct proc *p = td->td_proc;
2704 struct timespec ts[2];
2708 error = getutimes(tptr, ts);
2711 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2713 if (fp->f_nchandle.ncp)
2714 error = ncp_writechk(&fp->f_nchandle);
2716 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2722 * futimes_args(int fd, struct timeval *tptr)
2724 * Set the access and modification times of a file.
2727 sys_futimes(struct futimes_args *uap)
2729 struct timeval tv[2];
2733 error = copyin(uap->tptr, tv, sizeof(tv));
2738 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2744 kern_truncate(struct nlookupdata *nd, off_t length)
2752 /* XXX Add NLC flag indicating modifying operation? */
2753 if ((error = nlookup(nd)) != 0)
2755 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2757 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2759 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2763 if (vp->v_type == VDIR) {
2765 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
2766 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2768 vattr.va_size = length;
2769 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2776 * truncate(char *path, int pad, off_t length)
2778 * Truncate a file given its path name.
2781 sys_truncate(struct truncate_args *uap)
2783 struct nlookupdata nd;
2786 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2788 error = kern_truncate(&nd, uap->length);
2794 kern_ftruncate(int fd, off_t length)
2796 struct thread *td = curthread;
2797 struct proc *p = td->td_proc;
2805 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2807 if (fp->f_nchandle.ncp) {
2808 error = ncp_writechk(&fp->f_nchandle);
2812 if ((fp->f_flag & FWRITE) == 0) {
2816 vp = (struct vnode *)fp->f_data;
2817 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2818 if (vp->v_type == VDIR) {
2820 } else if ((error = vn_writechk(vp, NULL)) == 0) {
2822 vattr.va_size = length;
2823 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2832 * ftruncate_args(int fd, int pad, off_t length)
2834 * Truncate a file given a file descriptor.
2837 sys_ftruncate(struct ftruncate_args *uap)
2841 error = kern_ftruncate(uap->fd, uap->length);
2849 * Sync an open file.
2853 sys_fsync(struct fsync_args *uap)
2855 struct thread *td = curthread;
2856 struct proc *p = td->td_proc;
2862 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2864 vp = (struct vnode *)fp->f_data;
2865 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2866 if ((obj = vp->v_object) != NULL)
2867 vm_object_page_clean(obj, 0, 0, 0);
2868 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2869 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2871 error = (*bioops.io_fsync)(vp);
2879 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2881 struct nchandle fnchd;
2882 struct nchandle tnchd;
2883 struct namecache *ncp;
2890 if ((error = nlookup(fromnd)) != 0)
2892 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
2894 fnchd.mount = fromnd->nl_nch.mount;
2898 * unlock the source nch so we can lookup the target nch without
2899 * deadlocking. The target may or may not exist so we do not check
2900 * for a target vp like kern_mkdir() and other creation functions do.
2902 * The source and target directories are ref'd and rechecked after
2903 * everything is relocked to determine if the source or target file
2906 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2907 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2908 cache_unlock(&fromnd->nl_nch);
2910 tond->nl_flags |= NLC_CREATE;
2911 if ((error = nlookup(tond)) != 0) {
2915 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
2919 tnchd.mount = tond->nl_nch.mount;
2923 * If the source and target are the same there is nothing to do
2925 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
2932 * Mount points cannot be renamed or overwritten
2934 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
2943 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2944 * may have become invalid while it was unlocked, nc_vp and nc_mount
2947 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
2948 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2949 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
2950 cache_lock(&fromnd->nl_nch);
2951 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2953 cache_unlock(&tond->nl_nch);
2954 cache_lock(&fromnd->nl_nch);
2955 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2956 cache_lock(&tond->nl_nch);
2957 cache_resolve(&tond->nl_nch, tond->nl_cred);
2959 fromnd->nl_flags |= NLC_NCPISLOCKED;
2962 * make sure the parent directories linkages are the same
2964 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
2965 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
2972 * Both the source and target must be within the same filesystem and
2973 * in the same filesystem as their parent directories within the
2974 * namecache topology.
2976 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2979 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
2980 mp != tond->nl_nch.mount) {
2987 * Make sure the mount point is writable
2989 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
2996 * If the target exists and either the source or target is a directory,
2997 * then both must be directories.
2999 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3002 if (tond->nl_nch.ncp->nc_vp) {
3003 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
3005 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3006 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
3008 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
3014 * You cannot rename a source into itself or a subdirectory of itself.
3015 * We check this by travsersing the target directory upwards looking
3016 * for a match against the source.
3019 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3020 if (fromnd->nl_nch.ncp == ncp) {
3031 * Even though the namespaces are different, they may still represent
3032 * hardlinks to the same file. The filesystem might have a hard time
3033 * with this so we issue a NREMOVE of the source instead of a NRENAME
3034 * when we detect the situation.
3037 fdvp = fromnd->nl_nch.ncp->nc_parent->nc_vp;
3038 tdvp = tond->nl_nch.ncp->nc_parent->nc_vp;
3039 if (fdvp == NULL || tdvp == NULL) {
3041 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
3042 /* vhold(fdvp); - dvp can't go away */
3043 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3047 /* vhold(fdvp); - dvp can't go away */
3048 /* vhold(tdvp); - dvp can't go away */
3049 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
3050 fdvp, tdvp, tond->nl_cred);
3059 * rename_args(char *from, char *to)
3061 * Rename files. Source and destination must either both be directories,
3062 * or both not be directories. If target is a directory, it must be empty.
3065 sys_rename(struct rename_args *uap)
3067 struct nlookupdata fromnd, tond;
3070 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3072 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3074 error = kern_rename(&fromnd, &tond);
3075 nlookup_done(&tond);
3077 nlookup_done(&fromnd);
3082 kern_mkdir(struct nlookupdata *nd, int mode)
3084 struct thread *td = curthread;
3085 struct proc *p = td->td_proc;
3092 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
3093 if ((error = nlookup(nd)) != 0)
3096 if (nd->nl_nch.ncp->nc_vp)
3098 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3100 if ((dvp = nd->nl_nch.ncp->nc_parent->nc_vp) == NULL)
3102 /* vhold(dvp); - dvp can't go away */
3104 vattr.va_type = VDIR;
3105 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3108 error = VOP_NMKDIR(&nd->nl_nch, dvp, &vp, p->p_ucred, &vattr);
3116 * mkdir_args(char *path, int mode)
3118 * Make a directory file.
3122 sys_mkdir(struct mkdir_args *uap)
3124 struct nlookupdata nd;
3127 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3129 error = kern_mkdir(&nd, uap->mode);
3135 kern_rmdir(struct nlookupdata *nd)
3141 nd->nl_flags |= NLC_DELETE;
3142 if ((error = nlookup(nd)) != 0)
3146 * Do not allow directories representing mount points to be
3147 * deleted, even if empty. Check write perms on mount point
3148 * in case the vnode is aliased (aka nullfs).
3150 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3152 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3154 if ((dvp = nd->nl_nch.ncp->nc_parent->nc_vp) == NULL)
3156 /* vhold(dvp); - dvp can't go away */
3157 error = VOP_NRMDIR(&nd->nl_nch, dvp, nd->nl_cred);
3163 * rmdir_args(char *path)
3165 * Remove a directory file.
3169 sys_rmdir(struct rmdir_args *uap)
3171 struct nlookupdata nd;
3174 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3176 error = kern_rmdir(&nd);
3182 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3183 enum uio_seg direction)
3185 struct thread *td = curthread;
3186 struct proc *p = td->td_proc;
3194 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3196 if ((fp->f_flag & FREAD) == 0) {
3200 vp = (struct vnode *)fp->f_data;
3202 if (vp->v_type != VDIR) {
3206 aiov.iov_base = buf;
3207 aiov.iov_len = count;
3208 auio.uio_iov = &aiov;
3209 auio.uio_iovcnt = 1;
3210 auio.uio_rw = UIO_READ;
3211 auio.uio_segflg = direction;
3213 auio.uio_resid = count;
3214 loff = auio.uio_offset = fp->f_offset;
3215 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3216 fp->f_offset = auio.uio_offset;
3219 if (count == auio.uio_resid) {
3220 if (union_dircheckp) {
3221 error = union_dircheckp(td, &vp, fp);
3228 if ((vp->v_flag & VROOT) &&
3229 (vp->v_mount->mnt_flag & MNT_UNION)) {
3230 struct vnode *tvp = vp;
3231 vp = vp->v_mount->mnt_vnodecovered;
3243 *res = count - auio.uio_resid;
3250 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3252 * Read a block of directory entries in a file system independent format.
3255 sys_getdirentries(struct getdirentries_args *uap)
3260 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3261 &uap->sysmsg_result, UIO_USERSPACE);
3264 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3269 * getdents_args(int fd, char *buf, size_t count)
3272 sys_getdents(struct getdents_args *uap)
3276 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3277 &uap->sysmsg_result, UIO_USERSPACE);
3283 * umask(int newmask)
3285 * Set the mode mask for creation of filesystem nodes.
3290 sys_umask(struct umask_args *uap)
3292 struct thread *td = curthread;
3293 struct proc *p = td->td_proc;
3294 struct filedesc *fdp;
3297 uap->sysmsg_result = fdp->fd_cmask;
3298 fdp->fd_cmask = uap->newmask & ALLPERMS;
3303 * revoke(char *path)
3305 * Void all references to file by ripping underlying filesystem
3310 sys_revoke(struct revoke_args *uap)
3312 struct nlookupdata nd;
3319 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3321 error = nlookup(&nd);
3323 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3324 cred = crhold(nd.nl_cred);
3327 if (vp->v_type != VCHR && vp->v_type != VBLK)
3330 error = VOP_GETATTR(vp, &vattr);
3331 if (error == 0 && cred->cr_uid != vattr.va_uid)
3332 error = suser_cred(cred, PRISON_ROOT);
3333 if (error == 0 && count_udev(vp->v_umajor, vp->v_uminor) > 0) {
3336 VOP_REVOKE(vp, REVOKEALL);
3347 * getfh_args(char *fname, fhandle_t *fhp)
3349 * Get (NFS) file handle
3352 sys_getfh(struct getfh_args *uap)
3354 struct thread *td = curthread;
3355 struct nlookupdata nd;
3361 * Must be super user
3363 if ((error = suser(td)) != 0)
3367 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3369 error = nlookup(&nd);
3371 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3374 bzero(&fh, sizeof(fh));
3375 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3376 error = VFS_VPTOFH(vp, &fh.fh_fid);
3379 error = copyout(&fh, uap->fhp, sizeof(fh));
3385 * fhopen_args(const struct fhandle *u_fhp, int flags)
3387 * syscall for the rpc.lockd to use to translate a NFS file handle into
3388 * an open descriptor.
3390 * warning: do not remove the suser() call or this becomes one giant
3394 sys_fhopen(struct fhopen_args *uap)
3396 struct thread *td = curthread;
3397 struct proc *p = td->td_proc;
3402 struct vattr *vap = &vat;
3404 int fmode, mode, error, type;
3410 * Must be super user
3416 fmode = FFLAGS(uap->flags);
3417 /* why not allow a non-read/write open for our lockd? */
3418 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3420 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3423 /* find the mount point */
3424 mp = vfs_getvfs(&fhp.fh_fsid);
3427 /* now give me my vnode, it gets returned to me locked */
3428 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3432 * from now on we have to make sure not
3433 * to forget about the vnode
3434 * any error that causes an abort must vput(vp)
3435 * just set error = err and 'goto bad;'.
3441 if (vp->v_type == VLNK) {
3445 if (vp->v_type == VSOCK) {
3450 if (fmode & (FWRITE | O_TRUNC)) {
3451 if (vp->v_type == VDIR) {
3455 error = vn_writechk(vp, NULL);
3463 error = VOP_ACCESS(vp, mode, p->p_ucred);
3467 if (fmode & O_TRUNC) {
3468 vn_unlock(vp); /* XXX */
3469 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3472 error = VOP_SETATTR(vp, vap, p->p_ucred);
3478 * VOP_OPEN needs the file pointer so it can potentially override
3481 * WARNING! no f_nchandle will be associated when fhopen()ing a
3484 if ((error = falloc(p, &nfp, &indx)) != 0)
3488 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3491 * setting f_ops this way prevents VOP_CLOSE from being
3492 * called or fdrop() releasing the vp from v_data. Since
3493 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3495 fp->f_ops = &badfileops;
3501 * The fp is given its own reference, we still have our ref and lock.
3503 * Assert that all regular files must be created with a VM object.
3505 if (vp->v_type == VREG && vp->v_object == NULL) {
3506 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3511 * The open was successful. Handle any locking requirements.
3513 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3514 lf.l_whence = SEEK_SET;
3517 if (fmode & O_EXLOCK)
3518 lf.l_type = F_WRLCK;
3520 lf.l_type = F_RDLCK;
3521 if (fmode & FNONBLOCK)
3526 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3528 * release our private reference.
3530 fsetfd(p, NULL, indx);
3535 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3536 fp->f_flag |= FHASLOCK;
3540 * Clean up. Associate the file pointer with the previously
3541 * reserved descriptor and return it.
3544 fsetfd(p, fp, indx);
3546 uap->sysmsg_result = indx;
3550 fsetfd(p, NULL, indx);
3558 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3561 sys_fhstat(struct fhstat_args *uap)
3563 struct thread *td = curthread;
3571 * Must be super user
3577 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3581 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3583 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3585 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3589 error = copyout(&sb, uap->sb, sizeof(sb));
3594 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3597 sys_fhstatfs(struct fhstatfs_args *uap)
3599 struct thread *td = curthread;
3600 struct proc *p = td->td_proc;
3605 char *fullpath, *freepath;
3610 * Must be super user
3612 if ((error = suser(td)))
3615 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3618 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3621 if (p != NULL && !chroot_visible_mnt(mp, p))
3624 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3629 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3632 error = mount_path(p, mp, &fullpath, &freepath);
3635 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3636 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3637 kfree(freepath, M_TEMP);
3639 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3641 bcopy(sp, &sb, sizeof(sb));
3642 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3645 return (copyout(sp, uap->buf, sizeof(*sp)));
3649 * Syscall to push extended attribute configuration information into the
3650 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3651 * a command (int cmd), and attribute name and misc data. For now, the
3652 * attribute name is left in userspace for consumption by the VFS_op.
3653 * It will probably be changed to be copied into sysspace by the
3654 * syscall in the future, once issues with various consumers of the
3655 * attribute code have raised their hands.
3657 * Currently this is used only by UFS Extended Attributes.
3660 sys_extattrctl(struct extattrctl_args *uap)
3662 struct nlookupdata nd;
3668 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3670 error = nlookup(&nd);
3672 mp = nd.nl_nch.mount;
3673 error = VFS_EXTATTRCTL(mp, uap->cmd,
3674 uap->attrname, uap->arg,
3682 * Syscall to set a named extended attribute on a file or directory.
3683 * Accepts attribute name, and a uio structure pointing to the data to set.
3684 * The uio is consumed in the style of writev(). The real work happens
3685 * in VOP_SETEXTATTR().
3688 sys_extattr_set_file(struct extattr_set_file_args *uap)
3690 char attrname[EXTATTR_MAXNAMELEN];
3691 struct iovec aiov[UIO_SMALLIOV];
3692 struct iovec *needfree;
3693 struct nlookupdata nd;
3702 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3707 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3709 error = nlookup(&nd);
3711 error = ncp_writechk(&nd.nl_nch);
3713 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3720 iovlen = uap->iovcnt * sizeof(struct iovec);
3721 if (uap->iovcnt > UIO_SMALLIOV) {
3722 if (uap->iovcnt > UIO_MAXIOV) {
3726 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3732 auio.uio_iovcnt = uap->iovcnt;
3733 auio.uio_rw = UIO_WRITE;
3734 auio.uio_segflg = UIO_USERSPACE;
3735 auio.uio_td = nd.nl_td;
3736 auio.uio_offset = 0;
3737 if ((error = copyin(uap->iovp, iov, iovlen)))
3740 for (i = 0; i < uap->iovcnt; i++) {
3741 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3745 auio.uio_resid += iov->iov_len;
3748 cnt = auio.uio_resid;
3749 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3750 cnt -= auio.uio_resid;
3751 uap->sysmsg_result = cnt;
3756 FREE(needfree, M_IOV);
3761 * Syscall to get a named extended attribute on a file or directory.
3762 * Accepts attribute name, and a uio structure pointing to a buffer for the
3763 * data. The uio is consumed in the style of readv(). The real work
3764 * happens in VOP_GETEXTATTR();
3767 sys_extattr_get_file(struct extattr_get_file_args *uap)
3769 char attrname[EXTATTR_MAXNAMELEN];
3770 struct iovec aiov[UIO_SMALLIOV];
3771 struct iovec *needfree;
3772 struct nlookupdata nd;
3781 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3786 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3788 error = nlookup(&nd);
3790 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3796 iovlen = uap->iovcnt * sizeof (struct iovec);
3798 if (uap->iovcnt > UIO_SMALLIOV) {
3799 if (uap->iovcnt > UIO_MAXIOV) {
3803 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3809 auio.uio_iovcnt = uap->iovcnt;
3810 auio.uio_rw = UIO_READ;
3811 auio.uio_segflg = UIO_USERSPACE;
3812 auio.uio_td = nd.nl_td;
3813 auio.uio_offset = 0;
3814 if ((error = copyin(uap->iovp, iov, iovlen)))
3817 for (i = 0; i < uap->iovcnt; i++) {
3818 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3822 auio.uio_resid += iov->iov_len;
3825 cnt = auio.uio_resid;
3826 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3827 cnt -= auio.uio_resid;
3828 uap->sysmsg_result = cnt;
3833 FREE(needfree, M_IOV);
3838 * Syscall to delete a named extended attribute from a file or directory.
3839 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3842 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
3844 char attrname[EXTATTR_MAXNAMELEN];
3845 struct nlookupdata nd;
3849 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3854 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3856 error = nlookup(&nd);
3858 error = ncp_writechk(&nd.nl_nch);
3860 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3866 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3873 * Determine if the mount is visible to the process.
3876 chroot_visible_mnt(struct mount *mp, struct proc *p)
3878 struct nchandle nch;
3881 * Traverse from the mount point upwards. If we hit the process
3882 * root then the mount point is visible to the process.
3884 nch = mp->mnt_ncmountpt;
3886 if (nch.mount == p->p_fd->fd_nrdir.mount &&
3887 nch.ncp == p->p_fd->fd_nrdir.ncp) {
3890 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
3891 nch = nch.mount->mnt_ncmounton;
3893 nch.ncp = nch.ncp->nc_parent;
3898 * If the mount point is not visible to the process, but the
3899 * process root is in a subdirectory of the mount, return
3902 if (p->p_fd->fd_nrdir.mount == mp)