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.37 2004/05/26 19:09:04 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_WAITOK);
253 bzero((char *)mp, (u_long)sizeof(struct mount));
254 TAILQ_INIT(&mp->mnt_nvnodelist);
255 TAILQ_INIT(&mp->mnt_reservedvnlist);
256 mp->mnt_nvnodelistsize = 0;
257 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
258 vfs_busy(mp, LK_NOWAIT, NULL, td);
259 mp->mnt_op = vfsp->vfc_vfsops;
261 vfsp->vfc_refcount++;
262 mp->mnt_stat.f_type = vfsp->vfc_typenum;
263 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
264 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
265 mp->mnt_vnodecovered = vp;
266 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
267 mp->mnt_iosize_max = DFLTPHYS;
268 VOP_UNLOCK(vp, NULL, 0, td);
271 * Set the mount level flags.
273 if (SCARG(uap, flags) & MNT_RDONLY)
274 mp->mnt_flag |= MNT_RDONLY;
275 else if (mp->mnt_flag & MNT_RDONLY)
276 mp->mnt_kern_flag |= MNTK_WANTRDWR;
277 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
278 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
279 MNT_NOSYMFOLLOW | MNT_IGNORE |
280 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
281 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
282 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
283 MNT_NOSYMFOLLOW | MNT_IGNORE |
284 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
286 * Mount the filesystem.
287 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
288 * get. No freeing of cn_pnbuf.
290 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
291 if (mp->mnt_flag & MNT_UPDATE) {
292 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
293 mp->mnt_flag &= ~MNT_RDONLY;
294 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
295 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
298 mp->mnt_kern_flag = flag2;
301 lwkt_gettoken(&vlock, vp->v_interlock);
302 vp->v_flag &= ~VMOUNT;
303 lwkt_reltoken(&vlock);
307 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
309 * Put the new filesystem on the mount list after root.
313 lwkt_gettoken(&vlock, vp->v_interlock);
314 vp->v_flag &= ~VMOUNT;
315 vp->v_mountedhere = mp;
316 lwkt_reltoken(&vlock);
317 lwkt_gettoken(&ilock, &mountlist_token);
318 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
319 lwkt_reltoken(&ilock);
321 VOP_UNLOCK(vp, NULL, 0, td);
322 error = vfs_allocate_syncvnode(mp);
324 if ((error = VFS_START(mp, 0, td)) != 0)
327 lwkt_gettoken(&vlock, vp->v_interlock);
328 vp->v_flag &= ~VMOUNT;
329 lwkt_reltoken(&vlock);
330 mp->mnt_vfc->vfc_refcount--;
339 * Scan all active processes to see if any of them have a current
340 * or root directory onto which the new filesystem has just been
341 * mounted. If so, replace them with the new mount point.
344 checkdirs(struct vnode *olddp)
346 struct filedesc *fdp;
350 if (olddp->v_usecount == 1)
352 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
353 panic("mount: lost mount");
354 FOREACH_PROC_IN_SYSTEM(p) {
356 if (fdp->fd_cdir == olddp) {
359 fdp->fd_cdir = newdp;
361 if (fdp->fd_rdir == olddp) {
364 fdp->fd_rdir = newdp;
367 if (rootvnode == olddp) {
371 vfs_cache_setroot(rootvnode);
377 * Unmount a file system.
379 * Note: unmount takes a path to the vnode mounted on as argument,
380 * not special file (as before).
383 * umount_args(char *path, int flags)
387 unmount(struct unmount_args *uap)
389 struct thread *td = curthread;
390 struct proc *p = td->td_proc;
397 if (p->p_ucred->cr_prison != NULL)
399 if (usermount == 0 && (error = suser(td)))
402 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
403 SCARG(uap, path), td);
404 if ((error = namei(&nd)) != 0)
407 NDFREE(&nd, NDF_ONLY_PNBUF);
411 * Only root, or the user that did the original mount is
412 * permitted to unmount this filesystem.
414 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
415 (error = suser(td))) {
421 * Don't allow unmounting the root file system.
423 if (mp->mnt_flag & MNT_ROOTFS) {
429 * Must be the root of the filesystem
431 if ((vp->v_flag & VROOT) == 0) {
436 return (dounmount(mp, SCARG(uap, flags), td));
440 * Do the actual file system unmount.
443 dounmount(struct mount *mp, int flags, struct thread *td)
445 struct vnode *coveredvp;
450 lwkt_gettoken(&ilock, &mountlist_token);
451 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
452 lwkt_reltoken(&ilock);
455 mp->mnt_kern_flag |= MNTK_UNMOUNT;
456 /* Allow filesystems to detect that a forced unmount is in progress. */
457 if (flags & MNT_FORCE)
458 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
459 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
460 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
462 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
463 if (mp->mnt_kern_flag & MNTK_MWAIT)
468 if (mp->mnt_flag & MNT_EXPUBLIC)
469 vfs_setpublicfs(NULL, NULL, NULL);
471 vfs_msync(mp, MNT_WAIT);
472 async_flag = mp->mnt_flag & MNT_ASYNC;
473 mp->mnt_flag &=~ MNT_ASYNC;
474 cache_purgevfs(mp); /* remove cache entries for this file sys */
475 if (mp->mnt_syncer != NULL)
476 vrele(mp->mnt_syncer);
477 if (((mp->mnt_flag & MNT_RDONLY) ||
478 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
480 error = VFS_UNMOUNT(mp, flags, td);
481 lwkt_gettokref(&ilock);
483 if (mp->mnt_syncer == NULL)
484 vfs_allocate_syncvnode(mp);
485 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
486 mp->mnt_flag |= async_flag;
487 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
489 if (mp->mnt_kern_flag & MNTK_MWAIT)
493 TAILQ_REMOVE(&mountlist, mp, mnt_list);
494 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
495 coveredvp->v_mountedhere = NULL;
498 mp->mnt_vfc->vfc_refcount--;
499 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
500 panic("unmount: dangling vnode");
501 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
502 if (mp->mnt_kern_flag & MNTK_MWAIT)
509 * Sync each mounted filesystem.
513 static int syncprt = 0;
514 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
519 sync(struct sync_args *uap)
521 struct thread *td = curthread;
522 struct mount *mp, *nmp;
526 lwkt_gettoken(&ilock, &mountlist_token);
527 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
528 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
529 nmp = TAILQ_NEXT(mp, mnt_list);
532 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
533 asyncflag = mp->mnt_flag & MNT_ASYNC;
534 mp->mnt_flag &= ~MNT_ASYNC;
535 vfs_msync(mp, MNT_NOWAIT);
536 VFS_SYNC(mp, MNT_NOWAIT, td);
537 mp->mnt_flag |= asyncflag;
539 lwkt_gettokref(&ilock);
540 nmp = TAILQ_NEXT(mp, mnt_list);
543 lwkt_reltoken(&ilock);
545 * print out buffer pool stat information on each sync() call.
554 /* XXX PRISON: could be per prison flag */
555 static int prison_quotas;
557 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
561 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
563 * Change filesystem quotas.
567 quotactl(struct quotactl_args *uap)
569 struct thread *td = curthread;
570 struct proc *p = td->td_proc;
576 if (p->p_ucred->cr_prison && !prison_quotas)
578 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
579 SCARG(uap, path), td);
580 if ((error = namei(&nd)) != 0)
582 mp = nd.ni_vp->v_mount;
583 NDFREE(&nd, NDF_ONLY_PNBUF);
585 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
586 SCARG(uap, arg), td));
590 kern_statfs(struct nameidata *nd, struct statfs *buf)
592 struct thread *td = curthread;
600 mp = nd->ni_vp->v_mount;
602 NDFREE(nd, NDF_ONLY_PNBUF);
604 error = VFS_STATFS(mp, sp, td);
607 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
608 bcopy(sp, buf, sizeof(*buf));
609 /* Only root should have access to the fsid's. */
611 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
616 * statfs_args(char *path, struct statfs *buf)
618 * Get filesystem statistics.
621 statfs(struct statfs_args *uap)
623 struct thread *td = curthread;
628 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
630 error = kern_statfs(&nd, &buf);
633 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
638 kern_fstatfs(int fd, struct statfs *buf)
640 struct thread *td = curthread;
641 struct proc *p = td->td_proc;
648 error = getvnode(p->p_fd, fd, &fp);
651 mp = ((struct vnode *)fp->f_data)->v_mount;
655 error = VFS_STATFS(mp, sp, td);
658 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
659 bcopy(sp, buf, sizeof(*buf));
660 /* Only root should have access to the fsid's. */
662 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
667 * fstatfs_args(int fd, struct statfs *buf)
669 * Get filesystem statistics.
672 fstatfs(struct fstatfs_args *uap)
677 error = kern_fstatfs(uap->fd, &buf);
680 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
685 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
687 * Get statistics on all filesystems.
691 getfsstat(struct getfsstat_args *uap)
693 struct thread *td = curthread;
694 struct mount *mp, *nmp;
698 long count, maxcount, error;
700 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
701 sfsp = (caddr_t)SCARG(uap, buf);
703 lwkt_gettoken(&ilock, &mountlist_token);
704 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
705 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
706 nmp = TAILQ_NEXT(mp, mnt_list);
709 if (sfsp && count < maxcount) {
712 * If MNT_NOWAIT or MNT_LAZY is specified, do not
713 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
714 * overrides MNT_WAIT.
716 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
717 (SCARG(uap, flags) & MNT_WAIT)) &&
718 (error = VFS_STATFS(mp, sp, td))) {
719 lwkt_gettokref(&ilock);
720 nmp = TAILQ_NEXT(mp, mnt_list);
724 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
725 error = copyout(sp, sfsp, sizeof(*sp));
733 lwkt_gettokref(&ilock);
734 nmp = TAILQ_NEXT(mp, mnt_list);
737 lwkt_reltoken(&ilock);
738 if (sfsp && count > maxcount)
739 uap->sysmsg_result = maxcount;
741 uap->sysmsg_result = count;
746 * fchdir_args(int fd)
748 * Change current working directory to a given file descriptor.
752 fchdir(struct fchdir_args *uap)
754 struct thread *td = curthread;
755 struct proc *p = td->td_proc;
756 struct filedesc *fdp = p->p_fd;
757 struct vnode *vp, *tdp;
762 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
764 vp = (struct vnode *)fp->f_data;
766 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
767 if (vp->v_type != VDIR)
770 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
771 while (!error && (mp = vp->v_mountedhere) != NULL) {
772 if (vfs_busy(mp, 0, NULL, td))
774 error = VFS_ROOT(mp, &tdp);
785 VOP_UNLOCK(vp, NULL, 0, td);
792 kern_chdir(struct nameidata *nd)
794 struct thread *td = curthread;
795 struct proc *p = td->td_proc;
796 struct filedesc *fdp = p->p_fd;
799 if ((error = namei(nd)) != 0)
801 if ((error = checkvp_chdir(nd->ni_vp, td)) == 0) {
803 fdp->fd_cdir = nd->ni_vp;
806 NDFREE(nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
811 * chdir_args(char *path)
813 * Change current working directory (``.'').
816 chdir(struct chdir_args *uap)
818 struct thread *td = curthread;
822 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
825 error = kern_chdir(&nd);
831 * Helper function for raised chroot(2) security function: Refuse if
832 * any filedescriptors are open directories.
835 chroot_refuse_vdir_fds(fdp)
836 struct filedesc *fdp;
843 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
844 error = getvnode(fdp, fd, &fp);
847 vp = (struct vnode *)fp->f_data;
848 if (vp->v_type != VDIR)
856 * This sysctl determines if we will allow a process to chroot(2) if it
857 * has a directory open:
858 * 0: disallowed for all processes.
859 * 1: allowed for processes that were not already chroot(2)'ed.
860 * 2: allowed for all processes.
863 static int chroot_allow_open_directories = 1;
865 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
866 &chroot_allow_open_directories, 0, "");
869 * Chroot to the specified vnode. vp must be locked and referenced on
870 * call, and will be left locked and referenced on return. This routine
871 * may acquire additional refs on the vnode when associating it with
872 * the process's root and/or jail dirs.
875 kern_chroot(struct vnode *vp)
877 struct thread *td = curthread;
878 struct proc *p = td->td_proc;
879 struct filedesc *fdp = p->p_fd;
883 * Only root can chroot
885 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
889 * Disallow open directory descriptors (fchdir() breakouts).
891 if (chroot_allow_open_directories == 0 ||
892 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
893 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
898 * Check the validity of vp as a directory to change to and
899 * associate it with rdir/jdir.
901 if ((error = checkvp_chdir(vp, td)) == 0) {
905 if (fdp->fd_jdir == NULL) {
914 * chroot_args(char *path)
916 * Change notion of root (``/'') directory.
920 chroot(struct chroot_args *uap)
922 struct thread *td = curthread;
926 KKASSERT(td->td_proc);
927 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
928 SCARG(uap, path), td);
929 if ((error = namei(&nd)) == 0) {
930 error = kern_chroot(nd.ni_vp);
931 NDFREE(&nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
937 * Common routine for chroot and chdir. Given a locked, referenced vnode,
938 * determine whether it is legal to chdir to the vnode. The vnode's state
939 * is not changed by this call.
942 checkvp_chdir(struct vnode *vp, struct thread *td)
946 if (vp->v_type != VDIR)
949 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
954 kern_open(struct nameidata *nd, int oflags, int mode, int *res)
956 struct thread *td = curthread;
957 struct proc *p = td->td_proc;
958 struct filedesc *fdp = p->p_fd;
963 int type, indx, error;
966 if ((oflags & O_ACCMODE) == O_ACCMODE)
968 flags = FFLAGS(oflags);
969 error = falloc(p, &nfp, &indx);
973 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
974 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
976 * Bump the ref count to prevent another process from closing
977 * the descriptor while we are blocked in vn_open()
980 error = vn_open(nd, flags, cmode);
983 * release our own reference
988 * handle special fdopen() case. bleh. dupfdopen() is
989 * responsible for dropping the old contents of ofiles[indx]
992 if ((error == ENODEV || error == ENXIO) &&
993 p->p_dupfd >= 0 && /* XXX from fdopen */
995 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
1000 * Clean up the descriptor, but only if another thread hadn't
1001 * replaced or closed it.
1003 if (fdp->fd_ofiles[indx] == fp) {
1004 fdp->fd_ofiles[indx] = NULL;
1008 if (error == ERESTART)
1013 NDFREE(nd, NDF_ONLY_PNBUF);
1017 * There should be 2 references on the file, one from the descriptor
1018 * table, and one for us.
1020 * Handle the case where someone closed the file (via its file
1021 * descriptor) while we were blocked. The end result should look
1022 * like opening the file succeeded but it was immediately closed.
1024 if (fp->f_count == 1) {
1025 KASSERT(fdp->fd_ofiles[indx] != fp,
1026 ("Open file descriptor lost all refs"));
1027 VOP_UNLOCK(vp, NULL, 0, td);
1028 vn_close(vp, flags & FMASK, td);
1034 fp->f_data = (caddr_t)vp;
1035 fp->f_flag = flags & FMASK;
1037 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1038 if (flags & (O_EXLOCK | O_SHLOCK)) {
1039 lf.l_whence = SEEK_SET;
1042 if (flags & O_EXLOCK)
1043 lf.l_type = F_WRLCK;
1045 lf.l_type = F_RDLCK;
1047 if ((flags & FNONBLOCK) == 0)
1049 VOP_UNLOCK(vp, NULL, 0, td);
1050 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1052 * lock request failed. Normally close the descriptor
1053 * but handle the case where someone might have dup()d
1054 * it when we weren't looking. One reference is
1055 * owned by the descriptor array, the other by us.
1057 if (fdp->fd_ofiles[indx] == fp) {
1058 fdp->fd_ofiles[indx] = NULL;
1064 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1065 fp->f_flag |= FHASLOCK;
1067 /* assert that vn_open created a backing object if one is needed */
1068 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1069 ("open: vmio vnode has no backing object after vn_open"));
1070 VOP_UNLOCK(vp, NULL, 0, td);
1073 * release our private reference, leaving the one associated with the
1074 * descriptor table intact.
1082 * open_args(char *path, int flags, int mode)
1084 * Check permissions, allocate an open file structure,
1085 * and call the device open routine if any.
1088 open(struct open_args *uap)
1090 struct thread *td = curthread;
1091 struct nameidata nd;
1094 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1096 error = kern_open(&nd, uap->flags, uap->mode, &uap->sysmsg_result);
1102 kern_mknod(struct nameidata *nd, int mode, int dev)
1104 struct thread *td = curthread;
1105 struct proc *p = td->td_proc;
1113 switch (mode & S_IFMT) {
1119 error = suser_cred(p->p_ucred, PRISON_ROOT);
1133 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1134 vattr.va_rdev = dev;
1137 switch (mode & S_IFMT) {
1138 case S_IFMT: /* used by badsect to flag bad sectors */
1139 vattr.va_type = VBAD;
1142 vattr.va_type = VCHR;
1145 vattr.va_type = VBLK;
1156 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1158 error = VOP_WHITEOUT(nd->ni_dvp, NCPNULL,
1159 &nd->ni_cnd, NAMEI_CREATE);
1161 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp,
1162 &nd->ni_cnd, &vattr);
1166 NDFREE(nd, NDF_ONLY_PNBUF);
1169 NDFREE(nd, NDF_ONLY_PNBUF);
1170 if (nd->ni_dvp == vp)
1177 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mknod");
1178 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mknod");
1183 * mknod_args(char *path, int mode, int dev)
1185 * Create a special file.
1188 mknod(struct mknod_args *uap)
1190 struct thread *td = curthread;
1191 struct nameidata nd;
1194 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1197 error = kern_mknod(&nd, uap->mode, uap->dev);
1203 kern_mkfifo(struct nameidata *nd, int mode)
1205 struct thread *td = curthread;
1206 struct proc *p = td->td_proc;
1214 if (nd->ni_vp != NULL) {
1215 NDFREE(nd, NDF_ONLY_PNBUF);
1216 if (nd->ni_dvp == nd->ni_vp)
1224 vattr.va_type = VFIFO;
1225 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1226 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1227 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd, &vattr);
1230 NDFREE(nd, NDF_ONLY_PNBUF);
1236 * mkfifo_args(char *path, int mode)
1238 * Create a named pipe.
1241 mkfifo(struct mkfifo_args *uap)
1243 struct thread *td = curthread;
1244 struct nameidata nd;
1247 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1250 error = kern_mkfifo(&nd, uap->mode);
1256 kern_link(struct nameidata *nd, struct nameidata *linknd)
1258 struct thread *td = curthread;
1259 struct proc *p = td->td_proc;
1267 NDFREE(nd, NDF_ONLY_PNBUF);
1269 if (vp->v_type == VDIR)
1270 error = EPERM; /* POSIX */
1272 error = namei(linknd);
1274 if (linknd->ni_vp != NULL) {
1276 vrele(linknd->ni_vp);
1279 VOP_LEASE(linknd->ni_dvp, td, p->p_ucred,
1281 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1282 error = VOP_LINK(linknd->ni_dvp, NCPNULL, vp,
1285 NDFREE(linknd, NDF_ONLY_PNBUF);
1286 if (linknd->ni_dvp == linknd->ni_vp)
1287 vrele(linknd->ni_dvp);
1289 vput(linknd->ni_dvp);
1290 ASSERT_VOP_UNLOCKED(linknd->ni_dvp, "link");
1291 ASSERT_VOP_UNLOCKED(linknd->ni_vp, "link");
1299 * link_args(char *path, char *link)
1301 * Make a hard file link.
1304 link(struct link_args *uap)
1306 struct thread *td = curthread;
1307 struct nameidata nd, linknd;
1310 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1312 NDINIT(&linknd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1313 UIO_USERSPACE, uap->link, td);
1315 error = kern_link(&nd, &linknd);
1321 kern_symlink(char *path, struct nameidata *nd)
1323 struct thread *td = curthread;
1324 struct proc *p = td->td_proc;
1333 NDFREE(nd, NDF_ONLY_PNBUF);
1334 if (nd->ni_dvp == nd->ni_vp)
1342 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1343 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1344 error = VOP_SYMLINK(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
1346 NDFREE(nd, NDF_ONLY_PNBUF);
1350 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "symlink");
1351 ASSERT_VOP_UNLOCKED(nd->ni_vp, "symlink");
1357 * symlink(char *path, char *link)
1359 * Make a symbolic link.
1362 symlink(struct symlink_args *uap)
1364 struct thread *td = curthread;
1365 struct nameidata nd;
1369 path = zalloc(namei_zone);
1370 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1372 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1373 UIO_USERSPACE, uap->link, td);
1374 error = kern_symlink(path, &nd);
1376 zfree(namei_zone, path);
1381 * undelete_args(char *path)
1383 * Delete a whiteout from the filesystem.
1387 undelete(struct undelete_args *uap)
1389 struct thread *td = curthread;
1390 struct proc *p = td->td_proc;
1392 struct nameidata nd;
1395 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
1396 SCARG(uap, path), td);
1401 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
1402 NDFREE(&nd, NDF_ONLY_PNBUF);
1403 if (nd.ni_dvp == nd.ni_vp)
1412 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1413 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
1414 NDFREE(&nd, NDF_ONLY_PNBUF);
1416 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1417 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1422 kern_unlink(struct nameidata *nd)
1424 struct thread *td = curthread;
1425 struct proc *p = td->td_proc;
1434 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1435 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1437 if (vp->v_type == VDIR)
1438 error = EPERM; /* POSIX */
1441 * The root of a mounted filesystem cannot be deleted.
1443 * XXX: can this only be a VDIR case?
1445 if (vp->v_flag & VROOT)
1450 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1451 error = VOP_REMOVE(nd->ni_dvp, NCPNULL, vp, &nd->ni_cnd);
1453 NDFREE(nd, NDF_ONLY_PNBUF);
1454 if (nd->ni_dvp == vp)
1460 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "unlink");
1461 ASSERT_VOP_UNLOCKED(nd->ni_vp, "unlink");
1466 * unlink_args(char *path)
1468 * Delete a name from the filesystem.
1471 unlink(struct unlink_args *uap)
1473 struct thread *td = curthread;
1474 struct nameidata nd;
1477 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1480 error = kern_unlink(&nd);
1486 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1488 struct thread *td = curthread;
1489 struct proc *p = td->td_proc;
1490 struct filedesc *fdp = p->p_fd;
1495 if (fd >= fdp->fd_nfiles ||
1496 (fp = fdp->fd_ofiles[fd]) == NULL)
1498 if (fp->f_type != DTYPE_VNODE)
1502 fp->f_offset += offset;
1505 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1508 fp->f_offset = offset + vattr.va_size;
1511 fp->f_offset = offset;
1516 *res = fp->f_offset;
1521 * lseek_args(int fd, int pad, off_t offset, int whence)
1523 * Reposition read/write file offset.
1526 lseek(struct lseek_args *uap)
1530 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1531 &uap->sysmsg_offset);
1537 kern_access(struct nameidata *nd, int aflags)
1539 struct thread *td = curthread;
1540 struct proc *p = td->td_proc;
1541 struct ucred *cred, *tmpcred;
1547 * Create and modify a temporary credential instead of one that
1548 * is potentially shared. This could also mess up socket
1549 * buffer accounting which can run in an interrupt context.
1551 tmpcred = crdup(cred);
1552 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1553 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1554 p->p_ucred = tmpcred;
1555 nd->ni_cnd.cn_cred = tmpcred;
1561 /* Flags == 0 means only check for existence. */
1570 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1571 error = VOP_ACCESS(vp, flags, tmpcred, td);
1573 NDFREE(nd, NDF_ONLY_PNBUF);
1582 * access_args(char *path, int flags)
1584 * Check access permissions.
1587 access(struct access_args *uap)
1589 struct thread *td = curthread;
1590 struct nameidata nd;
1593 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1594 UIO_USERSPACE, uap->path, td);
1596 error = kern_access(&nd, uap->flags);
1602 kern_stat(struct nameidata *nd, struct stat *st)
1604 struct thread *td = curthread;
1610 error = vn_stat(nd->ni_vp, st, td);
1611 NDFREE(nd, NDF_ONLY_PNBUF);
1617 * stat_args(char *path, struct stat *ub)
1619 * Get file status; this version follows links.
1622 stat(struct stat_args *uap)
1624 struct thread *td = curthread;
1625 struct nameidata nd;
1629 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1630 UIO_USERSPACE, uap->path, td);
1632 error = kern_stat(&nd, &st);
1635 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1640 * lstat_args(char *path, struct stat *ub)
1642 * Get file status; this version does not follow links.
1645 lstat(struct lstat_args *uap)
1647 struct thread *td = curthread;
1648 struct nameidata nd;
1652 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1653 UIO_USERSPACE, SCARG(uap, path), td);
1655 error = kern_stat(&nd, &st);
1658 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1667 nsb->st_dev = sb->st_dev;
1668 nsb->st_ino = sb->st_ino;
1669 nsb->st_mode = sb->st_mode;
1670 nsb->st_nlink = sb->st_nlink;
1671 nsb->st_uid = sb->st_uid;
1672 nsb->st_gid = sb->st_gid;
1673 nsb->st_rdev = sb->st_rdev;
1674 nsb->st_atimespec = sb->st_atimespec;
1675 nsb->st_mtimespec = sb->st_mtimespec;
1676 nsb->st_ctimespec = sb->st_ctimespec;
1677 nsb->st_size = sb->st_size;
1678 nsb->st_blocks = sb->st_blocks;
1679 nsb->st_blksize = sb->st_blksize;
1680 nsb->st_flags = sb->st_flags;
1681 nsb->st_gen = sb->st_gen;
1682 nsb->st_qspare[0] = sb->st_qspare[0];
1683 nsb->st_qspare[1] = sb->st_qspare[1];
1687 * nstat_args(char *path, struct nstat *ub)
1691 nstat(struct nstat_args *uap)
1693 struct thread *td = curthread;
1697 struct nameidata nd;
1699 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1700 UIO_USERSPACE, SCARG(uap, path), td);
1701 if ((error = namei(&nd)) != 0)
1703 NDFREE(&nd, NDF_ONLY_PNBUF);
1704 error = vn_stat(nd.ni_vp, &sb, td);
1708 cvtnstat(&sb, &nsb);
1709 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1714 * lstat_args(char *path, struct stat *ub)
1716 * Get file status; this version does not follow links.
1720 nlstat(struct nlstat_args *uap)
1722 struct thread *td = curthread;
1727 struct nameidata nd;
1729 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1730 UIO_USERSPACE, SCARG(uap, path), td);
1731 if ((error = namei(&nd)) != 0)
1734 NDFREE(&nd, NDF_ONLY_PNBUF);
1735 error = vn_stat(vp, &sb, td);
1739 cvtnstat(&sb, &nsb);
1740 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1745 * pathconf_Args(char *path, int name)
1747 * Get configurable pathname variables.
1751 pathconf(struct pathconf_args *uap)
1753 struct thread *td = curthread;
1755 struct nameidata nd;
1757 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1758 UIO_USERSPACE, SCARG(uap, path), td);
1759 if ((error = namei(&nd)) != 0)
1761 NDFREE(&nd, NDF_ONLY_PNBUF);
1762 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1769 * kern_readlink isn't properly split yet. There is a copyin burried
1770 * in VOP_READLINK().
1773 kern_readlink(struct nameidata *nd, char *buf, int count, int *res)
1775 struct thread *td = curthread;
1776 struct proc *p = td->td_proc;
1785 NDFREE(nd, NDF_ONLY_PNBUF);
1787 if (vp->v_type != VLNK)
1790 aiov.iov_base = buf;
1791 aiov.iov_len = count;
1792 auio.uio_iov = &aiov;
1793 auio.uio_iovcnt = 1;
1794 auio.uio_offset = 0;
1795 auio.uio_rw = UIO_READ;
1796 auio.uio_segflg = UIO_USERSPACE;
1798 auio.uio_resid = count;
1799 error = VOP_READLINK(vp, &auio, p->p_ucred);
1802 *res = count - auio.uio_resid;
1807 * readlink_args(char *path, char *buf, int count)
1809 * Return target name of a symbolic link.
1812 readlink(struct readlink_args *uap)
1814 struct thread *td = curthread;
1815 struct nameidata nd;
1818 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ, UIO_USERSPACE,
1821 error = kern_readlink(&nd, uap->buf, uap->count,
1822 &uap->sysmsg_result);
1828 setfflags(struct vnode *vp, int flags)
1830 struct thread *td = curthread;
1831 struct proc *p = td->td_proc;
1836 * Prevent non-root users from setting flags on devices. When
1837 * a device is reused, users can retain ownership of the device
1838 * if they are allowed to set flags and programs assume that
1839 * chown can't fail when done as root.
1841 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1842 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1845 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1846 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1848 vattr.va_flags = flags;
1849 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1850 VOP_UNLOCK(vp, NULL, 0, td);
1855 * chflags(char *path, int flags)
1857 * Change flags of a file given a path name.
1861 chflags(struct chflags_args *uap)
1863 struct thread *td = curthread;
1865 struct nameidata nd;
1867 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1868 SCARG(uap, path), td);
1869 if ((error = namei(&nd)) != 0)
1871 NDFREE(&nd, NDF_ONLY_PNBUF);
1872 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1878 * fchflags_args(int fd, int flags)
1880 * Change flags of a file given a file descriptor.
1884 fchflags(struct fchflags_args *uap)
1886 struct thread *td = curthread;
1887 struct proc *p = td->td_proc;
1891 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1893 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1897 setfmode(struct vnode *vp, int mode)
1899 struct thread *td = curthread;
1900 struct proc *p = td->td_proc;
1904 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1905 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1907 vattr.va_mode = mode & ALLPERMS;
1908 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1909 VOP_UNLOCK(vp, NULL, 0, td);
1914 kern_chmod(struct nameidata *nd, int mode)
1921 NDFREE(nd, NDF_ONLY_PNBUF);
1922 error = setfmode(nd->ni_vp, mode);
1928 * chmod_args(char *path, int mode)
1930 * Change mode of a file given path name.
1934 chmod(struct chmod_args *uap)
1936 struct thread *td = curthread;
1937 struct nameidata nd;
1940 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1942 error = kern_chmod(&nd, uap->mode);
1948 * lchmod_args(char *path, int mode)
1950 * Change mode of a file given path name (don't follow links.)
1954 lchmod(struct lchmod_args *uap)
1956 struct thread *td = curthread;
1958 struct nameidata nd;
1960 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
1961 if ((error = namei(&nd)) != 0)
1963 NDFREE(&nd, NDF_ONLY_PNBUF);
1964 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1970 * fchmod_args(int fd, int mode)
1972 * Change mode of a file given a file descriptor.
1976 fchmod(struct fchmod_args *uap)
1978 struct thread *td = curthread;
1979 struct proc *p = td->td_proc;
1983 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1985 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1989 setfown(struct vnode *vp, uid_t uid, gid_t gid)
1991 struct thread *td = curthread;
1992 struct proc *p = td->td_proc;
1996 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1997 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2001 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2002 VOP_UNLOCK(vp, NULL, 0, td);
2007 kern_chown(struct nameidata *nd, int uid, int gid)
2014 NDFREE(nd, NDF_ONLY_PNBUF);
2015 error = setfown(nd->ni_vp, uid, gid);
2021 * chown(char *path, int uid, int gid)
2023 * Set ownership given a path name.
2026 chown(struct chown_args *uap)
2028 struct thread *td = curthread;
2029 struct nameidata nd;
2032 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2034 error = kern_chown(&nd, uap->uid, uap->gid);
2040 * lchown_args(char *path, int uid, int gid)
2042 * Set ownership given a path name, do not cross symlinks.
2045 lchown(struct lchown_args *uap)
2047 struct thread *td = curthread;
2049 struct nameidata nd;
2051 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2053 error = kern_chown(&nd, uap->uid, uap->gid);
2059 * fchown_args(int fd, int uid, int gid)
2061 * Set ownership given a file descriptor.
2065 fchown(struct fchown_args *uap)
2067 struct thread *td = curthread;
2068 struct proc *p = td->td_proc;
2072 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2074 return setfown((struct vnode *)fp->f_data,
2075 SCARG(uap, uid), SCARG(uap, gid));
2079 getutimes(const struct timeval *tvp, struct timespec *tsp)
2081 struct timeval tv[2];
2085 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2088 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2089 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2095 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2097 struct thread *td = curthread;
2098 struct proc *p = td->td_proc;
2102 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2103 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2105 vattr.va_atime = ts[0];
2106 vattr.va_mtime = ts[1];
2108 vattr.va_vaflags |= VA_UTIMES_NULL;
2109 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2110 VOP_UNLOCK(vp, NULL, 0, td);
2115 kern_utimes(struct nameidata *nd, struct timeval *tptr)
2117 struct timespec ts[2];
2120 error = getutimes(tptr, ts);
2126 NDFREE(nd, NDF_ONLY_PNBUF);
2127 error = setutimes(nd->ni_vp, ts, tptr == NULL);
2133 * utimes_args(char *path, struct timeval *tptr)
2135 * Set the access and modification times of a file.
2138 utimes(struct utimes_args *uap)
2140 struct thread *td = curthread;
2141 struct timeval tv[2];
2142 struct nameidata nd;
2146 error = copyin(uap->tptr, tv, sizeof(tv));
2150 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2152 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2158 * lutimes_args(char *path, struct timeval *tptr)
2160 * Set the access and modification times of a file.
2163 lutimes(struct lutimes_args *uap)
2165 struct thread *td = curthread;
2166 struct timeval tv[2];
2167 struct nameidata nd;
2171 error = copyin(uap->tptr, tv, sizeof(tv));
2175 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2177 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2183 kern_futimes(int fd, struct timeval *tptr)
2185 struct thread *td = curthread;
2186 struct proc *p = td->td_proc;
2187 struct timespec ts[2];
2191 error = getutimes(tptr, ts);
2194 error = getvnode(p->p_fd, fd, &fp);
2197 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2202 * futimes_args(int fd, struct timeval *tptr)
2204 * Set the access and modification times of a file.
2207 futimes(struct futimes_args *uap)
2209 struct timeval tv[2];
2213 error = copyin(uap->tptr, tv, sizeof(tv));
2218 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2224 kern_truncate(struct nameidata* nd, off_t length)
2226 struct thread *td = curthread;
2227 struct proc *p = td->td_proc;
2234 if ((error = namei(nd)) != 0)
2237 NDFREE(nd, NDF_ONLY_PNBUF);
2238 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2239 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2240 if (vp->v_type == VDIR)
2242 else if ((error = vn_writechk(vp)) == 0 &&
2243 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2245 vattr.va_size = length;
2246 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2253 * truncate(char *path, int pad, off_t length)
2255 * Truncate a file given its path name.
2258 truncate(struct truncate_args *uap)
2260 struct thread *td = curthread;
2261 struct nameidata nd;
2264 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2266 error = kern_truncate(&nd, uap->length);
2272 kern_ftruncate(int fd, off_t length)
2274 struct thread *td = curthread;
2275 struct proc *p = td->td_proc;
2283 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2285 if ((fp->f_flag & FWRITE) == 0)
2287 vp = (struct vnode *)fp->f_data;
2288 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2289 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2290 if (vp->v_type == VDIR)
2292 else if ((error = vn_writechk(vp)) == 0) {
2294 vattr.va_size = length;
2295 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2297 VOP_UNLOCK(vp, NULL, 0, td);
2302 * ftruncate_args(int fd, int pad, off_t length)
2304 * Truncate a file given a file descriptor.
2307 ftruncate(struct ftruncate_args *uap)
2311 error = kern_ftruncate(uap->fd, uap->length);
2319 * Sync an open file.
2323 fsync(struct fsync_args *uap)
2325 struct thread *td = curthread;
2326 struct proc *p = td->td_proc;
2332 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2334 vp = (struct vnode *)fp->f_data;
2335 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2336 if (VOP_GETVOBJECT(vp, &obj) == 0)
2337 vm_object_page_clean(obj, 0, 0, 0);
2338 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2339 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2341 error = (*bioops.io_fsync)(vp);
2342 VOP_UNLOCK(vp, NULL, 0, td);
2347 kern_rename(struct nameidata *fromnd, struct nameidata *tond)
2349 struct thread *td = curthread;
2350 struct proc *p = td->td_proc;
2351 struct vnode *tvp, *fvp, *tdvp;
2355 error = namei(fromnd);
2358 fvp = fromnd->ni_vp;
2359 if (fromnd->ni_vp->v_type == VDIR)
2360 tond->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2361 error = namei(tond);
2363 /* Translate error code for rename("dir1", "dir2/."). */
2364 if (error == EISDIR && fvp->v_type == VDIR)
2366 NDFREE(fromnd, NDF_ONLY_PNBUF);
2367 vrele(fromnd->ni_dvp);
2371 tdvp = tond->ni_dvp;
2374 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2377 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2385 * If the source is the same as the destination (that is, if they
2386 * are links to the same vnode), then there is nothing to do.
2392 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2393 if (fromnd->ni_dvp != tdvp) {
2394 VOP_LEASE(fromnd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2397 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2399 error = VOP_RENAME(fromnd->ni_dvp, NCPNULL, fromnd->ni_vp,
2400 &fromnd->ni_cnd, tond->ni_dvp, NCPNULL, tond->ni_vp,
2402 NDFREE(fromnd, NDF_ONLY_PNBUF);
2403 NDFREE(tond, NDF_ONLY_PNBUF);
2405 NDFREE(fromnd, NDF_ONLY_PNBUF);
2406 NDFREE(tond, NDF_ONLY_PNBUF);
2413 vrele(fromnd->ni_dvp);
2416 vrele(tond->ni_startdir);
2417 ASSERT_VOP_UNLOCKED(fromnd->ni_dvp, "rename");
2418 ASSERT_VOP_UNLOCKED(fromnd->ni_vp, "rename");
2419 ASSERT_VOP_UNLOCKED(tond->ni_dvp, "rename");
2420 ASSERT_VOP_UNLOCKED(tond->ni_vp, "rename");
2422 if (fromnd->ni_startdir)
2423 vrele(fromnd->ni_startdir);
2430 * rename_args(char *from, char *to)
2432 * Rename files. Source and destination must either both be directories,
2433 * or both not be directories. If target is a directory, it must be empty.
2436 rename(struct rename_args *uap)
2438 struct thread *td = curthread;
2439 struct nameidata fromnd, tond;
2442 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2443 UIO_USERSPACE, uap->from, td);
2444 NDINIT(&tond, NAMEI_RENAME,
2445 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2446 CNP_SAVESTART | CNP_NOOBJ,
2447 UIO_USERSPACE, uap->to, td);
2449 error = kern_rename(&fromnd, &tond);
2455 kern_mkdir(struct nameidata *nd, int mode)
2457 struct thread *td = curthread;
2458 struct proc *p = td->td_proc;
2464 nd->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2470 NDFREE(nd, NDF_ONLY_PNBUF);
2471 if (nd->ni_dvp == vp)
2479 vattr.va_type = VDIR;
2480 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2481 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2482 error = VOP_MKDIR(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
2484 NDFREE(nd, NDF_ONLY_PNBUF);
2488 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mkdir");
2489 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mkdir");
2494 * mkdir_args(char *path, int mode)
2496 * Make a directory file.
2500 mkdir(struct mkdir_args *uap)
2502 struct thread *td = curthread;
2503 struct nameidata nd;
2506 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
2509 error = kern_mkdir(&nd, uap->mode);
2515 kern_rmdir(struct nameidata *nd)
2517 struct thread *td = curthread;
2518 struct proc *p = td->td_proc;
2527 if (vp->v_type != VDIR) {
2532 * No rmdir "." please.
2534 if (nd->ni_dvp == vp) {
2539 * The root of a mounted filesystem cannot be deleted.
2541 if (vp->v_flag & VROOT)
2544 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2545 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2546 error = VOP_RMDIR(nd->ni_dvp, NCPNULL, nd->ni_vp,
2550 NDFREE(nd, NDF_ONLY_PNBUF);
2551 if (nd->ni_dvp == vp)
2557 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "rmdir");
2558 ASSERT_VOP_UNLOCKED(nd->ni_vp, "rmdir");
2563 * rmdir_args(char *path)
2565 * Remove a directory file.
2569 rmdir(struct rmdir_args *uap)
2571 struct thread *td = curthread;
2572 struct nameidata nd;
2575 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF,
2576 UIO_USERSPACE, uap->path, td);
2578 error = kern_rmdir(&nd);
2584 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2586 struct thread *td = curthread;
2587 struct proc *p = td->td_proc;
2595 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2597 if ((fp->f_flag & FREAD) == 0)
2599 vp = (struct vnode *)fp->f_data;
2601 if (vp->v_type != VDIR)
2603 aiov.iov_base = buf;
2604 aiov.iov_len = count;
2605 auio.uio_iov = &aiov;
2606 auio.uio_iovcnt = 1;
2607 auio.uio_rw = UIO_READ;
2608 auio.uio_segflg = UIO_USERSPACE;
2610 auio.uio_resid = count;
2611 /* vn_lock(vp, NULL, LK_SHARED | LK_RETRY, td); */
2612 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2613 loff = auio.uio_offset = fp->f_offset;
2614 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2615 fp->f_offset = auio.uio_offset;
2616 VOP_UNLOCK(vp, NULL, 0, td);
2619 if (count == auio.uio_resid) {
2620 if (union_dircheckp) {
2621 error = union_dircheckp(td, &vp, fp);
2627 if ((vp->v_flag & VROOT) &&
2628 (vp->v_mount->mnt_flag & MNT_UNION)) {
2629 struct vnode *tvp = vp;
2630 vp = vp->v_mount->mnt_vnodecovered;
2632 fp->f_data = (caddr_t)vp;
2641 *res = count - auio.uio_resid;
2646 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2648 * Read a block of directory entries in a file system independent format.
2651 getdirentries(struct getdirentries_args *uap)
2656 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2657 &uap->sysmsg_result);
2660 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2665 * getdents_args(int fd, char *buf, size_t count)
2668 getdents(struct getdents_args *uap)
2672 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2673 &uap->sysmsg_result);
2679 * umask(int newmask)
2681 * Set the mode mask for creation of filesystem nodes.
2686 umask(struct umask_args *uap)
2688 struct thread *td = curthread;
2689 struct proc *p = td->td_proc;
2690 struct filedesc *fdp;
2693 uap->sysmsg_result = fdp->fd_cmask;
2694 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2699 * revoke(char *path)
2701 * Void all references to file by ripping underlying filesystem
2706 revoke(struct revoke_args *uap)
2708 struct thread *td = curthread;
2709 struct proc *p = td->td_proc;
2713 struct nameidata nd;
2715 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2716 if ((error = namei(&nd)) != 0)
2719 NDFREE(&nd, NDF_ONLY_PNBUF);
2720 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2724 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2726 if (p->p_ucred->cr_uid != vattr.va_uid &&
2727 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2729 if (count_udev(vp->v_udev) > 0)
2730 VOP_REVOKE(vp, REVOKEALL);
2737 * Convert a user file descriptor to a kernel file entry.
2740 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2744 if ((u_int)fd >= fdp->fd_nfiles ||
2745 (fp = fdp->fd_ofiles[fd]) == NULL)
2747 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2753 * getfh_args(char *fname, fhandle_t *fhp)
2755 * Get (NFS) file handle
2758 getfh(struct getfh_args *uap)
2760 struct thread *td = curthread;
2761 struct nameidata nd;
2767 * Must be super user
2772 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2776 NDFREE(&nd, NDF_ONLY_PNBUF);
2778 bzero(&fh, sizeof(fh));
2779 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2780 error = VFS_VPTOFH(vp, &fh.fh_fid);
2784 error = copyout(&fh, uap->fhp, sizeof (fh));
2789 * fhopen_args(const struct fhandle *u_fhp, int flags)
2791 * syscall for the rpc.lockd to use to translate a NFS file handle into
2792 * an open descriptor.
2794 * warning: do not remove the suser() call or this becomes one giant
2798 fhopen(struct fhopen_args *uap)
2800 struct thread *td = curthread;
2801 struct proc *p = td->td_proc;
2806 struct vattr *vap = &vat;
2809 struct filedesc *fdp = p->p_fd;
2810 int fmode, mode, error, type;
2815 * Must be super user
2821 fmode = FFLAGS(SCARG(uap, flags));
2822 /* why not allow a non-read/write open for our lockd? */
2823 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2825 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2828 /* find the mount point */
2829 mp = vfs_getvfs(&fhp.fh_fsid);
2832 /* now give me my vnode, it gets returned to me locked */
2833 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2837 * from now on we have to make sure not
2838 * to forget about the vnode
2839 * any error that causes an abort must vput(vp)
2840 * just set error = err and 'goto bad;'.
2846 if (vp->v_type == VLNK) {
2850 if (vp->v_type == VSOCK) {
2855 if (fmode & (FWRITE | O_TRUNC)) {
2856 if (vp->v_type == VDIR) {
2860 error = vn_writechk(vp);
2868 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2872 if (fmode & O_TRUNC) {
2873 VOP_UNLOCK(vp, NULL, 0, td); /* XXX */
2874 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2875 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2878 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2882 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2886 * Make sure that a VM object is created for VMIO support.
2888 if (vn_canvmio(vp) == TRUE) {
2889 if ((error = vfs_object_create(vp, td)) != 0)
2896 * end of vn_open code
2899 if ((error = falloc(p, &nfp, &indx)) != 0) {
2907 * hold an extra reference to avoid having fp ripped out
2908 * from under us while we block in the lock op.
2911 nfp->f_data = (caddr_t)vp;
2912 nfp->f_flag = fmode & FMASK;
2913 nfp->f_ops = &vnops;
2914 nfp->f_type = DTYPE_VNODE;
2915 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2916 lf.l_whence = SEEK_SET;
2919 if (fmode & O_EXLOCK)
2920 lf.l_type = F_WRLCK;
2922 lf.l_type = F_RDLCK;
2924 if ((fmode & FNONBLOCK) == 0)
2926 VOP_UNLOCK(vp, NULL, 0, td);
2927 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2929 * lock request failed. Normally close the descriptor
2930 * but handle the case where someone might have dup()d
2931 * or close()d it when we weren't looking.
2933 if (fdp->fd_ofiles[indx] == fp) {
2934 fdp->fd_ofiles[indx] = NULL;
2939 * release our private reference.
2944 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2945 fp->f_flag |= FHASLOCK;
2947 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2948 vfs_object_create(vp, td);
2950 VOP_UNLOCK(vp, NULL, 0, td);
2952 uap->sysmsg_result = indx;
2961 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2964 fhstat(struct fhstat_args *uap)
2966 struct thread *td = curthread;
2974 * Must be super user
2980 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
2984 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2986 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
2988 error = vn_stat(vp, &sb, td);
2992 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
2997 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3000 fhstatfs(struct fhstatfs_args *uap)
3002 struct thread *td = curthread;
3011 * Must be super user
3013 if ((error = suser(td)))
3016 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3019 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3021 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3026 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3028 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3030 bcopy(sp, &sb, sizeof(sb));
3031 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3034 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3038 * Syscall to push extended attribute configuration information into the
3039 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3040 * a command (int cmd), and attribute name and misc data. For now, the
3041 * attribute name is left in userspace for consumption by the VFS_op.
3042 * It will probably be changed to be copied into sysspace by the
3043 * syscall in the future, once issues with various consumers of the
3044 * attribute code have raised their hands.
3046 * Currently this is used only by UFS Extended Attributes.
3049 extattrctl(struct extattrctl_args *uap)
3051 struct thread *td = curthread;
3052 struct nameidata nd;
3056 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3057 if ((error = namei(&nd)) != 0)
3059 mp = nd.ni_vp->v_mount;
3061 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3062 SCARG(uap, arg), td));
3066 * Syscall to set a named extended attribute on a file or directory.
3067 * Accepts attribute name, and a uio structure pointing to the data to set.
3068 * The uio is consumed in the style of writev(). The real work happens
3069 * in VOP_SETEXTATTR().
3072 extattr_set_file(struct extattr_set_file_args *uap)
3074 struct thread *td = curthread;
3075 struct proc *p = td->td_proc;
3076 struct nameidata nd;
3078 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3079 char attrname[EXTATTR_MAXNAMELEN];
3083 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3086 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3087 SCARG(uap, path), td);
3088 if ((error = namei(&nd)) != 0)
3090 iovlen = uap->iovcnt * sizeof(struct iovec);
3091 if (uap->iovcnt > UIO_SMALLIOV) {
3092 if (uap->iovcnt > UIO_MAXIOV) {
3096 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3101 auio.uio_iovcnt = uap->iovcnt;
3102 auio.uio_rw = UIO_WRITE;
3103 auio.uio_segflg = UIO_USERSPACE;
3105 auio.uio_offset = 0;
3106 if ((error = copyin(uap->iovp, iov, iovlen)))
3109 for (i = 0; i < uap->iovcnt; i++) {
3110 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3114 auio.uio_resid += iov->iov_len;
3117 cnt = auio.uio_resid;
3118 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3119 cnt -= auio.uio_resid;
3120 uap->sysmsg_result = cnt;
3123 FREE(needfree, M_IOV);
3129 * Syscall to get a named extended attribute on a file or directory.
3130 * Accepts attribute name, and a uio structure pointing to a buffer for the
3131 * data. The uio is consumed in the style of readv(). The real work
3132 * happens in VOP_GETEXTATTR();
3135 extattr_get_file(struct extattr_get_file_args *uap)
3137 struct thread *td = curthread;
3138 struct proc *p = td->td_proc;
3139 struct nameidata nd;
3141 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3142 char attrname[EXTATTR_MAXNAMELEN];
3146 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3149 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3150 SCARG(uap, path), td);
3151 if ((error = namei(&nd)) != 0)
3153 iovlen = uap->iovcnt * sizeof (struct iovec);
3154 if (uap->iovcnt > UIO_SMALLIOV) {
3155 if (uap->iovcnt > UIO_MAXIOV) {
3159 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3166 auio.uio_iovcnt = uap->iovcnt;
3167 auio.uio_rw = UIO_READ;
3168 auio.uio_segflg = UIO_USERSPACE;
3170 auio.uio_offset = 0;
3171 if ((error = copyin(uap->iovp, iov, iovlen)))
3174 for (i = 0; i < uap->iovcnt; i++) {
3175 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3179 auio.uio_resid += iov->iov_len;
3182 cnt = auio.uio_resid;
3183 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3184 cnt -= auio.uio_resid;
3185 uap->sysmsg_result = cnt;
3188 FREE(needfree, M_IOV);
3194 * Syscall to delete a named extended attribute from a file or directory.
3195 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3198 extattr_delete_file(struct extattr_delete_file_args *uap)
3200 struct thread *td = curthread;
3201 struct proc *p = td->td_proc;
3202 struct nameidata nd;
3203 char attrname[EXTATTR_MAXNAMELEN];
3206 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3209 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3210 SCARG(uap, path), td);
3211 if ((error = namei(&nd)) != 0)
3213 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);
3219 * print out statistics from the current status of the buffer pool
3220 * this can be toggeled by the system control option debug.syncprt
3229 int counts[(MAXBSIZE / PAGE_SIZE) + 1];
3230 static char *bname[3] = { "LOCKED", "LRU", "AGE" };
3232 for (dp = bufqueues, i = 0; dp < &bufqueues[3]; dp++, i++) {
3234 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3237 TAILQ_FOREACH(bp, dp, b_freelist) {
3238 counts[bp->b_bufsize/PAGE_SIZE]++;
3242 printf("%s: total-%d", bname[i], count);
3243 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3245 printf(", %d-%d", j * PAGE_SIZE, counts[j]);