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
42 /* For 4.3 integer FS ID compatibility */
43 #include "opt_compat.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/sysent.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/sysproto.h>
52 #include <sys/namei.h>
53 #include <sys/filedesc.h>
54 #include <sys/kernel.h>
55 #include <sys/fcntl.h>
57 #include <sys/linker.h>
59 #include <sys/unistd.h>
60 #include <sys/vnode.h>
62 #include <sys/dirent.h>
63 #include <sys/extattr.h>
65 #include <machine/limits.h>
66 #include <miscfs/union/union.h>
67 #include <sys/sysctl.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_zone.h>
72 static int change_dir __P((struct nameidata *ndp, struct proc *p));
73 static void checkdirs __P((struct vnode *olddp));
74 static int chroot_refuse_vdir_fds __P((struct filedesc *fdp));
75 static int getutimes __P((const struct timeval *, struct timespec *));
76 static int setfown __P((struct proc *, struct vnode *, uid_t, gid_t));
77 static int setfmode __P((struct proc *, struct vnode *, int));
78 static int setfflags __P((struct proc *, struct vnode *, int));
79 static int setutimes __P((struct proc *, struct vnode *,
80 const struct timespec *, int));
81 static int usermount = 0; /* if 1, non-root can mount fs. */
83 int (*union_dircheckp) __P((struct proc *, struct vnode **, struct file *));
85 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
88 * Virtual File System System Calls
92 * Mount a file system.
94 #ifndef _SYS_SYSPROTO_H_
106 register struct mount_args /* {
107 syscallarg(char *) type;
108 syscallarg(char *) path;
109 syscallarg(int) flags;
110 syscallarg(caddr_t) data;
115 struct vfsconf *vfsp;
116 int error, flag = 0, flag2 = 0;
122 char fstypename[MFSNAMELEN];
124 if (usermount == 0 && (error = suser(p)))
127 * Do not allow NFS export by non-root users.
129 if (SCARG(uap, flags) & MNT_EXPORTED) {
135 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
137 if (suser_xxx(p->p_ucred, 0, 0))
138 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
140 * Get vnode to be covered
142 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
143 SCARG(uap, path), p);
144 if ((error = namei(&nd)) != 0)
146 NDFREE(&nd, NDF_ONLY_PNBUF);
148 if (SCARG(uap, flags) & MNT_UPDATE) {
149 if ((vp->v_flag & VROOT) == 0) {
155 flag2 = mp->mnt_kern_flag;
157 * We only allow the filesystem to be reloaded if it
158 * is currently mounted read-only.
160 if ((SCARG(uap, flags) & MNT_RELOAD) &&
161 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
163 return (EOPNOTSUPP); /* Needs translation */
166 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
168 * Only root, or the user that did the original mount is
169 * permitted to update it.
171 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
172 (error = suser(p))) {
176 if (vfs_busy(mp, LK_NOWAIT, 0, p)) {
180 VOP_UNLOCK(vp, 0, p);
184 * If the user is not root, ensure that they own the directory
185 * onto which we are attempting to mount.
187 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) ||
188 (va.va_uid != p->p_ucred->cr_uid &&
189 (error = suser(p)))) {
193 if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
195 if (vp->v_type != VDIR) {
201 * Historically filesystem types were identified by number. If we
202 * get an integer for the filesystem type instead of a string, we
203 * check to see if it matches one of the historic filesystem types.
205 fstypenum = (uintptr_t)SCARG(uap, type);
206 if (fstypenum < maxvfsconf) {
207 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
208 if (vfsp->vfc_typenum == fstypenum)
214 strncpy(fstypename, vfsp->vfc_name, MFSNAMELEN);
216 #endif /* COMPAT_43 */
217 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
221 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
222 if (!strcmp(vfsp->vfc_name, fstypename))
227 /* Refuse to load modules if securelevel raised */
228 if (securelevel > 0) {
232 /* Only load modules for root (very important!) */
233 if ((error = suser(p)) != 0) {
237 error = linker_load_file(fstypename, &lf);
238 if (error || lf == NULL) {
245 /* lookup again, see if the VFS was loaded */
246 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
247 if (!strcmp(vfsp->vfc_name, fstypename))
251 linker_file_unload(lf);
256 simple_lock(&vp->v_interlock);
257 if ((vp->v_flag & VMOUNT) != 0 ||
258 vp->v_mountedhere != NULL) {
259 simple_unlock(&vp->v_interlock);
263 vp->v_flag |= VMOUNT;
264 simple_unlock(&vp->v_interlock);
267 * Allocate and initialize the filesystem.
269 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
270 bzero((char *)mp, (u_long)sizeof(struct mount));
271 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
272 (void)vfs_busy(mp, LK_NOWAIT, 0, p);
273 mp->mnt_op = vfsp->vfc_vfsops;
275 vfsp->vfc_refcount++;
276 mp->mnt_stat.f_type = vfsp->vfc_typenum;
277 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
278 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
279 mp->mnt_vnodecovered = vp;
280 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
281 mp->mnt_iosize_max = DFLTPHYS;
282 VOP_UNLOCK(vp, 0, p);
285 * Set the mount level flags.
287 if (SCARG(uap, flags) & MNT_RDONLY)
288 mp->mnt_flag |= MNT_RDONLY;
289 else if (mp->mnt_flag & MNT_RDONLY)
290 mp->mnt_kern_flag |= MNTK_WANTRDWR;
291 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
292 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
293 MNT_NOSYMFOLLOW | MNT_IGNORE |
294 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
295 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
296 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
297 MNT_NOSYMFOLLOW | MNT_IGNORE |
298 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
300 * Mount the filesystem.
301 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
302 * get. No freeing of cn_pnbuf.
304 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
305 if (mp->mnt_flag & MNT_UPDATE) {
307 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
308 mp->mnt_flag &= ~MNT_RDONLY;
309 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
310 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
313 mp->mnt_kern_flag = flag2;
315 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
316 if (mp->mnt_syncer == NULL)
317 error = vfs_allocate_syncvnode(mp);
319 if (mp->mnt_syncer != NULL)
320 vrele(mp->mnt_syncer);
321 mp->mnt_syncer = NULL;
326 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
328 * Put the new filesystem on the mount list after root.
332 simple_lock(&vp->v_interlock);
333 vp->v_flag &= ~VMOUNT;
334 vp->v_mountedhere = mp;
335 simple_unlock(&vp->v_interlock);
336 simple_lock(&mountlist_slock);
337 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
338 simple_unlock(&mountlist_slock);
340 VOP_UNLOCK(vp, 0, p);
341 if ((mp->mnt_flag & MNT_RDONLY) == 0)
342 error = vfs_allocate_syncvnode(mp);
344 if ((error = VFS_START(mp, 0, p)) != 0)
347 simple_lock(&vp->v_interlock);
348 vp->v_flag &= ~VMOUNT;
349 simple_unlock(&vp->v_interlock);
350 mp->mnt_vfc->vfc_refcount--;
352 free((caddr_t)mp, M_MOUNT);
359 * Scan all active processes to see if any of them have a current
360 * or root directory onto which the new filesystem has just been
361 * mounted. If so, replace them with the new mount point.
367 struct filedesc *fdp;
371 if (olddp->v_usecount == 1)
373 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
374 panic("mount: lost mount");
375 LIST_FOREACH(p, &allproc, p_list) {
377 if (fdp->fd_cdir == olddp) {
380 fdp->fd_cdir = newdp;
382 if (fdp->fd_rdir == olddp) {
385 fdp->fd_rdir = newdp;
388 if (rootvnode == olddp) {
397 * Unmount a file system.
399 * Note: unmount takes a path to the vnode mounted on as argument,
400 * not special file (as before).
402 #ifndef _SYS_SYSPROTO_H_
403 struct unmount_args {
412 register struct unmount_args /* {
413 syscallarg(char *) path;
414 syscallarg(int) flags;
417 register struct vnode *vp;
422 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
423 SCARG(uap, path), p);
424 if ((error = namei(&nd)) != 0)
427 NDFREE(&nd, NDF_ONLY_PNBUF);
431 * Only root, or the user that did the original mount is
432 * permitted to unmount this filesystem.
434 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
435 (error = suser(p))) {
441 * Don't allow unmounting the root file system.
443 if (mp->mnt_flag & MNT_ROOTFS) {
449 * Must be the root of the filesystem
451 if ((vp->v_flag & VROOT) == 0) {
456 return (dounmount(mp, SCARG(uap, flags), p));
460 * Do the actual file system unmount.
463 dounmount(mp, flags, p)
464 register struct mount *mp;
468 struct vnode *coveredvp;
472 simple_lock(&mountlist_slock);
473 mp->mnt_kern_flag |= MNTK_UNMOUNT;
474 lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_slock, p);
476 if (mp->mnt_flag & MNT_EXPUBLIC)
477 vfs_setpublicfs(NULL, NULL, NULL);
479 vfs_msync(mp, MNT_WAIT);
480 async_flag = mp->mnt_flag & MNT_ASYNC;
481 mp->mnt_flag &=~ MNT_ASYNC;
482 cache_purgevfs(mp); /* remove cache entries for this file sys */
483 if (mp->mnt_syncer != NULL)
484 vrele(mp->mnt_syncer);
485 if (((mp->mnt_flag & MNT_RDONLY) ||
486 (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) ||
488 error = VFS_UNMOUNT(mp, flags, p);
489 simple_lock(&mountlist_slock);
491 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
492 (void) vfs_allocate_syncvnode(mp);
493 mp->mnt_kern_flag &= ~MNTK_UNMOUNT;
494 mp->mnt_flag |= async_flag;
495 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
496 &mountlist_slock, p);
497 if (mp->mnt_kern_flag & MNTK_MWAIT)
501 TAILQ_REMOVE(&mountlist, mp, mnt_list);
502 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
503 coveredvp->v_mountedhere = (struct mount *)0;
506 mp->mnt_vfc->vfc_refcount--;
507 if (!LIST_EMPTY(&mp->mnt_vnodelist))
508 panic("unmount: dangling vnode");
509 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock, p);
510 if (mp->mnt_kern_flag & MNTK_MWAIT)
512 free((caddr_t)mp, M_MOUNT);
517 * Sync each mounted filesystem.
519 #ifndef _SYS_SYSPROTO_H_
526 static int syncprt = 0;
527 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
534 struct sync_args *uap;
536 register struct mount *mp, *nmp;
539 simple_lock(&mountlist_slock);
540 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
541 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
542 nmp = TAILQ_NEXT(mp, mnt_list);
545 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
546 asyncflag = mp->mnt_flag & MNT_ASYNC;
547 mp->mnt_flag &= ~MNT_ASYNC;
548 vfs_msync(mp, MNT_NOWAIT);
549 VFS_SYNC(mp, MNT_NOWAIT,
550 ((p != NULL) ? p->p_ucred : NOCRED), p);
551 mp->mnt_flag |= asyncflag;
553 simple_lock(&mountlist_slock);
554 nmp = TAILQ_NEXT(mp, mnt_list);
557 simple_unlock(&mountlist_slock);
560 * XXX don't call vfs_bufstats() yet because that routine
561 * was not imported in the Lite2 merge.
566 #endif /* DIAGNOSTIC */
571 /* XXX PRISON: could be per prison flag */
572 static int prison_quotas;
574 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
578 * Change filesystem quotas.
580 #ifndef _SYS_SYSPROTO_H_
581 struct quotactl_args {
592 register struct quotactl_args /* {
593 syscallarg(char *) path;
596 syscallarg(caddr_t) arg;
599 register struct mount *mp;
603 if (p->p_prison && !prison_quotas)
605 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
606 if ((error = namei(&nd)) != 0)
608 mp = nd.ni_vp->v_mount;
609 NDFREE(&nd, NDF_ONLY_PNBUF);
611 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
612 SCARG(uap, arg), p));
616 * Get filesystem statistics.
618 #ifndef _SYS_SYSPROTO_H_
628 register struct statfs_args /* {
629 syscallarg(char *) path;
630 syscallarg(struct statfs *) buf;
633 register struct mount *mp;
634 register struct statfs *sp;
639 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
640 if ((error = namei(&nd)) != 0)
642 mp = nd.ni_vp->v_mount;
644 NDFREE(&nd, NDF_ONLY_PNBUF);
646 error = VFS_STATFS(mp, sp, p);
649 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
650 if (suser_xxx(p->p_ucred, 0, 0)) {
651 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
652 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
655 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
659 * Get filesystem statistics.
661 #ifndef _SYS_SYSPROTO_H_
662 struct fstatfs_args {
671 register struct fstatfs_args /* {
673 syscallarg(struct statfs *) buf;
678 register struct statfs *sp;
682 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
684 mp = ((struct vnode *)fp->f_data)->v_mount;
686 error = VFS_STATFS(mp, sp, p);
689 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
690 if (suser_xxx(p->p_ucred, 0, 0)) {
691 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
692 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
695 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
699 * Get statistics on all filesystems.
701 #ifndef _SYS_SYSPROTO_H_
702 struct getfsstat_args {
711 register struct getfsstat_args /* {
712 syscallarg(struct statfs *) buf;
713 syscallarg(long) bufsize;
714 syscallarg(int) flags;
717 register struct mount *mp, *nmp;
718 register struct statfs *sp;
720 long count, maxcount, error;
722 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
723 sfsp = (caddr_t)SCARG(uap, buf);
725 simple_lock(&mountlist_slock);
726 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
727 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
728 nmp = TAILQ_NEXT(mp, mnt_list);
731 if (sfsp && count < maxcount) {
734 * If MNT_NOWAIT or MNT_LAZY is specified, do not
735 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
736 * overrides MNT_WAIT.
738 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
739 (SCARG(uap, flags) & MNT_WAIT)) &&
740 (error = VFS_STATFS(mp, sp, p))) {
741 simple_lock(&mountlist_slock);
742 nmp = TAILQ_NEXT(mp, mnt_list);
746 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
747 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
755 simple_lock(&mountlist_slock);
756 nmp = TAILQ_NEXT(mp, mnt_list);
759 simple_unlock(&mountlist_slock);
760 if (sfsp && count > maxcount)
761 p->p_retval[0] = maxcount;
763 p->p_retval[0] = count;
768 * Change current working directory to a given file descriptor.
770 #ifndef _SYS_SYSPROTO_H_
779 struct fchdir_args /* {
783 register struct filedesc *fdp = p->p_fd;
784 struct vnode *vp, *tdp;
789 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
791 vp = (struct vnode *)fp->f_data;
793 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
794 if (vp->v_type != VDIR)
797 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
798 while (!error && (mp = vp->v_mountedhere) != NULL) {
799 if (vfs_busy(mp, 0, 0, p))
801 error = VFS_ROOT(mp, &tdp);
812 VOP_UNLOCK(vp, 0, p);
819 * Change current working directory (``.'').
821 #ifndef _SYS_SYSPROTO_H_
830 struct chdir_args /* {
831 syscallarg(char *) path;
834 register struct filedesc *fdp = p->p_fd;
838 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
839 SCARG(uap, path), p);
840 if ((error = change_dir(&nd, p)) != 0)
842 NDFREE(&nd, NDF_ONLY_PNBUF);
844 fdp->fd_cdir = nd.ni_vp;
849 * Helper function for raised chroot(2) security function: Refuse if
850 * any filedescriptors are open directories.
853 chroot_refuse_vdir_fds(fdp)
854 struct filedesc *fdp;
861 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
862 error = getvnode(fdp, fd, &fp);
865 vp = (struct vnode *)fp->f_data;
866 if (vp->v_type != VDIR)
874 * This sysctl determines if we will allow a process to chroot(2) if it
875 * has a directory open:
876 * 0: disallowed for all processes.
877 * 1: allowed for processes that were not already chroot(2)'ed.
878 * 2: allowed for all processes.
881 static int chroot_allow_open_directories = 1;
883 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
884 &chroot_allow_open_directories, 0, "");
887 * Change notion of root (``/'') directory.
889 #ifndef _SYS_SYSPROTO_H_
898 struct chroot_args /* {
899 syscallarg(char *) path;
902 register struct filedesc *fdp = p->p_fd;
906 error = suser_xxx(0, p, PRISON_ROOT);
909 if (chroot_allow_open_directories == 0 ||
910 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode))
911 error = chroot_refuse_vdir_fds(fdp);
914 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
915 SCARG(uap, path), p);
916 if ((error = change_dir(&nd, p)) != 0)
918 NDFREE(&nd, NDF_ONLY_PNBUF);
920 fdp->fd_rdir = nd.ni_vp;
922 fdp->fd_jdir = nd.ni_vp;
929 * Common routine for chroot and chdir.
933 register struct nameidata *ndp;
943 if (vp->v_type != VDIR)
946 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
950 VOP_UNLOCK(vp, 0, p);
955 * Check permissions, allocate an open file structure,
956 * and call the device open routine if any.
958 #ifndef _SYS_SYSPROTO_H_
968 register struct open_args /* {
969 syscallarg(char *) path;
970 syscallarg(int) flags;
971 syscallarg(int) mode;
974 register struct filedesc *fdp = p->p_fd;
975 register struct file *fp;
976 register struct vnode *vp;
977 int cmode, flags, oflags;
979 int type, indx, error;
983 oflags = SCARG(uap, flags);
984 if ((oflags & O_ACCMODE) == O_ACCMODE)
986 flags = FFLAGS(oflags);
987 error = falloc(p, &nfp, &indx);
991 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
992 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
993 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
994 error = vn_open(&nd, flags, cmode);
997 if ((error == ENODEV || error == ENXIO) &&
998 p->p_dupfd >= 0 && /* XXX from fdopen */
1000 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
1001 p->p_retval[0] = indx;
1004 if (error == ERESTART)
1006 fdp->fd_ofiles[indx] = NULL;
1010 NDFREE(&nd, NDF_ONLY_PNBUF);
1013 fp->f_data = (caddr_t)vp;
1014 fp->f_flag = flags & FMASK;
1016 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1017 if (flags & (O_EXLOCK | O_SHLOCK)) {
1018 lf.l_whence = SEEK_SET;
1021 if (flags & O_EXLOCK)
1022 lf.l_type = F_WRLCK;
1024 lf.l_type = F_RDLCK;
1026 if ((flags & FNONBLOCK) == 0)
1028 VOP_UNLOCK(vp, 0, p);
1029 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1030 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
1032 fdp->fd_ofiles[indx] = NULL;
1035 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1036 fp->f_flag |= FHASLOCK;
1038 /* assert that vn_open created a backing object if one is needed */
1039 KASSERT(!vn_canvmio(vp) || vp->v_object != NULL,
1040 ("open: vmio vnode has no backing object after vn_open"));
1041 VOP_UNLOCK(vp, 0, p);
1042 p->p_retval[0] = indx;
1050 #ifndef _SYS_SYSPROTO_H_
1051 struct ocreat_args {
1059 register struct ocreat_args /* {
1060 syscallarg(char *) path;
1061 syscallarg(int) mode;
1064 struct open_args /* {
1065 syscallarg(char *) path;
1066 syscallarg(int) flags;
1067 syscallarg(int) mode;
1070 SCARG(&nuap, path) = SCARG(uap, path);
1071 SCARG(&nuap, mode) = SCARG(uap, mode);
1072 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
1073 return (open(p, &nuap));
1075 #endif /* COMPAT_43 */
1078 * Create a special file.
1080 #ifndef _SYS_SYSPROTO_H_
1091 register struct mknod_args /* {
1092 syscallarg(char *) path;
1093 syscallarg(int) mode;
1094 syscallarg(int) dev;
1097 register struct vnode *vp;
1101 struct nameidata nd;
1103 switch (SCARG(uap, mode) & S_IFMT) {
1109 error = suser_xxx(0, p, PRISON_ROOT);
1115 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1116 if ((error = namei(&nd)) != 0)
1123 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1124 vattr.va_rdev = SCARG(uap, dev);
1127 switch (SCARG(uap, mode) & S_IFMT) {
1128 case S_IFMT: /* used by badsect to flag bad sectors */
1129 vattr.va_type = VBAD;
1132 vattr.va_type = VCHR;
1135 vattr.va_type = VBLK;
1146 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1148 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1150 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1151 &nd.ni_cnd, &vattr);
1155 NDFREE(&nd, NDF_ONLY_PNBUF);
1158 NDFREE(&nd, NDF_ONLY_PNBUF);
1159 if (nd.ni_dvp == vp)
1166 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
1167 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
1172 * Create a named pipe.
1174 #ifndef _SYS_SYSPROTO_H_
1175 struct mkfifo_args {
1184 register struct mkfifo_args /* {
1185 syscallarg(char *) path;
1186 syscallarg(int) mode;
1191 struct nameidata nd;
1194 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1195 if ((error = namei(&nd)) != 0)
1197 if (nd.ni_vp != NULL) {
1198 NDFREE(&nd, NDF_ONLY_PNBUF);
1199 if (nd.ni_dvp == nd.ni_vp)
1207 vattr.va_type = VFIFO;
1208 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1209 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1210 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1213 NDFREE(&nd, NDF_ONLY_PNBUF);
1219 * Make a hard file link.
1221 #ifndef _SYS_SYSPROTO_H_
1231 register struct link_args /* {
1232 syscallarg(char *) path;
1233 syscallarg(char *) link;
1236 register struct vnode *vp;
1237 struct nameidata nd;
1241 NDINIT(&nd, LOOKUP, FOLLOW|NOOBJ, UIO_USERSPACE, SCARG(uap, path), p);
1242 if ((error = namei(&nd)) != 0)
1244 NDFREE(&nd, NDF_ONLY_PNBUF);
1246 if (vp->v_type == VDIR)
1247 error = EPERM; /* POSIX */
1249 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), p);
1252 if (nd.ni_vp != NULL) {
1257 VOP_LEASE(nd.ni_dvp, p, p->p_ucred,
1259 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1260 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1262 NDFREE(&nd, NDF_ONLY_PNBUF);
1263 if (nd.ni_dvp == nd.ni_vp)
1270 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
1271 ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
1276 * Make a symbolic link.
1278 #ifndef _SYS_SYSPROTO_H_
1279 struct symlink_args {
1288 register struct symlink_args /* {
1289 syscallarg(char *) path;
1290 syscallarg(char *) link;
1296 struct nameidata nd;
1298 path = zalloc(namei_zone);
1299 if ((error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL)) != 0)
1302 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), p);
1303 if ((error = namei(&nd)) != 0)
1306 NDFREE(&nd, NDF_ONLY_PNBUF);
1307 if (nd.ni_dvp == nd.ni_vp)
1316 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1317 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1318 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1319 NDFREE(&nd, NDF_ONLY_PNBUF);
1323 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
1324 ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
1326 zfree(namei_zone, path);
1331 * Delete a whiteout from the filesystem.
1337 register struct undelete_args /* {
1338 syscallarg(char *) path;
1342 struct nameidata nd;
1345 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1346 SCARG(uap, path), p);
1351 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1352 NDFREE(&nd, NDF_ONLY_PNBUF);
1353 if (nd.ni_dvp == nd.ni_vp)
1362 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1363 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1364 NDFREE(&nd, NDF_ONLY_PNBUF);
1366 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1367 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1372 * Delete a name from the filesystem.
1374 #ifndef _SYS_SYSPROTO_H_
1375 struct unlink_args {
1383 struct unlink_args /* {
1384 syscallarg(char *) path;
1387 register struct vnode *vp;
1389 struct nameidata nd;
1392 NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1393 if ((error = namei(&nd)) != 0)
1396 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1397 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1399 if (vp->v_type == VDIR)
1400 error = EPERM; /* POSIX */
1403 * The root of a mounted filesystem cannot be deleted.
1405 * XXX: can this only be a VDIR case?
1407 if (vp->v_flag & VROOT)
1412 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1413 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1415 NDFREE(&nd, NDF_ONLY_PNBUF);
1416 if (nd.ni_dvp == vp)
1422 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
1423 ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
1428 * Reposition read/write file offset.
1430 #ifndef _SYS_SYSPROTO_H_
1441 register struct lseek_args /* {
1443 syscallarg(int) pad;
1444 syscallarg(off_t) offset;
1445 syscallarg(int) whence;
1448 struct ucred *cred = p->p_ucred;
1449 register struct filedesc *fdp = p->p_fd;
1450 register struct file *fp;
1454 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1455 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1457 if (fp->f_type != DTYPE_VNODE)
1459 switch (SCARG(uap, whence)) {
1461 fp->f_offset += SCARG(uap, offset);
1464 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, cred, p);
1467 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1470 fp->f_offset = SCARG(uap, offset);
1475 *(off_t *)(p->p_retval) = fp->f_offset;
1479 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1481 * Reposition read/write file offset.
1483 #ifndef _SYS_SYSPROTO_H_
1484 struct olseek_args {
1493 register struct olseek_args /* {
1495 syscallarg(long) offset;
1496 syscallarg(int) whence;
1499 struct lseek_args /* {
1501 syscallarg(int) pad;
1502 syscallarg(off_t) offset;
1503 syscallarg(int) whence;
1507 SCARG(&nuap, fd) = SCARG(uap, fd);
1508 SCARG(&nuap, offset) = SCARG(uap, offset);
1509 SCARG(&nuap, whence) = SCARG(uap, whence);
1510 error = lseek(p, &nuap);
1513 #endif /* COMPAT_43 */
1516 * Check access permissions.
1518 #ifndef _SYS_SYSPROTO_H_
1519 struct access_args {
1527 register struct access_args /* {
1528 syscallarg(char *) path;
1529 syscallarg(int) flags;
1532 register struct ucred *cred = p->p_ucred;
1533 register struct vnode *vp;
1534 int error, flags, t_gid, t_uid;
1535 struct nameidata nd;
1537 t_uid = cred->cr_uid;
1538 t_gid = cred->cr_groups[0];
1539 cred->cr_uid = p->p_cred->p_ruid;
1540 cred->cr_groups[0] = p->p_cred->p_rgid;
1541 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1542 SCARG(uap, path), p);
1543 if ((error = namei(&nd)) != 0)
1547 /* Flags == 0 means only check for existence. */
1548 if (SCARG(uap, flags)) {
1550 if (SCARG(uap, flags) & R_OK)
1552 if (SCARG(uap, flags) & W_OK)
1554 if (SCARG(uap, flags) & X_OK)
1556 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1557 error = VOP_ACCESS(vp, flags, cred, p);
1559 NDFREE(&nd, NDF_ONLY_PNBUF);
1562 cred->cr_uid = t_uid;
1563 cred->cr_groups[0] = t_gid;
1567 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1569 * Get file status; this version follows links.
1571 #ifndef _SYS_SYSPROTO_H_
1581 register struct ostat_args /* {
1582 syscallarg(char *) path;
1583 syscallarg(struct ostat *) ub;
1589 struct nameidata nd;
1591 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1592 SCARG(uap, path), p);
1593 if ((error = namei(&nd)) != 0)
1595 NDFREE(&nd, NDF_ONLY_PNBUF);
1596 error = vn_stat(nd.ni_vp, &sb, p);
1601 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1606 * Get file status; this version does not follow links.
1608 #ifndef _SYS_SYSPROTO_H_
1609 struct olstat_args {
1618 register struct olstat_args /* {
1619 syscallarg(char *) path;
1620 syscallarg(struct ostat *) ub;
1627 struct nameidata nd;
1629 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1630 SCARG(uap, path), p);
1631 if ((error = namei(&nd)) != 0)
1634 error = vn_stat(vp, &sb, p);
1635 NDFREE(&nd, NDF_ONLY_PNBUF);
1640 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1645 * Convert from an old to a new stat structure.
1653 ost->st_dev = st->st_dev;
1654 ost->st_ino = st->st_ino;
1655 ost->st_mode = st->st_mode;
1656 ost->st_nlink = st->st_nlink;
1657 ost->st_uid = st->st_uid;
1658 ost->st_gid = st->st_gid;
1659 ost->st_rdev = st->st_rdev;
1660 if (st->st_size < (quad_t)1 << 32)
1661 ost->st_size = st->st_size;
1664 ost->st_atime = st->st_atime;
1665 ost->st_mtime = st->st_mtime;
1666 ost->st_ctime = st->st_ctime;
1667 ost->st_blksize = st->st_blksize;
1668 ost->st_blocks = st->st_blocks;
1669 ost->st_flags = st->st_flags;
1670 ost->st_gen = st->st_gen;
1672 #endif /* COMPAT_43 || COMPAT_SUNOS */
1675 * Get file status; this version follows links.
1677 #ifndef _SYS_SYSPROTO_H_
1687 register struct stat_args /* {
1688 syscallarg(char *) path;
1689 syscallarg(struct stat *) ub;
1694 struct nameidata nd;
1696 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1697 SCARG(uap, path), p);
1698 if ((error = namei(&nd)) != 0)
1700 error = vn_stat(nd.ni_vp, &sb, p);
1701 NDFREE(&nd, NDF_ONLY_PNBUF);
1705 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1710 * Get file status; this version does not follow links.
1712 #ifndef _SYS_SYSPROTO_H_
1722 register struct lstat_args /* {
1723 syscallarg(char *) path;
1724 syscallarg(struct stat *) ub;
1730 struct nameidata nd;
1732 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1733 SCARG(uap, path), p);
1734 if ((error = namei(&nd)) != 0)
1737 error = vn_stat(vp, &sb, p);
1738 NDFREE(&nd, NDF_ONLY_PNBUF);
1742 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1751 nsb->st_dev = sb->st_dev;
1752 nsb->st_ino = sb->st_ino;
1753 nsb->st_mode = sb->st_mode;
1754 nsb->st_nlink = sb->st_nlink;
1755 nsb->st_uid = sb->st_uid;
1756 nsb->st_gid = sb->st_gid;
1757 nsb->st_rdev = sb->st_rdev;
1758 nsb->st_atimespec = sb->st_atimespec;
1759 nsb->st_mtimespec = sb->st_mtimespec;
1760 nsb->st_ctimespec = sb->st_ctimespec;
1761 nsb->st_size = sb->st_size;
1762 nsb->st_blocks = sb->st_blocks;
1763 nsb->st_blksize = sb->st_blksize;
1764 nsb->st_flags = sb->st_flags;
1765 nsb->st_gen = sb->st_gen;
1766 nsb->st_qspare[0] = sb->st_qspare[0];
1767 nsb->st_qspare[1] = sb->st_qspare[1];
1770 #ifndef _SYS_SYSPROTO_H_
1780 register struct nstat_args /* {
1781 syscallarg(char *) path;
1782 syscallarg(struct nstat *) ub;
1788 struct nameidata nd;
1790 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1791 SCARG(uap, path), p);
1792 if ((error = namei(&nd)) != 0)
1794 NDFREE(&nd, NDF_ONLY_PNBUF);
1795 error = vn_stat(nd.ni_vp, &sb, p);
1799 cvtnstat(&sb, &nsb);
1800 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1805 * Get file status; this version does not follow links.
1807 #ifndef _SYS_SYSPROTO_H_
1817 register struct nlstat_args /* {
1818 syscallarg(char *) path;
1819 syscallarg(struct nstat *) ub;
1826 struct nameidata nd;
1828 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1829 SCARG(uap, path), p);
1830 if ((error = namei(&nd)) != 0)
1833 NDFREE(&nd, NDF_ONLY_PNBUF);
1834 error = vn_stat(vp, &sb, p);
1838 cvtnstat(&sb, &nsb);
1839 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1844 * Get configurable pathname variables.
1846 #ifndef _SYS_SYSPROTO_H_
1847 struct pathconf_args {
1856 register struct pathconf_args /* {
1857 syscallarg(char *) path;
1858 syscallarg(int) name;
1862 struct nameidata nd;
1864 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1865 SCARG(uap, path), p);
1866 if ((error = namei(&nd)) != 0)
1868 NDFREE(&nd, NDF_ONLY_PNBUF);
1869 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), p->p_retval);
1875 * Return target name of a symbolic link.
1877 #ifndef _SYS_SYSPROTO_H_
1878 struct readlink_args {
1888 register struct readlink_args /* {
1889 syscallarg(char *) path;
1890 syscallarg(char *) buf;
1891 syscallarg(int) count;
1894 register struct vnode *vp;
1898 struct nameidata nd;
1900 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1901 SCARG(uap, path), p);
1902 if ((error = namei(&nd)) != 0)
1904 NDFREE(&nd, NDF_ONLY_PNBUF);
1906 if (vp->v_type != VLNK)
1909 aiov.iov_base = SCARG(uap, buf);
1910 aiov.iov_len = SCARG(uap, count);
1911 auio.uio_iov = &aiov;
1912 auio.uio_iovcnt = 1;
1913 auio.uio_offset = 0;
1914 auio.uio_rw = UIO_READ;
1915 auio.uio_segflg = UIO_USERSPACE;
1917 auio.uio_resid = SCARG(uap, count);
1918 error = VOP_READLINK(vp, &auio, p->p_ucred);
1921 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
1926 setfflags(p, vp, flags)
1935 * Prevent non-root users from setting flags on devices. When
1936 * a device is reused, users can retain ownership of the device
1937 * if they are allowed to set flags and programs assume that
1938 * chown can't fail when done as root.
1940 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1941 ((error = suser_xxx(p->p_ucred, p, PRISON_ROOT)) != 0))
1944 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1945 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1947 vattr.va_flags = flags;
1948 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1949 VOP_UNLOCK(vp, 0, p);
1954 * Change flags of a file given a path name.
1956 #ifndef _SYS_SYSPROTO_H_
1957 struct chflags_args {
1966 register struct chflags_args /* {
1967 syscallarg(char *) path;
1968 syscallarg(int) flags;
1972 struct nameidata nd;
1974 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1975 if ((error = namei(&nd)) != 0)
1977 NDFREE(&nd, NDF_ONLY_PNBUF);
1978 error = setfflags(p, nd.ni_vp, SCARG(uap, flags));
1984 * Change flags of a file given a file descriptor.
1986 #ifndef _SYS_SYSPROTO_H_
1987 struct fchflags_args {
1996 register struct fchflags_args /* {
1998 syscallarg(int) flags;
2004 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2006 return setfflags(p, (struct vnode *) fp->f_data, SCARG(uap, flags));
2010 setfmode(p, vp, mode)
2018 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2019 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2021 vattr.va_mode = mode & ALLPERMS;
2022 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2023 VOP_UNLOCK(vp, 0, p);
2028 * Change mode of a file given path name.
2030 #ifndef _SYS_SYSPROTO_H_
2040 register struct chmod_args /* {
2041 syscallarg(char *) path;
2042 syscallarg(int) mode;
2046 struct nameidata nd;
2048 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2049 if ((error = namei(&nd)) != 0)
2051 NDFREE(&nd, NDF_ONLY_PNBUF);
2052 error = setfmode(p, nd.ni_vp, SCARG(uap, mode));
2058 * Change mode of a file given path name (don't follow links.)
2060 #ifndef _SYS_SYSPROTO_H_
2061 struct lchmod_args {
2070 register struct lchmod_args /* {
2071 syscallarg(char *) path;
2072 syscallarg(int) mode;
2076 struct nameidata nd;
2078 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2079 if ((error = namei(&nd)) != 0)
2081 NDFREE(&nd, NDF_ONLY_PNBUF);
2082 error = setfmode(p, nd.ni_vp, SCARG(uap, mode));
2088 * Change mode of a file given a file descriptor.
2090 #ifndef _SYS_SYSPROTO_H_
2091 struct fchmod_args {
2100 register struct fchmod_args /* {
2102 syscallarg(int) mode;
2108 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2110 return setfmode(p, (struct vnode *)fp->f_data, SCARG(uap, mode));
2114 setfown(p, vp, uid, gid)
2123 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2124 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2128 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2129 VOP_UNLOCK(vp, 0, p);
2134 * Set ownership given a path name.
2136 #ifndef _SYS_SYSPROTO_H_
2147 register struct chown_args /* {
2148 syscallarg(char *) path;
2149 syscallarg(int) uid;
2150 syscallarg(int) gid;
2154 struct nameidata nd;
2156 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2157 if ((error = namei(&nd)) != 0)
2159 NDFREE(&nd, NDF_ONLY_PNBUF);
2160 error = setfown(p, nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2166 * Set ownership given a path name, do not cross symlinks.
2168 #ifndef _SYS_SYSPROTO_H_
2169 struct lchown_args {
2179 register struct lchown_args /* {
2180 syscallarg(char *) path;
2181 syscallarg(int) uid;
2182 syscallarg(int) gid;
2186 struct nameidata nd;
2188 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2189 if ((error = namei(&nd)) != 0)
2191 NDFREE(&nd, NDF_ONLY_PNBUF);
2192 error = setfown(p, nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2198 * Set ownership given a file descriptor.
2200 #ifndef _SYS_SYSPROTO_H_
2201 struct fchown_args {
2211 register struct fchown_args /* {
2213 syscallarg(int) uid;
2214 syscallarg(int) gid;
2220 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2222 return setfown(p, (struct vnode *)fp->f_data,
2223 SCARG(uap, uid), SCARG(uap, gid));
2227 getutimes(usrtvp, tsp)
2228 const struct timeval *usrtvp;
2229 struct timespec *tsp;
2231 struct timeval tv[2];
2234 if (usrtvp == NULL) {
2236 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2239 if ((error = copyin(usrtvp, tv, sizeof (tv))) != 0)
2241 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2242 TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]);
2248 setutimes(p, vp, ts, nullflag)
2251 const struct timespec *ts;
2257 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2258 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2260 vattr.va_atime = ts[0];
2261 vattr.va_mtime = ts[1];
2263 vattr.va_vaflags |= VA_UTIMES_NULL;
2264 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2265 VOP_UNLOCK(vp, 0, p);
2270 * Set the access and modification times of a file.
2272 #ifndef _SYS_SYSPROTO_H_
2273 struct utimes_args {
2275 struct timeval *tptr;
2282 register struct utimes_args /* {
2283 syscallarg(char *) path;
2284 syscallarg(struct timeval *) tptr;
2287 struct timespec ts[2];
2288 struct timeval *usrtvp;
2290 struct nameidata nd;
2292 usrtvp = SCARG(uap, tptr);
2293 if ((error = getutimes(usrtvp, ts)) != 0)
2295 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2296 if ((error = namei(&nd)) != 0)
2298 NDFREE(&nd, NDF_ONLY_PNBUF);
2299 error = setutimes(p, nd.ni_vp, ts, usrtvp == NULL);
2305 * Set the access and modification times of a file.
2307 #ifndef _SYS_SYSPROTO_H_
2308 struct lutimes_args {
2310 struct timeval *tptr;
2317 register struct lutimes_args /* {
2318 syscallarg(char *) path;
2319 syscallarg(struct timeval *) tptr;
2322 struct timespec ts[2];
2323 struct timeval *usrtvp;
2325 struct nameidata nd;
2327 usrtvp = SCARG(uap, tptr);
2328 if ((error = getutimes(usrtvp, ts)) != 0)
2330 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2331 if ((error = namei(&nd)) != 0)
2333 NDFREE(&nd, NDF_ONLY_PNBUF);
2334 error = setutimes(p, nd.ni_vp, ts, usrtvp == NULL);
2340 * Set the access and modification times of a file.
2342 #ifndef _SYS_SYSPROTO_H_
2343 struct futimes_args {
2345 struct timeval *tptr;
2352 register struct futimes_args /* {
2353 syscallarg(int ) fd;
2354 syscallarg(struct timeval *) tptr;
2357 struct timespec ts[2];
2359 struct timeval *usrtvp;
2362 usrtvp = SCARG(uap, tptr);
2363 if ((error = getutimes(usrtvp, ts)) != 0)
2365 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2367 return setutimes(p, (struct vnode *)fp->f_data, ts, usrtvp == NULL);
2371 * Truncate a file given its path name.
2373 #ifndef _SYS_SYSPROTO_H_
2374 struct truncate_args {
2384 register struct truncate_args /* {
2385 syscallarg(char *) path;
2386 syscallarg(int) pad;
2387 syscallarg(off_t) length;
2390 register struct vnode *vp;
2393 struct nameidata nd;
2395 if (uap->length < 0)
2397 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2398 if ((error = namei(&nd)) != 0)
2401 NDFREE(&nd, NDF_ONLY_PNBUF);
2402 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2403 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2404 if (vp->v_type == VDIR)
2406 else if ((error = vn_writechk(vp)) == 0 &&
2407 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
2409 vattr.va_size = SCARG(uap, length);
2410 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2417 * Truncate a file given a file descriptor.
2419 #ifndef _SYS_SYSPROTO_H_
2420 struct ftruncate_args {
2430 register struct ftruncate_args /* {
2432 syscallarg(int) pad;
2433 syscallarg(off_t) length;
2441 if (uap->length < 0)
2443 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2445 if ((fp->f_flag & FWRITE) == 0)
2447 vp = (struct vnode *)fp->f_data;
2448 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2449 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2450 if (vp->v_type == VDIR)
2452 else if ((error = vn_writechk(vp)) == 0) {
2454 vattr.va_size = SCARG(uap, length);
2455 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
2457 VOP_UNLOCK(vp, 0, p);
2461 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2463 * Truncate a file given its path name.
2465 #ifndef _SYS_SYSPROTO_H_
2466 struct otruncate_args {
2475 register struct otruncate_args /* {
2476 syscallarg(char *) path;
2477 syscallarg(long) length;
2480 struct truncate_args /* {
2481 syscallarg(char *) path;
2482 syscallarg(int) pad;
2483 syscallarg(off_t) length;
2486 SCARG(&nuap, path) = SCARG(uap, path);
2487 SCARG(&nuap, length) = SCARG(uap, length);
2488 return (truncate(p, &nuap));
2492 * Truncate a file given a file descriptor.
2494 #ifndef _SYS_SYSPROTO_H_
2495 struct oftruncate_args {
2504 register struct oftruncate_args /* {
2506 syscallarg(long) length;
2509 struct ftruncate_args /* {
2511 syscallarg(int) pad;
2512 syscallarg(off_t) length;
2515 SCARG(&nuap, fd) = SCARG(uap, fd);
2516 SCARG(&nuap, length) = SCARG(uap, length);
2517 return (ftruncate(p, &nuap));
2519 #endif /* COMPAT_43 || COMPAT_SUNOS */
2522 * Sync an open file.
2524 #ifndef _SYS_SYSPROTO_H_
2533 struct fsync_args /* {
2537 register struct vnode *vp;
2541 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2543 vp = (struct vnode *)fp->f_data;
2544 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2546 vm_object_page_clean(vp->v_object, 0, 0, 0);
2547 if ((error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p)) == 0 &&
2548 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2550 error = (*bioops.io_fsync)(vp);
2551 VOP_UNLOCK(vp, 0, p);
2556 * Rename files. Source and destination must either both be directories,
2557 * or both not be directories. If target is a directory, it must be empty.
2559 #ifndef _SYS_SYSPROTO_H_
2560 struct rename_args {
2569 register struct rename_args /* {
2570 syscallarg(char *) from;
2571 syscallarg(char *) to;
2574 register struct vnode *tvp, *fvp, *tdvp;
2575 struct nameidata fromnd, tond;
2579 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2580 SCARG(uap, from), p);
2581 if ((error = namei(&fromnd)) != 0)
2584 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | NOOBJ,
2585 UIO_USERSPACE, SCARG(uap, to), p);
2586 if (fromnd.ni_vp->v_type == VDIR)
2587 tond.ni_cnd.cn_flags |= WILLBEDIR;
2588 if ((error = namei(&tond)) != 0) {
2589 /* Translate error code for rename("dir1", "dir2/."). */
2590 if (error == EISDIR && fvp->v_type == VDIR)
2592 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2593 vrele(fromnd.ni_dvp);
2600 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2603 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2611 * If source is the same as the destination (that is the
2612 * same inode number with the same name in the same directory),
2613 * then there is nothing to do.
2615 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2616 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2617 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2618 fromnd.ni_cnd.cn_namelen))
2622 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
2623 if (fromnd.ni_dvp != tdvp) {
2624 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2627 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
2629 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2630 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2631 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2632 NDFREE(&tond, NDF_ONLY_PNBUF);
2634 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2635 NDFREE(&tond, NDF_ONLY_PNBUF);
2642 vrele(fromnd.ni_dvp);
2645 vrele(tond.ni_startdir);
2646 ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
2647 ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
2648 ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
2649 ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
2651 if (fromnd.ni_startdir)
2652 vrele(fromnd.ni_startdir);
2659 * Make a directory file.
2661 #ifndef _SYS_SYSPROTO_H_
2671 register struct mkdir_args /* {
2672 syscallarg(char *) path;
2673 syscallarg(int) mode;
2676 register struct vnode *vp;
2679 struct nameidata nd;
2682 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2683 nd.ni_cnd.cn_flags |= WILLBEDIR;
2684 if ((error = namei(&nd)) != 0)
2688 NDFREE(&nd, NDF_ONLY_PNBUF);
2689 if (nd.ni_dvp == vp)
2697 vattr.va_type = VDIR;
2698 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2699 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2700 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2701 NDFREE(&nd, NDF_ONLY_PNBUF);
2705 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
2706 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
2711 * Remove a directory file.
2713 #ifndef _SYS_SYSPROTO_H_
2722 struct rmdir_args /* {
2723 syscallarg(char *) path;
2726 register struct vnode *vp;
2728 struct nameidata nd;
2731 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2732 SCARG(uap, path), p);
2733 if ((error = namei(&nd)) != 0)
2736 if (vp->v_type != VDIR) {
2741 * No rmdir "." please.
2743 if (nd.ni_dvp == vp) {
2748 * The root of a mounted filesystem cannot be deleted.
2750 if (vp->v_flag & VROOT)
2753 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2754 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2755 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2758 NDFREE(&nd, NDF_ONLY_PNBUF);
2759 if (nd.ni_dvp == vp)
2765 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
2766 ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
2772 * Read a block of directory entries in a file system independent format.
2774 #ifndef _SYS_SYSPROTO_H_
2775 struct ogetdirentries_args {
2783 ogetdirentries(p, uap)
2785 register struct ogetdirentries_args /* {
2787 syscallarg(char *) buf;
2788 syscallarg(u_int) count;
2789 syscallarg(long *) basep;
2794 struct uio auio, kuio;
2795 struct iovec aiov, kiov;
2796 struct dirent *dp, *edp;
2798 int error, eofflag, readcnt;
2801 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2803 if ((fp->f_flag & FREAD) == 0)
2805 vp = (struct vnode *)fp->f_data;
2807 if (vp->v_type != VDIR)
2809 aiov.iov_base = SCARG(uap, buf);
2810 aiov.iov_len = SCARG(uap, count);
2811 auio.uio_iov = &aiov;
2812 auio.uio_iovcnt = 1;
2813 auio.uio_rw = UIO_READ;
2814 auio.uio_segflg = UIO_USERSPACE;
2816 auio.uio_resid = SCARG(uap, count);
2817 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2818 loff = auio.uio_offset = fp->f_offset;
2819 # if (BYTE_ORDER != LITTLE_ENDIAN)
2820 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2821 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2823 fp->f_offset = auio.uio_offset;
2828 kuio.uio_iov = &kiov;
2829 kuio.uio_segflg = UIO_SYSSPACE;
2830 kiov.iov_len = SCARG(uap, count);
2831 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2832 kiov.iov_base = dirbuf;
2833 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2835 fp->f_offset = kuio.uio_offset;
2837 readcnt = SCARG(uap, count) - kuio.uio_resid;
2838 edp = (struct dirent *)&dirbuf[readcnt];
2839 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2840 # if (BYTE_ORDER == LITTLE_ENDIAN)
2842 * The expected low byte of
2843 * dp->d_namlen is our dp->d_type.
2844 * The high MBZ byte of dp->d_namlen
2845 * is our dp->d_namlen.
2847 dp->d_type = dp->d_namlen;
2851 * The dp->d_type is the high byte
2852 * of the expected dp->d_namlen,
2853 * so must be zero'ed.
2857 if (dp->d_reclen > 0) {
2858 dp = (struct dirent *)
2859 ((char *)dp + dp->d_reclen);
2866 error = uiomove(dirbuf, readcnt, &auio);
2868 FREE(dirbuf, M_TEMP);
2870 VOP_UNLOCK(vp, 0, p);
2873 if (SCARG(uap, count) == auio.uio_resid) {
2874 if (union_dircheckp) {
2875 error = union_dircheckp(p, &vp, fp);
2881 if ((vp->v_flag & VROOT) &&
2882 (vp->v_mount->mnt_flag & MNT_UNION)) {
2883 struct vnode *tvp = vp;
2884 vp = vp->v_mount->mnt_vnodecovered;
2886 fp->f_data = (caddr_t) vp;
2892 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2894 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2897 #endif /* COMPAT_43 */
2900 * Read a block of directory entries in a file system independent format.
2902 #ifndef _SYS_SYSPROTO_H_
2903 struct getdirentries_args {
2911 getdirentries(p, uap)
2913 register struct getdirentries_args /* {
2915 syscallarg(char *) buf;
2916 syscallarg(u_int) count;
2917 syscallarg(long *) basep;
2927 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2929 if ((fp->f_flag & FREAD) == 0)
2931 vp = (struct vnode *)fp->f_data;
2933 if (vp->v_type != VDIR)
2935 aiov.iov_base = SCARG(uap, buf);
2936 aiov.iov_len = SCARG(uap, count);
2937 auio.uio_iov = &aiov;
2938 auio.uio_iovcnt = 1;
2939 auio.uio_rw = UIO_READ;
2940 auio.uio_segflg = UIO_USERSPACE;
2942 auio.uio_resid = SCARG(uap, count);
2943 /* vn_lock(vp, LK_SHARED | LK_RETRY, p); */
2944 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2945 loff = auio.uio_offset = fp->f_offset;
2946 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2947 fp->f_offset = auio.uio_offset;
2948 VOP_UNLOCK(vp, 0, p);
2951 if (SCARG(uap, count) == auio.uio_resid) {
2952 if (union_dircheckp) {
2953 error = union_dircheckp(p, &vp, fp);
2959 if ((vp->v_flag & VROOT) &&
2960 (vp->v_mount->mnt_flag & MNT_UNION)) {
2961 struct vnode *tvp = vp;
2962 vp = vp->v_mount->mnt_vnodecovered;
2964 fp->f_data = (caddr_t) vp;
2970 if (SCARG(uap, basep) != NULL) {
2971 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2974 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2977 #ifndef _SYS_SYSPROTO_H_
2978 struct getdents_args {
2987 register struct getdents_args /* {
2989 syscallarg(char *) buf;
2990 syscallarg(u_int) count;
2993 struct getdirentries_args ap;
2996 ap.count = uap->count;
2998 return getdirentries(p, &ap);
3002 * Set the mode mask for creation of filesystem nodes.
3004 #ifndef _SYS_SYSPROTO_H_
3012 struct umask_args /* {
3013 syscallarg(int) newmask;
3016 register struct filedesc *fdp;
3019 p->p_retval[0] = fdp->fd_cmask;
3020 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
3025 * Void all references to file by ripping underlying filesystem
3028 #ifndef _SYS_SYSPROTO_H_
3029 struct revoke_args {
3037 register struct revoke_args /* {
3038 syscallarg(char *) path;
3041 register struct vnode *vp;
3044 struct nameidata nd;
3046 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3047 if ((error = namei(&nd)) != 0)
3050 NDFREE(&nd, NDF_ONLY_PNBUF);
3051 if (vp->v_type != VCHR && vp->v_type != VBLK) {
3055 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
3057 if (p->p_ucred->cr_uid != vattr.va_uid &&
3058 (error = suser_xxx(0, p, PRISON_ROOT)))
3061 VOP_REVOKE(vp, REVOKEALL);
3068 * Convert a user file descriptor to a kernel file entry.
3071 getvnode(fdp, fd, fpp)
3072 struct filedesc *fdp;
3078 if ((u_int)fd >= fdp->fd_nfiles ||
3079 (fp = fdp->fd_ofiles[fd]) == NULL)
3081 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
3087 * Get (NFS) file handle
3089 #ifndef _SYS_SYSPROTO_H_
3098 register struct getfh_args *uap;
3100 struct nameidata nd;
3102 register struct vnode *vp;
3106 * Must be super user
3111 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
3115 NDFREE(&nd, NDF_ONLY_PNBUF);
3117 bzero(&fh, sizeof(fh));
3118 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3119 error = VFS_VPTOFH(vp, &fh.fh_fid);
3123 error = copyout(&fh, uap->fhp, sizeof (fh));
3128 * syscall for the rpc.lockd to use to translate a NFS file handle into
3129 * an open descriptor.
3131 * warning: do not remove the suser() call or this becomes one giant
3134 #ifndef _SYS_SYSPROTO_H_
3135 struct fhopen_args {
3136 const struct fhandle *u_fhp;
3143 struct fhopen_args /* {
3144 syscallarg(const struct fhandle *) u_fhp;
3145 syscallarg(int) flags;
3152 struct vattr *vap = &vat;
3155 register struct filedesc *fdp = p->p_fd;
3156 int fmode, mode, error, type;
3161 * Must be super user
3167 fmode = FFLAGS(SCARG(uap, flags));
3168 /* why not allow a non-read/write open for our lockd? */
3169 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3171 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
3174 /* find the mount point */
3175 mp = vfs_getvfs(&fhp.fh_fsid);
3178 /* now give me my vnode, it gets returned to me locked */
3179 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3183 * from now on we have to make sure not
3184 * to forget about the vnode
3185 * any error that causes an abort must vput(vp)
3186 * just set error = err and 'goto bad;'.
3192 if (vp->v_type == VLNK) {
3196 if (vp->v_type == VSOCK) {
3201 if (fmode & (FWRITE | O_TRUNC)) {
3202 if (vp->v_type == VDIR) {
3206 error = vn_writechk(vp);
3214 error = VOP_ACCESS(vp, mode, p->p_ucred, p);
3218 if (fmode & O_TRUNC) {
3219 VOP_UNLOCK(vp, 0, p); /* XXX */
3220 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
3221 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */
3224 error = VOP_SETATTR(vp, vap, p->p_ucred, p);
3228 error = VOP_OPEN(vp, fmode, p->p_ucred, p);
3232 * Make sure that a VM object is created for VMIO support.
3234 if (vn_canvmio(vp) == TRUE) {
3235 if ((error = vfs_object_create(vp, p, p->p_ucred)) != 0)
3242 * end of vn_open code
3245 if ((error = falloc(p, &nfp, &indx)) != 0)
3248 nfp->f_data = (caddr_t)vp;
3249 nfp->f_flag = fmode & FMASK;
3250 nfp->f_ops = &vnops;
3251 nfp->f_type = DTYPE_VNODE;
3252 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3253 lf.l_whence = SEEK_SET;
3256 if (fmode & O_EXLOCK)
3257 lf.l_type = F_WRLCK;
3259 lf.l_type = F_RDLCK;
3261 if ((fmode & FNONBLOCK) == 0)
3263 VOP_UNLOCK(vp, 0, p);
3264 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3265 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
3267 fdp->fd_ofiles[indx] = NULL;
3270 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
3271 fp->f_flag |= FHASLOCK;
3273 if ((vp->v_type == VREG) && (vp->v_object == NULL))
3274 vfs_object_create(vp, p, p->p_ucred);
3276 VOP_UNLOCK(vp, 0, p);
3277 p->p_retval[0] = indx;
3285 #ifndef _SYS_SYSPROTO_H_
3286 struct fhstat_args {
3287 struct fhandle *u_fhp;
3294 register struct fhstat_args /* {
3295 syscallarg(struct fhandle *) u_fhp;
3296 syscallarg(struct stat *) sb;
3306 * Must be super user
3312 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3316 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3318 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3320 error = vn_stat(vp, &sb, p);
3324 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3328 #ifndef _SYS_SYSPROTO_H_
3329 struct fhstatfs_args {
3330 struct fhandle *u_fhp;
3337 struct fhstatfs_args /* {
3338 syscallarg(struct fhandle) *u_fhp;
3339 syscallarg(struct statfs) *buf;
3350 * Must be super user
3352 if ((error = suser(p)))
3355 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3358 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3360 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3365 if ((error = VFS_STATFS(mp, sp, p)) != 0)
3367 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3368 if (suser_xxx(p->p_ucred, 0, 0)) {
3369 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3370 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3373 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3377 * Syscall to push extended attribute configuration information into the
3378 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3379 * a command (int cmd), and attribute name and misc data. For now, the
3380 * attribute name is left in userspace for consumption by the VFS_op.
3381 * It will probably be changed to be copied into sysspace by the
3382 * syscall in the future, once issues with various consumers of the
3383 * attribute code have raised their hands.
3385 * Currently this is used only by UFS Extended Attributes.
3390 struct extattrctl_args *uap;
3392 struct nameidata nd;
3396 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3397 if ((error = namei(&nd)) != 0)
3399 mp = nd.ni_vp->v_mount;
3401 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3402 SCARG(uap, arg), p));
3406 * Syscall to set a named extended attribute on a file or directory.
3407 * Accepts attribute name, and a uio structure pointing to the data to set.
3408 * The uio is consumed in the style of writev(). The real work happens
3409 * in VOP_SETEXTATTR().
3412 extattr_set_file(p, uap)
3414 struct extattr_set_file_args *uap;
3416 struct nameidata nd;
3418 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3419 char attrname[EXTATTR_MAXNAMELEN];
3423 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3426 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE, SCARG(uap, path),
3428 if ((error = namei(&nd)) != 0)
3430 iovlen = uap->iovcnt * sizeof(struct iovec);
3431 if (uap->iovcnt > UIO_SMALLIOV) {
3432 if (uap->iovcnt > UIO_MAXIOV) {
3436 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3441 auio.uio_iovcnt = uap->iovcnt;
3442 auio.uio_rw = UIO_WRITE;
3443 auio.uio_segflg = UIO_USERSPACE;
3445 auio.uio_offset = 0;
3446 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3449 for (i = 0; i < uap->iovcnt; i++) {
3450 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3454 auio.uio_resid += iov->iov_len;
3457 cnt = auio.uio_resid;
3458 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_cred->pc_ucred,
3460 if (auio.uio_resid != cnt && (error == ERESTART ||
3461 error == EINTR || error == EWOULDBLOCK))
3463 cnt -= auio.uio_resid;
3464 p->p_retval[0] = cnt;
3467 FREE(needfree, M_IOV);
3473 * Syscall to get a named extended attribute on a file or directory.
3474 * Accepts attribute name, and a uio structure pointing to a buffer for the
3475 * data. The uio is consumed in the style of readv(). The real work
3476 * happens in VOP_GETEXTATTR();
3479 extattr_get_file(p, uap)
3481 struct extattr_get_file_args *uap;
3483 struct nameidata nd;
3485 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3486 char attrname[EXTATTR_MAXNAMELEN];
3490 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3493 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3494 if ((error = namei(&nd)) != 0)
3496 iovlen = uap->iovcnt * sizeof (struct iovec);
3497 if (uap->iovcnt > UIO_SMALLIOV) {
3498 if (uap->iovcnt > UIO_MAXIOV) {
3502 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3509 auio.uio_iovcnt = uap->iovcnt;
3510 auio.uio_rw = UIO_READ;
3511 auio.uio_segflg = UIO_USERSPACE;
3513 auio.uio_offset = 0;
3514 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3517 for (i = 0; i < uap->iovcnt; i++) {
3518 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3522 auio.uio_resid += iov->iov_len;
3525 cnt = auio.uio_resid;
3526 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_cred->pc_ucred,
3528 if (auio.uio_resid != cnt && (error == ERESTART ||
3529 error == EINTR || error == EWOULDBLOCK))
3531 cnt -= auio.uio_resid;
3532 p->p_retval[0] = cnt;
3535 FREE(needfree, M_IOV);
3541 * Syscall to delete a named extended attribute from a file or directory.
3542 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3545 extattr_delete_file(p, uap)
3547 struct extattr_delete_file_args *uap;
3549 struct nameidata nd;
3550 char attrname[EXTATTR_MAXNAMELEN];
3553 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3556 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE, SCARG(uap, path),
3558 if ((error = namei(&nd)) != 0)
3560 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_cred->pc_ucred,