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.91 2006/05/19 07:33:45 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/kern_syscall.h>
67 #include <machine/limits.h>
68 #include <vfs/union/union.h>
69 #include <sys/sysctl.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_zone.h>
73 #include <vm/vm_page.h>
75 #include <sys/file2.h>
77 static int checkvp_chdir (struct vnode *vn, struct thread *td);
78 static void checkdirs (struct vnode *olddp, struct namecache *ncp);
79 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
80 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
81 static int getutimes (const struct timeval *, struct timespec *);
82 static int setfown (struct vnode *, uid_t, gid_t);
83 static int setfmode (struct vnode *, int);
84 static int setfflags (struct vnode *, int);
85 static int setutimes (struct vnode *, const struct timespec *, int);
86 static int usermount = 0; /* if 1, non-root can mount fs. */
88 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
90 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
93 * Virtual File System System Calls
97 * Mount a file system.
100 * mount_args(char *type, char *path, int flags, caddr_t data)
104 mount(struct mount_args *uap)
106 struct thread *td = curthread;
107 struct proc *p = td->td_proc;
109 struct namecache *ncp;
111 struct vfsconf *vfsp;
112 int error, flag = 0, flag2 = 0;
114 struct nlookupdata nd;
115 char fstypename[MFSNAMELEN];
116 struct nlcomponent nlc;
117 struct ucred *cred = p->p_ucred;
120 if (cred->cr_prison != NULL)
122 if (usermount == 0 && (error = suser(td)))
125 * Do not allow NFS export by non-root users.
127 if (uap->flags & MNT_EXPORTED) {
133 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
136 uap->flags |= MNT_NOSUID | MNT_NODEV;
139 * Lookup the requested path and extract the ncp and vnode.
141 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
143 if ((error = nlookup(&nd)) == 0) {
144 if (nd.nl_ncp->nc_vp == NULL)
154 * Extract the locked+refd ncp and cleanup the nd structure
161 * now we have the locked ref'd ncp and unreferenced vnode.
164 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
171 * Now we have an unlocked ref'd ncp and a locked ref'd vp
173 if (uap->flags & MNT_UPDATE) {
174 if ((vp->v_flag & VROOT) == 0) {
181 flag2 = mp->mnt_kern_flag;
183 * We only allow the filesystem to be reloaded if it
184 * is currently mounted read-only.
186 if ((uap->flags & MNT_RELOAD) &&
187 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
190 return (EOPNOTSUPP); /* Needs translation */
193 * Only root, or the user that did the original mount is
194 * permitted to update it.
196 if (mp->mnt_stat.f_owner != cred->cr_uid &&
197 (error = suser(td))) {
202 if (vfs_busy(mp, LK_NOWAIT)) {
207 if ((vp->v_flag & VMOUNT) != 0 ||
208 vp->v_mountedhere != NULL) {
214 vp->v_flag |= VMOUNT;
216 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
221 * If the user is not root, ensure that they own the directory
222 * onto which we are attempting to mount.
224 if ((error = VOP_GETATTR(vp, &va)) ||
225 (va.va_uid != cred->cr_uid && (error = suser(td)))) {
230 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
235 if (vp->v_type != VDIR) {
240 if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) {
245 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
246 if (!strcmp(vfsp->vfc_name, fstypename))
252 /* Only load modules for root (very important!) */
253 if ((error = suser(td)) != 0) {
258 error = linker_load_file(fstypename, &lf);
259 if (error || lf == NULL) {
267 /* lookup again, see if the VFS was loaded */
268 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
269 if (!strcmp(vfsp->vfc_name, fstypename))
274 linker_file_unload(lf);
280 if ((vp->v_flag & VMOUNT) != 0 ||
281 vp->v_mountedhere != NULL) {
286 vp->v_flag |= VMOUNT;
289 * Allocate and initialize the filesystem.
291 mp = malloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
292 TAILQ_INIT(&mp->mnt_nvnodelist);
293 TAILQ_INIT(&mp->mnt_reservedvnlist);
294 TAILQ_INIT(&mp->mnt_jlist);
295 mp->mnt_nvnodelistsize = 0;
296 lockinit(&mp->mnt_lock, "vfslock", 0, LK_NOPAUSE);
297 vfs_busy(mp, LK_NOWAIT);
298 mp->mnt_op = vfsp->vfc_vfsops;
300 vfsp->vfc_refcount++;
301 mp->mnt_stat.f_type = vfsp->vfc_typenum;
302 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
303 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
304 mp->mnt_vnodecovered = vp;
305 mp->mnt_stat.f_owner = cred->cr_uid;
306 mp->mnt_iosize_max = DFLTPHYS;
310 * Set the mount level flags.
312 if (uap->flags & MNT_RDONLY)
313 mp->mnt_flag |= MNT_RDONLY;
314 else if (mp->mnt_flag & MNT_RDONLY)
315 mp->mnt_kern_flag |= MNTK_WANTRDWR;
316 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
317 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
318 MNT_NOSYMFOLLOW | MNT_IGNORE |
319 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
320 mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC |
321 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
322 MNT_NOSYMFOLLOW | MNT_IGNORE |
323 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
325 * Mount the filesystem.
326 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
329 error = VFS_MOUNT(mp, uap->path, uap->data, cred);
330 if (mp->mnt_flag & MNT_UPDATE) {
331 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
332 mp->mnt_flag &= ~MNT_RDONLY;
333 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
334 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
337 mp->mnt_kern_flag = flag2;
340 vp->v_flag &= ~VMOUNT;
345 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
347 * Put the new filesystem on the mount list after root. The mount
348 * point gets its own mnt_ncp which is a special ncp linking the
349 * vnode-under to the root of the new mount. The lookup code
350 * detects the mount point going forward and detects the special
351 * mnt_ncp via NCP_MOUNTPT going backwards.
353 * It is not necessary to invalidate or purge the vnode underneath
354 * because elements under the mount will be given their own glue
358 nlc.nlc_nameptr = "";
360 mp->mnt_ncp = cache_nlookup(ncp, &nlc);
361 cache_setunresolved(mp->mnt_ncp);
362 mp->mnt_ncp->nc_flag |= NCF_MOUNTPT;
363 mp->mnt_ncp->nc_mount = mp;
365 /* XXX get the root of the fs and cache_setvp(mnt_ncp...) */
366 vp->v_flag &= ~VMOUNT;
367 vp->v_mountedhere = mp;
368 mountlist_insert(mp, MNTINS_LAST);
369 checkdirs(vp, mp->mnt_ncp);
370 cache_unlock(mp->mnt_ncp); /* leave ref intact */
372 error = vfs_allocate_syncvnode(mp);
374 if ((error = VFS_START(mp, 0)) != 0)
377 vfs_rm_vnodeops(&mp->mnt_vn_coherency_ops);
378 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
379 vfs_rm_vnodeops(&mp->mnt_vn_norm_ops);
380 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
381 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
382 vp->v_flag &= ~VMOUNT;
383 mp->mnt_vfc->vfc_refcount--;
393 * Scan all active processes to see if any of them have a current
394 * or root directory onto which the new filesystem has just been
395 * mounted. If so, replace them with the new mount point.
397 * The passed ncp is ref'd and locked (from the mount code) and
398 * must be associated with the vnode representing the root of the
402 checkdirs(struct vnode *olddp, struct namecache *ncp)
404 struct filedesc *fdp;
409 if (olddp->v_usecount == 1)
411 mp = olddp->v_mountedhere;
412 if (VFS_ROOT(mp, &newdp))
413 panic("mount: lost mount");
414 cache_setvp(ncp, newdp);
416 if (rootvnode == olddp) {
418 vfs_cache_setroot(newdp, cache_hold(ncp));
421 FOREACH_PROC_IN_SYSTEM(p) {
423 if (fdp->fd_cdir == olddp) {
426 fdp->fd_cdir = newdp;
427 cache_drop(fdp->fd_ncdir);
428 fdp->fd_ncdir = cache_hold(ncp);
430 if (fdp->fd_rdir == olddp) {
433 fdp->fd_rdir = newdp;
434 cache_drop(fdp->fd_nrdir);
435 fdp->fd_nrdir = cache_hold(ncp);
442 * Unmount a file system.
444 * Note: unmount takes a path to the vnode mounted on as argument,
445 * not special file (as before).
448 * umount_args(char *path, int flags)
452 unmount(struct unmount_args *uap)
454 struct thread *td = curthread;
455 struct proc *p = td->td_proc;
456 struct mount *mp = NULL;
458 struct nlookupdata nd;
461 if (p->p_ucred->cr_prison != NULL)
463 if (usermount == 0 && (error = suser(td)))
466 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
468 error = nlookup(&nd);
472 mp = nd.nl_ncp->nc_mount;
475 * Only root, or the user that did the original mount is
476 * permitted to unmount this filesystem.
478 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
483 * Don't allow unmounting the root file system.
485 if (mp->mnt_flag & MNT_ROOTFS) {
491 * Must be the root of the filesystem
493 if (! (nd.nl_ncp->nc_flag & NCF_MOUNTPT)) {
502 return (dounmount(mp, uap->flags));
506 * Do the actual file system unmount.
509 dounmount_interlock(struct mount *mp)
511 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
513 mp->mnt_kern_flag |= MNTK_UNMOUNT;
518 dounmount(struct mount *mp, int flags)
520 struct vnode *coveredvp;
526 * Exclusive access for unmounting purposes
528 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
532 * Allow filesystems to detect that a forced unmount is in progress.
534 if (flags & MNT_FORCE)
535 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
536 lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT);
537 error = lockmgr(&mp->mnt_lock, lflags);
539 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
540 if (mp->mnt_kern_flag & MNTK_MWAIT)
545 if (mp->mnt_flag & MNT_EXPUBLIC)
546 vfs_setpublicfs(NULL, NULL, NULL);
548 vfs_msync(mp, MNT_WAIT);
549 async_flag = mp->mnt_flag & MNT_ASYNC;
550 mp->mnt_flag &=~ MNT_ASYNC;
551 cache_purgevfs(mp); /* remove cache entries for this file sys */
552 if (mp->mnt_syncer != NULL)
553 vrele(mp->mnt_syncer);
554 if (((mp->mnt_flag & MNT_RDONLY) ||
555 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) ||
557 error = VFS_UNMOUNT(mp, flags);
559 if (mp->mnt_syncer == NULL)
560 vfs_allocate_syncvnode(mp);
561 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
562 mp->mnt_flag |= async_flag;
563 lockmgr(&mp->mnt_lock, LK_RELEASE);
564 if (mp->mnt_kern_flag & MNTK_MWAIT)
569 * Clean up any journals still associated with the mount after
570 * filesystem activity has ceased.
572 journal_remove_all_journals(mp,
573 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
575 mountlist_remove(mp);
578 * Remove any installed vnode ops here so the individual VFSs don't
581 vfs_rm_vnodeops(&mp->mnt_vn_coherency_ops);
582 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
583 vfs_rm_vnodeops(&mp->mnt_vn_norm_ops);
584 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
585 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
587 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
588 coveredvp->v_mountedhere = NULL;
590 cache_drop(mp->mnt_ncp);
593 mp->mnt_vfc->vfc_refcount--;
594 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
595 panic("unmount: dangling vnode");
596 lockmgr(&mp->mnt_lock, LK_RELEASE);
597 if (mp->mnt_kern_flag & MNTK_MWAIT)
604 * Sync each mounted filesystem.
608 static int syncprt = 0;
609 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
612 static int sync_callback(struct mount *mp, void *data);
616 sync(struct sync_args *uap)
618 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
621 * print out buffer pool stat information on each sync() call.
631 sync_callback(struct mount *mp, void *data __unused)
635 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
636 asyncflag = mp->mnt_flag & MNT_ASYNC;
637 mp->mnt_flag &= ~MNT_ASYNC;
638 vfs_msync(mp, MNT_NOWAIT);
639 VFS_SYNC(mp, MNT_NOWAIT);
640 mp->mnt_flag |= asyncflag;
645 /* XXX PRISON: could be per prison flag */
646 static int prison_quotas;
648 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
652 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
654 * Change filesystem quotas.
658 quotactl(struct quotactl_args *uap)
660 struct nlookupdata nd;
668 if (p->p_ucred->cr_prison && !prison_quotas)
671 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
673 error = nlookup(&nd);
675 mp = nd.nl_ncp->nc_mount;
676 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
677 uap->arg, nd.nl_cred);
684 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
685 * void *buf, int buflen)
687 * This function operates on a mount point and executes the specified
688 * operation using the specified control data, and possibly returns data.
690 * The actual number of bytes stored in the result buffer is returned, 0
691 * if none, otherwise an error is returned.
695 mountctl(struct mountctl_args *uap)
697 struct thread *td = curthread;
698 struct proc *p = td->td_proc;
706 * Sanity and permissions checks. We must be root.
709 if (p->p_ucred->cr_prison != NULL)
711 if ((error = suser(td)) != 0)
715 * Argument length checks
717 if (uap->ctllen < 0 || uap->ctllen > 1024)
719 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
721 if (uap->path == NULL)
725 * Allocate the necessary buffers and copyin data
727 path = zalloc(namei_zone);
728 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
733 ctl = malloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
734 error = copyin(uap->ctl, ctl, uap->ctllen);
739 buf = malloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
742 * Validate the descriptor
744 fp = holdfp(p->p_fd, uap->fd, -1);
751 * Execute the internal kernel function and clean up.
753 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
756 if (error == 0 && uap->sysmsg_result > 0)
757 error = copyout(buf, uap->buf, uap->sysmsg_result);
760 zfree(namei_zone, path);
769 * Execute a mount control operation by resolving the path to a mount point
770 * and calling vop_mountctl().
773 kern_mountctl(const char *path, int op, struct file *fp,
774 const void *ctl, int ctllen,
775 void *buf, int buflen, int *res)
779 struct nlookupdata nd;
784 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
786 error = nlookup(&nd);
788 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
796 * Must be the root of the filesystem
798 if ((vp->v_flag & VROOT) == 0) {
802 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
809 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
811 struct thread *td = curthread;
812 struct proc *p = td->td_proc;
815 char *fullpath, *freepath;
818 if ((error = nlookup(nd)) != 0)
820 mp = nd->nl_ncp->nc_mount;
822 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
825 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
828 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
829 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
830 free(freepath, M_TEMP);
832 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
833 bcopy(sp, buf, sizeof(*buf));
834 /* Only root should have access to the fsid's. */
836 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
841 * statfs_args(char *path, struct statfs *buf)
843 * Get filesystem statistics.
846 statfs(struct statfs_args *uap)
848 struct nlookupdata nd;
852 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
854 error = kern_statfs(&nd, &buf);
857 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
862 kern_fstatfs(int fd, struct statfs *buf)
864 struct thread *td = curthread;
865 struct proc *p = td->td_proc;
869 char *fullpath, *freepath;
873 error = getvnode(p->p_fd, fd, &fp);
876 mp = ((struct vnode *)fp->f_data)->v_mount;
879 if (fp->f_cred == NULL)
882 error = VFS_STATFS(mp, sp, fp->f_cred);
886 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
889 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
890 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
891 free(freepath, M_TEMP);
893 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
894 bcopy(sp, buf, sizeof(*buf));
896 /* Only root should have access to the fsid's. */
898 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
903 * fstatfs_args(int fd, struct statfs *buf)
905 * Get filesystem statistics.
908 fstatfs(struct fstatfs_args *uap)
913 error = kern_fstatfs(uap->fd, &buf);
916 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
921 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
923 * Get statistics on all filesystems.
926 struct getfsstat_info {
936 static int getfsstat_callback(struct mount *, void *);
940 getfsstat(struct getfsstat_args *uap)
942 struct thread *td = curthread;
943 struct proc *p = td->td_proc;
944 struct getfsstat_info info;
946 bzero(&info, sizeof(info));
947 if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0)
948 info.is_chrooted = 1;
950 info.is_chrooted = 0;
952 info.maxcount = uap->bufsize / sizeof(struct statfs);
953 info.sfsp = uap->buf;
955 info.flags = uap->flags;
958 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
959 if (info.sfsp && info.count > info.maxcount)
960 uap->sysmsg_result = info.maxcount;
962 uap->sysmsg_result = info.count;
967 getfsstat_callback(struct mount *mp, void *data)
969 struct getfsstat_info *info = data;
975 if (info->sfsp && info->count < info->maxcount) {
976 if (info->is_chrooted && !chroot_visible_mnt(mp, info->p))
981 * If MNT_NOWAIT or MNT_LAZY is specified, do not
982 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
983 * overrides MNT_WAIT.
985 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
986 (info->flags & MNT_WAIT)) &&
987 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
990 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
992 error = cache_fullpath(info->p, mp->mnt_ncp,
993 &fullpath, &freepath);
998 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
999 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1000 free(freepath, M_TEMP);
1002 error = copyout(sp, info->sfsp, sizeof(*sp));
1004 info->error = error;
1014 * fchdir_args(int fd)
1016 * Change current working directory to a given file descriptor.
1020 fchdir(struct fchdir_args *uap)
1022 struct thread *td = curthread;
1023 struct proc *p = td->td_proc;
1024 struct filedesc *fdp = p->p_fd;
1025 struct vnode *vp, *ovp;
1028 struct namecache *ncp, *oncp;
1029 struct namecache *nct;
1032 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
1034 vp = (struct vnode *)fp->f_data;
1036 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1037 if (vp->v_type != VDIR || fp->f_ncp == NULL)
1040 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1045 ncp = cache_hold(fp->f_ncp);
1046 while (!error && (mp = vp->v_mountedhere) != NULL) {
1047 error = nlookup_mp(mp, &nct);
1049 cache_unlock(nct); /* leave ref intact */
1052 error = vget(vp, LK_SHARED);
1053 KKASSERT(error == 0);
1060 oncp = fdp->fd_ncdir;
1061 VOP_UNLOCK(vp, 0); /* leave ref intact */
1063 fdp->fd_ncdir = ncp;
1074 kern_chdir(struct nlookupdata *nd)
1076 struct thread *td = curthread;
1077 struct proc *p = td->td_proc;
1078 struct filedesc *fdp = p->p_fd;
1079 struct vnode *vp, *ovp;
1080 struct namecache *oncp;
1083 if ((error = nlookup(nd)) != 0)
1085 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1087 if ((error = vget(vp, LK_SHARED)) != 0)
1090 error = checkvp_chdir(vp, td);
1094 oncp = fdp->fd_ncdir;
1095 cache_unlock(nd->nl_ncp); /* leave reference intact */
1096 fdp->fd_ncdir = nd->nl_ncp;
1108 * chdir_args(char *path)
1110 * Change current working directory (``.'').
1113 chdir(struct chdir_args *uap)
1115 struct nlookupdata nd;
1118 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1120 error = kern_chdir(&nd);
1126 * Helper function for raised chroot(2) security function: Refuse if
1127 * any filedescriptors are open directories.
1130 chroot_refuse_vdir_fds(fdp)
1131 struct filedesc *fdp;
1138 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1139 error = getvnode(fdp, fd, &fp);
1142 vp = (struct vnode *)fp->f_data;
1143 if (vp->v_type != VDIR)
1151 * This sysctl determines if we will allow a process to chroot(2) if it
1152 * has a directory open:
1153 * 0: disallowed for all processes.
1154 * 1: allowed for processes that were not already chroot(2)'ed.
1155 * 2: allowed for all processes.
1158 static int chroot_allow_open_directories = 1;
1160 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1161 &chroot_allow_open_directories, 0, "");
1164 * chroot to the specified namecache entry. We obtain the vp from the
1165 * namecache data. The passed ncp must be locked and referenced and will
1166 * remain locked and referenced on return.
1169 kern_chroot(struct namecache *ncp)
1171 struct thread *td = curthread;
1172 struct proc *p = td->td_proc;
1173 struct filedesc *fdp = p->p_fd;
1178 * Only root can chroot
1180 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1184 * Disallow open directory descriptors (fchdir() breakouts).
1186 if (chroot_allow_open_directories == 0 ||
1187 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1188 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1191 if ((vp = ncp->nc_vp) == NULL)
1194 if ((error = vget(vp, LK_SHARED)) != 0)
1198 * Check the validity of vp as a directory to change to and
1199 * associate it with rdir/jdir.
1201 error = checkvp_chdir(vp, td);
1202 VOP_UNLOCK(vp, 0); /* leave reference intact */
1204 vrele(fdp->fd_rdir);
1205 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1206 cache_drop(fdp->fd_nrdir);
1207 fdp->fd_nrdir = cache_hold(ncp);
1208 if (fdp->fd_jdir == NULL) {
1211 fdp->fd_njdir = cache_hold(ncp);
1220 * chroot_args(char *path)
1222 * Change notion of root (``/'') directory.
1226 chroot(struct chroot_args *uap)
1228 struct thread *td = curthread;
1229 struct nlookupdata nd;
1232 KKASSERT(td->td_proc);
1233 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1238 error = nlookup(&nd);
1240 error = kern_chroot(nd.nl_ncp);
1246 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1247 * determine whether it is legal to chdir to the vnode. The vnode's state
1248 * is not changed by this call.
1251 checkvp_chdir(struct vnode *vp, struct thread *td)
1255 if (vp->v_type != VDIR)
1258 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1263 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1265 struct thread *td = curthread;
1266 struct proc *p = td->td_proc;
1267 struct lwp *lp = td->td_lwp;
1268 struct filedesc *fdp = p->p_fd;
1273 int type, indx, error;
1276 if ((oflags & O_ACCMODE) == O_ACCMODE)
1278 flags = FFLAGS(oflags);
1279 error = falloc(p, &nfp, NULL);
1283 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1286 * XXX p_dupfd is a real mess. It allows a device to return a
1287 * file descriptor to be duplicated rather then doing the open
1293 * Call vn_open() to do the lookup and assign the vnode to the
1294 * file pointer. vn_open() does not change the ref count on fp
1295 * and the vnode, on success, will be inherited by the file pointer
1298 nd->nl_flags |= NLC_LOCKVP;
1299 error = vn_open(nd, fp, flags, cmode);
1303 * handle special fdopen() case. bleh. dupfdopen() is
1304 * responsible for dropping the old contents of ofiles[indx]
1307 * Note that if fsetfd() succeeds it will add a ref to fp
1308 * which represents the fd_files[] assignment. We must still
1309 * drop our reference.
1311 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1312 if (fsetfd(p, fp, &indx) == 0) {
1313 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1316 fdrop(fp); /* our ref */
1319 fdealloc(p, fp, indx);
1322 fdrop(fp); /* our ref */
1323 if (error == ERESTART)
1329 * ref the vnode for ourselves so it can't be ripped out from under
1330 * is. XXX need an ND flag to request that the vnode be returned
1333 vp = (struct vnode *)fp->f_data;
1335 if ((error = fsetfd(p, fp, &indx)) != 0) {
1342 * If no error occurs the vp will have been assigned to the file
1348 * There should be 2 references on the file, one from the descriptor
1349 * table, and one for us.
1351 * Handle the case where someone closed the file (via its file
1352 * descriptor) while we were blocked. The end result should look
1353 * like opening the file succeeded but it was immediately closed.
1355 if (fp->f_count == 1) {
1356 KASSERT(fdp->fd_files[indx].fp != fp,
1357 ("Open file descriptor lost all refs"));
1365 if (flags & (O_EXLOCK | O_SHLOCK)) {
1366 lf.l_whence = SEEK_SET;
1369 if (flags & O_EXLOCK)
1370 lf.l_type = F_WRLCK;
1372 lf.l_type = F_RDLCK;
1373 if (flags & FNONBLOCK)
1378 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1380 * lock request failed. Normally close the descriptor
1381 * but handle the case where someone might have dup()d
1382 * it when we weren't looking. One reference is
1383 * owned by the descriptor array, the other by us.
1386 fdealloc(p, fp, indx);
1390 fp->f_flag |= FHASLOCK;
1395 * Assert that all regular file vnodes were created with a object.
1397 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1398 ("open: regular file has no backing object after vn_open"));
1404 * release our private reference, leaving the one associated with the
1405 * descriptor table intact.
1413 * open_args(char *path, int flags, int mode)
1415 * Check permissions, allocate an open file structure,
1416 * and call the device open routine if any.
1419 open(struct open_args *uap)
1421 struct nlookupdata nd;
1424 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1426 error = kern_open(&nd, uap->flags,
1427 uap->mode, &uap->sysmsg_result);
1434 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1436 struct namecache *ncp;
1437 struct thread *td = curthread;
1438 struct proc *p = td->td_proc;
1446 switch (mode & S_IFMT) {
1452 error = suser_cred(p->p_ucred, PRISON_ROOT);
1459 nd->nl_flags |= NLC_CREATE;
1460 if ((error = nlookup(nd)) != 0)
1467 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1468 vattr.va_rdev = dev;
1471 switch (mode & S_IFMT) {
1472 case S_IFMT: /* used by badsect to flag bad sectors */
1473 vattr.va_type = VBAD;
1476 vattr.va_type = VCHR;
1479 vattr.va_type = VBLK;
1490 error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1493 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1502 * mknod_args(char *path, int mode, int dev)
1504 * Create a special file.
1507 mknod(struct mknod_args *uap)
1509 struct nlookupdata nd;
1512 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1514 error = kern_mknod(&nd, uap->mode, uap->dev);
1520 kern_mkfifo(struct nlookupdata *nd, int mode)
1522 struct namecache *ncp;
1523 struct thread *td = curthread;
1524 struct proc *p = td->td_proc;
1531 nd->nl_flags |= NLC_CREATE;
1532 if ((error = nlookup(nd)) != 0)
1539 vattr.va_type = VFIFO;
1540 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1542 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1549 * mkfifo_args(char *path, int mode)
1551 * Create a named pipe.
1554 mkfifo(struct mkfifo_args *uap)
1556 struct nlookupdata nd;
1559 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1561 error = kern_mkfifo(&nd, uap->mode);
1566 static int hardlink_check_uid = 0;
1567 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1568 &hardlink_check_uid, 0,
1569 "Unprivileged processes cannot create hard links to files owned by other "
1571 static int hardlink_check_gid = 0;
1572 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1573 &hardlink_check_gid, 0,
1574 "Unprivileged processes cannot create hard links to files owned by other "
1578 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1584 * Shortcut if disabled
1586 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1590 * root cred can always hardlink
1592 if (suser_cred(cred, PRISON_ROOT) == 0)
1596 * Otherwise only if the originating file is owned by the
1597 * same user or group. Note that any group is allowed if
1598 * the file is owned by the caller.
1600 error = VOP_GETATTR(vp, &va);
1604 if (hardlink_check_uid) {
1605 if (cred->cr_uid != va.va_uid)
1609 if (hardlink_check_gid) {
1610 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1618 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1620 struct thread *td = curthread;
1625 * Lookup the source and obtained a locked vnode.
1627 * XXX relookup on vget failure / race ?
1630 if ((error = nlookup(nd)) != 0)
1632 vp = nd->nl_ncp->nc_vp;
1633 KKASSERT(vp != NULL);
1634 if (vp->v_type == VDIR)
1635 return (EPERM); /* POSIX */
1636 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1640 * Unlock the source so we can lookup the target without deadlocking
1641 * (XXX vp is locked already, possible other deadlock?). The target
1644 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1645 nd->nl_flags &= ~NLC_NCPISLOCKED;
1646 cache_unlock(nd->nl_ncp);
1648 linknd->nl_flags |= NLC_CREATE;
1649 if ((error = nlookup(linknd)) != 0) {
1653 if (linknd->nl_ncp->nc_vp) {
1659 * Finally run the new API VOP.
1661 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1663 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
1669 * link_args(char *path, char *link)
1671 * Make a hard file link.
1674 link(struct link_args *uap)
1676 struct nlookupdata nd, linknd;
1679 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1681 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1683 error = kern_link(&nd, &linknd);
1684 nlookup_done(&linknd);
1691 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1693 struct namecache *ncp;
1699 nd->nl_flags |= NLC_CREATE;
1700 if ((error = nlookup(nd)) != 0)
1707 vattr.va_mode = mode;
1708 error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
1715 * symlink(char *path, char *link)
1717 * Make a symbolic link.
1720 symlink(struct symlink_args *uap)
1722 struct thread *td = curthread;
1723 struct nlookupdata nd;
1728 path = zalloc(namei_zone);
1729 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1731 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1733 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1734 error = kern_symlink(&nd, path, mode);
1738 zfree(namei_zone, path);
1743 * undelete_args(char *path)
1745 * Delete a whiteout from the filesystem.
1749 undelete(struct undelete_args *uap)
1751 struct nlookupdata nd;
1754 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1756 nd.nl_flags |= NLC_DELETE;
1758 error = nlookup(&nd);
1760 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1766 kern_unlink(struct nlookupdata *nd)
1768 struct namecache *ncp;
1772 nd->nl_flags |= NLC_DELETE;
1773 if ((error = nlookup(nd)) != 0)
1776 error = VOP_NREMOVE(ncp, nd->nl_cred);
1781 * unlink_args(char *path)
1783 * Delete a name from the filesystem.
1786 unlink(struct unlink_args *uap)
1788 struct nlookupdata nd;
1791 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1793 error = kern_unlink(&nd);
1799 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1801 struct thread *td = curthread;
1802 struct proc *p = td->td_proc;
1807 fp = holdfp(p->p_fd, fd, -1);
1810 if (fp->f_type != DTYPE_VNODE) {
1817 fp->f_offset += offset;
1821 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
1823 fp->f_offset = offset + vattr.va_size;
1826 fp->f_offset = offset;
1833 *res = fp->f_offset;
1840 * lseek_args(int fd, int pad, off_t offset, int whence)
1842 * Reposition read/write file offset.
1845 lseek(struct lseek_args *uap)
1849 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1850 &uap->sysmsg_offset);
1856 kern_access(struct nlookupdata *nd, int aflags)
1861 if ((error = nlookup(nd)) != 0)
1864 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1868 /* Flags == 0 means only check for existence. */
1877 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1878 error = VOP_ACCESS(vp, flags, nd->nl_cred);
1881 * If the file handle is stale we have to re-resolve the
1882 * entry. This is a hack at the moment.
1884 if (error == ESTALE) {
1885 cache_setunresolved(nd->nl_ncp);
1886 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1899 * access_args(char *path, int flags)
1901 * Check access permissions.
1904 access(struct access_args *uap)
1906 struct nlookupdata nd;
1909 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1911 error = kern_access(&nd, uap->flags);
1917 kern_stat(struct nlookupdata *nd, struct stat *st)
1923 if ((error = nlookup(nd)) != 0)
1926 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1930 if ((error = vget(vp, LK_SHARED)) != 0)
1932 error = vn_stat(vp, st, nd->nl_cred);
1935 * If the file handle is stale we have to re-resolve the entry. This
1936 * is a hack at the moment.
1938 if (error == ESTALE) {
1939 cache_setunresolved(nd->nl_ncp);
1940 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1951 * stat_args(char *path, struct stat *ub)
1953 * Get file status; this version follows links.
1956 stat(struct stat_args *uap)
1958 struct nlookupdata nd;
1962 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1964 error = kern_stat(&nd, &st);
1966 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1973 * lstat_args(char *path, struct stat *ub)
1975 * Get file status; this version does not follow links.
1978 lstat(struct lstat_args *uap)
1980 struct nlookupdata nd;
1984 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1986 error = kern_stat(&nd, &st);
1988 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1995 * pathconf_Args(char *path, int name)
1997 * Get configurable pathname variables.
2001 pathconf(struct pathconf_args *uap)
2003 struct nlookupdata nd;
2008 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2010 error = nlookup(&nd);
2012 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2015 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2023 * kern_readlink isn't properly split yet. There is a copyin burried
2024 * in VOP_READLINK().
2027 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2029 struct thread *td = curthread;
2030 struct proc *p = td->td_proc;
2036 if ((error = nlookup(nd)) != 0)
2038 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
2041 if (vp->v_type != VLNK) {
2044 aiov.iov_base = buf;
2045 aiov.iov_len = count;
2046 auio.uio_iov = &aiov;
2047 auio.uio_iovcnt = 1;
2048 auio.uio_offset = 0;
2049 auio.uio_rw = UIO_READ;
2050 auio.uio_segflg = UIO_USERSPACE;
2052 auio.uio_resid = count;
2053 error = VOP_READLINK(vp, &auio, p->p_ucred);
2056 *res = count - auio.uio_resid;
2061 * readlink_args(char *path, char *buf, int count)
2063 * Return target name of a symbolic link.
2066 readlink(struct readlink_args *uap)
2068 struct nlookupdata nd;
2071 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2073 error = kern_readlink(&nd, uap->buf, uap->count,
2074 &uap->sysmsg_result);
2081 setfflags(struct vnode *vp, int flags)
2083 struct thread *td = curthread;
2084 struct proc *p = td->td_proc;
2089 * Prevent non-root users from setting flags on devices. When
2090 * a device is reused, users can retain ownership of the device
2091 * if they are allowed to set flags and programs assume that
2092 * chown can't fail when done as root.
2094 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2095 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2099 * note: vget is required for any operation that might mod the vnode
2100 * so VINACTIVE is properly cleared.
2102 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2104 vattr.va_flags = flags;
2105 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2112 * chflags(char *path, int flags)
2114 * Change flags of a file given a path name.
2118 chflags(struct chflags_args *uap)
2120 struct nlookupdata nd;
2125 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2126 /* XXX Add NLC flag indicating modifying operation? */
2128 error = nlookup(&nd);
2130 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2133 error = setfflags(vp, uap->flags);
2140 * fchflags_args(int fd, int flags)
2142 * Change flags of a file given a file descriptor.
2146 fchflags(struct fchflags_args *uap)
2148 struct thread *td = curthread;
2149 struct proc *p = td->td_proc;
2153 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2155 return setfflags((struct vnode *) fp->f_data, uap->flags);
2159 setfmode(struct vnode *vp, int mode)
2161 struct thread *td = curthread;
2162 struct proc *p = td->td_proc;
2167 * note: vget is required for any operation that might mod the vnode
2168 * so VINACTIVE is properly cleared.
2170 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2172 vattr.va_mode = mode & ALLPERMS;
2173 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2180 kern_chmod(struct nlookupdata *nd, int mode)
2185 /* XXX Add NLC flag indicating modifying operation? */
2186 if ((error = nlookup(nd)) != 0)
2188 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2190 error = setfmode(vp, mode);
2196 * chmod_args(char *path, int mode)
2198 * Change mode of a file given path name.
2202 chmod(struct chmod_args *uap)
2204 struct nlookupdata nd;
2207 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2209 error = kern_chmod(&nd, uap->mode);
2215 * lchmod_args(char *path, int mode)
2217 * Change mode of a file given path name (don't follow links.)
2221 lchmod(struct lchmod_args *uap)
2223 struct nlookupdata nd;
2226 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2228 error = kern_chmod(&nd, uap->mode);
2234 * fchmod_args(int fd, int mode)
2236 * Change mode of a file given a file descriptor.
2240 fchmod(struct fchmod_args *uap)
2242 struct thread *td = curthread;
2243 struct proc *p = td->td_proc;
2247 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2249 return setfmode((struct vnode *)fp->f_data, uap->mode);
2253 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2255 struct thread *td = curthread;
2256 struct proc *p = td->td_proc;
2261 * note: vget is required for any operation that might mod the vnode
2262 * so VINACTIVE is properly cleared.
2264 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2268 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2275 kern_chown(struct nlookupdata *nd, int uid, int gid)
2280 /* XXX Add NLC flag indicating modifying operation? */
2281 if ((error = nlookup(nd)) != 0)
2283 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2285 error = setfown(vp, uid, gid);
2291 * chown(char *path, int uid, int gid)
2293 * Set ownership given a path name.
2296 chown(struct chown_args *uap)
2298 struct nlookupdata nd;
2301 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2303 error = kern_chown(&nd, uap->uid, uap->gid);
2309 * lchown_args(char *path, int uid, int gid)
2311 * Set ownership given a path name, do not cross symlinks.
2314 lchown(struct lchown_args *uap)
2316 struct nlookupdata nd;
2319 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2321 error = kern_chown(&nd, uap->uid, uap->gid);
2327 * fchown_args(int fd, int uid, int gid)
2329 * Set ownership given a file descriptor.
2333 fchown(struct fchown_args *uap)
2335 struct thread *td = curthread;
2336 struct proc *p = td->td_proc;
2340 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2342 return setfown((struct vnode *)fp->f_data,
2343 uap->uid, uap->gid);
2347 getutimes(const struct timeval *tvp, struct timespec *tsp)
2349 struct timeval tv[2];
2353 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2356 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2357 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2363 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2365 struct thread *td = curthread;
2366 struct proc *p = td->td_proc;
2371 * note: vget is required for any operation that might mod the vnode
2372 * so VINACTIVE is properly cleared.
2374 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2376 vattr.va_atime = ts[0];
2377 vattr.va_mtime = ts[1];
2379 vattr.va_vaflags |= VA_UTIMES_NULL;
2380 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2387 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2389 struct timespec ts[2];
2393 if ((error = getutimes(tptr, ts)) != 0)
2395 /* XXX Add NLC flag indicating modifying operation? */
2396 if ((error = nlookup(nd)) != 0)
2398 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2400 error = setutimes(vp, ts, tptr == NULL);
2406 * utimes_args(char *path, struct timeval *tptr)
2408 * Set the access and modification times of a file.
2411 utimes(struct utimes_args *uap)
2413 struct timeval tv[2];
2414 struct nlookupdata nd;
2418 error = copyin(uap->tptr, tv, sizeof(tv));
2422 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2424 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2430 * lutimes_args(char *path, struct timeval *tptr)
2432 * Set the access and modification times of a file.
2435 lutimes(struct lutimes_args *uap)
2437 struct timeval tv[2];
2438 struct nlookupdata nd;
2442 error = copyin(uap->tptr, tv, sizeof(tv));
2446 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2448 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2454 kern_futimes(int fd, struct timeval *tptr)
2456 struct thread *td = curthread;
2457 struct proc *p = td->td_proc;
2458 struct timespec ts[2];
2462 error = getutimes(tptr, ts);
2465 error = getvnode(p->p_fd, fd, &fp);
2468 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2473 * futimes_args(int fd, struct timeval *tptr)
2475 * Set the access and modification times of a file.
2478 futimes(struct futimes_args *uap)
2480 struct timeval tv[2];
2484 error = copyin(uap->tptr, tv, sizeof(tv));
2489 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2495 kern_truncate(struct nlookupdata *nd, off_t length)
2503 /* XXX Add NLC flag indicating modifying operation? */
2504 if ((error = nlookup(nd)) != 0)
2506 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2508 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2512 if (vp->v_type == VDIR) {
2514 } else if ((error = vn_writechk(vp)) == 0 &&
2515 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2517 vattr.va_size = length;
2518 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2525 * truncate(char *path, int pad, off_t length)
2527 * Truncate a file given its path name.
2530 truncate(struct truncate_args *uap)
2532 struct nlookupdata nd;
2535 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2537 error = kern_truncate(&nd, uap->length);
2543 kern_ftruncate(int fd, off_t length)
2545 struct thread *td = curthread;
2546 struct proc *p = td->td_proc;
2554 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2556 if ((fp->f_flag & FWRITE) == 0)
2558 vp = (struct vnode *)fp->f_data;
2559 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2560 if (vp->v_type == VDIR)
2562 else if ((error = vn_writechk(vp)) == 0) {
2564 vattr.va_size = length;
2565 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2572 * ftruncate_args(int fd, int pad, off_t length)
2574 * Truncate a file given a file descriptor.
2577 ftruncate(struct ftruncate_args *uap)
2581 error = kern_ftruncate(uap->fd, uap->length);
2589 * Sync an open file.
2593 fsync(struct fsync_args *uap)
2595 struct thread *td = curthread;
2596 struct proc *p = td->td_proc;
2602 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2604 vp = (struct vnode *)fp->f_data;
2605 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2606 if ((obj = vp->v_object) != NULL)
2607 vm_object_page_clean(obj, 0, 0, 0);
2608 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2609 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2611 error = (*bioops.io_fsync)(vp);
2617 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2619 struct namecache *fncpd;
2620 struct namecache *tncpd;
2621 struct namecache *ncp;
2626 if ((error = nlookup(fromnd)) != 0)
2628 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2633 * unlock the source ncp so we can lookup the target ncp without
2634 * deadlocking. The target may or may not exist so we do not check
2635 * for a target vp like kern_mkdir() and other creation functions do.
2637 * The source and target directories are ref'd and rechecked after
2638 * everything is relocked to determine if the source or target file
2641 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2642 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2643 cache_unlock(fromnd->nl_ncp);
2645 tond->nl_flags |= NLC_CREATE;
2646 if ((error = nlookup(tond)) != 0) {
2650 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2657 * If the source and target are the same there is nothing to do
2659 if (fromnd->nl_ncp == tond->nl_ncp) {
2666 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2667 * may have become invalid while it was unlocked, nc_vp and nc_mount
2670 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2671 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2672 } else if (fromnd->nl_ncp > tond->nl_ncp) {
2673 cache_lock(fromnd->nl_ncp);
2674 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2676 cache_unlock(tond->nl_ncp);
2677 cache_lock(fromnd->nl_ncp);
2678 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2679 cache_lock(tond->nl_ncp);
2680 cache_resolve(tond->nl_ncp, tond->nl_cred);
2682 fromnd->nl_flags |= NLC_NCPISLOCKED;
2685 * make sure the parent directories linkages are the same
2687 if (fncpd != fromnd->nl_ncp->nc_parent ||
2688 tncpd != tond->nl_ncp->nc_parent) {
2695 * Both the source and target must be within the same filesystem and
2696 * in the same filesystem as their parent directories within the
2697 * namecache topology.
2699 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2701 mp = fncpd->nc_mount;
2702 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2703 mp != tond->nl_ncp->nc_mount) {
2710 * If the target exists and either the source or target is a directory,
2711 * then both must be directories.
2713 * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2716 if (tond->nl_ncp->nc_vp) {
2717 if (fromnd->nl_ncp->nc_vp == NULL) {
2719 } else if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2720 if (tond->nl_ncp->nc_vp->v_type != VDIR)
2722 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2728 * You cannot rename a source into itself or a subdirectory of itself.
2729 * We check this by travsersing the target directory upwards looking
2730 * for a match against the source.
2733 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2734 if (fromnd->nl_ncp == ncp) {
2745 * Even though the namespaces are different, they may still represent
2746 * hardlinks to the same file. The filesystem might have a hard time
2747 * with this so we issue a NREMOVE of the source instead of a NRENAME
2748 * when we detect the situation.
2751 if (fromnd->nl_ncp->nc_vp == tond->nl_ncp->nc_vp) {
2752 error = VOP_NREMOVE(fromnd->nl_ncp, fromnd->nl_cred);
2754 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp,
2762 * rename_args(char *from, char *to)
2764 * Rename files. Source and destination must either both be directories,
2765 * or both not be directories. If target is a directory, it must be empty.
2768 rename(struct rename_args *uap)
2770 struct nlookupdata fromnd, tond;
2773 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2775 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2777 error = kern_rename(&fromnd, &tond);
2778 nlookup_done(&tond);
2780 nlookup_done(&fromnd);
2785 kern_mkdir(struct nlookupdata *nd, int mode)
2787 struct thread *td = curthread;
2788 struct proc *p = td->td_proc;
2789 struct namecache *ncp;
2795 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2796 if ((error = nlookup(nd)) != 0)
2804 vattr.va_type = VDIR;
2805 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2808 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2815 * mkdir_args(char *path, int mode)
2817 * Make a directory file.
2821 mkdir(struct mkdir_args *uap)
2823 struct nlookupdata nd;
2826 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2828 error = kern_mkdir(&nd, uap->mode);
2834 kern_rmdir(struct nlookupdata *nd)
2836 struct namecache *ncp;
2840 nd->nl_flags |= NLC_DELETE;
2841 if ((error = nlookup(nd)) != 0)
2845 error = VOP_NRMDIR(ncp, nd->nl_cred);
2850 * rmdir_args(char *path)
2852 * Remove a directory file.
2856 rmdir(struct rmdir_args *uap)
2858 struct nlookupdata nd;
2861 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2863 error = kern_rmdir(&nd);
2869 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
2870 enum uio_seg direction)
2872 struct thread *td = curthread;
2873 struct proc *p = td->td_proc;
2881 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2883 if ((fp->f_flag & FREAD) == 0)
2885 vp = (struct vnode *)fp->f_data;
2887 if (vp->v_type != VDIR)
2889 aiov.iov_base = buf;
2890 aiov.iov_len = count;
2891 auio.uio_iov = &aiov;
2892 auio.uio_iovcnt = 1;
2893 auio.uio_rw = UIO_READ;
2894 auio.uio_segflg = direction;
2896 auio.uio_resid = count;
2897 /* vn_lock(vp, LK_SHARED | LK_RETRY); */
2898 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2899 loff = auio.uio_offset = fp->f_offset;
2900 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2901 fp->f_offset = auio.uio_offset;
2905 if (count == auio.uio_resid) {
2906 if (union_dircheckp) {
2907 error = union_dircheckp(td, &vp, fp);
2913 if ((vp->v_flag & VROOT) &&
2914 (vp->v_mount->mnt_flag & MNT_UNION)) {
2915 struct vnode *tvp = vp;
2916 vp = vp->v_mount->mnt_vnodecovered;
2927 *res = count - auio.uio_resid;
2932 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2934 * Read a block of directory entries in a file system independent format.
2937 getdirentries(struct getdirentries_args *uap)
2942 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2943 &uap->sysmsg_result, UIO_USERSPACE);
2946 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2951 * getdents_args(int fd, char *buf, size_t count)
2954 getdents(struct getdents_args *uap)
2958 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2959 &uap->sysmsg_result, UIO_USERSPACE);
2965 * umask(int newmask)
2967 * Set the mode mask for creation of filesystem nodes.
2972 umask(struct umask_args *uap)
2974 struct thread *td = curthread;
2975 struct proc *p = td->td_proc;
2976 struct filedesc *fdp;
2979 uap->sysmsg_result = fdp->fd_cmask;
2980 fdp->fd_cmask = uap->newmask & ALLPERMS;
2985 * revoke(char *path)
2987 * Void all references to file by ripping underlying filesystem
2992 revoke(struct revoke_args *uap)
2994 struct nlookupdata nd;
3001 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3003 error = nlookup(&nd);
3005 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
3006 cred = crhold(nd.nl_cred);
3009 if (vp->v_type != VCHR && vp->v_type != VBLK)
3012 error = VOP_GETATTR(vp, &vattr);
3013 if (error == 0 && cred->cr_uid != vattr.va_uid)
3014 error = suser_cred(cred, PRISON_ROOT);
3015 if (error == 0 && count_udev(vp->v_udev) > 0) {
3016 if ((error = vx_lock(vp)) == 0) {
3017 VOP_REVOKE(vp, REVOKEALL);
3029 * getfh_args(char *fname, fhandle_t *fhp)
3031 * Get (NFS) file handle
3034 getfh(struct getfh_args *uap)
3036 struct thread *td = curthread;
3037 struct nlookupdata nd;
3043 * Must be super user
3045 if ((error = suser(td)) != 0)
3049 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3051 error = nlookup(&nd);
3053 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3056 bzero(&fh, sizeof(fh));
3057 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3058 error = VFS_VPTOFH(vp, &fh.fh_fid);
3061 error = copyout(&fh, uap->fhp, sizeof(fh));
3067 * fhopen_args(const struct fhandle *u_fhp, int flags)
3069 * syscall for the rpc.lockd to use to translate a NFS file handle into
3070 * an open descriptor.
3072 * warning: do not remove the suser() call or this becomes one giant
3076 fhopen(struct fhopen_args *uap)
3078 struct thread *td = curthread;
3079 struct proc *p = td->td_proc;
3084 struct vattr *vap = &vat;
3086 int fmode, mode, error, type;
3092 * Must be super user
3098 fmode = FFLAGS(uap->flags);
3099 /* why not allow a non-read/write open for our lockd? */
3100 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3102 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3105 /* find the mount point */
3106 mp = vfs_getvfs(&fhp.fh_fsid);
3109 /* now give me my vnode, it gets returned to me locked */
3110 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3114 * from now on we have to make sure not
3115 * to forget about the vnode
3116 * any error that causes an abort must vput(vp)
3117 * just set error = err and 'goto bad;'.
3123 if (vp->v_type == VLNK) {
3127 if (vp->v_type == VSOCK) {
3132 if (fmode & (FWRITE | O_TRUNC)) {
3133 if (vp->v_type == VDIR) {
3137 error = vn_writechk(vp);
3145 error = VOP_ACCESS(vp, mode, p->p_ucred);
3149 if (fmode & O_TRUNC) {
3150 VOP_UNLOCK(vp, 0); /* XXX */
3151 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3154 error = VOP_SETATTR(vp, vap, p->p_ucred);
3160 * VOP_OPEN needs the file pointer so it can potentially override
3163 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3166 if ((error = falloc(p, &nfp, NULL)) != 0)
3170 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3173 * setting f_ops this way prevents VOP_CLOSE from being
3174 * called or fdrop() releasing the vp from v_data. Since
3175 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3177 fp->f_ops = &badfileops;
3184 * The fp is given its own reference, we still have our ref and lock.
3186 * Assert that all regular files must be created with a VM object.
3188 if (vp->v_type == VREG && vp->v_object == NULL) {
3189 printf("fhopen: regular file did not have VM object: %p\n", vp);
3195 * The open was successful, associate it with a file descriptor.
3197 if ((error = fsetfd(p, fp, &indx)) != 0) {
3202 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3203 lf.l_whence = SEEK_SET;
3206 if (fmode & O_EXLOCK)
3207 lf.l_type = F_WRLCK;
3209 lf.l_type = F_RDLCK;
3210 if (fmode & FNONBLOCK)
3215 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3217 * lock request failed. Normally close the descriptor
3218 * but handle the case where someone might have dup()d
3219 * or close()d it when we weren't looking.
3221 fdealloc(p, fp, indx);
3224 * release our private reference.
3230 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3231 fp->f_flag |= FHASLOCK;
3236 uap->sysmsg_result = indx;
3245 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3248 fhstat(struct fhstat_args *uap)
3250 struct thread *td = curthread;
3258 * Must be super user
3264 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3268 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3270 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3272 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3276 error = copyout(&sb, uap->sb, sizeof(sb));
3281 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3284 fhstatfs(struct fhstatfs_args *uap)
3286 struct thread *td = curthread;
3287 struct proc *p = td->td_proc;
3292 char *fullpath, *freepath;
3297 * Must be super user
3299 if ((error = suser(td)))
3302 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3305 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3308 if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0 &&
3309 !chroot_visible_mnt(mp, p))
3312 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3317 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3320 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
3323 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3324 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3325 free(freepath, M_TEMP);
3327 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3329 bcopy(sp, &sb, sizeof(sb));
3330 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3333 return (copyout(sp, uap->buf, sizeof(*sp)));
3337 * Syscall to push extended attribute configuration information into the
3338 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3339 * a command (int cmd), and attribute name and misc data. For now, the
3340 * attribute name is left in userspace for consumption by the VFS_op.
3341 * It will probably be changed to be copied into sysspace by the
3342 * syscall in the future, once issues with various consumers of the
3343 * attribute code have raised their hands.
3345 * Currently this is used only by UFS Extended Attributes.
3348 extattrctl(struct extattrctl_args *uap)
3350 struct nlookupdata nd;
3356 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3358 error = nlookup(&nd);
3360 mp = nd.nl_ncp->nc_mount;
3361 error = VFS_EXTATTRCTL(mp, uap->cmd,
3362 uap->attrname, uap->arg,
3370 * Syscall to set a named extended attribute on a file or directory.
3371 * Accepts attribute name, and a uio structure pointing to the data to set.
3372 * The uio is consumed in the style of writev(). The real work happens
3373 * in VOP_SETEXTATTR().
3376 extattr_set_file(struct extattr_set_file_args *uap)
3378 char attrname[EXTATTR_MAXNAMELEN];
3379 struct iovec aiov[UIO_SMALLIOV];
3380 struct iovec *needfree;
3381 struct nlookupdata nd;
3390 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3395 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3397 error = nlookup(&nd);
3399 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3406 iovlen = uap->iovcnt * sizeof(struct iovec);
3407 if (uap->iovcnt > UIO_SMALLIOV) {
3408 if (uap->iovcnt > UIO_MAXIOV) {
3412 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3418 auio.uio_iovcnt = uap->iovcnt;
3419 auio.uio_rw = UIO_WRITE;
3420 auio.uio_segflg = UIO_USERSPACE;
3421 auio.uio_td = nd.nl_td;
3422 auio.uio_offset = 0;
3423 if ((error = copyin(uap->iovp, iov, iovlen)))
3426 for (i = 0; i < uap->iovcnt; i++) {
3427 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3431 auio.uio_resid += iov->iov_len;
3434 cnt = auio.uio_resid;
3435 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3436 cnt -= auio.uio_resid;
3437 uap->sysmsg_result = cnt;
3442 FREE(needfree, M_IOV);
3447 * Syscall to get a named extended attribute on a file or directory.
3448 * Accepts attribute name, and a uio structure pointing to a buffer for the
3449 * data. The uio is consumed in the style of readv(). The real work
3450 * happens in VOP_GETEXTATTR();
3453 extattr_get_file(struct extattr_get_file_args *uap)
3455 char attrname[EXTATTR_MAXNAMELEN];
3456 struct iovec aiov[UIO_SMALLIOV];
3457 struct iovec *needfree;
3458 struct nlookupdata nd;
3467 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3472 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3474 error = nlookup(&nd);
3476 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3482 iovlen = uap->iovcnt * sizeof (struct iovec);
3484 if (uap->iovcnt > UIO_SMALLIOV) {
3485 if (uap->iovcnt > UIO_MAXIOV) {
3489 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3495 auio.uio_iovcnt = uap->iovcnt;
3496 auio.uio_rw = UIO_READ;
3497 auio.uio_segflg = UIO_USERSPACE;
3498 auio.uio_td = nd.nl_td;
3499 auio.uio_offset = 0;
3500 if ((error = copyin(uap->iovp, iov, iovlen)))
3503 for (i = 0; i < uap->iovcnt; i++) {
3504 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3508 auio.uio_resid += iov->iov_len;
3511 cnt = auio.uio_resid;
3512 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3513 cnt -= auio.uio_resid;
3514 uap->sysmsg_result = cnt;
3519 FREE(needfree, M_IOV);
3524 * Syscall to delete a named extended attribute from a file or directory.
3525 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3528 extattr_delete_file(struct extattr_delete_file_args *uap)
3530 char attrname[EXTATTR_MAXNAMELEN];
3531 struct nlookupdata nd;
3535 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3540 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3542 error = nlookup(&nd);
3544 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3550 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3557 chroot_visible_mnt(struct mount *mp, struct proc *p)
3559 struct namecache *ncp;
3561 * First check if this file system is below
3565 while (ncp != NULL && ncp != p->p_fd->fd_nrdir)
3566 ncp = ncp->nc_parent;
3569 * This is not below the chroot path.
3571 * Check if the chroot path is on the same filesystem,
3572 * by determing if we have to cross a mount point
3573 * before reaching mp->mnt_ncp.
3575 ncp = p->p_fd->fd_nrdir;
3576 while (ncp != NULL && ncp != mp->mnt_ncp) {
3577 if (ncp->nc_flag & NCF_MOUNTPT) {
3581 ncp = ncp->nc_parent;
3584 return(ncp != NULL);