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.73 2005/09/29 20:59:30 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);
216 VOP_UNLOCK(vp, 0, td);
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, 0, "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;
307 VOP_UNLOCK(vp, 0, td);
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, td);
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 */
371 VOP_UNLOCK(vp, 0, td);
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;
459 struct nlookupdata nd;
462 if (p->p_ucred->cr_prison != NULL)
464 if (usermount == 0 && (error = suser(td)))
468 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
470 error = nlookup(&nd);
472 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
480 * Only root, or the user that did the original mount is
481 * permitted to unmount this filesystem.
483 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
484 (error = suser(td))) {
490 * Don't allow unmounting the root file system.
492 if (mp->mnt_flag & MNT_ROOTFS) {
498 * Must be the root of the filesystem
500 if ((vp->v_flag & VROOT) == 0) {
505 return (dounmount(mp, uap->flags, td));
509 * Do the actual file system unmount.
512 dounmount_interlock(struct mount *mp)
514 if (mp->mnt_kern_flag & MNTK_UNMOUNT)
516 mp->mnt_kern_flag |= MNTK_UNMOUNT;
521 dounmount(struct mount *mp, int flags, struct thread *td)
523 struct vnode *coveredvp;
528 * Exclusive access for unmounting purposes
530 if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
534 * Allow filesystems to detect that a forced unmount is in progress.
536 if (flags & MNT_FORCE)
537 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
538 error = lockmgr(&mp->mnt_lock, LK_DRAIN |
539 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), NULL, td);
541 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
542 if (mp->mnt_kern_flag & MNTK_MWAIT)
547 if (mp->mnt_flag & MNT_EXPUBLIC)
548 vfs_setpublicfs(NULL, NULL, NULL);
550 vfs_msync(mp, MNT_WAIT);
551 async_flag = mp->mnt_flag & MNT_ASYNC;
552 mp->mnt_flag &=~ MNT_ASYNC;
553 cache_purgevfs(mp); /* remove cache entries for this file sys */
554 if (mp->mnt_syncer != NULL)
555 vrele(mp->mnt_syncer);
556 if (((mp->mnt_flag & MNT_RDONLY) ||
557 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
559 error = VFS_UNMOUNT(mp, flags, td);
561 if (mp->mnt_syncer == NULL)
562 vfs_allocate_syncvnode(mp);
563 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
564 mp->mnt_flag |= async_flag;
565 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_REENABLE, NULL, td);
566 if (mp->mnt_kern_flag & MNTK_MWAIT)
571 * Clean up any journals still associated with the mount after
572 * filesystem activity has ceased.
574 journal_remove_all_journals(mp,
575 ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
577 mountlist_remove(mp);
580 * Remove any installed vnode ops here so the individual VFSs don't
583 vfs_rm_vnodeops(&mp->mnt_vn_coherency_ops);
584 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
585 vfs_rm_vnodeops(&mp->mnt_vn_norm_ops);
586 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
587 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
589 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
590 coveredvp->v_mountedhere = NULL;
592 cache_drop(mp->mnt_ncp);
595 mp->mnt_vfc->vfc_refcount--;
596 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
597 panic("unmount: dangling vnode");
598 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
599 if (mp->mnt_kern_flag & MNTK_MWAIT)
606 * Sync each mounted filesystem.
610 static int syncprt = 0;
611 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
614 static int sync_callback(struct mount *mp, void *data);
618 sync(struct sync_args *uap)
620 mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
623 * print out buffer pool stat information on each sync() call.
633 sync_callback(struct mount *mp, void *data __unused)
637 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
638 asyncflag = mp->mnt_flag & MNT_ASYNC;
639 mp->mnt_flag &= ~MNT_ASYNC;
640 vfs_msync(mp, MNT_NOWAIT);
641 VFS_SYNC(mp, MNT_NOWAIT, curthread);
642 mp->mnt_flag |= asyncflag;
647 /* XXX PRISON: could be per prison flag */
648 static int prison_quotas;
650 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
654 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
656 * Change filesystem quotas.
660 quotactl(struct quotactl_args *uap)
662 struct nlookupdata nd;
670 if (p->p_ucred->cr_prison && !prison_quotas)
673 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
675 error = nlookup(&nd);
677 mp = nd.nl_ncp->nc_mount;
678 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
686 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
687 * void *buf, int buflen)
689 * This function operates on a mount point and executes the specified
690 * operation using the specified control data, and possibly returns data.
692 * The actual number of bytes stored in the result buffer is returned, 0
693 * if none, otherwise an error is returned.
697 mountctl(struct mountctl_args *uap)
699 struct thread *td = curthread;
700 struct proc *p = td->td_proc;
701 struct filedesc *fdp = p->p_fd;
709 * Sanity and permissions checks. We must be root.
712 if (p->p_ucred->cr_prison != NULL)
714 if ((error = suser(td)) != 0)
718 * Argument length checks
720 if (uap->ctllen < 0 || uap->ctllen > 1024)
722 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
724 if (uap->path == NULL)
728 * Allocate the necessary buffers and copyin data
730 path = zalloc(namei_zone);
731 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
736 ctl = malloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
737 error = copyin(uap->ctl, ctl, uap->ctllen);
742 buf = malloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
745 * Validate the descriptor
749 } else if ((u_int)uap->fd >= fdp->fd_nfiles ||
750 (fp = fdp->fd_files[uap->fd].fp) == NULL) {
758 * Execute the internal kernel function and clean up.
760 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
763 if (error == 0 && uap->sysmsg_result > 0)
764 error = copyout(buf, uap->buf, uap->sysmsg_result);
767 zfree(namei_zone, path);
776 * Execute a mount control operation by resolving the path to a mount point
777 * and calling vop_mountctl().
780 kern_mountctl(const char *path, int op, struct file *fp,
781 const void *ctl, int ctllen,
782 void *buf, int buflen, int *res)
786 struct nlookupdata nd;
791 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
793 error = nlookup(&nd);
795 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
803 * Must be the root of the filesystem
805 if ((vp->v_flag & VROOT) == 0) {
809 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
816 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
818 struct thread *td = curthread;
819 struct proc *p = td->td_proc;
822 char *fullpath, *freepath;
825 if ((error = nlookup(nd)) != 0)
827 mp = nd->nl_ncp->nc_mount;
829 if ((error = VFS_STATFS(mp, sp, td)) != 0)
832 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
835 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
836 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
837 free(freepath, M_TEMP);
839 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
840 bcopy(sp, buf, sizeof(*buf));
841 /* Only root should have access to the fsid's. */
843 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
848 * statfs_args(char *path, struct statfs *buf)
850 * Get filesystem statistics.
853 statfs(struct statfs_args *uap)
855 struct nlookupdata nd;
859 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
861 error = kern_statfs(&nd, &buf);
864 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
869 kern_fstatfs(int fd, struct statfs *buf)
871 struct thread *td = curthread;
872 struct proc *p = td->td_proc;
876 char *fullpath, *freepath;
880 error = getvnode(p->p_fd, fd, &fp);
883 mp = ((struct vnode *)fp->f_data)->v_mount;
887 error = VFS_STATFS(mp, sp, td);
891 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
894 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
895 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
896 free(freepath, M_TEMP);
898 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
899 bcopy(sp, buf, sizeof(*buf));
901 /* Only root should have access to the fsid's. */
903 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
908 * fstatfs_args(int fd, struct statfs *buf)
910 * Get filesystem statistics.
913 fstatfs(struct fstatfs_args *uap)
918 error = kern_fstatfs(uap->fd, &buf);
921 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
926 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
928 * Get statistics on all filesystems.
931 struct getfsstat_info {
942 static int getfsstat_callback(struct mount *, void *);
946 getfsstat(struct getfsstat_args *uap)
948 struct thread *td = curthread;
949 struct proc *p = td->td_proc;
950 struct getfsstat_info info;
952 bzero(&info, sizeof(info));
953 if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0)
954 info.is_chrooted = 1;
956 info.is_chrooted = 0;
958 info.maxcount = uap->bufsize / sizeof(struct statfs);
959 info.sfsp = uap->buf;
961 info.flags = uap->flags;
965 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
966 if (info.sfsp && info.count > info.maxcount)
967 uap->sysmsg_result = info.maxcount;
969 uap->sysmsg_result = info.count;
974 getfsstat_callback(struct mount *mp, void *data)
976 struct getfsstat_info *info = data;
982 if (info->sfsp && info->count < info->maxcount) {
983 if (info->is_chrooted && !chroot_visible_mnt(mp, info->p))
988 * If MNT_NOWAIT or MNT_LAZY is specified, do not
989 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
990 * overrides MNT_WAIT.
992 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
993 (info->flags & MNT_WAIT)) &&
994 (error = VFS_STATFS(mp, sp, info->td))) {
997 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
999 error = cache_fullpath(info->p, mp->mnt_ncp,
1000 &fullpath, &freepath);
1002 info->error = error;
1005 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1006 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1007 free(freepath, M_TEMP);
1009 error = copyout(sp, info->sfsp, sizeof(*sp));
1011 info->error = error;
1021 * fchdir_args(int fd)
1023 * Change current working directory to a given file descriptor.
1027 fchdir(struct fchdir_args *uap)
1029 struct thread *td = curthread;
1030 struct proc *p = td->td_proc;
1031 struct filedesc *fdp = p->p_fd;
1032 struct vnode *vp, *ovp;
1035 struct namecache *ncp, *oncp;
1036 struct namecache *nct;
1039 if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
1041 vp = (struct vnode *)fp->f_data;
1043 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1044 if (vp->v_type != VDIR || fp->f_ncp == NULL)
1047 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
1052 ncp = cache_hold(fp->f_ncp);
1053 while (!error && (mp = vp->v_mountedhere) != NULL) {
1054 error = nlookup_mp(mp, &nct);
1056 cache_unlock(nct); /* leave ref intact */
1059 error = vget(vp, LK_SHARED, td);
1060 KKASSERT(error == 0);
1067 oncp = fdp->fd_ncdir;
1068 VOP_UNLOCK(vp, 0, td); /* leave ref intact */
1070 fdp->fd_ncdir = ncp;
1081 kern_chdir(struct nlookupdata *nd)
1083 struct thread *td = curthread;
1084 struct proc *p = td->td_proc;
1085 struct filedesc *fdp = p->p_fd;
1086 struct vnode *vp, *ovp;
1087 struct namecache *oncp;
1090 if ((error = nlookup(nd)) != 0)
1092 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1094 if ((error = vget(vp, LK_SHARED, td)) != 0)
1097 error = checkvp_chdir(vp, td);
1098 VOP_UNLOCK(vp, 0, td);
1101 oncp = fdp->fd_ncdir;
1102 cache_unlock(nd->nl_ncp); /* leave reference intact */
1103 fdp->fd_ncdir = nd->nl_ncp;
1115 * chdir_args(char *path)
1117 * Change current working directory (``.'').
1120 chdir(struct chdir_args *uap)
1122 struct nlookupdata nd;
1125 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1127 error = kern_chdir(&nd);
1133 * Helper function for raised chroot(2) security function: Refuse if
1134 * any filedescriptors are open directories.
1137 chroot_refuse_vdir_fds(fdp)
1138 struct filedesc *fdp;
1145 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1146 error = getvnode(fdp, fd, &fp);
1149 vp = (struct vnode *)fp->f_data;
1150 if (vp->v_type != VDIR)
1158 * This sysctl determines if we will allow a process to chroot(2) if it
1159 * has a directory open:
1160 * 0: disallowed for all processes.
1161 * 1: allowed for processes that were not already chroot(2)'ed.
1162 * 2: allowed for all processes.
1165 static int chroot_allow_open_directories = 1;
1167 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1168 &chroot_allow_open_directories, 0, "");
1171 * chroot to the specified namecache entry. We obtain the vp from the
1172 * namecache data. The passed ncp must be locked and referenced and will
1173 * remain locked and referenced on return.
1176 kern_chroot(struct namecache *ncp)
1178 struct thread *td = curthread;
1179 struct proc *p = td->td_proc;
1180 struct filedesc *fdp = p->p_fd;
1185 * Only root can chroot
1187 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1191 * Disallow open directory descriptors (fchdir() breakouts).
1193 if (chroot_allow_open_directories == 0 ||
1194 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1195 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1198 if ((vp = ncp->nc_vp) == NULL)
1201 if ((error = vget(vp, LK_SHARED, td)) != 0)
1205 * Check the validity of vp as a directory to change to and
1206 * associate it with rdir/jdir.
1208 error = checkvp_chdir(vp, td);
1209 VOP_UNLOCK(vp, 0, td); /* leave reference intact */
1211 vrele(fdp->fd_rdir);
1212 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1213 cache_drop(fdp->fd_nrdir);
1214 fdp->fd_nrdir = cache_hold(ncp);
1215 if (fdp->fd_jdir == NULL) {
1218 fdp->fd_njdir = cache_hold(ncp);
1227 * chroot_args(char *path)
1229 * Change notion of root (``/'') directory.
1233 chroot(struct chroot_args *uap)
1235 struct thread *td = curthread;
1236 struct nlookupdata nd;
1239 KKASSERT(td->td_proc);
1240 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1245 error = nlookup(&nd);
1247 error = kern_chroot(nd.nl_ncp);
1253 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1254 * determine whether it is legal to chdir to the vnode. The vnode's state
1255 * is not changed by this call.
1258 checkvp_chdir(struct vnode *vp, struct thread *td)
1262 if (vp->v_type != VDIR)
1265 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
1270 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1272 struct thread *td = curthread;
1273 struct proc *p = td->td_proc;
1274 struct filedesc *fdp = p->p_fd;
1279 int type, indx, error;
1282 if ((oflags & O_ACCMODE) == O_ACCMODE)
1284 flags = FFLAGS(oflags);
1285 error = falloc(p, &nfp, NULL);
1289 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1292 * XXX p_dupfd is a real mess. It allows a device to return a
1293 * file descriptor to be duplicated rather then doing the open
1299 * Call vn_open() to do the lookup and assign the vnode to the
1300 * file pointer. vn_open() does not change the ref count on fp
1301 * and the vnode, on success, will be inherited by the file pointer
1304 nd->nl_flags |= NLC_LOCKVP;
1305 error = vn_open(nd, fp, flags, cmode);
1309 * handle special fdopen() case. bleh. dupfdopen() is
1310 * responsible for dropping the old contents of ofiles[indx]
1313 * Note that if fsetfd() succeeds it will add a ref to fp
1314 * which represents the fd_files[] assignment. We must still
1315 * drop our reference.
1317 if ((error == ENODEV || error == ENXIO) && p->p_dupfd >= 0) {
1318 if (fsetfd(p, fp, &indx) == 0) {
1319 error = dupfdopen(fdp, indx, p->p_dupfd, flags, error);
1322 fdrop(fp, td); /* our ref */
1325 if (fdp->fd_files[indx].fp == fp) {
1326 funsetfd(fdp, indx);
1327 fdrop(fp, td); /* fd_files[] ref */
1331 fdrop(fp, td); /* our ref */
1332 if (error == ERESTART)
1338 * ref the vnode for ourselves so it can't be ripped out from under
1339 * is. XXX need an ND flag to request that the vnode be returned
1342 vp = (struct vnode *)fp->f_data;
1344 if ((error = fsetfd(p, fp, &indx)) != 0) {
1351 * If no error occurs the vp will have been assigned to the file
1357 * There should be 2 references on the file, one from the descriptor
1358 * table, and one for us.
1360 * Handle the case where someone closed the file (via its file
1361 * descriptor) while we were blocked. The end result should look
1362 * like opening the file succeeded but it was immediately closed.
1364 if (fp->f_count == 1) {
1365 KASSERT(fdp->fd_files[indx].fp != fp,
1366 ("Open file descriptor lost all refs"));
1374 if (flags & (O_EXLOCK | O_SHLOCK)) {
1375 lf.l_whence = SEEK_SET;
1378 if (flags & O_EXLOCK)
1379 lf.l_type = F_WRLCK;
1381 lf.l_type = F_RDLCK;
1383 if ((flags & FNONBLOCK) == 0)
1386 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1388 * lock request failed. Normally close the descriptor
1389 * but handle the case where someone might have dup()d
1390 * it when we weren't looking. One reference is
1391 * owned by the descriptor array, the other by us.
1394 if (fdp->fd_files[indx].fp == fp) {
1395 funsetfd(fdp, indx);
1401 fp->f_flag |= FHASLOCK;
1403 /* assert that vn_open created a backing object if one is needed */
1404 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1405 ("open: vmio vnode has no backing object after vn_open"));
1410 * release our private reference, leaving the one associated with the
1411 * descriptor table intact.
1419 * open_args(char *path, int flags, int mode)
1421 * Check permissions, allocate an open file structure,
1422 * and call the device open routine if any.
1425 open(struct open_args *uap)
1427 struct nlookupdata nd;
1430 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1432 error = kern_open(&nd, uap->flags,
1433 uap->mode, &uap->sysmsg_result);
1440 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1442 struct namecache *ncp;
1443 struct thread *td = curthread;
1444 struct proc *p = td->td_proc;
1452 switch (mode & S_IFMT) {
1458 error = suser_cred(p->p_ucred, PRISON_ROOT);
1465 nd->nl_flags |= NLC_CREATE;
1466 if ((error = nlookup(nd)) != 0)
1473 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1474 vattr.va_rdev = dev;
1477 switch (mode & S_IFMT) {
1478 case S_IFMT: /* used by badsect to flag bad sectors */
1479 vattr.va_type = VBAD;
1482 vattr.va_type = VCHR;
1485 vattr.va_type = VBLK;
1496 error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1499 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1508 * mknod_args(char *path, int mode, int dev)
1510 * Create a special file.
1513 mknod(struct mknod_args *uap)
1515 struct nlookupdata nd;
1518 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1520 error = kern_mknod(&nd, uap->mode, uap->dev);
1526 kern_mkfifo(struct nlookupdata *nd, int mode)
1528 struct namecache *ncp;
1529 struct thread *td = curthread;
1530 struct proc *p = td->td_proc;
1537 nd->nl_flags |= NLC_CREATE;
1538 if ((error = nlookup(nd)) != 0)
1545 vattr.va_type = VFIFO;
1546 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1548 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1555 * mkfifo_args(char *path, int mode)
1557 * Create a named pipe.
1560 mkfifo(struct mkfifo_args *uap)
1562 struct nlookupdata nd;
1565 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1567 error = kern_mkfifo(&nd, uap->mode);
1572 static int hardlink_check_uid = 0;
1573 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1574 &hardlink_check_uid, 0,
1575 "Unprivileged processes cannot create hard links to files owned by other "
1577 static int hardlink_check_gid = 0;
1578 SYSCTL_INT(_kern, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1579 &hardlink_check_gid, 0,
1580 "Unprivileged processes cannot create hard links to files owned by other "
1584 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1590 * Shortcut if disabled
1592 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1596 * root cred can always hardlink
1598 if (suser_cred(cred, PRISON_ROOT) == 0)
1602 * Otherwise only if the originating file is owned by the
1603 * same user or group. Note that any group is allowed if
1604 * the file is owned by the caller.
1606 error = VOP_GETATTR(vp, &va, td);
1610 if (hardlink_check_uid) {
1611 if (cred->cr_uid != va.va_uid)
1615 if (hardlink_check_gid) {
1616 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1624 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1626 struct thread *td = curthread;
1631 * Lookup the source and obtained a locked vnode.
1633 * XXX relookup on vget failure / race ?
1636 if ((error = nlookup(nd)) != 0)
1638 vp = nd->nl_ncp->nc_vp;
1639 KKASSERT(vp != NULL);
1640 if (vp->v_type == VDIR)
1641 return (EPERM); /* POSIX */
1642 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0)
1646 * Unlock the source so we can lookup the target without deadlocking
1647 * (XXX vp is locked already, possible other deadlock?). The target
1650 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1651 nd->nl_flags &= ~NLC_NCPISLOCKED;
1652 cache_unlock(nd->nl_ncp);
1654 linknd->nl_flags |= NLC_CREATE;
1655 if ((error = nlookup(linknd)) != 0) {
1659 if (linknd->nl_ncp->nc_vp) {
1665 * Finally run the new API VOP.
1667 error = can_hardlink(vp, td, td->td_proc->p_ucred);
1669 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
1675 * link_args(char *path, char *link)
1677 * Make a hard file link.
1680 link(struct link_args *uap)
1682 struct nlookupdata nd, linknd;
1685 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1687 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1689 error = kern_link(&nd, &linknd);
1690 nlookup_done(&linknd);
1697 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1699 struct namecache *ncp;
1705 nd->nl_flags |= NLC_CREATE;
1706 if ((error = nlookup(nd)) != 0)
1713 vattr.va_mode = mode;
1714 error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
1721 * symlink(char *path, char *link)
1723 * Make a symbolic link.
1726 symlink(struct symlink_args *uap)
1728 struct thread *td = curthread;
1729 struct nlookupdata nd;
1734 path = zalloc(namei_zone);
1735 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1737 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1739 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1740 error = kern_symlink(&nd, path, mode);
1744 zfree(namei_zone, path);
1749 * undelete_args(char *path)
1751 * Delete a whiteout from the filesystem.
1755 undelete(struct undelete_args *uap)
1757 struct nlookupdata nd;
1760 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1762 nd.nl_flags |= NLC_DELETE;
1764 error = nlookup(&nd);
1766 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1772 kern_unlink(struct nlookupdata *nd)
1774 struct namecache *ncp;
1778 nd->nl_flags |= NLC_DELETE;
1779 if ((error = nlookup(nd)) != 0)
1782 error = VOP_NREMOVE(ncp, nd->nl_cred);
1787 * unlink_args(char *path)
1789 * Delete a name from the filesystem.
1792 unlink(struct unlink_args *uap)
1794 struct nlookupdata nd;
1797 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1799 error = kern_unlink(&nd);
1805 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1807 struct thread *td = curthread;
1808 struct proc *p = td->td_proc;
1809 struct filedesc *fdp = p->p_fd;
1814 if ((u_int)fd >= fdp->fd_nfiles ||
1815 (fp = fdp->fd_files[fd].fp) == NULL)
1817 if (fp->f_type != DTYPE_VNODE)
1821 fp->f_offset += offset;
1824 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1827 fp->f_offset = offset + vattr.va_size;
1830 fp->f_offset = offset;
1835 *res = fp->f_offset;
1840 * lseek_args(int fd, int pad, off_t offset, int whence)
1842 * Reposition read/write file offset.
1845 lseek(struct lseek_args *uap)
1849 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1850 &uap->sysmsg_offset);
1856 kern_access(struct nlookupdata *nd, int aflags)
1858 struct thread *td = curthread;
1862 if ((error = nlookup(nd)) != 0)
1865 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1869 /* Flags == 0 means only check for existence. */
1878 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1879 error = VOP_ACCESS(vp, flags, nd->nl_cred, td);
1882 * If the file handle is stale we have to re-resolve the
1883 * entry. This is a hack at the moment.
1885 if (error == ESTALE) {
1886 cache_setunresolved(nd->nl_ncp);
1887 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1900 * access_args(char *path, int flags)
1902 * Check access permissions.
1905 access(struct access_args *uap)
1907 struct nlookupdata nd;
1910 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1912 error = kern_access(&nd, uap->flags);
1918 kern_stat(struct nlookupdata *nd, struct stat *st)
1924 if ((error = nlookup(nd)) != 0)
1927 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1931 if ((error = vget(vp, LK_SHARED, td)) != 0)
1933 error = vn_stat(vp, st, td);
1936 * If the file handle is stale we have to re-resolve the entry. This
1937 * is a hack at the moment.
1939 if (error == ESTALE) {
1940 cache_setunresolved(nd->nl_ncp);
1941 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1952 * stat_args(char *path, struct stat *ub)
1954 * Get file status; this version follows links.
1957 stat(struct stat_args *uap)
1959 struct nlookupdata nd;
1963 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1965 error = kern_stat(&nd, &st);
1967 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1974 * lstat_args(char *path, struct stat *ub)
1976 * Get file status; this version does not follow links.
1979 lstat(struct lstat_args *uap)
1981 struct nlookupdata nd;
1985 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1987 error = kern_stat(&nd, &st);
1989 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1996 * pathconf_Args(char *path, int name)
1998 * Get configurable pathname variables.
2002 pathconf(struct pathconf_args *uap)
2004 struct nlookupdata nd;
2009 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2011 error = nlookup(&nd);
2013 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2016 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
2024 * kern_readlink isn't properly split yet. There is a copyin burried
2025 * in VOP_READLINK().
2028 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2030 struct thread *td = curthread;
2031 struct proc *p = td->td_proc;
2037 if ((error = nlookup(nd)) != 0)
2039 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
2042 if (vp->v_type != VLNK) {
2045 aiov.iov_base = buf;
2046 aiov.iov_len = count;
2047 auio.uio_iov = &aiov;
2048 auio.uio_iovcnt = 1;
2049 auio.uio_offset = 0;
2050 auio.uio_rw = UIO_READ;
2051 auio.uio_segflg = UIO_USERSPACE;
2053 auio.uio_resid = count;
2054 error = VOP_READLINK(vp, &auio, p->p_ucred);
2057 *res = count - auio.uio_resid;
2062 * readlink_args(char *path, char *buf, int count)
2064 * Return target name of a symbolic link.
2067 readlink(struct readlink_args *uap)
2069 struct nlookupdata nd;
2072 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2074 error = kern_readlink(&nd, uap->buf, uap->count,
2075 &uap->sysmsg_result);
2082 setfflags(struct vnode *vp, int flags)
2084 struct thread *td = curthread;
2085 struct proc *p = td->td_proc;
2090 * Prevent non-root users from setting flags on devices. When
2091 * a device is reused, users can retain ownership of the device
2092 * if they are allowed to set flags and programs assume that
2093 * chown can't fail when done as root.
2095 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2096 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2100 * note: vget is required for any operation that might mod the vnode
2101 * so VINACTIVE is properly cleared.
2103 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2104 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2106 vattr.va_flags = flags;
2107 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2114 * chflags(char *path, int flags)
2116 * Change flags of a file given a path name.
2120 chflags(struct chflags_args *uap)
2122 struct nlookupdata nd;
2127 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2128 /* XXX Add NLC flag indicating modifying operation? */
2130 error = nlookup(&nd);
2132 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2135 error = setfflags(vp, uap->flags);
2142 * fchflags_args(int fd, int flags)
2144 * Change flags of a file given a file descriptor.
2148 fchflags(struct fchflags_args *uap)
2150 struct thread *td = curthread;
2151 struct proc *p = td->td_proc;
2155 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2157 return setfflags((struct vnode *) fp->f_data, uap->flags);
2161 setfmode(struct vnode *vp, int mode)
2163 struct thread *td = curthread;
2164 struct proc *p = td->td_proc;
2169 * note: vget is required for any operation that might mod the vnode
2170 * so VINACTIVE is properly cleared.
2172 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2173 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2175 vattr.va_mode = mode & ALLPERMS;
2176 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2183 kern_chmod(struct nlookupdata *nd, int mode)
2188 /* XXX Add NLC flag indicating modifying operation? */
2189 if ((error = nlookup(nd)) != 0)
2191 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2193 error = setfmode(vp, mode);
2199 * chmod_args(char *path, int mode)
2201 * Change mode of a file given path name.
2205 chmod(struct chmod_args *uap)
2207 struct nlookupdata nd;
2210 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2212 error = kern_chmod(&nd, uap->mode);
2218 * lchmod_args(char *path, int mode)
2220 * Change mode of a file given path name (don't follow links.)
2224 lchmod(struct lchmod_args *uap)
2226 struct nlookupdata nd;
2229 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2231 error = kern_chmod(&nd, uap->mode);
2237 * fchmod_args(int fd, int mode)
2239 * Change mode of a file given a file descriptor.
2243 fchmod(struct fchmod_args *uap)
2245 struct thread *td = curthread;
2246 struct proc *p = td->td_proc;
2250 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2252 return setfmode((struct vnode *)fp->f_data, uap->mode);
2256 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2258 struct thread *td = curthread;
2259 struct proc *p = td->td_proc;
2264 * note: vget is required for any operation that might mod the vnode
2265 * so VINACTIVE is properly cleared.
2267 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2268 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2272 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2279 kern_chown(struct nlookupdata *nd, int uid, int gid)
2284 /* XXX Add NLC flag indicating modifying operation? */
2285 if ((error = nlookup(nd)) != 0)
2287 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2289 error = setfown(vp, uid, gid);
2295 * chown(char *path, int uid, int gid)
2297 * Set ownership given a path name.
2300 chown(struct chown_args *uap)
2302 struct nlookupdata nd;
2305 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2307 error = kern_chown(&nd, uap->uid, uap->gid);
2313 * lchown_args(char *path, int uid, int gid)
2315 * Set ownership given a path name, do not cross symlinks.
2318 lchown(struct lchown_args *uap)
2320 struct nlookupdata nd;
2323 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2325 error = kern_chown(&nd, uap->uid, uap->gid);
2331 * fchown_args(int fd, int uid, int gid)
2333 * Set ownership given a file descriptor.
2337 fchown(struct fchown_args *uap)
2339 struct thread *td = curthread;
2340 struct proc *p = td->td_proc;
2344 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2346 return setfown((struct vnode *)fp->f_data,
2347 uap->uid, uap->gid);
2351 getutimes(const struct timeval *tvp, struct timespec *tsp)
2353 struct timeval tv[2];
2357 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2360 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2361 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2367 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2369 struct thread *td = curthread;
2370 struct proc *p = td->td_proc;
2375 * note: vget is required for any operation that might mod the vnode
2376 * so VINACTIVE is properly cleared.
2378 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
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 VOP_LEASE(vp, nd->nl_td, nd->nl_cred, LEASE_WRITE);
2514 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, nd->nl_td)) != 0) {
2518 if (vp->v_type == VDIR) {
2520 } else if ((error = vn_writechk(vp)) == 0 &&
2521 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred, nd->nl_td)) == 0) {
2523 vattr.va_size = length;
2524 error = VOP_SETATTR(vp, &vattr, nd->nl_cred, nd->nl_td);
2531 * truncate(char *path, int pad, off_t length)
2533 * Truncate a file given its path name.
2536 truncate(struct truncate_args *uap)
2538 struct nlookupdata nd;
2541 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2543 error = kern_truncate(&nd, uap->length);
2549 kern_ftruncate(int fd, off_t length)
2551 struct thread *td = curthread;
2552 struct proc *p = td->td_proc;
2560 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2562 if ((fp->f_flag & FWRITE) == 0)
2564 vp = (struct vnode *)fp->f_data;
2565 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2566 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2567 if (vp->v_type == VDIR)
2569 else if ((error = vn_writechk(vp)) == 0) {
2571 vattr.va_size = length;
2572 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2574 VOP_UNLOCK(vp, 0, td);
2579 * ftruncate_args(int fd, int pad, off_t length)
2581 * Truncate a file given a file descriptor.
2584 ftruncate(struct ftruncate_args *uap)
2588 error = kern_ftruncate(uap->fd, uap->length);
2596 * Sync an open file.
2600 fsync(struct fsync_args *uap)
2602 struct thread *td = curthread;
2603 struct proc *p = td->td_proc;
2609 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
2611 vp = (struct vnode *)fp->f_data;
2612 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2613 if (VOP_GETVOBJECT(vp, &obj) == 0)
2614 vm_object_page_clean(obj, 0, 0, 0);
2615 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2616 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2618 error = (*bioops.io_fsync)(vp);
2619 VOP_UNLOCK(vp, 0, td);
2624 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2626 struct namecache *fncpd;
2627 struct namecache *tncpd;
2628 struct namecache *ncp;
2633 if ((error = nlookup(fromnd)) != 0)
2635 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2640 * unlock the source ncp so we can lookup the target ncp without
2641 * deadlocking. The target may or may not exist so we do not check
2642 * for a target vp like kern_mkdir() and other creation functions do.
2644 * The source and target directories are ref'd and rechecked after
2645 * everything is relocked to determine if the source or target file
2648 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2649 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2650 cache_unlock(fromnd->nl_ncp);
2652 tond->nl_flags |= NLC_CREATE;
2653 if ((error = nlookup(tond)) != 0) {
2657 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2664 * If the source and target are the same there is nothing to do
2666 if (fromnd->nl_ncp == tond->nl_ncp) {
2673 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
2674 * may have become invalid while it was unlocked, nc_vp and nc_mount
2677 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2678 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2679 } else if (fromnd->nl_ncp > tond->nl_ncp) {
2680 cache_lock(fromnd->nl_ncp);
2681 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2683 cache_unlock(tond->nl_ncp);
2684 cache_lock(fromnd->nl_ncp);
2685 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2686 cache_lock(tond->nl_ncp);
2687 cache_resolve(tond->nl_ncp, tond->nl_cred);
2689 fromnd->nl_flags |= NLC_NCPISLOCKED;
2692 * make sure the parent directories linkages are the same
2694 if (fncpd != fromnd->nl_ncp->nc_parent ||
2695 tncpd != tond->nl_ncp->nc_parent) {
2702 * Both the source and target must be within the same filesystem and
2703 * in the same filesystem as their parent directories within the
2704 * namecache topology.
2706 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
2708 mp = fncpd->nc_mount;
2709 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2710 mp != tond->nl_ncp->nc_mount) {
2717 * If the target exists and either the source or target is a directory,
2718 * then both must be directories.
2720 * Due to relocking of the source, fromnd->nl_ncp->nc_vp might have
2723 if (tond->nl_ncp->nc_vp) {
2724 if (fromnd->nl_ncp->nc_vp == NULL) {
2726 } else if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2727 if (tond->nl_ncp->nc_vp->v_type != VDIR)
2729 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2735 * You cannot rename a source into itself or a subdirectory of itself.
2736 * We check this by travsersing the target directory upwards looking
2737 * for a match against the source.
2740 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2741 if (fromnd->nl_ncp == ncp) {
2752 * Even though the namespaces are different, they may still represent
2753 * hardlinks to the same file. The filesystem might have a hard time
2754 * with this so we issue a NREMOVE of the source instead of a NRENAME
2755 * when we detect the situation.
2758 if (fromnd->nl_ncp->nc_vp == tond->nl_ncp->nc_vp) {
2759 error = VOP_NREMOVE(fromnd->nl_ncp, fromnd->nl_cred);
2761 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp,
2769 * rename_args(char *from, char *to)
2771 * Rename files. Source and destination must either both be directories,
2772 * or both not be directories. If target is a directory, it must be empty.
2775 rename(struct rename_args *uap)
2777 struct nlookupdata fromnd, tond;
2780 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2782 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2784 error = kern_rename(&fromnd, &tond);
2785 nlookup_done(&tond);
2787 nlookup_done(&fromnd);
2792 kern_mkdir(struct nlookupdata *nd, int mode)
2794 struct thread *td = curthread;
2795 struct proc *p = td->td_proc;
2796 struct namecache *ncp;
2802 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2803 if ((error = nlookup(nd)) != 0)
2811 vattr.va_type = VDIR;
2812 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2815 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2822 * mkdir_args(char *path, int mode)
2824 * Make a directory file.
2828 mkdir(struct mkdir_args *uap)
2830 struct nlookupdata nd;
2833 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2835 error = kern_mkdir(&nd, uap->mode);
2841 kern_rmdir(struct nlookupdata *nd)
2843 struct namecache *ncp;
2847 nd->nl_flags |= NLC_DELETE;
2848 if ((error = nlookup(nd)) != 0)
2852 error = VOP_NRMDIR(ncp, nd->nl_cred);
2857 * rmdir_args(char *path)
2859 * Remove a directory file.
2863 rmdir(struct rmdir_args *uap)
2865 struct nlookupdata nd;
2868 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2870 error = kern_rmdir(&nd);
2876 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
2877 enum uio_seg direction)
2879 struct thread *td = curthread;
2880 struct proc *p = td->td_proc;
2888 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2890 if ((fp->f_flag & FREAD) == 0)
2892 vp = (struct vnode *)fp->f_data;
2894 if (vp->v_type != VDIR)
2896 aiov.iov_base = buf;
2897 aiov.iov_len = count;
2898 auio.uio_iov = &aiov;
2899 auio.uio_iovcnt = 1;
2900 auio.uio_rw = UIO_READ;
2901 auio.uio_segflg = direction;
2903 auio.uio_resid = count;
2904 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2905 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2906 loff = auio.uio_offset = fp->f_offset;
2907 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2908 fp->f_offset = auio.uio_offset;
2909 VOP_UNLOCK(vp, 0, td);
2912 if (count == auio.uio_resid) {
2913 if (union_dircheckp) {
2914 error = union_dircheckp(td, &vp, fp);
2920 if ((vp->v_flag & VROOT) &&
2921 (vp->v_mount->mnt_flag & MNT_UNION)) {
2922 struct vnode *tvp = vp;
2923 vp = vp->v_mount->mnt_vnodecovered;
2934 *res = count - auio.uio_resid;
2939 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2941 * Read a block of directory entries in a file system independent format.
2944 getdirentries(struct getdirentries_args *uap)
2949 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2950 &uap->sysmsg_result, UIO_USERSPACE);
2953 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2958 * getdents_args(int fd, char *buf, size_t count)
2961 getdents(struct getdents_args *uap)
2965 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2966 &uap->sysmsg_result, UIO_USERSPACE);
2972 * umask(int newmask)
2974 * Set the mode mask for creation of filesystem nodes.
2979 umask(struct umask_args *uap)
2981 struct thread *td = curthread;
2982 struct proc *p = td->td_proc;
2983 struct filedesc *fdp;
2986 uap->sysmsg_result = fdp->fd_cmask;
2987 fdp->fd_cmask = uap->newmask & ALLPERMS;
2992 * revoke(char *path)
2994 * Void all references to file by ripping underlying filesystem
2999 revoke(struct revoke_args *uap)
3001 struct thread *td = curthread;
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, td);
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);
3036 * Convert a user file descriptor to a kernel file entry.
3039 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
3043 if ((u_int)fd >= fdp->fd_nfiles ||
3044 (fp = fdp->fd_files[fd].fp) == NULL)
3046 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
3052 * getfh_args(char *fname, fhandle_t *fhp)
3054 * Get (NFS) file handle
3057 getfh(struct getfh_args *uap)
3059 struct thread *td = curthread;
3060 struct nlookupdata nd;
3066 * Must be super user
3068 if ((error = suser(td)) != 0)
3072 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3074 error = nlookup(&nd);
3076 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3079 bzero(&fh, sizeof(fh));
3080 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3081 error = VFS_VPTOFH(vp, &fh.fh_fid);
3084 error = copyout(&fh, uap->fhp, sizeof(fh));
3090 * fhopen_args(const struct fhandle *u_fhp, int flags)
3092 * syscall for the rpc.lockd to use to translate a NFS file handle into
3093 * an open descriptor.
3095 * warning: do not remove the suser() call or this becomes one giant
3099 fhopen(struct fhopen_args *uap)
3101 struct thread *td = curthread;
3102 struct proc *p = td->td_proc;
3107 struct vattr *vap = &vat;
3109 struct filedesc *fdp = p->p_fd;
3110 int fmode, mode, error, type;
3116 * Must be super user
3122 fmode = FFLAGS(uap->flags);
3123 /* why not allow a non-read/write open for our lockd? */
3124 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3126 error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
3129 /* find the mount point */
3130 mp = vfs_getvfs(&fhp.fh_fsid);
3133 /* now give me my vnode, it gets returned to me locked */
3134 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3138 * from now on we have to make sure not
3139 * to forget about the vnode
3140 * any error that causes an abort must vput(vp)
3141 * just set error = err and 'goto bad;'.
3147 if (vp->v_type == VLNK) {
3151 if (vp->v_type == VSOCK) {
3156 if (fmode & (FWRITE | O_TRUNC)) {
3157 if (vp->v_type == VDIR) {
3161 error = vn_writechk(vp);
3169 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
3173 if (fmode & O_TRUNC) {
3174 VOP_UNLOCK(vp, 0, td); /* XXX */
3175 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
3176 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
3179 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
3185 * VOP_OPEN needs the file pointer so it can potentially override
3188 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3191 if ((error = falloc(p, &nfp, NULL)) != 0)
3195 fp->f_type = DTYPE_VNODE;
3196 fp->f_flag = fmode & FMASK;
3197 fp->f_ops = &vnode_fileops;
3200 error = VOP_OPEN(vp, fmode, p->p_ucred, fp, td);
3203 * setting f_ops this way prevents VOP_CLOSE from being
3204 * called or fdrop() releasing the vp from v_data. Since
3205 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3207 fp->f_ops = &badfileops;
3216 * The fp now owns a reference on the vnode. We still have our own
3222 * Make sure that a VM object is created for VMIO support. If this
3223 * fails just fdrop() normally to clean up.
3225 if (vn_canvmio(vp) == TRUE) {
3226 if ((error = vfs_object_create(vp, td)) != 0) {
3233 * The open was successful, associate it with a file descriptor.
3235 if ((error = fsetfd(p, fp, &indx)) != 0) {
3242 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3243 lf.l_whence = SEEK_SET;
3246 if (fmode & O_EXLOCK)
3247 lf.l_type = F_WRLCK;
3249 lf.l_type = F_RDLCK;
3251 if ((fmode & FNONBLOCK) == 0)
3253 VOP_UNLOCK(vp, 0, td);
3254 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3256 * lock request failed. Normally close the descriptor
3257 * but handle the case where someone might have dup()d
3258 * or close()d it when we weren't looking.
3260 if (fdp->fd_files[indx].fp == fp) {
3261 funsetfd(fdp, indx);
3266 * release our private reference.
3272 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3273 fp->f_flag |= FHASLOCK;
3275 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
3276 vfs_object_create(vp, td);
3280 uap->sysmsg_result = indx;
3289 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3292 fhstat(struct fhstat_args *uap)
3294 struct thread *td = curthread;
3302 * Must be super user
3308 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
3312 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3314 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3316 error = vn_stat(vp, &sb, td);
3320 error = copyout(&sb, uap->sb, sizeof(sb));
3325 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3328 fhstatfs(struct fhstatfs_args *uap)
3330 struct thread *td = curthread;
3331 struct proc *p = td->td_proc;
3336 char *fullpath, *freepath;
3341 * Must be super user
3343 if ((error = suser(td)))
3346 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
3349 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3352 if (p != NULL && (p->p_fd->fd_nrdir->nc_flag & NCF_ROOT) == 0 &&
3353 !chroot_visible_mnt(mp, p))
3356 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3361 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3364 error = cache_fullpath(p, mp->mnt_ncp, &fullpath, &freepath);
3367 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3368 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
3369 free(freepath, M_TEMP);
3371 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3373 bcopy(sp, &sb, sizeof(sb));
3374 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3377 return (copyout(sp, uap->buf, sizeof(*sp)));
3381 * Syscall to push extended attribute configuration information into the
3382 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3383 * a command (int cmd), and attribute name and misc data. For now, the
3384 * attribute name is left in userspace for consumption by the VFS_op.
3385 * It will probably be changed to be copied into sysspace by the
3386 * syscall in the future, once issues with various consumers of the
3387 * attribute code have raised their hands.
3389 * Currently this is used only by UFS Extended Attributes.
3392 extattrctl(struct extattrctl_args *uap)
3394 struct nlookupdata nd;
3400 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3402 error = nlookup(&nd);
3404 mp = nd.nl_ncp->nc_mount;
3405 error = VFS_EXTATTRCTL(mp, uap->cmd,
3406 uap->attrname, uap->arg,
3414 * Syscall to set a named extended attribute on a file or directory.
3415 * Accepts attribute name, and a uio structure pointing to the data to set.
3416 * The uio is consumed in the style of writev(). The real work happens
3417 * in VOP_SETEXTATTR().
3420 extattr_set_file(struct extattr_set_file_args *uap)
3422 char attrname[EXTATTR_MAXNAMELEN];
3423 struct iovec aiov[UIO_SMALLIOV];
3424 struct iovec *needfree;
3425 struct nlookupdata nd;
3434 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3439 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3441 error = nlookup(&nd);
3443 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3450 iovlen = uap->iovcnt * sizeof(struct iovec);
3451 if (uap->iovcnt > UIO_SMALLIOV) {
3452 if (uap->iovcnt > UIO_MAXIOV) {
3456 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3462 auio.uio_iovcnt = uap->iovcnt;
3463 auio.uio_rw = UIO_WRITE;
3464 auio.uio_segflg = UIO_USERSPACE;
3465 auio.uio_td = nd.nl_td;
3466 auio.uio_offset = 0;
3467 if ((error = copyin(uap->iovp, iov, iovlen)))
3470 for (i = 0; i < uap->iovcnt; i++) {
3471 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3475 auio.uio_resid += iov->iov_len;
3478 cnt = auio.uio_resid;
3479 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td);
3480 cnt -= auio.uio_resid;
3481 uap->sysmsg_result = cnt;
3486 FREE(needfree, M_IOV);
3491 * Syscall to get a named extended attribute on a file or directory.
3492 * Accepts attribute name, and a uio structure pointing to a buffer for the
3493 * data. The uio is consumed in the style of readv(). The real work
3494 * happens in VOP_GETEXTATTR();
3497 extattr_get_file(struct extattr_get_file_args *uap)
3499 char attrname[EXTATTR_MAXNAMELEN];
3500 struct iovec aiov[UIO_SMALLIOV];
3501 struct iovec *needfree;
3502 struct nlookupdata nd;
3511 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3516 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3518 error = nlookup(&nd);
3520 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3526 iovlen = uap->iovcnt * sizeof (struct iovec);
3528 if (uap->iovcnt > UIO_SMALLIOV) {
3529 if (uap->iovcnt > UIO_MAXIOV) {
3533 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3539 auio.uio_iovcnt = uap->iovcnt;
3540 auio.uio_rw = UIO_READ;
3541 auio.uio_segflg = UIO_USERSPACE;
3542 auio.uio_td = nd.nl_td;
3543 auio.uio_offset = 0;
3544 if ((error = copyin(uap->iovp, iov, iovlen)))
3547 for (i = 0; i < uap->iovcnt; i++) {
3548 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3552 auio.uio_resid += iov->iov_len;
3555 cnt = auio.uio_resid;
3556 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td);
3557 cnt -= auio.uio_resid;
3558 uap->sysmsg_result = cnt;
3563 FREE(needfree, M_IOV);
3568 * Syscall to delete a named extended attribute from a file or directory.
3569 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3572 extattr_delete_file(struct extattr_delete_file_args *uap)
3574 char attrname[EXTATTR_MAXNAMELEN];
3575 struct nlookupdata nd;
3579 error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
3584 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3586 error = nlookup(&nd);
3588 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3594 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred, nd.nl_td);
3601 chroot_visible_mnt(struct mount *mp, struct proc *p)
3603 struct namecache *ncp;
3605 * First check if this file system is below
3609 while (ncp != NULL && ncp != p->p_fd->fd_nrdir)
3610 ncp = ncp->nc_parent;
3613 * This is not below the chroot path.
3615 * Check if the chroot path is on the same filesystem,
3616 * by determing if we have to cross a mount point
3617 * before reaching mp->mnt_ncp.
3619 ncp = p->p_fd->fd_nrdir;
3620 while (ncp != NULL && ncp != mp->mnt_ncp) {
3621 if (ncp->nc_flag & NCF_MOUNTPT) {
3625 ncp = ncp->nc_parent;
3628 return(ncp != NULL);