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.38 2004/08/17 18:57:32 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/dirent.h>
62 #include <sys/extattr.h>
63 #include <sys/kern_syscall.h>
65 #include <machine/limits.h>
66 #include <vfs/union/union.h>
67 #include <sys/sysctl.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_zone.h>
71 #include <vm/vm_page.h>
73 #include <sys/file2.h>
75 static int checkvp_chdir (struct vnode *vn, struct thread *td);
76 static void checkdirs (struct vnode *olddp);
77 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
78 static int getutimes (const struct timeval *, struct timespec *);
79 static int setfown (struct vnode *, uid_t, gid_t);
80 static int setfmode (struct vnode *, int);
81 static int setfflags (struct vnode *, int);
82 static int setutimes (struct vnode *, const struct timespec *, int);
83 static int usermount = 0; /* if 1, non-root can mount fs. */
85 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
87 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
90 * Virtual File System System Calls
94 * Mount a file system.
97 * mount_args(char *type, char *path, int flags, caddr_t data)
101 mount(struct mount_args *uap)
103 struct thread *td = curthread;
104 struct proc *p = td->td_proc;
107 struct vfsconf *vfsp;
108 int error, flag = 0, flag2 = 0;
111 char fstypename[MFSNAMELEN];
116 if (p->p_ucred->cr_prison != NULL)
118 if (usermount == 0 && (error = suser(td)))
121 * Do not allow NFS export by non-root users.
123 if (SCARG(uap, flags) & MNT_EXPORTED) {
129 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
132 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
134 * Get vnode to be covered
136 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
137 SCARG(uap, path), td);
138 if ((error = namei(&nd)) != 0)
140 NDFREE(&nd, NDF_ONLY_PNBUF);
142 if (SCARG(uap, flags) & MNT_UPDATE) {
143 if ((vp->v_flag & VROOT) == 0) {
149 flag2 = mp->mnt_kern_flag;
151 * We only allow the filesystem to be reloaded if it
152 * is currently mounted read-only.
154 if ((SCARG(uap, flags) & MNT_RELOAD) &&
155 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
157 return (EOPNOTSUPP); /* Needs translation */
160 * Only root, or the user that did the original mount is
161 * permitted to update it.
163 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
164 (error = suser(td))) {
168 if (vfs_busy(mp, LK_NOWAIT, NULL, td)) {
172 lwkt_gettoken(&vlock, vp->v_interlock);
173 if ((vp->v_flag & VMOUNT) != 0 ||
174 vp->v_mountedhere != NULL) {
175 lwkt_reltoken(&vlock);
180 vp->v_flag |= VMOUNT;
181 lwkt_reltoken(&vlock);
183 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
184 VOP_UNLOCK(vp, NULL, 0, td);
188 * If the user is not root, ensure that they own the directory
189 * onto which we are attempting to mount.
191 if ((error = VOP_GETATTR(vp, &va, td)) ||
192 (va.va_uid != p->p_ucred->cr_uid &&
193 (error = suser(td)))) {
197 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
201 if (vp->v_type != VDIR) {
205 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
209 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
210 if (!strcmp(vfsp->vfc_name, fstypename))
215 /* Only load modules for root (very important!) */
216 if ((error = suser(td)) != 0) {
220 error = linker_load_file(fstypename, &lf);
221 if (error || lf == NULL) {
228 /* lookup again, see if the VFS was loaded */
229 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
230 if (!strcmp(vfsp->vfc_name, fstypename))
234 linker_file_unload(lf);
239 lwkt_gettoken(&vlock, vp->v_interlock);
240 if ((vp->v_flag & VMOUNT) != 0 ||
241 vp->v_mountedhere != NULL) {
242 lwkt_reltoken(&vlock);
246 vp->v_flag |= VMOUNT;
247 lwkt_reltoken(&vlock);
250 * Allocate and initialize the filesystem.
252 mp = malloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
253 TAILQ_INIT(&mp->mnt_nvnodelist);
254 TAILQ_INIT(&mp->mnt_reservedvnlist);
255 mp->mnt_nvnodelistsize = 0;
256 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
257 vfs_busy(mp, LK_NOWAIT, NULL, td);
258 mp->mnt_op = vfsp->vfc_vfsops;
260 vfsp->vfc_refcount++;
261 mp->mnt_stat.f_type = vfsp->vfc_typenum;
262 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
263 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
264 mp->mnt_vnodecovered = vp;
265 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
266 mp->mnt_iosize_max = DFLTPHYS;
267 VOP_UNLOCK(vp, NULL, 0, td);
270 * Set the mount level flags.
272 if (SCARG(uap, flags) & MNT_RDONLY)
273 mp->mnt_flag |= MNT_RDONLY;
274 else if (mp->mnt_flag & MNT_RDONLY)
275 mp->mnt_kern_flag |= MNTK_WANTRDWR;
276 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
277 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
278 MNT_NOSYMFOLLOW | MNT_IGNORE |
279 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
280 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
281 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
282 MNT_NOSYMFOLLOW | MNT_IGNORE |
283 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
285 * Mount the filesystem.
286 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
287 * get. No freeing of cn_pnbuf.
289 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
290 if (mp->mnt_flag & MNT_UPDATE) {
291 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
292 mp->mnt_flag &= ~MNT_RDONLY;
293 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
294 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
297 mp->mnt_kern_flag = flag2;
300 lwkt_gettoken(&vlock, vp->v_interlock);
301 vp->v_flag &= ~VMOUNT;
302 lwkt_reltoken(&vlock);
306 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
308 * Put the new filesystem on the mount list after root.
312 lwkt_gettoken(&vlock, vp->v_interlock);
313 vp->v_flag &= ~VMOUNT;
314 vp->v_mountedhere = mp;
315 lwkt_reltoken(&vlock);
316 lwkt_gettoken(&ilock, &mountlist_token);
317 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
318 lwkt_reltoken(&ilock);
320 VOP_UNLOCK(vp, NULL, 0, td);
321 error = vfs_allocate_syncvnode(mp);
323 if ((error = VFS_START(mp, 0, td)) != 0)
326 vfs_rm_vnodeops(&mp->mnt_vn_ops);
327 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
328 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
329 lwkt_gettoken(&vlock, vp->v_interlock);
330 vp->v_flag &= ~VMOUNT;
331 lwkt_reltoken(&vlock);
332 mp->mnt_vfc->vfc_refcount--;
341 * Scan all active processes to see if any of them have a current
342 * or root directory onto which the new filesystem has just been
343 * mounted. If so, replace them with the new mount point.
346 checkdirs(struct vnode *olddp)
348 struct filedesc *fdp;
352 if (olddp->v_usecount == 1)
354 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
355 panic("mount: lost mount");
356 FOREACH_PROC_IN_SYSTEM(p) {
358 if (fdp->fd_cdir == olddp) {
361 fdp->fd_cdir = newdp;
363 if (fdp->fd_rdir == olddp) {
366 fdp->fd_rdir = newdp;
369 if (rootvnode == olddp) {
373 vfs_cache_setroot(rootvnode);
379 * Unmount a file system.
381 * Note: unmount takes a path to the vnode mounted on as argument,
382 * not special file (as before).
385 * umount_args(char *path, int flags)
389 unmount(struct unmount_args *uap)
391 struct thread *td = curthread;
392 struct proc *p = td->td_proc;
399 if (p->p_ucred->cr_prison != NULL)
401 if (usermount == 0 && (error = suser(td)))
404 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
405 SCARG(uap, path), td);
406 if ((error = namei(&nd)) != 0)
409 NDFREE(&nd, NDF_ONLY_PNBUF);
413 * Only root, or the user that did the original mount is
414 * permitted to unmount this filesystem.
416 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
417 (error = suser(td))) {
423 * Don't allow unmounting the root file system.
425 if (mp->mnt_flag & MNT_ROOTFS) {
431 * Must be the root of the filesystem
433 if ((vp->v_flag & VROOT) == 0) {
438 return (dounmount(mp, SCARG(uap, flags), td));
442 * Do the actual file system unmount.
445 dounmount(struct mount *mp, int flags, struct thread *td)
447 struct vnode *coveredvp;
452 lwkt_gettoken(&ilock, &mountlist_token);
453 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
454 lwkt_reltoken(&ilock);
457 mp->mnt_kern_flag |= MNTK_UNMOUNT;
458 /* Allow filesystems to detect that a forced unmount is in progress. */
459 if (flags & MNT_FORCE)
460 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
461 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
462 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
464 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
465 if (mp->mnt_kern_flag & MNTK_MWAIT)
470 if (mp->mnt_flag & MNT_EXPUBLIC)
471 vfs_setpublicfs(NULL, NULL, NULL);
473 vfs_msync(mp, MNT_WAIT);
474 async_flag = mp->mnt_flag & MNT_ASYNC;
475 mp->mnt_flag &=~ MNT_ASYNC;
476 cache_purgevfs(mp); /* remove cache entries for this file sys */
477 if (mp->mnt_syncer != NULL)
478 vrele(mp->mnt_syncer);
479 if (((mp->mnt_flag & MNT_RDONLY) ||
480 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
482 error = VFS_UNMOUNT(mp, flags, td);
483 lwkt_gettokref(&ilock);
485 if (mp->mnt_syncer == NULL)
486 vfs_allocate_syncvnode(mp);
487 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
488 mp->mnt_flag |= async_flag;
489 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
491 if (mp->mnt_kern_flag & MNTK_MWAIT)
495 TAILQ_REMOVE(&mountlist, mp, mnt_list);
498 * Remove any installed vnode ops here so the individual VFSs don't
501 vfs_rm_vnodeops(&mp->mnt_vn_ops);
502 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
503 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
505 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
506 coveredvp->v_mountedhere = NULL;
509 mp->mnt_vfc->vfc_refcount--;
510 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
511 panic("unmount: dangling vnode");
512 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
513 if (mp->mnt_kern_flag & MNTK_MWAIT)
520 * Sync each mounted filesystem.
524 static int syncprt = 0;
525 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
530 sync(struct sync_args *uap)
532 struct thread *td = curthread;
533 struct mount *mp, *nmp;
537 lwkt_gettoken(&ilock, &mountlist_token);
538 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
539 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
540 nmp = TAILQ_NEXT(mp, mnt_list);
543 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
544 asyncflag = mp->mnt_flag & MNT_ASYNC;
545 mp->mnt_flag &= ~MNT_ASYNC;
546 vfs_msync(mp, MNT_NOWAIT);
547 VFS_SYNC(mp, MNT_NOWAIT, td);
548 mp->mnt_flag |= asyncflag;
550 lwkt_gettokref(&ilock);
551 nmp = TAILQ_NEXT(mp, mnt_list);
554 lwkt_reltoken(&ilock);
556 * print out buffer pool stat information on each sync() call.
565 /* XXX PRISON: could be per prison flag */
566 static int prison_quotas;
568 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
572 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
574 * Change filesystem quotas.
578 quotactl(struct quotactl_args *uap)
580 struct thread *td = curthread;
581 struct proc *p = td->td_proc;
587 if (p->p_ucred->cr_prison && !prison_quotas)
589 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
590 SCARG(uap, path), td);
591 if ((error = namei(&nd)) != 0)
593 mp = nd.ni_vp->v_mount;
594 NDFREE(&nd, NDF_ONLY_PNBUF);
596 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
597 SCARG(uap, arg), td));
601 kern_statfs(struct nameidata *nd, struct statfs *buf)
603 struct thread *td = curthread;
611 mp = nd->ni_vp->v_mount;
613 NDFREE(nd, NDF_ONLY_PNBUF);
615 error = VFS_STATFS(mp, sp, td);
618 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
619 bcopy(sp, buf, sizeof(*buf));
620 /* Only root should have access to the fsid's. */
622 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
627 * statfs_args(char *path, struct statfs *buf)
629 * Get filesystem statistics.
632 statfs(struct statfs_args *uap)
634 struct thread *td = curthread;
639 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
641 error = kern_statfs(&nd, &buf);
644 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
649 kern_fstatfs(int fd, struct statfs *buf)
651 struct thread *td = curthread;
652 struct proc *p = td->td_proc;
659 error = getvnode(p->p_fd, fd, &fp);
662 mp = ((struct vnode *)fp->f_data)->v_mount;
666 error = VFS_STATFS(mp, sp, td);
669 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
670 bcopy(sp, buf, sizeof(*buf));
671 /* Only root should have access to the fsid's. */
673 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
678 * fstatfs_args(int fd, struct statfs *buf)
680 * Get filesystem statistics.
683 fstatfs(struct fstatfs_args *uap)
688 error = kern_fstatfs(uap->fd, &buf);
691 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
696 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
698 * Get statistics on all filesystems.
702 getfsstat(struct getfsstat_args *uap)
704 struct thread *td = curthread;
705 struct mount *mp, *nmp;
709 long count, maxcount, error;
711 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
712 sfsp = (caddr_t)SCARG(uap, buf);
714 lwkt_gettoken(&ilock, &mountlist_token);
715 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
716 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
717 nmp = TAILQ_NEXT(mp, mnt_list);
720 if (sfsp && count < maxcount) {
723 * If MNT_NOWAIT or MNT_LAZY is specified, do not
724 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
725 * overrides MNT_WAIT.
727 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
728 (SCARG(uap, flags) & MNT_WAIT)) &&
729 (error = VFS_STATFS(mp, sp, td))) {
730 lwkt_gettokref(&ilock);
731 nmp = TAILQ_NEXT(mp, mnt_list);
735 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
736 error = copyout(sp, sfsp, sizeof(*sp));
744 lwkt_gettokref(&ilock);
745 nmp = TAILQ_NEXT(mp, mnt_list);
748 lwkt_reltoken(&ilock);
749 if (sfsp && count > maxcount)
750 uap->sysmsg_result = maxcount;
752 uap->sysmsg_result = count;
757 * fchdir_args(int fd)
759 * Change current working directory to a given file descriptor.
763 fchdir(struct fchdir_args *uap)
765 struct thread *td = curthread;
766 struct proc *p = td->td_proc;
767 struct filedesc *fdp = p->p_fd;
768 struct vnode *vp, *tdp;
773 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
775 vp = (struct vnode *)fp->f_data;
777 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
778 if (vp->v_type != VDIR)
781 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
782 while (!error && (mp = vp->v_mountedhere) != NULL) {
783 if (vfs_busy(mp, 0, NULL, td))
785 error = VFS_ROOT(mp, &tdp);
796 VOP_UNLOCK(vp, NULL, 0, td);
803 kern_chdir(struct nameidata *nd)
805 struct thread *td = curthread;
806 struct proc *p = td->td_proc;
807 struct filedesc *fdp = p->p_fd;
810 if ((error = namei(nd)) != 0)
812 if ((error = checkvp_chdir(nd->ni_vp, td)) == 0) {
814 fdp->fd_cdir = nd->ni_vp;
817 NDFREE(nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
822 * chdir_args(char *path)
824 * Change current working directory (``.'').
827 chdir(struct chdir_args *uap)
829 struct thread *td = curthread;
833 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
836 error = kern_chdir(&nd);
842 * Helper function for raised chroot(2) security function: Refuse if
843 * any filedescriptors are open directories.
846 chroot_refuse_vdir_fds(fdp)
847 struct filedesc *fdp;
854 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
855 error = getvnode(fdp, fd, &fp);
858 vp = (struct vnode *)fp->f_data;
859 if (vp->v_type != VDIR)
867 * This sysctl determines if we will allow a process to chroot(2) if it
868 * has a directory open:
869 * 0: disallowed for all processes.
870 * 1: allowed for processes that were not already chroot(2)'ed.
871 * 2: allowed for all processes.
874 static int chroot_allow_open_directories = 1;
876 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
877 &chroot_allow_open_directories, 0, "");
880 * Chroot to the specified vnode. vp must be locked and referenced on
881 * call, and will be left locked and referenced on return. This routine
882 * may acquire additional refs on the vnode when associating it with
883 * the process's root and/or jail dirs.
886 kern_chroot(struct vnode *vp)
888 struct thread *td = curthread;
889 struct proc *p = td->td_proc;
890 struct filedesc *fdp = p->p_fd;
894 * Only root can chroot
896 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
900 * Disallow open directory descriptors (fchdir() breakouts).
902 if (chroot_allow_open_directories == 0 ||
903 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
904 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
909 * Check the validity of vp as a directory to change to and
910 * associate it with rdir/jdir.
912 if ((error = checkvp_chdir(vp, td)) == 0) {
916 if (fdp->fd_jdir == NULL) {
925 * chroot_args(char *path)
927 * Change notion of root (``/'') directory.
931 chroot(struct chroot_args *uap)
933 struct thread *td = curthread;
937 KKASSERT(td->td_proc);
938 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
939 SCARG(uap, path), td);
940 if ((error = namei(&nd)) == 0) {
941 error = kern_chroot(nd.ni_vp);
942 NDFREE(&nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
948 * Common routine for chroot and chdir. Given a locked, referenced vnode,
949 * determine whether it is legal to chdir to the vnode. The vnode's state
950 * is not changed by this call.
953 checkvp_chdir(struct vnode *vp, struct thread *td)
957 if (vp->v_type != VDIR)
960 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
965 kern_open(struct nameidata *nd, int oflags, int mode, int *res)
967 struct thread *td = curthread;
968 struct proc *p = td->td_proc;
969 struct filedesc *fdp = p->p_fd;
974 int type, indx, error;
977 if ((oflags & O_ACCMODE) == O_ACCMODE)
979 flags = FFLAGS(oflags);
980 error = falloc(p, &nfp, &indx);
984 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
985 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
987 * Bump the ref count to prevent another process from closing
988 * the descriptor while we are blocked in vn_open()
991 error = vn_open(nd, flags, cmode);
994 * release our own reference
999 * handle special fdopen() case. bleh. dupfdopen() is
1000 * responsible for dropping the old contents of ofiles[indx]
1003 if ((error == ENODEV || error == ENXIO) &&
1004 p->p_dupfd >= 0 && /* XXX from fdopen */
1006 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
1011 * Clean up the descriptor, but only if another thread hadn't
1012 * replaced or closed it.
1014 if (fdp->fd_ofiles[indx] == fp) {
1015 fdp->fd_ofiles[indx] = NULL;
1019 if (error == ERESTART)
1024 NDFREE(nd, NDF_ONLY_PNBUF);
1028 * There should be 2 references on the file, one from the descriptor
1029 * table, and one for us.
1031 * Handle the case where someone closed the file (via its file
1032 * descriptor) while we were blocked. The end result should look
1033 * like opening the file succeeded but it was immediately closed.
1035 if (fp->f_count == 1) {
1036 KASSERT(fdp->fd_ofiles[indx] != fp,
1037 ("Open file descriptor lost all refs"));
1038 VOP_UNLOCK(vp, NULL, 0, td);
1039 vn_close(vp, flags & FMASK, td);
1045 fp->f_data = (caddr_t)vp;
1046 fp->f_flag = flags & FMASK;
1048 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1049 if (flags & (O_EXLOCK | O_SHLOCK)) {
1050 lf.l_whence = SEEK_SET;
1053 if (flags & O_EXLOCK)
1054 lf.l_type = F_WRLCK;
1056 lf.l_type = F_RDLCK;
1058 if ((flags & FNONBLOCK) == 0)
1060 VOP_UNLOCK(vp, NULL, 0, td);
1061 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1063 * lock request failed. Normally close the descriptor
1064 * but handle the case where someone might have dup()d
1065 * it when we weren't looking. One reference is
1066 * owned by the descriptor array, the other by us.
1068 if (fdp->fd_ofiles[indx] == fp) {
1069 fdp->fd_ofiles[indx] = NULL;
1075 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1076 fp->f_flag |= FHASLOCK;
1078 /* assert that vn_open created a backing object if one is needed */
1079 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1080 ("open: vmio vnode has no backing object after vn_open"));
1081 VOP_UNLOCK(vp, NULL, 0, td);
1084 * release our private reference, leaving the one associated with the
1085 * descriptor table intact.
1093 * open_args(char *path, int flags, int mode)
1095 * Check permissions, allocate an open file structure,
1096 * and call the device open routine if any.
1099 open(struct open_args *uap)
1101 struct thread *td = curthread;
1102 struct nameidata nd;
1105 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1107 error = kern_open(&nd, uap->flags, uap->mode, &uap->sysmsg_result);
1113 kern_mknod(struct nameidata *nd, int mode, int dev)
1115 struct thread *td = curthread;
1116 struct proc *p = td->td_proc;
1124 switch (mode & S_IFMT) {
1130 error = suser_cred(p->p_ucred, PRISON_ROOT);
1144 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1145 vattr.va_rdev = dev;
1148 switch (mode & S_IFMT) {
1149 case S_IFMT: /* used by badsect to flag bad sectors */
1150 vattr.va_type = VBAD;
1153 vattr.va_type = VCHR;
1156 vattr.va_type = VBLK;
1167 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1169 error = VOP_WHITEOUT(nd->ni_dvp, NCPNULL,
1170 &nd->ni_cnd, NAMEI_CREATE);
1172 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp,
1173 &nd->ni_cnd, &vattr);
1177 NDFREE(nd, NDF_ONLY_PNBUF);
1180 NDFREE(nd, NDF_ONLY_PNBUF);
1181 if (nd->ni_dvp == vp)
1188 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mknod");
1189 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mknod");
1194 * mknod_args(char *path, int mode, int dev)
1196 * Create a special file.
1199 mknod(struct mknod_args *uap)
1201 struct thread *td = curthread;
1202 struct nameidata nd;
1205 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1208 error = kern_mknod(&nd, uap->mode, uap->dev);
1214 kern_mkfifo(struct nameidata *nd, int mode)
1216 struct thread *td = curthread;
1217 struct proc *p = td->td_proc;
1225 if (nd->ni_vp != NULL) {
1226 NDFREE(nd, NDF_ONLY_PNBUF);
1227 if (nd->ni_dvp == nd->ni_vp)
1235 vattr.va_type = VFIFO;
1236 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1237 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1238 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd, &vattr);
1241 NDFREE(nd, NDF_ONLY_PNBUF);
1247 * mkfifo_args(char *path, int mode)
1249 * Create a named pipe.
1252 mkfifo(struct mkfifo_args *uap)
1254 struct thread *td = curthread;
1255 struct nameidata nd;
1258 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1261 error = kern_mkfifo(&nd, uap->mode);
1267 kern_link(struct nameidata *nd, struct nameidata *linknd)
1269 struct thread *td = curthread;
1270 struct proc *p = td->td_proc;
1278 NDFREE(nd, NDF_ONLY_PNBUF);
1280 if (vp->v_type == VDIR)
1281 error = EPERM; /* POSIX */
1283 error = namei(linknd);
1285 if (linknd->ni_vp != NULL) {
1287 vrele(linknd->ni_vp);
1290 VOP_LEASE(linknd->ni_dvp, td, p->p_ucred,
1292 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1293 error = VOP_LINK(linknd->ni_dvp, NCPNULL, vp,
1296 NDFREE(linknd, NDF_ONLY_PNBUF);
1297 if (linknd->ni_dvp == linknd->ni_vp)
1298 vrele(linknd->ni_dvp);
1300 vput(linknd->ni_dvp);
1301 ASSERT_VOP_UNLOCKED(linknd->ni_dvp, "link");
1302 ASSERT_VOP_UNLOCKED(linknd->ni_vp, "link");
1310 * link_args(char *path, char *link)
1312 * Make a hard file link.
1315 link(struct link_args *uap)
1317 struct thread *td = curthread;
1318 struct nameidata nd, linknd;
1321 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1323 NDINIT(&linknd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1324 UIO_USERSPACE, uap->link, td);
1326 error = kern_link(&nd, &linknd);
1332 kern_symlink(char *path, struct nameidata *nd)
1334 struct thread *td = curthread;
1335 struct proc *p = td->td_proc;
1344 NDFREE(nd, NDF_ONLY_PNBUF);
1345 if (nd->ni_dvp == nd->ni_vp)
1353 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1354 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1355 error = VOP_SYMLINK(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
1357 NDFREE(nd, NDF_ONLY_PNBUF);
1361 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "symlink");
1362 ASSERT_VOP_UNLOCKED(nd->ni_vp, "symlink");
1368 * symlink(char *path, char *link)
1370 * Make a symbolic link.
1373 symlink(struct symlink_args *uap)
1375 struct thread *td = curthread;
1376 struct nameidata nd;
1380 path = zalloc(namei_zone);
1381 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1383 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1384 UIO_USERSPACE, uap->link, td);
1385 error = kern_symlink(path, &nd);
1387 zfree(namei_zone, path);
1392 * undelete_args(char *path)
1394 * Delete a whiteout from the filesystem.
1398 undelete(struct undelete_args *uap)
1400 struct thread *td = curthread;
1401 struct proc *p = td->td_proc;
1403 struct nameidata nd;
1406 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
1407 SCARG(uap, path), td);
1412 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
1413 NDFREE(&nd, NDF_ONLY_PNBUF);
1414 if (nd.ni_dvp == nd.ni_vp)
1423 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1424 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
1425 NDFREE(&nd, NDF_ONLY_PNBUF);
1427 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1428 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1433 kern_unlink(struct nameidata *nd)
1435 struct thread *td = curthread;
1436 struct proc *p = td->td_proc;
1445 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1446 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1448 if (vp->v_type == VDIR)
1449 error = EPERM; /* POSIX */
1452 * The root of a mounted filesystem cannot be deleted.
1454 * XXX: can this only be a VDIR case?
1456 if (vp->v_flag & VROOT)
1461 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1462 error = VOP_REMOVE(nd->ni_dvp, NCPNULL, vp, &nd->ni_cnd);
1464 NDFREE(nd, NDF_ONLY_PNBUF);
1465 if (nd->ni_dvp == vp)
1471 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "unlink");
1472 ASSERT_VOP_UNLOCKED(nd->ni_vp, "unlink");
1477 * unlink_args(char *path)
1479 * Delete a name from the filesystem.
1482 unlink(struct unlink_args *uap)
1484 struct thread *td = curthread;
1485 struct nameidata nd;
1488 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1491 error = kern_unlink(&nd);
1497 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1499 struct thread *td = curthread;
1500 struct proc *p = td->td_proc;
1501 struct filedesc *fdp = p->p_fd;
1506 if (fd >= fdp->fd_nfiles ||
1507 (fp = fdp->fd_ofiles[fd]) == NULL)
1509 if (fp->f_type != DTYPE_VNODE)
1513 fp->f_offset += offset;
1516 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1519 fp->f_offset = offset + vattr.va_size;
1522 fp->f_offset = offset;
1527 *res = fp->f_offset;
1532 * lseek_args(int fd, int pad, off_t offset, int whence)
1534 * Reposition read/write file offset.
1537 lseek(struct lseek_args *uap)
1541 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1542 &uap->sysmsg_offset);
1548 kern_access(struct nameidata *nd, int aflags)
1550 struct thread *td = curthread;
1551 struct proc *p = td->td_proc;
1552 struct ucred *cred, *tmpcred;
1558 * Create and modify a temporary credential instead of one that
1559 * is potentially shared. This could also mess up socket
1560 * buffer accounting which can run in an interrupt context.
1562 tmpcred = crdup(cred);
1563 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1564 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1565 p->p_ucred = tmpcred;
1566 nd->ni_cnd.cn_cred = tmpcred;
1572 /* Flags == 0 means only check for existence. */
1581 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1582 error = VOP_ACCESS(vp, flags, tmpcred, td);
1584 NDFREE(nd, NDF_ONLY_PNBUF);
1593 * access_args(char *path, int flags)
1595 * Check access permissions.
1598 access(struct access_args *uap)
1600 struct thread *td = curthread;
1601 struct nameidata nd;
1604 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1605 UIO_USERSPACE, uap->path, td);
1607 error = kern_access(&nd, uap->flags);
1613 kern_stat(struct nameidata *nd, struct stat *st)
1615 struct thread *td = curthread;
1621 error = vn_stat(nd->ni_vp, st, td);
1622 NDFREE(nd, NDF_ONLY_PNBUF);
1628 * stat_args(char *path, struct stat *ub)
1630 * Get file status; this version follows links.
1633 stat(struct stat_args *uap)
1635 struct thread *td = curthread;
1636 struct nameidata nd;
1640 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1641 UIO_USERSPACE, uap->path, td);
1643 error = kern_stat(&nd, &st);
1646 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1651 * lstat_args(char *path, struct stat *ub)
1653 * Get file status; this version does not follow links.
1656 lstat(struct lstat_args *uap)
1658 struct thread *td = curthread;
1659 struct nameidata nd;
1663 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1664 UIO_USERSPACE, SCARG(uap, path), td);
1666 error = kern_stat(&nd, &st);
1669 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1678 nsb->st_dev = sb->st_dev;
1679 nsb->st_ino = sb->st_ino;
1680 nsb->st_mode = sb->st_mode;
1681 nsb->st_nlink = sb->st_nlink;
1682 nsb->st_uid = sb->st_uid;
1683 nsb->st_gid = sb->st_gid;
1684 nsb->st_rdev = sb->st_rdev;
1685 nsb->st_atimespec = sb->st_atimespec;
1686 nsb->st_mtimespec = sb->st_mtimespec;
1687 nsb->st_ctimespec = sb->st_ctimespec;
1688 nsb->st_size = sb->st_size;
1689 nsb->st_blocks = sb->st_blocks;
1690 nsb->st_blksize = sb->st_blksize;
1691 nsb->st_flags = sb->st_flags;
1692 nsb->st_gen = sb->st_gen;
1693 nsb->st_qspare[0] = sb->st_qspare[0];
1694 nsb->st_qspare[1] = sb->st_qspare[1];
1698 * nstat_args(char *path, struct nstat *ub)
1702 nstat(struct nstat_args *uap)
1704 struct thread *td = curthread;
1708 struct nameidata nd;
1710 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1711 UIO_USERSPACE, SCARG(uap, path), td);
1712 if ((error = namei(&nd)) != 0)
1714 NDFREE(&nd, NDF_ONLY_PNBUF);
1715 error = vn_stat(nd.ni_vp, &sb, td);
1719 cvtnstat(&sb, &nsb);
1720 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1725 * lstat_args(char *path, struct stat *ub)
1727 * Get file status; this version does not follow links.
1731 nlstat(struct nlstat_args *uap)
1733 struct thread *td = curthread;
1738 struct nameidata nd;
1740 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1741 UIO_USERSPACE, SCARG(uap, path), td);
1742 if ((error = namei(&nd)) != 0)
1745 NDFREE(&nd, NDF_ONLY_PNBUF);
1746 error = vn_stat(vp, &sb, td);
1750 cvtnstat(&sb, &nsb);
1751 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1756 * pathconf_Args(char *path, int name)
1758 * Get configurable pathname variables.
1762 pathconf(struct pathconf_args *uap)
1764 struct thread *td = curthread;
1766 struct nameidata nd;
1768 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1769 UIO_USERSPACE, SCARG(uap, path), td);
1770 if ((error = namei(&nd)) != 0)
1772 NDFREE(&nd, NDF_ONLY_PNBUF);
1773 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1780 * kern_readlink isn't properly split yet. There is a copyin burried
1781 * in VOP_READLINK().
1784 kern_readlink(struct nameidata *nd, char *buf, int count, int *res)
1786 struct thread *td = curthread;
1787 struct proc *p = td->td_proc;
1796 NDFREE(nd, NDF_ONLY_PNBUF);
1798 if (vp->v_type != VLNK)
1801 aiov.iov_base = buf;
1802 aiov.iov_len = count;
1803 auio.uio_iov = &aiov;
1804 auio.uio_iovcnt = 1;
1805 auio.uio_offset = 0;
1806 auio.uio_rw = UIO_READ;
1807 auio.uio_segflg = UIO_USERSPACE;
1809 auio.uio_resid = count;
1810 error = VOP_READLINK(vp, &auio, p->p_ucred);
1813 *res = count - auio.uio_resid;
1818 * readlink_args(char *path, char *buf, int count)
1820 * Return target name of a symbolic link.
1823 readlink(struct readlink_args *uap)
1825 struct thread *td = curthread;
1826 struct nameidata nd;
1829 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ, UIO_USERSPACE,
1832 error = kern_readlink(&nd, uap->buf, uap->count,
1833 &uap->sysmsg_result);
1839 setfflags(struct vnode *vp, int flags)
1841 struct thread *td = curthread;
1842 struct proc *p = td->td_proc;
1847 * Prevent non-root users from setting flags on devices. When
1848 * a device is reused, users can retain ownership of the device
1849 * if they are allowed to set flags and programs assume that
1850 * chown can't fail when done as root.
1852 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1853 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1856 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1857 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1859 vattr.va_flags = flags;
1860 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1861 VOP_UNLOCK(vp, NULL, 0, td);
1866 * chflags(char *path, int flags)
1868 * Change flags of a file given a path name.
1872 chflags(struct chflags_args *uap)
1874 struct thread *td = curthread;
1876 struct nameidata nd;
1878 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1879 SCARG(uap, path), td);
1880 if ((error = namei(&nd)) != 0)
1882 NDFREE(&nd, NDF_ONLY_PNBUF);
1883 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1889 * fchflags_args(int fd, int flags)
1891 * Change flags of a file given a file descriptor.
1895 fchflags(struct fchflags_args *uap)
1897 struct thread *td = curthread;
1898 struct proc *p = td->td_proc;
1902 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1904 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1908 setfmode(struct vnode *vp, int mode)
1910 struct thread *td = curthread;
1911 struct proc *p = td->td_proc;
1915 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1916 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1918 vattr.va_mode = mode & ALLPERMS;
1919 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1920 VOP_UNLOCK(vp, NULL, 0, td);
1925 kern_chmod(struct nameidata *nd, int mode)
1932 NDFREE(nd, NDF_ONLY_PNBUF);
1933 error = setfmode(nd->ni_vp, mode);
1939 * chmod_args(char *path, int mode)
1941 * Change mode of a file given path name.
1945 chmod(struct chmod_args *uap)
1947 struct thread *td = curthread;
1948 struct nameidata nd;
1951 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1953 error = kern_chmod(&nd, uap->mode);
1959 * lchmod_args(char *path, int mode)
1961 * Change mode of a file given path name (don't follow links.)
1965 lchmod(struct lchmod_args *uap)
1967 struct thread *td = curthread;
1969 struct nameidata nd;
1971 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
1972 if ((error = namei(&nd)) != 0)
1974 NDFREE(&nd, NDF_ONLY_PNBUF);
1975 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1981 * fchmod_args(int fd, int mode)
1983 * Change mode of a file given a file descriptor.
1987 fchmod(struct fchmod_args *uap)
1989 struct thread *td = curthread;
1990 struct proc *p = td->td_proc;
1994 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1996 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
2000 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2002 struct thread *td = curthread;
2003 struct proc *p = td->td_proc;
2007 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2008 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2012 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2013 VOP_UNLOCK(vp, NULL, 0, td);
2018 kern_chown(struct nameidata *nd, int uid, int gid)
2025 NDFREE(nd, NDF_ONLY_PNBUF);
2026 error = setfown(nd->ni_vp, uid, gid);
2032 * chown(char *path, int uid, int gid)
2034 * Set ownership given a path name.
2037 chown(struct chown_args *uap)
2039 struct thread *td = curthread;
2040 struct nameidata nd;
2043 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2045 error = kern_chown(&nd, uap->uid, uap->gid);
2051 * lchown_args(char *path, int uid, int gid)
2053 * Set ownership given a path name, do not cross symlinks.
2056 lchown(struct lchown_args *uap)
2058 struct thread *td = curthread;
2060 struct nameidata nd;
2062 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2064 error = kern_chown(&nd, uap->uid, uap->gid);
2070 * fchown_args(int fd, int uid, int gid)
2072 * Set ownership given a file descriptor.
2076 fchown(struct fchown_args *uap)
2078 struct thread *td = curthread;
2079 struct proc *p = td->td_proc;
2083 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2085 return setfown((struct vnode *)fp->f_data,
2086 SCARG(uap, uid), SCARG(uap, gid));
2090 getutimes(const struct timeval *tvp, struct timespec *tsp)
2092 struct timeval tv[2];
2096 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2099 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2100 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2106 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2108 struct thread *td = curthread;
2109 struct proc *p = td->td_proc;
2113 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2114 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2116 vattr.va_atime = ts[0];
2117 vattr.va_mtime = ts[1];
2119 vattr.va_vaflags |= VA_UTIMES_NULL;
2120 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2121 VOP_UNLOCK(vp, NULL, 0, td);
2126 kern_utimes(struct nameidata *nd, struct timeval *tptr)
2128 struct timespec ts[2];
2131 error = getutimes(tptr, ts);
2137 NDFREE(nd, NDF_ONLY_PNBUF);
2138 error = setutimes(nd->ni_vp, ts, tptr == NULL);
2144 * utimes_args(char *path, struct timeval *tptr)
2146 * Set the access and modification times of a file.
2149 utimes(struct utimes_args *uap)
2151 struct thread *td = curthread;
2152 struct timeval tv[2];
2153 struct nameidata nd;
2157 error = copyin(uap->tptr, tv, sizeof(tv));
2161 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2163 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2169 * lutimes_args(char *path, struct timeval *tptr)
2171 * Set the access and modification times of a file.
2174 lutimes(struct lutimes_args *uap)
2176 struct thread *td = curthread;
2177 struct timeval tv[2];
2178 struct nameidata nd;
2182 error = copyin(uap->tptr, tv, sizeof(tv));
2186 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2188 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2194 kern_futimes(int fd, struct timeval *tptr)
2196 struct thread *td = curthread;
2197 struct proc *p = td->td_proc;
2198 struct timespec ts[2];
2202 error = getutimes(tptr, ts);
2205 error = getvnode(p->p_fd, fd, &fp);
2208 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2213 * futimes_args(int fd, struct timeval *tptr)
2215 * Set the access and modification times of a file.
2218 futimes(struct futimes_args *uap)
2220 struct timeval tv[2];
2224 error = copyin(uap->tptr, tv, sizeof(tv));
2229 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2235 kern_truncate(struct nameidata* nd, off_t length)
2237 struct thread *td = curthread;
2238 struct proc *p = td->td_proc;
2245 if ((error = namei(nd)) != 0)
2248 NDFREE(nd, NDF_ONLY_PNBUF);
2249 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2250 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2251 if (vp->v_type == VDIR)
2253 else if ((error = vn_writechk(vp)) == 0 &&
2254 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2256 vattr.va_size = length;
2257 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2264 * truncate(char *path, int pad, off_t length)
2266 * Truncate a file given its path name.
2269 truncate(struct truncate_args *uap)
2271 struct thread *td = curthread;
2272 struct nameidata nd;
2275 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2277 error = kern_truncate(&nd, uap->length);
2283 kern_ftruncate(int fd, off_t length)
2285 struct thread *td = curthread;
2286 struct proc *p = td->td_proc;
2294 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2296 if ((fp->f_flag & FWRITE) == 0)
2298 vp = (struct vnode *)fp->f_data;
2299 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2300 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2301 if (vp->v_type == VDIR)
2303 else if ((error = vn_writechk(vp)) == 0) {
2305 vattr.va_size = length;
2306 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2308 VOP_UNLOCK(vp, NULL, 0, td);
2313 * ftruncate_args(int fd, int pad, off_t length)
2315 * Truncate a file given a file descriptor.
2318 ftruncate(struct ftruncate_args *uap)
2322 error = kern_ftruncate(uap->fd, uap->length);
2330 * Sync an open file.
2334 fsync(struct fsync_args *uap)
2336 struct thread *td = curthread;
2337 struct proc *p = td->td_proc;
2343 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2345 vp = (struct vnode *)fp->f_data;
2346 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2347 if (VOP_GETVOBJECT(vp, &obj) == 0)
2348 vm_object_page_clean(obj, 0, 0, 0);
2349 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2350 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2352 error = (*bioops.io_fsync)(vp);
2353 VOP_UNLOCK(vp, NULL, 0, td);
2358 kern_rename(struct nameidata *fromnd, struct nameidata *tond)
2360 struct thread *td = curthread;
2361 struct proc *p = td->td_proc;
2362 struct vnode *tvp, *fvp, *tdvp;
2366 error = namei(fromnd);
2369 fvp = fromnd->ni_vp;
2370 if (fromnd->ni_vp->v_type == VDIR)
2371 tond->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2372 error = namei(tond);
2374 /* Translate error code for rename("dir1", "dir2/."). */
2375 if (error == EISDIR && fvp->v_type == VDIR)
2377 NDFREE(fromnd, NDF_ONLY_PNBUF);
2378 vrele(fromnd->ni_dvp);
2382 tdvp = tond->ni_dvp;
2385 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2388 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2396 * If the source is the same as the destination (that is, if they
2397 * are links to the same vnode), then there is nothing to do.
2403 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2404 if (fromnd->ni_dvp != tdvp) {
2405 VOP_LEASE(fromnd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2408 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2410 error = VOP_RENAME(fromnd->ni_dvp, NCPNULL, fromnd->ni_vp,
2411 &fromnd->ni_cnd, tond->ni_dvp, NCPNULL, tond->ni_vp,
2413 NDFREE(fromnd, NDF_ONLY_PNBUF);
2414 NDFREE(tond, NDF_ONLY_PNBUF);
2416 NDFREE(fromnd, NDF_ONLY_PNBUF);
2417 NDFREE(tond, NDF_ONLY_PNBUF);
2424 vrele(fromnd->ni_dvp);
2427 vrele(tond->ni_startdir);
2428 ASSERT_VOP_UNLOCKED(fromnd->ni_dvp, "rename");
2429 ASSERT_VOP_UNLOCKED(fromnd->ni_vp, "rename");
2430 ASSERT_VOP_UNLOCKED(tond->ni_dvp, "rename");
2431 ASSERT_VOP_UNLOCKED(tond->ni_vp, "rename");
2433 if (fromnd->ni_startdir)
2434 vrele(fromnd->ni_startdir);
2441 * rename_args(char *from, char *to)
2443 * Rename files. Source and destination must either both be directories,
2444 * or both not be directories. If target is a directory, it must be empty.
2447 rename(struct rename_args *uap)
2449 struct thread *td = curthread;
2450 struct nameidata fromnd, tond;
2453 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2454 UIO_USERSPACE, uap->from, td);
2455 NDINIT(&tond, NAMEI_RENAME,
2456 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2457 CNP_SAVESTART | CNP_NOOBJ,
2458 UIO_USERSPACE, uap->to, td);
2460 error = kern_rename(&fromnd, &tond);
2466 kern_mkdir(struct nameidata *nd, int mode)
2468 struct thread *td = curthread;
2469 struct proc *p = td->td_proc;
2475 nd->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2481 NDFREE(nd, NDF_ONLY_PNBUF);
2482 if (nd->ni_dvp == vp)
2490 vattr.va_type = VDIR;
2491 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2492 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2493 error = VOP_MKDIR(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
2495 NDFREE(nd, NDF_ONLY_PNBUF);
2499 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mkdir");
2500 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mkdir");
2505 * mkdir_args(char *path, int mode)
2507 * Make a directory file.
2511 mkdir(struct mkdir_args *uap)
2513 struct thread *td = curthread;
2514 struct nameidata nd;
2517 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
2520 error = kern_mkdir(&nd, uap->mode);
2526 kern_rmdir(struct nameidata *nd)
2528 struct thread *td = curthread;
2529 struct proc *p = td->td_proc;
2538 if (vp->v_type != VDIR) {
2543 * No rmdir "." please.
2545 if (nd->ni_dvp == vp) {
2550 * The root of a mounted filesystem cannot be deleted.
2552 if (vp->v_flag & VROOT)
2555 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2556 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2557 error = VOP_RMDIR(nd->ni_dvp, NCPNULL, nd->ni_vp,
2561 NDFREE(nd, NDF_ONLY_PNBUF);
2562 if (nd->ni_dvp == vp)
2568 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "rmdir");
2569 ASSERT_VOP_UNLOCKED(nd->ni_vp, "rmdir");
2574 * rmdir_args(char *path)
2576 * Remove a directory file.
2580 rmdir(struct rmdir_args *uap)
2582 struct thread *td = curthread;
2583 struct nameidata nd;
2586 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF,
2587 UIO_USERSPACE, uap->path, td);
2589 error = kern_rmdir(&nd);
2595 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2597 struct thread *td = curthread;
2598 struct proc *p = td->td_proc;
2606 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2608 if ((fp->f_flag & FREAD) == 0)
2610 vp = (struct vnode *)fp->f_data;
2612 if (vp->v_type != VDIR)
2614 aiov.iov_base = buf;
2615 aiov.iov_len = count;
2616 auio.uio_iov = &aiov;
2617 auio.uio_iovcnt = 1;
2618 auio.uio_rw = UIO_READ;
2619 auio.uio_segflg = UIO_USERSPACE;
2621 auio.uio_resid = count;
2622 /* vn_lock(vp, NULL, LK_SHARED | LK_RETRY, td); */
2623 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2624 loff = auio.uio_offset = fp->f_offset;
2625 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2626 fp->f_offset = auio.uio_offset;
2627 VOP_UNLOCK(vp, NULL, 0, td);
2630 if (count == auio.uio_resid) {
2631 if (union_dircheckp) {
2632 error = union_dircheckp(td, &vp, fp);
2638 if ((vp->v_flag & VROOT) &&
2639 (vp->v_mount->mnt_flag & MNT_UNION)) {
2640 struct vnode *tvp = vp;
2641 vp = vp->v_mount->mnt_vnodecovered;
2643 fp->f_data = (caddr_t)vp;
2652 *res = count - auio.uio_resid;
2657 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2659 * Read a block of directory entries in a file system independent format.
2662 getdirentries(struct getdirentries_args *uap)
2667 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2668 &uap->sysmsg_result);
2671 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2676 * getdents_args(int fd, char *buf, size_t count)
2679 getdents(struct getdents_args *uap)
2683 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2684 &uap->sysmsg_result);
2690 * umask(int newmask)
2692 * Set the mode mask for creation of filesystem nodes.
2697 umask(struct umask_args *uap)
2699 struct thread *td = curthread;
2700 struct proc *p = td->td_proc;
2701 struct filedesc *fdp;
2704 uap->sysmsg_result = fdp->fd_cmask;
2705 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2710 * revoke(char *path)
2712 * Void all references to file by ripping underlying filesystem
2717 revoke(struct revoke_args *uap)
2719 struct thread *td = curthread;
2720 struct proc *p = td->td_proc;
2724 struct nameidata nd;
2726 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2727 if ((error = namei(&nd)) != 0)
2730 NDFREE(&nd, NDF_ONLY_PNBUF);
2731 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2735 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2737 if (p->p_ucred->cr_uid != vattr.va_uid &&
2738 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2740 if (count_udev(vp->v_udev) > 0)
2741 VOP_REVOKE(vp, REVOKEALL);
2748 * Convert a user file descriptor to a kernel file entry.
2751 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2755 if ((u_int)fd >= fdp->fd_nfiles ||
2756 (fp = fdp->fd_ofiles[fd]) == NULL)
2758 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2764 * getfh_args(char *fname, fhandle_t *fhp)
2766 * Get (NFS) file handle
2769 getfh(struct getfh_args *uap)
2771 struct thread *td = curthread;
2772 struct nameidata nd;
2778 * Must be super user
2783 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2787 NDFREE(&nd, NDF_ONLY_PNBUF);
2789 bzero(&fh, sizeof(fh));
2790 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2791 error = VFS_VPTOFH(vp, &fh.fh_fid);
2795 error = copyout(&fh, uap->fhp, sizeof (fh));
2800 * fhopen_args(const struct fhandle *u_fhp, int flags)
2802 * syscall for the rpc.lockd to use to translate a NFS file handle into
2803 * an open descriptor.
2805 * warning: do not remove the suser() call or this becomes one giant
2809 fhopen(struct fhopen_args *uap)
2811 struct thread *td = curthread;
2812 struct proc *p = td->td_proc;
2817 struct vattr *vap = &vat;
2820 struct filedesc *fdp = p->p_fd;
2821 int fmode, mode, error, type;
2826 * Must be super user
2832 fmode = FFLAGS(SCARG(uap, flags));
2833 /* why not allow a non-read/write open for our lockd? */
2834 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2836 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2839 /* find the mount point */
2840 mp = vfs_getvfs(&fhp.fh_fsid);
2843 /* now give me my vnode, it gets returned to me locked */
2844 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2848 * from now on we have to make sure not
2849 * to forget about the vnode
2850 * any error that causes an abort must vput(vp)
2851 * just set error = err and 'goto bad;'.
2857 if (vp->v_type == VLNK) {
2861 if (vp->v_type == VSOCK) {
2866 if (fmode & (FWRITE | O_TRUNC)) {
2867 if (vp->v_type == VDIR) {
2871 error = vn_writechk(vp);
2879 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2883 if (fmode & O_TRUNC) {
2884 VOP_UNLOCK(vp, NULL, 0, td); /* XXX */
2885 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2886 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2889 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2893 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2897 * Make sure that a VM object is created for VMIO support.
2899 if (vn_canvmio(vp) == TRUE) {
2900 if ((error = vfs_object_create(vp, td)) != 0)
2907 * end of vn_open code
2910 if ((error = falloc(p, &nfp, &indx)) != 0) {
2918 * hold an extra reference to avoid having fp ripped out
2919 * from under us while we block in the lock op.
2922 nfp->f_data = (caddr_t)vp;
2923 nfp->f_flag = fmode & FMASK;
2924 nfp->f_ops = &vnops;
2925 nfp->f_type = DTYPE_VNODE;
2926 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2927 lf.l_whence = SEEK_SET;
2930 if (fmode & O_EXLOCK)
2931 lf.l_type = F_WRLCK;
2933 lf.l_type = F_RDLCK;
2935 if ((fmode & FNONBLOCK) == 0)
2937 VOP_UNLOCK(vp, NULL, 0, td);
2938 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2940 * lock request failed. Normally close the descriptor
2941 * but handle the case where someone might have dup()d
2942 * or close()d it when we weren't looking.
2944 if (fdp->fd_ofiles[indx] == fp) {
2945 fdp->fd_ofiles[indx] = NULL;
2950 * release our private reference.
2955 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2956 fp->f_flag |= FHASLOCK;
2958 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2959 vfs_object_create(vp, td);
2961 VOP_UNLOCK(vp, NULL, 0, td);
2963 uap->sysmsg_result = indx;
2972 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2975 fhstat(struct fhstat_args *uap)
2977 struct thread *td = curthread;
2985 * Must be super user
2991 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
2995 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2997 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
2999 error = vn_stat(vp, &sb, td);
3003 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3008 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3011 fhstatfs(struct fhstatfs_args *uap)
3013 struct thread *td = curthread;
3022 * Must be super user
3024 if ((error = suser(td)))
3027 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3030 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3032 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3037 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3039 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3041 bcopy(sp, &sb, sizeof(sb));
3042 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3045 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3049 * Syscall to push extended attribute configuration information into the
3050 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3051 * a command (int cmd), and attribute name and misc data. For now, the
3052 * attribute name is left in userspace for consumption by the VFS_op.
3053 * It will probably be changed to be copied into sysspace by the
3054 * syscall in the future, once issues with various consumers of the
3055 * attribute code have raised their hands.
3057 * Currently this is used only by UFS Extended Attributes.
3060 extattrctl(struct extattrctl_args *uap)
3062 struct thread *td = curthread;
3063 struct nameidata nd;
3067 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3068 if ((error = namei(&nd)) != 0)
3070 mp = nd.ni_vp->v_mount;
3072 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3073 SCARG(uap, arg), td));
3077 * Syscall to set a named extended attribute on a file or directory.
3078 * Accepts attribute name, and a uio structure pointing to the data to set.
3079 * The uio is consumed in the style of writev(). The real work happens
3080 * in VOP_SETEXTATTR().
3083 extattr_set_file(struct extattr_set_file_args *uap)
3085 struct thread *td = curthread;
3086 struct proc *p = td->td_proc;
3087 struct nameidata nd;
3089 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3090 char attrname[EXTATTR_MAXNAMELEN];
3094 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3097 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3098 SCARG(uap, path), td);
3099 if ((error = namei(&nd)) != 0)
3101 iovlen = uap->iovcnt * sizeof(struct iovec);
3102 if (uap->iovcnt > UIO_SMALLIOV) {
3103 if (uap->iovcnt > UIO_MAXIOV) {
3107 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3112 auio.uio_iovcnt = uap->iovcnt;
3113 auio.uio_rw = UIO_WRITE;
3114 auio.uio_segflg = UIO_USERSPACE;
3116 auio.uio_offset = 0;
3117 if ((error = copyin(uap->iovp, iov, iovlen)))
3120 for (i = 0; i < uap->iovcnt; i++) {
3121 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3125 auio.uio_resid += iov->iov_len;
3128 cnt = auio.uio_resid;
3129 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3130 cnt -= auio.uio_resid;
3131 uap->sysmsg_result = cnt;
3134 FREE(needfree, M_IOV);
3140 * Syscall to get a named extended attribute on a file or directory.
3141 * Accepts attribute name, and a uio structure pointing to a buffer for the
3142 * data. The uio is consumed in the style of readv(). The real work
3143 * happens in VOP_GETEXTATTR();
3146 extattr_get_file(struct extattr_get_file_args *uap)
3148 struct thread *td = curthread;
3149 struct proc *p = td->td_proc;
3150 struct nameidata nd;
3152 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3153 char attrname[EXTATTR_MAXNAMELEN];
3157 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3160 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3161 SCARG(uap, path), td);
3162 if ((error = namei(&nd)) != 0)
3164 iovlen = uap->iovcnt * sizeof (struct iovec);
3165 if (uap->iovcnt > UIO_SMALLIOV) {
3166 if (uap->iovcnt > UIO_MAXIOV) {
3170 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3177 auio.uio_iovcnt = uap->iovcnt;
3178 auio.uio_rw = UIO_READ;
3179 auio.uio_segflg = UIO_USERSPACE;
3181 auio.uio_offset = 0;
3182 if ((error = copyin(uap->iovp, iov, iovlen)))
3185 for (i = 0; i < uap->iovcnt; i++) {
3186 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3190 auio.uio_resid += iov->iov_len;
3193 cnt = auio.uio_resid;
3194 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3195 cnt -= auio.uio_resid;
3196 uap->sysmsg_result = cnt;
3199 FREE(needfree, M_IOV);
3205 * Syscall to delete a named extended attribute from a file or directory.
3206 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3209 extattr_delete_file(struct extattr_delete_file_args *uap)
3211 struct thread *td = curthread;
3212 struct proc *p = td->td_proc;
3213 struct nameidata nd;
3214 char attrname[EXTATTR_MAXNAMELEN];
3217 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3220 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3221 SCARG(uap, path), td);
3222 if ((error = namei(&nd)) != 0)
3224 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);
3230 * print out statistics from the current status of the buffer pool
3231 * this can be toggeled by the system control option debug.syncprt
3240 int counts[(MAXBSIZE / PAGE_SIZE) + 1];
3241 static char *bname[3] = { "LOCKED", "LRU", "AGE" };
3243 for (dp = bufqueues, i = 0; dp < &bufqueues[3]; dp++, i++) {
3245 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3248 TAILQ_FOREACH(bp, dp, b_freelist) {
3249 counts[bp->b_bufsize/PAGE_SIZE]++;
3253 printf("%s: total-%d", bname[i], count);
3254 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3256 printf(", %d-%d", j * PAGE_SIZE, counts[j]);