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.52 2004/12/29 02:40:02 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/sysproto.h>
51 #include <sys/filedesc.h>
52 #include <sys/kernel.h>
53 #include <sys/fcntl.h>
55 #include <sys/linker.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
60 #include <sys/namei.h>
61 #include <sys/nlookup.h>
62 #include <sys/dirent.h>
63 #include <sys/extattr.h>
64 #include <sys/kern_syscall.h>
66 #include <machine/limits.h>
67 #include <vfs/union/union.h>
68 #include <sys/sysctl.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_zone.h>
72 #include <vm/vm_page.h>
74 #include <sys/file2.h>
76 static int checkvp_chdir (struct vnode *vn, struct thread *td);
77 static void checkdirs (struct vnode *olddp, struct namecache *ncp);
78 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
79 static int getutimes (const struct timeval *, struct timespec *);
80 static int setfown (struct vnode *, uid_t, gid_t);
81 static int setfmode (struct vnode *, int);
82 static int setfflags (struct vnode *, int);
83 static int setutimes (struct vnode *, const struct timespec *, int);
84 static int usermount = 0; /* if 1, non-root can mount fs. */
86 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
88 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
91 * Virtual File System System Calls
95 * Mount a file system.
98 * mount_args(char *type, char *path, int flags, caddr_t data)
102 mount(struct mount_args *uap)
104 struct thread *td = curthread;
105 struct proc *p = td->td_proc;
107 struct namecache *ncp;
109 struct vfsconf *vfsp;
110 int error, flag = 0, flag2 = 0;
112 struct nlookupdata nd;
113 char fstypename[MFSNAMELEN];
115 struct nlcomponent nlc;
118 if (p->p_ucred->cr_prison != NULL)
120 if (usermount == 0 && (error = suser(td)))
123 * Do not allow NFS export by non-root users.
125 if (SCARG(uap, flags) & MNT_EXPORTED) {
131 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
134 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
137 * Lookup the requested path and extract the ncp and vnode.
139 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
141 if ((error = nlookup(&nd)) == 0) {
142 if (nd.nl_ncp->nc_vp == NULL)
152 * Extract the locked+refd ncp and cleanup the nd structure
159 * now we have the locked ref'd ncp and unreferenced vnode.
162 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
169 * Now we have an unlocked ref'd ncp and a locked ref'd vp
171 if (SCARG(uap, flags) & MNT_UPDATE) {
172 if ((vp->v_flag & VROOT) == 0) {
179 flag2 = mp->mnt_kern_flag;
181 * We only allow the filesystem to be reloaded if it
182 * is currently mounted read-only.
184 if ((SCARG(uap, flags) & MNT_RELOAD) &&
185 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
188 return (EOPNOTSUPP); /* Needs translation */
191 * Only root, or the user that did the original mount is
192 * permitted to update it.
194 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
195 (error = suser(td))) {
200 if (vfs_busy(mp, LK_NOWAIT, NULL, td)) {
205 if ((vp->v_flag & VMOUNT) != 0 ||
206 vp->v_mountedhere != NULL) {
212 vp->v_flag |= VMOUNT;
214 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
215 VOP_UNLOCK(vp, 0, td);
219 * If the user is not root, ensure that they own the directory
220 * onto which we are attempting to mount.
222 if ((error = VOP_GETATTR(vp, &va, td)) ||
223 (va.va_uid != p->p_ucred->cr_uid &&
224 (error = suser(td)))) {
229 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
234 if (vp->v_type != VDIR) {
239 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
244 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
245 if (!strcmp(vfsp->vfc_name, fstypename))
251 /* Only load modules for root (very important!) */
252 if ((error = suser(td)) != 0) {
257 error = linker_load_file(fstypename, &lf);
258 if (error || lf == NULL) {
266 /* lookup again, see if the VFS was loaded */
267 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
268 if (!strcmp(vfsp->vfc_name, fstypename))
273 linker_file_unload(lf);
279 if ((vp->v_flag & VMOUNT) != 0 ||
280 vp->v_mountedhere != NULL) {
285 vp->v_flag |= VMOUNT;
288 * Allocate and initialize the filesystem.
290 mp = malloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
291 TAILQ_INIT(&mp->mnt_nvnodelist);
292 TAILQ_INIT(&mp->mnt_reservedvnlist);
293 TAILQ_INIT(&mp->mnt_jlist);
294 mp->mnt_nvnodelistsize = 0;
295 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
296 vfs_busy(mp, LK_NOWAIT, NULL, td);
297 mp->mnt_op = vfsp->vfc_vfsops;
299 vfsp->vfc_refcount++;
300 mp->mnt_stat.f_type = vfsp->vfc_typenum;
301 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
302 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
303 mp->mnt_vnodecovered = vp;
304 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
305 mp->mnt_iosize_max = DFLTPHYS;
306 VOP_UNLOCK(vp, 0, td);
309 * Set the mount level flags.
311 if (SCARG(uap, flags) & MNT_RDONLY)
312 mp->mnt_flag |= MNT_RDONLY;
313 else if (mp->mnt_flag & MNT_RDONLY)
314 mp->mnt_kern_flag |= MNTK_WANTRDWR;
315 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
316 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
317 MNT_NOSYMFOLLOW | MNT_IGNORE |
318 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
319 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
320 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
321 MNT_NOSYMFOLLOW | MNT_IGNORE |
322 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
324 * Mount the filesystem.
325 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
328 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), td);
329 if (mp->mnt_flag & MNT_UPDATE) {
330 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
331 mp->mnt_flag &= ~MNT_RDONLY;
332 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
333 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
336 mp->mnt_kern_flag = flag2;
339 vp->v_flag &= ~VMOUNT;
344 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
346 * Put the new filesystem on the mount list after root. The mount
347 * point gets its own mnt_ncp which is a special ncp linking the
348 * vnode-under to the root of the new mount. The lookup code
349 * detects the mount point going forward and detects the special
350 * mnt_ncp via NCP_MOUNTPT going backwards.
352 * It is not necessary to invalidate or purge the vnode underneath
353 * because elements under the mount will be given their own glue
357 nlc.nlc_nameptr = "";
359 mp->mnt_ncp = cache_nlookup(ncp, &nlc);
360 mp->mnt_ncp->nc_flag |= NCF_MOUNTPT;
361 mp->mnt_ncp->nc_mount = mp;
363 /* XXX get the root of the fs and cache_setvp(mnt_ncp...) */
364 vp->v_flag &= ~VMOUNT;
365 vp->v_mountedhere = mp;
366 lwkt_gettoken(&ilock, &mountlist_token);
367 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
368 lwkt_reltoken(&ilock);
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, SCARG(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, SCARG(uap, flags), td));
509 * Do the actual file system unmount.
512 dounmount(struct mount *mp, int flags, struct thread *td)
514 struct vnode *coveredvp;
519 lwkt_gettoken(&ilock, &mountlist_token);
520 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
521 lwkt_reltoken(&ilock);
524 mp->mnt_kern_flag |= MNTK_UNMOUNT;
525 /* Allow filesystems to detect that a forced unmount is in progress. */
526 if (flags & MNT_FORCE)
527 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
528 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
529 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
531 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
532 if (mp->mnt_kern_flag & MNTK_MWAIT)
537 if (mp->mnt_flag & MNT_EXPUBLIC)
538 vfs_setpublicfs(NULL, NULL, NULL);
540 vfs_msync(mp, MNT_WAIT);
541 async_flag = mp->mnt_flag & MNT_ASYNC;
542 mp->mnt_flag &=~ MNT_ASYNC;
543 cache_purgevfs(mp); /* remove cache entries for this file sys */
544 if (mp->mnt_syncer != NULL)
545 vrele(mp->mnt_syncer);
546 if (((mp->mnt_flag & MNT_RDONLY) ||
547 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
549 error = VFS_UNMOUNT(mp, flags, td);
550 lwkt_gettokref(&ilock);
552 if (mp->mnt_syncer == NULL)
553 vfs_allocate_syncvnode(mp);
554 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
555 mp->mnt_flag |= async_flag;
556 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
558 if (mp->mnt_kern_flag & MNTK_MWAIT)
562 TAILQ_REMOVE(&mountlist, mp, mnt_list);
565 * Remove any installed vnode ops here so the individual VFSs don't
568 vfs_rm_vnodeops(&mp->mnt_vn_coherency_ops);
569 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
570 vfs_rm_vnodeops(&mp->mnt_vn_norm_ops);
571 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
572 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
574 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
575 coveredvp->v_mountedhere = NULL;
577 cache_drop(mp->mnt_ncp);
580 mp->mnt_vfc->vfc_refcount--;
581 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
582 panic("unmount: dangling vnode");
583 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
584 if (mp->mnt_kern_flag & MNTK_MWAIT)
591 * Sync each mounted filesystem.
595 static int syncprt = 0;
596 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
601 sync(struct sync_args *uap)
603 struct thread *td = curthread;
604 struct mount *mp, *nmp;
608 lwkt_gettoken(&ilock, &mountlist_token);
609 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
610 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
611 nmp = TAILQ_NEXT(mp, mnt_list);
614 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
615 asyncflag = mp->mnt_flag & MNT_ASYNC;
616 mp->mnt_flag &= ~MNT_ASYNC;
617 vfs_msync(mp, MNT_NOWAIT);
618 VFS_SYNC(mp, MNT_NOWAIT, td);
619 mp->mnt_flag |= asyncflag;
621 lwkt_gettokref(&ilock);
622 nmp = TAILQ_NEXT(mp, mnt_list);
625 lwkt_reltoken(&ilock);
627 * print out buffer pool stat information on each sync() call.
636 /* XXX PRISON: could be per prison flag */
637 static int prison_quotas;
639 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
643 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
645 * Change filesystem quotas.
649 quotactl(struct quotactl_args *uap)
651 struct nlookupdata nd;
659 if (p->p_ucred->cr_prison && !prison_quotas)
662 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
664 error = nlookup(&nd);
666 mp = nd.nl_ncp->nc_mount;
667 error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
668 SCARG(uap, arg), nd.nl_td);
675 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
676 * void *buf, int buflen)
678 * This function operates on a mount point and executes the specified
679 * operation using the specified control data, and possibly returns data.
681 * The actual number of bytes stored in the result buffer is returned, 0
682 * if none, otherwise an error is returned.
686 mountctl(struct mountctl_args *uap)
688 struct thread *td = curthread;
689 struct proc *p = td->td_proc;
690 struct filedesc *fdp = p->p_fd;
698 * Sanity and permissions checks. We must be root.
701 if (p->p_ucred->cr_prison != NULL)
703 if ((error = suser(td)) != 0)
707 * Argument length checks
709 if (uap->ctllen < 0 || uap->ctllen > MAXPATHLEN)
711 if (uap->buflen < 0 || uap->buflen > MAXPATHLEN)
713 if (uap->path == NULL)
717 * Allocate the necessary buffers and copyin data
719 path = zalloc(namei_zone);
720 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
725 ctl = malloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
726 error = copyin(uap->ctl, ctl, uap->ctllen);
731 buf = malloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
734 * Validate the descriptor
738 } else if ((u_int)uap->fd >= fdp->fd_nfiles ||
739 (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
747 * Execute the internal kernel function and clean up.
749 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
752 if (error == 0 && uap->sysmsg_result > 0)
753 error = copyout(buf, uap->buf, uap->sysmsg_result);
756 zfree(namei_zone, path);
765 * Execute a mount control operation by resolving the path to a mount point
766 * and calling vop_mountctl().
769 kern_mountctl(const char *path, int op, struct file *fp,
770 const void *ctl, int ctllen,
771 void *buf, int buflen, int *res)
775 struct nlookupdata nd;
780 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
782 error = nlookup(&nd);
784 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
792 * Must be the root of the filesystem
794 if ((vp->v_flag & VROOT) == 0) {
798 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
805 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
807 struct thread *td = curthread;
812 if ((error = nlookup(nd)) != 0)
814 mp = nd->nl_ncp->nc_mount;
816 if ((error = VFS_STATFS(mp, sp, td)) != 0)
818 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
819 bcopy(sp, buf, sizeof(*buf));
820 /* Only root should have access to the fsid's. */
822 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
827 * statfs_args(char *path, struct statfs *buf)
829 * Get filesystem statistics.
832 statfs(struct statfs_args *uap)
834 struct nlookupdata nd;
838 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
840 error = kern_statfs(&nd, &buf);
843 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
848 kern_fstatfs(int fd, struct statfs *buf)
850 struct thread *td = curthread;
851 struct proc *p = td->td_proc;
858 error = getvnode(p->p_fd, fd, &fp);
861 mp = ((struct vnode *)fp->f_data)->v_mount;
865 error = VFS_STATFS(mp, sp, td);
868 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
869 bcopy(sp, buf, sizeof(*buf));
870 /* Only root should have access to the fsid's. */
872 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
877 * fstatfs_args(int fd, struct statfs *buf)
879 * Get filesystem statistics.
882 fstatfs(struct fstatfs_args *uap)
887 error = kern_fstatfs(uap->fd, &buf);
890 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
895 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
897 * Get statistics on all filesystems.
901 getfsstat(struct getfsstat_args *uap)
903 struct thread *td = curthread;
904 struct mount *mp, *nmp;
908 long count, maxcount, error;
910 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
911 sfsp = (caddr_t)SCARG(uap, buf);
913 lwkt_gettoken(&ilock, &mountlist_token);
914 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
915 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
916 nmp = TAILQ_NEXT(mp, mnt_list);
919 if (sfsp && count < maxcount) {
922 * If MNT_NOWAIT or MNT_LAZY is specified, do not
923 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
924 * overrides MNT_WAIT.
926 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
927 (SCARG(uap, flags) & MNT_WAIT)) &&
928 (error = VFS_STATFS(mp, sp, td))) {
929 lwkt_gettokref(&ilock);
930 nmp = TAILQ_NEXT(mp, mnt_list);
934 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
935 error = copyout(sp, sfsp, sizeof(*sp));
943 lwkt_gettokref(&ilock);
944 nmp = TAILQ_NEXT(mp, mnt_list);
947 lwkt_reltoken(&ilock);
948 if (sfsp && count > maxcount)
949 uap->sysmsg_result = maxcount;
951 uap->sysmsg_result = count;
956 * fchdir_args(int fd)
958 * Change current working directory to a given file descriptor.
962 fchdir(struct fchdir_args *uap)
964 struct thread *td = curthread;
965 struct proc *p = td->td_proc;
966 struct filedesc *fdp = p->p_fd;
967 struct vnode *vp, *ovp;
970 struct namecache *ncp, *oncp;
971 struct namecache *nct;
974 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
976 vp = (struct vnode *)fp->f_data;
978 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
979 if (vp->v_type != VDIR || fp->f_ncp == NULL)
982 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
987 ncp = cache_hold(fp->f_ncp);
988 while (!error && (mp = vp->v_mountedhere) != NULL) {
989 error = nlookup_mp(mp, &nct);
991 cache_unlock(nct); /* leave ref intact */
994 error = vget(vp, LK_SHARED, td);
995 KKASSERT(error == 0);
1002 oncp = fdp->fd_ncdir;
1003 VOP_UNLOCK(vp, 0, td); /* leave ref intact */
1005 fdp->fd_ncdir = ncp;
1016 kern_chdir(struct nlookupdata *nd)
1018 struct thread *td = curthread;
1019 struct proc *p = td->td_proc;
1020 struct filedesc *fdp = p->p_fd;
1021 struct vnode *vp, *ovp;
1022 struct namecache *oncp;
1025 if ((error = nlookup(nd)) != 0)
1027 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1029 if ((error = vget(vp, LK_SHARED, td)) != 0)
1032 error = checkvp_chdir(vp, td);
1033 VOP_UNLOCK(vp, 0, td);
1036 oncp = fdp->fd_ncdir;
1037 cache_unlock(nd->nl_ncp); /* leave reference intact */
1038 fdp->fd_ncdir = nd->nl_ncp;
1050 * chdir_args(char *path)
1052 * Change current working directory (``.'').
1055 chdir(struct chdir_args *uap)
1057 struct nlookupdata nd;
1060 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1062 error = kern_chdir(&nd);
1068 * Helper function for raised chroot(2) security function: Refuse if
1069 * any filedescriptors are open directories.
1072 chroot_refuse_vdir_fds(fdp)
1073 struct filedesc *fdp;
1080 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1081 error = getvnode(fdp, fd, &fp);
1084 vp = (struct vnode *)fp->f_data;
1085 if (vp->v_type != VDIR)
1093 * This sysctl determines if we will allow a process to chroot(2) if it
1094 * has a directory open:
1095 * 0: disallowed for all processes.
1096 * 1: allowed for processes that were not already chroot(2)'ed.
1097 * 2: allowed for all processes.
1100 static int chroot_allow_open_directories = 1;
1102 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1103 &chroot_allow_open_directories, 0, "");
1106 * chroot to the specified namecache entry. We obtain the vp from the
1107 * namecache data. The passed ncp must be locked and referenced and will
1108 * remain locked and referenced on return.
1111 kern_chroot(struct nlookupdata *nd)
1113 struct thread *td = curthread;
1114 struct proc *p = td->td_proc;
1115 struct filedesc *fdp = p->p_fd;
1116 struct namecache *ncp;
1121 * Only root can chroot
1123 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1126 if ((error = nlookup(nd)) != 0)
1131 * Disallow open directory descriptors (fchdir() breakouts).
1133 if (chroot_allow_open_directories == 0 ||
1134 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1135 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1138 if ((vp = ncp->nc_vp) == NULL)
1141 if ((error = vget(vp, LK_SHARED, td)) != 0)
1145 * Check the validity of vp as a directory to change to and
1146 * associate it with rdir/jdir.
1148 error = checkvp_chdir(vp, td);
1149 VOP_UNLOCK(vp, 0, td); /* leave reference intact */
1151 vrele(fdp->fd_rdir);
1152 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1153 cache_drop(fdp->fd_nrdir);
1154 fdp->fd_nrdir = cache_hold(ncp);
1155 if (fdp->fd_jdir == NULL) {
1158 fdp->fd_njdir = cache_hold(ncp);
1167 * chroot_args(char *path)
1169 * Change notion of root (``/'') directory.
1173 chroot(struct chroot_args *uap)
1175 struct thread *td = curthread;
1176 struct nlookupdata nd;
1179 KKASSERT(td->td_proc);
1180 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1182 error = kern_chroot(&nd);
1188 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1189 * determine whether it is legal to chdir to the vnode. The vnode's state
1190 * is not changed by this call.
1193 checkvp_chdir(struct vnode *vp, struct thread *td)
1197 if (vp->v_type != VDIR)
1200 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
1205 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1207 struct thread *td = curthread;
1208 struct proc *p = td->td_proc;
1209 struct filedesc *fdp = p->p_fd;
1214 int type, indx, error;
1217 if ((oflags & O_ACCMODE) == O_ACCMODE)
1219 flags = FFLAGS(oflags);
1220 error = falloc(p, &nfp, NULL);
1224 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1227 * XXX p_dupfd is a real mess. It allows a device to return a
1228 * file descriptor to be duplicated rather then doing the open
1234 * Call vn_open() to do the lookup and assign the vnode to the
1235 * file pointer. vn_open() does not change the ref count on fp
1236 * and the vnode, on success, will be inherited by the file pointer
1239 nd->nl_flags |= NLC_LOCKVP;
1240 error = vn_open(nd, fp, flags, cmode);
1244 * handle special fdopen() case. bleh. dupfdopen() is
1245 * responsible for dropping the old contents of ofiles[indx]
1248 * Note that if fsetfd() succeeds it will add a ref to fp
1249 * which represents the fd_ofiles[] assignment. We must still
1250 * drop our reference.
1252 if ((error == ENODEV || error == ENXIO) && p->p_dupfd >= 0) {
1253 if (fsetfd(p, fp, &indx) == 0) {
1254 error = dupfdopen(fdp, indx, p->p_dupfd, flags, error);
1257 fdrop(fp, td); /* our ref */
1260 if (fdp->fd_ofiles[indx] == fp) {
1261 fdp->fd_ofiles[indx] = NULL;
1262 fdrop(fp, td); /* fd_ofiles[] ref */
1266 fdrop(fp, td); /* our ref */
1267 if (error == ERESTART)
1273 * ref the vnode for ourselves so it can't be ripped out from under
1274 * is. XXX need an ND flag to request that the vnode be returned
1277 vp = (struct vnode *)fp->f_data;
1279 if ((error = fsetfd(p, fp, &indx)) != 0) {
1286 * If no error occurs the vp will have been assigned to the file
1292 * There should be 2 references on the file, one from the descriptor
1293 * table, and one for us.
1295 * Handle the case where someone closed the file (via its file
1296 * descriptor) while we were blocked. The end result should look
1297 * like opening the file succeeded but it was immediately closed.
1299 if (fp->f_count == 1) {
1300 KASSERT(fdp->fd_ofiles[indx] != fp,
1301 ("Open file descriptor lost all refs"));
1309 if (flags & (O_EXLOCK | O_SHLOCK)) {
1310 lf.l_whence = SEEK_SET;
1313 if (flags & O_EXLOCK)
1314 lf.l_type = F_WRLCK;
1316 lf.l_type = F_RDLCK;
1318 if ((flags & FNONBLOCK) == 0)
1321 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1323 * lock request failed. Normally close the descriptor
1324 * but handle the case where someone might have dup()d
1325 * it when we weren't looking. One reference is
1326 * owned by the descriptor array, the other by us.
1329 if (fdp->fd_ofiles[indx] == fp) {
1330 fdp->fd_ofiles[indx] = NULL;
1336 fp->f_flag |= FHASLOCK;
1338 /* assert that vn_open created a backing object if one is needed */
1339 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1340 ("open: vmio vnode has no backing object after vn_open"));
1345 * release our private reference, leaving the one associated with the
1346 * descriptor table intact.
1354 * open_args(char *path, int flags, int mode)
1356 * Check permissions, allocate an open file structure,
1357 * and call the device open routine if any.
1360 open(struct open_args *uap)
1362 struct nlookupdata nd;
1365 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1367 error = kern_open(&nd, uap->flags,
1368 uap->mode, &uap->sysmsg_result);
1375 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1377 struct namecache *ncp;
1378 struct thread *td = curthread;
1379 struct proc *p = td->td_proc;
1387 switch (mode & S_IFMT) {
1393 error = suser_cred(p->p_ucred, PRISON_ROOT);
1400 nd->nl_flags |= NLC_CREATE;
1401 if ((error = nlookup(nd)) != 0)
1408 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1409 vattr.va_rdev = dev;
1412 switch (mode & S_IFMT) {
1413 case S_IFMT: /* used by badsect to flag bad sectors */
1414 vattr.va_type = VBAD;
1417 vattr.va_type = VCHR;
1420 vattr.va_type = VBLK;
1431 error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1434 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1443 * mknod_args(char *path, int mode, int dev)
1445 * Create a special file.
1448 mknod(struct mknod_args *uap)
1450 struct nlookupdata nd;
1453 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1455 error = kern_mknod(&nd, uap->mode, uap->dev);
1461 kern_mkfifo(struct nlookupdata *nd, int mode)
1463 struct namecache *ncp;
1464 struct thread *td = curthread;
1465 struct proc *p = td->td_proc;
1472 nd->nl_flags |= NLC_CREATE;
1473 if ((error = nlookup(nd)) != 0)
1480 vattr.va_type = VFIFO;
1481 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1483 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1490 * mkfifo_args(char *path, int mode)
1492 * Create a named pipe.
1495 mkfifo(struct mkfifo_args *uap)
1497 struct nlookupdata nd;
1500 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1502 error = kern_mkfifo(&nd, uap->mode);
1508 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1510 struct thread *td = curthread;
1515 * Lookup the source and obtained a locked vnode.
1517 * XXX relookup on vget failure / race ?
1520 if ((error = nlookup(nd)) != 0)
1522 vp = nd->nl_ncp->nc_vp;
1523 KKASSERT(vp != NULL);
1524 if (vp->v_type == VDIR)
1525 return (EPERM); /* POSIX */
1526 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0)
1530 * Unlock the source so we can lookup the target without deadlocking
1531 * (XXX vp is locked already, possible other deadlock?). The target
1534 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1535 nd->nl_flags &= ~NLC_NCPISLOCKED;
1536 cache_unlock(nd->nl_ncp);
1538 linknd->nl_flags |= NLC_CREATE;
1539 if ((error = nlookup(linknd)) != 0) {
1543 if (linknd->nl_ncp->nc_vp) {
1549 * Finally run the new API VOP.
1551 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
1557 * link_args(char *path, char *link)
1559 * Make a hard file link.
1562 link(struct link_args *uap)
1564 struct nlookupdata nd, linknd;
1567 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1569 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1571 error = kern_link(&nd, &linknd);
1572 nlookup_done(&linknd);
1579 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1581 struct namecache *ncp;
1587 nd->nl_flags |= NLC_CREATE;
1588 if ((error = nlookup(nd)) != 0)
1595 vattr.va_mode = mode;
1596 error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
1603 * symlink(char *path, char *link)
1605 * Make a symbolic link.
1608 symlink(struct symlink_args *uap)
1610 struct thread *td = curthread;
1611 struct nlookupdata nd;
1616 path = zalloc(namei_zone);
1617 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1619 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1621 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1622 error = kern_symlink(&nd, path, mode);
1626 zfree(namei_zone, path);
1631 * undelete_args(char *path)
1633 * Delete a whiteout from the filesystem.
1637 undelete(struct undelete_args *uap)
1639 struct nlookupdata nd;
1642 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0);
1644 nd.nl_flags |= NLC_DELETE;
1646 error = nlookup(&nd);
1648 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1654 kern_unlink(struct nlookupdata *nd)
1656 struct namecache *ncp;
1660 nd->nl_flags |= NLC_DELETE;
1661 if ((error = nlookup(nd)) != 0)
1664 error = VOP_NREMOVE(ncp, nd->nl_cred);
1669 * unlink_args(char *path)
1671 * Delete a name from the filesystem.
1674 unlink(struct unlink_args *uap)
1676 struct nlookupdata nd;
1679 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1681 error = kern_unlink(&nd);
1687 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1689 struct thread *td = curthread;
1690 struct proc *p = td->td_proc;
1691 struct filedesc *fdp = p->p_fd;
1696 if ((u_int)fd >= fdp->fd_nfiles ||
1697 (fp = fdp->fd_ofiles[fd]) == NULL)
1699 if (fp->f_type != DTYPE_VNODE)
1703 fp->f_offset += offset;
1706 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1709 fp->f_offset = offset + vattr.va_size;
1712 fp->f_offset = offset;
1717 *res = fp->f_offset;
1722 * lseek_args(int fd, int pad, off_t offset, int whence)
1724 * Reposition read/write file offset.
1727 lseek(struct lseek_args *uap)
1731 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1732 &uap->sysmsg_offset);
1738 kern_access(struct nlookupdata *nd, int aflags)
1740 struct thread *td = curthread;
1744 if ((error = nlookup(nd)) != 0)
1747 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1751 /* Flags == 0 means only check for existence. */
1760 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1761 error = VOP_ACCESS(vp, flags, nd->nl_cred, td);
1764 * If the file handle is stale we have to re-resolve the
1765 * entry. This is a hack at the moment.
1767 if (error == ESTALE) {
1768 cache_setunresolved(nd->nl_ncp);
1769 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1782 * access_args(char *path, int flags)
1784 * Check access permissions.
1787 access(struct access_args *uap)
1789 struct nlookupdata nd;
1792 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1794 error = kern_access(&nd, uap->flags);
1800 kern_stat(struct nlookupdata *nd, struct stat *st)
1806 if ((error = nlookup(nd)) != 0)
1809 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1813 if ((error = vget(vp, LK_SHARED, td)) != 0)
1815 error = vn_stat(vp, st, td);
1818 * If the file handle is stale we have to re-resolve the entry. This
1819 * is a hack at the moment.
1821 if (error == ESTALE) {
1822 cache_setunresolved(nd->nl_ncp);
1823 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1834 * stat_args(char *path, struct stat *ub)
1836 * Get file status; this version follows links.
1839 stat(struct stat_args *uap)
1841 struct nlookupdata nd;
1845 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1847 error = kern_stat(&nd, &st);
1849 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1856 * lstat_args(char *path, struct stat *ub)
1858 * Get file status; this version does not follow links.
1861 lstat(struct lstat_args *uap)
1863 struct nlookupdata nd;
1867 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1869 error = kern_stat(&nd, &st);
1871 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1882 nsb->st_dev = sb->st_dev;
1883 nsb->st_ino = sb->st_ino;
1884 nsb->st_mode = sb->st_mode;
1885 nsb->st_nlink = sb->st_nlink;
1886 nsb->st_uid = sb->st_uid;
1887 nsb->st_gid = sb->st_gid;
1888 nsb->st_rdev = sb->st_rdev;
1889 nsb->st_atimespec = sb->st_atimespec;
1890 nsb->st_mtimespec = sb->st_mtimespec;
1891 nsb->st_ctimespec = sb->st_ctimespec;
1892 nsb->st_size = sb->st_size;
1893 nsb->st_blocks = sb->st_blocks;
1894 nsb->st_blksize = sb->st_blksize;
1895 nsb->st_flags = sb->st_flags;
1896 nsb->st_gen = sb->st_gen;
1897 nsb->st_qspare[0] = sb->st_qspare[0];
1898 nsb->st_qspare[1] = sb->st_qspare[1];
1902 * nstat_args(char *path, struct nstat *ub)
1906 nstat(struct nstat_args *uap)
1908 struct thread *td = curthread;
1912 struct nlookupdata nd;
1916 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
1918 error = nlookup(&nd);
1920 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
1923 error = vn_stat(vp, &sb, td);
1926 cvtnstat(&sb, &nsb);
1927 error = copyout(&nsb, SCARG(uap, ub), sizeof(nsb));
1934 * lstat_args(char *path, struct stat *ub)
1936 * Get file status; this version does not follow links.
1940 nlstat(struct nlstat_args *uap)
1942 struct thread *td = curthread;
1946 struct nlookupdata nd;
1950 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0);
1952 error = nlookup(&nd);
1954 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
1957 error = vn_stat(vp, &sb, td);
1960 cvtnstat(&sb, &nsb);
1961 error = copyout(&nsb, SCARG(uap, ub), sizeof(nsb));
1968 * pathconf_Args(char *path, int name)
1970 * Get configurable pathname variables.
1974 pathconf(struct pathconf_args *uap)
1976 struct nlookupdata nd;
1981 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
1983 error = nlookup(&nd);
1985 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
1988 error = VOP_PATHCONF(vp, SCARG(uap, name), uap->sysmsg_fds);
1996 * kern_readlink isn't properly split yet. There is a copyin burried
1997 * in VOP_READLINK().
2000 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2002 struct thread *td = curthread;
2003 struct proc *p = td->td_proc;
2009 if ((error = nlookup(nd)) != 0)
2011 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
2014 if (vp->v_type != VLNK) {
2017 aiov.iov_base = buf;
2018 aiov.iov_len = count;
2019 auio.uio_iov = &aiov;
2020 auio.uio_iovcnt = 1;
2021 auio.uio_offset = 0;
2022 auio.uio_rw = UIO_READ;
2023 auio.uio_segflg = UIO_USERSPACE;
2025 auio.uio_resid = count;
2026 error = VOP_READLINK(vp, &auio, p->p_ucred);
2029 *res = count - auio.uio_resid;
2034 * readlink_args(char *path, char *buf, int count)
2036 * Return target name of a symbolic link.
2039 readlink(struct readlink_args *uap)
2041 struct nlookupdata nd;
2044 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2046 error = kern_readlink(&nd, uap->buf, uap->count,
2047 &uap->sysmsg_result);
2054 setfflags(struct vnode *vp, int flags)
2056 struct thread *td = curthread;
2057 struct proc *p = td->td_proc;
2062 * Prevent non-root users from setting flags on devices. When
2063 * a device is reused, users can retain ownership of the device
2064 * if they are allowed to set flags and programs assume that
2065 * chown can't fail when done as root.
2067 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2068 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2072 * note: vget is required for any operation that might mod the vnode
2073 * so VINACTIVE is properly cleared.
2075 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2076 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2078 vattr.va_flags = flags;
2079 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2086 * chflags(char *path, int flags)
2088 * Change flags of a file given a path name.
2092 chflags(struct chflags_args *uap)
2094 struct nlookupdata nd;
2099 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
2100 /* XXX Add NLC flag indicating modifying operation? */
2102 error = nlookup(&nd);
2104 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2107 error = setfflags(vp, SCARG(uap, flags));
2114 * fchflags_args(int fd, int flags)
2116 * Change flags of a file given a file descriptor.
2120 fchflags(struct fchflags_args *uap)
2122 struct thread *td = curthread;
2123 struct proc *p = td->td_proc;
2127 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2129 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
2133 setfmode(struct vnode *vp, int mode)
2135 struct thread *td = curthread;
2136 struct proc *p = td->td_proc;
2141 * note: vget is required for any operation that might mod the vnode
2142 * so VINACTIVE is properly cleared.
2144 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2145 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2147 vattr.va_mode = mode & ALLPERMS;
2148 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2155 kern_chmod(struct nlookupdata *nd, int mode)
2160 /* XXX Add NLC flag indicating modifying operation? */
2161 if ((error = nlookup(nd)) != 0)
2163 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2165 error = setfmode(vp, mode);
2171 * chmod_args(char *path, int mode)
2173 * Change mode of a file given path name.
2177 chmod(struct chmod_args *uap)
2179 struct nlookupdata nd;
2182 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2184 error = kern_chmod(&nd, uap->mode);
2190 * lchmod_args(char *path, int mode)
2192 * Change mode of a file given path name (don't follow links.)
2196 lchmod(struct lchmod_args *uap)
2198 struct nlookupdata nd;
2201 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2203 error = kern_chmod(&nd, uap->mode);
2209 * fchmod_args(int fd, int mode)
2211 * Change mode of a file given a file descriptor.
2215 fchmod(struct fchmod_args *uap)
2217 struct thread *td = curthread;
2218 struct proc *p = td->td_proc;
2222 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2224 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
2228 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2230 struct thread *td = curthread;
2231 struct proc *p = td->td_proc;
2236 * note: vget is required for any operation that might mod the vnode
2237 * so VINACTIVE is properly cleared.
2239 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2240 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2244 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2251 kern_chown(struct nlookupdata *nd, int uid, int gid)
2256 /* XXX Add NLC flag indicating modifying operation? */
2257 if ((error = nlookup(nd)) != 0)
2259 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2261 error = setfown(vp, uid, gid);
2267 * chown(char *path, int uid, int gid)
2269 * Set ownership given a path name.
2272 chown(struct chown_args *uap)
2274 struct nlookupdata nd;
2277 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2279 error = kern_chown(&nd, uap->uid, uap->gid);
2285 * lchown_args(char *path, int uid, int gid)
2287 * Set ownership given a path name, do not cross symlinks.
2290 lchown(struct lchown_args *uap)
2292 struct nlookupdata nd;
2295 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2297 error = kern_chown(&nd, uap->uid, uap->gid);
2303 * fchown_args(int fd, int uid, int gid)
2305 * Set ownership given a file descriptor.
2309 fchown(struct fchown_args *uap)
2311 struct thread *td = curthread;
2312 struct proc *p = td->td_proc;
2316 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2318 return setfown((struct vnode *)fp->f_data,
2319 SCARG(uap, uid), SCARG(uap, gid));
2323 getutimes(const struct timeval *tvp, struct timespec *tsp)
2325 struct timeval tv[2];
2329 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2332 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2333 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2339 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2341 struct thread *td = curthread;
2342 struct proc *p = td->td_proc;
2347 * note: vget is required for any operation that might mod the vnode
2348 * so VINACTIVE is properly cleared.
2350 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2351 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2353 vattr.va_atime = ts[0];
2354 vattr.va_mtime = ts[1];
2356 vattr.va_vaflags |= VA_UTIMES_NULL;
2357 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2364 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2366 struct timespec ts[2];
2370 if ((error = getutimes(tptr, ts)) != 0)
2372 /* XXX Add NLC flag indicating modifying operation? */
2373 if ((error = nlookup(nd)) != 0)
2375 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2377 error = setutimes(vp, ts, tptr == NULL);
2383 * utimes_args(char *path, struct timeval *tptr)
2385 * Set the access and modification times of a file.
2388 utimes(struct utimes_args *uap)
2390 struct timeval tv[2];
2391 struct nlookupdata nd;
2395 error = copyin(uap->tptr, tv, sizeof(tv));
2399 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2401 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2407 * lutimes_args(char *path, struct timeval *tptr)
2409 * Set the access and modification times of a file.
2412 lutimes(struct lutimes_args *uap)
2414 struct timeval tv[2];
2415 struct nlookupdata nd;
2419 error = copyin(uap->tptr, tv, sizeof(tv));
2423 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2425 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2431 kern_futimes(int fd, struct timeval *tptr)
2433 struct thread *td = curthread;
2434 struct proc *p = td->td_proc;
2435 struct timespec ts[2];
2439 error = getutimes(tptr, ts);
2442 error = getvnode(p->p_fd, fd, &fp);
2445 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2450 * futimes_args(int fd, struct timeval *tptr)
2452 * Set the access and modification times of a file.
2455 futimes(struct futimes_args *uap)
2457 struct timeval tv[2];
2461 error = copyin(uap->tptr, tv, sizeof(tv));
2466 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2472 kern_truncate(struct nlookupdata *nd, off_t length)
2480 /* XXX Add NLC flag indicating modifying operation? */
2481 if ((error = nlookup(nd)) != 0)
2483 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2485 VOP_LEASE(vp, nd->nl_td, nd->nl_cred, LEASE_WRITE);
2486 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, nd->nl_td)) != 0) {
2490 if (vp->v_type == VDIR) {
2492 } else if ((error = vn_writechk(vp)) == 0 &&
2493 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred, nd->nl_td)) == 0) {
2495 vattr.va_size = length;
2496 error = VOP_SETATTR(vp, &vattr, nd->nl_cred, nd->nl_td);
2503 * truncate(char *path, int pad, off_t length)
2505 * Truncate a file given its path name.
2508 truncate(struct truncate_args *uap)
2510 struct nlookupdata nd;
2513 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2515 error = kern_truncate(&nd, uap->length);
2521 kern_ftruncate(int fd, off_t length)
2523 struct thread *td = curthread;
2524 struct proc *p = td->td_proc;
2532 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2534 if ((fp->f_flag & FWRITE) == 0)
2536 vp = (struct vnode *)fp->f_data;
2537 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2538 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2539 if (vp->v_type == VDIR)
2541 else if ((error = vn_writechk(vp)) == 0) {
2543 vattr.va_size = length;
2544 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2546 VOP_UNLOCK(vp, 0, td);
2551 * ftruncate_args(int fd, int pad, off_t length)
2553 * Truncate a file given a file descriptor.
2556 ftruncate(struct ftruncate_args *uap)
2560 error = kern_ftruncate(uap->fd, uap->length);
2568 * Sync an open file.
2572 fsync(struct fsync_args *uap)
2574 struct thread *td = curthread;
2575 struct proc *p = td->td_proc;
2581 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2583 vp = (struct vnode *)fp->f_data;
2584 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2585 if (VOP_GETVOBJECT(vp, &obj) == 0)
2586 vm_object_page_clean(obj, 0, 0, 0);
2587 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2588 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2590 error = (*bioops.io_fsync)(vp);
2591 VOP_UNLOCK(vp, 0, td);
2596 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2598 struct namecache *fncpd;
2599 struct namecache *tncpd;
2600 struct namecache *ncp;
2605 if ((error = nlookup(fromnd)) != 0)
2607 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2612 * unlock the source ncp so we can lookup the target ncp without
2613 * deadlocking. The target may or may not exist so we do not check
2614 * for a target vp like kern_mkdir() and other creation functions do.
2616 * The source and target directories are ref'd and rechecked after
2617 * everything is relocked to determine if the source or target file
2620 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2621 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2622 cache_unlock(fromnd->nl_ncp);
2624 tond->nl_flags |= NLC_CREATE;
2625 if ((error = nlookup(tond)) != 0) {
2629 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2636 * If the source and target are the same there is nothing to do
2638 if (fromnd->nl_ncp == tond->nl_ncp) {
2645 * relock the source ncp
2647 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2648 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2649 } else if (fromnd->nl_ncp > tond->nl_ncp) {
2650 cache_lock(fromnd->nl_ncp);
2651 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2653 cache_unlock(tond->nl_ncp);
2654 cache_lock(fromnd->nl_ncp);
2655 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2656 cache_lock(tond->nl_ncp);
2657 cache_resolve(tond->nl_ncp, tond->nl_cred);
2659 fromnd->nl_flags |= NLC_NCPISLOCKED;
2662 * make sure the parent directories linkages are the same
2664 if (fncpd != fromnd->nl_ncp->nc_parent ||
2665 tncpd != tond->nl_ncp->nc_parent) {
2672 * Both the source and target must be within the same filesystem and
2673 * in the same filesystem as their parent directories within the
2674 * namecache topology.
2676 mp = fncpd->nc_mount;
2677 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2678 mp != tond->nl_ncp->nc_mount) {
2685 * If the target exists and either the source or target is a directory,
2686 * then both must be directories.
2688 if (tond->nl_ncp->nc_vp) {
2689 if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2690 if (tond->nl_ncp->nc_vp->v_type != VDIR)
2692 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2698 * You cannot rename a source into itself or a subdirectory of itself.
2699 * We check this by travsersing the target directory upwards looking
2700 * for a match against the source.
2703 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2704 if (fromnd->nl_ncp == ncp) {
2715 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp, tond->nl_cred);
2720 * rename_args(char *from, char *to)
2722 * Rename files. Source and destination must either both be directories,
2723 * or both not be directories. If target is a directory, it must be empty.
2726 rename(struct rename_args *uap)
2728 struct nlookupdata fromnd, tond;
2731 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2733 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2735 error = kern_rename(&fromnd, &tond);
2736 nlookup_done(&tond);
2738 nlookup_done(&fromnd);
2743 kern_mkdir(struct nlookupdata *nd, int mode)
2745 struct thread *td = curthread;
2746 struct proc *p = td->td_proc;
2747 struct namecache *ncp;
2753 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2754 if ((error = nlookup(nd)) != 0)
2762 vattr.va_type = VDIR;
2763 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2766 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2773 * mkdir_args(char *path, int mode)
2775 * Make a directory file.
2779 mkdir(struct mkdir_args *uap)
2781 struct nlookupdata nd;
2784 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2786 error = kern_mkdir(&nd, uap->mode);
2792 kern_rmdir(struct nlookupdata *nd)
2794 struct namecache *ncp;
2798 nd->nl_flags |= NLC_DELETE;
2799 if ((error = nlookup(nd)) != 0)
2803 error = VOP_NRMDIR(ncp, nd->nl_cred);
2808 * rmdir_args(char *path)
2810 * Remove a directory file.
2814 rmdir(struct rmdir_args *uap)
2816 struct nlookupdata nd;
2819 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2821 error = kern_rmdir(&nd);
2827 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2829 struct thread *td = curthread;
2830 struct proc *p = td->td_proc;
2838 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2840 if ((fp->f_flag & FREAD) == 0)
2842 vp = (struct vnode *)fp->f_data;
2844 if (vp->v_type != VDIR)
2846 aiov.iov_base = buf;
2847 aiov.iov_len = count;
2848 auio.uio_iov = &aiov;
2849 auio.uio_iovcnt = 1;
2850 auio.uio_rw = UIO_READ;
2851 auio.uio_segflg = UIO_USERSPACE;
2853 auio.uio_resid = count;
2854 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2855 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2856 loff = auio.uio_offset = fp->f_offset;
2857 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2858 fp->f_offset = auio.uio_offset;
2859 VOP_UNLOCK(vp, 0, td);
2862 if (count == auio.uio_resid) {
2863 if (union_dircheckp) {
2864 error = union_dircheckp(td, &vp, fp);
2870 if ((vp->v_flag & VROOT) &&
2871 (vp->v_mount->mnt_flag & MNT_UNION)) {
2872 struct vnode *tvp = vp;
2873 vp = vp->v_mount->mnt_vnodecovered;
2875 fp->f_data = (caddr_t)vp;
2884 *res = count - auio.uio_resid;
2889 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2891 * Read a block of directory entries in a file system independent format.
2894 getdirentries(struct getdirentries_args *uap)
2899 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2900 &uap->sysmsg_result);
2903 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2908 * getdents_args(int fd, char *buf, size_t count)
2911 getdents(struct getdents_args *uap)
2915 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2916 &uap->sysmsg_result);
2922 * umask(int newmask)
2924 * Set the mode mask for creation of filesystem nodes.
2929 umask(struct umask_args *uap)
2931 struct thread *td = curthread;
2932 struct proc *p = td->td_proc;
2933 struct filedesc *fdp;
2936 uap->sysmsg_result = fdp->fd_cmask;
2937 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2942 * revoke(char *path)
2944 * Void all references to file by ripping underlying filesystem
2949 revoke(struct revoke_args *uap)
2951 struct thread *td = curthread;
2952 struct nlookupdata nd;
2959 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
2961 error = nlookup(&nd);
2963 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2964 cred = crhold(nd.nl_cred);
2967 if (vp->v_type != VCHR && vp->v_type != VBLK)
2970 error = VOP_GETATTR(vp, &vattr, td);
2971 if (error == 0 && cred->cr_uid != vattr.va_uid)
2972 error = suser_cred(cred, PRISON_ROOT);
2973 if (error == 0 && count_udev(vp->v_udev) > 0) {
2974 if ((error = vx_lock(vp)) == 0) {
2975 VOP_REVOKE(vp, REVOKEALL);
2986 * Convert a user file descriptor to a kernel file entry.
2989 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2993 if ((u_int)fd >= fdp->fd_nfiles ||
2994 (fp = fdp->fd_ofiles[fd]) == NULL)
2996 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
3002 * getfh_args(char *fname, fhandle_t *fhp)
3004 * Get (NFS) file handle
3007 getfh(struct getfh_args *uap)
3009 struct thread *td = curthread;
3010 struct nlookupdata nd;
3016 * Must be super user
3018 if ((error = suser(td)) != 0)
3022 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3024 error = nlookup(&nd);
3026 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3029 bzero(&fh, sizeof(fh));
3030 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3031 error = VFS_VPTOFH(vp, &fh.fh_fid);
3034 error = copyout(&fh, uap->fhp, sizeof(fh));
3040 * fhopen_args(const struct fhandle *u_fhp, int flags)
3042 * syscall for the rpc.lockd to use to translate a NFS file handle into
3043 * an open descriptor.
3045 * warning: do not remove the suser() call or this becomes one giant
3049 fhopen(struct fhopen_args *uap)
3051 struct thread *td = curthread;
3052 struct proc *p = td->td_proc;
3057 struct vattr *vap = &vat;
3059 struct filedesc *fdp = p->p_fd;
3060 int fmode, mode, error, type;
3066 * Must be super user
3072 fmode = FFLAGS(SCARG(uap, flags));
3073 /* why not allow a non-read/write open for our lockd? */
3074 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3076 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
3079 /* find the mount point */
3080 mp = vfs_getvfs(&fhp.fh_fsid);
3083 /* now give me my vnode, it gets returned to me locked */
3084 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3088 * from now on we have to make sure not
3089 * to forget about the vnode
3090 * any error that causes an abort must vput(vp)
3091 * just set error = err and 'goto bad;'.
3097 if (vp->v_type == VLNK) {
3101 if (vp->v_type == VSOCK) {
3106 if (fmode & (FWRITE | O_TRUNC)) {
3107 if (vp->v_type == VDIR) {
3111 error = vn_writechk(vp);
3119 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
3123 if (fmode & O_TRUNC) {
3124 VOP_UNLOCK(vp, 0, td); /* XXX */
3125 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
3126 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
3129 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
3135 * VOP_OPEN needs the file pointer so it can potentially override
3138 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3141 if ((error = falloc(p, &nfp, NULL)) != 0)
3145 fp->f_data = (caddr_t)vp;
3146 fp->f_flag = fmode & FMASK;
3147 fp->f_ops = &vnode_fileops;
3148 fp->f_type = DTYPE_VNODE;
3150 error = VOP_OPEN(vp, fmode, p->p_ucred, fp, td);
3153 * setting f_ops this way prevents VOP_CLOSE from being
3154 * called or fdrop() releasing the vp from v_data. Since
3155 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3157 fp->f_ops = &badfileops;
3166 * The fp now owns a reference on the vnode. We still have our own
3172 * Make sure that a VM object is created for VMIO support. If this
3173 * fails just fdrop() normally to clean up.
3175 if (vn_canvmio(vp) == TRUE) {
3176 if ((error = vfs_object_create(vp, td)) != 0) {
3183 * The open was successful, associate it with a file descriptor.
3185 if ((error = fsetfd(p, fp, &indx)) != 0) {
3192 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3193 lf.l_whence = SEEK_SET;
3196 if (fmode & O_EXLOCK)
3197 lf.l_type = F_WRLCK;
3199 lf.l_type = F_RDLCK;
3201 if ((fmode & FNONBLOCK) == 0)
3203 VOP_UNLOCK(vp, 0, td);
3204 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3206 * lock request failed. Normally close the descriptor
3207 * but handle the case where someone might have dup()d
3208 * or close()d it when we weren't looking.
3210 if (fdp->fd_ofiles[indx] == fp) {
3211 fdp->fd_ofiles[indx] = NULL;
3216 * release our private reference.
3222 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3223 fp->f_flag |= FHASLOCK;
3225 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
3226 vfs_object_create(vp, td);
3230 uap->sysmsg_result = indx;
3239 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3242 fhstat(struct fhstat_args *uap)
3244 struct thread *td = curthread;
3252 * Must be super user
3258 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3262 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3264 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3266 error = vn_stat(vp, &sb, td);
3270 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3275 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3278 fhstatfs(struct fhstatfs_args *uap)
3280 struct thread *td = curthread;
3289 * Must be super user
3291 if ((error = suser(td)))
3294 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3297 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3299 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3304 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3306 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3308 bcopy(sp, &sb, sizeof(sb));
3309 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3312 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3316 * Syscall to push extended attribute configuration information into the
3317 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3318 * a command (int cmd), and attribute name and misc data. For now, the
3319 * attribute name is left in userspace for consumption by the VFS_op.
3320 * It will probably be changed to be copied into sysspace by the
3321 * syscall in the future, once issues with various consumers of the
3322 * attribute code have raised their hands.
3324 * Currently this is used only by UFS Extended Attributes.
3327 extattrctl(struct extattrctl_args *uap)
3329 struct nlookupdata nd;
3335 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3337 error = nlookup(&nd);
3339 mp = nd.nl_ncp->nc_mount;
3340 error = VFS_EXTATTRCTL(mp, SCARG(uap, cmd),
3341 SCARG(uap, attrname), SCARG(uap, arg),
3349 * Syscall to set a named extended attribute on a file or directory.
3350 * Accepts attribute name, and a uio structure pointing to the data to set.
3351 * The uio is consumed in the style of writev(). The real work happens
3352 * in VOP_SETEXTATTR().
3355 extattr_set_file(struct extattr_set_file_args *uap)
3357 char attrname[EXTATTR_MAXNAMELEN];
3358 struct iovec aiov[UIO_SMALLIOV];
3359 struct iovec *needfree;
3360 struct nlookupdata nd;
3369 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3374 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3376 error = nlookup(&nd);
3378 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3385 iovlen = uap->iovcnt * sizeof(struct iovec);
3386 if (uap->iovcnt > UIO_SMALLIOV) {
3387 if (uap->iovcnt > UIO_MAXIOV) {
3391 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3397 auio.uio_iovcnt = uap->iovcnt;
3398 auio.uio_rw = UIO_WRITE;
3399 auio.uio_segflg = UIO_USERSPACE;
3400 auio.uio_td = nd.nl_td;
3401 auio.uio_offset = 0;
3402 if ((error = copyin(uap->iovp, iov, iovlen)))
3405 for (i = 0; i < uap->iovcnt; i++) {
3406 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3410 auio.uio_resid += iov->iov_len;
3413 cnt = auio.uio_resid;
3414 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td);
3415 cnt -= auio.uio_resid;
3416 uap->sysmsg_result = cnt;
3421 FREE(needfree, M_IOV);
3426 * Syscall to get a named extended attribute on a file or directory.
3427 * Accepts attribute name, and a uio structure pointing to a buffer for the
3428 * data. The uio is consumed in the style of readv(). The real work
3429 * happens in VOP_GETEXTATTR();
3432 extattr_get_file(struct extattr_get_file_args *uap)
3434 char attrname[EXTATTR_MAXNAMELEN];
3435 struct iovec aiov[UIO_SMALLIOV];
3436 struct iovec *needfree;
3437 struct nlookupdata nd;
3446 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3451 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3453 error = nlookup(&nd);
3455 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3461 iovlen = uap->iovcnt * sizeof (struct iovec);
3463 if (uap->iovcnt > UIO_SMALLIOV) {
3464 if (uap->iovcnt > UIO_MAXIOV) {
3468 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3474 auio.uio_iovcnt = uap->iovcnt;
3475 auio.uio_rw = UIO_READ;
3476 auio.uio_segflg = UIO_USERSPACE;
3477 auio.uio_td = nd.nl_td;
3478 auio.uio_offset = 0;
3479 if ((error = copyin(uap->iovp, iov, iovlen)))
3482 for (i = 0; i < uap->iovcnt; i++) {
3483 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3487 auio.uio_resid += iov->iov_len;
3490 cnt = auio.uio_resid;
3491 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td);
3492 cnt -= auio.uio_resid;
3493 uap->sysmsg_result = cnt;
3498 FREE(needfree, M_IOV);
3503 * Syscall to delete a named extended attribute from a file or directory.
3504 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3507 extattr_delete_file(struct extattr_delete_file_args *uap)
3509 char attrname[EXTATTR_MAXNAMELEN];
3510 struct nlookupdata nd;
3514 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3519 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3521 error = nlookup(&nd);
3523 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3529 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred, nd.nl_td);
3536 * print out statistics from the current status of the buffer pool
3537 * this can be toggeled by the system control option debug.syncprt
3546 int counts[(MAXBSIZE / PAGE_SIZE) + 1];
3547 static char *bname[3] = { "LOCKED", "LRU", "AGE" };
3549 for (dp = bufqueues, i = 0; dp < &bufqueues[3]; dp++, i++) {
3551 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3554 TAILQ_FOREACH(bp, dp, b_freelist) {
3555 counts[bp->b_bufsize/PAGE_SIZE]++;
3559 printf("%s: total-%d", bname[i], count);
3560 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3562 printf(", %d-%d", j * PAGE_SIZE, counts[j]);