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.3 2003/06/23 17:55:41 dillon Exp $
43 /* For 4.3 integer FS ID compatibility */
44 #include "opt_compat.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/sysent.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/sysproto.h>
53 #include <sys/namei.h>
54 #include <sys/filedesc.h>
55 #include <sys/kernel.h>
56 #include <sys/fcntl.h>
58 #include <sys/linker.h>
60 #include <sys/unistd.h>
61 #include <sys/vnode.h>
63 #include <sys/dirent.h>
64 #include <sys/extattr.h>
66 #include <machine/limits.h>
67 #include <miscfs/union/union.h>
68 #include <sys/sysctl.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_zone.h>
72 #include <vm/vm_page.h>
74 static int change_dir __P((struct nameidata *ndp, struct proc *p));
75 static void checkdirs __P((struct vnode *olddp));
76 static int chroot_refuse_vdir_fds __P((struct filedesc *fdp));
77 static int getutimes __P((const struct timeval *, struct timespec *));
78 static int setfown __P((struct vnode *, uid_t, gid_t));
79 static int setfmode __P((struct vnode *, int));
80 static int setfflags __P((struct vnode *, int));
81 static int setutimes __P((struct vnode *, const struct timespec *, int));
82 static int usermount = 0; /* if 1, non-root can mount fs. */
84 int (*union_dircheckp) __P((struct proc *, 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.
95 #ifndef _SYS_SYSPROTO_H_
104 * mount_args(char *type, char *path, int flags, caddr_t data)
108 mount(struct mount_args *uap)
110 struct proc *p = curproc;
113 struct vfsconf *vfsp;
114 int error, flag = 0, flag2 = 0;
120 char fstypename[MFSNAMELEN];
122 if (usermount == 0 && (error = suser()))
125 * Do not allow NFS export by non-root users.
127 if (SCARG(uap, flags) & MNT_EXPORTED) {
133 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
135 if (suser_xxx(p->p_ucred, 0))
136 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
138 * Get vnode to be covered
140 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
141 SCARG(uap, path), p);
142 if ((error = namei(&nd)) != 0)
144 NDFREE(&nd, NDF_ONLY_PNBUF);
146 if (SCARG(uap, flags) & MNT_UPDATE) {
147 if ((vp->v_flag & VROOT) == 0) {
153 flag2 = mp->mnt_kern_flag;
155 * We only allow the filesystem to be reloaded if it
156 * is currently mounted read-only.
158 if ((SCARG(uap, flags) & MNT_RELOAD) &&
159 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
161 return (EOPNOTSUPP); /* Needs translation */
164 * Only root, or the user that did the original mount is
165 * permitted to update it.
167 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
172 if (vfs_busy(mp, LK_NOWAIT, 0, p)) {
176 simple_lock(&vp->v_interlock);
177 if ((vp->v_flag & VMOUNT) != 0 ||
178 vp->v_mountedhere != NULL) {
179 simple_unlock(&vp->v_interlock);
184 vp->v_flag |= VMOUNT;
185 simple_unlock(&vp->v_interlock);
187 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
188 VOP_UNLOCK(vp, 0, p);
192 * If the user is not root, ensure that they own the directory
193 * onto which we are attempting to mount.
195 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) ||
196 (va.va_uid != p->p_ucred->cr_uid &&
197 (error = suser()))) {
201 if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0) {
205 if (vp->v_type != VDIR) {
211 * Historically filesystem types were identified by number. If we
212 * get an integer for the filesystem type instead of a string, we
213 * check to see if it matches one of the historic filesystem types.
215 fstypenum = (uintptr_t)SCARG(uap, type);
216 if (fstypenum < maxvfsconf) {
217 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
218 if (vfsp->vfc_typenum == fstypenum)
224 strncpy(fstypename, vfsp->vfc_name, MFSNAMELEN);
226 #endif /* COMPAT_43 */
227 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
231 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
232 if (!strcmp(vfsp->vfc_name, fstypename))
237 /* Only load modules for root (very important!) */
238 if ((error = suser()) != 0) {
242 error = linker_load_file(fstypename, &lf);
243 if (error || lf == NULL) {
250 /* lookup again, see if the VFS was loaded */
251 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
252 if (!strcmp(vfsp->vfc_name, fstypename))
256 linker_file_unload(lf);
261 simple_lock(&vp->v_interlock);
262 if ((vp->v_flag & VMOUNT) != 0 ||
263 vp->v_mountedhere != NULL) {
264 simple_unlock(&vp->v_interlock);
268 vp->v_flag |= VMOUNT;
269 simple_unlock(&vp->v_interlock);
272 * Allocate and initialize the filesystem.
274 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
275 bzero((char *)mp, (u_long)sizeof(struct mount));
276 TAILQ_INIT(&mp->mnt_nvnodelist);
277 TAILQ_INIT(&mp->mnt_reservedvnlist);
278 mp->mnt_nvnodelistsize = 0;
279 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
280 (void)vfs_busy(mp, LK_NOWAIT, 0, p);
281 mp->mnt_op = vfsp->vfc_vfsops;
283 vfsp->vfc_refcount++;
284 mp->mnt_stat.f_type = vfsp->vfc_typenum;
285 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
286 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
287 mp->mnt_vnodecovered = vp;
288 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
289 mp->mnt_iosize_max = DFLTPHYS;
290 VOP_UNLOCK(vp, 0, p);
293 * Set the mount level flags.
295 if (SCARG(uap, flags) & MNT_RDONLY)
296 mp->mnt_flag |= MNT_RDONLY;
297 else if (mp->mnt_flag & MNT_RDONLY)
298 mp->mnt_kern_flag |= MNTK_WANTRDWR;
299 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
300 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
301 MNT_NOSYMFOLLOW | MNT_IGNORE |
302 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
303 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
304 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
305 MNT_NOSYMFOLLOW | MNT_IGNORE |
306 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
308 * Mount the filesystem.
309 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
310 * get. No freeing of cn_pnbuf.
312 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
313 if (mp->mnt_flag & MNT_UPDATE) {
314 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
315 mp->mnt_flag &= ~MNT_RDONLY;
316 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
317 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
320 mp->mnt_kern_flag = flag2;
322 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
323 if (mp->mnt_syncer == NULL)
324 error = vfs_allocate_syncvnode(mp);
326 if (mp->mnt_syncer != NULL)
327 vrele(mp->mnt_syncer);
328 mp->mnt_syncer = NULL;
331 simple_lock(&vp->v_interlock);
332 vp->v_flag &= ~VMOUNT;
333 simple_unlock(&vp->v_interlock);
337 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
339 * Put the new filesystem on the mount list after root.
343 simple_lock(&vp->v_interlock);
344 vp->v_flag &= ~VMOUNT;
345 vp->v_mountedhere = mp;
346 simple_unlock(&vp->v_interlock);
347 simple_lock(&mountlist_slock);
348 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
349 simple_unlock(&mountlist_slock);
351 VOP_UNLOCK(vp, 0, p);
352 if ((mp->mnt_flag & MNT_RDONLY) == 0)
353 error = vfs_allocate_syncvnode(mp);
355 if ((error = VFS_START(mp, 0, p)) != 0)
358 simple_lock(&vp->v_interlock);
359 vp->v_flag &= ~VMOUNT;
360 simple_unlock(&vp->v_interlock);
361 mp->mnt_vfc->vfc_refcount--;
363 free((caddr_t)mp, M_MOUNT);
370 * Scan all active processes to see if any of them have a current
371 * or root directory onto which the new filesystem has just been
372 * mounted. If so, replace them with the new mount point.
378 struct filedesc *fdp;
382 if (olddp->v_usecount == 1)
384 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
385 panic("mount: lost mount");
386 LIST_FOREACH(p, &allproc, p_list) {
388 if (fdp->fd_cdir == olddp) {
391 fdp->fd_cdir = newdp;
393 if (fdp->fd_rdir == olddp) {
396 fdp->fd_rdir = newdp;
399 if (rootvnode == olddp) {
408 * Unmount a file system.
410 * Note: unmount takes a path to the vnode mounted on as argument,
411 * not special file (as before).
413 #ifndef _SYS_SYSPROTO_H_
414 struct unmount_args {
420 * umount_args(char *path, int flags)
424 unmount(struct unmount_args *uap)
426 struct proc *p = curproc;
432 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
433 SCARG(uap, path), p);
434 if ((error = namei(&nd)) != 0)
437 NDFREE(&nd, NDF_ONLY_PNBUF);
441 * Only root, or the user that did the original mount is
442 * permitted to unmount this filesystem.
444 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
451 * Don't allow unmounting the root file system.
453 if (mp->mnt_flag & MNT_ROOTFS) {
459 * Must be the root of the filesystem
461 if ((vp->v_flag & VROOT) == 0) {
466 return (dounmount(mp, SCARG(uap, flags), p));
470 * Do the actual file system unmount.
473 dounmount(mp, flags, p)
474 register struct mount *mp;
478 struct vnode *coveredvp;
482 simple_lock(&mountlist_slock);
483 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
484 simple_unlock(&mountlist_slock);
487 mp->mnt_kern_flag |= MNTK_UNMOUNT;
488 /* Allow filesystems to detect that a forced unmount is in progress. */
489 if (flags & MNT_FORCE)
490 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
491 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
492 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_slock, p);
494 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
495 if (mp->mnt_kern_flag & MNTK_MWAIT)
500 if (mp->mnt_flag & MNT_EXPUBLIC)
501 vfs_setpublicfs(NULL, NULL, NULL);
503 vfs_msync(mp, MNT_WAIT);
504 async_flag = mp->mnt_flag & MNT_ASYNC;
505 mp->mnt_flag &=~ MNT_ASYNC;
506 cache_purgevfs(mp); /* remove cache entries for this file sys */
507 if (mp->mnt_syncer != NULL)
508 vrele(mp->mnt_syncer);
509 if (((mp->mnt_flag & MNT_RDONLY) ||
510 (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) ||
512 error = VFS_UNMOUNT(mp, flags, p);
513 simple_lock(&mountlist_slock);
515 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
516 (void) vfs_allocate_syncvnode(mp);
517 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
518 mp->mnt_flag |= async_flag;
519 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
520 &mountlist_slock, p);
521 if (mp->mnt_kern_flag & MNTK_MWAIT)
525 TAILQ_REMOVE(&mountlist, mp, mnt_list);
526 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
527 coveredvp->v_mountedhere = (struct mount *)0;
530 mp->mnt_vfc->vfc_refcount--;
531 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
532 panic("unmount: dangling vnode");
533 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock, p);
534 if (mp->mnt_kern_flag & MNTK_MWAIT)
536 free((caddr_t)mp, M_MOUNT);
541 * Sync each mounted filesystem.
543 #ifndef _SYS_SYSPROTO_H_
550 static int syncprt = 0;
551 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
556 sync(struct sync_args *uap)
558 struct proc *p = curproc;
559 struct mount *mp, *nmp;
562 simple_lock(&mountlist_slock);
563 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
564 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
565 nmp = TAILQ_NEXT(mp, mnt_list);
568 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
569 asyncflag = mp->mnt_flag & MNT_ASYNC;
570 mp->mnt_flag &= ~MNT_ASYNC;
571 vfs_msync(mp, MNT_NOWAIT);
572 VFS_SYNC(mp, MNT_NOWAIT,
573 ((p != NULL) ? p->p_ucred : NOCRED), p);
574 mp->mnt_flag |= asyncflag;
576 simple_lock(&mountlist_slock);
577 nmp = TAILQ_NEXT(mp, mnt_list);
580 simple_unlock(&mountlist_slock);
583 * XXX don't call vfs_bufstats() yet because that routine
584 * was not imported in the Lite2 merge.
589 #endif /* DIAGNOSTIC */
594 /* XXX PRISON: could be per prison flag */
595 static int prison_quotas;
597 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
601 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
603 * Change filesystem quotas.
607 quotactl(struct quotactl_args *uap)
609 struct proc *p = curproc;
614 if (p->p_ucred->cr_prison && !prison_quotas)
616 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
617 if ((error = namei(&nd)) != 0)
619 mp = nd.ni_vp->v_mount;
620 NDFREE(&nd, NDF_ONLY_PNBUF);
622 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
623 SCARG(uap, arg), p));
627 * statfs_args(char *path, struct statfs *buf)
629 * Get filesystem statistics.
633 statfs(struct statfs_args *uap)
635 struct proc *p = curproc;
642 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
643 if ((error = namei(&nd)) != 0)
645 mp = nd.ni_vp->v_mount;
647 NDFREE(&nd, NDF_ONLY_PNBUF);
649 error = VFS_STATFS(mp, sp, p);
652 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
653 if (suser_xxx(p->p_ucred, 0)) {
654 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
655 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
658 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
662 * fstatfs_args(int fd, struct statfs *buf)
664 * Get filesystem statistics.
668 fstatfs(struct fstatfs_args *uap)
670 struct proc *p = curproc;
673 register struct statfs *sp;
677 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
679 mp = ((struct vnode *)fp->f_data)->v_mount;
683 error = VFS_STATFS(mp, sp, p);
686 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
687 if (suser_xxx(p->p_ucred, 0)) {
688 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
689 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
692 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
696 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
698 * Get statistics on all filesystems.
702 getfsstat(struct getfsstat_args *uap)
704 struct proc *p = curproc;
705 struct mount *mp, *nmp;
708 long count, maxcount, error;
710 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
711 sfsp = (caddr_t)SCARG(uap, buf);
713 simple_lock(&mountlist_slock);
714 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
715 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
716 nmp = TAILQ_NEXT(mp, mnt_list);
719 if (sfsp && count < maxcount) {
722 * If MNT_NOWAIT or MNT_LAZY is specified, do not
723 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
724 * overrides MNT_WAIT.
726 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
727 (SCARG(uap, flags) & MNT_WAIT)) &&
728 (error = VFS_STATFS(mp, sp, p))) {
729 simple_lock(&mountlist_slock);
730 nmp = TAILQ_NEXT(mp, mnt_list);
734 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
735 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
743 simple_lock(&mountlist_slock);
744 nmp = TAILQ_NEXT(mp, mnt_list);
747 simple_unlock(&mountlist_slock);
748 if (sfsp && count > maxcount)
749 p->p_retval[0] = maxcount;
751 p->p_retval[0] = count;
756 * fchdir_args(int fd)
758 * Change current working directory to a given file descriptor.
762 fchdir(struct fchdir_args *uap)
764 struct proc *p = curproc;
765 struct filedesc *fdp = p->p_fd;
766 struct vnode *vp, *tdp;
771 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
773 vp = (struct vnode *)fp->f_data;
775 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
776 if (vp->v_type != VDIR)
779 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
780 while (!error && (mp = vp->v_mountedhere) != NULL) {
781 if (vfs_busy(mp, 0, 0, p))
783 error = VFS_ROOT(mp, &tdp);
794 VOP_UNLOCK(vp, 0, p);
801 * chdir_args(char *path)
803 * Change current working directory (``.'').
807 chdir(struct chdir_args *uap)
809 struct proc *p = curproc;
810 struct filedesc *fdp = p->p_fd;
814 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
815 SCARG(uap, path), p);
816 if ((error = change_dir(&nd, p)) != 0)
818 NDFREE(&nd, NDF_ONLY_PNBUF);
820 fdp->fd_cdir = nd.ni_vp;
825 * Helper function for raised chroot(2) security function: Refuse if
826 * any filedescriptors are open directories.
829 chroot_refuse_vdir_fds(fdp)
830 struct filedesc *fdp;
837 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
838 error = getvnode(fdp, fd, &fp);
841 vp = (struct vnode *)fp->f_data;
842 if (vp->v_type != VDIR)
850 * This sysctl determines if we will allow a process to chroot(2) if it
851 * has a directory open:
852 * 0: disallowed for all processes.
853 * 1: allowed for processes that were not already chroot(2)'ed.
854 * 2: allowed for all processes.
857 static int chroot_allow_open_directories = 1;
859 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
860 &chroot_allow_open_directories, 0, "");
863 * chroot_args(char *path)
865 * Change notion of root (``/'') directory.
869 chroot(struct chroot_args *uap)
871 struct proc *p = curproc;
872 struct filedesc *fdp = p->p_fd;
876 error = suser_xxx(0, PRISON_ROOT);
879 if (chroot_allow_open_directories == 0 ||
880 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode))
881 error = chroot_refuse_vdir_fds(fdp);
884 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
885 SCARG(uap, path), p);
886 if ((error = change_dir(&nd, p)) != 0)
888 NDFREE(&nd, NDF_ONLY_PNBUF);
890 fdp->fd_rdir = nd.ni_vp;
892 fdp->fd_jdir = nd.ni_vp;
899 * Common routine for chroot and chdir.
902 change_dir(struct nameidata *ndp, struct proc *p)
911 if (vp->v_type != VDIR)
914 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
918 VOP_UNLOCK(vp, 0, p);
923 * open_args(char *path, int flags, int mode)
925 * Check permissions, allocate an open file structure,
926 * and call the device open routine if any.
929 open(struct open_args *uap)
931 struct proc *p = curproc;
932 struct filedesc *fdp = p->p_fd;
935 int cmode, flags, oflags;
937 int type, indx, error;
941 oflags = SCARG(uap, flags);
942 if ((oflags & O_ACCMODE) == O_ACCMODE)
944 flags = FFLAGS(oflags);
945 error = falloc(p, &nfp, &indx);
949 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
950 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
951 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
953 * Bump the ref count to prevent another process from closing
954 * the descriptor while we are blocked in vn_open()
957 error = vn_open(&nd, flags, cmode);
960 * release our own reference
965 * handle special fdopen() case. bleh. dupfdopen() is
966 * responsible for dropping the old contents of ofiles[indx]
969 if ((error == ENODEV || error == ENXIO) &&
970 p->p_dupfd >= 0 && /* XXX from fdopen */
972 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
973 p->p_retval[0] = indx;
977 * Clean up the descriptor, but only if another thread hadn't
978 * replaced or closed it.
980 if (fdp->fd_ofiles[indx] == fp) {
981 fdp->fd_ofiles[indx] = NULL;
985 if (error == ERESTART)
990 NDFREE(&nd, NDF_ONLY_PNBUF);
994 * There should be 2 references on the file, one from the descriptor
995 * table, and one for us.
997 * Handle the case where someone closed the file (via its file
998 * descriptor) while we were blocked. The end result should look
999 * like opening the file succeeded but it was immediately closed.
1001 if (fp->f_count == 1) {
1002 KASSERT(fdp->fd_ofiles[indx] != fp,
1003 ("Open file descriptor lost all refs"));
1004 VOP_UNLOCK(vp, 0, p);
1005 vn_close(vp, flags & FMASK, fp->f_cred, p);
1007 p->p_retval[0] = indx;
1011 fp->f_data = (caddr_t)vp;
1012 fp->f_flag = flags & FMASK;
1014 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1015 if (flags & (O_EXLOCK | O_SHLOCK)) {
1016 lf.l_whence = SEEK_SET;
1019 if (flags & O_EXLOCK)
1020 lf.l_type = F_WRLCK;
1022 lf.l_type = F_RDLCK;
1024 if ((flags & FNONBLOCK) == 0)
1026 VOP_UNLOCK(vp, 0, p);
1027 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1029 * lock request failed. Normally close the descriptor
1030 * but handle the case where someone might have dup()d
1031 * it when we weren't looking. One reference is
1032 * owned by the descriptor array, the other by us.
1034 if (fdp->fd_ofiles[indx] == fp) {
1035 fdp->fd_ofiles[indx] = NULL;
1041 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1042 fp->f_flag |= FHASLOCK;
1044 /* assert that vn_open created a backing object if one is needed */
1045 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1046 ("open: vmio vnode has no backing object after vn_open"));
1047 VOP_UNLOCK(vp, 0, p);
1050 * release our private reference, leaving the one associated with the
1051 * descriptor table intact.
1054 p->p_retval[0] = indx;
1060 * ocreat(char *path, int mode)
1065 ocreat(struct ocreat_args *uap)
1067 struct open_args /* {
1068 syscallarg(char *) path;
1069 syscallarg(int) flags;
1070 syscallarg(int) mode;
1073 SCARG(&nuap, path) = SCARG(uap, path);
1074 SCARG(&nuap, mode) = SCARG(uap, mode);
1075 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
1076 return (open(&nuap));
1078 #endif /* COMPAT_43 */
1081 * mknod_args(char *path, int mode, int dev)
1083 * Create a special file.
1087 mknod(struct mknod_args *uap)
1089 struct proc *p = curproc;
1094 struct nameidata nd;
1096 switch (SCARG(uap, mode) & S_IFMT) {
1102 error = suser_xxx(0, PRISON_ROOT);
1108 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1109 if ((error = namei(&nd)) != 0)
1116 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1117 vattr.va_rdev = SCARG(uap, dev);
1120 switch (SCARG(uap, mode) & S_IFMT) {
1121 case S_IFMT: /* used by badsect to flag bad sectors */
1122 vattr.va_type = VBAD;
1125 vattr.va_type = VCHR;
1128 vattr.va_type = VBLK;
1139 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1141 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1143 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1144 &nd.ni_cnd, &vattr);
1148 NDFREE(&nd, NDF_ONLY_PNBUF);
1151 NDFREE(&nd, NDF_ONLY_PNBUF);
1152 if (nd.ni_dvp == vp)
1159 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
1160 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
1165 * mkfifo_args(char *path, int mode)
1167 * Create a named pipe.
1171 mkfifo(struct mkfifo_args *uap)
1173 struct proc *p = curproc;
1176 struct nameidata nd;
1179 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1180 if ((error = namei(&nd)) != 0)
1182 if (nd.ni_vp != NULL) {
1183 NDFREE(&nd, NDF_ONLY_PNBUF);
1184 if (nd.ni_dvp == nd.ni_vp)
1192 vattr.va_type = VFIFO;
1193 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1194 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1195 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1198 NDFREE(&nd, NDF_ONLY_PNBUF);
1204 * link_args(char *path, char *link)
1206 * Make a hard file link.
1210 link(struct link_args *uap)
1212 struct proc *p = curproc;
1214 struct nameidata nd;
1218 NDINIT(&nd, LOOKUP, FOLLOW|NOOBJ, UIO_USERSPACE, SCARG(uap, path), p);
1219 if ((error = namei(&nd)) != 0)
1221 NDFREE(&nd, NDF_ONLY_PNBUF);
1223 if (vp->v_type == VDIR)
1224 error = EPERM; /* POSIX */
1226 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), p);
1229 if (nd.ni_vp != NULL) {
1234 VOP_LEASE(nd.ni_dvp, p, p->p_ucred,
1236 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1237 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1239 NDFREE(&nd, NDF_ONLY_PNBUF);
1240 if (nd.ni_dvp == nd.ni_vp)
1247 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
1248 ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
1253 * symlink(char *path, char *link)
1255 * Make a symbolic link.
1259 symlink(struct symlink_args *uap)
1261 struct proc *p = curproc;
1265 struct nameidata nd;
1267 path = zalloc(namei_zone);
1268 if ((error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL)) != 0)
1271 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), p);
1272 if ((error = namei(&nd)) != 0)
1275 NDFREE(&nd, NDF_ONLY_PNBUF);
1276 if (nd.ni_dvp == nd.ni_vp)
1285 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1286 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1287 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1288 NDFREE(&nd, NDF_ONLY_PNBUF);
1292 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
1293 ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
1295 zfree(namei_zone, path);
1300 * undelete_args(char *path)
1302 * Delete a whiteout from the filesystem.
1306 undelete(struct undelete_args *uap)
1308 struct proc *p = curproc;
1310 struct nameidata nd;
1313 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1314 SCARG(uap, path), p);
1319 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1320 NDFREE(&nd, NDF_ONLY_PNBUF);
1321 if (nd.ni_dvp == nd.ni_vp)
1330 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1331 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1332 NDFREE(&nd, NDF_ONLY_PNBUF);
1334 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1335 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1340 * unlink_args(char *path)
1342 * Delete a name from the filesystem.
1345 unlink(struct unlink_args *uap)
1347 struct proc *p = curproc;
1350 struct nameidata nd;
1353 NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1354 if ((error = namei(&nd)) != 0)
1357 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1358 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1360 if (vp->v_type == VDIR)
1361 error = EPERM; /* POSIX */
1364 * The root of a mounted filesystem cannot be deleted.
1366 * XXX: can this only be a VDIR case?
1368 if (vp->v_flag & VROOT)
1373 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1374 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1376 NDFREE(&nd, NDF_ONLY_PNBUF);
1377 if (nd.ni_dvp == vp)
1383 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
1384 ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
1389 * lseek_args(int fd, int pad, off_t offset, int whence)
1391 * Reposition read/write file offset.
1394 lseek(struct lseek_args *uap)
1396 struct proc *p = curproc;
1397 struct ucred *cred = p->p_ucred;
1398 struct filedesc *fdp = p->p_fd;
1403 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1404 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1406 if (fp->f_type != DTYPE_VNODE)
1408 switch (SCARG(uap, whence)) {
1410 fp->f_offset += SCARG(uap, offset);
1413 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, cred, p);
1416 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1419 fp->f_offset = SCARG(uap, offset);
1424 *(off_t *)(p->p_retval) = fp->f_offset;
1428 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1430 * Reposition read/write file offset.
1432 * olseek_args(int fd, long offset, int whence)
1435 olseek(struct olseek_args *uap)
1437 struct lseek_args /* {
1439 syscallarg(int) pad;
1440 syscallarg(off_t) offset;
1441 syscallarg(int) whence;
1445 SCARG(&nuap, fd) = SCARG(uap, fd);
1446 SCARG(&nuap, offset) = SCARG(uap, offset);
1447 SCARG(&nuap, whence) = SCARG(uap, whence);
1448 error = lseek(&nuap);
1451 #endif /* COMPAT_43 */
1454 * access_args(char *path, int flags)
1456 * Check access permissions.
1459 access(struct access_args *uap)
1461 struct proc *p = curproc;
1462 struct ucred *cred, *tmpcred;
1465 struct nameidata nd;
1469 * Create and modify a temporary credential instead of one that
1470 * is potentially shared. This could also mess up socket
1471 * buffer accounting which can run in an interrupt context.
1473 tmpcred = crdup(cred);
1474 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1475 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1476 p->p_ucred = tmpcred;
1477 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1478 SCARG(uap, path), p);
1479 if ((error = namei(&nd)) != 0)
1483 /* Flags == 0 means only check for existence. */
1484 if (SCARG(uap, flags)) {
1486 if (SCARG(uap, flags) & R_OK)
1488 if (SCARG(uap, flags) & W_OK)
1490 if (SCARG(uap, flags) & X_OK)
1492 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1493 error = VOP_ACCESS(vp, flags, tmpcred, p);
1495 NDFREE(&nd, NDF_ONLY_PNBUF);
1503 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1505 * ostat_args(char *path, struct ostat *ub)
1507 * Get file status; this version follows links.
1511 ostat(struct ostat_args *uap)
1513 struct proc *p = curproc;
1517 struct nameidata nd;
1519 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1520 SCARG(uap, path), p);
1521 if ((error = namei(&nd)) != 0)
1523 NDFREE(&nd, NDF_ONLY_PNBUF);
1524 error = vn_stat(nd.ni_vp, &sb, p);
1529 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1534 * olstat_args(char *path, struct ostat *ub)
1536 * Get file status; this version does not follow links.
1540 olstat(struct olstat_args *uap)
1542 struct proc *p = curproc;
1547 struct nameidata nd;
1549 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1550 SCARG(uap, path), p);
1551 if ((error = namei(&nd)) != 0)
1554 error = vn_stat(vp, &sb, p);
1555 NDFREE(&nd, NDF_ONLY_PNBUF);
1560 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1565 * Convert from an old to a new stat structure.
1572 ost->st_dev = st->st_dev;
1573 ost->st_ino = st->st_ino;
1574 ost->st_mode = st->st_mode;
1575 ost->st_nlink = st->st_nlink;
1576 ost->st_uid = st->st_uid;
1577 ost->st_gid = st->st_gid;
1578 ost->st_rdev = st->st_rdev;
1579 if (st->st_size < (quad_t)1 << 32)
1580 ost->st_size = st->st_size;
1583 ost->st_atime = st->st_atime;
1584 ost->st_mtime = st->st_mtime;
1585 ost->st_ctime = st->st_ctime;
1586 ost->st_blksize = st->st_blksize;
1587 ost->st_blocks = st->st_blocks;
1588 ost->st_flags = st->st_flags;
1589 ost->st_gen = st->st_gen;
1591 #endif /* COMPAT_43 || COMPAT_SUNOS */
1594 * stat_args(char *path, struct stat *ub)
1596 * Get file status; this version follows links.
1600 stat(struct stat_args *uap)
1602 struct proc *p = curproc;
1605 struct nameidata nd;
1607 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1608 SCARG(uap, path), p);
1609 if ((error = namei(&nd)) != 0)
1611 error = vn_stat(nd.ni_vp, &sb, p);
1612 NDFREE(&nd, NDF_ONLY_PNBUF);
1616 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1621 * lstat_args(char *path, struct stat *ub)
1623 * Get file status; this version does not follow links.
1627 lstat(struct lstat_args *uap)
1629 struct proc *p = curproc;
1633 struct nameidata nd;
1635 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1636 SCARG(uap, path), p);
1637 if ((error = namei(&nd)) != 0)
1640 error = vn_stat(vp, &sb, p);
1641 NDFREE(&nd, NDF_ONLY_PNBUF);
1645 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1654 nsb->st_dev = sb->st_dev;
1655 nsb->st_ino = sb->st_ino;
1656 nsb->st_mode = sb->st_mode;
1657 nsb->st_nlink = sb->st_nlink;
1658 nsb->st_uid = sb->st_uid;
1659 nsb->st_gid = sb->st_gid;
1660 nsb->st_rdev = sb->st_rdev;
1661 nsb->st_atimespec = sb->st_atimespec;
1662 nsb->st_mtimespec = sb->st_mtimespec;
1663 nsb->st_ctimespec = sb->st_ctimespec;
1664 nsb->st_size = sb->st_size;
1665 nsb->st_blocks = sb->st_blocks;
1666 nsb->st_blksize = sb->st_blksize;
1667 nsb->st_flags = sb->st_flags;
1668 nsb->st_gen = sb->st_gen;
1669 nsb->st_qspare[0] = sb->st_qspare[0];
1670 nsb->st_qspare[1] = sb->st_qspare[1];
1674 * nstat_args(char *path, struct nstat *ub)
1678 nstat(struct nstat_args *uap)
1680 struct proc *p = curproc;
1684 struct nameidata nd;
1686 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1687 SCARG(uap, path), p);
1688 if ((error = namei(&nd)) != 0)
1690 NDFREE(&nd, NDF_ONLY_PNBUF);
1691 error = vn_stat(nd.ni_vp, &sb, p);
1695 cvtnstat(&sb, &nsb);
1696 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1701 * lstat_args(char *path, struct stat *ub)
1703 * Get file status; this version does not follow links.
1707 nlstat(struct nlstat_args *uap)
1709 struct proc *p = curproc;
1714 struct nameidata nd;
1716 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1717 SCARG(uap, path), p);
1718 if ((error = namei(&nd)) != 0)
1721 NDFREE(&nd, NDF_ONLY_PNBUF);
1722 error = vn_stat(vp, &sb, p);
1726 cvtnstat(&sb, &nsb);
1727 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1732 * pathconf_Args(char *path, int name)
1734 * Get configurable pathname variables.
1738 pathconf(struct pathconf_args *uap)
1740 struct proc *p = curproc;
1742 struct nameidata nd;
1744 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1745 SCARG(uap, path), p);
1746 if ((error = namei(&nd)) != 0)
1748 NDFREE(&nd, NDF_ONLY_PNBUF);
1749 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), p->p_retval);
1755 * readlink_args(char *path, char *buf, int count)
1757 * Return target name of a symbolic link.
1761 readlink(struct readlink_args *uap)
1763 struct proc *p = curproc;
1768 struct nameidata nd;
1770 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1771 SCARG(uap, path), p);
1772 if ((error = namei(&nd)) != 0)
1774 NDFREE(&nd, NDF_ONLY_PNBUF);
1776 if (vp->v_type != VLNK)
1779 aiov.iov_base = SCARG(uap, buf);
1780 aiov.iov_len = SCARG(uap, count);
1781 auio.uio_iov = &aiov;
1782 auio.uio_iovcnt = 1;
1783 auio.uio_offset = 0;
1784 auio.uio_rw = UIO_READ;
1785 auio.uio_segflg = UIO_USERSPACE;
1787 auio.uio_resid = SCARG(uap, count);
1788 error = VOP_READLINK(vp, &auio, p->p_ucred);
1791 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
1796 setfflags(struct vnode *vp, int flags)
1798 struct proc *p = curproc;
1803 * Prevent non-root users from setting flags on devices. When
1804 * a device is reused, users can retain ownership of the device
1805 * if they are allowed to set flags and programs assume that
1806 * chown can't fail when done as root.
1808 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1809 ((error = suser_xxx(p->p_ucred, PRISON_ROOT)) != 0))
1812 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1813 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1815 vattr.va_flags = flags;
1816 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1817 VOP_UNLOCK(vp, 0, p);
1822 * chflags(char *path, int flags)
1824 * Change flags of a file given a path name.
1828 chflags(struct chflags_args *uap)
1830 struct proc *p = curproc;
1832 struct nameidata nd;
1834 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1835 if ((error = namei(&nd)) != 0)
1837 NDFREE(&nd, NDF_ONLY_PNBUF);
1838 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1844 * fchflags_args(int fd, int flags)
1846 * Change flags of a file given a file descriptor.
1850 fchflags(struct fchflags_args *uap)
1852 struct proc *p = curproc;
1856 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1858 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1862 setfmode(struct vnode *vp, int mode)
1864 struct proc *p = curproc;
1868 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1869 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1871 vattr.va_mode = mode & ALLPERMS;
1872 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1873 VOP_UNLOCK(vp, 0, p);
1878 * chmod_args(char *path, int mode)
1880 * Change mode of a file given path name.
1884 chmod(struct chmod_args *uap)
1886 struct proc *p = curproc;
1888 struct nameidata nd;
1890 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1891 if ((error = namei(&nd)) != 0)
1893 NDFREE(&nd, NDF_ONLY_PNBUF);
1894 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1900 * lchmod_args(char *path, int mode)
1902 * Change mode of a file given path name (don't follow links.)
1906 lchmod(struct lchmod_args *uap)
1908 struct proc *p = curproc;
1910 struct nameidata nd;
1912 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1913 if ((error = namei(&nd)) != 0)
1915 NDFREE(&nd, NDF_ONLY_PNBUF);
1916 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1922 * fchmod_args(int fd, int mode)
1924 * Change mode of a file given a file descriptor.
1928 fchmod(struct fchmod_args *uap)
1930 struct proc *p = curproc;
1934 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1936 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1940 setfown(struct vnode *vp, uid_t uid, gid_t gid)
1942 struct proc *p = curproc;
1946 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1947 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1951 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1952 VOP_UNLOCK(vp, 0, p);
1957 * chown(char *path, int uid, int gid)
1959 * Set ownership given a path name.
1963 chown(struct chown_args *uap)
1965 struct proc *p = curproc;
1967 struct nameidata nd;
1969 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1970 if ((error = namei(&nd)) != 0)
1972 NDFREE(&nd, NDF_ONLY_PNBUF);
1973 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
1979 * lchown_args(char *path, int uid, int gid)
1981 * Set ownership given a path name, do not cross symlinks.
1985 lchown(struct lchown_args *uap)
1987 struct proc *p = curproc;
1989 struct nameidata nd;
1991 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1992 if ((error = namei(&nd)) != 0)
1994 NDFREE(&nd, NDF_ONLY_PNBUF);
1995 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2001 * fchown_args(int fd, int uid, int gid)
2003 * Set ownership given a file descriptor.
2007 fchown(struct fchown_args *uap)
2009 struct proc *p = curproc;
2013 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2015 return setfown((struct vnode *)fp->f_data,
2016 SCARG(uap, uid), SCARG(uap, gid));
2020 getutimes(const struct timeval *usrtvp, struct timespec *tsp)
2022 struct timeval tv[2];
2025 if (usrtvp == NULL) {
2027 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2030 if ((error = copyin(usrtvp, tv, sizeof (tv))) != 0)
2032 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2033 TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]);
2039 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2041 struct proc *p = curproc;
2045 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2046 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2048 vattr.va_atime = ts[0];
2049 vattr.va_mtime = ts[1];
2051 vattr.va_vaflags |= VA_UTIMES_NULL;
2052 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2053 VOP_UNLOCK(vp, 0, p);
2058 * utimes_args(char *path, struct timeval *tptr)
2060 * Set the access and modification times of a file.
2064 utimes(struct utimes_args *uap)
2066 struct proc *p = curproc;
2067 struct timespec ts[2];
2068 struct timeval *usrtvp;
2070 struct nameidata nd;
2072 usrtvp = SCARG(uap, tptr);
2073 if ((error = getutimes(usrtvp, ts)) != 0)
2075 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2076 if ((error = namei(&nd)) != 0)
2078 NDFREE(&nd, NDF_ONLY_PNBUF);
2079 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2085 * lutimes_args(char *path, struct timeval *tptr)
2087 * Set the access and modification times of a file.
2091 lutimes(struct lutimes_args *uap)
2093 struct proc *p = curproc;
2094 struct timespec ts[2];
2095 struct timeval *usrtvp;
2097 struct nameidata nd;
2099 usrtvp = SCARG(uap, tptr);
2100 if ((error = getutimes(usrtvp, ts)) != 0)
2102 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2103 if ((error = namei(&nd)) != 0)
2105 NDFREE(&nd, NDF_ONLY_PNBUF);
2106 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2112 * futimes_args(int fd, struct timeval *tptr)
2114 * Set the access and modification times of a file.
2118 futimes(struct futimes_args *uap)
2120 struct proc *p = curproc;
2121 struct timespec ts[2];
2123 struct timeval *usrtvp;
2126 usrtvp = SCARG(uap, tptr);
2127 if ((error = getutimes(usrtvp, ts)) != 0)
2129 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2131 return setutimes((struct vnode *)fp->f_data, ts, usrtvp == NULL);
2135 * truncate(char *path, int pad, off_t length)
2137 * Truncate a file given its path name.
2141 truncate(struct truncate_args *uap)
2143 struct proc *p = curproc;
2147 struct nameidata nd;
2149 if (uap->length < 0)
2151 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2152 if ((error = namei(&nd)) != 0)
2155 NDFREE(&nd, NDF_ONLY_PNBUF);
2156 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2157 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2158 if (vp->v_type == VDIR)
2160 else if ((error = vn_writechk(vp)) == 0 &&
2161 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
2163 vattr.va_size = SCARG(uap, length);
2164 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2171 * ftruncate_args(int fd, int pad, off_t length)
2173 * Truncate a file given a file descriptor.
2177 ftruncate(struct ftruncate_args *uap)
2179 struct proc *p = curproc;
2185 if (uap->length < 0)
2187 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2189 if ((fp->f_flag & FWRITE) == 0)
2191 vp = (struct vnode *)fp->f_data;
2192 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2193 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2194 if (vp->v_type == VDIR)
2196 else if ((error = vn_writechk(vp)) == 0) {
2198 vattr.va_size = SCARG(uap, length);
2199 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
2201 VOP_UNLOCK(vp, 0, p);
2205 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2207 * otruncate_args(char *path, long length)
2209 * Truncate a file given its path name.
2213 otruncate(struct otruncate_args *uap)
2215 struct truncate_args /* {
2216 syscallarg(char *) path;
2217 syscallarg(int) pad;
2218 syscallarg(off_t) length;
2221 SCARG(&nuap, path) = SCARG(uap, path);
2222 SCARG(&nuap, length) = SCARG(uap, length);
2223 return (truncate(&nuap));
2227 * oftruncate_args(int fd, long length)
2229 * Truncate a file given a file descriptor.
2233 oftruncate(struct oftruncate_args *uap)
2235 struct ftruncate_args /* {
2237 syscallarg(int) pad;
2238 syscallarg(off_t) length;
2241 SCARG(&nuap, fd) = SCARG(uap, fd);
2242 SCARG(&nuap, length) = SCARG(uap, length);
2243 return (ftruncate(&nuap));
2245 #endif /* COMPAT_43 || COMPAT_SUNOS */
2250 * Sync an open file.
2254 fsync(struct fsync_args *uap)
2256 struct proc *p = curproc;
2262 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2264 vp = (struct vnode *)fp->f_data;
2265 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2266 if (VOP_GETVOBJECT(vp, &obj) == 0)
2267 vm_object_page_clean(obj, 0, 0, 0);
2268 if ((error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p)) == 0 &&
2269 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2271 error = (*bioops.io_fsync)(vp);
2272 VOP_UNLOCK(vp, 0, p);
2277 * rename_args(char *from, char *to)
2279 * Rename files. Source and destination must either both be directories,
2280 * or both not be directories. If target is a directory, it must be empty.
2284 rename(struct rename_args *uap)
2286 struct proc *p = curproc;
2287 struct vnode *tvp, *fvp, *tdvp;
2288 struct nameidata fromnd, tond;
2292 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2293 SCARG(uap, from), p);
2294 if ((error = namei(&fromnd)) != 0)
2297 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | NOOBJ,
2298 UIO_USERSPACE, SCARG(uap, to), p);
2299 if (fromnd.ni_vp->v_type == VDIR)
2300 tond.ni_cnd.cn_flags |= WILLBEDIR;
2301 if ((error = namei(&tond)) != 0) {
2302 /* Translate error code for rename("dir1", "dir2/."). */
2303 if (error == EISDIR && fvp->v_type == VDIR)
2305 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2306 vrele(fromnd.ni_dvp);
2313 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2316 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2324 * If the source is the same as the destination (that is, if they
2325 * are links to the same vnode), then there is nothing to do.
2331 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
2332 if (fromnd.ni_dvp != tdvp) {
2333 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2336 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
2338 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2339 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2340 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2341 NDFREE(&tond, NDF_ONLY_PNBUF);
2343 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2344 NDFREE(&tond, NDF_ONLY_PNBUF);
2351 vrele(fromnd.ni_dvp);
2354 vrele(tond.ni_startdir);
2355 ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
2356 ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
2357 ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
2358 ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
2360 if (fromnd.ni_startdir)
2361 vrele(fromnd.ni_startdir);
2368 * mkdir_args(char *path, int mode)
2370 * Make a directory file.
2374 mkdir(struct mkdir_args *uap)
2376 struct proc *p = curproc;
2380 struct nameidata nd;
2383 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2384 nd.ni_cnd.cn_flags |= WILLBEDIR;
2385 if ((error = namei(&nd)) != 0)
2389 NDFREE(&nd, NDF_ONLY_PNBUF);
2390 if (nd.ni_dvp == vp)
2398 vattr.va_type = VDIR;
2399 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2400 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2401 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2402 NDFREE(&nd, NDF_ONLY_PNBUF);
2406 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
2407 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
2412 * rmdir_args(char *path)
2414 * Remove a directory file.
2418 rmdir(struct rmdir_args *uap)
2420 struct proc *p = curproc;
2423 struct nameidata nd;
2426 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2427 SCARG(uap, path), p);
2428 if ((error = namei(&nd)) != 0)
2431 if (vp->v_type != VDIR) {
2436 * No rmdir "." please.
2438 if (nd.ni_dvp == vp) {
2443 * The root of a mounted filesystem cannot be deleted.
2445 if (vp->v_flag & VROOT)
2448 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2449 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2450 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2453 NDFREE(&nd, NDF_ONLY_PNBUF);
2454 if (nd.ni_dvp == vp)
2460 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
2461 ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
2467 * ogetdirentries_args(int fd, char *buf, u_int count, long *basep)
2469 * Read a block of directory entries in a file system independent format.
2472 ogetdirentries(struct ogetdirentries_args *uap)
2474 struct proc *p = curproc;
2477 struct uio auio, kuio;
2478 struct iovec aiov, kiov;
2479 struct dirent *dp, *edp;
2481 int error, eofflag, readcnt;
2484 /* XXX arbitrary sanity limit on `count'. */
2485 if (SCARG(uap, count) > 64 * 1024)
2487 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2489 if ((fp->f_flag & FREAD) == 0)
2491 vp = (struct vnode *)fp->f_data;
2493 if (vp->v_type != VDIR)
2495 aiov.iov_base = SCARG(uap, buf);
2496 aiov.iov_len = SCARG(uap, count);
2497 auio.uio_iov = &aiov;
2498 auio.uio_iovcnt = 1;
2499 auio.uio_rw = UIO_READ;
2500 auio.uio_segflg = UIO_USERSPACE;
2502 auio.uio_resid = SCARG(uap, count);
2503 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2504 loff = auio.uio_offset = fp->f_offset;
2505 # if (BYTE_ORDER != LITTLE_ENDIAN)
2506 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2507 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2509 fp->f_offset = auio.uio_offset;
2514 kuio.uio_iov = &kiov;
2515 kuio.uio_segflg = UIO_SYSSPACE;
2516 kiov.iov_len = SCARG(uap, count);
2517 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2518 kiov.iov_base = dirbuf;
2519 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2521 fp->f_offset = kuio.uio_offset;
2523 readcnt = SCARG(uap, count) - kuio.uio_resid;
2524 edp = (struct dirent *)&dirbuf[readcnt];
2525 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2526 # if (BYTE_ORDER == LITTLE_ENDIAN)
2528 * The expected low byte of
2529 * dp->d_namlen is our dp->d_type.
2530 * The high MBZ byte of dp->d_namlen
2531 * is our dp->d_namlen.
2533 dp->d_type = dp->d_namlen;
2537 * The dp->d_type is the high byte
2538 * of the expected dp->d_namlen,
2539 * so must be zero'ed.
2543 if (dp->d_reclen > 0) {
2544 dp = (struct dirent *)
2545 ((char *)dp + dp->d_reclen);
2552 error = uiomove(dirbuf, readcnt, &auio);
2554 FREE(dirbuf, M_TEMP);
2556 VOP_UNLOCK(vp, 0, p);
2559 if (SCARG(uap, count) == auio.uio_resid) {
2560 if (union_dircheckp) {
2561 error = union_dircheckp(p, &vp, fp);
2567 if ((vp->v_flag & VROOT) &&
2568 (vp->v_mount->mnt_flag & MNT_UNION)) {
2569 struct vnode *tvp = vp;
2570 vp = vp->v_mount->mnt_vnodecovered;
2572 fp->f_data = (caddr_t) vp;
2578 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2580 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2583 #endif /* COMPAT_43 */
2586 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2588 * Read a block of directory entries in a file system independent format.
2591 getdirentries(struct getdirentries_args *uap)
2593 struct proc *p = curproc;
2601 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2603 if ((fp->f_flag & FREAD) == 0)
2605 vp = (struct vnode *)fp->f_data;
2607 if (vp->v_type != VDIR)
2609 aiov.iov_base = SCARG(uap, buf);
2610 aiov.iov_len = SCARG(uap, count);
2611 auio.uio_iov = &aiov;
2612 auio.uio_iovcnt = 1;
2613 auio.uio_rw = UIO_READ;
2614 auio.uio_segflg = UIO_USERSPACE;
2616 auio.uio_resid = SCARG(uap, count);
2617 /* vn_lock(vp, LK_SHARED | LK_RETRY, p); */
2618 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2619 loff = auio.uio_offset = fp->f_offset;
2620 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2621 fp->f_offset = auio.uio_offset;
2622 VOP_UNLOCK(vp, 0, p);
2625 if (SCARG(uap, count) == auio.uio_resid) {
2626 if (union_dircheckp) {
2627 error = union_dircheckp(p, &vp, fp);
2633 if ((vp->v_flag & VROOT) &&
2634 (vp->v_mount->mnt_flag & MNT_UNION)) {
2635 struct vnode *tvp = vp;
2636 vp = vp->v_mount->mnt_vnodecovered;
2638 fp->f_data = (caddr_t) vp;
2644 if (SCARG(uap, basep) != NULL) {
2645 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2648 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2653 * getdents_args(int fd, char *buf, size_t count)
2656 getdents(struct getdents_args *uap)
2658 struct getdirentries_args ap;
2662 ap.count = uap->count;
2664 return getdirentries(&ap);
2668 * umask(int newmask)
2670 * Set the mode mask for creation of filesystem nodes.
2675 umask(struct umask_args *uap)
2677 struct proc *p = curproc;
2678 struct filedesc *fdp;
2681 p->p_retval[0] = fdp->fd_cmask;
2682 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2687 * revoke(char *path)
2689 * Void all references to file by ripping underlying filesystem
2694 revoke(struct revoke_args *uap)
2696 struct proc *p = curproc;
2700 struct nameidata nd;
2702 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2703 if ((error = namei(&nd)) != 0)
2706 NDFREE(&nd, NDF_ONLY_PNBUF);
2707 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2711 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2713 if (p->p_ucred->cr_uid != vattr.va_uid &&
2714 (error = suser_xxx(0, PRISON_ROOT)))
2717 VOP_REVOKE(vp, REVOKEALL);
2724 * Convert a user file descriptor to a kernel file entry.
2727 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2731 if ((u_int)fd >= fdp->fd_nfiles ||
2732 (fp = fdp->fd_ofiles[fd]) == NULL)
2734 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2740 * getfh_args(char *fname, fhandle_t *fhp)
2742 * Get (NFS) file handle
2745 getfh(struct getfh_args *uap)
2747 struct proc *p = curproc;
2748 struct nameidata nd;
2754 * Must be super user
2759 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
2763 NDFREE(&nd, NDF_ONLY_PNBUF);
2765 bzero(&fh, sizeof(fh));
2766 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2767 error = VFS_VPTOFH(vp, &fh.fh_fid);
2771 error = copyout(&fh, uap->fhp, sizeof (fh));
2776 * fhopen_args(const struct fhandle *u_fhp, int flags)
2778 * syscall for the rpc.lockd to use to translate a NFS file handle into
2779 * an open descriptor.
2781 * warning: do not remove the suser() call or this becomes one giant
2785 fhopen(struct fhopen_args *uap)
2787 struct proc *p = curproc;
2792 struct vattr *vap = &vat;
2795 struct filedesc *fdp = p->p_fd;
2796 int fmode, mode, error, type;
2801 * Must be super user
2807 fmode = FFLAGS(SCARG(uap, flags));
2808 /* why not allow a non-read/write open for our lockd? */
2809 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2811 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2814 /* find the mount point */
2815 mp = vfs_getvfs(&fhp.fh_fsid);
2818 /* now give me my vnode, it gets returned to me locked */
2819 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2823 * from now on we have to make sure not
2824 * to forget about the vnode
2825 * any error that causes an abort must vput(vp)
2826 * just set error = err and 'goto bad;'.
2832 if (vp->v_type == VLNK) {
2836 if (vp->v_type == VSOCK) {
2841 if (fmode & (FWRITE | O_TRUNC)) {
2842 if (vp->v_type == VDIR) {
2846 error = vn_writechk(vp);
2854 error = VOP_ACCESS(vp, mode, p->p_ucred, p);
2858 if (fmode & O_TRUNC) {
2859 VOP_UNLOCK(vp, 0, p); /* XXX */
2860 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2861 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */
2864 error = VOP_SETATTR(vp, vap, p->p_ucred, p);
2868 error = VOP_OPEN(vp, fmode, p->p_ucred, p);
2872 * Make sure that a VM object is created for VMIO support.
2874 if (vn_canvmio(vp) == TRUE) {
2875 if ((error = vfs_object_create(vp, p, p->p_ucred)) != 0)
2882 * end of vn_open code
2885 if ((error = falloc(p, &nfp, &indx)) != 0) {
2893 * hold an extra reference to avoid having fp ripped out
2894 * from under us while we block in the lock op.
2897 nfp->f_data = (caddr_t)vp;
2898 nfp->f_flag = fmode & FMASK;
2899 nfp->f_ops = &vnops;
2900 nfp->f_type = DTYPE_VNODE;
2901 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2902 lf.l_whence = SEEK_SET;
2905 if (fmode & O_EXLOCK)
2906 lf.l_type = F_WRLCK;
2908 lf.l_type = F_RDLCK;
2910 if ((fmode & FNONBLOCK) == 0)
2912 VOP_UNLOCK(vp, 0, p);
2913 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2915 * lock request failed. Normally close the descriptor
2916 * but handle the case where someone might have dup()d
2917 * or close()d it when we weren't looking.
2919 if (fdp->fd_ofiles[indx] == fp) {
2920 fdp->fd_ofiles[indx] = NULL;
2925 * release our private reference.
2930 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2931 fp->f_flag |= FHASLOCK;
2933 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2934 vfs_object_create(vp, p, p->p_ucred);
2936 VOP_UNLOCK(vp, 0, p);
2938 p->p_retval[0] = indx;
2947 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2950 fhstat(struct fhstat_args *uap)
2952 struct proc *p = curproc;
2960 * Must be super user
2966 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
2970 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2972 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
2974 error = vn_stat(vp, &sb, p);
2978 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
2983 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
2986 fhstatfs(struct fhstatfs_args *uap)
2988 struct proc *p = curproc;
2997 * Must be super user
2999 if ((error = suser()))
3002 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3005 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3007 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3012 if ((error = VFS_STATFS(mp, sp, p)) != 0)
3014 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3015 if (suser_xxx(p->p_ucred, 0)) {
3016 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3017 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3020 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3024 * Syscall to push extended attribute configuration information into the
3025 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3026 * a command (int cmd), and attribute name and misc data. For now, the
3027 * attribute name is left in userspace for consumption by the VFS_op.
3028 * It will probably be changed to be copied into sysspace by the
3029 * syscall in the future, once issues with various consumers of the
3030 * attribute code have raised their hands.
3032 * Currently this is used only by UFS Extended Attributes.
3035 extattrctl(struct extattrctl_args *uap)
3037 struct proc *p = curproc;
3038 struct nameidata nd;
3042 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3043 if ((error = namei(&nd)) != 0)
3045 mp = nd.ni_vp->v_mount;
3047 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3048 SCARG(uap, arg), p));
3052 * Syscall to set a named extended attribute on a file or directory.
3053 * Accepts attribute name, and a uio structure pointing to the data to set.
3054 * The uio is consumed in the style of writev(). The real work happens
3055 * in VOP_SETEXTATTR().
3058 extattr_set_file(struct extattr_set_file_args *uap)
3060 struct proc *p = curproc;
3061 struct nameidata nd;
3063 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3064 char attrname[EXTATTR_MAXNAMELEN];
3068 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3071 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3072 SCARG(uap, path), p);
3073 if ((error = namei(&nd)) != 0)
3075 iovlen = uap->iovcnt * sizeof(struct iovec);
3076 if (uap->iovcnt > UIO_SMALLIOV) {
3077 if (uap->iovcnt > UIO_MAXIOV) {
3081 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3086 auio.uio_iovcnt = uap->iovcnt;
3087 auio.uio_rw = UIO_WRITE;
3088 auio.uio_segflg = UIO_USERSPACE;
3090 auio.uio_offset = 0;
3091 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3094 for (i = 0; i < uap->iovcnt; i++) {
3095 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3099 auio.uio_resid += iov->iov_len;
3102 cnt = auio.uio_resid;
3103 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, p);
3104 cnt -= auio.uio_resid;
3105 p->p_retval[0] = cnt;
3108 FREE(needfree, M_IOV);
3114 * Syscall to get a named extended attribute on a file or directory.
3115 * Accepts attribute name, and a uio structure pointing to a buffer for the
3116 * data. The uio is consumed in the style of readv(). The real work
3117 * happens in VOP_GETEXTATTR();
3120 extattr_get_file(struct extattr_get_file_args *uap)
3122 struct proc *p = curproc;
3123 struct nameidata nd;
3125 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3126 char attrname[EXTATTR_MAXNAMELEN];
3130 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3133 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3134 SCARG(uap, path), p);
3135 if ((error = namei(&nd)) != 0)
3137 iovlen = uap->iovcnt * sizeof (struct iovec);
3138 if (uap->iovcnt > UIO_SMALLIOV) {
3139 if (uap->iovcnt > UIO_MAXIOV) {
3143 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3150 auio.uio_iovcnt = uap->iovcnt;
3151 auio.uio_rw = UIO_READ;
3152 auio.uio_segflg = UIO_USERSPACE;
3154 auio.uio_offset = 0;
3155 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3158 for (i = 0; i < uap->iovcnt; i++) {
3159 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3163 auio.uio_resid += iov->iov_len;
3166 cnt = auio.uio_resid;
3167 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, p);
3168 cnt -= auio.uio_resid;
3169 p->p_retval[0] = cnt;
3172 FREE(needfree, M_IOV);
3178 * Syscall to delete a named extended attribute from a file or directory.
3179 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3182 extattr_delete_file(struct extattr_delete_file_args *uap)
3184 struct proc *p = curproc;
3185 struct nameidata nd;
3186 char attrname[EXTATTR_MAXNAMELEN];
3189 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3192 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3193 SCARG(uap, path), p);
3194 if ((error = namei(&nd)) != 0)
3196 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, p);