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.36 2004/05/21 16:21:57 drhodus Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/sysent.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/sysproto.h>
50 #include <sys/filedesc.h>
51 #include <sys/kernel.h>
52 #include <sys/fcntl.h>
54 #include <sys/linker.h>
56 #include <sys/unistd.h>
57 #include <sys/vnode.h>
59 #include <sys/namei.h>
60 #include <sys/dirent.h>
61 #include <sys/extattr.h>
62 #include <sys/kern_syscall.h>
64 #include <machine/limits.h>
65 #include <vfs/union/union.h>
66 #include <sys/sysctl.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_zone.h>
70 #include <vm/vm_page.h>
72 #include <sys/file2.h>
74 static int checkvp_chdir (struct vnode *vn, struct thread *td);
75 static void checkdirs (struct vnode *olddp);
76 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
77 static int getutimes (const struct timeval *, struct timespec *);
78 static int setfown (struct vnode *, uid_t, gid_t);
79 static int setfmode (struct vnode *, int);
80 static int setfflags (struct vnode *, int);
81 static int setutimes (struct vnode *, const struct timespec *, int);
82 static int usermount = 0; /* if 1, non-root can mount fs. */
84 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
86 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
89 * Virtual File System System Calls
93 * Mount a file system.
96 * mount_args(char *type, char *path, int flags, caddr_t data)
100 mount(struct mount_args *uap)
102 struct thread *td = curthread;
103 struct proc *p = td->td_proc;
106 struct vfsconf *vfsp;
107 int error, flag = 0, flag2 = 0;
110 char fstypename[MFSNAMELEN];
115 if (p->p_ucred->cr_prison != NULL)
117 if (usermount == 0 && (error = suser(td)))
120 * Do not allow NFS export by non-root users.
122 if (SCARG(uap, flags) & MNT_EXPORTED) {
128 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
131 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
133 * Get vnode to be covered
135 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
136 SCARG(uap, path), td);
137 if ((error = namei(&nd)) != 0)
139 NDFREE(&nd, NDF_ONLY_PNBUF);
141 if (SCARG(uap, flags) & MNT_UPDATE) {
142 if ((vp->v_flag & VROOT) == 0) {
148 flag2 = mp->mnt_kern_flag;
150 * We only allow the filesystem to be reloaded if it
151 * is currently mounted read-only.
153 if ((SCARG(uap, flags) & MNT_RELOAD) &&
154 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
156 return (EOPNOTSUPP); /* Needs translation */
159 * Only root, or the user that did the original mount is
160 * permitted to update it.
162 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
163 (error = suser(td))) {
167 if (vfs_busy(mp, LK_NOWAIT, NULL, td)) {
171 lwkt_gettoken(&vlock, vp->v_interlock);
172 if ((vp->v_flag & VMOUNT) != 0 ||
173 vp->v_mountedhere != NULL) {
174 lwkt_reltoken(&vlock);
179 vp->v_flag |= VMOUNT;
180 lwkt_reltoken(&vlock);
182 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
183 VOP_UNLOCK(vp, NULL, 0, td);
187 * If the user is not root, ensure that they own the directory
188 * onto which we are attempting to mount.
190 if ((error = VOP_GETATTR(vp, &va, td)) ||
191 (va.va_uid != p->p_ucred->cr_uid &&
192 (error = suser(td)))) {
196 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
200 if (vp->v_type != VDIR) {
204 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
208 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
209 if (!strcmp(vfsp->vfc_name, fstypename))
214 /* Only load modules for root (very important!) */
215 if ((error = suser(td)) != 0) {
219 error = linker_load_file(fstypename, &lf);
220 if (error || lf == NULL) {
227 /* lookup again, see if the VFS was loaded */
228 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
229 if (!strcmp(vfsp->vfc_name, fstypename))
233 linker_file_unload(lf);
238 lwkt_gettoken(&vlock, vp->v_interlock);
239 if ((vp->v_flag & VMOUNT) != 0 ||
240 vp->v_mountedhere != NULL) {
241 lwkt_reltoken(&vlock);
245 vp->v_flag |= VMOUNT;
246 lwkt_reltoken(&vlock);
249 * Allocate and initialize the filesystem.
251 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
252 bzero((char *)mp, (u_long)sizeof(struct mount));
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 lwkt_gettoken(&vlock, vp->v_interlock);
327 vp->v_flag &= ~VMOUNT;
328 lwkt_reltoken(&vlock);
329 mp->mnt_vfc->vfc_refcount--;
338 * Scan all active processes to see if any of them have a current
339 * or root directory onto which the new filesystem has just been
340 * mounted. If so, replace them with the new mount point.
343 checkdirs(struct vnode *olddp)
345 struct filedesc *fdp;
349 if (olddp->v_usecount == 1)
351 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
352 panic("mount: lost mount");
353 FOREACH_PROC_IN_SYSTEM(p) {
355 if (fdp->fd_cdir == olddp) {
358 fdp->fd_cdir = newdp;
360 if (fdp->fd_rdir == olddp) {
363 fdp->fd_rdir = newdp;
366 if (rootvnode == olddp) {
370 vfs_cache_setroot(rootvnode);
376 * Unmount a file system.
378 * Note: unmount takes a path to the vnode mounted on as argument,
379 * not special file (as before).
382 * umount_args(char *path, int flags)
386 unmount(struct unmount_args *uap)
388 struct thread *td = curthread;
389 struct proc *p = td->td_proc;
396 if (p->p_ucred->cr_prison != NULL)
398 if (usermount == 0 && (error = suser(td)))
401 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
402 SCARG(uap, path), td);
403 if ((error = namei(&nd)) != 0)
406 NDFREE(&nd, NDF_ONLY_PNBUF);
410 * Only root, or the user that did the original mount is
411 * permitted to unmount this filesystem.
413 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
414 (error = suser(td))) {
420 * Don't allow unmounting the root file system.
422 if (mp->mnt_flag & MNT_ROOTFS) {
428 * Must be the root of the filesystem
430 if ((vp->v_flag & VROOT) == 0) {
435 return (dounmount(mp, SCARG(uap, flags), td));
439 * Do the actual file system unmount.
442 dounmount(struct mount *mp, int flags, struct thread *td)
444 struct vnode *coveredvp;
449 lwkt_gettoken(&ilock, &mountlist_token);
450 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
451 lwkt_reltoken(&ilock);
454 mp->mnt_kern_flag |= MNTK_UNMOUNT;
455 /* Allow filesystems to detect that a forced unmount is in progress. */
456 if (flags & MNT_FORCE)
457 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
458 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
459 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
461 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
462 if (mp->mnt_kern_flag & MNTK_MWAIT)
467 if (mp->mnt_flag & MNT_EXPUBLIC)
468 vfs_setpublicfs(NULL, NULL, NULL);
470 vfs_msync(mp, MNT_WAIT);
471 async_flag = mp->mnt_flag & MNT_ASYNC;
472 mp->mnt_flag &=~ MNT_ASYNC;
473 cache_purgevfs(mp); /* remove cache entries for this file sys */
474 if (mp->mnt_syncer != NULL)
475 vrele(mp->mnt_syncer);
476 if (((mp->mnt_flag & MNT_RDONLY) ||
477 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
479 error = VFS_UNMOUNT(mp, flags, td);
480 lwkt_gettokref(&ilock);
482 if (mp->mnt_syncer == NULL)
483 vfs_allocate_syncvnode(mp);
484 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
485 mp->mnt_flag |= async_flag;
486 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
488 if (mp->mnt_kern_flag & MNTK_MWAIT)
492 TAILQ_REMOVE(&mountlist, mp, mnt_list);
493 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
494 coveredvp->v_mountedhere = NULL;
497 mp->mnt_vfc->vfc_refcount--;
498 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
499 panic("unmount: dangling vnode");
500 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
501 if (mp->mnt_kern_flag & MNTK_MWAIT)
508 * Sync each mounted filesystem.
512 static int syncprt = 0;
513 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
518 sync(struct sync_args *uap)
520 struct thread *td = curthread;
521 struct mount *mp, *nmp;
525 lwkt_gettoken(&ilock, &mountlist_token);
526 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
527 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
528 nmp = TAILQ_NEXT(mp, mnt_list);
531 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
532 asyncflag = mp->mnt_flag & MNT_ASYNC;
533 mp->mnt_flag &= ~MNT_ASYNC;
534 vfs_msync(mp, MNT_NOWAIT);
535 VFS_SYNC(mp, MNT_NOWAIT, td);
536 mp->mnt_flag |= asyncflag;
538 lwkt_gettokref(&ilock);
539 nmp = TAILQ_NEXT(mp, mnt_list);
542 lwkt_reltoken(&ilock);
544 * print out buffer pool stat information on each sync() call.
553 /* XXX PRISON: could be per prison flag */
554 static int prison_quotas;
556 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
560 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
562 * Change filesystem quotas.
566 quotactl(struct quotactl_args *uap)
568 struct thread *td = curthread;
569 struct proc *p = td->td_proc;
575 if (p->p_ucred->cr_prison && !prison_quotas)
577 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
578 SCARG(uap, path), td);
579 if ((error = namei(&nd)) != 0)
581 mp = nd.ni_vp->v_mount;
582 NDFREE(&nd, NDF_ONLY_PNBUF);
584 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
585 SCARG(uap, arg), td));
589 kern_statfs(struct nameidata *nd, struct statfs *buf)
591 struct thread *td = curthread;
599 mp = nd->ni_vp->v_mount;
601 NDFREE(nd, NDF_ONLY_PNBUF);
603 error = VFS_STATFS(mp, sp, td);
606 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
607 bcopy(sp, buf, sizeof(*buf));
608 /* Only root should have access to the fsid's. */
610 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
615 * statfs_args(char *path, struct statfs *buf)
617 * Get filesystem statistics.
620 statfs(struct statfs_args *uap)
622 struct thread *td = curthread;
627 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
629 error = kern_statfs(&nd, &buf);
632 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
637 kern_fstatfs(int fd, struct statfs *buf)
639 struct thread *td = curthread;
640 struct proc *p = td->td_proc;
647 error = getvnode(p->p_fd, fd, &fp);
650 mp = ((struct vnode *)fp->f_data)->v_mount;
654 error = VFS_STATFS(mp, sp, td);
657 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
658 bcopy(sp, buf, sizeof(*buf));
659 /* Only root should have access to the fsid's. */
661 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
666 * fstatfs_args(int fd, struct statfs *buf)
668 * Get filesystem statistics.
671 fstatfs(struct fstatfs_args *uap)
676 error = kern_fstatfs(uap->fd, &buf);
679 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
684 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
686 * Get statistics on all filesystems.
690 getfsstat(struct getfsstat_args *uap)
692 struct thread *td = curthread;
693 struct mount *mp, *nmp;
697 long count, maxcount, error;
699 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
700 sfsp = (caddr_t)SCARG(uap, buf);
702 lwkt_gettoken(&ilock, &mountlist_token);
703 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
704 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
705 nmp = TAILQ_NEXT(mp, mnt_list);
708 if (sfsp && count < maxcount) {
711 * If MNT_NOWAIT or MNT_LAZY is specified, do not
712 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
713 * overrides MNT_WAIT.
715 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
716 (SCARG(uap, flags) & MNT_WAIT)) &&
717 (error = VFS_STATFS(mp, sp, td))) {
718 lwkt_gettokref(&ilock);
719 nmp = TAILQ_NEXT(mp, mnt_list);
723 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
724 error = copyout(sp, sfsp, sizeof(*sp));
732 lwkt_gettokref(&ilock);
733 nmp = TAILQ_NEXT(mp, mnt_list);
736 lwkt_reltoken(&ilock);
737 if (sfsp && count > maxcount)
738 uap->sysmsg_result = maxcount;
740 uap->sysmsg_result = count;
745 * fchdir_args(int fd)
747 * Change current working directory to a given file descriptor.
751 fchdir(struct fchdir_args *uap)
753 struct thread *td = curthread;
754 struct proc *p = td->td_proc;
755 struct filedesc *fdp = p->p_fd;
756 struct vnode *vp, *tdp;
761 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
763 vp = (struct vnode *)fp->f_data;
765 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
766 if (vp->v_type != VDIR)
769 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
770 while (!error && (mp = vp->v_mountedhere) != NULL) {
771 if (vfs_busy(mp, 0, NULL, td))
773 error = VFS_ROOT(mp, &tdp);
784 VOP_UNLOCK(vp, NULL, 0, td);
791 kern_chdir(struct nameidata *nd)
793 struct thread *td = curthread;
794 struct proc *p = td->td_proc;
795 struct filedesc *fdp = p->p_fd;
798 if ((error = namei(nd)) != 0)
800 if ((error = checkvp_chdir(nd->ni_vp, td)) == 0) {
802 fdp->fd_cdir = nd->ni_vp;
805 NDFREE(nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
810 * chdir_args(char *path)
812 * Change current working directory (``.'').
815 chdir(struct chdir_args *uap)
817 struct thread *td = curthread;
821 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
824 error = kern_chdir(&nd);
830 * Helper function for raised chroot(2) security function: Refuse if
831 * any filedescriptors are open directories.
834 chroot_refuse_vdir_fds(fdp)
835 struct filedesc *fdp;
842 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
843 error = getvnode(fdp, fd, &fp);
846 vp = (struct vnode *)fp->f_data;
847 if (vp->v_type != VDIR)
855 * This sysctl determines if we will allow a process to chroot(2) if it
856 * has a directory open:
857 * 0: disallowed for all processes.
858 * 1: allowed for processes that were not already chroot(2)'ed.
859 * 2: allowed for all processes.
862 static int chroot_allow_open_directories = 1;
864 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
865 &chroot_allow_open_directories, 0, "");
868 * Chroot to the specified vnode. vp must be locked and referenced on
869 * call, and will be left locked and referenced on return. This routine
870 * may acquire additional refs on the vnode when associating it with
871 * the process's root and/or jail dirs.
874 kern_chroot(struct vnode *vp)
876 struct thread *td = curthread;
877 struct proc *p = td->td_proc;
878 struct filedesc *fdp = p->p_fd;
882 * Only root can chroot
884 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
888 * Disallow open directory descriptors (fchdir() breakouts).
890 if (chroot_allow_open_directories == 0 ||
891 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
892 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
897 * Check the validity of vp as a directory to change to and
898 * associate it with rdir/jdir.
900 if ((error = checkvp_chdir(vp, td)) == 0) {
904 if (fdp->fd_jdir == NULL) {
913 * chroot_args(char *path)
915 * Change notion of root (``/'') directory.
919 chroot(struct chroot_args *uap)
921 struct thread *td = curthread;
925 KKASSERT(td->td_proc);
926 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
927 SCARG(uap, path), td);
928 if ((error = namei(&nd)) == 0) {
929 error = kern_chroot(nd.ni_vp);
930 NDFREE(&nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
936 * Common routine for chroot and chdir. Given a locked, referenced vnode,
937 * determine whether it is legal to chdir to the vnode. The vnode's state
938 * is not changed by this call.
941 checkvp_chdir(struct vnode *vp, struct thread *td)
945 if (vp->v_type != VDIR)
948 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
953 kern_open(struct nameidata *nd, int oflags, int mode, int *res)
955 struct thread *td = curthread;
956 struct proc *p = td->td_proc;
957 struct filedesc *fdp = p->p_fd;
962 int type, indx, error;
965 if ((oflags & O_ACCMODE) == O_ACCMODE)
967 flags = FFLAGS(oflags);
968 error = falloc(p, &nfp, &indx);
972 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
973 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
975 * Bump the ref count to prevent another process from closing
976 * the descriptor while we are blocked in vn_open()
979 error = vn_open(nd, flags, cmode);
982 * release our own reference
987 * handle special fdopen() case. bleh. dupfdopen() is
988 * responsible for dropping the old contents of ofiles[indx]
991 if ((error == ENODEV || error == ENXIO) &&
992 p->p_dupfd >= 0 && /* XXX from fdopen */
994 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
999 * Clean up the descriptor, but only if another thread hadn't
1000 * replaced or closed it.
1002 if (fdp->fd_ofiles[indx] == fp) {
1003 fdp->fd_ofiles[indx] = NULL;
1007 if (error == ERESTART)
1012 NDFREE(nd, NDF_ONLY_PNBUF);
1016 * There should be 2 references on the file, one from the descriptor
1017 * table, and one for us.
1019 * Handle the case where someone closed the file (via its file
1020 * descriptor) while we were blocked. The end result should look
1021 * like opening the file succeeded but it was immediately closed.
1023 if (fp->f_count == 1) {
1024 KASSERT(fdp->fd_ofiles[indx] != fp,
1025 ("Open file descriptor lost all refs"));
1026 VOP_UNLOCK(vp, NULL, 0, td);
1027 vn_close(vp, flags & FMASK, td);
1033 fp->f_data = (caddr_t)vp;
1034 fp->f_flag = flags & FMASK;
1036 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1037 if (flags & (O_EXLOCK | O_SHLOCK)) {
1038 lf.l_whence = SEEK_SET;
1041 if (flags & O_EXLOCK)
1042 lf.l_type = F_WRLCK;
1044 lf.l_type = F_RDLCK;
1046 if ((flags & FNONBLOCK) == 0)
1048 VOP_UNLOCK(vp, NULL, 0, td);
1049 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1051 * lock request failed. Normally close the descriptor
1052 * but handle the case where someone might have dup()d
1053 * it when we weren't looking. One reference is
1054 * owned by the descriptor array, the other by us.
1056 if (fdp->fd_ofiles[indx] == fp) {
1057 fdp->fd_ofiles[indx] = NULL;
1063 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1064 fp->f_flag |= FHASLOCK;
1066 /* assert that vn_open created a backing object if one is needed */
1067 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1068 ("open: vmio vnode has no backing object after vn_open"));
1069 VOP_UNLOCK(vp, NULL, 0, td);
1072 * release our private reference, leaving the one associated with the
1073 * descriptor table intact.
1081 * open_args(char *path, int flags, int mode)
1083 * Check permissions, allocate an open file structure,
1084 * and call the device open routine if any.
1087 open(struct open_args *uap)
1089 struct thread *td = curthread;
1090 struct nameidata nd;
1093 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1095 error = kern_open(&nd, uap->flags, uap->mode, &uap->sysmsg_result);
1101 kern_mknod(struct nameidata *nd, int mode, int dev)
1103 struct thread *td = curthread;
1104 struct proc *p = td->td_proc;
1112 switch (mode & S_IFMT) {
1118 error = suser_cred(p->p_ucred, PRISON_ROOT);
1132 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1133 vattr.va_rdev = dev;
1136 switch (mode & S_IFMT) {
1137 case S_IFMT: /* used by badsect to flag bad sectors */
1138 vattr.va_type = VBAD;
1141 vattr.va_type = VCHR;
1144 vattr.va_type = VBLK;
1155 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1157 error = VOP_WHITEOUT(nd->ni_dvp, NCPNULL,
1158 &nd->ni_cnd, NAMEI_CREATE);
1160 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp,
1161 &nd->ni_cnd, &vattr);
1165 NDFREE(nd, NDF_ONLY_PNBUF);
1168 NDFREE(nd, NDF_ONLY_PNBUF);
1169 if (nd->ni_dvp == vp)
1176 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mknod");
1177 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mknod");
1182 * mknod_args(char *path, int mode, int dev)
1184 * Create a special file.
1187 mknod(struct mknod_args *uap)
1189 struct thread *td = curthread;
1190 struct nameidata nd;
1193 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1196 error = kern_mknod(&nd, uap->mode, uap->dev);
1202 kern_mkfifo(struct nameidata *nd, int mode)
1204 struct thread *td = curthread;
1205 struct proc *p = td->td_proc;
1213 if (nd->ni_vp != NULL) {
1214 NDFREE(nd, NDF_ONLY_PNBUF);
1215 if (nd->ni_dvp == nd->ni_vp)
1223 vattr.va_type = VFIFO;
1224 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1225 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1226 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd, &vattr);
1229 NDFREE(nd, NDF_ONLY_PNBUF);
1235 * mkfifo_args(char *path, int mode)
1237 * Create a named pipe.
1240 mkfifo(struct mkfifo_args *uap)
1242 struct thread *td = curthread;
1243 struct nameidata nd;
1246 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1249 error = kern_mkfifo(&nd, uap->mode);
1255 kern_link(struct nameidata *nd, struct nameidata *linknd)
1257 struct thread *td = curthread;
1258 struct proc *p = td->td_proc;
1266 NDFREE(nd, NDF_ONLY_PNBUF);
1268 if (vp->v_type == VDIR)
1269 error = EPERM; /* POSIX */
1271 error = namei(linknd);
1273 if (linknd->ni_vp != NULL) {
1275 vrele(linknd->ni_vp);
1278 VOP_LEASE(linknd->ni_dvp, td, p->p_ucred,
1280 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1281 error = VOP_LINK(linknd->ni_dvp, NCPNULL, vp,
1284 NDFREE(linknd, NDF_ONLY_PNBUF);
1285 if (linknd->ni_dvp == linknd->ni_vp)
1286 vrele(linknd->ni_dvp);
1288 vput(linknd->ni_dvp);
1289 ASSERT_VOP_UNLOCKED(linknd->ni_dvp, "link");
1290 ASSERT_VOP_UNLOCKED(linknd->ni_vp, "link");
1298 * link_args(char *path, char *link)
1300 * Make a hard file link.
1303 link(struct link_args *uap)
1305 struct thread *td = curthread;
1306 struct nameidata nd, linknd;
1309 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1311 NDINIT(&linknd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1312 UIO_USERSPACE, uap->link, td);
1314 error = kern_link(&nd, &linknd);
1320 kern_symlink(char *path, struct nameidata *nd)
1322 struct thread *td = curthread;
1323 struct proc *p = td->td_proc;
1332 NDFREE(nd, NDF_ONLY_PNBUF);
1333 if (nd->ni_dvp == nd->ni_vp)
1341 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1342 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1343 error = VOP_SYMLINK(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
1345 NDFREE(nd, NDF_ONLY_PNBUF);
1349 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "symlink");
1350 ASSERT_VOP_UNLOCKED(nd->ni_vp, "symlink");
1356 * symlink(char *path, char *link)
1358 * Make a symbolic link.
1361 symlink(struct symlink_args *uap)
1363 struct thread *td = curthread;
1364 struct nameidata nd;
1368 path = zalloc(namei_zone);
1369 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1371 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1372 UIO_USERSPACE, uap->link, td);
1373 error = kern_symlink(path, &nd);
1375 zfree(namei_zone, path);
1380 * undelete_args(char *path)
1382 * Delete a whiteout from the filesystem.
1386 undelete(struct undelete_args *uap)
1388 struct thread *td = curthread;
1389 struct proc *p = td->td_proc;
1391 struct nameidata nd;
1394 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
1395 SCARG(uap, path), td);
1400 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
1401 NDFREE(&nd, NDF_ONLY_PNBUF);
1402 if (nd.ni_dvp == nd.ni_vp)
1411 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1412 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
1413 NDFREE(&nd, NDF_ONLY_PNBUF);
1415 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1416 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1421 kern_unlink(struct nameidata *nd)
1423 struct thread *td = curthread;
1424 struct proc *p = td->td_proc;
1433 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1434 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1436 if (vp->v_type == VDIR)
1437 error = EPERM; /* POSIX */
1440 * The root of a mounted filesystem cannot be deleted.
1442 * XXX: can this only be a VDIR case?
1444 if (vp->v_flag & VROOT)
1449 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1450 error = VOP_REMOVE(nd->ni_dvp, NCPNULL, vp, &nd->ni_cnd);
1452 NDFREE(nd, NDF_ONLY_PNBUF);
1453 if (nd->ni_dvp == vp)
1459 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "unlink");
1460 ASSERT_VOP_UNLOCKED(nd->ni_vp, "unlink");
1465 * unlink_args(char *path)
1467 * Delete a name from the filesystem.
1470 unlink(struct unlink_args *uap)
1472 struct thread *td = curthread;
1473 struct nameidata nd;
1476 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1479 error = kern_unlink(&nd);
1485 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1487 struct thread *td = curthread;
1488 struct proc *p = td->td_proc;
1489 struct filedesc *fdp = p->p_fd;
1494 if (fd >= fdp->fd_nfiles ||
1495 (fp = fdp->fd_ofiles[fd]) == NULL)
1497 if (fp->f_type != DTYPE_VNODE)
1501 fp->f_offset += offset;
1504 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1507 fp->f_offset = offset + vattr.va_size;
1510 fp->f_offset = offset;
1515 *res = fp->f_offset;
1520 * lseek_args(int fd, int pad, off_t offset, int whence)
1522 * Reposition read/write file offset.
1525 lseek(struct lseek_args *uap)
1529 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1530 &uap->sysmsg_offset);
1536 kern_access(struct nameidata *nd, int aflags)
1538 struct thread *td = curthread;
1539 struct proc *p = td->td_proc;
1540 struct ucred *cred, *tmpcred;
1546 * Create and modify a temporary credential instead of one that
1547 * is potentially shared. This could also mess up socket
1548 * buffer accounting which can run in an interrupt context.
1550 tmpcred = crdup(cred);
1551 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1552 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1553 p->p_ucred = tmpcred;
1554 nd->ni_cnd.cn_cred = tmpcred;
1560 /* Flags == 0 means only check for existence. */
1569 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1570 error = VOP_ACCESS(vp, flags, tmpcred, td);
1572 NDFREE(nd, NDF_ONLY_PNBUF);
1581 * access_args(char *path, int flags)
1583 * Check access permissions.
1586 access(struct access_args *uap)
1588 struct thread *td = curthread;
1589 struct nameidata nd;
1592 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1593 UIO_USERSPACE, uap->path, td);
1595 error = kern_access(&nd, uap->flags);
1601 kern_stat(struct nameidata *nd, struct stat *st)
1603 struct thread *td = curthread;
1609 error = vn_stat(nd->ni_vp, st, td);
1610 NDFREE(nd, NDF_ONLY_PNBUF);
1616 * stat_args(char *path, struct stat *ub)
1618 * Get file status; this version follows links.
1621 stat(struct stat_args *uap)
1623 struct thread *td = curthread;
1624 struct nameidata nd;
1628 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1629 UIO_USERSPACE, uap->path, td);
1631 error = kern_stat(&nd, &st);
1634 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1639 * lstat_args(char *path, struct stat *ub)
1641 * Get file status; this version does not follow links.
1644 lstat(struct lstat_args *uap)
1646 struct thread *td = curthread;
1647 struct nameidata nd;
1651 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1652 UIO_USERSPACE, SCARG(uap, path), td);
1654 error = kern_stat(&nd, &st);
1657 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1666 nsb->st_dev = sb->st_dev;
1667 nsb->st_ino = sb->st_ino;
1668 nsb->st_mode = sb->st_mode;
1669 nsb->st_nlink = sb->st_nlink;
1670 nsb->st_uid = sb->st_uid;
1671 nsb->st_gid = sb->st_gid;
1672 nsb->st_rdev = sb->st_rdev;
1673 nsb->st_atimespec = sb->st_atimespec;
1674 nsb->st_mtimespec = sb->st_mtimespec;
1675 nsb->st_ctimespec = sb->st_ctimespec;
1676 nsb->st_size = sb->st_size;
1677 nsb->st_blocks = sb->st_blocks;
1678 nsb->st_blksize = sb->st_blksize;
1679 nsb->st_flags = sb->st_flags;
1680 nsb->st_gen = sb->st_gen;
1681 nsb->st_qspare[0] = sb->st_qspare[0];
1682 nsb->st_qspare[1] = sb->st_qspare[1];
1686 * nstat_args(char *path, struct nstat *ub)
1690 nstat(struct nstat_args *uap)
1692 struct thread *td = curthread;
1696 struct nameidata nd;
1698 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1699 UIO_USERSPACE, SCARG(uap, path), td);
1700 if ((error = namei(&nd)) != 0)
1702 NDFREE(&nd, NDF_ONLY_PNBUF);
1703 error = vn_stat(nd.ni_vp, &sb, td);
1707 cvtnstat(&sb, &nsb);
1708 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1713 * lstat_args(char *path, struct stat *ub)
1715 * Get file status; this version does not follow links.
1719 nlstat(struct nlstat_args *uap)
1721 struct thread *td = curthread;
1726 struct nameidata nd;
1728 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1729 UIO_USERSPACE, SCARG(uap, path), td);
1730 if ((error = namei(&nd)) != 0)
1733 NDFREE(&nd, NDF_ONLY_PNBUF);
1734 error = vn_stat(vp, &sb, td);
1738 cvtnstat(&sb, &nsb);
1739 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1744 * pathconf_Args(char *path, int name)
1746 * Get configurable pathname variables.
1750 pathconf(struct pathconf_args *uap)
1752 struct thread *td = curthread;
1754 struct nameidata nd;
1756 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1757 UIO_USERSPACE, SCARG(uap, path), td);
1758 if ((error = namei(&nd)) != 0)
1760 NDFREE(&nd, NDF_ONLY_PNBUF);
1761 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1768 * kern_readlink isn't properly split yet. There is a copyin burried
1769 * in VOP_READLINK().
1772 kern_readlink(struct nameidata *nd, char *buf, int count, int *res)
1774 struct thread *td = curthread;
1775 struct proc *p = td->td_proc;
1784 NDFREE(nd, NDF_ONLY_PNBUF);
1786 if (vp->v_type != VLNK)
1789 aiov.iov_base = buf;
1790 aiov.iov_len = count;
1791 auio.uio_iov = &aiov;
1792 auio.uio_iovcnt = 1;
1793 auio.uio_offset = 0;
1794 auio.uio_rw = UIO_READ;
1795 auio.uio_segflg = UIO_USERSPACE;
1797 auio.uio_resid = count;
1798 error = VOP_READLINK(vp, &auio, p->p_ucred);
1801 *res = count - auio.uio_resid;
1806 * readlink_args(char *path, char *buf, int count)
1808 * Return target name of a symbolic link.
1811 readlink(struct readlink_args *uap)
1813 struct thread *td = curthread;
1814 struct nameidata nd;
1817 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ, UIO_USERSPACE,
1820 error = kern_readlink(&nd, uap->buf, uap->count,
1821 &uap->sysmsg_result);
1827 setfflags(struct vnode *vp, int flags)
1829 struct thread *td = curthread;
1830 struct proc *p = td->td_proc;
1835 * Prevent non-root users from setting flags on devices. When
1836 * a device is reused, users can retain ownership of the device
1837 * if they are allowed to set flags and programs assume that
1838 * chown can't fail when done as root.
1840 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1841 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1844 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1845 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1847 vattr.va_flags = flags;
1848 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1849 VOP_UNLOCK(vp, NULL, 0, td);
1854 * chflags(char *path, int flags)
1856 * Change flags of a file given a path name.
1860 chflags(struct chflags_args *uap)
1862 struct thread *td = curthread;
1864 struct nameidata nd;
1866 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1867 SCARG(uap, path), td);
1868 if ((error = namei(&nd)) != 0)
1870 NDFREE(&nd, NDF_ONLY_PNBUF);
1871 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1877 * fchflags_args(int fd, int flags)
1879 * Change flags of a file given a file descriptor.
1883 fchflags(struct fchflags_args *uap)
1885 struct thread *td = curthread;
1886 struct proc *p = td->td_proc;
1890 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1892 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1896 setfmode(struct vnode *vp, int mode)
1898 struct thread *td = curthread;
1899 struct proc *p = td->td_proc;
1903 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1904 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1906 vattr.va_mode = mode & ALLPERMS;
1907 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1908 VOP_UNLOCK(vp, NULL, 0, td);
1913 kern_chmod(struct nameidata *nd, int mode)
1920 NDFREE(nd, NDF_ONLY_PNBUF);
1921 error = setfmode(nd->ni_vp, mode);
1927 * chmod_args(char *path, int mode)
1929 * Change mode of a file given path name.
1933 chmod(struct chmod_args *uap)
1935 struct thread *td = curthread;
1936 struct nameidata nd;
1939 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1941 error = kern_chmod(&nd, uap->mode);
1947 * lchmod_args(char *path, int mode)
1949 * Change mode of a file given path name (don't follow links.)
1953 lchmod(struct lchmod_args *uap)
1955 struct thread *td = curthread;
1957 struct nameidata nd;
1959 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
1960 if ((error = namei(&nd)) != 0)
1962 NDFREE(&nd, NDF_ONLY_PNBUF);
1963 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1969 * fchmod_args(int fd, int mode)
1971 * Change mode of a file given a file descriptor.
1975 fchmod(struct fchmod_args *uap)
1977 struct thread *td = curthread;
1978 struct proc *p = td->td_proc;
1982 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1984 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1988 setfown(struct vnode *vp, uid_t uid, gid_t gid)
1990 struct thread *td = curthread;
1991 struct proc *p = td->td_proc;
1995 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1996 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2000 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2001 VOP_UNLOCK(vp, NULL, 0, td);
2006 kern_chown(struct nameidata *nd, int uid, int gid)
2013 NDFREE(nd, NDF_ONLY_PNBUF);
2014 error = setfown(nd->ni_vp, uid, gid);
2020 * chown(char *path, int uid, int gid)
2022 * Set ownership given a path name.
2025 chown(struct chown_args *uap)
2027 struct thread *td = curthread;
2028 struct nameidata nd;
2031 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2033 error = kern_chown(&nd, uap->uid, uap->gid);
2039 * lchown_args(char *path, int uid, int gid)
2041 * Set ownership given a path name, do not cross symlinks.
2044 lchown(struct lchown_args *uap)
2046 struct thread *td = curthread;
2048 struct nameidata nd;
2050 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2052 error = kern_chown(&nd, uap->uid, uap->gid);
2058 * fchown_args(int fd, int uid, int gid)
2060 * Set ownership given a file descriptor.
2064 fchown(struct fchown_args *uap)
2066 struct thread *td = curthread;
2067 struct proc *p = td->td_proc;
2071 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2073 return setfown((struct vnode *)fp->f_data,
2074 SCARG(uap, uid), SCARG(uap, gid));
2078 getutimes(const struct timeval *tvp, struct timespec *tsp)
2080 struct timeval tv[2];
2084 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2087 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2088 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2094 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2096 struct thread *td = curthread;
2097 struct proc *p = td->td_proc;
2101 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2102 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2104 vattr.va_atime = ts[0];
2105 vattr.va_mtime = ts[1];
2107 vattr.va_vaflags |= VA_UTIMES_NULL;
2108 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2109 VOP_UNLOCK(vp, NULL, 0, td);
2114 kern_utimes(struct nameidata *nd, struct timeval *tptr)
2116 struct timespec ts[2];
2119 error = getutimes(tptr, ts);
2125 NDFREE(nd, NDF_ONLY_PNBUF);
2126 error = setutimes(nd->ni_vp, ts, tptr == NULL);
2132 * utimes_args(char *path, struct timeval *tptr)
2134 * Set the access and modification times of a file.
2137 utimes(struct utimes_args *uap)
2139 struct thread *td = curthread;
2140 struct timeval tv[2];
2141 struct nameidata nd;
2145 error = copyin(uap->tptr, tv, sizeof(tv));
2149 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2151 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2157 * lutimes_args(char *path, struct timeval *tptr)
2159 * Set the access and modification times of a file.
2162 lutimes(struct lutimes_args *uap)
2164 struct thread *td = curthread;
2165 struct timeval tv[2];
2166 struct nameidata nd;
2170 error = copyin(uap->tptr, tv, sizeof(tv));
2174 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2176 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2182 kern_futimes(int fd, struct timeval *tptr)
2184 struct thread *td = curthread;
2185 struct proc *p = td->td_proc;
2186 struct timespec ts[2];
2190 error = getutimes(tptr, ts);
2193 error = getvnode(p->p_fd, fd, &fp);
2196 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2201 * futimes_args(int fd, struct timeval *tptr)
2203 * Set the access and modification times of a file.
2206 futimes(struct futimes_args *uap)
2208 struct timeval tv[2];
2212 error = copyin(uap->tptr, tv, sizeof(tv));
2217 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2223 kern_truncate(struct nameidata* nd, off_t length)
2225 struct thread *td = curthread;
2226 struct proc *p = td->td_proc;
2233 if ((error = namei(nd)) != 0)
2236 NDFREE(nd, NDF_ONLY_PNBUF);
2237 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2238 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2239 if (vp->v_type == VDIR)
2241 else if ((error = vn_writechk(vp)) == 0 &&
2242 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2244 vattr.va_size = length;
2245 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2252 * truncate(char *path, int pad, off_t length)
2254 * Truncate a file given its path name.
2257 truncate(struct truncate_args *uap)
2259 struct thread *td = curthread;
2260 struct nameidata nd;
2263 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2265 error = kern_truncate(&nd, uap->length);
2271 kern_ftruncate(int fd, off_t length)
2273 struct thread *td = curthread;
2274 struct proc *p = td->td_proc;
2282 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2284 if ((fp->f_flag & FWRITE) == 0)
2286 vp = (struct vnode *)fp->f_data;
2287 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2288 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2289 if (vp->v_type == VDIR)
2291 else if ((error = vn_writechk(vp)) == 0) {
2293 vattr.va_size = length;
2294 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2296 VOP_UNLOCK(vp, NULL, 0, td);
2301 * ftruncate_args(int fd, int pad, off_t length)
2303 * Truncate a file given a file descriptor.
2306 ftruncate(struct ftruncate_args *uap)
2310 error = kern_ftruncate(uap->fd, uap->length);
2318 * Sync an open file.
2322 fsync(struct fsync_args *uap)
2324 struct thread *td = curthread;
2325 struct proc *p = td->td_proc;
2331 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2333 vp = (struct vnode *)fp->f_data;
2334 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2335 if (VOP_GETVOBJECT(vp, &obj) == 0)
2336 vm_object_page_clean(obj, 0, 0, 0);
2337 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2338 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2340 error = (*bioops.io_fsync)(vp);
2341 VOP_UNLOCK(vp, NULL, 0, td);
2346 kern_rename(struct nameidata *fromnd, struct nameidata *tond)
2348 struct thread *td = curthread;
2349 struct proc *p = td->td_proc;
2350 struct vnode *tvp, *fvp, *tdvp;
2354 error = namei(fromnd);
2357 fvp = fromnd->ni_vp;
2358 if (fromnd->ni_vp->v_type == VDIR)
2359 tond->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2360 error = namei(tond);
2362 /* Translate error code for rename("dir1", "dir2/."). */
2363 if (error == EISDIR && fvp->v_type == VDIR)
2365 NDFREE(fromnd, NDF_ONLY_PNBUF);
2366 vrele(fromnd->ni_dvp);
2370 tdvp = tond->ni_dvp;
2373 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2376 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2384 * If the source is the same as the destination (that is, if they
2385 * are links to the same vnode), then there is nothing to do.
2391 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2392 if (fromnd->ni_dvp != tdvp) {
2393 VOP_LEASE(fromnd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2396 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2398 error = VOP_RENAME(fromnd->ni_dvp, NCPNULL, fromnd->ni_vp,
2399 &fromnd->ni_cnd, tond->ni_dvp, NCPNULL, tond->ni_vp,
2401 NDFREE(fromnd, NDF_ONLY_PNBUF);
2402 NDFREE(tond, NDF_ONLY_PNBUF);
2404 NDFREE(fromnd, NDF_ONLY_PNBUF);
2405 NDFREE(tond, NDF_ONLY_PNBUF);
2412 vrele(fromnd->ni_dvp);
2415 vrele(tond->ni_startdir);
2416 ASSERT_VOP_UNLOCKED(fromnd->ni_dvp, "rename");
2417 ASSERT_VOP_UNLOCKED(fromnd->ni_vp, "rename");
2418 ASSERT_VOP_UNLOCKED(tond->ni_dvp, "rename");
2419 ASSERT_VOP_UNLOCKED(tond->ni_vp, "rename");
2421 if (fromnd->ni_startdir)
2422 vrele(fromnd->ni_startdir);
2429 * rename_args(char *from, char *to)
2431 * Rename files. Source and destination must either both be directories,
2432 * or both not be directories. If target is a directory, it must be empty.
2435 rename(struct rename_args *uap)
2437 struct thread *td = curthread;
2438 struct nameidata fromnd, tond;
2441 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2442 UIO_USERSPACE, uap->from, td);
2443 NDINIT(&tond, NAMEI_RENAME,
2444 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2445 CNP_SAVESTART | CNP_NOOBJ,
2446 UIO_USERSPACE, uap->to, td);
2448 error = kern_rename(&fromnd, &tond);
2454 kern_mkdir(struct nameidata *nd, int mode)
2456 struct thread *td = curthread;
2457 struct proc *p = td->td_proc;
2463 nd->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2469 NDFREE(nd, NDF_ONLY_PNBUF);
2470 if (nd->ni_dvp == vp)
2478 vattr.va_type = VDIR;
2479 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2480 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2481 error = VOP_MKDIR(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
2483 NDFREE(nd, NDF_ONLY_PNBUF);
2487 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mkdir");
2488 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mkdir");
2493 * mkdir_args(char *path, int mode)
2495 * Make a directory file.
2499 mkdir(struct mkdir_args *uap)
2501 struct thread *td = curthread;
2502 struct nameidata nd;
2505 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
2508 error = kern_mkdir(&nd, uap->mode);
2514 kern_rmdir(struct nameidata *nd)
2516 struct thread *td = curthread;
2517 struct proc *p = td->td_proc;
2526 if (vp->v_type != VDIR) {
2531 * No rmdir "." please.
2533 if (nd->ni_dvp == vp) {
2538 * The root of a mounted filesystem cannot be deleted.
2540 if (vp->v_flag & VROOT)
2543 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2544 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2545 error = VOP_RMDIR(nd->ni_dvp, NCPNULL, nd->ni_vp,
2549 NDFREE(nd, NDF_ONLY_PNBUF);
2550 if (nd->ni_dvp == vp)
2556 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "rmdir");
2557 ASSERT_VOP_UNLOCKED(nd->ni_vp, "rmdir");
2562 * rmdir_args(char *path)
2564 * Remove a directory file.
2568 rmdir(struct rmdir_args *uap)
2570 struct thread *td = curthread;
2571 struct nameidata nd;
2574 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF,
2575 UIO_USERSPACE, uap->path, td);
2577 error = kern_rmdir(&nd);
2583 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2585 struct thread *td = curthread;
2586 struct proc *p = td->td_proc;
2594 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2596 if ((fp->f_flag & FREAD) == 0)
2598 vp = (struct vnode *)fp->f_data;
2600 if (vp->v_type != VDIR)
2602 aiov.iov_base = buf;
2603 aiov.iov_len = count;
2604 auio.uio_iov = &aiov;
2605 auio.uio_iovcnt = 1;
2606 auio.uio_rw = UIO_READ;
2607 auio.uio_segflg = UIO_USERSPACE;
2609 auio.uio_resid = count;
2610 /* vn_lock(vp, NULL, LK_SHARED | LK_RETRY, td); */
2611 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2612 loff = auio.uio_offset = fp->f_offset;
2613 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2614 fp->f_offset = auio.uio_offset;
2615 VOP_UNLOCK(vp, NULL, 0, td);
2618 if (count == auio.uio_resid) {
2619 if (union_dircheckp) {
2620 error = union_dircheckp(td, &vp, fp);
2626 if ((vp->v_flag & VROOT) &&
2627 (vp->v_mount->mnt_flag & MNT_UNION)) {
2628 struct vnode *tvp = vp;
2629 vp = vp->v_mount->mnt_vnodecovered;
2631 fp->f_data = (caddr_t)vp;
2640 *res = count - auio.uio_resid;
2645 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2647 * Read a block of directory entries in a file system independent format.
2650 getdirentries(struct getdirentries_args *uap)
2655 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2656 &uap->sysmsg_result);
2659 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2664 * getdents_args(int fd, char *buf, size_t count)
2667 getdents(struct getdents_args *uap)
2671 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2672 &uap->sysmsg_result);
2678 * umask(int newmask)
2680 * Set the mode mask for creation of filesystem nodes.
2685 umask(struct umask_args *uap)
2687 struct thread *td = curthread;
2688 struct proc *p = td->td_proc;
2689 struct filedesc *fdp;
2692 uap->sysmsg_result = fdp->fd_cmask;
2693 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2698 * revoke(char *path)
2700 * Void all references to file by ripping underlying filesystem
2705 revoke(struct revoke_args *uap)
2707 struct thread *td = curthread;
2708 struct proc *p = td->td_proc;
2712 struct nameidata nd;
2714 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2715 if ((error = namei(&nd)) != 0)
2718 NDFREE(&nd, NDF_ONLY_PNBUF);
2719 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2723 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2725 if (p->p_ucred->cr_uid != vattr.va_uid &&
2726 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2728 if (count_udev(vp->v_udev) > 0)
2729 VOP_REVOKE(vp, REVOKEALL);
2736 * Convert a user file descriptor to a kernel file entry.
2739 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2743 if ((u_int)fd >= fdp->fd_nfiles ||
2744 (fp = fdp->fd_ofiles[fd]) == NULL)
2746 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2752 * getfh_args(char *fname, fhandle_t *fhp)
2754 * Get (NFS) file handle
2757 getfh(struct getfh_args *uap)
2759 struct thread *td = curthread;
2760 struct nameidata nd;
2766 * Must be super user
2771 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2775 NDFREE(&nd, NDF_ONLY_PNBUF);
2777 bzero(&fh, sizeof(fh));
2778 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2779 error = VFS_VPTOFH(vp, &fh.fh_fid);
2783 error = copyout(&fh, uap->fhp, sizeof (fh));
2788 * fhopen_args(const struct fhandle *u_fhp, int flags)
2790 * syscall for the rpc.lockd to use to translate a NFS file handle into
2791 * an open descriptor.
2793 * warning: do not remove the suser() call or this becomes one giant
2797 fhopen(struct fhopen_args *uap)
2799 struct thread *td = curthread;
2800 struct proc *p = td->td_proc;
2805 struct vattr *vap = &vat;
2808 struct filedesc *fdp = p->p_fd;
2809 int fmode, mode, error, type;
2814 * Must be super user
2820 fmode = FFLAGS(SCARG(uap, flags));
2821 /* why not allow a non-read/write open for our lockd? */
2822 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2824 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2827 /* find the mount point */
2828 mp = vfs_getvfs(&fhp.fh_fsid);
2831 /* now give me my vnode, it gets returned to me locked */
2832 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2836 * from now on we have to make sure not
2837 * to forget about the vnode
2838 * any error that causes an abort must vput(vp)
2839 * just set error = err and 'goto bad;'.
2845 if (vp->v_type == VLNK) {
2849 if (vp->v_type == VSOCK) {
2854 if (fmode & (FWRITE | O_TRUNC)) {
2855 if (vp->v_type == VDIR) {
2859 error = vn_writechk(vp);
2867 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2871 if (fmode & O_TRUNC) {
2872 VOP_UNLOCK(vp, NULL, 0, td); /* XXX */
2873 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2874 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2877 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2881 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2885 * Make sure that a VM object is created for VMIO support.
2887 if (vn_canvmio(vp) == TRUE) {
2888 if ((error = vfs_object_create(vp, td)) != 0)
2895 * end of vn_open code
2898 if ((error = falloc(p, &nfp, &indx)) != 0) {
2906 * hold an extra reference to avoid having fp ripped out
2907 * from under us while we block in the lock op.
2910 nfp->f_data = (caddr_t)vp;
2911 nfp->f_flag = fmode & FMASK;
2912 nfp->f_ops = &vnops;
2913 nfp->f_type = DTYPE_VNODE;
2914 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2915 lf.l_whence = SEEK_SET;
2918 if (fmode & O_EXLOCK)
2919 lf.l_type = F_WRLCK;
2921 lf.l_type = F_RDLCK;
2923 if ((fmode & FNONBLOCK) == 0)
2925 VOP_UNLOCK(vp, NULL, 0, td);
2926 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2928 * lock request failed. Normally close the descriptor
2929 * but handle the case where someone might have dup()d
2930 * or close()d it when we weren't looking.
2932 if (fdp->fd_ofiles[indx] == fp) {
2933 fdp->fd_ofiles[indx] = NULL;
2938 * release our private reference.
2943 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2944 fp->f_flag |= FHASLOCK;
2946 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2947 vfs_object_create(vp, td);
2949 VOP_UNLOCK(vp, NULL, 0, td);
2951 uap->sysmsg_result = indx;
2960 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2963 fhstat(struct fhstat_args *uap)
2965 struct thread *td = curthread;
2973 * Must be super user
2979 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
2983 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2985 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
2987 error = vn_stat(vp, &sb, td);
2991 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
2996 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
2999 fhstatfs(struct fhstatfs_args *uap)
3001 struct thread *td = curthread;
3010 * Must be super user
3012 if ((error = suser(td)))
3015 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3018 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3020 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3025 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3027 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3029 bcopy(sp, &sb, sizeof(sb));
3030 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3033 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3037 * Syscall to push extended attribute configuration information into the
3038 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3039 * a command (int cmd), and attribute name and misc data. For now, the
3040 * attribute name is left in userspace for consumption by the VFS_op.
3041 * It will probably be changed to be copied into sysspace by the
3042 * syscall in the future, once issues with various consumers of the
3043 * attribute code have raised their hands.
3045 * Currently this is used only by UFS Extended Attributes.
3048 extattrctl(struct extattrctl_args *uap)
3050 struct thread *td = curthread;
3051 struct nameidata nd;
3055 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3056 if ((error = namei(&nd)) != 0)
3058 mp = nd.ni_vp->v_mount;
3060 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3061 SCARG(uap, arg), td));
3065 * Syscall to set a named extended attribute on a file or directory.
3066 * Accepts attribute name, and a uio structure pointing to the data to set.
3067 * The uio is consumed in the style of writev(). The real work happens
3068 * in VOP_SETEXTATTR().
3071 extattr_set_file(struct extattr_set_file_args *uap)
3073 struct thread *td = curthread;
3074 struct proc *p = td->td_proc;
3075 struct nameidata nd;
3077 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3078 char attrname[EXTATTR_MAXNAMELEN];
3082 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3085 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3086 SCARG(uap, path), td);
3087 if ((error = namei(&nd)) != 0)
3089 iovlen = uap->iovcnt * sizeof(struct iovec);
3090 if (uap->iovcnt > UIO_SMALLIOV) {
3091 if (uap->iovcnt > UIO_MAXIOV) {
3095 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3100 auio.uio_iovcnt = uap->iovcnt;
3101 auio.uio_rw = UIO_WRITE;
3102 auio.uio_segflg = UIO_USERSPACE;
3104 auio.uio_offset = 0;
3105 if ((error = copyin(uap->iovp, iov, iovlen)))
3108 for (i = 0; i < uap->iovcnt; i++) {
3109 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3113 auio.uio_resid += iov->iov_len;
3116 cnt = auio.uio_resid;
3117 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3118 cnt -= auio.uio_resid;
3119 uap->sysmsg_result = cnt;
3122 FREE(needfree, M_IOV);
3128 * Syscall to get a named extended attribute on a file or directory.
3129 * Accepts attribute name, and a uio structure pointing to a buffer for the
3130 * data. The uio is consumed in the style of readv(). The real work
3131 * happens in VOP_GETEXTATTR();
3134 extattr_get_file(struct extattr_get_file_args *uap)
3136 struct thread *td = curthread;
3137 struct proc *p = td->td_proc;
3138 struct nameidata nd;
3140 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3141 char attrname[EXTATTR_MAXNAMELEN];
3145 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3148 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3149 SCARG(uap, path), td);
3150 if ((error = namei(&nd)) != 0)
3152 iovlen = uap->iovcnt * sizeof (struct iovec);
3153 if (uap->iovcnt > UIO_SMALLIOV) {
3154 if (uap->iovcnt > UIO_MAXIOV) {
3158 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3165 auio.uio_iovcnt = uap->iovcnt;
3166 auio.uio_rw = UIO_READ;
3167 auio.uio_segflg = UIO_USERSPACE;
3169 auio.uio_offset = 0;
3170 if ((error = copyin(uap->iovp, iov, iovlen)))
3173 for (i = 0; i < uap->iovcnt; i++) {
3174 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3178 auio.uio_resid += iov->iov_len;
3181 cnt = auio.uio_resid;
3182 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3183 cnt -= auio.uio_resid;
3184 uap->sysmsg_result = cnt;
3187 FREE(needfree, M_IOV);
3193 * Syscall to delete a named extended attribute from a file or directory.
3194 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3197 extattr_delete_file(struct extattr_delete_file_args *uap)
3199 struct thread *td = curthread;
3200 struct proc *p = td->td_proc;
3201 struct nameidata nd;
3202 char attrname[EXTATTR_MAXNAMELEN];
3205 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3208 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3209 SCARG(uap, path), td);
3210 if ((error = namei(&nd)) != 0)
3212 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);
3218 * print out statistics from the current status of the buffer pool
3219 * this can be toggeled by the system control option debug.syncprt
3228 int counts[(MAXBSIZE / PAGE_SIZE) + 1];
3229 static char *bname[3] = { "LOCKED", "LRU", "AGE" };
3231 for (dp = bufqueues, i = 0; dp < &bufqueues[3]; dp++, i++) {
3233 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3236 TAILQ_FOREACH(bp, dp, b_freelist) {
3237 counts[bp->b_bufsize/PAGE_SIZE]++;
3241 printf("%s: total-%d", bname[i], count);
3242 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3244 printf(", %d-%d", j * PAGE_SIZE, counts[j]);