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.112 2007/01/24 01:25:47 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 checkvp_chdir (struct vnode *vn, struct thread *td);
83 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
84 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
85 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
86 static int getutimes (const struct timeval *, struct timespec *);
87 static int setfown (struct vnode *, uid_t, gid_t);
88 static int setfmode (struct vnode *, int);
89 static int setfflags (struct vnode *, int);
90 static int setutimes (struct vnode *, const struct timespec *, int);
91 static int usermount = 0; /* if 1, non-root can mount fs. */
93 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
95 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
98 * Virtual File System System Calls
102 * Mount a file system.
105 * mount_args(char *type, char *path, int flags, caddr_t data)
109 sys_mount(struct mount_args *uap)
111 struct thread *td = curthread;
112 struct proc *p = td->td_proc;
116 struct vfsconf *vfsp;
117 int error, flag = 0, flag2 = 0;
120 struct nlookupdata nd;
121 char fstypename[MFSNAMELEN];
122 struct ucred *cred = p->p_ucred;
125 if (cred->cr_prison != NULL)
127 if (usermount == 0 && (error = suser(td)))
130 * Do not allow NFS export by non-root users.
132 if (uap->flags & MNT_EXPORTED) {
138 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
141 uap->flags |= MNT_NOSUID | MNT_NODEV;
144 * Lookup the requested path and extract the nch and vnode.
146 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
148 if ((error = nlookup(&nd)) == 0) {
149 if (nd.nl_nch.ncp->nc_vp == NULL)
159 * Extract the locked+refd ncp and cleanup the nd structure
162 cache_zero(&nd.nl_nch);
165 if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch))
172 * now we have the locked ref'd nch and unreferenced vnode.
175 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
182 * Now we have an unlocked ref'd nch and a locked ref'd vp
184 if (uap->flags & MNT_UPDATE) {
185 if ((vp->v_flag & VROOT) == 0) {
192 flag2 = mp->mnt_kern_flag;
194 * We only allow the filesystem to be reloaded if it
195 * is currently mounted read-only.
197 if ((uap->flags & MNT_RELOAD) &&
198 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
201 return (EOPNOTSUPP); /* Needs translation */
204 * Only root, or the user that did the original mount is
205 * permitted to update it.
207 if (mp->mnt_stat.f_owner != cred->cr_uid &&
208 (error = suser(td))) {
213 if (vfs_busy(mp, LK_NOWAIT)) {
218 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
224 vp->v_flag |= VMOUNT;
226 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
231 * If the user is not root, ensure that they own the directory
232 * onto which we are attempting to mount.
234 if ((error = VOP_GETATTR(vp, &va)) ||
235 (va.va_uid != cred->cr_uid && (error = suser(td)))) {
240 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
245 if (vp->v_type != VDIR) {
250 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
255 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
256 if (!strcmp(vfsp->vfc_name, fstypename))
262 /* Only load modules for root (very important!) */
263 if ((error = suser(td)) != 0) {
268 error = linker_load_file(fstypename, &lf);
269 if (error || lf == NULL) {
277 /* lookup again, see if the VFS was loaded */
278 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
279 if (!strcmp(vfsp->vfc_name, fstypename))
284 linker_file_unload(lf);
290 if ((vp->v_flag & VMOUNT) != 0 || hasmount) {
295 vp->v_flag |= VMOUNT;
298 * Allocate and initialize the filesystem.
300 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
301 TAILQ_INIT(&mp->mnt_nvnodelist);
302 TAILQ_INIT(&mp->mnt_reservedvnlist);
303 TAILQ_INIT(&mp->mnt_jlist);
304 mp->mnt_nvnodelistsize = 0;
305 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
306 vfs_busy(mp, LK_NOWAIT);
307 mp->mnt_op = vfsp->vfc_vfsops;
309 vfsp->vfc_refcount++;
310 mp->mnt_stat.f_type = vfsp->vfc_typenum;
311 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
312 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
313 mp->mnt_stat.f_owner = cred->cr_uid;
314 mp->mnt_iosize_max = DFLTPHYS;
318 * Set the mount level flags.
320 if (uap->flags & MNT_RDONLY)
321 mp->mnt_flag |= MNT_RDONLY;
322 else if (mp->mnt_flag & MNT_RDONLY)
323 mp->mnt_kern_flag |= MNTK_WANTRDWR;
324 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
325 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
326 MNT_NOSYMFOLLOW | MNT_IGNORE |
327 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
328 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
329 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
330 MNT_NOSYMFOLLOW | MNT_IGNORE |
331 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
333 * Mount the filesystem.
334 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
337 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
338 if (mp->mnt_flag & MNT_UPDATE) {
339 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
340 mp->mnt_flag &= ~MNT_RDONLY;
341 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
342 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
345 mp->mnt_kern_flag = flag2;
348 vp->v_flag &= ~VMOUNT;
353 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
355 * Put the new filesystem on the mount list after root. The mount
356 * point gets its own mnt_ncmountpt (unless the VFS already set one
357 * up) which represents the root of the mount. The lookup code
358 * detects the mount point going forward and checks the root of
359 * the mount going backwards.
361 * It is not necessary to invalidate or purge the vnode underneath
362 * because elements under the mount will be given their own glue
366 if (mp->mnt_ncmountpt.ncp == NULL) {
368 * allocate, then unlock, but leave the ref intact
370 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
371 cache_unlock(&mp->mnt_ncmountpt);
373 mp->mnt_ncmounton = nch; /* inherits ref */
374 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
376 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
377 vp->v_flag &= ~VMOUNT;
378 mountlist_insert(mp, MNTINS_LAST);
379 checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
381 error = vfs_allocate_syncvnode(mp);
383 error = VFS_START(mp, 0);
386 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
387 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
388 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
389 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
390 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
391 vp->v_flag &= ~VMOUNT;
392 mp->mnt_vfc->vfc_refcount--;
402 * Scan all active processes to see if any of them have a current
403 * or root directory onto which the new filesystem has just been
404 * mounted. If so, replace them with the new mount point.
406 * The passed ncp is ref'd and locked (from the mount code) and
407 * must be associated with the vnode representing the root of the
410 struct checkdirs_info {
411 struct nchandle old_nch;
412 struct nchandle new_nch;
413 struct vnode *old_vp;
414 struct vnode *new_vp;
417 static int checkdirs_callback(struct proc *p, void *data);
420 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
422 struct checkdirs_info info;
428 * If the old mount point's vnode has a usecount of 1, it is not
429 * being held as a descriptor anywhere.
431 olddp = old_nch->ncp->nc_vp;
432 if (olddp == NULL || olddp->v_usecount == 1)
436 * Force the root vnode of the new mount point to be resolved
437 * so we can update any matching processes.
440 if (VFS_ROOT(mp, &newdp))
441 panic("mount: lost mount");
442 cache_setunresolved(new_nch);
443 cache_setvp(new_nch, newdp);
446 * Special handling of the root node
448 if (rootvnode == olddp) {
450 vfs_cache_setroot(newdp, cache_hold(new_nch));
454 * Pass newdp separately so the callback does not have to access
455 * it via new_nch->ncp->nc_vp.
457 info.old_nch = *old_nch;
458 info.new_nch = *new_nch;
460 allproc_scan(checkdirs_callback, &info);
465 * NOTE: callback is not MP safe because the scanned process's filedesc
466 * structure can be ripped out from under us, amoung other things.
469 checkdirs_callback(struct proc *p, void *data)
471 struct checkdirs_info *info = data;
472 struct filedesc *fdp;
473 struct nchandle ncdrop1;
474 struct nchandle ncdrop2;
475 struct vnode *vprele1;
476 struct vnode *vprele2;
478 if ((fdp = p->p_fd) != NULL) {
479 cache_zero(&ncdrop1);
480 cache_zero(&ncdrop2);
485 * MPUNSAFE - XXX fdp can be pulled out from under a
488 * A shared filedesc is ok, we don't have to copy it
489 * because we are making this change globally.
491 spin_lock_wr(&fdp->fd_spin);
492 if (fdp->fd_ncdir.mount == info->old_nch.mount &&
493 fdp->fd_ncdir.ncp == info->old_nch.ncp) {
494 vprele1 = fdp->fd_cdir;
496 fdp->fd_cdir = info->new_vp;
497 ncdrop1 = fdp->fd_ncdir;
498 cache_copy(&info->new_nch, &fdp->fd_ncdir);
500 if (fdp->fd_nrdir.mount == info->old_nch.mount &&
501 fdp->fd_nrdir.ncp == info->old_nch.ncp) {
502 vprele2 = fdp->fd_rdir;
504 fdp->fd_rdir = info->new_vp;
505 ncdrop2 = fdp->fd_nrdir;
506 cache_copy(&info->new_nch, &fdp->fd_nrdir);
508 spin_unlock_wr(&fdp->fd_spin);
510 cache_drop(&ncdrop1);
512 cache_drop(&ncdrop2);
522 * Unmount a file system.
524 * Note: unmount takes a path to the vnode mounted on as argument,
525 * not special file (as before).
528 * umount_args(char *path, int flags)
532 sys_unmount(struct unmount_args *uap)
534 struct thread *td = curthread;
535 struct proc *p = td->td_proc;
536 struct mount *mp = NULL;
538 struct nlookupdata nd;
541 if (p->p_ucred->cr_prison != NULL)
543 if (usermount == 0 && (error = suser(td)))
546 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
548 error = nlookup(&nd);
552 mp = nd.nl_nch.mount;
555 * Only root, or the user that did the original mount is
556 * permitted to unmount this filesystem.
558 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
563 * Don't allow unmounting the root file system.
565 if (mp->mnt_flag & MNT_ROOTFS) {
571 * Must be the root of the filesystem
573 if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
582 return (dounmount(mp, uap->flags));
586 * Do the actual file system unmount.
589 dounmount_interlock(struct mount *mp)
591 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
593 mp->mnt_kern_flag |= MNTK_UNMOUNT;
598 dounmount(struct mount *mp, int flags)
600 struct namecache *ncp;
608 * Exclusive access for unmounting purposes
610 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
614 * Allow filesystems to detect that a forced unmount is in progress.
616 if (flags & MNT_FORCE)
617 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
618 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
619 error = lockmgr(&mp->mnt_lock, lflags);
621 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
622 if (mp->mnt_kern_flag & MNTK_MWAIT)
627 if (mp->mnt_flag & MNT_EXPUBLIC)
628 vfs_setpublicfs(NULL, NULL, NULL);
630 vfs_msync(mp, MNT_WAIT);
631 async_flag = mp->mnt_flag & MNT_ASYNC;
632 mp->mnt_flag &=~ MNT_ASYNC;
635 * If this filesystem isn't aliasing other filesystems,
636 * try to invalidate any remaining namecache entries and
637 * check the count afterwords.
639 if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
640 cache_lock(&mp->mnt_ncmountpt);
641 cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN);
642 cache_unlock(&mp->mnt_ncmountpt);
644 if ((ncp = mp->mnt_ncmountpt.ncp) != NULL &&
645 (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) {
647 if ((flags & MNT_FORCE) == 0) {
649 mount_warning(mp, "Cannot unmount: "
655 mount_warning(mp, "Forced unmount: "
666 * nchandle records ref the mount structure. Expect a count of 1
667 * (our mount->mnt_ncmountpt).
669 if (mp->mnt_refs != 1) {
670 if ((flags & MNT_FORCE) == 0) {
671 mount_warning(mp, "Cannot unmount: "
672 "%d process references still "
673 "present", mp->mnt_refs);
676 mount_warning(mp, "Forced unmount: "
677 "%d process references still "
678 "present", mp->mnt_refs);
684 if (mp->mnt_syncer != NULL)
685 vrele(mp->mnt_syncer);
686 if (((mp->mnt_flag & MNT_RDONLY) ||
687 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
688 (flags & MNT_FORCE)) {
689 error = VFS_UNMOUNT(mp, flags);
693 if (mp->mnt_syncer == NULL)
694 vfs_allocate_syncvnode(mp);
695 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
696 mp->mnt_flag |= async_flag;
697 lockmgr(&mp->mnt_lock, LK_RELEASE);
698 if (mp->mnt_kern_flag & MNTK_MWAIT)
703 * Clean up any journals still associated with the mount after
704 * filesystem activity has ceased.
706 journal_remove_all_journals(mp,
707 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
709 mountlist_remove(mp);
712 * Remove any installed vnode ops here so the individual VFSs don't
715 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
716 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
717 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
718 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
719 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
721 if (mp->mnt_ncmountpt.ncp != NULL) {
722 nch = mp->mnt_ncmountpt;
723 cache_zero(&mp->mnt_ncmountpt);
724 cache_clrmountpt(&nch);
727 if (mp->mnt_ncmounton.ncp != NULL) {
728 nch = mp->mnt_ncmounton;
729 cache_zero(&mp->mnt_ncmounton);
730 cache_clrmountpt(&nch);
734 mp->mnt_vfc->vfc_refcount--;
735 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
736 panic("unmount: dangling vnode");
737 lockmgr(&mp->mnt_lock, LK_RELEASE);
738 if (mp->mnt_kern_flag & MNTK_MWAIT)
747 mount_warning(struct mount *mp, const char *ctl, ...)
754 if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) {
755 kprintf("unmount(%s): ", ptr);
760 kprintf("unmount(%p): ", mp);
768 * Sync each mounted filesystem.
772 static int syncprt = 0;
773 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
776 static int sync_callback(struct mount *mp, void *data);
780 sys_sync(struct sync_args *uap)
782 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
785 * print out buffer pool stat information on each sync() call.
795 sync_callback(struct mount *mp, void *data __unused)
799 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
800 asyncflag = mp->mnt_flag & MNT_ASYNC;
801 mp->mnt_flag &= ~MNT_ASYNC;
802 vfs_msync(mp, MNT_NOWAIT);
803 VFS_SYNC(mp, MNT_NOWAIT);
804 mp->mnt_flag |= asyncflag;
809 /* XXX PRISON: could be per prison flag */
810 static int prison_quotas;
812 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
816 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
818 * Change filesystem quotas.
822 sys_quotactl(struct quotactl_args *uap)
824 struct nlookupdata nd;
832 if (p->p_ucred->cr_prison && !prison_quotas)
835 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
837 error = nlookup(&nd);
839 mp = nd.nl_nch.mount;
840 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
841 uap->arg, nd.nl_cred);
848 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
849 * void *buf, int buflen)
851 * This function operates on a mount point and executes the specified
852 * operation using the specified control data, and possibly returns data.
854 * The actual number of bytes stored in the result buffer is returned, 0
855 * if none, otherwise an error is returned.
859 sys_mountctl(struct mountctl_args *uap)
861 struct thread *td = curthread;
862 struct proc *p = td->td_proc;
870 * Sanity and permissions checks. We must be root.
873 if (p->p_ucred->cr_prison != NULL)
875 if ((error = suser(td)) != 0)
879 * Argument length checks
881 if (uap->ctllen < 0 || uap->ctllen > 1024)
883 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
885 if (uap->path == NULL)
889 * Allocate the necessary buffers and copyin data
891 path = objcache_get(namei_oc, M_WAITOK);
892 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
897 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
898 error = copyin(uap->ctl, ctl, uap->ctllen);
903 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
906 * Validate the descriptor
908 fp = holdfp(p->p_fd, uap->fd, -1);
915 * Execute the internal kernel function and clean up.
917 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
920 if (error == 0 && uap->sysmsg_result > 0)
921 error = copyout(buf, uap->buf, uap->sysmsg_result);
924 objcache_put(namei_oc, path);
933 * Execute a mount control operation by resolving the path to a mount point
934 * and calling vop_mountctl().
937 kern_mountctl(const char *path, int op, struct file *fp,
938 const void *ctl, int ctllen,
939 void *buf, int buflen, int *res)
943 struct nlookupdata nd;
948 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
950 error = nlookup(&nd);
952 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
960 * Must be the root of the filesystem
962 if ((vp->v_flag & VROOT) == 0) {
966 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
973 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
975 struct thread *td = curthread;
976 struct proc *p = td->td_proc;
979 char *fullpath, *freepath;
982 if ((error = nlookup(nd)) != 0)
984 mp = nd->nl_nch.mount;
986 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
989 error = cache_fullpath(p, &mp->mnt_ncmountpt, &fullpath, &freepath);
992 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
993 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
994 kfree(freepath, M_TEMP);
996 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
997 bcopy(sp, buf, sizeof(*buf));
998 /* Only root should have access to the fsid's. */
1000 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1005 * statfs_args(char *path, struct statfs *buf)
1007 * Get filesystem statistics.
1010 sys_statfs(struct statfs_args *uap)
1012 struct nlookupdata nd;
1016 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1018 error = kern_statfs(&nd, &buf);
1021 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1026 kern_fstatfs(int fd, struct statfs *buf)
1028 struct thread *td = curthread;
1029 struct proc *p = td->td_proc;
1033 char *fullpath, *freepath;
1037 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1039 mp = ((struct vnode *)fp->f_data)->v_mount;
1044 if (fp->f_cred == NULL) {
1049 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1052 if ((error = cache_fullpath(p, &mp->mnt_ncmountpt, &fullpath, &freepath)) != 0)
1054 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1055 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1056 kfree(freepath, M_TEMP);
1058 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1059 bcopy(sp, buf, sizeof(*buf));
1061 /* Only root should have access to the fsid's. */
1063 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1071 * fstatfs_args(int fd, struct statfs *buf)
1073 * Get filesystem statistics.
1076 sys_fstatfs(struct fstatfs_args *uap)
1081 error = kern_fstatfs(uap->fd, &buf);
1084 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1089 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1091 * Get statistics on all filesystems.
1094 struct getfsstat_info {
1095 struct statfs *sfsp;
1103 static int getfsstat_callback(struct mount *, void *);
1107 sys_getfsstat(struct getfsstat_args *uap)
1109 struct thread *td = curthread;
1110 struct proc *p = td->td_proc;
1111 struct getfsstat_info info;
1113 bzero(&info, sizeof(info));
1115 info.maxcount = uap->bufsize / sizeof(struct statfs);
1116 info.sfsp = uap->buf;
1118 info.flags = uap->flags;
1121 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1122 if (info.sfsp && info.count > info.maxcount)
1123 uap->sysmsg_result = info.maxcount;
1125 uap->sysmsg_result = info.count;
1126 return (info.error);
1130 getfsstat_callback(struct mount *mp, void *data)
1132 struct getfsstat_info *info = data;
1138 if (info->sfsp && info->count < info->maxcount) {
1139 if (info->p && !chroot_visible_mnt(mp, info->p))
1144 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1145 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1146 * overrides MNT_WAIT.
1148 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1149 (info->flags & MNT_WAIT)) &&
1150 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1153 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1155 error = cache_fullpath(info->p, &mp->mnt_ncmountpt,
1156 &fullpath, &freepath);
1158 info->error = error;
1161 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1162 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1163 kfree(freepath, M_TEMP);
1165 error = copyout(sp, info->sfsp, sizeof(*sp));
1167 info->error = error;
1177 * fchdir_args(int fd)
1179 * Change current working directory to a given file descriptor.
1183 sys_fchdir(struct fchdir_args *uap)
1185 struct thread *td = curthread;
1186 struct proc *p = td->td_proc;
1187 struct filedesc *fdp = p->p_fd;
1188 struct vnode *vp, *ovp;
1191 struct nchandle nch, onch, tnch;
1194 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1196 vp = (struct vnode *)fp->f_data;
1198 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1199 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
1202 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1208 cache_copy(&fp->f_nchandle, &nch);
1211 * If the ncp has become a mount point, traverse through
1215 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1216 (mp = cache_findmount(&nch)) != NULL
1218 error = nlookup_mp(mp, &tnch);
1220 cache_unlock(&tnch); /* leave ref intact */
1222 vp = tnch.ncp->nc_vp;
1223 error = vget(vp, LK_SHARED);
1224 KKASSERT(error == 0);
1231 onch = fdp->fd_ncdir;
1232 vn_unlock(vp); /* leave ref intact */
1234 fdp->fd_ncdir = nch;
1246 kern_chdir(struct nlookupdata *nd)
1248 struct thread *td = curthread;
1249 struct proc *p = td->td_proc;
1250 struct filedesc *fdp = p->p_fd;
1251 struct vnode *vp, *ovp;
1252 struct nchandle onch;
1255 if ((error = nlookup(nd)) != 0)
1257 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1259 if ((error = vget(vp, LK_SHARED)) != 0)
1262 error = checkvp_chdir(vp, td);
1266 onch = fdp->fd_ncdir;
1267 cache_unlock(&nd->nl_nch); /* leave reference intact */
1268 fdp->fd_ncdir = nd->nl_nch;
1272 cache_zero(&nd->nl_nch);
1280 * chdir_args(char *path)
1282 * Change current working directory (``.'').
1285 sys_chdir(struct chdir_args *uap)
1287 struct nlookupdata nd;
1290 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1292 error = kern_chdir(&nd);
1298 * Helper function for raised chroot(2) security function: Refuse if
1299 * any filedescriptors are open directories.
1302 chroot_refuse_vdir_fds(struct filedesc *fdp)
1309 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1310 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1312 vp = (struct vnode *)fp->f_data;
1313 if (vp->v_type != VDIR) {
1324 * This sysctl determines if we will allow a process to chroot(2) if it
1325 * has a directory open:
1326 * 0: disallowed for all processes.
1327 * 1: allowed for processes that were not already chroot(2)'ed.
1328 * 2: allowed for all processes.
1331 static int chroot_allow_open_directories = 1;
1333 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1334 &chroot_allow_open_directories, 0, "");
1337 * chroot to the specified namecache entry. We obtain the vp from the
1338 * namecache data. The passed ncp must be locked and referenced and will
1339 * remain locked and referenced on return.
1342 kern_chroot(struct nchandle *nch)
1344 struct thread *td = curthread;
1345 struct proc *p = td->td_proc;
1346 struct filedesc *fdp = p->p_fd;
1351 * Only root can chroot
1353 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1357 * Disallow open directory descriptors (fchdir() breakouts).
1359 if (chroot_allow_open_directories == 0 ||
1360 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1361 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1364 if ((vp = nch->ncp->nc_vp) == NULL)
1367 if ((error = vget(vp, LK_SHARED)) != 0)
1371 * Check the validity of vp as a directory to change to and
1372 * associate it with rdir/jdir.
1374 error = checkvp_chdir(vp, td);
1375 vn_unlock(vp); /* leave reference intact */
1377 vrele(fdp->fd_rdir);
1378 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1379 cache_drop(&fdp->fd_nrdir);
1380 cache_copy(nch, &fdp->fd_nrdir);
1381 if (fdp->fd_jdir == NULL) {
1384 cache_copy(nch, &fdp->fd_njdir);
1393 * chroot_args(char *path)
1395 * Change notion of root (``/'') directory.
1399 sys_chroot(struct chroot_args *uap)
1401 struct thread *td = curthread;
1402 struct nlookupdata nd;
1405 KKASSERT(td->td_proc);
1406 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1411 error = nlookup(&nd);
1413 error = kern_chroot(&nd.nl_nch);
1419 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1420 * determine whether it is legal to chdir to the vnode. The vnode's state
1421 * is not changed by this call.
1424 checkvp_chdir(struct vnode *vp, struct thread *td)
1428 if (vp->v_type != VDIR)
1431 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1436 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1438 struct thread *td = curthread;
1439 struct proc *p = td->td_proc;
1440 struct lwp *lp = td->td_lwp;
1441 struct filedesc *fdp = p->p_fd;
1446 int type, indx, error;
1449 if ((oflags & O_ACCMODE) == O_ACCMODE)
1451 flags = FFLAGS(oflags);
1452 error = falloc(p, &nfp, NULL);
1456 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1459 * XXX p_dupfd is a real mess. It allows a device to return a
1460 * file descriptor to be duplicated rather then doing the open
1466 * Call vn_open() to do the lookup and assign the vnode to the
1467 * file pointer. vn_open() does not change the ref count on fp
1468 * and the vnode, on success, will be inherited by the file pointer
1471 nd->nl_flags |= NLC_LOCKVP;
1472 error = vn_open(nd, fp, flags, cmode);
1476 * handle special fdopen() case. bleh. dupfdopen() is
1477 * responsible for dropping the old contents of ofiles[indx]
1480 * Note that fsetfd() will add a ref to fp which represents
1481 * the fd_files[] assignment. We must still drop our
1484 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1485 if (fdalloc(p, 0, &indx) == 0) {
1486 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1489 fdrop(fp); /* our ref */
1492 fsetfd(p, NULL, indx);
1495 fdrop(fp); /* our ref */
1496 if (error == ERESTART)
1502 * ref the vnode for ourselves so it can't be ripped out from under
1503 * is. XXX need an ND flag to request that the vnode be returned
1506 * Reserve a file descriptor but do not assign it until the open
1509 vp = (struct vnode *)fp->f_data;
1511 if ((error = fdalloc(p, 0, &indx)) != 0) {
1518 * If no error occurs the vp will have been assigned to the file
1523 if (flags & (O_EXLOCK | O_SHLOCK)) {
1524 lf.l_whence = SEEK_SET;
1527 if (flags & O_EXLOCK)
1528 lf.l_type = F_WRLCK;
1530 lf.l_type = F_RDLCK;
1531 if (flags & FNONBLOCK)
1536 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1538 * lock request failed. Clean up the reserved
1542 fsetfd(p, NULL, indx);
1546 fp->f_flag |= FHASLOCK;
1550 * Assert that all regular file vnodes were created with a object.
1552 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1553 ("open: regular file has no backing object after vn_open"));
1559 * release our private reference, leaving the one associated with the
1560 * descriptor table intact.
1562 fsetfd(p, fp, indx);
1569 * open_args(char *path, int flags, int mode)
1571 * Check permissions, allocate an open file structure,
1572 * and call the device open routine if any.
1575 sys_open(struct open_args *uap)
1577 struct nlookupdata nd;
1580 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1582 error = kern_open(&nd, uap->flags,
1583 uap->mode, &uap->sysmsg_result);
1590 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1592 struct thread *td = curthread;
1593 struct proc *p = td->td_proc;
1601 switch (mode & S_IFMT) {
1607 error = suser_cred(p->p_ucred, PRISON_ROOT);
1614 nd->nl_flags |= NLC_CREATE;
1615 if ((error = nlookup(nd)) != 0)
1617 if (nd->nl_nch.ncp->nc_vp)
1619 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1623 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1624 vattr.va_rdev = dev;
1627 switch (mode & S_IFMT) {
1628 case S_IFMT: /* used by badsect to flag bad sectors */
1629 vattr.va_type = VBAD;
1632 vattr.va_type = VCHR;
1635 vattr.va_type = VBLK;
1646 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_cred, NAMEI_CREATE);
1649 error = VOP_NMKNOD(&nd->nl_nch, &vp, nd->nl_cred, &vattr);
1658 * mknod_args(char *path, int mode, int dev)
1660 * Create a special file.
1663 sys_mknod(struct mknod_args *uap)
1665 struct nlookupdata nd;
1668 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1670 error = kern_mknod(&nd, uap->mode, uap->dev);
1676 kern_mkfifo(struct nlookupdata *nd, int mode)
1678 struct thread *td = curthread;
1679 struct proc *p = td->td_proc;
1686 nd->nl_flags |= NLC_CREATE;
1687 if ((error = nlookup(nd)) != 0)
1689 if (nd->nl_nch.ncp->nc_vp)
1691 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1695 vattr.va_type = VFIFO;
1696 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1698 error = VOP_NMKNOD(&nd->nl_nch, &vp, nd->nl_cred, &vattr);
1705 * mkfifo_args(char *path, int mode)
1707 * Create a named pipe.
1710 sys_mkfifo(struct mkfifo_args *uap)
1712 struct nlookupdata nd;
1715 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1717 error = kern_mkfifo(&nd, uap->mode);
1722 static int hardlink_check_uid = 0;
1723 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1724 &hardlink_check_uid, 0,
1725 "Unprivileged processes cannot create hard links to files owned by other "
1727 static int hardlink_check_gid = 0;
1728 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1729 &hardlink_check_gid, 0,
1730 "Unprivileged processes cannot create hard links to files owned by other "
1734 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1740 * Shortcut if disabled
1742 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1746 * root cred can always hardlink
1748 if (suser_cred(cred, PRISON_ROOT) == 0)
1752 * Otherwise only if the originating file is owned by the
1753 * same user or group. Note that any group is allowed if
1754 * the file is owned by the caller.
1756 error = VOP_GETATTR(vp, &va);
1760 if (hardlink_check_uid) {
1761 if (cred->cr_uid != va.va_uid)
1765 if (hardlink_check_gid) {
1766 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1774 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1776 struct thread *td = curthread;
1781 * Lookup the source and obtained a locked vnode.
1783 * XXX relookup on vget failure / race ?
1786 if ((error = nlookup(nd)) != 0)
1788 vp = nd->nl_nch.ncp->nc_vp;
1789 KKASSERT(vp != NULL);
1790 if (vp->v_type == VDIR)
1791 return (EPERM); /* POSIX */
1792 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1794 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1798 * Unlock the source so we can lookup the target without deadlocking
1799 * (XXX vp is locked already, possible other deadlock?). The target
1802 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1803 nd->nl_flags &= ~NLC_NCPISLOCKED;
1804 cache_unlock(&nd->nl_nch);
1806 linknd->nl_flags |= NLC_CREATE;
1807 if ((error = nlookup(linknd)) != 0) {
1811 if (linknd->nl_nch.ncp->nc_vp) {
1817 * Finally run the new API VOP.
1819 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1821 error = VOP_NLINK(&linknd->nl_nch, vp, linknd->nl_cred);
1827 * link_args(char *path, char *link)
1829 * Make a hard file link.
1832 sys_link(struct link_args *uap)
1834 struct nlookupdata nd, linknd;
1837 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1839 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1841 error = kern_link(&nd, &linknd);
1842 nlookup_done(&linknd);
1849 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1856 nd->nl_flags |= NLC_CREATE;
1857 if ((error = nlookup(nd)) != 0)
1859 if (nd->nl_nch.ncp->nc_vp)
1861 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1864 vattr.va_mode = mode;
1865 error = VOP_NSYMLINK(&nd->nl_nch, &vp, nd->nl_cred, &vattr, path);
1872 * symlink(char *path, char *link)
1874 * Make a symbolic link.
1877 sys_symlink(struct symlink_args *uap)
1879 struct thread *td = curthread;
1880 struct nlookupdata nd;
1885 path = objcache_get(namei_oc, M_WAITOK);
1886 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1888 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1890 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1891 error = kern_symlink(&nd, path, mode);
1895 objcache_put(namei_oc, path);
1900 * undelete_args(char *path)
1902 * Delete a whiteout from the filesystem.
1906 sys_undelete(struct undelete_args *uap)
1908 struct nlookupdata nd;
1911 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1913 nd.nl_flags |= NLC_DELETE;
1915 error = nlookup(&nd);
1917 error = ncp_writechk(&nd.nl_nch);
1919 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_cred, NAMEI_DELETE);
1925 kern_unlink(struct nlookupdata *nd)
1930 nd->nl_flags |= NLC_DELETE;
1931 if ((error = nlookup(nd)) != 0)
1933 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1935 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_cred);
1940 * unlink_args(char *path)
1942 * Delete a name from the filesystem.
1945 sys_unlink(struct unlink_args *uap)
1947 struct nlookupdata nd;
1950 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1952 error = kern_unlink(&nd);
1958 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1960 struct thread *td = curthread;
1961 struct proc *p = td->td_proc;
1966 fp = holdfp(p->p_fd, fd, -1);
1969 if (fp->f_type != DTYPE_VNODE) {
1976 fp->f_offset += offset;
1980 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
1982 fp->f_offset = offset + vattr.va_size;
1985 fp->f_offset = offset;
1992 *res = fp->f_offset;
1999 * lseek_args(int fd, int pad, off_t offset, int whence)
2001 * Reposition read/write file offset.
2004 sys_lseek(struct lseek_args *uap)
2008 error = kern_lseek(uap->fd, uap->offset, uap->whence,
2009 &uap->sysmsg_offset);
2015 kern_access(struct nlookupdata *nd, int aflags)
2020 if ((error = nlookup(nd)) != 0)
2023 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2027 /* Flags == 0 means only check for existence. */
2036 if ((flags & VWRITE) == 0 ||
2037 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
2038 error = VOP_ACCESS(vp, flags, nd->nl_cred);
2041 * If the file handle is stale we have to re-resolve the
2042 * entry. This is a hack at the moment.
2044 if (error == ESTALE) {
2045 cache_setunresolved(&nd->nl_nch);
2046 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2059 * access_args(char *path, int flags)
2061 * Check access permissions.
2064 sys_access(struct access_args *uap)
2066 struct nlookupdata nd;
2069 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2071 error = kern_access(&nd, uap->flags);
2077 kern_stat(struct nlookupdata *nd, struct stat *st)
2083 if ((error = nlookup(nd)) != 0)
2086 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2090 if ((error = vget(vp, LK_SHARED)) != 0)
2092 error = vn_stat(vp, st, nd->nl_cred);
2095 * If the file handle is stale we have to re-resolve the entry. This
2096 * is a hack at the moment.
2098 if (error == ESTALE) {
2099 cache_setunresolved(&nd->nl_nch);
2100 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2111 * stat_args(char *path, struct stat *ub)
2113 * Get file status; this version follows links.
2116 sys_stat(struct stat_args *uap)
2118 struct nlookupdata nd;
2122 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2124 error = kern_stat(&nd, &st);
2126 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2133 * lstat_args(char *path, struct stat *ub)
2135 * Get file status; this version does not follow links.
2138 sys_lstat(struct lstat_args *uap)
2140 struct nlookupdata nd;
2144 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2146 error = kern_stat(&nd, &st);
2148 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2155 * pathconf_Args(char *path, int name)
2157 * Get configurable pathname variables.
2161 sys_pathconf(struct pathconf_args *uap)
2163 struct nlookupdata nd;
2168 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2170 error = nlookup(&nd);
2172 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
2175 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2183 * kern_readlink isn't properly split yet. There is a copyin burried
2184 * in VOP_READLINK().
2187 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2189 struct thread *td = curthread;
2190 struct proc *p = td->td_proc;
2196 if ((error = nlookup(nd)) != 0)
2198 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
2201 if (vp->v_type != VLNK) {
2204 aiov.iov_base = buf;
2205 aiov.iov_len = count;
2206 auio.uio_iov = &aiov;
2207 auio.uio_iovcnt = 1;
2208 auio.uio_offset = 0;
2209 auio.uio_rw = UIO_READ;
2210 auio.uio_segflg = UIO_USERSPACE;
2212 auio.uio_resid = count;
2213 error = VOP_READLINK(vp, &auio, p->p_ucred);
2216 *res = count - auio.uio_resid;
2221 * readlink_args(char *path, char *buf, int count)
2223 * Return target name of a symbolic link.
2226 sys_readlink(struct readlink_args *uap)
2228 struct nlookupdata nd;
2231 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2233 error = kern_readlink(&nd, uap->buf, uap->count,
2234 &uap->sysmsg_result);
2241 setfflags(struct vnode *vp, int flags)
2243 struct thread *td = curthread;
2244 struct proc *p = td->td_proc;
2249 * Prevent non-root users from setting flags on devices. When
2250 * a device is reused, users can retain ownership of the device
2251 * if they are allowed to set flags and programs assume that
2252 * chown can't fail when done as root.
2254 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2255 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2259 * note: vget is required for any operation that might mod the vnode
2260 * so VINACTIVE is properly cleared.
2262 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2264 vattr.va_flags = flags;
2265 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2272 * chflags(char *path, int flags)
2274 * Change flags of a file given a path name.
2278 sys_chflags(struct chflags_args *uap)
2280 struct nlookupdata nd;
2285 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2286 /* XXX Add NLC flag indicating modifying operation? */
2288 error = nlookup(&nd);
2290 error = ncp_writechk(&nd.nl_nch);
2292 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2295 error = setfflags(vp, uap->flags);
2302 * fchflags_args(int fd, int flags)
2304 * Change flags of a file given a file descriptor.
2308 sys_fchflags(struct fchflags_args *uap)
2310 struct thread *td = curthread;
2311 struct proc *p = td->td_proc;
2315 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2317 if (fp->f_nchandle.ncp)
2318 error = ncp_writechk(&fp->f_nchandle);
2320 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2326 setfmode(struct vnode *vp, int mode)
2328 struct thread *td = curthread;
2329 struct proc *p = td->td_proc;
2334 * note: vget is required for any operation that might mod the vnode
2335 * so VINACTIVE is properly cleared.
2337 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2339 vattr.va_mode = mode & ALLPERMS;
2340 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2347 kern_chmod(struct nlookupdata *nd, int mode)
2352 /* XXX Add NLC flag indicating modifying operation? */
2353 if ((error = nlookup(nd)) != 0)
2355 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2357 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2358 error = setfmode(vp, mode);
2364 * chmod_args(char *path, int mode)
2366 * Change mode of a file given path name.
2370 sys_chmod(struct chmod_args *uap)
2372 struct nlookupdata nd;
2375 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2377 error = kern_chmod(&nd, uap->mode);
2383 * lchmod_args(char *path, int mode)
2385 * Change mode of a file given path name (don't follow links.)
2389 sys_lchmod(struct lchmod_args *uap)
2391 struct nlookupdata nd;
2394 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2396 error = kern_chmod(&nd, uap->mode);
2402 * fchmod_args(int fd, int mode)
2404 * Change mode of a file given a file descriptor.
2408 sys_fchmod(struct fchmod_args *uap)
2410 struct thread *td = curthread;
2411 struct proc *p = td->td_proc;
2415 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2417 if (fp->f_nchandle.ncp)
2418 error = ncp_writechk(&fp->f_nchandle);
2420 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2426 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2428 struct thread *td = curthread;
2429 struct proc *p = td->td_proc;
2434 * note: vget is required for any operation that might mod the vnode
2435 * so VINACTIVE is properly cleared.
2437 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2441 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2448 kern_chown(struct nlookupdata *nd, int uid, int gid)
2453 /* XXX Add NLC flag indicating modifying operation? */
2454 if ((error = nlookup(nd)) != 0)
2456 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2458 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
2459 error = setfown(vp, uid, gid);
2465 * chown(char *path, int uid, int gid)
2467 * Set ownership given a path name.
2470 sys_chown(struct chown_args *uap)
2472 struct nlookupdata nd;
2475 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2477 error = kern_chown(&nd, uap->uid, uap->gid);
2483 * lchown_args(char *path, int uid, int gid)
2485 * Set ownership given a path name, do not cross symlinks.
2488 sys_lchown(struct lchown_args *uap)
2490 struct nlookupdata nd;
2493 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2495 error = kern_chown(&nd, uap->uid, uap->gid);
2501 * fchown_args(int fd, int uid, int gid)
2503 * Set ownership given a file descriptor.
2507 sys_fchown(struct fchown_args *uap)
2509 struct thread *td = curthread;
2510 struct proc *p = td->td_proc;
2514 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2516 if (fp->f_nchandle.ncp)
2517 error = ncp_writechk(&fp->f_nchandle);
2519 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2525 getutimes(const struct timeval *tvp, struct timespec *tsp)
2527 struct timeval tv[2];
2531 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2534 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2535 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2541 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2543 struct thread *td = curthread;
2544 struct proc *p = td->td_proc;
2549 * note: vget is required for any operation that might mod the vnode
2550 * so VINACTIVE is properly cleared.
2552 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2554 vattr.va_atime = ts[0];
2555 vattr.va_mtime = ts[1];
2557 vattr.va_vaflags |= VA_UTIMES_NULL;
2558 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2565 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2567 struct timespec ts[2];
2571 if ((error = getutimes(tptr, ts)) != 0)
2573 /* XXX Add NLC flag indicating modifying operation? */
2574 if ((error = nlookup(nd)) != 0)
2576 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2578 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2580 error = setutimes(vp, ts, tptr == NULL);
2586 * utimes_args(char *path, struct timeval *tptr)
2588 * Set the access and modification times of a file.
2591 sys_utimes(struct utimes_args *uap)
2593 struct timeval tv[2];
2594 struct nlookupdata nd;
2598 error = copyin(uap->tptr, tv, sizeof(tv));
2602 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2604 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2610 * lutimes_args(char *path, struct timeval *tptr)
2612 * Set the access and modification times of a file.
2615 sys_lutimes(struct lutimes_args *uap)
2617 struct timeval tv[2];
2618 struct nlookupdata nd;
2622 error = copyin(uap->tptr, tv, sizeof(tv));
2626 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2628 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2634 kern_futimes(int fd, struct timeval *tptr)
2636 struct thread *td = curthread;
2637 struct proc *p = td->td_proc;
2638 struct timespec ts[2];
2642 error = getutimes(tptr, ts);
2645 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2647 if (fp->f_nchandle.ncp)
2648 error = ncp_writechk(&fp->f_nchandle);
2650 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2656 * futimes_args(int fd, struct timeval *tptr)
2658 * Set the access and modification times of a file.
2661 sys_futimes(struct futimes_args *uap)
2663 struct timeval tv[2];
2667 error = copyin(uap->tptr, tv, sizeof(tv));
2672 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2678 kern_truncate(struct nlookupdata *nd, off_t length)
2686 /* XXX Add NLC flag indicating modifying operation? */
2687 if ((error = nlookup(nd)) != 0)
2689 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2691 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
2693 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2697 if (vp->v_type == VDIR) {
2699 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
2700 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2702 vattr.va_size = length;
2703 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2710 * truncate(char *path, int pad, off_t length)
2712 * Truncate a file given its path name.
2715 sys_truncate(struct truncate_args *uap)
2717 struct nlookupdata nd;
2720 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2722 error = kern_truncate(&nd, uap->length);
2728 kern_ftruncate(int fd, off_t length)
2730 struct thread *td = curthread;
2731 struct proc *p = td->td_proc;
2739 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2741 if (fp->f_nchandle.ncp) {
2742 error = ncp_writechk(&fp->f_nchandle);
2746 if ((fp->f_flag & FWRITE) == 0) {
2750 vp = (struct vnode *)fp->f_data;
2751 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2752 if (vp->v_type == VDIR) {
2754 } else if ((error = vn_writechk(vp, NULL)) == 0) {
2756 vattr.va_size = length;
2757 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2766 * ftruncate_args(int fd, int pad, off_t length)
2768 * Truncate a file given a file descriptor.
2771 sys_ftruncate(struct ftruncate_args *uap)
2775 error = kern_ftruncate(uap->fd, uap->length);
2783 * Sync an open file.
2787 sys_fsync(struct fsync_args *uap)
2789 struct thread *td = curthread;
2790 struct proc *p = td->td_proc;
2796 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2798 vp = (struct vnode *)fp->f_data;
2799 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2800 if ((obj = vp->v_object) != NULL)
2801 vm_object_page_clean(obj, 0, 0, 0);
2802 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2803 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2805 error = (*bioops.io_fsync)(vp);
2813 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2815 struct nchandle fnchd;
2816 struct nchandle tnchd;
2817 struct namecache *ncp;
2822 if ((error = nlookup(fromnd)) != 0)
2824 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
2826 fnchd.mount = fromnd->nl_nch.mount;
2830 * unlock the source nch so we can lookup the target nch without
2831 * deadlocking. The target may or may not exist so we do not check
2832 * for a target vp like kern_mkdir() and other creation functions do.
2834 * The source and target directories are ref'd and rechecked after
2835 * everything is relocked to determine if the source or target file
2838 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2839 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2840 cache_unlock(&fromnd->nl_nch);
2842 tond->nl_flags |= NLC_CREATE;
2843 if ((error = nlookup(tond)) != 0) {
2847 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
2851 tnchd.mount = tond->nl_nch.mount;
2855 * If the source and target are the same there is nothing to do
2857 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
2864 * Mount points cannot be renamed or overwritten
2866 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
2875 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2876 * may have become invalid while it was unlocked, nc_vp and nc_mount
2879 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
2880 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2881 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
2882 cache_lock(&fromnd->nl_nch);
2883 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2885 cache_unlock(&tond->nl_nch);
2886 cache_lock(&fromnd->nl_nch);
2887 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
2888 cache_lock(&tond->nl_nch);
2889 cache_resolve(&tond->nl_nch, tond->nl_cred);
2891 fromnd->nl_flags |= NLC_NCPISLOCKED;
2894 * make sure the parent directories linkages are the same
2896 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
2897 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
2904 * Both the source and target must be within the same filesystem and
2905 * in the same filesystem as their parent directories within the
2906 * namecache topology.
2908 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2911 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
2912 mp != tond->nl_nch.mount) {
2919 * Make sure the mount point is writable
2921 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
2928 * If the target exists and either the source or target is a directory,
2929 * then both must be directories.
2931 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
2934 if (tond->nl_nch.ncp->nc_vp) {
2935 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
2937 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
2938 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
2940 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
2946 * You cannot rename a source into itself or a subdirectory of itself.
2947 * We check this by travsersing the target directory upwards looking
2948 * for a match against the source.
2951 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
2952 if (fromnd->nl_nch.ncp == ncp) {
2963 * Even though the namespaces are different, they may still represent
2964 * hardlinks to the same file. The filesystem might have a hard time
2965 * with this so we issue a NREMOVE of the source instead of a NRENAME
2966 * when we detect the situation.
2969 if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
2970 error = VOP_NREMOVE(&fromnd->nl_nch, fromnd->nl_cred);
2972 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
2980 * rename_args(char *from, char *to)
2982 * Rename files. Source and destination must either both be directories,
2983 * or both not be directories. If target is a directory, it must be empty.
2986 sys_rename(struct rename_args *uap)
2988 struct nlookupdata fromnd, tond;
2991 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2993 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2995 error = kern_rename(&fromnd, &tond);
2996 nlookup_done(&tond);
2998 nlookup_done(&fromnd);
3003 kern_mkdir(struct nlookupdata *nd, int mode)
3005 struct thread *td = curthread;
3006 struct proc *p = td->td_proc;
3012 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
3013 if ((error = nlookup(nd)) != 0)
3016 if (nd->nl_nch.ncp->nc_vp)
3018 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3022 vattr.va_type = VDIR;
3023 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
3026 error = VOP_NMKDIR(&nd->nl_nch, &vp, p->p_ucred, &vattr);
3033 * mkdir_args(char *path, int mode)
3035 * Make a directory file.
3039 sys_mkdir(struct mkdir_args *uap)
3041 struct nlookupdata nd;
3044 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3046 error = kern_mkdir(&nd, uap->mode);
3052 kern_rmdir(struct nlookupdata *nd)
3057 nd->nl_flags |= NLC_DELETE;
3058 if ((error = nlookup(nd)) != 0)
3062 * Do not allow directories representing mount points to be
3063 * deleted, even if empty. Check write perms on mount point
3064 * in case the vnode is aliased (aka nullfs).
3066 if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
3068 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3071 error = VOP_NRMDIR(&nd->nl_nch, nd->nl_cred);
3076 * rmdir_args(char *path)
3078 * Remove a directory file.
3082 sys_rmdir(struct rmdir_args *uap)
3084 struct nlookupdata nd;
3087 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3089 error = kern_rmdir(&nd);
3095 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
3096 enum uio_seg direction)
3098 struct thread *td = curthread;
3099 struct proc *p = td->td_proc;
3107 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
3109 if ((fp->f_flag & FREAD) == 0) {
3113 vp = (struct vnode *)fp->f_data;
3115 if (vp->v_type != VDIR) {
3119 aiov.iov_base = buf;
3120 aiov.iov_len = count;
3121 auio.uio_iov = &aiov;
3122 auio.uio_iovcnt = 1;
3123 auio.uio_rw = UIO_READ;
3124 auio.uio_segflg = direction;
3126 auio.uio_resid = count;
3127 loff = auio.uio_offset = fp->f_offset;
3128 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3129 fp->f_offset = auio.uio_offset;
3132 if (count == auio.uio_resid) {
3133 if (union_dircheckp) {
3134 error = union_dircheckp(td, &vp, fp);
3141 if ((vp->v_flag & VROOT) &&
3142 (vp->v_mount->mnt_flag & MNT_UNION)) {
3143 struct vnode *tvp = vp;
3144 vp = vp->v_mount->mnt_vnodecovered;
3156 *res = count - auio.uio_resid;
3163 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3165 * Read a block of directory entries in a file system independent format.
3168 sys_getdirentries(struct getdirentries_args *uap)
3173 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
3174 &uap->sysmsg_result, UIO_USERSPACE);
3177 error = copyout(&base, uap->basep, sizeof(*uap->basep));
3182 * getdents_args(int fd, char *buf, size_t count)
3185 sys_getdents(struct getdents_args *uap)
3189 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
3190 &uap->sysmsg_result, UIO_USERSPACE);
3196 * umask(int newmask)
3198 * Set the mode mask for creation of filesystem nodes.
3203 sys_umask(struct umask_args *uap)
3205 struct thread *td = curthread;
3206 struct proc *p = td->td_proc;
3207 struct filedesc *fdp;
3210 uap->sysmsg_result = fdp->fd_cmask;
3211 fdp->fd_cmask = uap->newmask & ALLPERMS;
3216 * revoke(char *path)
3218 * Void all references to file by ripping underlying filesystem
3223 sys_revoke(struct revoke_args *uap)
3225 struct nlookupdata nd;
3232 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3234 error = nlookup(&nd);
3236 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3237 cred = crhold(nd.nl_cred);
3240 if (vp->v_type != VCHR && vp->v_type != VBLK)
3243 error = VOP_GETATTR(vp, &vattr);
3244 if (error == 0 && cred->cr_uid != vattr.va_uid)
3245 error = suser_cred(cred, PRISON_ROOT);
3246 if (error == 0 && count_udev(vp->v_udev) > 0) {
3249 VOP_REVOKE(vp, REVOKEALL);
3260 * getfh_args(char *fname, fhandle_t *fhp)
3262 * Get (NFS) file handle
3265 sys_getfh(struct getfh_args *uap)
3267 struct thread *td = curthread;
3268 struct nlookupdata nd;
3274 * Must be super user
3276 if ((error = suser(td)) != 0)
3280 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3282 error = nlookup(&nd);
3284 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3287 bzero(&fh, sizeof(fh));
3288 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3289 error = VFS_VPTOFH(vp, &fh.fh_fid);
3292 error = copyout(&fh, uap->fhp, sizeof(fh));
3298 * fhopen_args(const struct fhandle *u_fhp, int flags)
3300 * syscall for the rpc.lockd to use to translate a NFS file handle into
3301 * an open descriptor.
3303 * warning: do not remove the suser() call or this becomes one giant
3307 sys_fhopen(struct fhopen_args *uap)
3309 struct thread *td = curthread;
3310 struct proc *p = td->td_proc;
3315 struct vattr *vap = &vat;
3317 int fmode, mode, error, type;
3323 * Must be super user
3329 fmode = FFLAGS(uap->flags);
3330 /* why not allow a non-read/write open for our lockd? */
3331 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3333 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3336 /* find the mount point */
3337 mp = vfs_getvfs(&fhp.fh_fsid);
3340 /* now give me my vnode, it gets returned to me locked */
3341 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3345 * from now on we have to make sure not
3346 * to forget about the vnode
3347 * any error that causes an abort must vput(vp)
3348 * just set error = err and 'goto bad;'.
3354 if (vp->v_type == VLNK) {
3358 if (vp->v_type == VSOCK) {
3363 if (fmode & (FWRITE | O_TRUNC)) {
3364 if (vp->v_type == VDIR) {
3368 error = vn_writechk(vp, NULL);
3376 error = VOP_ACCESS(vp, mode, p->p_ucred);
3380 if (fmode & O_TRUNC) {
3381 vn_unlock(vp); /* XXX */
3382 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3385 error = VOP_SETATTR(vp, vap, p->p_ucred);
3391 * VOP_OPEN needs the file pointer so it can potentially override
3394 * WARNING! no f_nchandle will be associated when fhopen()ing a
3397 if ((error = falloc(p, &nfp, &indx)) != 0)
3401 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3404 * setting f_ops this way prevents VOP_CLOSE from being
3405 * called or fdrop() releasing the vp from v_data. Since
3406 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3408 fp->f_ops = &badfileops;
3414 * The fp is given its own reference, we still have our ref and lock.
3416 * Assert that all regular files must be created with a VM object.
3418 if (vp->v_type == VREG && vp->v_object == NULL) {
3419 kprintf("fhopen: regular file did not have VM object: %p\n", vp);
3424 * The open was successful. Handle any locking requirements.
3426 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3427 lf.l_whence = SEEK_SET;
3430 if (fmode & O_EXLOCK)
3431 lf.l_type = F_WRLCK;
3433 lf.l_type = F_RDLCK;
3434 if (fmode & FNONBLOCK)
3439 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3441 * release our private reference.
3443 fsetfd(p, NULL, indx);
3448 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3449 fp->f_flag |= FHASLOCK;
3453 * Clean up. Associate the file pointer with the previously
3454 * reserved descriptor and return it.
3457 fsetfd(p, fp, indx);
3459 uap->sysmsg_result = indx;
3463 fsetfd(p, NULL, indx);
3471 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3474 sys_fhstat(struct fhstat_args *uap)
3476 struct thread *td = curthread;
3484 * Must be super user
3490 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3494 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3496 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3498 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3502 error = copyout(&sb, uap->sb, sizeof(sb));
3507 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3510 sys_fhstatfs(struct fhstatfs_args *uap)
3512 struct thread *td = curthread;
3513 struct proc *p = td->td_proc;
3518 char *fullpath, *freepath;
3523 * Must be super user
3525 if ((error = suser(td)))
3528 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3531 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3534 if (p != NULL && !chroot_visible_mnt(mp, p))
3537 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3542 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3545 error = cache_fullpath(p, &mp->mnt_ncmountpt, &fullpath, &freepath);
3548 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3549 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3550 kfree(freepath, M_TEMP);
3552 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3554 bcopy(sp, &sb, sizeof(sb));
3555 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3558 return (copyout(sp, uap->buf, sizeof(*sp)));
3562 * Syscall to push extended attribute configuration information into the
3563 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3564 * a command (int cmd), and attribute name and misc data. For now, the
3565 * attribute name is left in userspace for consumption by the VFS_op.
3566 * It will probably be changed to be copied into sysspace by the
3567 * syscall in the future, once issues with various consumers of the
3568 * attribute code have raised their hands.
3570 * Currently this is used only by UFS Extended Attributes.
3573 sys_extattrctl(struct extattrctl_args *uap)
3575 struct nlookupdata nd;
3581 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3583 error = nlookup(&nd);
3585 mp = nd.nl_nch.mount;
3586 error = VFS_EXTATTRCTL(mp, uap->cmd,
3587 uap->attrname, uap->arg,
3595 * Syscall to set a named extended attribute on a file or directory.
3596 * Accepts attribute name, and a uio structure pointing to the data to set.
3597 * The uio is consumed in the style of writev(). The real work happens
3598 * in VOP_SETEXTATTR().
3601 sys_extattr_set_file(struct extattr_set_file_args *uap)
3603 char attrname[EXTATTR_MAXNAMELEN];
3604 struct iovec aiov[UIO_SMALLIOV];
3605 struct iovec *needfree;
3606 struct nlookupdata nd;
3615 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3620 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3622 error = nlookup(&nd);
3624 error = ncp_writechk(&nd.nl_nch);
3626 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3633 iovlen = uap->iovcnt * sizeof(struct iovec);
3634 if (uap->iovcnt > UIO_SMALLIOV) {
3635 if (uap->iovcnt > UIO_MAXIOV) {
3639 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3645 auio.uio_iovcnt = uap->iovcnt;
3646 auio.uio_rw = UIO_WRITE;
3647 auio.uio_segflg = UIO_USERSPACE;
3648 auio.uio_td = nd.nl_td;
3649 auio.uio_offset = 0;
3650 if ((error = copyin(uap->iovp, iov, iovlen)))
3653 for (i = 0; i < uap->iovcnt; i++) {
3654 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3658 auio.uio_resid += iov->iov_len;
3661 cnt = auio.uio_resid;
3662 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3663 cnt -= auio.uio_resid;
3664 uap->sysmsg_result = cnt;
3669 FREE(needfree, M_IOV);
3674 * Syscall to get a named extended attribute on a file or directory.
3675 * Accepts attribute name, and a uio structure pointing to a buffer for the
3676 * data. The uio is consumed in the style of readv(). The real work
3677 * happens in VOP_GETEXTATTR();
3680 sys_extattr_get_file(struct extattr_get_file_args *uap)
3682 char attrname[EXTATTR_MAXNAMELEN];
3683 struct iovec aiov[UIO_SMALLIOV];
3684 struct iovec *needfree;
3685 struct nlookupdata nd;
3694 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3699 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3701 error = nlookup(&nd);
3703 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3709 iovlen = uap->iovcnt * sizeof (struct iovec);
3711 if (uap->iovcnt > UIO_SMALLIOV) {
3712 if (uap->iovcnt > UIO_MAXIOV) {
3716 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3722 auio.uio_iovcnt = uap->iovcnt;
3723 auio.uio_rw = UIO_READ;
3724 auio.uio_segflg = UIO_USERSPACE;
3725 auio.uio_td = nd.nl_td;
3726 auio.uio_offset = 0;
3727 if ((error = copyin(uap->iovp, iov, iovlen)))
3730 for (i = 0; i < uap->iovcnt; i++) {
3731 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3735 auio.uio_resid += iov->iov_len;
3738 cnt = auio.uio_resid;
3739 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3740 cnt -= auio.uio_resid;
3741 uap->sysmsg_result = cnt;
3746 FREE(needfree, M_IOV);
3751 * Syscall to delete a named extended attribute from a file or directory.
3752 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3755 sys_extattr_delete_file(struct extattr_delete_file_args *uap)
3757 char attrname[EXTATTR_MAXNAMELEN];
3758 struct nlookupdata nd;
3762 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3767 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3769 error = nlookup(&nd);
3771 error = ncp_writechk(&nd.nl_nch);
3773 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3779 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3786 * Determine if the mount is visible to the process.
3789 chroot_visible_mnt(struct mount *mp, struct proc *p)
3791 struct nchandle nch;
3794 * Traverse from the mount point upwards. If we hit the process
3795 * root then the mount point is visible to the process.
3797 nch = mp->mnt_ncmountpt;
3799 if (nch.mount == p->p_fd->fd_nrdir.mount &&
3800 nch.ncp == p->p_fd->fd_nrdir.ncp) {
3803 if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
3804 nch = nch.mount->mnt_ncmounton;
3806 nch.ncp = nch.ncp->nc_parent;