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.84 2006/05/05 21:15:09 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;
119 if (p->p_ucred->cr_prison != NULL)
121 if (usermount == 0 && (error = suser(td)))
124 * Do not allow NFS export by non-root users.
126 if (uap->flags & MNT_EXPORTED) {
132 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
135 uap->flags |= MNT_NOSUID | MNT_NODEV;
138 * Lookup the requested path and extract the ncp and vnode.
140 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
142 if ((error = nlookup(&nd)) == 0) {
143 if (nd.nl_ncp->nc_vp == NULL)
153 * Extract the locked+refd ncp and cleanup the nd structure
160 * now we have the locked ref'd ncp and unreferenced vnode.
163 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
170 * Now we have an unlocked ref'd ncp and a locked ref'd vp
172 if (uap->flags & MNT_UPDATE) {
173 if ((vp->v_flag & VROOT) == 0) {
180 flag2 = mp->mnt_kern_flag;
182 * We only allow the filesystem to be reloaded if it
183 * is currently mounted read-only.
185 if ((uap->flags & MNT_RELOAD) &&
186 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
189 return (EOPNOTSUPP); /* Needs translation */
192 * Only root, or the user that did the original mount is
193 * permitted to update it.
195 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
196 (error = suser(td))) {
201 if (vfs_busy(mp, LK_NOWAIT, td)) {
206 if ((vp->v_flag & VMOUNT) != 0 ||
207 vp->v_mountedhere != NULL) {
213 vp->v_flag |= VMOUNT;
215 uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
220 * If the user is not root, ensure that they own the directory
221 * onto which we are attempting to mount.
223 if ((error = VOP_GETATTR(vp, &va, td)) ||
224 (va.va_uid != p->p_ucred->cr_uid &&
225 (error = suser(td)))) {
230 if ((error = vinvalbuf(vp, V_SAVE, td, 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, td);
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 = p->p_ucred->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, td);
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, td)) != 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, td));
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, struct thread *td)
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, td)) == 0) ||
557 error = VFS_UNMOUNT(mp, flags, td);
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, curthread);
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,
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;
699 struct filedesc *fdp = p->p_fd;
707 * Sanity and permissions checks. We must be root.
710 if (p->p_ucred->cr_prison != NULL)
712 if ((error = suser(td)) != 0)
716 * Argument length checks
718 if (uap->ctllen < 0 || uap->ctllen > 1024)
720 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
722 if (uap->path == NULL)
726 * Allocate the necessary buffers and copyin data
728 path = zalloc(namei_zone);
729 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
734 ctl = malloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
735 error = copyin(uap->ctl, ctl, uap->ctllen);
740 buf = malloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
743 * Validate the descriptor
747 } else if ((u_int)uap->fd >= fdp->fd_nfiles ||
748 (fp = fdp->fd_files[uap->fd].fp) == NULL) {
756 * Execute the internal kernel function and clean up.
758 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
761 if (error == 0 && uap->sysmsg_result > 0)
762 error = copyout(buf, uap->buf, uap->sysmsg_result);
765 zfree(namei_zone, path);
774 * Execute a mount control operation by resolving the path to a mount point
775 * and calling vop_mountctl().
778 kern_mountctl(const char *path, int op, struct file *fp,
779 const void *ctl, int ctllen,
780 void *buf, int buflen, int *res)
784 struct nlookupdata nd;
789 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
791 error = nlookup(&nd);
793 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
801 * Must be the root of the filesystem
803 if ((vp->v_flag & VROOT) == 0) {
807 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
814 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
816 struct thread *td = curthread;
817 struct proc *p = td->td_proc;
820 char *fullpath, *freepath;
823 if ((error = nlookup(nd)) != 0)
825 mp = nd->nl_ncp->nc_mount;
827 if ((error = VFS_STATFS(mp, sp, td)) != 0)
830 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
833 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
834 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
835 free(freepath, M_TEMP);
837 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
838 bcopy(sp, buf, sizeof(*buf));
839 /* Only root should have access to the fsid's. */
841 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
846 * statfs_args(char *path, struct statfs *buf)
848 * Get filesystem statistics.
851 statfs(struct statfs_args *uap)
853 struct nlookupdata nd;
857 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
859 error = kern_statfs(&nd, &buf);
862 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
867 kern_fstatfs(int fd, struct statfs *buf)
869 struct thread *td = curthread;
870 struct proc *p = td->td_proc;
874 char *fullpath, *freepath;
878 error = getvnode(p->p_fd, fd, &fp);
881 mp = ((struct vnode *)fp->f_data)->v_mount;
885 error = VFS_STATFS(mp, sp, td);
889 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
892 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
893 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
894 free(freepath, M_TEMP);
896 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
897 bcopy(sp, buf, sizeof(*buf));
899 /* Only root should have access to the fsid's. */
901 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
906 * fstatfs_args(int fd, struct statfs *buf)
908 * Get filesystem statistics.
911 fstatfs(struct fstatfs_args *uap)
916 error = kern_fstatfs(uap->fd, &buf);
919 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
924 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
926 * Get statistics on all filesystems.
929 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;
963 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
964 if (info.sfsp && info.count > info.maxcount)
965 uap->sysmsg_result = info.maxcount;
967 uap->sysmsg_result = info.count;
972 getfsstat_callback(struct mount *mp, void *data)
974 struct getfsstat_info *info = data;
980 if (info->sfsp && info->count < info->maxcount) {
981 if (info->is_chrooted && !chroot_visible_mnt(mp, info->p))
986 * If MNT_NOWAIT or MNT_LAZY is specified, do not
987 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
988 * overrides MNT_WAIT.
990 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
991 (info->flags & MNT_WAIT)) &&
992 (error = VFS_STATFS(mp, sp, info->td))) {
995 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
997 error = cache_fullpath(info->p, mp->mnt_ncp,
998 &fullpath, &freepath);
1000 info->error = error;
1003 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1004 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1005 free(freepath, M_TEMP);
1007 error = copyout(sp, info->sfsp, sizeof(*sp));
1009 info->error = error;
1019 * fchdir_args(int fd)
1021 * Change current working directory to a given file descriptor.
1025 fchdir(struct fchdir_args *uap)
1027 struct thread *td = curthread;
1028 struct proc *p = td->td_proc;
1029 struct filedesc *fdp = p->p_fd;
1030 struct vnode *vp, *ovp;
1033 struct namecache *ncp, *oncp;
1034 struct namecache *nct;
1037 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
1039 vp = (struct vnode *)fp->f_data;
1041 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1042 if (vp->v_type != VDIR || fp->f_ncp == NULL)
1045 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
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, td);
1058 KKASSERT(error == 0);
1065 oncp = fdp->fd_ncdir;
1066 VOP_UNLOCK(vp, 0); /* leave ref intact */
1068 fdp->fd_ncdir = ncp;
1079 kern_chdir(struct nlookupdata *nd)
1081 struct thread *td = curthread;
1082 struct proc *p = td->td_proc;
1083 struct filedesc *fdp = p->p_fd;
1084 struct vnode *vp, *ovp;
1085 struct namecache *oncp;
1088 if ((error = nlookup(nd)) != 0)
1090 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1092 if ((error = vget(vp, LK_SHARED, td)) != 0)
1095 error = checkvp_chdir(vp, td);
1099 oncp = fdp->fd_ncdir;
1100 cache_unlock(nd->nl_ncp); /* leave reference intact */
1101 fdp->fd_ncdir = nd->nl_ncp;
1113 * chdir_args(char *path)
1115 * Change current working directory (``.'').
1118 chdir(struct chdir_args *uap)
1120 struct nlookupdata nd;
1123 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1125 error = kern_chdir(&nd);
1131 * Helper function for raised chroot(2) security function: Refuse if
1132 * any filedescriptors are open directories.
1135 chroot_refuse_vdir_fds(fdp)
1136 struct filedesc *fdp;
1143 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1144 error = getvnode(fdp, fd, &fp);
1147 vp = (struct vnode *)fp->f_data;
1148 if (vp->v_type != VDIR)
1156 * This sysctl determines if we will allow a process to chroot(2) if it
1157 * has a directory open:
1158 * 0: disallowed for all processes.
1159 * 1: allowed for processes that were not already chroot(2)'ed.
1160 * 2: allowed for all processes.
1163 static int chroot_allow_open_directories = 1;
1165 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1166 &chroot_allow_open_directories, 0, "");
1169 * chroot to the specified namecache entry. We obtain the vp from the
1170 * namecache data. The passed ncp must be locked and referenced and will
1171 * remain locked and referenced on return.
1174 kern_chroot(struct namecache *ncp)
1176 struct thread *td = curthread;
1177 struct proc *p = td->td_proc;
1178 struct filedesc *fdp = p->p_fd;
1183 * Only root can chroot
1185 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1189 * Disallow open directory descriptors (fchdir() breakouts).
1191 if (chroot_allow_open_directories == 0 ||
1192 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1193 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1196 if ((vp = ncp->nc_vp) == NULL)
1199 if ((error = vget(vp, LK_SHARED, td)) != 0)
1203 * Check the validity of vp as a directory to change to and
1204 * associate it with rdir/jdir.
1206 error = checkvp_chdir(vp, td);
1207 VOP_UNLOCK(vp, 0); /* leave reference intact */
1209 vrele(fdp->fd_rdir);
1210 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1211 cache_drop(fdp->fd_nrdir);
1212 fdp->fd_nrdir = cache_hold(ncp);
1213 if (fdp->fd_jdir == NULL) {
1216 fdp->fd_njdir = cache_hold(ncp);
1225 * chroot_args(char *path)
1227 * Change notion of root (``/'') directory.
1231 chroot(struct chroot_args *uap)
1233 struct thread *td = curthread;
1234 struct nlookupdata nd;
1237 KKASSERT(td->td_proc);
1238 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1243 error = nlookup(&nd);
1245 error = kern_chroot(nd.nl_ncp);
1251 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1252 * determine whether it is legal to chdir to the vnode. The vnode's state
1253 * is not changed by this call.
1256 checkvp_chdir(struct vnode *vp, struct thread *td)
1260 if (vp->v_type != VDIR)
1263 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
1268 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1270 struct thread *td = curthread;
1271 struct proc *p = td->td_proc;
1272 struct lwp *lp = td->td_lwp;
1273 struct filedesc *fdp = p->p_fd;
1278 int type, indx, error;
1281 if ((oflags & O_ACCMODE) == O_ACCMODE)
1283 flags = FFLAGS(oflags);
1284 error = falloc(p, &nfp, NULL);
1288 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1291 * XXX p_dupfd is a real mess. It allows a device to return a
1292 * file descriptor to be duplicated rather then doing the open
1298 * Call vn_open() to do the lookup and assign the vnode to the
1299 * file pointer. vn_open() does not change the ref count on fp
1300 * and the vnode, on success, will be inherited by the file pointer
1303 nd->nl_flags |= NLC_LOCKVP;
1304 error = vn_open(nd, fp, flags, cmode);
1308 * handle special fdopen() case. bleh. dupfdopen() is
1309 * responsible for dropping the old contents of ofiles[indx]
1312 * Note that if fsetfd() succeeds it will add a ref to fp
1313 * which represents the fd_files[] assignment. We must still
1314 * drop our reference.
1316 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
1317 if (fsetfd(p, fp, &indx) == 0) {
1318 error = dupfdopen(fdp, indx, lp->lwp_dupfd, flags, error);
1321 fdrop(fp, td); /* our ref */
1324 if (fdp->fd_files[indx].fp == fp) {
1325 funsetfd(fdp, indx);
1326 fdrop(fp, td); /* fd_files[] ref */
1330 fdrop(fp, td); /* 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 vp = (struct vnode *)fp->f_data;
1343 if ((error = fsetfd(p, fp, &indx)) != 0) {
1350 * If no error occurs the vp will have been assigned to the file
1356 * There should be 2 references on the file, one from the descriptor
1357 * table, and one for us.
1359 * Handle the case where someone closed the file (via its file
1360 * descriptor) while we were blocked. The end result should look
1361 * like opening the file succeeded but it was immediately closed.
1363 if (fp->f_count == 1) {
1364 KASSERT(fdp->fd_files[indx].fp != fp,
1365 ("Open file descriptor lost all refs"));
1373 if (flags & (O_EXLOCK | O_SHLOCK)) {
1374 lf.l_whence = SEEK_SET;
1377 if (flags & O_EXLOCK)
1378 lf.l_type = F_WRLCK;
1380 lf.l_type = F_RDLCK;
1382 if ((flags & FNONBLOCK) == 0)
1385 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1387 * lock request failed. Normally close the descriptor
1388 * but handle the case where someone might have dup()d
1389 * it when we weren't looking. One reference is
1390 * owned by the descriptor array, the other by us.
1393 if (fdp->fd_files[indx].fp == fp) {
1394 funsetfd(fdp, indx);
1400 fp->f_flag |= FHASLOCK;
1405 * Assert that all regular file vnodes were created with a object.
1407 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1408 ("open: regular file has no backing object after vn_open"));
1414 * release our private reference, leaving the one associated with the
1415 * descriptor table intact.
1423 * open_args(char *path, int flags, int mode)
1425 * Check permissions, allocate an open file structure,
1426 * and call the device open routine if any.
1429 open(struct open_args *uap)
1431 struct nlookupdata nd;
1434 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1436 error = kern_open(&nd, uap->flags,
1437 uap->mode, &uap->sysmsg_result);
1444 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1446 struct namecache *ncp;
1447 struct thread *td = curthread;
1448 struct proc *p = td->td_proc;
1456 switch (mode & S_IFMT) {
1462 error = suser_cred(p->p_ucred, PRISON_ROOT);
1469 nd->nl_flags |= NLC_CREATE;
1470 if ((error = nlookup(nd)) != 0)
1477 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1478 vattr.va_rdev = dev;
1481 switch (mode & S_IFMT) {
1482 case S_IFMT: /* used by badsect to flag bad sectors */
1483 vattr.va_type = VBAD;
1486 vattr.va_type = VCHR;
1489 vattr.va_type = VBLK;
1500 error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1503 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1512 * mknod_args(char *path, int mode, int dev)
1514 * Create a special file.
1517 mknod(struct mknod_args *uap)
1519 struct nlookupdata nd;
1522 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1524 error = kern_mknod(&nd, uap->mode, uap->dev);
1530 kern_mkfifo(struct nlookupdata *nd, int mode)
1532 struct namecache *ncp;
1533 struct thread *td = curthread;
1534 struct proc *p = td->td_proc;
1541 nd->nl_flags |= NLC_CREATE;
1542 if ((error = nlookup(nd)) != 0)
1549 vattr.va_type = VFIFO;
1550 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1552 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1559 * mkfifo_args(char *path, int mode)
1561 * Create a named pipe.
1564 mkfifo(struct mkfifo_args *uap)
1566 struct nlookupdata nd;
1569 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1571 error = kern_mkfifo(&nd, uap->mode);
1576 static int hardlink_check_uid = 0;
1577 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1578 &hardlink_check_uid, 0,
1579 "Unprivileged processes cannot create hard links to files owned by other "
1581 static int hardlink_check_gid = 0;
1582 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1583 &hardlink_check_gid, 0,
1584 "Unprivileged processes cannot create hard links to files owned by other "
1588 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1594 * Shortcut if disabled
1596 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1600 * root cred can always hardlink
1602 if (suser_cred(cred, PRISON_ROOT) == 0)
1606 * Otherwise only if the originating file is owned by the
1607 * same user or group. Note that any group is allowed if
1608 * the file is owned by the caller.
1610 error = VOP_GETATTR(vp, &va, td);
1614 if (hardlink_check_uid) {
1615 if (cred->cr_uid != va.va_uid)
1619 if (hardlink_check_gid) {
1620 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1628 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1630 struct thread *td = curthread;
1635 * Lookup the source and obtained a locked vnode.
1637 * XXX relookup on vget failure / race ?
1640 if ((error = nlookup(nd)) != 0)
1642 vp = nd->nl_ncp->nc_vp;
1643 KKASSERT(vp != NULL);
1644 if (vp->v_type == VDIR)
1645 return (EPERM); /* POSIX */
1646 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0)
1650 * Unlock the source so we can lookup the target without deadlocking
1651 * (XXX vp is locked already, possible other deadlock?). The target
1654 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1655 nd->nl_flags &= ~NLC_NCPISLOCKED;
1656 cache_unlock(nd->nl_ncp);
1658 linknd->nl_flags |= NLC_CREATE;
1659 if ((error = nlookup(linknd)) != 0) {
1663 if (linknd->nl_ncp->nc_vp) {
1669 * Finally run the new API VOP.
1671 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1673 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
1679 * link_args(char *path, char *link)
1681 * Make a hard file link.
1684 link(struct link_args *uap)
1686 struct nlookupdata nd, linknd;
1689 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1691 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1693 error = kern_link(&nd, &linknd);
1694 nlookup_done(&linknd);
1701 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1703 struct namecache *ncp;
1709 nd->nl_flags |= NLC_CREATE;
1710 if ((error = nlookup(nd)) != 0)
1717 vattr.va_mode = mode;
1718 error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
1725 * symlink(char *path, char *link)
1727 * Make a symbolic link.
1730 symlink(struct symlink_args *uap)
1732 struct thread *td = curthread;
1733 struct nlookupdata nd;
1738 path = zalloc(namei_zone);
1739 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1741 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1743 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1744 error = kern_symlink(&nd, path, mode);
1748 zfree(namei_zone, path);
1753 * undelete_args(char *path)
1755 * Delete a whiteout from the filesystem.
1759 undelete(struct undelete_args *uap)
1761 struct nlookupdata nd;
1764 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1766 nd.nl_flags |= NLC_DELETE;
1768 error = nlookup(&nd);
1770 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1776 kern_unlink(struct nlookupdata *nd)
1778 struct namecache *ncp;
1782 nd->nl_flags |= NLC_DELETE;
1783 if ((error = nlookup(nd)) != 0)
1786 error = VOP_NREMOVE(ncp, nd->nl_cred);
1791 * unlink_args(char *path)
1793 * Delete a name from the filesystem.
1796 unlink(struct unlink_args *uap)
1798 struct nlookupdata nd;
1801 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1803 error = kern_unlink(&nd);
1809 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1811 struct thread *td = curthread;
1812 struct proc *p = td->td_proc;
1813 struct filedesc *fdp = p->p_fd;
1818 if ((u_int)fd >= fdp->fd_nfiles ||
1819 (fp = fdp->fd_files[fd].fp) == NULL)
1821 if (fp->f_type != DTYPE_VNODE)
1825 fp->f_offset += offset;
1828 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1831 fp->f_offset = offset + vattr.va_size;
1834 fp->f_offset = offset;
1839 *res = fp->f_offset;
1844 * lseek_args(int fd, int pad, off_t offset, int whence)
1846 * Reposition read/write file offset.
1849 lseek(struct lseek_args *uap)
1853 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1854 &uap->sysmsg_offset);
1860 kern_access(struct nlookupdata *nd, int aflags)
1862 struct thread *td = curthread;
1866 if ((error = nlookup(nd)) != 0)
1869 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1873 /* Flags == 0 means only check for existence. */
1882 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1883 error = VOP_ACCESS(vp, flags, nd->nl_cred, td);
1886 * If the file handle is stale we have to re-resolve the
1887 * entry. This is a hack at the moment.
1889 if (error == ESTALE) {
1890 cache_setunresolved(nd->nl_ncp);
1891 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1904 * access_args(char *path, int flags)
1906 * Check access permissions.
1909 access(struct access_args *uap)
1911 struct nlookupdata nd;
1914 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1916 error = kern_access(&nd, uap->flags);
1922 kern_stat(struct nlookupdata *nd, struct stat *st)
1928 if ((error = nlookup(nd)) != 0)
1931 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1935 if ((error = vget(vp, LK_SHARED, td)) != 0)
1937 error = vn_stat(vp, st, td);
1940 * If the file handle is stale we have to re-resolve the entry. This
1941 * is a hack at the moment.
1943 if (error == ESTALE) {
1944 cache_setunresolved(nd->nl_ncp);
1945 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1956 * stat_args(char *path, struct stat *ub)
1958 * Get file status; this version follows links.
1961 stat(struct stat_args *uap)
1963 struct nlookupdata nd;
1967 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1969 error = kern_stat(&nd, &st);
1971 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1978 * lstat_args(char *path, struct stat *ub)
1980 * Get file status; this version does not follow links.
1983 lstat(struct lstat_args *uap)
1985 struct nlookupdata nd;
1989 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1991 error = kern_stat(&nd, &st);
1993 error = copyout(&st, uap->ub, sizeof(*uap->ub));
2000 * pathconf_Args(char *path, int name)
2002 * Get configurable pathname variables.
2006 pathconf(struct pathconf_args *uap)
2008 struct nlookupdata nd;
2013 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2015 error = nlookup(&nd);
2017 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2020 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2028 * kern_readlink isn't properly split yet. There is a copyin burried
2029 * in VOP_READLINK().
2032 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2034 struct thread *td = curthread;
2035 struct proc *p = td->td_proc;
2041 if ((error = nlookup(nd)) != 0)
2043 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
2046 if (vp->v_type != VLNK) {
2049 aiov.iov_base = buf;
2050 aiov.iov_len = count;
2051 auio.uio_iov = &aiov;
2052 auio.uio_iovcnt = 1;
2053 auio.uio_offset = 0;
2054 auio.uio_rw = UIO_READ;
2055 auio.uio_segflg = UIO_USERSPACE;
2057 auio.uio_resid = count;
2058 error = VOP_READLINK(vp, &auio, p->p_ucred);
2061 *res = count - auio.uio_resid;
2066 * readlink_args(char *path, char *buf, int count)
2068 * Return target name of a symbolic link.
2071 readlink(struct readlink_args *uap)
2073 struct nlookupdata nd;
2076 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2078 error = kern_readlink(&nd, uap->buf, uap->count,
2079 &uap->sysmsg_result);
2086 setfflags(struct vnode *vp, int flags)
2088 struct thread *td = curthread;
2089 struct proc *p = td->td_proc;
2094 * Prevent non-root users from setting flags on devices. When
2095 * a device is reused, users can retain ownership of the device
2096 * if they are allowed to set flags and programs assume that
2097 * chown can't fail when done as root.
2099 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2100 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2104 * note: vget is required for any operation that might mod the vnode
2105 * so VINACTIVE is properly cleared.
2107 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2109 vattr.va_flags = flags;
2110 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2117 * chflags(char *path, int flags)
2119 * Change flags of a file given a path name.
2123 chflags(struct chflags_args *uap)
2125 struct nlookupdata nd;
2130 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2131 /* XXX Add NLC flag indicating modifying operation? */
2133 error = nlookup(&nd);
2135 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2138 error = setfflags(vp, uap->flags);
2145 * fchflags_args(int fd, int flags)
2147 * Change flags of a file given a file descriptor.
2151 fchflags(struct fchflags_args *uap)
2153 struct thread *td = curthread;
2154 struct proc *p = td->td_proc;
2158 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2160 return setfflags((struct vnode *) fp->f_data, uap->flags);
2164 setfmode(struct vnode *vp, int mode)
2166 struct thread *td = curthread;
2167 struct proc *p = td->td_proc;
2172 * note: vget is required for any operation that might mod the vnode
2173 * so VINACTIVE is properly cleared.
2175 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2177 vattr.va_mode = mode & ALLPERMS;
2178 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2185 kern_chmod(struct nlookupdata *nd, int mode)
2190 /* XXX Add NLC flag indicating modifying operation? */
2191 if ((error = nlookup(nd)) != 0)
2193 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2195 error = setfmode(vp, mode);
2201 * chmod_args(char *path, int mode)
2203 * Change mode of a file given path name.
2207 chmod(struct chmod_args *uap)
2209 struct nlookupdata nd;
2212 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2214 error = kern_chmod(&nd, uap->mode);
2220 * lchmod_args(char *path, int mode)
2222 * Change mode of a file given path name (don't follow links.)
2226 lchmod(struct lchmod_args *uap)
2228 struct nlookupdata nd;
2231 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2233 error = kern_chmod(&nd, uap->mode);
2239 * fchmod_args(int fd, int mode)
2241 * Change mode of a file given a file descriptor.
2245 fchmod(struct fchmod_args *uap)
2247 struct thread *td = curthread;
2248 struct proc *p = td->td_proc;
2252 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2254 return setfmode((struct vnode *)fp->f_data, uap->mode);
2258 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2260 struct thread *td = curthread;
2261 struct proc *p = td->td_proc;
2266 * note: vget is required for any operation that might mod the vnode
2267 * so VINACTIVE is properly cleared.
2269 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2273 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2280 kern_chown(struct nlookupdata *nd, int uid, int gid)
2285 /* XXX Add NLC flag indicating modifying operation? */
2286 if ((error = nlookup(nd)) != 0)
2288 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2290 error = setfown(vp, uid, gid);
2296 * chown(char *path, int uid, int gid)
2298 * Set ownership given a path name.
2301 chown(struct chown_args *uap)
2303 struct nlookupdata nd;
2306 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2308 error = kern_chown(&nd, uap->uid, uap->gid);
2314 * lchown_args(char *path, int uid, int gid)
2316 * Set ownership given a path name, do not cross symlinks.
2319 lchown(struct lchown_args *uap)
2321 struct nlookupdata nd;
2324 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2326 error = kern_chown(&nd, uap->uid, uap->gid);
2332 * fchown_args(int fd, int uid, int gid)
2334 * Set ownership given a file descriptor.
2338 fchown(struct fchown_args *uap)
2340 struct thread *td = curthread;
2341 struct proc *p = td->td_proc;
2345 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2347 return setfown((struct vnode *)fp->f_data,
2348 uap->uid, uap->gid);
2352 getutimes(const struct timeval *tvp, struct timespec *tsp)
2354 struct timeval tv[2];
2358 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2361 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2362 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2368 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2370 struct thread *td = curthread;
2371 struct proc *p = td->td_proc;
2376 * note: vget is required for any operation that might mod the vnode
2377 * so VINACTIVE is properly cleared.
2379 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2381 vattr.va_atime = ts[0];
2382 vattr.va_mtime = ts[1];
2384 vattr.va_vaflags |= VA_UTIMES_NULL;
2385 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2392 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2394 struct timespec ts[2];
2398 if ((error = getutimes(tptr, ts)) != 0)
2400 /* XXX Add NLC flag indicating modifying operation? */
2401 if ((error = nlookup(nd)) != 0)
2403 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2405 error = setutimes(vp, ts, tptr == NULL);
2411 * utimes_args(char *path, struct timeval *tptr)
2413 * Set the access and modification times of a file.
2416 utimes(struct utimes_args *uap)
2418 struct timeval tv[2];
2419 struct nlookupdata nd;
2423 error = copyin(uap->tptr, tv, sizeof(tv));
2427 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2429 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2435 * lutimes_args(char *path, struct timeval *tptr)
2437 * Set the access and modification times of a file.
2440 lutimes(struct lutimes_args *uap)
2442 struct timeval tv[2];
2443 struct nlookupdata nd;
2447 error = copyin(uap->tptr, tv, sizeof(tv));
2451 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2453 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2459 kern_futimes(int fd, struct timeval *tptr)
2461 struct thread *td = curthread;
2462 struct proc *p = td->td_proc;
2463 struct timespec ts[2];
2467 error = getutimes(tptr, ts);
2470 error = getvnode(p->p_fd, fd, &fp);
2473 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2478 * futimes_args(int fd, struct timeval *tptr)
2480 * Set the access and modification times of a file.
2483 futimes(struct futimes_args *uap)
2485 struct timeval tv[2];
2489 error = copyin(uap->tptr, tv, sizeof(tv));
2494 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2500 kern_truncate(struct nlookupdata *nd, off_t length)
2508 /* XXX Add NLC flag indicating modifying operation? */
2509 if ((error = nlookup(nd)) != 0)
2511 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2513 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
2517 if (vp->v_type == VDIR) {
2519 } else if ((error = vn_writechk(vp)) == 0 &&
2520 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred, nd->nl_td)) == 0) {
2522 vattr.va_size = length;
2523 error = VOP_SETATTR(vp, &vattr, nd->nl_cred, nd->nl_td);
2530 * truncate(char *path, int pad, off_t length)
2532 * Truncate a file given its path name.
2535 truncate(struct truncate_args *uap)
2537 struct nlookupdata nd;
2540 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2542 error = kern_truncate(&nd, uap->length);
2548 kern_ftruncate(int fd, off_t length)
2550 struct thread *td = curthread;
2551 struct proc *p = td->td_proc;
2559 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2561 if ((fp->f_flag & FWRITE) == 0)
2563 vp = (struct vnode *)fp->f_data;
2564 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2565 if (vp->v_type == VDIR)
2567 else if ((error = vn_writechk(vp)) == 0) {
2569 vattr.va_size = length;
2570 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2577 * ftruncate_args(int fd, int pad, off_t length)
2579 * Truncate a file given a file descriptor.
2582 ftruncate(struct ftruncate_args *uap)
2586 error = kern_ftruncate(uap->fd, uap->length);
2594 * Sync an open file.
2598 fsync(struct fsync_args *uap)
2600 struct thread *td = curthread;
2601 struct proc *p = td->td_proc;
2607 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2609 vp = (struct vnode *)fp->f_data;
2610 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2611 if ((obj = vp->v_object) != NULL)
2612 vm_object_page_clean(obj, 0, 0, 0);
2613 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2614 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2616 error = (*bioops.io_fsync)(vp);
2622 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2624 struct namecache *fncpd;
2625 struct namecache *tncpd;
2626 struct namecache *ncp;
2631 if ((error = nlookup(fromnd)) != 0)
2633 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2638 * unlock the source ncp so we can lookup the target ncp without
2639 * deadlocking. The target may or may not exist so we do not check
2640 * for a target vp like kern_mkdir() and other creation functions do.
2642 * The source and target directories are ref'd and rechecked after
2643 * everything is relocked to determine if the source or target file
2646 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2647 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2648 cache_unlock(fromnd->nl_ncp);
2650 tond->nl_flags |= NLC_CREATE;
2651 if ((error = nlookup(tond)) != 0) {
2655 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2662 * If the source and target are the same there is nothing to do
2664 if (fromnd->nl_ncp == tond->nl_ncp) {
2671 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2672 * may have become invalid while it was unlocked, nc_vp and nc_mount
2675 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2676 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2677 } else if (fromnd->nl_ncp > tond->nl_ncp) {
2678 cache_lock(fromnd->nl_ncp);
2679 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2681 cache_unlock(tond->nl_ncp);
2682 cache_lock(fromnd->nl_ncp);
2683 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2684 cache_lock(tond->nl_ncp);
2685 cache_resolve(tond->nl_ncp, tond->nl_cred);
2687 fromnd->nl_flags |= NLC_NCPISLOCKED;
2690 * make sure the parent directories linkages are the same
2692 if (fncpd != fromnd->nl_ncp->nc_parent ||
2693 tncpd != tond->nl_ncp->nc_parent) {
2700 * Both the source and target must be within the same filesystem and
2701 * in the same filesystem as their parent directories within the
2702 * namecache topology.
2704 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2706 mp = fncpd->nc_mount;
2707 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2708 mp != tond->nl_ncp->nc_mount) {
2715 * If the target exists and either the source or target is a directory,
2716 * then both must be directories.
2718 * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2721 if (tond->nl_ncp->nc_vp) {
2722 if (fromnd->nl_ncp->nc_vp == NULL) {
2724 } else if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2725 if (tond->nl_ncp->nc_vp->v_type != VDIR)
2727 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2733 * You cannot rename a source into itself or a subdirectory of itself.
2734 * We check this by travsersing the target directory upwards looking
2735 * for a match against the source.
2738 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2739 if (fromnd->nl_ncp == ncp) {
2750 * Even though the namespaces are different, they may still represent
2751 * hardlinks to the same file. The filesystem might have a hard time
2752 * with this so we issue a NREMOVE of the source instead of a NRENAME
2753 * when we detect the situation.
2756 if (fromnd->nl_ncp->nc_vp == tond->nl_ncp->nc_vp) {
2757 error = VOP_NREMOVE(fromnd->nl_ncp, fromnd->nl_cred);
2759 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp,
2767 * rename_args(char *from, char *to)
2769 * Rename files. Source and destination must either both be directories,
2770 * or both not be directories. If target is a directory, it must be empty.
2773 rename(struct rename_args *uap)
2775 struct nlookupdata fromnd, tond;
2778 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2780 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2782 error = kern_rename(&fromnd, &tond);
2783 nlookup_done(&tond);
2785 nlookup_done(&fromnd);
2790 kern_mkdir(struct nlookupdata *nd, int mode)
2792 struct thread *td = curthread;
2793 struct proc *p = td->td_proc;
2794 struct namecache *ncp;
2800 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2801 if ((error = nlookup(nd)) != 0)
2809 vattr.va_type = VDIR;
2810 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2813 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2820 * mkdir_args(char *path, int mode)
2822 * Make a directory file.
2826 mkdir(struct mkdir_args *uap)
2828 struct nlookupdata nd;
2831 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2833 error = kern_mkdir(&nd, uap->mode);
2839 kern_rmdir(struct nlookupdata *nd)
2841 struct namecache *ncp;
2845 nd->nl_flags |= NLC_DELETE;
2846 if ((error = nlookup(nd)) != 0)
2850 error = VOP_NRMDIR(ncp, nd->nl_cred);
2855 * rmdir_args(char *path)
2857 * Remove a directory file.
2861 rmdir(struct rmdir_args *uap)
2863 struct nlookupdata nd;
2866 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2868 error = kern_rmdir(&nd);
2874 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
2875 enum uio_seg direction)
2877 struct thread *td = curthread;
2878 struct proc *p = td->td_proc;
2886 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2888 if ((fp->f_flag & FREAD) == 0)
2890 vp = (struct vnode *)fp->f_data;
2892 if (vp->v_type != VDIR)
2894 aiov.iov_base = buf;
2895 aiov.iov_len = count;
2896 auio.uio_iov = &aiov;
2897 auio.uio_iovcnt = 1;
2898 auio.uio_rw = UIO_READ;
2899 auio.uio_segflg = direction;
2901 auio.uio_resid = count;
2902 /* vn_lock(vp, LK_SHARED | LK_RETRY); */
2903 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2904 loff = auio.uio_offset = fp->f_offset;
2905 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2906 fp->f_offset = auio.uio_offset;
2910 if (count == auio.uio_resid) {
2911 if (union_dircheckp) {
2912 error = union_dircheckp(td, &vp, fp);
2918 if ((vp->v_flag & VROOT) &&
2919 (vp->v_mount->mnt_flag & MNT_UNION)) {
2920 struct vnode *tvp = vp;
2921 vp = vp->v_mount->mnt_vnodecovered;
2932 *res = count - auio.uio_resid;
2937 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2939 * Read a block of directory entries in a file system independent format.
2942 getdirentries(struct getdirentries_args *uap)
2947 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2948 &uap->sysmsg_result, UIO_USERSPACE);
2951 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2956 * getdents_args(int fd, char *buf, size_t count)
2959 getdents(struct getdents_args *uap)
2963 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2964 &uap->sysmsg_result, UIO_USERSPACE);
2970 * umask(int newmask)
2972 * Set the mode mask for creation of filesystem nodes.
2977 umask(struct umask_args *uap)
2979 struct thread *td = curthread;
2980 struct proc *p = td->td_proc;
2981 struct filedesc *fdp;
2984 uap->sysmsg_result = fdp->fd_cmask;
2985 fdp->fd_cmask = uap->newmask & ALLPERMS;
2990 * revoke(char *path)
2992 * Void all references to file by ripping underlying filesystem
2997 revoke(struct revoke_args *uap)
2999 struct thread *td = curthread;
3000 struct nlookupdata nd;
3007 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3009 error = nlookup(&nd);
3011 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
3012 cred = crhold(nd.nl_cred);
3015 if (vp->v_type != VCHR && vp->v_type != VBLK)
3018 error = VOP_GETATTR(vp, &vattr, td);
3019 if (error == 0 && cred->cr_uid != vattr.va_uid)
3020 error = suser_cred(cred, PRISON_ROOT);
3021 if (error == 0 && count_udev(vp->v_udev) > 0) {
3022 if ((error = vx_lock(vp)) == 0) {
3023 VOP_REVOKE(vp, REVOKEALL);
3035 * Convert a user file descriptor to a kernel file entry.
3038 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
3042 if ((u_int)fd >= fdp->fd_nfiles ||
3043 (fp = fdp->fd_files[fd].fp) == NULL)
3045 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
3051 * getfh_args(char *fname, fhandle_t *fhp)
3053 * Get (NFS) file handle
3056 getfh(struct getfh_args *uap)
3058 struct thread *td = curthread;
3059 struct nlookupdata nd;
3065 * Must be super user
3067 if ((error = suser(td)) != 0)
3071 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3073 error = nlookup(&nd);
3075 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3078 bzero(&fh, sizeof(fh));
3079 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3080 error = VFS_VPTOFH(vp, &fh.fh_fid);
3083 error = copyout(&fh, uap->fhp, sizeof(fh));
3089 * fhopen_args(const struct fhandle *u_fhp, int flags)
3091 * syscall for the rpc.lockd to use to translate a NFS file handle into
3092 * an open descriptor.
3094 * warning: do not remove the suser() call or this becomes one giant
3098 fhopen(struct fhopen_args *uap)
3100 struct thread *td = curthread;
3101 struct proc *p = td->td_proc;
3106 struct vattr *vap = &vat;
3108 struct filedesc *fdp = p->p_fd;
3109 int fmode, mode, error, type;
3115 * Must be super user
3121 fmode = FFLAGS(uap->flags);
3122 /* why not allow a non-read/write open for our lockd? */
3123 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3125 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3128 /* find the mount point */
3129 mp = vfs_getvfs(&fhp.fh_fsid);
3132 /* now give me my vnode, it gets returned to me locked */
3133 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3137 * from now on we have to make sure not
3138 * to forget about the vnode
3139 * any error that causes an abort must vput(vp)
3140 * just set error = err and 'goto bad;'.
3146 if (vp->v_type == VLNK) {
3150 if (vp->v_type == VSOCK) {
3155 if (fmode & (FWRITE | O_TRUNC)) {
3156 if (vp->v_type == VDIR) {
3160 error = vn_writechk(vp);
3168 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
3172 if (fmode & O_TRUNC) {
3173 VOP_UNLOCK(vp, 0); /* XXX */
3174 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
3177 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
3183 * VOP_OPEN needs the file pointer so it can potentially override
3186 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3189 if ((error = falloc(p, &nfp, NULL)) != 0)
3193 error = VOP_OPEN(vp, fmode, p->p_ucred, fp, td);
3196 * setting f_ops this way prevents VOP_CLOSE from being
3197 * called or fdrop() releasing the vp from v_data. Since
3198 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3200 fp->f_ops = &badfileops;
3207 * The fp is given its own reference, we still have our ref and lock.
3209 * Assert that all regular files must be created with a VM object.
3211 if (vp->v_type == VREG && vp->v_object == NULL) {
3212 printf("fhopen: regular file did not have VM object: %p\n", vp);
3218 * The open was successful, associate it with a file descriptor.
3220 if ((error = fsetfd(p, fp, &indx)) != 0) {
3225 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3226 lf.l_whence = SEEK_SET;
3229 if (fmode & O_EXLOCK)
3230 lf.l_type = F_WRLCK;
3232 lf.l_type = F_RDLCK;
3234 if ((fmode & FNONBLOCK) == 0)
3237 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3239 * lock request failed. Normally close the descriptor
3240 * but handle the case where someone might have dup()d
3241 * or close()d it when we weren't looking.
3243 if (fdp->fd_files[indx].fp == fp) {
3244 funsetfd(fdp, indx);
3249 * release our private reference.
3255 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3256 fp->f_flag |= FHASLOCK;
3261 uap->sysmsg_result = indx;
3270 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3273 fhstat(struct fhstat_args *uap)
3275 struct thread *td = curthread;
3283 * Must be super user
3289 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3293 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3295 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3297 error = vn_stat(vp, &sb, td);
3301 error = copyout(&sb, uap->sb, sizeof(sb));
3306 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3309 fhstatfs(struct fhstatfs_args *uap)
3311 struct thread *td = curthread;
3312 struct proc *p = td->td_proc;
3317 char *fullpath, *freepath;
3322 * Must be super user
3324 if ((error = suser(td)))
3327 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3330 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3333 if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0 &&
3334 !chroot_visible_mnt(mp, p))
3337 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3342 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3345 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
3348 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3349 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3350 free(freepath, M_TEMP);
3352 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3354 bcopy(sp, &sb, sizeof(sb));
3355 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3358 return (copyout(sp, uap->buf, sizeof(*sp)));
3362 * Syscall to push extended attribute configuration information into the
3363 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3364 * a command (int cmd), and attribute name and misc data. For now, the
3365 * attribute name is left in userspace for consumption by the VFS_op.
3366 * It will probably be changed to be copied into sysspace by the
3367 * syscall in the future, once issues with various consumers of the
3368 * attribute code have raised their hands.
3370 * Currently this is used only by UFS Extended Attributes.
3373 extattrctl(struct extattrctl_args *uap)
3375 struct nlookupdata nd;
3381 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3383 error = nlookup(&nd);
3385 mp = nd.nl_ncp->nc_mount;
3386 error = VFS_EXTATTRCTL(mp, uap->cmd,
3387 uap->attrname, uap->arg,
3395 * Syscall to set a named extended attribute on a file or directory.
3396 * Accepts attribute name, and a uio structure pointing to the data to set.
3397 * The uio is consumed in the style of writev(). The real work happens
3398 * in VOP_SETEXTATTR().
3401 extattr_set_file(struct extattr_set_file_args *uap)
3403 char attrname[EXTATTR_MAXNAMELEN];
3404 struct iovec aiov[UIO_SMALLIOV];
3405 struct iovec *needfree;
3406 struct nlookupdata nd;
3415 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3420 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3422 error = nlookup(&nd);
3424 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3431 iovlen = uap->iovcnt * sizeof(struct iovec);
3432 if (uap->iovcnt > UIO_SMALLIOV) {
3433 if (uap->iovcnt > UIO_MAXIOV) {
3437 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3443 auio.uio_iovcnt = uap->iovcnt;
3444 auio.uio_rw = UIO_WRITE;
3445 auio.uio_segflg = UIO_USERSPACE;
3446 auio.uio_td = nd.nl_td;
3447 auio.uio_offset = 0;
3448 if ((error = copyin(uap->iovp, iov, iovlen)))
3451 for (i = 0; i < uap->iovcnt; i++) {
3452 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3456 auio.uio_resid += iov->iov_len;
3459 cnt = auio.uio_resid;
3460 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td);
3461 cnt -= auio.uio_resid;
3462 uap->sysmsg_result = cnt;
3467 FREE(needfree, M_IOV);
3472 * Syscall to get a named extended attribute on a file or directory.
3473 * Accepts attribute name, and a uio structure pointing to a buffer for the
3474 * data. The uio is consumed in the style of readv(). The real work
3475 * happens in VOP_GETEXTATTR();
3478 extattr_get_file(struct extattr_get_file_args *uap)
3480 char attrname[EXTATTR_MAXNAMELEN];
3481 struct iovec aiov[UIO_SMALLIOV];
3482 struct iovec *needfree;
3483 struct nlookupdata nd;
3492 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3497 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3499 error = nlookup(&nd);
3501 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3507 iovlen = uap->iovcnt * sizeof (struct iovec);
3509 if (uap->iovcnt > UIO_SMALLIOV) {
3510 if (uap->iovcnt > UIO_MAXIOV) {
3514 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3520 auio.uio_iovcnt = uap->iovcnt;
3521 auio.uio_rw = UIO_READ;
3522 auio.uio_segflg = UIO_USERSPACE;
3523 auio.uio_td = nd.nl_td;
3524 auio.uio_offset = 0;
3525 if ((error = copyin(uap->iovp, iov, iovlen)))
3528 for (i = 0; i < uap->iovcnt; i++) {
3529 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3533 auio.uio_resid += iov->iov_len;
3536 cnt = auio.uio_resid;
3537 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td);
3538 cnt -= auio.uio_resid;
3539 uap->sysmsg_result = cnt;
3544 FREE(needfree, M_IOV);
3549 * Syscall to delete a named extended attribute from a file or directory.
3550 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3553 extattr_delete_file(struct extattr_delete_file_args *uap)
3555 char attrname[EXTATTR_MAXNAMELEN];
3556 struct nlookupdata nd;
3560 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3565 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3567 error = nlookup(&nd);
3569 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3575 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred, nd.nl_td);
3582 chroot_visible_mnt(struct mount *mp, struct proc *p)
3584 struct namecache *ncp;
3586 * First check if this file system is below
3590 while (ncp != NULL && ncp != p->p_fd->fd_nrdir)
3591 ncp = ncp->nc_parent;
3594 * This is not below the chroot path.
3596 * Check if the chroot path is on the same filesystem,
3597 * by determing if we have to cross a mount point
3598 * before reaching mp->mnt_ncp.
3600 ncp = p->p_fd->fd_nrdir;
3601 while (ncp != NULL && ncp != mp->mnt_ncp) {
3602 if (ncp->nc_flag & NCF_MOUNTPT) {
3606 ncp = ncp->nc_parent;
3609 return(ncp != NULL);