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.93 2006/05/24 03:23:31 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 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
875 mp = ((struct vnode *)fp->f_data)->v_mount;
880 if (fp->f_cred == NULL) {
885 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
888 if ((error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath)) != 0)
890 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
891 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
892 free(freepath, M_TEMP);
894 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
895 bcopy(sp, buf, sizeof(*buf));
897 /* Only root should have access to the fsid's. */
899 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
907 * fstatfs_args(int fd, struct statfs *buf)
909 * Get filesystem statistics.
912 fstatfs(struct fstatfs_args *uap)
917 error = kern_fstatfs(uap->fd, &buf);
920 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
925 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
927 * Get statistics on all filesystems.
930 struct getfsstat_info {
940 static int getfsstat_callback(struct mount *, void *);
944 getfsstat(struct getfsstat_args *uap)
946 struct thread *td = curthread;
947 struct proc *p = td->td_proc;
948 struct getfsstat_info info;
950 bzero(&info, sizeof(info));
951 if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0)
952 info.is_chrooted = 1;
954 info.is_chrooted = 0;
956 info.maxcount = uap->bufsize / sizeof(struct statfs);
957 info.sfsp = uap->buf;
959 info.flags = uap->flags;
962 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
963 if (info.sfsp && info.count > info.maxcount)
964 uap->sysmsg_result = info.maxcount;
966 uap->sysmsg_result = info.count;
971 getfsstat_callback(struct mount *mp, void *data)
973 struct getfsstat_info *info = data;
979 if (info->sfsp && info->count < info->maxcount) {
980 if (info->is_chrooted && !chroot_visible_mnt(mp, info->p))
985 * If MNT_NOWAIT or MNT_LAZY is specified, do not
986 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
987 * overrides MNT_WAIT.
989 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
990 (info->flags & MNT_WAIT)) &&
991 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
994 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
996 error = cache_fullpath(info->p, mp->mnt_ncp,
997 &fullpath, &freepath);
1002 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1003 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1004 free(freepath, M_TEMP);
1006 error = copyout(sp, info->sfsp, sizeof(*sp));
1008 info->error = error;
1018 * fchdir_args(int fd)
1020 * Change current working directory to a given file descriptor.
1024 fchdir(struct fchdir_args *uap)
1026 struct thread *td = curthread;
1027 struct proc *p = td->td_proc;
1028 struct filedesc *fdp = p->p_fd;
1029 struct vnode *vp, *ovp;
1032 struct namecache *ncp, *oncp;
1033 struct namecache *nct;
1036 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
1038 vp = (struct vnode *)fp->f_data;
1040 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1041 if (vp->v_type != VDIR || fp->f_ncp == NULL)
1044 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
1050 ncp = cache_hold(fp->f_ncp);
1051 while (!error && (mp = vp->v_mountedhere) != NULL) {
1052 error = nlookup_mp(mp, &nct);
1054 cache_unlock(nct); /* leave ref intact */
1057 error = vget(vp, LK_SHARED);
1058 KKASSERT(error == 0);
1065 oncp = fdp->fd_ncdir;
1066 VOP_UNLOCK(vp, 0); /* leave ref intact */
1068 fdp->fd_ncdir = ncp;
1080 kern_chdir(struct nlookupdata *nd)
1082 struct thread *td = curthread;
1083 struct proc *p = td->td_proc;
1084 struct filedesc *fdp = p->p_fd;
1085 struct vnode *vp, *ovp;
1086 struct namecache *oncp;
1089 if ((error = nlookup(nd)) != 0)
1091 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1093 if ((error = vget(vp, LK_SHARED)) != 0)
1096 error = checkvp_chdir(vp, td);
1100 oncp = fdp->fd_ncdir;
1101 cache_unlock(nd->nl_ncp); /* leave reference intact */
1102 fdp->fd_ncdir = nd->nl_ncp;
1114 * chdir_args(char *path)
1116 * Change current working directory (``.'').
1119 chdir(struct chdir_args *uap)
1121 struct nlookupdata nd;
1124 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1126 error = kern_chdir(&nd);
1132 * Helper function for raised chroot(2) security function: Refuse if
1133 * any filedescriptors are open directories.
1136 chroot_refuse_vdir_fds(fdp)
1137 struct filedesc *fdp;
1144 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1145 if ((error = holdvnode(fdp, fd, &fp)) != 0)
1147 vp = (struct vnode *)fp->f_data;
1148 if (vp->v_type != VDIR) {
1159 * This sysctl determines if we will allow a process to chroot(2) if it
1160 * has a directory open:
1161 * 0: disallowed for all processes.
1162 * 1: allowed for processes that were not already chroot(2)'ed.
1163 * 2: allowed for all processes.
1166 static int chroot_allow_open_directories = 1;
1168 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1169 &chroot_allow_open_directories, 0, "");
1172 * chroot to the specified namecache entry. We obtain the vp from the
1173 * namecache data. The passed ncp must be locked and referenced and will
1174 * remain locked and referenced on return.
1177 kern_chroot(struct namecache *ncp)
1179 struct thread *td = curthread;
1180 struct proc *p = td->td_proc;
1181 struct filedesc *fdp = p->p_fd;
1186 * Only root can chroot
1188 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1192 * Disallow open directory descriptors (fchdir() breakouts).
1194 if (chroot_allow_open_directories == 0 ||
1195 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1196 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1199 if ((vp = ncp->nc_vp) == NULL)
1202 if ((error = vget(vp, LK_SHARED)) != 0)
1206 * Check the validity of vp as a directory to change to and
1207 * associate it with rdir/jdir.
1209 error = checkvp_chdir(vp, td);
1210 VOP_UNLOCK(vp, 0); /* leave reference intact */
1212 vrele(fdp->fd_rdir);
1213 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1214 cache_drop(fdp->fd_nrdir);
1215 fdp->fd_nrdir = cache_hold(ncp);
1216 if (fdp->fd_jdir == NULL) {
1219 fdp->fd_njdir = cache_hold(ncp);
1228 * chroot_args(char *path)
1230 * Change notion of root (``/'') directory.
1234 chroot(struct chroot_args *uap)
1236 struct thread *td = curthread;
1237 struct nlookupdata nd;
1240 KKASSERT(td->td_proc);
1241 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1246 error = nlookup(&nd);
1248 error = kern_chroot(nd.nl_ncp);
1254 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1255 * determine whether it is legal to chdir to the vnode. The vnode's state
1256 * is not changed by this call.
1259 checkvp_chdir(struct vnode *vp, struct thread *td)
1263 if (vp->v_type != VDIR)
1266 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
1271 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1273 struct thread *td = curthread;
1274 struct proc *p = td->td_proc;
1275 struct lwp *lp = td->td_lwp;
1276 struct filedesc *fdp = p->p_fd;
1281 int type, indx, error;
1284 if ((oflags & O_ACCMODE) == O_ACCMODE)
1286 flags = FFLAGS(oflags);
1287 error = falloc(p, &nfp, NULL);
1291 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1294 * XXX p_dupfd is a real mess. It allows a device to return a
1295 * file descriptor to be duplicated rather then doing the open
1301 * Call vn_open() to do the lookup and assign the vnode to the
1302 * file pointer. vn_open() does not change the ref count on fp
1303 * and the vnode, on success, will be inherited by the file pointer
1306 nd->nl_flags |= NLC_LOCKVP;
1307 error = vn_open(nd, fp, flags, cmode);
1311 * handle special fdopen() case. bleh. dupfdopen() is
1312 * responsible for dropping the old contents of ofiles[indx]
1315 * Note that fsetfd() will add a ref to fp which represents
1316 * the fd_files[] assignment. We must still drop our
1319 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1320 if (fdalloc(p, 0, &indx) == 0) {
1321 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
1324 fdrop(fp); /* our ref */
1327 fsetfd(p, NULL, indx);
1330 fdrop(fp); /* our ref */
1331 if (error == ERESTART)
1337 * ref the vnode for ourselves so it can't be ripped out from under
1338 * is. XXX need an ND flag to request that the vnode be returned
1341 * Reserve a file descriptor but do not assign it until the open
1344 vp = (struct vnode *)fp->f_data;
1346 if ((error = fdalloc(p, 0, &indx)) != 0) {
1353 * If no error occurs the vp will have been assigned to the file
1358 if (flags & (O_EXLOCK | O_SHLOCK)) {
1359 lf.l_whence = SEEK_SET;
1362 if (flags & O_EXLOCK)
1363 lf.l_type = F_WRLCK;
1365 lf.l_type = F_RDLCK;
1366 if (flags & FNONBLOCK)
1371 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1373 * lock request failed. Clean up the reserved
1377 fsetfd(p, NULL, indx);
1381 fp->f_flag |= FHASLOCK;
1385 * Assert that all regular file vnodes were created with a object.
1387 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1388 ("open: regular file has no backing object after vn_open"));
1394 * release our private reference, leaving the one associated with the
1395 * descriptor table intact.
1397 fsetfd(p, fp, indx);
1404 * open_args(char *path, int flags, int mode)
1406 * Check permissions, allocate an open file structure,
1407 * and call the device open routine if any.
1410 open(struct open_args *uap)
1412 struct nlookupdata nd;
1415 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1417 error = kern_open(&nd, uap->flags,
1418 uap->mode, &uap->sysmsg_result);
1425 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1427 struct namecache *ncp;
1428 struct thread *td = curthread;
1429 struct proc *p = td->td_proc;
1437 switch (mode & S_IFMT) {
1443 error = suser_cred(p->p_ucred, PRISON_ROOT);
1450 nd->nl_flags |= NLC_CREATE;
1451 if ((error = nlookup(nd)) != 0)
1458 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1459 vattr.va_rdev = dev;
1462 switch (mode & S_IFMT) {
1463 case S_IFMT: /* used by badsect to flag bad sectors */
1464 vattr.va_type = VBAD;
1467 vattr.va_type = VCHR;
1470 vattr.va_type = VBLK;
1481 error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1484 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1493 * mknod_args(char *path, int mode, int dev)
1495 * Create a special file.
1498 mknod(struct mknod_args *uap)
1500 struct nlookupdata nd;
1503 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1505 error = kern_mknod(&nd, uap->mode, uap->dev);
1511 kern_mkfifo(struct nlookupdata *nd, int mode)
1513 struct namecache *ncp;
1514 struct thread *td = curthread;
1515 struct proc *p = td->td_proc;
1522 nd->nl_flags |= NLC_CREATE;
1523 if ((error = nlookup(nd)) != 0)
1530 vattr.va_type = VFIFO;
1531 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1533 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1540 * mkfifo_args(char *path, int mode)
1542 * Create a named pipe.
1545 mkfifo(struct mkfifo_args *uap)
1547 struct nlookupdata nd;
1550 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1552 error = kern_mkfifo(&nd, uap->mode);
1557 static int hardlink_check_uid = 0;
1558 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1559 &hardlink_check_uid, 0,
1560 "Unprivileged processes cannot create hard links to files owned by other "
1562 static int hardlink_check_gid = 0;
1563 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1564 &hardlink_check_gid, 0,
1565 "Unprivileged processes cannot create hard links to files owned by other "
1569 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1575 * Shortcut if disabled
1577 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1581 * root cred can always hardlink
1583 if (suser_cred(cred, PRISON_ROOT) == 0)
1587 * Otherwise only if the originating file is owned by the
1588 * same user or group. Note that any group is allowed if
1589 * the file is owned by the caller.
1591 error = VOP_GETATTR(vp, &va);
1595 if (hardlink_check_uid) {
1596 if (cred->cr_uid != va.va_uid)
1600 if (hardlink_check_gid) {
1601 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1609 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1611 struct thread *td = curthread;
1616 * Lookup the source and obtained a locked vnode.
1618 * XXX relookup on vget failure / race ?
1621 if ((error = nlookup(nd)) != 0)
1623 vp = nd->nl_ncp->nc_vp;
1624 KKASSERT(vp != NULL);
1625 if (vp->v_type == VDIR)
1626 return (EPERM); /* POSIX */
1627 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
1631 * Unlock the source so we can lookup the target without deadlocking
1632 * (XXX vp is locked already, possible other deadlock?). The target
1635 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1636 nd->nl_flags &= ~NLC_NCPISLOCKED;
1637 cache_unlock(nd->nl_ncp);
1639 linknd->nl_flags |= NLC_CREATE;
1640 if ((error = nlookup(linknd)) != 0) {
1644 if (linknd->nl_ncp->nc_vp) {
1650 * Finally run the new API VOP.
1652 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1654 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
1660 * link_args(char *path, char *link)
1662 * Make a hard file link.
1665 link(struct link_args *uap)
1667 struct nlookupdata nd, linknd;
1670 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1672 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1674 error = kern_link(&nd, &linknd);
1675 nlookup_done(&linknd);
1682 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1684 struct namecache *ncp;
1690 nd->nl_flags |= NLC_CREATE;
1691 if ((error = nlookup(nd)) != 0)
1698 vattr.va_mode = mode;
1699 error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
1706 * symlink(char *path, char *link)
1708 * Make a symbolic link.
1711 symlink(struct symlink_args *uap)
1713 struct thread *td = curthread;
1714 struct nlookupdata nd;
1719 path = zalloc(namei_zone);
1720 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1722 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1724 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1725 error = kern_symlink(&nd, path, mode);
1729 zfree(namei_zone, path);
1734 * undelete_args(char *path)
1736 * Delete a whiteout from the filesystem.
1740 undelete(struct undelete_args *uap)
1742 struct nlookupdata nd;
1745 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1747 nd.nl_flags |= NLC_DELETE;
1749 error = nlookup(&nd);
1751 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1757 kern_unlink(struct nlookupdata *nd)
1759 struct namecache *ncp;
1763 nd->nl_flags |= NLC_DELETE;
1764 if ((error = nlookup(nd)) != 0)
1767 error = VOP_NREMOVE(ncp, nd->nl_cred);
1772 * unlink_args(char *path)
1774 * Delete a name from the filesystem.
1777 unlink(struct unlink_args *uap)
1779 struct nlookupdata nd;
1782 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1784 error = kern_unlink(&nd);
1790 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1792 struct thread *td = curthread;
1793 struct proc *p = td->td_proc;
1798 fp = holdfp(p->p_fd, fd, -1);
1801 if (fp->f_type != DTYPE_VNODE) {
1808 fp->f_offset += offset;
1812 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
1814 fp->f_offset = offset + vattr.va_size;
1817 fp->f_offset = offset;
1824 *res = fp->f_offset;
1831 * lseek_args(int fd, int pad, off_t offset, int whence)
1833 * Reposition read/write file offset.
1836 lseek(struct lseek_args *uap)
1840 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1841 &uap->sysmsg_offset);
1847 kern_access(struct nlookupdata *nd, int aflags)
1852 if ((error = nlookup(nd)) != 0)
1855 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1859 /* Flags == 0 means only check for existence. */
1868 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1869 error = VOP_ACCESS(vp, flags, nd->nl_cred);
1872 * If the file handle is stale we have to re-resolve the
1873 * entry. This is a hack at the moment.
1875 if (error == ESTALE) {
1876 cache_setunresolved(nd->nl_ncp);
1877 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1890 * access_args(char *path, int flags)
1892 * Check access permissions.
1895 access(struct access_args *uap)
1897 struct nlookupdata nd;
1900 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1902 error = kern_access(&nd, uap->flags);
1908 kern_stat(struct nlookupdata *nd, struct stat *st)
1914 if ((error = nlookup(nd)) != 0)
1917 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1921 if ((error = vget(vp, LK_SHARED)) != 0)
1923 error = vn_stat(vp, st, nd->nl_cred);
1926 * If the file handle is stale we have to re-resolve the entry. This
1927 * is a hack at the moment.
1929 if (error == ESTALE) {
1930 cache_setunresolved(nd->nl_ncp);
1931 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1942 * stat_args(char *path, struct stat *ub)
1944 * Get file status; this version follows links.
1947 stat(struct stat_args *uap)
1949 struct nlookupdata nd;
1953 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1955 error = kern_stat(&nd, &st);
1957 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1964 * lstat_args(char *path, struct stat *ub)
1966 * Get file status; this version does not follow links.
1969 lstat(struct lstat_args *uap)
1971 struct nlookupdata nd;
1975 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1977 error = kern_stat(&nd, &st);
1979 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1986 * pathconf_Args(char *path, int name)
1988 * Get configurable pathname variables.
1992 pathconf(struct pathconf_args *uap)
1994 struct nlookupdata nd;
1999 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2001 error = nlookup(&nd);
2003 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2006 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2014 * kern_readlink isn't properly split yet. There is a copyin burried
2015 * in VOP_READLINK().
2018 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2020 struct thread *td = curthread;
2021 struct proc *p = td->td_proc;
2027 if ((error = nlookup(nd)) != 0)
2029 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
2032 if (vp->v_type != VLNK) {
2035 aiov.iov_base = buf;
2036 aiov.iov_len = count;
2037 auio.uio_iov = &aiov;
2038 auio.uio_iovcnt = 1;
2039 auio.uio_offset = 0;
2040 auio.uio_rw = UIO_READ;
2041 auio.uio_segflg = UIO_USERSPACE;
2043 auio.uio_resid = count;
2044 error = VOP_READLINK(vp, &auio, p->p_ucred);
2047 *res = count - auio.uio_resid;
2052 * readlink_args(char *path, char *buf, int count)
2054 * Return target name of a symbolic link.
2057 readlink(struct readlink_args *uap)
2059 struct nlookupdata nd;
2062 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2064 error = kern_readlink(&nd, uap->buf, uap->count,
2065 &uap->sysmsg_result);
2072 setfflags(struct vnode *vp, int flags)
2074 struct thread *td = curthread;
2075 struct proc *p = td->td_proc;
2080 * Prevent non-root users from setting flags on devices. When
2081 * a device is reused, users can retain ownership of the device
2082 * if they are allowed to set flags and programs assume that
2083 * chown can't fail when done as root.
2085 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2086 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2090 * note: vget is required for any operation that might mod the vnode
2091 * so VINACTIVE is properly cleared.
2093 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2095 vattr.va_flags = flags;
2096 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2103 * chflags(char *path, int flags)
2105 * Change flags of a file given a path name.
2109 chflags(struct chflags_args *uap)
2111 struct nlookupdata nd;
2116 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2117 /* XXX Add NLC flag indicating modifying operation? */
2119 error = nlookup(&nd);
2121 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2124 error = setfflags(vp, uap->flags);
2131 * fchflags_args(int fd, int flags)
2133 * Change flags of a file given a file descriptor.
2137 fchflags(struct fchflags_args *uap)
2139 struct thread *td = curthread;
2140 struct proc *p = td->td_proc;
2144 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2146 error = setfflags((struct vnode *) fp->f_data, uap->flags);
2152 setfmode(struct vnode *vp, int mode)
2154 struct thread *td = curthread;
2155 struct proc *p = td->td_proc;
2160 * note: vget is required for any operation that might mod the vnode
2161 * so VINACTIVE is properly cleared.
2163 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2165 vattr.va_mode = mode & ALLPERMS;
2166 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2173 kern_chmod(struct nlookupdata *nd, int mode)
2178 /* XXX Add NLC flag indicating modifying operation? */
2179 if ((error = nlookup(nd)) != 0)
2181 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2183 error = setfmode(vp, mode);
2189 * chmod_args(char *path, int mode)
2191 * Change mode of a file given path name.
2195 chmod(struct chmod_args *uap)
2197 struct nlookupdata nd;
2200 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2202 error = kern_chmod(&nd, uap->mode);
2208 * lchmod_args(char *path, int mode)
2210 * Change mode of a file given path name (don't follow links.)
2214 lchmod(struct lchmod_args *uap)
2216 struct nlookupdata nd;
2219 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2221 error = kern_chmod(&nd, uap->mode);
2227 * fchmod_args(int fd, int mode)
2229 * Change mode of a file given a file descriptor.
2233 fchmod(struct fchmod_args *uap)
2235 struct thread *td = curthread;
2236 struct proc *p = td->td_proc;
2240 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2242 error = setfmode((struct vnode *)fp->f_data, uap->mode);
2248 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2250 struct thread *td = curthread;
2251 struct proc *p = td->td_proc;
2256 * note: vget is required for any operation that might mod the vnode
2257 * so VINACTIVE is properly cleared.
2259 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2263 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2270 kern_chown(struct nlookupdata *nd, int uid, int gid)
2275 /* XXX Add NLC flag indicating modifying operation? */
2276 if ((error = nlookup(nd)) != 0)
2278 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2280 error = setfown(vp, uid, gid);
2286 * chown(char *path, int uid, int gid)
2288 * Set ownership given a path name.
2291 chown(struct chown_args *uap)
2293 struct nlookupdata nd;
2296 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2298 error = kern_chown(&nd, uap->uid, uap->gid);
2304 * lchown_args(char *path, int uid, int gid)
2306 * Set ownership given a path name, do not cross symlinks.
2309 lchown(struct lchown_args *uap)
2311 struct nlookupdata nd;
2314 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2316 error = kern_chown(&nd, uap->uid, uap->gid);
2322 * fchown_args(int fd, int uid, int gid)
2324 * Set ownership given a file descriptor.
2328 fchown(struct fchown_args *uap)
2330 struct thread *td = curthread;
2331 struct proc *p = td->td_proc;
2335 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
2337 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
2343 getutimes(const struct timeval *tvp, struct timespec *tsp)
2345 struct timeval tv[2];
2349 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2352 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2353 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2359 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2361 struct thread *td = curthread;
2362 struct proc *p = td->td_proc;
2367 * note: vget is required for any operation that might mod the vnode
2368 * so VINACTIVE is properly cleared.
2370 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
2372 vattr.va_atime = ts[0];
2373 vattr.va_mtime = ts[1];
2375 vattr.va_vaflags |= VA_UTIMES_NULL;
2376 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
2383 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2385 struct timespec ts[2];
2389 if ((error = getutimes(tptr, ts)) != 0)
2391 /* XXX Add NLC flag indicating modifying operation? */
2392 if ((error = nlookup(nd)) != 0)
2394 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2396 error = setutimes(vp, ts, tptr == NULL);
2402 * utimes_args(char *path, struct timeval *tptr)
2404 * Set the access and modification times of a file.
2407 utimes(struct utimes_args *uap)
2409 struct timeval tv[2];
2410 struct nlookupdata nd;
2414 error = copyin(uap->tptr, tv, sizeof(tv));
2418 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2420 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2426 * lutimes_args(char *path, struct timeval *tptr)
2428 * Set the access and modification times of a file.
2431 lutimes(struct lutimes_args *uap)
2433 struct timeval tv[2];
2434 struct nlookupdata nd;
2438 error = copyin(uap->tptr, tv, sizeof(tv));
2442 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2444 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2450 kern_futimes(int fd, struct timeval *tptr)
2452 struct thread *td = curthread;
2453 struct proc *p = td->td_proc;
2454 struct timespec ts[2];
2458 error = getutimes(tptr, ts);
2461 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2463 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2469 * futimes_args(int fd, struct timeval *tptr)
2471 * Set the access and modification times of a file.
2474 futimes(struct futimes_args *uap)
2476 struct timeval tv[2];
2480 error = copyin(uap->tptr, tv, sizeof(tv));
2485 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2491 kern_truncate(struct nlookupdata *nd, off_t length)
2499 /* XXX Add NLC flag indicating modifying operation? */
2500 if ((error = nlookup(nd)) != 0)
2502 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2504 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2508 if (vp->v_type == VDIR) {
2510 } else if ((error = vn_writechk(vp)) == 0 &&
2511 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
2513 vattr.va_size = length;
2514 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
2521 * truncate(char *path, int pad, off_t length)
2523 * Truncate a file given its path name.
2526 truncate(struct truncate_args *uap)
2528 struct nlookupdata nd;
2531 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2533 error = kern_truncate(&nd, uap->length);
2539 kern_ftruncate(int fd, off_t length)
2541 struct thread *td = curthread;
2542 struct proc *p = td->td_proc;
2550 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2552 if ((fp->f_flag & FWRITE) == 0) {
2556 vp = (struct vnode *)fp->f_data;
2557 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2558 if (vp->v_type == VDIR) {
2560 } else if ((error = vn_writechk(vp)) == 0) {
2562 vattr.va_size = length;
2563 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 = holdvnode(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);
2619 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2621 struct namecache *fncpd;
2622 struct namecache *tncpd;
2623 struct namecache *ncp;
2628 if ((error = nlookup(fromnd)) != 0)
2630 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2635 * unlock the source ncp so we can lookup the target ncp without
2636 * deadlocking. The target may or may not exist so we do not check
2637 * for a target vp like kern_mkdir() and other creation functions do.
2639 * The source and target directories are ref'd and rechecked after
2640 * everything is relocked to determine if the source or target file
2643 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2644 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2645 cache_unlock(fromnd->nl_ncp);
2647 tond->nl_flags |= NLC_CREATE;
2648 if ((error = nlookup(tond)) != 0) {
2652 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2659 * If the source and target are the same there is nothing to do
2661 if (fromnd->nl_ncp == tond->nl_ncp) {
2668 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2669 * may have become invalid while it was unlocked, nc_vp and nc_mount
2672 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2673 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2674 } else if (fromnd->nl_ncp > tond->nl_ncp) {
2675 cache_lock(fromnd->nl_ncp);
2676 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2678 cache_unlock(tond->nl_ncp);
2679 cache_lock(fromnd->nl_ncp);
2680 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2681 cache_lock(tond->nl_ncp);
2682 cache_resolve(tond->nl_ncp, tond->nl_cred);
2684 fromnd->nl_flags |= NLC_NCPISLOCKED;
2687 * make sure the parent directories linkages are the same
2689 if (fncpd != fromnd->nl_ncp->nc_parent ||
2690 tncpd != tond->nl_ncp->nc_parent) {
2697 * Both the source and target must be within the same filesystem and
2698 * in the same filesystem as their parent directories within the
2699 * namecache topology.
2701 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2703 mp = fncpd->nc_mount;
2704 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2705 mp != tond->nl_ncp->nc_mount) {
2712 * If the target exists and either the source or target is a directory,
2713 * then both must be directories.
2715 * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2718 if (tond->nl_ncp->nc_vp) {
2719 if (fromnd->nl_ncp->nc_vp == NULL) {
2721 } else if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2722 if (tond->nl_ncp->nc_vp->v_type != VDIR)
2724 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2730 * You cannot rename a source into itself or a subdirectory of itself.
2731 * We check this by travsersing the target directory upwards looking
2732 * for a match against the source.
2735 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2736 if (fromnd->nl_ncp == ncp) {
2747 * Even though the namespaces are different, they may still represent
2748 * hardlinks to the same file. The filesystem might have a hard time
2749 * with this so we issue a NREMOVE of the source instead of a NRENAME
2750 * when we detect the situation.
2753 if (fromnd->nl_ncp->nc_vp == tond->nl_ncp->nc_vp) {
2754 error = VOP_NREMOVE(fromnd->nl_ncp, fromnd->nl_cred);
2756 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp,
2764 * rename_args(char *from, char *to)
2766 * Rename files. Source and destination must either both be directories,
2767 * or both not be directories. If target is a directory, it must be empty.
2770 rename(struct rename_args *uap)
2772 struct nlookupdata fromnd, tond;
2775 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2777 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2779 error = kern_rename(&fromnd, &tond);
2780 nlookup_done(&tond);
2782 nlookup_done(&fromnd);
2787 kern_mkdir(struct nlookupdata *nd, int mode)
2789 struct thread *td = curthread;
2790 struct proc *p = td->td_proc;
2791 struct namecache *ncp;
2797 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2798 if ((error = nlookup(nd)) != 0)
2806 vattr.va_type = VDIR;
2807 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2810 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2817 * mkdir_args(char *path, int mode)
2819 * Make a directory file.
2823 mkdir(struct mkdir_args *uap)
2825 struct nlookupdata nd;
2828 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2830 error = kern_mkdir(&nd, uap->mode);
2836 kern_rmdir(struct nlookupdata *nd)
2838 struct namecache *ncp;
2842 nd->nl_flags |= NLC_DELETE;
2843 if ((error = nlookup(nd)) != 0)
2847 error = VOP_NRMDIR(ncp, nd->nl_cred);
2852 * rmdir_args(char *path)
2854 * Remove a directory file.
2858 rmdir(struct rmdir_args *uap)
2860 struct nlookupdata nd;
2863 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2865 error = kern_rmdir(&nd);
2871 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
2872 enum uio_seg direction)
2874 struct thread *td = curthread;
2875 struct proc *p = td->td_proc;
2883 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
2885 if ((fp->f_flag & FREAD) == 0) {
2889 vp = (struct vnode *)fp->f_data;
2891 if (vp->v_type != VDIR) {
2895 aiov.iov_base = buf;
2896 aiov.iov_len = count;
2897 auio.uio_iov = &aiov;
2898 auio.uio_iovcnt = 1;
2899 auio.uio_rw = UIO_READ;
2900 auio.uio_segflg = direction;
2902 auio.uio_resid = count;
2903 /* vn_lock(vp, LK_SHARED | LK_RETRY); */
2904 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2905 loff = auio.uio_offset = fp->f_offset;
2906 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2907 fp->f_offset = auio.uio_offset;
2911 if (count == auio.uio_resid) {
2912 if (union_dircheckp) {
2913 error = union_dircheckp(td, &vp, fp);
2919 if ((vp->v_flag & VROOT) &&
2920 (vp->v_mount->mnt_flag & MNT_UNION)) {
2921 struct vnode *tvp = vp;
2922 vp = vp->v_mount->mnt_vnodecovered;
2933 *res = count - auio.uio_resid;
2940 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2942 * Read a block of directory entries in a file system independent format.
2945 getdirentries(struct getdirentries_args *uap)
2950 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2951 &uap->sysmsg_result, UIO_USERSPACE);
2954 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2959 * getdents_args(int fd, char *buf, size_t count)
2962 getdents(struct getdents_args *uap)
2966 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2967 &uap->sysmsg_result, UIO_USERSPACE);
2973 * umask(int newmask)
2975 * Set the mode mask for creation of filesystem nodes.
2980 umask(struct umask_args *uap)
2982 struct thread *td = curthread;
2983 struct proc *p = td->td_proc;
2984 struct filedesc *fdp;
2987 uap->sysmsg_result = fdp->fd_cmask;
2988 fdp->fd_cmask = uap->newmask & ALLPERMS;
2993 * revoke(char *path)
2995 * Void all references to file by ripping underlying filesystem
3000 revoke(struct revoke_args *uap)
3002 struct nlookupdata nd;
3009 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3011 error = nlookup(&nd);
3013 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
3014 cred = crhold(nd.nl_cred);
3017 if (vp->v_type != VCHR && vp->v_type != VBLK)
3020 error = VOP_GETATTR(vp, &vattr);
3021 if (error == 0 && cred->cr_uid != vattr.va_uid)
3022 error = suser_cred(cred, PRISON_ROOT);
3023 if (error == 0 && count_udev(vp->v_udev) > 0) {
3024 if ((error = vx_lock(vp)) == 0) {
3025 VOP_REVOKE(vp, REVOKEALL);
3037 * getfh_args(char *fname, fhandle_t *fhp)
3039 * Get (NFS) file handle
3042 getfh(struct getfh_args *uap)
3044 struct thread *td = curthread;
3045 struct nlookupdata nd;
3051 * Must be super user
3053 if ((error = suser(td)) != 0)
3057 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3059 error = nlookup(&nd);
3061 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3064 bzero(&fh, sizeof(fh));
3065 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3066 error = VFS_VPTOFH(vp, &fh.fh_fid);
3069 error = copyout(&fh, uap->fhp, sizeof(fh));
3075 * fhopen_args(const struct fhandle *u_fhp, int flags)
3077 * syscall for the rpc.lockd to use to translate a NFS file handle into
3078 * an open descriptor.
3080 * warning: do not remove the suser() call or this becomes one giant
3084 fhopen(struct fhopen_args *uap)
3086 struct thread *td = curthread;
3087 struct proc *p = td->td_proc;
3092 struct vattr *vap = &vat;
3094 int fmode, mode, error, type;
3100 * Must be super user
3106 fmode = FFLAGS(uap->flags);
3107 /* why not allow a non-read/write open for our lockd? */
3108 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3110 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3113 /* find the mount point */
3114 mp = vfs_getvfs(&fhp.fh_fsid);
3117 /* now give me my vnode, it gets returned to me locked */
3118 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3122 * from now on we have to make sure not
3123 * to forget about the vnode
3124 * any error that causes an abort must vput(vp)
3125 * just set error = err and 'goto bad;'.
3131 if (vp->v_type == VLNK) {
3135 if (vp->v_type == VSOCK) {
3140 if (fmode & (FWRITE | O_TRUNC)) {
3141 if (vp->v_type == VDIR) {
3145 error = vn_writechk(vp);
3153 error = VOP_ACCESS(vp, mode, p->p_ucred);
3157 if (fmode & O_TRUNC) {
3158 VOP_UNLOCK(vp, 0); /* XXX */
3159 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3162 error = VOP_SETATTR(vp, vap, p->p_ucred);
3168 * VOP_OPEN needs the file pointer so it can potentially override
3171 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3174 if ((error = falloc(p, &nfp, &indx)) != 0)
3178 error = VOP_OPEN(vp, fmode, p->p_ucred, fp);
3181 * setting f_ops this way prevents VOP_CLOSE from being
3182 * called or fdrop() releasing the vp from v_data. Since
3183 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3185 fp->f_ops = &badfileops;
3191 * The fp is given its own reference, we still have our ref and lock.
3193 * Assert that all regular files must be created with a VM object.
3195 if (vp->v_type == VREG && vp->v_object == NULL) {
3196 printf("fhopen: regular file did not have VM object: %p\n", vp);
3201 * The open was successful. Handle any locking requirements.
3203 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3204 lf.l_whence = SEEK_SET;
3207 if (fmode & O_EXLOCK)
3208 lf.l_type = F_WRLCK;
3210 lf.l_type = F_RDLCK;
3211 if (fmode & FNONBLOCK)
3216 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3218 * release our private reference.
3220 fsetfd(p, NULL, indx);
3225 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3226 fp->f_flag |= FHASLOCK;
3230 * Clean up. Associate the file pointer with the previously
3231 * reserved descriptor and return it.
3234 fsetfd(p, fp, indx);
3236 uap->sysmsg_result = indx;
3240 fsetfd(p, NULL, indx);
3248 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3251 fhstat(struct fhstat_args *uap)
3253 struct thread *td = curthread;
3261 * Must be super user
3267 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3271 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3273 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3275 error = vn_stat(vp, &sb, td->td_proc->p_ucred);
3279 error = copyout(&sb, uap->sb, sizeof(sb));
3284 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3287 fhstatfs(struct fhstatfs_args *uap)
3289 struct thread *td = curthread;
3290 struct proc *p = td->td_proc;
3295 char *fullpath, *freepath;
3300 * Must be super user
3302 if ((error = suser(td)))
3305 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3308 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3311 if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0 &&
3312 !chroot_visible_mnt(mp, p))
3315 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3320 if ((error = VFS_STATFS(mp, sp, p->p_ucred)) != 0)
3323 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
3326 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3327 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3328 free(freepath, M_TEMP);
3330 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3332 bcopy(sp, &sb, sizeof(sb));
3333 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3336 return (copyout(sp, uap->buf, sizeof(*sp)));
3340 * Syscall to push extended attribute configuration information into the
3341 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3342 * a command (int cmd), and attribute name and misc data. For now, the
3343 * attribute name is left in userspace for consumption by the VFS_op.
3344 * It will probably be changed to be copied into sysspace by the
3345 * syscall in the future, once issues with various consumers of the
3346 * attribute code have raised their hands.
3348 * Currently this is used only by UFS Extended Attributes.
3351 extattrctl(struct extattrctl_args *uap)
3353 struct nlookupdata nd;
3359 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3361 error = nlookup(&nd);
3363 mp = nd.nl_ncp->nc_mount;
3364 error = VFS_EXTATTRCTL(mp, uap->cmd,
3365 uap->attrname, uap->arg,
3373 * Syscall to set a named extended attribute on a file or directory.
3374 * Accepts attribute name, and a uio structure pointing to the data to set.
3375 * The uio is consumed in the style of writev(). The real work happens
3376 * in VOP_SETEXTATTR().
3379 extattr_set_file(struct extattr_set_file_args *uap)
3381 char attrname[EXTATTR_MAXNAMELEN];
3382 struct iovec aiov[UIO_SMALLIOV];
3383 struct iovec *needfree;
3384 struct nlookupdata nd;
3393 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3398 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3400 error = nlookup(&nd);
3402 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3409 iovlen = uap->iovcnt * sizeof(struct iovec);
3410 if (uap->iovcnt > UIO_SMALLIOV) {
3411 if (uap->iovcnt > UIO_MAXIOV) {
3415 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3421 auio.uio_iovcnt = uap->iovcnt;
3422 auio.uio_rw = UIO_WRITE;
3423 auio.uio_segflg = UIO_USERSPACE;
3424 auio.uio_td = nd.nl_td;
3425 auio.uio_offset = 0;
3426 if ((error = copyin(uap->iovp, iov, iovlen)))
3429 for (i = 0; i < uap->iovcnt; i++) {
3430 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3434 auio.uio_resid += iov->iov_len;
3437 cnt = auio.uio_resid;
3438 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3439 cnt -= auio.uio_resid;
3440 uap->sysmsg_result = cnt;
3445 FREE(needfree, M_IOV);
3450 * Syscall to get a named extended attribute on a file or directory.
3451 * Accepts attribute name, and a uio structure pointing to a buffer for the
3452 * data. The uio is consumed in the style of readv(). The real work
3453 * happens in VOP_GETEXTATTR();
3456 extattr_get_file(struct extattr_get_file_args *uap)
3458 char attrname[EXTATTR_MAXNAMELEN];
3459 struct iovec aiov[UIO_SMALLIOV];
3460 struct iovec *needfree;
3461 struct nlookupdata nd;
3470 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3475 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3477 error = nlookup(&nd);
3479 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3485 iovlen = uap->iovcnt * sizeof (struct iovec);
3487 if (uap->iovcnt > UIO_SMALLIOV) {
3488 if (uap->iovcnt > UIO_MAXIOV) {
3492 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3498 auio.uio_iovcnt = uap->iovcnt;
3499 auio.uio_rw = UIO_READ;
3500 auio.uio_segflg = UIO_USERSPACE;
3501 auio.uio_td = nd.nl_td;
3502 auio.uio_offset = 0;
3503 if ((error = copyin(uap->iovp, iov, iovlen)))
3506 for (i = 0; i < uap->iovcnt; i++) {
3507 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3511 auio.uio_resid += iov->iov_len;
3514 cnt = auio.uio_resid;
3515 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred);
3516 cnt -= auio.uio_resid;
3517 uap->sysmsg_result = cnt;
3522 FREE(needfree, M_IOV);
3527 * Syscall to delete a named extended attribute from a file or directory.
3528 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3531 extattr_delete_file(struct extattr_delete_file_args *uap)
3533 char attrname[EXTATTR_MAXNAMELEN];
3534 struct nlookupdata nd;
3538 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3543 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3545 error = nlookup(&nd);
3547 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3553 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred);
3560 chroot_visible_mnt(struct mount *mp, struct proc *p)
3562 struct namecache *ncp;
3564 * First check if this file system is below
3568 while (ncp != NULL && ncp != p->p_fd->fd_nrdir)
3569 ncp = ncp->nc_parent;
3572 * This is not below the chroot path.
3574 * Check if the chroot path is on the same filesystem,
3575 * by determing if we have to cross a mount point
3576 * before reaching mp->mnt_ncp.
3578 ncp = p->p_fd->fd_nrdir;
3579 while (ncp != NULL && ncp != mp->mnt_ncp) {
3580 if (ncp->nc_flag & NCF_MOUNTPT) {
3584 ncp = ncp->nc_parent;
3587 return(ncp != NULL);