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.90 2006/05/19 05:15:35 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;
1803 struct filedesc *fdp = p->p_fd;
1808 if ((u_int)fd >= fdp->fd_nfiles ||
1809 (fp = fdp->fd_files[fd].fp) == NULL)
1811 if (fp->f_type != DTYPE_VNODE)
1815 fp->f_offset += offset;
1818 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
1821 fp->f_offset = offset + vattr.va_size;
1824 fp->f_offset = offset;
1829 *res = fp->f_offset;
1834 * lseek_args(int fd, int pad, off_t offset, int whence)
1836 * Reposition read/write file offset.
1839 lseek(struct lseek_args *uap)
1843 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1844 &uap->sysmsg_offset);
1850 kern_access(struct nlookupdata *nd, int aflags)
1855 if ((error = nlookup(nd)) != 0)
1858 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1862 /* Flags == 0 means only check for existence. */
1871 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1872 error = VOP_ACCESS(vp, flags, nd->nl_cred);
1875 * If the file handle is stale we have to re-resolve the
1876 * entry. This is a hack at the moment.
1878 if (error == ESTALE) {
1879 cache_setunresolved(nd->nl_ncp);
1880 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1893 * access_args(char *path, int flags)
1895 * Check access permissions.
1898 access(struct access_args *uap)
1900 struct nlookupdata nd;
1903 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1905 error = kern_access(&nd, uap->flags);
1911 kern_stat(struct nlookupdata *nd, struct stat *st)
1917 if ((error = nlookup(nd)) != 0)
1920 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1924 if ((error = vget(vp, LK_SHARED)) != 0)
1926 error = vn_stat(vp, st, nd->nl_cred);
1929 * If the file handle is stale we have to re-resolve the entry. This
1930 * is a hack at the moment.
1932 if (error == ESTALE) {
1933 cache_setunresolved(nd->nl_ncp);
1934 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1945 * stat_args(char *path, struct stat *ub)
1947 * Get file status; this version follows links.
1950 stat(struct stat_args *uap)
1952 struct nlookupdata nd;
1956 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1958 error = kern_stat(&nd, &st);
1960 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1967 * lstat_args(char *path, struct stat *ub)
1969 * Get file status; this version does not follow links.
1972 lstat(struct lstat_args *uap)
1974 struct nlookupdata nd;
1978 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1980 error = kern_stat(&nd, &st);
1982 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1989 * pathconf_Args(char *path, int name)
1991 * Get configurable pathname variables.
1995 pathconf(struct pathconf_args *uap)
1997 struct nlookupdata nd;
2002 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2004 error = nlookup(&nd);
2006 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2009 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2017 * kern_readlink isn't properly split yet. There is a copyin burried
2018 * in VOP_READLINK().
2021 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2023 struct thread *td = curthread;
2024 struct proc *p = td->td_proc;
2030 if ((error = nlookup(nd)) != 0)
2032 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
2035 if (vp->v_type != VLNK) {
2038 aiov.iov_base = buf;
2039 aiov.iov_len = count;
2040 auio.uio_iov = &aiov;
2041 auio.uio_iovcnt = 1;
2042 auio.uio_offset = 0;
2043 auio.uio_rw = UIO_READ;
2044 auio.uio_segflg = UIO_USERSPACE;
2046 auio.uio_resid = count;
2047 error = VOP_READLINK(vp, &auio, p->p_ucred);
2050 *res = count - auio.uio_resid;
2055 * readlink_args(char *path, char *buf, int count)
2057 * Return target name of a symbolic link.
2060 readlink(struct readlink_args *uap)
2062 struct nlookupdata nd;
2065 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2067 error = kern_readlink(&nd, uap->buf, uap->count,
2068 &uap->sysmsg_result);
2075 setfflags(struct vnode *vp, int flags)
2077 struct thread *td = curthread;
2078 struct proc *p = td->td_proc;
2083 * Prevent non-root users from setting flags on devices. When
2084 * a device is reused, users can retain ownership of the device
2085 * if they are allowed to set flags and programs assume that
2086 * chown can't fail when done as root.
2088 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2089 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2093 * note: vget is required for any operation that might mod the vnode
2094 * so VINACTIVE is properly cleared.
2096 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2098 vattr.va_flags = flags;
2099 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2106 * chflags(char *path, int flags)
2108 * Change flags of a file given a path name.
2112 chflags(struct chflags_args *uap)
2114 struct nlookupdata nd;
2119 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2120 /* XXX Add NLC flag indicating modifying operation? */
2122 error = nlookup(&nd);
2124 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2127 error = setfflags(vp, uap->flags);
2134 * fchflags_args(int fd, int flags)
2136 * Change flags of a file given a file descriptor.
2140 fchflags(struct fchflags_args *uap)
2142 struct thread *td = curthread;
2143 struct proc *p = td->td_proc;
2147 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2149 return setfflags((struct vnode *) fp->f_data, uap->flags);
2153 setfmode(struct vnode *vp, int mode)
2155 struct thread *td = curthread;
2156 struct proc *p = td->td_proc;
2161 * note: vget is required for any operation that might mod the vnode
2162 * so VINACTIVE is properly cleared.
2164 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2166 vattr.va_mode = mode & ALLPERMS;
2167 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2174 kern_chmod(struct nlookupdata *nd, int mode)
2179 /* XXX Add NLC flag indicating modifying operation? */
2180 if ((error = nlookup(nd)) != 0)
2182 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2184 error = setfmode(vp, mode);
2190 * chmod_args(char *path, int mode)
2192 * Change mode of a file given path name.
2196 chmod(struct chmod_args *uap)
2198 struct nlookupdata nd;
2201 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2203 error = kern_chmod(&nd, uap->mode);
2209 * lchmod_args(char *path, int mode)
2211 * Change mode of a file given path name (don't follow links.)
2215 lchmod(struct lchmod_args *uap)
2217 struct nlookupdata nd;
2220 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2222 error = kern_chmod(&nd, uap->mode);
2228 * fchmod_args(int fd, int mode)
2230 * Change mode of a file given a file descriptor.
2234 fchmod(struct fchmod_args *uap)
2236 struct thread *td = curthread;
2237 struct proc *p = td->td_proc;
2241 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2243 return setfmode((struct vnode *)fp->f_data, uap->mode);
2247 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2249 struct thread *td = curthread;
2250 struct proc *p = td->td_proc;
2255 * note: vget is required for any operation that might mod the vnode
2256 * so VINACTIVE is properly cleared.
2258 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2262 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2269 kern_chown(struct nlookupdata *nd, int uid, int gid)
2274 /* XXX Add NLC flag indicating modifying operation? */
2275 if ((error = nlookup(nd)) != 0)
2277 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2279 error = setfown(vp, uid, gid);
2285 * chown(char *path, int uid, int gid)
2287 * Set ownership given a path name.
2290 chown(struct chown_args *uap)
2292 struct nlookupdata nd;
2295 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2297 error = kern_chown(&nd, uap->uid, uap->gid);
2303 * lchown_args(char *path, int uid, int gid)
2305 * Set ownership given a path name, do not cross symlinks.
2308 lchown(struct lchown_args *uap)
2310 struct nlookupdata nd;
2313 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2315 error = kern_chown(&nd, uap->uid, uap->gid);
2321 * fchown_args(int fd, int uid, int gid)
2323 * Set ownership given a file descriptor.
2327 fchown(struct fchown_args *uap)
2329 struct thread *td = curthread;
2330 struct proc *p = td->td_proc;
2334 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2336 return setfown((struct vnode *)fp->f_data,
2337 uap->uid, uap->gid);
2341 getutimes(const struct timeval *tvp, struct timespec *tsp)
2343 struct timeval tv[2];
2347 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2350 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2351 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2357 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2359 struct thread *td = curthread;
2360 struct proc *p = td->td_proc;
2365 * note: vget is required for any operation that might mod the vnode
2366 * so VINACTIVE is properly cleared.
2368 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2370 vattr.va_atime = ts[0];
2371 vattr.va_mtime = ts[1];
2373 vattr.va_vaflags |= VA_UTIMES_NULL;
2374 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2381 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2383 struct timespec ts[2];
2387 if ((error = getutimes(tptr, ts)) != 0)
2389 /* XXX Add NLC flag indicating modifying operation? */
2390 if ((error = nlookup(nd)) != 0)
2392 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2394 error = setutimes(vp, ts, tptr == NULL);
2400 * utimes_args(char *path, struct timeval *tptr)
2402 * Set the access and modification times of a file.
2405 utimes(struct utimes_args *uap)
2407 struct timeval tv[2];
2408 struct nlookupdata nd;
2412 error = copyin(uap->tptr, tv, sizeof(tv));
2416 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2418 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2424 * lutimes_args(char *path, struct timeval *tptr)
2426 * Set the access and modification times of a file.
2429 lutimes(struct lutimes_args *uap)
2431 struct timeval tv[2];
2432 struct nlookupdata nd;
2436 error = copyin(uap->tptr, tv, sizeof(tv));
2440 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2442 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2448 kern_futimes(int fd, struct timeval *tptr)
2450 struct thread *td = curthread;
2451 struct proc *p = td->td_proc;
2452 struct timespec ts[2];
2456 error = getutimes(tptr, ts);
2459 error = getvnode(p->p_fd, fd, &fp);
2462 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2467 * futimes_args(int fd, struct timeval *tptr)
2469 * Set the access and modification times of a file.
2472 futimes(struct futimes_args *uap)
2474 struct timeval tv[2];
2478 error = copyin(uap->tptr, tv, sizeof(tv));
2483 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2489 kern_truncate(struct nlookupdata *nd, off_t length)
2497 /* XXX Add NLC flag indicating modifying operation? */
2498 if ((error = nlookup(nd)) != 0)
2500 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2502 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2506 if (vp->v_type == VDIR) {
2508 } else if ((error = vn_writechk(vp)) == 0 &&
2509 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2511 vattr.va_size = length;
2512 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2519 * truncate(char *path, int pad, off_t length)
2521 * Truncate a file given its path name.
2524 truncate(struct truncate_args *uap)
2526 struct nlookupdata nd;
2529 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2531 error = kern_truncate(&nd, uap->length);
2537 kern_ftruncate(int fd, off_t length)
2539 struct thread *td = curthread;
2540 struct proc *p = td->td_proc;
2548 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2550 if ((fp->f_flag & FWRITE) == 0)
2552 vp = (struct vnode *)fp->f_data;
2553 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2554 if (vp->v_type == VDIR)
2556 else if ((error = vn_writechk(vp)) == 0) {
2558 vattr.va_size = length;
2559 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2566 * ftruncate_args(int fd, int pad, off_t length)
2568 * Truncate a file given a file descriptor.
2571 ftruncate(struct ftruncate_args *uap)
2575 error = kern_ftruncate(uap->fd, uap->length);
2583 * Sync an open file.
2587 fsync(struct fsync_args *uap)
2589 struct thread *td = curthread;
2590 struct proc *p = td->td_proc;
2596 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2598 vp = (struct vnode *)fp->f_data;
2599 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2600 if ((obj = vp->v_object) != NULL)
2601 vm_object_page_clean(obj, 0, 0, 0);
2602 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 &&
2603 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2605 error = (*bioops.io_fsync)(vp);
2611 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2613 struct namecache *fncpd;
2614 struct namecache *tncpd;
2615 struct namecache *ncp;
2620 if ((error = nlookup(fromnd)) != 0)
2622 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2627 * unlock the source ncp so we can lookup the target ncp without
2628 * deadlocking. The target may or may not exist so we do not check
2629 * for a target vp like kern_mkdir() and other creation functions do.
2631 * The source and target directories are ref'd and rechecked after
2632 * everything is relocked to determine if the source or target file
2635 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2636 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2637 cache_unlock(fromnd->nl_ncp);
2639 tond->nl_flags |= NLC_CREATE;
2640 if ((error = nlookup(tond)) != 0) {
2644 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2651 * If the source and target are the same there is nothing to do
2653 if (fromnd->nl_ncp == tond->nl_ncp) {
2660 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2661 * may have become invalid while it was unlocked, nc_vp and nc_mount
2664 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2665 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2666 } else if (fromnd->nl_ncp > tond->nl_ncp) {
2667 cache_lock(fromnd->nl_ncp);
2668 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2670 cache_unlock(tond->nl_ncp);
2671 cache_lock(fromnd->nl_ncp);
2672 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2673 cache_lock(tond->nl_ncp);
2674 cache_resolve(tond->nl_ncp, tond->nl_cred);
2676 fromnd->nl_flags |= NLC_NCPISLOCKED;
2679 * make sure the parent directories linkages are the same
2681 if (fncpd != fromnd->nl_ncp->nc_parent ||
2682 tncpd != tond->nl_ncp->nc_parent) {
2689 * Both the source and target must be within the same filesystem and
2690 * in the same filesystem as their parent directories within the
2691 * namecache topology.
2693 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2695 mp = fncpd->nc_mount;
2696 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2697 mp != tond->nl_ncp->nc_mount) {
2704 * If the target exists and either the source or target is a directory,
2705 * then both must be directories.
2707 * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2710 if (tond->nl_ncp->nc_vp) {
2711 if (fromnd->nl_ncp->nc_vp == NULL) {
2713 } else if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2714 if (tond->nl_ncp->nc_vp->v_type != VDIR)
2716 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2722 * You cannot rename a source into itself or a subdirectory of itself.
2723 * We check this by travsersing the target directory upwards looking
2724 * for a match against the source.
2727 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2728 if (fromnd->nl_ncp == ncp) {
2739 * Even though the namespaces are different, they may still represent
2740 * hardlinks to the same file. The filesystem might have a hard time
2741 * with this so we issue a NREMOVE of the source instead of a NRENAME
2742 * when we detect the situation.
2745 if (fromnd->nl_ncp->nc_vp == tond->nl_ncp->nc_vp) {
2746 error = VOP_NREMOVE(fromnd->nl_ncp, fromnd->nl_cred);
2748 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp,
2756 * rename_args(char *from, char *to)
2758 * Rename files. Source and destination must either both be directories,
2759 * or both not be directories. If target is a directory, it must be empty.
2762 rename(struct rename_args *uap)
2764 struct nlookupdata fromnd, tond;
2767 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2769 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2771 error = kern_rename(&fromnd, &tond);
2772 nlookup_done(&tond);
2774 nlookup_done(&fromnd);
2779 kern_mkdir(struct nlookupdata *nd, int mode)
2781 struct thread *td = curthread;
2782 struct proc *p = td->td_proc;
2783 struct namecache *ncp;
2789 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2790 if ((error = nlookup(nd)) != 0)
2798 vattr.va_type = VDIR;
2799 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2802 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2809 * mkdir_args(char *path, int mode)
2811 * Make a directory file.
2815 mkdir(struct mkdir_args *uap)
2817 struct nlookupdata nd;
2820 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2822 error = kern_mkdir(&nd, uap->mode);
2828 kern_rmdir(struct nlookupdata *nd)
2830 struct namecache *ncp;
2834 nd->nl_flags |= NLC_DELETE;
2835 if ((error = nlookup(nd)) != 0)
2839 error = VOP_NRMDIR(ncp, nd->nl_cred);
2844 * rmdir_args(char *path)
2846 * Remove a directory file.
2850 rmdir(struct rmdir_args *uap)
2852 struct nlookupdata nd;
2855 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2857 error = kern_rmdir(&nd);
2863 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
2864 enum uio_seg direction)
2866 struct thread *td = curthread;
2867 struct proc *p = td->td_proc;
2875 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2877 if ((fp->f_flag & FREAD) == 0)
2879 vp = (struct vnode *)fp->f_data;
2881 if (vp->v_type != VDIR)
2883 aiov.iov_base = buf;
2884 aiov.iov_len = count;
2885 auio.uio_iov = &aiov;
2886 auio.uio_iovcnt = 1;
2887 auio.uio_rw = UIO_READ;
2888 auio.uio_segflg = direction;
2890 auio.uio_resid = count;
2891 /* vn_lock(vp, LK_SHARED | LK_RETRY); */
2892 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2893 loff = auio.uio_offset = fp->f_offset;
2894 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2895 fp->f_offset = auio.uio_offset;
2899 if (count == auio.uio_resid) {
2900 if (union_dircheckp) {
2901 error = union_dircheckp(td, &vp, fp);
2907 if ((vp->v_flag & VROOT) &&
2908 (vp->v_mount->mnt_flag & MNT_UNION)) {
2909 struct vnode *tvp = vp;
2910 vp = vp->v_mount->mnt_vnodecovered;
2921 *res = count - auio.uio_resid;
2926 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2928 * Read a block of directory entries in a file system independent format.
2931 getdirentries(struct getdirentries_args *uap)
2936 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2937 &uap->sysmsg_result, UIO_USERSPACE);
2940 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2945 * getdents_args(int fd, char *buf, size_t count)
2948 getdents(struct getdents_args *uap)
2952 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2953 &uap->sysmsg_result, UIO_USERSPACE);
2959 * umask(int newmask)
2961 * Set the mode mask for creation of filesystem nodes.
2966 umask(struct umask_args *uap)
2968 struct thread *td = curthread;
2969 struct proc *p = td->td_proc;
2970 struct filedesc *fdp;
2973 uap->sysmsg_result = fdp->fd_cmask;
2974 fdp->fd_cmask = uap->newmask & ALLPERMS;
2979 * revoke(char *path)
2981 * Void all references to file by ripping underlying filesystem
2986 revoke(struct revoke_args *uap)
2988 struct nlookupdata nd;
2995 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2997 error = nlookup(&nd);
2999 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
3000 cred = crhold(nd.nl_cred);
3003 if (vp->v_type != VCHR && vp->v_type != VBLK)
3006 error = VOP_GETATTR(vp, &vattr);
3007 if (error == 0 && cred->cr_uid != vattr.va_uid)
3008 error = suser_cred(cred, PRISON_ROOT);
3009 if (error == 0 && count_udev(vp->v_udev) > 0) {
3010 if ((error = vx_lock(vp)) == 0) {
3011 VOP_REVOKE(vp, REVOKEALL);
3023 * Convert a user file descriptor to a kernel file entry.
3026 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
3030 if ((u_int)fd >= fdp->fd_nfiles ||
3031 (fp = fdp->fd_files[fd].fp) == NULL)
3033 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
3039 * getfh_args(char *fname, fhandle_t *fhp)
3041 * Get (NFS) file handle
3044 getfh(struct getfh_args *uap)
3046 struct thread *td = curthread;
3047 struct nlookupdata nd;
3053 * Must be super user
3055 if ((error = suser(td)) != 0)
3059 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3061 error = nlookup(&nd);
3063 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3066 bzero(&fh, sizeof(fh));
3067 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3068 error = VFS_VPTOFH(vp, &fh.fh_fid);
3071 error = copyout(&fh, uap->fhp, sizeof(fh));
3077 * fhopen_args(const struct fhandle *u_fhp, int flags)
3079 * syscall for the rpc.lockd to use to translate a NFS file handle into
3080 * an open descriptor.
3082 * warning: do not remove the suser() call or this becomes one giant
3086 fhopen(struct fhopen_args *uap)
3088 struct thread *td = curthread;
3089 struct proc *p = td->td_proc;
3094 struct vattr *vap = &vat;
3096 int fmode, mode, error, type;
3102 * Must be super user
3108 fmode = FFLAGS(uap->flags);
3109 /* why not allow a non-read/write open for our lockd? */
3110 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3112 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3115 /* find the mount point */
3116 mp = vfs_getvfs(&fhp.fh_fsid);
3119 /* now give me my vnode, it gets returned to me locked */
3120 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3124 * from now on we have to make sure not
3125 * to forget about the vnode
3126 * any error that causes an abort must vput(vp)
3127 * just set error = err and 'goto bad;'.
3133 if (vp->v_type == VLNK) {
3137 if (vp->v_type == VSOCK) {
3142 if (fmode & (FWRITE | O_TRUNC)) {
3143 if (vp->v_type == VDIR) {
3147 error = vn_writechk(vp);
3155 error = VOP_ACCESS(vp, mode, p->p_ucred);
3159 if (fmode & O_TRUNC) {
3160 VOP_UNLOCK(vp, 0); /* XXX */
3161 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3164 error = VOP_SETATTR(vp, vap, p->p_ucred);
3170 * VOP_OPEN needs the file pointer so it can potentially override
3173 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3176 if ((error = falloc(p, &nfp, NULL)) != 0)
3180 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3183 * setting f_ops this way prevents VOP_CLOSE from being
3184 * called or fdrop() releasing the vp from v_data. Since
3185 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3187 fp->f_ops = &badfileops;
3194 * The fp is given its own reference, we still have our ref and lock.
3196 * Assert that all regular files must be created with a VM object.
3198 if (vp->v_type == VREG && vp->v_object == NULL) {
3199 printf("fhopen: regular file did not have VM object: %p\n", vp);
3205 * The open was successful, associate it with a file descriptor.
3207 if ((error = fsetfd(p, fp, &indx)) != 0) {
3212 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3213 lf.l_whence = SEEK_SET;
3216 if (fmode & O_EXLOCK)
3217 lf.l_type = F_WRLCK;
3219 lf.l_type = F_RDLCK;
3220 if (fmode & FNONBLOCK)
3225 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3227 * lock request failed. Normally close the descriptor
3228 * but handle the case where someone might have dup()d
3229 * or close()d it when we weren't looking.
3231 fdealloc(p, fp, indx);
3234 * release our private reference.
3240 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3241 fp->f_flag |= FHASLOCK;
3246 uap->sysmsg_result = indx;
3255 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3258 fhstat(struct fhstat_args *uap)
3260 struct thread *td = curthread;
3268 * Must be super user
3274 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3278 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3280 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3282 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3286 error = copyout(&sb, uap->sb, sizeof(sb));
3291 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3294 fhstatfs(struct fhstatfs_args *uap)
3296 struct thread *td = curthread;
3297 struct proc *p = td->td_proc;
3302 char *fullpath, *freepath;
3307 * Must be super user
3309 if ((error = suser(td)))
3312 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3315 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3318 if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0 &&
3319 !chroot_visible_mnt(mp, p))
3322 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3327 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3330 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
3333 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3334 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3335 free(freepath, M_TEMP);
3337 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3339 bcopy(sp, &sb, sizeof(sb));
3340 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3343 return (copyout(sp, uap->buf, sizeof(*sp)));
3347 * Syscall to push extended attribute configuration information into the
3348 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3349 * a command (int cmd), and attribute name and misc data. For now, the
3350 * attribute name is left in userspace for consumption by the VFS_op.
3351 * It will probably be changed to be copied into sysspace by the
3352 * syscall in the future, once issues with various consumers of the
3353 * attribute code have raised their hands.
3355 * Currently this is used only by UFS Extended Attributes.
3358 extattrctl(struct extattrctl_args *uap)
3360 struct nlookupdata nd;
3366 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3368 error = nlookup(&nd);
3370 mp = nd.nl_ncp->nc_mount;
3371 error = VFS_EXTATTRCTL(mp, uap->cmd,
3372 uap->attrname, uap->arg,
3380 * Syscall to set a named extended attribute on a file or directory.
3381 * Accepts attribute name, and a uio structure pointing to the data to set.
3382 * The uio is consumed in the style of writev(). The real work happens
3383 * in VOP_SETEXTATTR().
3386 extattr_set_file(struct extattr_set_file_args *uap)
3388 char attrname[EXTATTR_MAXNAMELEN];
3389 struct iovec aiov[UIO_SMALLIOV];
3390 struct iovec *needfree;
3391 struct nlookupdata nd;
3400 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3405 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3407 error = nlookup(&nd);
3409 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3416 iovlen = uap->iovcnt * sizeof(struct iovec);
3417 if (uap->iovcnt > UIO_SMALLIOV) {
3418 if (uap->iovcnt > UIO_MAXIOV) {
3422 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3428 auio.uio_iovcnt = uap->iovcnt;
3429 auio.uio_rw = UIO_WRITE;
3430 auio.uio_segflg = UIO_USERSPACE;
3431 auio.uio_td = nd.nl_td;
3432 auio.uio_offset = 0;
3433 if ((error = copyin(uap->iovp, iov, iovlen)))
3436 for (i = 0; i < uap->iovcnt; i++) {
3437 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3441 auio.uio_resid += iov->iov_len;
3444 cnt = auio.uio_resid;
3445 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3446 cnt -= auio.uio_resid;
3447 uap->sysmsg_result = cnt;
3452 FREE(needfree, M_IOV);
3457 * Syscall to get a named extended attribute on a file or directory.
3458 * Accepts attribute name, and a uio structure pointing to a buffer for the
3459 * data. The uio is consumed in the style of readv(). The real work
3460 * happens in VOP_GETEXTATTR();
3463 extattr_get_file(struct extattr_get_file_args *uap)
3465 char attrname[EXTATTR_MAXNAMELEN];
3466 struct iovec aiov[UIO_SMALLIOV];
3467 struct iovec *needfree;
3468 struct nlookupdata nd;
3477 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3482 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3484 error = nlookup(&nd);
3486 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3492 iovlen = uap->iovcnt * sizeof (struct iovec);
3494 if (uap->iovcnt > UIO_SMALLIOV) {
3495 if (uap->iovcnt > UIO_MAXIOV) {
3499 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3505 auio.uio_iovcnt = uap->iovcnt;
3506 auio.uio_rw = UIO_READ;
3507 auio.uio_segflg = UIO_USERSPACE;
3508 auio.uio_td = nd.nl_td;
3509 auio.uio_offset = 0;
3510 if ((error = copyin(uap->iovp, iov, iovlen)))
3513 for (i = 0; i < uap->iovcnt; i++) {
3514 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3518 auio.uio_resid += iov->iov_len;
3521 cnt = auio.uio_resid;
3522 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3523 cnt -= auio.uio_resid;
3524 uap->sysmsg_result = cnt;
3529 FREE(needfree, M_IOV);
3534 * Syscall to delete a named extended attribute from a file or directory.
3535 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3538 extattr_delete_file(struct extattr_delete_file_args *uap)
3540 char attrname[EXTATTR_MAXNAMELEN];
3541 struct nlookupdata nd;
3545 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3550 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3552 error = nlookup(&nd);
3554 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3560 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3567 chroot_visible_mnt(struct mount *mp, struct proc *p)
3569 struct namecache *ncp;
3571 * First check if this file system is below
3575 while (ncp != NULL && ncp != p->p_fd->fd_nrdir)
3576 ncp = ncp->nc_parent;
3579 * This is not below the chroot path.
3581 * Check if the chroot path is on the same filesystem,
3582 * by determing if we have to cross a mount point
3583 * before reaching mp->mnt_ncp.
3585 ncp = p->p_fd->fd_nrdir;
3586 while (ncp != NULL && ncp != mp->mnt_ncp) {
3587 if (ncp->nc_flag & NCF_MOUNTPT) {
3591 ncp = ncp->nc_parent;
3594 return(ncp != NULL);